/
create.go
67 lines (49 loc) · 1.47 KB
/
create.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
package action
import (
"errors"
"bunnyshell.com/cli/pkg/api"
"bunnyshell.com/cli/pkg/api/project"
"bunnyshell.com/cli/pkg/config"
"bunnyshell.com/cli/pkg/lib"
"bunnyshell.com/cli/pkg/util"
"github.com/spf13/cobra"
)
func init() {
options := config.GetOptions()
settings := config.GetSettings()
createOptions := project.NewCreateOptions()
command := &cobra.Command{
Use: "create",
ValidArgsFunction: cobra.NoFileCompletions,
RunE: func(cmd *cobra.Command, args []string) error {
createOptions.Organization = settings.Profile.Context.Organization
model, err := project.Create(createOptions)
if err != nil {
var apiError api.Error
if errors.As(err, &apiError) {
return handleCreateErrors(cmd, apiError, createOptions)
}
return lib.FormatCommandError(cmd, err)
}
return lib.FormatCommandData(cmd, model)
},
}
flags := command.Flags()
flags.AddFlag(options.Organization.AddFlagWithExtraHelp(
"organization",
"Organization for the project",
"Organizations contain projects with their build settings and project variables",
util.FlagRequired,
))
createOptions.UpdateFlagSet(flags)
mainCmd.AddCommand(command)
}
func handleCreateErrors(cmd *cobra.Command, apiError api.Error, createOptions *project.CreateOptions) error {
if len(apiError.Violations) == 0 {
return apiError
}
for _, violation := range apiError.Violations {
cmd.Printf("Problem with creation: %s\n", violation.GetMessage())
}
return lib.ErrGeneric
}