-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
89 lines (78 loc) · 2.53 KB
/
parser.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
package workspace_config
import (
"fmt"
"gopkg.in/yaml.v3"
"os"
"path/filepath"
)
type GigoVSCodeConfig struct {
Enabled bool `yaml:"enabled"`
Extensions []string `yaml:"extensions"`
}
type GigoExecConfig struct {
Name string `yaml:"name" json:"name"`
Init bool `yaml:"init" json:"init"`
Command string `yaml:"command" json:"command"`
}
type GigoPortForwardConfig struct {
Name string `yaml:"name"`
Port uint16 `yaml:"port"`
}
type GigoWorkspaceConfig struct {
Version float64 `yaml:"version"`
Resources struct {
CPU int `yaml:"cpu"`
Mem int `yaml:"mem"`
Disk int `yaml:"disk"`
GPU struct {
Count int `yaml:"count"`
Class string `yaml:"class"`
} `yaml:"gpu"`
} `yaml:"resources"`
BaseContainer string `yaml:"base_container"`
WorkingDirectory string `yaml:"working_directory"`
Environment map[string]string `yaml:"environment"`
Containers map[string]interface{} `yaml:"containers"`
VSCode GigoVSCodeConfig `yaml:"vscode"`
PortForward []GigoPortForwardConfig `yaml:"port_forward"`
Exec []GigoExecConfig `yaml:"exec"`
}
type GigoWorkspaceConfigAgent struct {
Version float64 `yaml:"version"`
WorkingDirectory string `json:"working_directory"`
Environment map[string]string `json:"environment"`
Containers map[string]interface{} `json:"containers"`
VSCode GigoVSCodeConfig `yaml:"vscode"`
PortForward []GigoPortForwardConfig `json:"port_forward"`
Exec []GigoExecConfig `json:"exec"`
}
func ParseWorkspaceConfig(dir string) (*GigoWorkspaceConfig, error) {
// read the provided config file and returns a byte array
filename, err := filepath.Abs(dir)
if err != nil {
return nil, fmt.Errorf("failed to form the absolute path: %v", err)
}
f, err := os.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("failed to read file: %v", err)
}
// create empty Config struct
var c GigoWorkspaceConfig
// decode document and assigns to cfg
err = yaml.Unmarshal(f, &c)
if err != nil {
return nil, fmt.Errorf("failed decode config file: %v", err)
}
return &c, nil
}
func (c GigoWorkspaceConfig) ToAgent() GigoWorkspaceConfigAgent {
return GigoWorkspaceConfigAgent{
Version: c.Version,
WorkingDirectory: c.WorkingDirectory,
Environment: c.Environment,
Containers: c.Containers,
VSCode: c.VSCode,
PortForward: c.PortForward,
Exec: c.Exec,
}
}