command.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. // NewCommand creates and returns a new Git Command based on given command and arguments.
  34. func NewACommand(args ...string) *Command {
  35. return &Command{
  36. name: "git-annex",
  37. args: args,
  38. }
  39. }
  40. // AddArguments adds new argument(s) to the command.
  41. func (c *Command) AddArguments(args ...string) *Command {
  42. c.args = append(c.args, args...)
  43. return c
  44. }
  45. // AddEnvs adds new environment variables to the command.
  46. func (c *Command) AddEnvs(envs ...string) *Command {
  47. c.envs = append(c.envs, envs...)
  48. return c
  49. }
  50. const DEFAULT_TIMEOUT = 60 * time.Second
  51. // RunInDirTimeoutPipeline executes the command in given directory with given timeout,
  52. // it pipes stdout and stderr to given io.Writer.
  53. func (c *Command) RunInDirTimeoutPipeline(timeout time.Duration, dir string, stdout, stderr io.Writer) error {
  54. if timeout == -1 {
  55. timeout = DEFAULT_TIMEOUT
  56. }
  57. if len(dir) == 0 {
  58. log(c.String())
  59. } else {
  60. log("%s: %v", dir, c)
  61. }
  62. cmd := exec.Command(c.name, c.args...)
  63. if c.envs != nil {
  64. cmd.Env = append(os.Environ(), c.envs...)
  65. }
  66. cmd.Dir = dir
  67. cmd.Stdout = stdout
  68. cmd.Stderr = stderr
  69. if err := cmd.Start(); err != nil {
  70. return err
  71. }
  72. done := make(chan error)
  73. go func() {
  74. done <- cmd.Wait()
  75. }()
  76. var err error
  77. select {
  78. case <-time.After(timeout):
  79. if cmd.Process != nil && cmd.ProcessState != nil && !cmd.ProcessState.Exited() {
  80. if err := cmd.Process.Kill(); err != nil {
  81. return fmt.Errorf("fail to kill process: %v", err)
  82. }
  83. }
  84. <-done
  85. return ErrExecTimeout{timeout}
  86. case err = <-done:
  87. }
  88. return err
  89. }
  90. // RunInDirTimeout executes the command in given directory with given timeout,
  91. // and returns stdout in []byte and error (combined with stderr).
  92. func (c *Command) RunInDirTimeout(timeout time.Duration, dir string) ([]byte, error) {
  93. stdout := new(bytes.Buffer)
  94. stderr := new(bytes.Buffer)
  95. if err := c.RunInDirTimeoutPipeline(timeout, dir, stdout, stderr); err != nil {
  96. return nil, concatenateError(err, stderr.String())
  97. }
  98. if stdout.Len() > 0 {
  99. log("stdout:\n%s", stdout.Bytes()[:1024])
  100. }
  101. return stdout.Bytes(), nil
  102. }
  103. // RunInDirPipeline executes the command in given directory,
  104. // it pipes stdout and stderr to given io.Writer.
  105. func (c *Command) RunInDirPipeline(dir string, stdout, stderr io.Writer) error {
  106. return c.RunInDirTimeoutPipeline(-1, dir, stdout, stderr)
  107. }
  108. // RunInDirBytes executes the command in given directory
  109. // and returns stdout in []byte and error (combined with stderr).
  110. func (c *Command) RunInDirBytes(dir string) ([]byte, error) {
  111. return c.RunInDirTimeout(-1, dir)
  112. }
  113. // RunInDir executes the command in given directory
  114. // and returns stdout in string and error (combined with stderr).
  115. func (c *Command) RunInDir(dir string) (string, error) {
  116. stdout, err := c.RunInDirTimeout(-1, dir)
  117. if err != nil {
  118. return "", err
  119. }
  120. return string(stdout), nil
  121. }
  122. // RunTimeout executes the command in defualt working directory with given timeout,
  123. // and returns stdout in string and error (combined with stderr).
  124. func (c *Command) RunTimeout(timeout time.Duration) (string, error) {
  125. stdout, err := c.RunInDirTimeout(timeout, "")
  126. if err != nil {
  127. return "", err
  128. }
  129. return string(stdout), nil
  130. }
  131. // Run executes the command in defualt working directory
  132. // and returns stdout in string and error (combined with stderr).
  133. func (c *Command) Run() (string, error) {
  134. return c.RunTimeout(-1)
  135. }