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 f204d16164506335377009dab483832ca3d61bfc Author: Tadayoshi Sato <[email protected]> AuthorDate: Thu Jul 28 18:43:06 2022 +0900 chore(cli): further refactor kamel local cmds --- pkg/cmd/local_build.go | 9 +- pkg/cmd/local_inspect.go | 2 +- pkg/cmd/local_run.go | 12 +- pkg/cmd/local_util.go | 167 +++++++-------------------- pkg/cmd/local_util_container.go | 10 +- pkg/cmd/local_util_dirs.go | 244 ++++++++++++++++++++++++++++++++++++++++ pkg/util/docker/docker.go | 32 +++--- pkg/util/util.go | 156 ------------------------- 8 files changed, 316 insertions(+), 316 deletions(-) diff --git a/pkg/cmd/local_build.go b/pkg/cmd/local_build.go index 079bf0dc3..181f58979 100644 --- a/pkg/cmd/local_build.go +++ b/pkg/cmd/local_build.go @@ -160,7 +160,7 @@ func (o *localBuildCmdOptions) run(cmd *cobra.Command, args []string) error { var dependenciesList, propertyFilesList []string routeFiles := args if !o.BaseImage { - dependencies, err := GetDependencies(o.Context, args, o.Dependencies, o.MavenRepositories, true) + dependencies, err := getDependencies(o.Context, args, o.Dependencies, o.MavenRepositories, true) if err != nil { return err } @@ -176,12 +176,13 @@ func (o *localBuildCmdOptions) run(cmd *cobra.Command, args []string) error { dependenciesList = dependencies propertyFilesList = propertyFiles + if o.IntegrationDirectory != "" { // Create dependencies subdirectory. localDependenciesDirectory := getCustomDependenciesDir(o.IntegrationDirectory) // Copy dependencies in persistent IntegrationDirectory/dependencies - dependenciesList, err = CopyIntegrationFilesToDirectory(dependencies, localDependenciesDirectory) + dependenciesList, err = copyIntegrationFilesToDirectory(dependencies, localDependenciesDirectory) if err != nil { return err } @@ -195,7 +196,7 @@ func (o *localBuildCmdOptions) run(cmd *cobra.Command, args []string) error { localPropertiesDirectory := getCustomPropertiesDir(o.IntegrationDirectory) // Copy dependencies in persistent IntegrationDirectory/dependencies - propertyFilesList, err = CopyIntegrationFilesToDirectory(propertyFiles, localPropertiesDirectory) + propertyFilesList, err = copyIntegrationFilesToDirectory(propertyFiles, localPropertiesDirectory) if err != nil { return err } @@ -204,7 +205,7 @@ func (o *localBuildCmdOptions) run(cmd *cobra.Command, args []string) error { localRoutesDirectory := getCustomRoutesDir(o.IntegrationDirectory) // Copy routes in persistent IntegrationDirectory/dependencies - routeFiles, err = CopyIntegrationFilesToDirectory(args, localRoutesDirectory) + routeFiles, err = copyIntegrationFilesToDirectory(args, localRoutesDirectory) if err != nil { return err } diff --git a/pkg/cmd/local_inspect.go b/pkg/cmd/local_inspect.go index f5e523bc1..1c180d56b 100644 --- a/pkg/cmd/local_inspect.go +++ b/pkg/cmd/local_inspect.go @@ -87,7 +87,7 @@ func (o *localInspectCmdOptions) init() error { } func (o *localInspectCmdOptions) run(cmd *cobra.Command, args []string) error { - dependencies, err := GetDependencies(o.Context, args, o.Dependencies, o.MavenRepositories, o.AllDependencies) + dependencies, err := getDependencies(o.Context, args, o.Dependencies, o.MavenRepositories, o.AllDependencies) if err != nil { return err } diff --git a/pkg/cmd/local_run.go b/pkg/cmd/local_run.go index ad6f77ce3..951e455f3 100644 --- a/pkg/cmd/local_run.go +++ b/pkg/cmd/local_run.go @@ -25,8 +25,6 @@ import ( "github.com/pkg/errors" "github.com/spf13/cobra" - - "github.com/apache/camel-k/pkg/util" ) func newCmdLocalRun(localCmdOptions *LocalCmdOptions) (*cobra.Command, *localRunCmdOptions) { @@ -171,7 +169,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 = CopyQuarkusAppFiles(localDependenciesDirectory, localQuarkusDir) + err = copyQuarkusAppFiles(localDependenciesDirectory, localQuarkusDir) if err != nil { return err } @@ -179,7 +177,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 = CopyLibFiles(localDependenciesDirectory, localLibDirectory) + err = copyLibFiles(localDependenciesDirectory, localLibDirectory) if err != nil { return err } @@ -187,12 +185,12 @@ 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 = CopyAppFile(localDependenciesDirectory, localAppDirectory) + err = copyAppFile(localDependenciesDirectory, localAppDirectory) if err != nil { return err } } else { - computedDependencies, err := GetDependencies(o.Context, args, o.Dependencies, o.MavenRepositories, true) + computedDependencies, err := getDependencies(o.Context, args, o.Dependencies, o.MavenRepositories, true) if err != nil { return err } @@ -238,7 +236,7 @@ func (o *localRunCmdOptions) run(cmd *cobra.Command, args []string) error { return err } } else { - propertiesDir := util.GetLocalPropertiesDir() + propertiesDir := getLocalPropertiesDir() if hasIntegrationDir { propertiesDir = getCustomPropertiesDir(o.IntegrationDirectory) } diff --git a/pkg/cmd/local_util.go b/pkg/cmd/local_util.go index 3ecd9adc7..67ea7f905 100644 --- a/pkg/cmd/local_util.go +++ b/pkg/cmd/local_util.go @@ -44,8 +44,8 @@ var acceptedDependencyTypes = []string{ "github", "gitlab", "bitbucket", "gitee", "azure", } -// GetDependencies resolves and gets the list of dependencies from catalog and sources. -func GetDependencies(ctx context.Context, args []string, additionalDependencies []string, repositories []string, allDependencies bool) ([]string, error) { +// getDependencies resolves and gets the list of dependencies from catalog and sources. +func getDependencies(ctx context.Context, args []string, additionalDependencies []string, repositories []string, allDependencies bool) ([]string, error) { // Fetch existing catalog or create new one if one does not already exist catalog, err := createCamelCatalog(ctx) if err != nil { @@ -114,7 +114,7 @@ func getTransitiveDependencies(ctx context.Context, catalog *camel.RuntimeCatalo return nil, err } - mc := maven.NewContext(util.MavenWorkingDirectory) + mc := maven.NewContext(MavenWorkingDirectory) mc.LocalRepository = "" if len(repositories) > 0 { @@ -150,27 +150,31 @@ func getTransitiveDependencies(ctx context.Context, catalog *camel.RuntimeCatalo return transitiveDependencies, nil } -func getRegularFilesInDir(directory string) ([]string, error) { +func getRegularFilesInDir(directory string, dirnameInPath bool) ([]string, error) { var dirFiles []string files, err := ioutil.ReadDir(directory) + if err != nil { + return nil, err + } + for _, file := range files { fileName := file.Name() // Do not include hidden files or sub-directories. if !file.IsDir() && !strings.HasPrefix(fileName, ".") { - dirFiles = append(dirFiles, path.Join(directory, fileName)) + if dirnameInPath { + dirFiles = append(dirFiles, path.Join(directory, fileName)) + } else { + dirFiles = append(dirFiles, fileName) + } } } - if err != nil { - return nil, err - } - return dirFiles, nil } func getLocalBuildDependencies(integrationDirectory string) ([]string, error) { - locallyBuiltDependencies, err := getRegularFilesInDir(getCustomDependenciesDir(integrationDirectory)) + locallyBuiltDependencies, err := getRegularFilesInDir(getCustomDependenciesDir(integrationDirectory), true) if err != nil { return nil, err } @@ -178,7 +182,7 @@ func getLocalBuildDependencies(integrationDirectory string) ([]string, error) { } func getLocalBuildProperties(integrationDirectory string) ([]string, error) { - locallyBuiltProperties, err := getRegularFilesInDir(getCustomPropertiesDir(integrationDirectory)) + locallyBuiltProperties, err := getRegularFilesInDir(getCustomPropertiesDir(integrationDirectory), true) if err != nil { return nil, err } @@ -186,7 +190,7 @@ func getLocalBuildProperties(integrationDirectory string) ([]string, error) { } func getLocalBuildRoutes(integrationDirectory string) ([]string, error) { - locallyBuiltRoutes, err := getRegularFilesInDir(getCustomRoutesDir(integrationDirectory)) + locallyBuiltRoutes, err := getRegularFilesInDir(getCustomRoutesDir(integrationDirectory), true) if err != nil { return nil, err } @@ -347,17 +351,15 @@ func validatePropertyFile(fileName string) error { func updateIntegrationProperties(properties []string, propertyFiles []string, hasIntegrationDir bool) ([]string, error) { // Create properties directory under Maven working directory. // This ensures that property files of different integrations do not clash. - err := util.CreateLocalPropertiesDirectory() - if err != nil { + if err := createLocalPropertiesDirectory(); err != nil { return nil, err } // Relocate properties files to this integration's property directory. relocatedPropertyFiles := []string{} for _, propertyFile := range propertyFiles { - relocatedPropertyFile := path.Join(util.GetLocalPropertiesDir(), path.Base(propertyFile)) - _, err = util.CopyFile(propertyFile, relocatedPropertyFile) - if err != nil { + relocatedPropertyFile := path.Join(getLocalPropertiesDir(), path.Base(propertyFile)) + if _, err := util.CopyFile(propertyFile, relocatedPropertyFile); err != nil { return nil, err } relocatedPropertyFiles = append(relocatedPropertyFiles, relocatedPropertyFile) @@ -366,9 +368,8 @@ func updateIntegrationProperties(properties []string, propertyFiles []string, ha if !hasIntegrationDir { // Output list of properties to property file if any CLI properties were given. if len(properties) > 0 { - propertyFilePath := path.Join(util.GetLocalPropertiesDir(), "CLI.properties") - err = ioutil.WriteFile(propertyFilePath, []byte(strings.Join(properties, "\n")), 0o600) - if err != nil { + propertyFilePath := path.Join(getLocalPropertiesDir(), "CLI.properties") + if err := ioutil.WriteFile(propertyFilePath, []byte(strings.Join(properties, "\n")), 0o600); err != nil { return nil, err } relocatedPropertyFiles = append(relocatedPropertyFiles, propertyFilePath) @@ -381,7 +382,7 @@ func updateIntegrationProperties(properties []string, propertyFiles []string, ha func updateIntegrationDependencies(dependencies []string) error { // Create dependencies directory under Maven working directory. // This ensures that dependencies will be removed after they are not needed. - err := util.CreateLocalDependenciesDirectory() + err := createLocalDependenciesDirectory() if err != nil { return err } @@ -391,9 +392,9 @@ func updateIntegrationDependencies(dependencies []string) error { var targetPath string basePath := util.SubstringFrom(dependency, util.QuarkusDependenciesBaseDirectory) if basePath != "" { - targetPath = path.Join(util.GetLocalDependenciesDir(), basePath) + targetPath = path.Join(getLocalDependenciesDir(), basePath) } else { - targetPath = path.Join(util.GetLocalDependenciesDir(), path.Base(dependency)) + targetPath = path.Join(getLocalDependenciesDir(), path.Base(dependency)) } _, err = util.CopyFile(dependency, targetPath) if err != nil { @@ -405,13 +406,13 @@ func updateIntegrationDependencies(dependencies []string) error { } func updateIntegrationRoutes(routes []string) error { - err := util.CreateLocalRoutesDirectory() + err := createLocalRoutesDirectory() if err != nil { return err } for _, route := range routes { - _, err = util.CopyFile(route, path.Join(util.GetLocalRoutesDir(), path.Base(route))) + _, err = util.CopyFile(route, path.Join(getLocalRoutesDir(), path.Base(route))) if err != nil { return err } @@ -421,104 +422,42 @@ func updateIntegrationRoutes(routes []string) error { } func updateQuarkusDirectory() error { - err := util.CreateLocalQuarkusDirectory() + err := createLocalQuarkusDirectory() if err != nil { return err } // ignore error if custom dir doesn't exist - _ = CopyQuarkusAppFiles(util.CustomQuarkusDirectoryName, util.GetLocalQuarkusDir()) + _ = copyQuarkusAppFiles(util.CustomQuarkusDirectoryName, getLocalQuarkusDir()) return nil } func updateAppDirectory() error { - err := util.CreateLocalAppDirectory() + err := createLocalAppDirectory() if err != nil { return err } // ignore error if custom dir doesn't exist - _ = CopyAppFile(util.CustomAppDirectoryName, util.GetLocalAppDir()) + _ = copyAppFile(util.CustomAppDirectoryName, getLocalAppDir()) return nil } func updateLibDirectory() error { - err := util.CreateLocalLibDirectory() + err := createLocalLibDirectory() if err != nil { return err } // ignore error if custom dir doesn't exist - _ = CopyLibFiles(util.CustomLibDirectoryName, util.GetLocalLibDir()) - - return nil -} - -func createMavenWorkingDirectory() error { - // Create local Maven context - temporaryDirectory, err := ioutil.TempDir(os.TempDir(), "maven-") - if err != nil { - return err - } - - // Set the Maven directory to the default value - util.MavenWorkingDirectory = temporaryDirectory - - return nil -} - -func deleteMavenWorkingDirectory() error { - // Remove directory used for computing the dependencies - return os.RemoveAll(util.MavenWorkingDirectory) -} - -func getCustomDependenciesDir(integrationDirectory string) string { - return path.Join(integrationDirectory, "dependencies") -} - -func getCustomPropertiesDir(integrationDirectory string) string { - return path.Join(integrationDirectory, "properties") -} - -func getCustomRoutesDir(integrationDirectory string) string { - return path.Join(integrationDirectory, "routes") -} - -func getCustomQuarkusDir(integrationDirectory string) string { - parentDir := path.Dir(strings.TrimSuffix(integrationDirectory, "/")) - return path.Join(parentDir, "quarkus") -} - -func getCustomLibDir(integrationDirectory string) string { - parentDir := path.Dir(strings.TrimSuffix(integrationDirectory, "/")) - return path.Join(parentDir, "lib/main") -} - -func getCustomAppDir(integrationDirectory string) string { - parentDir := path.Dir(strings.TrimSuffix(integrationDirectory, "/")) - return path.Join(parentDir, "app") -} - -func deleteLocalIntegrationDirs(integrationDirectory string) error { - dirs := []string{ - getCustomQuarkusDir(integrationDirectory), - getCustomLibDir(integrationDirectory), - getCustomAppDir(integrationDirectory), - } - - for _, dir := range dirs { - err := os.RemoveAll(dir) - if err != nil { - return err - } - } + _ = copyLibFiles(util.CustomLibDirectoryName, getLocalLibDir()) return nil } -func CopyIntegrationFilesToDirectory(files []string, directory string) ([]string, error) { +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 @@ -538,15 +477,14 @@ func CopyIntegrationFilesToDirectory(files []string, directory string) ([]string return relocatedFilesList, nil } -func CopyQuarkusAppFiles(localDependenciesDirectory string, localQuarkusDir string) error { +func copyQuarkusAppFiles(localDependenciesDirectory string, localQuarkusDir string) error { // Create directory if one does not already exist - err := util.CreateDirectory(localQuarkusDir) - if err != nil { + if err := util.CreateDirectory(localQuarkusDir); err != nil { return err } // Transfer all files with a .dat extension and all files with a *-bytecode.jar suffix. - files, err := getRegularFileNamesInDir(localDependenciesDirectory) + files, err := getRegularFilesInDir(localDependenciesDirectory, false) if err != nil { return err } @@ -564,14 +502,13 @@ func CopyQuarkusAppFiles(localDependenciesDirectory string, localQuarkusDir stri return nil } -func CopyLibFiles(localDependenciesDirectory string, localLibDirectory string) error { +func copyLibFiles(localDependenciesDirectory string, localLibDirectory string) error { // Create directory if one does not already exist - err := util.CreateDirectory(localLibDirectory) - if err != nil { + if err := util.CreateDirectory(localLibDirectory); err != nil { return err } - fileNames, err := getRegularFileNamesInDir(localDependenciesDirectory) + fileNames, err := getRegularFilesInDir(localDependenciesDirectory, false) if err != nil { return err } @@ -588,14 +525,13 @@ func CopyLibFiles(localDependenciesDirectory string, localLibDirectory string) e return nil } -func CopyAppFile(localDependenciesDirectory string, localAppDirectory string) error { +func copyAppFile(localDependenciesDirectory string, localAppDirectory string) error { // Create directory if one does not already exist - err := util.CreateDirectory(localAppDirectory) - if err != nil { + if err := util.CreateDirectory(localAppDirectory); err != nil { return err } - fileNames, err := getRegularFileNamesInDir(localDependenciesDirectory) + fileNames, err := getRegularFilesInDir(localDependenciesDirectory, false) if err != nil { return err } @@ -613,22 +549,3 @@ func CopyAppFile(localDependenciesDirectory string, localAppDirectory string) er 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/local_util_container.go b/pkg/cmd/local_util_container.go index 64e9dd886..e03824b7c 100644 --- a/pkg/cmd/local_util_container.go +++ b/pkg/cmd/local_util_container.go @@ -35,8 +35,8 @@ import ( // Local Docker file system management functions. +// createDockerBaseWorkingDirectory creates local docker base directory. func createDockerBaseWorkingDirectory() error { - // Create local docker base directory. temporaryDirectory, err := ioutil.TempDir(os.TempDir(), "docker-base-") if err != nil { return err @@ -48,13 +48,13 @@ func createDockerBaseWorkingDirectory() error { return nil } +// deleteDockerBaseWorkingDirectory removes directory used for computing the base dependencies. func deleteDockerBaseWorkingDirectory() error { - // Remove directory used for computing the dependencies. return os.RemoveAll(docker.BaseWorkingDirectory) } +// createDockerWorkingDirectory creates local docker directory. func createDockerWorkingDirectory() error { - // Create local docker base directory. temporaryDirectory, err := ioutil.TempDir(os.TempDir(), "docker-") if err != nil { return err @@ -66,8 +66,8 @@ func createDockerWorkingDirectory() error { return nil } +// deleteDockerWorkingDirectory removes directory used for computing the integration dependencies. func deleteDockerWorkingDirectory() error { - // Remove directory used for computing the dependencies. return os.RemoveAll(docker.IntegrationWorkingDirectory) } @@ -180,7 +180,7 @@ func createAndBuildIntegrationImage(ctx context.Context, containerRegistry strin } // Get the Docker command arguments for building the base image and create the command. - args := docker.BuildIntegrationImageArgs(image) + args := docker.BuildIntegrationImageArgs(image, MavenWorkingDirectory) cmd := exec.CommandContext(ctx, "docker", args...) // Set stdout and stderr. diff --git a/pkg/cmd/local_util_dirs.go b/pkg/cmd/local_util_dirs.go new file mode 100644 index 000000000..7d0311d12 --- /dev/null +++ b/pkg/cmd/local_util_dirs.go @@ -0,0 +1,244 @@ +/* +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 ( + "io/ioutil" + "os" + "path" + "strings" + + "github.com/apache/camel-k/pkg/util" +) + +// MavenWorkingDirectory is the directory used by Maven for an invocation of the kamel local command. +// By default, a temporary folder will be used. +var MavenWorkingDirectory = "" + +// createMavenWorkingDirectory creates local Maven working directory. +func createMavenWorkingDirectory() error { + temporaryDirectory, err := ioutil.TempDir(os.TempDir(), "maven-") + if err != nil { + return err + } + + // Set the Maven directory to the default value + MavenWorkingDirectory = temporaryDirectory + + return nil +} + +// deleteMavenWorkingDirectory removes local Maven working directory. +func deleteMavenWorkingDirectory() error { + return os.RemoveAll(MavenWorkingDirectory) +} + +// getLocalDependenciesDir returns <mavenWorkingDirectory>/dependencies. +func getLocalDependenciesDir() string { + return path.Join(MavenWorkingDirectory, util.DefaultDependenciesDirectoryName) +} + +func createLocalDependenciesDirectory() error { + // Do not create a directory unless the maven directory contains a valid value. + if MavenWorkingDirectory == "" { + return nil + } + + directoryExists, err := util.DirectoryExists(getLocalDependenciesDir()) + if err != nil { + return err + } + + if !directoryExists { + if err := os.MkdirAll(getLocalDependenciesDir(), 0o700); err != nil { + return err + } + } + + return nil +} + +// getLocalPropertiesDir returns <mavenWorkingDirectory>/properties. +func getLocalPropertiesDir() string { + return path.Join(MavenWorkingDirectory, util.DefaultPropertiesDirectoryName) +} + +func createLocalPropertiesDirectory() error { + // Do not create a directory unless the maven directory contains a valid value. + if MavenWorkingDirectory == "" { + return nil + } + + directoryExists, err := util.DirectoryExists(getLocalPropertiesDir()) + if err != nil { + return err + } + + if !directoryExists { + err := os.MkdirAll(getLocalPropertiesDir(), 0o700) + if err != nil { + return err + } + } + + return nil +} + +// getLocalRoutesDir returns <mavenWorkingDirectory>/routes. +func getLocalRoutesDir() string { + return path.Join(MavenWorkingDirectory, util.DefaultRoutesDirectoryName) +} + +func createLocalRoutesDirectory() error { + // Do not create a directory unless the maven directory contains a valid value. + if MavenWorkingDirectory == "" { + return nil + } + + directoryExists, err := util.DirectoryExists(getLocalRoutesDir()) + if err != nil { + return err + } + + if !directoryExists { + if err := os.MkdirAll(getLocalRoutesDir(), 0o700); err != nil { + return err + } + } + + return nil +} + +// getLocalQuarkusDir returns <mavenWorkingDirectory>/quarkus. +func getLocalQuarkusDir() string { + return path.Join(MavenWorkingDirectory, util.CustomQuarkusDirectoryName) +} + +func createLocalQuarkusDirectory() error { + // Do not create a directory unless the maven directory contains a valid value. + if MavenWorkingDirectory == "" { + return nil + } + + directoryExists, err := util.DirectoryExists(getLocalQuarkusDir()) + if err != nil { + return err + } + + if !directoryExists { + if err := os.MkdirAll(getLocalQuarkusDir(), 0o700); err != nil { + return err + } + } + + return nil +} + +// getLocalAppDir returns <mavenWorkingDirectory>/app. +func getLocalAppDir() string { + return path.Join(MavenWorkingDirectory, util.CustomAppDirectoryName) +} + +func createLocalAppDirectory() error { + // Do not create a directory unless the maven directory contains a valid value. + if MavenWorkingDirectory == "" { + return nil + } + + directoryExists, err := util.DirectoryExists(getLocalAppDir()) + if err != nil { + return err + } + + if !directoryExists { + if err := os.MkdirAll(getLocalAppDir(), 0o700); err != nil { + return err + } + } + + return nil +} + +// getLocalLibDir returns <mavenWorkingDirectory>/lib/main. +func getLocalLibDir() string { + return path.Join(MavenWorkingDirectory, util.CustomLibDirectoryName) +} + +func createLocalLibDirectory() error { + // Do not create a directory unless the maven directory contains a valid value. + if MavenWorkingDirectory == "" { + return nil + } + + directoryExists, err := util.DirectoryExists(getLocalLibDir()) + if err != nil { + return err + } + + if !directoryExists { + if err := os.MkdirAll(getLocalLibDir(), 0o700); err != nil { + return err + } + } + + return nil +} + +func getCustomDependenciesDir(dir string) string { + return path.Join(dir, util.DefaultDependenciesDirectoryName) +} + +func getCustomPropertiesDir(dir string) string { + return path.Join(dir, util.DefaultPropertiesDirectoryName) +} + +func getCustomRoutesDir(dir string) string { + return path.Join(dir, util.DefaultRoutesDirectoryName) +} + +func getCustomQuarkusDir(dir string) string { + parentDir := path.Dir(strings.TrimSuffix(dir, "/")) + return path.Join(parentDir, util.CustomQuarkusDirectoryName) +} + +func getCustomLibDir(dir string) string { + parentDir := path.Dir(strings.TrimSuffix(dir, "/")) + return path.Join(parentDir, util.CustomLibDirectoryName) +} + +func getCustomAppDir(dir string) string { + parentDir := path.Dir(strings.TrimSuffix(dir, "/")) + return path.Join(parentDir, "app") +} + +func deleteLocalIntegrationDirs(dir string) error { + dirs := []string{ + getCustomQuarkusDir(dir), + getCustomLibDir(dir), + getCustomAppDir(dir), + } + + for _, dir := range dirs { + err := os.RemoveAll(dir) + if err != nil { + return err + } + } + + return nil +} diff --git a/pkg/util/docker/docker.go b/pkg/util/docker/docker.go index 38bf8a0e9..a053434c9 100644 --- a/pkg/util/docker/docker.go +++ b/pkg/util/docker/docker.go @@ -92,31 +92,27 @@ func CreateIntegrationImageDockerFile(integrationRunCmd *exec.Cmd, startsFromLoc return nil } -// BuildBaseImageArgs --. +// BuildBaseImageArgs constructs the docker command: +// +// docker build -f <BaseWorkingDirectory>/Dockerfile -t <dockerRegistry>/<BaseImageName> <BaseWorkingDirectory>. +// func BuildBaseImageArgs() []string { - // Construct the docker command: - // - // docker build -f <BaseWorkingDirectory>/Dockerfile -t <dockerRegistry>/<BaseImageName> <BaseWorkingDirectory> - // - // Add register return BuildImageArgs(BaseWorkingDirectory, GetBaseImagePath(), BaseWorkingDirectory) } -// BuildIntegrationImageArgs --. -func BuildIntegrationImageArgs(imagePath string) []string { - // Construct the docker command: - // - // docker build -f <BaseWorkingDirectory>/Dockerfile -t <imagePath> <MavenWorkingDirectory> - // - return BuildImageArgs(IntegrationWorkingDirectory, imagePath, util.MavenWorkingDirectory) +// BuildIntegrationImageArgs constructs the docker command: +// +// docker build -f <BaseWorkingDirectory>/Dockerfile -t <imagePath> <mavenWorkingDirectory>. +// +func BuildIntegrationImageArgs(imagePath string, mavenWorkingDirectory string) []string { + return BuildImageArgs(IntegrationWorkingDirectory, imagePath, mavenWorkingDirectory) } -// RunIntegrationImageArgs --. +// RunIntegrationImageArgs constructs the docker command: +// +// docker run --network=<network-name> --env LAZY_ENV_VAR=value <dockerRegistry>/<ImageName>. +// func RunIntegrationImageArgs(imagePath string) ([]string, error) { - // Construct the docker command: - // - // docker run --network=<network-name> --env LAZY_ENV_VAR=value <dockerRegistry>/<ImageName> - // return RunImageArgs(imagePath, latestTag) } diff --git a/pkg/util/util.go b/pkg/util/util.go index 45e02c0b9..878c5913d 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -44,10 +44,6 @@ import ( // Directories and file names: -// MavenWorkingDirectory is the directory used by Maven for an invocation of the kamel local command. -// By default, a temporary folder will be used. -var MavenWorkingDirectory = "" - // DefaultDependenciesDirectoryName --. const DefaultDependenciesDirectoryName = "dependencies" @@ -479,158 +475,6 @@ func WriteToFile(filePath string, fileContents string) error { return nil } -// Local directories: - -// GetLocalPropertiesDir -- <mavenWorkingDirectory>/properties. -func GetLocalPropertiesDir() string { - return path.Join(MavenWorkingDirectory, DefaultPropertiesDirectoryName) -} - -// GetLocalDependenciesDir --<mavenWorkingDirectory>/dependencies. -func GetLocalDependenciesDir() string { - return path.Join(MavenWorkingDirectory, DefaultDependenciesDirectoryName) -} - -// GetLocalRoutesDir -- <mavenWorkingDirectory>/routes. -func GetLocalRoutesDir() string { - return path.Join(MavenWorkingDirectory, DefaultRoutesDirectoryName) -} - -// GetLocalQuarkusDir -- <mavenWorkingDirectory>/quarkus. -func GetLocalQuarkusDir() string { - return path.Join(MavenWorkingDirectory, CustomQuarkusDirectoryName) -} - -// GetLocalAppDir -- <mavenWorkingDirectory>/app. -func GetLocalAppDir() string { - return path.Join(MavenWorkingDirectory, CustomAppDirectoryName) -} - -// GetLocalLibDir -- <mavenWorkingDirectory>/lib/main. -func GetLocalLibDir() string { - return path.Join(MavenWorkingDirectory, CustomLibDirectoryName) -} - -func CreateLocalPropertiesDirectory() error { - // Do not create a directory unless the maven directory contains a valid value. - if MavenWorkingDirectory == "" { - return nil - } - - directoryExists, err := DirectoryExists(GetLocalPropertiesDir()) - if err != nil { - return err - } - - if !directoryExists { - err := os.MkdirAll(GetLocalPropertiesDir(), 0o700) - if err != nil { - return err - } - } - return nil -} - -func CreateLocalDependenciesDirectory() error { - // Do not create a directory unless the maven directory contains a valid value. - if MavenWorkingDirectory == "" { - return nil - } - - directoryExists, err := DirectoryExists(GetLocalDependenciesDir()) - if err != nil { - return err - } - - if !directoryExists { - err := os.MkdirAll(GetLocalDependenciesDir(), 0o700) - if err != nil { - return err - } - } - return nil -} - -func CreateLocalRoutesDirectory() error { - // Do not create a directory unless the maven directory contains a valid value. - if MavenWorkingDirectory == "" { - return nil - } - - directoryExists, err := DirectoryExists(GetLocalRoutesDir()) - if err != nil { - return err - } - - if !directoryExists { - err := os.MkdirAll(GetLocalRoutesDir(), 0o700) - if err != nil { - return err - } - } - return nil -} - -func CreateLocalQuarkusDirectory() error { - // Do not create a directory unless the maven directory contains a valid value. - if MavenWorkingDirectory == "" { - return nil - } - - directoryExists, err := DirectoryExists(GetLocalQuarkusDir()) - if err != nil { - return err - } - - if !directoryExists { - err := os.MkdirAll(GetLocalQuarkusDir(), 0o700) - if err != nil { - return err - } - } - return nil -} - -func CreateLocalAppDirectory() error { - // Do not create a directory unless the maven directory contains a valid value. - if MavenWorkingDirectory == "" { - return nil - } - - directoryExists, err := DirectoryExists(GetLocalAppDir()) - if err != nil { - return err - } - - if !directoryExists { - err := os.MkdirAll(GetLocalAppDir(), 0o700) - if err != nil { - return err - } - } - return nil -} - -func CreateLocalLibDirectory() error { - // Do not create a directory unless the maven directory contains a valid value. - if MavenWorkingDirectory == "" { - return nil - } - - directoryExists, err := DirectoryExists(GetLocalLibDir()) - if err != nil { - return err - } - - if !directoryExists { - err := os.MkdirAll(GetLocalLibDir(), 0o700) - if err != nil { - return err - } - } - return nil -} - func GetEnvironmentVariable(variable string) (string, error) { value, isPresent := os.LookupEnv(variable) if !isPresent {
