@@ -14,22 +14,11 @@ import (
14
14
"go.coder.com/flog"
15
15
)
16
16
17
- const (
18
- sshConfigStartToken = "# ------------START-CODER-ENTERPRISE-----------"
19
- sshConfigStartMessage = `# The following has been auto-generated by "coder config-ssh"
20
- # to make accessing your Coder Enterprise environments easier.
21
- #
22
- # To remove this blob, run:
23
- #
24
- # coder config-ssh --remove
25
- #
26
- # You should not hand-edit this section, unless you are deleting it.`
27
- sshConfigEndToken = "# ------------END-CODER-ENTERPRISE------------"
28
- )
29
-
30
17
type configSSHCmd struct {
31
18
filepath string
32
19
remove bool
20
+
21
+ startToken , startMessage , endToken string
33
22
}
34
23
35
24
func (cmd * configSSHCmd ) Spec () cli.CommandSpec {
@@ -44,24 +33,35 @@ func (cmd *configSSHCmd) RegisterFlags(fl *pflag.FlagSet) {
44
33
fl .BoolVar (& cmd .remove , "remove" , false , "remove the auto-generated Coder Enterprise ssh config" )
45
34
defaultPath := filepath .Join (os .Getenv ("HOME" ), ".ssh" , "config" )
46
35
fl .StringVar (& cmd .filepath , "config-path" , defaultPath , "overide the default path of your ssh config file" )
36
+
37
+ cmd .startToken = "# ------------START-CODER-ENTERPRISE-----------"
38
+ cmd .startMessage = `# The following has been auto-generated by "coder config-ssh"
39
+ # to make accessing your Coder Enterprise environments easier.
40
+ #
41
+ # To remove this blob, run:
42
+ #
43
+ # coder config-ssh --remove
44
+ #
45
+ # You should not hand-edit this section, unless you are deleting it.`
46
+ cmd .endToken = "# ------------END-CODER-ENTERPRISE------------"
47
47
}
48
48
49
49
func (cmd * configSSHCmd ) Run (fl * pflag.FlagSet ) {
50
- currentConfiguration , err := readStr (cmd .filepath )
50
+ currentConfig , err := readStr (cmd .filepath )
51
51
if err != nil {
52
52
flog .Fatal ("failed to read ssh config file %q: %v" , cmd .filepath , err )
53
53
}
54
54
55
- startIndex := strings .Index (currentConfiguration , sshConfigStartToken )
56
- endIndex := strings .Index (currentConfiguration , sshConfigEndToken )
55
+ startIndex := strings .Index (currentConfig , cmd . startToken )
56
+ endIndex := strings .Index (currentConfig , cmd . endToken )
57
57
58
58
if cmd .remove {
59
59
if startIndex == - 1 || endIndex == - 1 {
60
- flog .Fatal ("the Coder Enterprise ssh configuration section could not be safely deleted or does not exist. " )
60
+ flog .Fatal ("the Coder Enterprise ssh configuration section could not be safely deleted or does not exist" )
61
61
}
62
- currentConfiguration = currentConfiguration [:startIndex - 1 ] + currentConfiguration [endIndex + len (sshConfigEndToken )+ 1 :]
62
+ currentConfig = currentConfig [:startIndex - 1 ] + currentConfig [endIndex + len (cmd . endToken )+ 1 :]
63
63
64
- err = writeStr (cmd .filepath , currentConfiguration )
64
+ err = writeStr (cmd .filepath , currentConfig )
65
65
if err != nil {
66
66
flog .Fatal ("failed to write to ssh config file %q: %v" , cmd .filepath , err )
67
67
}
@@ -75,35 +75,35 @@ func (cmd *configSSHCmd) Run(fl *pflag.FlagSet) {
75
75
if err != nil {
76
76
flog .Fatal ("failed to fetch username: %v" , err )
77
77
}
78
- envs := getEnvs (entClient )
79
78
80
- if startIndex == - 1 || endIndex == - 1 {
81
- newConfiguration := makeNewConfigs (me .Username , envs )
82
-
83
- err = writeStr (cmd .filepath , currentConfiguration + newConfiguration )
84
- if err != nil {
85
- flog .Fatal ("failed to write new configurations to ssh config file %q: %v" , cmd .filepath , err )
86
- }
79
+ envs := getEnvs (entClient )
80
+ if len (envs ) < 1 {
81
+ flog .Fatal ("no environments found" )
82
+ }
83
+ newConfig := cmd .makeNewConfigs (me .Username , envs )
87
84
88
- return
85
+ // if we find the old config, remove those chars from the string
86
+ if startIndex != - 1 && endIndex != - 1 {
87
+ currentConfig = currentConfig [:startIndex - 1 ] + currentConfig [endIndex + len (cmd .endToken )+ 1 :]
89
88
}
90
- currentConfiguration = currentConfiguration [:startIndex - 1 ] + currentConfiguration [endIndex + len (sshConfigEndToken )+ 1 :]
91
- newConfiguration := makeNewConfigs (me .Username , envs )
92
89
93
- err = writeStr (cmd .filepath , currentConfiguration + newConfiguration )
90
+ err = writeStr (cmd .filepath , currentConfig + newConfig )
94
91
if err != nil {
95
92
flog .Fatal ("failed to write new configurations to ssh config file %q: %v" , cmd .filepath , err )
96
93
}
94
+ fmt .Printf ("An auto-generated ssh config was written to %q\n " , cmd .filepath )
95
+ fmt .Println ("You should now be able to ssh into your environment" )
96
+ fmt .Printf ("For example, try running\n \n \t $ ssh coder:%s\n \n " , envs [0 ].Name )
97
97
}
98
98
99
- func makeNewConfigs (userName string , envs []entclient.Environment ) string {
100
- newConfiguration := fmt .Sprintf ("\n %s\n %s\n \n " , sshConfigStartToken , sshConfigStartMessage )
99
+ func ( cmd * configSSHCmd ) makeNewConfigs (userName string , envs []entclient.Environment ) string {
100
+ newConfig := fmt .Sprintf ("\n %s\n %s\n \n " , cmd . startToken , cmd . startMessage )
101
101
for _ , env := range envs {
102
- newConfiguration += makeConfig (userName , env .Name )
102
+ newConfig += makeConfig (userName , env .Name )
103
103
}
104
- newConfiguration += fmt .Sprintf ("\n %s\n " , sshConfigEndToken )
104
+ newConfig += fmt .Sprintf ("\n %s\n " , cmd . endToken )
105
105
106
- return newConfiguration
106
+ return newConfig
107
107
}
108
108
109
109
func makeConfig (userName , envName string ) string {
0 commit comments