diff --git a/internal/cli/app.go b/internal/cli/app.go index 10f0e08..a4f3fd3 100644 --- a/internal/cli/app.go +++ b/internal/cli/app.go @@ -7,7 +7,10 @@ import ( "io/ioutil" "log" "os" + "os/exec" "path/filepath" + "regexp" + "strconv" "strings" "time" @@ -96,6 +99,54 @@ func ticketingMustBeConfigured(c *cli.Context) error { return nil } +func pandocMustExist(c *cli.Context) error { + pandocErr := fmt.Errorf("Please install either Docker or the pandoc package and re-run `%s`", c.Command.Name) + + err := pandocBinaryMustExist(c) + fmt.Println(err) + if err != nil { + err = dockerMustExist(c) + if err != nil { + return pandocErr + } + } + return nil +} + +func pandocBinaryMustExist(c *cli.Context) error { + cmd := exec.Command("pandoc", "-v") + outputRaw, err := cmd.Output() + if err != nil { + return errors.Wrap(err, "error calling pandoc") + } + + output := strings.TrimSpace((string(outputRaw))) + versionErr := errors.New("cannot determine pandoc version") + if !strings.HasPrefix(output, "pandoc") { + return versionErr + } + + re := regexp.MustCompile(`pandoc (\d+)\.(\d+)`) + result := re.FindStringSubmatch(output) + if len(result) != 3 { + return versionErr + } + + major, err := strconv.Atoi(result[1]) + if err != nil { + return versionErr + } + minor, err := strconv.Atoi(result[2]) + if err != nil { + return versionErr + } + + if major < 2 || minor < 1 { + return errors.New("pandoc 2.1 or greater required") + } + return nil +} + func dockerMustExist(c *cli.Context) error { dockerErr := fmt.Errorf("Docker must be available in order to run `%s`", c.Command.Name) diff --git a/internal/cli/build.go b/internal/cli/build.go index ad4ac2b..205ff07 100644 --- a/internal/cli/build.go +++ b/internal/cli/build.go @@ -11,7 +11,7 @@ var buildCommand = cli.Command{ ShortName: "b", Usage: "generate a static website summarizing the compliance program", Action: buildAction, - Before: beforeAll(dockerMustExist, cleanContainers), + Before: beforeAll(pandocMustExist, cleanContainers), } func buildAction(c *cli.Context) error { diff --git a/internal/cli/serve.go b/internal/cli/serve.go index ee4d9e9..19184c6 100644 --- a/internal/cli/serve.go +++ b/internal/cli/serve.go @@ -10,7 +10,7 @@ var serveCommand = cli.Command{ Name: "serve", Usage: "live updating version of the build command", Action: serveAction, - Before: beforeAll(dockerMustExist, cleanContainers), + Before: beforeAll(pandocMustExist, cleanContainers), } func serveAction(c *cli.Context) error {