d2/d2cli/validate.go

42 lines
764 B
Go
Raw Normal View History

2025-03-09 16:35:16 +00:00
package d2cli
import (
"context"
2025-03-10 19:18:43 +00:00
"fmt"
2025-03-09 16:35:16 +00:00
"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) {
2025-03-10 19:18:43 +00:00
defer xdefer.Errorf(&err, "")
2025-03-09 16:35:16 +00:00
ms.Opts = xmain.NewOpts(ms.Env, ms.Opts.Flags.Args()[1:])
if len(ms.Opts.Args) == 0 {
2025-03-11 18:26:27 +00:00
return xmain.UsageErrorf("input argument required")
2025-03-09 16:35:16 +00:00
}
2025-03-10 17:22:51 +00:00
inputPath := ms.Opts.Args[0]
if inputPath != "-" {
inputPath = ms.AbsPath(inputPath)
}
2025-03-09 16:35:16 +00:00
2025-03-10 17:22:51 +00:00
input, err := ms.ReadPath(inputPath)
if err != nil {
return err
}
2025-03-09 16:35:16 +00:00
2025-03-10 17:22:51 +00:00
_, err = d2lib.Parse(ctx, string(input), nil)
if err != nil {
return err
2025-03-09 16:35:16 +00:00
}
2025-03-10 19:18:43 +00:00
if inputPath == "-" {
inputPath = "Input"
}
fmt.Printf("Success! [%s] is valid D2.\n", inputPath)
2025-03-09 16:35:16 +00:00
return nil
}