-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add an add command #87
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/fishi0x01/vsh/client" | ||
"github.com/fishi0x01/vsh/log" | ||
) | ||
|
||
// AddCommand container for all 'append' parameters | ||
type AddCommand struct { | ||
name string | ||
args *AddCommandArgs | ||
|
||
client *client.Client | ||
} | ||
|
||
// AddCommandArgs provides a struct for go-arg parsing | ||
type AddCommandArgs struct { | ||
Key string `arg:"positional,required"` | ||
Value string `arg:"positional,required"` | ||
Path string `arg:"positional,required"` | ||
Force bool `arg:"-f,--force" help:"Overwrite key if exists"` | ||
} | ||
|
||
// Description provides detail on what the command does | ||
func (AddCommandArgs) Description() string { | ||
return "adds a key with value to a path" | ||
} | ||
|
||
// NewAddCommand creates a new AddCommand parameter container | ||
func NewAddCommand(c *client.Client) *AddCommand { | ||
return &AddCommand{ | ||
name: "add", | ||
client: c, | ||
args: &AddCommandArgs{}, | ||
} | ||
} | ||
|
||
// GetName returns the AddCommand's name identifier | ||
func (cmd *AddCommand) GetName() string { | ||
return cmd.name | ||
} | ||
|
||
// GetArgs provides the struct holding arguments for the command | ||
func (cmd *AddCommand) GetArgs() interface{} { | ||
return cmd.args | ||
} | ||
|
||
// IsSane returns true if command is sane | ||
func (cmd *AddCommand) IsSane() bool { | ||
return cmd.args.Key != "" && cmd.args.Value != "" && cmd.args.Path != "" | ||
} | ||
|
||
// PrintUsage print command usage | ||
func (cmd *AddCommand) PrintUsage() { | ||
fmt.Println(Help(cmd)) | ||
} | ||
|
||
// Parse parses the arguments into the Command and Args structs | ||
func (cmd *AddCommand) Parse(args []string) error { | ||
_, err := parseCommandArgs(args, cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Run executes 'add' with given AddCommand's parameters | ||
func (cmd *AddCommand) Run() int { | ||
path := cmdPath(cmd.client.Pwd, cmd.args.Path) | ||
|
||
pathType := cmd.client.GetType(path) | ||
if pathType != client.LEAF { | ||
log.UserError("Not a valid path for operation: %s", path) | ||
return 1 | ||
} | ||
|
||
err := cmd.addKeyValue(cmd.args.Path, cmd.args.Key, cmd.args.Value) | ||
if err != nil { | ||
log.UserError("Add failed: " + err.Error()) | ||
return 1 | ||
} | ||
|
||
return 0 | ||
} | ||
|
||
func (cmd *AddCommand) addKeyValue(path string, key string, value string) error { | ||
secret, err := cmd.client.Read(path) | ||
if err != nil { | ||
return fmt.Errorf("Read failed: %s", err) | ||
} | ||
data := secret.GetData() | ||
if _, ok := data[key]; ok && !cmd.args.Force { | ||
return fmt.Errorf("Key already exists at path: %s", path) | ||
} | ||
data[key] = value | ||
secret.SetData(data) | ||
return cmd.client.Write(path, secret) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# add | ||
|
||
```text | ||
add [-f|--force] KEY VALUE PATH | ||
``` | ||
|
||
Add operation adds or overwrites a single key at path. | ||
|
||
By default, it will add the key if it does not already exist. To overwrite the key if it exists, use the `--force` flag. | ||
|
||
```bash | ||
> cat secret/path | ||
|
||
value = 1 | ||
other = thing | ||
|
||
> add fizz buzz secret/path | ||
> cat /secret/to | ||
|
||
value = 1 | ||
fizz = buzz | ||
other = thing | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
load ../../util/common | ||
load ../../util/standard-setup | ||
load ../../bin/plugins/bats-support/load | ||
load ../../bin/plugins/bats-assert/load | ||
|
||
@test "vault-${VAULT_VERSION} ${KV_BACKEND} 'add'" { | ||
####################################### | ||
echo "==== case: add value to non existing path ====" | ||
run ${APP_BIN} -c "add test value ${KV_BACKEND}/fake/path" | ||
assert_failure | ||
|
||
####################################### | ||
echo "==== case: add key to existing path ====" | ||
run ${APP_BIN} -c "add test value ${KV_BACKEND}/src/a/foo" | ||
assert_success | ||
|
||
echo "ensure the key was written to destination" | ||
run get_vault_value "test" "${KV_BACKEND}/src/a/foo" | ||
assert_success | ||
assert_output "value" | ||
|
||
####################################### | ||
echo "==== case: add existing key to existing path ====" | ||
run ${APP_BIN} -c "add value another ${KV_BACKEND}/src/a/foo" | ||
assert_failure | ||
assert_output --partial "Key already exists at path: ${KV_BACKEND}/src/a/foo" | ||
|
||
####################################### | ||
echo "==== case: overwrite existing key to existing path ====" | ||
run ${APP_BIN} -c "add -f value another ${KV_BACKEND}/src/a/foo" | ||
assert_success | ||
|
||
echo "ensure the key was written to destination" | ||
run get_vault_value "value" "${KV_BACKEND}/src/a/foo" | ||
assert_success | ||
assert_output "another" | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CodeClimate complains that this import is redundant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I'm not sure what it's on about there. It doesn't compile without it as client is used in the file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting 🤔
Once you merge #88 and rebase, it will evaluate again. Lets see what it says then. If it fails again, I will take a look
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. Still failing. I will have a look 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Im not exactly sure what the issue here is. It seems to me that codeclimate uses a govet wrapper https://github.com/codeclimate-community/codeclimate-govet
Might be this is a bug. Running
go vet .
locally doesn't show any errors. I've marked the issue in CodeClimate as invalid