| 1 | package App::pathed; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Config; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Getopt::Long; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Pod::Usage; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Pod::Find qw(pod_where); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | our $VERSION = '0.07'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 0 | ▶ | sub usage { | ||||||||||||||||||||||||||
|
Subroutine
usage: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 11 | 0 | pod2usage(-input => pod_where({ -inc => 1 }, __PACKAGE__), @_); | |||||||||||||||||||||||||||
| 12 | } | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 0 | ▶ | sub run { | ||||||||||||||||||||||||||
|
Subroutine
run: not called CC=13 SCAR=52.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 15 | 0 | my %opt; | |||||||||||||||||||||||||||
| 16 | 0 | ▶ | GetOptions( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | \%opt, qw( | ||||||||||||||||||||||||||||
| 18 | delete|d=s@ append|a=s@ prepend|p=s@ | ||||||||||||||||||||||||||||
| 19 | unique|u split|s check|c | ||||||||||||||||||||||||||||
| 20 | var|v=s sep|e=s help|h man | ||||||||||||||||||||||||||||
| 21 | ) | ||||||||||||||||||||||||||||
| 22 | ) or usage(-exitval => 2); | ||||||||||||||||||||||||||||
| 23 | 0 | ▶ | usage(-exitval => 1) if $opt{help}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 0 | ▶ | usage(-exitval => 0, -verbose => 2, -output => \*STDERR) if $opt{man}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | usage(-exitval => 2, -msg => '--split and --check are mutually exclusive') | ||||||||||||||||||||||||||||
| 26 | 0 | ▶ | if $opt{split} && $opt{check}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$opt{"split"} and $opt{"check"}
MC/DC0%0/2
$opt{"split"} and $opt{"check"}
$opt{"split"}
$opt{"check"}
Truth table0%0/3
A and B
A= $opt{"split"}B= $opt{"check"}
| |||||||||||||||||||||||||||||
| 27 | 0 | my $path = shift @ARGV; | |||||||||||||||||||||||||||
| 28 | usage( | ||||||||||||||||||||||||||||
| 29 | -exitval => 2, | ||||||||||||||||||||||||||||
| 30 | -msg => 'using a path argument and --var and are mutually exclusive' | ||||||||||||||||||||||||||||
| 31 | 0 | ▶ | ) if defined $path && $opt{var}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $path and $opt{"var"}
MC/DC0%0/2
defined $path and $opt{"var"}
defined $path
$opt{"var"}
Truth table0%0/3
A and B
A= defined $pathB= $opt{"var"}
| |||||||||||||||||||||||||||||
| 32 | 0 | ▶ | unless (defined $path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 0 | ▶ | if ($opt{var}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 0 | $path = $ENV{ $opt{var} }; | |||||||||||||||||||||||||||
| 35 | 0 | ▶ | unless (defined $path && length $path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $path and length $path
MC/DC0%0/2
defined $path and length $path
defined $path
length $path
Truth table0%0/3
A and B
A= defined $pathB= length $path
| |||||||||||||||||||||||||||||
| 36 | 0 | die "The $opt{var} environment variable is empty\n"; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | } else { | ||||||||||||||||||||||||||||
| 39 | 0 | $path = $ENV{PATH}; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | 0 | my @result = process($path, \%opt); | |||||||||||||||||||||||||||
| 43 | 0 | print "$_\n" for @result; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # separate methods so it's easily testable | ||||||||||||||||||||||||||||
| 47 | 12 | ▶ | sub process { | ||||||||||||||||||||||||||
|
Subroutine
process: called CC=12 SCAR=24.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 48 | 12 | my ($path, $opt) = @_; | |||||||||||||||||||||||||||
| 49 | 12 | ▶ | my $separator = $opt->{sep} // $Config::Config{path_sep}; | ||||||||||||||||||||||||||
|
Condition66%2/3
$opt->{"sep"} // $Config::Config{"path_sep"}
MC/DC0%0/2
$opt->{"sep"} // $Config::Config{"path_sep"}
$opt->{"sep"}
$Config::Config{"path_sep"}
Truth table66%2/3
A // B
A= $opt->{"sep"}B= $Config::Config{"path_sep"}
| |||||||||||||||||||||||||||||
| 50 | 12 | my @parts = split $separator => $path; | |||||||||||||||||||||||||||
| 51 | 12 | ▶ | if ($opt->{append}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 2 | push @parts, @{ $opt->{append} }; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | 12 | ▶ | if ($opt->{prepend}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 2 | unshift @parts, reverse @{ $opt->{prepend} }; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | 12 | ▶ | if ($opt->{delete}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 6 | for my $delete (@{ $opt->{delete} }) { | |||||||||||||||||||||||||||
| 59 | 8 | @parts = grep { index($_, $delete) == -1 } @parts; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | 12 | ▶ | if ($opt->{unique}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 1 | my %seen; | |||||||||||||||||||||||||||
| 64 | 1 | @parts = grep { !$seen{$_}++ } @parts; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | 12 | ▶ | if ($opt->{check}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 67 | 1 | my (%seen, @result); | |||||||||||||||||||||||||||
| 68 | 1 | for my $part (@parts) { | |||||||||||||||||||||||||||
| 69 | 5 | ▶ | next if $seen{$part}++; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 4 | ▶ | next if -r $part; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 2 | push @result => "$part is not readable"; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | 1 | return @result; | |||||||||||||||||||||||||||
| 74 | } elsif ($opt->{split}) { | ||||||||||||||||||||||||||||
| 75 | 3 | return @parts; | |||||||||||||||||||||||||||
| 76 | } else { | ||||||||||||||||||||||||||||
| 77 | 8 | return (join $separator => @parts); | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | 1 | 1; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =pod | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | =head1 NAME | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | App::pathed - munge the Bash PATH environment variable | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | $ PATH=$(pathed --unique --delete rbenv) | ||||||||||||||||||||||||||||
| 91 | $ PATH=$(pathed --append /home/my/bin -a /some/other/bin) | ||||||||||||||||||||||||||||
| 92 | $ PATH=$(pathed --prepend /home/my/bin -p /some/other/bin) | ||||||||||||||||||||||||||||
| 93 | $ for i in $(pathed --split); do ...; done | ||||||||||||||||||||||||||||
| 94 | $ pathed --check | ||||||||||||||||||||||||||||
| 95 | $ pathed -u --var PERL5LIB | ||||||||||||||||||||||||||||
| 96 | $ pathed -u $PERL5LIB | ||||||||||||||||||||||||||||
| 97 | $ pathed -d two --sep ';' '/foo/one;foo/two' | ||||||||||||||||||||||||||||
| 98 | $ pathed --man | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | The Bash C<PATH> environment variable contains a colon-separated list of paths. | ||||||||||||||||||||||||||||
| 103 | Platforms other than UNIX might use a different separator; C<pathed> uses the | ||||||||||||||||||||||||||||
| 104 | default separator for the current OS. C<pathed> - "path editor" - can split | ||||||||||||||||||||||||||||
| 105 | the path, append, prepend or remove elements, remove duplicates and reassemble | ||||||||||||||||||||||||||||
| 106 | it. | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | The result is then printed so you can assign it to the C<PATH> variable. If | ||||||||||||||||||||||||||||
| 109 | C<--split> is used, each path element is printed on a separate line, so you can | ||||||||||||||||||||||||||||
| 110 | iterate over them, for example. | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | The path elements can also be checked with C<--check> to make sure that the | ||||||||||||||||||||||||||||
| 113 | indicated paths are readable. | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | But C<pathed> isn't just for the C<PATH> variable. You can specify an | ||||||||||||||||||||||||||||
| 116 | environment variable to use with the C<--var> option, or just pass a value to | ||||||||||||||||||||||||||||
| 117 | be used directly after the options. | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | The following command-line options are supported: | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =over 4 | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =item C<--append>, C<-a> C<< <path> >> | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | Appends the given path to the list of path elements. This option can be | ||||||||||||||||||||||||||||
| 126 | specified several times; the paths are appended in the given order. | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =item C<--prepend>, C<-p> C<< <path> >> | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | Prepends the given path to the list of path elements. This option can be | ||||||||||||||||||||||||||||
| 131 | specified several times; the paths are prepended in the given order. For | ||||||||||||||||||||||||||||
| 132 | example: | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | $ pathed -p first -p second -p third | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | will result in C<third:second:first:$PATH>. | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =item C<--delete>, C<-d> C<< <substr> >> | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | Deletes those path elements which contain the given substring. This option can | ||||||||||||||||||||||||||||
| 141 | be specified several times; the path elements are deleted in the given order. | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | When options are mixed, C<--append> is processed first, then C<--prepend>, then | ||||||||||||||||||||||||||||
| 144 | C<--delete>. | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =item C<--unique>, C<-u> | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | Removes duplicate path elements. | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =item C<--split>, C<-s> | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | Prints each path element on its own line. If this option is not specified, the | ||||||||||||||||||||||||||||
| 153 | path elements are printed on one line, joined by the default path separator as | ||||||||||||||||||||||||||||
| 154 | reported by L<Config> - usually a colon -, like you would normally specify the | ||||||||||||||||||||||||||||
| 155 | C<PATH> variable. | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =item C<--check>, C<-c> | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | Checks whether each path element is readable and prints warnings if necessary. | ||||||||||||||||||||||||||||
| 160 | Does not check whether the path element is a directory because C<pathed> can | ||||||||||||||||||||||||||||
| 161 | also be used for specifying multiple files such as configuration files. | ||||||||||||||||||||||||||||
| 162 | Warnings are printed only once per path element, even if that element occurs | ||||||||||||||||||||||||||||
| 163 | several times in C<PATH>. | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | When C<--check> is used, the path is not printed. C<--check> and C<--split> are | ||||||||||||||||||||||||||||
| 166 | mutually exclusive. | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =item C<--var>, C<-v> C<< <variable> >> | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | Use the indicated environment variable. | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =item C<--sep>, C<-e> C<< <separator> >> | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | The default path separator is what L<Config> reports - usually a colon - but | ||||||||||||||||||||||||||||
| 175 | with this option you can specify a different separator. It is used to split the | ||||||||||||||||||||||||||||
| 176 | input path and to join the output path. | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =item C<--help>, C<-h> | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | Prints the synopsis. | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =item C<--man> | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | Prints the whole documentation. | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | =back | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =head1 WHY pathed? | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | The initial motivation for writing C<pathed> came when I tried to install | ||||||||||||||||||||||||||||
| 191 | C<vim> with C<homebrew> while C<rbenv> was active. C<vim> wanted to be compiled | ||||||||||||||||||||||||||||
| 192 | with the system ruby, so I was looking for a quick way to remove C<rbenv> from | ||||||||||||||||||||||||||||
| 193 | the C<PATH>: | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | $ PATH=$(pathed -d rbenv) brew install vim | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =head1 AUTHORS | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | The following person is the author of all the files provided in this | ||||||||||||||||||||||||||||
| 200 | distribution unless explicitly noted otherwise. | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | Marcel Gruenauer <marcel@cpan.org>, L<http://marcelgruenauer.com> | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | The following copyright notice applies to all the files provided in this | ||||||||||||||||||||||||||||
| 207 | distribution, including binary files, unless explicitly noted otherwise. | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | This software is copyright (c) 2013 by Marcel Gruenauer. | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | This is free software; you can redistribute it and/or modify it under the same | ||||||||||||||||||||||||||||
| 212 | terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||