Skip to content

Commit

Permalink
lua: plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
moqsien committed Feb 2, 2025
1 parent acc214d commit 1426f45
Show file tree
Hide file tree
Showing 11 changed files with 582 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/luapi/lua_global/conda.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package lua_global
1 change: 1 addition & 0 deletions internal/luapi/lua_global/cs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package lua_global
85 changes: 85 additions & 0 deletions internal/luapi/lua_global/gh/releases.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package gh

import (
"encoding/json"
"fmt"
"io"
"time"

"github.com/gvcgo/goutils/pkgs/request"
)

// ReleaseItem
type Asset struct {
Name string `json:"name"`
Url string `json:"browser_download_url"`
Size int64 `json:"size"`
}

type ReleaseItem struct {
Assets []Asset `json:"assets"`
TagName string `json:"tag_name"`
PreRelease any `json:"prerelease"`
}

type ReleaseList []ReleaseItem

/*
Get github releases
*/

const (
GithubAPI string = "https://api.github.com"
GithubReleaseAPI string = "%s/repos/%s/releases?per_page=100&page=%d"
AcceptHeader string = "application/vnd.github.v3+json"
AuthorizationHeader string = "token %s"
)

type Gh struct {
RepoName string
Token string
Proxy string
ReverseProxy string
fetcher *request.Fetcher
}

func NewGh(repo, token, proxy, reverseProxy string) (g *Gh) {
g = &Gh{
RepoName: repo,
Token: token,
Proxy: proxy,
ReverseProxy: reverseProxy,
}
return
}

func (g *Gh) getRelease(page int) (r []byte) {
if g.fetcher == nil {
g.fetcher = request.NewFetcher()
}

// https://api.github.com/repos/{owner}/{repo}/releases?per_page=100&page=1
dUrl := fmt.Sprintf(GithubReleaseAPI, GithubAPI, g.RepoName, page)
g.fetcher.SetUrl(dUrl)
g.fetcher.Timeout = 180 * time.Second
if resp := g.fetcher.Get(); resp != nil {
defer resp.RawResponse.Body.Close()
r, _ = io.ReadAll(resp.RawResponse.Body)
}
return
}

func (g *Gh) GetReleases() (rl ReleaseList) {
page := 1
for {
itemList := ReleaseList{}
r := g.getRelease(page)
json.Unmarshal(r, &itemList)
if len(itemList) == 0 || page >= 10 {
break
}
rl = append(rl, itemList...)
page++
}
return
}
1 change: 1 addition & 0 deletions internal/luapi/lua_global/github.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package lua_global
165 changes: 165 additions & 0 deletions internal/luapi/lua_global/gjson.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package lua_global

import (
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/util/gconv"
lua "github.com/yuin/gopher-lua"
)

func checkGJson(L *lua.LState) *gjson.Json {
ud := L.CheckUserData(1)
if j, ok := ud.Value.(*gjson.Json); ok {
return j
}
L.ArgError(1, "gjson expected")
return nil
}

func InitGJson(L *lua.LState) int {
arg := L.ToUserData(1)
if j, err := gjson.LoadJson(gconv.String(arg.Value)); err != nil {
prepareResult(L, nil)
return 0
} else {
j.SetViolenceCheck(true)
prepareResult(L, j)
return 1
}
}

func GetGJsonString(L *lua.LState) int {
j := checkGJson(L)
if j == nil {
return 0
}

jPath := L.ToString(2)
if jPath == "" {
return 0
}
res := j.Get(jPath).String()
L.Push(lua.LString(res))
return 1
}

func GetGJsonInt(L *lua.LState) int {
j := checkGJson(L)
if j == nil {
return 0
}

jPath := L.ToString(2)
if jPath == "" {
return 0
}
res := j.Get(jPath).Int()
L.Push(lua.LNumber(res))
return 1
}

func GetGJsonMapEach(L *lua.LState) int {
j := checkGJson(L)
if j == nil {
return 0
}
jPath := L.ToString(2)
if jPath == "" {
return 0
}
res := j.Get(jPath).Map()
if res == nil {
return 0
}

cb := L.ToFunction(3)
for k, v := range res {
ud := L.NewUserData()
ud.Value = v
if err := L.CallByParam(lua.P{
Fn: cb,
NRet: 0,
Protect: true,
}, lua.LString(k), ud); err != nil {
panic(err)
}
}
return 0
}

func GetGJsonFromMapByKey(L *lua.LState) int {
j := checkGJson(L)
if j == nil {
return 0
}
jPath := L.ToString(2)
if jPath == "" {
return 0
}
res := j.Get(jPath).Map()
if res == nil {
return 0
}

key := L.ToString(3)
val := res[key]
L.Push(lua.LString(gconv.String(val)))
return 1
}

func GetGJsonSliceEach(L *lua.LState) int {
j := checkGJson(L)
if j == nil {
return 0
}

jPath := L.ToString(2)
if jPath == "" {
return 0
}

res := j.Get(jPath).Array()

if res == nil {
return 0
}

cb := L.ToFunction(3)
for idx, v := range res {
if v == nil {
continue
}
ud := L.NewUserData()
ud.Value = v
if err := L.CallByParam(lua.P{
Fn: cb,
NRet: 0,
Protect: true,
}, lua.LNumber(idx+1), ud); err != nil {
panic(err)
}
}
return 0
}

func GetGJsonFromSliceByIndex(L *lua.LState) int {
j := checkGJson(L)
if j == nil {
return 0
}
jPath := L.ToString(2)
if jPath == "" {
return 0
}
res := j.Get(jPath).Array()
if res == nil {
return 0
}

index := L.ToInt(3)
if index < 1 || index > len(res) {
return 0
}
val := res[index-1]
L.Push(lua.LString(gconv.String(val)))
return 1
}
48 changes: 48 additions & 0 deletions internal/luapi/lua_global/gjson_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package lua_global

import "testing"

var jsonScript = `local headers = {}
headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
local url = "https://mirrors.tuna.tsinghua.edu.cn/julia-releases/bin/versions.json"
local resp = getResponse(url, 10,headers)
local j = initGJson(resp)
print("----------------gjson----------------")
function parseMapA(k, v)
print("=======***")
local jj = initGJson(v)
local stable = getString(jj, "stable")
if stable ~= "true" then
return
end
function parseSlice(idx, vvv)
local mapJ = initGJson(vvv)
local item = {}
item["version"] = getString(mapJ, "version")
item["url"] = getString(mapJ, "url")
item["sha256"] = getString(mapJ, "sha256")
item["os"] = getString(mapJ, "os")
item["arch"] = getString(mapJ, "arch")
item["size"] = getInt(mapJ, "size")
print(item.url)
end
sliceEach(jj, "files", parseSlice)
end
mapEach(j, ".", parseMapA)
`

func TestGJson(t *testing.T) {
ll := NewLua()
defer ll.Close()
L := ll.GetLState()

if err := L.DoString(jsonScript); err != nil {
t.Error(err)
}
}
Loading

0 comments on commit 1426f45

Please sign in to comment.