-
Notifications
You must be signed in to change notification settings - Fork 535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Experiemntal CLI - initial commit #741
Open
mfridman
wants to merge
1
commit into
master
Choose a base branch
from
mf/cli-refactor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,24 @@ | ||
module github.com/pressly/goose/v3 | ||
|
||
go 1.21.0 | ||
go 1.22 | ||
|
||
toolchain go1.23.1 | ||
|
||
require ( | ||
github.com/ClickHouse/clickhouse-go/v2 v2.28.3 | ||
github.com/charmbracelet/lipgloss v0.13.0 | ||
github.com/go-sql-driver/mysql v1.8.1 | ||
github.com/jackc/pgx/v5 v5.7.1 | ||
github.com/mfridman/buildversion v0.3.0 | ||
github.com/mfridman/interpolate v0.0.2 | ||
github.com/mfridman/xflag v0.0.0-20240825232106-efb77353e578 | ||
github.com/microsoft/go-mssqldb v1.7.2 | ||
github.com/peterbourgon/ff/v4 v4.0.0-alpha.4 | ||
github.com/sethvargo/go-retry v0.3.0 | ||
github.com/stretchr/testify v1.9.0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've given up trying to fight this, the rest of the community tends to favor this as the defacto assertion library. Instead of maintaining our own ./internal/check we should just use this. |
||
github.com/tursodatabase/libsql-client-go v0.0.0-20240902231107-85af5b9d094d | ||
github.com/vertica/vertica-sql-go v1.3.3 | ||
github.com/xo/dburl v0.23.2 | ||
github.com/ydb-platform/ydb-go-sdk/v3 v3.80.2 | ||
github.com/ziutek/mymysql v1.5.4 | ||
go.uber.org/multierr v1.11.0 | ||
|
@@ -25,6 +31,8 @@ require ( | |
github.com/ClickHouse/ch-go v0.61.5 // indirect | ||
github.com/andybalholm/brotli v1.1.0 // indirect | ||
github.com/antlr4-go/antlr/v4 v4.13.0 // indirect | ||
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect | ||
github.com/charmbracelet/x/ansi v0.1.4 // indirect | ||
github.com/coder/websocket v1.8.12 // indirect | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/dustin/go-humanize v1.0.1 // indirect | ||
|
@@ -44,14 +52,18 @@ require ( | |
github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901 // indirect | ||
github.com/jonboulle/clockwork v0.4.0 // indirect | ||
github.com/klauspost/compress v1.17.7 // indirect | ||
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect | ||
github.com/mattn/go-isatty v0.0.20 // indirect | ||
github.com/mattn/go-runewidth v0.0.15 // indirect | ||
github.com/muesli/termenv v0.15.2 // indirect | ||
github.com/ncruces/go-strftime v0.1.9 // indirect | ||
github.com/paulmach/orb v0.11.1 // indirect | ||
github.com/pierrec/lz4/v4 v4.1.21 // indirect | ||
github.com/pkg/errors v0.9.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/prometheus/procfs v0.12.0 // indirect | ||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect | ||
github.com/rivo/uniseg v0.4.7 // indirect | ||
github.com/segmentio/asm v1.2.0 // indirect | ||
github.com/shopspring/decimal v1.4.0 // indirect | ||
github.com/ydb-platform/ydb-go-genproto v0.0.0-20240528144234-5d5a685e41f7 // indirect | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package cli | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"os/signal" | ||
"runtime" | ||
"syscall" | ||
) | ||
|
||
// Main is the entry point for the CLI. | ||
// | ||
// If an error is returned, it is printed to stderr and the process exits with a non-zero exit code. | ||
// The process is also canceled when an interrupt signal is received. This function and does not | ||
// return. | ||
func Main(opts ...Options) { | ||
ctx, stop := newContext() | ||
go func() { | ||
defer stop() | ||
if err := Run(ctx, os.Args[1:], opts...); err != nil { | ||
fmt.Fprintln(os.Stderr, err.Error()) | ||
os.Exit(1) | ||
} | ||
}() | ||
// TODO(mf): this looks wonky because we're not waiting for the context to be done. But | ||
// eventually, I'd like to add a timeout here so we don't hang indefinitely. | ||
<-ctx.Done() | ||
os.Exit(0) | ||
} | ||
|
||
// Run runs the CLI with the provided arguments. The arguments should not include the command name | ||
// itself, only the arguments to the command, use os.Args[1:]. | ||
// | ||
// Options can be used to customize the behavior of the CLI, such as setting the environment, | ||
// redirecting stdout and stderr, and providing a custom filesystem such as embed.FS. | ||
func Run(ctx context.Context, args []string, opts ...Options) error { | ||
return run(ctx, args, opts...) | ||
} | ||
|
||
func newContext() (context.Context, context.CancelFunc) { | ||
signals := []os.Signal{os.Interrupt} | ||
if runtime.GOOS != "windows" { | ||
signals = append(signals, syscall.SIGTERM) | ||
} | ||
return signal.NotifyContext(context.Background(), signals...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package cli | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"strings" | ||
"testing" | ||
"testing/fstest" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
_ "modernc.org/sqlite" | ||
) | ||
|
||
const ( | ||
version = "devel" | ||
) | ||
|
||
func TestRun(t *testing.T) { | ||
t.Run("version", func(t *testing.T) { | ||
stdout, stderr, err := runCommand("--version") | ||
require.NoError(t, err) | ||
assert.Empty(t, stderr) | ||
assert.Equal(t, stdout, "goose version: "+version+"\n") | ||
}) | ||
t.Run("with_filesystem", func(t *testing.T) { | ||
fsys := fstest.MapFS{ | ||
"migrations/001_foo.sql": {Data: []byte(`-- +goose up`)}, | ||
} | ||
command := "status --dir=migrations --dbstring=sqlite3://:memory: --json" | ||
buf := bytes.NewBuffer(nil) | ||
err := Run(context.Background(), strings.Split(command, " "), WithFilesystem(fsys.Sub), WithStdout(buf)) | ||
require.NoError(t, err) | ||
var status migrationsStatus | ||
err = json.Unmarshal(buf.Bytes(), &status) | ||
require.NoError(t, err) | ||
require.Len(t, status.Migrations, 1) | ||
assert.True(t, status.HasPending) | ||
assert.Equal(t, "001_foo.sql", status.Migrations[0].Source.Path) | ||
assert.Equal(t, "pending", status.Migrations[0].State) | ||
assert.Equal(t, "", status.Migrations[0].AppliedAt) | ||
}) | ||
} | ||
|
||
func runCommand(args ...string) (string, string, error) { | ||
stdout, stderr := bytes.NewBuffer(nil), bytes.NewBuffer(nil) | ||
err := Run( | ||
context.Background(), | ||
args, | ||
WithStdout(stdout), | ||
WithStderr(stderr), | ||
WithVersion(version), | ||
) | ||
return stdout.String(), stderr.String(), err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package cli | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/peterbourgon/ff/v4" | ||
) | ||
|
||
type cmdRoot struct { | ||
state *state | ||
fs *ff.FlagSet | ||
|
||
// flags | ||
version bool | ||
} | ||
|
||
func newRootCommand(state *state) *ff.Command { | ||
c := &cmdRoot{ | ||
state: state, | ||
fs: ff.NewFlagSet("goose"), | ||
} | ||
c.fs.BoolVarDefault(&c.version, 0, "version", false, "print version and exit") | ||
|
||
cmd := &ff.Command{ | ||
Name: "goose", | ||
Usage: "goose <command> [flags] [args...]", | ||
ShortHelp: "A database migration tool. Supports SQL migrations and Go functions.", | ||
Flags: c.fs, | ||
Exec: c.exec, | ||
} | ||
return cmd | ||
} | ||
|
||
func (c *cmdRoot) exec(ctx context.Context, args []string) error { | ||
if c.version { | ||
fmt.Fprintf(c.state.stdout, "goose version: %s\n", c.state.version) | ||
return nil | ||
} | ||
return nil | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to avoid a long-running feature branch, so this gate is to allow us to iterate and also use it in real-world projects.