-
Notifications
You must be signed in to change notification settings - Fork 14
feat(config): add Validate() method for startup validation #2763
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
Changes from 2 commits
f183c2f
e56ab82
35bc20e
962a95b
f635813
4be0c65
9bcf488
c17748f
665eb44
dd2bd62
ef87d7f
cb45d3b
b61c234
cfc226f
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 |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| // Copyright 2026 Blink Labs Software | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/blinklabs-io/dingo/chainsync" | ||
| "github.com/blinklabs-io/dingo/internal/config" | ||
| ) | ||
|
|
||
| // TestChainsyncStrategyWhitelistParity guards against drift between the | ||
| // chainsync.strategy values internal/config.Validate accepts and those | ||
| // chainsync.ParseHeaderSyncStrategy actually accepts. internal/config | ||
| // cannot import chainsync without pulling node subsystems into the | ||
| // config package, so this parity check lives in cmd/dingo, which can | ||
| // import both. | ||
| func TestChainsyncStrategyWhitelistParity(t *testing.T) { | ||
| for _, strategy := range config.AcceptedChainsyncStrategies { | ||
| if _, err := chainsync.ParseHeaderSyncStrategy(strategy); err != nil { | ||
| t.Errorf( | ||
| "config accepts chainsync.strategy %q but "+ | ||
| "ParseHeaderSyncStrategy rejects it: %v", | ||
| strategy, err, | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // TestMithrilBackendWhitelistParity guards against drift between the | ||
| // mithril.backend values internal/config.Validate accepts and those | ||
| // resolveMithrilBackend actually accepts. | ||
| func TestMithrilBackendWhitelistParity(t *testing.T) { | ||
| for _, backend := range config.AcceptedMithrilBackends { | ||
| if _, err := resolveMithrilBackend(backend); err != nil { | ||
| t.Errorf( | ||
| "config accepts mithril.backend %q but "+ | ||
| "resolveMithrilBackend rejects it: %v", | ||
| backend, err, | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -307,12 +307,8 @@ DSN Override: | |
| // When no subcommand given, check RunMode from config | ||
| switch cfg.RunMode { | ||
| case config.RunModeLoad: | ||
| if cfg.ImmutableDbPath == "" { | ||
| slog.Error( | ||
| "immutableDbPath must be set when runMode is 'load'", | ||
| ) | ||
| os.Exit(1) | ||
| } | ||
| // Validate() has already enforced that ImmutableDbPath | ||
| // is set for load mode | ||
| loadRun(cmd.Context(), []string{cfg.ImmutableDbPath}, cfg) | ||
| case config.RunModeServe, config.RunModeDev, config.RunModeLeios: | ||
| // serve, dev, and leios modes all run the server | ||
|
|
@@ -357,6 +353,24 @@ DSN Override: | |
| return fmt.Errorf("applying CLI flags: %w", err) | ||
| } | ||
|
|
||
| // `dingo load <path>`: the positional argument is the | ||
| // highest-precedence source for ImmutableDbPath; merge it before | ||
| // validation so a config with runMode "load" and no | ||
| // immutableDbPath doesn't fail spuriously. | ||
| if cmd.Name() == "load" && len(args) > 0 { | ||
|
Member
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 only copies the positional path into ImmutableDbPath; it does not make validation treat the explicit |
||
| cfg.ImmutableDbPath = args[0] | ||
| } | ||
|
|
||
| // version and list are informational and start no services, so | ||
| // they must still run even when the merged config is invalid. | ||
| switch cmd.Name() { | ||
| case "version", "list": | ||
| default: | ||
| if err := cfg.Validate(); err != nil { | ||
| return fmt.Errorf("invalid configuration: %w", err) | ||
| } | ||
| } | ||
|
|
||
| cmd.SetContext(config.WithContext(cmd.Context(), cfg)) | ||
| return nil | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.