Repository: aws/aws-cli

Issue:
--query does not work with aws eks get-token
It looks like `aws eks get-token` returns JSON-like output, but not pretty printed, and not working with `--query`, like
```
aws eks get-token --cluster-name myclustername --query status.token
```
still returns the complete output. As well format output cannot be changed.

Tested with
```
aws --version
aws-cli/1.16.218 Python/3.6.8 Linux/4.15.0-1047-aws botocore/1.12.208
```
but others reported the same for `1.16.230`: https://stackoverflow.com/a/57878048/1545325

Thank you!


Discussion:
The `--query` option works by default for all the "built in" commands (the ones that are auto generated based on the service's API).  The `get-token` command is a hand-written command and needs explicit support to support the query option.  Marking as an enhancement.
More than 2 yrs passed. Any enhancements?
almost 3 years now, any progress on this ?

Resolving patch:
diff --git a/awscli/customizations/eks/get_token.py b/awscli/customizations/eks/get_token.py
--- a/awscli/customizations/eks/get_token.py
+++ b/awscli/customizations/eks/get_token.py
@@ -20,6 +20,7 @@
 from botocore.signers import RequestSigner
 from botocore.model import ServiceId
 
+from awscli.formatter import get_formatter
 from awscli.customizations.commands import BasicCommand
 from awscli.customizations.utils import uni_print
 from awscli.customizations.utils import validate_mutually_exclusive
@@ -142,7 +143,11 @@ def _run_main(self, parsed_args, parsed_globals):
             },
         }
 
-        uni_print(json.dumps(full_object))
+        output = self._session.get_config_variable('output')
+        formatter = get_formatter(output, parsed_globals)
+        formatter.query = parsed_globals.query
+
+        formatter(self.NAME, full_object)
         uni_print('\n')
         return 0
 


Test patch:
diff --git a/tests/functional/eks/test_get_token.py b/tests/functional/eks/test_get_token.py
--- a/tests/functional/eks/test_get_token.py
+++ b/tests/functional/eks/test_get_token.py
@@ -95,6 +95,48 @@ def test_get_token(self, mock_datetime):
             },
         )
 
+    @mock.patch('awscli.customizations.eks.get_token.datetime')
+    def test_query_nested_object(self, mock_datetime):
+        mock_datetime.utcnow.return_value = datetime(2019, 10, 23, 23, 0, 0, 0)
+        cmd = 'eks get-token --cluster-name %s' % self.cluster_name
+        cmd += ' --query status'
+        response = self.run_get_token(cmd)
+        self.assertEqual(
+            response,
+            {
+                "expirationTimestamp": "2019-10-23T23:14:00Z",
+                "token": mock.ANY,  # This is asserted in later cases
+            },
+        )
+
+    def test_query_value(self):
+        cmd = 'eks get-token --cluster-name %s' % self.cluster_name
+        cmd += ' --query apiVersion'
+        response = self.run_get_token(cmd)
+        self.assertEqual(
+            response, "client.authentication.k8s.io/v1beta1",
+        )
+
+    @mock.patch('awscli.customizations.eks.get_token.datetime')
+    def test_output_text(self, mock_datetime):
+        mock_datetime.utcnow.return_value = datetime(2019, 10, 23, 23, 0, 0, 0)
+        cmd = 'eks get-token --cluster-name %s' % self.cluster_name
+        cmd += ' --output text'
+        stdout, _, _ = self.run_cmd(cmd)
+        self.assertIn("ExecCredential", stdout)
+        self.assertIn("client.authentication.k8s.io/v1beta1", stdout)
+        self.assertIn("2019-10-23T23:14:00Z", stdout)
+
+    @mock.patch('awscli.customizations.eks.get_token.datetime')
+    def test_output_table(self, mock_datetime):
+        mock_datetime.utcnow.return_value = datetime(2019, 10, 23, 23, 0, 0, 0)
+        cmd = 'eks get-token --cluster-name %s' % self.cluster_name
+        cmd += ' --output table'
+        stdout, _, _ = self.run_cmd(cmd)
+        self.assertIn("ExecCredential", stdout)
+        self.assertIn("client.authentication.k8s.io/v1beta1", stdout)
+        self.assertIn("2019-10-23T23:14:00Z", stdout)
+
     def test_url(self):
         cmd = 'eks get-token --cluster-name %s' % self.cluster_name
         response = self.run_get_token(cmd)