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

Commit 117c7b4

Browse files
committed
Merge pull request #21 from pavanka/new_flow
update account key creation flow
2 parents dd7450b + 3988719 commit 117c7b4

File tree

4 files changed

+56
-74
lines changed

4 files changed

+56
-74
lines changed

configure_cmd.go

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,19 @@ type configureCmd struct {
1111
login login
1212
}
1313

14-
func (c *configureCmd) accessToken(e *env) error {
15-
fmt.Fprintf(e.Out,
16-
`Please enter an access token if you already generated it.
17-
18-
If you do not have an access token or would like to generate a new one,
19-
please type: "y" to open the browser or "n" to continue: `,
20-
)
21-
22-
c.login.helpCreateToken(e)
23-
24-
var credentials credentials
25-
fmt.Fprintf(e.Out, "Access Token: ")
26-
fmt.Fscanf(e.In, "%s\n", &credentials.token)
14+
func (c *configureCmd) accountKey(e *env) error {
15+
token, err := c.login.helpCreateToken(e)
16+
if err != nil {
17+
return err
18+
}
2719

28-
_, err := (&apps{login: login{credentials: credentials}}).restFetchApps(e)
20+
credentials := credentials{token: token}
21+
_, err = (&apps{login: login{credentials: credentials}}).restFetchApps(e)
2922
if err != nil {
3023
if err == errAuth {
3124
fmt.Fprintf(e.Err,
32-
`Sorry, the access token you provided is not valid.
33-
Please follow instructions at %s to generate a new access token.
25+
`Sorry, the account key you provided is not valid.
26+
Please follow instructions at %s to generate a new account key.
3427
`,
3528
keysURL,
3629
)
@@ -53,16 +46,17 @@ func newConfigureCmd(e *env) *cobra.Command {
5346
cmd := &cobra.Command{
5447
Use: "configure",
5548
Short: "Configure various Parse settings",
56-
Long: "Configure various Parse settings like access tokens, project type, and more.",
49+
Long: "Configure various Parse settings like account keys, project type, and more.",
5750
Run: func(c *cobra.Command, args []string) {
5851
c.Help()
5952
},
6053
}
6154
cmd.AddCommand(&cobra.Command{
62-
Use: "token",
63-
Short: "Store Parse access token on machine",
64-
Long: "Stores Parse access token in ~/.parse/netrc.",
65-
Run: runNoArgs(e, c.accessToken),
55+
Use: "accountkey",
56+
Short: "Store Parse account key on machine",
57+
Long: "Stores Parse account key in ~/.parse/netrc.",
58+
Run: runNoArgs(e, c.accountKey),
59+
Aliases: []string{"key"},
6660
})
6761
return cmd
6862
}

configure_cmd_test.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,21 @@ func TestConfigureAcessToken(t *testing.T) {
1616
defer h.Stop()
1717

1818
c := configureCmd{login: login{tokenReader: strings.NewReader("")}}
19-
h.env.In = ioutil.NopCloser(strings.NewReader("n\ntoken\n"))
20-
ensure.Nil(t, c.accessToken(h.env))
21-
ensure.DeepEqual(t,
19+
h.env.In = ioutil.NopCloser(strings.NewReader("token\n"))
20+
ensure.Nil(t, c.accountKey(h.env))
21+
ensure.DeepEqual(
22+
t,
2223
h.Out.String(),
23-
`Please enter an access token if you already generated it.
24-
25-
If you do not have an access token or would like to generate a new one,
26-
please type: "y" to open the browser or "n" to continue: Please open "https://www.parse.com/account_keys" in the browser
27-
and follow instructions to create an access token.
28-
Access Token: Successfully stored credentials.
24+
`
25+
Input your account key or press enter to generate a new one.
26+
Account Key: Successfully stored credentials.
2927
`)
30-
h.env.In = ioutil.NopCloser(strings.NewReader("n\nemail\ninvalid\n"))
31-
ensure.Err(t, c.accessToken(h.env), regexp.MustCompile("Please try again"))
28+
h.env.In = ioutil.NopCloser(strings.NewReader("email\ninvalid\n"))
29+
ensure.Err(t, c.accountKey(h.env), regexp.MustCompile("Please try again"))
3230
ensure.DeepEqual(t,
3331
h.Err.String(),
34-
`Sorry, the access token you provided is not valid.
35-
Please follow instructions at https://www.parse.com/account_keys to generate a new access token.
32+
`Sorry, the account key you provided is not valid.
33+
Please follow instructions at https://www.parse.com/account_keys to generate a new account key.
3634
`,
3735
)
3836
}

login.go

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77
"os"
88
"path/filepath"
9+
"strings"
910

1011
"github.com/bgentry/go-netrc/netrc"
1112
"github.com/bgentry/speakeasy"
@@ -168,8 +169,7 @@ func (l *login) authUserWithToken(e *env) error {
168169
_, err = apps.restFetchApps(e)
169170
if err == errAuth {
170171
fmt.Fprintln(e.Err,
171-
`Sorry, the token you configured is not valid.
172-
172+
`Sorry, the account key you configured is not valid.
173173
`)
174174
}
175175
if err != nil {
@@ -188,8 +188,11 @@ func (l *login) authUser(e *env) error {
188188
apps := &apps{}
189189
fmt.Fprintln(
190190
e.Out,
191-
`To avoid typing the email and password everytime,
192-
please type "parse configure token" and provide a valid access token.
191+
`
192+
We’ve changed the way the CLI works.
193+
To save time logging in, you should create an account key.
194+
195+
Type “parse configure accountkey” to create a new account key.
193196
194197
Please log in to Parse using your email and password.`,
195198
)
@@ -216,26 +219,27 @@ Please log in to Parse using your email and password.`,
216219
return errAuth
217220
}
218221

219-
func (l *login) helpCreateToken(e *env) {
220-
var shouldOpen string
221-
fmt.Fscanf(e.In, "%s\n", &shouldOpen)
222-
if shouldOpen == "n" {
223-
fmt.Fprintf(e.Out,
224-
`Please open %q in the browser
225-
and follow instructions to create an access token.
226-
`,
227-
keysURL,
228-
)
229-
return
230-
}
231-
err := open.Run(keysURL)
232-
if err != nil {
233-
fmt.Fprintf(e.Err,
234-
`Sorry, we could not open %q in the browser.
235-
Please open %q in the browser to create a new account key.
222+
func (l *login) helpCreateToken(e *env) (string, error) {
223+
for i := 0; i < 4; i++ {
224+
fmt.Fprintln(e.Out, "\nInput your account key or press enter to generate a new one.")
225+
fmt.Fprintf(e.Out, `Account Key: `)
226+
227+
var token string
228+
fmt.Fscanf(e.In, "%s\n", &token)
229+
token = strings.TrimSpace(token)
230+
if token != "" {
231+
return token, nil
232+
}
233+
234+
err := open.Run(keysURL)
235+
if err != nil {
236+
fmt.Fprintf(e.Err,
237+
`Sorry, we couldn’t open the browser for you.
238+
Go here to generate an account key: %q
236239
`,
237-
keysURL,
238-
keysURL,
239-
)
240+
keysURL,
241+
)
242+
}
240243
}
244+
return "", stackerr.New("Account key cannot be empty. Please try again.")
241245
}

new.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -187,24 +187,10 @@ func (n *newCmd) configureSample(
187187
return err
188188
}
189189

190-
// at this point user has already set a default app for the project
191-
// use its properties to fetch latest jssdk version and set it in config
192-
config, err := configFromDir(e.Root)
193-
if err != nil {
194-
return err
195-
}
196-
defaultApp, err := config.app(config.getDefaultApp())
197-
if err != nil {
198-
return err
199-
}
200-
masterKey, err := defaultApp.getMasterKey(e)
201-
if err != nil {
202-
return err
203-
}
204190
e.ParseAPIClient = e.ParseAPIClient.WithCredentials(
205191
parse.MasterKey{
206-
ApplicationID: defaultApp.getApplicationID(),
207-
MasterKey: masterKey,
192+
ApplicationID: app.ApplicationID,
193+
MasterKey: app.MasterKey,
208194
},
209195
)
210196

0 commit comments

Comments
 (0)