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

Commit 1c4ac05

Browse files
committed
[parse-cli] upgrade to new config format while still supporting the old format
1 parent 9830c60 commit 1c4ac05

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1519
-914
lines changed

add.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package main
2+
3+
import (
4+
"github.com/facebookgo/stackerr"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
type addCmd struct {
9+
MakeDefault bool
10+
apps *apps
11+
verbose bool
12+
}
13+
14+
func (a *addCmd) addSelectedApp(
15+
app *app,
16+
args []string,
17+
e *env,
18+
) error {
19+
switch e.Type {
20+
case legacyParseFormat, parseFormat:
21+
appConfig := a.getParseAppConfig(app)
22+
return a.addSelectedParseApp(app.Name, appConfig, args, e)
23+
}
24+
25+
return stackerr.Newf("Unknown project type: %d.", e.Type)
26+
}
27+
28+
func (a *addCmd) selectApp(e *env) (*app, error) {
29+
apps, err := a.apps.restFetchApps(e)
30+
if err != nil {
31+
return nil, err
32+
}
33+
app, err := a.apps.selectApp(apps, "Select an App to add to config: ", e)
34+
if err != nil {
35+
return nil, err
36+
}
37+
return app, nil
38+
}
39+
40+
func (a *addCmd) run(e *env, args []string) error {
41+
42+
if err := a.apps.login.authUser(e); err != nil {
43+
return err
44+
}
45+
app, err := a.selectApp(e)
46+
if err != nil {
47+
return err
48+
}
49+
return a.addSelectedApp(app, args, e)
50+
}
51+
52+
func newAddCmd(e *env) *cobra.Command {
53+
a := &addCmd{
54+
MakeDefault: false,
55+
apps: &apps{},
56+
verbose: true,
57+
}
58+
cmd := &cobra.Command{
59+
Use: "add [app]",
60+
Short: "Adds a new Parse App to config in current Cloud Code directory",
61+
Long: `Adds a new Parse App to config in current Cloud Code directory.
62+
If an argument is given, the added application can also be referenced by that name.`,
63+
Run: runWithArgs(e, a.run),
64+
}
65+
cmd.Flags().BoolVarP(&a.MakeDefault, "default", "d", a.MakeDefault,
66+
"Make the selected app default")
67+
return cmd
68+
}

add_cmd.go

Lines changed: 40 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,68 @@
11
package main
22

33
import (
4-
"os"
5-
"path/filepath"
4+
"fmt"
65

76
"github.com/facebookgo/stackerr"
8-
"github.com/spf13/cobra"
97
)
108

11-
type addCmd struct {
12-
MakeDefault bool
13-
apps *apps
9+
func (a *addCmd) getParseAppConfig(app *app) *parseAppConfig {
10+
return &parseAppConfig{
11+
ApplicationID: app.ApplicationID,
12+
masterKey: app.MasterKey,
13+
}
1414
}
1515

16-
func (a *addCmd) writeConfig(
17-
app *app,
16+
func (a *addCmd) addSelectedParseApp(
17+
appName string,
18+
appConfig *parseAppConfig,
1819
args []string,
1920
e *env,
20-
verbose bool,
2121
) error {
2222
config, err := configFromDir(e.Root)
2323
if err != nil {
2424
return err
2525
}
26+
parseConfig, ok := config.(*parseConfig)
27+
if !ok {
28+
return stackerr.New("Invalid Cloud Code config.")
29+
}
2630

27-
p := config.getProjectConfig()
28-
if p.Type == legacy {
29-
parseConfig, ok := config.(*parseConfig)
30-
if !ok {
31-
return stackerr.New("Invalid Cloud Code config.")
32-
}
33-
return a.writeParseConfig(parseConfig, app, args, e, verbose)
31+
// add app to config
32+
if _, ok := parseConfig.Applications[appName]; ok {
33+
return stackerr.Newf("App %s has already been added", appName)
3434
}
3535

36-
return stackerr.Newf("Unknown project type: %s.", p.Type)
37-
}
36+
parseConfig.Applications[appName] = appConfig
3837

39-
func (a *addCmd) selectApp(e *env) (*app, error) {
40-
apps, err := a.apps.restFetchApps(e)
41-
if err != nil {
42-
return nil, err
43-
}
44-
app, err := a.apps.selectApp(apps, "Select an App to add to config: ", e)
45-
if err != nil {
46-
return nil, err
38+
if len(args) > 0 && args[0] != "" {
39+
alias := args[0]
40+
aliasConfig, ok := parseConfig.Applications[alias]
41+
if !ok {
42+
parseConfig.Applications[alias] = &parseAppConfig{Link: appName}
43+
}
44+
if ok && aliasConfig.getLink() != "" {
45+
fmt.Fprintf(e.Out, "Overwriting alias: %q to point to %q\n", alias, appName)
46+
parseConfig.Applications[alias] = &parseAppConfig{Link: appName}
47+
}
4748
}
48-
return app, nil
49-
}
5049

51-
func (a *addCmd) run(e *env, args []string) error {
52-
_, err := os.Lstat(filepath.Join(e.Root, legacyConfigFile))
53-
if os.IsNotExist(err) {
54-
return stackerr.New("Please run add command inside a parse project.")
50+
if a.MakeDefault {
51+
if _, ok := parseConfig.Applications[defaultKey]; ok {
52+
return stackerr.New(`Default key already set. To override default, use command "parse default"`)
53+
}
54+
parseConfig.Applications[defaultKey] = &parseAppConfig{Link: appName}
5555
}
56-
if err := a.apps.login.authUser(e); err != nil {
56+
57+
if err := storeConfig(e, parseConfig); err != nil {
5758
return err
5859
}
59-
app, err := a.selectApp(e)
60-
if err != nil {
61-
return err
60+
if a.verbose {
61+
fmt.Fprintf(e.Out, "Written config for %q\n", appName)
62+
if a.MakeDefault {
63+
fmt.Fprintf(e.Out, "Set %q as default\n", appName)
64+
}
6265
}
63-
return a.writeConfig(app, args, e, true)
64-
}
6566

66-
func newAddCmd(e *env) *cobra.Command {
67-
a := &addCmd{
68-
MakeDefault: false,
69-
apps: &apps{},
70-
}
71-
cmd := &cobra.Command{
72-
Use: "add [app]",
73-
Short: "Adds a new Parse App to config in current Cloud Code directory",
74-
Long: `Adds a new Parse App to config in current Cloud Code directory.
75-
If an argument is given, the added application can also be referenced by that name.`,
76-
Run: runWithArgs(e, a.run),
77-
}
78-
cmd.Flags().BoolVarP(&a.MakeDefault, "default", "d", a.MakeDefault,
79-
"Make the selected app default")
80-
return cmd
67+
return nil
8168
}

add_cmd_parse.go

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)