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

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

commit a61394e1911c2871db04f3305f4b6f55ecdfde27
Author: Tadayoshi Sato <[email protected]>
AuthorDate: Fri Jul 29 14:10:52 2022 +0900

    chore(cli): further refactor kamel local cmds
---
 pkg/cmd/local_build.go          |  14 ++-
 pkg/cmd/local_inspect.go        |  11 ++-
 pkg/cmd/local_run.go            | 183 +++++++++++++++++++++-------------------
 pkg/cmd/local_util.go           |  69 +++++++--------
 pkg/cmd/local_util_container.go |   5 +-
 pkg/util/util.go                |  15 ----
 6 files changed, 147 insertions(+), 150 deletions(-)

diff --git a/pkg/cmd/local_build.go b/pkg/cmd/local_build.go
index bc506c27b..fba6d4d82 100644
--- a/pkg/cmd/local_build.go
+++ b/pkg/cmd/local_build.go
@@ -58,8 +58,10 @@ func newCmdLocalBuild(localCmdOptions *LocalCmdOptions) 
(*cobra.Command, *localB
        }
 
        cmd.Flags().Bool("base-image", false, "Build base image used as a 
starting point for any integration.")
-       cmd.Flags().Bool("dependencies-only", false, "Only output the 
integration dependencies. The integration-directory flag must be set.")
-       cmd.Flags().String("container-registry", "", "Registry that holds 
intermediate images. This flag should only be used in conjunction with the 
base-image flag.")
+       cmd.Flags().Bool("dependencies-only", false,
+               "Only output the integration dependencies. The 
integration-directory flag must be set.")
+       cmd.Flags().String("container-registry", "",
+               "Registry that holds intermediate images. This flag should only 
be used in conjunction with the base-image flag.")
        cmd.Flags().String("image", "", "Full path to integration image 
including registry.")
        cmd.Flags().String("integration-directory", "", "Directory to hold 
local integration files.")
        cmd.Flags().StringArray("property-file", nil, "Add a property file to 
the integration.")
@@ -84,7 +86,7 @@ type localBuildCmdOptions struct {
 func (o *localBuildCmdOptions) validate(args []string) error {
        // Validate integration files.
        if len(args) > 0 {
-               if err := validateIntegrationFiles(args); err != nil {
+               if err := validateFiles(args); err != nil {
                        return err
                }
        }
@@ -149,11 +151,7 @@ func (o *localBuildCmdOptions) init(args []string) error {
                }
        }
 
-       if err := createMavenWorkingDirectory(); err != nil {
-               return err
-       }
-
-       return nil
+       return createMavenWorkingDirectory()
 }
 
 func (o *localBuildCmdOptions) run(cmd *cobra.Command, args []string) error {
diff --git a/pkg/cmd/local_inspect.go b/pkg/cmd/local_inspect.go
index 1c180d56b..e5df5bd7b 100644
--- a/pkg/cmd/local_inspect.go
+++ b/pkg/cmd/local_inspect.go
@@ -18,6 +18,7 @@ limitations under the License.
 package cmd
 
 import (
+       "errors"
        "fmt"
 
        "github.com/spf13/cobra"
@@ -71,7 +72,12 @@ type localInspectCmdOptions struct {
 }
 
 func (o *localInspectCmdOptions) validate(args []string) error {
-       if err := validateIntegrationFiles(args); err != nil {
+       // If no source files have been provided there is nothing to inspect.
+       if len(args) == 0 {
+               return errors.New("no integration files have been provided")
+       }
+
+       if err := validateFiles(args); err != nil {
                return err
        }
 
@@ -92,8 +98,7 @@ func (o *localInspectCmdOptions) run(cmd *cobra.Command, args 
[]string) error {
                return err
        }
 
-       err = outputDependencies(dependencies, o.OutputFormat, cmd)
-       if err != nil {
+       if err = outputDependencies(dependencies, o.OutputFormat, cmd); err != 
nil {
                return err
        }
 
diff --git a/pkg/cmd/local_run.go b/pkg/cmd/local_run.go
index 62e53205a..d424a8e8c 100644
--- a/pkg/cmd/local_run.go
+++ b/pkg/cmd/local_run.go
@@ -74,7 +74,8 @@ func newCmdLocalRun(localCmdOptions *LocalCmdOptions) 
(*cobra.Command, *localRun
        cmd.Flags().Bool("containerize", false, "Run integration in a local 
container.")
        cmd.Flags().String("image", "", "Full path to integration image 
including registry.")
        cmd.Flags().String("network", "", "Custom network name to be used by 
the underlying Docker command.")
-       cmd.Flags().String("integration-directory", "", "Directory which holds 
the locally built integration and is the result of a local build action.")
+       cmd.Flags().String("integration-directory", "",
+               "Directory which holds the locally built integration and is the 
result of a local build action.")
        cmd.Flags().StringArrayP("env", "e", nil, "Flag to specify an 
environment variable [--env VARIABLE=value].")
        cmd.Flags().StringArray("property-file", nil, "Add a property file to 
the integration.")
        cmd.Flags().StringArrayP("property", "p", nil, "Add a Camel property to 
the integration.")
@@ -99,7 +100,12 @@ func (o *localRunCmdOptions) validate(args []string) error {
        // Validate integration files when no image is provided and we are
        // not running an already locally-built integration.
        if o.Image == "" && o.IntegrationDirectory == "" {
-               if err := validateIntegrationFiles(args); err != nil {
+               if len(args) == 0 {
+                       return errors.New("no integration files have been 
provided")
+               }
+
+               // Validate integration files.
+               if err := validateFiles(args); err != nil {
                        return err
                }
        }
@@ -144,129 +150,132 @@ func (o *localRunCmdOptions) run(cmd *cobra.Command, 
args []string) error {
        // If local run is provided with an image name, it will just run the 
image locally and exit.
        if o.Image != "" && !o.Containerize {
                // Run image locally.
-               if err := runIntegrationImage(o.Context, o.Image, 
cmd.OutOrStdout(), cmd.ErrOrStderr()); err != nil {
+               return runIntegrationImage(o.Context, o.Image, 
cmd.OutOrStdout(), cmd.ErrOrStderr())
+       }
+
+       dependencies, err := o.processDependencies(args)
+       if err != nil {
+               return err
+       }
+       propertyFiles, err := o.processPropertyFiles()
+       if err != nil {
+               return err
+       }
+       routes, err := o.processRoutes(args)
+       if err != nil {
+               return err
+       }
+
+       if o.Containerize {
+               // If this is a containerized local run, create, build and run 
the container image.
+               if err := createAndBuildIntegrationImage(o.Context, "", false, 
o.Image,
+                       propertyFiles, dependencies, routes, 
o.IntegrationDirectory != "",
+                       cmd.OutOrStdout(), cmd.ErrOrStderr()); err != nil {
                        return err
                }
 
-               return nil
+               // Run integration image.
+               return runIntegrationImage(o.Context, o.Image, 
cmd.OutOrStdout(), cmd.ErrOrStderr())
        }
 
-       hasIntegrationDir := o.IntegrationDirectory != ""
+       // Run integration locally.
+       return RunLocalIntegrationRunCommand(o.Context, propertyFiles, 
dependencies, routes, o.getPropertiesDir(),
+               cmd.OutOrStdout(), cmd.ErrOrStderr())
+}
 
-       var dependencies []string
-       if hasIntegrationDir {
-               // Fetch local dependencies
-               localBuildDependencies, err := 
getLocalBuildDependencies(o.IntegrationDirectory)
-               if err != nil {
-                       return err
-               }
-               dependencies = localBuildDependencies
+func (o *localRunCmdOptions) processDependencies(args []string) ([]string, 
error) {
+       if o.IntegrationDirectory == "" {
+               return getDependencies(o.Context, args, o.Dependencies, 
o.MavenRepositories, true)
+       }
 
-               // Local dependencies directory
-               localDependenciesDirectory := 
getCustomDependenciesDir(o.IntegrationDirectory)
+       // Set up on the integration directory
 
-               // The quarkus application files need to be at a specific 
location i.e.:
-               // <integration_directory>/../quarkus/quarkus-application.dat
-               // <integration_directory>/../quarkus/generated-bytecode.jar
-               localQuarkusDir := getCustomQuarkusDir(o.IntegrationDirectory)
-               err = copyQuarkusAppFiles(localDependenciesDirectory, 
localQuarkusDir)
-               if err != nil {
-                       return err
-               }
+       // Fetch local dependencies
+       dependencies, err := getLocalBuildDependencies(o.IntegrationDirectory)
+       if err != nil {
+               return nil, err
+       }
+       if err := o.setupDependenciesForQuarkusRun(); err != nil {
+               return dependencies, err
+       }
 
-               // The dependency jar files need to be at a specific location 
i.e.:
-               // <integration_directory>/../lib/main/*.jar
-               localLibDirectory := getCustomLibDir(o.IntegrationDirectory)
-               err = copyLibFiles(localDependenciesDirectory, 
localLibDirectory)
-               if err != nil {
-                       return err
-               }
+       return dependencies, nil
+}
 
-               // The Camel K jar file needs to be at a specific location i.e.:
-               // 
<integration_directory>/../app/camel-k-integration-X.X.X{-SNAPSHOT}.jar
-               localAppDirectory := getCustomAppDir(o.IntegrationDirectory)
-               err = copyAppFile(localDependenciesDirectory, localAppDirectory)
-               if err != nil {
-                       return err
-               }
-       } else {
-               computedDependencies, err := getDependencies(o.Context, args, 
o.Dependencies, o.MavenRepositories, true)
-               if err != nil {
-                       return err
-               }
-               dependencies = computedDependencies
+// setupDependenciesForQuarkusRun sets up resources under the integration 
directory for running Quarkus app.
+func (o *localRunCmdOptions) setupDependenciesForQuarkusRun() error {
+       // Local dependencies directory
+       localDependenciesDir := getCustomDependenciesDir(o.IntegrationDirectory)
+
+       // The quarkus application files need to be at a specific location i.e.:
+       // <integration_directory>/../quarkus/quarkus-application.dat
+       // <integration_directory>/../quarkus/generated-bytecode.jar
+       localQuarkusDir := getCustomQuarkusDir(o.IntegrationDirectory)
+       if err := copyQuarkusAppFiles(localDependenciesDir, localQuarkusDir); 
err != nil {
+               return err
        }
 
-       // Manage integration properties which may come from files or CLI.
-       propertyFiles := o.PropertyFiles
-       if hasIntegrationDir {
-               localBuildPropertyFiles, err := 
getLocalBuildProperties(o.IntegrationDirectory)
-               if err != nil {
-                       return err
-               }
-               propertyFiles = localBuildPropertyFiles
+       // The dependency jar files need to be at a specific location i.e.:
+       // <integration_directory>/../lib/main/*.jar
+       localLibDir := getCustomLibDir(o.IntegrationDirectory)
+       if err := copyLibFiles(localDependenciesDir, localLibDir); err != nil {
+               return err
        }
 
-       updatedPropertyFiles, err := updateIntegrationProperties(o.Properties, 
propertyFiles, hasIntegrationDir)
-       if err != nil {
+       // The Camel K jar file needs to be at a specific location i.e.:
+       // 
<integration_directory>/../app/camel-k-integration-X.X.X{-SNAPSHOT}.jar
+       localAppDir := getCustomAppDir(o.IntegrationDirectory)
+       if err := copyAppFile(localDependenciesDir, localAppDir); err != nil {
                return err
        }
-       propertyFiles = updatedPropertyFiles
 
-       routes := args
+       return nil
+}
+
+// processPropertyFiles processes integration properties which may come from 
files or CLI.
+func (o *localRunCmdOptions) processPropertyFiles() ([]string, error) {
+       propertyFiles := o.PropertyFiles
+       hasIntegrationDir := o.IntegrationDirectory != ""
        if hasIntegrationDir {
-               localBuildRoutes, err := 
getLocalBuildRoutes(o.IntegrationDirectory)
+               localPropertyFiles, err := 
getLocalBuildProperties(o.IntegrationDirectory)
                if err != nil {
-                       return err
+                       return nil, err
                }
-               routes = localBuildRoutes
+               propertyFiles = localPropertyFiles
        }
 
-       // If this is a containerized local run, create, build and run the 
container image.
-       if o.Containerize {
-               if err := createAndBuildIntegrationImage(o.Context, "", false, 
o.Image,
-                       propertyFiles, dependencies, routes, hasIntegrationDir,
-                       cmd.OutOrStdout(), cmd.ErrOrStderr()); err != nil {
-                       return err
-               }
+       return updateIntegrationProperties(o.Properties, propertyFiles, 
hasIntegrationDir)
+}
 
-               // Run integration image.
-               err = runIntegrationImage(o.Context, o.Image, 
cmd.OutOrStdout(), cmd.ErrOrStderr())
-               if err != nil {
-                       return err
-               }
-       } else {
-               propertiesDir := getLocalPropertiesDir()
-               if hasIntegrationDir {
-                       propertiesDir = 
getCustomPropertiesDir(o.IntegrationDirectory)
-               }
+func (o *localRunCmdOptions) processRoutes(args []string) ([]string, error) {
+       if o.IntegrationDirectory == "" {
+               return args, nil
+       }
 
-               // Run integration locally.
-               err := RunLocalIntegrationRunCommand(o.Context, propertyFiles, 
dependencies, routes, propertiesDir, cmd.OutOrStdout(), cmd.ErrOrStderr())
-               if err != nil {
-                       return err
-               }
+       return getLocalBuildRoutes(o.IntegrationDirectory)
+}
+
+func (o *localRunCmdOptions) getPropertiesDir() string {
+       if o.IntegrationDirectory == "" {
+               return getLocalPropertiesDir()
        }
 
-       return nil
+       return getCustomPropertiesDir(o.IntegrationDirectory)
 }
 
 func (o *localRunCmdOptions) deinit() error {
        if o.Containerize {
-               err := deleteDockerBaseWorkingDirectory()
-               if err != nil {
+               if err := deleteDockerBaseWorkingDirectory(); err != nil {
                        return err
                }
 
-               err = deleteDockerWorkingDirectory()
-               if err != nil {
+               if err := deleteDockerWorkingDirectory(); err != nil {
                        return err
                }
        }
 
        if o.IntegrationDirectory != "" {
-               err := deleteLocalIntegrationDirs(o.IntegrationDirectory)
-               if err != nil {
+               if err := deleteLocalIntegrationDirs(o.IntegrationDirectory); 
err != nil {
                        return err
                }
        }
diff --git a/pkg/cmd/local_util.go b/pkg/cmd/local_util.go
index bc0d26619..166489a7a 100644
--- a/pkg/cmd/local_util.go
+++ b/pkg/cmd/local_util.go
@@ -19,6 +19,7 @@ package cmd
 
 import (
        "context"
+       "encoding/json"
        "fmt"
        "io/ioutil"
        "os"
@@ -236,8 +237,7 @@ func createCamelCatalog(ctx context.Context) 
(*camel.RuntimeCatalog, error) {
 
 func outputDependencies(dependencies []string, format string, cmd 
*cobra.Command) error {
        if format != "" {
-               err := printDependencies(format, dependencies, cmd)
-               if err != nil {
+               if err := printDependencies(format, dependencies, cmd); err != 
nil {
                        return err
                }
        } else {
@@ -254,23 +254,38 @@ func outputDependencies(dependencies []string, format 
string, cmd *cobra.Command
 func printDependencies(format string, dependencies []string, cmd 
*cobra.Command) error {
        switch format {
        case "yaml":
-               data, err := util.DependenciesToYAML(dependencies)
+               data, err := dependenciesToYAML(dependencies)
                if err != nil {
                        return err
                }
                fmt.Fprint(cmd.OutOrStdout(), string(data))
        case "json":
-               data, err := util.DependenciesToJSON(dependencies)
+               data, err := dependenciesToJSON(dependencies)
                if err != nil {
                        return err
                }
                fmt.Fprint(cmd.OutOrStdout(), string(data))
        default:
-               return errors.New("unknown output format: " + format)
+               return errors.Errorf("unknown output format: %s", format)
        }
        return nil
 }
 
+func dependenciesToJSON(list []string) ([]byte, error) {
+       jsondata := map[string]interface{}{}
+       jsondata["dependencies"] = list
+       return json.Marshal(jsondata)
+}
+
+func dependenciesToYAML(list []string) ([]byte, error) {
+       data, err := dependenciesToJSON(list)
+       if err != nil {
+               return nil, err
+       }
+
+       return util.JSONToYAML(data)
+}
+
 func validateFile(file string) error {
        fileExists, err := util.FileExists(file)
        if err != nil {
@@ -310,20 +325,6 @@ func validateDependencies(dependencies []string) error {
        return nil
 }
 
-func validateIntegrationFiles(args []string) error {
-       // If no source files have been provided there is nothing to inspect.
-       if len(args) == 0 {
-               return errors.New("no integration files have been provided")
-       }
-
-       // Validate integration files.
-       if err := validateFiles(args); err != nil {
-               return err
-       }
-
-       return nil
-}
-
 func validatePropertyFiles(propertyFiles []string) error {
        for _, fileName := range propertyFiles {
                if err := validatePropertyFile(fileName); err != nil {
@@ -472,21 +473,21 @@ func copyIntegrationFilesToDirectory(files []string, 
directory string) ([]string
        return relocatedFilesList, nil
 }
 
-func copyQuarkusAppFiles(localDependenciesDirectory string, localQuarkusDir 
string) error {
+func copyQuarkusAppFiles(dependenciesDir string, quarkusDir string) error {
        // Create directory if one does not already exist
-       if err := util.CreateDirectory(localQuarkusDir); err != nil {
+       if err := util.CreateDirectory(quarkusDir); err != nil {
                return err
        }
 
        // Transfer all files with a .dat extension and all files with a 
*-bytecode.jar suffix.
-       files, err := getRegularFilesInDir(localDependenciesDirectory, false)
+       files, err := getRegularFilesInDir(dependenciesDir, false)
        if err != nil {
                return err
        }
        for _, file := range files {
                if strings.HasSuffix(file, ".dat") || strings.HasSuffix(file, 
"-bytecode.jar") {
-                       source := path.Join(localDependenciesDirectory, file)
-                       destination := path.Join(localQuarkusDir, file)
+                       source := path.Join(dependenciesDir, file)
+                       destination := path.Join(quarkusDir, file)
                        if _, err = util.CopyFile(source, destination); err != 
nil {
                                return err
                        }
@@ -496,20 +497,20 @@ func copyQuarkusAppFiles(localDependenciesDirectory 
string, localQuarkusDir stri
        return nil
 }
 
-func copyLibFiles(localDependenciesDirectory string, localLibDirectory string) 
error {
+func copyLibFiles(dependenciesDir string, libDir string) error {
        // Create directory if one does not already exist
-       if err := util.CreateDirectory(localLibDirectory); err != nil {
+       if err := util.CreateDirectory(libDir); err != nil {
                return err
        }
 
-       fileNames, err := getRegularFilesInDir(localDependenciesDirectory, 
false)
+       fileNames, err := getRegularFilesInDir(dependenciesDir, false)
        if err != nil {
                return err
        }
 
        for _, dependencyJar := range fileNames {
-               source := path.Join(localDependenciesDirectory, dependencyJar)
-               destination := path.Join(localLibDirectory, dependencyJar)
+               source := path.Join(dependenciesDir, dependencyJar)
+               destination := path.Join(libDir, dependencyJar)
                if _, err = util.CopyFile(source, destination); err != nil {
                        return err
                }
@@ -518,21 +519,21 @@ func copyLibFiles(localDependenciesDirectory string, 
localLibDirectory string) e
        return nil
 }
 
-func copyAppFile(localDependenciesDirectory string, localAppDirectory string) 
error {
+func copyAppFile(dependenciesDir string, appDir string) error {
        // Create directory if one does not already exist
-       if err := util.CreateDirectory(localAppDirectory); err != nil {
+       if err := util.CreateDirectory(appDir); err != nil {
                return err
        }
 
-       fileNames, err := getRegularFilesInDir(localDependenciesDirectory, 
false)
+       fileNames, err := getRegularFilesInDir(dependenciesDir, false)
        if err != nil {
                return err
        }
 
        for _, dependencyJar := range fileNames {
                if strings.HasPrefix(dependencyJar, "camel-k-integration-") {
-                       source := path.Join(localDependenciesDirectory, 
dependencyJar)
-                       destination := path.Join(localAppDirectory, 
dependencyJar)
+                       source := path.Join(dependenciesDir, dependencyJar)
+                       destination := path.Join(appDir, dependencyJar)
                        if _, err = util.CopyFile(source, destination); err != 
nil {
                                return err
                        }
diff --git a/pkg/cmd/local_util_container.go b/pkg/cmd/local_util_container.go
index e2bcdd191..a10c49025 100644
--- a/pkg/cmd/local_util_container.go
+++ b/pkg/cmd/local_util_container.go
@@ -190,11 +190,10 @@ func createAndBuildIntegrationImage(ctx context.Context, 
containerRegistry strin
                return err
        }
 
-       return buildIntegrationImage(ctx, image, startsFromLocalFolder, stdout, 
stderr)
+       return buildIntegrationImage(ctx, image, stdout, stderr)
 }
 
-func buildIntegrationImage(ctx context.Context, image string, 
startsFromLocalFolder bool,
-       stdout, stderr io.Writer) error {
+func buildIntegrationImage(ctx context.Context, image string, stdout, stderr 
io.Writer) error {
        // Get the Docker command arguments for building the base image and 
create the command.
        args := docker.BuildIntegrationImageArgs(image, MavenWorkingDirectory)
        cmd := exec.CommandContext(ctx, "docker", args...)
diff --git a/pkg/util/util.go b/pkg/util/util.go
index 878c5913d..25ca86517 100644
--- a/pkg/util/util.go
+++ b/pkg/util/util.go
@@ -423,21 +423,6 @@ func CopyMap(source map[string]string) map[string]string {
        return dest
 }
 
-func DependenciesToJSON(list []string) ([]byte, error) {
-       jsondata := map[string]interface{}{}
-       jsondata["dependencies"] = list
-       return json.Marshal(jsondata)
-}
-
-func DependenciesToYAML(list []string) ([]byte, error) {
-       data, err := DependenciesToJSON(list)
-       if err != nil {
-               return nil, err
-       }
-
-       return JSONToYAML(data)
-}
-
 func JSONToYAML(src []byte) ([]byte, error) {
        mapdata, err := JSONToMap(src)
        if err != nil {

Reply via email to