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

     1  package deletecmd
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/olli-ai/jx/v2/pkg/cmd/helper"
     8  
     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/util"
    13  	"github.com/spf13/cobra"
    14  )
    15  
    16  var (
    17  	deleteTrackerServer_long = templates.LongDesc(`
    18  		Deletes one or more issue tracker servers from your local settings
    19  `)
    20  
    21  	deleteTrackerServer_example = templates.Examples(`
    22  		# Deletes an issue tracker server
    23  		jx delete tracker server MyProvider
    24  	`)
    25  )
    26  
    27  // DeleteTrackerServerOptions the options for the create spring command
    28  type DeleteTrackerServerOptions struct {
    29  	*opts.CommonOptions
    30  
    31  	IgnoreMissingServer bool
    32  }
    33  
    34  // NewCmdDeleteTrackerServer defines the command
    35  func NewCmdDeleteTrackerServer(commonOpts *opts.CommonOptions) *cobra.Command {
    36  	options := &DeleteTrackerServerOptions{
    37  		CommonOptions: commonOpts,
    38  	}
    39  
    40  	cmd := &cobra.Command{
    41  		Use:     "server",
    42  		Short:   "Deletes one or more issue tracker server(s)",
    43  		Long:    deleteTrackerServer_long,
    44  		Example: deleteTrackerServer_example,
    45  		Run: func(cmd *cobra.Command, args []string) {
    46  			options.Cmd = cmd
    47  			options.Args = args
    48  			err := options.Run()
    49  			helper.CheckErr(err)
    50  		},
    51  	}
    52  	cmd.Flags().BoolVarP(&options.IgnoreMissingServer, "ignore-missing", "i", false, "Silently ignore attempts to remove an issue tracker server name that does not exist")
    53  	return cmd
    54  }
    55  
    56  // Run implements the command
    57  func (o *DeleteTrackerServerOptions) Run() error {
    58  	args := o.Args
    59  	if len(args) == 0 {
    60  		return fmt.Errorf("Missing issue tracker server name argument")
    61  	}
    62  	authConfigSvc, err := o.CreateIssueTrackerAuthConfigService("")
    63  	if err != nil {
    64  		return err
    65  	}
    66  	config := authConfigSvc.Config()
    67  
    68  	serverNames := config.GetServerNames()
    69  	for _, arg := range args {
    70  		idx := config.IndexOfServerName(arg)
    71  		if idx < 0 {
    72  			if o.IgnoreMissingServer {
    73  				return nil
    74  			}
    75  			return util.InvalidArg(arg, serverNames)
    76  		}
    77  		config.Servers = append(config.Servers[0:idx], config.Servers[idx+1:]...)
    78  	}
    79  	err = authConfigSvc.SaveConfig()
    80  	if err != nil {
    81  		return err
    82  	}
    83  	log.Logger().Infof("Deleted issue tracker servers: %s from local settings", util.ColorInfo(strings.Join(args, ", ")))
    84  	return nil
    85  }