<commit_before>// +build darwin

package keyring

import (
	"log"

	gokeychain "github.com/keybase/go-keychain"
)

type keychain struct {
	path       string
	service    string
	passphrase string
}

func init() {
	supportedBackends[KeychainBackend] = opener(func(name string) (Keyring, error) {
		if name == "" {
			name = "login"
		}

		return &keychain{
			service: name,
			path:    name + ".keychain",
		}, nil
	})

	DefaultBackend = KeychainBackend
}

func (k *keychain) Get(key string) (Item, error) {
	kc, err := k.createOrOpen()
	if err != nil {
		return Item{}, err
	}

	query := gokeychain.NewItem()
	query.SetSecClass(gokeychain.SecClassGenericPassword)
	query.SetService(k.service)
	query.SetAccount(key)
	query.SetMatchLimit(gokeychain.MatchLimitOne)
	query.SetReturnAttributes(true)
	query.SetReturnData(true)
	query.UseKeychain(kc)

	results, err := gokeychain.QueryItem(query)
	if err == gokeychain.ErrorItemNotFound || len(results) == 0 {
		return Item{}, ErrKeyNotFound
	}

	if err != nil {
		return Item{}, err
	}

	item := Item{
		Key:         key,
		Data:        results[0].Data,
		Label:       results[0].Label,
		Description: results[0].Description,
	}

	return item, nil
}

func (k *keychain) Set(item Item) error {
	kc, err := k.createOrOpen()
	if err != nil {
		return err
	}

	newItem := gokeychain.NewItem()
	newItem.SetSecClass(gokeychain.SecClassGenericPassword)
	newItem.SetService(k.service)
	newItem.SetAccount(item.Key)
	newItem.SetLabel(item.Label)
	newItem.SetDescription(item.Description)
	newItem.SetData(item.Data)
	newItem.SetSynchronizable(gokeychain.SynchronizableNo)
	newItem.SetAccessible(gokeychain.AccessibleWhenUnlocked)
	newItem.UseKeychain(kc)
	newItem.SetAccess(gokeychain.NoApplicationsTrusted)

	log.Printf("Adding service=%q, account=%q to osx keychain %s", k.service, item.Key, k.path)
	return gokeychain.AddItem(item)
}

func (k *keychain) Remove(key string) error {
	kc := gokeychain.NewWithPath(k.path)

	if err := kc.Status(); err != nil {
		return err
	}

	item := gokeychain.NewItem()
	item.SetSecClass(gokeychain.SecClassGenericPassword)
	item.SetService(k.service)
	item.SetAccount(key)

	log.Printf("Removing keychain item service=%q, account=%q from osx keychain %q", k.service, key, k.path)
	return gokeychain.DeleteItem(item)
}

func (k *keychain) Keys() ([]string, error) {
	kc := gokeychain.NewWithPath(k.path)

	if err := kc.Status(); err != nil {
		return nil, err
	}

	query := gokeychain.NewItem()<commit_msg>Fix compile error<commit_after>// +build darwin

package keyring

import (
	"log"

	gokeychain "github.com/keybase/go-keychain"
)

type keychain struct {
	path       string
	service    string
	passphrase string
}

func init() {
	supportedBackends[KeychainBackend] = opener(func(name string) (Keyring, error) {
		if name == "" {
			name = "login"
		}

		return &keychain{
			service: name,
			path:    name + ".keychain",
		}, nil
	})

	DefaultBackend = KeychainBackend
}

func (k *keychain) Get(key string) (Item, error) {
	kc, err := k.createOrOpen()
	if err != nil {
		return Item{}, err
	}

	query := gokeychain.NewItem()
	query.SetSecClass(gokeychain.SecClassGenericPassword)
	query.SetService(k.service)
	query.SetAccount(key)
	query.SetMatchLimit(gokeychain.MatchLimitOne)
	query.SetReturnAttributes(true)
	query.SetReturnData(true)
	query.UseKeychain(kc)

	results, err := gokeychain.QueryItem(query)
	if err == gokeychain.ErrorItemNotFound || len(results) == 0 {
		return Item{}, ErrKeyNotFound
	}

	if err != nil {
		return Item{}, err
	}

	item := Item{
		Key:         key,
		Data:        results[0].Data,
		Label:       results[0].Label,
		Description: results[0].Description,
	}

	return item, nil
}

func (k *keychain) Set(item Item) error {
	kc, err := k.createOrOpen()
	if err != nil {
		return err
	}

	kcItem := gokeychain.NewItem()
	kcItem.SetSecClass(gokeychain.SecClassGenericPassword)
	kcItem.SetService(k.service)
	kcItem.SetAccount(item.Key)
	kcItem.SetLabel(item.Label)
	kcItem.SetDescription(item.Description)
	kcItem.SetData(item.Data)
	kcItem.SetSynchronizable(gokeychain.SynchronizableNo)
	kcItem.SetAccessible(gokeychain.AccessibleWhenUnlocked)
	kcItem.UseKeychain(kc)
	kcItem.SetAccess(gokeychain.NoApplicationsTrusted)

	log.Printf("Adding service=%q, account=%q to osx keychain %s", k.service, item.Key, k.path)
	return gokeychain.AddItem(kcItem)
}

func (k *keychain) Remove(key string) error {
	kc := gokeychain.NewWithPath(k.path)

	if err := kc.Status(); err != nil {
		return err
	}

	item := gokeychain.NewItem()
	item.SetSecClass(gokeychain.SecClassGenericPassword)
	item.SetService(k.service)
	item.SetAccount(key)

	log.Printf("Removing keychain item service=%q, account=%q from osx keychain %q", k.service, key, k.path)
	return gokeychain.DeleteItem(item)
}

func (k *keychain) Keys() ([]string, error) {
	kc := gokeychain.NewWithPath(k.path)

	if err := kc.Status(); err != nil {
		return nil, err
	}

	query := gokeychain.NewItem()
{'fraction_non_alphanumeric': 0.13279515640766903, 'fraction_numerical': 0.0016145307769929364, 'mean_word_length': 4.416393442622951, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5773819', 'n_tokens_mistral': 1854, 'n_tokens_neox': 1783, 'n_words': 369}