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

Commit e88a3e6

Browse files
committed
Move init helper functions to init.go file
1 parent 31b3027 commit e88a3e6

2 files changed

Lines changed: 86 additions & 92 deletions

File tree

internals/secrethub/init.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ import (
99
"github.com/secrethub/secrethub-cli/internals/cli/progress"
1010
"github.com/secrethub/secrethub-cli/internals/cli/ui"
1111
"github.com/secrethub/secrethub-cli/internals/secrethub/command"
12+
"github.com/secrethub/secrethub-go/internals/api"
1213
"github.com/secrethub/secrethub-go/internals/errio"
1314
"github.com/secrethub/secrethub-go/pkg/secrethub"
15+
"github.com/secrethub/secrethub-go/pkg/secrethub/configdir"
1416
"github.com/secrethub/secrethub-go/pkg/secrethub/credentials"
17+
"github.com/secrethub/secrethub-go/pkg/secretpath"
1518
)
1619

1720
const signupMessage = "Go to https://signup.secrethub.io/ and follow the steps to create an account and get it set up on this machine."
@@ -279,3 +282,86 @@ func promptForDeviceName(io ui.IO) (string, error) {
279282
}
280283
return deviceName, nil
281284
}
285+
286+
// createStartRepo creates a start repository and writes a fist secret to it, so that
287+
// the user can start by reading their first secret. It returns the secret's path.
288+
// This is intended to smoothen onboarding.
289+
func createStartRepo(client secrethub.ClientInterface, username string, fullName string) (string, error) {
290+
repoPath := secretpath.Join(username, "start")
291+
_, err := client.Repos().Create(secretpath.Join(repoPath))
292+
if err != nil {
293+
return "", err
294+
}
295+
296+
secretPath := secretpath.Join(repoPath, "hello")
297+
message := fmt.Sprintf("Welcome %s! This is your first secret. To write a new version of this secret, run:\n\n secrethub write %s", fullName, secretPath)
298+
299+
_, err = client.Secrets().Write(secretPath, []byte(message))
300+
if err != nil {
301+
return "", err
302+
}
303+
return secretPath, nil
304+
}
305+
306+
// createWorkspace creates a new org with the given name and description.
307+
func createWorkspace(client secrethub.ClientInterface, io ui.IO, org string, orgDescription string, progressPrinter progress.Printer) error {
308+
if org == "" {
309+
createWorkspace, err := ui.AskYesNo(io, "Do you want to create a shared workspace for your team?", ui.DefaultYes)
310+
if err != nil {
311+
return err
312+
}
313+
fmt.Fprintln(io.Output())
314+
if !createWorkspace {
315+
fmt.Fprint(io.Output(), "You can create a shared workspace later using `secrethub org init`.\n\n")
316+
return nil
317+
}
318+
}
319+
320+
var err error
321+
if org == "" {
322+
org, err = ui.AskAndValidate(io, "Workspace name (e.g. your company name): ", 2, api.ValidateOrgName)
323+
if err != nil {
324+
return err
325+
}
326+
}
327+
if orgDescription == "" {
328+
orgDescription, err = ui.AskAndValidate(io, "A description (max 144 chars) for your team workspace so others will recognize it:\n", 2, api.ValidateOrgDescription)
329+
if err != nil {
330+
return err
331+
}
332+
}
333+
334+
fmt.Fprint(io.Output(), "Creating your shared workspace...")
335+
progressPrinter.Start()
336+
337+
_, err = client.Orgs().Create(org, orgDescription)
338+
progressPrinter.Stop()
339+
if err == api.ErrOrgAlreadyExists {
340+
fmt.Fprintf(io.Output(), "The workspace %s already exists. If it is your organization, ask a colleague to invite you to the workspace. You can also create a new one using `secrethub org init`.\n", org)
341+
} else if err != nil {
342+
return err
343+
} else {
344+
fmt.Fprint(io.Output(), "Created your shared workspace.\n\n")
345+
}
346+
return nil
347+
}
348+
349+
// writeCredential writes the given credential to the configuration directory.
350+
func writeNewCredential(credential *credentials.KeyCreator, passphrase string, credentialFile *configdir.CredentialFile) error {
351+
exportKey := credential.Key
352+
if passphrase != "" {
353+
exportKey = exportKey.Passphrase(credentials.FromString(passphrase))
354+
}
355+
356+
encodedCredential, err := credential.Export()
357+
if err != nil {
358+
return err
359+
}
360+
361+
return credentialFile.Write(encodedCredential)
362+
}
363+
364+
// askCredentialPassphrase prompts the user for a passphrase to protect the local credential.
365+
func askCredentialPassphrase(io ui.IO) (string, error) {
366+
return ui.AskPassphrase(io, "Please enter a passphrase to protect your local credential (leave empty for no passphrase): ", "Enter the same passphrase again: ", 3)
367+
}

