Ejemplo n.º 1
0
// Fork current private template into an other private one.
// Copies the whole directory from /templates/private/{host}/{current_template} to /templates/private/{host}/{inp:new_private_name}
func ForkPrivate(db *mgo.Database, opt map[string]interface{}, inp map[string][]string, root, host string) error {
	if scut.TemplateType(opt) != "private" {
		return fmt.Errorf("Your current template is not a private one.") // Kinda unsensical error message but ok...
	}
	rule := map[string]interface{}{
		"new_template_name": "must",
	}
	dat, e_err := extract.New(rule).Extract(inp)
	if e_err != nil {
		return e_err
	}
	new_template_name := dat["new_template_name"].(string)
	to := filepath.Join(root, "templates", "private", host, new_template_name)
	e, e_err := Exists(to)
	if e_err != nil {
		return fmt.Errorf("Can't determine if private template exists.")
	}
	if e {
		return fmt.Errorf("Private template named %v already exists.", new_template_name)
	}
	from := filepath.Join(root, "templates", "private", host, scut.TemplateName(opt))
	copy_err := copyrecur.CopyDir(from, to)
	if copy_err != nil {
		return fmt.Errorf("There was an error while copying.")
	}
	id := basic.CreateOptCopy(db)
	q := m{"_id": id}
	upd := m{
		"$set": m{
			"Template": new_template_name,
		},
	}
	return db.C("options").Update(q, upd)
}
Ejemplo n.º 2
0
func saveDefaultOpt(site_db *mgo.Database, def_opts map[string]interface{}) error {
	if def_opts == nil {
		return fmt.Errorf("Nothing to save.")
	}
	id := basic.CreateOptCopy(site_db)
	return site_db.C("options").Update(m{"_id": id}, m{"$set": def_opts})
}
Ejemplo n.º 3
0
// opt structure:
// Modules.modulename
func InstallB(db *mgo.Database, ev ifaces.Event, opt map[string]interface{}, modn, mode string) (bson.ObjectId, error) {
	var object_id bson.ObjectId
	if _, already := jsonp.Get(opt, "Modules."+modn); mode == "install" && already {
		return object_id, fmt.Errorf("Module " + modn + " is already installed.")
	} else if mode == "uninstall" && !already {
		return object_id, fmt.Errorf("Module " + modn + " is not installed.")
	}
	object_id = basic.CreateOptCopy(db)
	return object_id, nil
}
Ejemplo n.º 4
0
func SaveConfig(db *mgo.Database, ev ifaces.Event, encoded_conf string) error {
	var v interface{}
	json.Unmarshal([]byte(encoded_conf), &v)
	if v != nil {
		m := v.(map[string]interface{})
		// Just in case
		delete(m, "_id")
		object_id := basic.CreateOptCopy(db)
		m["created"] = time.Now().Unix()
		db.C("options").Update(bson.M{"_id": object_id}, m)
	} else {
		return fmt.Errorf("Invalid json.")
	}
	return nil
}
Ejemplo n.º 5
0
// Forks a public template into a private one: creates a deep recursive copy of the whole directory tree, so the user
// can edit his own template files as he wishes.
func ForkPublic(db *mgo.Database, opt map[string]interface{}, root, host string) error {
	if CanModifyTemplate(opt) {
		return fmt.Errorf("Template is already private.")
	}
	from := filepath.Join(root, scut.GetTPath(opt, host))
	to := filepath.Join(root, "templates", "private", host, scut.TemplateName(opt))
	copy_err := copyrecur.CopyDir(from, to)
	if copy_err != nil { // && copy_err.Error() != "Destination already exists"
		return copy_err
	}
	id := basic.CreateOptCopy(db)
	q := m{"_id": id}
	upd := m{
		"$set": m{
			"TplIsPrivate": true,
		},
	}
	return db.C("options").Update(q, upd)
}
Ejemplo n.º 6
0
// Does the database operation involved in template switch.
func switchToTemplateDb(db *mgo.Database, template_type, template_name string) error {
	id := basic.CreateOptCopy(db)
	q := m{"_id": id}
	var upd m
	if template_type == "private" {
		upd = m{
			"$set": m{
				"Template":     template_name,
				"TplIsPrivate": true,
			},
		}
	} else {
		upd = m{
			"$set": m{
				"Template": template_name,
			},
			"$unset": m{
				"TplIsPrivate": 1,
			},
		}
	}
	return db.C("options").Update(q, upd)
}