/
iconize.go
67 lines (60 loc) · 1.41 KB
/
iconize.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
package main
import (
"strings"
"fmt"
"os"
"os/user"
"flag"
)
func checkArgs() (string, string, string) {
namePtr := flag.String("n", "", "Name")
pathPtr := flag.String("p", "", "Path")
iconPtr := flag.String("i", "", "Icon")
flag.Parse()
if *namePtr == "" {
fmt.Println("Name required! (run iconize -h for help)")
os.Exit(1)
}
if *pathPtr == "" {
fmt.Println("Path required! (run iconize -h for help)")
os.Exit(1)
}
return *namePtr, *pathPtr, *iconPtr
}
func createDesktopEntry(name string) *os.File {
name = strings.ToLower(name)
name = strings.Replace(name, " ", "-", -1) // replace all spaces
user, err := user.Current()
home := user.HomeDir
path := home + "/.local/share/applications/" + name + ".desktop"
entry, err := os.Create(path)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return entry
}
func write(where *os.File, what string) {
_, err := where.WriteString(what + "\n")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func fill(entry *os.File, name, path, icon string) {
write(entry, "[Desktop Entry]")
write(entry, "Type=Application")
write(entry, "Name=" + name)
write(entry, "Exec=" + path + " %U")
write(entry, "Icon=" + icon)
}
func close(file *os.File) {
file.Sync()
file.Close()
}
func main() {
name, path, icon := checkArgs()
entry := createDesktopEntry(name)
fill(entry, name, path, icon)
close(entry)
}