Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@casperklein
Copy link
Member

@casperklein casperklein commented Feb 15, 2025

Description

  • Delete the mailbox data only if the user provides explicit confirmation.
  • Make it more clear to the user, when the mailbox data is deleted or not.

Fixes #4338

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Improvement (non-breaking change that does improve existing functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation (README.md or the documentation under docs/)
  • If necessary, I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have added information about changes made in this PR to CHANGELOG.md

@casperklein casperklein self-assigned this Feb 15, 2025
@casperklein casperklein added this to the v15.0.0 milestone Feb 15, 2025
@casperklein casperklein added area/scripts kind/improvement Improve an existing feature, configuration file or the documentation labels Feb 15, 2025
@casperklein casperklein marked this pull request as ready for review February 15, 2025 23:53
MAILDEL=1
fi
# Delete mailbox data only if the user provides explicit confirmation.
[[ ${MAILDEL_CHOSEN,,} == "y" ]] && MAILDEL=1
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

@polarathene polarathene Feb 16, 2025

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)'

y

More 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

@casperklein casperklein merged commit 0ebf820 into docker-mailserver:master Feb 16, 2025
7 checks passed
@casperklein casperklein deleted the delmailuser branch February 16, 2025 23:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/scripts kind/improvement Improve an existing feature, configuration file or the documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug report: mail got deleted even if I say no

3 participants