Compare commits
1
Commits
next
..
dc3a0150f3
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dc3a0150f3 |
@@ -123,7 +123,7 @@ func TestAnIndexWithALeadingZeroIsDecimal(t *testing.T) {
|
|||||||
func TestAnIndexThatIsNotDecimalDigitsIsRefused(t *testing.T) {
|
func TestAnIndexThatIsNotDecimalDigitsIsRefused(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
for _, index := range []string{"0x10", "1_0", "+1", "-1", ""} {
|
for _, index := range []string{"0x10", "1_0", "+1", "-1"} {
|
||||||
t.Run(index, func(t *testing.T) {
|
t.Run(index, func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
@@ -137,26 +137,6 @@ func TestAnIndexThatIsNotDecimalDigitsIsRefused(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Neither a command group nor --version reads the index, so these
|
|
||||||
// show that it is refused while the flags are parsed.
|
|
||||||
func TestAnIndexNotInDecimalIsRefusedBeforeAnyCommandRuns(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
for _, args := range [][]string{
|
|
||||||
{"ssh", "-n", "0x10"},
|
|
||||||
{"-n", "0x10", "--version"},
|
|
||||||
} {
|
|
||||||
t.Run(strings.Join(args, " "), func(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
out, err := execute(t, args...)
|
|
||||||
require.ErrorIs(t, err, strconv.ErrSyntax)
|
|
||||||
require.ErrorContains(t, err, "--index")
|
|
||||||
require.Empty(t, out)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTheMnemonicCommandIsUsed(t *testing.T) {
|
func TestTheMnemonicCommandIsUsed(t *testing.T) {
|
||||||
t.Setenv(mnemonic.Variable, "")
|
t.Setenv(mnemonic.Variable, "")
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package mnemonic
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"sneak.berlin/go/keyfunc/internal/childmnemonic"
|
"sneak.berlin/go/keyfunc/internal/childmnemonic"
|
||||||
@@ -31,10 +32,9 @@ func Command() *cobra.Command {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
words := options.Decimal(childmnemonic.DefaultWords)
|
cmd.Flags().String(
|
||||||
cmd.Flags().Var(
|
"words", strconv.Itoa(childmnemonic.DefaultWords),
|
||||||
&words, "words",
|
"the `number` of words in the child mnemonic: 12, 18 or 24",
|
||||||
"how long the child mnemonic is: 12, 18 or 24 words",
|
|
||||||
)
|
)
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
@@ -47,9 +47,9 @@ func derived(cmd *cobra.Command) (string, error) {
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
words, err := cmd.Flags().GetUint32("words")
|
words, err := options.Decimal(cmd, "words")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("reading the word count: %w", err)
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
parent, err := options.Mnemonic(cmd)
|
parent, err := options.Mnemonic(cmd)
|
||||||
|
|||||||
@@ -17,20 +17,35 @@ func Add(cmd *cobra.Command) {
|
|||||||
"mnemonic-command", "",
|
"mnemonic-command", "",
|
||||||
"shell command whose output is the mnemonic",
|
"shell command whose output is the mnemonic",
|
||||||
)
|
)
|
||||||
cmd.PersistentFlags().VarP(
|
cmd.PersistentFlags().StringP(
|
||||||
new(Decimal), "index", "n",
|
"index", "n", "0",
|
||||||
"which key to derive",
|
"which key to derive, as a decimal `number`",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Index returns the key index the user asked for.
|
// Index returns the key index the user asked for.
|
||||||
func Index(cmd *cobra.Command) (uint32, error) {
|
func Index(cmd *cobra.Command) (uint32, error) {
|
||||||
index, err := cmd.Flags().GetUint32("index")
|
return Decimal(cmd, "index")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decimal reads the named string flag as a number written in decimal
|
||||||
|
// digits only: a leading zero is still decimal, and a base prefix, an
|
||||||
|
// underscore or a sign is refused. The back-quoted word in the usage
|
||||||
|
// of such a flag is what --help shows as its type.
|
||||||
|
func Decimal(cmd *cobra.Command, name string) (uint32, error) {
|
||||||
|
text, err := cmd.Flags().GetString(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, fmt.Errorf("reading the index: %w", err)
|
return 0, fmt.Errorf("reading --%s: %w", name, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return index, nil
|
number, err := strconv.ParseUint(text, 10, 32)
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf(
|
||||||
|
"reading --%s as a decimal number: %w", name, err,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return uint32(number), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mnemonic returns the mnemonic, from the first source that has one.
|
// Mnemonic returns the mnemonic, from the first source that has one.
|
||||||
@@ -42,33 +57,3 @@ func Mnemonic(cmd *cobra.Command) (string, error) {
|
|||||||
|
|
||||||
return mnemonic.Read(cmd.Context(), command)
|
return mnemonic.Read(cmd.Context(), command)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decimal is a flag value for a number written in decimal digits only.
|
|
||||||
// The flag library's own uint32 flags read 010 as octal 8 and also
|
|
||||||
// take 0x10 or 1_0.
|
|
||||||
type Decimal uint32
|
|
||||||
|
|
||||||
// String returns the number in decimal digits.
|
|
||||||
func (d *Decimal) String() string {
|
|
||||||
return strconv.FormatUint(uint64(*d), 10)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set reads text as a decimal number that fits in 32 bits: a leading
|
|
||||||
// zero is still decimal, and a base prefix, an underscore or a sign is
|
|
||||||
// refused.
|
|
||||||
func (d *Decimal) Set(text string) error {
|
|
||||||
number, err := strconv.ParseUint(text, 10, 32)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
*d = Decimal(number)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Type is what --help shows as the flag's type, and what GetUint32
|
|
||||||
// checks before it reads the value back.
|
|
||||||
func (d *Decimal) Type() string {
|
|
||||||
return "uint32"
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user