Skip to content

Commit

Permalink
Update the AliasPath and ExpandPath function definition to not loop o…
Browse files Browse the repository at this point in the history
…ver []string, but work with strings directly.

This allows alias of Init path for the InitiasliseConfig function
And Aliasing and Expanding the InitDirectory var from the config.
  • Loading branch information
SwayKh committed Sep 18, 2024
1 parent 9eccb26 commit b936bcf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
17 changes: 13 additions & 4 deletions pkg/config/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,25 @@ func LoadConfig() error {
return fmt.Errorf("Error loading data to appConfig{}: \n%w", err)
}

for i := range Configuration.Records {
Configuration.Records[i].Paths = expandPath(Configuration.Records[i].Paths)
Configuration.InitDirectory = expandPath(Configuration.InitDirectory)

for i, v := range Configuration.Records {
for j := range v.Paths {
Configuration.Records[i].Paths[j] = expandPath(Configuration.Records[i].Paths[j])
}
}

return nil
}

func WriteConfig() error {
Configuration.InitDirectory = aliasPath(Configuration.InitDirectory, true)

// Alias path absolute paths before writing to config file
for i := range Configuration.Records {
Configuration.Records[i].Paths = aliasPath(Configuration.Records[i].Paths)
for i, v := range Configuration.Records {
for j := range v.Paths {
Configuration.Records[i].Paths[j] = aliasPath(Configuration.Records[i].Paths[j], false)
}
}

data, err := yaml.Marshal(&Configuration)
Expand Down
31 changes: 15 additions & 16 deletions pkg/config/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,23 @@ func CheckFile(path string) (bool, os.FileInfo, error) {
return true, fileInfo, nil
}

func expandPath(paths []string) []string {
for i, path := range paths {
if strings.HasPrefix(path, "$init_directory") {
paths[i] = strings.Replace(path, "$init_directory", InitDirectory, 1)
} else if strings.HasPrefix(path, "~") {
paths[i] = strings.Replace(path, "~", HomeDirectory, 1)
}
func expandPath(path string) string {
if strings.HasPrefix(path, "$init_directory") {
path = strings.Replace(path, "$init_directory", Configuration.InitDirectory, 1)
}
if strings.HasPrefix(path, "~") {
path = strings.Replace(path, "~", HomeDirectory, 1)
}
return paths
return path
}

func aliasPath(paths []string) []string {
for i, path := range paths {
if strings.HasPrefix(path, InitDirectory) {
paths[i] = strings.Replace(path, InitDirectory, "$init_directory", 1)
} else if strings.HasPrefix(path, HomeDirectory) {
paths[i] = strings.Replace(path, HomeDirectory, "~", 1)
}
func aliasPath(path string, skipInitDir bool) string {
if strings.HasPrefix(path, HomeDirectory) {
path = strings.Replace(path, HomeDirectory, "~", 1)
}
return paths
if !skipInitDir && strings.HasPrefix(path, Configuration.InitDirectory) {
path = strings.Replace(path, Configuration.InitDirectory, "$init_directory", 1)
}

return path
}

0 comments on commit b936bcf

Please sign in to comment.