mik-laj commented on a change in pull request #10081:
URL: https://github.com/apache/airflow/pull/10081#discussion_r465934031



##########
File path: airflow/cli/commands/connection_command.py
##########
@@ -67,6 +71,63 @@ def connections_list(args):
         print(msg)
 
 
+def _format_connections(conns: List[Connection], fmt: str) -> str:
+    if fmt == '.env':
+        connections_env = ""
+        for conn in conns:
+            connections_env += f"{conn.conn_id}={conn.get_uri()}\n"
+        return connections_env
+
+    connections_dict = {}
+    for conn in conns:
+        connections_dict[conn.conn_id] = {
+            'conn_type': conn.conn_type,
+            'host': conn.host,
+            'port': conn.port,
+            'is_encrypted': conn.is_encrypted,
+            'is_extra_encrypted': conn.is_encrypted,
+            'extra': conn.extra,
+        }
+
+    if fmt == '.yaml':
+        return yaml.dump(connections_dict)
+
+    if fmt == '.json':
+        return json.dumps(connections_dict)
+
+    return json.dumps(connections_dict)
+
+
+def _is_stdout(fileio: io.TextIOWrapper) -> bool:
+    if fileio.name == '<stdout>':
+        return True
+    return False
+
+
+def connections_export(args):
+    """Exports all connections to a file"""
+    allowed_formats = ['.yaml', '.json', '.env']
+    default_format = '.json'
+
+    with create_session() as session:
+        if _is_stdout(args.file):
+            # export connections in STDOUT in default format
+            filetype = default_format
+        else:
+            _, filetype = os.path.splitext(args.file.name)
+            if filetype not in allowed_formats:
+                msg = f"Unsupported file format. " \
+                      f"The file must have the extension {', 
'.join(allowed_formats)}"
+                raise SystemExit(msg)
+
+        connections = session.query(Connection).all()
+        msg = _format_connections(connections, filetype)
+        args.file.write(msg)
+
+        if not _is_stdout(args.file):
+            print(f"Connections successfully exported to {args.file.name}")

Review comment:
       ```suggestion
           if _is_stdout(args.file):
               print(f"Connections successfully exported.", file=sys.stderr)
           else:
               print(f"Connections successfully exported to {args.file.name}")
           
   ```




----------------------------------------------------------------
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.

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


Reply via email to