Skip to content
This repository has been archived by the owner on Aug 12, 2021. It is now read-only.

Commit

Permalink
Add status command to check if a machine is running or not
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Dubois <jan.dubois@suse.com>
  • Loading branch information
jandubois committed Apr 28, 2021
1 parent 6c22fb0 commit ef068b1
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions cmd/status.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit ef068b1

Please sign in to comment.