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

Remove dependency on HOME env var #103

Merged
merged 1 commit into from
Aug 31, 2020
Merged
Changes from all commits
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
29 changes: 14 additions & 15 deletions internal/cmd/configssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Copy link
Contributor

Choose a reason for hiding this comment

The 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.
It has been a while, I'm looking into this to see the extent, it might have improved.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, I didn't see we were using it before already.
I checked and at least in go1.15, it does work without cgo! Which is quite neat.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested with and without cgo, and tested on windows, works as expected.
Slight note on windows, the path output has double \.
window ssh-config coder-cli

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed a fix that resolves the double \ (straight to master by accident 😰 ).

if err != nil {
return xerrors.Errorf("get user home directory: %w", err)
}

privateKeyFilepath := filepath.Join(usr.HomeDir, ".ssh", "coder_enterprise")

if strings.HasPrefix(*configpath, "~") {
usr, err := user.Current()
if err != nil {
return xerrors.Errorf("get user home directory: %w", err)
}
*configpath = strings.Replace(*configpath, "~", usr.HomeDir, 1)
}

Expand Down Expand Up @@ -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)
newConfig, err := makeNewConfigs(user.Username, envs, startToken, startMessage, endToken, privateKeyFilepath)
if err != nil {
return xerrors.Errorf("make new ssh configurations: %w", err)
}
Expand All @@ -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)
err = writeSSHKey(ctx, client, privateKeyFilepath)
if err != nil {
return xerrors.Errorf("fetch and write ssh key: %w", err)
}
Expand All @@ -135,34 +138,30 @@ func configSSH(configpath *string, remove *bool) func(cmd *cobra.Command, _ []st
}
}

var (
privateKeyFilepath = filepath.Join(os.Getenv("HOME"), ".ssh", "coder_enterprise")
)

func writeSSHKey(ctx context.Context, client *coder.Client) error {
func writeSSHKey(ctx context.Context, client *coder.Client, privateKeyPath string) error {
key, err := client.SSHKey(ctx)
if err != nil {
return err
}
return ioutil.WriteFile(privateKeyFilepath, []byte(key.PrivateKey), 0400)
return ioutil.WriteFile(privateKeyPath, []byte(key.PrivateKey), 0400)
}

func makeNewConfigs(userName string, envs []coder.Environment, startToken, startMsg, endToken string) (string, error) {
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)
newConfig += makeSSHConfig(hostname, userName, env.Name, privateKeyFilepath)
}
newConfig += fmt.Sprintf("\n%s\n", endToken)

return newConfig, nil
}

func makeSSHConfig(host, userName, envName string) string {
func makeSSHConfig(host, userName, envName, privateKeyFilepath string) string {
return fmt.Sprintf(
`Host coder.%s
HostName %s
Expand Down