feat(config): Allow a server to be set as the "default" server (fix #17)
This commit is contained in:
parent
be2c17662d
commit
15ba984d76
40
config.go
40
config.go
@ -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
|
||||||
|
53
feed.go
53
feed.go
@ -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,
|
||||||
@ -48,41 +48,41 @@ func getActivityFeed() []Activity {
|
|||||||
var data []map[string]interface{}
|
var data []map[string]interface{}
|
||||||
err = json.NewDecoder(resp.Body).Decode(&data)
|
err = json.NewDecoder(resp.Body).Decode(&data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error decoding JSON: "+fmt.Sprint(err))
|
fmt.Println("Error decoding JSON: " + fmt.Sprint(err))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for _, obj := range data {
|
for _, obj := range data {
|
||||||
act_user, _, err := client.GetUserInfo(obj["act_user"].(map[string]interface{})["login"].(string))
|
act_user, _, err := client.GetUserInfo(obj["act_user"].(map[string]interface{})["login"].(string))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error getting user: "+fmt.Sprint(err))
|
fmt.Println("Error getting user: " + fmt.Sprint(err))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
var comment gitea.Comment
|
var comment gitea.Comment
|
||||||
if obj["comment"] != nil {
|
if obj["comment"] != nil {
|
||||||
raw_comment := obj["comment"].(map[string]interface{})
|
raw_comment := obj["comment"].(map[string]interface{})
|
||||||
comment = gitea.Comment{
|
comment = gitea.Comment{
|
||||||
Body: raw_comment["body"].(string),
|
Body: raw_comment["body"].(string),
|
||||||
HTMLURL: raw_comment["html_url"].(string),
|
HTMLURL: raw_comment["html_url"].(string),
|
||||||
ID: int64(raw_comment["id"].(float64)),
|
ID: int64(raw_comment["id"].(float64)),
|
||||||
IssueURL: raw_comment["issue_url"].(string),
|
IssueURL: raw_comment["issue_url"].(string),
|
||||||
OriginalAuthor: raw_comment["original_author"].(string),
|
OriginalAuthor: raw_comment["original_author"].(string),
|
||||||
OriginalAuthorID: int64(raw_comment["original_author_id"].(float64)),
|
OriginalAuthorID: int64(raw_comment["original_author_id"].(float64)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
feed = append(feed, Activity{
|
feed = append(feed, Activity{
|
||||||
// repo: repo,
|
// repo: repo,
|
||||||
act_user: act_user,
|
act_user: act_user,
|
||||||
act_user_id: int64(obj["act_user_id"].(float64)),
|
act_user_id: int64(obj["act_user_id"].(float64)),
|
||||||
comment: comment,
|
comment: comment,
|
||||||
comment_id: int64(obj["comment_id"].(float64)),
|
comment_id: int64(obj["comment_id"].(float64)),
|
||||||
content: obj["content"].(string),
|
content: obj["content"].(string),
|
||||||
created: obj["created"].(string),
|
created: obj["created"].(string),
|
||||||
id: int64(obj["id"].(float64)),
|
id: int64(obj["id"].(float64)),
|
||||||
is_private: obj["is_private"].(bool),
|
is_private: obj["is_private"].(bool),
|
||||||
op_type: obj["op_type"].(string),
|
op_type: obj["op_type"].(string),
|
||||||
ref_name: obj["ref_name"].(string),
|
ref_name: obj["ref_name"].(string),
|
||||||
repo_id: int64(obj["repo_id"].(float64)),
|
repo_id: int64(obj["repo_id"].(float64)),
|
||||||
user_id: int64(obj["user_id"].(float64)),
|
user_id: int64(obj["user_id"].(float64)),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -123,10 +123,10 @@ func newListKeyMap() *listKeyMap {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type feedviewer struct {
|
type feedviewer struct {
|
||||||
list list.Model
|
list list.Model
|
||||||
feed []Activity
|
feed []Activity
|
||||||
appStyle lipgloss.Style
|
appStyle lipgloss.Style
|
||||||
keys listKeyMap
|
keys listKeyMap
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m feedviewer) Init() tea.Cmd {
|
func (m feedviewer) Init() tea.Cmd {
|
||||||
@ -188,7 +188,7 @@ func (i item) Title() string {
|
|||||||
case "commit_repo":
|
case "commit_repo":
|
||||||
var data map[string]interface{}
|
var data map[string]interface{}
|
||||||
if err := json.NewDecoder(strings.NewReader(i.content)).Decode(&data); err != nil {
|
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 := data["Commits"].([]interface{})
|
||||||
commits_text := fmt.Sprintf("%d commit", len(commits))
|
commits_text := fmt.Sprintf("%d commit", len(commits))
|
||||||
@ -210,15 +210,16 @@ func (i item) Description() string {
|
|||||||
case "commit_repo":
|
case "commit_repo":
|
||||||
var data map[string]interface{}
|
var data map[string]interface{}
|
||||||
if err := json.NewDecoder(strings.NewReader(i.content)).Decode(&data); err != nil {
|
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 := ""
|
s := ""
|
||||||
commits := data["Commits"].([]interface{})
|
commits := data["Commits"].([]interface{})
|
||||||
commit := commits[0].(map[string]interface{})
|
commit := commits[0].(map[string]interface{})
|
||||||
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
22
main.go
@ -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)
|
||||||
}
|
}
|
||||||
|
2
times.go
2
times.go
@ -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 {
|
||||||
|
Loading…
Reference in New Issue
Block a user