From 3f88f3aac52e5a2202031402dfff1de7949354a3 Mon Sep 17 00:00:00 2001 From: Noah Date: Sat, 16 Mar 2024 17:05:19 -0500 Subject: [PATCH] Lua configuration! --- .gitignore | 1 + config.go | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ config.lua | 14 +++++++++++ go.mod | 3 +++ go.sum | 11 +++++++++ main.go | 46 ++++++++++++++++++++++++++---------- 6 files changed, 132 insertions(+), 12 deletions(-) create mode 100644 .gitignore create mode 100644 config.go create mode 100644 config.lua diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..69459c1 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +gitivity diff --git a/config.go b/config.go new file mode 100644 index 0000000..d95d6a5 --- /dev/null +++ b/config.go @@ -0,0 +1,69 @@ +package main + +import ( + "errors" + + "github.com/yuin/gopher-lua" +) + +// Server holds information about a Gitea server. +type Server struct { + servername string + url string + username string + token string +} + +// Configuration holds data retrieved from a Lua script. +type Configuration struct { + servers []Server +} + +// parseText parses a text value from the configuration file. +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 +} + +// 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 + } + + return server, nil +} + +// Configuration.Parse parses a Lua configuration file into a Configuration struct. +func (config *Configuration) Parse(fname string) error { + L := lua.NewState() + defer L.Close() + + L.DoFile(fname) + table := L.Get(-1) + + for i := 1; i <= L.ObjLen(table); i++ { + lserver := L.GetTable(table, lua.LNumber(i)) + if server, err := parseServerFromTable(L, lserver); err == nil { + config.servers = append(config.servers, server) + } else { + return err + } + } + return nil +} diff --git a/config.lua b/config.lua new file mode 100644 index 0000000..94154f3 --- /dev/null +++ b/config.lua @@ -0,0 +1,14 @@ +return { + { + servername = "RetroEdgeTech", + url = "https://code.retroedge.tech", + username = "Noah", + token = "token", + }, + { + servername = "Codeberg", + url = "https://codeberg.org", + username = "noah-matthew", + token = "token", + } +} diff --git a/go.mod b/go.mod index e6ccbd1..02536e7 100644 --- a/go.mod +++ b/go.mod @@ -4,9 +4,12 @@ go 1.19 require ( code.gitea.io/sdk/gitea v0.17.1 // indirect + github.com/akamensky/argparse v1.4.0 // indirect github.com/davidmz/go-pageant v1.0.2 // indirect github.com/go-fed/httpsig v1.1.0 // indirect github.com/hashicorp/go-version v1.6.0 // indirect + github.com/yuin/gopher-lua v1.1.1 // indirect golang.org/x/crypto v0.17.0 // indirect golang.org/x/sys v0.15.0 // indirect + layeh.com/gopher-luar v1.0.11 // indirect ) diff --git a/go.sum b/go.sum index 8f62e9a..a2ebd7e 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,10 @@ code.gitea.io/sdk/gitea v0.17.1 h1:3jCPOG2ojbl8AcfaUCRYLT5MUcBMFwS0OSK2mA5Zok8= code.gitea.io/sdk/gitea v0.17.1/go.mod h1:aCnBqhHpoEWA180gMbaCtdX9Pl6BWBAuuP2miadoTNM= +github.com/akamensky/argparse v1.4.0 h1:YGzvsTqCvbEZhL8zZu2AiA5nq805NZh75JNj4ajn1xc= +github.com/akamensky/argparse v1.4.0/go.mod h1:S5kwC7IuDcEr5VeXtGPRVZ5o/FdhcMlQz4IZQuw64xA= +github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= +github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= +github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davidmz/go-pageant v1.0.2 h1:bPblRCh5jGU+Uptpz6LgMZGD5hJoOt7otgT454WvHn0= github.com/davidmz/go-pageant v1.0.2/go.mod h1:P2EDDnMqIwG5Rrp05dTRITj9z2zpGcD9efWSkTNKLIE= @@ -11,6 +16,9 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +github.com/yuin/gopher-lua v0.0.0-20190206043414-8bfc7677f583/go.mod h1:gqRgreBUhTSL0GeU64rtZ3Uq3wtjOa/TB2YfrtkCbVQ= +github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M= +github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8= @@ -28,6 +36,7 @@ golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190204203706-41f3e6584952/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -56,3 +65,5 @@ golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +layeh.com/gopher-luar v1.0.11 h1:8zJudpKI6HWkoh9eyyNFaTM79PY6CAPcIr6X/KTiliw= +layeh.com/gopher-luar v1.0.11/go.mod h1:TPnIVCZ2RJBndm7ohXyaqfhzjlZ+OA2SZR/YwL8tECk= diff --git a/main.go b/main.go index 615d8ca..63aa09f 100644 --- a/main.go +++ b/main.go @@ -4,18 +4,40 @@ import ( "os" "fmt" - "code.gitea.io/sdk/gitea" + //"code.gitea.io/sdk/gitea" + "github.com/akamensky/argparse" ) -func main() { - client, err := gitea.NewClient("https://code.retroedge.tech") - if err != nil { - fmt.Printf("Failed to create Gitea client! (%s)\n", err) - os.Exit(1) - } - server_version, _, err := client.ServerVersion() - if err != nil { - fmt.Printf("Failed to get server version! (%s)\n", err) - } - fmt.Printf("Server version: %s\n", server_version) +func summary() { + fmt.Println("Summary") +} + +func main() { + parser := argparse.NewParser("gitivity", "Command line tool to get Gitea statistics") + + Summary := parser.NewCommand("summary", "Generate a summary of a user's activity.") + + parse_err := parser.Parse(os.Args) + if parse_err != nil { + fmt.Print(parser.Usage(parse_err)) + } + + config := Configuration{} + if err := config.Parse("config.lua"); err != nil { + panic("Failed to parse configuration file: "+fmt.Sprint(err)) + } + fmt.Printf("config.servers: %v\n", config.servers) + + //client, err := gitea.NewClient(*server_name) + //if err != nil { + // fmt.Printf("Failed to create Gitea client! (%s)\n", err) + // os.Exit(1) + //} + //server_version, _, err := client.ServerVersion() + //if err != nil { + // fmt.Printf("Failed to get server version! (%s)\n", err) + //} + if Summary.Happened() { + summary() + } }