This repository has been archived by the owner on Jan 16, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathadd.go
111 lines (98 loc) · 2.78 KB
/
add.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"github.com/ParsePlatform/parse-cli/herokucmd"
"github.com/ParsePlatform/parse-cli/parsecli"
"github.com/ParsePlatform/parse-cli/parsecmd"
"github.com/facebookgo/stackerr"
"github.com/spf13/cobra"
)
type addCmd struct {
MakeDefault bool
apps *parsecli.Apps
verbose bool
}
func (a *addCmd) addSelectedApp(
name string,
appConfig parsecli.AppConfig,
args []string,
e *parsecli.Env,
) error {
switch e.Type {
case parsecli.LegacyParseFormat, parsecli.ParseFormat:
parseAppConfig, ok := appConfig.(*parsecli.ParseAppConfig)
if !ok {
return stackerr.New("invalid parse app config passed.")
}
return parsecmd.AddSelectedParseApp(name, parseAppConfig, args, a.MakeDefault, a.verbose, e)
case parsecli.HerokuFormat:
herokuAppConfig, ok := appConfig.(*parsecli.HerokuAppConfig)
if !ok {
return stackerr.New("invalid heroku app config passed.")
}
return herokucmd.AddSelectedHerokuApp(name, herokuAppConfig, args, a.MakeDefault, a.verbose, e)
}
return stackerr.Newf("Unknown project type: %d.", e.Type)
}
func (a *addCmd) selectApp(e *parsecli.Env, appName string) (*parsecli.App, error) {
apps, err := a.apps.RestFetchApps(e)
if err != nil {
return nil, err
}
if appName != "" {
for _, app := range apps {
if app.Name == appName {
return app, nil
}
}
return nil, stackerr.Newf("You are not a collaborator on app: %s", appName)
}
app, err := a.apps.SelectApp(apps, "Select an App to add to config: ", e)
if err != nil {
return nil, err
}
return app, nil
}
func (a *addCmd) run(e *parsecli.Env, args []string) error {
if err := a.apps.Login.AuthUser(e, false); err != nil {
return err
}
var appName string
if len(args) > 1 {
return stackerr.New("Only an optional Parse app name is expected.")
}
if len(args) == 1 {
appName = args[0]
}
app, err := a.selectApp(e, appName)
if err != nil {
return err
}
var appConfig parsecli.AppConfig
switch e.Type {
case parsecli.LegacyParseFormat, parsecli.ParseFormat:
appConfig = parsecmd.GetParseAppConfig(app)
case parsecli.HerokuFormat:
_, appConfig, err = herokucmd.GetLinkedHerokuAppConfig(app, e)
if err != nil {
return err
}
}
return a.addSelectedApp(app.Name, appConfig, args, e)
}
func NewAddCmd(e *parsecli.Env) *cobra.Command {
a := &addCmd{
MakeDefault: false,
apps: &parsecli.Apps{},
verbose: true,
}
cmd := &cobra.Command{
Use: "add [app]",
Short: "Adds a new Parse App to config in current Cloud Code directory",
Long: `Adds a new Parse App to config in current Cloud Code directory.
If an argument is given, the added application can also be referenced by that name.`,
Run: parsecli.RunWithArgs(e, a.run),
}
cmd.Flags().BoolVarP(&a.MakeDefault, "default", "d", a.MakeDefault,
"Make the selected app default")
return cmd
}