Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add --clear-cache option #249

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ options:
--search "KEYWORDS" Search for a specific command from a query
-u, --update, --update_cache
Update the local cache of pages and exit
-k, --clear-cache
Delete the local cache of pages and exit
-p PLATFORM, --platform PLATFORM
Override the operating system [linux, osx, sunos, windows, common]
-l, --list List all available commands for operating system
Expand Down Expand Up @@ -103,7 +105,10 @@ In order of precedence:
- `$HOME/.cache/tldr`
- `~/.cache/tldr`

If you are experiencing issues with *tldr*, consider deleting the cache files before trying other measures.
If you are experiencing issues with *tldr*, consider deleting the cache files before trying other measures:
```
tldr --clear-cache
```

#### Autocomplete

Expand Down
44 changes: 44 additions & 0 deletions tldr.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,40 @@ def update_cache(language: Optional[List[str]] = None) -> None:
)


def clear_cache(language: Optional[List[str]] = None) -> None:
languages = get_language_list()
if language and language[0] not in languages:
languages.append(language[0])
for language in languages:
pages_dir = f'pages.{language}' if language != 'en' else 'pages'
cache_dir = get_cache_dir() / pages_dir
if cache_dir.exists() and cache_dir.is_dir():
# Recursively walk through the directory to delete files and subdirectories
for root, dirs, files in os.walk(cache_dir, topdown=False):
for name in files:
file_path = Path(root) / name
try:
file_path.unlink() # Attempt to delete the file
except Exception as e:
print(f"Error: Unable to delete cache file {file_path}: {e}")
for name in dirs:
dir_path = Path(root) / name
try:
dir_path.rmdir() # Attempt to delete the directory
except Exception as e:
print(
f"Error: Unable to delete cache directory {dir_path}: {e}"
)
# Attempt to remove the main cache directory after clearing its contents
try:
cache_dir.rmdir()
Comment on lines +501 to +519
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Recursively walk through the directory to delete files and subdirectories
for root, dirs, files in os.walk(cache_dir, topdown=False):
for name in files:
file_path = Path(root) / name
try:
file_path.unlink() # Attempt to delete the file
except Exception as e:
print(f"Error: Unable to delete cache file {file_path}: {e}")
for name in dirs:
dir_path = Path(root) / name
try:
dir_path.rmdir() # Attempt to delete the directory
except Exception as e:
print(
f"Error: Unable to delete cache directory {dir_path}: {e}"
)
# Attempt to remove the main cache directory after clearing its contents
try:
cache_dir.rmdir()
try:
shutil.rmtree(cache_dir)

print(f"Cleared cache for language {language}")
except Exception as e:
print(f"Error: Unable to delete cache directory {cache_dir}: {e}")
else:
print(f"No cache directory found for language {language}")


def create_parser() -> ArgumentParser:
parser = ArgumentParser(
prog="tldr",
Expand All @@ -514,6 +548,10 @@ def create_parser() -> ArgumentParser:
action='store_true',
help="Update the local cache of pages and exit")

parser.add_argument('-k', '--clear-cache',
action='store_true',
help="Delete the local cache of pages and exit")

parser.add_argument(
'-p', '--platform',
nargs=1,
Expand Down Expand Up @@ -591,6 +629,12 @@ def main() -> None:
elif len(sys.argv) == 1:
parser.print_help(sys.stderr)
sys.exit(1)
if options.clear_cache:
clear_cache(language=options.language)
return
elif len(sys.argv) == 1:
parser.print_help(sys.stderr)
sys.exit(1)
if options.list:
print('\n'.join(get_commands(options.platform, options.language)))
elif options.render:
Expand Down