From 9282eeb18ec329e46c3de1be8330b33ea2d3245e Mon Sep 17 00:00:00 2001 From: Samuel Pua Date: Fri, 5 May 2023 23:04:30 +0800 Subject: [PATCH] Feat(create-desktop-shortcut): Initial create-desktop-shortcut --- create-desktop-shortcut/desktopformat.go | 17 ++++ create-desktop-shortcut/go.mod | 5 ++ create-desktop-shortcut/go.sum | 2 + create-desktop-shortcut/main.go | 98 ++++++++++++++++++++++++ install.sh | 6 ++ uninstall.sh | 3 + 6 files changed, 131 insertions(+) create mode 100644 create-desktop-shortcut/desktopformat.go create mode 100644 create-desktop-shortcut/go.mod create mode 100644 create-desktop-shortcut/go.sum create mode 100644 create-desktop-shortcut/main.go diff --git a/create-desktop-shortcut/desktopformat.go b/create-desktop-shortcut/desktopformat.go new file mode 100644 index 0000000..2e7c531 --- /dev/null +++ b/create-desktop-shortcut/desktopformat.go @@ -0,0 +1,17 @@ +package main + +import "fmt" + +func generateDesktopFormat(name string, exec string, icon string) string { + desktopBaseFormat := `[Desktop Entry] +Version=1.0 +Type=Application +Terminal=false +Name=%s +GenericName=%s +Exec=%s +Icon=%s +StartupNotify=true` + createdDesktopFormat := fmt.Sprintf(desktopBaseFormat, name, name, exec, icon) + return createdDesktopFormat +} diff --git a/create-desktop-shortcut/go.mod b/create-desktop-shortcut/go.mod new file mode 100644 index 0000000..cc57cee --- /dev/null +++ b/create-desktop-shortcut/go.mod @@ -0,0 +1,5 @@ +module create-desktop-shortcut + +go 1.20 + +require github.com/akamensky/argparse v1.4.0 diff --git a/create-desktop-shortcut/go.sum b/create-desktop-shortcut/go.sum new file mode 100644 index 0000000..5bf91e2 --- /dev/null +++ b/create-desktop-shortcut/go.sum @@ -0,0 +1,2 @@ +github.com/akamensky/argparse v1.4.0 h1:YGzvsTqCvbEZhL8zZu2AiA5nq805NZh75JNj4ajn1xc= +github.com/akamensky/argparse v1.4.0/go.mod h1:S5kwC7IuDcEr5VeXtGPRVZ5o/FdhcMlQz4IZQuw64xA= diff --git a/create-desktop-shortcut/main.go b/create-desktop-shortcut/main.go new file mode 100644 index 0000000..048a583 --- /dev/null +++ b/create-desktop-shortcut/main.go @@ -0,0 +1,98 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "os" + "strings" + + "github.com/akamensky/argparse" +) + +func main() { + parser := argparse.NewParser("create-desktop-shortcut", "Create Linux desktop shortcut") + filepath := parser.StringPositional(nil) + entryName := parser.String("n", "name", &argparse.Options{Required: false, Help: "Name of the desktop entry"}) + iconPath := parser.String("i", "icon", &argparse.Options{Required: false, Help: "Path to the icon"}) + + err := parser.Parse(os.Args) + if err != nil { + fmt.Println(parser.Usage(err)) + return + } + + // Start generating all options + if *entryName == "" { + *entryName = *filepath + } + + currPath, err := os.Getwd() + if err != nil { + log.Fatal(err) + } + + if !strings.HasPrefix(*filepath, "/") { + *filepath = currPath + "/" + *filepath + } + + if *iconPath == "" { + *iconPath = *filepath + } else if !strings.HasPrefix(*iconPath, "/") { + *iconPath = currPath + "/" + *iconPath + } + + if !strings.HasPrefix(*filepath, "/") { + *filepath = currPath + "/" + *filepath + } + // End generating all options + + reader := bufio.NewReader(os.Stdin) + fmt.Printf("Confirm if the filepath is %s [Y/n]: ", *filepath) + userChoice, err := reader.ReadString('\n') + if err != nil { + log.Fatal(err) + } + + if userChoice == "\n" || strings.ToUpper(userChoice) == "Y\n" { + fmt.Printf("Confirming the filepath (%s)...\n", *filepath) + } else { + fmt.Println("Aborted") + return + } + + desktopFilepath := "/.local/share/applications/" + *entryName + ".desktop" + homePath, err := os.UserHomeDir() + if err != nil { + log.Fatal(err) + } + + desktopFilepath = homePath + desktopFilepath + + fmt.Printf("\n") + fmt.Printf("Program Path: %s\n", *filepath) + fmt.Printf("Program Name: %s\n", *entryName) + fmt.Printf("Icon Path: %s\n", *iconPath) + fmt.Printf("\n") + + fmt.Printf("Confirm if the file should be auto-generated in %s [Y/n]: ", desktopFilepath) + userChoice, err = reader.ReadString('\n') + if err != nil { + log.Fatal(err) + } + + generatedDesktopFormat := generateDesktopFormat(*entryName, *filepath, *iconPath) + + if userChoice == "\n" || strings.ToUpper(userChoice) == "Y\n" { + err = os.WriteFile(*&desktopFilepath, []byte(generatedDesktopFormat), 0644) + if err != nil { + log.Fatal(err) + } + fmt.Printf("\nFile created successfully to %s", *&desktopFilepath) + } else { + fmt.Println("\nGenerated desktop format:\n") + fmt.Println(generatedDesktopFormat) + return + } + +} diff --git a/install.sh b/install.sh index 7b4463a..25a2606 100755 --- a/install.sh +++ b/install.sh @@ -24,3 +24,9 @@ sudo cp ./permutateText/permutateText.py /usr/local/bin/ echo "Installing file2var" sudo cp ./file2var/file2var /usr/local/bin/ + +echo "Installing create-desktop-shortcut" +cd create-desktop-shortcut +sudo go build +sudo mv create-desktop-shortcut /usr/local/bin/ +cd .. \ No newline at end of file diff --git a/uninstall.sh b/uninstall.sh index 218d5f4..73b22b0 100755 --- a/uninstall.sh +++ b/uninstall.sh @@ -24,3 +24,6 @@ sudo rm /usr/local/bin/permutateText.py echo "Deleting file2var" sudo rm /usr/local/bin/file2var + +echo "Deleting create-desktop-shortcut" +sudo rm /usr/local/bin/create-desktop-shortcut