|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "os" |
5 | | - "path/filepath" |
| 4 | + "fmt" |
6 | 5 |
|
7 | 6 | "github.com/facebookgo/stackerr" |
8 | | - "github.com/spf13/cobra" |
9 | 7 | ) |
10 | 8 |
|
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 | + } |
14 | 14 | } |
15 | 15 |
|
16 | | -func (a *addCmd) writeConfig( |
17 | | - app *app, |
| 16 | +func (a *addCmd) addSelectedParseApp( |
| 17 | + appName string, |
| 18 | + appConfig *parseAppConfig, |
18 | 19 | args []string, |
19 | 20 | e *env, |
20 | | - verbose bool, |
21 | 21 | ) error { |
22 | 22 | config, err := configFromDir(e.Root) |
23 | 23 | if err != nil { |
24 | 24 | return err |
25 | 25 | } |
| 26 | + parseConfig, ok := config.(*parseConfig) |
| 27 | + if !ok { |
| 28 | + return stackerr.New("Invalid Cloud Code config.") |
| 29 | + } |
26 | 30 |
|
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) |
34 | 34 | } |
35 | 35 |
|
36 | | - return stackerr.Newf("Unknown project type: %s.", p.Type) |
37 | | -} |
| 36 | + parseConfig.Applications[appName] = appConfig |
38 | 37 |
|
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 | + } |
47 | 48 | } |
48 | | - return app, nil |
49 | | -} |
50 | 49 |
|
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} |
55 | 55 | } |
56 | | - if err := a.apps.login.authUser(e); err != nil { |
| 56 | + |
| 57 | + if err := storeConfig(e, parseConfig); err != nil { |
57 | 58 | return err |
58 | 59 | } |
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 | + } |
62 | 65 | } |
63 | | - return a.writeConfig(app, args, e, true) |
64 | | -} |
65 | 66 |
|
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 |
81 | 68 | } |
0 commit comments