From 7a5f7e443a51455acb307e9212377d7cfaedefac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Henrique=20Guard=C3=A3o=20Gandarez?= <782854+gandarez@users.noreply.github.com> Date: Sat, 21 Nov 2020 03:46:17 -0300 Subject: [PATCH 1/3] Make more libraries available (#1917) * Make more libraries available to plugin dvelopment * Add Unzip function to util --- cmd/micro/initlua.go | 3 +++ internal/lua/lua.go | 26 +++++++++++++++++++ internal/util/util.go | 55 +++++++++++++++++++++++++++++++++++++++++ runtime/help/plugins.md | 3 +++ 4 files changed, 87 insertions(+) diff --git a/cmd/micro/initlua.go b/cmd/micro/initlua.go index 9d90b482..2ed94c3d 100644 --- a/cmd/micro/initlua.go +++ b/cmd/micro/initlua.go @@ -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) diff --git a/internal/lua/lua.go b/internal/lua/lua.go index 9e502533..59ca6c45 100644 --- a/internal/lua/lua.go +++ b/internal/lua/lua.go @@ -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 +} diff --git a/internal/util/util.go b/internal/util/util.go index a40f9062..667b8565 100644 --- a/internal/util/util.go +++ b/internal/util/util.go @@ -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 +} diff --git a/runtime/help/plugins.md b/runtime/help/plugins.md index eff48d06..1818b96b 100644 --- a/runtime/help/plugins.md +++ b/runtime/help/plugins.md @@ -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 From 3fb5a7053fb5fb0c345e95dd0816594e179b7a1f Mon Sep 17 00:00:00 2001 From: Alekhine51 Date: Tue, 8 Dec 2020 22:43:37 -0500 Subject: [PATCH 2/3] Added a sentence to colors.md clarifying that the truecolor environment variable has to be created by the user. (#1928) --- runtime/help/colors.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/runtime/help/colors.md b/runtime/help/colors.md index 9ebb8689..bc64c75e 100644 --- a/runtime/help/colors.md +++ b/runtime/help/colors.md @@ -87,7 +87,8 @@ These may vary widely based on the 16 colors selected for your terminal. True color requires your terminal to support it. This means that the environment variable `COLORTERM` should have the value `truecolor`, `24bit`, or `24-bit`. In addition, to enable true color in micro, the environment -variable `MICRO_TRUECOLOR` must be set to 1. +variable `MICRO_TRUECOLOR` must be set to 1. Note that you have to create +and set this variable yourself. * `solarized-tc`: this is the solarized colorscheme for true color. * `atom-dark-tc`: this colorscheme is based off of Atom's "dark" colorscheme. From 4c21808c6cbde10521b9a44181b39a235d301bf9 Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Wed, 16 Dec 2020 21:35:07 -0500 Subject: [PATCH 3/3] Remove clipboard error message --- cmd/micro/micro.go | 3 ++- internal/clipboard/clipboard.go | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/cmd/micro/micro.go b/cmd/micro/micro.go index 0a2a55aa..e274e599 100644 --- a/cmd/micro/micro.go +++ b/cmd/micro/micro.go @@ -4,6 +4,7 @@ import ( "flag" "fmt" "io/ioutil" + "log" "os" "os/signal" "regexp" @@ -351,7 +352,7 @@ func main() { } if clipErr != nil { - action.InfoBar.Error(clipErr, " or change 'clipboard' option") + log.Println(clipErr, " or change 'clipboard' option") } if a := config.GetGlobalOption("autosave").(float64); a > 0 { diff --git a/internal/clipboard/clipboard.go b/internal/clipboard/clipboard.go index 6c68c40f..07287d29 100644 --- a/internal/clipboard/clipboard.go +++ b/internal/clipboard/clipboard.go @@ -42,6 +42,7 @@ func Initialize(m Method) error { case External: err = clipboard.Initialize() } + CurrentMethod = Internal return err }