-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
156 lines (128 loc) · 3.45 KB
/
helpers.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"bytes"
"errors"
"fmt"
"io"
"io/fs"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"text/tabwriter"
"github.com/spf13/viper"
"github.com/volatiletech/sqlboiler/v4/importers"
"golang.org/x/mod/modfile"
)
type commandFailure string
func (c commandFailure) Error() string {
return string(c)
}
// goModInfo returns the main module's root directory
// and the parsed contents of the go.mod file.
func goModInfo() (*modfile.File, error) {
goModPath, err := findGoMod()
if err != nil {
return nil, fmt.Errorf("cannot find main module: %w", err)
}
data, err := ioutil.ReadFile(goModPath)
if err != nil {
return nil, fmt.Errorf("cannot read main go.mod file: %w", err)
}
modf, err := modfile.Parse(goModPath, data, nil)
if err != nil {
return nil, fmt.Errorf("could not parse go.mod: %w", err)
}
return modf, nil
}
func findGoMod() (string, error) {
out, err := runCmd(".", "go", "env", "GOMOD")
if err != nil {
return "", err
}
out = strings.TrimSpace(out)
if out == "" {
return "", errors.New("no go.mod file found in any parent directory")
}
return strings.TrimSpace(out), nil
}
func runCmd(dir string, name string, args ...string) (string, error) {
var outData, errData bytes.Buffer
c := exec.Command(name, args...)
c.Stdout = &outData
c.Stderr = &errData
c.Dir = dir
err := c.Run()
if err == nil {
return outData.String(), nil
}
if _, ok := err.(*exec.ExitError); ok && errData.Len() > 0 {
return "", errors.New(strings.TrimSpace(errData.String()))
}
return "", fmt.Errorf("cannot run %q: %v", append([]string{name}, args...), err)
}
func allKeys(prefix string) []string {
keys := make(map[string]bool)
prefix += "."
for _, e := range os.Environ() {
splits := strings.SplitN(e, "=", 2)
key := strings.ReplaceAll(strings.ToLower(splits[0]), "_", ".")
if strings.HasPrefix(key, prefix) {
keys[strings.ReplaceAll(key, prefix, "")] = true
}
}
for _, key := range viper.AllKeys() {
if strings.HasPrefix(key, prefix) {
keys[strings.ReplaceAll(key, prefix, "")] = true
}
}
keySlice := make([]string, 0, len(keys))
for k := range keys {
keySlice = append(keySlice, k)
}
return keySlice
}
func copyTemplates(dir string) error {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 0, ' ', tabwriter.Debug)
defer w.Flush()
return fs.WalkDir(templates, ".", func(path string, info fs.DirEntry, err error) error {
if err != nil {
return fmt.Errorf("an error was passed to the walkFunc: %w", err)
}
if info.IsDir() {
return nil
}
relPath := strings.TrimPrefix(path, "templates/")
tplFile, err := templates.Open(path)
if err != nil {
return fmt.Errorf("error when opening template file: %w", err)
}
defer tplFile.Close()
newFile, err := os.Create(filepath.Join(dir, relPath))
if err != nil {
return fmt.Errorf("error when creating new file: %w", err)
}
defer newFile.Close()
_, err = io.Copy(newFile, tplFile)
if err != nil {
return fmt.Errorf("error when copying file: %w", err)
}
return nil
})
}
func configureImports() importers.Collection {
imports := importers.NewDefaultImports()
imports.All = importers.Set{
Standard: []string{`"fmt"`},
ThirdParty: []string{
fmt.Sprintf(`models "%s"`, modelsPkg),
`"github.com/volatiletech/sqlboiler/v4/boil"`,
`"github.com/volatiletech/sqlboiler/v4/queries"`,
},
}
imports.Singleton["boilingfactory_main"] = importers.Set{
Standard: []string{`"fmt"`, `"context"`, `"reflect"`},
}
return imports
}