remove the exclude arg and instead have the revs and notes args be opt.

And then only act on notes/revisions, if the relevant argument got
specified.
This makes the CLI cleaner.
This commit is contained in:
Julian Schacher 2024-12-09 17:50:26 +01:00
parent 4bee60b8dd
commit 07ef29225e
2 changed files with 36 additions and 26 deletions

View file

@ -12,12 +12,16 @@ Currently, it only works with Postgres.
### Expiring old revisions ### Expiring old revisions
Using the `-r` or `--revisions` argument.
All revisions that have been created before the specified age will be removed. If all revisions are expired, the note All revisions that have been created before the specified age will be removed. If all revisions are expired, the note
remains available, it just won't have any revisions to revert to. Once you continue editing it, new revisions will be remains available, it just won't have any revisions to revert to. Once you continue editing it, new revisions will be
added. added.
### Expiring old notes ### Expiring old notes
Using the `-n` or `--notes` argument.
Notes that are being expired will be emailed to the account that initially created the note. This allows that user to Notes that are being expired will be emailed to the account that initially created the note. This allows that user to
restore the note, if necessary. Expiring a note will also remove all existing revisions for the note. restore the note, if necessary. Expiring a note will also remove all existing revisions for the note.
@ -28,10 +32,22 @@ be recovered.
## Running `hedgedoc-expire.py` ## Running `hedgedoc-expire.py`
Locally from the command line: Locally from the command line expiring revisions and notes older than 7 days:
```shell ```shell
poetry run python ./hedgedoc-expire.py ... poetry run python ./hedgedoc-expire.py expire -n 7 -r 7
```
Locally from the command line only expiring revisions older than 7 days:
```shell
poetry run python ./hedgedoc-expire.py expire -r 7
```
Locally from the command line only expiring notes older than 7 days:
```shell
poetry run python ./hedgedoc-expire.py expire -n 7
``` ```
From Docker Compose: From Docker Compose:
@ -65,8 +81,8 @@ Without it, the expiry action will be taken.
| Option | Default | Description | | Option | Default | Description |
|--------|---------|-------------------------------------------------------------------| |--------|---------|-------------------------------------------------------------------|
| -n | 90 | remove all notes not changed in these many days | | -n | None | remove all notes not changed in these many days |
| -r | 7 | remove all revisions created more than these many days ago | | -r | None | remove all revisions created more than these many days ago |
| -v | false | Print info on current action during `cron` and `expire` commandds | | -v | false | Print info on current action during `cron` and `expire` commandds |
Command is one of: Command is one of:

View file

