fortify: handle errors via MustParse
All checks were successful
Test / Create distribution (push) Successful in 25s
Test / Run NixOS test (push) Successful in 3m29s

The errSuccess behaviour is kept for beforeExit.

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
Ophestra 2025-02-23 12:57:59 +09:00
parent ba1498cd18
commit 478b27922c
Signed by: cat
SSH Key Fingerprint: SHA256:gQ67O0enBZ7UdZypgtspB2FDM1g3GVw8nX0XSdcFw8Q

32
main.go
View File

@ -53,30 +53,14 @@ func main() {
log.Fatal("this program must not run as root") log.Fatal("this program must not run as root")
} }
err := buildCommand(os.Stderr).Parse(os.Args[1:]) buildCommand(os.Stderr).MustParse(os.Args[1:], func(err error) {
if errors.Is(err, errSuccess) || errors.Is(err, command.ErrHelp) { fmsg.Verbosef("command returned %v", err)
internal.Exit(0) if errors.Is(err, errSuccess) {
panic("unreachable") fmsg.BeforeExit()
} os.Exit(0)
if errors.Is(err, command.ErrNoMatch) || errors.Is(err, command.ErrEmptyTree) { }
internal.Exit(1) })
panic("unreachable") log.Fatal("unreachable")
}
if err == nil {
log.Fatal("unreachable")
}
var flagError command.FlagError
if !errors.As(err, &flagError) {
log.Printf("command: %v", err)
internal.Exit(1)
panic("unreachable")
}
fmsg.Verbose(flagError.Error())
if flagError.Success() {
internal.Exit(0)
}
internal.Exit(1)
} }
func buildCommand(out io.Writer) command.Command { func buildCommand(out io.Writer) command.Command {