InitWithSignals handles the flags defined in SetFlags, calls the Init method for each element of all, and returns a context canceled when any signal in signals is raised.
(all []Initializer, signals ...os.Signal)
| 42 | // for each element of all, and returns a context canceled when any signal in |
| 43 | // signals is raised. |
| 44 | func (f *Flags) InitWithSignals(all []Initializer, signals ...os.Signal) (context.Context, context.CancelFunc, error) { |
| 45 | if f.showVersion { |
| 46 | fmt.Printf("Version: %s\n", Version()) |
| 47 | os.Exit(0) |
| 48 | } |
| 49 | var err error |
| 50 | for _, flags := range all { |
| 51 | if initErr := flags.Init(); err == nil { |
| 52 | err = initErr |
| 53 | } |
| 54 | } |
| 55 | if err != nil { |
| 56 | return nil, nil, err |
| 57 | } |
| 58 | if f.cpuprofile != "" { |
| 59 | f.runCPUProfile(f.cpuprofile) |
| 60 | } |
| 61 | if f.trace != "" { |
| 62 | f.traceFile, err = os.Create(f.trace) |
| 63 | if err != nil { |
| 64 | log.Fatal(err) |
| 65 | } |
| 66 | trace.Start(f.traceFile) |
| 67 | } |
| 68 | ctx, cancel := signalContext(context.Background(), signals...) |
| 69 | cleanup := func() { |
| 70 | cancel() |
| 71 | f.cleanup() |
| 72 | } |
| 73 | return ctx, cleanup, nil |
| 74 | } |
| 75 | |
| 76 | func signalContext(ctx context.Context, signals ...os.Signal) (context.Context, context.CancelFunc) { |
| 77 | ctx, cancel := context.WithCancelCause(ctx) |
no test coverage detected