/
fake.go
47 lines (34 loc) · 1.1 KB
/
fake.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
package crypto
import (
"fmt"
"path/filepath"
"github.com/labstack/gommon/log"
"github.com/minnowo/astoryofand/internal/util"
)
type FakeEncryptionWriter struct {
AlwaysFailEncrypt bool
AlwaysFailWrite bool
AlwaysFailCanWriteCheck bool
OutputDirectory string
}
const FAIL_MSG string = "FakeEncryptionWriter always fails this!"
func (p *FakeEncryptionWriter) EnsureCanWriteDiskOrExit() {
if p.AlwaysFailCanWriteCheck {
log.Fatal("Cannot write to ", p.OutputDirectory, FAIL_MSG)
}
}
func (p *FakeEncryptionWriter) SaveAndEncryptData(uuid string, json []byte) (string, error) {
log.Warn("Using FakeEncryptionWriter! Nothing is actually about to happen.")
log.Info("Encrypting some raw byte data")
log.Debug(json)
if p.AlwaysFailEncrypt {
return "", fmt.Errorf("Could not encrypt data. %s", FAIL_MSG)
}
outfile := filepath.Join(p.OutputDirectory, util.GetNewOrderName(uuid))
log.Info("Order number: ", uuid)
if p.AlwaysFailWrite {
return "", fmt.Errorf("Could not write encrypted data to a file. %s", FAIL_MSG)
}
log.Info("Order saved:", outfile)
return uuid, nil
}