1919
2020@click .group ()
2121@click .option ('-f' , '--file' , default = os .path .join (os .getcwd (), '.env' ),
22- type = click .Path (exists = True ),
22+ type = click .Path (file_okay = True ),
2323 help = "Location of the .env file, defaults to .env file in current working directory." )
2424@click .option ('-q' , '--quote' , default = 'always' ,
2525 type = click .Choice (['always' , 'never' , 'auto' ]),
2626 help = "Whether to quote or not the variable values. Default mode is always. This does not affect parsing." )
27+ @click .option ('-e' , '--export' , default = False ,
28+ type = click .BOOL ,
29+ help = "Whether to write the dot file as an executable bash script." )
2730@click .version_option (version = __version__ )
2831@click .pass_context
29- def cli (ctx , file , quote ):
30- # type: (click.Context, Any, Any) -> None
32+ def cli (ctx , file , quote , export ):
33+ # type: (click.Context, Any, Any, Any ) -> None
3134 '''This script is used to set, get or unset values from a .env file.'''
3235 ctx .obj = {}
33- ctx .obj ['FILE' ] = file
3436 ctx .obj ['QUOTE' ] = quote
37+ ctx .obj ['EXPORT' ] = export
38+ ctx .obj ['FILE' ] = file
3539
3640
3741@cli .command ()
@@ -40,6 +44,11 @@ def list(ctx):
4044 # type: (click.Context) -> None
4145 '''Display all the stored key/value.'''
4246 file = ctx .obj ['FILE' ]
47+ if not os .path .isfile (file ):
48+ raise click .BadParameter (
49+ 'Path "%s" does not exist.' % (file ),
50+ ctx = ctx
51+ )
4352 dotenv_as_dict = dotenv_values (file )
4453 for k , v in dotenv_as_dict .items ():
4554 click .echo ('%s=%s' % (k , v ))
@@ -54,7 +63,8 @@ def set(ctx, key, value):
5463 '''Store the given key/value.'''
5564 file = ctx .obj ['FILE' ]
5665 quote = ctx .obj ['QUOTE' ]
57- success , key , value = set_key (file , key , value , quote )
66+ export = ctx .obj ['EXPORT' ]
67+ success , key , value = set_key (file , key , value , quote , export )
5868 if success :
5969 click .echo ('%s=%s' % (key , value ))
6070 else :
@@ -68,6 +78,11 @@ def get(ctx, key):
6878 # type: (click.Context, Any) -> None
6979 '''Retrieve the value for the given key.'''
7080 file = ctx .obj ['FILE' ]
81+ if not os .path .isfile (file ):
82+ raise click .BadParameter (
83+ 'Path "%s" does not exist.' % (file ),
84+ ctx = ctx
85+ )
7186 stored_value = get_key (file , key )
7287 if stored_value :
7388 click .echo ('%s=%s' % (key , stored_value ))
@@ -97,6 +112,11 @@ def run(ctx, commandline):
97112 # type: (click.Context, List[str]) -> None
98113 """Run command with environment variables present."""
99114 file = ctx .obj ['FILE' ]
115+ if not os .path .isfile (file ):
116+ raise click .BadParameter (
117+ 'Invalid value for \' -f\' "%s" does not exist.' % (file ),
118+ ctx = ctx
119+ )
100120 dotenv_as_dict = {to_env (k ): to_env (v ) for (k , v ) in dotenv_values (file ).items () if v is not None }
101121
102122 if not commandline :
0 commit comments