internals/secrethub/signup.go

Lines changed: 0 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,8 @@ package secrethub
33
import (
44
"fmt"
55

6-
"github.com/secrethub/secrethub-go/pkg/secrethub/configdir"
7-
8-
"github.com/secrethub/secrethub-go/pkg/secrethub"
9-
10-
"github.com/secrethub/secrethub-cli/internals/cli/progress"
116
"github.com/secrethub/secrethub-cli/internals/cli/ui"
127
"github.com/secrethub/secrethub-cli/internals/secrethub/command"
13-
14-
"github.com/secrethub/secrethub-go/internals/api"
15-
"github.com/secrethub/secrethub-go/pkg/secrethub/credentials"
16-
"github.com/secrethub/secrethub-go/pkg/secretpath"
178
)
189

1910
// Errors
@@ -49,86 +40,3 @@ func (cmd *SignUpCommand) Run() error {
4940
fmt.Fprintln(cmd.io.Output(), signupMessage)
5041
return nil
5142
}
52-
53-
// createStartRepo creates a start repository and writes a fist secret to it, so that
54-
// the user can start by reading their first secret. It returns the secret's path.
55-
// This is intended to smoothen onboarding.
56-
func createStartRepo(client secrethub.ClientInterface, username string, fullName string) (string, error) {
57-
repoPath := secretpath.Join(username, "start")
58-
_, err := client.Repos().Create(secretpath.Join(repoPath))
59-
if err != nil {
60-
return "", err
61-
}
62-
63-
secretPath := secretpath.Join(repoPath, "hello")
64-
message := fmt.Sprintf("Welcome %s! This is your first secret. To write a new version of this secret, run:\n\n secrethub write %s", fullName, secretPath)
65-
66-
_, err = client.Secrets().Write(secretPath, []byte(message))
67-
if err != nil {
68-
return "", err
69-
}
70-
return secretPath, nil
71-
}
72-
73-
// createWorkspace creates a new org with the given name and description.
74-
func createWorkspace(client secrethub.ClientInterface, io ui.IO, org string, orgDescription string, progressPrinter progress.Printer) error {
75-
if org == "" {
76-
createWorkspace, err := ui.AskYesNo(io, "Do you want to create a shared workspace for your team?", ui.DefaultYes)
77-
if err != nil {
78-
return err
79-
}
80-
fmt.Fprintln(io.Output())
81-
if !createWorkspace {
82-
fmt.Fprint(io.Output(), "You can create a shared workspace later using `secrethub org init`.\n\n")
83-
return nil
84-
}
85-
}
86-
87-
var err error
88-
if org == "" {
89-
org, err = ui.AskAndValidate(io, "Workspace name (e.g. your company name): ", 2, api.ValidateOrgName)
90-
if err != nil {
91-
return err
92-
}
93-
}
94-
if orgDescription == "" {
95-
orgDescription, err = ui.AskAndValidate(io, "A description (max 144 chars) for your team workspace so others will recognize it:\n", 2, api.ValidateOrgDescription)
96-
if err != nil {
97-
return err
98-
}
99-
}
100-
101-
fmt.Fprint(io.Output(), "Creating your shared workspace...")
102-
progressPrinter.Start()
103-
104-
_, err = client.Orgs().Create(org, orgDescription)
105-
progressPrinter.Stop()
106-
if err == api.ErrOrgAlreadyExists {
107-
fmt.Fprintf(io.Output(), "The workspace %s already exists. If it is your organization, ask a colleague to invite you to the workspace. You can also create a new one using `secrethub org init`.\n", org)
108-
} else if err != nil {
109-
return err
110-
} else {
111-
fmt.Fprint(io.Output(), "Created your shared workspace.\n\n")
112-
}
113-
return nil
114-
}
115-
116-
// writeCredential writes the given credential to the configuration directory.
117-
func writeNewCredential(credential *credentials.KeyCreator, passphrase string, credentialFile *configdir.CredentialFile) error {
118-
exportKey := credential.Key
119-
if passphrase != "" {
120-
exportKey = exportKey.Passphrase(credentials.FromString(passphrase))
121-
}
122-
123-
encodedCredential, err := credential.Export()
124-
if err != nil {
125-
return err
126-
}
127-
128-
return credentialFile.Write(encodedCredential)
129-
}
130-
131-
// askCredentialPassphrase prompts the user for a passphrase to protect the local credential.
132-
func askCredentialPassphrase(io ui.IO) (string, error) {
133-
return ui.AskPassphrase(io, "Please enter a passphrase to protect your local credential (leave empty for no passphrase): ", "Enter the same passphrase again: ", 3)
134-
}

0 commit comments

Comments
 (0)