forked from uncloud/uncloud
uncloud cli converted to argparse, code isn't beautiful yet. Would make it soom
This commit is contained in:
parent
cd2f0aaa0d
commit
50fb135726
14 changed files with 278 additions and 264 deletions
42
uncloud/cli/helper.py
Normal file
42
uncloud/cli/helper.py
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
import requests
|
||||
import json
|
||||
import argparse
|
||||
import binascii
|
||||
|
||||
from pyotp import TOTP
|
||||
from os.path import join as join_path
|
||||
from uncloud.settings import settings
|
||||
|
||||
|
||||
def get_otp_parser():
|
||||
otp_parser = argparse.ArgumentParser('otp')
|
||||
otp_parser.add_argument('--name', default=settings['client']['name'])
|
||||
otp_parser.add_argument('--realm', default=settings['client']['realm'])
|
||||
otp_parser.add_argument('--seed', type=get_token, default=settings['client']['seed'],
|
||||
dest='token', metavar='SEED')
|
||||
return otp_parser
|
||||
|
||||
|
||||
def load_dump_pretty(content):
|
||||
if isinstance(content, bytes):
|
||||
content = content.decode('utf-8')
|
||||
parsed = json.loads(content)
|
||||
return json.dumps(parsed, indent=4, sort_keys=True)
|
||||
|
||||
|
||||
def make_request(*args, data=None, request_method=requests.post):
|
||||
r = request_method(join_path(settings['client']['api_server'], *args), json=data)
|
||||
try:
|
||||
print(load_dump_pretty(r.content))
|
||||
except Exception:
|
||||
print('Error occurred while getting output from api server.')
|
||||
|
||||
|
||||
def get_token(seed):
|
||||
if seed is not None:
|
||||
try:
|
||||
token = TOTP(seed).now()
|
||||
except binascii.Error:
|
||||
raise argparse.ArgumentTypeError('Invalid seed')
|
||||
else:
|
||||
return token
|
||||
Loading…
Add table
Add a link
Reference in a new issue