Beispiel #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)
}
Beispiel #2
0
// Publish a private template, so others can use it too.
// Copies the whole directory of /templates/private/{host}/{current_template} to /templates/public/{input:public_name}
// Fails if a public template with the chosen name already exists.
func PublishPrivate(db *mgo.Database, opt map[string]interface{}, inp map[string][]string, root, host string) error {
	if scut.TemplateType(opt) == "public" {
		return fmt.Errorf("You can't publish your current template, because it is already public.")
	}
	rule := map[string]interface{}{
		"public_name": map[string]interface{}{
			"must": 1,
			"type": "string",
			"min":  2,
		},
	}
	dat, ex_err := extract.New(rule).Extract(inp)
	if ex_err != nil {
		return ex_err
	}
	public_name := dat["public_name"].(string)
	from := filepath.Join(root, "templates", "private", host, scut.TemplateName(opt))
	to := filepath.Join(root, "templates", "public", public_name)
	// copyrecur.CopyDir checks for existence too, but for safety reasons we check here in case copyrecur semantics change.
	exis, exis_err := Exists(to)
	if exis {
		return fmt.Errorf("Public template with name " + public_name + " already exists.")
	}
	if exis_err != nil {
		return exis_err
	}
	copy_err := copyrecur.CopyDir(from, to)
	if copy_err != nil {
		return fmt.Errorf("There was an error while copying.")
	}
	return nil
}
Beispiel #3
0
func copy_file(c *Command) int {

	source := c.GetPath() + c.GetPathParam("source")
	dest := c.GetPath() + c.GetPathParam("dest")

	fi, err := os.Stat(source)
	if err != nil {
		c.Stderr = err.Error()
		c.status = 1
		return 1
	}

	if fi.IsDir() {
		err := copyrecur.CopyDir(source, dest)
		if err != nil {
			c.Stderr = err.Error()
			c.status = 1
			return 1
		}
		return 0
	} else {
		err := copyrecur.CopyFile(source, dest)
		if err != nil {
			c.Stderr = err.Error()
			c.status = 1
			return 1
		}
		return 0
	}
	return 0
}
Beispiel #4
0
func cp(source, dest string) {
	err := copyrecur.CopyDir(source, dest)
	if err != nil {
		log.Fatal(err)
	} else {
		log.Print("Files copied.")
	}
}
Beispiel #5
0
func main() {
	err = copyrecur.CopyDir("/home/jaybill/data", "/home/jaybill/backup")
	if err != nil {
		log.Fatal(err)
	} else {
		log.Print("Files copied.")
	}
}
Beispiel #6
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)
}