diff --git a/cobble.go b/cobble.go new file mode 100644 index 0000000..700054b --- /dev/null +++ b/cobble.go @@ -0,0 +1,53 @@ +package main + +import ( + "github.com/spf13/cobra" + "log" + "os" + "strconv" +) + +var cli = &cobra.Command{ + Use: "cobble", + Short: "Cobble is a simple SQL migration utility.", +} + +var up = &cobra.Command{ + Use: "up", + Short: "Applies migrations", + Run: func(cmd *cobra.Command, args []string) { + dbPort, _ := strconv.Atoi(os.Getenv("DB_PORT")) + + dbConnection, err := ConnectToDatabase(DatabaseConfiguration{ + User: os.Getenv("DB_APP_USER"), + Password: os.Getenv("DB_APP_PASSWORD"), + DatabaseName: os.Getenv("DB_NAME"), + Host: os.Getenv("DB_HOST"), + Port: dbPort, + }) + + if err != nil { + log.Printf("Failed to connect to the database: %s", err) + os.Exit(1) + } + + migrationRoot := args[0] + migrationHistory, err := getMigrations(migrationRoot) + + for _, migration := range migrationHistory { + log.Printf("%s", migration.Name) + if err = migrate(dbConnection, migration.Path); err != nil { + os.Exit(1) + } + } + }, +} + +func main() { + cli.AddCommand(up) + + if err := cli.Execute(); err != nil { + log.Fatal(err) + os.Exit(1) + } +} diff --git a/go.mod b/go.mod index 6715750..1c237d6 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,12 @@ module cobble go 1.22.2 -require github.com/lib/pq v1.10.9 +require ( + github.com/lib/pq v1.10.9 + github.com/spf13/cobra v1.8.1 +) + +require ( + github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect +) diff --git a/go.sum b/go.sum index aeddeae..c985bb1 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,12 @@ +github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= +github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/migrate.go b/migrate.go index c4216ed..c37492b 100644 --- a/migrate.go +++ b/migrate.go @@ -14,7 +14,6 @@ import ( "log" "os" "path/filepath" - "strconv" ) var commentPrefix = []byte("--") @@ -132,30 +131,3 @@ func ConnectToDatabase(config DatabaseConfiguration) (*sql.DB, error) { connectionString := fmt.Sprintf("postgresql://%s:%s@%s:%d?sslmode=disable", config.User, config.Password, config.Host, config.Port) return sql.Open(config.DatabaseName, connectionString) } - -func main() { - dbPort, _ := strconv.Atoi(os.Getenv("DB_PORT")) - - dbConnection, err := ConnectToDatabase(DatabaseConfiguration{ - User: os.Getenv("DB_APP_USER"), - Password: os.Getenv("DB_APP_PASSWORD"), - DatabaseName: os.Getenv("DB_NAME"), - Host: os.Getenv("DB_HOST"), - Port: dbPort, - }) - - if err != nil { - log.Printf("Failed to connect to the database: %s", err) - os.Exit(1) - } - - migrationRoot := os.Args[1] - migrationHistory, err := getMigrations(migrationRoot) - - for _, migration := range migrationHistory { - log.Printf("%s", migration.Name) - if err = migrate(dbConnection, migration.Path); err != nil { - os.Exit(1) - } - } -}