Skip to content

Commit

Permalink
fix: reading symlinks
Browse files Browse the repository at this point in the history
  • Loading branch information
haveachin committed Dec 21, 2022
1 parent 8747183 commit d08fc4e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
4 changes: 2 additions & 2 deletions docs/INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ curl -LO https://github.com/haveachin/infrared/releases/download/{version}/infra

Downloading by using Powershell on Windows:
```Powershell
Invoke-WebRequest -Uri https://github.com/haveachin/infrared/releases/download/v1.3.4/infrared_Windows_x86_64.zip -OutFile c:\temp\infrared.zip
Invoke-WebRequest -Uri https://github.com/haveachin/infrared/releases/download/v1.3.4/infrared_Windows_x86_64.zip -OutFile c:\infrared.zip
```

### Extracting the binary
Expand All @@ -52,7 +52,7 @@ Extracting by using the terminal on macOS or GNU/Linux:
tar -xzf infrared_{architecture}.tar.gz
```

Downloading by using Powershell on Windows:
Extracting by using Powershell on Windows:
```Powershell
Expand-Archive c:\infrared.zip -DestinationPath c:\
```
57 changes: 51 additions & 6 deletions internal/pkg/config/provider/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,43 @@ func (p file) readConfigData() (Data, error) {
}

func readConfigsFromDir(dir string, v any) error {
readConfig := func(path string, info fs.FileInfo, err error) error {
fi, err := os.Lstat(dir)
if err != nil {
return err
}

if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
dir, err = os.Readlink(dir)
if err != nil {
return err
}
}

if info.IsDir() {
readConfig := func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}

if d.IsDir() {
return nil
}

if d.Type()&os.ModeSymlink == os.ModeSymlink {
path, err := filepath.EvalSymlinks(path)
if err != nil {
return err
}

fi, err := os.Lstat(path)
if err != nil {
return err
}

if fi.IsDir() {
return nil
}
}

cfgData := map[string]any{}
if err := ReadConfigFile(path, &cfgData); err != nil {
return fmt.Errorf("could not read %s; %v", path, err)
Expand All @@ -187,16 +215,33 @@ func readConfigsFromDir(dir string, v any) error {
return mergo.Merge(v, cfgData, mergo.WithOverride)
}

return filepath.Walk(dir, readConfig)
return filepath.WalkDir(dir, readConfig)
}

func ReadConfigFile(filename string, v any) error {
bb, err := os.ReadFile(filename)
func ReadConfigFile(name string, v any) error {
name, err := filepath.EvalSymlinks(name)
if err != nil {
return err
}

fi, err := os.Lstat(name)
if err != nil {
return err
}

if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
name, err = os.Readlink(name)
if err != nil {
return err
}
}

bb, err := os.ReadFile(name)
if err != nil {
return err
}

ext := filepath.Ext(filename)[1:]
ext := filepath.Ext(name)[1:]
switch ext {
case "json":
if err := json.Unmarshal(bb, v); err != nil {
Expand Down

0 comments on commit d08fc4e

Please sign in to comment.