Skip to content

Commit e2b8c3d

Browse files
committed
fix: enhance config file handling by expanding paths and improving directory validation
1 parent ad0c799 commit e2b8c3d

3 files changed

Lines changed: 29 additions & 11 deletions

File tree

src/internal/lib/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func InitConfig() error {
3434
}
3535

3636
func getOrCreateConfigFile() (string, error) {
37-
path := getPath()
37+
path := sys.ExpandPath(getPath())
3838
if !sys.FileExists(path) {
3939
f, err := sys.CreateFile(path)
4040
if err != nil {

src/internal/lib/lib.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import (
1313
"sync"
1414

1515
sys "github.com/8bitalex/raid/src/internal/sys"
16-
"github.com/8bitalex/raid/src/internal/utils"
1716
"github.com/santhosh-tekuri/jsonschema/v6"
17+
"gopkg.in/yaml.v3"
1818
)
1919

2020
//go:embed schemas/*.json
@@ -207,21 +207,38 @@ func validateFile(path string, sch *jsonschema.Schema) error {
207207
}
208208
defer f.Close()
209209

210-
var reader io.Reader = f
211210
ext := strings.ToLower(filepath.Ext(path))
212211
if ext == ".yaml" || ext == ".yml" {
213-
data, err := utils.YAMLToJSON(f)
214-
if err != nil {
215-
return err
212+
// Validate every document in a multi-doc YAML stream individually so
213+
// that profile files using --- separators are fully validated.
214+
dec := yaml.NewDecoder(f)
215+
for {
216+
var raw any
217+
if err := dec.Decode(&raw); err != nil {
218+
if err == io.EOF {
219+
break
220+
}
221+
return err
222+
}
223+
jsonBytes, err := json.Marshal(raw)
224+
if err != nil {
225+
return err
226+
}
227+
doc, err := jsonschema.UnmarshalJSON(bytes.NewReader(jsonBytes))
228+
if err != nil {
229+
return err
230+
}
231+
if err := sch.Validate(doc); err != nil {
232+
return fmt.Errorf("invalid format: %w", err)
233+
}
216234
}
217-
reader = bytes.NewReader(data)
235+
return nil
218236
}
219237

220-
doc, err := jsonschema.UnmarshalJSON(reader)
238+
doc, err := jsonschema.UnmarshalJSON(f)
221239
if err != nil {
222240
return err
223241
}
224-
225242
if err := sch.Validate(doc); err != nil {
226243
return fmt.Errorf("invalid format: %w", err)
227244
}

src/internal/lib/task_runner.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,9 @@ func execGit(task Task) error {
370370
}
371371
}
372372

373-
if !sys.FileExists(dir) {
374-
return fmt.Errorf("directory does not exist: %s", dir)
373+
info, statErr := os.Stat(dir)
374+
if statErr != nil || !info.IsDir() {
375+
return fmt.Errorf("path is not a directory: %s", dir)
375376
}
376377

377378
var args []string

0 commit comments

Comments
 (0)