2024-02-14 05:58:43 +00:00
|
|
|
package nns
|
|
|
|
|
|
|
|
import (
|
2024-09-16 08:09:51 +00:00
|
|
|
"math/big"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-contract/nns"
|
|
|
|
client "git.frostfs.info/TrueCloudLab/frostfs-contract/rpcclient/nns"
|
2024-02-14 05:58:43 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/commonflags"
|
|
|
|
commonCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2024-09-16 08:09:51 +00:00
|
|
|
const (
|
|
|
|
verboseDesc = "Include additional information about CNAME record."
|
|
|
|
)
|
|
|
|
|
2024-02-14 05:58:43 +00:00
|
|
|
func initTokensCmd() {
|
|
|
|
Cmd.AddCommand(tokensCmd)
|
|
|
|
tokensCmd.Flags().StringP(commonflags.EndpointFlag, commonflags.EndpointFlagShort, "", commonflags.EndpointFlagDesc)
|
2024-09-16 08:09:51 +00:00
|
|
|
tokensCmd.Flags().BoolP(commonflags.Verbose, commonflags.VerboseShorthand, false, verboseDesc)
|
2024-02-14 05:58:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func listTokens(cmd *cobra.Command, _ []string) {
|
2024-11-21 07:43:53 +00:00
|
|
|
c, _ := nnsReader(cmd)
|
2024-02-14 05:58:43 +00:00
|
|
|
it, err := c.Tokens()
|
|
|
|
commonCmd.ExitOnErr(cmd, "unable to get tokens: %w", err)
|
|
|
|
for toks, err := it.Next(10); err == nil && len(toks) > 0; toks, err = it.Next(10) {
|
|
|
|
for _, token := range toks {
|
2024-09-16 08:09:51 +00:00
|
|
|
output := string(token)
|
|
|
|
if verbose, _ := cmd.Flags().GetBool(commonflags.Verbose); verbose {
|
|
|
|
cname, err := getCnameRecord(c, token)
|
|
|
|
commonCmd.ExitOnErr(cmd, "", err)
|
|
|
|
if cname != "" {
|
|
|
|
output += " (CNAME: " + cname + ")"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cmd.Println(output)
|
2024-02-14 05:58:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-09-16 08:09:51 +00:00
|
|
|
|
2024-11-21 07:43:53 +00:00
|
|
|
func getCnameRecord(c *client.ContractReader, token []byte) (string, error) {
|
2024-09-16 08:09:51 +00:00
|
|
|
items, err := c.GetRecords(string(token), big.NewInt(int64(nns.CNAME)))
|
|
|
|
|
|
|
|
// GetRecords returns the error "not an array" if the domain does not contain records.
|
|
|
|
if err != nil && strings.Contains(err.Error(), "not an array") {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(items) == 0 {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
record, err := items[0].TryBytes()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(record), nil
|
|
|
|
}
|