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 (
"errors"
"fmt"
"github.com/yuin/gopher-lua"
)
@ -15,9 +16,22 @@ type Server struct {
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.
type Configuration struct {
servers map[string]*Server
servers Servers
}
// 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
}
// 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.
func parseServerFromTable(L *lua.LState, table lua.LValue) (Server, error) {
server := Server{}
@ -47,6 +72,13 @@ func parseServerFromTable(L *lua.LState, table lua.LValue) (Server, error) {
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
}
@ -59,9 +91,15 @@ func (config *Configuration) Parse(fname string) error {
L.DoFile(fname)
table := L.Get(-1)
found_primary := false
for i := 1; i <= L.ObjLen(table); i++ {
lserver := L.GetTable(table, lua.LNumber(i))
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
} else {
return err

13
feed.go
View File

@ -34,7 +34,7 @@ type Activity struct {
func getActivityFeed() []Activity {
feed := []Activity{}
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",
server.url,
server.username,
@ -48,13 +48,13 @@ func getActivityFeed() []Activity {
var data []map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
fmt.Println("Error decoding JSON: "+fmt.Sprint(err))
fmt.Println("Error decoding JSON: " + fmt.Sprint(err))
continue
}
for _, obj := range data {
act_user, _, err := client.GetUserInfo(obj["act_user"].(map[string]interface{})["login"].(string))
if err != nil {
fmt.Println("Error getting user: "+fmt.Sprint(err))
fmt.Println("Error getting user: " + fmt.Sprint(err))
continue
}
var comment gitea.Comment
@ -188,7 +188,7 @@ func (i item) Title() string {
case "commit_repo":
var data map[string]interface{}
if err := json.NewDecoder(strings.NewReader(i.content)).Decode(&data); err != nil {
return "JSON decode error: "+fmt.Sprint(err)
return "JSON decode error: " + fmt.Sprint(err)
}
commits := data["Commits"].([]interface{})
commits_text := fmt.Sprintf("%d commit", len(commits))
@ -210,7 +210,7 @@ func (i item) Description() string {
case "commit_repo":
var data map[string]interface{}
if err := json.NewDecoder(strings.NewReader(i.content)).Decode(&data); err != nil {
return "JSON decode error: "+fmt.Sprint(err)
return "JSON decode error: " + fmt.Sprint(err)
}
s := ""
commits := data["Commits"].([]interface{})
@ -218,7 +218,8 @@ func (i item) Description() string {
s += styles.text.Render(fmt.Sprintf("[%s] ", commit["Sha1"].(string)[0:10])) +
commit["Message"].(string)
return s
default: return ""
default:
return ""
}
}

22
main.go
View File

@ -26,7 +26,7 @@ type Arguments struct {
var arguments Arguments
var config Configuration
var Servers map[string]*gitea.Client
var servers map[string]*gitea.Client
func main() {
parser := argparse.NewParser("gitivity", "Command line tool to get Gitea statistics")
@ -56,13 +56,24 @@ func main() {
}
config = Configuration{}
Servers = make(map[string]*gitea.Client)
servers = make(map[string]*gitea.Client)
if err := config.Parse(*arguments.global.config_path); err != nil {
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 {
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
}
client_opts := []gitea.ClientOption{
@ -73,9 +84,10 @@ func main() {
fmt.Printf("Failed to create Gitea client! (%s)\n", err)
os.Exit(1)
}
Servers[server.servername] = client
servers[server.servername] = client
}
if len(Servers) == 0 {
if len(servers) == 0 {
println("No servers configured / specified")
os.Exit(0)
}

View File

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