Skip to content

Commit

Permalink
chore(lint): fix cyclomatic complexiti on cmd::run::updateIntegration…
Browse files Browse the repository at this point in the history
…Code
  • Loading branch information
lburgazzoli committed Dec 10, 2018
1 parent 9ad6974 commit d51bb0d
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 67 deletions.
24 changes: 24 additions & 0 deletions pkg/apis/camel/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ limitations under the License.
package v1alpha1

import (
"strings"

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

"github.com/mitchellh/mapstructure"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -69,6 +73,26 @@ func (is *IntegrationSpec) AddSources(sources ...SourceSpec) {
is.Sources = append(is.Sources, sources...)
}

// AddConfiguration --
func (is *IntegrationSpec) AddConfiguration(confType string, confValue string) {
is.Configuration = append(is.Configuration, ConfigurationSpec{
Type: confType,
Value: confValue,
})
}

// AddDependency --
func (is *IntegrationSpec) AddDependency(dependency string) {
switch {
case strings.HasPrefix(dependency, "mvn:"):
util.StringSliceUniqueAdd(&is.Dependencies, dependency)
case strings.HasPrefix(dependency, "file:"):
util.StringSliceUniqueAdd(&is.Dependencies, dependency)
case strings.HasPrefix(dependency, "camel-"):
util.StringSliceUniqueAdd(&is.Dependencies, "camel:"+strings.TrimPrefix(dependency, "camel-"))
}
}

// SourceSpec --
type SourceSpec struct {
Name string `json:"name,omitempty"`
Expand Down
79 changes: 12 additions & 67 deletions pkg/client/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package cmd
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
Expand All @@ -34,11 +33,6 @@ import (

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

"github.com/operator-framework/operator-sdk/pkg/util/k8sutil"
"gopkg.in/yaml.v2"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"

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

Expand Down Expand Up @@ -257,7 +251,6 @@ func (o *runCmdOptions) createIntegration(args []string) (*v1alpha1.Integration,
return o.updateIntegrationCode(args)
}

// nolint: gocyclo
func (o *runCmdOptions) updateIntegrationCode(sources []string) (*v1alpha1.Integration, error) {
namespace := o.Namespace

Expand Down Expand Up @@ -314,44 +307,24 @@ func (o *runCmdOptions) updateIntegrationCode(sources []string) (*v1alpha1.Integ
})
}

for _, item := range o.Dependencies {
switch {
case strings.HasPrefix(item, "mvn:"):
integration.Spec.Dependencies = append(integration.Spec.Dependencies, item)
case strings.HasPrefix(item, "file:"):
integration.Spec.Dependencies = append(integration.Spec.Dependencies, item)
case strings.HasPrefix(item, "camel-"):
integration.Spec.Dependencies = append(integration.Spec.Dependencies, "camel:"+strings.TrimPrefix(item, "camel-"))
}
}

if o.Runtime != "" {
util.StringSliceUniqueAdd(&integration.Spec.Dependencies, "runtime:"+o.Runtime)
integration.Spec.AddDependency("runtime:" + o.Runtime)
}

for _, item := range o.Dependencies {
integration.Spec.AddDependency(item)
}
for _, item := range o.Properties {
integration.Spec.Configuration = append(integration.Spec.Configuration, v1alpha1.ConfigurationSpec{
Type: "property",
Value: item,
})
integration.Spec.AddConfiguration("property", item)
}
for _, item := range o.LoggingLevels {
integration.Spec.Configuration = append(integration.Spec.Configuration, v1alpha1.ConfigurationSpec{
Type: "property",
Value: "logging.level." + item,
})
integration.Spec.AddConfiguration("property", "logging.level."+item)
}
for _, item := range o.ConfigMaps {
integration.Spec.Configuration = append(integration.Spec.Configuration, v1alpha1.ConfigurationSpec{
Type: "configmap",
Value: item,
})
integration.Spec.AddConfiguration("configmap", item)
}
for _, item := range o.Secrets {
integration.Spec.Configuration = append(integration.Spec.Configuration, v1alpha1.ConfigurationSpec{
Type: "secret",
Value: item,
})
integration.Spec.AddConfiguration("secret", item)
}

for _, traitConf := range o.Traits {
Expand All @@ -364,19 +337,16 @@ func (o *runCmdOptions) updateIntegrationCode(sources []string) (*v1alpha1.Integ
case "":
// continue..
case "yaml":
jsondata, err := toJSON(&integration)
if err != nil {
return nil, err
}
yamldata, err := jsonToYaml(jsondata)
data, err := kubernetes.ToYAML(&integration)
if err != nil {
return nil, err
}
fmt.Print(string(yamldata))

fmt.Print(string(data))
return nil, nil

case "json":
data, err := toJSON(&integration)
data, err := kubernetes.ToJSON(&integration)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -412,31 +382,6 @@ func (o *runCmdOptions) updateIntegrationCode(sources []string) (*v1alpha1.Integ
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)
}
data, err := runtime.Encode(unstructured.UnstructuredJSONScheme, u)
if err != nil {
return nil, fmt.Errorf("error marshalling to json: %v", 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)
Expand Down
66 changes: 66 additions & 0 deletions pkg/util/kubernetes/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package kubernetes

import (
"encoding/json"
"fmt"

"github.com/operator-framework/operator-sdk/pkg/util/k8sutil"
yaml "gopkg.in/yaml.v2"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
)

// ToJSON --
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)
}
data, err := runtime.Encode(unstructured.UnstructuredJSONScheme, u)
if err != nil {
return nil, fmt.Errorf("error marshalling to json: %v", err)
}
return data, nil
}

// ToYAML --
func ToYAML(value runtime.Object) ([]byte, error) {
data, err := ToJSON(value)
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 := yaml.Marshal(&jsondata)
if err != nil {
return nil, fmt.Errorf("error marshalling to yaml: %v", err)
}

return yamldata, nil
}

0 comments on commit d51bb0d

Please sign in to comment.