2018-02-02 22:48:57 +01:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2018-05-15 02:55:45 +02:00
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
2018-02-02 22:48:57 +01:00
|
|
|
"os"
|
2020-10-06 00:07:06 +02:00
|
|
|
"strings"
|
2018-02-02 22:48:57 +01:00
|
|
|
|
2021-07-20 02:09:14 +02:00
|
|
|
"github.com/lbryio/reflector.go/updater"
|
|
|
|
|
2019-11-14 01:11:35 +01:00
|
|
|
"github.com/lbryio/lbry.go/v2/dht"
|
|
|
|
"github.com/lbryio/lbry.go/v2/extras/errors"
|
|
|
|
"github.com/lbryio/lbry.go/v2/extras/util"
|
2018-05-15 02:55:45 +02:00
|
|
|
|
2018-08-15 16:55:24 +02:00
|
|
|
"github.com/johntdyer/slackrus"
|
2018-06-25 22:49:40 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
2018-02-02 22:48:57 +01:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// Config is the base configuration for Prism when no sub commands are used.
|
2018-02-02 22:48:57 +01:00
|
|
|
type Config struct {
|
|
|
|
AwsID string `json:"aws_id"`
|
|
|
|
AwsSecret string `json:"aws_secret"`
|
|
|
|
BucketRegion string `json:"bucket_region"`
|
|
|
|
BucketName string `json:"bucket_name"`
|
|
|
|
DBConn string `json:"db_conn"`
|
2018-08-15 16:55:24 +02:00
|
|
|
SlackHookURL string `json:"slack_hook_url"`
|
2021-10-30 00:21:58 +02:00
|
|
|
SlackChannel string `json:"slack_channel"`
|
2018-08-30 22:50:46 +02:00
|
|
|
UpdateBinURL string `json:"update_bin_url"`
|
|
|
|
UpdateCmd string `json:"update_cmd"`
|
2018-02-02 22:48:57 +01:00
|
|
|
}
|
|
|
|
|
2018-06-25 22:49:40 +02:00
|
|
|
var verbose []string
|
2018-06-25 21:48:57 +02:00
|
|
|
|
|
|
|
const (
|
2018-06-25 22:49:40 +02:00
|
|
|
verboseAll = "all"
|
|
|
|
verboseDHT = "dht"
|
2018-07-17 23:19:03 +02:00
|
|
|
verboseNodeFinder = "node_finder"
|
2018-06-25 21:48:57 +02:00
|
|
|
)
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
var conf string
|
|
|
|
var globalConfig Config
|
2018-02-02 22:48:57 +01:00
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
var rootCmd = &cobra.Command{
|
2018-08-15 16:55:24 +02:00
|
|
|
Use: "prism",
|
|
|
|
Short: "Prism is a single entry point application with multiple sub modules which can be leveraged individually or together",
|
|
|
|
PersistentPreRun: preRun,
|
2018-02-02 22:48:57 +01:00
|
|
|
// Uncomment the following line if your bare application
|
|
|
|
// has an action associated with it:
|
|
|
|
// Run: func(cmd *cobra.Command, args []string) { },
|
|
|
|
}
|
|
|
|
|
2018-05-15 02:55:45 +02:00
|
|
|
func init() {
|
2018-06-25 22:49:40 +02:00
|
|
|
rootCmd.PersistentFlags().StringSliceVarP(&verbose, "verbose", "v", []string{}, "Verbose logging for specific components")
|
2020-05-09 05:14:26 +02:00
|
|
|
rootCmd.PersistentFlags().StringVar(&conf, "conf", "config.json", "Path to config. Use 'none' to disable")
|
2018-05-15 02:55:45 +02:00
|
|
|
}
|
|
|
|
|
2018-02-02 22:48:57 +01:00
|
|
|
// Execute adds all child commands to the root command and sets flags appropriately.
|
|
|
|
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
|
|
|
func Execute() {
|
2018-06-15 04:30:37 +02:00
|
|
|
err := rootCmd.Execute()
|
|
|
|
if err != nil {
|
2018-06-25 22:49:40 +02:00
|
|
|
logrus.Errorln(err)
|
2018-02-02 22:48:57 +01:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-15 16:55:24 +02:00
|
|
|
func preRun(cmd *cobra.Command, args []string) {
|
|
|
|
debugLogger := logrus.New()
|
|
|
|
debugLogger.SetLevel(logrus.DebugLevel)
|
|
|
|
debugLogger.SetOutput(os.Stderr)
|
|
|
|
|
|
|
|
if util.InSlice(verboseAll, verbose) {
|
2020-10-06 00:07:06 +02:00
|
|
|
logrus.Info("global verbose logging enabled")
|
2018-08-15 16:55:24 +02:00
|
|
|
logrus.SetLevel(logrus.DebugLevel)
|
|
|
|
verbose = []string{verboseDHT, verboseNodeFinder}
|
2020-10-06 00:07:06 +02:00
|
|
|
} else if len(verbose) > 0 {
|
|
|
|
logrus.Infof("verbose logging enabled for: %s", strings.Join(verbose, ", "))
|
2018-08-15 16:55:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, debugType := range verbose {
|
|
|
|
switch debugType {
|
|
|
|
case verboseDHT:
|
|
|
|
dht.UseLogger(debugLogger)
|
|
|
|
case verboseNodeFinder:
|
|
|
|
dht.NodeFinderUseLogger(debugLogger)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if conf == "" {
|
|
|
|
logrus.Errorln("--conf flag required")
|
|
|
|
os.Exit(1)
|
|
|
|
} else if conf != "none" {
|
|
|
|
globalConfig, err = loadConfig(conf)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if globalConfig.SlackHookURL != "" {
|
|
|
|
hook := &slackrus.SlackrusHook{
|
|
|
|
HookURL: globalConfig.SlackHookURL,
|
|
|
|
AcceptedLevels: slackrus.LevelThreshold(logrus.InfoLevel),
|
2021-10-30 00:21:58 +02:00
|
|
|
Channel: globalConfig.SlackChannel,
|
2018-08-15 16:55:24 +02:00
|
|
|
//IconEmoji: ":ghost:",
|
|
|
|
//Username: "reflector.go",
|
|
|
|
}
|
|
|
|
//logrus.SetFormatter(&logrus.JSONFormatter{})
|
|
|
|
logrus.AddHook(hook)
|
|
|
|
debugLogger.AddHook(hook)
|
|
|
|
}
|
2018-08-30 22:50:46 +02:00
|
|
|
|
|
|
|
if globalConfig.UpdateBinURL != "" {
|
|
|
|
if globalConfig.UpdateCmd == "" {
|
|
|
|
logrus.Warnln("update_cmd is empty in conf file")
|
|
|
|
}
|
|
|
|
logrus.Println("starting update checker")
|
|
|
|
go updater.Run(globalConfig.UpdateBinURL, globalConfig.UpdateCmd)
|
|
|
|
}
|
2018-08-15 16:55:24 +02:00
|
|
|
}
|
|
|
|
|
2018-02-02 22:48:57 +01:00
|
|
|
func checkErr(err error) {
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2018-02-08 19:33:52 +01:00
|
|
|
|
|
|
|
func argFuncs(funcs ...cobra.PositionalArgs) cobra.PositionalArgs {
|
|
|
|
return func(cmd *cobra.Command, args []string) error {
|
|
|
|
for _, f := range funcs {
|
|
|
|
err := f(cmd, args)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2018-05-15 02:55:45 +02:00
|
|
|
|
|
|
|
func loadConfig(path string) (Config, error) {
|
|
|
|
var c Config
|
|
|
|
|
|
|
|
raw, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return c, errors.Err("config file not found")
|
|
|
|
}
|
2020-02-25 21:49:51 +01:00
|
|
|
return c, errors.Err(err)
|
2018-05-15 02:55:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(raw, &c)
|
2020-02-25 21:49:51 +01:00
|
|
|
return c, errors.Err(err)
|
2018-05-15 02:55:45 +02:00
|
|
|
}
|
2020-01-09 04:12:40 +01:00
|
|
|
|
|
|
|
func mustGetFlagString(cmd *cobra.Command, name string) string {
|
|
|
|
v, err := cmd.Flags().GetString(name)
|
|
|
|
checkErr(err)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
func mustGetFlagInt64(cmd *cobra.Command, name string) int64 {
|
|
|
|
v, err := cmd.Flags().GetInt64(name)
|
|
|
|
checkErr(err)
|
|
|
|
return v
|
|
|
|
}
|
2021-04-02 20:30:36 +02:00
|
|
|
|
2021-07-24 00:08:13 +02:00
|
|
|
//func mustGetFlagBool(cmd *cobra.Command, name string) bool {
|
|
|
|
// v, err := cmd.Flags().GetBool(name)
|
|
|
|
// checkErr(err)
|
|
|
|
// return v
|
|
|
|
//}
|