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)
|
- 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)
|
- 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)
|
- 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 🧹
|
#### Improvements 🧹
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package d2cli
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type exportExtension string
|
type exportExtension string
|
||||||
|
|
@ -15,20 +16,20 @@ const SVG exportExtension = ".svg"
|
||||||
|
|
||||||
var SUPPORTED_EXTENSIONS = []exportExtension{SVG, PNG, PDF, PPTX, GIF}
|
var SUPPORTED_EXTENSIONS = []exportExtension{SVG, PNG, PDF, PPTX, GIF}
|
||||||
|
|
||||||
func getOutputFormat(formatFlag *string, outputPath string) (exportExtension, error) {
|
var STDOUT_FORMAT_MAP = map[string]exportExtension{
|
||||||
var formatMap = map[string]exportExtension{
|
"png": PNG,
|
||||||
"png": PNG,
|
"svg": SVG,
|
||||||
"svg": SVG,
|
}
|
||||||
"pdf": PDF,
|
|
||||||
"pptx": PPTX,
|
|
||||||
"gif": GIF,
|
|
||||||
}
|
|
||||||
|
|
||||||
if *formatFlag != "" {
|
var SUPPORTED_STDOUT_FORMATS = []string{"png", "svg"}
|
||||||
if format, ok := formatMap[*formatFlag]; ok {
|
|
||||||
return format, nil
|
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
|
return getExportExtension(outputPath), nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ import (
|
||||||
|
|
||||||
func TestOutputFormat(t *testing.T) {
|
func TestOutputFormat(t *testing.T) {
|
||||||
type testCase struct {
|
type testCase struct {
|
||||||
formatFlag string
|
stdoutFormatFlag string
|
||||||
outputPath string
|
outputPath string
|
||||||
extension exportExtension
|
extension exportExtension
|
||||||
supportsDarkTheme bool
|
supportsDarkTheme bool
|
||||||
|
|
@ -43,7 +43,7 @@ func TestOutputFormat(t *testing.T) {
|
||||||
requiresPngRender: false,
|
requiresPngRender: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
formatFlag: "png",
|
stdoutFormatFlag: "png",
|
||||||
outputPath: "-",
|
outputPath: "-",
|
||||||
extension: PNG,
|
extension: PNG,
|
||||||
supportsDarkTheme: false,
|
supportsDarkTheme: false,
|
||||||
|
|
@ -88,7 +88,7 @@ func TestOutputFormat(t *testing.T) {
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
tc := tc
|
tc := tc
|
||||||
t.Run(tc.outputPath, func(t *testing.T) {
|
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.NoError(t, err)
|
||||||
assert.Equal(t, tc.extension, extension)
|
assert.Equal(t, tc.extension, extension)
|
||||||
assert.Equal(t, tc.supportsAnimation, extension.supportsAnimation())
|
assert.Equal(t, tc.supportsAnimation, extension.supportsAnimation())
|
||||||
|
|
|
||||||
|
|
@ -103,7 +103,7 @@ func Run(ctx context.Context, ms *xmain.State) (err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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 {
|
if err != nil {
|
||||||
return err
|
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\"?")
|
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 {
|
if err != nil {
|
||||||
return xmain.UsageErrorf("%v", err)
|
return xmain.UsageErrorf("%v", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue