You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.3 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package cli
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "os"
  8. "path/filepath"
  9. )
  10. // WriteConfigVals writes a toml file with the given values.
  11. // It returns an error if writing was impossible.
  12. func WriteConfigVals(dir string, vals map[string]string) error {
  13. data := ""
  14. for k, v := range vals {
  15. data += fmt.Sprintf("%s = \"%s\"\n", k, v)
  16. }
  17. cfile := filepath.Join(dir, "config.toml")
  18. return ioutil.WriteFile(cfile, []byte(data), 0666)
  19. }
  20. // RunWithArgs executes the given command with the specified command line args
  21. // and environmental variables set. It returns any error returned from cmd.Execute()
  22. func RunWithArgs(cmd Executable, args []string, env map[string]string) error {
  23. oargs := os.Args
  24. oenv := map[string]string{}
  25. // defer returns the environment back to normal
  26. defer func() {
  27. os.Args = oargs
  28. for k, v := range oenv {
  29. os.Setenv(k, v)
  30. }
  31. }()
  32. // set the args and env how we want them
  33. os.Args = args
  34. for k, v := range env {
  35. // backup old value if there, to restore at end
  36. oenv[k] = os.Getenv(k)
  37. err := os.Setenv(k, v)
  38. if err != nil {
  39. return err
  40. }
  41. }
  42. // and finally run the command
  43. return cmd.Execute()
  44. }
  45. // RunCaptureWithArgs executes the given command with the specified command
  46. // line args and environmental variables set. It returns string fields
  47. // representing output written to stdout and stderr, additionally any error
  48. // from cmd.Execute() is also returned
  49. func RunCaptureWithArgs(cmd Executable, args []string, env map[string]string) (stdout, stderr string, err error) {
  50. oldout, olderr := os.Stdout, os.Stderr // keep backup of the real stdout
  51. rOut, wOut, _ := os.Pipe()
  52. rErr, wErr, _ := os.Pipe()
  53. os.Stdout, os.Stderr = wOut, wErr
  54. defer func() {
  55. os.Stdout, os.Stderr = oldout, olderr // restoring the real stdout
  56. }()
  57. // copy the output in a separate goroutine so printing can't block indefinitely
  58. copyStd := func(reader *os.File) *(chan string) {
  59. stdC := make(chan string)
  60. go func() {
  61. var buf bytes.Buffer
  62. // io.Copy will end when we call reader.Close() below
  63. io.Copy(&buf, reader)
  64. stdC <- buf.String()
  65. }()
  66. return &stdC
  67. }
  68. outC := copyStd(rOut)
  69. errC := copyStd(rErr)
  70. // now run the command
  71. err = RunWithArgs(cmd, args, env)
  72. // and grab the stdout to return
  73. wOut.Close()
  74. wErr.Close()
  75. stdout = <-*outC
  76. stderr = <-*errC
  77. return stdout, stderr, err
  78. }