mirror of
https://gerrit.googlesource.com/git-repo
synced 2024-12-21 07:16:21 +00:00
Allow command options to be set from environment variables
Extend the Command base class to allow options to be set from values in environment variables, if the user has not given the option on the command line and the environment variable is set. Derived classes of Command can override the implementation of the method _GetEnvironmentOptions to configure which of its options may be set from environment variables. Change-Id: I7c780bcf9644d6567893d9930984c054bce7351e
This commit is contained in:
parent
a67df63ef1
commit
b148ac9d9a
40
command.py
40
command.py
@ -22,6 +22,7 @@ import sys
|
|||||||
from error import NoSuchProjectError
|
from error import NoSuchProjectError
|
||||||
from error import InvalidProjectGroupsError
|
from error import InvalidProjectGroupsError
|
||||||
|
|
||||||
|
|
||||||
class Command(object):
|
class Command(object):
|
||||||
"""Base class for any command line action in repo.
|
"""Base class for any command line action in repo.
|
||||||
"""
|
"""
|
||||||
@ -33,6 +34,27 @@ class Command(object):
|
|||||||
def WantPager(self, opt):
|
def WantPager(self, opt):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def ReadEnvironmentOptions(self, opts):
|
||||||
|
""" Set options from environment variables. """
|
||||||
|
|
||||||
|
env_options = self._RegisteredEnvironmentOptions()
|
||||||
|
|
||||||
|
for env_key, opt_key in env_options.items():
|
||||||
|
# Get the user-set option value if any
|
||||||
|
opt_value = getattr(opts, opt_key)
|
||||||
|
|
||||||
|
# If the value is set, it means the user has passed it as a command
|
||||||
|
# line option, and we should use that. Otherwise we can try to set it
|
||||||
|
# with the value from the corresponding environment variable.
|
||||||
|
if opt_value is not None:
|
||||||
|
continue
|
||||||
|
|
||||||
|
env_value = os.environ.get(env_key)
|
||||||
|
if env_value is not None:
|
||||||
|
setattr(opts, opt_key, env_value)
|
||||||
|
|
||||||
|
return opts
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def OptionParser(self):
|
def OptionParser(self):
|
||||||
if self._optparse is None:
|
if self._optparse is None:
|
||||||
@ -49,6 +71,24 @@ class Command(object):
|
|||||||
"""Initialize the option parser.
|
"""Initialize the option parser.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
def _RegisteredEnvironmentOptions(self):
|
||||||
|
"""Get options that can be set from environment variables.
|
||||||
|
|
||||||
|
Return a dictionary mapping environment variable name
|
||||||
|
to option key name that it can override.
|
||||||
|
|
||||||
|
Example: {'REPO_MY_OPTION': 'my_option'}
|
||||||
|
|
||||||
|
Will allow the option with key value 'my_option' to be set
|
||||||
|
from the value in the environment variable named 'REPO_MY_OPTION'.
|
||||||
|
|
||||||
|
Note: This does not work properly for options that are explicitly
|
||||||
|
set to None by the user, or options that are defined with a
|
||||||
|
default value other than None.
|
||||||
|
|
||||||
|
"""
|
||||||
|
return {}
|
||||||
|
|
||||||
def Usage(self):
|
def Usage(self):
|
||||||
"""Display usage and terminate.
|
"""Display usage and terminate.
|
||||||
"""
|
"""
|
||||||
|
1
main.py
1
main.py
@ -120,6 +120,7 @@ class _Repo(object):
|
|||||||
return 1
|
return 1
|
||||||
|
|
||||||
copts, cargs = cmd.OptionParser.parse_args(argv)
|
copts, cargs = cmd.OptionParser.parse_args(argv)
|
||||||
|
copts = cmd.ReadEnvironmentOptions(copts)
|
||||||
|
|
||||||
if not gopts.no_pager and not isinstance(cmd, InteractiveCommand):
|
if not gopts.no_pager and not isinstance(cmd, InteractiveCommand):
|
||||||
config = cmd.manifest.globalConfig
|
config = cmd.manifest.globalConfig
|
||||||
|
Loading…
Reference in New Issue
Block a user