-
Notifications
You must be signed in to change notification settings - Fork 192
/
environment.go
79 lines (62 loc) · 2.16 KB
/
environment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package env
import (
"os"
"github.com/celo-org/celo-blockchain/core"
"github.com/celo-org/celo-blockchain/internal/fileutils"
"github.com/celo-org/celo-blockchain/mycelo/internal/utils"
)
// Environment represents a mycelo environment
// which is the home to any mycelo operation within a computer
type Environment struct {
paths paths
Config Config
}
// New creates a new environment
func New(envpath string, cfg *Config) (*Environment, error) {
env := &Environment{
paths: paths{Workdir: envpath},
Config: *cfg,
}
return env, nil
}
// Load will load an environment located in envpath folder
func Load(envpath string) (*Environment, error) {
env := &Environment{
paths: paths{Workdir: envpath},
}
if err := utils.ReadJson(&env.Config, env.paths.envJSON()); err != nil {
return nil, err
}
return env, nil
}
// Save will save environment's config into the environment folder
func (env *Environment) Save() error {
env.ensureWorkdir()
if err := utils.WriteJson(env.Config, env.paths.envJSON()); err != nil {
return err
}
return nil
}
// Accounts retrieves accounts config
func (env *Environment) Accounts() *AccountsConfig { return &env.Config.Accounts }
// GenesisPath returns the paths to the genesis.json file (if present on the environment)
func (env *Environment) GenesisPath() string { return env.paths.genesisJSON() }
// ValidatorDatadir returns the datadir that mycelo uses to run the validator-[idx]
func (env *Environment) ValidatorDatadir(idx int) string { return env.paths.validatorDatadir(idx) }
// ValidatorIPC returns the ipc path to validator-[idx]
func (env *Environment) ValidatorIPC(idx int) string { return env.paths.validatorIPC(idx) }
// IPC returns the IPC path to the first validator
func (env *Environment) IPC() string { return env.paths.validatorIPC(0) }
// SaveGenesis writes genesis.json within the env.json
func (env *Environment) SaveGenesis(genesis *core.Genesis) error {
env.ensureWorkdir()
if err := utils.WriteJson(genesis, env.paths.genesisJSON()); err != nil {
return err
}
return nil
}
func (env *Environment) ensureWorkdir() {
if !fileutils.FileExists(env.paths.Workdir) {
os.MkdirAll(env.paths.Workdir, os.ModePerm)
}
}