Skip to content

Commit

Permalink
fix: support SUPABASE_DB_PASSWORD env var
Browse files Browse the repository at this point in the history
  • Loading branch information
sweatybridge committed Aug 12, 2022
1 parent 7ecd108 commit 5728c8d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
21 changes: 14 additions & 7 deletions cmd/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/supabase/cli/internal/db/branch/create"
"github.com/supabase/cli/internal/db/branch/delete"
"github.com/supabase/cli/internal/db/branch/list"
Expand Down Expand Up @@ -87,6 +88,7 @@ var (
Use: "push",
Short: "Push new migrations to the remote database",
RunE: func(cmd *cobra.Command, args []string) error {
password := viper.GetString("DB_PASSWORD")
if password == "" {
password = PromptPassword(os.Stdin)
}
Expand All @@ -105,6 +107,7 @@ var (
Short: "Show changes on the remote database",
Long: "Show changes on the remote database since last migration.",
RunE: func(cmd *cobra.Command, args []string) error {
password := viper.GetString("DB_PASSWORD")
if password == "" {
password = PromptPassword(os.Stdin)
}
Expand All @@ -116,6 +119,7 @@ var (
Use: "commit",
Short: "Commit remote changes as a new migration",
RunE: func(cmd *cobra.Command, args []string) error {
password := viper.GetString("DB_PASSWORD")
if password == "" {
password = PromptPassword(os.Stdin)
}
Expand All @@ -134,28 +138,31 @@ var (
)

func init() {
// Build branch command
dbBranchCmd.AddCommand(dbBranchCreateCmd)
dbBranchCmd.AddCommand(dbBranchDeleteCmd)
dbBranchCmd.AddCommand(dbBranchListCmd)
dbBranchCmd.AddCommand(dbSwitchCmd)
dbCmd.AddCommand(dbBranchCmd)
// Build diff command
dbDiffCmd.Flags().BoolVar(&useMigra, "use-migra", false, "Use migra to generate schema diff.")
dbDiffCmd.Flags().StringVarP(&file, "file", "f", "", "Saves schema diff to a file.")
dbDiffCmd.Flags().StringSliceVarP(&schema, "schema", "s", []string{"public"}, "List of schema to include.")
dbCmd.AddCommand(dbDiffCmd)
// Build push command
pushFlags := dbPushCmd.Flags()
pushFlags.BoolVar(&dryRun, "dry-run", false, "Print the migrations that would be applied, but don't actually apply them.")
// pushFlags.StringVarP(&database, "database", "d", "postgres", "Name of your remote Postgres database.")
// pushFlags.StringVarP(&username, "username", "u", "postgres", "Username to your remote Postgres database.")
pushFlags.StringVarP(&password, "password", "p", "", "Password to your remote Postgres database.")
pushFlags.StringVarP(&dbPassword, "password", "p", "", "Password to your remote Postgres database.")
cobra.CheckErr(viper.BindPFlag("DB_PASSWORD", pushFlags.Lookup("password")))
dbCmd.AddCommand(dbPushCmd)
// Build remote command
remoteFlags := dbRemoteCmd.PersistentFlags()
remoteFlags.StringVarP(&dbPassword, "password", "p", "", "Password to your remote Postgres database.")
cobra.CheckErr(viper.BindPFlag("DB_PASSWORD", remoteFlags.Lookup("password")))
dbRemoteCmd.AddCommand(dbRemoteChangesCmd)
commitFlags := dbRemoteCommitCmd.Flags()
// commitFlags.StringVarP(&database, "database", "d", "postgres", "Name of your remote Postgres database.")
// commitFlags.StringVarP(&username, "username", "u", "postgres", "Username to your remote Postgres database.")
commitFlags.StringVarP(&password, "password", "p", "", "Password to your remote Postgres database.")
dbRemoteCmd.AddCommand(dbRemoteCommitCmd)
dbCmd.AddCommand(dbRemoteCmd)
// Buidl reset command
dbCmd.AddCommand(dbResetCmd)
rootCmd.AddCommand(dbCmd)
}
10 changes: 5 additions & 5 deletions cmd/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/supabase/cli/internal/link"
"github.com/supabase/cli/internal/utils"
"golang.org/x/term"
Expand All @@ -16,7 +17,6 @@ var (
// TODO: allow switching roles on backend
database = "postgres"
username = "postgres"
password string

linkCmd = &cobra.Command{
Use: "link",
Expand All @@ -27,6 +27,7 @@ var (
return err
}

password := viper.GetString("DB_PASSWORD")
if password == "" {
password = PromptPassword(os.Stdin)
}
Expand All @@ -46,10 +47,9 @@ var (
func init() {
flags := linkCmd.Flags()
flags.String("project-ref", "", "Project ref of the Supabase project.")
// flags.StringVarP(&database, "database", "d", "postgres", "Name of your remote Postgres database.")
// flags.StringVarP(&username, "username", "u", "postgres", "Username to your remote Postgres database.")
flags.StringVarP(&password, "password", "p", "", "Password to your remote Postgres database.")
_ = linkCmd.MarkFlagRequired("project-ref")
flags.StringVarP(&dbPassword, "password", "p", "", "Password to your remote Postgres database.")
cobra.CheckErr(viper.BindPFlag("DB_PASSWORD", flags.Lookup("password")))
cobra.CheckErr(linkCmd.MarkFlagRequired("project-ref"))
rootCmd.AddCommand(linkCmd)
}

Expand Down
6 changes: 5 additions & 1 deletion cmd/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/supabase/cli/internal/migration/list"
"github.com/supabase/cli/internal/migration/new"
)
Expand All @@ -19,6 +20,7 @@ var (
Use: "list",
Short: "List local and remote migrations",
RunE: func(cmd *cobra.Command, args []string) error {
password := viper.GetString("DB_PASSWORD")
if password == "" {
password = PromptPassword(os.Stdin)
}
Expand All @@ -37,7 +39,9 @@ var (
)

func init() {
migrationListCmd.Flags().StringVarP(&password, "password", "p", "", "Password to your remote Postgres database.")
flags := migrationListCmd.Flags()
flags.StringVarP(&dbPassword, "password", "p", "", "Password to your remote Postgres database.")
cobra.CheckErr(viper.BindPFlag("DB_PASSWORD", flags.Lookup("password")))
migrationCmd.AddCommand(migrationListCmd)
migrationCmd.AddCommand(migrationNewCmd)
rootCmd.AddCommand(migrationCmd)
Expand Down
2 changes: 2 additions & 0 deletions cmd/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/supabase/cli/internal/projects/create"
"github.com/supabase/cli/internal/projects/list"
"github.com/supabase/cli/internal/utils"
Expand Down Expand Up @@ -86,6 +87,7 @@ func init() {
createFlags.StringVar(&dbPassword, "db-password", "", "Database password of the project.")
createFlags.Var(&region, "region", "Select a region close to you for the best performance.")
createFlags.Var(&plan, "plan", "Select a plan that suits your needs.")
cobra.CheckErr(viper.BindPFlag("DB_PASSWORD", createFlags.Lookup("db-password")))
// Add commands to root
projectsCmd.AddCommand(projectsCreateCmd)
projectsCmd.AddCommand(projectsListCmd)
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func init() {
flags.Bool("debug", false, "output debug logs to stderr")
flags.VisitAll(func(f *pflag.Flag) {
key := strings.ReplaceAll(f.Name, "-", "_")
_ = viper.BindPFlag(key, flags.Lookup(f.Name))
cobra.CheckErr(viper.BindPFlag(key, flags.Lookup(f.Name)))
})
rootCmd.SetVersionTemplate("{{.Version}}\n")
}
Expand Down

0 comments on commit 5728c8d

Please sign in to comment.