Пример #1
0
// ReadRecipe reads the named recipe file. If the name is not specified, it
// searchs the recipe file in specified directory.
// If failed to read recipe, it returns an error.
func ReadRecipe(name string, dir string) (*Recipe, error) {
	var r io.Reader
	var err error
	if name != "" {
		name, r, err = openFile(name)
	} else {
		name, r, err = findFile(dir)
	}
	if err != nil {
		return nil, err
	}

	logger.Debugf("------ reading recipe file: %s", name)
	data, err := ioutil.ReadAll(r)
	if err != nil {
		logger.Fatalf("can not read the file: %s", err)
	}

	switch {
	case strings.HasSuffix(name, ".json"):
		r, err := ParseJSON(data)
		if err != nil {
			if e, ok := err.(*json.SyntaxError); ok {
				line, pos := getCaretPos(data, int(e.Offset))
				return nil, fmt.Errorf("can not parse the JSON file: %s near line %d, pos %d", err, line, pos)
			}
			return nil, fmt.Errorf("can not parse the JSON file: %s", err)
		}
		return r, nil
	}

	return nil, fmt.Errorf("unsupported file format")
}
Пример #2
0
func (c *logging) initLogging() {
	if c.Quiet {
		logger.RemoveOutput(logger.StdoutOutput)
	}
	if c.TextLogFile != "" {
		file, err := os.OpenFile(c.TextLogFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
		if err != nil {
			logger.Fatalf("can not open the log file, %s", err)
		}
		logger.AddOutput(logger.TextOutput(file))
	}
	if c.JSONLogFile != "" {
		file, err := os.OpenFile(c.JSONLogFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
		if err != nil {
			logger.Fatalf("can not open the JSON log file, %s", err)
		}
		logger.AddOutput(logger.JSONOutput(file))
	}
}