diff options
| author | Jeff Carr <[email protected]> | 2025-10-02 18:35:52 -0500 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-10-02 18:35:52 -0500 |
| commit | 435b9fdee2f51170aacf21f14a0118c89c84e166 (patch) | |
| tree | a30fdfd1bfa7db366be1a23ce80f0454c3bea9c1 | |
| parent | 45c1ecc507ab0943af87434b6661b2c3b7e1fa9e (diff) | |
make a smart argv() function
| -rw-r--r-- | bash.orig.go | 17 | ||||
| -rw-r--r-- | complete.go | 48 | ||||
| -rw-r--r-- | debugger.go | 10 |
3 files changed, 67 insertions, 8 deletions
diff --git a/bash.orig.go b/bash.orig.go index 52920eb..ecd6bdd 100644 --- a/bash.orig.go +++ b/bash.orig.go @@ -26,14 +26,15 @@ var myAuto *AutoArgs // this is a work in progress type AutoArgs struct { - id int // should be unique - hidden bool // don't update the toolkits when it's hidden - Auto func([]string) // the function for shell autocomplete - appName string // a good way to track the name of the binary ? - examples func() string // some examples - pp *arg.Parser // for parsing the command line args. Yay to alexf lint! - autoFunc func(*Auto) // also a function for autocomplete - match map[string]string // maps for strings + id int // should be unique + hidden bool // don't update the toolkits when it's hidden + Auto func([]string) // the function for shell autocomplete + appName string // a good way to track the name of the binary ? + examples func() string // some examples + buildtime func() (string, string) // some examples + pp *arg.Parser // for parsing the command line args. Yay to alexf lint! + autoFunc func(*Auto) // also a function for autocomplete + match map[string]string // maps for strings } // print out auto complete debugging info diff --git a/complete.go b/complete.go index f0b1ef9..03440b2 100644 --- a/complete.go +++ b/complete.go @@ -6,6 +6,7 @@ import ( "fmt" "os" "path/filepath" + "strconv" "strings" "time" @@ -221,6 +222,14 @@ func parseArgv(argname string) *Auto { os.Exit(0) } + if len(os.Args) > 1 && os.Args[1] == "--version" { + if myAuto.buildtime == nil { + // if binary defined buildtime() then process standard version output here + doVersion(pb) + os.Exit(0) + } + } + // should we do auto complete here? if len(os.Args) > 1 && os.Args[1] == "--auto-complete" { pb.IsAuto = true @@ -391,3 +400,42 @@ func doBash(argname string) { } os.Exit(0) } + +func doVersion(pb *Auto) { + if myAuto.buildtime == nil { + return + } + BUILDTIME, VERSION := myAuto.buildtime() + + parts := strings.Split(BUILDTIME, ".") + if len(parts) == 1 { + // The input epoch seconds + // epochSeconds := int64(1758646486) + num, err := strconv.Atoi(BUILDTIME) + epochSeconds := int64(num) + if err == nil { + + // 1. Convert the epoch seconds to a time.Time object. + // time.Unix() creates the time in the UTC timezone by default. + t := time.Unix(epochSeconds, 0) + + // 2. Convert the UTC time to the computer's local timezone. + localTime := t.Local() + + // 3. Print the result. The default format is clear and includes the timezone. + // fmt.Println("Default format:", localTime) + // For a more human-friendly format, use the Format() method. + // Go uses a special reference time for formatting: Mon Jan 2 15:04:05 2006 MST + // You lay out your desired format using these specific numbers. + // formattedString := localTime.Format("Monday, January 2, 2006 at 3:04:05 PM (MST)") + // fmt.Println(" Custom format:", formattedString) + + // now := time.Now() + // dur := time.Since(localTime) + BUILDTIME = fmt.Sprintf("%s age(%v)", localTime.String(), shell.FormatDuration(time.Since(localTime))) + } + } + + log.Infof("%s %s Built on %s\n", pb.Argname, VERSION, BUILDTIME) + os.Exit(0) +} diff --git a/debugger.go b/debugger.go index 238580f..7f20819 100644 --- a/debugger.go +++ b/debugger.go @@ -55,6 +55,10 @@ type AutoFuncd interface { DoAutoComplete(*Auto) } +type Buildtimed interface { + Buildtime() (string, string) +} + type Examplesd interface { // Version returns the version string that will be printed on a line by itself // at the top of the help message. @@ -69,6 +73,12 @@ func findAppInfo(tmp interface{}) { panic("you must define in your app the function: (argv) func Appname() string") } + if tmp, ok := tmp.(Buildtimed); ok { + myAuto.buildtime = tmp.Buildtime + } else { + // panic("you need to make the function argv.Appname()") + } + if tmp, ok := tmp.(Examplesd); ok { myAuto.examples = tmp.Examples } else { |
