Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 50f9ab7

Browse files
committed
Refactor and add success log message
1 parent 4c4540a commit 50f9ab7

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed

cmd/coder/config_ssh.go

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,11 @@ import (
1414
"go.coder.com/flog"
1515
)
1616

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-
3017
type configSSHCmd struct {
3118
filepath string
3219
remove bool
20+
21+
startToken, startMessage, endToken string
3322
}
3423

3524
func (cmd *configSSHCmd) Spec() cli.CommandSpec {
@@ -44,24 +33,35 @@ func (cmd *configSSHCmd) RegisterFlags(fl *pflag.FlagSet) {
4433
fl.BoolVar(&cmd.remove, "remove", false, "remove the auto-generated Coder Enterprise ssh config")
4534
defaultPath := filepath.Join(os.Getenv("HOME"), ".ssh", "config")
4635
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------------"
4747
}
4848

4949
func (cmd *configSSHCmd) Run(fl *pflag.FlagSet) {
50-
currentConfiguration, err := readStr(cmd.filepath)
50+
currentConfig, err := readStr(cmd.filepath)
5151
if err != nil {
5252
flog.Fatal("failed to read ssh config file %q: %v", cmd.filepath, err)
5353
}
5454

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)
5757

5858
if cmd.remove {
5959
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")
6161
}
62-
currentConfiguration = currentConfiguration[:startIndex-1] + currentConfiguration[endIndex+len(sshConfigEndToken)+1:]
62+
currentConfig = currentConfig[:startIndex-1] + currentConfig[endIndex+len(cmd.endToken)+1:]
6363

64-
err = writeStr(cmd.filepath, currentConfiguration)
64+
err = writeStr(cmd.filepath, currentConfig)
6565
if err != nil {
6666
flog.Fatal("failed to write to ssh config file %q: %v", cmd.filepath, err)
6767
}
@@ -75,35 +75,35 @@ func (cmd *configSSHCmd) Run(fl *pflag.FlagSet) {
7575
if err != nil {
7676
flog.Fatal("failed to fetch username: %v", err)
7777
}
78-
envs := getEnvs(entClient)
7978

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)
8784

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:]
8988
}
90-
currentConfiguration = currentConfiguration[:startIndex-1] + currentConfiguration[endIndex+len(sshConfigEndToken)+1:]
91-
newConfiguration := makeNewConfigs(me.Username, envs)
9289

93-
err = writeStr(cmd.filepath, currentConfiguration+newConfiguration)
90+
err = writeStr(cmd.filepath, currentConfig+newConfig)
9491
if err != nil {
9592
flog.Fatal("failed to write new configurations to ssh config file %q: %v", cmd.filepath, err)
9693
}
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)
9797
}
9898

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)
101101
for _, env := range envs {
102-
newConfiguration += makeConfig(userName, env.Name)
102+
newConfig += makeConfig(userName, env.Name)
103103
}
104-
newConfiguration += fmt.Sprintf("\n%s\n", sshConfigEndToken)
104+
newConfig += fmt.Sprintf("\n%s\n", cmd.endToken)
105105

106-
return newConfiguration
106+
return newConfig
107107
}
108108

109109
func makeConfig(userName, envName string) string {

0 commit comments

Comments
 (0)