cli

package
v0.1.16 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	GetAPIInfo = cliutil.GetAPIInfo
	GetRawAPI  = cliutil.GetRawAPI
	GetAPI     = cliutil.GetCommonAPI
)
View Source
var (
	DaemonContext = cliutil.DaemonContext
	ReqContext    = cliutil.ReqContext
)
View Source
var (
	GetSchedulerAPI = cliutil.GetSchedulerAPI
	GetCandidateAPI = cliutil.GetCandidateAPI
	GetEdgeAPI      = cliutil.GetEdgeAPI
	GetLocatorAPI   = cliutil.GetLocatorAPI
)
View Source
var AuthAPIInfoToken = &cli.Command{
	Name:  "api-info",
	Usage: "Get token with API info required to connect to this node",
	Flags: []cli.Flag{
		&cli.StringFlag{
			Name:  "id",
			Usage: "special a id for web or candidate, edge, locator,admin",
		},
		&cli.StringFlag{
			Name:  "perm",
			Usage: "permission to assign to the token, one of: web, candidate, edge, locator, admin, web",
		},
	},

	Action: func(cctx *cli.Context) error {
		napi, closer, err := GetAPI(cctx)
		if err != nil {
			return err
		}
		defer closer()

		ctx := ReqContext(cctx)
		id := cctx.String("id")
		perm := cctx.String("perm")
		token, err := napi.AuthNew(ctx, &types.JWTPayload{Allow: []auth.Permission{auth.Permission(perm)}, ID: id})
		if err != nil {
			return err
		}

		ti, ok := cctx.App.Metadata["repoType"]
		if !ok {
			log.Errorf("unknown repo type, are you sure you want to use GetCommonAPI?")
			ti = repo.Scheduler
		}
		t, ok := ti.(repo.RepoType)
		if !ok {
			log.Errorf("repoType type does not match the type of repo.RepoType")
		}

		ainfo, err := GetAPIInfo(cctx, t)
		if err != nil {
			return xerrors.Errorf("could not get API info for %s: %w", t, err)
		}

		currentEnv, _, _ := t.APIInfoEnvVars()
		fmt.Printf("%s=%s:%s\n", currentEnv, token, ainfo.Addr)
		return nil
	},
}
View Source
var AuthCmd = &cli.Command{
	Name:  "auth",
	Usage: "Manage RPC permissions",
	Subcommands: []*cli.Command{
		AuthCreateAdminToken,
		AuthAPIInfoToken,
	},
}
View Source
var AuthCreateAdminToken = &cli.Command{
	Name:  "create-token",
	Usage: "Create token",
	Flags: []cli.Flag{
		&cli.StringFlag{
			Name:  "perm",
			Usage: "permission to assign to the token, one of: web, candidate, edge, locator,admin",
		},
	},

	Action: func(cctx *cli.Context) error {
		napi, closer, err := GetAPI(cctx)
		if err != nil {
			return err
		}
		defer closer()

		ctx := ReqContext(cctx)

		if !cctx.IsSet("perm") {
			return xerrors.New("--perm flag not set")
		}

		perm := cctx.String("perm")
		token, err := napi.AuthNew(ctx, &types.JWTPayload{Allow: []auth.Permission{auth.Permission(perm)}, ID: uuid.NewString()})
		if err != nil {
			return err
		}

		fmt.Println(token)
		return nil
	},
}
View Source
var CandidateCmds = []*cli.Command{
	nodeInfoCmd,
	cacheStatCmd,
	progressCmd,
	keyCmds,
	configCmds,
}
View Source
var Commands = []*cli.Command{
	WithCategory("developer", AuthCmd),
	WithCategory("developer", LogCmd),
	PprofCmd,
	VersionCmd,
}
View Source
var CommonCommands = []*cli.Command{
	AuthCmd,
	LogCmd,
	PprofCmd,
	VersionCmd,
}
View Source
var EdgeCmds = []*cli.Command{
	nodeInfoCmd,
	showCmds,
	cacheStatCmd,
	progressCmd,
	keyCmds,
	configCmds,
	stateCmd,
	signCmd,
	bindCmd,
	syncDataCmd,
}
View Source
var LogAlerts = &cli.Command{
	Name:  "alerts",
	Usage: "Get alert states",
	Flags: []cli.Flag{
		&cli.BoolFlag{
			Name:  "all",
			Usage: "get all (active and inactive) alerts",
		},
	},
	Action: func(cctx *cli.Context) error {
		api, closer, err := GetAPI(cctx)
		if err != nil {
			return err
		}
		defer closer()

		ctx := ReqContext(cctx)

		alerts, err := api.LogAlerts(ctx)
		if err != nil {
			return xerrors.Errorf("getting alerts: %w", err)
		}

		all := cctx.Bool("all")

		for _, alert := range alerts {
			if !all && !alert.Active {
				continue
			}

			active := color.RedString("active  ")
			if !alert.Active {
				active = color.GreenString("inactive")
			}

			fmt.Printf("%s %s:%s\n", active, alert.Type.System, alert.Type.Subsystem)
			if alert.LastResolved != nil {
				fmt.Printf("         last resolved at %s; reason: %s\n", alert.LastResolved.Time.Truncate(time.Millisecond), alert.LastResolved.Message)
			}
			if alert.LastActive != nil {
				fmt.Printf("         %s %s; reason: %s\n", color.YellowString("last raised at"), alert.LastActive.Time.Truncate(time.Millisecond), alert.LastActive.Message)
			}
		}

		return nil
	},
}
View Source
var LogCmd = &cli.Command{
	Name:  "log",
	Usage: "Manage logging",
	Subcommands: []*cli.Command{
		LogList,
		LogSetLevel,
		LogAlerts,
	},
}
View Source
var LogList = &cli.Command{
	Name:  "list",
	Usage: "List log systems",
	Action: func(cctx *cli.Context) error {
		api, closer, err := GetAPI(cctx)
		if err != nil {
			return err
		}
		defer closer()

		ctx := ReqContext(cctx)

		systems, err := api.LogList(ctx)
		if err != nil {
			return err
		}

		for _, system := range systems {
			fmt.Println(system)
		}

		return nil
	},
}
View Source
var LogSetLevel = &cli.Command{
	Name:      "set-level",
	Usage:     "Set log level",
	ArgsUsage: "[level]",
	Description: `Set the log level for logging systems:

   The system flag can be specified multiple times.

   eg) log set-level --system chain --system chainxchg debug

   Available Levels:
   debug
   info
   warn
   error

   Environment Variables:
   GOLOG_LOG_LEVEL - Default log level for all log systems
   GOLOG_LOG_FMT   - Change output log format (json, nocolor)
   GOLOG_FILE      - Write logs to file
   GOLOG_OUTPUT    - Specify whether to output to file, stderr, stdout or a combination, i.e. file+stderr
`,
	Flags: []cli.Flag{
		&cli.StringSliceFlag{
			Name:  "system",
			Usage: "limit to log system",
			Value: &cli.StringSlice{},
		},
	},
	Action: func(cctx *cli.Context) error {
		api, closer, err := GetAPI(cctx)
		if err != nil {
			return err
		}
		defer closer()
		ctx := ReqContext(cctx)

		if !cctx.Args().Present() {
			return fmt.Errorf("level is required")
		}

		systems := cctx.StringSlice("system")
		if len(systems) == 0 {
			var err error
			systems, err = api.LogList(ctx)
			if err != nil {
				return err
			}
		}

		for _, system := range systems {
			if err := api.LogSetLevel(ctx, system, cctx.Args().First()); err != nil {
				return xerrors.Errorf("setting log level on %s: %v", system, err)
			}
		}

		return nil
	},
}
View Source
var PprofCmd = &cli.Command{
	Name:   "pprof",
	Hidden: true,
	Subcommands: []*cli.Command{
		PprofGoroutines,
	},
}
View Source
var PprofGoroutines = &cli.Command{
	Name:  "goroutines",
	Usage: "Get goroutine stacks",
	Action: func(cctx *cli.Context) error {
		ti, ok := cctx.App.Metadata["repoType"]
		if !ok {
			log.Errorf("unknown repo type, are you sure you want to use GetAPI?")
			ti = repo.Scheduler
		}
		t, ok := ti.(repo.RepoType)
		if !ok {
			log.Errorf("repoType type does not match the type of repo.RepoType")
		}
		ainfo, err := GetAPIInfo(cctx, t)
		if err != nil {
			return xerrors.Errorf("could not get API info for %s: %w", t, err)
		}
		addr, err := ainfo.Host()
		if err != nil {
			return err
		}

		addr = "http://" + addr + "/debug/pprof/goroutine?debug=2"

		r, err := http.Get(addr)
		if err != nil {
			return err
		}

		if _, err := io.Copy(os.Stdout, r.Body); err != nil {
			return err
		}

		return r.Body.Close()
	},
}
View Source
var SchedulerCMDs = []*cli.Command{
	WithCategory("node", nodeCmds),
	WithCategory("asset", assetCmds),
	WithCategory("config", sConfigCmds),
	WithCategory("user", userCmds),
	startElectionCmd,

	edgeUpdaterCmd,
	electValidatorsCmd,
}

