Lee-W commented on code in PR #53726:
URL: https://github.com/apache/airflow/pull/53726#discussion_r2235838491


##########
airflow-core/src/airflow/secrets/local_filesystem.py:
##########
@@ -233,10 +233,23 @@ def load_variables(file_path: str) -> dict[str, str]:
     log.debug("Loading variables from a text file")
 
     secrets = _parse_secret_file(file_path)
-    invalid_keys = [key for key, values in secrets.items() if 
isinstance(values, list) and len(values) != 1]
-    if invalid_keys:
-        raise AirflowException(f'The "{file_path}" file contains multiple 
values for keys: {invalid_keys}')
-    variables = {key: values[0] if isinstance(values, list) else values for 
key, values in secrets.items()}
+
+    # Determine a file format to handle lists correctly
+    ext = file_path.rsplit(".", 2)[-1].lower()

Review Comment:
   Same



##########
airflow-core/src/airflow/secrets/local_filesystem.py:
##########
@@ -233,10 +233,23 @@ def load_variables(file_path: str) -> dict[str, str]:
     log.debug("Loading variables from a text file")
 
     secrets = _parse_secret_file(file_path)
-    invalid_keys = [key for key, values in secrets.items() if 
isinstance(values, list) and len(values) != 1]
-    if invalid_keys:
-        raise AirflowException(f'The "{file_path}" file contains multiple 
values for keys: {invalid_keys}')
-    variables = {key: values[0] if isinstance(values, list) else values for 
key, values in secrets.items()}
+
+    # Determine a file format to handle lists correctly
+    ext = file_path.rsplit(".", 2)[-1].lower()
+    is_env_file = ext == "env"
+
+    variables = {}
+    for key, secret_values in secrets.items():
+        if is_env_file and isinstance(secret_values, list):
+            # Only ENV files: Check for duplicate keys
+            if len(secret_values) > 1:
+                raise AirflowException(

Review Comment:
   Let's create a custom exception for it instead of using `AirflowException`



##########
airflow-core/src/airflow/cli/commands/variable_command.py:
##########
@@ -81,20 +81,25 @@ def variables_import(args, session):
     """Import variables from a given file."""
     if not os.path.exists(args.file):
         raise SystemExit("Missing variables file.")
-    with open(args.file) as varfile:
-        try:
-            var_json = json.load(varfile)
-        except JSONDecodeError:
-            raise SystemExit("Invalid variables file.")
+
+    try:
+        # Use load_variables which supports JSON, YAML, and ENV formats
+        var_dict = load_variables(args.file)
+    except Exception as e:

Review Comment:
   Is it possible to catch a narrower exception?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to