-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
269 lines (226 loc) · 5.88 KB
/
Copy pathinit.lua
File metadata and controls
269 lines (226 loc) · 5.88 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
if (runx.api_version or 0) < 1 then
error("Runx plugin API v1 or newer is required")
end
local function trim(value)
return value:gsub("^%s+", ""):gsub("%s+$", "")
end
local function first_non_flag(parts)
for _, value in ipairs(parts) do
if value:sub(1, 1) ~= "-" then
return value
end
end
return nil
end
local function password_store_dir()
local config = runx.plugin_config or {}
local dir = config.store_dir
if dir and dir ~= "" then
return dir
end
return runx.home_dir() .. "/.password-store"
end
local function list_entries()
local files = runx.walk_files(password_store_dir())
local entries = {}
for _, file in ipairs(files) do
if file:match("%.gpg$") and not file:match("^%.") then
local entry = file:gsub("%.gpg$", "")
table.insert(entries, entry)
end
end
return entries
end
local function pass_show(entry)
return runx.exec_capture("pass", { "show", entry }, true)
end
local function pass_copy(entry)
runx.exec_status("pass", { "show", "-c1", entry }, false)
return "Copied password"
end
local function pass_otp(entry)
return runx.exec_capture("pass", { "otp", entry }, true)
end
local function pass_otp_copy(entry)
runx.exec_status("pass", { "otp", "-c", entry }, false)
return "Copied OTP"
end
local function pass_generate(parts)
if #parts == 0 then
error("pass-gen needs arguments")
end
local command = { "generate" }
for _, part in ipairs(parts) do
table.insert(command, part)
end
runx.exec_status("pass", command, false)
local entry = first_non_flag(parts)
if not entry then
error("could not infer generated pass entry")
end
return pass_show(entry)
end
local function pass_generate_copy(parts)
if #parts == 0 then
error("pass-gen-copy needs arguments")
end
local command = { "generate", "-c" }
for _, part in ipairs(parts) do
table.insert(command, part)
end
runx.exec_status("pass", command, false)
return "Generated password and copied it"
end
local function result_limit()
local config = runx.plugin_config or {}
return tonumber(config.result_limit) or 12
end
local function score_entries(query, action_kind)
local trimmed = trim(query)
if trimmed == "" then
return {}
end
local entries = list_entries()
local matches = {}
for _, entry in ipairs(entries) do
local score = runx.fuzzy_score(entry, trimmed)
if score > 0 then
table.insert(matches, {
id = action_kind .. ":" .. entry,
title = entry,
score = score,
payload = {
kind = action_kind,
entry = entry,
},
})
end
end
table.sort(matches, function(left, right)
if left.score == right.score then
return left.title < right.title
end
return left.score > right.score
end)
local limit = result_limit()
while #matches > limit do
table.remove(matches)
end
return matches
end
return {
id = "pass",
name = "pass",
badge = "PASS",
commands = {
["pass"] = "search_type_password",
["pass-copy"] = "search_copy_password",
["pass-otp"] = "search_type_otp",
["pass-otp-copy"] = "search_copy_otp",
["pass-gen"] = "search_generate_type",
["pass-gen-copy"] = "search_generate_copy",
},
search_type_password = function(raw)
return score_entries(raw, "type_password")
end,
search_copy_password = function(raw)
return score_entries(raw, "copy_password")
end,
search_type_otp = function(raw)
return score_entries(raw, "type_otp")
end,
search_copy_otp = function(raw)
return score_entries(raw, "copy_otp")
end,
search_generate_type = function(raw, argv)
local trimmed = trim(raw)
if trimmed == "" then
return {
{
id = "generate_type:hint",
title = "pass-gen <args>",
style = "full",
subtitle = "Run `pass generate` and type the result",
badge = "GEN",
score = 10,
payload = {
kind = "noop",
},
},
}
end
return {
{
id = "generate_type:" .. trimmed,
title = "pass-gen " .. trimmed,
style = "full",
subtitle = "Run `pass generate ...` and type the result",
badge = "GEN",
score = 1000,
payload = {
kind = "generate_type",
argv = argv,
},
},
}
end,
search_generate_copy = function(raw, argv)
local trimmed = trim(raw)
if trimmed == "" then
return {
{
id = "generate_copy:hint",
title = "pass-gen-copy <args>",
style = "full",
subtitle = "Run `pass generate -c ...`",
badge = "GEN",
score = 10,
payload = {
kind = "noop",
},
},
}
end
return {
{
id = "generate_copy:" .. trimmed,
title = "pass-gen-copy " .. trimmed,
style = "full",
subtitle = "Run `pass generate -c ...`",
badge = "GEN",
score = 1000,
payload = {
kind = "generate_copy",
argv = argv,
},
},
}
end,
run = function(payload)
if payload.kind == "noop" then
return "Pass generator is ready."
end
if payload.kind == "type_password" then
local text = pass_show(payload.entry)
return runx.type_text(text)
end
if payload.kind == "copy_password" then
return pass_copy(payload.entry)
end
if payload.kind == "type_otp" then
local text = pass_otp(payload.entry)
return runx.type_text(text)
end
if payload.kind == "copy_otp" then
return pass_otp_copy(payload.entry)
end
if payload.kind == "generate_type" then
local text = pass_generate(payload.argv or {})
return runx.type_text(text)
end
if payload.kind == "generate_copy" then
return pass_generate_copy(payload.argv or {})
end
error("unknown pass action: " .. tostring(payload.kind))
end,
}