Newer
Older
from json import dumps
from pathlib import Path
from zephir_client import remote_json_to_config, cmdline_factory, send, remove_token, list_messages, authenticate
# get calling script base name
progname = Path(sys.argv[0]).parts[-1]
def main():
zcliconf = zconf.ZcliConfig()
URL = zcliconf.url
VERSION = zcliconf.version
OUTPUT = 'yaml'
list_msgs = False
if '--json' in sys.argv:
sys.argv.remove('--json')
if len(sys.argv) == 1:
help()
if '-h' == sys.argv[1] or '--help' == sys.argv[1]:
if len(sys.argv) == 2 :
help()
if 'logout' in sys.argv:
if '-h' in sys.argv or '--help' in sys.argv:
help()
sys.argv.remove('logout')
print("Jeton d'authentification supprimé")
sys.exit(0)
if 'login' in sys.argv:
if '-h' in sys.argv or '--help' in sys.argv:
help()
remove_token()
user = None
if len(sys.argv) > 2 :
user = sys.argv[2]
authenticate(URL, user)
sys.exit(0)
if 'list' in sys.argv:
if '-h' in sys.argv or '--help' in sys.argv:
help()
list_msgs = True
start = None
if len(sys.argv) > 2 :
start = sys.argv[2]
sys.argv.remove(start)
sys.argv.remove('list')
remote_config = remote_json_to_config(URL, VERSION)
config = cmdline_factory(remote_config)
if list_msgs:
messages = list_messages(config, start)
if not messages:
print("Aucun résultat.")
elif OUTPUT == "yaml":
print("\n{}".format(yaml.dump(messages, default_flow_style=False)))
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
print(dumps(messages))
sys.exit(0)
# special case: no message name given
if 'message' in config.value.mandatory():
print(f"""\nNom de commande manquant. Vous devez spécifier une commande à exécuter.
Utiliser <{progname} list> pour lister les commandes disponibles.\n""")
sys.exit(0)
# ask for missing parameters
for key in config.value.mandatory():
if not config.option(key).option.issymlinkoption():
val_ok = False
while not val_ok:
text = input("* "+config.option(key).option.doc() + " : ")
try:
if config.option(key).option.type() == 'integer':
config.option(key).value.set(int(text))
else:
config.option(key).value.set(text)
val_ok = True
except Exception as err:
print('\nValeur invalide: {}\n'.format(err))
result = send(URL, VERSION, config)
if OUTPUT == "yaml":
print(yaml.dump(result, default_flow_style=False))
else:
print(result)
def help():
print(f"""
Client pour interroger un serveur Zéphir.
{progname} login [utilisateur]
Génère un token d'authentification.
{progname} logout
Supprime le token d'authentification.
{progname} list [recherche]
Liste les commandes disponibles commençant par <recherche>.
{progname} -h <commande>
Affiche l'aide d'une commande.
{progname} commande [paramètres]
Exécute la commande avec ses paramètres.
""")
sys.exit(0)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\n Sortie utilisateur...")
sys.exit(0)