From bf77fac649be1437d57b352aa2b646340e3be5e8 Mon Sep 17 00:00:00 2001 From: Timon Ringwald Date: Mon, 29 Aug 2022 10:30:43 +0200 Subject: [PATCH] options introduced --- buildInfo.go | 39 ++++++++++++++++++++++++++++++--------- debug.go | 13 ------------- version.go | 11 +++++------ 3 files changed, 35 insertions(+), 28 deletions(-) delete mode 100644 debug.go diff --git a/buildInfo.go b/buildInfo.go index 5daefa4..43b6db6 100644 --- a/buildInfo.go +++ b/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, + 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 +} diff --git a/debug.go b/debug.go deleted file mode 100644 index 7ae05d4..0000000 --- a/debug.go +++ /dev/null @@ -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) -} diff --git a/version.go b/version.go index b7230bf..6d4db49 100644 --- a/version.go +++ b/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) }