Skip to content

Commit

Permalink
Add selfupdate (#73)
Browse files Browse the repository at this point in the history
* feat: add update command scaffold

* feat: change initial version to 0.0.0+dev

* feat: add update command
  • Loading branch information
elhmn committed Sep 4, 2021
1 parent fcce5de commit 60b6a61
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cmd/add_code_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func createConfig(t *testing.T) config.Config {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

conf := config.NewDefaultConfig(config.Options{Version: "0.0.0.dev"})
conf := config.NewDefaultConfig(config.Options{Version: "0.0.0+dev"})
conf.Exec = mocks.NewMockIExec(mockCtrl)
conf.Printers = mocks.NewMockIPrinters(mockCtrl)

Expand Down
1 change: 1 addition & 0 deletions cmd/ckp.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ func NewCKPCommand(config config.Config) *cobra.Command {
ckpCommand.AddCommand(NewEditCommand(config))
ckpCommand.AddCommand(NewRunCommand(config))
ckpCommand.AddCommand(NewVersionCommand(config))
ckpCommand.AddCommand(NewUpdateCommand(config))
return ckpCommand
}
66 changes: 66 additions & 0 deletions cmd/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package cmd

import (
"bufio"
"fmt"
"os"

"github.com/blang/semver"
"github.com/elhmn/ckp/internal/config"
"github.com/rhysd/go-github-selfupdate/selfupdate"
"github.com/spf13/cobra"
)

//NewUpdateCommand will update your binary to the latest release
func NewUpdateCommand(conf config.Config) *cobra.Command {
command := &cobra.Command{
Use: "update",
Short: "Update the binary to the latest release",
Run: func(cmd *cobra.Command, args []string) {
if err := updateCommand(conf); err != nil {
fmt.Fprintf(conf.OutWriter, "Error: %s\n", err)
return
}
},
}

return command
}

func updateCommand(conf config.Config) error {
return confirmAndUpdate(conf)
}

func confirmAndUpdate(conf config.Config) error {
latest, found, err := selfupdate.DetectLatest(conf.Repository)
if err != nil {
return fmt.Errorf("Error occurred while detecting version: %s", err)
}

v := semver.MustParse(conf.Version)
if !found || latest.Version.LTE(v) {
fmt.Fprintf(conf.OutWriter, "Current version is the latest")
return nil
}

fmt.Fprintf(conf.OutWriter, "Do you want to update to %s ? (y/n): \n", latest.Version)
input, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil || (input != "y\n" && input != "n\n") {
fmt.Fprintf(conf.OutWriter, "Invalid input")
return nil
}
if input == "n\n" {
return nil
}

exe, err := os.Executable()
if err != nil {
return fmt.Errorf("Could not locate executable path: %s", err)
}
if err := selfupdate.UpdateTo(latest.AssetURL, exe); err != nil {
return fmt.Errorf("Error occurred while updating binary: %s", err)
}

fmt.Fprintf(conf.OutWriter, "Successfully updated to version %s", latest.Version)
return nil
}
27 changes: 27 additions & 0 deletions cmd/update_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cmd_test

import (
"bytes"
"testing"

"github.com/elhmn/ckp/cmd"
)

//TestUpdateCommand test the `ckp update` command
func TestUpdateCommand(t *testing.T) {
t.Run("Run the update successfully", func(t *testing.T) {
conf := createConfig(t)
writer := &bytes.Buffer{}
conf.OutWriter = writer

command := cmd.NewUpdateCommand(conf)

//Set writer
command.SetOutput(conf.OutWriter)

err := command.Execute()
if err != nil {
t.Errorf("Error: failed with %s", err)
}
})
}
2 changes: 1 addition & 1 deletion cmd/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestVersionCommand(t *testing.T) {
}

got := writer.String()
exp := "Version: 0.0.0.dev\nBuild by elhmn\nSupport osscameroon here https://opencollective.com/osscameroon\n"
exp := "Version: 0.0.0+dev\nBuild by elhmn\nSupport osscameroon here https://opencollective.com/osscameroon\n"
assert.Contains(t, got, exp)
})
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ module github.com/elhmn/ckp
go 1.16

require (
github.com/blang/semver v3.5.1+incompatible
github.com/briandowns/spinner v1.12.0
github.com/golang/mock v1.5.0
github.com/manifoldco/promptui v0.8.0
github.com/mitchellh/go-homedir v1.1.0
github.com/rhysd/go-github-selfupdate v1.2.3
github.com/spf13/cobra v1.1.3
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.8.1
Expand Down
35 changes: 35 additions & 0 deletions go.sum

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Config struct {
CKPStorageFolder string
Spin printers.ISpinner
Printers printers.IPrinters
Repository string

//MainBranch is a your remote repository main branch
MainBranch string
Expand Down Expand Up @@ -62,6 +63,7 @@ func NewDefaultConfig(opt Options) Config {
MainBranch: MainBranch,
WorkingBranch: "working-" + MainBranch,
Version: opt.Version,
Repository: "elhmn/ckp",
}

conf.Viper = setupViper(conf)
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/elhmn/ckp/internal/config"
)

var version = "0.0.0.dev"
var version = "0.0.0+dev"

func main() {
conf := config.NewDefaultConfig(config.Options{Version: version})
Expand Down

0 comments on commit 60b6a61

Please sign in to comment.