<commit_before>//go:build linux && cgo && !agent
// +build linux,cgo,!agent

package db

import (
	"database/sql"
	"fmt"

	"github.com/pkg/errors"

	"github.com/lxc/lxd/lxd/db/query"
	"github.com/lxc/lxd/shared"
	"github.com/lxc/lxd/shared/api"
)

// Code generation directives.
//
//go:generate -command mapper lxd-generate db mapper -t projects.mapper.go
//go:generate mapper reset
//
//go:generate mapper stmt -e project names
//go:generate mapper stmt -e project names-by-Name
//go:generate mapper stmt -e project objects
//go:generate mapper stmt -e project objects-by-Name
//go:generate mapper stmt -e project used-by-ref
//go:generate mapper stmt -e project used-by-ref-by-Name
//go:generate mapper stmt -e project config-ref
//go:generate mapper stmt -e project config-ref-by-Name
//go:generate mapper stmt -e project create
//go:generate mapper stmt -e project create-config-ref
//go:generate mapper stmt -e project id
//go:generate mapper stmt -e project rename
//go:generate mapper stmt -e project update
//go:generate mapper stmt -e project delete
//
//go:generate mapper method -e project URIs
//go:generate mapper method -e project List
//go:generate mapper method -e project Get
//go:generate mapper method -e project ConfigRef
//go:generate mapper method -e project Exists
//go:generate mapper method -e project Create
//go:generate mapper method -e project UsedByRef
//go:generate mapper method -e project ID
//go:generate mapper method -e project Rename
//go:generate mapper method -e project Delete

// ProjectFilter can be used to filter results yielded by ProjectList.
type ProjectFilter struct {
	Name string // If non-empty, return only the project with this name.
}

// ProjectHasProfiles is a helper to check if a project has the profiles
// feature enabled.
func (c *ClusterTx) ProjectHasProfiles(name string) (bool, error) {
	return projectHasProfiles(c.tx, name)
}

// GetProjectNames returns the names of all available projects.
func (c *ClusterTx) GetProjectNames() ([]string, error) {
	stmt := "SELECT name FROM projects"

	names, err := query.SelectStrings(c.tx, stmt)
	if err != nil {
		return nil, errors.Wrap(err, "Fetch project names")
	}

	return names, nil
}

// GetProjectIDsToNames returns a map associating each project ID to its
// project name.
func (c *ClusterTx) GetProjectIDsToNames() (map[int64]string, error) {
	stmt := "SELECT id, name FROM projects"

	rows, err := c.tx.Query(stmt)
	if err != nil {
		return nil, err
	}
	defer rows.Close()

	result := map[int64]string{}
	for i := 0; rows.Next(); i++ {
		var id int64
		var name string

		err := rows.Scan(&id, &name)
		if err != nil {
			return nil, err
		}

		result[id] = name
	}

	err = rows.Err()
	if err != nil {
		return nil, err
	}

	return result, nil
}

func projectHasProfiles(tx *sql.Tx, name string) (bool, error) {
	stmt := `
SELECT projects_config.value
  FROM projects_config
  JOIN projects ON projects.id=projects_config.project_id
 WHERE projects.name=? AND projects_config.key='features.profiles'
`
	values, err := query.SelectStrings(tx, stmt, name)
	if err != nil {
		return false, errors.Wrap(err, "Fetch project config")
	}

	if len(values) == 0 {
		return false, nil
	}

	return shared.IsTrue(values[0]), nil
}

// ProjectHasImages is a helper to check if a project has the images
// feature enabled.
func (c *ClusterTx) ProjectHasImages(name string) (bool, error) {
	project, err := c.GetProject(name)
	if err != nil {
		return false, errors.Wrap(err, "fetch project")
	}

	enabled := shared.IsTrue(project.Config["features.images"])

	return enabled, nil
}

// UpdateProject updates the project matching the given key parameters.
func (c *ClusterTx) UpdateProject(name string, object api.ProjectPut) error {
	id, err := c.GetProjectID(name)
	if err != nil {
		return errors.Wrap(err, "Fetch project ID")
	}

	stmt := c.stmt(projectUpdate)
	result, err := stmt.Exec(object.Description, id)
	if err != nil {
		return errors.Wrap(err, "Update project")
	}

	n, err := result.RowsAffected()
	if err != nil {
		return errors.Wrap(err, "Fetch affected rows")
	}
	if n != 1 {
		return fmt.Errorf("Query updated %d rows instead of 1", n)
	}

	// Clear config.
	_, err = c.tx.Exec(`
DELETE FROM projects_config WHERE projects_config.project_id = ?
`, id)
	if err != nil {
		return errors.Wrap(err, "Delete project config")
	}

	// Insert new config.
	stmt = c.stmt(projectCreateConfigRef)
	for key, value := range object.Config {
		if value == "" {
			continue
		}

		_, err := stmt.Exec(id, key, value)
		if err != nil {
			return errors.Wrap(err, "Insert config for project")
		}
	}

	return nil
}

// InitProjectWithoutImages updates populates the images_profiles table with
// all images from the default project when a project is created with
// features.images=false.
func (c *ClusterTx) InitProjectWithoutImages(project string) error {
	defaultProfileID, err := c.GetProfileID(project, "default")
	if err != nil {
		return errors.Wrap(err, "Fetch project ID")
	}
	stmt := `INSERT INTO images_profiles (image_id, profile_id)
	SELECT images.id, ? FROM images WHERE project_id=1`
	_, err = c.tx.Exec(stmt, defaultProfileID)
	return err
}

// GetProject returns the project with the given key.
func (c *Cluster) GetProject(projectName string) (*api.Project, error) {
	var err error
	var p *api.Project
	err = c.Transaction(func(tx *ClusterTx) error {
		p, err = tx.GetProject(projectName)
		if err != nil {
			return err
		}

		return nil
	})
	if err != nil {
		return nil, err
	}

	return p, nil
}
<commit_msg>lxd/db/projects: add new entries for code generation<commit_after>//go:build linux && cgo && !agent
// +build linux,cgo,!agent

package db

import (
	"database/sql"
	"fmt"

	"github.com/pkg/errors"

	"github.com/lxc/lxd/lxd/db/query"
	"github.com/lxc/lxd/shared"
	"github.com/lxc/lxd/shared/api"
)

// Code generation directives.
//
//go:generate -command mapper lxd-generate db mapper -t projects.mapper.go
//go:generate mapper reset
//
//go:generate mapper stmt -e project names
//go:generate mapper stmt -e project names-by-Name
//go:generate mapper stmt -e project objects
//go:generate mapper stmt -e project objects-by-Name
//go:generate mapper stmt -e project used-by-ref
//go:generate mapper stmt -e project used-by-ref-by-Name
//go:generate mapper stmt -e project config-ref
//go:generate mapper stmt -e project config-ref-by-Name
//go:generate mapper stmt -e project create
//go:generate mapper stmt -e project create-config-ref
//go:generate mapper stmt -e project id
//go:generate mapper stmt -e project rename
//go:generate mapper stmt -e project update
//go:generate mapper stmt -e project delete-by-Name
//
//go:generate mapper method -e project URIs
//go:generate mapper method -e project List
//go:generate mapper method -e project Get
//go:generate mapper method -e project ConfigRef
//go:generate mapper method -e project Exists
//go:generate mapper method -e project Create
//go:generate mapper method -e project UsedByRef
//go:generate mapper method -e project ID
//go:generate mapper method -e project Rename
//go:generate mapper method -e project DeleteOne

// ProjectFilter specifies potential query parameter fields.
type ProjectFilter struct {
	Name string // If non-empty, return only the project with this name.
}

// ProjectHasProfiles is a helper to check if a project has the profiles
// feature enabled.
func (c *ClusterTx) ProjectHasProfiles(name string) (bool, error) {
	return projectHasProfiles(c.tx, name)
}

// GetProjectNames returns the names of all available projects.
func (c *ClusterTx) GetProjectNames() ([]string, error) {
	stmt := "SELECT name FROM projects"

	names, err := query.SelectStrings(c.tx, stmt)
	if err != nil {
		return nil, errors.Wrap(err, "Fetch project names")
	}

	return names, nil
}

// GetProjectIDsToNames returns a map associating each project ID to its
// project name.
func (c *ClusterTx) GetProjectIDsToNames() (map[int64]string, error) {
	stmt := "SELECT id, name FROM projects"

	rows, err := c.tx.Query(stmt)
	if err != nil {
		return nil, err
	}
	defer rows.Close()

	result := map[int64]string{}
	for i := 0; rows.Next(); i++ {
		var id int64
		var name string

		err := rows.Scan(&id, &name)
		if err != nil {
			return nil, err
		}

		result[id] = name
	}

	err = rows.Err()
	if err != nil {
		return nil, err
	}

	return result, nil
}

func projectHasProfiles(tx *sql.Tx, name string) (bool, error) {
	stmt := `
SELECT projects_config.value
  FROM projects_config
  JOIN projects ON projects.id=projects_config.project_id
 WHERE projects.name=? AND projects_config.key='features.profiles'
`
	values, err := query.SelectStrings(tx, stmt, name)
	if err != nil {
		return false, errors.Wrap(err, "Fetch project config")
	}

	if len(values) == 0 {
		return false, nil
	}

	return shared.IsTrue(values[0]), nil
}

// ProjectHasImages is a helper to check if a project has the images
// feature enabled.
func (c *ClusterTx) ProjectHasImages(name string) (bool, error) {
	project, err := c.GetProject(name)
	if err != nil {
		return false, errors.Wrap(err, "fetch project")
	}

	enabled := shared.IsTrue(project.Config["features.images"])

	return enabled, nil
}

// UpdateProject updates the project matching the given key parameters.
func (c *ClusterTx) UpdateProject(name string, object api.ProjectPut) error {
	id, err := c.GetProjectID(name)
	if err != nil {
		return errors.Wrap(err, "Fetch project ID")
	}

	stmt := c.stmt(projectUpdate)
	result, err := stmt.Exec(object.Description, id)
	if err != nil {
		return errors.Wrap(err, "Update project")
	}

	n, err := result.RowsAffected()
	if err != nil {
		return errors.Wrap(err, "Fetch affected rows")
	}
	if n != 1 {
		return fmt.Errorf("Query updated %d rows instead of 1", n)
	}

	// Clear config.
	_, err = c.tx.Exec(`
DELETE FROM projects_config WHERE projects_config.project_id = ?
`, id)
	if err != nil {
		return errors.Wrap(err, "Delete project config")
	}

	// Insert new config.
	stmt = c.stmt(projectCreateConfigRef)
	for key, value := range object.Config {
		if value == "" {
			continue
		}

		_, err := stmt.Exec(id, key, value)
		if err != nil {
			return errors.Wrap(err, "Insert config for project")
		}
	}

	return nil
}

// InitProjectWithoutImages updates populates the images_profiles table with
// all images from the default project when a project is created with
// features.images=false.
func (c *ClusterTx) InitProjectWithoutImages(project string) error {
	defaultProfileID, err := c.GetProfileID(project, "default")
	if err != nil {
		return errors.Wrap(err, "Fetch project ID")
	}
	stmt := `INSERT INTO images_profiles (image_id, profile_id)
	SELECT images.id, ? FROM images WHERE project_id=1`
	_, err = c.tx.Exec(stmt, defaultProfileID)
	return err
}

// GetProject returns the project with the given key.
func (c *Cluster) GetProject(projectName string) (*api.Project, error) {
	var err error
	var p *api.Project
	err = c.Transaction(func(tx *ClusterTx) error {
		p, err = tx.GetProject(projectName)
		if err != nil {
			return err
		}

		return nil
	})
	if err != nil {
		return nil, err
	}

	return p, nil
}
<|endoftext|>


<commit_before>package cl11

import (
	"fmt"
	"reflect"
	"unsafe"

	clw "github.com/rdwilliamson/clw11"
)

type Kernel struct {
	id            clw.Kernel
	Arguments     int
	FunctionName  string
	Context       *Context
	Program       *Program
	WorkGroupInfo []KernelWorkGroupInfo
	argScratch    [][scratchSize]byte
}

type KernelWorkGroupInfo struct {
	Device                         *Device
	WorkGroupSize                  int
	CompileWorkGroupSize           [3]int
	LocalMemSize                   int
	PreferredWorkGroupSizeMultiple int
	PrivateMemSize                 int
}

type LocalSpaceArg int

func (p *Program) CreateKernel(name string) (*Kernel, error) {

	kernel, err := clw.CreateKernel(p.id, name)
	if err != nil {
		return nil, err
	}

	k := &Kernel{id: kernel, FunctionName: name, Context: p.Context, Program: p,
		WorkGroupInfo: make([]KernelWorkGroupInfo, len(p.Devices))}
	for i := range p.Devices {
		k.WorkGroupInfo[i].Device = p.Devices[i]
	}

	err = k.getAllInfo()
	if err != nil {
		return nil, err
	}

	return k, nil
}

func (k *Kernel) getAllInfo() (err error) {
	defer func() {
		if r := recover(); r != nil {
			err = r.(error)
		}
	}()

	k.Arguments = k.getUint(clw.KernelNumArgs)
	k.argScratch = make([][8]byte, k.Arguments)

	for i := range k.WorkGroupInfo {
		wgi := &k.WorkGroupInfo[i]

		wgi.WorkGroupSize = k.getWorkGroupSize(wgi.Device, clw.KernelWorkGroupSize)
		wgi.LocalMemSize = k.getWorkGroupUlong(wgi.Device, clw.KernelLocalMemSize)
		wgi.PreferredWorkGroupSizeMultiple =
			k.getWorkGroupSize(wgi.Device, clw.KernelPreferredWorkGroupSizeMultiple)
		wgi.PrivateMemSize = k.getWorkGroupUlong(wgi.Device, clw.KernelPrivateMemSize)
		wgi.CompileWorkGroupSize = k.getWorkGroupSize3(wgi.Device, clw.KernelCompileWorkGroupSize)
	}

	return
}

func (k *Kernel) getUint(paramName clw.KernelInfo) int {
	var param clw.Uint
	err := clw.GetKernelInfo(k.id, clw.KernelNumArgs, clw.Size(unsafe.Sizeof(param)), unsafe.Pointer(&param), nil)
	if err != nil {
		panic(err)
	}
	return int(param)
}

