• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Golang fileutils.TempFile函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/cloudfoundry/gofileutils/fileutils.TempFile函数的典型用法代码示例。如果您正苦于以下问题:Golang TempFile函数的具体用法?Golang TempFile怎么用?Golang TempFile使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了TempFile函数的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: downloadBuildpack

func (repo CloudControllerBuildpackBitsRepository) downloadBuildpack(url string, cb func(*os.File, error)) {
	fileutils.TempFile("buildpack-download", func(tempfile *os.File, err error) {
		if err != nil {
			cb(nil, err)
			return
		}

		var certPool *x509.CertPool
		if len(repo.TrustedCerts) > 0 {
			certPool = x509.NewCertPool()
			for _, tlsCert := range repo.TrustedCerts {
				cert, _ := x509.ParseCertificate(tlsCert.Certificate[0])
				certPool.AddCert(cert)
			}
		}

		client := &http.Client{
			Transport: &http.Transport{
				TLSClientConfig: &tls.Config{RootCAs: certPool},
				Proxy:           http.ProxyFromEnvironment,
			},
		}

		response, err := client.Get(url)
		if err != nil {
			cb(nil, err)
			return
		}

		io.Copy(tempfile, response.Body)
		tempfile.Seek(0, 0)
		cb(tempfile, nil)
	})
}
开发者ID:nota-ja,项目名称:cli,代码行数:34,代码来源:buildpack_bits.go


示例2: UploadApp

func (repo CloudControllerApplicationBitsRepository) UploadApp(appGuid string, appDir string, cb func(path string, zipSize, fileCount uint64)) (apiErr error) {
	fileutils.TempDir("apps", func(uploadDir string, err error) {
		if err != nil {
			apiErr = err
			return
		}

		var presentFiles []resources.AppFileResource
		repo.sourceDir(appDir, func(sourceDir string, sourceErr error) {
			if sourceErr != nil {
				err = sourceErr
				return
			}
			presentFiles, err = repo.copyUploadableFiles(sourceDir, uploadDir)
		})

		if err != nil {
			apiErr = err
			return
		}

		fileutils.TempFile("uploads", func(zipFile *os.File, err error) {
			if err != nil {
				apiErr = err
				return
			}

			zipFileSize := uint64(0)
			zipFileCount := uint64(0)

			err = repo.zipper.Zip(uploadDir, zipFile)
			switch err := err.(type) {
			case nil:
				stat, err := zipFile.Stat()
				if err != nil {
					apiErr = errors.NewWithError("Error zipping application", err)
					return
				}

				zipFileSize = uint64(stat.Size())
				zipFileCount = app_files.CountFiles(uploadDir)
			case *errors.EmptyDirError:
				zipFile = nil
			default:
				apiErr = errors.NewWithError("Error zipping application", err)
				return
			}

			cb(appDir, zipFileSize, zipFileCount)

			apiErr = repo.uploadBits(appGuid, zipFile, presentFiles)
			if apiErr != nil {
				return
			}
		})
	})
	return
}
开发者ID:julz,项目名称:cli,代码行数:58,代码来源:application_bits.go


示例3: UploadBuildpack

