Compare commits
No commits in common. "ae2b5d80362eadeafa031ee5036be1c2d81febf9" and "6f1879eebaa64bb0a61ba1ccb0b08dc6dd2ca650" have entirely different histories.
ae2b5d8036
...
6f1879eeba
8 changed files with 47 additions and 198 deletions
|
@ -7,34 +7,29 @@ import (
|
||||||
|
|
||||||
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
|
||||||
"codeflow.dananglin.me.uk/apollo/enbas/internal/config"
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/config"
|
||||||
"codeflow.dananglin.me.uk/apollo/enbas/internal/model"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type showCommand struct {
|
type showCommand struct {
|
||||||
*flag.FlagSet
|
*flag.FlagSet
|
||||||
myAccount bool
|
myAccount bool
|
||||||
targetType string
|
targetType string
|
||||||
account string
|
account string
|
||||||
statusID string
|
statusID string
|
||||||
timelineType string
|
|
||||||
timelineListID string
|
|
||||||
timelineTagName string
|
|
||||||
timelineLimit int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func newShowCommand(name, summary string) *showCommand {
|
func newShowCommand(name, summary string) *showCommand {
|
||||||
command := showCommand{
|
command := showCommand{
|
||||||
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
||||||
|
myAccount: false,
|
||||||
|
targetType: "",
|
||||||
|
account: "",
|
||||||
|
statusID: "",
|
||||||
}
|
}
|
||||||
|
|
||||||
command.BoolVar(&command.myAccount, "my-account", false, "set to true to lookup your account")
|
command.BoolVar(&command.myAccount, "my-account", false, "set to true to lookup your account")
|
||||||
command.StringVar(&command.targetType, "type", "", "specify the type of resource to display")
|
command.StringVar(&command.targetType, "type", "", "specify the type of resource to display")
|
||||||
command.StringVar(&command.account, "account", "", "specify the account URI to lookup")
|
command.StringVar(&command.account, "account", "", "specify the account URI to lookup")
|
||||||
command.StringVar(&command.statusID, "status-id", "", "specify the ID of the status to display")
|
command.StringVar(&command.statusID, "status-id", "", "specify the ID of the status to display")
|
||||||
command.StringVar(&command.timelineType, "timeline-type", "home", "specify the type of timeline to display (valid values are home, public, list and tag)")
|
|
||||||
command.StringVar(&command.timelineListID, "timeline-list-id", "", "specify the ID of the list timeline to display")
|
|
||||||
command.StringVar(&command.timelineTagName, "timeline-tag-name", "", "specify the name of the tag timeline to display")
|
|
||||||
command.IntVar(&command.timelineLimit, "timeline-limit", 5, "specify the number of statuses to display")
|
|
||||||
command.Usage = commandUsageFunc(name, summary, command.FlagSet)
|
command.Usage = commandUsageFunc(name, summary, command.FlagSet)
|
||||||
|
|
||||||
return &command
|
return &command
|
||||||
|
@ -50,7 +45,6 @@ func (c *showCommand) Execute() error {
|
||||||
"instance": c.showInstance,
|
"instance": c.showInstance,
|
||||||
"account": c.showAccount,
|
"account": c.showAccount,
|
||||||
"status": c.showStatus,
|
"status": c.showStatus,
|
||||||
"timeline": c.showTimeline,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
doFunc, ok := funcMap[c.targetType]
|
doFunc, ok := funcMap[c.targetType]
|
||||||
|
@ -114,45 +108,3 @@ func (c *showCommand) showStatus(gts *client.Client) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *showCommand) showTimeline(gts *client.Client) error {
|
|
||||||
var (
|
|
||||||
timeline model.Timeline
|
|
||||||
err error
|
|
||||||
)
|
|
||||||
|
|
||||||
switch c.timelineType {
|
|
||||||
case "home":
|
|
||||||
timeline, err = gts.GetHomeTimeline(c.timelineLimit)
|
|
||||||
case "public":
|
|
||||||
timeline, err = gts.GetPublicTimeline(c.timelineLimit)
|
|
||||||
case "list":
|
|
||||||
if c.timelineListID == "" {
|
|
||||||
return errors.New("the timeline-list-id flag is not set")
|
|
||||||
}
|
|
||||||
|
|
||||||
timeline, err = gts.GetListTimeline(c.timelineListID, c.timelineLimit)
|
|
||||||
case "tag":
|
|
||||||
if c.timelineTagName == "" {
|
|
||||||
return errors.New("the timeline-tag-name flag is not set")
|
|
||||||
}
|
|
||||||
|
|
||||||
timeline, err = gts.GetTagTimeline(c.timelineTagName, c.timelineLimit)
|
|
||||||
default:
|
|
||||||
return fmt.Errorf("%q is not a valid type of timeline", c.timelineType)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("unable to retrieve the %s timeline; %w", c.timelineType, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(timeline.Statuses) == 0 {
|
|
||||||
fmt.Println("There are no statuses in this timeline.")
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println(timeline)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
|
@ -22,7 +22,8 @@ func commandUsageFunc(name, summary string, flagset *flag.FlagSet) func() {
|
||||||
flagset.VisitAll(func(f *flag.Flag) {
|
flagset.VisitAll(func(f *flag.Flag) {
|
||||||
fmt.Fprintf(
|
fmt.Fprintf(
|
||||||
&builder,
|
&builder,
|
||||||
"\n --%s\n %s",
|
"\n -%s, --%s\n %s",
|
||||||
|
f.Name,
|
||||||
f.Name,
|
f.Name,
|
||||||
f.Usage,
|
f.Usage,
|
||||||
)
|
)
|
||||||
|
@ -62,9 +63,9 @@ func enbasUsageFunc(summaries map[string]string) func() {
|
||||||
fmt.Fprintf(&builder, "\n %s\t%s", cmd, summaries[cmd])
|
fmt.Fprintf(&builder, "\n %s\t%s", cmd, summaries[cmd])
|
||||||
}
|
}
|
||||||
|
|
||||||
builder.WriteString("\n\nFLAGS:\n --help\n print the help message\n")
|
builder.WriteString("\n\nFLAGS:\n -help, --help\n print the help message\n")
|
||||||
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, --%s\n %s\n", f.Name, f.Name, f.Usage)
|
||||||
})
|
})
|
||||||
|
|
||||||
builder.WriteString("\nUse \"enbas [command] --help\" for more information about a command.\n")
|
builder.WriteString("\nUse \"enbas [command] --help\" for more information about a command.\n")
|
||||||
|
|
|
@ -34,14 +34,14 @@ func NewClientFromConfig() (*Client, error) {
|
||||||
func NewClient(authentication config.Authentication) *Client {
|
func NewClient(authentication config.Authentication) *Client {
|
||||||
httpClient := http.Client{}
|
httpClient := http.Client{}
|
||||||
|
|
||||||
gtsClient := Client{
|
client := Client{
|
||||||
Authentication: authentication,
|
Authentication: authentication,
|
||||||
HTTPClient: httpClient,
|
HTTPClient: httpClient,
|
||||||
UserAgent: internal.UserAgent,
|
UserAgent: internal.UserAgent,
|
||||||
Timeout: 5 * time.Second,
|
Timeout: 5 * time.Second,
|
||||||
}
|
}
|
||||||
|
|
||||||
return >sClient
|
return &client
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Client) VerifyCredentials() (model.Account, error) {
|
func (g *Client) VerifyCredentials() (model.Account, error) {
|
||||||
|
@ -71,7 +71,7 @@ func (g *Client) GetInstance() (model.InstanceV2, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Client) GetAccount(accountURI string) (model.Account, error) {
|
func (g *Client) GetAccount(accountURI string) (model.Account, error) {
|
||||||
path := "/api/v1/accounts/lookup?acct=" + accountURI
|
path := "/api/v1/accounts/lookup" + "?acct=" + accountURI
|
||||||
url := g.Authentication.Instance + path
|
url := g.Authentication.Instance + path
|
||||||
|
|
||||||
var account model.Account
|
var account model.Account
|
||||||
|
@ -96,64 +96,6 @@ func (g *Client) GetStatus(statusID string) (model.Status, error) {
|
||||||
return status, nil
|
return status, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Client) GetHomeTimeline(limit int) (model.Timeline, error) {
|
|
||||||
path := fmt.Sprintf("/api/v1/timelines/home?limit=%d", limit)
|
|
||||||
|
|
||||||
timeline := model.Timeline{
|
|
||||||
Name: "HOME TIMELINE",
|
|
||||||
Statuses: nil,
|
|
||||||
}
|
|
||||||
|
|
||||||
return g.getTimeline(path, timeline)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *Client) GetPublicTimeline(limit int) (model.Timeline, error) {
|
|
||||||
path := fmt.Sprintf("/api/v1/timelines/public?limit=%d", limit)
|
|
||||||
|
|
||||||
timeline := model.Timeline{
|
|
||||||
Name: "PUBLIC TIMELINE",
|
|
||||||
Statuses: nil,
|
|
||||||
}
|
|
||||||
|
|
||||||
return g.getTimeline(path, timeline)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *Client) GetListTimeline(listID string, limit int) (model.Timeline, error) {
|
|
||||||
path := fmt.Sprintf("/api/v1/timelines/list/%s?limit=%d", listID, limit)
|
|
||||||
|
|
||||||
timeline := model.Timeline{
|
|
||||||
Name: "LIST: " + listID,
|
|
||||||
Statuses: nil,
|
|
||||||
}
|
|
||||||
|
|
||||||
return g.getTimeline(path, timeline)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *Client) GetTagTimeline(tag string, limit int) (model.Timeline, error) {
|
|
||||||
path := fmt.Sprintf("/api/v1/timelines/tag/%s?limit=%d", tag, limit)
|
|
||||||
|
|
||||||
timeline := model.Timeline{
|
|
||||||
Name: "TAG: " + tag,
|
|
||||||
Statuses: nil,
|
|
||||||
}
|
|
||||||
|
|
||||||
return g.getTimeline(path, timeline)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *Client) getTimeline(path string, timeline model.Timeline) (model.Timeline, error) {
|
|
||||||
url := g.Authentication.Instance + path
|
|
||||||
|
|
||||||
var statuses []model.Status
|
|
||||||
|
|
||||||
if err := g.sendRequest(http.MethodGet, url, nil, &statuses); err != nil {
|
|
||||||
return timeline, fmt.Errorf("received an error after sending the request to get the timeline; %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
timeline.Statuses = statuses
|
|
||||||
|
|
||||||
return timeline, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *Client) sendRequest(method string, url string, requestBody io.Reader, object any) error {
|
func (g *Client) sendRequest(method string, url string, requestBody io.Reader, object any) error {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), g.Timeout)
|
ctx, cancel := context.WithTimeout(context.Background(), g.Timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
|
@ -67,9 +67,9 @@ func (a Account) String() string {
|
||||||
%s
|
%s
|
||||||
|
|
||||||
%s
|
%s
|
||||||
%s %d
|
Followers: %d
|
||||||
%s %d
|
Following: %d
|
||||||
%s %d
|
Statuses: %d
|
||||||
|
|
||||||
%s
|
%s
|
||||||
%s
|
%s
|
||||||
|
@ -84,28 +84,28 @@ func (a Account) String() string {
|
||||||
for _, field := range a.Fields {
|
for _, field := range a.Fields {
|
||||||
metadata += fmt.Sprintf(
|
metadata += fmt.Sprintf(
|
||||||
"\n %s: %s",
|
"\n %s: %s",
|
||||||
utilities.FieldFormat(field.Name),
|
field.Name,
|
||||||
utilities.StripHTMLTags(field.Value),
|
utilities.StripHTMLTags(field.Value),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Sprintf(
|
return fmt.Sprintf(
|
||||||
format,
|
format,
|
||||||
utilities.DisplayNameFormat(a.DisplayName),
|
a.DisplayName,
|
||||||
a.Username,
|
a.Username,
|
||||||
utilities.HeaderFormat("ACCOUNT ID:"),
|
utilities.Header("ACCOUNT ID:"),
|
||||||
a.ID,
|
a.ID,
|
||||||
utilities.HeaderFormat("JOINED ON:"),
|
utilities.Header("JOINED ON:"),
|
||||||
utilities.FormatDate(a.CreatedAt),
|
utilities.FormatDate(a.CreatedAt),
|
||||||
utilities.HeaderFormat("STATS:"),
|
utilities.Header("STATS:"),
|
||||||
utilities.FieldFormat("Followers:"), a.FollowersCount,
|
a.FollowersCount,
|
||||||
utilities.FieldFormat("Following:"), a.FollowingCount,
|
a.FollowingCount,
|
||||||
utilities.FieldFormat("Statuses:"), a.StatusCount,
|
a.StatusCount,
|
||||||
utilities.HeaderFormat("BIOGRAPHY:"),
|
utilities.Header("BIOGRAPHY:"),
|
||||||
utilities.WrapLine(utilities.StripHTMLTags(a.Note), "\n ", 80),
|
utilities.WrapLine(utilities.StripHTMLTags(a.Note), "\n ", 80),
|
||||||
utilities.HeaderFormat("METADATA:"),
|
utilities.Header("METADATA:"),
|
||||||
metadata,
|
metadata,
|
||||||
utilities.HeaderFormat("ACCOUNT URL:"),
|
utilities.Header("ACCOUNT URL:"),
|
||||||
a.URL,
|
a.URL,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -123,26 +123,23 @@ func (i InstanceV2) String() string {
|
||||||
Running GoToSocial %s
|
Running GoToSocial %s
|
||||||
|
|
||||||
%s
|
%s
|
||||||
%s %s
|
name: %s
|
||||||
%s %s
|
username: %s
|
||||||
%s %s
|
email: %s
|
||||||
`
|
`
|
||||||
|
|
||||||
return fmt.Sprintf(
|
return fmt.Sprintf(
|
||||||
format,
|
format,
|
||||||
utilities.HeaderFormat("INSTANCE:"),
|
utilities.Header("INSTANCE:"),
|
||||||
i.Title,
|
i.Title,
|
||||||
i.Description,
|
i.Description,
|
||||||
utilities.HeaderFormat("DOMAIN:"),
|
utilities.Header("DOMAIN:"),
|
||||||
i.Domain,
|
i.Domain,
|
||||||
utilities.HeaderFormat("VERSION:"),
|
utilities.Header("VERSION:"),
|
||||||
i.Version,
|
i.Version,
|
||||||
utilities.HeaderFormat("CONTACT:"),
|
utilities.Header("CONTACT:"),
|
||||||
utilities.FieldFormat("Name:"),
|
i.Contact.Account.DisplayName,
|
||||||
utilities.DisplayNameFormat(i.Contact.Account.DisplayName),
|
|
||||||
utilities.FieldFormat("Username:"),
|
|
||||||
i.Contact.Account.Username,
|
i.Contact.Account.Username,
|
||||||
utilities.FieldFormat("Email:"),
|
|
||||||
i.Contact.Email,
|
i.Contact.Email,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -179,21 +179,21 @@ func (s Status) String() string {
|
||||||
|
|
||||||
return fmt.Sprintf(
|
return fmt.Sprintf(
|
||||||
format,
|
format,
|
||||||
utilities.DisplayNameFormat(s.Account.DisplayName),
|
s.Account.DisplayName,
|
||||||
s.Account.Username,
|
s.Account.Username,
|
||||||
utilities.HeaderFormat("CONTENT:"),
|
utilities.Header("CONTENT:"),
|
||||||
s.Text,
|
utilities.StripHTMLTags(s.Content),
|
||||||
utilities.HeaderFormat("STATUS ID:"),
|
utilities.Header("STATUS ID:"),
|
||||||
s.ID,
|
s.ID,
|
||||||
utilities.HeaderFormat("CREATED AT:"),
|
utilities.Header("CREATED AT:"),
|
||||||
utilities.FormatTime(s.CreatedAt),
|
utilities.FormatTime(s.CreatedAt),
|
||||||
utilities.HeaderFormat("STATS:"),
|
utilities.Header("STATS:"),
|
||||||
s.RebloggsCount,
|
s.RebloggsCount,
|
||||||
s.FavouritesCount,
|
s.FavouritesCount,
|
||||||
s.RepliesCount,
|
s.RepliesCount,
|
||||||
utilities.HeaderFormat("VISIBILITY:"),
|
utilities.Header("VISIBILITY:"),
|
||||||
s.Visibility,
|
s.Visibility,
|
||||||
utilities.HeaderFormat("URL:"),
|
utilities.Header("URL:"),
|
||||||
s.URL,
|
s.URL,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,29 +0,0 @@
|
||||||
package model
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"codeflow.dananglin.me.uk/apollo/enbas/internal/utilities"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Timeline struct {
|
|
||||||
Name string
|
|
||||||
Statuses []Status
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t Timeline) String() string {
|
|
||||||
var builder strings.Builder
|
|
||||||
|
|
||||||
separator := "────────────────────────────────────────────────────────────────────────────────"
|
|
||||||
|
|
||||||
builder.WriteString(t.Name + "\n" + separator + "\n")
|
|
||||||
|
|
||||||
for _, status := range t.Statuses {
|
|
||||||
builder.WriteString(utilities.DisplayNameFormat(status.Account.DisplayName) + " (@" + status.Account.Username + ")\n\n")
|
|
||||||
builder.WriteString(utilities.WrapLine(status.Text, "\n", 80) + "\n\n")
|
|
||||||
builder.WriteString(utilities.FieldFormat("ID:") + " " + status.ID + "\t" + utilities.FieldFormat("Created at:") + " " + utilities.FormatTime(status.CreatedAt) + "\n")
|
|
||||||
builder.WriteString(separator + "\n")
|
|
||||||
}
|
|
||||||
|
|
||||||
return builder.String()
|
|
||||||
}
|
|
|
@ -1,7 +1,6 @@
|
||||||
package utilities
|
package utilities
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"regexp"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
@ -12,8 +11,6 @@ import (
|
||||||
const (
|
const (
|
||||||
reset = "\033[0m"
|
reset = "\033[0m"
|
||||||
boldblue = "\033[34;1m"
|
boldblue = "\033[34;1m"
|
||||||
boldmagenta = "\033[35;1m"
|
|
||||||
green = "\033[32m"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func StripHTMLTags(text string) string {
|
func StripHTMLTags(text string) string {
|
||||||
|
@ -55,21 +52,10 @@ func WrapLine(line, separator string, charLimit int) string {
|
||||||
return builder.String()
|
return builder.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func HeaderFormat(text string) string {
|
func Header(text string) string {
|
||||||
return boldblue + text + reset
|
return boldblue + text + reset
|
||||||
}
|
}
|
||||||
|
|
||||||
func FieldFormat(text string) string {
|
|
||||||
return green + text + reset
|
|
||||||
}
|
|
||||||
|
|
||||||
func DisplayNameFormat(text string) string {
|
|
||||||
// use this pattern to remove all emoji strings
|
|
||||||
pattern := regexp.MustCompile(`\s:[A-Za-z0-9]*:`)
|
|
||||||
|
|
||||||
return boldmagenta + pattern.ReplaceAllString(text, "") + reset
|
|
||||||
}
|
|
||||||
|
|
||||||
func FormatDate(date time.Time) string {
|
func FormatDate(date time.Time) string {
|
||||||
return date.Local().Format("02 Jan 2006")
|
return date.Local().Format("02 Jan 2006")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue