Permalink
Browse files

feat: Support access token in LINODE_TOKEN environment variable.

  • Loading branch information...
alxbl committed Oct 6, 2018
1 parent 2e5a48d commit 549ad4b04da78672513a1c15a6a6cc101099dba4
Showing with 29 additions and 18 deletions.
  1. +1 −0 .gitignore
  2. +28 −18 linodecli/configuration.py
View
@@ -7,4 +7,5 @@ linode-cli.sh
data-2
data-3
.DS_STORE
Pipfile*
View
@@ -15,6 +15,7 @@
import os
import sys
ENV_TOKEN_NAME='LINODE_TOKEN'
CONFIG_DIR = os.path.expanduser('~')
CONFIG_NAME = '.linode-cli'
@@ -36,7 +37,7 @@ def __init__(self, base_url, username=None, skip_config=False):
self.base_url = base_url
self.username = username
self.config = self._get_config()
if not self.config.has_option('DEFAULT', 'token') and not skip_config:
if not self.config.has_option('DEFAULT', 'token') and not skip_config and not os.environ[ENV_TOKEN_NAME]:
self.configure()
def update_namespace(self, namespace, new_dict):
@@ -69,7 +70,8 @@ def get_token(self, username=None):
"""
Returns the token for a configured user
"""
return self.config.get(username or 'DEFAULT', "token")
t = os.environ[ENV_TOKEN_NAME]
return t if t else self.config.get(username or 'DEFAULT', "token")
def configure(self, username=None):
"""
@@ -81,28 +83,36 @@ def configure(self, username=None):
is_default = username == None
print("""Welcome to the Linode CLI. This will walk you through some
initial setup.
initial setup.""")
if ENV_TOKEN_NAME in os.environ:
print("""Using token from LINODE_TOKEN.
Note that no token will be saved in your configuration file.
* If you lose or remove LINODE_TOKEN, Linode CLI will stop working.
* All profiles will use LINODE_TOKEN.""")
username = 'DEFAULT'
else:
print("""
First, we need a Personal Access Token. To get one, please visit
{} and click
"Create a Personal Access Token". The CLI needs access to everything
on your account to work correctly.
""".format(TOKEN_GENERATION_URL))
on your account to work correctly.""".format(TOKEN_GENERATION_URL))
while True:
config['token'] = input_helper("Personal Access Token: ")
while True:
config['token'] = input_helper("Personal Access Token: ")
u = self._do_get_request('/profile', token=config['token'], exit_on_error=False)
if "errors" in u:
print("That token didn't work: {}".format(','.join([c["reason"] for c in u['errors']])))
continue
u = self._do_get_request('/profile', token=config['token'], exit_on_error=False)
if "errors" in u:
print("That token didn't work: {}".format(','.join([c["reason"] for c in u['errors']])))
continue
if username is None:
username = u['username']
elif u['username'] != username:
print("That is a token for {}, not {}\n".format(u['username'], username))
continue
break
if username is None:
username = u['username']
elif u['username'] != username:
print("That is a token for {}, not {}\n".format(u['username'], username))
continue
break
regions = [r['id'] for r in self._do_get_request('/regions')['data']]
types = [t['id'] for t in self._do_get_request('/linode/types')['data']]
@@ -114,7 +124,7 @@ def configure(self, username=None):
regions,
'Default Region (Optional): ',
'Please select a valid Region, or press Enter to skip')
config['type'] = self._default_thing_input(
'Default Type of Linode to deploy.',
types,

0 comments on commit 549ad4b

Please sign in to comment.