Skip to content

Commit 271afdd

Browse files
authored
Fix execution context file (and folder) creation during save (#53)
1 parent eccdd06 commit 271afdd

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

internal/context/context.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"os"
7+
"path/filepath"
78

89
"github.com/BurntSushi/toml"
910
)
@@ -61,7 +62,14 @@ func LoadExecutionContextConfig(path string) (ExecutionContextConfig, error) {
6162

6263
// SaveExecutionContextConfig saves a runtime execution context file to disk, using the specified path.
6364
func SaveExecutionContextConfig(path string, cfg ExecutionContextConfig) (err error) {
64-
f, err := os.Create(path) // TODO: Needs os.MkDirAll too.
65+
// Ensure the directory exists before creating the file...
66+
// owner: rwx, group: r--, others: ---
67+
if err := os.MkdirAll(filepath.Dir(path), 0o740); err != nil {
68+
return fmt.Errorf("could not ensure execution context directory exists for '%s': %w", path, err)
69+
}
70+
71+
// owner: rw-, group: ---, others: ---
72+
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o600)
6573
if err != nil {
6674
return fmt.Errorf("could not create file '%s': %w", path, err)
6775
}

internal/context/context_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ func TestSaveAndLoadExecutionContextConfig(t *testing.T) {
8888
t.Parallel()
8989

9090
dir := t.TempDir()
91-
path := filepath.Join(dir, "secrets.dev.toml")
91+
92+
// Include extra, currently non-existing folder along the way.
93+
path := filepath.Join(dir, ".mcpd", "secrets.dev.toml")
9294

9395
original := ExecutionContextConfig{
9496
Servers: map[string]ServerExecutionContext{

0 commit comments

Comments
 (0)