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:32 UTC

[camel-k] 02/09: chore(cli): further 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 d7e68a7175cfbfa9c6a22dd1c7504f2b750158ea
Author: Tadayoshi Sato <sa...@gmail.com>
AuthorDate: Thu Jul 28 15:35:34 2022 +0900

    chore(cli): further refactor kamel local cmds
---
 pkg/cmd/local.go                                   |   3 +-
 pkg/cmd/local_build.go                             |  37 +++---
 pkg/cmd/local_run.go                               |   6 +-
 pkg/cmd/local_util.go                              | 121 +++++++++++++++++-
 ...containerization.go => local_util_container.go} |  24 ++--
 pkg/util/util.go                                   | 141 ++-------------------
 6 files changed, 160 insertions(+), 172 deletions(-)

diff --git a/pkg/cmd/local.go b/pkg/cmd/local.go
index 1a480490f..65163bfa5 100644
--- a/pkg/cmd/local.go
+++ b/pkg/cmd/local.go
@@ -75,6 +75,7 @@ func (o *LocalCmdOptions) persistentPreRun(cmd *cobra.Command, args []string) er
 
 func warnTraitUsages(cmd *cobra.Command, traits []string) {
 	if len(traits) > 0 {
-		fmt.Fprintf(cmd.OutOrStdout(), "Warning: traits are specified but don't take effect for local run: %v\n", traits)
+		fmt.Fprintf(cmd.OutOrStdout(),
+			"Warning: traits are specified but don't take effect for local run: %v\n", traits)
 	}
 }
