From 64b29a3807917b6203b85d8f7f99ff622b947784 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Tue, 21 Oct 2025 09:21:42 -0500 Subject: callbacks to argv Exit() --- exit.go | 22 ++++++++++++++++++++++ init.go | 7 ++++++- structs.go | 3 +++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 exit.go diff --git a/exit.go b/exit.go new file mode 100644 index 0000000..6cf489f --- /dev/null +++ b/exit.go @@ -0,0 +1,22 @@ +package ENV + +import "os" + +// this is an experiment at this point to +// see how this turns out + +// argv normally sets these callbacks +// argv has timing set from when os.Args was parsed +func GoodExit(reason string, err error) { + if goodExit != nil { + goodExit(reason) + } + os.Exit(0) +} + +func BadExit(reason string, err error) { + if badExit != nil { + badExit(reason, err) + } + os.Exit(-1) +} diff --git a/init.go b/init.go index 3df1e17..a20b5d2 100644 --- a/init.go +++ b/init.go @@ -3,16 +3,21 @@ package ENV // this is an experiment at this point to // see how this turns out -func Init(appname, version, buildtime string, fromargv []string) error { +func Init(appname, version, buildtime string, fromargv []string, goodFunc func(string), badFunc func(string, error)) error { APPNAME = appname VERSION = version BUILDTIME = buildtime argv = fromargv + goodExit = goodFunc + badExit = badFunc err := loadENV() if err == nil { envPB.Init = true } + SetGlobal("lib/ENV", "APPNAME", APPNAME) + SetGlobal("lib/ENV", "VERSION", VERSION) + SetGlobal("lib/ENV", "BUILDTIME", BUILDTIME) return err } diff --git a/structs.go b/structs.go index 89c5177..90a0f14 100644 --- a/structs.go +++ b/structs.go @@ -15,6 +15,9 @@ var APPNAME string var BUILDTIME string var VERSION string +var goodExit func(string) +var badExit func(string, error) + var argv []string var NotInitialized error = errors.New("your application config not initialized") -- cgit v1.2.3