Skip to content

Commit

Permalink
Replace vars by functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ggrossetie committed Jan 6, 2021
1 parent 905f734 commit 1338204
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 61 deletions.
115 changes: 58 additions & 57 deletions cmd/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,64 +16,72 @@ import (
"github.com/yuzutech/kroki-go"
)

var (

// SupportedImageFormats maps string names back to their
// kroki.ImageFormat constant value.
SupportedImageFormats = []kroki.ImageFormat{
// getSupportedImageFormats returns the list of all the supported image formats
// Please note that not all image formats are available on all diagram types
func getSupportedImageFormats() []kroki.ImageFormat {
return []kroki.ImageFormat{
kroki.SVG,
kroki.PNG,
kroki.JPEG,
kroki.PDF,
kroki.Base64,
}
}

// SupportedDiagramTypes maps string names back to the
// corresponding kroki.DiagramType constant.
SupportedDiagramTypes = []kroki.DiagramType{
kroki.GraphViz,
kroki.PlantUML,
kroki.Nomnoml,
// getSupportedDiagramTypes returns the list of all supported diagram types
func getSupportedDiagramTypes() []kroki.DiagramType {
return []kroki.DiagramType{
kroki.ActDiag,
kroki.BlockDiag,
kroki.Mermaid,
kroki.Svgbob,
kroki.UMlet,
kroki.BPMN,
kroki.Bytefield,
kroki.C4PlantUML,
kroki.SeqDiag,
kroki.Ditaa,
kroki.Erd,
kroki.Excalidraw,
kroki.GraphViz,
kroki.Mermaid,
kroki.Nomnoml,
kroki.NwDiag,
kroki.ActDiag,
kroki.Ditaa,
kroki.PacketDiag,
kroki.PlantUML,
kroki.RackDiag,
kroki.SeqDiag,
kroki.Svgbob,
kroki.UMlet,
kroki.Vega,
kroki.VegaLite,
kroki.WaveDrom,
}
}

// The mappings are pre-populated with some Additional
// forms that are matched/accepted for certain types.
// The init() function will flesh them out with a set of
// standard names based on the two Supported* slices.

// Valid --format arguments for output file type
ImageFormatNames = map[string]kroki.ImageFormat{
"jpg": kroki.JPEG,
}

// File extensions matched to derive output file type
ImageFormatExtensions = map[string]kroki.ImageFormat{
// 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 := getSupportedImageFormats()
for _, v := range supportedImageFormats {
imageFormatExtensions["."+string(v)] = v
}
return imageFormatExtensions
}

// Valid --type arguments for input file type
DiagramTypeNames = map[string]kroki.DiagramType{
// 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,
"er": kroki.Erd,
"nwdiag": kroki.NwDiag,
}
supportedDiagramTypes := getSupportedDiagramTypes()
for _, v := range supportedDiagramTypes {
diagramTypeNames[string(v)] = v
}
return diagramTypeNames
}

// Filename matching for input file type
DiagramTypeExtensions = map[string]kroki.DiagramType{
// 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,
Expand All @@ -84,7 +92,12 @@ var (
".vgl": kroki.VegaLite,
".vl": kroki.VegaLite,
}
)
supportedDiagramTypes := getSupportedDiagramTypes()
for _, v := range supportedDiagramTypes {
diagramTypeExtensions["."+string(v)] = v
}
return diagramTypeExtensions
}

func Convert(cmd *cobra.Command, args []string) {
filePath := args[0]
Expand Down Expand Up @@ -187,21 +200,21 @@ func ResolveImageFormat(imageFormatRaw string, outFile string) (kroki.ImageForma

func ImageFormatFromValue(imageFormatRaw string) (kroki.ImageFormat, error) {
value := strings.ToLower(imageFormatRaw)
if f, ok := ImageFormatNames[value]; ok {
if f, ok := getImageFormatExtensions()["."+value]; ok {
return f, nil
}
return kroki.ImageFormat(""), errors.Errorf(
return "", errors.Errorf(
"invalid image format %s.",
value)
}

func ImageFormatFromFile(filePath string) (kroki.ImageFormat, error) {
fileExtension := filepath.Ext(filePath)
value := strings.ToLower(fileExtension)
if f, ok := ImageFormatExtensions[value]; ok {
if f, ok := getImageFormatExtensions()[value]; ok {
return f, nil
}
return kroki.ImageFormat(""), errors.Errorf(
return "", errors.Errorf(
"invalid image format %s.",
value)
}
Expand All @@ -216,21 +229,21 @@ func ResolveGraphFormat(graphFormatRaw string, filePath string) (kroki.DiagramTy

func GraphFormatFromValue(value string) (kroki.DiagramType, error) {
value = strings.ToLower(value)
if d, ok := DiagramTypeNames[value]; ok {
if d, ok := getDiagramTypeNames()[value]; ok {
return d, nil
}
return kroki.DiagramType(""), errors.Errorf(
return "", errors.Errorf(
"invalid graph format %s.",
value)
}

func GraphFormatFromFile(filePath string) (kroki.DiagramType, error) {
fileExtension := filepath.Ext(filePath)
value := strings.ToLower(fileExtension)
if d, ok := DiagramTypeExtensions[fileExtension]; ok {
if d, ok := getDiagramTypeExtensions()[fileExtension]; ok {
return d, nil
}
return kroki.DiagramType(""), errors.Errorf(
return "", errors.Errorf(
"unable to infer the graph format from the file extension %s, please specify the diagram type using --type flag.",
value)
}
Expand All @@ -254,16 +267,4 @@ func GetClient(cmd *cobra.Command) kroki.Client {
URL: viper.GetString("endpoint"),
Timeout: viper.GetDuration("timeout"),
})
}

// Set up mappings
func init() {
for _, v := range SupportedDiagramTypes {
DiagramTypeNames[string(v)] = v
DiagramTypeExtensions["."+string(v)] = v
}
for _, v := range SupportedImageFormats {
ImageFormatNames[string(v)] = v
ImageFormatExtensions["."+string(v)] = v
}
}
}
10 changes: 6 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@ func Execute(version, commit string) {
}

func init() {
diagramTypeNames := make([]string, len(SupportedDiagramTypes))
imageFormatNames := make([]string, len(SupportedImageFormats))
for i, v := range SupportedDiagramTypes {
supportedDiagramTypes := getSupportedDiagramTypes()
supportedImageFormats := 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 {
for i, v := range supportedImageFormats {
imageFormatNames[i] = string(v)
}
sort.Strings(imageFormatNames)
Expand Down

0 comments on commit 1338204

Please sign in to comment.