func (repo CloudControllerBuildpackBitsRepository) UploadBuildpack(buildpack models.Buildpack, buildpackLocation string) (apiErr error) {
	fileutils.TempFile("buildpack-upload", func(zipFileToUpload *os.File, err error) {
		if err != nil {
			apiErr = errors.NewWithError(T("Couldn't create temp file for upload"), err)
			return
		}

		var buildpackFileName string
		if isWebURL(buildpackLocation) {
			buildpackFileName = path.Base(buildpackLocation)
			repo.downloadBuildpack(buildpackLocation, func(downloadFile *os.File, downloadErr error) {
				if downloadErr != nil {
					err = downloadErr
					return
				}

				err = normalizeBuildpackArchive(downloadFile, zipFileToUpload)
			})
		} else {
			buildpackFileName = filepath.Base(buildpackLocation)

			stats, statError := os.Stat(buildpackLocation)
			if statError != nil {
				apiErr = errors.NewWithError(T("Error opening buildpack file"), statError)
				err = statError
				return
			}

			if stats.IsDir() {
				buildpackFileName += ".zip" // FIXME: remove once #71167394 is fixed
				err = repo.zipper.Zip(buildpackLocation, zipFileToUpload)
			} else {
				specifiedFile, openError := os.Open(buildpackLocation)
				if openError != nil {
					apiErr = errors.NewWithError(T("Couldn't open buildpack file"), openError)
					err = openError
					return
				}
				err = normalizeBuildpackArchive(specifiedFile, zipFileToUpload)
			}
		}

		if err != nil {
			apiErr = errors.NewWithError(T("Couldn't write zip file"), err)
			return
		}

		apiErr = repo.uploadBits(buildpack, zipFileToUpload, buildpackFileName)
	})

	return
}
开发者ID:tools-alexuser01,项目名称:cli,代码行数:52,代码来源:buildpack_bits.go


示例4: performMultiPartUpload

func (repo CloudControllerBuildpackBitsRepository) performMultiPartUpload(url string, fieldName string, fileName string, body io.Reader) error {
	var capturedErr error

	fileutils.TempFile("requests", func(requestFile *os.File, err error) {
		if err != nil {
			capturedErr = err
			return
		}

		writer := multipart.NewWriter(requestFile)
		part, err := writer.CreateFormFile(fieldName, fileName)

		if err != nil {
			_ = writer.Close()
			capturedErr = err
			return
		}

		_, err = io.Copy(part, body)
		if err != nil {
			capturedErr = fmt.Errorf("%s: %s", T("Error creating upload"), err.Error())
			return
		}

		err = writer.Close()
		if err != nil {
			capturedErr = err
			return
		}

		var request *net.Request
		request, err = repo.gateway.NewRequestForFile("PUT", url, repo.config.AccessToken(), requestFile)
		if err != nil {
			capturedErr = err
			return
		}

		contentType := fmt.Sprintf("multipart/form-data; boundary=%s", writer.Boundary())
		request.HTTPReq.Header.Set("Content-Type", contentType)

		_, err = repo.gateway.PerformRequest(request)
		if err != nil {
			capturedErr = err
		}
	})

	return capturedErr
}
开发者ID:Reejoshi,项目名称:cli,代码行数:48,代码来源:buildpack_bits.go


示例5: UploadBits

func (repo CloudControllerApplicationBitsRepository) UploadBits(appGuid string, zipFile *os.File, presentFiles []resources.AppFileResource) (apiErr error) {
	url := fmt.Sprintf("%s/v2/apps/%s/bits", repo.config.ApiEndpoint(), appGuid)
	fileutils.TempFile("requests", func(requestFile *os.File, err error) {
		if err != nil {
			apiErr = errors.NewWithError(T("Error creating tmp file: {{.Err}}", map[string]interface{}{"Err": err}), err)
			return
		}

		// json.Marshal represents a nil value as "null" instead of an empty slice "[]"
		if presentFiles == nil {
			presentFiles = []resources.AppFileResource{}
		}

		presentFilesJSON, err := json.Marshal(presentFiles)
		if err != nil {
			apiErr = errors.NewWithError(T("Error marshaling JSON"), err)
			return
		}

		boundary, err := repo.writeUploadBody(zipFile, requestFile, presentFilesJSON)
		if err != nil {
			apiErr = errors.NewWithError(T("Error writing to tmp file: {{.Err}}", map[string]interface{}{"Err": err}), err)
			return
		}

		var request *net.Request
		request, apiErr = repo.gateway.NewRequest("PUT", url, repo.config.AccessToken(), requestFile)
		if apiErr != nil {
			return
		}

		contentType := fmt.Sprintf("multipart/form-data; boundary=%s", boundary)
		request.HttpReq.Header.Set("Content-Type", contentType)

		response := &resources.Resource{}
		_, apiErr = repo.gateway.PerformPollingRequestForJSONResponse(request, response, DefaultAppUploadBitsTimeout)
		if apiErr != nil {
			return
		}
	})

	return
}
开发者ID:matanzit,项目名称:cli,代码行数:43,代码来源:application_bits.go


