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

     1  package get
     2  
     3  import (
     4  	"github.com/olli-ai/jx/v2/pkg/auth"
     5  	"github.com/olli-ai/jx/v2/pkg/cmd/helper"
     6  	"github.com/olli-ai/jx/v2/pkg/cmd/opts"
     7  	"github.com/spf13/cobra"
     8  )
     9  
    10  // GetTokenOptions the command line options
    11  type GetTokenOptions struct {
    12  	Options
    13  
    14  	Kind string
    15  	Name string
    16  }
    17  
    18  // NewCmdGetToken creates the command
    19  func NewCmdGetToken(commonOpts *opts.CommonOptions) *cobra.Command {
    20  	options := &GetTokenOptions{
    21  		Options: Options{
    22  			CommonOptions: commonOpts,
    23  		},
    24  	}
    25  
    26  	cmd := &cobra.Command{
    27  		Use:     "token",
    28  		Short:   "Display the tokens for different kinds of services",
    29  		Aliases: []string{"api-token"},
    30  		Run: func(cmd *cobra.Command, args []string) {
    31  			options.Cmd = cmd
    32  			options.Args = args
    33  			err := options.Run()
    34  			helper.CheckErr(err)
    35  		},
    36  	}
    37  	cmd.AddCommand(NewCmdGetTokenAddon(commonOpts))
    38  	return cmd
    39  }
    40  
    41  func (o *GetTokenOptions) addFlags(cmd *cobra.Command) {
    42  	cmd.Flags().StringVarP(&o.Kind, "kind", "k", "", "Filters the services by the kind")
    43  	cmd.Flags().StringVarP(&o.Name, "name", "n", "", "Filters the services by the name")
    44  }
    45  
    46  // Run implements this command
    47  func (o *GetTokenOptions) Run() error {
    48  	return o.Cmd.Help()
    49  }
    50  
    51  func (o *GetTokenOptions) displayUsersWithTokens(authConfigSvc auth.ConfigService) error {
    52  	config := authConfigSvc.Config()
    53  
    54  	filterKind := o.Kind
    55  	filterName := o.Name
    56  
    57  	table := o.CreateTable()
    58  	table.AddRow("KIND", "NAME", "URL", "USERNAME", "TOKEN?")
    59  
    60  	for _, s := range config.Servers {
    61  		kind := s.Kind
    62  		name := s.Name
    63  		if (filterKind == "" || filterKind == kind) && (filterName == "" || filterName == name) {
    64  			user := ""
    65  			pwd := ""
    66  			if len(s.Users) == 0 {
    67  				table.AddRow(kind, name, kind, s.URL, user, pwd)
    68  			} else {
    69  				for _, u := range s.Users {
    70  					user = u.Username
    71  					pwd = ""
    72  					if u.ApiToken != "" {
    73  						pwd = "yes"
    74  					}
    75  				}
    76  				table.AddRow(kind, name, s.URL, user, pwd)
    77  			}
    78  		}
    79  	}
    80  	table.Render()
    81  	return nil
    82  }