Skip to content

Commit

Permalink
cmd(kamel): validate traits
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Oct 11, 2018
1 parent 223dc77 commit fb115e8
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 41 deletions.
36 changes: 2 additions & 34 deletions pkg/client/cmd/completion_bash.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ package cmd

import (
"os"
"reflect"
"strings"

"github.com/apache/camel-k/pkg/trait"

"github.com/apache/camel-k/pkg/util/camel"
"github.com/fatih/structs"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -70,7 +69,7 @@ __kamel_dependency_type() {
}
__kamel_traits() {
local type_list="` + computeTraits() + `"
local type_list="` + strings.Join(trait.ComputeTraitsProperties(), " ") + `"
COMPREPLY=( $( compgen -W "${type_list}" -- "$cur") )
compopt -o nospace
}
Expand Down Expand Up @@ -242,34 +241,3 @@ func computeCamelDependencies() string {

return strings.Join(results, " ")
}

func computeTraits() string {
results := make([]string, 0)

for _, t := range trait.UserFacing {
processFields(structs.Fields(t), func(name string) {
results = append(results, string(t.ID())+"."+name)
})
}

return strings.Join(results, " ")
}

func processFields(fields []*structs.Field, processor func(string)) {
for _, f := range fields {
if f.IsEmbedded() && f.IsExported() && f.Kind() == reflect.Struct {
processFields(f.Fields(), processor)
}

if f.IsEmbedded() {
continue
}

property := f.Tag("property")

if property != "" {
items := strings.Split(property, ",")
processor(items[0])
}
}
}
13 changes: 12 additions & 1 deletion pkg/client/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"strconv"
"strings"

"github.com/apache/camel-k/pkg/trait"

"github.com/apache/camel-k/pkg/util"

"github.com/apache/camel-k/pkg/util/sync"
Expand Down Expand Up @@ -100,7 +102,7 @@ type runCmdOptions struct {
Traits []string
}

func (*runCmdOptions) validateArgs(cmd *cobra.Command, args []string) error {
func (o *runCmdOptions) validateArgs(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("accepts 1 arg, received " + strconv.Itoa(len(args)))
}
Expand All @@ -119,10 +121,19 @@ func (*runCmdOptions) validateArgs(cmd *cobra.Command, args []string) error {
return errors.New("The URL provided is not reachable " + fileName + " The error code returned is " + strconv.Itoa(resp.StatusCode))
}
}

return nil
}

func (o *runCmdOptions) run(cmd *cobra.Command, args []string) error {
tp := trait.ComputeTraitsProperties()
for _, t := range o.Traits {
if !util.StringSliceExists(tp, t) {
fmt.Printf("Error: %s is not a valid trait property\n", t)
return nil
}
}

integration, err := o.createIntegration(cmd, args)
if err != nil {
return err
Expand Down
5 changes: 0 additions & 5 deletions pkg/trait/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ var (
tService = newServiceTrait()
tRoute = newRouteTrait()
tOwner = newOwnerTrait()

// UserFacing is list of user facing services traits
UserFacing = []Identifiable{
&tService,
}
)

// customizersFor returns a Catalog for the given integration details
Expand Down
35 changes: 34 additions & 1 deletion pkg/trait/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ package trait

import (
"fmt"
"reflect"
"strings"

"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
"github.com/fatih/structs"
"github.com/operator-framework/operator-sdk/pkg/sdk"
"github.com/pkg/errors"
"k8s.io/api/core/v1"
"strings"
)

// GetIntegrationContext retrieves the context set on the integration
Expand Down Expand Up @@ -115,3 +118,33 @@ func CombineConfigurationAsSlice(configurationType string, context *v1alpha1.Int

return keys
}

// ComputeTraitsProperties --
func ComputeTraitsProperties() []string {
results := make([]string, 0)

processFields(structs.Fields(tService), func(name string) {
results = append(results, string(tService.ID())+"."+name)
})

return results
}

func processFields(fields []*structs.Field, processor func(string)) {
for _, f := range fields {
if f.IsEmbedded() && f.IsExported() && f.Kind() == reflect.Struct {
processFields(f.Fields(), processor)
}

if f.IsEmbedded() {
continue
}

property := f.Tag("property")

if property != "" {
items := strings.Split(property, ",")
processor(items[0])
}
}
}

0 comments on commit fb115e8

Please sign in to comment.