You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ts...@apache.org on 2022/08/07 13:01:31 UTC

[camel-k] 01/09: chore(cli): refactor kamel local cmds

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 944146586f1fb8b82c683b9050e574fa6095f6a6
Author: Tadayoshi Sato <sa...@gmail.com>
AuthorDate: Thu Jul 28 13:14:12 2022 +0900

    chore(cli): refactor kamel local cmds
---
 e2e/namespace/install/cli/dev_mode_test.go |  2 +-
 pkg/cmd/local_build.go                     | 54 ++++++++++++-------------
 pkg/cmd/local_inspect.go                   | 14 +++----
 pkg/cmd/local_run.go                       | 64 +++++++++++++++---------------
 pkg/cmd/local_util.go                      |  1 +
 5 files changed, 68 insertions(+), 67 deletions(-)

diff --git a/e2e/namespace/install/cli/dev_mode_test.go b/e2e/namespace/install/cli/dev_mode_test.go
index dd808e2c5..289af3697 100644
--- a/e2e/namespace/install/cli/dev_mode_test.go
+++ b/e2e/namespace/install/cli/dev_mode_test.go
@@ -28,9 +28,9 @@ import (
 	"io"
 	"io/ioutil"
 	"os"
+	"path/filepath"
 	"testing"
 	"time"
-	"path/filepath"
 
 	. "github.com/onsi/gomega"
 	"github.com/stretchr/testify/assert"
diff --git a/pkg/cmd/local_build.go b/pkg/cmd/local_build.go
index 5c0d72cd4..2bbb4b3f5 100644
--- a/pkg/cmd/local_build.go
+++ b/pkg/cmd/local_build.go
@@ -81,7 +81,7 @@ type localBuildCmdOptions struct {
 	MavenRepositories    []string `mapstructure:"maven-repositories"`
 }
 
-func (command *localBuildCmdOptions) validate(args []string) error {
+func (o *localBuildCmdOptions) validate(args []string) error {
 	// Validate integration files.
 	if len(args) > 0 {
 		if err := validateIntegrationFiles(args); err != nil {
@@ -90,51 +90,51 @@ func (command *localBuildCmdOptions) validate(args []string) error {
 	}
 
 	// Validate additional dependencies specified by the user.
-	if err := validateDependencies(command.Dependencies); err != nil {
+	if err := validateDependencies(o.Dependencies); err != nil {
 		return err
 	}
 
 	// Validate properties file.
-	if err := validateFiles(command.PropertyFiles); err != nil {
+	if err := validateFiles(o.PropertyFiles); err != nil {
 		return err
 	}
 
-	if command.BaseImage {
+	if o.BaseImage {
 		// Cannot have both integration files and the base image construction enabled.
 		if len(args) > 0 {
 			return errors.New("integration files have been provided and the base image construction is enabled")
 		}
 
 		// Docker registry must be set.
-		if command.ContainerRegistry == "" {
+		if o.ContainerRegistry == "" {
 			return errors.New("base image cannot be built because container registry has not been provided")
 		}
 
 		// If an integration directory is provided then no base image containerization can be enabled.
-		if command.IntegrationDirectory != "" {
+		if o.IntegrationDirectory != "" {
 			return errors.New("base image construction does not use integration files")
 		}
-	} else if command.ContainerRegistry != "" {
+	} else if o.ContainerRegistry != "" {
 		// ContainerRegistry should only be specified when building the base image.
 		return errors.New("cannot specify container registry unless a base integration image is being built")
 	}
 
 	// The integration directory must be set when only outputting dependencies.
-	if command.DependenciesOnly && command.IntegrationDirectory == "" {
+	if o.DependenciesOnly && o.IntegrationDirectory == "" {
 		return errors.New("to output dependencies the integration directory flag must be set")
 	}
 
 	return nil
 }
 
-func (command *localBuildCmdOptions) init(args []string) error {
+func (o *localBuildCmdOptions) init(args []string) error {
 	// Create integration directory if one is provided.
-	err := util.CreateDirectory(command.IntegrationDirectory)
+	err := util.CreateDirectory(o.IntegrationDirectory)
 	if err != nil {
 		return err
 	}
 
-	if command.BaseImage || command.Image != "" {
+	if o.BaseImage || o.Image != "" {
 		// If base image construction is enabled create a directory for it.
 		err := createDockerBaseWorkingDirectory()
 		if err != nil {
@@ -142,7 +142,7 @@ func (command *localBuildCmdOptions) init(args []string) error {
 		}
 
 		// If integration image construction is enabled, an integration image will be built.
-		if command.Image != "" {
+		if o.Image != "" {
 			err := createDockerWorkingDirectory()
 			if err != nil {
 				return err
@@ -158,19 +158,19 @@ func (command *localBuildCmdOptions) init(args []string) error {
 	return nil
 }
 
-func (command *localBuildCmdOptions) run(cmd *cobra.Command, args []string) error {
+func (o *localBuildCmdOptions) run(cmd *cobra.Command, args []string) error {
 	var dependenciesList, propertyFilesList []string
 	routeFiles := args
-	if !command.BaseImage {
-		dependencies, err := GetDependencies(command.Context, args, command.Dependencies, command.MavenRepositories, true)
+	if !o.BaseImage {
+		dependencies, err := GetDependencies(o.Context, args, o.Dependencies, o.MavenRepositories, true)
 		if err != nil {
 			return err
 		}
 
 		var propertyFiles []string
-		if !command.DependenciesOnly {
+		if !o.DependenciesOnly {
 			// Manage integration properties which may come from files or CLI
-			propertyFiles, err = updateIntegrationProperties(command.Properties, command.PropertyFiles, false)
+			propertyFiles, err = updateIntegrationProperties(o.Properties, o.PropertyFiles, false)
 			if err != nil {
 				return err
 			}
@@ -178,10 +178,10 @@ func (command *localBuildCmdOptions) run(cmd *cobra.Command, args []string) erro
 
 		dependenciesList = dependencies
 		propertyFilesList = propertyFiles
-		hasIntegrationDir := command.IntegrationDirectory != ""
+		hasIntegrationDir := o.IntegrationDirectory != ""
 		if hasIntegrationDir {
 			// Create dependencies subdirectory.
-			localDependenciesDirectory := getCustomDependenciesDir(command.IntegrationDirectory)
+			localDependenciesDirectory := getCustomDependenciesDir(o.IntegrationDirectory)
 
 			// Copy dependencies in persistent IntegrationDirectory/dependencies
 			dependenciesList, err = util.CopyIntegrationFilesToDirectory(dependencies, localDependenciesDirectory)
@@ -190,12 +190,12 @@ func (command *localBuildCmdOptions) run(cmd *cobra.Command, args []string) erro
 			}
 
 			// Once dependencies have been copied to local folder, we can exit.
-			if command.DependenciesOnly {
+			if o.DependenciesOnly {
 				return nil
 			}
 
 			// Create dependencies subdirectory.
-			localPropertiesDirectory := getCustomPropertiesDir(command.IntegrationDirectory)
+			localPropertiesDirectory := getCustomPropertiesDir(o.IntegrationDirectory)
 
 			// Copy dependencies in persistent IntegrationDirectory/dependencies
 			propertyFilesList, err = util.CopyIntegrationFilesToDirectory(propertyFiles, localPropertiesDirectory)
@@ -204,7 +204,7 @@ func (command *localBuildCmdOptions) run(cmd *cobra.Command, args []string) erro
 			}
 
 			// Save routes.
-			localRoutesDirectory := getCustomRoutesDir(command.IntegrationDirectory)
+			localRoutesDirectory := getCustomRoutesDir(o.IntegrationDirectory)
 
 			// Copy routes in persistent IntegrationDirectory/dependencies
 			routeFiles, err = util.CopyIntegrationFilesToDirectory(args, localRoutesDirectory)
@@ -220,13 +220,13 @@ func (command *localBuildCmdOptions) run(cmd *cobra.Command, args []string) erro
 
 	// The only case in which we should not execute the integration image creation is when we want to
 	// just output the files that comprise the integration locally.
-	if command.IntegrationDirectory != "" && command.Image == "" {
+	if o.IntegrationDirectory != "" && o.Image == "" {
 		return nil
 	}
 
 	// Create and build integration image.
-	err := createAndBuildIntegrationImage(command.Context, command.ContainerRegistry, command.BaseImage,
-		command.Image, propertyFilesList, dependenciesList, routeFiles, false, cmd.OutOrStdout(), cmd.ErrOrStderr())
+	err := createAndBuildIntegrationImage(o.Context, o.ContainerRegistry, o.BaseImage,
+		o.Image, propertyFilesList, dependenciesList, routeFiles, false, cmd.OutOrStdout(), cmd.ErrOrStderr())
 	if err != nil {
 		return err
 	}
@@ -234,7 +234,7 @@ func (command *localBuildCmdOptions) run(cmd *cobra.Command, args []string) erro
 	return nil
 }
 
-func (command *localBuildCmdOptions) deinit() error {
+func (o *localBuildCmdOptions) deinit() error {
 	// If base image construction is enabled delete the directory for it.
 	err := deleteDockerBaseWorkingDirectory()
 	if err != nil {
@@ -242,7 +242,7 @@ func (command *localBuildCmdOptions) deinit() error {
 	}
 
 	// If integration files are provided delete the maven project folder.
-	if !command.BaseImage {
+	if !o.BaseImage {
 		err = deleteDockerWorkingDirectory()
 		if err != nil {
 			return err
diff --git a/pkg/cmd/local_inspect.go b/pkg/cmd/local_inspect.go
index 54fca9862..f5e523bc1 100644
--- a/pkg/cmd/local_inspect.go
+++ b/pkg/cmd/local_inspect.go
@@ -70,29 +70,29 @@ type localInspectCmdOptions struct {
 	MavenRepositories []string `mapstructure:"maven-repositories"`
 }
 
-func (command *localInspectCmdOptions) validate(args []string) error {
+func (o *localInspectCmdOptions) validate(args []string) error {
 	if err := validateIntegrationFiles(args); err != nil {
 		return err
 	}
 
-	if err := validateDependencies(command.Dependencies); err != nil {
+	if err := validateDependencies(o.Dependencies); err != nil {
 		return err
 	}
 
 	return nil
 }
 
-func (command *localInspectCmdOptions) init() error {
+func (o *localInspectCmdOptions) init() error {
 	return createMavenWorkingDirectory()
 }
 
-func (command *localInspectCmdOptions) run(cmd *cobra.Command, args []string) error {
-	dependencies, err := GetDependencies(command.Context, args, command.Dependencies, command.MavenRepositories, command.AllDependencies)
+func (o *localInspectCmdOptions) run(cmd *cobra.Command, args []string) error {
+	dependencies, err := GetDependencies(o.Context, args, o.Dependencies, o.MavenRepositories, o.AllDependencies)
 	if err != nil {
 		return err
 	}
 
-	err = outputDependencies(dependencies, command.OutputFormat, cmd)
+	err = outputDependencies(dependencies, o.OutputFormat, cmd)
 	if err != nil {
 		return err
 	}
@@ -100,6 +100,6 @@ func (command *localInspectCmdOptions) run(cmd *cobra.Command, args []string) er
 	return nil
 }
 
-func (command *localInspectCmdOptions) deinit() error {
+func (o *localInspectCmdOptions) deinit() error {
 	return deleteMavenWorkingDirectory()
 }
diff --git a/pkg/cmd/local_run.go b/pkg/cmd/local_run.go
index ec021ed0d..024a361a3 100644
--- a/pkg/cmd/local_run.go
+++ b/pkg/cmd/local_run.go
@@ -97,35 +97,35 @@ type localRunCmdOptions struct {
 	MavenRepositories    []string `mapstructure:"maven-repositories"`
 }
 
-func (command *localRunCmdOptions) validate(args []string) error {
+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 command.Image == "" && command.IntegrationDirectory == "" {
+	if o.Image == "" && o.IntegrationDirectory == "" {
 		if err := validateIntegrationFiles(args); err != nil {
 			return err
 		}
 	}
 
 	// Validate additional dependencies specified by the user.
-	if err := validateDependencies(command.Dependencies); err != nil {
+	if err := validateDependencies(o.Dependencies); err != nil {
 		return err
 	}
 
 	// Validate properties file.
-	if err := validatePropertyFiles(command.PropertyFiles); err != nil {
+	if err := validatePropertyFiles(o.PropertyFiles); err != nil {
 		return err
 	}
 
 	// If containerize is set then docker image name must be set.
-	if command.Containerize && command.Image == "" {
+	if o.Containerize && o.Image == "" {
 		return errors.New("containerization is active but no image name has been provided")
 	}
 
 	return nil
 }
 
-func (command *localRunCmdOptions) init() error {
-	if command.Containerize {
+func (o *localRunCmdOptions) init() error {
+	if o.Containerize {
 		if err := createDockerBaseWorkingDirectory(); err != nil {
 			return err
 		}
@@ -135,42 +135,42 @@ func (command *localRunCmdOptions) init() error {
 		}
 	}
 
-	setDockerNetworkName(command.Network)
+	setDockerNetworkName(o.Network)
 
-	setDockerEnvVars(command.EnvironmentVariables)
+	setDockerEnvVars(o.EnvironmentVariables)
 
 	return createMavenWorkingDirectory()
 }
 
-func (command *localRunCmdOptions) run(cmd *cobra.Command, args []string) error {
+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 command.Image != "" && !command.Containerize {
+	if o.Image != "" && !o.Containerize {
 		// Run image locally.
-		if err := runIntegrationImage(command.Context, command.Image, cmd.OutOrStdout(), cmd.ErrOrStderr()); err != nil {
+		if err := runIntegrationImage(o.Context, o.Image, cmd.OutOrStdout(), cmd.ErrOrStderr()); err != nil {
 			return err
 		}
 
 		return nil
 	}
 
-	hasIntegrationDir := command.IntegrationDirectory != ""
+	hasIntegrationDir := o.IntegrationDirectory != ""
 
 	var dependencies []string
 	if hasIntegrationDir {
 		// Fetch local dependencies
-		localBuildDependencies, err := getLocalBuildDependencies(command.IntegrationDirectory)
+		localBuildDependencies, err := getLocalBuildDependencies(o.IntegrationDirectory)
 		if err != nil {
 			return err
 		}
 		dependencies = localBuildDependencies
 
 		// Local dependencies directory
-		localDependenciesDirectory := getCustomDependenciesDir(command.IntegrationDirectory)
+		localDependenciesDirectory := 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(command.IntegrationDirectory)
+		localQuarkusDir := getCustomQuarkusDir(o.IntegrationDirectory)
 		err = util.CopyQuarkusAppFiles(localDependenciesDirectory, localQuarkusDir)
 		if err != nil {
 			return err
@@ -178,7 +178,7 @@ func (command *localRunCmdOptions) run(cmd *cobra.Command, args []string) error
 
 		// The dependency jar files need to be at a specific location i.e.:
 		// <integration_directory>/../lib/main/*.jar
-		localLibDirectory := getCustomLibDir(command.IntegrationDirectory)
+		localLibDirectory := getCustomLibDir(o.IntegrationDirectory)
 		err = util.CopyLibFiles(localDependenciesDirectory, localLibDirectory)
 		if err != nil {
 			return err
@@ -186,13 +186,13 @@ func (command *localRunCmdOptions) run(cmd *cobra.Command, args []string) error
 
 		// 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(command.IntegrationDirectory)
+		localAppDirectory := getCustomAppDir(o.IntegrationDirectory)
 		err = util.CopyAppFile(localDependenciesDirectory, localAppDirectory)
 		if err != nil {
 			return err
 		}
 	} else {
-		computedDependencies, err := GetDependencies(command.Context, args, command.Dependencies, command.MavenRepositories, true)
+		computedDependencies, err := GetDependencies(o.Context, args, o.Dependencies, o.MavenRepositories, true)
 		if err != nil {
 			return err
 		}
@@ -200,16 +200,16 @@ func (command *localRunCmdOptions) run(cmd *cobra.Command, args []string) error
 	}
 
 	// Manage integration properties which may come from files or CLI.
-	propertyFiles := command.PropertyFiles
+	propertyFiles := o.PropertyFiles
 	if hasIntegrationDir {
-		localBuildPropertyFiles, err := getLocalBuildProperties(command.IntegrationDirectory)
+		localBuildPropertyFiles, err := getLocalBuildProperties(o.IntegrationDirectory)
 		if err != nil {
 			return err
 		}
 		propertyFiles = localBuildPropertyFiles
 	}
 
-	updatedPropertyFiles, err := updateIntegrationProperties(command.Properties, propertyFiles, hasIntegrationDir)
+	updatedPropertyFiles, err := updateIntegrationProperties(o.Properties, propertyFiles, hasIntegrationDir)
 	if err != nil {
 		return err
 	}
@@ -217,7 +217,7 @@ func (command *localRunCmdOptions) run(cmd *cobra.Command, args []string) error
 
 	routes := args
 	if hasIntegrationDir {
-		localBuildRoutes, err := getLocalBuildRoutes(command.IntegrationDirectory)
+		localBuildRoutes, err := getLocalBuildRoutes(o.IntegrationDirectory)
 		if err != nil {
 			return err
 		}
@@ -225,26 +225,26 @@ func (command *localRunCmdOptions) run(cmd *cobra.Command, args []string) error
 	}
 
 	// If this is a containerized local run, create, build and run the container image.
-	if command.Containerize {
+	if o.Containerize {
 		// Create and build integration image.
-		err := createAndBuildIntegrationImage(command.Context, "", false, command.Image, propertyFiles, dependencies, routes, hasIntegrationDir, cmd.OutOrStdout(), cmd.ErrOrStderr())
+		err := createAndBuildIntegrationImage(o.Context, "", false, o.Image, propertyFiles, dependencies, routes, hasIntegrationDir, cmd.OutOrStdout(), cmd.ErrOrStderr())
 		if err != nil {
 			return err
 		}
 
 		// Run integration image.
-		err = runIntegrationImage(command.Context, command.Image, cmd.OutOrStdout(), cmd.ErrOrStderr())
+		err = runIntegrationImage(o.Context, o.Image, cmd.OutOrStdout(), cmd.ErrOrStderr())
 		if err != nil {
 			return err
 		}
 	} else {
 		propertiesDir := util.GetLocalPropertiesDir()
 		if hasIntegrationDir {
-			propertiesDir = getCustomPropertiesDir(command.IntegrationDirectory)
+			propertiesDir = getCustomPropertiesDir(o.IntegrationDirectory)
 		}
 
 		// Run integration locally.
-		err := RunLocalIntegrationRunCommand(command.Context, propertyFiles, dependencies, routes, propertiesDir, cmd.OutOrStdout(), cmd.ErrOrStderr())
+		err := RunLocalIntegrationRunCommand(o.Context, propertyFiles, dependencies, routes, propertiesDir, cmd.OutOrStdout(), cmd.ErrOrStderr())
 		if err != nil {
 			return err
 		}
@@ -253,8 +253,8 @@ func (command *localRunCmdOptions) run(cmd *cobra.Command, args []string) error
 	return nil
 }
 
-func (command *localRunCmdOptions) deinit() error {
-	if command.Containerize {
+func (o *localRunCmdOptions) deinit() error {
+	if o.Containerize {
 		err := deleteDockerBaseWorkingDirectory()
 		if err != nil {
 			return err
@@ -266,8 +266,8 @@ func (command *localRunCmdOptions) deinit() error {
 		}
 	}
 
-	if command.IntegrationDirectory != "" {
-		err := deleteLocalIntegrationDirs(command.IntegrationDirectory)
+	if o.IntegrationDirectory != "" {
+		err := deleteLocalIntegrationDirs(o.IntegrationDirectory)
 		if err != nil {
 			return err
 		}
diff --git a/pkg/cmd/local_util.go b/pkg/cmd/local_util.go
index c9b04feda..75837d85f 100644
--- a/pkg/cmd/local_util.go
+++ b/pkg/cmd/local_util.go
@@ -280,6 +280,7 @@ func validateFile(file string) error {
 	return nil
 }
 
+// validateFiles ensures existence of given files.
 func validateFiles(args []string) error {
 	// Ensure source files exist
 	for _, arg := range args {