You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by nf...@apache.org on 2020/10/21 21:27:39 UTC

[camel-k] 04/13: Add output flag to both yaml and json formats.

This is an automated email from the ASF dual-hosted git repository.

nferraro pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-k.git

commit 8667fdaaab92a667eea05775d88df9505e0a91b1
Author: Doru Bercea <gh...@ibm.com>
AuthorDate: Mon Oct 12 16:18:57 2020 -0400

    Add output flag to both yaml and json formats.
---
 pkg/cmd/inspect.go          | 34 ++++++++++++++++++++++++++++++++--
 pkg/util/kubernetes/util.go | 19 ++-----------------
 pkg/util/util.go            | 34 ++++++++++++++++++++++++++++++++++
 3 files changed, 68 insertions(+), 19 deletions(-)

diff --git a/pkg/cmd/inspect.go b/pkg/cmd/inspect.go
index c26cbb6..37d5b70 100644
--- a/pkg/cmd/inspect.go
+++ b/pkg/cmd/inspect.go
@@ -53,6 +53,7 @@ func newCmdInspect(rootCmdOptions *RootCmdOptions) (*cobra.Command, *inspectCmdO
 		},
 	}
 
+	cmd.Flags().StringP("output", "o", "", "Output format. One of: json|yaml")
 	// TODO: support the following options:
 	cmd.Flags().Bool("all-dependencies", false, "Include both top level and transitive dependencies.")
 	cmd.Flags().String("dependencies-directory", "", "If set, directory will contain all integration dependencies.")
@@ -64,6 +65,7 @@ func newCmdInspect(rootCmdOptions *RootCmdOptions) (*cobra.Command, *inspectCmdO
 type inspectCmdOptions struct {
 	*RootCmdOptions
 	AllDependencies        bool   `mapstructure:"all-dependencies"`
+	OutputFormat           string `mapstructure:"output"`
 	DependenciesDirectory  string `mapstructure:"dependencies-directory"`
 	AdditionalDependencies string `mapstructure:"additional-dependencies"`
 }
@@ -150,8 +152,16 @@ func (command *inspectCmdOptions) run(args []string) error {
 		dependencies.Merge(trait.AddSourceDependencies(sourceSpec, catalog))
 	}
 
-	for _, dep := range dependencies.List() {
-		fmt.Printf("%v\n", dep)
+	if command.OutputFormat != "" {
+		err := printDependencies(command.OutputFormat, dependencies)
+		if err != nil {
+			return err
+		}
+	} else {
+		// Print output in text form.
+		for _, dep := range dependencies.List() {
+			fmt.Printf("%v\n", dep)
+		}
 	}
 
 	return nil
@@ -175,3 +185,23 @@ func generateCatalog() (*camel.RuntimeCatalog, error) {
 
 	return catalog, nil
 }
+
+func printDependencies(format string, dependecies *strset.Set) error {
+	switch format {
+	case "yaml":
+		data, err := util.DependenciesToYAML(dependecies.List())
+		if err != nil {
+			return err
+		}
+		fmt.Print(string(data))
+	case "json":
+		data, err := util.DependenciesToJSON(dependecies.List())
+		if err != nil {
+			return err
+		}
+		fmt.Print(string(data))
+	default:
+		return errors.New("unknown output format: " + format)
+	}
+	return nil
+}
diff --git a/pkg/util/kubernetes/util.go b/pkg/util/kubernetes/util.go
index 9fd960a..0ce1517 100644
--- a/pkg/util/kubernetes/util.go
+++ b/pkg/util/kubernetes/util.go
@@ -29,7 +29,7 @@ import (
 
 	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
 	"github.com/apache/camel-k/pkg/client"
-	yaml2 "gopkg.in/yaml.v2"
+	"github.com/apache/camel-k/pkg/util"
 )
 
 // ToJSON --
@@ -44,22 +44,7 @@ func ToYAML(value runtime.Object) ([]byte, error) {
 		return nil, err
 	}
 
-	return JSONToYAML(data)
-}
-
-// JSONToYAML --
-func JSONToYAML(src []byte) ([]byte, error) {
-	jsondata := map[string]interface{}{}
-	err := json.Unmarshal(src, &jsondata)
-	if err != nil {
-		return nil, fmt.Errorf("error unmarshalling json: %v", err)
-	}
-	yamldata, err := yaml2.Marshal(&jsondata)
-	if err != nil {
-		return nil, fmt.Errorf("error marshalling to yaml: %v", err)
-	}
-
-	return yamldata, nil
+	return util.JSONToYAML(data)
 }
 
 // GetConfigMap --
diff --git a/pkg/util/util.go b/pkg/util/util.go
index 7b76d88..1ecff9b 100644
--- a/pkg/util/util.go
+++ b/pkg/util/util.go
@@ -19,6 +19,7 @@ package util
 
 import (
 	"bytes"
+	"encoding/json"
 	"encoding/xml"
 	"fmt"
 	"io"
@@ -31,6 +32,7 @@ import (
 	"github.com/pkg/errors"
 
 	"github.com/scylladb/go-set/strset"
+	yaml2 "gopkg.in/yaml.v2"
 )
 
 // StringSliceJoin --
@@ -273,3 +275,35 @@ func SortedStringMapKeys(m map[string]string) []string {
 	sort.Strings(res)
 	return res
 }
+
+// DependenciesToJSON --
+func DependenciesToJSON(list []string) ([]byte, error) {
+	jsondata := map[string]interface{}{}
+	jsondata["dependencies"] = list
+	return json.Marshal(jsondata)
+}
+
+// DependenciesToYAML --
+func DependenciesToYAML(list []string) ([]byte, error) {
+	data, err := DependenciesToJSON(list)
+	if err != nil {
+		return nil, err
+	}
+
+	return JSONToYAML(data)
+}
+
+// JSONToYAML --
+func JSONToYAML(src []byte) ([]byte, error) {
+	jsondata := map[string]interface{}{}
+	err := json.Unmarshal(src, &jsondata)
+	if err != nil {
+		return nil, fmt.Errorf("error unmarshalling json: %v", err)
+	}
+	yamldata, err := yaml2.Marshal(&jsondata)
+	if err != nil {
+		return nil, fmt.Errorf("error marshalling to yaml: %v", err)
+	}
+
+	return yamldata, nil
+}