Skip to content

Commit d853ce5

Browse files
committed
Avoid resolving executable() until requested at runtime
This is to avoid hitting the filesystem and resolving symlinks unnecessarily. The value of executable is just used conditionally by a handful of commands.
1 parent 78b35b7 commit d853ce5

File tree

7 files changed

+18
-12
lines changed

7 files changed

+18
-12
lines changed

cmd/gh/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ func mainRun() exitCode {
236236

237237
newRelease := <-updateMessageChan
238238
if newRelease != nil {
239-
isHomebrew := isUnderHomebrew(cmdFactory.Executable)
239+
isHomebrew := isUnderHomebrew(cmdFactory.Executable())
240240
if isHomebrew && isRecentRelease(newRelease.PublishedAt) {
241241
// do not notify Homebrew users before the version bump had a chance to get merged into homebrew-core
242242
return exitOK

pkg/cmd/auth/login/login.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ func NewCmdLogin(f *cmdutil.Factory, runF func(*LoginOptions) error) *cobra.Comm
3838
IO: f.IOStreams,
3939
Config: f.Config,
4040
HttpClient: f.HttpClient,
41-
42-
MainExecutable: f.Executable,
4341
}
4442

4543
var tokenStdin bool
@@ -103,6 +101,7 @@ func NewCmdLogin(f *cmdutil.Factory, runF func(*LoginOptions) error) *cobra.Comm
103101
}
104102
}
105103

104+
opts.MainExecutable = f.Executable()
106105
if runF != nil {
107106
return runF(opts)
108107
}

pkg/cmd/auth/login/login_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ func Test_NewCmdLogin(t *testing.T) {
147147
t.Run(tt.name, func(t *testing.T) {
148148
io, stdin, _, _ := iostreams.Test()
149149
f := &cmdutil.Factory{
150-
IOStreams: io,
150+
IOStreams: io,
151+
Executable: func() string { return "/path/to/gh" },
151152
}
152153

153154
io.SetStdoutTTY(true)

pkg/cmd/auth/refresh/refresh.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ func NewCmdRefresh(f *cmdutil.Factory, runF func(*RefreshOptions) error) *cobra.
3636
_, err := authflow.AuthFlowWithConfig(cfg, io, hostname, "", scopes)
3737
return err
3838
},
39-
MainExecutable: f.Executable,
4039
}
4140

4241
cmd := &cobra.Command{
@@ -62,6 +61,7 @@ func NewCmdRefresh(f *cmdutil.Factory, runF func(*RefreshOptions) error) *cobra.
6261
return &cmdutil.FlagError{Err: errors.New("--hostname required when not running interactively")}
6362
}
6463

64+
opts.MainExecutable = f.Executable()
6565
if runF != nil {
6666
return runF(opts)
6767
}

pkg/cmd/auth/refresh/refresh_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ func Test_NewCmdRefresh(t *testing.T) {
8888
t.Run(tt.name, func(t *testing.T) {
8989
io, _, _, _ := iostreams.Test()
9090
f := &cmdutil.Factory{
91-
IOStreams: io,
91+
IOStreams: io,
92+
Executable: func() string { return "/path/to/gh" },
9293
}
9394
io.SetStdinTTY(tt.tty)
9495
io.SetStdoutTTY(tt.tty)

pkg/cmd/factory/default.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,17 @@ import (
1919
)
2020

2121
func New(appVersion string) *cmdutil.Factory {
22+
var exe string
2223
f := &cmdutil.Factory{
23-
Config: configFunc(), // No factory dependencies
24-
Branch: branchFunc(), // No factory dependencies
25-
Executable: executable("gh"), // No factory dependencies
26-
27-
ExtensionManager: extension.NewManager(),
24+
Config: configFunc(), // No factory dependencies
25+
Branch: branchFunc(), // No factory dependencies
26+
Executable: func() string {
27+
if exe != "" {
28+
return exe
29+
}
30+
exe = executable("gh")
31+
return exe
32+
},
2833
}
2934

3035
f.IOStreams = ioStreams(f) // Depends on Config

pkg/cmdutil/factory.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ type Factory struct {
2727
ExtensionManager extensions.ExtensionManager
2828

2929
// Executable is the path to the currently invoked gh binary
30-
Executable string
30+
Executable func() string
3131
}

0 commit comments

Comments
 (0)