Skip to content

Commit 74bd6b0

Browse files
Saas 1123 1336 zsh and file-dir completion (#268)
* saas-1336 zsh completion + files completion * tests added + small fixes * fix scripts * update version
1 parent b30c8d1 commit 74bd6b0

15 files changed

+361
-19
lines changed

lib/interface/cli/commands/completion/completion.sh.hbs renamed to lib/interface/cli/commands/completion/bash.sh.hbs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,19 @@
77
#
88
_codefresh_completions()
99
{
10-
local cur_word args type_list
10+
local cur args type_list
1111

12-
cur_word="${COMP_WORDS[COMP_CWORD]}"
12+
cur="${COMP_WORDS[COMP_CWORD]}"
1313
args=("${COMP_WORDS[@]}")
1414

1515
# ask codefresh to generate completions.
16-
type_list=$({{app_path}} --get-yargs-completions "${args[@]}")
16+
type_list=$(codefresh --get-yargs-completions "${args[@]}")
1717

18-
COMPREPLY=( $(compgen -W "${type_list}" -- ${cur_word}) )
18+
if [[ ${type_list} == '__files_completion__' ]]; then
19+
_filedir "@(yaml|yml|json)"
20+
else
21+
COMPREPLY=( $(compgen -W "${type_list}" -- ${cur}) )
22+
fi
1923

2024
return 0
2125
}

lib/interface/cli/commands/completion/completion.cmd.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ const fs = require('fs');
66
/**
77
* yargs script generation file redone
88
* */
9-
function _generateCompletionScript(appPath, appName, completionCommand) {
9+
function _generateCompletionScript(appPath, appName, completionCommand, shell) {
1010
let script = fs.readFileSync(
11-
path.resolve(__dirname, 'completion.sh.hbs'),
11+
path.resolve(__dirname, `${shell}.sh.hbs`),
1212
'utf-8',
1313
);
1414
const name = appName || path.basename(appPath);
@@ -20,7 +20,8 @@ function _generateCompletionScript(appPath, appName, completionCommand) {
2020

2121
const command = new Command({
2222
root: true,
23-
command: 'completion',
23+
command: 'completion [shell-name]',
24+
aliases: ['completions'],
2425
description: 'Generate codefresh completion',
2526
usage: 'Prints completion script with specified or default path to executable and command alias',
2627
webDocs: {
@@ -31,6 +32,11 @@ const command = new Command({
3132
},
3233
builder: (yargs) => {
3334
return yargs
35+
.positional('shellName', {
36+
description: 'Name of the shell to generate completion for',
37+
choices: ['bash', 'zsh'],
38+
default: 'bash',
39+
})
3440
.option('executable', {
3541
alias: 'e',
3642
description: 'Name or path to your codefresh executable (default same as alias)',
@@ -40,14 +46,18 @@ const command = new Command({
4046
description: 'Alias used for calling codefresh executable',
4147
default: 'codefresh',
4248
})
43-
.example('codefresh completion', 'Print completion script')
49+
.example('codefresh completion', 'Print bash completion script')
50+
.example('codefresh completion zsh', 'Print zsh completion script')
51+
.example('codefresh completion zsh >> ~/.zshrc', 'Install zsh completion script')
52+
.example('codefresh completion bash >> ~/.bashrc', 'Install bash completion script')
53+
.example('codefresh completion bash >> ~/.bash_profile', 'Install bash completion script (on OSX)')
4454
.example('cf completion --alias cf', 'Print completion script for codefresh aliased as "cf"')
45-
.example('/some/path/codefresh completion --e /some/path/codefresh', 'Print completion script with specified path to codefresh executable');
55+
.example('/some/path/codefresh completion -e /some/path/codefresh', 'Print completion script with specified path to codefresh executable');
4656
},
4757
handler: async (argv) => {
48-
const { executable, alias: appName } = argv;
58+
const { executable, alias: appName, shellName } = argv;
4959
const appPath = executable || appName;
50-
const script = _generateCompletionScript(appPath, appName, 'completion');
60+
const script = _generateCompletionScript(appPath, appName, 'completion', shellName);
5161
console.log(script);
5262
},
5363
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
###-begin-{{app_name}}-completions-###
2+
#
3+
# codefresh command completion script
4+
#
5+
# Installation: {{app_name}} {{completion_command}} zsh >> ~/.zshrc
6+
#
7+
_codefresh_completions()
8+
{
9+
type_list=($({{app_path}} --impl-zsh-file-dir --get-yargs-completions "${words[@]}"))
10+
11+
if [[ ${type_list[1]} == '__files_completion__' ]]; then
12+
compadd -- "${type_list[@]:1}"
13+
else
14+
compadd -- "${type_list[@]}"
15+
fi
16+
17+
return 0
18+
}
19+
compdef _codefresh_completions {{app_name}}
20+
###-end-{{app_name}}-completions-###
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const { handleOptions, fileDir } = require('../../completion/helpers');
2+
3+
const FILENAME_OPTIONS = ['--variable-file', '--var-file', '--yaml', '-y'];
4+
5+
function optionHandler({ word, argv, option }) {
6+
if (FILENAME_OPTIONS.includes(option)) {
7+
return fileDir(word);
8+
}
9+
return [];
10+
}
11+
12+
module.exports = {
13+
optionHandler: handleOptions([...FILENAME_OPTIONS], optionHandler),
14+
};
15+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const { handleOptions, fileDir } = require('../../completion/helpers');
2+
3+
const FILENAME_OPTIONS = ['-f', '--filename'];
4+
5+
function optionHandler({ word, argv, option }) {
6+
if (FILENAME_OPTIONS.includes(option)) {
7+
return fileDir(word);
8+
}
9+
return [];
10+
}
11+
12+
module.exports = {
13+
optionHandler: handleOptions([...FILENAME_OPTIONS], optionHandler),
14+
};
15+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const { handleOptions, fileDir } = require('../../completion/helpers');
2+
3+
const FILENAME_OPTIONS = ['-f', '--filename'];
4+
5+
function optionHandler({ word, argv, option }) {
6+
if (FILENAME_OPTIONS.includes(option)) {
7+
return fileDir(word);
8+
}
9+
return [];
10+
}
11+
12+
module.exports = {
13+
optionHandler: handleOptions([...FILENAME_OPTIONS], optionHandler),
14+
};
15+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const { handleOptions, fileDir } = require('../../completion/helpers');
2+
3+
const FILENAME_OPTIONS = ['-f', '--filename'];
4+
5+
function optionHandler({ word, argv, option }) {
6+
if (FILENAME_OPTIONS.includes(option)) {
7+
return fileDir(word);
8+
}
9+
return [];
10+
}
11+
12+
module.exports = {
13+
optionHandler: handleOptions([...FILENAME_OPTIONS], optionHandler),
14+
};
15+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const { handleOptions, fileDir } = require('../../completion/helpers');
2+
3+
const FILENAME_OPTIONS = ['-f', '--filename'];
4+
5+
function optionHandler({ word, argv, option }) {
6+
if (FILENAME_OPTIONS.includes(option)) {
7+
return fileDir(word);
8+
}
9+
return [];
10+
}
11+
12+
module.exports = {
13+
optionHandler: handleOptions([...FILENAME_OPTIONS], optionHandler),
14+
};
15+
Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1-
const { handleOptions } = require('../../completion/helpers');
1+
const { handleOptions, fileDir } = require('../../completion/helpers');
2+
3+
const FILENAME_OPTIONS = ['-f', '--filename'];
4+
const OUTPUT_OPTIONS = ['-o', '--output'];
25

36
function optionHandler({ word, argv, option }) {
4-
return ['json', 'yaml', 'wide', 'name', 'id'];
7+
if (FILENAME_OPTIONS.includes(option)) {
8+
return fileDir(word);
9+
}
10+
if (OUTPUT_OPTIONS.includes(option)) {
11+
return ['json', 'yaml', 'wide', 'name', 'id'];
12+
}
13+
return [];
514
}
615

716
module.exports = {
8-
optionHandler: handleOptions(['-o', '--output'], optionHandler),
17+
optionHandler: handleOptions([...FILENAME_OPTIONS, ...OUTPUT_OPTIONS], optionHandler),
918
};
1019

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const { handleOptions, fileDir } = require('../../completion/helpers');
2+
3+
const FILENAME_OPTIONS = ['-f', '--filename'];
4+
5+
function optionHandler({ word, argv, option }) {
6+
if (FILENAME_OPTIONS.includes(option)) {
7+
return fileDir(word);
8+
}
9+
return [];
10+
}
11+
12+
module.exports = {
13+
optionHandler: handleOptions([...FILENAME_OPTIONS], optionHandler),
14+
};
15+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const { fileDir } = require('../../completion/helpers');
2+
3+
function positionalHandler({ word, argv}) {
4+
return fileDir(word);
5+
}
6+
7+
module.exports = {
8+
positionalHandler,
9+
};
10+

0 commit comments

Comments
 (0)