1
0
mirror of https://github.com/strongdm/comply synced 2024-11-22 07:34:54 +00:00

more precise cross-platform dependency checking

This commit is contained in:
Justin McCarthy 2018-07-20 17:44:28 -07:00
parent c99d800397
commit e60d7285f4
No known key found for this signature in database
GPG Key ID: 900437410E142A48

View File

@ -133,14 +133,32 @@ func notifyVersion(c *cli.Context) error {
} }
func pandocMustExist(c *cli.Context) error { func pandocMustExist(c *cli.Context) error {
eitherMustExistErr := fmt.Errorf("Please install either Docker or the pandoc package and re-run `%s`", c.Command.Name) eitherMustExistErr := fmt.Errorf("\n\nPlease install either Docker or the pandoc package and re-run `%s`. Find OS-specific pandoc installation instructions at: [TODO]", c.Command.Name)
pandocExistErr := pandocBinaryMustExist(c) pandocExistErr, found, goodVersion, pdfLatex := pandocBinaryMustExist(c)
dockerExistErr := dockerMustExist(c) dockerExistErr, inPath, isRunning := dockerMustExist(c)
config.SetPandoc(pandocExistErr == nil, dockerExistErr == nil) config.SetPandoc(pandocExistErr == nil, dockerExistErr == nil)
check := func(b bool) string {
if b {
return "✔"
} else {
return "✖"
}
}
if pandocExistErr != nil && dockerExistErr != nil { if pandocExistErr != nil && dockerExistErr != nil {
fmt.Printf(`
[%s] pandoc binary installed and in PATH
[%s] pandoc version compatible
[%s] pdflatex binary installed and in PATH
[%s] docker binary installed
[%s] docker running
`, check(found), check(goodVersion), check(pdfLatex), check(inPath), check(isRunning))
return eitherMustExistErr return eitherMustExistErr
} }
@ -152,67 +170,89 @@ func pandocMustExist(c *cli.Context) error {
return nil return nil
} }
func pandocBinaryMustExist(c *cli.Context) error { func pandocBinaryMustExist(c *cli.Context) (e error, found, goodVersion, pdfLatex bool) {
cmd := exec.Command("pandoc", "-v") cmd := exec.Command("pandoc", "-v")
outputRaw, err := cmd.Output() outputRaw, err := cmd.Output()
e = nil
found = false
goodVersion = false
pdfLatex = false
if err != nil { if err != nil {
return errors.Wrap(err, "error calling pandoc") e = errors.Wrap(err, "error calling pandoc")
} } else {
found = true
output := strings.TrimSpace((string(outputRaw))) goodVersion = true
versionErr := errors.New("cannot determine pandoc version") output := strings.TrimSpace((string(outputRaw)))
if !strings.HasPrefix(output, "pandoc") { versionErr := errors.New("cannot determine pandoc version")
return versionErr if !strings.HasPrefix(output, "pandoc") {
} e = versionErr
goodVersion = false
re := regexp.MustCompile(`pandoc (\d+)\.(\d+)`) } else {
result := re.FindStringSubmatch(output) re := regexp.MustCompile(`pandoc (\d+)\.(\d+)`)
if len(result) != 3 { result := re.FindStringSubmatch(output)
return versionErr if len(result) != 3 {
} e = versionErr
goodVersion = false
major, err := strconv.Atoi(result[1]) } else {
if err != nil { major, err := strconv.Atoi(result[1])
return versionErr if err != nil {
} e = versionErr
minor, err := strconv.Atoi(result[2]) goodVersion = false
if err != nil { }
return versionErr minor, err := strconv.Atoi(result[2])
} if err != nil {
e = versionErr
if major < 2 || minor < 1 { goodVersion = false
return errors.New("pandoc 2.1 or greater required") }
if major < 2 || minor < 1 {
e = errors.New("pandoc 2.1 or greater required")
goodVersion = false
}
}
}
} }
// pdflatex must also be present // pdflatex must also be present
cmd = exec.Command("pdflatex", "--version") cmd = exec.Command("pdflatex", "--version")
outputRaw, err = cmd.Output() outputRaw, err = cmd.Output()
if err != nil { if err != nil {
return errors.Wrap(err, "error calling pdflatex") e = errors.Wrap(err, "error calling pdflatex")
} else if !strings.Contains(string(outputRaw), "TeX") {
e = errors.New("pdflatex is required")
} else {
pdfLatex = true
} }
if !strings.Contains(string(outputRaw), "TeX") { return e, found, goodVersion, pdfLatex
return errors.New("pdflatex is required")
}
return nil
} }
func dockerMustExist(c *cli.Context) error { func dockerMustExist(c *cli.Context) (e error, inPath, isRunning bool) {
dockerErr := fmt.Errorf("Docker must be available in order to run `%s`", c.Command.Name) dockerErr := fmt.Errorf("Docker must be available in order to run `%s`", c.Command.Name)
inPath = true
cmd := exec.Command("docker", "--version")
_, err := cmd.Output()
if err != nil {
inPath = false
}
isRunning = true
ctx := context.Background() ctx := context.Background()
cli, err := client.NewEnvClient() cli, err := client.NewEnvClient()
if err != nil { if err != nil {
return dockerErr isRunning = false
return dockerErr, inPath, isRunning
} }
_, err = cli.Ping(ctx) _, err = cli.Ping(ctx)
if err != nil { if err != nil {
return dockerErr isRunning = false
return dockerErr, inPath, isRunning
} }
return nil return nil, inPath, isRunning
} }
func dockerPull(c *cli.Context) error { func dockerPull(c *cli.Context) error {