nicolaferraro closed pull request #4: Minor refactor of client cmd
URL: https://github.com/apache/camel-k/pull/4
 
 
   

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/completion.go b/pkg/client/cmd/completion.go
new file mode 100644
index 0000000..3429dad
--- /dev/null
+++ b/pkg/client/cmd/completion.go
@@ -0,0 +1,46 @@
+/*
+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 cmd
+
+import (
+       "os"
+
+       "github.com/spf13/cobra"
+)
+
+const completionCmdLongDescription = `
+To load completion run
+
+. <(kamel completion)
+
+To configure your bash shell to load completions for each session add to your 
bashrc
+
+# ~/.bashrc or ~/.profile
+. <(kamel completion)
+`
+
+func NewCmdCompletion() *cobra.Command {
+       return &cobra.Command{
+               Use:   "completion",
+               Short: "Generates bash completion scripts",
+               Long:  completionCmdLongDescription,
+               Run: func(cmd *cobra.Command, args []string) {
+                       cmd.GenBashCompletion(os.Stdout)
+               },
+       }
+}
diff --git a/pkg/client/cmd/config.go b/pkg/client/cmd/config.go
index 590b02d..f7b347b 100644
--- a/pkg/client/cmd/config.go
+++ b/pkg/client/cmd/config.go
@@ -18,17 +18,23 @@ limitations under the License.
 package cmd
 
 import (
+       "os/user"
        "path/filepath"
-       "k8s.io/client-go/tools/clientcmd"
-       "os"
-       "github.com/spf13/cobra"
+
        "github.com/operator-framework/operator-sdk/pkg/k8sclient"
+       "github.com/spf13/cobra"
+       "k8s.io/client-go/tools/clientcmd"
 )
 
 func initKubeClient(cmd *cobra.Command) error {
        kubeconfig := cmd.Flag("config").Value.String()
        if kubeconfig == "" {
-               kubeconfig = filepath.Join(homeDir(), ".kube", "config")
+               usr, err := user.Current()
+               if err != nil {
+                       return err
+               }
+
+               kubeconfig = filepath.Join(usr.HomeDir, ".kube", "config")
        }
 
        // use the current context in kubeconfig
@@ -40,10 +46,3 @@ func initKubeClient(cmd *cobra.Command) error {
        k8sclient.CustomConfig = config
        return nil
 }
-
-func homeDir() string {
-       if h := os.Getenv("HOME"); h != "" {
-               return h
-       }
-       return os.Getenv("USERPROFILE") // windows
-}
diff --git a/pkg/client/cmd/cmd.go b/pkg/client/cmd/root.go
similarity index 70%
rename from pkg/client/cmd/cmd.go
rename to pkg/client/cmd/root.go
index be575a3..e49da28 100644
--- a/pkg/client/cmd/cmd.go
+++ b/pkg/client/cmd/root.go
@@ -18,24 +18,9 @@ limitations under the License.
 package cmd
 
 import (
-       "os"
-
-       "github.com/apache/camel-k/pkg/client/cmd/run"
-       "github.com/apache/camel-k/pkg/client/cmd/version"
        "github.com/spf13/cobra"
 )
 
-const completionCmdLongDescription = `
-To load completion run
-
-. <(kamel completion)
-
-To configure your bash shell to load completions for each session add to your 
bashrc
-
-# ~/.bashrc or ~/.profile
-. <(kamel completion)
-`
-
 func NewKamelCommand() (*cobra.Command, error) {
        var cmd = cobra.Command{
                Use:   "kamel",
@@ -52,17 +37,9 @@ func NewKamelCommand() (*cobra.Command, error) {
                return nil, err
        }
 
-       cmd.AddCommand(&cobra.Command{
-               Use:   "completion",
-               Short: "Generates bash completion scripts",
-               Long:  completionCmdLongDescription,
-               Run: func(cmd *cobra.Command, args []string) {
-                       cmd.GenBashCompletion(os.Stdout)
-               },
-       })
-
-       cmd.AddCommand(version.NewCmdVersion())
-       cmd.AddCommand(run.NewCmdRun())
+       cmd.AddCommand(NewCmdCompletion())
+       cmd.AddCommand(NewCmdVersion())
+       cmd.AddCommand(NewCmdRun())
 
        return &cmd, nil
 }
diff --git a/pkg/client/cmd/run/run.go b/pkg/client/cmd/run.go
similarity index 84%
rename from pkg/client/cmd/run/run.go
rename to pkg/client/cmd/run.go
index fd6df53..ca0872a 100644
--- a/pkg/client/cmd/run/run.go
+++ b/pkg/client/cmd/run.go
@@ -15,42 +15,44 @@ See the License for the specific language governing 
permissions and
 limitations under the License.
 */
 
