2024-02-23 09:44:57 +00:00
package main
import (
"flag"
"fmt"
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
"codeflow.dananglin.me.uk/apollo/enbas/internal/config"
2024-02-24 15:53:29 +00:00
"codeflow.dananglin.me.uk/apollo/enbas/internal/model"
2024-02-27 19:52:59 +00:00
"codeflow.dananglin.me.uk/apollo/enbas/internal/utilities"
2024-02-23 09:44:57 +00:00
)
type showCommand struct {
* flag . FlagSet
2024-05-20 17:26:06 +01:00
myAccount bool
showAccountRelationship bool
resourceType string
account string
accountID string
statusID string
timelineCategory string
listID string
tag string
limit int
2024-02-23 09:44:57 +00:00
}
func newShowCommand ( name , summary string ) * showCommand {
command := showCommand {
2024-02-24 15:53:29 +00:00
FlagSet : flag . NewFlagSet ( name , flag . ExitOnError ) ,
2024-02-23 09:44:57 +00:00
}
2024-02-28 19:35:18 +00:00
command . BoolVar ( & command . myAccount , myAccountFlag , false , "set to true to lookup your account" )
2024-05-20 17:26:06 +01:00
command . BoolVar ( & command . showAccountRelationship , "show-account-relationship" , false , "show your relationship to the specified account" )
2024-02-28 19:35:18 +00:00
command . StringVar ( & command . resourceType , resourceTypeFlag , "" , "specify the type of resource to display" )
command . StringVar ( & command . account , accountFlag , "" , "specify the account URI to lookup" )
2024-05-20 17:26:06 +01:00
command . StringVar ( & command . accountID , accountIDFlag , "" , "specify the account ID" )
2024-02-28 19:35:18 +00:00
command . StringVar ( & command . statusID , statusIDFlag , "" , "specify the ID of the status to display" )
command . StringVar ( & command . timelineCategory , timelineCategoryFlag , "home" , "specify the type of timeline to display (valid values are home, public, list and tag)" )
command . StringVar ( & command . listID , listIDFlag , "" , "specify the ID of the list to display" )
command . StringVar ( & command . tag , tagFlag , "" , "specify the name of the tag to use" )
2024-05-20 17:26:06 +01:00
command . IntVar ( & command . limit , limitFlag , 20 , "specify the limit of items to display" )
2024-02-28 19:35:18 +00:00
2024-02-23 09:44:57 +00:00
command . Usage = commandUsageFunc ( name , summary , command . FlagSet )
return & command
}
func ( c * showCommand ) Execute ( ) error {
2024-02-27 19:52:59 +00:00
if c . resourceType == "" {
2024-02-28 19:35:18 +00:00
return flagNotSetError { flagText : resourceTypeFlag }
2024-02-23 09:44:57 +00:00
}
funcMap := map [ string ] func ( * client . Client ) error {
2024-05-20 17:26:06 +01:00
instanceResource : c . showInstance ,
accountResource : c . showAccount ,
statusResource : c . showStatus ,
timelineResource : c . showTimeline ,
listResource : c . showList ,
followersResource : c . showFollowers ,
followingResource : c . showFollowing ,
2024-02-23 09:44:57 +00:00
}
2024-02-27 19:52:59 +00:00
doFunc , ok := funcMap [ c . resourceType ]
2024-02-23 09:44:57 +00:00
if ! ok {
2024-02-28 19:35:18 +00:00
return unsupportedResourceTypeError { resourceType : c . resourceType }
}
gtsClient , err := client . NewClientFromConfig ( )
if err != nil {
return fmt . Errorf ( "unable to create the GoToSocial client; %w" , err )
2024-02-23 09:44:57 +00:00
}
return doFunc ( gtsClient )
}
func ( c * showCommand ) showInstance ( gts * client . Client ) error {
instance , err := gts . GetInstance ( )
if err != nil {
return fmt . Errorf ( "unable to retrieve the instance details; %w" , err )
}
2024-02-23 14:19:12 +00:00
fmt . Println ( instance )
2024-02-23 09:44:57 +00:00
return nil
}
func ( c * showCommand ) showAccount ( gts * client . Client ) error {
var accountURI string
if c . myAccount {
authConfig , err := config . NewAuthenticationConfigFromFile ( )
if err != nil {
return fmt . Errorf ( "unable to retrieve the authentication configuration; %w" , err )
}
accountURI = authConfig . CurrentAccount
} else {
if c . account == "" {
2024-02-28 19:35:18 +00:00
return flagNotSetError { flagText : accountFlag }
2024-02-23 09:44:57 +00:00
}
accountURI = c . account
}
account , err := gts . GetAccount ( accountURI )
if err != nil {
return fmt . Errorf ( "unable to retrieve the account details; %w" , err )
}
2024-02-23 14:19:12 +00:00
fmt . Println ( account )
2024-02-23 09:44:57 +00:00
2024-05-20 17:26:06 +01:00
if c . showAccountRelationship {
relationship , err := gts . GetAccountRelationship ( account . ID )
if err != nil {
return fmt . Errorf ( "unable to retrieve the relationship to this account; %w" , err )
}
fmt . Println ( relationship )
}
2024-02-23 09:44:57 +00:00
return nil
}
2024-02-23 13:14:50 +00:00
func ( c * showCommand ) showStatus ( gts * client . Client ) error {
if c . statusID == "" {
2024-02-28 19:35:18 +00:00
return flagNotSetError { flagText : statusIDFlag }
2024-02-23 13:14:50 +00:00
}
status , err := gts . GetStatus ( c . statusID )
if err != nil {
return fmt . Errorf ( "unable to retrieve the status; %w" , err )
}
2024-02-23 14:19:12 +00:00
fmt . Println ( status )
2024-02-23 13:14:50 +00:00
return nil
}
2024-02-24 15:53:29 +00:00
func ( c * showCommand ) showTimeline ( gts * client . Client ) error {
var (
timeline model . Timeline
err error
)
2024-02-28 19:35:18 +00:00
switch c . timelineCategory {
2024-02-24 15:53:29 +00:00
case "home" :
2024-05-20 17:26:06 +01:00
timeline , err = gts . GetHomeTimeline ( c . limit )
2024-02-24 15:53:29 +00:00
case "public" :
2024-05-20 17:26:06 +01:00
timeline , err = gts . GetPublicTimeline ( c . limit )
2024-02-24 15:53:29 +00:00
case "list" :
2024-02-28 19:35:18 +00:00
if c . listID == "" {
return flagNotSetError { flagText : listIDFlag }
2024-02-24 15:53:29 +00:00
}
2024-05-20 17:26:06 +01:00
timeline , err = gts . GetListTimeline ( c . listID , c . limit )
2024-02-24 15:53:29 +00:00
case "tag" :
2024-02-28 19:35:18 +00:00
if c . tag == "" {
return flagNotSetError { flagText : tagFlag }
2024-02-24 15:53:29 +00:00
}
2024-05-20 17:26:06 +01:00
timeline , err = gts . GetTagTimeline ( c . tag , c . limit )
2024-02-24 15:53:29 +00:00
default :
2024-02-28 19:35:18 +00:00
return invalidTimelineCategoryError { category : c . timelineCategory }
2024-02-24 15:53:29 +00:00
}
if err != nil {
2024-02-28 19:35:18 +00:00
return fmt . Errorf ( "unable to retrieve the %s timeline; %w" , c . timelineCategory , err )
2024-02-24 15:53:29 +00:00
}
if len ( timeline . Statuses ) == 0 {
fmt . Println ( "There are no statuses in this timeline." )
return nil
}
fmt . Println ( timeline )
return nil
}
2024-02-27 19:52:59 +00:00
2024-05-19 11:48:36 +01:00
func ( c * showCommand ) showList ( gts * client . Client ) error {
if c . listID == "" {
return c . showLists ( gts )
}
list , err := gts . GetList ( c . listID )
if err != nil {
return fmt . Errorf ( "unable to retrieve the list; %w" , err )
}
accounts , err := gts . GetAccountsFromList ( c . listID , 0 )
if err != nil {
return fmt . Errorf ( "unable to retrieve the accounts from the list; %w" , err )
}
if len ( accounts ) > 0 {
accountMap := make ( map [ string ] string )
for i := range accounts {
accountMap [ accounts [ i ] . ID ] = accounts [ i ] . Username
}
2024-05-20 17:26:06 +01:00
2024-05-19 11:48:36 +01:00
list . Accounts = accountMap
}
fmt . Println ( list )
return nil
}
2024-02-27 19:52:59 +00:00
func ( c * showCommand ) showLists ( gts * client . Client ) error {
lists , err := gts . GetAllLists ( )
if err != nil {
return fmt . Errorf ( "unable to retrieve the lists; %w" , err )
}
if len ( lists ) == 0 {
fmt . Println ( "You have no lists." )
return nil
}
fmt . Println ( utilities . HeaderFormat ( "LISTS" ) )
2024-05-19 11:48:36 +01:00
fmt . Println ( lists )
2024-02-27 19:52:59 +00:00
return nil
}
2024-05-20 17:26:06 +01:00
func ( c * showCommand ) showFollowers ( gts * client . Client ) error {
if c . accountID == "" {
return flagNotSetError { flagText : accountIDFlag }
}
followers , err := gts . GetFollowers ( c . accountID , c . limit )
if err != nil {
return fmt . Errorf ( "unable to retrieve the list of followers; %w" , err )
}
if len ( followers ) > 0 {
fmt . Println ( followers )
} else {
fmt . Println ( "There are no followers for this account or the list is hidden." )
}
return nil
}
func ( c * showCommand ) showFollowing ( gts * client . Client ) error {
if c . accountID == "" {
return flagNotSetError { flagText : accountIDFlag }
}
following , err := gts . GetFollowing ( c . accountID , c . limit )
if err != nil {
return fmt . Errorf ( "unable to retrieve the list of followed accounts; %w" , err )
}
if len ( following ) > 0 {
fmt . Println ( following )
} else {
fmt . Println ( "This account is not following anyone or the list is hidden." )
}
return nil
}