-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support go embed #164
Support go embed #164
Changes from all commits
d18189d
9ef1384
f07f3d9
1f7f7dc
2565f41
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
|
||
|
@@ -25,7 +27,7 @@ func NewService(logger log.Logger) Service { | |
|
||
func (s *Service) Load(config interface{}) error { | ||
if err := s.LoadFile(pkger.Include("/configs/config.default.yml"), config); err != nil { | ||
return err | ||
s.logger.LogErrorf("loading default config file using pkger: %w", err) | ||
} | ||
|
||
if err := LoadEnvironmentFile(s.logger, APP_CONFIG, config); err != nil { | ||
|
@@ -45,12 +47,29 @@ func (s *Service) LoadFile(file string, config interface{}) error { | |
|
||
f, err := pkger.Open(file) | ||
if err != nil { | ||
return logger.LogErrorf("pkger unable to load %s: %w", file, err).Err() | ||
return fmt.Errorf("pkger unable to load %s: %w", file, err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use our logger. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It duplicates information in logs. I changed it as I saw the output and was duplicating the same errors... |
||
} | ||
|
||
deflt := viper.New() | ||
deflt.SetConfigType("yaml") | ||
if err := deflt.ReadConfig(f); err != nil { | ||
return fmt.Errorf("unable to load the defaults: %w", err) | ||
} | ||
|
||
if err := deflt.Unmarshal(config); err != nil { | ||
return fmt.Errorf("unable to unmarshal the defaults: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s *Service) LoadFromReader(in io.Reader, config interface{}) error { | ||
logger := s.logger | ||
logger.Info().Logf("loading config file from reader") | ||
|
||
deflt := viper.New() | ||
deflt.SetConfigType("yaml") | ||
if err := deflt.ReadConfig(in); err != nil { | ||
return logger.LogErrorf("unable to load the defaults: %w", err).Err() | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package config_test | ||
|
||
import ( | ||
"embed" | ||
"os" | ||
"testing" | ||
|
||
|
@@ -17,6 +18,7 @@ type ConfigModel struct { | |
Default string | ||
App string | ||
Secret string | ||
Custom string | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whats this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to test that we read a custom field from a custom file. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
} | ||
|
||
func Test_Load(t *testing.T) { | ||
|
@@ -37,3 +39,20 @@ func Test_Load(t *testing.T) { | |
require.Equal(t, "app", cfg.Config.App) | ||
require.Equal(t, "keep secret!", cfg.Config.Secret) | ||
} | ||
|
||
//go:embed testdata/*.yml | ||
var configs embed.FS | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This PR only adds There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeap. embed works for the package directory or subdirectories. Here I'm using this for tests. templater has separate packages |
||
|
||
func Test_LoadFile(t *testing.T) { | ||
cfg := &GlobalConfigModel{} | ||
|
||
service := config.NewService(log.NewDefaultLogger()) | ||
|
||
file, err := configs.Open("testdata/config.yml") | ||
require.NoError(t, err) | ||
|
||
err = service.LoadFromReader(file, cfg) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, "custom", cfg.Config.Custom) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Config: | ||
Custom: "custom" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ import ( | |
|
||
var migrationMutex sync.Mutex | ||
|
||
func RunMigrations(logger log.Logger, config DatabaseConfig) error { | ||
func RunMigrations(logger log.Logger, config DatabaseConfig, migrationsSource source.Driver) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we want to keep |
||
db, err := New(context.Background(), logger, config) | ||
if err != nil { | ||
return err | ||
|
@@ -31,9 +31,13 @@ func RunMigrations(logger log.Logger, config DatabaseConfig) error { | |
return err | ||
} | ||
|
||
if migrationsSource != nil { | ||
source = migrationsSource | ||
} | ||
|
||
migrationMutex.Lock() | ||
m, err := migrate.NewWithInstance( | ||
"filtering-pkger", | ||
"migrations", | ||
source, | ||
config.DatabaseName, | ||
driver, | ||
|
@@ -42,7 +46,18 @@ func RunMigrations(logger log.Logger, config DatabaseConfig) error { | |
return logger.Fatal().LogErrorf("Error running migration: %w", err).Err() | ||
} | ||
|
||
ver, _, err := m.Version() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why was this added in? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Based on current output I had no idea what was the version before migration and what's the version after. It was not clear for me if migrations run at all. That's why I decided to add some information to migrations. |
||
switch { | ||
case err == migrate.ErrNilVersion: | ||
logger.Info().Log("Clean DB, no migrations") | ||
case err != nil: | ||
return logger.Fatal().LogErrorf("Error getting current migration version: %w", err).Err() | ||
default: | ||
logger.Info().Logf("DB is at version: %d", ver) | ||
} | ||
|
||
err = m.Up() | ||
|
||
migrationMutex.Unlock() | ||
|
||
switch err { | ||
|
@@ -53,6 +68,16 @@ func RunMigrations(logger log.Logger, config DatabaseConfig) error { | |
return logger.Fatal().LogErrorf("Error running migrations: %w", err).Err() | ||
} | ||
|
||
ver, _, err = m.Version() | ||
switch { | ||
case err == migrate.ErrNilVersion: | ||
logger.Info().Log("Clean DB, no migrations") | ||
case err != nil: | ||
return logger.Fatal().LogErrorf("Error getting current migration version: %w", err).Err() | ||
default: | ||
logger.Info().Logf("DB migrated to version: %d", ver) | ||
} | ||
|
||
logger.Info().Log("Migrations complete") | ||
|
||
return nil | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module github.com/moov-io/base | ||
|
||
go 1.13 | ||
go 1.16 | ||
|
||
require ( | ||
github.com/go-kit/kit v0.10.0 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not returning the error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeap. My idea here, if we want to keep
pkger
... for backward compatibility?, just to ignore the error. And log it instead. I would prefer to get rid ofpkger
then we don't need it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If its going embedded completely we don't need it??? because the next compile should bring in the default. If it doesn't work that way we can't use the
embed
tooling yet then and have to re-do how we do configs.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have to re-do how we do configs as we now have to pass file (or its content) outside of the config package.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It sounds like moovfinancial needs a config package to address the needs of those services, so moov-io/base might not be the best location for these changes.