Skip to content

Commit 6a0e74c

Browse files
committed
Revert "refactor: ssh - better messages"
This reverts commit 7dc27369fb349ddc78238457c8f18723dc3af557. Because gh auth doesn't correctly handle connection timeouts unlike calling the rest api see cli/cli#8293 We no longer handle the error case where there is an auth token but no config file. But that's ok because its a rare edge case.
1 parent 82aaa02 commit 6a0e74c

3 files changed

Lines changed: 18 additions & 54 deletions

File tree

‎cmd/auth.go‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ func ensureAuth(opts *AuthOptions) error {
9898
}
9999
}
100100

101-
// TODO: just call gh auth status
102101
username, err := fetchAuthenticatedUser(client)
103102
if err != nil {
104103
return err
@@ -113,7 +112,7 @@ func ghAuthLogin(hostname string, scopes []string) error {
113112
for _, s := range scopes {
114113
args = append(args, "-s", s)
115114
}
116-
err := util.ExecGhInteractive(args...)
115+
err := util.ExecGh(args...)
117116
return err
118117
}
119118

‎cmd/ssh.go‎

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"regexp"
1313
"strings"
1414

15+
"github.com/cli/go-gh/v2/pkg/api"
1516
"github.com/kevinburke/ssh_config"
1617
"github.com/spf13/cobra"
1718
"github.com/tekumara/gh-doctor/internal/util"
@@ -70,12 +71,21 @@ func ensureSsh(opts *SshOptions) error {
7071
}
7172
}
7273

73-
// TODO: delete file if exists and rotating
74-
_, err := ensureGhAuth(opts.Hostname)
74+
_, err := api.NewRESTClient(api.ClientOptions{Host: opts.Hostname})
7575
if err != nil {
76+
if strings.Contains(err.Error(), "authentication token not found") {
77+
hostFlag := ""
78+
if opts.Hostname != githubCom {
79+
hostFlag = fmt.Sprintf(" -h %s ", opts.Hostname)
80+
}
81+
return fmt.Errorf("%w\n Please run: gh doctor auth %s-s admin:public_key", err, hostFlag)
82+
}
83+
7684
return err
7785
}
7886

87+
// TODO: delete file if exists and rotating
88+
7989
keyFile := expand(opts.KeyFile)
8090

8191
if err := ensureKeyFileExists(keyFile, opts.Hostname); err != nil {
@@ -90,11 +100,6 @@ func ensureSsh(opts *SshOptions) error {
90100
return err
91101
}
92102

93-
_, err = ensureSshAuth(opts.Hostname)
94-
if err != nil {
95-
return err
96-
}
97-
98103
return nil
99104
}
100105

@@ -124,7 +129,6 @@ func removeSshAgentIdentities(sshAuthSock string) error {
124129

125130
var regexSshSuccess = regexp.MustCompile(`Hi (.*)! You've successfully authenticated`)
126131
var regexAcceptedKey = regexp.MustCompile(`Server accepts key: .*`)
127-
var regexLoggedInTo = regexp.MustCompile(`Logged in to .*`)
128132

129133
// Authenticate to github using ssh.
130134
// Returns
@@ -205,29 +209,9 @@ func ensureKeyFileExists(keyFile string, hostname string) error {
205209
}
206210
}
207211

208-
func ensureGhAuth(hostname string) (string, error) {
209-
args := []string{"auth", "status", "-h", hostname}
210-
stdout, err := util.ExecGh(args...)
211-
if err != nil && strings.Contains(err.Error(), "You are not logged into any GitHub hosts") {
212-
hostFlag := ""
213-
if hostname != githubCom {
214-
hostFlag = fmt.Sprintf(" -h %s ", hostname)
215-
}
216-
//lint:ignore ST1005 capitalise error message to match gh
217-
return "", fmt.Errorf("You are not logged into any GitHub hosts.\n Please run: gh doctor auth %s-s admin:public_key", hostFlag)
218-
}
219-
220-
username := regexLoggedInTo.FindString(string(stdout))
221-
if len(username) > 0 {
222-
fmt.Printf("✓ %s\n", username)
223-
}
224-
225-
return username, err
226-
}
227-
228212
func addKey(keyFile string, hostname string) error {
229213
args := []string{"ssh-key", "add", keyFile}
230-
err := util.ExecGhInteractive(args...)
214+
err := util.ExecGh(args...)
231215
return err
232216
}
233217

‎internal/util/gh.go‎

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,10 @@ import (
88
"github.com/cli/go-gh/v2"
99
)
1010

11-
// GH_PROMPT_DISABLED ensures the minimal amount of prompting eg: during gh auth login
12-
// GH_NO_UPDATE_NOTIFIER prevents notification of a new gh release
13-
var ghEnv = []string{"GH_PROMPT_DISABLED=1", "GH_NO_UPDATE_NOTIFIER=1"}
14-
1511
// Invoke a gh command in a subprocess with its stdin, stdout, and stderr streams connected to
1612
// those of the parent process. This is suitable for running gh commands with interactive prompts.
1713
// Adapted from https://github.com/cli/go-gh/blob/47a83eeb1778d8e60e98e356b9e5d6178a567f31/gh.go#L41
18-
func ExecGhInteractive(args ...string) error {
14+
func ExecGh(args ...string) error {
1915
ghExe, err := gh.Path()
2016
if err != nil {
2117
return err
@@ -24,28 +20,13 @@ func ExecGhInteractive(args ...string) error {
2420
cmd.Stdin = os.Stdin
2521
cmd.Stdout = os.Stdout
2622
cmd.Stderr = os.Stderr
23+
// GH_PROMPT_DISABLED ensures the minimal amount of prompting eg: during gh auth login
24+
// GH_NO_UPDATE_NOTIFIER prevents notification of a new gh release
2725
// append this processes's env vars so gh can locate its config, state and data dirs
2826
// as per https://github.com/cli/go-gh/blob/47a83eeb1778d8e60e98e356b9e5d6178a567f31/pkg/config/config.go#L236
29-
cmd.Env = append(ghEnv, os.Environ()...)
27+
cmd.Env = append([]string{"GH_PROMPT_DISABLED=1", "GH_NO_UPDATE_NOTIFIER=1"}, os.Environ()...)
3028
if err := cmd.Run(); err != nil {
3129
return fmt.Errorf("gh execution failed: %w", err)
3230
}
3331
return nil
3432
}
35-
36-
// ExecGh invokes a gh command in a subprocess and captures stdout and stderr
37-
func ExecGh(args ...string) (stdout []byte, err error) {
38-
ghExe, err := gh.Path()
39-
if err != nil {
40-
return
41-
}
42-
cmd := exec.Command(ghExe, args...)
43-
// append this processes's env vars so gh can locate its config, state and data dirs
44-
// as per https://github.com/cli/go-gh/blob/47a83eeb1778d8e60e98e356b9e5d6178a567f31/pkg/config/config.go#L236
45-
cmd.Env = append(ghEnv, os.Environ()...)
46-
stdout, err = cmd.Output()
47-
if ee, ok := err.(*exec.ExitError); ok {
48-
return stdout, fmt.Errorf("%s", ee.Stderr)
49-
}
50-
return
51-
}

0 commit comments

Comments
 (0)