Skip to content

Commit 90bb4a9

Browse files
committed
refactor(cli): embed templates.yaml and simplify LoadTemplatesConfig function
1 parent 7dd0d84 commit 90bb4a9

File tree

4 files changed

+13
-230
lines changed

4 files changed

+13
-230
lines changed

src/client/acontext-cli/internal/config/templates.go

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package config
22

33
import (
4+
_ "embed"
45
"fmt"
5-
"os"
6-
"path/filepath"
76

87
"gopkg.in/yaml.v3"
98
)
@@ -26,37 +25,19 @@ type TemplatesConfig struct {
2625
Presets map[string][]Preset `yaml:"presets"`
2726
}
2827

28+
//go:embed templates.yaml
29+
var templatesYAML string
30+
2931
var cachedConfig *TemplatesConfig
3032

31-
// LoadTemplatesConfig loads template configuration file
33+
// LoadTemplatesConfig loads template configuration from embedded file
3234
func LoadTemplatesConfig() (*TemplatesConfig, error) {
3335
if cachedConfig != nil {
3436
return cachedConfig, nil
3537
}
3638

37-
// Get config file path
38-
exePath, err := os.Executable()
39-
if err != nil {
40-
return nil, fmt.Errorf("failed to get executable path: %w", err)
41-
}
42-
43-
exeDir := filepath.Dir(exePath)
44-
configPath := filepath.Join(exeDir, "templates", "templates.yaml")
45-
46-
// If not found in executable directory, try development path
47-
if _, err := os.Stat(configPath); os.IsNotExist(err) {
48-
// Development environment
49-
cwd, _ := os.Getwd()
50-
configPath = filepath.Join(cwd, "templates", "templates.yaml")
51-
}
52-
53-
data, err := os.ReadFile(configPath)
54-
if err != nil {
55-
return nil, fmt.Errorf("failed to read templates config: %w", err)
56-
}
57-
5839
var config TemplatesConfig
59-
if err := yaml.Unmarshal(data, &config); err != nil {
40+
if err := yaml.Unmarshal([]byte(templatesYAML), &config); err != nil {
6041
return nil, fmt.Errorf("failed to parse templates config: %w", err)
6142
}
6243

File renamed without changes.

src/client/acontext-cli/internal/config/templates_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ func TestLoadTemplatesConfig(t *testing.T) {
1919
{
2020
name: "load valid config from templates directory",
2121
setup: func(t *testing.T) string {
22-
// This will use the actual templates.yaml file
22+
// Reset cached config for clean test
23+
cachedConfig = nil
2324
return ""
2425
},
2526
wantErr: false,
@@ -28,18 +29,16 @@ func TestLoadTemplatesConfig(t *testing.T) {
2829

2930
for _, tt := range tests {
3031
t.Run(tt.name, func(t *testing.T) {
32+
tt.setup(t)
3133
config, err := LoadTemplatesConfig()
3234
if tt.wantErr {
3335
assert.Error(t, err)
3436
assert.Nil(t, config)
3537
} else {
36-
// If config file exists, should load successfully
37-
// If not, error is acceptable
38-
if err == nil {
39-
assert.NotNil(t, config)
40-
assert.NotEmpty(t, config.Templates)
41-
assert.NotEmpty(t, config.Presets)
42-
}
38+
require.NoError(t, err)
39+
assert.NotNil(t, config)
40+
assert.NotEmpty(t, config.Templates)
41+
assert.NotEmpty(t, config.Presets)
4342
}
4443
})
4544
}

src/client/acontext-cli/templates/README.md

Lines changed: 0 additions & 197 deletions
This file was deleted.

0 commit comments

Comments
 (0)