From d3661d867649a64e68aa1d3ce3788391d6680afb Mon Sep 17 00:00:00 2001 From: Dominik Schulz Date: Tue, 27 Mar 2018 10:55:47 +0200 Subject: [PATCH] Fix git init (#729) Fixes #728 --- commands.go | 18 +++++++++++++----- pkg/action/git.go | 14 +++++++++++--- pkg/action/history_test.go | 2 +- pkg/action/init.go | 39 +++++++++++++++++++++++++++----------- pkg/notify/icon.go | 3 ++- tests/mount_test.go | 6 +++--- tests/tester.go | 2 +- 7 files changed, 59 insertions(+), 25 deletions(-) diff --git a/commands.go b/commands.go index a2b56e7852..01943b2dca 100644 --- a/commands.go +++ b/commands.go @@ -514,6 +514,10 @@ func getCommands(ctx context.Context, action *ap.Action, app *cli.App) []cli.Com Name: "sign-key", Usage: "GPG Key to sign commits", }, + cli.StringFlag{ + Name: "rcs", + Usage: "Select sync backend (git, gitcli, gogit, noop)", + }, }, }, { @@ -641,16 +645,12 @@ func getCommands(ctx context.Context, action *ap.Action, app *cli.App) []cli.Com Name: "store, s", Usage: "Set the name of the sub store", }, - cli.BoolFlag{ - Name: "nogit", - Usage: "Do not init git repo", - }, cli.StringFlag{ Name: "crypto", Usage: "Select crypto backend (gpg, gpgcli, plain, xc)", }, cli.StringFlag{ - Name: "sync", + Name: "rcs", Usage: "Select sync backend (git, gitcli, gogit, noop)", }, }, @@ -865,6 +865,14 @@ func getCommands(ctx context.Context, action *ap.Action, app *cli.App) []cli.Com Name: "email", Usage: "EMail for unattended GPG key generation", }, + cli.StringFlag{ + Name: "crypto", + Usage: "Select crypto backend (gpg, gpgcli, plain, xc)", + }, + cli.StringFlag{ + Name: "rcs", + Usage: "Select sync backend (git, gitcli, gogit, noop)", + }, }, }, { diff --git a/pkg/action/git.go b/pkg/action/git.go index e122f018b1..7f0791da6a 100644 --- a/pkg/action/git.go +++ b/pkg/action/git.go @@ -5,6 +5,7 @@ import ( "os" "github.com/fatih/color" + "github.com/justwatchcom/gopass/pkg/backend" "github.com/justwatchcom/gopass/pkg/cui" "github.com/justwatchcom/gopass/pkg/out" "github.com/justwatchcom/gopass/pkg/termio" @@ -17,15 +18,22 @@ func (s *Action) GitInit(ctx context.Context, c *cli.Context) error { store := c.String("store") un := c.String("username") ue := c.String("useremail") + ctx = backend.WithRCSBackendString(ctx, c.String("rcs")) - if err := s.gitInit(ctx, store, un, ue); err != nil { + // default to git + if !backend.HasRCSBackend(ctx) { + ctx = backend.WithRCSBackend(ctx, backend.GitCLI) + } + + if err := s.rcsInit(ctx, store, un, ue); err != nil { return ExitError(ctx, ExitGit, err, "failed to initialize git: %s", err) } return nil } -func (s *Action) gitInit(ctx context.Context, store, un, ue string) error { - out.Green(ctx, "Initializing git repository ...") +func (s *Action) rcsInit(ctx context.Context, store, un, ue string) error { + bn := backend.RCSBackendName(backend.GetRCSBackend(ctx)) + out.Green(ctx, "Initializing git repository (%s) ...", bn) userName, userEmail := s.getUserData(ctx, store, un, ue) if err := s.Store.GitInit(ctx, store, userName, userEmail); err != nil { diff --git a/pkg/action/history_test.go b/pkg/action/history_test.go index ef75b98990..231f903446 100644 --- a/pkg/action/history_test.go +++ b/pkg/action/history_test.go @@ -42,7 +42,7 @@ func TestHistory(t *testing.T) { app := cli.NewApp() // init git - assert.NoError(t, act.gitInit(ctx, "", "foo bar", "foo.bar@example.org")) + assert.NoError(t, act.rcsInit(ctx, "", "foo bar", "foo.bar@example.org")) buf.Reset() // insert bar diff --git a/pkg/action/init.go b/pkg/action/init.go index 63a986c228..e0be697bad 100644 --- a/pkg/action/init.go +++ b/pkg/action/init.go @@ -42,20 +42,25 @@ func (s *Action) Initialized(ctx context.Context, c *cli.Context) error { func (s *Action) Init(ctx context.Context, c *cli.Context) error { path := c.String("path") alias := c.String("store") - nogit := c.Bool("nogit") ctx = backend.WithCryptoBackendString(ctx, c.String("crypto")) - ctx = backend.WithRCSBackendString(ctx, c.String("sync")) + ctx = backend.WithRCSBackendString(ctx, c.String("rcs")) + + // default to git + if !backend.HasRCSBackend(ctx) { + out.Debug(ctx, "Using default RCS backend (GitCLI)") + ctx = backend.WithRCSBackend(ctx, backend.GitCLI) + } ctx = out.WithPrefix(ctx, "[init] ") out.Cyan(ctx, "Initializing a new password store ...") - if err := s.init(ctx, alias, path, nogit, c.Args()...); err != nil { + if err := s.init(ctx, alias, path, c.Args()...); err != nil { return ExitError(ctx, ExitUnknown, err, "failed to initialized store: %s", err) } return nil } -func (s *Action) init(ctx context.Context, alias, path string, nogit bool, keys ...string) error { +func (s *Action) init(ctx context.Context, alias, path string, keys ...string) error { if path == "" { if alias != "" { path = config.PwStoreDir(alias) @@ -63,7 +68,7 @@ func (s *Action) init(ctx context.Context, alias, path string, nogit bool, keys path = s.Store.Path() } } - out.Debug(ctx, "init(%s, %s, %t, %+v)", alias, path, nogit, keys) + out.Debug(ctx, "action.init(%s, %s, %+v)", alias, path, keys) out.Debug(ctx, "Checking private keys ...") if len(keys) < 1 { @@ -86,12 +91,15 @@ func (s *Action) init(ctx context.Context, alias, path string, nogit bool, keys } } - if !nogit { - out.Debug(ctx, "Initializing RCS ...") - if err := s.gitInit(ctx, alias, "", ""); err != nil { + if backend.HasRCSBackend(ctx) { + bn := backend.RCSBackendName(backend.GetRCSBackend(ctx)) + out.Debug(ctx, "Initializing RCS (%s) ...", bn) + if err := s.rcsInit(ctx, alias, "", ""); err != nil { out.Debug(ctx, "Stacktrace: %+v\n", err) - out.Red(ctx, "Failed to init git: %s", err) + out.Red(ctx, "Failed to init RCS (%s): %s", bn, err) } + } else { + out.Debug(ctx, "not initializing RCS backend ...") } out.Green(ctx, "Password store %s initialized for:", path) @@ -123,6 +131,15 @@ func (s *Action) InitOnboarding(ctx context.Context, c *cli.Context) error { create := c.Bool("create") name := c.String("name") email := c.String("email") + ctx = backend.WithCryptoBackendString(ctx, c.String("crypto")) + + // default to git + if rcs := c.String("rcs"); rcs != "" { + ctx = backend.WithRCSBackendString(ctx, c.String("rcs")) + } else { + out.Debug(ctx, "Using default RCS backend (GitCLI)") + ctx = backend.WithRCSBackend(ctx, backend.GitCLI) + } ctx = out.AddPrefix(ctx, "[init] ") out.Debug(ctx, "Starting Onboarding Wizard - remote: %s - team: %s - create: %t - name: %s - email: %s", remote, team, create, name, email) @@ -275,7 +292,7 @@ func (s *Action) initLocal(ctx context.Context, c *cli.Context) error { } out.Print(ctx, "Initializing your local store ...") - if err := s.init(out.WithHidden(ctx, true), "", path, false); err != nil { + if err := s.init(out.WithHidden(ctx, true), "", path); err != nil { return errors.Wrapf(err, "failed to init local store") } out.Green(ctx, " -> OK") @@ -326,7 +343,7 @@ func (s *Action) initCreateTeam(ctx context.Context, c *cli.Context, team, remot ctx = out.AddPrefix(ctx, "["+team+"] ") out.Print(ctx, "Initializing your shared store ...") - if err := s.init(out.WithHidden(ctx, true), team, "", false); err != nil { + if err := s.init(out.WithHidden(ctx, true), team, ""); err != nil { return errors.Wrapf(err, "failed to init shared store") } out.Green(ctx, " -> OK") diff --git a/pkg/notify/icon.go b/pkg/notify/icon.go index b879a6d66d..0044332e2e 100644 --- a/pkg/notify/icon.go +++ b/pkg/notify/icon.go @@ -7,11 +7,12 @@ import ( "os" "path/filepath" + "github.com/justwatchcom/gopass/pkg/config" "github.com/justwatchcom/gopass/pkg/fsutil" ) func iconURI() string { - iconFN := filepath.Join(os.TempDir(), "gopass-logo-small.png") + iconFN := filepath.Join(config.Directory(), "gopass-logo-small.png") if !fsutil.IsFile(iconFN) { fh, err := os.OpenFile(iconFN, os.O_WRONLY|os.O_CREATE, 0644) if err != nil { diff --git a/tests/mount_test.go b/tests/mount_test.go index 33c2e410e8..436bf29125 100644 --- a/tests/mount_test.go +++ b/tests/mount_test.go @@ -22,7 +22,7 @@ func TestSingleMount(t *testing.T) { assert.NoError(t, err) assert.Equal(t, "moar", out) - out, err = ts.run("init --store mnt/m1 --path " + ts.storeDir("m1") + " --nogit " + keyID) + out, err = ts.run("init --store mnt/m1 --path " + ts.storeDir("m1") + " --rcs=noop " + keyID) t.Logf("Output: %s", out) assert.NoError(t, err) @@ -62,7 +62,7 @@ func TestMultiMount(t *testing.T) { ts.initSecrets("") // mount m1 - out, err := ts.run("init --store mnt/m1 --path " + ts.storeDir("m1") + " --nogit " + keyID) + out, err := ts.run("init --store mnt/m1 --path " + ts.storeDir("m1") + " --rcs=noop " + keyID) t.Logf("Output: %s", out) assert.NoError(t, err) @@ -90,7 +90,7 @@ func TestMultiMount(t *testing.T) { assert.Equal(t, strings.TrimSpace(list), out) // mount m2 - out, err = ts.run("init --store mnt/m2 --path " + ts.storeDir("m2") + " --nogit " + keyID) + out, err = ts.run("init --store mnt/m2 --path " + ts.storeDir("m2") + " --rcs=noop " + keyID) t.Logf("Output: %s", out) assert.NoError(t, err) diff --git a/tests/tester.go b/tests/tester.go index 54a7a4adbf..602a830d67 100644 --- a/tests/tester.go +++ b/tests/tester.go @@ -196,7 +196,7 @@ func (ts tester) runWithInputReader(arg string, input io.Reader) ([]byte, error) } func (ts *tester) initStore() { - out, err := ts.run("init --nogit " + keyID) + out, err := ts.run("init --rcs=noop " + keyID) require.NoError(ts.t, err, "failed to init password store:\n%s", out) }