Skip to content

Commit

Permalink
Improve output (#392)
Browse files Browse the repository at this point in the history
This commit introduces a new out package to unify the output and
improve visibility and structure.
  • Loading branch information
dominikschulz authored Oct 11, 2017
1 parent 9eba7bb commit df5aa3d
Show file tree
Hide file tree
Showing 58 changed files with 1,114 additions and 1,429 deletions.
13 changes: 7 additions & 6 deletions action/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"

"github.com/fatih/color"
"github.com/justwatchcom/gopass/utils/out"
"github.com/muesli/crunchy"
"github.com/muesli/goprogressbar"
"github.com/urfave/cli"
Expand Down Expand Up @@ -91,19 +92,19 @@ func (s *Action) Audit(ctx context.Context, c *cli.Context) error {
if len(secrets) > 1 {
foundDuplicates = true

fmt.Println(color.CyanString("Detected a shared secret for:"))
out.Cyan(ctx, "Detected a shared secret for:")
for _, secret := range secrets {
fmt.Println(color.CyanString("\t- %s", secret))
out.Cyan(ctx, "\t- %s", secret)
}
}
}
if !foundDuplicates {
fmt.Println(color.GreenString("No shared secrets found."))
out.Green(ctx, "No shared secrets found.")
}

foundWeakPasswords := printAuditResults(ctx, messages, "%s:\n", color.CyanString)
if !foundWeakPasswords {
fmt.Println(color.GreenString("No weak secrets detected."))
out.Green(ctx, "No weak secrets detected.")
}
foundErrors := printAuditResults(ctx, errors, "%s:\n", color.RedString)

Expand Down Expand Up @@ -157,9 +158,9 @@ func printAuditResults(ctx context.Context, m map[string][]string, format string
return b
}

func printAuditResult(pw string) {
func printAuditResult(ctx context.Context, pw string) {
validator := crunchy.NewValidator()
if err := validator.Check(pw); err != nil {
fmt.Println(color.CyanString(fmt.Sprintf("Warning: %s", err)))
out.Cyan(ctx, fmt.Sprintf("Warning: %s", err))
}
}
3 changes: 2 additions & 1 deletion action/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/justwatchcom/gopass/store/secret"
"github.com/justwatchcom/gopass/store/sub"
"github.com/justwatchcom/gopass/utils/fsutil"
"github.com/justwatchcom/gopass/utils/out"
"github.com/pkg/errors"
"github.com/urfave/cli"
)
Expand Down Expand Up @@ -80,7 +81,7 @@ func (s *Action) BinarySum(ctx context.Context, c *cli.Context) error {

h := sha256.New()
_, _ = h.Write(buf)
fmt.Println(color.YellowString("%x", h.Sum(nil)))
out.Yellow(ctx, "%x", h.Sum(nil))

return nil
}
Expand Down
4 changes: 2 additions & 2 deletions action/clihelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"strconv"
"strings"

"github.com/fatih/color"
"github.com/justwatchcom/gopass/utils/ctxutil"
"github.com/justwatchcom/gopass/utils/out"
"github.com/pkg/errors"
)

