-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Thread context.Context for config (#75)
- Loading branch information
Showing
22 changed files
with
249 additions
and
194 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package config | ||
|
||
import ( | ||
"context" | ||
"github.com/jessevdk/go-flags" | ||
"log" | ||
) | ||
|
||
const settingsKey = "_settings" | ||
|
||
type Settings struct { | ||
Config *Config | ||
VerboseLogging bool | ||
} | ||
|
||
// InjectSettingsIntoContext is used for tests ONLY | ||
func InjectSettingsIntoContext(ctx context.Context, settings *Settings) context.Context { | ||
return context.WithValue(ctx, settingsKey, settings) | ||
} | ||
|
||
func FromContext(ctx context.Context) *Settings { | ||
settingsVal := ctx.Value(settingsKey) | ||
if settingsVal == nil { | ||
log.Fatalf("failed to grab settings from context") | ||
} | ||
|
||
settings, isOk := settingsVal.(*Settings) | ||
if !isOk { | ||
log.Fatalf("settings in context is not of *config.Settings type") | ||
} | ||
|
||
return settings | ||
} | ||
|
||
// InitializeCfgIntoContext will take the flags and then parse | ||
// loadConfig is optional for testing purposes. | ||
func InitializeCfgIntoContext(ctx context.Context, args []string, loadConfig bool) context.Context { | ||
var opts struct { | ||
ConfigFilePath string `short:"c" long:"config" description:"path to the config file"` | ||
Verbose bool `short:"v" long:"verbose" description:"debug logging" optional:"true"` | ||
} | ||
|
||
_, err := flags.ParseArgs(&opts, args) | ||
if err != nil { | ||
log.Fatalf("failed to parse args, err: %v", err) | ||
} | ||
|
||
var config *Config | ||
if loadConfig { | ||
config, err = readFileToConfig(opts.ConfigFilePath) | ||
if err != nil { | ||
log.Fatalf("failed to parse config file. Please check your config, err: %v", err) | ||
} | ||
|
||
err = config.Validate() | ||
if err != nil { | ||
log.Fatalf("Failed to validate config, err: %v", err) | ||
} | ||
} | ||
|
||
settings := &Settings{ | ||
Config: config, | ||
VerboseLogging: opts.Verbose, | ||
} | ||
|
||
return context.WithValue(ctx, settingsKey, settings) | ||
} |
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,19 @@ | ||
package config | ||
|
||
import ( | ||
"context" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestParseArgs(t *testing.T) { | ||
ctx := InitializeCfgIntoContext(context.Background(), []string{}, false) | ||
settings := FromContext(ctx) | ||
|
||
assert.Equal(t, settings.VerboseLogging, false) | ||
assert.Nil(t, settings.Config) | ||
|
||
ctx = InitializeCfgIntoContext(context.Background(), []string{"-v"}, false) | ||
settings = FromContext(ctx) | ||
assert.Equal(t, settings.VerboseLogging, true) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.