-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathconfig_v6.go
221 lines (205 loc) · 7.88 KB
/
config_v6.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package config
import(
"path/filepath"
Utils "github.com/matthieudelaro/nut/utils"
containerFilepath "github.com/matthieudelaro/nut/container/filepath"
log "github.com/Sirupsen/logrus"
"strings"
"errors"
)
type VolumeV6 struct {
VolumeBase `yaml:"inheritedValues,inline"`
Host string `yaml:host_path`
Container string `yaml:container_path`
}
func (self *VolumeV6) getFullHostPath(context Utils.Context) (string, error) {
if self.Host == "" {
return "", errors.New("Undefined host path")
} else {
// log.Debug("getFullHostPath value ", self.Host)
res := filepath.Clean(self.Host)
// log.Debug("getFullHostPath clean ", res)
if !filepath.IsAbs(res) {
// log.Debug("getFullHostPath is not Abs")
res = filepath.Join(context.GetRootDirectory(), res)
}
// log.Debug("getFullHostPath value ", res)
if strings.Contains(res, `:\`) { // path on windows. Eg: C:\\Users\
// log.Debug("getFullHostPath windows ", `:\`)
parts := strings.Split(res, `:\`)
parts[0] = strings.ToLower(parts[0]) // drive letter should be lower case
res = "//" + parts[0] + "/" + filepath.ToSlash(parts[1])
}
log.Debug("getFullHostPath res ", res)
return res, nil
}
}
func (self *VolumeV6) getFullContainerPath(context Utils.Context) (string, error) {
if self.Container == "" {
return "", errors.New("Undefined container path")
} else {
// log.Debug("getFullContainerPath value ", self.Container)
clean := containerFilepath.ToSlash(containerFilepath.Clean(self.Container))
// log.Debug("getFullContainerPath clean ", clean)
if containerFilepath.IsAbs(clean) {
// log.Debug("getFullContainerPath isAbs")
return clean, nil
} else {
// log.Debug("getFullContainerPath is not Abs")
log.Debug("getFullContainerPath return ", containerFilepath.Join(context.GetRootDirectory(), clean))
return containerFilepath.Join(context.GetRootDirectory(), clean), nil
}
}
}
type BaseEnvironmentV6 struct {
BaseEnvironmentBase `yaml:"inheritedValues,inline"`
FilePath string `yaml:"nut_file_path,omitempty"`
GitHub string `yaml:"github,omitempty"`
}
func (self *BaseEnvironmentV6) getFilePath() string{
return self.FilePath
}
func (self *BaseEnvironmentV6) getGitHub() string{
return self.GitHub
}
type ConfigV6 struct {
ConfigBase `yaml:"inheritedValues,inline"`
DockerImage string `yaml:"docker_image,omitempty"`
Mount map[string][]string `yaml:"mount,omitempty"`
WorkingDir string `yaml:"container_working_directory,omitempty"`
EnvironmentVariables map[string]string `yaml:"environment,omitempty"`
Ports []string `yaml:"ports,omitempty"`
EnableGUI string `yaml:"enable_gui,omitempty"`
EnableNvidiaDevices string `yaml:"enable_nvidia_devices,omitempty"`
Privileged string `yaml:"privileged,omitempty"`
SecurityOpts []string `yaml:"security_opts,omitempty"`
parent Config
}
func (self *ConfigV6) getDockerImage() string {
return self.DockerImage
}
func (self *ConfigV6) getParent() Config {
return self.parent
}
func (self *ConfigV6) getWorkingDir() string {
return self.WorkingDir
}
func (self *ConfigV6) getVolumes() map[string]Volume {
cacheVolumes := make(map[string]Volume)
for name, data := range(self.Mount) {
cacheVolumes[name] = &VolumeV6{
Host: data[0],
Container: data[1],
}
}
return cacheVolumes
}
func (self *ConfigV6) getEnvironmentVariables() map[string]string {
return self.EnvironmentVariables
}
func (self *ConfigV6) getPorts() []string {
return self.Ports
}
func (self *ConfigV6) getEnableGui() (bool, bool) {
return TruthyString(self.EnableGUI)
}
func (self *ConfigV6) getEnableNvidiaDevices() (bool, bool) {
return TruthyString(self.EnableNvidiaDevices)
}
func (self *ConfigV6) getPrivileged() (bool, bool) {
return TruthyString(self.Privileged)
}
func (self *ConfigV6) getSecurityOpts() []string {
return self.SecurityOpts
}
type ProjectV6 struct {
SyntaxVersion string `yaml:"syntax_version"`
ProjectName string `yaml:"project_name"`
Base BaseEnvironmentV6 `yaml:"based_on,omitempty"`
Macros map[string]*MacroV6 `yaml:"macros,omitempty"`
parent Project
ProjectBase `yaml:"inheritedValues,inline"`
ConfigV6 `yaml:"inheritedValues,inline"`
}
func (self *ProjectV6) getSyntaxVersion() string {
return self.SyntaxVersion
}
func (self *ProjectV6) getProjectName() string {
return self.ProjectName
}
func (self *ProjectV6) getBaseEnv() BaseEnvironment {
return &self.Base
}
func (self *ProjectV6) getMacros() map[string]Macro {
// make the list of macros
cacheMacros := make(map[string]Macro)
for name, data := range self.Macros {
data.parent = self
cacheMacros[name] = data
}
return cacheMacros
}
// func (self *ProjectV6) createMacro(usage string, commands []string) Macro {
// return &MacroV6 {
// ConfigV6: *NewConfigV6(self,),
// Usage: usage,
// Actions: commands,
// }
// }
func (self *ProjectV6) getParent() Config {
return self.parent
}
func (self *ProjectV6) getParentProject() Project {
return self.parent
}
func (self *ProjectV6) setParentProject(project Project) {
self.parent = project
}
type MacroV6 struct {
// A short description of the usage of this macro
Usage string `yaml:"usage,omitempty"`
// The commands to execute when this macro is invoked
Actions []string `yaml:"actions,omitempty"`
// A list of aliases for the macro
Aliases []string `yaml:"aliases,omitempty"`
// Custom text to show on USAGE section of help
UsageText string `yaml:"usage_for_help_section,omitempty"`
// A longer explanation of how the macro works
Description string `yaml:"description,omitempty"`
MacroBase `yaml:"inheritedValues,inline"`
ConfigV6 `yaml:"inheritedValues,inline"`
}
func (self *MacroV6) setParentProject(project Project) {
self.ConfigV6.parent = project
}
func (self *MacroV6) getUsage() string {
return self.Usage
}
func (self *MacroV6) getActions() []string {
return self.Actions
}
func (self *MacroV6) getAliases() []string {
return self.Aliases
}
func (self *MacroV6) getUsageText() string {
return self.UsageText
}
func (self *MacroV6) getDescription() string {
return self.Description
}
func NewConfigV6(parent Config) *ConfigV6 {
return &ConfigV6{
Mount: make(map[string][]string),
EnvironmentVariables: map[string]string{},
parent: parent,
}
}
func NewProjectV6(parent Project) *ProjectV6 {
project := &ProjectV6 {
SyntaxVersion: "6",
Macros: make(map[string]*MacroV6),
ConfigV6: *NewConfigV6(nil),
parent: parent,
}
return project
}