-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add self-update command (#778)
* feat: add auto updater * fix: query latest release * feat: implement the upgrader * fix: move upgrader to a specific folder * ci: adjust CI to not include upgrader in npm and rubygems packages * chore: add package description * chore: add colors * chore: simplify the look and feel of commands * chore: small cosmetic changes * chore: add backup recover on issues with file system * test: add tests * fix: special fix for windows
- Loading branch information
Showing
17 changed files
with
680 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//go:build !no_self_update | ||
|
||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/evilmartians/lefthook/internal/lefthook" | ||
) | ||
|
||
type command interface { | ||
New(*lefthook.Options) *cobra.Command | ||
} | ||
|
||
var commands = [...]command{ | ||
version{}, | ||
add{}, | ||
install{}, | ||
uninstall{}, | ||
run{}, | ||
dump{}, | ||
selfUpdate{}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
New(*lefthook.Options) *cobra.Command | ||
} | ||
|
||
var commands = [...]command{ | ||
version{}, | ||
add{}, | ||
install{}, | ||
uninstall{}, | ||
run{}, | ||
dump{}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/evilmartians/lefthook/internal/lefthook" | ||
"github.com/evilmartians/lefthook/internal/log" | ||
"github.com/evilmartians/lefthook/internal/updater" | ||
) | ||
|
||
type selfUpdate struct{} | ||
|
||
func (selfUpdate) New(opts *lefthook.Options) *cobra.Command { | ||
var yes bool | ||
upgradeCmd := cobra.Command{ | ||
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 update(opts, yes) | ||
}, | ||
} | ||
|
||
upgradeCmd.Flags().BoolVarP(&yes, "yes", "y", false, "no prompt") | ||
upgradeCmd.Flags().BoolVarP(&opts.Force, "force", "f", false, "force upgrade") | ||
upgradeCmd.Flags().BoolVarP(&opts.Verbose, "verbose", "v", false, "show verbose logs") | ||
|
||
return &upgradeCmd | ||
} | ||
|
||
func update(opts *lefthook.Options, yes bool) error { | ||
if os.Getenv(lefthook.EnvVerbose) == "1" || os.Getenv(lefthook.EnvVerbose) == "true" { | ||
opts.Verbose = true | ||
} | ||
if opts.Verbose { | ||
log.SetLevel(log.DebugLevel) | ||
log.Debug("Verbose mode enabled") | ||
} | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
// Handle interrupts | ||
signalChan := make(chan os.Signal, 1) | ||
signal.Notify( | ||
signalChan, | ||
syscall.SIGINT, | ||
syscall.SIGTERM, | ||
) | ||
go func() { | ||
<-signalChan | ||
cancel() | ||
}() | ||
|
||
exePath, err := os.Executable() | ||
if err != nil { | ||
return fmt.Errorf("failed to determine the binary path: %w", err) | ||
} | ||
|
||
return updater.New().SelfUpdate(ctx, updater.Options{ | ||
Yes: yes, | ||
Force: opts.Force, | ||
ExePath: exePath, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.