diff --git a/.github/scripts/get_release_version.py b/.github/scripts/get_release_version.py index 968a70e45..03f294de3 100644 --- a/.github/scripts/get_release_version.py +++ b/.github/scripts/get_release_version.py @@ -1,5 +1,5 @@ # ------------------------------------------------------------ -# Copyright (c) Microsoft Corporation. +# Copyright (c) Microsoft Corporation and Dapr Contributors. # Licensed under the MIT License. # ------------------------------------------------------------ diff --git a/.github/workflows/dapr_cli.yaml b/.github/workflows/dapr_cli.yaml index 782996777..480a80821 100644 --- a/.github/workflows/dapr_cli.yaml +++ b/.github/workflows/dapr_cli.yaml @@ -1,5 +1,5 @@ # ------------------------------------------------------------ -# Copyright (c) Microsoft Corporation. +# Copyright (c) Microsoft Corporation and Dapr Contributors. # Licensed under the MIT License. # ------------------------------------------------------------ @@ -102,6 +102,10 @@ jobs: REL_VER=`cat ${REL_VERSION_FILE}` echo "REL_VERSION=${REL_VER}" >> $GITHUB_ENV rm -f ${REL_VERSION_FILE} + - name: generate checksum files + run: cd ${ARTIFACT_DIR} && for i in *; do sha256sum -b $i > "$i.sha256"; done && cd - + - name: lists artifacts + run: ls -l ${{ env.ARTIFACT_DIR }} - name: publish binaries to github if: startswith(github.ref, 'refs/tags/v') run: | diff --git a/.github/workflows/release_notification.yaml b/.github/workflows/release_notification.yaml index 9085fc975..319deca4f 100644 --- a/.github/workflows/release_notification.yaml +++ b/.github/workflows/release_notification.yaml @@ -1,5 +1,5 @@ # ------------------------------------------------------------ -# Copyright (c) Microsoft Corporation. +# Copyright (c) Microsoft Corporation and Dapr Contributors. # Licensed under the MIT License. # ------------------------------------------------------------ diff --git a/LICENSE b/LICENSE index 63447fd8b..f18561f5d 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) Microsoft Corporation. +Copyright (c) Microsoft Corporation and Dapr Contributors. MIT License diff --git a/Makefile b/Makefile index 02d202a89..97888d4b1 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ # ------------------------------------------------------------ -# Copyright (c) Microsoft Corporation. +# Copyright (c) Microsoft Corporation and Dapr Contributors. # Licensed under the MIT License. # ------------------------------------------------------------ diff --git a/cmd/completion.go b/cmd/completion.go index 21ca8b0b0..2021cd51f 100644 --- a/cmd/completion.go +++ b/cmd/completion.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/cmd/components.go b/cmd/components.go index c39d4fe88..dce923847 100644 --- a/cmd/components.go +++ b/cmd/components.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ @@ -10,29 +10,24 @@ import ( "github.com/dapr/cli/pkg/kubernetes" "github.com/dapr/cli/pkg/print" - "github.com/dapr/cli/utils" - "github.com/gocarina/gocsv" "github.com/spf13/cobra" ) +var ( + componentsName string + componentsOutputFormat string +) + var ComponentsCmd = &cobra.Command{ Use: "components", Short: "List all Dapr components. Supported platforms: Kubernetes", Run: func(cmd *cobra.Command, args []string) { if kubernetesMode { - components, err := kubernetes.Components() - if err != nil { - print.FailureStatusEvent(os.Stdout, err.Error()) - os.Exit(1) - } - - table, err := gocsv.MarshalString(components) + err := kubernetes.PrintComponents(componentsName, componentsOutputFormat) if err != nil { print.FailureStatusEvent(os.Stdout, err.Error()) os.Exit(1) } - - utils.PrintTable(table) } }, Example: ` @@ -42,6 +37,8 @@ dapr components -k } func init() { + ComponentsCmd.Flags().StringVarP(&componentsName, "name", "n", "", "The components name to be printed (optional)") + ComponentsCmd.Flags().StringVarP(&componentsOutputFormat, "output", "o", "list", "Output format (options: json or yaml or list)") ComponentsCmd.Flags().BoolVarP(&kubernetesMode, "kubernetes", "k", false, "List all Dapr components in a Kubernetes cluster") ComponentsCmd.Flags().BoolP("help", "h", false, "Print this help message") ComponentsCmd.MarkFlagRequired("kubernetes") diff --git a/cmd/configurations.go b/cmd/configurations.go index 2df324c3b..6d5812833 100644 --- a/cmd/configurations.go +++ b/cmd/configurations.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/cmd/dapr.go b/cmd/dapr.go index 2374b9f0d..b2a49b3ab 100644 --- a/cmd/dapr.go +++ b/cmd/dapr.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/cmd/dashboard.go b/cmd/dashboard.go index aba5235e8..b811cb4a2 100644 --- a/cmd/dashboard.go +++ b/cmd/dashboard.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/cmd/init.go b/cmd/init.go index 33ab6ba17..c806f73bb 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/cmd/invoke.go b/cmd/invoke.go index 3688f22ec..70232e1fb 100644 --- a/cmd/invoke.go +++ b/cmd/invoke.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/cmd/list.go b/cmd/list.go index d8a9f3c75..01ec918bf 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/cmd/logs.go b/cmd/logs.go index 034f5f14a..79d936e62 100644 --- a/cmd/logs.go +++ b/cmd/logs.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/cmd/mtls.go b/cmd/mtls.go index 3c3e26147..d50aba51e 100644 --- a/cmd/mtls.go +++ b/cmd/mtls.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/cmd/publish.go b/cmd/publish.go index 88236cbfb..bcbedacc6 100644 --- a/cmd/publish.go +++ b/cmd/publish.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/cmd/run.go b/cmd/run.go index 37a36db5a..162c9eebb 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/cmd/status.go b/cmd/status.go index a5976bd2a..2a3209ea4 100644 --- a/cmd/status.go +++ b/cmd/status.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/cmd/stop.go b/cmd/stop.go index 9d471f204..6af465f5e 100644 --- a/cmd/stop.go +++ b/cmd/stop.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/cmd/uninstall.go b/cmd/uninstall.go index 1fb76bcb4..abc9fb137 100644 --- a/cmd/uninstall.go +++ b/cmd/uninstall.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/cmd/upgrade.go b/cmd/upgrade.go index 9d65c9af3..82bccd26c 100644 --- a/cmd/upgrade.go +++ b/cmd/upgrade.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/go.mod b/go.mod index f160b9345..7414bb70f 100644 --- a/go.mod +++ b/go.mod @@ -26,7 +26,6 @@ require ( gopkg.in/yaml.v2 v2.3.0 helm.sh/helm/v3 v3.4.0 k8s.io/api v0.20.0 - k8s.io/apiextensions-apiserver v0.20.0 k8s.io/apimachinery v0.20.0 k8s.io/cli-runtime v0.20.0 k8s.io/client-go v0.20.0 diff --git a/install/install.ps1 b/install/install.ps1 index d899ef78f..a452ac523 100755 --- a/install/install.ps1 +++ b/install/install.ps1 @@ -1,5 +1,5 @@ # ------------------------------------------------------------ -# Copyright (c) Microsoft Corporation. +# Copyright (c) Microsoft Corporation and Dapr Contributors. # Licensed under the MIT License. # ------------------------------------------------------------ param ( diff --git a/install/install.sh b/install/install.sh index e3fd5ac71..cf59fca32 100755 --- a/install/install.sh +++ b/install/install.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash # ------------------------------------------------------------ -# Copyright (c) Microsoft Corporation. +# Copyright (c) Microsoft Corporation and Dapr Contributors. # Licensed under the MIT License. # ------------------------------------------------------------ diff --git a/main.go b/main.go index cbf65df05..875bab734 100644 --- a/main.go +++ b/main.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/pkg/age/age.go b/pkg/age/age.go index c6bdb8052..e741fb6eb 100644 --- a/pkg/age/age.go +++ b/pkg/age/age.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/pkg/api/api.go b/pkg/api/api.go index e8ecd3e63..de1a858c3 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/pkg/kubernetes/client.go b/pkg/kubernetes/client.go index b2a071d7e..e202ad475 100644 --- a/pkg/kubernetes/client.go +++ b/pkg/kubernetes/client.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/pkg/kubernetes/components.go b/pkg/kubernetes/components.go index 01153ae90..0cca71a74 100644 --- a/pkg/kubernetes/components.go +++ b/pkg/kubernetes/components.go @@ -1,15 +1,20 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ package kubernetes import ( + "io" + "os" "strings" - "github.com/dapr/cli/pkg/age" meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + "github.com/dapr/cli/pkg/age" + "github.com/dapr/cli/utils" + v1alpha1 "github.com/dapr/dapr/pkg/apis/components/v1alpha1" ) // ComponentsOutput represent a Dapr component. @@ -22,20 +27,51 @@ type ComponentsOutput struct { Age string `csv:"AGE"` } -// List outputs all Dapr components. -func Components() ([]ComponentsOutput, error) { - client, err := DaprClient() +// PrintComponents prints all Dapr components. +func PrintComponents(name, outputFormat string) error { + return writeComponents(os.Stdout, func() (*v1alpha1.ComponentList, error) { + client, err := DaprClient() + if err != nil { + return nil, err + } + + return client.ComponentsV1alpha1().Components(meta_v1.NamespaceAll).List(meta_v1.ListOptions{}) + }, name, outputFormat) +} + +func writeComponents(writer io.Writer, getConfigFunc func() (*v1alpha1.ComponentList, error), name, outputFormat string) error { + confs, err := getConfigFunc() if err != nil { - return nil, err + return err } - comps, err := client.ComponentsV1alpha1().Components(meta_v1.NamespaceAll).List(meta_v1.ListOptions{}) - if err != nil { - return nil, err + filtered := []v1alpha1.Component{} + filteredSpecs := []configurationDetailedOutput{} + for _, c := range confs.Items { + confName := c.GetName() + if confName == "daprsystem" { + continue + } + + if name == "" || strings.EqualFold(confName, name) { + filtered = append(filtered, c) + filteredSpecs = append(filteredSpecs, configurationDetailedOutput{ + Name: confName, + Spec: c.Spec, + }) + } + } + + if outputFormat == "" || outputFormat == "list" { + return printComponentList(writer, filtered) } + return utils.PrintDetail(writer, outputFormat, filteredSpecs) +} + +func printComponentList(writer io.Writer, list []v1alpha1.Component) error { co := []ComponentsOutput{} - for _, c := range comps.Items { + for _, c := range list { co = append(co, ComponentsOutput{ Name: c.GetName(), Type: c.Spec.Type, @@ -45,5 +81,6 @@ func Components() ([]ComponentsOutput, error) { Scopes: strings.Join(c.Scopes, ","), }) } - return co, nil + + return utils.MarshalAndWriteTable(writer, co) } diff --git a/pkg/kubernetes/components_test.go b/pkg/kubernetes/components_test.go new file mode 100644 index 000000000..63bb4f05a --- /dev/null +++ b/pkg/kubernetes/components_test.go @@ -0,0 +1,248 @@ +// ------------------------------------------------------------ +// Copyright (c) Microsoft Corporation and Dapr Contributors. +// Licensed under the MIT License. +// ------------------------------------------------------------ + +package kubernetes + +import ( + "bytes" + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + v1alpha1 "github.com/dapr/dapr/pkg/apis/components/v1alpha1" +) + +func TestComponents(t *testing.T) { + now := meta_v1.Now() + formattedNow := now.Format("2006-01-02 15:04.05") + testCases := []struct { + name string + configName string + outputFormat string + expectedOutput string + errString string + errorExpected bool + k8sConfig []v1alpha1.Component + }{ + { + name: "List one config", + configName: "", + outputFormat: "", + expectedOutput: " NAME TYPE VERSION SCOPES CREATED AGE \n appConfig state.redis v1 " + formattedNow + " 0s \n", + errString: "", + errorExpected: false, + k8sConfig: []v1alpha1.Component{ + { + ObjectMeta: meta_v1.ObjectMeta{ + Name: "appConfig", + CreationTimestamp: now, + }, + Spec: v1alpha1.ComponentSpec{ + Type: "state.redis", + Version: "v1", + }, + }, + }, + }, + { + name: "Error on fetching config", + configName: "", + outputFormat: "", + expectedOutput: "", + errString: "could not fetch config", + errorExpected: true, + k8sConfig: []v1alpha1.Component{}, + }, + { + name: "Filters out daprsystem", + configName: "", + outputFormat: "", + expectedOutput: " NAME TYPE VERSION SCOPES CREATED AGE \n appConfig state.redis v1 " + formattedNow + " 0s \n", + errString: "", + errorExpected: false, + k8sConfig: []v1alpha1.Component{ + { + ObjectMeta: meta_v1.ObjectMeta{ + Name: "appConfig", + CreationTimestamp: now, + }, + Spec: v1alpha1.ComponentSpec{ + Type: "state.redis", + Version: "v1", + }, + }, + { + ObjectMeta: meta_v1.ObjectMeta{ + Name: "daprsystem", + CreationTimestamp: now, + }, + Spec: v1alpha1.ComponentSpec{}, + }, + }, + }, + { + name: "Name does match", + configName: "appConfig", + outputFormat: "list", + expectedOutput: " NAME TYPE VERSION SCOPES CREATED AGE \n appConfig state.redis v1 " + formattedNow + " 0s \n", + errString: "", + errorExpected: false, + k8sConfig: []v1alpha1.Component{ + { + ObjectMeta: meta_v1.ObjectMeta{ + Name: "appConfig", + CreationTimestamp: now, + }, + Spec: v1alpha1.ComponentSpec{ + Type: "state.redis", + Version: "v1", + }, + }, + }, + }, + { + name: "Name does not match", + configName: "appConfig", + outputFormat: "list", + expectedOutput: " NAME TYPE VERSION SCOPES CREATED AGE \n", + errString: "", + errorExpected: false, + k8sConfig: []v1alpha1.Component{ + { + ObjectMeta: meta_v1.ObjectMeta{ + Name: "not config", + CreationTimestamp: now, + }, + Spec: v1alpha1.ComponentSpec{ + Type: "state.redis", + Version: "v1", + }, + }, + }, + }, + { + name: "Yaml one config", + configName: "", + outputFormat: "yaml", + expectedOutput: "name: appConfig\nspec:\n type: state.redis\n version: v1\n ignoreerrors: false\n metadata: []\n inittimeout: \"\"\n", + errString: "", + errorExpected: false, + k8sConfig: []v1alpha1.Component{ + { + ObjectMeta: meta_v1.ObjectMeta{ + Name: "appConfig", + CreationTimestamp: now, + }, + Spec: v1alpha1.ComponentSpec{ + Type: "state.redis", + Version: "v1", + }, + }, + }, + }, + { + name: "Yaml two configs", + configName: "", + outputFormat: "yaml", + expectedOutput: "- name: appConfig1\n spec:\n type: state.redis\n version: v1\n ignoreerrors: false\n metadata: []\n inittimeout: \"\"\n- name: appConfig2\n spec:\n type: state.redis\n version: v1\n ignoreerrors: false\n metadata: []\n inittimeout: \"\"\n", + errString: "", + errorExpected: false, + k8sConfig: []v1alpha1.Component{ + { + ObjectMeta: meta_v1.ObjectMeta{ + Name: "appConfig1", + CreationTimestamp: now, + }, + Spec: v1alpha1.ComponentSpec{ + Type: "state.redis", + Version: "v1", + }, + }, + { + ObjectMeta: meta_v1.ObjectMeta{ + Name: "appConfig2", + CreationTimestamp: now, + }, + Spec: v1alpha1.ComponentSpec{ + Type: "state.redis", + Version: "v1", + }, + }, + }, + }, + { + name: "Json one config", + configName: "", + outputFormat: "json", + expectedOutput: "{\n \"name\": \"appConfig\",\n \"spec\": {\n \"type\": \"state.redis\",\n \"version\": \"v1\",\n \"ignoreErrors\": false,\n \"metadata\": null,\n \"initTimeout\": \"\"\n }\n}", + errString: "", + errorExpected: false, + k8sConfig: []v1alpha1.Component{ + { + ObjectMeta: meta_v1.ObjectMeta{ + Name: "appConfig", + CreationTimestamp: now, + }, + Spec: v1alpha1.ComponentSpec{ + Type: "state.redis", + Version: "v1", + }, + }, + }, + }, + { + name: "Json two configs", + configName: "", + outputFormat: "json", + expectedOutput: "[\n {\n \"name\": \"appConfig1\",\n \"spec\": {\n \"type\": \"state.redis\",\n \"version\": \"v1\",\n \"ignoreErrors\": false,\n \"metadata\": null,\n \"initTimeout\": \"\"\n }\n },\n {\n \"name\": \"appConfig2\",\n \"spec\": {\n \"type\": \"state.redis\",\n \"version\": \"v1\",\n \"ignoreErrors\": false,\n \"metadata\": null,\n \"initTimeout\": \"\"\n }\n }\n]", + errString: "", + errorExpected: false, + k8sConfig: []v1alpha1.Component{ + { + ObjectMeta: meta_v1.ObjectMeta{ + Name: "appConfig1", + CreationTimestamp: now, + }, + Spec: v1alpha1.ComponentSpec{ + Type: "state.redis", + Version: "v1", + }, + }, + { + ObjectMeta: meta_v1.ObjectMeta{ + Name: "appConfig2", + CreationTimestamp: now, + }, + Spec: v1alpha1.ComponentSpec{ + Type: "state.redis", + Version: "v1", + }, + }, + }, + }, + } + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + var buff bytes.Buffer + err := writeComponents(&buff, + func() (*v1alpha1.ComponentList, error) { + if len(tc.errString) > 0 { + return nil, fmt.Errorf(tc.errString) + } + + return &v1alpha1.ComponentList{Items: tc.k8sConfig}, nil + }, tc.configName, tc.outputFormat) + if tc.errorExpected { + assert.Error(t, err, "expected an error") + assert.Equal(t, tc.errString, err.Error(), "expected error strings to match") + } else { + assert.NoError(t, err, "expected no error") + assert.Equal(t, tc.expectedOutput, buff.String(), "expected output strings to match") + } + }) + } +} diff --git a/pkg/kubernetes/configurations.go b/pkg/kubernetes/configurations.go index 0683d3076..0d3d57c8d 100644 --- a/pkg/kubernetes/configurations.go +++ b/pkg/kubernetes/configurations.go @@ -1,22 +1,21 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ package kubernetes import ( - "encoding/json" "io" "os" "strconv" + "strings" + + meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "github.com/dapr/cli/pkg/age" "github.com/dapr/cli/utils" v1alpha1 "github.com/dapr/dapr/pkg/apis/configuration/v1alpha1" - "github.com/gocarina/gocsv" - "gopkg.in/yaml.v2" - meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) type configurationsOutput struct { @@ -58,7 +57,7 @@ func writeConfigurations(writer io.Writer, getConfigFunc func() (*v1alpha1.Confi continue } - if name == "" || confName == name { + if name == "" || strings.EqualFold(confName, name) { filtered = append(filtered, c) filteredSpecs = append(filteredSpecs, configurationDetailedOutput{ Name: confName, @@ -68,36 +67,13 @@ func writeConfigurations(writer io.Writer, getConfigFunc func() (*v1alpha1.Confi } if outputFormat == "" || outputFormat == "list" { - return printList(writer, filtered) - } - - return printDetail(writer, outputFormat, filteredSpecs) -} - -func printDetail(writer io.Writer, outputFormat string, list []configurationDetailedOutput) error { - var err error - output := []byte{} - var obj interface{} = list - if len(list) == 1 { - obj = list[0] - } - if outputFormat == "yaml" { - output, err = yaml.Marshal(obj) - } - - if outputFormat == "json" { - output, err = json.MarshalIndent(obj, "", " ") - } - - if err != nil { - return err + return printConfigurationList(writer, filtered) } - writer.Write(output) - return nil + return utils.PrintDetail(writer, outputFormat, filteredSpecs) } -func printList(writer io.Writer, list []v1alpha1.Configuration) error { +func printConfigurationList(writer io.Writer, list []v1alpha1.Configuration) error { co := []configurationsOutput{} for _, c := range list { co = append(co, configurationsOutput{ @@ -109,13 +85,7 @@ func printList(writer io.Writer, list []v1alpha1.Configuration) error { }) } - table, err := gocsv.MarshalString(co) - if err != nil { - return err - } - - utils.WriteTable(writer, table) - return nil + return utils.MarshalAndWriteTable(writer, co) } func tracingEnabled(spec v1alpha1.TracingSpec) bool { diff --git a/pkg/kubernetes/configurations_test.go b/pkg/kubernetes/configurations_test.go index e762e81f6..c7c6ff16c 100644 --- a/pkg/kubernetes/configurations_test.go +++ b/pkg/kubernetes/configurations_test.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/pkg/kubernetes/kubernetes.go b/pkg/kubernetes/kubernetes.go index 7e5783862..3bbf7ee18 100644 --- a/pkg/kubernetes/kubernetes.go +++ b/pkg/kubernetes/kubernetes.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/pkg/kubernetes/list.go b/pkg/kubernetes/list.go index 3f7d01de8..32e82ac7b 100644 --- a/pkg/kubernetes/list.go +++ b/pkg/kubernetes/list.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/pkg/kubernetes/logs.go b/pkg/kubernetes/logs.go index 8cc16e231..41bfcbba7 100644 --- a/pkg/kubernetes/logs.go +++ b/pkg/kubernetes/logs.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/pkg/kubernetes/pods_test.go b/pkg/kubernetes/pods_test.go index e4e0eac7c..4570d7963 100644 --- a/pkg/kubernetes/pods_test.go +++ b/pkg/kubernetes/pods_test.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/pkg/kubernetes/portforward.go b/pkg/kubernetes/portforward.go index c57e1b720..3623f784c 100644 --- a/pkg/kubernetes/portforward.go +++ b/pkg/kubernetes/portforward.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/pkg/kubernetes/run.go b/pkg/kubernetes/run.go index 6432acea5..4227a0938 100644 --- a/pkg/kubernetes/run.go +++ b/pkg/kubernetes/run.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/pkg/kubernetes/status.go b/pkg/kubernetes/status.go index 1fac2cf37..9a2a60771 100644 --- a/pkg/kubernetes/status.go +++ b/pkg/kubernetes/status.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/pkg/kubernetes/status_test.go b/pkg/kubernetes/status_test.go index 647460db5..31138d906 100644 --- a/pkg/kubernetes/status_test.go +++ b/pkg/kubernetes/status_test.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/pkg/kubernetes/uninstall.go b/pkg/kubernetes/uninstall.go index 8a3934771..cc50ee33f 100644 --- a/pkg/kubernetes/uninstall.go +++ b/pkg/kubernetes/uninstall.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/pkg/kubernetes/upgrade.go b/pkg/kubernetes/upgrade.go index 0132b80f5..ad9ef9039 100644 --- a/pkg/kubernetes/upgrade.go +++ b/pkg/kubernetes/upgrade.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/pkg/kubernetes/upgrade_test.go b/pkg/kubernetes/upgrade_test.go index ed8b03cb6..1d1e4c576 100644 --- a/pkg/kubernetes/upgrade_test.go +++ b/pkg/kubernetes/upgrade_test.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/pkg/metadata/metadata.go b/pkg/metadata/metadata.go index d42020464..0afb1c2de 100644 --- a/pkg/metadata/metadata.go +++ b/pkg/metadata/metadata.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/pkg/metadata/metadata_test.go b/pkg/metadata/metadata_test.go index 7f0c75ea5..1658aa35e 100644 --- a/pkg/metadata/metadata_test.go +++ b/pkg/metadata/metadata_test.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/pkg/print/print.go b/pkg/print/print.go index da82a3cee..9e85fc862 100644 --- a/pkg/print/print.go +++ b/pkg/print/print.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/pkg/rundata/rundata.go b/pkg/rundata/rundata.go index 0b89fd235..fbc865f2d 100644 --- a/pkg/rundata/rundata.go +++ b/pkg/rundata/rundata.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/pkg/standalone/client.go b/pkg/standalone/client.go index d4089d4d8..1b4895753 100644 --- a/pkg/standalone/client.go +++ b/pkg/standalone/client.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/pkg/standalone/common.go b/pkg/standalone/common.go index 5e75de951..2336fed2f 100644 --- a/pkg/standalone/common.go +++ b/pkg/standalone/common.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/pkg/standalone/dashboard.go b/pkg/standalone/dashboard.go index 9852855ed..bcf68738f 100644 --- a/pkg/standalone/dashboard.go +++ b/pkg/standalone/dashboard.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/pkg/standalone/dashboard_test.go b/pkg/standalone/dashboard_test.go index fb5c853f6..0cc4d7a1b 100644 --- a/pkg/standalone/dashboard_test.go +++ b/pkg/standalone/dashboard_test.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/pkg/standalone/invoke.go b/pkg/standalone/invoke.go index 37c6be2b3..0e0d20dd1 100644 --- a/pkg/standalone/invoke.go +++ b/pkg/standalone/invoke.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/pkg/standalone/invoke_test.go b/pkg/standalone/invoke_test.go index 4fc185d66..19410e946 100644 --- a/pkg/standalone/invoke_test.go +++ b/pkg/standalone/invoke_test.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/pkg/standalone/list.go b/pkg/standalone/list.go index 6e06e103a..31b6ca2a9 100644 --- a/pkg/standalone/list.go +++ b/pkg/standalone/list.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/pkg/standalone/mtls_config.go b/pkg/standalone/mtls_config.go index 022dd192f..224c6833b 100644 --- a/pkg/standalone/mtls_config.go +++ b/pkg/standalone/mtls_config.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/pkg/standalone/publish.go b/pkg/standalone/publish.go index 90c45d19a..da6e30cca 100644 --- a/pkg/standalone/publish.go +++ b/pkg/standalone/publish.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/pkg/standalone/publish_test.go b/pkg/standalone/publish_test.go index e0f35ebf2..9198588ce 100644 --- a/pkg/standalone/publish_test.go +++ b/pkg/standalone/publish_test.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/pkg/standalone/run.go b/pkg/standalone/run.go index abb10459e..02606d4bb 100644 --- a/pkg/standalone/run.go +++ b/pkg/standalone/run.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/pkg/standalone/run_test.go b/pkg/standalone/run_test.go index 8b24b25fc..94ebc3444 100644 --- a/pkg/standalone/run_test.go +++ b/pkg/standalone/run_test.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/pkg/standalone/standalone.go b/pkg/standalone/standalone.go index 01bd405ae..35a40fdc5 100644 --- a/pkg/standalone/standalone.go +++ b/pkg/standalone/standalone.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/pkg/standalone/standalone_test.go b/pkg/standalone/standalone_test.go index 65429dfd9..12e7c03b0 100644 --- a/pkg/standalone/standalone_test.go +++ b/pkg/standalone/standalone_test.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/pkg/standalone/stop.go b/pkg/standalone/stop.go index 791a1cf30..cb3694734 100644 --- a/pkg/standalone/stop.go +++ b/pkg/standalone/stop.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/pkg/standalone/testutils.go b/pkg/standalone/testutils.go index 13daab830..06be42a35 100644 --- a/pkg/standalone/testutils.go +++ b/pkg/standalone/testutils.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/pkg/standalone/version.go b/pkg/standalone/version.go index e77a5bf16..e4a2cd0c4 100644 --- a/pkg/standalone/version.go +++ b/pkg/standalone/version.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/pkg/version/version.go b/pkg/version/version.go index cfbcf55f8..39a3e24c8 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ diff --git a/utils/utils.go b/utils/utils.go index 98de104ba..2855e83ca 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -1,5 +1,5 @@ // ------------------------------------------------------------ -// Copyright (c) Microsoft Corporation. +// Copyright (c) Microsoft Corporation and Dapr Contributors. // Licensed under the MIT License. // ------------------------------------------------------------ @@ -8,6 +8,7 @@ package utils import ( "bufio" "context" + "encoding/json" "errors" "fmt" "io" @@ -15,11 +16,14 @@ import ( "net" "os" "os/exec" + "reflect" "strings" "time" "github.com/docker/docker/client" + "github.com/gocarina/gocsv" "github.com/olekukonko/tablewriter" + "gopkg.in/yaml.v2" ) // PrintTable to print in the table format. @@ -147,3 +151,37 @@ func IsDaprListeningOnPort(port int, timeout time.Duration) error { time.Sleep(time.Second) } } + +func MarshalAndWriteTable(writer io.Writer, in interface{}) error { + table, err := gocsv.MarshalString(in) + if err != nil { + return err + } + + WriteTable(writer, table) + return nil +} + +func PrintDetail(writer io.Writer, outputFormat string, list interface{}) error { + obj := list + s := reflect.ValueOf(list) + if s.Kind() == reflect.Slice && s.Len() == 1 { + obj = s.Index(0).Interface() + } + + var err error + output := []byte{} + + switch outputFormat { + case "yaml": + output, err = yaml.Marshal(obj) + case "json": + output, err = json.MarshalIndent(obj, "", " ") + } + if err != nil { + return err + } + + _, err = writer.Write(output) + return err +}