Esempio n. 1
0
File: github.go Progetto: yl10/goxc
//POST https://<upload_url>/repos/:owner/:repo/releases/:id/assets?name=foo.zip
func ghDoUpload(apiHost, apikey, owner, repository, release, relativePath, fullPath, contentType string, isVerbose, isQuiet bool) error {
	//POST /repos/:owner/:repo/releases/:id/assets?name=foo.zip
	url := apiHost + "/repos/" + owner + "/" + repository + "/releases/" + release + "/assets?name=" + relativePath
	if !isQuiet {
		log.Printf("Uploading to %v", url)
	}
	resp, err := httpc.UploadFile("POST", url, repository, owner, apikey, fullPath, relativePath, contentType, isVerbose)
	if err != nil {
		if serr, ok := err.(httpc.HttpError); ok {
			if serr.StatusCode == 409 || serr.StatusCode == 422 {
				//conflict. skip
				//continue but dont publish.
				//TODO - provide an option to replace existing artifact
				//TODO - ?check exists before attempting upload?
				log.Printf("WARNING - file already exists. Skipping. %v", resp)
				return nil
			} else {
				return err
			}
		} else {
			return err
		}
	}
	if isVerbose {
		log.Printf("File uploaded. Response: %v", resp)
	} else if !isQuiet {
		log.Printf("File uploaded.")
	}
	return err
}
Esempio n. 2
0
File: bintray.go Progetto: yl10/goxc
func walkFunc(fullPath string, fi2 os.FileInfo, err error, reportFilename string, dirs []string, tp TaskParams, format string, report BtReport) error {
	if fi2.IsDir() || fi2.Name() == reportFilename {

		return nil
	}
	subject := tp.Settings.GetTaskSettingString(TASK_BINTRAY, "subject")
	user := tp.Settings.GetTaskSettingString(TASK_BINTRAY, "user")
	if user == "" {
		user = subject
	}
	apikey := tp.Settings.GetTaskSettingString(TASK_BINTRAY, "apikey")
	repository := tp.Settings.GetTaskSettingString(TASK_BINTRAY, "repository")
	pkg := tp.Settings.GetTaskSettingString(TASK_BINTRAY, "package")
	apiHost := tp.Settings.GetTaskSettingString(TASK_BINTRAY, "apihost")
	downloadsHost := tp.Settings.GetTaskSettingString(TASK_BINTRAY, "downloadshost")
	includeResources := tp.Settings.GetTaskSettingString(TASK_BINTRAY, "include")
	excludeResources := tp.Settings.GetTaskSettingString(TASK_BINTRAY, "exclude")
	versionDir := filepath.Join(tp.OutDestRoot, tp.Settings.GetFullVersionName())

	relativePath := strings.Replace(fullPath, versionDir, "", -1)
	relativePath = strings.TrimPrefix(relativePath, "/")
	fmt.Printf("relative path %s, full path %s\n", relativePath, fullPath)

	resourceGlobs := core.ParseCommaGlobs(includeResources)
	//log.Printf("IncludeGlobs: %v", resourceGlobs)
	excludeGlobs := core.ParseCommaGlobs(excludeResources)
	//log.Printf("ExcludeGlobs: %v", excludeGlobs)
	matches := false
	for _, resourceGlob := range resourceGlobs {
		ok, err := filepath.Match(resourceGlob, fi2.Name())
		if err != nil {
			return err
		}
		if ok {
			matches = true
		}
	}
	if matches == false {
		if !tp.Settings.IsQuiet() {
			log.Printf("Not included: %s (pattern %v)", relativePath, includeResources)
		}
		return nil
	}
	for _, excludeGlob := range excludeGlobs {
		ok, err := filepath.Match(excludeGlob, fi2.Name())
		if err != nil {
			return err
		}
		if ok {
			if !tp.Settings.IsQuiet() {
				log.Printf("Excluded: %s (pattern %v)", relativePath, excludeGlob)
			}
			return nil
		}
	}
	first := true

	parent := filepath.Dir(relativePath)
	//platform := strings.Replace(parent, "_", "/", -1)
	//fmt.Fprintf(f, "\n * **%s**:", platform)
	for _, d := range dirs {
		if d == parent {
			first = false
		}
	}
	if first {
		dirs = append(dirs, parent)
	}
	//fmt.Printf("relative path %s, platform %s\n", relativePath, parent)
	text := fi2.Name()
	/*
		text := strings.Replace(fi2.Name(), "_", "\\_", -1)
		if strings.HasSuffix(fi2.Name(), ".zip") {
			text = "zip"
		} else if strings.HasSuffix(fi2.Name(), ".deb") {
			text = "deb"
		} else if strings.HasSuffix(fi2.Name(), ".tar.gz") {
			text = "tar.gz"
		} else if fi2.Name() == tp.AppName || fi2.Name() == tp.AppName+".exe" {
			text = "executable"
		}
	*/
	//PUT /content/:subject/:repo/:package/:version/:path
	url := apiHost + "/content/" + subject + "/" + repository + "/" + pkg + "/" + tp.Settings.GetFullVersionName() + "/" + relativePath
	// for some reason there's no /pkg/ level in the downloads url.
	downloadsUrl := downloadsHost + "/content/" + subject + "/" + repository + "/" + relativePath + "?direct"
	contentType := httpc.GetContentType(text)
	resp, err := httpc.UploadFile("PUT", url, subject, user, apikey, fullPath, relativePath, contentType, !tp.Settings.IsQuiet())
	if err != nil {
		if serr, ok := err.(httpc.HttpError); ok {
			if serr.StatusCode == 409 {
				//conflict. skip
				//continue but dont publish.
				//TODO - provide an option to replace existing artifact
				//TODO - ?check exists before attempting upload?
				log.Printf("WARNING - file already exists. Skipping. %v", resp)
				return nil
			} else {
				return err
			}
		} else {
			return err
		}
	}
	if !tp.Settings.IsQuiet() {
		log.Printf("File uploaded. (expected empty map[]): %v", resp)
	}
	//commaIfRequired := ""
	if first {
		first = false
	} else {
		//commaIfRequired = ","
	}
	if format == "markdown" {
		text = strings.Replace(text, "_", "\\_", -1)
	}
	category := GetCategory(relativePath)
	download := BtDownload{text, downloadsUrl}
	v, ok := report.Categories[category]
	var existing []BtDownload
	if !ok {
		existing = []BtDownload{}
	} else {
		existing = *v
	}
	existing = append(existing, download)
	report.Categories[category] = &existing

	//_, err = fmt.Fprintf(f, "%s [[%s](%s)]", commaIfRequired, text, downloadsUrl)
	if err != nil {
		return err
	}
	err = publish(apiHost, user, apikey, subject, repository, pkg, tp.Settings.GetFullVersionName(), !tp.Settings.IsQuiet())
	return err
}