summaryrefslogtreecommitdiff
path: root/internal/cli/root.go
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-03-03 23:53:25 +0200
committerPaul Buetow <paul@buetow.org>2026-03-03 23:53:25 +0200
commit09b8aa9d74df0ef7d50858cd398efc8a952a1aad (patch)
tree66b77086b1f55c349d7edb32635a52566ca70c8d /internal/cli/root.go
parente785973bc3a0add31a7ff68a94b9ea5d43413b59 (diff)
cli: thread config via command context
Diffstat (limited to 'internal/cli/root.go')
-rw-r--r--internal/cli/root.go31
1 files changed, 26 insertions, 5 deletions
diff --git a/internal/cli/root.go b/internal/cli/root.go
index 11dabc5..faba592 100644
--- a/internal/cli/root.go
+++ b/internal/cli/root.go
@@ -1,6 +1,7 @@
package cli
import (
+ "context"
"fmt"
timr "codeberg.org/snonux/timr/internal"
@@ -8,7 +9,7 @@ import (
"github.com/spf13/cobra"
)
-var loadedConfig = config.Default()
+type configContextKey struct{}
// Execute runs the root command.
func Execute() error {
@@ -34,7 +35,7 @@ func NewRootCmd() *cobra.Command {
return fmt.Errorf("load config: %w", err)
}
- loadedConfig = cfg
+ setConfig(cmd, cfg)
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
@@ -56,7 +57,27 @@ func NewRootCmd() *cobra.Command {
return cmd
}
-// CurrentConfig returns the config loaded in PersistentPreRunE.
-func CurrentConfig() config.Config {
- return loadedConfig
+func setConfig(cmd *cobra.Command, cfg config.Config) {
+ baseContext := cmd.Context()
+ if baseContext == nil {
+ baseContext = context.Background()
+ }
+ cmd.SetContext(context.WithValue(baseContext, configContextKey{}, cfg))
+}
+
+func currentConfig(cmd *cobra.Command) config.Config {
+ if cmd == nil {
+ return config.Default()
+ }
+
+ commandContext := cmd.Context()
+ if commandContext == nil {
+ return config.Default()
+ }
+
+ cfg, ok := commandContext.Value(configContextKey{}).(config.Config)
+ if !ok {
+ return config.Default()
+ }
+ return cfg
}