From f7c093bec0b6d57c47231d5b2ade604e87e13fc9 Mon Sep 17 00:00:00 2001 From: Chris Lane Date: Tue, 5 Nov 2019 19:08:40 -0500 Subject: [PATCH] fix: resolves #474 Resolves #474, whereby `cheat` failed to resolve symlinks. The root cause was that `path/filepath#Walk` simply does not resolve symlinks: https://golang.org/pkg/path/filepath/#Walk --- internal/config/config.go | 14 +++++++++++++- internal/sheets/load.go | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index 737fa64..0f5a3d0 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -4,6 +4,7 @@ import ( "fmt" "io/ioutil" "os" + "path/filepath" cp "github.com/cheat/cheat/internal/cheatpath" @@ -38,14 +39,25 @@ func New(opts map[string]interface{}, confPath string) (Config, error) { return Config{}, fmt.Errorf("could not unmarshal yaml: %v", err) } - // expand ~ in config paths + // process cheatpaths for i, cheatpath := range conf.Cheatpaths { + // expand ~ in config paths expanded, err := homedir.Expand(cheatpath.Path) if err != nil { return Config{}, fmt.Errorf("failed to expand ~: %v", err) } + // follow symlinks + expanded, err = filepath.EvalSymlinks(expanded) + if err != nil { + return Config{}, fmt.Errorf( + "failed to resolve symlink: %s, %v", + expanded, + err, + ) + } + conf.Cheatpaths[i].Path = expanded } diff --git a/internal/sheets/load.go b/internal/sheets/load.go index 5cadddb..7dcc6a6 100644 --- a/internal/sheets/load.go +++ b/internal/sheets/load.go @@ -54,7 +54,7 @@ func Load(cheatpaths []cp.Cheatpath) ([]map[string]sheet.Sheet, error) { // NB: this is still somewhat brittle in that it will miss files // contained within hidden directories in the middle of a path, though // that should not realistically occur. - if strings.HasPrefix(title, ".") || strings.HasPrefix(info.Name(), "."){ + if strings.HasPrefix(title, ".") || strings.HasPrefix(info.Name(), ".") { return nil }