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