Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
efca0ebb39 |
@@ -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,6 +137,26 @@ 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,7 +3,6 @@ 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"
|
||||||
@@ -32,9 +31,10 @@ func Command() *cobra.Command {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.Flags().String(
|
words := options.Decimal(childmnemonic.DefaultWords)
|
||||||
"words", strconv.Itoa(childmnemonic.DefaultWords),
|
cmd.Flags().Var(
|
||||||
"the `number` of words in the child mnemonic: 12, 18 or 24",
|
&words, "words",
|
||||||
|
"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 := options.Decimal(cmd, "words")
|
words, err := cmd.Flags().GetUint32("words")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", fmt.Errorf("reading the word count: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
parent, err := options.Mnemonic(cmd)
|
parent, err := options.Mnemonic(cmd)
|
||||||
|
|||||||
@@ -17,35 +17,20 @@ 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().StringP(
|
cmd.PersistentFlags().VarP(
|
||||||
"index", "n", "0",
|
new(Decimal), "index", "n",
|
||||||
"which key to derive, as a decimal `number`",
|
"which key to derive",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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) {
|
||||||
return Decimal(cmd, "index")
|
index, err := cmd.Flags().GetUint32("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 --%s: %w", name, err)
|
return 0, fmt.Errorf("reading the index: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
number, err := strconv.ParseUint(text, 10, 32)
|
return index, nil
|
||||||
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.
|
||||||
@@ -57,3 +42,33 @@ 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