github.com/jenkins-x/jx/v2@v2.1.155/pkg/cmd/create/create_tracker_server.go (about) 1 package create 2 3 import ( 4 "fmt" 5 6 "github.com/jenkins-x/jx/v2/pkg/cmd/create/options" 7 8 "github.com/jenkins-x/jx/v2/pkg/cmd/helper" 9 10 "github.com/jenkins-x/jx-logging/pkg/log" 11 "github.com/jenkins-x/jx/v2/pkg/cmd/opts" 12 "github.com/jenkins-x/jx/v2/pkg/cmd/templates" 13 "github.com/jenkins-x/jx/v2/pkg/util" 14 "github.com/spf13/cobra" 15 ) 16 17 var ( 18 createTrackerServer_long = templates.LongDesc(` 19 Adds a new Issue Tracker Server URL 20 `) 21 22 createTrackerServer_example = templates.Examples(` 23 # Add a new issue tracker server URL 24 jx create tracker server jira myURL 25 `) 26 27 trackerKindToServiceName = map[string]string{ 28 "bitbucket": "bitbucket-bitbucket", 29 } 30 ) 31 32 // CreateTrackerServerOptions the options for the create spring command 33 type CreateTrackerServerOptions struct { 34 options.CreateOptions 35 36 Name string 37 } 38 39 // NewCmdCreateTrackerServer creates a command object for the "create" command 40 func NewCmdCreateTrackerServer(commonOpts *opts.CommonOptions) *cobra.Command { 41 options := &CreateTrackerServerOptions{ 42 CreateOptions: options.CreateOptions{ 43 CommonOptions: commonOpts, 44 }, 45 } 46 47 cmd := &cobra.Command{ 48 Use: "server kind [url]", 49 Short: "Creates a new issue tracker server URL", 50 Aliases: []string{"provider"}, 51 Long: createTrackerServer_long, 52 Example: createTrackerServer_example, 53 Run: func(cmd *cobra.Command, args []string) { 54 options.Cmd = cmd 55 options.Args = args 56 err := options.Run() 57 helper.CheckErr(err) 58 }, 59 } 60 61 cmd.Flags().StringVarP(&options.Name, "name", "n", "", "The name for the issue tracker server being created") 62 return cmd 63 } 64 65 // Run implements the command 66 func (o *CreateTrackerServerOptions) Run() error { 67 args := o.Args 68 if len(args) < 1 { 69 return missingTrackerArguments() 70 } 71 kind := args[0] 72 name := o.Name 73 if name == "" { 74 name = kind 75 } 76 gitUrl := "" 77 if len(args) > 1 { 78 gitUrl = args[1] 79 } else { 80 // lets try find the git URL based on the provider 81 serviceName := trackerKindToServiceName[kind] 82 if serviceName != "" { 83 url, err := o.FindService(serviceName) 84 if err != nil { 85 return fmt.Errorf("Failed to find %s issue tracker serivce %s: %s", kind, serviceName, err) 86 } 87 gitUrl = url 88 } 89 } 90 91 if gitUrl == "" { 92 return missingTrackerArguments() 93 } 94 authConfigSvc, err := o.CreateIssueTrackerAuthConfigService(kind) 95 if err != nil { 96 return err 97 } 98 config := authConfigSvc.Config() 99 config.GetOrCreateServerName(gitUrl, name, kind) 100 config.CurrentServer = gitUrl 101 err = authConfigSvc.SaveConfig() 102 if err != nil { 103 return err 104 } 105 log.Logger().Infof("Added issue tracker server %s for URL %s", util.ColorInfo(name), util.ColorInfo(gitUrl)) 106 return nil 107 } 108 109 func missingTrackerArguments() error { 110 return fmt.Errorf("Missing tracker server URL arguments. Usage: jx create tracker server kind [url]") 111 }