2024-06-02 07:53:13 +01:00
// SPDX-FileCopyrightText: 2024 Dan Anglin <d.n.i.anglin@gmail.com>
//
// SPDX-License-Identifier: GPL-3.0-or-later
2024-05-23 18:06:49 +01:00
package executor
import (
"flag"
"fmt"
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
2024-06-24 15:02:43 +01:00
"codeflow.dananglin.me.uk/apollo/enbas/internal/config"
2024-05-23 18:06:49 +01:00
"codeflow.dananglin.me.uk/apollo/enbas/internal/model"
2024-06-17 18:59:20 +01:00
"codeflow.dananglin.me.uk/apollo/enbas/internal/printer"
2024-05-23 18:06:49 +01:00
)
type EditExecutor struct {
* flag . FlagSet
2024-06-17 18:59:20 +01:00
printer * printer . Printer
2024-06-24 15:02:43 +01:00
config * config . Config
2024-05-23 18:06:49 +01:00
resourceType string
listID string
listTitle string
listRepliesPolicy string
}
2024-06-24 15:02:43 +01:00
func NewEditExecutor ( printer * printer . Printer , config * config . Config , name , summary string ) * EditExecutor {
2024-05-23 18:06:49 +01:00
editExe := EditExecutor {
2024-06-17 18:59:20 +01:00
FlagSet : flag . NewFlagSet ( name , flag . ExitOnError ) ,
2024-06-24 15:02:43 +01:00
printer : printer ,
config : config ,
2024-05-23 18:06:49 +01:00
}
2024-06-09 15:58:10 +01:00
editExe . StringVar ( & editExe . resourceType , flagType , "" , "Specify the type of resource to update" )
editExe . StringVar ( & editExe . listID , flagListID , "" , "Specify the ID of the list to update" )
editExe . StringVar ( & editExe . listTitle , flagListTitle , "" , "Specify the title of the list" )
editExe . StringVar ( & editExe . listRepliesPolicy , flagListRepliesPolicy , "" , "Specify the policy of the replies for this list (valid values are followed, list and none)" )
2024-05-23 18:06:49 +01:00
editExe . Usage = commandUsageFunc ( name , summary , editExe . FlagSet )
return & editExe
}
func ( e * EditExecutor ) Execute ( ) error {
if e . resourceType == "" {
return FlagNotSetError { flagText : flagType }
}
funcMap := map [ string ] func ( * client . Client ) error {
2024-05-31 21:18:11 +01:00
resourceList : e . editList ,
2024-05-23 18:06:49 +01:00
}
doFunc , ok := funcMap [ e . resourceType ]
if ! ok {
return UnsupportedTypeError { resourceType : e . resourceType }
}
2024-06-24 15:02:43 +01:00
gtsClient , err := client . NewClientFromFile ( e . 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 )
}
2024-05-31 21:18:11 +01:00
func ( e * EditExecutor ) editList ( gtsClient * client . Client ) error {
2024-05-23 18:06:49 +01:00
if e . listID == "" {
return FlagNotSetError { flagText : flagListID }
}
list , err := gtsClient . GetList ( e . listID )
if err != nil {
2024-06-02 11:35:43 +01:00
return fmt . Errorf ( "unable to get the list: %w" , err )
2024-05-23 18:06:49 +01:00
}
if e . listTitle != "" {
list . Title = e . listTitle
}
if e . listRepliesPolicy != "" {
2024-06-02 11:35:43 +01:00
parsedListRepliesPolicy , err := model . ParseListRepliesPolicy ( e . listRepliesPolicy )
if err != nil {
return err
2024-05-23 18:06:49 +01:00
}
2024-05-30 19:42:11 +01:00
list . RepliesPolicy = parsedListRepliesPolicy
2024-05-23 18:06:49 +01:00
}
updatedList , err := gtsClient . UpdateList ( list )
if err != nil {
2024-06-02 11:35:43 +01:00
return fmt . Errorf ( "unable to update the list: %w" , err )
2024-05-23 18:06:49 +01:00
}
2024-06-17 18:59:20 +01:00
e . printer . PrintSuccess ( "Successfully updated the list." )
e . printer . PrintList ( updatedList )
2024-05-23 18:06:49 +01:00
return nil
}