github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cmd/deletecmd/delete_quickstart_location.go (about) 1 package deletecmd 2 3 import ( 4 "fmt" 5 6 "github.com/olli-ai/jx/v2/pkg/cmd/helper" 7 8 v1 "github.com/jenkins-x/jx-api/pkg/apis/jenkins.io/v1" 9 "github.com/jenkins-x/jx-logging/pkg/log" 10 "github.com/olli-ai/jx/v2/pkg/cmd/opts" 11 "github.com/olli-ai/jx/v2/pkg/cmd/templates" 12 "github.com/olli-ai/jx/v2/pkg/gits" 13 "github.com/olli-ai/jx/v2/pkg/kube" 14 "github.com/olli-ai/jx/v2/pkg/util" 15 "github.com/spf13/cobra" 16 ) 17 18 const ( 19 optionOwner = "owner" 20 optionGitUrl = "url" 21 ) 22 23 var ( 24 deleteQuickstartLocationLong = templates.LongDesc(` 25 Deletes one or more quickstart locations for your team 26 27 For more documentation see: [https://jenkins-x.io/developing/create-quickstart/#customising-your-teams-quickstarts](https://jenkins-x.io/developing/create-quickstart/#customising-your-teams-quickstarts) 28 29 `) 30 31 deleteQuickstartLocationExample = templates.Examples(` 32 # Pick a quickstart location to delete for your team 33 jx delete quickstartlocation 34 35 # Pick a quickstart location to delete for your team using an abbreviation 36 jx delete qsloc 37 38 # Delete a GitHub organisation 'myorg' for your team 39 jx delete qsloc --owner myorg 40 41 # Delete a specific location for your team 42 jx delete qsloc --url https://foo.com --owner myowner 43 44 `) 45 ) 46 47 // DeleteQuickstartLocationOptions the options for the create spring command 48 type DeleteQuickstartLocationOptions struct { 49 *opts.CommonOptions 50 51 GitUrl string 52 Owner string 53 } 54 55 // NewCmdDeleteQuickstartLocation defines the command 56 func NewCmdDeleteQuickstartLocation(commonOpts *opts.CommonOptions) *cobra.Command { 57 options := &DeleteQuickstartLocationOptions{ 58 CommonOptions: commonOpts, 59 } 60 61 cmd := &cobra.Command{ 62 Use: opts.QuickStartLocationCommandName, 63 Short: "Deletes one or more quickstart locations for your team", 64 Aliases: opts.QuickStartLocationCommandAliases, 65 Long: deleteQuickstartLocationLong, 66 Example: deleteQuickstartLocationExample, 67 Run: func(cmd *cobra.Command, args []string) { 68 options.Cmd = cmd 69 options.Args = args 70 err := options.Run() 71 helper.CheckErr(err) 72 }, 73 } 74 cmd.Flags().StringVarP(&options.GitUrl, optionGitUrl, "u", gits.GitHubURL, "The URL of the Git service") 75 cmd.Flags().StringVarP(&options.Owner, optionOwner, "o", "", "The owner is the user or organisation of the Git provider") 76 77 return cmd 78 } 79 80 // Run implements the command 81 func (o *DeleteQuickstartLocationOptions) Run() error { 82 jxClient, ns, err := o.JXClientAndDevNamespace() 83 if err != nil { 84 return err 85 } 86 87 locations, err := kube.GetQuickstartLocations(jxClient, ns) 88 if err != nil { 89 return err 90 } 91 92 if o.GitUrl == "" || o.Owner == "" { 93 if o.BatchMode { 94 if o.GitUrl == "" { 95 return util.MissingOption(optionGitUrl) 96 } 97 if o.Owner == "" { 98 return util.MissingOption(optionOwner) 99 } 100 } else { 101 // TODO lets pick the available combinations to delete 102 names := []string{} 103 m := map[string]v1.QuickStartLocation{} 104 for _, loc := range locations { 105 key := util.UrlJoin(loc.GitURL, loc.Owner) 106 m[key] = loc 107 names = append(names, key) 108 } 109 110 name, err := util.PickName(names, "Pick the quickstart git owner to remove from the team settings: ", "", o.GetIOFileHandles()) 111 if err != nil { 112 return err 113 } 114 if name == "" { 115 return fmt.Errorf("No owner name chosen") 116 } 117 loc := m[name] 118 o.Owner = loc.Owner 119 o.GitUrl = loc.GitURL 120 } 121 } 122 123 callback := func(env *v1.Environment) error { 124 settings := &env.Spec.TeamSettings 125 for i, l := range settings.QuickstartLocations { 126 if l.GitURL == o.GitUrl && l.Owner == o.Owner { 127 settings.QuickstartLocations = append(settings.QuickstartLocations[0:i], settings.QuickstartLocations[i+1:]...) 128 log.Logger().Infof("Removing quickstart git owner %s", util.ColorInfo(util.UrlJoin(l.GitURL, l.Owner))) 129 return nil 130 } 131 } 132 return fmt.Errorf("No quickstart location found for git URL: %s and owner: %s", o.GitUrl, o.Owner) 133 } 134 return o.ModifyDevEnvironment(callback) 135 }