diff --git a/.gitignore b/.gitignore index 7c0b9f0..d41d141 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ *.so *.dylib *.syso +/yutu # Generated by IDE .idea diff --git a/.goreleaser.yaml b/.goreleaser.yaml index cbf85f4..bf31ca1 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -10,7 +10,8 @@ version: 2 project_name: yutu env: - - MODULE_PATH=github.com/{{ .Env.GITHUB_REPOSITORY }} + - MODULE_PATH=github.com/{{ if index .Env "GITHUB_REPOSITORY" }}{{ .Env.GITHUB_REPOSITORY }}{{ else }}eat-pray-ai/yutu{{ end }} + - Builder={{ if index .Env "GITHUB_ACTOR" }}{{ .Env.GITHUB_ACTOR }}{{ else }}Gopher{{ end }} before: hooks: @@ -53,10 +54,11 @@ builds: - -X {{ .Env.MODULE_PATH }}/cmd.CommitDate={{ .CommitDate }} - -X {{ .Env.MODULE_PATH }}/cmd.Os={{ .Os }} - -X {{ .Env.MODULE_PATH }}/cmd.Arch={{ .Arch }} + - -X {{ .Env.MODULE_PATH }}/cmd.Builder={{ .Env.Builder }} mod_timestamp: "{{ .CommitTimestamp }}" archives: - - formats: [ 'binary' ] + - formats: ["binary"] ids: - yutu name_template: "{{ .Binary }}" @@ -96,4 +98,3 @@ changelog: release: discussion_category_name: General - diff --git a/BUILD.bazel b/BUILD.bazel index d7886f5..a579583 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -46,13 +46,6 @@ go_binary( name = "yutu", embed = [":yutu_lib"], visibility = ["//visibility:public"], - x_defs = { - "Version": "{STABLE_VERSION}", - "Commit": "{STABLE_GIT_SHORT_SHA}", - "CommitDate": "{STABLE_GIT_COMMIT_DATE}", - "Os": "{STABLE_OS}", - "Arch": "{STABLE_ARCH}", - }, ) go_cross_binary( diff --git a/cmd/BUILD.bazel b/cmd/BUILD.bazel index a04b8f8..10d77d2 100644 --- a/cmd/BUILD.bazel +++ b/cmd/BUILD.bazel @@ -16,6 +16,7 @@ go_library( "CommitDate": "{STABLE_GIT_COMMIT_DATE}", "Os": "{STABLE_OS}", "Arch": "{STABLE_ARCH}", + "Builder": "{STABLE_Builder}", }, deps = [ "//pkg", diff --git a/cmd/version.go b/cmd/version.go index f83454d..0f31dfc 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -4,13 +4,17 @@ package cmd import ( + "runtime/debug" + "github.com/savioxavier/termlink" "github.com/spf13/cobra" ) const ( - versionShort = "Show the version of yutu" - versionLong = "Show the version of yutu" + verShort = "Show the version of yutu" + verLong = "Show the version of yutu" + repo = "Github/eat-pray-ai/yutu" + repoUrl = "https://github.com/eat-pray-ai/yutu" ) var ( @@ -19,20 +23,37 @@ var ( CommitDate = "" Os = "" Arch = "" - repo = "https://github.com/eat-pray-ai/yutu" + Builder = "Gopher" ) var versionCmd = &cobra.Command{ Use: "version", - Short: versionShort, - Long: versionLong, + Short: verShort, + Long: verLong, Run: func(cmd *cobra.Command, args []string) { - cmd.Printf("🐰yutu version %s %s/%s", Version, Os, Arch) - if Commit != "" && CommitDate != "" { - cmd.Printf("\n📦build %s-%s", Commit, CommitDate) + info, ok := debug.ReadBuildInfo() + if ok && Version == "" { + Version = info.Main.Version + + settings := make(map[string]string) + for _, setting := range info.Settings { + settings[setting.Key] = setting.Value + } + + if val, exists := settings["vcs.time"]; exists { + CommitDate = val + } + if val, exists := settings["GOOS"]; exists { + Os = val + } + if val, exists := settings["GOARCH"]; exists { + Arch = val + } } - cmd.Println("\n🌟Star:", termlink.Link("Github/eat-pray-ai/yutu", repo)) + cmd.Printf("🐰yutu %s %s/%s\n", Version, Os, Arch) + cmd.Printf("📦build %s-%s\n", Builder, CommitDate) + cmd.Printf("🌟Star: %s\n", termlink.Link(repo, repoUrl)) }, } diff --git a/docs/CONTRIBUTING.md b/docs/CONTRIBUTING.md index a144597..f3aad2a 100644 --- a/docs/CONTRIBUTING.md +++ b/docs/CONTRIBUTING.md @@ -16,13 +16,13 @@ Here are some commands which may useful. ## run tests ### unit tests ❯ go test ./... -coverprofile=./cover.out -coverpkg="$(go list || go list -m | head -1)/pkg/..." -❯ go tool cover -html=cover.out -o=cover.html +❯ go tool cover -html=cover.out -o=cover.html ## build the binary ### install goreleaser: https://goreleaser.com/install -❯ GITHUB_REPOSITORY=eat-pray-ai/yutu goreleaser build --clean --auto-snapshot +❯ goreleaser build --clean --auto-snapshot -❯ tree dist +❯ tree dist dist ├── artifacts.json ├── config.yaml diff --git a/scripts/bazel-status.sh b/scripts/bazel-status.sh index e861096..dd4b988 100755 --- a/scripts/bazel-status.sh +++ b/scripts/bazel-status.sh @@ -19,3 +19,4 @@ fi echo STABLE_OS "$(uname -s | tr '[:upper:]' '[:lower:]')" echo STABLE_ARCH "$(uname -m | sed 's/x86_64/amd64/' | tr '[:upper:]' '[:lower:]')" +echo STABLE_Builder "${GITHUB_ACTOR:-$USER}" diff --git a/scripts/command-test.sh b/scripts/command-test.sh index ee19317..fc005e4 100755 --- a/scripts/command-test.sh +++ b/scripts/command-test.sh @@ -17,7 +17,8 @@ if [[ -z "$YUTU_PATH" ]]; then CommitDate="${MOD}.CommitDate=$(git log -1 --date='format:%Y-%m-%dT%H:%M:%SZ' --pretty=%cd)" Os="${MOD}.Os=$(go env GOOS)" Arch="${MOD}.Arch=$(go env GOARCH)" - ldflags="-s -X ${Version} -X ${Commit} -X ${CommitDate} -X ${Os} -X ${Arch}" + Builder="${MOD}.Builder=${GITHUB_ACTOR:-$USER}" + ldflags="-s -X ${Version} -X ${Commit} -X ${CommitDate} -X ${Os} -X ${Arch} -X ${Builder}" go mod download go build -ldflags "${ldflags}" -o yutu .