Skip to content

Commit

Permalink
Tidy up onepassword template functions
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Jul 3, 2021
1 parent 509bda7 commit 9d02a5a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 51 deletions.
32 changes: 16 additions & 16 deletions docs/REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -1748,11 +1748,11 @@ using the [1Password
CLI](https://support.1password.com/command-line-getting-started/) (`op`). *uuid*
is passed to `op get item <uuid>` and the output from `op` is parsed as JSON.
The output from `op` is cached so calling `onepassword` multiple times with the
same *uuid* will only invoke `op` once. If the optional *vault-uuid* is supplied,
it will be passed along to the `op get` call, which can significantly improve
performance. If the optional *account-name* is supplied, it will be passed along
to the `op get` call, which will help it look in the right account, in case you
have multiple accounts (eg. personal and work account).
same *uuid* will only invoke `op` once. If the optional *vault-uuid* is
supplied, it will be passed along to the `op get` call, which can significantly
improve performance. If the optional *account-name* is supplied, it will be
passed along to the `op get` call, which will help it look in the right account,
in case you have multiple accounts (eg. personal and work accounts).

#### `onepassword` examples

Expand All @@ -1765,16 +1765,16 @@ have multiple accounts (eg. personal and work account).

### `onepasswordDocument` *uuid* [*vault-uuid* [*account-name*]]

`onepassword` returns a document from [1Password](https://1password.com/)
using the [1Password
CLI](https://support.1password.com/command-line-getting-started/) (`op`). *uuid*
is passed to `op get document <uuid>` and the output from `op` is returned.
The output from `op` is cached so calling `onepasswordDocument` multiple times with the
same *uuid* will only invoke `op` once. If the optional *vault-uuid* is supplied,
it will be passed along to the `op get` call, which can significantly improve
performance. If the optional *account-name* is supplied, it will be passed along
to the `op get` call, which will help it look in the right account, in case you
have multiple accounts (eg. personal and work account).
`onepassword` returns a document from [1Password](https://1password.com/) using
the [1Password CLI](https://support.1password.com/command-line-getting-started/)
(`op`). *uuid* is passed to `op get document <uuid>` and the output from `op` is
returned. The output from `op` is cached so calling `onepasswordDocument`
multiple times with the same *uuid* will only invoke `op` once. If the optional
*vault-uuid* is supplied, it will be passed along to the `op get` call, which
can significantly improve performance. If the optional *account-name* is
supplied, it will be passed along to the `op get` call, which will help it look
in the right account, in case you have multiple accounts (eg. personal and work
accounts).

#### `onepasswordDocument` examples

Expand Down Expand Up @@ -1841,7 +1841,7 @@ times with the same *uuid* will only invoke `op` once. If the optional
can significantly improve performance. If the optional *account-name* is
supplied, it will be passed along to the `op get` call, which will help it look
in the right account, in case you have multiple accounts (eg. personal and work
account).
accounts).

#### `onepasswordDetailsFields` examples

Expand Down
50 changes: 15 additions & 35 deletions internal/cmd/onepasswordtemplatefuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,7 @@ type onepasswordConfig struct {
}

func (c *Config) onepasswordDetailsFieldsTemplateFunc(args ...string) map[string]interface{} {
key, vault, account := onepasswordGetKeyAndVaultAndAccount(args)
onepasswordArgs := []string{"get", "item", key}
if vault != "" {
onepasswordArgs = append(onepasswordArgs, "--vault", vault)
}
if account != "" {
onepasswordArgs = append(onepasswordArgs, "--account", account)
}
onepasswordArgs := getOnepasswordArgs([]string{"get", "item"}, args)
output := c.onepasswordOutput(onepasswordArgs)
var data struct {
Details struct {
Expand All @@ -44,14 +37,7 @@ func (c *Config) onepasswordDetailsFieldsTemplateFunc(args ...string) map[string
}

func (c *Config) onepasswordDocumentTemplateFunc(args ...string) string {
key, vault, account := onepasswordGetKeyAndVaultAndAccount(args)
onepasswordArgs := []string{"get", "document", key}
if vault != "" {
onepasswordArgs = append(onepasswordArgs, "--vault", vault)
}
if account != "" {
onepasswordArgs = append(onepasswordArgs, "--account", account)
}
onepasswordArgs := getOnepasswordArgs([]string{"get", "document"}, args)
output := c.onepasswordOutput(onepasswordArgs)
return string(output)
}
Expand Down Expand Up @@ -81,14 +67,7 @@ func (c *Config) onepasswordOutput(args []string) []byte {
}

func (c *Config) onepasswordTemplateFunc(args ...string) map[string]interface{} {
key, vault, account := onepasswordGetKeyAndVaultAndAccount(args)
onepasswordArgs := []string{"get", "item", key}
if vault != "" {
onepasswordArgs = append(onepasswordArgs, "--vault", vault)
}
if account != "" {
onepasswordArgs = append(onepasswordArgs, "--account", account)
}
onepasswordArgs := getOnepasswordArgs([]string{"get", "item"}, args)
output := c.onepasswordOutput(onepasswordArgs)
var data map[string]interface{}
if err := json.Unmarshal(output, &data); err != nil {
Expand All @@ -98,16 +77,17 @@ func (c *Config) onepasswordTemplateFunc(args ...string) map[string]interface{}
return data
}

func onepasswordGetKeyAndVaultAndAccount(args []string) (string, string, string) {
switch len(args) {
case 1:
return args[0], "", ""
case 2:
return args[0], args[1], ""
case 3:
return args[0], args[1], args[2]
default:
returnTemplateError(fmt.Errorf("expected 1 or 2 or 3 arguments, got %d", len(args)))
return "", "", ""
func getOnepasswordArgs(baseArgs, args []string) []string {
if len(args) < 1 || len(args) > 3 {
returnTemplateError(fmt.Errorf("expected 1, 2, or 3 arguments, got %d", len(args)))
return nil
}
baseArgs = append(baseArgs, args[0])
if len(args) > 1 {
baseArgs = append(baseArgs, "--vault", args[1])
}
if len(args) > 2 {
baseArgs = append(baseArgs, "--account", args[2])
}
return baseArgs
}

0 comments on commit 9d02a5a

Please sign in to comment.