示例6: uploadBits

func (repo CloudControllerApplicationBitsRepository) uploadBits(appGuid string, zipFile *os.File, presentFiles []resources.AppFileResource) (apiErr error) {
	url := fmt.Sprintf("%s/v2/apps/%s/bits", repo.config.ApiEndpoint(), appGuid)
	fileutils.TempFile("requests", func(requestFile *os.File, err error) {
		if err != nil {
			apiErr = errors.NewWithError("Error creating tmp file: %s", err)
			return
		}

		presentFilesJSON, err := json.Marshal(presentFiles)
		if err != nil {
			apiErr = errors.NewWithError("Error marshaling JSON", err)
			return
		}

		boundary, err := repo.writeUploadBody(zipFile, requestFile, presentFilesJSON)
		if err != nil {
			apiErr = errors.NewWithError("Error writing to tmp file: %s", err)
			return
		}

		var request *net.Request
		request, apiErr = repo.gateway.NewRequest("PUT", url, repo.config.AccessToken(), requestFile)
		if apiErr != nil {
			return
		}

		contentType := fmt.Sprintf("multipart/form-data; boundary=%s", boundary)
		request.HttpReq.Header.Set("Content-Type", contentType)

		response := &resources.Resource{}
		_, apiErr = repo.gateway.PerformPollingRequestForJSONResponse(request, response, 5*time.Minute)
		if apiErr != nil {
			return
		}
	})

	return
}
开发者ID:julz,项目名称:cli,代码行数:38,代码来源:application_bits.go


示例7: performMultiPartUpload

func (repo CloudControllerBuildpackBitsRepository) performMultiPartUpload(url string, fieldName string, fileName string, body io.Reader) (apiErr error) {
	fileutils.TempFile("requests", func(requestFile *os.File, err error) {
		if err != nil {
			apiErr = err
			return
		}

		writer := multipart.NewWriter(requestFile)
		part, err := writer.CreateFormFile(fieldName, fileName)

		if err != nil {
			writer.Close()
			return
		}

		_, err = io.Copy(part, body)
		writer.Close()

		if err != nil {
			apiErr = errors.NewWithError("Error creating upload", err)
			return
		}

		var request *net.Request
		request, apiErr = repo.gateway.NewRequest("PUT", url, repo.config.AccessToken(), requestFile)
		contentType := fmt.Sprintf("multipart/form-data; boundary=%s", writer.Boundary())
		request.HttpReq.Header.Set("Content-Type", contentType)
		if apiErr != nil {
			return
		}

		_, apiErr = repo.gateway.PerformRequest(request)
	})

	return
}
开发者ID:nota-ja,项目名称:cli,代码行数:36,代码来源:buildpack_bits.go


