- Notifications
You must be signed in to change notification settings - Fork18
Remove dependency on HOME env var#103
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -52,11 +52,14 @@ func configSSH(configpath *string, remove *bool) func(cmd *cobra.Command, _ []st | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
usr, err := user.Current() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. That is going to be a problem on some systems and will prevent us from shipping a static binaries. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Alright, I didn't see we were using it before already. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Pushed a fix that resolves the double | ||
if err != nil { | ||
return xerrors.Errorf("get user home directory: %w", err) | ||
} | ||
privateKeyFilepath := filepath.Join(usr.HomeDir, ".ssh", "coder_enterprise") | ||
if strings.HasPrefix(*configpath, "~") { | ||
*configpath = strings.Replace(*configpath, "~", usr.HomeDir, 1) | ||
} | ||
@@ -104,7 +107,7 @@ func configSSH(configpath *string, remove *bool) func(cmd *cobra.Command, _ []st | ||
if len(envs) < 1 { | ||
return xerrors.New("no environments found") | ||
} | ||
newConfig, err := makeNewConfigs(user.Username, envs, startToken, startMessage, endToken, privateKeyFilepath) | ||
if err != nil { | ||
return xerrors.Errorf("make new ssh configurations: %w", err) | ||
} | ||
@@ -122,7 +125,7 @@ func configSSH(configpath *string, remove *bool) func(cmd *cobra.Command, _ []st | ||
if err != nil { | ||
return xerrors.Errorf("write new configurations to ssh config file %q: %w", *configpath, err) | ||
} | ||
err = writeSSHKey(ctx, client, privateKeyFilepath) | ||
if err != nil { | ||
return xerrors.Errorf("fetch and write ssh key: %w", err) | ||
} | ||
@@ -135,34 +138,30 @@ func configSSH(configpath *string, remove *bool) func(cmd *cobra.Command, _ []st | ||
} | ||
} | ||
func writeSSHKey(ctx context.Context, client *coder.Client, privateKeyPath string) error { | ||
key, err := client.SSHKey(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
return ioutil.WriteFile(privateKeyPath, []byte(key.PrivateKey), 0400) | ||
} | ||
func makeNewConfigs(userName string, envs []coder.Environment, startToken, startMsg, endToken, privateKeyFilepath string) (string, error) { | ||
hostname, err := configuredHostname() | ||
if err != nil { | ||
return "", nil | ||
} | ||
newConfig := fmt.Sprintf("\n%s\n%s\n\n", startToken, startMsg) | ||
for _, env := range envs { | ||
newConfig += makeSSHConfig(hostname, userName, env.Name, privateKeyFilepath) | ||
} | ||
newConfig += fmt.Sprintf("\n%s\n", endToken) | ||
return newConfig, nil | ||
} | ||
func makeSSHConfig(host, userName, envName, privateKeyFilepath string) string { | ||
return fmt.Sprintf( | ||
`Host coder.%s | ||
HostName %s | ||