-
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathcreate.go
More file actions
157 lines (131 loc) · 3.94 KB
/
create.go
File metadata and controls
157 lines (131 loc) · 3.94 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package project
import (
"fmt"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/ory/cli/cmd/cloudx/client"
"github.com/ory/x/cmdx"
"github.com/ory/x/flagx"
"github.com/ory/x/stringsx"
)
const (
nameFlag = "name"
createWorkspaceFlag = "create-workspace"
environmentFlag = "environment"
useProjectFlag = "use-project"
)
func NewCreateProjectCmd() *cobra.Command {
name := ""
createWorkspace := ""
environment := environmentValue("dev")
useProject := false
cmd := &cobra.Command{
Use: "project",
Short: "Create a new Ory Network project",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
h, err := client.NewCobraCommandHelper(cmd)
if err != nil {
return err
}
isQuiet := flagx.MustGetBool(cmd, cmdx.FlagQuiet)
wsID := h.WorkspaceID()
if wsID == nil && createWorkspace == "" && isQuiet {
return errors.New("no workspace found, you must specify the --workspace or --create-workspace flag to create a project when using --quiet")
}
if name == "" && isQuiet {
return errors.New("you must specify the --name flag when using --quiet")
}
for wsID == nil && createWorkspace == "" {
_, _ = fmt.Fprint(cmd.ErrOrStderr(), "It seems like you do not have a workspace yet.\nEnter a name for the workspace: ")
createWorkspace, err = h.Stdin.ReadString('\n')
if err != nil {
return errors.Wrap(err, "failed to read from stdin")
}
}
if createWorkspace != "" {
ws, err := h.CreateWorkspace(ctx, createWorkspace)
if err != nil {
return cmdx.PrintOpenAPIError(cmd, err)
}
wsID = new(ws.Id)
}
for name == "" {
_, _ = fmt.Fprint(cmd.ErrOrStderr(), "Enter a name for your project: ")
name, err = h.Stdin.ReadString('\n')
if err != nil {
return errors.Wrap(err, "failed to read from stdin")
}
}
p, err := h.CreateProject(ctx, name, string(environment), wsID, useProject)
if err != nil {
return cmdx.PrintOpenAPIError(cmd, err)
}
_, _ = fmt.Fprintln(h.VerboseErrWriter, "Project created successfully!")
cmdx.PrintRow(cmd, (*outputProject)(p))
return nil
},
}
cmd.Flags().StringVarP(&name, nameFlag, "n", "", "The name of the project, required when quiet mode is used")
cmd.Flags().VarP(&environment, environmentFlag, "e", "The environment of the project. Valid values are: prod, stage, dev")
cmd.Flags().BoolVar(&useProject, useProjectFlag, false, "Set the created project as the default")
cmd.Flags().StringVar(&createWorkspace, createWorkspaceFlag, "", "Create a new workspace with the given name and use it for the project")
client.RegisterWorkspaceFlag(cmd.Flags())
cmdx.RegisterFormatFlags(cmd.Flags())
return cmd
}
type environmentValue string
const (
EnvironmentProduction environmentValue = "prod"
EnvironmentStaging environmentValue = "stage"
EnvironmentDevelopment environmentValue = "dev"
)
var _ pflag.Value = (*environmentValue)(nil)
func (e *environmentValue) normalize() {
if e == nil {
return
}
switch *e {
case "production", "p":
*e = EnvironmentProduction
case "staging", "s":
*e = EnvironmentStaging
case "development", "d":
*e = EnvironmentDevelopment
}
}
func (e *environmentValue) valid() error {
if e == nil {
return errors.Errorf("environment value is nil")
}
switch c := stringsx.SwitchExact(string(*e)); {
case c.AddCase(string(EnvironmentProduction)),
c.AddCase(string(EnvironmentStaging)),
c.AddCase(string(EnvironmentDevelopment)):
return nil
default:
return c.ToUnknownCaseErr()
}
}
func (e *environmentValue) String() string {
if e == nil {
return ""
}
return string(*e)
}
func (e *environmentValue) Set(s string) error {
se := environmentValue(s)
se.normalize()
if err := se.valid(); err != nil {
return err
}
*e = se
return nil
}
func (e *environmentValue) Type() string {
return "environment"
}