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

Golang afero.ReadFile函数代码示例

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

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



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

示例1: AddTemplateFile

func (t *GoHTMLTemplate) AddTemplateFile(name, baseTemplatePath, path string) error {
	t.checkState()
	// get the suffix and switch on that
	ext := filepath.Ext(path)
	switch ext {
	case ".amber":
		templateName := strings.TrimSuffix(name, filepath.Ext(name)) + ".html"
		compiler := amber.New()
		b, err := afero.ReadFile(hugofs.Source(), path)

		if err != nil {
			return err
		}

		// Parse the input data
		if err := compiler.ParseData(b, path); err != nil {
			return err
		}

		if _, err := compiler.CompileWithTemplate(t.New(templateName)); err != nil {
			return err
		}
	case ".ace":
		var innerContent, baseContent []byte
		innerContent, err := afero.ReadFile(hugofs.Source(), path)

		if err != nil {
			return err
		}

		if baseTemplatePath != "" {
			baseContent, err = afero.ReadFile(hugofs.Source(), baseTemplatePath)
			if err != nil {
				return err
			}
		}

		return t.AddAceTemplate(name, baseTemplatePath, path, baseContent, innerContent)
	default:

		if baseTemplatePath != "" {
			return t.AddTemplateFileWithMaster(name, path, baseTemplatePath)
		}

		b, err := afero.ReadFile(hugofs.Source(), path)

		if err != nil {
			return err
		}

		jww.DEBUG.Printf("Add template file from path %s", path)

		return t.AddTemplate(name, string(b))
	}

	return nil

}
开发者ID:digitalcraftsman,项目名称:hugo,代码行数:58,代码来源:template.go


示例2: AddTemplateFileWithMaster

func (t *GoHTMLTemplate) AddTemplateFileWithMaster(name, overlayFilename, masterFilename string) error {

	// There is currently no known way to associate a cloned template with an existing one.
	// This funky master/overlay design will hopefully improve in a future version of Go.
	//
	// Simplicity is hard.
	//
	// Until then we'll have to live with this hackery.
	//
	// See https://github.com/golang/go/issues/14285
	//
	// So, to do minimum amount of changes to get this to work:
	//
	// 1. Lookup or Parse the master
	// 2. Parse and store the overlay in a separate map

	masterTpl := t.Lookup(masterFilename)

	if masterTpl == nil {
		b, err := afero.ReadFile(hugofs.Source(), masterFilename)
		if err != nil {
			return err
		}
		masterTpl, err = t.New(masterFilename).Parse(string(b))

		if err != nil {
			// TODO(bep) Add a method that does this
			t.errors = append(t.errors, &templateErr{name: name, err: err})
			return err
		}
	}

	b, err := afero.ReadFile(hugofs.Source(), overlayFilename)
	if err != nil {
		return err
	}

	overlayTpl, err := template.Must(masterTpl.Clone()).Parse(string(b))
	if err != nil {
		t.errors = append(t.errors, &templateErr{name: name, err: err})
	} else {
		// The extra lookup is a workaround, see
		// * https://github.com/golang/go/issues/16101
		// * https://github.com/spf13/hugo/issues/2549
		t.overlays[name] = overlayTpl.Lookup(overlayTpl.Name())
	}

	return err
}
开发者ID:digitalcraftsman,项目名称:hugo,代码行数:49,代码来源:template.go


示例3: NewContent

// NewContent creates a new content file in the content directory based upon the
// given kind, which is used to lookup an archetype.
func NewContent(fs afero.Fs, kind, name string) (err error) {
	jww.INFO.Println("attempting to create ", name, "of", kind)

	location := FindArchetype(fs, kind)

	var by []byte

	if location != "" {
		by, err = afero.ReadFile(fs, location)
		if err != nil {
			jww.ERROR.Println(err)
		}
	}
	if location == "" || err != nil {
		by = []byte("+++\n title = \"title\"\n draft = true \n+++\n")
	}

	psr, err := parser.ReadFrom(bytes.NewReader(by))
	if err != nil {
		return err
	}

	metadata, err := createMetadata(psr, name)
	if err != nil {
		jww.ERROR.Printf("Error processing archetype file %s: %s\n", location, err)
		return err
	}

	page, err := hugolib.NewPage(name)
	if err != nil {
		return err
	}

	if err = page.SetSourceMetaData(metadata, parser.FormatToLeadRune(viper.GetString("metaDataFormat"))); err != nil {
		return
	}

	page.SetSourceContent(psr.Content())

	if err = page.SafeSaveSourceAs(filepath.Join(viper.GetString("contentDir"), name)); err != nil {
		return
	}
	jww.FEEDBACK.Println(helpers.AbsPathify(filepath.Join(viper.GetString("contentDir"), name)), "created")

	editor := viper.GetString("newContentEditor")

	if editor != "" {
		jww.FEEDBACK.Printf("Editing %s with %q ...\n", name, editor)

		cmd := exec.Command(editor, helpers.AbsPathify(path.Join(viper.GetString("contentDir"), name)))
		cmd.Stdin = os.Stdin
		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr

		return cmd.Run()
	}

	return nil
}
开发者ID:digitalcraftsman,项目名称:hugo,代码行数:61,代码来源:content.go


