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 return nil
} }
// If NoColor is still unspecified, // If NoColor is still unspecified, check to see if the NO_COLOR environment variable is set
// check to see if the NO_COLOR environment variable is set
if noColor == nil { if noColor == nil {
noColor = new(bool) noColor = new(bool)
if os.Getenv("NO_COLOR") != "" { if os.Getenv("NO_COLOR") != "" {
@ -124,12 +123,6 @@ func run() error {
executor.CommandFollow, executor.CommandFollow,
executor.CommandSummaryLookup(executor.CommandFollow), executor.CommandSummaryLookup(executor.CommandFollow),
), ),
executor.CommandInit: executor.NewInitExecutor(
printer,
configDir,
executor.CommandInit,
executor.CommandSummaryLookup(executor.CommandInit),
),
executor.CommandLogin: executor.NewLoginExecutor( executor.CommandLogin: executor.NewLoginExecutor(
printer, printer,
config, 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 package config
import ( import (
@ -41,6 +37,17 @@ type Integrations struct {
func NewConfigFromFile(configDir string) (*Config, error) { func NewConfigFromFile(configDir string) (*Config, error) {
path := configFile(configDir) 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) file, err := os.Open(path)
if err != nil { if err != nil {
return nil, fmt.Errorf("unable to open %s: %w", path, err) return nil, fmt.Errorf("unable to open %s: %w", path, err)
@ -56,18 +63,14 @@ func NewConfigFromFile(configDir string) (*Config, error) {
return &config, nil return &config, nil
} }
func FileExists(configDir string) (bool, error) { func configFile(configDir string) string {
path := configFile(configDir) return filepath.Join(utilities.CalculateConfigDir(configDir), configFileName)
return utilities.FileExists(path)
} }
func SaveDefaultConfigToFile(configDir string) error { func saveDefaultConfigToFile(configDir, configPath string) error {
path := configFile(configDir) file, err := os.Create(configPath)
file, err := os.Create(path)
if err != nil { 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() defer file.Close()
@ -82,10 +85,6 @@ func SaveDefaultConfigToFile(configDir string) error {
return nil return nil
} }
func configFile(configDir string) string {
return filepath.Join(utilities.CalculateConfigDir(configDir), configFileName)
}
func defaultConfig(configDir string) Config { func defaultConfig(configDir string) Config {
credentialsFilePath := defaultCredentialsConfigFile(configDir) credentialsFilePath := defaultCredentialsConfigFile(configDir)

View file

@ -12,7 +12,6 @@ const (
CommandDelete string = "delete" CommandDelete string = "delete"
CommandEdit string = "edit" CommandEdit string = "edit"
CommandFollow string = "follow" CommandFollow string = "follow"
CommandInit string = "init"
CommandLogin string = "login" CommandLogin string = "login"
CommandMute string = "mute" CommandMute string = "mute"
CommandReject string = "reject" CommandReject string = "reject"
@ -32,7 +31,6 @@ const (
commandDeleteSummary string = "Delete a specific resource" commandDeleteSummary string = "Delete a specific resource"
commandEditSummary string = "Edit a specific resource" commandEditSummary string = "Edit a specific resource"
commandFollowSummary string = "Follow a resource (e.g. an account)" 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" commandLoginSummary string = "Login to an account on GoToSocial"
commandMuteSummary string = "Mute a resource (e.g. an account)" commandMuteSummary string = "Mute a resource (e.g. an account)"
commandRejectSummary string = "Reject a request (e.g. a follow request)" commandRejectSummary string = "Reject a request (e.g. a follow request)"
@ -55,7 +53,6 @@ func CommandSummaryMap() map[string]string {
CommandDelete: commandDeleteSummary, CommandDelete: commandDeleteSummary,
CommandEdit: commandEditSummary, CommandEdit: commandEditSummary,
CommandFollow: commandFollowSummary, CommandFollow: commandFollowSummary,
CommandInit: commandInitSummary,
CommandLogin: commandLoginSummary, CommandLogin: commandLoginSummary,
CommandMute: commandMuteSummary, CommandMute: commandMuteSummary,
CommandReject: commandRejectSummary, 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 ( import (
"flag" "flag"
"slices" "fmt"
"strings" "strings"
) )
@ -15,41 +15,27 @@ func commandUsageFunc(name, summary string, flagset *flag.FlagSet) func() {
return func() { return func() {
var builder strings.Builder var builder strings.Builder
builder.WriteString("SUMMARY:") fmt.Fprintf(
builder.WriteString("\n " + name + " - " + summary) &builder,
builder.WriteString("\n\nUSAGE:") "SUMMARY:\n %s - %s\n\nUSAGE:\n enbas %s [flags]\n\nFLAGS:",
builder.WriteString("\n enbas " + name) name,
summary,
flagMap := make(map[string]string) name,
)
flagset.VisitAll(func(f *flag.Flag) { 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") builder.WriteString("\n")
w := flag.CommandLine.Output() w := flag.CommandLine.Output()
_, _ = w.Write([]byte(builder.String())) fmt.Fprint(w, builder.String())
} }
} }

View file

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

View file

@ -8,8 +8,6 @@ import (
"fmt" "fmt"
"os/exec" "os/exec"
"regexp" "regexp"
"slices"
"strings"
) )
func GetFQDN(url string) string { func GetFQDN(url string) string {
@ -29,12 +27,10 @@ func OpenMedia(viewer string, paths []string) error {
return UnspecifiedProgramError{} return UnspecifiedProgramError{}
} }
cmd := slices.Concat(strings.Split(viewer, " "), paths) command := exec.Command(viewer, paths...)
command := exec.Command(cmd[0], cmd[1:]...) //nolint:gosec
if err := command.Start(); err != nil { 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 return nil