Ordo.pm untested

filter: Statementstmt 0.0% 0 / 167 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 31 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 298 CC 28 SCAR 39.6
CC
28
Cov
0%
less
132.0
extract_command
90.0
epoch_to_tminus
30.0
CRAP 52.3
1 package App::Ordo;
2 use strict;
3 use warnings;
4 use feature qw(say);
5 use Moo;
6 use Exporter 'import';
7  
8 use JSON qw(encode_json);
9 use File::Path qw(make_path);
10 use File::Copy qw(copy);
11 use File::ShareDir qw(dist_file);
12 use Term::ANSIColor qw(colored);
13 use Term::ReadLine::Perl5;
14 use Term::ReadKey qw(ReadMode ReadKey GetTerminalSize);
15 use Email::Valid;
16  
17 use App::Ordo::API;
18  
19 our @EXPORT_OK = qw(
20 $CURRENT_PATH
21 extract_command
22 epoch_to_tminus
23 epoch_to_duration
24 );
25  
26 our $VERSION = '0.06';
27 our $CURRENT_PATH = '/';
28  
29 has 'api' => (
30 is => 'lazy',
31 default => sub { App::Ordo::API->new },
32 );
33  
34 # ------------------------------------------------------------------
35 # Session / Auth
36 # ------------------------------------------------------------------
37 sub ensure_session {
38 my ($self) = @_;
39  
40 my $config = $self->api->config;
41 my $token = $config->{token} // '';
42  
43 my $res = $self->api->call('login_user', { token => $token });
44  
45 if ($res->{success}) {
46 $CURRENT_PATH = $res->{path} // '/';
47 say colored(["bold green"], "Connected to " . $self->api->config->{api} . " as $res->{email}");
48 return $res;
49 }
50  
51 say colored(["bold yellow"], "No valid token found");
52 return $self->prompt_for_token;
53 }
54  
55 sub prompt_for_token {
56 my $self = shift;
57 while (1) {
58 print colored(["bold white"], "Enter API token: ");
59 chomp(my $token = <STDIN>);
60 $token =~ s/^\s+|\s+$//g;
61  
62 my $res = $self->api->call('login_user', { token => $token });
63  
64 if ($res->{success} && $res->{level} >= 1) {
65 my $config = $self->api->config;
66 $config->{token} = $token;
67 open my $fh, '>', $self->api->config_file or die "Cannot save config: $!";
68 print $fh encode_json($config);
69 close $fh;
70 $CURRENT_PATH = $res->{path} // '/';
71 say colored(["bold green"], "Logged in successfully");
72 return $res;
73 }
74 say colored(["bold red"], $res->{message} || "Invalid token");
75 }
76 }
77  
78 # ------------------------------------------------------------------
79 # Interactive Shell
80 # ------------------------------------------------------------------
81 sub run_interactive {
82 my $self = shift;
83  
84 $self->ensure_session;
85  
86 my $term = Term::ReadLine::Perl5->new('ordo');
87  
88 say colored(["bold white"], "\nWelcome to Ordo - the hierarchical job scheduler");
89 say "Type 'help' for commands, Ctrl-D to exit.\n";
90  
91 while (defined(my $line = $term->readline("ordo:$CURRENT_PATH> "))) {
92 $line =~ s/^\s+|\s+$//g;
93 next unless $line;
94 my @args = extract_command($line);
95 App::Ordo::Runner->new(api => $self->api)->run(@args);
96 }
97  
98 say colored(["bold yellow"], "\nGoodbye!");
99 }
100  
101 # ------------------------------------------------------------------
102 # Command line parsing
103 # ------------------------------------------------------------------
104 sub extract_command {
105 my ($line) = @_;
106 return () unless defined $line;
107 my @args;
108 my $current = '';
109 my $in_quote = '';
110 for my $char (split //, $line) {
111 if ($in_quote) {
112 $current .= $char;
113 $in_quote = '' if $char eq $in_quote;
114 } elsif ($char eq '"' || $char eq "'") {
115 $in_quote = $char;
116 $current .= $char;
117 } elsif ($char =~ /\s/) {
118 push @args, $current if length $current;
119 $current = '';
120 } else {
121 $current .= $char;
122 }
123 }
124 push @args, $current if length $current;
125 # Strip surrounding quotes
126 @args = map { /^['"](.*)['"]$/ ? $1 : $_ } @args;
127 return @args;
128 }
129  
130 # ------------------------------------------------------------------
131 # Time formatting helpers
132 # ------------------------------------------------------------------
133 sub epoch_to_tminus {
134 my $epoch = shift;
135 return '' unless $epoch && $epoch =~ /^\d+$/;
136 my $current_epoch = time;
137 my $diff = $current_epoch - $epoch;
138 return $diff < 60 ? "${diff}s ago"
139 : $diff < 3600 ? int($diff/60) . "m ago"
140 : $diff < 86400 ? int($diff/3600) . "h ago"
141 : int($diff/86400) . "d ago";
142 }
143  
144 sub epoch_to_duration {
145 my ($start, $end) = @_;
146 return '' unless $start;
147 $end //= time;
148 my $diff_seconds = abs($end - $start);
149 my $days = int($diff_seconds / (24 * 60 * 60));
150 $diff_seconds %= (24 * 60 * 60);
151 my $hours = int($diff_seconds / (60 * 60));
152 $diff_seconds %= (60 * 60);
153 my $minutes = int($diff_seconds / 60);
154 my $seconds = $diff_seconds % 60;
155 my $duration = sprintf("%02d:%02d:%02d", $hours, $minutes, $seconds);
156 if ($days) {
157 $duration .= " +$days" . 'd';
158 }
159 return $duration;
160 }
161  
162 # ------------------------------------------------------------------
163 # Pager (optional but useful)
164 # ------------------------------------------------------------------
165 sub less {
166 my ($string) = @_;
167 my @lines = split /\n/, $string;
168 $_ .= "\n" for @lines;
169 push @lines, "\n";
170 my ($width, $height) = GetTerminalSize();
171 $height -= 1;
172 my $total_lines = @lines;
173 if ($total_lines <= $height) {
174 print for @lines;
175 return;
176 }
177 my $current = 0;
178 ReadMode 'cbreak';
179 while (1) {
180 system("clear");
181 for my $i ($current .. $current + $height - 1) {
182 last if $i >= $total_lines;
183 print $lines[$i];
184 }
185 my $percent = int(($current + $height) / $total_lines * 100);
186 printf "\033[7m(line %d of %d, %d%%) [q quit, space page down, b page up, j/k lines]\033[0m\n",
187 $current + $height > $total_lines ? $total_lines : $current + $height,
188 $total_lines, $percent;
189 my $key = ReadKey(0);
190 last if $key eq 'q' || $key eq 'Q';
191 $current += $height if $key eq ' ';
192 $current -= $height if $key eq 'b';
193 $current++ if $key eq 'j' || $key eq "\n" || $key eq "\r";
194 $current-- if $key eq 'k';
195 $current = 0 if $current < 0;
196 $current = $total_lines - $height if $current > $total_lines - $height;
197 }
198 ReadMode 'normal';
199 print "\n";
200 }
201  
202 1;