示例4: resGetLocal

// resGetLocal loads the content of a local file
func resGetLocal(url string, fs afero.Fs) ([]byte, error) {
	filename := filepath.Join(viper.GetString("workingDir"), url)
	if e, err := helpers.Exists(filename, fs); !e {
		return nil, err
	}

	return afero.ReadFile(fs, filename)

}
开发者ID:digitalcraftsman,项目名称:hugo,代码行数:10,代码来源:template_resources.go


示例5: ReadFile

func (env *TestEnv) ReadFile(path string) string {
	data, err := afero.ReadFile(env.fs, path)

	if err != nil {
		panic(err)
	}

	return string(data)
}
开发者ID:miros,项目名称:init-exporter,代码行数:9,代码来源:test_env.go


示例6: MergeInConfig

func (v *Viper) MergeInConfig() error {
	jww.INFO.Println("Attempting to merge in config file")
	if !stringInSlice(v.getConfigType(), SupportedExts) {
		return UnsupportedConfigError(v.getConfigType())
	}

	file, err := afero.ReadFile(v.fs, v.getConfigFile())
	if err != nil {
		return err
	}

	return v.MergeConfig(bytes.NewReader(file))
}
开发者ID:jbeda,项目名称:kubernetes,代码行数:13,代码来源:viper.go


示例7: TestPostDownload

func TestPostDownload(t *testing.T) {
	a := &server{
		key:         testKey,
		fs:          &afero.MemMapFs{},
		downloadDir: "/magopie/downloads",
	}

	hash := "337b6dbb824ff8acf38846d4698746df7bf2b5c9"
	file := strings.ToUpper(hash) + ".torrent"
	fullFile := filepath.Join(a.downloadDir, file)
	content := "torrents!"

	var torMethod, torPath string
	torcacheSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		torMethod = r.Method
		torPath = r.URL.Path
		content := strings.NewReader(content)
		http.ServeContent(w, r, file, time.Time{}, content)
	}))
	defer torcacheSrv.Close()
	a.torcacheURL = torcacheSrv.URL

	// Make the request to magopie to download a particular torrent by hash
	req := mustNewRequest(t, "POST", "/download/"+hash, nil)
	res := httptest.NewRecorder()

	router(a).ServeHTTP(res, req)

	if res.Code != http.StatusCreated {
		t.Errorf("%s : status = %d, expected %d", describeReq(req), res.Code, http.StatusCreated)
	}

	// Ensure things about the post to the torache server
	if torMethod != "GET" {
		t.Errorf("torcache server method = %q, expected %q", torMethod, "GET")
	}
	expectedPath := "/torrent/337B6DBB824FF8ACF38846D4698746DF7BF2B5C9.torrent"
	if torPath != expectedPath {
		t.Errorf("torcache server path = %q, expected %q", torPath, expectedPath)
	}

	// Ensure we downloaded the file from the torcache server
	actualContent, err := afero.ReadFile(a.fs, fullFile)
	if err != nil {
		t.Errorf("err reading file in test: %v", err)
	}

	if string(actualContent) != content {
		t.Errorf("downloaded file contents %s\nexpected %s", actualContent, content)
	}
}
开发者ID:devict,项目名称:magopie,代码行数:51,代码来源:server_test.go


示例8: ReadInConfig

func (v *Viper) ReadInConfig() error {
	jww.INFO.Println("Attempting to read in config file")
	if !stringInSlice(v.getConfigType(), SupportedExts) {
		return UnsupportedConfigError(v.getConfigType())
	}

	file, err := afero.ReadFile(v.fs, v.getConfigFile())
	if err != nil {
		return err
	}

	v.config = make(map[string]interface{})

	return v.unmarshalReader(bytes.NewReader(file), v.config)
}
开发者ID:jbeda,项目名称:kubernetes,代码行数:15,代码来源:viper.go


示例9: Read

