-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_addon_gitea.go
181 lines (166 loc) · 5.03 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
package cmd
import (
"github.com/spf13/cobra"
"io"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
cmdutil "github.com/jenkins-x/jx/pkg/jx/cmd/util"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/util"
"gopkg.in/AlecAivazis/survey.v1"
)
const (
optionChart = "chart"
optionRelease = "release"
)
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 cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command {
options := &CreateAddonGiteaOptions{
CreateAddonOptions: CreateAddonOptions{
CreateOptions: CreateOptions{
CommonOptions: CommonOptions{
Factory: f,
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()
cmdutil.CheckErr(err)
},
}
options.addCommonFlags(cmd)
options.addFlags(cmd)
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.Version, "version", "v", "", "The verison of the gitea addon to use")
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 {
if o.ReleaseName == "" {
return util.MissingOption(optionRelease)
}
if o.Chart == "" {
return util.MissingOption(optionChart)
}
err := o.installChart(o.ReleaseName, o.Chart, o.Version, o.Namespace, true)
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)
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)
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)
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 {
o.Printf("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 {
o.Printf("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()
}