cli.go 2.59 KB
Newer Older
Will Cory's avatar
Will Cory committed
1 2 3
package cli

import (
4
	"context"
Will Cory's avatar
Will Cory committed
5 6
	"fmt"

7
	"github.com/ethereum-optimism/optimism/indexer"
Will Cory's avatar
Will Cory committed
8
	"github.com/ethereum-optimism/optimism/indexer/config"
9 10
	"github.com/ethereum-optimism/optimism/op-service/log"
	"github.com/ethereum-optimism/optimism/op-service/opio"
11

Will Cory's avatar
Will Cory committed
12
	"github.com/ethereum/go-ethereum/params"
13

Will Cory's avatar
Will Cory committed
14 15 16 17 18 19 20 21 22 23 24 25
	"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 {
26 27
	logger := log.NewLogger(log.ReadCLIConfig(ctx))

Will Cory's avatar
Will Cory committed
28
	configPath := ctx.String(ConfigFlag.Name)
29 30
	cfg, err := config.LoadConfig(configPath)
	if err != nil {
31
		logger.Error("failed to load config", "err", err)
32 33
		return err
	}
Will Cory's avatar
Will Cory committed
34

35
	cfg.Logger = logger
36
	indexer, err := indexer.NewIndexer(cfg)
Will Cory's avatar
Will Cory committed
37
	if err != nil {
38
		return err
Will Cory's avatar
Will Cory committed
39 40
	}

Hamdi Allam's avatar
Hamdi Allam committed
41
	indexerCtx, indexerCancel := context.WithCancel(context.Background())
42
	go func() {
43
		opio.BlockOnInterrupts()
Hamdi Allam's avatar
Hamdi Allam committed
44
		indexerCancel()
45 46 47
	}()

	return indexer.Run(indexerCtx)
Will Cory's avatar
Will Cory committed
48 49 50
}

func runApi(ctx *cli.Context) error {
51
	logger := log.NewLogger(log.ReadCLIConfig(ctx))
Will Cory's avatar
Will Cory committed
52

53 54
	configPath := ctx.String(ConfigFlag.Name)
	cfg, err := config.LoadConfig(configPath)
Will Cory's avatar
Will Cory committed
55
	if err != nil {
56 57
		logger.Error("failed to load config", "err", err)
		return err
Will Cory's avatar
Will Cory committed
58
	}
59

60 61 62
	cfg.Logger = logger
	fmt.Println(cfg)

Will Cory's avatar
Will Cory committed
63
	// finish me
64
	return err
Will Cory's avatar
Will Cory committed
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
}

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 {
93
	flags := append([]cli.Flag{ConfigFlag}, log.CLIFlags("INDEXER")...)
Will Cory's avatar
Will Cory committed
94 95
	app := &cli.App{
		Version:     fmt.Sprintf("%s-%s", GitVersion, params.VersionWithCommit(GitCommit, GitDate)),
Will Cory's avatar
Will Cory committed
96
		Description: "An indexer of all optimism events with a serving api layer",
Will Cory's avatar
Will Cory committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
		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,
	}
}