Skip to content

Commit

Permalink
Fix binary path resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
sansmoraxz committed Jun 12, 2024
1 parent c7d26bd commit c0dcbc6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
18 changes: 13 additions & 5 deletions cmd/godm/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"os"
"path/filepath"
"runtime/debug"
"strings"
)
Expand All @@ -18,12 +19,19 @@ var version = func() string {
return ""
}()

func getCurrentBinaryName() string {
func getCurrentBinaryName() (string, error) {
currentBinaryPath, err := os.Executable()
if err != nil {
println("Error getting current binary path:", err)
os.Exit(1)
return "", err
}
return strings.Split(currentBinaryPath, string(os.PathSeparator))[len(strings.Split(currentBinaryPath, string(os.PathSeparator)))-1]
currentBinaryPath, err = filepath.EvalSymlinks(currentBinaryPath)
if ; err != nil {
return "", err
}
currentBinaryPath = filepath.Base(currentBinaryPath)
// whitespace is not allowed in the binary name
if strings.Contains(currentBinaryPath, " ") {
return "", os.ErrInvalid
}
return currentBinaryPath, nil
}

6 changes: 5 additions & 1 deletion cmd/godm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ import (
)

func rootCmd() *cobra.Command {
binPath, err := getCurrentBinaryName()
if err != nil {
binPath = "godm"
}
rootCmd := &cobra.Command{
Use: getCurrentBinaryName(),
Use: binPath,
Short: "A download manager for large files",
}

Expand Down

0 comments on commit c0dcbc6

Please sign in to comment.