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.

cmd.go 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // +build go1.3
  2. // Copyright 2013 com authors
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  5. // not use this file except in compliance with the License. You may obtain
  6. // a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. // License for the specific language governing permissions and limitations
  14. // under the License.
  15. // Package com is an open source project for commonly used functions for the Go programming language.
  16. package com
  17. import (
  18. "bytes"
  19. "fmt"
  20. "os/exec"
  21. "runtime"
  22. "strings"
  23. )
  24. // ExecCmdDirBytes executes system command in given directory
  25. // and return stdout, stderr in bytes type, along with possible error.
  26. func ExecCmdDirBytes(dir, cmdName string, args ...string) ([]byte, []byte, error) {
  27. bufOut := new(bytes.Buffer)
  28. bufErr := new(bytes.Buffer)
  29. cmd := exec.Command(cmdName, args...)
  30. cmd.Dir = dir
  31. cmd.Stdout = bufOut
  32. cmd.Stderr = bufErr
  33. err := cmd.Run()
  34. return bufOut.Bytes(), bufErr.Bytes(), err
  35. }
  36. // ExecCmdBytes executes system command
  37. // and return stdout, stderr in bytes type, along with possible error.
  38. func ExecCmdBytes(cmdName string, args ...string) ([]byte, []byte, error) {
  39. return ExecCmdDirBytes("", cmdName, args...)
  40. }
  41. // ExecCmdDir executes system command in given directory
  42. // and return stdout, stderr in string type, along with possible error.
  43. func ExecCmdDir(dir, cmdName string, args ...string) (string, string, error) {
  44. bufOut, bufErr, err := ExecCmdDirBytes(dir, cmdName, args...)
  45. return string(bufOut), string(bufErr), err
  46. }
  47. // ExecCmd executes system command
  48. // and return stdout, stderr in string type, along with possible error.
  49. func ExecCmd(cmdName string, args ...string) (string, string, error) {
  50. return ExecCmdDir("", cmdName, args...)
  51. }
  52. // _________ .__ .____
  53. // \_ ___ \ ____ | | ___________ | | ____ ____
  54. // / \ \/ / _ \| | / _ \_ __ \ | | / _ \ / ___\
  55. // \ \___( <_> ) |_( <_> ) | \/ | |__( <_> ) /_/ >
  56. // \______ /\____/|____/\____/|__| |_______ \____/\___ /
  57. // \/ \/ /_____/
  58. // Color number constants.
  59. const (
  60. Gray = uint8(iota + 90)
  61. Red
  62. Green
  63. Yellow
  64. Blue
  65. Magenta
  66. //NRed = uint8(31) // Normal
  67. EndColor = "\033[0m"
  68. )
  69. // getColorLevel returns colored level string by given level.
  70. func getColorLevel(level string) string {
  71. level = strings.ToUpper(level)
  72. switch level {
  73. case "TRAC":
  74. return fmt.Sprintf("\033[%dm%s\033[0m", Blue, level)
  75. case "ERRO":
  76. return fmt.Sprintf("\033[%dm%s\033[0m", Red, level)
  77. case "WARN":
  78. return fmt.Sprintf("\033[%dm%s\033[0m", Magenta, level)
  79. case "SUCC":
  80. return fmt.Sprintf("\033[%dm%s\033[0m", Green, level)
  81. default:
  82. return level
  83. }
  84. }
  85. // ColorLogS colors log and return colored content.
  86. // Log format: <level> <content [highlight][path]> [ error ].
  87. // Level: TRAC -> blue; ERRO -> red; WARN -> Magenta; SUCC -> green; others -> default.
  88. // Content: default; path: yellow; error -> red.
  89. // Level has to be surrounded by "[" and "]".
  90. // Highlights have to be surrounded by "# " and " #"(space), "#" will be deleted.
  91. // Paths have to be surrounded by "( " and " )"(space).
  92. // Errors have to be surrounded by "[ " and " ]"(space).
  93. // Note: it hasn't support windows yet, contribute is welcome.
  94. func ColorLogS(format string, a ...interface{}) string {
  95. log := fmt.Sprintf(format, a...)
  96. var clog string
  97. if runtime.GOOS != "windows" {
  98. // Level.
  99. i := strings.Index(log, "]")
  100. if log[0] == '[' && i > -1 {
  101. clog += "[" + getColorLevel(log[1:i]) + "]"
  102. }
  103. log = log[i+1:]
  104. // Error.
  105. log = strings.Replace(log, "[ ", fmt.Sprintf("[\033[%dm", Red), -1)
  106. log = strings.Replace(log, " ]", EndColor+"]", -1)
  107. // Path.
  108. log = strings.Replace(log, "( ", fmt.Sprintf("(\033[%dm", Yellow), -1)
  109. log = strings.Replace(log, " )", EndColor+")", -1)
  110. // Highlights.
  111. log = strings.Replace(log, "# ", fmt.Sprintf("\033[%dm", Gray), -1)
  112. log = strings.Replace(log, " #", EndColor, -1)
  113. } else {
  114. // Level.
  115. i := strings.Index(log, "]")
  116. if log[0] == '[' && i > -1 {
  117. clog += "[" + log[1:i] + "]"
  118. }
  119. log = log[i+1:]
  120. // Error.
  121. log = strings.Replace(log, "[ ", "[", -1)
  122. log = strings.Replace(log, " ]", "]", -1)
  123. // Path.
  124. log = strings.Replace(log, "( ", "(", -1)
  125. log = strings.Replace(log, " )", ")", -1)
  126. // Highlights.
  127. log = strings.Replace(log, "# ", "", -1)
  128. log = strings.Replace(log, " #", "", -1)
  129. }
  130. return clog + log
  131. }
  132. // ColorLog prints colored log to stdout.
  133. // See color rules in function 'ColorLogS'.
  134. func ColorLog(format string, a ...interface{}) {
  135. fmt.Print(ColorLogS(format, a...))
  136. }