欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用golang 进行git批量下载 golang 

程序员文章站 2022-05-28 15:00:52
...
package main

import (
	"os"
	"bufio"
	"os/exec"
	"fmt"
	"io"
	"sync"
	"bytes"
)

func main() {

	if len(os.Args) != 2 {

		panic("非法的参数")
		return

	}

	inFilePath := os.Args[1];
	inFile, err := os.Open(inFilePath)

	if err != nil {
		panic(err)
		return
	}

	defer inFile.Close()

	reader := bufio.NewReader(inFile)
	waiter := &sync.WaitGroup{}
	for {

		line, _, err := reader.ReadLine();
		if err != nil {

			if err != io.EOF {
				panic(err)

			}

			break
		}
		fmt.Println(string(line))
		waiter.Add(1)
		go worker(string(line), waiter)

	}

	waiter.Wait()

	fmt.Println("============所有都已下载完毕!==============")

}

func worker(url string, waiter *sync.WaitGroup) {

	command := exec.Command("git", "clone", string(url))
	fmt.Println(command.Args)

	var bys bytes.Buffer
	command.Stdout = &bys

	fmt.Printf("=====>开始下载:%s<========\n", url)

	err := command.Start()
	if err != nil {
		panic(err)
	}

	command.Wait()

	fmt.Println(bys.String())
	waiter.Done()

	fmt.Printf("=====>下载完毕:%s<========\n", url)

}

 

相关标签: golang