]> Sergey Matveev's repositories - public-inbox.git/blobdiff - script/public-inbox-edit
treewide: "require" + "use" cleanup and docs
[public-inbox.git] / script / public-inbox-edit
index c98840531fd674c1716aeb0a1aed0b77746d725f..e408c7bccdd260f1e37086d41e095240e873552d 100755 (executable)
@@ -8,13 +8,13 @@ use strict;
 use warnings;
 use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev);
 use PublicInbox::AdminEdit;
-use File::Temp qw(tempfile);
+use File::Temp 0.19 (); # 0.19 for TMPDIR
 use PublicInbox::ContentId qw(content_id);
 use PublicInbox::MID qw(mid_clean mids);
 PublicInbox::Admin::check_require('-index');
-require PublicInbox::MIME;
-require PublicInbox::InboxWritable;
-require PublicInbox::Import;
+use PublicInbox::MIME;
+use PublicInbox::InboxWritable;
+use PublicInbox::Import;
 
 my $usage = "$0 -m MESSAGE_ID [--all] [INBOX_DIRS]";
 my $opt = { verbose => 1, all => 0, -min_inbox_version => 2, raw => 0 };
@@ -22,12 +22,11 @@ my @opt = qw(mid|m=s file|F=s raw);
 GetOptions($opt, @PublicInbox::AdminEdit::OPT, @opt) or
        die "bad command-line args\n$usage\n";
 
+my $cfg = eval { PublicInbox::Config->new };
 my $editor = $ENV{MAIL_EDITOR}; # e.g. "mutt -f"
 unless (defined $editor) {
        my $k = 'publicinbox.mailEditor';
-       if (my $cfg = PublicInbox::Admin::config()) {
-               $editor = $cfg->{lc($k)};
-       }
+       $editor = $cfg->{lc($k)} if $cfg;
        unless (defined $editor) {
                warn "\`$k' not configured, trying \`git var GIT_EDITOR'\n";
                chomp($editor = `git var GIT_EDITOR`);
@@ -41,7 +40,7 @@ if (defined $mid && defined $file) {
        die "the --mid and --file options are mutually exclusive\n";
 }
 
-my @ibxs = PublicInbox::Admin::resolve_inboxes(\@ARGV, $opt);
+my @ibxs = PublicInbox::Admin::resolve_inboxes(\@ARGV, $opt, $cfg);
 PublicInbox::AdminEdit::check_editable(\@ibxs);
 
 my $found = {}; # cid => [ [ibx, smsg] [, [ibx, smsg] ] ]
@@ -58,7 +57,7 @@ sub find_mid ($$$) {
                        my $tuple = [ $ibx, $smsg ];
                        push @{$found->{$cid} ||= []}, $tuple
                }
-               delete @$ibx{qw(over mm git search)}; # cleanup
+               PublicInbox::InboxWritable::cleanup($ibx);
        }
        $found;
 }
@@ -120,13 +119,19 @@ $mids
        $found = { $cid => $to_edit };
 }
 
-my $tmpl = 'public-inbox-edit-XXXXXX';
+my %tmpopt = (
+       TEMPLATE => 'public-inbox-edit-XXXXXX',
+       TMPDIR => 1,
+       SUFFIX => $opt->{raw} ? '.eml' : '.mbox',
+);
+
 foreach my $to_edit (values %$found) {
-       my ($edit_fh, $edit_fn) = tempfile($tmpl, TMPDIR => 1, UNLINK => 1);
+       my $edit_fh = File::Temp->new(%tmpopt);
        $edit_fh->autoflush(1);
+       my $edit_fn = $edit_fh->filename;
        my ($ibx, $smsg) = @{$to_edit->[0]};
        my $old_raw = $ibx->msg_by_smsg($smsg);
-       delete @$ibx{qw(over mm git search)}; # cleanup
+       PublicInbox::InboxWritable::cleanup($ibx);
 
        my $tmp = $$old_raw;
        if (!$opt->{raw}) {