6 changed files with 131 additions and 0 deletions
@ -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 |
||||
|
} |
@ -0,0 +1,5 @@ |
|||||
|
module create-desktop-shortcut |
||||
|
|
||||
|
go 1.20 |
||||
|
|
||||
|
require github.com/akamensky/argparse v1.4.0 |
@ -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= |
@ -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 |
||||
|
} |
||||
|
|
||||
|
} |
Loading…
Reference in new issue