-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
129 lines (111 loc) · 2.92 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
package main
import (
"bufio"
"bytes"
_ "embed"
"errors"
"flag"
"fmt"
"html/template"
"io/fs"
"log"
"os"
"path/filepath"
"regexp"
"strings"
)
//go:embed init-templates/schema.graphql
var schemaFileContent string
//go:embed init-templates/gqlgen.yml.gotmpl
var configFileTemplate string
var modregex = regexp.MustCompile(`module ([^\s]*)`)
func getConfigFileContent(pkgName string) string {
var buf bytes.Buffer
if err := template.Must(template.New("gqlgen.yml").Parse(configFileTemplate)).Execute(&buf, pkgName); err != nil {
panic(err)
}
return buf.String()
}
func fileExists(filename string) bool {
_, err := os.Stat(filename)
return !errors.Is(err, fs.ErrNotExist)
}
func findModuleRoot(dir string) (roots string) {
if dir == "" {
panic("dir not set")
}
dir = filepath.Clean(dir)
// Look for enclosing go.mod.
for {
if fi, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil && !fi.IsDir() {
return dir
}
d := filepath.Dir(dir)
if d == dir { // the parent of the root is itself, so we can go no further
break
}
dir = d
}
return ""
}
func initFile(filename, contents string) error {
if err := os.MkdirAll(filepath.Dir(filename), 0o755); err != nil {
return fmt.Errorf("unable to create directory for file '%s': %w\n", filename, err)
}
if err := os.WriteFile(filename, []byte(contents), 0o644); err != nil {
return fmt.Errorf("unable to write file '%s': %w\n", filename, err)
}
return nil
}
func extractModuleName(modDir string) string {
content, err := os.ReadFile(filepath.Join(modDir, "go.mod"))
if err != nil {
log.Fatal("couldn't read file")
}
for {
advance, tkn, err := bufio.ScanLines(content, false)
if err != nil {
panic(fmt.Errorf("error parsing mod file: %w", err))
}
if advance == 0 {
break
}
s := strings.Trim(string(tkn), " \t")
if len(s) != 0 && !strings.HasPrefix(s, "//") {
break
}
if advance <= len(content) {
content = content[advance:]
}
}
moduleName := string(modregex.FindSubmatch(content)[1])
return moduleName
}
func main() {
configFlag := flag.String("config", "gqlgen.yml", "the config filename")
schemaFlag := flag.String("schema", "graph/schema.graphql", "where to write the schema stub to")
//serverFlag := flag.String("server", "where to write the server stub to", "server.go")
flag.Parse()
cwd, err := os.Getwd()
if err != nil {
log.Println(err)
err = fmt.Errorf("unable to determine current directory:%w", err)
log.Println(err)
}
modRoot := findModuleRoot(cwd)
if modRoot == "" {
err = fmt.Errorf("go.mod is missing. Please, do 'go mod init' first\n")
log.Println(err)
}
// create config
fmt.Println("Creating", *configFlag)
moduleName := extractModuleName(modRoot)
if err := initFile(*configFlag, getConfigFileContent(moduleName)); err != nil {
log.Println(err)
}
// create schema
fmt.Println("Creating", *schemaFlag)
if err := initFile(*schemaFlag, schemaFileContent); err != nil {
log.Println(err)
}
}