-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.go
More file actions
70 lines (66 loc) · 1.77 KB
/
Copy pathcli.go
File metadata and controls
70 lines (66 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"fmt"
"os"
"strconv"
)
type CLI struct {
bc *BlockChain
}
const Usage = `
printChain "Forward print all blockchain data"
printChainR "Reverse print all blockchain data"
getBalance --address ADDRESS "Obtain designated address balance"
transfer FROM TO AMOUNT MINER DATA "FROM transfers AMOUNT to TO, MINER mine and write to data"
newWallet "Create a new wallet"
listAddresses "List all addresses"
`
// Run go build example => ./example.exe command
func (cli *CLI) Run() {
args := os.Args // get command
if len(args) < 2 {
fmt.Println("Too few parameters")
fmt.Printf(Usage)
return
}
cmd := args[1]
switch cmd {
case "printChain":
fmt.Println("Forward print all blockchain data:")
cli.PrintBlockChain()
case "printChainR":
fmt.Println("Reverse print all blockchain data:")
cli.PrintBlockChainReverse()
case "getBalance":
fmt.Println("Obtain designated address balance:")
if len(args) == 4 && args[2] == "--address" {
address := args[3] // get command line data
cli.GetBalance(address)
} else {
fmt.Println("GetBalance parameters error!")
fmt.Printf(Usage)
}
case "transfer":
fmt.Println("Begin transfer...")
if len(args) != 7 {
fmt.Println("Transfer parameters error!")
fmt.Printf(Usage)
return
}
from := args[2]
to := args[3]
amount, _ := strconv.ParseFloat(args[4], 64) // string change to float64
miner := args[5]
data := args[6]
cli.Transfer(from, to, amount, miner, data)
case "newWallet":
fmt.Println("Create a new wallet:")
cli.CliNewWallet()
case "listAddresses":
fmt.Println("List all addresses:")
cli.ListAddresses()
default:
fmt.Println("Invalid command")
fmt.Printf(Usage)
}
}