format unix

This commit is contained in:
admju 2024-09-11 13:45:09 +00:00
parent 3271246e74
commit 6fef803ae9
3 changed files with 233 additions and 233 deletions

View File

@ -1,6 +1,6 @@
package occonst package occonst
var PUBLISH_URL = "https://cloud.o-forge.io" var PUBLISH_URL = "https://cloud.o-forge.io"
var PUBLISH_VERSION = "core/oc-deploy" var PUBLISH_VERSION = "core/oc-deploy"
var PUBLISH_TOKEN = "8dd3e2d88ff358d86742697f5a431ef96f6da7c7" var PUBLISH_TOKEN = ""

View File

@ -1,166 +1,166 @@
package releases package releases
import ( import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"mime/multipart" "mime/multipart"
"io" "io"
"encoding/json" "encoding/json"
"net/http" "net/http"
"bytes" "bytes"
"oc-publish/occonst" "oc-publish/occonst"
) )
type assetStruct struct { type assetStruct struct {
Name string `json:"name"` Name string `json:"name"`
Id int `json:"id"` Id int `json:"id"`
} }
func GetAssetId(idRelease int, name string) (int, error) { func GetAssetId(idRelease int, name string) (int, error) {
url := fmt.Sprintf("%s/api/v1/repos/%s/releases/%d/assets", url := fmt.Sprintf("%s/api/v1/repos/%s/releases/%d/assets",
occonst.PUBLISH_URL, occonst.PUBLISH_URL,
occonst.PUBLISH_VERSION, occonst.PUBLISH_VERSION,
idRelease) idRelease)
res, err := http.Get(url) res, err := http.Get(url)
if err != nil { if err != nil {
return -1, err return -1, err
} }
body, err := io.ReadAll(res.Body) body, err := io.ReadAll(res.Body)
if err != nil { if err != nil {
return -2, err return -2, err
} }
var data []assetStruct var data []assetStruct
err = json.Unmarshal(body, &data) err = json.Unmarshal(body, &data)
fmt.Println(err) fmt.Println(err)
if err != nil { if err != nil {
return -3, err return -3, err
} }
for _, ele := range data { for _, ele := range data {
if ele.Name == name { if ele.Name == name {
return ele.Id, nil return ele.Id, nil
} }
} }
return 0, nil return 0, nil
} }
// curl -X 'POST' \ // curl -X 'POST' \
// 'https://cloud.o-forge.io/api/v1/repos/core/oc-deploy/releases/2/assets?name=zzzz' \ // 'https://cloud.o-forge.io/api/v1/repos/core/oc-deploy/releases/2/assets?name=zzzz' \
// -H 'accept: application/json' \ // -H 'accept: application/json' \
// -H 'Content-Type: multipart/form-data' \ // -H 'Content-Type: multipart/form-data' \
// -F 'attachment=oc-deploy' // -F 'attachment=oc-deploy'
func CreateAsset(idRelease int, filename string) (int, error) { func CreateAsset(idRelease int, filename string) (int, error) {
url := fmt.Sprintf("%s/api/v1/repos/%s/releases/%d/assets?name=%s&token=%s", url := fmt.Sprintf("%s/api/v1/repos/%s/releases/%d/assets?name=%s&token=%s",
occonst.PUBLISH_URL, occonst.PUBLISH_URL,
occonst.PUBLISH_VERSION, occonst.PUBLISH_VERSION,
idRelease, idRelease,
"name", "name",
occonst.PUBLISH_TOKEN) occonst.PUBLISH_TOKEN)
// request, err := newfileUploadRequest(url, extraParams, "file", "/tmp/doc.pdf") // request, err := newfileUploadRequest(url, extraParams, "file", "/tmp/doc.pdf")
err := uploadFile(url, "attachment", filename) err := uploadFile(url, "attachment", filename)
fmt.Println(url, err) fmt.Println(url, err)
// fmt.Println(url) // fmt.Println(url)
// body := []byte("CONTENU") // body := []byte("CONTENU")
// req, err := http.NewRequest("POST", url, bytes.NewBuffer(body)) // req, err := http.NewRequest("POST", url, bytes.NewBuffer(body))
// if err != nil { // if err != nil {
// return -1, err // return -1, err
// } // }
// req.Header.Add("accept", "application/json") // req.Header.Add("accept", "application/json")
// req.Header.Add("Content-Type", "multipart/form-data") // req.Header.Add("Content-Type", "multipart/form-data")
// client := &http.Client{} // client := &http.Client{}
// res, err := client.Do(req) // res, err := client.Do(req)
// fmt.Println(res, err) // fmt.Println(res, err)
// cnt, err := io.ReadAll(res.Body) // cnt, err := io.ReadAll(res.Body)
// fmt.Println(string(cnt), err) // fmt.Println(string(cnt), err)
// if err != nil { // if err != nil {
// return -1, err // return -1, err
// } // }
// if err != nil { // if err != nil {
// return -2, err // return -2, err
// } // }
// defer res.Body.Close() // defer res.Body.Close()
return 0, nil return 0, nil
} }
func uploadFile(url string, paramName string, filePath string) error { func uploadFile(url string, paramName string, filePath string) error {
file, err := os.Open(filePath) file, err := os.Open(filePath)
if err != nil { if err != nil {
return err return err
} }
defer file.Close() defer file.Close()
body := &bytes.Buffer{} body := &bytes.Buffer{}
writer := multipart.NewWriter(body) writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile(paramName, filepath.Base(filePath)) part, err := writer.CreateFormFile(paramName, filepath.Base(filePath))
if err != nil { if err != nil {
return err return err
} }
_, err = io.Copy(part, file) _, err = io.Copy(part, file)
err = writer.Close() err = writer.Close()
if err != nil { if err != nil {
return err return err
} }
request, err := http.NewRequest("POST", url, body) request, err := http.NewRequest("POST", url, body)
request.Header.Add("Content-Type", writer.FormDataContentType()) request.Header.Add("Content-Type", writer.FormDataContentType())
request.Header.Add("accept", "application/json") request.Header.Add("accept", "application/json")
client := &http.Client{} client := &http.Client{}
response, err := client.Do(request) response, err := client.Do(request)
if err != nil { if err != nil {
return err return err
} }
defer response.Body.Close() defer response.Body.Close()
cnt, err := io.ReadAll(response.Body) cnt, err := io.ReadAll(response.Body)
fmt.Println(string(cnt), err, writer.FormDataContentType()) fmt.Println(string(cnt), err, writer.FormDataContentType())
// Handle the server response... // Handle the server response...
return nil return nil
} }
// func newfileUploadRequest(uri string, params map[string]string, paramName, path string) (*http.Request, error) { // func newfileUploadRequest(uri string, params map[string]string, paramName, path string) (*http.Request, error) {
// file, err := os.Open(path) // file, err := os.Open(path)
// if err != nil { // if err != nil {
// return nil, err // return nil, err
// } // }
// defer file.Close() // defer file.Close()
// body := &bytes.Buffer{} // body := &bytes.Buffer{}
// writer := multipart.NewWriter(body) // writer := multipart.NewWriter(body)
// part, err := writer.CreateFormFile(paramName, filepath.Base(path)) // part, err := writer.CreateFormFile(paramName, filepath.Base(path))
// if err != nil { // if err != nil {
// return nil, err // return nil, err
// } // }
// _, err = io.Copy(part, file) // _, err = io.Copy(part, file)
// // for key, val := range params { // // for key, val := range params {
// // _ = writer.WriteField(key, val) // // _ = writer.WriteField(key, val)
// // } // // }
// // err = writer.Close() // // err = writer.Close()
// // if err != nil { // // if err != nil {
// // return nil, err // // return nil, err
// // } // // }
// req, err := http.NewRequest("POST", uri, body) // req, err := http.NewRequest("POST", uri, body)
// req.Header.Set("Content-Type", writer.FormDataContentType()) // req.Header.Set("Content-Type", writer.FormDataContentType())
// return req, err // return req, err
// } // }

