CV/magefiles/helpers.go
Dan Anglin 95cf8dbcd6
feat: update CV and project
Changes:

- Add Adarga employment section
- Refined Experian employment section
- Removed GitLab CI files
- Updated Dockerfile
- Updated Magefiles
2023-02-14 07:30:27 +00:00

145 lines
3.6 KiB
Go

//go:build mage
package main
import (
"encoding/json"
"fmt"
"io"
"os"
"os/exec"
"strings"
"text/template"
)
const (
aspellLang string = "en_GB"
defaultImageName string = "cv-builder"
defaultImageTag string = "latest"
)
// imageName generates the docker image name from the environment
// variables IMAGE_NAME and IMAGE_TAG. Default vaules will be used
// if these variables are not set or empty.
func imageName() string {
name := os.Getenv("IMAGE_NAME")
if len(name) == 0 {
name = defaultImageName
}
tag := os.Getenv("IMAGE_TAG")
if len(tag) == 0 {
tag = defaultImageTag
}
return name + ":" + tag
}
// createCVTex generates the CV docuemnt as a Tex file.
func createCVTex(cvJsonDataFile, cvTemplateDir, cvOutputDir, cvOutputFileName string) error {
fmt.Printf("INFO: Attempting to read data from %s...\n", cvJsonDataFile)
data, err := os.ReadFile(cvJsonDataFile)
if err != nil {
return fmt.Errorf("unable to read data from file. %w", err)
}
fmt.Println("INFO: Successfully read data.")
fmt.Println("INFO: Attempting to unmarshal JSON data...")
var cv Cv
if err = json.Unmarshal(data, &cv); err != nil {
return fmt.Errorf("unable to unmarshal JSON data; %w", err)
}
fmt.Println("INFO: JSON unmarshalling was successful.")
// if CV_CONTACT_PHONE is set then add it to the CV
phone := os.Getenv("CV_CONTACT_PHONE")
if len(phone) > 0 {
cv.Contact.Phone = phone
}
cvOutputPath := cvOutputDir + "/" + cvOutputFileName
fmt.Printf("INFO: Attempting to create output file %s...\n", cvOutputPath)
if err = os.MkdirAll(cvOutputDir, 0750); err != nil {
return fmt.Errorf("unable to create output directory %s; %w", cvOutputDir, err)
}
output, err := os.Create(cvOutputPath)
if err != nil {
return fmt.Errorf("unable to create output file %s; %w", cvOutputPath, err)
}
fmt.Printf("INFO: Successfully created output file %s.\n", cvOutputPath)
defer output.Close()
fmt.Println("INFO: Attempting template execution...")
fmap := template.FuncMap{
"notLastElement": notLastElement,
"join": join,
"durationToString": durationToString,
}
t := template.Must(template.New("cv.tmpl.tex").Funcs(fmap).Delims("<<", ">>").ParseGlob(cvTemplateDir + "*.tmpl.tex"))
if err = t.Execute(output, cv); err != nil {
return fmt.Errorf("unable to execute the CV template. %w", err)
}
fmt.Println("INFO: Template execution successful.")
return nil
}
func spellCheck(dataFile, aspellPersonalWordlist string) error {
fmt.Printf("INFO: Reading data from %s...\n", dataFile)
data, err := os.ReadFile(dataFile)
if err != nil {
return fmt.Errorf("unable to read data from %s, %s", dataFile, err.Error())
}
// declare the aspell command and its standard input pipe.
cmd := exec.Command("aspell", "-d", aspellLang, "-p", aspellPersonalWordlist, "list")
stdin, err := cmd.StdinPipe()
if err != nil {
return err
}
// write the CV data to standard input for piping.
go func() {
defer stdin.Close()
io.WriteString(stdin, string(data))
}()
// run aspell and get the list of mispelt words (if any).
// (the output is a string.)
fmt.Println("Running aspell...")
out, err := cmd.CombinedOutput()
if err != nil {
return err
}
list := strings.Split(string(out), "\n")
if list[len(list)-1] == "" {
list = list[:len(list)-1]
}
if len(list) > 0 {
var b strings.Builder
errMsg := fmt.Sprintf("the following spelling errors were found in %s:", dataFile)
b.WriteString(errMsg)
for _, v := range list {
s := "\n- " + v
b.WriteString(s)
}
return fmt.Errorf(b.String())
} else {
fmt.Println("No spelling errors were found.")
}
return nil
}