Skip to content

Commit f4dc812

Browse files
committed
Allow the format option to appear anywhere in the command hierarchy
Signed-off-by: Kevin Stanley <stanleyk@objectcomputing.com>
1 parent 29357cc commit f4dc812

7 files changed

Lines changed: 102 additions & 13 deletions

File tree

unityauth-cli/src/unityauth_cli/cli.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,48 @@ def wrapper(ctx: CLIContext, *args, **kwargs):
116116
return wrapper # type: ignore[return-value]
117117

118118

119+
def format_option(f: F) -> F:
120+
"""Decorator that adds -o/--format option to a command.
121+
122+
Allows format to be specified after the command (more intuitive):
123+
unityauth tenant list -o json
124+
125+
If provided, overrides the global format from the parent context.
126+
Must be used before @pass_context decorator.
127+
128+
Example:
129+
@click.command()
130+
@format_option
131+
@pass_context
132+
@require_auth
133+
def list_tenants(ctx: CLIContext, client: UnityAuthAPIClient) -> None:
134+
# ctx.output_format is set (from local -o or global -o or config)
135+
...
136+
"""
137+
@functools.wraps(f)
138+
def wrapper(*args, output_format: Optional[str] = None, **kwargs):
139+
# Override context format if local option provided
140+
if output_format:
141+
# Get CLIContext from Click's current context
142+
click_ctx = click.get_current_context(silent=True)
143+
if click_ctx:
144+
cli_ctx = click_ctx.find_object(CLIContext)
145+
if cli_ctx:
146+
cli_ctx.output_format = output_format.lower()
147+
148+
return f(*args, **kwargs)
149+
150+
# Apply the click option decorator
151+
decorated = click.option(
152+
'-o', '--format',
153+
'output_format',
154+
type=click.Choice(['table', 'json', 'csv'], case_sensitive=False),
155+
help='Output format (default: table)',
156+
)(wrapper)
157+
158+
return decorated # type: ignore[return-value]
159+
160+
119161
@click.group()
120162
@click.option(
121163
'--api-url',
@@ -146,7 +188,7 @@ def cli(ctx: click.Context, api_url: Optional[str], output_format: Optional[str]
146188
unityauth user create ... # Create a new user
147189
unityauth user list --tenant-id 1 # List users in tenant
148190
unityauth tenant list # List accessible tenants
149-
unityauth batch create-users FILE # Batch create from CSV
191+
unityauth role list # List available roles
150192
151193
For command-specific help:
152194
unityauth COMMAND --help

unityauth-cli/src/unityauth_cli/commands/config.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import click
1111

12-
from unityauth_cli.cli import CLIContext, console, error, info, pass_context, success
12+
from unityauth_cli.cli import CLIContext, console, error, format_option, info, pass_context, success
1313
from unityauth_cli.formatters.table import format_key_value_table
1414
from unityauth_cli.formatters.json_fmt import format_json
1515

@@ -31,6 +31,7 @@ def config() -> None:
3131

3232

3333
@config.command()
34+
@format_option
3435
@pass_context
3536
def show(ctx: CLIContext) -> None:
3637
"""Display current configuration settings.
@@ -40,7 +41,7 @@ def show(ctx: CLIContext) -> None:
4041
\b
4142
Examples:
4243
unityauth config show
43-
unityauth config show --format json
44+
unityauth config show -o json
4445
"""
4546
try:
4647
if not ctx.config:

unityauth-cli/src/unityauth_cli/commands/login.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
CLIContext,
1414
console,
1515
error,
16+
format_option,
1617
handle_error,
1718
info,
1819
pass_context,
@@ -141,6 +142,7 @@ def logout(ctx: CLIContext) -> None:
141142

142143

143144
@click.command(name='token-info')
145+
@format_option
144146
@pass_context
145147
@require_auth
146148
def token_info(ctx: CLIContext, client: UnityAuthAPIClient) -> None:
@@ -152,7 +154,7 @@ def token_info(ctx: CLIContext, client: UnityAuthAPIClient) -> None:
152154
\b
153155
Examples:
154156
unityauth token-info
155-
unityauth token-info --format json
157+
unityauth token-info -o json
156158
"""
157159
try:
158160
# Make token_info request

unityauth-cli/src/unityauth_cli/commands/permissions.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
CLIContext,
1212
console,
1313
error,
14+
format_option,
1415
handle_error,
1516
info,
1617
pass_context,
@@ -28,6 +29,7 @@
2829
@click.command('list')
2930
@click.option('-t', '--tenant-id', required=True, type=int, help='Tenant ID to check permissions for')
3031
@click.option('-s', '--service-id', required=True, type=int, help='Service ID to check permissions for')
32+
@format_option
3133
@pass_context
3234
@require_auth
3335
def list_permissions(
@@ -41,10 +43,15 @@ def list_permissions(
4143
Returns all permissions the authenticated user has for the specified
4244
tenant and service combination.
4345
46+
\b
47+
Finding IDs:
48+
- Tenant IDs: Run 'unityauth tenant list' to see available tenants
49+
- Service IDs: Assigned by system administrator (common: 1=Libre311)
50+
4451
\b
4552
Examples:
4653
unityauth permissions list --tenant-id 1 --service-id 1
47-
unityauth permissions list --tenant-id 1 --service-id 1 --format json
54+
unityauth permissions list --tenant-id 1 --service-id 1 -o json
4855
"""
4956
try:
5057
# Validate IDs

unityauth-cli/src/unityauth_cli/commands/roles.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
CLIContext,
1212
console,
1313
error,
14+
format_option,
1415
handle_error,
1516
info,
1617
pass_context,
@@ -25,6 +26,7 @@
2526

2627

2728
@click.command('list')
29+
@format_option
2830
@pass_context
2931
@require_auth
3032
def list_roles(ctx: CLIContext, client: UnityAuthAPIClient) -> None:
@@ -33,11 +35,16 @@ def list_roles(ctx: CLIContext, client: UnityAuthAPIClient) -> None:
3335
Lists all roles defined in the system with their descriptions.
3436
Requires Unity Administrator or Tenant Administrator permissions.
3537
38+
\b
39+
Role IDs are used in:
40+
- 'unityauth user create --role-ids "1,2"'
41+
- 'unityauth user update <ID> --role-ids "1,2"'
42+
3643
\b
3744
Examples:
3845
unityauth role list
39-
unityauth role list --format json
40-
unityauth role list --format csv
46+
unityauth role list -o json
47+
unityauth role list -o csv
4148
"""
4249
try:
4350
if ctx.verbose:

unityauth-cli/src/unityauth_cli/commands/tenants.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
CLIContext,
1212
console,
1313
error,
14+
format_option,
1415
handle_error,
1516
info,
1617
pass_context,
@@ -25,6 +26,7 @@
2526

2627

2728
@click.command('list')
29+
@format_option
2830
@pass_context
2931
@require_auth
3032
def list_tenants(ctx: CLIContext, client: UnityAuthAPIClient) -> None:
@@ -34,11 +36,17 @@ def list_tenants(ctx: CLIContext, client: UnityAuthAPIClient) -> None:
3436
Unity Administrators see all tenants.
3537
Tenant Administrators see only their assigned tenants.
3638
39+
\b
40+
Tenant IDs are used in:
41+
- 'unityauth user list --tenant-id <ID>'
42+
- 'unityauth user create --tenant-id <ID>'
43+
- 'unityauth permissions list --tenant-id <ID>'
44+
3745
\b
3846
Examples:
3947
unityauth tenant list
40-
unityauth tenant list --format json
41-
unityauth tenant list --format csv
48+
unityauth tenant list -o json
49+
unityauth tenant list -o csv
4250
"""
4351
try:
4452
if ctx.verbose:
@@ -89,6 +97,7 @@ def list_tenants(ctx: CLIContext, client: UnityAuthAPIClient) -> None:
8997

9098
@click.command('users')
9199
@click.argument('tenant_id', type=int)
100+
@format_option
92101
@pass_context
93102
@require_auth
94103
def tenant_users(ctx: CLIContext, tenant_id: int, client: UnityAuthAPIClient) -> None:
@@ -100,8 +109,8 @@ def tenant_users(ctx: CLIContext, tenant_id: int, client: UnityAuthAPIClient) ->
100109
\b
101110
Examples:
102111
unityauth tenant users 1
103-
unityauth tenant users 1 --format json
104-
unityauth tenant users 2 --format csv
112+
unityauth tenant users 1 -o json
113+
unityauth tenant users 2 -o csv
105114
"""
106115
try:
107116
# Validate tenant ID

unityauth-cli/src/unityauth_cli/commands/users.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
CLIContext,
1212
console,
1313
error,
14+
format_option,
1415
handle_error,
1516
info,
1617
pass_context,
@@ -52,6 +53,11 @@ def create(
5253
Creates a new user with the specified details and assigns roles.
5354
Requires Unity Administrator or Tenant Administrator permissions.
5455
56+
\b
57+
Finding IDs:
58+
- Tenant IDs: Run 'unityauth tenant list'
59+
- Role IDs: Run 'unityauth role list'
60+
5561
\b
5662
Examples:
5763
unityauth user create --email user@example.com --first-name John --last-name Doe --password MyP@ss123 --tenant-id 1 --role-ids "2"
@@ -171,6 +177,12 @@ def update(
171177
Updates the role assignments for an existing user in a specific tenant.
172178
This replaces all current roles with the specified roles for that tenant.
173179
180+
\b
181+
Finding IDs:
182+
- User IDs: Run 'unityauth user list --tenant-id <ID>'
183+
- Tenant IDs: Run 'unityauth tenant list'
184+
- Role IDs: Run 'unityauth role list'
185+
174186
\b
175187
Examples:
176188
unityauth user update 5 --tenant-id 1 --role-ids "1,2"
@@ -256,6 +268,10 @@ def update_profile(
256268
257269
At least one field must be provided.
258270
271+
\b
272+
Finding your user ID:
273+
Run 'unityauth token-info' to see your user ID.
274+
259275
\b
260276
Examples:
261277
unityauth user update-profile 5 --first-name John
@@ -332,6 +348,7 @@ def update_profile(
332348

333349
@click.command()
334350
@click.option('-t', '--tenant-id', type=int, required=True, help='Tenant ID to list users from')
351+
@format_option
335352
@pass_context
336353
@require_auth
337354
def list_users(ctx: CLIContext, tenant_id: int, client: UnityAuthAPIClient) -> None:
@@ -340,11 +357,15 @@ def list_users(ctx: CLIContext, tenant_id: int, client: UnityAuthAPIClient) -> N
340357
Lists all users in the specified tenant. Requires Unity Administrator
341358
or Tenant Administrator permissions for the target tenant.
342359
360+
\b
361+
Finding IDs:
362+
- Tenant IDs: Run 'unityauth tenant list'
363+
343364
\b
344365
Examples:
345366
unityauth user list --tenant-id 1
346-
unityauth user list --tenant-id 1 --format json
347-
unityauth user list --tenant-id 1 --format csv
367+
unityauth user list --tenant-id 1 -o json
368+
unityauth user list --tenant-id 1 -o csv
348369
"""
349370
try:
350371
# Validate tenant ID

0 commit comments

Comments
 (0)