check_file.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package executor
  2. import (
  3. "fmt"
  4. schemepkg "git.buran.team/main/cep/scheme"
  5. fw "git.buran.team/main/fairwind"
  6. )
  7. type CheckFile struct {
  8. global *Global
  9. ticket *Ticket
  10. id string
  11. container Container
  12. timeout int
  13. path string
  14. processors []Processor
  15. }
  16. func NewCheckFile(global *Global, ticket *Ticket, container Container, checkScheme schemepkg.Check) (*CheckFile, error) {
  17. processors := []Processor{}
  18. for _, processorScheme := range checkScheme.KindFile.Processors {
  19. processor, err := NewProcessor(processorScheme)
  20. if err != nil {
  21. return nil, fmt.Errorf("can't create result: %w", err)
  22. }
  23. processors = append(processors, processor)
  24. }
  25. return &CheckFile{
  26. global: global,
  27. ticket: ticket,
  28. id: checkScheme.ID,
  29. container: container,
  30. timeout: checkScheme.Timeout,
  31. path: checkScheme.KindFile.Path,
  32. processors: processors,
  33. }, nil
  34. }
  35. func (this *CheckFile) ID() string {
  36. return this.id
  37. }
  38. func (this *CheckFile) Check() CheckResult {
  39. this.global.Log.Debug("beginning file check", fw.LogValue("uuid", this.ticket.UUID))
  40. buffer, err := this.container.Read(this.timeout, this.path)
  41. if err != nil {
  42. this.global.Log.Debug("file check ended with error", fw.LogValue("uuid", this.ticket.UUID), fw.LogError(err))
  43. return CheckResult{
  44. Success: false,
  45. Data: nil,
  46. }
  47. }
  48. for _, processor := range this.processors {
  49. if !processor.Process(buffer) {
  50. this.global.Log.Debug("file check ended with error", fw.LogValue("uuid", this.ticket.UUID))
  51. return CheckResult{
  52. Success: false,
  53. Data: nil,
  54. }
  55. }
  56. }
  57. this.global.Log.Debug("file check ended successfully", fw.LogValue("uuid", this.ticket.UUID))
  58. return CheckResult{
  59. Success: true,
  60. Data: nil,
  61. }
  62. }