-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexit.go
More file actions
42 lines (35 loc) · 873 Bytes
/
exit.go
File metadata and controls
42 lines (35 loc) · 873 Bytes
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
// SPDX-FileCopyrightText: 2025 The Cipher Host Team <[email protected]>
//
// SPDX-License-Identifier: MIT
package cmdkit
import (
"errors"
"flag"
"os"
)
// Exit is a convenience function that calls os.Exit with the error code
// associated with the error.
// Exit provides a consistent way to terminate a command-line application
// while respecting Unix exit code conventions.
//
// It handles the following cases:
//
// - nil error -> exit 0 (success)
// - flag.ErrHelp -> exit 0 (help display is not an error)
// - ExitError -> use provided exit code
// - other errors -> exit 1 (general failure)
//
// This function should typically be called at the top level of main().
func Exit(err error) {
if err == nil {
os.Exit(0)
}
if errors.Is(err, flag.ErrHelp) {
os.Exit(0)
}
var e *ExitError
if errors.As(err, &e) {
os.Exit(e.Code())
}
os.Exit(1)
}