Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
103 lines (89 loc) · 2.97 KB
/
Copy pathmain.py
File metadata and controls
103 lines (89 loc) · 2.97 KB
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 2019 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License.
"""Executable for TCLI. Sets up command completion and then prompts the user."""
import importlib
import sys
import types
from absl import app
from absl import flags
import tcli.tcli_lib as tcli
flags.DEFINE_string(
'cmds',
None,
"""
Commands (newline separated) to send to devices in the target list.
'Prompting' commands, commands that request further input from the
user before completing are discouraged and will fail.
Examples to avoid: telnet, ping, reload.
Shortname: 'C'.""",
short_name='C',
)
flags.DEFINE_boolean(
'interactive',
False,
"""
TCLI runs in interactive mode. This is the default if no cmds are supplied.'
Shortname: 'I'.""",
short_name='I',
)
flags.DEFINE_string(
'accessor_file',
'accessor_canned',
'Name of the module that implements SendRequests.',
)
flags.DEFINE_string(
'inventory_file',
'inventory_csv',
'Name of the module that implements the Inventory class.',
)
FLAGS = flags.FLAGS
def _ParseFlags(argv: list[str]) -> list[str]:
"""Pre-loads accessor and inventory modules before parsing all CLI flags."""
FLAGS(argv, known_only=True)
# Replace the generic Accessor function with the site specific one.
tcli.accessor = importlib.import_module(f'tcli.{FLAGS.accessor_file}')
# Replace the generic Inventory class with the site specific one.
tcli.inventory = importlib.import_module(f'tcli.{FLAGS.inventory_file}')
return app.parse_flags_with_usage(argv)
def main(_):
try:
# If no commands supplied via flags then assume interactive mode.
interactive = FLAGS.interactive or not FLAGS.cmds
tcli_singleton = tcli.TCLI(interactive, FLAGS.cmds)
except (
EOFError,
tcli.TcliCmdError,
tcli.inventory.AuthError,
tcli.inventory.InventoryError,
ValueError,
) as error_message:
print('%s' % error_message, file=sys.stderr)
sys.exit(1)
if not tcli_singleton.interactive:
sys.exit(0)
# Interactive prompt, setup Tab completion
tcli.readline.set_completer(tcli_singleton.Completer)
tcli.readline.parse_and_bind('tab: complete')
tcli.readline.parse_and_bind('?: complete')
tcli.readline.set_completer_delims(' ')
tcli_singleton.Motd()
while True:
try:
tcli_singleton.Prompt()
except KeyboardInterrupt:
continue
except EOFError:
sys.exit(0)
if __name__ == '__main__':
app.run(main, flags_parser=_ParseFlags)