options introduced
This commit is contained in:
parent
0a6362c21a
commit
bf77fac649
39
buildInfo.go
39
buildInfo.go
@ -11,21 +11,42 @@ type buildInfo struct {
|
|||||||
Deps []Dep
|
Deps []Dep
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Options struct {
|
||||||
|
BuildTime bool
|
||||||
|
Commit bool
|
||||||
|
OsArch bool
|
||||||
|
Deps bool
|
||||||
|
}
|
||||||
|
|
||||||
type Dep struct {
|
type Dep struct {
|
||||||
Name string
|
Name string
|
||||||
Version string
|
Version string
|
||||||
Sum 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{
|
return &buildInfo{
|
||||||
Name: Name,
|
Name: Name,
|
||||||
Version: Version,
|
Version: Version,
|
||||||
Commit: Commit,
|
Deps: dependencies,
|
||||||
BuildTime: BuildTime,
|
|
||||||
OS: OS,
|
Commit: commit,
|
||||||
Arch: Arch,
|
BuildTime: buildTime,
|
||||||
Deps: dependencies,
|
OS: os,
|
||||||
ShowDeps: ShowDeps,
|
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 = ""
|
BuildTime string = ""
|
||||||
OS string = ""
|
OS string = ""
|
||||||
Arch string = ""
|
Arch string = ""
|
||||||
ShowDeps bool = true
|
|
||||||
|
|
||||||
dependencies = []Dep{}
|
dependencies = []Dep{}
|
||||||
|
|
||||||
@ -48,22 +47,22 @@ var (
|
|||||||
fmtTmpl = template.Must(template.New("print-format").Parse(PrintFormat))
|
fmtTmpl = template.Must(template.New("print-format").Parse(PrintFormat))
|
||||||
)
|
)
|
||||||
|
|
||||||
func Print() {
|
func Print(options Options) {
|
||||||
Fprint(os.Stdout)
|
Fprint(os.Stdout, options)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Fprint(w io.Writer) {
|
func Fprint(w io.Writer, options Options) {
|
||||||
if Name == "" {
|
if Name == "" {
|
||||||
Name = filepath.Base(os.Args[0])
|
Name = filepath.Base(os.Args[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
if OS == "" || Arch == "" || ShowDeps {
|
if OS == "" || Arch == "" || options.Deps {
|
||||||
fillDebugInfo()
|
fillDebugInfo()
|
||||||
}
|
}
|
||||||
|
|
||||||
b := new(strings.Builder)
|
b := new(strings.Builder)
|
||||||
|
|
||||||
err := fmtTmpl.Execute(b, newBuildInfo())
|
err := fmtTmpl.Execute(b, newBuildInfo(options))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user