From 355794f0b55ac7e6ea81efbefa438fd176ea0de6 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sun, 3 Nov 2024 11:51:50 +0200 Subject: move platform parsing to args --- cmd/gos/main.go | 21 +++------------------ internal/config/args.go | 23 ++++++++++++++++++++++- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/cmd/gos/main.go b/cmd/gos/main.go index 060e39f..55a9312 100644 --- a/cmd/gos/main.go +++ b/cmd/gos/main.go @@ -7,8 +7,6 @@ import ( "log" "os" "path/filepath" - "strconv" - "strings" "time" "codeberg.org/snonux/gos/internal" @@ -48,23 +46,10 @@ func main() { Secrets: secrets, OAuth2Browser: *browser, } - for _, platform := range strings.Split(*platforms, ",") { - // TODO: Move parsing to config package. - // E.g. Mastodon:500 - parts := strings.Split(platform, ":") - var err error - // E.g. args.Platform["mastodon"] = 500 - if len(parts) > 1 { - args.Platforms[parts[0]], err = strconv.Atoi(parts[1]) - if err != nil { - log.Fatalln(err) - } - } else { - log.Println("No message length specified for", platform, "so assuming 500") - args.Platforms[parts[0]] = 500 - } - } + if err := args.ParsePlatforms(*platforms); err != nil { + log.Fatal(err) + } if err := args.Validate(); err != nil { log.Fatal(err) } diff --git a/internal/config/args.go b/internal/config/args.go index b06d2e2..b22ff95 100644 --- a/internal/config/args.go +++ b/internal/config/args.go @@ -2,7 +2,9 @@ package config import ( "fmt" + "log" "slices" + "strconv" "strings" "time" ) @@ -22,7 +24,26 @@ type Args struct { OAuth2Browser string } -func (a Args) Validate() error { +func (a *Args) ParsePlatforms(platforms string) error { + for _, platform := range strings.Split(platforms, ",") { + // E.g. Mastodon:500 + parts := strings.Split(platform, ":") + var err error + // E.g. args.Platform["mastodon"] = 500 + if len(parts) > 1 { + a.Platforms[parts[0]], err = strconv.Atoi(parts[1]) + if err != nil { + return err + } + } else { + log.Println("No message length specified for", platform, "so assuming 500") + a.Platforms[parts[0]] = 500 + } + } + return nil +} + +func (a *Args) Validate() error { for platform := range a.Platforms { if !slices.Contains(validPlatforms, strings.ToLower(platform)) { return fmt.Errorf("Platform %s not supported", platform) -- cgit v1.2.3