]>
Commit | Line | Data |
---|---|---|
bc05a8fc TL |
1 | package Proxmox::UI::StdIO; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
8fcdc5b2 CH |
6 | use JSON qw(from_json to_json); |
7 | ||
bc05a8fc TL |
8 | use base qw(Proxmox::UI::Base); |
9 | ||
a8fbe0ff TL |
10 | use Proxmox::Log; |
11 | ||
70f4ffef | 12 | my sub send_msg { |
8fcdc5b2 CH |
13 | my ($type, %values) = @_; |
14 | ||
15 | my $json = to_json({ type => $type, %values }, { utf8 => 1, canonical => 1 }); | |
16 | print STDOUT "$json\n"; | |
17 | } | |
18 | ||
70f4ffef | 19 | my sub recv_msg { |
8fcdc5b2 CH |
20 | my $response = <STDIN> // ''; # FIXME: error handling? |
21 | chomp($response); | |
22 | ||
23 | return eval { from_json($response, { utf8 => 1 }) }; | |
24 | } | |
25 | ||
0c4590e0 CH |
26 | sub init { |
27 | my ($self) = @_; | |
28 | ||
29 | STDOUT->autoflush(1); | |
30 | } | |
31 | ||
bc05a8fc TL |
32 | sub message { |
33 | my ($self, $msg) = @_; | |
34 | ||
70f4ffef | 35 | send_msg('message', message => $msg); |
bc05a8fc TL |
36 | } |
37 | ||
38 | sub error { | |
39 | my ($self, $msg) = @_; | |
8fcdc5b2 CH |
40 | |
41 | log_error("error: $msg"); | |
70f4ffef | 42 | send_msg('error', message => $msg); |
bc05a8fc TL |
43 | } |
44 | ||
a8fbe0ff TL |
45 | sub finished { |
46 | my ($self, $success, $msg) = @_; | |
47 | ||
48 | my $state = $success ? 'ok' : 'err'; | |
8fcdc5b2 | 49 | log_info("finished: $state, $msg"); |
70f4ffef | 50 | send_msg('finished', state => $state, message => $msg); |
a8fbe0ff TL |
51 | } |
52 | ||
bc05a8fc TL |
53 | sub prompt { |
54 | my ($self, $query) = @_; | |
55 | ||
70f4ffef TL |
56 | send_msg('prompt', query => $query); |
57 | my $response = recv_msg(); | |
bc05a8fc | 58 | |
8fcdc5b2 CH |
59 | if (defined($response) && $response->{type} eq 'prompt-answer') { |
60 | return lc($response->{answer}) eq 'ok'; | |
61 | } | |
bc05a8fc TL |
62 | } |
63 | ||
0903eb5c TL |
64 | sub display_html { |
65 | my ($raw_html, $html_dir) = @_; | |
66 | ||
faaaab30 | 67 | log_error("display_html() not available for stdio backend!"); |
0903eb5c TL |
68 | } |
69 | ||
3f463615 TL |
70 | sub progress { |
71 | my ($self, $ratio, $text) = @_; | |
72 | ||
347014ee CH |
73 | $text = '' if !defined($text); |
74 | ||
70f4ffef | 75 | send_msg('progress', ratio => $ratio, text => $text); |
3f463615 TL |
76 | } |
77 | ||
78 | sub process_events { | |
79 | my ($self) = @_; | |
80 | ||
81 | # nothing to do for now? | |
82 | } | |
83 | ||
bc05a8fc | 84 | 1; |