Commit f0bb9bdb
f0bb9bdbda4eMerged
Amir Shams <[email protected]> ยท 2 years ago ยท 1 file ยท ~1 modified
Description
refactor: delete unnecessary structs in main.go
Details
- Commit
- f0bb9bdbda4e
- Parent
- 48fd3bfef559
cmd/helmwave/main.gomodifiedView file
@@ -74,10 +74,10 @@
7474
"2. $ helmwave up\n"7575
7676
c.Before = func(ctx *cli.Context) error {77-
if ctx.Bool("cancel-on-kill") {78-
// cancel the ctx if it gets these signals77+
if ctx.Bool("handle-signal") {7978
ctx.Context = cancelCtxOnSignal(ctx.Context, syscall.SIGTERM, syscall.SIGINT)8079
}80+
8181
err := logSetup.Default.Run(ctx)8282
if err != nil {8383
return err@@ -131,48 +131,31 @@
131131
132132
func cancelFlag() *cli.BoolFlag {133133
return &cli.BoolFlag{134-
Name: "cancel-on-kill",134+
Name: "handle-signal",135135
Usage: "cancel helm on SigINT,SigTERM",136136
137137
Value: false,138138
EnvVars: []string{"HELMWAVE_HANDLE_SIGNAL"},139-
Hidden: true, // experimental140139
}141140
}142141
143-
type signalCtx struct {144-
context.Context145-
}146-
147-
func (ctx signalCtx) Err() error {148-
// Unwrap for readablity, if ctx cancelled by reciving a signal149-
if sigErr, ok := context.Cause(ctx.Context).(signalErr); ok {150-
return sigErr151-
}152-
return ctx.Context.Err()153-
}154-
155-
type signalErr struct {156-
sig os.Signal157-
}158-
159-
func (e signalErr) Error() string {160-
return "context canceled: got signal " + e.sig.String()161-
}162-
163142
// cancelCtxOnSignal closes Done channel when one of the listed signals arrives164143
func cancelCtxOnSignal(parent context.Context, signals ...os.Signal) (ctx context.Context) {165144
ctx, cancel := context.WithCancelCause(parent)166-
ctx = signalCtx{ctx} // wrap for err handling167145
146+
context.AfterFunc(ctx, func() {147+
if err := context.Cause(ctx); err != nil {148+
log.Error(err)149+
}150+
})168151
ch := make(chan os.Signal, 1)169152
signal.Notify(ch, signals...)170153
if ctx.Err() == nil {171154
go func() {172155
select {173156
case sig := <-ch:174-
// log.Warn("got signal " + sig.String())175-
cancel(signalErr{sig})157+
cancel(fmt.Errorf("got signal %v", sig))158+
176159
case <-ctx.Done():177160
}178161
}()