Exemple #1
0
func reFindSubmatch(re *regexp.Regexp) lua.Function {
	return func(l *lua.State) int {
		s := lua.CheckString(l, 1)
		allSubmatch := re.FindStringSubmatch(s)
		return util.DeepPush(l, allSubmatch)
	}
}
Exemple #2
0
func split(l *lua.State) int {
	str := lua.CheckString(l, 1)
	sep := lua.CheckString(l, 2)

	strArr := strings.Split(str, sep)

	return util.DeepPush(l, strArr)
}
Exemple #3
0
func reFindAll(re *regexp.Regexp) lua.Function {
	return func(l *lua.State) int {
		s := lua.CheckString(l, 1)
		n := lua.CheckInteger(l, 2)
		all := re.FindAllString(s, n)
		return util.DeepPush(l, all)
	}
}
Exemple #4
0
func unmarshal(l *lua.State) int {
	payload := lua.CheckString(l, 1)

	var output interface{}

	if err := json.Unmarshal([]byte(payload), &output); err != nil {
		lua.Errorf(l, err.Error())
		panic("unreachable")
	}
	return util.DeepPush(l, output)
}
func testRandomMap(t *testing.T, size int) {
	l := lua.NewState()
	want := randomMap(size)

	util.DeepPush(l, want)
	got, err := util.PullStringTable(l, 1)

	if err != nil {
		t.Fatalf("pulling table, %v", err)
	}

	checkMaps(t, want, got)
}
func oauthRequest(l *lua.State, entryName, method, urlString string, body string, query string) int {
	req, err := http.NewRequest(method, urlString, bytes.NewBuffer([]byte(body)))

	if err != nil {
		lua.Errorf(l, "%s", err.Error())
		panic("unreachable")
	}

	if query != "" {
		parsedQuery, err := url.ParseQuery(query)

		if err != nil {
			lua.Errorf(l, "%s", err.Error())
			panic("unreachable")
		}

		req.Form = parsedQuery
	}

	res, err := oauth.Do(entryName, req)

	if err != nil {
		lua.Errorf(l, "%s", err.Error())
		panic("unreachable")
	}

	defer res.Body.Close()

	data, err := ioutil.ReadAll(res.Body)

	if err != nil {
		lua.Errorf(l, "%s", err.Error())
		panic("unreachable")
	}

	util.DeepPush(l, string(data))

	return 1
}
var excelLibrary = []lua.RegistryFunction{
	{
		"import",
		func(l *lua.State) int {

			path := lua.CheckString(l, 1)

			res, err := xlsx.FileToSlice(path)

			if err != nil {
				lua.Errorf(l, "%s", err)
				panic("unreachable")
			}

			util.DeepPush(l, res)
			return 1
		},
	},
}

func openExcelLibrary(l *lua.State) {
	open := func(l *lua.State) int {
		lua.NewLibrary(l, excelLibrary)
		return 1
	}

	lua.Require(l, "telemetry/excel", open, false)
	l.Pop(1)
}
)

var mongoDBFunctions = map[string]func(db *mgo.Database) lua.Function{
	"collections": func(db *mgo.Database) lua.Function {
		return func(l *lua.State) int {
			names, err := db.CollectionNames()

			if err != nil {
				lua.Errorf(l, "%s", err.Error())
				panic("unreachable")
			}

			pushArray(l)

			for index, value := range names {
				util.DeepPush(l, value)
				l.RawSetInt(-2, index+1)
			}

			return 1
		}
	},

	"collection": func(db *mgo.Database) lua.Function {
		return func(l *lua.State) int {
			pushMongoCollection(l, db, lua.CheckString(l, 1))

			return 1
		}
	},
				result = append(result, row)
			}

			l.CreateTable(len(result), 0)

			pushArray(l)

			for index, value := range result {
				for i, v := range value {
					if vv, ok := v.([]byte); ok {
						value[i] = string(vv)
					}
				}

				util.DeepPush(l, value)
				l.RawSetInt(-2, index+1)
			}

			return 1
		}
	},
}

func pushSQLInstance(l *lua.State, driverName, dataSourceName string) {
	instance, err := sqlx.Open(driverName, dataSourceName)

	if err != nil {
		lua.Errorf(l, "%s", err.Error())
		panic("unreachable")
	}
					lua.Errorf(l, "%s", err)
					panic("unreachable")
				}
			} else {
				lua.Errorf(l, "Only tables can be converted to XML")
				panic("unreachable")
			}

			res, err := mxj.Map(v.(map[string]interface{})).Xml()

			if err != nil {
				lua.Errorf(l, "%s", err.Error())
				panic("unreachable")
			}

			util.DeepPush(l, string(res))

			return 1
		},
	},

	{
		"decode",
		func(l *lua.State) int {
			res, err := mxj.NewMapXml([]byte(lua.CheckString(l, 1)))

			if err != nil {
				lua.Errorf(l, "%s", err.Error())
				panic("unreachable")
			}
package lua

import (
	"time"

	"github.com/telemetryapp/go-lua"
	"github.com/telemetryapp/goluago/util"
	"github.com/telemetryapp/gotelemetry_agent/agent/aggregations"
	"github.com/telemetryapp/gotelemetry_agent/agent/config"
)

var seriesFunctions = map[string]func(s *aggregations.Series) lua.Function{
	"name": func(s *aggregations.Series) lua.Function {
		return func(l *lua.State) int {
			util.DeepPush(l, s.Name)

			return 1
		}
	},

	"trimSince": func(s *aggregations.Series) lua.Function {
		return func(l *lua.State) int {

			if l.TypeOf(1) == lua.TypeString {
				duration, err := config.ParseTimeInterval(lua.CheckString(l, 1))

				if err != nil {
					lua.Errorf(l, "%s", err)
					panic("unreachable")
				}
var mongoConnectionFunctions = map[string]func(s *mgo.Session) lua.Function{
	"db": func(s *mgo.Session) lua.Function {
		return func(l *lua.State) int {
			pushMongoDatabase(l, s, lua.CheckString(l, 1))

			return 1
		}
	},

	"live_servers": func(s *mgo.Session) lua.Function {
		return func(l *lua.State) int {
			pushArray(l)

			for index, server := range s.LiveServers() {
				util.DeepPush(l, server)
				l.RawSetInt(-2, index+1)
			}

			return 1
		}
	},

	"close": func(s *mgo.Session) lua.Function {
		return func(l *lua.State) int {
			s.Close()

			return 0
		}
	},
}