Skip to content

Commit

Permalink
feat: Cannon
Browse files Browse the repository at this point in the history
  • Loading branch information
samcm committed Aug 31, 2023
1 parent 6cc4dbc commit ee93e01
Show file tree
Hide file tree
Showing 18 changed files with 3,250 additions and 1,619 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ sentry.yaml
server.yaml
discovery.yaml
mimicry.yaml
cannon.yaml
dist
GeoLite2-ASN.mmdb
GeoLite2-City.mmdb
Expand Down
86 changes: 86 additions & 0 deletions cmd/cannon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//nolint:dupl // disable duplicate code warning for cmds
package cmd

import (
"os"

"github.com/creasty/defaults"
"github.com/ethpandaops/xatu/pkg/cannon"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
yaml "gopkg.in/yaml.v3"
)

var (
cannonCfgFile string
)

// cannonCmd represents the cannon command
var cannonCmd = &cobra.Command{
Use: "cannon",
Short: "Runs Xatu in cannon mode.",
Long: `Runs Xatu in cannon mode, which means it will connect to xatu
server and process jobs like deriving events from beacon blocks..`,
Run: func(cmd *cobra.Command, args []string) {
initCommon()

log.WithField("location", cannonCfgFile).Info("Loading config")

config, err := loadcannonConfigFromFile(cannonCfgFile)
if err != nil {
log.Fatal(err)
}

log.Info("Config loaded")

logLevel, err := logrus.ParseLevel(config.LoggingLevel)
if err != nil {
log.WithField("logLevel", config.LoggingLevel).Fatal("invalid logging level")
}

log.SetLevel(logLevel)

cannon, err := cannon.New(cmd.Context(), log, config)
if err != nil {
log.Fatal(err)
}

if err := cannon.Start(cmd.Context()); err != nil {
log.Fatal(err)
}

log.Info("Xatu cannon exited - cya!")
},
}

func init() {
rootCmd.AddCommand(cannonCmd)

cannonCmd.Flags().StringVar(&cannonCfgFile, "config", "cannon.yaml", "config file (default is cannon.yaml)")
}

func loadcannonConfigFromFile(file string) (*cannon.Config, error) {
if file == "" {
file = "cannon.yaml"
}

config := &cannon.Config{}

if err := defaults.Set(config); err != nil {
return nil, err
}

yamlFile, err := os.ReadFile(file)

if err != nil {
return nil, err
}

type plain cannon.Config

if err := yaml.Unmarshal(yamlFile, (*plain)(config)); err != nil {
return nil, err
}

return config, nil
}
Loading

0 comments on commit ee93e01

Please sign in to comment.