new init command

This commit is contained in:
Dan Anglin 2024-06-25 07:14:44 +01:00
parent 77d782af21
commit f678273544
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
6 changed files with 121 additions and 34 deletions

View file

@ -57,7 +57,8 @@ func run() error {
return nil 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 { if noColor == nil {
noColor = new(bool) noColor = new(bool)
if os.Getenv("NO_COLOR") != "" { if os.Getenv("NO_COLOR") != "" {
@ -123,6 +124,12 @@ 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,3 +1,7 @@
// 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 (
@ -37,17 +41,6 @@ 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)
@ -63,14 +56,18 @@ func NewConfigFromFile(configDir string) (*Config, error) {
return &config, nil return &config, nil
} }
func configFile(configDir string) string { func FileExists(configDir string) (bool, error) {
return filepath.Join(utilities.CalculateConfigDir(configDir), configFileName) path := configFile(configDir)
return utilities.FileExists(path)
} }
func saveDefaultConfigToFile(configDir, configPath string) error { func SaveDefaultConfigToFile(configDir string) error {
file, err := os.Create(configPath) path := configFile(configDir)
file, err := os.Create(path)
if err != nil { if err != nil {
return fmt.Errorf("unable to create the file at %s: %w", configPath, err) return fmt.Errorf("unable to create the file at %s: %w", path, err)
} }
defer file.Close() defer file.Close()
@ -85,6 +82,10 @@ func saveDefaultConfigToFile(configDir, configPath 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,6 +12,7 @@ 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"
@ -31,6 +32,7 @@ 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)"
@ -53,6 +55,7 @@ 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,

61
internal/executor/init.go Normal file
View file

@ -0,0 +1,61 @@
// 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"
"fmt" "slices"
"strings" "strings"
) )
@ -15,27 +15,41 @@ func commandUsageFunc(name, summary string, flagset *flag.FlagSet) func() {
return func() { return func() {
var builder strings.Builder var builder strings.Builder
fmt.Fprintf( builder.WriteString("SUMMARY:")
&builder, builder.WriteString("\n " + name + " - " + summary)
"SUMMARY:\n %s - %s\n\nUSAGE:\n enbas %s [flags]\n\nFLAGS:", builder.WriteString("\n\nUSAGE:")
name, builder.WriteString("\n enbas " + name)
summary,
name, flagMap := make(map[string]string)
)
flagset.VisitAll(func(f *flag.Flag) { flagset.VisitAll(func(f *flag.Flag) {
fmt.Fprintf( flagMap[f.Name] = f.Usage
&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()
fmt.Fprint(w, builder.String()) _, _ = w.Write([]byte(builder.String()))
} }
} }

View file

@ -12,6 +12,7 @@ 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
@ -21,7 +22,7 @@ type VersionExecutor struct {
} }
func NewVersionExecutor( func NewVersionExecutor(
enbasPrinter *printer.Printer, printer *printer.Printer,
name, name,
summary, summary,
binaryVersion, binaryVersion,
@ -32,7 +33,7 @@ func NewVersionExecutor(
command := VersionExecutor{ command := VersionExecutor{
FlagSet: flag.NewFlagSet(name, flag.ExitOnError), FlagSet: flag.NewFlagSet(name, flag.ExitOnError),
printer: enbasPrinter, printer: printer,
binaryVersion: binaryVersion, binaryVersion: binaryVersion,
buildTime: buildTime, buildTime: buildTime,
goVersion: goVersion, goVersion: goVersion,