From 709912cc0c58cda1e4454894c4cfe935579596a8 Mon Sep 17 00:00:00 2001 From: devhindo Date: Mon, 15 Apr 2024 08:00:30 +0200 Subject: [PATCH] changed back to cli --- src/cli/cmd/update.go | 118 +++++++++++++++++++++++++++++++++++ src/cli/cmd/update/update.go | 63 ------------------- src/cli/cmd/version.go | 22 +++++++ 3 files changed, 140 insertions(+), 63 deletions(-) create mode 100644 src/cli/cmd/update.go delete mode 100644 src/cli/cmd/update/update.go create mode 100644 src/cli/cmd/version.go diff --git a/src/cli/cmd/update.go b/src/cli/cmd/update.go new file mode 100644 index 0000000..9c01ec9 --- /dev/null +++ b/src/cli/cmd/update.go @@ -0,0 +1,118 @@ +package cmd + +import ( + "fmt" + "os" + "os/exec" + "regexp" + + "github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(updateCmd) + + updateCmd.Flags().StringVarP(&vFlag, "version", "v", "latest", "Update the CLI to specific version") +} + +var ( + vFlag string + + updateCmd = &cobra.Command{ + Use: "update", + Short: "Update the CLI to specific version ", + Long: `Update the CLI to the latest version or specify a version using -v flag + Example: x update -v 1.1.1 + default: x update -v latest + `, + RunE: update, + } +) + +func update(cmd *cobra.Command, args []string) error { + + fmt.Println("Updating CLI...") + + vFlag, err := cmd.Flags().GetString("version") + if err != nil { + err = fmt.Errorf("error getting version flag: %v", err) + return err + } + + // validate version + if vFlag != "latest" { + err = validateVersion(vFlag) + if err != nil { + return err + } + } + + if isGoInstalled() { + err = updateUsingGo() + if err != nil { + return err + } + return nil + } + + return nil +} + +func validateVersion(v string) error { + // Regular expression pattern for semantic versioning + pattern := `^v\d+\.\d+\.\d+$` + match, err := regexp.MatchString(pattern, v) + if err != nil { + return err + } + if !match { + return fmt.Errorf("invalid version format. Please use semantic versioning like v1.1.1") + } + return nil +} + +func cmdUpdate() { + if !isGoInstalled() { + fmt.Println("") + } + + cmd := exec.Command("go", "get", "-u", "github.com/bradford-hamilton/monkey") + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + err := cmd.Run() + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + if isGoInstalled() { + fmt.Println("Go is installed...") + + return + } + + fmt.Println("Go is not installed...") + + fmt.Println("CLI updated successfully!") +} + +func isGoInstalled() bool { + _, err := exec.LookPath("go") + + return err == nil +} + +func updateUsingGo() error { + cmd := exec.Command("go", "get", "-u", "github.com/devhindo/x/src/cli/cmd") + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + err := cmd.Run() + if err != nil { + fmt.Println(err) + return err + } + fmt.Println("CLI updated successfully!") + return nil +} + +//TODO: x update -v 1.1.1 (default for v:latest) diff --git a/src/cli/cmd/update/update.go b/src/cli/cmd/update/update.go deleted file mode 100644 index 8c4488a..0000000 --- a/src/cli/cmd/update/update.go +++ /dev/null @@ -1,63 +0,0 @@ -package cmd - -import ( - "fmt" - "os" - "os/exec" - "strings" - - "github.com/spf13/cobra" -) - -var updateCmd = &cobra.Command{ - Use: "update", - Short: "Update the CLI to the latest version", - Run: func(cmd *cobra.Command, args []string) { - cmdUpdate() - }, -} - -func cmdUpdate() { - fmt.Println("Updating CLI...") - - if !isGoInstalled() { - fmt.Println("") - } - - cmd := exec.Command("go", "get", "-u", "github.com/bradford-hamilton/monkey") - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - err := cmd.Run() - if err != nil { - fmt.Println(err) - os.Exit(1) - } - fmt.Println("CLI updated successfully!") -} - -func isGoInstalled() bool { - _, err := exec.LookPath("go") - if err != nil { - return false - } - return true -} - -func updateUsingGo() error { - cmd := exec.Command("go", "get", "-u", "github.com/bradford-hamilton/monkey") - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - err := cmd.Run() - if err != nil { - fmt.Println(err) - return err - } - fmt.Println("CLI updated successfully!") - return nil -} - -func init() { - rootCmd.AddCommand(updateCmd) -} - -//TODO: x update -v 1.1.1 (default for v:latest) \ No newline at end of file diff --git a/src/cli/cmd/version.go b/src/cli/cmd/version.go new file mode 100644 index 0000000..5158fcc --- /dev/null +++ b/src/cli/cmd/version.go @@ -0,0 +1,22 @@ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(versionCmd) +} + +//TODO: make it for -v and --version too + +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Print the version of the CLI", + Long: `Print the version of the CLI`, + Run: func(cmd *cobra.Command, args []string) { + fmt.Println(version) + }, +} \ No newline at end of file