Skip to content

Commit

Permalink
chore: simplify the look and feel of commands
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox committed Jul 23, 2024
1 parent 85f4e23 commit 76122ee
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 79 deletions.
4 changes: 2 additions & 2 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
22 changes: 13 additions & 9 deletions cmd/commands.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !no_upgrade
//go:build !no_self_update

package cmd

Expand All @@ -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{},
}
22 changes: 22 additions & 0 deletions cmd/commands_no_self_update.go
Original file line number Diff line number Diff line change
@@ -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{},

Check failure on line 16 in cmd/commands_no_self_update.go

View workflow job for this annotation

GitHub Actions / build

cannot use version{} (value of type version) as command value in array or slice literal: version does not implement command (missing method add)
add{},

Check failure on line 17 in cmd/commands_no_self_update.go

View workflow job for this annotation

GitHub Actions / build

cannot use add{} (value of type add) as command value in array or slice literal: add does not implement command (missing method add)
install{},

Check failure on line 18 in cmd/commands_no_self_update.go

View workflow job for this annotation

GitHub Actions / build

cannot use install{} (value of type install) as command value in array or slice literal: install does not implement command (missing method add)
uninstall{},

Check failure on line 19 in cmd/commands_no_self_update.go

View workflow job for this annotation

GitHub Actions / build

cannot use uninstall{} (value of type uninstall) as command value in array or slice literal: uninstall does not implement command (missing method add)
run{},

Check failure on line 20 in cmd/commands_no_self_update.go

View workflow job for this annotation

GitHub Actions / build

cannot use run{} (value of type run) as command value in array or slice literal: run does not implement command (missing method add)
dump{},

Check failure on line 21 in cmd/commands_no_self_update.go

View workflow job for this annotation

GitHub Actions / build

cannot use dump{} (value of type dump) as command value in array or slice literal: dump does not implement command (missing method add)
}
17 changes: 0 additions & 17 deletions cmd/commands_no_upgrade.go

This file was deleted.

4 changes: 3 additions & 1 deletion cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 3 additions & 1 deletion cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func newRootCmd() *cobra.Command {
}

for _, subcommand := range commands {
rootCmd.AddCommand(subcommand(&options))
rootCmd.AddCommand(subcommand.New(&options))

Check failure on line 55 in cmd/root.go

View workflow job for this annotation

GitHub Actions / build

subcommand.New undefined (type command has no field or method New)
}

return rootCmd
Expand Down
4 changes: 3 additions & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
18 changes: 10 additions & 8 deletions cmd/upgrade.go → cmd/self_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand All @@ -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
}
Expand All @@ -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)
}
4 changes: 3 additions & 1 deletion cmd/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
8 changes: 5 additions & 3 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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))
},
}

Expand Down
20 changes: 10 additions & 10 deletions internal/upgrader/upgrader.go → internal/updater/updater.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
48 changes: 24 additions & 24 deletions packaging/pack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 76122ee

Please sign in to comment.