Commit 78c5696d authored by ale's avatar ale

moved ExpandTilde to the util package

parent 2dd4bcbf
......@@ -16,6 +16,7 @@ import (
"sync"
"git.autistici.org/ale/liber"
"git.autistici.org/ale/liber/util"
)
var (
......@@ -152,7 +153,7 @@ func doUpdate(db *liber.Database, dir string) {
}
func doSync(db *liber.Database, remoteAddr string) {
storage := liber.NewFileStorage(expandTilde(*bookDir))
storage := liber.NewFileStorage(util.ExpandTilde(*bookDir))
sc := liber.NewRemoteServer(remoteAddr)
if err := db.Sync(storage, sc); err != nil {
log.Fatal(err)
......@@ -181,8 +182,8 @@ func doSearch(db *liber.Database, query string) {
}
func doHttpServer(db *liber.Database, addr string) {
storage := liber.NewRWFileStorage(expandTilde(*bookDir), 2)
cache := liber.NewRWFileStorage(filepath.Join(expandTilde(*databaseDir), "cache"), 2)
storage := liber.NewRWFileStorage(util.ExpandTilde(*bookDir), 2)
cache := liber.NewRWFileStorage(filepath.Join(util.ExpandTilde(*databaseDir), "cache"), 2)
server := liber.NewHttpServer(db, storage, cache, addr)
log.Fatal(server.ListenAndServe())
}
......@@ -202,14 +203,6 @@ func b2i(b bool) int {
return 0
}
func expandTilde(path string) string {
if strings.HasPrefix(path, "~/") {
curUser, _ := user.Current()
return filepath.Join(curUser.HomeDir, strings.TrimPrefix(path, "~/"))
}
return path
}
func main() {
log.SetFlags(0)
flag.Parse()
......@@ -221,7 +214,7 @@ func main() {
log.Fatal("Must specify --book-dir")
}
dbdir := expandTilde(*databaseDir)
dbdir := util.ExpandTilde(*databaseDir)
db, err := liber.NewDb(dbdir)
if err != nil {
log.Fatal(err)
......@@ -237,7 +230,7 @@ func main() {
log.SetFlags(log.Ldate | log.Ltime)
}
doUpdate(db, expandTilde(*bookDir))
doUpdate(db, util.ExpandTilde(*bookDir))
} else if *remotesync != "" {
doSync(db, *remotesync)
} else if *search {
......
package util
import (
"os/user"
"path/filepath"
"strings"
)
// ExpandTilde replaces an initial "~/" in the given path with the
// current user's home directory. It is intended as a convenience for
// those cases where the provided path does not come from a shell,
// which would otherwise handle the expansion.
func ExpandTilde(path string) string {
if strings.HasPrefix(path, "~/") {
if u, err := user.Current(); err == nil {
return filepath.Join(u.HomeDir, path[2:])
}
}
return path
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment