Compare commits

...

3 commits

Author SHA1 Message Date
986407f728
checkpoint: remove default config dir; replace with err if user's congig home not found
All checks were successful
Tests / test (pull_request) Has been skipped
2024-08-18 07:22:32 +01:00
3c8633ff04
refactor: minimise the main package
All checks were successful
REUSE Compliance Check / check (push) Successful in 5s
Minimise the main package by moving all flag parsing to the Execute
function.
2024-08-18 07:18:57 +01:00
6e5e0c4c5a
fix(BREAKING): rename spoiler-text flag to summary
All checks were successful
REUSE Compliance Check / check (push) Successful in 5s
Rename the spoiler-text flag to summary as the text represents the
status' summary. The usage message and manual indicates that the text is
also known as the spoiler text or content warning.
2024-08-18 06:48:15 +01:00
10 changed files with 114 additions and 73 deletions

View file

@ -11,22 +11,46 @@ package executor
{{ print "" }}
{{ print "" }}
import "fmt"
import "flag"
import "os"
import "codeflow.dananglin.me.uk/apollo/enbas/internal/config"
import internalFlag "codeflow.dananglin.me.uk/apollo/enbas/internal/flag"
import "codeflow.dananglin.me.uk/apollo/enbas/internal/printer"
import "codeflow.dananglin.me.uk/apollo/enbas/internal/usage"
{{ print "" }}
{{ print "" }}
func Execute(
command string,
args []string,
noColor bool,
configDir string,
) error {
func Execute() error {
var (
configDir string
noColorFlag internalFlag.BoolPtrValue
noColor bool
enbasConfig *config.Config
enbasPrinter *printer.Printer
err error
)
flag.StringVar(&configDir, "config-dir", "", "The path to your configuration directory")
flag.Var(&noColorFlag, "no-color", "Set to true to disable ANSI colour output when displaying text on screen")
flag.Usage = usage.AppUsageFunc()
flag.Parse()
if flag.NArg() < 1 {
flag.Usage()
return nil
}
if noColorFlag.Value != nil {
noColor = *noColorFlag.Value
} else if os.Getenv("NO_COLOR") != "" {
noColor = true
}
command := flag.Arg(0)
args := flag.Args()[1:]
switch command {
case "init", "version":
enbasPrinter = printer.NewPrinter(noColor, "", 0)
@ -94,7 +118,7 @@ func execute(
exe, ok := executorMap[command]
if !ok {
return UnknownCommandError{Command: command}
return UnknownCommandError{command: command}
}
if err := exe.Parse(args); err != nil {

View file

@ -1,49 +1,13 @@
package main
import (
"flag"
"os"
"codeflow.dananglin.me.uk/apollo/enbas/internal/executor"
internalFlag "codeflow.dananglin.me.uk/apollo/enbas/internal/flag"
"codeflow.dananglin.me.uk/apollo/enbas/internal/usage"
)
func main() {
if err := run(); err != nil {
if err := executor.Execute(); err != nil {
os.Exit(1)
}
}
func run() error {
var (
configDir string
noColorFlag internalFlag.BoolPtrValue
)
flag.StringVar(&configDir, "config-dir", "", "Specify your config directory")
flag.Var(&noColorFlag, "no-color", "Disable ANSI colour output when displaying text on screen")
flag.Usage = usage.AppUsageFunc()
flag.Parse()
if flag.NArg() < 1 {
flag.Usage()
return nil
}
var noColor bool
if noColorFlag.Value != nil {
noColor = *noColorFlag.Value
} else if os.Getenv("NO_COLOR") != "" {
noColor = true
}
command := flag.Arg(0)
args := flag.Args()[1:]
return executor.Execute(command, args, noColor, configDir) //nolint:wrapcheck
}

View file

@ -517,7 +517,7 @@ Creates a new status.
| `media-description` | string | false | The description of the media attachment which will be used as the media's alt-text.<br>To use a description from a text file, use the `flag@` prefix followed by the path to the file (e.g. `file@description.txt`)<br>Use this flag multiple times to set multiple descriptions.| |
| `media-focus` | string | false | The media's focus values. This should be in the form of two comma-separated numbers between -1 and 1 (e.g. 0.25,-0.34).<br>Use this flag multiple times to set multiple focus values. | |
| `sensitive` | string | false | The status should be marked as sensitive.<br>If this is not specified then the default sensitivity from your posting preferences will be used. | |
| `spoiler-text` | string | false | The text to display as the status' warning or subject. | |
| `summary` | string | false | The summary of the status (a.k.a the subject, spoiler text or content warning). | |
| `visibility` | string | false | The visibility of the status.<br>Valid values are `public`, `private`, `unlisted`, `mutuals_only` and `direct`.<br>If this is not specified then the default visibility from your posting preferences will be used. | |
Additional flags for polls.

View file

@ -35,7 +35,10 @@ type Integrations struct {
}
func NewConfigFromFile(configDir string) (*Config, error) {
path := configFile(configDir)
path, err := configPath(configDir)
if err != nil {
return nil, fmt.Errorf("unable to calculate the path to your config file: %w", err)
}
file, err := os.Open(path)
if err != nil {
@ -53,13 +56,19 @@ func NewConfigFromFile(configDir string) (*Config, error) {
}
func FileExists(configDir string) (bool, error) {
path := configFile(configDir)
path, err := configPath(configDir)
if err != nil {
return false, fmt.Errorf("unable to calculate the path to your config file: %w", err)
}
return utilities.FileExists(path)
}
func SaveDefaultConfigToFile(configDir string) error {
path := configFile(configDir)
path, err := configPath(configDir)
if err != nil {
return fmt.Errorf("unable to calculate the path to your config file: %w", err)
}
file, err := os.Create(path)
if err != nil {
@ -69,7 +78,7 @@ func SaveDefaultConfigToFile(configDir string) error {
config := defaultConfig()
credentialsFilePath, err := utilities.AbsolutePath(defaultCredentialsConfigFile(configDir))
credentialsFilePath, err := defaultCredentialsConfigFile(configDir)
if err != nil {
return fmt.Errorf("unable to calculate the path to the credentials file: %w", err)
}
@ -86,8 +95,13 @@ func SaveDefaultConfigToFile(configDir string) error {
return nil
}
func configFile(configDir string) string {
return filepath.Join(utilities.CalculateConfigDir(configDir), configFileName)
func configPath(configDir string) (string, error) {
configDir, err := utilities.CalculateConfigDir(configDir)
if err != nil {
return "", fmt.Errorf("unable to get the config directory: %w", err)
}
return filepath.Join(configDir, configFileName), nil
}
func defaultConfig() Config {

View file

@ -38,9 +38,14 @@ func (e CredentialsNotFoundError) Error() string {
// directory. If the directory is not specified then the default directory is used. If the directory
// is not present, it will be created.
func SaveCredentials(filePath, username string, credentials Credentials) (string, error) {
directory := filepath.Dir(filePath)
part := filepath.Dir(filePath)
if err := utilities.EnsureDirectory(utilities.CalculateConfigDir(directory)); err != nil {
credentialsDir, err := utilities.CalculateConfigDir(part)
if err != nil {
fmt.Errorf("unable to calculate the directory to your credentials file: %w", err)
}
if err := utilities.EnsureDirectory(credentialsDir); err != nil {
return "", fmt.Errorf("unable to ensure the configuration directory: %w", err)
}
@ -128,6 +133,16 @@ func saveCredentialsConfigFile(authConfig CredentialsConfig, filePath string) er
return nil
}
func defaultCredentialsConfigFile(configDir string) string {
return filepath.Join(utilities.CalculateConfigDir(configDir), defaultCredentialsFileName)
func defaultCredentialsConfigFile(configDir string) (string, error) {
dir, err := utilities.CalculateConfigDir(configDir)
if err != nil {
return "", fmt.Errorf("unable to calculate the config directory: %w", err)
}
path, err := utilities.AbsolutePath(filepath.Join(dir, defaultCredentialsFileName))
if err != nil {
return "", fmt.Errorf("unable to get the absolute path to the credentials config file: %w", err)
}
return path, nil
}

View file

@ -191,7 +191,7 @@ func (c *CreateExecutor) createStatus(gtsClient *client.Client) error {
Content: content,
ContentType: parsedContentType,
Language: language,
SpoilerText: c.spoilerText,
SpoilerText: c.summary,
Boostable: c.boostable,
Federated: c.federated,
InReplyTo: c.inReplyTo,

View file

@ -6,24 +6,48 @@
package executor
import (
"flag"
"fmt"
"os"
"codeflow.dananglin.me.uk/apollo/enbas/internal/config"
internalFlag "codeflow.dananglin.me.uk/apollo/enbas/internal/flag"
"codeflow.dananglin.me.uk/apollo/enbas/internal/printer"
"codeflow.dananglin.me.uk/apollo/enbas/internal/usage"
)
func Execute(
command string,
args []string,
noColor bool,
configDir string,
) error {
func Execute() error {
var (
configDir string
noColorFlag internalFlag.BoolPtrValue
noColor bool
enbasConfig *config.Config
enbasPrinter *printer.Printer
err error
)
flag.StringVar(&configDir, "config-dir", "", "The path to your configuration directory")
flag.Var(&noColorFlag, "no-color", "Set to true to disable ANSI colour output when displaying text on screen")
flag.Usage = usage.AppUsageFunc()
flag.Parse()
if flag.NArg() < 1 {
flag.Usage()
return nil
}
if noColorFlag.Value != nil {
noColor = *noColorFlag.Value
} else if os.Getenv("NO_COLOR") != "" {
noColor = true
}
command := flag.Arg(0)
args := flag.Args()[1:]
switch command {
case "init", "version":
enbasPrinter = printer.NewPrinter(noColor, "", 0)

View file

@ -140,7 +140,7 @@ type CreateExecutor struct {
pollHidesVoteCounts bool
pollOptions internalFlag.StringSliceValue
sensitive internalFlag.BoolPtrValue
spoilerText string
summary string
resourceType string
visibility string
}
@ -184,7 +184,7 @@ func NewCreateExecutor(
exe.BoolVar(&exe.pollHidesVoteCounts, "poll-hides-vote-counts", false, "Set to true to hide the vote count until the poll is closed")
exe.Var(&exe.pollOptions, "poll-option", "A poll option. Use this multiple times to set multiple options")
exe.Var(&exe.sensitive, "sensitive", "Set to true if the status should be marked as sensitive")
exe.StringVar(&exe.spoilerText, "spoiler-text", "", "The subject, summary or content warning for the status")
exe.StringVar(&exe.summary, "summary", "", "The summary of the status (a.k.a the subject, spoiler text or content warning)")
exe.StringVar(&exe.resourceType, "type", "", "The type of resource you want to action on (e.g. account, status)")
exe.StringVar(&exe.visibility, "visibility", "", "The visibility of the posted status")

View file

@ -14,17 +14,17 @@ const (
cacheStatusesDir = "statuses"
)
func CalculateConfigDir(configDir string) string {
func CalculateConfigDir(configDir string) (string, error) {
if configDir != "" {
return configDir
return configDir, nil
}
configRoot, err := os.UserConfigDir()
if err != nil {
return filepath.Join(os.Getenv("HOME"), "."+internal.ApplicationName, "config")
return "", fmt.Errorf("unable to get your default config diretory: %w", err)
}
return filepath.Join(configRoot, internal.ApplicationName)
return filepath.Join(configRoot, internal.ApplicationName), nil
}
func CalculateMediaCacheDir(cacheRoot, instance string) (string, error) {

View file

@ -172,14 +172,14 @@
"type": "bool",
"description": "Set to true to skip showing your relationship to the account that you are viewing"
},
"spoiler-text": {
"type": "string",
"description": "The subject, summary or content warning for the status"
},
"status-id": {
"type": "string",
"description": "The ID of the status"
},
"summary": {
"type": "string",
"description": "The summary of the status (a.k.a the subject, spoiler text or content warning)"
},
"tag": {
"type": "string",
"description": "The name of the tag"
@ -265,7 +265,7 @@
{ "flag": "poll-hides-vote-counts", "default": "false" },
{ "flag": "poll-option", "fieldName": "pollOptions" },
{ "flag": "sensitive" },
{ "flag": "spoiler-text", "default": "" },
{ "flag": "summary", "default": "" },
{ "flag": "type", "fieldName": "resourceType", "default": "" },
{ "flag": "visibility", "default": "" }
],