Пример #1
0
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

}
Пример #2
0
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
}
Пример #3
0
// 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
}
Пример #4
0
// 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)

}
Пример #5
0
func (env *TestEnv) ReadFile(path string) string {
	data, err := afero.ReadFile(env.fs, path)

	if err != nil {
		panic(err)
	}

	return string(data)
}
Пример #6
0
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))
}
Пример #7
0
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)
	}
}
Пример #8
0
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)
}
Пример #9
0
// 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
}
Пример #10
0
// 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)

}
Пример #11
0
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)
}
Пример #12
0
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)
}
Пример #13
0
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{"*****@*****.**", "123456"})

	// assert
	expectedResult := `{"Email":"*****@*****.**","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))
	}
}
Пример #14
0
// 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
}
Пример #15
0
// 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
}
Пример #16
0
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")
}
Пример #17
0
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":"*****@*****.**","Token":"543"}`)
	f1.Close()

	credentialStore := filesystemCredentialStore{fs, credentialFilePath}

	// act
	credentialStore.SaveCredentials(deens.APICredentials{"*****@*****.**", "123456"})

	// assert
	expectedResult := `{"Email":"*****@*****.**","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))
	}
}
Пример #18
0
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)
			}
		}
	}
}