diff --git a/src/cmd/generateCmd.go b/src/cmd/generateCmd.go index 7486e76..f01d47e 100644 --- a/src/cmd/generateCmd.go +++ b/src/cmd/generateCmd.go @@ -9,19 +9,19 @@ import ( ) func GenerateCmd(project string, version string) { - log.Log().Info().Msg("Generate >> ") + log.Log().Info().Msg("Generate >> ") - version, err := versionOc.Get(version) - if err != nil { - log.Log().Fatal().Msg("OpenCloud >> " + err.Error()) - } - log.Log().Info().Msg(" >> Version : " + version) + version, err := versionOc.GetFromFile(version) + if err != nil { + log.Log().Fatal().Msg("OpenCloud >> " + err.Error()) + } + log.Log().Info().Msg(" >> Version : " + version) - obj := generate.GenerateClass{Workspace: "workspace_" + project, Version: version} - fic, err := obj.New() - if err != nil { - log.Log().Fatal().Msg(" >> " + err.Error()) - } - log.Log().Info().Msg(" >> Value : " + fic) + obj := generate.GenerateClass{Workspace: "workspace_" + project, Version: version} + fic, err := obj.New() + if err != nil { + log.Log().Fatal().Msg(" >> " + err.Error()) + } + log.Log().Info().Msg(" >> Value : " + fic) } \ No newline at end of file diff --git a/src/install/install.go b/src/install/install.go index 36a4310..ff388b6 100644 --- a/src/install/install.go +++ b/src/install/install.go @@ -38,20 +38,20 @@ func (this *InstallClass) NewInstall() (string, error) { this.Version = version } else { log.Log().Debug().Msg("Téléchargement du fichier de version") - version, err := versionOc.Get(this.Version) + // version, fileversion, err := versionOc.Get(this.Version) + version, fileversion, err := versionOc.GetFromOnline(this.Version) if err != nil { return "", err } - src := fmt.Sprintf("../offline/oc_%s.yml", version) - err = utils.CopyFile(src, dst) + this.Version = version + + err = utils.CopyContentFile(fileversion, dst) if err != nil { return "", err } } // Lecture du fichier de conf - // this.versionFile = dst - var err error this.tools, err = tool.FromConfigFile(dst) if err != nil { diff --git a/src/occonst/variables.go b/src/occonst/variables.go new file mode 100644 index 0000000..6fa10e6 --- /dev/null +++ b/src/occonst/variables.go @@ -0,0 +1,6 @@ +package occonst + +const ONLINE_URL = "https://cloud.o-forge.io" +const ONLINE_VERSION = "core/oc-deploy" + +var OFFLINE_DIR = "../../offline" diff --git a/src/utils/copyFile.go b/src/utils/copyFile.go index 13271e4..4e9ea16 100644 --- a/src/utils/copyFile.go +++ b/src/utils/copyFile.go @@ -32,4 +32,22 @@ func CopyFile(src string, dst string) (error) { return errCopy } return nil +} + +func CopyContentFile(content string, dst string) (error) { + + folderPath := filepath.Dir(dst) + os.MkdirAll(folderPath, os.ModePerm) + + fout, errCreate := os.Create(dst) + if errCreate != nil { + return errCreate + } + defer fout.Close() + + _, errCopy := fout.WriteString(content) + if errCopy != nil { + return errCopy + } + return nil } \ No newline at end of file diff --git a/src/versionOc/offline.go b/src/versionOc/offline.go new file mode 100644 index 0000000..1695822 --- /dev/null +++ b/src/versionOc/offline.go @@ -0,0 +1,71 @@ +// Package : +package versionOc + +import ( + "fmt" + "os" + "errors" + "io" + "path/filepath" + "gopkg.in/yaml.v2" + log "oc-deploy/log_wrapper" + "oc-deploy/occonst" +) + +type versionInput struct { + Version string `yaml:"version"` +} + +// Get : Retourne la version +func GetFromOffline(version string) (string, string, error) { + + version2 := version + if version == "latest" { + versionLatest, err := readLatestFromOffline() + if err != nil { + return "", "", err + } + version2 = versionLatest + } + + ficversion := fmt.Sprintf("oc_%s.yml", version2) + src := filepath.Join(occonst.OFFLINE_DIR, ficversion) + if _, err := os.Stat(src); err != nil { + log.Log().Debug().Msg(err.Error()) + return "", "", errors.New("Version non disponible") + } + + fin, err := os.Open(src) + if err != nil { + return "", "", err + } + defer fin.Close() + byteValue, err := io.ReadAll(fin) + + return version2, string(byteValue), nil +} + +// readLatestFile : Lit le numéro de la version dans le fichier lastest.yml +func readLatestFromOffline() (string, error) { + src := filepath.Join(occonst.OFFLINE_DIR, "latest.yml") + + fin, err := os.Open(src) + if err != nil { + return "", err + } + defer fin.Close() + + byteValue, err := io.ReadAll(fin) + if err != nil { + return "", err + } + + var objmap versionInput + + err = yaml.Unmarshal(byteValue, &objmap) + if err != nil { + return "", err + } + + return objmap.Version, nil +} diff --git a/src/versionOc/offline_test.go b/src/versionOc/offline_test.go new file mode 100644 index 0000000..85a84ec --- /dev/null +++ b/src/versionOc/offline_test.go @@ -0,0 +1,27 @@ +package versionOc + +import ( + "path/filepath" + + "testing" + + "github.com/stretchr/testify/assert" + + "oc-deploy/occonst" +) + +func TestGetOffline(t *testing.T) { + + _, _, err := GetFromOffline("99.1") + assert.NotNilf(t, err, "error message %s", err) +} + +func TestGetLatest(t *testing.T) { + + occonst.OFFLINE_DIR = filepath.Join(TEST_SRC_DIR, "offline") + + version, _, err := GetFromOffline("latest") + assert.Nilf(t, err, "error message %s", err) + assert.Equal(t, "99.1", version, "TestGetFromFile error") + +} \ No newline at end of file diff --git a/src/versionOc/online.go b/src/versionOc/online.go new file mode 100644 index 0000000..2db691b --- /dev/null +++ b/src/versionOc/online.go @@ -0,0 +1,132 @@ +package versionOc + +import ( + "fmt" + "io" + "errors" + "net/http" + "encoding/json" + "encoding/base64" + + "oc-deploy/occonst" + log "oc-deploy/log_wrapper" +) + + +func GetFromOnline(version string) (string, string, error) { + + version2 := version + if version == "latest" { + versionLatest, err := readLatestFromOnline() + if err != nil { + return "", "", err + } + version2 = versionLatest + } + + content, err := getFileVersion(version2) + if err != nil { + log.Log().Debug().Msg(err.Error()) + return "", "", errors.New("Version non disponible") + } + + return version2, content, nil +} + +type releaseStruct struct { + Id int `json:"id"` + Name string `json:"name"` +} + +type ocJsonStruct struct { + Value string `json:"value"` +} + +// Cherche dans Online la dernière version (non draft, non pre-releases) +func readLatestFromOnline() (string, error) { + + url := fmt.Sprintf("%s/api/v1/repos/%s/releases/latest", + occonst.ONLINE_URL, + occonst.ONLINE_VERSION) + + res, err := http.Get(url) + if err != nil { + return "", err + } + body, err := io.ReadAll(res.Body) + if err != nil { + return "", err + } + + var data releaseStruct + + err = json.Unmarshal(body, &data) + if err != nil { + return "", err + } + + return data.Name, err +} + +// Récupère le fichier version de la version +func getFileVersion(version string) (string, error) { + + url := fmt.Sprintf("%s/%s/releases/download/%s/oc.json", + occonst.ONLINE_URL, + occonst.ONLINE_VERSION, + version) + + res, err := http.Get(url) + if err != nil { + return "", err + } + body, err := io.ReadAll(res.Body) + if err != nil { + return "", err + } + + if string(body) == "Not Found\n" { + return "", fmt.Errorf("Not found %s", version) + } + + var data ocJsonStruct + + err = json.Unmarshal(body, &data) + if err != nil { + return "", err + } + + data64, err64 := base64.StdEncoding.DecodeString(data.Value) + if err64 != nil { + return "", err64 + } + + return string(data64), nil +} + +// Cherche l'Id de la release +func getIdRelease(version string) (int, error) { + url := fmt.Sprintf("%s/api/v1/repos/%s/releases/tags/%s", + occonst.ONLINE_URL, + occonst.ONLINE_VERSION, + version) + + res, err := http.Get(url) + if err != nil { + return 0, err + } + body, err := io.ReadAll(res.Body) + if err != nil { + return 0, err + } + + var data releaseStruct + + err = json.Unmarshal(body, &data) + if err != nil { + return 0, err + } + + + return data.Id, nil +} diff --git a/src/versionOc/versionOc.go b/src/versionOc/versionOc.go index 9e89b33..cf351f5 100644 --- a/src/versionOc/versionOc.go +++ b/src/versionOc/versionOc.go @@ -1,89 +1,30 @@ -// Package : -package versionOc - -import ( - "fmt" - "os" - "errors" - "io" - "path/filepath" - "gopkg.in/yaml.v2" - log "oc-deploy/log_wrapper" -) - -type versionInput struct { - Version string `yaml:"version"` -} - -var OFFLINE_DIR string = "../offline" - -func GetFromFile(fileversion string) (string, error) { - fin, err := os.Open(fileversion) - if err != nil { - return "", err - } - defer fin.Close() - - byteValue, err := io.ReadAll(fin) - if err != nil { - return "", err - } - - var objmap versionInput - - err = yaml.Unmarshal(byteValue, &objmap) - if err != nil { - return "", err - } - - return objmap.Version, nil -} - -// Get : Retourne la version -func Get(version string) (string, error) { - - version2 := version - if version == "latest" { - versionLatest, err := readLatestFile() - if err != nil { - return "", err - } - version2 = versionLatest - } - - ficversion := fmt.Sprintf("oc_%s.yml", version2) - src := filepath.Join(OFFLINE_DIR, ficversion) - if _, err := os.Stat(src); err != nil { - log.Log().Debug().Msg(err.Error()) - return "", errors.New("Version non disponible") - } - - return version2, nil -} - -// readLatestFile : Lit le numéro de la version dans le fichier lastest.yml -func readLatestFile() (string, error) { - src := filepath.Join(OFFLINE_DIR, "latest.yml") - - fin, err := os.Open(src) - - if err != nil { - return "", err - } - defer fin.Close() - - byteValue, err := io.ReadAll(fin) - if err != nil { - return "", err - } - - var objmap versionInput - - err = yaml.Unmarshal(byteValue, &objmap) - - if err != nil { - return "", err - } - - return objmap.Version, nil -} +// Package : +package versionOc + +import ( + "os" + "io" + "gopkg.in/yaml.v2" +) + +func GetFromFile(fileversion string) (string, error) { + fin, err := os.Open(fileversion) + if err != nil { + return "", err + } + defer fin.Close() + + byteValue, err := io.ReadAll(fin) + if err != nil { + return "", err + } + + var objmap versionInput + + err = yaml.Unmarshal(byteValue, &objmap) + if err != nil { + return "", err + } + + return objmap.Version, nil +} diff --git a/src/versionOc/versionOc_test.go b/src/versionOc/versionOc_test.go index a01a8af..3af6964 100644 --- a/src/versionOc/versionOc_test.go +++ b/src/versionOc/versionOc_test.go @@ -25,18 +25,18 @@ func TestGetFromFileErr(t *testing.T) { assert.NotNilf(t, err, "error message %s", ocyaml) } -func TestGet(t *testing.T) { +// func TestGet(t *testing.T) { - _, err := Get("99.1") - assert.NotNilf(t, err, "error message %s", err) -} +// _, err := Get("99.1") +// assert.NotNilf(t, err, "error message %s", err) +// } -func TestGetLatest(t *testing.T) { +// func TestGetLatest(t *testing.T) { - OFFLINE_DIR = filepath.Join(TEST_SRC_DIR, "offline") +// OFFLINE_DIR = filepath.Join(TEST_SRC_DIR, "offline") - version, err := Get("latest") - assert.Nilf(t, err, "error message %s", err) - assert.Equal(t, "99.1", version, "TestGetFromFile error") +// version, err := Get("latest") +// assert.Nilf(t, err, "error message %s", err) +// assert.Equal(t, "99.1", version, "TestGetFromFile error") -} \ No newline at end of file +// } \ No newline at end of file