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

Golang fileutils.TempFile函数代码示例

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

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



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

示例1: TestTraceSetToFile

func TestTraceSetToFile(t *testing.T) {
	stdOut := bytes.NewBuffer([]byte{})
	trace.SetStdout(stdOut)

	fileutils.TempFile("trace_test", func(file *os.File, err error) {
		assert.NoError(t, err)
		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)
		assert.NoError(t, err)

		byteString := string(result)
		assert.Contains(t, byteString, "pre-existing content")
		assert.Contains(t, byteString, "hello world")

		result, _ = ioutil.ReadAll(stdOut)
		assert.Equal(t, string(result), "")
	})
}
开发者ID:jalateras,项目名称:cli,代码行数:25,代码来源:trace_test.go


示例2: uploadBits

func (repo CloudControllerApplicationBitsRepository) uploadBits(appGuid string, zipFile *os.File, presentResourcesJson []byte) (apiResponse net.ApiResponse) {
	url := fmt.Sprintf("%s/v2/apps/%s/bits", repo.config.ApiEndpoint(), appGuid)
	fileutils.TempFile("requests", func(requestFile *os.File, err error) {
		if err != nil {
			apiResponse = net.NewApiResponseWithError("Error creating tmp file: %s", err)
			return
		}

		boundary, err := repo.writeUploadBody(zipFile, requestFile, presentResourcesJson)
		if err != nil {
			apiResponse = net.NewApiResponseWithError("Error writing to tmp file: %s", err)
			return
		}

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

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

		response := &Resource{}
		_, apiResponse = repo.gateway.PerformPollingRequestForJSONResponse(request, response, 5*time.Minute)
		if apiResponse.IsNotSuccessful() {
			return
		}
	})

	return
}
开发者ID:rodney-vin,项目名称:cli,代码行数:32,代码来源:application_bits.go


示例3: uploadBits

func (repo CloudControllerBuildpackBitsRepository) uploadBits(buildpack cf.Buildpack, zipFile *os.File) (apiResponse net.ApiResponse) {
	url := fmt.Sprintf("%s/v2/buildpacks/%s/bits", repo.config.Target, buildpack.Guid)

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

		boundary, err := repo.writeUploadBody(zipFile, requestFile)
		if err != nil {
			apiResponse = net.NewApiResponseWithError("Error creating upload", err)
			return
		}

		request, apiResponse := repo.gateway.NewRequest("PUT", url, repo.config.AccessToken, requestFile)
		contentType := fmt.Sprintf("multipart/form-data; boundary=%s", boundary)
		request.HttpReq.Header.Set("Content-Type", contentType)
		if apiResponse.IsNotSuccessful() {
			return
		}

		apiResponse = repo.gateway.PerformRequest(request)
	})

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


示例4: 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:juggernaut,项目名称:cli,代码行数:34,代码来源:buildpack_bits.go


示例5: TestZipWithInvalidFile

func TestZipWithInvalidFile(t *testing.T) {
	fileutils.TempFile("zip_test", func(zipFile *os.File, err error) {
		zipper := ApplicationZipper{}
		err = zipper.Zip("/a/bogus/directory", zipFile)
		assert.Error(t, err)
		assert.Contains(t, err.Error(), "open /a/bogus/directory")
	})
}
开发者ID:jalateras,项目名称:cli,代码行数:8,代码来源:zipper_test.go


示例6: TestZipWithEmptyDir

func TestZipWithEmptyDir(t *testing.T) {
	fileutils.TempFile("zip_test", func(zipFile *os.File, err error) {
		fileutils.TempDir("zip_test", func(emptyDir string, err error) {
			zipper := ApplicationZipper{}
			err = zipper.Zip(emptyDir, zipFile)
			assert.Error(t, err)
			assert.Equal(t, err.Error(), "Directory is empty")
		})
	})
}
开发者ID:jalateras,项目名称:cli,代码行数:10,代码来源:zipper_test.go


示例7: TestZipWithJarFile

