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

Add CreateAll func to dir service to create dirs recursively #146

Merged
merged 4 commits into from
Nov 14, 2019
Merged
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
42 changes: 42 additions & 0 deletions pkg/secrethub/dir.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package secrethub

import (
"strings"

"github.com/secrethub/secrethub-go/internals/api"
"github.com/secrethub/secrethub-go/internals/api/uuid"
"github.com/secrethub/secrethub-go/internals/errio"
"github.com/secrethub/secrethub-go/pkg/secretpath"
)

// DirService handles operations on directories from SecretHub.
type DirService interface {
// Create a directory at a given path.
Create(path string) (*api.Dir, error)
// CreateAll creates all directories in the given path that do not exist yet.
SimonBarendse marked this conversation as resolved.
Show resolved Hide resolved
//
// Contrary to Create, it doesn't return an error when the directories already exist.
CreateAll(path string) 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 @@ -169,6 +176,41 @@ func (s dirService) Delete(path string) error {
return nil
}

// CreateAll creates all directories in the given path that do not exist yet.
//
// Contrary to Create, it doesn't return an error when the directories already exist.
func (s dirService) CreateAll(path string) error {
err := api.ValidateDirPath(path)
if err != nil {
return err
}
return s.createAll(path)
}

func (s dirService) createAll(path string) error {
if len(strings.Split(path, "/")) < 3 {
return nil
}

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

err = s.createAll(secretpath.Parent(path))
if err != nil {
return err
SimonBarendse marked this conversation as resolved.
Show resolved Hide resolved
}

_, err = s.Create(path)
if err == api.ErrDirAlreadyExists {
return nil
}
// err might be nil
return err
}

// listDirAccounts list the accounts with read permission.
func (c *Client) listDirAccounts(path api.BlindNamePath) ([]*api.Account, error) {
blindName, err := c.convertPathToBlindName(path)
Expand Down