package main import ( "bufio" "errors" "fmt" "log" "os" "strconv" "time" "gopkg.in/yaml.v2" ) type ( DB interface { HistoryOf(string) map[string][]History Next(string, string) time.Time Question(string) Question LastAnswer(string, string) Answer Answer(string) Answer PushAnswer(string, string, string, bool) error } Question struct { Q string Tags []string } Answer struct { Q string A string TS int64 Author string } History struct { A string TS int64 Pass bool } Duration time.Duration ) func main() { if err := Main(); err != nil { panic(err) } } func Main() error { db, err := NewDB() if err != nil { return err } user := os.Getenv("USER") for q, history := range db.HistoryOf(user) { log.Printf("%s/%s/%+v", user, q, history) if time.Until(db.Next(user, q)) > 0 { continue } question := db.Question(q) fmt.Printf("> Q: %s\n", question.Q) fmt.Printf("> %+v\n\t", question.Tags) response := readline() if lastAnswer := db.Answer(db.LastAnswer(user, q).A); lastAnswer.A != "" { fmt.Printf("> Last time, you responded:\n\t%s\n", lastAnswer.A) } fmt.Printf("> Did you pass this time? [Yn] ") pass := readline() == "n" if err := db.PushAnswer(user, q, response, pass); err != nil { return err } fmt.Println() } if b, _ := yaml.Marshal(db); len(b) > 0 { log.Printf("%s\n", b) } return nil } func readline() string { reader := bufio.NewReader(os.Stdin) text, _ := reader.ReadString('\n') return text } func NewDB() (DB, error) { var db db if b, err := os.ReadFile(os.Getenv("DB")); err != nil { return nil, err } else if err := yaml.Unmarshal(b, &db); err != nil { return nil, err } return db, nil } func (d Duration) MarshalYAML() (interface{}, error) { return time.Duration(d).String(), nil } func (d *Duration) UnmarshalYAML(unmarshal func(interface{}) error) error { var s string if err := unmarshal(&s); err != nil { return err } count := "" var ttl time.Duration for i := range s { if s[i] < '0' || s[i] > '9' { n, err := strconv.Atoi(count) if err != nil { return err } count += s[i : i+1] switch s[i] { case 'w': count = fmt.Sprintf("%dh", n*24*7) case 'd': count = fmt.Sprintf("%dh", n*24) } d, err := time.ParseDuration(count) if err != nil { return err } ttl += d count = "" } else { count += s[i : i+1] } } if count != "" { return errors.New(count) } return nil }