Skip to content

Commit 3b7e5fc

Browse files
committed
Store Executable() information on codespaces App
This is to avoid having to explicitly pass it to each subcommand that needs it. Each codespaces command runs in the context of App, so that's a point of shared context that we can store state in.
1 parent 3cce16e commit 3b7e5fc

File tree

5 files changed

+25
-18
lines changed

5 files changed

+25
-18
lines changed

pkg/cmd/codespace/common.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,25 @@ import (
2121
"golang.org/x/term"
2222
)
2323

24+
type executable interface {
25+
Executable() string
26+
}
27+
2428
type App struct {
25-
io *iostreams.IOStreams
26-
apiClient apiClient
27-
errLogger *log.Logger
29+
io *iostreams.IOStreams
30+
apiClient apiClient
31+
errLogger *log.Logger
32+
executable executable
2833
}
2934

30-
func NewApp(io *iostreams.IOStreams, apiClient apiClient) *App {
35+
func NewApp(io *iostreams.IOStreams, exe executable, apiClient apiClient) *App {
3136
errLogger := log.New(io.ErrOut, "", 0)
3237

3338
return &App{
34-
io: io,
35-
apiClient: apiClient,
36-
errLogger: errLogger,
39+
io: io,
40+
apiClient: apiClient,
41+
errLogger: errLogger,
42+
executable: exe,
3743
}
3844
}
3945

pkg/cmd/codespace/delete_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func TestDelete(t *testing.T) {
190190
io, _, stdout, stderr := iostreams.Test()
191191
io.SetStdinTTY(true)
192192
io.SetStdoutTTY(true)
193-
app := NewApp(io, apiMock)
193+
app := NewApp(io, nil, apiMock)
194194
err := app.Delete(context.Background(), opts)
195195
if (err != nil) != tt.wantErr {
196196
t.Errorf("delete() error = %v, wantErr %v", err, tt.wantErr)

pkg/cmd/codespace/root.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package codespace
22

33
import (
4-
"github.com/cli/cli/v2/pkg/cmdutil"
54
"github.com/spf13/cobra"
65
)
76

8-
func NewRootCmd(app *App, f *cmdutil.Factory) *cobra.Command {
7+
func NewRootCmd(app *App) *cobra.Command {
98
root := &cobra.Command{
109
Use: "codespace",
1110
Short: "Connect to and manage your codespaces",
@@ -17,7 +16,7 @@ func NewRootCmd(app *App, f *cmdutil.Factory) *cobra.Command {
1716
root.AddCommand(newListCmd(app))
1817
root.AddCommand(newLogsCmd(app))
1918
root.AddCommand(newPortsCmd(app))
20-
root.AddCommand(newSSHCmd(app, f))
19+
root.AddCommand(newSSHCmd(app))
2120
root.AddCommand(newCpCmd(app))
2221
root.AddCommand(newStopCmd(app))
2322

pkg/cmd/codespace/ssh.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type sshOptions struct {
3434
scpArgs []string // scp arguments, for 'cs cp' (nil for 'cs ssh')
3535
}
3636

37-
func newSSHCmd(app *App, f *cmdutil.Factory) *cobra.Command {
37+
func newSSHCmd(app *App) *cobra.Command {
3838
var opts sshOptions
3939

4040
sshCmd := &cobra.Command{
@@ -70,7 +70,7 @@ func newSSHCmd(app *App, f *cmdutil.Factory) *cobra.Command {
7070
fmt.Fprintf(os.Stderr, "%v\n", err)
7171
}
7272

73-
sshCmd.AddCommand(newConfigCmd(app, f))
73+
sshCmd.AddCommand(newConfigCmd(app))
7474

7575
return sshCmd
7676
}
@@ -174,7 +174,7 @@ func (a *App) SSH(ctx context.Context, sshArgs []string, opts sshOptions) (err e
174174
}
175175
}
176176

177-
func (a *App) printOpenSSHConfig(ctx context.Context, opts configOptions, executable string) error {
177+
func (a *App) printOpenSSHConfig(ctx context.Context, opts configOptions) error {
178178
ctx, cancel := context.WithCancel(ctx)
179179
defer cancel()
180180

@@ -258,6 +258,7 @@ func (a *App) printOpenSSHConfig(ctx context.Context, opts configOptions, execut
258258
return fmt.Errorf("error formatting template: %w", err)
259259
}
260260

261+
ghExec := a.executable.Executable()
261262
for result := range sshUsers {
262263
if result.err != nil {
263264
fmt.Fprintf(os.Stderr, "%v\n", result.err)
@@ -287,7 +288,7 @@ func (a *App) printOpenSSHConfig(ctx context.Context, opts configOptions, execut
287288
Name: result.codespace.Name,
288289
EscapedRef: strings.ReplaceAll(result.codespace.GitStatus.Ref, "/", "-"),
289290
SSHUser: result.user,
290-
GHExec: executable,
291+
GHExec: ghExec,
291292
}
292293
if err := t.Execute(a.io.Out, conf); err != nil {
293294
return err
@@ -392,7 +393,7 @@ type configOptions struct {
392393
codespace string
393394
}
394395

395-
func newConfigCmd(app *App, f *cmdutil.Factory) *cobra.Command {
396+
func newConfigCmd(app *App) *cobra.Command {
396397
var opts configOptions
397398

398399
configCmd := &cobra.Command{
@@ -418,7 +419,7 @@ func newConfigCmd(app *App, f *cmdutil.Factory) *cobra.Command {
418419
$ echo 'include ~/.ssh/codespaces' >> ~/.ssh/config'
419420
`),
420421
RunE: func(cmd *cobra.Command, args []string) error {
421-
return app.printOpenSSHConfig(cmd.Context(), opts, f.Executable())
422+
return app.printOpenSSHConfig(cmd.Context(), opts)
422423
},
423424
DisableFlagsInUseLine: true,
424425
}

pkg/cmd/root/root.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,15 @@ func newCodespaceCmd(f *cmdutil.Factory) *cobra.Command {
135135
vscsURL := os.Getenv("INTERNAL_VSCS_TARGET_URL")
136136
app := codespaceCmd.NewApp(
137137
f.IOStreams,
138+
f,
138139
codespacesAPI.New(
139140
serverURL,
140141
apiURL,
141142
vscsURL,
142143
&lazyLoadedHTTPClient{factory: f},
143144
),
144145
)
145-
cmd := codespaceCmd.NewRootCmd(app, f)
146+
cmd := codespaceCmd.NewRootCmd(app)
146147
cmd.Use = "codespace"
147148
cmd.Aliases = []string{"cs"}
148149
cmd.Annotations = map[string]string{"IsCore": "true"}

0 commit comments

Comments
 (0)