func (k *Kernel) getWorkGroupSize(d *Device, paramName clw.KernelWorkGroupInfo) int {
	var param clw.Size
	err := clw.GetKernelWorkGroupInfo(k.id, d.id, paramName, clw.Size(unsafe.Sizeof(param)), unsafe.Pointer(&param),
		nil)
	if err != nil {
		panic(err)
	}
	return int(param)
}

func (k *Kernel) getWorkGroupSize3(d *Device, paramName clw.KernelWorkGroupInfo) [3]int {
	var param [3]clw.Size
	err := clw.GetKernelWorkGroupInfo(k.id, d.id, paramName, clw.Size(unsafe.Sizeof(param)), unsafe.Pointer(&param),
		nil)
	if err != nil {
		panic(err)
	}
	return [3]int{int(param[0]), int(param[1]), int(param[2])}
}

func (k *Kernel) getWorkGroupUlong(d *Device, paramName clw.KernelWorkGroupInfo) int {
	var param clw.Ulong
	err := clw.GetKernelWorkGroupInfo(k.id, d.id, paramName, clw.Size(unsafe.Sizeof(param)), unsafe.Pointer(&param),
		nil)
	if err != nil {
		panic(err)
	}
	return int(param)
}

func (k *Kernel) SetArgument(index int, arg interface{}) error {

	var size uintptr
	var pointer unsafe.Pointer

	switch v := arg.(type) {

	case *Buffer:
		pointer = unsafe.Pointer(&v.id)
		size = unsafe.Sizeof(v.id)

	case LocalSpaceArg:
		pointer = nil
		size = uintptr(v)

	default:
		value := reflect.ValueOf(arg)
		kind := value.Kind()
		for kind == reflect.Ptr || kind == reflect.Interface {
			value = value.Elem()
			kind = value.Kind()
		}

		pointer = unsafe.Pointer(&k.argScratch[index][0])

		switch kind {

		case reflect.Bool:

			localCopy := reflect.NewAt(int32Type, pointer).Elem()
			if value.Bool() {
				localCopy.SetInt(1)
			} else {
				localCopy.SetInt(0)
			}

			size = int32Size

		case reflect.Int:

			localCopy := reflect.NewAt(int32Type, pointer).Elem()
			localCopy.SetInt(value.Int())

			size = int32Size

		case reflect.Uint:

			localCopy := reflect.NewAt(uint32Type, pointer).Elem()
			localCopy.SetUint(value.Uint())

			size = uint32Size

		case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint8, reflect.Uint16, reflect.Uint32,
			reflect.Uint64, reflect.Float32, reflect.Float64:

			localType := value.Type()
			localCopy := reflect.NewAt(localType, pointer).Elem()
			localCopy.Set(value)

			size = localType.Size()

		default:
			return wrapError(fmt.Errorf("invaild argument kind: %s", kind.String()))
		}
	}

	return clw.SetKernelArg(k.id, clw.Uint(index), clw.Size(size), pointer)
}

func (k *Kernel) SetArguments(args ...interface{}) error {

	if len(args) != k.Arguments {
		return wrapError(fmt.Errorf("invalid number of arguments: expecting %d, got %d", k.Arguments, len(args)))
	}

	for i := range args {
		err := k.SetArgument(i, args[i])
		if err != nil {
			return wrapError(fmt.Errorf("setting argument %d: %s", i, err.Error()))
		}
	}

	return nil
}

func (cq *CommandQueue) EnqueueNDRangeKernel(k *Kernel, globalOffset, globalSize, localSize []int,
	waitList []*Event, e *Event) error {

	var event *clw.Event
	if e != nil {
		event = &e.id
		e.Context = cq.Context
		e.CommandType = CommandNDRangeKernel
		e.CommandQueue = cq
	}

	dims := len(globalSize)
	sizes := make([]clw.Size, dims*3)
	if globalOffset != nil {
		for i := 0; i < dims; i++ {
			sizes[i] = clw.Size(globalOffset[i])
		}
	}
	for i := 0; i < dims; i++ {
		sizes[dims+i] = clw.Size(globalSize[i])
		sizes[2*dims+i] = clw.Size(localSize[i])
	}

	return clw.EnqueueNDRangeKernel(cq.id, k.id, sizes[:dims], sizes[dims:2*dims], sizes[2*dims:],
		cq.toEvents(waitList), event)
}
<commit_msg>Use the defined values for true and false.<commit_after>package cl11

import (
	"fmt"
	"reflect"
	"unsafe"

	clw "github.com/rdwilliamson/clw11"
)

type Kernel struct {
	id            clw.Kernel
	Arguments     int
	FunctionName  string
	Context       *Context
	Program       *Program
	WorkGroupInfo []KernelWorkGroupInfo
	argScratch    [][scratchSize]byte
}

type KernelWorkGroupInfo struct {
	Device                         *Device
	WorkGroupSize                  int
	CompileWorkGroupSize           [3]int
	LocalMemSize                   int
	PreferredWorkGroupSizeMultiple int
	PrivateMemSize                 int
}

type LocalSpaceArg int

func (p *Program) CreateKernel(name string) (*Kernel, error) {

	kernel, err := clw.CreateKernel(p.id, name)
	if err != nil {
		return nil, err
	}

	k := &Kernel{id: kernel, FunctionName: name, Context: p.Context, Program: p,
		WorkGroupInfo: make([]KernelWorkGroupInfo, len(p.Devices))}
	for i := range p.Devices {
		k.WorkGroupInfo[i].Device = p.Devices[i]
	}

	err = k.getAllInfo()
	if err != nil {
		return nil, err
	}

	return k, nil
}

func (k *Kernel) getAllInfo() (err error) {
	defer func() {
		if r := recover(); r != nil {
			err = r.(error)
		}
	}()

	k.Arguments = k.getUint(clw.KernelNumArgs)
	k.argScratch = make([][8]byte, k.Arguments)

	for i := range k.WorkGroupInfo {
		wgi := &k.WorkGroupInfo[i]

		wgi.WorkGroupSize = k.getWorkGroupSize(wgi.Device, clw.KernelWorkGroupSize)
		wgi.LocalMemSize = k.getWorkGroupUlong(wgi.Device, clw.KernelLocalMemSize)
		wgi.PreferredWorkGroupSizeMultiple =
			k.getWorkGroupSize(wgi.Device, clw.KernelPreferredWorkGroupSizeMultiple)
		wgi.PrivateMemSize = k.getWorkGroupUlong(wgi.Device, clw.KernelPrivateMemSize)
		wgi.CompileWorkGroupSize = k.getWorkGroupSize3(wgi.Device, clw.KernelCompileWorkGroupSize)
	}

	return
}

func (k *Kernel) getUint(paramName clw.KernelInfo) int {
	var param clw.Uint
	err := clw.GetKernelInfo(k.id, clw.KernelNumArgs, clw.Size(unsafe.Sizeof(param)), unsafe.Pointer(&param), nil)
	if err != nil {
		panic(err)
	}
	return int(param)
}

func (k *Kernel) getWorkGroupSize(d *Device, paramName clw.KernelWorkGroupInfo) int {
	var param clw.Size
	err := clw.GetKernelWorkGroupInfo(k.id, d.id, paramName, clw.Size(unsafe.Sizeof(param)), unsafe.Pointer(&param),
		nil)
	if err != nil {
		panic(err)
	}
	return int(param)
}

func (k *Kernel) getWorkGroupSize3(d *Device, paramName clw.KernelWorkGroupInfo) [3]int {
	var param [3]clw.Size
	err := clw.GetKernelWorkGroupInfo(k.id, d.id, paramName, clw.Size(unsafe.Sizeof(param)), unsafe.Pointer(&param),
		nil)
	if err != nil {
		panic(err)
	}
	return [3]int{int(param[0]), int(param[1]), int(param[2])}
}

func (k *Kernel) getWorkGroupUlong(d *Device, paramName clw.KernelWorkGroupInfo) int {
	var param clw.Ulong
	err := clw.GetKernelWorkGroupInfo(k.id, d.id, paramName, clw.Size(unsafe.Sizeof(param)), unsafe.Pointer(&param),
		nil)
	if err != nil {
		panic(err)
	}
	return int(param)
}

func (k *Kernel) SetArgument(index int, arg interface{}) error {

	var size uintptr
	var pointer unsafe.Pointer

	switch v := arg.(type) {

	case *Buffer:
		pointer = unsafe.Pointer(&v.id)
		size = unsafe.Sizeof(v.id)

	case LocalSpaceArg:
		pointer = nil
		size = uintptr(v)

	default:
		value := reflect.ValueOf(arg)
		kind := value.Kind()
		for kind == reflect.Ptr || kind == reflect.Interface {
			value = value.Elem()
			kind = value.Kind()
		}

		pointer = unsafe.Pointer(&k.argScratch[index][0])

		switch kind {

		case reflect.Bool:

			localCopy := reflect.NewAt(int32Type, pointer).Elem()
			if value.Bool() {
				localCopy.SetInt(int64(clw.True))
			} else {
				localCopy.SetInt(int64(clw.False))
			}

			size = int32Size

		case reflect.Int:

			localCopy := reflect.NewAt(int32Type, pointer).Elem()
			localCopy.SetInt(value.Int())

			size = int32Size

		case reflect.Uint:

			localCopy := reflect.NewAt(uint32Type, pointer).Elem()
			localCopy.SetUint(value.Uint())

			size = uint32Size

		case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint8, reflect.Uint16, reflect.Uint32,
			reflect.Uint64, reflect.Float32, reflect.Float64:

			localType := value.Type()
			localCopy := reflect.NewAt(localType, pointer).Elem()
			localCopy.Set(value)

			size = localType.Size()

		default:
			return wrapError(fmt.Errorf("invaild argument kind: %s", kind.String()))
		}
	}

	return clw.SetKernelArg(k.id, clw.Uint(index), clw.Size(size), pointer)
}

func (k *Kernel) SetArguments(args ...interface{}) error {

	if len(args) != k.Arguments {
		return wrapError(fmt.Errorf("invalid number of arguments: expecting %d, got %d", k.Arguments, len(args)))
	}

	for i := range args {
		err := k.SetArgument(i, args[i])
		if err != nil {
			return wrapError(fmt.Errorf("setting argument %d: %s", i, err.Error()))
		}
	}

	return nil
}

func (cq *CommandQueue) EnqueueNDRangeKernel(k *Kernel, globalOffset, globalSize, localSize []int,
	waitList []*Event, e *Event) error {

	var event *clw.Event
	if e != nil {
		event = &e.id
		e.Context = cq.Context
		e.CommandType = CommandNDRangeKernel
		e.CommandQueue = cq
	}

	dims := len(globalSize)
	sizes := make([]clw.Size, dims*3)
	if globalOffset != nil {
		for i := 0; i < dims; i++ {
			sizes[i] = clw.Size(globalOffset[i])
		}
	}
	for i := 0; i < dims; i++ {
		sizes[dims+i] = clw.Size(globalSize[i])
		sizes[2*dims+i] = clw.Size(localSize[i])
	}

	return clw.EnqueueNDRangeKernel(cq.id, k.id, sizes[:dims], sizes[dims:2*dims], sizes[2*dims:],
		cq.toEvents(waitList), event)
}
<|endoftext|>


<commit_before>package main

import (
	"bufio"
	"container/ring"
	"fmt"
	"os"
	"os/exec"
	"strings"
	"time"

	"github.com/mattn/go-runewidth"
	"github.com/nsf/termbox-go"
)

// BufferLineCount is the number of lines of buffer to keep in memory from logcat.
const BufferLineCount = 1000

// devices is the list of devices that we currently know about.
var devices []*Device

// deviceIndex the index into devices that we're currently displaying.
var deviceIndex int

// Device is all the stuff we know about a single attached device.
type Device struct {
	// ID is the identfiied of the device, that you'd pass to adb's "-s" parameter
	ID string

	// Name is the display name of the device, that we show in the UI.
	Name string

	logcat *ring.Ring

	waiting bool
	ping    chan int
}

func (d *Device) appendLine(line string) {
	d.logcat = d.logcat.Prev()
	d.logcat.Value = line

	if d.waiting {
		d.ping <- 1
	}
}

// Open opens a connection to the given device via an adb command. Basically we start streaming
// logcat output to the device's AbdContext.
func (d *Device) Open() {
	d.logcat = ring.New(BufferLineCount)
	d.ping = make(chan int)
	d.waiting = false

	cmd := exec.Command("adb", "-s", d.ID, "logcat", "-v", "threadtime")
	stdout, err := cmd.StdoutPipe()
	if err != nil {
		panic("An error occurred reading output: " + err.Error())
	}
	scanner := bufio.NewScanner(stdout)
	go func() {
		lastTime := time.Now()
		for scanner.Scan() {
			if !d.waiting {
				thisTime := time.Now()
				if thisTime.UnixNano()-lastTime.UnixNano() > 1000000000 {
					// More than a second passed, we can start notifying listeners of new updates
					d.waiting = true
				}
				lastTime = thisTime
			}
			d.appendLine(scanner.Text())
		}
		if err := scanner.Err(); err != nil {
			panic("An error occurred reading output: " + err.Error())
		}
	}()
	err = cmd.Start()
	if err != nil {
		panic("Error starting adb logcat: " + err.Error())
	}
}

func tbprint(x, y int, fg, bg termbox.Attribute, msg string) int {
	n := 0
	for _, c := range msg {
		termbox.SetCell(x, y, c, fg, bg)
		width := runewidth.RuneWidth(c)
		x += width
		n += width
	}
	return n
}

