-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
139 lines (118 loc) · 3.38 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"os/exec"
"strings"
"golang.org/x/mod/modfile"
"github.com/walterwanderley/xo-grpc/metadata"
)
var (
databaseDriverModule string
databaseDriverName string
module string
showVersion bool
help bool
verbose bool
)
func main() {
flag.BoolVar(&help, "h", false, "Help for this program")
flag.BoolVar(&showVersion, "v", false, "Show version")
flag.BoolVar(&verbose, "verbose", false, "Verbose")
flag.StringVar(&module, "m", "my-project", "Go module name if there are no go.mod")
flag.StringVar(&databaseDriverModule, "db-module", "github.com/jackc/pgx/v4/stdlib", "Database driver module")
flag.StringVar(&databaseDriverName, "db-driver", "pgx", "Database driver name")
flag.Parse()
if help {
printHelp()
return
}
if showVersion {
fmt.Println(version)
return
}
if len(flag.Args()) < 1 {
fmt.Printf("\nmodelsPath is required\n\n")
printHelp()
os.Exit(1)
}
modelsPath := strings.TrimPrefix(strings.TrimPrefix(strings.TrimSuffix(flag.Arg(0), "/"), "."), "/")
if m := moduleFromGoMod(); m != "" {
fmt.Println("Using module path from go.mod:", m)
module = m
}
def := metadata.Definition{
GoModule: module,
DatabaseDriverModule: databaseDriverModule,
DatabaseDriverName: databaseDriverName,
ModelsPath: modelsPath,
Packages: make([]*metadata.Package, 0),
}
pkgs, err := metadata.ParsePackages(modelsPath, module)
if err != nil {
log.Fatal("parser error:", err.Error())
}
def.Packages = pkgs
wd, err := os.Getwd()
if err != nil {
log.Fatal("unable to get working directory:", err.Error())
}
err = process(&def, wd)
if err != nil {
log.Fatal("unable to process templates:", err.Error())
}
postProcess(&def, wd)
}
func moduleFromGoMod() string {
f, err := os.Open("go.mod")
if err != nil {
return ""
}
defer f.Close()
b, err := io.ReadAll(f)
if err != nil {
return ""
}
return modfile.ModulePath(b)
}
func postProcess(def *metadata.Definition, workingDirectory string) {
fmt.Printf("Configuring project %s...\n", def.GoModule)
execCommand("go mod init " + def.GoModule)
execCommand("go mod tidy")
execCommand("go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@latest")
execCommand("go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2@latest")
execCommand("go install google.golang.org/protobuf/cmd/protoc-gen-go@latest")
execCommand("go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest")
execCommand("go install github.com/bufbuild/buf/cmd/buf@latest")
fmt.Println("Compiling protocol buffers...")
if err := os.Chdir("proto"); err != nil {
panic(err)
}
execCommand("buf mod update")
if err := os.Chdir(workingDirectory); err != nil {
panic(err)
}
execCommand("buf generate")
execCommand("buf format -w")
execCommand("go mod tidy")
fmt.Println("Finished!")
}
func execCommand(command string) error {
line := strings.Split(command, " ")
cmd := exec.Command(line[0], line[1:]...)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
return fmt.Errorf("[error] %q: %w", command, err)
}
return nil
}
func printHelp() {
fmt.Println("xo-grpc [flags] modelsPath")
fmt.Println("\nflags:")
flag.PrintDefaults()
fmt.Println("\nFor more information, please visit https://github.com/walterwanderley/xo-grpc")
}