1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-21 19:50:56 +08:00

Correct style.

This commit is contained in:
Shanti Bouchez-Mongardé
2014-09-25 14:52:36 +02:00
parent 7705dfb939
commit e200c089fa
4 changed files with 35 additions and 18 deletions

View File

@ -49,7 +49,7 @@ func configCmd(c *commander.Command, inp []string) error {
return err return err
} }
filename, err := config.GetConfigFilePath(confdir) filename, err := config.Filename(confdir)
if err != nil { if err != nil {
return err return err
} }

View File

@ -39,7 +39,7 @@ func initCmd(c *commander.Command, inp []string) error {
} }
u.POut("initializing ipfs node at %s\n", configpath) u.POut("initializing ipfs node at %s\n", configpath)
filename, err := config.GetConfigFilePath(configpath) filename, err := config.Filename(configpath)
if err != nil { if err != nil {
return errors.New("Couldn't get home directory path") return errors.New("Couldn't get home directory path")
} }
@ -63,11 +63,10 @@ func initCmd(c *commander.Command, inp []string) error {
cfg.Datastore = config.Datastore{} cfg.Datastore = config.Datastore{}
if len(dspath) == 0 { if len(dspath) == 0 {
dspath, err = config.GetDefaultPathRoot() dspath, err = config.DataStorePath("")
if err != nil { if err != nil {
return err return err
} }
dspath = dspath + "/datastore"
} }
cfg.Datastore.Path = dspath cfg.Datastore.Path = dspath
cfg.Datastore.Type = "leveldb" cfg.Datastore.Type = "leveldb"

View File

@ -53,7 +53,7 @@ Use "ipfs help <command>" for more information about a command.
} }
func init() { func init() {
config, err := config.GetDefaultPathRoot() config, err := config.PathRoot()
if err != nil { if err != nil {
config = "" config = ""
} }
@ -78,7 +78,7 @@ func main() {
} }
func localNode(confdir string, online bool) (*core.IpfsNode, error) { func localNode(confdir string, online bool) (*core.IpfsNode, error) {
filename, err := config.GetConfigFilePath(confdir) filename, err := config.Filename(confdir)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -96,14 +96,14 @@ func localNode(confdir string, online bool) (*core.IpfsNode, error) {
func getConfigDir(c *commander.Command) (string, error) { func getConfigDir(c *commander.Command) (string, error) {
conf := c.Flag.Lookup("c").Value.Get() conf := c.Flag.Lookup("c").Value.Get()
if conf == nil { if conf == nil {
return config.GetDefaultPathRoot() return config.PathRoot()
} }
confStr, ok := conf.(string) confStr, ok := conf.(string)
if !ok { if !ok {
return "", errors.New("failed to retrieve config flag value.") return "", errors.New("failed to retrieve config flag value.")
} }
if len(confStr) == 0 { if len(confStr) == 0 {
return config.GetDefaultPathRoot() return config.PathRoot()
} }
return u.TildeExpansion(confStr) return u.TildeExpansion(confStr)

View File

@ -6,6 +6,7 @@ import (
"encoding/base64" "encoding/base64"
"errors" "errors"
"os" "os"
"path/filepath"
u "github.com/jbenet/go-ipfs/util" u "github.com/jbenet/go-ipfs/util"
) )
@ -42,27 +43,44 @@ type Config struct {
Bootstrap []*BootstrapPeer // local nodes's bootstrap peers Bootstrap []*BootstrapPeer // local nodes's bootstrap peers
} }
// Return the default configuration root directory const DefaultPathRoot = "~/.go-ipfs"
func GetDefaultPathRoot() (string, error) { const DefaultConfigFile = "config"
dir := os.Getenv("IPFS_CONFIG_DIR") const DefaultDataStoreDirectory = "datastore"
const EnvDir = "IPFS_DIR"
// PathRoot returns the default configuration root directory
func PathRoot() (string, error) {
dir := os.Getenv(EnvDir)
var err error var err error
if len(dir) == 0 { if len(dir) == 0 {
dir, err = u.TildeExpansion("~/.go-ipfs") dir, err = u.TildeExpansion(DefaultPathRoot)
} }
return dir, err return dir, err
} }
// Return the configuration file path given a configuration root directory // Path returns the path `extension` relative to the configuration root. If an
// If the configuration root directory is empty, use the default one // empty string is provided for `configroot`, the default root is used.
func GetConfigFilePath(configroot string) (string, error) { func Path(configroot, extension string) (string, error) {
if len(configroot) == 0 { if len(configroot) == 0 {
dir, err := GetDefaultPathRoot() dir, err := PathRoot()
return dir+"/config", err return filepath.Join(dir, extension), err
} else { } else {
return configroot+"/config", nil return filepath.Join(configroot, extension), nil
} }
} }
// DataStorePath returns the default data store path given a configuration root
// (set an empty string to have the default configuration root)
func DataStorePath(configroot string) (string, error) {
return Path(configroot, DefaultDataStoreDirectory);
}
// Filename returns the configuration file path given a configuration root
// directory. If the configuration root directory is empty, use the default one
func Filename(configroot string) (string, error) {
return Path(configroot, DefaultConfigFile);
}
// DecodePrivateKey is a helper to decode the users PrivateKey // DecodePrivateKey is a helper to decode the users PrivateKey
func (i *Identity) DecodePrivateKey(passphrase string) (crypto.PrivateKey, error) { func (i *Identity) DecodePrivateKey(passphrase string) (crypto.PrivateKey, error) {
pkb, err := base64.StdEncoding.DecodeString(i.PrivKey) pkb, err := base64.StdEncoding.DecodeString(i.PrivKey)