makeLifecycleCmd returns a RunE handler for start/stop/shutdown/restart.
( operation string, fn func(*api.Client, *api.VM) (string, error), )
| 403 | |
| 404 | // makeLifecycleCmd returns a RunE handler for start/stop/shutdown/restart. |
| 405 | func makeLifecycleCmd( |
| 406 | operation string, |
| 407 | fn func(*api.Client, *api.VM) (string, error), |
| 408 | ) func(*cobra.Command, []string) error { |
| 409 | return func(cmd *cobra.Command, args []string) error { |
| 410 | vmid, err := parseVMID(args[0]) |
| 411 | if err != nil { |
| 412 | return printError(err) |
| 413 | } |
| 414 | |
| 415 | session, initErr := initCLISession(cmd) |
| 416 | if initErr != nil { |
| 417 | return printError(initErr) |
| 418 | } |
| 419 | |
| 420 | if session == nil { |
| 421 | return nil |
| 422 | } |
| 423 | |
| 424 | ctx := context.Background() |
| 425 | |
| 426 | vm, err := session.findVM(ctx, vmid) |
| 427 | if err != nil { |
| 428 | return printError(err) |
| 429 | } |
| 430 | |
| 431 | if vm.Template { |
| 432 | return printError(fmt.Errorf("guest %d is a template; lifecycle operations are not supported", vmid)) |
| 433 | } |
| 434 | |
| 435 | client, err := session.clientForVM(vm) |
| 436 | if err != nil { |
| 437 | return printError(err) |
| 438 | } |
| 439 | |
| 440 | upid, err := fn(client, vm) |
| 441 | if err != nil { |
| 442 | return printError(fmt.Errorf("failed to %s guest %d: %w", operation, vmid, err)) |
| 443 | } |
| 444 | |
| 445 | out := lifecycleOutput{ |
| 446 | VMID: vmid, |
| 447 | Operation: operation, |
| 448 | UPID: upid, |
| 449 | Node: vm.Node, |
| 450 | } |
| 451 | |
| 452 | if getOutputFormat(cmd) == outputTable { |
| 453 | printTable( |
| 454 | []string{"FIELD", "VALUE"}, |
| 455 | [][]string{ |
| 456 | {"VMID", strconv.Itoa(out.VMID)}, |
| 457 | {"Operation", out.Operation}, |
| 458 | {"Node", out.Node}, |
| 459 | {"UPID", out.UPID}, |
| 460 | }, |
| 461 | ) |
| 462 |
no test coverage detected