func render() {
	coldef := termbox.ColorDefault
	termbox.Clear(coldef, coldef)
	w, h := termbox.Size()

	// Top line, device list
	x := 0
	coldef = termbox.ColorDefault | termbox.AttrReverse
	for _, d := range devices {
		x += tbprint(x, 0, coldef, coldef, "［")
		coldef = termbox.ColorDefault
		x += tbprint(x, 0, coldef, coldef, d.Name)
		coldef = termbox.ColorDefault | termbox.AttrReverse
		x += tbprint(x, 0, coldef, coldef, "］")
	}
	for ; x < w; x++ {
		termbox.SetCell(x, 0, ' ', coldef, coldef)
	}

	// Start from bottom and write up
	if len(devices) > deviceIndex {
		logcat := devices[deviceIndex].logcat
		coldef = termbox.ColorDefault
		for y := h - 3; y >= 1; y-- {
			if logcat.Value == nil {
				break
			}
			tbprint(0, y, coldef, coldef, logcat.Value.(string))
			logcat = logcat.Next()
		}
	}

	// Second from bottom line, filter.
	// TODO: the first tab ("no filter") should have no filter line
	y := h - 2
	termbox.SetCursor(1, y)

	// Last line, tabs, one tab per configured filter
	x = 0
	y = h - 1
	// TODO: for tabs:
	coldef = termbox.ColorDefault
	x += tbprint(x, y, coldef, coldef, "［")
	coldef = termbox.ColorDefault | termbox.AttrReverse
	x += tbprint(x, y, coldef, coldef, "no filter")
	coldef = termbox.ColorDefault
	x += tbprint(x, y, coldef, coldef, "］")
	x += tbprint(x, y, coldef, coldef, "［+filter］")
	for ; x < w; x++ {
		termbox.SetCell(x, y, ' ', coldef, coldef)
	}

	termbox.Flush()
}

// refreshDevices refreshes the list of attached devices (by running 'adb devices' basically).
func refreshDevices() {
	cmd := exec.Command("adb", "devices", "-l")
	stdout, err := cmd.StdoutPipe()
	if err != nil {
		panic("'adb devices' error: " + err.Error())
	}

	scanner := bufio.NewScanner(stdout)
	err = cmd.Start()
	if err != nil {
		panic("'adb devices' error: " + err.Error())
	}

	for scanner.Scan() {
		line := scanner.Text()
		parts := strings.Fields(line)
		if len(parts) < 2 || parts[1] != "device" {
			fmt.Fprintf(os.Stderr, "Not a device line: '%s'", line)
			continue
		}

		id := parts[0]
		name := id
		for i := 2; i < len(parts); i++ {
			kvp := strings.Split(parts[i], ":")
			if len(kvp) == 2 && kvp[0] == "model" {
				name = kvp[1]
			}
		}

		d := &Device{
			ID:   id,
			Name: strings.Replace(name, "_", " ", -1),
		}
		d.Open()
		devices = append(devices, d)
	}
	if err := scanner.Err(); err != nil {
		panic("An error occurred reading output: " + err.Error())
	}
}

func main() {
	err := termbox.Init()
	if err != nil {
		panic(err)
	}
	defer termbox.Close()
	termbox.SetInputMode(termbox.InputEsc)

	refreshDevices()
	render()

	events := make(chan termbox.Event)
	go func() {
		for {
			events <- termbox.PollEvent()
		}
	}()

mainloop:
	for {
		select {
		case ev := <-events:
			if ev.Key == termbox.KeyEsc {
				break mainloop
			}
			render()

		case <-devices[deviceIndex].ping:
			render()
		}
	}
}
<commit_msg>Added a custom 'buffer' for keeping the log lines.<commit_after>package main

import (
	"bufio"
	"fmt"
	"os"
	"os/exec"
	"strings"
	"sync"
	"time"

	"github.com/mattn/go-runewidth"
	"github.com/nsf/termbox-go"
)

// BufferLineCount is the number of lines of buffer to keep in memory from logcat.
const BufferLineCount = 1000

// devices is the list of devices that we currently know about.
var devices []*Device

// deviceIndex the index into devices that we're currently displaying.
var deviceIndex int

// LogBuffer represents a fixed-size buffer of log lines. The lines are indexed with 0 being the
// oldest line and N being the most recent log line. Older logs will be expired, but the index
// will never be reused (this means a LogView can reference expired lines without having to
// update the views every time a line is expired).
type LogBuffer struct {
	lines []string

	// nextLineIndex is the index into the lines[] slice that the *next* log line will go.
	nextLineIndex int

	// lineNo is the line number (starting from 1) that the *most recent* log line has. This number
	// increments for every line that's added to the buffer, whereas nextLineIndex wraps around as
	// the buffer fills up.
	lineNo int64
}

// LogView is a "view" over a device's logs. There's a special view that represents all logs, and
// then there is zero or more LogView's for filtered results.
type LogView struct {
}

// Device is all the stuff we know about a single attached device.
type Device struct {
	// ID is the identfiied of the device, that you'd pass to adb's "-s" parameter
	ID string

	// Name is the display name of the device, that we show in the UI.
	Name string

	logBuffer *LogBuffer

	// mutex is used to synchronize access to the log buffer.
	mutex *sync.Mutex

	waiting bool
	ping    chan int
}

func (d *Device) appendLine(line string) {
	d.mutex.Lock()
	d.logBuffer.lines[d.logBuffer.nextLineIndex] = line
	d.logBuffer.lineNo++
	d.logBuffer.nextLineIndex++
	if d.logBuffer.nextLineIndex >= len(d.logBuffer.lines) {
		d.logBuffer.nextLineIndex = 0
	}
	d.mutex.Unlock()

	if d.waiting {
		d.ping <- 1
	}
}

// NewDevice creates a new instance of Device for the device with the given ID and name.
func NewDevice(id, name string) *Device {
	return &Device{
		ID:   id,
		Name: name,
		logBuffer: &LogBuffer{
			lines:         make([]string, BufferLineCount),
			nextLineIndex: 0,
			lineNo:        0,
		},
		mutex:   &sync.Mutex{},
		ping:    make(chan int),
		waiting: false,
	}
}

// Open opens a connection to the given device via an adb command. Basically we start streaming
// logcat output to the device's AbdContext.
func (d *Device) Open() {
	cmd := exec.Command("adb", "-s", d.ID, "logcat", "-v", "threadtime")
	stdout, err := cmd.StdoutPipe()
	if err != nil {
		panic("An error occurred reading output: " + err.Error())
	}
	scanner := bufio.NewScanner(stdout)
	go func() {
		lastTime := time.Now()
		for scanner.Scan() {
			if !d.waiting {
				thisTime := time.Now()
				if thisTime.UnixNano()-lastTime.UnixNano() > 500000000 {
					// More than 1/2 second passed, we can start notifying listeners of new updates
					d.waiting = true
				}
				lastTime = thisTime
			}
			d.appendLine(scanner.Text())
		}
		if err := scanner.Err(); err != nil {
			panic("An error occurred reading output: " + err.Error())
		}
	}()
	err = cmd.Start()
	if err != nil {
		panic("Error starting adb logcat: " + err.Error())
	}
}

// GetLastLineNo returns the index of the last line in the log buffer.
// You should only call this method when you've got the device's mutex locked.
func (lb *LogBuffer) GetLastLineNo() int64 {
	return lb.lineNo
}

// GetLines returns a slice of the lines from the given line number to the given line number.
// You should only call this method when you've got the device's mutex locked.
func (lb *LogBuffer) GetLines(from, to int64) []string {
	if from < 0 {
		from = 0
	}
	if from == to {
		return make([]string, 0)
	}

	// TODO: can we keep these in a buffer to avoid allocating the new array each time?
	res := make([]string, int(to-from))
	i := 0
	for lineNo := to; lineNo > from; lineNo-- {
		index := lb.nextLineIndex - int(lb.lineNo-lineNo) - 1
		if index < 0 {
			index += len(lb.lines)
		}
		if index < 0 {
			break
		}
		res[i] = lb.lines[index]
		i++
	}
	return res
}

func tbprint(x, y int, fg, bg termbox.Attribute, msg string) int {
	n := 0
	for _, c := range msg {
		termbox.SetCell(x, y, c, fg, bg)
		width := runewidth.RuneWidth(c)
		x += width
		n += width
	}
	return n
}

func render() {
	coldef := termbox.ColorDefault
	termbox.Clear(coldef, coldef)
	w, h := termbox.Size()

	// Top line, device list
	x := 0
	coldef = termbox.ColorDefault | termbox.AttrReverse
	for _, d := range devices {
		x += tbprint(x, 0, coldef, coldef, "［")
		coldef = termbox.ColorDefault
		x += tbprint(x, 0, coldef, coldef, d.Name)
		coldef = termbox.ColorDefault | termbox.AttrReverse
		x += tbprint(x, 0, coldef, coldef, "］")
	}
	for ; x < w; x++ {
		termbox.SetCell(x, 0, ' ', coldef, coldef)
	}

	// Start from bottom and write up
	if len(devices) > deviceIndex {
		logBuffer := devices[deviceIndex].logBuffer
		devices[deviceIndex].mutex.Lock()
		lastLineNo := logBuffer.GetLastLineNo()
		firstLineNo := lastLineNo - int64(h) - 3
		lines := logBuffer.GetLines(firstLineNo, lastLineNo)
		devices[deviceIndex].mutex.Unlock()

		coldef = termbox.ColorDefault
		for i := 0; i < len(lines); i++ {
			y := h - 2 - i
			tbprint(0, y, coldef, coldef, lines[i])
		}
	}

	// Second from bottom line, filter.
	// TODO: the first tab ("no filter") should have no filter line
	y := h - 2
	termbox.SetCursor(1, y)

	// Last line, tabs, one tab per configured filter
	x = 0
	y = h - 1
	// TODO: for tabs:
	coldef = termbox.ColorDefault
	x += tbprint(x, y, coldef, coldef, "［")
	coldef = termbox.ColorDefault | termbox.AttrReverse
	x += tbprint(x, y, coldef, coldef, "no filter")
	coldef = termbox.ColorDefault
	x += tbprint(x, y, coldef, coldef, "］")
	x += tbprint(x, y, coldef, coldef, "［+filter］")
	for ; x < w; x++ {
		termbox.SetCell(x, y, ' ', coldef, coldef)
	}

	termbox.Flush()
}

// refreshDevices refreshes the list of attached devices (by running 'adb devices' basically).
func refreshDevices() {
	cmd := exec.Command("adb", "devices", "-l")
	stdout, err := cmd.StdoutPipe()
	if err != nil {
		panic("'adb devices' error: " + err.Error())
	}

	scanner := bufio.NewScanner(stdout)
	err = cmd.Start()
	if err != nil {
		panic("'adb devices' error: " + err.Error())
	}

	for scanner.Scan() {
		line := scanner.Text()
		parts := strings.Fields(line)
		if len(parts) < 2 || parts[1] != "device" {
			fmt.Fprintf(os.Stderr, "Not a device line: '%s'\n", line)
			continue
		}

		id := parts[0]
		name := id
		for i := 2; i < len(parts); i++ {
			kvp := strings.Split(parts[i], ":")
			if len(kvp) == 2 && kvp[0] == "model" {
				name = kvp[1]
			}
		}

		d := NewDevice(id, strings.Replace(name, "_", " ", -1))
		d.Open()
		devices = append(devices, d)
	}
	if err := scanner.Err(); err != nil {
		panic("An error occurred reading output: " + err.Error())
	}
}

func main() {
	err := termbox.Init()
	if err != nil {
		panic(err)
	}
	defer termbox.Close()
	termbox.SetInputMode(termbox.InputEsc)

	refreshDevices()
	render()

	events := make(chan termbox.Event)
	go func() {
		for {
			events <- termbox.PollEvent()
		}
	}()

mainloop:
	for {
		select {
		case ev := <-events:
			if ev.Key == termbox.KeyEsc {
				break mainloop
			}
			render()

		case <-devices[deviceIndex].ping:
			render()
		}
	}
}
<|endoftext|>


<commit_before>package libkb

import (
	"crypto/rsa"
	"fmt"
	"github.com/keybase/go-jsonw"
	"github.com/keybase/go-triplesec"
	"golang.org/x/crypto/openpgp"
	"golang.org/x/crypto/openpgp/errors"
	"golang.org/x/crypto/openpgp/packet"
)

//
// General Strategy:
//
//   1. Use AGL's NewEntity() with a keybase UID to generate a new
//      key.  Or maybe go with a 2-subkey configuration/
//   2. Triplesec the result and write to ~/.config/keybase/keys.3s
//   3. Sign a signature link.
//   4. Upload public and signature to server (optionally upload private)
//   5. Change local DB and rewrite user to reflect what we just uploaded
//

//
// Todos:
//    A. Verify that the PW is correct (with server?) and/or allow other key
//       passwords to be used on the key
//    3. Signatures
//    4. Upload
//    5. Local DB changed to reflect the upload
//    B. Custom usernames
//    C. Custom Email Addresses
//

//
// Password strategy:
//   1. If server push, 3sec with login password.
//   2. If local write, prompt for new password
//
//

