Go——Artifactory的AQL查询以及json解析解决方案
Posted Starzkg
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Go——Artifactory的AQL查询以及json解析解决方案相关的知识,希望对你有一定的参考价值。
源代码:https://gitee.com/shentuzhigang/mini-project/blob/master/jfrog-client-demo/artifactory/
官方SDK:http://github.com/jfrog/jfrog-client-go
AQL:https://www.jfrog.com/confluence/display/JFROG/Artifactory+Query+Language
解决方案
package artifactory
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/config"
"github.com/jfrog/jfrog-client-go/utils/log"
"io/ioutil"
"os"
"testing"
"time"
)
type Range struct {
StartPos int `json:"start_pos"`
EndPos int `json:"end_pos"`
Total int `json:"total"`
}
type Result struct {
Repo string `json:"repo"`
Name string `json:"name"`
}
type AqlResponse struct {
Results []Result `json:"results"`
Range Range `json:"range"`
}
func TestAQLAndJSON(t *testing.T) {
var file *os.File
log.SetLogger(log.NewLogger(log.INFO, file))
rtDetails := auth.NewArtifactoryDetails()
rtDetails.SetUrl("http://192.168.0.112:8081/artifactory/")
rtDetails.SetUser("admin")
rtDetails.SetPassword("123456")
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)
reader, err := rtManager.Aql("items.find()")
if err != nil {
t.Fail()
return
}
bytes, err := ioutil.ReadAll(reader)
if err != nil {
t.Fail()
return
}
res := &AqlResponse{}
err = json.Unmarshal(bytes, res)
if err != nil {
t.Fail()
return
}
fmt.Println(res)
}
参考文章
以上是关于Go——Artifactory的AQL查询以及json解析解决方案的主要内容,如果未能解决你的问题,请参考以下文章