From 76122eec215ffe9af4c36f04ac3ae5058e36532b Mon Sep 17 00:00:00 2001 From: Valentin Kiselev Date: Tue, 23 Jul 2024 10:34:40 +0300 Subject: [PATCH] chore: simplify the look and feel of commands --- .goreleaser.yml | 4 +- cmd/add.go | 4 +- cmd/commands.go | 22 +++++---- cmd/commands_no_self_update.go | 22 +++++++++ cmd/commands_no_upgrade.go | 17 ------- cmd/dump.go | 4 +- cmd/install.go | 4 +- cmd/root.go | 2 +- cmd/run.go | 4 +- cmd/{upgrade.go => self_update.go} | 18 +++---- cmd/uninstall.go | 4 +- cmd/version.go | 8 ++-- .../upgrader.go => updater/updater.go} | 20 ++++---- packaging/pack.rb | 48 +++++++++---------- 14 files changed, 102 insertions(+), 79 deletions(-) create mode 100644 cmd/commands_no_self_update.go delete mode 100644 cmd/commands_no_upgrade.go rename cmd/{upgrade.go => self_update.go} (72%) rename internal/{upgrader/upgrader.go => updater/updater.go} (91%) diff --git a/.goreleaser.yml b/.goreleaser.yml index 7804d93d..b611c018 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -4,9 +4,9 @@ before: - go generate ./... builds: # Builds the binaries without `lefthook upgrade` - - id: no_upgrade + - id: no_self_update tags: - - no_upgrade + - no_self_update env: - GCO_ENABLED=0 goos: diff --git a/cmd/add.go b/cmd/add.go index ad3f1f16..871727ba 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -12,7 +12,9 @@ import ( //go:embed add-doc.txt var addDoc string -func newAddCmd(opts *lefthook.Options) *cobra.Command { +type add struct{} + +func (add) New(opts *lefthook.Options) *cobra.Command { args := lefthook.AddArgs{} addHookCompletions := func(cmd *cobra.Command, args []string, toComplete string) (ret []string, compDir cobra.ShellCompDirective) { diff --git a/cmd/commands.go b/cmd/commands.go index 80662636..4871458f 100644 --- a/cmd/commands.go +++ b/cmd/commands.go @@ -1,4 +1,4 @@ -//go:build !no_upgrade +//go:build !no_self_update package cmd @@ -8,12 +8,16 @@ import ( "github.com/evilmartians/lefthook/internal/lefthook" ) -var commands = [...]func(*lefthook.Options) *cobra.Command{ - newVersionCmd, - newAddCmd, - newInstallCmd, - newUninstallCmd, - newRunCmd, - newDumpCmd, - newUpgradeCmd, +type command interface { + New(*lefthook.Options) *cobra.Command +} + +var commands = [...]command{ + version{}, + add{}, + install{}, + uninstall{}, + run{}, + dump{}, + selfUpdate{}, } diff --git a/cmd/commands_no_self_update.go b/cmd/commands_no_self_update.go new file mode 100644 index 00000000..7c6a591e --- /dev/null +++ b/cmd/commands_no_self_update.go @@ -0,0 +1,22 @@ +//go:build no_self_update + +package cmd + +import ( + "github.com/spf13/cobra" + + "github.com/evilmartians/lefthook/internal/lefthook" +) + +type command interface { + add(*lefthook.Options) *cobra.Command +} + +var commands = [...]command{ + version{}, + add{}, + install{}, + uninstall{}, + run{}, + dump{}, +} diff --git a/cmd/commands_no_upgrade.go b/cmd/commands_no_upgrade.go deleted file mode 100644 index e5dbfda5..00000000 --- a/cmd/commands_no_upgrade.go +++ /dev/null @@ -1,17 +0,0 @@ -//go:build no_upgrade - -package cmd - -import ( - "github.com/evilmartians/lefthook/internal/lefthook" - "github.com/spf13/cobra" -) - -var commands = [...]func(*lefthook.Options) *cobra.Command{ - newVersionCmd, - newAddCmd, - newInstallCmd, - newUninstallCmd, - newRunCmd, - newDumpCmd, -} diff --git a/cmd/dump.go b/cmd/dump.go index e0071fc3..13d38744 100644 --- a/cmd/dump.go +++ b/cmd/dump.go @@ -6,7 +6,9 @@ import ( "github.com/evilmartians/lefthook/internal/lefthook" ) -func newDumpCmd(opts *lefthook.Options) *cobra.Command { +type dump struct{} + +func (dump) New(opts *lefthook.Options) *cobra.Command { dumpArgs := lefthook.DumpArgs{} dumpCmd := cobra.Command{ Use: "dump", diff --git a/cmd/install.go b/cmd/install.go index f27dbf5e..6d020212 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -7,7 +7,9 @@ import ( "github.com/evilmartians/lefthook/internal/log" ) -func newInstallCmd(opts *lefthook.Options) *cobra.Command { +type install struct{} + +func (install) New(opts *lefthook.Options) *cobra.Command { var a, force bool installCmd := cobra.Command{ diff --git a/cmd/root.go b/cmd/root.go index f1dcd052..2190990a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -52,7 +52,7 @@ func newRootCmd() *cobra.Command { } for _, subcommand := range commands { - rootCmd.AddCommand(subcommand(&options)) + rootCmd.AddCommand(subcommand.New(&options)) } return rootCmd diff --git a/cmd/run.go b/cmd/run.go index a07d2985..c7b48615 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -8,7 +8,9 @@ import ( "github.com/evilmartians/lefthook/internal/log" ) -func newRunCmd(opts *lefthook.Options) *cobra.Command { +type run struct{} + +func (run) New(opts *lefthook.Options) *cobra.Command { runArgs := lefthook.RunArgs{} runHookCompletions := func(cmd *cobra.Command, args []string, toComplete string) (ret []string, compDir cobra.ShellCompDirective) { diff --git a/cmd/upgrade.go b/cmd/self_update.go similarity index 72% rename from cmd/upgrade.go rename to cmd/self_update.go index d293862c..85d88fe6 100644 --- a/cmd/upgrade.go +++ b/cmd/self_update.go @@ -10,19 +10,21 @@ import ( "github.com/evilmartians/lefthook/internal/lefthook" "github.com/evilmartians/lefthook/internal/log" - "github.com/evilmartians/lefthook/internal/upgrader" + "github.com/evilmartians/lefthook/internal/updater" ) -func newUpgradeCmd(opts *lefthook.Options) *cobra.Command { +type selfUpdate struct{} + +func (selfUpdate) New(opts *lefthook.Options) *cobra.Command { var yes bool upgradeCmd := cobra.Command{ - Use: "upgrade", - Short: "Upgrade lefthook executable", - Example: "lefthook upgrade", + Use: "self-update", + Short: "Update lefthook executable", + Example: "lefthook self-update", ValidArgsFunction: cobra.NoFileCompletions, Args: cobra.NoArgs, RunE: func(_cmd *cobra.Command, _args []string) error { - return upgrade(opts, yes) + return update(opts, yes) }, } @@ -33,7 +35,7 @@ func newUpgradeCmd(opts *lefthook.Options) *cobra.Command { return &upgradeCmd } -func upgrade(opts *lefthook.Options, yes bool) error { +func update(opts *lefthook.Options, yes bool) error { if os.Getenv(lefthook.EnvVerbose) == "1" || os.Getenv(lefthook.EnvVerbose) == "true" { opts.Verbose = true } @@ -57,5 +59,5 @@ func upgrade(opts *lefthook.Options, yes bool) error { cancel() }() - return upgrader.New().Upgrade(ctx, yes, opts.Force) + return updater.New().SelfUpdate(ctx, yes, opts.Force) } diff --git a/cmd/uninstall.go b/cmd/uninstall.go index 7b3cdee7..470f85c5 100644 --- a/cmd/uninstall.go +++ b/cmd/uninstall.go @@ -6,7 +6,9 @@ import ( "github.com/evilmartians/lefthook/internal/lefthook" ) -func newUninstallCmd(opts *lefthook.Options) *cobra.Command { +type uninstall struct{} + +func (uninstall) New(opts *lefthook.Options) *cobra.Command { args := lefthook.UninstallArgs{} uninstallCmd := cobra.Command{ diff --git a/cmd/version.go b/cmd/version.go index b6c1a8e7..162ae0fa 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -5,10 +5,12 @@ import ( "github.com/evilmartians/lefthook/internal/lefthook" "github.com/evilmartians/lefthook/internal/log" - "github.com/evilmartians/lefthook/internal/version" + ver "github.com/evilmartians/lefthook/internal/version" ) -func newVersionCmd(_opts *lefthook.Options) *cobra.Command { +type version struct{} + +func (version) New(_opts *lefthook.Options) *cobra.Command { var verbose bool versionCmd := cobra.Command{ @@ -17,7 +19,7 @@ func newVersionCmd(_opts *lefthook.Options) *cobra.Command { ValidArgsFunction: cobra.NoFileCompletions, Args: cobra.NoArgs, Run: func(cmd *cobra.Command, args []string) { - log.Println(version.Version(verbose)) + log.Println(ver.Version(verbose)) }, } diff --git a/internal/upgrader/upgrader.go b/internal/updater/updater.go similarity index 91% rename from internal/upgrader/upgrader.go rename to internal/updater/updater.go index 06827936..d178c6c9 100644 --- a/internal/upgrader/upgrader.go +++ b/internal/updater/updater.go @@ -1,5 +1,5 @@ -// Package upgrader contains the auto-upgrade implementation for the lefthook executable. -package upgrader +// Package updater contains the self-update implementation for the lefthook executable. +package updater import ( "bufio" @@ -58,17 +58,17 @@ type asset struct { DownloadURL string `json:"browser_download_url"` } -type Upgrader struct { +type Updater struct { client *http.Client } -func New() *Upgrader { - return &Upgrader{ +func New() *Updater { + return &Updater{ client: &http.Client{Timeout: timeout}, } } -func (u *Upgrader) Upgrade(ctx context.Context, yes, force bool) error { +func (u *Updater) SelfUpdate(ctx context.Context, yes, force bool) error { rel, ferr := u.fetchLatestRelease(ctx) if ferr != nil { return fmt.Errorf("latest release fetch failed: %w", ferr) @@ -117,13 +117,13 @@ func (u *Upgrader) Upgrade(ctx context.Context, yes, force bool) error { } if !yes { - log.Infof("Upgrade %s to %s? %s ", log.Cyan("lefthook"), log.Yellow(latestVersion), log.Gray("[Y/n]")) + log.Infof("Update %s to %s? %s ", log.Cyan("lefthook"), log.Yellow(latestVersion), log.Gray("[Y/n]")) scanner := bufio.NewScanner(os.Stdin) scanner.Scan() ans := scanner.Text() if len(ans) > 0 && ans[0] != 'y' && ans[0] != 'Y' { - log.Debug("Upgrade rejected") + log.Debug("Update rejected") return nil } } @@ -176,7 +176,7 @@ func (u *Upgrader) Upgrade(ctx context.Context, yes, force bool) error { return nil } -func (u *Upgrader) fetchLatestRelease(ctx context.Context) (*release, error) { +func (u *Updater) fetchLatestRelease(ctx context.Context) (*release, error) { req, err := http.NewRequestWithContext(ctx, http.MethodGet, latestReleaseURL, nil) if err != nil { return nil, fmt.Errorf("failed to initialize a request: %w", err) @@ -198,7 +198,7 @@ func (u *Upgrader) fetchLatestRelease(ctx context.Context) (*release, error) { return &rel, nil } -func (u *Upgrader) download(ctx context.Context, name, fileURL, checksumURL, path string) (bool, error) { +func (u *Updater) download(ctx context.Context, name, fileURL, checksumURL, path string) (bool, error) { filereq, err := http.NewRequestWithContext(ctx, http.MethodGet, fileURL, nil) if err != nil { return false, fmt.Errorf("failed to build download request: %w", err) diff --git a/packaging/pack.rb b/packaging/pack.rb index 69a36a88..bb9e9f52 100755 --- a/packaging/pack.rb +++ b/packaging/pack.rb @@ -54,42 +54,42 @@ def put_binaries cd(__dir__) puts "Putting binaries to packages..." { - "#{DIST}/no_upgrade_linux_amd64_v1/lefthook" => "npm/lefthook-linux-x64/bin/lefthook", - "#{DIST}/no_upgrade_linux_arm64/lefthook" => "npm/lefthook-linux-arm64/bin/lefthook", - "#{DIST}/no_upgrade_freebsd_amd64_v1/lefthook" => "npm/lefthook-freebsd-x64/bin/lefthook", - "#{DIST}/no_upgrade_freebsd_arm64/lefthook" => "npm/lefthook-freebsd-arm64/bin/lefthook", - "#{DIST}/no_upgrade_windows_amd64_v1/lefthook.exe" => "npm/lefthook-windows-x64/bin/lefthook.exe", - "#{DIST}/no_upgrade_windows_arm64/lefthook.exe" => "npm/lefthook-windows-arm64/bin/lefthook.exe", - "#{DIST}/no_upgrade_darwin_amd64_v1/lefthook" => "npm/lefthook-darwin-x64/bin/lefthook", - "#{DIST}/no_upgrade_darwin_arm64/lefthook" => "npm/lefthook-darwin-arm64/bin/lefthook", + "#{DIST}/no_self_update_linux_amd64_v1/lefthook" => "npm/lefthook-linux-x64/bin/lefthook", + "#{DIST}/no_self_update_linux_arm64/lefthook" => "npm/lefthook-linux-arm64/bin/lefthook", + "#{DIST}/no_self_update_freebsd_amd64_v1/lefthook" => "npm/lefthook-freebsd-x64/bin/lefthook", + "#{DIST}/no_self_update_freebsd_arm64/lefthook" => "npm/lefthook-freebsd-arm64/bin/lefthook", + "#{DIST}/no_self_update_windows_amd64_v1/lefthook.exe" => "npm/lefthook-windows-x64/bin/lefthook.exe", + "#{DIST}/no_self_update_windows_arm64/lefthook.exe" => "npm/lefthook-windows-arm64/bin/lefthook.exe", + "#{DIST}/no_self_update_darwin_amd64_v1/lefthook" => "npm/lefthook-darwin-x64/bin/lefthook", + "#{DIST}/no_self_update_darwin_arm64/lefthook" => "npm/lefthook-darwin-arm64/bin/lefthook", }.each do |(source, dest)| mkdir_p(File.dirname(dest)) cp(source, dest, verbose: true) end { - "#{DIST}/no_upgrade_linux_amd64_v1/lefthook" => "npm-bundled/bin/lefthook-linux-x64/lefthook", - "#{DIST}/no_upgrade_linux_arm64/lefthook" => "npm-bundled/bin/lefthook-linux-arm64/lefthook", - "#{DIST}/no_upgrade_freebsd_amd64_v1/lefthook" => "npm-bundled/bin/lefthook-freebsd-x64/lefthook", - "#{DIST}/no_upgrade_freebsd_arm64/lefthook" => "npm-bundled/bin/lefthook-freebsd-arm64/lefthook", - "#{DIST}/no_upgrade_windows_amd64_v1/lefthook.exe" => "npm-bundled/bin/lefthook-windows-x64/lefthook.exe", - "#{DIST}/no_upgrade_windows_arm64/lefthook.exe" => "npm-bundled/bin/lefthook-windows-arm64/lefthook.exe", - "#{DIST}/no_upgrade_darwin_amd64_v1/lefthook" => "npm-bundled/bin/lefthook-darwin-x64/lefthook", - "#{DIST}/no_upgrade_darwin_arm64/lefthook" => "npm-bundled/bin/lefthook-darwin-arm64/lefthook", + "#{DIST}/no_self_update_linux_amd64_v1/lefthook" => "npm-bundled/bin/lefthook-linux-x64/lefthook", + "#{DIST}/no_self_update_linux_arm64/lefthook" => "npm-bundled/bin/lefthook-linux-arm64/lefthook", + "#{DIST}/no_self_update_freebsd_amd64_v1/lefthook" => "npm-bundled/bin/lefthook-freebsd-x64/lefthook", + "#{DIST}/no_self_update_freebsd_arm64/lefthook" => "npm-bundled/bin/lefthook-freebsd-arm64/lefthook", + "#{DIST}/no_self_update_windows_amd64_v1/lefthook.exe" => "npm-bundled/bin/lefthook-windows-x64/lefthook.exe", + "#{DIST}/no_self_update_windows_arm64/lefthook.exe" => "npm-bundled/bin/lefthook-windows-arm64/lefthook.exe", + "#{DIST}/no_self_update_darwin_amd64_v1/lefthook" => "npm-bundled/bin/lefthook-darwin-x64/lefthook", + "#{DIST}/no_self_update_darwin_arm64/lefthook" => "npm-bundled/bin/lefthook-darwin-arm64/lefthook", }.each do |(source, dest)| mkdir_p(File.dirname(dest)) cp(source, dest, verbose: true) end { - "#{DIST}/no_upgrade_linux_amd64_v1/lefthook" => "rubygems/libexec/lefthook-linux-x64/lefthook", - "#{DIST}/no_upgrade_linux_arm64/lefthook" => "rubygems/libexec/lefthook-linux-arm64/lefthook", - "#{DIST}/no_upgrade_freebsd_amd64_v1/lefthook" => "rubygems/libexec/lefthook-freebsd-x64/lefthook", - "#{DIST}/no_upgrade_freebsd_arm64/lefthook" => "rubygems/libexec/lefthook-freebsd-arm64/lefthook", - "#{DIST}/no_upgrade_windows_amd64_v1/lefthook.exe" => "rubygems/libexec/lefthook-windows-x64/lefthook.exe", - "#{DIST}/no_upgrade_windows_arm64/lefthook.exe" => "rubygems/libexec/lefthook-windows-arm64/lefthook.exe", - "#{DIST}/no_upgrade_darwin_amd64_v1/lefthook" => "rubygems/libexec/lefthook-darwin-x64/lefthook", - "#{DIST}/no_upgrade_darwin_arm64/lefthook" => "rubygems/libexec/lefthook-darwin-arm64/lefthook", + "#{DIST}/no_self_update_linux_amd64_v1/lefthook" => "rubygems/libexec/lefthook-linux-x64/lefthook", + "#{DIST}/no_self_update_linux_arm64/lefthook" => "rubygems/libexec/lefthook-linux-arm64/lefthook", + "#{DIST}/no_self_update_freebsd_amd64_v1/lefthook" => "rubygems/libexec/lefthook-freebsd-x64/lefthook", + "#{DIST}/no_self_update_freebsd_arm64/lefthook" => "rubygems/libexec/lefthook-freebsd-arm64/lefthook", + "#{DIST}/no_self_update_windows_amd64_v1/lefthook.exe" => "rubygems/libexec/lefthook-windows-x64/lefthook.exe", + "#{DIST}/no_self_update_windows_arm64/lefthook.exe" => "rubygems/libexec/lefthook-windows-arm64/lefthook.exe", + "#{DIST}/no_self_update_darwin_amd64_v1/lefthook" => "rubygems/libexec/lefthook-darwin-x64/lefthook", + "#{DIST}/no_self_update_darwin_arm64/lefthook" => "rubygems/libexec/lefthook-darwin-arm64/lefthook", }.each do |(source, dest)| mkdir_p(File.dirname(dest)) cp(source, dest, verbose: true)