-
Notifications
You must be signed in to change notification settings - Fork 24
/
generate.sh
executable file
·123 lines (100 loc) · 3.81 KB
/
generate.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# remove SFDX spinner
export FORCE_SPINNER_DELAY=
export FORCE_SHOW_SPINNER=
# create the commands-list.json file
sfdx force:doc:commands:list --json | jq -r .result > commands-list.json
sfdx force:doc:commands:display --json | jq '.result[] | select((.command) != null)' > commands-display.json
completion=""
completion+="#compdef sfdx"
# header
completion+="\n"
completion+="\n# DESCRIPTION: Zsh completion script for the Salesforce CLI"
completion+="\n# AUTHOR: Wade Wegner (@WadeWegner)"
completion+="\n# REPO: https://github.com/wadewegner/salesforce-cli-zsh-completion"
completion+="\n# LICENSE: https://github.com/wadewegner/salesforce-cli-zsh-completion/blob/master/LICENSE"
completion+="\n"
completion+="\nlocal -a _1st_arguments"
completion+="\n"
completion+="\n_1st_arguments=("
commands=""
while read name description
do
name="$(echo $name | sed -e 's/:/\\:/g')"
description="$(echo $description | cut -d . -f 1)"
if [[ $name == force\\:* ]]; then
commands="${commands}\n\t\"${name//\"/\\\"}\":\"${description//\"/\\\"}\""
fi
done <<< "$(jq -r 'to_entries[] | "\(.value.name)\t\(.value.description)"' commands-list.json)"
completion+=$commands
completion+="\n)"
completion+="\n"
completion+="\n_arguments '*:: :->command'"
completion+="\n"
completion+="\nif (( CURRENT == 1 )); then"
completion+="\n _describe -t commands \"sfdx command\" _1st_arguments"
completion+="\n return"
completion+="\nfi"
completion+="\n"
completion+="\nlocal -a _command_args"
completion+="\ncase \"\$words[1]\" in"
while read delimitedFullCommand
do
fullCommandArray=($delimitedFullCommand)
topic=${fullCommandArray[0]}
command=${fullCommandArray[1]}
fullCommand=$topic:$command
completion+="\n $fullCommand)"
completion+="\n _command_args=("
delimitedFlags=$(jq -r '. | select((.command == "'$command'") and (.topic == "'$topic'")) | .flags | .[] | .name + "\t" + .description + "\t" + .type + "\t" + (.hasValue | tostring) + "\t" + .char' commands-display.json)
# create the array based on newlines
IFS=$'\n'
flagArray=($delimitedFlags)
# create the array based on tabs (from the jq above)
IFS=$'\t'
for flagArrayRow in "${flagArray[@]}"
do
flagArray2=($flagArrayRow)
flagName=${flagArray2[0]}
flagDescription=${flagArray2[1]}
flagType=${flagArray2[2]}
flagValue=${flagArray2[3]}
flagChar=${flagArray2[4]}
includeFiles=""
if [ "$flagType" == "file" ] || [ "$flagType" == "filepath" ] || [ "$flagType" == "directory" ]; then
includeFiles=":file:_files"
elif [ "$flagValue" == "true" ]; then
includeFiles=":"
fi
# escape braces
flagDescription=$(echo $flagDescription | sed -e "s/\[/\\\[/g")
flagDescription=$(echo $flagDescription | sed -e "s/\]/\\\]/g")
# escape quotes
flagDescription=$(echo $flagDescription | sed -e "s/'/'\\\\''/g")
# different format if there's not a single character arg
if [ "$flagValue" == "true" ]
then
if [ "$flagChar" != "" ]
then
completion+="\n '(-"$flagChar"|--"$flagName")'{-"$flagChar"=,--"$flagName"=}'["$flagDescription"]$includeFiles' \\\\"
else
completion+="\n '(--"$flagName")--"$flagName"=["$flagDescription"]$includeFiles' \\\\"
fi
else
if [ "$flagChar" != "" ]
then
completion+="\n '(-"$flagChar"|--"$flagName")'{-"$flagChar",--"$flagName"}'["$flagDescription"]$includeFiles' \\\\"
else
completion+="\n '(--"$flagName")--"$flagName"["$flagDescription"]$includeFiles' \\\\"
fi
fi
done
IFS=' '
completion+="\n )"
completion+="\n ;;"
done <<< "$(jq -r '"\(.topic) \(.command)"' commands-display.json)"
completion+="\n esac"
completion+="\n"
completion+="\n_arguments \\\\"
completion+="\n \$_command_args \\\\"
completion+="\n && return 0"
echo $completion > _sfdx