Пример #1
1
// Load loads the configuration from the filesystem.
func (config *Config) Load(filePath string) error {
	var errOutput error

	_, err := utils.JSONLoad(filePath, &config)
	if err != nil {
		return err
	}

	// Load provider definitions
	config.Providers = make(map[string]*ProviderConfig)

	walkFunc := func(filePath string, fileInfo os.FileInfo, err error) error {
		if fileInfo.IsDir() || !strings.HasSuffix(filePath, ".json") {
			return nil
		}

		_, providerName := path.Split(strings.TrimSuffix(filePath, ".json"))

		config.Lock()
		defer config.Unlock()

		config.Providers[providerName] = &ProviderConfig{}

		if fileInfo, err = utils.JSONLoad(filePath, config.Providers[providerName]); err != nil {
			err = fmt.Errorf("in %s, %s", filePath, err)
			if errOutput == nil {
				errOutput = err
			}

			return err
		}

		return nil
	}

	if err := utils.WalkDir(config.ProvidersDir, walkFunc); err != nil {
		return fmt.Errorf("unable to load provider definitions: %s", err)
	}

	if errOutput != nil {
		return errOutput
	}

	return nil
}
Пример #2
0
// LoadItem loads an item by its identifier.
func (library *Library) LoadItem(id string, itemType int) error {
	// Load item from file
	switch itemType {
	case LibraryItemSourceGroup, LibraryItemMetricGroup:
		tmpGroup := &Group{}

		filePath := library.getFilePath(id, itemType)

		fileInfo, err := utils.JSONLoad(filePath, &tmpGroup)
		if err != nil {
			return fmt.Errorf("in %s, %s", filePath, err)
		}

		library.Groups[id] = tmpGroup
		library.Groups[id].Type = itemType
		library.Groups[id].Modified = fileInfo.ModTime()

	case LibraryItemScale:
		tmpScale := &Scale{}

		filePath := library.getFilePath(id, itemType)

		fileInfo, err := utils.JSONLoad(filePath, &tmpScale)
		if err != nil {
			return fmt.Errorf("in %s, %s", filePath, err)
		}

		library.Scales[id] = tmpScale
		library.Scales[id].Modified = fileInfo.ModTime()

	case LibraryItemUnit:
		tmpUnit := &Unit{}

		filePath := library.getFilePath(id, itemType)

		fileInfo, err := utils.JSONLoad(filePath, &tmpUnit)
		if err != nil {
			return fmt.Errorf("in %s, %s", filePath, err)
		}

		library.Units[id] = tmpUnit
		library.Units[id].Modified = fileInfo.ModTime()

	case LibraryItemGraph:
		tmpGraph := &Graph{}

		filePath := library.getFilePath(id, itemType)

		fileInfo, err := utils.JSONLoad(filePath, &tmpGraph)
		if err != nil {
			return fmt.Errorf("in %s, %s", filePath, err)
		}

		library.Graphs[id] = tmpGraph
		library.Graphs[id].Modified = fileInfo.ModTime()

	case LibraryItemCollection:
		var tmpCollection *struct {
			*Collection
			Parent string `json:"parent"`
		}

		filePath := library.getFilePath(id, LibraryItemCollection)

		fileInfo, err := utils.JSONLoad(filePath, &tmpCollection)
		if err != nil {
			return fmt.Errorf("in %s, %s", filePath, err)
		}

		if !library.ItemExists(id, LibraryItemCollection) {
			library.Collections[id] = &Collection{}
		}

		*library.Collections[id] = *tmpCollection.Collection

		if tmpCollection.Parent != "" {
			library.Collections[id].ParentID = tmpCollection.Parent
		}

		library.Collections[id].Modified = fileInfo.ModTime()
	}

	return nil
}