Skip to content

Commit

Permalink
Add bash completion for opensips-event
Browse files Browse the repository at this point in the history
  • Loading branch information
dariusstefan committed Dec 9, 2024
1 parent 1a1cf29 commit 81d1fbe
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
31 changes: 30 additions & 1 deletion completion.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# OpenSIPS CLI autocompletion
function _opensips-mi-complete() {
local cur prev opts completed_args
COMPREPLY=()
Expand Down Expand Up @@ -28,3 +27,33 @@ function _opensips-mi-complete() {
}

complete -F _opensips-mi-complete opensips-mi

function _opensips-event-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-event $completed_args -bc)"
else
while [[ "${prev:0:1}" == "-" ]]; do
prev="${prev:1}"
done
completed_args="${COMP_WORDS[@]:1:COMP_CWORD-2}"
opts="$(opensips-event -bc $prev)"
fi

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

complete -F _opensips-event-complete opensips-event
45 changes: 43 additions & 2 deletions opensips/event/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,17 @@
type=str,
help='OpenSIPS MI FIFO Reply Directory')

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

event = parser.add_argument_group('event')

event.add_argument('event',
type=str,
nargs='?',
help='OpenSIPS Event Name')

event.add_argument('-T', '--transport',
Expand Down Expand Up @@ -101,9 +108,43 @@ def main():
elif args.type == 'datagram':
mi = OpenSIPSMI('datagram',
datagram_ip=args.ip,
datagram_port=args.port)
datagram_port=args.port,
timeout=0.1)
else:
print(f'Unknown type: {args.type}')
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('events_list', [])
events = response.get("Events", [])
event_names = [event["name"] for event in events]
print(' '.join(event_names))
sys.exit(0)
except Exception as e:
sys.exit(1)

if args.event is None:
print('Event name is required')
sys.exit(1)

hdl = OpenSIPSEventHandler(mi, args.transport,
Expand Down

0 comments on commit 81d1fbe

Please sign in to comment.