diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index e27fa7d19..00c243142 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -197,7 +197,7 @@ below. 3. Discussion - issues that are labeled as `feature` or `bug` should be connected to the PR that resolves it. - Whoever is working on a `feature` or `bug` issue (whether a maintainer or someone from the - community), should either assign the issue to themself or make a comment in the issue saying + community), should either assign the issue to themselves or make a comment in the issue saying that they are taking it. - `proposal` and `support/question` issues should stay open until resolved or if they have not been active for more than 30 days. This will help keep the issue queue to a manageable size diff --git a/cmd/helm/plugin_list.go b/cmd/helm/plugin_list.go index 49a454963..8bbc11855 100644 --- a/cmd/helm/plugin_list.go +++ b/cmd/helm/plugin_list.go @@ -16,17 +16,18 @@ limitations under the License. package main import ( - "fmt" "io" "strings" "github.com/gosuri/uitable" "github.com/spf13/cobra" + "helm.sh/helm/v3/pkg/cli/output" "helm.sh/helm/v3/pkg/plugin" ) func newPluginListCmd(out io.Writer) *cobra.Command { + var outfmt output.Format cmd := &cobra.Command{ Use: "list", Aliases: []string{"ls"}, @@ -38,18 +39,35 @@ func newPluginListCmd(out io.Writer) *cobra.Command { return err } - table := uitable.New() - table.AddRow("NAME", "VERSION", "DESCRIPTION") - for _, p := range plugins { - table.AddRow(p.Metadata.Name, p.Metadata.Version, p.Metadata.Description) - } - fmt.Fprintln(out, table) - return nil + return outfmt.Write(out, &pluginListWriter{plugins}) }, } + bindOutputFlag(cmd, &outfmt) + return cmd } +type pluginListWriter struct { + plugins []*plugin.Plugin +} + +func (p *pluginListWriter) WriteTable(out io.Writer) error { + table := uitable.New() + table.AddRow("NAME", "VERSION", "DESCRIPTION") + for _, p := range p.plugins { + table.AddRow(p.Metadata.Name, p.Metadata.Version, p.Metadata.Description) + } + return output.EncodeTable(out, table) +} + +func (p *pluginListWriter) WriteJSON(out io.Writer) error { + return output.EncodeJSON(out, p.plugins) +} + +func (p *pluginListWriter) WriteYAML(out io.Writer) error { + return output.EncodeYAML(out, p.plugins) +} + // Provide dynamic auto-completion for plugin names func compListPlugins(toComplete string) []string { var pNames []string diff --git a/cmd/helm/plugin_list_test.go b/cmd/helm/plugin_list_test.go new file mode 100644 index 000000000..bf7e1153f --- /dev/null +++ b/cmd/helm/plugin_list_test.go @@ -0,0 +1,42 @@ +/* +Copyright The Helm Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package main + +import ( + "testing" +) + +func TestPluginListCmd(t *testing.T) { + tests := []cmdTestCase{{ + name: "List plugins", + cmd: "plugin list", + golden: "output/plugin-list.txt", + }, { + name: "List plugins with JSON", + cmd: "plugin list -o json", + golden: "output/plugin-list-json.txt", + }, { + name: "List plugins with YAML", + cmd: "plugin list -o yaml", + golden: "output/plugin-list-yaml.txt", + }} + runTestCmd(t, tests) +} + +func TestPluginListOutputCompletion(t *testing.T) { + outputFlagCompletionTest(t, "plugin list") +} diff --git a/cmd/helm/testdata/output/plugin-list-json.txt b/cmd/helm/testdata/output/plugin-list-json.txt new file mode 100644 index 000000000..fe51488c7 --- /dev/null +++ b/cmd/helm/testdata/output/plugin-list-json.txt @@ -0,0 +1 @@ +[] diff --git a/cmd/helm/testdata/output/plugin-list-yaml.txt b/cmd/helm/testdata/output/plugin-list-yaml.txt new file mode 100644 index 000000000..fe51488c7 --- /dev/null +++ b/cmd/helm/testdata/output/plugin-list-yaml.txt @@ -0,0 +1 @@ +[] diff --git a/cmd/helm/testdata/output/plugin-list.txt b/cmd/helm/testdata/output/plugin-list.txt new file mode 100644 index 000000000..ffed9ea71 --- /dev/null +++ b/cmd/helm/testdata/output/plugin-list.txt @@ -0,0 +1 @@ +NAME VERSION DESCRIPTION