Skip to content

Commit 0e6abda

Browse files
committed
allow generating ssh config for a single codespace
1 parent 92403f3 commit 0e6abda

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

pkg/cmd/codespace/ssh.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616

1717
"github.com/MakeNowJust/heredoc"
1818
"github.com/cli/cli/v2/internal/codespaces"
19+
"github.com/cli/cli/v2/internal/codespaces/api"
1920
"github.com/cli/cli/v2/pkg/cmdutil"
2021
"github.com/cli/cli/v2/pkg/liveshare"
2122
"github.com/hashicorp/go-multierror"
@@ -72,7 +73,11 @@ func newSSHCmd(app *App) *cobra.Command {
7273
return nil
7374
},
7475
RunE: func(cmd *cobra.Command, args []string) error {
75-
return app.SSH(cmd.Context(), args, opts)
76+
if opts.config {
77+
return app.ListOpensshConfig(cmd.Context(), opts)
78+
} else {
79+
return app.SSH(cmd.Context(), args, opts)
80+
}
7681
},
7782
DisableFlagsInUseLine: true,
7883
}
@@ -94,10 +99,6 @@ func (a *App) SSH(ctx context.Context, sshArgs []string, opts sshOptions) (err e
9499
ctx, cancel := context.WithCancel(ctx)
95100
defer cancel()
96101

97-
if opts.config {
98-
return a.ListOpensshConfig(ctx)
99-
}
100-
101102
liveshareLogger := noopLogger()
102103
if opts.debug {
103104
debugLogger, err := newFileLogger(opts.debugFile)
@@ -176,12 +177,24 @@ func (a *App) SSH(ctx context.Context, sshArgs []string, opts sshOptions) (err e
176177
}
177178
}
178179

179-
func (a *App) ListOpensshConfig(ctx context.Context) error {
180-
a.StartProgressIndicatorWithLabel("Fetching codespaces")
181-
codespaces, err := a.apiClient.ListCodespaces(ctx, -1)
182-
a.StopProgressIndicator()
180+
func (a *App) ListOpensshConfig(ctx context.Context, opts sshOptions) error {
181+
// Ensure all child tasks (e.g. port forwarding) terminate before return.
182+
ctx, cancel := context.WithCancel(ctx)
183+
defer cancel()
184+
185+
var err error
186+
var codespaces []*api.Codespace
187+
if opts.codespace == "" {
188+
a.StartProgressIndicatorWithLabel("Fetching codespaces")
189+
codespaces, err = a.apiClient.ListCodespaces(ctx, -1)
190+
a.StopProgressIndicator()
191+
} else {
192+
var codespace *api.Codespace
193+
codespace, err = getOrChooseCodespace(ctx, a.apiClient, opts.codespace)
194+
codespaces = []*api.Codespace{codespace}
195+
}
183196
if err != nil {
184-
return fmt.Errorf("error getting codespaces: %w", err)
197+
return fmt.Errorf("error getting codespace info: %w", err)
185198
}
186199

187200
t, err := template.New("ssh_config").Parse(`Host cs.{{.Name}}.{{.EscapedRef}}

0 commit comments

Comments
 (0)