joao/pkg/op-client/connect.go

74 lines
1.6 KiB
Go
Raw Permalink Normal View History

2022-11-16 08:17:07 +00:00
// Copyright © 2022 Roberto Hidalgo <joao@un.rob.mx>
2022-12-31 06:14:08 +00:00
// SPDX-License-Identifier: Apache-2.0
2022-12-14 05:41:03 +00:00
package opclient
2022-11-16 08:17:07 +00:00
import (
2023-01-11 04:50:06 +00:00
"strings"
2022-11-16 08:17:07 +00:00
"github.com/1Password/connect-sdk-go/connect"
op "github.com/1Password/connect-sdk-go/onepassword"
)
2023-01-11 04:50:06 +00:00
var ConnectClientFactory func(host, token, userAgent string) connect.Client = connect.NewClientWithUserAgent
2022-12-14 05:41:03 +00:00
// UUIDLength defines the required length of UUIDs.
2022-11-16 08:17:07 +00:00
const UUIDLength = 26
// IsValidClientUUID returns true if the given client uuid is valid.
func IsValidClientUUID(uuid string) bool {
if len(uuid) != UUIDLength {
return false
}
for _, c := range uuid {
valid := (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')
if !valid {
return false
}
}
return true
}
type Connect struct {
client connect.Client
}
var _ opClient = &Connect{}
2022-11-16 08:17:07 +00:00
const userAgent = "nidito-joao"
func NewConnect(host, token string) *Connect {
2023-01-11 04:50:06 +00:00
client := ConnectClientFactory(host, token, userAgent)
2022-11-16 08:17:07 +00:00
return &Connect{client: client}
}
func (b *Connect) Get(vault, name string) (*op.Item, error) {
return b.client.GetItem(name, vault)
}
2023-01-11 04:50:06 +00:00
func (b *Connect) Update(item *op.Item, remote *op.Item) error {
2022-11-16 08:17:07 +00:00
_, err := b.client.UpdateItem(item, item.Vault.ID)
return err
}
func (b *Connect) List(vault, prefix string) ([]string, error) {
2023-01-11 04:50:06 +00:00
items, err := b.client.GetItems(vault)
if err != nil {
return nil, err
}
res := []string{}
for _, item := range items {
if prefix != "" && !strings.HasPrefix(item.Title, prefix) {
continue
}
res = append(res, item.Title)
}
return res, nil
}
func (b *Connect) Create(vault string, item *op.Item) error {
_, err := b.client.CreateItem(item, vault)
2023-01-11 04:50:06 +00:00
return err
2022-11-16 08:17:07 +00:00
}