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

Change Path to URL #698

Merged
merged 1 commit into from
Mar 8, 2018
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ codequality:
$(GO) get -u github.com/fzipp/gocyclo; \
fi
@$(foreach gofile, $(GOFILES_NOVENDOR),\
gocyclo -over 20 $(gofile) || exit 1;)
gocyclo -over 22 $(gofile) || exit 1;)
@$(call ok)

@echo -n " LINT "
Expand Down
4 changes: 2 additions & 2 deletions action/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

func newMock(ctx context.Context, u *gptest.Unit) (*Action, error) {
cfg := config.New()
cfg.Root.Path = u.StoreDir("")
cfg.Root.Path = backend.FromPath(u.StoreDir(""))

ctx = backend.WithSyncBackendString(ctx, "gitmock")
ctx = backend.WithCryptoBackendString(ctx, "gpgmock")
Expand Down Expand Up @@ -50,7 +50,7 @@ func TestNew(t *testing.T) {
_, err = New(ctx, cfg, sv)
assert.Error(t, err)

cfg.Root.Path = filepath.Join(td, "store")
cfg.Root.Path = backend.FromPath(filepath.Join(td, "store"))
_, err = New(ctx, cfg, sv)
assert.NoError(t, err)
}
12 changes: 6 additions & 6 deletions action/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ func (s *Action) clone(ctx context.Context, repo, mount, path string) error {
return exitError(ctx, ExitMount, err, "Failed to add mount: %s", err)
}
out.Green(ctx, "Mounted password store %s at mount point `%s` ...", path, mount)
s.cfg.Mounts[mount].CryptoBackend = backend.CryptoBackendName(backend.GetCryptoBackend(ctx))
s.cfg.Mounts[mount].SyncBackend = backend.SyncBackendName(backend.GetSyncBackend(ctx))
s.cfg.Mounts[mount].StoreBackend = backend.StoreBackendName(backend.GetStoreBackend(ctx))
s.cfg.Mounts[mount].Path.Crypto = backend.GetCryptoBackend(ctx)
s.cfg.Mounts[mount].Path.Sync = backend.GetSyncBackend(ctx)
s.cfg.Mounts[mount].Path.Store = backend.GetStoreBackend(ctx)
} else {
s.cfg.Root.CryptoBackend = backend.CryptoBackendName(backend.GetCryptoBackend(ctx))
s.cfg.Root.SyncBackend = backend.SyncBackendName(backend.GetSyncBackend(ctx))
s.cfg.Root.StoreBackend = backend.StoreBackendName(backend.GetStoreBackend(ctx))
s.cfg.Root.Path.Crypto = backend.GetCryptoBackend(ctx)
s.cfg.Root.Path.Sync = backend.GetSyncBackend(ctx)
s.cfg.Root.Path.Store = backend.GetStoreBackend(ctx)
}

// save new mount in config file
Expand Down
25 changes: 9 additions & 16 deletions action/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"testing"

