/ internal / executor / unfollow.go
unfollow.go
 1  package executor
 2  
 3  import (
 4  	"fmt"
 5  	"net/rpc"
 6  	"strings"
 7  
 8  	"codeflow.dananglin.me.uk/apollo/enbas/internal/printer"
 9  	"codeflow.dananglin.me.uk/apollo/enbas/internal/server"
10  )
11  
12  func (f *UnfollowExecutor) Execute() error {
13  	funcMap := map[string]func(*rpc.Client) error{
14  		resourceAccount: f.unfollowAccount,
15  		resourceTag:     f.unfollowTag,
16  	}
17  
18  	doFunc, ok := funcMap[f.resourceType]
19  	if !ok {
20  		return UnsupportedTypeError{resourceType: f.resourceType}
21  	}
22  
23  	client, err := server.Connect(f.config.Server, f.configDir)
24  	if err != nil {
25  		return fmt.Errorf("error creating the client for the daemon process: %w", err)
26  	}
27  	defer client.Close()
28  
29  	return doFunc(client)
30  }
31  
32  func (f *UnfollowExecutor) unfollowAccount(client *rpc.Client) error {
33  	accountID, err := getAccountID(client, false, f.accountName)
34  	if err != nil {
35  		return fmt.Errorf("received an error while getting the account ID: %w", err)
36  	}
37  
38  	if err := client.Call("GTSClient.UnfollowAccount", accountID, nil); err != nil {
39  		return fmt.Errorf("unable to unfollow the account: %w", err)
40  	}
41  
42  	printer.PrintSuccess(f.printSettings, "Successfully unfollowed the account.")
43  
44  	return nil
45  }
46  
47  func (f *UnfollowExecutor) unfollowTag(client *rpc.Client) error {
48  	if f.tag == "" {
49  		return Error{"please provide the name of the tag"}
50  	}
51  
52  	tag := strings.TrimLeft(f.tag, "#")
53  
54  	if err := client.Call("GTSClient.UnfollowTag", tag, nil); err != nil {
55  		return fmt.Errorf("unable to unfollow the tag: %w", err)
56  	}
57  
58  	printer.PrintSuccess(f.printSettings, "Successfully unfollowed '"+tag+"'.")
59  
60  	return nil
61  }