Skip to content

Commit

Permalink
Merge pull request #9 from matt4biz/get-fix
Browse files Browse the repository at this point in the history
Fix docs for the get command, add option -n to string the final newline
  • Loading branch information
matt4biz authored Dec 16, 2021
2 parents 7f2e24d + ce6bd58 commit 92549a2
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ SOURCES := $(wildcard internal/*.go cmd/*.go)
## doesn't have the -o option to name the file

envy: envy.go $(SOURCES)
go build -ldflags "-X main.version=$(version)" -o $@ ./cmd && mv $@ $(GOPATH)/bin
go build -ldflags "-X main.version=$(version)" -o $@ ./cmd && install $@ $(GOPATH)/bin

child: hack/main.go
go build -o $@ ./hack
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,15 @@ $ envy read -q test - | jq

The embedded JSON can't be processed without having the extra quote marks removed, which is what the `-q` option does (it also removes embedded newlines for convenience).

### Get
The `get` command just reads out a key (or keys of a realm) directly to stdout. The `-n` option avoids a final newline (which may cause an issue with passwords).

For example, you can use it this way to populate a command's parameter on the command line (as opposed to using an environment variable):

```
$ my-command -a=`envy get -n test/a`
```

## As a library
Envy is not just a command-line tool, it's also a library that can be used in building another tool.

Expand Down
1 change: 1 addition & 0 deletions cmd/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ Usage: envy [opts] subcommand
add realm key=value [key=value ...]
get realm[/key]
-n don't add a trailing newline
drop realm[/key]
exec realm[/key] command [args ...]
list [opts] [realm[/key]]
Expand Down
26 changes: 24 additions & 2 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/json"
"flag"
"fmt"
"strings"
)
Expand All @@ -11,6 +12,18 @@ type GetCommand struct {
}

func (cmd *GetCommand) Run() int {
fs := flag.NewFlagSet("get", flag.ContinueOnError)
raw := fs.Bool("n", false, "remove trailing newline")

fs.Usage = cmd.usage

if err := fs.Parse(cmd.args); err != nil {
cmd.usage()
return 1
}

cmd.args = fs.Args()

if len(cmd.args) < 1 {
cmd.usage()
return 1
Expand All @@ -24,13 +37,22 @@ func (cmd *GetCommand) Run() int {
var m json.RawMessage

if m, err = cmd.FetchAsJSON(cmd.args[0]); err == nil {
fmt.Fprintln(cmd.stdout, string(m))
if *raw {
fmt.Fprint(cmd.stdout, string(m))
} else {
fmt.Fprintln(cmd.stdout, string(m))
}

}
} else {
var s string

if s, err = cmd.Get(parts[0], parts[1]); err == nil {
fmt.Fprintln(cmd.stdout, s)
if *raw {
fmt.Fprint(cmd.stdout, s)
} else {
fmt.Fprintln(cmd.stdout, s)
}
}
}

Expand Down
52 changes: 48 additions & 4 deletions cmd/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"bytes"
"strings"
"fmt"
"testing"
)

Expand All @@ -27,7 +27,7 @@ func TestGet(t *testing.T) {
t.Fatalf("invalid 1st return: %d", o)
}

if s := strings.TrimSpace(stdout.String()); s != "XX" {
if s := stdout.String(); s != "XX\n" {
t.Errorf("wrong data, got %q", s)
}

Expand All @@ -41,7 +41,51 @@ func TestGet(t *testing.T) {
t.Fatalf("invalid 1st return: %d", o)
}

if s := strings.TrimSpace(stdout.String()); s != `{"a":"XX","b":"YY"}` {
t.Errorf("wrong data, got %s", s)
wanted := fmt.Sprintf("%s\n", `{"a":"XX","b":"YY"}`)

if s := stdout.String(); s != wanted {
t.Errorf("wrong data, got %q", s)
}
}

func TestGetRaw(t *testing.T) {
stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)
app := NewTestApp(t, stdout, stderr)

app.args = []string{"-n", "top/a"}

cmd := GetCommand{app}
data := map[string]string{"a": "XX", "b": "YY"}

if err := app.Add("top", data); err != nil {
t.Fatal("setup", err)
}

o := cmd.Run()

if o != 0 {
t.Errorf("errors: %s", stderr.String())
t.Fatalf("invalid 1st return: %d", o)
}

if s := stdout.String(); s != "XX" {
t.Errorf("wrong data, got %q", s)
}

stdout.Reset()

app.args = []string{"-n", "top"}
o = cmd.Run()

if o != 0 {
t.Errorf("errors: %s", stderr.String())
t.Fatalf("invalid 1st return: %d", o)
}

wanted := `{"a":"XX","b":"YY"}`

if s := stdout.String(); s != wanted {
t.Errorf("wrong data, got %q", s)
}
}

0 comments on commit 92549a2

Please sign in to comment.