var account mastodon.Account
code := account.Authorize(host, app)
- token, err := account.RequestToken(host, app, code)
+ token, err = account.RequestToken(host, app, code)
+ if err != nil {
+ panic(err.Error())
+ }
+
+ err = token.Save()
if err != nil {
panic(err.Error())
}
"fmt"
"os"
+ "git.vexingworkshop.com/signal9/dead-tooter/pkg/mastodon"
"github.com/spf13/cobra"
)
+var token mastodon.Token
+
var rootCmd = &cobra.Command{
Use: "dead-tooter",
Short: "A CLI for Mastodon hate scripts",
Long: `Provides a collection of capabilities that may or may not
be present in the Mastodon web UI`,
+ PersistentPreRun: func(cmd *cobra.Command, args []string) {
+ var err error
+ token, err = mastodon.LoadToken()
+ if err != nil {
+ fmt.Println("No authentication token found.\nYou must login before performing futher commands.")
+ }
+ },
+
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("d34d-t00t3r")
},
+
+ PersistentPostRun: func(cmd *cobra.Command, args []string) {
+ err := token.Save()
+ if err != nil {
+ panic(err.Error())
+ }
+ },
}
func Execute() {
v.Set("grant_type", "authorization_code")
u := url.URL{
- Host: host,
- Path: "oauth/token",
- RawQuery: v.Encode(),
+ Host: host,
+ Path: "oauth/token",
}
u.Scheme = "https"
"os"
)
-const configdir = "foodir/"
+const ConfigDir = "foodir/"
// RedirectUris when passed to the redirect_uris parameter, will
// show return the authorization code instead of redirecting the client.
const RedirectUris = "urn:ietf:wg:oauth:2.0:oob"
-// Token struct contains the data returned by the Application login request.
-type Token struct {
- AccessToken string `json:"access_token"`
- TokenType string `json:"token_type"`
- Scope string `json:"scope"`
- CreatedAt int `json:"created_at"`
-}
-
// APIError contains the application specific error message returned
// by the Mastodon API.
type APIError struct {
// Load will hydrate an Application instance based upon data stored in
// a file.
func Load(name string) (app Application, err error) {
- data, err := os.ReadFile(configdir + name)
+ data, err := os.ReadFile(ConfigDir + name)
if err != nil && os.IsNotExist(err) {
return
}
// Save will store a serialized version of the Application struct to a file.
func (a *Application) Save() (err error) {
- err = os.Mkdir(configdir, 0750)
+ err = os.Mkdir(ConfigDir, 0750)
if err != nil && !os.IsExist(err) {
return
}
return
}
- err = os.WriteFile(configdir+a.Name, data, 0666)
+ err = os.WriteFile(ConfigDir+a.Name, data, 0666)
if err != nil {
return
}
--- /dev/null
+package mastodon
+
+import (
+ "encoding/json"
+ "os"
+)
+
+// Token struct contains the data returned by the Application login request.
+type Token struct {
+ AccessToken string `json:"access_token"`
+ TokenType string `json:"token_type"`
+ Scope string `json:"scope"`
+ CreatedAt int `json:"created_at"`
+}
+
+// LoadToken deserializes the application authentication token from disc, if it
+// exists.
+func LoadToken() (token Token, err error) {
+ data, err := os.ReadFile(ConfigDir + "/token")
+ if err != nil && os.IsNotExist(err) {
+ return
+ }
+
+ err = json.Unmarshal(data, &token)
+ if err != nil {
+ return
+ }
+
+ return
+}
+
+// Save serializes the application token to disk.
+func (t *Token) Save() (err error) {
+ data, err := json.Marshal(t)
+ if err != nil {
+ return
+ }
+
+ err = os.WriteFile(ConfigDir+"/token", data, 0666)
+ if err != nil {
+ return
+ }
+
+ return
+}