2022-04-27 04:45:53 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2022-07-12 04:37:48 -05:00
|
|
|
"syscall"
|
2022-04-27 04:45:53 -05:00
|
|
|
|
2023-02-22 10:19:43 -06:00
|
|
|
"codeberg.org/forgejo/runner/cmd"
|
2022-04-27 04:45:53 -05:00
|
|
|
)
|
|
|
|
|
2022-07-13 00:44:14 -05:00
|
|
|
func withContextFunc(ctx context.Context, f func()) context.Context {
|
2022-04-27 04:45:53 -05:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
go func() {
|
2022-07-13 00:44:14 -05:00
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
defer signal.Stop(c)
|
|
|
|
|
2022-04-27 04:45:53 -05:00
|
|
|
select {
|
2022-07-13 00:44:14 -05:00
|
|
|
case <-ctx.Done():
|
2022-04-27 04:45:53 -05:00
|
|
|
case <-c:
|
|
|
|
cancel()
|
2022-07-13 00:44:14 -05:00
|
|
|
f()
|
2022-04-27 04:45:53 -05:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-07-13 00:44:14 -05:00
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
ctx := withContextFunc(context.Background(), func() {})
|
2022-04-27 04:45:53 -05:00
|
|
|
// run the command
|
|
|
|
cmd.Execute(ctx)
|
|
|
|
}
|