// NewEntity returns an Entity that contains a fresh RSA/RSA keypair with a
// single identity composed of the given full name, comment and email, any of
// which may be empty but must not contain any of "()<>\x00".
// If config is nil, sensible defaults will be used.
//
// Modification of: https://code.google.com/p/go/source/browse/openpgp/keys.go?repo=crypto&r=8fec09c61d5d66f460d227fd1df3473d7e015bc6#456
//  From golang.com/x/crypto/openpgp/keys.go
func NewPgpKeyBundle(arg KeyGenArg) (*PgpKeyBundle, error) {
	currentTime := arg.Config.Now()

	uid := arg.Id.ToPgpUserId()

	if uid == nil {
		return nil, errors.InvalidArgumentError("UserId field was nil")
	}

	if arg.PrimaryBits == 0 {
		arg.PrimaryBits = 4096
	}
	if arg.SubkeyBits == 0 {
		arg.SubkeyBits = 4096
	}

	G.Log.Info("Generating primary key (%d bits)", arg.PrimaryBits)
	masterPriv, err := rsa.GenerateKey(arg.Config.Random(), arg.PrimaryBits)
	if err != nil {
		return nil, err
	}

	G.Log.Info("Generating encryption subkey (%d bits)", arg.SubkeyBits)
	encryptingPriv, err := rsa.GenerateKey(arg.Config.Random(), arg.SubkeyBits)
	if err != nil {
		return nil, err
	}
	G.Log.Info("Generating signing subkey (%d bits)", arg.SubkeyBits)
	signingPriv, err := rsa.GenerateKey(arg.Config.Random(), arg.SubkeyBits)
	if err != nil {
		return nil, err
	}

	e := &openpgp.Entity{
		PrimaryKey: packet.NewRSAPublicKey(currentTime, &masterPriv.PublicKey),
		PrivateKey: packet.NewRSAPrivateKey(currentTime, masterPriv),
		Identities: make(map[string]*openpgp.Identity),
	}
	isPrimaryId := true
	e.Identities[uid.Id] = &openpgp.Identity{
		Name:   uid.Name,
		UserId: uid,
		SelfSignature: &packet.Signature{
			CreationTime: currentTime,
			SigType:      packet.SigTypePositiveCert,
			PubKeyAlgo:   packet.PubKeyAlgoRSA,
			Hash:         arg.Config.Hash(),
			IsPrimaryId:  &isPrimaryId,
			FlagsValid:   true,
			FlagSign:     true,
			FlagCertify:  true,
			IssuerKeyId:  &e.PrimaryKey.KeyId,
		},
	}

	e.Subkeys = make([]openpgp.Subkey, 2)
	e.Subkeys[0] = openpgp.Subkey{
		PublicKey:  packet.NewRSAPublicKey(currentTime, &encryptingPriv.PublicKey),
		PrivateKey: packet.NewRSAPrivateKey(currentTime, encryptingPriv),
		Sig: &packet.Signature{
			CreationTime:              currentTime,
			SigType:                   packet.SigTypeSubkeyBinding,
			PubKeyAlgo:                packet.PubKeyAlgoRSA,
			Hash:                      arg.Config.Hash(),
			FlagsValid:                true,
			FlagEncryptStorage:        true,
			FlagEncryptCommunications: true,
			IssuerKeyId:               &e.PrimaryKey.KeyId,
		},
	}
	e.Subkeys[0].PublicKey.IsSubkey = true
	e.Subkeys[0].PrivateKey.IsSubkey = true

	e.Subkeys[1] = openpgp.Subkey{
		PublicKey:  packet.NewRSAPublicKey(currentTime, &signingPriv.PublicKey),
		PrivateKey: packet.NewRSAPrivateKey(currentTime, signingPriv),
		Sig: &packet.Signature{
			CreationTime: currentTime,
			SigType:      packet.SigTypeSubkeyBinding,
			PubKeyAlgo:   packet.PubKeyAlgoRSA,
			Hash:         arg.Config.Hash(),
			FlagsValid:   true,
			FlagSign:     true,
			IssuerKeyId:  &e.PrimaryKey.KeyId,
		},
	}
	e.Subkeys[1].PublicKey.IsSubkey = true
	e.Subkeys[1].PrivateKey.IsSubkey = true

	return (*PgpKeyBundle)(e), nil
}

type keyGenState struct {
	me       *User
	bundle   *PgpKeyBundle
	p3skb    *P3SKB
	tsec     *triplesec.Cipher
	httpArgs *HttpArgs
}

func (s *keyGenState) CheckNoKey() error {
	fp, err := s.me.GetActivePgpFingerprint()
	if err == nil && fp != nil {
		err = KeyExistsError{fp}
	}
	return err
}

func (s *keyGenState) LoadMe() (err error) {
	s.me, err = LoadMe(LoadUserArg{PublicKeyOptional: true})
	return err
}

func (s *keyGenState) GenerateKey(arg KeyGenArg) (err error) {
	if arg.Id == nil {
		arg.Id = KeybaseIdentity("")
	}
	s.bundle, err = NewPgpKeyBundle(arg)
	return
}

func (s *keyGenState) WriteKey() (err error) {
	if s.p3skb, err = s.bundle.ToP3SKB(s.tsec); err != nil {
	} else if G.Keyrings == nil {
		err = fmt.Errorf("No keyrings available")
	} else if err = G.Keyrings.P3SKB.Push(s.p3skb); err != nil {
	} else if err = G.Keyrings.P3SKB.Save(); err != nil {
	}
	return
}

func (s *keyGenState) GeneratePost() (err error) {
	var jw *jsonw.Wrapper
	var tmp []byte
	var seckey, pubkey string
	var sig string
	var sigid *SigId

	if jw, err = s.me.SelfProof(); err != nil {
		return
	}
	if tmp, err = jw.Marshal(); err != nil {
		return
	}
	if sig, sigid, err = SimpleSign(tmp, *s.bundle); err != nil {
		return
	}
	if pubkey, err = s.bundle.Encode(); err != nil {
		return
	}
	if seckey, err = s.p3skb.ArmoredEncode(); err != nil {
		return
	}

	s.httpArgs = &HttpArgs{
		"sig_id_base":  S{sigid.ToString(false)},
		"sig_id_short": S{sigid.ToShortId()},
		"sig":          S{sig},
		"public_key":   S{pubkey},
		"secret_key":   S{seckey},
		"is_primary":   I{1},
	}

	return
}

func (s *keyGenState) PostToServer() error {
	_, err := G.API.Post(ApiArg{
		Endpoint:    "key/add",
		NeedSession: true,
		Args:        *s.httpArgs,
	})
	return err
}

type KeyGenArg struct {
	PrimaryBits  int
	SubkeyBits   int
	Id           *Identity
	Config       *packet.Config
	DoPush       bool
	NoPassphrase bool
}

func (s *keyGenState) UpdateUser() error {
	err := s.me.SetActiveKey(s.bundle)
	fp := s.bundle.GetFingerprint()
	G.Env.GetConfigWriter().SetPgpFingerprint(&fp)
	return err
}

func (s *keyGenState) ReloadMe() (err error) {
	s.me, err = LoadMe(LoadUserArg{ForceReload: true})
	return
}

func KeyGen(arg KeyGenArg) (ret *PgpKeyBundle, err error) {
	state := keyGenState{}

	G.Log.Debug("+ KeyGen")
	defer func() {
		G.Log.Debug("- Keygen: %s", ErrToOk(err))
	}()

	larg := LoginArg{}

	// If we're not using a PW, we have to check that we have the right
	// password loaded into our triplesec, so need to force a relogin
	if arg.DoPush {
		larg.Force = true
		larg.Retry = 4
	}

	if err = G.LoginState.Login(larg); err != nil {
		return
	}

	if arg.DoPush {
		state.tsec = G.LoginState.tsec
	} else if !arg.NoPassphrase {
		state.tsec, err = PromptForNewTsec(PromptArg{
			TerminalPrompt: "A good passphrase to protect your key",
			PinentryDesc:   "Please pick a good passphrase to protect your key (12+ characters)",
			PinentryPrompt: "Key passphrase",
		})
		if err != nil {
			return
		}
	}

	G.Log.Debug("| Load me")
	if err = state.LoadMe(); err != nil {
		return
	}
	G.Log.Debug("| CheckNoKey")
	if err = state.CheckNoKey(); err != nil {
		return
	}
	G.Log.Debug("| GenerateKey")
	if err = state.GenerateKey(arg); err != nil {
		return
	}
	G.Log.Debug("| WriteKey")
	if err = state.WriteKey(); err != nil {
		return
	}
	G.Log.Debug("| UpdateUser")
	if err = state.UpdateUser(); err != nil {
		return
	}
	G.Log.Debug("| Generate HTTP Post")
	if err = state.GeneratePost(); err != nil {
		return
	}
	G.Log.Debug("| Post to server")
	if err = state.PostToServer(); err != nil {
		return
	}
	G.Log.Debug("| Reload user")
	if err = state.ReloadMe(); err != nil {

	}
	return
}
<commit_msg>more features on keygen<commit_after>package libkb

import (
	"crypto/rsa"
	"fmt"
	"github.com/keybase/go-jsonw"
	"github.com/keybase/go-triplesec"
	"golang.org/x/crypto/openpgp"
	"golang.org/x/crypto/openpgp/errors"
	"golang.org/x/crypto/openpgp/packet"
)

//
// General Strategy:
//
//   1. Use AGL's NewEntity() with a keybase UID to generate a new
//      key.  Or maybe go with a 2-subkey configuration/
//   2. Triplesec the result and write to ~/.config/keybase/keys.3s
//   3. Sign a signature link.
//   4. Upload public and signature to server (optionally upload private)
//   5. Change local DB and rewrite user to reflect what we just uploaded
//

//
// Todos:
//    A. Verify that the PW is correct (with server?) and/or allow other key
//       passwords to be used on the key
//    3. Signatures
//    4. Upload
//    5. Local DB changed to reflect the upload
//    B. Custom usernames
//    C. Custom Email Addresses
//

//
// Password strategy:
//   1. If server push, 3sec with login password.
//   2. If local write, prompt for new password
//
//

// NewEntity returns an Entity that contains a fresh RSA/RSA keypair with a
// single identity composed of the given full name, comment and email, any of
// which may be empty but must not contain any of "()<>\x00".
// If config is nil, sensible defaults will be used.
//
// Modification of: https://code.google.com/p/go/source/browse/openpgp/keys.go?repo=crypto&r=8fec09c61d5d66f460d227fd1df3473d7e015bc6#456
//  From golang.com/x/crypto/openpgp/keys.go
func NewPgpKeyBundle(arg KeyGenArg) (*PgpKeyBundle, error) {
	currentTime := arg.Config.Now()

	uid := arg.Id.ToPgpUserId()

	if uid == nil {
		return nil, errors.InvalidArgumentError("UserId field was nil")
	}

	if arg.PrimaryBits == 0 {
		arg.PrimaryBits = 4096
	}
	if arg.SubkeyBits == 0 {
		arg.SubkeyBits = 4096
	}

	G.Log.Info("Generating primary key (%d bits)", arg.PrimaryBits)
	masterPriv, err := rsa.GenerateKey(arg.Config.Random(), arg.PrimaryBits)
	if err != nil {
		return nil, err
	}

	G.Log.Info("Generating encryption subkey (%d bits)", arg.SubkeyBits)
	encryptingPriv, err := rsa.GenerateKey(arg.Config.Random(), arg.SubkeyBits)
	if err != nil {
		return nil, err
	}
	G.Log.Info("Generating signing subkey (%d bits)", arg.SubkeyBits)
	signingPriv, err := rsa.GenerateKey(arg.Config.Random(), arg.SubkeyBits)
	if err != nil {
		return nil, err
	}

	e := &openpgp.Entity{
		PrimaryKey: packet.NewRSAPublicKey(currentTime, &masterPriv.PublicKey),
		PrivateKey: packet.NewRSAPrivateKey(currentTime, masterPriv),
		Identities: make(map[string]*openpgp.Identity),
	}
	isPrimaryId := true
	e.Identities[uid.Id] = &openpgp.Identity{
		Name:   uid.Name,
		UserId: uid,
		SelfSignature: &packet.Signature{
			CreationTime: currentTime,
			SigType:      packet.SigTypePositiveCert,
			PubKeyAlgo:   packet.PubKeyAlgoRSA,
			Hash:         arg.Config.Hash(),
			IsPrimaryId:  &isPrimaryId,
			FlagsValid:   true,
			FlagSign:     true,
			FlagCertify:  true,
			IssuerKeyId:  &e.PrimaryKey.KeyId,
		},
	}

	e.Subkeys = make([]openpgp.Subkey, 2)
	e.Subkeys[0] = openpgp.Subkey{
		PublicKey:  packet.NewRSAPublicKey(currentTime, &encryptingPriv.PublicKey),
		PrivateKey: packet.NewRSAPrivateKey(currentTime, encryptingPriv),
		Sig: &packet.Signature{
			CreationTime:              currentTime,
			SigType:                   packet.SigTypeSubkeyBinding,
			PubKeyAlgo:                packet.PubKeyAlgoRSA,
			Hash:                      arg.Config.Hash(),
			FlagsValid:                true,
			FlagEncryptStorage:        true,
			FlagEncryptCommunications: true,
			IssuerKeyId:               &e.PrimaryKey.KeyId,
		},
	}
	e.Subkeys[0].PublicKey.IsSubkey = true
	e.Subkeys[0].PrivateKey.IsSubkey = true

	e.Subkeys[1] = openpgp.Subkey{
		PublicKey:  packet.NewRSAPublicKey(currentTime, &signingPriv.PublicKey),
		PrivateKey: packet.NewRSAPrivateKey(currentTime, signingPriv),
		Sig: &packet.Signature{
			CreationTime: currentTime,
			SigType:      packet.SigTypeSubkeyBinding,
			PubKeyAlgo:   packet.PubKeyAlgoRSA,
			Hash:         arg.Config.Hash(),
			FlagsValid:   true,
			FlagSign:     true,
			IssuerKeyId:  &e.PrimaryKey.KeyId,
		},
	}
	e.Subkeys[1].PublicKey.IsSubkey = true
	e.Subkeys[1].PrivateKey.IsSubkey = true

	return (*PgpKeyBundle)(e), nil
}

type keyGenState struct {
	me       *User
	bundle   *PgpKeyBundle
	p3skb    *P3SKB
	tsec     *triplesec.Cipher
	httpArgs *HttpArgs
}

func (s *keyGenState) CheckNoKey() error {
	fp, err := s.me.GetActivePgpFingerprint()
	if err == nil && fp != nil {
		err = KeyExistsError{fp}
	}
	return err
}

func (s *keyGenState) LoadMe() (err error) {
	s.me, err = LoadMe(LoadUserArg{PublicKeyOptional: true})
	return err
}

func (s *keyGenState) GenerateKey(arg KeyGenArg) (err error) {
	if arg.Id == nil {
		arg.Id = KeybaseIdentity("")
	}
	s.bundle, err = NewPgpKeyBundle(arg)
	return
}

func (s *keyGenState) WriteKey() (err error) {
	if s.p3skb, err = s.bundle.ToP3SKB(s.tsec); err != nil {
	} else if G.Keyrings == nil {
		err = fmt.Errorf("No keyrings available")
	} else if err = G.Keyrings.P3SKB.Push(s.p3skb); err != nil {
	} else if err = G.Keyrings.P3SKB.Save(); err != nil {
	}
	return
}

func (s *keyGenState) GeneratePost(doSecret bool) (err error) {
	var jw *jsonw.Wrapper
	var tmp []byte
	var seckey, pubkey string
	var sig string
	var sigid *SigId

	if jw, err = s.me.SelfProof(); err != nil {
		return
	}
	if tmp, err = jw.Marshal(); err != nil {
		return
	}
	if sig, sigid, err = SimpleSign(tmp, *s.bundle); err != nil {
		return
	}
	if pubkey, err = s.bundle.Encode(); err != nil {
		return
	}
	if seckey, err = s.p3skb.ArmoredEncode(); err != nil {
		return
	}

	s.httpArgs = &HttpArgs{
		"sig_id_base":  S{sigid.ToString(false)},
		"sig_id_short": S{sigid.ToShortId()},
		"sig":          S{sig},
		"public_key":   S{pubkey},
		"is_primary":   I{1},
	}
	if doSecret {
		s.httpArgs.Add("secret_key", S{seckey})
	}
	return
}

