From 6f1fb2339508ee915e93ef485e7ad96d8257682f Mon Sep 17 00:00:00 2001 From: simbados <22945705+simbados@users.noreply.github.com> Date: Mon, 12 Feb 2024 15:48:21 +0100 Subject: [PATCH] Add expansion of path for config file extension --- README.md | 4 +-- internal/util/osFile.go | 2 +- internal/util/parseConfig.go | 18 +++++------ internal/util/parseRootBinaryConfig_test.go | 2 +- internal/util/path.go | 34 +++++++++++++-------- internal/util/{ => testFiles}/test.json | 0 6 files changed, 34 insertions(+), 26 deletions(-) rename internal/util/{ => testFiles}/test.json (100%) diff --git a/README.md b/README.md index d23c463..9ad87fb 100644 --- a/README.md +++ b/README.md @@ -34,8 +34,8 @@ Sorted by priority - [x] Merge local and root config - [x] Vigilant mode, ask at the end to proceed with command and config - [x] Possibility to add overall config json file to apply to all commands (discuss if good idea?) - Will be solved with extend keyword -- [ ] Extend other config file, e.g. commonNode.json could be applied to npm.json, npx.json - - [ ] Look for "__extend__" key while parsing. Limit to 2 parent configs +- [x] Extend other config file, e.g. commonNode.json could be applied to npm.json, npx.json + - [x] Look for "__extend__" key while parsing. Limit to 2 parent configs - [ ] Validate json config files, if anything can not be parsed return error (e.g. no array provided) - [x] No array/bool provided - [ ] Config keys duplicated (can lead to bug that only first config is applied, disallow double config keys) diff --git a/internal/util/osFile.go b/internal/util/osFile.go index 9a49204..be2c092 100644 --- a/internal/util/osFile.go +++ b/internal/util/osFile.go @@ -11,7 +11,7 @@ import ( func DoesPathExist(filePath string) (bool, error) { _, err := os.Stat(filePath) if os.IsNotExist(err) { - return false, errors.New("file or directory does not exist") + return false, errors.New(fmt.Sprintf("file or directory does not exist for path: %v", filePath)) } else if err != nil { return false, errors.New(fmt.Sprintf("File could not be checked + %v", err)) } else { diff --git a/internal/util/parseConfig.go b/internal/util/parseConfig.go index 1ea0250..7fc9df3 100644 --- a/internal/util/parseConfig.go +++ b/internal/util/parseConfig.go @@ -126,18 +126,18 @@ func parseExtendedConfig(paths *types.Paths, path string, commands []string, dep var configs []*types.SbConfig if value, exists := configJson[types.ExtendsConfigKey]; exists { if extendPath, isString := value.(string); isString { - exists, err := DoesPathExist(extendPath) - if err != nil { - log.LogErr(err) + extendPathExpanded, pathExpansionError := CommonPathExpansion(paths, extendPath) + if pathExpansionError != nil { + log.LogErr(pathExpansionError) } - if exists { - log.LogDebug("Extending config with config of path: ", extendPath) - configs = append(configs, parseJsonConfig(paths, extendPath, commands, depth+1)) - } else { - log.LogWarn("Path which was provided for extending the config does not exists: ", path) + _, err := DoesPathExist(extendPathExpanded) + if err != nil { + log.LogErr(fmt.Sprintf("Error while finding extended path in config: %v \n%v", path, err)) } + log.LogDebug("Extending config with config from path: ", extendPath) + configs = append(configs, parseJsonConfig(paths, extendPathExpanded, commands, depth+1)) } else { - log.LogWarn(types.ExtendsConfigKey, " key is not a string at path", extendPath) + log.LogWarn(types.ExtendsConfigKey, " key is not a string at path: ", path) } } return configs diff --git a/internal/util/parseRootBinaryConfig_test.go b/internal/util/parseRootBinaryConfig_test.go index 0ac4bf1..2824ed6 100644 --- a/internal/util/parseRootBinaryConfig_test.go +++ b/internal/util/parseRootBinaryConfig_test.go @@ -10,7 +10,7 @@ import ( func TestRootConfig(t *testing.T) { var commands = []string{"run", "some"} paths := types.Paths{LocalConfigPath: "Users/test/sb", HomePath: "/Users/test", RootConfigPath: "Users/test", WorkingDir: "/Users/test/sb", BinPath: "/usr/bin", BinaryPath: "/usr/bin/ls"} - config := parseJsonConfig(&paths, "./test.json", commands, 1) + config := parseJsonConfig(&paths, "./testFiles/test.json", commands, 1) if len(config.Write) != 2 { t.Errorf("parseJsonConfig should have 2 entries for write but was %v", config.Write) } diff --git a/internal/util/path.go b/internal/util/path.go index cc92cad..126cc12 100644 --- a/internal/util/path.go +++ b/internal/util/path.go @@ -31,6 +31,27 @@ func buildSymbolToPathMatching(paths *types.Paths) map[string]string { } func expandPaths(paths *types.Paths, value string) (string, error) { + value, err := CommonPathExpansion(paths, value) + if err != nil { + return "", err + } + if strings.Contains(value, "*") || strings.Contains(value, ".") { + value = regexp.MustCompile(dotRegex).ReplaceAllString(value, dotReplace) + if val, err := regexp.Compile(globEndRegex); err == nil && val.MatchString(value) { + value = val.ReplaceAllString(value, globEndReplace) + } else if val, err := regexp.Compile(globMiddleRegex); err == nil && val.MatchString(value) { + value = val.ReplaceAllString(value, globMiddleReplace) + } else if val, err := regexp.Compile(globSingleRegex); err == nil && val.MatchString(value) { + value = val.ReplaceAllString(value, globSingleReplace) + } + value = "(regex #\"" + value + "\")" + } else { + value = "(literal \"" + value + "\")" + } + return value, nil +} + +func CommonPathExpansion(paths *types.Paths, value string) (string, error) { initialPath := value matching := buildSymbolToPathMatching(paths) for key, path := range matching { @@ -52,18 +73,5 @@ func expandPaths(paths *types.Paths, value string) (string, error) { } value = "/" + filepath.Join(splits...) } - if strings.Contains(value, "*") || strings.Contains(value, ".") { - value = regexp.MustCompile(dotRegex).ReplaceAllString(value, dotReplace) - if val, err := regexp.Compile(globEndRegex); err == nil && val.MatchString(value) { - value = val.ReplaceAllString(value, globEndReplace) - } else if val, err := regexp.Compile(globMiddleRegex); err == nil && val.MatchString(value) { - value = val.ReplaceAllString(value, globMiddleReplace) - } else if val, err := regexp.Compile(globSingleRegex); err == nil && val.MatchString(value) { - value = val.ReplaceAllString(value, globSingleReplace) - } - value = "(regex #\"" + value + "\")" - } else { - value = "(literal \"" + value + "\")" - } return value, nil } diff --git a/internal/util/test.json b/internal/util/testFiles/test.json similarity index 100% rename from internal/util/test.json rename to internal/util/testFiles/test.json