From 5871dc652f2bcd7915d73a32b1d4703ec75f1219 Mon Sep 17 00:00:00 2001 From: Sean Hellum Date: Tue, 14 Jan 2020 13:53:23 +0000 Subject: [PATCH 1/2] instead of `defer f.close()` just move `f.close` to the botttom --- internal/history.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/history.go b/internal/history.go index 9f69a67b..3a55eee8 100644 --- a/internal/history.go +++ b/internal/history.go @@ -54,8 +54,8 @@ func UpdateHistory(command string) { if err != nil { log.Println(err) } - defer f.Close() if _, err := f.WriteString("\n" + command); err != nil { log.Println(err) } + f.Close() } From 6c17ba55b398ca2c3f3e891d3b588a86c8b04bbc Mon Sep 17 00:00:00 2001 From: Sean Hellum Date: Tue, 14 Jan 2020 13:56:18 +0000 Subject: [PATCH 2/2] rename variables --- internal/complete.go | 8 ++++---- internal/history.go | 12 ++++++------ 2 files changed, 10 insertions(+), 10 deletions(-) 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 3a55eee8..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,8 +48,8 @@ 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)