103 lines
2.7 KiB
Go
103 lines
2.7 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
"github.com/akamensky/argparse"
|
|
)
|
|
|
|
// Arguments is a place to store arguments / options / flags passed from the command line.
|
|
type Arguments struct {
|
|
Times struct {
|
|
since *string
|
|
}
|
|
Summary struct {
|
|
}
|
|
Feed struct {
|
|
}
|
|
global struct {
|
|
config_path *string
|
|
server *string
|
|
}
|
|
}
|
|
|
|
var arguments Arguments
|
|
var config Configuration
|
|
var servers map[string]*gitea.Client
|
|
|
|
func main() {
|
|
parser := argparse.NewParser("gitivity", "Command line tool to get Gitea statistics")
|
|
arguments.global.config_path = parser.String("c", "config", &argparse.Options{
|
|
Required: false,
|
|
Help: "Configuration file",
|
|
Default: "./config.lua",
|
|
})
|
|
arguments.global.server = parser.String("s", "server", &argparse.Options{
|
|
Required: false,
|
|
Help: "Specific server to use",
|
|
})
|
|
|
|
Times := parser.NewCommand("times", "Get a user's tracked times.")
|
|
arguments.Times.since = Times.String("d", "since", &argparse.Options{
|
|
Required: false,
|
|
Help: "Get all time logs since this time. Format: 3d / 1w / 2mo",
|
|
Default: "1w",
|
|
})
|
|
Summary := parser.NewCommand("summary", "Display a summary of a user's activity.")
|
|
Feed := parser.NewCommand("feed", "Display the user's activity feed.")
|
|
|
|
parse_err := parser.Parse(os.Args)
|
|
if parse_err != nil {
|
|
fmt.Print(parser.Usage(parse_err))
|
|
return
|
|
}
|
|
|
|
config = Configuration{}
|
|
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 specific_server != "" && strings.ToLower(server.servername) != strings.ToLower(specific_server) {
|
|
continue
|
|
}
|
|
client_opts := []gitea.ClientOption{
|
|
gitea.SetToken(server.token),
|
|
}
|
|
client, err := gitea.NewClient(server.url, client_opts...)
|
|
if err != nil {
|
|
fmt.Printf("Failed to create Gitea client! (%s)\n", err)
|
|
os.Exit(1)
|
|
}
|
|
servers[server.servername] = client
|
|
}
|
|
|
|
if len(servers) == 0 {
|
|
println("No servers configured / specified")
|
|
os.Exit(0)
|
|
}
|
|
|
|
if Times.Happened() {
|
|
times()
|
|
} else if Summary.Happened() {
|
|
summary()
|
|
} else if Feed.Happened() {
|
|
feed()
|
|
}
|
|
}
|