/
config_unparsed.go
50 lines (43 loc) · 1.82 KB
/
config_unparsed.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package regolith
import (
"os"
"github.com/Bedrock-OSS/go-burrito/burrito"
"github.com/muhammadmuzzammil1998/jsonc"
)
// Functions for accessing information from the config file without parsing it
// to a Config object. This is useful for accessing the config information
// for functions that modify the content of the config file, like
// "regolith install" and for accessing the config information when the file
// might have some errors.
// LoadConfigAsMap loads the config.json file as map[string]interface{}
func LoadConfigAsMap() (map[string]interface{}, error) {
err := CheckSuspiciousLocation()
if err != nil {
return nil, burrito.PassError(err)
}
file, err := os.ReadFile(ConfigFilePath)
if err != nil {
return nil, burrito.WrappedError( // We don't need to pass OS error. It's confusing.
"Failed to open \"config.json\". This directory is not a Regolith project.\n" +
"Please make sure to run this command in a Regolith project directory.\n" +
"If you want to create new Regolith project here, use \"regolith init\".")
}
var configJson map[string]interface{}
err = jsonc.Unmarshal(file, &configJson)
if err != nil {
return nil, burrito.WrapErrorf(err, jsonUnmarshalError, ConfigFilePath)
}
return configJson, nil
}
// dataPathFromConfigMap returns the value of the data path from the config
// file map, without parsing it to a Config object.
func dataPathFromConfigMap(config map[string]interface{}) (string, error) {
return FindByJSONPath[string](config, "regolith/dataPath")
}
// filterDefinitionFromConfigMap returns the filter definitions as map from
// the config file map, without parsing it to a Config object.
func filterDefinitionsFromConfigMap(
config map[string]interface{},
) (map[string]interface{}, error) {
return FindByJSONPath[map[string]interface{}](config, "regolith/filterDefinitions")
}