io.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. //
  94. // TODO(InvokeEditor) implement a channel & goroutine based concurrency lifecycle
  95. func InvokeEditor(path string) error {
  96. // determine editor
  97. // 1. Git config
  98. // 2. $EDITOR
  99. // 3. Panic?
  100. editor := os.Getenv("ISSUES_EDITOR")
  101. // if issue editor wasn't present, check git editor
  102. if editor == "" {
  103. editor = os.Getenv("GIT_EDITOR")
  104. }
  105. // if git editor wasn't present, check if editor set
  106. if editor == "" {
  107. editor = os.Getenv("EDITOR")
  108. }
  109. // if editor wasn't present, error out
  110. if editor == "" {
  111. log.Fatal("no editor set by system")
  112. os.Exit(2)
  113. }
  114. // execute editor
  115. editorCmd, err := shlex.Split(editor)
  116. if err != nil {
  117. log.Fatal("could not parse editor")
  118. os.Exit(2)
  119. }
  120. editorCmd = append(editorCmd, path)
  121. cmd := exec.Command(editorCmd[0], editorCmd[1:]...)
  122. cmd.Stdin = os.Stdin // pass stdin to cmd
  123. cmd.Stdout = os.Stdout // pass stdout to cmd
  124. cmd.Stderr = os.Stderr // pass stderr to cmd
  125. // wait for cmd and error out if err
  126. if err := cmd.Run(); err != nil {
  127. return fmt.Errorf("editor execution failed: %w", err)
  128. }
  129. return nil
  130. }
  131. // EditTemplate wraps GenerateTemplate and ReadTemplate, providing
  132. // both with a tempfile. EditTemplate then reports the content of the tempfile
  133. // after any modifications made by the editFunc callback and any encountered
  134. // errors.
  135. //
  136. // Note: A pre-built editor callback is provided. See InvokeEditor for more.
  137. func EditTemplate(t Template, editFunc func(path string) error) (data []string, err error) {
  138. // note: wraps writeText
  139. var seededRand = rand.New(
  140. rand.NewSource(time.Now().UnixNano()))
  141. makeID := func(length int) string {
  142. var charset = "abcdefghijklmnopqrstuvwxyz" +
  143. "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  144. b := make([]byte, length)
  145. for i := range b {
  146. b[i] = charset[seededRand.Intn(len(charset))]
  147. }
  148. return string(b)
  149. }
  150. // get get temp file
  151. tempfile, err := os.CreateTemp("", makeID(8))
  152. if err != nil {
  153. return []string{}, nil
  154. }
  155. cleanup := func() {
  156. err = os.Remove(tempfile.Name())
  157. if err != nil {
  158. panic(err)
  159. }
  160. }
  161. defer cleanup()
  162. err = GenerateTemplate(DescriptionTemplate, tempfile.Name())
  163. if err != nil {
  164. return []string{}, nil
  165. }
  166. // invoke editor
  167. err = editFunc(tempfile.Name())
  168. if err != nil {
  169. return []string{}, nil
  170. }
  171. // get data
  172. result, err := ReadTemplate(tempfile.Name())
  173. return result, err
  174. }
  175. // Reports true when the specified path conforms to the minimum Poorman spec
  176. func IsIssue(path string) bool {
  177. files, err := os.ReadDir(path)
  178. if err != nil {
  179. return false
  180. }
  181. var specFiles []bool
  182. for _, file := range files {
  183. if file.Name() == "description" || file.Name() == "status" {
  184. specFiles = append(specFiles, true)
  185. }
  186. }
  187. if len(specFiles) >= 2 {
  188. return true
  189. }
  190. return false
  191. }
  192. // Reports true when the specified path is a directory of Issues
  193. func IsIssueCollection(path string) bool {
  194. if IsIssue(path) {
  195. return false
  196. }
  197. files, err := os.ReadDir(path)
  198. if err != nil {
  199. return false
  200. }
  201. var isIssue []bool
  202. for _, file := range files {
  203. if IsIssue(path + "/" + file.Name()) {
  204. isIssue = append(isIssue, true)
  205. }
  206. }
  207. if len(isIssue) > 0 {
  208. return true
  209. }
  210. return false
  211. }
  212. // Writes a issue to disk
  213. func WriteIssue(issue Issue, overwrite bool) (success bool, err error) {
  214. if IsIssue(issue.Path) && !overwrite {
  215. return false, errors.New("path exists")
  216. }
  217. // make base directory (effectively, the title)
  218. err = os.Mkdir(issue.Path, 0755)
  219. if err != nil && !overwrite {
  220. return false, err
  221. } else {
  222. err = nil
  223. }
  224. // Write Fields
  225. f := []Field{issue.Description, issue.Status}
  226. for _, field := range f {
  227. if len(field.Path) > 0 { // skip empty paths
  228. path := filepath.Join(issue.Path, field.Path)
  229. err = os.WriteFile(path, []byte(field.Data), 0755)
  230. if err != nil {
  231. return false, err // test by overwrite path
  232. }
  233. }
  234. }
  235. // Write Tags
  236. if len(issue.Tags.Path) > 0 { // skip tags with no path init
  237. err = os.Mkdir(filepath.Join(issue.Path, issue.Tags.Path), 0755)
  238. if err != nil && !overwrite {
  239. return false, err
  240. }
  241. for _, field := range issue.Tags.Fields {
  242. if len(field.Path) > 0 { // skip empty paths
  243. path := filepath.Join(issue.Path, issue.Tags.Path, field.Path)
  244. err = os.WriteFile(path, []byte(field.Data), 0755)
  245. if err != nil {
  246. return false, err // test by overwrite path
  247. }
  248. }
  249. }
  250. }
  251. // Write Blockedby
  252. if len(issue.Blockedby.Path) > 0 { // skip blockedby with no path init
  253. err = os.Mkdir(filepath.Join(issue.Path, issue.Blockedby.Path), 0755)
  254. if err != nil && !overwrite {
  255. return false, err
  256. }
  257. for _, field := range issue.Blockedby.Fields {
  258. if len(field.Path) > 0 { // skip empty paths
  259. path := filepath.Join(issue.Path, issue.Blockedby.Path, field.Path)
  260. err = os.WriteFile(path, []byte(field.Data), 0755)
  261. if err != nil {
  262. return false, err // test by overwrite path
  263. }
  264. }
  265. }
  266. if err != nil && !overwrite {
  267. return false, err
  268. }
  269. }
  270. return true, nil
  271. }
  272. // Removes a issue from disk
  273. func DeleteIssue(issue Issue) (success bool, err error) { return false, nil } // TODO: implement