func ListGadgets(db *bolt.DB) { gadgets, err := quimby.GetGadgets(db) if err != nil { log.Fatal(err) } listGadgets(gadgets) }
func GetGadgets(w http.ResponseWriter, req *http.Request) { args := GetArgs(req) g, err := quimby.GetGadgets(args.DB) if err != nil { context.Set(req, "error", err) return } enc := json.NewEncoder(w) enc.Encode(g) }
func DeleteGadget(db *bolt.DB) { gadgets, err := quimby.GetGadgets(db) if err != nil { log.Fatal(err) } fmt.Println("pick a number") listGadgets(gadgets) var n int fmt.Scanf("%d\n", &n) g := gadgets[n-1] g.DB = db fmt.Println(g.Delete()) }
func IndexPage(w http.ResponseWriter, req *http.Request) { args := handlers.GetArgs(req) g, err := quimby.GetGadgets(args.DB) if err != nil { w.WriteHeader(http.StatusInternalServerError) return } i := indexPage{ Gadgets: g, userPage: userPage{ User: args.User.Username, Admin: handlers.Admin(args), Links: []link{ {"quimby", "/"}, }, }, } templates["index.html"].template.ExecuteTemplate(w, "base", i) }
func EditGadget(db *bolt.DB) { gadgets, err := quimby.GetGadgets(db) if err != nil { log.Fatal(err) } listGadgets(gadgets) var i int fmt.Scanf("%d\n", &i) g := gadgets[i-1] g.DB = db var n string fmt.Printf("name (%s): ", g.Name) fmt.Scanf("%s\n", &n) fmt.Printf("host (%s): ", g.Host) fmt.Scanf("%s\n", &g.Host) if n != g.Name && n != "" { g.Delete() g.Name = n } fmt.Println(g.Save()) }
func AdminPage(w http.ResponseWriter, req *http.Request) { args := handlers.GetArgs(req) gadgets, err := quimby.GetGadgets(args.DB) if err != nil { w.WriteHeader(http.StatusInternalServerError) return } links := make([]link, len(gadgets)) for i, g := range gadgets { links[i] = link{Name: g.Name, Path: fmt.Sprintf("/admin/gadgets/%s", g.Id)} } users, err := quimby.GetUsers(args.DB) if err != nil { w.WriteHeader(http.StatusInternalServerError) return } userLinks := make([]link, len(users)) for i, u := range users { userLinks[i] = link{Name: u.Username, Path: fmt.Sprintf("/admin/users/%s", u.Username)} } i := adminPage{ userPage: userPage{ User: args.User.Username, Admin: handlers.Admin(args), Links: []link{ {"quimby", "/"}, {"admin", "/admin.html"}, }, }, Gadgets: links, Users: userLinks, } templates["admin.html"].template.ExecuteTemplate(w, "base", i) }
func SendCommand(db *bolt.DB) { gadgets, err := quimby.GetGadgets(db) if err != nil { log.Fatal(err) } fmt.Println("pick a number") listGadgets(gadgets) var n int fmt.Scanf("%d", &n) g := gadgets[n-1] g.DB = db fmt.Print("command: ") in := bufio.NewReader(os.Stdin) cmd, err := in.ReadString('\n') if err != nil { log.Fatal(err) } log.Println(g.SendCommand(cmd)) }
It("can delete", func() { err := g.Delete() Expect(err).To(BeNil()) g2 := &quimby.Gadget{ Name: "lights", DB: db, } err = g2.Fetch() Expect(err).ToNot(BeNil()) Expect(g2.Host).To(Equal("")) }) It("gets all gadgets", func() { gadgets, err := quimby.GetGadgets(db) Expect(err).To(BeNil()) Expect(len(gadgets)).To(Equal(1)) g2 := gadgets[0] Expect(g2.Host).To(Equal(ts.URL)) }) It("reads the status of the gadget", func() { buf := &bytes.Buffer{} err := g.ReadStatus(buf) Expect(err).To(BeNil()) var status map[string]map[string]gogadgets.Value dec := json.NewDecoder(buf) err = dec.Decode(&status) Expect(err).To(BeNil())