@@ -20,38 +20,51 @@ func Builder(opts *configs.BuildCmdOptions, args []string) {
20
20
if err != nil {
21
21
panic ("[build error] error resolve path : " + err .Error ())
22
22
}
23
- var baseOptions = append (getGlobalBuildahOptions (abs , args [0 ]), getBuildOptions (abs , args [0 ])... )
24
- authFileOpts , err := getAuthFileOptions ()
23
+ _ , err = os .Stat (filepath .Join (abs , args [0 ]))
24
+ if err == nil {
25
+ panic ("[ERROR] directory already exists! '" + args [0 ] + "'" )
26
+ }
27
+ var globalOptions = getGlobalBuildahOptions ()
28
+ imgName , buildOptions , err := getBuildOptions ()
25
29
if err != nil {
26
- panic ("[devc error] : " + err .Error ())
30
+ panic ("[devc error] getting builder options : " + err .Error ())
27
31
}
28
- baseOptions = append (baseOptions , authFileOpts ... )
32
+ buildOptions = append (globalOptions , buildOptions ... )
29
33
for i := 0 ; i < len (opts .BuildArgs ); i ++ {
30
- baseOptions = append (baseOptions , []string {"--build-arg" , opts .BuildArgs [i ]}... )
34
+ buildOptions = append (buildOptions , []string {"--build-arg" , opts .BuildArgs [i ]}... )
31
35
}
32
36
33
37
if len (opts .Containerfile ) > 0 {
34
- baseOptions = append (baseOptions , "--file" )
35
- baseOptions = append (baseOptions , filepath .Join (abs , opts .Containerfile ))
38
+ buildOptions = append (buildOptions , "--file" )
39
+ buildOptions = append (buildOptions , filepath .Join (abs , opts .Containerfile ))
36
40
}
37
- baseOptions = append (baseOptions , filepath .Join (abs , opts .Context ))
41
+ buildOptions = append (buildOptions , filepath .Join (abs , opts .Context ))
38
42
39
- // fmt.Println("options", buildCmd.Path, buildCmd.Args )
40
- err = runCommand (configs .Config .Buildah .Path , baseOptions )
43
+ fmt .Println ("[devc] building container.." )
44
+ err = runCommand (configs .Config .Buildah .Path , buildOptions )
41
45
if err != nil {
42
- panic ("[builder error] : " + err .Error ())
46
+ panic ("[builder error] stage 1 : " + err .Error ())
43
47
}
44
- fmt .Println ("[devc] container created" )
48
+ fmt .Println ("[devc] image created.." )
49
+ fmt .Println ("[devc] creating devc env " )
50
+
51
+ err = exportImageAsRootFs (imgName , filepath .Join (abs , args [0 ], configs .Config .EnvSettings .RootDir ))
52
+ if err != nil {
53
+ panic ("[devc] mount error : " + err .Error ())
54
+ }
55
+
45
56
err = env .SetupEnv (filepath .Join (abs , args [0 ]))
46
57
if err != nil {
47
58
panic ("[setup error] : " + err .Error ())
48
59
}
49
60
fmt .Printf ("[devc] env created : %v\n " , args [0 ])
50
61
if ! opts .KeepCache {
51
- err = clearBuildCache (args [0 ])
52
- if err != nil {
53
- panic ("[finishup error] : " + err .Error ())
54
- }
62
+ defer func () {
63
+ if v := recover (); v != nil {
64
+ panic (fmt .Sprint ("[finishup error] : " , v ))
65
+ }
66
+ }()
67
+ Rmi (& configs.RmiCmdOptions {}, []string {imgName })
55
68
}
56
69
fmt .Printf ("\n [devc] tada! the env is all yours : [%v]\n " , args [0 ])
57
70
}
@@ -82,66 +95,43 @@ func Activate(opts *configs.ActivateCmdOptions, args []string) {
82
95
83
96
// pull images and create env
84
97
func Pull (opts * configs.PullCmdOptions , args []string ) {
85
- if len (args ) != 2 {
86
- panic ("Invalid number of positional argument. Execute command with --help to get detailed usecase" )
87
- }
88
- file , err := os .CreateTemp ("" , "pullcmd" )
89
- if err != nil {
90
- panic ("[pull error] : " + err .Error ())
91
- }
92
- defer file .Close ()
93
- defer os .Remove (file .Name ())
94
- _ , err = file .Write ([]byte (fmt .Sprintf ("FROM %v" , args [0 ])))
95
- if err != nil {
96
- panic ("[pull error] : " + err .Error ())
97
- }
98
98
99
99
abs , err := filepath .Abs ("" )
100
100
if err != nil {
101
101
panic ("[pull error] error resolve path : " + err .Error ())
102
102
}
103
-
104
- var baseOptions = append (getGlobalBuildahOptions ("" , "" ), getBuildOptions (abs , args [1 ])... )
105
- authFileOpts , err := getAuthFileOptions ()
106
- if err != nil {
107
- panic ("[devc error] : " + err .Error ())
103
+ _ , err = os .Stat (filepath .Join (abs , args [1 ]))
104
+ if err == nil {
105
+ panic ("[ERROR] directory already exists! '" + args [1 ] + "'" )
108
106
}
109
- baseOptions = append (baseOptions , authFileOpts ... )
110
-
111
- baseOptions = append (baseOptions , "--file" )
112
- baseOptions = append (baseOptions , file .Name ())
113
- baseOptions = append (baseOptions , "." )
114
-
115
- // fmt.Println("options", buildCmd.Path, buildCmd.Args)
116
- err = runCommand (configs .Config .Buildah .Path , baseOptions )
107
+ err = exportImageAsRootFs (args [0 ], filepath .Join (abs , args [1 ], configs .Config .EnvSettings .RootDir ))
117
108
if err != nil {
118
- panic ("[pull error] : " + err .Error ())
109
+ panic ("[devc] mount error : " + err .Error ())
119
110
}
120
- fmt . Println ( "[devc] container created.." )
111
+
121
112
err = env .SetupEnv (filepath .Join (abs , args [1 ]))
122
113
if err != nil {
123
114
panic ("[setup error] : " + err .Error ())
124
115
}
116
+ fmt .Printf ("[devc] env created : %v\n " , args [1 ])
125
117
if opts .NoCaching {
126
- baseOptions = getGlobalBuildahOptions ("" , "" )
127
- baseOptions = append (baseOptions , "rmi" )
128
- baseOptions = append (baseOptions , args [0 ])
129
-
130
- err := runCommand (configs .Config .Buildah .Path , baseOptions )
131
- if err != nil {
132
- panic ("[devc cleanup error] : " + err .Error ())
133
- }
134
- fmt .Println ("[devc] pull cache removed.." )
118
+ defer func () {
119
+ if v := recover (); v != nil {
120
+ panic (fmt .Sprint ("[finishup error] : " , v ))
121
+ }
122
+ }()
123
+ Rmi (& configs.RmiCmdOptions {}, []string {args [0 ]})
135
124
}
136
125
fmt .Printf ("\n [devc] tada! the env is all yours : [%v]\n " , args [1 ])
126
+
137
127
}
138
128
139
129
// login to a registry
140
130
func Login (opts * configs.LoginCmdOptions , args []string ) {
141
131
if len (args ) != 1 {
142
132
panic ("Invalid number of positional argument. Execute command with --help to get detailed usecase" )
143
133
}
144
- var baseOptions = getGlobalBuildahOptions ("" , "" )
134
+ var baseOptions = getGlobalBuildahOptions ()
145
135
baseOptions = append (baseOptions , "login" )
146
136
authFileOpts , err := getAuthFileOptions ()
147
137
if err != nil {
@@ -174,7 +164,7 @@ func Logout(opts *configs.LogoutCmdOptions, args []string) {
174
164
if len (args ) != 1 {
175
165
panic ("Invalid number of positional argument. Execute command with --help to get detailed usecase" )
176
166
}
177
- var baseOptions = getGlobalBuildahOptions ("" , "" )
167
+ var baseOptions = getGlobalBuildahOptions ()
178
168
baseOptions = append (baseOptions , "logout" )
179
169
authFileOpts , err := getAuthFileOptions ()
180
170
if err != nil {
@@ -196,7 +186,7 @@ func Images(opts *configs.ImagesCmdOptions, args []string) {
196
186
panic ("Invalid number of positional argument. Execute command with --help to get detailed usecase" )
197
187
}
198
188
199
- var baseOptions = getGlobalBuildahOptions ("" , "" )
189
+ var baseOptions = getGlobalBuildahOptions ()
200
190
baseOptions = append (baseOptions , "images" )
201
191
// fmt.Println("options", buildCmd.Path, buildCmd.Args)
202
192
err := runCommand (configs .Config .Buildah .Path , baseOptions )
@@ -210,7 +200,7 @@ func Prune(opts *configs.PruneCmdOptions, args []string) {
210
200
if len (args ) != 0 {
211
201
panic ("Invalid number of positional argument. Execute command with --help to get detailed usecase" )
212
202
}
213
- err := clearBuildCache ("" )
203
+ err := clearBuildCache ()
214
204
if err != nil {
215
205
panic ("[devc prune error] : " + err .Error ())
216
206
}
@@ -221,33 +211,12 @@ func Prune(opts *configs.PruneCmdOptions, args []string) {
221
211
fmt .Println ("[devc] system prune complete!" )
222
212
}
223
213
224
- // Remove an existing devc env (simply deleting it will also work)
225
- func Rm (opts * configs.RmCmdOptions , args []string ) {
226
- if len (args ) != 1 {
227
- panic ("Invalid number of positional argument. Execute command with --help to get detailed usecase" )
228
- }
229
- // check if it's a valid env
230
- _ , err := os .Stat (filepath .Join (args [0 ], configs .Config .EnvSettings .DevcBin , "activate" ))
231
- if err != nil {
232
- panic ("[devc rm error] : seems not to be a devc environment" )
233
- }
234
- err = clearBuildCache (args [0 ])
235
- if err != nil {
236
- panic ("[devc rm error] : " + err .Error ())
237
- }
238
- err = runCommand ("rm" , []string {"-rf" , args [0 ]})
239
- if err != nil {
240
- panic ("[devc rm error] : " + err .Error ())
241
- }
242
- fmt .Println ("[devc] env removed successfully!" )
243
- }
244
-
245
214
// Remove a cached image (cache is only applicable for devc pull command)
246
215
func Rmi (opts * configs.RmiCmdOptions , args []string ) {
247
216
if len (args ) != 1 {
248
217
panic ("Invalid number of positional argument. Execute command with --help to get detailed usecase" )
249
218
}
250
- options := getGlobalBuildahOptions ("" , "" )
219
+ options := getGlobalBuildahOptions ()
251
220
options = append (options , "rmi" )
252
221
options = append (options , args [0 ])
253
222
0 commit comments