@@ -4,23 +4,28 @@ import (
4
4
"context"
5
5
"fmt"
6
6
"io/ioutil"
7
+ "net/url"
7
8
"os"
8
9
"path/filepath"
9
10
"strings"
10
11
12
+ "cdr.dev/coder-cli/internal/config"
11
13
"cdr.dev/coder-cli/internal/entclient"
12
14
"github.com/spf13/pflag"
13
15
14
16
"go.coder.com/cli"
15
17
"go.coder.com/flog"
16
18
)
17
19
20
+ var (
21
+ privateKeyFilepath = filepath .Join (os .Getenv ("HOME" ), ".ssh" , "coder_enterprise" )
22
+ )
23
+
18
24
type configSSHCmd struct {
19
25
filepath string
20
26
remove bool
21
27
22
28
startToken , startMessage , endToken string
23
- privateKeyFilepath string
24
29
}
25
30
26
31
func (cmd * configSSHCmd ) Spec () cli.CommandSpec {
@@ -47,7 +52,6 @@ func (cmd *configSSHCmd) RegisterFlags(fl *pflag.FlagSet) {
47
52
#
48
53
# You should not hand-edit this section, unless you are deleting it.`
49
54
cmd .endToken = "# ------------END-CODER-ENTERPRISE------------"
50
- cmd .privateKeyFilepath = filepath .Join (home , ".ssh" , "coder_enterprise" )
51
55
}
52
56
53
57
func (cmd * configSSHCmd ) Run (fl * pflag.FlagSet ) {
@@ -87,7 +91,10 @@ func (cmd *configSSHCmd) Run(fl *pflag.FlagSet) {
87
91
if len (envs ) < 1 {
88
92
flog .Fatal ("no environments found" )
89
93
}
90
- newConfig := cmd .makeNewConfigs (me .Username , envs )
94
+ newConfig , err := cmd .makeNewConfigs (me .Username , envs )
95
+ if err != nil {
96
+ flog .Fatal ("failed to make new ssh configurations: %v" , err )
97
+ }
91
98
92
99
// if we find the old config, remove those chars from the string
93
100
if startIndex != - 1 && endIndex != - 1 {
@@ -98,48 +105,57 @@ func (cmd *configSSHCmd) Run(fl *pflag.FlagSet) {
98
105
if err != nil {
99
106
flog .Fatal ("failed to write new configurations to ssh config file %q: %v" , cmd .filepath , err )
100
107
}
101
- err = cmd . writeSSHKey (ctx , entClient )
108
+ err = writeSSHKey (ctx , entClient )
102
109
if err != nil {
103
110
flog .Fatal ("failed to fetch and write ssh key: %v" , err )
104
111
}
105
112
106
113
fmt .Printf ("An auto-generated ssh config was written to %q\n " , cmd .filepath )
107
- fmt .Printf ("Your private ssh key was written to %q\n " , cmd . privateKeyFilepath )
114
+ fmt .Printf ("Your private ssh key was written to %q\n " , privateKeyFilepath )
108
115
fmt .Println ("You should now be able to ssh into your environment" )
109
116
fmt .Printf ("For example, try running\n \n \t $ ssh coder.%s\n \n " , envs [0 ].Name )
110
117
}
111
118
112
- func ( cmd * configSSHCmd ) writeSSHKey (ctx context.Context , client * entclient.Client ) error {
119
+ func writeSSHKey (ctx context.Context , client * entclient.Client ) error {
113
120
key , err := client .SSHKey ()
114
121
if err != nil {
115
122
return err
116
123
}
117
- err = ioutil .WriteFile (cmd . privateKeyFilepath , []byte (key .PrivateKey ), 400 )
124
+ err = ioutil .WriteFile (privateKeyFilepath , []byte (key .PrivateKey ), 400 )
118
125
if err != nil {
119
126
return err
120
127
}
121
128
return nil
122
129
}
123
130
124
- func (cmd * configSSHCmd ) makeNewConfigs (userName string , envs []entclient.Environment ) string {
131
+ func (cmd * configSSHCmd ) makeNewConfigs (userName string , envs []entclient.Environment ) (string , error ) {
132
+ u , err := config .URL .Read ()
133
+ if err != nil {
134
+ return "" , err
135
+ }
136
+ url , err := url .Parse (u )
137
+ if err != nil {
138
+ return "" , err
139
+ }
140
+
125
141
newConfig := fmt .Sprintf ("\n %s\n %s\n \n " , cmd .startToken , cmd .startMessage )
126
142
for _ , env := range envs {
127
- newConfig += cmd .makeConfig (userName , env .Name )
143
+ newConfig += cmd .makeConfig (url . Hostname (), userName , env .Name )
128
144
}
129
145
newConfig += fmt .Sprintf ("\n %s\n " , cmd .endToken )
130
146
131
- return newConfig
147
+ return newConfig , nil
132
148
}
133
149
134
- func (cmd * configSSHCmd ) makeConfig (userName , envName string ) string {
150
+ func (cmd * configSSHCmd ) makeConfig (host , userName , envName string ) string {
135
151
return fmt .Sprintf (
136
152
`Host coder.%s
137
153
HostName %s
138
154
User %s-%s
139
155
StrictHostKeyChecking no
140
156
ConnectTimeout=0
141
157
IdentityFile=%s
142
- ` , envName , "MOCK-SSHPROXY-IP" , userName , envName , cmd . privateKeyFilepath ) // TODO: get real ssh proxy ip address
158
+ ` , envName , host , userName , envName , privateKeyFilepath )
143
159
}
144
160
145
161
func writeStr (filename , data string ) error {
0 commit comments