Skip to content

Commit 0f33983

Browse files
authored
feat: clear cache through the cli (#271)
1 parent 583c262 commit 0f33983

File tree

5 files changed

+55
-6
lines changed

5 files changed

+55
-6
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,20 @@ The file format for each reference is as follows:
169169
rows: {project: string}[]
170170
}
171171
```
172+
173+
### Cache
174+
By default, Twyn will cache the list of trusted packages to a cache file (.twyn/trusted_packages.json).
175+
176+
You can disable the cache by adding the following flag:
177+
178+
```python
179+
twyn run --no-cache
180+
```
181+
In which case it will download again the list of trusted packages, withou saving them to the cache file.
182+
183+
Cache file is valid for 30 days, after that period it will download again the trusted packages list.
184+
185+
To clear the cache, run:
186+
```python
187+
twyn run cache clear
188+
```

src/twyn/cli.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import logging
12
import sys
23
from typing import Optional
34

45
import click
6+
from rich.logging import RichHandler
57

68
from twyn.__version__ import __version__
79
from twyn.base.constants import (
@@ -13,6 +15,14 @@
1315
from twyn.config.config_handler import ConfigHandler
1416
from twyn.file_handler.file_handler import FileHandler
1517
from twyn.main import check_dependencies
18+
from twyn.trusted_packages.constants import TRUSTED_PACKAGES_FILE_PATH
19+
20+
logging.basicConfig(
21+
format="%(message)s",
22+
datefmt="[%X]",
23+
handlers=[RichHandler(rich_tracebacks=True, show_path=False)],
24+
)
25+
logger = logging.getLogger("twyn")
1626

1727

1828
@click.group()
@@ -137,5 +147,21 @@ def remove(package_name: str, config: str) -> None:
137147
ConfigHandler(fh).remove_package_from_allowlist(package_name)
138148

139149

150+
@entry_point.group()
151+
def cache() -> None:
152+
pass
153+
154+
155+
@cache.command()
156+
def clear() -> None:
157+
fp = FileHandler(TRUSTED_PACKAGES_FILE_PATH).file_path
158+
if fp.exists():
159+
fp.unlink()
160+
fp.parent.rmdir()
161+
logger.warning("Cache has been cleared")
162+
else:
163+
logger.warning("Could not clear cache. Cache file not found.")
164+
165+
140166
if __name__ == "__main__":
141167
sys.exit(entry_point())

src/twyn/main.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import logging
22
from typing import Optional
33

4-
from rich.logging import RichHandler
54
from rich.progress import track
65

76
from twyn.base.constants import (
@@ -22,11 +21,6 @@
2221
TyposquatCheckResult,
2322
)
2423

25-
logging.basicConfig(
26-
format="%(message)s",
27-
datefmt="[%X]",
28-
handlers=[RichHandler(rich_tracebacks=True, show_path=False)],
29-
)
3024
logger = logging.getLogger("twyn")
3125

3226

tests/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def tmp_cache_file(tmp_path: Path, data: Union[dict[str, Any], None] = None) ->
4343

4444
with (
4545
mock.patch("twyn.trusted_packages.references.TRUSTED_PACKAGES_FILE_PATH", str(cache_file)),
46+
mock.patch("twyn.cli.TRUSTED_PACKAGES_FILE_PATH", str(cache_file)),
4647
):
4748
yield cache_file
4849

tests/main/test_cli.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@
77

88

99
class TestCli:
10+
def test_cache_rm_erases_cache(self, tmp_cache_file):
11+
runner = CliRunner()
12+
13+
tmp_cache_file.write_text("{}")
14+
assert tmp_cache_file.exists()
15+
16+
result = runner.invoke(cli.cache.commands["clear"])
17+
18+
assert result.exit_code == 0
19+
assert not tmp_cache_file.exists()
20+
1021
@patch("twyn.cli.check_dependencies")
1122
def test_no_cache_option_disables_cache(self, mock_check_dependencies):
1223
runner = CliRunner()

0 commit comments

Comments
 (0)