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

import (
4
	u "github.com/jbenet/go-ipfs/util"
5
	"os"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
6 7
)

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

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

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
19
// Config is used to load IPFS config files.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
20
type Config struct {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
21 22
	Identity  *Identity
	Datastore *Datastore
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
23 24
}

25
var defaultConfigFilePath = "~/.go-ipfs/config"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
26 27 28 29 30 31 32 33 34
var defaultConfigFile = `{
  "identity": {},
  "datastore": {
    "type": "leveldb",
    "path": "~/.go-ipfs/datastore"
  }
}
`

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
35 36
// Filename returns the proper tilde expanded config filename.
func Filename(filename string) (string, error) {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
37
	if len(filename) == 0 {
38
		filename = defaultConfigFilePath
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
39 40
	}

41
	// tilde expansion on config file
42 43 44
	return u.TildeExpansion(filename)
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
45 46 47
// Load reads given file and returns the read config, or error.
func Load(filename string) (*Config, error) {
	filename, err := Filename(filename)
48 49
	if err != nil {
		return nil, err
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
50 51
	}

Aaron Hill's avatar
Aaron Hill committed
52
	// if nothing is there, write first config file.
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
53
	if _, err := os.Stat(filename); os.IsNotExist(err) {
54 55 56
		if err := WriteFile(filename, []byte(defaultConfigFile)); err != nil {
			return nil, err
		}
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
57 58 59
	}

	var cfg Config
60 61 62 63 64 65 66
	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
67 68 69 70 71
	if err != nil {
		return nil, err
	}

	return &cfg, err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
72
}
73 74 75 76 77

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