Skip to content
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
wants to merge 15 commits into
base: master
Choose a base branch
from
Open

Experiemntal CLI - initial commit #741

wants to merge 15 commits into from

Conversation

mfridman
Copy link
Collaborator

@mfridman mfridman commented Apr 6, 2024

This PR stubs out a newcli package for goose. There are no changes with the existing goose CLI code and everything is gated behind a feature flag GOOSE_CLI=1.

Some highlights

Help for every command (--help and -h)

$ GOOSE_CLI=1 goose --help

Will output nicely formatted help (currently only supports the goose status command, but others will quickly follow)

JSON support (--json)

It supports --json out-of-the-box, e.g.,

$ GOOSE_CLI=1 ./bin/goose status --dir ./testdata/migrations --dbstring=sqlite:./test.db --json | jq

Returns output that can be used programmatically, recently brought up by @witsch in #420 (comment), but requested for a long time #225, #350, etc.

{
  "migrations": [
    {
      "state": "pending",
      "source": {
        "type": "sql",
        "path": "00001_users_table.sql",
        "version": 1
      }
    }
  ],
  "has_pending": true
}

Driver and dialect automatically inferred from dbstring

We can infer the dialect based on the supplied DSN (called --dbstring).

Previously goose had the caller supply a driver name AND a dbstring, but the former is not necessary and we already know ahead of time which databases we support so we can used a handy package to map that connection string to a dialect, https://github.com/xo/dburl#database-schemes-aliases-and-drivers.

  • Can we make this backwards compatible with goose [OPTIONS] DRIVER DBSTRING COMMAND where the DRIVER AND DBSTRING are automatically converted to --dbstring?

Use a lightweight framework for composing CLI

...

Expose the CLI code to users

These 2 functions will be available to users so they can integrate goose into their own applications and get the same CLI experience. Should resolve a few requests like this one #646

// 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) {

}

// 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 {

}

github.com/sethvargo/go-retry v0.2.4
github.com/stretchr/testify v1.9.0
Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

@@ -42,6 +44,10 @@ var (
var version string

func main() {
if ok, err := strconv.ParseBool(os.Getenv("GOOSE_CLI")); err == nil && ok {
Copy link
Collaborator Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant