-
Notifications
You must be signed in to change notification settings - Fork 1
/
flot.go
178 lines (143 loc) · 3.42 KB
/
flot.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package flot
import (
"io"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"
"github.com/fatih/color"
"github.com/flothq/flot/cmd"
"github.com/flothq/flot/core"
"github.com/glebarez/sqlite"
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats.go"
"github.com/spf13/cobra"
"gorm.io/gorm"
)
var _ core.App = (*Flot)(nil)
var Version = "(untracked)"
type appWrapper struct {
core.App
}
type Flot struct {
*appWrapper
devFlag bool
dataDirFlag string
encryptionKeyFlag string
RootCmd *cobra.Command
}
type Config struct {
DefaultDev bool
DefaultDataDir string
DefaultEncryptionKey string
NatsURL string
Db *gorm.DB
}
func New() *Flot {
baseDir, isUsingGoRun := inspectRuntime()
dataDir := filepath.Join(baseDir, "flot_data")
opts := &server.Options{
JetStream: true,
StoreDir: dataDir + "/nats",
}
ns, _ := server.NewServer(opts)
go ns.Start()
if !ns.ReadyForConnections(5 * time.Second) {
panic("not ready for connection")
}
db, err := gorm.Open(sqlite.Open(filepath.Join(dataDir, "data.db")), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
return NewWithConfig(Config{
DefaultDev: isUsingGoRun,
NatsURL: ns.ClientURL(),
Db: db,
})
}
func NewWithConfig(config Config) *Flot {
if config.DefaultDataDir == "" {
baseDir, _ := inspectRuntime()
config.DefaultDataDir = filepath.Join(baseDir, "flot_data")
}
nc, err := nats.Connect(config.NatsURL)
if err != nil {
panic(err)
}
flot := &Flot{
RootCmd: &cobra.Command{
Use: filepath.Base(os.Args[0]),
Short: "Flot CLI",
Version: Version,
FParseErrWhitelist: cobra.FParseErrWhitelist{
UnknownFlags: true,
},
CompletionOptions: cobra.CompletionOptions{
DisableDefaultCmd: true,
},
},
devFlag: config.DefaultDev,
dataDirFlag: config.DefaultDataDir,
encryptionKeyFlag: config.DefaultEncryptionKey,
}
flot.RootCmd.SetErr(newErrWriter())
flot.appWrapper = &appWrapper{core.NewBaseApp(core.BaseAppConfig{
IsDev: flot.devFlag,
DataDir: flot.dataDirFlag,
EncryptionEnv: flot.encryptionKeyFlag,
Nc: nc,
Db: config.Db,
})}
return flot
}
func (f *Flot) Start() error {
f.RootCmd.AddCommand(cmd.NewServeCommand(f.appWrapper, true))
return f.Execute()
}
func (f *Flot) Execute() (err error) {
done := make(chan bool, 1)
// listen for interrupt signal to gracefully shutdown the application
go func() {
sigch := make(chan os.Signal, 1)
signal.Notify(sigch, os.Interrupt, syscall.SIGTERM)
<-sigch
done <- true
}()
// execute the root command
go func() {
// note: leave to the commands to decide whether to print their error
f.RootCmd.Execute()
done <- true
}()
<-done
return
}
func inspectRuntime() (baseDir string, withGoRun bool) {
if strings.HasPrefix(os.Args[0], os.TempDir()) {
// probably ran with go run
withGoRun = true
baseDir, _ = os.Getwd()
} else {
// probably ran with go build
withGoRun = false
baseDir = filepath.Dir(os.Args[0])
}
return
}
func newErrWriter() *coloredWriter {
return &coloredWriter{
w: os.Stderr,
c: color.New(color.FgRed),
}
}
type coloredWriter struct {
w io.Writer
c *color.Color
}
func (colored *coloredWriter) Write(p []byte) (n int, err error) {
colored.c.SetWriter(colored.w)
defer colored.c.UnsetWriter(colored.w)
return colored.c.Print(string(p))
}