commit 66f1745bb34516d63b6bbde0e3dedf4dcbc41543
parent 827c61575a30fb5af97f94f6de741d93a13a2f77
Author: parazyd <parazyd@dyne.org>
Date: Tue, 13 Mar 2018 11:10:59 +0100
Add support for local directories.txt and add the DIR: parsing in damlib.
Diffstat:
2 files changed, 25 insertions(+), 11 deletions(-)
diff --git a/cmd/dam-client/main.go b/cmd/dam-client/main.go
@@ -34,7 +34,6 @@ import (
"os"
"os/exec"
"strconv"
- "strings"
"sync"
"time"
@@ -162,16 +161,14 @@ func fetchDirlist(locations []string) ([]string, error) {
if err != nil {
return nil, err
}
- dirStr := string(dirs)
- _dirs := strings.Split(dirStr, "\n")
- for _, j := range _dirs {
- if strings.HasPrefix(j, "DIR:") {
- t := strings.Split(j, "DIR:")
- if !(lib.StringInSlice(t[1], dirSlice)) {
- dirSlice = append(dirSlice, t[1])
- }
- }
- }
+ dirSlice = lib.ParseDirs(dirSlice, dirs)
+ }
+
+ // Local ~/.dam/directories.txt
+ if _, err := os.Stat("directories.txt"); err == nil {
+ dirs, err := ioutil.ReadFile("directories.txt")
+ lib.CheckError(err)
+ dirSlice = lib.ParseDirs(dirSlice, dirs)
}
// Local nodes known to redis
diff --git a/pkg/damlib/helpers.go b/pkg/damlib/helpers.go
@@ -25,6 +25,7 @@ import (
"compress/gzip"
"encoding/base64"
"log"
+ "strings"
)
// CheckError is a handler for errors. It takes an error type as an argument,
@@ -62,3 +63,19 @@ func GzipEncode(data []byte) (string, error) {
}
return base64.StdEncoding.EncodeToString(b.Bytes()), nil
}
+
+// ParseDirs parses and appends a given slice of bytes and returns an appended
+// slice of strings with new contents.
+func ParseDirs(sl []string, data []byte) []string {
+ dirStr := string(data)
+ _dirs := strings.Split(dirStr, "\n")
+ for _, j := range _dirs {
+ if strings.HasPrefix(j, "DIR:") {
+ t := strings.Split(j, "DIR:")
+ if !(StringInSlice(t[1], sl)) {
+ sl = append(sl, t[1])
+ }
+ }
+ }
+ return sl
+}