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

Commit e3bae97

Browse files
committed
Merge pull request #23 from pavanka/emails
add multiple email support for account keys
2 parents b3f0b8e + e08f5b3 commit e3bae97

File tree

11 files changed

+333
-73
lines changed

11 files changed

+333
-73
lines changed

config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ type projectConfig struct {
4545
// Currently jssdk version is the only
4646
// project level config for Parse type.
4747
Parse *parseProjectConfig `json:"parse,omitempty"`
48+
// ParserEmail is an email id of the Parse developer.
49+
// It is associated with this project.
50+
// It is used to fetch appropriate credentials from netrc.
51+
ParserEmail string `json:"email,omitempty"`
4852
}
4953

5054
func getConfigFile(e *env) string {
@@ -168,6 +172,7 @@ func storeProjectConfig(e *env, c config) error {
168172
}
169173
lconf := &legacyConfig{Applications: p.Applications}
170174
lconf.Global.ParseVersion = p.projectConfig.Parse.JSSDK
175+
lconf.Global.ParserEmail = p.projectConfig.ParserEmail
171176
return writeLegacyConfigFile(
172177
lconf,
173178
filepath.Join(e.Root, legacyConfigFile),

configure_cmd.go

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import (
88
)
99

1010
type configureCmd struct {
11-
login login
11+
login login
12+
isDefault bool
1213
}
1314

1415
func (c *configureCmd) accountKey(e *env) error {
@@ -17,29 +18,61 @@ func (c *configureCmd) accountKey(e *env) error {
1718
return err
1819
}
1920

20-
credentials := credentials{token: token}
21-
_, err = (&apps{login: login{credentials: credentials}}).restFetchApps(e)
21+
email, err := c.login.authToken(e, token)
2222
if err != nil {
23-
if err == errAuth {
24-
fmt.Fprintf(e.Err,
25-
`Sorry, the account key you provided is not valid.
26-
Please follow instructions at %s to generate a new account key.
27-
`,
28-
keysURL,
23+
fmt.Fprintln(e.Err, "Could not store credentials. Please try again.\n")
24+
return err
25+
}
26+
27+
if c.isDefault {
28+
email = ""
29+
}
30+
31+
creds, _ := (&login{}).getTokenCredentials(e, email)
32+
if creds != nil {
33+
if c.isDefault {
34+
fmt.Fprintln(
35+
e.Err,
36+
"Note: this operation will overwrite the default account key",
2937
)
3038
} else {
31-
fmt.Fprintf(e.Err, "Unable to validate token with error:\n%s\n", err)
39+
fmt.Fprintf(
40+
e.Err,
41+
`Note: this operation will overwrite the account key:
42+
%q
43+
for email: %q
44+
`,
45+
last4(token),
46+
email,
47+
)
3248
}
33-
return stackerr.New("Could not store credentials. Please try again.")
3449
}
3550

36-
err = c.login.storeCredentials(e, &credentials)
51+
err = c.login.storeCredentials(e, email, &credentials{token: token})
3752
if err == nil {
3853
fmt.Fprintln(e.Out, "Successfully stored credentials.")
3954
}
4055
return stackerr.Wrap(err)
4156
}
4257

58+
func (c *configureCmd) parserEmail(e *env, args []string) error {
59+
config, err := configFromDir(e.Root)
60+
if err != nil {
61+
return err
62+
}
63+
if len(args) != 1 {
64+
return fmt.Errorf("Invalid args: %v, only an email argument is expected.", args)
65+
}
66+
config.getProjectConfig().ParserEmail = args[0]
67+
err = storeProjectConfig(e, config)
68+
if err != nil {
69+
fmt.Fprintln(e.Err, "Could not set parser email for project.")
70+
return err
71+
}
72+
fmt.Fprintf(e.Out, "Successfully configured email for current project to: %q\n", args[0])
73+
return nil
74+
}
75+
4376
func newConfigureCmd(e *env) *cobra.Command {
4477
var c configureCmd
4578

@@ -51,12 +84,25 @@ func newConfigureCmd(e *env) *cobra.Command {
5184
c.Help()
5285
},
5386
}
54-
cmd.AddCommand(&cobra.Command{
87+
88+
keyCmd := &cobra.Command{
5589
Use: "accountkey",
5690
Short: "Store Parse account key on machine",
5791
Long: "Stores Parse account key in ~/.parse/netrc.",
5892
Run: runNoArgs(e, c.accountKey),
5993
Aliases: []string{"key"},
60-
})
94+
}
95+
keyCmd.Flags().BoolVarP(&c.isDefault, "default", "d", c.isDefault,
96+
"Make this token a system default")
97+
cmd.AddCommand(keyCmd)
98+
99+
emailCmd := &cobra.Command{
100+
Use: "email",
101+
Short: "Configures the parser email for this project",
102+
Long: "Configures the parser email for current project.",
103+
Run: runWithArgs(e, c.parserEmail),
104+
}
105+
cmd.AddCommand(emailCmd)
106+
61107
return cmd
62108
}

configure_cmd_test.go

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ package main
22

33
import (
44
"io/ioutil"
5+
"path/filepath"
56
"regexp"
67
"strings"
78
"testing"
89

910
"github.com/facebookgo/ensure"
1011
)
1112

12-
func TestConfigureAcessToken(t *testing.T) {
13+
func TestConfigureAccessToken(t *testing.T) {
1314
t.Parallel()
1415

15-
h, _ := newAppHarness(t)
16+
h := newTokenHarness(t)
1617
defer h.Stop()
1718

1819
c := configureCmd{login: login{tokenReader: strings.NewReader("")}}
@@ -26,11 +27,38 @@ Input your account key or press enter to generate a new one.
2627
Account Key: Successfully stored credentials.
2728
`)
2829
h.env.In = ioutil.NopCloser(strings.NewReader("email\ninvalid\n"))
29-
ensure.Err(t, c.accountKey(h.env), regexp.MustCompile("Please try again"))
30+
ensure.Err(t, c.accountKey(h.env), regexp.MustCompile("is not valid"))
3031
ensure.DeepEqual(t,
3132
h.Err.String(),
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.
33+
"Could not store credentials. Please try again.\n\n",
34+
)
35+
}
36+
37+
func TestParserEmail(t *testing.T) {
38+
t.Parallel()
39+
40+
h := newTokenHarness(t)
41+
h.makeEmptyRoot()
42+
defer h.Stop()
43+
44+
var n newCmd
45+
ensure.Nil(t, n.createConfigWithContent(filepath.Join(h.env.Root, parseLocal), "{}"))
46+
ensure.Nil(t,
47+
n.createConfigWithContent(
48+
filepath.Join(h.env.Root, parseProject),
49+
`{"project_type": 1}`,
50+
),
51+
)
52+
53+
var c configureCmd
54+
ensure.Nil(t, c.parserEmail(h.env, []string{"email2"}))
55+
ensure.DeepEqual(
56+
t,
57+
h.Out.String(),
58+
`Successfully configured email for current project to: "email2"
3459
`,
3560
)
61+
62+
ensure.Err(t, c.parserEmail(h.env, nil), regexp.MustCompile("Invalid args:"))
63+
ensure.Err(t, c.parserEmail(h.env, []string{"a", "b"}), regexp.MustCompile("Invalid args:"))
3664
}

legacy_config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var legacyConfigFile = filepath.Join(configDir, "global.json")
1616
type legacyConfig struct {
1717
Global struct {
1818
ParseVersion string `json:"parseVersion,omitempty"`
19+
ParserEmail string `json:"email,omitempty"`
1920
} `json:"global,omitempty"`
2021
Applications map[string]*parseAppConfig `json:"applications,omitempty"`
2122
}

list_cmd.go

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

33
import (
4+
"fmt"
45
"os"
56

67
"github.com/facebookgo/stackerr"
@@ -23,6 +24,7 @@ func (l *listCmd) printListOfApps(e *env) error {
2324
return stackerr.Wrap(err)
2425
}
2526
config.pprintApps(e)
27+
fmt.Fprintln(e.Out, "")
2628
return nil
2729
}
2830

0 commit comments

Comments
 (0)