forked from weaveworks/footloose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
show.go
52 lines (46 loc) · 1.38 KB
/
show.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/kingdonb/footloose/pkg/cluster"
)
var showCmd = &cobra.Command{
Use: "show [HOSTNAME]",
Aliases: []string{"status"},
Short: "Show all running machines or a single machine with a given hostname.",
Long: `Provides information about machines created by footloose in JSON or Table format.
Optionally, provide show with a hostname to look for a specific machine. Exp: 'show node0'.`,
RunE: show,
Args: cobra.MaximumNArgs(1),
}
var showOptions struct {
output string
config string
}
func init() {
showCmd.Flags().StringVarP(&showOptions.config, "config", "c", Footloose, "Cluster configuration file")
showCmd.Flags().StringVarP(&showOptions.output, "output", "o", "table", "Output formatting options: {json,table}.")
footloose.AddCommand(showCmd)
}
// show will show all machines in a given cluster.
func show(cmd *cobra.Command, args []string) error {
c, err := cluster.NewFromFile(configFile(showOptions.config))
if err != nil {
return err
}
var formatter cluster.Formatter
switch showOptions.output {
case "json":
formatter = new(cluster.JSONFormatter)
case "table":
formatter = new(cluster.TableFormatter)
default:
return fmt.Errorf("unknown formatter '%s'", showOptions.output)
}
machines, err := c.Inspect(args)
if err != nil {
return err
}
return formatter.Format(os.Stdout, machines)
}