diff --git a/internal/cmd/create.go b/internal/cmd/create.go index 6f75c32..f3c5aa5 100644 --- a/internal/cmd/create.go +++ b/internal/cmd/create.go @@ -4,7 +4,7 @@ import ( "encoding/json" "flag" "fmt" - "log" + "log/slog" "os" "time" @@ -116,7 +116,7 @@ func (c *CreateCommand) Run() error { return fmt.Errorf("unable to write the data to %s; %w", c.filename, err) } - log.Printf("INFO: Successfully written the new CV to %s\n", file.Name()) + slog.Info("CV successfully created", "filename", file.Name()) return nil } diff --git a/internal/cmd/generate.go b/internal/cmd/generate.go index 2fb2b3f..95cb6d7 100644 --- a/internal/cmd/generate.go +++ b/internal/cmd/generate.go @@ -5,7 +5,7 @@ import ( "flag" "fmt" "io" - "log" + "log/slog" "os" "os/exec" "path/filepath" @@ -24,6 +24,7 @@ type GenerateCommand struct { input string output string employmentHistory int + verbose bool } func NewGenerateCommand() *GenerateCommand { @@ -34,6 +35,7 @@ func NewGenerateCommand() *GenerateCommand { gc.StringVar(&gc.input, "input", "", "specify the CV JSON file that you want to input to the builder.") gc.StringVar(&gc.output, "output", "", "specify the name of the output CV file.") gc.IntVar(&gc.employmentHistory, "employment-history", 10, "show employment history within these number of years.") + gc.BoolVar(&gc.verbose, "verbose", false, "set to true to enable verbose logging") return &gc } @@ -49,7 +51,7 @@ func (g *GenerateCommand) Run() error { defer func() { err := os.RemoveAll(tempDir) if err != nil { - log.Printf("WARN: An error occurred when removing the temporary directory; %v", err) + slog.Warn(fmt.Sprintf("WARN: An error occurred when removing the temporary directory; %v", err)) } }() @@ -58,7 +60,7 @@ func (g *GenerateCommand) Run() error { return fmt.Errorf("unable to create the tex file; %w", err) } - if err := pdf(tempDir, texFile, g.output); err != nil { + if err := pdf(tempDir, texFile, g.output, g.verbose); err != nil { return fmt.Errorf("unable to create the PDF file; %w", err) } @@ -67,6 +69,8 @@ func (g *GenerateCommand) Run() error { // tex generates the CV document as a Tex file. func tex(input, tempDir string, historyLimit time.Time) (string, error) { + slog.Info("Creating the Tex file.") + c, err := cv.NewCVFromFile(input) if err != nil { return "", fmt.Errorf("unable to create a new CV value from %s; %w", input, err) @@ -97,19 +101,23 @@ func tex(input, tempDir string, historyLimit time.Time) (string, error) { return "", fmt.Errorf("unable to execute the CV template. %w", err) } - log.Printf("INFO: Tex file %s was successfully created.", output) + slog.Info("Tex file successfully created.", "filename", output) return output, nil } // pdf generates the CV document as a PDF file from the tex file. -func pdf(tempDir, texFile, output string) error { +func pdf(tempDir, texFile, output string, verbose bool) error { + slog.Info("Creating the PDF document.") + pathArg := "--path=" + tempDir command := exec.Command("mtxrun", pathArg, "--script", "context", texFile) - command.Stderr = os.Stderr - command.Stdout = os.Stdout + if verbose { + command.Stderr = os.Stderr + command.Stdout = os.Stdout + } if err := command.Run(); err != nil { return err @@ -123,6 +131,8 @@ func pdf(tempDir, texFile, output string) error { return err } + slog.Info("PDF document successfully created.", "filename", output) + return nil } diff --git a/main.go b/main.go index d0ac159..29cf4bc 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,8 @@ package main import ( - "log" + "fmt" + "log/slog" "os" "codeflow.dananglin.me.uk/apollo/spruce/internal/cmd" @@ -21,6 +22,14 @@ var ( ) func main() { + logOptions := slog.HandlerOptions{ + AddSource: false, + } + + logger := slog.New(slog.NewTextHandler(os.Stdout, &logOptions)) + + slog.SetDefault(logger) + commandMap := map[string]Runner{ "version": cmd.NewVersionCommand( binaryVersion, @@ -37,14 +46,17 @@ func main() { runner, ok := commandMap[subcommand] if !ok { - log.Fatalf("ERROR: unknown subcommand '%s'.", subcommand) + slog.Error("unknown subcommand", "subcommand", subcommand) + os.Exit(1) } if err := runner.Parse(os.Args[2:]); err != nil { - log.Fatalf("ERROR: unable to parse the command line flags; %v.", err) + slog.Error(fmt.Sprintf("unable to parse the command line flags; %v.", err)) + os.Exit(1) } if err := runner.Run(); err != nil { - log.Fatalf("ERROR: unable to run '%s'; %v.", runner.Name(), err) + slog.Error(fmt.Sprintf("unable to run '%s'; %v.", runner.Name(), err)) + os.Exit(1) } }