示例#1
0
// Uploads the file in the specified local path to the specified target path.
// Returns true if the file was successfully uploaded.
func uploadFile(localPath string, targetPath string, flags *utils.Flags, logMsgPrefix string) bool {
	if flags.Props != "" {
		targetPath += ";" + flags.Props
	}

	println(logMsgPrefix + " Uploading artifact: " + targetPath)
	file, err := os.Open(localPath)
	utils.CheckError(err)
	defer file.Close()
	fileInfo, err := file.Stat()
	utils.CheckError(err)

	var checksumDeployed bool = false
	var resp *http.Response
	var details *utils.FileDetails
	if fileInfo.Size() >= 10240 {
		resp, details = tryChecksumDeploy(localPath, targetPath, flags)
		checksumDeployed = !flags.DryRun && (resp.StatusCode == 201 || resp.StatusCode == 200)
	}
	if !flags.DryRun && !checksumDeployed {
		resp = utils.UploadFile(file, targetPath, flags.ArtDetails.User, flags.ArtDetails.Password, details)
	}
	if !flags.DryRun {
		var strChecksumDeployed string
		if checksumDeployed {
			strChecksumDeployed = " (Checksum deploy)"
		} else {
			strChecksumDeployed = ""
		}
		println(logMsgPrefix + " Artifactory response: " + resp.Status + strChecksumDeployed)
	}

	return flags.DryRun || checksumDeployed || resp.StatusCode == 201 || resp.StatusCode == 200
}
示例#2
0
func writeConfFile(details *utils.ArtifactoryDetails) {
	confFilePath := getConFilePath()
	if !utils.IsFileExists(confFilePath) {
		out, err := os.Create(confFilePath)
		utils.CheckError(err)
		defer out.Close()
	}

	b, err := json.Marshal(&details)
	utils.CheckError(err)
	var content bytes.Buffer
	err = json.Indent(&content, b, "", "  ")
	utils.CheckError(err)

	ioutil.WriteFile(confFilePath, []byte(content.String()), 0x777)
}
示例#3
0
func getConFilePath() string {
	userDir, err := user.Current()
	utils.CheckError(err)
	confPath := userDir.HomeDir + "/.jfrog/"
	os.MkdirAll(confPath, 0777)
	return confPath + "art-cli.conf"
}
示例#4
0
func Config(details *utils.ArtifactoryDetails, interactive, shouldEncPassword bool) {
	var bytePassword []byte
	if interactive {
		if details.Url == "" {
			print("Artifactory Url: ")
			fmt.Scanln(&details.Url)
		}
		if details.User == "" {
			print("User: "******"" {
			print("Password: ")
			var err error
			bytePassword, err = terminal.ReadPassword(int(syscall.Stdin))
			details.Password = string(bytePassword)
			utils.CheckError(err)
		}
	}
	details.Url = utils.AddTrailingSlashIfNeeded(details.Url)
	if shouldEncPassword {
		details = encryptPassword(details)
	}
	writeConfFile(details)
}
示例#5
0
func getFilesToUpload(localpath string, targetPath string, flags *utils.Flags) []Artifact {
	if strings.Index(targetPath, "/") < 0 {
		targetPath += "/"
	}
	rootPath := getRootPath(localpath, flags.UseRegExp)
	if !utils.IsPathExists(rootPath) {
		utils.Exit("Path does not exist: " + rootPath)
	}
	localpath = prepareLocalPath(localpath, flags.UseRegExp)

	artifacts := []Artifact{}
	// If the path is a single file then return it
	if !utils.IsDir(rootPath) {
		targetPath := prepareUploadPath(targetPath + rootPath)
		artifacts = append(artifacts, Artifact{rootPath, targetPath})
		return artifacts
	}

	r, err := regexp.Compile(localpath)
	utils.CheckError(err)

	var paths []string
	if flags.Recursive {
		paths = utils.ListFilesRecursive(rootPath)
	} else {
		paths = utils.ListFiles(rootPath)
	}

	for _, path := range paths {
		if utils.IsDir(path) {
			continue
		}

		groups := r.FindStringSubmatch(path)
		size := len(groups)
		target := targetPath
		if size > 0 {
			for i := 1; i < size; i++ {
				group := strings.Replace(groups[i], "\\", "/", -1)
				target = strings.Replace(target, "{"+strconv.Itoa(i)+"}", group, -1)
			}
			if strings.HasSuffix(target, "/") {
				if flags.Flat {
					target += utils.GetFileNameFromPath(path)
				} else {
					uploadPath := prepareUploadPath(path)
					target += uploadPath
				}
			}

			artifacts = append(artifacts, Artifact{path, target})
		}
	}
	return artifacts
}
示例#6
0
func parseAqlSearchResponse(resp []byte) []AqlSearchResultItem {
	var result AqlSearchResult
	err := json.Unmarshal(resp, &result)
	utils.CheckError(err)
	return result.Results
}