-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ 🐛 Reading Go Runtime Details from GOENV (#17)
* ✨ 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
Showing
3 changed files
with
69 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |