| 1 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | package Acme::CatFS; | ||
| 4 | |||
| 5 | # ABSTRACT: Fuse filesystem with a random pic of a cat | ||
| 6 | |||
| 7 | 2 | ▶ | use feature qw(say state); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 2 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 2 | ▶ | use Try::Tiny; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 2 | ▶ | use LWP::Simple; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 0 | ▶ | use Fuse::Simple; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 12 | |||
| 13 | use Moo; | ||
| 14 | use MooX::Options; | ||
| 15 | use Types::Path::Tiny qw(Dir); | ||
| 16 | |||
| 17 | option mountpoint => ( | ||
| 18 | is => 'ro', | ||
| 19 | isa => Dir, | ||
| 20 | required => 1, | ||
| 21 | format => 's', | ||
| 22 | coerce => Dir->coercion, | ||
| 23 | doc => 'mount point for catfs (should be a directory). Required.', | ||
| 24 | ); | ||
| 25 | |||
| 26 | option cat_url => ( | ||
| 27 | is => 'ro', | ||
| 28 | format => 's', | ||
| 29 | default => sub { | ||
| 30 | 'http://thecatapi.com/api/images/get?format=src&type=jpg' | ||
| 31 | }, | ||
| 32 | doc => 'url used to find a random pic of a cat (default thecatapi.com)', | ||
| 33 | ); | ||
| 34 | |||
| 35 | option cat_file => ( | ||
| 36 | is => 'ro', | ||
| 37 | format => 's', | ||
| 38 | default => sub { 'cat.jpg' }, | ||
| 39 | doc => 'name of the file (default is cat.jpg)', | ||
| 40 | ); | ||
| 41 | |||
| 42 | option forking => ( | ||
| 43 | is => 'ro', | ||
| 44 | doc => 'if enable, will fork and exit (default false)', | ||
| 45 | ); | ||
| 46 | |||
| 47 | option debug => ( | ||
| 48 | is => 'ro', | ||
| 49 | doc => 'if enable, will run Fuse::Simple in debug mode (default false)', | ||
| 50 | ); | ||
| 51 | |||
| 52 | option cached => ( | ||
| 53 | is => 'ro', | ||
| 54 | doc => 'if enable, will cached the picture instead choose another each open (default false)', | ||
| 55 | ); | ||
| 56 | |||
| 57 | sub _get_cat_picture { | ||
| 58 | my $self = shift; | ||
| 59 | state $cached_content; | ||
| 60 | |||
| 61 | if($self->cached && $cached_content){ | ||
| 62 | return $cached_content; | ||
| 63 | } | ||
| 64 | |||
| 65 | my $content = try { | ||
| 66 | LWP::Simple::get($self->cat_url) | ||
| 67 | } catch { | ||
| 68 | carp $_ if $self->debug; | ||
| 69 | }; | ||
| 70 | |||
| 71 | if($self->cached){ | ||
| 72 | $cached_content = $content | ||
| 73 | } | ||
| 74 | |||
| 75 | $content | ||
| 76 | } | ||
| 77 | |||
| 78 | sub run { | ||
| 79 | my ($self) = @_; | ||
| 80 | |||
| 81 | if($self->forking){ | ||
| 82 | fork and exit | ||
| 83 | } | ||
| 84 | |||
| 85 | my $mountpoint = $self->mountpoint; | ||
| 86 | my $cat_file = $self->cat_file; | ||
| 87 | |||
| 88 | say "Initializing Fuse mountpoint '$mountpoint'... "; | ||
| 89 | |||
| 90 | Fuse::Simple::main( | ||
| 91 | mountpoint => $mountpoint, | ||
| 92 | debug => $self->debug, | ||
| 93 | '/' => { | ||
| 94 | $cat_file => sub { | ||
| 95 | $self->_get_cat_picture | ||
| 96 | }, | ||
| 97 | }, | ||
| 98 | ); | ||
| 99 | } | ||
| 100 | |||
| 101 | END { | ||
| 102 | say "Don't forget run 'fusermount -u <mountpoint>'" | ||
| 103 | } | ||
| 104 | |||
| 105 | =head1 NAME | ||
| 106 | |||
| 107 | Acme::CatFS | ||
| 108 | |||
| 109 | =head1 SYNOPSIS | ||
| 110 | |||
| 111 | Acme::CatFS->new(mountpoint => '/tmp/catfs', debug => 0, cat_file => 'kitten.jpg')->run(); | ||
| 112 | |||
| 113 | =head1 DESCRIPTION | ||
| 114 | |||
| 115 | Acme::CatFS will create a Fuse mountpoint and generate one virtual file, a random image of a cat. Will return a different image each time. | ||
| 116 | |||
| 117 | It is the equivalent to: | ||
| 118 | |||
| 119 | Fuse::Simple::main( | ||
| 120 | mountpoint => $mountpoint, | ||
| 121 | "/" => { | ||
| 122 | 'cat.jpg' => sub { | ||
| 123 | LWP::Simple::get('http://thecatapi.com/api/images/get?format=src&type=jpg'); | ||
| 124 | }, | ||
| 125 | }, | ||
| 126 | ); | ||
| 127 | |||
| 128 | =head1 METHODS | ||
| 129 | |||
| 130 | =head2 run | ||
| 131 | |||
| 132 | Will initialize the Fuse mountpoint. | ||
| 133 | |||
| 134 | =head1 SCRIPT | ||
| 135 | |||
| 136 | You can call acme-catfs helper script in you command line to easily create the mountpoint. Try C<acme-catfs -h> to see the options. | ||
| 137 | |||
| 138 | |||
| 139 | =head1 ATTRIBUTES | ||
| 140 | |||
| 141 | =head2 mountpoint | ||
| 142 | |||
| 143 | Specify the directory mountpoint for Fuse. Should be an empty directory. | ||
| 144 | |||
| 145 | =head2 cat_url | ||
| 146 | |||
| 147 | Specify the url for the random pic of cat. Default is 'thecatapi.com' service. | ||
| 148 | |||
| 149 | =head2 cat_file | ||
| 150 | |||
| 151 | Specify the name of the file. Default is 'cat.jpg' | ||
| 152 | |||
| 153 | =head2 debug | ||
| 154 | |||
| 155 | If true, will run Fuse::Simple::main in debug mode. | ||
| 156 | |||
| 157 | =head2 forking | ||
| 158 | |||
| 159 | If true, we will fork then exit. | ||
| 160 | |||
| 161 | =head2 cached | ||
| 162 | |||
| 163 | if true, we will cache the cat picture instead download a new one. | ||
| 164 | |||
| 165 | =head1 SEE ALSO | ||
| 166 | |||
| 167 | L<Fuse::Simple> and L<Fuse> | ||
| 168 | |||
| 169 | =head1 ACKNOWLEDGE | ||
| 170 | |||
| 171 | Thanks to Julia Evans (twitter => @b0rk, blog => L<http://jvns.ca/>) with the original idea and support on twitter. And Nick Waterman (pause => NOSEYNICK) by L<Fuse::Simple> module - it is awesome! | ||
| 172 | |||
| 173 | =head1 AUTHOR | ||
| 174 | |||
| 175 | Tiago Peczenyj, E<lt>tiago.peczenyj@gmail.comE<gt> | ||
| 176 | |||
| 177 | =head1 COPYRIGHT AND LICENSE | ||
| 178 | |||
| 179 | Copyright (C) 2014 by Tiago Peczenyj | ||
| 180 | |||
| 181 | This library is free software; you can redistribute it and/or modify | ||
| 182 | it under the same terms as Perl itself, either Perl version 5.10.0 or, | ||
| 183 | at your option, any later version of Perl 5 you may have available. | ||
| 184 | |||
| 185 | =cut | ||
| 186 | |||
| 187 | 1; | ||