2016-02-06 15:23:11 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-06-01 15:34:33 +00:00
|
|
|
"fmt"
|
2016-02-06 15:23:11 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
|
2017-03-21 13:54:59 +00:00
|
|
|
openssl "github.com/Luzifer/go-openssl"
|
2016-02-06 15:23:11 +00:00
|
|
|
"github.com/Luzifer/rconfig"
|
2018-06-01 15:34:33 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2016-02-06 15:23:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-06-01 15:34:33 +00:00
|
|
|
cfg = struct {
|
|
|
|
EnvFile string `flag:"env-file" default:".env" description:"Location of the environment file"`
|
|
|
|
Silent bool `flag:"q" default:"false" description:"Suppress informational messages from envrun (DEPRECATED, use --log-level=warn)"`
|
|
|
|
CleanEnv bool `flag:"clean" default:"false" description:"Do not pass current environment to child process"`
|
|
|
|
LogLevel string `flag:"log-level" default:"info" description:"Log level (debug, info, warn, error, fatal)"`
|
|
|
|
Password string `flag:"password,p" default:"" env:"PASSWORD" description:"Password to decrypt environment file"`
|
2018-06-01 15:37:47 +00:00
|
|
|
PasswordFile string `flag:"password-file" default:"" description:"Read encryption key from file"`
|
2018-06-01 15:34:33 +00:00
|
|
|
VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"`
|
2016-02-06 15:23:11 +00:00
|
|
|
}{}
|
2018-06-01 15:34:33 +00:00
|
|
|
|
|
|
|
version = "dev"
|
2016-02-06 15:23:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2018-06-01 15:34:33 +00:00
|
|
|
if err := rconfig.ParseAndValidate(&cfg); err != nil {
|
|
|
|
log.Fatalf("Unable to parse commandline options: %s", err)
|
|
|
|
}
|
2016-02-06 15:23:11 +00:00
|
|
|
|
2018-06-01 15:34:33 +00:00
|
|
|
if cfg.VersionAndExit {
|
|
|
|
fmt.Printf("envrun %s\n", version)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.Silent && cfg.LogLevel == "info" {
|
|
|
|
// Migration of deprecated flag
|
|
|
|
cfg.LogLevel = "warn"
|
|
|
|
}
|
|
|
|
|
|
|
|
if l, err := log.ParseLevel(cfg.LogLevel); err != nil {
|
|
|
|
log.WithError(err).Fatal("Unable to parse log level")
|
|
|
|
} else {
|
|
|
|
log.SetLevel(l)
|
2016-02-06 15:23:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func envListToMap(list []string) map[string]string {
|
|
|
|
out := map[string]string{}
|
|
|
|
for _, entry := range list {
|
2016-04-15 20:12:15 +00:00
|
|
|
if len(entry) == 0 || entry[0] == '#' {
|
2016-02-06 15:23:11 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.SplitN(entry, "=", 2)
|
|
|
|
out[parts[0]] = parts[1]
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func envMapToList(envMap map[string]string) []string {
|
|
|
|
out := []string{}
|
|
|
|
for k, v := range envMap {
|
|
|
|
out = append(out, k+"="+v)
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
body, err := ioutil.ReadFile(cfg.EnvFile)
|
|
|
|
if err != nil {
|
2018-06-01 15:34:33 +00:00
|
|
|
log.WithError(err).Fatal("Could not read env-file")
|
2016-02-06 15:23:11 +00:00
|
|
|
}
|
|
|
|
|
2018-06-01 15:37:47 +00:00
|
|
|
if cfg.Password == "" && cfg.PasswordFile != "" {
|
|
|
|
if _, err := os.Stat(cfg.PasswordFile); err == nil {
|
|
|
|
data, err := ioutil.ReadFile(cfg.PasswordFile)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Fatal("Unable to read password from file")
|
|
|
|
}
|
|
|
|
cfg.Password = string(data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-21 13:54:59 +00:00
|
|
|
if cfg.Password != "" {
|
|
|
|
if body, err = openssl.New().DecryptString(cfg.Password, string(body)); err != nil {
|
2018-06-01 15:34:33 +00:00
|
|
|
log.WithError(err).Fatal("Could not decrypt env-file")
|
2017-03-21 13:54:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-01 15:37:47 +00:00
|
|
|
var childenv = envListToMap(os.Environ())
|
2016-02-06 15:23:11 +00:00
|
|
|
if cfg.CleanEnv {
|
|
|
|
childenv = map[string]string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
pairs := envListToMap(strings.Split(string(body), "\n"))
|
|
|
|
for k, v := range pairs {
|
|
|
|
childenv[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
c := exec.Command(rconfig.Args()[1], rconfig.Args()[2:]...)
|
|
|
|
c.Env = envMapToList(childenv)
|
|
|
|
c.Stdout = os.Stdout
|
|
|
|
c.Stderr = os.Stderr
|
|
|
|
c.Stdin = os.Stdin
|
|
|
|
|
|
|
|
err = c.Run()
|
|
|
|
|
|
|
|
switch err.(type) {
|
|
|
|
case nil:
|
2018-06-01 15:34:33 +00:00
|
|
|
log.Info("Process exitted with code 0")
|
2016-02-06 15:23:11 +00:00
|
|
|
os.Exit(0)
|
|
|
|
case *exec.ExitError:
|
2018-06-01 15:34:33 +00:00
|
|
|
log.Error("Unclean exit with exit-code != 0")
|
2016-02-06 15:23:11 +00:00
|
|
|
os.Exit(1)
|
|
|
|
default:
|
2018-06-01 15:34:33 +00:00
|
|
|
log.WithError(err).Error("An unknown error ocurred")
|
2016-02-06 15:23:11 +00:00
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
}
|