feat: extract cli and add parsing for up command
This commit is contained in:
parent
be782fb9c2
commit
369fd41e6d
4 changed files with 72 additions and 29 deletions
53
cobble.go
Normal file
53
cobble.go
Normal file
|
@ -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)
|
||||
}
|
||||
}
|
10
go.mod
10
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
|
||||
)
|
||||
|
|
10
go.sum
10
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=
|
||||
|
|
28
migrate.go
28
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue