forked from sqlc-dev/sqlc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd.go
More file actions
169 lines (153 loc) · 4.2 KB
/
cmd.go
File metadata and controls
169 lines (153 loc) · 4.2 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
package cmd
import (
"context"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"runtime/trace"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
yaml "gopkg.in/yaml.v3"
"github.com/kyleconroy/sqlc/internal/config"
"github.com/kyleconroy/sqlc/internal/debug"
"github.com/kyleconroy/sqlc/internal/tracer"
)
// Do runs the command logic.
func Do(args []string, stdin io.Reader, stdout io.Writer, stderr io.Writer) int {
rootCmd := &cobra.Command{Use: "sqlc", SilenceUsage: true}
rootCmd.PersistentFlags().StringP("file", "f", "", "specify an alternate config file (default: sqlc.yaml)")
rootCmd.PersistentFlags().BoolP("experimental", "x", false, "enable experimental features (default: false)")
rootCmd.AddCommand(checkCmd)
rootCmd.AddCommand(genCmd)
rootCmd.AddCommand(initCmd)
rootCmd.AddCommand(versionCmd)
rootCmd.SetArgs(args)
rootCmd.SetIn(stdin)
rootCmd.SetOut(stdout)
rootCmd.SetErr(stderr)
ctx, cleanup, err := tracer.Start(context.Background())
if err != nil {
fmt.Printf("failed to start trace: %v\n", err)
return 1
}
defer cleanup()
if err := rootCmd.ExecuteContext(ctx); err == nil {
return 0
}
if exitError, ok := err.(*exec.ExitError); ok {
return exitError.ExitCode()
}
return 1
}
var version string
var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the sqlc version number",
Run: func(cmd *cobra.Command, args []string) {
if debug.Traced {
defer trace.StartRegion(cmd.Context(), "version").End()
}
if version == "" {
// When no version is set, return the next bug fix version
// after the most recent tag
fmt.Printf("%s\n", "v1.11.0")
} else {
fmt.Printf("%s\n", version)
}
},
}
var initCmd = &cobra.Command{
Use: "init",
Short: "Create an empty sqlc.yaml settings file",
RunE: func(cmd *cobra.Command, args []string) error {
if debug.Traced {
defer trace.StartRegion(cmd.Context(), "init").End()
}
file := "sqlc.yaml"
if f := cmd.Flag("file"); f != nil && f.Changed {
file = f.Value.String()
if file == "" {
return fmt.Errorf("file argument is empty")
}
}
if _, err := os.Stat(file); !os.IsNotExist(err) {
return nil
}
blob, err := yaml.Marshal(config.V1GenerateSettings{Version: "1"})
if err != nil {
return err
}
return os.WriteFile(file, blob, 0644)
},
}
type Env struct {
ExperimentalFeatures bool
}
func ParseEnv(c *cobra.Command) Env {
x := c.Flag("experimental")
return Env{ExperimentalFeatures: x != nil && x.Changed}
}
func getConfigPath(stderr io.Writer, f *pflag.Flag) (string, string) {
if f != nil && f.Changed {
file := f.Value.String()
if file == "" {
fmt.Fprintln(stderr, "error parsing config: file argument is empty")
os.Exit(1)
}
abspath, err := filepath.Abs(file)
if err != nil {
fmt.Fprintf(stderr, "error parsing config: absolute file path lookup failed: %s\n", err)
os.Exit(1)
}
return filepath.Dir(abspath), filepath.Base(abspath)
} else {
wd, err := os.Getwd()
if err != nil {
fmt.Fprintln(stderr, "error parsing sqlc.json: file does not exist")
os.Exit(1)
}
return wd, ""
}
}
var genCmd = &cobra.Command{
Use: "generate",
Short: "Generate Go code from SQL",
Run: func(cmd *cobra.Command, args []string) {
if debug.Traced {
defer trace.StartRegion(cmd.Context(), "generate").End()
}
stderr := cmd.ErrOrStderr()
dir, name := getConfigPath(stderr, cmd.Flag("file"))
output, err := Generate(cmd.Context(), ParseEnv(cmd), dir, name, stderr)
if err != nil {
os.Exit(1)
}
if debug.Traced {
defer trace.StartRegion(cmd.Context(), "writefiles").End()
}
for filename, source := range output {
os.MkdirAll(filepath.Dir(filename), 0755)
if err := os.WriteFile(filename, []byte(source), 0644); err != nil {
fmt.Fprintf(stderr, "%s: %s\n", filename, err)
os.Exit(1)
}
}
},
}
var checkCmd = &cobra.Command{
Use: "compile",
Short: "Statically check SQL for syntax and type errors",
RunE: func(cmd *cobra.Command, args []string) error {
if debug.Traced {
defer trace.StartRegion(cmd.Context(), "compile").End()
}
stderr := cmd.ErrOrStderr()
dir, name := getConfigPath(stderr, cmd.Flag("file"))
if _, err := Generate(cmd.Context(), ParseEnv(cmd), dir, name, stderr); err != nil {
os.Exit(1)
}
return nil
},
}