Skip to content

Commit 147ab0c

Browse files
committed
refactor: simplify run_command and run_service examples
1 parent 22854bb commit 147ab0c

File tree

2 files changed

+43
-41
lines changed

2 files changed

+43
-41
lines changed

examples/run_command.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,40 @@
44
import asyncio
55
from gitpod import Gitpod
66
import gitpod.lib as util
7+
from gitpod.types.environment_spec_param import EnvironmentSpecParam
78

8-
# Example: ./examples/run_command.py https://github.com/gitpod-io/empty 'echo "Hello World!"'
9+
# Examples:
10+
# - ./examples/run_command.py 'echo "Hello World!"'
11+
# - ./examples/run_command.py 'echo "Hello World!"' https://github.com/gitpod-io/empty
912
async def main() -> None:
1013
client = Gitpod()
1114

12-
if len(sys.argv) < 3:
13-
print("Usage: ./examples/run_command.py <CONTEXT_URL> <COMMAND>")
15+
if len(sys.argv) < 2:
16+
print("Usage: ./examples/run_command.py '<COMMAND>' [CONTEXT_URL]")
1417
sys.exit(1)
15-
context_url = sys.argv[1]
16-
command = " ".join(sys.argv[2:])
18+
19+
command = sys.argv[1]
20+
context_url = sys.argv[2] if len(sys.argv) > 2 else None
1721

1822
env_class = util.find_most_used_environment_class(client)
1923
if not env_class:
2024
print("Error: No environment class found. Please create one first.")
2125
sys.exit(1)
2226

23-
environment_id = client.environments.create(
24-
spec={
25-
"desired_phase": "ENVIRONMENT_PHASE_RUNNING",
26-
"content": {
27-
"initializer": {"specs": [{
28-
"contextUrl": {
29-
"url": context_url
30-
}
31-
}]}
32-
},
33-
"machine": {"class": env_class.id},
27+
spec: EnvironmentSpecParam = {
28+
"desired_phase": "ENVIRONMENT_PHASE_RUNNING",
29+
"machine": {"class": env_class.id},
30+
}
31+
if context_url:
32+
spec["content"] = {
33+
"initializer": {"specs": [{
34+
"contextUrl": {
35+
"url": context_url
36+
}
37+
}]}
3438
}
35-
).environment.id
3639

40+
environment_id = client.environments.create(spec=spec).environment.id
3741
try:
3842
util.wait_for_environment_ready(client, environment_id)
3943

examples/run_service.py

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
import asyncio
55
from gitpod import Gitpod
66
import gitpod.lib as util
7+
from gitpod.types.environment_spec_param import EnvironmentSpecParam
78

8-
# Example: ./examples/run_service.py https://github.com/gitpod-io/empty
9+
# Examples:
10+
# - ./examples/run_service.py
11+
# - ./examples/run_service.py https://github.com/gitpod-io/empty
912
async def main() -> None:
1013
client = Gitpod()
1114

12-
if len(sys.argv) < 2:
13-
print("Usage: ./examples/run_service.py <CONTEXT_URL>")
14-
sys.exit(1)
15-
context_url = sys.argv[1]
15+
context_url = sys.argv[1] if len(sys.argv) > 1 else None
1616

1717
env_class = util.find_most_used_environment_class(client)
1818
if not env_class:
@@ -21,27 +21,25 @@ async def main() -> None:
2121

2222
port = 8888
2323

24-
environment_id = client.environments.create(
25-
spec={
26-
"desired_phase": "ENVIRONMENT_PHASE_RUNNING",
27-
"content": {
28-
"initializer": {"specs": [{
29-
"contextUrl": {
30-
"url": context_url
31-
}
32-
}]}
33-
},
34-
"machine": {"class": env_class.id},
35-
"ports": [
36-
{
37-
"name": "Lama Service",
38-
"port": port,
39-
"admission": "ADMISSION_LEVEL_EVERYONE"
24+
spec: EnvironmentSpecParam = {
25+
"desired_phase": "ENVIRONMENT_PHASE_RUNNING",
26+
"machine": {"class": env_class.id},
27+
"ports": [{
28+
"name": "Lama Service",
29+
"port": port,
30+
"admission": "ADMISSION_LEVEL_EVERYONE"
31+
}]
32+
}
33+
if context_url:
34+
spec["content"] = {
35+
"initializer": {"specs": [{
36+
"contextUrl": {
37+
"url": context_url
4038
}
41-
]
42-
}
43-
).environment.id
39+
}]}
40+
}
4441

42+
environment_id = client.environments.create(spec=spec).environment.id
4543
try:
4644
util.wait_for_environment_ready(client, environment_id)
4745

0 commit comments

Comments
 (0)