Skip to content

Commit e4f23be

Browse files
Add an example for context cancellation
1 parent 4c54757 commit e4f23be

6 files changed

Lines changed: 107 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ go get go.followtheprocess.codes/cli@latest
5050
package main
5151

5252
import (
53-
"context"
53+
"context"
5454
"fmt"
5555
"os"
5656

docs/img/cancel.gif

68.4 KB
Loading

docs/src/cancel.tape

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
Output ./docs/img/cancel.gif
2+
3+
Set FontSize 18
4+
Set FontFamily "Geist Mono"
5+
Set Theme "Catppuccin Macchiato"
6+
Set WindowBar Colorful
7+
Set Padding 5
8+
Set Height 800
9+
Set Width 1200
10+
Set Margin 40
11+
Set MarginFill "#7983FF"
12+
Set BorderRadius 10
13+
Set TypingSpeed 100ms
14+
15+
# Setup
16+
Hide
17+
Type "go build -o ./cancel ./examples/cancel && clear" Sleep 2s
18+
Enter
19+
Sleep 2s
20+
Show
21+
Sleep 1s
22+
23+
Type "./cancel --help" Sleep 500ms Enter
24+
25+
Sleep 6s
26+
27+
Type "clear" Sleep 500ms Enter
28+
29+
Sleep 1s
30+
31+
Type "./cancel" Sleep 500ms Enter
32+
33+
Sleep 8s
34+
35+
Ctrl+c
36+
37+
Sleep 1s
38+
39+
# Cleanup
40+
Hide
41+
Type "rm -f ./cancel"
42+
Enter

examples/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This directory contains a bunch of example programs built with `cli` to show you
77
- [`./quickstart`](#quickstart)
88
- [`./subcommands`](#subcommands)
99
- [`./namedargs`](#namedargs)
10+
- [`./cancel`](#cancel)
1011
- [TODO](#todo)
1112

1213
## `./cover`
@@ -31,6 +32,12 @@ A CLI with named positional arguments that may or may not have default values. S
3132

3233
![namedargs](../docs/img/namedargs.gif)
3334

35+
## `./cancel`
36+
37+
This examples shows how `cli` requiring you to pass a `context.Context` to your run functions leads to elegant and resilient cancellation and `CTRL+C` handling.
38+
39+
![cancel](../docs/img/cancel.gif)
40+
3441
### TODO
3542

3643
- Replicate one or two well known CLI tools as an example

examples/cancel/cli.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"time"
7+
8+
"go.followtheprocess.codes/cli"
9+
)
10+
11+
func BuildCLI() (*cli.Command, error) {
12+
return cli.New(
13+
"cancel",
14+
cli.Short("Cancel me!"),
15+
cli.Run(func(ctx context.Context, cmd *cli.Command) error {
16+
for {
17+
select {
18+
case <-ctx.Done():
19+
return ctx.Err()
20+
case <-time.After(1 * time.Second):
21+
fmt.Fprintln(cmd.Stdout(), "working...")
22+
}
23+
}
24+
}),
25+
)
26+
}

examples/cancel/main.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Package cancel demonstrates how to handle CTRL+C and cancellation/timeouts
2+
// easily with cli.
3+
package main
4+
5+
import (
6+
"context"
7+
"fmt"
8+
"os"
9+
"os/signal"
10+
)
11+
12+
func main() {
13+
if err := run(); err != nil {
14+
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
15+
os.Exit(1)
16+
}
17+
}
18+
19+
func run() error {
20+
ctx := context.Background()
21+
22+
ctx, cancel := signal.NotifyContext(ctx, os.Interrupt)
23+
defer cancel()
24+
25+
cli, err := BuildCLI()
26+
if err != nil {
27+
return err
28+
}
29+
30+
return cli.Execute(ctx)
31+
}

0 commit comments

Comments
 (0)