Skip to content

Auto updating local data if too old #97

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

Merged
merged 8 commits into from
Apr 22, 2023
Merged
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ available commands:
-r, --render=PATH render a local page for testing purposes
```

## Configuration

To prevent `tldr` from automatically updating its database, set the environment variable `TLDR_AUTO_UPDATE_DISABLED`.

## Contributing

Expand Down
15 changes: 9 additions & 6 deletions src/local.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,16 @@ check_localdate(void)
curtime = time(NULL);
oldtime = strtol(buffer, NULL, 10);
difftime = curtime - oldtime;
if (difftime > (60 * 60 * 24 * 7 * 2)) {
if (difftime > (60 * 60 * 24 * 7 * 2) && !getenv(PREVENT_UPDATE_ENV_VARIABLE)) {
/* *INDENT-OFF* */
fprintf(stdout, "%s", ANSI_BOLD_ON);
fprintf(stdout, "%s", ANSI_COLOR_CODE_FG);
fprintf(stdout, "Local data is older than two weeks, use --update to update it.\n\n");
fprintf(stdout, "%s", ANSI_COLOR_RESET_FG);
fprintf(stdout, "%s", ANSI_BOLD_OFF);
fprintf(stdout, "Local database is older than two weeks, attempting to update it...\n");
fprintf(stdout, "To prevent automatic updates, set the environment variable %s.\n", PREVENT_UPDATE_ENV_VARIABLE);
if (update_localdb(0)) {
fprintf(stdout, "%s%s", ANSI_BOLD_ON, ANSI_COLOR_CODE_FG);
fprintf(stdout, "Failed to update local database.\n");
fprintf(stdout, "%s%s", ANSI_COLOR_RESET_FG, ANSI_BOLD_OFF);
}
fprintf(stdout, "\n");
/* *INDENT-ON* */
}

Expand Down
4 changes: 4 additions & 0 deletions src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ print_tldrpage(char const *input, char const *poverride)
}
}

if (getenv(PREVENT_UPDATE_ENV_VARIABLE))
return 1;

construct_url(url, URLBUFSIZ, input, platform);

/* make clang's static analyzer happy */
Expand All @@ -209,6 +212,7 @@ print_tldrpage(char const *input, char const *poverride)
parse_tldrpage(output);

free(output);

return 0;
}

Expand Down
4 changes: 4 additions & 0 deletions src/tldr.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ main(int argc, char **argv)
if (print_tldrpage(buf, pbuf[0] != 0 ? pbuf : NULL)) {
fprintf(stdout, "This page doesn't exist yet!\n");
fprintf(stdout, "Submit new pages here: https://github.com/tldr-pages/tldr\n");
if (getenv(PREVENT_UPDATE_ENV_VARIABLE)) {
fprintf(stdout, "Checking the online database was skipped because automatic updates are disabled.\n");
fprintf(stdout, "You could try updating the local database manually with: tldr --update\n");
}
return EXIT_FAILURE;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/tldr.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
#define TLDR_EXT "/.tldrc/tldr/pages/"
#define TLDR_EXT_LEN (sizeof(TLDR_EXT) - 1)

#define PREVENT_UPDATE_ENV_VARIABLE "TLDR_AUTO_UPDATE_DISABLED"

#define ANSI_COLOR_RESET_FG "\x1b[39m"
#define ANSI_COLOR_TITLE_FG "\x1b[39m"
#define ANSI_COLOR_EXPLANATION_FG "\x1b[39m"
Expand Down