rcc: use simpler error handling with a wrapper main

This commit is contained in:
mappu 2025-02-06 19:17:44 +13:00
parent b1c757cd3c
commit 0da95c13f8

View File

@ -12,7 +12,7 @@ import (
"strings" "strings"
) )
func main() { func RccExec() error {
// Parse arguments // Parse arguments
@ -28,8 +28,7 @@ func main() {
// Check if input file exists // Check if input file exists
if _, err := os.Stat(*input); os.IsNotExist(err) { if _, err := os.Stat(*input); os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "Input file '%s' not found\n", *input) return fmt.Errorf("Input file '%s' not found\n", *input)
os.Exit(1)
} }
// Fill in default output names, if not specified // Fill in default output names, if not specified
@ -49,8 +48,7 @@ func main() {
rccCmd.Stdout = os.Stdout rccCmd.Stdout = os.Stdout
err := rccCmd.Run() err := rccCmd.Run()
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "rcc: %s\n", err.Error()) return err
os.Exit(1)
} }
// Create Go file that loads the resource // Create Go file that loads the resource
@ -83,12 +81,21 @@ func init() {
outputData, err := format.Source([]byte(goSrcData)) outputData, err := format.Source([]byte(goSrcData))
if err != nil { if err != nil {
panic(err) return err
} }
err = ioutil.WriteFile(*outputGo, outputData, 0644) err = ioutil.WriteFile(*outputGo, outputData, 0644)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Writing to '%s': %s\n", *outputGo, err.Error()) return fmt.Errorf("Writing to '%s': %w", *outputGo, err)
}
return nil
}
func main() {
err := RccExec()
if err != nil {
fmt.Fprintf(os.Stderr, "rcc: %s\n", err.Error())
os.Exit(1) os.Exit(1)
} }