From 2880e62f966823660aaefc5eeacf2609b53ec147 Mon Sep 17 00:00:00 2001
From: blag <blag@users.noreply.github.com>
Date: Tue, 21 Jun 2022 17:39:25 -0600
Subject: [PATCH 1/4] Rewrite the 'version' command to use Click

---
 airflow/cli/__main__.py            |  1 +
 airflow/cli/commands/version.py    | 28 ++++++++++++++++++++++++++
 tests/cli/commands/test_version.py | 32 ++++++++++++++++++++++++++++++
 3 files changed, 61 insertions(+)
 create mode 100644 airflow/cli/commands/version.py
 create mode 100644 tests/cli/commands/test_version.py

diff --git a/airflow/cli/__main__.py b/airflow/cli/__main__.py
index 2ae6c91cb39f1..f1074714161d3 100644
--- a/airflow/cli/__main__.py
+++ b/airflow/cli/__main__.py
@@ -18,6 +18,7 @@
 # under the License.
 
 from airflow.cli import airflow_cmd
+from airflow.cli.commands import version  # noqa: F401
 
 if __name__ == '__main__':
     airflow_cmd(obj={})
diff --git a/airflow/cli/commands/version.py b/airflow/cli/commands/version.py
new file mode 100644
index 0000000000000..be4a0a9b4d17b
--- /dev/null
+++ b/airflow/cli/commands/version.py
@@ -0,0 +1,28 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Version command"""
+from rich.console import Console
+
+import airflow
+from airflow.cli import airflow_cmd
+
+
+@airflow_cmd.command('version')
+def version():
+    """Displays Airflow version at the command line"""
+    console = Console()
+    console.print(airflow.__version__)
diff --git a/tests/cli/commands/test_version.py b/tests/cli/commands/test_version.py
new file mode 100644
index 0000000000000..fa767345d5335
--- /dev/null
+++ b/tests/cli/commands/test_version.py
@@ -0,0 +1,32 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import unittest
+
+from click.testing import CliRunner
+
+import airflow
+from airflow.cli.commands.version import version
+
+
+class TestCliVersion(unittest.TestCase):
+    def test_cli_version(self):
+        runner = CliRunner()
+        response = runner.invoke(version)
+
+        assert response.exit_code == 0
+        assert airflow.__version__ in response.output

From e67a39c18791b78378510d0c7876b08e4aedddba Mon Sep 17 00:00:00 2001
From: blag <blag@users.noreply.github.com>
Date: Wed, 29 Jun 2022 03:13:14 -0600
Subject: [PATCH 2/4] Use __all__ to avoid noqa comments

---
 airflow/cli/__main__.py | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/airflow/cli/__main__.py b/airflow/cli/__main__.py
index f1074714161d3..e2ee6a0178457 100644
--- a/airflow/cli/__main__.py
+++ b/airflow/cli/__main__.py
@@ -18,7 +18,9 @@
 # under the License.
 
 from airflow.cli import airflow_cmd
-from airflow.cli.commands import version  # noqa: F401
+from airflow.cli.commands import version
+
+__all__ = ["version"]
 
 if __name__ == '__main__':
     airflow_cmd(obj={})

From 3f1e67d145b30cfce1556de14199244a16fc7b8b Mon Sep 17 00:00:00 2001
From: blag <blag@users.noreply.github.com>
Date: Wed, 29 Jun 2022 03:14:51 -0600
Subject: [PATCH 3/4] Delay imports for as long as possible

---
 airflow/cli/commands/version.py | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/airflow/cli/commands/version.py b/airflow/cli/commands/version.py
index be4a0a9b4d17b..2c220172f5b72 100644
--- a/airflow/cli/commands/version.py
+++ b/airflow/cli/commands/version.py
@@ -15,14 +15,15 @@
 # specific language governing permissions and limitations
 # under the License.
 """Version command"""
-from rich.console import Console
-
-import airflow
 from airflow.cli import airflow_cmd
 
 
 @airflow_cmd.command('version')
 def version():
     """Displays Airflow version at the command line"""
+    from rich.console import Console
+
+    import airflow
+
     console = Console()
     console.print(airflow.__version__)

From 43a8992d8254fde0a5f8da517a11810a8d9eb7cf Mon Sep 17 00:00:00 2001
From: blag <blag@users.noreply.github.com>
Date: Wed, 29 Jun 2022 03:16:21 -0600
Subject: [PATCH 4/4] Fix nit

---
 airflow/cli/commands/version.py | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/airflow/cli/commands/version.py b/airflow/cli/commands/version.py
index 2c220172f5b72..b90f37ad8ace7 100644
--- a/airflow/cli/commands/version.py
+++ b/airflow/cli/commands/version.py
@@ -25,5 +25,4 @@ def version():
 
     import airflow
 
-    console = Console()
-    console.print(airflow.__version__)
+    Console().print(airflow.__version__)
