Skip to content

Commit

Permalink
refactor: add build version
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 committed Mar 20, 2023
1 parent 74d6613 commit 9980af8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ release:
name_template: "v{{.Version}}"

builds:
- env:
- CGO_ENABLED=0
- env: [ CGO_ENABLED=0 ]
ldflags: -s -w

archives:
- name_template: "{{ .ProjectName }}_{{ .Os }}_{{ .Arch }}"
Expand Down
33 changes: 28 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ import (
"github.com/spf13/pflag"
)

var (
version = "dev"
commit = "none"
date = "unknown"
)

func main() {
var opt = Option{}

Expand All @@ -20,11 +26,18 @@ func main() {
cli.BoolVar(&opt.Quiet, "quiet", false, "hide command stdout/stderr")
flags, cmd := partitionCommand(os.Args[1:])
if len(cmd) == 0 {
// handle help message
fmt.Println("Usage: try [flags] -- command")
fmt.Println("\nflags:")
cli.PrintDefaults()
os.Exit(1)
if contains(flags, "--version") {
fmt.Printf("version: %s\n", version)
fmt.Printf("commit: %s\n", commit)
fmt.Printf("build at: %s\n", date)
os.Exit(0)
} else {
// handle help message
fmt.Println("Usage: try [flags] -- command")
fmt.Println("\nflags:")
cli.PrintDefaults()
os.Exit(1)
}
}
if err := cli.Parse(flags); err != nil {
fmt.Println(err.Error())
Expand Down Expand Up @@ -89,3 +102,13 @@ func partitionCommand(args []string) ([]string, []string) {

return args[:splitIndex], args[splitIndex+1:]
}

func contains(s []string, item string) bool {
for _, i := range s {
if i == item {
return true
}
}

return false
}

0 comments on commit 9980af8

Please sign in to comment.