diff --git a/lib/store.go b/lib/store.go index 38e0a01..1b6874e 100644 --- a/lib/store.go +++ b/lib/store.go @@ -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 } @@ -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 diff --git a/lib/store_test.go b/lib/store_test.go index e128332..721430d 100644 --- a/lib/store_test.go +++ b/lib/store_test.go @@ -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) } @@ -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) diff --git a/lib/vault_file.go b/lib/vault_file.go index b6c7bad..44e4a60 100644 --- a/lib/vault_file.go +++ b/lib/vault_file.go @@ -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