refactor: move information values to info package

- Move the build and application information to the internal info
  package.
- Move the user agent and redirect URI string to the internal client
  package.
This commit is contained in:
Dan Anglin 2024-08-23 02:35:30 +01:00
parent 89e53bcc9f
commit cc5e3f0044
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
10 changed files with 34 additions and 36 deletions

View file

@ -10,12 +10,13 @@ import (
"os"
"time"
"codeflow.dananglin.me.uk/apollo/enbas/internal"
"codeflow.dananglin.me.uk/apollo/enbas/internal/config"
)
const (
applicationJSON string = "application/json; charset=utf-8"
redirectURI string = "urn:ietf:wg:oauth:2.0:oob"
userAgent string = "Enbas/0.0.0"
)
type Client struct {
@ -42,7 +43,7 @@ func NewClient(authentication config.Credentials) *Client {
gtsClient := Client{
Authentication: authentication,
HTTPClient: httpClient,
UserAgent: internal.UserAgent,
UserAgent: userAgent,
Timeout: 5 * time.Second,
}
@ -51,7 +52,7 @@ func NewClient(authentication config.Credentials) *Client {
func (g *Client) AuthCodeURL() string {
format := "%s/oauth/authorize?client_id=%s&redirect_uri=%s&response_type=code"
escapedRedirectURI := url.QueryEscape(internal.RedirectURI)
escapedRedirectURI := url.QueryEscape(redirectURI)
return fmt.Sprintf(
format,

View file

@ -6,7 +6,7 @@ import (
"fmt"
"net/http"
"codeflow.dananglin.me.uk/apollo/enbas/internal"
"codeflow.dananglin.me.uk/apollo/enbas/internal/info"
"codeflow.dananglin.me.uk/apollo/enbas/internal/model"
)
@ -19,10 +19,10 @@ type registerRequest struct {
func (g *Client) Register() error {
registerParams := registerRequest{
ClientName: internal.ApplicationName,
RedirectUris: internal.RedirectURI,
ClientName: info.ApplicationName,
RedirectUris: redirectURI,
Scopes: "read write",
Website: internal.ApplicationWebsite,
Website: info.ApplicationWebsite,
}
data, err := json.Marshal(registerParams)

View file

@ -5,8 +5,6 @@ import (
"encoding/json"
"fmt"
"net/http"
"codeflow.dananglin.me.uk/apollo/enbas/internal"
)
type tokenRequest struct {
@ -26,7 +24,7 @@ type tokenResponse struct {
func (g *Client) UpdateToken(code string) error {
tokenReq := tokenRequest{
RedirectURI: internal.RedirectURI,
RedirectURI: redirectURI,
ClientID: g.Authentication.ClientID,
ClientSecret: g.Authentication.ClientSecret,
GrantType: "authorization_code",

View file

@ -1,4 +1,9 @@
package version
package info
const (
ApplicationName string = "enbas"
ApplicationWebsite string = "https://codeflow.dananglin.me.uk/apollo/enbas"
)
var (
BinaryVersion string //nolint:gochecknoglobals

View file

@ -1,8 +0,0 @@
package internal
const (
ApplicationName = "enbas"
ApplicationWebsite = "https://codeflow.dananglin.me.uk/apollo/enbas"
RedirectURI = "urn:ietf:wg:oauth:2.0:oob"
UserAgent = "Enbas/0.0.0"
)

View file

@ -4,12 +4,12 @@ import (
"strings"
"text/tabwriter"
"codeflow.dananglin.me.uk/apollo/enbas/internal/version"
"codeflow.dananglin.me.uk/apollo/enbas/internal/info"
)
func (p Printer) PrintVersion(showFullVersion bool) {
if !showFullVersion {
printToStdout("Enbas " + version.BinaryVersion + "\n")
printToStdout("Enbas " + info.BinaryVersion + "\n")
return
}
@ -20,10 +20,10 @@ func (p Printer) PrintVersion(showFullVersion bool) {
tableWriter := tabwriter.NewWriter(&builder, 0, 4, 1, ' ', 0)
_, _ = tableWriter.Write([]byte(p.fieldFormat("Version:") + "\t" + version.BinaryVersion + "\n"))
_, _ = tableWriter.Write([]byte(p.fieldFormat("Git commit:") + "\t" + version.GitCommit + "\n"))
_, _ = tableWriter.Write([]byte(p.fieldFormat("Go version:") + "\t" + version.GoVersion + "\n"))
_, _ = tableWriter.Write([]byte(p.fieldFormat("Build date:") + "\t" + version.BuildTime + "\n"))
_, _ = tableWriter.Write([]byte(p.fieldFormat("Version:") + "\t" + info.BinaryVersion + "\n"))
_, _ = tableWriter.Write([]byte(p.fieldFormat("Git commit:") + "\t" + info.GitCommit + "\n"))
_, _ = tableWriter.Write([]byte(p.fieldFormat("Go version:") + "\t" + info.GoVersion + "\n"))
_, _ = tableWriter.Write([]byte(p.fieldFormat("Build date:") + "\t" + info.BuildTime + "\n"))
tableWriter.Flush()

View file

@ -7,7 +7,7 @@ import (
"strings"
"text/tabwriter"
"codeflow.dananglin.me.uk/apollo/enbas/internal/version"
"codeflow.dananglin.me.uk/apollo/enbas/internal/info"
)
func AppUsageFunc() func() {
@ -24,13 +24,13 @@ func AppUsageFunc() func() {
return func() {
var builder strings.Builder
builder.WriteString("SUMMARY:\n enbas - A GoToSocial client for the terminal.\n\n")
builder.WriteString("SUMMARY:\n " + info.ApplicationName + " - A GoToSocial client for the terminal.\n\n")
if version.BinaryVersion != "" {
builder.WriteString("VERSION:\n " + version.BinaryVersion + "\n\n")
if info.BinaryVersion != "" {
builder.WriteString("VERSION:\n " + info.BinaryVersion + "\n\n")
}
builder.WriteString("USAGE:\n enbas [flags]\n enbas [flags] [command]\n\nCOMMANDS:")
builder.WriteString("USAGE:\n " + info.ApplicationName + " [flags]\n " + info.ApplicationName + " [flags] [command]\n\nCOMMANDS:")
tableWriter := tabwriter.NewWriter(&builder, 0, 8, 0, '\t', 0)
@ -45,7 +45,7 @@ func AppUsageFunc() func() {
fmt.Fprintf(&builder, "\n --%s\n %s", f.Name, f.Usage)
})
builder.WriteString("\n\nUse \"enbas [command] --help\" for more information about a command.\n")
builder.WriteString("\n\nUse \"" + info.ApplicationName + " [command] --help\" for more information about a command.\n")
w := flag.CommandLine.Output()
fmt.Fprint(w, builder.String())

View file

@ -4,6 +4,8 @@ import (
"flag"
"slices"
"strings"
"codeflow.dananglin.me.uk/apollo/enbas/internal/info"
)
// ExecutorUsageFunc returns the function used to print a command's help page.
@ -14,7 +16,7 @@ func ExecutorUsageFunc(name, summary string, flagset *flag.FlagSet) func() {
builder.WriteString("SUMMARY:")
builder.WriteString("\n " + name + " - " + summary)
builder.WriteString("\n\nUSAGE:")
builder.WriteString("\n enbas " + name)
builder.WriteString("\n " + info.ApplicationName + " " + name)
flagMap := make(map[string]string)

View file

@ -6,7 +6,7 @@ import (
"os"
"path/filepath"
"codeflow.dananglin.me.uk/apollo/enbas/internal"
"codeflow.dananglin.me.uk/apollo/enbas/internal/info"
)
const (
@ -24,7 +24,7 @@ func CalculateConfigDir(configDir string) (string, error) {
return "", fmt.Errorf("unable to get your default config diretory: %w", err)
}
return filepath.Join(configRoot, internal.ApplicationName), nil
return filepath.Join(configRoot, info.ApplicationName), nil
}
func CalculateMediaCacheDir(cacheRoot, instance string) (string, error) {
@ -57,7 +57,7 @@ func calculateCacheDir(cacheRoot, instance string) (string, error) {
return "", fmt.Errorf("unable to get your default cache directory: %w", err)
}
return filepath.Join(cacheRoot, internal.ApplicationName, fqdn), nil
return filepath.Join(cacheRoot, info.ApplicationName, fqdn), nil
}
func EnsureDirectory(dir string) error {

View file

@ -110,7 +110,7 @@ func Clean() error {
// ldflags returns the build flags.
func ldflags() string {
versionPackage := "codeflow.dananglin.me.uk/apollo/enbas/internal/version"
versionPackage := "codeflow.dananglin.me.uk/apollo/enbas/internal/info"
binaryVersionVar := versionPackage + "." + "BinaryVersion"
gitCommitVar := versionPackage + "." + "GitCommit"
goVersionVar := versionPackage + "." + "GoVersion"