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

Add config-ssh command #17

Merged
merged 6 commits into from
May 26, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor and add success log message
  • Loading branch information
cmoog committed May 26, 2020
commit 50f9ab7ea82710766b9b487d2deb52e4499dfa7d
72 changes: 36 additions & 36 deletions cmd/coder/config_ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,11 @@ import (
"go.coder.com/flog"
)

const (
sshConfigStartToken = "# ------------START-CODER-ENTERPRISE-----------"
sshConfigStartMessage = `# The following has been auto-generated by "coder config-ssh"
# to make accessing your Coder Enterprise environments easier.
#
# To remove this blob, run:
#
# coder config-ssh --remove
#
# You should not hand-edit this section, unless you are deleting it.`
sshConfigEndToken = "# ------------END-CODER-ENTERPRISE------------"
)

type configSSHCmd struct {
filepath string
remove bool

startToken, startMessage, endToken string
}

func (cmd *configSSHCmd) Spec() cli.CommandSpec {
Expand All @@ -44,24 +33,35 @@ func (cmd *configSSHCmd) RegisterFlags(fl *pflag.FlagSet) {
fl.BoolVar(&cmd.remove, "remove", false, "remove the auto-generated Coder Enterprise ssh config")
defaultPath := filepath.Join(os.Getenv("HOME"), ".ssh", "config")
fl.StringVar(&cmd.filepath, "config-path", defaultPath, "overide the default path of your ssh config file")

cmd.startToken = "# ------------START-CODER-ENTERPRISE-----------"
cmd.startMessage = `# The following has been auto-generated by "coder config-ssh"
# to make accessing your Coder Enterprise environments easier.
#
# To remove this blob, run:
#
# coder config-ssh --remove
#
# You should not hand-edit this section, unless you are deleting it.`
cmd.endToken = "# ------------END-CODER-ENTERPRISE------------"
}

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

startIndex := strings.Index(currentConfiguration, sshConfigStartToken)
endIndex := strings.Index(currentConfiguration, sshConfigEndToken)
startIndex := strings.Index(currentConfig, cmd.startToken)
endIndex := strings.Index(currentConfig, cmd.endToken)

if cmd.remove {
if startIndex == -1 || endIndex == -1 {
flog.Fatal("the Coder Enterprise ssh configuration section could not be safely deleted or does not exist.")
flog.Fatal("the Coder Enterprise ssh configuration section could not be safely deleted or does not exist")
}
currentConfiguration = currentConfiguration[:startIndex-1] + currentConfiguration[endIndex+len(sshConfigEndToken)+1:]
currentConfig = currentConfig[:startIndex-1] + currentConfig[endIndex+len(cmd.endToken)+1:]

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

if startIndex == -1 || endIndex == -1 {
newConfiguration := makeNewConfigs(me.Username, envs)

err = writeStr(cmd.filepath, currentConfiguration+newConfiguration)
if err != nil {
flog.Fatal("failed to write new configurations to ssh config file %q: %v", cmd.filepath, err)
}
envs := getEnvs(entClient)
if len(envs) < 1 {
flog.Fatal("no environments found")
}
newConfig := cmd.makeNewConfigs(me.Username, envs)

return
// if we find the old config, remove those chars from the string
if startIndex != -1 && endIndex != -1 {
currentConfig = currentConfig[:startIndex-1] + currentConfig[endIndex+len(cmd.endToken)+1:]
}
currentConfiguration = currentConfiguration[:startIndex-1] + currentConfiguration[endIndex+len(sshConfigEndToken)+1:]
newConfiguration := makeNewConfigs(me.Username, envs)

err = writeStr(cmd.filepath, currentConfiguration+newConfiguration)
err = writeStr(cmd.filepath, currentConfig+newConfig)
if err != nil {
flog.Fatal("failed to write new configurations to ssh config file %q: %v", cmd.filepath, err)
}
fmt.Printf("An auto-generated ssh config was written to %q\n", cmd.filepath)
fmt.Println("You should now be able to ssh into your environment")
fmt.Printf("For example, try running\n\n\t$ ssh coder:%s\n\n", envs[0].Name)
}

func makeNewConfigs(userName string, envs []entclient.Environment) string {
newConfiguration := fmt.Sprintf("\n%s\n%s\n\n", sshConfigStartToken, sshConfigStartMessage)
func (cmd *configSSHCmd) makeNewConfigs(userName string, envs []entclient.Environment) string {
newConfig := fmt.Sprintf("\n%s\n%s\n\n", cmd.startToken, cmd.startMessage)
for _, env := range envs {
newConfiguration += makeConfig(userName, env.Name)
newConfig += makeConfig(userName, env.Name)
}
newConfiguration += fmt.Sprintf("\n%s\n", sshConfigEndToken)
newConfig += fmt.Sprintf("\n%s\n", cmd.endToken)

return newConfiguration
return newConfig
}

func makeConfig(userName, envName string) string {
Expand Down