author | Mikael Berthe <mikael@lilotux.net> |
Sun, 30 Apr 2017 23:22:09 +0200 | |
changeset 45 | b58a7ea1aeb2 |
parent 44 | 6da40ca4534c |
child 46 | 13717ea83333 |
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 |
if appID != "" && appSecret != "" { |
|
47 |
// We already have an app key/secret pair |
|
48 |
gClient, err = madon.RestoreApp(AppName, instanceURL, appID, appSecret, nil) |
|
49 |
if err != nil { |
|
50 |
return err |
|
51 |
} |
|
52 |
// Check instance |
|
53 |
if _, err := gClient.GetCurrentInstance(); err != nil { |
|
45
b58a7ea1aeb2
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
44
diff
changeset
|
54 |
return errors.New("could not use provided app secrets") |
0 | 55 |
} |
56 |
if verbose { |
|
57 |
errPrint("Using provided app secrets") |
|
58 |
} |
|
59 |
return nil |
|
60 |
} |
|
61 |
||
62 |
if appID != "" || appSecret != "" { |
|
63 |
errPrint("Warning: provided app id/secrets incomplete -- registering again") |
|
64 |
} |
|
65 |
||
66 |
gClient, err = madon.NewApp(AppName, scopes, madon.NoRedirect, instanceURL) |
|
67 |
if err != nil { |
|
45
b58a7ea1aeb2
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
44
diff
changeset
|
68 |
return errors.Wrap(err, "app registration failed") |
0 | 69 |
} |
70 |
||
71 |
errPrint("Registred new application.") |
|
72 |
return nil |
|
73 |
} |
|
74 |
||
75 |
func madonLogin() error { |
|
76 |
if gClient == nil { |
|
77 |
return errors.New("application not registred") |
|
78 |
} |
|
79 |
||
80 |
token = viper.GetString("token") |
|
81 |
login = viper.GetString("login") |
|
82 |
password = viper.GetString("password") |
|
83 |
||
84 |
if token != "" { // TODO check token validity? |
|
85 |
if verbose { |
|
86 |
errPrint("Reusing existing token.") |
|
87 |
} |
|
88 |
gClient.SetUserToken(token, login, password, []string{}) |
|
89 |
return nil |
|
90 |
} |
|
91 |
||
92 |
err := gClient.LoginBasic(login, password, scopes) |
|
93 |
if err == nil { |
|
94 |
return nil |
|
95 |
} |
|
11
3884f5276808
Work around server sending HTML error page on failed login
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
96 |
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
|
97 |
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
|
98 |
} |
45
b58a7ea1aeb2
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
44
diff
changeset
|
99 |
return errors.Wrap(err, "login failed") |
0 | 100 |
} |
101 |
||
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
11
diff
changeset
|
102 |
// 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
|
103 |
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
|
104 |
var i int64 |
0 | 105 |
if ids == "" { |
106 |
return |
|
107 |
} |
|
108 |
l := strings.Split(ids, ",") |
|
109 |
for _, s := range l { |
|
44
6da40ca4534c
Sync with Madon; switch IDs to int64 integers
Mikael Berthe <mikael@lilotux.net>
parents:
11
diff
changeset
|
110 |
i, err = strconv.ParseInt(s, 10, 64) |
0 | 111 |
if err != nil { |
112 |
return |
|
113 |
} |
|
114 |
list = append(list, i) |
|
115 |
} |
|
116 |
return |
|
117 |
} |