this repo has no description

use cli.context for envvars

Hailey ef2e26cd bf1a93fc

Changed files
+8 -7
cmd
cocoon
+8 -7
cmd/cocoon/main.go
··· 47 }, 48 &cli.StringFlag{ 49 Name: "database-url", 50 Usage: "PostgreSQL connection string (required if db-type is postgres)", 51 EnvVars: []string{"COCOON_DATABASE_URL", "DATABASE_URL"}, 52 }, ··· 293 }, 294 }, 295 Action: func(cmd *cli.Context) error { 296 - db, err := newDb() 297 if err != nil { 298 return err 299 } ··· 332 }, 333 }, 334 Action: func(cmd *cli.Context) error { 335 - db, err := newDb() 336 if err != nil { 337 return err 338 } ··· 359 }, 360 } 361 362 - func newDb() (*gorm.DB, error) { 363 - dbType := os.Getenv("COCOON_DB_TYPE") 364 if dbType == "" { 365 dbType = "sqlite" 366 } 367 368 switch dbType { 369 case "postgres": 370 - databaseURL := os.Getenv("COCOON_DATABASE_URL") 371 if databaseURL == "" { 372 - databaseURL = os.Getenv("DATABASE_URL") 373 } 374 if databaseURL == "" { 375 return nil, fmt.Errorf("COCOON_DATABASE_URL or DATABASE_URL must be set when using postgres") 376 } 377 return gorm.Open(postgres.Open(databaseURL), &gorm.Config{}) 378 default: 379 - dbName := os.Getenv("COCOON_DB_NAME") 380 if dbName == "" { 381 dbName = "cocoon.db" 382 }
··· 47 }, 48 &cli.StringFlag{ 49 Name: "database-url", 50 + Aliases: []string{"db-url"}, 51 Usage: "PostgreSQL connection string (required if db-type is postgres)", 52 EnvVars: []string{"COCOON_DATABASE_URL", "DATABASE_URL"}, 53 }, ··· 294 }, 295 }, 296 Action: func(cmd *cli.Context) error { 297 + db, err := newDb(cmd) 298 if err != nil { 299 return err 300 } ··· 333 }, 334 }, 335 Action: func(cmd *cli.Context) error { 336 + db, err := newDb(cmd) 337 if err != nil { 338 return err 339 } ··· 360 }, 361 } 362 363 + func newDb(cmd *cli.Context) (*gorm.DB, error) { 364 + dbType := cmd.String("db-type") 365 if dbType == "" { 366 dbType = "sqlite" 367 } 368 369 switch dbType { 370 case "postgres": 371 + databaseURL := cmd.String("database-url") 372 if databaseURL == "" { 373 + databaseURL = cmd.String("database-url") 374 } 375 if databaseURL == "" { 376 return nil, fmt.Errorf("COCOON_DATABASE_URL or DATABASE_URL must be set when using postgres") 377 } 378 return gorm.Open(postgres.Open(databaseURL), &gorm.Config{}) 379 default: 380 + dbName := cmd.String("db-name") 381 if dbName == "" { 382 dbName = "cocoon.db" 383 }