You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@camel.apache.org by GitBox <gi...@apache.org> on 2018/11/08 17:12:46 UTC

[GitHub] nicolaferraro closed pull request #208: feat: #207 Provide option to get the Json/Yaml of a integration

nicolaferraro closed pull request #208: feat: #207 Provide option to get the Json/Yaml of a integration
URL: https://github.com/apache/camel-k/pull/208
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/pkg/client/cmd/run.go b/pkg/client/cmd/run.go
index 6a52e03c..01439b58 100644
--- a/pkg/client/cmd/run.go
+++ b/pkg/client/cmd/run.go
@@ -18,8 +18,13 @@ limitations under the License.
 package cmd
 
 import (
+	"encoding/json"
 	"fmt"
+	"github.com/operator-framework/operator-sdk/pkg/util/k8sutil"
+	"gopkg.in/yaml.v2"
 	"io/ioutil"
+	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
+	"k8s.io/apimachinery/pkg/runtime"
 	"net/http"
 	"os"
 	"os/signal"
@@ -76,6 +81,7 @@ func newCmdRun(rootCmdOptions *RootCmdOptions) *cobra.Command {
 	cmd.Flags().BoolVar(&options.Dev, "dev", false, "Enable Dev mode (equivalent to \"-w --logs --sync\")")
 	cmd.Flags().StringSliceVarP(&options.Traits, "trait", "t", nil, "Configure a trait. E.g. \"-t service.enabled=false\"")
 	cmd.Flags().StringSliceVar(&options.LoggingLevels, "logging-level", nil, "Configure the logging level. E.g. \"--logging-level org.apache.camel=DEBUG\"")
+	cmd.Flags().StringVarP(&options.OutputFormat, "output", "o", "", "Output format. One of: json|yaml")
 
 	// completion support
 	configureKnownCompletions(&cmd)
@@ -99,6 +105,7 @@ type runCmdOptions struct {
 	Dev                bool
 	Traits             []string
 	LoggingLevels      []string
+	OutputFormat       string
 }
 
 func (o *runCmdOptions) validateArgs(cmd *cobra.Command, args []string) error {
@@ -322,6 +329,33 @@ func (o *runCmdOptions) updateIntegrationCode(filename string) (*v1alpha1.Integr
 		}
 	}
 
+	switch o.OutputFormat {
+	case "":
+		// continue..
+	case "yaml":
+		jsondata, err := toJson(&integration)
+		if err != nil {
+			return nil, err
+		}
+		yamldata, err := jsonToYaml(jsondata)
+		if err != nil {
+			return nil, err
+		}
+		fmt.Print(string(yamldata))
+		return nil, nil
+
+	case "json":
+		data, err := toJson(&integration)
+		if err != nil {
+			return nil, err
+		}
+		fmt.Print(string(data))
+		return nil, nil
+
+	default:
+		return nil, fmt.Errorf("invalid output format option '%s', should be one of: yaml|json", o.OutputFormat)
+	}
+
 	existed := false
 	err = sdk.Create(&integration)
 	if err != nil && k8serrors.IsAlreadyExists(err) {
@@ -347,6 +381,33 @@ func (o *runCmdOptions) updateIntegrationCode(filename string) (*v1alpha1.Integr
 	return &integration, nil
 }
 
+func toJson(value runtime.Object) ([]byte, error) {
+	u, err := k8sutil.UnstructuredFromRuntimeObject(value)
+	if err != nil {
+		return nil, fmt.Errorf("error creating unstructured data: %v", err)
+		return nil, err
+	}
+	data, err := runtime.Encode(unstructured.UnstructuredJSONScheme, u)
+	if err != nil {
+		return nil, fmt.Errorf("error marshalling to json: %v", err)
+		return nil, err
+	}
+	return data, nil
+}
+
+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 := yaml.Marshal(&jsondata)
+	if err != nil {
+		return nil, fmt.Errorf("error marshalling to yaml: %v", err)
+	}
+	return yamldata, nil
+}
+
 func (*runCmdOptions) loadCode(fileName string) (string, error) {
 	if !strings.HasPrefix(fileName, "http://") && !strings.HasPrefix(fileName, "https://") {
 		content, err := ioutil.ReadFile(fileName)


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services