Skip to content

Commit f223aef

Browse files
committed
Merge pull request #126 from mildred/master
Rework config file location
2 parents 68f1a1a + 832d84e commit f223aef

File tree

4 files changed

+90
-41
lines changed

4 files changed

+90
-41
lines changed

cmd/ipfs/config.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,12 @@ func init() {
4444

4545
func configCmd(c *commander.Command, inp []string) error {
4646

47-
// todo: implement --config filename flag.
48-
filename, err := config.Filename("")
47+
confdir, err := getConfigDir(c.Parent)
48+
if err != nil {
49+
return err
50+
}
51+
52+
filename, err := config.Filename(confdir)
4953
if err != nil {
5054
return err
5155
}

cmd/ipfs/init.go

+26-16
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"encoding/base64"
5+
"path/filepath"
56
"errors"
67
"os"
78

@@ -29,26 +30,26 @@ func init() {
2930
cmdIpfsInit.Flag.Int("b", 4096, "number of bits for keypair")
3031
cmdIpfsInit.Flag.String("p", "", "passphrase for encrypting keys")
3132
cmdIpfsInit.Flag.Bool("f", false, "force overwrite of existing config")
33+
cmdIpfsInit.Flag.String("d", "", "Change default datastore location")
3234
}
3335

3436
func initCmd(c *commander.Command, inp []string) error {
3537
configpath, err := getConfigDir(c.Parent)
3638
if err != nil {
3739
return err
3840
}
39-
if configpath == "" {
40-
configpath, err = u.TildeExpansion("~/.go-ipfs")
41-
if err != nil {
42-
return err
43-
}
44-
}
4541

4642
u.POut("initializing ipfs node at %s\n", configpath)
47-
filename, err := config.Filename(configpath + "/config")
43+
filename, err := config.Filename(configpath)
4844
if err != nil {
4945
return errors.New("Couldn't get home directory path")
5046
}
5147

48+
dspath, ok := c.Flag.Lookup("d").Value.Get().(string)
49+
if !ok {
50+
return errors.New("failed to parse datastore flag")
51+
}
52+
5253
fi, err := os.Lstat(filename)
5354
force, ok := c.Flag.Lookup("f").Value.Get().(bool)
5455
if !ok {
@@ -62,13 +63,27 @@ func initCmd(c *commander.Command, inp []string) error {
6263
cfg := new(config.Config)
6364

6465
cfg.Datastore = config.Datastore{}
65-
dspath, err := u.TildeExpansion("~/.go-ipfs/datastore")
66-
if err != nil {
67-
return err
66+
if len(dspath) == 0 {
67+
dspath, err = config.DataStorePath("")
68+
if err != nil {
69+
return err
70+
}
6871
}
6972
cfg.Datastore.Path = dspath
7073
cfg.Datastore.Type = "leveldb"
7174

75+
// Construct the data store if missing
76+
if err := os.MkdirAll(dspath, os.ModePerm); err != nil {
77+
return err
78+
}
79+
80+
// Check the directory is writeable
81+
if f, err := os.Create(filepath.Join(dspath, "._check_writeable")); err == nil {
82+
os.Remove(f.Name())
83+
} else {
84+
return errors.New("Datastore '" + dspath + "' is not writeable")
85+
}
86+
7287
cfg.Identity = config.Identity{}
7388

7489
// setup the node addresses.
@@ -114,12 +129,7 @@ func initCmd(c *commander.Command, inp []string) error {
114129
},
115130
}
116131

117-
path, err := u.TildeExpansion(config.DefaultConfigFilePath)
118-
if err != nil {
119-
return err
120-
}
121-
122-
err = config.WriteConfigFile(path, cfg)
132+
err = config.WriteConfigFile(filename, cfg)
123133
if err != nil {
124134
return err
125135
}

cmd/ipfs/ipfs.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ Use "ipfs help <command>" for more information about a command.
5353
}
5454

5555
func init() {
56-
CmdIpfs.Flag.String("c", config.DefaultPathRoot, "specify config directory")
56+
config, err := config.PathRoot()
57+
if err != nil {
58+
config = ""
59+
}
60+
CmdIpfs.Flag.String("c", config, "specify config directory")
5761
}
5862

5963
func ipfsCmd(c *commander.Command, args []string) error {
@@ -74,7 +78,12 @@ func main() {
7478
}
7579

7680
func localNode(confdir string, online bool) (*core.IpfsNode, error) {
77-
cfg, err := config.Load(confdir + "/config")
81+
filename, err := config.Filename(confdir)
82+
if err != nil {
83+
return nil, err
84+
}
85+
86+
cfg, err := config.Load(filename)
7887
if err != nil {
7988
return nil, err
8089
}
@@ -83,16 +92,19 @@ func localNode(confdir string, online bool) (*core.IpfsNode, error) {
8392
}
8493

8594
// Gets the config "-c" flag from the command, or returns
86-
// the empty string
95+
// the default configuration root directory
8796
func getConfigDir(c *commander.Command) (string, error) {
8897
conf := c.Flag.Lookup("c").Value.Get()
8998
if conf == nil {
90-
return "", nil
99+
return config.PathRoot()
91100
}
92101
confStr, ok := conf.(string)
93102
if !ok {
94103
return "", errors.New("failed to retrieve config flag value.")
95104
}
105+
if len(confStr) == 0 {
106+
return config.PathRoot()
107+
}
96108

97109
return u.TildeExpansion(confStr)
98110
}

config/config.go

+42-19
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/base64"
77
"errors"
88
"os"
9+
"path/filepath"
910

1011
u "github.com/jbenet/go-ipfs/util"
1112
)
@@ -42,11 +43,48 @@ type Config struct {
4243
Bootstrap []*BootstrapPeer // local nodes's bootstrap peers
4344
}
4445

45-
// DefaultPathRoot is the default parth for the IPFS node's root dir.
4646
const DefaultPathRoot = "~/.go-ipfs"
47+
const DefaultConfigFile = "config"
48+
const DefaultDataStoreDirectory = "datastore"
49+
const EnvDir = "IPFS_DIR"
50+
51+
// PathRoot returns the default configuration root directory
52+
func PathRoot() (string, error) {
53+
dir := os.Getenv(EnvDir)
54+
var err error
55+
if len(dir) == 0 {
56+
dir, err = u.TildeExpansion(DefaultPathRoot)
57+
}
58+
return dir, err
59+
}
60+
61+
// Path returns the path `extension` relative to the configuration root. If an
62+
// empty string is provided for `configroot`, the default root is used.
63+
func Path(configroot, extension string) (string, error) {
64+
if len(configroot) == 0 {
65+
dir, err := PathRoot()
66+
if err != nil {
67+
return "", err
68+
} else {
69+
return filepath.Join(dir, extension), nil
70+
}
71+
72+
} else {
73+
return filepath.Join(configroot, extension), nil
74+
}
75+
}
76+
77+
// DataStorePath returns the default data store path given a configuration root
78+
// (set an empty string to have the default configuration root)
79+
func DataStorePath(configroot string) (string, error) {
80+
return Path(configroot, DefaultDataStoreDirectory);
81+
}
4782

48-
// DefaultConfigFilePath points to the ipfs node config file.
49-
const DefaultConfigFilePath = DefaultPathRoot + "/config"
83+
// Filename returns the configuration file path given a configuration root
84+
// directory. If the configuration root directory is empty, use the default one
85+
func Filename(configroot string) (string, error) {
86+
return Path(configroot, DefaultConfigFile);
87+
}
5088

5189
// DecodePrivateKey is a helper to decode the users PrivateKey
5290
func (i *Identity) DecodePrivateKey(passphrase string) (crypto.PrivateKey, error) {
@@ -60,30 +98,15 @@ func (i *Identity) DecodePrivateKey(passphrase string) (crypto.PrivateKey, error
6098
return x509.ParsePKCS1PrivateKey(pkb)
6199
}
62100

63-
// Filename returns the proper tilde expanded config filename.
64-
func Filename(filename string) (string, error) {
65-
if len(filename) == 0 {
66-
filename = DefaultConfigFilePath
67-
}
68-
69-
// tilde expansion on config file
70-
return u.TildeExpansion(filename)
71-
}
72-
73101
// Load reads given file and returns the read config, or error.
74102
func Load(filename string) (*Config, error) {
75-
filename, err := Filename(filename)
76-
if err != nil {
77-
return nil, err
78-
}
79-
80103
// if nothing is there, fail. User must run 'ipfs init'
81104
if _, err := os.Stat(filename); os.IsNotExist(err) {
82105
return nil, errors.New("ipfs not initialized, please run 'ipfs init'")
83106
}
84107

85108
var cfg Config
86-
err = ReadConfigFile(filename, &cfg)
109+
err := ReadConfigFile(filename, &cfg)
87110
if err != nil {
88111
return nil, err
89112
}

0 commit comments

Comments
 (0)