]> git.lizzy.rs Git - micro.git/commitdiff
Make more libraries available (#1917)
authorCarlos Henrique Guardão Gandarez <782854+gandarez@users.noreply.github.com>
Sat, 21 Nov 2020 06:46:17 +0000 (03:46 -0300)
committerGitHub <noreply@github.com>
Sat, 21 Nov 2020 06:46:17 +0000 (01:46 -0500)
* Make more libraries available to plugin dvelopment

* Add Unzip function to util

cmd/micro/initlua.go
internal/lua/lua.go
internal/util/util.go
runtime/help/plugins.md

index 9d90b48239f4de2a5aa42ffa9d26f4507b7f37e0..2ed94c3d0f76491c42f0ac6ec60406537e6e1f5c 100644 (file)
@@ -144,6 +144,9 @@ func luaImportMicroUtil() *lua.LTable {
        ulua.L.SetField(pkg, "GetLeadingWhitespace", luar.New(ulua.L, util.LuaGetLeadingWhitespace))
        ulua.L.SetField(pkg, "IsWordChar", luar.New(ulua.L, util.LuaIsWordChar))
        ulua.L.SetField(pkg, "String", luar.New(ulua.L, util.String))
+       ulua.L.SetField(pkg, "Unzip", luar.New(ulua.L, util.Unzip))
+       ulua.L.SetField(pkg, "Version", luar.New(ulua.L, util.Version))
+       ulua.L.SetField(pkg, "SemVersion", luar.New(ulua.L, util.SemVersion))
        ulua.L.SetField(pkg, "CharacterCountInString", luar.New(ulua.L, util.CharacterCountInString))
        ulua.L.SetField(pkg, "RuneStr", luar.New(ulua.L, func(r rune) string {
                return string(r)
index 9e5025330f1b1a0bc898a1d9a4df06d45cbf74ec..59ca6c451826935830de746f4fa7dc8ce7cb8cbb 100644 (file)
@@ -1,6 +1,7 @@
 package lua
 
 import (
+       "archive/zip"
        "bytes"
        "errors"
        "fmt"
@@ -9,6 +10,7 @@ import (
        "math"
        "math/rand"
        "net"
+       "net/http"
        "os"
        "path"
        "path/filepath"
@@ -74,6 +76,10 @@ func Import(pkg string) *lua.LTable {
                return importUtf8()
        case "humanize":
                return importHumanize()
+       case "net/http", "http":
+               return importHTTP()
+       case "archive/zip":
+               return importArchiveZip()
        default:
                return nil
        }
@@ -383,6 +389,7 @@ func importOs() *lua.LTable {
        L.SetField(pkg, "Symlink", luar.New(L, os.Symlink))
        L.SetField(pkg, "TempDir", luar.New(L, os.TempDir))
        L.SetField(pkg, "Truncate", luar.New(L, os.Truncate))
+       L.SetField(pkg, "UserHomeDir", luar.New(L, os.UserHomeDir))
 
        return pkg
 }
@@ -570,3 +577,22 @@ func importHumanize() *lua.LTable {
 
        return pkg
 }
+
+func importHTTP() *lua.LTable {
+       pkg := L.NewTable()
+
+       L.SetField(pkg, "Get", luar.New(L, http.Get))
+       L.SetField(pkg, "Post", luar.New(L, http.Post))
+
+       return pkg
+}
+
+func importArchiveZip() *lua.LTable {
+       pkg := L.NewTable()
+
+       L.SetField(pkg, "OpenReader", luar.New(L, zip.OpenReader))
+       L.SetField(pkg, "NewReader", luar.New(L, zip.NewReader))
+       L.SetField(pkg, "NewWriter", luar.New(L, zip.NewWriter))
+
+       return pkg
+}
index a40f9062ee496501d022828b10d94d3419bb63be..667b85650b04ea739e65e933380f29f40383f2a1 100644 (file)
@@ -1,9 +1,11 @@
 package util
 
 import (
+       "archive/zip"
        "bytes"
        "errors"
        "fmt"
+       "io"
        "os"
        "os/user"
        "path/filepath"
@@ -435,3 +437,56 @@ func ParseSpecial(s string) string {
 func String(s []byte) string {
        return string(s)
 }
+
+// Unzip unzips a file to given folder
+func Unzip(src, dest string) error {
+       r, err := zip.OpenReader(src)
+       if err != nil {
+               return err
+       }
+       defer r.Close()
+
+       os.MkdirAll(dest, 0755)
+
+       // Closure to address file descriptors issue with all the deferred .Close() methods
+       extractAndWriteFile := func(f *zip.File) error {
+               rc, err := f.Open()
+               if err != nil {
+                       return err
+               }
+               defer rc.Close()
+
+               path := filepath.Join(dest, f.Name)
+
+               // Check for ZipSlip (Directory traversal)
+               if !strings.HasPrefix(path, filepath.Clean(dest)+string(os.PathSeparator)) {
+                       return fmt.Errorf("illegal file path: %s", path)
+               }
+
+               if f.FileInfo().IsDir() {
+                       os.MkdirAll(path, f.Mode())
+               } else {
+                       os.MkdirAll(filepath.Dir(path), f.Mode())
+                       f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
+                       if err != nil {
+                               return err
+                       }
+                       defer f.Close()
+
+                       _, err = io.Copy(f, rc)
+                       if err != nil {
+                               return err
+                       }
+               }
+               return nil
+       }
+
+       for _, f := range r.File {
+               err := extractAndWriteFile(f)
+               if err != nil {
+                       return err
+               }
+       }
+
+       return nil
+}
index eff48d06ecfb03a85ccc0d3d105fe3ba01b10aa6..1818b96bddd4d11f677f4b4439c6556f2d214830 100644 (file)
@@ -285,6 +285,7 @@ The packages and functions are listed below (in Go type signatures):
        string is a word character.
     - `String(b []byte) string`: converts a byte array to a string.
     - `RuneStr(r rune) string`: converts a rune to a string.
+    - `Unzip(src, dest string) error`: unzips a file to given folder.
 
 This may seem like a small list of available functions but some of the objects
 returned by the functions have many methods. The Lua plugin may access any
@@ -358,6 +359,8 @@ strings
 regexp
 errors
 time
+archive/zip
+net/http
 ```
 
 For documentation for each of these functions, see the Go standard