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!

Comments:

Patch:
diff --git a/awscli/customizations/eks/get_token.py b/awscli/customizations/eks/get_token.py
index 0b10f2bc8..aedc1022f 100644
--- a/awscli/customizations/eks/get_token.py
+++ b/awscli/customizations/eks/get_token.py
@@ -20,6 +20,7 @@ from datetime import datetime, timedelta
 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 @@ class GetTokenCommand(BasicCommand):
             },
         }
 
-        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
index bf2165899..f22211fa0 100644
--- a/tests/functional/eks/test_get_token.py
+++ b/tests/functional/eks/test_get_token.py
@@ -95,6 +95,48 @@ class TestGetTokenCommand(BaseAWSCommandParamsTest):
             },
         )
 
+    @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)