Version online

This commit is contained in:
admju 2024-09-04 15:32:18 +00:00
parent 48301bf82e
commit a7a5465a22
9 changed files with 311 additions and 116 deletions

View File

@ -9,19 +9,19 @@ import (
) )
func GenerateCmd(project string, version string) { func GenerateCmd(project string, version string) {
log.Log().Info().Msg("Generate >> ") log.Log().Info().Msg("Generate >> ")
version, err := versionOc.Get(version) version, err := versionOc.GetFromFile(version)
if err != nil { if err != nil {
log.Log().Fatal().Msg("OpenCloud >> " + err.Error()) log.Log().Fatal().Msg("OpenCloud >> " + err.Error())
} }
log.Log().Info().Msg(" >> Version : " + version) log.Log().Info().Msg(" >> Version : " + version)
obj := generate.GenerateClass{Workspace: "workspace_" + project, Version: version} obj := generate.GenerateClass{Workspace: "workspace_" + project, Version: version}
fic, err := obj.New() fic, err := obj.New()
if err != nil { if err != nil {
log.Log().Fatal().Msg(" >> " + err.Error()) log.Log().Fatal().Msg(" >> " + err.Error())
} }
log.Log().Info().Msg(" >> Value : " + fic) log.Log().Info().Msg(" >> Value : " + fic)
} }

View File

@ -38,20 +38,20 @@ func (this *InstallClass) NewInstall() (string, error) {
this.Version = version this.Version = version
} else { } else {
log.Log().Debug().Msg("Téléchargement du fichier de version") 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 { if err != nil {
return "", err return "", err
} }
src := fmt.Sprintf("../offline/oc_%s.yml", version) this.Version = version
err = utils.CopyFile(src, dst)
err = utils.CopyContentFile(fileversion, dst)
if err != nil { if err != nil {
return "", err return "", err
} }
} }
// Lecture du fichier de conf // Lecture du fichier de conf
// this.versionFile = dst
var err error var err error
this.tools, err = tool.FromConfigFile(dst) this.tools, err = tool.FromConfigFile(dst)
if err != nil { if err != nil {

6
src/occonst/variables.go Normal file
View File

@ -0,0 +1,6 @@
package occonst
const ONLINE_URL = "https://cloud.o-forge.io"
const ONLINE_VERSION = "core/oc-deploy"
var OFFLINE_DIR = "../../offline"

View File

@ -33,3 +33,21 @@ func CopyFile(src string, dst string) (error) {
} }
return nil 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
}

71
src/versionOc/offline.go Normal file
View File

@ -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
}

View File

@ -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")
}

132
src/versionOc/online.go Normal file
View File

@ -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
}

View File

@ -2,21 +2,11 @@
package versionOc package versionOc
import ( import (
"fmt"
"os" "os"
"errors"
"io" "io"
"path/filepath"
"gopkg.in/yaml.v2" "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) { func GetFromFile(fileversion string) (string, error) {
fin, err := os.Open(fileversion) fin, err := os.Open(fileversion)
if err != nil { if err != nil {
@ -38,52 +28,3 @@ func GetFromFile(fileversion string) (string, error) {
return objmap.Version, nil 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
}

View File

@ -25,18 +25,18 @@ func TestGetFromFileErr(t *testing.T) {
assert.NotNilf(t, err, "error message %s", ocyaml) assert.NotNilf(t, err, "error message %s", ocyaml)
} }
func TestGet(t *testing.T) { // func TestGet(t *testing.T) {
_, err := Get("99.1") // _, err := Get("99.1")
assert.NotNilf(t, err, "error message %s", err) // 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") // version, err := Get("latest")
assert.Nilf(t, err, "error message %s", err) // assert.Nilf(t, err, "error message %s", err)
assert.Equal(t, "99.1", version, "TestGetFromFile error") // assert.Equal(t, "99.1", version, "TestGetFromFile error")
} // }