Skip to content
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

add a new command opm clean #22

Closed
wants to merge 7 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 63 additions & 9 deletions bin/opm
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ my $SpecialDepPat = qr/^(?:openresty|luajit|ngx_(?:http_)?lua|nginx)$/;

sub err (@);
sub shell ($);
sub is_valid_dist_name ($$);
sub find_sys_install_dir ();
sub get_install_dir ();
sub install_file ($$$);
Expand Down Expand Up @@ -126,6 +127,9 @@ for ($cmd) {
} elsif ($_ eq 'search') {
do_search(@ARGV);

} elsif ($_ eq 'clean') {
do_clean(@ARGV);

} else {
warn "ERROR: unknown command: $cmd\n\n";
usage(1);
Expand Down Expand Up @@ -184,15 +188,7 @@ sub do_build ($) {
my $default_sec = delete $data->{default};

my $dist_name = delete $default_sec->{name};
if (!$dist_name) {
err "$dist_file: key \"name\" not found in the default section.\n";
}

if (length $dist_name < 3
|| $dist_name =~ /[^-\w]|^(?:nginx|luajit|resty|openresty|opm|restydoc.*|ngx_.*|.*-nginx-module)$/i)
{
err "$dist_file: bad dist name: $dist_name\n";
}
is_valid_dist_name($dist_name, $dist_file);

if ($server_build) {
$account = delete $default_sec->{account}
Expand Down Expand Up @@ -2195,6 +2191,51 @@ sub rebase_path ($$$) {
return undef;
}

sub do_clean {

if (@_ == 0) {
err "no clean argument specified.\n";
}

if ($_[0] eq 'dist') {
my $dist_file = "dist.ini";
my $data = read_ini($dist_file);
my $default_sec = $data->{default};
my $dist_name = $default_sec->{name};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need error handling when the name field does not exist or does not have meaningful value. We surely do not want to remove anything than desired later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a little hard for me. wait

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name is a varible not a string literal, right? but I can't find where it is defined in do_build.


is_valid_dist_name($dist_name, $dist_file);

opendir(my $dh, '.')
or err "failed to open directory '.': $!\n";

my @entities = readdir($dh);
for my $entity (@entities) {

if (-d $entity) {

if ($entity =~ /^\Q$dist_name\E-[.\w]*\d[.\w]*$/) {

if (-f "$entity/dist.ini") {
shell "rm -rf $entity";
print "removed directory $entity\n";
}
}
} else {

if ($entity =~ /^\Q$dist_name\E-[.\w]*\d[.\w]*\.tar\.gz$/) {
unlink $entity;
print "removed file $entity\n";
}
}
}
closedir $dh
or err "failed to close directory $dh: $!\n";
} else {
err "unrecognized argument for clean: $_[0]. recognized clean arguments are: " .
join(", ", ('dist'));
}
}

sub err (@) {
die "ERROR: ", @_;
}
Expand All @@ -2206,6 +2247,19 @@ sub shell ($) {
}
}

sub is_valid_dist_name ($$) {
my ($dist_name, $dist_file) = @_;
if (!$dist_name) {
err "$dist_file: key \"name\" not found in the default section.\n";
}

if (length $dist_name < 3
|| $dist_name =~ /[^-\w]|^(?:nginx|luajit|resty|openresty|opm|restydoc.*|ngx_.*|.*-nginx-module)$/i)
{
err "$dist_file: bad dist name: $dist_name\n";
}
}

sub usage {
my $rc = shift;

Expand Down