示例#1
0
// Build builds the inventory using the JSON-encoded metadata files from the
// first level subdirectories of the named directory. Returns an error if the
// named directory is not a directory, or if it cannot be read.
func (i *Inventory) Build(name string) error {
	if !util.IsDir(name) {
		return fmt.Errorf("%s: not a directory", name)
	}
	entries, err := ioutil.ReadDir(name)
	if err != nil {
		return err
	}
	for _, entry := range entries {
		mjson := filepath.Join(name, entry.Name(), "metadata.json")
		if !(entry.IsDir() && util.Exists(mjson)) {
			// No metadata file, skip entry.
			// TODO: Add logging.
			continue
		}
		metadata := new(font.Metadata)
		if err := metadata.Read(mjson); err != nil {
			// Invalid metadata file, skip font family.
			// TODO: Add logging.
			continue
		}
		fonts := metadata.Fonts()
		for _, font := range fonts {
			format := font.Format.String()
			weight := strconv.Itoa(font.Weight)
			columnKey := format + weight + font.Style
			i.Put(font.Family, columnKey, font)
		}
	}
	return nil
}
示例#2
0
// Read reads and parses the JSON-encoded contents of the named file and stores
// the result in the whitelist.
// Returns an error if the named file cannot be read or correctly parsed.
func (w *Whitelist) Read(name string) error {
	if util.IsDir(name) {
		return fmt.Errorf("%s: is a directory", name)
	}
	if err := util.ReadJson(name, &w); err != nil {
		return err
	}
	return nil
}