forked from nanobox-io/nanobox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
185 lines (152 loc) · 4.49 KB
/
config.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//
package config
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"sync"
"github.com/ghodss/yaml"
"github.com/mitchellh/go-homedir"
"github.com/nanobox-io/nanobox-golang-stylish"
)
const (
OS = runtime.GOOS
ARCH = runtime.GOARCH
LOGTAP_PORT = ":6361"
MIST_PORT = ":1445"
SERVER_PORT = ":1757"
VERSION = "0.16.9"
)
type (
exiter func(int)
)
var (
err error //
mutex *sync.Mutex
//
AppDir string // the path to the application (~/.nanobox/apps/<app>)
AppsDir string // ~/.nanobox/apps
CWDir string // the current working directory
EnginesDir string // ~/.nanobox/engines
Home string // the users home directory (~)
IP string // the guest vm's private network ip (generated from app name)
Root string // nanobox's root directory path (~/.nanobox)
UpdateFile string // the path to the .update file (~/.nanobox/.update)
//
Nanofile NanofileConfig // parsed nanofile options
VMfile VMfileConfig // parsed nanofile options
//
ServerURI string // nanobox-server host:port combo (IP:1757)
ServerURL string // nanobox-server host:port combo (IP:1757) (http)
MistURI string // mist's host:port combo (IP:1445)
LogtapURI string // logtap's host:port combo (IP:6361)
// flags
Background bool //
Devmode bool //
Force bool //
Verbose bool //
Silent bool //
LogLevel string //
//
Exit exiter = os.Exit
)
//
func init() {
// default log level
LogLevel = "info"
// set the current working directory first, as it's used in other steps of the
// configuration process
if p, err := os.Getwd(); err != nil {
Log.Fatal("[config/config] os.Getwd() failed", err.Error())
} else {
CWDir = filepath.ToSlash(p)
}
// set Home based off the users homedir (~)
if p, err := homedir.Dir(); err != nil {
Log.Fatal("[config/config] homedir.Dir() failed", err.Error())
} else {
Home = filepath.ToSlash(p)
}
// set nanobox's root directory;
Root = filepath.ToSlash(filepath.Join(Home, ".nanobox"))
// check for a ~/.nanobox dir and create one if it's not found
if _, err := os.Stat(Root); err != nil {
fmt.Printf(stylish.Bullet("Creating %s directory", Root))
if err := os.Mkdir(Root, 0755); err != nil {
Log.Fatal("[config/config] os.Mkdir() failed", err.Error())
}
}
// check for a ~/.nanobox/.update file and create one if it's not found
UpdateFile = filepath.ToSlash(filepath.Join(Root, ".update"))
if _, err := os.Stat(UpdateFile); err != nil {
f, err := os.Create(UpdateFile)
if err != nil {
Log.Fatal("[config/config] os.Create() failed", err.Error())
}
defer f.Close()
}
// check for a ~/.nanobox/engines dir and create one if it's not found
EnginesDir = filepath.ToSlash(filepath.Join(Root, "engines"))
if _, err := os.Stat(EnginesDir); err != nil {
if err := os.Mkdir(EnginesDir, 0755); err != nil {
Log.Fatal("[config/config] os.Mkdir() failed", err.Error())
}
}
// check for a ~/.nanobox/apps dir and create one if it's not found
AppsDir = filepath.ToSlash(filepath.Join(Root, "apps"))
if _, err := os.Stat(AppsDir); err != nil {
if err := os.Mkdir(AppsDir, 0755); err != nil {
Log.Fatal("[config/config] os.Mkdir() failed", err.Error())
}
}
// the .nanofile needs to be parsed right away so that its config options are
// available as soon as possible
Nanofile = ParseNanofile()
//
ServerURI = Nanofile.IP + SERVER_PORT
ServerURL = "http://" + ServerURI
MistURI = Nanofile.IP + MIST_PORT
LogtapURI = Nanofile.IP + LOGTAP_PORT
// set the 'App' first so it can be used in subsequent configurations; the 'App'
// is set to the name of the cwd; this can be overriden from a .nanofile
AppDir = filepath.ToSlash(filepath.Join(AppsDir, Nanofile.Name))
}
// ParseConfig
func ParseConfig(path string, v interface{}) error {
//
fp, err := filepath.Abs(path)
if err != nil {
return err
}
//
f, err := ioutil.ReadFile(fp)
if err != nil {
return err
}
//
return yaml.Unmarshal(f, v)
}
// writeConfig
func writeConfig(path string, v interface{}) error {
// take a config objects path and create (and truncate) the file, preparing it
// to receive new configurations
f, err := os.Create(path)
if err != nil {
Fatal("[config/config] os.Create() failed", err.Error())
}
defer f.Close()
// marshal the config object
b, err := yaml.Marshal(v)
if err != nil {
Fatal("[config/config] yaml.Marshal() failed", err.Error())
}
// mutex.Lock()
// write it back to the file
if _, err := f.Write(b); err != nil {
return err
}
// mutex.Unlock()
return nil
}