Repository navigation
Expand file tree
/
Copy pathexitcode.go
More file actions
80 lines (72 loc) · 1.67 KB
/
Copy pathexitcode.go
File metadata and controls
80 lines (72 loc) · 1.67 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"errors"
"flag"
"fmt"
)
// Exit codes let callers branch on the failure class without parsing text.
const (
exitOK = 0
exitFailure = 1 // unclassified error
exitUsage = 2 // invalid options or unusable supplied message
exitNoChanges = 3 // nothing to commit
exitConfig = 4 // missing config, API key, or model
exitAPI = 5 // OpenRouter request failed
exitGit = 6 // git operation failed
exitPush = 7 // commit succeeded but push failed
)
// exitError attaches an exit code and a stable machine-readable kind to an error.
type exitError struct {
code int
kind string
err error
}
func (e *exitError) Error() string { return e.err.Error() }
func (e *exitError) Unwrap() error { return e.err }
func withExit(code int, err error) error {
if err == nil {
return nil
}
var existing *exitError
if errors.As(err, &existing) {
return err
}
return &exitError{code: code, kind: exitKind(code), err: err}
}
func usageErrorf(format string, args ...interface{}) error {
return withExit(exitUsage, fmt.Errorf(format, args...))
}
func exitKind(code int) string {
switch code {
case exitOK:
return "ok"
case exitUsage:
return "usage"
case exitNoChanges:
return "no_changes"
case exitConfig:
return "config"
case exitAPI:
return "api"
case exitGit:
return "git"
case exitPush:
return "push"
default:
return "failure"
}
}
// exitCodeFor maps an error returned by run to a process exit code.
func exitCodeFor(err error) int {
if err == nil {
return exitOK
}
var coded *exitError
if errors.As(err, &coded) {
return coded.code
}
if errors.Is(err, flag.ErrHelp) {
return exitUsage
}
return exitFailure
}