enbas/cmd/enbas/main.go
Dan Anglin 77d782af21
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-25 04:49:14 +01:00

214 lines
5.1 KiB
Go

// SPDX-FileCopyrightText: 2024 Dan Anglin <d.n.i.anglin@gmail.com>
//
// SPDX-License-Identifier: GPL-3.0-or-later
package main
import (
"flag"
"fmt"
"os"
"strconv"
"codeflow.dananglin.me.uk/apollo/enbas/internal/config"
"codeflow.dananglin.me.uk/apollo/enbas/internal/executor"
"codeflow.dananglin.me.uk/apollo/enbas/internal/printer"
)
var (
binaryVersion string //nolint:gochecknoglobals
buildTime string //nolint:gochecknoglobals
goVersion string //nolint:gochecknoglobals
gitCommit string //nolint:gochecknoglobals
)
func main() {
if err := run(); err != nil {
os.Exit(1)
}
}
func run() error {
var (
configDir string
noColor *bool
)
flag.StringVar(&configDir, "config-dir", "", "Specify your config directory")
flag.BoolFunc("no-color", "Disable ANSI colour output when displaying text on screen", func(value string) error {
boolVal, err := strconv.ParseBool(value)
if err != nil {
return fmt.Errorf("unable to parse %q as a boolean: %w", value, err)
}
noColor = new(bool)
*noColor = boolVal
return nil
})
flag.Usage = usageFunc(executor.CommandSummaryMap())
flag.Parse()
if flag.NArg() < 1 {
flag.Usage()
return nil
}
// 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") != "" {
*noColor = true
} else {
*noColor = false
}
}
command := flag.Arg(0)
args := flag.Args()[1:]
config, err := config.NewConfigFromFile(configDir)
if err != nil {
printer := printer.NewPrinter(*noColor, "", 80)
printer.PrintFailure("unable to load the configuration: " + err.Error() + ".")
return err
}
printer := printer.NewPrinter(*noColor, config.Integrations.Pager, config.LineWrapMaxWidth)
executorMap := map[string]executor.Executor{
executor.CommandAccept: executor.NewAcceptOrRejectExecutor(
printer,
config,
executor.CommandAccept,
executor.CommandSummaryLookup(executor.CommandAccept),
),
executor.CommandAdd: executor.NewAddExecutor(
printer,
config,
executor.CommandAdd,
executor.CommandSummaryLookup(executor.CommandAdd),
),
executor.CommandBlock: executor.NewBlockOrUnblockExecutor(
printer,
config,
executor.CommandBlock,
executor.CommandSummaryLookup(executor.CommandBlock),
),
executor.CommandCreate: executor.NewCreateExecutor(
printer,
config,
executor.CommandCreate,
executor.CommandSummaryLookup(executor.CommandCreate),
),
executor.CommandDelete: executor.NewDeleteExecutor(
printer,
config,
executor.CommandDelete,
executor.CommandSummaryLookup(executor.CommandDelete),
),
executor.CommandEdit: executor.NewEditExecutor(
printer,
config,
executor.CommandEdit,
executor.CommandSummaryLookup(executor.CommandEdit),
),
executor.CommandFollow: executor.NewFollowOrUnfollowExecutor(
printer,
config,
executor.CommandFollow,
executor.CommandSummaryLookup(executor.CommandFollow),
),
executor.CommandLogin: executor.NewLoginExecutor(
printer,
config,
executor.CommandLogin,
executor.CommandSummaryLookup(executor.CommandLogin),
),
executor.CommandMute: executor.NewMuteOrUnmuteExecutor(
printer,
config,
executor.CommandMute,
executor.CommandSummaryLookup(executor.CommandMute),
),
executor.CommandReject: executor.NewAcceptOrRejectExecutor(
printer,
config,
executor.CommandReject,
executor.CommandSummaryLookup(executor.CommandReject),
),
executor.CommandRemove: executor.NewRemoveExecutor(
printer,
config,
executor.CommandRemove,
executor.CommandSummaryLookup(executor.CommandRemove),
),
executor.CommandSwitch: executor.NewSwitchExecutor(
printer,
config,
executor.CommandSwitch,
executor.CommandSummaryLookup(executor.CommandSwitch),
),
executor.CommandUnfollow: executor.NewFollowOrUnfollowExecutor(
printer,
config,
executor.CommandUnfollow,
executor.CommandSummaryLookup(executor.CommandUnfollow),
),
executor.CommandUnmute: executor.NewMuteOrUnmuteExecutor(
printer,
config,
executor.CommandUnmute,
executor.CommandSummaryLookup(executor.CommandUnmute),
),
executor.CommandUnblock: executor.NewBlockOrUnblockExecutor(
printer,
config,
executor.CommandUnblock,
executor.CommandSummaryLookup(executor.CommandUnblock),
),
executor.CommandShow: executor.NewShowExecutor(
printer,
config,
executor.CommandShow,
executor.CommandSummaryLookup(executor.CommandShow),
),
executor.CommandVersion: executor.NewVersionExecutor(
printer,
executor.CommandVersion,
executor.CommandSummaryLookup(executor.CommandVersion),
binaryVersion,
buildTime,
goVersion,
gitCommit,
),
executor.CommandWhoami: executor.NewWhoAmIExecutor(
printer,
config,
executor.CommandWhoami,
executor.CommandSummaryLookup(executor.CommandWhoami),
),
}
exe, ok := executorMap[command]
if !ok {
err := executor.UnknownCommandError{Command: command}
printer.PrintFailure(err.Error() + ".")
flag.Usage()
return err
}
if err := executor.Execute(exe, args); err != nil {
printer.PrintFailure("(" + command + ") " + err.Error() + ".")
return err
}
return nil
}