func TestZipWithJarFile(t *testing.T) {
	fileutils.TempFile("zip_test", func(zipFile *os.File, err error) {
		dir, err := os.Getwd()
		assert.NoError(t, err)

		zipper := ApplicationZipper{}
		err = zipper.Zip(filepath.Join(dir, "../fixtures/application.jar"), zipFile)
		assert.NoError(t, err)

		assert.Equal(t, fileToString(t, zipFile), "This is an application jar file\n")
	})
}
开发者ID:jalateras,项目名称:cli,代码行数:12,代码来源:zipper_test.go


示例8: TestZipWithDirectory

func TestZipWithDirectory(t *testing.T) {
	fileutils.TempFile("zip_test", func(zipFile *os.File, err error) {

		workingDir, err := os.Getwd()
		assert.NoError(t, err)

		dir := filepath.Join(workingDir, "../fixtures/zip/")
		err = os.Chmod(filepath.Join(dir, "subDir/bar.txt"), 0666)
		assert.NoError(t, err)

		zipper := ApplicationZipper{}
		err = zipper.Zip(dir, zipFile)
		assert.NoError(t, err)

		offset, err := zipFile.Seek(0, os.SEEK_CUR)
		assert.NoError(t, err)
		assert.Equal(t, offset, 0)

		fileStat, err := zipFile.Stat()
		assert.NoError(t, err)

		reader, err := zip.NewReader(zipFile, fileStat.Size())
		assert.NoError(t, err)

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

			assert.NoError(t, err)

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

		assert.Equal(t, len(reader.File), 3)

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

		name, contents = readFileInZip(1)
		assert.Equal(t, name, "subDir/bar.txt")
		assert.Equal(t, contents, "I am in a subdirectory.")
		assert.Equal(t, reader.File[1].FileInfo().Mode(), uint32(0666))

		name, contents = readFileInZip(2)
		assert.Equal(t, name, "subDir/otherDir/file.txt")
		assert.Equal(t, contents, "This file should be present.")
	})
}
开发者ID:nsnt,项目名称:cli,代码行数:51,代码来源:zipper_test.go


示例9: 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("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, err := os.Stat(buildpackLocation)
			if err != nil {
				apiErr = errors.NewWithError("Error opening buildpack file", err)
				return
			}

			if stats.IsDir() {
				err = repo.zipper.Zip(buildpackLocation, zipFileToUpload)
			} else {
				specifiedFile, err := os.Open(buildpackLocation)
				if err != nil {
					apiErr = errors.NewWithError("Couldn't open buildpack file", err)
					return
				}
				err = normalizeBuildpackArchive(specifiedFile, zipFileToUpload)
			}
		}

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

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

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


示例10: UploadApp

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

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

		if err != nil {
			apiResponse = net.NewApiResponseWithMessage("%s", err)
			return
		}

		fileutils.TempFile("uploads", func(zipFile *os.File, err error) {
			if err != nil {
				apiResponse = net.NewApiResponseWithMessage("%s", err.Error())
				return
			}

			err = repo.zipper.Zip(uploadDir, zipFile)
			if err != nil {
				apiResponse = net.NewApiResponseWithError("Error zipping application", err)
				return
			}

			stat, err := zipFile.Stat()
			if err != nil {
				apiResponse = net.NewApiResponseWithError("Error zipping application", err)
				return
			}
			cb(appDir, uint64(stat.Size()), cf.CountFiles(uploadDir))

			apiResponse = repo.uploadBits(appGuid, zipFile, presentResourcesJson)
			if apiResponse.IsNotSuccessful() {
				return
			}
		})
	})
	return
}
开发者ID:rodney-vin,项目名称:cli,代码行数:48,代码来源:application_bits.go


示例11: UploadBuildpack

