Skip to content

Commit 1381ad8

Browse files
authored
Update execution context file modification to match config file loader (#79)
* Update execution context file modification to match cfg loader etc. * Ensure execution config is OK with new items in a new file * Change for Get/Upsert
1 parent f301e8b commit 1381ad8

File tree

17 files changed

+542
-135
lines changed

17 files changed

+542
-135
lines changed

cmd/config/args/clear.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,19 @@ import (
1414

1515
type ClearCmd struct {
1616
*cmd.BaseCmd
17-
Force bool
17+
Force bool
18+
ctxLoader context.Loader
1819
}
1920

20-
func NewClearCmd(baseCmd *cmd.BaseCmd, _ ...options.CmdOption) (*cobra.Command, error) {
21+
func NewClearCmd(baseCmd *cmd.BaseCmd, opt ...options.CmdOption) (*cobra.Command, error) {
22+
opts, err := options.NewOptions(opt...)
23+
if err != nil {
24+
return nil, err
25+
}
26+
2127
c := &ClearCmd{
22-
BaseCmd: baseCmd,
28+
BaseCmd: baseCmd,
29+
ctxLoader: opts.ContextLoader,
2330
}
2431

2532
cobraCmd := &cobra.Command{
@@ -52,20 +59,23 @@ func (c *ClearCmd) run(cmd *cobra.Command, args []string) error {
5259
"please re-run the command with the --force flag", serverName)
5360
}
5461

55-
cfg, err := context.LoadExecutionContextConfig(flags.RuntimeFile)
62+
cfg, err := c.ctxLoader.Load(flags.RuntimeFile)
5663
if err != nil {
5764
return fmt.Errorf("failed to load execution context config: %w", err)
5865
}
5966

60-
if s, ok := cfg.Servers[serverName]; ok {
61-
// Clear and reassign the server in the config.
62-
s.Args = []string{}
63-
cfg.Servers[serverName] = s
64-
if err := context.SaveExecutionContextConfig(flags.RuntimeFile, cfg); err != nil {
65-
return fmt.Errorf("failed to clear argument config for '%s': %w", serverName, err)
66-
}
67+
s, ok := cfg.Get(serverName)
68+
if !ok {
69+
return fmt.Errorf("server '%s' not found in configuration", serverName)
6770
}
6871

69-
fmt.Fprintf(cmd.OutOrStdout(), "✓ Arguments cleared for server '%s'\n", serverName)
72+
s.Args = []string{}
73+
res, err := cfg.Upsert(s)
74+
if err != nil {
75+
return fmt.Errorf("error clearing arguments for server '%s': %w", serverName, err)
76+
}
77+
78+
fmt.Fprintf(cmd.OutOrStdout(), "✓ Arguments cleared for server '%s' (operation: %s)\n", serverName, string(res))
79+
7080
return nil
7181
}

cmd/config/args/list.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,18 @@ import (
1414

1515
type ListCmd struct {
1616
*cmd.BaseCmd
17+
ctxLoader context.Loader
1718
}
1819

19-
func NewListCmd(baseCmd *cmd.BaseCmd, _ ...options.CmdOption) (*cobra.Command, error) {
20+
func NewListCmd(baseCmd *cmd.BaseCmd, opt ...options.CmdOption) (*cobra.Command, error) {
21+
opts, err := options.NewOptions(opt...)
22+
if err != nil {
23+
return nil, err
24+
}
25+
2026
c := &ListCmd{
21-
BaseCmd: baseCmd,
27+
BaseCmd: baseCmd,
28+
ctxLoader: opts.ContextLoader,
2229
}
2330

2431
cobraCmd := &cobra.Command{
@@ -39,12 +46,12 @@ func (c *ListCmd) run(cmd *cobra.Command, args []string) error {
3946
return fmt.Errorf("server-name is required")
4047
}
4148

42-
cfg, err := context.LoadExecutionContextConfig(flags.RuntimeFile)
49+
cfg, err := c.ctxLoader.Load(flags.RuntimeFile)
4350
if err != nil {
4451
return fmt.Errorf("failed to load execution context config: %w", err)
4552
}
4653

47-
server, ok := cfg.Servers[serverName]
54+
server, ok := cfg.Get(serverName)
4855
if !ok {
4956
return fmt.Errorf("server '%s' not found in configuration", serverName)
5057
}

cmd/config/args/remove.go

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,18 @@ import (
1717

1818
type RemoveCmd struct {
1919
*cmd.BaseCmd
20+
ctxLoader context.Loader
2021
}
2122

22-
func NewRemoveCmd(baseCmd *cmd.BaseCmd, _ ...options.CmdOption) (*cobra.Command, error) {
23+
func NewRemoveCmd(baseCmd *cmd.BaseCmd, opt ...options.CmdOption) (*cobra.Command, error) {
24+
opts, err := options.NewOptions(opt...)
25+
if err != nil {
26+
return nil, err
27+
}
28+
2329
c := &RemoveCmd{
24-
BaseCmd: baseCmd,
30+
BaseCmd: baseCmd,
31+
ctxLoader: opts.ContextLoader,
2532
}
2633

2734
// mcpd config args remove time -- --arg [--arg ...]
@@ -51,27 +58,27 @@ func (c *RemoveCmd) run(cmd *cobra.Command, args []string) error {
5158
argMap[key] = struct{}{}
5259
}
5360

54-
cfg, err := context.LoadExecutionContextConfig(flags.RuntimeFile)
61+
cfg, err := c.ctxLoader.Load(flags.RuntimeFile)
5562
if err != nil {
5663
return fmt.Errorf("failed to load execution context config: %w", err)
5764
}
5865

59-
if serverCtx := cfg.Servers[serverName]; serverCtx.Args != nil {
60-
toRemove := slices.Collect(maps.Keys(argMap))
61-
filtered := config.RemoveMatchingFlags(serverCtx.Args, toRemove)
66+
serverCtx, ok := cfg.Get(serverName)
67+
if !ok {
68+
return fmt.Errorf("server '%s' not found in configuration", serverName)
69+
}
6270

63-
// Only modify the file if there are actual changes to be made.
64-
if !slices.Equal(slices.Clone(serverCtx.Args), slices.Clone(filtered)) {
65-
// Update the args, and the server.
66-
serverCtx.Args = filtered
67-
cfg.Servers[serverName] = serverCtx
71+
toRemove := slices.Collect(maps.Keys(argMap))
72+
filtered := config.RemoveMatchingFlags(serverCtx.Args, toRemove)
6873

69-
if err := context.SaveExecutionContextConfig(flags.RuntimeFile, cfg); err != nil {
70-
return fmt.Errorf("failed to save config: %w", err)
71-
}
72-
}
74+
// Update the args, and the server.
75+
serverCtx.Args = filtered
76+
res, err := cfg.Upsert(serverCtx)
77+
if err != nil {
78+
return fmt.Errorf("error removing arguments for server '%s': %w", serverName, err)
7379
}
7480

75-
fmt.Fprintf(cmd.OutOrStdout(), "✓ Args removed for server '%s': %v\n", serverName, slices.Collect(maps.Keys(argMap)))
81+
fmt.Fprintf(cmd.OutOrStdout(), "✓ Arguments removed for server '%s' (operation: %s): %v\n", serverName, string(res), slices.Collect(maps.Keys(argMap)))
82+
7683
return nil
7784
}

cmd/config/args/set.go

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,18 @@ import (
1515

1616
type SetCmd struct {
1717
*cmd.BaseCmd
18+
ctxLoader context.Loader
1819
}
1920

20-
func NewSetCmd(baseCmd *cmd.BaseCmd, _ ...cmdopts.CmdOption) (*cobra.Command, error) {
21+
func NewSetCmd(baseCmd *cmd.BaseCmd, opt ...cmdopts.CmdOption) (*cobra.Command, error) {
22+
opts, err := cmdopts.NewOptions(opt...)
23+
if err != nil {
24+
return nil, err
25+
}
26+
2127
c := &SetCmd{
22-
BaseCmd: baseCmd,
28+
BaseCmd: baseCmd,
29+
ctxLoader: opts.ContextLoader,
2330
}
2431

2532
cobraCmd := &cobra.Command{
@@ -49,26 +56,31 @@ func (c *SetCmd) run(cmd *cobra.Command, args []string) error {
4956
return fmt.Errorf("server-name is required")
5057
}
5158

52-
fmt.Fprintf(cmd.OutOrStdout(), "args: %#v\n", args)
53-
5459
normalizedArgs := config.NormalizeArgs(args[1:])
55-
cfg, err := context.LoadOrInitExecutionContext(flags.RuntimeFile)
60+
cfg, err := c.ctxLoader.Load(flags.RuntimeFile)
5661
if err != nil {
5762
return fmt.Errorf("failed to load execution context config: %w", err)
5863
}
5964

60-
serverCtx := cfg.Servers[serverName]
61-
serverCtx.Args = config.MergeArgs(serverCtx.Args, normalizedArgs)
62-
if serverCtx.Env == nil {
63-
serverCtx.Env = map[string]string{}
65+
server, exists := cfg.Get(serverName)
66+
if !exists {
67+
server.Name = serverName
6468
}
65-
cfg.Servers[serverName] = serverCtx
6669

67-
if err := context.SaveExecutionContextConfig(flags.RuntimeFile, cfg); err != nil {
68-
return fmt.Errorf("failed to save config: %w", err)
70+
newArgs := config.MergeArgs(server.Args, normalizedArgs)
71+
72+
// Update...
73+
server.Args = newArgs
74+
if len(server.Env) == 0 {
75+
server.Env = map[string]string{}
76+
}
77+
78+
res, err := cfg.Upsert(server)
79+
if err != nil {
80+
return fmt.Errorf("error setting arguments for server '%s': %w", serverName, err)
6981
}
7082

71-
fmt.Fprintf(cmd.OutOrStdout(), "✓ Startup arguments set for server '%s': %v\n", serverName, normalizedArgs)
83+
fmt.Fprintf(cmd.OutOrStdout(), "✓ Startup arguments set for server '%s' (operation: %s): %v\n", serverName, string(res), normalizedArgs)
7284

7385
return nil
7486
}

cmd/config/env/clear.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,19 @@ import (
1414

1515
type ClearCmd struct {
1616
*cmd.BaseCmd
17-
Force bool
17+
Force bool
18+
ctxLoader context.Loader
1819
}
1920

20-
func NewClearCmd(baseCmd *cmd.BaseCmd, _ ...options.CmdOption) (*cobra.Command, error) {
21+
func NewClearCmd(baseCmd *cmd.BaseCmd, opt ...options.CmdOption) (*cobra.Command, error) {
22+
opts, err := options.NewOptions(opt...)
23+
if err != nil {
24+
return nil, err
25+
}
26+
2127
c := &ClearCmd{
22-
BaseCmd: baseCmd,
28+
BaseCmd: baseCmd,
29+
ctxLoader: opts.ContextLoader,
2330
}
2431

2532
cobraCmd := &cobra.Command{
@@ -52,21 +59,23 @@ func (c *ClearCmd) run(cmd *cobra.Command, args []string) error {
5259
"please re-run the command with the --force flag", serverName)
5360
}
5461

55-
cfg, err := context.LoadExecutionContextConfig(flags.RuntimeFile)
62+
cfg, err := c.ctxLoader.Load(flags.RuntimeFile)
5663
if err != nil {
5764
return fmt.Errorf("failed to load execution context config: %w", err)
5865
}
5966

60-
if s, ok := cfg.Servers[serverName]; ok {
61-
// Clear the env map and reassign the server in the config.
62-
s.Env = make(map[string]string)
63-
cfg.Servers[serverName] = s
64-
if err := context.SaveExecutionContextConfig(flags.RuntimeFile, cfg); err != nil {
65-
return fmt.Errorf("failed to clear env var config for '%s': %w", serverName, err)
66-
}
67+
s, ok := cfg.Get(serverName)
68+
if !ok {
69+
return fmt.Errorf("server '%s' not found in configuration", serverName)
70+
}
71+
72+
s.Env = make(map[string]string)
73+
res, err := cfg.Upsert(s)
74+
if err != nil {
75+
return fmt.Errorf("error clearing environment variables for server '%s': %w", serverName, err)
6776
}
6877

69-
fmt.Fprintf(cmd.OutOrStdout(), "✓ Environment variables cleared for server '%s'\n", serverName)
78+
fmt.Fprintf(cmd.OutOrStdout(), "✓ Environment variables cleared for server '%s' (operation: %s)\n", serverName, string(res))
7079

7180
return nil
7281
}

cmd/config/env/list.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,27 @@ import (
99

1010
"github.com/mozilla-ai/mcpd/v2/internal/cmd"
1111
"github.com/mozilla-ai/mcpd/v2/internal/cmd/options"
12+
"github.com/mozilla-ai/mcpd/v2/internal/config"
1213
"github.com/mozilla-ai/mcpd/v2/internal/context"
1314
"github.com/mozilla-ai/mcpd/v2/internal/flags"
1415
)
1516

1617
type ListCmd struct {
1718
*cmd.BaseCmd
19+
cfgLoader config.Loader
20+
ctxLoader context.Loader
1821
}
1922

20-
func NewListCmd(baseCmd *cmd.BaseCmd, _ ...options.CmdOption) (*cobra.Command, error) {
23+
func NewListCmd(baseCmd *cmd.BaseCmd, opt ...options.CmdOption) (*cobra.Command, error) {
24+
opts, err := options.NewOptions(opt...)
25+
if err != nil {
26+
return nil, err
27+
}
28+
2129
c := &ListCmd{
22-
BaseCmd: baseCmd,
30+
BaseCmd: baseCmd,
31+
cfgLoader: opts.ConfigLoader,
32+
ctxLoader: opts.ContextLoader,
2333
}
2434

2535
cobraCmd := &cobra.Command{
@@ -40,12 +50,12 @@ func (c *ListCmd) run(cmd *cobra.Command, args []string) error {
4050
return fmt.Errorf("server-name is required")
4151
}
4252

43-
cfg, err := context.LoadExecutionContextConfig(flags.RuntimeFile)
53+
cfg, err := c.ctxLoader.Load(flags.RuntimeFile)
4454
if err != nil {
4555
return fmt.Errorf("failed to load execution context config: %w", err)
4656
}
4757

48-
server, ok := cfg.Servers[serverName]
58+
server, ok := cfg.Get(serverName)
4959
if !ok {
5060
return fmt.Errorf("server '%s' not found in configuration", serverName)
5161
}

cmd/config/env/remove.go

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,19 @@ import (
1616

1717
type RemoveCmd struct {
1818
*cmd.BaseCmd
19-
EnvVars []string
19+
EnvVars []string
20+
ctxLoader context.Loader
2021
}
2122

22-
func NewRemoveCmd(baseCmd *cmd.BaseCmd, _ ...options.CmdOption) (*cobra.Command, error) {
23+
func NewRemoveCmd(baseCmd *cmd.BaseCmd, opt ...options.CmdOption) (*cobra.Command, error) {
24+
opts, err := options.NewOptions(opt...)
25+
if err != nil {
26+
return nil, err
27+
}
28+
2329
c := &RemoveCmd{
24-
BaseCmd: baseCmd,
30+
BaseCmd: baseCmd,
31+
ctxLoader: opts.ContextLoader,
2532
}
2633

2734
// mcpd config env remove time KEY [KEY ...]
@@ -50,23 +57,25 @@ func (c *RemoveCmd) run(cmd *cobra.Command, args []string) error {
5057
envMap[key] = struct{}{}
5158
}
5259

53-
cfg, err := context.LoadExecutionContextConfig(flags.RuntimeFile)
60+
cfg, err := c.ctxLoader.Load(flags.RuntimeFile)
5461
if err != nil {
5562
return fmt.Errorf("failed to load execution context config: %w", err)
5663
}
5764

58-
if serverCtx := cfg.Servers[serverName]; serverCtx.Env != nil {
59-
evs := serverCtx.Env
60-
for key := range envMap {
61-
delete(evs, key)
62-
}
63-
cfg.Servers[serverName] = serverCtx
65+
serverCtx, ok := cfg.Get(serverName)
66+
if !ok {
67+
return fmt.Errorf("server '%s' not found in configuration", serverName)
68+
}
6469

65-
if err := context.SaveExecutionContextConfig(flags.RuntimeFile, cfg); err != nil {
66-
return fmt.Errorf("failed to save config: %w", err)
67-
}
70+
for key := range envMap {
71+
delete(serverCtx.Env, key)
6872
}
73+
res, err := cfg.Upsert(serverCtx)
74+
if err != nil {
75+
return fmt.Errorf("error removing environment variables for server '%s': %w", serverName, err)
76+
}
77+
78+
fmt.Fprintf(cmd.OutOrStdout(), "✓ Environment variables removed for server '%s' (operation: %s): %v\n", serverName, string(res), slices.Collect(maps.Keys(envMap)))
6979

70-
fmt.Fprintf(cmd.OutOrStdout(), "✓ Environment variables removed for server '%s': %v\n", serverName, slices.Collect(maps.Keys(envMap)))
7180
return nil
7281
}

0 commit comments

Comments
 (0)