Skip to content

Commit

Permalink
feat: add version variable to cocommit which gets set through build c…
Browse files Browse the repository at this point in the history
…ommand
  • Loading branch information
Slug-Boi committed Nov 29, 2024
1 parent 0350779 commit 08d1942
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import (
"github.com/spf13/cobra"
)

// Variables lives in here in case of possible future check of updates on running the CLI
var Coco_Version string

// rootCmd represents the base command when called without any subcommands
// func RootCmd() *cobra.Command {
var rootCmd = &cobra.Command{
Expand All @@ -33,6 +36,12 @@ var rootCmd = &cobra.Command{
pflag, _ := cmd.Flags().GetBool("print")
tflag, _ := cmd.Flags().GetBool("test_print")
aflag, _ := cmd.Flags().GetBool("authors")
vflag, _ := cmd.Flags().GetBool("version")

if vflag {
fmt.Println("Cocommit version:", Coco_Version)
os.Exit(0)
}

if aflag {
tui.Entry()
Expand Down Expand Up @@ -108,4 +117,5 @@ func init() {
rootCmd.Flags().BoolP("test_print", "t", false, "Prints the commit message to the console without running the git commit command")
rootCmd.Flags().BoolP("message", "m", false, "Does nothing but allows for -m to be used in the command")
rootCmd.Flags().BoolP("authors", "a", false, "Runs the author list TUI")
rootCmd.Flags().BoolP("version", "v", false, "Prints the version of the cocommit cli tool")
}
35 changes: 34 additions & 1 deletion src/cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"archive/tar"
"compress/gzip"
"encoding/json"
"fmt"
"io"
"log"
Expand All @@ -17,6 +18,11 @@ import (
"github.com/spf13/cobra"
)


type github_release struct {
TagName string `json:"tag_name"`
}

// updateCmd represents the update command
var updateCmd = &cobra.Command{
Use: "update",
Expand All @@ -25,6 +31,33 @@ var updateCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
gflag, _ := cmd.Flags().GetBool("go-get")

// check version of the cli tool
Github, err := http.Get("https://api.github.com/repos/Slug-Boi/cocommit/releases/latest")
if err != nil {
fmt.Println("Error getting latest release version")
fmt.Println("Would you still like to update? (y/n)")
var input string
fmt.Scanln(&input)
if input == "y" || input == "Y" || input == "yes" {
fmt.Println("Running update script to update cocommit cli tool")
} else {
fmt.Println("Update cancelled")
return
}
}
defer Github.Body.Close()

var release github_release
err = json.NewDecoder(Github.Body).Decode(&release)
if err != nil {
panic("Error decoding json")
}

if release.TagName == Coco_Version {
fmt.Println("Cocommit cli tool is already up to date")
return
}

if gflag {
fmt.Println("Running go get command to update cocommit cli tool")
cmd := exec.Command("go", "get", "-u", "github.com/Slug-Boi/cocommit")
Expand All @@ -34,7 +67,7 @@ var updateCmd = &cobra.Command{
}
fmt.Println("Cocommit cli tool updated successfully")
} else {
fmt.Println("Running update script to update cocommit cli tool")
fmt.Println("Running binary replace to update cocommit cli tool")
updateScript()
}

Expand Down

0 comments on commit 08d1942

Please sign in to comment.