Skip to content

Commit

Permalink
fix ambiguous copy (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
fishi0x01 authored Oct 4, 2020
1 parent ee4e05b commit d715fc3
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 58 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v0.7.2 (October 4, 2020)

BUG FIXES:

* Fix copy of ambiguous sub-file ([#55](https://github.com/fishi0x01/vsh/pull/55))

## v0.7.1 (October 2, 2020)

BUG FIXES:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,4 @@ make integration-tests

## Debugging

The `-v` option enables verbose mode, which also creates a `vsh_trace.log` file to log any error object from the vault API.
`-v DEBUG` sets debug log level, which also creates a `vsh_trace.log` file to log any error object from the vault API.
27 changes: 27 additions & 0 deletions cli/command.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"os"
"path/filepath"
"strings"

Expand All @@ -16,6 +17,32 @@ type Command interface {
Parse(args []string) error
}

// Commands contains all available commands
type Commands struct {
Mv *MoveCommand
Cp *CopyCommand
Append *AppendCommand
Rm *RemoveCommand
Ls *ListCommand
Cd *CdCommand
Cat *CatCommand
Grep *GrepCommand
}

// NewCommands returns a Commands struct with all available commands
func NewCommands(client *client.Client) *Commands {
return &Commands{
Mv: NewMoveCommand(client),
Cp: NewCopyCommand(client),
Append: NewAppendCommand(client),
Rm: NewRemoveCommand(client),
Ls: NewListCommand(client),
Cd: NewCdCommand(client),
Cat: NewCatCommand(client),
Grep: NewGrepCommand(client, os.Stdout, os.Stderr),
}
}

func cmdPath(pwd string, arg string) (result string) {
result = filepath.Clean(pwd + arg)

Expand Down
2 changes: 1 addition & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (client *Client) GetType(absolutePath string) (kind PathKind) {
// Traverse traverses given absolutePath via DFS and returns sub-paths in array
func (client *Client) Traverse(absolutePath string) (paths []string) {
if client.isTopLevelPath(absolutePath) {
paths = client.topLevelTraverse(normalizedVaultPath(absolutePath))
paths = client.topLevelTraverse()
} else {
paths = client.lowLevelTraverse(normalizedVaultPath(absolutePath))
}
Expand Down
12 changes: 10 additions & 2 deletions client/traverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"
)

func (client *Client) topLevelTraverse(path string) (result []string) {
func (client *Client) topLevelTraverse() (result []string) {
for k := range client.KVBackends {
result = append(result, k)
}
Expand All @@ -24,7 +24,15 @@ func (client *Client) lowLevelTraverse(path string) (result []string) {
if keysInterface, ok := s.Data["keys"]; ok {
for _, valInterface := range keysInterface.([]interface{}) {
val := valInterface.(string)
result = append(result, client.lowLevelTraverse(path+"/"+val)...)
// prevent ambiguous dir/file to be added twice
if strings.HasSuffix(val, "/") {
// dir
result = append(result, client.lowLevelTraverse(path+"/"+val)...)
} else {
// file
leaf := strings.ReplaceAll("/"+path+"/"+val, "//", "/")
result = append(result, leaf)
}
}
}
} else {
Expand Down
76 changes: 26 additions & 50 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,6 @@ import (

var vaultClient *client.Client

type commands struct {
mv *cli.MoveCommand
cp *cli.CopyCommand
append *cli.AppendCommand
rm *cli.RemoveCommand
ls *cli.ListCommand
cd *cli.CdCommand
cat *cli.CatCommand
grep *cli.GrepCommand
}

func newCommands(client *client.Client) *commands {
return &commands{
mv: cli.NewMoveCommand(client),
cp: cli.NewCopyCommand(client),
append: cli.NewAppendCommand(client),
rm: cli.NewRemoveCommand(client),
ls: cli.NewListCommand(client),
cd: cli.NewCdCommand(client),
cat: cli.NewCatCommand(client),
grep: cli.NewGrepCommand(client, os.Stdout, os.Stderr),
}
}

var (
vshVersion = ""
verbosity = "INFO"
Expand All @@ -65,7 +41,7 @@ func executor(in string) {
// Split the input separate the command and the arguments.
in = strings.TrimSpace(in)
args := parseInput(in)
commands := newCommands(vaultClient)
commands := cli.NewCommands(vaultClient)
var cmd cli.Command
var err error

Expand Down Expand Up @@ -105,32 +81,32 @@ func executor(in string) {
}
}

func getCommand(args []string, commands *commands) (cmd cli.Command, err error) {
func getCommand(args []string, commands *cli.Commands) (cmd cli.Command, err error) {
switch args[0] {
case commands.ls.GetName():
err = commands.ls.Parse(args)
cmd = commands.ls
case commands.cd.GetName():
err = commands.cd.Parse(args)
cmd = commands.cd
case commands.mv.GetName():
err = commands.mv.Parse(args)
cmd = commands.mv
case commands.append.GetName():
err = commands.append.Parse(args)
cmd = commands.append
case commands.cp.GetName():
err = commands.cp.Parse(args)
cmd = commands.cp
case commands.rm.GetName():
err = commands.rm.Parse(args)
cmd = commands.rm
case commands.cat.GetName():
err = commands.cat.Parse(args)
cmd = commands.cat
case commands.grep.GetName():
err = commands.grep.Parse(args)
cmd = commands.grep
case commands.Ls.GetName():
err = commands.Ls.Parse(args)
cmd = commands.Ls
case commands.Cd.GetName():
err = commands.Cd.Parse(args)
cmd = commands.Cd
case commands.Mv.GetName():
err = commands.Mv.Parse(args)
cmd = commands.Mv
case commands.Append.GetName():
err = commands.Append.Parse(args)
cmd = commands.Append
case commands.Cp.GetName():
err = commands.Cp.Parse(args)
cmd = commands.Cp
case commands.Rm.GetName():
err = commands.Rm.Parse(args)
cmd = commands.Rm
case commands.Cat.GetName():
err = commands.Cat.Parse(args)
cmd = commands.Cat
case commands.Grep.GetName():
err = commands.Grep.Parse(args)
cmd = commands.Grep
default:
log.UserError("Not a valid command: %s", args[0])
return nil, fmt.Errorf("not a valid command")
Expand Down
21 changes: 17 additions & 4 deletions test/suites/commands/cp.bats
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ load ../../bin/plugins/bats-assert/load
assert_output "3"

#######################################
echo "==== case: copy ambigious directory ===="
echo "==== case: copy ambiguous directory ===="
run ${APP_BIN} -c "cp ${KV_BACKEND}/src/staging/all/ ${KV_BACKEND}/dest/staging/all/"
assert_success

Expand All @@ -161,13 +161,13 @@ load ../../bin/plugins/bats-assert/load
assert_success
assert_output "v2"

echo "ensure the ambigious file still exists"
echo "ensure the ambiguous file still exists"
run get_vault_value "value" "${KV_BACKEND}/src/staging/all"
assert_success
assert_output "all"

#######################################
echo "==== case: copy ambigious file ===="
echo "==== case: copy ambiguous file ===="
run ${APP_BIN} -c "cp ${KV_BACKEND}/src/tooling ${KV_BACKEND}/dest/tooling"
assert_success

Expand All @@ -181,12 +181,25 @@ load ../../bin/plugins/bats-assert/load
assert_success
assert_output "tooling"

echo "ensure the ambigious directory still exists"
echo "ensure the ambiguous directory still exists"
run get_vault_value "value" "${KV_BACKEND}/src/tooling/v1"
assert_success
assert_output "v1"

run get_vault_value "value" "${KV_BACKEND}/src/tooling/v2"
assert_success
assert_output "v2"

#######################################
echo "==== case: copy ambiguous directory parent ===="
run ${APP_BIN} -c "cp ${KV_BACKEND}/src/staging/ ${KV_BACKEND}/dest/staging5/"
assert_success

echo "ensure ambiguous file got copied"
run get_vault_value "value" "${KV_BACKEND}/dest/staging5/all"
assert_success
assert_output "all"
run get_vault_value "value" "${KV_BACKEND}/dest/staging/all/v2"
assert_success
assert_output "v2"
}

0 comments on commit d715fc3

Please sign in to comment.