func getScripts(w http.ResponseWriter, r *http.Request, args []string, user interfaces.User) { var scripts []interfaces.Script if args[0] == all { scripts = user.GetGlobalScripts() user.GetNetworks().ForEach(func(net interfaces.Network) { scripts = append(scripts, net.GetScripts()...) }) } else if args[0] == global { scripts = user.GetGlobalScripts() } else { net := user.GetNetwork(args[0]) if net == nil { errors.Write(w, errors.NetworkNotFound) return } scripts = net.GetScripts() } data, err := json.Marshal(scripts) if err != nil { errors.Write(w, errors.Internal) return } w.WriteHeader(http.StatusOK) w.Write(data) }
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) } }