github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/cmd/workflow/list/list.go (about)

     1  package list
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	"github.com/ungtb10d/cli/v2/api"
     8  	"github.com/ungtb10d/cli/v2/internal/ghrepo"
     9  	"github.com/ungtb10d/cli/v2/pkg/cmd/workflow/shared"
    10  	"github.com/ungtb10d/cli/v2/pkg/cmdutil"
    11  	"github.com/ungtb10d/cli/v2/pkg/iostreams"
    12  	"github.com/ungtb10d/cli/v2/utils"
    13  	"github.com/spf13/cobra"
    14  )
    15  
    16  const defaultLimit = 50
    17  
    18  type ListOptions struct {
    19  	IO         *iostreams.IOStreams
    20  	HttpClient func() (*http.Client, error)
    21  	BaseRepo   func() (ghrepo.Interface, error)
    22  
    23  	PlainOutput bool
    24  
    25  	All   bool
    26  	Limit int
    27  }
    28  
    29  func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Command {
    30  	opts := &ListOptions{
    31  		IO:         f.IOStreams,
    32  		HttpClient: f.HttpClient,
    33  	}
    34  
    35  	cmd := &cobra.Command{
    36  		Use:     "list",
    37  		Short:   "List workflows",
    38  		Long:    "List workflow files, hiding disabled workflows by default.",
    39  		Aliases: []string{"ls"},
    40  		Args:    cobra.NoArgs,
    41  		RunE: func(cmd *cobra.Command, args []string) error {
    42  			// support `-R, --repo` override
    43  			opts.BaseRepo = f.BaseRepo
    44  
    45  			terminal := opts.IO.IsStdoutTTY() && opts.IO.IsStdinTTY()
    46  			opts.PlainOutput = !terminal
    47  
    48  			if opts.Limit < 1 {
    49  				return cmdutil.FlagErrorf("invalid limit: %v", opts.Limit)
    50  			}
    51  
    52  			if runF != nil {
    53  				return runF(opts)
    54  			}
    55  
    56  			return listRun(opts)
    57  		},
    58  	}
    59  
    60  	cmd.Flags().IntVarP(&opts.Limit, "limit", "L", defaultLimit, "Maximum number of workflows to fetch")
    61  	cmd.Flags().BoolVarP(&opts.All, "all", "a", false, "Show all workflows, including disabled workflows")
    62  
    63  	return cmd
    64  }
    65  
    66  func listRun(opts *ListOptions) error {
    67  	repo, err := opts.BaseRepo()
    68  	if err != nil {
    69  		return fmt.Errorf("could not determine base repo: %w", err)
    70  	}
    71  
    72  	httpClient, err := opts.HttpClient()
    73  	if err != nil {
    74  		return fmt.Errorf("could not create http client: %w", err)
    75  	}
    76  	client := api.NewClientFromHTTP(httpClient)
    77  
    78  	opts.IO.StartProgressIndicator()
    79  	workflows, err := shared.GetWorkflows(client, repo, opts.Limit)
    80  	opts.IO.StopProgressIndicator()
    81  	if err != nil {
    82  		return fmt.Errorf("could not get workflows: %w", err)
    83  	}
    84  
    85  	if len(workflows) == 0 {
    86  		return cmdutil.NewNoResultsError("no workflows found")
    87  	}
    88  
    89  	if err := opts.IO.StartPager(); err == nil {
    90  		defer opts.IO.StopPager()
    91  	} else {
    92  		fmt.Fprintf(opts.IO.ErrOut, "failed to start pager: %v\n", err)
    93  	}
    94  
    95  	//nolint:staticcheck // SA1019: utils.NewTablePrinter is deprecated: use internal/tableprinter
    96  	tp := utils.NewTablePrinter(opts.IO)
    97  	cs := opts.IO.ColorScheme()
    98  
    99  	for _, workflow := range workflows {
   100  		if workflow.Disabled() && !opts.All {
   101  			continue
   102  		}
   103  		tp.AddField(workflow.Name, nil, cs.Bold)
   104  		tp.AddField(string(workflow.State), nil, nil)
   105  		tp.AddField(fmt.Sprintf("%d", workflow.ID), nil, cs.Cyan)
   106  		tp.EndRow()
   107  	}
   108  
   109  	return tp.Render()
   110  }