refactor: move spellcheck to helpers package
Move the spellcheck functionality to the helpers package to clean up the magefile.
This commit is contained in:
parent
feb5f35e18
commit
964fea7938
2 changed files with 57 additions and 54 deletions
|
@ -3,12 +3,16 @@ package helpers
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
aspellLang string = "en_GB"
|
||||||
defaultImageName string = "cv-builder"
|
defaultImageName string = "cv-builder"
|
||||||
defaultImageTag string = "latest"
|
defaultImageTag string = "latest"
|
||||||
)
|
)
|
||||||
|
@ -84,3 +88,55 @@ func CreateCVTex(cvJsonDataFile, cvTemplateDir, cvOutputDir, cvOutputFileName st
|
||||||
|
|
||||||
return nil
|
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
|
||||||
|
}
|
||||||
|
|
55
magefile.go
55
magefile.go
|
@ -4,10 +4,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
|
||||||
"os/exec"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/magefile/mage/mg"
|
"github.com/magefile/mage/mg"
|
||||||
"github.com/magefile/mage/sh"
|
"github.com/magefile/mage/sh"
|
||||||
|
@ -15,7 +11,6 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
aspellLang string = "en_GB"
|
|
||||||
aspellPersonalWordlist string = "./.aspell/.aspell.en.pws"
|
aspellPersonalWordlist string = "./.aspell/.aspell.en.pws"
|
||||||
cvJsonDataFile string = "./data/cv.json"
|
cvJsonDataFile string = "./data/cv.json"
|
||||||
cvOutputDir string = "./__output"
|
cvOutputDir string = "./__output"
|
||||||
|
@ -32,55 +27,7 @@ var Default = Pdf
|
||||||
// Any error found while gathering the list is returned.
|
// Any error found while gathering the list is returned.
|
||||||
// This function depends on the aspell program.
|
// This function depends on the aspell program.
|
||||||
func SpellCheck() error {
|
func SpellCheck() error {
|
||||||
fmt.Printf("Reading data from %s...\n", cvJsonDataFile)
|
return helpers.SpellCheck(cvJsonDataFile, aspellPersonalWordlist)
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tex takes the CV data file and generates the output tex file from
|
// Tex takes the CV data file and generates the output tex file from
|
||||||
|
|
Loading…
Reference in a new issue