// Read reads up to len(p) bytes from the LazyFileReader's file and copies them
// into p. It returns the number of bytes read and any error encountered. If
// the file is once read, it returns its contents from cache, doesn't re-read
// the file.
func (l *LazyFileReader) Read(p []byte) (n int, err error) {
	if l.contents == nil {
		b, err := afero.ReadFile(l.fs, l.filename)
		if err != nil {
			return 0, fmt.Errorf("failed to read content from %s: %s", l.filename, err.Error())
		}
		l.contents = bytes.NewReader(b)
	}
	l.contents.Seek(l.pos, 0)
	if err != nil {
		return 0, errors.New("failed to set read position: " + err.Error())
	}
	n, err = l.contents.Read(p)
	l.pos += int64(n)
	return n, err
}
开发者ID:CowPanda,项目名称:hugo,代码行数:20,代码来源:lazy_file_reader.go


示例10: resGetCache

// resGetCache returns the content for an ID from the file cache or an error
// if the file is not found returns nil,nil
func resGetCache(id string, fs afero.Fs, ignoreCache bool) ([]byte, error) {
	if ignoreCache {
		return nil, nil
	}
	fID := getCacheFileID(id)
	isExists, err := helpers.Exists(fID, fs)
	if err != nil {
		return nil, err
	}
	if !isExists {
		return nil, nil
	}

	return afero.ReadFile(fs, fID)

}
开发者ID:digitalcraftsman,项目名称:hugo,代码行数:18,代码来源:template_resources.go


示例11: TestWriteTo

func TestWriteTo(t *testing.T) {
	fs := afero.NewOsFs()
	filename := "lazy_file_reader_test.go"
	fi, err := fs.Stat(filename)
	if err != nil {
		t.Fatalf("os.Stat: %v", err)
	}

	b, err := afero.ReadFile(fs, filename)
	if err != nil {
		t.Fatalf("afero.ReadFile: %v", err)
	}

	rd, err := NewLazyFileReader(fs, filename)
	if err != nil {
		t.Fatalf("NewLazyFileReader %s: %v", filename, err)
	}

	tst := func(testcase string, expectedSize int64, checkEqual bool) {
		buf := bytes.NewBuffer(make([]byte, 0, bytes.MinRead))
		n, err := rd.WriteTo(buf)
		if err != nil {
			t.Fatalf("WriteTo %s case: %v", testcase, err)
		}
		if n != expectedSize {
			t.Errorf("WriteTo %s case: written bytes length expected %d, got %d", testcase, expectedSize, n)
		}
		if checkEqual && !bytes.Equal(b, buf.Bytes()) {
			t.Errorf("WriteTo %s case: written bytes are different from expected", testcase)
		}
	}
	tst("No cache", fi.Size(), true)
	tst("No cache 2nd", 0, false)

	p := make([]byte, fi.Size())
	_, err = rd.Read(p)
	if err != nil && err != io.EOF {
		t.Fatalf("Read: %v", err)
	}
	_, err = rd.Seek(0, 0)
	if err != nil {
		t.Fatalf("Seek: %v", err)
	}

	tst("Cache", fi.Size(), true)
}
开发者ID:CowPanda,项目名称:hugo,代码行数:46,代码来源:lazy_file_reader_test.go


示例12: readFileFromFs

func readFileFromFs(t *testing.T, fs afero.Fs, filename string) string {
	filename = filepath.FromSlash(filename)
	b, err := afero.ReadFile(fs, filename)
	if err != nil {
		// Print some debug info
		root := strings.Split(filename, helpers.FilePathSeparator)[0]
		afero.Walk(fs, root, func(path string, info os.FileInfo, err error) error {
			if info != nil && !info.IsDir() {
				fmt.Println("    ", path)
			}

			return nil
		})
		t.Fatalf("Failed to read file: %s", err)
	}
	return string(b)
}
开发者ID:digitalcraftsman,项目名称:hugo,代码行数:17,代码来源:hugo_sites_test.go


示例13: Test_filesystemCredentialStore_SaveCredentials_CredentialsAreValid_JSONIsWrittenToFile

