From 0f40059c2a11af2b98f4ee45ff0896f75a07a64f Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Wed, 6 Dec 2023 20:15:36 +0800 Subject: [PATCH] Validate server login in setup --- client.go | 18 ++++++++++++++++++ offlinerss.go | 28 +++------------------------- server.go | 12 ++++++++++++ setup.go | 25 ++++++++++++++++--------- 4 files changed, 49 insertions(+), 34 deletions(-) diff --git a/client.go b/client.go index e64794f..159707e 100644 --- a/client.go +++ b/client.go @@ -4,10 +4,28 @@ import ( "errors" "os" + "github.com/limero/offlinerss/client/feedreader" + "github.com/limero/offlinerss/client/newsboat" + "github.com/limero/offlinerss/client/quiterss" "github.com/limero/offlinerss/log" "github.com/limero/offlinerss/models" ) +func getClients(clientConfigs []models.ClientConfig) models.Clients { + var clients models.Clients + for _, clientConfig := range clientConfigs { + switch clientConfig.Name { + case models.ClientFeedReader: + clients = append(clients, feedreader.New(clientConfig)) + case models.ClientNewsboat: + clients = append(clients, newsboat.New(clientConfig)) + case models.ClientQuiteRSS: + clients = append(clients, quiterss.New(clientConfig)) + } + } + return clients +} + func GetSyncToActions(clients models.Clients) (models.SyncToActions, error) { if len(clients) == 0 { return nil, errors.New("You have to enable at least one client in the config file") diff --git a/offlinerss.go b/offlinerss.go index 57e881f..8dc973d 100644 --- a/offlinerss.go +++ b/offlinerss.go @@ -3,13 +3,7 @@ package main import ( "fmt" - "github.com/limero/offlinerss/client/feedreader" - "github.com/limero/offlinerss/client/newsboat" - "github.com/limero/offlinerss/client/quiterss" "github.com/limero/offlinerss/log" - "github.com/limero/offlinerss/models" - "github.com/limero/offlinerss/server/miniflux" - "github.com/limero/offlinerss/server/newsblur" ) func run() error { @@ -18,32 +12,16 @@ func run() error { return err } - var clients models.Clients - for _, clientConfig := range config.Clients { - switch clientConfig.Name { - case models.ClientFeedReader: - clients = append(clients, feedreader.New(clientConfig)) - case models.ClientNewsboat: - clients = append(clients, newsboat.New(clientConfig)) - case models.ClientQuiteRSS: - clients = append(clients, quiterss.New(clientConfig)) - } - } + clients := getClients(config.Clients) syncToActions, err := GetSyncToActions(clients) if err != nil { return err } - var s models.Server - switch config.Server.Name { - case models.ServerMiniflux: - s = miniflux.New(config.Server) - case models.ServerNewsBlur: - s = newsblur.New(config.Server) - } + server := getServer(config.Server) - folders, err := SyncServer(s, syncToActions) + folders, err := SyncServer(server, syncToActions) if err != nil { return err } diff --git a/server.go b/server.go index 7f599ce..445e947 100644 --- a/server.go +++ b/server.go @@ -3,8 +3,20 @@ package main import ( "github.com/limero/offlinerss/log" "github.com/limero/offlinerss/models" + "github.com/limero/offlinerss/server/miniflux" + "github.com/limero/offlinerss/server/newsblur" ) +func getServer(serverConfig models.ServerConfig) models.Server { + switch serverConfig.Name { + case models.ServerMiniflux: + return miniflux.New(serverConfig) + case models.ServerNewsBlur: + return newsblur.New(serverConfig) + } + return nil +} + func SyncServer(server models.Server, syncToActions models.SyncToActions) (models.Folders, error) { // Sync changes back to server and get new stories diff --git a/setup.go b/setup.go index 9fb1583..77856bf 100644 --- a/setup.go +++ b/setup.go @@ -64,7 +64,18 @@ func setup() (models.Config, error) { return models.Config{}, err } - // TODO: Validate login + serverConfig := models.ServerConfig{ + Name: models.ServerName(strings.ToLower(answers.Server)), + Username: answers.Username, + Password: answers.Password, + } + + server := getServer(serverConfig) + fmt.Printf("Attempting to login to %s as user %q\n", serverConfig.Name, serverConfig.Username) + if err := server.Login(); err != nil { + return models.Config{}, err + } + fmt.Printf("Successfully logged in!\n\n") qs3 := []*survey.Question{ { @@ -91,19 +102,15 @@ func setup() (models.Config, error) { return models.Config{}, err } - clients := make([]models.ClientConfig, len(answers.Clients)) + clientConfigs := make([]models.ClientConfig, len(answers.Clients)) for i, c := range answers.Clients { - clients[i] = models.ClientConfig{ + clientConfigs[i] = models.ClientConfig{ Name: models.ClientName(strings.ToLower(c)), } } return models.Config{ - Server: models.ServerConfig{ - Name: models.ServerName(strings.ToLower(answers.Server)), - Username: answers.Username, - Password: answers.Password, - }, - Clients: clients, + Server: serverConfig, + Clients: clientConfigs, }, nil }