View File

@ -1,63 +1,63 @@
package releases package releases
import ( import (
"fmt" "fmt"
"io" "io"
"encoding/json" "encoding/json"
"net/http" "net/http"
"oc-publish/occonst" "oc-publish/occonst"
) )
type checkStruct struct { type checkStruct struct {
Name string `json:"name"` Name string `json:"name"`
Id int `json:"id"` Id int `json:"id"`
} }
func CheckRelease(version string) (bool, error) { func CheckRelease(version string) (bool, error) {
url := fmt.Sprintf("%s/api/v1/repos/%s/releases/tags/%s", url := fmt.Sprintf("%s/api/v1/repos/%s/releases/tags/%s",
occonst.PUBLISH_URL, occonst.PUBLISH_URL,
occonst.PUBLISH_VERSION, occonst.PUBLISH_VERSION,
version) version)
res, err := http.Get(url) res, err := http.Get(url)
if err != nil { if err != nil {
return false, err return false, err
} }
body, err := io.ReadAll(res.Body) body, err := io.ReadAll(res.Body)
if err != nil { if err != nil {
return false, err return false, err
} }
var data checkStruct var data checkStruct
err = json.Unmarshal(body, &data) err = json.Unmarshal(body, &data)
if err != nil { if err != nil {
return false, err return false, err
} }
return data.Name != "", nil return data.Name != "", nil
} }
func GetReleaseId(version string) (int, error) { func GetReleaseId(version string) (int, error) {
url := fmt.Sprintf("%s/api/v1/repos/%s/releases/tags/%s", url := fmt.Sprintf("%s/api/v1/repos/%s/releases/tags/%s",
occonst.PUBLISH_URL, occonst.PUBLISH_URL,
occonst.PUBLISH_VERSION, occonst.PUBLISH_VERSION,
version) version)
res, err := http.Get(url) res, err := http.Get(url)
if err != nil { if err != nil {
return 0, err return 0, err
} }
body, err := io.ReadAll(res.Body) body, err := io.ReadAll(res.Body)
if err != nil { if err != nil {
return 0, err return 0, err
} }
var data checkStruct var data checkStruct
err = json.Unmarshal(body, &data) err = json.Unmarshal(body, &data)
if err != nil { if err != nil {
return 0, err return 0, err
} }
return data.Id, nil return data.Id, nil
} }