forked from nanobox-io/nanobox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vmfile.go
99 lines (79 loc) · 2.08 KB
/
vmfile.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
//
package config
import "os"
// VMfileConfig represents all available/expected .vmfile configurable options
type VMfileConfig struct {
Background bool // is the CLI running in "background" mode
Deployed bool // was the most recent deploy successufl
Reloaded bool // did the previous CLI command cause a 'reload'
Suspendable bool // is the VM able to be suspended
}
// ParseVMfile
func ParseVMfile() (vmfile VMfileConfig) {
//
vmfilePath := AppDir + "/.vmfile"
// if a .vmfile doesn't exist - create it
if _, err := os.Stat(vmfilePath); err != nil {
vmfile.Background = false
vmfile.Deployed = false
vmfile.Reloaded = false
vmfile.Suspendable = true
writeVMfile()
return
}
// if a .vmfile does exists - parse it
if err := ParseConfig(vmfilePath, &vmfile); err != nil {
Fatal("[config/vmfile] ParseConfig() failed", err.Error())
}
return
}
//
func (c *VMfileConfig) HasDeployed() bool {
return c.parseVMfile(c.Deployed)
}
//
func (c *VMfileConfig) IsBackground() bool {
return c.parseVMfile(c.Background)
}
//
func (c *VMfileConfig) HasReloaded() bool {
return c.parseVMfile(c.Reloaded)
}
//
func (c *VMfileConfig) IsSuspendable() bool {
return c.parseVMfile(c.Suspendable)
}
//
func (c *VMfileConfig) BackgroundIs(background bool) {
c.Background = background
writeVMfile()
}
//
func (c *VMfileConfig) DeployedIs(deployed bool) {
c.Deployed = deployed
writeVMfile()
}
//
func (c *VMfileConfig) ReloadedIs(reloaded bool) {
c.Reloaded = reloaded
writeVMfile()
}
//
func (c *VMfileConfig) SuspendableIs(suspendable bool) {
c.Suspendable = suspendable
writeVMfile()
}
// parseVMfile is a wrapper that simply handles the error once rather than in
// each individual call
func (c *VMfileConfig) parseVMfile(field bool) bool {
if err := ParseConfig(AppDir+"/.vmfile", c); err != nil {
Fatal("[config/vmfile] ParseConfig() failed", err.Error())
}
return field
}
// writeVMfile writes to the vmfile with each field update
func writeVMfile() {
if err := writeConfig(AppDir+"/.vmfile", VMfile); err != nil {
Fatal("[config/vmfile] writeConfig() failed", err.Error())
}
}