-
Notifications
You must be signed in to change notification settings - Fork 126
/
text-writer.go
41 lines (34 loc) · 1.18 KB
/
text-writer.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
package writers
import (
"fmt"
"os"
"strings"
"time"
log "github.com/sirupsen/logrus"
)
// FileWriter - One implementation of the Writer interface. This one will write to txt files, and generate file names like yyyy-mm-dd-exchange-market.txt.
type FileWriter struct{}
// GetWriteFileName - Will generate the file name for you of the format yyyy-mm-dd-exchange-market.txt. New files will be created at midnight because the scrapers are calling this method each time before writing to file.
func (f *FileWriter) GetWriteFileName(exchange string, market string) string {
now := time.Now()
return f.clean(fmt.Sprintf("%v-%v-%v-%v-%v.txt", now.Year(), int(now.Month()), now.Day(), exchange, market))
}
// Write - Will write to the filename the line.
func (f *FileWriter) Write(line string, filename string) (int, error) {
file, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0666) //nolint:gosec
if err != nil {
return 0, err
}
n, err := file.WriteString(line)
if err != nil {
cerr := file.Close()
if cerr != nil {
log.Error(cerr)
}
return n, err
}
return n, file.Close()
}
func (f *FileWriter) clean(s string) string {
return strings.Replace(s, "/", "_", -1)
}