-
Notifications
You must be signed in to change notification settings - Fork 525
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
957 additions
and
22 deletions.
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
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.