[camel-k] branch master updated: feat: #207 Provide option to get the Json/Yaml of a integration

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[camel-k] branch master updated: feat: #207 Provide option to get the Json/Yaml of a integration

nferraro
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


The following commit(s) were added to refs/heads/master by this push:
     new 434b3d9  feat: #207 Provide option to get the Json/Yaml of a integration
434b3d9 is described below

commit 434b3d93cd88fc5c0e7d3695c443fcb5a48a5b3a
Author: Hiram Chirino <[hidden email]>
AuthorDate: Thu Nov 8 11:17:09 2018 -0500

    feat: #207 Provide option to get the Json/Yaml of a integration
   
    Add an option to the run command:
   
      -o, --output string           Output format. One of: json|yaml
---
 pkg/client/cmd/run.go | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)

diff --git a/pkg/client/cmd/run.go b/pkg/client/cmd/run.go
index 6a52e03..01439b5 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)