Skip to content

Commit

Permalink
Improved unique key algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
cgxeiji committed Nov 2, 2018
1 parent cea032c commit 8b60e40
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions scholar/cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,18 +173,22 @@ func requestSearch() string {
return res
}

func commit(entry *scholar.Entry) {
key := entry.GetKey()
saveTo := filepath.Join(viper.GetString("deflib"), key)
if currentLibrary != "" {
saveTo = filepath.Join(viper.Sub("LIBRARIES").GetString(currentLibrary), key)
func getUniqueKey(key string) string {
path := libraryPath()
mark := 'a'
valid := key

for _, err := os.Stat(filepath.Join(path, valid)); !os.IsNotExist(err); _, err = os.Stat(filepath.Join(path, valid)) {
valid = fmt.Sprintf("%s%s", key, string(mark))
mark++
}

if _, err := os.Stat(saveTo); !os.IsNotExist(err) {
//TODO: make a better algorithm for unique keys
saveTo = fmt.Sprintf("%sa", saveTo)
entry.Key = fmt.Sprintf("%sa", key)
}
return valid
}

func commit(entry *scholar.Entry) {
entry.Key = getUniqueKey(entry.GetKey())
saveTo := filepath.Join(libraryPath(), entry.Key)

err := os.MkdirAll(saveTo, os.ModePerm)
if err != nil {
Expand Down

0 comments on commit 8b60e40

Please sign in to comment.