enbas/internal/executor/login.go
Dan Anglin 84091f398d
feat: add Enbas CLI schema and code generator
Summary:

- Created a custom CLI schema for Enbas which will act as the Source
  of Truth for code and document generation.
- Created a code generator which uses the schema to generate the
  executor definitions and code in the internal usage package.

Changes:

- Created the Enbas CLI schema as the Source of Truth for Enbas.
- Created the code generator that generates the executor
  definitions and code in the usage package.
- Regenerated the executor definitions using the code generator.
- Moved the custom flag value types to the new internal flag
  package.
- Created a new flag value type for the bool pointer to replace the
  flag.BoolFunc() used for the sensitive and no-color flags.
- Moved the version and build variables to the new internal version
  package to simplify the version executor.
- Created a new usage package and moved the usage functions there.
- Changed the type of the account-name flag from string to the
  internal StringSliceValue type.
2024-08-13 14:53:26 +01:00

76 lines
2.2 KiB
Go

package executor
import (
"fmt"
"strings"
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
"codeflow.dananglin.me.uk/apollo/enbas/internal/config"
"codeflow.dananglin.me.uk/apollo/enbas/internal/utilities"
)
func (l *LoginExecutor) Execute() error {
var err error
if l.instance == "" {
return FlagNotSetError{flagText: flagInstance}
}
instance := l.instance
if !strings.HasPrefix(instance, "https") || !strings.HasPrefix(instance, "http") {
instance = "https://" + instance
}
for strings.HasSuffix(instance, "/") {
instance = instance[:len(instance)-1]
}
credentials := config.Credentials{
Instance: instance,
}
gtsClient := client.NewClient(credentials)
if err := gtsClient.Register(); err != nil {
return fmt.Errorf("unable to register the application: %w", err)
}
consentPageURL := gtsClient.AuthCodeURL()
_ = utilities.OpenLink(l.config.Integrations.Browser, consentPageURL)
var builder strings.Builder
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: ")
l.printer.PrintInfo(builder.String())
var code string
if _, err := fmt.Scanln(&code); err != nil {
return fmt.Errorf("failed to read access code: %w", err)
}
if err := gtsClient.UpdateToken(code); err != nil {
return fmt.Errorf("unable to update the client's access token: %w", err)
}
account, err := gtsClient.VerifyCredentials()
if err != nil {
return fmt.Errorf("unable to verify the credentials: %w", err)
}
loginName, err := config.SaveCredentials(l.config.CredentialsFile, account.Username, gtsClient.Authentication)
if err != nil {
return fmt.Errorf("unable to save the authentication details: %w", err)
}
l.printer.PrintSuccess("You have successfully logged as " + loginName + ".")
return nil
}