github.com/puellanivis/breton@v0.2.16/lib/os/process/init.go (about)

     1  package process
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	flag "github.com/puellanivis/breton/lib/gnuflag"
     8  )
     9  
    10  // Go libraries should not set flags themselves, these are an exception.
    11  var (
    12  	profile = flag.String("profile", "write cpu profile to `filename`.prof and heap profile to filename.mprof")
    13  	_       = flag.BoolFunc("version", "display version information", func() {
    14  		fmt.Println(Version())
    15  		Exit(0)
    16  	})
    17  )
    18  
    19  // Init is initialization code that provides basic functionality for processes.
    20  //
    21  // Init takes as parameters version information, identifying the Command Name, Semver, and a Buildstamp.
    22  // The Buildstamp could be just a timestamp, or could include a commit hash or other reference.
    23  //
    24  // Init parses flags, sets up AtExit, and will start profiling if the appropriate flag is set.
    25  //
    26  // It returns the `context.Context` from `process.Context()`,
    27  // and a function that `main` should `defer`,
    28  // which will take care of executing the queued AtExit functions.
    29  func Init(cmdname, semver, buildstamp string) (context.Context, func()) {
    30  	buildVersion(cmdname, semver, buildstamp)
    31  
    32  	flag.Parse()
    33  
    34  	if *profile != "" {
    35  		setupProfiling()
    36  	}
    37  
    38  	return Context(), func() {
    39  		runExitFuncs()
    40  
    41  		if r := recover(); r != nil {
    42  			panic(r)
    43  		}
    44  	}
    45  }