@@ -14,8 +14,7 @@ import (
14
14
"cdr.dev/coder-cli/internal/config"
15
15
"cdr.dev/coder-cli/internal/entclient"
16
16
"github.com/urfave/cli"
17
-
18
- "go.coder.com/flog"
17
+ "golang.org/x/xerrors"
19
18
)
20
19
21
20
func makeConfigSSHCmd () cli.Command {
@@ -46,7 +45,7 @@ func makeConfigSSHCmd() cli.Command {
46
45
}
47
46
}
48
47
49
- func configSSH (filepath * string , remove * bool ) func (c * cli.Context ) {
48
+ func configSSH (filepath * string , remove * bool ) func (c * cli.Context ) error {
50
49
startToken := "# ------------START-CODER-ENTERPRISE-----------"
51
50
startMessage := `# The following has been auto-generated by "coder config-ssh"
52
51
# to make accessing your Coder Enterprise environments easier.
@@ -58,7 +57,7 @@ func configSSH(filepath *string, remove *bool) func(c *cli.Context) {
58
57
# You should not hand-edit this section, unless you are deleting it.`
59
58
endToken := "# ------------END-CODER-ENTERPRISE------------"
60
59
61
- return func (c * cli.Context ) {
60
+ return func (c * cli.Context ) error {
62
61
ctx , cancel := context .WithCancel (context .Background ())
63
62
defer cancel ()
64
63
@@ -67,45 +66,48 @@ func configSSH(filepath *string, remove *bool) func(c *cli.Context) {
67
66
// SSH configs are not always already there.
68
67
currentConfig = ""
69
68
} else if err != nil {
70
- flog . Fatal ("failed to read ssh config file %q: %v " , filepath , err )
69
+ return xerrors . Errorf ("failed to read ssh config file %q: %w " , filepath , err )
71
70
}
72
71
73
72
startIndex := strings .Index (currentConfig , startToken )
74
73
endIndex := strings .Index (currentConfig , endToken )
75
74
76
75
if * remove {
77
76
if startIndex == - 1 || endIndex == - 1 {
78
- flog . Fatal ("the Coder Enterprise ssh configuration section could not be safely deleted or does not exist" )
77
+ return xerrors . Errorf ("the Coder Enterprise ssh configuration section could not be safely deleted or does not exist" )
79
78
}
80
79
currentConfig = currentConfig [:startIndex - 1 ] + currentConfig [endIndex + len (endToken )+ 1 :]
81
80
82
81
err = writeStr (* filepath , currentConfig )
83
82
if err != nil {
84
- flog . Fatal ("failed to write to ssh config file %q: %v" , * filepath , err )
83
+ return xerrors . Errorf ("failed to write to ssh config file %q: %v" , * filepath , err )
85
84
}
86
85
87
- return
86
+ return nil
88
87
}
89
88
90
89
entClient := requireAuth ()
91
90
92
91
sshAvailable := isSSHAvailable (ctx )
93
92
if ! sshAvailable {
94
- flog . Fatal ("SSH is disabled or not available for your Coder Enterprise deployment." )
93
+ return xerrors . New ("SSH is disabled or not available for your Coder Enterprise deployment." )
95
94
}
96
95
97
96
me , err := entClient .Me ()
98
97
if err != nil {
99
- flog . Fatal ("failed to fetch username: %v " , err )
98
+ return xerrors . Errorf ("failed to fetch username: %w " , err )
100
99
}
101
100
102
- envs := getEnvs (entClient )
101
+ envs , err := getEnvs (entClient )
102
+ if err != nil {
103
+ return err
104
+ }
103
105
if len (envs ) < 1 {
104
- flog . Fatal ("no environments found" )
106
+ return xerrors . New ("no environments found" )
105
107
}
106
108
newConfig , err := makeNewConfigs (me .Username , envs , startToken , startMessage , endToken )
107
109
if err != nil {
108
- flog . Fatal ("failed to make new ssh configurations: %v " , err )
110
+ return xerrors . Errorf ("failed to make new ssh configurations: %w " , err )
109
111
}
110
112
111
113
// if we find the old config, remove those chars from the string
@@ -115,17 +117,18 @@ func configSSH(filepath *string, remove *bool) func(c *cli.Context) {
115
117
116
118
err = writeStr (* filepath , currentConfig + newConfig )
117
119
if err != nil {
118
- flog . Fatal ("failed to write new configurations to ssh config file %q: %v " , filepath , err )
120
+ return xerrors . Errorf ("failed to write new configurations to ssh config file %q: %w " , filepath , err )
119
121
}
120
122
err = writeSSHKey (ctx , entClient )
121
123
if err != nil {
122
- flog . Fatal ("failed to fetch and write ssh key: %v " , err )
124
+ return xerrors . Errorf ("failed to fetch and write ssh key: %w " , err )
123
125
}
124
126
125
127
fmt .Printf ("An auto-generated ssh config was written to %q\n " , * filepath )
126
128
fmt .Printf ("Your private ssh key was written to %q\n " , privateKeyFilepath )
127
129
fmt .Println ("You should now be able to ssh into your environment" )
128
130
fmt .Printf ("For example, try running\n \n \t $ ssh coder.%s\n \n " , envs [0 ].Name )
131
+ return nil
129
132
}
130
133
}
131
134
0 commit comments