diff --git a/cmd/ipfs/init.go b/cmd/ipfs/init.go index 77f3fb9f031..839ee2c9d1b 100644 --- a/cmd/ipfs/init.go +++ b/cmd/ipfs/init.go @@ -32,9 +32,13 @@ func init() { } func initCmd(c *commander.Command, inp []string) error { - _, err := os.Lstat(config.DefaultConfigFilePath) + filename, err := config.Filename(config.DefaultConfigFilePath) + if err != nil { + return errors.New("Couldn't get home directory path") + } + fi, err := os.Lstat(filename) force := c.Flag.Lookup("f").Value.Get().(bool) - if err != nil && !force { + if fi != nil || (err != nil && !os.IsNotExist(err)) && !force { return errors.New("ipfs configuration file already exists!\nReinitializing would overwrite your keys.\n(use -f to force overwrite)") } cfg := new(config.Config) diff --git a/config/config.go b/config/config.go index b80ff1a2c49..76088da1f0e 100644 --- a/config/config.go +++ b/config/config.go @@ -34,12 +34,13 @@ type Config struct { Peers []*SavedPeer } -var DefaultConfigFilePath = "~/.go-ipfs/config" -var DefaultConfigFile = `{ +const DefaultPathRoot = "~/.go-ipfs" +const DefaultConfigFilePath = DefaultPathRoot + "/config" +const DefaultConfigFile = `{ "identity": {}, "datastore": { "type": "leveldb", - "path": "~/.go-ipfs/datastore" + "path": "` + DefaultPathRoot + `/datastore" } } ` diff --git a/config/serialize.go b/config/serialize.go index bf495a2603d..647e19e3305 100644 --- a/config/serialize.go +++ b/config/serialize.go @@ -22,6 +22,11 @@ func ReadConfigFile(filename string, cfg interface{}) error { // WriteConfigFile writes the config from `cfg` into `filename`. func WriteConfigFile(filename string, cfg interface{}) error { + err := os.MkdirAll(filepath.Dir(filename), 0775) + if err != nil { + return err + } + f, err := os.Create(filename) if err != nil { return err