Added encode and decode flags to the Triplex CLI.
All checks were successful
CI / test (push) Successful in 16s
All checks were successful
CI / test (push) Successful in 16s
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"git.hoiting.org/micha/triplex/serial"
|
"git.hoiting.org/micha/triplex/serial"
|
||||||
)
|
)
|
||||||
@@ -32,7 +33,45 @@ func myRandomIndex(max uint) (uint, error) {
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
complete := flag.String("complete", "", "complete code without checksum in format LLL-NNN-LL")
|
complete := flag.String("complete", "", "complete code without checksum in format LLL-NNN-LL")
|
||||||
|
encode := flag.String("encode", "", "encode a value to a code")
|
||||||
|
encodeShort := flag.String("e", "", "alias for --encode")
|
||||||
|
decode := flag.String("decode", "", "decode code in format LLL-NNN-LLL")
|
||||||
|
decodeShort := flag.String("d", "", "alias for --decode")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
resolvedEncode := ""
|
||||||
|
if *encode != "" && *encodeShort != "" && *encode != *encodeShort {
|
||||||
|
fmt.Println("flags --encode and -e cannot both be set with different values")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
if *encode != "" {
|
||||||
|
resolvedEncode = *encode
|
||||||
|
} else if *encodeShort != "" {
|
||||||
|
resolvedEncode = *encodeShort
|
||||||
|
}
|
||||||
|
resolvedDecode := ""
|
||||||
|
if *decode != "" && *decodeShort != "" && *decode != *decodeShort {
|
||||||
|
fmt.Println("flags --decode and -d cannot both be set with different values")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
if *decode != "" {
|
||||||
|
resolvedDecode = *decode
|
||||||
|
} else if *decodeShort != "" {
|
||||||
|
resolvedDecode = *decodeShort
|
||||||
|
}
|
||||||
|
selected := 0
|
||||||
|
if *complete != "" {
|
||||||
|
selected++
|
||||||
|
}
|
||||||
|
if resolvedEncode != "" {
|
||||||
|
selected++
|
||||||
|
}
|
||||||
|
if resolvedDecode != "" {
|
||||||
|
selected++
|
||||||
|
}
|
||||||
|
if selected > 1 {
|
||||||
|
fmt.Println("flags --complete, --encode, and --decode are mutually exclusive")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
if *complete != "" {
|
if *complete != "" {
|
||||||
full, idx, err := serial.CompleteCode(*complete)
|
full, idx, err := serial.CompleteCode(*complete)
|
||||||
@@ -40,10 +79,34 @@ func main() {
|
|||||||
fmt.Println("complete error:", err)
|
fmt.Println("complete error:", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("code:", full)
|
fmt.Println("code:", full)
|
||||||
fmt.Println("idx:", idx)
|
fmt.Println("idx:", idx)
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
|
} else if resolvedEncode != "" {
|
||||||
|
n, err := strconv.Atoi(resolvedEncode)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("invalid number:", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
idx := uint(n)
|
||||||
|
code, err := serial.Encode(idx)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("encode error:", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
fmt.Println("code:", code)
|
||||||
|
fmt.Println("idx:", idx)
|
||||||
|
os.Exit(0)
|
||||||
|
} else if resolvedDecode != "" {
|
||||||
|
code := resolvedDecode
|
||||||
|
idx, err := serial.Decode(code)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("decode error:", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
fmt.Println("code:", code)
|
||||||
|
fmt.Println("idx:", idx)
|
||||||
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
opts := serial.RandomCodeOptions{
|
opts := serial.RandomCodeOptions{
|
||||||
|
|||||||
Reference in New Issue
Block a user