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

Commit 755ac71

Browse files
Merge pull request #321 from secrethub/feature/idempotent-demo-init
Make `secrethub demo init` do nothing on subsequent runs
2 parents 5ccc9e9 + d5e6cb7 commit 755ac71

1 file changed

Lines changed: 40 additions & 2 deletions

File tree

internals/demo/init.go

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,15 @@ func (cmd *InitCommand) Run() error {
6464
}
6565

6666
_, err = client.Repos().Create(repoPath)
67-
if err == api.ErrRepoAlreadyExists && cmd.repo == "" {
68-
return fmt.Errorf("demo repo %s already exists, use --repo to specify another repo to use", repoPath)
67+
if err == api.ErrRepoAlreadyExists {
68+
demoRepo, err := cmd.isDemoRepo(client, repoPath)
69+
if err != nil {
70+
return err
71+
}
72+
if demoRepo {
73+
return nil
74+
}
75+
return fmt.Errorf("repo %s already exists and is not a demo repo, use --repo to specify another repo to use", repoPath)
6976
} else if err != nil {
7077
return err
7178
}
@@ -89,3 +96,34 @@ func (cmd *InitCommand) Run() error {
8996

9097
return nil
9198
}
99+
100+
// isDemoRepo checks whether the repo on the given path is a demo repository.
101+
// It returns true iff the repository contains exactly two secrets named username and password.
102+
func (cmd *InitCommand) isDemoRepo(client secrethub.ClientInterface, repoPath string) (bool, error) {
103+
repo, err := client.Repos().Get(repoPath)
104+
if err != nil {
105+
return false, err
106+
}
107+
if repo.SecretCount != 2 {
108+
return false, nil
109+
}
110+
111+
usernamePath := secretpath.Join(repoPath, "username")
112+
exists, err := client.Secrets().Exists(usernamePath)
113+
if err != nil {
114+
return false, err
115+
}
116+
if !exists {
117+
return false, nil
118+
}
119+
120+
passwordPath := secretpath.Join(repoPath, "password")
121+
exists, err = client.Secrets().Exists(passwordPath)
122+
if err != nil {
123+
return false, err
124+
}
125+
if !exists {
126+
return false, nil
127+
}
128+
return true, nil
129+
}

0 commit comments

Comments
 (0)