From a79cad553964c4bfaeba8a426af277654a20e1a4 Mon Sep 17 00:00:00 2001 From: cndoit18 Date: Sat, 8 May 2021 19:27:57 +0800 Subject: [PATCH] support max-col-width parameters support commands: - helm chart list - helm dependency list Signed-off-by: cndoit18 --- cmd/helm/chart_list.go | 8 ++++++-- cmd/helm/dependency.go | 5 ++++- internal/experimental/registry/client.go | 7 ++++++- internal/experimental/registry/client_opts.go | 7 +++++++ pkg/action/chart_list.go | 10 ++++++++-- pkg/action/dependency.go | 7 +++++-- 6 files changed, 36 insertions(+), 8 deletions(-) diff --git a/cmd/helm/chart_list.go b/cmd/helm/chart_list.go index a9d01c9bd..e81882ccc 100644 --- a/cmd/helm/chart_list.go +++ b/cmd/helm/chart_list.go @@ -31,14 +31,18 @@ Charts are sorted by ref name, alphabetically. ` func newChartListCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { - return &cobra.Command{ + chartList := action.NewChartList(cfg) + cmd := &cobra.Command{ Use: "list", Aliases: []string{"ls"}, Short: "list all saved charts", Long: chartListDesc, Hidden: !FeatureGateOCI.IsEnabled(), RunE: func(cmd *cobra.Command, args []string) error { - return action.NewChartList(cfg).Run(out) + return chartList.Run(out) }, } + f := cmd.Flags() + f.UintVar(&chartList.ColumnWidth, "max-col-width", 60, "maximum column width for output table") + return cmd } diff --git a/cmd/helm/dependency.go b/cmd/helm/dependency.go index 6bb82e217..03874742c 100644 --- a/cmd/helm/dependency.go +++ b/cmd/helm/dependency.go @@ -100,7 +100,6 @@ func newDependencyCmd(cfg *action.Configuration, out io.Writer) *cobra.Command { func newDependencyListCmd(out io.Writer) *cobra.Command { client := action.NewDependency() - cmd := &cobra.Command{ Use: "list CHART", Aliases: []string{"ls"}, @@ -115,5 +114,9 @@ func newDependencyListCmd(out io.Writer) *cobra.Command { return client.List(chartpath, out) }, } + + f := cmd.Flags() + + f.UintVar(&client.ColumnWidth, "max-col-width", 80, "maximum column width for output table") return cmd } diff --git a/internal/experimental/registry/client.go b/internal/experimental/registry/client.go index c889ee913..26556533c 100644 --- a/internal/experimental/registry/client.go +++ b/internal/experimental/registry/client.go @@ -51,6 +51,7 @@ type ( authorizer *Authorizer resolver *Resolver cache *Cache + columnWidth uint } ) @@ -95,6 +96,10 @@ func NewClient(opts ...ClientOption) (*Client, error) { } client.cache = cache } + + if client.columnWidth == 0 { + client.columnWidth = 60 + } return client, nil } @@ -279,7 +284,7 @@ func (c *Client) RemoveChart(ref *Reference) error { // PrintChartTable prints a list of locally stored charts func (c *Client) PrintChartTable() error { table := uitable.New() - table.MaxColWidth = 60 + table.MaxColWidth = c.columnWidth table.AddRow("REF", "NAME", "VERSION", "DIGEST", "SIZE", "CREATED") rows, err := c.getChartTableRows() if err != nil { diff --git a/internal/experimental/registry/client_opts.go b/internal/experimental/registry/client_opts.go index e2f742aec..266cf3f6f 100644 --- a/internal/experimental/registry/client_opts.go +++ b/internal/experimental/registry/client_opts.go @@ -67,3 +67,10 @@ func ClientOptCredentialsFile(credentialsFile string) ClientOption { client.credentialsFile = credentialsFile } } + +// ClientOptColumnWidth returns a function that sets the column width on a client options set +func ClientOptColumnWidth(columnWidth uint) ClientOption { + return func(client *Client) { + client.columnWidth = columnWidth + } +} diff --git a/pkg/action/chart_list.go b/pkg/action/chart_list.go index db764b3a3..ba49a887d 100644 --- a/pkg/action/chart_list.go +++ b/pkg/action/chart_list.go @@ -18,11 +18,14 @@ package action import ( "io" + + "helm.sh/helm/v3/internal/experimental/registry" ) // ChartList performs a chart list operation. type ChartList struct { - cfg *Configuration + cfg *Configuration + ColumnWidth uint } // NewChartList creates a new ChartList object with the given configuration. @@ -34,5 +37,8 @@ func NewChartList(cfg *Configuration) *ChartList { // Run executes the chart list operation func (a *ChartList) Run(out io.Writer) error { - return a.cfg.RegistryClient.PrintChartTable() + client := a.cfg.RegistryClient + opt := registry.ClientOptColumnWidth(a.ColumnWidth) + opt(client) + return client.PrintChartTable() } diff --git a/pkg/action/dependency.go b/pkg/action/dependency.go index 578a46aec..6a284d762 100644 --- a/pkg/action/dependency.go +++ b/pkg/action/dependency.go @@ -37,11 +37,14 @@ type Dependency struct { Verify bool Keyring string SkipRefresh bool + ColumnWidth uint } // NewDependency creates a new Dependency object with the given configuration. func NewDependency() *Dependency { - return &Dependency{} + return &Dependency{ + ColumnWidth: 80, + } } // List executes 'helm dependency list'. @@ -181,7 +184,7 @@ func statArchiveForStatus(archive string, dep *chart.Dependency) string { // printDependencies prints all of the dependencies in the yaml file. func (d *Dependency) printDependencies(chartpath string, out io.Writer, c *chart.Chart) { table := uitable.New() - table.MaxColWidth = 80 + table.MaxColWidth = d.ColumnWidth table.AddRow("NAME", "VERSION", "REPOSITORY", "STATUS") for _, row := range c.Metadata.Dependencies { table.AddRow(row.Name, row.Version, row.Repository, d.dependencyStatus(chartpath, row, c))