aboutsummaryrefslogtreecommitdiff
path: root/src/page_config.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/page_config.go')
-rw-r--r--src/page_config.go57
1 files changed, 40 insertions, 17 deletions
diff --git a/src/page_config.go b/src/page_config.go
index bb8afca..090a70b 100644
--- a/src/page_config.go
+++ b/src/page_config.go
@@ -1,6 +1,7 @@
1package src 1package src
2 2
3import ( 3import (
4 "fmt"
4 "net/http" 5 "net/http"
5 6
6 "github.com/delucks/go-subsonic" 7 "github.com/delucks/go-subsonic"
@@ -8,20 +9,14 @@ import (
8) 9)
9 10
10func configPage(a *app) *tview.Form { 11func configPage(a *app) *tview.Form {
12 var err error
13
11 form := tview.NewForm(). 14 form := tview.NewForm().
12 AddInputField("Server URL", a.cfg.BaseURL, 40, nil, func(txt string) { a.cfg.BaseURL = txt }). 15 AddInputField("Server URL", a.cfg.BaseURL, 40, nil, func(txt string) { a.cfg.BaseURL = txt }).
13 AddInputField("Username", a.cfg.Username, 20, nil, func(txt string) { a.cfg.Username = txt }). 16 AddInputField("Username", a.cfg.Username, 20, nil, func(txt string) { a.cfg.Username = txt }).
14 AddPasswordField("Password", a.cfg.Password, 20, '*', func(txt string) { a.cfg.Password = txt }). 17 AddPasswordField("Password", a.cfg.Password, 20, '*', func(txt string) { a.cfg.Password = txt }).
15 AddButton("Test", func() { 18 AddButton("Test", func() {
16 tmpSub := &subsonic.Client{ 19 if err = testConfig(a.cfg); err != nil {
17 Client: http.DefaultClient,
18 BaseUrl: a.cfg.BaseURL,
19 User: a.cfg.Username,
20 ClientName: "termsonic",
21 PasswordAuth: true,
22 }
23
24 if err := tmpSub.Authenticate(a.cfg.Password); err != nil {
25 alert(a, "Could not auth: %v", err) 20 alert(a, "Could not auth: %v", err)
26 } else { 21 } else {
27 alert(a, "Success.") 22 alert(a, "Success.")
@@ -34,14 +29,8 @@ func configPage(a *app) *tview.Form {
34 return 29 return
35 } 30 }
36 31
37 a.sub = &subsonic.Client{ 32 a.sub, err = buildSubsonicClient(a.cfg)
38 Client: http.DefaultClient, 33 if err != nil {
39 BaseUrl: a.cfg.BaseURL,
40 User: a.cfg.Username,
41 ClientName: "termsonic",
42 PasswordAuth: true,
43 }
44 if err := a.sub.Authenticate(a.cfg.Password); err != nil {
45 alert(a, "Could not auth: %v", err) 34 alert(a, "Could not auth: %v", err)
46 } else { 35 } else {
47 alert(a, "All good!") 36 alert(a, "All good!")
@@ -49,3 +38,37 @@ func configPage(a *app) *tview.Form {
49 }) 38 })
50 return form 39 return form
51} 40}
41
42func testConfig(cfg *Config) error {
43 if cfg.BaseURL == "" {
44 return fmt.Errorf("empty base URL")
45 }
46
47 if cfg.Username == "" {
48 return fmt.Errorf("empty username")
49 }
50
51 if cfg.Password == "" {
52 return fmt.Errorf("empty password")
53 }
54
55 _, err := buildSubsonicClient(cfg)
56 return err
57}
58
59func buildSubsonicClient(cfg *Config) (*subsonic.Client, error) {
60 tmpSub := &subsonic.Client{
61 Client: http.DefaultClient,
62 BaseUrl: cfg.BaseURL,
63 User: cfg.Username,
64 ClientName: "termsonic",
65 PasswordAuth: true,
66 }
67
68 err := tmpSub.Authenticate(cfg.Password)
69 if err != nil {
70 return nil, err
71 }
72
73 return tmpSub, nil
74}