Skip to content

Commit

Permalink
Check if cnev is running latest version
Browse files Browse the repository at this point in the history
  • Loading branch information
omfj committed Nov 24, 2024
1 parent f781c21 commit 54506e4
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ jobs:
goarch: ${{ matrix.goarch }}
multi_binaries: true
binary_name: cenv
ldflags: -X main.Version=${{ github.event.release.tag_name }}
project_path: .
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,16 @@ You can fix and outdated .env with the fix command. Note that this will overwrit
cenv fix
```

## Building

To build the project, you need to have Go installed. Run the following command to build the project:

```sh
go build -o cenv cmd/cenv/main.go
```

If you want to overwrite the `Version` variable in `main.go` you have add the following flags:

```sh
go build -o cenv -ldflags "-X main.Version=<your-version>" cmd/cenv/main.go
```
47 changes: 47 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package cmd

import (
"encoding/json"
"fmt"
"net/http"
"os"
"os/exec"
"strings"

"github.com/echo-webkom/cenv/cenv"
"github.com/fatih/color"
)

var Version = "dev"

func showHelp() {
fmt.Println("cenv [command] <args>")
fmt.Println()
Expand All @@ -23,6 +28,7 @@ func showHelp() {
fmt.Println("Flags:")
fmt.Println(" --env <path> Path to env file, default is current dir")
fmt.Println(" --schema <path> Path to schema file, default is current dir")
fmt.Println(" --skip-version Skip version check")
fmt.Println()
}

Expand All @@ -36,7 +42,48 @@ func errorExit(err error) {
os.Exit(1)
}

func checkIfLatestVersion() {
if Version == "dev" {
return
}

for _, arg := range os.Args {
if arg == "--skip-version" {
return
}
}

resp, err := http.Get("https://api.github.com/repos/echo-webkom/cenv/releases/latest")
if err != nil {
return
}
defer resp.Body.Close()

if resp.StatusCode != 200 {
return
}

var result struct {
TagName string `json:"tag_name"`
}
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
return
}

latestVersion := strings.TrimSpace(result.TagName)
currentVersion := strings.TrimSpace(Version)

isLatest := currentVersion == latestVersion

if !isLatest {
color.Yellow("A new version of cenv is available. Run 'cenv upgrade' to upgrade.")
}
}

func Run() {
checkIfLatestVersion()

if len(os.Args) < 2 {
showHelp()
os.Exit(1)
Expand Down

0 comments on commit 54506e4

Please sign in to comment.