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

     1  package deletecmd
     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  	"strings"
    11  
    12  	"github.com/jenkins-x/jx-logging/pkg/log"
    13  	"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
    14  	"github.com/jenkins-x/jx/v2/pkg/cmd/templates"
    15  	"github.com/jenkins-x/jx/v2/pkg/util"
    16  	"github.com/spf13/cobra"
    17  )
    18  
    19  var (
    20  	deleteChatTokenLong = templates.LongDesc(`
    21  		Deletes one or more API tokens for your chat server from your local settings
    22  `)
    23  
    24  	deleteChatTokenExample = templates.Examples(`
    25  		# Deletes a chat user token
    26  		jx delete chat token -n slack myusername
    27  	`)
    28  )
    29  
    30  // DeleteChatTokenOptions the options for the create spring command
    31  type DeleteChatTokenOptions struct {
    32  	options.CreateOptions
    33  
    34  	ServerFlags opts.ServerFlags
    35  }
    36  
    37  // NewCmdDeleteChatToken defines the command
    38  func NewCmdDeleteChatToken(commonOpts *opts.CommonOptions) *cobra.Command {
    39  	options := &DeleteChatTokenOptions{
    40  		CreateOptions: options.CreateOptions{
    41  			CommonOptions: commonOpts,
    42  		},
    43  	}
    44  
    45  	cmd := &cobra.Command{
    46  		Use:     "token",
    47  		Short:   "Deletes one or more API tokens for a user on a chat server",
    48  		Aliases: []string{"api-token"},
    49  		Long:    deleteChatTokenLong,
    50  		Example: deleteChatTokenExample,
    51  		Run: func(cmd *cobra.Command, args []string) {
    52  			options.Cmd = cmd
    53  			options.Args = args
    54  			err := options.Run()
    55  			helper.CheckErr(err)
    56  		},
    57  	}
    58  	options.ServerFlags.AddGitServerFlags(cmd)
    59  	return cmd
    60  }
    61  
    62  // Run implements the command
    63  func (o *DeleteChatTokenOptions) Run() error {
    64  	args := o.Args
    65  	if len(args) == 0 {
    66  		return fmt.Errorf("Missing chat server user name")
    67  	}
    68  	authConfigSvc, err := o.CreateChatAuthConfigService("")
    69  	if err != nil {
    70  		return err
    71  	}
    72  	config := authConfigSvc.Config()
    73  
    74  	server, err := o.FindChatServer(config, &o.ServerFlags)
    75  	if err != nil {
    76  		return err
    77  	}
    78  	for _, username := range args {
    79  		err = server.DeleteUser(username)
    80  		if err != nil {
    81  			return err
    82  		}
    83  	}
    84  	err = authConfigSvc.SaveConfig()
    85  	if err != nil {
    86  		return err
    87  	}
    88  	log.Logger().Infof("Deleted API tokens for users: %s for chat server server %s at %s from local settings",
    89  		util.ColorInfo(strings.Join(args, ", ")), util.ColorInfo(server.Name), util.ColorInfo(server.URL))
    90  	return nil
    91  }