Skip to content
This repository has been archived by the owner on Feb 16, 2023. It is now read-only.

Commit

Permalink
Merge pull request #149 from secrethub/feature/dir-exists
Browse files Browse the repository at this point in the history
 Add Exists func to dir service
  • Loading branch information
SimonBarendse authored Nov 26, 2019
2 parents 1fe163a + 332f4d7 commit e2e462c
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions pkg/secrethub/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ type DirService interface {
//
// Contrary to Create, it doesn't return an error when the directories already exist.
CreateAll(path string) error
// Exists returns whether a directory where you have access to exists at a given path.
Exists(path string) (bool, error)
// Get returns the directory with the given ID.
GetByID(id uuid.UUID) (*api.Dir, error)
// Delete removes the directory at the given path.
Expand Down Expand Up @@ -156,6 +158,17 @@ func (s dirService) Create(path string) (*api.Dir, error) {
return dir, errio.Error(err)
}

// Exists returns whether a directory where you have access to exists at a given path.
func (s dirService) Exists(path string) (bool, error) {
_, err := s.GetTree(path, 0, false)
if err == api.ErrDirNotFound || err == api.ErrRepoNotFound {
return false, nil
} else if err != nil {
return false, err
}
return true, nil
}

// Delete removes the directory at the given path.
func (s dirService) Delete(path string) error {
p, err := api.NewDirPath(path)
Expand Down Expand Up @@ -192,11 +205,13 @@ func (s dirService) createAll(path string) error {
return nil
}

_, err := s.GetTree(path, 0, false)
if err != api.ErrDirNotFound {
// err might be nil
exists, err := s.Exists(path)
if err != nil {
return err
}
if exists {
return nil
}

err = s.createAll(secretpath.Parent(path))
if err != nil {
Expand Down

0 comments on commit e2e462c

Please sign in to comment.