-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
131 lines (115 loc) · 3.46 KB
/
cli.go
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"flag"
"fmt"
"log"
"os"
)
// CLI is the interface to this program
type CLI struct{}
// printUsage prints out usage information
func (cli *CLI) printUsage() {
fmt.Println("-----------------------------------------------------------------------------------------------")
fmt.Println("Usage:")
// fmt.Println(" addblock -data BLOCK_DATA: add a new block to the blockchain")
fmt.Println(" createblockchain -address ADDRESS: create a blockchain with the given ADDRESS")
fmt.Println(" createwallet: generate a new key pair and save it to wallets file")
fmt.Println(" getbalance -address ADDRESS: get balance of ADDRESS")
fmt.Println(" newaddress: generate new address")
fmt.Println(" printchain: print all the blocks in the blockchain")
fmt.Println(" send: -from FROM -to TO -amount AMOUNT: send AMOUNT from FROM to TO")
fmt.Println("-----------------------------------------------------------------------------------------------")
}
// Run is the entry point for CLI
func (cli *CLI) Run() {
cli.validateArgs()
addBlockCmd := flag.NewFlagSet("addblock", flag.ExitOnError)
createBlockchainCmd := flag.NewFlagSet("createblockchain", flag.ExitOnError)
createWalletCmd := flag.NewFlagSet("createwallet", flag.ExitOnError)
getBalanceCmd := flag.NewFlagSet("getbalance", flag.ExitOnError)
newAddressCmd := flag.NewFlagSet("newaddress", flag.ExitOnError)
printChainCmd := flag.NewFlagSet("printchain", flag.ExitOnError)
sendCmd := flag.NewFlagSet("send", flag.ExitOnError)
// addBlockData := addBlockCmd.String("data", "", "block data")
createBlockchainAddress := createBlockchainCmd.String("address", "", "the address to send Genesis reward to")
getBalanceAddress := getBalanceCmd.String("address", "", "the address to getbalance for")
sendFrom := sendCmd.String("from", "", "source wallet address")
sendTo := sendCmd.String("to", "", "destination wallet address")
sendAmount := sendCmd.Int("amount", 0, "amount to send")
switch os.Args[1] {
case "addblock":
err := addBlockCmd.Parse(os.Args[2:])
if err != nil {
log.Panicln(err)
}
case "createblockchain":
err := createBlockchainCmd.Parse(os.Args[2:])
if err != nil {
log.Panicln(err)
}
case "createwallet":
err := createWalletCmd.Parse(os.Args[2:])
if err != nil {
log.Panicln(err)
}
case "getbalance":
err := getBalanceCmd.Parse(os.Args[2:])
if err != nil {
log.Panicln(err)
}
case "newaddress":
err := newAddressCmd.Parse(os.Args[2:])
if err != nil {
log.Panicln(err)
}
case "printchain":
err := printChainCmd.Parse(os.Args[2:])
if err != nil {
log.Panicln(err)
}
case "send":
err := sendCmd.Parse(os.Args[2:])
if err != nil {
log.Panicln(err)
}
default:
cli.printUsage()
}
if createBlockchainCmd.Parsed() {
if *createBlockchainAddress == "" {
createBlockchainCmd.Usage()
os.Exit(1)
}
cli.CreateBlockchain(*createBlockchainAddress)
}
if createWalletCmd.Parsed() {
cli.createWallet()
}
if getBalanceCmd.Parsed() {
if *getBalanceAddress == "" {
getBalanceCmd.Usage()
os.Exit(1)
}
cli.getBalance(*getBalanceAddress)
}
if newAddressCmd.Parsed() {
cli.newAddress()
}
if printChainCmd.Parsed() {
cli.printChain()
}
if sendCmd.Parsed() {
if *sendFrom == "" || *sendTo == "" || *sendAmount <= 0 {
sendCmd.Usage()
os.Exit(1)
}
cli.send(*sendFrom, *sendTo, *sendAmount)
}
}
// validateArgs checks whether the input is valid
func (cli *CLI) validateArgs() {
if len(os.Args) < 2 {
cli.printUsage()
os.Exit(1)
}
}