io.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. "fmt"
  29. "math/rand"
  30. "os"
  31. "os/exec"
  32. "path/filepath"
  33. "strings"
  34. "time"
  35. )
  36. // converts file from []string to string, reports errors
  37. func readPath(path string) (output string, err error) { // TODO DEPRECATE
  38. content, err := os.ReadFile(path)
  39. if err != nil {
  40. return "", err
  41. }
  42. for _, line := range content {
  43. output = output + string(line)
  44. }
  45. return output, nil
  46. }
  47. // The quote string used to generate a template description
  48. type Template string
  49. var DescriptionTemplate = Template(`# Please provide a short, one line description.
  50. # Include any additional comments here.
  51. # -----------
  52. # Note: Lines beginning with "#" are automatically ignored.
  53. # Note: It is recommended to leave a blank line after the short description.`)
  54. // generates template description files
  55. func GenerateTemplate(t Template, path string) error {
  56. err := os.WriteFile(path, []byte(DescriptionTemplate), 0755)
  57. return err
  58. }
  59. // parses template description files, removing any lines beginning with #
  60. //
  61. // Note: the function deletes the template file upon completion.
  62. func ReadTemplate(path string) (lines []string, err error) {
  63. data, err := os.ReadFile(path)
  64. if err != nil {
  65. return []string{}, err
  66. }
  67. sdata := string(data)
  68. slines := strings.SplitSeq(sdata, "\n")
  69. for line := range slines {
  70. if len(string(line)) > 0 {
  71. if string(line[0]) != "#" {
  72. lines = append(lines, line)
  73. }
  74. } else {
  75. lines = append(lines, line)
  76. }
  77. }
  78. return lines, err
  79. }
  80. // InvokeEditor invokes a preconfigured editor, and reports the output and any errors.
  81. func InvokeEditor(path string) error {
  82. // determine editor
  83. // 1. Git config
  84. // 2. $EDITOR
  85. // 3. Panic?
  86. // execute editor
  87. cmd := exec.Command("vim", path)
  88. cmd.Stdin = os.Stdin // capture data
  89. cmd.Stdout = os.Stdout // capture data
  90. cmd.Stderr = os.Stderr // capture data
  91. // wait for cmd and error out if err
  92. if err := cmd.Run(); err != nil {
  93. return fmt.Errorf("editor execution failed: %w", err)
  94. }
  95. return nil
  96. }
  97. // EditTemplate wraps GenerateTemplate and ReadTemplate, providing
  98. // both with a tempfile. EditTemplate then reports the content of the tempfile
  99. // after any modifications made by the editFunc callback and any encountered
  100. // errors.
  101. //
  102. // Note: A pre-built editor function is provided. See InvokeEditor for more.
  103. func EditTemplate(t Template, editFunc func(path string) error) (data []string, err error) {
  104. // note: wraps writeText
  105. var seededRand = rand.New(
  106. rand.NewSource(time.Now().UnixNano()))
  107. makeID := func(length int) string {
  108. var charset = "abcdefghijklmnopqrstuvwxyz" +
  109. "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  110. b := make([]byte, length)
  111. for i := range b {
  112. b[i] = charset[seededRand.Intn(len(charset))]
  113. }
  114. return string(b)
  115. }
  116. // get get temp file
  117. tempfile, err := os.CreateTemp("", makeID(8))
  118. if err != nil {
  119. return []string{}, nil
  120. }
  121. cleanup := func() {
  122. err = os.Remove(tempfile.Name())
  123. if err != nil {
  124. panic(err)
  125. }
  126. }
  127. defer cleanup()
  128. err = GenerateTemplate(DescriptionTemplate, tempfile.Name())
  129. if err != nil {
  130. return []string{}, nil
  131. }
  132. // invoke editor
  133. err = editFunc(tempfile.Name())
  134. if err != nil {
  135. return []string{}, nil
  136. }
  137. // get data
  138. result, err := ReadTemplate(tempfile.Name())
  139. return result, err
  140. }
  141. // Reports true when the specified path conforms to the minimum Poorman spec
  142. func IsIssue(path string) bool {
  143. files, err := os.ReadDir(path)
  144. if err != nil {
  145. return false
  146. }
  147. var specFiles []bool
  148. for _, file := range files {
  149. if file.Name() == "description" || file.Name() == "status" {
  150. specFiles = append(specFiles, true)
  151. }
  152. }
  153. if len(specFiles) >= 2 {
  154. return true
  155. }
  156. return false
  157. }
  158. // Reports true when the specified path is a directory of Issues
  159. func IsIssueCollection(path string) bool {
  160. if IsIssue(path) {
  161. return false
  162. }
  163. files, err := os.ReadDir(path)
  164. if err != nil {
  165. return false
  166. }
  167. var isIssue []bool
  168. for _, file := range files {
  169. if IsIssue(path + "/" + file.Name()) {
  170. isIssue = append(isIssue, true)
  171. }
  172. }
  173. if len(isIssue) > 0 {
  174. return true
  175. }
  176. return false
  177. }
  178. // Writes a issue to disk
  179. func WriteIssue(issue Issue, ignoreExisting bool) (success bool, err error) {
  180. if IsIssue(issue.Path) && !ignoreExisting {
  181. return false, errors.New("path exists")
  182. }
  183. // make base directory (effectively, the title)
  184. err = os.Mkdir(issue.Path, 0755)
  185. if err != nil {
  186. return false, err
  187. }
  188. // Write Fields
  189. f := []Field{issue.Description, issue.Status}
  190. for _, field := range f {
  191. if len(field.Path) > 0 { // skip empty paths
  192. path := filepath.Join(issue.Path, field.Path)
  193. err = os.WriteFile(path, []byte(field.Data), 0755)
  194. if err != nil {
  195. return false, err // test by overwrite path
  196. }
  197. }
  198. }
  199. // Write Tags
  200. if len(issue.Tags.Path) > 0 { // skip tags with no path init
  201. err = os.Mkdir(filepath.Join(issue.Path, issue.Tags.Path), 0755)
  202. if err != nil {
  203. return false, err // test by overwrite path
  204. }
  205. for _, field := range issue.Tags.Fields {
  206. if len(field.Path) > 0 { // skip empty paths
  207. path := filepath.Join(issue.Path, issue.Tags.Path, field.Path)
  208. err = os.WriteFile(path, []byte(field.Data), 0755)
  209. if err != nil {
  210. return false, err // test by overwrite path
  211. }
  212. }
  213. }
  214. }
  215. // Write Blockedby
  216. if len(issue.Tags.Path) > 0 { // skip blockedby with no path init
  217. err = os.Mkdir(filepath.Join(issue.Path, issue.Blockedby.Path), 0755)
  218. if err != nil {
  219. return false, err // test by overwrite path
  220. }
  221. for _, field := range issue.Blockedby.Fields {
  222. if len(field.Path) > 0 { // skip empty paths
  223. path := filepath.Join(issue.Path, issue.Blockedby.Path, field.Path)
  224. err = os.WriteFile(path, []byte(field.Data), 0755)
  225. if err != nil {
  226. return false, err // test by overwrite path
  227. }
  228. }
  229. }
  230. }
  231. return true, nil
  232. }
  233. // Removes a issue from disk
  234. func DeleteIssue(issue Issue) (success bool, err error) { return false, nil } // TODO: implement