Skip to content

Commit

Permalink
feat: add version and update command (#28)
Browse files Browse the repository at this point in the history
* feat: add version and update command
* docs: Update PULL_REQUEST_TEMPLATE.md
  • Loading branch information
Pradumnasaraf authored Sep 7, 2024
1 parent c0f24c8 commit 1b72d0c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 58 deletions.
17 changes: 7 additions & 10 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
<!-- If your PR fixes an open issue, use `Closes #101` to link your PR with the issue. #101 stands for the issue number you are fixing -->

## 🛠️ Fixes Issue

<!-- Remove this section if not applicable -->
### 🛠️ Fixes Issue <!-- Remove this section if not applicable -->

<!-- Example: Closes #31 -->

## 👨‍💻 Changes proposed
### 👨‍💻 Changes proposed

<!-- List all the proposed changes in your PR -->

## ✔️ Check List (Check all the applicable boxes) <!-- Follow the below conventions to check the box -->
### ✔️ Check List (Check all the applicable boxes) <!-- Follow the below conventions to check the box -->

<!-- Mark all the applicable boxes. To mark the box as done follow the following conventions -->
<!--
Expand All @@ -21,9 +17,10 @@
- [ ] My code follows the code style of this project.
- [ ] This PR does not contain plagiarized content.
- [ ] The title of my pull request is a short description of the requested changes.
- [ ] I have updated the documentation accordingly (if required).
- [ ] Update the version of the `CliVersion` variable in `cmd/version.go` file (if their is a version change).

## 📄 Note to reviewers
### 📄 Note to reviewers <!-- Add notes to reviewers if applicable -->

<!-- Add notes to reviewers if applicable -->

## 📷 Screenshots
### 📷 Screenshots <!-- Remove this section if not applicable -->
2 changes: 1 addition & 1 deletion .github/workflows/releases.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
github-token: ${{ secrets.PA_TOKEN }}
output-file: "false"
skip-commit: "true"

create-summary: 'true'
outputs:
tag: ${{ steps.changelog.outputs.tag }}

Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
var rootCmd = &cobra.Command{
Use: "candy [command]",
Short: "Do all your tedious tasks with a single command",

Run: func(cmd *cobra.Command, args []string) {
err := cmd.Help()
checkNilErr(err)
Expand Down
34 changes: 34 additions & 0 deletions cmd/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cmd

import (
"fmt"
"os/exec"

"github.com/spf13/cobra"
)

// updateCmd represents the update command
var updateCmd = &cobra.Command{
Use: "update",
Short: "Update candy to the latest version",
Long: `This command will help you to update candy to the latest version.`,
Run: func(cmd *cobra.Command, args []string) {
update()
},
}

func update() {
cmd := exec.Command("go", "install", "github.com/Pradumnasaraf/candy@latest")
_, err := cmd.Output()

if err != nil {
fmt.Println("Error executing command:", err)
return
}

fmt.Printf("CLI updated successfully to the latest version (If any). Current version is: %s\n", CliVersion)
}

func init() {
rootCmd.AddCommand(updateCmd)
}
51 changes: 4 additions & 47 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,16 @@ import (
"fmt"

"github.com/spf13/cobra"
"github.com/tcnksm/go-latest"
)

var (
checkLatest bool
)

const (
CLI_VERSION = "1.7.0"
OWNER = "Pradumnasaraf"
REPO = "candy"
)
const CliVersion = "v1.8.0"

// versionCmd represents the version command
var versionCmd = &cobra.Command{
Use: "version",
Short: "outputs the cli version",
Short: "Know the installed version of candy",
Long: `This command will help you to know the installed version of candy`,
Run: func(cmd *cobra.Command, args []string) {

if checkLatest {
checkForNewVersion()
} else {
fmt.Println(CLI_VERSION)
}

fmt.Println("candy version:", CliVersion)
},
}

func checkForNewVersion() {

githubTag := &latest.GithubTag{
Owner: OWNER,
Repository: REPO,
FixVersionStrFunc: latest.DeleteFrontV(),
}

res, err := latest.Check(githubTag, CLI_VERSION)

if err != nil {
fmt.Println("Unable to check for latest version. Check your internet connection")
return
}

if res.Outdated {
fmt.Printf("The latest version of candy is %s.\nPlease update to the latest version by running go get -u github.com/Pradumnasaraf/candy@latest", res.Current)
return
}

fmt.Println("You are using the latest version of candy")

}

func init() {
// Flags for the version command
versionCmd.Flags().BoolVarP(&checkLatest, "latest", "l", false, "Check if the latest version is installed")
}

0 comments on commit 1b72d0c

Please sign in to comment.