2022-11-03 13:54:49 +00:00
|
|
|
package version
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2022-11-14 10:13:37 +00:00
|
|
|
"fmt"
|
2022-11-03 13:54:49 +00:00
|
|
|
|
|
|
|
|
"github.com/google/go-github/github"
|
|
|
|
|
"oss.terrastruct.com/cmdlog"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Pre-built binaries will have version set during build time.
|
|
|
|
|
var Version = "master (built from source)"
|
|
|
|
|
|
|
|
|
|
func CheckVersion(ctx context.Context, logger *cmdlog.Logger) {
|
2022-11-14 10:13:37 +00:00
|
|
|
fmt.Println(Version)
|
2022-11-03 13:54:49 +00:00
|
|
|
|
|
|
|
|
if Version == "master (built from source)" {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-14 10:13:37 +00:00
|
|
|
// Install script uses -v to check the version, we shouldn't be checking for
|
|
|
|
|
// updates here...
|
|
|
|
|
// https://github.com/terrastruct/d2/issues/49#issuecomment-1313229683
|
|
|
|
|
return
|
|
|
|
|
|
2022-11-03 13:54:49 +00:00
|
|
|
logger.Info.Printf("Checking for updates...")
|
|
|
|
|
latest, err := getLatestVersion(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Debug.Printf("Error reaching Github for latest version: %s", err.Error())
|
|
|
|
|
} else if Version != "master" && Version != latest {
|
|
|
|
|
logger.Info.Printf("A new version of D2 is available: %s -> %s", Version, latest)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getLatestVersion(ctx context.Context) (string, error) {
|
|
|
|
|
client := github.NewClient(nil)
|
|
|
|
|
rep, _, err := client.Repositories.GetLatestRelease(ctx, "terrastruct", "d2")
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return *rep.TagName, nil
|
|
|
|
|
}
|