-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Make deletion of mailbox data opt-in #4365
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
Make deletion of mailbox data opt-in #4365
Conversation
| MAILDEL=1 | ||
| fi | ||
| # Delete mailbox data only if the user provides explicit confirmation. | ||
| [[ ${MAILDEL_CHOSEN,,} == "y" ]] && MAILDEL=1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the ,, portion meant to do?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes the var value lowercase.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fun 😒
While a bit more verbose, this is the equivalent with Nu:
if ($MAILDEL_CHOSEN | str downcase) == 'y' { $MAILDEL = true }Container example:
$ docker run --rm ghcr.io/nushell/nushell -c 'mut MAILDEL_CHOSEN = "Y" ; print ($MAILDEL_CHOSEN | str downcase)'
yMore fleshed out example
Similar to a previous example I shared in the past to compare to Bash, here's what some more NuShell looks like (I'm still not really sold on it, has some good/bad aspects 😅)
#!/usr/bin/env nu
# Implicit default method run:
def main [] {
print "this is setup CLI"
}
# Subcommand (see usage example below),
# also implicitly has `--help` generated `email del --help` (can be improved)
# Args are between the [], a bool flag with long and short forms,
# and spread syntax to indicate the rest of the CLI args will be collected as a list variable
def "main email del" [--yes (-y), ...accounts] {
# `let` is immutable (read-only) after assignment
let force_skip = $yes
# `mut` allow updating the variable after assignment
mut should_delete = false
if not $force_skip {
print 'Do you want to delete the mailbox data as well (removing all mails)? [y/N]'
# Read the next input from the user, lowercase it and compare to get true/false:
# `--numchar 1` is like `read -n 1` stopping after the first char input from the user.
# `--suppress-output` so regardless of char when input is not `y`, we later print `no`.
$should_delete = (input --numchar 1 --suppress-output | str downcase) == 'y'
# Alternative that provides an interactive prompt and converts selection to boolean:
# https://github.com/nushell/nushell/issues/15124
#$should_delete = try { (['no', 'yes'] | input list --index | into bool) } catch { false }
# Formats the bool result as yes/no for optionally logging to stdout:
print (if $should_delete { 'yes' } else { 'no' })
}
# Iterate through the array of 1 or more accounts:
for account in $accounts {
if $should_delete { delete_mailbox $account }
}
}
# Pretend this has full functionality of `_remove_maildir()` :)
def delete_mailbox [account: string] {
# This variable will split the email address at `@` into subfields `.local` + `.domain`:
let mail_parts = ($account | split column --number 2 '@' local domain | first)
let mailbox_path = $"/var/mail/($mail_parts.domain)/($mail_parts.local)"
print $"Deleting Mailbox: '($mailbox_path)'"
}$ ./setup.nu email del [email protected]
Do you want to delete the mailbox data as well (removing all mails)? [y/N]
yes
Deleting Mailbox: '/var/mail/example.test/john.doe'Some relevant NuShell doc links if you're interested
- https://www.nushell.sh/book/working_with_strings.html
- https://www.nushell.sh/commands/categories/strings.html
- https://www.nushell.sh/commands/docs/str_downcase.html
- https://www.nushell.sh/commands/docs/input.html
- https://www.nushell.sh/commands/docs/split_column.html
- https://www.nushell.sh/book/cheat_sheet.html#variables
- https://www.nushell.sh/commands/
- https://www.nushell.sh/book/thinking_in_nu.html#nushell-isn-t-bash
- An issue of mine with some basic examples / insights for prompting input
Description
Fixes #4338
Type of change
Checklist
docs/)CHANGELOG.md