Skip to content

Commit a3ae571

Browse files
committed
Add --error-on-diff flag to get an exit code 2 when there were diffs generated
1 parent 91aca1d commit a3ae571

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

cmd/generate.go

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,28 @@ import (
2020
"github.com/printeers/trek/internal/postgres"
2121
)
2222

23+
// ExitError is an error that carries a specific exit code.
24+
type ExitError struct {
25+
Code int
26+
Message string
27+
}
28+
29+
func (e *ExitError) Error() string {
30+
return e.Message
31+
}
32+
33+
// ErrDiffDetected is returned by generate when diff statements are generated.
34+
var ErrDiffDetected = &ExitError{Code: 2, Message: "diff statements detected"}
35+
2336
//nolint:gocognit,cyclop
2437
func NewGenerateCommand() *cobra.Command {
2538
var (
26-
dev bool
27-
cleanup bool
28-
overwrite bool
29-
stdout bool
30-
check bool
39+
dev bool
40+
cleanup bool
41+
overwrite bool
42+
stdout bool
43+
check bool
44+
errorOnDiff bool
3145
)
3246

3347
generateCmd := &cobra.Command{
@@ -99,7 +113,7 @@ func NewGenerateCommand() *cobra.Command {
99113
}
100114
}
101115

102-
err = runWithStdout(ctx, config, wd, tmpDir, migrationsDir, len(migrationFiles) == 0)
116+
err = runWithStdout(ctx, config, wd, tmpDir, migrationsDir, len(migrationFiles) == 0, errorOnDiff)
103117
if err != nil {
104118
return err
105119
}
@@ -115,7 +129,7 @@ func NewGenerateCommand() *cobra.Command {
115129
return fmt.Errorf("failed to create temporary directory: %w", err)
116130
}
117131

118-
err = runWithStdout(ctx, config, wd, tmpDir, migrationsDir, len(migrationFiles) == 0)
132+
err = runWithStdout(ctx, config, wd, tmpDir, migrationsDir, len(migrationFiles) == 0, errorOnDiff)
119133
if err != nil {
120134
return err
121135
}
@@ -156,7 +170,8 @@ func NewGenerateCommand() *cobra.Command {
156170
}
157171

158172
var updated bool
159-
updated, err = runWithFile(ctx, config, wd, tmpDir, migrationsDir, newMigrationFilePath, migrationNumber)
173+
updated, err = runWithFile(
174+
ctx, config, wd, tmpDir, migrationsDir, newMigrationFilePath, migrationNumber, errorOnDiff)
160175
if err != nil {
161176
return err
162177
}
@@ -200,6 +215,7 @@ func NewGenerateCommand() *cobra.Command {
200215
generateCmd.Flags().BoolVar(&overwrite, "overwrite", false, "Overwrite existing files")
201216
generateCmd.Flags().BoolVar(&stdout, "stdout", false, "Output migration statements to stdout")
202217
generateCmd.Flags().BoolVar(&check, "check", true, "Run checks after generating the migration")
218+
generateCmd.Flags().BoolVar(&errorOnDiff, "error-on-diff", false, "Exit with code 2 if diff statements are generated")
203219

204220
return generateCmd
205221
}
@@ -222,6 +238,7 @@ func runWithStdout(
222238
tmpDir,
223239
migrationsDir string,
224240
initial bool,
241+
errorOnDiff bool,
225242
) error {
226243
updated, err := checkIfUpdated(config, wd)
227244
if err != nil {
@@ -313,6 +330,10 @@ func runWithStdout(
313330
fmt.Println("--")
314331
fmt.Println(statements)
315332
fmt.Println("--")
333+
334+
if errorOnDiff && statements != "" {
335+
return ErrDiffDetected
336+
}
316337
}
317338

318339
return nil
@@ -327,6 +348,7 @@ func runWithFile(
327348
migrationsDir,
328349
newMigrationFilePath string,
329350
migrationNumber uint,
351+
errorOnDiff bool,
330352
) (bool, error) {
331353
updated, err := checkIfUpdated(config, wd)
332354
if err != nil {
@@ -412,6 +434,10 @@ func runWithFile(
412434
return false, fmt.Errorf("failed to write template files: %w", err)
413435
}
414436

437+
if errorOnDiff && statements != "" {
438+
return true, ErrDiffDetected
439+
}
440+
415441
return true, nil
416442
}
417443

cmd/init.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,16 @@ func NewInitCommand() *cobra.Command {
163163
return fmt.Errorf("failed to create temporary directory: %w", err)
164164
}
165165

166-
_, err = runWithFile(ctx, config, wd, tmpDir, migrationsDir, filepath.Join(migrationsDir, "001_init.up.sql"), 1)
166+
_, err = runWithFile(
167+
ctx,
168+
config,
169+
wd,
170+
tmpDir,
171+
migrationsDir,
172+
filepath.Join(migrationsDir, "001_init.up.sql"),
173+
1,
174+
false,
175+
)
167176
if err != nil {
168177
return fmt.Errorf("failed to generate first migration: %w", err)
169178
}

main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"errors"
45
"fmt"
56
"os"
67

@@ -10,6 +11,10 @@ import (
1011
func main() {
1112
if err := cmd.NewRootCommand().Execute(); err != nil {
1213
_, _ = fmt.Fprintln(os.Stderr, err)
14+
var exitErr *cmd.ExitError
15+
if errors.As(err, &exitErr) {
16+
os.Exit(exitErr.Code)
17+
}
1318
os.Exit(1)
1419
}
1520
}

0 commit comments

Comments
 (0)