package discovery

import (
	"bytes"
	"io/ioutil"

	"github.com/Masterminds/cookoo"
)

// TokenFile for etcd
var TokenFile = "/var/run/secrets/drycc/etcd/discovery/token"

// ClusterDiscoveryURL for drycc
const ClusterDiscoveryURL = "http://%s:%s/v2/keys/drycc/discovery/%s"

// ClusterSizeKey for discovery
const ClusterSizeKey = "drycc/discovery/%s/_config/size"

// ClusterStatusKey for drycc
const ClusterStatusKey = "drycc/status/%s/%s"

// Token reads the discovery token from the TokenFile and returns it.
func Token() ([]byte, error) {
	data, err := ioutil.ReadFile(TokenFile)
	if err != nil {
		return data, err
	}
	data = bytes.TrimSpace(data)
	return data, nil
}

// GetToken is a command to get a token.
//
// This is a convenience for calling Token in a route.
func GetToken(c cookoo.Context, p *cookoo.Params) (interface{}, cookoo.Interrupt) {
	t, err := Token()
	if err != nil {
		return "", err
	}
	return string(t), err
}