func (s *keyGenState) PostToServer() error {
	_, err := G.API.Post(ApiArg{
		Endpoint:    "key/add",
		NeedSession: true,
		Args:        *s.httpArgs,
	})
	return err
}

type KeyGenArg struct {
	PrimaryBits  int
	SubkeyBits   int
	Id           *Identity
	Config       *packet.Config
	DoPush       bool
	DoSecretPush bool
	NoPassphrase bool
	KbPassphrase bool
}

func (s *keyGenState) UpdateUser() error {
	err := s.me.SetActiveKey(s.bundle)
	fp := s.bundle.GetFingerprint()
	G.Env.GetConfigWriter().SetPgpFingerprint(&fp)
	return err
}

func (s *keyGenState) ReloadMe() (err error) {
	s.me, err = LoadMe(LoadUserArg{ForceReload: true})
	return
}

func KeyGen(arg KeyGenArg) (ret *PgpKeyBundle, err error) {
	state := keyGenState{}

	G.Log.Debug("+ KeyGen")
	defer func() {
		G.Log.Debug("- Keygen: %s", ErrToOk(err))
	}()

	larg := LoginArg{}

	// If we're not using a PW, we have to check that we have the right
	// password loaded into our triplesec, so need to force a relogin
	if arg.KbPassphrase {
		larg.Force = true
		larg.Retry = 4
	}

	if arg.KbPassphrase || arg.DoPush {
		if err = G.LoginState.Login(larg); err != nil {
			return
		}
	}

	if arg.KbPassphrase {
		state.tsec = G.LoginState.tsec
	} else if !arg.NoPassphrase {
		state.tsec, err = PromptForNewTsec(PromptArg{
			TerminalPrompt: "A good passphrase to protect your key",
			PinentryDesc:   "Please pick a good passphrase to protect your key (12+ characters)",
			PinentryPrompt: "Key passphrase",
		})
		if err != nil {
			return
		}
	}

	G.Log.Debug("| Load me")
	if err = state.LoadMe(); err != nil {
		return
	}
	G.Log.Debug("| CheckNoKey")
	if err = state.CheckNoKey(); err != nil {
		return
	}
	G.Log.Debug("| GenerateKey")
	if err = state.GenerateKey(arg); err != nil {
		return
	}
	G.Log.Debug("| WriteKey")
	if err = state.WriteKey(); err != nil {
		return
	}

	if arg.DoPush {
		err = state.doPush(arg.DoSecretPush)
	}

	return
}

func (state *keyGenState) doPush(doSecret bool) (err error) {
	G.Log.Debug("+ doPush")
	defer func() {
		G.Log.Debug("- doPush -> %s", ErrToOk(err))
	}()

	G.Log.Debug("| UpdateUser")
	if err = state.UpdateUser(); err != nil {
		return
	}
	G.Log.Debug("| Generate HTTP Post")
	if err = state.GeneratePost(doSecret); err != nil {
		return
	}
	G.Log.Debug("| Post to server")
	if err = state.PostToServer(); err != nil {
		return
	}
	G.Log.Debug("| Reload user")
	if err = state.ReloadMe(); err != nil {

	}
	return
}
<|endoftext|>


<commit_before>package main

import(
  "fmt"
  "os"
)

const(
  LUNCHY_VERSION = "0.1.0"
)

func printUsage() {
  fmt.Printf("Lunchy %s, the friendly launchctl wrapper\n", LUNCHY_VERSION)
  fmt.Println("Usage: lunchy [start|stop|restart|list|status|install|show|edit] [options]")
}

func main() {
  args := os.Args

  if (len(args) == 1) {
    printUsage()
    os.Exit(1)
  }
}<commit_msg>Add method to print plists<commit_after>package main

import(
  "fmt"
  "os"
  "io/ioutil"
  "path/filepath"
)

const(
  LUNCHY_VERSION = "0.1.0"
)

func printUsage() {
  fmt.Printf("Lunchy %s, the friendly launchctl wrapper\n", LUNCHY_VERSION)
  fmt.Println("Usage: lunchy [start|stop|restart|list|status|install|show|edit] [options]")
}

func findPlists(path string) []string {
  result := []string{}
  files, err := ioutil.ReadDir(path)

  if err != nil {
    return result
  }

  for _, file := range files {
    if (filepath.Ext(file.Name())) == ".plist" {
      result = append(result, file.Name())
    }
  }

  return result
}

func printList() {
  path  := fmt.Sprintf("%s/Library/LaunchAgents", os.Getenv("HOME"))
  files := findPlists(path)

  for _, file := range files {
    fmt.Println(file)
  }
}

func main() {
  args := os.Args

  if (len(args) == 1) {
    printUsage()
    os.Exit(1)
  }

  printList()
}<|endoftext|>


<commit_before>package yiigo

import (
	"fmt"
	"sync"

	"gopkg.in/gomail.v2"
)

// EMail email
type EMail struct {
	Title   string
	Subject string
	From    string
	To      []string
	Cc      []string
	Content string
	Attach  []string
}

// emailOptions email options
type emailOptions struct {
	charset     string
	encoding    gomail.Encoding
	contentType string
}

// EMailOption configures how we set up the db
type EMailOption interface {
	apply(options *emailOptions)
}

// funcEMailOption implements email option
type funcEMailOption struct {
	f func(options *emailOptions)
}

func (fo *funcEMailOption) apply(o *emailOptions) {
	fo.f(o)
}

func newFuncEMailOption(f func(options *emailOptions)) *funcEMailOption {
	return &funcEMailOption{f: f}
}

// EMailDialer email dialer
type EMailDialer struct {
	dialer *gomail.Dialer
}

// Send send an email.
func (m *EMailDialer) Send(e *EMail, options ...EMailOption) error {
	o := &emailOptions{contentType: "text/html"}

	if len(options) > 0 {
		for _, option := range options {
			option.apply(o)
		}
	}

	settings := make([]gomail.MessageSetting, 0, 2)

	if o.charset != "" {
		settings = append(settings, gomail.SetCharset(o.charset))
	}

	if o.encoding != "" {
		settings = append(settings, gomail.SetEncoding(o.encoding))
	}

	msg := gomail.NewMessage(settings...)

	msg.SetHeader("Subject", e.Subject)
	msg.SetAddressHeader("From", e.From, e.Title)
	msg.SetHeader("To", e.To...)

	if len(e.Cc) > 0 {
		msg.SetHeader("Cc", e.Cc...)
	}

	if len(e.Attach) > 0 {
		for _, v := range e.Attach {
			msg.Attach(v)
		}
	}

	msg.SetBody(o.contentType, e.Content)

	// Send the email
	err := m.dialer.DialAndSend(msg)

	return err
}

var (
	Mailer    *mailer
	mailerMap sync.Map
)

// WithEMailCharset specifies the `Charset` to email.
func WithEMailCharset(s string) EMailOption {
	return newFuncEMailOption(func(o *emailOptions) {
		o.charset = s
	})
}

// WithEMailEncoding specifies the `Encoding` to email.
func WithEMailEncoding(e gomail.Encoding) EMailOption {
	return newFuncEMailOption(func(o *emailOptions) {
		o.encoding = e
	})
}

// WithEMailContentType specifies the `ContentType` to email.
func WithEMailContentType(s string) EMailOption {
	return newFuncEMailOption(func(o *emailOptions) {
		o.contentType = s
	})
}

// RegisterMailer register a mailer
func RegisterMailer(name, host string, port int, account, password string) {
	m := &EMailDialer{dialer: gomail.NewDialer(host, port, account, password)}

	mailerMap.Store(name, m)

	if name == AsDefault {
		Mailer = m
	}
}

// UseMailer returns a mailer
func UseMailer(name string) *EMailDialer {
	v, ok := mailerMap.Load(name)

	if !ok {
		panic(fmt.Errorf("yiigo: mailer.%s is not registered", name))
	}

	return v.(*EMailDialer)
}
<commit_msg>update<commit_after>package yiigo

import (
	"fmt"
	"sync"

	"gopkg.in/gomail.v2"
)

// EMail email
type EMail struct {
	Title   string
	Subject string
	From    string
	To      []string
	Cc      []string
	Content string
	Attach  []string
}

// emailOptions email options
type emailOptions struct {
	charset     string
	encoding    gomail.Encoding
	contentType string
}

// EMailOption configures how we set up the db
type EMailOption interface {
	apply(options *emailOptions)
}

// funcEMailOption implements email option
type funcEMailOption struct {
	f func(options *emailOptions)
}

func (fo *funcEMailOption) apply(o *emailOptions) {
	fo.f(o)
}

func newFuncEMailOption(f func(options *emailOptions)) *funcEMailOption {
	return &funcEMailOption{f: f}
}

// EMailDialer email dialer
type EMailDialer struct {
	dialer *gomail.Dialer
}

// Send send an email.
func (m *EMailDialer) Send(e *EMail, options ...EMailOption) error {
	o := &emailOptions{contentType: "text/html"}

	if len(options) > 0 {
		for _, option := range options {
			option.apply(o)
		}
	}

	settings := make([]gomail.MessageSetting, 0, 2)

	if o.charset != "" {
		settings = append(settings, gomail.SetCharset(o.charset))
	}

	if o.encoding != "" {
		settings = append(settings, gomail.SetEncoding(o.encoding))
	}

	msg := gomail.NewMessage(settings...)

	msg.SetHeader("Subject", e.Subject)
	msg.SetAddressHeader("From", e.From, e.Title)
	msg.SetHeader("To", e.To...)

	if len(e.Cc) > 0 {
		msg.SetHeader("Cc", e.Cc...)
	}

	if len(e.Attach) > 0 {
		for _, v := range e.Attach {
			msg.Attach(v)
		}
	}

	msg.SetBody(o.contentType, e.Content)

	// Send the email
	err := m.dialer.DialAndSend(msg)

	return err
}

var (
	Mailer    *EMailDialer
	mailerMap sync.Map
)

// WithEMailCharset specifies the `Charset` to email.
func WithEMailCharset(s string) EMailOption {
	return newFuncEMailOption(func(o *emailOptions) {
		o.charset = s
	})
}

// WithEMailEncoding specifies the `Encoding` to email.
func WithEMailEncoding(e gomail.Encoding) EMailOption {
	return newFuncEMailOption(func(o *emailOptions) {
		o.encoding = e
	})
}

// WithEMailContentType specifies the `ContentType` to email.
func WithEMailContentType(s string) EMailOption {
	return newFuncEMailOption(func(o *emailOptions) {
		o.contentType = s
	})
}

// RegisterMailer register a mailer
func RegisterMailer(name, host string, port int, account, password string) {
	m := &EMailDialer{dialer: gomail.NewDialer(host, port, account, password)}

	mailerMap.Store(name, m)

	if name == AsDefault {
		Mailer = m
	}
}

// UseMailer returns a mailer
func UseMailer(name string) *EMailDialer {
	v, ok := mailerMap.Load(name)

	if !ok {
		panic(fmt.Errorf("yiigo: mailer.%s is not registered", name))
	}

	return v.(*EMailDialer)
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"log"
	"regexp"
	"net/http"
	"flag"
	"net/url"
	"io/ioutil"
)

func fetchHtml(inputUrl string) string {
	parsedUrl, err := url.Parse(inputUrl)
	if err != nil {
		log.Fatal(err)
	}
	if !parsedUrl.IsAbs() {
		log.Fatal("Url must be absolute")
	}
	resp, err := http.Get(parsedUrl.String())
	defer resp.Body.Close()
	if err != nil {
		log.Fatalf("Failed to reach %s", parsedUrl)
	}
	if resp.StatusCode != 200 {
		log.Fatalf("Failed to reach %s (%s)", parsedUrl, resp.StatusCode)
	}
	body, err := ioutil.ReadAll(resp.Body)
	return string(body)
}

func findLinkUrls(inputHtml string, parentUrlRaw string) []string {
	parentUrl, _ := url.Parse(parentUrlRaw)

	regex := regexp.MustCompile(`src="(\S+)"`)
	submatches := regex.FindAllStringSubmatch(inputHtml, -1)

	linkUrls := make([]string, len(submatches))
	for i, v := range submatches {
		linkUrl, _ := url.Parse(v[1])
		if !linkUrl.IsAbs() {
			linkUrl = parentUrl.ResolveReference(linkUrl)
		}
		linkUrls[i] = linkUrl.String()
	}
	return linkUrls
}

func testLinkUrl(linkUrl string, c chan map[string]int) {
	resp, err := http.Get(linkUrl)
	result := make(map[string]int)
	if err != nil {
		result[linkUrl] = -1
	} else {
		result[linkUrl] = resp.StatusCode
	}
	c <- result
}

func testLinkUrls(linkUrls []string) (chan map[string]int, int) {
	c := make(chan map[string]int)
	for _, linkUrl := range linkUrls {
		go testLinkUrl(linkUrl, c)
	}
	return c, len(linkUrls)
}

func main() {
	var inputUrl string
	flag.StringVar(&inputUrl, "url", "", "Provide a valid url")
	flag.Parse()
	fmt.Println(inputUrl)

	responseHtml := fetchHtml(inputUrl)
	log.Printf("Fetched %d characters", len(responseHtml))

	linkUrls := findLinkUrls(responseHtml, inputUrl)
	c, count := testLinkUrls(linkUrls)
	for i := 0; i < count; i++ {
		result := <- c
		log.Print("result: ", result)
	}
}
<commit_msg>Working<commit_after>package main

import (
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"regexp"
)

type TestResult struct {
	Url        string
	StatusCode int
}

var colors = map[string]string{
    "header": "\033[95m",
    "blue": "\033[94m",
    "green": "\033[92m",
    "yellow": "\033[93m",
    "red": "\033[91m",
    "bold": "\033[1m",
    "end": "\033[0m",
}

func printColor(s string, color string) {
	formatted := fmt.Sprintf("%s%s%s", colors[color], s, colors["end"])
	fmt.Println(formatted)
}

