-
-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move cron scheduling inside application (#338)
* Move cron scheduling inside application * Make envvar a fallback and check for errors * Panic significantly less * propagate error out of runBackup * Add structured logging * FIx error propagation to exit * Enable the new scheduler by default * Review fixes * Added docs and better error propagation
- Loading branch information
Showing
8 changed files
with
218 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright 2021-2022 - Offen Authors <[email protected]> | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/joho/godotenv" | ||
"github.com/offen/envconfig" | ||
) | ||
|
||
// envProxy is a function that mimics os.LookupEnv but can read values from any other source | ||
type envProxy func(string) (string, bool) | ||
|
||
func loadConfig(lookup envProxy) (*Config, error) { | ||
envconfig.Lookup = func(key string) (string, bool) { | ||
value, okValue := lookup(key) | ||
location, okFile := lookup(key + "_FILE") | ||
|
||
switch { | ||
case okValue && !okFile: // only value | ||
return value, true | ||
case !okValue && okFile: // only file | ||
contents, err := os.ReadFile(location) | ||
if err != nil { | ||
return "", false | ||
} | ||
return string(contents), true | ||
case okValue && okFile: // both | ||
return "", false | ||
default: // neither, ignore | ||
return "", false | ||
} | ||
} | ||
|
||
var c = &Config{} | ||
if err := envconfig.Process("", c); err != nil { | ||
return nil, fmt.Errorf("failed to process configuration values, error: %w", err) | ||
} | ||
|
||
return c, nil | ||
} | ||
|
||
func loadEnvVars() (*Config, error) { | ||
return loadConfig(os.LookupEnv) | ||
} | ||
|
||
func loadEnvFiles(directory string) ([]*Config, error) { | ||
items, err := os.ReadDir(directory) | ||
if err != nil { | ||
if os.IsNotExist(err) { | ||
return nil, err | ||
} | ||
return nil, fmt.Errorf("failed to read files from env directory, error: %w", err) | ||
} | ||
|
||
var cs = make([]*Config, 0) | ||
for _, item := range items { | ||
if !item.IsDir() { | ||
p := filepath.Join(directory, item.Name()) | ||
envFile, err := godotenv.Read(p) | ||
if err != nil { | ||
return nil, fmt.Errorf("error reading config file %s, error: %w", p, err) | ||
} | ||
lookup := func(key string) (string, bool) { | ||
val, ok := envFile[key] | ||
return val, ok | ||
} | ||
c, err := loadConfig(lookup) | ||
if err != nil { | ||
return nil, fmt.Errorf("error loading config from file %s, error: %w", p, err) | ||
} | ||
cs = append(cs, c) | ||
} | ||
} | ||
|
||
return cs, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters