OSDN Git Service

remove redundancy code
[bytom/bytom.git] / cmd / bytomcli / commands / asset.go
1 package commands
2
3 import (
4         "os"
5         "strings"
6
7         "github.com/spf13/cobra"
8         jww "github.com/spf13/jwalterweatherman"
9
10         "github.com/bytom/crypto/ed25519/chainkd"
11         "github.com/bytom/util"
12 )
13
14 func init() {
15         createAssetCmd.PersistentFlags().IntVarP(&assetQuorum, "quorom", "q", 1, "quorum must be greater than 0 and less than or equal to the number of signers")
16         createAssetCmd.PersistentFlags().StringVarP(&assetToken, "access", "a", "", "access token")
17         createAssetCmd.PersistentFlags().StringVarP(&assetDefiniton, "definition", "d", "", "definition for the asset")
18
19         listAssetsCmd.PersistentFlags().StringVar(&assetID, "id", "", "ID of asset")
20 }
21
22 var (
23         assetID        = ""
24         assetQuorum    = 1
25         assetToken     = ""
26         assetDefiniton = ""
27 )
28
29 var createAssetCmd = &cobra.Command{
30         Use:   "create-asset <alias> <xpub(s)>",
31         Short: "Create an asset",
32         Args:  cobra.MinimumNArgs(2),
33         Run: func(cmd *cobra.Command, args []string) {
34
35                 var ins assetIns
36
37                 for _, x := range args[1:] {
38                         xpub := chainkd.XPub{}
39                         if err := xpub.UnmarshalText([]byte(x)); err != nil {
40                                 jww.ERROR.Println(err)
41                                 os.Exit(util.ErrLocalExe)
42                         }
43                         ins.RootXPubs = append(ins.RootXPubs, xpub)
44                 }
45
46                 ins.Quorum = assetQuorum
47                 ins.Alias = args[0]
48                 ins.AccessToken = assetToken
49
50                 if len(assetDefiniton) != 0 {
51                         definition := strings.Split(assetDefiniton, ":")
52                         if len(definition) != 2 {
53                                 jww.ERROR.Println("Invalid definition")
54                                 os.Exit(util.ErrLocalExe)
55                         }
56                         ins.Definition = map[string]interface{}{definition[0]: definition[1]}
57                 }
58
59                 data, exitCode := util.ClientCall("/create-asset", &ins)
60                 if exitCode != util.Success {
61                         os.Exit(exitCode)
62                 }
63
64                 printJSON(data)
65         },
66 }
67
68 var listAssetsCmd = &cobra.Command{
69         Use:   "list-assets",
70         Short: "List the existing assets",
71         Args:  cobra.NoArgs,
72         Run: func(cmd *cobra.Command, args []string) {
73                 filter := struct {
74                         ID string `json:"id"`
75                 }{ID: assetID}
76
77                 data, exitCode := util.ClientCall("/list-assets", &filter)
78                 if exitCode != util.Success {
79                         os.Exit(exitCode)
80                 }
81
82                 printJSONList(data)
83         },
84 }
85
86 var updateAssetAliasCmd = &cobra.Command{
87         Use:   "update-asset-alias <oldAlias> <newAlias>",
88         Short: "Update the asset alias",
89         Args:  cobra.ExactArgs(2),
90         Run: func(cmd *cobra.Command, args []string) {
91                 var updateAlias = struct {
92                         OldAlias string `json:"old_alias"`
93                         NewAlias string `json:"new_alias"`
94                 }{OldAlias: args[0], NewAlias: args[1]}
95
96                 if _, exitCode := util.ClientCall("/update-asset-alias", &updateAlias); exitCode != util.Success {
97                         os.Exit(exitCode)
98                 }
99
100                 jww.FEEDBACK.Println("Successfully update asset alias")
101         },
102 }