<commit_before>package build

import (
	"bufio"
	"bytes"
	"fmt"
	"github.com/jingweno/gotask/tasking"
	"go/ast"
	"go/build"
	goparser "go/parser"
	"go/token"
	"io"
	"os"
	"path/filepath"
	"regexp"
	"strings"
	"unicode"
	"unicode/utf8"
)

func NewParser() *parser {
	return &parser{}
}

type parser struct{}

func (l *parser) Parse(dir string) (taskSet *tasking.TaskSet, err error) {
	dir, err = expandPath(dir)
	if err != nil {
		return
	}

	importPath, err := findImportPath(os.Getenv("GOPATH"), dir)
	if err != nil {
		return
	}

	p, e := build.Import(importPath, dir, 0)
	taskFiles := append(p.GoFiles, p.IgnoredGoFiles...)
	taskFiles = append(taskFiles, p.CgoFiles...)
	if e != nil {
		// task files may be ignored for build
		if _, ok := e.(*build.NoGoError); !ok || len(taskFiles) == 0 {
			err = e
			return
		}
	}

	tasks, err := loadTasks(dir, taskFiles)
	if err != nil {
		return
	}

	name := p.Name
	if name == "" {
		name = filepath.Base(p.Dir)
	}

	taskSet = &tasking.TaskSet{Name: name, Dir: p.Dir, PkgObj: p.PkgObj, ImportPath: p.ImportPath, Tasks: tasks}

	return
}

func expandPath(path string) (expanded string, err error) {
	expanded, err = filepath.Abs(path)
	if err != nil {
		return
	}

	if !isFileExist(expanded) {
		err = fmt.Errorf("Path %s does not exist", expanded)
		return
	}

	return
}

func findImportPath(gp, dir string) (importPath string, err error) {
	gopaths := strings.Split(gp, ":")
	if len(gopaths) == 0 {
		err = fmt.Errorf("No environment variable GOPATH found")
		return
	}

	for _, gopath := range gopaths {
		gopath, e := expandPath(gopath)
		if e != nil {
			continue
		}

		srcPath := filepath.Join(gopath, "src")
		if !strings.HasPrefix(dir, srcPath) {
			continue
		}

		importPath, e = filepath.Rel(srcPath, dir)
		if e == nil && importPath != "" {
			break
		}
	}

	if importPath == "" {
		err = fmt.Errorf("Can't find import path in %s", dir)
	}

	return
}

func loadTasks(dir string, files []string) (tasks []tasking.Task, err error) {
	taskFiles := filterTaskFiles(files)
	for _, taskFile := range taskFiles {
		ts, e := parseTasks(filepath.Join(dir, taskFile))
		if e != nil {
			err = e
			return
		}

		tasks = append(tasks, ts...)
	}

	return
}

func filterTaskFiles(files []string) (taskFiles []string) {
	for _, f := range files {
		if isTaskFile(f, "_task.go") {
			taskFiles = append(taskFiles, f)
		}
	}

	return
}

func parseTasks(filename string) (tasks []tasking.Task, err error) {
	taskFileSet := token.NewFileSet()
	f, err := goparser.ParseFile(taskFileSet, filename, nil, goparser.ParseComments)
	if err != nil {
		return
	}

	for _, d := range f.Decls {
		n, ok := d.(*ast.FuncDecl)
		if !ok {
			continue
		}

		if n.Recv != nil {
			continue
		}

		actionName := n.Name.String()
		if isTask(actionName, "Task") {
			usage, desc, e := parseUsageAndDesc(n.Doc.Text())
			if e != nil {
				continue
			}

			name := convertActionNameToTaskName(actionName)
			t := tasking.Task{Name: name, ActionName: actionName, Usage: usage, Description: desc}
			tasks = append(tasks, t)
		}
	}

	return
}

func isTaskFile(name, suffix string) bool {
	if strings.HasSuffix(name, suffix) {
		return true
	}

	return false
}

func isTask(name, prefix string) bool {
	if !strings.HasPrefix(name, prefix) {
		return false
	}
	if len(name) == len(prefix) { // "Task" is ok
		return true
	}

	rune, _ := utf8.DecodeRuneInString(name[len(prefix):])
	return !unicode.IsLower(rune)
}

func convertActionNameToTaskName(s string) string {
	n := strings.TrimPrefix(s, "Task")
	return dasherize(n)
}

func parseUsageAndDesc(doc string) (usage, desc string, err error) {
	reader := bufio.NewReader(bytes.NewReader([]byte(doc)))
	r := regexp.MustCompile("\\S")
	var usageParts, descParts []string

	line, err := readLine(reader)
	for err == nil {
		if len(descParts) == 0 && r.MatchString(line) {
			usageParts = append(usageParts, line)
		} else {
			descParts = append(descParts, line)
		}

		line, err = readLine(reader)
	}

	if err == io.EOF {
		err = nil
	}

	usage = strings.Join(usageParts, " ")
	usage = strings.TrimSpace(usage)

	desc = strings.Join(descParts, "\n")
	desc = strings.TrimSpace(desc)

	return
}

func readLine(r *bufio.Reader) (string, error) {
	var (
		isPrefix = true
		err      error
		line, ln []byte
	)

	for isPrefix && err == nil {
		line, isPrefix, err = r.ReadLine()
		ln = append(ln, line...)
	}

	return string(ln), err
}
<commit_msg>Find the right import paths on Windows<commit_after>package build

import (
	"bufio"
	"bytes"
	"fmt"
	"github.com/jingweno/gotask/tasking"
	"go/ast"
	"go/build"
	goparser "go/parser"
	"go/token"
	"io"
	"os"
	"path/filepath"
	"regexp"
	"runtime"
	"strings"
	"unicode"
	"unicode/utf8"
)

func NewParser() *parser {
	return &parser{}
}

type parser struct{}

func (l *parser) Parse(dir string) (taskSet *tasking.TaskSet, err error) {
	dir, err = expandPath(dir)
	if err != nil {
		return
	}

	importPath, err := findImportPath(os.Getenv("GOPATH"), dir)
	if err != nil {
		return
	}

	p, e := build.Import(importPath, dir, 0)
	taskFiles := append(p.GoFiles, p.IgnoredGoFiles...)
	taskFiles = append(taskFiles, p.CgoFiles...)
	if e != nil {
		// task files may be ignored for build
		if _, ok := e.(*build.NoGoError); !ok || len(taskFiles) == 0 {
			err = e
			return
		}
	}

	tasks, err := loadTasks(dir, taskFiles)
	if err != nil {
		return
	}

	name := p.Name
	if name == "" {
		name = filepath.Base(p.Dir)
	}

	taskSet = &tasking.TaskSet{Name: name, Dir: p.Dir, PkgObj: p.PkgObj, ImportPath: p.ImportPath, Tasks: tasks}

	return
}

func expandPath(path string) (expanded string, err error) {
	expanded, err = filepath.Abs(path)
	if err != nil {
		return
	}

	if !isFileExist(expanded) {
		err = fmt.Errorf("Path %s does not exist", expanded)
		return
	}

	return
}

func findImportPath(gp, dir string) (importPath string, err error) {
	var gopaths []string
  // GOPATHs are separated by ; on Windows
	if runtime.GOOS == "windows" {
		gopaths = strings.Split(gp, ";")
	} else {
		gopaths = strings.Split(gp, ":")
	}

	if len(gopaths) == 0 {
		err = fmt.Errorf("Environment variable GOPATH is not found")
		return
	}

	for _, gopath := range gopaths {
		gopath, e := expandPath(gopath)
		if e != nil {
			continue
		}

		srcPath := filepath.Join(gopath, "src")
		if !strings.HasPrefix(dir, srcPath) {
			continue
		}

		importPath, e = filepath.Rel(srcPath, dir)
		if e == nil && importPath != "" {
			break
		}
	}

	if importPath == "" {
		err = fmt.Errorf("Can't find import path in %s", dir)
	}

	return
}

func loadTasks(dir string, files []string) (tasks []tasking.Task, err error) {
	taskFiles := filterTaskFiles(files)
	for _, taskFile := range taskFiles {
		ts, e := parseTasks(filepath.Join(dir, taskFile))
		if e != nil {
			err = e
			return
		}

		tasks = append(tasks, ts...)
	}

	return
}

func filterTaskFiles(files []string) (taskFiles []string) {
	for _, f := range files {
		if isTaskFile(f, "_task.go") {
			taskFiles = append(taskFiles, f)
		}
	}

	return
}

func parseTasks(filename string) (tasks []tasking.Task, err error) {
	taskFileSet := token.NewFileSet()
	f, err := goparser.ParseFile(taskFileSet, filename, nil, goparser.ParseComments)
	if err != nil {
		return
	}

	for _, d := range f.Decls {
		n, ok := d.(*ast.FuncDecl)
		if !ok {
			continue
		}

		if n.Recv != nil {
			continue
		}

		actionName := n.Name.String()
		if isTask(actionName, "Task") {
			usage, desc, e := parseUsageAndDesc(n.Doc.Text())
			if e != nil {
				continue
			}

			name := convertActionNameToTaskName(actionName)
			t := tasking.Task{Name: name, ActionName: actionName, Usage: usage, Description: desc}
			tasks = append(tasks, t)
		}
	}

	return
}

func isTaskFile(name, suffix string) bool {
	if strings.HasSuffix(name, suffix) {
		return true
	}

	return false
}

func isTask(name, prefix string) bool {
	if !strings.HasPrefix(name, prefix) {
		return false
	}
	if len(name) == len(prefix) { // "Task" is ok
		return true
	}

	rune, _ := utf8.DecodeRuneInString(name[len(prefix):])
	return !unicode.IsLower(rune)
}

func convertActionNameToTaskName(s string) string {
	n := strings.TrimPrefix(s, "Task")
	return dasherize(n)
}

func parseUsageAndDesc(doc string) (usage, desc string, err error) {
	reader := bufio.NewReader(bytes.NewReader([]byte(doc)))
	r := regexp.MustCompile("\\S")
	var usageParts, descParts []string

	line, err := readLine(reader)
	for err == nil {
		if len(descParts) == 0 && r.MatchString(line) {
			usageParts = append(usageParts, line)
		} else {
			descParts = append(descParts, line)
		}

		line, err = readLine(reader)
	}

	if err == io.EOF {
		err = nil
	}

	usage = strings.Join(usageParts, " ")
	usage = strings.TrimSpace(usage)

	desc = strings.Join(descParts, "\n")
	desc = strings.TrimSpace(desc)

	return
}

func readLine(r *bufio.Reader) (string, error) {
	var (
		isPrefix = true
		err      error
		line, ln []byte
	)

	for isPrefix && err == nil {
		line, isPrefix, err = r.ReadLine()
		ln = append(ln, line...)
	}

	return string(ln), err
}
<|endoftext|>


<commit_before>package domain

import (
	"cf"
	"cf/api"
	"cf/configuration"
	"cf/requirements"
	"cf/terminal"
	"errors"
	"github.com/codegangsta/cli"
)

type CreateDomain struct {
	ui         terminal.UI
	config     *configuration.Configuration
	domainRepo api.DomainRepository
	orgReq     requirements.OrganizationRequirement
}

func NewCreateDomain(ui terminal.UI, config *configuration.Configuration, domainRepo api.DomainRepository) (cmd *CreateDomain) {
	cmd = new(CreateDomain)
	cmd.ui = ui
	cmd.config = config
	cmd.domainRepo = domainRepo
	return
}

func (cmd *CreateDomain) GetRequirements(reqFactory requirements.Factory, c *cli.Context) (reqs []requirements.Requirement, err error) {
	if len(c.Args()) != 2 {
		err = errors.New("Incorrect Usage")
		cmd.ui.FailWithUsage(c, "create-domain")
		return
	}

	cmd.orgReq = reqFactory.NewOrganizationRequirement(c.Args()[0])
	reqs = []requirements.Requirement{
		reqFactory.NewLoginRequirement(),
		cmd.orgReq,
	}
	return
}

