-
Notifications
You must be signed in to change notification settings - Fork 1
/
mapper.go
65 lines (52 loc) · 1.39 KB
/
mapper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main // import "github.com/drgrib/maps/cmd/mapper"
import (
"flag"
"fmt"
"os"
"strings"
"github.com/drgrib/maps/maptemplate"
)
func main() {
types := flag.String("types", "", "key:value type pair (required)")
suffix := flag.String("suffix", "*", "custom function suffix (optional)")
output := flag.String("output", "", "target file name (optional)")
packageName := flag.String("package", "", "target package name (optional)")
flag.Parse()
if *types == "" {
exitWithError("Error: At least one key:value -types pair required")
}
mapFile, err := parseToDefaultMapFile(*types)
if err != nil {
exitWithError(err.Error())
}
if *suffix != "*" {
mapFile.MapTemplate.KeySuffix = *suffix
mapFile.MapTemplate.ValueSuffix = ""
}
if *output != "" {
mapFile.FileName = *output
}
if *packageName != "" {
mapFile.MapTemplate.Package = *packageName
}
err = mapFile.Write()
if err != nil {
exitWithError(err.Error())
}
}
func parseToDefaultMapFile(types string) (*maptemplate.MapFile, error) {
pairSplit := strings.Split(types, ":")
if len(pairSplit) != 2 {
return nil, fmt.Errorf("Error: Bad types string %q", types)
}
keytype, valuetype := pairSplit[0], pairSplit[1]
mapFile, err := maptemplate.NewMapFile(keytype, valuetype)
if err != nil {
return nil, err
}
return mapFile, nil
}
func exitWithError(message string) {
fmt.Fprintf(os.Stderr, message+"\n")
os.Exit(2)
}