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

Commit 5629d1e

Browse files
committed
Merge pull request #8 from pavanka/master
[parse-cli] refactor code from login.go to create a 'configure token' command
2 parents 3ba44cd + 831a31a commit 5629d1e

File tree

5 files changed

+109
-66
lines changed

5 files changed

+109
-66
lines changed

configure_cmd.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/facebookgo/stackerr"
7+
"github.com/spf13/cobra"
8+
)
9+
10+
type configureCmd struct {
11+
login login
12+
}
13+
14+
func (c *configureCmd) accessToken(e *env) error {
15+
fmt.Fprintf(e.Out,
16+
`Please enter the email id you used to register with Parse
17+
and an access token if you already generated it.
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, "Email: ")
26+
fmt.Fscanf(e.In, "%s\n", &credentials.email)
27+
fmt.Fprintf(e.Out, "Account Key: ")
28+
fmt.Fscanf(e.In, "%s\n", &credentials.token)
29+
30+
_, err := (&apps{login: login{credentials: credentials}}).restFetchApps(e)
31+
if err != nil {
32+
if err == errAuth {
33+
fmt.Fprintf(e.Err,
34+
`Sorry, we do not have a user with this email and access token.
35+
Please follow instructions at %s to generate a new access token.
36+
`,
37+
keysURL,
38+
)
39+
} else {
40+
fmt.Fprintf(e.Err, "Unable to validate token with error:\n%s\n", err)
41+
}
42+
return stackerr.New("Could not store credentials. Please try again.")
43+
}
44+
45+
err = c.login.storeCredentials(e, &credentials)
46+
if err == nil {
47+
fmt.Fprintln(e.Out, "Successfully stored credentials.")
48+
}
49+
return stackerr.Wrap(err)
50+
}
51+
52+
func newConfigureCmd(e *env) *cobra.Command {
53+
var c configureCmd
54+
55+
cmd := &cobra.Command{
56+
Use: "configure",
57+
Short: "Configure various Parse settings.",
58+
Long: "Configure various Parse settings like access tokens, project type, and more.",
59+
Run: func(c *cobra.Command, args []string) {
60+
c.Help()
61+
},
62+
}
63+
cmd.AddCommand(&cobra.Command{
64+
Use: "token",
65+
Short: "Store Parse access token on machine",
66+
Long: "Stores Parse access token in ~/.parse/netrc.",
67+
Run: runNoArgs(e, c.accessToken),
68+
})
69+
return cmd
70+
}

configure_cmd_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
import (
4+
"io/ioutil"
5+
"regexp"
6+
"strings"
7+
"testing"
8+
9+
"github.com/facebookgo/ensure"
10+
)
11+
12+
func TestConfigureAcessToken(t *testing.T) {
13+
t.Parallel()
14+
15+
h, _ := newAppHarness(t)
16+
defer h.Stop()
17+
18+
c := configureCmd{login: login{tokenReader: strings.NewReader("")}}
19+
h.env.In = ioutil.NopCloser(strings.NewReader("n\nemail\ntoken\n"))
20+
ensure.Nil(t, c.accessToken(h.env))
21+
ensure.DeepEqual(t,
22+
h.Out.String(),
23+
`Please enter the email id you used to register with Parse
24+
and an access token if you already generated it.
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: Email: Account Key: Successfully stored credentials.
27+
`)
28+
29+
h.env.In = ioutil.NopCloser(strings.NewReader("n\nemail\ninvalid\n"))
30+
ensure.Err(t, c.accessToken(h.env), regexp.MustCompile("Please try again"))
31+
ensure.DeepEqual(t,
32+
h.Err.String(),
33+
`Sorry, we do not have a user with this email and access token.
34+
Please follow instructions at https://www.parse.com/account/account_keys to generate a new access token.
35+
`)
36+
}

login.go

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
"github.com/skratchdot/open-golang/open"
1515
)
1616

17+
const keysURL = "https://www.parse.com/account/account_keys"
18+
1719
type credentials struct {
1820
email string
1921
password string
@@ -231,41 +233,3 @@ Please open %q in the browser to create a new account key.
231233
)
232234
}
233235
}
234-
235-
const keysURL = "https://www.parse.com/account/keys"
236-
237-
func (l *login) run(e *env) error {
238-
fmt.Fprintf(e.Out,
239-
`Please enter the email id you used to register with Parse
240-
and an account key if you already generated it.
241-
If you do not have an account key or would like to generate a new one,
242-
please type: "y" to open the browser or "n" to continue: `,
243-
)
244-
l.helpCreateToken(e)
245-
246-
var credentials credentials
247-
fmt.Fprintf(e.Out, "Email: ")
248-
fmt.Fscanf(e.In, "%s\n", &credentials.email)
249-
fmt.Fprintf(e.Out, "Account Key: ")
250-
fmt.Fscanf(e.In, "%s\n", &credentials.token)
251-
252-
_, err := (&apps{login: login{credentials: credentials}}).restFetchApps(e)
253-
if err != nil {
254-
if err == errAuth {
255-
fmt.Fprintf(e.Err, `Sorry, we do not have a user with this email and account key.
256-
Please follow instructions at %s to generate a new account key.
257-
`,
258-
keysURL,
259-
)
260-
} else {
261-
fmt.Fprintf(e.Err, "Unable to validate token with error:\n%s\n", err)
262-
}
263-
return stackerr.New("Could not store credentials. Please try again.")
264-
}
265-
266-
err = l.storeCredentials(e, &credentials)
267-
if err == nil {
268-
fmt.Fprintln(e.Out, "Successfully stored credentials.")
269-
}
270-
return stackerr.Wrap(err)
271-
}

login_test.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"io/ioutil"
54
"regexp"
65
"strings"
76
"testing"
@@ -124,30 +123,3 @@ machine api.example.org
124123
password token`,
125124
)
126125
}
127-
128-
func TestLogin(t *testing.T) {
129-
t.Parallel()
130-
131-
h, _ := newAppHarness(t)
132-
defer h.Stop()
133-
134-
l := &login{tokenReader: strings.NewReader("")}
135-
136-
h.env.In = ioutil.NopCloser(strings.NewReader("n\nemail\ntoken\n"))
137-
ensure.Nil(t, l.run(h.env))
138-
ensure.DeepEqual(t,
139-
h.Out.String(),
140-
`Please enter the email id you used to register with Parse
141-
and an account key if you already generated it.
142-
If you do not have an account key or would like to generate a new one,
143-
please type: "y" to open the browser or "n" to continue: Email: Account Key: Successfully stored credentials.
144-
`)
145-
146-
h.env.In = ioutil.NopCloser(strings.NewReader("n\nemail\ninvalid\n"))
147-
ensure.Err(t, l.run(h.env), regexp.MustCompile("Please try again"))
148-
ensure.DeepEqual(t,
149-
h.Err.String(),
150-
`Sorry, we do not have a user with this email and account key.
151-
Please follow instructions at https://www.parse.com/account/keys to generate a new account key.
152-
`)
153-
}

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ http://parse.com`,
280280
}
281281

282282
c.AddCommand(newAddCmd(e))
283+
c.AddCommand(newConfigureCmd(e))
283284
c.AddCommand(newDefaultCmd(e))
284285
c.AddCommand(newDeployCmd(e))
285286
c.AddCommand(newDevelopCmd(e))

0 commit comments

Comments
 (0)