Feat(create-desktop-shortcut): Initial create-desktop-shortcut

This commit is contained in:
2023-05-05 23:04:30 +08:00
parent 2f7b619b3b
commit 9282eeb18e
6 changed files with 131 additions and 0 deletions

View File

@@ -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
}

View File

@@ -0,0 +1,5 @@
module create-desktop-shortcut
go 1.20
require github.com/akamensky/argparse v1.4.0

View File

@@ -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=

View File

@@ -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
}
}

View File

@@ -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 ..

View File

@@ -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