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

Commit

Permalink
Enhance go port recognition (#181)
Browse files Browse the repository at this point in the history
* Add udi image case for mocked values of devfile types

Signed-off-by: thepetk <thepetk@gmail.com>

* Add Go ports detection for project without framework

Signed-off-by: thepetk <thepetk@gmail.com>

* Add tests for raw go project port recognition

Signed-off-by: thepetk <thepetk@gmail.com>

* Remove unnecessary comments on test project

Signed-off-by: thepetk <thepetk@gmail.com>

---------

Signed-off-by: thepetk <thepetk@gmail.com>
  • Loading branch information
thepetk authored Apr 19, 2023
1 parent 065a8a4 commit 894ac41
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
40 changes: 40 additions & 0 deletions go/pkg/apis/enricher/framework/go/go_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
package enricher

import (
"context"
"os"
"regexp"
"strings"

Expand All @@ -28,6 +30,44 @@ func hasFramework(modules []*modfile.Require, tag string) bool {
return false
}

func DoGoPortsDetection(component *model.Component, ctx *context.Context) {
files, err := utils.GetCachedFilePathsFromRoot(component.Path, ctx)
if err != nil {
return
}

matchRegexRules := model.PortMatchRules{
MatchIndexRegexes: []model.PortMatchRule{
{
Regex: regexp.MustCompile(`.ListenAndServe\([^,)]*`),
ToReplace: ".ListenAndServe(",
},
{
Regex: regexp.MustCompile(`.Start\([^,)]*`),
ToReplace: ".Start(",
},
},
MatchRegexes: []model.PortMatchSubRule{
{
Regex: regexp.MustCompile(`Addr:\s+"([^",]+)`),
SubRegex: regexp.MustCompile(`:*(\d+)$`),
},
},
}

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
}
}
}

func GetPortFromFileGo(rules model.PortMatchRules, text string) []int {
ports := []int{}
for _, matchIndexRegex := range rules.MatchIndexRegexes {
Expand Down
3 changes: 3 additions & 0 deletions go/pkg/apis/enricher/go_enricher.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ func (j GoEnricher) DoEnrichComponent(component *model.Component, settings model
}
}
}
if len(component.Ports) == 0 {
framework.DoGoPortsDetection(component, ctx)
}
}
}
if len(ports) > 0 {
Expand Down
4 changes: 4 additions & 0 deletions go/test/apis/component_recognizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,10 @@ func TestPortDetectionGoGin(t *testing.T) {
testPortDetectionInProject(t, "projectGin", []int{8789})
}

func TestPortDetectionGo(t *testing.T) {
testPortDetectionInProject(t, "projectGo", []int{8080})
}

func TestPortDetectionGoFiber(t *testing.T) {
testPortDetectionInProject(t, "projectGoFiber", []int{3000})
}
Expand Down
3 changes: 3 additions & 0 deletions resources/projectPortTesting/projectGo/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/burrsutter/go-net-http-hello

go 1.19
22 changes: 22 additions & 0 deletions resources/projectPortTesting/projectGo/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"fmt"
"net/http"
"os"
)

func main() {
http.HandleFunc("/", HelloHandler)
fmt.Println("Listening on localhost:8080")
http.ListenAndServe(":8080", nil)
}

func HelloHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
hostname, err := os.Hostname()
if err != nil {
fmt.Println("unable to get hostname")
}
fmt.Fprintf(w, "Go Hello on %s\n", hostname)
}

0 comments on commit 894ac41

Please sign in to comment.