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

Commit b752218

Browse files
Merge pull request #192 from secrethub/feature/demo-init
Add demo init command
2 parents a7e8e23 + c657786 commit b752218

3 files changed

Lines changed: 100 additions & 5 deletions

File tree

internals/demo/command.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,23 @@ import (
77

88
// Command is a command to run the secrethub example app.
99
type Command struct {
10-
io ui.IO
10+
io ui.IO
11+
newClient newClientFunc
1112
}
1213

1314
// NewCommand creates a new example app command.
14-
func NewCommand(io ui.IO) *Command {
15+
func NewCommand(io ui.IO, newClient newClientFunc) *Command {
1516
return &Command{
16-
io: io,
17+
io: io,
18+
newClient: newClient,
1719
}
1820
}
1921

2022
// Register registers the command, arguments and flags on the provided Registerer.
2123
func (cmd *Command) Register(r command.Registerer) {
22-
clause := r.Command("demo", "Runs the secrethub demo app as used in different guides.")
24+
clause := r.Command("demo", "Manage the demo application.")
25+
clause.Hidden()
2326

27+
NewInitCommand(cmd.io, cmd.newClient).Register(clause)
2428
NewServeCommand(cmd.io).Register(clause)
2529
}

internals/demo/init.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package demo
2+
3+
import (
4+
"crypto/hmac"
5+
"crypto/sha256"
6+
"encoding/base64"
7+
"fmt"
8+
9+
"github.com/secrethub/secrethub-cli/internals/cli/ui"
10+
"github.com/secrethub/secrethub-cli/internals/secrethub/command"
11+
12+
"github.com/secrethub/secrethub-go/internals/api"
13+
"github.com/secrethub/secrethub-go/pkg/secrethub"
14+
"github.com/secrethub/secrethub-go/pkg/secretpath"
15+
)
16+
17+
type newClientFunc func() (secrethub.ClientInterface, error)
18+
19+
const defaultDemoRepo = "demo"
20+
21+
type InitCommand struct {
22+
repo api.RepoPath
23+
24+
io ui.IO
25+
newClient newClientFunc
26+
}
27+
28+
func NewInitCommand(io ui.IO, newClient newClientFunc) *InitCommand {
29+
return &InitCommand{
30+
io: io,
31+
newClient: newClient,
32+
}
33+
}
34+
35+
// Register registers the command, arguments and flags on the provided Registerer.
36+
func (cmd *InitCommand) Register(r command.Registerer) {
37+
clause := r.Command("init", "Create the secrets necessary to connect with the demo application.")
38+
clause.HelpLong("demo init creates a repository with the username and password needed to connect to the demo API.")
39+
40+
clause.Flag("repo", "The path of the repository to create. Defaults to a "+defaultDemoRepo+" repo in your personal namespace.").SetValue(&cmd.repo)
41+
42+
command.BindAction(clause, cmd.Run)
43+
}
44+
45+
// Run handles the command with the options as specified in the command.
46+
func (cmd *InitCommand) Run() error {
47+
client, err := cmd.newClient()
48+
if err != nil {
49+
return err
50+
}
51+
52+
var repoPath string
53+
var username string
54+
if cmd.repo == "" {
55+
me, err := client.Me().GetUser()
56+
if err != nil {
57+
return err
58+
}
59+
username = me.Username
60+
repoPath = secretpath.Join(me.Username, defaultDemoRepo)
61+
} else {
62+
username = secretpath.Namespace(cmd.repo.Value())
63+
repoPath = cmd.repo.Value()
64+
}
65+
66+
_, 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)
69+
} else if err != nil {
70+
return err
71+
}
72+
73+
usernamePath := secretpath.Join(repoPath, "username")
74+
_, err = client.Secrets().Write(usernamePath, []byte(username))
75+
if err != nil {
76+
return err
77+
}
78+
79+
h := hmac.New(sha256.New, []byte("this-is-no-good-way-to-generate-a-password-that-is-why-we-only-use-it-for-demo-purposes"))
80+
password := base64.RawStdEncoding.EncodeToString(h.Sum([]byte(username)))[:20]
81+
82+
passwordPath := secretpath.Join(repoPath, "password")
83+
_, err = client.Secrets().Write(passwordPath, []byte(password))
84+
if err != nil {
85+
return err
86+
}
87+
88+
fmt.Printf("Created the following secrets:\n%s\n%s\n", usernamePath, passwordPath)
89+
90+
return nil
91+
}

internals/secrethub/app.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,5 +184,5 @@ func (app *App) registerCommands() {
184184
NewClearClipboardCommand().Register(app.cli)
185185
NewKeyringClearCommand().Register(app.cli)
186186

187-
demo.NewCommand(app.io).Register(app.cli)
187+
demo.NewCommand(app.io, app.clientFactory.NewClient).Register(app.cli)
188188
}

0 commit comments

Comments
 (0)