github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cmd/create/create_addon_gitea.go (about)

     1  package create
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/olli-ai/jx/v2/pkg/cmd/create/options"
     7  
     8  	"github.com/olli-ai/jx/v2/pkg/cmd/helper"
     9  
    10  	"github.com/olli-ai/jx/v2/pkg/helm"
    11  	survey "gopkg.in/AlecAivazis/survey.v1"
    12  
    13  	"github.com/pkg/errors"
    14  	"github.com/spf13/cobra"
    15  
    16  	"github.com/jenkins-x/jx-logging/pkg/log"
    17  	"github.com/olli-ai/jx/v2/pkg/cmd/opts"
    18  	"github.com/olli-ai/jx/v2/pkg/cmd/templates"
    19  	"github.com/olli-ai/jx/v2/pkg/kube"
    20  	"github.com/olli-ai/jx/v2/pkg/util"
    21  )
    22  
    23  const (
    24  	optionChart   = "chart"
    25  	optionRelease = "release"
    26  
    27  	defaultGiteaReleaseName = "gitea"
    28  	defaultGiteaVersion     = ""
    29  )
    30  
    31  var (
    32  	create_addon_gitea_long = templates.LongDesc(`
    33  		Creates the Gitea addon (hosted Git server)
    34  `)
    35  
    36  	create_addon_gitea_example = templates.Examples(`
    37  		# Create the Gitea addon 
    38  		jx create addon gitea
    39  	`)
    40  )
    41  
    42  // CreateAddonGiteaOptions the options for the create spring command
    43  type CreateAddonGiteaOptions struct {
    44  	CreateAddonOptions
    45  
    46  	Chart    string
    47  	Username string
    48  	Password string
    49  	Email    string
    50  	IsAdmin  bool
    51  	NoUser   bool
    52  	NoToken  bool
    53  }
    54  
    55  // NewCmdCreateAddonGitea creates a command object for the "create" command
    56  func NewCmdCreateAddonGitea(commonOpts *opts.CommonOptions) *cobra.Command {
    57  	options := &CreateAddonGiteaOptions{
    58  		CreateAddonOptions: CreateAddonOptions{
    59  			CreateOptions: options.CreateOptions{
    60  				CommonOptions: commonOpts,
    61  			},
    62  		},
    63  	}
    64  
    65  	cmd := &cobra.Command{
    66  		Use:     "gitea",
    67  		Short:   "Create a Gitea addon for hosting Git repositories",
    68  		Aliases: []string{"env"},
    69  		Long:    create_addon_gitea_long,
    70  		Example: create_addon_gitea_example,
    71  		Run: func(cmd *cobra.Command, args []string) {
    72  			options.Cmd = cmd
    73  			options.Args = args
    74  			err := options.Run()
    75  			helper.CheckErr(err)
    76  		},
    77  	}
    78  
    79  	options.addFlags(cmd, "", defaultGiteaReleaseName, defaultGiteaVersion)
    80  
    81  	cmd.Flags().StringVarP(&options.Username, "username", "u", "", "The name for the user to create in Gitea. Note that Gitea tends to reject 'admin'")
    82  	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")
    83  	cmd.Flags().StringVarP(&options.Email, "email", "e", "", "The email address of the new user to create in Gitea")
    84  	cmd.Flags().StringVarP(&options.Chart, optionChart, "c", kube.ChartGitea, "The name of the chart to use")
    85  	cmd.Flags().BoolVarP(&options.IsAdmin, "admin", "", false, "Should the new user created be an admin of the Gitea server")
    86  	cmd.Flags().BoolVarP(&options.NoUser, "no-user", "", false, "If true disable trying to create a new user in the Gitea server")
    87  	cmd.Flags().BoolVarP(&options.NoToken, "no-token", "", false, "If true disable trying to create a new token in the Gitea server")
    88  	return cmd
    89  }
    90  
    91  // Run implements the command
    92  func (o *CreateAddonGiteaOptions) Run() error {
    93  	surveyOpts := survey.WithStdio(o.In, o.Out, o.Err)
    94  	if o.ReleaseName == "" {
    95  		return util.MissingOption(optionRelease)
    96  	}
    97  	if o.Chart == "" {
    98  		return util.MissingOption(optionChart)
    99  	}
   100  
   101  	err := o.EnsureHelm()
   102  	if err != nil {
   103  		return errors.Wrap(err, "failed to ensure that helm is present")
   104  	}
   105  	setValues := strings.Split(o.SetValues, ",")
   106  	helmOptions := helm.InstallChartOptions{
   107  		Chart:       o.Chart,
   108  		ReleaseName: o.ReleaseName,
   109  		Version:     o.Version,
   110  		Ns:          o.Namespace,
   111  		SetValues:   setValues,
   112  	}
   113  	err = o.InstallChartWithOptions(helmOptions)
   114  	if err != nil {
   115  		return err
   116  	}
   117  	err = o.createGitServer()
   118  	if err != nil {
   119  		return err
   120  	}
   121  	if !o.NoUser {
   122  		// now to add the Git server + a user
   123  		if !o.BatchMode {
   124  			if o.Username == "" {
   125  				prompt := &survey.Input{
   126  					Message: "Enter the user name to create in Gitea: ",
   127  				}
   128  				err = survey.AskOne(prompt, &o.Username, nil, surveyOpts)
   129  				if err != nil {
   130  					return err
   131  				}
   132  			}
   133  			if o.Username != "" {
   134  				if o.Password == "" {
   135  					prompt := &survey.Password{
   136  						Message: "Enter the password for the new user in Gitea: ",
   137  					}
   138  					err = survey.AskOne(prompt, &o.Password, nil, surveyOpts)
   139  					if err != nil {
   140  						return err
   141  					}
   142  				}
   143  				if o.Password != "" {
   144  					if o.Email == "" {
   145  						prompt := &survey.Input{
   146  							Message: "Enter the email address of the user to create in Gitea: ",
   147  						}
   148  						err = survey.AskOne(prompt, &o.Email, nil, surveyOpts)
   149  						if err != nil {
   150  							return err
   151  						}
   152  					}
   153  				}
   154  			}
   155  		}
   156  		if o.Username != "" && o.Password != "" && o.Email != "" {
   157  			err = o.createGitUser()
   158  			if err != nil {
   159  				return err
   160  			}
   161  		}
   162  	}
   163  	if !o.NoUser && o.Username != "" && o.Password != "" {
   164  		return o.createGitToken()
   165  	}
   166  	return nil
   167  }
   168  
   169  func (o *CreateAddonGiteaOptions) createGitServer() error {
   170  	options := &CreateGitServerOptions{
   171  		CreateOptions: o.CreateOptions,
   172  	}
   173  	options.Args = []string{"gitea"}
   174  	return options.Run()
   175  }
   176  
   177  func (o *CreateAddonGiteaOptions) createGitUser() error {
   178  	log.Logger().Infof("Generating user: %s with email: %s", util.ColorInfo(o.Username), util.ColorInfo(o.Email))
   179  	options := &CreateGitUserOptions{
   180  		CreateOptions: o.CreateOptions,
   181  		Username:      o.Username,
   182  		Password:      o.Password,
   183  		Email:         o.Email,
   184  		IsAdmin:       o.IsAdmin,
   185  	}
   186  	options.CommonOptions.Args = []string{}
   187  	options.ServerFlags.ServerName = "gitea"
   188  	return options.Run()
   189  }
   190  
   191  func (o *CreateAddonGiteaOptions) createGitToken() error {
   192  	log.Logger().Infof("Generating token for user %s with email %s", util.ColorInfo(o.Username), util.ColorInfo(o.Email))
   193  	options := &CreateGitTokenOptions{
   194  		CreateOptions: o.CreateOptions,
   195  		Username:      o.Username,
   196  		Password:      o.Password,
   197  	}
   198  	options.CommonOptions.Args = []string{}
   199  	options.ServerFlags.ServerName = "gitea"
   200  	return options.Run()
   201  }