]> Vexing Labs - dead-tooter.git/commitdiff
Improve errors
authorAdam Shamblin <adam@vexingworkshop.com>
Tue, 20 Dec 2022 04:01:34 +0000 (21:01 -0700)
committerAdam Shamblin <adam@vexingworkshop.com>
Tue, 20 Dec 2022 04:01:34 +0000 (21:01 -0700)
cmd/login.go
cmd/root.go
pkg/mastodon/account.go
pkg/mastodon/application.go
pkg/mastodon/oauth.go

index a2767253094d9571eca01e3c65cfc9629b5baaf4..32f3078dbc35c5e8246bec60f502c536445ca206 100644 (file)
@@ -1,6 +1,8 @@
 package tooter
 
 import (
+       "fmt"
+
        "git.vexingworkshop.com/signal9/dead-tooter/pkg/mastodon"
        "github.com/spf13/cobra"
 )
@@ -30,12 +32,12 @@ func login() {
 
                app, err = mastodon.Create(host, client)
                if err != nil {
-                       panic(err.Error())
+                       fmt.Printf("Failed to create client: %s\n", err.Error())
                }
 
                err = app.Save()
                if err != nil {
-                       panic(err.Error())
+                       fmt.Printf("Failed to store client info: %s\n", err.Error())
                }
        }
 
@@ -43,16 +45,16 @@ func login() {
        code := account.Authorize(host, app)
        token, err = account.RequestToken(host, app, code)
        if err != nil {
-               panic(err.Error())
+               fmt.Printf("Failed to acquire request token: %s\n", err.Error())
        }
 
        err = token.Save()
        if err != nil {
-               panic(err.Error())
+               fmt.Printf("Failed to store access token: %s\n", err.Error())
        }
 
        err = account.VerifyCredentials(host, token)
        if err != nil {
-               panic(err.Error())
+               fmt.Printf("Failed to verify credentials: %s\n", err.Error())
        }
 }
index f7b91f8b9c439b8ea5378857354324274e28bd18..94b50b74c5e9da50e1a81f807ac99cbdc3e8ad0f 100644 (file)
@@ -15,13 +15,14 @@ func init() {
        rootCmd.PersistentFlags().StringVarP(&host,
                "host", "H", "", "Mastodon host where your account lives.")
        rootCmd.MarkFlagRequired("host")
+
 }
 
 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`,
+be present in the Mastodon web UI.`,
 
        PersistentPreRun: func(cmd *cobra.Command, args []string) {
                var err error
@@ -31,10 +32,6 @@ var rootCmd = &cobra.Command{
                }
        },
 
-       Run: func(cmd *cobra.Command, args []string) {
-               fmt.Println("d34d-t00t3r")
-       },
-
        PersistentPostRun: func(cmd *cobra.Command, args []string) {
                err := token.Save()
                if err != nil {
index f554f9136f8bed1719950d43c233a44645e47e4f..dcc0896c6dfb206ab1337b21c038c67e7f6c5ff4 100644 (file)
@@ -77,13 +77,13 @@ func (a *Account) Authorize(host string, app Application) (code string) {
 
        err := exec.Command("xdg-open", u.String()).Start()
        if err != nil {
-               panic(err.Error())
+               return
        }
 
        fmt.Print("Enter returned code: ")
        _, err = fmt.Scanln(&code)
        if err != nil {
-               panic(err.Error())
+               return
        }
 
        return
index df8af3f3cf09af7f3bc698ae943ac14514100395..0736b8439af657e9dc74d49bcca16e6182900c8e 100644 (file)
@@ -10,10 +10,6 @@ import (
        "path/filepath"
 )
 
-// ConfigDir points to the default location of serialized files,
-// such as application info and tokens
-const ConfigDir = ".dead-tooter"
-
 // 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"
@@ -109,7 +105,7 @@ func (a *Application) Save() (err error) {
        }
        configdir = filepath.Join(configdir, "dead-tooter")
 
-       err = os.Mkdir(ConfigDir, 0750)
+       err = os.Mkdir(configdir, 0750)
        if err != nil && !os.IsExist(err) {
                return
        }
index 105557560d40d5b9c326c9bb07bd08b0a288da9b..70866ad76c97e125cae8b3552ffb691c57b6e2d3 100644 (file)
@@ -47,8 +47,8 @@ func (t *Token) Save() (err error) {
        if err != nil {
                return
        }
-
        tokenfile := filepath.Join(configdir, "dead-tooter", "token")
+
        err = os.WriteFile(tokenfile, data, 0666)
        if err != nil {
                return