cli-pretty: Refactor to not take model as argument

Instead focus on the task to do 'show hardware', 'show interfaces' and more,
this to make it more logical.
This commit is contained in:
Mattias Walström
2024-03-18 16:32:18 +01:00
parent 3519f748e5
commit d9ffff2b5a
5 changed files with 63 additions and 53 deletions
+9 -9
View File
@@ -277,7 +277,7 @@
if [ -n "$KLISH_PARAM_name" ]; then
cat "$tmp" | /usr/libexec/statd/cli-pretty "infix-system" -n "$KLISH_PARAM_name"
else
cat "$tmp" | /usr/libexec/statd/cli-pretty "infix-system"
cat "$tmp" | /usr/libexec/statd/cli-pretty "show-software"
fi
rm "$tmp"
</ACTION>
@@ -311,27 +311,27 @@
</COMMAND>
<COMMAND name="routes" help="Show routing table">
<PARAM name="name" ptype="/STRING" help="ipv4 or ipv6" min="0" max="1">
<PARAM name="ip" ptype="/STRING" help="ipv4 or ipv6" min="0" max="1">
<COMPL>
<ACTION sym="printl">ipv4</ACTION>
<ACTION sym="printl">ipv6</ACTION>
</COMPL>
</PARAM>
<ACTION sym="script">
if [ "$KLISH_PARAM_name" != "ipv4" ]; then
if [ "$KLISH_PARAM_name" != "ipv6" ]; then
KLISH_PARAM_name=ipv4
if [ "$KLISH_PARAM_ip" != "ipv4" ]; then
if [ "$KLISH_PARAM_ip" != "ipv6" ]; then
KLISH_PARAM_ip=ipv4
fi
fi
sysrepocfg -f json -X -d operational -x "/ietf-routing:routing/ribs" | \
/usr/libexec/statd/cli-pretty "ietf-routing" -n "$KLISH_PARAM_name"
/usr/libexec/statd/cli-pretty "show-routing-table" -i "$KLISH_PARAM_ip"
</ACTION>
</COMMAND>
<COMMAND name="hardware" help="Show hardware information">
<ACTION sym="script">
sysrepocfg -f json -X -d operational -x "/ietf-hardware:hardware" | \
/usr/libexec/statd/cli-pretty "ietf-hardware"
/usr/libexec/statd/cli-pretty "show-hardware"
</ACTION>
</COMMAND>
@@ -347,10 +347,10 @@
if [ -n "$KLISH_PARAM_name" ]; then
sysrepocfg -f json -X -d operational -x \
"/ietf-interfaces:interfaces/interface[name='$KLISH_PARAM_name']" | \
/usr/libexec/statd/cli-pretty "ietf-interfaces" -n "$KLISH_PARAM_name"
/usr/libexec/statd/cli-pretty "show-interfaces" -n "$KLISH_PARAM_name"
else
sysrepocfg -f json -X -d operational -m ietf-interfaces | \
/usr/libexec/statd/cli-pretty "ietf-interfaces"
/usr/libexec/statd/cli-pretty "show-interfaces"
fi
</ACTION>
</COMMAND>
+30 -18
View File
@@ -4,11 +4,6 @@ import json
import re
import sys
parser = argparse.ArgumentParser(description="JSON CLI Pretty Printer")
parser.add_argument("module", help="IETF Module")
parser.add_argument("-n", "--name", help="Focus on specific name")
args = parser.parse_args()
class Pad:
iface = 16
proto = 11
@@ -434,7 +429,7 @@ def pr_interface_list(json):
iface.pr_proto_ipv4()
iface.pr_proto_ipv6()
def ietf_interfaces(json, name):
def show_interfaces(json, name):
if name:
if not json.get("ietf-interfaces:interfaces"):
print(f"No interface data found for \"{name}\"")
@@ -450,7 +445,7 @@ def ietf_interfaces(json, name):
sys.exit(1)
pr_interface_list(json)
def ietf_routing(json, ip="ipv4"):
def show_routing_table(json, ip):
if not json.get("ietf-routing:routing"):
print(f"Error, top level \"ietf-routing:routing\" missing")
sys.exit(1)
@@ -476,7 +471,7 @@ def find_slot(_slots, name):
return False
def infix_system(json, name):
def show_software(json, name):
if not json.get("ietf-system:system-state", "infix-system:software"):
print("Error, cannot find infix-system:software")
sys.exit(1)
@@ -497,7 +492,7 @@ def infix_system(json, name):
if slot.is_rootfs():
slot.print()
def ietf_hardware(json):
def show_hardware(json):
if not json.get("ietf-hardware:hardware"):
print(f"Error, top level \"ietf-hardware:component\" missing")
sys.exit(1)
@@ -525,16 +520,33 @@ def main():
print("Error, unexpected error parsing JSON")
sys.exit(1)
if args.module == "ietf-interfaces":
sys.exit(ietf_interfaces(json_data, args.name))
if args.module == "ietf-routing":
sys.exit(ietf_routing(json_data, args.name))
if args.module == "infix-system":
sys.exit(infix_system(json_data, args.name))
if args.module == "ietf-hardware":
sys.exit(ietf_hardware(json_data))
parser = argparse.ArgumentParser(description="JSON CLI Pretty Printer")
subparsers = parser.add_subparsers(dest='command', help='Commands')
parser_show_routing_table = subparsers.add_parser('show-routing-table', help='Show the routing table')
parser_show_routing_table.add_argument('-i', '--ip', required=True, help='IPv4 or IPv6 address')
parser_show_interfaces = subparsers.add_parser('show-interfaces', help='Show interfaces')
parser_show_interfaces.add_argument('-n', '--name', help='Interface name')
parser_show_software = subparsers.add_parser('show-software', help='Show software versions')
parser_show_software.add_argument('-n', '--name', help='Slotname')
parser_show_routing_table = subparsers.add_parser('show-hardware', help='Show USB ports')
args = parser.parse_args()
if args.command == "show-interfaces":
show_interfaces(json_data, args.name)
elif args.command == "show-routing-table":
show_routing_table(json_data, args.ip)
elif args.command == "show-software":
show_software(json_data, args.name)
elif args.command == "show-hardware":
show_hardware(json_data)
else:
print(f"Error, unknown module {args.module}")
print(f"Error, unknown command {args.command}")
sys.exit(1)
if __name__ == "__main__":
+15 -17
View File
@@ -48,15 +48,15 @@ if [ ! -e "$CLI_PRETTY_TOOL" ]; then
fi
if [ $# -eq 2 ] && [ $1 = "update" ]; then
if [ $2 = "ietf-interfaces" ]; then
"$SR_EMULATOR_TOOL" | "$CLI_PRETTY_TOOL" "ietf-interfaces" > "$CLI_OUTPUT_PATH/show-interfaces.txt"
if [ $2 = "show-interfaces" ]; then
"$SR_EMULATOR_TOOL" | "$CLI_PRETTY_TOOL" "show-interfaces" > "$CLI_OUTPUT_PATH/show-interfaces.txt"
for iface in "br0" "e0" "e1" "e2" "e3" "e4"; do
"$SR_EMULATOR_TOOL" | "$CLI_PRETTY_TOOL" "ietf-interfaces" -n "$iface" \
"$SR_EMULATOR_TOOL" | "$CLI_PRETTY_TOOL" "show-interfaces" -n "$iface" \
> "$CLI_OUTPUT_PATH/show-interface-${iface}.txt"
done
elif [ $2 = "ietf-routing" ]; then
"$SR_EMULATOR_TOOL" | "$CLI_PRETTY_TOOL" "ietf-routing" -n "ipv4" > "$CLI_OUTPUT_PATH/show-routes-ipv4.txt"
"$SR_EMULATOR_TOOL" | "$CLI_PRETTY_TOOL" "ietf-routing" -n "ipv6" > "$CLI_OUTPUT_PATH/show-routes-ipv6.txt"
elif [ $2 = "show-routing-table" ]; then
"$SR_EMULATOR_TOOL" | "$CLI_PRETTY_TOOL" "show-routing-table" -i "ipv4" > "$CLI_OUTPUT_PATH/show-routes-ipv4.txt"
"$SR_EMULATOR_TOOL" | "$CLI_PRETTY_TOOL" "show-routing-table" -i "ipv6" > "$CLI_OUTPUT_PATH/show-routes-ipv6.txt"
else
echo "Unsupported model $2"
exit 1
@@ -69,9 +69,9 @@ echo "1..9"
echo "# Running:"
# Show interfaces
echo "# $SR_EMULATOR_TOOL | $CLI_PRETTY_TOOL ietf-interfaces"
"$SR_EMULATOR_TOOL" | "$CLI_PRETTY_TOOL" "ietf-interfaces" > "$CLI_OUTPUT_FILE"
# Show routes
echo "# $SR_EMULATOR_TOOL | $CLI_PRETTY_TOOL show-interfaces"
"$SR_EMULATOR_TOOL" | "$CLI_PRETTY_TOOL" "show-interfaces" > "$CLI_OUTPUT_FILE"
if ! diff -u "$CLI_OUTPUT_PATH/show-interfaces.txt" "$CLI_OUTPUT_FILE"; then
print_update_txt
fail "\"show interfaces\" output has changed"
@@ -79,9 +79,9 @@ fi
ok "\"show interfaces\" output looks intact"
# Show ipv4 routes
echo "# $SR_EMULATOR_TOOL | $CLI_PRETTY_TOOL ietf-routing -n ipv4"
"$SR_EMULATOR_TOOL" | "$CLI_PRETTY_TOOL" "ietf-routing" -n "ipv4" > "$CLI_OUTPUT_FILE"
# Show routes
echo "# $SR_EMULATOR_TOOL | $CLI_PRETTY_TOOL show-routing-table -i ipv4"
"$SR_EMULATOR_TOOL" | "$CLI_PRETTY_TOOL" "show-routing-table" -i "ipv4" > "$CLI_OUTPUT_FILE"
if ! diff -u "$CLI_OUTPUT_PATH/show-routes-ipv4.txt" "$CLI_OUTPUT_FILE"; then
print_update_txt
fail "\"show routes ipv4\" output has changed"
@@ -89,10 +89,9 @@ fi
ok "\"show routes ipv4\" output looks intact"
# Show ipv6 routes
echo "# $SR_EMULATOR_TOOL | $CLI_PRETTY_TOOL ietf-routing -n ipv6"
"$SR_EMULATOR_TOOL" | "$CLI_PRETTY_TOOL" "ietf-routing" -n "ipv6" > "$CLI_OUTPUT_FILE"
echo "# $SR_EMULATOR_TOOL | $CLI_PRETTY_TOOL show-routing-table -i ipv6"
"$SR_EMULATOR_TOOL" | "$CLI_PRETTY_TOOL" "show-routing-table" -i "ipv6" > "$CLI_OUTPUT_FILE"
# Show routes
if ! diff -u "$CLI_OUTPUT_PATH/show-routes-ipv6.txt" "$CLI_OUTPUT_FILE"; then
print_update_txt
fail "\"show routes ipv6\" output has changed"
@@ -101,11 +100,10 @@ ok "\"show routes ipv6\" output looks intact"
# Show detailed interfaces
for iface in "br0" "e0" "e1" "e2" "e3" "e4"; do
"$SR_EMULATOR_TOOL" | "$CLI_PRETTY_TOOL" "ietf-interfaces" -n "$iface" > "$CLI_OUTPUT_FILE"
"$SR_EMULATOR_TOOL" | "$CLI_PRETTY_TOOL" "show-interfaces" -n "$iface" > "$CLI_OUTPUT_FILE"
if ! diff -u "$CLI_OUTPUT_PATH/show-interface-${iface}.txt" "$CLI_OUTPUT_FILE"; then
print_update_txt
fail "\"show interface name $iface\" output has changed"
fi
ok "\"show interface name $iface\" output looks intact"
done
+6 -6
View File
@@ -3,37 +3,37 @@
name: "interfaces-empty"
opts:
- "json/empty.json"
- "ietf-interfaces"
- "show-interfaces"
- case: run.sh
name: "interfaces-factory"
opts:
- "json/factory.json"
- "ietf-interfaces"
- "show-interfaces"
- case: run.sh
name: "interfaces-bloated"
opts:
- "json/bloated.json"
- "ietf-interfaces"
- "show-interfaces"
- case: run.sh
name: "interface-ethernet"
opts:
- "json/bloated.json"
- "ietf-interfaces"
- "show-interfaces"
- "-n e0"
- case: run.sh
name: "interface-vlan"
opts:
- "json/bloated.json"
- "ietf-interfaces"
- "show-interfaces"
- "-n vlan1"
- case: run.sh
name: "interface-bridge"
opts:
- "json/bloated.json"
- "ietf-interfaces"
- "show-interfaces"
- "-n br0"
+3 -3
View File
@@ -3,12 +3,12 @@
SCRIPT_PATH="$(dirname "$(readlink -f "$0")")"
if [ $# -lt 2 ]; then
echo "Usage: $0 JSON-FILE MODULE [ ARGS ]"
echo "Usage: $0 JSON-FILE COMMAND [ ARGS ]"
exit 1
fi
json=$1; shift
module=$1; shift
command=$1; shift
echo "1..1"
@@ -19,7 +19,7 @@ fi
cat "$SCRIPT_PATH/$json" | \
"$SCRIPT_PATH"/../../../src/statd/cli-pretty \
"$module" $*
"$command" $*
if [ $? -eq 0 ]; then
echo "ok 1 - $json printed without crashing"
exit 0