Skip to content

Commit

Permalink
Fix git init (gopasspw#729)
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikschulz authored Mar 27, 2018
1 parent c1083d1 commit d3661d8
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 25 deletions.
18 changes: 13 additions & 5 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
},
},
},
{
Expand Down Expand Up @@ -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)",
},
},
Expand Down Expand Up @@ -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)",
},
},
},
{
Expand Down
14 changes: 11 additions & 3 deletions pkg/action/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/action/history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 28 additions & 11 deletions pkg/action/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,33 @@ 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)
} else {
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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
3 changes: 2 additions & 1 deletion pkg/notify/icon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions tests/mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion tests/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit d3661d8

Please sign in to comment.