-
Notifications
You must be signed in to change notification settings - Fork 244
/
fileutils.go
132 lines (119 loc) · 4.56 KB
/
fileutils.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package testingutil
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/pkg/errors"
)
// TempMkdir creates a temporary directory
func TempMkdir(parentDir string, newDirPrefix string) (string, error) {
parentDir = filepath.FromSlash(parentDir)
dir, err := ioutil.TempDir(parentDir, newDirPrefix)
if err != nil {
return "", errors.Wrapf(err, "failed to create dir with prefix %s in directory %s", newDirPrefix, parentDir)
}
return dir, nil
}
// TempMkFile creates a temporary file.
func TempMkFile(dir string, fileName string) (string, error) {
dir = filepath.FromSlash(dir)
f, err := ioutil.TempFile(dir, fileName)
if err != nil {
return "", errors.Wrapf(err, "failed to create test file %s in dir %s", fileName, dir)
}
if err := f.Close(); err != nil {
return "", err
}
return f.Name(), nil
}
// FileType custom type to indicate type of file
type FileType int
const (
// RegularFile enum to represent regular file
RegularFile FileType = 0
// Directory enum to represent directory
Directory FileType = 1
)
// ModificationType custom type to indicate file modification type
type ModificationType string
const (
// UPDATE enum representing update operation on a file
UPDATE ModificationType = "update"
// CREATE enum representing create operation for a file/folder
CREATE ModificationType = "create"
// DELETE enum representing delete operation for a file/folder
DELETE ModificationType = "delete"
// APPEND enum representing append operation on a file
APPEND ModificationType = "append"
)
// FileProperties to contain meta-data of a file like, file/folder name, file/folder parent dir, file type and desired file modification type
type FileProperties struct {
FilePath string
FileParent string
FileType FileType
ModificationType ModificationType
}
// SimulateFileModifications mock function to simulate requested file/folder operation
// Parameters:
// basePath: The parent directory for file/folder involved in desired file operation
// fileModification: Meta-data of file/folder
// Returns:
// path to file/folder involved in the operation
// error if any or nil
func SimulateFileModifications(basePath string, fileModification FileProperties) (string, error) {
// Files/folders intended to be directly under basepath will be indicated by fileModification.FileParent set to empty string
if fileModification.FileParent != "" {
// If fileModification.FileParent is not empty, use it to generate file/folder absolute path
basePath = filepath.Join(basePath, fileModification.FileParent)
}
switch fileModification.ModificationType {
case CREATE:
if fileModification.FileType == Directory {
filePath, err := TempMkdir(basePath, fileModification.FilePath)
// t.Logf("In simulateFileModifications, Attempting to create folder %s in %s. Error : %v", fileModification.filePath, basePath, err)
return filePath, err
} else if fileModification.FileType == RegularFile {
folderPath, err := TempMkFile(basePath, fileModification.FilePath)
// t.Logf("In simulateFileModifications, Attempting to create file %s in %s", fileModification.filePath, basePath)
return folderPath, err
}
case DELETE:
if fileModification.FileType == Directory {
return filepath.Join(basePath, fileModification.FilePath), os.RemoveAll(filepath.Join(basePath, fileModification.FilePath))
} else if fileModification.FileType == RegularFile {
return filepath.Join(basePath, fileModification.FilePath), os.Remove(filepath.Join(basePath, fileModification.FilePath))
}
case UPDATE:
if fileModification.FileType == Directory {
return "", fmt.Errorf("Updating directory %s is not supported", fileModification.FilePath)
} else if fileModification.FileType == RegularFile {
f, err := os.Open(filepath.Join(basePath, fileModification.FilePath))
if err != nil {
return "", err
}
if _, err := f.WriteString("Hello from Odo"); err != nil {
return "", err
}
if err := f.Sync(); err != nil {
return "", err
}
if err := f.Close(); err != nil {
return "", err
}
return filepath.Join(basePath, fileModification.FilePath), nil
}
case APPEND:
if fileModification.FileType == RegularFile {
err := ioutil.WriteFile(filepath.Join(basePath, fileModification.FilePath), []byte("// Check watch command"), os.ModeAppend)
if err != nil {
return "", err
}
return filepath.Join(basePath, fileModification.FilePath), nil
}
return "", fmt.Errorf("Append not supported for file of type %v", fileModification.FileType)
default:
return "", fmt.Errorf("Unsupported file operation %s", fileModification.ModificationType)
}
return "", nil
}