Compare commits

..

2 commits

Author SHA1 Message Date
beae4a72ab
checkpoint: implement config everywhere
- disable some linters
- all executors now uses the config value
- some variable renaming based on golangci-lint feedback
- add path to the credentials file to the config so users can define the
  path to the credentials file
2024-06-24 15:02:43 +01:00
f451ed22c3
checkpoint: add configuration 2024-06-24 09:01:28 +01:00
7 changed files with 36 additions and 127 deletions

View file

@ -57,8 +57,7 @@ func run() error {
return nil
}
// If NoColor is still unspecified,
// check to see if the NO_COLOR environment variable is set
// If NoColor is still unspecified, check to see if the NO_COLOR environment variable is set
if noColor == nil {
noColor = new(bool)
if os.Getenv("NO_COLOR") != "" {
@ -124,12 +123,6 @@ func run() error {
executor.CommandFollow,
executor.CommandSummaryLookup(executor.CommandFollow),
),
executor.CommandInit: executor.NewInitExecutor(
printer,
configDir,
executor.CommandInit,
executor.CommandSummaryLookup(executor.CommandInit),
),
executor.CommandLogin: executor.NewLoginExecutor(
printer,
config,

View file

@ -1,7 +1,3 @@
// SPDX-FileCopyrightText: 2024 Dan Anglin <d.n.i.anglin@gmail.com>
//
// SPDX-License-Identifier: GPL-3.0-or-later
package config
import (
@ -41,6 +37,17 @@ type Integrations struct {
func NewConfigFromFile(configDir string) (*Config, error) {
path := configFile(configDir)
fileExists, err := utilities.FileExists(path)
if err != nil {
return nil, fmt.Errorf("unable to check if the config file exists: %w", err)
}
if !fileExists {
if err := saveDefaultConfigToFile(configDir, path); err != nil {
return nil, fmt.Errorf("unable to save the default config to file: %w", err)
}
}
file, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("unable to open %s: %w", path, err)
@ -56,18 +63,14 @@ func NewConfigFromFile(configDir string) (*Config, error) {
return &config, nil
}
func FileExists(configDir string) (bool, error) {
path := configFile(configDir)
return utilities.FileExists(path)
func configFile(configDir string) string {
return filepath.Join(utilities.CalculateConfigDir(configDir), configFileName)
}
func SaveDefaultConfigToFile(configDir string) error {
path := configFile(configDir)
file, err := os.Create(path)
func saveDefaultConfigToFile(configDir, configPath string) error {
file, err := os.Create(configPath)
if err != nil {
return fmt.Errorf("unable to create the file at %s: %w", path, err)
return fmt.Errorf("unable to create the file at %s: %w", configPath, err)
}
defer file.Close()
@ -82,10 +85,6 @@ func SaveDefaultConfigToFile(configDir string) error {
return nil
}
func configFile(configDir string) string {
return filepath.Join(utilities.CalculateConfigDir(configDir), configFileName)
}
func defaultConfig(configDir string) Config {
credentialsFilePath := defaultCredentialsConfigFile(configDir)

View file

@ -12,7 +12,6 @@ const (
CommandDelete string = "delete"
CommandEdit string = "edit"
CommandFollow string = "follow"
CommandInit string = "init"
CommandLogin string = "login"
CommandMute string = "mute"
CommandReject string = "reject"
@ -32,7 +31,6 @@ const (
commandDeleteSummary string = "Delete a specific resource"
commandEditSummary string = "Edit a specific resource"
commandFollowSummary string = "Follow a resource (e.g. an account)"
commandInitSummary string = "Create a new configuration file in the specified configuration directory"
commandLoginSummary string = "Login to an account on GoToSocial"
commandMuteSummary string = "Mute a resource (e.g. an account)"
commandRejectSummary string = "Reject a request (e.g. a follow request)"
@ -55,7 +53,6 @@ func CommandSummaryMap() map[string]string {
CommandDelete: commandDeleteSummary,
CommandEdit: commandEditSummary,
CommandFollow: commandFollowSummary,
CommandInit: commandInitSummary,
CommandLogin: commandLoginSummary,
CommandMute: commandMuteSummary,
CommandReject: commandRejectSummary,

View file

@ -1,61 +0,0 @@
// SPDX-FileCopyrightText: 2024 Dan Anglin <d.n.i.anglin@gmail.com>
//
// SPDX-License-Identifier: GPL-3.0-or-later
package executor
import (
"flag"
"fmt"
"codeflow.dananglin.me.uk/apollo/enbas/internal/config"
"codeflow.dananglin.me.uk/apollo/enbas/internal/printer"
"codeflow.dananglin.me.uk/apollo/enbas/internal/utilities"
)
type InitExecutor struct {
*flag.FlagSet
printer *printer.Printer
configDir string
}
func NewInitExecutor(printer *printer.Printer, configDir, name, summary string) *InitExecutor {
initExe := InitExecutor{
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
printer: printer,
configDir: configDir,
}
initExe.Usage = commandUsageFunc(name, summary, initExe.FlagSet)
return &initExe
}
func (i *InitExecutor) Execute() error {
if err := utilities.EnsureDirectory(i.configDir); err != nil {
return fmt.Errorf("unable to ensure that the configuration directory is present: %w", err)
}
i.printer.PrintSuccess("The configuration directory is present.")
fileExists, err := config.FileExists(i.configDir)
if err != nil {
return fmt.Errorf("unable to check if the config file exists: %w", err)
}
if fileExists {
i.printer.PrintInfo("The configuration file is already present in " + i.configDir)
return nil
}
if err := config.SaveDefaultConfigToFile(i.configDir); err != nil {
return fmt.Errorf("unable to create a new configuration file in %s: %w", i.configDir, err)
}
i.printer.PrintSuccess("Successfully created a new configuration file in " + i.configDir)
return nil
}

View file

@ -6,7 +6,7 @@ package executor
import (
"flag"
"slices"
"fmt"
"strings"
)
@ -15,41 +15,27 @@ func commandUsageFunc(name, summary string, flagset *flag.FlagSet) func() {
return func() {
var builder strings.Builder
builder.WriteString("SUMMARY:")
builder.WriteString("\n " + name + " - " + summary)
builder.WriteString("\n\nUSAGE:")
builder.WriteString("\n enbas " + name)
flagMap := make(map[string]string)
fmt.Fprintf(
&builder,
"SUMMARY:\n %s - %s\n\nUSAGE:\n enbas %s [flags]\n\nFLAGS:",
name,
summary,
name,
)
flagset.VisitAll(func(f *flag.Flag) {
flagMap[f.Name] = f.Usage
fmt.Fprintf(
&builder,
"\n --%s\n %s",
f.Name,
f.Usage,
)
})
if len(flagMap) > 0 {
flags := make([]string, len(flagMap))
ind := 0
for f := range flagMap {
flags[ind] = f
ind++
}
slices.Sort(flags)
builder.WriteString(" [flags]")
builder.WriteString("\n\nFLAGS:")
for _, value := range flags {
builder.WriteString("\n --" + value)
builder.WriteString("\n " + flagMap[value])
}
}
builder.WriteString("\n")
w := flag.CommandLine.Output()
_, _ = w.Write([]byte(builder.String()))
fmt.Fprint(w, builder.String())
}
}

View file

@ -12,7 +12,6 @@ import (
type VersionExecutor struct {
*flag.FlagSet
printer *printer.Printer
showFullVersion bool
binaryVersion string
@ -22,7 +21,7 @@ type VersionExecutor struct {
}
func NewVersionExecutor(
printer *printer.Printer,
enbasPrinter *printer.Printer,
name,
summary,
binaryVersion,
@ -33,7 +32,7 @@ func NewVersionExecutor(
command := VersionExecutor{
FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
printer: printer,
printer: enbasPrinter,
binaryVersion: binaryVersion,
buildTime: buildTime,
goVersion: goVersion,

View file

@ -8,8 +8,6 @@ import (
"fmt"
"os/exec"
"regexp"
"slices"
"strings"
)
func GetFQDN(url string) string {
@ -29,12 +27,10 @@ func OpenMedia(viewer string, paths []string) error {
return UnspecifiedProgramError{}
}
cmd := slices.Concat(strings.Split(viewer, " "), paths)
command := exec.Command(cmd[0], cmd[1:]...) //nolint:gosec
command := exec.Command(viewer, paths...)
if err := command.Start(); err != nil {
return fmt.Errorf("received an error after starting the program: %w", err)
return fmt.Errorf("received an error after starting the image viewer: %w", err)
}
return nil