io.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package buggo
  2. import (
  3. "os"
  4. )
  5. func readPath(path string) (output string, err error) {
  6. content, err := os.ReadFile(path)
  7. if err != nil {
  8. return "", err
  9. }
  10. for _, line := range content {
  11. output = output + string(line)
  12. }
  13. return output, nil
  14. }
  15. func loadVariadicField(pathOnDisk string, vf VariadicField) (v VariadicField, err error) {
  16. rootPath := pathOnDisk + "/" + vf.Path
  17. files, err := os.ReadDir(rootPath)
  18. if err != nil {
  19. return vf, err
  20. }
  21. for _, file := range files {
  22. data, err := readPath(rootPath + "/" + file.Name())
  23. if err != nil {
  24. return vf, err
  25. }
  26. vf.Fields = append(vf.Fields, Field{Data: data, Path: file.Name()})
  27. }
  28. return vf, err
  29. }
  30. // Loads a bug from disk
  31. func LoadBug(path string) (bug Bug, err error) {
  32. // Required Fields
  33. description := &Field{Path: "/description"}
  34. status := &Field{Path: "/status"}
  35. requiredFields := []*Field{description, status}
  36. for _, field := range requiredFields {
  37. data, err := readPath(path + field.Path)
  38. if err != nil {
  39. return Bug{}, err
  40. }
  41. field.Data = data
  42. }
  43. // Variadic Fields
  44. tags := VariadicField{Path: "/tags"}
  45. blockers := VariadicField{Path: "/blockedby"}
  46. tags, _ = loadVariadicField(path, tags)
  47. blockers, _ = loadVariadicField(path, blockers)
  48. // we can ignore the errors, as loadVariadicField already gracefully handles
  49. //them.
  50. return Bug{
  51. Description: *description,
  52. Status: *status,
  53. Tags: tags,
  54. Blockedby: blockers,
  55. }, err
  56. }
  57. // Writes a bug to disk
  58. func (b Bug) WriteBug() (success bool, err error) { return false, nil } // TODO: implement
  59. // Removes a bug from disk
  60. func (b Bug) DeleteBug() (success bool, err error) { return false, nil } // TODO: implement