-
Notifications
You must be signed in to change notification settings - Fork 2
Refactor/architecture optimization #8
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1 +1 @@ | ||
| 1.7.1 | ||
| 1.8.2 |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,143 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "gopkg.in/yaml.v3" | ||
| ) | ||
|
|
||
| // ConfigSource represents a configuration input that can mutate the manager state. | ||
| type ConfigSource interface { | ||
| Apply(*ConfigManager) error | ||
| } | ||
|
|
||
| // ConfigSourceFunc allows plain functions to be used as ConfigSource. | ||
| type ConfigSourceFunc func(*ConfigManager) error | ||
|
|
||
| // Apply executes the underlying function. | ||
| func (f ConfigSourceFunc) Apply(cm *ConfigManager) error { return f(cm) } | ||
|
|
||
| // YAMLFileSource loads configuration values from a YAML file. | ||
| type YAMLFileSource struct { | ||
| Path string | ||
| } | ||
|
|
||
| // Apply reads and merges YAML content into the manager. | ||
| func (s YAMLFileSource) Apply(cm *ConfigManager) error { | ||
| if strings.TrimSpace(s.Path) == "" { | ||
| return errors.New("config: YAML path is empty") | ||
| } | ||
|
|
||
| data, err := os.ReadFile(s.Path) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| var fileCfg ConfigManager | ||
| if err := yaml.Unmarshal(data, &fileCfg); err != nil { | ||
| return fmt.Errorf("config: unmarshal %s: %w", s.Path, err) | ||
| } | ||
|
|
||
| cm.mergeConfigModules(&fileCfg) | ||
| cm.mergeUserConfig(fileCfg.User) | ||
| cm.mergeSimpleFields(&fileCfg) | ||
| return nil | ||
| } | ||
|
|
||
| type envOverride struct { | ||
| key string | ||
| apply func(string, *ConfigManager) error | ||
| } | ||
|
|
||
| // EnvSource mutates configuration using environment variables. | ||
| type EnvSource struct { | ||
| overrides []envOverride | ||
| lookup func(string) string | ||
| } | ||
|
|
||
| // NewDefaultEnvSource returns the built-in environment overrides. | ||
| func NewDefaultEnvSource() EnvSource { | ||
| return EnvSource{ | ||
| overrides: []envOverride{ | ||
| {key: "PORT", apply: applyPortOverride}, | ||
| {key: "DATA_PATH", apply: applyDataPathOverride}, | ||
| {key: "ENABLE_MCP_SERVER", apply: applyMCPEnabledOverride}, | ||
| {key: "MCP_PORT", apply: applyMCPPortOverride}, | ||
| {key: "MCP_HOST", apply: applyMCPHostOverride}, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| // Apply applies every configured override. | ||
| func (s EnvSource) Apply(cm *ConfigManager) error { | ||
| lookup := s.lookup | ||
| if lookup == nil { | ||
| lookup = os.Getenv | ||
| } | ||
|
|
||
| var errs []error | ||
| for _, override := range s.overrides { | ||
| if value := lookup(override.key); value != "" { | ||
| if err := override.apply(value, cm); err != nil { | ||
| errs = append(errs, fmt.Errorf("%s: %w", override.key, err)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return errors.Join(errs...) | ||
| } | ||
|
|
||
| func applyPortOverride(val string, cm *ConfigManager) error { | ||
| port, err := strconv.Atoi(val) | ||
| if err != nil { | ||
| return fmt.Errorf("invalid port %q", val) | ||
| } | ||
| cm.Base.Port = port | ||
| return nil | ||
| } | ||
|
|
||
| func applyDataPathOverride(val string, cm *ConfigManager) error { | ||
| if strings.TrimSpace(val) == "" { | ||
| return errors.New("data path cannot be blank") | ||
| } | ||
| cm.Base.DataPath = val | ||
| return nil | ||
| } | ||
|
|
||
| func applyMCPEnabledOverride(val string, cm *ConfigManager) error { | ||
| enabled, err := parseBool(val) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if enabled { | ||
| cm.MCP.EnableMCPServer = 1 | ||
| } else { | ||
| cm.MCP.EnableMCPServer = 0 | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func applyMCPPortOverride(val string, cm *ConfigManager) error { | ||
| cm.MCP.MCPPort = val | ||
| return nil | ||
| } | ||
|
|
||
| func applyMCPHostOverride(val string, cm *ConfigManager) error { | ||
| cm.MCP.MCPHost = val | ||
| return nil | ||
| } | ||
|
|
||
| func parseBool(val string) (bool, error) { | ||
| switch strings.ToLower(strings.TrimSpace(val)) { | ||
| case "1", "true", "t", "yes", "y": | ||
| return true, nil | ||
| case "0", "false", "f", "no", "n": | ||
| return false, nil | ||
| default: | ||
| return false, fmt.Errorf("invalid boolean value %q", val) | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This custom boolean parsing duplicates functionality available in Go's standard library. Consider using
strconv.ParseBool()which is more standard and well-tested, or clearly document why this custom implementation with additional accepted values ('y', 'n', 't', 'f') is necessary.