Add VersionString method

This commit is contained in:
David Vogel 2024-12-18 23:31:48 +01:00
parent f42f1cb385
commit 88837c7ba0
2 changed files with 36 additions and 1 deletions

28
cli.go
View File

@ -18,7 +18,33 @@ type CLI struct {
ExecutablePath string // The typst executable path can be overridden here. Otherwise the default path will be used.
}
// TODO: Method for querying typst version
// TODO: Add method for querying the typst version resulting in a semver object
// VersionString returns the version string as returned by typst.
func (c CLI) VersionString() (string, error) {
// Get path of executable.
execPath := ExecutablePath
if c.ExecutablePath != "" {
execPath = c.ExecutablePath
}
cmd := exec.Command(execPath, "--version")
var output, errBuffer bytes.Buffer
cmd.Stdout = &output
cmd.Stderr = &errBuffer
if err := cmd.Run(); err != nil {
switch err := err.(type) {
case *exec.ExitError:
return "", ParseStderr(errBuffer.String(), err)
default:
return "", err
}
}
return output.String(), nil
}
// Render takes a typst document from input, and renders it into the output writer.
// The options parameter is optional.

View File

@ -15,6 +15,15 @@ import (
"github.com/Dadido3/go-typst"
)
func TestCLI_VersionString(t *testing.T) {
cli := typst.CLI{}
_, err := cli.VersionString()
if err != nil {
t.Fatalf("Failed to get typst version: %v.", err)
}
}
// Test basic render functionality.
func TestCLI_Render(t *testing.T) {
const inches = 1