From 965eabd503c6e43278101b13999496a43fb0f820 Mon Sep 17 00:00:00 2001 From: rajhawaldar Date: Mon, 25 Sep 2023 21:35:47 +0530 Subject: [PATCH 1/2] Support standard output format flags for secret and deploy-key list Signed-off-by: rajhawaldar --- pkg/cmd/repo/deploy-key/list/http.go | 6 +++--- pkg/cmd/repo/deploy-key/list/list.go | 15 ++++++++++++++- pkg/cmd/secret/list/list.go | 25 +++++++++++++++++++------ 3 files changed, 36 insertions(+), 10 deletions(-) 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..11623a679 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", + "created_at", + "read_only", } 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..b2b01834e 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{ + "selected_repos_url", + "name", + "visibility", + "updated_at", + "num_selected_repos", +} + 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 { From 79296c0a62bfaba134d92f775bd411f8147e7ba2 Mon Sep 17 00:00:00 2001 From: rajhawaldar Date: Fri, 29 Sep 2023 13:02:59 +0530 Subject: [PATCH 2/2] Use camelCase for fields Signed-off-by: rajhawaldar --- pkg/cmd/repo/deploy-key/list/list.go | 4 ++-- pkg/cmd/secret/list/list.go | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/cmd/repo/deploy-key/list/list.go b/pkg/cmd/repo/deploy-key/list/list.go index 11623a679..537599fe5 100644 --- a/pkg/cmd/repo/deploy-key/list/list.go +++ b/pkg/cmd/repo/deploy-key/list/list.go @@ -25,8 +25,8 @@ var deployKeyFields = []string{ "id", "key", "title", - "created_at", - "read_only", + "createdAt", + "readOnly", } func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Command { diff --git a/pkg/cmd/secret/list/list.go b/pkg/cmd/secret/list/list.go index b2b01834e..dae097fce 100644 --- a/pkg/cmd/secret/list/list.go +++ b/pkg/cmd/secret/list/list.go @@ -32,11 +32,11 @@ type ListOptions struct { } var secretFields = []string{ - "selected_repos_url", + "selectedReposURL", "name", "visibility", - "updated_at", - "num_selected_repos", + "updatedAt", + "numSelectedRepos", } func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Command {