diff --git a/pkg/cmd/local_build.go b/pkg/cmd/local_build.go
index 2bbb4b3f5..079bf0dc3 100644
--- a/pkg/cmd/local_build.go
+++ b/pkg/cmd/local_build.go
@@ -129,29 +129,27 @@ func (o *localBuildCmdOptions) validate(args []string) error {
 
 func (o *localBuildCmdOptions) init(args []string) error {
 	// Create integration directory if one is provided.
-	err := util.CreateDirectory(o.IntegrationDirectory)
-	if err != nil {
-		return err
+	if o.IntegrationDirectory != "" {
+		if err := util.CreateDirectory(o.IntegrationDirectory); err != nil {
+			return err
+		}
 	}
 
 	if o.BaseImage || o.Image != "" {
 		// If base image construction is enabled create a directory for it.
-		err := createDockerBaseWorkingDirectory()
-		if err != nil {
+		if err := createDockerBaseWorkingDirectory(); err != nil {
 			return err
 		}
 
 		// If integration image construction is enabled, an integration image will be built.
 		if o.Image != "" {
-			err := createDockerWorkingDirectory()
-			if err != nil {
+			if err := createDockerWorkingDirectory(); err != nil {
 				return err
 			}
 		}
 	}
 
-	err = createMavenWorkingDirectory()
-	if err != nil {
+	if err := createMavenWorkingDirectory(); err != nil {
 		return err
 	}
 
@@ -178,13 +176,12 @@ func (o *localBuildCmdOptions) run(cmd *cobra.Command, args []string) error {
 
 		dependenciesList = dependencies
 		propertyFilesList = propertyFiles
-		hasIntegrationDir := o.IntegrationDirectory != ""
-		if hasIntegrationDir {
+		if o.IntegrationDirectory != "" {
 			// Create dependencies subdirectory.
 			localDependenciesDirectory := getCustomDependenciesDir(o.IntegrationDirectory)
 
 			// Copy dependencies in persistent IntegrationDirectory/dependencies
-			dependenciesList, err = util.CopyIntegrationFilesToDirectory(dependencies, localDependenciesDirectory)
+			dependenciesList, err = CopyIntegrationFilesToDirectory(dependencies, localDependenciesDirectory)
 			if err != nil {
 				return err
 			}
@@ -198,7 +195,7 @@ func (o *localBuildCmdOptions) run(cmd *cobra.Command, args []string) error {
 			localPropertiesDirectory := getCustomPropertiesDir(o.IntegrationDirectory)
 
 			// Copy dependencies in persistent IntegrationDirectory/dependencies
-			propertyFilesList, err = util.CopyIntegrationFilesToDirectory(propertyFiles, localPropertiesDirectory)
+			propertyFilesList, err = CopyIntegrationFilesToDirectory(propertyFiles, localPropertiesDirectory)
 			if err != nil {
 				return err
 			}
@@ -207,7 +204,7 @@ func (o *localBuildCmdOptions) run(cmd *cobra.Command, args []string) error {
 			localRoutesDirectory := getCustomRoutesDir(o.IntegrationDirectory)
 
 			// Copy routes in persistent IntegrationDirectory/dependencies
-			routeFiles, err = util.CopyIntegrationFilesToDirectory(args, localRoutesDirectory)
+			routeFiles, err = CopyIntegrationFilesToDirectory(args, localRoutesDirectory)
 			if err != nil {
 				return err
 			}
@@ -226,7 +223,8 @@ func (o *localBuildCmdOptions) run(cmd *cobra.Command, args []string) error {
 
 	// Create and build integration image.
 	err := createAndBuildIntegrationImage(o.Context, o.ContainerRegistry, o.BaseImage,
-		o.Image, propertyFilesList, dependenciesList, routeFiles, false, cmd.OutOrStdout(), cmd.ErrOrStderr())
+		o.Image, propertyFilesList, dependenciesList, routeFiles, false,
+		cmd.OutOrStdout(), cmd.ErrOrStderr())
 	if err != nil {
 		return err
 	}
@@ -236,19 +234,16 @@ func (o *localBuildCmdOptions) run(cmd *cobra.Command, args []string) error {
 
 func (o *localBuildCmdOptions) deinit() error {
 	// If base image construction is enabled delete the directory for it.
-	err := deleteDockerBaseWorkingDirectory()
-	if err != nil {
+	if err := deleteDockerBaseWorkingDirectory(); err != nil {
 		return err
 	}
 
 	// If integration files are provided delete the maven project folder.
 	if !o.BaseImage {
-		err = deleteDockerWorkingDirectory()
-		if err != nil {
+		if err := deleteDockerWorkingDirectory(); err != nil {
 			return err
 		}
-		err = deleteMavenWorkingDirectory()
-		if err != nil {
+		if err := deleteMavenWorkingDirectory(); err != nil {
 			return err
 		}
 	}
diff --git a/pkg/cmd/local_run.go b/pkg/cmd/local_run.go
index 024a361a3..ad6f77ce3 100644
--- a/pkg/cmd/local_run.go
+++ b/pkg/cmd/local_run.go
@@ -171,7 +171,7 @@ func (o *localRunCmdOptions) run(cmd *cobra.Command, args []string) error {
 		// <integration_directory>/../quarkus/quarkus-application.dat
 		// <integration_directory>/../quarkus/generated-bytecode.jar
 		localQuarkusDir := getCustomQuarkusDir(o.IntegrationDirectory)
-		err = util.CopyQuarkusAppFiles(localDependenciesDirectory, localQuarkusDir)
+		err = CopyQuarkusAppFiles(localDependenciesDirectory, localQuarkusDir)
 		if err != nil {
 			return err
 		}
@@ -179,7 +179,7 @@ func (o *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(o.IntegrationDirectory)
-		err = util.CopyLibFiles(localDependenciesDirectory, localLibDirectory)
+		err = CopyLibFiles(localDependenciesDirectory, localLibDirectory)
 		if err != nil {
 			return err
 		}
@@ -187,7 +187,7 @@ func (o *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(o.IntegrationDirectory)
-		err = util.CopyAppFile(localDependenciesDirectory, localAppDirectory)
+		err = CopyAppFile(localDependenciesDirectory, localAppDirectory)
 		if err != nil {
 			return err
 		}
diff --git a/pkg/cmd/local_util.go b/pkg/cmd/local_util.go
index 75837d85f..3ecd9adc7 100644
--- a/pkg/cmd/local_util.go
+++ b/pkg/cmd/local_util.go
@@ -427,7 +427,7 @@ func updateQuarkusDirectory() error {
 	}
 
 	// ignore error if custom dir doesn't exist
-	_ = util.CopyQuarkusAppFiles(util.CustomQuarkusDirectoryName, util.GetLocalQuarkusDir())
+	_ = CopyQuarkusAppFiles(util.CustomQuarkusDirectoryName, util.GetLocalQuarkusDir())
 
 	return nil
 }
@@ -439,7 +439,7 @@ func updateAppDirectory() error {
 	}
 
 	// ignore error if custom dir doesn't exist
-	_ = util.CopyAppFile(util.CustomAppDirectoryName, util.GetLocalAppDir())
+	_ = CopyAppFile(util.CustomAppDirectoryName, util.GetLocalAppDir())
 
 	return nil
 }
@@ -451,7 +451,7 @@ func updateLibDirectory() error {
 	}
 
 	// ignore error if custom dir doesn't exist
-	_ = util.CopyLibFiles(util.CustomLibDirectoryName, util.GetLocalLibDir())
+	_ = CopyLibFiles(util.CustomLibDirectoryName, util.GetLocalLibDir())
 
 	return nil
 }
@@ -517,3 +517,118 @@ func deleteLocalIntegrationDirs(integrationDirectory string) error {
 
 	return nil
 }
+
+func CopyIntegrationFilesToDirectory(files []string, directory string) ([]string, error) {
+	// Create directory if one does not already exist
+	if err := util.CreateDirectory(directory); err != nil {
+		return nil, err
+	}
+
+	// Copy files to new location. Also create the list with relocated files.
+	relocatedFilesList := []string{}
+	for _, filePath := range files {
+		newFilePath := path.Join(directory, path.Base(filePath))
+		_, err := util.CopyFile(filePath, newFilePath)
+		if err != nil {
+			return relocatedFilesList, err
+		}
+		relocatedFilesList = append(relocatedFilesList, newFilePath)
+	}
+
+	return relocatedFilesList, nil
+}
+
+func CopyQuarkusAppFiles(localDependenciesDirectory string, localQuarkusDir string) error {
+	// Create directory if one does not already exist
+	err := util.CreateDirectory(localQuarkusDir)
+	if err != nil {
+		return err
+	}
+
+	// Transfer all files with a .dat extension and all files with a *-bytecode.jar suffix.
+	files, err := getRegularFileNamesInDir(localDependenciesDirectory)
+	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)
+			_, err = util.CopyFile(source, destination)
+			if err != nil {
+				return err
+			}
+		}
+	}
+
+	return nil
+}
+
+func CopyLibFiles(localDependenciesDirectory string, localLibDirectory string) error {
+	// Create directory if one does not already exist
+	err := util.CreateDirectory(localLibDirectory)
+	if err != nil {
+		return err
+	}
+
+	fileNames, err := getRegularFileNamesInDir(localDependenciesDirectory)
+	if err != nil {
+		return err
+	}
+
+	for _, dependencyJar := range fileNames {
+		source := path.Join(localDependenciesDirectory, dependencyJar)
+		destination := path.Join(localLibDirectory, dependencyJar)
+		_, err = util.CopyFile(source, destination)
+		if err != nil {
+			return err
+		}
+	}
+
+	return nil
+}
+
+func CopyAppFile(localDependenciesDirectory string, localAppDirectory string) error {
+	// Create directory if one does not already exist
+	err := util.CreateDirectory(localAppDirectory)
+	if err != nil {
+		return err
+	}
+
+	fileNames, err := getRegularFileNamesInDir(localDependenciesDirectory)
+	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)
+			_, err = util.CopyFile(source, destination)
+			if err != nil {
+				return err
+			}
+		}
+	}
+
+	return nil
+}
+
+func getRegularFileNamesInDir(directory string) ([]string, error) {
+	var dirFiles []string
+	files, err := ioutil.ReadDir(directory)
+	for _, file := range files {
+		fileName := file.Name()
+
+		// Do not include hidden files or sub-directories.
+		if !file.IsDir() && !strings.HasPrefix(fileName, ".") {
+			dirFiles = append(dirFiles, fileName)
+		}
+	}
+
+	if err != nil {
+		return nil, err
+	}
+
+	return dirFiles, nil
+}
diff --git a/pkg/cmd/util_containerization.go b/pkg/cmd/local_util_container.go
similarity index 92%
rename from pkg/cmd/util_containerization.go
rename to pkg/cmd/local_util_container.go
index ec1c2e932..64e9dd886 100644
--- a/pkg/cmd/util_containerization.go
+++ b/pkg/cmd/local_util_container.go
@@ -85,8 +85,7 @@ func setDockerEnvVars(envVars []string) {
 
 func createAndBuildBaseImage(ctx context.Context, stdout, stderr io.Writer) error {
 	// Create the base image Docker file.
-	err := docker.CreateBaseImageDockerFile()
-	if err != nil {
+	if err := docker.CreateBaseImageDockerFile(); err != nil {
 		return err
 	}
 
@@ -130,8 +129,7 @@ func createAndBuildIntegrationImage(ctx context.Context, containerRegistry strin
 	}
 
 	// Create the Dockerfile and build the base image.
-	err := createAndBuildBaseImage(ctx, stdout, stderr)
-	if err != nil {
+	if err := createAndBuildBaseImage(ctx, stdout, stderr); err != nil {
 		return err
 	}
 
@@ -145,32 +143,27 @@ func createAndBuildIntegrationImage(ctx context.Context, containerRegistry strin
 
 	// Create integration image if integration files were provided.
 	// Copy dependencies to a dependencies folder under a local directory.
-	err = updateIntegrationDependencies(dependencies)
-	if err != nil {
+	if err := updateIntegrationDependencies(dependencies); err != nil {
 		return err
 	}
 
 	// Copy routes to a routes folder under a local directory.
-	err = updateIntegrationRoutes(routes)
-	if err != nil {
+	if err := updateIntegrationRoutes(routes); err != nil {
 		return err
 	}
 
 	// Copy quarkus files in maven subdirectory
-	err = updateQuarkusDirectory()
-	if err != nil {
+	if err := updateQuarkusDirectory(); err != nil {
 		return err
 	}
 
 	// Copy app files in maven subdirectory
-	err = updateAppDirectory()
-	if err != nil {
+	if err := updateAppDirectory(); err != nil {
 		return err
 	}
 
 	// Copy lib files in maven subdirectory
-	err = updateLibDirectory()
-	if err != nil {
+	if err := updateLibDirectory(); err != nil {
 		return err
 	}
 
@@ -182,8 +175,7 @@ func createAndBuildIntegrationImage(ctx context.Context, containerRegistry strin
 	}
 
 	// Create the integration image Docker file.
-	err = docker.CreateIntegrationImageDockerFile(containerCmd, startsFromLocalFolder)
-	if err != nil {
+	if err := docker.CreateIntegrationImageDockerFile(containerCmd, startsFromLocalFolder); err != nil {
 		return err
 	}
 
diff --git a/pkg/util/util.go b/pkg/util/util.go
index c68f27717..45e02c0b9 100644
--- a/pkg/util/util.go
+++ b/pkg/util/util.go
@@ -368,20 +368,21 @@ func DirectoryEmpty(directory string) (bool, error) {
 	return ok, err
 }
 
+// CreateDirectory creates a directory if it does not exist.
 func CreateDirectory(directory string) error {
-	if directory != "" {
-		// If directory does not exist, create it
-		directoryExists, err := DirectoryExists(directory)
-		if err != nil {
-			return err
-		}
+	if directory == "" {
+		return errors.New("directory name must not be empty")
+	}
 
-		if !directoryExists {
-			// #nosec G301
-			err := os.MkdirAll(directory, 0o755)
-			if err != nil {
-				return err
-			}
+	directoryExists, err := DirectoryExists(directory)
+	if err != nil {
+		return err
+	}
+
+	if !directoryExists {
+		// #nosec G301
+		if err := os.MkdirAll(directory, 0o755); err != nil {
+			return err
 		}
 	}
 
@@ -682,122 +683,6 @@ func EvaluateCLIAndLazyEnvVars() ([]string, error) {
 	return evaluatedEnvVars, nil
 }
 
-func CopyIntegrationFilesToDirectory(files []string, directory string) ([]string, error) {
-	// Create directory if one does not already exist
-	err := CreateDirectory(directory)
-	if err != nil {
-		return nil, err
-	}
-
-	// Copy files to new location. Also create the list with relocated files.
-	relocatedFilesList := []string{}
-	for _, filePath := range files {
-		newFilePath := path.Join(directory, path.Base(filePath))
-		_, err := CopyFile(filePath, newFilePath)
-		if err != nil {
-			return relocatedFilesList, err
-		}
-		relocatedFilesList = append(relocatedFilesList, newFilePath)
-	}
-
-	return relocatedFilesList, nil
-}
-
-func CopyQuarkusAppFiles(localDependenciesDirectory string, localQuarkusDir string) error {
-	// Create directory if one does not already exist
-	err := CreateDirectory(localQuarkusDir)
-	if err != nil {
-		return err
-	}
-
-	// Transfer all files with a .dat extension and all files with a *-bytecode.jar suffix.
-	files, err := getRegularFileNamesInDir(localDependenciesDirectory)
-	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)
-			_, err = CopyFile(source, destination)
-			if err != nil {
-				return err
-			}
-		}
-	}
-
-	return nil
-}
-
-func getRegularFileNamesInDir(directory string) ([]string, error) {
-	var dirFiles []string
-	files, err := ioutil.ReadDir(directory)
-	for _, file := range files {
-		fileName := file.Name()
-
-		// Do not include hidden files or sub-directories.
-		if !file.IsDir() && !strings.HasPrefix(fileName, ".") {
-			dirFiles = append(dirFiles, fileName)
-		}
-	}
-
-	if err != nil {
-		return nil, err
-	}
-
-	return dirFiles, nil
-}
-
-func CopyLibFiles(localDependenciesDirectory string, localLibDirectory string) error {
-	// Create directory if one does not already exist
-	err := CreateDirectory(localLibDirectory)
-	if err != nil {
-		return err
-	}
-
-	fileNames, err := getRegularFileNamesInDir(localDependenciesDirectory)
-	if err != nil {
-		return err
-	}
-
-	for _, dependencyJar := range fileNames {
-		source := path.Join(localDependenciesDirectory, dependencyJar)
-		destination := path.Join(localLibDirectory, dependencyJar)
-		_, err = CopyFile(source, destination)
-		if err != nil {
-			return err
-		}
-	}
-
-	return nil
-}
-
-func CopyAppFile(localDependenciesDirectory string, localAppDirectory string) error {
-	// Create directory if one does not already exist
-	err := CreateDirectory(localAppDirectory)
-	if err != nil {
-		return err
-	}
-
-	fileNames, err := getRegularFileNamesInDir(localDependenciesDirectory)
-	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)
-			_, err = CopyFile(source, destination)
-			if err != nil {
-				return err
-			}
-		}
-	}
-
-	return nil
-}
-
 // Open a safe wrapper of os.Open.
 func Open(name string) (*os.File, error) {
 	return os.Open(filepath.Clean(name))