SchedulerCMDs Scheduler cmd

View Source
var VersionCmd = &cli.Command{
	Name:  "version",
	Usage: "Print version",
	Action: func(cctx *cli.Context) error {
		api, closer, err := GetAPI(cctx)
		if err != nil {
			return err
		}
		defer closer()

		ctx := ReqContext(cctx)

		v, err := api.Version(ctx)
		if err != nil {
			fmt.Println("err:", err.Error())
			return err
		}
		fmt.Println("Daemon: ", v)

		fmt.Print("Local: ")
		cli.VersionPrinter(cctx)
		return nil
	},
}

Functions

func IncorrectNumArgs

func IncorrectNumArgs(cctx *ufcli.Context) error

func NewCliError

func NewCliError(s string) error

func RegitsterNode added in v0.1.11

func RegitsterNode(cctx *cli.Context, lr repo.LockedRepo, locatorURL string, nodeType types.NodeType) error

func ShowHelp

func ShowHelp(cctx *ufcli.Context, err error) error

func WithCategory

func WithCategory(cat string, cmd *cli.Command) *cli.Command

Types

type APIConnector

type APIConnector func() api.Scheduler

APIConnector returns API instance

type ErrCmdFailed

type ErrCmdFailed struct {
	// contains filtered or unexported fields
}

func (*ErrCmdFailed) Error

func (e *ErrCmdFailed) Error() string

type PrintHelpErr

type PrintHelpErr struct {
	Err error
	Ctx *ufcli.Context
}

func (*PrintHelpErr) Error

func (e *PrintHelpErr) Error() string

func (*PrintHelpErr) Is

func (e *PrintHelpErr) Is(o error) bool

func (*PrintHelpErr) Unwrap

func (e *PrintHelpErr) Unwrap() error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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