summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-11-03 11:51:50 +0200
committerPaul Buetow <paul@buetow.org>2024-11-03 11:51:50 +0200
commit355794f0b55ac7e6ea81efbefa438fd176ea0de6 (patch)
treef4c8c04be14cc83477603901bbbc1bbcbf5cd605
parentacb0d5e00c0b19e149eef429881b998414555a5d (diff)
move platform parsing to args
-rw-r--r--cmd/gos/main.go21
-rw-r--r--internal/config/args.go23
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)