func Test_filesystemCredentialStore_SaveCredentials_CredentialsAreValid_JSONIsWrittenToFile(t *testing.T) {

	// arrange
	fs := afero.NewMemMapFs()
	credentialFilePath := "/home/user/.dee/credentials.json"
	credentialStore := filesystemCredentialStore{fs, credentialFilePath}

	// act
	credentialStore.SaveCredentials(deens.APICredentials{"[email protected]", "123456"})

	// assert
	expectedResult := `{"Email":"[email protected]","Token":"123456"}`
	content, _ := afero.ReadFile(fs, credentialFilePath)
	if string(content) != expectedResult {
		t.Fail()
		t.Logf("SaveCredentials should have written the credentials as JSON to %q. Expected: %q, Actual: %q", credentialFilePath, expectedResult, string(content))
	}
}
开发者ID:andreaskoch,项目名称:dee-cli,代码行数:18,代码来源:credentials_test.go


示例14: readFile

// readFile reads the file named by filename relative to the given basepath
// and returns the contents as a string.
// There is a upper size limit set at 1 megabytes.
func readFile(fs *afero.BasePathFs, filename string) (string, error) {
	if filename == "" {
		return "", errors.New("readFile needs a filename")
	}

	if info, err := fs.Stat(filename); err == nil {
		if info.Size() > 1000000 {
			return "", fmt.Errorf("File %q is too big", filename)
		}
	} else {
		return "", err
	}
	b, err := afero.ReadFile(fs, filename)

	if err != nil {
		return "", err
	}

	return string(b), nil
}
开发者ID:camunda-third-party,项目名称:hugo,代码行数:23,代码来源:template_funcs.go


示例15: isThemeVsHugoVersionMismatch

// isThemeVsHugoVersionMismatch returns whether the current Hugo version is
// less than the theme's min_version.
func isThemeVsHugoVersionMismatch() (mismatch bool, requiredMinVersion string) {
	if !helpers.ThemeSet() {
		return
	}

	themeDir := helpers.GetThemeDir()

	fs := hugofs.Source()
	path := filepath.Join(themeDir, "theme.toml")

	exists, err := helpers.Exists(path, fs)

	if err != nil || !exists {
		return
	}

	b, err := afero.ReadFile(fs, path)

	c, err := parser.HandleTOMLMetaData(b)

	if err != nil {
		return
	}

	config := c.(map[string]interface{})

	if minVersion, ok := config["min_version"]; ok {
		switch minVersion.(type) {
		case float32:
			return helpers.HugoVersionNumber < minVersion.(float32), fmt.Sprint(minVersion)
		case float64:
			return helpers.HugoVersionNumber < minVersion.(float64), fmt.Sprint(minVersion)
		default:
			return
		}

	}

	return
}
开发者ID:vincentsys,项目名称:hugo,代码行数:42,代码来源:hugo.go


示例16: TestRead

func TestRead(t *testing.T) {
	fs := afero.NewOsFs()
	filename := "lazy_file_reader_test.go"
	fi, err := fs.Stat(filename)
	if err != nil {
		t.Fatalf("os.Stat: %v", err)
	}

	b, err := afero.ReadFile(fs, filename)
	if err != nil {
		t.Fatalf("afero.ReadFile: %v", err)
	}

	rd, err := NewLazyFileReader(fs, filename)
	if err != nil {
		t.Fatalf("NewLazyFileReader %s: %v", filename, err)
	}

	tst := func(testcase string) {
		p := make([]byte, fi.Size())
		n, err := rd.Read(p)
		if err != nil {
			t.Fatalf("Read %s case: %v", testcase, err)
		}
		if int64(n) != fi.Size() {
			t.Errorf("Read %s case: read bytes length expected %d, got %d", testcase, fi.Size(), n)
		}
		if !bytes.Equal(b, p) {
			t.Errorf("Read %s case: read bytes are different from expected", testcase)
		}
	}
	tst("No cache")
	_, err = rd.Seek(0, 0)
	if err != nil {
		t.Fatalf("Seek: %v", err)
	}
	tst("Cache")
}
开发者ID:CowPanda,项目名称:hugo,代码行数:38,代码来源:lazy_file_reader_test.go


示例17: Test_filesystemCredentialStore_SaveCredentials_FileExists_FileIsOverridden

func Test_filesystemCredentialStore_SaveCredentials_FileExists_FileIsOverridden(t *testing.T) {

	// arrange
	fs := afero.NewMemMapFs()
	credentialFilePath := "/home/user/.dee/credentials.json"

	// create the initial file
	f1, _ := fs.Create(credentialFilePath)
	f1.WriteString(`{"Email":"[email protected]","Token":"543"}`)
	f1.Close()

	credentialStore := filesystemCredentialStore{fs, credentialFilePath}

	// act
	credentialStore.SaveCredentials(deens.APICredentials{"[email protected]", "123456"})

	// assert
	expectedResult := `{"Email":"[email protected]","Token":"123456"}`
	content, _ := afero.ReadFile(fs, credentialFilePath)
	if string(content) != expectedResult {
		t.Fail()
		t.Logf("SaveCredentials should have written the credentials as JSON to %q. Expected: %q, Actual: %q", credentialFilePath, expectedResult, string(content))
	}
}
开发者ID:andreaskoch,项目名称:dee-cli,代码行数:24,代码来源:credentials_test.go


