env.go 727 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package control
  2. import (
  3. "log"
  4. "os"
  5. "os/exec"
  6. "syscall"
  7. "github.com/rancher/os/config"
  8. "github.com/rancher/os/pkg/util"
  9. "github.com/codegangsta/cli"
  10. )
  11. func envAction(c *cli.Context) error {
  12. cfg := config.LoadConfig()
  13. args := c.Args()
  14. if len(args) == 0 {
  15. return nil
  16. }
  17. osEnv := os.Environ()
  18. envMap := make(map[string]string, len(cfg.Rancher.Environment)+len(osEnv))
  19. for k, v := range cfg.Rancher.Environment {
  20. envMap[k] = v
  21. }
  22. for k, v := range util.KVPairs2Map(osEnv) {
  23. envMap[k] = v
  24. }
  25. if cmd, err := exec.LookPath(args[0]); err != nil {
  26. log.Fatal(err)
  27. } else {
  28. args[0] = cmd
  29. }
  30. if err := syscall.Exec(args[0], args, util.Map2KVPairs(envMap)); err != nil {
  31. log.Fatal(err)
  32. }
  33. return nil
  34. }