2020-07-14 11:35:32 +03:00
|
|
|
// Copyright 2020 The go-ethereum Authors
|
2022-05-24 21:39:40 +03:00
|
|
|
// This file is part of the go-ethereum library.
|
2020-07-14 11:35:32 +03:00
|
|
|
//
|
2022-05-24 21:39:40 +03:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
2020-07-14 11:35:32 +03:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2022-05-24 21:39:40 +03:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2020-07-14 11:35:32 +03:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2022-05-24 21:39:40 +03:00
|
|
|
// GNU Lesser General Public License for more details.
|
2020-07-14 11:35:32 +03:00
|
|
|
//
|
2022-05-24 21:39:40 +03:00
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2020-07-14 11:35:32 +03:00
|
|
|
|
|
|
|
package flags
|
|
|
|
|
|
|
|
import (
|
2022-06-27 19:22:36 +03:00
|
|
|
"fmt"
|
2023-09-14 10:33:59 +03:00
|
|
|
"os"
|
|
|
|
"regexp"
|
2023-09-15 15:52:53 +03:00
|
|
|
"sort"
|
2022-06-27 19:22:36 +03:00
|
|
|
"strings"
|
2020-07-14 11:35:32 +03:00
|
|
|
|
2022-09-23 15:08:25 +03:00
|
|
|
"github.com/ethereum/go-ethereum/internal/version"
|
2023-09-15 15:52:53 +03:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2020-07-14 11:35:32 +03:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2023-09-14 10:33:59 +03:00
|
|
|
"github.com/mattn/go-isatty"
|
2022-06-27 19:22:36 +03:00
|
|
|
"github.com/urfave/cli/v2"
|
2020-07-14 11:35:32 +03:00
|
|
|
)
|
|
|
|
|
2023-09-14 10:33:59 +03:00
|
|
|
// usecolor defines whether the CLI help should use colored output or normal dumb
|
|
|
|
// colorless terminal formatting.
|
|
|
|
var usecolor = (isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd())) && os.Getenv("TERM") != "dumb"
|
|
|
|
|
2022-06-27 19:22:36 +03:00
|
|
|
// NewApp creates an app with sane defaults.
|
2022-09-23 15:08:25 +03:00
|
|
|
func NewApp(usage string) *cli.App {
|
|
|
|
git, _ := version.VCS()
|
2022-06-27 19:22:36 +03:00
|
|
|
app := cli.NewApp()
|
|
|
|
app.EnableBashCompletion = true
|
2022-09-23 15:08:25 +03:00
|
|
|
app.Version = params.VersionWithCommit(git.Commit, git.Date)
|
2022-06-27 19:22:36 +03:00
|
|
|
app.Usage = usage
|
2023-01-03 12:04:54 +03:00
|
|
|
app.Copyright = "Copyright 2013-2023 The go-ethereum Authors"
|
2022-06-27 19:22:36 +03:00
|
|
|
app.Before = func(ctx *cli.Context) error {
|
|
|
|
MigrateGlobalFlags(ctx)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
2022-07-04 20:52:19 +03:00
|
|
|
// Merge merges the given flag slices.
|
|
|
|
func Merge(groups ...[]cli.Flag) []cli.Flag {
|
|
|
|
var ret []cli.Flag
|
|
|
|
for _, group := range groups {
|
|
|
|
ret = append(ret, group...)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2022-06-27 19:22:36 +03:00
|
|
|
var migrationApplied = map[*cli.Command]struct{}{}
|
|
|
|
|
|
|
|
// MigrateGlobalFlags makes all global flag values available in the
|
|
|
|
// context. This should be called as early as possible in app.Before.
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
2022-09-10 14:25:40 +03:00
|
|
|
// geth account new --keystore /tmp/mykeystore --lightkdf
|
2022-06-27 19:22:36 +03:00
|
|
|
//
|
|
|
|
// is equivalent after calling this method with:
|
|
|
|
//
|
2022-09-10 14:25:40 +03:00
|
|
|
// geth --keystore /tmp/mykeystore --lightkdf account new
|
2022-06-27 19:22:36 +03:00
|
|
|
//
|
|
|
|
// i.e. in the subcommand Action function of 'account new', ctx.Bool("lightkdf)
|
|
|
|
// will return true even if --lightkdf is set as a global option.
|
|
|
|
//
|
|
|
|
// This function may become unnecessary when https://github.com/urfave/cli/pull/1245 is merged.
|
|
|
|
func MigrateGlobalFlags(ctx *cli.Context) {
|
|
|
|
var iterate func(cs []*cli.Command, fn func(*cli.Command))
|
|
|
|
iterate = func(cs []*cli.Command, fn func(*cli.Command)) {
|
|
|
|
for _, cmd := range cs {
|
|
|
|
if _, ok := migrationApplied[cmd]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
migrationApplied[cmd] = struct{}{}
|
|
|
|
fn(cmd)
|
|
|
|
iterate(cmd.Subcommands, fn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This iterates over all commands and wraps their action function.
|
|
|
|
iterate(ctx.App.Commands, func(cmd *cli.Command) {
|
2022-07-04 20:52:19 +03:00
|
|
|
if cmd.Action == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-27 19:22:36 +03:00
|
|
|
action := cmd.Action
|
|
|
|
cmd.Action = func(ctx *cli.Context) error {
|
|
|
|
doMigrateFlags(ctx)
|
|
|
|
return action(ctx)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-07-14 11:35:32 +03:00
|
|
|
|
2022-06-27 19:22:36 +03:00
|
|
|
func doMigrateFlags(ctx *cli.Context) {
|
2022-09-29 21:16:36 +03:00
|
|
|
// Figure out if there are any aliases of commands. If there are, we want
|
|
|
|
// to ignore them when iterating over the flags.
|
2023-12-18 15:28:41 +03:00
|
|
|
aliases := make(map[string]bool)
|
2022-09-29 21:16:36 +03:00
|
|
|
for _, fl := range ctx.Command.Flags {
|
|
|
|
for _, alias := range fl.Names()[1:] {
|
|
|
|
aliases[alias] = true
|
|
|
|
}
|
|
|
|
}
|
2022-06-27 19:22:36 +03:00
|
|
|
for _, name := range ctx.FlagNames() {
|
|
|
|
for _, parent := range ctx.Lineage()[1:] {
|
|
|
|
if parent.IsSet(name) {
|
2022-09-29 21:16:36 +03:00
|
|
|
// When iterating across the lineage, we will be served both
|
|
|
|
// the 'canon' and alias formats of all commmands. In most cases,
|
|
|
|
// it's fine to set it in the ctx multiple times (one for each
|
|
|
|
// name), however, the Slice-flags are not fine.
|
|
|
|
// The slice-flags accumulate, so if we set it once as
|
|
|
|
// "foo" and once as alias "F", then both will be present in the slice.
|
|
|
|
if _, isAlias := aliases[name]; isAlias {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// If it is a string-slice, we need to set it as
|
|
|
|
// "alfa, beta, gamma" instead of "[alfa beta gamma]", in order
|
|
|
|
// for the backing StringSlice to parse it properly.
|
|
|
|
if result := parent.StringSlice(name); len(result) > 0 {
|
|
|
|
ctx.Set(name, strings.Join(result, ","))
|
|
|
|
} else {
|
|
|
|
ctx.Set(name, parent.String(name))
|
|
|
|
}
|
2022-06-27 19:22:36 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-14 11:35:32 +03:00
|
|
|
}
|
|
|
|
|
2022-06-27 19:22:36 +03:00
|
|
|
func init() {
|
2023-09-14 10:33:59 +03:00
|
|
|
if usecolor {
|
|
|
|
// Annotate all help categories with colors
|
|
|
|
cli.AppHelpTemplate = regexp.MustCompile("[A-Z ]+:").ReplaceAllString(cli.AppHelpTemplate, "\u001B[33m$0\u001B[0m")
|
|
|
|
|
|
|
|
// Annotate flag categories with colors (private template, so need to
|
|
|
|
// copy-paste the entire thing here...)
|
|
|
|
cli.AppHelpTemplate = strings.ReplaceAll(cli.AppHelpTemplate, "{{template \"visibleFlagCategoryTemplate\" .}}", "{{range .VisibleFlagCategories}}\n {{if .Name}}\u001B[33m{{.Name}}\u001B[0m\n\n {{end}}{{$flglen := len .Flags}}{{range $i, $e := .Flags}}{{if eq (subtract $flglen $i) 1}}{{$e}}\n{{else}}{{$e}}\n {{end}}{{end}}{{end}}")
|
|
|
|
}
|
2022-06-27 19:22:36 +03:00
|
|
|
cli.FlagStringer = FlagString
|
2020-07-14 11:35:32 +03:00
|
|
|
}
|
|
|
|
|
2022-06-27 19:22:36 +03:00
|
|
|
// FlagString prints a single flag in help.
|
|
|
|
func FlagString(f cli.Flag) string {
|
|
|
|
df, ok := f.(cli.DocGenerationFlag)
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
needsPlaceholder := df.TakesValue()
|
|
|
|
placeholder := ""
|
|
|
|
if needsPlaceholder {
|
|
|
|
placeholder = "value"
|
|
|
|
}
|
2020-07-14 11:35:32 +03:00
|
|
|
|
2023-09-14 10:33:59 +03:00
|
|
|
namesText := cli.FlagNamePrefixer(df.Names(), placeholder)
|
2022-06-27 19:22:36 +03:00
|
|
|
|
|
|
|
defaultValueString := ""
|
|
|
|
if s := df.GetDefaultText(); s != "" {
|
|
|
|
defaultValueString = " (default: " + s + ")"
|
|
|
|
}
|
|
|
|
envHint := strings.TrimSpace(cli.FlagEnvHinter(df.GetEnvVars(), ""))
|
2023-09-14 10:33:59 +03:00
|
|
|
if envHint != "" {
|
|
|
|
envHint = " (" + envHint[1:len(envHint)-1] + ")"
|
2022-06-27 19:22:36 +03:00
|
|
|
}
|
2023-09-14 10:33:59 +03:00
|
|
|
usage := strings.TrimSpace(df.GetUsage())
|
2022-06-27 19:22:36 +03:00
|
|
|
usage = wordWrap(usage, 80)
|
|
|
|
usage = indent(usage, 10)
|
|
|
|
|
2023-09-14 10:33:59 +03:00
|
|
|
if usecolor {
|
|
|
|
return fmt.Sprintf("\n \u001B[32m%-35s%-35s\u001B[0m%s\n%s", namesText, defaultValueString, envHint, usage)
|
|
|
|
} else {
|
|
|
|
return fmt.Sprintf("\n %-35s%-35s%s\n%s", namesText, defaultValueString, envHint, usage)
|
|
|
|
}
|
2022-06-27 19:22:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func indent(s string, nspace int) string {
|
|
|
|
ind := strings.Repeat(" ", nspace)
|
|
|
|
return ind + strings.ReplaceAll(s, "\n", "\n"+ind)
|
2020-07-14 11:35:32 +03:00
|
|
|
}
|
|
|
|
|
2022-06-27 19:22:36 +03:00
|
|
|
func wordWrap(s string, width int) string {
|
|
|
|
var (
|
|
|
|
output strings.Builder
|
|
|
|
lineLength = 0
|
|
|
|
)
|
|
|
|
|
|
|
|
for {
|
|
|
|
sp := strings.IndexByte(s, ' ')
|
|
|
|
var word string
|
|
|
|
if sp == -1 {
|
|
|
|
word = s
|
|
|
|
} else {
|
|
|
|
word = s[:sp]
|
|
|
|
}
|
|
|
|
wlen := len(word)
|
|
|
|
over := lineLength+wlen >= width
|
|
|
|
if over {
|
|
|
|
output.WriteByte('\n')
|
|
|
|
lineLength = 0
|
|
|
|
} else {
|
|
|
|
if lineLength != 0 {
|
|
|
|
output.WriteByte(' ')
|
|
|
|
lineLength++
|
2020-07-14 11:35:32 +03:00
|
|
|
}
|
|
|
|
}
|
2022-06-27 19:22:36 +03:00
|
|
|
|
|
|
|
output.WriteString(word)
|
|
|
|
lineLength += wlen
|
|
|
|
|
|
|
|
if sp == -1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
s = s[wlen+1:]
|
2020-07-14 11:35:32 +03:00
|
|
|
}
|
|
|
|
|
2022-06-27 19:22:36 +03:00
|
|
|
return output.String()
|
2020-07-14 11:35:32 +03:00
|
|
|
}
|
2023-09-14 10:33:59 +03:00
|
|
|
|
2023-09-17 17:02:11 +03:00
|
|
|
// AutoEnvVars extends all the specific CLI flags with automatically generated
|
2023-09-14 10:33:59 +03:00
|
|
|
// env vars by capitalizing the flag, replacing . with _ and prefixing it with
|
|
|
|
// the specified string.
|
|
|
|
//
|
|
|
|
// Note, the prefix should *not* contain the separator underscore, that will be
|
|
|
|
// added automatically.
|
|
|
|
func AutoEnvVars(flags []cli.Flag, prefix string) {
|
|
|
|
for _, flag := range flags {
|
|
|
|
envvar := strings.ToUpper(prefix + "_" + strings.ReplaceAll(strings.ReplaceAll(flag.Names()[0], ".", "_"), "-", "_"))
|
|
|
|
|
|
|
|
switch flag := flag.(type) {
|
|
|
|
case *cli.StringFlag:
|
|
|
|
flag.EnvVars = append(flag.EnvVars, envvar)
|
|
|
|
|
2023-12-18 15:28:41 +03:00
|
|
|
case *cli.StringSliceFlag:
|
|
|
|
flag.EnvVars = append(flag.EnvVars, envvar)
|
|
|
|
|
2023-09-14 10:33:59 +03:00
|
|
|
case *cli.BoolFlag:
|
|
|
|
flag.EnvVars = append(flag.EnvVars, envvar)
|
|
|
|
|
|
|
|
case *cli.IntFlag:
|
|
|
|
flag.EnvVars = append(flag.EnvVars, envvar)
|
|
|
|
|
2023-12-18 15:28:41 +03:00
|
|
|
case *cli.Int64Flag:
|
|
|
|
flag.EnvVars = append(flag.EnvVars, envvar)
|
|
|
|
|
2023-09-14 10:33:59 +03:00
|
|
|
case *cli.Uint64Flag:
|
|
|
|
flag.EnvVars = append(flag.EnvVars, envvar)
|
|
|
|
|
2023-12-18 15:28:41 +03:00
|
|
|
case *cli.Float64Flag:
|
|
|
|
flag.EnvVars = append(flag.EnvVars, envvar)
|
|
|
|
|
2023-09-14 10:33:59 +03:00
|
|
|
case *cli.DurationFlag:
|
|
|
|
flag.EnvVars = append(flag.EnvVars, envvar)
|
|
|
|
|
|
|
|
case *cli.PathFlag:
|
|
|
|
flag.EnvVars = append(flag.EnvVars, envvar)
|
|
|
|
|
|
|
|
case *BigFlag:
|
|
|
|
flag.EnvVars = append(flag.EnvVars, envvar)
|
|
|
|
|
|
|
|
case *TextMarshalerFlag:
|
|
|
|
flag.EnvVars = append(flag.EnvVars, envvar)
|
|
|
|
|
|
|
|
case *DirectoryFlag:
|
|
|
|
flag.EnvVars = append(flag.EnvVars, envvar)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-15 15:52:53 +03:00
|
|
|
|
|
|
|
// CheckEnvVars iterates over all the environment variables and checks if any of
|
|
|
|
// them look like a CLI flag but is not consumed. This can be used to detect old
|
|
|
|
// or mistyped names.
|
|
|
|
func CheckEnvVars(ctx *cli.Context, flags []cli.Flag, prefix string) {
|
|
|
|
known := make(map[string]string)
|
|
|
|
for _, flag := range flags {
|
|
|
|
docflag, ok := flag.(cli.DocGenerationFlag)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, envvar := range docflag.GetEnvVars() {
|
|
|
|
known[envvar] = flag.Names()[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
keyvals := os.Environ()
|
|
|
|
sort.Strings(keyvals)
|
|
|
|
|
|
|
|
for _, keyval := range keyvals {
|
|
|
|
key := strings.Split(keyval, "=")[0]
|
|
|
|
if !strings.HasPrefix(key, prefix) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if flag, ok := known[key]; ok {
|
|
|
|
if ctx.Count(flag) > 0 {
|
|
|
|
log.Info("Config environment variable found", "envvar", key, "shadowedby", "--"+flag)
|
|
|
|
} else {
|
|
|
|
log.Info("Config environment variable found", "envvar", key)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Warn("Unknown config environment variable", "envvar", key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|