Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plugins/plugin-kubectl): sort-by does not work for "direct" kubec… #7869

Merged
merged 1 commit into from
Aug 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions plugins/plugin-kubectl/src/controller/kubectl/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ export interface KubeOptions extends ParsedOptions {
help?: boolean

limit?: number

'sort-by'?: string
}

export function isForAllNamespaces(parsedOptions: KubeOptions) {
Expand Down
20 changes: 20 additions & 0 deletions plugins/plugin-kubectl/src/lib/view/formatTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

import jsonpath from '@kui-shell/jsonpath'

import {
Breadcrumb,
Table,
Expand Down Expand Up @@ -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,
Expand Down