-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
125 lines (104 loc) · 2.48 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
125
package main
import (
"bytes"
"errors"
"flag"
"fmt"
"io"
"log"
"os"
"os/exec"
"sync"
"text/template"
"github.com/joncalhoun/pipe"
)
func main() {
var wg sync.WaitGroup
var err error
var out bytes.Buffer
var d data
flag.StringVar(&d.Pkg, "pkg", "relay", "String. The name of the package where the generated entities will live. Default: relay.")
flag.StringVar(&d.Marshal, "marshal", "bson", "String. The marshaling mode for the generated fields. Default: bson.")
flag.StringVar(&d.Name, "name", "", "String. The name of the entity to generate its relay types. Required if not base.")
flag.StringVar(&d.Type, "type", "", "String. The entity type used in your GQL pipelines (usually the pointer to the entity w/pkg name). Required if not base.")
flag.BoolVar(&d.IsSDL, "sdl", false, "Boolean. Generate the SDL into a .graphql file for the desired template. Default: false.")
flag.BoolVar(&d.RenderBaseTemplate, "base", false, "Boolean. Generate the base template with the common interfaces. Default: false.")
flag.Parse()
err = d.validate()
if err != nil {
log.Fatal(err)
}
err = d.selectTemplate().Execute(&out, d)
if err != nil {
log.Fatal(err)
}
cmds := d.commands()
// BREAKPOINT: If there arent commands, ommit the processing
if len(cmds) == 0 {
fmt.Println(&out)
return
}
rc, wc, errCh := pipe.Commands(cmds...)
wg.Add(1)
go func(ch <-chan error, wg *sync.WaitGroup) {
defer wg.Done()
select {
case err := <-ch:
if err != nil {
panic(err)
}
}
return
}(errCh, &wg)
_, err = io.Copy(wc, &out)
if err != nil {
log.Fatal(err)
}
err = wc.Close()
if err != nil {
log.Fatal(err)
}
_, err = io.Copy(os.Stdout, rc)
if err != nil {
log.Fatal(err)
}
wg.Wait()
}
type data struct {
Pkg string
Name string
Marshal string
Type string
RenderBaseTemplate bool
IsSDL bool
}
func (d *data) commands() []*exec.Cmd {
if d.IsSDL {
return []*exec.Cmd{}
}
return []*exec.Cmd{exec.Command("gofmt"), exec.Command("goimports")}
}
func (d *data) validate() (err error) {
if d.RenderBaseTemplate {
if d.Name == "" || d.Type == "" {
return errors.New("Name or Type flags cannot be empty when calling a non-base generation")
}
}
return nil
}
func (d *data) selectTemplate() (temp *template.Template) {
if d.RenderBaseTemplate {
if d.IsSDL {
temp = sdlBaseTemplate
} else {
temp = baseTemplate
}
} else {
if d.IsSDL {
temp = sdlEntityTemplate
} else {
temp = entityTemplate
}
}
return temp
}