refactor: move spellcheck to helpers package

Move the spellcheck functionality to the helpers package to clean up the
magefile.
This commit is contained in:
Dan Anglin 2020-09-13 14:24:29 +01:00
parent feb5f35e18
commit 964fea7938
No known key found for this signature in database
GPG key ID: 7AC2B18EC1D09F27
2 changed files with 57 additions and 54 deletions

View file

@ -3,12 +3,16 @@ package helpers
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"strings"
"text/template"
)
const (
aspellLang string = "en_GB"
defaultImageName string = "cv-builder"
defaultImageTag string = "latest"
)
@ -84,3 +88,55 @@ func CreateCVTex(cvJsonDataFile, cvTemplateDir, cvOutputDir, cvOutputFileName st
return nil
}
func SpellCheck(dataFile, aspellPersonalWordlist string) error {
fmt.Printf("INFO: Reading data from %s...\n", dataFile)
data, err := ioutil.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
}

View file

@ -4,10 +4,6 @@ package main
import (
"fmt"
"io"
"io/ioutil"
"os/exec"
"strings"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
@ -15,7 +11,6 @@ import (
)
const (
aspellLang string = "en_GB"
aspellPersonalWordlist string = "./.aspell/.aspell.en.pws"
cvJsonDataFile string = "./data/cv.json"
cvOutputDir string = "./__output"
@ -32,55 +27,7 @@ var Default = Pdf
// Any error found while gathering the list is returned.
// This function depends on the aspell program.
func SpellCheck() error {
fmt.Printf("Reading data from %s...\n", cvJsonDataFile)
data, err := ioutil.ReadFile(cvJsonDataFile)
if err != nil {
return fmt.Errorf("unable to read data from %s, %s", cvJsonDataFile, 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:", cvJsonDataFile)
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
return helpers.SpellCheck(cvJsonDataFile, aspellPersonalWordlist)
}
// Tex takes the CV data file and generates the output tex file from