Skip to content

Commit 09eb014

Browse files
committed
add kcl lab-1
1 parent 0968e88 commit 09eb014

File tree

6 files changed

+57
-3
lines changed

6 files changed

+57
-3
lines changed

docs/kcl.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ brew install kcl-lang/tap/kcl
1717

1818
```sh
1919
# execute program and output yaml
20-
kcl kcl/hello.k
21-
kcl kcl/server-1.k
22-
kcl kcl/server-2.k
20+
kcl kcl/examples/hello.k
21+
kcl kcl/examples/server-1.k
22+
kcl kcl/examples/server-2.k
23+
24+
# labs
25+
kcl kcl/lab-1/my_config.k kcl/lab-1/my_config_test.k
26+
kcl kcl/lab-1/my_config.k -D priority=2
2327
```
File renamed without changes.
File renamed without changes.
File renamed without changes.

kcl/lab-1/my_config.k

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# variables with underscore prefix are non-exported and mutable
2+
# i.e. it will not appear in the output YAML, and it can be assigned multiple times
3+
_priority = option("priority")
4+
_cpu = 256
5+
_env = "dev"
6+
7+
if _priority == 1:
8+
_cpu = 256
9+
elif _priority == 2:
10+
_cpu = 512
11+
elif _priority == 3:
12+
_cpu = 1024
13+
else:
14+
_cpu = 2048
15+
16+
_priorityCpuMap = {
17+
"1" = 256
18+
"2" = 512
19+
"3" = 1024
20+
}
21+
22+
_name = "nginx"
23+
24+
cpu = _cpu
25+
# using a dict to simplify logic and the default value is 2048
26+
cpuFromMap = _priorityCpuMap[str(_priority)] or 2048
27+
memory = _cpu * 2
28+
29+
_command = [_name] # a list
30+
_command = _command + ["-f", "file"] # Append items into command using + operator to contact two lists
31+
command = [c.lower() for c in _command] # Take each element in the list to lowercase
32+
33+
_labels = {
34+
run = "my-{}".format(_name)
35+
if _env:
36+
env = _env # Append a dict key-value pair when the _env is not None/Undefined or empty using if expressions
37+
}
38+
labels = _labels # a dict
39+
40+
image = "{}:1.14.2".format(_name) # string format
41+
service = "my-service"

kcl/lab-1/my_config_test.k

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import my_config
2+
3+
# debugging
4+
print(my_config.labels)
5+
6+
# tests
7+
assert len(my_config.labels) > 0, "labels can't be empty"
8+
assert my_config.cpu > 256, "cpu cannot be less than 256"
9+
# assert "foo" in labels, "foo label is a must"

0 commit comments

Comments
 (0)