Compare commits

...

2 commits

Author SHA1 Message Date
9c8476fa97
feat: add reblogging (boosting) support
Add support for reblogging (boosting) a status by adding a boost to said
status.
2024-06-06 06:23:26 +01:00
c8187587a8
fix: print commands in aligned columns
Use the text/tabwriter to write the commands and their summaries in
aligned columns when printing the help text.
2024-06-06 06:21:16 +01:00
5 changed files with 71 additions and 17 deletions

View file

@ -9,6 +9,7 @@ import (
"fmt" "fmt"
"slices" "slices"
"strings" "strings"
"text/tabwriter"
) )
func usageFunc(summaries map[string]string) func() { func usageFunc(summaries map[string]string) func() {
@ -31,18 +32,22 @@ func usageFunc(summaries map[string]string) func() {
builder.WriteString("VERSION:\n " + binaryVersion + "\n\n") builder.WriteString("VERSION:\n " + binaryVersion + "\n\n")
} }
builder.WriteString("USAGE:\n enbas [flags]\n enbas [command]\n\nCOMMANDS:") builder.WriteString("USAGE:\n enbas [flags]\n enbas [flags] [command]\n\nCOMMANDS:")
tableWriter := tabwriter.NewWriter(&builder, 0, 8, 0, '\t', 0)
for _, cmd := range cmds { for _, cmd := range cmds {
fmt.Fprintf(&builder, "\n %s\t%s", cmd, summaries[cmd]) fmt.Fprintf(tableWriter, "\n %s\t%s", cmd, summaries[cmd])
} }
builder.WriteString("\n\nFLAGS:\n --help\n print the help message\n") tableWriter.Flush()
builder.WriteString("\n\nFLAGS:\n --help\n print the help message")
flag.VisitAll(func(f *flag.Flag) { flag.VisitAll(func(f *flag.Flag) {
fmt.Fprintf(&builder, "\n --%s\n %s\n", f.Name, f.Usage) fmt.Fprintf(&builder, "\n --%s\n %s", f.Name, f.Usage)
}) })
builder.WriteString("\nUse \"enbas [command] --help\" for more information about a command.\n") builder.WriteString("\n\nUse \"enbas [command] --help\" for more information about a command.\n")
w := flag.CommandLine.Output() w := flag.CommandLine.Output()
fmt.Fprint(w, builder.String()) fmt.Fprint(w, builder.String())

View file

@ -153,3 +153,29 @@ func (g *Client) GetLikedStatuses(limit int, resourceName string) (model.StatusL
return liked, nil return liked, nil
} }
func (g *Client) ReblogStatus(statusID string) error {
url := g.Authentication.Instance + "/api/v1/statuses/" + statusID + "/reblog"
if err := g.sendRequest(http.MethodPost, url, nil, nil); err != nil {
return fmt.Errorf(
"received an error after sending the request to reblog the status; %w",
err,
)
}
return nil
}
func (g *Client) UnreblogStatus(statusID string) error {
url := g.Authentication.Instance + "/api/v1/statuses/" + statusID + "/unreblog"
if err := g.sendRequest(http.MethodPost, url, nil, nil); err != nil {
return fmt.Errorf(
"received an error after sending the request to un-reblog the status; %w",
err,
)
}
return nil
}

View file

