jackc / tern

The SQL Fan's Migrator
MIT License
925 stars 68 forks source link

Expose CLI as an importable package #27

Closed codeclown closed 3 years ago

codeclown commented 3 years ago

I want to embed migration logic into my application so I don't need to install tern into the container separately.

I can do that by importing github.com/jackc/tern/migrate and creating a new migrator like is done in the tern CLI:

https://github.com/jackc/tern/blob/7208d820a07db608374260af329b2c07b2949d73/main.go#L411

However, I don't really need to customize that process in any way. For my purposes I'd be just copying most of func Migrate from that file.

It would be a nicer alternative if I could do this:

args := [0]string{}
tern.Migrate(cobra.Command{}, args)

In fact, my use case would be to just pass the args from my own cobra command which is powering my CLI:

func RunMigrate(cmd *cobra.Command, args []string) {
    tern.Migrate(cmd, args)
}

So I could run my-command migrate ... which would just run tern.

Would you consider supporting this and/or approving a PR?

jackc commented 3 years ago

Hmm... I see how it could be useful in some cases... But this would essentially make the entire CLI interface and all the code part of the public interface. I'd rather not commit to making that stable. It's also only useful to applications using cobra.

When I've embedded tern in an application I've usually been able to simplify the interface a bit. Also, the application probably has its own config for the database connection. So I would recommend using github.com/jackc/tern/migrate and copying whatever you need / want from func Migrate.

codeclown commented 3 years ago

I understand that turning it into a stable API might not be worth it, although it is unfortunate because I don't really want to replicate the console output logic that is already present there.

It's also only useful to applications using cobra.

Well, it could be useful to more applications than just those that use cobra, with a small refactoring of moving the meaningful logic outside to functions that don't require a Cobra command as argument.

But again, I fully understand! Closing for now.