| 1 | package App::Ordo::Command::Help; | ||
| 2 | use Moo; | ||
| 3 | use feature qw(say); | ||
| 4 | use utf8; | ||
| 5 | use open ':std', ':utf8'; | ||
| 6 | extends 'App::Ordo::Command::Base'; | ||
| 7 | |||
| 8 | use Term::ANSIColor qw(colored); | ||
| 9 | |||
| 10 | has 'commands' => (is => 'ro', required => 0); | ||
| 11 | |||
| 12 | sub name { "help" } | ||
| 13 | sub summary { "Show help information" } | ||
| 14 | sub usage { "[topic]" } | ||
| 15 | |||
| 16 | sub option_spec { {} } | ||
| 17 | |||
| 18 | sub execute { | ||
| 19 | my ($self, $opt, @topic_parts) = @_; | ||
| 20 | shift @topic_parts; | ||
| 21 | my $topic = join ' ', @topic_parts; | ||
| 22 | |||
| 23 | if (!$topic) { | ||
| 24 | $self->_help_overview; | ||
| 25 | return; | ||
| 26 | } | ||
| 27 | |||
| 28 | $self->_help_topic($topic); | ||
| 29 | } | ||
| 30 | |||
| 31 | sub _help_topic { | ||
| 32 | my ($self, $topic) = @_; | ||
| 33 | $topic =~ s/^\s+|\s+$//g; | ||
| 34 | |||
| 35 | my @parts = split /\s+/, $topic; | ||
| 36 | my $cmd_class = "App::Ordo::Command::" . join('::', map { ucfirst lc } @parts); | ||
| 37 | |||
| 38 | if (eval "require $cmd_class; 1") { | ||
| 39 | $cmd_class->new(api => $self->api)->show_help; | ||
| 40 | } else { | ||
| 41 | say colored(["bold red"], "No help available for '$topic'"); | ||
| 42 | say "Try 'help' for a list of commands"; | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | sub _help_overview { | ||
| 47 | my ($self) = @_; | ||
| 48 | |||
| 49 | say colored(["bold cyan"], "Usage:"); | ||
| 50 | say " ordo # interactive shell"; | ||
| 51 | say " ordo <command> [args] # one-shot mode\n"; | ||
| 52 | |||
| 53 | say colored(["bold yellow"], "Commands:\n"); | ||
| 54 | |||
| 55 | my %groups = ( | ||
| 56 | Navigation => [], | ||
| 57 | Jobs => [], | ||
| 58 | Clusters => [], | ||
| 59 | Calendars => [], | ||
| 60 | Servers => [], | ||
| 61 | User => [], | ||
| 62 | System => [], | ||
| 63 | ); | ||
| 64 | |||
| 65 | $self->_walk_tree($self->commands, \%groups, []); | ||
| 66 | |||
| 67 | for my $group (qw(Navigation Jobs Clusters Calendars Servers User System)) { | ||
| 68 | say colored(["bold green"], " $group:"); | ||
| 69 | if (@{$groups{$group}}) { | ||
| 70 | my $max = 0; | ||
| 71 | $max = length($_->[0]) > $max ? length($_->[0]) : $max for @{$groups{$group}}; | ||
| 72 | printf " %-*s %s\n", $max + 4, $_->[0], $_->[1] for @{$groups{$group}}; | ||
| 73 | } else { | ||
| 74 | say " (no commands)"; | ||
| 75 | } | ||
| 76 | say ""; | ||
| 77 | } | ||
| 78 | |||
| 79 | say colored(["bold magenta"], "Examples:"); | ||
| 80 | say " server add myserver --host 192.168.1.100 --user alice"; | ||
| 81 | say " cluster create mycluster"; | ||
| 82 | say " job create mycluster/myjob --server myserver --script 'ls -ltr'"; | ||
| 83 | |||
| 84 | say "\nType 'help <command>' for detailed help"; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub _walk_tree { | ||
| 88 | my ($self, $node, $groups, $path) = @_; | ||
| 89 | |||
| 90 | if (ref $node eq 'HASH') { | ||
| 91 | for my $key (keys %$node) { | ||
| 92 | $self->_walk_tree($node->{$key}, $groups, [@$path, $key]); | ||
| 93 | } | ||
| 94 | } else { | ||
| 95 | my $class = $node; | ||
| 96 | my $name = join ' ', @$path; | ||
| 97 | |||
| 98 | my $summary = "no description"; | ||
| 99 | if (eval "require $class; 1") { | ||
| 100 | $summary = eval { $class->new(api => $self->api)->summary } || "no description"; | ||
| 101 | } | ||
| 102 | |||
| 103 | if ($name =~ /^job /i) { | ||
| 104 | push @{$groups->{Jobs}}, [$name, $summary]; | ||
| 105 | } | ||
| 106 | elsif ($name =~ /^cluster /i) { | ||
| 107 | push @{$groups->{Clusters}}, [$name, $summary]; | ||
| 108 | } | ||
| 109 | elsif ($name =~ /^cal /i) { | ||
| 110 | push @{$groups->{Calendars}}, [$name, $summary]; | ||
| 111 | } | ||
| 112 | elsif ($name =~ /^server /i) { | ||
| 113 | push @{$groups->{Servers}}, [$name, $summary]; | ||
| 114 | } | ||
| 115 | elsif ($name =~ /^(ls|cd|pwd)$/i) { | ||
| 116 | push @{$groups->{Navigation}}, [$name, $summary]; | ||
| 117 | } | ||
| 118 | elsif ($name =~ /^user /i) { | ||
| 119 | push @{$groups->{User}}, [$name, $summary]; | ||
| 120 | } | ||
| 121 | else { | ||
| 122 | push @{$groups->{System}}, [$name, $summary]; | ||
| 123 | } | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | 1; |