MCPcopy
hub / github.com/cheat/cheat / New

Function New

internal/config/new.go:16–129  ·  view source on GitHub ↗

New returns a new Config struct

(confPath string, resolve bool)

Source from the content-addressed store, hash-verified

14
15// New returns a new Config struct
16func New(confPath string, resolve bool) (Config, error) {
17
18 // read the config file
19 buf, err := os.ReadFile(confPath)
20 if err != nil {
21 return Config{}, fmt.Errorf("could not read config file: %v", err)
22 }
23
24 // initialize a config object
25 conf := Config{}
26
27 // store the config path
28 conf.Path = confPath
29
30 // unmarshal the yaml
31 err = yaml.Unmarshal(buf, &conf)
32 if err != nil {
33 return Config{}, fmt.Errorf("could not unmarshal yaml: %v", err)
34 }
35
36 // if a .cheat directory exists in the current directory or any ancestor,
37 // append it to the cheatpaths
38 cwd, err := os.Getwd()
39 if err != nil {
40 return Config{}, fmt.Errorf("failed to get cwd: %v", err)
41 }
42
43 if local := findLocalCheatpath(cwd); local != "" {
44 path := cp.Path{
45 Name: "cwd",
46 Path: local,
47 ReadOnly: false,
48 Tags: []string{},
49 }
50 conf.Cheatpaths = append(conf.Cheatpaths, path)
51 }
52
53 // process cheatpaths
54 var validPaths []cp.Path
55 for _, cheatpath := range conf.Cheatpaths {
56
57 // expand ~ in config paths
58 expanded, err := homedir.Expand(cheatpath.Path)
59 if err != nil {
60 return Config{}, fmt.Errorf("failed to expand ~: %v", err)
61 }
62
63 // follow symlinks
64 //
65 // NB: `resolve` is an ugly kludge that exists for the sake of unit-tests.
66 // It's necessary because `EvalSymlinks` will error if the symlink points
67 // to a non-existent location on the filesystem. When unit-testing,
68 // however, we don't want to have dependencies on the filesystem. As such,
69 // `resolve` is a switch that allows us to turn off symlink resolution when
70 // running the config tests.
71 if resolve {
72 evaled, err := filepath.EvalSymlinks(expanded)
73 if err != nil {

Callers 13

loadConfigFunction · 0.92
runFunction · 0.92
TestConfigSuccessfulFunction · 0.70
TestConfigFailureFunction · 0.70
TestEditorEnvOverrideFunction · 0.70
TestEditorEnvFallbackFunction · 0.70
TestNewTrimsWhitespaceFunction · 0.70
TestConfigYAMLErrorsFunction · 0.70
TestConfigDefaultsFunction · 0.70

Calls 2

findLocalCheatpathFunction · 0.85
EditorFunction · 0.85

Tested by 11

TestConfigSuccessfulFunction · 0.56
TestConfigFailureFunction · 0.56
TestEditorEnvOverrideFunction · 0.56
TestEditorEnvFallbackFunction · 0.56
TestNewTrimsWhitespaceFunction · 0.56
TestConfigYAMLErrorsFunction · 0.56
TestConfigDefaultsFunction · 0.56
TestConfigBrokenSymlinkFunction · 0.56