-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcn-cli
executable file
·68 lines (68 loc) · 3.18 KB
/
cn-cli
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
#!/usr/bin/python3
import argparse
import os
import json
import sys
from libcn.libcn import CypherNode
def main(arg):
conf = handle_cmdline_config(arg)
# for item in conf.items():
# print(item)
cn = CypherNode(cnid=conf['cnid'][0], key=conf['key'][0], url=conf['url'][0], unsecure=conf['unsecure'], verbose=conf['verbose'], configfile=conf['config_file'])
resp = None
if conf['args']:
resp = eval('cn.{}{}'.format(conf['command'], tuple(conf['args'])))
elif not conf['args']:
if conf['command']:
resp = eval('cn.{}()'.format(conf['command']))
if conf['info']:
data = cn.inform(conf['info'][0])
print(data)
if conf['list']:
cn.listing(conf['list'][0])
if conf['token']:
token = cn.get_token()
print(token)
if resp:
if conf['json'] == True:
print(json.dumps(resp, indent=2, sort_keys=False))
else:
print(json.dumps(resp))
def handle_cmdline_config(arg):
conf = argparse.ArgumentParser()
# config_file
conf.add_argument('command', nargs='?', help='Command')
conf.add_argument('arguments', nargs='*', help='Command arguments')
conf.add_argument('--cnid', nargs=1, type=str, default=['None'], choices=['000', '001', '002', '003', None], help='Set the cyphernode ID')
conf.add_argument('--key', nargs=1, type=str, default=['None'], help='Set the cyphernode autorisation key')
conf.add_argument('--url', nargs=1, type=str, default=['None'], help='Set the cyphernode URL')
# conf.add_argument('-h', '--help', help='Show this help message and exit')
conf.add_argument('-l', '--list', nargs=1, type=str, choices=['all', 'stats', 'watcher', 'spender',' admin'], help='List command available')
conf.add_argument('-i', '--info', nargs=1, type=str, metavar='COMMAND', help='Get command informations')
# if configuration file exist, load it !
if os.path.exists("{}/.cn/cn.conf".format(os.path.expanduser('~'))):
conf.add_argument('-c', '--configfile', nargs=1, type=str, default="{}/.cn/cn.conf".format(os.path.expanduser('~')), help='Define the configuration file absolute path')
else:
conf.add_argument('-c', '--configfile', nargs=1, type=str, default=None, help='Define the configuration file absolute path')
conf.add_argument('-u', '--unsecure', action="store_true", default=None, help='Ignore ssl certificate error')
conf.add_argument('-j', '--json', action="store_true", default=None, help='Use json indentation formating')
conf.add_argument('-t', '--token', action="store_true", default=None, help='Generate and return autorisation token')
conf.add_argument('-v', '--verbose', action="store_true", help='Use verbose mode')
args = conf.parse_args(arg)
hcc = {}
hcc['config_file'] = args.configfile
hcc['unsecure'] = args.unsecure
hcc['list'] = args.list
hcc['json'] = args.json
hcc['info'] = args.info
hcc['token'] = args.token
hcc['verbose'] = args.verbose
hcc['command'] = args.command
hcc['args'] = args.arguments
hcc['cnid'] = args.cnid
hcc['key'] = args.key
hcc['url'] = args.url
hcc['response'] = None
return hcc
if __name__=='__main__':
main(sys.argv[1:])