diff --git a/src/environment.go b/src/environment.go index 8c713acfb26e..1537a0483fe5 100644 --- a/src/environment.go +++ b/src/environment.go @@ -38,7 +38,7 @@ type environmentInfo interface { getHostName() (string, error) getRuntimeGOOS() string getPlatform() string - hasCommand(command string) bool + hasCommand(command string) (string, bool) runCommand(command string, args ...string) (string, error) runShellCommand(shell, command string) string lastErrorCode() int @@ -192,9 +192,9 @@ func (env *environment) runShellCommand(shell, command string) string { return strings.TrimSpace(string(out)) } -func (env *environment) hasCommand(command string) bool { - _, err := exec.LookPath(command) - return err == nil +func (env *environment) hasCommand(command string) (string, bool) { + path, err := exec.LookPath(command) + return path, err == nil } func (env *environment) lastErrorCode() int { diff --git a/src/segment_az.go b/src/segment_az.go index f8a73a9a55ac..8a3baa7c38ba 100644 --- a/src/segment_az.go +++ b/src/segment_az.go @@ -36,11 +36,12 @@ func (a *az) init(props *properties, env environmentInfo) { } func (a *az) enabled() bool { - if (!a.idEnabled() && !a.nameEnabled()) || !a.env.hasCommand("az") { + commandPath, commandExists := a.env.hasCommand("az") + if (!a.idEnabled() && !a.nameEnabled()) || !commandExists { return false } - output, _ := a.env.runCommand("az", "account", "show", "--query=[name,id]", "-o=tsv") + output, _ := a.env.runCommand(commandPath, "account", "show", "--query=[name,id]", "-o=tsv") if output == "" { return false } diff --git a/src/segment_az_test.go b/src/segment_az_test.go index 98c7a1eb54ad..fe6b736562cf 100644 --- a/src/segment_az_test.go +++ b/src/segment_az_test.go @@ -18,7 +18,7 @@ type azArgs struct { func bootStrapAzTest(args *azArgs) *az { env := new(MockedEnvironment) - env.On("hasCommand", "az").Return(args.enabled) + env.On("hasCommand", "az").Return("az", args.enabled) env.On("runCommand", "az", []string{"account", "show", "--query=[name,id]", "-o=tsv"}).Return(fmt.Sprintf("%s\n%s\n", args.subscriptionName, args.subscriptionID), nil) props := &properties{ values: map[Property]interface{}{ diff --git a/src/segment_command.go b/src/segment_command.go index bbc26ac06f48..8c37875280e3 100644 --- a/src/segment_command.go +++ b/src/segment_command.go @@ -17,7 +17,8 @@ const ( func (c *command) enabled() bool { shell := c.props.getString(ExecutableShell, "bash") - if !c.env.hasCommand(shell) { + shell, commandExists := c.env.hasCommand(shell) + if !commandExists { return false } command := c.props.getString(Command, "echo no command specified") diff --git a/src/segment_dotnet_test.go b/src/segment_dotnet_test.go index dc07b79da1c0..3d5ff478d8a8 100644 --- a/src/segment_dotnet_test.go +++ b/src/segment_dotnet_test.go @@ -16,7 +16,7 @@ type dotnetArgs struct { func bootStrapDotnetTest(args *dotnetArgs) *dotnet { env := new(MockedEnvironment) - env.On("hasCommand", "dotnet").Return(args.enabled) + env.On("hasCommand", "dotnet").Return("dotnet", args.enabled) if args.unsupported { err := &commandError{exitCode: 145} env.On("runCommand", "dotnet", []string{"--version"}).Return("", err) diff --git a/src/segment_git.go b/src/segment_git.go index dc8a7a1c6c10..96fc6a11c6db 100644 --- a/src/segment_git.go +++ b/src/segment_git.go @@ -47,9 +47,10 @@ func (s *gitStatus) string(prefix, color string) string { } type git struct { - props *properties - env environmentInfo - repo *gitRepo + props *properties + env environmentInfo + repo *gitRepo + commandPath string } const ( @@ -114,10 +115,12 @@ const ( ) func (g *git) enabled() bool { - if !g.env.hasCommand("git") { + commandPath, commandExists := g.env.hasCommand("git") + if !commandExists { return false } - output, _ := g.env.runCommand("git", "rev-parse", "--is-inside-work-tree") + g.commandPath = commandPath + output, _ := g.env.runCommand(g.commandPath, "rev-parse", "--is-inside-work-tree") return output == "true" } @@ -235,7 +238,7 @@ func (g *git) getStatusColor(defaultValue string) string { func (g *git) getGitCommandOutput(args ...string) string { args = append([]string{"-c", "core.quotepath=false", "-c", "color.status=false"}, args...) - val, _ := g.env.runCommand("git", args...) + val, _ := g.env.runCommand(g.commandPath, args...) return val } diff --git a/src/segment_git_test.go b/src/segment_git_test.go index dd4159adef50..8f831bbc9f4e 100644 --- a/src/segment_git_test.go +++ b/src/segment_git_test.go @@ -12,7 +12,7 @@ const ( func TestEnabledGitNotFound(t *testing.T) { env := new(MockedEnvironment) - env.On("hasCommand", "git").Return(false) + env.On("hasCommand", "git").Return("", false) g := &git{ env: env, } @@ -21,7 +21,7 @@ func TestEnabledGitNotFound(t *testing.T) { func TestEnabledInWorkingDirectory(t *testing.T) { env := new(MockedEnvironment) - env.On("hasCommand", "git").Return(true) + env.On("hasCommand", "git").Return("git", true) env.On("runCommand", "git", []string{"rev-parse", "--is-inside-work-tree"}).Return("true", nil) g := &git{ env: env, @@ -36,7 +36,8 @@ func TestGetGitOutputForCommand(t *testing.T) { env := new(MockedEnvironment) env.On("runCommand", "git", append(args, commandArgs...)).Return(want, nil) g := &git{ - env: env, + env: env, + commandPath: "git", } got := g.getGitCommandOutput(commandArgs...) assert.Equal(t, want, got) @@ -85,6 +86,7 @@ func setupHEADContextEnv(context *detachedContext) *git { repo: &gitRepo{ root: "", }, + commandPath: "git", } return g } @@ -211,7 +213,8 @@ func TestGetStashContextZeroEntries(t *testing.T) { env := new(MockedEnvironment) env.On("runCommand", "git", []string{"-c", "core.quotepath=false", "-c", "color.status=false", "rev-list", "--walk-reflogs", "--count", "refs/stash"}).Return("", nil) g := &git{ - env: env, + env: env, + commandPath: "git", } got := g.getStashContext() assert.Equal(t, want, got) @@ -222,7 +225,8 @@ func TestGetStashContextMultipleEntries(t *testing.T) { env := new(MockedEnvironment) env.On("runCommand", "git", []string{"-c", "core.quotepath=false", "-c", "color.status=false", "rev-list", "--walk-reflogs", "--count", "refs/stash"}).Return("2", nil) g := &git{ - env: env, + env: env, + commandPath: "git", } got := g.getStashContext() assert.Equal(t, want, got) @@ -390,7 +394,8 @@ func bootstrapUpstreamTest(upstream string) *git { repo: &gitRepo{ upstream: "origin/main", }, - props: props, + props: props, + commandPath: "git", } return g } diff --git a/src/segment_kubectl.go b/src/segment_kubectl.go index 4e07620551e3..8d2c84117e4a 100644 --- a/src/segment_kubectl.go +++ b/src/segment_kubectl.go @@ -16,9 +16,10 @@ func (k *kubectl) init(props *properties, env environmentInfo) { } func (k *kubectl) enabled() bool { - if !k.env.hasCommand("kubectl") { + commandPath, commandExists := k.env.hasCommand("kubectl") + if !commandExists { return false } - k.contextName, _ = k.env.runCommand("kubectl", "config", "current-context") + k.contextName, _ = k.env.runCommand(commandPath, "config", "current-context") return k.contextName != "" } diff --git a/src/segment_kubectl_test.go b/src/segment_kubectl_test.go index 070e8759014a..3fb2f02cf1cc 100644 --- a/src/segment_kubectl_test.go +++ b/src/segment_kubectl_test.go @@ -13,7 +13,7 @@ type kubectlArgs struct { func bootStrapKubectlTest(args *kubectlArgs) *kubectl { env := new(MockedEnvironment) - env.On("hasCommand", "kubectl").Return(args.enabled) + env.On("hasCommand", "kubectl").Return("kubectl", args.enabled) env.On("runCommand", "kubectl", []string{"config", "current-context"}).Return(args.contextName, nil) k := &kubectl{ env: env, diff --git a/src/segment_language.go b/src/segment_language.go index 09499c30d2de..6e0db33e4eb0 100644 --- a/src/segment_language.go +++ b/src/segment_language.go @@ -88,8 +88,9 @@ func (l *language) getVersion() bool { // hasCommand checks if one of the commands exists and set it as executablr func (l *language) hasCommand() bool { for i, command := range l.commands { - if l.env.hasCommand(command) { - l.executable = command + commandPath, commandExists := l.env.hasCommand(command) + if commandExists { + l.executable = commandPath break } if i == len(l.commands)-1 { diff --git a/src/segment_language_test.go b/src/segment_language_test.go index 50a4e49a0c48..d0b517b1b722 100644 --- a/src/segment_language_test.go +++ b/src/segment_language_test.go @@ -36,7 +36,7 @@ func (l *languageArgs) hasvalue(value string, list []string) bool { func bootStrapLanguageTest(args *languageArgs) *language { env := new(MockedEnvironment) for _, command := range args.commands { - env.On("hasCommand", command).Return(args.hasvalue(command, args.enabledCommands)) + env.On("hasCommand", command).Return(command, args.hasvalue(command, args.enabledCommands)) env.On("runCommand", command, []string{args.versionParam}).Return(args.version, nil) } for _, extension := range args.extensions { diff --git a/src/segment_path_test.go b/src/segment_path_test.go index d8b428c59ac8..c51fa9850475 100644 --- a/src/segment_path_test.go +++ b/src/segment_path_test.go @@ -72,9 +72,9 @@ func (env *MockedEnvironment) getPlatform() string { return args.String(0) } -func (env *MockedEnvironment) hasCommand(command string) bool { +func (env *MockedEnvironment) hasCommand(command string) (string, bool) { args := env.Called(command) - return args.Bool(0) + return args.String(0), args.Bool(1) } func (env *MockedEnvironment) runCommand(command string, args ...string) (string, error) { diff --git a/src/segment_python_test.go b/src/segment_python_test.go index 984ce0f868b9..e99d92fcc257 100644 --- a/src/segment_python_test.go +++ b/src/segment_python_test.go @@ -16,7 +16,7 @@ type pythonArgs struct { func bootStrapPythonTest(args *pythonArgs) *python { env := new(MockedEnvironment) - env.On("hasCommand", "python").Return(true) + env.On("hasCommand", "python").Return("python", true) env.On("runCommand", "python", []string{"--version"}).Return("Python 3.8.4", nil) env.On("hasFiles", "*.py").Return(true) env.On("getenv", "VIRTUAL_ENV").Return(args.virtualEnvName) diff --git a/src/segment_terraform.go b/src/segment_terraform.go index a49dcc361e90..26b52855a4d0 100644 --- a/src/segment_terraform.go +++ b/src/segment_terraform.go @@ -16,9 +16,10 @@ func (tf *terraform) init(props *properties, env environmentInfo) { } func (tf *terraform) enabled() bool { - if !tf.env.hasCommand("terraform") || !tf.env.hasFolder(".terraform") { + commandPath, commandExists := tf.env.hasCommand("terraform") + if !commandExists || !tf.env.hasFolder(".terraform") { return false } - tf.workspaceName, _ = tf.env.runCommand("terraform", "workspace", "show") + tf.workspaceName, _ = tf.env.runCommand(commandPath, "workspace", "show") return true } diff --git a/src/segment_terraform_test.go b/src/segment_terraform_test.go index dc98ec998027..3970f2c839f2 100644 --- a/src/segment_terraform_test.go +++ b/src/segment_terraform_test.go @@ -14,7 +14,7 @@ type terraformArgs struct { func bootStrapTerraformTest(args *terraformArgs) *terraform { env := new(MockedEnvironment) - env.On("hasCommand", "terraform").Return(args.hasTfCommand) + env.On("hasCommand", "terraform").Return("terraform", args.hasTfCommand) env.On("hasFolder", ".terraform").Return(args.hasTfFolder) env.On("runCommand", "terraform", []string{"workspace", "show"}).Return(args.workspaceName, nil) k := &terraform{