<commit_before>package command

import (
	"crypto/tls"
	"fmt"
	"net/http"
	"strings"

	consulapi "github.com/hashicorp/consul/api"
	flag "github.com/ogier/pflag"
	"github.com/mitchellh/cli"
)

type Meta struct {
	UI		cli.Ui
	consulAddr	string
	sslEnabled	bool
	sslVerify	bool
	sslCert		string
	sslCaCert	string
	token		string
	auth		*Auth
	dc		string
}

func (m *Meta) FlagSet() *flag.FlagSet {
	f := flag.NewFlagSet("consul-cli", flag.ContinueOnError)
	f.StringVar(&m.consulAddr, "consul", "127.0.0.1:8500", "")
	f.BoolVar(&m.sslEnabled, "ssl", false, "")
	f.BoolVar(&m.sslVerify, "ssl-verify", true, "")
	f.StringVar(&m.sslCert, "ssl-cert", "", "")
	f.StringVar(&m.sslCaCert, "ssl-ca-cert", "", "")
	f.StringVar(&m.token, "token", "", "")
	f.StringVar(&m.dc, "datacenter", "", "")

	m.auth = new(Auth)
	f.Var((*Auth)(m.auth), "auth", "")

	return f
}

func (m *Meta) Client() (*consulapi.Client, error) {
	config := consulapi.DefaultConfig()
	config.Address = m.consulAddr

	if m.token != "" {
		config.Token = m.token
	}

	if m.sslEnabled {
		config.Scheme = "https"
	}

	if !m.sslVerify {
		config.HttpClient.Transport = &http.Transport{
			TLSClientConfig: &tls.Config{
				InsecureSkipVerify: true,
			},
		}
	}

	if m.auth.Enabled {
		config.HttpAuth = &consulapi.HttpBasicAuth{
			Username: m.auth.Username,
			Password: <PASSWORD>,
		}
	}

	client, err := consulapi.NewClient(config)
	if err != nil {
		return nil, err
	}
	return client, nil
}

func (m *Meta) WriteOptions() *consulapi.WriteOptions {
	writeOpts := new(consulapi.WriteOptions)
	if m.token != "" {
		writeOpts.Token = m.token
	}

	if m.dc != "" {
		writeOpts.Datacenter = m.dc
	}

	return writeOpts
}

func (m *Meta) QueryOptions() *consulapi.QueryOptions {
	queryOpts := new(consulapi.QueryOptions)
	if m.token != "" {
		queryOpts.Token = m.token
	}

	if m.dc != "" {
		queryOpts.Datacenter = m.dc
	}

	return queryOpts
}

func (m *Meta) ConsulHelp() string {
	helpText := `
  --consul=127.0.0.1:8500	HTTP address of the Consul Agent
  --ssl				Use HTTPS when talking to Consul
				(default: false)
  --ssl-verify			Verify certificates when connecting via SSL
				(default: true)
  --ssl-cert			Path to an SSL certificate to use to authenticate
				to the Consul server
				(default: not set)
  --ssl-ca-cert			Path to an SSL client certificate to use to authenticate
				to the Consul server<commit_msg>Add `--auth` option to the command line usage<commit_after>package command

import (
	"crypto/tls"
	"fmt"
	"net/http"
	"strings"

	consulapi "github.com/hashicorp/consul/api"
	flag "github.com/ogier/pflag"
	"github.com/mitchellh/cli"
)

type Meta struct {
	UI		cli.Ui
	consulAddr	string
	sslEnabled	bool
	sslVerify	bool
	sslCert		string
	sslCaCert	string
	token		string
	auth		*Auth
	dc		string
}

func (m *Meta) FlagSet() *flag.FlagSet {
	f := flag.NewFlagSet("consul-cli", flag.ContinueOnError)
	f.StringVar(&m.consulAddr, "consul", "127.0.0.1:8500", "")
	f.BoolVar(&m.sslEnabled, "ssl", false, "")
	f.BoolVar(&m.sslVerify, "ssl-verify", true, "")
	f.StringVar(&m.sslCert, "ssl-cert", "", "")
	f.StringVar(&m.sslCaCert, "ssl-ca-cert", "", "")
	f.StringVar(&m.token, "token", "", "")
	f.StringVar(&m.dc, "datacenter", "", "")

	m.auth = new(Auth)
	f.Var((*Auth)(m.auth), "auth", "")

	return f
}

func (m *Meta) Client() (*consulapi.Client, error) {
	config := consulapi.DefaultConfig()
	config.Address = m.consulAddr

	if m.token != "" {
		config.Token = m.token
	}

	if m.sslEnabled {
		config.Scheme = "https"
	}

	if !m.sslVerify {
		config.HttpClient.Transport = &http.Transport{
			TLSClientConfig: &tls.Config{
				InsecureSkipVerify: true,
			},
		}
	}

	if m.auth.Enabled {
		config.HttpAuth = &consulapi.HttpBasicAuth{
			Username: m.auth.Username,
			Password: <PASSWORD>,
		}
	}

	client, err := consulapi.NewClient(config)
	if err != nil {
		return nil, err
	}
	return client, nil
}

func (m *Meta) WriteOptions() *consulapi.WriteOptions {
	writeOpts := new(consulapi.WriteOptions)
	if m.token != "" {
		writeOpts.Token = m.token
	}

	if m.dc != "" {
		writeOpts.Datacenter = m.dc
	}

	return writeOpts
}

func (m *Meta) QueryOptions() *consulapi.QueryOptions {
	queryOpts := new(consulapi.QueryOptions)
	if m.token != "" {
		queryOpts.Token = m.token
	}

	if m.dc != "" {
		queryOpts.Datacenter = m.dc
	}

	return queryOpts
}

func (m *Meta) ConsulHelp() string {
	helpText := `
  --consul=127.0.0.1:8500	HTTP address of the Consul Agent
  --ssl				Use HTTPS when talking to Consul
				(default: false)
  --ssl-verify			Verify certificates when connecting via SSL
				(default: true)
  --ssl-cert			Path to an SSL client certificate to use to authenticate
				to the consul server.
				(default: not set)
  --ssl-ca-cert			Path to a CA certificate file, containing one or more CA
				certificates to use to validate the certificate sent
				by the consul server to us.
				(default: not set)
  --auth			The basic authentication username (and optional password),
				separated by a colon.
{'fraction_non_alphanumeric': 0.15017064846416384, 'fraction_numerical': 0.008030515960650473, 'mean_word_length': 3.4561717352415027, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '6090799', 'n_tokens_mistral': 2024, 'n_tokens_neox': 1903, 'n_words': 447}