From 4cbf1d4b4d9f4b7d8527574b1a1112aa7d685b18 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Fri, 23 Feb 2024 07:49:02 +0000 Subject: [PATCH] refactor: some code refactoring - moved the usage functions to usage.go - removed duplicate definitions of oauth2Conf --- cmd/enbas/login.go | 48 ++++++++++++----------------- cmd/enbas/main.go | 70 ------------------------------------------ cmd/enbas/usage.go | 76 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 98 deletions(-) create mode 100644 cmd/enbas/usage.go diff --git a/cmd/enbas/login.go b/cmd/enbas/login.go index 1fc7266..6559222 100644 --- a/cmd/enbas/login.go +++ b/cmd/enbas/login.go @@ -76,7 +76,18 @@ func (c *loginCommand) Execute() error { return fmt.Errorf("unable to register the application; %w", err) } - consentPageURL := authCodeURL(gtsClient.Authentication) + oauth2Conf := oauth2.Config{ + ClientID: gtsClient.Authentication.ClientID, + ClientSecret: gtsClient.Authentication.ClientSecret, + Scopes: []string{"read"}, + RedirectURL: internal.RedirectUri, + Endpoint: oauth2.Endpoint{ + AuthURL: gtsClient.Authentication.Instance + "/oauth/authorize", + TokenURL: gtsClient.Authentication.Instance + "/oauth/token", + }, + } + + consentPageURL := authCodeURL(oauth2Conf) openLink(consentPageURL) @@ -89,7 +100,7 @@ func (c *loginCommand) Execute() error { return fmt.Errorf("failed to read access code; %w", err) } - gtsClient.Authentication, err = addAccessToken(gtsClient.Authentication, code) + gtsClient.Authentication, err = addAccessToken(gtsClient.Authentication, oauth2Conf, code) if err != nil { return fmt.Errorf("unable to get the access token; %w", err) } @@ -109,36 +120,17 @@ func (c *loginCommand) Execute() error { return nil } -func authCodeURL(account config.Authentication) string { - config := oauth2.Config{ - ClientID: account.ClientID, - ClientSecret: account.ClientSecret, - Scopes: []string{"read"}, - RedirectURL: internal.RedirectUri, - Endpoint: oauth2.Endpoint{ - AuthURL: account.Instance + "/oauth/authorize", - TokenURL: account.Instance + "/oauth/token", - }, - } - - url := config.AuthCodeURL("state", oauth2.AccessTypeOffline) + fmt.Sprintf("&client_name=%s", internal.ApplicationName) +func authCodeURL(oauth2Conf oauth2.Config) string { + url := oauth2Conf.AuthCodeURL( + "state", + oauth2.AccessTypeOffline, + ) + "&client_name=" + internal.ApplicationName return url } -func addAccessToken(authentication config.Authentication, code string) (config.Authentication, error) { - ouauth2Conf := oauth2.Config{ - ClientID: authentication.ClientID, - ClientSecret: authentication.ClientSecret, - Scopes: []string{"read", "write"}, - RedirectURL: internal.RedirectUri, - Endpoint: oauth2.Endpoint{ - AuthURL: authentication.Instance + "/oauth/authorize", - TokenURL: authentication.Instance + "/oauth/token", - }, - } - - token, err := ouauth2Conf.Exchange(context.Background(), code) +func addAccessToken(authentication config.Authentication, oauth2Conf oauth2.Config, code string) (config.Authentication, error) { + token, err := oauth2Conf.Exchange(context.Background(), code) if err != nil { return config.Authentication{}, fmt.Errorf("unable to exchange the code for an access token; %w", err) } diff --git a/cmd/enbas/main.go b/cmd/enbas/main.go index 66157ed..b8ffc20 100644 --- a/cmd/enbas/main.go +++ b/cmd/enbas/main.go @@ -4,8 +4,6 @@ import ( "flag" "fmt" "os" - "slices" - "strings" ) type Executor interface { @@ -75,71 +73,3 @@ func run() error { return nil } - -func commandUsageFunc(name, summary string, flagset *flag.FlagSet) func() { - return func() { - var builder strings.Builder - - fmt.Fprintf( - &builder, - "SUMMARY:\n %s - %s\n\nUSAGE:\n enbas %s [flags]\n\nFLAGS:", - name, - summary, - name, - ) - - flagset.VisitAll(func(f *flag.Flag) { - fmt.Fprintf( - &builder, - "\n -%s, --%s\n %s", - f.Name, - f.Name, - f.Usage, - ) - }) - - builder.WriteString("\n") - - w := flag.CommandLine.Output() - - fmt.Fprint(w, builder.String()) - } -} - -func enbasUsageFunc(summaries map[string]string) func() { - cmds := make([]string, len(summaries)) - ind := 0 - - for k := range summaries { - cmds[ind] = k - ind++ - } - - slices.Sort(cmds) - - return func() { - var builder strings.Builder - - builder.WriteString("SUMMARY:\n enbas - A GoToSocial client for the terminal.\n\n") - - if binaryVersion != "" { - builder.WriteString("VERSION:\n " + binaryVersion + "\n\n") - } - - builder.WriteString("USAGE:\n enbas [flags]\n enbas [command]\n\nCOMMANDS:") - - for _, cmd := range cmds { - fmt.Fprintf(&builder, "\n %s\t%s", cmd, summaries[cmd]) - } - - builder.WriteString("\n\nFLAGS:\n -help, --help\n print the help message\n") - flag.VisitAll(func(f *flag.Flag) { - fmt.Fprintf(&builder, "\n -%s, --%s\n %s\n", f.Name, f.Name, f.Usage) - }) - - builder.WriteString("\nUse \"enbas [command] --help\" for more information about a command.\n") - - w := flag.CommandLine.Output() - fmt.Fprint(w, builder.String()) - } -} diff --git a/cmd/enbas/usage.go b/cmd/enbas/usage.go new file mode 100644 index 0000000..660139b --- /dev/null +++ b/cmd/enbas/usage.go @@ -0,0 +1,76 @@ +package main + +import ( + "flag" + "fmt" + "slices" + "strings" +) + +func commandUsageFunc(name, summary string, flagset *flag.FlagSet) func() { + return func() { + var builder strings.Builder + + fmt.Fprintf( + &builder, + "SUMMARY:\n %s - %s\n\nUSAGE:\n enbas %s [flags]\n\nFLAGS:", + name, + summary, + name, + ) + + flagset.VisitAll(func(f *flag.Flag) { + fmt.Fprintf( + &builder, + "\n -%s, --%s\n %s", + f.Name, + f.Name, + f.Usage, + ) + }) + + builder.WriteString("\n") + + w := flag.CommandLine.Output() + + fmt.Fprint(w, builder.String()) + } +} + +func enbasUsageFunc(summaries map[string]string) func() { + cmds := make([]string, len(summaries)) + ind := 0 + + for k := range summaries { + cmds[ind] = k + ind++ + } + + slices.Sort(cmds) + + return func() { + var builder strings.Builder + + builder.WriteString("SUMMARY:\n enbas - A GoToSocial client for the terminal.\n\n") + + if binaryVersion != "" { + builder.WriteString("VERSION:\n " + binaryVersion + "\n\n") + } + + builder.WriteString("USAGE:\n enbas [flags]\n enbas [command]\n\nCOMMANDS:") + + for _, cmd := range cmds { + fmt.Fprintf(&builder, "\n %s\t%s", cmd, summaries[cmd]) + } + + builder.WriteString("\n\nFLAGS:\n -help, --help\n print the help message\n") + flag.VisitAll(func(f *flag.Flag) { + fmt.Fprintf(&builder, "\n -%s, --%s\n %s\n", f.Name, f.Name, f.Usage) + }) + + builder.WriteString("\nUse \"enbas [command] --help\" for more information about a command.\n") + + w := flag.CommandLine.Output() + fmt.Fprint(w, builder.String()) + } +}