<commit_before>package discovery

import (
	"errors"
	"fmt"
	"net/url"
	"os"
	"strings"

	kv "gopkg.in/Clever/kayvee-go.v2"
)

// m is a convenience type for using kv.
type m map[string]interface{}

const (
	templateVar = "SERVICE_%s_%s_%%s"
)

func getVar(envVar string) (string, error) {
	envVar = strings.ToUpper(envVar)
	envVar = strings.Replace(envVar, "-", "_", -1)
	val := os.Getenv(envVar)
	if val == "" {
		return "", errors.New(kv.FormatLog("discovery-go", kv.Error, "missing env var", m{
			"var": envVar,
		}))
	}
	return val, nil
}

// URL finds the specified URL for a service based off of the service's name and which
// interface you are accessing. Values are found in environment variables fitting the scheme:
// SERVICE_{SERVICE NAME}_{INTERFACE NAME}_{PROTO,HOST,PORT}.
func URL(service, name string) (string, error) {
	proto, err := Proto(service, name)
	if err != nil {
		return "", err
	}
	host, err := Host(service, name)
	if err != nil {
		return "", err
	}
	port, err := Port(service, name)
	if err != nil {
		return "", err
	}

	u := url.URL{
		Scheme: proto,
		Host:   fmt.Sprintf("%s:%s", host, port),
	}
	return u.String(), nil
}

// Proto finds the specified protocol for a service based off of the service's name and which
// interface you are accessing. Values are found in environment variables fitting the scheme:
// SERVICE_{SERVICE NAME}_{INTERFACE NAME}_PROTO.
func Proto(service, name string) (string, error) {
	template := fmt.Sprintf(templateVar, service, name)<commit_msg>more extendable approach on URL parsing<commit_after>package discovery

import (
	"errors"
	"fmt"
	"net/url"
	"os"
	"strings"

	kv "gopkg.in/Clever/kayvee-go.v2"
)

// m is a convenience type for using kv.
type m map[string]interface{}

const (
	templateVar = "SERVICE_%s_%s_%%s"
)

func getVar(envVar string) (string, error) {
	envVar = strings.ToUpper(envVar)
	envVar = strings.Replace(envVar, "-", "_", -1)
	val := os.Getenv(envVar)
	if val == "" {
		return "", errors.New(kv.FormatLog("discovery-go", kv.Error, "missing.env.var", m{
			"var": envVar,
		}))
	}
	return val, nil
}

// URL finds the specified URL for a service based off of the service's name and which
// interface you are accessing. Values are found in environment variables fitting the scheme:
// SERVICE_{SERVICE NAME}_{INTERFACE NAME}_{PROTO,HOST,PORT}.
func URL(service, name string) (string, error) {
	proto, err := Proto(service, name)
	if err != nil {
		return "", err
	}
	host, err := Host(service, name)
	if err != nil {
		return "", err
	}
	port, err := Port(service, name)
	if err != nil {
		return "", err
	}

	rawURL := fmt.Sprintf("%s://%s:%s", proto, host, port)
	u, err := url.Parse(rawURL)
	if err != nil {
		return "", errors.New(kv.FormatLog("discovery-go", kv.Error, "missing env var", m{
			"url":   rawURL,
			"error": fmt.Errorf("Failed to parse URL: %s", err.Error()),
		}))
	}
	return u.String(), nil
}

// Proto finds the specified protocol for a service based off of the service's name and which
// interface you are accessing. Values are found in environment variables fitting the scheme:
// SERVICE_{SERVICE NAME}_{INTERFACE NAME}_PROTO.
func Proto(service, name string) (string, error) {
	template := fmt.Sprintf(templateVar, service, name)
{'fraction_non_alphanumeric': 0.14724869351367967, 'fraction_numerical': 0.0012296341838303104, 'mean_word_length': 3.9755351681957185, 'pattern_counts': {'":': 4, '<': 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': '2070843', 'n_tokens_mistral': 1143, 'n_tokens_neox': 1093, 'n_words': 357}