1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-28 17:03:58 +08:00

move tilde expnasion to util.

This commit is contained in:
Juan Batiz-Benet
2014-07-05 17:34:44 -07:00
parent 3b241486a9
commit 4e1c413e82
2 changed files with 28 additions and 12 deletions

View File

@ -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,15 +34,10 @@ 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.
@ -52,7 +46,13 @@ func LoadConfig(filename string) (*Config, error) {
} }
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
} }

View File

@ -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) {
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...)