| 1 | package App::Ordo::Runner; | ||
| 2 | use Moo; | ||
| 3 | use feature qw(say); | ||
| 4 | use utf8; | ||
| 5 | use open ':std', ':utf8'; | ||
| 6 | use Term::ANSIColor qw(colored); | ||
| 7 | |||
| 8 | use App::Ordo qw($CURRENT_PATH extract_command); | ||
| 9 | |||
| 10 | # Pre-load all command classes to avoid "Can't locate object method 'new'" | ||
| 11 | use App::Ordo::Command::Ls; | ||
| 12 | use App::Ordo::Command::Cd; | ||
| 13 | use App::Ordo::Command::Help; | ||
| 14 | use App::Ordo::Command::Sync; | ||
| 15 | use App::Ordo::Command::User::Show; | ||
| 16 | use App::Ordo::Command::Job::Create; | ||
| 17 | use App::Ordo::Command::Job::Update; | ||
| 18 | use App::Ordo::Command::Job::Hold; | ||
| 19 | use App::Ordo::Command::Job::Release; | ||
| 20 | use App::Ordo::Command::Job::Ice; | ||
| 21 | use App::Ordo::Command::Job::Melt; | ||
| 22 | use App::Ordo::Command::Job::Run; | ||
| 23 | use App::Ordo::Command::Job::Show; | ||
| 24 | use App::Ordo::Command::Job::Log; | ||
| 25 | use App::Ordo::Command::Job::Logs; | ||
| 26 | use App::Ordo::Command::Job::Delete; | ||
| 27 | use App::Ordo::Command::Job::Kill; | ||
| 28 | use App::Ordo::Command::Job::Complete; | ||
| 29 | use App::Ordo::Command::Job::Watch; | ||
| 30 | use App::Ordo::Command::Cluster::Create; | ||
| 31 | use App::Ordo::Command::Cluster::Update; | ||
| 32 | use App::Ordo::Command::Cluster::Hold; | ||
| 33 | use App::Ordo::Command::Cluster::Release; | ||
| 34 | use App::Ordo::Command::Cluster::Ice; | ||
| 35 | use App::Ordo::Command::Cluster::Melt; | ||
| 36 | use App::Ordo::Command::Cluster::Run; | ||
| 37 | use App::Ordo::Command::Cluster::Delete; | ||
| 38 | use App::Ordo::Command::Cluster::Show; | ||
| 39 | use App::Ordo::Command::Cluster::Kill; | ||
| 40 | use App::Ordo::Command::Cluster::Complete; | ||
| 41 | use App::Ordo::Command::Cluster::Reset; | ||
| 42 | use App::Ordo::Command::Cal::List; | ||
| 43 | use App::Ordo::Command::Cal::Create; | ||
| 44 | use App::Ordo::Command::Cal::Delete; | ||
| 45 | use App::Ordo::Command::Cal::Show; | ||
| 46 | use App::Ordo::Command::Cal::Attach; | ||
| 47 | use App::Ordo::Command::Cal::Detach; | ||
| 48 | use App::Ordo::Command::Server::List; | ||
| 49 | use App::Ordo::Command::Server::Add; | ||
| 50 | use App::Ordo::Command::Server::Delete; | ||
| 51 | |||
| 52 | has 'api' => (is => 'lazy'); | ||
| 53 | |||
| 54 | sub _build_api { | ||
| 55 | App::Ordo::API->new; | ||
| 56 | } | ||
| 57 | |||
| 58 | # Full command tree | ||
| 59 | my %COMMANDS = ( | ||
| 60 | ls => 'App::Ordo::Command::Ls', | ||
| 61 | cd => 'App::Ordo::Command::Cd', | ||
| 62 | |||
| 63 | help => 'App::Ordo::Command::Help', | ||
| 64 | sync => 'App::Ordo::Command::Sync', | ||
| 65 | |||
| 66 | user => { | ||
| 67 | show => 'App::Ordo::Command::User::Show', | ||
| 68 | }, | ||
| 69 | |||
| 70 | job => { | ||
| 71 | create => 'App::Ordo::Command::Job::Create', | ||
| 72 | update => 'App::Ordo::Command::Job::Update', | ||
| 73 | hold => 'App::Ordo::Command::Job::Hold', | ||
| 74 | release => 'App::Ordo::Command::Job::Release', | ||
| 75 | ice => 'App::Ordo::Command::Job::Ice', | ||
| 76 | melt => 'App::Ordo::Command::Job::Melt', | ||
| 77 | run => 'App::Ordo::Command::Job::Run', | ||
| 78 | show => 'App::Ordo::Command::Job::Show', | ||
| 79 | log => 'App::Ordo::Command::Job::Log', | ||
| 80 | logs => 'App::Ordo::Command::Job::Logs', | ||
| 81 | delete => 'App::Ordo::Command::Job::Delete', | ||
| 82 | kill => 'App::Ordo::Command::Job::Kill', | ||
| 83 | complete => 'App::Ordo::Command::Job::Complete', | ||
| 84 | watch => 'App::Ordo::Command::Job::Watch', | ||
| 85 | }, | ||
| 86 | |||
| 87 | cluster => { | ||
| 88 | create => 'App::Ordo::Command::Cluster::Create', | ||
| 89 | update => 'App::Ordo::Command::Cluster::Update', | ||
| 90 | hold => 'App::Ordo::Command::Cluster::Hold', | ||
| 91 | release => 'App::Ordo::Command::Cluster::Release', | ||
| 92 | ice => 'App::Ordo::Command::Cluster::Ice', | ||
| 93 | melt => 'App::Ordo::Command::Cluster::Melt', | ||
| 94 | run => 'App::Ordo::Command::Cluster::Run', | ||
| 95 | delete => 'App::Ordo::Command::Cluster::Delete', | ||
| 96 | show => 'App::Ordo::Command::Cluster::Show', | ||
| 97 | kill => 'App::Ordo::Command::Cluster::Kill', | ||
| 98 | complete => 'App::Ordo::Command::Cluster::Complete', | ||
| 99 | reset => 'App::Ordo::Command::Cluster::Reset', | ||
| 100 | }, | ||
| 101 | |||
| 102 | cal => { | ||
| 103 | list => 'App::Ordo::Command::Cal::List', | ||
| 104 | create => 'App::Ordo::Command::Cal::Create', | ||
| 105 | delete => 'App::Ordo::Command::Cal::Delete', | ||
| 106 | show => 'App::Ordo::Command::Cal::Show', | ||
| 107 | attach => 'App::Ordo::Command::Cal::Attach', | ||
| 108 | detach => 'App::Ordo::Command::Cal::Detach', | ||
| 109 | cron => { | ||
| 110 | add => 'App::Ordo::Command::Cal::Cron::Add', | ||
| 111 | delete => 'App::Ordo::Command::Cal::Cron::Delete', | ||
| 112 | }, | ||
| 113 | }, | ||
| 114 | |||
| 115 | server => { | ||
| 116 | list => 'App::Ordo::Command::Server::List', | ||
| 117 | add => 'App::Ordo::Command::Server::Add', | ||
| 118 | delete => 'App::Ordo::Command::Server::Delete', | ||
| 119 | }, | ||
| 120 | ); | ||
| 121 | |||
| 122 | sub run { | ||
| 123 | my ($self, @args) = @_; | ||
| 124 | |||
| 125 | my $first = $args[0]; | ||
| 126 | |||
| 127 | if ($first && $first eq 'help') { | ||
| 128 | App::Ordo::Command::Help->new( | ||
| 129 | api => $self->api, | ||
| 130 | commands => \%COMMANDS, | ||
| 131 | )->run(@args); | ||
| 132 | return; | ||
| 133 | } | ||
| 134 | |||
| 135 | my $node = \%COMMANDS; | ||
| 136 | my @path; | ||
| 137 | |||
| 138 | while (@args && ref($node) eq 'HASH' && exists $node->{$args[0]}) { | ||
| 139 | push @path, shift @args; | ||
| 140 | $node = $node->{$path[-1]}; | ||
| 141 | } | ||
| 142 | |||
| 143 | my $cmd_class = ref($node) eq '' ? $node : undef; | ||
| 144 | |||
| 145 | unless ($cmd_class) { | ||
| 146 | say colored(["bold red"], "Unknown command"); | ||
| 147 | say "Try 'help' for available commands"; | ||
| 148 | return; | ||
| 149 | } | ||
| 150 | |||
| 151 | eval { | ||
| 152 | eval "require $cmd_class; 1" or die "Failed to load $cmd_class: $@"; | ||
| 153 | $cmd_class->new(api => $self->api)->run(@args); | ||
| 154 | }; | ||
| 155 | if ($@) { | ||
| 156 | chomp $@; | ||
| 157 | say colored(["bold red"], "Error: $@"); | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | sub run_interactive { | ||
| 162 | my $self = shift; | ||
| 163 | App::Ordo->new(api => $self->api)->run_interactive; | ||
| 164 | } | ||
| 165 | |||
| 166 | 1; |