OSDN Git Service

Merge pull request #285 from Bytom/dev
[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(&assetTags, "tags", "t", "", "tags")
18         createAssetCmd.PersistentFlags().StringVarP(&assetDefiniton, "definition", "d", "", "definition for the asset")
19
20         updateAssetTagsCmd.PersistentFlags().StringVarP(&assetUpdateTags, "tags", "t", "", "tags to add, delete or update")
21
22         listAssetsCmd.PersistentFlags().StringVar(&assetID, "id", "", "ID of asset")
23 }
24
25 var (
26         assetID         = ""
27         assetQuorum     = 1
28         assetToken      = ""
29         assetTags       = ""
30         assetDefiniton  = ""
31         assetUpdateTags = ""
32 )
33
34 var createAssetCmd = &cobra.Command{
35         Use:   "create-asset <alias> <xpub(s)>",
36         Short: "Create an asset",
37         Args:  cobra.MinimumNArgs(2),
38         Run: func(cmd *cobra.Command, args []string) {
39
40                 var ins assetIns
41
42                 for _, x := range args[1:] {
43                         xpub := chainkd.XPub{}
44                         if err := xpub.UnmarshalText([]byte(x)); err != nil {
45                                 jww.ERROR.Println(err)
46                                 os.Exit(util.ErrLocalExe)
47                         }
48                         ins.RootXPubs = append(ins.RootXPubs, xpub)
49                 }
50
51                 ins.Quorum = assetQuorum
52                 ins.Alias = args[0]
53                 ins.AccessToken = assetToken
54
55                 if len(assetTags) != 0 {
56                         tags := strings.Split(assetTags, ":")
57                         if len(tags) != 2 {
58                                 jww.ERROR.Println("Invalid tags")
59                                 os.Exit(util.ErrLocalExe)
60                         }
61                         ins.Tags = map[string]interface{}{tags[0]: tags[1]}
62                 }
63
64                 if len(assetDefiniton) != 0 {
65                         definition := strings.Split(assetDefiniton, ":")
66                         if len(definition) != 2 {
67                                 jww.ERROR.Println("Invalid definition")
68                                 os.Exit(util.ErrLocalExe)
69                         }
70                         ins.Definition = map[string]interface{}{definition[0]: definition[1]}
71                 }
72
73                 data, exitCode := util.ClientCall("/create-asset", &ins)
74                 if exitCode != util.Success {
75                         os.Exit(exitCode)
76                 }
77
78                 printJSON(data)
79         },
80 }
81
82 var listAssetsCmd = &cobra.Command{
83         Use:   "list-assets",
84         Short: "List the existing assets",
85         Args:  cobra.NoArgs,
86         Run: func(cmd *cobra.Command, args []string) {
87                 filter := struct {
88                         ID string `json:"id"`
89                 }{ID: assetID}
90
91                 data, exitCode := util.ClientCall("/list-assets", &filter)
92                 if exitCode != util.Success {
93                         os.Exit(exitCode)
94                 }
95
96                 printJSONList(data)
97         },
98 }
99
100 var updateAssetTagsCmd = &cobra.Command{
101         Use:   "update-asset-tags <assetID|alias>",
102         Short: "Update the asset tags",
103         Args:  cobra.ExactArgs(1),
104         PreRun: func(cmd *cobra.Command, args []string) {
105                 cmd.MarkFlagRequired("tags")
106         },
107         Run: func(cmd *cobra.Command, args []string) {
108                 var updateTag = struct {
109                         AssetInfo string                 `json:"asset_info"`
110                         Tags      map[string]interface{} `json:"tags"`
111                 }{}
112
113                 if len(assetUpdateTags) != 0 {
114                         tags := strings.Split(assetUpdateTags, ":")
115                         if len(tags) != 2 {
116                                 jww.ERROR.Println("Invalid tags")
117                                 os.Exit(util.ErrLocalExe)
118                         }
119                         updateTag.Tags = map[string]interface{}{tags[0]: tags[1]}
120                 }
121
122                 updateTag.AssetInfo = args[0]
123                 if _, exitCode := util.ClientCall("/update-asset-tags", &updateTag); exitCode != util.Success {
124                         os.Exit(exitCode)
125                 }
126
127                 jww.FEEDBACK.Println("Successfully update asset tags")
128         },
129 }