-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.go
More file actions
63 lines (53 loc) · 1.75 KB
/
commands.go
File metadata and controls
63 lines (53 loc) · 1.75 KB
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
package main
import (
"github.com/mitchellh/cli"
"github.com/voronelf/logview/command"
"github.com/voronelf/logview/core"
"github.com/voronelf/logview/filter"
"github.com/voronelf/logview/formatter"
"github.com/voronelf/logview/provider"
"github.com/voronelf/logview/settings"
"os"
"os/signal"
"syscall"
)
func provideCommandsDependenciesInDI(di *core.DIContainer, ui cli.Ui) {
di.Provide("CliUi", ui)
var rowProvider core.RowProvider = provider.NewRowProvider()
di.Provide("RowProvider", rowProvider)
var factory core.FilterFactory = filter.NewFactory()
di.Provide("FilterFactory", factory)
var f core.Formatter = formatter.NewCliColor()
di.Provide("FormatterCliColor", f)
var store core.Settings = settings.NewStore()
di.Provide("Settings", store)
}
func getCommands(di *core.DIContainer) map[string]cli.CommandFactory {
return map[string]cli.CommandFactory{
"watch": newCmdFactory(di, &command.Watch{ShutdownCh: getShutdownCh(), Stdin: os.Stdin}),
"tail": newCmdFactory(di, &command.Tail{ShutdownCh: getShutdownCh()}),
"tpl": newCmdFactory(di, &command.Tpl{}),
"tpl list": newCmdFactory(di, &command.TplList{}),
"tpl save": newCmdFactory(di, &command.TplSave{}),
}
}
func newCmdFactory(di *core.DIContainer, cmd cli.Command) cli.CommandFactory {
di.Provide("", cmd)
return func() (cli.Command, error) { return cmd, nil }
}
var shutdownCh chan struct{}
// getShutdownCh returns a channel that will be closed for every interrupt or SIGTERM received.
func getShutdownCh() <-chan struct{} {
if shutdownCh == nil {
shutdownCh = make(chan struct{})
go func() {
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)
for {
<-signalCh
close(shutdownCh)
}
}()
}
return shutdownCh
}