-
Notifications
You must be signed in to change notification settings - Fork 19
/
common.go
127 lines (109 loc) · 2.85 KB
/
common.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
package cmd
import (
"fmt"
"log"
"os"
"sync"
"github.com/0chain/gosdk/core/transaction"
"github.com/0chain/gosdk/zboxcore/sdk"
"gopkg.in/cheggaaa/pb.v1"
)
const (
ZCNStatusSuccess int = 0
ZCNStatusError int = 1
)
func (s *StatusBar) Started(allocationId, filePath string, op int, totalBytes int) {
s.b = pb.StartNew(totalBytes)
s.b.Set(0)
}
func (s *StatusBar) InProgress(allocationId, filePath string, op int, completedBytes int, data []byte) {
s.b.Set(completedBytes)
}
func (s *StatusBar) Completed(allocationId, filePath string, filename string, mimetype string, size int, op int) {
if s.b != nil {
s.b.Finish()
}
s.success = true
if !allocUnderRepair {
defer s.wg.Done()
}
fmt.Println("Status completed callback. Type = " + mimetype + ". Name = " + filename)
}
func (s *StatusBar) Error(allocationID string, filePath string, op int, err error) {
if s.b != nil {
s.b.Finish()
}
s.success = false
if !allocUnderRepair {
defer s.wg.Done()
}
var errDetail interface{} = "Unknown Error"
if err != nil {
errDetail = err.Error()
}
PrintError("Error in file operation:", errDetail)
}
func (s *StatusBar) CommitMetaCompleted(request, response string, txn *transaction.Transaction, err error) {
defer s.wg.Done()
if err != nil {
s.success = false
PrintError("Error in commitMetaTransaction." + err.Error())
} else {
s.success = true
fmt.Println("Commit Metadata successful, Response :", response)
}
}
func (s *StatusBar) RepairCompleted(filesRepaired int) {
defer s.wg.Done()
allocUnderRepair = false
fmt.Println("Repair file completed, Total files repaired: ", filesRepaired)
}
type StatusBar struct {
b *pb.ProgressBar
wg *sync.WaitGroup
success bool
}
type ZCNStatus struct {
walletString string
wg *sync.WaitGroup
success bool
errMsg string
}
func (zcn *ZCNStatus) OnWalletCreateComplete(status int, wallet string, err string) {
defer zcn.wg.Done()
if status == ZCNStatusError {
zcn.success = false
zcn.errMsg = err
zcn.walletString = ""
return
}
zcn.success = true
zcn.errMsg = ""
zcn.walletString = wallet
return
}
func PrintError(v ...interface{}) {
fmt.Fprintln(os.Stderr, v...)
}
func PrintInfo(v ...interface{}) {
fmt.Fprintln(os.Stdin, v...)
}
func commitMetaTxn(path, crudOp, authTicket, lookupHash string, a *sdk.Allocation, fileMeta *sdk.ConsolidatedFileMeta, status *StatusBar) {
err := a.CommitMetaTransaction(path, crudOp, authTicket, lookupHash, fileMeta, status)
if err != nil {
PrintError("Commit failed.", err)
os.Exit(1)
}
}
func commitFolderTxn(operation, preValue, currValue string, a *sdk.Allocation) {
resp, err := a.CommitFolderChange(operation, preValue, currValue)
if err != nil {
PrintError("Commit failed.", err)
os.Exit(1)
}
fmt.Println("Commit Metadata successful, Response :", resp)
}
func init() {
log.SetOutput(os.Stdout)
log.SetFlags(0)
}