From 2843d32af07539c00acdb01e1a11a360bd3cb813 Mon Sep 17 00:00:00 2001 From: meiji163 Date: Mon, 15 Nov 2021 14:27:30 -0800 Subject: [PATCH 1/5] init --- pkg/cmd/config/config.go | 2 + pkg/cmd/config/list/list.go | 73 +++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 pkg/cmd/config/list/list.go diff --git a/pkg/cmd/config/config.go b/pkg/cmd/config/config.go index c011ddcf0..2168516d3 100644 --- a/pkg/cmd/config/config.go +++ b/pkg/cmd/config/config.go @@ -6,6 +6,7 @@ import ( "github.com/cli/cli/v2/internal/config" cmdGet "github.com/cli/cli/v2/pkg/cmd/config/get" + cmdList "github.com/cli/cli/v2/pkg/cmd/config/list" cmdSet "github.com/cli/cli/v2/pkg/cmd/config/set" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/spf13/cobra" @@ -33,6 +34,7 @@ func NewCmdConfig(f *cmdutil.Factory) *cobra.Command { cmd.AddCommand(cmdGet.NewCmdConfigGet(f, nil)) cmd.AddCommand(cmdSet.NewCmdConfigSet(f, nil)) + cmd.AddCommand(cmdList.NewCmdConfigList(f, nil)) return cmd } diff --git a/pkg/cmd/config/list/list.go b/pkg/cmd/config/list/list.go new file mode 100644 index 000000000..0fa82c47d --- /dev/null +++ b/pkg/cmd/config/list/list.go @@ -0,0 +1,73 @@ +package list + +import ( + "fmt" + + "github.com/cli/cli/v2/internal/config" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/spf13/cobra" +) + +type ListOptions struct { + IO *iostreams.IOStreams + Config func() (config.Config, error) + + Hostname string +} + +func NewCmdConfigList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Command { + opts := &ListOptions{ + IO: f.IOStreams, + } + + cmd := &cobra.Command{ + Use: "list", + Short: "Print a list of configuration keys and values", + Args: cobra.ExactArgs(0), + RunE: func(cmd *cobra.Command, args []string) error { + opts.Config = f.Config + + if runF != nil { + return runF(opts) + } + + return listRun(opts) + }, + } + + cmd.Flags().StringVarP(&opts.Hostname, "host", "h", "", "Get per-host setting") + + return cmd +} + +func listRun(opts *ListOptions) error { + cfg, err := opts.Config() + if err != nil { + return err + } + + //cs := opts.IO.ColorScheme() + isTTY := opts.IO.IsStdoutTTY() + + host, err := cfg.DefaultHost() + if err != nil { + return err + } + + if isTTY { + fmt.Printf("Settings configured for %s\n\n", host) + } + + configOptions := config.ConfigOptions() + + for _, key := range configOptions { + val, err := cfg.Get(opts.Hostname, key.Key) + if err != nil { + return err + } + fmt.Printf("%s: %s\n", key.Key, val) + } + + return nil +} From f5bb6e84767df14c4a55558305ac93c3ea4082f0 Mon Sep 17 00:00:00 2001 From: meiji163 Date: Mon, 15 Nov 2021 16:46:50 -0800 Subject: [PATCH 2/5] fix host and add tests --- pkg/cmd/config/list/list.go | 29 +++++--- pkg/cmd/config/list/list_test.go | 119 +++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+), 10 deletions(-) create mode 100644 pkg/cmd/config/list/list_test.go diff --git a/pkg/cmd/config/list/list.go b/pkg/cmd/config/list/list.go index 0fa82c47d..ebfee64a9 100644 --- a/pkg/cmd/config/list/list.go +++ b/pkg/cmd/config/list/list.go @@ -18,7 +18,8 @@ type ListOptions struct { func NewCmdConfigList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Command { opts := &ListOptions{ - IO: f.IOStreams, + IO: f.IOStreams, + Config: f.Config, } cmd := &cobra.Command{ @@ -26,8 +27,6 @@ func NewCmdConfigList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra. Short: "Print a list of configuration keys and values", Args: cobra.ExactArgs(0), RunE: func(cmd *cobra.Command, args []string) error { - opts.Config = f.Config - if runF != nil { return runF(opts) } @@ -36,7 +35,7 @@ func NewCmdConfigList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra. }, } - cmd.Flags().StringVarP(&opts.Hostname, "host", "h", "", "Get per-host setting") + cmd.Flags().StringVarP(&opts.Hostname, "host", "h", "", "Get per-host configuration") return cmd } @@ -47,16 +46,21 @@ func listRun(opts *ListOptions) error { return err } - //cs := opts.IO.ColorScheme() + cs := opts.IO.ColorScheme() isTTY := opts.IO.IsStdoutTTY() - host, err := cfg.DefaultHost() - if err != nil { - return err + var host string + if opts.Hostname != "" { + host = opts.Hostname + } else { + host, err = cfg.DefaultHost() + if err != nil { + return err + } } if isTTY { - fmt.Printf("Settings configured for %s\n\n", host) + fmt.Fprintf(opts.IO.Out, cs.Grayf("Settings configured for %s\n\n", host)) } configOptions := config.ConfigOptions() @@ -66,7 +70,12 @@ func listRun(opts *ListOptions) error { if err != nil { return err } - fmt.Printf("%s: %s\n", key.Key, val) + configLine := fmt.Sprintf("%s: %s", key.Key, val) + if isTTY && key.DefaultValue != "" && val != key.DefaultValue { + configLine = cs.Bold(configLine) + configLine += fmt.Sprintf(" (default: %s)", key.DefaultValue) + } + fmt.Fprintln(opts.IO.Out, configLine) } return nil diff --git a/pkg/cmd/config/list/list_test.go b/pkg/cmd/config/list/list_test.go new file mode 100644 index 000000000..de83adfb2 --- /dev/null +++ b/pkg/cmd/config/list/list_test.go @@ -0,0 +1,119 @@ +package list + +import ( + "bytes" + "testing" + + "github.com/cli/cli/v2/internal/config" + "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/pkg/iostreams" + "github.com/google/shlex" + "github.com/stretchr/testify/assert" +) + +func TestNewCmdConfigList(t *testing.T) { + tests := []struct { + name string + input string + output ListOptions + wantsErr bool + }{ + { + name: "no arguments", + input: "", + output: ListOptions{}, + wantsErr: false, + }, + { + name: "list with host", + input: "--host HOST.com", + output: ListOptions{Hostname: "HOST.com"}, + wantsErr: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + f := &cmdutil.Factory{ + Config: func() (config.Config, error) { + return config.ConfigStub{}, nil + }, + } + + argv, err := shlex.Split(tt.input) + assert.NoError(t, err) + + var gotOpts *ListOptions + cmd := NewCmdConfigList(f, func(opts *ListOptions) error { + gotOpts = opts + return nil + }) + cmd.Flags().BoolP("help", "x", false, "") + + cmd.SetArgs(argv) + cmd.SetIn(&bytes.Buffer{}) + cmd.SetOut(&bytes.Buffer{}) + cmd.SetErr(&bytes.Buffer{}) + + _, err = cmd.ExecuteC() + if tt.wantsErr { + assert.Error(t, err) + return + } + + assert.NoError(t, err) + assert.Equal(t, tt.output.Hostname, gotOpts.Hostname) + }) + } +} + +func Test_listRun(t *testing.T) { + tests := []struct { + name string + input *ListOptions + config config.ConfigStub + isTTY bool + stdout string + wantErr bool + }{ + { + name: "list", + isTTY: true, + config: config.ConfigStub{ + "HOST:git_protocol": "ssh", + "HOST:editor": "/usr/bin/vim", + "HOST:prompt": "disabled", + "HOST:pager": "less", + "HOST:http_unix_socket": "", + "HOST:browser": "brave", + }, + input: &ListOptions{Hostname: "HOST"}, // ConfigStub gives empty DefaultHost + stdout: `Settings configured for HOST + +git_protocol: ssh (default: https) +editor: /usr/bin/vim +prompt: disabled (default: enabled) +pager: less +http_unix_socket: +browser: brave +`, + }, + } + + for _, tt := range tests { + io, _, stdout, _ := iostreams.Test() + io.SetStdoutTTY(tt.isTTY) + io.SetStdinTTY(tt.isTTY) + tt.input.IO = io + tt.input.Config = func() (config.Config, error) { + return tt.config, nil + } + + t.Run(tt.name, func(t *testing.T) { + err := listRun(tt.input) + assert.NoError(t, err) + assert.Equal(t, tt.stdout, stdout.String()) + //assert.Equal(t, tt.stderr, stderr.String()) + }) + } +} From 5b7b4b8a717d6a574210c9ceb6cb24c132addf30 Mon Sep 17 00:00:00 2001 From: meiji163 Date: Mon, 15 Nov 2021 16:53:49 -0800 Subject: [PATCH 3/5] linter not like FPrintf --- pkg/cmd/config/list/list.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/config/list/list.go b/pkg/cmd/config/list/list.go index ebfee64a9..c6d45e5ff 100644 --- a/pkg/cmd/config/list/list.go +++ b/pkg/cmd/config/list/list.go @@ -60,7 +60,7 @@ func listRun(opts *ListOptions) error { } if isTTY { - fmt.Fprintf(opts.IO.Out, cs.Grayf("Settings configured for %s\n\n", host)) + fmt.Fprint(opts.IO.Out, cs.Grayf("Settings configured for %s\n\n", host)) } configOptions := config.ConfigOptions() @@ -75,7 +75,7 @@ func listRun(opts *ListOptions) error { configLine = cs.Bold(configLine) configLine += fmt.Sprintf(" (default: %s)", key.DefaultValue) } - fmt.Fprintln(opts.IO.Out, configLine) + fmt.Fprint(opts.IO.Out, configLine, "\n") } return nil From 8d82534461287f60c34e6ef50252634859c76d75 Mon Sep 17 00:00:00 2001 From: meiji163 Date: Tue, 16 Nov 2021 10:39:22 -0800 Subject: [PATCH 4/5] simple printing --- pkg/cmd/config/list/list.go | 16 ++-------------- pkg/cmd/config/list/list_test.go | 20 +++++++------------- 2 files changed, 9 insertions(+), 27 deletions(-) diff --git a/pkg/cmd/config/list/list.go b/pkg/cmd/config/list/list.go index c6d45e5ff..f610de579 100644 --- a/pkg/cmd/config/list/list.go +++ b/pkg/cmd/config/list/list.go @@ -46,9 +46,6 @@ func listRun(opts *ListOptions) error { return err } - cs := opts.IO.ColorScheme() - isTTY := opts.IO.IsStdoutTTY() - var host string if opts.Hostname != "" { host = opts.Hostname @@ -59,23 +56,14 @@ func listRun(opts *ListOptions) error { } } - if isTTY { - fmt.Fprint(opts.IO.Out, cs.Grayf("Settings configured for %s\n\n", host)) - } - configOptions := config.ConfigOptions() for _, key := range configOptions { - val, err := cfg.Get(opts.Hostname, key.Key) + val, err := cfg.Get(host, key.Key) if err != nil { return err } - configLine := fmt.Sprintf("%s: %s", key.Key, val) - if isTTY && key.DefaultValue != "" && val != key.DefaultValue { - configLine = cs.Bold(configLine) - configLine += fmt.Sprintf(" (default: %s)", key.DefaultValue) - } - fmt.Fprint(opts.IO.Out, configLine, "\n") + fmt.Fprint(opts.IO.Out, fmt.Sprintf("%s=%s\n", key.Key, val)) } return nil diff --git a/pkg/cmd/config/list/list_test.go b/pkg/cmd/config/list/list_test.go index de83adfb2..14f9aba4b 100644 --- a/pkg/cmd/config/list/list_test.go +++ b/pkg/cmd/config/list/list_test.go @@ -72,13 +72,11 @@ func Test_listRun(t *testing.T) { name string input *ListOptions config config.ConfigStub - isTTY bool stdout string wantErr bool }{ { - name: "list", - isTTY: true, + name: "list", config: config.ConfigStub{ "HOST:git_protocol": "ssh", "HOST:editor": "/usr/bin/vim", @@ -88,22 +86,18 @@ func Test_listRun(t *testing.T) { "HOST:browser": "brave", }, input: &ListOptions{Hostname: "HOST"}, // ConfigStub gives empty DefaultHost - stdout: `Settings configured for HOST - -git_protocol: ssh (default: https) -editor: /usr/bin/vim -prompt: disabled (default: enabled) -pager: less -http_unix_socket: -browser: brave + stdout: `git_protocol=ssh +editor=/usr/bin/vim +prompt=disabled +pager=less +http_unix_socket= +browser=brave `, }, } for _, tt := range tests { io, _, stdout, _ := iostreams.Test() - io.SetStdoutTTY(tt.isTTY) - io.SetStdinTTY(tt.isTTY) tt.input.IO = io tt.input.Config = func() (config.Config, error) { return tt.config, nil From 5b13decfb312cc7201001ce92fb26692b2f0c312 Mon Sep 17 00:00:00 2001 From: meiji163 Date: Tue, 16 Nov 2021 10:43:25 -0800 Subject: [PATCH 5/5] linter complains again --- pkg/cmd/config/list/list.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cmd/config/list/list.go b/pkg/cmd/config/list/list.go index f610de579..8b1157b1f 100644 --- a/pkg/cmd/config/list/list.go +++ b/pkg/cmd/config/list/list.go @@ -63,7 +63,7 @@ func listRun(opts *ListOptions) error { if err != nil { return err } - fmt.Fprint(opts.IO.Out, fmt.Sprintf("%s=%s\n", key.Key, val)) + fmt.Fprintf(opts.IO.Out, "%s=%s\n", key.Key, val) } return nil