You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by lb...@apache.org on 2018/10/08 14:19:49 UTC

[camel-k] 09/14: Added trait command line config

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

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

commit 6243fb71147e8aa57cef4d46a1b4e5d02ec5c10a
Author: nferraro <ni...@gmail.com>
AuthorDate: Fri Oct 5 00:01:34 2018 +0200

    Added trait command line config
---
 pkg/client/cmd/run.go | 43 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)

diff --git a/pkg/client/cmd/run.go b/pkg/client/cmd/run.go
index 10e75ed..afd9667 100644
--- a/pkg/client/cmd/run.go
+++ b/pkg/client/cmd/run.go
@@ -22,6 +22,7 @@ import (
 	"io/ioutil"
 	"net/http"
 	"os"
+	"regexp"
 	"strconv"
 	"strings"
 
@@ -43,6 +44,10 @@ import (
 	"k8s.io/apimachinery/pkg/apis/meta/v1"
 )
 
+var (
+	traitConfigRegexp = regexp.MustCompile("^([a-z-]+)((?:\\.[a-z-]+)+)=(.*)$")
+)
+
 func newCmdRun(rootCmdOptions *RootCmdOptions) *cobra.Command {
 	options := runCmdOptions{
 		RootCmdOptions: rootCmdOptions,
@@ -69,6 +74,7 @@ func newCmdRun(rootCmdOptions *RootCmdOptions) *cobra.Command {
 	cmd.Flags().BoolVar(&options.Sync, "sync", false, "Synchronize the local source file with the cluster, republishing at each change")
 	cmd.Flags().BoolVar(&options.Dev, "dev", false, "Enable Dev mode (equivalent to \"-w --logs --sync\")")
 	cmd.Flags().BoolVar(&options.DependenciesAutoDiscovery, "auto-discovery", true, "Automatically discover Camel modules by analyzing user code")
+	cmd.Flags().StringSliceVarP(&options.Traits, "trait", "t", nil, "Configure a trait. E.g. \"-t service.enabled=false\"")
 
 	// completion support
 	configureKnownCompletions(&cmd)
@@ -91,6 +97,7 @@ type runCmdOptions struct {
 	Sync                      bool
 	Dev                       bool
 	DependenciesAutoDiscovery bool
+	Traits                    []string
 }
 
 func (*runCmdOptions) validateArgs(cmd *cobra.Command, args []string) error {
@@ -318,6 +325,10 @@ func (o *runCmdOptions) updateIntegrationCode(filename string) (*v1alpha1.Integr
 		})
 	}
 
+	for _, traitConf := range o.Traits {
+		o.configureTrait(&integration, traitConf)
+	}
+
 	existed := false
 	err = sdk.Create(&integration)
 	if err != nil && k8serrors.IsAlreadyExists(err) {
@@ -361,3 +372,35 @@ func (*runCmdOptions) loadCode(fileName string) (string, error) {
 	bodyString := string(bodyBytes)
 	return string(bodyString), err
 }
+
+func (*runCmdOptions) configureTrait(integration *v1alpha1.Integration, config string) error {
+	if integration.Spec.Traits == nil {
+		integration.Spec.Traits = make(map[string]v1alpha1.IntegrationTraitSpec)
+	}
+
+	parts := traitConfigRegexp.FindStringSubmatch(config)
+	if len(parts) < 4 {
+		return errors.New("unrecognized config format (expected \"<trait>.<prop>=<val>\"): " + config)
+	}
+	traitID := parts[1]
+	prop := parts[2][1:]
+	val := parts[3]
+	var spec v1alpha1.IntegrationTraitSpec
+	var ok bool
+	if spec, ok = integration.Spec.Traits[traitID]; !ok {
+		spec = v1alpha1.IntegrationTraitSpec{
+			Configuration: make(map[string]string),
+		}
+	}
+	if prop == "enabled" {
+		boolVal, err := strconv.ParseBool(val)
+		if err != nil {
+			return errors.Wrap(err, "cannot parse bool value "+val)
+		}
+		spec.Enabled = &boolVal
+	} else {
+		spec.Configuration[prop] = val
+	}
+	integration.Spec.Traits[traitID] = spec
+	return nil
+}