Skip to content

Commit

Permalink
Improve template handling output
Browse files Browse the repository at this point in the history
Fixes #1026

Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>
  • Loading branch information
dominikschulz committed Dec 29, 2018
1 parent 7f475c9 commit 0991ff5
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 42 deletions.
17 changes: 0 additions & 17 deletions pkg/action/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/gopasspw/gopass/pkg/pwgen"
"github.com/gopasspw/gopass/pkg/store/secret"
"github.com/gopasspw/gopass/pkg/store/sub"
"github.com/gopasspw/gopass/pkg/tpl"

"github.com/urfave/cli"
)
Expand Down Expand Up @@ -102,19 +101,3 @@ func (s *Action) editGetContent(ctx context.Context, name string, create bool) (
// new entry, no template
return name, nil, false, nil
}

func (s *Action) renderTemplate(ctx context.Context, name string, content []byte) ([]byte, bool) {
tmpl, found := s.Store.LookupTemplate(ctx, name)
if !found {
return content, false
}

// load template if it exists
if nc, err := tpl.Execute(ctx, string(tmpl), name, content, s.Store); err == nil {
content = nc
} else {
fmt.Fprintf(stdout, "failed to execute template: %s\n", err)
}
return content, true

}
15 changes: 6 additions & 9 deletions pkg/action/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/gopasspw/gopass/pkg/store/secret"
"github.com/gopasspw/gopass/pkg/store/sub"
"github.com/gopasspw/gopass/pkg/termio"
"github.com/gopasspw/gopass/pkg/tpl"

