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

Commit a954ed7

Browse files
committed
main, ccvm: Extract flag parsing out of work functions
Move flag parsing from Create/Start into their own exported functions in preparation for using cobra to parse these. Signed-off-by: Rob Bradford <[email protected]>
1 parent 596b4f5 commit a954ed7

File tree

2 files changed

+55
-64
lines changed

2 files changed

+55
-64
lines changed

ccvm/ccvm.go

+39-54
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,6 @@ func (p *ports) Set(value string) error {
7575
return nil
7676
}
7777

78-
type packageUpgrade string
79-
80-
func (p *packageUpgrade) String() string {
81-
return string(*p)
82-
}
83-
84-
func (p *packageUpgrade) Set(value string) error {
85-
if value != "true" && value != "false" {
86-
return fmt.Errorf("--package-update parameter should be true or false")
87-
}
88-
*p = packageUpgrade(value)
89-
return nil
90-
}
91-
9278
type drives []drive
9379

9480
func (d *drives) String() string {
@@ -142,9 +128,10 @@ func checkDirectory(dir string) error {
142128
return nil
143129
}
144130

145-
func createFlags(ctx context.Context, ws *workspace) (*workload, bool, error) {
131+
// CreateFlags parses the flags to the create command
132+
func CreateFlags() (string, bool, bool, VMSpec, error) {
146133
var debug bool
147-
var update packageUpgrade
134+
var update bool
148135
var customSpec VMSpec
149136
fs := flag.NewFlagSet("create", flag.ExitOnError)
150137
fs.Usage = func() {
@@ -154,74 +141,72 @@ func createFlags(ctx context.Context, ws *workspace) (*workload, bool, error) {
154141
}
155142
vmFlags(fs, &customSpec)
156143
fs.BoolVar(&debug, "debug", false, "Enables debug mode")
157-
fs.Var(&update, "package-upgrade",
144+
fs.BoolVar(&update, "package-upgrade", false,
158145
"Hint to enable or disable update of VM packages. Should be true or false")
159146

160147
if err := fs.Parse(flag.Args()[1:]); err != nil {
161-
return nil, false, err
148+
return "", debug, update, customSpec, err
162149
}
163150

164151
if fs.NArg() != 1 {
165152
fs.Usage()
166-
return nil, false, errors.New("no workload specified")
153+
return "", debug, update, customSpec, errors.New("no workload specified")
167154
}
168155
workloadName := fs.Arg(0)
169156

170-
wkl, err := createWorkload(ctx, ws, workloadName)
171-
if err != nil {
172-
return nil, false, err
173-
}
174-
175-
in := &wkl.spec.VM
176-
177-
err = in.mergeCustom(&customSpec)
178-
if err != nil {
179-
return nil, false, err
180-
}
181-
182-
ws.Mounts = in.Mounts
183-
ws.Hostname = wkl.spec.Hostname
184-
if ws.NoProxy != "" {
185-
ws.NoProxy = fmt.Sprintf("%s,%s", ws.Hostname, ws.NoProxy)
186-
} else if ws.HTTPProxy != "" || ws.HTTPSProxy != "" {
187-
ws.NoProxy = ws.Hostname
188-
}
189-
ws.PackageUpgrade = string(update)
190-
191-
return wkl, debug, nil
157+
return workloadName, debug, update, customSpec, nil
192158
}
193159

194-
func startFlags(in *VMSpec) error {
195-
customSpec := *in
160+
// StartFlags parsed the flags for the start command
161+
func StartFlags() (VMSpec, error) {
162+
var customSpec VMSpec
196163

197164
fs := flag.NewFlagSet("start", flag.ExitOnError)
198165
vmFlags(fs, &customSpec)
199166
if err := fs.Parse(flag.Args()[1:]); err != nil {
200-
return err
167+
return customSpec, err
201168
}
202169

203-
return in.mergeCustom(&customSpec)
170+
return customSpec, nil
204171
}
205172

206173
// Create sets up the VM
207-
func Create(ctx context.Context) error {
174+
func Create(ctx context.Context, workloadName string, debug bool, update bool, customSpec *VMSpec) error {
208175
var err error
209176

210177
ws, err := prepareEnv(ctx)
211178
if err != nil {
212179
return err
213180
}
214181

215-
wkld, debug, err := createFlags(ctx, ws)
182+
wkld, err := createWorkload(ctx, ws, workloadName)
216183
if err != nil {
217184
return err
218185
}
219186

187+
in := &wkld.spec.VM
188+
189+
err = in.mergeCustom(customSpec)
190+
if err != nil {
191+
return err
192+
}
193+
194+
ws.Mounts = in.Mounts
195+
ws.Hostname = wkld.spec.Hostname
196+
if ws.NoProxy != "" {
197+
ws.NoProxy = fmt.Sprintf("%s,%s", ws.Hostname, ws.NoProxy)
198+
} else if ws.HTTPProxy != "" || ws.HTTPSProxy != "" {
199+
ws.NoProxy = ws.Hostname
200+
}
201+
ws.PackageUpgrade = "false"
202+
if update {
203+
ws.PackageUpgrade = "true"
204+
}
205+
220206
if wkld.spec.NeedsNestedVM && !hostSupportsNestedKVM() {
221207
return fmt.Errorf("nested KVM is not enabled. Please enable and try again")
222208
}
223209

224-
in := &wkld.spec.VM
225210
_, err = os.Stat(ws.instanceDir)
226211
if err == nil {
227212
return fmt.Errorf("instance already exists")
@@ -286,7 +271,7 @@ func Create(ctx context.Context) error {
286271
}
287272

288273
// Start launches the VM
289-
func Start(ctx context.Context) error {
274+
func Start(ctx context.Context, customSpec *VMSpec) error {
290275
ws, err := prepareEnv(ctx)
291276
if err != nil {
292277
return err
@@ -298,6 +283,11 @@ func Start(ctx context.Context) error {
298283
}
299284
in := &wkld.spec.VM
300285

286+
err = in.mergeCustom(customSpec)
287+
if err != nil {
288+
return err
289+
}
290+
301291
memGiB, CPUs := getMemAndCpus()
302292
if in.MemGiB == 0 {
303293
in.MemGiB = memGiB
@@ -306,11 +296,6 @@ func Start(ctx context.Context) error {
306296
in.CPUs = CPUs
307297
}
308298

309-
err = startFlags(in)
310-
if err != nil {
311-
return err
312-
}
313-
314299
if wkld.spec.NeedsNestedVM && !hostSupportsNestedKVM() {
315300
return fmt.Errorf("nested KVM is not enabled. Please enable and try again")
316301
}

main.go

+16-10
Original file line numberDiff line numberDiff line change
@@ -48,26 +48,32 @@ func init() {
4848
}
4949

5050
func runCommand(ctx context.Context) error {
51-
var err error
52-
5351
switch os.Args[1] {
5452
case "create":
55-
err = ccvm.Create(ctx)
53+
workloadName, debug, update, customSpec, err := ccvm.CreateFlags()
54+
if err != nil {
55+
return err
56+
}
57+
return ccvm.Create(ctx, workloadName, debug, update, &customSpec)
5658
case "start":
57-
err = ccvm.Start(ctx)
59+
customSpec, err := ccvm.StartFlags()
60+
if err != nil {
61+
return err
62+
}
63+
return ccvm.Start(ctx, &customSpec)
5864
case "stop":
59-
err = ccvm.Stop(ctx)
65+
return ccvm.Stop(ctx)
6066
case "quit":
61-
err = ccvm.Quit(ctx)
67+
return ccvm.Quit(ctx)
6268
case "status":
63-
err = ccvm.Status(ctx)
69+
return ccvm.Status(ctx)
6470
case "connect":
65-
err = ccvm.Connect(ctx)
71+
return ccvm.Connect(ctx)
6672
case "delete":
67-
err = ccvm.Delete(ctx)
73+
return ccvm.Delete(ctx)
6874
}
6975

70-
return err
76+
return nil
7177
}
7278

7379
func getSignalContext() (context.Context, context.CancelFunc) {

0 commit comments

Comments
 (0)