diff --git a/internal/complete.go b/internal/complete.go index 53924d45..bdb417ab 100644 --- a/internal/complete.go +++ b/internal/complete.go @@ -24,8 +24,8 @@ func Unique(intSlice []prompt.Suggest) []prompt.Suggest { // Completer complete commands func Completer(d prompt.Document) []prompt.Suggest { s := []prompt.Suggest{{Text: "help", Description: "gosh"}, {Text: "exit", Description: "gosh"}, {Text: "history", Description: "gosh"}, {Text: "clearhist", Description: "gosh"}, {Text: "tree", Description: "gosh"}, {Text: "touch", Description: "gosh"}, {Text: "mkdir", Description: "gosh"}} - var gopath string = os.Getenv("GOSH_HOME") - file, _ := os.Open(gopath + "/history.txt") + var goshHome string = os.Getenv("GOSH_HOME") + file, _ := os.Open(goshHome + "/history.txt") scanner := bufio.NewScanner(file) for scanner.Scan() { if strings.Compare(string(scanner.Text()), "") == 0 { @@ -51,8 +51,8 @@ func Completer(d prompt.Document) []prompt.Suggest { // GetCommandHist Get command History func GetCommandHist() []string { s := []string{} - var gopath string = os.Getenv("GOSH_HOME") - file, _ := os.Open(gopath + "/history.txt") + var goshHome string = os.Getenv("GOSH_HOME") + file, _ := os.Open(goshHome + "/history.txt") scanner := bufio.NewScanner(file) for scanner.Scan() { if strings.Compare(string(scanner.Text()), "") == 0 { diff --git a/internal/history.go b/internal/history.go index 9f69a67b..b3c7bdf5 100644 --- a/internal/history.go +++ b/internal/history.go @@ -11,8 +11,8 @@ import ( // ClearHistory clears the command history func ClearHistory() string { - var gopath string = os.Getenv("GOSH_HOME") - f, _ := os.OpenFile(gopath+"/history.txt", + var goshHome string = os.Getenv("GOSH_HOME") + f, _ := os.OpenFile(goshHome+"/history.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) f.Truncate(0) fmt.Println("\033[0;32mHistory has been cleared ✔\033[0m") @@ -21,8 +21,8 @@ func ClearHistory() string { // History the history command func History() { - var gopath string = os.Getenv("GOSH_HOME") - file, _ := os.Open(gopath + "/history.txt") + var goshHome string = os.Getenv("GOSH_HOME") + file, _ := os.Open(goshHome + "/history.txt") scanner := bufio.NewScanner(file) var num = 1 fmt.Println(" \033[0;32m# command\033[0m") @@ -48,14 +48,14 @@ func History() { // UpdateHistory update the command history func UpdateHistory(command string) { - var gopath string = os.Getenv("GOSH_HOME") - f, err := os.OpenFile(gopath+"/history.txt", + var goshHome string = os.Getenv("GOSH_HOME") + f, err := os.OpenFile(goshHome+"/history.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) if err != nil { log.Println(err) } - defer f.Close() if _, err := f.WriteString("\n" + command); err != nil { log.Println(err) } + f.Close() }