func fetchHtml(inputUrl string) string {
	parsedUrl, err := url.Parse(inputUrl)
	if err != nil {
		log.Fatal(err)
	}
	if !parsedUrl.IsAbs() {
		log.Fatal("Url must be absolute")
	}

	resp, err := http.Get(parsedUrl.String())
	defer resp.Body.Close()
	if err != nil {
		log.Fatalf("Failed to reach %s", parsedUrl)
	}
	if resp.StatusCode != 200 {
		log.Fatalf("Failed to reach %s (%s)", parsedUrl, resp.StatusCode)
	}

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatalf("Failed to read response body")
	}
	return string(body)
}

func findLinkUrls(inputHtml string, parentUrlRaw string) []string {
	parentUrl, _ := url.Parse(parentUrlRaw)

	regex := regexp.MustCompile(`(?:src|href)="(\S+)"`)
	submatches := regex.FindAllStringSubmatch(inputHtml, -1)

	linkUrls := make([]string, len(submatches))
	for i, v := range submatches {
		linkUrl, _ := url.Parse(v[1])
		if !linkUrl.IsAbs() {
			linkUrl = parentUrl.ResolveReference(linkUrl)
		}
		linkUrls[i] = linkUrl.String()
	}
	return linkUrls
}

func testLinkUrl(linkUrl string, c chan TestResult) {
	resp, err := http.Get(linkUrl)
	var code int
	if err != nil {
		code = -111
	} else {
		code = resp.StatusCode
	}
	result := TestResult{linkUrl, code}
	c <- result
}

func testLinkUrls(linkUrls []string) (chan TestResult) {
	c := make(chan TestResult)
	for _, linkUrl := range linkUrls {
		go testLinkUrl(linkUrl, c)
	}
	return c
}

func main() {
	printColor("--- Linkie Time! ---", "header")
	var inputUrl string
	flag.StringVar(&inputUrl, "url", "", "Provide a valid url")
	flag.Parse()

	responseHtml := fetchHtml(inputUrl)
	printColor(inputUrl, "bold")

	linkUrls := findLinkUrls(responseHtml, inputUrl)
	c := testLinkUrls(linkUrls)
	for i := 0; i < len(linkUrls); i++ {
		result := <-c
		color := "green"
		if result.StatusCode != 200 {
			color = "red"
		}
		printColor(fmt.Sprintf("[%d] %s", result.StatusCode, result.Url), color)
	}
}
<|endoftext|>


<commit_before>package mqt

import (
	"encoding/json"
)

type MesosData struct {
	State State
}

type State struct {
	ActivatedSlaves   int64   `json:"activated_slaves"`
	BuildDate         string  `json:"build_date"`
	BuildTime         float64 `json:"build_time"`
	BuildUser         string  `json:"build_user"`
	Cluster           string  `json:"cluster"`
	DeactivatedSlaves int64   `json:"deactivated_slaves"`
	ElectedTime       float64 `json:"elected_time"`
	FailedTasks       int64   `json:"failed_tasks"`
	FinishedTasks     int64   `json:"finished_tasks"`
	GitSha            string  `json:"git_sha"`
	GitTag            string  `json:"git_tag"`
	Hostname          string  `json:"hostname"`
	Id                string  `json:"id"`
	KilledTasks       int64   `json:"killed_tasks"`
	Leader            string  `json:"leader"`
	LogDir            string  `json:"log_dir"`
	LostTasks         int64   `json:"lost_tasks"`
	StagedTasks       int64   `json:"staged_tasks"`
	StartTime         string  `json:"start_time"`
	StartedTasks      string  `json:"started_tasks"`
	Version           string  `json:"version"`
}

// Does a healthcheck on master.
// See http://<master-ip>:<port>/help/master/health
func HealthCheck() bool {
	return true
}

func decodeState(data []byte) (mState State, err error) {
	if len(data) == 0 || data == nil {
		return State{}, nil
	}
	err = json.Unmarshal(data, &mState)

	if err != nil {
		return State{}, err
	}
	return
}
<commit_msg>developing cli ui.<commit_after>
// mqt = mesos query tool
// tool to query info from mesos
// Type of information that can be queried
// - System overall
// - Master
// - Slave
// - frameworks
// - tasks
//
// # Query on partial attribute names
// $ mqt --system --attr="build*"

// # Query with attr an value filters
// $ mqt --master --attr="cluster" --val="3"

// $ mqt

package mqt

import (
	"encoding/json"
)

type MesosData struct {
	State State
}

type State struct {
	ActivatedSlaves   int64   `json:"activated_slaves"`
	BuildDate         string  `json:"build_date"`
	BuildTime         float64 `json:"build_time"`
	BuildUser         string  `json:"build_user"`
	Cluster           string  `json:"cluster"`
	DeactivatedSlaves int64   `json:"deactivated_slaves"`
	ElectedTime       float64 `json:"elected_time"`
	FailedTasks       int64   `json:"failed_tasks"`
	FinishedTasks     int64   `json:"finished_tasks"`
	GitSha            string  `json:"git_sha"`
	GitTag            string  `json:"git_tag"`
	Hostname          string  `json:"hostname"`
	Id                string  `json:"id"`
	KilledTasks       int64   `json:"killed_tasks"`
	Leader            string  `json:"leader"`
	LogDir            string  `json:"log_dir"`
	LostTasks         int64   `json:"lost_tasks"`
	StagedTasks       int64   `json:"staged_tasks"`
	StartTime         string  `json:"start_time"`
	StartedTasks      string  `json:"started_tasks"`
	Version           string  `json:"version"`
}

// Does a healthcheck on master.
// See http://<master-ip>:<port>/help/master/health
func HealthCheck() bool {
	return true
}

func Query(q string) {

}

func decodeState(data []byte) (mState State, err error) {
	if len(data) == 0 || data == nil {
		return State{}, nil
	}
	err = json.Unmarshal(data, &mState)

	if err != nil {
		return State{}, err
	}
	return
}<|endoftext|>


<commit_before>/*
 * matrix.go, part of gochem.
 *
 *
 * Copyright 2012 Raul Mera <rmera{at}chemDOThelsinkiDOTfi>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 *
 * Gochem is developed at the laboratory for instruction in Swedish, Department of Chemistry,
 * University of Helsinki, Finland.
 *
 *
 */
/***Dedicated to the long life of the Ven. Khenpo Phuntzok Tenzin Rinpoche***/

package chem

import "github.com/skelterjohn/go.matrix"
import "math"
import "fmt"

//These ones are basic math, belonging more to the go.matrix package
//If there is something similar already made
//in go.matrix this functions will be deleted. Otherwise they could be
//made methods for DenseMatrix and included in go.matrix

//Some of this functions don't return error messages because they are meant to
//Be inserted in mathematical expressions and thus they need to return only one value.

/*SetRows replaces the coordinates of atoms in the positions given by atomlist with the coordinates
in newcoords (in order) If atomlist contains a single element, it replaces as many coordinates
as given in newcoords, starting
at the element in atomlist. In the latter case, the function checks that there are enough coordinates to
replace and panics if not. */
func SetRows(target, newcoords *matrix.DenseMatrix, atomlist []int) *matrix.DenseMatrix{
	//If supplies a list with one number, the newcoords will replace the old coords
	//Starting that number. We do check that you don't put more coords than spaces we have.
	returned:=target.Copy()
	if len(atomlist)==1{
		if newcoords.Rows()>target.Rows()-atomlist[0]-1{
			panic(fmt.Sprintf("Cant replace starting from position %d: Not enough atoms in molecule", atomlist[0]))
			}
		returned.SetMatrix(atomlist[0],0,newcoords)
		return returned
		}
	//If the list has more than one atom
	lenatoms:=target.Rows()
	for k,j:=range(atomlist){
		if j>lenatoms-1{
			panic(fmt.Sprintf("Requested position number: %d (%d) out of range",k,j))
			}
		returned.SetMatrix(j,0,newcoords.GetRowVector(k))
		}
	return returned
	}

//Somerows, given a list of ints and the desired frame, returns an slice matrix.DenseMatrix
//containing the coordinates of the atoms with the corresponding index.
//This function returns a copy, not a reference, so changes to the returned matrix
//don't alter the original. It check for correctness of the frame and the
//Atoms requested.
func SomeRows(M *matrix.DenseMatrix, clist []int) (*matrix.DenseMatrix){
	rows:=M.Rows()
	ret:=make([][]float64,0,len(clist))
	for k,j:=range(clist){
		if j>rows-1{
			panic(fmt.Sprintf("Coordinate requested (Number: %d, value: %d) out of range!",k,j))
			}
		tmp:=M.GetRowVector(j).Array()
		if len(tmp)!=3{
			panic(fmt.Sprintf("Coordinate %d has %d components instead of 3",k, len(tmp)))
			}
		ret=append(ret,tmp)
		}
	return matrix.MakeDenseMatrixStacked(ret)
	}

//SafeSomeRows, given a list of ints and the desired frame, returns an slice matrix.DenseMatrix
//containing the coordinates of the atoms with the corresponding index.
//This function returns a copy, not a reference, so changes to the returned matrix
//don't alter the original. It check for correctness of the frame and the
//Atoms requested. Unlike SomeRow, which panics in these cases, SafeSomeRows returns an error. if problems,
//or nil.
func SafeSomeRows(M *matrix.DenseMatrix, clist []int) (*matrix.DenseMatrix, error){
	rows:=M.Rows()
	ret:=make([][]float64,0,len(clist))
	for k,j:=range(clist){
		if j>rows-1{
			return nil, fmt.Errorf("Coordinate requested (Number: %d, value: %d) out of range!",k,j)
			}
		tmp:=M.GetRowVector(j).Array()
		if len(tmp)!=3{
			return nil, fmt.Errorf("Coordinate %d has %d components instead of 3",k, len(tmp))
			}
		ret=append(ret,tmp)
		}
	return matrix.MakeDenseMatrixStacked(ret)
	}

//Unitarize takes a vector and divides it by its norm
//thus obtaining an unitary vector pointing in the same direction as
//vector.
func Unitarize(vector *matrix.DenseMatrix) *matrix.DenseMatrix{
	norm:=vector.TwoNorm()
	norm=1.0/norm
	F:=vector.Copy()
	F.Scale(norm)
	return F
	}

//AddRow adds the row vector row to each row of the matrix big, in place. Both need the same ammount of columns.
func AddRow(big,row *matrix.DenseMatrix)(error){
	bigrows:=big.Rows()
	if big.Cols() != row.Cols() || row.Rows()!=1{
		return fmt.Errorf("Ill-formed matrices for multiplication")
		}
	for i:=0;i<bigrows;i++{
		j:=big.GetRowVector(i)
		j.Add(row)
		}
	return nil
	}

//SubRow substract the row bector row from each row of the matrix big, in place. Bothe ned the same ammount
//of coulmn. This is not an efficient function since it contains two function calls, one to the DenseMatrix
//method Scale() and other to the function AddRow().
func SubRow(big,row *matrix.DenseMatrix)(error){
	row2:=row.Copy()
	row2.Scale(-1)
	err:=AddRow(big,row2)
	return err  //nil if no error, something else otherwise
	}

//Cross3D Takes 2 3-len column or row vectors and returns a column or a row
//vector, respectively, with the Cross product of them.
func Cross3D(a,b *matrix.DenseMatrix)(*matrix.DenseMatrix,error){
	ac:=a.Cols()
	ar:=a.Rows()
	bc:=b.Cols()
	br:=b.Rows()
	if ac != bc || ar != br {
		return nil, fmt.Errorf("Malformed vectors for cross product")
		}
	if ac!=3 {
		//Ok, Im sure one can do this better.
		c:=a.Transpose()
		d:=b.Transpose()
		e:=Cross3DRow(c,d)
		f:=e.Transpose()
		return f, nil
		}
	if ar!=3 {
		return nil, fmt.Errorf("Malformed vectors for cross product")
		}
	return Cross3DRow(a,b), nil
	}

//Cross3DRow returns the cross product of 2 row vectors. No error checking!
func Cross3DRow(a,b *matrix.DenseMatrix)*matrix.DenseMatrix{
	vec:=make([]float64,3,3)
	vec[0]=a.Get(0,1)*b.Get(0,2) - a.Get(0,2)*b.Get(0,1)
	vec[1]=a.Get(0,2)*b.Get(0,0) - a.Get(0,0)*b.Get(0,2)
	vec[2]=a.Get(0,0)*b.Get(0,1) - a.Get(0,1)*b.Get(0,0)
	return matrix.MakeDenseMatrix(vec,1,3)
	}

//InvSqrt return the inverse of the square root of val, or zero if
//|val|<appzero. Returns -1 if val is negative
func InvSqrt(val float64) float64{
	if math.Abs(val)<=appzero{  //Not sure if need the
		return 0
		}else if val<0{  //negative
		return  -1  //might change
		}
	return 1.0/math.Sqrt(val)
	}

//KronekerDelta is a naive implementation of the kroneker delta function.
func KronekerDelta(a,b float64) float64{
	if math.Abs(a-b)<=appzero{
		return 1
		}
	return 0
	}

//Dot returns the dot product between 2 vectors or matrices. Just the sum of the
//Element-wise multiplication. In this case returning error
//makes it problematic to use in complex operations, so this returns -1
//when problems.
func Dot(A, B *matrix.DenseMatrix) float64{
	var err error
	if A.Cols()!=B.Cols() || A.Rows()!=B.Rows(){
		panic("Matrices must have the same dimmension to obtain dot product")
		}
	//For some crazy reason if the F variable is called C, I get a
	//"ScaleMatrixDense undeclared" error at compile time :S
	F := A.Copy()
	err=F.ScaleMatrixDense(B)
	if err!=nil{
		panic(err.Error())
		}
	return DMSummation(F)
	}

//DMPowInPlace raises the DenseMatrix A, element-wise, to the nth power.
func DMPowInPlace(A *matrix.DenseMatrix, n float64){
	for i:=0;i<A.Rows();i++{
		for j:=0;j<A.Cols();j++{
		//	fmt.Println(i,j, A.Rows(),A.Cols())
			A.Set(i,j,math.Pow(A.Get(i,j),n))
			}
		}
	}

