This is an automated email from the ASF dual-hosted git repository. nferraro pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel-k.git
commit f0684ca3b9488a4da5ea6719eb3f79c73c110516 Author: Doru Bercea <[email protected]> AuthorDate: Tue Oct 27 14:05:28 2020 -0400 Remove workspace directory. Make maven directory temporary. --- pkg/cmd/inspect.go | 92 +++++++++++++++++++++--------------------------------- 1 file changed, 36 insertions(+), 56 deletions(-) diff --git a/pkg/cmd/inspect.go b/pkg/cmd/inspect.go index cbb823d..a4539ff 100644 --- a/pkg/cmd/inspect.go +++ b/pkg/cmd/inspect.go @@ -20,6 +20,7 @@ package cmd import ( "errors" "fmt" + "io/ioutil" "os" "path" "strings" @@ -38,10 +39,7 @@ import ( var acceptedDependencyTypes = []string{"bom", "camel", "camel-k", "camel-quarkus", "mvn", "github"} -const ( - defaultWorkspaceDirectoryName = "workspace" - defaultDependenciesDirectoryName = "dependencies" -) +const defaultDependenciesDirectoryName = "dependencies" func newCmdInspect(rootCmdOptions *RootCmdOptions) (*cobra.Command, *inspectCmdOptions) { options := inspectCmdOptions{ @@ -75,11 +73,10 @@ will be generated by calling Maven and then copied into the directory pointed to } cmd.Flags().Bool("all-dependencies", false, "Compute transitive dependencies and move them to directory pointed to by the --dependencies-directory flag.") - cmd.Flags().StringArrayP("additional-dependencies", "d", nil, `Comma-separated lists of additional top-level dependencies with the format: + cmd.Flags().StringArrayP("dependency", "d", nil, `Additional top-level dependency with the format: <type>:<dependency-name> where <type> is one of {`+strings.Join(acceptedDependencyTypes, "|")+`}.`) - cmd.Flags().String("workspace", "", "Workspace directory. Default: <kamel-invocation-directory>/workspace") - cmd.Flags().String("dependencies-directory", "", "Directory that will contain all the computed dependencies. Default: <kamel-invocation-directory>/<kamel-workspace-directory>/dependencies") + cmd.Flags().String("dependencies-directory", "", "Directory that will contain all the computed dependencies. Default: <kamel-invocation-directory>/dependencies") cmd.Flags().StringP("output", "o", "", "Output format. One of: json|yaml") return &cmd, &options @@ -89,9 +86,8 @@ type inspectCmdOptions struct { *RootCmdOptions AllDependencies bool `mapstructure:"all-dependencies"` OutputFormat string `mapstructure:"output"` - Workspace string `mapstructure:"workspace"` DependenciesDirectory string `mapstructure:"dependencies-directory"` - AdditionalDependencies []string `mapstructure:"additional-dependencies"` + AdditionalDependencies []string `mapstructure:"dependencies"` } func (command *inspectCmdOptions) validate(args []string) error { @@ -120,22 +116,19 @@ func (command *inspectCmdOptions) validate(args []string) error { // a valid type. if command.AdditionalDependencies != nil { for _, additionalDependency := range command.AdditionalDependencies { - additionalDependencies := strings.Split(additionalDependency, ",") - - for _, dependency := range additionalDependencies { - dependencyComponents := strings.Split(dependency, ":") + dependencyComponents := strings.Split(additionalDependency, ":") - TypeIsValid := false - for _, dependencyType := range acceptedDependencyTypes { - if dependencyType == dependencyComponents[0] { - TypeIsValid = true - } + TypeIsValid := false + for _, dependencyType := range acceptedDependencyTypes { + if dependencyType == dependencyComponents[0] { + TypeIsValid = true } + } - if !TypeIsValid { - return errors.New("Unexpected type for user-provided dependency: " + dependency + ", check command usage for valid format.") - } + if !TypeIsValid { + return errors.New("Unexpected type for user-provided dependency: " + additionalDependency + ", check command usage for valid format.") } + } } @@ -157,18 +150,11 @@ func (command *inspectCmdOptions) validate(args []string) error { } func (command *inspectCmdOptions) initialize(args []string) error { - // If --all-dependencies flag is set the workspace and dependencies directories need to have - // valid values. If not provided on the command line, the values need to be initialized with - // their default values. + // If --all-dependencies flag is set the dependencies directory needs to have a valid value. + // If not provided on the command line, the value needs to be initialized with the default. if command.AllDependencies { - // Create workspace directory to hold all intermediate files. - err := createAndSetWorkspaceDirectory(command) - if err != nil { - return err - } - // Move the integration dependecies to the dependencies directory. - err = createAndSetDependenciesDirectory(command) + err := createAndSetDependenciesDirectory(command) if err != nil { return err } @@ -192,10 +178,8 @@ func (command *inspectCmdOptions) run(args []string) error { // Add additional user-provided dependencies. if command.AdditionalDependencies != nil { for _, additionalDependency := range command.AdditionalDependencies { - additionalDependencies := strings.Split(additionalDependency, ",") - for _, dependency := range additionalDependencies { - dependencies = append(dependencies, dependency) - } + fmt.Printf(" Dep : %v \n", additionalDependency) + dependencies = append(dependencies, additionalDependency) } } @@ -306,7 +290,12 @@ func getTransitiveDependencies( } // Create local Maven context. - mc := maven.NewContext(path.Join(command.Workspace, "maven"), project) + temporaryDirectory, err := ioutil.TempDir(os.TempDir(), "maven-") + if err != nil { + return err + } + + mc := maven.NewContext(temporaryDirectory, project) mc.LocalRepository = mvn.LocalRepository mc.Timeout = mvn.GetTimeout().Duration @@ -331,40 +320,31 @@ func getTransitiveDependencies( } } + defer os.RemoveAll(temporaryDirectory) + return nil } -func createAndSetWorkspaceDirectory(command *inspectCmdOptions) error { - if command.Workspace == "" { - currentDirectory, err := os.Getwd() - if err != nil { - return err - } - command.Workspace = path.Join(currentDirectory, defaultWorkspaceDirectoryName) - } - - // Create the workspace directory if it does not already exist. - err := util.CreateDirectory(command.Workspace) +func getWorkingDirectory() (string, error) { + currentDirectory, err := os.Getwd() if err != nil { - return err + return "", err } - return nil + return currentDirectory, nil } func createAndSetDependenciesDirectory(command *inspectCmdOptions) error { if command.DependenciesDirectory == "" { - if command.Workspace == "" { - err := createAndSetWorkspaceDirectory(command) - if err != nil { - return err - } + currentDirectory, err := getWorkingDirectory() + if err != nil { + return err } - command.DependenciesDirectory = path.Join(command.Workspace, defaultDependenciesDirectoryName) + command.DependenciesDirectory = path.Join(currentDirectory, defaultDependenciesDirectoryName) } - // Create the workspace directory if it does not already exist. + // Create the dependencies directory if it does not already exist. err := util.CreateDirectory(command.DependenciesDirectory) if err != nil { return err
