Ejemplo n.º 1
0
func fileState(r *Resource) (state.State, error) {
	if r.Path == "" {
		return nil, fmt.Errorf(`parameter "path" is required`)
	}
	if r.Src == "" {
		if strings.HasPrefix(r.Path, "/") {
			wd, err := os.Getwd()
			if err != nil {
				return nil, err
			}
			r.Src = path.Join(wd, "files", r.Path[1:])
		}
		logger.Debugf(`parameter "src" is not specified, assume as "%s"`, r.Src)
	}
	if r.Backup != "" {
		if !strings.ContainsRune(r.Backup, '/') {
			r.Backup = path.Join(path.Dir(r.Path), r.Backup)
		}
	}
	return &file.Copy{
		Name:   r.Path,
		Src:    r.Src,
		Backup: r.Backup,
	}, nil
}
Ejemplo n.º 2
0
func (c *test) run(args []string) int {
	f := c.flags(args)
	c.initLogging()
	logger.Infof("Started at %s", time.Now().Format("2006-01-02T15:04:05-07:00"))

	wd, err := os.Getwd()
	if err != nil {
		logger.Fatal(err)
	}
	name := f.Arg(0)
	rec, err := recipe.ReadRecipe(name, wd)
	if err != nil {
		logger.Fatal(err)
	}

	exit := 0
	for _, res := range rec.Resources {
		logger.Debugf("------ testing %s", res)
		if err := resource.Test(res); err != nil {
			exit = 1
			logger.Errorf("[FAIL] %s", err)
			continue
		}
		logger.Infof("[ OK ] %s", res)
	}

	return exit
}
Ejemplo n.º 3
0
func (r *Resource) States() ([]state.State, error) {
	states := []state.State{}

	if r.State == "" {
		r.State = "file"
		logger.Debugf(`parameter "state" is not specified, assume as "%s"`, r.State)
	}
	if stateFuncMap[r.State] != nil {
		s, err := stateFuncMap[r.State](r)
		if err != nil {
			return nil, err
		}
		states = append(states, s)
	}

	if r.Mode != "" {
		s := &file.Mode{
			Name: r.Path,
			Mode: r.Mode,
		}
		states = append(states, s)
	}

	return states, nil
}
Ejemplo n.º 4
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")
}
Ejemplo n.º 5
0
func Test(r Resource) error {
	states, err := r.States()
	if err != nil {
		return err
	}
	for _, state := range states {
		logger.Debugf("testing state: %s", state)
		if err := state.Test(); err != nil {
			return err
		}
	}
	return nil
}
Ejemplo n.º 6
0
func Apply(r Resource) error {
	states, err := r.States()
	if err != nil {
		return err
	}
	for _, state := range states {
		logger.Debugf("applying state: %s", state)
		if err := state.Apply(); err != nil {
			return err
		}
	}
	return nil
}
Ejemplo n.º 7
0
func (r *Resource) States() ([]state.State, error) {
	states := []state.State{}

	if r.State == "" {
		r.State = "installed"
		logger.Debugf(`parameter "state" is not specified, assume as "%s"`, r.State)
	}

	var s state.State
	var err error
	switch r.State {
	case "installed":
		s, err = r.installedState()
	case "removed":
		s, err = r.removedState()
	}
	if err != nil {
		return nil, err
	}
	states = append(states, s)

	return states, nil
}