forked from Dataport/xk6-filewriter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
k6FileWriter.go
82 lines (70 loc) · 1.93 KB
/
k6FileWriter.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
package fileWriter
import (
"errors"
"fmt"
"go.k6.io/k6/js/modules"
"os"
)
type FileWriter struct{}
func init() {
modules.Register("k6/x/filewriter", new(FileWriter))
}
func (d *FileWriter) WriteString(path string, filename string, s string) error {
//check if path exists
_, err := os.Stat(path)
pathString := fmt.Sprintf("%s%s", path, filename)
if err != nil {
os.MkdirAll(path, 0750)
file, pErr := os.Create(pathString)
if pErr != nil {
var errorsString string = fmt.Sprintf("Path with file: %s can not be created successfully", pathString)
return errors.New(errorsString)
}
file.WriteString(s)
return nil
} else {
err := d.AppendString(path, filename, s)
return err
}
}
func (d *FileWriter) AppendString(path string, filename string, s string) error {
pathString := fmt.Sprintf("%s%s", path, filename)
f, err := os.OpenFile(pathString,
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0750)
if err != nil {
return err
}
defer f.Close()
newLineString := fmt.Sprintf("\n%s", s)
_, writeErr := f.WriteString(newLineString)
if writeErr != nil {
return writeErr
}
return nil
}
// note: no check if other fileWriter threads are running
// note: on Windows write protected files can be overwritten when not protected against deletion
func (d *FileWriter) CreateFile(path string, filename string) error {
// check and create path
_, err := os.Stat(path)
if err != nil {
os.MkdirAll(path, 0750)
}
filePath := fmt.Sprintf("%s%s", path, filename)
// delete existing file
_, err = os.Stat(filePath);
if !errors.Is(err, os.ErrNotExist) {
err := os.Remove(filePath)
if(err != nil){
var errorsString string = fmt.Sprintf("File %s cannot be removed", filePath)
return errors.New(errorsString)
}
}
// create new file
_, err = os.Create(filePath)
if err != nil {
var errorsString string = fmt.Sprintf("File %s cannot be created successfully", filePath)
return errors.New(errorsString)
}
return nil
}