7
7
"net"
8
8
"net/url"
9
9
"os"
10
+ "os/user"
10
11
"path/filepath"
11
12
"strings"
12
13
"time"
@@ -35,7 +36,7 @@ func makeConfigSSHCmd() *cobra.Command {
35
36
return cmd
36
37
}
37
38
38
- func configSSH (filepath * string ,remove * bool )func (cmd * cobra.Command ,_ []string )error {
39
+ func configSSH (configpath * string ,remove * bool )func (cmd * cobra.Command ,_ []string )error {
39
40
startToken := "# ------------START-CODER-ENTERPRISE-----------"
40
41
startMessage := `# The following has been auto-generated by "coder config-ssh"
41
42
# to make accessing your Coder Enterprise environments easier.
@@ -51,12 +52,20 @@ func configSSH(filepath *string, remove *bool) func(cmd *cobra.Command, _ []stri
51
52
ctx ,cancel := context .WithCancel (context .Background ())
52
53
defer cancel ()
53
54
54
- currentConfig ,err := readStr (* filepath )
55
+ if strings .HasPrefix (* configpath ,"~" ) {
56
+ usr ,err := user .Current ()
57
+ if err != nil {
58
+ return xerrors .Errorf ("get user home directory: %w" ,err )
59
+ }
60
+ * configpath = strings .Replace (* configpath ,"~" ,usr .HomeDir ,1 )
61
+ }
62
+
63
+ currentConfig ,err := readStr (* configpath )
55
64
if os .IsNotExist (err ) {
56
65
// SSH configs are not always already there.
57
66
currentConfig = ""
58
67
}else if err != nil {
59
- return xerrors .Errorf ("read ssh config file %q: %w" ,filepath ,err )
68
+ return xerrors .Errorf ("read ssh config file %q: %w" ,configpath ,err )
60
69
}
61
70
62
71
startIndex := strings .Index (currentConfig ,startToken )
@@ -68,9 +77,9 @@ func configSSH(filepath *string, remove *bool) func(cmd *cobra.Command, _ []stri
68
77
}
69
78
currentConfig = currentConfig [:startIndex - 1 ]+ currentConfig [endIndex + len (endToken )+ 1 :]
70
79
71
- err = writeStr (* filepath ,currentConfig )
80
+ err = writeStr (* configpath ,currentConfig )
72
81
if err != nil {
73
- return xerrors .Errorf ("write to ssh config file %q: %v" ,* filepath ,err )
82
+ return xerrors .Errorf ("write to ssh config file %q: %v" ,* configpath ,err )
74
83
}
75
84
76
85
return nil
@@ -105,16 +114,16 @@ func configSSH(filepath *string, remove *bool) func(cmd *cobra.Command, _ []stri
105
114
currentConfig = currentConfig [:startIndex - 1 ]+ currentConfig [endIndex + len (endToken )+ 1 :]
106
115
}
107
116
108
- err = writeStr (* filepath ,currentConfig + newConfig )
117
+ err = writeStr (* configpath ,currentConfig + newConfig )
109
118
if err != nil {
110
- return xerrors .Errorf ("write new configurations to ssh config file %q: %w" ,filepath ,err )
119
+ return xerrors .Errorf ("write new configurations to ssh config file %q: %w" ,* configpath ,err )
111
120
}
112
121
err = writeSSHKey (ctx ,entClient )
113
122
if err != nil {
114
123
return xerrors .Errorf ("fetch and write ssh key: %w" ,err )
115
124
}
116
125
117
- fmt .Printf ("An auto-generated ssh config was written to %q\n " ,* filepath )
126
+ fmt .Printf ("An auto-generated ssh config was written to %q\n " ,* configpath )
118
127
fmt .Printf ("Your private ssh key was written to %q\n " ,privateKeyFilepath )
119
128
fmt .Println ("You should now be able to ssh into your environment" )
120
129
fmt .Printf ("For example, try running\n \n \t $ ssh coder.%s\n \n " ,envs [0 ].Name )