/
main.go
108 lines (99 loc) · 2.59 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
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"fmt"
"os"
"path"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/sgettys/bellman/pkg/hub"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
)
var (
conf, data, dataFile, name string
)
var rootCmd = &cobra.Command{
Use: "bellman",
Short: "Send messages to configured receivers",
Long: `Send messages to configured receivers.`,
Run: func(cmd *cobra.Command, args []string) {
execute()
},
}
func init() {
rootCmd.PersistentFlags().StringVarP(&conf, "config", "c", "", "config file path")
rootCmd.PersistentFlags().StringVarP(&data, "data", "d", "", "data to send")
rootCmd.PersistentFlags().StringVarP(&dataFile, "file", "f", "", "file with data to send")
rootCmd.PersistentFlags().StringVarP(&name, "name", "n", "", "receiver name to send to")
cobra.OnInitialize(initConfig)
}
func initConfig() {
var confDir string
log.Logger = log.With().Caller().Logger().Level(zerolog.DebugLevel)
if conf != "" {
confDir = path.Dir(conf)
} else {
confDir = defaultConfDir
}
mergeInConfig(confDir)
viper.AutomaticEnv()
}
func main() {
rootCmd.Execute()
}
func execute() {
var cfg hub.Config
var d []byte
err := yaml.Unmarshal([]byte(getViperAsString(viper.GetViper())), &cfg)
if err != nil {
log.Fatal().Err(err).Msg("cannot parse config to YAML")
}
if cfg.LogLevel != "" {
level, err := zerolog.ParseLevel(cfg.LogLevel)
if err != nil {
log.Fatal().Err(err).Str("level", cfg.LogLevel).Msg("Invalid log level")
}
log.Logger = log.Logger.Level(level)
}
if cfg.LogFormat == "json" {
fmt.Println(cfg.LogFormat)
// Defaults to JSON do nothing
} else if cfg.LogFormat == "" || cfg.LogFormat == "pretty" {
log.Logger = log.Logger.Output(zerolog.ConsoleWriter{
Out: os.Stdout,
NoColor: false,
TimeFormat: time.RFC3339,
})
}
registry := hub.SingleMessageRegistry{}
hub := hub.NewHub(&cfg, ®istry)
if dataFile != "" {
log.Debug().Msgf("Reading data from %s", dataFile)
dat, err := os.ReadFile(dataFile)
if err != nil {
log.Fatal().Err(err).Msg("Unable to read file")
}
d = []byte(dat)
} else {
d = []byte(data)
}
if name != "" {
hub.Registry.Send(name, d)
} else {
hub.Registry.SendAll(d)
}
// TODO: Implement plugin registration
// plugins, err := listPlugins("./.plugins", ".*.so")
// if err != nil {
// log.Debug().Err(err).Msg("Unable to load plugins")
// }
// if len(plugins) == 0 {
// log.Debug().Msg("No plugins found")
// }
// err = registerPlugins(hub, plugins)
// if err != nil {
// log.Fatal().Err(err).Msg("Unable to register criers from plugins")
// }
}