Skip to content

Commit bab3b1e

Browse files
committed
feat(execute_command): enhance command management with update functionality and improve example usage
1 parent cb1e3e8 commit bab3b1e

File tree

2 files changed

+55
-18
lines changed

2 files changed

+55
-18
lines changed

examples/execute_command.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from gitpod import Gitpod
44
from utils.context import get_context_url, get_authenticated_user
55
from utils.environment import get_environment_class_id, get_environment_class, create_environment
6-
from utils.task import create_command, run_command, delete_command, stream_command_logs
6+
from utils.task import create_command, run_command, update_command, delete_command
77

88
def main() -> None:
99
"""
@@ -33,22 +33,21 @@ def main() -> None:
3333

3434
try:
3535
# Define command
36-
reference = "hello-world"
37-
name = "Hello, World!"
38-
description = "Prints 'Hello, World!' to the console."
39-
task_id = create_command(client, environment_id, reference, name, description)
36+
reference = "greeting"
37+
name = "Greeting"
38+
description = "Prints a greeting to the console."
39+
command = "echo 'Hello, World!'"
40+
task_id = create_command(client, environment_id, reference, name, description, command)
4041
try:
41-
# Run command
42-
execution_id = run_command(client, task_id)
43-
44-
# Stream logs
45-
stream_command_logs(client, environment_id, execution_id)
42+
# Run command with first greeting
43+
run_command(client, environment_id, task_id)
4644

47-
# Check command status
48-
execution = client.environments.automations.tasks.executions.retrieve(id=execution_id).task_execution
49-
print(f"\nTask execution status: {execution.status.phase}")
50-
if execution.status.phase == "TASK_EXECUTION_PHASE_FAILED":
51-
print(f"Task execution failed: {execution.status.failure_message}")
45+
# Update command with second greeting
46+
command = "echo 'Howdy, Partner!'"
47+
update_command(client, task_id, command)
48+
49+
# Run command with second greeting
50+
run_command(client, environment_id, task_id)
5251
finally:
5352
# Clean up command
5453
delete_command(client, task_id)

examples/utils/task.py

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from utils.environment import ensure_environment_healthy
55

66

7-
def create_command(client: Gitpod, environment_id: str, reference: str, name: str, description: str) -> str:
7+
def create_command(client: Gitpod, environment_id: str, reference: str, name: str, description: str, command: str) -> str:
88
"""
99
Create a task in the given environment.
1010
@@ -20,7 +20,7 @@ def create_command(client: Gitpod, environment_id: str, reference: str, name: st
2020
"""
2121
task = client.environments.automations.tasks.create(
2222
spec={
23-
"command": "echo 'Hello, World!'",
23+
"command": command,
2424
},
2525
environment_id=environment_id,
2626
metadata={
@@ -32,8 +32,24 @@ def create_command(client: Gitpod, environment_id: str, reference: str, name: st
3232
print(f"\nCreated task: {task.id}")
3333
return task.id
3434

35+
def update_command(client: Gitpod, task_id: str, command: str) -> None:
36+
"""
37+
Update the command of an existing task.
38+
39+
Args:
40+
client (Gitpod): The Gitpod client instance.
41+
task_id (str): The task ID.
42+
new_command (str): The new command to set.
43+
"""
44+
client.environments.automations.tasks.update(
45+
id=task_id,
46+
spec={
47+
"command": command,
48+
}
49+
)
50+
print(f"Updated task: {task_id}")
3551

36-
def run_command(client: Gitpod, task_id: str) -> str:
52+
def start_command(client: Gitpod, task_id: str) -> str:
3753
"""
3854
Start a task execution.
3955
@@ -127,3 +143,25 @@ def stream_command_logs(client: Gitpod, environment_id: str, execution_id: str)
127143

128144
logsAccessToken = client.environments.create_logs_token(environment_id=environment_id).access_token
129145
asyncio.run(stream_logs(log_url, logsAccessToken))
146+
147+
def run_command(client: Gitpod, environment_id: str, task_id: str) -> None:
148+
"""
149+
Run a command, stream its logs, and check its status.
150+
151+
Args:
152+
client (Gitpod): The Gitpod client instance.
153+
environment_id (str): The environment ID.
154+
task_id (str): The task ID.
155+
"""
156+
# Start command
157+
execution_id = start_command(client, task_id)
158+
159+
# Stream logs
160+
stream_command_logs(client, environment_id, execution_id)
161+
162+
# Check command status
163+
execution = client.environments.automations.tasks.executions.retrieve(id=execution_id).task_execution
164+
print(f"\nTask execution status: {execution.status.phase}")
165+
if execution.status.phase == "TASK_EXECUTION_PHASE_FAILED":
166+
print(f"Task execution failed: {execution.status.failure_message}")
167+

0 commit comments

Comments
 (0)