Skip to content
This repository was archived by the owner on Oct 17, 2022. It is now read-only.

Commit 3944214

Browse files
committed
ccvm: Split up Create to keep gocyclo happy
Splitting off flag parsing increased the cyclo count slightly, so refactor Create() to move out some prepatory code to its own function. Signed-off-by: Rob Bradford <[email protected]>
1 parent a954ed7 commit 3944214

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

ccvm/ccvm.go

+21-10
Original file line numberDiff line numberDiff line change
@@ -170,25 +170,22 @@ func StartFlags() (VMSpec, error) {
170170
return customSpec, nil
171171
}
172172

173-
// Create sets up the VM
174-
func Create(ctx context.Context, workloadName string, debug bool, update bool, customSpec *VMSpec) error {
175-
var err error
176-
173+
func prepareCreate(ctx context.Context, workloadName string, debug bool, update bool, customSpec *VMSpec) (*workload, *workspace, error) {
177174
ws, err := prepareEnv(ctx)
178175
if err != nil {
179-
return err
176+
return nil, nil, err
180177
}
181178

182179
wkld, err := createWorkload(ctx, ws, workloadName)
183180
if err != nil {
184-
return err
181+
return nil, nil, err
185182
}
186183

187184
in := &wkld.spec.VM
188185

189186
err = in.mergeCustom(customSpec)
190187
if err != nil {
191-
return err
188+
return nil, nil, err
192189
}
193190

194191
ws.Mounts = in.Mounts
@@ -198,13 +195,26 @@ func Create(ctx context.Context, workloadName string, debug bool, update bool, c
198195
} else if ws.HTTPProxy != "" || ws.HTTPSProxy != "" {
199196
ws.NoProxy = ws.Hostname
200197
}
198+
201199
ws.PackageUpgrade = "false"
202200
if update {
203201
ws.PackageUpgrade = "true"
204202
}
205203

206204
if wkld.spec.NeedsNestedVM && !hostSupportsNestedKVM() {
207-
return fmt.Errorf("nested KVM is not enabled. Please enable and try again")
205+
return nil, nil, fmt.Errorf("nested KVM is not enabled. Please enable and try again")
206+
}
207+
208+
return wkld, ws, nil
209+
}
210+
211+
// Create sets up the VM
212+
func Create(ctx context.Context, workloadName string, debug bool, update bool, customSpec *VMSpec) error {
213+
var err error
214+
215+
wkld, ws, err := prepareCreate(ctx, workloadName, debug, update, customSpec)
216+
if err != nil {
217+
return err
208218
}
209219

210220
_, err = os.Stat(ws.instanceDir)
@@ -252,9 +262,10 @@ func Create(ctx context.Context, workloadName string, debug bool, update bool, c
252262
return err
253263
}
254264

255-
fmt.Printf("Booting VM with %d GB RAM and %d cpus\n", in.MemGiB, in.CPUs)
265+
spec := wkld.spec.VM
266+
fmt.Printf("Booting VM with %d GB RAM and %d cpus\n", spec.MemGiB, spec.CPUs)
256267

257-
err = bootVM(ctx, ws, in)
268+
err = bootVM(ctx, ws, &spec)
258269
if err != nil {
259270
return err
260271
}

0 commit comments

Comments
 (0)