Skip to content
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

Proper logging #52

Merged
merged 1 commit into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
*.swp
build/
test/bin
vsh_trace.log

5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
## v0.7.1 (TBD)
## v0.7.1 (October 2, 2020)

BUG FIXES:

* Proper return codes [#51](https://github.com/fishi0x01/vsh/pull/51)
* Proper return codes ([#51](https://github.com/fishi0x01/vsh/pull/51))
* Proper logging ([#52](https://github.com/fishi0x01/vsh/pull/52))

## v0.7.0 (September 26, 2020)

Expand Down
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ get-bats:
mkdir -p test/bin/core
mkdir -p test/bin/plugins/bats-assert
mkdir -p test/bin/plugins/bats-support
mkdir -p test/bin/plugins/bats-file
curl -sL https://github.com/bats-core/bats-core/archive/v1.2.0.tar.gz | tar xvz --strip 1 -C test/bin/core
curl -sL https://github.com/bats-core/bats-assert/archive/v2.0.0.tar.gz | tar xvz --strip 1 -C test/bin/plugins/bats-assert
curl -sL https://github.com/bats-core/bats-support/archive/v0.3.0.tar.gz | tar xvz --strip 1 -C test/bin/plugins/bats-support
curl -sL https://github.com/bats-core/bats-file/archive/v0.2.0.tar.gz | tar xvz --strip 1 -C test/bin/plugins/bats-file

integration-tests:
test/run.sh
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ Working on vault secrets can be critical, making quality and correct behavior a
That being said, `vsh` is still a small open source project, meaning we cannot make any guarantees.
However, we put strong emphasis on [TDD](https://en.wikipedia.org/wiki/Test-driven_development).
Every PR is tested with an extensive [suite](test/suites) of integration tests.
Most tests run on KV1 and KV2 and every test runs against vault `1.0.0` and `1.5.3`, i.e., versions in between should also be compatible.
Most tests run on KV1 and KV2 and every test runs against vault `1.0.0` and `1.5.4`, i.e., versions in between should also be compatible.

## Local Development

Expand All @@ -193,3 +193,7 @@ make compile
make get-bats
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.
25 changes: 10 additions & 15 deletions cli/append.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cli

import (
"fmt"
"io"
"strconv"
"strings"

Expand Down Expand Up @@ -30,20 +29,16 @@ type AppendCommand struct {
name string

client *client.Client
stderr io.Writer
stdout io.Writer
Source string
Target string
Mode AppendMode
}

// NewAppendCommand creates a new AppendCommand parameter container
func NewAppendCommand(c *client.Client, stdout io.Writer, stderr io.Writer) *AppendCommand {
func NewAppendCommand(c *client.Client) *AppendCommand {
return &AppendCommand{
name: "append",
client: c,
stdout: stdout,
stderr: stderr,
Mode: ModeSkip,
}
}
Expand All @@ -58,10 +53,6 @@ func (cmd *AppendCommand) IsSane() bool {
return cmd.Source != "" && cmd.Target != "" && cmd.Mode != ModeInvalid
}

func printUsage() {
fmt.Println("Usage:\nappend <from> <to> [-f|--force|-r|--rename|-s|--skip]")
}

func isFlag(flag string) bool {
return strings.HasPrefix(flag, "-")
}
Expand Down Expand Up @@ -108,11 +99,15 @@ func (cmd *AppendCommand) tryParse(args []string) (success bool) {
return false
}

// PrintUsage print command usage
func (cmd *AppendCommand) PrintUsage() {
log.UserInfo("Usage:\nappend <from> <to> [-f|--force|-r|--rename|-s|--skip]")
}

// Parse parses the arguments and returns true on success; otherwise it prints usage and returns false
func (cmd *AppendCommand) Parse(args []string) error {
success := cmd.tryParse(args)
if !success {
printUsage()
return fmt.Errorf("cannot parse arguments")
}
return nil
Expand All @@ -125,12 +120,12 @@ func (cmd *AppendCommand) Run() int {

src := cmd.client.GetType(newSrcPwd)
if src != client.LEAF {
log.NotAValidPath(newSrcPwd)
log.UserError("Not a valid path for operation: %s", newSrcPwd)
return 1
}

if err := cmd.mergeSecrets(newSrcPwd, newTargetPwd); err != nil {
log.Error("Append failed: " + err.Error())
log.AppError("Append failed: " + err.Error())
return 1
}
return 0
Expand Down Expand Up @@ -193,9 +188,9 @@ func (cmd *AppendCommand) mergeSecrets(source string, target string) error {
fmt.Println(err)
return err
}
log.Info("Appended values from %s to %s", source, target)
log.UserDebug("Appended values from %s to %s", source, target)
if len(skippedKeys) > 0 {
log.Info("Handled conflicting keys according to the '%s' strategy. Keys: %s", onConflict, strings.Join(skippedKeys, ", "))
log.UserDebug("Handled conflicting keys according to the '%s' strategy. Keys: %s", onConflict, strings.Join(skippedKeys, ", "))
}
return nil
}
Expand Down
20 changes: 9 additions & 11 deletions cli/cat.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package cli

import (
"fmt"
"io"

"github.com/fishi0x01/vsh/client"
"github.com/fishi0x01/vsh/log"
)
Expand All @@ -13,18 +11,14 @@ type CatCommand struct {
name string

client *client.Client
stderr io.Writer
stdout io.Writer
Path string
}

// NewCatCommand creates a new CatCommand parameter container
func NewCatCommand(c *client.Client, stdout io.Writer, stderr io.Writer) *CatCommand {
func NewCatCommand(c *client.Client) *CatCommand {
return &CatCommand{
name: "cat",
client: c,
stderr: stderr,
stdout: stdout,
}
}

Expand All @@ -38,10 +32,14 @@ func (cmd *CatCommand) IsSane() bool {
return cmd.Path != ""
}

// PrintUsage print command usage
func (cmd *CatCommand) PrintUsage() {
log.UserInfo("Usage:\ncat <secret>")
}

// Parse given arguments and return status
func (cmd *CatCommand) Parse(args []string) error {
if len(args) != 2 {
fmt.Println("Usage:\ncat <secret>")
return fmt.Errorf("cannot parse arguments")
}
cmd.Path = args[1]
Expand All @@ -63,15 +61,15 @@ func (cmd *CatCommand) Run() int {
if rec, ok := v.(map[string]interface{}); ok {
// KV 2
for kk, vv := range rec {
fmt.Fprintln(cmd.stdout, kk, "=", vv)
log.UserInfo("%s = %s", kk, vv)
}
} else {
// KV 1
fmt.Fprintln(cmd.stdout, k, "=", v)
log.UserInfo("%s = %s", k, v)
}
}
} else {
log.NotAValidPath(absPath)
log.UserError("Not a valid path for operation: %s", absPath)
return 1
}
return 0
Expand Down
17 changes: 8 additions & 9 deletions cli/cd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cli

import (
"fmt"
"io"
"strings"

"github.com/fishi0x01/vsh/client"
Expand All @@ -14,18 +13,14 @@ type CdCommand struct {
name string

client *client.Client
stderr io.Writer
stdout io.Writer
Path string
}

// NewCdCommand creates a new CdCommand parameter container
func NewCdCommand(c *client.Client, stdout io.Writer, stderr io.Writer) *CdCommand {
func NewCdCommand(c *client.Client) *CdCommand {
return &CdCommand{
name: "cd",
client: c,
stdout: stdout,
stderr: stderr,
}
}

Expand All @@ -39,10 +34,14 @@ func (cmd *CdCommand) IsSane() bool {
return cmd.Path != ""
}

// PrintUsage print command usage
func (cmd *CdCommand) PrintUsage() {
log.UserInfo("Usage:\ncd <path>")
}

// Parse given arguments and return status
func (cmd *CdCommand) Parse(args []string) error {
if len(args) != 2 {
fmt.Println("Usage:\ncd <path>")
return fmt.Errorf("cannot parse arguments")
}
cmd.Path = args[1]
Expand All @@ -56,12 +55,12 @@ func (cmd *CdCommand) Run() int {
t := cmd.client.GetType(newPwd)

if t == client.NONE {
log.NotAValidPath(newPwd)
log.UserError("Not a valid path for operation: %s", newPwd)
return 1
}

if t == client.LEAF {
log.NotAValidPath(newPwd)
log.UserError("Not a valid path for operation: %s", newPwd)
return 1
}

Expand Down
1 change: 1 addition & 0 deletions cli/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Command interface {
Run() int
GetName() string
IsSane() bool
PrintUsage()
Parse(args []string) error
}

Expand Down
17 changes: 8 additions & 9 deletions cli/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cli

import (
"fmt"
"io"
"path/filepath"

"github.com/fishi0x01/vsh/client"
Expand All @@ -14,19 +13,15 @@ type CopyCommand struct {
name string

client *client.Client
stderr io.Writer
stdout io.Writer
Source string
Target string
}

// NewCopyCommand creates a new CopyCommand parameter container
func NewCopyCommand(c *client.Client, stdout io.Writer, stderr io.Writer) *CopyCommand {
func NewCopyCommand(c *client.Client) *CopyCommand {
return &CopyCommand{
name: "cp",
client: c,
stdout: stdout,
stderr: stderr,
}
}

Expand All @@ -40,10 +35,14 @@ func (cmd *CopyCommand) IsSane() bool {
return cmd.Source != "" && cmd.Target != ""
}

// PrintUsage print command usage
func (cmd *CopyCommand) PrintUsage() {
log.UserInfo("Usage:\ncp <from> <to>")
}

// Parse given arguments and return status
func (cmd *CopyCommand) Parse(args []string) error {
if len(args) != 3 {
fmt.Println("Usage:\ncp <from> <to>")
return fmt.Errorf("cannot parse arguments")
}
cmd.Source = args[1]
Expand All @@ -62,7 +61,7 @@ func (cmd *CopyCommand) Run() int {
case client.NODE:
runCommandWithTraverseTwoPaths(cmd.client, newSrcPwd, newTargetPwd, cmd.copySecret)
default:
log.NotAValidPath(newSrcPwd)
log.UserError("Not a valid path for operation: %s", newSrcPwd)
return 1
}

Expand All @@ -83,7 +82,7 @@ func (cmd *CopyCommand) copySecret(source string, target string) error {
return err
}

log.Info("Copied %s to %s", source, target)
log.UserDebug("Copied %s to %s", source, target)

return nil
}
10 changes: 7 additions & 3 deletions cli/grep.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,14 @@ func (cmd *GrepCommand) IsSane() bool {
return cmd.Path != "" && cmd.Search != ""
}

// PrintUsage print command usage
func (cmd *GrepCommand) PrintUsage() {
log.UserInfo("Usage:\ngrep <term-string> <path>")
}

// Parse given arguments and return status
func (cmd *GrepCommand) Parse(args []string) error {
if len(args) != 3 {
fmt.Println("Usage:\ngrep <term-string> <path>")
return fmt.Errorf("cannot parse arguments")
}
cmd.Search = args[1]
Expand All @@ -68,7 +72,7 @@ func (cmd *GrepCommand) Parse(args []string) error {
// Run executes 'grep' with given RemoveCommand's parameters
func (cmd *GrepCommand) Run() int {
path := cmdPath(cmd.client.Pwd, cmd.Path)
filePaths := []string{}
var filePaths []string

switch t := cmd.client.GetType(path); t {
case client.LEAF:
Expand All @@ -78,7 +82,7 @@ func (cmd *GrepCommand) Run() int {
filePaths = append(filePaths, traversedPath)
}
default:
log.NotAValidPath(path)
log.UserError("Not a valid path for operation: %s", path)
return 1
}

Expand Down
Loading