command/write: adding force flag for when no data fields are necessary. Fixes #357

This commit is contained in:
Armon Dadgar 2015-06-18 13:51:06 -07:00
parent 7394c7bd8d
commit 3533d87746
2 changed files with 33 additions and 1 deletions

View File

@ -19,15 +19,18 @@ type WriteCommand struct {
func (c *WriteCommand) Run(args []string) int {
var format string
var force bool
flags := c.Meta.FlagSet("write", FlagSetDefault)
flags.StringVar(&format, "format", "table", "")
flags.BoolVar(&force, "force", false, "")
flags.BoolVar(&force, "f", false, "")
flags.Usage = func() { c.Ui.Error(c.Help()) }
if err := flags.Parse(args); err != nil {
return 1
}
args = flags.Args()
if len(args) < 2 {
if len(args) < 2 && !force {
c.Ui.Error("write expects at least two arguments")
flags.Usage()
return 1
@ -117,6 +120,12 @@ General Options:
not recommended. This is especially not recommended
for unsealing a vault.
Write Options:
-f | -force Force the write to continue without any data values
specified. This allows writing to keys that do not
need or expect any fields to be specified.
`
return strings.TrimSpace(helpText)
}

View File

@ -246,3 +246,26 @@ func TestWrite_Output(t *testing.T) {
t.Fatalf("bad: %s", string(ui.OutputWriter.Bytes()))
}
}
func TestWrite_force(t *testing.T) {
core, _, token := vault.TestCoreUnsealed(t)
ln, addr := http.TestServer(t, core)
defer ln.Close()
ui := new(cli.MockUi)
c := &WriteCommand{
Meta: Meta{
ClientToken: token,
Ui: ui,
},
}
args := []string{
"-address", addr,
"-force",
"sys/rotate",
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
}