Compare commits
No commits in common. "75570bd6568261dd5b59be6ae6ffbc6f021c9e59" and "96ad6a7b558a644d1b2adffda2e36a22752b277c" have entirely different histories.
75570bd656
...
96ad6a7b55
8 changed files with 47 additions and 235 deletions
|
@ -30,17 +30,13 @@ func main() {
|
||||||
func run() error {
|
func run() error {
|
||||||
var (
|
var (
|
||||||
configDir string
|
configDir string
|
||||||
cacheDir string
|
|
||||||
pager string
|
pager string
|
||||||
imageViewer string
|
|
||||||
maxTerminalWidth int
|
maxTerminalWidth int
|
||||||
noColor *bool
|
noColor *bool
|
||||||
)
|
)
|
||||||
|
|
||||||
flag.StringVar(&configDir, "config-dir", "", "Specify your config directory")
|
flag.StringVar(&configDir, "config-dir", "", "Specify your config directory")
|
||||||
flag.StringVar(&cacheDir, "cache-dir", "", "Specify your cache directory")
|
|
||||||
flag.StringVar(&pager, "pager", "", "Specify your preferred pager to page through long outputs. This is disabled by default.")
|
flag.StringVar(&pager, "pager", "", "Specify your preferred pager to page through long outputs. This is disabled by default.")
|
||||||
flag.StringVar(&imageViewer, "image-viewer", "", "Specify your preferred image viewer for viewing images attached to statuses.")
|
|
||||||
flag.IntVar(&maxTerminalWidth, "max-terminal-width", 80, "Specify the maximum terminal width when displaying resources on screen.")
|
flag.IntVar(&maxTerminalWidth, "max-terminal-width", 80, "Specify the maximum terminal width when displaying resources on screen.")
|
||||||
|
|
||||||
flag.BoolFunc("no-color", "Disable ANSI colour output when displaying text on screen", func(value string) error {
|
flag.BoolFunc("no-color", "Disable ANSI colour output when displaying text on screen", func(value string) error {
|
||||||
|
@ -174,8 +170,6 @@ func run() error {
|
||||||
executor.CommandShow: executor.NewShowExecutor(
|
executor.CommandShow: executor.NewShowExecutor(
|
||||||
printer,
|
printer,
|
||||||
configDir,
|
configDir,
|
||||||
cacheDir,
|
|
||||||
imageViewer,
|
|
||||||
executor.CommandShow,
|
executor.CommandShow,
|
||||||
executor.CommandSummaryLookup(executor.CommandShow),
|
executor.CommandSummaryLookup(executor.CommandShow),
|
||||||
),
|
),
|
||||||
|
|
|
@ -60,10 +60,6 @@ func (g *Client) AuthCodeURL() string {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Client) DownloadFile(url, path string) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *Client) sendRequest(method string, url string, requestBody io.Reader, object any) error {
|
func (g *Client) sendRequest(method string, url string, requestBody io.Reader, object any) error {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), g.Timeout)
|
ctx, cancel := context.WithTimeout(context.Background(), g.Timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
|
@ -11,8 +11,6 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"codeflow.dananglin.me.uk/apollo/enbas/internal/utilities"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -43,7 +41,7 @@ func (e CredentialsNotFoundError) Error() string {
|
||||||
// directory. If the directory is not specified then the default directory is used. If the directory
|
// directory. If the directory is not specified then the default directory is used. If the directory
|
||||||
// is not present, it will be created.
|
// is not present, it will be created.
|
||||||
func SaveCredentials(configDir, username string, credentials Credentials) (string, error) {
|
func SaveCredentials(configDir, username string, credentials Credentials) (string, error) {
|
||||||
if err := utilities.EnsureDirectory(utilities.CalculateConfigDir(configDir)); err != nil {
|
if err := ensureConfigDir(calculateConfigDir(configDir)); err != nil {
|
||||||
return "", fmt.Errorf("unable to ensure the configuration directory: %w", err)
|
return "", fmt.Errorf("unable to ensure the configuration directory: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -143,5 +141,5 @@ func saveCredentialsConfigFile(authConfig CredentialsConfig, configDir string) e
|
||||||
}
|
}
|
||||||
|
|
||||||
func credentialsConfigFile(configDir string) string {
|
func credentialsConfigFile(configDir string) string {
|
||||||
return filepath.Join(utilities.CalculateConfigDir(configDir), credentialsFileName)
|
return filepath.Join(calculateConfigDir(configDir), credentialsFileName)
|
||||||
}
|
}
|
||||||
|
|
41
internal/config/directory.go
Normal file
41
internal/config/directory.go
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
// SPDX-FileCopyrightText: 2024 Dan Anglin <d.n.i.anglin@gmail.com>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"codeflow.dananglin.me.uk/apollo/enbas/internal"
|
||||||
|
)
|
||||||
|
|
||||||
|
func calculateConfigDir(configDir string) string {
|
||||||
|
if configDir != "" {
|
||||||
|
return configDir
|
||||||
|
}
|
||||||
|
|
||||||
|
rootDir, err := os.UserConfigDir()
|
||||||
|
if err != nil {
|
||||||
|
rootDir = "."
|
||||||
|
}
|
||||||
|
|
||||||
|
return filepath.Join(rootDir, internal.ApplicationName)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ensureConfigDir(configDir string) error {
|
||||||
|
if _, err := os.Stat(configDir); err != nil {
|
||||||
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
|
if err := os.MkdirAll(configDir, 0o750); err != nil {
|
||||||
|
return fmt.Errorf("unable to create %s: %w", configDir, err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return fmt.Errorf("unknown error received after getting the config directory information: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -5,11 +5,8 @@
|
||||||
package executor
|
package executor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
|
||||||
"codeflow.dananglin.me.uk/apollo/enbas/internal/model"
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/model"
|
||||||
|
@ -26,7 +23,6 @@ type ShowExecutor struct {
|
||||||
showUserPreferences bool
|
showUserPreferences bool
|
||||||
showInBrowser bool
|
showInBrowser bool
|
||||||
configDir string
|
configDir string
|
||||||
cacheRoot string
|
|
||||||
resourceType string
|
resourceType string
|
||||||
accountName string
|
accountName string
|
||||||
statusID string
|
statusID string
|
||||||
|
@ -35,19 +31,15 @@ type ShowExecutor struct {
|
||||||
tag string
|
tag string
|
||||||
pollID string
|
pollID string
|
||||||
attachmentID string
|
attachmentID string
|
||||||
fromResourceType string
|
|
||||||
imageViewer string
|
|
||||||
limit int
|
limit int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewShowExecutor(printer *printer.Printer, configDir, cacheRoot, imageViewer, name, summary string) *ShowExecutor {
|
func NewShowExecutor(printer *printer.Printer, configDir, name, summary string) *ShowExecutor {
|
||||||
showExe := ShowExecutor{
|
showExe := ShowExecutor{
|
||||||
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
|
||||||
|
|
||||||
printer: printer,
|
printer: printer,
|
||||||
configDir: configDir,
|
configDir: configDir,
|
||||||
cacheRoot: cacheRoot,
|
|
||||||
imageViewer: imageViewer,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
showExe.BoolVar(&showExe.myAccount, flagMyAccount, false, "Set to true to lookup your account")
|
showExe.BoolVar(&showExe.myAccount, flagMyAccount, false, "Set to true to lookup your account")
|
||||||
|
@ -62,7 +54,6 @@ func NewShowExecutor(printer *printer.Printer, configDir, cacheRoot, imageViewer
|
||||||
showExe.StringVar(&showExe.tag, flagTag, "", "Specify the name of the tag to use")
|
showExe.StringVar(&showExe.tag, flagTag, "", "Specify the name of the tag to use")
|
||||||
showExe.StringVar(&showExe.pollID, flagPollID, "", "Specify the ID of the poll to display")
|
showExe.StringVar(&showExe.pollID, flagPollID, "", "Specify the ID of the poll to display")
|
||||||
showExe.StringVar(&showExe.attachmentID, flagAttachmentID, "", "Specify the ID of the media attachment to display")
|
showExe.StringVar(&showExe.attachmentID, flagAttachmentID, "", "Specify the ID of the media attachment to display")
|
||||||
showExe.StringVar(&showExe.fromResourceType, flagFrom, "", "Specify the resource type to view the target resource from (e.g. status for viewing media from, etc)")
|
|
||||||
showExe.IntVar(&showExe.limit, flagLimit, 20, "Specify the limit of items to display")
|
showExe.IntVar(&showExe.limit, flagLimit, 20, "Specify the limit of items to display")
|
||||||
|
|
||||||
showExe.Usage = commandUsageFunc(name, summary, showExe.FlagSet)
|
showExe.Usage = commandUsageFunc(name, summary, showExe.FlagSet)
|
||||||
|
@ -90,7 +81,7 @@ func (s *ShowExecutor) Execute() error {
|
||||||
resourceFollowRequest: s.showFollowRequests,
|
resourceFollowRequest: s.showFollowRequests,
|
||||||
resourcePoll: s.showPoll,
|
resourcePoll: s.showPoll,
|
||||||
resourceMutedAccounts: s.showMutedAccounts,
|
resourceMutedAccounts: s.showMutedAccounts,
|
||||||
resourceMedia: s.showMedia,
|
resourceMedia: s.showMediaAttachment,
|
||||||
resourceMediaAttachment: s.showMediaAttachment,
|
resourceMediaAttachment: s.showMediaAttachment,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -430,83 +421,3 @@ func (s *ShowExecutor) showMediaAttachment(gtsClient *client.Client) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ShowExecutor) showMedia(gtsClient *client.Client) error {
|
|
||||||
if s.fromResourceType == "" {
|
|
||||||
return FlagNotSetError{flagText: flagFrom}
|
|
||||||
}
|
|
||||||
|
|
||||||
funcMap := map[string]func(*client.Client) error{
|
|
||||||
resourceStatus: s.showMediaFromStatus,
|
|
||||||
}
|
|
||||||
|
|
||||||
doFunc, ok := funcMap[s.fromResourceType]
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("do not support viewing media from %s", s.fromResourceType)
|
|
||||||
}
|
|
||||||
|
|
||||||
return doFunc(gtsClient)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *ShowExecutor) showMediaFromStatus(gtsClient *client.Client) error {
|
|
||||||
if s.attachmentID == "" {
|
|
||||||
return FlagNotSetError{flagText: flagAttachmentID}
|
|
||||||
}
|
|
||||||
|
|
||||||
if s.statusID == "" {
|
|
||||||
return FlagNotSetError{flagText: flagStatusID}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
status, err := gtsClient.GetStatus(s.statusID)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("unable to retrieve the status: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
attachmentExists := false
|
|
||||||
mediaURL := ""
|
|
||||||
mediaFilename := ""
|
|
||||||
|
|
||||||
for _, attachment := range status.MediaAttachments {
|
|
||||||
if attachment.ID == s.attachmentID {
|
|
||||||
mediaURL = attachment.URL
|
|
||||||
split := strings.Split(attachment.URL, "/")
|
|
||||||
mediaFilename = split[len(split)-1]
|
|
||||||
attachmentExists = true
|
|
||||||
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !attachmentExists {
|
|
||||||
return errors.New("this media is not attached to this status")
|
|
||||||
}
|
|
||||||
|
|
||||||
cacheDir := filepath.Join(
|
|
||||||
utilities.CalculateCacheDir(s.cacheRoot, utilities.GetFQDN(gtsClient.Authentication.Instance)),
|
|
||||||
"media",
|
|
||||||
)
|
|
||||||
|
|
||||||
if err := utilities.EnsureDirectory(cacheDir); err != nil {
|
|
||||||
return fmt.Errorf("unable to ensure the existence of the directory %q: %w", cacheDir, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
mediaFilePath := filepath.Join(cacheDir, mediaFilename)
|
|
||||||
|
|
||||||
fileExists, err := utilities.FileExists(mediaFilePath)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("unable to check if the media file is already downloaded: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !fileExists {
|
|
||||||
if err := utilities.DownloadFile(mediaURL, mediaFilePath); err != nil {
|
|
||||||
return fmt.Errorf("unable to download the media attachment: %w", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := utilities.OpenMedia(s.imageViewer, mediaFilePath); err != nil {
|
|
||||||
return fmt.Errorf("unable to open the image viewer: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,50 +0,0 @@
|
||||||
package utilities
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"codeflow.dananglin.me.uk/apollo/enbas/internal"
|
|
||||||
)
|
|
||||||
|
|
||||||
func CalculateConfigDir(configDir string) string {
|
|
||||||
if configDir != "" {
|
|
||||||
return configDir
|
|
||||||
}
|
|
||||||
|
|
||||||
configRoot, err := os.UserConfigDir()
|
|
||||||
if err != nil {
|
|
||||||
configRoot = "."
|
|
||||||
}
|
|
||||||
|
|
||||||
return filepath.Join(configRoot, internal.ApplicationName)
|
|
||||||
}
|
|
||||||
|
|
||||||
func CalculateCacheDir(cacheDir, instanceFQDN string) string {
|
|
||||||
if cacheDir != "" {
|
|
||||||
return cacheDir
|
|
||||||
}
|
|
||||||
|
|
||||||
cacheRoot, err := os.UserCacheDir()
|
|
||||||
if err != nil {
|
|
||||||
cacheRoot = "."
|
|
||||||
}
|
|
||||||
|
|
||||||
return filepath.Join(cacheRoot, internal.ApplicationName, instanceFQDN)
|
|
||||||
}
|
|
||||||
|
|
||||||
func EnsureDirectory(dir string) error {
|
|
||||||
if _, err := os.Stat(dir); err != nil {
|
|
||||||
if errors.Is(err, os.ErrNotExist) {
|
|
||||||
if err := os.MkdirAll(dir, 0o750); err != nil {
|
|
||||||
return fmt.Errorf("unable to create %s: %w", dir, err)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return fmt.Errorf("received an unknown error after getting the directory information: %w", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
|
@ -5,12 +5,8 @@
|
||||||
package utilities
|
package utilities
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
"os"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func ReadFile(path string) (string, error) {
|
func ReadFile(path string) (string, error) {
|
||||||
|
@ -21,45 +17,3 @@ func ReadFile(path string) (string, error) {
|
||||||
|
|
||||||
return string(data), nil
|
return string(data), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func FileExists(path string) (bool, error) {
|
|
||||||
if _, err := os.Stat(path); err != nil {
|
|
||||||
if errors.Is(err, os.ErrNotExist) {
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return false, fmt.Errorf("unable to check if the file exists: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: move to client package
|
|
||||||
func DownloadFile(url, path string) error {
|
|
||||||
client := http.Client{
|
|
||||||
CheckRedirect: func(r *http.Request, _ []*http.Request) error {
|
|
||||||
r.URL.Opaque = r.URL.Path
|
|
||||||
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
Timeout: time.Second * 30,
|
|
||||||
}
|
|
||||||
|
|
||||||
resp, err := client.Get(url)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("unable to download the file: %w", err)
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
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, resp.Body); err != nil {
|
|
||||||
return fmt.Errorf("unable to save the download to %s: %w", path, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,32 +0,0 @@
|
||||||
// SPDX-FileCopyrightText: 2024 Dan Anglin <d.n.i.anglin@gmail.com>
|
|
||||||
//
|
|
||||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
||||||
|
|
||||||
package utilities
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"os/exec"
|
|
||||||
"regexp"
|
|
||||||
)
|
|
||||||
|
|
||||||
func GetFQDN(url string) string {
|
|
||||||
r := regexp.MustCompile(`http(s)?:\/\/`)
|
|
||||||
|
|
||||||
return r.ReplaceAllString(url, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
func OpenMedia(viewer, path string) error {
|
|
||||||
if viewer == "" {
|
|
||||||
return errors.New("the image viewer is not specified")
|
|
||||||
}
|
|
||||||
|
|
||||||
command := exec.Command(viewer, path)
|
|
||||||
|
|
||||||
if err := command.Start(); err != nil {
|
|
||||||
return fmt.Errorf("received an error after starting the image viewer: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
Loading…
Reference in a new issue