-
Notifications
You must be signed in to change notification settings - Fork 786
/
create_addon_gitea.go
195 lines (177 loc) · 5.4 KB
/
create_addon_gitea.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package cmd
import (
"io"
"strings"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"gopkg.in/AlecAivazis/survey.v1"
"gopkg.in/AlecAivazis/survey.v1/terminal"
)
const (
optionChart = "chart"
optionRelease = "release"
defaultGiteaReleaseName = "gitea"
defaultGiteaVersion = ""
)
var (
create_addon_gitea_long = templates.LongDesc(`
Creates the Gitea addon (hosted Git server)
`)
create_addon_gitea_example = templates.Examples(`
# Create the Gitea addon
jx create addon gitea
`)
)
// CreateAddonGiteaOptions the options for the create spring command
type CreateAddonGiteaOptions struct {
CreateAddonOptions
Chart string
Username string
Password string
Email string
IsAdmin bool
NoUser bool
NoToken bool
}
// NewCmdCreateAddonGitea creates a command object for the "create" command
func NewCmdCreateAddonGitea(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &CreateAddonGiteaOptions{
CreateAddonOptions: CreateAddonOptions{
CreateOptions: CreateOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
},
},
}
cmd := &cobra.Command{
Use: "gitea",
Short: "Create a Gitea addon for hosting Git repositories",
Aliases: []string{"env"},
Long: create_addon_gitea_long,
Example: create_addon_gitea_example,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
options.addCommonFlags(cmd)
options.addFlags(cmd, "", defaultGiteaReleaseName, defaultGiteaVersion)
cmd.Flags().StringVarP(&options.Username, "username", "u", "", "The name for the user to create in Gitea. Note that Gitea tends to reject 'admin'")
cmd.Flags().StringVarP(&options.Password, "password", "p", "", "The password for the user to create in Gitea. Note that Gitea tends to reject passwords less than 6 characters")
cmd.Flags().StringVarP(&options.Email, "email", "e", "", "The email address of the new user to create in Gitea")
cmd.Flags().StringVarP(&options.Chart, optionChart, "c", kube.ChartGitea, "The name of the chart to use")
cmd.Flags().BoolVarP(&options.IsAdmin, "admin", "", false, "Should the new user created be an admin of the Gitea server")
cmd.Flags().BoolVarP(&options.NoUser, "no-user", "", false, "If true disable trying to create a new user in the Gitea server")
cmd.Flags().BoolVarP(&options.NoToken, "no-token", "", false, "If true disable trying to create a new token in the Gitea server")
return cmd
}
// Run implements the command
func (o *CreateAddonGiteaOptions) Run() error {
surveyOpts := survey.WithStdio(o.In, o.Out, o.Err)
if o.ReleaseName == "" {
return util.MissingOption(optionRelease)
}
if o.Chart == "" {
return util.MissingOption(optionChart)
}
err := o.ensureHelm()
if err != nil {
return errors.Wrap(err, "failed to ensure that helm is present")
}
setValues := strings.Split(o.SetValues, ",")
err = o.installChart(o.ReleaseName, o.Chart, o.Version, o.Namespace, true, setValues, nil, "")
if err != nil {
return err
}
err = o.createGitServer()
if err != nil {
return err
}
if !o.NoUser {
// now to add the Git server + a user
if !o.BatchMode {
if o.Username == "" {
prompt := &survey.Input{
Message: "Enter the user name to create in Gitea: ",
}
err = survey.AskOne(prompt, &o.Username, nil, surveyOpts)
if err != nil {
return err
}
}
if o.Username != "" {
if o.Password == "" {
prompt := &survey.Password{
Message: "Enter the password for the new user in Gitea: ",
}
err = survey.AskOne(prompt, &o.Password, nil, surveyOpts)
if err != nil {
return err
}
}
if o.Password != "" {
if o.Email == "" {
prompt := &survey.Input{
Message: "Enter the email address of the user to create in Gitea: ",
}
err = survey.AskOne(prompt, &o.Email, nil, surveyOpts)
if err != nil {
return err
}
}
}
}
}
if o.Username != "" && o.Password != "" && o.Email != "" {
err = o.createGitUser()
if err != nil {
return err
}
}
}
if !o.NoUser && o.Username != "" && o.Password != "" {
return o.createGitToken()
}
return nil
}
func (o *CreateAddonGiteaOptions) createGitServer() error {
options := &CreateGitServerOptions{
CreateOptions: o.CreateOptions,
}
options.Args = []string{"gitea"}
return options.Run()
}
func (o *CreateAddonGiteaOptions) createGitUser() error {
log.Infof("Generating user: %s with email: %s\n", util.ColorInfo(o.Username), util.ColorInfo(o.Email))
options := &CreateGitUserOptions{
CreateOptions: o.CreateOptions,
Username: o.Username,
Password: o.Password,
Email: o.Email,
IsAdmin: o.IsAdmin,
}
options.CommonOptions.Args = []string{}
options.ServerFlags.ServerName = "gitea"
return options.Run()
}
func (o *CreateAddonGiteaOptions) createGitToken() error {
log.Infof("Generating token for user %s with email %s\n", util.ColorInfo(o.Username), util.ColorInfo(o.Email))
options := &CreateGitTokenOptions{
CreateOptions: o.CreateOptions,
Username: o.Username,
Password: o.Password,
}
options.CommonOptions.Args = []string{}
options.ServerFlags.ServerName = "gitea"
return options.Run()
}