-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add update command scaffold * feat: change initial version to 0.0.0+dev * feat: add update command
- Loading branch information
Showing
9 changed files
with
136 additions
and
3 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,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 | ||
} |
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,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) | ||
} | ||
}) | ||
} |
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