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

Refactor to reduce code complexity #541

Merged
merged 1 commit into from
Dec 20, 2017
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
4 changes: 4 additions & 0 deletions action/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ func (s *Action) Audit(ctx context.Context, c *cli.Context) error {
close(checked)
fmt.Println() // Print empty line after the progressbar.

return s.auditPrintResults(ctx, duplicates, messages, errors)
}

func (s *Action) auditPrintResults(ctx context.Context, duplicates, messages, errors map[string][]string) error {
foundDuplicates := false
for _, secrets := range duplicates {
if len(secrets) > 1 {
Expand Down
22 changes: 2 additions & 20 deletions action/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ package action
import (
"context"
"fmt"
"os"
"os/exec"

"github.com/fatih/color"
git "github.com/justwatchcom/gopass/backend/git/cli"
"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 @@ -40,7 +37,7 @@ func (s *Action) clone(ctx context.Context, repo, mount, path string) error {
}

// clone repo
if err := gitClone(ctx, repo, path); err != nil {
if _, err := git.Clone(ctx, s.gpg.Binary(), repo, path); err != nil {
return exitError(ctx, ExitGit, err, "failed to clone repo '%s' to '%s'", repo, path)
}

Expand Down Expand Up @@ -95,18 +92,3 @@ func (s *Action) clone(ctx context.Context, repo, mount, path string) error {

return nil
}

func gitClone(ctx context.Context, repo, path string) error {
if fsutil.IsDir(path) {
return errors.Errorf("%s is a directory that already exists", path)
}

fmt.Printf("Cloning repository %s to %s ...\n", repo, path)

cmd := exec.CommandContext(ctx, "git", "clone", repo, path)
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr

return cmd.Run()
}
26 changes: 15 additions & 11 deletions action/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,25 @@ func (s *Action) Delete(ctx context.Context, c *cli.Context) error {

// deletes a single key from a YAML doc
if key != "" {
sec, err := s.Store.Get(ctx, name)
if err != nil {
return exitError(ctx, ExitIO, err, "Can not delete key '%s' from '%s': %s", key, name, err)
}
if err := sec.DeleteKey(key); err != nil {
return exitError(ctx, ExitIO, err, "Can not delete key '%s' from '%s': %s", key, name, err)
}
if err := s.Store.Set(sub.WithReason(ctx, "Updated Key in YAML"), name, sec); err != nil {
return exitError(ctx, ExitIO, err, "Can not delete key '%s' from '%s': %s", key, name, err)
}
return nil
return s.deleteKeyFromYAML(ctx, name, key)
}

if err := s.Store.Delete(ctx, name); err != nil {
return exitError(ctx, ExitIO, err, "Can not delete '%s': %s", name, err)
}
return nil
}

func (s *Action) deleteKeyFromYAML(ctx context.Context, name, key string) error {
sec, err := s.Store.Get(ctx, name)
if err != nil {
return exitError(ctx, ExitIO, err, "Can not delete key '%s' from '%s': %s", key, name, err)
}
if err := sec.DeleteKey(key); err != nil {
return exitError(ctx, ExitIO, err, "Can not delete key '%s' from '%s': %s", key, name, err)
}
if err := s.Store.Set(sub.WithReason(ctx, "Updated Key in YAML"), name, sec); err != nil {
return exitError(ctx, ExitIO, err, "Can not delete key '%s' from '%s': %s", key, name, err)
}
return nil
}
4 changes: 4 additions & 0 deletions action/hibp.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ func (s *Action) HIBP(ctx context.Context, c *cli.Context) error {
<-done
}

return s.printHIBPMatches(ctx, matchList)
}

func (s *Action) printHIBPMatches(ctx context.Context, matchList []string) error {
if len(matchList) < 0 {
_ = notify.Notify("gopass - audit HIBP", "Good news - No matches found!")
out.Green(ctx, "Good news - No matches found!")
Expand Down
18 changes: 11 additions & 7 deletions action/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,7 @@ func (s *Action) init(ctx context.Context, alias, path string, nogit bool, keys
}

out.Green(ctx, "Password store %s initialized for:", path)
for _, recipient := range s.Store.ListRecipients(ctx, alias) {
r := "0x" + recipient
if kl, err := s.gpg.FindPublicKeys(ctx, recipient); err == nil && len(kl) > 0 {
r = kl[0].OneLine()
}
out.Yellow(ctx, " "+r)
}
s.printRecipients(ctx, path, alias)

// write config
if err := s.cfg.Save(); err != nil {
Expand All @@ -108,6 +102,16 @@ func (s *Action) init(ctx context.Context, alias, path string, nogit bool, keys
return nil
}

func (s *Action) printRecipients(ctx context.Context, path, alias string) {
for _, recipient := range s.Store.ListRecipients(ctx, alias) {
r := "0x" + recipient
if kl, err := s.gpg.FindPublicKeys(ctx, recipient); err == nil && len(kl) > 0 {
r = kl[0].OneLine()
}
out.Yellow(ctx, " "+r)
}
}

// InitOnboarding will invoke the onboarding / setup wizard
func (s *Action) InitOnboarding(ctx context.Context, c *cli.Context) error {
remote := c.String("remote")
Expand Down
53 changes: 31 additions & 22 deletions action/recipients.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,29 +134,11 @@ func (s *Action) RecipientsRemove(ctx context.Context, c *cli.Context) error {
// select recipient
recipients := []string(c.Args())
if len(recipients) < 1 {
ids := s.Store.ListRecipients(ctx, store)
choices := make([]string, 0, len(ids))
kl, err := s.gpg.FindPublicKeys(ctx, ids...)
if err == nil && kl != nil {
for _, id := range ids {
if key, err := kl.FindKey(id); err == nil {
choices = append(choices, key.OneLine())
continue
}
choices = append(choices, id)
}
}
if len(choices) > 0 {
act, sel := termwiz.GetSelection(ctx, "Remove recipient -", "<↑/↓> to change the selection, <→> to remove this recipient, <ESC> to quit", choices)
switch act {
case "default":
fallthrough
case "show":
recipients = []string{ids[sel]}
default:
return exitError(ctx, ExitAborted, nil, "user aborted")
}
rs, err := s.recipientsSelectForRemoval(ctx, store)
if err != nil {
return err
}
recipients = rs
}

removed := 0
Expand All @@ -180,3 +162,30 @@ func (s *Action) RecipientsRemove(ctx context.Context, c *cli.Context) error {
out.Cyan(ctx, "You need to run 'gopass sync' to push these changes")
return nil
}

func (s *Action) recipientsSelectForRemoval(ctx context.Context, store string) ([]string, error) {
ids := s.Store.ListRecipients(ctx, store)
choices := make([]string, 0, len(ids))
kl, err := s.gpg.FindPublicKeys(ctx, ids...)
if err == nil && kl != nil {
for _, id := range ids {
if key, err := kl.FindKey(id); err == nil {
choices = append(choices, key.OneLine())
continue
}
choices = append(choices, id)
}
}
if len(choices) < 1 {
return nil, nil
}
act, sel := termwiz.GetSelection(ctx, "Remove recipient -", "<↑/↓> to change the selection, <→> to remove this recipient, <ESC> to quit", choices)
switch act {
case "default":
fallthrough
case "show":
return []string{ids[sel]}, nil
default:
return nil, exitError(ctx, ExitAborted, nil, "user aborted")
}
}
14 changes: 14 additions & 0 deletions backend/git/cli/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ func New(path, gpg string) *Git {
}
}

// Clone clones an existing git repo and returns a new cli based git backend
// configured for this clone repo
func Clone(ctx context.Context, gpg, repo, path string) (*Git, error) {
g := &Git{
gpg: gpg,
path: filepath.Dir(path),
}
if err := g.Cmd(ctx, "Clone", "clone", repo, path); err != nil {
return nil, err
}
g.path = path
return g, nil
}

// Cmd runs an git command
func (g *Git) Cmd(ctx context.Context, name string, args ...string) error {
buf := &bytes.Buffer{}
Expand Down
103 changes: 47 additions & 56 deletions utils/jsonapi/manifest/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,59 @@ package manifest

import (
"fmt"
"path"
"runtime"
)

var globalLocations = map[string]map[string]string{
"darwin": map[string]string{
"firefox": "/Library/Application Support/Mozilla/NativeMessagingHosts/%s.json",
"chrome": "/Library/Google/Chrome/NativeMessagingHosts/%s.json",
"chromium": "/Library/Application Support/Chromium/NativeMessagingHosts/%s.json",
},
"linux": map[string]string{
"firefox": "mozilla/native-messaging-hosts/%s.json",
"chrome": "/etc/opt/chrome/native-messaging-hosts/%s.json",
"chromium": "/etc/chromium/native-messaging-hosts/%s.json",
},
}

var locations = map[string]map[string]string{
"darwin": map[string]string{
"firefox": "~/Library/Application Support/Mozilla/NativeMessagingHosts/%s.json",
"chrome": "~/Library/Application Support/Google/Chrome/NativeMessagingHosts/%s.json",
"chromium": "~/Library/Application Support/Chromium/NativeMessagingHosts/%s.json",
},
"linux": map[string]string{
"firefox": "~/.mozilla/native-messaging-hosts/%s.json",
"chrome": "~/.config/google-chrome/NativeMessagingHosts/%s.json",
"chromium": "~/.config/chromium/NativeMessagingHosts/%s.json",
},
}

func getLocation(browser, libpath string, globalInstall bool) (string, error) {
switch platform := runtime.GOOS; platform {
case "darwin":
{
switch browser {
case "firefox":
{
if globalInstall {
return "/Library/Application Support/Mozilla/NativeMessagingHosts/%s.json", nil
}
return "~/Library/Application Support/Mozilla/NativeMessagingHosts/%s.json", nil
}
case "chrome":
{
if globalInstall {
return "/Library/Google/Chrome/NativeMessagingHosts/%s.json", nil
}
return "~/Library/Application Support/Google/Chrome/NativeMessagingHosts/%s.json", nil
}
case "chromium":
{
if globalInstall {
return "/Library/Application Support/Chromium/NativeMessagingHosts/%s.json", nil
}
return "~/Library/Application Support/Chromium/NativeMessagingHosts/%s.json", nil
}
}
platform := runtime.GOOS
if globalInstall {
pm, found := globalLocations[platform]
if !found {
return "", fmt.Errorf("platform %s is currently not supported", platform)
}
case "linux":
{
switch browser {
case "firefox":
{
if globalInstall {
return path.Join(libpath, "mozilla/native-messaging-hosts/%s.json"), nil
}
return "~/.mozilla/native-messaging-hosts/%s.json", nil
}
case "chrome":
{
if globalInstall {
return "/etc/opt/chrome/native-messaging-hosts/%s.json", nil
}
return "~/.config/google-chrome/NativeMessagingHosts/%s.json", nil
}
case "chromium":
{
if globalInstall {
return "/etc/chromium/native-messaging-hosts/%s.json", nil
}
return "~/.config/chromium/NativeMessagingHosts/%s.json", nil
}
}
path, found := pm[browser]
if !found {
return "", fmt.Errorf("browser %s on %s is currently not supported", browser, platform)
}
default:
{
return "", fmt.Errorf("platform %s is currently not supported", platform)
if browser == "firefox" {
path = libpath + "/" + path
}
return path, nil
}

pm, found := locations[platform]
if !found {
return "", fmt.Errorf("platform %s is currently not supported", platform)
}
path, found := pm[browser]
if !found {
return "", fmt.Errorf("browser %s on %s is currently not supported", browser, platform)
}
return "", nil
return path, nil
}