cheat/internal/config/config.go
Chris Lane f7c093bec0 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
2019-11-05 19:08:40 -05:00

86 lines
1.9 KiB
Go

package config
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
cp "github.com/cheat/cheat/internal/cheatpath"
"github.com/mitchellh/go-homedir"
"gopkg.in/yaml.v2"
)
// Config encapsulates configuration parameters
type Config struct {
Colorize bool `yaml:colorize`
Editor string `yaml:editor`
Cheatpaths []cp.Cheatpath `yaml:cheatpaths`
Style string `yaml:style`
Formatter string `yaml:formatter`
}
// New returns a new Config struct
func New(opts map[string]interface{}, confPath string) (Config, error) {
// read the config file
buf, err := ioutil.ReadFile(confPath)
if err != nil {
return Config{}, fmt.Errorf("could not read config file: %v", err)
}
// initialize a config object
conf := Config{}
// unmarshal the yaml
err = yaml.UnmarshalStrict(buf, &conf)
if err != nil {
return Config{}, fmt.Errorf("could not unmarshal yaml: %v", err)
}
// 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
}
// if an editor was not provided in the configs, look to envvars
if conf.Editor == "" {
if os.Getenv("VISUAL") != "" {
conf.Editor = os.Getenv("VISUAL")
} else if os.Getenv("EDITOR") != "" {
conf.Editor = os.Getenv("EDITOR")
} else {
return Config{}, fmt.Errorf("no editor set")
}
}
// if a chroma style was not provided, set a default
if conf.Style == "" {
conf.Style = "bw"
}
// if a chroma formatter was not provided, set a default
if conf.Formatter == "" {
conf.Formatter = "terminal16m"
}
return conf, nil
}