@@ -20,38 +20,51 @@ func Builder(opts *configs.BuildCmdOptions, args []string) {
2020 if err != nil {
2121 panic ("[build error] error resolve path : " + err .Error ())
2222 }
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 ()
2529 if err != nil {
26- panic ("[devc error] : " + err .Error ())
30+ panic ("[devc error] getting builder options : " + err .Error ())
2731 }
28- baseOptions = append (baseOptions , authFileOpts ... )
32+ buildOptions = append (globalOptions , buildOptions ... )
2933 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 ]}... )
3135 }
3236
3337 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 ))
3640 }
37- baseOptions = append (baseOptions , filepath .Join (abs , opts .Context ))
41+ buildOptions = append (buildOptions , filepath .Join (abs , opts .Context ))
3842
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 )
4145 if err != nil {
42- panic ("[builder error] : " + err .Error ())
46+ panic ("[builder error] stage 1 : " + err .Error ())
4347 }
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+
4556 err = env .SetupEnv (filepath .Join (abs , args [0 ]))
4657 if err != nil {
4758 panic ("[setup error] : " + err .Error ())
4859 }
4960 fmt .Printf ("[devc] env created : %v\n " , args [0 ])
5061 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 })
5568 }
5669 fmt .Printf ("\n [devc] tada! the env is all yours : [%v]\n " , args [0 ])
5770}
@@ -82,66 +95,43 @@ func Activate(opts *configs.ActivateCmdOptions, args []string) {
8295
8396// pull images and create env
8497func 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- }
9898
9999 abs , err := filepath .Abs ("" )
100100 if err != nil {
101101 panic ("[pull error] error resolve path : " + err .Error ())
102102 }
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 ] + "'" )
108106 }
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 ))
117108 if err != nil {
118- panic ("[pull error] : " + err .Error ())
109+ panic ("[devc] mount error : " + err .Error ())
119110 }
120- fmt . Println ( "[devc] container created.." )
111+
121112 err = env .SetupEnv (filepath .Join (abs , args [1 ]))
122113 if err != nil {
123114 panic ("[setup error] : " + err .Error ())
124115 }
116+ fmt .Printf ("[devc] env created : %v\n " , args [1 ])
125117 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 ]})
135124 }
136125 fmt .Printf ("\n [devc] tada! the env is all yours : [%v]\n " , args [1 ])
126+
137127}
138128
139129// login to a registry
140130func Login (opts * configs.LoginCmdOptions , args []string ) {
141131 if len (args ) != 1 {
142132 panic ("Invalid number of positional argument. Execute command with --help to get detailed usecase" )
143133 }
144- var baseOptions = getGlobalBuildahOptions ("" , "" )
134+ var baseOptions = getGlobalBuildahOptions ()
145135 baseOptions = append (baseOptions , "login" )
146136 authFileOpts , err := getAuthFileOptions ()
147137 if err != nil {
@@ -174,7 +164,7 @@ func Logout(opts *configs.LogoutCmdOptions, args []string) {
174164 if len (args ) != 1 {
175165 panic ("Invalid number of positional argument. Execute command with --help to get detailed usecase" )
176166 }
177- var baseOptions = getGlobalBuildahOptions ("" , "" )
167+ var baseOptions = getGlobalBuildahOptions ()
178168 baseOptions = append (baseOptions , "logout" )
179169 authFileOpts , err := getAuthFileOptions ()
180170 if err != nil {
@@ -196,7 +186,7 @@ func Images(opts *configs.ImagesCmdOptions, args []string) {
196186 panic ("Invalid number of positional argument. Execute command with --help to get detailed usecase" )
197187 }
198188
199- var baseOptions = getGlobalBuildahOptions ("" , "" )
189+ var baseOptions = getGlobalBuildahOptions ()
200190 baseOptions = append (baseOptions , "images" )
201191 // fmt.Println("options", buildCmd.Path, buildCmd.Args)
202192 err := runCommand (configs .Config .Buildah .Path , baseOptions )
@@ -210,7 +200,7 @@ func Prune(opts *configs.PruneCmdOptions, args []string) {
210200 if len (args ) != 0 {
211201 panic ("Invalid number of positional argument. Execute command with --help to get detailed usecase" )
212202 }
213- err := clearBuildCache ("" )
203+ err := clearBuildCache ()
214204 if err != nil {
215205 panic ("[devc prune error] : " + err .Error ())
216206 }
@@ -221,33 +211,12 @@ func Prune(opts *configs.PruneCmdOptions, args []string) {
221211 fmt .Println ("[devc] system prune complete!" )
222212}
223213
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-
245214// Remove a cached image (cache is only applicable for devc pull command)
246215func Rmi (opts * configs.RmiCmdOptions , args []string ) {
247216 if len (args ) != 1 {
248217 panic ("Invalid number of positional argument. Execute command with --help to get detailed usecase" )
249218 }
250- options := getGlobalBuildahOptions ("" , "" )
219+ options := getGlobalBuildahOptions ()
251220 options = append (options , "rmi" )
252221 options = append (options , args [0 ])
253222
0 commit comments