"github.com/justwatchcom/gopass/backend"
"github.com/justwatchcom/gopass/config"
"github.com/justwatchcom/gopass/tests/gptest"
"github.com/justwatchcom/gopass/utils/out"
Expand Down Expand Up @@ -41,37 +42,34 @@ func TestConfig(t *testing.T) {
autoimport: true
autosync: true
cliptimeout: 45
cryptobackend: gpg
nocolor: false
noconfirm: false
nopager: false
notifications: true
`
want += " path: " + u.StoreDir("") + "\n"
want += " path: " + backend.FromPath(u.StoreDir("")).String() + "\n"
want += ` safecontent: false
storebackend: fs
syncbackend: git
usesymbols: false
`
assert.Equal(t, want, buf.String())
buf.Reset()

// action.setConfigValue
assert.NoError(t, act.setConfigValue(ctx, "", "nopager", "true"))
assert.Equal(t, "nopager: true", strings.TrimSpace(buf.String()))
assert.Equal(t, "nopager: true", strings.TrimSpace(buf.String()), "action.setConfigValue")
buf.Reset()

// action.printConfigValues
act.cfg.Mounts["foo"] = &config.StoreConfig{}
act.printConfigValues(ctx, "", "nopager")
want = `nopager: true
foo/nopager: false`
assert.Equal(t, want, strings.TrimSpace(buf.String()))
assert.Equal(t, want, strings.TrimSpace(buf.String()), "action.printConfigValues")
buf.Reset()

// action.setConfigValue on substore
assert.NoError(t, act.setConfigValue(ctx, "foo", "cliptimeout", "23"))
assert.Equal(t, "foo/cliptimeout: 23", strings.TrimSpace(buf.String()))
assert.Equal(t, "foo/cliptimeout: 23", strings.TrimSpace(buf.String()), "action.setConfigValue on substore")
buf.Reset()

// action.printConfigValues
Expand All @@ -81,25 +79,23 @@ foo/nopager: false`
autoimport: true
autosync: true
cliptimeout: 45
cryptobackend: gpg
nocolor: false
noconfirm: false
nopager: true
notifications: true
`
want += " path: " + u.StoreDir("") + "\n"
want += " path: " + backend.FromPath(u.StoreDir("")).String() + "\n"
want += ` safecontent: false
storebackend: fs
syncbackend: git
usesymbols: false
mount 'foo' config:
autoimport: false
autosync: false
cliptimeout: 23
nopager: false
notifications: false
path:`
assert.Equal(t, want, strings.TrimSpace(buf.String()))
`
want += " path: " + backend.FromPath("").String()
assert.Equal(t, want, strings.TrimSpace(buf.String()), "action.setConfigValues")
buf.Reset()

delete(act.cfg.Mounts, "foo")
Expand Down Expand Up @@ -127,15 +123,12 @@ mount 'foo' config:
autoimport
autosync
cliptimeout
cryptobackend
nocolor
noconfirm
nopager
notifications
path
safecontent
storebackend
syncbackend
usesymbols
`
assert.Equal(t, want, buf.String())
Expand Down
2 changes: 1 addition & 1 deletion action/history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestHistory(t *testing.T) {
ctx = backend.WithCryptoBackend(ctx, backend.GPGMock)

cfg := config.New()
cfg.Root.Path = u.StoreDir("")
cfg.Root.Path = backend.FromPath(u.StoreDir(""))
act, err := newAction(ctx, cfg, semver.Version{})
assert.NoError(t, err)

Expand Down
78 changes: 16 additions & 62 deletions backend/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,15 @@ const (

// CryptoBackendName returns the name of the given backend
func CryptoBackendName(cb CryptoBackend) string {
switch cb {
case GPGMock:
return "gpgmock"
case GPGCLI:
return "gpgcli"
case XC:
return "xc"
case OpenPGP:
return "openpgp"
default:
return ""
}
return cryptoNameFromBackend(cb)
}

// WithCryptoBackendString returns a context with the given crypto backend set
func WithCryptoBackendString(ctx context.Context, be string) context.Context {
switch be {
case "gpg":
fallthrough
case "gpgcli":
return WithCryptoBackend(ctx, GPGCLI)
case "gpgmock":
return WithCryptoBackend(ctx, GPGMock)
case "xc":
return WithCryptoBackend(ctx, XC)
case "openpgp":
return WithCryptoBackend(ctx, OpenPGP)
default:
return ctx
if cb := cryptoBackendFromName(be); cb >= 0 {
ctx = WithCryptoBackend(ctx, cb)
}
return ctx
}

// WithCryptoBackend returns a context with the given crypto backend set
Expand All @@ -66,32 +45,15 @@ func GetCryptoBackend(ctx context.Context) CryptoBackend {

// SyncBackendName returns the name of the given backend
func SyncBackendName(sb SyncBackend) string {
switch sb {
case GitMock:
return "gitmock"
case GitCLI:
return "gitcli"
case GoGit:
return "gogit"
default:
return ""
}
return syncNameFromBackend(sb)
}

// WithSyncBackendString returns a context with the given sync backend set
func WithSyncBackendString(ctx context.Context, sb string) context.Context {
switch sb {
case "git":
fallthrough
case "gitcli":
return WithSyncBackend(ctx, GitCLI)
case "gogit":
return WithSyncBackend(ctx, GoGit)
case "gitmock":
return WithSyncBackend(ctx, GitMock)
default:
return WithSyncBackend(ctx, GitMock)
if be := syncBackendFromName(sb); be >= 0 {
return WithSyncBackend(ctx, be)
}
return WithSyncBackend(ctx, GitMock)
}

// WithSyncBackend returns a context with the given sync backend set
Expand All @@ -116,14 +78,7 @@ func GetSyncBackend(ctx context.Context) SyncBackend {

// WithStoreBackendString returns a context with the given store backend set
func WithStoreBackendString(ctx context.Context, sb string) context.Context {
switch sb {
case "kvmock":
return WithStoreBackend(ctx, KVMock)
case "fs":
return WithStoreBackend(ctx, FS)
default:
return WithStoreBackend(ctx, FS)
}
return WithStoreBackend(ctx, storeBackendFromName(sb))
}

// WithStoreBackend returns a context with the given store backend set
Expand All @@ -140,14 +95,13 @@ func GetStoreBackend(ctx context.Context) StoreBackend {
return be
}

// HasStoreBackend returns true if a value for store backend was set
func HasStoreBackend(ctx context.Context) bool {
_, ok := ctx.Value(ctxKeyStoreBackend).(StoreBackend)
return ok
}

// StoreBackendName returns the name of the given backend
func StoreBackendName(sb StoreBackend) string {
switch sb {
case FS:
return "fs"
case KVMock:
return "kvmock"
default:
return ""
}
return storeNameFromBackend(sb)
}
4 changes: 4 additions & 0 deletions backend/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ const (
OpenPGP
)

func (c CryptoBackend) String() string {
return cryptoNameFromBackend(c)
}

// Keyring is a public/private key manager
type Keyring interface {
ImportPublicKey(ctx context.Context, key []byte) error
Expand Down
4 changes: 2 additions & 2 deletions backend/crypto/gpg/cli/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ func Binary(ctx context.Context, bin string) (string, error) {
}
bv := make(byVersion, 0, len(bins))
for _, b := range bins {
out.Debug(ctx, "gpg.detectBinary - Looking for '%s' ...", b)
// TODO out.Debug(ctx, "gpg.detectBinary - Looking for '%s' ...", b)
if p, err := exec.LookPath(b); err == nil {
gb := gpgBin{
path: p,
ver: version(ctx, p),
}
out.Debug(ctx, "gpg.detectBinary - Found '%s' at '%s' (%s)", b, p, gb.ver.String())
// TODO out.Debug(ctx, "gpg.detectBinary - Found '%s' at '%s' (%s)", b, p, gb.ver.String())
bv = append(bv, gb)
}
}
Expand Down
10 changes: 7 additions & 3 deletions backend/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ import (
type StoreBackend int

const (
// KVMock is an in-memory mock store for tests
KVMock StoreBackend = iota
// FS is a filesystem-backend storage
FS
FS StoreBackend = iota
// KVMock is an in-memory mock store for tests
KVMock
)

func (s StoreBackend) String() string {
return storeNameFromBackend(s)
}

// Store is an storage backend
type Store interface {
Get(ctx context.Context, name string) ([]byte, error)
Expand Down
76 changes: 76 additions & 0 deletions backend/strings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package backend

var (
cryptoNameToBackendMap = map[string]CryptoBackend{
"gpgmock": GPGMock,
"gpgcli": GPGCLI,
"xc": XC,
"openpgp": OpenPGP,
}
cryptoBackendToNameMap = map[CryptoBackend]string{}
syncNameToBackendMap = map[string]SyncBackend{
"gitcli": GitCLI,
"gitmock": GitMock,
"gogit": GoGit,
}
syncBackendToNameMap = map[SyncBackend]string{}
storeNameToBackendMap = map[string]StoreBackend{
"kvmock": KVMock,
"fs": FS,
}
storeBackendToNameMap = map[StoreBackend]string{}
)

func init() {
for k, v := range cryptoNameToBackendMap {
cryptoBackendToNameMap[v] = k
}
for k, v := range syncNameToBackendMap {
syncBackendToNameMap[v] = k
}
for k, v := range storeNameToBackendMap {
storeBackendToNameMap[v] = k
}
}

func cryptoBackendFromName(name string) CryptoBackend {
if b, found := cryptoNameToBackendMap[name]; found {
return b
}
return -1
}

func cryptoNameFromBackend(be CryptoBackend) string {
if b, found := cryptoBackendToNameMap[be]; found {
return b
}
return ""
}

func syncBackendFromName(name string) SyncBackend {
if b, found := syncNameToBackendMap[name]; found {
return b
}
return -1
}

func syncNameFromBackend(be SyncBackend) string {
if b, found := syncBackendToNameMap[be]; found {
return b
}
return ""
}

func storeBackendFromName(name string) StoreBackend {
if b, found := storeNameToBackendMap[name]; found {
return b
}
return FS
}

func storeNameFromBackend(be StoreBackend) string {
if b, found := storeBackendToNameMap[be]; found {
return b
}
return ""
}
Loading