summaryrefslogtreecommitdiff
path: root/icktool
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2017-10-15 15:29:15 +0300
committerLars Wirzenius <liw@liw.fi>2017-10-15 15:29:15 +0300
commitacf8ac6c4ae6c82805dd502ec3f7701943507077 (patch)
tree463ab825762f90a2528e502e097ef6585adbf2a7 /icktool
parent181f76776da5465416160bd571b399da9490f7fd (diff)
downloadick2-acf8ac6c4ae6c82805dd502ec3f7701943507077.tar.gz
Refactor: icktool to be clearer, extensible code
Diffstat (limited to 'icktool')
-rwxr-xr-xicktool100
1 files changed, 69 insertions, 31 deletions
diff --git a/icktool b/icktool
index afeff3d..1842570 100755
--- a/icktool
+++ b/icktool
@@ -68,47 +68,85 @@ class Icktool(cliapp.Application):
logging.captureWarnings(True)
def cmd_token(self, args):
- uri = self.settings['controller']
- if not uri:
- raise cliapp.AppException('no --controller specified')
+ token = self._new_token()
+ sys.stdout.write(token)
+ def cmd_version(self, args):
+ api = self._new_api()
+ self._prettyson(api.get('/version'))
+
+ def _new_token(self):
+ scopes = self.settings['scope']
cmd = self.settings['token-private-key-cmd']
if not cmd:
raise cliapp.AppException('no --token-private-cmd specified')
- scopes = self.settings['scope']
-
- privkey = cliapp.runcmd(['sh', '-c', cmd])
- token = cliapp.runcmd(['./create-token', ' '.join(scopes)], feed_stdin=privkey)
- sys.stdout.write(token.decode('ascii'))
+ gen = TokenGenerator()
+ gen.set_cmd(cmd)
+ gen.set_scopes(scopes)
+ return gen.new_token()
+
+ def _new_api(self):
+ token = self.settings['token'] or self._new_token()
+ api = API()
+ api.set_token(token)
+ api.set_url(self.settings['controller'])
+ api.set_verify(self.settings['verify-tls'])
+ return api
+
+ def _prettyson(self, obj):
+ json.dump(obj, sys.stdout, indent=4)
sys.stdout.write('\n')
- def cmd_version(self, args):
- verify = self.settings['verify-tls']
- token = self.settings['token']
- if not token:
- scopes = self.settings['scope']
- cmd = self.settings['token-private-key-cmd']
- if not cmd:
- raise cliapp.AppException('no --token-private-cmd specified')
-
- privkey = cliapp.runcmd(['sh', '-c', cmd])
- token = cliapp.runcmd(['./create-token', ' '.join(scopes)], feed_stdin=privkey)
- token = token.decode('ascii')
-
- uri = self.settings['controller']
- if not uri:
- raise cliapp.AppException('no --controller specified')
-
- version_url = '{}/version'.format(uri)
+
+class API:
+
+ def __init__(self):
+ self._url = None
+ self._token = None
+ self._verify = True
+
+ def set_url(self, url):
+ self._url = url
+
+ def set_token(self, token):
+ self._token = token
+
+ def set_verify(self, verify):
+ self._verify = verify
+
+ def get(self, path):
+ assert self._url is not None
+ assert self._token is not None
+
+ version_url = '{}/version'.format(self._url)
headers = {
- 'Authorization': 'Bearer {}'.format(token),
+ 'Authorization': 'Bearer {}'.format(self._token),
}
- r = requests.get(version_url, headers=headers, verify=verify)
+ r = requests.get(version_url, headers=headers, verify=self._verify)
+ return r.json()
- obj = r.json()
- json.dump(r.json(), sys.stdout, indent=4)
- sys.stdout.write('\n')
+
+class TokenGenerator:
+
+ def __init__(self):
+ self._cmd = None
+ self._scopes = None
+
+ def set_cmd(self, cmd):
+ self._cmd = cmd
+
+ def set_scopes(self, scopes):
+ self._scopes = scopes
+
+ def new_token(self):
+ assert self._cmd is not None
+ assert self._scopes is not None
+
+ privkey = cliapp.runcmd(['sh', '-c', self._cmd])
+ token = cliapp.runcmd(
+ ['./create-token', ' '.join(self._scopes)], feed_stdin=privkey)
+ return token.decode('ascii')
Icktool(version=ick2.__version__).run()