MCPcopy Create free account
hub / github.com/devnullvoid/pvetui / makeLifecycleCmd

Function makeLifecycleCmd

internal/cli/guests.go:405–468  ·  view source on GitHub ↗

makeLifecycleCmd returns a RunE handler for start/stop/shutdown/restart.

(
	operation string,
	fn func(*api.Client, *api.VM) (string, error),
)

Source from the content-addressed store, hash-verified

403
404// makeLifecycleCmd returns a RunE handler for start/stop/shutdown/restart.
405func 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

Callers 4

newGuestsStartCmdFunction · 0.85
newGuestsStopCmdFunction · 0.85
newGuestsShutdownCmdFunction · 0.85
newGuestsRestartCmdFunction · 0.85

Calls 8

parseVMIDFunction · 0.85
printErrorFunction · 0.85
initCLISessionFunction · 0.85
getOutputFormatFunction · 0.85
printTableFunction · 0.85
printJSONFunction · 0.85
findVMMethod · 0.80
clientForVMMethod · 0.80

Tested by

no test coverage detected