Merge pull request #8081 from rajhawaldar/SupportStandardOutputFormatFlags

Support standard output format flags for secret and deploy-key list
This commit is contained in:
Andy Feller 2023-10-02 13:21:47 -04:00 committed by GitHub
commit bd072c5e83
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 10 deletions

View file

@ -13,9 +13,9 @@ import (
)
type deployKey struct {
ID int
Key string
Title string
ID int `json:"id"`
Key string `json:"key"`
Title string `json:"title"`
CreatedAt time.Time `json:"created_at"`
ReadOnly bool `json:"read_only"`
}

View file

@ -18,6 +18,15 @@ type ListOptions struct {
IO *iostreams.IOStreams
HTTPClient func() (*http.Client, error)
BaseRepo func() (ghrepo.Interface, error)
Exporter cmdutil.Exporter
}
var deployKeyFields = []string{
"id",
"key",
"title",
"createdAt",
"readOnly",
}
func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Command {
@ -40,7 +49,7 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
return listRun(opts)
},
}
cmdutil.AddJSONFlags(cmd, &opts.Exporter, deployKeyFields)
return cmd
}
@ -64,6 +73,10 @@ func listRun(opts *ListOptions) error {
return cmdutil.NewNoResultsError(fmt.Sprintf("no deploy keys found in %s", ghrepo.FullName(repo)))
}
if opts.Exporter != nil {
return opts.Exporter.Write(opts.IO, deployKeys)
}
//nolint:staticcheck // SA1019: utils.NewTablePrinter is deprecated: use internal/tableprinter
t := utils.NewTablePrinter(opts.IO)
cs := opts.IO.ColorScheme()

View file

@ -23,6 +23,7 @@ type ListOptions struct {
Config func() (config.Config, error)
BaseRepo func() (ghrepo.Interface, error)
Now func() time.Time
Exporter cmdutil.Exporter
OrgName string
EnvName string
@ -30,6 +31,14 @@ type ListOptions struct {
Application string
}
var secretFields = []string{
"selectedReposURL",
"name",
"visibility",
"updatedAt",
"numSelectedRepos",
}
func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Command {
opts := &ListOptions{
IO: f.IOStreams,
@ -70,7 +79,7 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
cmd.Flags().StringVarP(&opts.EnvName, "env", "e", "", "List secrets for an environment")
cmd.Flags().BoolVarP(&opts.UserSecrets, "user", "u", false, "List a secret for your user")
cmdutil.StringEnumFlag(cmd, &opts.Application, "app", "a", "", []string{shared.Actions, shared.Codespaces, shared.Dependabot}, "List secrets for a specific application")
cmdutil.AddJSONFlags(cmd, &opts.Exporter, secretFields)
return cmd
}
@ -145,6 +154,10 @@ func listRun(opts *ListOptions) error {
fmt.Fprintf(opts.IO.ErrOut, "failed to start pager: %v\n", err)
}
if opts.Exporter != nil {
return opts.Exporter.Write(opts.IO, secrets)
}
table := tableprinter.New(opts.IO)
if secretEntity == shared.Organization || secretEntity == shared.User {
table.HeaderRow("Name", "Updated", "Visibility")
@ -173,11 +186,11 @@ func listRun(opts *ListOptions) error {
}
type Secret struct {
Name string
UpdatedAt time.Time `json:"updated_at"`
Visibility shared.Visibility
SelectedReposURL string `json:"selected_repositories_url"`
NumSelectedRepos int
Name string `json:"name"`
UpdatedAt time.Time `json:"updated_at"`
Visibility shared.Visibility `json:"visibility"`
SelectedReposURL string `json:"selected_repositories_url"`
NumSelectedRepos int `json:"num_selected_repos"`
}
func fmtVisibility(s Secret) string {