-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
123 lines (103 loc) · 2.4 KB
/
main.go
File metadata and controls
123 lines (103 loc) · 2.4 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
_ "embed"
"flag"
"fmt"
"log"
"os"
"strings"
"text/template"
"github.com/dogenzaka/tsv"
"github.com/iancoleman/strcase"
)
type ExitCode struct {
Code int
GoStyleName string
PyStyleName string
Description string
Group string
}
type ExitCodeTSVRow struct {
Code int
Name string
PSOverride string
GSOverride string
Description string
Group string
}
//go:embed exitcodes.tsv
var ecSrc string
//go:embed golang.tmpl
var goTmpl string
//go:embed python.tmpl
var pyTmpl string
func main() {
goOutfile := flag.String("go", "", "Golang output file")
pyOutfile := flag.String("py", "", "Python output file")
flag.Parse()
if *goOutfile == "" && *pyOutfile == "" {
log.Fatalln("At least one of -go or -py must be given.")
return
}
var ecs []ExitCode
var ecRow ExitCodeTSVRow
rowN := 0
parser := tsv.NewParserWithoutHeader(strings.NewReader(ecSrc), &ecRow)
for {
eof, err := parser.Next()
rowN++
if eof {
break
}
if err != nil {
log.Fatalf("row %d: failed to parse: %s", rowN, err)
}
ec := ExitCode{
Code: ecRow.Code,
Description: ecRow.Description,
Group: ecRow.Group,
}
name := ecRow.Name
if strings.HasPrefix(name, "EX_") {
name = strings.TrimPrefix(name, "EX_")
} else if strings.HasPrefix(name, "EXIT_") {
name = strings.TrimPrefix(name, "EXIT_")
}
if ecRow.PSOverride != "" {
ec.PyStyleName = ecRow.PSOverride
} else {
ec.PyStyleName = name
}
if ecRow.GSOverride != "" {
ec.GoStyleName = ecRow.GSOverride
} else {
ec.GoStyleName = strcase.ToCamel(name)
}
ecs = append(ecs, ec)
}
if *goOutfile != "" {
if err := execTmpl("go", *goOutfile, goTmpl, ecs); err != nil {
log.Fatalf("Failed to execute Go template: %v", err)
}
}
if *pyOutfile != "" {
if err := execTmpl("py", *pyOutfile, pyTmpl, ecs); err != nil {
log.Fatalf("Failed to execute Python template: %v", err)
}
}
}
func execTmpl(name, outfile, templ string, ecs []ExitCode) error {
t, err := template.New(name).Parse(templ)
if err != nil {
return fmt.Errorf("failed to parse template: %w", err)
}
f, err := os.OpenFile(outfile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("failed to open output file: %w", err)
}
defer f.Close()
if err := t.Execute(f, ecs); err != nil {
return fmt.Errorf("failed to execute template: %w", err)
}
return nil
}