From b148ac9d9aefb18baddbf9e28ed33b1b8c6e96e6 Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Fri, 16 Nov 2012 09:33:39 +0900 Subject: [PATCH] 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 --- command.py | 40 ++++++++++++++++++++++++++++++++++++++++ main.py | 1 + 2 files changed, 41 insertions(+) diff --git a/command.py b/command.py index babb8338..dc6052a7 100644 --- a/command.py +++ b/command.py @@ -22,6 +22,7 @@ import sys from error import NoSuchProjectError from error import InvalidProjectGroupsError + class Command(object): """Base class for any command line action in repo. """ @@ -33,6 +34,27 @@ class Command(object): def WantPager(self, opt): 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 def OptionParser(self): if self._optparse is None: @@ -49,6 +71,24 @@ class Command(object): """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): """Display usage and terminate. """ diff --git a/main.py b/main.py index 83967f77..9cc2639a 100755 --- a/main.py +++ b/main.py @@ -120,6 +120,7 @@ class _Repo(object): return 1 copts, cargs = cmd.OptionParser.parse_args(argv) + copts = cmd.ReadEnvironmentOptions(copts) if not gopts.no_pager and not isinstance(cmd, InteractiveCommand): config = cmd.manifest.globalConfig