Expand All @@ -37,7 +37,7 @@ func (s *Action) ConfirmRecipients(ctx context.Context, name string, recipients

kl, err := s.gpg.FindPublicKeys(ctx, r)
if err != nil {
fmt.Println(color.RedString("Failed to read public key for '%s': %s", name, err))
out.Red(ctx, "Failed to read public key for '%s': %s", name, err)
continue
}
if len(kl) < 1 {
Expand Down
6 changes: 3 additions & 3 deletions action/clihelper_others.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"os"
"os/signal"

"github.com/fatih/color"
"github.com/justwatchcom/gopass/utils/ctxutil"
"github.com/justwatchcom/gopass/utils/out"
"github.com/pkg/errors"
"golang.org/x/crypto/ssh/terminal"
)
Expand All @@ -28,7 +28,7 @@ func (s *Action) promptPass(ctx context.Context, prompt string) (pass string, er
}
defer func() {
if err := terminal.Restore(fd, oldState); err != nil {
fmt.Println(color.RedString("Failed to restore terminal: %s", err))
out.Red(ctx, "Failed to restore terminal: %s", err)
}
}()

Expand All @@ -38,7 +38,7 @@ func (s *Action) promptPass(ctx context.Context, prompt string) (pass string, er
go func() {
for range sigch {
if err := terminal.Restore(fd, oldState); err != nil {
fmt.Println(color.RedString("Failed to restore terminal: %s", err))
out.Red(ctx, "Failed to restore terminal: %s", err)
}
os.Exit(1)
}
Expand Down
4 changes: 2 additions & 2 deletions action/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"os"
"os/exec"

"github.com/fatih/color"
"github.com/justwatchcom/gopass/config"
"github.com/justwatchcom/gopass/utils/fsutil"
"github.com/justwatchcom/gopass/utils/out"
"github.com/pkg/errors"
"github.com/urfave/cli"
)
Expand Down Expand Up @@ -59,7 +59,7 @@ func (s *Action) clone(ctx context.Context, repo, mount, path string) error {
return s.exitError(ctx, ExitIO, err, "Failed to update config: %s", err)
}

fmt.Println(color.GreenString("Your password store is ready to use! Have a look around: `%s %s`\n", s.Name, mount))
out.Green(ctx, "Your password store is ready to use! Have a look around: `%s %s`\n", s.Name, mount)

return nil
}
Expand Down
7 changes: 4 additions & 3 deletions action/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/justwatchcom/gopass/store/secret"
"github.com/justwatchcom/gopass/store/sub"
"github.com/justwatchcom/gopass/utils/fsutil"
"github.com/justwatchcom/gopass/utils/out"
"github.com/justwatchcom/gopass/utils/pwgen"
"github.com/justwatchcom/gopass/utils/tpl"
shellquote "github.com/kballard/go-shellquote"
Expand Down Expand Up @@ -40,7 +41,7 @@ func (s *Action) Edit(ctx context.Context, c *cli.Context) error {
} else if tmpl, found := s.Store.LookupTemplate(ctx, name); found {
changed = true
// load template if it exists
content = pwgen.GeneratePassword(defaultLength, false)
content = []byte(pwgen.GeneratePassword(defaultLength, false))
if nc, err := tpl.Execute(ctx, string(tmpl), name, content, s.Store); err == nil {
content = nc
} else {
Expand All @@ -60,11 +61,11 @@ func (s *Action) Edit(ctx context.Context, c *cli.Context) error {

nSec, err := secret.Parse(nContent)
if err != nil {
fmt.Println(color.RedString("WARNING: Invalid YAML: %s", err))
out.Red(ctx, "WARNING: Invalid YAML: %s", err)
}

if pw := nSec.Password(); pw != "" {
printAuditResult(pw)
printAuditResult(ctx, pw)
}

return s.Store.Set(sub.WithReason(ctx, fmt.Sprintf("Edited with %s", getEditor())), name, nSec)
Expand Down
7 changes: 3 additions & 4 deletions action/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import (
"context"
"fmt"

"github.com/fatih/color"
"github.com/justwatchcom/gopass/utils/ctxutil"
"github.com/justwatchcom/gopass/utils/out"
"github.com/urfave/cli"
)

Expand Down Expand Up @@ -53,8 +52,8 @@ const (
)

func (s *Action) exitError(ctx context.Context, exitCode int, err error, format string, args ...interface{}) error {
if ctxutil.IsDebug(ctx) && err != nil {
fmt.Println(color.RedString("Stacktrace: %+v", err))
if err != nil {
out.Debug(ctx, "Stacktrace: %+v", err)
}
return cli.NewExitError(fmt.Sprintf(format, args...), exitCode)
}
4 changes: 2 additions & 2 deletions action/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"fmt"
"strings"

"github.com/fatih/color"
"github.com/justwatchcom/gopass/utils/ctxutil"
"github.com/justwatchcom/gopass/utils/out"
"github.com/justwatchcom/gopass/utils/termwiz"
"github.com/schollz/closestmatch"
"github.com/urfave/cli"
Expand Down Expand Up @@ -34,7 +34,7 @@ func (s *Action) Find(ctx context.Context, c *cli.Context) error {
clip := c.Bool("clip")

if len(choices) == 1 {
fmt.Println(color.GreenString("Found exact match in '%s'", choices[0]))
out.Green(ctx, "Found exact match in '%s'", choices[0])
return s.show(ctx, c, choices[0], "", clip, false, false, false)
}

Expand Down
5 changes: 2 additions & 3 deletions action/fsck.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package action

import (
"context"
"fmt"
"os"
"path/filepath"

"github.com/fatih/color"
"github.com/justwatchcom/gopass/config"
"github.com/justwatchcom/gopass/store/sub"
"github.com/justwatchcom/gopass/utils/fsutil"
"github.com/justwatchcom/gopass/utils/out"
"github.com/urfave/cli"
)

Expand All @@ -30,7 +29,7 @@ func (s *Action) Fsck(ctx context.Context, c *cli.Context) error {
oldCfg := filepath.Join(config.Homedir(), ".gopass.yml")
if fsutil.IsFile(oldCfg) {
if err := os.Remove(oldCfg); err != nil {
fmt.Println(color.RedString("Failed to remove old gopass config %s: %s", oldCfg, err))
out.Red(ctx, "Failed to remove old gopass config %s: %s", oldCfg, err)
}
}

Expand Down
17 changes: 7 additions & 10 deletions action/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
"github.com/justwatchcom/gopass/store/secret"
"github.com/justwatchcom/gopass/store/sub"
"github.com/justwatchcom/gopass/utils/ctxutil"
"github.com/justwatchcom/gopass/utils/out"
"github.com/justwatchcom/gopass/utils/pwgen"
"github.com/martinhoefling/goxkcdpwgen/xkcdpwgen"
"github.com/justwatchcom/gopass/utils/pwgen/xkcdgen"
"github.com/urfave/cli"
)

Expand All @@ -32,7 +33,7 @@ func (s *Action) Generate(ctx context.Context, c *cli.Context) error {
}

if c.IsSet("no-symbols") {
fmt.Println(color.RedString("Warning: -n/--no-symbols is deprecated. This is now the default. Use -s to enable symbols"))
out.Red(ctx, "Warning: -n/--no-symbols is deprecated. This is now the default. Use -s to enable symbols")
}

name := c.Args().Get(0)
Expand Down Expand Up @@ -83,13 +84,9 @@ func (s *Action) Generate(ctx context.Context, c *cli.Context) error {
return s.exitError(ctx, ExitUsage, nil, "password length must not be zero")
}

var password []byte
var password string
if xkcd {
g := xkcdpwgen.NewGenerator()
g.SetNumWords(pwlen)
g.SetDelimiter(xkcdSeparator)
g.SetCapitalize(xkcdSeparator == "")
password = g.GeneratePassword()
password = xkcdgen.RandomLengthDelim(pwlen, xkcdSeparator)
} else {
password = pwgen.GeneratePassword(pwlen, symbols)
}
Expand All @@ -111,7 +108,7 @@ func (s *Action) Generate(ctx context.Context, c *cli.Context) error {
if err != nil {
return s.exitError(ctx, ExitEncrypt, err, "failed to set key '%s' of '%s': %s", key, name, err)
}
sec.SetPassword(string(password))
sec.SetPassword(password)
if err := s.Store.Set(sub.WithReason(ctx, "Generated password for YAML key"), name, sec); err != nil {
return s.exitError(ctx, ExitEncrypt, err, "failed to set key '%s' of '%s': %s", key, name, err)
}
Expand All @@ -122,7 +119,7 @@ func (s *Action) Generate(ctx context.Context, c *cli.Context) error {
}

if c.Bool("clip") {
return s.copyToClipboard(ctx, name, password)
return s.copyToClipboard(ctx, name, []byte(password))
}

if key != "" {
Expand Down
6 changes: 3 additions & 3 deletions action/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package action

import (
"context"
"fmt"

"github.com/fatih/color"
"github.com/justwatchcom/gopass/utils/out"
"github.com/pkg/errors"
"github.com/urfave/cli"
)
Expand Down Expand Up @@ -36,7 +36,7 @@ func (s *Action) GitInit(ctx context.Context, c *cli.Context) error {
}

func (s *Action) gitInit(ctx context.Context, store, sk string) error {
fmt.Println(color.GreenString("Initializing embedded password store git repo ..."))
out.Green(ctx, "Initializing git repository ...")
if sk == "" {
s, err := s.askForPrivateKey(ctx, color.CyanString("Please select a key for signing Git Commits"))
if err == nil {
Expand Down Expand Up @@ -67,6 +67,6 @@ func (s *Action) gitInit(ctx context.Context, store, sk string) error {
return errors.Wrapf(err, "failed to run git init")
}

fmt.Println(color.GreenString("Git initialized\n"))
out.Green(ctx, "Git initialized")
return nil
}
3 changes: 2 additions & 1 deletion action/grep.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/fatih/color"
"github.com/justwatchcom/gopass/utils/out"
"github.com/urfave/cli"
)

Expand All @@ -25,7 +26,7 @@ func (s *Action) Grep(ctx context.Context, c *cli.Context) error {
for _, v := range l {
sec, err := s.Store.Get(ctx, v)
if err != nil {
fmt.Println(color.RedString("failed to decrypt %s: %v", v, err))
out.Red(ctx, "failed to decrypt %s: %v", v, err)
continue
}

Expand Down
27 changes: 11 additions & 16 deletions action/hibp.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"time"

"github.com/fatih/color"
"github.com/justwatchcom/gopass/utils/ctxutil"
"github.com/justwatchcom/gopass/utils/out"
"github.com/muesli/goprogressbar"
"github.com/pkg/errors"
"github.com/urfave/cli"
Expand Down Expand Up @@ -102,15 +102,15 @@ func (s *Action) HIBP(ctx context.Context, c *cli.Context) error {
}

if len(matchList) < 0 {
fmt.Println(color.GreenString("Good news - No matches found!"))
out.Green(ctx, "Good news - No matches found!")
return nil
}
sort.Strings(matchList)
fmt.Println(color.RedString("Oh no - Found some matches:"))
out.Red(ctx, "Oh no - Found some matches:")
for _, m := range matchList {
fmt.Println(color.RedString("\t- %s", m))
out.Red(ctx, "\t- %s", m)
}
fmt.Println(color.CyanString("The passwords in the listed secrets were included in public leaks in the past. This means they are likely included in many word-list attacks and provide only very little security. Strongly consider changing those passwords!"))
out.Cyan(ctx, "The passwords in the listed secrets were included in public leaks in the past. This means they are likely included in many word-list attacks and provide only very little security. Strongly consider changing those passwords!")
return s.exitError(ctx, ExitAudit, nil, "weak passwords found")
}

Expand All @@ -122,16 +122,14 @@ func (s *Action) findHIBPMatches(ctx context.Context, fn string, shaSums map[str

fh, err := os.Open(fn)
if err != nil {
fmt.Println(color.RedString("Failed to open file %s: %s", fn, err))
out.Red(ctx, "Failed to open file %s: %s", fn, err)
return
}
defer func() {
_ = fh.Close()
}()

if ctxutil.IsDebug(ctx) {
fmt.Printf("Checking file %s ...\n", fn)
}
out.Debug(ctx, "Checking file %s ...\n", fn)

// index in sortedShaSums
i := 0
Expand All @@ -154,9 +152,7 @@ SCAN:
}
if line == sortedShaSums[i] {
matches <- shaSums[line]
if ctxutil.IsDebug(ctx) {
fmt.Printf("MATCH at line %d: %s / %s from %s\n", lineNo, line, shaSums[line], fn)
}
out.Debug(ctx, "MATCH at line %d: %s / %s from %s", lineNo, line, shaSums[line], fn)
numMatches++
// advance to next sha sum from store and next line in file
i++
Expand All @@ -168,10 +164,9 @@ SCAN:
i++
}
}
if ctxutil.IsDebug(ctx) {
d0 := time.Since(t0)
fmt.Printf("Found %d matches in %d lines from %s in %.2fs (%.2f lines / s)\n", numMatches, lineNo, fn, d0.Seconds(), float64(lineNo)/d0.Seconds())
}

d0 := time.Since(t0)
out.Debug(ctx, "Found %d matches in %d lines from %s in %.2fs (%.2f lines / s)\n", numMatches, lineNo, fn, d0.Seconds(), float64(lineNo)/d0.Seconds())
}

func sha1sum(data string) string {
Expand Down
Loading

0 comments on commit df5aa3d

Please sign in to comment.