Skip to content

Commit 907d5c3

Browse files
committed
update
1 parent 5abcf2d commit 907d5c3

File tree

6 files changed

+203
-2
lines changed

6 files changed

+203
-2
lines changed

internal/cnf/common.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,9 @@ func GetCacheRetentionTime() int64 {
156156
}
157157
return cnf.CacheRetentionTime
158158
}
159+
160+
func GetCacheDisabled() bool {
161+
cnf := NewVMRConf()
162+
cnf.Load()
163+
return cnf.DisableCache
164+
}

internal/cnf/conf.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ type VMRConf struct {
136136
AllowNestedSessions bool `json,toml:"allow_nested_sessions"`
137137
GithubToken string `json,toml:"github_token"`
138138
CacheRetentionTime int64 `json,toml:"cache_retention_time"` // in seconds.
139+
DisableCache bool `json,toml:"disable_cache"`
139140
}
140141

141142
func NewVMRConf() (v *VMRConf) {
@@ -247,3 +248,9 @@ func (v *VMRConf) SetCacheRetentionTime(t int64) {
247248
}
248249
v.Save()
249250
}
251+
252+
func (v *VMRConf) ToggleCache() {
253+
v.Load()
254+
v.DisableCache = !v.DisableCache
255+
v.Save()
256+
}

