From 435b9fdee2f51170aacf21f14a0118c89c84e166 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Thu, 2 Oct 2025 18:35:52 -0500 Subject: make a smart argv() function --- complete.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'complete.go') 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) +} -- cgit v1.2.3