github.com/jenkins-x/jx/v2@v2.1.155/pkg/cmd/deletecmd/delete_git_server.go (about)

     1  package deletecmd
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
     8  
     9  	"github.com/jenkins-x/jx-logging/pkg/log"
    10  	"github.com/jenkins-x/jx/v2/pkg/auth"
    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  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    16  )
    17  
    18  var (
    19  	deleteGitServerLong = templates.LongDesc(`
    20  		Deletes one or more Git servers from your local settings
    21  `)
    22  
    23  	deleteGitServerExample = templates.Examples(`
    24  		# Deletes a Git provider
    25  		jx delete git server MyProvider
    26  	`)
    27  )
    28  
    29  // DeleteGitServerOptions the options for the create spring command
    30  type DeleteGitServerOptions struct {
    31  	*opts.CommonOptions
    32  
    33  	IgnoreMissingServer bool
    34  }
    35  
    36  // NewCmdDeleteGitServer defines the command
    37  func NewCmdDeleteGitServer(commonOpts *opts.CommonOptions) *cobra.Command {
    38  	options := &DeleteGitServerOptions{
    39  		CommonOptions: commonOpts,
    40  	}
    41  
    42  	cmd := &cobra.Command{
    43  		Use:     "server",
    44  		Short:   "Deletes one or more Git servers",
    45  		Long:    deleteGitServerLong,
    46  		Example: deleteGitServerExample,
    47  		Run: func(cmd *cobra.Command, args []string) {
    48  			options.Cmd = cmd
    49  			options.Args = args
    50  			err := options.Run()
    51  			helper.CheckErr(err)
    52  		},
    53  	}
    54  	cmd.Flags().BoolVarP(&options.IgnoreMissingServer, "ignore-missing", "i", false, "Silently ignore attempts to remove a Git server name that does not exist")
    55  	return cmd
    56  }
    57  
    58  // Run implements the command
    59  func (o *DeleteGitServerOptions) Run() error {
    60  	args := o.Args
    61  	if len(args) == 0 {
    62  		return fmt.Errorf("Missing Git server name argument")
    63  	}
    64  	authConfigSvc, err := o.GitAuthConfigService()
    65  	if err != nil {
    66  		return err
    67  	}
    68  	config := authConfigSvc.Config()
    69  	if config == nil {
    70  		return fmt.Errorf("empty git auth config")
    71  	}
    72  
    73  	serverNames := config.GetServerNames()
    74  	for _, arg := range args {
    75  		idx := config.IndexOfServerName(arg)
    76  		if idx < 0 {
    77  			if o.IgnoreMissingServer {
    78  				return nil
    79  			}
    80  			return util.InvalidArg(arg, serverNames)
    81  		}
    82  		server := config.Servers[idx]
    83  		if server != nil {
    84  			err = o.deleteServerResources(server)
    85  			if err != nil {
    86  				return err
    87  			}
    88  		}
    89  		config.Servers = append(config.Servers[0:idx], config.Servers[idx+1:]...)
    90  	}
    91  	err = authConfigSvc.SaveConfig()
    92  	if err != nil {
    93  		return err
    94  	}
    95  	log.Logger().Infof("Deleted Git servers: %s from local settings", util.ColorInfo(strings.Join(args, ", ")))
    96  	return nil
    97  }
    98  
    99  func (o *DeleteGitServerOptions) deleteServerResources(server *auth.AuthServer) error {
   100  	jxClient, ns, err := o.JXClientAndDevNamespace()
   101  	if err != nil {
   102  		return err
   103  	}
   104  	gitServiceResources := jxClient.JenkinsV1().GitServices(ns)
   105  	gitServices, err := gitServiceResources.List(metav1.ListOptions{})
   106  	if err != nil {
   107  		return err
   108  	}
   109  	for _, gitService := range gitServices.Items {
   110  		if gitService.Spec.URL == server.URL {
   111  			name := gitService.Name
   112  			log.Logger().Infof("Deleting GitService %s", util.ColorInfo(name))
   113  			err = gitServiceResources.Delete(name, nil)
   114  			if err != nil {
   115  				return err
   116  			}
   117  		}
   118  	}
   119  	return nil
   120  }