Skip to content

Commit 096f183

Browse files
committed
support tags in cli config and init
1 parent 2a2b4c5 commit 096f183

4 files changed

Lines changed: 42 additions & 15 deletions

File tree

taskbadger/config.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import dataclasses
2-
import inspect
32
import os
3+
import textwrap
44
from pathlib import Path
55

66
import tomlkit
@@ -18,6 +18,7 @@ class Config:
1818
organization_slug: str = None
1919
project_slug: str = None
2020
host: str = _TB_HOST
21+
tags: dict = None
2122

2223
def is_valid(self):
2324
return bool(self.token and self.organization_slug and self.project_slug)
@@ -51,18 +52,26 @@ def from_dict(config_dict, **overrides) -> "Config":
5152
organization_slug=overrides.get("org") or _from_env("ORG", defaults.get("org")),
5253
project_slug=overrides.get("project") or _from_env("PROJECT", defaults.get("project")),
5354
host=overrides.get("host") or auth.get("host"),
55+
tags=config_dict.get("tags", {}),
5456
)
5557

5658
def __str__(self):
5759
host = ""
58-
if self.host != _TB_HOST:
59-
host = f"\n Host: {self.host}"
60-
return inspect.cleandoc(
61-
f"""
60+
if self.host and self.host != _TB_HOST:
61+
host = f"Host: {self.host or '-'}\n"
62+
tags = ""
63+
if self.tags:
64+
tags = "Tags:\n " + "\n ".join(f"{k}: {v}" for k, v in self.tags.items())
65+
return (
66+
textwrap.dedent(
67+
f"""
6268
Organization slug: {self.organization_slug or "-"}
6369
Project slug: {self.project_slug or "-"}
64-
Auth token: {self.token or "-"}{host}
70+
Auth token: {self.token or "-"}
6571
"""
72+
)
73+
+ host
74+
+ tags
6675
)
6776

6877

@@ -79,6 +88,11 @@ def write_config(config):
7988
)
8089

8190
doc.add("auth", table().add("token", config.token))
91+
if config.tags:
92+
tags = table()
93+
for key, value in config.tags.items():
94+
tags.add(key, value)
95+
doc.add("tags", tags)
8296

8397
config_path = _get_config_path()
8498
if not config_path.parent.exists():

taskbadger/mug.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,9 @@ def __init__(self, settings_or_mug=None):
124124
self._session = ReentrantSession()
125125
self._scope = Scope()
126126

127-
def bind(self, settings):
127+
def bind(self, settings, tags=None):
128128
self.settings = settings
129+
self.scope().tags = tags or {}
129130

130131
def session(self) -> ReentrantSession:
131132
return self._session

taskbadger/sdk.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@ def init(
3737
project_slug: str = None,
3838
token: str = None,
3939
systems: list[System] = None,
40+
tags: dict[str, str] = None,
4041
):
4142
"""Initialize Task Badger client
4243
4344
Call this function once per thread
4445
"""
45-
_init(_TB_HOST, organization_slug, project_slug, token, systems)
46+
_init(_TB_HOST, organization_slug, project_slug, token, systems, tags)
4647

4748

4849
def _init(
@@ -51,6 +52,7 @@ def _init(
5152
project_slug: str = None,
5253
token: str = None,
5354
systems: list[System] = None,
55+
tags: dict[str, str] = None,
5456
):
5557
host = host or os.environ.get("TASKBADGER_HOST", "https://taskbadger.net")
5658
organization_slug = organization_slug or os.environ.get("TASKBADGER_ORG")
@@ -66,7 +68,7 @@ def _init(
6668
project_slug,
6769
systems={system.identifier: system for system in systems},
6870
)
69-
Badger.current.bind(settings)
71+
Badger.current.bind(settings, tags)
7072
else:
7173
raise ConfigurationError(
7274
host=host,

tests/test_cli_config.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ def mock_config_location():
2323

2424
@pytest.fixture()
2525
def _mock_config(mock_config_location):
26-
config = Config(organization_slug="test_org", project_slug="test_project", token="test_token")
26+
config = Config(
27+
organization_slug="test_org",
28+
project_slug="test_project",
29+
token="test_token",
30+
tags={"env": "prod", "host": "localhost"},
31+
)
2732
write_config(config)
2833

2934

@@ -67,7 +72,7 @@ def test_info_args_trump_env():
6772
@pytest.mark.usefixtures("_mock_config")
6873
def test_info_config():
6974
result = runner.invoke(app, ["info"])
70-
_check_output(result, "test_org", "test_project", "test_token")
75+
_check_output(result, "test_org", "test_project", "test_token", tags={"env": "prod", "host": "localhost"})
7176

7277

7378
@mock.patch.dict(
@@ -82,13 +87,13 @@ def test_info_config():
8287
@pytest.mark.usefixtures("_mock_config")
8388
def test_info_config_env():
8489
result = runner.invoke(app, ["info"])
85-
_check_output(result, "org2", "project2", "token2")
90+
_check_output(result, "org2", "project2", "token2", tags={"env": "prod", "host": "localhost"})
8691

8792

8893
@pytest.mark.usefixtures("_mock_config")
8994
def test_info_config_args():
9095
result = runner.invoke(app, ["-o", "org1", "-p", "project1", "info"])
91-
_check_output(result, "org1", "project1", "test_token")
96+
_check_output(result, "org1", "project1", "test_token", tags={"env": "prod", "host": "localhost"})
9297

9398

9499
@mock.patch.dict(
@@ -103,7 +108,7 @@ def test_info_config_args():
103108
@pytest.mark.usefixtures("_mock_config")
104109
def test_info_config_env_args():
105110
result = runner.invoke(app, ["-o", "org1", "-p", "project1", "info"])
106-
_check_output(result, "org1", "project1", "token2")
111+
_check_output(result, "org1", "project1", "token2", tags={"env": "prod", "host": "localhost"})
107112

108113

109114
def test_configure(mock_config_location):
@@ -122,8 +127,13 @@ def test_configure(mock_config_location):
122127
}
123128

124129

125-
def _check_output(result, org, project, token):
130+
def _check_output(result, org, project, token, tags=None):
126131
assert result.exit_code == 0
127132
assert f"Organization slug: {org}" in result.stdout
128133
assert f"Project slug: {project}" in result.stdout
129134
assert f"Auth token: {token}" in result.stdout
135+
if tags:
136+
for key, value in tags.items():
137+
assert f"{key}: {value}" in result.stdout
138+
else:
139+
assert "Tags:" not in result.stdout

0 commit comments

Comments
 (0)