mirror of
https://codeberg.org/shroff/phylum.git
synced 2026-05-01 01:31:01 -05:00
[server] Better database config
This commit is contained in:
@@ -3,6 +3,7 @@ package command
|
||||
import (
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/shroff/phylum/server/internal/command/fs"
|
||||
@@ -21,21 +22,29 @@ import (
|
||||
|
||||
func SetupCommand() {
|
||||
viper.SetEnvPrefix("phylum")
|
||||
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||||
viper.AutomaticEnv()
|
||||
|
||||
var rootCmd = &cobra.Command{Use: path.Base(os.Args[0])}
|
||||
flags := rootCmd.PersistentFlags()
|
||||
|
||||
flags.Bool("debug", false, "Debug mode")
|
||||
flags.MarkHidden("debug")
|
||||
viper.BindPFlag("debug", flags.Lookup("debug"))
|
||||
|
||||
flags.Bool("trace-sql", false, "Trace SQL Queries")
|
||||
viper.BindPFlag("trace_sql", flags.Lookup("trace-sql"))
|
||||
flags.Bool("trace-queries", false, "Trace Database Queries")
|
||||
flags.MarkHidden("trace-queries")
|
||||
viper.BindPFlag("db.trace_queries", flags.Lookup("trace-queries"))
|
||||
|
||||
flags.StringP("working-dir", "W", "data", "Working Directory")
|
||||
viper.BindPFlag("working_dir", flags.Lookup("working-dir"))
|
||||
|
||||
flags.String("database-url", "postgres://phylum:phylum@localhost:5432/phylum", "Database URL or DSN")
|
||||
viper.BindPFlag("database_url", flags.Lookup("database-url"))
|
||||
viper.SetDefault("db.host", "localhost")
|
||||
viper.SetDefault("db.port", "5432")
|
||||
viper.SetDefault("db.name", "phylum")
|
||||
viper.SetDefault("db.user", "phylum")
|
||||
viper.SetDefault("db.trace_queries", "false")
|
||||
viper.SetDefault("db.skip_migration", "false")
|
||||
|
||||
uuid.EnableRandPool()
|
||||
rootCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
|
||||
@@ -51,10 +60,7 @@ func SetupCommand() {
|
||||
os.Chdir(workDir)
|
||||
}
|
||||
|
||||
db.DatabaseURL = viper.GetString("database_url")
|
||||
db.TraceSQL = viper.GetBool("debug") && viper.GetBool("trace_sql")
|
||||
db.AutoMigrate = viper.GetBool("auto_migrate")
|
||||
|
||||
db.Config = viper.Sub("db")
|
||||
storage.DefaultStorageDir = viper.GetString("default_storage_dir")
|
||||
}
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ func setupSchemaResetCommand() *cobra.Command {
|
||||
Use: "reset",
|
||||
Short: "Reset Database Schema",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
db.AutoMigrate = false
|
||||
db.Config.Set("skip_migration", true)
|
||||
if err := db.DeleteSchema(context.Background()); err != nil {
|
||||
fmt.Println("unable to delete database schema: " + err.Error())
|
||||
os.Exit(1)
|
||||
@@ -49,10 +49,10 @@ func setupSchemaMigrateCommand() *cobra.Command {
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
ctx := context.Background()
|
||||
if args[0] == "auto" || args[0] == "latest" {
|
||||
db.AutoMigrate = true
|
||||
db.Config.Set("skip_migration", false)
|
||||
db.Get(ctx)
|
||||
} else {
|
||||
db.AutoMigrate = false
|
||||
db.Config.Set("skip_migration", true)
|
||||
if v, err := strconv.Atoi(args[0]); err != nil {
|
||||
fmt.Println(err.Error())
|
||||
os.Exit(3)
|
||||
|
||||
Reference in New Issue
Block a user