Commit 4e1c413e authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

move tilde expnasion to util.

parent 3b241486
......@@ -2,8 +2,7 @@ package config
import (
"os"
"os/user"
"strings"
u "github.com/jbenet/go-ipfs/util"
)
type Identity struct {
......@@ -35,15 +34,10 @@ func LoadConfig(filename string) (*Config, error) {
filename = defaultConfigFilePath
}
// expand ~/
if strings.HasPrefix(filename, "~/") {
usr, err := user.Current()
if err != nil {
return nil, err
}
dir := usr.HomeDir + "/"
filename = strings.Replace(filename, "~/", dir, 1)
// tilde expansion on config file
filename, err := u.TildeExpansion(filename)
if err != nil {
return nil, err
}
// if nothing is there, write first conifg file.
......@@ -52,7 +46,13 @@ func LoadConfig(filename string) (*Config, error) {
}
var cfg Config
err := ReadConfigFile(filename, &cfg)
err = ReadConfigFile(filename, &cfg)
if err != nil {
return nil, err
}
// tilde expansion on datastore path
cfg.Datastore.Path, err = u.TildeExpansion(cfg.Datastore.Path)
if err != nil {
return nil, err
}
......
......@@ -4,6 +4,8 @@ import (
"fmt"
mh "github.com/jbenet/go-multihash"
"os"
"os/user"
"strings"
)
var Debug bool
......@@ -17,6 +19,20 @@ func Hash(data []byte) (mh.Multihash, error) {
return mh.Sum(data, mh.SHA2_256, -1)
}
// tilde expansion
func TildeExpansion(filename string) (string, error) {
if strings.HasPrefix(filename, "~/") {
usr, err := user.Current()
if err != nil {
return "", err
}
dir := usr.HomeDir + "/"
filename = strings.Replace(filename, "~/", dir, 1)
}
return filename, nil
}
// Shorthand printing functions.
func PErr(format string, a ...interface{}) {
fmt.Fprintf(os.Stderr, format, a...)
......
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