-
Notifications
You must be signed in to change notification settings - Fork 47
/
cmd_sequence.go
55 lines (48 loc) · 1.22 KB
/
cmd_sequence.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"encoding/hex"
"flag"
"fmt"
"io"
"strconv"
)
func cmdAsSequence(input io.Reader, output io.Writer, args []string) error {
fl := flag.NewFlagSet("", flag.ExitOnError)
fl.Usage = func() {
fmt.Fprintln(flag.CommandLine.Output(), `
Convet given number into a hex-encoded sequence representation.
`)
fl.PrintDefaults()
}
fl.Parse(args)
for _, raw := range fl.Args() {
n, err := strconv.ParseInt(raw, 10, 64)
if err != nil {
return fmt.Errorf("%q is not a valid number: %s", raw, err)
}
fmt.Fprintln(output, hex.EncodeToString(sequenceID(uint64(n))))
}
return nil
}
func cmdFromSequence(input io.Reader, output io.Writer, args []string) error {
fl := flag.NewFlagSet("", flag.ExitOnError)
fl.Usage = func() {
fmt.Fprintln(flag.CommandLine.Output(), `
Convet given hex-encoded sequence value into its decimal representation.
`)
fl.PrintDefaults()
}
fl.Parse(args)
for _, s := range fl.Args() {
raw, err := hex.DecodeString(s)
if err != nil {
return fmt.Errorf("%q is not a valid hex representation: %s", s, err)
}
n, err := fromSequence(raw)
if err != nil {
return fmt.Errorf("%q is not a valid sequence value: %s", s, err)
}
fmt.Fprintln(output, n)
}
return nil
}