1 package main |
|
2 |
|
3 import ( |
|
4 "log" |
|
5 "os" |
|
6 "strings" |
|
7 |
|
8 "github.com/urfave/cli" |
|
9 |
|
10 "github.com/McKael/gondole" |
|
11 ) |
|
12 |
|
13 var ( |
|
14 fVerbose bool |
|
15 fInstance string |
|
16 fAuthMethod string |
|
17 fUsername, fPassword string |
|
18 fScopes string |
|
19 |
|
20 instance *gondole.Client |
|
21 cnf *Server |
|
22 |
|
23 // Default scopes |
|
24 ourScopes = []string{ |
|
25 "read", |
|
26 "write", |
|
27 "follow", |
|
28 } |
|
29 |
|
30 defaultInstanceURL = "https://mastodon.social" |
|
31 |
|
32 authMethods = map[string]bool{ |
|
33 "basic": true, |
|
34 "oauth2": true, |
|
35 } |
|
36 ) |
|
37 |
|
38 // Server holds our application details |
|
39 type Server struct { |
|
40 ID string `json:"id"` |
|
41 Name string `json:"name"` |
|
42 BearerToken string `json:"bearer_token"` |
|
43 APIBase string `json:"base_url"` |
|
44 InstanceURL string `json:"instance_url"` |
|
45 } |
|
46 |
|
47 type Config struct { |
|
48 Default string |
|
49 |
|
50 // Can be "oauth2", "basic" |
|
51 Auth string |
|
52 |
|
53 // If not using OAuth2 |
|
54 User string |
|
55 Password string |
|
56 } |
|
57 |
|
58 func setupEnvironment(c *cli.Context) (err error) { |
|
59 var config Config |
|
60 var scopes []string |
|
61 |
|
62 instanceURL := defaultInstanceURL |
|
63 if fInstance != "" { |
|
64 if strings.Contains(fInstance, "://") { |
|
65 instanceURL = fInstance |
|
66 } else { |
|
67 instanceURL = "https://" + fInstance |
|
68 } |
|
69 } |
|
70 |
|
71 instanceName := basename(instanceURL) |
|
72 |
|
73 if fAuthMethod != "" && authMethods[fAuthMethod] { |
|
74 |
|
75 } |
|
76 |
|
77 // Set scopes |
|
78 if fScopes != "" { |
|
79 scopes = strings.Split(fScopes, " ") |
|
80 } else { |
|
81 scopes = ourScopes |
|
82 } |
|
83 |
|
84 // Load configuration, will register if none is found |
|
85 cnf, err = LoadConfig(instanceName) |
|
86 if err == nil && cnf != nil { |
|
87 instance = &gondole.Client{ |
|
88 ID: cnf.ID, |
|
89 InstanceURL: cnf.InstanceURL, |
|
90 APIBase: cnf.APIBase, |
|
91 Name: cnf.Name, |
|
92 Secret: cnf.BearerToken, |
|
93 } |
|
94 } else { |
|
95 // Nothing exist yet |
|
96 /* |
|
97 defName := Config{ |
|
98 Default: instanceName, |
|
99 Auth: "basic", |
|
100 User: "", |
|
101 Password: "", |
|
102 } |
|
103 */ |
|
104 |
|
105 err = config.Write() |
|
106 if err != nil { |
|
107 log.Fatalf("error: can not write config for %s", instanceName) |
|
108 } |
|
109 |
|
110 instance, err = gondole.NewApp("gondole-cli", scopes, gondole.NoRedirect, instanceURL) |
|
111 if err != nil { |
|
112 log.Fatalf("error: can not register application:", err.Error()) |
|
113 } |
|
114 |
|
115 server := &Server{ |
|
116 ID: instance.ID, |
|
117 Name: instance.Name, |
|
118 BearerToken: instance.Secret, |
|
119 APIBase: instance.APIBase, |
|
120 InstanceURL: instance.InstanceURL, |
|
121 } |
|
122 err = server.WriteToken(instanceName) |
|
123 if err != nil { |
|
124 log.Fatalf("error: can not write token for %s", instance.Name) |
|
125 } |
|
126 |
|
127 cnf := Config{ |
|
128 Default: instance.Name, |
|
129 } |
|
130 |
|
131 err = cnf.Write() |
|
132 if err != nil { |
|
133 log.Fatalf("error: can not write config for %s", instance.Name) |
|
134 } |
|
135 } |
|
136 |
|
137 // Log in to the instance |
|
138 if fAuthMethod != "oauth2" { |
|
139 err = instance.LoginBasic(fUsername, fPassword, scopes) |
|
140 } |
|
141 |
|
142 return err |
|
143 } |
|
144 |
|
145 func init() { |
|
146 cli.VersionFlag = cli.BoolFlag{Name: "version, V"} |
|
147 |
|
148 cli.VersionPrinter = func(c *cli.Context) { |
|
149 log.Printf("API wrapper: %s Mastodon CLI: %s\n", c.App.Version, gondole.GondoleVersion) |
|
150 } |
|
151 } |
|
152 |
|
153 func main() { |
|
154 |
|
155 app := cli.NewApp() |
|
156 app.Name = "gondole" |
|
157 app.Usage = "Mastodon CLI interface" |
|
158 app.Author = "Ollivier Robert <roberto@keltia.net>" |
|
159 app.Version = gondole.GondoleVersion |
|
160 //app.HideVersion = true |
|
161 |
|
162 app.Before = setupEnvironment |
|
163 |
|
164 app.Flags = []cli.Flag{ |
|
165 cli.StringFlag{ |
|
166 Name: "auth,A", |
|
167 Usage: "authentication mode", |
|
168 Destination: &fAuthMethod, |
|
169 }, |
|
170 cli.StringFlag{ |
|
171 Name: "instance,I", |
|
172 Usage: "use that instance", |
|
173 Destination: &fInstance, |
|
174 }, |
|
175 cli.StringFlag{ |
|
176 Name: "scopes,S", |
|
177 Usage: "use these scopes", |
|
178 Destination: &fScopes, |
|
179 }, |
|
180 cli.StringFlag{ |
|
181 Name: "username,login", |
|
182 Usage: "user name", |
|
183 Destination: &fUsername, |
|
184 }, |
|
185 cli.StringFlag{ |
|
186 Name: "password", |
|
187 Usage: "user password", |
|
188 Destination: &fPassword, |
|
189 }, |
|
190 cli.BoolFlag{ |
|
191 Name: "verbose,v", |
|
192 Usage: "verbose mode", |
|
193 Destination: &fVerbose, |
|
194 }, |
|
195 } |
|
196 app.Run(os.Args) |
|
197 } |
|