示例8:

		fileutils.TempFile("zip_test", func(zipFile *os.File, err error) {
			workingDir, err := os.Getwd()
			Expect(err).NotTo(HaveOccurred())

			dir := filepath.Join(workingDir, "../../fixtures/zip/")
			err = os.Chmod(filepath.Join(dir, "subDir/bar.txt"), 0666)
			Expect(err).NotTo(HaveOccurred())

			zipper := ApplicationZipper{}
			err = zipper.Zip(dir, zipFile)
			Expect(err).NotTo(HaveOccurred())

			fileStat, err := zipFile.Stat()
			Expect(err).NotTo(HaveOccurred())

			reader, err := zip.NewReader(zipFile, fileStat.Size())
			Expect(err).NotTo(HaveOccurred())

			filenames := []string{}
			for _, file := range reader.File {
				filenames = append(filenames, file.Name)
			}

			Expect(filenames).To(Equal(filesInZip))

			readFileInZip := func(index int) (string, string) {
				buf := &bytes.Buffer{}
				file := reader.File[index]
				fReader, err := file.Open()
				_, err = io.Copy(buf, fReader)

				Expect(err).NotTo(HaveOccurred())

				return file.Name, string(buf.Bytes())
			}

			Expect(err).NotTo(HaveOccurred())

			name, contents := readFileInZip(0)
			Expect(name).To(Equal("foo.txt"))
			Expect(contents).To(Equal("This is a simple text file."))

			name, contents = readFileInZip(5)
			Expect(name).To(Equal("subDir/bar.txt"))
			Expect(contents).To(Equal("I am in a subdirectory."))
			Expect(reader.File[5].FileInfo().Mode()).To(Equal(os.FileMode(0666)))
		})
开发者ID:nttlabs,项目名称:cli,代码行数:47,代码来源:zipper_test.go


