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

     1  package deletecmd
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/olli-ai/jx/v2/pkg/cmd/create/options"
     8  
     9  	"github.com/olli-ai/jx/v2/pkg/cmd/helper"
    10  
    11  	"github.com/jenkins-x/jx-logging/pkg/log"
    12  	"github.com/olli-ai/jx/v2/pkg/cmd/opts"
    13  	"github.com/olli-ai/jx/v2/pkg/cmd/templates"
    14  	"github.com/olli-ai/jx/v2/pkg/util"
    15  	"github.com/spf13/cobra"
    16  )
    17  
    18  var (
    19  	deleteTokenAddonLong = templates.LongDesc(`
    20  		Deletes one or more API tokens for your addon from your local settings
    21  `)
    22  
    23  	deleteTokenAddonExample = templates.Examples(`
    24  		# Deletes an addon user token
    25  		jx delete token addon -n anchore myusername
    26  	`)
    27  )
    28  
    29  // DeleteTokenAddonOptions the options for the create spring command
    30  type DeleteTokenAddonOptions struct {
    31  	options.CreateOptions
    32  
    33  	ServerFlags opts.ServerFlags
    34  	Kind        string
    35  }
    36  
    37  // NewCmdDeleteTokenAddon defines the command
    38  func NewCmdDeleteTokenAddon(commonOpts *opts.CommonOptions) *cobra.Command {
    39  	options := &DeleteTokenAddonOptions{
    40  		CreateOptions: options.CreateOptions{
    41  			CommonOptions: commonOpts,
    42  		},
    43  	}
    44  
    45  	cmd := &cobra.Command{
    46  		Use:     "addon",
    47  		Short:   "Deletes one or more API tokens for a user on an issue addon server",
    48  		Long:    deleteTokenAddonLong,
    49  		Example: deleteTokenAddonExample,
    50  		Run: func(cmd *cobra.Command, args []string) {
    51  			options.Cmd = cmd
    52  			options.Args = args
    53  			err := options.Run()
    54  			helper.CheckErr(err)
    55  		},
    56  	}
    57  	options.ServerFlags.AddGitServerFlags(cmd)
    58  	cmd.Flags().StringVarP(&options.Kind, "kind", "k", "", "The kind of addon. Defaults to the addon name if not specified")
    59  	return cmd
    60  }
    61  
    62  // Run implements the command
    63  func (o *DeleteTokenAddonOptions) Run() error {
    64  	args := o.Args
    65  	if len(args) == 0 {
    66  		return fmt.Errorf("Missing addon user name")
    67  	}
    68  	kind := o.Kind
    69  	if kind == "" {
    70  		kind = o.ServerFlags.ServerName
    71  	}
    72  	if kind == "" {
    73  		kind = "addon"
    74  	}
    75  	authConfigSvc, err := o.AddonAuthConfigService(kind)
    76  	if err != nil {
    77  		return err
    78  	}
    79  	config := authConfigSvc.Config()
    80  
    81  	server, err := o.FindAddonServer(config, &o.ServerFlags, kind)
    82  	if err != nil {
    83  		return err
    84  	}
    85  	for _, username := range args {
    86  		err = server.DeleteUser(username)
    87  		if err != nil {
    88  			return err
    89  		}
    90  	}
    91  	err = authConfigSvc.SaveConfig()
    92  	if err != nil {
    93  		return err
    94  	}
    95  	log.Logger().Infof("Deleted API tokens for users: %s for addon server %s at %s from local settings",
    96  		util.ColorInfo(strings.Join(args, ", ")), util.ColorInfo(server.Name), util.ColorInfo(server.URL))
    97  	return nil
    98  }