@@ -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:
@@ -1542,17 +1593,17 @@ def shortcuts(self, args):
15421593SHORTCUTS = OrderedDict ([
15431594 ('create' , 'apps:create' ),
15441595 ('destroy' , 'apps:destroy' ),
1545- ('init' , 'clusters:create' ),
15461596 ('info' , 'apps:info' ),
1547- ('run' , 'apps:run' ),
1548- ('open' , 'apps:open' ),
1549- ('logs' , 'apps:logs' ),
1550- ('register' , 'auth:register' ),
1597+ ('init' , 'clusters:create' ),
15511598 ('login' , 'auth:login' ),
15521599 ('logout' , 'auth:logout' ),
1600+ ('logs' , 'apps:logs' ),
1601+ ('open' , 'apps:open' ),
15531602 ('pull' , 'builds:create' ),
1554- ('scale ' , 'ps:scale ' ),
1603+ ('register ' , 'auth:register ' ),
15551604 ('rollback' , 'releases:rollback' ),
1605+ ('run' , 'apps:run' ),
1606+ ('scale' , 'ps:scale' ),
15561607 ('sharing' , 'perms:list' ),
15571608 ('sharing:list' , 'perms:list' ),
15581609 ('sharing:add' , 'perms:create' ),
0 commit comments