~edwargix/tallyard

2dc306b017d2dff9756d6063945d7d8b9035e04b — David Florness 2 years ago e7cfd1c
Use logger and close TUI (if present) when handling panics
2 files changed, 65 insertions(+), 10 deletions(-)

M cmd/tallyard/main.go
M ui/tui.go
M cmd/tallyard/main.go => cmd/tallyard/main.go +10 -10
@@ 41,7 41,7 @@ func main() {

	client, err := mautrix.NewClient(authInfo.Homeserver, authInfo.UserID, authInfo.AccessToken)
	if err != nil {
		panic(err)
		log.Panic(err)
	}

	setDeviceName(client, authInfo.DeviceID)


@@ 49,7 49,7 @@ func main() {
	// setup the elections store
	electionsMap, err := getElections(authInfo.UserID)
	if err != nil {
		panic(err)
		log.Panic(err)
	}
	client.Store = matrix.NewTallyardStore(electionsMap)
	defer electionsMap.Save()


@@ 62,19 62,19 @@ func main() {
	go func() {
		res, err := client.CreateFilter(electionFilter)
		if err != nil {
			panic(err)
			log.Panic(err)
		}
		client.Store.SaveFilterID(client.UserID, res.FilterID)
		err = client.Sync()
		if err != nil {
			panic(err)
			log.Panic(err)
		}
	}()

	// select a room
	room, err := ui.RoomListTUI(client.Store.(*matrix.TallyardStore), client.UserID, syncer)
	if err != nil {
		panic(err)
		log.Panic(err)
	}
	if room == nil {
		// no room selected; user likely hit C-c


@@ 84,7 84,7 @@ func main() {
	// select an election
	el, err := ui.RoomTUI(client, room, electionsMap, syncer)
	if err != nil {
		panic(err)
		log.Panic(err)
	}
	if el == nil || el.LocalVoter == nil {
		// no election selected; user likely hit C-c


@@ 94,7 94,7 @@ func main() {
	// wait for election to start if needed
	err = ui.ElectionWaitTUI(client, el, electionsMap.EventStore)
	if err != nil {
		panic(err)
		log.Panic(err)
	}
	if el.StartID == nil || el.FinalJoinIDs == nil {
		// election never started; user likely hit C-c


@@ 117,7 117,7 @@ func main() {
	if el.LocalVoter != nil && el.LocalVoter.Poly == nil {
		ballot, err := ui.VoteTUI(el.Candidates)
		if err != nil {
			panic(err)
			log.Panic(err)
		}
		if ballot == nil {
			// user likely hit C-c


@@ 135,7 135,7 @@ func main() {
	if el.LocalVoter != nil && el.LocalVoter.EvalsID == nil {
		err = el.SendEvals(client, electionsMap.EventStore)
		if err != nil {
			panic(err)
			log.Panic(err)
		}
	}



@@ 149,7 149,7 @@ func main() {
		}
		err = el.SendSum(client, electionsMap.EventStore)
		if err != nil {
			panic(err)
			log.Panic(err)
		}
	}


M ui/tui.go => ui/tui.go +55 -0
@@ 2,6 2,7 @@ package ui

import (
	"fmt"
	"runtime/debug"
	"sort"
	"strconv"
	"strings"


@@ 23,6 24,15 @@ import (
// store is already properly saving rooms
func RoomListTUI(store *matrix.TallyardStore, localUserID id.UserID, syncer mautrix.ExtensibleSyncer) (*election.Room, error) {
	app := newTallyardApplication()
	defer func() {
		if r := recover(); r != nil {
			if app.alive {
				app.Stop()
			}
			log.Panicf("RoomListTUI panicked! panic=%s\n%s", r, debug.Stack())
		}
	}()

	list := tview.NewList().AddItem("syncing...", "", 0, nil)
	list.SetTitle("Select a room").SetBorder(true)
	var rooms []*roomWithTitle


@@ 170,6 180,15 @@ func (roomI *roomWithTitle) cmp(roomJ *roomWithTitle) bool {

func RoomTUI(client *mautrix.Client, room *election.Room, electionsMap *election.ElectionsMap, syncer mautrix.ExtensibleSyncer) (el *election.Election, err error) {
	app := newTallyardApplication()
	defer func() {
		if r := recover(); r != nil {
			if app.alive {
				app.Stop()
			}
			log.Panicf("RoomTUI panicked! panic=%s\n%s", r, debug.Stack())
		}
	}()

	list := tview.NewList().
		AddItem("Create Election", "start a new election in this room", 0, nil)
	list.SetTitle("Select election").SetBorder(true)


@@ 261,6 280,15 @@ func RoomTUI(client *mautrix.Client, room *election.Room, electionsMap *election

func electionConfirmation(el *election.Election) (shouldJoin bool) {
	app := newTallyardApplication()
	defer func() {
		if r := recover(); r != nil {
			if app.alive {
				app.Stop()
			}
			log.Panicf("electionConfirmation panicked! panic=%s\n%s", r, debug.Stack())
		}
	}()

	var text string

	el.RLock()


@@ 301,6 329,15 @@ func CreateElectionTUI() (title string, candidates []election.Candidate) {
	var form *tview.Form
	n := 2
	app := newTallyardApplication()
	defer func() {
		if r := recover(); r != nil {
			if app.alive {
				app.Stop()
			}
			log.Panicf("CreateElectionTUI panicked! panic=%s\n%s", r, debug.Stack())
		}
	}()

	plus := func() {
		if n < 5 {
			form.AddInputField(fmt.Sprintf("%d.", n+1), "", 50, nil, nil)


@@ 351,6 388,15 @@ func ElectionWaitTUI(client *mautrix.Client, el *election.Election, eventStore *
	frame := tview.NewFrame(votersTextView)
	frame.SetTitle(el.Title).SetBorder(true)
	app := newTallyardApplication()
	defer func() {
		if r := recover(); r != nil {
			if app.alive {
				app.Stop()
			}
			log.Panicf("ElectionWaitTUI panicked! panic=%s\n%s", r, debug.Stack())
		}
	}()

	el.RLock()
	if el.LocalVoter != nil && el.CreateEvt.Sender == el.LocalVoter.JoinEvt.Sender {
		frame.AddText("Press enter to start the election", false, tview.AlignCenter, tcell.ColorWhite)


@@ 443,6 489,15 @@ func ElectionWaitTUI(client *mautrix.Client, el *election.Election, eventStore *
// displays a voting UI to the user and returns the encoded ballot
func VoteTUI(candidates []election.Candidate) (ballot *[][]byte, err error) {
	app := newTallyardApplication()
	defer func() {
		if r := recover(); r != nil {
			if app.alive {
				app.Stop()
			}
			log.Panicf("VoteTUI panicked! panic=%s\n%s", r, debug.Stack())
		}
	}()

	form := tview.NewForm()
	form.SetTitle("Vote").SetBorder(true)