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

     1  package get
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/olli-ai/jx/v2/pkg/cmd/helper"
     7  
     8  	"github.com/jenkins-x/jx-logging/pkg/log"
     9  	"github.com/olli-ai/jx/v2/pkg/issues"
    10  	"github.com/olli-ai/jx/v2/pkg/util"
    11  	"github.com/spf13/cobra"
    12  
    13  	"github.com/olli-ai/jx/v2/pkg/cmd/opts"
    14  	"github.com/olli-ai/jx/v2/pkg/cmd/templates"
    15  )
    16  
    17  // GetTrackerOptions the command line options
    18  type GetTrackerOptions struct {
    19  	Options
    20  
    21  	Kind string
    22  	Dir  string
    23  }
    24  
    25  var (
    26  	getTrackerLong = templates.LongDesc(`
    27  		Display the issue tracker server URLs.
    28  
    29  `)
    30  
    31  	getTrackerExample = templates.Examples(`
    32  		# List all registered issue tracker server URLs
    33  		jx get tracker
    34  	`)
    35  )
    36  
    37  // NewCmdGetTracker creates the command
    38  func NewCmdGetTracker(commonOpts *opts.CommonOptions) *cobra.Command {
    39  	options := &GetTrackerOptions{
    40  		Options: Options{
    41  			CommonOptions: commonOpts,
    42  		},
    43  	}
    44  
    45  	cmd := &cobra.Command{
    46  		Use:     "tracker [flags]",
    47  		Short:   "Display the current registered issue tracker service URLs",
    48  		Long:    getTrackerLong,
    49  		Example: getTrackerExample,
    50  		Aliases: []string{"issue-tracker"},
    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  	cmd.Flags().StringVarP(&options.Kind, "kind", "k", "", "Filters the issue trackers by the kinds: "+strings.Join(issues.IssueTrackerKinds, ", "))
    59  	return cmd
    60  }
    61  
    62  // Run implements this command
    63  func (o *GetTrackerOptions) Run() error {
    64  	authConfigSvc, err := o.CreateIssueTrackerAuthConfigService("")
    65  	if err != nil {
    66  		return err
    67  	}
    68  	config := authConfigSvc.Config()
    69  	if len(config.Servers) == 0 {
    70  		log.Logger().Infof("No issue trackers registered. To register a new issue tracker use: %s", util.ColorInfo("jx create tracker server"))
    71  		return nil
    72  	}
    73  
    74  	filterKind := o.Kind
    75  
    76  	table := o.CreateTable()
    77  	if filterKind == "" {
    78  		table.AddRow("Name", "Kind", "URL")
    79  	} else {
    80  		table.AddRow(strings.ToUpper(filterKind), "URL")
    81  	}
    82  
    83  	for _, s := range config.Servers {
    84  		kind := s.Kind
    85  		if filterKind == "" || filterKind == kind {
    86  			table.AddRow(s.Name, kind, s.URL)
    87  		} else if filterKind == kind {
    88  			table.AddRow(s.Name, s.URL)
    89  		}
    90  	}
    91  	table.Render()
    92  	return nil
    93  }