Skip to content

Commit

Permalink
fix(plugins/plugin-kubectl): certain filepath options to kubectl were…
Browse files Browse the repository at this point in the history
… not tilde-expanded

Fixes #5415
  • Loading branch information
starpit committed Aug 20, 2020
1 parent f461b8b commit 92b17c3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 18 deletions.
20 changes: 18 additions & 2 deletions plugins/plugin-kubectl/src/controller/kubectl/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ type TableFormat = 'wide' | string // want: 'custom-columns-file=' | 'custom-col
type CustomFormat = string // want: 'go-template' | 'go-template-file' | 'jsonpath' | 'jsonpath-file'
type OutputFormat = EntityFormat | TableFormat | CustomFormat

export function fileOf(args: Arguments<KubeOptions>): string {
return args.parsedOptions.f || args.parsedOptions.filename
export function fileOf(args: Pick<Arguments<KubeOptions>, 'parsedOptions'>): string {
const filename = args.parsedOptions.f || args.parsedOptions.filename
return typeof filename === 'string' ? filename : undefined
}

export function kustomizeOf(args: Arguments<KubeOptions>): string {
Expand Down Expand Up @@ -207,11 +208,26 @@ export interface KubeExecOptions extends ExecOptions {
initialResponse: string
}

/** Options that specify a filepath */
export type FilepathOption =
| 'kubeconfig'
| 'f'
| 'filename'
| 'k'
| 'kustomize'
| 'client-key'
| 'client-certificate'
| 'certificate-authority'
| 'cache-dir'

/** An incomplete set of kubectl options */
export interface KubeOptions extends ParsedOptions {
A?: boolean
'all-namespaces'?: boolean

cluster?: string
context?: string
kubeconfig?: string

n?: string
namespace?: string
Expand Down
43 changes: 27 additions & 16 deletions plugins/plugin-kubectl/src/controller/kubectl/raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,51 @@
import Debug from 'debug'
import { spawn } from 'child_process'

import { expandHomeDir, split, CodedError, inBrowser, ExecOptions, Registrar } from '@kui-shell/core'
import { Arguments, expandHomeDir, split, CodedError, inBrowser, ExecOptions, Registrar } from '@kui-shell/core'

import flags from './flags'
import RawResponse from './response'
import { KubeOptions } from './options'
import commandPrefix from '../command-prefix'
import { KubeOptions, FilepathOption } from './options'

const debug = Debug('plugin-kubeui/controller/kubectl/raw')

/** this is the subset of Commands.Arguments that we need */
interface Arguments {
command: string
argv: string[]
parsedOptions: KubeOptions
execOptions: ExecOptions
/** this is the subset of Arguments that we need */
type Args = Pick<Arguments<KubeOptions>, 'command' | 'argv' | 'parsedOptions' | 'execOptions'>

/** Part of expandTildes: replace one option with the tilde expansion */
function expand(args: Args, option: FilepathOption) {
const idx = args.argv.indexOf(option.length === 1 ? `-${option}` : `--${option}`)
if (idx >= 0) {
const orig = args.argv[idx + 1]
if (orig) {
args.argv[idx + 1] = expandHomeDir(orig)
}
}
}

/** Expand ~ to the full path of the user's home directory */
function expandTildes(args: Args) {
expand(args, 'cache-dir')
expand(args, 'certificate-authority')
expand(args, 'client-key')
expand(args, 'client-certificate')
expand(args, 'kubeconfig')
expand(args, args.parsedOptions.f ? 'f' : 'filename')
expand(args, args.parsedOptions.k ? 'k' : 'kustomize')
}

/**
* This is the final bottoming out of the exec/spawn of the native
* executable.
*
*/
export const doNativeExec = (args: Arguments): Promise<RawResponse> =>
export const doNativeExec = (args: Args): Promise<RawResponse> =>
new Promise((resolve, reject) => {
const env = Object.assign({}, !inBrowser() ? process.env : {}, args.execOptions.env)
delete env.DEBUG

if ((args.parsedOptions.f || args.parsedOptions.filename) && typeof args.parsedOptions.f === 'string') {
const filename = expandHomeDir(args.parsedOptions.f || args.parsedOptions.filename)
const idx = args.argv.indexOf(args.parsedOptions.f ? '-f' : '--filename')
if (idx >= 0) {
args.argv[idx + 1] = filename
}
}
expandTildes(args)

const executable = args.argv[0].replace(/^_/, '')
const child = spawn(executable, args.argv.slice(1), { env })
Expand Down

0 comments on commit 92b17c3

Please sign in to comment.