refactor: implement the Stringer interface
Account, Instance and Status now implement the Stringer interface.
This commit is contained in:
parent
4f561f9305
commit
e6ec5c71b6
5 changed files with 191 additions and 158 deletions
|
@ -4,76 +4,11 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
"unicode"
|
|
||||||
|
|
||||||
"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"
|
||||||
"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
|
|
||||||
`
|
|
||||||
|
|
||||||
var statusDetailsFormat = `
|
|
||||||
%s (@%s)
|
|
||||||
|
|
||||||
CONTENT:
|
|
||||||
%s
|
|
||||||
|
|
||||||
STATUS ID:
|
|
||||||
%s
|
|
||||||
|
|
||||||
CREATED AT:
|
|
||||||
%s
|
|
||||||
|
|
||||||
STATS:
|
|
||||||
Boosts: %d
|
|
||||||
Likes: %d
|
|
||||||
Replies: %d
|
|
||||||
|
|
||||||
VISIBILITY:
|
|
||||||
%s
|
|
||||||
|
|
||||||
URL:
|
|
||||||
%s
|
|
||||||
`
|
|
||||||
|
|
||||||
type showCommand struct {
|
type showCommand struct {
|
||||||
*flag.FlagSet
|
*flag.FlagSet
|
||||||
myAccount bool
|
myAccount bool
|
||||||
|
@ -126,16 +61,7 @@ func (c *showCommand) showInstance(gts *client.Client) error {
|
||||||
return fmt.Errorf("unable to retrieve the instance details; %w", err)
|
return fmt.Errorf("unable to retrieve the instance details; %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf(
|
fmt.Println(instance)
|
||||||
instanceDetailsFormat,
|
|
||||||
instance.Title,
|
|
||||||
instance.Description,
|
|
||||||
instance.Domain,
|
|
||||||
instance.Version,
|
|
||||||
instance.Contact.Account.DisplayName,
|
|
||||||
instance.Contact.Account.Username,
|
|
||||||
instance.Contact.Email,
|
|
||||||
)
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -163,25 +89,7 @@ func (c *showCommand) showAccount(gts *client.Client) error {
|
||||||
return fmt.Errorf("unable to retrieve the account details; %w", err)
|
return fmt.Errorf("unable to retrieve the account details; %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
metadata := ""
|
fmt.Println(account)
|
||||||
|
|
||||||
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,
|
|
||||||
)
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -196,58 +104,7 @@ func (c *showCommand) showStatus(gts *client.Client) error {
|
||||||
return fmt.Errorf("unable to retrieve the status; %w", err)
|
return fmt.Errorf("unable to retrieve the status; %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf(
|
fmt.Println(status)
|
||||||
statusDetailsFormat,
|
|
||||||
status.Account.DisplayName,
|
|
||||||
status.Account.Username,
|
|
||||||
stripHTMLTags(status.Content),
|
|
||||||
status.ID,
|
|
||||||
status.CreatedAt.Format("02 Jan 2006, 03:04"),
|
|
||||||
status.RebloggsCount,
|
|
||||||
status.FavouritesCount,
|
|
||||||
status.RepliesCount,
|
|
||||||
status.Visibility,
|
|
||||||
status.URL,
|
|
||||||
)
|
|
||||||
|
|
||||||
return nil
|
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 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()
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,32 +1,37 @@
|
||||||
package model
|
package model
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/utilities"
|
||||||
|
)
|
||||||
|
|
||||||
type Account struct {
|
type Account struct {
|
||||||
Acct string `json:"acct"`
|
Acct string `json:"acct"`
|
||||||
Avatar string `json:"avatar"`
|
Avatar string `json:"avatar"`
|
||||||
AvatarStatic string `json:"avatar_static"`
|
AvatarStatic string `json:"avatar_static"`
|
||||||
Bot bool `json:"bot"`
|
|
||||||
CreatedAt time.Time `json:"created_at"`
|
|
||||||
CustomCSS string `json:"custom_css"`
|
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"`
|
Header string `json:"header"`
|
||||||
HeaderStatic string `json:"header_static"`
|
HeaderStatic string `json:"header_static"`
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
LastStatusAt string `json:"last_status_at"`
|
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"`
|
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"`
|
MuteExpiresAt time.Time `json:"mute_expires_at"`
|
||||||
Note string `json:"note"`
|
Note string `json:"note"`
|
||||||
Role AccountRole `json:"role"`
|
Role AccountRole `json:"role"`
|
||||||
Source Source `json:"source"`
|
Source Source `json:"source"`
|
||||||
StatusCount int `json:"statuses_count"`
|
StatusCount int `json:"statuses_count"`
|
||||||
Suspended bool `json:"suspended"`
|
|
||||||
URL string `json:"url"`
|
URL string `json:"url"`
|
||||||
Username string `json:"username"`
|
Username string `json:"username"`
|
||||||
}
|
}
|
||||||
|
@ -50,3 +55,51 @@ type Field struct {
|
||||||
Value string `json:"value"`
|
Value string `json:"value"`
|
||||||
VerifiedAt string `json:"verified_at"`
|
VerifiedAt string `json:"verified_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a Account) String() string {
|
||||||
|
format := `
|
||||||
|
%s (@%s)
|
||||||
|
|
||||||
|
ACCOUNT ID:
|
||||||
|
%s
|
||||||
|
|
||||||
|
JOINED ON:
|
||||||
|
%s
|
||||||
|
|
||||||
|
STATS:
|
||||||
|
Followers: %d
|
||||||
|
Following: %d
|
||||||
|
Statuses: %d
|
||||||
|
|
||||||
|
BIOGRAPHY:
|
||||||
|
%s
|
||||||
|
|
||||||
|
METADATA: %s
|
||||||
|
|
||||||
|
ACCOUNT URL:
|
||||||
|
%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,
|
||||||
|
a.ID,
|
||||||
|
a.CreatedAt.Format("02 Jan 2006"),
|
||||||
|
a.FollowersCount,
|
||||||
|
a.FollowingCount,
|
||||||
|
a.StatusCount,
|
||||||
|
utilities.WrapLine(utilities.StripHTMLTags(a.Note), "\n ", 80),
|
||||||
|
metadata,
|
||||||
|
a.URL,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package model
|
package model
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
type InstanceV2 struct {
|
type InstanceV2 struct {
|
||||||
AccountDomain string `json:"account_domain"`
|
AccountDomain string `json:"account_domain"`
|
||||||
Configuration InstanceConfiguration `json:"configuration"`
|
Configuration InstanceConfiguration `json:"configuration"`
|
||||||
|
@ -104,3 +106,32 @@ type InstanceV2Usage struct {
|
||||||
type InstanceV2Users struct {
|
type InstanceV2Users struct {
|
||||||
ActiveMonth int `json:"active_month"`
|
ActiveMonth int `json:"active_month"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (i InstanceV2) String() string {
|
||||||
|
format := `
|
||||||
|
INSTANCE:
|
||||||
|
%s - %s
|
||||||
|
|
||||||
|
DOMAIN:
|
||||||
|
%s
|
||||||
|
|
||||||
|
VERSION:
|
||||||
|
Running GoToSocial %s
|
||||||
|
|
||||||
|
CONTACT:
|
||||||
|
name: %s
|
||||||
|
username: %s
|
||||||
|
email: %s
|
||||||
|
`
|
||||||
|
|
||||||
|
return fmt.Sprintf(
|
||||||
|
format,
|
||||||
|
i.Title,
|
||||||
|
i.Description,
|
||||||
|
i.Domain,
|
||||||
|
i.Version,
|
||||||
|
i.Contact.Account.DisplayName,
|
||||||
|
i.Contact.Account.Username,
|
||||||
|
i.Contact.Email,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,11 @@
|
||||||
package model
|
package model
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/utilities"
|
||||||
|
)
|
||||||
|
|
||||||
type Status struct {
|
type Status struct {
|
||||||
Account Account `json:"account"`
|
Account Account `json:"account"`
|
||||||
|
@ -146,3 +151,43 @@ type MediaDimensions struct {
|
||||||
Height int `json:"height"`
|
Height int `json:"height"`
|
||||||
Width int `json:"width"`
|
Width int `json:"width"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s Status) String() string {
|
||||||
|
format := `
|
||||||
|
%s (@%s)
|
||||||
|
|
||||||
|
CONTENT:
|
||||||
|
%s
|
||||||
|
|
||||||
|
STATUS ID:
|
||||||
|
%s
|
||||||
|
|
||||||
|
CREATED AT:
|
||||||
|
%s
|
||||||
|
|
||||||
|
STATS:
|
||||||
|
Boosts: %d
|
||||||
|
Likes: %d
|
||||||
|
Replies: %d
|
||||||
|
|
||||||
|
VISIBILITY:
|
||||||
|
%s
|
||||||
|
|
||||||
|
URL:
|
||||||
|
%s
|
||||||
|
`
|
||||||
|
|
||||||
|
return fmt.Sprintf(
|
||||||
|
format,
|
||||||
|
s.Account.DisplayName,
|
||||||
|
s.Account.Username,
|
||||||
|
utilities.StripHTMLTags(s.Content),
|
||||||
|
s.ID,
|
||||||
|
s.CreatedAt.Format("02 Jan 2006, 03:04"),
|
||||||
|
s.RebloggsCount,
|
||||||
|
s.FavouritesCount,
|
||||||
|
s.RepliesCount,
|
||||||
|
s.Visibility,
|
||||||
|
s.URL,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
47
internal/utilities/utilities.go
Normal file
47
internal/utilities/utilities.go
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
package utilities
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"unicode"
|
||||||
|
|
||||||
|
"golang.org/x/net/html"
|
||||||
|
)
|
||||||
|
|
||||||
|
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()
|
||||||
|
}
|
Loading…
Reference in a new issue