This repository has been archived by the owner on Aug 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement TOML-based configuration Resolves #22
- Loading branch information
Showing
52 changed files
with
4,347 additions
and
369 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,46 @@ | ||
package config | ||
|
||
import "errors" | ||
|
||
// DebugUserConfig defines the API debug user configurations | ||
type DebugUserConfig struct { | ||
Mode DebugUserMode | ||
Username string | ||
Password string | ||
} | ||
|
||
// Prepares sets defaults and validates the configurations | ||
func (conf *DebugUserConfig) Prepares(mode Mode) error { | ||
// Set default debug user mode | ||
if conf.Mode == DebugUserUnset { | ||
switch mode { | ||
case ModeProduction: | ||
conf.Mode = DebugUserDisabled | ||
case ModeBeta: | ||
conf.Mode = DebugUserReadOnly | ||
default: | ||
conf.Mode = DebugUserRW | ||
} | ||
} | ||
|
||
// Use "debug" as the default debug username | ||
if conf.Username == "" { | ||
conf.Username = "debug" | ||
} | ||
|
||
// Use "debug" as the default debug password | ||
if conf.Password == "" { | ||
conf.Password = "debug" | ||
} | ||
|
||
// VALIDATE | ||
|
||
// Ensure the debug user isn't enabled in production mode | ||
if mode == ModeProduction { | ||
if conf.Mode != DebugUserDisabled { | ||
return errors.New("debug user must be disabled in production mode") | ||
} | ||
} | ||
|
||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package config | ||
|
||
import "fmt" | ||
|
||
// DebugUserMode represents a debug user mode | ||
type DebugUserMode string | ||
|
||
const ( | ||
// DebugUserUnset represents the default unset option value | ||
DebugUserUnset DebugUserMode = "" | ||
|
||
// DebugUserDisabled disables the debug user | ||
DebugUserDisabled DebugUserMode = "disabled" | ||
|
||
// DebugUserReadOnly enables the debug user in a read-only mode | ||
DebugUserReadOnly DebugUserMode = "read-only" | ||
|
||
// DebugUserRW enables the debug user in a read-write mode | ||
DebugUserRW DebugUserMode = "read-write" | ||
) | ||
|
||
// Validate returns an error if the value is invalid | ||
func (md DebugUserMode) Validate() error { | ||
switch md { | ||
case DebugUserUnset: | ||
fallthrough | ||
case DebugUserDisabled: | ||
fallthrough | ||
case DebugUserRW: | ||
fallthrough | ||
case DebugUserReadOnly: | ||
return nil | ||
} | ||
return fmt.Errorf("invalid debug user mode: '%s'", md) | ||
} |
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,26 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"time" | ||
) | ||
|
||
// Duration represents a duration | ||
type Duration time.Duration | ||
|
||
// UnmarshalTOML implements the TOML unmarshaler interface | ||
func (v *Duration) UnmarshalTOML(val interface{}) error { | ||
if str, isString := val.(string); isString { | ||
dur, err := time.ParseDuration(str) | ||
if err != nil { | ||
return err | ||
} | ||
*v = Duration(dur) | ||
return nil | ||
} | ||
return fmt.Errorf( | ||
"unexpected duration value type: %s", | ||
reflect.TypeOf(val), | ||
) | ||
} |
Oops, something went wrong.