From 1a6fcee6a78c3ba656c96a5fa2c733b6bbaacf33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=99=93=E4=BC=9F?= Date: Sun, 15 Oct 2023 20:11:24 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E5=AE=B9=E5=99=A8=E4=BB=93?= =?UTF-8?q?=E5=BA=93=20API:=20=E5=88=97=E5=87=BA=E4=BB=93=E5=BA=93?= =?UTF-8?q?=E5=86=85=E5=AD=98=E5=82=A8=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 26 ++++++ container_registry/container_registry.go | 112 +++++++++++++++++++++++ main.go | 2 + 3 files changed, 140 insertions(+) create mode 100644 container_registry/container_registry.go diff --git a/README.md b/README.md index 11d2379..275bf77 100644 --- a/README.md +++ b/README.md @@ -160,6 +160,7 @@ AUTHOR: COMMANDS: access-request, access-requests, ar 群组和项目访问请求 API,中文文档:https://docs.gitlab.cn/jh/api/access_requests.html board, boards 项目议题板 API,中文文档:https://docs.gitlab.cn/jh/api/boards.html + container-registry, cr 容器仓库 API,中文文档:https://docs.gitlab.cn/jh/api/container_registry.html instance-level-ci-variable, instance-level-ci-variables, ilcv 实例级 CI/CD 变量 API,中文文档:https://docs.gitlab.cn/jh/api/instance_level_ci_variables.html issue, issues 议题 API,中文文档:https://docs.gitlab.cn/jh/api/issues.html job-artifact, job-artifacts, ja 作业产物 API,中文文档:https://docs.gitlab.cn/jh/api/job_artifacts.html @@ -219,6 +220,31 @@ COPYRIGHT: --help, -h show help ``` +- [container-registry - 容器仓库 API](https://docs.gitlab.cn/jh/api/container_registry.html) + + ```shell + $ go run main.go container-registry --help + NAME: + gitlab-go container-registry - 容器仓库 API,中文文档:https://docs.gitlab.cn/jh/api/container_registry.html + + USAGE: + gitlab-go container-registry command [command options] [arguments...] + + COMMANDS: + list 列出仓库内存储库 + help, h Shows a list of commands or help for one command + + OPTIONS: + --base-url value 实例地址,例如:https://gitlab.xuxiaowei.com.cn/api/v4 (default: "https://gitlab.com/api/v4") [%CI_API_V4_URL%] + --token value your_access_token + --page value 页码(默认:1),中文文档 https://docs.gitlab.cn/jh/api/rest/index.html#pagination (default: 1) + --per-page value 每页列出的项目数(默认:20;最大:100),中文文档 https://docs.gitlab.cn/jh/api/rest/index.html#pagination (default: 20) + --print-json 打印 JSON (default: false) + --print-time 打印时间 (default: false) + --id value 项目 ID 或 URL 编码的路径 + --help, -h show help + ``` + - [instance-level-ci-variable - 实例级 CI/CD 变量 API](https://docs.gitlab.cn/jh/api/instance_level_ci_variables.html) ```shell diff --git a/container_registry/container_registry.go b/container_registry/container_registry.go new file mode 100644 index 0000000..9827bc3 --- /dev/null +++ b/container_registry/container_registry.go @@ -0,0 +1,112 @@ +package container_registry + +import ( + "encoding/json" + "fmt" + "github.com/urfave/cli/v2" + "github.com/xanzy/go-gitlab" + "github.com/xuxiaowei-com-cn/gitlab-go/constant" + "github.com/xuxiaowei-com-cn/gitlab-go/flag" + "log" +) + +// ContainerRegistry 容器仓库 API https://docs.gitlab.cn/jh/api/container_registry.html +func ContainerRegistry() *cli.Command { + return &cli.Command{ + Name: "container-registry", + Aliases: []string{"cr"}, + Usage: "容器仓库 API,中文文档:https://docs.gitlab.cn/jh/api/container_registry.html", + Flags: append(flag.Common(), flag.Page(), flag.PerPage(), flag.PrintJson(), flag.PrintTime(), + flag.Id(false)), + Subcommands: []*cli.Command{ + { + Name: "list", + Usage: "列出仓库内存储库", + Flags: append(flag.CommonTokenRequired(), flag.Page(), flag.PerPage(), flag.PrintJson(), flag.PrintTime(), + flag.Id(true)), + Action: func(context *cli.Context) error { + var baseUrl = context.String(constant.BaseUrl) + var token = context.String(constant.Token) + var id = context.String(constant.Id) + var page = context.Int(constant.Page) + var perPage = context.Int(constant.PerPage) + var printJson = context.Bool(constant.PrintJson) + var printTime = context.Bool(constant.PrintTime) + + gitClient, err := gitlab.NewClient(token, gitlab.WithBaseURL(baseUrl)) + if err != nil { + return err + } + opt := &gitlab.ListRegistryRepositoriesOptions{ + ListOptions: gitlab.ListOptions{ + Page: page, + PerPage: perPage, + }, + } + + registryRepositorys, response, err := gitClient.ContainerRegistry.ListProjectRegistryRepositories(id, opt) + log.Printf("Response StatusCode: %d\n", response.Response.StatusCode) + if err != nil { + return err + } + + fmt.Println("") + + if printJson { + if printTime { + for _, registryRepository := range registryRepositorys { + jsonData, err := json.Marshal(registryRepository) + if err != nil { + panic(err) + } + + log.Printf("\n%s\n", string(jsonData)) + fmt.Println("") + } + } else { + for _, registryRepository := range registryRepositorys { + jsonData, err := json.Marshal(registryRepository) + if err != nil { + panic(err) + } + + fmt.Printf("%s\n", string(jsonData)) + fmt.Println("") + } + } + } else { + if printTime { + for _, registryRepository := range registryRepositorys { + log.Printf("ID: %d\n", registryRepository.ID) + log.Printf("Name: %s\n", registryRepository.Name) + log.Printf("Path: %s\n", registryRepository.Path) + log.Printf("ProjectID: %d\n", registryRepository.ProjectID) + log.Printf("Location: %s\n", registryRepository.Location) + log.Printf("CreatedAt: %s\n", registryRepository.CreatedAt) + log.Printf("CleanupPolicyStartedAt: %s\n", registryRepository.CleanupPolicyStartedAt) + log.Printf("TagsCount: %d\n", registryRepository.TagsCount) + log.Printf("Tags: %s\n", registryRepository.Tags) + fmt.Println("") + } + } else { + for _, registryRepository := range registryRepositorys { + fmt.Printf("ID: %d\n", registryRepository.ID) + fmt.Printf("Name: %s\n", registryRepository.Name) + fmt.Printf("Path: %s\n", registryRepository.Path) + fmt.Printf("ProjectID: %d\n", registryRepository.ProjectID) + fmt.Printf("Location: %s\n", registryRepository.Location) + fmt.Printf("CreatedAt: %s\n", registryRepository.CreatedAt) + fmt.Printf("CleanupPolicyStartedAt: %s\n", registryRepository.CleanupPolicyStartedAt) + fmt.Printf("TagsCount: %d\n", registryRepository.TagsCount) + fmt.Printf("Tags: %s\n", registryRepository.Tags) + fmt.Println("") + } + } + } + + return nil + }, + }, + }, + } +} diff --git a/main.go b/main.go index b4afd22..24fa082 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "github.com/xuxiaowei-com-cn/git-go/buildinfo" "github.com/xuxiaowei-com-cn/gitlab-go/access_requests" "github.com/xuxiaowei-com-cn/gitlab-go/boards" + "github.com/xuxiaowei-com-cn/gitlab-go/container_registry" "github.com/xuxiaowei-com-cn/gitlab-go/instance_level_ci_variables" "github.com/xuxiaowei-com-cn/gitlab-go/issues" "github.com/xuxiaowei-com-cn/gitlab-go/job_artifacts" @@ -66,6 +67,7 @@ func main() { Commands: []*cli.Command{ access_requests.AccessRequests(), boards.Boards(), + container_registry.ContainerRegistry(), instance_level_ci_variables.InstanceLevelCiVariables(), issues.Issues(), job_artifacts.JobsArtifacts(),