options introduced

This commit is contained in:
Timon Ringwald 2022-08-29 10:30:43 +02:00
parent 0a6362c21a
commit bf77fac649
3 changed files with 35 additions and 28 deletions

View File

@ -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,
Name: Name,
Version: Version,
Deps: dependencies,
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
}

View File

@ -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)
}

View File

@ -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)
}