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

support / inside of vault names #197

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
34 changes: 31 additions & 3 deletions lib/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,14 @@ func (s *store) Steward() Steward {
return s.steward
}

func (s *store) ListVaults() ([]string, error) {
vaults, err := xdg.DATA.Glob(filepath.Join("vaulted", "*"))
func (s *store) listRecursive(extraParts string) ([]string, error) {
var vaults []string
var err error
if extraParts == "" {
vaults, err = xdg.DATA.Glob(filepath.Join("vaulted", "*"))
} else {
vaults, err = xdg.DATA.Glob(filepath.Join("vaulted", extraParts, "*"))
}
if err != nil {
return nil, err
}
Expand All @@ -63,19 +69,41 @@ func (s *store) ListVaults() ([]string, error) {
if err != nil {
return nil, err
}
if info.Mode().IsDir() {
var innerVaults []string
if extraParts == "" {
innerVaults, err = s.listRecursive(info.Name())
} else {
innerVaults, err = s.listRecursive(filepath.Join(extraParts, info.Name()))
}

if err != nil {
return nil, err
}
found = append(found, innerVaults...)
continue
}
if !info.Mode().IsRegular() {
continue
}

if !emitted[info.Name()] {
emitted[info.Name()] = true
found = append(found, info.Name())
if extraParts == "" {
found = append(found, info.Name())
} else {
found = append(found, extraParts+"/"+info.Name())
}
}
}

return found, nil
}

func (s *store) ListVaults() ([]string, error) {
return s.listRecursive("")
}

func (s *store) VaultExists(name string) bool {
existing := xdg.DATA.Find(filepath.Join("vaulted", name))
return len(existing) != 0
Expand Down
11 changes: 10 additions & 1 deletion lib/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestListVaults(t *testing.T) {
}

sort.Strings(vaults)
expected := []string{"aaa", "bbb", "ccc"}
expected := []string{"aaa", "bbb", "ccc", "subfolder/aaa"}
if !reflect.DeepEqual(expected, vaults) {
t.Fatalf("expected %#v, got %#v", expected, vaults)
}
Expand Down Expand Up @@ -128,6 +128,15 @@ func setupVaults(t *testing.T) {
t.Fatalf("failed to create vaulted DATA_HOME dir: %v", err)
}

err = os.Mkdir(filepath.Join(string(xdg.DATA_HOME), "vaulted", "subfolder"), 0700)
if err != nil {
t.Fatalf("failed to create vaulted DATA_HOME subfolder dir: %v", err)
}
err = ioutil.WriteFile(filepath.Join(string(xdg.DATA_HOME), "vaulted", "subfolder", "aaa"), []byte(VAULT_AAA), 0600)
if err != nil {
t.Fatalf("failed to write 'subfolder/aaa' home vault file: %v", err)
}

err = ioutil.WriteFile(filepath.Join(string(xdg.DATA_HOME), "vaulted", "aaa"), []byte(VAULT_AAA), 0600)
if err != nil {
t.Fatalf("failed to write 'aaa' home vault file: %v", err)
Expand Down
6 changes: 2 additions & 4 deletions lib/vault_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,11 @@ func readVaultFile(name string) (*VaultFile, error) {
}

func writeVaultFile(name string, vaultFile *VaultFile) error {
pathname := xdg.DATA_HOME.Join("vaulted")
err := os.MkdirAll(pathname, 0700)
filename := xdg.DATA_HOME.Join(filepath.Join("vaulted", name))
err := os.MkdirAll(filepath.Dir(filename), 0700)
if err != nil {
return err
}

filename := xdg.DATA_HOME.Join(filepath.Join("vaulted", name))
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
Expand Down