Skip to content

Commit

Permalink
✨ 🐛 Reading Go Runtime Details from GOENV (#17)
Browse files Browse the repository at this point in the history
* ✨ Reading Go Runtime Infos

To avoid reading the runtime information of the compiled code vs the real one used by govulncheck

* ♻️ Streamlined Function

* ✅ Added test
  • Loading branch information
Templum authored Nov 17, 2022
1 parent d00d03e commit f115ae3
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
10 changes: 6 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package main

import (
"os"
"runtime"

"github.com/Templum/govulncheck-action/pkg/action"
"github.com/Templum/govulncheck-action/pkg/github"
"github.com/Templum/govulncheck-action/pkg/sarif"
"github.com/Templum/govulncheck-action/pkg/vulncheck"
Expand Down Expand Up @@ -33,10 +33,12 @@ func main() {
logger.Debug().Msg("Enabled Local Development mode, scanner will return static result based on found.json")
}

info := action.ReadRuntimeInfoFromEnv()

logger.Info().
Str("Go-Version", runtime.Version()).
Str("Go-Os", runtime.GOOS).
Str("Go-Arch", runtime.GOARCH).
Str("Go-Version", info.Version).
Str("Go-Os", info.Os).
Str("Go-Arch", info.Arch).
Msg("GoEnvironment Details:")

logger.Debug().
Expand Down
43 changes: 43 additions & 0 deletions pkg/action/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package action

import (
"os/exec"
"strings"
)

type RuntimeInfos struct {
Version string
Os string
Arch string
}

// ReadRuntimeInfoFromEnv using go env this ensures the real information are used and no compile time versions
func ReadRuntimeInfoFromEnv() *RuntimeInfos {
cmd := exec.Command("go", "env")
out, _ := cmd.Output()

info := RuntimeInfos{Version: "Unknown", Os: "Unknown", Arch: "Unknown"}

envs := strings.Split(string(out), "\n")

for _, env := range envs {

if strings.Contains(env, "GOARCH") {
keyVal := strings.SplitAfter(env, "=")
info.Arch = strings.Trim(keyVal[1], "\"")
}

if strings.Contains(env, "GOVERSION") {
keyVal := strings.SplitAfter(env, "=")
info.Version = strings.Trim(keyVal[1], "\"")
}

if strings.Contains(env, "GOOS") {
keyVal := strings.SplitAfter(env, "=")
info.Os = strings.Trim(keyVal[1], "\"")
}

}

return &info
}
20 changes: 20 additions & 0 deletions pkg/action/env_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package action

import (
"runtime"
"testing"

"github.com/stretchr/testify/assert"
)

func TestReadRuntimeInfoFromEnv(t *testing.T) {
t.Run("should go runtime information from go env", func(t *testing.T) {
info := ReadRuntimeInfoFromEnv()

assert.NotNil(t, info, "should not return nil")

assert.Equal(t, runtime.Version(), info.Version)
assert.Equal(t, runtime.GOOS, info.Os)
assert.Equal(t, runtime.GOARCH, info.Arch)
})
}

0 comments on commit f115ae3

Please sign in to comment.