Skip to content

Commit 9eb677e

Browse files
Auto updating local data if too old (#97)
Co-authored-by: Seth Falco <[email protected]>
1 parent 640027b commit 9eb677e

File tree

5 files changed

+22
-6
lines changed

5 files changed

+22
-6
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ available commands:
9696
-r, --render=PATH render a local page for testing purposes
9797
```
9898

99+
## Configuration
100+
101+
To prevent `tldr` from automatically updating its database, set the environment variable `TLDR_AUTO_UPDATE_DISABLED`.
99102

100103
## Contributing
101104

src/local.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,16 @@ check_localdate(void)
5252
curtime = time(NULL);
5353
oldtime = strtol(buffer, NULL, 10);
5454
difftime = curtime - oldtime;
55-
if (difftime > (60 * 60 * 24 * 7 * 2)) {
55+
if (difftime > (60 * 60 * 24 * 7 * 2) && !getenv(PREVENT_UPDATE_ENV_VARIABLE)) {
5656
/* *INDENT-OFF* */
57-
fprintf(stdout, "%s", ANSI_BOLD_ON);
58-
fprintf(stdout, "%s", ANSI_COLOR_CODE_FG);
59-
fprintf(stdout, "Local data is older than two weeks, use --update to update it.\n\n");
60-
fprintf(stdout, "%s", ANSI_COLOR_RESET_FG);
61-
fprintf(stdout, "%s", ANSI_BOLD_OFF);
57+
fprintf(stdout, "Local database is older than two weeks, attempting to update it...\n");
58+
fprintf(stdout, "To prevent automatic updates, set the environment variable %s.\n", PREVENT_UPDATE_ENV_VARIABLE);
59+
if (update_localdb(0)) {
60+
fprintf(stdout, "%s%s", ANSI_BOLD_ON, ANSI_COLOR_CODE_FG);
61+
fprintf(stdout, "Failed to update local database.\n");
62+
fprintf(stdout, "%s%s", ANSI_COLOR_RESET_FG, ANSI_BOLD_OFF);
63+
}
64+
fprintf(stdout, "\n");
6265
/* *INDENT-ON* */
6366
}
6467

src/parser.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ print_tldrpage(char const *input, char const *poverride)
194194
}
195195
}
196196

197+
if (getenv(PREVENT_UPDATE_ENV_VARIABLE))
198+
return 1;
199+
197200
construct_url(url, URLBUFSIZ, input, platform);
198201

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

211214
free(output);
215+
212216
return 0;
213217
}
214218

src/tldr.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ main(int argc, char **argv)
177177
if (print_tldrpage(buf, pbuf[0] != 0 ? pbuf : NULL)) {
178178
fprintf(stdout, "This page doesn't exist yet!\n");
179179
fprintf(stdout, "Submit new pages here: https://github.com/tldr-pages/tldr\n");
180+
if (getenv(PREVENT_UPDATE_ENV_VARIABLE)) {
181+
fprintf(stdout, "Checking the online database was skipped because automatic updates are disabled.\n");
182+
fprintf(stdout, "You could try updating the local database manually with: tldr --update\n");
183+
}
180184
return EXIT_FAILURE;
181185
}
182186
}

src/tldr.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
#define TLDR_EXT "/.tldrc/tldr/pages/"
4242
#define TLDR_EXT_LEN (sizeof(TLDR_EXT) - 1)
4343

44+
#define PREVENT_UPDATE_ENV_VARIABLE "TLDR_AUTO_UPDATE_DISABLED"
45+
4446
#define ANSI_COLOR_RESET_FG "\x1b[39m"
4547
#define ANSI_COLOR_TITLE_FG "\x1b[39m"
4648
#define ANSI_COLOR_EXPLANATION_FG "\x1b[39m"

0 commit comments

Comments
 (0)