Skip to content

Commit

Permalink
Merge pull request #268 from doanac/el2g-ux
Browse files Browse the repository at this point in the history
el2g: Explain the device-id a bit better
  • Loading branch information
doanac authored Sep 21, 2023
2 parents fe1bcc4 + 47a8873 commit 5405c0e
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion subcommands/el2g/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package el2g

import (
"fmt"
"math/big"
"strings"

"github.com/cheynewallace/tabby"
"github.com/foundriesio/fioctl/subcommands"
Expand Down Expand Up @@ -43,9 +45,14 @@ func init() {
# $ ssscli se05x uid | grep "Unique ID:" | cut -d: -f2
# ssscli se05x uid | grep "Unique ID:" | cut -d: -f2
# 04005001eee3ba1ee96e60047e57da0f6880
# This ID is hexadecimal and must be prefixed in the CLI with 0x. For example:
# This ID is hexadecimal and must be prefixed in the CLI with 0x0 (0x + 36 digits).
# For example:
fioctl el2g devices add 935389312472 0x04005001eee3ba1ee96e60047e57da0f6880
# A base-10 decimal ID(42 digits) may be used as well. To do the equivalent of
# the example above in decimal:
fioctl el2g devices add 935389312472 348555492004256518532939906410866457667712
# Add a production device with an SE051 HSM (product ID: 935414457472)
fioctl el2g devices add --production 935414457472 0x04005001eee3ba1ee96e60047e57da0f6880
`,
Expand Down Expand Up @@ -77,6 +84,7 @@ func doList(cmd *cobra.Command, args []string) {
func doShow(cmd *cobra.Command, args []string) {
factory := viper.GetString("factory")
deviceId := args[0]
verifyDeviceId(deviceId)

info, err := api.El2gProductInfo(factory, deviceId)
subcommands.DieNotNil(err)
Expand Down Expand Up @@ -113,12 +121,37 @@ func doAdd(cmd *cobra.Command, args []string) {
factory := viper.GetString("factory")
prodId := args[0]
deviceId := args[1]
verifyDeviceId(deviceId)
subcommands.DieNotNil(api.El2gAddDevice(factory, prodId, deviceId, production))
}

func doDelete(cmd *cobra.Command, args []string) {
factory := viper.GetString("factory")
prodId := args[0]
deviceId := args[1]
verifyDeviceId(deviceId)
subcommands.DieNotNil(api.El2gDeleteDevice(factory, prodId, deviceId, production))
}

func verifyDeviceId(id string) {
msg := "device IDs must be either 36 digit hex including a 0x0 prefix or 42 digit base-10 value. %s"
if strings.HasPrefix(id, "0x") {
if len(id) != 38 {
detail := fmt.Sprintf("Invalid hexadecimal length: %d", len(id))
subcommands.DieNotNil(fmt.Errorf(msg, detail))
}
n := new(big.Int)
if _, ok := n.SetString(id[2:], 16); !ok {
subcommands.DieNotNil(fmt.Errorf(msg, "Invalid base 16 conversion to big int"))
}
} else {
if len(id) != 42 {
detail := fmt.Sprintf("Invalid decimal length: %d", len(id))
subcommands.DieNotNil(fmt.Errorf(msg, detail))
}
n := new(big.Int)
if _, ok := n.SetString(id, 10); !ok {
subcommands.DieNotNil(fmt.Errorf(msg, "Invalid base 10 conversion to big int"))
}
}
}

0 comments on commit 5405c0e

Please sign in to comment.