From de740de211da87cf52d67fb7f73892a5be9e8abe Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Mon, 12 Aug 2024 12:39:43 +0100 Subject: [PATCH] checkpoint: move build information to internal package - move the build information to the internal package to simplify the version executor. - add the Executor interface to internal/executor/executors.go --- cmd/enbas-codegen/templates.go | 7 ++ cmd/enbas/main.go | 134 +--------------------------- cmd/enbas/usage.go | 6 +- internal/executor/commands.go | 76 ++++++++-------- internal/executor/execute.go | 155 +++++++++++++++++++++++++++++++++ internal/executor/executor.go | 21 ----- internal/executor/executors.go | 26 +++--- internal/executor/version.go | 2 +- internal/printer/version.go | 14 +-- internal/version/version.go | 8 ++ magefiles/mage.go | 15 +++- schema/enbas_cli_schema.json | 7 +- 12 files changed, 246 insertions(+), 225 deletions(-) create mode 100644 internal/executor/execute.go delete mode 100644 internal/executor/executor.go create mode 100644 internal/version/version.go diff --git a/cmd/enbas-codegen/templates.go b/cmd/enbas-codegen/templates.go index 279c228..a7fd082 100644 --- a/cmd/enbas-codegen/templates.go +++ b/cmd/enbas-codegen/templates.go @@ -9,6 +9,13 @@ package executor {{ print "" }} {{ print "" }} import internalFlag "codeflow.dananglin.me.uk/apollo/enbas/internal/flag" +{{ print "" }} +{{ print "" }} +type Executor interface { + Name() string + Parse(args []string) error + Execute() error +} {{ range $name, $command := . }} {{- $struct_name := capitalise $name | printf "%sExecutor" -}} {{- $new_executor_function_name := capitalise $name | printf "New%sExecutor" -}} diff --git a/cmd/enbas/main.go b/cmd/enbas/main.go index 6d3f18f..456ffd4 100644 --- a/cmd/enbas/main.go +++ b/cmd/enbas/main.go @@ -4,17 +4,8 @@ import ( "flag" "os" - "codeflow.dananglin.me.uk/apollo/enbas/internal/config" "codeflow.dananglin.me.uk/apollo/enbas/internal/executor" internalFlag "codeflow.dananglin.me.uk/apollo/enbas/internal/flag" - "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() { @@ -53,128 +44,5 @@ func run() error { command := flag.Arg(0) args := flag.Args()[1:] - var ( - enbasConfig *config.Config - enbasPrinter *printer.Printer - err error - ) - - switch command { - case executor.CommandInit, executor.CommandVersion: - enbasPrinter = printer.NewPrinter(noColor, "", 0) - default: - enbasConfig, err = config.NewConfigFromFile(configDir) - if err != nil { - enbasPrinter = printer.NewPrinter(noColor, "", 0) - enbasPrinter.PrintFailure("unable to load the configuration: " + err.Error() + ".") - - return err - } - - enbasPrinter = printer.NewPrinter( - noColor, - enbasConfig.Integrations.Pager, - enbasConfig.LineWrapMaxWidth, - ) - } - - executorMap := map[string]executor.Executor{ - executor.CommandAccept: executor.NewAcceptExecutor( - enbasPrinter, - enbasConfig, - ), - executor.CommandAdd: executor.NewAddExecutor( - enbasPrinter, - enbasConfig, - ), - executor.CommandBlock: executor.NewBlockExecutor( - enbasPrinter, - enbasConfig, - ), - executor.CommandCreate: executor.NewCreateExecutor( - enbasPrinter, - enbasConfig, - ), - executor.CommandDelete: executor.NewDeleteExecutor( - enbasPrinter, - enbasConfig, - ), - executor.CommandEdit: executor.NewEditExecutor( - enbasPrinter, - enbasConfig, - ), - executor.CommandFollow: executor.NewFollowExecutor( - enbasPrinter, - enbasConfig, - ), - executor.CommandInit: executor.NewInitExecutor( - enbasPrinter, - configDir, - ), - executor.CommandLogin: executor.NewLoginExecutor( - enbasPrinter, - enbasConfig, - ), - executor.CommandMute: executor.NewMuteExecutor( - enbasPrinter, - enbasConfig, - ), - executor.CommandReject: executor.NewRejectExecutor( - enbasPrinter, - enbasConfig, - ), - executor.CommandRemove: executor.NewRemoveExecutor( - enbasPrinter, - enbasConfig, - ), - executor.CommandSwitch: executor.NewSwitchExecutor( - enbasPrinter, - enbasConfig, - ), - executor.CommandUnfollow: executor.NewUnfollowExecutor( - enbasPrinter, - enbasConfig, - ), - executor.CommandUnmute: executor.NewUnmuteExecutor( - enbasPrinter, - enbasConfig, - ), - executor.CommandUnblock: executor.NewUnblockExecutor( - enbasPrinter, - enbasConfig, - ), - executor.CommandShow: executor.NewShowExecutor( - enbasPrinter, - enbasConfig, - ), - executor.CommandVersion: executor.NewVersionExecutor( - enbasPrinter, - binaryVersion, - buildTime, - goVersion, - gitCommit, - ), - executor.CommandWhoami: executor.NewWhoamiExecutor( - enbasPrinter, - enbasConfig, - ), - } - - exe, ok := executorMap[command] - if !ok { - err = executor.UnknownCommandError{Command: command} - - enbasPrinter.PrintFailure(err.Error() + ".") - flag.Usage() - - return err - } - - if err = executor.Execute(exe, args); err != nil { - enbasPrinter.PrintFailure("(" + command + ") " + err.Error() + ".") - - return err - } - - return nil + return executor.Execute(command, args, noColor, configDir) } diff --git a/cmd/enbas/usage.go b/cmd/enbas/usage.go index 48a6332..d5deadf 100644 --- a/cmd/enbas/usage.go +++ b/cmd/enbas/usage.go @@ -6,6 +6,8 @@ import ( "slices" "strings" "text/tabwriter" + + "codeflow.dananglin.me.uk/apollo/enbas/internal/version" ) func usageFunc(summaries map[string]string) func() { @@ -24,8 +26,8 @@ func usageFunc(summaries map[string]string) func() { builder.WriteString("SUMMARY:\n enbas - A GoToSocial client for the terminal.\n\n") - if binaryVersion != "" { - builder.WriteString("VERSION:\n " + binaryVersion + "\n\n") + if version.BinaryVersion != "" { + builder.WriteString("VERSION:\n " + version.BinaryVersion + "\n\n") } builder.WriteString("USAGE:\n enbas [flags]\n enbas [flags] [command]\n\nCOMMANDS:") diff --git a/internal/executor/commands.go b/internal/executor/commands.go index 6ca174e..6123c72 100644 --- a/internal/executor/commands.go +++ b/internal/executor/commands.go @@ -1,25 +1,25 @@ package executor const ( - CommandAccept string = "accept" - CommandAdd string = "add" - CommandBlock string = "block" - CommandCreate string = "create" - CommandDelete string = "delete" - CommandEdit string = "edit" - CommandFollow string = "follow" - CommandInit string = "init" - CommandLogin string = "login" - CommandMute string = "mute" - CommandReject string = "reject" - CommandRemove string = "remove" - CommandShow string = "show" - CommandSwitch string = "switch" - CommandUnblock string = "unblock" - CommandUnfollow string = "unfollow" - CommandUnmute string = "unmute" - CommandVersion string = "version" - CommandWhoami string = "whoami" + commandAccept string = "accept" + commandAdd string = "add" + commandBlock string = "block" + commandCreate string = "create" + commandDelete string = "delete" + commandEdit string = "edit" + commandFollow string = "follow" + commandInit string = "init" + commandLogin string = "login" + commandMute string = "mute" + commandReject string = "reject" + commandRemove string = "remove" + commandShow string = "show" + commandSwitch string = "switch" + commandUnblock string = "unblock" + commandUnfollow string = "unfollow" + commandUnmute string = "unmute" + commandVersion string = "version" + commandWhoami string = "whoami" commandAcceptSummary string = "Accept a request (e.g. a follow request)" commandAddSummary string = "Add a resource to another resource" @@ -44,25 +44,25 @@ const ( func CommandSummaryMap() map[string]string { return map[string]string{ - CommandAccept: commandAcceptSummary, - CommandAdd: commandAddSummary, - CommandBlock: commandBlockSummary, - CommandCreate: commandCreateSummary, - CommandDelete: commandDeleteSummary, - CommandEdit: commandEditSummary, - CommandFollow: commandFollowSummary, - CommandInit: commandInitSummary, - CommandLogin: commandLoginSummary, - CommandMute: commandMuteSummary, - CommandReject: commandRejectSummary, - CommandRemove: commandRemoveSummary, - CommandShow: commandShowSummary, - CommandSwitch: commandSwitchSummary, - CommandUnblock: commandUnblockSummary, - CommandUnfollow: commandUnfollowSummary, - CommandUnmute: commandUnmuteSummary, - CommandVersion: commandVersionSummary, - CommandWhoami: commandWhoamiSummary, + commandAccept: commandAcceptSummary, + commandAdd: commandAddSummary, + commandBlock: commandBlockSummary, + commandCreate: commandCreateSummary, + commandDelete: commandDeleteSummary, + commandEdit: commandEditSummary, + commandFollow: commandFollowSummary, + commandInit: commandInitSummary, + commandLogin: commandLoginSummary, + commandMute: commandMuteSummary, + commandReject: commandRejectSummary, + commandRemove: commandRemoveSummary, + commandShow: commandShowSummary, + commandSwitch: commandSwitchSummary, + commandUnblock: commandUnblockSummary, + commandUnfollow: commandUnfollowSummary, + commandUnmute: commandUnmuteSummary, + commandVersion: commandVersionSummary, + commandWhoami: commandWhoamiSummary, } } diff --git a/internal/executor/execute.go b/internal/executor/execute.go new file mode 100644 index 0000000..fe4b2ab --- /dev/null +++ b/internal/executor/execute.go @@ -0,0 +1,155 @@ +package executor + +import ( + "fmt" + + "codeflow.dananglin.me.uk/apollo/enbas/internal/config" + "codeflow.dananglin.me.uk/apollo/enbas/internal/printer" +) + +func Execute( + command string, + args []string, + noColor bool, + configDir string, +) error { + var ( + enbasConfig *config.Config + enbasPrinter *printer.Printer + err error + ) + + switch command { + case commandInit, commandVersion: + enbasPrinter = printer.NewPrinter(noColor, "", 0) + default: + enbasConfig, err = config.NewConfigFromFile(configDir) + if err != nil { + enbasPrinter = printer.NewPrinter(noColor, "", 0) + enbasPrinter.PrintFailure("unable to load the configuration: " + err.Error() + ".") + + return err + } + + enbasPrinter = printer.NewPrinter( + noColor, + enbasConfig.Integrations.Pager, + enbasConfig.LineWrapMaxWidth, + ) + } + + if err = execute( + command, + args, + enbasPrinter, + enbasConfig, + configDir, + ); err != nil { + enbasPrinter.PrintFailure("(" + command + ") " + err.Error() + ".") + + return err + } + + return nil +} + +func execute( + command string, + args []string, + enbasPrinter *printer.Printer, + enbasConfig *config.Config, + configDir string, +) error { + executorMap := map[string]Executor{ + commandAccept: NewAcceptExecutor( + enbasPrinter, + enbasConfig, + ), + commandAdd: NewAddExecutor( + enbasPrinter, + enbasConfig, + ), + commandBlock: NewBlockExecutor( + enbasPrinter, + enbasConfig, + ), + commandCreate: NewCreateExecutor( + enbasPrinter, + enbasConfig, + ), + commandDelete: NewDeleteExecutor( + enbasPrinter, + enbasConfig, + ), + commandEdit: NewEditExecutor( + enbasPrinter, + enbasConfig, + ), + commandFollow: NewFollowExecutor( + enbasPrinter, + enbasConfig, + ), + commandInit: NewInitExecutor( + enbasPrinter, + configDir, + ), + commandLogin: NewLoginExecutor( + enbasPrinter, + enbasConfig, + ), + commandMute: NewMuteExecutor( + enbasPrinter, + enbasConfig, + ), + commandReject: NewRejectExecutor( + enbasPrinter, + enbasConfig, + ), + commandRemove: NewRemoveExecutor( + enbasPrinter, + enbasConfig, + ), + commandSwitch: NewSwitchExecutor( + enbasPrinter, + enbasConfig, + ), + commandUnfollow: NewUnfollowExecutor( + enbasPrinter, + enbasConfig, + ), + commandUnmute: NewUnmuteExecutor( + enbasPrinter, + enbasConfig, + ), + commandUnblock: NewUnblockExecutor( + enbasPrinter, + enbasConfig, + ), + commandShow: NewShowExecutor( + enbasPrinter, + enbasConfig, + ), + commandVersion: NewVersionExecutor( + enbasPrinter, + ), + commandWhoami: NewWhoamiExecutor( + enbasPrinter, + enbasConfig, + ), + } + + exe, ok := executorMap[command] + if !ok { + return UnknownCommandError{Command: command} + } + + if err := exe.Parse(args); err != nil { + return fmt.Errorf("flag parsing error: %w", err) + } + + if err := exe.Execute(); err != nil { + return fmt.Errorf("execution error: %w", err) + } + + return nil +} diff --git a/internal/executor/executor.go b/internal/executor/executor.go deleted file mode 100644 index 81d73e9..0000000 --- a/internal/executor/executor.go +++ /dev/null @@ -1,21 +0,0 @@ -package executor - -import "fmt" - -type Executor interface { - Name() string - Parse(args []string) error - Execute() error -} - -func Execute(executor Executor, args []string) error { - if err := executor.Parse(args); err != nil { - return fmt.Errorf("flag parsing error: %w", err) - } - - if err := executor.Execute(); err != nil { - return fmt.Errorf("execution error: %w", err) - } - - return nil -} diff --git a/internal/executor/executors.go b/internal/executor/executors.go index 44a644e..43cca40 100644 --- a/internal/executor/executors.go +++ b/internal/executor/executors.go @@ -13,6 +13,12 @@ import ( "codeflow.dananglin.me.uk/apollo/enbas/internal/printer" ) +type Executor interface { + Name() string + Parse(args []string) error + Execute() error +} + // AcceptExecutor is the executor for the accept command. type AcceptExecutor struct { *flag.FlagSet @@ -592,28 +598,16 @@ func NewUnmuteExecutor( // VersionExecutor is the executor for the version command. type VersionExecutor struct { *flag.FlagSet - printer *printer.Printer - full bool - binaryVersion string - buildTime string - goVersion string - gitCommit string + printer *printer.Printer + full bool } func NewVersionExecutor( printer *printer.Printer, - binaryVersion string, - buildTime string, - goVersion string, - gitCommit string, ) *VersionExecutor { exe := VersionExecutor{ - FlagSet: flag.NewFlagSet("version", flag.ExitOnError), - printer: printer, - binaryVersion: binaryVersion, - buildTime: buildTime, - goVersion: goVersion, - gitCommit: gitCommit, + FlagSet: flag.NewFlagSet("version", flag.ExitOnError), + printer: printer, } exe.Usage = commandUsageFunc("version", "Prints the application's version and build information", exe.FlagSet) diff --git a/internal/executor/version.go b/internal/executor/version.go index 59df58a..6fbd357 100644 --- a/internal/executor/version.go +++ b/internal/executor/version.go @@ -1,7 +1,7 @@ package executor func (v *VersionExecutor) Execute() error { - v.printer.PrintVersion(v.full, v.binaryVersion, v.buildTime, v.goVersion, v.gitCommit) + v.printer.PrintVersion(v.full) return nil } diff --git a/internal/printer/version.go b/internal/printer/version.go index 93e9c5e..430624d 100644 --- a/internal/printer/version.go +++ b/internal/printer/version.go @@ -3,11 +3,13 @@ package printer import ( "strings" "text/tabwriter" + + "codeflow.dananglin.me.uk/apollo/enbas/internal/version" ) -func (p Printer) PrintVersion(showFullVersion bool, binaryVersion, buildTime, goVersion, gitCommit string) { +func (p Printer) PrintVersion(showFullVersion bool) { if !showFullVersion { - printToStdout("Enbas " + binaryVersion + "\n") + printToStdout("Enbas " + version.BinaryVersion + "\n") return } @@ -18,10 +20,10 @@ func (p Printer) PrintVersion(showFullVersion bool, binaryVersion, buildTime, go tableWriter := tabwriter.NewWriter(&builder, 0, 4, 1, ' ', 0) - _, _ = tableWriter.Write([]byte(p.fieldFormat("Version:") + "\t" + binaryVersion + "\n")) - _, _ = tableWriter.Write([]byte(p.fieldFormat("Git commit:") + "\t" + gitCommit + "\n")) - _, _ = tableWriter.Write([]byte(p.fieldFormat("Go version:") + "\t" + goVersion + "\n")) - _, _ = tableWriter.Write([]byte(p.fieldFormat("Build date:") + "\t" + buildTime + "\n")) + _, _ = tableWriter.Write([]byte(p.fieldFormat("Version:") + "\t" + version.BinaryVersion + "\n")) + _, _ = tableWriter.Write([]byte(p.fieldFormat("Git commit:") + "\t" + version.GitCommit + "\n")) + _, _ = tableWriter.Write([]byte(p.fieldFormat("Go version:") + "\t" + version.GoVersion + "\n")) + _, _ = tableWriter.Write([]byte(p.fieldFormat("Build date:") + "\t" + version.BuildTime + "\n")) tableWriter.Flush() diff --git a/internal/version/version.go b/internal/version/version.go new file mode 100644 index 0000000..65ed05f --- /dev/null +++ b/internal/version/version.go @@ -0,0 +1,8 @@ +package version + +var ( + BinaryVersion string //nolint:gochecknoglobals + BuildTime string //nolint:gochecknoglobals + GoVersion string //nolint:gochecknoglobals + GitCommit string //nolint:gochecknoglobals +) diff --git a/magefiles/mage.go b/magefiles/mage.go index 55ab68b..7f861c1 100644 --- a/magefiles/mage.go +++ b/magefiles/mage.go @@ -110,10 +110,21 @@ func Clean() error { // ldflags returns the build flags. func ldflags() string { - ldflagsfmt := "-s -w -X main.binaryVersion=%s -X main.gitCommit=%s -X main.goVersion=%s -X main.buildTime=%s" + versionPackage := "codeflow.dananglin.me.uk/apollo/enbas/internal/version" + binaryVersionVar := versionPackage + "." + "BinaryVersion" + gitCommitVar := versionPackage + "." + "GitCommit" + goVersionVar := versionPackage + "." + "GoVersion" + buildTimeVar := versionPackage + "." + "BuildTime" + ldflagsfmt := "-s -w -X %s=%s -X %s=%s -X %s=%s -X %s=%s" buildTime := time.Now().UTC().Format(time.RFC3339) - return fmt.Sprintf(ldflagsfmt, version(), gitCommit(), runtime.Version(), buildTime) + return fmt.Sprintf( + ldflagsfmt, + binaryVersionVar, version(), + gitCommitVar, gitCommit(), + goVersionVar, runtime.Version(), + buildTimeVar, buildTime, + ) } // version returns the latest git tag using git describe. diff --git a/schema/enbas_cli_schema.json b/schema/enbas_cli_schema.json index 0e631d5..c1eb52f 100644 --- a/schema/enbas_cli_schema.json +++ b/schema/enbas_cli_schema.json @@ -421,12 +421,7 @@ "usePrinter": true }, "version": { - "additionalFields": [ - { "name": "binaryVersion", "type": "string"}, - { "name": "buildTime", "type": "string"}, - { "name": "goVersion", "type": "string"}, - { "name": "gitCommit", "type": "string"} - ], + "additionalFields": [], "flags": [ { "flag": "full", "default": "false" } ],