2024-05-23 18:06:49 +01:00
package executor
2024-02-23 09:44:57 +00:00
import (
"fmt"
"strings"
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
"codeflow.dananglin.me.uk/apollo/enbas/internal/config"
2024-02-25 06:41:13 +00:00
"codeflow.dananglin.me.uk/apollo/enbas/internal/utilities"
2024-02-23 09:44:57 +00:00
)
2024-06-26 12:38:27 +01:00
func ( l * LoginExecutor ) Execute ( ) error {
2024-02-23 09:44:57 +00:00
var err error
2024-06-26 12:38:27 +01:00
if l . instance == "" {
2024-08-18 09:57:48 +01:00
return Error { "please specify the instance that you want to log into" }
2024-02-23 09:44:57 +00:00
}
2024-06-26 12:38:27 +01:00
instance := l . instance
2024-02-23 09:44:57 +00:00
if ! strings . HasPrefix ( instance , "https" ) || ! strings . HasPrefix ( instance , "http" ) {
instance = "https://" + instance
}
for strings . HasSuffix ( instance , "/" ) {
instance = instance [ : len ( instance ) - 1 ]
}
2024-02-27 09:31:17 +00:00
credentials := config . Credentials {
2024-02-23 09:44:57 +00:00
Instance : instance ,
}
2024-02-27 09:31:17 +00:00
gtsClient := client . NewClient ( credentials )
2024-02-23 09:44:57 +00:00
if err := gtsClient . Register ( ) ; err != nil {
2024-06-02 11:35:43 +01:00
return fmt . Errorf ( "unable to register the application: %w" , err )
2024-02-23 09:44:57 +00:00
}
2024-02-25 06:41:13 +00:00
consentPageURL := gtsClient . AuthCodeURL ( )
2024-06-26 12:38:27 +01:00
_ = utilities . OpenLink ( l . config . Integrations . Browser , consentPageURL )
2024-02-25 06:41:13 +00:00
2024-06-17 18:59:20 +01:00
var builder strings . Builder
2024-02-25 06:41:13 +00:00
2024-06-17 18:59:20 +01:00
builder . WriteString ( "\nYou'll need to sign into your GoToSocial's consent page in order to generate the out-of-band token to continue with the application's login process." )
builder . WriteString ( "\nYour browser may have opened the link to the consent page already. If not, please copy and paste the link below to your browser:" )
builder . WriteString ( "\n\n" + consentPageURL )
builder . WriteString ( "\n\n" + "Once you have the code please copy and paste it below." )
builder . WriteString ( "\n" + "Out-of-band token: " )
2024-02-23 09:44:57 +00:00
2024-06-26 12:38:27 +01:00
l . printer . PrintInfo ( builder . String ( ) )
2024-02-23 09:44:57 +00:00
var code string
if _ , err := fmt . Scanln ( & code ) ; err != nil {
2024-06-02 11:35:43 +01:00
return fmt . Errorf ( "failed to read access code: %w" , err )
2024-02-23 09:44:57 +00:00
}
2024-02-25 06:41:13 +00:00
if err := gtsClient . UpdateToken ( code ) ; err != nil {
2024-06-02 11:35:43 +01:00
return fmt . Errorf ( "unable to update the client's access token: %w" , err )
2024-02-23 09:44:57 +00:00
}
account , err := gtsClient . VerifyCredentials ( )
if err != nil {
2024-06-02 11:35:43 +01:00
return fmt . Errorf ( "unable to verify the credentials: %w" , err )
2024-02-23 09:44:57 +00:00
}
2024-06-26 12:38:27 +01:00
loginName , err := config . SaveCredentials ( l . config . CredentialsFile , account . Username , gtsClient . Authentication )
2024-02-23 09:44:57 +00:00
if err != nil {
2024-06-02 11:35:43 +01:00
return fmt . Errorf ( "unable to save the authentication details: %w" , err )
2024-02-23 09:44:57 +00:00
}
2024-07-01 13:49:11 +01:00
l . printer . PrintSuccess ( "You have successfully logged as " + loginName + "." )
2024-02-23 09:44:57 +00:00
return nil
}