@ -29,7 +29,6 @@ class Config:
self.verbose = False self.verbose = False
self.revision_age = timedelta(days=14) self.revision_age = timedelta(days=14)
self.note_age = timedelta(days=95) self.note_age = timedelta(days=95)
self.exclude = []
self.postgres_connection_string = getenv('POSTGRES_CONNSTRING', 'postgresql://hedgedoc:geheim@localhost:5432/hedgedoc') self.postgres_connection_string = getenv('POSTGRES_CONNSTRING', 'postgresql://hedgedoc:geheim@localhost:5432/hedgedoc')
@ -276,29 +275,29 @@ class HedgedocExpire:
def cmd_check(self) -> None: def cmd_check(self) -> None:
with psycopg.connect(self.config.postgres_connection_string) as conn: with psycopg.connect(self.config.postgres_connection_string) as conn:
if 'revision' not in self.config.exclude: if self.config.revision_age is not None:
print(self.check_revisions_to_be_expired(conn)) print(self.check_revisions_to_be_expired(conn))
elif self.config.verbose: elif self.config.verbose:
print("Revisions were excluded from check, not checking.\n") print("Revisions weren't included in the check, not checking.\n")
if 'note' not in self.config.exclude: if self.config.note_age is not None:
print(self.check_notes_to_be_expired(conn)) print(self.check_notes_to_be_expired(conn))
elif self.config.verbose: elif self.config.verbose:
print("Notes were excluded from check, not checking.\n") print("Notes weren't included in the check, not checking.\n")
def cmd_emailcheck(self) -> None: def cmd_emailcheck(self) -> None:
with psycopg.connect(self.config.postgres_connection_string) as conn: with psycopg.connect(self.config.postgres_connection_string) as conn:
report = '' report = ''
if 'revision' not in self.config.exclude: if self.config.revision_age is not None:
report += self.check_revisions_to_be_expired(conn) report += self.check_revisions_to_be_expired(conn)
else: else:
report += "Revisions were excluded from check.\n" report += "Revisions weren't included in the check.\n"
if 'note' not in self.config.exclude: if self.config.note_age is not None:
report += self.check_notes_to_be_expired(conn) report += self.check_notes_to_be_expired(conn)
else: else:
report += "Notes were excluded from check.\n" report += "Notes weren't included in the check.\n"
msg = MIMEMultipart() msg = MIMEMultipart()
msg['From'] = self.email_sender.mail_from msg['From'] = self.email_sender.mail_from
msg['To'] = self.email_sender.mail_from msg['To'] = self.email_sender.mail_from
@ -315,15 +314,15 @@ class HedgedocExpire:
def cmd_expire(self) -> None: def cmd_expire(self) -> None:
with psycopg.connect(self.config.postgres_connection_string) as conn: with psycopg.connect(self.config.postgres_connection_string) as conn:
if 'revision' not in self.config.exclude: if self.config.revision_age is not None:
self.expire_old_revisions(conn) self.expire_old_revisions(conn)
elif self.config.verbose: elif self.config.verbose:
print("Revisions were excluded from action, not expiring.\n") print("Revisions weren't included in the expire action, not expiring.\n")
if 'note' not in self.config.exclude: if self.config.note_age is not None:
self.expire_old_notes(conn) self.expire_old_notes(conn)
elif self.config.verbose: elif self.config.verbose:
print("Notes were excluded from action, not expiring.\n") print("Notes weren't included in the expire action, not expiring.\n")
def main(): def main():
@ -346,25 +345,20 @@ def main():
See https://git.hamburg.ccc.de/CCCHH/hedgedoc-expire See https://git.hamburg.ccc.de/CCCHH/hedgedoc-expire
''') ''')
) )
parser.add_argument('-n', '--notes', metavar='DAYS', type=float, default=95, parser.add_argument('-n', '--notes', metavar='DAYS', type=float,
help='remove all notes not changed in these many days') help='remove all notes not changed in these many days')
parser.add_argument('-r', '--revisions', metavar='DAYS', type=float, default=14, parser.add_argument('-r', '--revisions', metavar='DAYS', type=float,
help='remove all revisions created more than these many days ago') help='remove all revisions created more than these many days ago')
parser.add_argument('command', choices=['check', 'cron', 'emailcheck', 'expire'], default='check', nargs='?', parser.add_argument('command', choices=['check', 'cron', 'emailcheck', 'expire'], default='check', nargs='?',
help='action to perform') help='action to perform')
parser.add_argument('-v', '--verbose', action='store_true', default=False, parser.add_argument('-v', '--verbose', action='store_true', default=False,
help='print more info while running') help='print more info while running')
parser.add_argument('--exclude', nargs="+", choices=['revision', 'note'],
help='Type ("revision" or "note") to exclude from the action.')
args = parser.parse_args() args = parser.parse_args()
config = Config() config = Config()
config.note_age = timedelta(days=args.notes) config.note_age = timedelta(days=args.notes) if args.notes is not None else None
config.revision_age = timedelta(days=args.revisions) config.revision_age = timedelta(days=args.revisions) if args.revisions is not None else None
config.verbose = args.verbose config.verbose = args.verbose
if (args.exclude):
config.exclude = args.exclude
print(config.exclude)
mail = EmailSender(config.smtp_hostname, config.smtp_port, config.smtp_username, config.smtp_password, mail = EmailSender(config.smtp_hostname, config.smtp_port, config.smtp_username, config.smtp_password,
config.smtp_from) config.smtp_from)
hedgedoc_expire = HedgedocExpire(config, mail) hedgedoc_expire = HedgedocExpire(config, mail)