/
utils.go
165 lines (139 loc) · 3.75 KB
/
utils.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"bytes"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"github.com/dustin/go-humanize"
)
// WriteCounter stores dl state of the cl binaries
type WriteCounter struct {
Total uint64
LastEmit uint64
Filename string
a *WalletApplication
}
func (a *WalletApplication) javaInstalled() bool {
var javaInstalled bool
if a.paths.Java[len(a.paths.Java)-9:] != "javaw.exe" {
javaInstalled = false
} else {
javaInstalled = true
}
return javaInstalled
}
func (a *WalletApplication) detectJavaPath() {
if runtime.GOOS == "windows" {
var jwPath string
cmd := exec.Command("cmd", "/c", "where", "java")
a.log.Infoln("Running command: ", cmd)
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out // Captures STDOUT
cmd.Stderr = &stderr // Captures STDERR
err := cmd.Run()
if err != nil {
errFormatted := fmt.Sprint(err) + ": " + stderr.String()
a.log.Errorf(errFormatted)
a.LoginError("Unable to find Java Installation")
a.paths.Java = "No valid path detected"
return
}
jPath := out.String() // May contain multiple
if jPath == "" {
a.LoginError("Unable to find Java Installation")
a.paths.Java = "No valid path detected"
return
}
s := strings.Split(strings.Replace(jPath, "\r\n", "\n", -1), "\n")
jwPath = string(s[0][:len(s[0])-4]) + "w.exe" // Shifting to javaw.exe
if s[1] != "" {
jwPath = string(s[1][:len(s[1])-4]) + "w.exe" // Shifting to javaw.exe
a.log.Info("Detected a secondary java path. Using that over the first one.")
}
a.log.Infoln("Java path selected: " + jwPath)
a.log.Debugln(cmd)
a.paths.Java = jwPath
}
}
//normalizeAmounts takes amount/fee in int64 and normalizes it. Example: passing 821500000000 will return 8215
func normalizeAmounts(i int64) (string, error) {
f := fmt.Sprintf("%.8f", float64(i)/1e8)
return f, nil
}
// TempFileName creates temporary file names for the transaction files
func (a *WalletApplication) TempFileName(prefix, suffix string) string {
randBytes := make([]byte, 16)
rand.Read(randBytes)
return filepath.Join(a.paths.TMPDir, prefix+hex.EncodeToString(randBytes)+suffix)
}
// Write emits the download progress of the CL binaries to the frontend
func (wc *WriteCounter) Write(p []byte) (int, error) {
n := len(p)
wc.Total += uint64(n)
if (wc.Total - wc.LastEmit) > uint64(800) {
wc.a.RT.Events.Emit("downloading", wc.Filename, humanize.Bytes(wc.Total))
wc.LastEmit = wc.Total
}
return n, nil
}
func (a *WalletApplication) fetchWalletJar(filename string, filepath string) error {
url := a.WalletCLI.URL + "/v" + a.WalletCLI.Version + "/" + filename
a.log.Info(url)
out, err := os.Create(filepath + ".tmp")
if err != nil {
return err
}
resp, err := http.Get(url)
if err != nil {
out.Close()
return err
}
defer resp.Body.Close()
counter := &WriteCounter{}
counter.a = a
counter.Filename = filename
counter.LastEmit = uint64(0)
if _, err = io.Copy(out, io.TeeReader(resp.Body, counter)); err != nil {
out.Close()
return err
}
out.Close()
if err = os.Rename(filepath+".tmp", filepath); err != nil {
return err
}
return nil
}
func (a *WalletApplication) directoryCreator(directories ...string) error {
for _, d := range directories {
err := os.MkdirAll(d, os.ModePerm)
if err != nil {
return err
}
}
return nil
}
func (a *WalletApplication) fileExists(path string) bool {
info, err := os.Stat(path)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
// WriteToFile will print any string of text to a file safely by
// checking for errors and syncing at the end.
func WriteToFile(filename string, data []byte) error {
err := ioutil.WriteFile(filename, data, 0666)
if err != nil {
return err
}
return nil
}