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 334d848d86464c5c230a745f05670d0fcb46adf9 Author: Doru Bercea <[email protected]> AuthorDate: Wed Nov 11 14:32:58 2020 -0500 Add support for modeline options. --- pkg/cmd/modeline.go | 43 +++++++++++++++++++++------------- pkg/cmd/util_dependencies.go | 55 +++++++++++++++++++++++++++++++++++--------- pkg/cmd/util_sources.go | 46 +++++++++++++++++++++--------------- 3 files changed, 99 insertions(+), 45 deletions(-) diff --git a/pkg/cmd/modeline.go b/pkg/cmd/modeline.go index 4eab57a..b232110 100644 --- a/pkg/cmd/modeline.go +++ b/pkg/cmd/modeline.go @@ -155,27 +155,38 @@ func extractModelineOptions(ctx context.Context, sources []string) ([]modeline.O } for _, resolvedSource := range resolvedSources { - ops, err := modeline.Parse(resolvedSource.Location, resolvedSource.Content) + ops, err := ExtractModelineOptionsFromSource(resolvedSource) if err != nil { - return opts, errors.Wrapf(err, "cannot process file %s", resolvedSource.Location) + return opts, err } - for i, o := range ops { - if disallowedOptions[o.Name] { - return opts, fmt.Errorf("option %q is disallowed in modeline", o.Name) - } - if fileOptions[o.Name] && resolvedSource.Local { - baseDir := filepath.Dir(resolvedSource.Origin) - refPath := o.Value - if !filepath.IsAbs(refPath) { - full := path.Join(baseDir, refPath) - o.Value = full - ops[i] = o - } - } - } opts = append(opts, ops...) } return opts, nil } + +// ExtractModelineOptionsFromSource -- +func ExtractModelineOptionsFromSource(resolvedSource Source) ([]modeline.Option, error) { + ops, err := modeline.Parse(resolvedSource.Location, resolvedSource.Content) + if err != nil { + return ops, errors.Wrapf(err, "cannot process file %s", resolvedSource.Location) + } + for i, o := range ops { + if disallowedOptions[o.Name] { + return ops, fmt.Errorf("option %q is disallowed in modeline", o.Name) + } + + if fileOptions[o.Name] && resolvedSource.Local { + baseDir := filepath.Dir(resolvedSource.Origin) + refPath := o.Value + if !filepath.IsAbs(refPath) { + full := path.Join(baseDir, refPath) + o.Value = full + ops[i] = o + } + } + } + + return ops, nil +} diff --git a/pkg/cmd/util_dependencies.go b/pkg/cmd/util_dependencies.go index cd29a3e..e21ca89 100644 --- a/pkg/cmd/util_dependencies.go +++ b/pkg/cmd/util_dependencies.go @@ -41,6 +41,10 @@ var mavenWorkingDirectory string = "" var acceptedDependencyTypes = []string{"bom", "camel", "camel-k", "camel-quarkus", "mvn", "github"} +var additionalDependencyUsageMessage = `Additional top-level dependencies are specified with the format: +<type>:<dependency-name> +where <type> is one of {` + strings.Join(acceptedDependencyTypes, "|") + `}.` + const defaultDependenciesDirectoryName = "dependencies" func getDependencies(args []string, additionalDependencies []string, allDependencies bool) ([]string, error) { @@ -92,6 +96,31 @@ func getTopLevelDependencies(catalog *camel.RuntimeCatalog, args []string) ([]st // Extract list of top-level dependencies. dependencies.Merge(trait.AddSourceDependencies(sourceSpec, catalog)) + + // Extract modeline dependencies from file and add them to the list of + // top-level dependencies. + resolvedSource, err := ResolveLocalSource(source, false) + if err != nil { + return []string{}, err + } + + opts, err := ExtractModelineOptionsFromSource(resolvedSource) + if err != nil { + return []string{}, err + } + + for _, o := range opts { + if o.Name == "dependency" { + // Make sure dependency is valid. + isValid := validateDependency(o.Value) + if !isValid { + return []string{}, errors.New("Unexpected type for modeline dependency: " + o.Value + ". " + additionalDependencyUsageMessage) + } + + // Only valid modeline dependencies are added to the top level dependencies list. + dependencies.Add(o.Value) + } + } } return dependencies.List(), nil @@ -257,23 +286,27 @@ func validateAdditionalDependencies(additionalDependencies []string) error { // a valid type. if additionalDependencies != nil { for _, additionalDependency := range additionalDependencies { - dependencyComponents := strings.Split(additionalDependency, ":") - - TypeIsValid := false - for _, dependencyType := range acceptedDependencyTypes { - if dependencyType == dependencyComponents[0] { - TypeIsValid = true - } + isValid := validateDependency(additionalDependency) + if !isValid { + return errors.New("Unexpected type for user-provided dependency: " + additionalDependency + ". " + additionalDependencyUsageMessage) } + } + } - if !TypeIsValid { - return errors.New("Unexpected type for user-provided dependency: " + additionalDependency + ", check command usage for valid format.") - } + return nil +} +func validateDependency(additionalDependency string) bool { + dependencyComponents := strings.Split(additionalDependency, ":") + + TypeIsValid := false + for _, dependencyType := range acceptedDependencyTypes { + if dependencyType == dependencyComponents[0] { + TypeIsValid = true } } - return nil + return TypeIsValid } func validateIntegrationForDependencies(args []string, additionalDependencies []string) error { diff --git a/pkg/cmd/util_sources.go b/pkg/cmd/util_sources.go index af218f0..50a0838 100644 --- a/pkg/cmd/util_sources.go +++ b/pkg/cmd/util_sources.go @@ -66,27 +66,10 @@ func ResolveSources(ctx context.Context, locations []string, compress bool) ([]S for _, location := range locations { if isLocal(location) { - if _, err := os.Stat(location); err != nil && os.IsNotExist(err) { - return sources, errors.Wrapf(err, "file %s does not exist", location) - } else if err != nil { - return sources, errors.Wrapf(err, "error while accessing file %s", location) - } - - answer := Source{ - Name: path.Base(location), - Origin: location, - Location: location, - Compress: compress, - Local: true, - } - - content, err := ioutil.ReadFile(location) + answer, err := ResolveLocalSource(location, compress) if err != nil { return sources, err } - if err := answer.setContent(content); err != nil { - return sources, err - } sources = append(sources, answer) } else { @@ -199,3 +182,30 @@ func ResolveSources(ctx context.Context, locations []string, compress bool) ([]S return sources, nil } + +// ResolveLocalSource -- +func ResolveLocalSource(location string, compress bool) (Source, error) { + if _, err := os.Stat(location); err != nil && os.IsNotExist(err) { + return Source{}, errors.Wrapf(err, "file %s does not exist", location) + } else if err != nil { + return Source{}, errors.Wrapf(err, "error while accessing file %s", location) + } + + answer := Source{ + Name: path.Base(location), + Origin: location, + Location: location, + Compress: compress, + Local: true, + } + + content, err := ioutil.ReadFile(location) + if err != nil { + return Source{}, err + } + if err := answer.setContent(content); err != nil { + return Source{}, err + } + + return answer, nil +}
