forked from booski/passman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompletion.pm
executable file
·116 lines (89 loc) · 2.72 KB
/
completion.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env perl
use lib ($ENV{RLWRAP_FILTERDIR} or ".");
use RlwrapFilter;
use strict;
my $filter = new RlwrapFilter;
my $name = $filter -> name;
$filter -> help_text("");
$filter -> completion_handler(\&complete);
$filter -> run;
sub users {
return qx(./passman list user);
}
sub groups {
return qx(./passman list group);
}
sub passwords {
return qx(./passman list pass);
}
sub complete {
my ($line, $prefix, @completions) = @_;
my @base = ('get','modify',
'list','info',
'passwd',
'help',
'add','del',
'manage',
'promote','demote');
my @words = split /\s+/, $line;
my $nwords = scalar @words;
# only count completed words
$nwords-- if $prefix;
my @result = ();
if($nwords == 0) {
@result = @base;
} else {
# try to avoid suggesting completions in the middle of a line
return '' unless grep /$prefix$/, $words[-1];
my $first = $words[0];
if($first eq 'passwd') {
@result = ();
} elsif($nwords == 1
&& $first eq 'help') {
@result = @base;
} elsif($nwords == 1
&& ($first eq 'get'
|| $first eq 'modify')) {
@result = &passwords;
} elsif($first eq 'list'
|| $first eq 'info'
|| $first eq 'add'
|| $first eq 'del') {
if($nwords == 1) {
@result = ('user','group','pass');
} elsif($nwords == 2) {
my $second = $words[1];
if($second eq 'user') {
@result = &users;
} elsif($second eq 'group') {
@result = &groups;
} elsif($second eq 'pass') {
@result = &passwords;
}
}
} elsif($nwords == 1
&& ($first eq 'promote'
|| $first eq 'demote')) {
@result = &users;
} elsif($first eq 'manage') {
if($nwords == 1) {
@result = ('user','pass');
} else {
my $second = $words[1];
if($nwords == 2) {
if($second eq 'user') {
@result = &users;
} elsif($second eq 'pass') {
@result = &passwords;
}
} elsif($nwords == 3) {
my @temp = &groups;
@result = map { '-' . $_ } @temp;
push @result, map { '+' . $_ } @temp;
}
}
}
}
chomp(@result);
return grep /^$prefix/, @result;
}