Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ 🐛 Reading Go Runtime Details from GOENV #17

Merged
merged 3 commits into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
}