drone-kuboard/update.go
2023-10-17 16:32:16 +08:00

72 lines
1.5 KiB
Go

package main
import (
"bytes"
"encoding/json"
"errors"
"github.com/urfave/cli/v2"
"io"
"net/http"
"strings"
)
func update(ctx *cli.Context) error {
cluster := ctx.Value("cluster").(string)
uri := strings.Trim(ctx.Value("kuboard_uri").(string), "/")
uri = uri + "/kuboard-api/cluster/" + cluster + "/kind/CICDApi/admin/resource/updateImageTag"
image := strings.Trim(ctx.Value("image").(string), "/")
tag := ctx.Value("tag").(string)
data, err := json.Marshal(payload{
Name: ctx.Value("name").(string),
Namespace: ctx.Value("namespace").(string),
Kind: ctx.Value("kind").(string),
Images: map[string]string{
image: image + ":" + tag,
},
})
if err != nil {
return err
}
request, err := http.NewRequest("PUT", uri, bytes.NewBuffer(data))
if err != nil {
return err
}
request.Header.Add("Content-Type", "application/json")
request.AddCookie(&http.Cookie{
Name: "KuboardUsername",
Value: ctx.Value("kuboard_username").(string),
})
request.AddCookie(&http.Cookie{
Name: "KuboardAccessKey",
Value: ctx.Value("kuboard_key").(string),
})
client := &http.Client{}
response, err := client.Do(request)
if err != nil {
return err
}
defer response.Body.Close()
if response.StatusCode != 200 {
body, _ := io.ReadAll(response.Body)
errResp := &errorResponse{}
err := json.Unmarshal(body, errResp)
if err != nil {
return errors.New(response.Status)
} else {
return errors.New(errResp.Reason)
}
}
return err
}