Beispiel #1
0
func (t *MetaObject) setFileMode() error {
	if t.Mode == "" {
		if !utils.IsFileExist(t.Dest) {
			t.FileMode = 0644
		} else {
			fi, err := os.Stat(t.Dest)
			if err != nil {
				return err
			}
			t.FileMode = fi.Mode()
		}
	} else {
		mode, err := strconv.ParseUint(t.Mode, 0, 32)
		if err != nil {
			return err
		}
		t.FileMode = os.FileMode(mode)
	}
	return nil
}
Beispiel #2
0
func (t *MetaObject) createConfigFile(data map[string]interface{}) error {
	log.Debug("Using template " + t.Tmpl)

	if !utils.IsFileExist(t.Tmpl) {
		return errors.New("Missing meta file: " + t.Tmpl)
	}

	log.Debug("Compiling source template " + t.Tmpl)
	tmpl, err := template.New(path.Base(t.Tmpl)).Funcs(t.funcMap).ParseFiles(t.Tmpl)
	if err != nil {
		return fmt.Errorf("Unable to process template file %s, %s", t.Tmpl, err)
	}

	// 创建临时文件
	temp, err := ioutil.TempFile(filepath.Dir(t.Dest), "."+filepath.Base(t.Dest))
	defer os.Remove(temp.Name())
	defer temp.Close()

	log.Debug("Temp path: ", temp.Name())

	if err != nil {
		return err
	}

	if err = tmpl.Execute(temp, nil); err != nil {
		temp.Close()
		os.Remove(temp.Name())
		return err
	}
	os.Chmod(temp.Name(), t.FileMode)

	log.Debug("Overwriting target config: ", t.Dest)
	err = os.Rename(temp.Name(), t.Dest)
	if err != nil {
		log.Fatal("Rename ", temp.Name(), " to ", t.Dest, " failed")
		return err
	}

	log.Info("Target config has been updated: ", t.Dest)
	return nil
}