2023-08-12 10:26:23 +01:00
|
|
|
package cmd
|
2023-08-11 18:33:26 +01:00
|
|
|
|
|
|
|
import (
|
2023-08-12 10:26:23 +01:00
|
|
|
"embed"
|
2023-08-11 18:33:26 +01:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2023-08-12 12:02:44 +01:00
|
|
|
"log/slog"
|
2023-08-11 18:33:26 +01:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"text/template"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"codeflow.dananglin.me.uk/apollo/spruce/internal/cv"
|
|
|
|
tf "codeflow.dananglin.me.uk/apollo/spruce/internal/templateFuncs"
|
|
|
|
)
|
|
|
|
|
2023-08-12 10:26:23 +01:00
|
|
|
//go:embed templates/tex/*
|
|
|
|
var templates embed.FS
|
|
|
|
|
2023-08-11 18:33:26 +01:00
|
|
|
type GenerateCommand struct {
|
2023-08-11 19:26:24 +01:00
|
|
|
*flag.FlagSet
|
2023-08-13 17:45:33 +01:00
|
|
|
summary string
|
2023-08-11 18:33:26 +01:00
|
|
|
input string
|
|
|
|
output string
|
|
|
|
employmentHistory int
|
2023-08-12 12:02:44 +01:00
|
|
|
verbose bool
|
2023-08-11 18:33:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewGenerateCommand() *GenerateCommand {
|
|
|
|
gc := GenerateCommand{
|
2023-08-11 19:26:24 +01:00
|
|
|
FlagSet: flag.NewFlagSet("generate", flag.ExitOnError),
|
2023-08-13 17:45:33 +01:00
|
|
|
summary: "Generate a PDF file from an existing CV JSON file.",
|
2023-08-11 18:33:26 +01:00
|
|
|
}
|
|
|
|
|
2023-08-11 19:26:24 +01:00
|
|
|
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.")
|
2023-08-13 17:45:33 +01:00
|
|
|
gc.BoolVar(&gc.verbose, "verbose", false, "set to true to enable verbose logging.")
|
|
|
|
|
|
|
|
gc.Usage = usageFunc(gc.Name(), gc.summary, gc.FlagSet)
|
2023-08-11 18:33:26 +01:00
|
|
|
|
|
|
|
return &gc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GenerateCommand) Run() error {
|
|
|
|
historyLimit := time.Now().AddDate(-1*g.employmentHistory, 0, 0)
|
|
|
|
|
|
|
|
tempDir, err := os.MkdirTemp("/tmp", "cv-builder-")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to create a temporary directory; %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err := os.RemoveAll(tempDir)
|
|
|
|
if err != nil {
|
2023-08-12 12:02:44 +01:00
|
|
|
slog.Warn(fmt.Sprintf("WARN: An error occurred when removing the temporary directory; %v", err))
|
2023-08-11 18:33:26 +01:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
texFile, err := tex(g.input, tempDir, historyLimit)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to create the tex file; %w", err)
|
|
|
|
}
|
|
|
|
|
2023-08-12 12:02:44 +01:00
|
|
|
if err := pdf(tempDir, texFile, g.output, g.verbose); err != nil {
|
2023-08-11 18:33:26 +01:00
|
|
|
return fmt.Errorf("unable to create the PDF file; %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// tex generates the CV document as a Tex file.
|
|
|
|
func tex(input, tempDir string, historyLimit time.Time) (string, error) {
|
2023-08-12 12:02:44 +01:00
|
|
|
slog.Info("Creating the Tex file.")
|
|
|
|
|
2023-08-12 09:43:45 +01:00
|
|
|
c, err := cv.NewCVFromFile(input)
|
2023-08-11 18:33:26 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("unable to create a new CV value from %s; %w", input, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
output := filepath.Join(tempDir, "cv.tex")
|
|
|
|
|
|
|
|
file, err := os.Create(output)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("unable to create output file %s; %w", output, err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
fmap := template.FuncMap{
|
|
|
|
"notLastElement": tf.NotLastElement,
|
|
|
|
"join": tf.JoinSentences,
|
|
|
|
"durationToString": tf.FormatDuration,
|
|
|
|
"withinTimePeriod": tf.WithinTimePeriod(historyLimit),
|
|
|
|
}
|
|
|
|
|
|
|
|
t := template.Must(template.New("cv.tmpl.tex").
|
|
|
|
Funcs(fmap).
|
|
|
|
Delims("<<", ">>").
|
|
|
|
ParseFS(templates, "templates/tex/*.tmpl.tex"),
|
|
|
|
)
|
|
|
|
|
|
|
|
if err = t.Execute(file, c); err != nil {
|
|
|
|
return "", fmt.Errorf("unable to execute the CV template. %w", err)
|
|
|
|
}
|
|
|
|
|
2023-08-12 12:02:44 +01:00
|
|
|
slog.Info("Tex file successfully created.", "filename", output)
|
2023-08-11 18:33:26 +01:00
|
|
|
|
|
|
|
return output, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// pdf generates the CV document as a PDF file from the tex file.
|
2023-08-12 12:02:44 +01:00
|
|
|
func pdf(tempDir, texFile, output string, verbose bool) error {
|
|
|
|
slog.Info("Creating the PDF document.")
|
|
|
|
|
2023-08-11 18:33:26 +01:00
|
|
|
pathArg := "--path=" + tempDir
|
|
|
|
|
|
|
|
command := exec.Command("mtxrun", pathArg, "--script", "context", texFile)
|
|
|
|
|
2023-08-12 12:02:44 +01:00
|
|
|
if verbose {
|
|
|
|
command.Stderr = os.Stderr
|
|
|
|
command.Stdout = os.Stdout
|
|
|
|
}
|
2023-08-11 18:33:26 +01:00
|
|
|
|
|
|
|
if err := command.Run(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if output == "" {
|
|
|
|
output = "./cv.pdf"
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := copyfile(filepath.Join(tempDir, "cv.pdf"), output); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-12 12:02:44 +01:00
|
|
|
slog.Info("PDF document successfully created.", "filename", output)
|
|
|
|
|
2023-08-11 18:33:26 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func copyfile(input, output string) error {
|
|
|
|
inputFile, err := os.Open(input)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to open %s; %w", input, err)
|
|
|
|
}
|
|
|
|
defer inputFile.Close()
|
|
|
|
|
|
|
|
outputFile, err := os.Create(output)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to create %s; %w", output, err)
|
|
|
|
}
|
|
|
|
defer outputFile.Close()
|
|
|
|
|
|
|
|
_, err = io.Copy(outputFile, inputFile)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to copy %s to %s; %w", input, output, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|