예제 #1
0
func postScript(w http.ResponseWriter, r *http.Request, args []string, user interfaces.User) {
	data, err := ioutil.ReadAll(r.Body)
	if err != nil {
		errors.Write(w, errors.BodyNotFound)
		return
	}
	newPath := string(data)
	parts := strings.Split(newPath, ",")
	if len(parts) != 2 {
		errors.Write(w, errors.InvalidBodyFormat)
		return
	}

	var scripts []interfaces.Script
	var success bool
	if args[0] == global {
		scripts = user.GetGlobalScripts()
		success = user.RemoveGlobalScript(args[1])
	} else {
		net := user.GetNetwork(args[0])
		if net == nil {
			errors.Write(w, errors.NetworkNotFound)
			return
		}
		scripts = net.GetScripts()
		success = net.RemoveScript(args[1])
	}

	if !success {
		errors.Write(w, errors.ScriptNotFound)
		return
	}

	var script interfaces.Script
	for _, s := range scripts {
		if s.GetName() == args[1] {
			script = plugin.Script{Name: parts[1], TheScript: s.GetScript()}
			break
		}
	}

	if parts[0] == global {
		user.AddGlobalScript(script)
	} else {
		net := user.GetNetwork(parts[0])
		if net == nil {
			errors.Write(w, errors.NetworkNotFound)
			return
		}
		net.AddScript(script)
	}
}
예제 #2
0
func putScript(w http.ResponseWriter, r *http.Request, args []string, user interfaces.User) {
	data, err := ioutil.ReadAll(r.Body)
	if err != nil {
		errors.Write(w, errors.BodyNotFound)
		return
	}
	script := plugin.Script{Name: args[1], TheScript: string(data)}

	if args[0] == global {
		user.AddGlobalScript(script)
	} else {
		net := user.GetNetwork(args[0])
		if net == nil {
			errors.Write(w, errors.NetworkNotFound)
			return
		}
		net.AddScript(script)
	}
}