使用golang 进行git批量下载

阅读更多
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)