Beispiel #1
0
func ExampleFetchFilesCurl() {
	// Code that should be outside of your function body.
	// type rawFile struct {
	// name   string
	// 	rawURL string
	// 	data   []byte
	// }

	// func (rf *rawFile) Name() string {
	// 	return rf.name
	// }

	// func (rf *rawFile) RawUrl() string {
	// 	return rf.rawURL
	// }

	// func (rf *rawFile) Data() []byte {
	// 	return rf.data
	// }

	// func (rf *rawFile) SetData(p []byte) {
	// 	rf.data = p
	// }

	files := []com.RawFile{
		&rawFile{rawURL: "http://example.com"},
		&rawFile{rawURL: "http://example.com/foo"},
	}
	err := com.FetchFilesCurl(files)
	fmt.Println(err, len(files[0].Data()), len(files[1].Data()))
}
Beispiel #2
0
func fetchGoogleFiles(files []com.RawFile) error {
	count := len(files)
	step := 5
	start := 0
	end := step
	isExit := false

	for {
		if end > count {
			end = count
			isExit = true
		}

		if err := com.FetchFilesCurl(files[start:end]); err != nil {
			return err
		}

		if isExit {
			return nil
		}

		start += step
		end += step
	}
}
Beispiel #3
0
func downloadFiles(client *http.Client, match map[string]string, rootPath, installPath, commit string, dirs []string) error {
	suf := "?r=" + commit
	if len(commit) == 0 {
		suf = ""
	}

	for _, d := range dirs {
		p, err := com.HttpGetBytes(client, rootPath+d+suf, nil)
		if err != nil {
			return err
		}

		// Create destination directory.
		os.MkdirAll(installPath+d, os.ModePerm)

		// Get source files in current path.
		files := make([]com.RawFile, 0, 5)
		for _, m := range googleFileRe.FindAllSubmatch(p, -1) {
			fname := strings.Split(string(m[1]), "?")[0]
			files = append(files, &rawFile{
				name:   fname,
				rawURL: rootPath + d + fname + suf,
			})
		}

		// Fetch files from VCS.
		if err := com.FetchFilesCurl(files); err != nil {
			return err
		}

		// Save files.
		for _, f := range files {
			absPath := installPath + d

			// Create diretory before create file.
			os.MkdirAll(path.Dir(absPath), os.ModePerm)

			// Write data to file
			fw, err := os.Create(absPath + f.Name())
			if err != nil {
				return err
			}

			_, err = fw.Write(f.Data())
			fw.Close()
			if err != nil {
				return err
			}
		}
		files = nil

		subdirs := make([]string, 0, 3)
		// Get subdirectories.
		for _, m := range googleDirRe.FindAllSubmatch(p, -1) {
			dirName := strings.Split(string(m[1]), "?")[0]
			if strings.HasSuffix(dirName, "/") {
				subdirs = append(subdirs, d+dirName)
			}
		}

		err = downloadFiles(client, match, rootPath, installPath, commit, subdirs)
		if err != nil {
			return err
		}
	}
	return nil
}