Skip to content

Commit

Permalink
👔 update: goinfo - move parse go info func to package goinfo from sys…
Browse files Browse the repository at this point in the history
…util
  • Loading branch information
inhere committed Jul 19, 2023
1 parent 19943b5 commit dd238bb
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 50 deletions.
71 changes: 70 additions & 1 deletion goinfo/goinfo.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,78 @@
// Package goinfo provide some standard util functions for go.
package goinfo

import "runtime"
import (
"errors"
"os/exec"
"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 := goinfo.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{
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"}
bs, err := exec.Command("go", cmdArgs...).Output()
if err != nil {
return nil, err
}

lines := strings.Split(strings.TrimSpace(string(bs)), "\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
}
17 changes: 17 additions & 0 deletions goinfo/goinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,27 @@ package goinfo_test
import (
"testing"

"github.com/gookit/goutil/dump"
"github.com/gookit/goutil/goinfo"
"github.com/gookit/goutil/testutil/assert"
)

func TestGoVersion(t *testing.T) {
assert.NotEmpty(t, goinfo.GoVersion())
}

func TestOsGoInfo(t *testing.T) {
assert.NotEmpty(t, goinfo.GoVersion())

info, err := goinfo.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 = goinfo.OsGoInfo()
assert.NoErr(t, err)
assert.NotEmpty(t, info)
dump.P(info)
}
57 changes: 8 additions & 49 deletions sysutil/sysgo.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,20 @@
package sysutil

import (
"errors"
"regexp"
"runtime"
"strings"

"github.com/gookit/goutil/goinfo"
)

// 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+)`)
// GoInfo define. alias of goinfo.GoInfo
type GoInfo = goinfo.GoInfo

// ParseGoVersion get info by parse `go version` results.
// ParseGoVersion get info by parse `go version` results. alias of goinfo.ParseGoVersion()
//
// Examples:
//
Expand All @@ -39,38 +26,10 @@ var goVerRegex = regexp.MustCompile(`\sgo([\d.]+)\s(\w+)/(\w+)`)
// 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
return goinfo.ParseGoVersion(line)
}

// OsGoInfo fetch and parse
// OsGoInfo fetch and parse. alias of goinfo.OsGoInfo()
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
return goinfo.OsGoInfo()
}

0 comments on commit dd238bb

Please sign in to comment.