-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.go
123 lines (109 loc) · 2.73 KB
/
setup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"errors"
"fmt"
"strings"
"github.com/AlecAivazis/survey/v2"
"github.com/limero/offlinerss/models"
)
func setup() (models.Config, error) {
answers := struct {
Server string
Username string
Password string
Hostname string
Clients []string
}{}
survey.ErrorTemplate = `{{color .Icon.Format }}{{ .Icon.Text }} Error: {{ .Error.Error }}{{color "reset"}}
`
qs1 := []*survey.Question{
{
Name: "server",
Prompt: &survey.Select{
Message: "Pick server:",
Options: []string{
"NewsBlur",
"Miniflux",
},
VimMode: true,
},
},
}
if err := survey.Ask(qs1, &answers); err != nil {
return models.Config{}, err
}
qs2 := []*survey.Question{
{
Name: "username",
Prompt: &survey.Input{Message: fmt.Sprintf("Enter username for %s:", answers.Server)},
Validate: func(val interface{}) error {
if str, ok := val.(string); !ok || len(str) == 0 {
return errors.New("invalid username")
}
return nil
},
},
{
Name: "password",
Prompt: &survey.Password{Message: fmt.Sprintf("Enter password for %s:", answers.Server)},
Validate: func(val interface{}) error {
if str, ok := val.(string); !ok || len(str) == 0 {
return errors.New("invalid password")
}
return nil
},
},
{
Name: "hostname",
Prompt: &survey.Input{Message: fmt.Sprintf("(Optional) Enter custom hostname for connecting to %s:", answers.Server)},
},
}
if err := survey.Ask(qs2, &answers); err != nil {
return models.Config{}, err
}
serverConfig := models.ServerConfig{
Name: models.ServerName(strings.ToLower(answers.Server)),
Username: answers.Username,
Password: answers.Password,
Hostname: answers.Hostname,
}
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{
{
Name: "clients",
Prompt: &survey.MultiSelect{
Message: "Select clients:",
Options: []string{
"FeedReader",
"Newsboat",
"QuiteRSS",
},
VimMode: true,
},
Validate: func(val interface{}) error {
if opts, ok := val.([]survey.OptionAnswer); !ok || len(opts) == 0 {
return errors.New("you need to pick at least one client")
}
return nil
},
},
}
if err := survey.Ask(qs3, &answers); err != nil {
return models.Config{}, err
}
clientConfigs := make([]models.ClientConfig, len(answers.Clients))
for i, c := range answers.Clients {
clientConfigs[i] = models.ClientConfig{
Name: models.ClientName(strings.ToLower(c)),
}
}
return models.Config{
Server: serverConfig,
Clients: clientConfigs,
}, nil
}