diff --git a/src/environment.go b/src/environment.go index 2dc18156..faee3f8a 100644 --- a/src/environment.go +++ b/src/environment.go @@ -10,6 +10,7 @@ import ( "path/filepath" "runtime" "strings" + "sync" "time" "github.com/distatus/battery" @@ -65,11 +66,15 @@ type environmentInfo interface { } type environment struct { - args *args - cwd string - commands map[string]string + args *args + cwd string } +var ( + commands map[string]string = make(map[string]string) + lock = sync.Mutex{} +) + func (env *environment) getenv(key string) string { return os.Getenv(key) } @@ -160,7 +165,7 @@ func (env *environment) getPlatform() string { } func (env *environment) runCommand(command string, args ...string) (string, error) { - if cmd, ok := env.commands[command]; ok { + if cmd, ok := commands[command]; ok { command = cmd } out, err := exec.Command(command, args...).Output() @@ -176,12 +181,14 @@ func (env *environment) runShellCommand(shell, command string) string { } func (env *environment) hasCommand(command string) bool { - if _, ok := env.commands[command]; ok { + if _, ok := commands[command]; ok { return true } path, err := exec.LookPath(command) if err == nil { - env.commands[command] = path + lock.Lock() + commands[command] = path + lock.Unlock() return true } return false diff --git a/src/main.go b/src/main.go index dcf9672c..5f0e91c8 100644 --- a/src/main.go +++ b/src/main.go @@ -101,8 +101,7 @@ func main() { } flag.Parse() env := &environment{ - args: args, - commands: make(map[string]string), + args: args, } if *args.Millis { fmt.Print(time.Now().UnixNano() / 1000000) diff --git a/src/segment_command_test.go b/src/segment_command_test.go index 9bb86c7b..09c2bf11 100644 --- a/src/segment_command_test.go +++ b/src/segment_command_test.go @@ -7,9 +7,7 @@ import ( ) func TestExecuteCommand(t *testing.T) { - env := &environment{ - commands: make(map[string]string), - } + env := &environment{} props := &properties{ values: map[Property]interface{}{ Command: "echo hello", @@ -25,9 +23,7 @@ func TestExecuteCommand(t *testing.T) { } func TestExecuteMultipleCommandsOrFirst(t *testing.T) { - env := &environment{ - commands: make(map[string]string), - } + env := &environment{} props := &properties{ values: map[Property]interface{}{ Command: "exit 1 || echo hello", @@ -43,9 +39,7 @@ func TestExecuteMultipleCommandsOrFirst(t *testing.T) { } func TestExecuteMultipleCommandsOrSecond(t *testing.T) { - env := &environment{ - commands: make(map[string]string), - } + env := &environment{} props := &properties{ values: map[Property]interface{}{ Command: "echo hello || echo world", @@ -61,9 +55,7 @@ func TestExecuteMultipleCommandsOrSecond(t *testing.T) { } func TestExecuteMultipleCommandsAnd(t *testing.T) { - env := &environment{ - commands: make(map[string]string), - } + env := &environment{} props := &properties{ values: map[Property]interface{}{ Command: "echo hello && echo world", @@ -79,9 +71,7 @@ func TestExecuteMultipleCommandsAnd(t *testing.T) { } func TestExecuteSingleCommandEmpty(t *testing.T) { - env := &environment{ - commands: make(map[string]string), - } + env := &environment{} props := &properties{ values: map[Property]interface{}{ Command: "", @@ -96,9 +86,7 @@ func TestExecuteSingleCommandEmpty(t *testing.T) { } func TestExecuteSingleCommandNoCommandProperty(t *testing.T) { - env := &environment{ - commands: make(map[string]string), - } + env := &environment{} props := &properties{} c := &command{ props: props, @@ -110,9 +98,7 @@ func TestExecuteSingleCommandNoCommandProperty(t *testing.T) { } func TestExecuteMultipleCommandsAndDisabled(t *testing.T) { - env := &environment{ - commands: make(map[string]string), - } + env := &environment{} props := &properties{ values: map[Property]interface{}{ Command: "echo && echo", @@ -127,9 +113,7 @@ func TestExecuteMultipleCommandsAndDisabled(t *testing.T) { } func TestExecuteMultipleCommandsOrDisabled(t *testing.T) { - env := &environment{ - commands: make(map[string]string), - } + env := &environment{} props := &properties{ values: map[Property]interface{}{ Command: "echo|| echo",