Beispiel #1
0
// return a 'revel.Template' from Go's template.
func (mustmpl *MustacheTemplate) Render(wr io.Writer, arg interface{}) error {
	if _, err := wr.Write([]byte(mustache.Render(arg.(string)))); err != nil {
		return err
	}

	return nil
}
Beispiel #2
0
func LuaFetchConfig(addr string, config interface{},
	config_field_must_exist bool, pre_defines ...interface{}) (err error) {
	// fetch data
	real_addr := mustache.Render(addr, pre_defines...)
	data, err := LoadData(real_addr)
	if err != nil {
		return
	}

	vm := lua.LuaVM{}
	vm.Start()
	defer vm.Stop()

	// set predeined value
	for i := range pre_defines {
		define := pre_defines[i]
		if !vm.SetObject("_G", define, true) {
			return errors.New(fmt.Sprint("set lua define variable failed for ", i))
		}
	}

	if err = vm.RunBuffer(unsafe.Pointer(&data[0]), uint(len(data))); err != nil {
		return
	}

	if !vm.GetObject("_G", config, !config_field_must_exist) {
		return errors.New("get lua config variable error ")
	}

	return nil
}
Beispiel #3
0
func getPaste(ctx *web.Context, uri string) string {
	c, err := redis.Dial("tcp", "localhost:6379")
	if err != nil {
		Log(err)
		return mustache.Render("Cannot connect to DB \r\n")
	}
	defer c.Close()
	y := "PASTE_DOES_NOT_EXIST"
	if matched, _ := regexp.MatchString("[a-zA-Z0-9]+", uri); matched {
		y = "paste_" + uri
	}
	x, err := redis.String(c.Do("GET", y))
	ctx.SetHeader("Content-Type", "text/plain", true)
	ctx.SetHeader("X-Powered-By", "web.go", true)
	ctx.SetHeader("Connection", "close", true)
	return mustache.Render(x)
}
Beispiel #4
0
func Loadmustache(filename string, args *map[string]string) string {
	file, _err := ioutil.ReadFile("Mst/" + filename)
	if _err != nil {
		fmt.Println(_err)
		return "File not found"
	}
	data := mustache.Render(string(file), args)
	return data
}
Beispiel #5
0
func paste(ctx *web.Context) string {
	c, err := redis.Dial("tcp", "localhost:6379")
	if err != nil {
		Log(err)
		return mustache.Render("Cannot connect to DB \r\n")
	}
	defer c.Close()
	rstr := rands(5)
	if val, ok := ctx.Params["name"]; ok {
		if matched, _ := regexp.MatchString("[a-zA-Z0-9]+", val); matched {
			rstr = val
		}
	}
	y := "paste_" + rstr
	c.Do("SET", y, ctx.Params["paste"])
	sname := "/" //"http://" + ctx.Server.Config.Addr + ":" + string(ctx.Server.Config.Port) + "/"
	return mustache.Render(sname + rstr + "\n")
}
Beispiel #6
0
func compileDirs(fullPath string, f os.FileInfo, err error) error {
	if f.IsDir() {
		newPath := mustache.Render(fullPath, templateConfig.Variables)
		os.Remove(fullPath)
		os.Mkdir(newPath, f.Mode())
		printCreating(newPath)
	}

	return nil
}
Beispiel #7
0
func compileFiles(fullPath string, f os.FileInfo, err error) error {
	if !f.IsDir() {
		newPath := mustache.Render(fullPath, templateConfig.Variables)
		rendered := mustache.RenderFile(fullPath, templateConfig.Variables)

		os.Remove(fullPath)
		ioutil.WriteFile(newPath, []byte(rendered), f.Mode())

		printCreating(newPath)
	}

	return nil
}
Beispiel #8
0
// Report last state of reported streams. Only failures counted.
func ReportLast(vars map[string]string, critical bool) []byte {
	var values []map[string]string
	var page string

	ReportedStreams.RLock()
	defer ReportedStreams.RUnlock()

	if _, exists := vars["group"]; exists { // report for selected group
		for _, value := range ReportedStreams.data[vars["group"]] {
			rprtLastAddRow(&values, value, critical)
		}
		page = mustache.Render(ReportGroupLastTemplate, ReportData{Vars: vars, TableData: values})
	} else { // report for all groups
		for _, group := range ReportedStreams.data {
			for _, value := range group {
				rprtLastAddRow(&values, value, critical)
			}
		}
		page = mustache.Render(ReportLastTemplate, ReportData{TableData: values})
	}

	return []byte(page)
}
Beispiel #9
0
func commands() {
	if len(templateConfig.Commands) != 0 {
		boldWhite.Printf("Template wants to run the following commands with path=%s:\n", fullPath)

		for n, command := range templateConfig.Commands {
			cmdCompiled := mustache.Render(command, templateConfig.Variables)
			templateConfig.Commands[n] = cmdCompiled

			fmt.Printf("%d. ", n+1)
			fmt.Printf("%s", cmdCompiled)
			println()
		}

		println()
		boldWhite.Printf("Do you want to run this commands? [N/y] ")
		input := ""
		fmt.Scanln(&input)

		if input == "y" || input == "Y" {
			for _, cmdTemplate := range templateConfig.Commands {
				color.New(color.FgGreen).Print("* running ")
				fmt.Printf("%s", cmdTemplate)
				println()

				cmdArray := strings.Split(cmdTemplate, " ")
				cmdString := cmdArray[0]
				arguments := cmdArray[1:len(cmdArray)]

				command := exec.Command(cmdString, arguments...)
				command.Dir = fullPath

				out, err := command.Output()
				if err != nil {
					log.Fatal(err)
				} else if string(out) != "" {
					fmt.Printf("> %s", out)
				}
			}
		}
		println()
	}
}
Beispiel #10
0
Datei: clam.go Projekt: miku/clam
// RunOutput runs a command and returns the output filename as string.
func (r Runner) RunOutput(t string, m Map) (string, error) {
	output, ok := m["output"]
	if !ok || output == "" {
		if output == "" {
			f, err := ioutil.TempFile("", "clam-")
			if err != nil {
				return output, err
			}
			m["output"] = f.Name()
		}
	}
	c := mustache.Render(t, m)

	color.Set(color.FgGreen)
	log.Println(c)
	color.Unset()

	cmd := exec.Command(DefaultShell, "-c", c)
	cmd.Stdout = r.Stdout
	cmd.Stderr = r.Stderr

	done := make(chan bool)
	errc := make(chan error)

	if r.Timeout == 0 {
		return m["output"], cmd.Run()
	}

	go func() {
		err := cmd.Run()
		done <- true
		errc <- err
	}()
	select {
	case <-time.After(r.Timeout):
		_ = cmd.Process.Kill()
		return "", Timeout{fmt.Sprintf("timed out: %s", c)}
	case <-done:
		return m["output"], <-errc
	}
}
Beispiel #11
0
func TestRenderingABuildFile(t *testing.T) {
	var buildFile BuildFile

	travisData, err := ioutil.ReadFile("./example.travis.yml")
	if err != nil {
		panic(err)
	}

	yaml.Unmarshal(travisData, &buildFile)

	fmt.Println(fmt.Sprintf("%v", buildFile))

	buildTemplate, err := ioutil.ReadFile("./template-build.sh")
	if err != nil {
		panic(err)
	}

	data := mustache.Render(string(buildTemplate), buildFile)

	println(data)
}
Beispiel #12
0
func (middleware *SelectBackendMiddleware) Handle(h http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

		log.Println("select backend called")

		if middleware.config.Backend != nil {
			var capturedBackend *config.Backend
			for _, backend := range middleware.config.Backend {
				if backend.Pattern != "" {
					backendMatched, _ := regexp.MatchString(backend.Pattern, r.URL.String())
					if backendMatched {
						log.Println("Found backend based on pattern")
						capturedBackend = &backend
						break
					}
				} else if backend.Fn != "" && middleware.config.SelectorFunctions[backend.Fn] != nil {
					log.Println("Found the function")
					rv := context.Get(r, "templateParameters")
					if middleware.config.SelectorFunctions[backend.Fn](r, rv.(map[string]string)) {
						capturedBackend = &backend
						break
					}
				}
			}

			if capturedBackend == nil {
				log.Println("Backend not found") //Log this as a warning
				w.WriteHeader(http.StatusNotFound)
			} else {
				log.Println("Backend selected")
				setBackendDefaults(capturedBackend)
				util.PrintJson(capturedBackend)

				params := context.Get(r, "templateParameters").(map[string]string)

				log.Print("Printing template params...")
				util.PrintJson(params)

				log.Printf("Grabbing target: %v", capturedBackend.Target)
				resp, err := http.Get(capturedBackend.Target)
				if err != nil {
					log.Println("Backend not found")
					w.WriteHeader(http.StatusNotFound)
				}
				defer resp.Body.Close()
				body, err := ioutil.ReadAll(resp.Body)
				if err != nil {
					log.Println("Backend not read")
					w.WriteHeader(http.StatusNotFound)
				} else {

					log.Printf("Backend Target response code: %f", resp.Status)
					renderedTarget := mustache.Render(string(body), params)
					log.Print(renderedTarget)
					context.Set(r, "renderedTarget", renderedTarget)

					// TODO: Caching of Backend
				}
			}
		}

		h.ServeHTTP(w, r)
	})
}
Beispiel #13
0
// render a template, self explanitory
func (self templateEngine) renderTemplate(name string, obj interface{}) string {
	t := self.getTemplate(name)
	return mustache.Render(t, obj)
}
Beispiel #14
0
func Rendermustache(mst string, args *map[string]string) string {
	data := mustache.Render(mst, args)
	return data
}
Beispiel #15
0
func mustache(res http.ResponseWriter, req *http.Request) {
	data := mus.Render("Hello, {{name}}!", map[string]string{"name": "mustache"})
	fmt.Fprintf(res, data)
}
Beispiel #16
0
func main() {
	data := mustache.Render("hello {{c}}", map[string]string{"c": "world"})
	fmt.Printf("data = %s", data)
}
Beispiel #17
0
// The Active state.
func (performer *performer) Active() error {
	config.Log.Info("transitioning to Active")
	if err := performer.replicate(false); err != nil {
		return err
	}

	// do an initial copy of files which might be corrupt because they are not consistant
	// this will be fixed later. we do this now so that a majority of the data will make it across without
	// having to pause the Durablility (ACID compliance) of postgres
	config.Log.Debug("[action] pre-backup started")
	dataDir, err := performer.other.GetDataDir()
	if err != nil {
		return err
	}
	location := performer.other.Location()
	ip, _, err := net.SplitHostPort(location)
	if err != nil {
		return err
	}
	sync := mustache.Render(performer.config.SyncCommand, map[string]string{"local_dir": performer.config.DataDir, "slave_ip": ip, "slave_dir": dataDir})

	if err := performer.sync(sync); err != nil {
		return err
	}

	db, err := performer.pgConnect()
	if err != nil {
		return err
	}
	defer db.Close()

	// this informs postgres to make the files on disk consistant for copying,
	// all changes are kept in memory from this point on
	_, err = db.Exec("select pg_start_backup('replication')")
	if err != nil {
		return err
	}

	config.Log.Debug("[action] backup started")

	if err := performer.sync(sync); err != nil {
		// stop the backup, if it fails, there is nothing we can do so we return the original error
		db.Exec("select pg_stop_backup()")

		// something went wrong, we are the master still, so lets wait for the slave to reconnect
		return nil
	}

	// connect to DB and tell it to stop backup
	if _, err = db.Exec("select pg_stop_backup()"); err != nil {
		return err
	}

	config.Log.Debug("[action] backup complete")

	// if we were unsucessfull at setting the sync flag on the other node
	// then we need to start all over
	if performer.other.SetSynced(true) != nil {
		return nil
	}

	// enable syncronus transaction commits.
	if err := performer.setSync(true, db); err != nil {
		return err
	}

	performer.addVip()
	performer.roleChangeCommand("master")

	performer.me.SetDBRole("active")
	return nil
}
Beispiel #18
0
func printWithMoustache(template string, context map[string]string) {
	message := mustache.Render(template, context)
	fmt.Println(message)
}
Beispiel #19
0
// Errors for 3 last hours for all groups
// TODO number of hours make variable
func Report3Hours(vars map[string]string) []byte {
	var values []map[string]string
	var page string

	ErrHistory.RLock()
	defer ErrHistory.RUnlock()

	now := time.Now()
	curhour := now.Format("06010215")
	//	h2ago := now.Add(-2 * time.Hour).Format("06010215")
	//h3ago := now.Add(-3 * time.Hour).Format("06010215")
	tmptbl := make(map[string]map[string]map[string]string)

	for key, val := range ErrHistory.count {
		errtype := ""
		if key.Curhour == curhour {
			switch key.ErrType {
			case SLOW, VERYSLOW:
				errtype = "sw"
			case BADSTATUS:
				errtype = "bs"
			case BADURI:
				errtype = "bu"
			case RTIMEOUT:
				errtype = "rt"
			case CTIMEOUT:
				errtype = "ct"
			case REFUSED:
				errtype = "cr"
			default:
				continue // count only errors listed above
			}
			if _, exists := tmptbl[key.Group]; !exists {
				tmptbl[key.Group] = make(map[string]map[string]string)
			}
			if _, exists := tmptbl[key.Group][key.Name]; !exists {
				tmptbl[key.Group][key.Name] = make(map[string]string)
			}
			tmptbl[key.Group][key.Name][errtype] = strconv.FormatInt(int64(val), 10)
			tmptbl[key.Group][key.Name]["group"] = key.Group
			tmptbl[key.Group][key.Name]["name"] = key.Name
			tmptbl[key.Group][key.Name]["uri"] = key.URI
			switch { // how much errors per hour?
			case val == 1:
				tmptbl[key.Group][key.Name][fmt.Sprintf("%s-severity", errtype)] = "info"
			case val > 1 && val <= 6:
				tmptbl[key.Group][key.Name][fmt.Sprintf("%s-severity", errtype)] = "warning"
			case val > 6:
				tmptbl[key.Group][key.Name][fmt.Sprintf("%s-severity", errtype)] = "error"
			default:
				tmptbl[key.Group][key.Name][fmt.Sprintf("%s-severity", errtype)] = "success"
			}
		}
	}

	for _, val := range tmptbl {
		for _, counter := range val {
			values = append(values, counter)
		}
	}
	page = mustache.Render(Report3HoursTemplate, ReportData{TableData: values})

	return []byte(page)
}