refactor: drop boilerplate
All checks were successful
continuous-integration/drone/push Build is passing

Closes #2
This commit is contained in:
decentral1se 2024-08-04 10:09:58 +02:00
parent 5012fc4a3f
commit a25279a835
Signed by: decentral1se
GPG Key ID: 03789458B3D0C410
2 changed files with 15 additions and 52 deletions

BIN
blurp

Binary file not shown.

View File

@ -36,18 +36,21 @@ func getAccount(authClient *auth.Client) (*models.Account, error) {
return resp.GetPayload(), nil
}
// main is the command-line entrypoint.
func main() {
if err := rootCmd.Execute(); err != nil {
slog.Error("woops, something went wrong", "error", err)
}
}
// rootCmd is the root command.
var rootCmd = &cobra.Command{
Use: "blurp",
Short: "A GoToSocial status deletion tool",
}
var authLoginCmd = &cobra.Command{
// loginCmd is the login command.
var loginCmd = &cobra.Command{
Use: "login",
Short: "Log in",
RunE: func(cmd *cobra.Command, args []string) error {
@ -55,9 +58,10 @@ var authLoginCmd = &cobra.Command{
},
}
// archiveCmd is the archive command.
var archiveCmd = &cobra.Command{
Use: "archive",
Short: "Archive statuses",
Short: "Archive all account statuses (favourites, boosted, etc.)",
RunE: func(cmd *cobra.Command, args []string) error {
authClient, err := auth.NewAuthClient(user)
if err != nil {
@ -69,8 +73,7 @@ var archiveCmd = &cobra.Command{
slog.Error("unable to retrieve account", "error", err)
}
pagedRequester := &statusPagedRequester{accID: acc.ID}
statuses, err := ReadAllPaged(authClient, pagedRequester)
statuses, err := ReadAllPaged(authClient, acc.ID)
if err != nil {
slog.Error("unable to download paged response", "error", err)
}
@ -150,15 +153,6 @@ func httpGetFile(filepath, url string) error {
return nil
}
type PagedRequester[Response PagedResponse[Element], Element any] interface {
Request(authClient *auth.Client, maxID *string) (Response, error)
}
type PagedResponse[Element any] interface {
Link() string
Elements() []Element
}
// ParseLinkMaxID extracts the `max_id` from the `next` link for paging to older items.
func ParseLinkMaxID(linkHeader string) (*string, error) {
next := link.Parse(linkHeader)["next"]
@ -177,11 +171,8 @@ func ParseLinkMaxID(linkHeader string) (*string, error) {
return &nextMaxID, err
}
func ReadAllPaged[
Requester PagedRequester[Response, Element],
Response PagedResponse[Element], Element any](
authClient *auth.Client, pagedRequester Requester) ([]Element, error) {
var all []Element
func ReadAllPaged(authClient *auth.Client, accID string) ([]*models.Status, error) {
var all []*models.Status
var maxID *string
for {
@ -190,13 +181,14 @@ func ReadAllPaged[
return all, errors.WithStack(err)
}
pagedResponse, err := pagedRequester.Request(authClient, maxID)
params := &accounts.AccountStatusesParams{ID: accID, MaxID: maxID}
resp, err := authClient.Client.Accounts.AccountStatuses(params, authClient.Auth)
if err != nil {
slog.Error("error fetching page", "error", err)
return all, errors.WithStack(err)
}
maxID, err = ParseLinkMaxID(pagedResponse.Link())
maxID, err = ParseLinkMaxID(resp.Link)
if err != nil {
slog.Error("error parsing Link header", "error", err)
return all, errors.WithStack(err)
@ -206,40 +198,12 @@ func ReadAllPaged[
break
}
all = append(all, pagedResponse.Elements()...)
all = append(all, resp.GetPayload()...)
}
return all, nil
}
type statusPagedRequester struct {
accID string
}
type statusPagedResponse struct {
resp *accounts.AccountStatusesOK
}
func (pagedResponse *statusPagedResponse) Link() string {
return pagedResponse.resp.Link
}
func (pagedResponse *statusPagedResponse) Elements() []*models.Status {
return pagedResponse.resp.GetPayload()
}
func (pagedRequester *statusPagedRequester) Request(
authClient *auth.Client, maxID *string) (*statusPagedResponse, error) {
resp, err := authClient.Client.Accounts.AccountStatuses(&accounts.AccountStatusesParams{
ID: pagedRequester.accID,
MaxID: maxID,
}, authClient.Auth)
if err != nil {
return nil, err
}
return &statusPagedResponse{resp}, nil
}
var deleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete statuses like tears in the rain",
@ -254,8 +218,7 @@ var deleteCmd = &cobra.Command{
slog.Error("unable to retrieve account", err)
}
pagedRequester := &statusPagedRequester{accID: acc.ID}
allStatuses, err := ReadAllPaged(authClient, pagedRequester)
allStatuses, err := ReadAllPaged(authClient, acc.ID)
if err != nil {
slog.Error("unable to download paged response", err)
}
@ -292,7 +255,7 @@ func init() {
&user, "user", "u", "", "username@domain of account",
)
rootCmd.AddCommand(authLoginCmd)
rootCmd.AddCommand(loginCmd)
rootCmd.AddCommand(archiveCmd)
rootCmd.AddCommand(deleteCmd)
}