@@ -529,3 +529,55 @@ func startBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobr
529529 compTmpl , _ := bashCompleteTemplateNames (cmd )
530530 return append (compInst , compTmpl ... ), cobra .ShellCompDirectiveDefault
531531}
532+
533+ // startInteractive starts the instance after a confirmation prompt.
534+ //
535+ // If newInst is set to true, this function creates the instance too.
536+ //
537+ // If mandatory is set to true, and the answer to the confirmation prompt is No,
538+ // the function returns an error that contains an instruction to start the instance manually.
539+ func startInteractive (cmd * cobra.Command , instName string , newInst , mandatory bool ) (bool , error ) {
540+ flags := cmd .Flags ()
541+ tty , err := flags .GetBool ("tty" )
542+ if err != nil {
543+ return false , err
544+ }
545+ errS := fmt .Sprintf ("instance %q is stopped, run `limactl start %s` to start the instance" , instName , instName )
546+ if newInst {
547+ errS = fmt .Sprintf ("instance %q does not exist, run `limactl create %s` to create a new instance" , instName , instName )
548+ }
549+ if ! tty {
550+ if mandatory {
551+ return false , errors .New (errS )
552+ }
553+ return false , nil
554+ }
555+ logrus .Error (errS )
556+
557+ message := fmt .Sprintf ("Do you want to start the instance %q now?" , instName )
558+ if newInst {
559+ message = fmt .Sprintf ("Do you want to create and start the instance %q using the default template now?" , instName )
560+ }
561+ ans , err := uiutil .Confirm (message , true )
562+ if err != nil {
563+ return false , err
564+ }
565+ if ! ans {
566+ if mandatory {
567+ return ans , errors .New (errS )
568+ }
569+ return ans , nil
570+ }
571+ if newInst {
572+ rootCmd := cmd .Root ()
573+ // The create command shows the template chooser UI, etc.
574+ rootCmd .SetArgs ([]string {"create" , instName })
575+ if err := rootCmd .Execute (); err != nil {
576+ return ans , err
577+ }
578+ }
579+ rootCmd := cmd .Root ()
580+ // The start command reconciliates the networks, etc.
581+ rootCmd .SetArgs ([]string {"start" , instName })
582+ return ans , rootCmd .Execute ()
583+ }
0 commit comments