-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
95 lines (85 loc) · 2.04 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"flag"
"fmt"
"log"
"os"
"github.com/RocketChat/rocketchat-tui/ui"
tea "github.com/charmbracelet/bubbletea"
"github.com/joho/godotenv"
)
var (
debug bool
prod bool
url string
)
// To load environment variables from .env file
func initialiseEnvVariable() error {
err := godotenv.Load(".env")
if err != nil {
return err
}
return nil
}
// It will check for flag arguments.
// If debug flag is set true all logs will be written in debug.log file.
// If prod flag is set true TUI will use Production server url for all rest and realtime function calls.
// If connection url is provided while starting TUI as flag value use that URL.
// Server Url is set in the model state and intial model state of the TUI will be returned for Tea to start TUI
func createModel() (*ui.Model, *os.File) {
var loggerFile *os.File
var err error
var sUrl string
if debug {
loggerFile, err = tea.LogToFile("debug.log", "debug")
if err != nil {
fmt.Println("Error setting up logger")
}
}
if prod {
sUrl = os.Getenv("PROD_SERVER_URL")
} else if url != "" {
sUrl = url
} else {
sUrl = os.Getenv("DEV_SERVER_URL")
}
return ui.IntialModelState(sUrl), loggerFile
}
// It intialises environment variables to pick them from .env file.
// It defines all the flags and parse their values.
// Initial model state with all required methods is used to start the TUI..
func main() {
err := initialiseEnvVariable()
if err != nil {
log.Println("Environment variable file not found", err)
panic(err)
}
flag.BoolVar(&debug,
"debug",
false,
"passing this flag will allow writing debug output to debug.log",
)
flag.BoolVar(&prod,
"prod",
false,
"passing this flag will use production server url for connecting",
)
flag.StringVar(&url,
"url",
"",
"user can pass sever url in it default is loacalhost",
)
flag.Parse()
model, logger := createModel()
if logger != nil {
defer logger.Close()
}
tui := tea.NewProgram(
model,
tea.WithAltScreen(),
)
if err := tui.Start(); err != nil {
log.Fatal(err)
os.Exit(1)
}
}