From fcbb2a5e081ffac3c0d1c063e8704cfdec39b9cd Mon Sep 17 00:00:00 2001 From: Nick Mitchell Date: Wed, 18 Aug 2021 10:10:11 -0400 Subject: [PATCH] fix(plugins/plugin-kubectl): sort-by does not work for "direct" kubectl client part of #7868 --- .../src/controller/kubectl/options.ts | 2 ++ .../src/lib/view/formatTable.ts | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/plugins/plugin-kubectl/src/controller/kubectl/options.ts b/plugins/plugin-kubectl/src/controller/kubectl/options.ts index 68938830e7e..891cf1089f8 100644 --- a/plugins/plugin-kubectl/src/controller/kubectl/options.ts +++ b/plugins/plugin-kubectl/src/controller/kubectl/options.ts @@ -310,6 +310,8 @@ export interface KubeOptions extends ParsedOptions { help?: boolean limit?: number + + 'sort-by'?: string } export function isForAllNamespaces(parsedOptions: KubeOptions) { diff --git a/plugins/plugin-kubectl/src/lib/view/formatTable.ts b/plugins/plugin-kubectl/src/lib/view/formatTable.ts index 9f3863316f1..a71618e12f1 100644 --- a/plugins/plugin-kubectl/src/lib/view/formatTable.ts +++ b/plugins/plugin-kubectl/src/lib/view/formatTable.ts @@ -14,6 +14,8 @@ * limitations under the License. */ +import jsonpath from '@kui-shell/jsonpath' + import { Breadcrumb, Table, @@ -747,6 +749,24 @@ export function toKuiTable( ? [{ label: table.rows[0].object.metadata.namespace }] : undefined) + const sortBy = args.parsedOptions['sort-by'] + if (sortBy) { + const sortByDate = /Time/.test(sortBy) + + // the jsonpath npm needs a leading "$" + const qquery = jsonpath.parse('$' + sortBy) + const query = (qquery as any) as string // bad typing in @types/jsonpath + + body.sort((a, b) => { + const vvA = jsonpath.value(a.object, query) + const vvB = jsonpath.value(b.object, query) + + const vA = sortByDate ? new Date(vvA) : vvA + const vB = sortByDate ? new Date(vvB) : vvB + return vA - vB + }) + } + const kuiTable = { header, body,