Dan Anglin
42251f6df8
SUMMARY This commit adds configuration support to enbas. The configuration is stored as a JSON file in the user specified configuration directory. When using enbas for the first time, the user will first need to execute the new init command in order to generate the configuration. Once this has been generated the user can edit the settings to personalise their experience, login to their account and use enbas as normal. For now the configurable settings included in the configuration are as follows: - The path to the credentials file (by default this is set to a file in the same directory as the configuration file). - The path to the cache directory. - The character limit used for line wrapping. - The programs used for integrations such as paging, media viewing, opening URLs, etc. CHANGES - added the new config type. - added the new init executor for generating a new configuration file. - removed the following top level flags in favour of the new configration support. - cache-dir - pager - image-viewer - video-player - max-terminal-width - added a new error type for use when an unknown media attachment ID is specified. - updated the usage function for the executors to support a case where a flagsets has no flags. - update .golangci.yaml to disable some linters
144 lines
3.5 KiB
Go
144 lines
3.5 KiB
Go
// SPDX-FileCopyrightText: 2024 Dan Anglin <d.n.i.anglin@gmail.com>
|
|
//
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package client
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"time"
|
|
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal"
|
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/config"
|
|
)
|
|
|
|
type Client struct {
|
|
Authentication config.Credentials
|
|
HTTPClient http.Client
|
|
UserAgent string
|
|
Timeout time.Duration
|
|
}
|
|
|
|
func NewClientFromFile(path string) (*Client, error) {
|
|
config, err := config.NewCredentialsConfigFromFile(path)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("unable to get the authentication configuration: %w", err)
|
|
}
|
|
|
|
currentAuthentication := config.Credentials[config.CurrentAccount]
|
|
|
|
return NewClient(currentAuthentication), nil
|
|
}
|
|
|
|
func NewClient(authentication config.Credentials) *Client {
|
|
httpClient := http.Client{}
|
|
|
|
gtsClient := Client{
|
|
Authentication: authentication,
|
|
HTTPClient: httpClient,
|
|
UserAgent: internal.UserAgent,
|
|
Timeout: 5 * time.Second,
|
|
}
|
|
|
|
return >sClient
|
|
}
|
|
|
|
func (g *Client) AuthCodeURL() string {
|
|
format := "%s/oauth/authorize?client_id=%s&redirect_uri=%s&response_type=code"
|
|
escapedRedirectURI := url.QueryEscape(internal.RedirectURI)
|
|
|
|
return fmt.Sprintf(
|
|
format,
|
|
g.Authentication.Instance,
|
|
g.Authentication.ClientID,
|
|
escapedRedirectURI,
|
|
)
|
|
}
|
|
|
|
func (g *Client) DownloadMedia(url, path string) error {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
|
|
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
|
if err != nil {
|
|
return fmt.Errorf("unable to create the HTTP request: %w", err)
|
|
}
|
|
|
|
request.Header.Set("User-Agent", g.UserAgent)
|
|
|
|
response, err := g.HTTPClient.Do(request)
|
|
if err != nil {
|
|
return fmt.Errorf("received an error after attempting the download: %w", err)
|
|
}
|
|
defer response.Body.Close()
|
|
|
|
if response.StatusCode != http.StatusOK {
|
|
return fmt.Errorf(
|
|
"did not receive an OK response from the GoToSocial server: got %d",
|
|
response.StatusCode,
|
|
)
|
|
}
|
|
|
|
file, err := os.Create(path)
|
|
if err != nil {
|
|
return fmt.Errorf("unable to create %s: %w", path, err)
|
|
}
|
|
defer file.Close()
|
|
|
|
if _, err = io.Copy(file, response.Body); err != nil {
|
|
return fmt.Errorf("unable to save the download to %s: %w", path, err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (g *Client) sendRequest(method string, url string, requestBody io.Reader, object any) error {
|
|
ctx, cancel := context.WithTimeout(context.Background(), g.Timeout)
|
|
defer cancel()
|
|
|
|
request, err := http.NewRequestWithContext(ctx, method, url, requestBody)
|
|
if err != nil {
|
|
return fmt.Errorf("unable to create the HTTP request: %w", err)
|
|
}
|
|
|
|
request.Header.Set("Content-Type", "application/json; charset=utf-8")
|
|
request.Header.Set("Accept", "application/json; charset=utf-8")
|
|
request.Header.Set("User-Agent", g.UserAgent)
|
|
|
|
if len(g.Authentication.AccessToken) > 0 {
|
|
request.Header.Set("Authorization", "Bearer "+g.Authentication.AccessToken)
|
|
}
|
|
|
|
response, err := g.HTTPClient.Do(request)
|
|
if err != nil {
|
|
return fmt.Errorf("received an error after sending the request: %w", err)
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
if response.StatusCode < http.StatusOK || response.StatusCode >= http.StatusBadRequest {
|
|
return fmt.Errorf(
|
|
"did not receive an OK response from the GoToSocial server: got %d",
|
|
response.StatusCode,
|
|
)
|
|
}
|
|
|
|
if object == nil {
|
|
return nil
|
|
}
|
|
|
|
if err := json.NewDecoder(response.Body).Decode(object); err != nil {
|
|
return fmt.Errorf(
|
|
"unable to decode the response from the GoToSocial server: %w",
|
|
err,
|
|
)
|
|
}
|
|
|
|
return nil
|
|
}
|