Skip to content

Commit

Permalink
Add expansion of path for config file extension
Browse files Browse the repository at this point in the history
  • Loading branch information
simbados committed Feb 12, 2024
1 parent 026d8f6 commit 6f1fb23
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 26 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion internal/util/osFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 9 additions & 9 deletions internal/util/parseConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/util/parseRootBinaryConfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
34 changes: 21 additions & 13 deletions internal/util/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
File renamed without changes.

0 comments on commit 6f1fb23

Please sign in to comment.