//DMPow returns B^n. It does not modify B.
func DMPow(B *matrix.DenseMatrix, n float64) *matrix.DenseMatrix{
	A:=B.Copy()
	for i:=0;i<A.Rows();i++{
		for j:=0;j<A.Cols();j++{
		//	fmt.Println(i,j, A.Rows(),A.Cols())
			A.Set(i,j,math.Pow(A.Get(i,j),n))
			}
		}
	return A
	}

//DMScaleByCol scales each column of matrix A by Col.
func DMScaleByCol(A, Col *matrix.DenseMatrix) error{
	Rows:=A.Rows()
	if Rows != Col.Rows() || Col.Cols()>1{
		return fmt.Errorf("Malformed matrices for scaling")
		}
	for i:=0;i<Rows;i++{
		A.ScaleRow(i,Col.Get(i,0))
		}
	return nil
	}

//DMScaleByRow each row of matrix A by Row.
func DMScaleByRow(A, Row *matrix.DenseMatrix) error{
	Cols:=A.Cols()
	if Cols != Row.Cols() || Row.Rows()>1{
		return fmt.Errorf("Malformed matrices for scaling")
		}
	for i:=0;i<Cols;i++{
		mult:=Row.Get(0,i)
		for j:=0;j<A.Rows();j++{
			A.Set(j,i,mult*A.Get(j,i))
			}
		}
	return nil
	}

//DMSummation returns the sum of all elements in matrix A.
func DMSummation(A *matrix.DenseMatrix) float64 {
	Rows:=A.Rows()
	Cols:=A.Cols()
	var sum float64
	for i:=0;i<Cols;i++{
		for j:=0;j<Rows;j++{
			sum+=A.Get(j,i)
			}
		}
	return sum
	}

//DMaddfloat returns a matrix which elements are those of matrix A plus the float B.
func DMaddfloat(A *matrix.DenseMatrix, B float64)*matrix.DenseMatrix{
	Rows:=A.Rows()
	Cols:=A.Cols()
	copy:=matrix.MakeDenseCopy(A)
	for i:=0;i<Cols;i++{
		for j:=0;j<Rows;j++{
			copy.Set(j,i,(A.Get(j,i)+B))
			}
		}
	return copy
	}

//DMDelRow removes Row i from matrix A.
func DMDelRow(A *matrix.DenseMatrix,i int)(*matrix.DenseMatrix, error){
		//Im not sure if copying takes place.
		var err error
		lenght:=A.Rows()
		upper:=A.GetMatrix(0,0,i,3)
		lower:=A.GetMatrix(i+1,0,lenght-i-1,3)
		A,err=upper.Stack(lower)
		return A, err
	}

<commit_msg>bugfix<commit_after>/*
 * matrix.go, part of gochem.
 *
 *
 * Copyright 2012 Raul Mera <rmera{at}chemDOThelsinkiDOTfi>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 *
 * Gochem is developed at the laboratory for instruction in Swedish, Department of Chemistry,
 * University of Helsinki, Finland.
 *
 *
 */
/***Dedicated to the long life of the Ven. Khenpo Phuntzok Tenzin Rinpoche***/

package chem

import "github.com/skelterjohn/go.matrix"
import "math"
import "fmt"

//These ones are basic math, belonging more to the go.matrix package
//If there is something similar already made
//in go.matrix this functions will be deleted. Otherwise they could be
//made methods for DenseMatrix and included in go.matrix

//Some of this functions don't return error messages because they are meant to
//Be inserted in mathematical expressions and thus they need to return only one value.

/*SetRows replaces the coordinates of atoms in the positions given by atomlist with the coordinates
in newcoords (in order) If atomlist contains a single element, it replaces as many coordinates
as given in newcoords, starting
at the element in atomlist. In the latter case, the function checks that there are enough coordinates to
replace and panics if not. */
func SetRows(target, newcoords *matrix.DenseMatrix, atomlist []int) *matrix.DenseMatrix{
	//If supplies a list with one number, the newcoords will replace the old coords
	//Starting that number. We do check that you don't put more coords than spaces we have.
	returned:=target.Copy()
	if len(atomlist)==1{
		if newcoords.Rows()>target.Rows()-atomlist[0]-1{
			panic(fmt.Sprintf("Cant replace starting from position %d: Not enough atoms in molecule", atomlist[0]))
			}
		returned.SetMatrix(atomlist[0],0,newcoords)
		return returned
		}
	//If the list has more than one atom
	lenatoms:=target.Rows()
	for k,j:=range(atomlist){
		if j>lenatoms-1{
			panic(fmt.Sprintf("Requested position number: %d (%d) out of range",k,j))
			}
		returned.SetMatrix(j,0,newcoords.GetRowVector(k))
		}
	return returned
	}

//Somerows, given a list of ints and the desired frame, returns an slice matrix.DenseMatrix
//containing the coordinates of the atoms with the corresponding index.
//This function returns a copy, not a reference, so changes to the returned matrix
//don't alter the original. It check for correctness of the frame and the
//Atoms requested.
func SomeRows(M *matrix.DenseMatrix, clist []int) (*matrix.DenseMatrix){
	rows:=M.Rows()
	ret:=make([][]float64,0,len(clist))
	for k,j:=range(clist){
		if j>rows-1{
			panic(fmt.Sprintf("Coordinate requested (Number: %d, value: %d) out of range!",k,j))
			}
		tmp:=M.GetRowVector(j).Array()
		if len(tmp)!=3{
			panic(fmt.Sprintf("Coordinate %d has %d components instead of 3",k, len(tmp)))
			}
		ret=append(ret,tmp)
		}
	return matrix.MakeDenseMatrixStacked(ret)
	}

//SafeSomeRows, given a list of ints and the desired frame, returns an slice matrix.DenseMatrix
//containing the coordinates of the atoms with the corresponding index.
//This function returns a copy, not a reference, so changes to the returned matrix
//don't alter the original. It check for correctness of the frame and the
//Atoms requested. Unlike SomeRow, which panics in these cases, SafeSomeRows returns an error. if problems,
//or nil.
func SafeSomeRows(M *matrix.DenseMatrix, clist []int) (*matrix.DenseMatrix, error){
	rows:=M.Rows()
	ret:=make([][]float64,0,len(clist))
	for k,j:=range(clist){
		if j>rows-1{
			return nil, fmt.Errorf("Coordinate requested (Number: %d, value: %d) out of range!",k,j)
			}
		tmp:=M.GetRowVector(j).Array()
		if len(tmp)!=3{
			return nil, fmt.Errorf("Coordinate %d has %d components instead of 3",k, len(tmp))
			}
		ret=append(ret,tmp)
		}
	return matrix.MakeDenseMatrixStacked(ret), nil
	}

//Unitarize takes a vector and divides it by its norm
//thus obtaining an unitary vector pointing in the same direction as
//vector.
func Unitarize(vector *matrix.DenseMatrix) *matrix.DenseMatrix{
	norm:=vector.TwoNorm()
	norm=1.0/norm
	F:=vector.Copy()
	F.Scale(norm)
	return F
	}

//AddRow adds the row vector row to each row of the matrix big, in place. Both need the same ammount of columns.
func AddRow(big,row *matrix.DenseMatrix)(error){
	bigrows:=big.Rows()
	if big.Cols() != row.Cols() || row.Rows()!=1{
		return fmt.Errorf("Ill-formed matrices for multiplication")
		}
	for i:=0;i<bigrows;i++{
		j:=big.GetRowVector(i)
		j.Add(row)
		}
	return nil
	}

//SubRow substract the row bector row from each row of the matrix big, in place. Bothe ned the same ammount
//of coulmn. This is not an efficient function since it contains two function calls, one to the DenseMatrix
//method Scale() and other to the function AddRow().
func SubRow(big,row *matrix.DenseMatrix)(error){
	row2:=row.Copy()
	row2.Scale(-1)
	err:=AddRow(big,row2)
	return err  //nil if no error, something else otherwise
	}

//Cross3D Takes 2 3-len column or row vectors and returns a column or a row
//vector, respectively, with the Cross product of them.
func Cross3D(a,b *matrix.DenseMatrix)(*matrix.DenseMatrix,error){
	ac:=a.Cols()
	ar:=a.Rows()
	bc:=b.Cols()
	br:=b.Rows()
	if ac != bc || ar != br {
		return nil, fmt.Errorf("Malformed vectors for cross product")
		}
	if ac!=3 {
		//Ok, Im sure one can do this better.
		c:=a.Transpose()
		d:=b.Transpose()
		e:=Cross3DRow(c,d)
		f:=e.Transpose()
		return f, nil
		}
	if ar!=3 {
		return nil, fmt.Errorf("Malformed vectors for cross product")
		}
	return Cross3DRow(a,b), nil
	}

//Cross3DRow returns the cross product of 2 row vectors. No error checking!
func Cross3DRow(a,b *matrix.DenseMatrix)*matrix.DenseMatrix{
	vec:=make([]float64,3,3)
	vec[0]=a.Get(0,1)*b.Get(0,2) - a.Get(0,2)*b.Get(0,1)
	vec[1]=a.Get(0,2)*b.Get(0,0) - a.Get(0,0)*b.Get(0,2)
	vec[2]=a.Get(0,0)*b.Get(0,1) - a.Get(0,1)*b.Get(0,0)
	return matrix.MakeDenseMatrix(vec,1,3)
	}

//InvSqrt return the inverse of the square root of val, or zero if
//|val|<appzero. Returns -1 if val is negative
func InvSqrt(val float64) float64{
	if math.Abs(val)<=appzero{  //Not sure if need the
		return 0
		}else if val<0{  //negative
		return  -1  //might change
		}
	return 1.0/math.Sqrt(val)
	}

//KronekerDelta is a naive implementation of the kroneker delta function.
func KronekerDelta(a,b float64) float64{
	if math.Abs(a-b)<=appzero{
		return 1
		}
	return 0
	}

//Dot returns the dot product between 2 vectors or matrices. Just the sum of the
//Element-wise multiplication. In this case returning error
//makes it problematic to use in complex operations, so this returns -1
//when problems.
func Dot(A, B *matrix.DenseMatrix) float64{
	var err error
	if A.Cols()!=B.Cols() || A.Rows()!=B.Rows(){
		panic("Matrices must have the same dimmension to obtain dot product")
		}
	//For some crazy reason if the F variable is called C, I get a
	//"ScaleMatrixDense undeclared" error at compile time :S
	F := A.Copy()
	err=F.ScaleMatrixDense(B)
	if err!=nil{
		panic(err.Error())
		}
	return DMSummation(F)
	}

//DMPowInPlace raises the DenseMatrix A, element-wise, to the nth power.
func DMPowInPlace(A *matrix.DenseMatrix, n float64){
	for i:=0;i<A.Rows();i++{
		for j:=0;j<A.Cols();j++{
		//	fmt.Println(i,j, A.Rows(),A.Cols())
			A.Set(i,j,math.Pow(A.Get(i,j),n))
			}
		}
	}

//DMPow returns B^n. It does not modify B.
func DMPow(B *matrix.DenseMatrix, n float64) *matrix.DenseMatrix{
	A:=B.Copy()
	for i:=0;i<A.Rows();i++{
		for j:=0;j<A.Cols();j++{
		//	fmt.Println(i,j, A.Rows(),A.Cols())
			A.Set(i,j,math.Pow(A.Get(i,j),n))
			}
		}
	return A
	}

//DMScaleByCol scales each column of matrix A by Col.
func DMScaleByCol(A, Col *matrix.DenseMatrix) error{
	Rows:=A.Rows()
	if Rows != Col.Rows() || Col.Cols()>1{
		return fmt.Errorf("Malformed matrices for scaling")
		}
	for i:=0;i<Rows;i++{
		A.ScaleRow(i,Col.Get(i,0))
		}
	return nil
	}

//DMScaleByRow each row of matrix A by Row.
func DMScaleByRow(A, Row *matrix.DenseMatrix) error{
	Cols:=A.Cols()
	if Cols != Row.Cols() || Row.Rows()>1{
		return fmt.Errorf("Malformed matrices for scaling")
		}
	for i:=0;i<Cols;i++{
		mult:=Row.Get(0,i)
		for j:=0;j<A.Rows();j++{
			A.Set(j,i,mult*A.Get(j,i))
			}
		}
	return nil
	}

//DMSummation returns the sum of all elements in matrix A.
func DMSummation(A *matrix.DenseMatrix) float64 {
	Rows:=A.Rows()
	Cols:=A.Cols()
	var sum float64
	for i:=0;i<Cols;i++{
		for j:=0;j<Rows;j++{
			sum+=A.Get(j,i)
			}
		}
	return sum
	}

//DMaddfloat returns a matrix which elements are those of matrix A plus the float B.
func DMaddfloat(A *matrix.DenseMatrix, B float64)*matrix.DenseMatrix{
	Rows:=A.Rows()
	Cols:=A.Cols()
	copy:=matrix.MakeDenseCopy(A)
	for i:=0;i<Cols;i++{
		for j:=0;j<Rows;j++{
			copy.Set(j,i,(A.Get(j,i)+B))
			}
		}
	return copy
	}

//DMDelRow removes Row i from matrix A.
func DMDelRow(A *matrix.DenseMatrix,i int)(*matrix.DenseMatrix, error){
		//Im not sure if copying takes place.
		var err error
		lenght:=A.Rows()
		upper:=A.GetMatrix(0,0,i,3)
		lower:=A.GetMatrix(i+1,0,lenght-i-1,3)
		A,err=upper.Stack(lower)
		return A, err
	}

<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"log"
	"text/tabwriter"

	"github.com/coreos/updatectl/client/update/v1"
)

var (
	channelFlags struct {
		appId   StringFlag
		channel StringFlag
	}

	cmdChannel = &Command{
		Name:    "channel",
		Summary: "Manage channels for an application",
		Subcommands: []*Command{
			cmdChannelList,
			cmdChannelUpdate,
		},
	}

	cmdChannelList = &Command{
		Name:        "channel list",
		Usage:       "[OPTION]...",
		Description: `List all channels for an application.`,
		Run:         channelList,
	}
	cmdChannelUpdate = &Command{
		Name:    "channel update",
		Usage:   "[OPTION]... <version>",
		Summary: `Update a channel for an application to a new version.`,
		Description: `Given an application ID (--app-id) and channel (--channel),
you can change the channel to a new version (<version>).`,
		Run: channelUpdate,
	}
)

