Skip to content

Commit

Permalink
feat: allow symlink
Browse files Browse the repository at this point in the history
Signed-off-by: Batuhan Apaydın <batuhan.apaydin@trendyol.com>
  • Loading branch information
developer-guy committed Feb 28, 2022
1 parent ea93812 commit 3843eab
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
22 changes: 21 additions & 1 deletion pkg/commands/options/filestuff.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,30 @@ func EnumerateFiles(fo *FilenameOptions) chan string {
// processing (unless we are in recursive mode). If we decide to process
// the directory, and we're in watch mode, then we set up a watch on the
// directory.
symlink, err := ResolveSymlink(path, fi)
if err != nil {
return err
}

if fi.IsDir() {
if path != paths && !fo.Recursive {
return filepath.SkipDir
}
if watcher != nil {
watcher.Add(path)
watcher.Add(symlink)
} else {
if symlink != "" {
files <- symlink
}
}
// We don't stream back directories, we just decide to skip them, or not.
return nil
}

if symlink != "" {
files <- symlink
}

// Don't check extension if the filepath was passed explicitly
if path != paths {
switch filepath.Ext(path) {
Expand Down Expand Up @@ -142,3 +155,10 @@ func EnumerateFiles(fo *FilenameOptions) chan string {
}()
return files
}

func ResolveSymlink(path string, fi os.FileInfo) (symlink string, err error) {
if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
symlink, err = os.Readlink(path)
}
return
}
20 changes: 19 additions & 1 deletion pkg/commands/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,25 @@ func resolveFile(
if f == "-" {
b, err = ioutil.ReadAll(os.Stdin)
} else {
b, err = ioutil.ReadFile(f)
fi, err := os.Lstat(f)
if err != nil {
return nil, fmt.Errorf("unable to find file %s selector: %wß", f, err)
}

symlink, err := options.ResolveSymlink(f, fi)
if err != nil {
return nil, fmt.Errorf("unable to resolve symlink of file %s selector: %w", f, err)
}

if symlink != "" {
b, err = ioutil.ReadFile(symlink)
} else {
b, err = ioutil.ReadFile(f)
}

if err != nil {
return nil, fmt.Errorf("unable to read file %s: %w", f, err)
}
}
if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions test/symlink.yaml

0 comments on commit 3843eab

Please sign in to comment.