Skip to content

Commit 7e33d80

Browse files
authored
feat: Add helper output on failed gitssh (coder#1127)
1 parent 4417dd5 commit 7e33d80

File tree

5 files changed

+24
-6
lines changed

5 files changed

+24
-6
lines changed

cli/gitssh.go

+19-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package cli
22

33
import (
4+
"fmt"
45
"net/url"
56
"os"
67
"os/exec"
8+
"strings"
79

10+
"github.com/coder/coder/cli/cliui"
11+
"github.com/coder/coder/codersdk"
812
"github.com/spf13/cobra"
913
"golang.org/x/xerrors"
10-
11-
"github.com/coder/coder/codersdk"
1214
)
1315

1416
func gitssh() *cobra.Command {
@@ -55,12 +57,25 @@ func gitssh() *cobra.Command {
5557
return xerrors.Errorf("close temp gitsshkey file: %w", err)
5658
}
5759

58-
a := append([]string{"-i", privateKeyFile.Name()}, args...)
59-
c := exec.CommandContext(cmd.Context(), "ssh", a...)
60+
args = append([]string{"-i", privateKeyFile.Name()}, args...)
61+
c := exec.CommandContext(cmd.Context(), "ssh", args...)
62+
c.Stderr = cmd.ErrOrStderr()
6063
c.Stdout = cmd.OutOrStdout()
6164
c.Stdin = cmd.InOrStdin()
6265
err = c.Run()
6366
if err != nil {
67+
exitErr := &exec.ExitError{}
68+
if xerrors.As(err, &exitErr) && exitErr.ExitCode() == 255 {
69+
_, _ = fmt.Fprintln(cmd.ErrOrStderr(),
70+
"\n"+cliui.Styles.Wrap.Render("Coder authenticates with "+cliui.Styles.Field.Render("git")+
71+
" using the public key below. All clones with SSH are authenticated automatically 🪄.")+"\n")
72+
_, _ = fmt.Fprintln(cmd.ErrOrStderr(), cliui.Styles.Code.Render(strings.TrimSpace(key.PublicKey))+"\n")
73+
_, _ = fmt.Fprintln(cmd.ErrOrStderr(), "Add to GitHub and GitLab:")
74+
_, _ = fmt.Fprintln(cmd.ErrOrStderr(), cliui.Styles.Prompt.String()+"https://github.com/settings/ssh/new")
75+
_, _ = fmt.Fprintln(cmd.ErrOrStderr(), cliui.Styles.Prompt.String()+"https://gitlab.com/-/profile/keys")
76+
_, _ = fmt.Fprintln(cmd.ErrOrStderr())
77+
return err
78+
}
6479
return xerrors.Errorf("run ssh command: %w", err)
6580
}
6681

cli/publickey.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ func publickey() *cobra.Command {
3030
"Coder. All clones with SSH will be authenticated automatically 🪄.",
3131
))
3232
cmd.Println()
33+
cmd.Println(cliui.Styles.Code.Render(strings.TrimSpace(key.PublicKey)))
34+
cmd.Println()
3335
cmd.Println("Add to GitHub and GitLab:")
3436
cmd.Println(cliui.Styles.Prompt.String() + "https://github.com/settings/ssh/new")
3537
cmd.Println(cliui.Styles.Prompt.String() + "https://gitlab.com/-/profile/keys")
36-
cmd.Println()
37-
cmd.Println(cliui.Styles.Code.Render(strings.TrimSpace(key.PublicKey)))
3838

3939
return nil
4040
},

coderd/gitsshkey.go

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ func (api *api) agentGitSSHKey(rw http.ResponseWriter, r *http.Request) {
105105
}
106106

107107
httpapi.Write(rw, http.StatusOK, codersdk.AgentGitSSHKey{
108+
PublicKey: gitSSHKey.PublicKey,
108109
PrivateKey: gitSSHKey.PrivateKey,
109110
})
110111
}

codersdk/gitsshkey.go

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type GitSSHKey struct {
1919
}
2020

2121
type AgentGitSSHKey struct {
22+
PublicKey string `json:"public_key"`
2223
PrivateKey string `json:"private_key"`
2324
}
2425

site/src/api/typesGenerated.ts

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export interface GitSSHKey {
1616

1717
// From codersdk/gitsshkey.go:21:6.
1818
export interface AgentGitSSHKey {
19+
readonly public_key: string
1920
readonly private_key: string
2021
}
2122

0 commit comments

Comments
 (0)