Skip to content

Commit

Permalink
feat: replace OP_MODE
Browse files Browse the repository at this point in the history
  • Loading branch information
franklinkim committed Feb 5, 2024
1 parent d1d5b20 commit ec3024a
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions internal/template/onepassword.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var (

var ErrOnePasswordNotSignedIn = errors.New("not signed in")

func onePasswordCIGet(client connect.Client, vaultUUID, itemUUID string) (map[string]string, error) {
func onePasswordConnectGet(client connect.Client, vaultUUID, itemUUID string) (map[string]string, error) {
var item *onepassword.Item
if onePasswordUUID.Match([]byte(itemUUID)) {
if v, err := client.GetItem(itemUUID, vaultUUID); err != nil {
Expand Down Expand Up @@ -115,13 +115,17 @@ func onePasswordSignIn(ctx context.Context, account string) error {
return nil
}

func isConnect() bool {
return os.Getenv("OP_CONNECT_HOST") != "" && os.Getenv("OP_CONNECT_TOKEN") != ""
}

func onePassword(ctx context.Context, templateVars interface{}, errorOnMissing bool) func(account, vaultUUID, itemUUID, field string) (string, error) {
if onePasswordCache == nil {
onePasswordCache = map[string]map[string]string{}
}
return func(account, vaultUUID, itemUUID, field string) (string, error) {
// validate command
if mode := os.Getenv("OP_MODE"); mode == "ci" {
if isConnect() {
// do nothing
} else if _, err := exec.LookPath("op"); err != nil {
fmt.Println("Your templates includes a call to 1Password, please install it:")
Expand Down Expand Up @@ -150,11 +154,11 @@ func onePassword(ctx context.Context, templateVars interface{}, errorOnMissing b
// create cache key
cacheKey := strings.Join([]string{account, vaultUUID, itemUUID}, "#")

if mode := os.Getenv("OP_MODE"); mode == "ci" {
if isConnect() {
if _, ok := onePasswordCache[cacheKey]; !ok {
if client, err := connect.NewClientFromEnvironment(); err != nil {
return "", err
} else if res, err := onePasswordCIGet(client, vaultUUID, itemUUID); err != nil {
} else if res, err := onePasswordConnectGet(client, vaultUUID, itemUUID); err != nil {
return "", err
} else {
onePasswordCache[cacheKey] = res
Expand Down

0 comments on commit ec3024a

Please sign in to comment.