-
Notifications
You must be signed in to change notification settings - Fork 0
/
macos-apps.go
90 lines (71 loc) · 1.82 KB
/
macos-apps.go
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
package main
import (
"log"
"os"
"path/filepath"
"runtime"
"strings"
)
const rootAppsDir = "/Applications/"
const macosSysApps = "macos-system-apps"
const macosUserApps = "macos-user-apps"
func getApps(targetDirPath string) (string, error) {
dirEntries, err := os.ReadDir(targetDirPath)
if err != nil {
return "", err
}
var entryNames []string
for _, entry := range dirEntries {
appName := entry.Name()
if !strings.HasPrefix(appName, ".") {
entryNames = append(entryNames, appName)
}
}
return strings.Join(entryNames, "\n"), nil
}
func saveApps(appsPath, baseTargetDir, listType, appsType string) {
appsList, err := getApps(appsPath)
if err != nil {
log.Printf("%v", err)
// return err
}
targetDir := filepath.Join(baseTargetDir, listType)
filePath := filepath.Join(targetDir, appsType)
err = os.MkdirAll(targetDir, 0755)
if err != nil {
log.Printf("%v", err)
// return err
}
err = SaveFileContent(appsList, filePath)
if err != nil {
log.Printf("%v", err)
// return err
}
// return nil
}
func saveCurrentAppsList(targetDir string) {
// system apps @ current
saveApps(rootAppsDir, targetDir, ListTypeCurrent, macosSysApps)
// user apps @ current
appsDir := filepath.Join(GetHomeDir(), rootAppsDir)
saveApps(appsDir, targetDir, ListTypeCurrent, macosUserApps)
}
func saveAllTimeAppsList(/*targetDir string*/) {
// TODO
log.Println("[macos-apps.go::saveAllTimeAppsList] not implemented yet")
}
func doesEnvMatch() bool {
return runtime.GOOS == "darwin"
}
func SaveMacOsApps(targetDir string) {
if !doesEnvMatch() {
log.Printf("SaveCurrentMacOsApps: current platform %s is not suported\n", runtime.GOOS)
return
}
saveCurrentAppsList(targetDir)
saveAllTimeAppsList(/*targetDir*/)
}
// if error := saveUserApps(targetDir); error != nil {
// log.Printf("%v", error)
// return error
// }