summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-10-12 00:56:11 -0500
committerJeff Carr <[email protected]>2025-10-12 00:56:11 -0500
commit07eccc7f591fca0914a94782f4b5c8ba973d14d1 (patch)
treeb7d8e1c14cd4f44e26d0a12b698852796b78e93b
parent9ad9c25bd36643e954fe0a3a1e5a2ba3ec92151b (diff)
Exit() callback
-rw-r--r--debugger.go51
-rw-r--r--exit.go8
-rw-r--r--interface.go66
-rw-r--r--structs.go1
4 files changed, 74 insertions, 52 deletions
diff --git a/debugger.go b/debugger.go
index 7f20819..5d1298a 100644
--- a/debugger.go
+++ b/debugger.go
@@ -40,54 +40,3 @@ func Debugger() {
CHAN = log.NewFlag("CHAN", true, full, short, "chan() test code output")
WARN = log.NewFlag("WARN", true, full, short, "should warn the user")
}
-
-// Versioned is the interface that the destination struct should implement to
-// make a version string appear at the top of the help message.
-type Appnamed interface {
- // Version returns the version string that will be printed on a line by itself
- // at the top of the help message.
- Appname() string
-}
-
-type AutoFuncd interface {
- // Version returns the version string that will be printed on a line by itself
- // at the top of the help message.
- 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.
- Examples() string
-}
-
-// Described is the interface that the destination struct should implement to
-func findAppInfo(tmp interface{}) {
- if tmp, ok := tmp.(Appnamed); ok {
- myAuto.appName = tmp.Appname()
- } else {
- 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 {
- // panic("you need to make the function argv.Appname()")
- }
-
- if tmp, ok := tmp.(AutoFuncd); ok {
- myAuto.autoFunc = tmp.DoAutoComplete
- } else {
- // panic("you need to make the function argv.DoAutoComplete()")
- }
-}
diff --git a/exit.go b/exit.go
index 5a6c5a1..6643b64 100644
--- a/exit.go
+++ b/exit.go
@@ -17,6 +17,9 @@ import (
func (pb *Auto) GoodExit(msg string) {
go ExitWatchdog()
+ if myAuto.appExit != nil {
+ myAuto.appExit()
+ }
dur := time.Since(pb.Ctime.AsTime())
log.Infof("%s: %s (%s)\n", pb.Argname, msg, config.FormatDuration(dur))
os.Exit(0)
@@ -24,6 +27,9 @@ func (pb *Auto) GoodExit(msg string) {
func (pb *Auto) BadExit(msg string, err error) {
go ExitWatchdog()
+ if myAuto.appExit != nil {
+ myAuto.appExit()
+ }
if err != nil {
log.Info(err)
}
@@ -34,7 +40,7 @@ func (pb *Auto) BadExit(msg string, err error) {
// this code doesn't need to be this complicated. I put it here as reference code for myself so I could remember where it is.
func ExitWatchdog() {
- dog := time.NewTicker(time.Second)
+ dog := time.NewTicker(5 * time.Second)
defer dog.Stop()
dogchan := make(chan bool)
/*
diff --git a/interface.go b/interface.go
new file mode 100644
index 0000000..d6d4a63
--- /dev/null
+++ b/interface.go
@@ -0,0 +1,66 @@
+package prep
+
+// this is a work in progress
+
+// Versioned is the interface that the destination struct should implement to
+// make a version string appear at the top of the help message.
+type Appnamed interface {
+ // Version returns the version string that will be printed on a line by itself
+ // at the top of the help message.
+ Appname() string
+}
+
+type AutoFuncd interface {
+ // Version returns the version string that will be printed on a line by itself
+ // at the top of the help message.
+ 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.
+ Examples() string
+}
+
+type ExitI interface {
+ // Version returns the version string that will be printed on a line by itself
+ // at the top of the help message.
+ Exit()
+}
+
+// Described is the interface that the destination struct should implement to
+func findAppInfo(tmp interface{}) {
+ if tmp, ok := tmp.(Appnamed); ok {
+ myAuto.appName = tmp.Appname()
+ } else {
+ 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 {
+ // panic("you need to make the function argv.Appname()")
+ }
+
+ if tmp, ok := tmp.(AutoFuncd); ok {
+ myAuto.autoFunc = tmp.DoAutoComplete
+ } else {
+ // panic("you need to make the function argv.DoAutoComplete()")
+ }
+
+ if tmp, ok := tmp.(ExitI); ok {
+ myAuto.appExit = tmp.Exit
+ } else {
+ // panic("you need to make the function argv.Exit()")
+ }
+}
diff --git a/structs.go b/structs.go
index 1f274f1..5b28289 100644
--- a/structs.go
+++ b/structs.go
@@ -31,6 +31,7 @@ type AutoArgs struct {
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
+ appExit func() // app Exit()
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