Compare commits
3 commits
2c5123253a
...
427ad5daf5
Author | SHA1 | Date | |
---|---|---|---|
427ad5daf5 | |||
e6ec5c71b6 | |||
4f561f9305 |
6 changed files with 254 additions and 123 deletions
|
@ -4,69 +4,32 @@ import (
|
|||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
|
||||
"codeflow.dananglin.me.uk/apollo/enbas/internal/config"
|
||||
"golang.org/x/net/html"
|
||||
)
|
||||
|
||||
var instanceDetailsFormat = `INSTANCE:
|
||||
%s - %s
|
||||
|
||||
DOMAIN:
|
||||
%s
|
||||
|
||||
VERSION:
|
||||
Running GoToSocial %s
|
||||
|
||||
CONTACT:
|
||||
name: %s
|
||||
username: %s
|
||||
email: %s
|
||||
`
|
||||
|
||||
var accountDetailsFormat = `
|
||||
%s (@%s)
|
||||
|
||||
ACCOUNT ID:
|
||||
%s
|
||||
|
||||
JOINED ON:
|
||||
%s
|
||||
|
||||
STATS:
|
||||
Followers: %d
|
||||
Following: %d
|
||||
Statuses: %d
|
||||
|
||||
BIOGRAPHY:
|
||||
%s
|
||||
|
||||
METADATA: %s
|
||||
|
||||
ACCOUNT URL:
|
||||
%s
|
||||
`
|
||||
|
||||
type showCommand struct {
|
||||
*flag.FlagSet
|
||||
myAccount bool
|
||||
targetType string
|
||||
account string
|
||||
myAccount bool
|
||||
statusID string
|
||||
}
|
||||
|
||||
func newShowCommand(name, summary string) *showCommand {
|
||||
command := showCommand{
|
||||
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
||||
targetType: "",
|
||||
myAccount: false,
|
||||
targetType: "",
|
||||
account: "",
|
||||
statusID: "",
|
||||
}
|
||||
|
||||
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.account, "account", "", "specify the account URI to lookup")
|
||||
command.BoolVar(&command.myAccount, "my-account", false, "set to true to lookup your account")
|
||||
command.StringVar(&command.statusID, "status-id", "", "specify the ID of the status to display")
|
||||
command.Usage = commandUsageFunc(name, summary, command.FlagSet)
|
||||
|
||||
return &command
|
||||
|
@ -81,6 +44,7 @@ func (c *showCommand) Execute() error {
|
|||
funcMap := map[string]func(*client.Client) error{
|
||||
"instance": c.showInstance,
|
||||
"account": c.showAccount,
|
||||
"status": c.showStatus,
|
||||
}
|
||||
|
||||
doFunc, ok := funcMap[c.targetType]
|
||||
|
@ -97,16 +61,7 @@ func (c *showCommand) showInstance(gts *client.Client) error {
|
|||
return fmt.Errorf("unable to retrieve the instance details; %w", err)
|
||||
}
|
||||
|
||||
fmt.Printf(
|
||||
instanceDetailsFormat,
|
||||
instance.Title,
|
||||
instance.Description,
|
||||
instance.Domain,
|
||||
instance.Version,
|
||||
instance.Contact.Account.DisplayName,
|
||||
instance.Contact.Account.Username,
|
||||
instance.Contact.Email,
|
||||
)
|
||||
fmt.Println(instance)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -134,64 +89,22 @@ func (c *showCommand) showAccount(gts *client.Client) error {
|
|||
return fmt.Errorf("unable to retrieve the account details; %w", err)
|
||||
}
|
||||
|
||||
metadata := ""
|
||||
|
||||
for _, field := range account.Fields {
|
||||
metadata += fmt.Sprintf("\n %s: %s", field.Name, stripHTMLTags(field.Value))
|
||||
}
|
||||
|
||||
fmt.Printf(
|
||||
accountDetailsFormat,
|
||||
account.DisplayName,
|
||||
account.Username,
|
||||
account.ID,
|
||||
account.CreatedAt.Format("02 Jan 2006"),
|
||||
account.FollowersCount,
|
||||
account.FollowingCount,
|
||||
account.StatusCount,
|
||||
wrapLine(stripHTMLTags(account.Note), "\n ", 80),
|
||||
metadata,
|
||||
account.URL,
|
||||
)
|
||||
fmt.Println(account)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func stripHTMLTags(text string) string {
|
||||
token := html.NewTokenizer(strings.NewReader(text))
|
||||
|
||||
var builder strings.Builder
|
||||
|
||||
for {
|
||||
tt := token.Next()
|
||||
switch tt {
|
||||
case html.ErrorToken:
|
||||
return builder.String()
|
||||
case html.TextToken:
|
||||
builder.WriteString(token.Token().Data + " ")
|
||||
}
|
||||
}
|
||||
func (c *showCommand) showStatus(gts *client.Client) error {
|
||||
if c.statusID == "" {
|
||||
return errors.New("the status-id flag is not set")
|
||||
}
|
||||
|
||||
func wrapLine(line, separator string, charLimit int) string {
|
||||
if len(line) <= charLimit {
|
||||
return line
|
||||
status, err := gts.GetStatus(c.statusID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to retrieve the status; %w", err)
|
||||
}
|
||||
|
||||
leftcursor, rightcursor := 0, 0
|
||||
fmt.Println(status)
|
||||
|
||||
var builder strings.Builder
|
||||
|
||||
for rightcursor < (len(line) - charLimit) {
|
||||
rightcursor += charLimit
|
||||
for !unicode.IsSpace(rune(line[rightcursor-1])) {
|
||||
rightcursor--
|
||||
}
|
||||
builder.WriteString(line[leftcursor:rightcursor] + separator)
|
||||
leftcursor = rightcursor
|
||||
}
|
||||
|
||||
builder.WriteString(line[rightcursor:])
|
||||
|
||||
return builder.String()
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -71,8 +71,8 @@ func (g *Client) GetInstance() (model.InstanceV2, error) {
|
|||
}
|
||||
|
||||
func (g *Client) GetAccount(accountURI string) (model.Account, error) {
|
||||
path := "/api/v1/accounts/lookup"
|
||||
url := g.Authentication.Instance + path + "?acct=" + accountURI
|
||||
path := "/api/v1/accounts/lookup" + "?acct=" + accountURI
|
||||
url := g.Authentication.Instance + path
|
||||
|
||||
var account model.Account
|
||||
|
||||
|
@ -83,6 +83,19 @@ func (g *Client) GetAccount(accountURI string) (model.Account, error) {
|
|||
return account, nil
|
||||
}
|
||||
|
||||
func (g *Client) GetStatus(statusID string) (model.Status, error) {
|
||||
path := "/api/v1/statuses/" + statusID
|
||||
url := g.Authentication.Instance + path
|
||||
|
||||
var status model.Status
|
||||
|
||||
if err := g.sendRequest(http.MethodGet, url, nil, &status); err != nil {
|
||||
return model.Status{}, fmt.Errorf("received an error after sending the request to get the status information; %w", err)
|
||||
}
|
||||
|
||||
return status, nil
|
||||
}
|
||||
|
||||
func (g *Client) sendRequest(method string, url string, requestBody io.Reader, object any) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), g.Timeout)
|
||||
defer cancel()
|
||||
|
|
|
@ -1,32 +1,37 @@
|
|||
package model
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"codeflow.dananglin.me.uk/apollo/enbas/internal/utilities"
|
||||
)
|
||||
|
||||
type Account struct {
|
||||
Acct string `json:"acct"`
|
||||
Avatar string `json:"avatar"`
|
||||
AvatarStatic string `json:"avatar_static"`
|
||||
Bot bool `json:"bot"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
CustomCSS string `json:"custom_css"`
|
||||
Discoverable bool `json:"discoverable"`
|
||||
DisplayName string `json:"display_name"`
|
||||
Emojis []Emoji `json:"emojis"`
|
||||
EnableRSS bool `json:"enable_rss"`
|
||||
Fields []Field `json:"fields"`
|
||||
FollowersCount int `json:"followers_count"`
|
||||
FollowingCount int `json:"following_count"`
|
||||
Header string `json:"header"`
|
||||
HeaderStatic string `json:"header_static"`
|
||||
ID string `json:"id"`
|
||||
LastStatusAt string `json:"last_status_at"`
|
||||
DisplayName string `json:"display_name"`
|
||||
Emojis []Emoji `json:"emojis"`
|
||||
EnableRSS bool `json:"enable_rss"`
|
||||
Bot bool `json:"bot"`
|
||||
Locked bool `json:"locked"`
|
||||
Suspended bool `json:"suspended"`
|
||||
Discoverable bool `json:"discoverable"`
|
||||
Fields []Field `json:"fields"`
|
||||
FollowersCount int `json:"followers_count"`
|
||||
FollowingCount int `json:"following_count"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
MuteExpiresAt time.Time `json:"mute_expires_at"`
|
||||
Note string `json:"note"`
|
||||
Role AccountRole `json:"role"`
|
||||
Source Source `json:"source"`
|
||||
StatusCount int `json:"statuses_count"`
|
||||
Suspended bool `json:"suspended"`
|
||||
URL string `json:"url"`
|
||||
Username string `json:"username"`
|
||||
}
|
||||
|
@ -50,3 +55,57 @@ type Field struct {
|
|||
Value string `json:"value"`
|
||||
VerifiedAt string `json:"verified_at"`
|
||||
}
|
||||
|
||||
func (a Account) String() string {
|
||||
format := `
|
||||
%s (@%s)
|
||||
|
||||
%s
|
||||
%s
|
||||
|
||||
%s
|
||||
%s
|
||||
|
||||
%s
|
||||
Followers: %d
|
||||
Following: %d
|
||||
Statuses: %d
|
||||
|
||||
%s
|
||||
%s
|
||||
|
||||
%s %s
|
||||
|
||||
%s
|
||||
%s
|
||||
`
|
||||
metadata := ""
|
||||
|
||||
for _, field := range a.Fields {
|
||||
metadata += fmt.Sprintf(
|
||||
"\n %s: %s",
|
||||
field.Name,
|
||||
utilities.StripHTMLTags(field.Value),
|
||||
)
|
||||
}
|
||||
|
||||
return fmt.Sprintf(
|
||||
format,
|
||||
a.DisplayName,
|
||||
a.Username,
|
||||
utilities.Header("ACCOUNT ID:"),
|
||||
a.ID,
|
||||
utilities.Header("JOINED ON:"),
|
||||
a.CreatedAt.Format("02 Jan 2006"),
|
||||
utilities.Header("STATS:"),
|
||||
a.FollowersCount,
|
||||
a.FollowingCount,
|
||||
a.StatusCount,
|
||||
utilities.Header("BIOGRAPHY:"),
|
||||
utilities.WrapLine(utilities.StripHTMLTags(a.Note), "\n ", 80),
|
||||
utilities.Header("METADATA:"),
|
||||
metadata,
|
||||
utilities.Header("ACCOUNT URL:"),
|
||||
a.URL,
|
||||
)
|
||||
}
|
||||
|
|
|
@ -1,5 +1,11 @@
|
|||
package model
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"codeflow.dananglin.me.uk/apollo/enbas/internal/utilities"
|
||||
)
|
||||
|
||||
type InstanceV2 struct {
|
||||
AccountDomain string `json:"account_domain"`
|
||||
Configuration InstanceConfiguration `json:"configuration"`
|
||||
|
@ -104,3 +110,36 @@ type InstanceV2Usage struct {
|
|||
type InstanceV2Users struct {
|
||||
ActiveMonth int `json:"active_month"`
|
||||
}
|
||||
|
||||
func (i InstanceV2) String() string {
|
||||
format := `
|
||||
%s
|
||||
%s - %s
|
||||
|
||||
%s
|
||||
%s
|
||||
|
||||
%s
|
||||
Running GoToSocial %s
|
||||
|
||||
%s
|
||||
name: %s
|
||||
username: %s
|
||||
email: %s
|
||||
`
|
||||
|
||||
return fmt.Sprintf(
|
||||
format,
|
||||
utilities.Header("INSTANCE:"),
|
||||
i.Title,
|
||||
i.Description,
|
||||
utilities.Header("DOMAIN:"),
|
||||
i.Domain,
|
||||
utilities.Header("VERSION:"),
|
||||
i.Version,
|
||||
utilities.Header("CONTACT:"),
|
||||
i.Contact.Account.DisplayName,
|
||||
i.Contact.Account.Username,
|
||||
i.Contact.Email,
|
||||
)
|
||||
}
|
||||
|
|
|
@ -1,6 +1,11 @@
|
|||
package model
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"codeflow.dananglin.me.uk/apollo/enbas/internal/utilities"
|
||||
)
|
||||
|
||||
type Status struct {
|
||||
Account Account `json:"account"`
|
||||
|
@ -146,3 +151,49 @@ type MediaDimensions struct {
|
|||
Height int `json:"height"`
|
||||
Width int `json:"width"`
|
||||
}
|
||||
|
||||
func (s Status) String() string {
|
||||
format := `
|
||||
%s (@%s)
|
||||
|
||||
%s
|
||||
%s
|
||||
|
||||
%s
|
||||
%s
|
||||
|
||||
%s
|
||||
%s
|
||||
|
||||
%s
|
||||
Boosts: %d
|
||||
Likes: %d
|
||||
Replies: %d
|
||||
|
||||
%s
|
||||
%s
|
||||
|
||||
%s
|
||||
%s
|
||||
`
|
||||
|
||||
return fmt.Sprintf(
|
||||
format,
|
||||
s.Account.DisplayName,
|
||||
s.Account.Username,
|
||||
utilities.Header("CONTENT:"),
|
||||
utilities.StripHTMLTags(s.Content),
|
||||
utilities.Header("STATUS ID:"),
|
||||
s.ID,
|
||||
utilities.Header("CREATED AT:"),
|
||||
s.CreatedAt.Format("02 Jan 2006, 03:04"),
|
||||
utilities.Header("STATS:"),
|
||||
s.RebloggsCount,
|
||||
s.FavouritesCount,
|
||||
s.RepliesCount,
|
||||
utilities.Header("VISIBILITY:"),
|
||||
s.Visibility,
|
||||
utilities.Header("URL:"),
|
||||
s.URL,
|
||||
)
|
||||
}
|
||||
|
|
56
internal/utilities/utilities.go
Normal file
56
internal/utilities/utilities.go
Normal file
|
@ -0,0 +1,56 @@
|
|||
package utilities
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"golang.org/x/net/html"
|
||||
)
|
||||
|
||||
const (
|
||||
reset = "\033[0m"
|
||||
boldblue = "\033[34;1m"
|
||||
)
|
||||
|
||||
func StripHTMLTags(text string) string {
|
||||
token := html.NewTokenizer(strings.NewReader(text))
|
||||
|
||||
var builder strings.Builder
|
||||
|
||||
for {
|
||||
tt := token.Next()
|
||||
switch tt {
|
||||
case html.ErrorToken:
|
||||
return builder.String()
|
||||
case html.TextToken:
|
||||
builder.WriteString(token.Token().Data + " ")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func WrapLine(line, separator string, charLimit int) string {
|
||||
if len(line) <= charLimit {
|
||||
return line
|
||||
}
|
||||
|
||||
leftcursor, rightcursor := 0, 0
|
||||
|
||||
var builder strings.Builder
|
||||
|
||||
for rightcursor < (len(line) - charLimit) {
|
||||
rightcursor += charLimit
|
||||
for !unicode.IsSpace(rune(line[rightcursor-1])) {
|
||||
rightcursor--
|
||||
}
|
||||
builder.WriteString(line[leftcursor:rightcursor] + separator)
|
||||
leftcursor = rightcursor
|
||||
}
|
||||
|
||||
builder.WriteString(line[rightcursor:])
|
||||
|
||||
return builder.String()
|
||||
}
|
||||
|
||||
func Header(text string) string {
|
||||
return boldblue + text + reset
|
||||
}
|
Loading…
Reference in a new issue