コード例 #1
0
ファイル: main.go プロジェクト: Chronojam/hugot-handlers
func ivyHandler(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message) error {
	m.Parse()
	out, err := ivy.Eval(strings.Join(m.Args(), " ") + "\n")
	if err != nil {
		return err
	}

	fmt.Fprint(w, out)
	return nil
}
コード例 #2
0
ファイル: main.go プロジェクト: Chronojam/hugot-handlers
func brainFuckHandler(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message) error {
	m.Parse()

	var program = strings.Join(m.Args(), " ") + "\n"
	fmt.Println(program)

	// https://github.com/gfranxman/GFY

	// prepare our vm
	data := make([]uint8, MAX_PROG_LEN)
	data_ptr := 0
	loop_depth := 0
	instruction_ptr := 0

	// execution loop
	for instruction_ptr = 0; instruction_ptr < len(program); instruction_ptr++ {
		switch program[instruction_ptr] {
		case '>':
			// increment the data pointer (to point to the next cell to the right).
			data_ptr++

		case '<':
			// decrement the data pointer (to point to the next cell to the left).
			data_ptr--

		case '+':
			// increment (increase by one) the byte at the data pointer.
			data[data_ptr]++

		case '-':
			// decrement (decrease by one) the byte at the data pointer.
			data[data_ptr]--

		case '.':
			// output the value of the byte at the data pointer.
			print(string(uint8(data[data_ptr])))

		case ',':
			// accept one byte of input, storing its value in the byte at the data pointer.
			var b = make([]uint8, 1)
			_, _ = os.Stdin.Read(b)
			data[data_ptr] = b[0]

		case '[':
			// if the byte at the data pointer is zero, then
			//    instead of moving the instruction pointer forward to the next command,
			//    jump it forward to the command after the matching ] command.
			// * interesting note -- the spec does not mention that the jump instructions should be nestable,
			//    but without this feature my test suite fails.
			if data[data_ptr] == 0 {
				instruction_ptr++
				// allow nested [ ] pairs by looping until we hit the end-jump for our loop depth
				for loop_depth > 0 || program[instruction_ptr] != ']' {
					if program[instruction_ptr] == '[' {
						loop_depth++
					} else if program[instruction_ptr] == ']' {
						loop_depth--
					}
					instruction_ptr++
				}
			}

		case ']':
			// if the byte at the data pointer is nonzero,
			//    then instead of moving the instruction pointer forward to the next command,
			//    jump it back to the command after the matching [ command.
			// * interesting note -- the spec calls for a check that the datapointer is pointing to a non-zero value,
			//    but doing so causes my test suite of bf programs to fail.
			instruction_ptr--
			// allow nested [ ] pairs by looping until we hit the end-jump for our loop depth
			for loop_depth > 0 || program[instruction_ptr] != '[' {
				if program[instruction_ptr] == ']' {
					loop_depth++
				} else if program[instruction_ptr] == '[' {
					loop_depth--
				}
				instruction_ptr--
			}
			instruction_ptr--
		} // end-switch
	}

	fmt.Fprint(w, program)
	return nil
}
コード例 #3
0
ファイル: query.go プロジェクト: Chronojam/hugot-handlers
func (p *promH) graphCmd(ctx context.Context, w hugot.ResponseWriter, m *hugot.Message) error {
	defText, defGraph := false, false
	if !hugot.IsTextOnly(w) {
		defText = true
		defGraph = false
	} else {
		defText = false
		defGraph = true
	}

	_ = m.Bool("t", defText, "Render the graphs as text sparkline.")
	webGraph := m.Bool("g", defGraph, "Render the graphs as a png.")
	dur := m.Duration("d", 15*time.Minute, "how far back to render")

	if err := m.Parse(); err != nil {
		return err
	}

	if len(m.Args()) == 0 {
		return fmt.Errorf("you need to give a query")
	}
	q := strings.Join(m.Args(), " ")
	s := time.Now().Add(-1 * *dur)
	e := time.Now()

	if *webGraph {
		nu := *p.URL()
		nu.Path = nu.Path + "graph/thing.png"
		qs := nu.Query()
		qs.Set("q", q)
		qs.Set("s", fmt.Sprintf("%d", s.UnixNano()))
		qs.Set("e", fmt.Sprintf("%d", e.UnixNano()))
		nu.RawQuery = qs.Encode()

		m := hugot.Message{
			Channel: m.Channel,
			Attachments: []hugot.Attachment{
				{
					Fallback: "fallback",
					Pretext:  "image",
					ImageURL: nu.String(),
				},
			},
		}
		w.Send(ctx, &m)
		return nil
	}

	qapi := prom.NewQueryAPI(*p.client)
	d, err := qapi.QueryRange(ctx, q, prom.Range{s, e, 15 * time.Second})
	if err != nil {
		return err
	}

	switch d.Type() {
	case model.ValScalar:
		m := d.(*model.Scalar)
		glog.Infof("scalar %v", m)
	case model.ValVector:
		m := d.(model.Vector)
		glog.Infof("vector %v", m)
	case model.ValMatrix:
		mx := d.(model.Matrix)
		sort.Sort(mx)
		for _, ss := range mx {
			l := line(ss.Values)
			fmt.Fprintf(w, "%v\n%s", ss.Metric, l)
		}
	case model.ValString:
		m := d.(*model.String)
		glog.Infof("matrix %v", m)
	case model.ValNone:
	}

	return nil
}