main.go

 1package main
 2
 3import (
 4	"fmt"
 5	"log"
 6	"os"
 7
 8	"github.com/andrinoff/email-cli/config"
 9	"github.com/andrinoff/email-cli/tui"
10)
11
12func main() {
13	cfg, err := config.LoadConfig()
14	if err != nil {
15		// If config file does not exist, run the login UI
16		if os.IsNotExist(err) {
17			fmt.Println("No configuration found. Starting setup...")
18			newCfg, err := tui.RunLogin()
19			if err != nil {
20				log.Fatalf("Could not run login UI: %v", err)
21			}
22			// If user quit the login screen without saving, exit
23			if newCfg == nil {
24				fmt.Println("\nSetup cancelled. Exiting.")
25				os.Exit(0)
26			}
27			cfg = newCfg
28		} else {
29			// For any other error loading the config, exit
30			log.Fatalf("Could not load config: %v", err)
31		}
32	}
33
34	// Now that we have a config, let the user choose what to do
35	choice, err := tui.RunChoice()
36	if err != nil {
37		log.Fatalf("Could not run choice UI: %v", err)
38	}
39
40	switch choice {
41	case "send":
42		fmt.Printf("Logged in as %s. Starting email composer...\n", cfg.Email)
43		if err := tui.RunComposer(cfg); err != nil {
44			log.Fatalf("Application error: %v", err)
45		}
46	case "inbox":
47		fmt.Printf("Logged in as %s. Opening inbox...\n", cfg.Email)
48		if err := tui.RunInbox(cfg); err != nil {
49			log.Fatalf("Application error: %v", err)
50		}
51	}
52}