-
Notifications
You must be signed in to change notification settings - Fork 93
/
datadir.go
53 lines (46 loc) · 1.19 KB
/
datadir.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
package config
import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
)
func algodPaths(datadir string) (netpath, tokenpath string) {
netpath = filepath.Join(datadir, "algod.net")
tokenpath = filepath.Join(datadir, "algod.token")
return
}
func algodStat(netpath string) (lastmod time.Time, err error) {
nstat, err := os.Stat(netpath)
if err != nil {
err = fmt.Errorf("%s: %v", netpath, err)
return
}
lastmod = nstat.ModTime()
return
}
// AlgodArgsForDataDir opens the token and network files in the data directory, returning data for constructing client
func AlgodArgsForDataDir(datadir string) (netAddr string, token string, lastmod time.Time, err error) {
netpath, tokenpath := algodPaths(datadir)
var netaddrbytes []byte
netaddrbytes, err = os.ReadFile(netpath)
if err != nil {
err = fmt.Errorf("%s: %v", netpath, err)
return
}
netAddr = strings.TrimSpace(string(netaddrbytes))
if !strings.HasPrefix(netAddr, "http") {
netAddr = "http://" + netAddr
}
tokenBytes, err := os.ReadFile(tokenpath)
if err != nil {
err = fmt.Errorf("%s: %v", tokenpath, err)
return
}
token = strings.TrimSpace(string(tokenBytes))
if err == nil {
lastmod, err = algodStat(netpath)
}
return
}