<commit_before>package vault

import (
	"crypto/tls"
	"crypto/x509"
	"fmt"
	"net/http"
	"net/url"
	"time"

	"github.com/cloudfoundry-community/vaultkv"
)

type Client struct {
	Prefix string

	vault vaultkv.Client
	kv    *vaultkv.KV
}

type Credentials struct {
	SealKey   string `json:"seal_key"`
	RootToken string `json:"root_token"`
}

func Connect(uri, cacert string) (*Client, error) {
	pool := x509.NewCertPool()
	if cacert != "" {
		if ok := pool.AppendCertsFromPEM([]byte(cacert)); !ok {
			return nil, fmt.Errorf("Invalid or malformed CA Certificate")
		}
	}

	vaultURI, err := url.Parse(uri)
	if err != nil {
		return nil, fmt.Errorf("Invalid or malformed Vault URI '%s': %s", uri, err)
	}

	c := &Client{
		Prefix: "secret/secret",
		vault: vaultkv.Client{
			VaultURL: vaultURI,
			Client: &http.Client{
				Timeout: 30 * time.Second,
				Transport: &http.Transport{
					TLSClientConfig: &tls.Config{
						RootCAs: pool,
					},
				},
			},
		},
	}
	c.kv = c.vault.NewKV()

	return c, nil
}

type Status int

const (
	Unknown Status = iota
	Blank
	Locked
	Ready
)

func (c *Client) Status() (Status, error) {
	err := c.vault.TokenIsValid()
	if err != nil {
		return Locked, nil
	}
	err = c.vault.Health(false)
	if err == nil {
		return Ready, nil
	}
	if vaultkv.IsUninitialized(err) {
		return Blank, nil
	}
	if vaultkv.IsSealed(err) {
		return Locked, nil
	}
	return Unknown, err
}
func (c *Client) StatusString() (string, error) {
	st, err := c.Status()
	if err != nil {
		return "unknown", err
	}
	switch st {
	case Blank:
		return "uninitialized", nil
	case Locked:
		return "locked", nil
	case Ready:
		return "unlocked", nil
	}
	return "unknown", nil<commit_msg>fixed status sealed vs uninit priority<commit_after>package vault

import (
	"crypto/tls"
	"crypto/x509"
	"fmt"
	"net/http"
	"net/url"
	"time"

	"github.com/cloudfoundry-community/vaultkv"
)

type Client struct {
	Prefix string

	vault vaultkv.Client
	kv    *vaultkv.KV
}

type Credentials struct {
	SealKey   string `json:"seal_key"`
	RootToken string `json:"root_token"`
}

func Connect(uri, cacert string) (*Client, error) {
	pool := x509.NewCertPool()
	if cacert != "" {
		if ok := pool.AppendCertsFromPEM([]byte(cacert)); !ok {
			return nil, fmt.Errorf("Invalid or malformed CA Certificate")
		}
	}

	vaultURI, err := url.Parse(uri)
	if err != nil {
		return nil, fmt.Errorf("Invalid or malformed Vault URI '%s': %s", uri, err)
	}

	c := &Client{
		Prefix: "secret/secret",
		vault: vaultkv.Client{
			VaultURL: vaultURI,
			Client: &http.Client{
				Timeout: 30 * time.Second,
				Transport: &http.Transport{
					TLSClientConfig: &tls.Config{
						RootCAs: pool,
					},
				},
			},
		},
	}
	c.kv = c.vault.NewKV()

	return c, nil
}

type Status int

const (
	Unknown Status = iota
	Blank
	Locked
	Ready
)

func (c *Client) Status() (Status, error) {
	err := c.vault.Health(false)
	if err == nil {
		return Ready, nil
	}
	if vaultkv.IsUninitialized(err) {
		return Blank, nil
	}
	if vaultkv.IsSealed(err) {
		return Locked, nil
	}
	err = c.vault.TokenIsValid()
	if err != nil {
		return Locked, nil
	}
	return Unknown, err
}
func (c *Client) StatusString() (string, error) {
	st, err := c.Status()
	if err != nil {
		return "unknown", err
	}
	switch st {
	case Blank:
		return "uninitialized", nil
	case Locked:
		return "locked", nil
	case Ready:
		return "unlocked", nil
	}
	return "unknown", nil
{'fraction_non_alphanumeric': 0.1415511648481274, 'fraction_numerical': 0.004718372161604247, 'mean_word_length': 3.393782383419689, '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': '311247', 'n_tokens_mistral': 1423, 'n_tokens_neox': 1302, 'n_words': 344}