io.go 7.3 KB

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