智慧屏安装APP的最佳实践与跨平台小程序开发的结合
1008
2022-11-19
golang操作gitlab API:获取某个group下的所有仓库并写入文件 && 解决gitpab限制单次查询100条的限制
获取某个group下的仓库: mainimport ( "bufio" "encoding/json" "fmt" "io" "log" "math" "net/ "os" "os/exec" "strconv" "github.com/bitly/go-simplejson")const ( PRIVATE_TOKEN = "ThmspJQsBdf99nBYEo5Ns-wRxxx" GET_DMCCA_HEAD = " REPOS_FILE = "gitlab-url.txt")var ( client *= & repos string)func getTotal() { head, err := GET_DMCCA_HEAD, nil) if err != nil { log.Panicln(err) } head.Header.Add("PRIVATE-TOKEN", PRIVATE_TOKEN) r, err := client.Do(head) if err != nil { log.Panicln(err) } defer r.Body.Close() totalNums, _ := strconv.Atoi(r.Header.Get("X-Total")) fmt.Printf("totalNums: %v\n", totalNums) page := int(math.Ceil(float64(totalNums) / 100)) for i := 1; i <= page; i++ { getProjects(i) }}func getProjects(page int) { GET_DMCCA_PROJECTS := "+ strconv.Itoa(page) + "&simple=true" req, err := GET_DMCCA_PROJECTS, nil) if err != nil { log.Panicln(err) } req.Header.Add("PRIVATE-TOKEN", PRIVATE_TOKEN) resp, err := client.Do(req) if err != nil { log.Panicln(err) } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { log.Panicln(err) } var aa []map[string]interface{} json.Unmarshal(body, &aa) // fmt.Printf("len(aa): %v\n", len(aa)) for _, project := range aa { byt, _ := json.Marshal(project) json, _ := simplejson.NewJson(byt) s, _ := json.Get(" repos = s + "\n" + repos }}func WriteFile() { fmt.Printf("repos: %v\n", repos) file, err2 := os.OpenFile(REPOS_FILE, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0777) defer file.Close() if err2 != nil { log.Panicln(err2) } w := bufio.NewWriter(file) w.WriteString(repos) w.Flush()}func main() { // 获取group_id group下所有代码仓库 getTotal() // 将git仓库URL地址写入文件 WriteFile() // 删除最后一行的空行 sed := exec.Command("sed -i ':n;/^\n*$/{$! N;;bn}' ", REPOS_FILE) sed.Run()}
gitlab文档地址: https://ranying666.github.io/2017/06/20/gitlab-api/
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~