-package run
+package cmd
 
 import (
-       "github.com/spf13/cobra"
        "errors"
-       "strconv"
+       "fmt"
+       "io/ioutil"
        "os"
-       "github.com/operator-framework/operator-sdk/pkg/sdk"
+       "strconv"
+
        "github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
-       "k8s.io/apimachinery/pkg/apis/meta/v1"
-       "io/ioutil"
-       k8serrors "k8s.io/apimachinery/pkg/api/errors"
        "github.com/apache/camel-k/pkg/util/kubernetes"
-       "fmt"
+       "github.com/operator-framework/operator-sdk/pkg/sdk"
+       "github.com/spf13/cobra"
+       k8serrors "k8s.io/apimachinery/pkg/api/errors"
+       "k8s.io/apimachinery/pkg/apis/meta/v1"
 )
 
-type runCmdFlags struct {
-       language string
-}
-
 func NewCmdRun() *cobra.Command {
-       flags := runCmdFlags{}
+       impl := runCmd{}
+
        cmd := cobra.Command{
                Use:   "run [file to run]",
                Short: "Run a integration on Kubernetes",
                Long:  `Deploys and execute a integration pod on Kubernetes.`,
-               Args: validateArgs,
-               RunE: run,
+               Args:  impl.validateArgs,
+               RunE:  impl.execute,
        }
 
-       cmd.Flags().StringVarP(&flags.language, "language", "l", "", 
"Programming Language used to write the file")
+       cmd.Flags().StringVarP(&impl.language, "language", "l", "", 
"Programming Language used to write the file")
 
        return &cmd
 }
 
-func validateArgs(cmd *cobra.Command, args []string) error {
+type runCmd struct {
+       language string
+}
+
+func (target runCmd) validateArgs(cmd *cobra.Command, args []string) error {
        if len(args) != 1 {
                return errors.New("accepts 1 arg, received " + 
strconv.Itoa(len(args)))
        }
@@ -63,8 +65,8 @@ func validateArgs(cmd *cobra.Command, args []string) error {
        return nil
 }
 
-func run(cmd *cobra.Command, args []string) error {
-       code, err := loadCode(args[0])
+func (target runCmd) execute(cmd *cobra.Command, args []string) error {
+       code, err := target.loadCode(args[0])
        if err != nil {
                return err
        }
@@ -76,12 +78,12 @@ func run(cmd *cobra.Command, args []string) error {
 
        integration := v1alpha1.Integration{
                TypeMeta: v1.TypeMeta{
-                       Kind: "Integration",
+                       Kind:       "Integration",
                        APIVersion: v1alpha1.SchemeGroupVersion.String(),
                },
                ObjectMeta: v1.ObjectMeta{
                        Namespace: "test", // TODO discover current namespace 
dynamically (and with command option)
-                       Name: name,
+                       Name:      name,
                },
                Spec: v1alpha1.IntegrationSpec{
                        Source: v1alpha1.SourceSpec{
@@ -115,7 +117,7 @@ func run(cmd *cobra.Command, args []string) error {
        return nil
 }
 
-func loadCode(fileName string) (string, error) {
+func (target runCmd) loadCode(fileName string) (string, error) {
        content, err := ioutil.ReadFile(fileName)
        if err != nil {
                return "", err
diff --git a/pkg/client/cmd/version/version.go b/pkg/client/cmd/version.go
similarity index 98%
rename from pkg/client/cmd/version/version.go
rename to pkg/client/cmd/version.go
index 5eadbfb..1424a37 100644
--- a/pkg/client/cmd/version/version.go
+++ b/pkg/client/cmd/version.go
@@ -15,15 +15,15 @@ See the License for the specific language governing 
permissions and
 limitations under the License.
 */
 
-package version
+package cmd
 
 import (
-       "github.com/spf13/cobra"
        "fmt"
+
        rootVersion "github.com/apache/camel-k/version"
+       "github.com/spf13/cobra"
 )
 
-
 func NewCmdVersion() *cobra.Command {
        return &cobra.Command{
                Use:   "version",
@@ -34,4 +34,3 @@ func NewCmdVersion() *cobra.Command {
                },
        }
 }
-


 

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to