From 4ba4d1ffdbbd9026bbbe92ed1018d47757091423 Mon Sep 17 00:00:00 2001 From: Adam Shamblin Date: Fri, 30 Dec 2022 00:34:12 -0700 Subject: [PATCH] Clean up some error handling, start on output template for accounts --- cmd/account.go | 24 ++++++++++++++++-------- pkg/mastodon/account.go | 19 +++++++++++++++++++ pkg/mastodon/display.go | 20 +++++++++++++++++--- 3 files changed, 52 insertions(+), 11 deletions(-) diff --git a/cmd/account.go b/cmd/account.go index 014fd72..0555c31 100644 --- a/cmd/account.go +++ b/cmd/account.go @@ -2,6 +2,7 @@ package tooter import ( "fmt" + "os" "git.vexingworkshop.com/signal9/dead-tooter/pkg/mastodon" "github.com/spf13/cobra" @@ -32,7 +33,8 @@ var getAccountCmd = &cobra.Command{ PreRun: func(cmd *cobra.Command, args []string) { err := account.VerifyCredentials(host, token) if err != nil { - fmt.Printf("Error verifying credentials: %s", err.Error()) + fmt.Println("Failed to verify credentials.") + os.Exit(1) } }, @@ -47,10 +49,11 @@ var getAccountCmd = &cobra.Command{ acct, err := mastodon.GetAccount(id, host, token) if err != nil { - panic(err.Error()) + fmt.Println("Failed to get account.") + os.Exit(1) } - fmt.Printf("%+v", acct) + acct.DisplayLong() }, } @@ -62,7 +65,8 @@ var getFollowersCmd = &cobra.Command{ PreRun: func(cmd *cobra.Command, args []string) { err := account.VerifyCredentials(host, token) if err != nil { - panic(err.Error()) + fmt.Println("Failed to verify credentials.") + os.Exit(1) } }, @@ -84,14 +88,16 @@ var getFollowingCmd = &cobra.Command{ PreRun: func(cmd *cobra.Command, args []string) { err := account.VerifyCredentials(host, token) if err != nil { - panic(err.Error()) + fmt.Println("Failed to verify credentials.") + os.Exit(1) } }, Run: func(cmd *cobra.Command, args []string) { following, err := account.GetFollowing(host, token) if err != nil { - panic(err.Error()) + fmt.Println("Failed to get following list.") + os.Exit(1) } following.Display() @@ -106,14 +112,16 @@ var getListsCmd = &cobra.Command{ PreRun: func(cmd *cobra.Command, args []string) { err := account.VerifyCredentials(host, token) if err != nil { - panic(err.Error()) + fmt.Println("Failed to verify credentials.") + os.Exit(1) } }, Run: func(cmd *cobra.Command, args []string) { lists, err := account.GetLists(host, token) if err != nil { - panic(err.Error()) + fmt.Println("Failed to get following list.") + os.Exit(1) } lists.Display() diff --git a/pkg/mastodon/account.go b/pkg/mastodon/account.go index d91699e..352d53d 100644 --- a/pkg/mastodon/account.go +++ b/pkg/mastodon/account.go @@ -11,6 +11,7 @@ import ( "os/exec" "strings" "text/tabwriter" + "text/template" "git.vexingworkshop.com/signal9/dead-tooter/pkg/mastodon/api" ) @@ -73,6 +74,24 @@ func (a *Account) Display() string { return strings.Join([]string{a.ID, a.Acct}, "\t") } +const accountTemplate = `Display Name: {{.DisplayName}} +Account ID: {{.ID}} User Name: {{.UserName}} +Followers: {{.FollowersCount}} Following: {{.FollowingCount}} +Last Status: {{.LastStatusAt}} +` + +// DisplayLong is a multiline display for a single Account. +func (a *Account) DisplayLong() (err error) { + tmpl, err := template.New("account-long").Parse(accountTemplate) + if err != nil { + return + } + + tmpl.Execute(os.Stdout, a) + + return err +} + // Display a collection of Accounts func (aa AccountCollection) Display() { w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0) diff --git a/pkg/mastodon/display.go b/pkg/mastodon/display.go index a7ebc0f..9bd233f 100644 --- a/pkg/mastodon/display.go +++ b/pkg/mastodon/display.go @@ -1,9 +1,23 @@ package mastodon +import ( + "fmt" + "os" + "text/tabwriter" +) + +// DisplayItem interface provides a means to display a single item. type DisplayItem interface { - Display() (string, error) + Display() string } -type DisplayList interface { - Display(items []interface{}) error +// DisplayItemCollection provides a means to display a collection of items. +type DisplayItemCollection []DisplayItem + +func (c DisplayItemCollection) Display() { + w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0) + for _, item := range c { + fmt.Fprintln(w, item.Display()) + } + w.Flush() } -- 2.39.5