Skip to content

Commit 2a58d83

Browse files
committed
feat: bind context for signal termination
1 parent a70a529 commit 2a58d83

3 files changed

Lines changed: 13 additions & 2 deletions

File tree

cmd/go-cli-github/main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
package main
33

44
import (
5+
"context"
6+
"os"
7+
"os/signal"
8+
"syscall"
9+
510
"github.com/alecthomas/kong"
611
)
712

@@ -12,10 +17,14 @@ type CLI struct {
1217
}
1318

1419
func main() {
20+
ctx, stop := signal.NotifyContext(
21+
context.Background(), os.Interrupt, syscall.SIGTERM)
22+
defer stop()
1523
// parse CLI config
1624
cli := CLI{}
1725
kctx := kong.Parse(&cli,
1826
kong.UsageOnError(),
27+
kong.BindFor(ctx),
1928
)
2029
// execute CLI
2130
kctx.FatalIfErrorf(kctx.Run())

cmd/go-cli-github/serve.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"context"
45
"fmt"
56
"time"
67

@@ -11,7 +12,7 @@ import (
1112
type ServeCmd struct{}
1213

1314
// Run the serve command.
14-
func (*ServeCmd) Run() error {
15+
func (*ServeCmd) Run(ctx context.Context) error {
1516
fmt.Println(server.New(time.Now).Serve())
1617
return nil
1718
}

cmd/go-cli-github/version.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
67
"runtime"
@@ -18,7 +19,7 @@ var (
1819
type VersionCmd struct{}
1920

2021
// Run the Version command.
21-
func (*VersionCmd) Run() error {
22+
func (*VersionCmd) Run(ctx context.Context) error {
2223
v, err := json.Marshal(
2324
struct {
2425
ProjectName string

0 commit comments

Comments
 (0)