func (repo CloudControllerBuildpackBitsRepository) UploadBuildpack(buildpack cf.Buildpack, dir string) (apiResponse net.ApiResponse) {
	fileutils.TempFile("buildpack", func(zipFile *os.File, err error) {
		if err != nil {
			apiResponse = net.NewApiResponseWithMessage(err.Error())
			return
		}
		err = repo.zipper.Zip(dir, zipFile)
		if err != nil {
			apiResponse = net.NewApiResponseWithError("Invalid buildpack", err)
			return
		}
		apiResponse = repo.uploadBits(buildpack, zipFile)
		if apiResponse.IsNotSuccessful() {
			return
		}
	})
	return
}
开发者ID:pmuellr,项目名称:cli,代码行数:18,代码来源:buildpack_bits.go


示例12: init

func init() {
	permissionsToSet = 0467
	fileutils.TempFile("permissionedFile", func(file *os.File, err error) {
		if err != nil {
			panic("could not create tmp file")
		}

		fileInfo, err := file.Stat()
		if err != nil {
			panic("could not stat tmp file")
		}

		expectedPermissionBits = fileInfo.Mode()
		if runtime.GOOS != "windows" {
			expectedPermissionBits |= permissionsToSet & 0111
		}
	})
}
开发者ID:normalnorman,项目名称:cli,代码行数:18,代码来源:application_bits_test.go


示例13: UploadApp

func (repo CloudControllerApplicationBitsRepository) UploadApp(app cf.Application, appDir string) (apiResponse net.ApiResponse) {
	fileutils.TempDir("apps", func(uploadDir string, err error) {
		if err != nil {
			apiResponse = net.NewApiResponseWithMessage(err.Error())
			return
		}

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

		if err != nil {
			apiResponse = net.NewApiResponseWithMessage(err.Error())
			return
		}

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

			err = repo.zipper.Zip(uploadDir, zipFile)
			if err != nil {
				apiResponse = net.NewApiResponseWithError("Error zipping application", err)
				return
			}

			apiResponse = repo.uploadBits(app, zipFile, resourcesJson)
			if apiResponse.IsNotSuccessful() {
				return
			}
		})
	})
	return
}
开发者ID:jalateras,项目名称:cli,代码行数:41,代码来源:application_bits.go


示例14: TestTraceSetToInvalidFile

func TestTraceSetToInvalidFile(t *testing.T) {
	stdOut := bytes.NewBuffer([]byte{})
	trace.SetStdout(stdOut)

	fileutils.TempFile("trace_test", func(file *os.File, err error) {
		assert.NoError(t, err)

		file.Chmod(0000)

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

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

		result, _ := ioutil.ReadAll(file)
		assert.Equal(t, string(result), "")

		result, _ = ioutil.ReadAll(stdOut)
		assert.Contains(t, string(result), "hello world")
	})
}
开发者ID:jalateras,项目名称:cli,代码行数:21,代码来源:trace_test.go


示例15: 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:juggernaut,项目名称:cli,代码行数:36,代码来源:buildpack_bits.go


示例16: downloadBuildpack

func 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
		}

		client := &http.Client{
			Transport: &http.Transport{
				TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
				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:normalnorman,项目名称:cli,代码行数:24,代码来源:buildpack_bits.go


示例17:

		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())

			offset, err := zipFile.Seek(0, os.SEEK_CUR)
			Expect(err).NotTo(HaveOccurred())
			Expect(offset).To(Equal(int64(0)))

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

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

			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(len(reader.File)).To(Equal(3))

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

			name, contents = readFileInZip(1)
			Expect(name).To(Equal("subDir/bar.txt"))
			Expect(contents).To(Equal("I am in a subdirectory."))
			Expect(reader.File[1].FileInfo().Mode()).To(Equal(os.FileMode(0666)))

			name, contents = readFileInZip(2)
			Expect(name).To(Equal("subDir/otherDir/file.txt"))
			Expect(contents).To(Equal("This file should be present."))
		})
开发者ID:normalnorman,项目名称:cli,代码行数:48,代码来源:zipper_test.go


示例18:

	})

	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:knolleary,项目名称:cli,代码行数:32,代码来源:trace_test.go



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Golang filter.CurrentUser函数代码示例发布时间:2022-05-24
下一篇:
Golang file2.BlockDevice类代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

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

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

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