Re-structure command / option / flag handling (fix #14)

This commit is contained in:
Noah 2024-04-07 13:11:48 -05:00
parent 2a8f2e46d3
commit e5b3edb460
2 changed files with 29 additions and 5 deletions

32
main.go
View File

@ -9,16 +9,40 @@ import (
"github.com/akamensky/argparse" "github.com/akamensky/argparse"
) )
// 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
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")
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.") Times := parser.NewCommand("times", "Get a user's tracked times.")
Times.Flag("t", "today", &argparse.Options{})
Summary := parser.NewCommand("summary", "Display a summary of a user's activity.") Summary := parser.NewCommand("summary", "Display a summary of a user's activity.")
Feed := parser.NewCommand("feed", "Display the user's activity feed.") Feed := parser.NewCommand("feed", "Display the user's activity feed.")
config_path := parser.String("c", "config", &argparse.Options{Required: false, Help: "Configuration file", Default: "./config.lua"})
server_option := parser.String("s", "server", &argparse.Options{Required: false, Help: "Specific server to use"})
parse_err := parser.Parse(os.Args) parse_err := parser.Parse(os.Args)
if parse_err != nil { if parse_err != nil {
@ -28,12 +52,12 @@ func main() {
config = Configuration{} config = Configuration{}
Servers = make(map[string]*gitea.Client) Servers = make(map[string]*gitea.Client)
if err := config.Parse(*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")
} }
for _, server := range config.servers { for _, server := range config.servers {
if *server_option != "" && strings.ToLower(server.servername) != strings.ToLower(*server_option) { if *arguments.global.server != "" && strings.ToLower(server.servername) != strings.ToLower(*arguments.global.server) {
continue continue
} }
client_opts := []gitea.ClientOption{ client_opts := []gitea.ClientOption{

View File

@ -26,7 +26,7 @@ func times() {
} }
}() }()
since := time.Now().AddDate(0, -1, 0) since := time.Now().AddDate(0, -6, 0)
times := getTimeLogs(since, func(repo gitea.Repository, took time.Duration) { times := getTimeLogs(since, func(repo gitea.Repository, took time.Duration) {
p.Send(IndicatorInfo{ p.Send(IndicatorInfo{
info: fmt.Sprintf("%s / %s", repo.Owner.UserName, repo.Name), info: fmt.Sprintf("%s / %s", repo.Owner.UserName, repo.Name),