Merge pull request #47 from Slug-Boi/feat_add_update_command

This commit is contained in:
Theis
2024-12-01 19:51:33 +01:00
committed by GitHub
4 changed files with 51 additions and 9 deletions
+5 -2
View File
@@ -42,6 +42,9 @@ func main() {
buildDir := test.Directory("/src_d/src/") buildDir := test.Directory("/src_d/src/")
Coco_var := os.Getenv("Coco_Version")
for _, goos := range geese { for _, goos := range geese {
path := fmt.Sprintf("/dist/") path := fmt.Sprintf("/dist/")
filename := fmt.Sprintf("/dist/cocommit-%s", goos) filename := fmt.Sprintf("/dist/cocommit-%s", goos)
@@ -50,7 +53,7 @@ func main() {
build := test. build := test.
WithEnvVariable("GOOS", goos). WithEnvVariable("GOOS", goos).
WithEnvVariable("GOARCH", goarch). WithEnvVariable("GOARCH", goarch).
WithExec([]string{"go", "build", "-o", filename}).WithEnvVariable("CI", "true") WithExec([]string{"go", "build", "-o", filename, "-ldflags", "-X github.com/Slug-Boi/cocommit/src/cmd.Coco_Version="+Coco_var}).WithEnvVariable("CI", "true")
buildDir = buildDir.WithDirectory(path, build.Directory(path)) buildDir = buildDir.WithDirectory(path, build.Directory(path))
@@ -63,7 +66,7 @@ func main() {
build := test. build := test.
WithEnvVariable("GOOS", "darwin"). WithEnvVariable("GOOS", "darwin").
WithEnvVariable("GOARCH", "arm64"). WithEnvVariable("GOARCH", "arm64").
WithExec([]string{"go", "build", "-o", filename}).WithEnvVariable("CI", "true") WithExec([]string{"go", "build", "-o", filename, "-ldflags", "-X github.com/Slug-Boi/cocommit/src/cmd.Coco_Version="+Coco_var}).WithEnvVariable("CI", "true")
buildDir = buildDir.WithDirectory(path, build.Directory(path)) buildDir = buildDir.WithDirectory(path, build.Directory(path))
+11 -1
View File
@@ -12,6 +12,9 @@ import (
"github.com/spf13/cobra" "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 // rootCmd represents the base command when called without any subcommands
// func RootCmd() *cobra.Command { // func RootCmd() *cobra.Command {
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
@@ -34,7 +37,13 @@ var rootCmd = &cobra.Command{
pflag, _ := cmd.Flags().GetBool("print") pflag, _ := cmd.Flags().GetBool("print")
tflag, _ := cmd.Flags().GetBool("test_print") tflag, _ := cmd.Flags().GetBool("test_print")
aflag, _ := cmd.Flags().GetBool("authors") aflag, _ := cmd.Flags().GetBool("authors")
gflag, _ := cmd.Flags().GetString("git") vflag, _ := cmd.Flags().GetBool("version")
gflag, _ := cmd.Flags().GetString("git")
if vflag {
fmt.Println("Cocommit version:", Coco_Version)
os.Exit(0)
}
var git_flags []string var git_flags []string
// runs the git commit command // runs the git commit command
@@ -121,5 +130,6 @@ 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("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("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("authors", "a", false, "Runs the author list TUI")
rootCmd.Flags().BoolP("version", "v", false, "Prints the version of the cocommit cli tool")
rootCmd.Flags().StringP("git", "g", "", "Adds the given flags to the git command") rootCmd.Flags().StringP("git", "g", "", "Adds the given flags to the git command")
} }
+34 -1
View File
@@ -3,6 +3,7 @@ package cmd
import ( import (
"archive/tar" "archive/tar"
"compress/gzip" "compress/gzip"
"encoding/json"
"fmt" "fmt"
"io" "io"
"log" "log"
@@ -17,6 +18,11 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
type github_release struct {
TagName string `json:"tag_name"`
}
// updateCmd represents the update command // updateCmd represents the update command
var updateCmd = &cobra.Command{ var updateCmd = &cobra.Command{
Use: "update", Use: "update",
@@ -25,6 +31,33 @@ var updateCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
gflag, _ := cmd.Flags().GetBool("go-get") 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 { if gflag {
fmt.Println("Running go get command to update cocommit cli tool") fmt.Println("Running go get command to update cocommit cli tool")
cmd := exec.Command("go", "get", "-u", "github.com/Slug-Boi/cocommit") cmd := exec.Command("go", "get", "-u", "github.com/Slug-Boi/cocommit")
@@ -34,7 +67,7 @@ var updateCmd = &cobra.Command{
} }
fmt.Println("Cocommit cli tool updated successfully") fmt.Println("Cocommit cli tool updated successfully")
} else { } else {
fmt.Println("Running update script to update cocommit cli tool") fmt.Println("Running binary replace to update cocommit cli tool")
updateScript() updateScript()
} }
-4
View File
@@ -1,7 +1,3 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package main package main
import "github.com/Slug-Boi/cocommit/src/cmd" import "github.com/Slug-Boi/cocommit/src/cmd"