diff --git a/bash.go b/bash.go index 118a635..51f39a7 100644 --- a/bash.go +++ b/bash.go @@ -9,6 +9,7 @@ import ( "go.wit.com/dev/alexflint/arg" "go.wit.com/lib/gui/shell" + "go.wit.com/log" ) var argBash ArgsBash @@ -57,21 +58,31 @@ func Bash(argname string, autocomplete func([]string)) *BashAuto { // makes a bash autocomplete file for your command func doBash(argname string) { - if homeDir, err := os.UserHomeDir(); err == nil { - filename := filepath.Join(homeDir, ".local/share/bash-completion/completions", argname) - if shell.Exists(filename) { - } else { - if f, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); err == nil { - f.Write([]byte(makeBashCompletionText(argname))) - f.Close() - } - fmt.Println("bash file created:", filename) - } - } - fmt.Println(makeBashCompletionText(argname)) - fmt.Println("restart bash") + homeDir, err := os.UserHomeDir() + if err != nil { + log.Printf("%v\n", err) + os.Exit(0) + } + filename := filepath.Join(homeDir, ".local/share/bash-completion/completions", argname) + if shell.Exists(filename) { + log.Println(filename, "file already exists") + os.Exit(0) + } + basedir, _ := filepath.Split(filename) + if !shell.IsDir(basedir) { + os.MkdirAll(basedir, os.ModePerm) + } + + if f, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); err == nil { + f.Write([]byte(makeBashCompletionText(argname))) + f.Close() + log.Println("bash file created:", filename) + log.Println("restart bash") + } else { + log.Info(filename, err) + } os.Exit(0) }