Skip to content

Commit 99dfdf1

Browse files
author
Matthew Fisher
committed
feat(client): add config:pull command
This command pulls in existing config variables set with deis config:set and writes them out to a local .env file for development purposes. If a .env file exists and a environment variable in that file is present in the application's config, the file's envvar will be chosen unless the --overwrite option is selected. If --interactive is present, a prompt to write the environment variable to the file will be shown. fixes #560
1 parent 36de49c commit 99dfdf1

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

client/deis.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,7 @@ def config(self, args):
970970
config:list list environment variables for an app
971971
config:set set environment variables for an app
972972
config:unset unset environment variables for an app
973+
config:pull extract environment variables to .env
973974
974975
Use `deis help [command]` to learn more
975976
"""
@@ -1084,6 +1085,56 @@ def config_unset(self, args):
10841085
else:
10851086
raise ResponseError(response)
10861087

1088+
def config_pull(self, args):
1089+
"""
1090+
Extract all environment variables from an application for local use.
1091+
1092+
Your environment will be stored locally in a file named .env. This file can be
1093+
read by foreman to load the local environment for your app.
1094+
1095+
Usage: deis config:pull [options]
1096+
1097+
Options:
1098+
-a APP --app=APP The application that you wish to pull from
1099+
-i --interactive Prompts for each value to be overwritten
1100+
-o --overwrite Allows you to have the pull overwrite keys in .env
1101+
"""
1102+
app = args.get('--app')
1103+
overwrite = args.get('--overwrite')
1104+
interactive = args.get('--interactive')
1105+
env_dict = {}
1106+
if not app:
1107+
app = self._session.app
1108+
try:
1109+
# load env_dict from existing .env, if it exists
1110+
with open('.env') as f:
1111+
for line in f.readlines():
1112+
k, v = line.split('=', 1)[0], line.split('=', 1)[1].strip('\n')
1113+
env_dict[k] = v
1114+
except IOError:
1115+
pass
1116+
response = self._dispatch('get', "/api/apps/{}/config".format(app))
1117+
if response.status_code == requests.codes.ok: # @UndefinedVariable
1118+
config = json.loads(response.json()['values'])
1119+
for k, v in config.items():
1120+
if interactive:
1121+
confirm = raw_input('overwrite {} with {}? (y/N) '.format(k, v))
1122+
if confirm == 'y':
1123+
env_dict[k] = v
1124+
if k in env_dict and not overwrite:
1125+
continue
1126+
env_dict[k] = v
1127+
# write env_dict to .env
1128+
try:
1129+
with open('.env', 'w') as f:
1130+
for i in env_dict.keys():
1131+
f.write("{}={}\n".format(i, env_dict[i]))
1132+
except IOError:
1133+
print('could not write to local env')
1134+
sys.exit(1)
1135+
else:
1136+
raise ResponseError(response)
1137+
10871138
def domains(self, args):
10881139
"""
10891140
Valid commands for domains:

docs/using_deis/config-application.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Use ``deis config`` to modify environment variables for a deployed application.
1919
config:list list environment variables for an app
2020
config:set set environment variables for an app
2121
config:unset unset environment variables for an app
22+
config:pull extract environment variables to .env
2223
2324
Use `deis help [command]` to learn more
2425

0 commit comments

Comments
 (0)