From a0e16d514bd4f6eaf378345d0da1d06dc0b4ca7b Mon Sep 17 00:00:00 2001 From: Dominik Schulz Date: Fri, 16 Jun 2017 10:15:43 +0200 Subject: [PATCH] Fix git invocation This commit fixes some of the git subcommands that were missing their primary subcommand due to a previous refactoring. Fixes #139 --- action/action.go | 31 ++++++++--------- config/config.go | 2 +- gpg/gpg.go | 14 ++++---- store/root/config.go | 2 ++ store/root/git.go | 2 +- store/root/store.go | 1 + store/sub/git.go | 80 +++++++++----------------------------------- 7 files changed, 43 insertions(+), 89 deletions(-) diff --git a/action/action.go b/action/action.go index 0ac5b5844b..951f3094d3 100644 --- a/action/action.go +++ b/action/action.go @@ -27,8 +27,19 @@ func New(v string) *Action { name = filepath.Base(os.Args[0]) } + debug := false if gdb := os.Getenv("GOPASS_DEBUG"); gdb == "true" { gpg.Debug = true + debug = true + } + noColor := false + // need this override for our integration tests + if nc := os.Getenv("GOPASS_NOCOLOR"); nc == "true" { + noColor = true + } + // only emit color codes when stdout is a terminal + if !terminal.IsTerminal(int(os.Stdout.Fd())) { + noColor = true } // try to read config (if it exists) @@ -36,14 +47,10 @@ func New(v string) *Action { cfg.ImportFunc = askForKeyImport cfg.FsckFunc = askForConfirmation cfg.Version = v + cfg.Debug = debug color.NoColor = cfg.NoColor - // need this override for our integration tests - if nc := os.Getenv("GOPASS_NOCOLOR"); nc == "true" { - color.NoColor = true - } - // only emit color codes when stdout is a terminal - if !terminal.IsTerminal(int(os.Stdout.Fd())) { - color.NoColor = true + if noColor { + color.NoColor = noColor } store, err := root.New(cfg) if err != nil { @@ -59,18 +66,12 @@ func New(v string) *Action { cfg.Path = pwStoreDir("") cfg.ImportFunc = askForKeyImport cfg.FsckFunc = askForConfirmation + cfg.Debug = debug rs, err := root.New(cfg) if err != nil { panic(err) } - - // need this override for our integration tests - if nc := os.Getenv("GOPASS_NOCOLOR"); nc == "true" { - color.NoColor = true - } - if !terminal.IsTerminal(int(os.Stdout.Fd())) { - color.NoColor = true - } + color.NoColor = noColor return &Action{ Name: name, diff --git a/config/config.go b/config/config.go index a03c47ed2c..fbc0fd8f05 100644 --- a/config/config.go +++ b/config/config.go @@ -128,7 +128,7 @@ func Load() (*Config, error) { for _, l := range configLocations() { if cfg, err := load(l); err == nil { if gdb := os.Getenv("GOPASS_DEBUG"); gdb == "true" { - fmt.Printf("Loaded config from %s: %+v\n", l, cfg) + fmt.Printf("[DEBUG] Loaded config from %s: %+v\n", l, cfg) } return cfg, err } diff --git a/gpg/gpg.go b/gpg/gpg.go index 5259ab4460..86c4c362d8 100644 --- a/gpg/gpg.go +++ b/gpg/gpg.go @@ -226,7 +226,7 @@ func listKeys(typ string, search ...string) (KeyList, error) { args = append(args, search...) cmd := exec.Command(GPGBin, args...) if Debug { - fmt.Printf("gpg.listKeys: %s %+v\n", cmd.Path, cmd.Args) + fmt.Printf("[DEBUG] gpg.listKeys: %s %+v\n", cmd.Path, cmd.Args) } out, err := cmd.CombinedOutput() if err != nil { @@ -257,7 +257,7 @@ func GetRecipients(file string) ([]string, error) { args := []string{"--batch", "--list-only", "--list-packets", "--no-default-keyring", "--secret-keyring", "/dev/null", file} cmd := exec.Command(GPGBin, args...) if Debug { - fmt.Printf("gpg.GetRecipients: %s %+v\n", cmd.Path, cmd.Args) + fmt.Printf("[DEBUG] gpg.GetRecipients: %s %+v\n", cmd.Path, cmd.Args) } out, err := cmd.CombinedOutput() if err != nil { @@ -268,7 +268,7 @@ func GetRecipients(file string) ([]string, error) { for scanner.Scan() { line := strings.TrimSpace(scanner.Text()) if Debug { - fmt.Printf("gpg Out: %s\n", line) + fmt.Printf("[DEBUG] gpg Output: %s\n", line) } if !strings.HasPrefix(line, ":pubkey enc packet:") { continue @@ -315,7 +315,7 @@ func Encrypt(path string, content []byte, recipients []string, alwaysTrust bool) cmd := exec.Command(GPGBin, args...) if Debug { - fmt.Printf("gpg.Encrypt: %s %+v\n", cmd.Path, cmd.Args) + fmt.Printf("[DEBUG] gpg.Encrypt: %s %+v\n", cmd.Path, cmd.Args) } cmd.Stdin = bytes.NewReader(content) cmd.Stdout = os.Stdout @@ -329,7 +329,7 @@ func Decrypt(path string) ([]byte, error) { args := append(GPGArgs, "--decrypt", path) cmd := exec.Command(GPGBin, args...) if Debug { - fmt.Printf("gpg.Decrypt: %s %+v\n", cmd.Path, cmd.Args) + fmt.Printf("[DEBUG] gpg.Decrypt: %s %+v\n", cmd.Path, cmd.Args) } return cmd.Output() } @@ -339,7 +339,7 @@ func ExportPublicKey(id, filename string) error { args := append(GPGArgs, "--armor", "--export", id) cmd := exec.Command(GPGBin, args...) if Debug { - fmt.Printf("gpg.ExportPublicKey: %s %+v\n", cmd.Path, cmd.Args) + fmt.Printf("[DEBUG] gpg.ExportPublicKey: %s %+v\n", cmd.Path, cmd.Args) } out, err := cmd.Output() if err != nil { @@ -359,7 +359,7 @@ func ImportPublicKey(filename string) error { args := append(GPGArgs, "--import") cmd := exec.Command(GPGBin, args...) if Debug { - fmt.Printf("gpg.ImportPublicKey: %s %+v\n", cmd.Path, cmd.Args) + fmt.Printf("[DEBUG] gpg.ImportPublicKey: %s %+v\n", cmd.Path, cmd.Args) } cmd.Stdin = bytes.NewReader(buf) cmd.Stdout = os.Stdout diff --git a/store/root/config.go b/store/root/config.go index d39a407712..e31a41b9a0 100644 --- a/store/root/config.go +++ b/store/root/config.go @@ -15,6 +15,7 @@ func (s *Store) Config() *config.Config { AutoPull: s.autoPull, AutoPush: s.autoPush, ClipTimeout: s.clipTimeout, + Debug: s.debug, LoadKeys: s.loadKeys, Mounts: make(map[string]string, len(s.mounts)), NoColor: s.noColor, @@ -41,6 +42,7 @@ func (s *Store) UpdateConfig(cfg *config.Config) error { s.autoImport = cfg.AutoImport s.autoPull = cfg.AutoPull s.autoPush = cfg.AutoPush + s.debug = cfg.Debug s.clipTimeout = cfg.ClipTimeout s.loadKeys = cfg.LoadKeys s.noColor = cfg.NoColor diff --git a/store/root/git.go b/store/root/git.go index 5bb67381c4..f8a1c9d160 100644 --- a/store/root/git.go +++ b/store/root/git.go @@ -9,5 +9,5 @@ func (r *Store) GitInit(name, sk string) error { // Git runs arbitrary git commands on this store and all substores func (r *Store) Git(name string, args ...string) error { store := r.getStore(name) - return store.Git(store.Alias(), args...) + return store.Git(args...) } diff --git a/store/root/store.go b/store/root/store.go index 2ccdb28421..9f2e60fe3f 100644 --- a/store/root/store.go +++ b/store/root/store.go @@ -63,6 +63,7 @@ func New(cfg *config.Config) (*Store, error) { safeContent: cfg.SafeContent, } + // TODO(dschulz) this should be passed down from main, not set here if d := os.Getenv("GOPASS_DEBUG"); d == "true" { r.debug = true } diff --git a/store/sub/git.go b/store/sub/git.go index 3a46814af0..490c524bb3 100644 --- a/store/sub/git.go +++ b/store/sub/git.go @@ -14,14 +14,14 @@ import ( "github.com/justwatchcom/gopass/store" ) -func (s *Store) gitCmd(args ...string) error { - cmd := exec.Command(args[0], args[1:]...) +func (s *Store) gitCmd(name string, args ...string) error { + cmd := exec.Command("git", args[0:]...) cmd.Dir = s.path cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr if s.debug { - fmt.Printf("store.GitInit: %s %+v\n", cmd.Path, cmd.Args) + fmt.Printf("[DEBUG] store.%s: %s %+v\n", name, cmd.Path, cmd.Args) } return cmd.Run() } @@ -32,7 +32,7 @@ func (s *Store) GitInit(alias, signKey string) error { // the git repo may be empty (i.e. no branches, cloned from a fresh remote) // or already initialized. Only run git init if the folder is completely empty if !s.isGit() { - if err := s.gitCmd("git", "init"); err != nil { + if err := s.gitCmd("GitInit", "init"); err != nil { return fmt.Errorf("Failed to initialize git: %s", err) } } @@ -55,10 +55,10 @@ func (s *Store) GitInit(alias, signKey string) error { } // setup for proper diffs - if err := s.gitCmd("git", "config", "--local", "diff.gpg.binary", "true"); err != nil { + if err := s.gitCmd("GitInit", "config", "--local", "diff.gpg.binary", "true"); err != nil { color.Yellow("Error while initializing git: %s\n", err) } - if err := s.gitCmd("git", "config", "--local", "diff.gpg.textconv", "gpg --no-tty --decrypt"); err != nil { + if err := s.gitCmd("GitInit", "config", "--local", "diff.gpg.textconv", "gpg --no-tty --decrypt"); err != nil { color.Yellow("Error while initializing git: %s\n", err) } @@ -75,49 +75,22 @@ func (s *Store) gitSetSignKey(sk string) error { return fmt.Errorf("SignKey not set") } - cmd := exec.Command("git", "config", "--local", "user.signingkey", sk) - cmd.Dir = s.path - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - - if s.debug { - fmt.Printf("store.gitSetSignKey: %s %+v\n", cmd.Path, cmd.Args) - } - if err := cmd.Run(); err != nil { + if err := s.gitCmd("gitSetSignKey", "config", "--local", "user.signingkey", sk); err != nil { return err } - cmd = exec.Command("git", "config", "--local", "commit.gpgsign", "true") - cmd.Dir = s.path - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - - if s.debug { - fmt.Printf("store.gitSetSignKey: %s %+v\n", cmd.Path, cmd.Args) - } - return cmd.Run() + return s.gitCmd("gitSetSignKey", "config", "--local", "commit.gpgsign", "true") } -// Git runs arbitrary git commands on this store and all substores -func (s *Store) Git(alias string, args ...string) error { - cmd := exec.Command("git", args...) - cmd.Dir = s.path - cmd.Stdout = os.Stdout - cmd.Stdin = os.Stdin - cmd.Stderr = os.Stderr - - if s.debug { - fmt.Printf("store.Git: %s %+v\n", cmd.Path, cmd.Args) - } - if err := cmd.Run(); err != nil { - return err - } - - return nil +// Git runs arbitrary git commands on this store +func (s *Store) Git(args ...string) error { + return s.gitCmd("Git", args...) } // isGit returns true if this stores has a .git folder func (s *Store) isGit() bool { + // TODO(dschulz) we may want to check if the folder actually contains + // an initialized git setup return fsutil.IsDir(filepath.Join(s.path, ".git")) } @@ -132,19 +105,8 @@ func (s *Store) gitAdd(files ...string) error { args := []string{"add", "--all"} args = append(args, files...) - cmd := exec.Command("git", args...) - cmd.Dir = s.path - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - - if s.debug { - fmt.Printf("store.gitAdd: %s %+v\n", cmd.Path, cmd.Args) - } - if err := cmd.Run(); err != nil { - return fmt.Errorf("failed to add files to git: %v", err) - } - return nil + return s.gitCmd("gitAdd", args...) } // gitCommit creates a new git commit with the given commit message @@ -153,19 +115,7 @@ func (s *Store) gitCommit(msg string) error { return store.ErrGitNotInit } - cmd := exec.Command("git", "commit", "-m", msg) - cmd.Dir = s.path - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - - if s.debug { - fmt.Printf("store.gitCommit: %s %+v\n", cmd.Path, cmd.Args) - } - if err := cmd.Run(); err != nil { - return fmt.Errorf("failed to commit files to git: %v", err) - } - - return nil + return s.gitCmd("gitCommit", "commit", "-m", msg) } func (s *Store) gitConfigValue(key string) (string, error) {