diff --git a/pkg/cmd/repo/deploy-key/list/http.go b/pkg/cmd/repo/deploy-key/list/http.go index 2be4436bc..4470cec04 100644 --- a/pkg/cmd/repo/deploy-key/list/http.go +++ b/pkg/cmd/repo/deploy-key/list/http.go @@ -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"` } diff --git a/pkg/cmd/repo/deploy-key/list/list.go b/pkg/cmd/repo/deploy-key/list/list.go index 9504b3725..537599fe5 100644 --- a/pkg/cmd/repo/deploy-key/list/list.go +++ b/pkg/cmd/repo/deploy-key/list/list.go @@ -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() diff --git a/pkg/cmd/secret/list/list.go b/pkg/cmd/secret/list/list.go index 965466b67..dae097fce 100644 --- a/pkg/cmd/secret/list/list.go +++ b/pkg/cmd/secret/list/list.go @@ -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 {