io.go 7.3 KB

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