github.com/cli/cli@v1.14.1-0.20210902173923-1af6a669e342/pkg/cmd/workflow/list/list.go (about)

     1  package list
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	"github.com/cli/cli/api"
     8  	"github.com/cli/cli/internal/ghrepo"
     9  	"github.com/cli/cli/pkg/cmd/workflow/shared"
    10  	"github.com/cli/cli/pkg/cmdutil"
    11  	"github.com/cli/cli/pkg/iostreams"
    12  	"github.com/cli/cli/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  		Args:  cobra.NoArgs,
    40  		RunE: func(cmd *cobra.Command, args []string) error {
    41  			// support `-R, --repo` override
    42  			opts.BaseRepo = f.BaseRepo
    43  
    44  			terminal := opts.IO.IsStdoutTTY() && opts.IO.IsStdinTTY()
    45  			opts.PlainOutput = !terminal
    46  
    47  			if opts.Limit < 1 {
    48  				return &cmdutil.FlagError{Err: fmt.Errorf("invalid limit: %v", opts.Limit)}
    49  			}
    50  
    51  			if runF != nil {
    52  				return runF(opts)
    53  			}
    54  
    55  			return listRun(opts)
    56  		},
    57  	}
    58  
    59  	cmd.Flags().IntVarP(&opts.Limit, "limit", "L", defaultLimit, "Maximum number of workflows to fetch")
    60  	cmd.Flags().BoolVarP(&opts.All, "all", "a", false, "Show all workflows, including disabled workflows")
    61  
    62  	return cmd
    63  }
    64  
    65  func listRun(opts *ListOptions) error {
    66  	repo, err := opts.BaseRepo()
    67  	if err != nil {
    68  		return fmt.Errorf("could not determine base repo: %w", err)
    69  	}
    70  
    71  	httpClient, err := opts.HttpClient()
    72  	if err != nil {
    73  		return fmt.Errorf("could not create http client: %w", err)
    74  	}
    75  	client := api.NewClientFromHTTP(httpClient)
    76  
    77  	opts.IO.StartProgressIndicator()
    78  	workflows, err := shared.GetWorkflows(client, repo, opts.Limit)
    79  	opts.IO.StopProgressIndicator()
    80  	if err != nil {
    81  		return fmt.Errorf("could not get workflows: %w", err)
    82  	}
    83  
    84  	if len(workflows) == 0 {
    85  		if !opts.PlainOutput {
    86  			fmt.Fprintln(opts.IO.ErrOut, "No workflows found")
    87  		}
    88  		return nil
    89  	}
    90  
    91  	tp := utils.NewTablePrinter(opts.IO)
    92  	cs := opts.IO.ColorScheme()
    93  
    94  	for _, workflow := range workflows {
    95  		if workflow.Disabled() && !opts.All {
    96  			continue
    97  		}
    98  		tp.AddField(workflow.Name, nil, cs.Bold)
    99  		tp.AddField(string(workflow.State), nil, nil)
   100  		tp.AddField(fmt.Sprintf("%d", workflow.ID), nil, cs.Cyan)
   101  		tp.EndRow()
   102  	}
   103  
   104  	return tp.Render()
   105  }