cmd

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 29, 2024 License: MIT Imports: 33 Imported by: 0

Documentation

Overview

Package cmd provides the command line interface for the guard.

Index

Constants

This section is empty.

Variables

View Source
var GuardInfoCommand = &cli.Command{
	Name:        "guard-info",
	Description: "learn how to use guard cli",
	Action: func(c *cli.Context) error {
		fmt.Println(string(markdown.Render(help, termsize.Width(), 6)))
		return nil
	},
}

GuardInfoCommand gets info about using the guard agent.

View Source
var GuardRunCommand = &cli.Command{
	Name:        "guard-run",
	Description: "runs the guard service",
	Flags:       []cli.Flag{configFlag, metricsPortFlag, debugFlag},
	Action: func(c *cli.Context) error {
		var scribeClient client.ScribeClient

		handler, err := metrics.NewFromEnv(c.Context, metadata.BuildInfo())
		if err != nil {
			return fmt.Errorf("failed to create metrics handler: %w", err)
		}

		guardConfig, guardDB, err := createGuardParameters(c.Context, c, handler)
		if err != nil {
			return fmt.Errorf("failed to create guard parameters: %w", err)
		}

		var baseOmniRPCClient omnirpcClient.RPCClient
		if debugFlag.IsSet() {
			baseOmniRPCClient = omnirpcClient.NewOmnirpcClient(guardConfig.BaseOmnirpcURL, handler, omnirpcClient.WithCaptureReqRes())
		} else {
			baseOmniRPCClient = omnirpcClient.NewOmnirpcClient(guardConfig.BaseOmnirpcURL, handler)
		}

		var shouldRetryAtomic atomic.Bool
		shouldRetryAtomic.Store(true)

		for shouldRetryAtomic.Load() {
			shouldRetryAtomic.Store(false)

			g, ctx := errgroup.WithContext(c.Context)

			switch guardConfig.ScribeConfig.Type {
			case "embedded":
				eventDB, err := scribeAPI.InitDB(
					ctx,
					guardConfig.DBConfig.Type,
					guardConfig.DBConfig.Source,
					handler,
					false,
				)
				if err != nil {
					return fmt.Errorf("failed to initialize database: %w", err)
				}

				scribeClients := make(map[uint32][]backend.ScribeBackend)

				for _, client := range guardConfig.ScribeConfig.EmbeddedScribeConfig.Chains {
					for confNum := 1; confNum <= scribeCmd.MaxConfirmations; confNum++ {
						backendClient, err := backend.DialBackend(ctx, fmt.Sprintf("%s/%d/rpc/%d", guardConfig.BaseOmnirpcURL, confNum, client.ChainID), handler)
						if err != nil {
							return fmt.Errorf("could not start client for %s", fmt.Sprintf("%s/1/rpc/%d", guardConfig.BaseOmnirpcURL, client.ChainID))
						}

						scribeClients[client.ChainID] = append(scribeClients[client.ChainID], backendClient)
					}
				}

				scribe, err := service.NewScribe(eventDB, scribeClients, guardConfig.ScribeConfig.EmbeddedScribeConfig, handler)
				if err != nil {
					return fmt.Errorf("failed to initialize scribe: %w", err)
				}

				g.Go(func() error {
					err := scribe.Start(ctx)
					if err != nil {
						return fmt.Errorf("failed to start scribe: %w", err)
					}

					return nil
				})
				embedded := client.NewEmbeddedScribe(
					guardConfig.DBConfig.Type,
					guardConfig.DBConfig.Source,
					handler,
				)

				g.Go(func() error {
					err := embedded.Start(c.Context)
					if err != nil {
						return fmt.Errorf("failed to start embedded scribe: %w", err)
					}

					return nil
				})

				scribeClient = embedded.ScribeClient
			case "remote":
				scribeClient = client.NewRemoteScribe(
					uint16(guardConfig.ScribeConfig.Port),
					guardConfig.ScribeConfig.URL,
					handler,
				).ScribeClient
			default:
				return fmt.Errorf("invalid scribe type: %s", guardConfig.ScribeConfig.Type)
			}
			guard, err := guard.NewGuard(c.Context, guardConfig, baseOmniRPCClient, scribeClient, guardDB, handler)
			if err != nil {
				return fmt.Errorf("failed to create guard: %w", err)
			}

			g.Go(func() error {
				err = guard.Start(c.Context)
				if err != nil {
					shouldRetryAtomic.Store(true)

					log.Errorf("Error running guard, will sleep for a minute and retry: %v", err)
					time.Sleep(60 * time.Second)
					return fmt.Errorf("failed to run guard: %w", err)
				}

				return nil
			})

			g.Go(func() error {
				err := api.Start(c.Context, uint16(c.Uint(metricsPortFlag.Name)))
				if err != nil {
					return fmt.Errorf("failed to start api: %w", err)
				}

				return nil
			})

			if err := g.Wait(); err != nil {
				return fmt.Errorf("failed to run guard: %w", err)
			}
		}

		return nil
	},
}

GuardRunCommand runs the guard.

Functions

func InitGuardDB added in v0.0.216

func InitGuardDB(parentCtx context.Context, database string, path string, tablePrefix string, handler metrics.Handler) (_ db.GuardDB, err error)

InitGuardDB initializes a database given a database type and path.

func Start

func Start(args []string, buildInfo config.BuildInfo)

Start starts the command line.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL