fix: fixed the calculations in cache directories #53
12 changed files with 149 additions and 27 deletions
|
@ -15,6 +15,8 @@ path = [
|
||||||
"cmd/enbas-codegen/templates/**/*.go.gotmpl",
|
"cmd/enbas-codegen/templates/**/*.go.gotmpl",
|
||||||
"schema/enbas_cli_schema.json",
|
"schema/enbas_cli_schema.json",
|
||||||
".forgejo/workflows/*.yaml",
|
".forgejo/workflows/*.yaml",
|
||||||
|
".forgejo/actions/**/action.yaml",
|
||||||
|
".forgejo/actions/**/Dockerfile",
|
||||||
]
|
]
|
||||||
precedence = "override"
|
precedence = "override"
|
||||||
SPDX-FileCopyrightText = "2024 Dan Anglin <d.n.i.anglin@gmail.com>"
|
SPDX-FileCopyrightText = "2024 Dan Anglin <d.n.i.anglin@gmail.com>"
|
||||||
|
|
|
@ -24,11 +24,13 @@ func main() {
|
||||||
|
|
||||||
schema, err := newEnbasCLISchemaFromFile(enbasCLISchemaFilepath)
|
schema, err := newEnbasCLISchemaFromFile(enbasCLISchemaFilepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("ERROR: Unable to read the schema file: %v.\n", err)
|
fmt.Fprintf(os.Stderr, "ERROR: Unable to read the schema file: %v.\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := generateExecutors(schema, packageName); err != nil {
|
if err := generateExecutors(schema, packageName); err != nil {
|
||||||
fmt.Printf("ERROR: Unable to generate the executors: %v.\n", err)
|
fmt.Fprintf(os.Stderr, "ERROR: Unable to generate the executors: %v.\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -45,5 +45,5 @@ func run() error {
|
||||||
command := flag.Arg(0)
|
command := flag.Arg(0)
|
||||||
args := flag.Args()[1:]
|
args := flag.Args()[1:]
|
||||||
|
|
||||||
return executor.Execute(command, args, noColor, configDir)
|
return executor.Execute(command, args, noColor, configDir) //nolint:wrapcheck
|
||||||
}
|
}
|
||||||
|
|
|
@ -79,7 +79,7 @@ func getOtherAccounts(gtsClient *client.Client, accountNames internalFlag.String
|
||||||
numAccountNames := len(accountNames)
|
numAccountNames := len(accountNames)
|
||||||
accounts := make([]model.Account, numAccountNames)
|
accounts := make([]model.Account, numAccountNames)
|
||||||
|
|
||||||
for ind := 0; ind < numAccountNames; ind++ {
|
for ind := range numAccountNames {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
accounts[ind], err = gtsClient.GetAccount(accountNames[ind])
|
accounts[ind], err = gtsClient.GetAccount(accountNames[ind])
|
||||||
|
|
|
@ -99,7 +99,7 @@ func (c *CreateExecutor) createStatus(gtsClient *client.Client) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if descriptionsExists {
|
if descriptionsExists {
|
||||||
for ind := 0; ind < numMediaFiles; ind++ {
|
for ind := range numMediaFiles {
|
||||||
mediaDesc, err := utilities.ReadContents(c.mediaDescriptions[ind])
|
mediaDesc, err := utilities.ReadContents(c.mediaDescriptions[ind])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to read the contents from %s: %w", c.mediaDescriptions[ind], err)
|
return fmt.Errorf("unable to read the contents from %s: %w", c.mediaDescriptions[ind], err)
|
||||||
|
@ -109,7 +109,7 @@ func (c *CreateExecutor) createStatus(gtsClient *client.Client) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for ind := 0; ind < numMediaFiles; ind++ {
|
for ind := range numMediaFiles {
|
||||||
var (
|
var (
|
||||||
mediaFile string
|
mediaFile string
|
||||||
description string
|
description string
|
||||||
|
@ -156,7 +156,7 @@ func (c *CreateExecutor) createStatus(gtsClient *client.Client) error {
|
||||||
|
|
||||||
preferences, err := gtsClient.GetUserPreferences()
|
preferences, err := gtsClient.GetUserPreferences()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("WARNING: Unable to get your posting preferences: %w", err)
|
c.printer.PrintInfo("WARNING: Unable to get your posting preferences: " + err.Error() + ".\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.language != "" {
|
if c.language != "" {
|
||||||
|
@ -179,12 +179,12 @@ func (c *CreateExecutor) createStatus(gtsClient *client.Client) error {
|
||||||
|
|
||||||
parsedVisibility, err := model.ParseStatusVisibility(visibility)
|
parsedVisibility, err := model.ParseStatusVisibility(visibility)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err //nolint:wrapcheck
|
||||||
}
|
}
|
||||||
|
|
||||||
parsedContentType, err := model.ParseStatusContentType(c.contentType)
|
parsedContentType, err := model.ParseStatusContentType(c.contentType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err //nolint:wrapcheck
|
||||||
}
|
}
|
||||||
|
|
||||||
form := client.CreateStatusForm{
|
form := client.CreateStatusForm{
|
||||||
|
|
|
@ -72,13 +72,10 @@ func (d *DeleteExecutor) deleteStatus(gtsClient *client.Client) error {
|
||||||
d.printer.PrintSuccess("The status was successfully deleted.")
|
d.printer.PrintSuccess("The status was successfully deleted.")
|
||||||
|
|
||||||
if d.saveText {
|
if d.saveText {
|
||||||
cacheDir := filepath.Join(
|
cacheDir, err := utilities.CalculateStatusesCacheDir(d.config.CacheDirectory, gtsClient.Authentication.Instance)
|
||||||
utilities.CalculateCacheDir(
|
if err != nil {
|
||||||
d.config.CacheDirectory,
|
return fmt.Errorf("unable to get the cache directory for the status: %w", err)
|
||||||
utilities.GetFQDN(gtsClient.Authentication.Instance),
|
}
|
||||||
),
|
|
||||||
"statuses",
|
|
||||||
)
|
|
||||||
|
|
||||||
if err := utilities.EnsureDirectory(cacheDir); err != nil {
|
if err := utilities.EnsureDirectory(cacheDir); err != nil {
|
||||||
return fmt.Errorf("unable to ensure the existence of the directory %q: %w", cacheDir, err)
|
return fmt.Errorf("unable to ensure the existence of the directory %q: %w", cacheDir, err)
|
||||||
|
|
|
@ -2,7 +2,6 @@ package executor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/client"
|
||||||
"codeflow.dananglin.me.uk/apollo/enbas/internal/media"
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/media"
|
||||||
|
@ -462,10 +461,10 @@ func (s *ShowExecutor) showMediaFromStatus(gtsClient *client.Client) error {
|
||||||
return fmt.Errorf("unable to retrieve the status: %w", err)
|
return fmt.Errorf("unable to retrieve the status: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
cacheDir := filepath.Join(
|
cacheDir, err := utilities.CalculateMediaCacheDir(s.config.CacheDirectory, gtsClient.Authentication.Instance)
|
||||||
utilities.CalculateCacheDir(s.config.CacheDirectory, utilities.GetFQDN(gtsClient.Authentication.Instance)),
|
if err != nil {
|
||||||
"media",
|
return fmt.Errorf("unable to get the media cache directory: %w", err)
|
||||||
)
|
}
|
||||||
|
|
||||||
if err := utilities.EnsureDirectory(cacheDir); err != nil {
|
if err := utilities.EnsureDirectory(cacheDir); err != nil {
|
||||||
return fmt.Errorf("unable to ensure the existence of the directory %q: %w", cacheDir, err)
|
return fmt.Errorf("unable to ensure the existence of the directory %q: %w", cacheDir, err)
|
||||||
|
|
|
@ -9,6 +9,11 @@ import (
|
||||||
"codeflow.dananglin.me.uk/apollo/enbas/internal"
|
"codeflow.dananglin.me.uk/apollo/enbas/internal"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
cacheMediaDir = "media"
|
||||||
|
cacheStatusesDir = "statuses"
|
||||||
|
)
|
||||||
|
|
||||||
func CalculateConfigDir(configDir string) string {
|
func CalculateConfigDir(configDir string) string {
|
||||||
if configDir != "" {
|
if configDir != "" {
|
||||||
return configDir
|
return configDir
|
||||||
|
@ -22,17 +27,37 @@ func CalculateConfigDir(configDir string) string {
|
||||||
return filepath.Join(configRoot, internal.ApplicationName)
|
return filepath.Join(configRoot, internal.ApplicationName)
|
||||||
}
|
}
|
||||||
|
|
||||||
func CalculateCacheDir(cacheDir, instanceFQDN string) string {
|
func CalculateMediaCacheDir(cacheRoot, instance string) (string, error) {
|
||||||
if cacheDir != "" {
|
cacheDir, err := calculateCacheDir(cacheRoot, instance)
|
||||||
return cacheDir
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("unable to calculate the cache directory: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return filepath.Join(cacheDir, cacheMediaDir), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func CalculateStatusesCacheDir(cacheRoot, instance string) (string, error) {
|
||||||
|
cacheDir, err := calculateCacheDir(cacheRoot, instance)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("unable to calculate the cache directory: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return filepath.Join(cacheDir, cacheStatusesDir), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func calculateCacheDir(cacheRoot, instance string) (string, error) {
|
||||||
|
fqdn := GetFQDN(instance)
|
||||||
|
|
||||||
|
if cacheRoot != "" {
|
||||||
|
return filepath.Join(cacheRoot, fqdn), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
cacheRoot, err := os.UserCacheDir()
|
cacheRoot, err := os.UserCacheDir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return filepath.Join(os.Getenv("HOME"), "."+internal.ApplicationName, "cache")
|
return "", fmt.Errorf("unable to get your default cache directory: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return filepath.Join(cacheRoot, internal.ApplicationName, instanceFQDN)
|
return filepath.Join(cacheRoot, internal.ApplicationName, fqdn), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func EnsureDirectory(dir string) error {
|
func EnsureDirectory(dir string) error {
|
||||||
|
@ -42,7 +67,10 @@ func EnsureDirectory(dir string) error {
|
||||||
return fmt.Errorf("unable to create %s: %w", dir, err)
|
return fmt.Errorf("unable to create %s: %w", dir, err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return fmt.Errorf("received an unknown error after getting the directory information: %w", err)
|
return fmt.Errorf(
|
||||||
|
"received an unknown error after getting the directory information: %w",
|
||||||
|
err,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
78
internal/utilities/directories_test.go
Normal file
78
internal/utilities/directories_test.go
Normal file
|
@ -0,0 +1,78 @@
|
||||||
|
package utilities_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"codeflow.dananglin.me.uk/apollo/enbas/internal/utilities"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCalculateMediaCacheDir(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
projectDir, err := projectRoot()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Unable to get the project root directory: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cacheRoot := filepath.Join(projectDir, "test", "cache")
|
||||||
|
instance := "http://gotosocial.yellow-desert.social"
|
||||||
|
|
||||||
|
got, err := utilities.CalculateMediaCacheDir(cacheRoot, instance)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Unable to calculate the media cache directory: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
want := projectDir + "/test/cache/gotosocial.yellow-desert.social/media"
|
||||||
|
|
||||||
|
if got != want {
|
||||||
|
t.Errorf("Unexpected media cache directory calculated: want %s, got %s", want, got)
|
||||||
|
} else {
|
||||||
|
t.Logf("Expected media cache directory calculated: got %s", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCalculateMediaCacheDirWithXDG(t *testing.T) {
|
||||||
|
t.Setenv("XDG_CACHE_HOME", "/home/enbas/.cache")
|
||||||
|
|
||||||
|
cacheRoot := ""
|
||||||
|
instance := "https://gotosocial.yellow-desert.social"
|
||||||
|
|
||||||
|
got, err := utilities.CalculateMediaCacheDir(cacheRoot, instance)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Unable to calculate the media cache directory: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
want := "/home/enbas/.cache/enbas/gotosocial.yellow-desert.social/media"
|
||||||
|
|
||||||
|
if got != want {
|
||||||
|
t.Errorf("Unexpected media cache directory calculated: want %s, got %s", want, got)
|
||||||
|
} else {
|
||||||
|
t.Logf("Expected media cache directory calculated: got %s", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCalculateStatusesCacheDir(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
projectDir, err := projectRoot()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Unable to get the project root directory: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cacheRoot := filepath.Join(projectDir, "test", "cache")
|
||||||
|
instance := "https://fedi.blue-mammoth.party"
|
||||||
|
|
||||||
|
got, err := utilities.CalculateStatusesCacheDir(cacheRoot, instance)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Unable to calculate the statuses cache directory: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
want := projectDir + "/test/cache/fedi.blue-mammoth.party/statuses"
|
||||||
|
|
||||||
|
if got != want {
|
||||||
|
t.Errorf("Unexpected statuses cache directory calculated: want %s, got %s", want, got)
|
||||||
|
} else {
|
||||||
|
t.Logf("Expected statuses cache directory calculated: got %s", got)
|
||||||
|
}
|
||||||
|
}
|
16
internal/utilities/helpers_test.go
Normal file
16
internal/utilities/helpers_test.go
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
package utilities_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
func projectRoot() (string, error) {
|
||||||
|
cwd, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("unable to get the current working directory, %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return filepath.Join(cwd, "..", ".."), nil
|
||||||
|
}
|
0
test/cache/.gitkeep
vendored
Normal file
0
test/cache/.gitkeep
vendored
Normal file
0
test/config/.gitkeep
Normal file
0
test/config/.gitkeep
Normal file
Loading…
Reference in a new issue