1package command
2
3import (
4	"fmt"
5	"strings"
6
7	"github.com/mitchellh/cli"
8	"github.com/posener/complete"
9)
10
11var _ cli.Command = (*StatusCommand)(nil)
12var _ cli.CommandAutocomplete = (*StatusCommand)(nil)
13
14type StatusCommand struct {
15	*BaseCommand
16}
17
18func (c *StatusCommand) Synopsis() string {
19	return "Print seal and HA status"
20}
21
22func (c *StatusCommand) Help() string {
23	helpText := `
24Usage: vault status [options]
25
26  Prints the current state of Vault including whether it is sealed and if HA
27  mode is enabled. This command prints regardless of whether the Vault is
28  sealed.
29
30  The exit code reflects the seal status:
31
32      - 0 - unsealed
33      - 1 - error
34      - 2 - sealed
35
36` + c.Flags().Help()
37
38	return strings.TrimSpace(helpText)
39}
40
41func (c *StatusCommand) Flags() *FlagSets {
42	return c.flagSet(FlagSetHTTP | FlagSetOutputFormat)
43}
44
45func (c *StatusCommand) AutocompleteArgs() complete.Predictor {
46	return complete.PredictNothing
47}
48
49func (c *StatusCommand) AutocompleteFlags() complete.Flags {
50	return c.Flags().Completions()
51}
52
53func (c *StatusCommand) Run(args []string) int {
54	f := c.Flags()
55
56	if err := f.Parse(args); err != nil {
57		c.UI.Error(err.Error())
58		return 1
59	}
60
61	args = f.Args()
62	if len(args) > 0 {
63		c.UI.Error(fmt.Sprintf("Too many arguments (expected 0, got %d)", len(args)))
64		return 1
65	}
66
67	client, err := c.Client()
68	if err != nil {
69		c.UI.Error(err.Error())
70		// We return 2 everywhere else, but 2 is reserved for "sealed" here
71		return 1
72	}
73
74	status, err := client.Sys().SealStatus()
75	if err != nil {
76		c.UI.Error(fmt.Sprintf("Error checking seal status: %s", err))
77		return 1
78	}
79
80	// Do not return the int here yet, since we may want to return a custom error
81	// code depending on the seal status.
82	code := OutputSealStatus(c.UI, client, status)
83
84	if status.Sealed {
85		return 2
86	}
87
88	return code
89}
90