init commit
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
package images
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"floares/config"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type DeleteRsp struct {
|
||||
Cause string `json:"cause"` //cause image is in use by a container
|
||||
Message string `json:"message"`
|
||||
Response int `json:"response"`
|
||||
}
|
||||
type DeleteRsult struct {
|
||||
Code int `json:"code"`
|
||||
Error string `json:"err,omitempty"`
|
||||
}
|
||||
|
||||
func Delete(id string) DeleteRsult {
|
||||
req, _ := http.NewRequestWithContext(context.Background(), http.MethodDelete, config.Entrypoint+fmt.Sprintf("/images/%s", id), nil)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return DeleteRsult{
|
||||
Error: err.Error(),
|
||||
}
|
||||
}
|
||||
var deleteRsult DeleteRsp
|
||||
var response DeleteRsult
|
||||
response.Code = resp.StatusCode
|
||||
_ = json.NewDecoder(resp.Body).Decode(&deleteRsult)
|
||||
log.Println(deleteRsult)
|
||||
if deleteRsult.Cause != "" {
|
||||
return DeleteRsult{
|
||||
Error: deleteRsult.Cause,
|
||||
}
|
||||
}
|
||||
return response
|
||||
}
|
||||
@@ -1 +1,40 @@
|
||||
package images
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"floares/config"
|
||||
"floares/lib/model"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Image struct {
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Tag string `json:"tag"`
|
||||
Digest string `json:"digest"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
Size string `json:"size"`
|
||||
}
|
||||
|
||||
func List() []Image {
|
||||
var images []model.Image
|
||||
|
||||
req, _ := http.NewRequest(http.MethodGet, config.Entrypoint+"/images/json", nil)
|
||||
resp, _ := http.DefaultClient.Do(req)
|
||||
defer resp.Body.Close()
|
||||
json.NewDecoder(resp.Body).Decode(&images)
|
||||
var imagesList []Image
|
||||
for _, v := range images {
|
||||
imagesList = append(imagesList, Image{
|
||||
Id: v.Id[7:],
|
||||
Name: v.Names[0],
|
||||
Tag: v.RepoTags[0],
|
||||
Digest: v.Digest,
|
||||
CreatedAt: time.Unix(int64(v.Created), 0).Format(time.DateTime),
|
||||
Size: strconv.Itoa(v.Size/1024/1024) + " MB",
|
||||
})
|
||||
}
|
||||
return imagesList
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package images
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"floares/config"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type PullRsp struct {
|
||||
Images []string `json:"images"`
|
||||
Id string `json:"id"`
|
||||
Error string `json:"errr,omitempty"`
|
||||
Info string `json:"info,omitempty"`
|
||||
}
|
||||
|
||||
func Pull(ref string) PullRsp {
|
||||
var pull PullRsp
|
||||
req, _ := http.NewRequestWithContext(context.Background(), http.MethodPost, config.Entrypoint+fmt.Sprintf("/v2.2.2/libpod/images/pull?reference=%s&quiet=true", ref), nil)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return PullRsp{
|
||||
Error: err.Error(),
|
||||
}
|
||||
}
|
||||
defer func(Body io.ReadCloser) {
|
||||
err = Body.Close()
|
||||
if err != nil {
|
||||
log.Printf("Failed to close response body: %v", err)
|
||||
}
|
||||
}(resp.Body)
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return PullRsp{
|
||||
Error: "status code " + resp.Status,
|
||||
}
|
||||
}
|
||||
_ = json.NewDecoder(resp.Body).Decode(&pull)
|
||||
|
||||
return pull
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user