@ -187,9 +187,14 @@ func (a *AddExecutor) addStatusToBookmarks(gtsClient *client.Client) error {
} }
func (a *AddExecutor) addToStatus(gtsClient *client.Client) error { func (a *AddExecutor) addToStatus(gtsClient *client.Client) error {
if a.statusID == "" {
return FlagNotSetError{flagText: flagStatusID}
}
funcMap := map[string]func(*client.Client) error{ funcMap := map[string]func(*client.Client) error{
resourceStar: a.addStarToStatus, resourceStar: a.addStarToStatus,
resourceLike: a.addStarToStatus, resourceLike: a.addStarToStatus,
resourceBoost: a.addBoostToStatus,
} }
doFunc, ok := funcMap[a.resourceType] doFunc, ok := funcMap[a.resourceType]
@ -204,10 +209,6 @@ func (a *AddExecutor) addToStatus(gtsClient *client.Client) error {
} }
func (a *AddExecutor) addStarToStatus(gtsClient *client.Client) error { func (a *AddExecutor) addStarToStatus(gtsClient *client.Client) error {
if a.statusID == "" {
return FlagNotSetError{flagText: flagStatusID}
}
if err := gtsClient.LikeStatus(a.statusID); err != nil { if err := gtsClient.LikeStatus(a.statusID); err != nil {
return fmt.Errorf("unable to add the %s to the status: %w", a.resourceType, err) return fmt.Errorf("unable to add the %s to the status: %w", a.resourceType, err)
} }
@ -216,3 +217,13 @@ func (a *AddExecutor) addStarToStatus(gtsClient *client.Client) error {
return nil return nil
} }
func (a *AddExecutor) addBoostToStatus(gtsClient *client.Client) error {
if err := gtsClient.ReblogStatus(a.statusID); err != nil {
return fmt.Errorf("unable to add the boost to the status: %w", err)
}
fmt.Println("Successfully added the boost to the status.")
return nil
}

View file

@ -38,6 +38,7 @@ const (
resourceAccount = "account" resourceAccount = "account"
resourceBlocked = "blocked" resourceBlocked = "blocked"
resourceBookmarks = "bookmarks" resourceBookmarks = "bookmarks"
resourceBoost = "boost"
resourceFollowers = "followers" resourceFollowers = "followers"
resourceFollowing = "following" resourceFollowing = "following"
resourceInstance = "instance" resourceInstance = "instance"

View file

@ -178,9 +178,14 @@ func (r *RemoveExecutor) removeStatusFromBookmarks(gtsClient *client.Client) err
} }
func (r *RemoveExecutor) removeFromStatus(gtsClient *client.Client) error { func (r *RemoveExecutor) removeFromStatus(gtsClient *client.Client) error {
if r.statusID == "" {
return FlagNotSetError{flagText: flagStatusID}
}
funcMap := map[string]func(*client.Client) error{ funcMap := map[string]func(*client.Client) error{
resourceStar: r.removeStarFromStatus, resourceStar: r.removeStarFromStatus,
resourceLike: r.removeStarFromStatus, resourceLike: r.removeStarFromStatus,
resourceBoost: r.removeBoostFromStatus,
} }
doFunc, ok := funcMap[r.resourceType] doFunc, ok := funcMap[r.resourceType]
@ -195,10 +200,6 @@ func (r *RemoveExecutor) removeFromStatus(gtsClient *client.Client) error {
} }
func (r *RemoveExecutor) removeStarFromStatus(gtsClient *client.Client) error { func (r *RemoveExecutor) removeStarFromStatus(gtsClient *client.Client) error {
if r.statusID == "" {
return FlagNotSetError{flagText: flagStatusID}
}
if err := gtsClient.UnlikeStatus(r.statusID); err != nil { if err := gtsClient.UnlikeStatus(r.statusID); err != nil {
return fmt.Errorf("unable to remove the %s from the status: %w", r.resourceType, err) return fmt.Errorf("unable to remove the %s from the status: %w", r.resourceType, err)
} }
@ -207,3 +208,13 @@ func (r *RemoveExecutor) removeStarFromStatus(gtsClient *client.Client) error {
return nil return nil
} }
func (r *RemoveExecutor) removeBoostFromStatus(gtsClient *client.Client) error {
if err := gtsClient.UnreblogStatus(r.statusID); err != nil {
return fmt.Errorf("unable to remove the boost from the status: %w", err)
}
fmt.Println("Successfully removed the boost from the status.")
return nil
}