示例9:

			Expect(deps.curlRepo.Method).To(Equal("GET"))
			Expect(deps.curlRepo.Path).To(Equal("/foo"))
			Expect(deps.ui.Outputs).To(ContainSubstrings([]string{"response for get"}))
			Expect(deps.ui.Outputs).ToNot(ContainSubstrings(
				[]string{"FAILED"},
				[]string{"Content-Size:1024"},
			))
		})

		Context("when the --output flag is provided", func() {
			It("saves the body of the response to the given filepath if it exists", func() {
				fileutils.TempFile("poor-mans-pipe", func(tempFile *os.File, err error) {
					Expect(err).ToNot(HaveOccurred())
					deps.curlRepo.ResponseBody = "hai"

					runCurlWithInputs(deps, []string{"--output", tempFile.Name(), "/foo"})
					contents, err := ioutil.ReadAll(tempFile)
					Expect(err).ToNot(HaveOccurred())
					Expect(string(contents)).To(Equal("hai"))
				})
			})

			It("saves the body of the response to the given filepath if it doesn't exists", func() {
				fileutils.TempDir("poor-mans-dir", func(tmpDir string, err error) {
					Expect(err).ToNot(HaveOccurred())
					deps.curlRepo.ResponseBody = "hai"

					filePath := filepath.Join(tmpDir, "subdir1", "banana.txt")
					runCurlWithInputs(deps, []string{"--output", filePath, "/foo"})

					file, err := os.Open(filePath)
开发者ID:jacopen,项目名称:cli,代码行数:31,代码来源:curl_test.go


示例10:

	})

	It("TestTraceSetToFile", func() {
		stdOut := bytes.NewBuffer([]byte{})
		trace.SetStdout(stdOut)

		fileutils.TempFile("trace_test", func(file *os.File, err error) {
			Expect(err).NotTo(HaveOccurred())
			file.Write([]byte("pre-existing content"))

			os.Setenv(trace.CF_TRACE, file.Name())

			logger := trace.NewLogger()
			logger.Print("hello world")

			file.Seek(0, os.SEEK_SET)
			result, err := ioutil.ReadAll(file)
			Expect(err).NotTo(HaveOccurred())

			byteString := string(result)
			Expect(byteString).To(ContainSubstring("pre-existing content"))
			Expect(byteString).To(ContainSubstring("hello world"))

			result, _ = ioutil.ReadAll(stdOut)
			Expect(string(result)).To(Equal(""))
		})
	})

	It("TestTraceSetToInvalidFile", func() {
		if runtime.GOOS != "windows" {
			stdOut := bytes.NewBuffer([]byte{})
			trace.SetStdout(stdOut)
开发者ID:julz,项目名称:cli,代码行数:32,代码来源:trace_test.go


示例11:

			result, err := ioutil.ReadAll(stdout)
			Expect(err).ToNot(HaveOccurred())
			Expect(result).To(BeEmpty())
		})

		It("prints to a file when given a string", func() {
			fileutils.TempFile("trace_test", func(file *os.File, err error) {
				Expect(err).NotTo(HaveOccurred())
				file.Write([]byte("pre-existing content"))

				logger := NewLogger(file.Name())
				logger.Print("hello world")

				file.Seek(0, os.SEEK_SET)
				result, err := ioutil.ReadAll(file)
				Expect(err).NotTo(HaveOccurred())

				byteString := string(result)
				Expect(byteString).To(ContainSubstring("pre-existing content"))
				Expect(byteString).To(ContainSubstring("hello world"))

				result, _ = ioutil.ReadAll(stdout)
				Expect(string(result)).To(BeEmpty())
			})
		})

		Context("when CF_TRACE is set to a file path that cannot be opened", func() {
			It("defaults to printing to its out pipe", func() {
				if runtime.GOOS != "windows" {
					stdOut := bytes.NewBuffer([]byte{})
					SetStdout(stdOut)
开发者ID:hail100,项目名称:cli,代码行数:31,代码来源:trace_test.go


示例12:

		logger := NewLogger(buffer, true, "", "false")

		logger.Print("Hello World")

		Expect(buffer).To(gbytes.Say("Hello World"))

		_, err := os.Open("false")
		Expect(err).To(HaveOccurred())
	})

	It("returns a logger that writes to STDOUT and a file when verbose is set and CF_TRACE is a path", func() {
		fileutils.TempFile("trace_test", func(file *os.File, err error) {
			logger := NewLogger(buffer, true, file.Name(), "")

			logger.Print("Hello World")

			Expect(buffer).To(gbytes.Say("Hello World"))

			fileContents, _ := ioutil.ReadAll(file)
			Expect(fileContents).To(ContainSubstring("Hello World"))
		})
	})

	It("returns a logger that writes to STDOUT and a file when verbose is set and config.trace is a path", func() {
		fileutils.TempFile("trace_test", func(file *os.File, err error) {
			logger := NewLogger(buffer, true, "", file.Name())

			logger.Print("Hello World")

			Expect(buffer).To(gbytes.Say("Hello World"))

			fileContents, _ := ioutil.ReadAll(file)
开发者ID:Reejoshi,项目名称:cli,代码行数:32,代码来源:logger_provider_test.go


示例13:

	. "github.com/onsi/gomega"
)

var _ = Describe("Flag Content Helpers", func() {
	Describe("GetContentsFromOptionalFlagValue", func() {
		It("returns an empty byte slice when given an empty string", func() {
			bs, err := util.GetContentsFromOptionalFlagValue("")
			Expect(err).NotTo(HaveOccurred())
			Expect(bs).To(Equal([]byte{}))
		})

		It("returns bytes when given a file name prefixed with @", func() {
			fileutils.TempFile("get-data-test", func(tmpFile *os.File, err error) {
				fileData := `{"foo": "bar"}`
				tmpFile.WriteString(fileData)

				bs, err := util.GetContentsFromOptionalFlagValue("@" + tmpFile.Name())
				Expect(err).NotTo(HaveOccurred())
				Expect(bs).To(Equal([]byte(fileData)))
			})
		})

		It("returns bytes when given a file name not prefixed with @", func() {
			fileutils.TempFile("get-data-test", func(tmpFile *os.File, err error) {
				fileData := `{"foo": "bar"}`
				tmpFile.WriteString(fileData)

				bs, err := util.GetContentsFromOptionalFlagValue(tmpFile.Name())
				Expect(err).NotTo(HaveOccurred())
				Expect(bs).To(Equal([]byte(fileData)))
			})
		})
开发者ID:Reejoshi,项目名称:cli,代码行数:32,代码来源:flag_content_helper_test.go



注:本文中的github.com/cloudfoundry/gofileutils/fileutils.TempFile函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang access_log.CreateRunningAccessLogger函数代码示例发布时间:2022-05-23
下一篇:
Golang fileutils.TempDir函数代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap