2022-05-18 16:12:54 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2022 Vulcanize
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2022-04-19 21:09:59 +00:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-04-20 22:12:44 +00:00
|
|
|
"io"
|
2022-04-19 21:09:59 +00:00
|
|
|
"os"
|
|
|
|
|
2022-04-20 19:44:15 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-04-19 21:09:59 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-04-22 16:27:54 +00:00
|
|
|
cfgFile string
|
2022-04-19 21:09:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// rootCmd represents the base command when called without any subcommands
|
|
|
|
var rootCmd = &cobra.Command{
|
|
|
|
Use: "ipld-ethcl-indexer",
|
2022-04-20 22:12:44 +00:00
|
|
|
Short: "This application will keep track of all BeaconState's and SignedBeaconBlock's on the Beacon Chain.",
|
|
|
|
Long: `This is an application that will capture the BeaconState's and SignedBeaconBlock's on the Beacon Chain.
|
2022-04-19 21:09:59 +00:00
|
|
|
It can either do this will keeping track of head, or backfilling historic data.`,
|
2022-04-20 19:44:15 +00:00
|
|
|
PersistentPreRun: initFuncs,
|
2022-04-19 21:09:59 +00:00
|
|
|
// Uncomment the following line if your bare application
|
|
|
|
// has an action associated with it:
|
|
|
|
// Run: func(cmd *cobra.Command, args []string) {},
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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() {
|
|
|
|
err := rootCmd.Execute()
|
|
|
|
if err != nil {
|
2022-05-24 20:18:55 +00:00
|
|
|
fmt.Println("Err when executing rootCmd", err)
|
2022-04-19 21:09:59 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 19:44:15 +00:00
|
|
|
// Prerun for Cobra
|
|
|
|
func initFuncs(cmd *cobra.Command, args []string) {
|
2022-04-20 22:12:44 +00:00
|
|
|
logFormat()
|
2022-04-22 16:27:54 +00:00
|
|
|
if err := logFile(); err != nil {
|
|
|
|
log.WithField("err", err).Error("Could not set log file")
|
|
|
|
}
|
2022-04-20 19:44:15 +00:00
|
|
|
if err := logLevel(); err != nil {
|
2022-04-20 22:12:44 +00:00
|
|
|
log.WithField("err", err).Error("Could not set log level")
|
2022-04-20 19:44:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the log level for the application
|
|
|
|
func logLevel() error {
|
2022-04-22 16:27:54 +00:00
|
|
|
err := viper.BindEnv("log.level", "LOGRUS_LEVEL")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-20 19:44:15 +00:00
|
|
|
lvl, err := log.ParseLevel(viper.GetString("log.level"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.SetLevel(lvl)
|
|
|
|
if lvl > log.InfoLevel {
|
|
|
|
log.SetReportCaller(true)
|
|
|
|
}
|
|
|
|
log.Info("Log level set to ", lvl.String())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 22:12:44 +00:00
|
|
|
// Create a log file
|
2022-04-22 16:27:54 +00:00
|
|
|
func logFile() error {
|
|
|
|
err := viper.BindEnv("log.file", "LOGRUS_FILE")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-20 22:12:44 +00:00
|
|
|
logfile := viper.GetString("log.file")
|
|
|
|
if logfile != "" {
|
|
|
|
file, err := os.OpenFile(logfile,
|
|
|
|
os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
|
|
|
if err == nil {
|
2022-04-22 16:27:54 +00:00
|
|
|
if viper.GetBool("log.output") {
|
|
|
|
log.Infof("Directing output to %s", logfile)
|
|
|
|
mw := io.MultiWriter(os.Stdout, file)
|
|
|
|
log.SetOutput(mw)
|
|
|
|
} else {
|
|
|
|
log.SetOutput(file)
|
|
|
|
}
|
2022-04-20 22:12:44 +00:00
|
|
|
} else {
|
|
|
|
log.SetOutput(os.Stdout)
|
|
|
|
log.Info("Failed to log to file, using default stdout")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.SetOutput(os.Stdout)
|
|
|
|
}
|
2022-04-22 16:27:54 +00:00
|
|
|
return nil
|
2022-04-20 22:12:44 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 22:23:21 +00:00
|
|
|
// Format the logger
|
2022-04-20 22:12:44 +00:00
|
|
|
func logFormat() {
|
|
|
|
logFormat := viper.GetString("log.format")
|
|
|
|
|
|
|
|
if logFormat == "json" {
|
|
|
|
log.SetFormatter(&log.JSONFormatter{})
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 21:09:59 +00:00
|
|
|
func init() {
|
|
|
|
cobra.OnInitialize(initConfig)
|
|
|
|
|
|
|
|
// Here you will define your flags and configuration settings.
|
|
|
|
// Cobra supports persistent flags, which, if defined here,
|
|
|
|
// will be global for your application.
|
|
|
|
|
|
|
|
// Optional Flags
|
|
|
|
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.ipld-ethcl-indexer.yaml)")
|
2022-04-20 22:12:44 +00:00
|
|
|
rootCmd.PersistentFlags().String("log.level", log.InfoLevel.String(), "log level (trace, debug, info, warn, error, fatal, panic)")
|
|
|
|
rootCmd.PersistentFlags().String("log.file", "ipld-ethcl-indexer.log", "file path for logging")
|
2022-04-22 16:27:54 +00:00
|
|
|
rootCmd.PersistentFlags().Bool("log.output", true, "Should we log to STDOUT")
|
2022-04-20 22:12:44 +00:00
|
|
|
rootCmd.PersistentFlags().String("log.format", "json", "json or text")
|
2022-04-19 21:09:59 +00:00
|
|
|
|
|
|
|
// Bind Flags with Viper
|
2022-04-20 19:44:15 +00:00
|
|
|
// Optional
|
2022-04-22 16:27:54 +00:00
|
|
|
err := viper.BindPFlag("log.level", rootCmd.PersistentFlags().Lookup("log.level"))
|
|
|
|
exitErr(err)
|
|
|
|
err = viper.BindPFlag("log.file", rootCmd.PersistentFlags().Lookup("log.file"))
|
|
|
|
exitErr(err)
|
|
|
|
err = viper.BindPFlag("log.output", rootCmd.PersistentFlags().Lookup("log.output"))
|
|
|
|
exitErr(err)
|
|
|
|
err = viper.BindPFlag("log.format", rootCmd.PersistentFlags().Lookup("log.format"))
|
|
|
|
exitErr(err)
|
2022-04-19 21:09:59 +00:00
|
|
|
|
|
|
|
// Cobra also supports local flags, which will only run
|
|
|
|
// when this action is called directly.
|
|
|
|
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// initConfig reads in config file and ENV variables if set.
|
|
|
|
func initConfig() {
|
|
|
|
if cfgFile != "" {
|
|
|
|
// Use config file from the flag.
|
|
|
|
viper.SetConfigFile(cfgFile)
|
|
|
|
} else {
|
|
|
|
// Find home directory.
|
|
|
|
home, err := os.UserHomeDir()
|
|
|
|
cobra.CheckErr(err)
|
|
|
|
|
|
|
|
// Search config in home directory with name ".ipld-ethcl-indexer" (without extension).
|
|
|
|
viper.AddConfigPath(home)
|
|
|
|
viper.SetConfigType("yaml")
|
|
|
|
viper.SetConfigName(".ipld-ethcl-indexer")
|
|
|
|
}
|
|
|
|
|
|
|
|
viper.AutomaticEnv() // read in environment variables that match
|
|
|
|
|
|
|
|
// If a config file is found, read it in.
|
|
|
|
if err := viper.ReadInConfig(); err == nil {
|
|
|
|
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
|
|
|
|
}
|
|
|
|
}
|