Skip to content
This repository was archived by the owner on Feb 5, 2026. It is now read-only.

Commit f9cf874

Browse files
authored
refactor: add config status enum for adding validation in the future (#71)
* feat: add config status for future validation * refactor: use status enum and switch
1 parent 44b2a85 commit f9cf874

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

cmd/ajisai/root.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ func Run(args []string) error {
5353
return config.StoreNotFoundInContext(ctx), nil
5454
}
5555

56-
return ctx, fmt.Errorf("failed to load configuration from %s: %w", cfgPath, err)
56+
// TODO: add validation and distinguish between validation errors and other errors.
57+
return config.StoreValidationErrorInContext(ctx, err), nil
5758
}
5859

5960
return config.StoreInContext(ctx, loadedCfg), nil
@@ -114,8 +115,14 @@ func doApply(c context.Context, _ *cli.Command) error {
114115
return fmt.Errorf("failed to retrieve config from context: %w", err)
115116
}
116117

117-
if cfgCtx.NotFound {
118+
switch cfgCtx.Status {
119+
case config.StatusValid:
120+
// No action needed.
121+
// But we include it for use exhaustive linter.
122+
case config.StatusNotFound:
118123
return errors.New("apply command requires an existing config file")
124+
case config.StatusValidationFailed:
125+
return cfgCtx.ValidationError
119126
}
120127

121128
cfg := cfgCtx.Config
@@ -145,8 +152,14 @@ func doClean(c context.Context, cmd *cli.Command) error {
145152
return fmt.Errorf("failed to retrieve config from context: %w", err)
146153
}
147154

148-
if cfgCtx.NotFound {
155+
switch cfgCtx.Status {
156+
case config.StatusValid:
157+
// No action needed.
158+
// But we include it for use exhaustive linter.
159+
case config.StatusNotFound:
149160
return errors.New("clean command requires an existing config file")
161+
case config.StatusValidationFailed:
162+
return cfgCtx.ValidationError
150163
}
151164

152165
cfg := cfgCtx.Config

internal/config/context.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,54 @@ import (
88
type (
99
contextKey string
1010

11+
// Status represents the status of a configuration.
12+
Status int
13+
1114
Context struct {
1215
Config *Config
1316

14-
NotFound bool
17+
// Status represents the state of the configuration.
18+
Status Status
19+
20+
// ValidationError holds the validation error if Status is StatusValidationFailed.
21+
ValidationError error
1522
}
1623
)
1724

1825
const (
1926
configContextKey contextKey = "config"
2027
)
2128

29+
const (
30+
// StatusValid indicates the configuration is valid and ready to use.
31+
StatusValid Status = iota
32+
33+
// StatusNotFound indicates the configuration file was not found.
34+
StatusNotFound
35+
36+
// StatusValidationFailed indicates the configuration failed validation.
37+
StatusValidationFailed
38+
39+
// Add more status types here as needed...
40+
)
41+
2242
func StoreInContext(ctx context.Context, cfg *Config) context.Context {
2343
return context.WithValue(ctx, configContextKey, &Context{
2444
Config: cfg,
45+
Status: StatusValid,
2546
})
2647
}
2748

2849
func StoreNotFoundInContext(ctx context.Context) context.Context {
2950
return context.WithValue(ctx, configContextKey, &Context{
30-
NotFound: true,
51+
Status: StatusNotFound,
52+
})
53+
}
54+
55+
func StoreValidationErrorInContext(ctx context.Context, validationError error) context.Context {
56+
return context.WithValue(ctx, configContextKey, &Context{
57+
Status: StatusValidationFailed,
58+
ValidationError: validationError,
3159
})
3260
}
3361

0 commit comments

Comments
 (0)