@@ -23,6 +23,7 @@ import (
2323 "github.com/lima-vm/lima/pkg/templatestore"
2424 "github.com/lima-vm/lima/pkg/uiutil"
2525 "github.com/lima-vm/lima/pkg/yqutil"
26+ "github.com/mattn/go-isatty"
2627 "github.com/sirupsen/logrus"
2728 "github.com/spf13/cobra"
2829)
@@ -529,3 +530,55 @@ func startBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobr
529530 compTmpl , _ := bashCompleteTemplateNames (cmd )
530531 return append (compInst , compTmpl ... ), cobra .ShellCompDirectiveDefault
531532}
533+
534+ // interactive returns true if --tty is true and both STDIN and STDOUT are terminals.
535+ func interactive (cmd * cobra.Command ) (bool , error ) {
536+ flags := cmd .Flags ()
537+ tty , err := flags .GetBool ("tty" )
538+ if err != nil {
539+ return false , err
540+ }
541+ if ! isatty .IsTerminal (os .Stdin .Fd ()) && ! isatty .IsCygwinTerminal (os .Stdin .Fd ()) {
542+ tty = false
543+ }
544+ if ! isatty .IsTerminal (os .Stdout .Fd ()) && ! isatty .IsCygwinTerminal (os .Stdout .Fd ()) {
545+ tty = false
546+ }
547+ return tty , nil
548+ }
549+
550+ func askToStart (cmd * cobra.Command , instName string , create bool ) error {
551+ template := "default"
552+ templates , err := templatestore .Templates ()
553+ if err != nil {
554+ return err
555+ }
556+ for _ , t := range templates {
557+ if t .Name == instName {
558+ template = instName
559+ break
560+ }
561+ }
562+ var message string
563+ if create {
564+ message = fmt .Sprintf ("Do you want to create and start the instance %q using the %q template now?" , instName , template )
565+ } else {
566+ message = fmt .Sprintf ("Do you want to start the instance %q now?" , instName )
567+ }
568+ ans , err := uiutil .Confirm (message , true )
569+ if ! ans || err != nil {
570+ return err
571+ }
572+
573+ rootCmd := cmd .Root ()
574+ if create {
575+ // The create command shows the template chooser UI, etc.
576+ rootCmd .SetArgs ([]string {"create" , "template://" + template })
577+ if err := rootCmd .Execute (); err != nil {
578+ return err
579+ }
580+ }
581+ // The start command reconciles the networks, etc.
582+ rootCmd .SetArgs ([]string {"start" , instName })
583+ return rootCmd .Execute ()
584+ }
0 commit comments