Skip to content

Commit c8c0233

Browse files
authored
*: clean up some tiny things (#1966)
1 parent 2395aec commit c8c0233

File tree

9 files changed

+55
-79
lines changed

9 files changed

+55
-79
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ install:
99
test:
1010
go test ./...
1111

12+
vet:
13+
go vet ./...
14+
1215
test-examples:
1316
go test --tags=examples ./...
1417

1518
build-endtoend:
1619
cd ./internal/endtoend/testdata && go build ./...
1720

18-
test-ci: test-examples build-endtoend
21+
test-ci: test-examples build-endtoend vet
1922

2023
regen: sqlc-dev sqlc-gen-json
2124
go run ./scripts/regenerate/

internal/cmd/cmd.go

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -45,46 +45,49 @@ func Do(args []string, stdin io.Reader, stdout io.Writer, stderr io.Writer) int
4545
rootCmd.SetOut(stdout)
4646
rootCmd.SetErr(stderr)
4747

48-
ctx, cleanup, err := tracer.Start(context.Background())
49-
if err != nil {
50-
fmt.Printf("failed to start trace: %v\n", err)
51-
return 1
48+
ctx := context.Background()
49+
if debug.Debug.Trace != "" {
50+
tracectx, cleanup, err := tracer.Start(ctx)
51+
if err != nil {
52+
fmt.Printf("failed to start trace: %v\n", err)
53+
return 1
54+
}
55+
ctx = tracectx
56+
defer cleanup()
5257
}
53-
defer cleanup()
5458

55-
if err := rootCmd.ExecuteContext(ctx); err == nil {
56-
return 0
57-
}
58-
if exitError, ok := err.(*exec.ExitError); ok {
59-
return exitError.ExitCode()
59+
if err := rootCmd.ExecuteContext(ctx); err != nil {
60+
fmt.Fprintf(stderr, "%v\n", err)
61+
if exitError, ok := err.(*exec.ExitError); ok {
62+
return exitError.ExitCode()
63+
} else {
64+
return 1
65+
}
6066
}
61-
return 1
67+
return 0
6268
}
6369

6470
var version string
6571

6672
var versionCmd = &cobra.Command{
6773
Use: "version",
6874
Short: "Print the sqlc version number",
69-
Run: func(cmd *cobra.Command, args []string) {
70-
if debug.Traced {
71-
defer trace.StartRegion(cmd.Context(), "version").End()
72-
}
75+
RunE: func(cmd *cobra.Command, args []string) error {
76+
defer trace.StartRegion(cmd.Context(), "version").End()
7377
if version == "" {
7478
fmt.Printf("%s\n", info.Version)
7579
} else {
7680
fmt.Printf("%s\n", version)
7781
}
82+
return nil
7883
},
7984
}
8085

8186
var initCmd = &cobra.Command{
8287
Use: "init",
8388
Short: "Create an empty sqlc.yaml settings file",
8489
RunE: func(cmd *cobra.Command, args []string) error {
85-
if debug.Traced {
86-
defer trace.StartRegion(cmd.Context(), "init").End()
87-
}
90+
defer trace.StartRegion(cmd.Context(), "init").End()
8891
file := "sqlc.yaml"
8992
if f := cmd.Flag("file"); f != nil && f.Changed {
9093
file = f.Value.String()
@@ -153,26 +156,23 @@ func getConfigPath(stderr io.Writer, f *pflag.Flag) (string, string) {
153156
var genCmd = &cobra.Command{
154157
Use: "generate",
155158
Short: "Generate Go code from SQL",
156-
Run: func(cmd *cobra.Command, args []string) {
157-
if debug.Traced {
158-
defer trace.StartRegion(cmd.Context(), "generate").End()
159-
}
159+
RunE: func(cmd *cobra.Command, args []string) error {
160+
defer trace.StartRegion(cmd.Context(), "generate").End()
160161
stderr := cmd.ErrOrStderr()
161162
dir, name := getConfigPath(stderr, cmd.Flag("file"))
162163
output, err := Generate(cmd.Context(), ParseEnv(cmd), dir, name, stderr)
163164
if err != nil {
164-
os.Exit(1)
165-
}
166-
if debug.Traced {
167-
defer trace.StartRegion(cmd.Context(), "writefiles").End()
165+
return err
168166
}
167+
defer trace.StartRegion(cmd.Context(), "writefiles").End()
169168
for filename, source := range output {
170169
os.MkdirAll(filepath.Dir(filename), 0755)
171170
if err := os.WriteFile(filename, []byte(source), 0644); err != nil {
172171
fmt.Fprintf(stderr, "%s: %s\n", filename, err)
173-
os.Exit(1)
172+
return err
174173
}
175174
}
175+
return nil
176176
},
177177
}
178178

@@ -194,9 +194,7 @@ var checkCmd = &cobra.Command{
194194
Use: "compile",
195195
Short: "Statically check SQL for syntax and type errors",
196196
RunE: func(cmd *cobra.Command, args []string) error {
197-
if debug.Traced {
198-
defer trace.StartRegion(cmd.Context(), "compile").End()
199-
}
197+
defer trace.StartRegion(cmd.Context(), "compile").End()
200198
stderr := cmd.ErrOrStderr()
201199
dir, name := getConfigPath(stderr, cmd.Flag("file"))
202200
if _, err := Generate(cmd.Context(), ParseEnv(cmd), dir, name, stderr); err != nil {
@@ -239,9 +237,7 @@ var diffCmd = &cobra.Command{
239237
Use: "diff",
240238
Short: "Compare the generated files to the existing files",
241239
RunE: func(cmd *cobra.Command, args []string) error {
242-
if debug.Traced {
243-
defer trace.StartRegion(cmd.Context(), "diff").End()
244-
}
240+
defer trace.StartRegion(cmd.Context(), "diff").End()
245241
stderr := cmd.ErrOrStderr()
246242
dir, name := getConfigPath(stderr, cmd.Flag("file"))
247243
if err := Diff(cmd.Context(), ParseEnv(cmd), dir, name, stderr); err != nil {

internal/cmd/diff.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,14 @@ import (
1111
"strings"
1212

1313
"github.com/cubicdaiya/gonp"
14-
"github.com/kyleconroy/sqlc/internal/debug"
1514
)
1615

1716
func Diff(ctx context.Context, e Env, dir, name string, stderr io.Writer) error {
1817
output, err := Generate(ctx, e, dir, name, stderr)
1918
if err != nil {
2019
return err
2120
}
22-
if debug.Traced {
23-
defer trace.StartRegion(ctx, "checkfiles").End()
24-
}
21+
defer trace.StartRegion(ctx, "checkfiles").End()
2522
var errored bool
2623

2724
keys := make([]string, 0, len(output))

internal/cmd/generate.go

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -193,17 +193,12 @@ func Generate(ctx context.Context, e Env, dir, filename string, stderr io.Writer
193193
name = sql.Plugin.Plugin
194194
}
195195

196-
var packageRegion *trace.Region
197-
if debug.Traced {
198-
packageRegion = trace.StartRegion(ctx, "package")
199-
trace.Logf(ctx, "", "name=%s dir=%s plugin=%s", name, dir, lang)
200-
}
196+
packageRegion := trace.StartRegion(ctx, "package")
197+
trace.Logf(ctx, "", "name=%s dir=%s plugin=%s", name, dir, lang)
201198

202199
result, failed := parse(ctx, name, dir, sql.SQL, combo, parseOpts, stderr)
203200
if failed {
204-
if packageRegion != nil {
205-
packageRegion.End()
206-
}
201+
packageRegion.End()
207202
errored = true
208203
break
209204
}
@@ -213,9 +208,7 @@ func Generate(ctx context.Context, e Env, dir, filename string, stderr io.Writer
213208
fmt.Fprintf(stderr, "# package %s\n", name)
214209
fmt.Fprintf(stderr, "error generating code: %s\n", err)
215210
errored = true
216-
if packageRegion != nil {
217-
packageRegion.End()
218-
}
211+
packageRegion.End()
219212
continue
220213
}
221214

@@ -227,9 +220,7 @@ func Generate(ctx context.Context, e Env, dir, filename string, stderr io.Writer
227220
filename := filepath.Join(dir, out, n)
228221
output[filename] = source
229222
}
230-
if packageRegion != nil {
231-
packageRegion.End()
232-
}
223+
packageRegion.End()
233224
}
234225

235226
if errored {
@@ -239,9 +230,7 @@ func Generate(ctx context.Context, e Env, dir, filename string, stderr io.Writer
239230
}
240231

241232
func parse(ctx context.Context, name, dir string, sql config.SQL, combo config.CombinedSettings, parserOpts opts.Parser, stderr io.Writer) (*compiler.Result, bool) {
242-
if debug.Traced {
243-
defer trace.StartRegion(ctx, "parse").End()
244-
}
233+
defer trace.StartRegion(ctx, "parse").End()
245234
c := compiler.NewCompiler(sql, combo)
246235
if err := c.ParseCatalog(sql.Schema); err != nil {
247236
fmt.Fprintf(stderr, "# package %s\n", name)
@@ -272,10 +261,7 @@ func parse(ctx context.Context, name, dir string, sql config.SQL, combo config.C
272261
}
273262

274263
func codegen(ctx context.Context, combo config.CombinedSettings, sql outPair, result *compiler.Result) (string, *plugin.CodeGenResponse, error) {
275-
var region *trace.Region
276-
if debug.Traced {
277-
region = trace.StartRegion(ctx, "codegen")
278-
}
264+
defer trace.StartRegion(ctx, "codegen").End()
279265
req := codeGenRequest(result, combo)
280266
var handler ext.Handler
281267
var out string
@@ -319,8 +305,5 @@ func codegen(ctx context.Context, combo config.CombinedSettings, sql outPair, re
319305
return "", nil, fmt.Errorf("missing language backend")
320306
}
321307
resp, err := handler.Generate(ctx, req)
322-
if region != nil {
323-
region.End()
324-
}
325308
return out, resp, err
326309
}

internal/codegen/golang/result.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func buildStructs(req *plugin.CodeGenRequest) []Struct {
7171
})
7272
}
7373
s := Struct{
74-
Table: plugin.Identifier{Schema: schema.Name, Name: table.Rel.Name},
74+
Table: &plugin.Identifier{Schema: schema.Name, Name: table.Rel.Name},
7575
Name: StructName(structName, req.Settings),
7676
Comment: table.Comment,
7777
}
@@ -214,7 +214,7 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
214214
c := query.Columns[i]
215215
sameName := f.Name == StructName(columnName(c, i), req.Settings)
216216
sameType := f.Type == goType(req, c)
217-
sameTable := sdk.SameTableName(c.Table, &s.Table, req.Catalog.DefaultSchema)
217+
sameTable := sdk.SameTableName(c.Table, s.Table, req.Catalog.DefaultSchema)
218218
if !sameName || !sameType || !sameTable {
219219
same = false
220220
}

internal/codegen/golang/struct.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
type Struct struct {
12-
Table plugin.Identifier
12+
Table *plugin.Identifier
1313
Name string
1414
Fields []Field
1515
Comment string

internal/codegen/json/gen.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ import (
1111
"github.com/kyleconroy/sqlc/internal/plugin"
1212
)
1313

14-
func parseOptions(req *plugin.CodeGenRequest) (plugin.JSONCode, error) {
14+
func parseOptions(req *plugin.CodeGenRequest) (*plugin.JSONCode, error) {
1515
if req.Settings == nil {
16-
return plugin.JSONCode{}, nil
16+
return new(plugin.JSONCode), nil
1717
}
1818
if req.Settings.Codegen != nil {
1919
if len(req.Settings.Codegen.Options) != 0 {
20-
var options plugin.JSONCode
20+
var options *plugin.JSONCode
2121
dec := ejson.NewDecoder(bytes.NewReader(req.Settings.Codegen.Options))
2222
dec.DisallowUnknownFields()
2323
if err := dec.Decode(&options); err != nil {
@@ -27,9 +27,9 @@ func parseOptions(req *plugin.CodeGenRequest) (plugin.JSONCode, error) {
2727
}
2828
}
2929
if req.Settings.Json != nil {
30-
return *req.Settings.Json, nil
30+
return req.Settings.Json, nil
3131
}
32-
return plugin.JSONCode{}, nil
32+
return new(plugin.JSONCode), nil
3333
}
3434

3535
func Generate(ctx context.Context, req *plugin.CodeGenRequest) (*plugin.CodeGenResponse, error) {

internal/debug/dump.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@ import (
99
)
1010

1111
var Active bool
12-
var Traced bool
1312
var Debug opts.Debug
1413

1514
func init() {
1615
Active = os.Getenv("SQLCDEBUG") != ""
1716
if Active {
1817
Debug = opts.DebugFromEnv()
19-
Traced = Debug.Trace != ""
2018
}
2119
}
2220

internal/tracer/trace.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,17 @@ import (
99
"github.com/kyleconroy/sqlc/internal/debug"
1010
)
1111

12-
func Start(base context.Context) (context.Context, func(), error) {
13-
if !debug.Traced {
14-
return base, func() {}, nil
15-
}
16-
12+
// Start starts Go's runtime tracing facility.
13+
// Traces will be written to the file named by [debug.Debug.Trace].
14+
// It also starts a new [*trace.Task] that will be stopped when the cleanup is called.
15+
func Start(base context.Context) (_ context.Context, cleanup func(), _ error) {
1716
f, err := os.Create(debug.Debug.Trace)
1817
if err != nil {
19-
return base, func() {}, fmt.Errorf("failed to create trace output file: %v", err)
18+
return base, cleanup, fmt.Errorf("failed to create trace output file: %v", err)
2019
}
2120

2221
if err := trace.Start(f); err != nil {
23-
return base, func() {}, fmt.Errorf("failed to start trace: %v", err)
22+
return base, cleanup, fmt.Errorf("failed to start trace: %v", err)
2423
}
2524

2625
ctx, task := trace.NewTask(base, "sqlc")

0 commit comments

Comments
 (0)