2025-01-08 19:58:29 -06:00
|
|
|
package main
|
|
|
|
|
2025-01-09 16:18:06 -06:00
|
|
|
import (
|
2025-01-09 20:08:22 -06:00
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
2025-01-09 16:18:06 -06:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"go.wit.com/log"
|
|
|
|
)
|
2025-01-08 19:58:29 -06:00
|
|
|
|
|
|
|
// sent via -ldflags
|
|
|
|
var VERSION string
|
|
|
|
var BUILDTIME string
|
|
|
|
|
2025-01-11 02:44:21 -06:00
|
|
|
var counter int = rand.Intn(100)
|
2025-01-08 19:58:29 -06:00
|
|
|
|
|
|
|
func main() {
|
2025-01-11 02:44:21 -06:00
|
|
|
var pb *Fruits
|
|
|
|
pb = new(Fruits)
|
2025-01-09 20:08:22 -06:00
|
|
|
if err := pb.loadBasket(); err != nil {
|
2025-01-11 03:13:20 -06:00
|
|
|
log.Info("First time user?")
|
2025-01-10 23:55:12 -06:00
|
|
|
}
|
2025-01-11 03:13:20 -06:00
|
|
|
// attempt to load basket.pb
|
|
|
|
fruit := &Fruit{
|
2025-01-11 02:44:21 -06:00
|
|
|
Brand: "mom",
|
|
|
|
City: "New NewYork",
|
2025-01-11 03:13:20 -06:00
|
|
|
UPC: "2000",
|
2025-01-11 02:44:21 -06:00
|
|
|
}
|
2025-01-11 03:13:20 -06:00
|
|
|
pb.Fruits = append(pb.Fruits, fruit)
|
2025-01-11 02:44:21 -06:00
|
|
|
|
2025-01-11 03:13:20 -06:00
|
|
|
a := &Fruit{
|
2025-01-09 20:08:22 -06:00
|
|
|
Brand: "fry",
|
2025-01-11 03:13:20 -06:00
|
|
|
City: "the moon",
|
|
|
|
UPC: "2001",
|
2025-01-09 17:15:53 -06:00
|
|
|
}
|
2025-01-11 03:13:20 -06:00
|
|
|
pb.Fruits = append(pb.Fruits, a)
|
2025-01-09 17:15:53 -06:00
|
|
|
|
2025-01-11 03:13:20 -06:00
|
|
|
b := pb.getNewFruit()
|
|
|
|
pb.Fruits = append(pb.Fruits, b)
|
2025-01-09 20:08:22 -06:00
|
|
|
|
2025-01-11 03:13:20 -06:00
|
|
|
pb.saveBasket()
|
|
|
|
|
|
|
|
if err := pb.loadBasket(); err != nil {
|
|
|
|
badExit(err)
|
2025-01-09 20:08:22 -06:00
|
|
|
}
|
2025-01-11 03:13:20 -06:00
|
|
|
pb.printTable()
|
2025-01-09 20:08:22 -06:00
|
|
|
}
|
|
|
|
|
2025-01-11 03:13:20 -06:00
|
|
|
func (pb *Fruits) addFruits(i int) {
|
|
|
|
var x int
|
|
|
|
for x < i {
|
|
|
|
x += 1
|
|
|
|
b := pb.getNewFruit()
|
|
|
|
pb.Fruits = append(pb.Fruits, b)
|
2025-01-09 20:08:22 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-11 03:13:20 -06:00
|
|
|
func (pb *Fruits) getNewFruit() *Fruit {
|
|
|
|
counter += rand.Intn(10)
|
|
|
|
upc := fmt.Sprintf("%d", counter)
|
|
|
|
|
|
|
|
a := &Fruit{
|
|
|
|
Brand: "fry",
|
|
|
|
City: "the moon",
|
|
|
|
UPC: upc,
|
2025-01-09 20:08:22 -06:00
|
|
|
}
|
2025-01-11 03:13:20 -06:00
|
|
|
return a
|
2025-01-09 17:15:53 -06:00
|
|
|
}
|
|
|
|
|
2025-01-09 20:08:22 -06:00
|
|
|
func (pb *Fruits) saveBasket() error {
|
|
|
|
data, err := pb.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
w, err := os.OpenFile("basket.pb", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
w.Write(data)
|
|
|
|
w.Close()
|
|
|
|
log.Info("saved basket.pb ok")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2025-01-11 03:13:20 -06:00
|
|
|
func badExit(err error) {
|
|
|
|
log.Info("autogenpb error:", err)
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
|
2025-01-09 20:08:22 -06:00
|
|
|
func (pb *Fruits) loadBasket() error {
|
|
|
|
if pb == nil {
|
|
|
|
pb = new(Fruits)
|
|
|
|
}
|
|
|
|
data, err := os.ReadFile("basket.pb")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := pb.Unmarshal(data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Info("loaded basket.pb ok")
|
|
|
|
return nil
|
|
|
|
}
|
2025-01-11 03:13:20 -06:00
|
|
|
func (pb *Fruits) printTable() {
|
|
|
|
var count int
|
|
|
|
all := pb.All()
|
|
|
|
for all.Scan() {
|
|
|
|
tmp := all.Next()
|
|
|
|
count += 1
|
|
|
|
log.Printf("found %d %s %s %s\n", count, tmp.Brand, tmp.City, tmp.UPC)
|
|
|
|
if count > 10 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Printf("Total entries: %d\n", pb.Len())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pb *Fruits) sortTable(i int) {
|
|
|
|
var count int
|
|
|
|
all := pb.SortByBrand()
|
|
|
|
for all.Scan() {
|
|
|
|
tmp := all.Next()
|
|
|
|
count += 1
|
|
|
|
log.Printf("found %d %s %s\n", count, tmp.Brand, tmp.City)
|
|
|
|
if count > i {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Printf("Total entries: %d\n", pb.Len())
|
|
|
|
}
|