cmd/version: simplified some code + added homebrew support
Signed-off-by: Sam Alba <sam.alba@gmail.com>
This commit is contained in:
parent
aee09b2d47
commit
c7323e79bc
@ -25,8 +25,12 @@ func init() {
|
|||||||
rootCmd.PersistentFlags().StringP("log-level", "l", "info", "Log level")
|
rootCmd.PersistentFlags().StringP("log-level", "l", "info", "Log level")
|
||||||
rootCmd.PersistentFlags().StringP("environment", "e", "", "Select an environment")
|
rootCmd.PersistentFlags().StringP("environment", "e", "", "Select an environment")
|
||||||
|
|
||||||
rootCmd.PersistentPreRun = checkVersionHook
|
rootCmd.PersistentPreRun = func(*cobra.Command, []string) {
|
||||||
rootCmd.PersistentPostRun = warnVersionHook
|
go checkVersion()
|
||||||
|
}
|
||||||
|
rootCmd.PersistentPostRun = func(*cobra.Command, []string) {
|
||||||
|
warnVersion()
|
||||||
|
}
|
||||||
|
|
||||||
rootCmd.AddCommand(
|
rootCmd.AddCommand(
|
||||||
computeCmd,
|
computeCmd,
|
||||||
@ -53,14 +57,6 @@ func init() {
|
|||||||
viper.AutomaticEnv()
|
viper.AutomaticEnv()
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkVersionHook(cmd *cobra.Command, args []string) {
|
|
||||||
go checkVersion()
|
|
||||||
}
|
|
||||||
|
|
||||||
func warnVersionHook(cmd *cobra.Command, args []string) {
|
|
||||||
warnVersion()
|
|
||||||
}
|
|
||||||
|
|
||||||
func Execute() {
|
func Execute() {
|
||||||
var (
|
var (
|
||||||
ctx = appcontext.Context()
|
ctx = appcontext.Context()
|
||||||
|
@ -20,6 +20,8 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
defaultVersion = "devel"
|
defaultVersion = "devel"
|
||||||
|
versionFile = "$HOME/.dagger/version-check"
|
||||||
|
versionURL = "https://releases.dagger.io/dagger/latest_version"
|
||||||
)
|
)
|
||||||
|
|
||||||
// set by goreleaser or other builder using
|
// set by goreleaser or other builder using
|
||||||
@ -29,12 +31,12 @@ var (
|
|||||||
versionMessage = ""
|
versionMessage = ""
|
||||||
)
|
)
|
||||||
|
|
||||||
// Disable version hook here
|
|
||||||
// It can lead to a double check if --check flag is enable
|
|
||||||
var versionCmd = &cobra.Command{
|
var versionCmd = &cobra.Command{
|
||||||
Use: "version",
|
Use: "version",
|
||||||
Short: "Print dagger version",
|
Short: "Print dagger version",
|
||||||
PersistentPreRun: func(cmd *cobra.Command, args []string) {},
|
// Disable version hook here to avoid double version check
|
||||||
|
PersistentPreRun: func(*cobra.Command, []string) {},
|
||||||
|
PersistentPostRun: func(*cobra.Command, []string) {},
|
||||||
Args: cobra.NoArgs,
|
Args: cobra.NoArgs,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
if bi, ok := debug.ReadBuildInfo(); ok && version == defaultVersion {
|
if bi, ok := debug.ReadBuildInfo(); ok && version == defaultVersion {
|
||||||
@ -47,15 +49,9 @@ var versionCmd = &cobra.Command{
|
|||||||
)
|
)
|
||||||
|
|
||||||
if check := viper.GetBool("check"); check {
|
if check := viper.GetBool("check"); check {
|
||||||
latestVersion, err := isVersionLatest()
|
_ = os.Remove(os.ExpandEnv(versionFile))
|
||||||
if err != nil {
|
checkVersion()
|
||||||
fmt.Println("error: could not check version.")
|
if !warnVersion() {
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if latestVersion != "" {
|
|
||||||
fmt.Printf("there is a new version available (%s), please go to https://github.com/dagger/dagger/doc/update.md for instructions.\n", latestVersion)
|
|
||||||
} else {
|
|
||||||
fmt.Println("dagger is up to date.")
|
fmt.Println("dagger is up to date.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -106,7 +102,7 @@ func getCurrentVersion() (*goVersion.Version, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getLatestVersion(currentVersion *goVersion.Version) (*goVersion.Version, error) {
|
func getLatestVersion(currentVersion *goVersion.Version) (*goVersion.Version, error) {
|
||||||
req, err := http.NewRequest("GET", "https://releases.dagger.io/dagger/latest_version", nil)
|
req, err := http.NewRequest("GET", versionURL, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -150,17 +146,22 @@ func isVersionLatest() (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func checkVersion() {
|
func checkVersion() {
|
||||||
home, err := os.UserHomeDir()
|
if version == defaultVersion {
|
||||||
if err != nil {
|
// running devel version
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
daggerDirectory := path.Join(home, ".dagger")
|
versionFilePath := os.ExpandEnv(versionFile)
|
||||||
if err := os.MkdirAll(daggerDirectory, 0666); err != nil {
|
baseDir := path.Dir(versionFilePath)
|
||||||
|
|
||||||
|
if _, err := os.Stat(baseDir); os.IsNotExist(err) {
|
||||||
|
if err := os.MkdirAll(baseDir, 0755); err != nil {
|
||||||
|
// mkdir fails, ignore silently
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if !isCheckOutdated(path.Join(daggerDirectory, "version_check.txt")) {
|
if !isCheckOutdated(versionFilePath) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -171,16 +172,30 @@ func checkVersion() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if latestVersion != "" {
|
if latestVersion != "" {
|
||||||
versionMessage = fmt.Sprintf("there is a new version available (%s), please go to https://github.com/dagger/dagger/doc/update.md for instructions.", latestVersion)
|
versionMessage = fmt.Sprintf("\nA new version is available (%s), please go to https://github.com/dagger/dagger/doc/install.md for instructions.", latestVersion)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update check timestamps file
|
// Update check timestamps file
|
||||||
now := time.Now().Format(time.RFC3339)
|
now := time.Now().Format(time.RFC3339)
|
||||||
ioutil.WriteFile(path.Join(daggerDirectory, "version_check.txt"), []byte(now), 0600)
|
ioutil.WriteFile(path.Join(versionFilePath), []byte(now), 0600)
|
||||||
}
|
}
|
||||||
|
|
||||||
func warnVersion() {
|
func warnVersion() bool {
|
||||||
if versionMessage != "" {
|
if versionMessage == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if binPath, err := os.Executable(); err == nil {
|
||||||
|
if p, err := os.Readlink(binPath); err == nil {
|
||||||
|
// Homebrew detected, print custom message
|
||||||
|
if strings.Contains(p, "/Cellar/") {
|
||||||
|
fmt.Println("\nA new version is available, please run:\n\nbrew update && brew upgrade dagger")
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print default message
|
||||||
fmt.Println(versionMessage)
|
fmt.Println(versionMessage)
|
||||||
}
|
return true
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user