Use hex-encoding for restore's index flag

This commit changes the `-index` flag on the `restore` command by
parsing it as a hex number instead of a decimal number. This is
done because the index is represented in hex form everywhere else
in the application.
This commit is contained in:
Ben Johnson
2021-04-19 11:58:13 -06:00
parent 63e51d2050
commit 9439822763
2 changed files with 24 additions and 2 deletions

View File

@@ -14,6 +14,7 @@ import (
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
@@ -497,3 +498,24 @@ func expand(s string) (string, error) {
}
return filepath.Join(u.HomeDir, strings.TrimPrefix(s, prefix)), nil
}
// indexVar allows the flag package to parse index flags as 4-byte hexadecimal values.
type indexVar int
// Ensure type implements interface.
var _ flag.Value = (*indexVar)(nil)
// String returns an 8-character hexadecimal value.
func (v *indexVar) String() string {
return fmt.Sprintf("%08x", int(*v))
}
// Set parses s into an integer from a hexadecimal value.
func (v *indexVar) Set(s string) error {
i, err := strconv.ParseInt(s, 16, 32)
if err != nil {
return fmt.Errorf("invalid hexadecimal format")
}
*v = indexVar(i)
return nil
}

View File

@@ -25,7 +25,7 @@ func (c *RestoreCommand) Run(ctx context.Context, args []string) (err error) {
fs.StringVar(&opt.OutputPath, "o", "", "output path")
fs.StringVar(&opt.ReplicaName, "replica", "", "replica name")
fs.StringVar(&opt.Generation, "generation", "", "generation name")
fs.IntVar(&opt.Index, "index", opt.Index, "wal index")
fs.Var((*indexVar)(&opt.Index), "index", "wal index")
ifReplicaExists := fs.Bool("if-replica-exists", false, "")
timestampStr := fs.String("timestamp", "", "timestamp")
verbose := fs.Bool("v", false, "verbose output")
@@ -156,7 +156,7 @@ Arguments:
Defaults to generation with latest data.
-index NUM
Restore up to a specific WAL index (inclusive).
Restore up to a specific hex-encoded WAL index (inclusive).
Defaults to use the highest available index.
-timestamp TIMESTAMP