Setup-File-TextFragment
view release on metacpan or search on metacpan
lib/Setup/File/TextFragment.pm view on Meta::CPAN
my $is_sym = (-l $path);
my @st = stat($path);
my $exists = $is_sym || (-e _);
my $is_file = (-f _);
my @cmd;
return [412, "$path does not exist"] unless $exists;
return [412, "$path is not a regular file"] if $is_sym||!$is_file;
open my($fh), "<", $path or return [500, "Can't open $path: $!"];
my $text = do { local $/; scalar <$fh> };
my $res;
if ($should_exist) {
$res = Text::Fragment::insert_fragment(
text=>$text, id=>$id, payload=>$payload,
comment_style=>$comment_style, label=>$label, attrs=>$attrs,
good_pattern=>$good_pattern, replace_pattern=>$replace_pattern,
top_style=>$top_style,
);
} else {
$res = Text::Fragment::delete_fragment(
text=>$text, id=>$id,
comment_style=>$comment_style, label=>$label,
);
}
return $res if $res->[0] == 304;
return $res if $res->[0] != 200;
if ($tx_action eq 'check_state') {
if ($should_exist) {
log_info("(DRY) Inserting fragment $id to $path ...")
if $dry_run;
} else {
log_info("(DRY) Deleting fragment $id from $path ...")
if $dry_run;
}
return [200, "Fragment $id needs to be inserted to $path", undef,
{undo_actions=>[
['File::Trash::Undoable::untrash', # restore old file
{path=>$path, suffix=>substr($taid,0,8)}],
['File::Trash::Undoable::trash', # trash new file
{path=>$path, suffix=>substr($taid,0,8)."n"}],
]}];
} elsif ($tx_action eq 'fix_state') {
if ($should_exist) {
log_info("Inserting fragment $id to $path ...");
} else {
log_info("Deleting fragment $id from $path ...");
}
File::Trash::Undoable::trash(
path=>$path, suffix=>substr($taid,0,8), -tx_action=>'fix_state');
open my($fh), ">", $path or return [500, "Can't open: $!"];
print $fh $res->[2]{text};
close $fh or return [500, "Can't write: $!"];
chmod $st[2] & 07777, $path; # XXX ignore error?
unless ($>) { chown $st[4], $st[5], $path } # XXX ignore error?
return [200, "OK"];
}
[400, "Invalid -tx_action"];
}
1;
# ABSTRACT: Insert/delete text fragment in a file (with undo support)
__END__
=pod
=encoding UTF-8
=head1 NAME
Setup::File::TextFragment - Insert/delete text fragment in a file (with undo support)
=head1 VERSION
This document describes version 0.070 of Setup::File::TextFragment (from Perl distribution Setup-File-TextFragment), released on 2021-08-02.
=head1 CONTRIBUTOR
=for stopwords Steven Haryanto
Steven Haryanto <sharyanto@cpan.org>
=head1 FUNCTIONS
=head2 setup_text_fragment
Usage:
setup_text_fragment(%args) -> [$status_code, $reason, $payload, \%result_meta]
InsertE<sol>delete text fragment in a file (with undo support).
On do, will insert fragment to file (or delete, if C<should_exist> is set to
false). On undo, will restore old file.
Unfixable state: file does not exist or not a regular file (directory and
symlink included).
Fixed state: file exists, fragment already exists and with the same content (if
C<should_exist> is true) or fragment already does not exist (if C<should_exist> is
false).
Fixable state: file exists, fragment doesn't exist or payload is not the same
(if C<should_exist> is true) or fragment still exists (if C<should_exist> is
false).
This function is not exported by default, but exportable.
This function is idempotent (repeated invocations with same arguments has the same effect as single invocation). This function supports transactions.
Arguments ('*' denotes required arguments):
( run in 0.673 second using v1.01-cache-2.11-cpan-5511b514fd6 )