From 87d6bd237beaa35743f6e97ae6a51c0988253af9 Mon Sep 17 00:00:00 2001 From: Adam Shamblin Date: Mon, 19 Dec 2022 21:01:34 -0700 Subject: [PATCH] Improve errors --- cmd/login.go | 12 +++++++----- cmd/root.go | 7 ++----- pkg/mastodon/account.go | 4 ++-- pkg/mastodon/application.go | 6 +----- pkg/mastodon/oauth.go | 2 +- 5 files changed, 13 insertions(+), 18 deletions(-) diff --git a/cmd/login.go b/cmd/login.go index a276725..32f3078 100644 --- a/cmd/login.go +++ b/cmd/login.go @@ -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()) } } diff --git a/cmd/root.go b/cmd/root.go index f7b91f8..94b50b7 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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 { diff --git a/pkg/mastodon/account.go b/pkg/mastodon/account.go index f554f91..dcc0896 100644 --- a/pkg/mastodon/account.go +++ b/pkg/mastodon/account.go @@ -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 diff --git a/pkg/mastodon/application.go b/pkg/mastodon/application.go index df8af3f..0736b84 100644 --- a/pkg/mastodon/application.go +++ b/pkg/mastodon/application.go @@ -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 } diff --git a/pkg/mastodon/oauth.go b/pkg/mastodon/oauth.go index 1055575..70866ad 100644 --- a/pkg/mastodon/oauth.go +++ b/pkg/mastodon/oauth.go @@ -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 -- 2.39.5