Skip to content

Commit

Permalink
ugh
Browse files Browse the repository at this point in the history
  • Loading branch information
mortenson committed Jun 27, 2024
1 parent 13fbf71 commit eab781a
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.vscode
tmp
ripoff
./ripoff
84 changes: 84 additions & 0 deletions cmd/ripoff/ripoff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package main

import (
"context"
"flag"
"fmt"
"log/slog"
"os"
"path"

"github.com/jackc/pgx/v5"

"github.com/mortenson/ripoff"
)

func errAttr(err error) slog.Attr {
return slog.Any("error", err)
}

func main() {
verbosePtr := flag.Bool("v", false, "enable verbose output")
softPtr := flag.Bool("s", false, "do not commit generated queries")
flag.Parse()

if *verbosePtr {
slog.SetLogLoggerLevel(slog.LevelDebug)
}

dburl := os.Getenv("DATABASE_URL")
if dburl == "" {
slog.Error("DATABASE_URL env variable is required")
os.Exit(1)
}

if len(flag.Args()) != 1 {
slog.Error("Path to YAML files required")
os.Exit(1)
}
rootDirectory := path.Clean(flag.Arg(0))
totalRipoff, err := ripoff.RipoffFromDirectory(rootDirectory)
if err != nil {
slog.Error("Could not load ripoff", errAttr(err))
os.Exit(1)
}

ctx := context.Background()
conn, err := pgx.Connect(ctx, dburl)
if err != nil {
slog.Error("Could not connect to database", errAttr(err))
os.Exit(1)
}
defer conn.Close(ctx)

tx, err := conn.Begin(ctx)
if err != nil {
slog.Error("Could not create transaction", errAttr(err))
os.Exit(1)
}
defer func() {
err = tx.Rollback(ctx)
if err != nil {
slog.Error("Could not rollback transaction", errAttr(err))
os.Exit(1)
}
}()

err = ripoff.RunRipoff(ctx, tx, totalRipoff)
if err != nil {
slog.Error("Could not run ripoff", errAttr(err))
os.Exit(1)
}

if *softPtr {
slog.Info("Not committing transaction due to -s flag")
} else {
err = tx.Commit(ctx)
if err != nil {
slog.Error("Could not commit transaction", errAttr(err))
os.Exit(1)
}
}

slog.Info(fmt.Sprintf("Ripoff complete, %d rows processed", len(totalRipoff.Rows)))
}

0 comments on commit eab781a

Please sign in to comment.