figtree.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package figtree
  2. import (
  3. "log"
  4. "os"
  5. "time"
  6. "path"
  7. "math/rand"
  8. "database/sql"
  9. _ "github.com/mattn/go-sqlite3"
  10. )
  11. var FIGS = "/home/jord/figs/"
  12. var tree *sql.DB
  13. func init() {
  14. rand.Seed(time.Now().UnixNano())
  15. treePlanted := isPlanted()
  16. if ! treePlanted {
  17. plant()
  18. }
  19. mytree, err := sql.Open("sqlite3", "figtree.db")
  20. if err != nil {
  21. log.Fatal(err.Error())
  22. }
  23. tree = mytree
  24. }
  25. func main() {
  26. defer tree.Close()
  27. }
  28. func isPlanted() bool {
  29. _, err := os.Stat("figtree.db")
  30. isTree := !os.IsNotExist(err)
  31. return isTree
  32. }
  33. func plant() {
  34. log.Println("planting fig tree 🌿")
  35. file, err := os.Create("figtree.db")
  36. if err != nil {
  37. log.Fatal(err.Error())
  38. }
  39. file.Close()
  40. log.Println("fig tree planted")
  41. log.Println("sprouting tree")
  42. treeSchema := `CREATE TABLE tree (
  43. "id" string primary key not null,
  44. "name" string,
  45. "desc" string,
  46. "type" string not null,
  47. "ext" string,
  48. "birth" string
  49. );`
  50. mytree, err := sql.Open("sqlite3", "figtree.db")
  51. defer mytree.Close()
  52. _, err = mytree.Exec(treeSchema)
  53. if err != nil {
  54. log.Fatal(err.Error())
  55. }
  56. log.Println("tree sprouted :)")
  57. }
  58. func nameAFig() string {
  59. name := newName()
  60. for nameExists(name) {
  61. name = newName()
  62. }
  63. return name
  64. }
  65. func newName() string {
  66. NAMELEN := 6
  67. CHARS := []rune("abcdefghijklmnopqrstuvwxyz0123456789")
  68. figname := make([]rune, NAMELEN)
  69. for i := range figname {
  70. figname[i] = CHARS[rand.Intn(len(CHARS))]
  71. }
  72. return string(figname)
  73. }
  74. func nameExists(figname string) bool {
  75. check := "select name from tree where name like '%" + figname + "%'"
  76. rows, err := tree.Query(check)
  77. if err != nil {
  78. log.Fatal(err.Error())
  79. }
  80. defer rows.Close()
  81. return rows.Next()
  82. }
  83. func DryFig(fig Fig) error {
  84. addFig := `insert into tree (id, name, desc, type, ext, birth) values (?, ?, ?, ?, ?, ?);`
  85. statement, err := tree.Prepare(addFig)
  86. if err != nil {
  87. return err
  88. }
  89. _, err = statement.Exec(fig.Id, fig.Name, fig.Desc, fig.Type, fig.Ext, fig.Birth)
  90. if err != nil {
  91. return err
  92. }
  93. return nil
  94. }
  95. func AllFigsFancy() ([]FancyFig, error) {
  96. var figs []FancyFig
  97. row, err := tree.Query("select * from tree")
  98. if err != nil {
  99. return nil, err
  100. }
  101. defer row.Close()
  102. for row.Next() {
  103. var id, name, desc, figtype, ext, birth string
  104. row.Scan(&id, &name, &desc, &figtype, &ext, &birth)
  105. f := Fig{id, name, desc, figtype, ext, birth}
  106. figs = append(figs, f.Fancy())
  107. }
  108. return figs, nil
  109. }
  110. func HarvestFig(name, desc, figtype, ext, birth string) (string, Fig, error) {
  111. if len(figtype) == 0 {
  112. figtype = "untyped"
  113. }
  114. figBranch := path.Join(FIGS, figtype)
  115. _, err := os.Stat(figBranch)
  116. if os.IsNotExist(err) {
  117. err = os.Mkdir(figBranch, 0774)
  118. if err != nil {
  119. return "", Fig{}, err
  120. }
  121. }
  122. newfig := Fig{
  123. nameAFig(),
  124. name,
  125. desc,
  126. figtype,
  127. ext,
  128. birth}
  129. figid := newfig.Id + "." + newfig.Ext
  130. figBlossom := path.Join(figBranch, figid)
  131. return figBlossom, newfig, nil
  132. }