Skip to content

Commit 0763e31

Browse files
authored
Merge pull request #669 from 0chain/error-log-stderr
remove log imports
2 parents bf90249 + 552bf7d commit 0763e31

12 files changed

+152
-155
lines changed

cmd/challengepool.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"fmt"
5-
"log"
65
"os"
76

87
"github.com/0chain/gosdk/zboxcore/sdk"
@@ -42,16 +41,16 @@ var cpInfo = &cobra.Command{
4241
doJSON, _ := cmd.Flags().GetBool("json")
4342

4443
if !flags.Changed("allocation") {
45-
log.Fatal("missing required 'allocation' flag")
44+
Fatal("missing required 'allocation' flag")
4645
}
4746

4847
if allocID, err = flags.GetString("allocation"); err != nil {
49-
log.Fatalf("can't get 'allocation' flag: %v", err)
48+
Fatalf("can't get 'allocation' flag: %v", err)
5049
}
5150

5251
var info *sdk.ChallengePoolInfo
5352
if info, err = sdk.GetChallengePoolInfo(allocID); err != nil {
54-
log.Fatalf("Failed to get challenge pool info: %v", err)
53+
Fatalf("Failed to get challenge pool info: %v", err)
5554
}
5655
if doJSON {
5756
util.PrintJSON(info)

cmd/common.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,17 @@ func PrintError(v ...interface{}) {
9595
}
9696

9797
func PrintInfo(v ...interface{}) {
98-
fmt.Fprintln(os.Stdin, v...)
98+
fmt.Fprintln(os.Stdout, v...)
99+
}
100+
101+
func Fatal(v ...interface{}) {
102+
fmt.Fprintln(os.Stderr, v...)
103+
os.Exit(1)
104+
}
105+
106+
func Fatalf(format string, v ...interface{}) {
107+
fmt.Fprintf(os.Stderr, format, v...)
108+
os.Exit(1)
99109
}
100110

101111
func init() {

cmd/get_download_cost.go

+15-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"fmt"
5-
"log"
65

76
"github.com/0chain/gosdk/core/common"
87
"github.com/0chain/gosdk/zboxcore/fileref"
@@ -17,7 +16,7 @@ func calculateDownloadCost(alloc *sdk.Allocation, fileSize int64, numBlocks int6
1716
for _, d := range alloc.BlobberDetails {
1817
readPrice, err := d.Terms.ReadPrice.ToToken()
1918
if err != nil {
20-
log.Fatalf("failed to convert %v to token, %v", d.Terms.ReadPrice, err)
19+
Fatalf("failed to convert %v to token, %v", d.Terms.ReadPrice, err)
2120
}
2221

2322
cost += sizeInGB(numBlocks*fileref.CHUNK_SIZE) * float64(readPrice)
@@ -26,14 +25,14 @@ func calculateDownloadCost(alloc *sdk.Allocation, fileSize int64, numBlocks int6
2625

2726
balance, err := common.ToBalance(cost)
2827
if err != nil {
29-
log.Fatalf("failed to convert %v to balance, %v", cost, err)
28+
Fatalf("failed to convert %v to balance, %v", cost, err)
3029
}
3130
return balance
3231
}
3332

3433
func downloadCost(alloc *sdk.Allocation, meta *sdk.ConsolidatedFileMeta, blocksPerMarker int) {
3534
if meta.Type != fileref.FILE {
36-
log.Fatal("not a file")
35+
Fatal("not a file")
3736
}
3837

3938
shardSize := (meta.ActualFileSize + int64(alloc.DataShards) - 1) / int64(alloc.DataShards)
@@ -61,18 +60,18 @@ var getDownloadCostCmd = &cobra.Command{
6160
)
6261

6362
if !fflags.Changed("allocation") {
64-
log.Fatal("missing required 'allocation' flag")
63+
Fatal("missing required 'allocation' flag")
6564
}
6665

6766
allocID = cmd.Flag("allocation").Value.String()
6867
blocksPerMarker, err := cmd.Flags().GetInt("blocks-per-marker")
6968

7069
if err != nil {
71-
log.Fatal("invalid blocks-per-marker value: ", err)
70+
Fatal("invalid blocks-per-marker value: ", err)
7271
}
7372

7473
if blocksPerMarker <= 0 {
75-
log.Fatal("blocks-per-marker value cannot be <= 0")
74+
Fatal("blocks-per-marker value cannot be <= 0")
7675
}
7776

7877
var (
@@ -83,24 +82,24 @@ var getDownloadCostCmd = &cobra.Command{
8382

8483
if fflags.Changed("remotepath") {
8584
if remotePath, err = fflags.GetString("remotepath"); err != nil {
86-
log.Fatal("invalid 'remotepath' flag: ", err)
85+
Fatal("invalid 'remotepath' flag: ", err)
8786
}
8887
}
8988

9089
if fflags.Changed("authticket") {
9190
if authTicket, err = fflags.GetString("authticket"); err != nil {
92-
log.Fatal("invalid 'authticket' flag: ", err)
91+
Fatal("invalid 'authticket' flag: ", err)
9392
}
9493
}
9594

9695
if fflags.Changed("lookuphash") {
9796
if lookupHash, err = fflags.GetString("lookuphash"); err != nil {
98-
log.Fatal("invalid 'lookuphash' flag: ", err)
97+
Fatal("invalid 'lookuphash' flag: ", err)
9998
}
10099
}
101100

102101
if remotePath == "" && authTicket == "" {
103-
log.Fatal("'remotepath' or 'authticket' flag required")
102+
Fatal("'remotepath' or 'authticket' flag required")
104103
}
105104

106105
var (
@@ -113,11 +112,11 @@ var getDownloadCostCmd = &cobra.Command{
113112
// by remote path
114113

115114
if alloc, err = sdk.GetAllocation(allocID); err != nil {
116-
log.Fatal("fetching the allocation: ", err)
115+
Fatal("fetching the allocation: ", err)
117116
}
118117

119118
if meta, err = alloc.GetFileMeta(remotePath); err != nil {
120-
log.Fatal("can't get file meta: ", err)
119+
Fatal("can't get file meta: ", err)
121120
}
122121

123122
downloadCost(alloc, meta, blocksPerMarker)
@@ -128,19 +127,19 @@ var getDownloadCostCmd = &cobra.Command{
128127

129128
alloc, err = sdk.GetAllocationFromAuthTicket(authTicket)
130129
if err != nil {
131-
log.Fatal("can't get allocation object: ", err)
130+
Fatal("can't get allocation object: ", err)
132131
}
133132
var at = sdk.InitAuthTicket(authTicket)
134133

135134
if lookupHash == "" {
136135
if lookupHash, err = at.GetLookupHash(); err != nil {
137-
log.Fatal("can't get lookup hash from auth ticket: ", err)
136+
Fatal("can't get lookup hash from auth ticket: ", err)
138137
}
139138
}
140139

141140
meta, err = alloc.GetFileMetaFromAuthTicket(authTicket, lookupHash)
142141
if err != nil {
143-
log.Fatal("can't get file meta: ", err)
142+
Fatal("can't get file meta: ", err)
144143
}
145144

146145
downloadCost(alloc, meta, blocksPerMarker)

cmd/get_upload_cost.go

+10-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"fmt"
5-
"log"
65
"os"
76
"time"
87

@@ -28,47 +27,47 @@ var getUploadCostCmd = &cobra.Command{
2827
)
2928

3029
if !fflags.Changed("allocation") {
31-
log.Fatal("missing required 'allocation' flag")
30+
Fatal("missing required 'allocation' flag")
3231
}
3332

3433
allocID = cmd.Flag("allocation").Value.String()
3534

3635
var localPath string
3736

3837
if !fflags.Changed("localpath") {
39-
log.Fatal("missing requried 'localpath' flag")
38+
Fatal("missing requried 'localpath' flag")
4039
}
4140

4241
if localPath, err = fflags.GetString("localpath"); err != nil {
43-
log.Fatal("invalid 'localpath' flag: ", err)
42+
Fatal("invalid 'localpath' flag: ", err)
4443
}
4544

4645
if localPath == "" {
47-
log.Fatal("empty local path")
46+
Fatal("empty local path")
4847
}
4948

5049
var fi os.FileInfo
5150
if fi, err = os.Stat(localPath); err != nil {
52-
log.Fatal(err)
51+
Fatal(err)
5352
}
5453

5554
if !fi.Mode().IsRegular() {
56-
log.Fatal("not a regular file")
55+
Fatal("not a regular file")
5756
}
5857

5958
if duration, err = fflags.GetDuration("duration"); err != nil {
60-
log.Fatal("invalid 'duration' flag:", err)
59+
Fatal("invalid 'duration' flag:", err)
6160
} else if duration < 0 {
62-
log.Fatal("negative duration not allowed: ", duration)
61+
Fatal("negative duration not allowed: ", duration)
6362
}
6463

6564
if end, err = fflags.GetBool("end"); err != nil {
66-
log.Fatal("invalid 'end' flag:", err)
65+
Fatal("invalid 'end' flag:", err)
6766
}
6867

6968
var alloc *sdk.Allocation
7069
if alloc, err = sdk.GetAllocation(allocID); err != nil {
71-
log.Fatal("fetching the allocation: ", err)
70+
Fatal("fetching the allocation: ", err)
7271
}
7372

7473
// until allocation ends

cmd/getallocation.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"fmt"
5-
"log"
65
"os"
76

87
"github.com/0chain/gosdk/core/common"
@@ -32,7 +31,7 @@ var getallocationCmd = &cobra.Command{
3231
alloc, err := sdk.GetAllocation(allocationID)
3332
if err != nil {
3433
logger.Logger.Error("Error fetching the allocation", err)
35-
log.Fatal("Error fetching/verifying the allocation")
34+
Fatal("Error fetching/verifying the allocation")
3635
}
3736
if doJSON {
3837
util.PrintJSON(alloc)
@@ -53,11 +52,11 @@ var getallocationCmd = &cobra.Command{
5352

5453
blocksPerMarker, err := cmd.Flags().GetInt("blocks-per-marker")
5554
if err != nil {
56-
log.Fatal("invalid blocks-per-marker. Error: ", err)
55+
Fatal("invalid blocks-per-marker. Error: ", err)
5756
}
5857

5958
if blocksPerMarker <= 0 {
60-
log.Fatal("invalid blocks-per-marker. Should be greater than 0")
59+
Fatal("invalid blocks-per-marker. Should be greater than 0")
6160
}
6261

6362
fmt.Println("allocation:")

cmd/getmpt.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7-
"log"
87
"strings"
98

109
"github.com/0chain/gosdk/zboxcore/sdk"
@@ -19,18 +18,18 @@ var getMptKeyCommand = &cobra.Command{
1918
Hidden: true,
2019
Run: func(cmd *cobra.Command, args []string) {
2120
if cmd.Flags().Changed("key") == false {
22-
log.Fatal("Required Mpt key missing\n")
21+
Fatal("Required Mpt key missing\n")
2322
}
2423
key := cmd.Flag("key").Value.String()
2524
jsonBytes, err := sdk.GetMptData(key)
2625
if err != nil {
27-
log.Fatalf("Failed to get Mpt key: %v\n", err)
26+
Fatalf("Failed to get Mpt key: %v\n", err)
2827
}
2928

3029
var indented bytes.Buffer
3130
err = json.Indent(&indented, jsonBytes, "", "\t")
3231
if err != nil {
33-
log.Fatalf("Result %s baddly formated: %v\n", string(jsonBytes), err)
32+
Fatalf("Result %s baddly formated: %v\n", string(jsonBytes), err)
3433
}
3534

3635
noBackSlash := strings.Replace(indented.String(), "\\", "", -1)

cmd/rename.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd
22

33
import (
4-
"fmt"
54
"os"
65

76
"github.com/0chain/gosdk/constants"
@@ -41,7 +40,7 @@ var renameCmd = &cobra.Command{
4140
destName := cmd.Flag("destname").Value.String()
4241
oldName := pathutil.Dir(remotePath)
4342
if oldName == destName {
44-
fmt.Println(remotePath + " renamed")
43+
PrintInfo(remotePath + " renamed")
4544
return
4645
}
4746

@@ -56,7 +55,7 @@ var renameCmd = &cobra.Command{
5655
PrintError("Rename failed.", err)
5756
os.Exit(1)
5857
}
59-
fmt.Println(remotePath + " renamed")
58+
PrintInfo(remotePath + " renamed")
6059
},
6160
}
6261

0 commit comments

Comments
 (0)