|
@@ -0,0 +1,153 @@
|
|
|
+package figtree
|
|
|
+
|
|
|
+import (
|
|
|
+ "log"
|
|
|
+ "os"
|
|
|
+ "time"
|
|
|
+ "path"
|
|
|
+
|
|
|
+ "math/rand"
|
|
|
+ "database/sql"
|
|
|
+
|
|
|
+ _ "github.com/mattn/go-sqlite3"
|
|
|
+)
|
|
|
+
|
|
|
+var FIGS = "/home/jord/figs/"
|
|
|
+
|
|
|
+var tree *sql.DB
|
|
|
+
|
|
|
+func init() {
|
|
|
+ rand.Seed(time.Now().UnixNano())
|
|
|
+ treePlanted := isPlanted()
|
|
|
+ if ! treePlanted {
|
|
|
+ plant()
|
|
|
+ }
|
|
|
+ mytree, err := sql.Open("sqlite3", "figtree.db")
|
|
|
+ if err != nil {
|
|
|
+ log.Fatal(err.Error())
|
|
|
+ }
|
|
|
+ tree = mytree
|
|
|
+}
|
|
|
+
|
|
|
+func main() {
|
|
|
+ defer tree.Close()
|
|
|
+}
|
|
|
+
|
|
|
+func isPlanted() bool {
|
|
|
+ _, err := os.Stat("figtree.db")
|
|
|
+ isTree := !os.IsNotExist(err)
|
|
|
+ return isTree
|
|
|
+}
|
|
|
+
|
|
|
+func plant() {
|
|
|
+ log.Println("planting fig tree 🌿")
|
|
|
+ file, err := os.Create("figtree.db")
|
|
|
+ if err != nil {
|
|
|
+ log.Fatal(err.Error())
|
|
|
+ }
|
|
|
+ file.Close()
|
|
|
+ log.Println("fig tree planted")
|
|
|
+
|
|
|
+ log.Println("sprouting tree")
|
|
|
+
|
|
|
+ treeSchema := `CREATE TABLE tree (
|
|
|
+ "id" string primary key not null,
|
|
|
+ "name" string,
|
|
|
+ "desc" string,
|
|
|
+ "type" string not null,
|
|
|
+ "ext" string,
|
|
|
+ "birth" string
|
|
|
+ );`
|
|
|
+
|
|
|
+ mytree, err := sql.Open("sqlite3", "figtree.db")
|
|
|
+ defer mytree.Close()
|
|
|
+ _, err = mytree.Exec(treeSchema)
|
|
|
+ if err != nil {
|
|
|
+ log.Fatal(err.Error())
|
|
|
+ }
|
|
|
+ log.Println("tree sprouted :)")
|
|
|
+}
|
|
|
+
|
|
|
+func nameAFig() string {
|
|
|
+ name := newName()
|
|
|
+ for nameExists(name) {
|
|
|
+ name = newName()
|
|
|
+ }
|
|
|
+ return name
|
|
|
+}
|
|
|
+
|
|
|
+func newName() string {
|
|
|
+ NAMELEN := 6
|
|
|
+ CHARS := []rune("abcdefghijklmnopqrstuvwxyz0123456789")
|
|
|
+ figname := make([]rune, NAMELEN)
|
|
|
+ for i := range figname {
|
|
|
+ figname[i] = CHARS[rand.Intn(len(CHARS))]
|
|
|
+ }
|
|
|
+ return string(figname)
|
|
|
+}
|
|
|
+
|
|
|
+func nameExists(figname string) bool {
|
|
|
+ check := "select name from tree where name like '%" + figname + "%'"
|
|
|
+ rows, err := tree.Query(check)
|
|
|
+ if err != nil {
|
|
|
+ log.Fatal(err.Error())
|
|
|
+ }
|
|
|
+ defer rows.Close()
|
|
|
+ return rows.Next()
|
|
|
+}
|
|
|
+
|
|
|
+func DryFig(fig Fig) error {
|
|
|
+ addFig := `insert into tree (id, name, desc, type, ext, birth) values (?, ?, ?, ?, ?, ?);`
|
|
|
+ statement, err := tree.Prepare(addFig)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ _, err = statement.Exec(fig.Id, fig.Name, fig.Desc, fig.Type, fig.Ext, fig.Birth)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func AllFigsFancy() ([]FancyFig, error) {
|
|
|
+ var figs []FancyFig
|
|
|
+ row, err := tree.Query("select * from tree")
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ defer row.Close()
|
|
|
+ for row.Next() {
|
|
|
+ var id, name, desc, figtype, ext, birth string
|
|
|
+ row.Scan(&id, &name, &desc, &figtype, &ext, &birth)
|
|
|
+ f := Fig{id, name, desc, figtype, ext, birth}
|
|
|
+ figs = append(figs, f.Fancy())
|
|
|
+ }
|
|
|
+ return figs, nil
|
|
|
+}
|
|
|
+
|
|
|
+func HarvestFig(name, desc, figtype, ext, birth string) (string, Fig, error) {
|
|
|
+ if len(figtype) == 0 {
|
|
|
+ figtype = "untyped"
|
|
|
+ }
|
|
|
+ figBranch := path.Join(FIGS, figtype)
|
|
|
+ _, err := os.Stat(figBranch)
|
|
|
+ if os.IsNotExist(err) {
|
|
|
+ err = os.Mkdir(figBranch, 0774)
|
|
|
+ if err != nil {
|
|
|
+ return "", Fig{}, err
|
|
|
+ }
|
|
|
+ }
|
|
|
+ newfig := Fig{
|
|
|
+ nameAFig(),
|
|
|
+ name,
|
|
|
+ desc,
|
|
|
+ figtype,
|
|
|
+ ext,
|
|
|
+ birth}
|
|
|
+
|
|
|
+ figid := newfig.Id + "." + newfig.Ext
|
|
|
+ figBlossom := path.Join(figBranch, figid)
|
|
|
+ return figBlossom, newfig, nil
|
|
|
+}
|