-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
var_purge.go
121 lines (94 loc) · 2.77 KB
/
var_purge.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package command
import (
"errors"
"fmt"
"strconv"
"strings"
"github.com/mitchellh/cli"
"github.com/posener/complete"
)
type VarPurgeCommand struct {
Meta
}
func (c *VarPurgeCommand) Help() string {
helpText := `
Usage: nomad var purge [options] <path>
Purge is used to permanently delete an existing variable.
If ACLs are enabled, this command requires a token with the 'variables:destroy'
capability for the target variable's namespace and path.
General Options:
` + generalOptionsUsage(usageOptsDefault) + `
Purge Options:
-check-index
If set, the variable is only acted upon if the server side version's modify
index matches the provided value.
`
return strings.TrimSpace(helpText)
}
func (c *VarPurgeCommand) AutocompleteFlags() complete.Flags {
return c.Meta.AutocompleteFlags(FlagSetClient)
}
func (c *VarPurgeCommand) AutocompleteArgs() complete.Predictor {
return VariablePathPredictor(c.Meta.Client)
}
func (c *VarPurgeCommand) Synopsis() string {
return "Purge a variable"
}
func (c *VarPurgeCommand) Name() string { return "var purge" }
func (c *VarPurgeCommand) Run(args []string) int {
var checkIndexStr string
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
flags.StringVar(&checkIndexStr, "check-index", "", "")
if err := flags.Parse(args); err != nil {
return 1
}
// Check that we got one argument
args = flags.Args()
if l := len(args); l != 1 {
c.Ui.Error("This command takes one argument: <path>")
c.Ui.Error(commandErrorText(c))
return 1
}
// Parse the check-index
checkIndex, enforce, err := parseCheckIndex(checkIndexStr)
if err != nil {
switch {
case errors.Is(err, strconv.ErrRange):
c.Ui.Error(fmt.Sprintf("Invalid -check-index value %q: out of range for uint64", checkIndexStr))
case errors.Is(err, strconv.ErrSyntax):
c.Ui.Error(fmt.Sprintf("Invalid -check-index value %q: not parsable as uint64", checkIndexStr))
default:
c.Ui.Error(fmt.Sprintf("Error parsing -check-index value %q: %v", checkIndexStr, err))
}
return 1
}
if c.Meta.namespace == "*" {
c.Ui.Error(errWildcardNamespaceNotAllowed)
return 1
}
path := args[0]
// Get the HTTP client
client, err := c.Meta.Client()
if err != nil {
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
return 1
}
if enforce {
_, err = client.Variables().CheckedDelete(path, checkIndex, nil)
} else {
_, err = client.Variables().Delete(path, nil)
}
if err != nil {
if handled := handleCASError(err, c); handled {
return 1
}
c.Ui.Error(fmt.Sprintf("Error purging variable: %s", err))
return 1
}
c.Ui.Output(fmt.Sprintf("Successfully purged variable %q!", path))
return 0
}
func (c *VarPurgeCommand) GetConcurrentUI() cli.ConcurrentUi {
return cli.ConcurrentUi{Ui: c.Ui}
}