@@ -14,29 +14,36 @@ import (
14
14
15
15
"github.com/AlecAivazis/survey/v2"
16
16
"github.com/fatih/color"
17
- "go.jetpack.io/devbox"
18
17
"go.jetpack.io/devbox/cloud/mutagen"
19
18
"go.jetpack.io/devbox/cloud/sshclient"
20
19
"go.jetpack.io/devbox/cloud/sshconfig"
21
20
"go.jetpack.io/devbox/cloud/stepper"
22
21
"go.jetpack.io/devbox/debug"
23
22
)
24
23
25
- func Shell (box * devbox. Devbox ) error {
24
+ func Shell (configDir string ) error {
26
25
setupSSHConfig ()
27
26
28
27
c := color .New (color .FgMagenta ).Add (color .Bold )
29
28
c .Println ("Devbox Cloud" )
30
29
fmt .Println ("Blazingly fast remote development that feels local" )
31
30
fmt .Print ("\n " )
32
31
33
- username := promptUsername ()
34
- s1 := stepper .Start ("Creating a virtual machine on the cloud..." )
35
- vmHostname := getVirtualMachine (username )
36
- s1 .Success ("Created virtual machine" )
32
+ username , vmHostname := parseVMEnvVar ()
33
+ if username == "" {
34
+ username = promptUsername ()
35
+ }
36
+ debug .Log ("username: %s" , username )
37
+
38
+ if vmHostname == "" {
39
+ s1 := stepper .Start ("Creating a virtual machine on the cloud..." )
40
+ vmHostname = getVirtualMachine (username )
41
+ s1 .Success ("Created virtual machine" )
42
+ }
43
+ debug .Log ("vm_hostname: %s" , vmHostname )
37
44
38
45
s2 := stepper .Start ("Starting file syncing..." )
39
- err := syncFiles (username , vmHostname , box )
46
+ err := syncFiles (username , vmHostname , configDir )
40
47
if err != nil {
41
48
s2 .Fail ("Starting file syncing [FAILED]" )
42
49
log .Fatal (err )
@@ -46,10 +53,9 @@ func Shell(box *devbox.Devbox) error {
46
53
s3 := stepper .Start ("Connecting to virtual machine..." )
47
54
time .Sleep (1 * time .Second )
48
55
s3 .Stop ("Connecting to virtual machine" )
49
-
50
56
fmt .Print ("\n " )
51
57
52
- return shell (username , vmHostname )
58
+ return shell (username , vmHostname , configDir )
53
59
}
54
60
55
61
func setupSSHConfig () {
@@ -90,6 +96,7 @@ func getVirtualMachine(username string) string {
90
96
if err != nil {
91
97
log .Fatal (err )
92
98
}
99
+ debug .Log ("gateway.devbox.sh auth response: %s" , string (bytes ))
93
100
resp := & authResponse {}
94
101
err = json .Unmarshal (bytes , resp )
95
102
if err != nil {
@@ -99,8 +106,8 @@ func getVirtualMachine(username string) string {
99
106
return resp .VMHostname
100
107
}
101
108
102
- func syncFiles (username string , hostname string , box * devbox. Devbox ) error {
103
- projectName := projectDirName (box . ConfigDir () )
109
+ func syncFiles (username , hostname , configDir string ) error {
110
+ projectName := projectDirName (configDir )
104
111
debug .Log ("Will sync files to directory: ~/code/%s" , projectName )
105
112
106
113
// TODO: instead of id, have the server return the machine's name and use that
@@ -110,13 +117,13 @@ func syncFiles(username string, hostname string, box *devbox.Devbox) error {
110
117
// If multiple projects can sync to the same machine, we need the name to also include
111
118
// the project's id.
112
119
Name : fmt .Sprintf ("devbox-%s" , id ),
113
- AlphaPath : box . ConfigDir () ,
120
+ AlphaPath : configDir ,
114
121
BetaAddress : fmt .Sprintf ("%s@%s" , username , hostname ),
115
122
// It's important that the beta path is a "clean" directory that will contain *only*
116
123
// the projects files. If we pick a pre-existing directories with other files, those
117
124
// files will be synced back to the local directory (due to two-way-sync) and pollute
118
125
// the user's local project
119
- BetaPath : "~/Code/ " , // TODO Use ~/code/{ projectName}
126
+ BetaPath : fmt . Sprintf ( "~/code/%s " , projectName ),
120
127
IgnoreVCS : true ,
121
128
SyncMode : "two-way-resolved" ,
122
129
})
@@ -127,15 +134,16 @@ func syncFiles(username string, hostname string, box *devbox.Devbox) error {
127
134
return nil
128
135
}
129
136
130
- func shell (username string , hostname string ) error {
137
+ func shell (username , hostname , configDir string ) error {
131
138
client := & sshclient.Client {
132
- Username : username ,
133
- Hostname : hostname ,
139
+ Username : username ,
140
+ Hostname : hostname ,
141
+ ProjectDirName : projectDirName (configDir ),
134
142
}
135
143
return client .Shell ()
136
144
}
137
145
138
- const defaultProjectDirName = "DevboxProject "
146
+ const defaultProjectDirName = "devbox_project "
139
147
140
148
// Ideally, we'd pass in devbox.Devbox struct and call ConfigDir but it
141
149
// makes it hard to wrap this in a test
@@ -146,3 +154,22 @@ func projectDirName(configDir string) string {
146
154
}
147
155
return name
148
156
}
157
+
158
+ func parseVMEnvVar () (username string , vmHostname string ) {
159
+ vmEnvVar := os .Getenv ("DEVBOX_VM" )
160
+ if vmEnvVar == "" {
161
+ return "" , ""
162
+ }
163
+ parts := strings .Split (vmEnvVar , "@" )
164
+
165
+ // DEVBOX_VM = <hostname>
166
+ if len (parts ) == 1 {
167
+ vmHostname = parts [0 ]
168
+ return
169
+ }
170
+
171
+ // DEVBOX_VM = <username>@<hostname>
172
+ username = parts [0 ]
173
+ vmHostname = parts [1 ]
174
+ return
175
+ }
0 commit comments