diff --git a/pkg/generate/app/sourcelookup.go b/pkg/generate/app/sourcelookup.go index 3ddd6bc63ff7..116a28f216e3 100644 --- a/pkg/generate/app/sourcelookup.go +++ b/pkg/generate/app/sourcelookup.go @@ -502,7 +502,7 @@ func (e SourceRepositoryEnumerator) Detect(dir string, noSourceDetection bool) ( // is docker or pipeline if !noSourceDetection { for _, d := range e.Detectors { - if detected, ok := d(dir); ok { + if detected := d(dir); detected != nil { info.Types = append(info.Types, SourceLanguageType{ Platform: detected.Platform, Version: detected.Version, diff --git a/pkg/generate/source/detector.go b/pkg/generate/source/detector.go index 60ca1e8fe46c..cdf0bc360ab7 100644 --- a/pkg/generate/source/detector.go +++ b/pkg/generate/source/detector.go @@ -1,9 +1,6 @@ package source -import ( - "os" - "path/filepath" -) +import "path/filepath" // Info is detected platform information from a source directory type Info struct { @@ -13,7 +10,7 @@ type Info struct { // DetectorFunc is a function that returns source Info from a given directory. // It returns true if it was able to detect the code in the given directory. -type DetectorFunc func(dir string) (*Info, bool) +type DetectorFunc func(dir string) *Info // Detectors is a set of DetectorFunc that is used to detect the // language/platform for a given source directory @@ -33,66 +30,56 @@ var DefaultDetectors = Detectors{ } // DetectRuby detects Ruby source -func DetectRuby(dir string) (*Info, bool) { +func DetectRuby(dir string) *Info { return detect("ruby", dir, "Gemfile", "Rakefile", "config.ru") } // DetectJava detects Java source -func DetectJava(dir string) (*Info, bool) { +func DetectJava(dir string) *Info { return detect("jee", dir, "pom.xml") } // DetectNodeJS detects NodeJS source -func DetectNodeJS(dir string) (*Info, bool) { +func DetectNodeJS(dir string) *Info { return detect("nodejs", dir, "app.json", "package.json") } // DetectPHP detects PHP source -func DetectPHP(dir string) (*Info, bool) { +func DetectPHP(dir string) *Info { return detect("php", dir, "index.php", "composer.json") } // DetectPython detects Python source -func DetectPython(dir string) (*Info, bool) { +func DetectPython(dir string) *Info { return detect("python", dir, "requirements.txt", "setup.py") } // DetectPerl detects Perl source -func DetectPerl(dir string) (*Info, bool) { +func DetectPerl(dir string) *Info { return detect("perl", dir, "index.pl", "cpanfile") } // DetectScala detects Scala source -func DetectScala(dir string) (*Info, bool) { +func DetectScala(dir string) *Info { return detect("scala", dir, "build.sbt") } -// DetectDotNet detects .NET source and matches it to a dotnet supported annotatin or dotnet imagestream name -func DetectDotNet(dir string) (*Info, bool) { - return detect("dotnet", dir, "project.json") +// DetectDotNet detects .NET source and matches it to a dotnet supported annotation or dotnet imagestream name +func DetectDotNet(dir string) *Info { + return detect("dotnet", dir, "project.json", "*.csproj") } // DetectLiteralDotNet detects .NET source and matches it to a .net supported annotation -func DetectLiteralDotNet(dir string) (*Info, bool) { - return detect(".net", dir, "project.json") +func DetectLiteralDotNet(dir string) *Info { + return detect(".net", dir, "project.json", "*.csproj") } // detect returns an Info object with the given platform if the source at dir contains any of the argument files -func detect(platform string, dir string, files ...string) (*Info, bool) { - if filesPresent(dir, files) { - return &Info{ - Platform: platform, - }, true - } - return nil, false -} - -func filesPresent(dir string, files []string) bool { - for _, f := range files { - _, err := os.Stat(filepath.Join(dir, f)) - if err == nil { - return true +func detect(platform string, dir string, globs ...string) *Info { + for _, g := range globs { + if matches, _ := filepath.Glob(filepath.Join(dir, g)); len(matches) > 0 { + return &Info{Platform: platform} } } - return false + return nil }