-
Notifications
You must be signed in to change notification settings - Fork 1
/
out.go
63 lines (51 loc) · 1.37 KB
/
out.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
package gen
import (
"os"
"path/filepath"
"github.com/apigear-io/cli/pkg/helper"
)
type Output interface {
Write(input []byte, target string, force bool) error
Copy(source, target string, force bool) error
Compare(input []byte, target string) (bool, error)
}
type FileOutput struct {
}
func (f *FileOutput) Write(input []byte, target string, force bool) error {
// write document to file system
dir := filepath.Dir(target)
err := os.MkdirAll(dir, 0755)
if err != nil {
return err
}
return os.WriteFile(target, input, 0644)
}
func (f *FileOutput) Copy(source, target string, force bool) error {
return helper.CopyFile(source, target)
}
func (f *FileOutput) Compare(input []byte, target string) (bool, error) {
return CompareContentWithFile(input, target)
}
type MockOutput struct {
Writes map[string]string
Copies map[string]string
Compares map[string]bool
}
func NewMockOutput() *MockOutput {
return &MockOutput{
Writes: make(map[string]string),
Copies: make(map[string]string),
Compares: make(map[string]bool),
}
}
func (m *MockOutput) Write(input []byte, target string, force bool) error {
m.Writes[target] = string(input)
return nil
}
func (m *MockOutput) Copy(source, target string, force bool) error {
m.Copies[source] = target
return nil
}
func (m *MockOutput) Compare(input []byte, target string) (bool, error) {
return m.Compares[target], nil
}