2024-03-16 17:05:19 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2024-04-07 15:11:10 -05:00
|
|
|
"fmt"
|
2024-03-16 17:05:19 -05:00
|
|
|
|
|
|
|
"github.com/yuin/gopher-lua"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Server holds information about a Gitea server.
|
|
|
|
type Server struct {
|
|
|
|
servername string
|
2024-03-19 18:11:49 -05:00
|
|
|
url string
|
|
|
|
username string
|
|
|
|
token string
|
2024-04-07 14:25:53 -05:00
|
|
|
primary bool
|
2024-03-16 17:05:19 -05:00
|
|
|
}
|
|
|
|
|
2024-04-07 15:11:10 -05:00
|
|
|
// Servers holds all of the server structures.
|
|
|
|
type Servers map[string]*Server
|
|
|
|
|
|
|
|
// getPrimary gets the primary server contained in this map. Returns an error if no primary server is defined.
|
|
|
|
func (servers *Servers) getPrimary() (*Server, error) {
|
|
|
|
for _, s := range *servers {
|
|
|
|
if s.primary {
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &Server{}, errors.New("No primary server found.")
|
|
|
|
}
|
|
|
|
|
2024-03-16 17:05:19 -05:00
|
|
|
// Configuration holds data retrieved from a Lua script.
|
|
|
|
type Configuration struct {
|
2024-04-07 15:11:10 -05:00
|
|
|
servers Servers
|
2024-03-16 17:05:19 -05:00
|
|
|
}
|
|
|
|
|
2024-04-07 14:25:53 -05:00
|
|
|
// getStringFromTable parses a text value from the configuration file.
|
2024-03-16 17:05:19 -05:00
|
|
|
func getStringFromTable(L *lua.LState, lobj lua.LValue, key string, out *string) error {
|
|
|
|
lv := L.GetTable(lobj, lua.LString(key))
|
|
|
|
if text, ok := lv.(lua.LString); ok {
|
|
|
|
*out = string(text)
|
|
|
|
} else {
|
|
|
|
return errors.New("Text is not a string!")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-07 15:11:10 -05:00
|
|
|
// getBoolFromTable parses a boolean value from the configuration file.
|
|
|
|
func getBoolFromTable(L *lua.LState, lobj lua.LValue, key string, out *bool) error {
|
|
|
|
lv := L.GetTable(lobj, lua.LString(key))
|
|
|
|
if val, ok := lv.(lua.LBool); ok {
|
|
|
|
*out = bool(val)
|
|
|
|
} else {
|
|
|
|
return errors.New(fmt.Sprintf("Failed to get configuration value '%s'", key))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-16 17:05:19 -05:00
|
|
|
// parseServerFromTable parses a Lua table into a Server structure.
|
|
|
|
func parseServerFromTable(L *lua.LState, table lua.LValue) (Server, error) {
|
|
|
|
server := Server{}
|
|
|
|
if err := getStringFromTable(L, table, "servername", &server.servername); err != nil {
|
|
|
|
return server, err
|
|
|
|
}
|
|
|
|
if err := getStringFromTable(L, table, "username", &server.username); err != nil {
|
|
|
|
return server, err
|
|
|
|
}
|
|
|
|
if err := getStringFromTable(L, table, "token", &server.token); err != nil {
|
|
|
|
return server, err
|
|
|
|
}
|
|
|
|
if err := getStringFromTable(L, table, "url", &server.url); err != nil {
|
|
|
|
return server, err
|
|
|
|
}
|
2024-03-19 18:11:49 -05:00
|
|
|
|
2024-04-07 15:11:10 -05:00
|
|
|
lv := L.GetTable(table, lua.LString("primary"))
|
|
|
|
if val, ok := lv.(lua.LBool); ok {
|
|
|
|
server.primary = bool(val)
|
|
|
|
} else if _, ok := lv.(*lua.LNilType); !ok {
|
|
|
|
panic("Config error : " + server.servername + " : Primary is neither nil nor boolean")
|
|
|
|
}
|
|
|
|
|
2024-03-16 17:05:19 -05:00
|
|
|
return server, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Configuration.Parse parses a Lua configuration file into a Configuration struct.
|
|
|
|
func (config *Configuration) Parse(fname string) error {
|
2024-03-21 18:28:19 -05:00
|
|
|
config.servers = make(map[string]*Server)
|
2024-03-16 17:05:19 -05:00
|
|
|
L := lua.NewState()
|
|
|
|
defer L.Close()
|
|
|
|
|
|
|
|
L.DoFile(fname)
|
|
|
|
table := L.Get(-1)
|
|
|
|
|
2024-04-07 15:11:10 -05:00
|
|
|
found_primary := false
|
2024-03-16 17:05:19 -05:00
|
|
|
for i := 1; i <= L.ObjLen(table); i++ {
|
|
|
|
lserver := L.GetTable(table, lua.LNumber(i))
|
|
|
|
if server, err := parseServerFromTable(L, lserver); err == nil {
|
2024-04-07 15:11:10 -05:00
|
|
|
if !found_primary && server.primary {
|
|
|
|
found_primary = true
|
|
|
|
} else if server.primary {
|
|
|
|
panic("Config error : " + server.servername + " : Cannot have multiple primary servers!")
|
|
|
|
}
|
2024-03-21 18:28:19 -05:00
|
|
|
config.servers[server.servername] = &server
|
2024-03-16 17:05:19 -05:00
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-19 18:11:49 -05:00
|
|
|
}
|
2024-03-16 17:05:19 -05:00
|
|
|
return nil
|
|
|
|
}
|