2024-03-16 14:08:19 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-03-16 17:44:01 -05:00
|
|
|
"os"
|
|
|
|
"strings"
|
2024-03-16 14:08:19 -05:00
|
|
|
|
2024-03-16 17:44:01 -05:00
|
|
|
"code.gitea.io/sdk/gitea"
|
2024-03-16 17:05:19 -05:00
|
|
|
"github.com/akamensky/argparse"
|
2024-03-16 14:08:19 -05:00
|
|
|
)
|
|
|
|
|
2024-04-07 13:11:48 -05:00
|
|
|
// Arguments is a place to store arguments / options / flags passed from the command line.
|
|
|
|
type Arguments struct {
|
|
|
|
Times struct {
|
|
|
|
}
|
|
|
|
Summary struct {
|
|
|
|
}
|
|
|
|
Feed struct {
|
|
|
|
}
|
|
|
|
global struct {
|
|
|
|
config_path *string
|
|
|
|
server *string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var arguments Arguments
|
2024-03-16 17:25:18 -05:00
|
|
|
var config Configuration
|
2024-03-19 18:11:49 -05:00
|
|
|
var Servers map[string]*gitea.Client
|
2024-03-16 17:25:18 -05:00
|
|
|
|
2024-03-16 14:08:19 -05:00
|
|
|
func main() {
|
2024-03-16 17:05:19 -05:00
|
|
|
parser := argparse.NewParser("gitivity", "Command line tool to get Gitea statistics")
|
2024-04-07 13:11:48 -05:00
|
|
|
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",
|
|
|
|
})
|
|
|
|
|
2024-03-17 17:59:56 -05:00
|
|
|
Times := parser.NewCommand("times", "Get a user's tracked times.")
|
2024-04-07 13:11:48 -05:00
|
|
|
Times.Flag("t", "today", &argparse.Options{})
|
2024-03-19 18:11:49 -05:00
|
|
|
Summary := parser.NewCommand("summary", "Display a summary of a user's activity.")
|
|
|
|
Feed := parser.NewCommand("feed", "Display the user's activity feed.")
|
2024-03-16 17:05:19 -05:00
|
|
|
|
|
|
|
parse_err := parser.Parse(os.Args)
|
|
|
|
if parse_err != nil {
|
|
|
|
fmt.Print(parser.Usage(parse_err))
|
2024-03-16 17:25:18 -05:00
|
|
|
return
|
2024-03-16 14:08:19 -05:00
|
|
|
}
|
2024-03-16 17:05:19 -05:00
|
|
|
|
2024-03-16 17:25:18 -05:00
|
|
|
config = Configuration{}
|
2024-03-19 18:11:49 -05:00
|
|
|
Servers = make(map[string]*gitea.Client)
|
2024-04-07 13:11:48 -05:00
|
|
|
if err := config.Parse(*arguments.global.config_path); err != nil {
|
2024-03-16 17:25:18 -05:00
|
|
|
panic("Failed to parse configuration file")
|
2024-03-16 14:08:19 -05:00
|
|
|
}
|
2024-03-16 17:05:19 -05:00
|
|
|
|
2024-03-16 17:44:01 -05:00
|
|
|
for _, server := range config.servers {
|
2024-04-07 13:11:48 -05:00
|
|
|
if *arguments.global.server != "" && strings.ToLower(server.servername) != strings.ToLower(*arguments.global.server) {
|
2024-03-16 17:44:01 -05:00
|
|
|
continue
|
|
|
|
}
|
2024-03-16 21:17:40 -05:00
|
|
|
client_opts := []gitea.ClientOption{
|
|
|
|
gitea.SetToken(server.token),
|
|
|
|
}
|
|
|
|
client, err := gitea.NewClient(server.url, client_opts...)
|
2024-03-16 17:44:01 -05:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to create Gitea client! (%s)\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2024-03-19 18:11:49 -05:00
|
|
|
Servers[server.servername] = client
|
2024-03-16 17:44:01 -05:00
|
|
|
}
|
2024-03-19 18:11:49 -05:00
|
|
|
if len(Servers) == 0 {
|
2024-03-16 17:44:01 -05:00
|
|
|
println("No servers configured / specified")
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
2024-03-17 17:59:56 -05:00
|
|
|
|
2024-03-16 21:17:40 -05:00
|
|
|
if Times.Happened() {
|
2024-03-17 17:59:56 -05:00
|
|
|
times()
|
2024-03-19 18:11:49 -05:00
|
|
|
} else if Summary.Happened() {
|
|
|
|
summary()
|
|
|
|
} else if Feed.Happened() {
|
2024-03-19 20:35:37 -05:00
|
|
|
feed()
|
2024-03-17 17:59:56 -05:00
|
|
|
}
|
2024-03-16 14:08:19 -05:00
|
|
|
}
|