-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add feature to send library update message (#174)
* feat: add feature to send library update message
- Loading branch information
1 parent
8b51ab0
commit fa2c3ae
Showing
4 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package versionupdate | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/fatih/color" | ||
|
||
"github.com/blang/semver/v4" | ||
) | ||
|
||
const ( | ||
organization = "Layr-Labs" | ||
repository = "eigenlayer-cli" | ||
) | ||
|
||
type release struct { | ||
TagName string `json:"tag_name"` | ||
Name string `json:"name"` | ||
} | ||
|
||
// Check if there is a new version of the package | ||
// If there is, print a message to the user | ||
// If there isn't, do nothing | ||
// Don't do anything for development version | ||
// If anything fails in this, it will silently pass since this doesn't affect operations | ||
func Check(currentVersion string) { | ||
if currentVersion == "development" { | ||
fmt.Println("Running development version") | ||
return | ||
} | ||
|
||
// Get latest version from GitHub releases | ||
latestReleaseUrl := fmt.Sprintf("https://api.github.com/repos/%s/%s/releases/latest", organization, repository) | ||
response, err := http.Get(latestReleaseUrl) | ||
if err != nil { | ||
return | ||
} | ||
defer response.Body.Close() | ||
respBytes, err := io.ReadAll(response.Body) | ||
if err != nil { | ||
return | ||
} | ||
var data release | ||
|
||
err = json.Unmarshal(respBytes, &data) | ||
if err != nil { | ||
return | ||
} | ||
|
||
// GitHub API returns in vX.X.X format so remove v | ||
latestVersion := data.TagName[1:] | ||
latestSemVer, err := semver.Make(latestVersion) | ||
if err != nil { | ||
return | ||
} | ||
|
||
currentSemVer, err := semver.Make(currentVersion) | ||
if err != nil { | ||
return | ||
} | ||
|
||
if latestSemVer.GT(currentSemVer) { | ||
greenVersion := color.GreenString(latestVersion) | ||
yellowOldVersion := color.YellowString(currentVersion) | ||
fmt.Println() | ||
fmt.Printf("There is a new version (%s) for this library available.\n", greenVersion) | ||
fmt.Printf("Your current running verison is (%s).\n", yellowOldVersion) | ||
fmt.Println("Please update (https://github.com/Layr-Labs/eigenlayer-cli#install-eigenlayer-cli-using-a-binary) to get latest features and bug fixes.") | ||
fmt.Println() | ||
} | ||
} |