From b9c29ef071b31d776116c00ae7d084ce38ecf7f6 Mon Sep 17 00:00:00 2001 From: Gabe Cook Date: Sun, 28 Apr 2024 15:16:34 -0500 Subject: [PATCH] feat(generate): Improve generator error logging --- internal/generate/completions/main.go | 23 ++++++++------------ internal/generate/docs/main.go | 3 +++ internal/generate/gzip/main.go | 20 +++++++++++------ internal/generate/manpages/main.go | 31 ++++++++++++++++----------- 4 files changed, 44 insertions(+), 33 deletions(-) diff --git a/internal/generate/completions/main.go b/internal/generate/completions/main.go index 3fa5b9b3..8dcc34bb 100644 --- a/internal/generate/completions/main.go +++ b/internal/generate/completions/main.go @@ -2,20 +2,23 @@ package main import ( "bytes" - "io" "os" "path/filepath" "github.com/gabe565/ascii-movie/cmd" + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" ) func main() { + log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) + if err := os.RemoveAll("completions"); err != nil { - panic(err) + log.Fatal().Err(err).Msg("failed to remove completions dir") } if err := os.MkdirAll("completions", 0o777); err != nil { - panic(err) + log.Fatal().Err(err).Msg("failed to create completions dir") } rootCmd := cmd.NewCommand("latest", "") @@ -26,20 +29,12 @@ func main() { for _, shell := range []string{"bash", "zsh", "fish"} { rootCmd.SetArgs([]string{"completion", shell}) if err := rootCmd.Execute(); err != nil { - panic(err) + log.Fatal().Err(err).Msg("failed to generate completion") } - f, err := os.Create(filepath.Join("completions", name+"."+shell)) + err := os.WriteFile(filepath.Join("completions", name+"."+shell), buf.Bytes(), 0o644) if err != nil { - panic(err) - } - - if _, err := io.Copy(f, &buf); err != nil { - panic(err) - } - - if err := f.Close(); err != nil { - panic(err) + log.Fatal().Err(err).Msg("failed to write completion") } } } diff --git a/internal/generate/docs/main.go b/internal/generate/docs/main.go index 9a1df569..8d928917 100644 --- a/internal/generate/docs/main.go +++ b/internal/generate/docs/main.go @@ -4,11 +4,14 @@ import ( "os" "github.com/gabe565/ascii-movie/cmd" + "github.com/rs/zerolog" "github.com/rs/zerolog/log" "github.com/spf13/cobra/doc" ) func main() { + log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) + output := "./docs" if err := os.RemoveAll(output); err != nil { diff --git a/internal/generate/gzip/main.go b/internal/generate/gzip/main.go index 3341fc19..184c34ae 100644 --- a/internal/generate/gzip/main.go +++ b/internal/generate/gzip/main.go @@ -4,16 +4,20 @@ package main import ( "compress/gzip" + "fmt" "io" "io/fs" "os" "path/filepath" "github.com/gabe565/ascii-movie/movies" + "github.com/rs/zerolog" "github.com/rs/zerolog/log" ) func main() { + log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) + if err := fs.WalkDir(movies.Movies, ".", func(path string, d fs.DirEntry, err error) error { if err != nil { return err @@ -27,28 +31,32 @@ func main() { log.Debug().Str("path", outPath).Msg("Create output") out, err := os.Create(outPath) if err != nil { - return err + return fmt.Errorf("create output: %w", err) } log.Debug().Str("path", path).Msg("Open input") in, err := movies.Movies.Open(path) if err != nil { - return err + return fmt.Errorf("open input: %w", err) } log.Debug().Msg("Copy input to gzip writer") gz := gzip.NewWriter(out) if _, err := io.Copy(gz, in); err != nil { - return err + return fmt.Errorf("copy input to gzip writer: %w", err) } if err := gz.Close(); err != nil { - return err + return fmt.Errorf("close gzip writer: %w", err) } log.Debug().Msg("Close output") - return out.Close() + if err := out.Close(); err != nil { + return fmt.Errorf("close output: %w", err) + } + + return nil }); err != nil { - log.Fatal().Err(err).Msg("Failed to walk movies") + log.Fatal().Err(err).Msg("failed to gzip movies") } } diff --git a/internal/generate/manpages/main.go b/internal/generate/manpages/main.go index 2f6c659b..2831d65a 100644 --- a/internal/generate/manpages/main.go +++ b/internal/generate/manpages/main.go @@ -2,6 +2,7 @@ package main import ( "compress/gzip" + "fmt" "io" "io/fs" "os" @@ -10,11 +11,15 @@ import ( "time" "github.com/gabe565/ascii-movie/cmd" + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" "github.com/spf13/cobra/doc" flag "github.com/spf13/pflag" ) func main() { + log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) + flags := flag.NewFlagSet("", flag.ContinueOnError) var version string @@ -24,15 +29,15 @@ func main() { flags.StringVar(&dateParam, "date", time.Now().Format(time.RFC3339), "Build date") if err := flags.Parse(os.Args); err != nil { - panic(err) + log.Fatal().Err(err).Msg("failed to parse arguments") } if err := os.RemoveAll("manpages"); err != nil { - panic(err) + log.Fatal().Err(err).Msg("failed to remove manpages dir") } if err := os.MkdirAll("manpages", 0o755); err != nil { - panic(err) + log.Fatal().Err(err).Msg("failed to create manpages dir") } rootCmd := cmd.NewCommand("beta", "") @@ -40,7 +45,7 @@ func main() { date, err := time.Parse(time.RFC3339, dateParam) if err != nil { - panic(err) + log.Fatal().Err(err).Str("raw", dateParam).Msg("failed to parse date") } header := doc.GenManHeader{ @@ -52,7 +57,7 @@ func main() { } if err := doc.GenManTree(rootCmd, &header, "manpages"); err != nil { - panic(err) + log.Fatal().Err(err).Msg("failed to generate manpages") } if err := filepath.Walk("manpages", func(path string, info fs.FileInfo, err error) error { @@ -62,35 +67,35 @@ func main() { in, err := os.Open(path) if err != nil { - return err + return fmt.Errorf("open input: %w", err) } out, err := os.Create(path + ".gz") if err != nil { - return err + return fmt.Errorf("create output: %w", err) } gz := gzip.NewWriter(out) if _, err := io.Copy(gz, in); err != nil { - return err + return fmt.Errorf("copy input to gzip writer: %w", err) } if err := in.Close(); err != nil { - return err + return fmt.Errorf("close input: %w", err) } if err := os.Remove(path); err != nil { - return err + return fmt.Errorf("remove input: %w", err) } if err := gz.Close(); err != nil { - return err + return fmt.Errorf("close gzip: %w", err) } if err := out.Close(); err != nil { - return err + return fmt.Errorf("close output: %w", err) } return nil }); err != nil { - panic(err) + log.Fatal().Err(err).Msg("failed to gzip manpages") } }