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