From ef068b134c49e8ca927e94afa364c939ef50498f Mon Sep 17 00:00:00 2001 From: Jan Dubois Date: Wed, 28 Apr 2021 11:33:29 -0700 Subject: [PATCH] Add status command to check if a machine is running or not Signed-off-by: Jan Dubois --- cmd/status.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 cmd/status.go diff --git a/cmd/status.go b/cmd/status.go new file mode 100644 index 0000000..7a637c5 --- /dev/null +++ b/cmd/status.go @@ -0,0 +1,45 @@ +package cmd + +import ( + "fmt" + "strings" + + "github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(statusCmd) +} + +var statusCmd = &cobra.Command{ + Use: "status", + Short: "Print status of a machine.", + Long: `Print status of a machine.`, + RunE: statusCommand, +} + +func statusCommand(cmd *cobra.Command, args []string) error { + api := newAPI() + defer api.Close() + + host, err := api.Load(machineName) + if err != nil { + if strings.Contains(strings.ToLower(err.Error()), "does not exist") { + fmt.Println("Does not exist") + return nil + } + return fmt.Errorf("error loading config for host %s: %v", machineName, err) + } + + currentState, err := host.Driver.GetState() + if err != nil { + if strings.Contains(strings.ToLower(err.Error()), "not found") { + fmt.Println("Not found") + return nil + } + return fmt.Errorf("error getting state for host %s: %s", host.Name, err) + } + + fmt.Println(currentState) + return nil +}