service.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. package service
  2. import (
  3. "bufio"
  4. "bytes"
  5. "crypto/md5"
  6. "encoding/hex"
  7. "encoding/json"
  8. "errors"
  9. "fmt"
  10. "io"
  11. "io/ioutil"
  12. "math/rand"
  13. "net/http"
  14. "net/url"
  15. "os"
  16. "reflect"
  17. "runtime"
  18. "strconv"
  19. "strings"
  20. "sync/atomic"
  21. "time"
  22. "github.com/alibabacloud-go/tea/tea"
  23. )
  24. var defaultUserAgent = fmt.Sprintf("AlibabaCloud (%s; %s) Golang/%s Core/%s TeaDSL/1", runtime.GOOS, runtime.GOARCH, strings.Trim(runtime.Version(), "go"), "0.01")
  25. type ExtendsParameters struct {
  26. Headers map[string]*string `json:"headers,omitempty" xml:"headers,omitempty"`
  27. Queries map[string]*string `json:"queries,omitempty" xml:"queries,omitempty"`
  28. }
  29. func (s ExtendsParameters) String() string {
  30. return tea.Prettify(s)
  31. }
  32. func (s ExtendsParameters) GoString() string {
  33. return s.String()
  34. }
  35. func (s *ExtendsParameters) SetHeaders(v map[string]*string) *ExtendsParameters {
  36. s.Headers = v
  37. return s
  38. }
  39. func (s *ExtendsParameters) SetQueries(v map[string]*string) *ExtendsParameters {
  40. s.Queries = v
  41. return s
  42. }
  43. type RuntimeOptions struct {
  44. IdleTimeout *int `json:"idleTimeout" xml:"idleTimeout"`
  45. Autoretry *bool `json:"autoretry" xml:"autoretry"`
  46. IgnoreSSL *bool `json:"ignoreSSL" xml:"ignoreSSL"`
  47. Key *string `json:"key,omitempty" xml:"key,omitempty"`
  48. Cert *string `json:"cert,omitempty" xml:"cert,omitempty"`
  49. Ca *string `json:"ca,omitempty" xml:"ca,omitempty"`
  50. MaxAttempts *int `json:"maxAttempts" xml:"maxAttempts"`
  51. BackoffPolicy *string `json:"backoffPolicy" xml:"backoffPolicy"`
  52. BackoffPeriod *int `json:"backoffPeriod" xml:"backoffPeriod"`
  53. ReadTimeout *int `json:"readTimeout" xml:"readTimeout"`
  54. ConnectTimeout *int `json:"connectTimeout" xml:"connectTimeout"`
  55. LocalAddr *string `json:"localAddr" xml:"localAddr"`
  56. HttpProxy *string `json:"httpProxy" xml:"httpProxy"`
  57. HttpsProxy *string `json:"httpsProxy" xml:"httpsProxy"`
  58. NoProxy *string `json:"noProxy" xml:"noProxy"`
  59. MaxIdleConns *int `json:"maxIdleConns" xml:"maxIdleConns"`
  60. Socks5Proxy *string `json:"socks5Proxy" xml:"socks5Proxy"`
  61. Socks5NetWork *string `json:"socks5NetWork" xml:"socks5NetWork"`
  62. KeepAlive *bool `json:"keepAlive" xml:"keepAlive"`
  63. ExtendsParameters *ExtendsParameters `json:"extendsParameters,omitempty" xml:"extendsParameters,omitempty"`
  64. // WebSocket Specific Configuration
  65. WebSocketPingInterval *int `json:"webSocketPingInterval" xml:"webSocketPingInterval"` // Ping 间隔(毫秒)
  66. WebSocketPongTimeout *int `json:"webSocketPongTimeout" xml:"webSocketPongTimeout"` // Pong 超时(毫秒)
  67. WebSocketEnableReconnect *bool `json:"webSocketEnableReconnect" xml:"webSocketEnableReconnect"` // 是否启用自动重连
  68. WebSocketReconnectInterval *int `json:"webSocketReconnectInterval" xml:"webSocketReconnectInterval"` // 重连间隔(毫秒)
  69. WebSocketMaxReconnectTimes *int `json:"webSocketMaxReconnectTimes" xml:"webSocketMaxReconnectTimes"` // 最大重连次数
  70. WebSocketWriteTimeout *int `json:"webSocketWriteTimeout" xml:"webSocketWriteTimeout"` // 写入超时(毫秒)
  71. WebSocketHandshakeTimeout *int `json:"webSocketHandshakeTimeout" xml:"webSocketHandshakeTimeout"` // 握手超时(毫秒)
  72. WebSocketHandler interface{} `json:"-" xml:"-"` // WebSocket 消息处理器(不序列化),应使用 dara.WebSocketHandler 类型
  73. }
  74. var processStartTime int64 = time.Now().UnixNano() / 1e6
  75. var seqId int64 = 0
  76. type SSEEvent struct {
  77. ID *string
  78. Event *string
  79. Data *string
  80. Retry *int
  81. }
  82. func parseEvent(eventLines []string) (SSEEvent, error) {
  83. var event SSEEvent
  84. for _, line := range eventLines {
  85. if strings.HasPrefix(line, "data:") {
  86. event.Data = tea.String(tea.StringValue(event.Data) + strings.TrimPrefix(line, "data:") + "\n")
  87. } else if strings.HasPrefix(line, "id:") {
  88. id := strings.TrimPrefix(line, "id:")
  89. event.ID = tea.String(strings.Trim(id, " "))
  90. } else if strings.HasPrefix(line, "event:") {
  91. eventName := strings.TrimPrefix(line, "event:")
  92. event.Event = tea.String(strings.Trim(eventName, " "))
  93. } else if strings.HasPrefix(line, "retry:") {
  94. trimmedLine := strings.TrimPrefix(line, "retry:")
  95. trimmedLine = strings.Trim(trimmedLine, " ")
  96. retryValue, _err := strconv.Atoi(trimmedLine)
  97. if _err != nil {
  98. return event, fmt.Errorf("retry %v is not a int", trimmedLine)
  99. }
  100. event.Retry = tea.Int(retryValue)
  101. }
  102. }
  103. data := strings.TrimRight(tea.StringValue(event.Data), "\n")
  104. event.Data = tea.String(strings.Trim(data, " "))
  105. return event, nil
  106. }
  107. func getGID() uint64 {
  108. // https://blog.sgmansfield.com/2015/12/goroutine-ids/
  109. b := make([]byte, 64)
  110. b = b[:runtime.Stack(b, false)]
  111. b = bytes.TrimPrefix(b, []byte("goroutine "))
  112. b = b[:bytes.IndexByte(b, ' ')]
  113. n, _ := strconv.ParseUint(string(b), 10, 64)
  114. return n
  115. }
  116. func (s RuntimeOptions) String() string {
  117. return tea.Prettify(s)
  118. }
  119. func (s RuntimeOptions) GoString() string {
  120. return s.String()
  121. }
  122. func (s *RuntimeOptions) SetAutoretry(v bool) *RuntimeOptions {
  123. s.Autoretry = &v
  124. return s
  125. }
  126. func (s *RuntimeOptions) SetIgnoreSSL(v bool) *RuntimeOptions {
  127. s.IgnoreSSL = &v
  128. return s
  129. }
  130. func (s *RuntimeOptions) SetKey(v string) *RuntimeOptions {
  131. s.Key = &v
  132. return s
  133. }
  134. func (s *RuntimeOptions) SetCert(v string) *RuntimeOptions {
  135. s.Cert = &v
  136. return s
  137. }
  138. func (s *RuntimeOptions) SetCa(v string) *RuntimeOptions {
  139. s.Ca = &v
  140. return s
  141. }
  142. func (s *RuntimeOptions) SetMaxAttempts(v int) *RuntimeOptions {
  143. s.MaxAttempts = &v
  144. return s
  145. }
  146. func (s *RuntimeOptions) SetBackoffPolicy(v string) *RuntimeOptions {
  147. s.BackoffPolicy = &v
  148. return s
  149. }
  150. func (s *RuntimeOptions) SetBackoffPeriod(v int) *RuntimeOptions {
  151. s.BackoffPeriod = &v
  152. return s
  153. }
  154. func (s *RuntimeOptions) SetReadTimeout(v int) *RuntimeOptions {
  155. s.ReadTimeout = &v
  156. return s
  157. }
  158. func (s *RuntimeOptions) SetConnectTimeout(v int) *RuntimeOptions {
  159. s.ConnectTimeout = &v
  160. return s
  161. }
  162. func (s *RuntimeOptions) SetIdleTimeout(v int) *RuntimeOptions {
  163. s.IdleTimeout = &v
  164. return s
  165. }
  166. func (s *RuntimeOptions) SetHttpProxy(v string) *RuntimeOptions {
  167. s.HttpProxy = &v
  168. return s
  169. }
  170. func (s *RuntimeOptions) SetHttpsProxy(v string) *RuntimeOptions {
  171. s.HttpsProxy = &v
  172. return s
  173. }
  174. func (s *RuntimeOptions) SetNoProxy(v string) *RuntimeOptions {
  175. s.NoProxy = &v
  176. return s
  177. }
  178. func (s *RuntimeOptions) SetMaxIdleConns(v int) *RuntimeOptions {
  179. s.MaxIdleConns = &v
  180. return s
  181. }
  182. func (s *RuntimeOptions) SetLocalAddr(v string) *RuntimeOptions {
  183. s.LocalAddr = &v
  184. return s
  185. }
  186. func (s *RuntimeOptions) SetSocks5Proxy(v string) *RuntimeOptions {
  187. s.Socks5Proxy = &v
  188. return s
  189. }
  190. func (s *RuntimeOptions) SetSocks5NetWork(v string) *RuntimeOptions {
  191. s.Socks5NetWork = &v
  192. return s
  193. }
  194. func (s *RuntimeOptions) SetKeepAlive(v bool) *RuntimeOptions {
  195. s.KeepAlive = &v
  196. return s
  197. }
  198. func (s *RuntimeOptions) SetExtendsParameters(v *ExtendsParameters) *RuntimeOptions {
  199. s.ExtendsParameters = v
  200. return s
  201. }
  202. func (s *RuntimeOptions) SetWebSocketPingInterval(v int) *RuntimeOptions {
  203. s.WebSocketPingInterval = &v
  204. return s
  205. }
  206. func (s *RuntimeOptions) SetWebSocketPongTimeout(v int) *RuntimeOptions {
  207. s.WebSocketPongTimeout = &v
  208. return s
  209. }
  210. func (s *RuntimeOptions) SetWebSocketEnableReconnect(v bool) *RuntimeOptions {
  211. s.WebSocketEnableReconnect = &v
  212. return s
  213. }
  214. func (s *RuntimeOptions) SetWebSocketReconnectInterval(v int) *RuntimeOptions {
  215. s.WebSocketReconnectInterval = &v
  216. return s
  217. }
  218. func (s *RuntimeOptions) SetWebSocketMaxReconnectTimes(v int) *RuntimeOptions {
  219. s.WebSocketMaxReconnectTimes = &v
  220. return s
  221. }
  222. func (s *RuntimeOptions) SetWebSocketWriteTimeout(v int) *RuntimeOptions {
  223. s.WebSocketWriteTimeout = &v
  224. return s
  225. }
  226. func (s *RuntimeOptions) SetWebSocketHandshakeTimeout(v int) *RuntimeOptions {
  227. s.WebSocketHandshakeTimeout = &v
  228. return s
  229. }
  230. func (s *RuntimeOptions) SetWebSocketHandler(v interface{}) *RuntimeOptions {
  231. s.WebSocketHandler = v
  232. return s
  233. }
  234. func ReadAsString(body io.Reader) (*string, error) {
  235. byt, err := ioutil.ReadAll(body)
  236. if err != nil {
  237. return tea.String(""), err
  238. }
  239. r, ok := body.(io.ReadCloser)
  240. if ok {
  241. r.Close()
  242. }
  243. return tea.String(string(byt)), nil
  244. }
  245. func StringifyMapValue(a map[string]interface{}) map[string]*string {
  246. res := make(map[string]*string)
  247. for key, value := range a {
  248. if value != nil {
  249. res[key] = ToJSONString(value)
  250. }
  251. }
  252. return res
  253. }
  254. func AnyifyMapValue(a map[string]*string) map[string]interface{} {
  255. res := make(map[string]interface{})
  256. for key, value := range a {
  257. res[key] = tea.StringValue(value)
  258. }
  259. return res
  260. }
  261. func ReadAsBytes(body io.Reader) ([]byte, error) {
  262. byt, err := ioutil.ReadAll(body)
  263. if err != nil {
  264. return nil, err
  265. }
  266. r, ok := body.(io.ReadCloser)
  267. if ok {
  268. r.Close()
  269. }
  270. return byt, nil
  271. }
  272. func DefaultString(reaStr, defaultStr *string) *string {
  273. if reaStr == nil {
  274. return defaultStr
  275. }
  276. return reaStr
  277. }
  278. func ToJSONString(a interface{}) *string {
  279. switch v := a.(type) {
  280. case *string:
  281. return v
  282. case string:
  283. return tea.String(v)
  284. case []byte:
  285. return tea.String(string(v))
  286. case io.Reader:
  287. byt, err := ioutil.ReadAll(v)
  288. if err != nil {
  289. return nil
  290. }
  291. return tea.String(string(byt))
  292. }
  293. byt := bytes.NewBuffer([]byte{})
  294. jsonEncoder := json.NewEncoder(byt)
  295. jsonEncoder.SetEscapeHTML(false)
  296. if err := jsonEncoder.Encode(a); err != nil {
  297. return nil
  298. }
  299. return tea.String(string(bytes.TrimSpace(byt.Bytes())))
  300. }
  301. func DefaultNumber(reaNum, defaultNum *int) *int {
  302. if reaNum == nil {
  303. return defaultNum
  304. }
  305. return reaNum
  306. }
  307. func ReadAsJSON(body io.Reader) (result interface{}, err error) {
  308. byt, err := ioutil.ReadAll(body)
  309. if err != nil {
  310. return
  311. }
  312. if string(byt) == "" {
  313. return
  314. }
  315. r, ok := body.(io.ReadCloser)
  316. if ok {
  317. r.Close()
  318. }
  319. d := json.NewDecoder(bytes.NewReader(byt))
  320. d.UseNumber()
  321. err = d.Decode(&result)
  322. return
  323. }
  324. func GetNonce() *string {
  325. routineId := getGID()
  326. currentTime := time.Now().UnixNano() / 1e6
  327. seq := atomic.AddInt64(&seqId, 1)
  328. randNum := rand.Int63()
  329. msg := fmt.Sprintf("%d-%d-%d-%d-%d", processStartTime, routineId, currentTime, seq, randNum)
  330. h := md5.New()
  331. h.Write([]byte(msg))
  332. ret := hex.EncodeToString(h.Sum(nil))
  333. return &ret
  334. }
  335. func Empty(val *string) *bool {
  336. return tea.Bool(val == nil || tea.StringValue(val) == "")
  337. }
  338. func ValidateModel(a interface{}) error {
  339. if a == nil {
  340. return nil
  341. }
  342. err := tea.Validate(a)
  343. return err
  344. }
  345. func EqualString(val1, val2 *string) *bool {
  346. return tea.Bool(tea.StringValue(val1) == tea.StringValue(val2))
  347. }
  348. func EqualNumber(val1, val2 *int) *bool {
  349. return tea.Bool(tea.IntValue(val1) == tea.IntValue(val2))
  350. }
  351. func IsUnset(val interface{}) *bool {
  352. if val == nil {
  353. return tea.Bool(true)
  354. }
  355. v := reflect.ValueOf(val)
  356. if v.Kind() == reflect.Ptr || v.Kind() == reflect.Slice || v.Kind() == reflect.Map {
  357. return tea.Bool(v.IsNil())
  358. }
  359. valType := reflect.TypeOf(val)
  360. valZero := reflect.Zero(valType)
  361. return tea.Bool(valZero == v)
  362. }
  363. func ToBytes(a *string) []byte {
  364. return []byte(tea.StringValue(a))
  365. }
  366. func AssertAsMap(a interface{}) (_result map[string]interface{}, _err error) {
  367. r := reflect.ValueOf(a)
  368. if r.Kind().String() != "map" {
  369. return nil, errors.New(fmt.Sprintf("%v is not a map[string]interface{}", a))
  370. }
  371. res := make(map[string]interface{})
  372. tmp := r.MapKeys()
  373. for _, key := range tmp {
  374. res[key.String()] = r.MapIndex(key).Interface()
  375. }
  376. return res, nil
  377. }
  378. func AssertAsNumber(a interface{}) (_result *int, _err error) {
  379. res := 0
  380. switch a.(type) {
  381. case int:
  382. tmp := a.(int)
  383. res = tmp
  384. case *int:
  385. tmp := a.(*int)
  386. res = tea.IntValue(tmp)
  387. default:
  388. return nil, errors.New(fmt.Sprintf("%v is not a int", a))
  389. }
  390. return tea.Int(res), nil
  391. }
  392. /**
  393. * Assert a value, if it is a integer, return it, otherwise throws
  394. * @return the integer value
  395. */
  396. func AssertAsInteger(value interface{}) (_result *int, _err error) {
  397. res := 0
  398. switch value.(type) {
  399. case int:
  400. tmp := value.(int)
  401. res = tmp
  402. case *int:
  403. tmp := value.(*int)
  404. res = tea.IntValue(tmp)
  405. default:
  406. return nil, errors.New(fmt.Sprintf("%v is not a int", value))
  407. }
  408. return tea.Int(res), nil
  409. }
  410. func AssertAsBoolean(a interface{}) (_result *bool, _err error) {
  411. res := false
  412. switch a.(type) {
  413. case bool:
  414. tmp := a.(bool)
  415. res = tmp
  416. case *bool:
  417. tmp := a.(*bool)
  418. res = tea.BoolValue(tmp)
  419. default:
  420. return nil, errors.New(fmt.Sprintf("%v is not a bool", a))
  421. }
  422. return tea.Bool(res), nil
  423. }
  424. func AssertAsString(a interface{}) (_result *string, _err error) {
  425. res := ""
  426. switch a.(type) {
  427. case string:
  428. tmp := a.(string)
  429. res = tmp
  430. case *string:
  431. tmp := a.(*string)
  432. res = tea.StringValue(tmp)
  433. default:
  434. return nil, errors.New(fmt.Sprintf("%v is not a string", a))
  435. }
  436. return tea.String(res), nil
  437. }
  438. func AssertAsBytes(a interface{}) (_result []byte, _err error) {
  439. res, ok := a.([]byte)
  440. if !ok {
  441. return nil, errors.New(fmt.Sprintf("%v is not a []byte", a))
  442. }
  443. return res, nil
  444. }
  445. func AssertAsReadable(a interface{}) (_result io.Reader, _err error) {
  446. res, ok := a.(io.Reader)
  447. if !ok {
  448. return nil, errors.New(fmt.Sprintf("%v is not a reader", a))
  449. }
  450. return res, nil
  451. }
  452. func AssertAsArray(a interface{}) (_result []interface{}, _err error) {
  453. r := reflect.ValueOf(a)
  454. if r.Kind().String() != "array" && r.Kind().String() != "slice" {
  455. return nil, errors.New(fmt.Sprintf("%v is not a []interface{}", a))
  456. }
  457. aLen := r.Len()
  458. res := make([]interface{}, 0)
  459. for i := 0; i < aLen; i++ {
  460. res = append(res, r.Index(i).Interface())
  461. }
  462. return res, nil
  463. }
  464. func ParseJSON(a *string) interface{} {
  465. mapTmp := make(map[string]interface{})
  466. d := json.NewDecoder(bytes.NewReader([]byte(tea.StringValue(a))))
  467. d.UseNumber()
  468. err := d.Decode(&mapTmp)
  469. if err == nil {
  470. return mapTmp
  471. }
  472. sliceTmp := make([]interface{}, 0)
  473. d = json.NewDecoder(bytes.NewReader([]byte(tea.StringValue(a))))
  474. d.UseNumber()
  475. err = d.Decode(&sliceTmp)
  476. if err == nil {
  477. return sliceTmp
  478. }
  479. if num, err := strconv.Atoi(tea.StringValue(a)); err == nil {
  480. return num
  481. }
  482. if ok, err := strconv.ParseBool(tea.StringValue(a)); err == nil {
  483. return ok
  484. }
  485. if floa64tVal, err := strconv.ParseFloat(tea.StringValue(a), 64); err == nil {
  486. return floa64tVal
  487. }
  488. return nil
  489. }
  490. func ToString(a []byte) *string {
  491. return tea.String(string(a))
  492. }
  493. func ToMap(in interface{}) map[string]interface{} {
  494. if in == nil {
  495. return nil
  496. }
  497. res := tea.ToMap(in)
  498. return res
  499. }
  500. func ToFormString(a map[string]interface{}) *string {
  501. if a == nil {
  502. return tea.String("")
  503. }
  504. res := ""
  505. urlEncoder := url.Values{}
  506. for key, value := range a {
  507. v := fmt.Sprintf("%v", value)
  508. urlEncoder.Add(key, v)
  509. }
  510. res = urlEncoder.Encode()
  511. return tea.String(res)
  512. }
  513. func GetDateUTCString() *string {
  514. return tea.String(time.Now().UTC().Format(http.TimeFormat))
  515. }
  516. func GetUserAgent(userAgent *string) *string {
  517. if userAgent != nil && tea.StringValue(userAgent) != "" {
  518. return tea.String(defaultUserAgent + " " + tea.StringValue(userAgent))
  519. }
  520. return tea.String(defaultUserAgent)
  521. }
  522. func Is2xx(code *int) *bool {
  523. tmp := tea.IntValue(code)
  524. return tea.Bool(tmp >= 200 && tmp < 300)
  525. }
  526. func Is3xx(code *int) *bool {
  527. tmp := tea.IntValue(code)
  528. return tea.Bool(tmp >= 300 && tmp < 400)
  529. }
  530. func Is4xx(code *int) *bool {
  531. tmp := tea.IntValue(code)
  532. return tea.Bool(tmp >= 400 && tmp < 500)
  533. }
  534. func Is5xx(code *int) *bool {
  535. tmp := tea.IntValue(code)
  536. return tea.Bool(tmp >= 500 && tmp < 600)
  537. }
  538. func Sleep(millisecond *int) error {
  539. ms := tea.IntValue(millisecond)
  540. time.Sleep(time.Duration(ms) * time.Millisecond)
  541. return nil
  542. }
  543. func ToArray(in interface{}) []map[string]interface{} {
  544. if tea.BoolValue(IsUnset(in)) {
  545. return nil
  546. }
  547. tmp := make([]map[string]interface{}, 0)
  548. byt, _ := json.Marshal(in)
  549. d := json.NewDecoder(bytes.NewReader(byt))
  550. d.UseNumber()
  551. err := d.Decode(&tmp)
  552. if err != nil {
  553. return nil
  554. }
  555. return tmp
  556. }
  557. func ReadAsSSE(body io.ReadCloser) (<-chan SSEEvent, <-chan error) {
  558. eventChannel := make(chan SSEEvent)
  559. errorChannel := make(chan error)
  560. go func() {
  561. defer body.Close()
  562. defer close(eventChannel)
  563. reader := bufio.NewReader(body)
  564. var eventLines []string
  565. for {
  566. line, err := reader.ReadString('\n')
  567. if err == io.EOF {
  568. break
  569. }
  570. if err != nil {
  571. errorChannel <- err
  572. }
  573. line = strings.TrimRight(line, "\n")
  574. if line == "" {
  575. if len(eventLines) > 0 {
  576. event, err := parseEvent(eventLines)
  577. if err != nil {
  578. errorChannel <- err
  579. }
  580. eventChannel <- event
  581. eventLines = []string{}
  582. }
  583. continue
  584. }
  585. eventLines = append(eventLines, line)
  586. }
  587. }()
  588. return eventChannel, errorChannel
  589. }
  590. func GetHostName() *string {
  591. hostname, err := os.Hostname()
  592. if err != nil {
  593. return tea.String("")
  594. }
  595. return tea.String(hostname)
  596. }