[pve-devel] [pve-common PATCH V2] Refactoring read_password.

Wolfgang Link w.link at proxmox.com
Fri Jan 13 13:36:45 CET 2017


We use this function in 3 different packets with the same code.

It will moved to the CLIHandler, because we need it only on the command line.
---
 src/PVE/CLIHandler.pm | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/src/PVE/CLIHandler.pm b/src/PVE/CLIHandler.pm
index e61fa6a..f0bafa2 100644

Change the variable names like Ditmar suggest.
Remove flag undo_shadow and unset shadow always. 

--- a/src/PVE/CLIHandler.pm
+++ b/src/PVE/CLIHandler.pm
@@ -8,6 +8,7 @@ use PVE::SafeSyslog;
 use PVE::Exception qw(raise raise_param_exc);
 use PVE::RESTHandler;
 use PVE::INotify;
+use Term::ReadLine;
 
 use base qw(PVE::RESTHandler);
 
@@ -48,6 +49,30 @@ my $complete_command_names = sub {
     return $res;
 };
 
+sub read_password_impl{
+    my ($command, $verify_passwd) = @_;
+
+    my $term = new Term::ReadLine($command);
+    my $attribs = $term->Attribs;
+    my $redisplay = $attribs->{redisplay_function};
+    $attribs->{redisplay_function} = $attribs->{shadow_redisplay};
+
+    my $remove_hist = $term->Features->{autohistory} ? 1 : 0;
+
+    my $passwd = undef;
+    $passwd = $term->readline('Enter password: ');
+    $term->remove_history($term->where_history()) if $remove_hist;
+
+    my $verify = $term->readline('Retype password: ') if $verify_passwd;
+    $term->remove_history($term->where_history()) if $remove_hist;
+
+    $attribs->{redisplay_function} = $redisplay;
+    die "Passwords do not match.\n" if ($verify_passwd && ($passwd ne $verify));
+
+    return $passwd;
+
+}
+
 __PACKAGE__->register_method ({
     name => 'help', 
     path => 'help',
-- 
2.1.4





More information about the pve-devel mailing list