generated from kurtosis-tech/package-template-repo
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update version command with caching
- Loading branch information
1 parent
4ca2a7f
commit ffd1e02
Showing
5 changed files
with
118 additions
and
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,80 @@ | ||
package utility | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/google/go-github/github" | ||
"github.com/hugobyte/dive-core/cli/common" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
latestVersion = "" | ||
) | ||
|
||
const versionFile = "version" | ||
|
||
var VersionCmd = common.NewDiveCommandBuilder(). | ||
SetUse("version"). | ||
SetShort("Checks The DIVE CLI Version"). | ||
SetLong("Checks the current DIVE CLI version and warns if you are using an old version."). | ||
SetRun(version). | ||
Build() | ||
|
||
func version(cmd *cobra.Command, args []string) {} | ||
func version(cmd *cobra.Command, args []string) { | ||
|
||
cli := common.GetCli() | ||
|
||
fmt.Println(GetLatestVersion(cli)) | ||
|
||
} | ||
|
||
// This function will fetch the latest version from HugoByte/Dive repo | ||
func GetLatestVersion(cli *common.Cli) string { | ||
|
||
// Repo Name | ||
repo := "DIVE" | ||
owner := "HugoByte" | ||
|
||
versionFilePath, err := cli.FileHandler().GetAppDirPathOrAppFilePath(versionFile) | ||
if err != nil { | ||
cli.Logger().SetErrorToStderr() | ||
cli.Logger().Fatal(common.CodeOf(err), err.Error()) | ||
} | ||
|
||
versionFileInfo, err := os.Stat(versionFilePath) | ||
if os.IsNotExist(err) { | ||
|
||
} else if err != nil { | ||
cli.Logger().SetErrorToStderr() | ||
cli.Logger().Fatal(common.CodeOf(err), err.Error()) | ||
|
||
} | ||
|
||
if versionFileInfo == nil || time.Since(versionFileInfo.ModTime()).Hours() > 1 { | ||
|
||
client := github.NewClient(nil) | ||
release, _, err := client.Repositories.GetLatestRelease(context.Background(), owner, repo) | ||
if err != nil { | ||
cli.Logger().SetErrorToStderr() | ||
cli.Logger().Error(common.CodeOf(err), err.Error()) | ||
return "" | ||
} | ||
|
||
latestVersion = release.GetName() | ||
cli.FileHandler().WriteAppFile(versionFile, []byte(latestVersion)) | ||
os.Chtimes(versionFilePath, time.Now(), time.Now()) | ||
|
||
} else { | ||
|
||
cachedVersion, err := cli.FileHandler().ReadAppFile(versionFile) | ||
if err == nil && string(cachedVersion) != "" { | ||
latestVersion = string(cachedVersion) | ||
} | ||
} | ||
|
||
return latestVersion | ||
} |
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