示例18: TestSeek

func TestSeek(t *testing.T) {
	type testcase struct {
		seek     int
		offset   int64
		length   int
		moveto   int64
		expected []byte
	}
	fs := afero.NewOsFs()
	filename := "lazy_file_reader_test.go"
	b, err := afero.ReadFile(fs, filename)
	if err != nil {
		t.Fatalf("afero.ReadFile: %v", err)
	}

	// no cache case
	for i, this := range []testcase{
		{seek: os.SEEK_SET, offset: 0, length: 10, moveto: 0, expected: b[:10]},
		{seek: os.SEEK_SET, offset: 5, length: 10, moveto: 5, expected: b[5:15]},
		{seek: os.SEEK_CUR, offset: 5, length: 10, moveto: 5, expected: b[5:15]}, // current pos = 0
		{seek: os.SEEK_END, offset: -1, length: 1, moveto: int64(len(b) - 1), expected: b[len(b)-1:]},
		{seek: 3, expected: nil},
		{seek: os.SEEK_SET, offset: -1, expected: nil},
	} {
		rd, err := NewLazyFileReader(fs, filename)
		if err != nil {
			t.Errorf("[%d] NewLazyFileReader %s: %v", i, filename, err)
			continue
		}

		pos, err := rd.Seek(this.offset, this.seek)
		if this.expected == nil {
			if err == nil {
				t.Errorf("[%d] Seek didn't return an expected error", i)
			}
		} else {
			if err != nil {
				t.Errorf("[%d] Seek failed unexpectedly: %v", i, err)
				continue
			}
			if pos != this.moveto {
				t.Errorf("[%d] Seek failed to move the pointer: got %d, expected: %d", i, pos, this.moveto)
			}

			buf := make([]byte, this.length)
			n, err := rd.Read(buf)
			if err != nil {
				t.Errorf("[%d] Read failed unexpectedly: %v", i, err)
			}
			if !bytes.Equal(this.expected, buf[:n]) {
				t.Errorf("[%d] Seek and Read got %q but expected %q", i, buf[:n], this.expected)
			}
		}
	}

	// cache case
	rd, err := NewLazyFileReader(fs, filename)
	if err != nil {
		t.Fatalf("NewLazyFileReader %s: %v", filename, err)
	}
	dummy := make([]byte, len(b))
	_, err = rd.Read(dummy)
	if err != nil {
		t.Fatalf("Read failed unexpectedly: %v", err)
	}

	for i, this := range []testcase{
		{seek: os.SEEK_SET, offset: 0, length: 10, moveto: 0, expected: b[:10]},
		{seek: os.SEEK_SET, offset: 5, length: 10, moveto: 5, expected: b[5:15]},
		{seek: os.SEEK_CUR, offset: 1, length: 10, moveto: 16, expected: b[16:26]}, // current pos = 15
		{seek: os.SEEK_END, offset: -1, length: 1, moveto: int64(len(b) - 1), expected: b[len(b)-1:]},
		{seek: 3, expected: nil},
		{seek: os.SEEK_SET, offset: -1, expected: nil},
	} {
		pos, err := rd.Seek(this.offset, this.seek)
		if this.expected == nil {
			if err == nil {
				t.Errorf("[%d] Seek didn't return an expected error", i)
			}
		} else {
			if err != nil {
				t.Errorf("[%d] Seek failed unexpectedly: %v", i, err)
				continue
			}
			if pos != this.moveto {
				t.Errorf("[%d] Seek failed to move the pointer: got %d, expected: %d", i, pos, this.moveto)
			}

			buf := make([]byte, this.length)
			n, err := rd.Read(buf)
			if err != nil {
				t.Errorf("[%d] Read failed unexpectedly: %v", i, err)
			}
			if !bytes.Equal(this.expected, buf[:n]) {
				t.Errorf("[%d] Seek and Read got %q but expected %q", i, buf[:n], this.expected)
			}
		}
	}
}
开发者ID:CowPanda,项目名称:hugo,代码行数:99,代码来源:lazy_file_reader_test.go



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Golang afero.File类代码示例发布时间:2022-05-28
下一篇:
Golang afero.NewMemMapFs函数代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

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

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

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