Skip to content

Commit 7a5f7e4

Browse files
authored
Make more libraries available (micro-editor#1917)
* Make more libraries available to plugin dvelopment * Add Unzip function to util
1 parent 7df04a5 commit 7a5f7e4

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

cmd/micro/initlua.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ func luaImportMicroUtil() *lua.LTable {
144144
ulua.L.SetField(pkg, "GetLeadingWhitespace", luar.New(ulua.L, util.LuaGetLeadingWhitespace))
145145
ulua.L.SetField(pkg, "IsWordChar", luar.New(ulua.L, util.LuaIsWordChar))
146146
ulua.L.SetField(pkg, "String", luar.New(ulua.L, util.String))
147+
ulua.L.SetField(pkg, "Unzip", luar.New(ulua.L, util.Unzip))
148+
ulua.L.SetField(pkg, "Version", luar.New(ulua.L, util.Version))
149+
ulua.L.SetField(pkg, "SemVersion", luar.New(ulua.L, util.SemVersion))
147150
ulua.L.SetField(pkg, "CharacterCountInString", luar.New(ulua.L, util.CharacterCountInString))
148151
ulua.L.SetField(pkg, "RuneStr", luar.New(ulua.L, func(r rune) string {
149152
return string(r)

internal/lua/lua.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package lua
22

33
import (
4+
"archive/zip"
45
"bytes"
56
"errors"
67
"fmt"
@@ -9,6 +10,7 @@ import (
910
"math"
1011
"math/rand"
1112
"net"
13+
"net/http"
1214
"os"
1315
"path"
1416
"path/filepath"
@@ -74,6 +76,10 @@ func Import(pkg string) *lua.LTable {
7476
return importUtf8()
7577
case "humanize":
7678
return importHumanize()
79+
case "net/http", "http":
80+
return importHTTP()
81+
case "archive/zip":
82+
return importArchiveZip()
7783
default:
7884
return nil
7985
}
@@ -383,6 +389,7 @@ func importOs() *lua.LTable {
383389
L.SetField(pkg, "Symlink", luar.New(L, os.Symlink))
384390
L.SetField(pkg, "TempDir", luar.New(L, os.TempDir))
385391
L.SetField(pkg, "Truncate", luar.New(L, os.Truncate))
392+
L.SetField(pkg, "UserHomeDir", luar.New(L, os.UserHomeDir))
386393

387394
return pkg
388395
}
@@ -570,3 +577,22 @@ func importHumanize() *lua.LTable {
570577

571578
return pkg
572579
}
580+
581+
func importHTTP() *lua.LTable {
582+
pkg := L.NewTable()
583+
584+
L.SetField(pkg, "Get", luar.New(L, http.Get))
585+
L.SetField(pkg, "Post", luar.New(L, http.Post))
586+
587+
return pkg
588+
}
589+
590+
func importArchiveZip() *lua.LTable {
591+
pkg := L.NewTable()
592+
593+
L.SetField(pkg, "OpenReader", luar.New(L, zip.OpenReader))
594+
L.SetField(pkg, "NewReader", luar.New(L, zip.NewReader))
595+
L.SetField(pkg, "NewWriter", luar.New(L, zip.NewWriter))
596+
597+
return pkg
598+
}

internal/util/util.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package util
22

33
import (
4+
"archive/zip"
45
"bytes"
56
"errors"
67
"fmt"
8+
"io"
79
"os"
810
"os/user"
911
"path/filepath"
@@ -435,3 +437,56 @@ func ParseSpecial(s string) string {
435437
func String(s []byte) string {
436438
return string(s)
437439
}
440+
441+
// Unzip unzips a file to given folder
442+
func Unzip(src, dest string) error {
443+
r, err := zip.OpenReader(src)
444+
if err != nil {
445+
return err
446+
}
447+
defer r.Close()
448+
449+
os.MkdirAll(dest, 0755)
450+
451+
// Closure to address file descriptors issue with all the deferred .Close() methods
452+
extractAndWriteFile := func(f *zip.File) error {
453+
rc, err := f.Open()
454+
if err != nil {
455+
return err
456+
}
457+
defer rc.Close()
458+
459+
path := filepath.Join(dest, f.Name)
460+
461+
// Check for ZipSlip (Directory traversal)
462+
if !strings.HasPrefix(path, filepath.Clean(dest)+string(os.PathSeparator)) {
463+
return fmt.Errorf("illegal file path: %s", path)
464+
}
465+
466+
if f.FileInfo().IsDir() {
467+
os.MkdirAll(path, f.Mode())
468+
} else {
469+
os.MkdirAll(filepath.Dir(path), f.Mode())
470+
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
471+
if err != nil {
472+
return err
473+
}
474+
defer f.Close()
475+
476+
_, err = io.Copy(f, rc)
477+
if err != nil {
478+
return err
479+
}
480+
}
481+
return nil
482+
}
483+
484+
for _, f := range r.File {
485+
err := extractAndWriteFile(f)
486+
if err != nil {
487+
return err
488+
}
489+
}
490+
491+
return nil
492+
}

runtime/help/plugins.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ The packages and functions are listed below (in Go type signatures):
285285
string is a word character.
286286
- `String(b []byte) string`: converts a byte array to a string.
287287
- `RuneStr(r rune) string`: converts a rune to a string.
288+
- `Unzip(src, dest string) error`: unzips a file to given folder.
288289

289290
This may seem like a small list of available functions but some of the objects
290291
returned by the functions have many methods. The Lua plugin may access any
@@ -358,6 +359,8 @@ strings
358359
regexp
359360
errors
360361
time
362+
archive/zip
363+
net/http
361364
```
362365

363366
For documentation for each of these functions, see the Go standard

0 commit comments

Comments
 (0)