"github.com/fatih/color"
"github.com/urfave/cli"
Expand Down Expand Up @@ -241,16 +240,14 @@ func (s *Action) generateSetPassword(ctx context.Context, name, key, password st
// generate a completely new secret
var err error
sec := secret.New(password, "")
if tmpl, found := s.Store.LookupTemplate(ctx, name); found {
content, err := tpl.Execute(ctx, string(tmpl), name, []byte(password), s.Store)
if err != nil {
return nil, ExitError(ctx, ExitUnknown, err, "failed to execute template: %s", err)
}
sec, err = secret.Parse(content)
if err != nil {
return nil, ExitError(ctx, ExitUnknown, err, "failed to parse secret: %s", err)

if content, found := s.renderTemplate(ctx, name, []byte(password)); found {
nSec, err := secret.Parse(content)
if err == nil {
sec = nSec
}
}

ctx, err = s.Store.SetContext(sub.WithReason(ctx, "Generated Password"), name, sec)
if err != nil {
return ctx, ExitError(ctx, ExitEncrypt, err, "failed to create '%s': %s", name, err)
Expand Down
28 changes: 28 additions & 0 deletions pkg/action/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import (
"bytes"
"context"
"fmt"
"strings"

"github.com/gopasspw/gopass/pkg/editor"
"github.com/gopasspw/gopass/pkg/out"
"github.com/gopasspw/gopass/pkg/tpl"

"github.com/urfave/cli"
)
Expand Down Expand Up @@ -110,3 +113,28 @@ func (s *Action) TemplatesComplete(ctx context.Context, c *cli.Context) {
fmt.Fprintln(stdout, v)
}
}

func (s *Action) renderTemplate(ctx context.Context, name string, content []byte) ([]byte, bool) {
tName, tmpl, found := s.Store.LookupTemplate(ctx, name)
if !found {
out.Debug(ctx, "No template found for %s", name)
return content, false
}

tmplStr := strings.TrimSpace(string(tmpl))
if tmplStr == "" {
out.Debug(ctx, "Skipping empty template '%s', for %s", tName, name)
return content, false
}

// load template if it exists
nc, err := tpl.Execute(ctx, string(tmpl), name, content, s.Store)
if err != nil {
fmt.Fprintf(stdout, "failed to execute template '%s': %s\n", tName, err)
return content, false
}

out.Cyan(ctx, "Note: Using template %s", tName)

return nc, true
}
12 changes: 11 additions & 1 deletion pkg/out/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"io"
"os"
"runtime"
"strings"

"github.com/gopasspw/gopass/pkg/ctxutil"

Expand Down Expand Up @@ -38,7 +40,15 @@ func Debug(ctx context.Context, format string, args ...interface{}) {
if !ctxutil.IsDebug(ctx) {
return
}
fmt.Fprintf(Stdout, Prefix(ctx)+"[DEBUG] "+format+newline(ctx), args...)

var loc string
if _, file, line, ok := runtime.Caller(1); ok {
file = file[strings.Index(file, "pkg/"):]
file = strings.TrimPrefix(file, "pkg/")
loc = fmt.Sprintf("%s:%d ", file, line)
}

fmt.Fprintf(Stdout, Prefix(ctx)+"[DEBUG] "+loc+format+newline(ctx), args...)
}

// Black prints the string in black
Expand Down
9 changes: 3 additions & 6 deletions pkg/out/print_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,12 @@ func TestDebug(t *testing.T) {
}()

Debug(ctx, "foobar")
if buf.String() != "" {
t.Errorf("Got output: %s", buf.String())
}
assert.Equal(t, "", buf.String())

ctx = ctxutil.WithDebug(ctx, true)
Debug(ctx, "foobar")
if buf.String() != "[DEBUG] foobar\n" {
t.Errorf("Wrong output: %s", buf.String())
}
assert.Contains(t, buf.String(), "[DEBUG]")
assert.Contains(t, buf.String(), "foobar")
}

func TestColor(t *testing.T) {
Expand Down
8 changes: 6 additions & 2 deletions pkg/store/root/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package root

import (
"context"
"path/filepath"
"sort"

"github.com/gopasspw/gopass/pkg/out"
Expand All @@ -13,9 +14,12 @@ import (
)

// LookupTemplate will lookup and return a template
func (r *Store) LookupTemplate(ctx context.Context, name string) ([]byte, bool) {
func (r *Store) LookupTemplate(ctx context.Context, name string) (string, []byte, bool) {
oName := name
_, store, name := r.getStore(ctx, name)
return store.LookupTemplate(ctx, name)
tName, content, found := store.LookupTemplate(ctx, name)
tName = filepath.Join(r.MountPoint(oName), tName)
return tName, content, found
}

// TemplateTree returns a tree of all templates
Expand Down
2 changes: 1 addition & 1 deletion pkg/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type TemplateStore interface {
GetTemplate(context.Context, string) ([]byte, error)
HasTemplate(context.Context, string) bool
ListTemplates(context.Context, string) []string
LookupTemplate(context.Context, string) ([]byte, bool)
LookupTemplate(context.Context, string) (string, []byte, bool)
RemoveTemplate(context.Context, string) error
SetTemplate(context.Context, string, []byte) error
TemplateTree(context.Context) (tree.Tree, error)
Expand Down
11 changes: 7 additions & 4 deletions pkg/store/sub/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ const (
)

// LookupTemplate will lookup and return a template
func (s *Store) LookupTemplate(ctx context.Context, name string) ([]byte, bool) {
// chop off one path element until we find something
func (s *Store) LookupTemplate(ctx context.Context, name string) (string, []byte, bool) {
oName := name
// go upwards in the directory tree until we find a template
// by chopping off one path element by one.
for {
l1 := len(name)
name = filepath.Dir(name)
Expand All @@ -32,11 +34,12 @@ func (s *Store) LookupTemplate(ctx context.Context, name string) ([]byte, bool)
tpl := filepath.Join(name, TemplateFile)
if s.storage.Exists(ctx, tpl) {
if content, err := s.storage.Get(ctx, tpl); err == nil {
return content, true
out.Debug(ctx, "Found template '%s' for '%s'", tpl, oName)
return tpl, content, true
}
}
}
return []byte{}, false
return "", []byte{}, false
}

// ListTemplates will list all templates in this store
Expand Down
4 changes: 2 additions & 2 deletions pkg/store/vault/unsupported.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func (s *Store) ListTemplates(context.Context, string) []string {
}

// LookupTemplate is unsupported
func (s *Store) LookupTemplate(context.Context, string) ([]byte, bool) {
return nil, false
func (s *Store) LookupTemplate(context.Context, string) (string, []byte, bool) {
return "", nil, false
}

// RemoveTemplate is unsupported
Expand Down

0 comments on commit 0991ff5

Please sign in to comment.