From a4b468f3f92b1debd3c982b6ccb13cbd4b275975 Mon Sep 17 00:00:00 2001 From: nikhilsbhat Date: Sun, 20 Nov 2022 21:52:46 +0530 Subject: [PATCH] Add example to the cli and add few validations --- .golangci.yml | 2 +- cmd/register.go | 12 +++++++++++- pkg/render_test.go | 6 ++++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 840d237..ef1ea44 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -36,7 +36,7 @@ issues: linters-settings: funlen: - lines: 100 + lines: 160 statements: 60 cyclop: max-complexity: 30 diff --git a/cmd/register.go b/cmd/register.go index 4f5cd3f..8cb8ed3 100644 --- a/cmd/register.go +++ b/cmd/register.go @@ -4,6 +4,7 @@ import ( "bufio" "encoding/json" "errors" + "fmt" "log" "os" "strings" @@ -53,7 +54,11 @@ func getImagesCommand() *cobra.Command { Use: "get [RELEASE] [CHART] [flags]", Short: "Fetches all images those are part of specified chart/release", Long: "Lists all images those are part of specified chart/release and matches the pattern or part of specified registry.", - Args: minimumArgError, + Example: ` helm images get prometheus-standalone path/to/chart/prometheus-standalone -f ~/path/to/override-config.yaml + helm images get prometheus-standalone --from-release --registry quay.io + helm images get prometheus-standalone --from-release --registry quay.io --unique + helm images get prometheus-standalone --from-release --registry quay.io --yaml`, + Args: minimumArgError, RunE: func(cmd *cobra.Command, args []string) error { images.SetLogger(images.LogLevel) images.SetWriter(os.Stdout) @@ -64,6 +69,11 @@ func getImagesCommand() *cobra.Command { images.SetChart(args[1]) } + if (images.JSON && images.YAML && images.Table) || (images.JSON && images.YAML) || + (images.Table && images.YAML) || (images.Table && images.JSON) { + return fmt.Errorf("cannot render the output to multiple format, enable any of '--yaml --json --table' at a time") + } + return images.GetImages() }, } diff --git a/pkg/render_test.go b/pkg/render_test.go index ed89fcd..17c5ca5 100644 --- a/pkg/render_test.go +++ b/pkg/render_test.go @@ -25,7 +25,8 @@ func TestImages_toYAML(t *testing.T) { } imagesFiltered := []*k8s.Image{&images} - expected := "---\n- image:\n - prom/pushgateway:v1.3.1\n - jimmidyson/configmap-reload:v0.5.0\n kind: Deployment\n name: sample-deployment\n" + expected := "---\n- image:\n - prom/pushgateway:v1.3.1\n - jimmidyson/configmap-reload:v0.5.0\n " + + "kind: Deployment\n name: sample-deployment\n" err := imageClient.toYAML(imagesFiltered) assert.NoError(t, err) assert.Equal(t, expected, yamlOut.String()) @@ -49,7 +50,8 @@ func TestImages_toJSON(t *testing.T) { } imagesFiltered := []*k8s.Image{&images} - expected := "[\n {\n \"kind\": \"Deployment\",\n \"name\": \"sample-deployment\",\n \"image\": [\n \"prom/pushgateway:v1.3.1\",\n \"jimmidyson/configmap-reload:v0.5.0\"\n ]\n }\n ]" + expected := "[\n {\n \"kind\": \"Deployment\",\n \"name\": \"sample-deployment\",\n " + + "\"image\": [\n \"prom/pushgateway:v1.3.1\",\n \"jimmidyson/configmap-reload:v0.5.0\"\n ]\n }\n ]" err := imageClient.toJSON(imagesFiltered) assert.NoError(t, err) assert.Equal(t, expected, jsonOut.String())