From b1188960dfd86f075b5eba2cc4d5f2047b7600ec Mon Sep 17 00:00:00 2001 From: Tom Chauveau Date: Fri, 10 Dec 2021 16:29:37 +0100 Subject: [PATCH] Add universe check version in europa up Signed-off-by: Vasek - Tom C --- cmd/dagger/cmd/up.go | 27 ++++++++++++++++++++++++++ mod/mod.go | 45 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/cmd/dagger/cmd/up.go b/cmd/dagger/cmd/up.go index 12ccf559..7ad1ad98 100644 --- a/cmd/dagger/cmd/up.go +++ b/cmd/dagger/cmd/up.go @@ -3,6 +3,7 @@ package cmd import ( "context" "errors" + "fmt" "os" "cuelang.org/go/cue" @@ -12,6 +13,7 @@ import ( "go.dagger.io/dagger/cmd/dagger/logger" "go.dagger.io/dagger/compiler" "go.dagger.io/dagger/environment" + "go.dagger.io/dagger/mod" "go.dagger.io/dagger/plan" "go.dagger.io/dagger/plancontext" "go.dagger.io/dagger/solver" @@ -74,6 +76,11 @@ var upCmd = &cobra.Command{ Str("environment", st.Name). Logger() + universeUpdateCh := make(chan bool) + go func() { + universeUpdateCh <- checkUniverseVersion(ctx, project.Path) + }() + doneCh := common.TrackProjectCommand(ctx, cmd, project, st) env, err := environment.New(st) @@ -109,9 +116,29 @@ var upCmd = &cobra.Command{ if err != nil { lg.Fatal().Err(err).Msg("failed to up environment") } + + // Warn universe version if out of date + if update := <-universeUpdateCh; update { + fmt.Println("A new version of universe is available, please run 'dagger mod get alpha.dagger.io'") + } }, } +func checkUniverseVersion(ctx context.Context, projectPath string) bool { + lg := log.Ctx(ctx) + + isLatest, err := mod.IsUniverseLatest(ctx, projectPath) + if err != nil { + lg.Debug().Err(err).Msg("failed to check universe version") + return false + } + if !isLatest { + return true + } + lg.Debug().Msg("universe is up to date") + return false +} + func europaUp(ctx context.Context, cl *client.Client, args ...string) error { lg := log.Ctx(ctx) diff --git a/mod/mod.go b/mod/mod.go index 21bfd0d1..84f58912 100644 --- a/mod/mod.go +++ b/mod/mod.go @@ -2,6 +2,7 @@ package mod import ( "context" + "fmt" "os" "path" "strings" @@ -19,6 +20,50 @@ func isUniverse(repoName string) bool { return strings.HasPrefix(strings.ToLower(repoName), stdlib.ModuleName) } +// IsUniverseLatest check that current universe is up-to-date or no +// It returns true if universe is up-to-date, otherwise false +// If universe was not installed from `dagger mod get`, it will +// not compare anything. +// +// The latest tag is fetch from universe repo itself. +func IsUniverseLatest(ctx context.Context, workspace string) (bool, error) { + modfile, err := readPath(workspace) + if err != nil { + return false, err + } + + req, err := newRequire("alpha.dagger.io", UniverseVersionConstraint) + if err != nil { + return false, err + } + + // Get current universe version + universe := modfile.searchInstalledRequire(req) + if universe == nil { + return false, fmt.Errorf("universe not installed") + } + + tmpPath := path.Join(modfile.workspacePath, tmpBasePath, req.fullPath()) + defer os.RemoveAll(tmpPath) + + repo, err := clone(ctx, req, tmpPath, "", "") + if err != nil { + return false, err + } + + // Get latest tag + latestTag, err := repo.latestTag(ctx, req.versionConstraint) + if err != nil { + return false, err + } + + c, err := compareVersions(latestTag, universe.version) + if err != nil { + return false, err + } + return !(c == 1), nil +} + func Install(ctx context.Context, workspace, repoName, versionConstraint string) (*Require, error) { lg := log.Ctx(ctx)