Skip to content

Commit

Permalink
feat(generate): Improve generator error logging
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Apr 28, 2024
1 parent cd930bf commit b9c29ef
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 33 deletions.
23 changes: 9 additions & 14 deletions internal/generate/completions/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", "")
Expand All @@ -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")
}
}
}
3 changes: 3 additions & 0 deletions internal/generate/docs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 14 additions & 6 deletions internal/generate/gzip/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
}
}
31 changes: 18 additions & 13 deletions internal/generate/manpages/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"compress/gzip"
"fmt"
"io"
"io/fs"
"os"
Expand All @@ -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
Expand All @@ -24,23 +29,23 @@ 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", "")
rootName := rootCmd.Name()

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{
Expand All @@ -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 {
Expand All @@ -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")
}
}

0 comments on commit b9c29ef

Please sign in to comment.