| 1 | package Sparrow::Commands::Plugin; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | use base 'Exporter'; | ||
| 6 | |||
| 7 | use Sparrow::Constants; | ||
| 8 | use Sparrow::Misc; | ||
| 9 | |||
| 10 | use Carp; | ||
| 11 | use File::Basename; | ||
| 12 | use JSON; | ||
| 13 | |||
| 14 | use version; | ||
| 15 | |||
| 16 | use Getopt::Long qw(GetOptionsFromArray); | ||
| 17 | |||
| 18 | use Archive::Extract; | ||
| 19 | use File::Path qw(rmtree); | ||
| 20 | |||
| 21 | use Cwd; | ||
| 22 | |||
| 23 | use File::Copy::Recursive qw(dircopy); | ||
| 24 | |||
| 25 | use File::Find; | ||
| 26 | |||
| 27 | our @EXPORT = qw{ | ||
| 28 | |||
| 29 | search_plugins | ||
| 30 | |||
| 31 | show_installed_plugins | ||
| 32 | |||
| 33 | install_plugin | ||
| 34 | |||
| 35 | show_plugin | ||
| 36 | |||
| 37 | man_plugin | ||
| 38 | |||
| 39 | remove_plugin | ||
| 40 | |||
| 41 | run_plugin | ||
| 42 | |||
| 43 | upload_plugin | ||
| 44 | |||
| 45 | plugin_meta | ||
| 46 | |||
| 47 | }; | ||
| 48 | |||
| 49 | |||
| 50 | sub search_plugins { | ||
| 51 | |||
| 52 | my $pattern = shift || '.*'; | ||
| 53 | |||
| 54 | my $list = read_plugin_list(); | ||
| 55 | |||
| 56 | print "[found sparrow plugins]\n\n"; | ||
| 57 | print "type name\n\n"; | ||
| 58 | |||
| 59 | |||
| 60 | my $re = qr/$pattern/; | ||
| 61 | for my $p (grep { $_->{name}=~ $re } @{$list}){ | ||
| 62 | print "$p->{type}\t$p->{name}\n"; | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | |||
| 67 | sub show_installed_plugins { | ||
| 68 | |||
| 69 | print "[installed sparrow plugins]\n\n"; | ||
| 70 | |||
| 71 | print "[public]\n\n"; | ||
| 72 | |||
| 73 | my $root_dir = sparrow_root.'/plugins/public'; | ||
| 74 | |||
| 75 | opendir(my $dh, $root_dir) || confess "can't opendir $root_dir: $!"; | ||
| 76 | |||
| 77 | for my $p (grep { ! /^\.{1,2}$/ } readdir($dh)){ | ||
| 78 | print basename($p),"\n"; | ||
| 79 | } | ||
| 80 | |||
| 81 | closedir $dh; | ||
| 82 | |||
| 83 | print "\n\n[private]\n\n"; | ||
| 84 | |||
| 85 | my $root_dir = sparrow_root.'/plugins/private'; | ||
| 86 | |||
| 87 | opendir(my $dh, $root_dir) || confess "can't opendir $root_dir: $!"; | ||
| 88 | |||
| 89 | for my $p (grep { ! /^\.{1,2}$/ } readdir($dh)){ | ||
| 90 | print basename($p),"\n"; | ||
| 91 | } | ||
| 92 | |||
| 93 | closedir $dh; | ||
| 94 | |||
| 95 | } | ||
| 96 | |||
| 97 | sub install_plugin_deps { | ||
| 98 | |||
| 99 | my $plg_src = shift; | ||
| 100 | |||
| 101 | print "install deps for $plg_src ...\n"; | ||
| 102 | |||
| 103 | my $pip_command = 'pip'; | ||
| 104 | |||
| 105 | if ( -f "$plg_src/cpanfile" ){ | ||
| 106 | execute_shell_command("cd $plg_src && carton install"); | ||
| 107 | } | ||
| 108 | |||
| 109 | if ( -f "$plg_src/Gemfile" ){ | ||
| 110 | execute_shell_command("cd $plg_src && bundle --path local"); | ||
| 111 | } | ||
| 112 | |||
| 113 | if ( -f "$plg_src/requirements.txt"){ | ||
| 114 | open F, "$plg_src/sparrow.json" or confess "can't open file $plg_src/sparrow.json to read: $!"; | ||
| 115 | my $sp = join "", <F>; | ||
| 116 | my $spj = decode_json($sp); | ||
| 117 | close F; | ||
| 118 | if ( $spj->{python_version} && $spj->{python_version} eq '3' ) { | ||
| 119 | $pip_command = 'pip3' | ||
| 120 | } | ||
| 121 | execute_shell_command("cd $plg_src && $pip_command install -t ./python-lib -r requirements.txt --install-option \"--install-scripts=\$PWD/local/bin\""); | ||
| 122 | } | ||
| 123 | |||
| 124 | } | ||
| 125 | |||
| 126 | sub install_plugin_recursive { | ||
| 127 | |||
| 128 | my $path = shift or die "usage: install_plugin_recursive(\$path)"; | ||
| 129 | my $force = shift; | ||
| 130 | |||
| 131 | die "directory [$path] does not exit" unless -d $path; | ||
| 132 | |||
| 133 | find( | ||
| 134 | { | ||
| 135 | wanted => sub { | ||
| 136 | |||
| 137 | my $file = $_; | ||
| 138 | |||
| 139 | return unless $file eq 'sparrow.json'; | ||
| 140 | |||
| 141 | my @args = (".","--local"); | ||
| 142 | |||
| 143 | push @args, "--force" if $force; | ||
| 144 | |||
| 145 | install_plugin(@args); | ||
| 146 | |||
| 147 | } | ||
| 148 | }, $path | ||
| 149 | ); | ||
| 150 | |||
| 151 | |||
| 152 | } | ||
| 153 | |||
| 154 | sub install_plugin { | ||
| 155 | |||
| 156 | my $pid = shift or confess 'usage: install_plugin(name,opts)'; | ||
| 157 | |||
| 158 | my @args = @_; | ||
| 159 | |||
| 160 | my $version; | ||
| 161 | my $recursive; | ||
| 162 | my $local_install; | ||
| 163 | my $install_deps; | ||
| 164 | my $force; | ||
| 165 | |||
| 166 | my $args_st = GetOptionsFromArray( | ||
| 167 | \@args, | ||
| 168 | "local" => \$local_install, | ||
| 169 | "force" => \$force, | ||
| 170 | "install-deps" => \$install_deps, | ||
| 171 | "recursive" => \$recursive, | ||
| 172 | "version=s" => \$version, | ||
| 173 | ); | ||
| 174 | |||
| 175 | my $ptype; | ||
| 176 | |||
| 177 | if ($pid=~/(public|private)@/){ | ||
| 178 | $ptype = $1; | ||
| 179 | $pid=~s/(public|private)@//; | ||
| 180 | } | ||
| 181 | |||
| 182 | my $list = read_plugin_list('as_hash'); | ||
| 183 | |||
| 184 | if ($recursive and $local_install){ # install plugin from local source recursively | ||
| 185 | |||
| 186 | install_plugin_recursive($pid, $force); | ||
| 187 | |||
| 188 | } elsif ( $local_install ) { # install plugin from local source as a public plugin | ||
| 189 | |||
| 190 | my $dir = $pid eq '.' ? getcwd : $pid; | ||
| 191 | |||
| 192 | print "install from source: $dir ... \n"; | ||
| 193 | open F, "$dir/sparrow.json" or confess "can't open file $dir/sparrow.json to read: $!"; | ||
| 194 | my $sp = join "", <F>; | ||
| 195 | my $spj = decode_json($sp); | ||
| 196 | close F; | ||
| 197 | |||
| 198 | my $v = version->parse($spj->{version}); | ||
| 199 | $pid = $spj->{name}; | ||
| 200 | |||
| 201 | print "install public\@$pid version $v from local source\n"; | ||
| 202 | |||
| 203 | if (-f sparrow_root."/plugins/public/$pid/sparrow.json" ){ | ||
| 204 | |||
| 205 | open F, sparrow_root."/plugins/public/$pid/sparrow.json" or confess "can't open file to read: $!"; | ||
| 206 | my $sp = join "", <F>; | ||
| 207 | my $spj = decode_json($sp); | ||
| 208 | close F; | ||
| 209 | |||
| 210 | my $inst_v = version->parse($spj->{version}||'0.0.0'); | ||
| 211 | |||
| 212 | if ($inst_v >= $v and ! $force ){ | ||
| 213 | print "plugin is already istalled and has higher version: $inst_v\n"; | ||
| 214 | return; | ||
| 215 | } | ||
| 216 | |||
| 217 | } | ||
| 218 | |||
| 219 | if ( -d sparrow_root()."/plugins/public/$pid" ){ | ||
| 220 | rmtree(sparrow_root()."/plugins/public/$pid") or die "can't remove dir: ".sparrow_root()."/plugins/public/$pid, error: $!"; | ||
| 221 | } | ||
| 222 | |||
| 223 | mkdir(sparrow_root()."/plugins/public/$pid") or die "can't create dir: ".sparrow_root()."/plugins/public/$pid, error: $!"; | ||
| 224 | |||
| 225 | dircopy($dir,sparrow_root()."/plugins/public/$pid/"); | ||
| 226 | |||
| 227 | if ( -d sparrow_root()."/plugins/public/$pid/.git" ){ | ||
| 228 | rmtree(sparrow_root()."/plugins/public/$pid/.git") or die "can't remove dir: ".sparrow_root()."/plugins/public/$pid/.git, error: $!"; | ||
| 229 | } | ||
| 230 | |||
| 231 | install_plugin_deps(sparrow_root."/plugins/public/$pid"); | ||
| 232 | |||
| 233 | } elsif (! $ptype && $list->{'public@'.$pid} && $list->{'private@'.$pid} && ! $ptype ){ | ||
| 234 | |||
| 235 | warn "both public and private $pid plugin exists; choose `sparrow plg install public\@$pid` or `sparrow plg install private\@$pid` to overcome this ambiguity"; | ||
| 236 | |||
| 237 | return; | ||
| 238 | |||
| 239 | } elsif ( $list->{'public@'.$pid} and $ptype ne 'private' ) { | ||
| 240 | |||
| 241 | if (! $version and -f sparrow_root."/plugins/public/$pid/sparrow.json" ){ | ||
| 242 | |||
| 243 | open F, sparrow_root."/plugins/public/$pid/sparrow.json" or confess "can't open file to read: $!"; | ||
| 244 | my $sp = join "", <F>; | ||
| 245 | my $spj = decode_json($sp); | ||
| 246 | close F; | ||
| 247 | |||
| 248 | my $plg_v = version->parse($list->{'public@'.$pid}->{version}); | ||
| 249 | my $inst_v = version->parse($spj->{version}); | ||
| 250 | |||
| 251 | if ($plg_v > $inst_v){ | ||
| 252 | |||
| 253 | print "upgrading public\@$pid from version $inst_v to version $plg_v ...\n"; | ||
| 254 | |||
| 255 | if ( -d sparrow_root()."/plugins/public/$pid" ){ | ||
| 256 | rmtree(sparrow_root()."/plugins/public/$pid") or die "can't remove dir: ".sparrow_root()."/plugins/public/$pid, error: $!"; | ||
| 257 | } | ||
| 258 | |||
| 259 | mkdir(sparrow_root()."/plugins/public/$pid") or die "can't create dir: ".sparrow_root()."/plugins/public/$pid, error: $!"; | ||
| 260 | |||
| 261 | my $data = get_http_resource( sparrow_hub_api_url()."/plugins/$pid-v$plg_v.tar.gz", agent => 'sparrow' ); | ||
| 262 | my $plg_file = sparrow_root."/plugins/public/$pid/$pid-v$plg_v.tar.gz"; | ||
| 263 | |||
| 264 | open my $fh, ">:raw", $plg_file or die "can't open $plg_file to write"; | ||
| 265 | print $fh $data; | ||
| 266 | close $fh; | ||
| 267 | |||
| 268 | Archive::Extract->new( archive => $plg_file )->extract( to => sparrow_root()."/plugins/public/$pid" ) | ||
| 269 | or die "can't extract file $plg_file to ".sparrow_root()."/plugins/public/$pid, error: $!"; | ||
| 270 | |||
| 271 | install_plugin_deps(sparrow_root."/plugins/public/$pid"); | ||
| 272 | |||
| 273 | } else { | ||
| 274 | |||
| 275 | print "public\@$pid is uptodate ($inst_v)\n"; | ||
| 276 | |||
| 277 | install_plugin_deps(sparrow_root."/plugins/public/$pid") if $install_deps; | ||
| 278 | } | ||
| 279 | |||
| 280 | } else { | ||
| 281 | |||
| 282 | my $v = $version || $list->{'public@'.$pid}->{version}; | ||
| 283 | my $vn = version->parse($v)->numify; | ||
| 284 | |||
| 285 | print "installing public\@$pid version $v ...\n"; | ||
| 286 | |||
| 287 | if ( -d sparrow_root()."/plugins/public/$pid" ){ | ||
| 288 | rmtree(sparrow_root()."/plugins/public/$pid") or die "can't remove dir: ".sparrow_root()."/plugins/public/$pid, error: $!"; | ||
| 289 | } | ||
| 290 | |||
| 291 | mkdir(sparrow_root()."/plugins/public/$pid") or die "can't create dir: ".sparrow_root()."/plugins/public/$pid, error: $!"; | ||
| 292 | |||
| 293 | my $data = get_http_resource( sparrow_hub_api_url()."/plugins/$pid-v$vn.tar.gz", agent => 'sparrow' ); | ||
| 294 | my $plg_file = sparrow_root."/plugins/public/$pid/$pid-v$vn.tar.gz"; | ||
| 295 | |||
| 296 | open my $fh, ">:raw", $plg_file or die "can't open $plg_file to write"; | ||
| 297 | print $fh $data; | ||
| 298 | close $fh; | ||
| 299 | |||
| 300 | Archive::Extract->new( archive => $plg_file )->extract( to => sparrow_root()."/plugins/public/$pid" ) | ||
| 301 | or die "can't extract file $plg_file to ".sparrow_root()."/plugins/public/$pid, error: $!"; | ||
| 302 | |||
| 303 | install_plugin_deps(sparrow_root."/plugins/public/$pid"); | ||
| 304 | |||
| 305 | } | ||
| 306 | |||
| 307 | } elsif ($list->{'private@'.$pid} and $ptype ne 'public' ) { | ||
| 308 | |||
| 309 | if ( -d sparrow_root()."/plugins/private/$pid/" ) { | ||
| 310 | |||
| 311 | print "installing private\@$pid ...\n"; | ||
| 312 | |||
| 313 | execute_shell_command("cd ".sparrow_root."/plugins/private/$pid && git pull"); | ||
| 314 | |||
| 315 | execute_shell_command("cd ".sparrow_root."/plugins/private/$pid && git config credential.helper 'cache --timeout=3000000'"); | ||
| 316 | |||
| 317 | install_plugin_deps(sparrow_root."/plugins/private/$pid"); | ||
| 318 | |||
| 319 | } else { | ||
| 320 | |||
| 321 | execute_shell_command("git clone ".($list->{'private@'.$pid}->{url}).' '.sparrow_root."/plugins/private/$pid"); | ||
| 322 | |||
| 323 | execute_shell_command("cd ".sparrow_root."/plugins/private/$pid && git config credential.helper 'cache --timeout=3000000'"); | ||
| 324 | |||
| 325 | install_plugin_deps(sparrow_root."/plugins/private/$pid"); | ||
| 326 | } | ||
| 327 | } elsif ( -d sparrow_root()."/plugins/public/$pid/" ) { | ||
| 328 | |||
| 329 | print "plugin ".sparrow_root()."/plugins/public/$pid/ installed locally, nothing to do here ...\n"; | ||
| 330 | |||
| 331 | } else { | ||
| 332 | |||
| 333 | confess "unknown plugin"; | ||
| 334 | |||
| 335 | } | ||
| 336 | |||
| 337 | } | ||
| 338 | |||
| 339 | sub run_plugin { | ||
| 340 | |||
| 341 | my $pid = shift or confess "usage: run_plugin(*plugin_name,parameters)"; | ||
| 342 | |||
| 343 | |||
| 344 | my @args = @_; | ||
| 345 | |||
| 346 | my $verbose_mode = 0; | ||
| 347 | |||
| 348 | my $dump_config_arg; | ||
| 349 | my $format_arg = $ENV{OUTTHENTIC_FORMAT} || sparrow_config->{'format'} || 'default'; | ||
| 350 | my $debug_arg; | ||
| 351 | my $purge_cache_arg; | ||
| 352 | my $match_l_arg; | ||
| 353 | my $cwd_arg; | ||
| 354 | my $story_arg; | ||
| 355 | my $ini_arg; | ||
| 356 | my $yaml_arg; | ||
| 357 | my $json_arg; | ||
| 358 | my $nocolor_arg; | ||
| 359 | my $args_file_arg; | ||
| 360 | |||
| 361 | my @runtime_params; | ||
| 362 | |||
| 363 | my $cli_args; | ||
| 364 | |||
| 365 | my $i=0; | ||
| 366 | for my $a (@args) { | ||
| 367 | if ($a eq '--') { | ||
| 368 | delete $args[$i]; | ||
| 369 | $cli_args = join ' ', delete @args[$i .. $#args]; | ||
| 370 | last; | ||
| 371 | } | ||
| 372 | $i++; | ||
| 373 | } | ||
| 374 | |||
| 375 | |||
| 376 | my $args_st = GetOptionsFromArray( | ||
| 377 | \@args, | ||
| 378 | "verbose" => \$verbose_mode, | ||
| 379 | "param=s" => \@runtime_params, | ||
| 380 | "dump-config" => \$dump_config_arg, | ||
| 381 | "args-file=s" => \$args_file_arg, | ||
| 382 | "purge-cache" => \$purge_cache_arg, | ||
| 383 | "format=s" => \$format_arg, | ||
| 384 | "debug=i" => \$debug_arg, | ||
| 385 | "match_l=i" => \$match_l_arg, | ||
| 386 | "cwd=s" => \$cwd_arg, | ||
| 387 | "story=s" => \$story_arg, | ||
| 388 | "ini=s" => \$ini_arg, | ||
| 389 | "yaml=s" => \$yaml_arg, | ||
| 390 | "json=s" => \$json_arg, | ||
| 391 | "nocolor" => \$nocolor_arg, | ||
| 392 | ); | ||
| 393 | |||
| 394 | my $ptype; | ||
| 395 | |||
| 396 | my $pdir; | ||
| 397 | |||
| 398 | if ($pid=~/(public|private)@/){ | ||
| 399 | $ptype = $1; | ||
| 400 | $pid=~s/(public|private)@//; | ||
| 401 | } | ||
| 402 | |||
| 403 | my $list = read_plugin_list('as_hash'); | ||
| 404 | |||
| 405 | if (! $ptype && $list->{'public@'.$pid} && $list->{'private@'.$pid} && ! $ptype){ | ||
| 406 | warn "both public and private $pid plugin exists; choose `sparrow plg install public\@$pid` or `sparrow plg install private\@$pid` to overcome this ambiguity"; | ||
| 407 | return; | ||
| 408 | } elsif($list->{'public@'.$pid} and $ptype ne 'private' ) { | ||
| 409 | $pdir = sparrow_root."/plugins/public/$pid"; | ||
| 410 | confess 'plugin not installed' unless -d $pdir; | ||
| 411 | } elsif ($list->{'private@'.$pid} and $ptype ne 'public' ) { | ||
| 412 | $pdir = sparrow_root."/plugins/private/$pid"; | ||
| 413 | confess 'plugin not installed' unless -d $pdir; | ||
| 414 | } elsif ( -d sparrow_root."/plugins/public/$pid" ) { | ||
| 415 | warn "plugin is not listed in the index, locally installed one?"; | ||
| 416 | $pdir = sparrow_root."/plugins/public/$pid"; | ||
| 417 | } else{ | ||
| 418 | confess "unknown plugin"; | ||
| 419 | } | ||
| 420 | |||
| 421 | |||
| 422 | my $spj = plugin_meta($pdir); | ||
| 423 | |||
| 424 | if ($spj->{sparrow_version}){ | ||
| 425 | # check sparrow version if it's defined at sparrow.json | ||
| 426 | |||
| 427 | my $curr_sp_v = version->parse($Sparrow::VERSION); | ||
| 428 | my $req_sp_v = version->parse($spj->{sparrow_version}); | ||
| 429 | |||
| 430 | if ($req_sp_v > $curr_sp_v){ | ||
| 431 | die "plugin require sparrow version: $req_sp_v, but you have: $curr_sp_v"; | ||
| 432 | }; | ||
| 433 | |||
| 434 | } | ||
| 435 | |||
| 436 | my $cmd; | ||
| 437 | |||
| 438 | if ($^O =~ 'MSWin') { | ||
| 439 | $cmd = "cd $pdir && set PATH=%PATH%;%cd%/local/bin && set PERL5LIB=%cd%/local/lib/perl5;\%PERL5LIB% && set PYTHONPATH=%cd%/python-lib;%PYTHONPATH% && "; | ||
| 440 | } else { | ||
| 441 | $cmd = "cd $pdir && export PATH=\$PATH:\$PWD/local/bin && export PERL5LIB=\$PWD/local/lib/perl5:\$PERL5LIB && export PYTHONPATH=\$PWD/python-lib:\$PYTHONPATH && "; | ||
| 442 | } | ||
| 443 | |||
| 444 | if ($spj->{plugin_type} eq 'outthentic'){ | ||
| 445 | |||
| 446 | if ($^O =~ 'MSWin') { | ||
| 447 | $cmd.=" strun --root ./ --task \"[plg] $pid\""; | ||
| 448 | } else { | ||
| 449 | $cmd.=" strun --root ./ --task '[plg] $pid'"; | ||
| 450 | } | ||
| 451 | |||
| 452 | }elsif ( $spj->{plugin_type} eq 'swat' ) { | ||
| 453 | $cmd.=" swat ./ "; | ||
| 454 | }else{ | ||
| 455 | confess "unsupported plugin type: $spj->{plugin_type}" | ||
| 456 | } | ||
| 457 | |||
| 458 | for my $rp (@runtime_params){ | ||
| 459 | $rp=~/(\S+?)=(.*)/; | ||
| 460 | #warn $1; warn $2; | ||
| 461 | my $k = $1; my $v = $2; | ||
| 462 | if ($^O =~ 'MSWin') { | ||
| 463 | $cmd.= " --param $k=$v"; | ||
| 464 | } else { | ||
| 465 | $cmd.= " --param $k='$v'"; | ||
| 466 | } | ||
| 467 | } | ||
| 468 | |||
| 469 | |||
| 470 | $cmd.= " --nocolor" if $nocolor_arg; | ||
| 471 | $cmd.= " --dump-config" if $dump_config_arg; | ||
| 472 | $cmd.= " --purge-cache" if $purge_cache_arg; | ||
| 473 | |||
| 474 | $cmd.= " --format $format_arg" if $format_arg; | ||
| 475 | $cmd.= " --debug $debug_arg" if $debug_arg; | ||
| 476 | $cmd.= " --match_l $match_l_arg" if $match_l_arg; | ||
| 477 | |||
| 478 | $cmd.= " --ini $ini_arg" if $ini_arg; | ||
| 479 | $cmd.= " --yaml $yaml_arg" if $yaml_arg; | ||
| 480 | $cmd.= " --json $json_arg" if $json_arg; | ||
| 481 | |||
| 482 | $cmd.= " --cwd $cwd_arg" if $cwd_arg; | ||
| 483 | $cmd.= " --story $story_arg" if $story_arg; | ||
| 484 | $cmd.= " --args-file $args_file_arg" if $args_file_arg; | ||
| 485 | |||
| 486 | $cmd.= " -- $cli_args" if $cli_args; | ||
| 487 | |||
| 488 | if ($verbose_mode){ | ||
| 489 | print map {"# $_\n"} split /&&\s+/, $cmd; | ||
| 490 | print "\n"; | ||
| 491 | } | ||
| 492 | |||
| 493 | if ($^O =~ 'MSWin') { | ||
| 494 | system($cmd) == 0 or "die $!"; | ||
| 495 | } else { | ||
| 496 | exec $cmd; | ||
| 497 | } | ||
| 498 | |||
| 499 | } | ||
| 500 | |||
| 501 | sub show_plugin { | ||
| 502 | |||
| 503 | my $pid = shift or confess 'usage: show_plugin(plugin_name)'; | ||
| 504 | |||
| 505 | my $list = read_plugin_list('as_hash'); | ||
| 506 | |||
| 507 | my $listed = ( $list->{'public@'.$pid} or $list->{'private@'.$pid} ) ? 1 : 0; | ||
| 508 | |||
| 509 | if ($listed and $list->{'public@'.$pid} ) { | ||
| 510 | |||
| 511 | my $inst_version = ''; | ||
| 512 | my $desc = ''; | ||
| 513 | |||
| 514 | if ( open F, sparrow_root."/plugins/public/$pid/sparrow.json" ){ | ||
| 515 | my $s = join "", <F>; | ||
| 516 | close F; | ||
| 517 | my $spj = decode_json($s); | ||
| 518 | $inst_version = eval { version->parse($spj->{version})->numify }; | ||
| 519 | $desc = $spj->{description}; | ||
| 520 | } else { | ||
| 521 | $inst_version = 'unknown'; | ||
| 522 | $desc = 'unknown'; | ||
| 523 | } | ||
| 524 | |||
| 525 | print "\n"; | ||
| 526 | print "name: $pid\n"; | ||
| 527 | print "type: public\n"; | ||
| 528 | print "installed: ",( -f sparrow_root."/plugins/public/$pid/sparrow.json" ? 'YES':'NO'),"\n"; | ||
| 529 | print "version: ",$list->{'public@'.$pid}->{version},"\n"; | ||
| 530 | print "installed version: ",$inst_version,"\n" if -f sparrow_root."/plugins/public/$pid/sparrow.json"; | ||
| 531 | print "description: $desc\n"; | ||
| 532 | |||
| 533 | } | ||
| 534 | |||
| 535 | if( $listed and $list->{'private@'.$pid} ) { | ||
| 536 | print "\n"; | ||
| 537 | print "name: $pid\n"; | ||
| 538 | print "type: private\n"; | ||
| 539 | print "installed: ",( -d sparrow_root."/plugins/private/$pid/" ? 'YES':'NO'),"\n"; | ||
| 540 | } | ||
| 541 | |||
| 542 | if (! $listed ) { | ||
| 543 | if ( -f sparrow_root."/plugins/public/$pid/sparrow.json" ){ | ||
| 544 | print "public\@$pid plugin installed, but not found at sparrow index. is it locally installed plugin?\n"; | ||
| 545 | } | ||
| 546 | if ( -d sparrow_root."/plugins/private/$pid" ){ | ||
| 547 | print "private\@$pid plugin installed, but not found at sparrow index. is it obsolete plugin?\n"; | ||
| 548 | } | ||
| 549 | warn "unknown plugin" unless ( | ||
| 550 | -f sparrow_root."/plugins/public/$pid/sparrow.json" or | ||
| 551 | -d sparrow_root."/plugins/private/$pid" | ||
| 552 | ); | ||
| 553 | } | ||
| 554 | |||
| 555 | } | ||
| 556 | |||
| 557 | sub man_plugin { | ||
| 558 | |||
| 559 | |||
| 560 | my $pid = shift or confess('usage: man_plugin(*plugin_name)'); | ||
| 561 | |||
| 562 | my $ptype; | ||
| 563 | |||
| 564 | if ($pid=~/(public|private)@/){ | ||
| 565 | $ptype = $1; | ||
| 566 | $pid=~s/(public|private)@//; | ||
| 567 | } | ||
| 568 | |||
| 569 | if (-d sparrow_root."/plugins/public/$pid" and $ptype ne 'private' ){ | ||
| 570 | my $pdir = sparrow_root."/plugins/public/$pid"; | ||
| 571 | my $spj = plugin_meta($pdir); | ||
| 572 | my $readme_file = $spj->{doc} || 'README.md'; | ||
| 573 | exec("cat ".sparrow_root."/plugins/public/$pid/$readme_file"); | ||
| 574 | } elsif (-d sparrow_root."/plugins/private/$pid" and $ptype ne 'public' ){ | ||
| 575 | my $pdir = sparrow_root."/plugins/private/$pid"; | ||
| 576 | my $spj = plugin_meta($pdir); | ||
| 577 | my $readme_file = $spj->{doc} || 'README.md'; | ||
| 578 | exec("cat ".sparrow_root."/plugins/private/$pid/$readme_file"); | ||
| 579 | } else { | ||
| 580 | warn "plugin not found"; | ||
| 581 | } | ||
| 582 | |||
| 583 | } | ||
| 584 | |||
| 585 | sub remove_plugin { | ||
| 586 | |||
| 587 | my $pid = shift or confess('usage: remove_plugin(*plugin_name)'); | ||
| 588 | my $rm_cnt = 0; | ||
| 589 | |||
| 590 | my $ptype; | ||
| 591 | |||
| 592 | if ($pid=~/(public|private)@/){ | ||
| 593 | $ptype = $1; | ||
| 594 | $pid=~s/(public|private)@//; | ||
| 595 | } | ||
| 596 | |||
| 597 | if (-d sparrow_root."/plugins/public/$pid" and $ptype ne 'private' ){ | ||
| 598 | print "removing public\@$pid ...\n"; | ||
| 599 | rmtree(sparrow_root()."/plugins/public/$pid") or die "can't remove dir: ".sparrow_root()."/plugins/public/$pid, error: $!"; | ||
| 600 | $rm_cnt++; | ||
| 601 | } | ||
| 602 | |||
| 603 | if (-d sparrow_root."/plugins/private/$pid" and $ptype ne 'public' ){ | ||
| 604 | print "removing private\@$pid ...\n"; | ||
| 605 | rmtree(sparrow_root()."/plugins/private/$pid") or die "can't remove dir: ".sparrow_root()."/plugins/private/$pid, error: $!"; | ||
| 606 | $rm_cnt++; | ||
| 607 | } | ||
| 608 | |||
| 609 | warn "plugin is not installed" unless $rm_cnt; | ||
| 610 | |||
| 611 | } | ||
| 612 | |||
| 613 | sub read_plugin_list { | ||
| 614 | |||
| 615 | my @list; | ||
| 616 | my %list; | ||
| 617 | |||
| 618 | my $mode = shift || 'as_array'; | ||
| 619 | |||
| 620 | # read public plugins list first | ||
| 621 | open F, spi_file() or confess "can't open ".spi_file()." to read - $!"; | ||
| 622 | |||
| 623 | while ( my $i = <F> ){ | ||
| 624 | chomp $i; | ||
| 625 | next unless $i=~/\S+/; | ||
| 626 | my @foo = split /\s+/, $i; | ||
| 627 | push @list, { name => $foo[0], version => $foo[1], type => 'public' } ; | ||
| 628 | $list{'public@'.$foo[0]} = { name => $foo[0], version => $foo[1], type => 'public' }; | ||
| 629 | } | ||
| 630 | |||
| 631 | close F; | ||
| 632 | |||
| 633 | # read custome plugins list then | ||
| 634 | |||
| 635 | open F, spci_file() or confess "can't open ".spci_file()." to read"; | ||
| 636 | |||
| 637 | while ( my $i = <F> ){ | ||
| 638 | chomp $i; | ||
| 639 | next unless $i=~/\S+/; | ||
| 640 | next if $i=~/^\s*#/; | ||
| 641 | my @foo = split /\s+/, $i; | ||
| 642 | push @list, { name => $foo[0], url => $foo[1], type => 'private' } ; | ||
| 643 | $list{'private@'.$foo[0]} = { name => $foo[0], url => $foo[1], type => 'private' }; | ||
| 644 | } | ||
| 645 | |||
| 646 | close F; | ||
| 647 | |||
| 648 | # read private plugins list at the end | ||
| 649 | |||
| 650 | open F, spl_file() or confess "can't open ".spl_file()." to read"; | ||
| 651 | |||
| 652 | while ( my $i = <F> ){ | ||
| 653 | chomp $i; | ||
| 654 | next unless $i=~/\S+/; | ||
| 655 | next if $i=~/^\s*#/; | ||
| 656 | my @foo = split /\s+/, $i; | ||
| 657 | push @list, { name => $foo[0], url => $foo[1], type => 'private' } ; | ||
| 658 | $list{'private@'.$foo[0]} = { name => $foo[0], url => $foo[1], type => 'private' }; | ||
| 659 | } | ||
| 660 | |||
| 661 | close F; | ||
| 662 | |||
| 663 | my $retval; | ||
| 664 | |||
| 665 | if ($mode eq 'as_hash'){ | ||
| 666 | $retval = \%list; | ||
| 667 | }else{ | ||
| 668 | $retval = \@list; | ||
| 669 | } | ||
| 670 | |||
| 671 | return $retval; | ||
| 672 | |||
| 673 | } | ||
| 674 | |||
| 675 | sub upload_plugin { | ||
| 676 | |||
| 677 | my @args = @_; | ||
| 678 | |||
| 679 | my $verbose_mode; | ||
| 680 | |||
| 681 | my $args_st = GetOptionsFromArray( | ||
| 682 | \@args, | ||
| 683 | "verbose" => \$verbose_mode, | ||
| 684 | ); | ||
| 685 | |||
| 686 | # get user/token by environment variables | ||
| 687 | # usefull when making tests | ||
| 688 | |||
| 689 | my $cred; | ||
| 690 | |||
| 691 | if ($ENV{sph_user} and $ENV{sph_token}){ | ||
| 692 | $cred->{user} = $ENV{sph_user}; | ||
| 693 | $cred->{token} = $ENV{sph_token}; | ||
| 694 | } | ||
| 695 | |||
| 696 | # or read from $ENV{HOME}/sparrowhub.json | ||
| 697 | else{ | ||
| 698 | open F, "$ENV{HOME}/sparrowhub.json" or confess "can't open $ENV{HOME}/sparrowhub.json to read: $!"; | ||
| 699 | my $s = join "", <F>; | ||
| 700 | close F; | ||
| 701 | $cred = decode_json($s); | ||
| 702 | } | ||
| 703 | |||
| 704 | |||
| 705 | open F, 'sparrow.json' or confess "can't open sparrow.json to read: $!"; | ||
| 706 | my $s = join "", <F>; | ||
| 707 | close F; | ||
| 708 | |||
| 709 | my $spj = decode_json($s); | ||
| 710 | |||
| 711 | # validating json file | ||
| 712 | |||
| 713 | my $plg_v = version->parse($spj->{version}) or confess "version not found in sparrow.json file";; | ||
| 714 | my $plg_name = $spj->{name} or confess "name not found in sparrow.json file"; | ||
| 715 | |||
| 716 | $plg_name=~/^[\w\d-\._]+$/ or confess 'name parameter does not meet naming requirements - /^[\w\d-\._]+$/'; | ||
| 717 | |||
| 718 | print "sparrow.json file validated ... \n"; | ||
| 719 | |||
| 720 | execute_shell_command('tar --exclude=.tom --exclude=local --exclude=*.log --exclude=log --exclude Gemfile.lock --exclude local/ --exclude-vcs -zcf /tmp/archive.tar.gz .', debug => $verbose_mode ); | ||
| 721 | |||
| 722 | my $unsecure_flag = $ENV{SPARROW_UNSECURE} ? "-k" : ""; | ||
| 723 | execute_shell_command( | ||
| 724 | "curl -s -k $unsecure_flag -H 'sparrow-user: $cred->{user}' " . | ||
| 725 | "-H 'sparrow-token: $cred->{token}' " . | ||
| 726 | '-f -X POST '.sparrow_hub_api_url().'/api/v1/upload -F archive=@/tmp/archive.tar.gz', | ||
| 727 | silent => ($verbose_mode ? 0 : 1), | ||
| 728 | debug => ($verbose_mode ? 1 : 0) | ||
| 729 | ); | ||
| 730 | |||
| 731 | } | ||
| 732 | |||
| 733 | sub plugin_meta { | ||
| 734 | |||
| 735 | my $path = shift or confess('usage: plugin_meta(path)'); | ||
| 736 | |||
| 737 | open F, "$path/sparrow.json" or confess "can't open sparrow.json to read: $!"; | ||
| 738 | my $s = join "", <F>; | ||
| 739 | close F; | ||
| 740 | |||
| 741 | my $spj = decode_json($s); | ||
| 742 | |||
| 743 | $spj->{plugin_type} ||= 'outthentic'; | ||
| 744 | |||
| 745 | return $spj; | ||
| 746 | |||
| 747 | } | ||
| 748 | |||
| 749 | 1; | ||
| 750 |