2024-05-23 18:06:49 +01:00
|
|
|
package executor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-08-16 18:53:08 +01:00
|
|
|
"path/filepath"
|
2024-05-23 18:06:49 +01:00
|
|
|
|
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
|
2024-08-16 18:53:08 +01:00
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/utilities"
|
2024-05-23 18:06:49 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func (d *DeleteExecutor) Execute() error {
|
|
|
|
if d.resourceType == "" {
|
|
|
|
return FlagNotSetError{flagText: flagType}
|
|
|
|
}
|
|
|
|
|
|
|
|
funcMap := map[string]func(*client.Client) error{
|
2024-08-16 18:53:08 +01:00
|
|
|
resourceList: d.deleteList,
|
|
|
|
resourceStatus: d.deleteStatus,
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
doFunc, ok := funcMap[d.resourceType]
|
|
|
|
if !ok {
|
|
|
|
return UnsupportedTypeError{resourceType: d.resourceType}
|
|
|
|
}
|
|
|
|
|
2024-06-25 12:39:39 +01:00
|
|
|
gtsClient, err := client.NewClientFromFile(d.config.CredentialsFile)
|
2024-05-23 18:06:49 +01:00
|
|
|
if err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unable to create the GoToSocial client: %w", err)
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return doFunc(gtsClient)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DeleteExecutor) deleteList(gtsClient *client.Client) error {
|
|
|
|
if d.listID == "" {
|
2024-08-18 08:59:44 +01:00
|
|
|
return MissingIDError{
|
|
|
|
resource: resourceList,
|
|
|
|
action: "delete",
|
|
|
|
}
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := gtsClient.DeleteList(d.listID); err != nil {
|
2024-06-02 11:35:43 +01:00
|
|
|
return fmt.Errorf("unable to delete the list: %w", err)
|
2024-05-23 18:06:49 +01:00
|
|
|
}
|
|
|
|
|
2024-06-17 18:59:20 +01:00
|
|
|
d.printer.PrintSuccess("The list was successfully deleted.")
|
2024-05-23 18:06:49 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-08-16 18:53:08 +01:00
|
|
|
|
|
|
|
func (d *DeleteExecutor) deleteStatus(gtsClient *client.Client) error {
|
|
|
|
if d.statusID == "" {
|
2024-08-18 08:59:44 +01:00
|
|
|
return MissingIDError{
|
|
|
|
resource: resourceStatus,
|
|
|
|
action: "delete",
|
|
|
|
}
|
2024-08-16 18:53:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
status, err := gtsClient.GetStatus(d.statusID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to get the status: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
myAccountID, err := getAccountID(gtsClient, true, nil)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to get your account ID: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if status.Account.ID != myAccountID {
|
2024-08-17 11:23:21 +01:00
|
|
|
return Error{"unable to delete the status because the status does not belong to you"}
|
2024-08-16 18:53:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
text, err := gtsClient.DeleteStatus(d.statusID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to delete the status: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.printer.PrintSuccess("The status was successfully deleted.")
|
|
|
|
|
|
|
|
if d.saveText {
|
2024-08-17 21:56:54 +01:00
|
|
|
cacheDir, err := utilities.CalculateStatusesCacheDir(d.config.CacheDirectory, gtsClient.Authentication.Instance)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to get the cache directory for the status: %w", err)
|
|
|
|
}
|
2024-08-16 18:53:08 +01:00
|
|
|
|
|
|
|
if err := utilities.EnsureDirectory(cacheDir); err != nil {
|
|
|
|
return fmt.Errorf("unable to ensure the existence of the directory %q: %w", cacheDir, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
path := filepath.Join(cacheDir, fmt.Sprintf("deleted-status-%s.txt", d.statusID))
|
|
|
|
|
|
|
|
if err := utilities.SaveTextToFile(path, text); err != nil {
|
|
|
|
return fmt.Errorf("unable to save the text to %q: %w", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.printer.PrintSuccess("The text was successfully saved to '" + path + "'.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|