Skip to content

Commit

Permalink
Validate server login in setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Limero committed Dec 6, 2023
1 parent 7c6d0f6 commit 0f40059
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 34 deletions.
18 changes: 18 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
28 changes: 3 additions & 25 deletions offlinerss.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
12 changes: 12 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
25 changes: 16 additions & 9 deletions setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand All @@ -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
}

0 comments on commit 0f40059

Please sign in to comment.