diff --git a/cmd/convert.go b/cmd/convert.go index 3095daa..5d10055 100644 --- a/cmd/convert.go +++ b/cmd/convert.go @@ -16,6 +16,50 @@ import ( "github.com/yuzutech/kroki-go" ) +// getImageFormatExtensions returns a map of file extensions (including '.') with their corresponding image format +func getImageFormatExtensions() map[string]kroki.ImageFormat { + imageFormatExtensions := map[string]kroki.ImageFormat{ + ".jpg": kroki.JPEG, + } + supportedImageFormats := kroki.GetSupportedImageFormats() + for _, v := range supportedImageFormats { + imageFormatExtensions["."+string(v)] = v + } + return imageFormatExtensions +} + +// getDiagramTypeNames returns a map of diagram names with their corresponding diagram type +func getDiagramTypeNames() map[string]kroki.DiagramType{ + diagramTypeNames := map[string]kroki.DiagramType{ + "dot": kroki.GraphViz, + } + supportedDiagramTypes := kroki.GetSupportedDiagramTypes() + for _, v := range supportedDiagramTypes { + diagramTypeNames[string(v)] = v + } + return diagramTypeNames +} + +// getDiagramTypeExtensions returns a map of diagram file extensions (including '.') with their corresponding diagram type +func getDiagramTypeExtensions() map[string]kroki.DiagramType { + diagramTypeExtensions := map[string]kroki.DiagramType{ + ".dot": kroki.GraphViz, + ".gv": kroki.GraphViz, + ".puml": kroki.PlantUML, + ".c4puml": kroki.C4PlantUML, + ".c4": kroki.C4PlantUML, + ".er": kroki.Erd, + ".vg": kroki.Vega, + ".vgl": kroki.VegaLite, + ".vl": kroki.VegaLite, + } + supportedDiagramTypes := kroki.GetSupportedDiagramTypes() + for _, v := range supportedDiagramTypes { + diagramTypeExtensions["."+string(v)] = v + } + return diagramTypeExtensions +} + func Convert(cmd *cobra.Command, args []string) { filePath := args[0] graphFormat, err := cmd.Flags().GetString("type") @@ -117,41 +161,23 @@ func ResolveImageFormat(imageFormatRaw string, outFile string) (kroki.ImageForma func ImageFormatFromValue(imageFormatRaw string) (kroki.ImageFormat, error) { value := strings.ToLower(imageFormatRaw) - switch value { - case "svg": - return kroki.SVG, nil - case "png": - return kroki.PNG, nil - case "jpeg": - return kroki.JPEG, nil - case "pdf": - return kroki.PDF, nil - case "base64": - return kroki.Base64, nil - default: - return kroki.ImageFormat(""), errors.Errorf( - "invalid image format %s.", - value) + if f, ok := getImageFormatExtensions()["."+value]; ok { + return f, nil } + return "", errors.Errorf( + "invalid image format %s.", + value) } func ImageFormatFromFile(filePath string) (kroki.ImageFormat, error) { fileExtension := filepath.Ext(filePath) value := strings.ToLower(fileExtension) - switch value { - case ".svg": - return kroki.SVG, nil - case ".png": - return kroki.PNG, nil - case ".jpeg", ".jpg": - return kroki.JPEG, nil - case ".pdf": - return kroki.PDF, nil - default: - return kroki.ImageFormat(""), errors.Errorf( - "invalid image format %s.", - value) + if f, ok := getImageFormatExtensions()[value]; ok { + return f, nil } + return "", errors.Errorf( + "invalid image format %s.", + value) } func ResolveGraphFormat(graphFormatRaw string, filePath string) (kroki.DiagramType, error) { @@ -164,101 +190,23 @@ func ResolveGraphFormat(graphFormatRaw string, filePath string) (kroki.DiagramTy func GraphFormatFromValue(value string) (kroki.DiagramType, error) { value = strings.ToLower(value) - switch value { - case "dot", "graphviz": - return kroki.GraphViz, nil - case "plantuml": - return kroki.PlantUML, nil - case "nomnoml": - return kroki.Nomnoml, nil - case "blockdiag": - return kroki.BlockDiag, nil - case "mermaid": - return kroki.Mermaid, nil - case "svgbob": - return kroki.Svgbob, nil - case "umlet": - return kroki.UMlet, nil - case "c4plantuml": - return kroki.C4PlantUML, nil - case "seqdiag": - return kroki.SeqDiag, nil - case "erd", "er": - return kroki.Erd, nil - case "nwdiag": - return kroki.NwDiag, nil - case "actdiag": - return kroki.ActDiag, nil - case "ditaa": - return kroki.Ditaa, nil - case "rackdiag": - return kroki.RackDiag, nil - case "packetdiag": - return kroki.PacketDiag, nil - case "vega": - return kroki.Vega, nil - case "vegalite": - return kroki.VegaLite, nil - case "wavedrom": - return kroki.WaveDrom, nil - default: - return kroki.DiagramType(""), errors.Errorf( - "invalid graph format %s.", - value) + if d, ok := getDiagramTypeNames()[value]; ok { + return d, nil } + return "", errors.Errorf( + "invalid graph format %s.", + value) } func GraphFormatFromFile(filePath string) (kroki.DiagramType, error) { fileExtension := filepath.Ext(filePath) value := strings.ToLower(fileExtension) - switch value { - case ".actdiag": - return kroki.ActDiag, nil - case ".blockdiag": - return kroki.BlockDiag, nil - case ".bpmn": - return kroki.BPMN, nil - case ".bytefield": - return kroki.Bytefield, nil - case ".c4puml", ".c4", ".c4plantuml": - return kroki.C4PlantUML, nil - case ".ditaa": - return kroki.Ditaa, nil - case ".erd", ".er": - return kroki.Erd, nil - case ".excalidraw": - return kroki.Excalidraw, nil - case ".dot", ".gv", ".graphviz": - return kroki.GraphViz, nil - case ".mermaid": - return kroki.Mermaid, nil - case ".nomnoml": - return kroki.Nomnoml, nil - case ".nwdiag": - return kroki.NwDiag, nil - case ".packetdiag": - return kroki.PacketDiag, nil - case ".puml", ".plantuml": - return kroki.PlantUML, nil - case ".rackdiag": - return kroki.RackDiag, nil - case ".seqdiag": - return kroki.SeqDiag, nil - case ".svgbob": - return kroki.Svgbob, nil - case ".umlet": - return kroki.UMlet, nil - case ".vega", ".vg": - return kroki.Vega, nil - case ".vegalite", ".vgl", ".vl": - return kroki.VegaLite, nil - case ".wavedrom": - return kroki.WaveDrom, nil - default: - return kroki.DiagramType(""), errors.Errorf( - "unable to infer the graph format from the file extension %s, please specify the diagram type using --type flag.", - value) + if d, ok := getDiagramTypeExtensions()[fileExtension]; ok { + return d, nil } + return "", errors.Errorf( + "unable to infer the graph format from the file extension %s, please specify the diagram type using --type flag.", + value) } func GetClient(cmd *cobra.Command) kroki.Client { @@ -280,4 +228,4 @@ func GetClient(cmd *cobra.Command) kroki.Client { URL: viper.GetString("endpoint"), Timeout: viper.GetDuration("timeout"), }) -} +} \ No newline at end of file diff --git a/cmd/root.go b/cmd/root.go index d3f38aa..b6416f0 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -2,6 +2,8 @@ package cmd import ( "fmt" + "github.com/yuzutech/kroki-go" + "sort" "github.com/spf13/cobra" ) @@ -56,9 +58,25 @@ func Execute(version, commit string) { } func init() { + supportedDiagramTypes := kroki.GetSupportedDiagramTypes() + supportedImageFormats := kroki.GetSupportedImageFormats() + diagramTypeNames := make([]string, len(supportedDiagramTypes)) + imageFormatNames := make([]string, len(supportedImageFormats)) + for i, v := range supportedDiagramTypes { + diagramTypeNames[i] = string(v) + } + sort.Strings(diagramTypeNames) + for i, v := range supportedImageFormats { + imageFormatNames[i] = string(v) + } + sort.Strings(imageFormatNames) + + typeHelp := fmt.Sprintf("diagram type %s (default: infer from file extension)", diagramTypeNames) + formatHelp := fmt.Sprintf("output format %s (default: infer from output file extension otherwise svg)", imageFormatNames) + convertCmd.PersistentFlags().StringP("config", "c", "", "alternate config file [env KROKI_CONFIG]") - convertCmd.PersistentFlags().StringP("type", "t", "", "diagram type [actdiag, blockdiag, c4plantuml, ditaa, dot, erd, graphviz, nomnoml, nwdiag, plantuml, seqdiag, svgbob, umlet] (default: infer from file extension)") - convertCmd.PersistentFlags().StringP("format", "f", "", "output format (default: infer from output file extension otherwise svg)") + convertCmd.PersistentFlags().StringP("type", "t", "", typeHelp) + convertCmd.PersistentFlags().StringP("format", "f", "", formatHelp) convertCmd.PersistentFlags().StringP("out-file", "o", "", "output file (default: based on path of input file); use - to output to STDOUT") RootCmd.AddCommand(versionCmd) RootCmd.AddCommand(convertCmd) diff --git a/go.mod b/go.mod index cb5588e..9df6148 100644 --- a/go.mod +++ b/go.mod @@ -8,5 +8,5 @@ require ( github.com/pkg/errors v0.8.1 github.com/spf13/cobra v0.0.3 github.com/spf13/viper v1.3.1 - github.com/yuzutech/kroki-go v0.5.0 + github.com/yuzutech/kroki-go v0.6.2 ) diff --git a/go.sum b/go.sum index 9c0e687..c25c99b 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +1,8 @@ -github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk= github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= @@ -20,7 +18,6 @@ github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181 github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI= github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= @@ -34,20 +31,20 @@ github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/viper v1.3.1 h1:5+8j8FTpnFV4nEImW/ofkzEt8VoOiLXxdYIDsB73T38= github.com/spf13/viper v1.3.1/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s= -github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= -github.com/yuzutech/kroki-go v0.5.0 h1:XWmxmG22/7PXsqt1TihYjYqmAcs3AO3JtvuwGF20Ehs= -github.com/yuzutech/kroki-go v0.5.0/go.mod h1:Cys0gjXdlViePdrZ4AKMUyX4yVIAoIhMBhvpYPJahrk= -github.com/yuzutech/kroki-go v0.5.0 h1:XWmxmG22/7PXsqt1TihYjYqmAcs3AO3JtvuwGF20Ehs= -github.com/yuzutech/kroki-go v0.5.0/go.mod h1:Cys0gjXdlViePdrZ4AKMUyX4yVIAoIhMBhvpYPJahrk= +github.com/yuzutech/kroki-go v0.6.0 h1:0BNsiwS/xFupdXzaz4MTMzyfYuqoK/RXiFff1YMkWM4= +github.com/yuzutech/kroki-go v0.6.0/go.mod h1:Cys0gjXdlViePdrZ4AKMUyX4yVIAoIhMBhvpYPJahrk= +github.com/yuzutech/kroki-go v0.6.1 h1:ruEUFpglD2B901cRPajOm8XfaMR+lAcvgayx2tn3G8w= +github.com/yuzutech/kroki-go v0.6.1/go.mod h1:gtJV1+Z2gBmJlCTjAeIZZY9J4zo1AVBsWQCn1WeCD9c= +github.com/yuzutech/kroki-go v0.6.2 h1:+VHC0PQdtG68A8v/9oK85slzovhHFiLkJ2H/3CRwLBw= +github.com/yuzutech/kroki-go v0.6.2/go.mod h1:gtJV1+Z2gBmJlCTjAeIZZY9J4zo1AVBsWQCn1WeCD9c= golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a h1:1n5lsVfiQW3yfsRGu98756EH1YthsFqr/5mxHduZW2A= golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=