-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselfinstall.go
More file actions
105 lines (85 loc) · 2.94 KB
/
Copy pathselfinstall.go
File metadata and controls
105 lines (85 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"appy/appimage"
)
const desktopFileContent = `[Desktop Entry]
Type=Application
Name=Appy
Comment=AppImage installer and manager
Exec=%s %%f
Icon=application-x-appimage
Terminal=false
Categories=Utility;
MimeType=application/x-iso9660-appimage;application/vnd.appimage;application/x-appimage;
`
func installAppy() error {
homeDir, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("cannot determine home directory: %w", err)
}
// Get path to current executable
execPath, err := os.Executable()
if err != nil {
return fmt.Errorf("cannot determine executable path: %w", err)
}
execPath, err = filepath.EvalSymlinks(execPath)
if err != nil {
return fmt.Errorf("cannot resolve executable path: %w", err)
}
targetBinaryPath := filepath.Join(homeDir, "Applications", "appy")
targetDesktopPath := filepath.Join(homeDir, ".local/share/applications", "appy.desktop")
// Create ~/Applications directory
if err := os.MkdirAll(filepath.Dir(targetBinaryPath), 0755); err != nil {
return fmt.Errorf("cannot create Applications directory: %w", err)
}
// Copy binary
if err := appimage.CopyFile(execPath, targetBinaryPath, 0755); err != nil {
return fmt.Errorf("cannot copy binary: %w", err)
}
// Create desktop file directory
if err := os.MkdirAll(filepath.Dir(targetDesktopPath), 0755); err != nil {
return fmt.Errorf("cannot create applications directory: %w", err)
}
// Write desktop file
desktopContent := fmt.Sprintf(desktopFileContent, targetBinaryPath)
if err := os.WriteFile(targetDesktopPath, []byte(desktopContent), 0644); err != nil {
_ = os.Remove(targetBinaryPath)
return fmt.Errorf("cannot write desktop file: %w", err)
}
// Update desktop database (ignore errors - command may not exist)
_ = exec.Command("update-desktop-database", filepath.Dir(targetDesktopPath)).Run()
// Set as default handler for AppImage MIME types
for _, mimeType := range []string{
"application/x-iso9660-appimage",
"application/vnd.appimage",
"application/x-appimage",
} {
_ = exec.Command("xdg-mime", "default", "appy.desktop", mimeType).Run()
}
return nil
}
func uninstallAppy() error {
homeDir, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("cannot determine home directory: %w", err)
}
targetBinaryPath := filepath.Join(homeDir, "Applications", "appy")
targetDesktopPath := filepath.Join(homeDir, ".local/share/applications", "appy.desktop")
// Check if installed
if _, err := os.Stat(targetBinaryPath); os.IsNotExist(err) {
return fmt.Errorf("appy is not installed")
}
// Remove binary
if err := os.Remove(targetBinaryPath); err != nil {
return fmt.Errorf("cannot remove binary: %w", err)
}
// Remove desktop file (ignore error if it doesn't exist)
_ = os.Remove(targetDesktopPath)
// Update desktop database (ignore errors - command may not exist)
_ = exec.Command("update-desktop-database", filepath.Dir(targetDesktopPath)).Run()
return nil
}