Skip to content

Commit

Permalink
Expand tilde in store path
Browse files Browse the repository at this point in the history
Fixes #801
  • Loading branch information
Dominik Schulz authored and dominikschulz committed May 28, 2018
1 parent d7d56af commit fa3f8d8
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 3 deletions.
2 changes: 2 additions & 0 deletions pkg/backend/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package backend

import (
"context"
"fmt"

"github.com/blang/semver"
)
Expand All @@ -24,6 +25,7 @@ func (s StorageBackend) String() string {

// Storage is an storage backend
type Storage interface {
fmt.Stringer
Get(ctx context.Context, name string) ([]byte, error)
Set(ctx context.Context, name string, value []byte) error
Delete(ctx context.Context, name string) error
Expand Down
8 changes: 7 additions & 1 deletion pkg/backend/storage/fs/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fs

import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
Expand All @@ -22,7 +23,7 @@ type Store struct {
// New creates a new store
func New(dir string) *Store {
return &Store{
path: dir,
path: filepath.Clean(dir),
}
}

Expand Down Expand Up @@ -114,6 +115,11 @@ func (s *Store) Version() semver.Version {
return semver.Version{Minor: 1}
}

// String implements fmt.Stringer
func (s *Store) String() string {
return fmt.Sprintf("fs(v0.1.0,path:%s)", s.path)
}

// Available will check if this backend is useable
func (s *Store) Available(ctx context.Context) error {
return s.Set(ctx, ".test", []byte("test"))
Expand Down
6 changes: 6 additions & 0 deletions pkg/backend/storage/kv/consul/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package consul

import (
"context"
"fmt"
"strings"

"github.com/justwatchcom/gopass/pkg/out"
Expand Down Expand Up @@ -131,6 +132,11 @@ func (s *Store) Version() semver.Version {
return semver.Version{Major: 1}
}

// String implements fmt.Stringer
func (s *Store) String() string {
return fmt.Sprintf("consul(1.0.0,%s)", s.prefix)
}

// Available will check if this backend is useable
func (s *Store) Available(ctx context.Context) error {
return s.Set(ctx, ".test", []byte("test"))
Expand Down
5 changes: 5 additions & 0 deletions pkg/backend/storage/kv/inmem/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ func (m *InMem) Version() semver.Version {
return semver.Version{Major: 1}
}

// String implement fmt.Stringer
func (m *InMem) String() string {
return "inmem()"
}

// Available will check if this backend is useable
func (m *InMem) Available(ctx context.Context) error {
if m.data == nil {
Expand Down
11 changes: 11 additions & 0 deletions pkg/backend/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import (
"fmt"
"net"
"net/url"
"path/filepath"
"strings"

homedir "github.com/mitchellh/go-homedir"
)

// URL is a parsed backend URL
Expand Down Expand Up @@ -56,6 +59,14 @@ func ParseURL(us string) (*URL, error) {
u.Username = nu.User.Username()
u.Password, _ = nu.User.Password()
}
if nu.Host == "~" {
hd, err := homedir.Dir()
if err != nil {
return u, err
}
u.Path = filepath.Join(hd, u.Path)
nu.Host = ""
}
u.Query = nu.Query()
if nu.Host != "" {
h, p, err := net.SplitHostPort(nu.Host)
Expand Down
17 changes: 17 additions & 0 deletions pkg/backend/url_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package backend

import (
"path/filepath"
"testing"

homedir "github.com/mitchellh/go-homedir"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
yaml "gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -68,12 +71,15 @@ func TestURLString(t *testing.T) {
}

func TestParseScheme(t *testing.T) {
hd, err := homedir.Dir()
require.NoError(t, err)
for _, tc := range []struct {
Name string
URL string
Crypto CryptoBackend
RCS RCSBackend
Storage StorageBackend
Path string
}{
{
Name: "legacy file path",
Expand All @@ -96,6 +102,14 @@ func TestParseScheme(t *testing.T) {
RCS: Noop,
Storage: Consul,
},
{
Name: "Homedir expansion",
URL: "gpgcli-gitcli-fs+file://~/.local/share/password-store",
Crypto: GPGCLI,
RCS: GitCLI,
Storage: FS,
Path: filepath.Join(hd, ".local", "share", "password-store"),
},
//{
// URL: "plain+vault-http://localhost:9600/foo/bar",
// Crypto: Plain,
Expand All @@ -109,6 +123,9 @@ func TestParseScheme(t *testing.T) {
assert.Equal(t, tc.Crypto, u.Crypto, tc.Name)
assert.Equal(t, tc.RCS, u.RCS, tc.Name)
assert.Equal(t, tc.Storage, u.Storage, tc.Name)
if tc.Path != "" {
assert.Equal(t, tc.Path, u.Path, tc.Name)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/store/sub/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
func (s *Store) initStorageBackend(ctx context.Context) error {
switch s.url.Storage {
case backend.FS:
out.Debug(ctx, "Using Storage Backend: fs")
s.storage = fs.New(s.url.Path)
out.Debug(ctx, "Using Storage Backend: %s", s.storage.String())
case backend.InMem:
out.Debug(ctx, "Using Storage Backend: inmem")
s.storage = inmem.New()
Expand Down
2 changes: 1 addition & 1 deletion pkg/store/sub/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func New(ctx context.Context, alias string, u *backend.URL, cfgdir string, agent
return nil, errors.Wrapf(err, "failed to init crypto backend: %s", err)
}

out.Debug(ctx, "sub.New - initialized - storage: %p - rcs: %p - crypto: %p", s.storage, s.rcs, s.crypto)
out.Debug(ctx, "sub.New - initialized - storage: %s (%p) - rcs: %s (%p) - crypto: %s (%p)", s.storage.Name(), s.storage, s.rcs.Name(), s.rcs, s.crypto.Name(), s.crypto)
return s, nil
}

Expand Down

0 comments on commit fa3f8d8

Please sign in to comment.