Skip to content

Commit

Permalink
feat: add -version, also output in verbose mode
Browse files Browse the repository at this point in the history
Closes #34
  • Loading branch information
scop committed Nov 16, 2023
1 parent e64ae09 commit 28edff7
Showing 1 changed file with 65 additions and 2 deletions.
67 changes: 65 additions & 2 deletions wrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"path"
"path/filepath"
"runtime"
"runtime/debug"
"strconv"
"strings"
"syscall"
Expand All @@ -41,9 +42,59 @@ import (
)

var (
version = "dev"
version = "devel"
versionString = ""
)

func init() {
vs := make([]string, 0, 15)
vs = append(vs, "wrun ", version)
if bi, ok := debug.ReadBuildInfo(); ok {
if bi.GoVersion != "" {
vs = append(vs, ", built with ", bi.GoVersion)
}
for _, bs := range bi.Settings {
if bs.Key == "GOOS" {
vs = append(vs, ", for ", bs.Value)
for _, bs = range bi.Settings {
if bs.Key == "GOARCH" {
vs = append(vs, "/", bs.Value)
break
}
}
break
}
}
for _, bs := range bi.Settings {
if bs.Key == "vcs" {
vs = append(vs, ", from ", bs.Value)
for _, bs = range bi.Settings {
if bs.Key == "vcs.revision" {
vs = append(vs, " rev ", bs.Value)
break
}
}
for _, bs = range bi.Settings {
if bs.Key == "vcs.time" {
vs = append(vs, " dated ", bs.Value)
break
}
}
for _, bs = range bi.Settings {
if bs.Key == "vcs.modified" {
if dirty, err := strconv.ParseBool(bs.Value); err == nil && dirty {
vs = append(vs, " (dirty)")
}
break
}
}
break
}
}
}
versionString = strings.Join(vs, "")
}

const (
cacheHomeEnvVar = "WRUN_CACHE_HOME"
verboseEnvVar = "WRUN_VERBOSE"
Expand Down Expand Up @@ -128,6 +179,7 @@ type config struct {
archiveExePathMatches []archiveExePathMatch
usePreCommitCache bool
httpTimeout time.Duration
done bool
}

// parseFlags parses command line flags using the given flag set.
Expand Down Expand Up @@ -173,10 +225,17 @@ func parseFlags(set *flag.FlagSet, args []string) (config, error) {
})
set.BoolVar(&cfg.usePreCommitCache, "use-pre-commit-cache", false, "Use pre-commit's cache dir")
set.DurationVar(&cfg.httpTimeout, "http-timeout", defaultHttpTimeout, "HTTP client timeout")
set.BoolFunc("version", "Output version and exit", func(s string) error {
if _, err := fmt.Fprintln(set.Output(), versionString); err != nil {
return fmt.Errorf("write version: %w", err)
}
cfg.done = true
return nil
})
if err := set.Parse(args); err != nil {
return config{}, err
}
if len(cfg.urlMatches) == 0 {
if !cfg.done && len(cfg.urlMatches) == 0 {
err := errors.New("flag must occur at least once: -url")
_, _ = fmt.Fprintln(set.Output(), err)
set.Usage()
Expand Down Expand Up @@ -325,7 +384,11 @@ Environment variables:
rc = 2 // usage
}
return
} else if cfg.done {
// All done
return
}
infoOut("%s", versionString)

// Figure out download URL and exe path in archive

Expand Down

0 comments on commit 28edff7

Please sign in to comment.