fix: use slog for logging
- Created a new slog logger for logging. - Optionally enable verbose logging when generating the PDF document.
This commit is contained in:
parent
2c5c7332be
commit
2a6cc07624
3 changed files with 35 additions and 13 deletions
|
@ -4,7 +4,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -116,7 +116,7 @@ func (c *CreateCommand) Run() error {
|
||||||
return fmt.Errorf("unable to write the data to %s; %w", c.filename, err)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -24,6 +24,7 @@ type GenerateCommand struct {
|
||||||
input string
|
input string
|
||||||
output string
|
output string
|
||||||
employmentHistory int
|
employmentHistory int
|
||||||
|
verbose bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewGenerateCommand() *GenerateCommand {
|
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.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.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.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
|
return &gc
|
||||||
}
|
}
|
||||||
|
@ -49,7 +51,7 @@ func (g *GenerateCommand) Run() error {
|
||||||
defer func() {
|
defer func() {
|
||||||
err := os.RemoveAll(tempDir)
|
err := os.RemoveAll(tempDir)
|
||||||
if err != nil {
|
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)
|
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)
|
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.
|
// tex generates the CV document as a Tex file.
|
||||||
func tex(input, tempDir string, historyLimit time.Time) (string, error) {
|
func tex(input, tempDir string, historyLimit time.Time) (string, error) {
|
||||||
|
slog.Info("Creating the Tex file.")
|
||||||
|
|
||||||
c, err := cv.NewCVFromFile(input)
|
c, err := cv.NewCVFromFile(input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("unable to create a new CV value from %s; %w", input, err)
|
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)
|
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
|
return output, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// pdf generates the CV document as a PDF file from the tex file.
|
// 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
|
pathArg := "--path=" + tempDir
|
||||||
|
|
||||||
command := exec.Command("mtxrun", pathArg, "--script", "context", texFile)
|
command := exec.Command("mtxrun", pathArg, "--script", "context", texFile)
|
||||||
|
|
||||||
|
if verbose {
|
||||||
command.Stderr = os.Stderr
|
command.Stderr = os.Stderr
|
||||||
command.Stdout = os.Stdout
|
command.Stdout = os.Stdout
|
||||||
|
}
|
||||||
|
|
||||||
if err := command.Run(); err != nil {
|
if err := command.Run(); err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -123,6 +131,8 @@ func pdf(tempDir, texFile, output string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
slog.Info("PDF document successfully created.", "filename", output)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
20
main.go
20
main.go
|
@ -1,7 +1,8 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"codeflow.dananglin.me.uk/apollo/spruce/internal/cmd"
|
"codeflow.dananglin.me.uk/apollo/spruce/internal/cmd"
|
||||||
|
@ -21,6 +22,14 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
logOptions := slog.HandlerOptions{
|
||||||
|
AddSource: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
logger := slog.New(slog.NewTextHandler(os.Stdout, &logOptions))
|
||||||
|
|
||||||
|
slog.SetDefault(logger)
|
||||||
|
|
||||||
commandMap := map[string]Runner{
|
commandMap := map[string]Runner{
|
||||||
"version": cmd.NewVersionCommand(
|
"version": cmd.NewVersionCommand(
|
||||||
binaryVersion,
|
binaryVersion,
|
||||||
|
@ -37,14 +46,17 @@ func main() {
|
||||||
runner, ok := commandMap[subcommand]
|
runner, ok := commandMap[subcommand]
|
||||||
|
|
||||||
if !ok {
|
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 {
|
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 {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue