File Coverage

blib/lib/App/Stow/Check.pm
Criterion Covered Total %
statement 33 54 61.1
branch 1 12 8.3
condition 2 9 22.2
subroutine 9 10 90.0
pod 2 2 100.0
total 47 87 54.0


line stmt bran cond sub pod time code
1             package App::Stow::Check;
2              
3 4     4   67894 use strict;
  4         20  
  4         96  
4 4     4   17 use warnings;
  4         8  
  4         95  
5              
6 4     4   16 use Cwd qw(realpath);
  4         6  
  4         223  
7 4     4   1569 use File::Spec::Functions qw(abs2rel splitdir);
  4         3052  
  4         228  
8 4     4   1752 use File::Which qw(which);
  4         3590  
  4         178  
9 4     4   6787 use Getopt::Std;
  4         188  
  4         251  
10 4     4   1749 use Readonly;
  4         13307  
  4         1682  
11              
12             Readonly::Scalar our $DEFAULT_STOW_DIR => '/usr/local/stow';
13              
14             our $VERSION = 0.03;
15              
16             # Constructor.
17             sub new {
18 2     2 1 1312 my ($class, @params) = @_;
19              
20             # Create object.
21 2         5 my $self = bless {}, $class;
22              
23             # Object.
24 2         8 return $self;
25             }
26              
27             # Run.
28             sub run {
29 1     1 1 1 my $self = shift;
30              
31             # Process arguments.
32 1         7 $self->{'_opts'} = {
33             'd' => $DEFAULT_STOW_DIR,
34             'h' => 0,
35             };
36 1 50 33     5 if (! getopts('d:h', $self->{'_opts'}) || $self->{'_opts'}->{'h'} || @ARGV < 1) {
      33        
37 1         90 print STDERR "Usage: $0 [-d stow_dir] [-h] [--version] command\n";
38 1         15 print STDERR "\t-d stow_dir\tStow directory (default value is '$DEFAULT_STOW_DIR').\n";
39 1         11 print STDERR "\t-h\t\tPrint help.\n";
40 1         10 print STDERR "\t--version\tPrint version.\n";
41 1         10 print STDERR "\tcommand\t\tCommand for which is stow dist looking.\n";
42 1         4 return 1;
43             }
44 0           $self->{'_command'} = $ARGV[0];
45              
46 0           my ($exit_code, $message) = $self->_check;
47 0 0         if ($exit_code == 0) {
48 0           print $message."\n";
49             } else {
50 0           print STDERR $message."\n";
51             }
52              
53 0           return $exit_code;
54             }
55              
56             sub _check {
57 0     0     my $self = shift;
58              
59 0           my $command = $self->{'_command'};
60              
61 0           my $command_path = which($command);
62 0 0         if (! defined $command_path) {
63 0           return (1, "Command '$command' not found.");
64             }
65              
66 0           my $stow_dir = $self->{'_opts'}->{'d'};
67 0 0         if (! -d $stow_dir) {
68 0           return (1, "Stow directory '$stow_dir' doesn't exist.");
69             }
70 0           my $rel_path = abs2rel(realpath($command_path), $stow_dir);
71 0           my @rel_path = splitdir($rel_path);
72 0 0         if ($rel_path[0] eq '..') {
73 0           return (1, "Command '$command' doesn't use stow.");
74             }
75 0 0 0       if ($rel_path[1] ne 'bin' && $rel_path[1] ne 'sbin') {
76 0           return (1, "Command '$command' don't use 'bin/sbin' path.");
77             }
78              
79 0           return (0, $rel_path[0]);
80             }
81              
82             1;
83              
84              
85             __END__