Skip to content

Commit 04cca1a

Browse files
committed
feat!: Make get_secret utility functions use secret references
1 parent b70669b commit 04cca1a

File tree

3 files changed

+30
-24
lines changed

3 files changed

+30
-24
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,12 @@ able to access the session. You also **must** configure `op.nvim` with `biometri
221221

222222
All commands are also available as a Lua API, see [API](#api). Additionally there are two utility methods for grabbing secrets to use in scripting:
223223

224-
- `require('op').get_secret(item_name: string, field_name: string): string|nil, string|nil`
225-
- `require('op').get_secret_async(item_name: string, field_name: string, callback: fun(secret: string|nil, stderr: string|nil))`
224+
- `require('op').get_secret(item_reference: string): string|nil, string|nil`
225+
- `require('op').get_secret_async(item_reference: string, callback: fun(secret: string|nil, stderr: string|nil))`
226+
227+
If the CLI integration is enabled in the 1Password desktop app, the field action menu will have an item to "Copy secret reference".
228+
You can also go to Settings -> Developer and "Show 1Password Developer experience" to enable the "Copy UUID" actions in the field and item action
229+
menus and the right click menu on vaults. To get an account UUID, run `op account list` and copy the `USER ID` field.
226230

227231
## Features
228232

@@ -314,8 +318,8 @@ All commands are also available as a Lua API as described below:
314318

315319
Additionally there are two utility methods for grabbing secrets to use in scripting:
316320

317-
- `require('op').get_secret(item_name: string, field_name: string): string|nil, string|nil`
318-
- `require('op').get_secret_async(item_name: string, field_name: string, callback: fun(secret: string|nil, stderr: string|nil))`
321+
- `require('op').get_secret(item_reference: string): string|nil, string|nil`
322+
- `require('op').get_secret_async(item_reference: string, callback: fun(secret: string|nil, stderr: string|nil))`
319323

320324
Additionally, part of `op.nvim`'s design includes complete bindings to the CLI that you can use for scripting with Lua. This API
321325
is available in the `op.api` module. This module returns a table that matches the hierarchy of the 1Password CLI commands.

doc/op.txt

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*op.txt* Last change: 2024 March 18
1+
*op.txt* Last change: 2024 May 28
22

33
==============================================================================
44
Table of Contents *op-table-of-contents*
@@ -266,10 +266,16 @@ COMMANDS *op-commands*
266266
All commands are also available as a Lua API, see API <#api>. Additionally
267267
there are two utility methods for grabbing secrets to use in scripting:
268268

269-
- `require('op').get_secret(item_name: string, field_name: string):
270-
string|nil, string|nil`
271-
- `require('op').get_secret_async(item_name: string, field_name: string,
272-
callback: fun(secret: string|nil, stderr: string|nil))`
269+
- `require('op').get_secret(item_reference: string): string|nil, string|nil`
270+
- `require('op').get_secret_async(item_reference: string, callback:
271+
fun(secret: string|nil, stderr: string|nil))`
272+
If the CLI integration is enabled in the 1Password desktop app, the field
273+
action menu will have an item to "Copy secret reference". You can also go to
274+
Settings -> Developer and "Show 1Password Developer experience" to enable the
275+
"Copy UUID" actions in the field and item action menus and the right click
276+
menu on vaults. To get an account UUID, run `op account list` and copy the
277+
`USER ID` field.
278+
273279

274280
------------------------------------------------------------------------------
275281
FEATURES *op-features*
@@ -397,10 +403,9 @@ All commands are also available as a Lua API as described below:
397403
Additionally there are two utility methods for grabbing secrets to use in
398404
scripting:
399405

400-
- `require('op').get_secret(item_name: string, field_name: string):
401-
string|nil, string|nil`
402-
- `require('op').get_secret_async(item_name: string, field_name: string,
403-
callback: fun(secret: string|nil, stderr: string|nil))`
406+
- `require('op').get_secret(item_reference: string): string|nil, string|nil`
407+
- `require('op').get_secret_async(item_reference: string, callback:
408+
fun(secret: string|nil, stderr: string|nil))`
404409
Additionally, part of `op.nvim`'s design includes complete bindings to the CLI
405410
that you can use for scripting with Lua. This API is available in the `op.api`
406411
module. This module returns a table that matches the hierarchy of the
@@ -433,4 +438,4 @@ See lua/op/types.lua <./lua/op/types.lua> for type annotations describing the
433438
completions when using `lua-language-server`.
434439

435440

436-
vim:tw=78:ts=8:ft=help:norl:
441+
vim:tw=78:ts=8:ft=help:norl:

lua/op/init.lua

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -266,12 +266,10 @@ function M.op_analyze_buffer()
266266
end
267267

268268
---Utility method to get a secret in 1 line of code.
269-
---@param item_name string the name of the item in 1Password to get the secret from
270-
---@param field_name string the name of the field on the 1Password item that holds the secret
269+
---@param item_reference string The `op://` item reference; perfer using UUIDs if you have multiple accounts
271270
---@return string|nil secret, string|nil stderr secret if successful, nil and the `STDERR` otherwise
272-
function M.get_secret(item_name, field_name)
273-
local args = { item_name, '--reveal', '--fields', field_name }
274-
local stdout, stderr = require('op.api').item.get(args)
271+
function M.get_secret(item_reference)
272+
local stdout, stderr = require('op.api').read({ item_reference })
275273
if not stdout or #stdout == 0 then
276274
return nil, table.concat(stderr, '\n')
277275
end
@@ -280,12 +278,11 @@ function M.get_secret(item_name, field_name)
280278
end
281279

282280
---Utility method to asynchronously get a secret in 1 line of code.
283-
---@param item_name string the name of the item in 1Password to get the secret from
284-
---@param field_name string the name of the field on the 1Password item that holds the secret
281+
---@param item_reference string The `op://` item reference; prefer using UUIDs if you have multiple accounts
285282
---@param callback fun(secret:string|nil, stderr:string|nil) callback to call with the secret on success, else `STDERR`
286-
function M.get_secret_async(item_name, field_name, callback)
287-
local args = { async = true, item_name, '--reveal', '--fields', field_name }
288-
require('op.api').item.get(args, function(stdout, stderr)
283+
function M.get_secret_async(item_reference, callback)
284+
local args = { async = true, item_reference }
285+
require('op.api').read(args, function(stdout, stderr)
289286
if not stdout or #stdout == 0 then
290287
callback(nil, table.concat(stderr, '\n'))
291288
else

0 commit comments

Comments
 (0)