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 |
"errors" |
|
10 |
"fmt" |
|
11 |
"strconv" |
|
12 |
"strings" |
|
13 |
|
|
14 |
"github.com/McKael/madon" |
|
15 |
"github.com/spf13/viper" |
|
16 |
) |
|
17 |
|
|
18 |
var scopes = []string{"read", "write", "follow"} |
|
19 |
|
|
20 |
func madonInit(signIn bool) error { |
|
21 |
if gClient == nil { |
|
22 |
if err := madonInitClient(); err != nil { |
|
23 |
return err |
|
24 |
} |
|
25 |
} |
|
26 |
if signIn { |
|
27 |
return madonLogin() |
|
28 |
} |
|
29 |
return nil |
|
30 |
} |
|
31 |
|
|
32 |
func madonInitClient() error { |
|
33 |
if gClient != nil { |
|
34 |
return nil |
|
35 |
} |
|
36 |
var err error |
|
37 |
|
|
38 |
// Overwrite variables using Viper |
|
39 |
instanceURL = viper.GetString("instance") |
|
40 |
appID = viper.GetString("app_id") |
|
41 |
appSecret = viper.GetString("app_secret") |
|
42 |
|
|
43 |
if instanceURL == "" { |
|
44 |
return errors.New("no instance provided") |
|
45 |
} |
|
46 |
|
|
47 |
if appID != "" && appSecret != "" { |
|
48 |
// We already have an app key/secret pair |
|
49 |
gClient, err = madon.RestoreApp(AppName, instanceURL, appID, appSecret, nil) |
|
50 |
if err != nil { |
|
51 |
return err |
|
52 |
} |
|
53 |
// Check instance |
|
54 |
if _, err := gClient.GetCurrentInstance(); err != nil { |
|
55 |
return fmt.Errorf("could not use provided app secrets") |
|
56 |
} |
|
57 |
if verbose { |
|
58 |
errPrint("Using provided app secrets") |
|
59 |
} |
|
60 |
return nil |
|
61 |
} |
|
62 |
|
|
63 |
if appID != "" || appSecret != "" { |
|
64 |
errPrint("Warning: provided app id/secrets incomplete -- registering again") |
|
65 |
} |
|
66 |
|
|
67 |
gClient, err = madon.NewApp(AppName, scopes, madon.NoRedirect, instanceURL) |
|
68 |
if err != nil { |
|
69 |
return fmt.Errorf("app registration failed: %s", err.Error()) |
|
70 |
} |
|
71 |
|
|
72 |
errPrint("Registred new application.") |
|
73 |
return nil |
|
74 |
} |
|
75 |
|
|
76 |
func madonLogin() error { |
|
77 |
if gClient == nil { |
|
78 |
return errors.New("application not registred") |
|
79 |
} |
|
80 |
|
|
81 |
token = viper.GetString("token") |
|
82 |
login = viper.GetString("login") |
|
83 |
password = viper.GetString("password") |
|
84 |
|
|
85 |
if token != "" { // TODO check token validity? |
|
86 |
if verbose { |
|
87 |
errPrint("Reusing existing token.") |
|
88 |
} |
|
89 |
gClient.SetUserToken(token, login, password, []string{}) |
|
90 |
return nil |
|
91 |
} |
|
92 |
|
|
93 |
err := gClient.LoginBasic(login, password, scopes) |
|
94 |
if err == nil { |
|
95 |
return nil |
|
96 |
} |
|
97 |
return fmt.Errorf("login failed: %s", err.Error()) |
|
98 |
} |
|
99 |
|
|
100 |
// splitIDs splits a list of IDs into an int array |
|
101 |
func splitIDs(ids string) (list []int, err error) { |
|
102 |
var i int |
|
103 |
if ids == "" { |
|
104 |
return |
|
105 |
} |
|
106 |
l := strings.Split(ids, ",") |
|
107 |
for _, s := range l { |
|
108 |
i, err = strconv.Atoi(s) |
|
109 |
if err != nil { |
|
110 |
return |
|
111 |
} |
|
112 |
list = append(list, i) |
|
113 |
} |
|
114 |
return |
|
115 |
} |