Skip to content

Commit

Permalink
Create aliasPath and expandPath function to store records with alias …
Browse files Browse the repository at this point in the history
…like ~ and $InitWorkdingDir. And expand paths to use internally
  • Loading branch information
SwayKh committed Sep 12, 2024
1 parent 991e68b commit 30bad6f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
25 changes: 24 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -77,10 +78,32 @@ func AddRecord(sourcePath, destinationPath string) error {

recordSlice := []string{}
recordSlice = append(recordSlice, sourcePath, destinationPath)
configuration.Record = append(configuration.Record, recordSlice)
configuration.Record = append(configuration.Record, aliasPath(recordSlice))

if err := writeConfig(configuration); err != nil {
return err
}
return nil
}

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

func expandPath(paths []string) []string {
for i, path := range paths {
if strings.HasPrefix(path, "$init_directory") {
paths[i] = strings.Replace(path, "$init_directory", CurrentWorkingDirectory, 1)
} else if strings.HasPrefix(path, "~") {
paths[i] = strings.Replace(path, "~", HomeDirectory, 1)
}
}
return paths
}
4 changes: 4 additions & 0 deletions pkg/config/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func loadConfig(configPath string) (appConfig, error) {
if err != nil {
return appConfig{}, fmt.Errorf("Error loading data to appConfig{}: %w", err)
}

for i, record := range config.Record {
config.Record[i] = expandPath(record)
}
return config, nil
}

Expand Down

0 comments on commit 30bad6f

Please sign in to comment.