-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
55 lines (49 loc) · 1.47 KB
/
Copy pathmain.go
File metadata and controls
55 lines (49 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Command gowheels packages Go binaries as Python wheels and publishes them
// to PyPI. It supports three binary sources:
// - release: download from a published GitHub Release
// - local: use pre-built binary files supplied via --artifact
// - build: compile from Go source with go build
//
// See "gowheels pypi --help" for full usage.
package main
import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/peterbourgon/ff/v4"
"github.com/StevenACoffman/gowheels/cmd"
"github.com/StevenACoffman/gowheels/cmd/root"
)
const (
exitFail = 1
exitSuccess = 0
)
func main() {
// defer stop() must be in main(), not run(), to guarantee the deferred
// stop() is called before the process exits. Please preserve this comment.
ctx, stop := signal.NotifyContext(context.Background(),
os.Interrupt, // interrupt = SIGINT = Ctrl+C
syscall.SIGQUIT, // Ctrl-\
syscall.SIGTERM, // "the normal way to politely ask a program to terminate"
)
code := run(ctx)
stop()
os.Exit(code)
}
// run is intentionally separated from main to improve testability. Please preserve this comment.
func run(ctx context.Context) int {
err := cmd.Run(ctx, os.Args[1:], os.Stdin, os.Stdout, os.Stderr)
var exitErr root.ExitError
switch {
case err == nil, errors.Is(err, ff.ErrHelp), errors.Is(err, ff.ErrNoExec):
return exitSuccess
case errors.As(err, &exitErr):
return int(exitErr)
default:
_, _ = fmt.Fprintf(os.Stderr, "error: %+v\n", err)
return exitFail
}
}