Complete version checking with getUniverseCurrentVersion

Dagger now compare the remote universe version with the one written
in dagger.mod.

I've also added logs to easily debug code

Signed-off-by: Tom Chauveau <tom.chauveau@epitech.eu>
This commit is contained in:
Tom Chauveau 2021-10-29 19:31:59 +02:00
parent 927762481a
commit 5f0a4202c9
No known key found for this signature in database
GPG Key ID: 3C9847D981AAC1BF
2 changed files with 43 additions and 8 deletions

View File

@ -1,6 +1,7 @@
package cmd package cmd
import ( import (
"context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
@ -16,6 +17,8 @@ import (
"github.com/mitchellh/go-homedir" "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"go.dagger.io/dagger/cmd/dagger/cmd/common"
"go.dagger.io/dagger/cmd/dagger/logger"
"go.dagger.io/dagger/mod" "go.dagger.io/dagger/mod"
"go.dagger.io/dagger/version" "go.dagger.io/dagger/version"
"golang.org/x/term" "golang.org/x/term"
@ -46,8 +49,10 @@ var versionCmd = &cobra.Command{
runtime.GOOS, runtime.GOARCH, runtime.GOOS, runtime.GOARCH,
) )
// TODO Display universe version universeVersion, err := getUniverseCurrentVersion()
// How can I retrieve it if it's not vendor ? if err == nil {
fmt.Printf("universe %s\n", universeVersion.Original())
}
if check := viper.GetBool("check"); check { if check := viper.GetBool("check"); check {
versionFilePath, err := homedir.Expand(versionFile) versionFilePath, err := homedir.Expand(versionFile)
@ -215,10 +220,33 @@ func getUniverseLatestVersion() (*goVersion.Version, error) {
return versions[0], nil return versions[0], nil
} }
// Retrieve the current universe version from `cue.mod/dagger.mod`
func getUniverseCurrentVersion() (*goVersion.Version, error) { func getUniverseCurrentVersion() (*goVersion.Version, error) {
// TODO Should be replaced with the current universe version project := common.CurrentProject(context.Background())
// How I can fetch it pathMod := path.Join(project.Path, mod.ModFilePath)
return goVersion.NewVersion("0.1.0") fileMod, err := os.Open(pathMod)
if err != nil {
return nil, err
}
defer fileMod.Close()
data, err := ioutil.ReadAll(fileMod)
if err != nil {
return nil, err
}
currentVersion := ""
modules := strings.Split(string(data), "\n")
for _, module := range modules {
if !strings.HasPrefix(module, "alpha.dagger.io") {
continue
}
// Retrieve tag
tag := strings.Split(module, " ")
currentVersion = tag[1]
}
return goVersion.NewVersion(currentVersion)
} }
// Compare the universe version with the latest version online // Compare the universe version with the latest version online
@ -241,8 +269,11 @@ func isUniverseVersionLatest() (string, error) {
} }
func checkVersion() { func checkVersion() {
lg := logger.New()
if version.Version == version.DevelopmentVersion { if version.Version == version.DevelopmentVersion {
// running devel version // running devel version
lg.Debug().Msg("ignore check version on devel version")
return return
} }
@ -264,8 +295,10 @@ func checkVersion() {
} }
// Check version // Check version
lg.Debug().Msg("check for universe latest version...")
universeLatestVersion, err := isUniverseVersionLatest() universeLatestVersion, err := isUniverseVersionLatest()
if err != nil { if err != nil {
lg.Debug().Msg(err.Error())
return return
} }
@ -274,8 +307,10 @@ func checkVersion() {
} }
// Check timestamp // Check timestamp
lg.Debug().Msg("check for dagger latest version...")
daggerLatestVersion, err := isDaggerVersionLatest() daggerLatestVersion, err := isDaggerVersionLatest()
if err != nil { if err != nil {
lg.Debug().Msg(err.Error())
return return
} }

View File

@ -17,7 +17,7 @@ import (
) )
const ( const (
modFilePath = "./cue.mod/dagger.mod" ModFilePath = "./cue.mod/dagger.mod"
sumFilePath = "./cue.mod/dagger.sum" sumFilePath = "./cue.mod/dagger.sum"
lockFilePath = "./cue.mod/dagger.lock" lockFilePath = "./cue.mod/dagger.lock"
destBasePath = "./cue.mod/pkg" destBasePath = "./cue.mod/pkg"
@ -31,7 +31,7 @@ type file struct {
} }
func readPath(workspacePath string) (*file, error) { func readPath(workspacePath string) (*file, error) {
pMod := path.Join(workspacePath, modFilePath) pMod := path.Join(workspacePath, ModFilePath)
fMod, err := os.Open(pMod) fMod, err := os.Open(pMod)
if err != nil { if err != nil {
if !errors.Is(err, fs.ErrNotExist) { if !errors.Is(err, fs.ErrNotExist) {
@ -276,7 +276,7 @@ func (f *file) write() error {
bMod.WriteString(fmt.Sprintf("%s %s\n", r.fullPath(), r.version)) bMod.WriteString(fmt.Sprintf("%s %s\n", r.fullPath(), r.version))
} }
err := ioutil.WriteFile(path.Join(f.workspacePath, modFilePath), bMod.Bytes(), 0600) err := ioutil.WriteFile(path.Join(f.workspacePath, ModFilePath), bMod.Bytes(), 0600)
if err != nil { if err != nil {
return err return err
} }