internal/luapi/lua_global/installer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func EnableFlagDirExcepted(L *lua.LState) int {
146146
lua:
147147
ic = vmrNewInstallerConfig()
148148
os, arch = vmrGetOsArch()
149-
ic = vmrAddBinaryDirs(ic, os, {"usr", "bin", ...})
149+
ic = vmrAddBinaryDirs(ic, os, {"usr", "bin", ...}) -- multi binary dirs can be added.
150150
*/
151151
func AddBinaryDirs(L *lua.LState) int {
152152
ic := checkInstallerConfig(L, 1)

internal/luapi/lua_global/lua.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func (l *Lua) init() {
5959
l.SetGlobal("vmrTrimSuffix", TrimSuffix)
6060
l.SetGlobal("vmrTrim", Trim)
6161
l.SetGlobal("vmrTrimSpace", TrimSpace)
62+
l.SetGlobal("vmrToLower", ToLower)
6263
l.SetGlobal("vmrSprintf", Sprintf)
6364
l.SetGlobal("vmrUrlJoin", UrlJoin)
6465
l.SetGlobal("vmrLenString", LenString)
@@ -67,6 +68,8 @@ func (l *Lua) init() {
6768
l.SetGlobal("vmrExecSystemCmd", ExecSystemCmd)
6869
l.SetGlobal("vmrReadFile", ReadFile)
6970
l.SetGlobal("vmrWriteFile", WriteFile)
71+
l.SetGlobal("vmrCopyFile", CopyFile)
72+
l.SetGlobal("vmrCopyDir", CopyDir)
7073
// version
7174
l.SetGlobal("vmrNewVersionList", NewVersionList)
7275
l.SetGlobal("vmrAddItem", AddItem)

internal/luapi/lua_global/utils.go

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,15 @@ func TrimSpace(L *lua.LState) int {
114114
return 1
115115
}
116116

117+
/*
118+
lua: s = vmrToLower(string)
119+
*/
120+
func ToLower(L *lua.LState) int {
121+
str := L.ToString(1)
122+
L.Push(lua.LString(strings.ToLower(str)))
123+
return 1
124+
}
125+
117126
/*
118127
lua: string = vmrSprintf(pattern, {s1, s2, s3, ...})
119128
*/
@@ -223,11 +232,17 @@ func ReadFile(L *lua.LState) int {
223232
}
224233

225234
/*
226-
lua: bool = vmrWriteFile(filePath string)
235+
lua: bool = vmrWriteFile(filePath string, content string)
227236
*/
228237
func WriteFile(L *lua.LState) int {
229238
filePath := L.ToString(1)
230239
content := L.ToString(2)
240+
if len(content) == 0 {
241+
ud := L.ToUserData(2)
242+
if ud != nil {
243+
content = ud.Value.(string)
244+
}
245+
}
231246
if filePath == "" {
232247
L.Push(lua.LFalse)
233248
return 1
@@ -240,3 +255,33 @@ func WriteFile(L *lua.LState) int {
240255
}
241256
return 1
242257
}
258+
259+
/*
260+
lua: bool = vmrCopyFile(src string, dst string)
261+
*/
262+
func CopyFile(L *lua.LState) int {
263+
src := L.ToString(1)
264+
dst := L.ToString(2)
265+
_, err := utils.CopyFile(src, dst)
266+
if err != nil {
267+
L.Push(lua.LFalse)
268+
} else {
269+
L.Push(lua.LTrue)
270+
}
271+
return 1
272+
}
273+
274+
/*
275+
lua: bool = vmrCopyDir(src string, dst string)
276+
*/
277+
func CopyDir(L *lua.LState) int {
278+
src := L.ToString(1)
279+
dst := L.ToString(2)
280+
err := utils.CopyDirectory(src, dst)
281+
if err != nil {
282+
L.Push(lua.LFalse)
283+
} else {
284+
L.Push(lua.LTrue)
285+
}
286+
return 1
287+
}

internal/utils/copy.go

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package utils
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"io"
7+
"os"
8+
"path/filepath"
9+
10+
"github.com/gogf/gf/v2/util/gconv"
11+
)
12+
13+
func CopyFile(src, dst string) (written int64, err error) {
14+
srcFile, err := os.Open(src)
15+
16+
if err != nil {
17+
return 0, err
18+
}
19+
defer srcFile.Close()
20+
21+
dstFile, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE, os.ModePerm)
22+
if err != nil {
23+
return 0, fmt.Errorf("open dst file failed: %s", err.Error())
24+
}
25+
defer dstFile.Close()
26+
27+
return io.Copy(dstFile, srcFile)
28+
}
29+
30+
// CopyAFile copies the file at the source path to the provided destination.
31+
func CopyAFile(source, destination string) error {
32+
//Validate the source and destination paths
33+
if len(source) == 0 {
34+
return errors.New("no source file path provided")
35+
}
36+
37+
if len(destination) == 0 {
38+
return errors.New("no destination file path provided")
39+
}
40+
41+
//Verify the source path refers to a regular file
42+
sourceFileInfo, err := os.Lstat(source)
43+
if err != nil {
44+
return err
45+
}
46+
47+
//Handle regular files differently than symbolic links and other non-regular files.
48+
if sourceFileInfo.Mode().IsRegular() {
49+
//open the source file
50+
sourceFile, err := os.Open(source)
51+
if err != nil {
52+
return err
53+
}
54+
defer sourceFile.Close()
55+
56+
//create the destinatin file
57+
destinationFile, err := os.Create(destination)
58+
if err != nil {
59+
return err
60+
}
61+
defer destinationFile.Close()
62+
63+
//copy the source file contents to the destination file
64+
if _, err = io.Copy(destinationFile, sourceFile); err != nil {
65+
return err
66+
}
67+
68+
//replicate the source file mode for the destination file
69+
if err := os.Chmod(destination, sourceFileInfo.Mode()); err != nil {
70+
return err
71+
}
72+
} else if sourceFileInfo.Mode()&os.ModeSymlink != 0 {
73+
linkDestinaton, err := os.Readlink(source)
74+
if err != nil {
75+
return errors.New("Unable to read symlink. " + err.Error())
76+
}
77+
78+
if err := os.Symlink(linkDestinaton, destination); err != nil {
79+
return errors.New("Unable to replicate symlink. " + err.Error())
80+
}
81+
} else {
82+
return errors.New("Unable to use io.Copy on file with mode " + gconv.String(sourceFileInfo.Mode()))
83+
}
84+
85+
return nil
86+
}
87+
88+
// CopyDirectory copies the directory at the source path to the provided destination, recursively copying subdirectories.
89+
func CopyDirectory(source string, destination string) error {
90+
if len(source) == 0 || len(destination) == 0 {
91+
return errors.New("file paths must not be empty")
92+
}
93+
94+
//get properties of the source directory
95+
sourceInfo, err := os.Stat(source)
96+
if err != nil {
97+
return err
98+
}
99+
100+
//create the destination directory
101+
err = os.MkdirAll(destination, sourceInfo.Mode())
102+
if err != nil {
103+
return err
104+
}
105+
106+
sourceDirectory, err := os.Open(source)
107+
if err != nil {
108+
return err
109+
}
110+
defer sourceDirectory.Close()
111+
112+
objects, err := sourceDirectory.Readdir(-1)
113+
if err != nil {
114+
return err
115+
}
116+
117+
for _, object := range objects {
118+
if object.Name() == ".Trashes" || object.Name() == ".DS_Store" {
119+
continue
120+
}
121+
122+
sourceObjectName := source + string(filepath.Separator) + object.Name()
123+
destObjectName := destination + string(filepath.Separator) + object.Name()
124+
125+
if object.IsDir() {
126+
//create sub-directories
127+
err = CopyDirectory(sourceObjectName, destObjectName)
128+
if err != nil {
129+
return err
130+
}
131+
} else {
132+
err = CopyAFile(sourceObjectName, destObjectName)
133+
if err != nil {
134+
return err
135+
}
136+
}
137+
}
138+
139+
return nil
140+
}

0 commit comments

Comments
 (0)