github.com/aeternity/aepp-sdk-go@v1.0.3-0.20190606142815-1c0ffdc21fd9/cmd/inspect.go (about) 1 // Copyright © 2018 NAME HERE <EMAIL ADDRESS> 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package cmd 16 17 import ( 18 "fmt" 19 "os" 20 "regexp" 21 "strconv" 22 "strings" 23 24 "github.com/aeternity/aepp-sdk-go/aeternity" 25 26 "github.com/spf13/cobra" 27 ) 28 29 // inspectCmd represents the inspect command 30 var inspectCmd = &cobra.Command{ 31 Use: "inspect", 32 Short: "Inspect an object of the blockchain", 33 Long: `Inspect an object of the chain 34 35 Valid object to inspect are block hash, transaction hash, accounts`, 36 RunE: inspectFunc, 37 Args: cobra.MinimumNArgs(1), 38 } 39 40 func init() { 41 RootCmd.AddCommand(inspectCmd) 42 43 // Here you will define your flags and configuration settings. 44 45 // Cobra supports Persistent Flags which will work for this command 46 // and all subcommands, e.g.: 47 // inspectCmd.PersistentFlags().String("foo", "", "A help for foo") 48 49 // Cobra supports local flags which will only run when this command 50 // is called directly, e.g.: 51 // inspectCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") 52 53 } 54 55 func printResult(title string, v interface{}, err error) { 56 if err != nil { 57 fmt.Println(err) 58 os.Exit(1) 59 } 60 aeternity.PrintObject(title, v) 61 } 62 63 func inspectFunc(cmd *cobra.Command, args []string) (err error) { 64 aeCli := NewAeCli() 65 for _, object := range args { 66 // height 67 if matched, _ := regexp.MatchString(`^\d+$`, object); matched { 68 height, _ := strconv.ParseUint(object, 10, 64) 69 aeCli.PrintGenerationByHeight(height) 70 continue 71 } 72 // name 73 if strings.HasSuffix(object, ".aet") { 74 v, err := aeCli.APIGetNameEntryByName(object) 75 if err != nil { 76 return err 77 } 78 printResult("aens", v, err) 79 continue 80 } 81 82 switch aeternity.GetHashPrefix(object) { 83 case aeternity.PrefixAccountPubkey: 84 // account balance 85 v, err := aeCli.APIGetAccount(object) 86 if err != nil { 87 return err 88 } 89 90 printResult("account", v, err) 91 92 case aeternity.PrefixMicroBlockHash: 93 v, err := aeCli.APIGetMicroBlockHeaderByHash(object) 94 if err != nil { 95 return err 96 } 97 printResult("block", v, err) 98 v1, err := aeCli.APIGetMicroBlockTransactionsByHash(object) 99 if err != nil { 100 return err 101 } 102 printResult("transaction", v1, err) 103 104 case aeternity.PrefixKeyBlockHash: 105 // block 106 v, err := aeCli.APIGetKeyBlockByHash(object) 107 if err != nil { 108 return err 109 } 110 printResult("key-block", v, err) 111 112 case aeternity.PrefixTransactionHash: 113 // transaction 114 v, err := aeCli.APIGetTransactionByHash(object) 115 if err != nil { 116 return err 117 } 118 printResult("transaction", v, err) 119 120 case aeternity.PrefixOraclePubkey: 121 // oracle 122 v, err := aeCli.APIGetOracleByPubkey(object) 123 if err != nil { 124 return err 125 } 126 printResult("oracle", v, err) 127 128 default: 129 return fmt.Errorf("Object %v not yet supported", object) 130 } 131 } 132 return nil 133 }