plugin: Add output format in list command

Added JSON, YAML and Table output format in helm plugin list

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
pull/8410/head
Abhijeet Kasurde 5 years ago
parent 97774ee13c
commit d7591693f7

@ -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

@ -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,16 +39,33 @@ func newPluginListCmd(out io.Writer) *cobra.Command {
return err
}
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 plugins {
for _, p := range p.plugins {
table.AddRow(p.Metadata.Name, p.Metadata.Version, p.Metadata.Description)
}
fmt.Fprintln(out, table)
return nil
},
return output.EncodeTable(out, table)
}
return cmd
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

@ -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")
}

@ -0,0 +1 @@
NAME VERSION DESCRIPTION
Loading…
Cancel
Save