From d304a89b85d8218ec31fd33e98787f6c0a89afff Mon Sep 17 00:00:00 2001 From: Simon Barendse Date: Mon, 25 Nov 2019 11:55:09 +0100 Subject: [PATCH 1/3] Add Exists func to dir service --- pkg/secrethub/dir.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pkg/secrethub/dir.go b/pkg/secrethub/dir.go index c529ece3..394d8fcf 100644 --- a/pkg/secrethub/dir.go +++ b/pkg/secrethub/dir.go @@ -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. @@ -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 { + 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) From c63235947cf0d4332d6ac56d7a80f162233db1d0 Mon Sep 17 00:00:00 2001 From: Simon Barendse Date: Mon, 25 Nov 2019 11:56:49 +0100 Subject: [PATCH 2/3] Use Exists in CreateAll --- pkg/secrethub/dir.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pkg/secrethub/dir.go b/pkg/secrethub/dir.go index 394d8fcf..4f556086 100644 --- a/pkg/secrethub/dir.go +++ b/pkg/secrethub/dir.go @@ -205,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 { From 332f4d7e3787bd9fe9fc799073a1ef47b1687379 Mon Sep 17 00:00:00 2001 From: Simon Barendse Date: Mon, 25 Nov 2019 15:00:29 +0100 Subject: [PATCH 3/3] Don't return repo not found error from dirService.Exists --- pkg/secrethub/dir.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/secrethub/dir.go b/pkg/secrethub/dir.go index 4f556086..74dd4f25 100644 --- a/pkg/secrethub/dir.go +++ b/pkg/secrethub/dir.go @@ -161,7 +161,7 @@ func (s dirService) Create(path string) (*api.Dir, error) { // 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 { + if err == api.ErrDirNotFound || err == api.ErrRepoNotFound { return false, nil } else if err != nil { return false, err