2017-01-16 23:13:32 -05:00
|
|
|
package configenv
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
2017-04-22 15:58:03 -04:00
|
|
|
"io"
|
2017-01-16 23:13:32 -05:00
|
|
|
"path/filepath"
|
|
|
|
|
"sort"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2017-04-22 15:58:03 -04:00
|
|
|
"archive/tar"
|
|
|
|
|
|
2017-05-20 14:38:27 -04:00
|
|
|
"os"
|
|
|
|
|
|
|
|
|
|
godotenv "github.com/alexquick/godotenv"
|
2017-01-16 23:13:32 -05:00
|
|
|
common "github.com/dokku/dokku/plugins/common"
|
|
|
|
|
)
|
|
|
|
|
|
2017-07-16 19:50:50 -04:00
|
|
|
//ExportFormat types of possible exports
|
2017-05-20 14:38:27 -04:00
|
|
|
type ExportFormat int
|
|
|
|
|
|
|
|
|
|
const (
|
2017-07-16 19:50:50 -04:00
|
|
|
//Exports format: Sourceable exports
|
2017-05-20 14:38:27 -04:00
|
|
|
Exports ExportFormat = iota
|
2017-07-16 19:50:50 -04:00
|
|
|
//Envfile format: dotenv file
|
2017-05-20 14:38:27 -04:00
|
|
|
Envfile
|
2017-07-16 19:50:50 -04:00
|
|
|
//DockerArgs format: --env args for docker
|
2017-05-20 14:38:27 -04:00
|
|
|
DockerArgs
|
2017-07-16 19:50:50 -04:00
|
|
|
//Shell format: env arguments for shell
|
|
|
|
|
Shell
|
2017-05-20 14:38:27 -04:00
|
|
|
)
|
|
|
|
|
|
2017-01-16 23:13:32 -05:00
|
|
|
//Env is a representation for global or app environment
|
|
|
|
|
type Env struct {
|
2017-05-20 14:38:27 -04:00
|
|
|
name string
|
|
|
|
|
filename string
|
|
|
|
|
env map[string]string
|
2017-01-16 23:13:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *Env) String() string {
|
|
|
|
|
return e.EnvfileString()
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-16 19:50:50 -04:00
|
|
|
//Export the Env in the given format
|
2017-05-20 14:38:27 -04:00
|
|
|
func (e *Env) Export(format ExportFormat) string {
|
|
|
|
|
switch format {
|
|
|
|
|
case Exports:
|
|
|
|
|
return e.ExportfileString()
|
|
|
|
|
case Envfile:
|
|
|
|
|
return e.EnvfileString()
|
|
|
|
|
case DockerArgs:
|
|
|
|
|
return e.DockerArgsString()
|
2017-07-16 19:50:50 -04:00
|
|
|
case Shell:
|
|
|
|
|
return e.ShellString()
|
2017-05-20 14:38:27 -04:00
|
|
|
default:
|
|
|
|
|
common.LogFail(fmt.Sprintf("Unknown export format: %v", format))
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//EnvfileString returns the contents of this Env in dotenv format
|
2017-01-16 23:13:32 -05:00
|
|
|
func (e *Env) EnvfileString() string {
|
2017-05-20 14:38:27 -04:00
|
|
|
rep, _ := godotenv.WriteString(e.Map())
|
|
|
|
|
return rep
|
2017-01-16 23:13:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//ExportfileString returns the contents of this Env as bash exports
|
|
|
|
|
func (e *Env) ExportfileString() string {
|
2017-05-20 14:38:27 -04:00
|
|
|
return e.stringWithPrefixAndSeparator("export ", "\n", true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//DockerArgsString gets the contents of this Env in the form -env=KEY=VALUE --env...
|
|
|
|
|
func (e *Env) DockerArgsString() string {
|
|
|
|
|
return e.stringWithPrefixAndSeparator("--env=", " ", true)
|
2017-01-16 23:13:32 -05:00
|
|
|
}
|
|
|
|
|
|
2017-07-16 19:50:50 -04:00
|
|
|
//ShellString gets the contents of this Env in the form "KEY='value' KEY2='value'"
|
|
|
|
|
// for passing the environment in the shell
|
|
|
|
|
func (e *Env) ShellString() string {
|
|
|
|
|
return e.stringWithPrefixAndSeparator("", " ", true)
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-18 15:58:32 -05:00
|
|
|
//StringWithPrefixAndSeparator makes a string of the environment
|
|
|
|
|
// with the given prefix and separator for each entry
|
2017-05-20 14:38:27 -04:00
|
|
|
func (e *Env) stringWithPrefixAndSeparator(prefix string, separator string, allowNewlines bool) string {
|
2017-01-16 23:13:32 -05:00
|
|
|
keys := e.Keys()
|
|
|
|
|
entries := make([]string, len(keys))
|
|
|
|
|
for i, k := range keys {
|
2017-04-22 15:58:03 -04:00
|
|
|
v := SingleQuoteEscape(e.env[k])
|
2017-05-20 14:38:27 -04:00
|
|
|
if !allowNewlines {
|
2017-04-22 15:58:03 -04:00
|
|
|
v = strings.Replace(v, "\n", "'$'\\n''", -1)
|
|
|
|
|
}
|
|
|
|
|
entries[i] = fmt.Sprintf("%s%s='%s'", prefix, k, v)
|
2017-01-16 23:13:32 -05:00
|
|
|
}
|
2017-02-18 15:58:32 -05:00
|
|
|
return strings.Join(entries, separator)
|
2017-01-16 23:13:32 -05:00
|
|
|
}
|
|
|
|
|
|
2017-01-30 22:20:39 -05:00
|
|
|
//SingleQuoteEscape escapes the value as if it were shell-quoted in single quotes
|
|
|
|
|
func SingleQuoteEscape(value string) string { // so that 'esc'apped' -> 'esc'\''aped'
|
2017-01-19 00:02:56 -05:00
|
|
|
return strings.Replace(value, "'", "'\\''", -1)
|
2017-01-16 23:13:32 -05:00
|
|
|
}
|
|
|
|
|
|
2017-04-22 15:58:03 -04:00
|
|
|
//ExportBundle writes a tarfile of the environmnet to the given io.Writer.
|
|
|
|
|
// for every environment variable there is a file with the variable's key
|
|
|
|
|
// with its content set to the variable's value
|
|
|
|
|
func (e *Env) ExportBundle(dest io.Writer) error {
|
|
|
|
|
tarfile := tar.NewWriter(dest)
|
|
|
|
|
defer tarfile.Close()
|
|
|
|
|
|
|
|
|
|
for _, k := range e.Keys() {
|
|
|
|
|
val, _ := e.Get(k)
|
|
|
|
|
valbin := []byte(val)
|
|
|
|
|
|
|
|
|
|
header := &tar.Header{
|
|
|
|
|
Name: k,
|
|
|
|
|
Mode: 0600,
|
|
|
|
|
Size: int64(len(valbin)),
|
|
|
|
|
}
|
|
|
|
|
tarfile.WriteHeader(header)
|
|
|
|
|
tarfile.Write(valbin)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-18 15:58:32 -05:00
|
|
|
//LoadApp loads an environment for the given app
|
|
|
|
|
func LoadApp(appName string) (*Env, error) {
|
|
|
|
|
appfile, err := getAppFile(appName)
|
2017-01-16 23:13:32 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2017-05-20 14:38:27 -04:00
|
|
|
return loadFromFile(appName, appfile)
|
2017-02-18 15:58:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//LoadGlobal loads the global environmen
|
|
|
|
|
func LoadGlobal() (*Env, error) {
|
2017-05-20 14:38:27 -04:00
|
|
|
return loadFromFile("global", getGlobalFile())
|
2017-01-16 23:13:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//NewFromString creates an env from the given ENVFILE contents representation
|
|
|
|
|
func NewFromString(rep string) (*Env, error) {
|
2017-07-19 09:59:44 -04:00
|
|
|
envMap, err := godotenv.ReadString(rep)
|
2017-05-20 14:38:27 -04:00
|
|
|
env := &Env{
|
|
|
|
|
"<unknown>",
|
|
|
|
|
"",
|
|
|
|
|
envMap,
|
|
|
|
|
}
|
|
|
|
|
return env, err
|
|
|
|
|
}
|
2017-05-21 16:13:53 -04:00
|
|
|
func loadFromFile(name string, filename string) (env *Env, err error) {
|
|
|
|
|
envMap := make(map[string]string)
|
|
|
|
|
|
|
|
|
|
if _, err := os.Stat(filename); err == nil {
|
|
|
|
|
envMap, err = godotenv.Read(filename)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
env = &Env{
|
2017-05-20 14:38:27 -04:00
|
|
|
name,
|
|
|
|
|
filename,
|
|
|
|
|
envMap,
|
|
|
|
|
}
|
2017-05-21 16:13:53 -04:00
|
|
|
return
|
2017-01-16 23:13:32 -05:00
|
|
|
}
|
|
|
|
|
|
2017-02-18 15:58:32 -05:00
|
|
|
//Merge merges the given environment on top of the reciever
|
|
|
|
|
func (e *Env) Merge(other *Env) {
|
|
|
|
|
for _, k := range other.Keys() {
|
|
|
|
|
e.Set(k, other.GetDefault(k, ""))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-16 23:13:32 -05:00
|
|
|
//Set an environment variable
|
|
|
|
|
func (e *Env) Set(key string, value string) {
|
|
|
|
|
e.env[key] = value
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Unset an environment variable
|
|
|
|
|
func (e *Env) Unset(key string) {
|
|
|
|
|
delete(e.env, key)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Keys gets the keys in this environment
|
|
|
|
|
func (e *Env) Keys() []string {
|
|
|
|
|
keys := make([]string, 0, len(e.env))
|
|
|
|
|
for k := range e.env {
|
|
|
|
|
keys = append(keys, k)
|
|
|
|
|
}
|
2017-02-18 15:58:32 -05:00
|
|
|
sort.Strings(keys)
|
2017-01-16 23:13:32 -05:00
|
|
|
return keys
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Get an environment variable
|
|
|
|
|
func (e *Env) Get(key string) (string, bool) {
|
|
|
|
|
v, ok := e.env[key]
|
|
|
|
|
return v, ok
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//GetDefault an environment variable or a default if it doesnt exist
|
|
|
|
|
func (e *Env) GetDefault(key string, defaultValue string) string {
|
|
|
|
|
v, ok := e.env[key]
|
|
|
|
|
if !ok {
|
|
|
|
|
return defaultValue
|
|
|
|
|
}
|
|
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//GetBoolDefault gets the bool value of the given key with the given default
|
|
|
|
|
//right now that is evaluated as `value != "0"`
|
|
|
|
|
func (e *Env) GetBoolDefault(key string, defaultValue bool) bool {
|
|
|
|
|
v, ok := e.Get(key)
|
|
|
|
|
if !ok {
|
|
|
|
|
return defaultValue
|
|
|
|
|
}
|
|
|
|
|
return v != "0"
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-18 15:58:32 -05:00
|
|
|
//Len return the number of items in this environment
|
|
|
|
|
func (e *Env) Len() int {
|
|
|
|
|
return len(e.env)
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-16 23:13:32 -05:00
|
|
|
//Map return the Env as a map
|
|
|
|
|
func (e *Env) Map() map[string]string {
|
|
|
|
|
return e.env
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Write an Env back to the file it was read from as an exportfile
|
|
|
|
|
func (e *Env) Write() error {
|
|
|
|
|
if e.filename == "" {
|
|
|
|
|
return errors.New("this Env was created unbound to a file")
|
|
|
|
|
}
|
2017-05-20 14:38:27 -04:00
|
|
|
return godotenv.Write(e.Map(), e.filename)
|
2017-01-16 23:13:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getAppFile(appName string) (string, error) {
|
|
|
|
|
err := common.VerifyAppName(appName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
return filepath.Join(common.MustGetEnv("DOKKU_ROOT"), appName, "ENV"), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getGlobalFile() string {
|
|
|
|
|
return filepath.Join(common.MustGetEnv("DOKKU_ROOT"), "ENV")
|
|
|
|
|
}
|