Skip to content

Commit

Permalink
Add bash completion for opensips-mi
Browse files Browse the repository at this point in the history
  • Loading branch information
dariusstefan committed Dec 9, 2024
1 parent ccb1d13 commit 1a1cf29
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 19 deletions.
30 changes: 30 additions & 0 deletions completion.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# OpenSIPS CLI autocompletion
function _opensips-mi-complete() {
local cur prev opts completed_args
COMPREPLY=()

cur="${COMP_WORDS[COMP_CWORD]}"

prev="${COMP_WORDS[COMP_CWORD-1]}"

completed_args=""
if [[ "${prev:0:1}" != "-" ]]; then
if [[ $COMP_CWORD -ge 2 ]]; then
completed_args="${COMP_WORDS[@]:1:COMP_CWORD-2}"
if [[ "${COMP_WORDS[COMP_CWORD-2]:0:1}" == "-" ]]; then
completed_args="$completed_args $prev"
fi
fi
opts="$(opensips-mi $completed_args -bc)"
else
while [[ "${prev:0:1}" == "-" ]]; do
prev="${prev:1}"
done
completed_args="${COMP_WORDS[@]:1:COMP_CWORD-2}"
opts="$(opensips-mi -bc $prev)"
fi

COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
}

complete -F _opensips-mi-complete opensips-mi
73 changes: 54 additions & 19 deletions opensips/mi/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@
type=str,
help='command')

group.add_argument('-bc', '--bash-complete',
type=str,
nargs='?',
const='',
help='Provide options for bash completion')

group = parser.add_mutually_exclusive_group(required=False)

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

if args.type == 'fifo':
fifo_args = {}
if args.fifo_file:
fifo_args['fifo_file'] = args.fifo_file
if args.fifo_fallback:
fifo_args['fifo_file_fallback'] = args.fifo_fallback
if args.fifo_reply_dir:
fifo_args['fifo_reply_dir'] = args.fifo_reply_dir
mi = OpenSIPSMI('fifo', **fifo_args)
elif args.type == 'http':
mi = OpenSIPSMI('http', url=f'http://{args.ip}:{args.port}/mi')
elif args.type == 'datagram':
mi = OpenSIPSMI('datagram',
datagram_ip=args.ip,
datagram_port=args.port,
timeout=0.1)
else:
if not args.bash_complete:
print(f'Unknown type: {args.type}')
sys.exit(1)


if args.bash_complete is not None:
if args.bash_complete != '':
if len(args.bash_complete) > 1:
last_arg = '--' + args.bash_complete
else:
last_arg = '-' + args.bash_complete

for action in parser._actions:
if last_arg in action.option_strings:
if action.choices:
print(' '.join(action.choices))
break
sys.exit(0)
else:
options = []
for action in parser._actions:
for opt in action.option_strings:
options.append(opt)
print(' '.join(options))
try:
response = mi.execute('which', [])
print(" ".join(response))
sys.exit(0)
except Exception as e:
sys.exit(1)

if args.stats:
print('Using get_statistics! Be careful not to use '
'command after -s/--stats.')
Expand All @@ -99,25 +153,6 @@ def main():
print('Invalid JSON: ', e)
sys.exit(1)

if args.type == 'fifo':
fifo_args = {}
if args.fifo_file:
fifo_args['fifo_file'] = args.fifo_file
if args.fifo_fallback:
fifo_args['fifo_file_fallback'] = args.fifo_fallback
if args.fifo_reply_dir:
fifo_args['fifo_reply_dir'] = args.fifo_reply_dir
mi = OpenSIPSMI('fifo', **fifo_args)
elif args.type == 'http':
mi = OpenSIPSMI('http', url=f'http://{args.ip}:{args.port}/mi')
elif args.type == 'datagram':
mi = OpenSIPSMI('datagram',
datagram_ip=args.ip,
datagram_port=args.port)
else:
print(f'Unknownt type: {args.type}')
sys.exit(1)

try:
response = mi.execute(args.command, args.parameters)
print(json.dumps(response, indent=4))
Expand Down

0 comments on commit 1a1cf29

Please sign in to comment.