Skip to content

Commit 41979de

Browse files
committed
refactor: move FindLocalConfig
1 parent 5b22ec4 commit 41979de

File tree

6 files changed

+63
-65
lines changed

6 files changed

+63
-65
lines changed

.goreleaser.yml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,3 @@ scoops:
8282
homepage: https://github.com/Norgate-AV/spc
8383
description: "CLI wrapper for the Crestron SIMPL+ Compiler"
8484
license: MIT
85-
86-
winget:
87-
- publisher: damienbutt
88-
release_notes_url: "https://github.com/Norgate-AV/spc/releases/tag/{{.Tag}}"
89-
license_url: https://github.com/Norgate-AV/spc/blob/main/LICENSE
90-
short_description: "CLI wrapper for the Crestron SIMPL+ Compiler"
91-
license: MIT
92-
repository:
93-
owner: "damienbutt"
94-
name: winget-pkgs
95-
token: "{{ .Env.RELEASE_TOKEN }}"

internal/config/finder.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package config
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
)
7+
8+
// FindLocalConfig finds local config file by walking up directories
9+
func FindLocalConfig(dir string) string {
10+
for {
11+
for _, ext := range []string{"yml", "yaml", "json", "toml"} {
12+
path := filepath.Join(dir, ".spc."+ext)
13+
14+
if _, err := os.Stat(path); err == nil {
15+
return path
16+
}
17+
}
18+
19+
parent := filepath.Dir(dir)
20+
if parent == dir {
21+
break
22+
}
23+
24+
dir = parent
25+
}
26+
27+
return ""
28+
}

internal/config/finder_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package config
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestFindLocalConfig(t *testing.T) {
12+
// Create a temporary directory structure
13+
tempDir := t.TempDir()
14+
subDir := filepath.Join(tempDir, "subdir")
15+
err := os.Mkdir(subDir, 0o755)
16+
assert.NoError(t, err)
17+
18+
// Create config files
19+
configYML := filepath.Join(subDir, ".spc.yml")
20+
err = os.WriteFile(configYML, []byte("target: \"3\""), 0o644)
21+
assert.NoError(t, err)
22+
23+
// Test finding in subdir
24+
result := FindLocalConfig(subDir)
25+
assert.Equal(t, configYML, result)
26+
27+
// Test finding in parent
28+
result = FindLocalConfig(filepath.Join(subDir, "deep"))
29+
assert.Equal(t, configYML, result)
30+
31+
// Test not found
32+
result = FindLocalConfig(tempDir)
33+
assert.Equal(t, "", result)
34+
}

internal/config/loader.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66

77
"github.com/spf13/cobra"
88
"github.com/spf13/viper"
9-
10-
"github.com/Norgate-AV/spc/internal/utils"
119
)
1210

1311
// Loader handles configuration loading from various sources
@@ -65,7 +63,7 @@ func (l *Loader) loadLocalConfig(args []string) {
6563
}
6664

6765
dir := filepath.Dir(absFirstFile)
68-
localPath := utils.FindLocalConfig(dir)
66+
localPath := FindLocalConfig(dir)
6967
if localPath != "" {
7068
viper.SetConfigFile(localPath)
7169
_ = viper.ReadInConfig()

internal/utils/target.go

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package utils
22

33
import (
4-
"os"
5-
"path/filepath"
64
"strconv"
75
)
86

@@ -18,25 +16,3 @@ func ParseTarget(t string) []string {
1816

1917
return series
2018
}
21-
22-
// FindLocalConfig finds local config file by walking up directories
23-
func FindLocalConfig(dir string) string {
24-
for {
25-
for _, ext := range []string{"yml", "yaml", "json", "toml"} {
26-
path := filepath.Join(dir, ".spc."+ext)
27-
28-
if _, err := os.Stat(path); err == nil {
29-
return path
30-
}
31-
}
32-
33-
parent := filepath.Dir(dir)
34-
if parent == dir {
35-
break
36-
}
37-
38-
dir = parent
39-
}
40-
41-
return ""
42-
}

internal/utils/target_test.go

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package utils
22

33
import (
4-
"os"
5-
"path/filepath"
64
"testing"
75

86
"github.com/stretchr/testify/assert"
@@ -29,28 +27,3 @@ func TestParseTarget(t *testing.T) {
2927
assert.Equal(t, test.expected, result, "ParseTarget(%q)", test.input)
3028
}
3129
}
32-
33-
func TestFindLocalConfig(t *testing.T) {
34-
// Create a temporary directory structure
35-
tempDir := t.TempDir()
36-
subDir := filepath.Join(tempDir, "subdir")
37-
err := os.Mkdir(subDir, 0o755)
38-
assert.NoError(t, err)
39-
40-
// Create config files
41-
configYML := filepath.Join(subDir, ".spc.yml")
42-
err = os.WriteFile(configYML, []byte("target: \"3\""), 0o644)
43-
assert.NoError(t, err)
44-
45-
// Test finding in subdir
46-
result := FindLocalConfig(subDir)
47-
assert.Equal(t, configYML, result)
48-
49-
// Test finding in parent
50-
result = FindLocalConfig(filepath.Join(subDir, "deep"))
51-
assert.Equal(t, configYML, result)
52-
53-
// Test not found
54-
result = FindLocalConfig(tempDir)
55-
assert.Equal(t, "", result)
56-
}

0 commit comments

Comments
 (0)