From: Matt S Trout Date: Fri, 11 Nov 2011 05:46:24 +0000 (+0000) Subject: Initial script infrastructure X-Git-Tag: v0.001001~22 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=e367483dafe72876d1d3bc5ae4b3e61db44b2640;p=scpubgit%2FTak.git Initial script infrastructure --- diff --git a/bin/tak b/bin/tak new file mode 100644 index 0000000..00898c3 --- /dev/null +++ b/bin/tak @@ -0,0 +1,6 @@ +#!/usr/bin/env perl + +use strictures 1; +use App::Tak; + +App::Tak->new_from_environment->run; diff --git a/lib/Tak/MyScript.pm b/lib/Tak/MyScript.pm new file mode 100644 index 0000000..149e910 --- /dev/null +++ b/lib/Tak/MyScript.pm @@ -0,0 +1,13 @@ +package Tak::MyScript; + +use Moo; + +extends 'Tak::Script'; + +sub _my_script_package { 'Tak::MyScript' } + +sub _load_file_in_my_script { + require $_[1]; +} + +1; diff --git a/lib/Tak/Script.pm b/lib/Tak/Script.pm new file mode 100644 index 0000000..6868fe4 --- /dev/null +++ b/lib/Tak/Script.pm @@ -0,0 +1,76 @@ +package Tak::Script; + +use Getopt::Long qw(GetOptionsFromArray :config posix_defaults bundling); +use Config::Settings; +use IO::Handle; +use Moo; + +has options => (is => 'ro', required => 1); +has env => (is => 'ro', required => 1); + +has stdin => (is => 'lazy'); +has stdout => (is => 'lazy'); +has stderr => (is => 'lazy'); + +sub _build_stdin { shift->env->{stdin} } +sub _build_stdout { shift->env->{stdout} } +sub _build_stderr { shift->env->{stderr} } + +has config => (is => 'lazy'); + +sub _build_config { + my ($self) = @_; + Config::Settings->new->parse_file( + $self->options->{config} || '.tak/default.conf' + ); +} + +sub BUILD { + shift->setup_logger; +} + +sub setup_logger { } + +sub _parse_options { + my ($self, $string, \@argv) = @_; + my @spec = split ';', $string; + +sub run { + my ($self) = @_; + my @argv = @{$self->env->{argv}}; + unless (@argv && $argv[0]) { + return $self->local_help; + } + my $cmd = shift(@argv); + if (my $code = $self->can("local_$cmd")) { + return $self->_run($cmd, $code, @argv); + } elsif ($code = $self->can("each_$cmd")) { + return $self->_run_each($cmd, $code, @argv); + } + $self->stderr->print("No such command: ${cmd}\n"); + return $self->local_help; +} + +sub _load_file { + my ($self, $file) = @_; + $self->_load_file_in_my_script($file); +} + +sub local_help { + my ($self) = @_; + $self->stderr->print("Help unimplemented\n"); +} + +sub _run_local { + my ($self, $cmd, $code, @argv) = @_; + my $opt = do { + if (my $proto = prototype($code)) { + $self->_parse_options($proto, \@argv); + } else { + {}; + } + }; + $self->$code($opt, @argv); +} + +1;