refactor: rename format flag to stdout-format
This commit is contained in:
parent
b15161f231
commit
9bd151bea9
4 changed files with 19 additions and 17 deletions
|
|
@ -4,6 +4,7 @@
|
|||
- Connections now support `link` [#1955](https://github.com/terrastruct/d2/pull/1955)
|
||||
- Vars: vars in markdown blocks are substituted [#2218](https://github.com/terrastruct/d2/pull/2218)
|
||||
- Markdown: Github-flavored tables work in `md` blocks [#2221](https://github.com/terrastruct/d2/pull/2221)
|
||||
- CLI: PNG output to stdout is supported using `--stdout-format png -` [#2260](https://github.com/terrastruct/d2/pull/2260)
|
||||
|
||||
#### Improvements 🧹
|
||||
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package d2cli
|
|||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type exportExtension string
|
||||
|
|
@ -15,20 +16,20 @@ const SVG exportExtension = ".svg"
|
|||
|
||||
var SUPPORTED_EXTENSIONS = []exportExtension{SVG, PNG, PDF, PPTX, GIF}
|
||||
|
||||
func getOutputFormat(formatFlag *string, outputPath string) (exportExtension, error) {
|
||||
var formatMap = map[string]exportExtension{
|
||||
"png": PNG,
|
||||
"svg": SVG,
|
||||
"pdf": PDF,
|
||||
"pptx": PPTX,
|
||||
"gif": GIF,
|
||||
}
|
||||
var STDOUT_FORMAT_MAP = map[string]exportExtension{
|
||||
"png": PNG,
|
||||
"svg": SVG,
|
||||
}
|
||||
|
||||
if *formatFlag != "" {
|
||||
if format, ok := formatMap[*formatFlag]; ok {
|
||||
return format, nil
|
||||
var SUPPORTED_STDOUT_FORMATS = []string{"png", "svg"}
|
||||
|
||||
func getOutputFormat(stdoutFormatFlag *string, outputPath string) (exportExtension, error) {
|
||||
if *stdoutFormatFlag != "" {
|
||||
format := strings.ToLower(*stdoutFormatFlag)
|
||||
if ext, ok := STDOUT_FORMAT_MAP[format]; ok {
|
||||
return ext, nil
|
||||
}
|
||||
return "", fmt.Errorf("unsupported format: %s", *formatFlag)
|
||||
return "", fmt.Errorf("%s is not a supported format. Supported formats are: %s", *stdoutFormatFlag, SUPPORTED_STDOUT_FORMATS)
|
||||
}
|
||||
return getExportExtension(outputPath), nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ import (
|
|||
|
||||
func TestOutputFormat(t *testing.T) {
|
||||
type testCase struct {
|
||||
formatFlag string
|
||||
stdoutFormatFlag string
|
||||
outputPath string
|
||||
extension exportExtension
|
||||
supportsDarkTheme bool
|
||||
|
|
@ -43,7 +43,7 @@ func TestOutputFormat(t *testing.T) {
|
|||
requiresPngRender: false,
|
||||
},
|
||||
{
|
||||
formatFlag: "png",
|
||||
stdoutFormatFlag: "png",
|
||||
outputPath: "-",
|
||||
extension: PNG,
|
||||
supportsDarkTheme: false,
|
||||
|
|
@ -88,7 +88,7 @@ func TestOutputFormat(t *testing.T) {
|
|||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
t.Run(tc.outputPath, func(t *testing.T) {
|
||||
extension, err := getOutputFormat(&tc.formatFlag, tc.outputPath)
|
||||
extension, err := getOutputFormat(&tc.stdoutFormatFlag, tc.outputPath)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, tc.extension, extension)
|
||||
assert.Equal(t, tc.supportsAnimation, extension.supportsAnimation())
|
||||
|
|
|
|||
|
|
@ -103,7 +103,7 @@ func Run(ctx context.Context, ms *xmain.State) (err error) {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
formatFlag := ms.Opts.String("", "format", "f", "", "stdout output format (svg, png)")
|
||||
stdoutFormatFlag := ms.Opts.String("", "stdout-format", "", "", "output format when writing to stdout (svg, png). Usage: d2 input.d2 --stdout-format png - > output.png")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -219,7 +219,7 @@ func Run(ctx context.Context, ms *xmain.State) (err error) {
|
|||
return xmain.UsageErrorf("D2 does not support ppt exports, did you mean \"pptx\"?")
|
||||
}
|
||||
|
||||
outputFormat, err := getOutputFormat(formatFlag, outputPath)
|
||||
outputFormat, err := getOutputFormat(stdoutFormatFlag, outputPath)
|
||||
if err != nil {
|
||||
return xmain.UsageErrorf("%v", err)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue