diff --git a/configure_cmd.go b/configure_cmd.go index 35d0497..e0aca63 100644 --- a/configure_cmd.go +++ b/configure_cmd.go @@ -11,26 +11,19 @@ type configureCmd struct { login login } -func (c *configureCmd) accessToken(e *env) error { - fmt.Fprintf(e.Out, - `Please enter an access token if you already generated it. - -If you do not have an access token or would like to generate a new one, -please type: "y" to open the browser or "n" to continue: `, - ) - - c.login.helpCreateToken(e) - - var credentials credentials - fmt.Fprintf(e.Out, "Access Token: ") - fmt.Fscanf(e.In, "%s\n", &credentials.token) +func (c *configureCmd) accountKey(e *env) error { + token, err := c.login.helpCreateToken(e) + if err != nil { + return err + } - _, err := (&apps{login: login{credentials: credentials}}).restFetchApps(e) + credentials := credentials{token: token} + _, err = (&apps{login: login{credentials: credentials}}).restFetchApps(e) if err != nil { if err == errAuth { fmt.Fprintf(e.Err, - `Sorry, the access token you provided is not valid. -Please follow instructions at %s to generate a new access token. + `Sorry, the account key you provided is not valid. +Please follow instructions at %s to generate a new account key. `, keysURL, ) @@ -53,16 +46,17 @@ func newConfigureCmd(e *env) *cobra.Command { cmd := &cobra.Command{ Use: "configure", Short: "Configure various Parse settings", - Long: "Configure various Parse settings like access tokens, project type, and more.", + Long: "Configure various Parse settings like account keys, project type, and more.", Run: func(c *cobra.Command, args []string) { c.Help() }, } cmd.AddCommand(&cobra.Command{ - Use: "token", - Short: "Store Parse access token on machine", - Long: "Stores Parse access token in ~/.parse/netrc.", - Run: runNoArgs(e, c.accessToken), + Use: "accountkey", + Short: "Store Parse account key on machine", + Long: "Stores Parse account key in ~/.parse/netrc.", + Run: runNoArgs(e, c.accountKey), + Aliases: []string{"key"}, }) return cmd } diff --git a/configure_cmd_test.go b/configure_cmd_test.go index 3d71085..e146525 100644 --- a/configure_cmd_test.go +++ b/configure_cmd_test.go @@ -16,23 +16,21 @@ func TestConfigureAcessToken(t *testing.T) { defer h.Stop() c := configureCmd{login: login{tokenReader: strings.NewReader("")}} - h.env.In = ioutil.NopCloser(strings.NewReader("n\ntoken\n")) - ensure.Nil(t, c.accessToken(h.env)) - ensure.DeepEqual(t, + h.env.In = ioutil.NopCloser(strings.NewReader("token\n")) + ensure.Nil(t, c.accountKey(h.env)) + ensure.DeepEqual( + t, h.Out.String(), - `Please enter an access token if you already generated it. - -If you do not have an access token or would like to generate a new one, -please type: "y" to open the browser or "n" to continue: Please open "https://www.parse.com/account_keys" in the browser -and follow instructions to create an access token. -Access Token: Successfully stored credentials. + ` +Input your account key or press enter to generate a new one. +Account Key: Successfully stored credentials. `) - h.env.In = ioutil.NopCloser(strings.NewReader("n\nemail\ninvalid\n")) - ensure.Err(t, c.accessToken(h.env), regexp.MustCompile("Please try again")) + h.env.In = ioutil.NopCloser(strings.NewReader("email\ninvalid\n")) + ensure.Err(t, c.accountKey(h.env), regexp.MustCompile("Please try again")) ensure.DeepEqual(t, h.Err.String(), - `Sorry, the access token you provided is not valid. -Please follow instructions at https://www.parse.com/account_keys to generate a new access token. + `Sorry, the account key you provided is not valid. +Please follow instructions at https://www.parse.com/account_keys to generate a new account key. `, ) } diff --git a/login.go b/login.go index 448c2d3..8265fd1 100644 --- a/login.go +++ b/login.go @@ -6,6 +6,7 @@ import ( "io" "os" "path/filepath" + "strings" "github.com/bgentry/go-netrc/netrc" "github.com/bgentry/speakeasy" @@ -168,8 +169,7 @@ func (l *login) authUserWithToken(e *env) error { _, err = apps.restFetchApps(e) if err == errAuth { fmt.Fprintln(e.Err, - `Sorry, the token you configured is not valid. - + `Sorry, the account key you configured is not valid. `) } if err != nil { @@ -188,8 +188,11 @@ func (l *login) authUser(e *env) error { apps := &apps{} fmt.Fprintln( e.Out, - `To avoid typing the email and password everytime, -please type "parse configure token" and provide a valid access token. + ` +We’ve changed the way the CLI works. +To save time logging in, you should create an account key. + +Type “parse configure accountkey” to create a new account key. Please log in to Parse using your email and password.`, ) @@ -216,26 +219,27 @@ Please log in to Parse using your email and password.`, return errAuth } -func (l *login) helpCreateToken(e *env) { - var shouldOpen string - fmt.Fscanf(e.In, "%s\n", &shouldOpen) - if shouldOpen == "n" { - fmt.Fprintf(e.Out, - `Please open %q in the browser -and follow instructions to create an access token. -`, - keysURL, - ) - return - } - err := open.Run(keysURL) - if err != nil { - fmt.Fprintf(e.Err, - `Sorry, we could not open %q in the browser. -Please open %q in the browser to create a new account key. +func (l *login) helpCreateToken(e *env) (string, error) { + for i := 0; i < 4; i++ { + fmt.Fprintln(e.Out, "\nInput your account key or press enter to generate a new one.") + fmt.Fprintf(e.Out, `Account Key: `) + + var token string + fmt.Fscanf(e.In, "%s\n", &token) + token = strings.TrimSpace(token) + if token != "" { + return token, nil + } + + err := open.Run(keysURL) + if err != nil { + fmt.Fprintf(e.Err, + `Sorry, we couldn’t open the browser for you. +Go here to generate an account key: %q `, - keysURL, - keysURL, - ) + keysURL, + ) + } } + return "", stackerr.New("Account key cannot be empty. Please try again.") } diff --git a/new.go b/new.go index fd56a2a..d97e865 100644 --- a/new.go +++ b/new.go @@ -187,24 +187,10 @@ func (n *newCmd) configureSample( return err } - // at this point user has already set a default app for the project - // use its properties to fetch latest jssdk version and set it in config - config, err := configFromDir(e.Root) - if err != nil { - return err - } - defaultApp, err := config.app(config.getDefaultApp()) - if err != nil { - return err - } - masterKey, err := defaultApp.getMasterKey(e) - if err != nil { - return err - } e.ParseAPIClient = e.ParseAPIClient.WithCredentials( parse.MasterKey{ - ApplicationID: defaultApp.getApplicationID(), - MasterKey: masterKey, + ApplicationID: app.ApplicationID, + MasterKey: app.MasterKey, }, )