feat(config): Allow a server to be set as the "default" server (fix #17)

This commit is contained in:
Noah 2024-04-07 15:11:10 -05:00
parent be2c17662d
commit 15ba984d76
4 changed files with 84 additions and 33 deletions

View File

@ -2,6 +2,7 @@ package main
import ( import (
"errors" "errors"
"fmt"
"github.com/yuin/gopher-lua" "github.com/yuin/gopher-lua"
) )
@ -15,9 +16,22 @@ type Server struct {
primary bool primary bool
} }
// 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.")
}
// Configuration holds data retrieved from a Lua script. // Configuration holds data retrieved from a Lua script.
type Configuration struct { type Configuration struct {
servers map[string]*Server servers Servers
} }
// getStringFromTable parses a text value from the configuration file. // getStringFromTable parses a text value from the configuration file.
@ -31,6 +45,17 @@ func getStringFromTable(L *lua.LState, lobj lua.LValue, key string, out *string)
return nil return nil
} }
// 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
}
// parseServerFromTable parses a Lua table into a Server structure. // parseServerFromTable parses a Lua table into a Server structure.
func parseServerFromTable(L *lua.LState, table lua.LValue) (Server, error) { func parseServerFromTable(L *lua.LState, table lua.LValue) (Server, error) {
server := Server{} server := Server{}
@ -47,6 +72,13 @@ func parseServerFromTable(L *lua.LState, table lua.LValue) (Server, error) {
return server, err return server, err
} }
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")
}
return server, nil return server, nil
} }
@ -59,9 +91,15 @@ func (config *Configuration) Parse(fname string) error {
L.DoFile(fname) L.DoFile(fname)
table := L.Get(-1) table := L.Get(-1)
found_primary := false
for i := 1; i <= L.ObjLen(table); i++ { for i := 1; i <= L.ObjLen(table); i++ {
lserver := L.GetTable(table, lua.LNumber(i)) lserver := L.GetTable(table, lua.LNumber(i))
if server, err := parseServerFromTable(L, lserver); err == nil { if server, err := parseServerFromTable(L, lserver); err == nil {
if !found_primary && server.primary {
found_primary = true
} else if server.primary {
panic("Config error : " + server.servername + " : Cannot have multiple primary servers!")
}
config.servers[server.servername] = &server config.servers[server.servername] = &server
} else { } else {
return err return err

View File

@ -34,7 +34,7 @@ type Activity struct {
func getActivityFeed() []Activity { func getActivityFeed() []Activity {
feed := []Activity{} feed := []Activity{}
for _, server := range config.servers { for _, server := range config.servers {
client := Servers[server.servername] client := servers[server.servername]
resp, err := http.Get(fmt.Sprintf("%s/api/v1/users/%s/activities/feeds?limit=15&page=1", resp, err := http.Get(fmt.Sprintf("%s/api/v1/users/%s/activities/feeds?limit=15&page=1",
server.url, server.url,
server.username, server.username,
@ -218,7 +218,8 @@ func (i item) Description() string {
s += styles.text.Render(fmt.Sprintf("[%s] ", commit["Sha1"].(string)[0:10])) + s += styles.text.Render(fmt.Sprintf("[%s] ", commit["Sha1"].(string)[0:10])) +
commit["Message"].(string) commit["Message"].(string)
return s return s
default: return "" default:
return ""
} }
} }

22
main.go
View File

@ -26,7 +26,7 @@ type Arguments struct {
var arguments Arguments var arguments Arguments
var config Configuration var config Configuration
var Servers map[string]*gitea.Client var servers map[string]*gitea.Client
func main() { func main() {
parser := argparse.NewParser("gitivity", "Command line tool to get Gitea statistics") parser := argparse.NewParser("gitivity", "Command line tool to get Gitea statistics")
@ -56,13 +56,24 @@ func main() {
} }
config = Configuration{} config = Configuration{}
Servers = make(map[string]*gitea.Client) servers = make(map[string]*gitea.Client)
if err := config.Parse(*arguments.global.config_path); err != nil { if err := config.Parse(*arguments.global.config_path); err != nil {
panic("Failed to parse configuration file") panic("Failed to parse configuration file")
} }
// Specific server is used to ONLY use one of the available servers configured.
specific_server := ""
if *arguments.global.server != "" { // If a server was passed from CLI,
specific_server = *arguments.global.server // use that server.
} else { // Otherwise,
server, err := config.servers.getPrimary() // (Attempt to) get the primary server.
if err == nil { // If no error occured, we got the primary server.
specific_server = server.servername // Use the primary server
}
}
for _, server := range config.servers { for _, server := range config.servers {
if *arguments.global.server != "" && strings.ToLower(server.servername) != strings.ToLower(*arguments.global.server) { if specific_server != "" && strings.ToLower(server.servername) != strings.ToLower(specific_server) {
continue continue
} }
client_opts := []gitea.ClientOption{ client_opts := []gitea.ClientOption{
@ -73,9 +84,10 @@ func main() {
fmt.Printf("Failed to create Gitea client! (%s)\n", err) fmt.Printf("Failed to create Gitea client! (%s)\n", err)
os.Exit(1) os.Exit(1)
} }
Servers[server.servername] = client servers[server.servername] = client
} }
if len(Servers) == 0 {
if len(servers) == 0 {
println("No servers configured / specified") println("No servers configured / specified")
os.Exit(0) os.Exit(0)
} }

View File

@ -114,7 +114,7 @@ type TrackedTime struct {
// getTimeLogs gets every single time log possible. // getTimeLogs gets every single time log possible.
func getTimeLogs(since time.Time, on_process_repo func(repo gitea.Repository, took time.Duration)) []TrackedTime { func getTimeLogs(since time.Time, on_process_repo func(repo gitea.Repository, took time.Duration)) []TrackedTime {
var times []TrackedTime var times []TrackedTime
for server_name, client := range Servers { for server_name, client := range servers {
page := 1 page := 1
user, _, err := client.GetMyUserInfo() user, _, err := client.GetMyUserInfo()
if err != nil { if err != nil {