Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b41786b

Browse files
little bit of renaming inside the crypto reader project
1 parent ff56a8e commit b41786b

File tree

11 files changed

+139
-134
lines changed

11 files changed

+139
-134
lines changed
File renamed without changes.

‎mutexes/log-reader/cmd/generator/main.go‎ renamed to ‎mutexes/crypto-reader/cmd/generator/main.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func main() {
5353
}
5454

5555
bs := generateTransactions(cfg, now)
56-
filename := fmt.Sprintf("transaction-%s", now.Format(dateFileFormat))
56+
filename := fmt.Sprintf("transaction-%s.txt", now.Format(dateFileFormat))
5757
file, err := os.Create(path.Join(*directoryFlag, filename))
5858
if err != nil {
5959
log.Fatalf("could not create file: %v", err)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"flag"
6+
"log"
7+
"os"
8+
"os/signal"
9+
"syscall"
10+
"time"
11+
12+
"githubc.com/steevehook/crypto-reader/crypto"
13+
)
14+
15+
func main() {
16+
quit := make(chan os.Signal, 1)
17+
addrFlag := flag.String("addr", "", "the crypto address to look for")
18+
intervalFlag := flag.Duration("interval", time.Hour, "the interval of transactions to look for")
19+
dirFlag := flag.String("dir", ".", "the directory of crypto transaction files")
20+
nFlag := flag.Int("n", 100, "the maximum number of transactions to read")
21+
22+
if *addrFlag == "" {
23+
log.Fatal("provide a crypto address to read transactions for")
24+
}
25+
26+
flag.Parse()
27+
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
28+
29+
ctx, cancel := context.WithCancel(context.Background())
30+
cfg := crypto.TransactionsReaderConfig{
31+
Address: *addrFlag,
32+
Interval: *intervalFlag,
33+
Directory: *dirFlag,
34+
Limit: *nFlag,
35+
}
36+
reader, err := crypto.NewTransactionsReader(cfg)
37+
if err != nil {
38+
log.Fatalf("could not create crypto transactions reader: %v", err)
39+
}
40+
41+
go func() {
42+
err := reader.Read(ctx, os.Stdout)
43+
if err != nil {
44+
log.Fatalf("could not read crypto transactions: %v", err)
45+
}
46+
47+
quit <- os.Interrupt
48+
}()
49+
50+
<-quit
51+
cancel()
52+
}

‎mutexes/crypto-reader/crypto/file.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package crypto
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package crypto
2+
3+
import (
4+
"bufio"
5+
"context"
6+
"io"
7+
"io/ioutil"
8+
"os"
9+
"sort"
10+
"time"
11+
)
12+
13+
// TransactionsReaderConfig represents the configuration to start the crypto transactions reader
14+
type TransactionsReaderConfig struct {
15+
Address string
16+
Interval time.Duration
17+
Directory string
18+
Limit int
19+
}
20+
21+
// NewTransactionsReader creates a new instance of log reader
22+
func NewTransactionsReader(cfg TransactionsReaderConfig) (*TransactionsReader, error) {
23+
filesInfo, err := ioutil.ReadDir(cfg.Directory)
24+
if err != nil {
25+
return nil, err
26+
}
27+
28+
info := make([]os.FileInfo, 0, len(filesInfo))
29+
for _, fi := range filesInfo {
30+
if fi.IsDir() {
31+
continue
32+
}
33+
34+
info = append(info, fi)
35+
}
36+
sort.Slice(filesInfo, func(i, j int) bool {
37+
return filesInfo[i].ModTime().Sub(filesInfo[j].ModTime()) < 0
38+
})
39+
40+
reader := &TransactionsReader{
41+
cfg: cfg,
42+
filesInfo: info,
43+
}
44+
return reader, nil
45+
}
46+
47+
// TransactionsReader represents the crypto transactions reader type
48+
// responsible for reading crypto transactions within a given interval for a given address
49+
type TransactionsReader struct {
50+
cfg TransactionsReaderConfig
51+
filesInfo []os.FileInfo
52+
}
53+
54+
// Read reads and streams the crypto transactions to an io.Writer using the given config
55+
func (r *TransactionsReader) Read(ctx context.Context, w io.Writer) error {
56+
select {
57+
case <-ctx.Done():
58+
return nil
59+
default:
60+
return r.read(w)
61+
}
62+
}
63+
64+
func (r *TransactionsReader) read(w io.Writer) error {
65+
return nil
66+
}
67+
68+
func (r *TransactionsReader) stream(file io.ReadCloser) chan string {
69+
out := make(chan string)
70+
71+
go func() {
72+
defer func() { _ = file.Close() }()
73+
scanner := bufio.NewScanner(file)
74+
for scanner.Scan() {
75+
out <- scanner.Text()
76+
}
77+
78+
close(out)
79+
}()
80+
81+
return out
82+
}

‎mutexes/crypto-reader/go.mod‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module githubc.com/steevehook/crypto-reader
2+
3+
go 1.17
File renamed without changes.

‎mutexes/log-reader/go.mod‎

Lines changed: 0 additions & 3 deletions
This file was deleted.

‎mutexes/log-reader/logging/file.go‎

Lines changed: 0 additions & 1 deletion
This file was deleted.

‎mutexes/log-reader/logging/reader.go‎

Lines changed: 0 additions & 82 deletions
This file was deleted.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /