command.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package git
  5. import (
  6. "bytes"
  7. "fmt"
  8. "io"
  9. "os"
  10. "os/exec"
  11. "strings"
  12. "time"
  13. )
  14. // Command represents a command with its subcommands or arguments.
  15. type Command struct {
  16. name string
  17. args []string
  18. envs []string
  19. }
  20. func (c *Command) String() string {
  21. if len(c.args) == 0 {
  22. return c.name
  23. }
  24. return fmt.Sprintf("%s %s", c.name, strings.Join(c.args, " "))
  25. }
  26. // NewCommand creates and returns a new Git Command based on given command and arguments.
  27. func NewCommand(args ...string) *Command {
  28. return &Command{
  29. name: "git",
  30. args: args,
  31. }
  32. }
  33. // AddArguments adds new argument(s) to the command.
  34. func (c *Command) AddArguments(args ...string) *Command {
  35. c.args = append(c.args, args...)
  36. return c
  37. }
  38. // AddEnvs adds new environment variables to the command.
  39. func (c *Command) AddEnvs(envs ...string) *Command {
  40. c.envs = append(c.envs, envs...)
  41. return c
  42. }
  43. const DEFAULT_TIMEOUT = 60 * time.Second
  44. // RunInDirTimeoutPipeline executes the command in given directory with given timeout,
  45. // it pipes stdout and stderr to given io.Writer.
  46. func (c *Command) RunInDirTimeoutPipeline(timeout time.Duration, dir string, stdout, stderr io.Writer) error {
  47. if timeout == -1 {
  48. timeout = DEFAULT_TIMEOUT
  49. }
  50. if len(dir) == 0 {
  51. log(c.String())
  52. } else {
  53. log("%s: %v", dir, c)
  54. }
  55. cmd := exec.Command(c.name, c.args...)
  56. if c.envs != nil {
  57. cmd.Env = append(os.Environ(), c.envs...)
  58. }
  59. cmd.Dir = dir
  60. cmd.Stdout = stdout
  61. cmd.Stderr = stderr
  62. if err := cmd.Start(); err != nil {
  63. return err
  64. }
  65. done := make(chan error)
  66. go func() {
  67. done <- cmd.Wait()
  68. }()
  69. var err error
  70. select {
  71. case <-time.After(timeout):
  72. if cmd.Process != nil && cmd.ProcessState != nil && !cmd.ProcessState.Exited() {
  73. if err := cmd.Process.Kill(); err != nil {
  74. return fmt.Errorf("fail to kill process: %v", err)
  75. }
  76. }
  77. <-done
  78. return ErrExecTimeout{timeout}
  79. case err = <-done:
  80. }
  81. return err
  82. }
  83. // RunInDirTimeout executes the command in given directory with given timeout,
  84. // and returns stdout in []byte and error (combined with stderr).
  85. func (c *Command) RunInDirTimeout(timeout time.Duration, dir string) ([]byte, error) {
  86. stdout := new(bytes.Buffer)
  87. stderr := new(bytes.Buffer)
  88. if err := c.RunInDirTimeoutPipeline(timeout, dir, stdout, stderr); err != nil {
  89. return nil, concatenateError(err, stderr.String())
  90. }
  91. if stdout.Len() > 0 {
  92. log("stdout:\n%s", stdout.Bytes()[:1024])
  93. }
  94. return stdout.Bytes(), nil
  95. }
  96. // RunInDirPipeline executes the command in given directory,
  97. // it pipes stdout and stderr to given io.Writer.
  98. func (c *Command) RunInDirPipeline(dir string, stdout, stderr io.Writer) error {
  99. return c.RunInDirTimeoutPipeline(-1, dir, stdout, stderr)
  100. }
  101. // RunInDir executes the command in given directory
  102. // and returns stdout in []byte and error (combined with stderr).
  103. func (c *Command) RunInDirBytes(dir string) ([]byte, error) {
  104. return c.RunInDirTimeout(-1, dir)
  105. }
  106. // RunInDir executes the command in given directory
  107. // and returns stdout in string and error (combined with stderr).
  108. func (c *Command) RunInDir(dir string) (string, error) {
  109. stdout, err := c.RunInDirTimeout(-1, dir)
  110. if err != nil {
  111. return "", err
  112. }
  113. return string(stdout), nil
  114. }
  115. // RunTimeout executes the command in defualt working directory with given timeout,
  116. // and returns stdout in string and error (combined with stderr).
  117. func (c *Command) RunTimeout(timeout time.Duration) (string, error) {
  118. stdout, err := c.RunInDirTimeout(timeout, "")
  119. if err != nil {
  120. return "", err
  121. }
  122. return string(stdout), nil
  123. }
  124. // Run executes the command in defualt working directory
  125. // and returns stdout in string and error (combined with stderr).
  126. func (c *Command) Run() (string, error) {
  127. return c.RunTimeout(-1)
  128. }