在jfrog 获取所有仓库 博客中,我们已经拿到了所有关于docker仓库的信息,接着我们使用AQL查询指定仓库中的镜像信息。
代码如下:
package main
import (
"encoding/json"
"fmt"
"github.com/jfrog/jfrog-client-go/artifactory"
"github.com/jfrog/jfrog-client-go/artifactory/auth"
"github.com/jfrog/jfrog-client-go/artifactory/services"
"io/ioutil"
"github.com/jfrog/jfrog-client-go/config"
"github.com/jfrog/jfrog-client-go/utils/log"
"os"
"time"
)
type Result struct {
Repo string `json:"repo"`
Path string `json:"path"`
Name string `json:"name"`
Type string `json:"type"`
Size int `json:"size"`
Created time.Time `json:"created"`
Created_by string `json:"created_by"`
Modified string `json:"modified"`
Modified_by string `json:"modified_by"`
Updated time.Time `json:"updated"`
}
type Range struct {
Start_pos int `json:"start_pos"`
End_pos int `json:"end_pos"`
Total int `json:"total"`
}
type ImagesInfo struct {
Res[] Result `json:"results"`
Ran Range `json:"range"`
}
func main() {
var file *os.File
log.SetLogger(log.NewLogger(log.INFO, file))
rtDetails := auth.NewArtifactoryDetails()
rtDetails.SetUrl("http://localhost:8081/artifactory/")
rtDetails.SetUser("admin")
rtDetails.SetPassword("password")
serviceConfig, err := config.NewConfigBuilder().
SetServiceDetails(rtDetails).
// Optionally overwrite the default HTTP timeout, which is set to 30 seconds.
SetHttpTimeout(180 * time.Second).
// Optionally overwrite the default HTTP retries, which is set to 3.
SetHttpRetries(8).
Build()
if err != nil {
fmt.Println(err)
}
rtManager, err := artifactory.New(serviceConfig)
params := services.NewRepositoriesFilterParams()
params.PackageType = "Docker"
params.RepoType = "LOCAL"
reposDetail, err := rtManager.GetAllRepositoriesFiltered(params)
if err != nil {
return
}
for _, v := range *reposDetail {
log.Output(v)
str := fmt.Sprintf("items.find({\"repo\": \"%s\"})", v.Key)
reader, err := rtManager.Aql(str)
if nil != err{
continue
}
content, err := ioutil.ReadAll(reader)
fmt.Println(string(content))
var imgInfo ImagesInfo
json.Unmarshal(content, &imgInfo)
for i := 0; i < imgInfo.Ran.Total; i++ {
data , _ :=json.Marshal(imgInfo.Res[i])
fmt.Println(string(data))
}
}
}
ImagesInfo :将json格式的数据转换成这个对象
结果如下:
GOROOT=D:\go #gosetup
GOPATH=D:\go_pkg #gosetup
D:\go\bin\go.exe build -o C:\Users\Administrator\AppData\Local\Temp\___1go_build_main_go.exe D:\test\go\jfrog\main.go #gosetup
C:\Users\Administrator\AppData\Local\Temp\___1go_build_main_go.exe #gosetup
{hello_world LOCAL http://192.168.0.105:8081/artifactory/hello_world Docker}
{
"results" : [ {
"repo" : "hello_world",
"path" : ".",
"name" : "repository.catalog",
"type" : "file",
"size" : 41,
"created" : "2022-04-22T20:55:45.581+08:00",
"created_by" : "admin",
"modified" : "2022-04-22T20:55:45.558+08:00",
"modified_by" : "admin",
"updated" : "2022-04-22T20:55:45.592+08:00"
},{
"repo" : "hello_world",
"path" : "ubuntu/v2",
"name" : "manifest.json",
"type" : "file",
"size" : 529,
"created" : "2022-04-22T20:55:45.497+08:00",
"created_by" : "admin",
"modified" : "2022-04-22T20:55:45.473+08:00",
"modified_by" : "admin",
"updated" : "2022-04-22T20:55:45.498+08:00"
},{
"repo" : "hello_world",
"path" : "ubuntu/v2",
"name" : "sha256__4d32b49e2995210e8937f0898327f196d3fcc52486f0be920e8b2d65f150a7ab",
"type" : "file",
"size" : 28565909,
"created" : "2022-04-22T20:55:44.959+08:00",
"created_by" : "admin",
"modified" : "2022-04-22T20:55:35.008+08:00",
"modified_by" : "admin",
"updated" : "2022-04-22T20:55:44.964+08:00"
},{
"repo" : "hello_world",
"path" : "ubuntu/v2",
"name" : "sha256__ff0fea8310f3957d9b1e6ba494f3e4b63cb348c76160c6c15578e65995ffaa87",
"type" : "file",
"size" : 1461,
"created" : "2022-04-22T20:55:45.341+08:00",
"created_by" : "admin",
"modified" : "2022-04-22T20:55:45.330+08:00",
"modified_by" : "admin",
"updated" : "2022-04-22T20:55:45.342+08:00"
} ],
"range" : {
"start_pos" : 0,
"end_pos" : 4,
"total" : 4
}
}
{"repo":"hello_world","path":".","name":"repository.catalog","type":"file","size":41,"created":"2022-04-22T20:55:45.581+08:00","created_by":"admin","modified":"2022-04-22T20:55:45.558+08:00","modified_by":"admin","updated":"2022-04-22T20:55:45.592+08:00"}
{"repo":"hello_world","path":"ubuntu/v2","name":"manifest.json","type":"file","size":529,"created":"2022-04-22T20:55:45.497+08:00","created_by":"admin","modified":"2022-04-22T20:55:45.473+08:00","modified_by":"admin","updated":"2022-04-22T20:55:45.498+08:00"}
{"repo":"hello_world","path":"ubuntu/v2","name":"sha256__4d32b49e2995210e8937f0898327f196d3fcc52486f0be920e8b2d65f150a7ab","type":"file","size":28565909,"created":"2022-04-22T20:55:44.959+08:00","created_by":"admin","modified":"2022-04-22T20:55:35.008+08:00","modified_by":"admin","updated":"2022-04-22T20:55:44.964+08:00"}
{"repo":"hello_world","path":"ubuntu/v2","name":"sha256__ff0fea8310f3957d9b1e6ba494f3e4b63cb348c76160c6c15578e65995ffaa87","type":"file","size":1461,"created":"2022-04-22T20:55:45.341+08:00","created_by":"admin","modified":"2022-04-22T20:55:45.330+08:00","modified_by":"admin","updated":"2022-04-22T20:55:45.342+08:00"}
Process finished with the exit code 0
我们登录管理界面看看是否一致:
是一致的
我们用curl来操作时,需要注意header需要设置content-type、请求方法、请求体之类的设置
例如:
curl -u admin:password http://lcoalhost:8081/artifactory/api/search/aql -X POST -H "Content-Type: text/plain" -d "items.find({\"repo\": \"hello_world\"})"