options introduced
This commit is contained in:
parent
0a6362c21a
commit
bf77fac649
33
buildInfo.go
33
buildInfo.go
@ -11,21 +11,42 @@ type buildInfo struct {
|
||||
Deps []Dep
|
||||
}
|
||||
|
||||
type Options struct {
|
||||
BuildTime bool
|
||||
Commit bool
|
||||
OsArch bool
|
||||
Deps bool
|
||||
}
|
||||
|
||||
type Dep struct {
|
||||
Name string
|
||||
Version string
|
||||
Sum string
|
||||
}
|
||||
|
||||
func newBuildInfo() *buildInfo {
|
||||
func newBuildInfo(options Options) *buildInfo {
|
||||
buildTime := iff(options.BuildTime, BuildTime, "")
|
||||
commit := iff(options.Commit, Commit, "")
|
||||
os := iff(options.OsArch, OS, "")
|
||||
arch := iff(options.OsArch, Arch, "")
|
||||
showDeps := iff(options.Deps, true, false)
|
||||
|
||||
return &buildInfo{
|
||||
Name: Name,
|
||||
Version: Version,
|
||||
Commit: Commit,
|
||||
BuildTime: BuildTime,
|
||||
OS: OS,
|
||||
Arch: Arch,
|
||||
Deps: dependencies,
|
||||
ShowDeps: ShowDeps,
|
||||
|
||||
Commit: commit,
|
||||
BuildTime: buildTime,
|
||||
OS: os,
|
||||
Arch: arch,
|
||||
ShowDeps: showDeps,
|
||||
}
|
||||
}
|
||||
|
||||
func iff[T any](condition bool, trueValue, falseValue T) T {
|
||||
if condition {
|
||||
return trueValue
|
||||
}
|
||||
return falseValue
|
||||
}
|
||||
|
13
debug.go
13
debug.go
@ -1,13 +0,0 @@
|
||||
package buildinfo
|
||||
|
||||
import "fmt"
|
||||
|
||||
func Debug() {
|
||||
fmt.Printf("Name: %s\n", Name)
|
||||
fmt.Printf("Version: %s\n", Version)
|
||||
fmt.Printf("Commit: %s\n", Commit)
|
||||
fmt.Printf("BuildTime: %s\n", BuildTime)
|
||||
fmt.Printf("OS: %s\n", OS)
|
||||
fmt.Printf("Arch: %s\n", Arch)
|
||||
fmt.Printf("ShowDeps: %t\n", ShowDeps)
|
||||
}
|
11
version.go
11
version.go
@ -17,7 +17,6 @@ var (
|
||||
BuildTime string = ""
|
||||
OS string = ""
|
||||
Arch string = ""
|
||||
ShowDeps bool = true
|
||||
|
||||
dependencies = []Dep{}
|
||||
|
||||
@ -48,22 +47,22 @@ var (
|
||||
fmtTmpl = template.Must(template.New("print-format").Parse(PrintFormat))
|
||||
)
|
||||
|
||||
func Print() {
|
||||
Fprint(os.Stdout)
|
||||
func Print(options Options) {
|
||||
Fprint(os.Stdout, options)
|
||||
}
|
||||
|
||||
func Fprint(w io.Writer) {
|
||||
func Fprint(w io.Writer, options Options) {
|
||||
if Name == "" {
|
||||
Name = filepath.Base(os.Args[0])
|
||||
}
|
||||
|
||||
if OS == "" || Arch == "" || ShowDeps {
|
||||
if OS == "" || Arch == "" || options.Deps {
|
||||
fillDebugInfo()
|
||||
}
|
||||
|
||||
b := new(strings.Builder)
|
||||
|
||||
err := fmtTmpl.Execute(b, newBuildInfo())
|
||||
err := fmtTmpl.Execute(b, newBuildInfo(options))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user