-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
124 lines (109 loc) · 3.53 KB
/
main.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
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
124
package main
import (
"fmt"
"io"
"log"
"os"
"strings"
"github.com/muesli/coral"
"github.com/nchern/go-codegen/pkg/code"
"github.com/nchern/go-codegen/pkg/constructor"
"github.com/nchern/go-codegen/pkg/generic"
"github.com/nchern/go-codegen/pkg/immutable"
"github.com/nchern/go-codegen/pkg/impl"
)
func init() {
log.SetFlags(0)
}
func dieIf(err error) {
if err != nil {
log.Fatal(err)
}
}
func inputStream(shouldMirrorSource bool) io.Reader {
if shouldMirrorSource {
return io.TeeReader(os.Stdin, os.Stdout)
}
return os.Stdin
}
var (
flagPkgName = ""
flagFileName = ""
flagBuiltInType = ""
flagOutputSource = false
commands = []*coral.Command{
{
Use: "generic [list of concrete types to substitute type vars]",
Short: "Processes go source as generic file and outputs code with substituted type vars.",
Args: coral.MinimumNArgs(1),
Run: func(cmd *coral.Command, args []string) {
var err error
var generator *generic.Generator
if flagBuiltInType != "" {
generator, err = generic.BuiltIn(flagBuiltInType)
dieIf(err)
} else {
generator = generic.FromFile(flagFileName)
}
err = code.WrapWithBannerPrinter(
generator.
WithPackageName(flagPkgName).
WithTypeMapping(generic.TypeMapFromStrings(args...))).
Generate(os.Stdout)
dieIf(err)
},
},
{
Use: "immutable",
Short: "Generates immutable structure implementation by a given interface.",
Args: coral.NoArgs,
Run: func(cmd *coral.Command, args []string) {
err := code.WrapWithBannerPrinter(
immutable.FromFile(flagFileName).
WithPackageName(flagPkgName)).
Generate(os.Stdout)
dieIf(err)
},
},
{
Use: "constructor",
Short: "Generates constructor function for a given struct read from stdin.",
Args: coral.NoArgs,
Run: func(cmd *coral.Command, args []string) {
err := constructor.FromReader(inputStream(flagOutputSource)).
Generate(os.Stdout)
dieIf(err)
},
},
{
Use: "impl",
Short: "Generates minimal interface implementation.",
Long: "Generates stubs to implement a given interface\n" +
"The difference between this command and impl(https://github.com/josharian/impl) utility is that interface declaration is read from stdin.\n" +
"So that it's really easy to use it with editors like Vim",
Args: coral.NoArgs,
Run: func(cmd *coral.Command, args []string) {
err := impl.FromReader(inputStream(flagOutputSource)).
Generate(os.Stdout)
dieIf(err)
},
},
}
)
func main() {
rootCmd := &coral.Command{
Use: "go-codegen",
Long: "Go code generation tool. Prints output to stdout",
}
rootCmd.PersistentFlags().StringVarP(&flagPkgName, "pkg", "p", "", "Golang package name. Substitutes existing package name or makes generator to add one")
rootCmd.PersistentFlags().StringVarP(&flagFileName, "file", "f", "", "input file name (reqiured, if no built-ins used)")
commands[0].Flags().StringVarP(&flagBuiltInType, "type", "t", "",
fmt.Sprintf("Generates based on predefined generic file. One of: %s", strings.Join(generic.BuiltInTypes(), ", ")))
commands[2].Flags().BoolVarP(&flagOutputSource, "out-src", "s", false, "if set, outputs the input source code before generated code. Could be helpful with editor integrations(e.g. vim)")
commands[3].Flags().BoolVarP(&flagOutputSource, "out-src", "s", false, "if set, outputs the input source code before generated code. Could be helpful with editor integrations(e.g. vim)")
for _, cmd := range commands {
rootCmd.AddCommand(cmd)
}
err := rootCmd.Execute()
dieIf(err)
}