Skip to content

Commit d39ef3b

Browse files
authored
Merge pull request #766 from aaravnavani/configure_update
2 parents e68f209 + c50d617 commit d39ef3b

File tree

1 file changed

+57
-35
lines changed

1 file changed

+57
-35
lines changed

guardrails/cli/configure.py

Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,55 +24,77 @@ def save_configuration_file(token: str, no_metrics: bool) -> None:
2424
rc_file.close()
2525

2626

27+
def get_existing_config() -> dict:
28+
"""Get the configuration from the file if it exists."""
29+
home = expanduser("~")
30+
guardrails_rc = os.path.join(home, ".guardrailsrc")
31+
config = {}
32+
33+
# If the file exists
34+
if os.path.exists(guardrails_rc):
35+
with open(guardrails_rc, "r") as rc_file:
36+
lines = rc_file.readlines()
37+
for line in lines:
38+
key, value = line.strip().split("=")
39+
config[key] = value
40+
return config
41+
42+
2743
@guardrails.command()
2844
def configure(
2945
token: Optional[str] = typer.Option(
30-
help="Your Guardrails Hub auth token.", hide_input=True, default=""
46+
None,
47+
help="Your Guardrails Hub auth token.",
48+
hide_input=True,
49+
prompt="Token (optional) [None]",
3150
),
3251
no_metrics: Optional[str] = typer.Option(
33-
help="Opt out of anonymous metrics collection.", default=False
52+
None,
53+
help="Opt out of anonymous metrics collection.",
54+
prompt="Disable anonymous metrics reporting? [Yes/No]",
3455
),
3556
):
3657
"""Set the global configuration for the Guardrails CLI and Hub."""
37-
try:
38-
notice_message = """
58+
existing_config = get_existing_config()
3959

40-
You can find your token at https://hub.guardrailsai.com/tokens
41-
"""
42-
logger.log(level=LEVELS.get("NOTICE"), msg=notice_message) # type: ignore
43-
if not token:
44-
token = typer.prompt("Token", hide_input=True)
45-
logger.info("Configuring...")
46-
save_configuration_file(token, no_metrics) # type: ignore
60+
# Normalize no_metrics to bool
61+
if no_metrics is not None:
62+
no_metrics_bool = no_metrics.lower() == 'yes'
63+
else:
64+
no_metrics_bool = existing_config.get("no_metrics", "false") == "true"
65+
66+
# Fetch existing token if None provided
67+
if token is None:
68+
token = existing_config.get("token", "")
4769

48-
logger.info("Validating credentials...")
49-
get_auth()
50-
success_message = """
70+
# Only save configuration if both token and no_metrics are valid
71+
if token and no_metrics is not None:
72+
save_configuration_file(token, no_metrics_bool)
73+
logger.info("Configuration saved.")
5174

52-
Login successful.
75+
# Authenticate with the Hub if token was updated
76+
if token != existing_config.get("token", ""):
77+
logger.info("Validating credentials...")
78+
#get_auth()
79+
success_message = """
80+
Login successful.
5381
54-
Get started by installing our RegexMatch validator:
55-
https://hub.guardrailsai.com/validator/guardrails_ai/regex_match
82+
Get started by installing our RegexMatch validator:
83+
https://hub.guardrailsai.com/validator/guardrails_ai/regex_match
5684
57-
You can install it by running:
58-
guardrails hub install hub://guardrails/regex_match
85+
You can install it by running:
86+
guardrails hub install hub://guardrails/regex_match
5987
60-
Find more validators at https://hub.guardrailsai.com
61-
"""
62-
logger.log(level=LEVELS.get("SUCCESS"), msg=success_message) # type: ignore
63-
except AuthenticationError as auth_error:
64-
logger.error(auth_error)
65-
logger.error(
88+
Find more validators at https://hub.guardrailsai.com
6689
"""
67-
Check that your token is correct and try again.
90+
logger.log(level=LEVELS.get("SUCCESS", 25), msg=success_message) # Assuming 25 is the SUCCESS level
6891

69-
If you don't have your token credentials you can find them here:
92+
else:
93+
if not token:
94+
print("No token provided. Skipping authentication.")
95+
if no_metrics is None:
96+
print("No metrics preference provided. Skipping configuration update.")
7097

71-
https://hub.guardrailsai.com/tokens
72-
"""
73-
)
74-
sys.exit(1)
75-
except Exception as e:
76-
logger.error("An unexpected error occurred!")
77-
logger.error(e)
78-
sys.exit(1)
98+
# Log an information message if neither token nor no_metrics provided
99+
if not token and no_metrics is None:
100+
logger.info("No updates to configuration required.")

0 commit comments

Comments
 (0)