]> Vexing Labs - dead-tooter.git/commitdiff
Clean up some error handling, start on output template for accounts
authorAdam Shamblin <adam@vexingworkshop.com>
Fri, 30 Dec 2022 07:34:12 +0000 (00:34 -0700)
committerAdam Shamblin <adam@vexingworkshop.com>
Fri, 30 Dec 2022 07:34:12 +0000 (00:34 -0700)
cmd/account.go
pkg/mastodon/account.go
pkg/mastodon/display.go

index 014fd724fd65fa47a8f60cd0a79cc1cd942f0bc7..0555c3185a84a27bd88596b456a9cd3c196d04ad 100644 (file)
@@ -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()
index d91699ec3d5376dcb45fdcd3a39eec8de10173dd..352d53da049f1fa52f61f80c179f538b713b956a 100644 (file)
@@ -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)
index a7ebc0f6c734f6dc4c359370480d976cea191e6e..9bd233f0b61643fa223d403c1904112104502756 100644 (file)
@@ -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()
 }