author | Mikael Berthe <mikael@lilotux.net> |
Mon, 19 Mar 2018 11:29:06 +0100 | |
changeset 179 | a60295c41321 |
parent 158 | d5c2f8528af1 |
child 185 | 564d92b54b00 |
permissions | -rw-r--r-- |
0 | 1 |
// Copyright © 2017 Mikael Berthe <mikael@lilotux.net> |
2 |
// |
|
3 |
// Licensed under the MIT license. |
|
4 |
// Please see the LICENSE file is this directory. |
|
5 |
||
6 |
package cmd |
|
7 |
||
8 |
import ( |
|
9 |
"strconv" |
|
10 |
"strings" |
|
11 |
||
12 |
"github.com/McKael/madon" |
|
45
b58a7ea1aeb2
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
44
diff
changeset
|
13 |
"github.com/pkg/errors" |
0 | 14 |
"github.com/spf13/viper" |
15 |
) |
|
16 |
||
17 |
var scopes = []string{"read", "write", "follow"} |
|
18 |
||
19 |
func madonInit(signIn bool) error { |
|
20 |
if gClient == nil { |
|
21 |
if err := madonInitClient(); err != nil { |
|
22 |
return err |
|
23 |
} |
|
24 |
} |
|
25 |
if signIn { |
|
26 |
return madonLogin() |
|
27 |
} |
|
28 |
return nil |
|
29 |
} |
|
30 |
||
31 |
func madonInitClient() error { |
|
32 |
if gClient != nil { |
|
33 |
return nil |
|
34 |
} |
|
35 |
var err error |
|
36 |
||
37 |
// Overwrite variables using Viper |
|
38 |
instanceURL = viper.GetString("instance") |
|
39 |
appID = viper.GetString("app_id") |
|
40 |
appSecret = viper.GetString("app_secret") |
|
41 |
||
42 |
if instanceURL == "" { |
|
43 |
return errors.New("no instance provided") |
|
44 |
} |
|
45 |
||
46
13717ea83333
Improve init logging/error message
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
46 |
if verbose { |
13717ea83333
Improve init logging/error message
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
47 |
errPrint("Instance: '%s'", instanceURL) |
13717ea83333
Improve init logging/error message
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
48 |
} |
13717ea83333
Improve init logging/error message
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
49 |
|
0 | 50 |
if appID != "" && appSecret != "" { |
51 |
// We already have an app key/secret pair |
|
52 |
gClient, err = madon.RestoreApp(AppName, instanceURL, appID, appSecret, nil) |
|
53 |
if err != nil { |
|
54 |
return err |
|
55 |
} |
|
56 |
// Check instance |
|
57 |
if _, err := gClient.GetCurrentInstance(); err != nil { |
|
53
4088535c4c4c
Be more verbose when connection fails
Mikael Berthe <mikael@lilotux.net>
parents:
46
diff
changeset
|
58 |
return errors.Wrap(err, "could not connect to server with provided app ID/secret") |
0 | 59 |
} |
60 |
if verbose { |
|
46
13717ea83333
Improve init logging/error message
Mikael Berthe <mikael@lilotux.net>
parents:
45
diff
changeset
|
61 |
errPrint("Using provided app ID/secret") |
0 | 62 |
} |
63 |
return nil |
|
64 |
} |
|
65 |
||
66 |
if appID != "" || appSecret != "" { |
|
67 |
errPrint("Warning: provided app id/secrets incomplete -- registering again") |
|
68 |
} |
|
69 |
||
55
e9df533a1c4f
Sync w/ madon 1.4.0 -- add website parameter to application registration
Mikael Berthe <mikael@lilotux.net>
parents:
53
diff
changeset
|
70 |
gClient, err = madon.NewApp(AppName, AppWebsite, scopes, madon.NoRedirect, instanceURL) |
0 | 71 |
if err != nil { |
45
b58a7ea1aeb2
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
44
diff
changeset
|
72 |
return errors.Wrap(err, "app registration failed") |
0 | 73 |
} |
74 |
||
158 | 75 |
errPrint("Registered new application.") |
0 | 76 |
return nil |
77 |
} |
|
78 |
||
79 |
func madonLogin() error { |
|
80 |
if gClient == nil { |
|
158 | 81 |
return errors.New("application not registered") |
0 | 82 |
} |
83 |
||
84 |
token = viper.GetString("token") |
|
85 |
login = viper.GetString("login") |
|
86 |
password = viper.GetString("password") |
|
87 |
||
88 |
if token != "" { // TODO check token validity? |
|
89 |
if verbose { |
|
90 |
errPrint("Reusing existing token.") |
|
91 |
} |
|
92 |
gClient.SetUserToken(token, login, password, []string{}) |
|
93 |
return nil |
|
94 |
} |
|
95 |
||
96 |
err := gClient.LoginBasic(login, password, scopes) |
|
97 |
if err == nil { |
|
98 |
return nil |
|
99 |
} |
|
11
3884f5276808
Work around server sending HTML error page on failed login
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
100 |
if !verbose && err.Error() == "cannot unmarshal server response: invalid character '<' looking for beginning of value" { |
45
b58a7ea1aeb2
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
44
diff
changeset
|
101 |
return errors.New("login failed (server did not return a JSON response - check your credentials)") |
11
3884f5276808
Work around server sending HTML error page on failed login
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
102 |
} |
45
b58a7ea1aeb2
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
44
diff
changeset
|
103 |
return errors.Wrap(err, "login failed") |
0 | 104 |
} |
105 |
||
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
11
diff
changeset
|
106 |
// splitIDs splits a list of IDs into an int64 array |
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
11
diff
changeset
|
107 |
func splitIDs(ids string) (list []int64, err error) { |
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
11
diff
changeset
|
108 |
var i int64 |
0 | 109 |
if ids == "" { |
110 |
return |
|
111 |
} |
|
112 |
l := strings.Split(ids, ",") |
|
113 |
for _, s := range l { |
|
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
11
diff
changeset
|
114 |
i, err = strconv.ParseInt(s, 10, 64) |
0 | 115 |
if err != nil { |
116 |
return |
|
117 |
} |
|
118 |
list = append(list, i) |
|
119 |
} |
|
120 |
return |
|
121 |
} |