func init() {
	cmdChannelList.Flags.Var(&channelFlags.appId, "app-id", "The application ID to list the channels of.")

	cmdChannelUpdate.Flags.Var(&channelFlags.appId, "app-id", "The application ID that the channel belongs to.")
	cmdChannelUpdate.Flags.Var(&channelFlags.channel, "channel", "The channel to update.")
}

func formatChannel(channel *update.AppChannel) string {
	return fmt.Sprintf("%s\t%s\t%t\t%s\n", channel.Label, channel.Version, channel.Publish, channel.Upstream)
}

func channelList(args []string, service *update.Service, out *tabwriter.Writer) int {
	if channelFlags.appId.Get() == nil {
		return ERROR_USAGE
	}

	listCall := service.Channel.List(channelFlags.appId.String())
	list, err := listCall.Do()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Fprint(out, "Label\tVersion\tPublish\tUpstream\n")
	for _, channel := range list.Items {
		fmt.Fprintf(out, "%s", formatChannel(channel))
	}
	out.Flush()
	return OK
}

func channelUpdate(args []string, service *update.Service, out *tabwriter.Writer) int {
	if len(args) != 1 || channelFlags.appId.Get() == nil ||
		channelFlags.channel.Get() == nil {
		return ERROR_USAGE
	}

	channelReq := &update.ChannelRequest{Version: args[0]}

	call := service.Channel.Update(channelFlags.appId.String(), channelFlags.channel.String(), channelReq)
	channel, err := call.Do()
	if err != nil {
		log.Fatal(err)
	}

	fmt.Fprintf(out, "%s\n", channel.Version)
	out.Flush()
	return OK
}
<commit_msg>channel: support for publish/unpublish. make version a flag<commit_after>package main

import (
	"fmt"
	"log"
	"text/tabwriter"

	"github.com/coreos/updatectl/client/update/v1"
)

var (
	channelFlags struct {
		appId   StringFlag
		channel StringFlag
		version StringFlag
		publish bool
	}

	cmdChannel = &Command{
		Name:    "channel",
		Summary: "Manage channels for an application",
		Subcommands: []*Command{
			cmdChannelList,
			cmdChannelUpdate,
		},
	}

	cmdChannelList = &Command{
		Name:        "channel list",
		Usage:       "[OPTION]...",
		Description: `List all channels for an application.`,
		Run:         channelList,
	}
	cmdChannelUpdate = &Command{
		Name:    "channel update",
		Usage:   "[OPTION]...",
		Summary: `Update the version and publish state for an application channel.`,
		Description: `Given an application ID (--app-id) and channel (--channel),
you can change the channel to a new version (--version), or set the publish state (--publish).`,
		Run: channelUpdate,
	}
)

func init() {
	cmdChannelList.Flags.Var(&channelFlags.appId, "app-id", "The application ID to list the channels of.")

	cmdChannelUpdate.Flags.Var(&channelFlags.appId, "app-id", "The application ID that the channel belongs to.")
	cmdChannelUpdate.Flags.Var(&channelFlags.channel, "channel", "The channel to update.")
	cmdChannelUpdate.Flags.BoolVar(&channelFlags.publish, "publish", false, "Publish or unpublish the channel.")
	cmdChannelUpdate.Flags.Var(&channelFlags.version, "version", "The version to update the channel to.")
}

func formatChannel(channel *update.AppChannel) string {
	return fmt.Sprintf("%s\t%s\t%t\t%s\n", channel.Label, channel.Version, channel.Publish, channel.Upstream)
}

func channelList(args []string, service *update.Service, out *tabwriter.Writer) int {
	if channelFlags.appId.Get() == nil {
		return ERROR_USAGE
	}

	listCall := service.Channel.List(channelFlags.appId.String())
	list, err := listCall.Do()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Fprint(out, "Label\tVersion\tPublish\tUpstream\n")
	for _, channel := range list.Items {
		fmt.Fprintf(out, "%s", formatChannel(channel))
	}
	out.Flush()
	return OK
}

func channelUpdate(args []string, service *update.Service, out *tabwriter.Writer) int {
	if channelFlags.version.Get() == nil || channelFlags.appId.Get() == nil || channelFlags.channel.Get() == nil {
		return ERROR_USAGE
	}

	channelReq := &update.ChannelRequest{Version: *channelFlags.version.Get(), Publish: channelFlags.publish}

	call := service.Channel.Update(channelFlags.appId.String(), channelFlags.channel.String(), channelReq)
	channel, err := call.Do()
	if err != nil {
		log.Fatal(err)
	}

	fmt.Fprintf(out, "%s", formatChannel(channel))
	out.Flush()
	return OK
}
<|endoftext|>


<commit_before>// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package gsharedmemory

import (
	"github.com/ava-labs/avalanchego/chains/atomic/gsharedmemory/gsharedmemoryproto"
)

type filteredBatch struct {
	writes  map[string][]byte
	deletes map[string]struct{}
}

func (b *filteredBatch) Put(key []byte, value []byte) error {
	keyStr := string(key)
	delete(b.deletes, keyStr)
	b.writes[keyStr] = value
	return nil
}

func (b *filteredBatch) Delete(key []byte) error {
	keyStr := string(key)
	delete(b.writes, keyStr)
	b.deletes[keyStr] = struct{}{}
	return nil
}

func (b *filteredBatch) PutRequests() []*gsharedmemoryproto.BatchPut {
	reqs := make([]*gsharedmemoryproto.BatchPut, 0, len(b.writes))
	for keyStr, value := range b.writes {
		reqs = append(reqs, &gsharedmemoryproto.BatchPut{
			Key:   []byte(keyStr),
			Value: value,
		})
	}
	return reqs
}

func (b *filteredBatch) DeleteRequests() []*gsharedmemoryproto.BatchDelete {
	reqs := make([]*gsharedmemoryproto.BatchDelete, 0, len(b.deletes))
	for keyStr := range b.deletes {
		reqs = append(reqs, &gsharedmemoryproto.BatchDelete{
			Key: []byte(keyStr),
		})
	}
	return reqs
}
<commit_msg>nits optimized slice population<commit_after>// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package gsharedmemory

import (
	"github.com/ava-labs/avalanchego/chains/atomic/gsharedmemory/gsharedmemoryproto"
)

type filteredBatch struct {
	writes  map[string][]byte
	deletes map[string]struct{}
}

func (b *filteredBatch) Put(key []byte, value []byte) error {
	keyStr := string(key)
	delete(b.deletes, keyStr)
	b.writes[keyStr] = value
	return nil
}

func (b *filteredBatch) Delete(key []byte) error {
	keyStr := string(key)
	delete(b.writes, keyStr)
	b.deletes[keyStr] = struct{}{}
	return nil
}

func (b *filteredBatch) PutRequests() []*gsharedmemoryproto.BatchPut {
	reqs := make([]*gsharedmemoryproto.BatchPut, len(b.writes))
	i := 0
	for keyStr, value := range b.writes {
		reqs[i] = &gsharedmemoryproto.BatchPut{
			Key:   []byte(keyStr),
			Value: value,
		}
		i++
	}
	return reqs
}

func (b *filteredBatch) DeleteRequests() []*gsharedmemoryproto.BatchDelete {
	reqs := make([]*gsharedmemoryproto.BatchDelete, len(b.deletes))
	i := 0
	for keyStr := range b.deletes {
		reqs[i] = &gsharedmemoryproto.BatchDelete{
			Key: []byte(keyStr),
		}
		i++
	}
	return reqs
}
<|endoftext|>


<commit_before>// Package source implements the Source RCON protocol.
// This implementation can be used for games using the Source engine,
// most notably Valve games.
package source

//TODO multi-packet responses

import (
	"bytes"
	"encoding/binary"
	"errors"
	"fmt"
	"github.com/adabei/goldenbot/rcon"
	"log"
	"net"
	"time"
)

const (
	SERVERDATA_AUTH           = 3
	SERVERDATA_AUTH_RESPONSE  = 2
	SERVERDATA_EXECCOMMAND    = 2
	SERVERDATA_RESPONSE_VALUE = 0
)

func init() {
	rcon.Register("source", Relay)
}

// Query sends a single RCON command cmd using the connection conn.
// It returns the server response and any error encountered.
func Query(conn net.Conn, packet []byte) ([]byte, error) {
	_, err := conn.Write(packet)
	if err != nil {
		return nil, err
	}

	var buf [4096]byte
	conn.SetReadDeadline(time.Now().Add(5000 * time.Millisecond))
	n, err := conn.Read(buf[0:])
	if err != nil {
		return nil, err
	}

	return buf[12:n], nil
}

// Relay allows for easier use in concurrent system.
// By calling Relay in a goroutine and passing requests using
// the queries channel the password does not need to be exposed.
func Relay(addr, password string, queries chan rcon.RCONQuery) {
	conn, err := authorizeConnection(addr, password)
	if err != nil {
		log.Fatal(err)
	}

	for req := range queries {
		res, err := Query(conn, rconPacket(1337, SERVERDATA_EXECCOMMAND, req.Command))
		if err != nil {
			log.Println("RCON request failed:", req.Command)
		}

		if req.Response != nil {
			req.Response <- res
		}

		time.Sleep(500 * time.Millisecond)
	}
}

// authorizeConnection establishes the RCON connection to the server addr
// using password for authorization.
func authorizeConnection(addr, password string) (net.Conn, error) {
	conn, err := net.Dial("tcp", addr)
	if err != nil {
		return nil, err
	}

	id := 0
	conn.Write(rconPacket(id, 3, password))

	var buf [4096]byte

	// serverdata_response_value
	conn.SetReadDeadline(time.Now().Add(5000 * time.Millisecond))
	_, err = conn.Read(buf[0:])
	if err != nil {
		return nil, errors.New("Authorization failed: " + err.Error())
	}

	// serverdata_auth_response
	_, err = conn.Read(buf[0:])
	conn.SetReadDeadline(time.Now().Add(15000 * time.Millisecond))
	if err != nil {
		return nil, errors.New("Authorization failed: " + err.Error())
	}

	resID, _ := binary.Varint(buf[4:7])
	if int(resID) != id {
		return nil, errors.New("Authorization failed: invalid RCON password")
	}

	return conn, nil
}

// rconPacket generates a Source compatible packet.
func rconPacket(id, packetType int, body string) []byte {
	buf := make([]byte, 0)
	buf = append(buf, littleEndianInt32(len(body)+10)...)
	buf = append(buf, littleEndianInt32(id)...)
	buf = append(buf, littleEndianInt32(packetType)...)
	buf = append(buf, []byte(body+"\x00")...)
	buf = append(buf, []byte("\x00")...)

	return buf
}

// littleEndianInt32 converts a 32 bit integer value to []byte.
func littleEndianInt32(n int) []byte {
	buf := &bytes.Buffer{}
	binary.Write(buf, binary.LittleEndian, int32(n))
	return buf.Bytes()
}
<commit_msg>removes unused import<commit_after>// Package source implements the Source RCON protocol.
// This implementation can be used for games using the Source engine,
// most notably Valve games.
package source

//TODO multi-packet responses

import (
	"bytes"
	"encoding/binary"
	"errors"
	"github.com/adabei/goldenbot/rcon"
	"log"
	"net"
	"time"
)

const (
	SERVERDATA_AUTH           = 3
	SERVERDATA_AUTH_RESPONSE  = 2
	SERVERDATA_EXECCOMMAND    = 2
	SERVERDATA_RESPONSE_VALUE = 0
)

func init() {
	rcon.Register("source", Relay)
}

// Query sends a single RCON command cmd using the connection conn.
// It returns the server response and any error encountered.
func Query(conn net.Conn, packet []byte) ([]byte, error) {
	_, err := conn.Write(packet)
	if err != nil {
		return nil, err
	}

	var buf [4096]byte
	conn.SetReadDeadline(time.Now().Add(5000 * time.Millisecond))
	n, err := conn.Read(buf[0:])
	if err != nil {
		return nil, err
	}

	return buf[12:n], nil
}

// Relay allows for easier use in concurrent system.
// By calling Relay in a goroutine and passing requests using
// the queries channel the password does not need to be exposed.
func Relay(addr, password string, queries chan rcon.RCONQuery) {
	conn, err := authorizeConnection(addr, password)
	if err != nil {
		log.Fatal(err)
	}

	for req := range queries {
		res, err := Query(conn, rconPacket(1337, SERVERDATA_EXECCOMMAND, req.Command))
		if err != nil {
			log.Println("RCON request failed:", req.Command)
		}

		if req.Response != nil {
			req.Response <- res
		}

		time.Sleep(500 * time.Millisecond)
	}
}

// authorizeConnection establishes the RCON connection to the server addr
// using password for authorization.
func authorizeConnection(addr, password string) (net.Conn, error) {
	conn, err := net.Dial("tcp", addr)
	if err != nil {
		return nil, err
	}

	id := 0
	conn.Write(rconPacket(id, 3, password))

	var buf [4096]byte

	// serverdata_response_value
	conn.SetReadDeadline(time.Now().Add(5000 * time.Millisecond))
	_, err = conn.Read(buf[0:])
	if err != nil {
		return nil, errors.New("Authorization failed: " + err.Error())
	}

	// serverdata_auth_response
	_, err = conn.Read(buf[0:])
	conn.SetReadDeadline(time.Now().Add(15000 * time.Millisecond))
	if err != nil {
		return nil, errors.New("Authorization failed: " + err.Error())
	}

	resID, _ := binary.Varint(buf[4:7])
	if int(resID) != id {
		return nil, errors.New("Authorization failed: invalid RCON password")
	}

	return conn, nil
}

// rconPacket generates a Source compatible packet.
func rconPacket(id, packetType int, body string) []byte {
	buf := make([]byte, 0)
	buf = append(buf, littleEndianInt32(len(body)+10)...)
	buf = append(buf, littleEndianInt32(id)...)
	buf = append(buf, littleEndianInt32(packetType)...)
	buf = append(buf, []byte(body+"\x00")...)
	buf = append(buf, []byte("\x00")...)

	return buf
}

// littleEndianInt32 converts a 32 bit integer value to []byte.
func littleEndianInt32(n int) []byte {
	buf := &bytes.Buffer{}
	binary.Write(buf, binary.LittleEndian, int32(n))
	return buf.Bytes()
}
<|endoftext|>