Skip to content

Commit

Permalink
Implement XDG base directory specification
Browse files Browse the repository at this point in the history
  • Loading branch information
Shanti Bouchez-Mongardé committed Sep 25, 2014
1 parent ac7404a commit 557469a
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 22 deletions.
2 changes: 1 addition & 1 deletion cmd/ipfs/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func addCmd(c *commander.Command, inp []string) error {
err := daemon.SendCommand(cmd, "localhost:12345")
if err != nil {
// Do locally
conf, err := getConfigDir(c.Parent)
conf, err := getConfigFlag(c.Parent)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/ipfs/cat.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func catCmd(c *commander.Command, inp []string) error {

err := daemon.SendCommand(com, "localhost:12345")
if err != nil {
conf, err := getConfigDir(c.Parent)
conf, err := getConfigFlag(c.Parent)
if err != nil {
return err
}
Expand Down
17 changes: 12 additions & 5 deletions cmd/ipfs/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,32 @@ func init() {
cmdIpfsInit.Flag.Int("b", 4096, "number of bits for keypair")
cmdIpfsInit.Flag.String("p", "", "passphrase for encrypting keys")
cmdIpfsInit.Flag.Bool("f", false, "force overwrite of existing config")
cmdIpfsInit.Flag.String("d", "", "Change default datastore location")
}

func initCmd(c *commander.Command, inp []string) error {
configpath, err := getConfigDir(c.Parent)
configpath, err := getConfigFlag(c.Parent)
if err != nil {
return err
}
if configpath == "" {
configpath, err = u.TildeExpansion("~/.go-ipfs")
configpath, err = config.WriteConfigFilePath()
if err != nil {
return err
}
}

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

dataStorePath, ok := c.Flag.Lookup("d").Value.Get().(string)
if !ok {
return errors.New("failed to parse datastore flag")
}

fi, err := os.Lstat(filename)
force, ok := c.Flag.Lookup("f").Value.Get().(bool)
if !ok {
Expand All @@ -62,7 +68,8 @@ func initCmd(c *commander.Command, inp []string) error {
cfg := new(config.Config)

cfg.Datastore = config.Datastore{}
dspath, err := u.TildeExpansion("~/.go-ipfs/datastore")
cfg.Datastore.Path = dataStorePath
dspath, err := cfg.Datastore.GetPath()
if err != nil {
return err
}
Expand Down Expand Up @@ -113,7 +120,7 @@ func initCmd(c *commander.Command, inp []string) error {
},
}

path, err := u.TildeExpansion(config.DefaultConfigFilePath)
path, err := config.WriteConfigFilePath()
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/ipfs/ipfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Use "ipfs help <command>" for more information about a command.
}

func init() {
CmdIpfs.Flag.String("c", config.DefaultPathRoot, "specify config directory")
CmdIpfs.Flag.String("c", "", "specify config file")
}

func ipfsCmd(c *commander.Command, args []string) error {
Expand All @@ -73,8 +73,8 @@ func main() {
return
}

func localNode(confdir string, online bool) (*core.IpfsNode, error) {
cfg, err := config.Load(confdir + "/config")
func localNode(confFile string, online bool) (*core.IpfsNode, error) {
cfg, err := config.Load(confFile)
if err != nil {
return nil, err
}
Expand All @@ -84,7 +84,7 @@ func localNode(confdir string, online bool) (*core.IpfsNode, error) {

// Gets the config "-c" flag from the command, or returns
// the empty string
func getConfigDir(c *commander.Command) (string, error) {
func getConfigFlag(c *commander.Command) (string, error) {
conf := c.Flag.Lookup("c").Value.Get()
if conf == nil {
return "", nil
Expand Down
2 changes: 1 addition & 1 deletion cmd/ipfs/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func lsCmd(c *commander.Command, inp []string) error {
com.Args = inp
err := daemon.SendCommand(com, "localhost:12345")
if err != nil {
conf, err := getConfigDir(c.Parent)
conf, err := getConfigFlag(c.Parent)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/ipfs/mount_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func mountCmd(c *commander.Command, inp []string) error {
return nil
}

conf, err := getConfigDir(c.Parent)
conf, err := getConfigFlag(c.Parent)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/ipfs/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func refCmd(c *commander.Command, inp []string) error {
err := daemon.SendCommand(cmd, "localhost:12345")
if err != nil {
// Do locally
conf, err := getConfigDir(c.Parent)
conf, err := getConfigFlag(c.Parent)
if err != nil {
return err
}
Expand Down
28 changes: 22 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"crypto"
"crypto/x509"
"encoding/base64"
"path/filepath"
"errors"
"os"

u "github.com/jbenet/go-ipfs/util"
u "github.com/jbenet/go-ipfs/util"
xdg "github.com/mildred/go-xdg"
)

// Identity tracks the configuration of the local node's identity.
Expand Down Expand Up @@ -36,17 +38,31 @@ type Config struct {
Peers []*SavedPeer // local nodes's bootstrap peers
}

const DefaultPathRoot = "~/.go-ipfs"
const DefaultConfigFilePath = DefaultPathRoot + "/config"
const DefaultConfigFile = `{
"identity": {},
"datastore": {
"type": "leveldb",
"path": "` + DefaultPathRoot + `/datastore"
"path": "datastore"
}
}
`

func (ds Datastore) GetPath() (string, error) {
path := ds.Path
if len(path) == 0 {
path = "go-ipfs/datastore"
}
if filepath.IsAbs(path) {
return path, nil
} else {
return xdg.Data.EnsureDir(path);
}
}

func WriteConfigFilePath() (string, error) {
return xdg.Config.FindHome("go-ipfs/config")
}

func (i *Identity) DecodePrivateKey(passphrase string) (crypto.PrivateKey, error) {
pkb, err := base64.StdEncoding.DecodeString(i.PrivKey)
if err != nil {
Expand All @@ -61,14 +77,14 @@ func (i *Identity) DecodePrivateKey(passphrase string) (crypto.PrivateKey, error
// Filename returns the proper tilde expanded config filename.
func Filename(filename string) (string, error) {
if len(filename) == 0 {
filename = DefaultConfigFilePath
return xdg.Config.Find("go-ipfs/config")
}

// tilde expansion on config file
return u.TildeExpansion(filename)
}

// Load reads given file and returns the read config, or error.
// Load reads given file (empty string to get default file) and returns the read config, or error.
func Load(filename string) (*Config, error) {
filename, err := Filename(filename)
if err != nil {
Expand Down
9 changes: 7 additions & 2 deletions core/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@ func makeDatastore(cfg config.Datastore) (ds.Datastore, error) {
}

func makeLevelDBDatastore(cfg config.Datastore) (ds.Datastore, error) {
if len(cfg.Path) == 0 {
path, err := cfg.GetPath()
if err != nil {
return nil, err
}

if len(path) == 0 {
return nil, fmt.Errorf("config datastore.path required for leveldb")
}

return lds.NewDatastore(cfg.Path, nil)
return lds.NewDatastore(path, nil)
}

0 comments on commit 557469a

Please sign in to comment.