Skip to content

Commit d7c7d5c

Browse files
feat: add output format option for list-jobs command
1 parent b1a5e32 commit d7c7d5c

File tree

1 file changed

+29
-1
lines changed
  • src/hyperpod_cli/commands

1 file changed

+29
-1
lines changed

src/hyperpod_cli/commands/job.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import sys
2020
import subprocess
2121
from typing import Any, Dict, Optional, List
22+
from tabulate import tabulate
2223

2324
import click
2425
import yaml
@@ -40,6 +41,7 @@
4041
SAGEMAKER_QUOTA_ALLOCATION_LABEL,
4142
JobPatchType,
4243
SAGEMAKER_TRAINING_LAUNCHER_DIR,
44+
OutputFormat,
4345
PullPolicy,
4446
RestartPolicy,
4547
PersistentVolumeClaim,
@@ -170,6 +172,13 @@ def get_job(
170172
required=False,
171173
help="Optional. A label selector to filter the listed jobs. The selector supports the '=', '==', and '!=' operators (e.g., `-l key1=value1,key2=value2`).",
172174
)
175+
@click.option(
176+
"--output",
177+
type=click.Choice([c.value for c in OutputFormat]),
178+
required=False,
179+
default=OutputFormat.JSON.value,
180+
help="Optional. The output format. Available values are `TABLE` and `JSON`. The default value is `JSON`.",
181+
)
173182
@click.option(
174183
"--debug",
175184
is_flag=True,
@@ -179,6 +188,7 @@ def list_jobs(
179188
namespace: Optional[str],
180189
all_namespaces: Optional[bool],
181190
selector: Optional[str],
191+
output: Optional[str],
182192
debug: bool,
183193
):
184194
if debug:
@@ -191,7 +201,25 @@ def list_jobs(
191201
result = list_training_job_service.list_training_jobs(
192202
namespace, all_namespaces, selector
193203
)
194-
click.echo(result)
204+
result_dict = json.loads(result)
205+
206+
jobs = []
207+
if "jobs" in result_dict and isinstance(result_dict["jobs"], list):
208+
jobs = [job.values() for job in result_dict["jobs"]]
209+
210+
headers = [
211+
"Name",
212+
"Namespace",
213+
"CreationTime",
214+
"State"
215+
]
216+
217+
if output == OutputFormat.TABLE.value:
218+
click.echo(tabulate(jobs, headers=headers, tablefmt="presto"))
219+
elif output == OutputFormat.JSON.value:
220+
json_list = [dict(zip(headers, value)) for value in jobs]
221+
json_jobs = {"jobs": json_list}
222+
click.echo(json.dumps(json_jobs, indent=4))
195223
except Exception as e:
196224
sys.exit(f"Unexpected error happens when trying to list training job : {e}")
197225

0 commit comments

Comments
 (0)