Skip to content

Commit a1d049c

Browse files
authored
internal/flags: remove low-use type TextMarshalerFlag (#30707)
Currently we have a custom TextMarshalerFlag. It's a nice idea, allowing anything implementing text marshaller to be used as a flag. That said, we only ever used it in one place because it's not that obvious how to use and it needs some boilerplate on the type itself too, apart of the heavy boilerplate got the custom flag. All in all there's no *need* to drop this feature just now, but while porting the cmds over to cli @V3, all other custom flags worker perfectly, whereas this one started crashing deep inside the cli package. The flag handling in v3 got rebuild on generics and there are a number of new methods needed; and my guess is that maybe one of them doesn't work like this flag currently is designed too. We could definitely try and redesign this flag for cli v3... but all that effort and boilerplate just to use it for 1 flag in 1 location, seems not worth it. So for now I'm suggesting removing it and maybe reconsider a similar feature in cli v3 with however it will work.
1 parent 20bf543 commit a1d049c

File tree

3 files changed

+6
-122
lines changed

3 files changed

+6
-122
lines changed

cmd/utils/flags.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,7 @@ var (
211211
Value: 0,
212212
}
213213

214-
defaultSyncMode = ethconfig.Defaults.SyncMode
215-
SnapshotFlag = &cli.BoolFlag{
214+
SnapshotFlag = &cli.BoolFlag{
216215
Name: "snapshot",
217216
Usage: `Enables snapshot-database mode (default = enable)`,
218217
Value: true,
@@ -244,10 +243,10 @@ var (
244243
Usage: "Manually specify the Verkle fork timestamp, overriding the bundled setting",
245244
Category: flags.EthCategory,
246245
}
247-
SyncModeFlag = &flags.TextMarshalerFlag{
246+
SyncModeFlag = &cli.StringFlag{
248247
Name: "syncmode",
249248
Usage: `Blockchain sync mode ("snap" or "full")`,
250-
Value: &defaultSyncMode,
249+
Value: ethconfig.Defaults.SyncMode.String(),
251250
Category: flags.StateCategory,
252251
}
253252
GCModeFlag = &cli.StringFlag{
@@ -1669,7 +1668,9 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
16691668
if ctx.IsSet(SyncTargetFlag.Name) {
16701669
cfg.SyncMode = downloader.FullSync // dev sync target forces full sync
16711670
} else if ctx.IsSet(SyncModeFlag.Name) {
1672-
cfg.SyncMode = *flags.GlobalTextMarshaler(ctx, SyncModeFlag.Name).(*downloader.SyncMode)
1671+
if err = cfg.SyncMode.UnmarshalText([]byte(ctx.String(SyncModeFlag.Name))); err != nil {
1672+
Fatalf("invalid --syncmode flag: %v", err)
1673+
}
16731674
}
16741675
if ctx.IsSet(NetworkIdFlag.Name) {
16751676
cfg.NetworkId = ctx.Uint64(NetworkIdFlag.Name)

internal/flags/flags.go

Lines changed: 0 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package flags
1818

1919
import (
20-
"encoding"
2120
"errors"
2221
"flag"
2322
"fmt"
@@ -122,119 +121,6 @@ func (f *DirectoryFlag) GetDefaultText() string {
122121
return f.GetValue()
123122
}
124123

125-
type TextMarshaler interface {
126-
encoding.TextMarshaler
127-
encoding.TextUnmarshaler
128-
}
129-
130-
// textMarshalerVal turns a TextMarshaler into a flag.Value
131-
type textMarshalerVal struct {
132-
v TextMarshaler
133-
}
134-
135-
func (v textMarshalerVal) String() string {
136-
if v.v == nil {
137-
return ""
138-
}
139-
text, _ := v.v.MarshalText()
140-
return string(text)
141-
}
142-
143-
func (v textMarshalerVal) Set(s string) error {
144-
return v.v.UnmarshalText([]byte(s))
145-
}
146-
147-
var (
148-
_ cli.Flag = (*TextMarshalerFlag)(nil)
149-
_ cli.RequiredFlag = (*TextMarshalerFlag)(nil)
150-
_ cli.VisibleFlag = (*TextMarshalerFlag)(nil)
151-
_ cli.DocGenerationFlag = (*TextMarshalerFlag)(nil)
152-
_ cli.CategorizableFlag = (*TextMarshalerFlag)(nil)
153-
)
154-
155-
// TextMarshalerFlag wraps a TextMarshaler value.
156-
type TextMarshalerFlag struct {
157-
Name string
158-
159-
Category string
160-
DefaultText string
161-
Usage string
162-
163-
Required bool
164-
Hidden bool
165-
HasBeenSet bool
166-
167-
Value TextMarshaler
168-
169-
Aliases []string
170-
EnvVars []string
171-
}
172-
173-
// For cli.Flag:
174-
175-
func (f *TextMarshalerFlag) Names() []string { return append([]string{f.Name}, f.Aliases...) }
176-
func (f *TextMarshalerFlag) IsSet() bool { return f.HasBeenSet }
177-
func (f *TextMarshalerFlag) String() string { return cli.FlagStringer(f) }
178-
179-
func (f *TextMarshalerFlag) Apply(set *flag.FlagSet) error {
180-
for _, envVar := range f.EnvVars {
181-
envVar = strings.TrimSpace(envVar)
182-
if value, found := syscall.Getenv(envVar); found {
183-
if err := f.Value.UnmarshalText([]byte(value)); err != nil {
184-
return fmt.Errorf("could not parse %q from environment variable %q for flag %s: %s", value, envVar, f.Name, err)
185-
}
186-
f.HasBeenSet = true
187-
break
188-
}
189-
}
190-
eachName(f, func(name string) {
191-
set.Var(textMarshalerVal{f.Value}, f.Name, f.Usage)
192-
})
193-
return nil
194-
}
195-
196-
// For cli.RequiredFlag:
197-
198-
func (f *TextMarshalerFlag) IsRequired() bool { return f.Required }
199-
200-
// For cli.VisibleFlag:
201-
202-
func (f *TextMarshalerFlag) IsVisible() bool { return !f.Hidden }
203-
204-
// For cli.CategorizableFlag:
205-
206-
func (f *TextMarshalerFlag) GetCategory() string { return f.Category }
207-
208-
// For cli.DocGenerationFlag:
209-
210-
func (f *TextMarshalerFlag) TakesValue() bool { return true }
211-
func (f *TextMarshalerFlag) GetUsage() string { return f.Usage }
212-
func (f *TextMarshalerFlag) GetEnvVars() []string { return f.EnvVars }
213-
214-
func (f *TextMarshalerFlag) GetValue() string {
215-
t, err := f.Value.MarshalText()
216-
if err != nil {
217-
return "(ERR: " + err.Error() + ")"
218-
}
219-
return string(t)
220-
}
221-
222-
func (f *TextMarshalerFlag) GetDefaultText() string {
223-
if f.DefaultText != "" {
224-
return f.DefaultText
225-
}
226-
return f.GetValue()
227-
}
228-
229-
// GlobalTextMarshaler returns the value of a TextMarshalerFlag from the global flag set.
230-
func GlobalTextMarshaler(ctx *cli.Context, name string) TextMarshaler {
231-
val := ctx.Generic(name)
232-
if val == nil {
233-
return nil
234-
}
235-
return val.(textMarshalerVal).v
236-
}
237-
238124
var (
239125
_ cli.Flag = (*BigFlag)(nil)
240126
_ cli.RequiredFlag = (*BigFlag)(nil)

internal/flags/helpers.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,6 @@ func AutoEnvVars(flags []cli.Flag, prefix string) {
256256
case *BigFlag:
257257
flag.EnvVars = append(flag.EnvVars, envvar)
258258

259-
case *TextMarshalerFlag:
260-
flag.EnvVars = append(flag.EnvVars, envvar)
261-
262259
case *DirectoryFlag:
263260
flag.EnvVars = append(flag.EnvVars, envvar)
264261
}

0 commit comments

Comments
 (0)