Skip to content

Commit 401e702

Browse files
committed
feat: allow an environment variable to specify config location
It can be useful (especially in scripts) to specify a configuration location via an environment variable. If the "PYTHON_GITLAB_CFG" environment variable is defined, treat its value as the path to a configuration file and include it in the set of default configuration locations.
1 parent c7c431a commit 401e702

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

docs/cli.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ Configuration
1414
Files
1515
-----
1616

17-
``gitlab`` looks up 2 configuration files by default:
17+
``gitlab`` looks up 3 configuration files by default:
18+
19+
``PYTHON_GITLAB_CFG`` environment variable
20+
An environment variable that contains the path to a configuration file
1821

1922
``/etc/python-gitlab.cfg``
2023
System-wide configuration file

gitlab/config.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,17 @@
1818
import os
1919
import configparser
2020

21-
_DEFAULT_FILES = ["/etc/python-gitlab.cfg", os.path.expanduser("~/.python-gitlab.cfg")]
21+
22+
def _env_config():
23+
if "PYTHON_GITLAB_CFG" in os.environ:
24+
return [os.environ["PYTHON_GITLAB_CFG"]]
25+
return []
26+
27+
28+
_DEFAULT_FILES = _env_config() + [
29+
"/etc/python-gitlab.cfg",
30+
os.path.expanduser("~/.python-gitlab.cfg"),
31+
]
2232

2333

2434
class ConfigError(Exception):

gitlab/tests/test_config.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# You should have received a copy of the GNU Lesser General Public License
1616
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1717

18+
import os
1819
import unittest
1920

2021
import mock
@@ -72,6 +73,16 @@
7273
"""
7374

7475

76+
class TestEnvConfig(unittest.TestCase):
77+
def test_env_present(self):
78+
with mock.patch.dict(os.environ, {"PYTHON_GITLAB_CFG": "/some/path"}):
79+
self.assertEqual(["/some/path"], config._env_config())
80+
81+
def test_env_missing(self):
82+
with mock.patch.dict(os.environ, {}, clear=True):
83+
self.assertEqual([], config._env_config())
84+
85+
7586
class TestConfigParser(unittest.TestCase):
7687
@mock.patch("os.path.exists")
7788
def test_missing_config(self, path_exists):

0 commit comments

Comments
 (0)