2019-10-20 16:02:28 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2019-11-06 01:08:40 +01:00
|
|
|
"path/filepath"
|
2020-06-26 00:21:51 +02:00
|
|
|
"strings"
|
2019-10-20 16:02:28 +02:00
|
|
|
|
|
|
|
cp "github.com/cheat/cheat/internal/cheatpath"
|
|
|
|
|
|
|
|
"github.com/mitchellh/go-homedir"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config encapsulates configuration parameters
|
|
|
|
type Config struct {
|
2020-01-20 18:34:48 +01:00
|
|
|
Colorize bool `yaml:"colorize"`
|
|
|
|
Editor string `yaml:"editor"`
|
|
|
|
Cheatpaths []cp.Cheatpath `yaml:"cheatpaths"`
|
|
|
|
Style string `yaml:"style"`
|
|
|
|
Formatter string `yaml:"formatter"`
|
2020-06-26 00:21:51 +02:00
|
|
|
Pager string `yaml:"pager"`
|
2022-08-07 20:08:25 +02:00
|
|
|
Path string
|
2019-10-20 16:02:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new Config struct
|
2019-11-08 01:59:18 +01:00
|
|
|
func New(opts map[string]interface{}, confPath string, resolve bool) (Config, error) {
|
2019-10-20 16:02:28 +02:00
|
|
|
|
|
|
|
// read the config file
|
2022-08-05 02:38:49 +02:00
|
|
|
buf, err := os.ReadFile(confPath)
|
2019-10-20 16:02:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return Config{}, fmt.Errorf("could not read config file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialize a config object
|
|
|
|
conf := Config{}
|
|
|
|
|
2022-08-07 20:08:25 +02:00
|
|
|
// store the config path
|
|
|
|
conf.Path = confPath
|
|
|
|
|
2019-10-20 16:02:28 +02:00
|
|
|
// unmarshal the yaml
|
|
|
|
err = yaml.UnmarshalStrict(buf, &conf)
|
|
|
|
if err != nil {
|
|
|
|
return Config{}, fmt.Errorf("could not unmarshal yaml: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:34:48 +01:00
|
|
|
// if a .cheat directory exists locally, append it to the cheatpaths
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return Config{}, fmt.Errorf("failed to get cwd: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
local := filepath.Join(cwd, ".cheat")
|
|
|
|
if _, err := os.Stat(local); err == nil {
|
|
|
|
path := cp.Cheatpath{
|
|
|
|
Name: "cwd",
|
|
|
|
Path: local,
|
|
|
|
ReadOnly: false,
|
|
|
|
Tags: []string{},
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.Cheatpaths = append(conf.Cheatpaths, path)
|
|
|
|
}
|
|
|
|
|
2019-11-06 01:08:40 +01:00
|
|
|
// process cheatpaths
|
2019-10-20 16:02:28 +02:00
|
|
|
for i, cheatpath := range conf.Cheatpaths {
|
|
|
|
|
2019-11-06 01:08:40 +01:00
|
|
|
// expand ~ in config paths
|
2019-10-20 16:02:28 +02:00
|
|
|
expanded, err := homedir.Expand(cheatpath.Path)
|
|
|
|
if err != nil {
|
|
|
|
return Config{}, fmt.Errorf("failed to expand ~: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-11-06 01:08:40 +01:00
|
|
|
// follow symlinks
|
2019-11-08 01:59:18 +01:00
|
|
|
//
|
|
|
|
// NB: `resolve` is an ugly kludge that exists for the sake of unit-tests.
|
|
|
|
// It's necessary because `EvalSymlinks` will error if the symlink points
|
|
|
|
// to a non-existent location on the filesystem. When unit-testing,
|
|
|
|
// however, we don't want to have dependencies on the filesystem. As such,
|
|
|
|
// `resolve` is a switch that allows us to turn off symlink resolution when
|
|
|
|
// running the config tests.
|
|
|
|
if resolve {
|
2020-01-29 20:05:27 +01:00
|
|
|
evaled, err := filepath.EvalSymlinks(expanded)
|
2019-11-08 01:59:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return Config{}, fmt.Errorf(
|
2020-01-29 20:05:27 +01:00
|
|
|
"failed to resolve symlink: %s: %v",
|
2019-11-08 01:59:18 +01:00
|
|
|
expanded,
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
2020-01-29 20:05:27 +01:00
|
|
|
|
|
|
|
expanded = evaled
|
2019-11-06 01:08:40 +01:00
|
|
|
}
|
|
|
|
|
2019-10-20 16:02:28 +02:00
|
|
|
conf.Cheatpaths[i].Path = expanded
|
|
|
|
}
|
|
|
|
|
2022-08-07 16:16:52 +02:00
|
|
|
// if an editor was not provided in the configs, attempt to choose one
|
|
|
|
// that's appropriate for the environment
|
2019-10-20 16:02:28 +02:00
|
|
|
if conf.Editor == "" {
|
2022-08-07 16:16:52 +02:00
|
|
|
if conf.Editor, err = Editor(); err != nil {
|
|
|
|
return Config{}, err
|
2019-10-20 16:02:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 == "" {
|
2022-07-04 22:06:37 +02:00
|
|
|
conf.Formatter = "terminal"
|
2019-10-20 16:02:28 +02:00
|
|
|
}
|
|
|
|
|
2022-08-07 16:16:52 +02:00
|
|
|
// load the pager
|
2022-07-04 22:06:37 +02:00
|
|
|
conf.Pager = strings.TrimSpace(conf.Pager)
|
2020-06-26 00:21:51 +02:00
|
|
|
|
2019-10-20 16:02:28 +02:00
|
|
|
return conf, nil
|
|
|
|
}
|