Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

Commit

Permalink
Clean filpath for os.open func
Browse files Browse the repository at this point in the history
Signed-off-by: thepetk <[email protected]>
  • Loading branch information
thepetk committed Jun 1, 2023
1 parent e0bac85 commit 1d6a565
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 75 deletions.
15 changes: 3 additions & 12 deletions go/pkg/apis/enricher/framework/go/echo_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ package enricher

import (
"context"
"os"
"regexp"

"github.com/redhat-developer/alizer/go/pkg/apis/model"
Expand Down Expand Up @@ -59,16 +58,8 @@ func (e EchoDetector) DoPortsDetection(component *model.Component, ctx *context.
},
}

for _, file := range files {
bytes, err := os.ReadFile(file)
if err != nil {
continue
}
ports := GetPortFromFileGo(matchRegexRules, string(bytes))
if len(ports) > 0 {
component.Ports = ports
return
}
ports := GetPortFromFilesGo(matchRegexRules, files)
if len(ports) > 0 {
component.Ports = ports
}

}
17 changes: 4 additions & 13 deletions go/pkg/apis/enricher/framework/go/fasthttp_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ package enricher

import (
"context"
"os"
"regexp"

"github.com/redhat-developer/alizer/go/pkg/apis/model"
Expand All @@ -40,24 +39,16 @@ func (f FastHttpDetector) DoPortsDetection(component *model.Component, ctx *cont
return
}

matchRegexRule := model.PortMatchRules{
matchRegexRules := model.PortMatchRules{
MatchIndexRegexes: []model.PortMatchRule{
{
Regex: regexp.MustCompile(`.ListenAndServe\([^,)]*`),
ToReplace: ".ListenAndServe(",
},
},
}
for _, file := range files {
bytes, err := os.ReadFile(file)
if err != nil {
continue
}
ports := GetPortFromFileGo(matchRegexRule, string(bytes))
if len(ports) > 0 {
component.Ports = ports
return
}
ports := GetPortFromFilesGo(matchRegexRules, files)
if len(ports) > 0 {
component.Ports = ports
}

}
16 changes: 4 additions & 12 deletions go/pkg/apis/enricher/framework/go/gin_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ package enricher

import (
"context"
"os"
"regexp"

"github.com/redhat-developer/alizer/go/pkg/apis/model"
Expand All @@ -40,7 +39,7 @@ func (g GinDetector) DoPortsDetection(component *model.Component, ctx *context.C
return
}

matchRegexRule := model.PortMatchRules{
matchRegexRules := model.PortMatchRules{
MatchIndexRegexes: []model.PortMatchRule{
{
Regex: regexp.MustCompile(`.Run\(([^,)]*)`),
Expand All @@ -49,15 +48,8 @@ func (g GinDetector) DoPortsDetection(component *model.Component, ctx *context.C
},
}

for _, file := range files {
bytes, err := os.ReadFile(file)
if err != nil {
continue
}
ports := GetPortFromFileGo(matchRegexRule, string(bytes))
if len(ports) > 0 {
component.Ports = ports
return
}
ports := GetPortFromFilesGo(matchRegexRules, files)
if len(ports) > 0 {
component.Ports = ports
}
}
31 changes: 21 additions & 10 deletions go/pkg/apis/enricher/framework/go/go_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ package enricher
import (
"context"
"os"
"path/filepath"
"regexp"
"strings"

Expand Down Expand Up @@ -57,16 +58,9 @@ func DoGoPortsDetection(component *model.Component, ctx *context.Context) {
},
}

for _, file := range files {
bytes, err := os.ReadFile(file)
if err != nil {
continue
}
ports := GetPortFromFileGo(matchRegexRules, string(bytes))
if len(ports) > 0 {
component.Ports = ports
return
}
ports := GetPortFromFilesGo(matchRegexRules, files)
if len(ports) > 0 {
component.Ports = ports
}
}

Expand Down Expand Up @@ -134,3 +128,20 @@ func GetPortWithMatchIndexesGo(content string, matchIndexes []int, toBeReplaced

return -1
}

// GetPortFromFilesGo loops through a list of paths and tries to find a port matching the
// given set PortMatchRules
func GetPortFromFilesGo(matchRegexRules model.PortMatchRules, files []string) []int {
for _, file := range files {
cleanFile := filepath.Clean(file)
bytes, err := os.ReadFile(cleanFile)
if err != nil {
continue
}
ports := GetPortFromFileGo(matchRegexRules, string(bytes))
if len(ports) > 0 {
return ports
}
}
return []int{}
}
16 changes: 4 additions & 12 deletions go/pkg/apis/enricher/framework/go/gofiber_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ package enricher

import (
"context"
"os"
"regexp"

"github.com/redhat-developer/alizer/go/pkg/apis/model"
Expand All @@ -40,23 +39,16 @@ func (g GoFiberDetector) DoPortsDetection(component *model.Component, ctx *conte
return
}

matchRegexRule := model.PortMatchRules{
matchRegexRules := model.PortMatchRules{
MatchIndexRegexes: []model.PortMatchRule{
{
Regex: regexp.MustCompile(`.Listen\(([^,)]*)`),
ToReplace: ".Listen(",
},
},
}
for _, file := range files {
bytes, err := os.ReadFile(file)
if err != nil {
continue
}
ports := GetPortFromFileGo(matchRegexRule, string(bytes))
if len(ports) > 0 {
component.Ports = ports
return
}
ports := GetPortFromFilesGo(matchRegexRules, files)
if len(ports) > 0 {
component.Ports = ports
}
}
14 changes: 3 additions & 11 deletions go/pkg/apis/enricher/framework/go/mux_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ package enricher

import (
"context"
"os"
"regexp"

"github.com/redhat-developer/alizer/go/pkg/apis/model"
Expand Down Expand Up @@ -55,15 +54,8 @@ func (m MuxDetector) DoPortsDetection(component *model.Component, ctx *context.C
},
}

for _, file := range files {
bytes, err := os.ReadFile(file)
if err != nil {
continue
}
ports := GetPortFromFileGo(matchRegexRules, string(bytes))
if len(ports) > 0 {
component.Ports = ports
return
}
ports := GetPortFromFilesGo(matchRegexRules, files)
if len(ports) > 0 {
component.Ports = ports
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ package enricher
import (
"context"
"os"
"path/filepath"
"regexp"
"strings"

Expand Down Expand Up @@ -43,7 +44,8 @@ func (e ExpressDetector) DoPortsDetection(component *model.Component, ctx *conte
re := regexp.MustCompile(`\.listen\([^,)]*`)
var ports []int
for _, file := range files {
bytes, err := os.ReadFile(file)
cleanFile := filepath.Clean(file)
bytes, err := os.ReadFile(cleanFile)
if err != nil {
continue
}
Expand Down
3 changes: 2 additions & 1 deletion go/pkg/apis/enricher/java_enricher.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ func getProjectNameGradle(root string) string {
settingsGradlePath := filepath.Join(root, "settings.gradle")
if _, err := os.Stat(settingsGradlePath); err == nil {
re := regexp.MustCompile(`rootProject.name\s*=\s*(.*)`)
bytes, err := os.ReadFile(settingsGradlePath)
cleanSettingsGradlePath := filepath.Clean(settingsGradlePath)
bytes, err := os.ReadFile(cleanSettingsGradlePath)
if err != nil {
return ""
}
Expand Down
9 changes: 6 additions & 3 deletions go/pkg/utils/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ func isTagInDependencies(deps map[string]string, tag string) bool {

// GetPackageJsonSchemaFromFile returns the package.json found in the path.
func GetPackageJsonSchemaFromFile(path string) (schema.PackageJson, error) {
bytes, err := os.ReadFile(path)
cleanPath := filepath.Clean(path)
bytes, err := os.ReadFile(cleanPath)
if err != nil {
return schema.PackageJson{}, err
}
Expand Down Expand Up @@ -216,7 +217,8 @@ func IsTagInComposerJsonFile(file string, tag string) bool {

// GetComposerJsonSchemaFromFile returns the composer.json found in the path.
func GetComposerJsonSchemaFromFile(path string) (schema.ComposerJson, error) {
bytes, err := os.ReadFile(path)
cleanPath := filepath.Clean(path)
bytes, err := os.ReadFile(cleanPath)
if err != nil {
return schema.ComposerJson{}, err
}
Expand Down Expand Up @@ -513,7 +515,8 @@ func GetStringValueFromEnvFile(root string, regex string) string {
// getEnvFileContent is exposed as a global variable for the purpose of running mock tests
var getEnvFileContent = func(root string) (string, error) {
envPath := filepath.Join(root, ".env")
bytes, err := os.ReadFile(envPath)
cleanEnvPath := filepath.Clean(envPath)
bytes, err := os.ReadFile(cleanEnvPath)
if err != nil {
return "", err
}
Expand Down

0 comments on commit 1d6a565

Please sign in to comment.