func (cmd *CreateDomain) Run(c *cli.Context) {
	domainName := c.Args()[1]
	owningOrg := cmd.orgReq.GetOrganization()

	cmd.ui.Say("Creating domain %s for org %s as %s...",
		terminal.EntityNameColor(domainName),
		terminal.EntityNameColor(owningOrg.Name),
		terminal.EntityNameColor(cmd.config.Username()),
	)

	_, apiResponse := cmd.domainRepo.Create(domainName, owningOrg.Guid)
	if apiResponse.IsNotSuccessful() {
		cmd.ui.Failed(apiResponse.Message)
		return
	}

	cmd.ui.Ok()
	cmd.ui.Say("TIP: Use '%s map-domain' to assign it to a space", cf.Name())
}
<commit_msg>Remove tip after creaeting a domain [finishes #61777636]<commit_after>package domain

import (
	"cf/api"
	"cf/configuration"
	"cf/requirements"
	"cf/terminal"
	"errors"
	"github.com/codegangsta/cli"
)

type CreateDomain struct {
	ui         terminal.UI
	config     *configuration.Configuration
	domainRepo api.DomainRepository
	orgReq     requirements.OrganizationRequirement
}

func NewCreateDomain(ui terminal.UI, config *configuration.Configuration, domainRepo api.DomainRepository) (cmd *CreateDomain) {
	cmd = new(CreateDomain)
	cmd.ui = ui
	cmd.config = config
	cmd.domainRepo = domainRepo
	return
}

func (cmd *CreateDomain) GetRequirements(reqFactory requirements.Factory, c *cli.Context) (reqs []requirements.Requirement, err error) {
	if len(c.Args()) != 2 {
		err = errors.New("Incorrect Usage")
		cmd.ui.FailWithUsage(c, "create-domain")
		return
	}

	cmd.orgReq = reqFactory.NewOrganizationRequirement(c.Args()[0])
	reqs = []requirements.Requirement{
		reqFactory.NewLoginRequirement(),
		cmd.orgReq,
	}
	return
}

func (cmd *CreateDomain) Run(c *cli.Context) {
	domainName := c.Args()[1]
	owningOrg := cmd.orgReq.GetOrganization()

	cmd.ui.Say("Creating domain %s for org %s as %s...",
		terminal.EntityNameColor(domainName),
		terminal.EntityNameColor(owningOrg.Name),
		terminal.EntityNameColor(cmd.config.Username()),
	)

	_, apiResponse := cmd.domainRepo.Create(domainName, owningOrg.Guid)
	if apiResponse.IsNotSuccessful() {
		cmd.ui.Failed(apiResponse.Message)
		return
	}

	cmd.ui.Ok()
}
<|endoftext|>


<commit_before>package flow

import (
	"reflect"
)

// map can work with multiple kinds of inputs and outputs
// Input Types:
//   1. single value
//   2. (key, value) : Most common format for key value pair
//   3. (key, values) : GroupByKey() outputs
//   4. (key, values1, values2) : CoGroup() outputs
//   5. (key, value1, value2) : Join() outputs
// Output Types:
//   1. return single value
//   2. return (key, value)
//   3. return no value
//   4. return no value, but last parameter is a output channel
func (d *Dataset) Map(f interface{}) *Dataset {
	outType := guessFunctionOutputType(f)
	ret, step := add1ShardTo1Step(d, outType)
	step.Name = "Map"
	step.Function = func(task *Task) {
		fn := reflect.ValueOf(f)
		ft := reflect.TypeOf(f)

		var invokeMapFunc func(input reflect.Value)

		var outChan reflect.Value
		if ft.In(ft.NumIn()-1).Kind() == reflect.Chan || ft.NumOut() > 0 {
			outChan = task.Outputs[0].WriteChan
		}

		if ft.In(ft.NumIn()-1).Kind() == reflect.Chan {
			// if last parameter in the function is a channel
			// use the channel element type as output type
			invokeMapFunc = func(input reflect.Value) {
				switch input.Type() {
				case KeyValueType:
					kv := input.Interface().(KeyValue)
					outs := _functionCallWithChanOutput(fn, outChan, kv.Key, kv.Value)
					sendMapOutputs(outChan, outs)
				case KeyValueValueType:
					kv := input.Interface().(KeyValueValue)
					outs := _functionCall(fn, outChan, kv.Key, kv.Value1, kv.Value2)
					sendMapOutputs(outChan, outs)
				case KeyValuesType:
					kvs := input.Interface().(KeyValues)
					outs := _functionCall(fn, outChan, kvs.Key, kvs.Values)
					sendMapOutputs(outChan, outs)
				case KeyValuesValuesType:
					kvv := input.Interface().(KeyValuesValues)
					outs := _functionCall(fn, outChan, kvv.Key, kvv.Values1, kvv.Values2)
					sendMapOutputs(outChan, outs)
				default:
					outs := fn.Call([]reflect.Value{input, outChan})
					sendMapOutputs(outChan, outs)
				}
			}
		} else {
			invokeMapFunc = func(input reflect.Value) {
				switch input.Type() {
				case KeyValueType:
					kv := input.Interface().(KeyValue)
					outs := _functionCall(fn, kv.Key, kv.Value)
					sendMapOutputs(outChan, outs)
				case KeyValueValueType:
					kv := input.Interface().(KeyValueValue)
					outs := _functionCall(fn, kv.Key, kv.Value1, kv.Value2)
					sendMapOutputs(outChan, outs)
				case KeyValuesType:
					kvs := input.Interface().(KeyValues)
					outs := _functionCall(fn, kvs.Key, kvs.Values)
					sendMapOutputs(outChan, outs)
				case KeyValuesValuesType:
					kvv := input.Interface().(KeyValuesValues)
					outs := _functionCall(fn, kvv.Key, kvv.Values1, kvv.Values2)
					sendMapOutputs(outChan, outs)
				default:
					outs := fn.Call([]reflect.Value{input})
					sendMapOutputs(outChan, outs)
				}
			}
		}

		for input := range task.InputChan() {
			invokeMapFunc(input)
		}
		// println("exiting d:", d.Id, "step:", step.Id, "task:", task.Id)
	}
	return ret
}

func _functionCallWithChanOutput(fn reflect.Value, outChan reflect.Value, inputs ...interface{}) []reflect.Value {
	var args []reflect.Value
	for _, input := range inputs {
		args = append(args, reflect.ValueOf(input))
	}
	args = append(args, outChan)
	return fn.Call(args)
}

func _functionCall(fn reflect.Value, inputs ...interface{}) []reflect.Value {
	var args []reflect.Value
	for _, input := range inputs {
		args = append(args, reflect.ValueOf(input))
	}
	return fn.Call(args)
}

// f(A)bool
func (d *Dataset) Filter(f interface{}) *Dataset {
	ret, step := add1ShardTo1Step(d, d.Type)
	step.Name = "Filter"
	step.Function = func(task *Task) {
		fn := reflect.ValueOf(f)
		outChan := task.Outputs[0].WriteChan
		for input := range task.InputChan() {
			outs := fn.Call([]reflect.Value{input})
			if outs[0].Bool() {
				outChan.Send(input)
			}
		}
	}
	return ret
}

func add1ShardTo1Step(d *Dataset, nextDataType reflect.Type) (ret *Dataset, step *Step) {
	ret = d.context.newNextDataset(len(d.Shards), nextDataType)
	step = d.context.AddOneToOneStep(d, ret)
	return
}

// the value over the outChan is always reflect.Value
// but the inner values are always actual interface{} object
func sendMapOutputs(outChan reflect.Value, values []reflect.Value) {
	if !outChan.IsValid() {
		return
	}
	if len(values) == 2 {
		outChan.Send(reflect.ValueOf(KeyValue{values[0].Interface(), values[1].Interface()}))
		return
	}
	if len(values) == 1 {
		outChan.Send(values[0])
		return
	}
}
<commit_msg>Filter() also support multiple key value type combination<commit_after>package flow

import (
	"reflect"
)

// map can work with multiple kinds of inputs and outputs
// Input Types:
//   1. single value
//   2. (key, value) : Most common format for key value pair
//   3. (key, values) : GroupByKey() outputs
//   4. (key, values1, values2) : CoGroup() outputs
//   5. (key, value1, value2) : Join() outputs
// Output Types:
//   1. return single value
//   2. return (key, value)
//   3. return no value
//   4. return no value, but last parameter is a output channel
func (d *Dataset) Map(f interface{}) *Dataset {
	outType := guessFunctionOutputType(f)
	ret, step := add1ShardTo1Step(d, outType)
	step.Name = "Map"
	step.Function = func(task *Task) {
		fn := reflect.ValueOf(f)
		ft := reflect.TypeOf(f)

		var invokeMapFunc func(input reflect.Value)

		var outChan reflect.Value
		if ft.In(ft.NumIn()-1).Kind() == reflect.Chan || ft.NumOut() > 0 {
			outChan = task.Outputs[0].WriteChan
		}

		if ft.In(ft.NumIn()-1).Kind() == reflect.Chan {
			// if last parameter in the function is a channel
			// use the channel element type as output type
			invokeMapFunc = func(input reflect.Value) {
				switch input.Type() {
				case KeyValueType:
					kv := input.Interface().(KeyValue)
					outs := _functionCallWithChanOutput(fn, outChan, kv.Key, kv.Value)
					sendMapOutputs(outChan, outs)
				case KeyValueValueType:
					kv := input.Interface().(KeyValueValue)
					outs := _functionCall(fn, outChan, kv.Key, kv.Value1, kv.Value2)
					sendMapOutputs(outChan, outs)
				case KeyValuesType:
					kvs := input.Interface().(KeyValues)
					outs := _functionCall(fn, outChan, kvs.Key, kvs.Values)
					sendMapOutputs(outChan, outs)
				case KeyValuesValuesType:
					kvv := input.Interface().(KeyValuesValues)
					outs := _functionCall(fn, outChan, kvv.Key, kvv.Values1, kvv.Values2)
					sendMapOutputs(outChan, outs)
				default:
					outs := fn.Call([]reflect.Value{input, outChan})
					sendMapOutputs(outChan, outs)
				}
			}
		} else {
			invokeMapFunc = func(input reflect.Value) {
				switch input.Type() {
				case KeyValueType:
					kv := input.Interface().(KeyValue)
					outs := _functionCall(fn, kv.Key, kv.Value)
					sendMapOutputs(outChan, outs)
				case KeyValueValueType:
					kv := input.Interface().(KeyValueValue)
					outs := _functionCall(fn, kv.Key, kv.Value1, kv.Value2)
					sendMapOutputs(outChan, outs)
				case KeyValuesType:
					kvs := input.Interface().(KeyValues)
					outs := _functionCall(fn, kvs.Key, kvs.Values)
					sendMapOutputs(outChan, outs)
				case KeyValuesValuesType:
					kvv := input.Interface().(KeyValuesValues)
					outs := _functionCall(fn, kvv.Key, kvv.Values1, kvv.Values2)
					sendMapOutputs(outChan, outs)
				default:
					outs := fn.Call([]reflect.Value{input})
					sendMapOutputs(outChan, outs)
				}
			}
		}

		for input := range task.InputChan() {
			invokeMapFunc(input)
		}
		// println("exiting d:", d.Id, "step:", step.Id, "task:", task.Id)
	}
	return ret
}

func _functionCallWithChanOutput(fn reflect.Value, outChan reflect.Value, inputs ...interface{}) []reflect.Value {
	var args []reflect.Value
	for _, input := range inputs {
		args = append(args, reflect.ValueOf(input))
	}
	args = append(args, outChan)
	return fn.Call(args)
}

func _functionCall(fn reflect.Value, inputs ...interface{}) []reflect.Value {
	var args []reflect.Value
	for _, input := range inputs {
		args = append(args, reflect.ValueOf(input))
	}
	return fn.Call(args)
}

// f(A)bool
func (d *Dataset) Filter(f interface{}) *Dataset {
	ret, step := add1ShardTo1Step(d, d.Type)
	step.Name = "Filter"
	step.Function = func(task *Task) {
		fn := reflect.ValueOf(f)
		outChan := task.Outputs[0].WriteChan
		var outs []reflect.Value
		for input := range task.InputChan() {
			switch input.Type() {
			case KeyValueType:
				kv := input.Interface().(KeyValue)
				outs = _functionCall(fn, kv.Key, kv.Value)
			case KeyValueValueType:
				kv := input.Interface().(KeyValueValue)
				outs = _functionCall(fn, kv.Key, kv.Value1, kv.Value2)
			case KeyValuesType:
				kvs := input.Interface().(KeyValues)
				outs = _functionCall(fn, kvs.Key, kvs.Values)
			case KeyValuesValuesType:
				kvv := input.Interface().(KeyValuesValues)
				outs = _functionCall(fn, kvv.Key, kvv.Values1, kvv.Values2)
			default:
				outs = fn.Call([]reflect.Value{input})
			}
			if outs[0].Bool() {
				outChan.Send(input)
			}
		}
	}
	return ret
}

func add1ShardTo1Step(d *Dataset, nextDataType reflect.Type) (ret *Dataset, step *Step) {
	ret = d.context.newNextDataset(len(d.Shards), nextDataType)
	step = d.context.AddOneToOneStep(d, ret)
	return
}

// the value over the outChan is always reflect.Value
// but the inner values are always actual interface{} object
func sendMapOutputs(outChan reflect.Value, values []reflect.Value) {
	if !outChan.IsValid() {
		return
	}
	if len(values) == 2 {
		outChan.Send(reflect.ValueOf(KeyValue{values[0].Interface(), values[1].Interface()}))
		return
	}
	if len(values) == 1 {
		outChan.Send(values[0])
		return
	}
}
<|endoftext|>


<commit_before>/*
 * Copyright (C) 2016 Red Hat, Inc.
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

package probes

import (
	"errors"
	"fmt"
	"sync"
	"time"

	"github.com/google/gopacket"
	"github.com/google/gopacket/pcap"
	"github.com/redhat-cip/skydive/analyzer"
	"github.com/redhat-cip/skydive/api"
	"github.com/redhat-cip/skydive/flow"
	"github.com/redhat-cip/skydive/flow/mappings"
	"github.com/redhat-cip/skydive/logging"
	"github.com/redhat-cip/skydive/topology"
	"github.com/redhat-cip/skydive/topology/graph"
	"github.com/redhat-cip/skydive/topology/probes"
)

type PcapProbe struct {
	handle    *pcap.Handle
	channel   chan gopacket.Packet
	probePath string
}

type PcapProbesHandler struct {
	graph               *graph.Graph
	analyzerClient      *analyzer.Client
	flowTable           *flow.FlowTable
	flowMappingPipeline *mappings.FlowMappingPipeline
	wg                  sync.WaitGroup
	probes              map[string]*PcapProbe
	probesLock          sync.RWMutex
}

const (
	snaplen int32 = 256
)

func (p *PcapProbesHandler) handlePacket(pcapProbe *PcapProbe, packet gopacket.Packet) {
	flows := []*flow.Flow{flow.FlowFromGoPacket(p.flowTable, &packet, pcapProbe.probePath)}
	p.flowTable.Update(flows)
	p.flowMappingPipeline.Enhance(flows)

	if p.analyzerClient != nil {
		// FIX(safchain) add flow state cache in order to send only flow changes
		// to not flood the analyzer
		p.analyzerClient.SendFlows(flows)
	}
}

func (p *PcapProbesHandler) RegisterProbe(n *graph.Node, capture *api.Capture) error {
	logging.GetLogger().Debugf("Starting pcap capture on %s", n.Metadata()["Name"])

	if name, ok := n.Metadata()["Name"]; ok && name != "" {
		ifName := name.(string)

		if _, ok := p.probes[ifName]; ok {
			return errors.New(fmt.Sprintf("A pcap probe already exists for %s", ifName))
		}

		nodes := p.graph.LookupShortestPath(n, graph.Metadata{"Type": "host"}, topology.IsOwnershipEdge)
		if len(nodes) == 0 {
			return errors.New(fmt.Sprintf("Failed to determine probePath for %s", ifName))
		}

		handle, err := pcap.OpenLive(ifName, snaplen, true, time.Second)
		if err != nil {
			return err
		}

		if capture.BPFFilter != "" {
			handle.SetBPFFilter(capture.BPFFilter)
		}

		probePath := topology.NodePath{nodes}.Marshal()

		packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
		packetChannel := packetSource.Packets()

		probe := &PcapProbe{
			handle:    handle,
			channel:   packetChannel,
			probePath: probePath,
		}

		p.probesLock.Lock()
		p.probes[ifName] = probe
		p.probesLock.Unlock()

		p.wg.Add(1)
		go func() {
			defer p.wg.Done()

			for packet := range packetChannel {
				p.handlePacket(probe, packet)
			}
		}()
	}
	return nil
}

func (p *PcapProbesHandler) unregisterProbe(ifName string) error {
	if probe, ok := p.probes[ifName]; ok {
		close(probe.channel)
		delete(p.probes, ifName)
	}

	return nil
}

func (p *PcapProbesHandler) UnregisterProbe(n *graph.Node) error {
	p.probesLock.Lock()
	defer p.probesLock.Unlock()

	if name, ok := n.Metadata()["Name"]; ok && name != "" {
		ifName := name.(string)
		err := p.unregisterProbe(ifName)
		if err != nil {
			return err
		}
	}
	return nil
}

func (p *PcapProbesHandler) Start() {
}

func (p *PcapProbesHandler) Stop() {
	p.probesLock.Lock()
	defer p.probesLock.Unlock()

	for name := range p.probes {
		p.unregisterProbe(name)
	}
	p.wg.Wait()
}

func (o *PcapProbesHandler) Flush() {
}

func NewPcapProbesHandler(tb *probes.TopologyProbeBundle, g *graph.Graph, p *mappings.FlowMappingPipeline, a *analyzer.Client) *PcapProbesHandler {
	handler := &PcapProbesHandler{
		graph:               g,
		analyzerClient:      a,
		flowMappingPipeline: p,
		flowTable:           flow.NewFlowTable(),
		probes:              make(map[string]*PcapProbe),
	}
	return handler
}
<commit_msg>Fixup pcap probe Unregister()<commit_after>/*
 * Copyright (C) 2016 Red Hat, Inc.
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

package probes

import (
	"errors"
	"fmt"
	"sync"
	"time"

	"github.com/google/gopacket"
	"github.com/google/gopacket/pcap"
	"github.com/redhat-cip/skydive/analyzer"
	"github.com/redhat-cip/skydive/api"
	"github.com/redhat-cip/skydive/flow"
	"github.com/redhat-cip/skydive/flow/mappings"
	"github.com/redhat-cip/skydive/logging"
	"github.com/redhat-cip/skydive/topology"
	"github.com/redhat-cip/skydive/topology/graph"
	"github.com/redhat-cip/skydive/topology/probes"
)

type PcapProbe struct {
	handle    *pcap.Handle
	channel   chan gopacket.Packet
	probePath string
}

type PcapProbesHandler struct {
	graph               *graph.Graph
	analyzerClient      *analyzer.Client
	flowTable           *flow.FlowTable
	flowMappingPipeline *mappings.FlowMappingPipeline
	wg                  sync.WaitGroup
	probes              map[string]*PcapProbe
	probesLock          sync.RWMutex
}

const (
	snaplen int32 = 256
)

func (p *PcapProbesHandler) handlePacket(pcapProbe *PcapProbe, packet gopacket.Packet) {
	flows := []*flow.Flow{flow.FlowFromGoPacket(p.flowTable, &packet, pcapProbe.probePath)}
	p.flowTable.Update(flows)
	p.flowMappingPipeline.Enhance(flows)

	if p.analyzerClient != nil {
		// FIX(safchain) add flow state cache in order to send only flow changes
		// to not flood the analyzer
		p.analyzerClient.SendFlows(flows)
	}
}

func (p *PcapProbesHandler) RegisterProbe(n *graph.Node, capture *api.Capture) error {
	logging.GetLogger().Debugf("Starting pcap capture on %s", n.Metadata()["Name"])

	if name, ok := n.Metadata()["Name"]; ok && name != "" {
		ifName := name.(string)

		if _, ok := p.probes[ifName]; ok {
			return errors.New(fmt.Sprintf("A pcap probe already exists for %s", ifName))
		}

		nodes := p.graph.LookupShortestPath(n, graph.Metadata{"Type": "host"}, topology.IsOwnershipEdge)
		if len(nodes) == 0 {
			return errors.New(fmt.Sprintf("Failed to determine probePath for %s", ifName))
		}

		handle, err := pcap.OpenLive(ifName, snaplen, true, time.Second)
		if err != nil {
			return err
		}

		if capture.BPFFilter != "" {
			handle.SetBPFFilter(capture.BPFFilter)
		}

		probePath := topology.NodePath{nodes}.Marshal()

		packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
		packetChannel := packetSource.Packets()

		probe := &PcapProbe{
			handle:    handle,
			channel:   packetChannel,
			probePath: probePath,
		}

		p.probesLock.Lock()
		p.probes[ifName] = probe
		p.probesLock.Unlock()

		p.wg.Add(1)
		go func() {
			defer p.wg.Done()

			for packet := range probe.channel {
				p.handlePacket(probe, packet)
			}
		}()
	}
	return nil
}

func (p *PcapProbesHandler) unregisterProbe(ifName string) error {
	if probe, ok := p.probes[ifName]; ok {
		probe.handle.Close()
		delete(p.probes, ifName)
	}

	return nil
}

func (p *PcapProbesHandler) UnregisterProbe(n *graph.Node) error {
	p.probesLock.Lock()
	defer p.probesLock.Unlock()

	if name, ok := n.Metadata()["Name"]; ok && name != "" {
		ifName := name.(string)
		err := p.unregisterProbe(ifName)
		if err != nil {
			return err
		}
	}
	return nil
}

func (p *PcapProbesHandler) Start() {
}

func (p *PcapProbesHandler) Stop() {
	p.probesLock.Lock()
	defer p.probesLock.Unlock()

	for name := range p.probes {
		p.unregisterProbe(name)
	}
	p.wg.Wait()
}

func (o *PcapProbesHandler) Flush() {
}

func NewPcapProbesHandler(tb *probes.TopologyProbeBundle, g *graph.Graph, p *mappings.FlowMappingPipeline, a *analyzer.Client) *PcapProbesHandler {
	handler := &PcapProbesHandler{
		graph:               g,
		analyzerClient:      a,
		flowMappingPipeline: p,
		flowTable:           flow.NewFlowTable(),
		probes:              make(map[string]*PcapProbe),
	}
	return handler
}
<|endoftext|>


<commit_before>package main

import (
	"encoding/xml"
	"flag"
	"fmt"
	"net/http"
	"net/url"
	"strconv"

	"code.google.com/p/go-charset/charset"
	_ "code.google.com/p/go-charset/data"
)

type DateTimeType struct {
	Date struct {
		Day   int `xml:"day,attr"`
		Month int `xml:"month,attr"`
		Year  int `xml:"year,attr"`
	} `xml:"itdDate"`

	Time struct {
		Hour   int `xml:"hour,attr"`
		Minute int `xml:"minute,attr"`
	} `xml:"itdTime"`
}

type Line struct {
	Number    string `xml:"number,attr"`
	Direction string `xml:"direction,attr"`
}

type Departure struct {
	Countdown int    `xml:"countdown,attr"`
	Platform  string `xml:"platform,attr"`

	DateTime    DateTimeType `xml:"itdDateTime"`
	ServingLine Line         `xml:"itdServingLine"`
}

type StopInfo struct {
	State string `xml:"state,attr"`

	IdfdStop struct {
		StopName  string `xml:",chardata"`
		MatchQlty int    `xml:"matchQuality,attr"`
		StopID    int    `xml:"stopID,attr"`
	} `xml:"odvNameElem"`
}

type XmlResult struct {
	Stop       StopInfo    `xml:"itdDepartureMonitorRequest>itdOdv>itdOdvName"`
	Departures []Departure `xml:"itdDepartureMonitorRequest>itdDepartureList>itdDeparture"`
}

func main() {
	station_id := flag.String("stop", "Königsplatz", "id or (part of the) stop name")
	max_results := flag.Int("results", 5, "how many results to show")
	flag.Parse()

	baseURL := "http://efa.avv-augsburg.de/avv/"
	endpoint := "XML_DM_REQUEST"

	params := url.Values{
		"type_dm":              {"stop"},
		"name_dm":              {*station_id},
		"useRealtime":          {"1"},
		"locationServerActive": {"1"},
		"dmLineSelection":      {"all"},
		"limit":                {strconv.Itoa(*max_results)},
		"mode":                 {"direct"},
	}

	resp, err := http.PostForm(baseURL+endpoint, params)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	var result XmlResult
	decoder := xml.NewDecoder(resp.Body)
	decoder.CharsetReader = charset.NewReader
	if err = decoder.Decode(&result); err != nil {
		panic(err)
	}
	//fmt.Printf("%+v", result)

	if result.Stop.State != "identified" {
		fmt.Println("Stop does not exist or name is not unique!")
		return
	}
	fmt.Printf("Selected stop: %s (%d)\n\n",
		result.Stop.IdfdStop.StopName,
		result.Stop.IdfdStop.StopID)

	for _, departure := range result.Departures {
		plu := ""
		if departure.Countdown != 1 {
			plu = "s"
		}

		fmt.Printf("Route %-5s due in %-2d minute%s --> %s\n",
			departure.ServingLine.Number,
			departure.Countdown,
			plu,
			departure.ServingLine.Direction)
	}

}
<commit_msg>* Allow to set base-url via -baseurl.<commit_after>package main

import (
	"encoding/xml"
	"flag"
	"fmt"
	"net/http"
	"net/url"
	"strconv"

	"code.google.com/p/go-charset/charset"
	_ "code.google.com/p/go-charset/data"
)

type DateTimeType struct {
	Date struct {
		Day   int `xml:"day,attr"`
		Month int `xml:"month,attr"`
		Year  int `xml:"year,attr"`
	} `xml:"itdDate"`

	Time struct {
		Hour   int `xml:"hour,attr"`
		Minute int `xml:"minute,attr"`
	} `xml:"itdTime"`
}

type Line struct {
	Number    string `xml:"number,attr"`
	Direction string `xml:"direction,attr"`
}

type Departure struct {
	Countdown int    `xml:"countdown,attr"`
	Platform  string `xml:"platform,attr"`

	DateTime    DateTimeType `xml:"itdDateTime"`
	ServingLine Line         `xml:"itdServingLine"`
}

type StopInfo struct {
	State string `xml:"state,attr"`

	IdfdStop struct {
		StopName  string `xml:",chardata"`
		MatchQlty int    `xml:"matchQuality,attr"`
		StopID    int    `xml:"stopID,attr"`
	} `xml:"odvNameElem"`
}

type XmlResult struct {
	Stop       StopInfo    `xml:"itdDepartureMonitorRequest>itdOdv>itdOdvName"`
	Departures []Departure `xml:"itdDepartureMonitorRequest>itdDepartureList>itdDeparture"`
}

var (
	baseURL string
)

func main() {
	station_id := flag.String("stop", "Königsplatz", "id or (part of the) stop name")
	max_results := flag.Int("results", 5, "how many results to show")
	flag.StringVar(&baseURL, "baseurl", "http://efa.avv-augsburg.de/avv/", "base-url for EFA API")
	flag.Parse()

	endpoint := "XML_DM_REQUEST"
	params := url.Values{
		"type_dm":              {"stop"},
		"name_dm":              {*station_id},
		"useRealtime":          {"1"},
		"locationServerActive": {"1"},
		"dmLineSelection":      {"all"},
		"limit":                {strconv.Itoa(*max_results)},
		"mode":                 {"direct"},
	}

	resp, err := http.PostForm(baseURL+endpoint, params)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	var result XmlResult
	decoder := xml.NewDecoder(resp.Body)
	decoder.CharsetReader = charset.NewReader
	if err = decoder.Decode(&result); err != nil {
		panic(err)
	}
	//fmt.Printf("%+v", result)

	if result.Stop.State != "identified" {
		fmt.Println("Stop does not exist or name is not unique!")
		return
	}
	fmt.Printf("Selected stop: %s (%d)\n\n",
		result.Stop.IdfdStop.StopName,
		result.Stop.IdfdStop.StopID)

	for _, departure := range result.Departures {
		plu := ""
		if departure.Countdown != 1 {
			plu = "s"
		}

		fmt.Printf("Route %-5s due in %-2d minute%s --> %s\n",
			departure.ServingLine.Number,
			departure.Countdown,
			plu,
			departure.ServingLine.Direction)
	}

}
<|endoftext|>


<commit_before>package main

import (
	"log"

	"k8s.io/kubernetes/pkg/api"
)

func populateMetadata(obj *api.ObjectMeta, metadatas []interface{}) {
	if len(metadatas) == 0 {
		return
	}
	metadata := metadatas[0].(map[string]interface{})

	if _, ok := metadata["name"]; ok {
		obj.Name = metadata["name"].(string)
	}
	if _, ok := metadata["namespace"]; ok {
		obj.Namespace = metadata["namespace"].(string)
	}
	if _, ok := metadata["resource_version"]; ok {
		obj.ResourceVersion = metadata["resource_version"].(string)
	}
	if _, ok := metadata["labels"]; ok {
		obj.Labels = convertMapTypeToStringMap(metadata["labels"].(map[string]interface{}))
	}
	if _, ok := metadata["annotations"]; ok {
		obj.Labels = convertMapTypeToStringMap(metadata["annotations"].(map[string]interface{}))
	}
}

func buildEnvVar(userEnvVars []interface{}) []api.EnvVar {
	if len(userEnvVars) == 0 {
		return nil
	}

	var envVars []api.EnvVar

	for _, e := range userEnvVars {
		userEnvVar := e.(map[string]interface{})

		envVar := api.EnvVar{
			Name: userEnvVar["name"].(string),
		}

		if _, ok := userEnvVar["value"]; ok {
			log.Printf("envvar value : %s", userEnvVar["value"].(string))
			envVar.Value = userEnvVar["value"].(string)
		}

		envVars = append(envVars, envVar)
	}
	return envVars
}

func convertListToStringArray(list []interface{}) []string {
	if list == nil || len(list) == 0 {
		return nil
	}
	ret := make([]string, len(list))
	for po, val := range list {
		ret[po] = val.(string)
	}
	return ret
}

func convertMapTypeToStringMap(userConfig map[string]interface{}) map[string]string {
	config := make(map[string]string)
	for k, v := range userConfig {
		config[k] = v.(string)
	}
	return config
}
<commit_msg>setting proper annotation<commit_after>package main

import (
	"log"

	"k8s.io/kubernetes/pkg/api"
)

func populateMetadata(obj *api.ObjectMeta, metadatas []interface{}) {
	if len(metadatas) == 0 {
		return
	}
	metadata := metadatas[0].(map[string]interface{})

	if _, ok := metadata["name"]; ok {
		obj.Name = metadata["name"].(string)
	}
	if _, ok := metadata["namespace"]; ok {
		obj.Namespace = metadata["namespace"].(string)
	}
	if _, ok := metadata["resource_version"]; ok {
		obj.ResourceVersion = metadata["resource_version"].(string)
	}
	if _, ok := metadata["labels"]; ok {
		obj.Labels = convertMapTypeToStringMap(metadata["labels"].(map[string]interface{}))
	}
	if _, ok := metadata["annotations"]; ok {
		obj.Annotations = convertMapTypeToStringMap(metadata["annotations"].(map[string]interface{}))
	}
}

func buildEnvVar(userEnvVars []interface{}) []api.EnvVar {
	if len(userEnvVars) == 0 {
		return nil
	}

	var envVars []api.EnvVar

	for _, e := range userEnvVars {
		userEnvVar := e.(map[string]interface{})

		envVar := api.EnvVar{
			Name: userEnvVar["name"].(string),
		}

		if _, ok := userEnvVar["value"]; ok {
			log.Printf("envvar value : %s", userEnvVar["value"].(string))
			envVar.Value = userEnvVar["value"].(string)
		}

		envVars = append(envVars, envVar)
	}
	return envVars
}

func convertListToStringArray(list []interface{}) []string {
	if list == nil || len(list) == 0 {
		return nil
	}
	ret := make([]string, len(list))
	for po, val := range list {
		ret[po] = val.(string)
	}
	return ret
}

func convertMapTypeToStringMap(userConfig map[string]interface{}) map[string]string {
	config := make(map[string]string)
	for k, v := range userConfig {
		config[k] = v.(string)
	}
	return config
}
<|endoftext|>


<commit_before>package main

import (
	"github.com/prometheus/client_golang/prometheus"
)

type (
	executor struct {
		ID          string      `json:"executor_id"`
		Name        string      `json:"executor_name"`
		FrameworkID string      `json:"framework_id"`
		Source      string      `json:"source"`
		Statistics  *statistics `json:"statistics"`
		Tasks       []task      `json:"tasks"`
	}

	statistics struct {
		Processes             float64 `json:"processes"`
		Threads               float64 `json:"threads"`

		CpusLimit             float64 `json:"cpus_limit"`
		CpusSystemTimeSecs    float64 `json:"cpus_system_time_secs"`
		CpusUserTimeSecs      float64 `json:"cpus_user_time_secs"`
		CpusThrottledTimeSecs float64 `json:"cpus_throttled_time_secs"`
		CpusNrPeriods         float64 `json:"cpus_nr_periods"`
		CpusNrThrottled       float64 `json:"cpus_nr_throttled"`

		MemLimitBytes float64 `json:"mem_limit_bytes"`
		MemRssBytes   float64 `json:"mem_rss_bytes"`
		MemTotalBytes float64 `json:"mem_total_bytes"`
		MemCacheBytes float64 `json:"mem_cache_bytes"`
		MemSwapBytes  float64 `json:"mem_swap_bytes"`

		DiskLimitBytes float64 `json:"disk_limit_bytes"`
		DiskUsedBytes  float64 `json:"disk_used_bytes"`

		NetRxBytes   float64 `json:"net_rx_bytes"`
		NetRxDropped float64 `json:"net_rx_dropped"`
		NetRxErrors  float64 `json:"net_rx_errors"`
		NetRxPackets float64 `json:"net_rx_packets"`
		NetTxBytes   float64 `json:"net_tx_bytes"`
		NetTxDropped float64 `json:"net_tx_dropped"`
		NetTxErrors  float64 `json:"net_tx_errors"`
		NetTxPackets float64 `json:"net_tx_packets"`
	}

	slaveCollector struct {
		*httpClient
		metrics map[*prometheus.Desc]metric
	}

	metric struct {
		valueType prometheus.ValueType
		get       func(*statistics) float64
	}
)

func newSlaveMonitorCollector(httpClient *httpClient) prometheus.Collector {
	labels := []string{"id", "framework_id", "source"}

	return &slaveCollector{
		httpClient: httpClient,
		metrics: map[*prometheus.Desc]metric{
			// Processes
			prometheus.NewDesc(
				"processes",
				"Current number of processes",
				labels, nil,
			): metric{prometheus.GaugeValue, func(s *statistics) float64 { return s.Processes }},
			prometheus.NewDesc(
				"threads",
				"Current number of threads",
				labels, nil,
			): metric{prometheus.GaugeValue, func(s *statistics) float64 { return s.Threads }},

			// CPU
			prometheus.NewDesc(
				"cpus_limit",
				"Current limit of CPUs for task",
				labels, nil,
			): metric{prometheus.GaugeValue, func(s *statistics) float64 { return s.CpusLimit }},
			prometheus.NewDesc(
				"cpu_system_seconds_total",
				"Total system CPU seconds",
				labels, nil,
			): metric{prometheus.CounterValue, func(s *statistics) float64 { return s.CpusSystemTimeSecs }},
			prometheus.NewDesc(
				"cpu_user_seconds_total",
				"Total user CPU seconds",
				labels, nil,
			): metric{prometheus.CounterValue, func(s *statistics) float64 { return s.CpusUserTimeSecs }},
			prometheus.NewDesc(
				"cpu_throttled_seconds_total",
				"Total time CPU was throttled due to CFS bandwidth control",
				labels, nil,
			): metric{prometheus.CounterValue, func(s *statistics) float64 { return s.CpusThrottledTimeSecs }},
			prometheus.NewDesc(
				"cpu_nr_periods_total",
				"Total number of elapsed CFS enforcement intervals",
				labels, nil,
			): metric{prometheus.CounterValue, func(s *statistics) float64 { return s.CpusNrPeriods }},
			prometheus.NewDesc(
				"cpu_nr_throttled_total",
				"Total number of throttled CFS enforcement intervals.",
				labels, nil,
			): metric{prometheus.CounterValue, func(s *statistics) float64 { return s.CpusNrThrottled }},

			// Memory
			prometheus.NewDesc(
				"mem_limit_bytes",
				"Current memory limit in bytes",
				labels, nil,
			): metric{prometheus.GaugeValue, func(s *statistics) float64 { return s.MemLimitBytes }},
			prometheus.NewDesc(
				"mem_rss_bytes",
				"Current rss memory usage",
				labels, nil,
			): metric{prometheus.GaugeValue, func(s *statistics) float64 { return s.MemRssBytes }},
			prometheus.NewDesc(
				"mem_total_bytes",
				"Current total memory usage",
				labels, nil,
			): metric{prometheus.GaugeValue, func(s *statistics) float64 { return s.MemTotalBytes }},
			prometheus.NewDesc(
				"mem_cache_bytes",
				"Current page cache memory usage",
				labels, nil,
			): metric{prometheus.GaugeValue, func(s *statistics) float64 { return s.MemCacheBytes }},
			prometheus.NewDesc(
				"mem_swap_bytes",
				"Current swap usage",
				labels, nil,
			): metric{prometheus.GaugeValue, func(s *statistics) float64 { return s.MemSwapBytes }},

			// Disk
			prometheus.NewDesc(
				"disk_limit_bytes",
				"Current disk limit in bytes",
				labels, nil,
			): metric{prometheus.GaugeValue, func(s *statistics) float64 { return s.DiskLimitBytes }},
			prometheus.NewDesc(
				"disk_used_bytes",
				"Current disk usage",
				labels, nil,
			): metric{prometheus.GaugeValue, func(s *statistics) float64 { return s.DiskUsedBytes }},

			// Network
			// - RX
			prometheus.NewDesc(
				"network_receive_bytes_total",
				"Total bytes received",
				labels, nil,
			): metric{prometheus.CounterValue, func(s *statistics) float64 { return s.NetRxBytes }},
			prometheus.NewDesc(
				"network_receive_dropped_total",
				"Total packets dropped while receiving",
				labels, nil,
			): metric{prometheus.CounterValue, func(s *statistics) float64 { return s.NetRxDropped }},
			prometheus.NewDesc(
				"network_receive_errors_total",
				"Total errors while receiving",
				labels, nil,
			): metric{prometheus.CounterValue, func(s *statistics) float64 { return s.NetRxErrors }},
			prometheus.NewDesc(
				"network_receive_packets_total",
				"Total packets received",
				labels, nil,
			): metric{prometheus.CounterValue, func(s *statistics) float64 { return s.NetRxPackets }},
			// - TX
			prometheus.NewDesc(
				"network_transmit_bytes_total",
				"Total bytes transmitted",
				labels, nil,
			): metric{prometheus.CounterValue, func(s *statistics) float64 { return s.NetTxBytes }},
			prometheus.NewDesc(
				"network_transmit_dropped_total",
				"Total packets dropped while transmitting",
				labels, nil,
			): metric{prometheus.CounterValue, func(s *statistics) float64 { return s.NetTxDropped }},
			prometheus.NewDesc(
				"network_transmit_errors_total",
				"Total errors while transmitting",
				labels, nil,
			): metric{prometheus.CounterValue, func(s *statistics) float64 { return s.NetTxErrors }},
			prometheus.NewDesc(
				"network_transmit_packets_total",
				"Total packets transmitted",
				labels, nil,
			): metric{prometheus.CounterValue, func(s *statistics) float64 { return s.NetTxPackets }},
		},
	}
}

func (c *slaveCollector) Collect(ch chan<- prometheus.Metric) {
	stats := []executor{}
	c.fetchAndDecode("/monitor/statistics", &stats)

	for _, exec := range stats {
		for desc, m := range c.metrics {
			ch <- prometheus.MustNewConstMetric(desc, m.valueType, m.get(exec.Statistics), exec.ID, exec.FrameworkID, exec.Source)
		}
	}
}

func (c *slaveCollector) Describe(ch chan<- *prometheus.Desc) {
	for metric := range c.metrics {
		ch <- metric
	}
}
<commit_msg>Fix formatting (gofmt)<commit_after>package main

import (
	"github.com/prometheus/client_golang/prometheus"
)

type (
	executor struct {
		ID          string      `json:"executor_id"`
		Name        string      `json:"executor_name"`
		FrameworkID string      `json:"framework_id"`
		Source      string      `json:"source"`
		Statistics  *statistics `json:"statistics"`
		Tasks       []task      `json:"tasks"`
	}

	statistics struct {
		Processes float64 `json:"processes"`
		Threads   float64 `json:"threads"`

		CpusLimit             float64 `json:"cpus_limit"`
		CpusSystemTimeSecs    float64 `json:"cpus_system_time_secs"`
		CpusUserTimeSecs      float64 `json:"cpus_user_time_secs"`
		CpusThrottledTimeSecs float64 `json:"cpus_throttled_time_secs"`
		CpusNrPeriods         float64 `json:"cpus_nr_periods"`
		CpusNrThrottled       float64 `json:"cpus_nr_throttled"`

		MemLimitBytes float64 `json:"mem_limit_bytes"`
		MemRssBytes   float64 `json:"mem_rss_bytes"`
		MemTotalBytes float64 `json:"mem_total_bytes"`
		MemCacheBytes float64 `json:"mem_cache_bytes"`
		MemSwapBytes  float64 `json:"mem_swap_bytes"`

		DiskLimitBytes float64 `json:"disk_limit_bytes"`
		DiskUsedBytes  float64 `json:"disk_used_bytes"`

		NetRxBytes   float64 `json:"net_rx_bytes"`
		NetRxDropped float64 `json:"net_rx_dropped"`
		NetRxErrors  float64 `json:"net_rx_errors"`
		NetRxPackets float64 `json:"net_rx_packets"`
		NetTxBytes   float64 `json:"net_tx_bytes"`
		NetTxDropped float64 `json:"net_tx_dropped"`
		NetTxErrors  float64 `json:"net_tx_errors"`
		NetTxPackets float64 `json:"net_tx_packets"`
	}

	slaveCollector struct {
		*httpClient
		metrics map[*prometheus.Desc]metric
	}

	metric struct {
		valueType prometheus.ValueType
		get       func(*statistics) float64
	}
)

func newSlaveMonitorCollector(httpClient *httpClient) prometheus.Collector {
	labels := []string{"id", "framework_id", "source"}

	return &slaveCollector{
		httpClient: httpClient,
		metrics: map[*prometheus.Desc]metric{
			// Processes
			prometheus.NewDesc(
				"processes",
				"Current number of processes",
				labels, nil,
			): metric{prometheus.GaugeValue, func(s *statistics) float64 { return s.Processes }},
			prometheus.NewDesc(
				"threads",
				"Current number of threads",
				labels, nil,
			): metric{prometheus.GaugeValue, func(s *statistics) float64 { return s.Threads }},

			// CPU
			prometheus.NewDesc(
				"cpus_limit",
				"Current limit of CPUs for task",
				labels, nil,
			): metric{prometheus.GaugeValue, func(s *statistics) float64 { return s.CpusLimit }},
			prometheus.NewDesc(
				"cpu_system_seconds_total",
				"Total system CPU seconds",
				labels, nil,
			): metric{prometheus.CounterValue, func(s *statistics) float64 { return s.CpusSystemTimeSecs }},
			prometheus.NewDesc(
				"cpu_user_seconds_total",
				"Total user CPU seconds",
				labels, nil,
			): metric{prometheus.CounterValue, func(s *statistics) float64 { return s.CpusUserTimeSecs }},
			prometheus.NewDesc(
				"cpu_throttled_seconds_total",
				"Total time CPU was throttled due to CFS bandwidth control",
				labels, nil,
			): metric{prometheus.CounterValue, func(s *statistics) float64 { return s.CpusThrottledTimeSecs }},
			prometheus.NewDesc(
				"cpu_nr_periods_total",
				"Total number of elapsed CFS enforcement intervals",
				labels, nil,
			): metric{prometheus.CounterValue, func(s *statistics) float64 { return s.CpusNrPeriods }},
			prometheus.NewDesc(
				"cpu_nr_throttled_total",
				"Total number of throttled CFS enforcement intervals.",
				labels, nil,
			): metric{prometheus.CounterValue, func(s *statistics) float64 { return s.CpusNrThrottled }},

			// Memory
			prometheus.NewDesc(
				"mem_limit_bytes",
				"Current memory limit in bytes",
				labels, nil,
			): metric{prometheus.GaugeValue, func(s *statistics) float64 { return s.MemLimitBytes }},
			prometheus.NewDesc(
				"mem_rss_bytes",
				"Current rss memory usage",
				labels, nil,
			): metric{prometheus.GaugeValue, func(s *statistics) float64 { return s.MemRssBytes }},
			prometheus.NewDesc(
				"mem_total_bytes",
				"Current total memory usage",
				labels, nil,
			): metric{prometheus.GaugeValue, func(s *statistics) float64 { return s.MemTotalBytes }},
			prometheus.NewDesc(
				"mem_cache_bytes",
				"Current page cache memory usage",
				labels, nil,
			): metric{prometheus.GaugeValue, func(s *statistics) float64 { return s.MemCacheBytes }},
			prometheus.NewDesc(
				"mem_swap_bytes",
				"Current swap usage",
				labels, nil,
			): metric{prometheus.GaugeValue, func(s *statistics) float64 { return s.MemSwapBytes }},

			// Disk
			prometheus.NewDesc(
				"disk_limit_bytes",
				"Current disk limit in bytes",
				labels, nil,
			): metric{prometheus.GaugeValue, func(s *statistics) float64 { return s.DiskLimitBytes }},
			prometheus.NewDesc(
				"disk_used_bytes",
				"Current disk usage",
				labels, nil,
			): metric{prometheus.GaugeValue, func(s *statistics) float64 { return s.DiskUsedBytes }},

			// Network
			// - RX
			prometheus.NewDesc(
				"network_receive_bytes_total",
				"Total bytes received",
				labels, nil,
			): metric{prometheus.CounterValue, func(s *statistics) float64 { return s.NetRxBytes }},
			prometheus.NewDesc(
				"network_receive_dropped_total",
				"Total packets dropped while receiving",
				labels, nil,
			): metric{prometheus.CounterValue, func(s *statistics) float64 { return s.NetRxDropped }},
			prometheus.NewDesc(
				"network_receive_errors_total",
				"Total errors while receiving",
				labels, nil,
			): metric{prometheus.CounterValue, func(s *statistics) float64 { return s.NetRxErrors }},
			prometheus.NewDesc(
				"network_receive_packets_total",
				"Total packets received",
				labels, nil,
			): metric{prometheus.CounterValue, func(s *statistics) float64 { return s.NetRxPackets }},
			// - TX
			prometheus.NewDesc(
				"network_transmit_bytes_total",
				"Total bytes transmitted",
				labels, nil,
			): metric{prometheus.CounterValue, func(s *statistics) float64 { return s.NetTxBytes }},
			prometheus.NewDesc(
				"network_transmit_dropped_total",
				"Total packets dropped while transmitting",
				labels, nil,
			): metric{prometheus.CounterValue, func(s *statistics) float64 { return s.NetTxDropped }},
			prometheus.NewDesc(
				"network_transmit_errors_total",
				"Total errors while transmitting",
				labels, nil,
			): metric{prometheus.CounterValue, func(s *statistics) float64 { return s.NetTxErrors }},
			prometheus.NewDesc(
				"network_transmit_packets_total",
				"Total packets transmitted",
				labels, nil,
			): metric{prometheus.CounterValue, func(s *statistics) float64 { return s.NetTxPackets }},
		},
	}
}

func (c *slaveCollector) Collect(ch chan<- prometheus.Metric) {
	stats := []executor{}
	c.fetchAndDecode("/monitor/statistics", &stats)

	for _, exec := range stats {
		for desc, m := range c.metrics {
			ch <- prometheus.MustNewConstMetric(desc, m.valueType, m.get(exec.Statistics), exec.ID, exec.FrameworkID, exec.Source)
		}
	}
}

func (c *slaveCollector) Describe(ch chan<- *prometheus.Desc) {
	for metric := range c.metrics {
		ch <- metric
	}
}
<|endoftext|>


<commit_before>package dsbldr

import (
	"net/http"
	"net/url"
	"reflect"
	"testing"
)

func TestNewBuilder(t *testing.T) {
	b := NewBuilder(4, 100)
	if got, want := len(b.data), 101; got != want {
		t.Fatalf("got: %v\n want: %v\n ", got, want)
	}
	if got, want := len(b.data[0]), 4; got != want {
		t.Fatalf("got: %v\n want: %v\n ", got, want)
	}
	if got, want := b.records, 100; got != want {
		t.Fatalf("got: %v\n want: %v\n ", got, want)
	}
}

func TestAddFeatureData(t *testing.T) {
	b := NewBuilder(4, 3)
	b.addFeatureData("newFeature", []string{"one", "two", "three"})
	expectedData := [][]string{
		[]string{"newFeature", "", "", ""},
		[]string{"one", "", "", ""},
		[]string{"two", "", "", ""},
		[]string{"three", "", "", ""},
	}
	if got, want := b.data, expectedData; !reflect.DeepEqual(got, want) {
		t.Fatalf("got: %v\n want: %v\n ", got, want)
	}
}

func TestAddFeature(t *testing.T) {
	f := &Feature{
		Name:     "feat1",
		Endpoint: "/endpoint1/",
		RunFunc: func(res []string) []string {
			return []string{"one", "two", "three"}
		},
	}
	b := NewBuilder(4, 3)
	b.AddFeatures(f)
	if got, want := b.featureMap["feat1"], f; got != want {
		t.Fatalf("got: %v\n want: %v\n ", got, want)
	}
}

func TestGetFeatureData(t *testing.T) {
	f := &Feature{
		Name:     "feat1",
		Endpoint: "/endpoint1/",
	}
	b := NewBuilder(4, 3)
	data := []string{"one", "two", "three"}

	t.Log(b.data)
	err := b.addFeatureData(f.Name, data)
	t.Log(b.data)
	if err != nil {
		t.Errorf("Error Occured: %v", err)
	}

	want := []string{"one", "two", "three"}
	if got := b.getFeatureData("feat1"); !reflect.DeepEqual(got, want) {
		t.Fatalf("got: %v\n want: %v\n ", got, want)
	}
}

func TestGetFeature(t *testing.T) {
	f := &Feature{
		Name:     "feat1",
		Endpoint: "/endpoint1/",
		RunFunc: func(res []string) []string {
			return []string{"one", "two", "three"}
		},
	}
	b := NewBuilder(4, 3)
	b.AddFeatures(f)
	if got, want := b.GetFeature("feat1"), f; !reflect.DeepEqual(got, want) {
		t.Fatalf("got: %v\n want: %v\n ", got, want)
	}
}

func TestGetFeatureIfNoFeatures(t *testing.T) {
	var NilFeature *Feature
	b := NewBuilder(4, 3)
	if got, want := b.GetFeature("feat1"), NilFeature; !reflect.DeepEqual(got, want) {
		t.Fatalf("got: %v\n want: %v\n ", got, want)
	}
}

func TestCreateRequest(t *testing.T) {
	b := NewBuilder(4, 3) // arbitrary preallocations
	b.BaseURL = "baseurl.com"
	sampleOAuthHeader := BasicOAuthHeader(
		"consumerKey", "nonce", "signature", "signatureMethod",
		"timestamp", "token",
	)
	b.RequestHeaders = map[string]string{
		"OAuth": sampleOAuthHeader,
	}
	endpoint := "/myEndpoint"

	got, err := b.createRequest(endpoint)
	if err != nil {
		t.Errorf("Error Occured: %v", err)
	}

	u, err := url.Parse(b.BaseURL + endpoint)
	if err != nil {
		t.Errorf("Error Occured: %v", err)
	}

	want := &http.Request{
		Method:     "GET",
		URL:        u,
		Proto:      "HTTP/1.1",
		ProtoMajor: 1,
		ProtoMinor: 1,
		Header: map[string][]string{
			"Authorization": []string{sampleOAuthHeader},
		},
		Body: nil,
		Host: u.Host,
	}

	if reflect.DeepEqual(got, want) {
		t.Fatalf("got: %v\n want: %v\n ", got, want)
	}

}

func TestResolveFeatureEndpoints(t *testing.T) {
	b := NewBuilder(2, 3)
	b.BaseURL = "baseurl.com"

	f1 := &Feature{
		Name:     "feat1",
		Endpoint: "/endpoint1/",
	}

	f2 := &Feature{
		Name:     "feat2",
		Endpoint: "/endpoint2/{{feat1}}",
	}

	b.AddFeatures(f1, f2)
	b.addFeatureData(f1.Name, []string{"one", "two", "three"})

	got, err := b.resolveFeatureEndpoints(f2)
	if err != nil {
		t.Errorf("Error Occured: %v", err)
	}

	want := []string{
		"/endpoint2/one",
		"/endpoint2/two",
		"/endpoint2/three",
	}

	if !reflect.DeepEqual(got, want) {
		t.Fatalf("got: %v\n want: %v\n ", got, want)
	}
}

// func TestRetrieveFeature(t *testing.T) {
// 	b := NewBuilder(4, 3)
// }

type fakeHTTPClient http.Client

func (fhc *fakeHTTPClient) Do(http.Request) (*http.Response, error) {
	return &http.Response{
		Status:     "200 OK",
		StatusCode: 200,
	}, nil
}

func TestPopulateFeatureData(t *testing.T) {

}
<commit_msg>Fix TestGetFeatureData<commit_after>package dsbldr

import (
	"bytes"
	"io/ioutil"
	"net/http"
	"net/url"
	"reflect"
	"testing"
)

func TestNewBuilder(t *testing.T) {
	b := NewBuilder(4, 100)
	if got, want := len(b.data), 101; got != want {
		t.Fatalf("got: %v\n want: %v\n ", got, want)
	}
	if got, want := len(b.data[0]), 4; got != want {
		t.Fatalf("got: %v\n want: %v\n ", got, want)
	}
	if got, want := b.records, 100; got != want {
		t.Fatalf("got: %v\n want: %v\n ", got, want)
	}
}

func TestAddFeatureData(t *testing.T) {
	b := NewBuilder(4, 3)
	b.addFeatureData("newFeature", []string{"one", "two", "three"})
	expectedData := [][]string{
		[]string{"newFeature", "", "", ""},
		[]string{"one", "", "", ""},
		[]string{"two", "", "", ""},
		[]string{"three", "", "", ""},
	}
	if got, want := b.data, expectedData; !reflect.DeepEqual(got, want) {
		t.Fatalf("got: %v\n want: %v\n ", got, want)
	}
}

func TestAddFeature(t *testing.T) {
	f := &Feature{
		Name:     "feat1",
		Endpoint: "/endpoint1/",
		RunFunc: func(res []string) []string {
			return []string{"one", "two", "three"}
		},
	}
	b := NewBuilder(4, 3)
	b.AddFeatures(f)
	if got, want := b.featureMap["feat1"], f; got != want {
		t.Fatalf("got: %v\n want: %v\n ", got, want)
	}
}

func TestGetFeatureData(t *testing.T) {
	f := &Feature{
		Name:     "feat1",
		Endpoint: "/endpoint1/",
	}
	// Note that the test fails when there is a greater featureCount
	// than there are features when builder.getFeatureData is called
	b := NewBuilder(1, 3)
	data := []string{"one", "two", "three"}

	t.Log(b.data)
	err := b.addFeatureData(f.Name, data)
	t.Log(b.data)
	if err != nil {
		t.Errorf("Error Occured: %v", err)
	}

	want := []string{"one", "two", "three"}
	if got := b.getFeatureData("feat1"); !reflect.DeepEqual(got, want) {
		t.Fatalf("got: %v\n want: %v\n ", got, want)
	}
}

func TestGetFeature(t *testing.T) {
	f := &Feature{
		Name:     "feat1",
		Endpoint: "/endpoint1/",
		RunFunc: func(res []string) []string {
			return []string{"one", "two", "three"}
		},
	}
	b := NewBuilder(4, 3)
	b.AddFeatures(f)
	if got, want := b.GetFeature("feat1"), f; !reflect.DeepEqual(got, want) {
		t.Fatalf("got: %v\n want: %v\n ", got, want)
	}
}

func TestGetFeatureIfNoFeatures(t *testing.T) {
	var NilFeature *Feature
	b := NewBuilder(4, 3)
	if got, want := b.GetFeature("feat1"), NilFeature; !reflect.DeepEqual(got, want) {
		t.Fatalf("got: %v\n want: %v\n ", got, want)
	}
}

func TestCreateRequest(t *testing.T) {
	b := NewBuilder(4, 3) // arbitrary preallocations
	b.BaseURL = "baseurl.com"
	sampleOAuthHeader := BasicOAuthHeader(
		"consumerKey", "nonce", "signature", "signatureMethod",
		"timestamp", "token",
	)
	b.RequestHeaders = map[string]string{
		"OAuth": sampleOAuthHeader,
	}
	endpoint := "/myEndpoint"

	got, err := b.createRequest(endpoint)
	if err != nil {
		t.Errorf("Error Occured: %v", err)
	}

	u, err := url.Parse(b.BaseURL + endpoint)
	if err != nil {
		t.Errorf("Error Occured: %v", err)
	}

	want := &http.Request{
		Method:     "GET",
		URL:        u,
		Proto:      "HTTP/1.1",
		ProtoMajor: 1,
		ProtoMinor: 1,
		Header: map[string][]string{
			"Authorization": []string{sampleOAuthHeader},
		},
		Body: nil,
		Host: u.Host,
	}

	if reflect.DeepEqual(got, want) {
		t.Fatalf("got: %v\n want: %v\n ", got, want)
	}

}

func TestResolveFeatureEndpoints(t *testing.T) {
	b := NewBuilder(2, 3)
	b.BaseURL = "baseurl.com"

	f1 := &Feature{
		Name:     "feat1",
		Endpoint: "/endpoint1/",
	}

	f2 := &Feature{
		Name:     "feat2",
		Endpoint: "/endpoint2/{{feat1}}",
	}

	b.AddFeatures(f1, f2)
	b.addFeatureData(f1.Name, []string{"one", "two", "three"})

	got, err := b.resolveFeatureEndpoints(f2)
	if err != nil {
		t.Errorf("Error Occured: %v", err)
	}

	want := []string{
		"/endpoint2/one",
		"/endpoint2/two",
		"/endpoint2/three",
	}

	if !reflect.DeepEqual(got, want) {
		t.Fatalf("got: %v\n want: %v\n ", got, want)
	}
}

// func TestRetrieveFeature(t *testing.T) {
// 	b := NewBuilder(4, 3)
// }

type fakeHttpClient struct{}

var fakeResponseDump string = `
{
	id: 1000
	id_str: "1000"
	text: "this is a young tweet; tweety tweet tweet"
}
`

func (fhc *fakeHttpClient) Do(req http.Request) (*http.Response, error) {
	return &http.Response{
		Status:     "200 OK",
		StatusCode: 200,
		Body:       ioutil.NopCloser(bytes.NewBufferString(fakeResponseDump)),
	}, nil
}

func TestPopulateFeatureData(t *testing.T) {
	b := NewBuilder(2, 3)
	fakeClient := fakeHttpClient{}
	b.BaseURL = "baseurl.com"
	f := &Feature{
		Name:     "f1",
		Endpoint: "/endpoint",
		RunFunc: func(res []string) []string {
			return []string{"one", "two", "three"}
		},
	}

	got, err := b.populateFeatureData(f, &fakeClient)
	if err != nil {
		t.Errorf("Error Occured: %v", err)
	}
	if want := fakeResponseDump; got[0] != want {
		t.Fatalf("got: %v\n want: %v\n", got, want)
	}
}

func TestRun(t *testing.T) {

}
<|endoftext|>


<commit_before>/* {{{ Copyright (c) Paul R. Tagliamonte <paultag@debian.org>, 2015
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE. }}} */

package reprepro

import (
	"bufio"
	"fmt"
	"io"
	"strings"
)

type BuildNeedingPackage struct {
	Source   string
	Version  string
	Location string
	Arch     string
}

func (repo *Repo) BuildNeeding(suite string, arch string) ([]BuildNeedingPackage, error) {
	ret := []BuildNeedingPackage{}
	cmd := repo.Command("build-needing", suite, arch)
	out, err := cmd.Output()
	if err != nil {
		return ret, err
	}
	reader := bufio.NewReader(strings.NewReader(string(out)))
	for {
		line, err := reader.ReadString('\n')
		line = strings.Trim(line, " \n\r\t")
		if err != nil {
			if err == io.EOF {
				break
			}
			return ret, err
		}
		els := strings.Split(line, " ")
		if len(els) != 4 {
			return ret, fmt.Errorf("Unexpected input: %s", line)
		}
		ret = append(ret, BuildNeedingPackage{
			Source:   els[0],
			Version:  els[1],
			Location: els[2],
			Arch:     els[3],
		})
	}
	return ret, nil
}
<commit_msg>add vim modeline<commit_after>/* {{{ Copyright (c) Paul R. Tagliamonte <paultag@debian.org>, 2015
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE. }}} */

package reprepro

import (
	"bufio"
	"fmt"
	"io"
	"strings"
)

type BuildNeedingPackage struct {
	Source   string
	Version  string
	Location string
	Arch     string
}

func (repo *Repo) BuildNeeding(suite string, arch string) ([]BuildNeedingPackage, error) {
	ret := []BuildNeedingPackage{}
	cmd := repo.Command("build-needing", suite, arch)
	out, err := cmd.Output()
	if err != nil {
		return ret, err
	}
	reader := bufio.NewReader(strings.NewReader(string(out)))
	for {
		line, err := reader.ReadString('\n')
		line = strings.Trim(line, " \n\r\t")
		if err != nil {
			if err == io.EOF {
				break
			}
			return ret, err
		}
		els := strings.Split(line, " ")
		if len(els) != 4 {
			return ret, fmt.Errorf("Unexpected input: %s", line)
		}
		ret = append(ret, BuildNeedingPackage{
			Source:   els[0],
			Version:  els[1],
			Location: els[2],
			Arch:     els[3],
		})
	}
	return ret, nil
}

// vim: foldmethod=marker
<|endoftext|>


<commit_before>package main

import(
  "fmt"
  "io"
  "io/ioutil"
  "crypto/sha1"
  "os"
  "os/exec"
  "path/filepath"
  "bytes"
  "runtime"
  "strings"
  "github.com/kr/s3/s3util"
)

func fileExists(path string) bool {
  _, err := os.Stat(path)
  return err == nil
}

func open(s string) (io.ReadCloser, error) {
  if isURL(s) {
    return s3util.Open(s, nil)
  }
  return os.Open(s)
}

func create(s string) (io.WriteCloser, error) {
  if isURL(s) {
    return s3util.Create(s, nil, nil)
  }
  return os.Create(s)
}

func isURL(s string) bool {
  return strings.HasPrefix(s, "http://") || strings.HasPrefix(s, "https://")
}

func s3url(filename string) string {
  format := "https://s3.amazonaws.com/%s/%s"
  url := fmt.Sprintf(format, os.Getenv("S3_BUCKET"), filename)

  return url
}

func sh(command string) (string, error) {
  var output bytes.Buffer

  cmd := exec.Command("bash", "-c", command)

  cmd.Stdout = &output
  cmd.Stderr = &output

  err := cmd.Run()
  return output.String(), err
}

func calculateChecksum(buffer string) string {
  h := sha1.New()
  io.WriteString(h, buffer)
  return fmt.Sprintf("%x", h.Sum(nil))
}

func transferArchive(file string, url string) {
  s3util.DefaultConfig.AccessKey = os.Getenv("S3_ACCESS_KEY")
  s3util.DefaultConfig.SecretKey = os.Getenv("S3_SECRET_KEY")

  r, err := open(url)
  if err != nil {
    fmt.Fprintln(os.Stderr, err)
    os.Exit(1)
  }

  w, err := create(file)
  if err != nil {
    fmt.Fprintln(os.Stderr, err)
    os.Exit(1)
  }

  _, err = io.Copy(w, r)
  if err != nil {
    fmt.Fprintln(os.Stderr, err)
    os.Exit(1)
  }

  err = w.Close()
  if err != nil {
    fmt.Fprintln(os.Stderr, err)
    os.Exit(1)
  }
}

/* Extract archive to path */
func extractArchive(filename string, path string) bool {
  cmd_mkdir   := fmt.Sprintf("cd %s && mkdir .bundle", path)
  cmd_move    := fmt.Sprintf("mv %s %s/.bundle/bundle_cache.tar.gz", filename, path)
  cmd_extract := fmt.Sprintf("cd %s/.bundle && tar -xzf ./bundle_cache.tar.gz", path)
  cmd_remove  := fmt.Sprintf("rm %s/.bundle/bundle_cache.tar.gz", path)

  /* Create bundle directory */
  if _, err := sh(cmd_mkdir) ; err != nil {
    fmt.Println("Bundle directory '.bundle' already exists")
    return false
  }

  /* Move file */
  if _, err := sh(cmd_move) ; err != nil {
    fmt.Println("Unable to move file")
    return false
  }

  if out, err := sh(cmd_extract) ; err != nil {
    fmt.Println("Unable to extract:", out)
    return false
  }

  if _, err := sh(cmd_remove) ; err != nil {
    fmt.Println("Unable to remove archive")
    return false
  }

  return true
}

func envDefined(name string) bool {
  result := os.Getenv(name)
  return len(result) > 0
}

func checkS3Credentials() {
  required := [3]string { "S3_ACCESS_KEY", "S3_SECRET_KEY", "S3_BUCKET" }

  for _, v := range required {
    if !envDefined(v) {
      fmt.Printf("Please define %s environment variable\n", v)
      os.Exit(2)
    }
  }
}

func printUsage() {
  fmt.Println("Usage: bundle_cache [download|upload]")
  os.Exit(2)
}

func main() {
  args := os.Args[1:]

  if len(args) != 1 {
    printUsage()
  }

  action := args[0]

  /* Check if S3 credentials are set */
  checkS3Credentials()

  /* Get all path information */
  path, _       := os.Getwd()
  name          := filepath.Base(path)
  bundle_path   := fmt.Sprintf("%s/.bundle", path)
  lockfile_path := fmt.Sprintf("%s/Gemfile.lock", path)

  /* Check if lockfile exists */
  if !fileExists(lockfile_path) {
    fmt.Println("Gemfile.lock does not exist")
    os.Exit(1)
  }

  /* Read contents of lockfile */
  lockfile, err := ioutil.ReadFile(lockfile_path)
  if err != nil {
    fmt.Println("Unable to read Gemfile.lock")
    os.Exit(1)
  }

  /* Calculate SHA1 checksum for Gemfile.lock */
  checksum := calculateChecksum(string(lockfile))

  /* Make archive save filename */
  archive_name := fmt.Sprintf("%s_%s_%s.tar.gz", name, checksum, runtime.GOARCH)
  archive_path := fmt.Sprintf("/tmp/%s", archive_name)
  archive_url  := fmt.Sprintf("https://s3.amazonaws.com/bundle_cache/%s", archive_name)

  /* Check if archive already exists */
  if fileExists(archive_path) {
    if os.Remove(archive_path) != nil {
      fmt.Println("Failed to remove existing archive")
      os.Exit(1)
    }
  }

  if action == "upload" || action == "up" {
    if !fileExists(bundle_path) {
      fmt.Println("Bundle path does not exist")
      os.Exit(1)
    }

    cmd := fmt.Sprintf("cd %s && tar -czf %s .", bundle_path, archive_path)

    if out, err := sh(cmd); err != nil {
      fmt.Println("Failed to make archive:", out)
      os.Exit(1)
    }

    fmt.Println("Archived bundle at", archive_path)
    transferArchive(archive_path, archive_url)

    os.Exit(0)
  }

  if action == "download" || action == "down" {
    if fileExists(bundle_path) {
      fmt.Println("Bundle path already exists")
      os.Exit(0)
    }

    /* Download archive from S3 */
    fmt.Println("Downloading from S3:", archive_url)
    transferArchive(archive_url, archive_path)

    /* Extract */
    fmt.Println("Extracting to:", path)
    extractArchive(archive_path, path)
  }

  printUsage()
}
<commit_msg>Fix archive transfer, remove useless comments<commit_after>package main

import(
  "fmt"
  "io"
  "io/ioutil"
  "crypto/sha1"
  "os"
  "os/exec"
  "path/filepath"
  "bytes"
  "runtime"
  "strings"
  "github.com/kr/s3/s3util"
)

func fileExists(path string) bool {
  _, err := os.Stat(path)
  return err == nil
}

func open(s string) (io.ReadCloser, error) {
  if isURL(s) {
    return s3util.Open(s, nil)
  }
  return os.Open(s)
}

func create(s string) (io.WriteCloser, error) {
  if isURL(s) {
    return s3util.Create(s, nil, nil)
  }
  return os.Create(s)
}

func isURL(s string) bool {
  return strings.HasPrefix(s, "http://") || strings.HasPrefix(s, "https://")
}

func s3url(filename string) string {
  format := "https://s3.amazonaws.com/%s/%s"
  url := fmt.Sprintf(format, os.Getenv("S3_BUCKET"), filename)

  return url
}

func sh(command string) (string, error) {
  var output bytes.Buffer

  cmd := exec.Command("bash", "-c", command)

  cmd.Stdout = &output
  cmd.Stderr = &output

  err := cmd.Run()
  return output.String(), err
}

func calculateChecksum(buffer string) string {
  h := sha1.New()
  io.WriteString(h, buffer)
  return fmt.Sprintf("%x", h.Sum(nil))
}

func transferArchive(file string, url string) {
  s3util.DefaultConfig.AccessKey = os.Getenv("S3_ACCESS_KEY")
  s3util.DefaultConfig.SecretKey = os.Getenv("S3_SECRET_KEY")

  r, err := open(file)
  if err != nil {
    fmt.Fprintln(os.Stderr, err)
    os.Exit(1)
  }

  w, err := create(url)
  if err != nil {
    fmt.Fprintln(os.Stderr, err)
    os.Exit(1)
  }

  _, err = io.Copy(w, r)
  if err != nil {
    fmt.Fprintln(os.Stderr, err)
    os.Exit(1)
  }

  err = w.Close()
  if err != nil {
    fmt.Fprintln(os.Stderr, err)
    os.Exit(1)
  }
}

func extractArchive(filename string, path string) bool {
  cmd_mkdir   := fmt.Sprintf("cd %s && mkdir .bundle", path)
  cmd_move    := fmt.Sprintf("mv %s %s/.bundle/bundle_cache.tar.gz", filename, path)
  cmd_extract := fmt.Sprintf("cd %s/.bundle && tar -xzf ./bundle_cache.tar.gz", path)
  cmd_remove  := fmt.Sprintf("rm %s/.bundle/bundle_cache.tar.gz", path)

  if _, err := sh(cmd_mkdir) ; err != nil {
    fmt.Println("Bundle directory '.bundle' already exists")
    return false
  }

  if _, err := sh(cmd_move) ; err != nil {
    fmt.Println("Unable to move file")
    return false
  }

  if out, err := sh(cmd_extract) ; err != nil {
    fmt.Println("Unable to extract:", out)
    return false
  }

  if _, err := sh(cmd_remove) ; err != nil {
    fmt.Println("Unable to remove archive")
    return false
  }

  return true
}

func envDefined(name string) bool {
  result := os.Getenv(name)
  return len(result) > 0
}

func checkS3Credentials() {
  required := [3]string { "S3_ACCESS_KEY", "S3_SECRET_KEY", "S3_BUCKET" }

  for _, v := range required {
    if !envDefined(v) {
      fmt.Printf("Please define %s environment variable\n", v)
      os.Exit(2)
    }
  }
}

func printUsage() {
  fmt.Println("Usage: bundle_cache [download|upload]")
  os.Exit(2)
}

func main() {
  args := os.Args[1:]

  if len(args) != 1 {
    printUsage()
  }

  action := args[0]

  /* Check if S3 credentials are set */
  checkS3Credentials()

  /* Get all path information */
  path, _       := os.Getwd()
  name          := filepath.Base(path)
  bundle_path   := fmt.Sprintf("%s/.bundle", path)
  lockfile_path := fmt.Sprintf("%s/Gemfile.lock", path)

  /* Check if lockfile exists */
  if !fileExists(lockfile_path) {
    fmt.Println("Gemfile.lock does not exist")
    os.Exit(1)
  }

  /* Read contents of lockfile */
  lockfile, err := ioutil.ReadFile(lockfile_path)
  if err != nil {
    fmt.Println("Unable to read Gemfile.lock")
    os.Exit(1)
  }

  /* Calculate SHA1 checksum for Gemfile.lock */
  checksum := calculateChecksum(string(lockfile))

  /* Make archive save filename */
  archive_name := fmt.Sprintf("%s_%s_%s.tar.gz", name, checksum, runtime.GOARCH)
  archive_path := fmt.Sprintf("/tmp/%s", archive_name)
  archive_url  := fmt.Sprintf("https://s3.amazonaws.com/bundle_cache/%s", archive_name)

  /* Check if archive already exists */
  if fileExists(archive_path) {
    if os.Remove(archive_path) != nil {
      fmt.Println("Failed to remove existing archive")
      os.Exit(1)
    }
  }

  if action == "upload" || action == "up" {
    if !fileExists(bundle_path) {
      fmt.Println("Bundle path does not exist")
      os.Exit(1)
    }

    cmd := fmt.Sprintf("cd %s && tar -czf %s .", bundle_path, archive_path)

    if out, err := sh(cmd); err != nil {
      fmt.Println("Failed to make archive:", out)
      os.Exit(1)
    }

    fmt.Println("Archived bundle at", archive_path)
    transferArchive(archive_path, archive_url)

    os.Exit(0)
  }

  if action == "download" || action == "down" {
    if fileExists(bundle_path) {
      fmt.Println("Bundle path already exists")
      os.Exit(0)
    }

    /* Download archive from S3 */
    fmt.Println("Downloading from S3:", archive_url)
    transferArchive(archive_url, archive_path)

    /* Extract */
    fmt.Println("Extracting to:", path)
    extractArchive(archive_path, path)
  }

  printUsage()
}
<|endoftext|>


<commit_before>package sobjects

// Base struct that contains fields that all objects, standard and custom, include.
type BaseSObject struct {
	Attributes       SObjectAttributes `json:"-" force:"attributes,omitempty"`
	Id               string            `force:",omitempty"`
	IsDeleted        bool              `force:",omitempty"`
	Name             string            `force:",omitempty"`
	CreatedDate      string            `force:",omitempty"`
	CreatedById      string            `force:",omitempty"`
	LastModifiedDate string            `force:",omitempty"`
	LastModifiedById string            `force:",omitempty"`
	SystemModstamp   string            `force:",omitempty"`
}

type SObjectAttributes struct {
	Type string `force:"type,omitempty"`
	Url  string `force:"url,omitempty"`
}

// Implementing this here because most object don't have an external id and as such this is not needed.
func (b BaseSObject) ExternalIdApiName() string {
	return ""
}

// Fields that are returned in every query response. Use this to build custom structs.
// type MyCustomQueryResponse struct {
// 	BaseQuery
// 	Records []sobjects.Account `json:"records" force:"records"`
// }
type BaseQuery struct {
	Done           bool    `json:"Done" force:"done"`
	TotalSize      float64 `json:"TotalSize" force:"totalSize"`
	NextRecordsUri string  `json:"NextRecordsUrl" force:"nextRecordsUrl"`
}

type Time time.Time

func (t *Time) UnmarshalJSON(data []byte) (err error) {
	// Fractional seconds are handled implicitly by Parse.
	*t, err = Parse(`"2006-01-02T15:04:05.000-0700"`, string(data))
	return
}

func (t Time) MarshalJSON() ([]byte, error) {
	if y := t.Year(); y < 0 || y >= 10000 {
		return nil, errors.New("Time.MarshalJSON: year outside of range [0,9999]")
	}
	return []byte(t.Format(`"2006-01-02T15:04:05.000-0700"`)), nil
}
<commit_msg>revert base sobject. Added time parser by accident, not ready for prime time. Hoping Go 1.2 will help resolve some issues with json parsing.<commit_after>package sobjects

// Base struct that contains fields that all objects, standard and custom, include.
type BaseSObject struct {
	Attributes       SObjectAttributes `json:"-" force:"attributes,omitempty"`
	Id               string            `force:",omitempty"`
	IsDeleted        bool              `force:",omitempty"`
	Name             string            `force:",omitempty"`
	CreatedDate      string            `force:",omitempty"`
	CreatedById      string            `force:",omitempty"`
	LastModifiedDate string            `force:",omitempty"`
	LastModifiedById string            `force:",omitempty"`
	SystemModstamp   string            `force:",omitempty"`
}

type SObjectAttributes struct {
	Type string `force:"type,omitempty"`
	Url  string `force:"url,omitempty"`
}

// Implementing this here because most object don't have an external id and as such this is not needed.
func (b BaseSObject) ExternalIdApiName() string {
	return ""
}

// Fields that are returned in every query response. Use this to build custom structs.
// type MyCustomQueryResponse struct {
// 	BaseQuery
// 	Records []sobjects.Account `json:"records" force:"records"`
// }
type BaseQuery struct {
	Done           bool    `json:"Done" force:"done"`
	TotalSize      float64 `json:"TotalSize" force:"totalSize"`
	NextRecordsUri string  `json:"NextRecordsUrl" force:"nextRecordsUrl"`
}
<|endoftext|>


<commit_before>// package name: riffle
package main

import (
	"C"
	"unsafe"

	"github.com/exis-io/core"
	"github.com/exis-io/core/goRiffle"
)

// Required main method
func main() {}

// By default always connect to the production fabric at node.exis.io
var fabric string = core.FabricProduction

//export CBID
func CBID() uint {
	return core.NewID()
}

//export NewDomain
func NewDomain(name *C.char) unsafe.Pointer {
	d := core.NewDomain(C.GoString(name), nil)
	return unsafe.Pointer(&d)
}

//export Subdomain
func Subdomain(pdomain unsafe.Pointer, name *C.char) unsafe.Pointer {
	d := *(*core.Domain)(pdomain)
	n := d.Subdomain(C.GoString(name))
	return unsafe.Pointer(&n)
}

//export Receive
func Receive(pdomain unsafe.Pointer) []byte {
	d := *(*core.Domain)(pdomain)
	return []byte(core.MantleMarshall(d.GetApp().CallbackListen()))
}

//export Join
func Join(pdomain unsafe.Pointer, cb uint, eb uint) {
	d := *(*core.Domain)(pdomain)

	if c, err := goRiffle.Open(fabric); err != nil {
		d.GetApp().CallbackSend(eb, err.Error())
	} else {
		if err := d.Join(c); err != nil {
			d.GetApp().CallbackSend(eb, err.Error())
		} else {
			d.GetApp().CallbackSend(cb)
		}
	}
}

//export Subscribe
func Subscribe(pdomain unsafe.Pointer, cb uint, endpoint *C.char) {
	d := *(*core.Domain)(pdomain)
	go func() {
		d.Subscribe(C.GoString(endpoint), cb, make([]interface{}, 0))
	}()
}

//export Register
func Register(pdomain unsafe.Pointer, cb uint, endpoint *C.char) {
	d := *(*core.Domain)(pdomain)
	go func() {
		d.Register(C.GoString(endpoint), cb, make([]interface{}, 0))
	}()
}

//export Publish
func Publish(pdomain unsafe.Pointer, cb uint, endpoint *C.char, args *C.char) {
	d := *(*core.Domain)(pdomain)
	go func() {
		d.Publish(C.GoString(endpoint), cb, core.MantleUnmarshal(C.GoString(args)))
	}()
}

//export Call
func Call(pdomain unsafe.Pointer, cb uint, endpoint *C.char, args *C.char) {
	d := *(*core.Domain)(pdomain)
	go func() {
		d.Call(C.GoString(endpoint), cb, core.MantleUnmarshal(C.GoString(args)))
	}()
}

//export Yield
func Yield(pdomain unsafe.Pointer, request uint, args *C.char) {
	d := *(*core.Domain)(pdomain)
	go func() {
		d.GetApp().Yield(request, core.MantleUnmarshal(C.GoString(args)))
	}()
}

//export Unsubscribe
func Unsubscribe(pdomain unsafe.Pointer, endpoint *C.char) {
	d := *(*core.Domain)(pdomain)
	go func() {
		d.Unsubscribe(C.GoString(endpoint))
	}()
}

//export Unregister
func Unregister(pdomain unsafe.Pointer, endpoint *C.char) {
	d := *(*core.Domain)(pdomain)
	go func() {
		d.Unregister(C.GoString(endpoint))
	}()
}

//export Leave
func Leave(pdomain unsafe.Pointer) {
	d := *(*core.Domain)(pdomain)
	go func() {
		d.Leave()
	}()
}

//export SetLogLevelOff
func SetLogLevelOff() { core.LogLevel = core.LogLevelOff }

//export SetLogLevelApp
func SetLogLevelApp() { core.LogLevel = core.LogLevelApp }

//export SetLogLevelErr
func SetLogLevelErr() { core.LogLevel = core.LogLevelErr }

//export SetLogLevelWarn
func SetLogLevelWarn() { core.LogLevel = core.LogLevelWarn }

//export SetLogLevelInfo
func SetLogLevelInfo() { core.LogLevel = core.LogLevelInfo }

//export SetLogLevelDebug
func SetLogLevelDebug() { core.LogLevel = core.LogLevelDebug }

//export SetFabricDev
func SetFabricDev() { fabric = core.FabricDev }

//export SetFabricSandbox
func SetFabricSandbox() { fabric = core.FabricSandbox }

//export SetFabricProduction
func SetFabricProduction() { fabric = core.FabricProduction }

//export SetFabricLocal
func SetFabricLocal() { fabric = core.FabricLocal }

//export MantleSetFabric
func MantleSetFabric(url *C.char) { fabric = C.GoString(url) }

//export Application
func Application(s string) { core.Application("%s", s) }

//export Debug
func Debug(s string) { core.Debug("%s", s) }

//export Info
func Info(s string) { core.Info("%s", s) }

//export Warn
func Warn(s string) { core.Warn("%s", s) }

//export Error
func Error(s string) { core.Error("%s", s) }
<commit_msg>publish with arguments<commit_after>// package name: riffle
package main

import (
	"C"
	"unsafe"

	"github.com/exis-io/core"
	"github.com/exis-io/core/goRiffle"
)

// Required main method
func main() {}

// By default always connect to the production fabric at node.exis.io
var fabric string = core.FabricProduction

//export CBID
func CBID() uint {
	return core.NewID()
}

//export NewDomain
func NewDomain(name *C.char) unsafe.Pointer {
	d := core.NewDomain(C.GoString(name), nil)
	return unsafe.Pointer(&d)
}

//export Subdomain
func Subdomain(pdomain unsafe.Pointer, name *C.char) unsafe.Pointer {
	d := *(*core.Domain)(pdomain)
	n := d.Subdomain(C.GoString(name))
	return unsafe.Pointer(&n)
}

//export Receive
func Receive(pdomain unsafe.Pointer) []byte {
	d := *(*core.Domain)(pdomain)
	return []byte(core.MantleMarshall(d.GetApp().CallbackListen()))
}

//export Join
func Join(pdomain unsafe.Pointer, cb uint, eb uint) {
	d := *(*core.Domain)(pdomain)

	if c, err := goRiffle.Open(fabric); err != nil {
		d.GetApp().CallbackSend(eb, err.Error())
	} else {
		if err := d.Join(c); err != nil {
			d.GetApp().CallbackSend(eb, err.Error())
		} else {
			d.GetApp().CallbackSend(cb)
		}
	}
}

//export Subscribe
func Subscribe(pdomain unsafe.Pointer, cb uint, endpoint *C.char) {
	d := *(*core.Domain)(pdomain)
	go func() {
		d.Subscribe(C.GoString(endpoint), cb, make([]interface{}, 0))
	}()
}

//export Register
func Register(pdomain unsafe.Pointer, cb uint, endpoint *C.char) {
	d := *(*core.Domain)(pdomain)
	go func() {
		d.Register(C.GoString(endpoint), cb, make([]interface{}, 0))
	}()
}

//export Publish
func Publish(pdomain unsafe.Pointer, cb uint, endpoint *C.char, args *C.char) {
	d := *(*core.Domain)(pdomain)
    a := C.GoString(args)
    s := core.MantleUnmarshal(a)
    core.Debug("String: %s, Unmarshalled: %s", a, s)
	go func() {
		d.Publish(C.GoString(endpoint), cb, s)
	}()
}

//export Call
func Call(pdomain unsafe.Pointer, cb uint, endpoint *C.char, args *C.char) {
	d := *(*core.Domain)(pdomain)
	go func() {
		d.Call(C.GoString(endpoint), cb, core.MantleUnmarshal(C.GoString(args)))
	}()
}

//export Yield
func Yield(pdomain unsafe.Pointer, request uint, args *C.char) {
	d := *(*core.Domain)(pdomain)
	go func() {
		d.GetApp().Yield(request, core.MantleUnmarshal(C.GoString(args)))
	}()
}

//export Unsubscribe
func Unsubscribe(pdomain unsafe.Pointer, endpoint *C.char) {
	d := *(*core.Domain)(pdomain)
	go func() {
		d.Unsubscribe(C.GoString(endpoint))
	}()
}

//export Unregister
func Unregister(pdomain unsafe.Pointer, endpoint *C.char) {
	d := *(*core.Domain)(pdomain)
	go func() {
		d.Unregister(C.GoString(endpoint))
	}()
}

//export Leave
func Leave(pdomain unsafe.Pointer) {
	d := *(*core.Domain)(pdomain)
	go func() {
		d.Leave()
	}()
}

//export SetLogLevelOff
func SetLogLevelOff() { core.LogLevel = core.LogLevelOff }

//export SetLogLevelApp
func SetLogLevelApp() { core.LogLevel = core.LogLevelApp }

//export SetLogLevelErr
func SetLogLevelErr() { core.LogLevel = core.LogLevelErr }

//export SetLogLevelWarn
func SetLogLevelWarn() { core.LogLevel = core.LogLevelWarn }

//export SetLogLevelInfo
func SetLogLevelInfo() { core.LogLevel = core.LogLevelInfo }

//export SetLogLevelDebug
func SetLogLevelDebug() { core.LogLevel = core.LogLevelDebug }

//export SetFabricDev
func SetFabricDev() { fabric = core.FabricDev }

//export SetFabricSandbox
func SetFabricSandbox() { fabric = core.FabricSandbox }

//export SetFabricProduction
func SetFabricProduction() { fabric = core.FabricProduction }

//export SetFabricLocal
func SetFabricLocal() { fabric = core.FabricLocal }

//export MantleSetFabric
func MantleSetFabric(url *C.char) { fabric = C.GoString(url) }

//export Application
func Application(s string) { core.Application("%s", s) }

//export Debug
func Debug(s string) { core.Debug("%s", s) }

//export Info
func Info(s string) { core.Info("%s", s) }

//export Warn
func Warn(s string) { core.Warn("%s", s) }

//export Error
func Error(s string) { core.Error("%s", s) }
<|endoftext|>


<commit_before>/* Copyright 2015 Simon Schmidt */

/*
 Package camellia is an implementation of the CAMELLIA encryption algorithm,
 based on a Go port of the OpenSSL C code.
 Like the OpenSSL code, it available under a permissive non-GPL-License.

 IMPORTANT: This library is not subject to the Apache license.

 ALSO IMPORTANT: The translation basically works, but it is not guaranteed, that it doesnt violate the Spec.
 */
package camellia

import "errors"

var WrongKeySizeError = errors.New("goencrypt/camellia: the keysize is != 16,24 or 32 bytes")

// This structure eases the usage of this cipher.
// It implements the cipher.Block interface
type Camellia struct{
	rounds int
	table *KEY_TABLE_TYPE
}
// Initializes the structure. Must be called before any Encrypt or Decrypt method.
func (c *Camellia) Init(key []byte) error{
	switch len(key){
	case 16,24,32:
		c.table = new(KEY_TABLE_TYPE)
		c.rounds = Camellia_Ekeygen(len(key)*8,key,c.table)
	default:
		return WrongKeySizeError
	}
	return nil
}
func (c *Camellia) Encrypt(dest, src []byte) {
	Camellia_EncryptBlock_Rounds(c.rounds,src,c.table,dest)
}
func (c *Camellia) Decrypt(dest, src []byte) {
	Camellia_DecryptBlock_Rounds(c.rounds,src,c.table,dest)
}

<commit_msg>Update api.go<commit_after>/* Copyright 2015 Simon Schmidt */

/*
 Package camellia is an implementation of the CAMELLIA encryption algorithm,
 based on a Go port of the OpenSSL C code.
 Like the OpenSSL code, it available under a permissive non-GPL-License.

 IMPORTANT: This library is not subject to the Apache license.

 ALSO IMPORTANT: The translation basically works, but it is not guaranteed, that it doesnt violate the Spec.
 */
package camellia

import "errors"

var WrongKeySizeError = errors.New("goencrypt/camellia: the keysize is != 16,24 or 32 bytes")

// This structure eases the usage of this cipher.
// It implements the cipher.Block interface
type Camellia struct{
	rounds int
	table *KEY_TABLE_TYPE
}
// Initializes the structure. Must be called before any Encrypt or Decrypt method.
// Key length should be one of 16,24 or 32.
func (c *Camellia) Init(key []byte) error{
	switch len(key){
	case 16,24,32:
		c.table = new(KEY_TABLE_TYPE)
		c.rounds = Camellia_Ekeygen(len(key)*8,key,c.table)
	default:
		return WrongKeySizeError
	}
	return nil
}
func (c *Camellia) Encrypt(dest, src []byte) {
	Camellia_EncryptBlock_Rounds(c.rounds,src,c.table,dest)
}
func (c *Camellia) Decrypt(dest, src []byte) {
	Camellia_DecryptBlock_Rounds(c.rounds,src,c.table,dest)
}

<|endoftext|>