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

Commit

Permalink
Merge pull request #13 from adfinis-sygroup/feature/improve_edit
Browse files Browse the repository at this point in the history
Feature/improve edit
  • Loading branch information
keachi authored Jan 25, 2017
2 parents 5785c1a + d21022f commit b60e1fd
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,22 @@ func (c *EditCommand) Run(args []string) int {

editedData, err := ProcessSecret(data)
if err != nil {
c.Ui.Output(fmt.Sprintf("%v\nSecret has not changed.", err))
c.Ui.Error(fmt.Sprintf("%v\nSecret has not changed.", err))
return 1
}

_, err = vc.Logical().Write(path, editedData)
if err != nil {
return 1
if len(editedData) == 0 {
// Delete the secret if no key/value pairs are left
_, err = vc.Logical().Delete(path)
if err != nil {
return 1
}
c.Ui.Output(fmt.Sprintf("Secret was deleted because no K/V pairs were associated with it."))
} else {
_, err = vc.Logical().Write(path, editedData)
if err != nil {
return 1
}
}

return 0
Expand All @@ -80,8 +89,6 @@ func (c *EditCommand) Synopsis() string {
// After the file was edit it will reread the tempfile marhsal the data and clean up.
func ProcessSecret(data map[string]interface{}) (map[string]interface{}, error) {

parsedData := make(map[string]interface{})

f, err := ioutil.TempFile("", "vaultsecret")
if err != nil {
return nil, err
Expand All @@ -100,6 +107,8 @@ func ProcessSecret(data map[string]interface{}) (map[string]interface{}, error)
return nil, err
}

parsedData := make(map[string]interface{})

err = yaml.Unmarshal(editedData, parsedData)
if err != nil {
return nil, fmt.Errorf("Unable to parse yaml tempfile: %q", err)
Expand Down

0 comments on commit b60e1fd

Please sign in to comment.