github.com/puellanivis/breton@v0.2.16/lib/util/atexit.go (about)

     1  package util
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/puellanivis/breton/lib/os/process"
     7  )
     8  
     9  // AtExitFunc is a function type that can be deferred for execution at the time of the program's exit. (Will NOT work if you call os.Exit(), you must call util.Exit())
    10  type AtExitFunc func()
    11  
    12  // AtExit queues the given AtExitFuncs to be executed at program exit time.
    13  func AtExit(f AtExitFunc) {
    14  	process.AtExit(f)
    15  }
    16  
    17  // Exit runs the queued AtExitFuncs and then calls os.Exit with the given status.
    18  func Exit(status int) {
    19  	process.Exit(status)
    20  }
    21  
    22  // Init is initialization code that provides basic functionality for command-line programs.
    23  // It parses flags, sets up AtExit, and will start profiling if the appropriate flag is set.
    24  // It takes as parameters version information,
    25  // the first argument being the command's identifying string,
    26  // and then a series of numbers which indicate the various version points.
    27  // It returns the context.Context from util.Context(),
    28  // and a function that should be defer'ed in your main() function,
    29  // which will take care of executing the queued AtExitFuncs even in a panic() situation.
    30  func Init(cmdname string, versions ...uint) (context.Context, func()) {
    31  	return process.Init(cmdname, buildSemver(versions), BUILD)
    32  }