/
vyatta-op-run
102 lines (89 loc) · 3.22 KB
/
vyatta-op-run
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
# Copyright (c) 2018-2019 AT&T Intellectual Property.
# All Rights Reserved.
# Copyright (c) 2013-2017 by Brocade Communications Systems, Inc.
# All rights reserved.
#
# SPDX-License-Identifier: LGPL-2.1-only
source /lib/opc/opc-args-encoder
_vyatta_op_init ()
{
local bashopts=""
# empty and first word or default line completion
complete -E -F _vyatta_op_expand
# bash >= 5.0
if [ ${BASH_VERSION:0:1} -ge 5 ]; then
complete -I -F _vyatta_op_default_expand &>/dev/null
# fallback to vyatta-bash
if [ $? -ne 0 ]; then
complete -D -F _vyatta_op_default_expand -o nofirstword &>/dev/null
# fallback to bash << 5.0
if [ $? -ne 0 ]; then
complete -D -F _vyatta_op_default_expand
fi
fi
else
complete -D -F _vyatta_op_default_expand -o nofirstword &>/dev/null
fi
# Override shopt to ensure the output it returns, when called with -po,
# will result in the builtin set being called, rather than our alias.
# The output from all calls to shopt is passed to sed although we are
# only interested in the output when -po is used.
shopt() {
builtin shopt "$@" | sed -Ee 's/^set ([-+])/builtin set \1/'
}
# create the top level aliases for the unambiguous portions of the commands
# this is the only place we need an entire enumerated list of the subcommands
for cmd in $( /opt/vyatta/bin/opc -op children '' ); do
for pos in $(seq 1 ${#cmd}); do
case ${cmd:0:$pos} in
for|do|done|if|fi|case|while|tr )
continue ;;
*) ;;
esac
complete -F _vyatta_op_expand ${cmd:0:$pos}
eval alias ${cmd:0:$pos}=\'_vyatta_op_run ${cmd:0:$pos}\'
done
done
shopt -s histverify
}
_vyatta_op_encode_args() {
opc_encode_args "$@"
}
_vyatta_op_get_node_def_field ()
{
OPC_FIELD=$2 OPC_ARGS=$(_vyatta_op_encode_args $1) /opt/vyatta/bin/opc -op field-from-env
}
_vyatta_op_run_local ()
{
eval "$(OPC_FIELD=run OPC_ARGS=$(_vyatta_op_encode_args $@) /opt/vyatta/bin/opc -op field-from-env)"
}
_vyatta_op_run ()
{
local -i estat
local tpath=$vyatta_op_templates
local restore_shopts=$( shopt -p extglob nullglob | tr \\n \; )
shopt -s extglob nullglob
args=($(OPC_ARGS=$(_vyatta_op_encode_args "$@") /opt/vyatta/bin/opc -op expand-from-env 2>/dev/null))
_vyatta_op_last_comp=${_vyatta_op_last_comp_init}
false; estat=$?
i=1
local ret=0
local cmd_regex="^(LESSOPEN=|less|pager|tail|/opt/vyatta/bin/vyatta-tshark-interface-port.pl).*"
eval $restore_shopts
if [[ -t 1 && "${args[0]}" == "show" && ! $run_cmd =~ $cmd_regex ]] ; then
OPC_ARGS=$(_vyatta_op_encode_args "$@") /opt/vyatta/bin/opc -op run-from-env | ${VYATTA_PAGER:-cat}
ret=${PIPESTATUS[0]}
else
if [[ $(OPC_FIELD=local OPC_ARGS=$(_vyatta_op_encode_args "$@") /opt/vyatta/bin/opc -op field-from-env 2>/dev/null) == "true" ]]; then
_vyatta_op_run_local "${args[@]}" #pass expanded args for eval
ret=$?
else
OPC_ARGS=$(_vyatta_op_encode_args "$@") /opt/vyatta/bin/opc -op run-from-env
ret=$?
fi
fi
return $ret
}
### Local Variables:
### mode: shell-script
### End: