Commit 3b95b924 authored by Will Cory's avatar Will Cory Committed by GitHub

Merge pull request #5917 from ethereum-optimism/willc/cli

feat(indexer): Add cli app
parents 513db8f0 ba38b15d
package cli
import (
"fmt"
"os"
"github.com/ethereum-optimism/optimism/indexer/config"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
"github.com/urfave/cli/v2"
)
type Cli struct {
GitVersion string
GitCommit string
GitDate string
app *cli.App
Flags []cli.Flag
}
func runIndexer(ctx *cli.Context) error {
configPath := ctx.String(ConfigFlag.Name)
conf, err := config.LoadConfig(configPath)
fmt.Println(conf)
if err != nil {
log.Crit("Failed to load config", "message", err)
}
// finish me
return nil
}
func runApi(ctx *cli.Context) error {
configPath := ctx.String(ConfigFlag.Name)
conf, err := config.LoadConfig(configPath)
fmt.Println(conf)
if err != nil {
log.Crit("Failed to load config", "message", err)
}
// finish me
return nil
}
var (
ConfigFlag = &cli.StringFlag{
Name: "config",
Value: "./indexer.toml",
Aliases: []string{"c"},
Usage: "path to config file",
EnvVars: []string{"INDEXER_CONFIG"},
}
// Not used yet. Use this flag to run legacy app instead
// Remove me after indexer is released
IndexerRefreshFlag = &cli.BoolFlag{
Name: "indexer-refresh",
Value: false,
Aliases: []string{"i"},
Usage: "run new unreleased indexer by passing in flag",
EnvVars: []string{"INDEXER_REFRESH"},
}
)
// make a instance method on Cli called Run that runs cli
// and returns an error
func (c *Cli) Run(args []string) error {
return c.app.Run(args)
}
func NewCli(GitVersion string, GitCommit string, GitDate string) *Cli {
log.Root().SetHandler(
log.LvlFilterHandler(
log.LvlInfo,
log.StreamHandler(os.Stdout, log.TerminalFormat(true)),
),
)
flags := []cli.Flag{
ConfigFlag,
}
app := &cli.App{
Version: fmt.Sprintf("%s-%s", GitVersion, params.VersionWithCommit(GitCommit, GitDate)),
Description: "An indexer of all optimism events with a serving api layer",
Commands: []*cli.Command{
{
Name: "api",
Flags: flags,
Description: "Runs the api service",
Action: runApi,
},
{
Name: "indexer",
Flags: flags,
Description: "Runs the indexing service",
Action: runIndexer,
},
},
}
return &Cli{
app: app,
Flags: flags,
}
}
package main package main
import ( import (
"fmt"
"os" "os"
"github.com/ethereum-optimism/optimism/indexer/cli"
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
"github.com/urfave/cli"
"github.com/ethereum-optimism/optimism/indexer"
"github.com/ethereum-optimism/optimism/indexer/config"
"github.com/ethereum-optimism/optimism/indexer/flags"
) )
var ( var (
...@@ -20,34 +14,8 @@ var ( ...@@ -20,34 +14,8 @@ var (
) )
func main() { func main() {
// Set up logger with a default INFO level in case we fail to parse flags. app := cli.NewCli(GitVersion, GitCommit, GitDate)
// Otherwise the final crtiical log won't show what the parsing error was.
log.Root().SetHandler(
log.LvlFilterHandler(
log.LvlInfo,
log.StreamHandler(os.Stdout, log.TerminalFormat(true)),
),
)
// TODO https://linear.app/optimism/issue/DX-55/api-implement-rest-api-with-mocked-data
// don't hardcode this
conf, err := config.LoadConfig("../../indexer.toml")
if err != nil {
log.Crit("Failed to load config", "message", err)
}
log.Debug("Loaded config", "config", conf)
app := cli.NewApp()
app.Flags = []cli.Flag{flags.LogLevelFlag, flags.L1EthRPCFlag, flags.L2EthRPCFlag, flags.DBNameFlag}
app.Version = fmt.Sprintf("%s-%s", GitVersion, params.VersionWithCommit(GitCommit, GitDate))
app.Name = "indexer"
app.Usage = "Indexer Service"
app.Description = "Service for indexing deposits and withdrawals " +
"by account on L1 and L2"
app.Action = indexer.Main(GitVersion)
if err := app.Run(os.Args); err != nil { if err := app.Run(os.Args); err != nil {
log.Crit("Application failed", "message", err) log.Crit("Application failed", "message", err)
} }
......
...@@ -2,5 +2,8 @@ ...@@ -2,5 +2,8 @@
INDEXER_L1_ETH_RPC=FILL_ME_IN INDEXER_L1_ETH_RPC=FILL_ME_IN
INDEXER_L2_ETH_RPC=FILL_ME_IN INDEXER_L2_ETH_RPC=FILL_ME_IN
# temporary env variable to enable to new indexer
INDEXER_REFRESH=1
# Fill in to use prisma studio ui with a db other than the default # Fill in to use prisma studio ui with a db other than the default
# DATABASE_URL=FILL_ME_IN # DATABASE_URL=FILL_ME_IN
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment