Skip to content

Commit 38851ef

Browse files
authored
Merge pull request #2415 from melsonic/issue-2410
cli: add validate command
2 parents 32c14d5 + 6253d59 commit 38851ef

File tree

6 files changed

+68
-0
lines changed

6 files changed

+68
-0
lines changed

ci/release/changelogs/next.md

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
- Support relative imports. Improve elk error handling: [#2382](https://github.com/terrastruct/d2/pull/2382)
2525
- Support fonts (`fontRegular`, `fontItalic`, `fontBold`, `fontSemiBold`): [#2384](https://github.com/terrastruct/d2/pull/2384)
2626

27+
- d2cli:
28+
- Support `validate` command. [#2415](https://github.com/terrastruct/d2/pull/2415)
29+
2730
#### Bugfixes ⛑️
2831

2932
- Compiler:

ci/release/template/man/d2.1

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
.Ar fmt Ar file.d2 ...
1818
.Nm d2
1919
.Ar play Ar file.d2
20+
.Nm d2
21+
.Ar validate Ar file.d2
2022
.Sh DESCRIPTION
2123
.Nm
2224
compiles and renders
@@ -162,6 +164,8 @@ Lists available themes
162164
Format all passed files
163165
.It Ar play Ar file.d2
164166
Opens the file in playground, an online web viewer (https://play.d2lang.com)
167+
.It Ar validate Ar file.d2
168+
Validates file.d2
165169
.El
166170
.Sh ENVIRONMENT VARIABLES
167171
Many flags can also be set with environment variables.

d2cli/help.go

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Usage:
2323
%[1]s layout [name]
2424
%[1]s fmt file.d2 ...
2525
%[1]s play [--theme=0] [--sketch] file.d2
26+
%[1]s validate file.d2
2627
2728
%[1]s compiles and renders file.d2 to file.svg | file.png
2829
It defaults to file.svg if an output path is not provided.
@@ -40,6 +41,7 @@ Subcommands:
4041
%[1]s themes - Lists available themes
4142
%[1]s fmt file.d2 ... - Format passed files
4243
%[1]s play file.d2 - Opens the file in playground, an online web viewer (https://play.d2lang.com)
44+
%[1]s validate file.d2 - Validates file.d2
4345
4446
See more docs and the source code at https://oss.terrastruct.com/d2.
4547
Hosted icons at https://icons.terrastruct.com.

d2cli/main.go

+2
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ func Run(ctx context.Context, ms *xmain.State) (err error) {
173173
return fmtCmd(ctx, ms, *checkFlag)
174174
case "play":
175175
return playCmd(ctx, ms)
176+
case "validate":
177+
return validateCmd(ctx, ms)
176178
case "version":
177179
if len(ms.Opts.Flags.Args()) > 1 {
178180
return xmain.UsageErrorf("version subcommand accepts no arguments")

d2cli/validate.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package d2cli
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"oss.terrastruct.com/d2/d2lib"
8+
"oss.terrastruct.com/util-go/xdefer"
9+
"oss.terrastruct.com/util-go/xmain"
10+
)
11+
12+
func validateCmd(ctx context.Context, ms *xmain.State) (err error) {
13+
defer xdefer.Errorf(&err, "")
14+
15+
ms.Opts = xmain.NewOpts(ms.Env, ms.Opts.Flags.Args()[1:])
16+
if len(ms.Opts.Args) == 0 {
17+
return xmain.UsageErrorf("input argument required")
18+
}
19+
20+
inputPath := ms.Opts.Args[0]
21+
if inputPath != "-" {
22+
inputPath = ms.AbsPath(inputPath)
23+
}
24+
25+
input, err := ms.ReadPath(inputPath)
26+
if err != nil {
27+
return err
28+
}
29+
30+
_, err = d2lib.Parse(ctx, string(input), nil)
31+
if err != nil {
32+
return err
33+
}
34+
35+
if inputPath == "-" {
36+
inputPath = "Input"
37+
}
38+
39+
fmt.Printf("Success! [%s] is valid D2.\n", inputPath)
40+
return nil
41+
}

e2etests-cli/main_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,22 @@ c
13461346
assert.Success(t, err)
13471347
},
13481348
},
1349+
{
1350+
name: "validate-against-correct-d2",
1351+
run: func(t *testing.T, ctx context.Context, dir string, env *xos.Env) {
1352+
writeFile(t, dir, "correct.d2", `x -> y`)
1353+
err := runTestMainPersist(t, ctx, dir, env, "validate", "correct.d2")
1354+
assert.Success(t, err)
1355+
},
1356+
},
1357+
{
1358+
name: "validate-against-incorrect-d2",
1359+
run: func(t *testing.T, ctx context.Context, dir string, env *xos.Env) {
1360+
writeFile(t, dir, "incorrect.d2", `x > y`)
1361+
err := runTestMainPersist(t, ctx, dir, env, "validate", "incorrect.d2")
1362+
assert.Error(t, err)
1363+
},
1364+
},
13491365
}
13501366

13511367
ctx := context.Background()

0 commit comments

Comments
 (0)