Skip to content

Commit 8ccc271

Browse files
committed
pytest
1 parent 0df7de9 commit 8ccc271

File tree

1 file changed

+37
-42
lines changed

1 file changed

+37
-42
lines changed

guardrails/cli/configure.py

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -55,66 +55,61 @@ def configure(
5555
),
5656
):
5757
"""Set the global configuration for the Guardrails CLI and Hub."""
58-
# Get the existing configuration if present
59-
60-
headless = token is not None or no_metrics is not None
6158
existing_config = get_existing_config()
6259

63-
existing_token = existing_config.get("token", "")
64-
existing_no_metrics = existing_config.get("no_metrics", "false")
60+
# Normalize no_metrics to bool
61+
input_no_metrics = no_metrics.lower() if no_metrics else None
62+
if input_no_metrics == 'yes':
63+
no_metrics_bool = True
64+
elif input_no_metrics == 'no':
65+
no_metrics_bool = False
66+
else:
67+
no_metrics_bool = bool(existing_config.get("no_metrics", "false") == "true")
6568

66-
if not headless:
67-
notice_message = """
69+
# Default token to existing token if None provided
70+
if token is None:
71+
token = existing_config.get("token", "")
6872

69-
You can find your token at https://hub.guardrailsai.com/tokens
70-
"""
71-
logger.log(level=LEVELS.get("NOTICE"), msg=notice_message) # type: ignore
73+
# Provide default logging level for NOTICE if not in LEVELS
74+
notice_level = LEVELS.get("NOTICE", 20) # Assuming 20 is a reasonable default
7275

73-
# Prompt for token if not provided
74-
if not token and not headless:
75-
token = typer.prompt(
76-
"> Token (optional) [None]",
77-
default=existing_token,
78-
hide_input=True,
79-
)
80-
81-
# Prompt for no_metrics if not provided and not running headless
82-
if no_metrics is None and not headless:
83-
no_metrics = typer.prompt(
84-
"> Disable anonymous metrics reporting?",
85-
default=existing_no_metrics,
86-
)
76+
if not token and not no_metrics:
77+
notice_message = """
78+
You can find your token at https://hub.guardrailsai.com/tokens
79+
"""
80+
logger.log(level=notice_level, msg=notice_message)
8781

8882
try:
8983
# If token or no_metrics was updated, save the configuration
90-
token_was_updated = token and token != existing_token
91-
no_metrics_was_updated = no_metrics != existing_no_metrics
84+
token_was_updated = token and token != existing_config.get("token", "")
85+
no_metrics_was_updated = no_metrics_bool != (existing_config.get("no_metrics", "false") == "true")
86+
9287
if token_was_updated or no_metrics_was_updated:
9388
logger.info("Configuring...")
94-
save_configuration_file(token, no_metrics)
89+
save_configuration_file(token, no_metrics_bool)
9590

9691
# Authenticate with the Hub if token was updated
97-
if token_was_updated:
98-
logger.info("Validating credentials...")
99-
get_auth()
100-
success_message = """
101-
102-
Login successful.
92+
if token_was_updated:
93+
logger.info("Validating credentials...")
94+
get_auth()
95+
success_message = """
96+
Login successful.
10397
104-
Get started by installing our RegexMatch validator:
105-
https://hub.guardrailsai.com/validator/guardrails_ai/regex_match
98+
Get started by installing our RegexMatch validator:
99+
https://hub.guardrailsai.com/validator/guardrails_ai/regex_match
106100
107-
You can install it by running:
108-
guardrails hub install hub://guardrails/regex_match
101+
You can install it by running:
102+
guardrails hub install hub://guardrails/regex_match
109103
110-
Find more validators at https://hub.guardrailsai.com
111-
"""
112-
logger.log(level=LEVELS.get("SUCCESS"), msg=success_message) # type: ignore`
104+
Find more validators at https://hub.guardrailsai.com
105+
"""
106+
logger.log(level=LEVELS.get("SUCCESS", 25), msg=success_message) # Assuming 25 is the SUCCESS level
113107
elif not token:
114108
print("No token provided. Skipping authentication.")
115109

116-
if not headless and not token_was_updated:
117-
print("Existing token found. Skipping re-authentication.")
110+
if not (token_was_updated or no_metrics_was_updated) and not (token and no_metrics):
111+
print("Existing configuration found. Skipping re-authentication.")
112+
118113
except AuthenticationError as auth_error:
119114
logger.error(auth_error)
120115
logger.error(

0 commit comments

Comments
 (0)