-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
up: update readme template and re-generate README
- Loading branch information
Showing
8 changed files
with
655 additions
and
495 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,76 @@ | ||
package sysutil | ||
|
||
import ( | ||
"errors" | ||
"regexp" | ||
"runtime" | ||
"strings" | ||
) | ||
|
||
// GoVersion get go runtime version. eg: "1.18.2" | ||
func GoVersion() string { | ||
return runtime.Version()[2:] | ||
} | ||
|
||
// GoInfo define | ||
// | ||
// On os by: | ||
// | ||
// go env GOVERSION GOOS GOARCH | ||
// go version // "go version go1.19 darwin/amd64" | ||
type GoInfo struct { | ||
Version string | ||
GoOS string | ||
Arch string | ||
} | ||
|
||
// match "go version go1.19 darwin/amd64" | ||
var goVerRegex = regexp.MustCompile(`\sgo([\d.]+)\s(\w+)/(\w+)`) | ||
|
||
// ParseGoVersion get info by parse `go version` results. | ||
// | ||
// Examples: | ||
// | ||
// line, err := sysutil.ExecLine("go version") | ||
// if err != nil { | ||
// return err | ||
// } | ||
// | ||
// info, err := sysutil.ParseGoVersion() | ||
// dump.P(info) | ||
func ParseGoVersion(line string) (*GoInfo, error) { | ||
// eg: [" go1.19 darwin/amd64", "1.19", "darwin", "amd64"] | ||
lines := goVerRegex.FindStringSubmatch(line) | ||
if len(lines) != 4 { | ||
return nil, errors.New("returns go info is not full") | ||
} | ||
|
||
info := &GoInfo{} | ||
info.Version = strings.TrimPrefix(lines[1], "go") | ||
info.GoOS = lines[2] | ||
info.Arch = lines[3] | ||
|
||
return info, nil | ||
} | ||
|
||
// OsGoInfo fetch and parse | ||
func OsGoInfo() (*GoInfo, error) { | ||
cmdArgs := []string{"env", "GOVERSION", "GOOS", "GOARCH"} | ||
line, err := ExecCmd("go", cmdArgs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
lines := strings.Split(strings.TrimSpace(line), "\n") | ||
|
||
if len(lines) != len(cmdArgs)-1 { | ||
return nil, errors.New("returns go info is not full") | ||
} | ||
|
||
info := &GoInfo{} | ||
info.Version = strings.TrimPrefix(lines[0], "go") | ||
info.GoOS = lines[1] | ||
info.Arch = lines[2] | ||
|
||
return info, nil | ||
} |
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,25 @@ | ||
package sysutil_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/gookit/goutil/dump" | ||
"github.com/gookit/goutil/sysutil" | ||
"github.com/gookit/goutil/testutil/assert" | ||
) | ||
|
||
func TestGoVersion(t *testing.T) { | ||
assert.NotEmpty(t, sysutil.GoVersion()) | ||
|
||
info, err := sysutil.ParseGoVersion("go version go1.19.2 darwin/amd64") | ||
assert.NoErr(t, err) | ||
assert.NotEmpty(t, info) | ||
assert.Eq(t, "1.19.2", info.Version) | ||
assert.Eq(t, "darwin", info.GoOS) | ||
assert.Eq(t, "amd64", info.Arch) | ||
|
||
info, err = sysutil.OsGoInfo() | ||
assert.NoErr(t, err) | ||
assert.NotEmpty(t, info) | ||
dump.P(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
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