2024-05-23 18:06:49 +01:00
package executor
2024-02-27 19:52:59 +00:00
import (
"flag"
"fmt"
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
"codeflow.dananglin.me.uk/apollo/enbas/internal/model"
)
2024-05-23 18:06:49 +01:00
type CreateExecutor struct {
2024-02-27 19:52:59 +00:00
* flag . FlagSet
2024-05-23 18:06:49 +01:00
topLevelFlags TopLevelFlags
2024-02-27 19:52:59 +00:00
resourceType string
listTitle string
listRepliesPolicy string
}
2024-05-23 18:06:49 +01:00
func NewCreateExecutor ( tlf TopLevelFlags , name , summary string ) * CreateExecutor {
createExe := CreateExecutor {
2024-02-27 19:52:59 +00:00
FlagSet : flag . NewFlagSet ( name , flag . ExitOnError ) ,
2024-05-22 23:30:09 +01:00
topLevelFlags : tlf ,
2024-02-27 19:52:59 +00:00
}
2024-05-23 18:06:49 +01:00
createExe . StringVar ( & createExe . resourceType , flagType , "" , "specify the type of resource to create" )
createExe . StringVar ( & createExe . listTitle , flagListTitle , "" , "specify the title of the list" )
createExe . StringVar ( & createExe . listRepliesPolicy , flagListRepliesPolicy , "list" , "specify the policy of the replies for this list (valid values are followed, list and none)" )
2024-02-27 19:52:59 +00:00
2024-05-23 18:06:49 +01:00
createExe . Usage = commandUsageFunc ( name , summary , createExe . FlagSet )
2024-02-27 19:52:59 +00:00
2024-05-23 18:06:49 +01:00
return & createExe
2024-02-27 19:52:59 +00:00
}
2024-05-23 18:06:49 +01:00
func ( c * CreateExecutor ) Execute ( ) error {
2024-02-27 19:52:59 +00:00
if c . resourceType == "" {
2024-05-23 18:06:49 +01:00
return FlagNotSetError { flagText : flagType }
2024-02-27 19:52:59 +00:00
}
2024-05-23 18:06:49 +01:00
gtsClient , err := client . NewClientFromConfig ( c . topLevelFlags . ConfigDir )
2024-02-27 19:52:59 +00:00
if err != nil {
return fmt . Errorf ( "unable to create the GoToSocial client; %w" , err )
}
funcMap := map [ string ] func ( * client . Client ) error {
2024-05-23 18:06:49 +01:00
resourceList : c . createList ,
2024-02-27 19:52:59 +00:00
}
doFunc , ok := funcMap [ c . resourceType ]
if ! ok {
2024-05-23 18:06:49 +01:00
return UnsupportedTypeError { resourceType : c . resourceType }
2024-02-27 19:52:59 +00:00
}
return doFunc ( gtsClient )
}
2024-05-23 18:06:49 +01:00
func ( c * CreateExecutor ) createList ( gtsClient * client . Client ) error {
2024-02-27 19:52:59 +00:00
if c . listTitle == "" {
2024-05-23 18:06:49 +01:00
return FlagNotSetError { flagText : flagListTitle }
2024-02-27 19:52:59 +00:00
}
repliesPolicy , err := model . ParseListRepliesPolicy ( c . listRepliesPolicy )
if err != nil {
return fmt . Errorf ( "unable to parse the list replies policy; %w" , err )
}
list , err := gtsClient . CreateList ( c . listTitle , repliesPolicy )
if err != nil {
return fmt . Errorf ( "unable to create the list; %w" , err )
}
fmt . Println ( "Successfully created the following list:" )
fmt . Printf ( "\n%s\n" , list )
return nil
}