-
Notifications
You must be signed in to change notification settings - Fork 1
/
sgsconfig.go
161 lines (137 loc) · 3.59 KB
/
sgsconfig.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
package main
import (
"flag"
"log"
"os"
"gopkg.in/yaml.v3"
)
type SGSConfig struct {
// Common fields
Name string `yaml:"name"`
Server string `yaml:"server"`
Workspace string `yaml:"workspace"`
// Job specific fields
VolumeMounts []struct {
Name string `yaml:"name"`
MountPath string `yaml:"mountPath"`
} `yaml:"volumeMounts"`
GPU string `yaml:"gpu"`
Image string `yaml:"image"`
WorkingDir string `yaml:"workingDir"`
Command []string `yaml:"command"`
Env []struct {
Name string `yaml:"name"`
Value string `yaml:"value"`
} `yaml:"env"`
Secret string `yaml:"secret"`
// Volume specific fields
Size string `yaml:"size"`
// Job & Volume specific fields
CPU string `yaml:"cpu"`
Memory string `yaml:"memory"`
// Secret specific fields
Data map[string]string `yaml:"data"`
}
func ParseSGSConfig() (string, string, SGSConfig) {
// TODO: flag cannot handle the case where behavior and subject are provided before the flags
// Read the name from command line flag
name := flag.String("n", "", "Name")
// Read the server from command line flag
server := flag.String("s", "", "Server")
// Read the workspace from command line flag
workspace := flag.String("w", "", "Workspace")
// Read the file name from command line flag
fileName := flag.String("f", "", "Path to the YAML file")
flag.Parse()
// Read non-flag command line arguments
args := flag.Args()
// Check the number of command line arguments
if len(args) != 2 {
log.Fatalf("Usage: %s <behavior> <subject>", os.Args[0])
}
// Read the behavior and subject from command line arguments
behavior := args[0]
subject := args[1]
// Read the YAML file
yamlFile, err := os.ReadFile(*fileName)
if err != nil {
log.Fatalf("Failed to read YAML file: %v", err)
}
// Parse the YAML data into a struct
var sgsConfig SGSConfig
err = yaml.Unmarshal(yamlFile, &sgsConfig)
if err != nil {
log.Fatalf("Failed to parse YAML: %v", err)
}
// Override the name if provided
if *name != "" {
sgsConfig.Name = *name
}
// Override the server if provided
if *server != "" {
sgsConfig.Server = *server
}
// Override the workspace if provided
if *workspace != "" {
sgsConfig.Workspace = *workspace
}
return behavior, subject, sgsConfig
}
func CheckSGSConfig(behavior string, subject string, sgsConfig SGSConfig) {
// Check the common fields
if sgsConfig.Name == "" {
log.Fatalf("Name is required")
}
if sgsConfig.Server == "" {
log.Fatalf("Server is required")
}
if sgsConfig.Workspace == "" {
log.Fatalf("Workspace is required")
}
// Check the behavior
switch behavior {
case "create":
switch subject {
case "job":
if sgsConfig.Image == "" {
log.Fatalf("Image is required for creating a job")
}
if len(sgsConfig.Command) == 0 {
log.Fatalf("Command is required for creating a job")
}
case "volume":
if sgsConfig.Size == "" {
log.Fatalf("Size is required for creating a volume")
}
case "secret":
if len(sgsConfig.Data) == 0 {
log.Fatalf("Data is required for creating a secret")
}
default:
log.Fatalf("Invalid subject: %s", subject)
}
case "delete":
// No additional fields are required for deleting a job, volume, or secret
switch subject {
case "job":
case "volume":
case "secret":
default:
log.Fatalf("Invalid subject: %s", subject)
}
case "log":
switch subject {
case "job":
default:
log.Fatalf("Invalid subject: %s", subject)
}
case "connect":
switch subject {
case "volume":
default:
log.Fatalf("Invalid subject: %s", subject)
}
default:
log.Fatalf("Invalid behavior: %s", behavior)
}
}