Skip to content

Commit

Permalink
cmd: Adds ability to specify db url via env var in migrate
Browse files Browse the repository at this point in the history
  • Loading branch information
arekkas authored and arekkas committed Jun 14, 2018
1 parent 25e0dee commit b2cc5d2
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions cmd/migrate_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,31 @@ import (

"github.com/ory/oathkeeper/rule"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// sqlCmd represents the sql command
var sqlCmd = &cobra.Command{
Use: "sql <database-url>",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
fmt.Println(cmd.UsageString())
return
var dburl string
if readFromEnv, _ := cmd.Flags().GetBool("read-from-env"); readFromEnv {
if len(viper.GetString("DATABASE_URL")) == 0 {
fmt.Println(cmd.UsageString())
fmt.Println("")
fmt.Println("When using flag -e, environment variable DATABASE_URL must be set")
return
}
dburl = viper.GetString("DATABASE_URL")
} else {
if len(args) != 1 {
fmt.Println(cmd.UsageString())
return
}
dburl = args[0]
}

db, err := connectToSql(args[0])
db, err := connectToSql(dburl)
if err != nil {
fmt.Printf("Could not connect to database because %s.\n", err)
os.Exit(1)
Expand Down Expand Up @@ -64,4 +77,5 @@ func init() {
// is called directly, e.g.:
// sqlCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")

sqlCmd.Flags().BoolP("read-from-env", "e", false, "If set, reads the database URL from the environment variable DATABASE_URL.")
}

0 comments on commit b2cc5d2

Please sign in to comment.