Skip to content

Commit 1a1cf29

Browse files
committed
Add bash completion for opensips-mi
1 parent ccb1d13 commit 1a1cf29

File tree

2 files changed

+84
-19
lines changed

2 files changed

+84
-19
lines changed

completion.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# OpenSIPS CLI autocompletion
2+
function _opensips-mi-complete() {
3+
local cur prev opts completed_args
4+
COMPREPLY=()
5+
6+
cur="${COMP_WORDS[COMP_CWORD]}"
7+
8+
prev="${COMP_WORDS[COMP_CWORD-1]}"
9+
10+
completed_args=""
11+
if [[ "${prev:0:1}" != "-" ]]; then
12+
if [[ $COMP_CWORD -ge 2 ]]; then
13+
completed_args="${COMP_WORDS[@]:1:COMP_CWORD-2}"
14+
if [[ "${COMP_WORDS[COMP_CWORD-2]:0:1}" == "-" ]]; then
15+
completed_args="$completed_args $prev"
16+
fi
17+
fi
18+
opts="$(opensips-mi $completed_args -bc)"
19+
else
20+
while [[ "${prev:0:1}" == "-" ]]; do
21+
prev="${prev:1}"
22+
done
23+
completed_args="${COMP_WORDS[@]:1:COMP_CWORD-2}"
24+
opts="$(opensips-mi -bc $prev)"
25+
fi
26+
27+
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
28+
}
29+
30+
complete -F _opensips-mi-complete opensips-mi

opensips/mi/__main__.py

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@
6363
type=str,
6464
help='command')
6565

66+
group.add_argument('-bc', '--bash-complete',
67+
type=str,
68+
nargs='?',
69+
const='',
70+
help='Provide options for bash completion')
71+
6672
group = parser.add_mutually_exclusive_group(required=False)
6773

6874
group.add_argument('-j', '--json',
@@ -80,6 +86,54 @@ def main():
8086
""" Main function of the opensips-mi script """
8187
args = parser.parse_args()
8288

89+
if args.type == 'fifo':
90+
fifo_args = {}
91+
if args.fifo_file:
92+
fifo_args['fifo_file'] = args.fifo_file
93+
if args.fifo_fallback:
94+
fifo_args['fifo_file_fallback'] = args.fifo_fallback
95+
if args.fifo_reply_dir:
96+
fifo_args['fifo_reply_dir'] = args.fifo_reply_dir
97+
mi = OpenSIPSMI('fifo', **fifo_args)
98+
elif args.type == 'http':
99+
mi = OpenSIPSMI('http', url=f'http://{args.ip}:{args.port}/mi')
100+
elif args.type == 'datagram':
101+
mi = OpenSIPSMI('datagram',
102+
datagram_ip=args.ip,
103+
datagram_port=args.port,
104+
timeout=0.1)
105+
else:
106+
if not args.bash_complete:
107+
print(f'Unknown type: {args.type}')
108+
sys.exit(1)
109+
110+
111+
if args.bash_complete is not None:
112+
if args.bash_complete != '':
113+
if len(args.bash_complete) > 1:
114+
last_arg = '--' + args.bash_complete
115+
else:
116+
last_arg = '-' + args.bash_complete
117+
118+
for action in parser._actions:
119+
if last_arg in action.option_strings:
120+
if action.choices:
121+
print(' '.join(action.choices))
122+
break
123+
sys.exit(0)
124+
else:
125+
options = []
126+
for action in parser._actions:
127+
for opt in action.option_strings:
128+
options.append(opt)
129+
print(' '.join(options))
130+
try:
131+
response = mi.execute('which', [])
132+
print(" ".join(response))
133+
sys.exit(0)
134+
except Exception as e:
135+
sys.exit(1)
136+
83137
if args.stats:
84138
print('Using get_statistics! Be careful not to use '
85139
'command after -s/--stats.')
@@ -99,25 +153,6 @@ def main():
99153
print('Invalid JSON: ', e)
100154
sys.exit(1)
101155

102-
if args.type == 'fifo':
103-
fifo_args = {}
104-
if args.fifo_file:
105-
fifo_args['fifo_file'] = args.fifo_file
106-
if args.fifo_fallback:
107-
fifo_args['fifo_file_fallback'] = args.fifo_fallback
108-
if args.fifo_reply_dir:
109-
fifo_args['fifo_reply_dir'] = args.fifo_reply_dir
110-
mi = OpenSIPSMI('fifo', **fifo_args)
111-
elif args.type == 'http':
112-
mi = OpenSIPSMI('http', url=f'http://{args.ip}:{args.port}/mi')
113-
elif args.type == 'datagram':
114-
mi = OpenSIPSMI('datagram',
115-
datagram_ip=args.ip,
116-
datagram_port=args.port)
117-
else:
118-
print(f'Unknownt type: {args.type}')
119-
sys.exit(1)
120-
121156
try:
122157
response = mi.execute(args.command, args.parameters)
123158
print(json.dumps(response, indent=4))

0 commit comments

Comments
 (0)