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

     1  package get
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/olli-ai/jx/v2/pkg/cmd/helper"
     7  	"github.com/olli-ai/jx/v2/pkg/quickstarts"
     8  
     9  	"github.com/spf13/cobra"
    10  
    11  	"github.com/olli-ai/jx/v2/pkg/cmd/opts"
    12  	"github.com/olli-ai/jx/v2/pkg/cmd/templates"
    13  )
    14  
    15  //GetQuickstartsOptions -  the command line options
    16  type GetQuickstartsOptions struct {
    17  	Options
    18  	GitHubOrganisations []string
    19  	Filter              quickstarts.QuickstartFilter
    20  	ShortFormat         bool
    21  	IgnoreTeam          bool
    22  }
    23  
    24  var (
    25  	getQuickstartsLong = templates.LongDesc(`
    26  		Display the available quickstarts
    27  
    28  `)
    29  
    30  	getQuickstartsExample = templates.Examples(`
    31  		# List all the available quickstarts
    32  		jx get quickstarts
    33  	`)
    34  )
    35  
    36  //NewCmdGetQuickstarts creates the command
    37  func NewCmdGetQuickstarts(commonOpts *opts.CommonOptions) *cobra.Command {
    38  	options := &GetQuickstartsOptions{
    39  		Options: Options{
    40  			CommonOptions: commonOpts,
    41  		},
    42  	}
    43  
    44  	cmd := &cobra.Command{
    45  		Use:     "quickstarts [flags]",
    46  		Short:   "Lists the available quickstarts",
    47  		Long:    getQuickstartsLong,
    48  		Example: getQuickstartsExample,
    49  		Aliases: []string{"quickstart", "qs"},
    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  
    58  	cmd.Flags().StringArrayVarP(&options.GitHubOrganisations, "organisations", "g", []string{}, "The GitHub organisations to query for quickstarts")
    59  	cmd.Flags().StringArrayVarP(&options.Filter.Tags, "tag", "t", []string{}, "The tags on the quickstarts to filter")
    60  	cmd.Flags().StringVarP(&options.Filter.Text, "filter", "f", "", "The text filter")
    61  	cmd.Flags().StringVarP(&options.Filter.Owner, "owner", "", "", "The owner to filter on")
    62  	cmd.Flags().StringVarP(&options.Filter.Language, "language", "l", "", "The language to filter on")
    63  	cmd.Flags().StringVarP(&options.Filter.Framework, "framework", "", "", "The framework to filter on")
    64  	cmd.Flags().BoolVarP(&options.Filter.AllowML, "machine-learning", "", false, "Allow machine-learning quickstarts in results")
    65  	cmd.Flags().BoolVarP(&options.ShortFormat, "short", "s", false, "return minimal details")
    66  	cmd.Flags().BoolVarP(&options.IgnoreTeam, "ignore-team", "", false, "ignores the quickstarts added to the Team Settings")
    67  
    68  	return cmd
    69  }
    70  
    71  // Run implements this command
    72  func (o *GetQuickstartsOptions) Run() error {
    73  	model, err := o.LoadQuickStartsModel(o.GitHubOrganisations, o.IgnoreTeam)
    74  	if err != nil {
    75  		return fmt.Errorf("failed to load quickstarts: %s", err)
    76  	}
    77  
    78  	//output list of available quickstarts and exit
    79  	filteredQuickstarts := model.Filter(&o.Filter)
    80  	table := o.CreateTable()
    81  	if o.ShortFormat {
    82  		table.AddRow("NAME")
    83  	} else {
    84  		table.AddRow("NAME", "OWNER", "VERSION", "LANGUAGE", "URL")
    85  	}
    86  
    87  	for _, qs := range filteredQuickstarts {
    88  		if o.ShortFormat {
    89  			table.AddRow(qs.Name)
    90  		} else {
    91  			table.AddRow(qs.Name, qs.Owner, qs.Version, qs.Language, qs.DownloadZipURL)
    92  		}
    93  	}
    94  	table.Render()
    95  	return nil
    96  }