Skip to content

Commit

Permalink
perf: cache exexutable path
Browse files Browse the repository at this point in the history
  • Loading branch information
lnu committed Dec 30, 2020
1 parent 957a2b2 commit a987351
Show file tree
Hide file tree
Showing 15 changed files with 46 additions and 33 deletions.
8 changes: 4 additions & 4 deletions src/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions src/segment_az.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion src/segment_az_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{
Expand Down
3 changes: 2 additions & 1 deletion src/segment_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion src/segment_dotnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 9 additions & 6 deletions src/segment_git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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"
}

Expand Down Expand Up @@ -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
}

Expand Down
17 changes: 11 additions & 6 deletions src/segment_git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -85,6 +86,7 @@ func setupHEADContextEnv(context *detachedContext) *git {
repo: &gitRepo{
root: "",
},
commandPath: "git",
}
return g
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -390,7 +394,8 @@ func bootstrapUpstreamTest(upstream string) *git {
repo: &gitRepo{
upstream: "origin/main",
},
props: props,
props: props,
commandPath: "git",
}
return g
}
Expand Down
5 changes: 3 additions & 2 deletions src/segment_kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != ""
}
2 changes: 1 addition & 1 deletion src/segment_kubectl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions src/segment_language.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/segment_language_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/segment_path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/segment_python_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions src/segment_terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion src/segment_terraform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit a987351

Please sign in to comment.