io.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // This package defines several top level IO functions for interacting with
  2. // issues and collections of issues.
  3. //
  4. // Examples:
  5. //
  6. // To check if a folder is spec compliant:
  7. // ...
  8. // if issues.IsIssue(path_to_folder) {
  9. // process_issue(path_to_folder)
  10. // }
  11. //
  12. // Additionally, to check if a folder is a collection of spec compliant issues:
  13. // ...
  14. // if issues.IsIssueCollection(path_to_folder) {
  15. // process_collection(path_to_folder)
  16. // }
  17. //
  18. // To write an [Issue] object to disk:
  19. // ...
  20. // // NOT IMPLEMENTED
  21. //
  22. // To remove an [Issue] object from disk:
  23. // ...
  24. // // NOT IMPLEMENTED
  25. package issues
  26. import (
  27. "errors"
  28. "os"
  29. "path/filepath"
  30. )
  31. // converts file from []string to string, reports errors
  32. func readPath(path string) (output string, err error) {
  33. content, err := os.ReadFile(path)
  34. if err != nil {
  35. return "", err
  36. }
  37. for _, line := range content {
  38. output = output + string(line)
  39. }
  40. return output, nil
  41. }
  42. // Reports true when the specified path conforms to the minimum Poorman spec
  43. func IsIssue(path string) bool {
  44. files, err := os.ReadDir(path)
  45. if err != nil {
  46. return false
  47. }
  48. var specFiles []bool
  49. for _, file := range files {
  50. if file.Name() == "description" || file.Name() == "status" {
  51. specFiles = append(specFiles, true)
  52. }
  53. }
  54. if len(specFiles) >= 2 {
  55. return true
  56. }
  57. return false
  58. }
  59. // Reports true when the specified path is a directory of Issues
  60. func IsIssueCollection(path string) bool {
  61. if IsIssue(path) {
  62. return false
  63. }
  64. files, err := os.ReadDir(path)
  65. if err != nil {
  66. return false
  67. }
  68. var isIssue []bool
  69. for _, file := range files {
  70. if IsIssue(path + "/" + file.Name()) {
  71. isIssue = append(isIssue, true)
  72. }
  73. }
  74. if len(isIssue) > 0 {
  75. return true
  76. }
  77. return false
  78. }
  79. // Writes a issue to disk
  80. func WriteIssue(issue Issue, ignoreExisting bool) (success bool, err error) {
  81. if IsIssue(issue.Path) && !ignoreExisting {
  82. return false, errors.New("path exists")
  83. }
  84. // make base directory (effectively, the title)
  85. err = os.Mkdir(issue.Path, 0755)
  86. if err != nil {
  87. return false, err
  88. }
  89. // Write Fields
  90. f := []Field{issue.Description, issue.Status}
  91. for _, field := range f {
  92. if len(field.Path) > 0 { // skip empty paths
  93. path := filepath.Join(issue.Path, field.Path)
  94. err = os.WriteFile(path, []byte(field.Data), 0755)
  95. if err != nil {
  96. return false, err // test by overwrite path
  97. }
  98. }
  99. }
  100. // Write Tags
  101. if len(issue.Tags.Path) > 0 { // skip tags with no path init
  102. err = os.Mkdir(filepath.Join(issue.Path, issue.Tags.Path), 0755)
  103. if err != nil {
  104. return false, err // test by overwrite path
  105. }
  106. for _, field := range issue.Tags.Fields {
  107. if len(field.Path) > 0 { // skip empty paths
  108. path := filepath.Join(issue.Path, issue.Tags.Path, field.Path)
  109. err = os.WriteFile(path, []byte(field.Data), 0755)
  110. if err != nil {
  111. return false, err // test by overwrite path
  112. }
  113. }
  114. }
  115. }
  116. // Write Blockedby
  117. if len(issue.Tags.Path) > 0 { // skip blockedby with no path init
  118. err = os.Mkdir(filepath.Join(issue.Path, issue.Blockedby.Path), 0755)
  119. if err != nil {
  120. return false, err // test by overwrite path
  121. }
  122. for _, field := range issue.Blockedby.Fields {
  123. if len(field.Path) > 0 { // skip empty paths
  124. path := filepath.Join(issue.Path, issue.Blockedby.Path, field.Path)
  125. err = os.WriteFile(path, []byte(field.Data), 0755)
  126. if err != nil {
  127. return false, err // test by overwrite path
  128. }
  129. }
  130. }
  131. }
  132. return true, nil
  133. }
  134. // Removes a issue from disk
  135. func DeleteIssue(issue Issue) (success bool, err error) { return false, nil } // TODO: implement