ci: add validate command

This commit is contained in:
melsonic 2025-03-09 22:05:16 +05:30 committed by melsonic
parent 32c14d586c
commit 0955ef608d
No known key found for this signature in database
GPG key ID: DFA426742F621CD7
3 changed files with 39 additions and 0 deletions

View file

@ -23,6 +23,7 @@ Usage:
%[1]s layout [name]
%[1]s fmt file.d2 ...
%[1]s play [--theme=0] [--sketch] file.d2
%[1]s validate file.d2 ...
%[1]s compiles and renders file.d2 to file.svg | file.png
It defaults to file.svg if an output path is not provided.
@ -40,6 +41,7 @@ Subcommands:
%[1]s themes - Lists available themes
%[1]s fmt file.d2 ... - Format passed files
%[1]s play file.d2 - Opens the file in playground, an online web viewer (https://play.d2lang.com)
%[1]s validate file.d2 ... - Validate input files
See more docs and the source code at https://oss.terrastruct.com/d2.
Hosted icons at https://icons.terrastruct.com.

View file

@ -173,6 +173,8 @@ func Run(ctx context.Context, ms *xmain.State) (err error) {
return fmtCmd(ctx, ms, *checkFlag)
case "play":
return playCmd(ctx, ms)
case "validate":
return validateCmd(ctx, ms)
case "version":
if len(ms.Opts.Flags.Args()) > 1 {
return xmain.UsageErrorf("version subcommand accepts no arguments")

35
d2cli/validate.go Normal file
View file

@ -0,0 +1,35 @@
package d2cli
import (
"context"
"oss.terrastruct.com/d2/d2lib"
"oss.terrastruct.com/util-go/xdefer"
"oss.terrastruct.com/util-go/xmain"
)
func validateCmd(ctx context.Context, ms *xmain.State) (err error) {
defer xdefer.Errorf(&err, "failed to validate")
ms.Opts = xmain.NewOpts(ms.Env, ms.Opts.Flags.Args()[1:])
if len(ms.Opts.Args) == 0 {
return xmain.UsageErrorf("validate must be passed at least one file to be validated")
}
for _, inputPath := range ms.Opts.Args {
if inputPath != "-" {
inputPath = ms.AbsPath(inputPath)
}
input, err := ms.ReadPath(inputPath)
if err != nil {
return err
}
_, err = d2lib.Parse(ctx, string(input), nil)
if err != nil {
return err
}
}
return nil
}