<commit_before>// Package Env allows developers to use the
// [consul](https://github.com/hashicorp/consul) api
// as a storage for environment variables.
//
// At the production level this package abstracts out the
// consul.KV() api and at the dev level it abstracts out
// the core `os` package.
package env // import "github.com/kyani-inc/consul/env"

import (
	"fmt"
	"os"
	"strings"

	consul "github.com/hashicorp/consul/api"
)

// DefaultConfig() abstracts out the api.DefaultConfig call
// entirely. This is so in default usecases you will
// only ever need to import this one package.
// Obviously if you require more power you have the ability to import
// github.com/hashicorp/consul/api and configure your client as needed.
func DefaultConfig() *consul.Config {
	return consul.DefaultConfig()
}

type Env struct {
	namespace string
	kv        *consul.KV
}

// New is used to return a new instance of a KV() consul client.
// It is provided a config to create the client.
// No default namespace is used.
func New(config *consul.Config) (Env, error) {
	client, err := consul.NewClient(config)
	if err != nil {
		return Env{}, err
	}

	return Env{
		kv: client.KV(),
	}, nil
}

// osCleanKey is designed to clean a key for fallback purposes
func (env Env) osCleanKey(key string) string {
	// key shouldn't have the namespace but we'll remove it just in case
	key = strings.Replace(key, env.namespace, "", -1)

	// Convert '/' to '.'
	key = strings.Replace(key, "/", ".", -1)

	// Trim "/"
	key = strings.Trim(key, "/")
	return key
}

func (env Env) Get(key string) string {
	key = env.Namespace() + key

	pair, _, err := env.kv.Get(key, &consul.QueryOptions{
		// AllowStale: true is a set as an optimization technique,
		// allowing for us to query the consul agent and potentially
		// receive stale data.
		AllowStale: true,<commit_msg>Can't use '.' instead use '___'<commit_after>// Package Env allows developers to use the
// [consul](https://github.com/hashicorp/consul) api
// as a storage for environment variables.
//
// At the production level this package abstracts out the
// consul.KV() api and at the dev level it abstracts out
// the core `os` package.
package env // import "github.com/kyani-inc/consul/env"

import (
	"fmt"
	"os"
	"strings"

	consul "github.com/hashicorp/consul/api"
)

// DefaultConfig() abstracts out the api.DefaultConfig call
// entirely. This is so in default usecases you will
// only ever need to import this one package.
// Obviously if you require more power you have the ability to import
// github.com/hashicorp/consul/api and configure your client as needed.
func DefaultConfig() *consul.Config {
	return consul.DefaultConfig()
}

type Env struct {
	namespace string
	kv        *consul.KV
}

// New is used to return a new instance of a KV() consul client.
// It is provided a config to create the client.
// No default namespace is used.
func New(config *consul.Config) (Env, error) {
	client, err := consul.NewClient(config)
	if err != nil {
		return Env{}, err
	}

	return Env{
		kv: client.KV(),
	}, nil
}

// osCleanKey is designed to clean a key for fallback purposes
func (env Env) osCleanKey(key string) string {
	// key shouldn't have the namespace but we'll remove it just in case
	key = strings.Replace(key, env.namespace, "", -1)

	// Convert '/' to '___'
	key = strings.Replace(key, "/", "___", -1)

	// Trim "/"
	key = strings.Trim(key, "/")
	return key
}

func (env Env) Get(key string) string {
	key = env.Namespace() + key

	pair, _, err := env.kv.Get(key, &consul.QueryOptions{
		// AllowStale: true is a set as an optimization technique,
		// allowing for us to query the consul agent and potentially
		// receive stale data.
		AllowStale: true,
{'fraction_non_alphanumeric': 0.11989247311827957, 'fraction_numerical': 0.001075268817204301, 'mean_word_length': 3.331781140861467, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '2294272', 'n_tokens_mistral': 1201, 'n_tokens_neox': 1169, 'n_words': 459}