config.go 1.63 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3
package config

import (
4
	"os"
5 6

	u "github.com/jbenet/go-ipfs/util"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
7 8
)

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
9
// Identity tracks the configuration of the local node's identity.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
10
type Identity struct {
11 12
	PeerID  string
	Address string
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
13 14
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
15
// Datastore tracks the configuration of the datastore.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
16
type Datastore struct {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
17 18
	Type string
	Path string
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
19 20
}

21 22 23 24
type SavedPeer struct {
	Address string
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
25
// Config is used to load IPFS config files.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
26
type Config struct {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
27 28
	Identity  *Identity
	Datastore *Datastore
29
	Peers     []*SavedPeer
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
30 31
}

32
var defaultConfigFilePath = "~/.go-ipfs/config"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
33 34 35 36 37 38 39 40 41
var defaultConfigFile = `{
  "identity": {},
  "datastore": {
    "type": "leveldb",
    "path": "~/.go-ipfs/datastore"
  }
}
`

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
42 43
// Filename returns the proper tilde expanded config filename.
func Filename(filename string) (string, error) {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
44
	if len(filename) == 0 {
45
		filename = defaultConfigFilePath
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
46 47
	}

48
	// tilde expansion on config file
49 50 51
	return u.TildeExpansion(filename)
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
52 53 54
// Load reads given file and returns the read config, or error.
func Load(filename string) (*Config, error) {
	filename, err := Filename(filename)
55 56
	if err != nil {
		return nil, err
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
57 58
	}

Aaron Hill's avatar
Aaron Hill committed
59
	// if nothing is there, write first config file.
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
60
	if _, err := os.Stat(filename); os.IsNotExist(err) {
61 62 63
		if err := WriteFile(filename, []byte(defaultConfigFile)); err != nil {
			return nil, err
		}
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
64 65 66
	}

	var cfg Config
67 68 69 70 71 72 73
	err = ReadConfigFile(filename, &cfg)
	if err != nil {
		return nil, err
	}

	// tilde expansion on datastore path
	cfg.Datastore.Path, err = u.TildeExpansion(cfg.Datastore.Path)
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
74 75 76 77 78
	if err != nil {
		return nil, err
	}

	return &cfg, err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
79
}
80 81 82 83 84

// Set sets the value of a particular config key
func Set(filename, key, value string) error {
	return nil
}