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