support single input file

This commit is contained in:
melsonic 2025-03-10 22:52:51 +05:30 committed by melsonic
parent 3727173f31
commit 20ecd634cc
No known key found for this signature in database
GPG key ID: DFA426742F621CD7
3 changed files with 17 additions and 18 deletions

View file

@ -18,7 +18,7 @@
.Nm d2
.Ar play Ar file.d2
.Nm d2
.Ar validate Ar file.d2 ...
.Ar validate Ar file.d2
.Sh DESCRIPTION
.Nm
compiles and renders
@ -164,8 +164,8 @@ Lists available themes
Format all passed files
.It Ar play Ar file.d2
Opens the file in playground, an online web viewer (https://play.d2lang.com)
.It Ar validate Ar file.d2 ...
Validate all input files
.It Ar validate Ar file.d2
Validate all input file
.El
.Sh ENVIRONMENT VARIABLES
Many flags can also be set with environment variables.

View file

@ -23,7 +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 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.
@ -41,7 +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
%[1]s validate file.d2 - Validate input file
See more docs and the source code at https://oss.terrastruct.com/d2.
Hosted icons at https://icons.terrastruct.com.

View file

@ -13,23 +13,22 @@ func validateCmd(ctx context.Context, ms *xmain.State) (err error) {
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")
return xmain.UsageErrorf("validate must be passed an input file to be validated")
}
for _, inputPath := range ms.Opts.Args {
if inputPath != "-" {
inputPath = ms.AbsPath(inputPath)
}
inputPath := ms.Opts.Args[0]
if inputPath != "-" {
inputPath = ms.AbsPath(inputPath)
}
input, err := ms.ReadPath(inputPath)
if err != nil {
return err
}
input, err := ms.ReadPath(inputPath)
if err != nil {
return err
}
_, err = d2lib.Parse(ctx, string(input), nil)
if err != nil {
return err
}
_, err = d2lib.Parse(ctx, string(input), nil)
if err != nil {
return err
}
return nil
}