Skip to content

Commit

Permalink
Add TERN_CONFIG and TERN_MIGRATIONS environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
jackc committed Jul 23, 2022
1 parent c78eac0 commit 4bdf795
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
5 changes: 5 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ as test, development, and production in several ways.
* Program arguments for database settings and optionally one config file for
shared settings

In addition to program arguments, `TERN_CONFIG` and `TERN_MIGRATIONS`
environment variables may be used to set the config path and migrations path
respectively.

## Migrations

To create a new migration:
Expand Down Expand Up @@ -386,6 +390,7 @@ is still available through RubyGems and the source code is on the ruby branch.
* Replace MigratorFS interface with fs.FS
* Upgrade to pgx v5
* Upgrade to sprig v3
* Add TERN_CONFIG and TERN_MIGRATIONS environment variables

## 1.13.0 (April 21, 2022)

Expand Down
27 changes: 21 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ The word "last":
Args: cobra.ExactArgs(1),
Run: SnapshotCode,
}
cmdCodeSnapshot.Flags().StringVarP(&cliOptions.migrationsPath, "migrations", "m", ".", "migrations path")
cmdCodeSnapshot.Flags().StringVarP(&cliOptions.migrationsPath, "migrations", "m", "", "migrations path (default is .)")

cmdStatus := &cobra.Command{
Use: "status",
Expand All @@ -256,7 +256,7 @@ The word "last":
Long: "Generate a new migration with the next sequence number and provided name",
Run: NewMigration,
}
cmdNew.Flags().StringVarP(&cliOptions.migrationsPath, "migrations", "m", ".", "migrations path")
cmdNew.Flags().StringVarP(&cliOptions.migrationsPath, "migrations", "m", "", "migrations path (default is .)")

cmdRenumber := &cobra.Command{
Use: "renumber COMMAND",
Expand All @@ -269,7 +269,7 @@ The word "last":
Long: "Start renumbering with the current migration numbering preserved",
Run: RenumberStart,
}
cmdRenumberStart.Flags().StringVarP(&cliOptions.migrationsPath, "migrations", "m", ".", "migrations path")
cmdRenumberStart.Flags().StringVarP(&cliOptions.migrationsPath, "migrations", "m", "", "migrations path (default is .)")

cmdRenumberFinish := &cobra.Command{
Use: "finish",
Expand All @@ -278,7 +278,7 @@ The word "last":

Run: RenumberFinish,
}
cmdRenumberFinish.Flags().StringVarP(&cliOptions.migrationsPath, "migrations", "m", ".", "migrations path")
cmdRenumberFinish.Flags().StringVarP(&cliOptions.migrationsPath, "migrations", "m", "", "migrations path (default is .)")

cmdVersion := &cobra.Command{
Use: "version",
Expand Down Expand Up @@ -326,7 +326,7 @@ func addCoreConfigFlagsToCommand(cmd *cobra.Command) {
}

func addConfigFlagsToCommand(cmd *cobra.Command) {
cmd.Flags().StringVarP(&cliOptions.migrationsPath, "migrations", "m", ".", "migrations path")
cmd.Flags().StringVarP(&cliOptions.migrationsPath, "migrations", "m", "", "migrations path (default is .)")
addCoreConfigFlagsToCommand(cmd)
}

Expand Down Expand Up @@ -851,9 +851,14 @@ func LoadConfig() (*Config, error) {
} else {
return nil, err
}
// If no config path was set in CLI argument look in environment.
if cliOptions.configPath == "" {
cliOptions.configPath = os.Getenv("TERN_CONFIG")
}

// Set default config path only if it exists
// If no config path was set in CLI or environment try default location.
if cliOptions.configPath == "" {
// Set default config path only if file exists.
if _, err := os.Stat("./tern.conf"); err == nil {
cliOptions.configPath = "./tern.conf"
}
Expand All @@ -866,6 +871,16 @@ func LoadConfig() (*Config, error) {
}
}

// If no migrations path was set in CLI argument look in environment.
if cliOptions.migrationsPath == "" {
cliOptions.migrationsPath = os.Getenv("TERN_MIGRATIONS")
}

// If no migrations path was set in CLI argument or environment use default.
if cliOptions.migrationsPath == "" {
cliOptions.migrationsPath = "."
}

err := appendConfigFromCLIArgs(config)
if err != nil {
return nil, err
Expand Down

0 comments on commit 4bdf795

Please sign in to comment.