streamformatter.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package utils
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. )
  7. type StreamFormatter struct {
  8. json bool
  9. }
  10. func NewStreamFormatter(json bool) *StreamFormatter {
  11. return &StreamFormatter{json}
  12. }
  13. const streamNewline = "\r\n"
  14. var streamNewlineBytes = []byte(streamNewline)
  15. func (sf *StreamFormatter) FormatStream(str string) []byte {
  16. if sf.json {
  17. b, err := json.Marshal(&JSONMessage{Stream: str})
  18. if err != nil {
  19. return sf.FormatError(err)
  20. }
  21. return append(b, streamNewlineBytes...)
  22. }
  23. return []byte(str + "\r")
  24. }
  25. func (sf *StreamFormatter) FormatStatus(id, format string, a ...interface{}) []byte {
  26. str := fmt.Sprintf(format, a...)
  27. if sf.json {
  28. b, err := json.Marshal(&JSONMessage{ID: id, Status: str})
  29. if err != nil {
  30. return sf.FormatError(err)
  31. }
  32. return append(b, streamNewlineBytes...)
  33. }
  34. return []byte(str + streamNewline)
  35. }
  36. func (sf *StreamFormatter) FormatError(err error) []byte {
  37. if sf.json {
  38. jsonError, ok := err.(*JSONError)
  39. if !ok {
  40. jsonError = &JSONError{Message: err.Error()}
  41. }
  42. if b, err := json.Marshal(&JSONMessage{Error: jsonError, ErrorMessage: err.Error()}); err == nil {
  43. return append(b, streamNewlineBytes...)
  44. }
  45. return []byte("{\"error\":\"format error\"}" + streamNewline)
  46. }
  47. return []byte("Error: " + err.Error() + streamNewline)
  48. }
  49. func (sf *StreamFormatter) FormatProgress(id, action string, progress *JSONProgress) []byte {
  50. if progress == nil {
  51. progress = &JSONProgress{}
  52. }
  53. if sf.json {
  54. b, err := json.Marshal(&JSONMessage{
  55. Status: action,
  56. ProgressMessage: progress.String(),
  57. Progress: progress,
  58. ID: id,
  59. })
  60. if err != nil {
  61. return nil
  62. }
  63. return b
  64. }
  65. endl := "\r"
  66. if progress.String() == "" {
  67. endl += "\n"
  68. }
  69. return []byte(action + " " + progress.String() + endl)
  70. }
  71. func (sf *StreamFormatter) Json() bool {
  72. return sf.json
  73. }
  74. type StdoutFormater struct {
  75. io.Writer
  76. *StreamFormatter
  77. }
  78. func (sf *StdoutFormater) Write(buf []byte) (int, error) {
  79. formattedBuf := sf.StreamFormatter.FormatStream(string(buf))
  80. n, err := sf.Writer.Write(formattedBuf)
  81. if n != len(formattedBuf) {
  82. return n, io.ErrShortWrite
  83. }
  84. return len(buf), err
  85. }
  86. type StderrFormater struct {
  87. io.Writer
  88. *StreamFormatter
  89. }
  90. func (sf *StderrFormater) Write(buf []byte) (int, error) {
  91. formattedBuf := sf.StreamFormatter.FormatStream("\033[91m" + string(buf) + "\033[0m")
  92. n, err := sf.Writer.Write(formattedBuf)
  93. if n != len(formattedBuf) {
  94. return n, io.ErrShortWrite
  95. }
  96. return len(buf), err
  97. }