<commit_before>package main

import (
	"encoding/json"
	"fmt"
	"net/http"

	"github.com/gorilla/mux"
	_ "github.com/mattn/go-sqlite3"

	"github.com/lxc/lxd/shared"

	log "gopkg.in/inconshreveable/log15.v2"
)

/* This is used for both profiles post and profile put */
type profilesPostReq struct {
	Name    string            `json:"name"`
	Config  map[string]string `json:"config"`
	Devices shared.Devices    `json:"devices"`
}

func profilesGet(d *Daemon, r *http.Request) Response {
	results, err := dbProfilesGet(d.db)
	if err != nil {
		return SmartError(err)
	}

	recursion := d.isRecursionRequest(r)

	resultString := make([]string, len(results))
	resultMap := make([]*shared.ProfileConfig, len(results))
	i := 0
	for _, name := range results {
		if !recursion {
			url := fmt.Sprintf("/%s/profiles/%s", shared.APIVersion, name)
			resultString[i] = url
		} else {
			profile, err := doProfileGet(d, name)
			if err != nil {
				shared.Log.Error("Failed to get profile", log.Ctx{"profile": name})
				continue
			}
			resultMap[i] = profile
		}
		i++
	}

	if !recursion {
		return SyncResponse(true, resultString)
	}

	return SyncResponse(true, resultMap)
}

func profilesPost(d *Daemon, r *http.Request) Response {
	req := profilesPostReq{}
	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
		return BadRequest(err)
	}

	if req.Name == "" {
		return BadRequest(fmt.Errorf("No name provided"))
	}

	_, err := dbProfileCreate(d.db, req.Name, req.Config, req.Devices)
	if err != nil {
		return InternalError(
			fmt.Errorf("Error inserting %s into database", req.Name))
	}

	return EmptySyncResponse
}

var profilesCmd = Command{
	name: "profiles",
	get:  profilesGet,
	post: profilesPost}

func doProfileGet(d *Daemon, name string) (*shared.ProfileConfig, error) {
	config, err := dbProfileConfigGet(d.db, name)
	if err != nil {
		return nil, err
	}

	devices, err := dbDevicesGet(d.db, name, true)
	if err != nil {
		return nil, err
	}

	return &shared.ProfileConfig{
		Name:    name,
		Config:  config,
		Devices: devices,
	}, nil
}

func profileGet(d *Daemon, r *http.Request) Response {
	name := mux.Vars(r)["name"]

	resp, err := doProfileGet(d, name)
	if err != nil {
		return SmartError(err)
	}

	return SyncResponse(true, resp)
}

func getRunningContainersWithProfile(d *Daemon, profile string) []container {
	results := []container{}

	output, err := dbProfileContainersGet(d.db, profile)
	if err != nil {
		return results
	}

	for _, name := range output {
		c, err := containerLXDLoad(d, name)
		if err != nil {
			shared.Log.Error("failed opening container", log.Ctx{"container": name})
			continue
		}
		results = append(results, c)
	}
	return results
}

func profilePut(d *Daemon, r *http.Request) Response {
	name := mux.Vars(r)["name"]

	req := profilesPostReq{}
	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
		return BadRequest(err)
	}

	preDevList, err := dbDevicesGet(d.db, name, true)
	if err != nil {
		return InternalError(err)
	}
	clist := getRunningContainersWithProfile(d, name)

	id, err := dbProfileIDGet(d.db, name)
	if err != nil {
		return InternalError(fmt.Errorf("Failed to retrieve profile='%s'", name))
	}

	tx, err := dbBegin(d.db)
	if err != nil {
		return InternalError(err)
	}

	err = dbProfileConfigClear(tx, id)
	if err != nil {
		tx.Rollback()
		return InternalError(err)
	}

	err = dbProfileConfigAdd(tx, id, req.Config)
	if err != nil {
		tx.Rollback()
		return SmartError(err)
	}

	err = dbDevicesAdd(tx, "profile", id, req.Devices)
	if err != nil {
		tx.Rollback()
		return SmartError(err)
	}

	postDevList := req.Devices
	// do our best to update the device list for each container using
	// this profile
	for _, c := range clist {
		if !c.IsRunning() {
			continue
		}
		fmt.Printf("Updating profile device list for %s\n", c.NameGet())
		if err := devicesApplyDeltaLive(tx, c, preDevList, postDevList); err != nil {
			shared.Debugf("Warning: failed to update device list for container %s (profile %s updated)\n", c.NameGet(), name)
		}
	}

	err = txCommit(tx)
	if err != nil {
		return InternalError(err)
	}

	return EmptySyncResponse
}

// The handler for the delete operation.
func profileDelete(d *Daemon, r *http.Request) Response {
	name := mux.Vars(r)["name"]
	err := dbProfileDelete(d.db, name)

	if err != nil {
		return InternalError(err)
	}

	return EmptySyncResponse
}

var profileCmd = Command{name: "profiles/{name}", get: profileGet, put: profilePut, delete: profileDelete}
<commit_msg>actually report DB error<commit_after>package main

import (
	"encoding/json"
	"fmt"
	"net/http"

	"github.com/gorilla/mux"
	_ "github.com/mattn/go-sqlite3"

	"github.com/lxc/lxd/shared"

	log "gopkg.in/inconshreveable/log15.v2"
)

/* This is used for both profiles post and profile put */
type profilesPostReq struct {
	Name    string            `json:"name"`
	Config  map[string]string `json:"config"`
	Devices shared.Devices    `json:"devices"`
}

func profilesGet(d *Daemon, r *http.Request) Response {
	results, err := dbProfilesGet(d.db)
	if err != nil {
		return SmartError(err)
	}

	recursion := d.isRecursionRequest(r)

	resultString := make([]string, len(results))
	resultMap := make([]*shared.ProfileConfig, len(results))
	i := 0
	for _, name := range results {
		if !recursion {
			url := fmt.Sprintf("/%s/profiles/%s", shared.APIVersion, name)
			resultString[i] = url
		} else {
			profile, err := doProfileGet(d, name)
			if err != nil {
				shared.Log.Error("Failed to get profile", log.Ctx{"profile": name})
				continue
			}
			resultMap[i] = profile
		}
		i++
	}

	if !recursion {
		return SyncResponse(true, resultString)
	}

	return SyncResponse(true, resultMap)
}

func profilesPost(d *Daemon, r *http.Request) Response {
	req := profilesPostReq{}
	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
		return BadRequest(err)
	}

	if req.Name == "" {
		return BadRequest(fmt.Errorf("No name provided"))
	}

	_, err := dbProfileCreate(d.db, req.Name, req.Config, req.Devices)
	if err != nil {
		return InternalError(
			fmt.Errorf("Error inserting %s into database: %s", req.Name, err))
	}

	return EmptySyncResponse
}

var profilesCmd = Command{
	name: "profiles",
	get:  profilesGet,
	post: profilesPost}

func doProfileGet(d *Daemon, name string) (*shared.ProfileConfig, error) {
	config, err := dbProfileConfigGet(d.db, name)
	if err != nil {
		return nil, err
	}

	devices, err := dbDevicesGet(d.db, name, true)
	if err != nil {
		return nil, err
	}

	return &shared.ProfileConfig{
		Name:    name,
		Config:  config,
		Devices: devices,
	}, nil
}

func profileGet(d *Daemon, r *http.Request) Response {
	name := mux.Vars(r)["name"]

	resp, err := doProfileGet(d, name)
	if err != nil {
		return SmartError(err)
	}

	return SyncResponse(true, resp)
}

func getRunningContainersWithProfile(d *Daemon, profile string) []container {
	results := []container{}

	output, err := dbProfileContainersGet(d.db, profile)
	if err != nil {
		return results
	}

	for _, name := range output {
		c, err := containerLXDLoad(d, name)
		if err != nil {
			shared.Log.Error("failed opening container", log.Ctx{"container": name})
			continue
		}
		results = append(results, c)
	}
	return results
}

func profilePut(d *Daemon, r *http.Request) Response {
	name := mux.Vars(r)["name"]

	req := profilesPostReq{}
	if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
		return BadRequest(err)
	}

	preDevList, err := dbDevicesGet(d.db, name, true)
	if err != nil {
		return InternalError(err)
	}
	clist := getRunningContainersWithProfile(d, name)

	id, err := dbProfileIDGet(d.db, name)
	if err != nil {
		return InternalError(fmt.Errorf("Failed to retrieve profile='%s'", name))
	}

	tx, err := dbBegin(d.db)
	if err != nil {
		return InternalError(err)
	}

	err = dbProfileConfigClear(tx, id)
	if err != nil {
		tx.Rollback()
		return InternalError(err)
	}

	err = dbProfileConfigAdd(tx, id, req.Config)
	if err != nil {
		tx.Rollback()
		return SmartError(err)
	}

	err = dbDevicesAdd(tx, "profile", id, req.Devices)
	if err != nil {
		tx.Rollback()
		return SmartError(err)
	}

	postDevList := req.Devices
	// do our best to update the device list for each container using
	// this profile
	for _, c := range clist {
		if !c.IsRunning() {
			continue
		}
		fmt.Printf("Updating profile device list for %s\n", c.NameGet())
		if err := devicesApplyDeltaLive(tx, c, preDevList, postDevList); err != nil {
			shared.Debugf("Warning: failed to update device list for container %s (profile %s updated)\n", c.NameGet(), name)
		}
	}

	err = txCommit(tx)
	if err != nil {
		return InternalError(err)
	}

	return EmptySyncResponse
}

// The handler for the delete operation.
func profileDelete(d *Daemon, r *http.Request) Response {
	name := mux.Vars(r)["name"]
	err := dbProfileDelete(d.db, name)

	if err != nil {
		return InternalError(err)
	}

	return EmptySyncResponse
}

var profileCmd = Command{name: "profiles/{name}", get: profileGet, put: profilePut, delete: profileDelete}
<|endoftext|>


<commit_before>package util

import (
	"crypto/tls"
	"crypto/x509"
	"fmt"
	"io/ioutil"
	"net"

	"github.com/lxc/lxd/shared"
	"github.com/lxc/lxd/shared/logger"
)

// InMemoryNetwork creates a fully in-memory listener and dial function.
//
// Each time the dial function is invoked a new pair of net.Conn objects will
// be created using net.Pipe: the listener's Accept method will unblock and
// return one end of the pipe and the other end will be returned by the dial
// function.
func InMemoryNetwork() (net.Listener, func() net.Conn) {
	listener := &inMemoryListener{
		conns:  make(chan net.Conn, 16),
		closed: make(chan struct{}),
	}
	dialer := func() net.Conn {
		server, client := net.Pipe()
		listener.conns <- server
		return client
	}
	return listener, dialer
}

type inMemoryListener struct {
	conns  chan net.Conn
	closed chan struct{}
}

// Accept waits for and returns the next connection to the listener.
func (l *inMemoryListener) Accept() (net.Conn, error) {
	select {
	case conn := <-l.conns:
		return conn, nil
	case <-l.closed:
		return nil, fmt.Errorf("closed")
	}
}

// Close closes the listener.
// Any blocked Accept operations will be unblocked and return errors.
func (l *inMemoryListener) Close() error {
	close(l.closed)
	return nil
}

// Addr returns the listener's network address.
func (l *inMemoryListener) Addr() net.Addr {
	return &inMemoryAddr{}
}

type inMemoryAddr struct {
}

func (a *inMemoryAddr) Network() string {
	return "memory"
}

func (a *inMemoryAddr) String() string {
	return ""
}

// CanonicalNetworkAddress parses the given network address and returns a
// string of the form "host:port", possibly filling it with the default port if
// it's missing.
func CanonicalNetworkAddress(address string) string {
	_, _, err := net.SplitHostPort(address)
	if err != nil {
		ip := net.ParseIP(address)
		if ip != nil && ip.To4() == nil {
			address = fmt.Sprintf("[%s]:%s", address, shared.DefaultPort)
		} else {
			address = fmt.Sprintf("%s:%s", address, shared.DefaultPort)
		}
	}
	return address
}

// ServerTLSConfig returns a new server-side tls.Config generated from the give
// certificate info.
func ServerTLSConfig(cert *shared.CertInfo) *tls.Config {
	config := shared.InitTLSConfig()
	config.ClientAuth = tls.RequestClientCert
	config.Certificates = []tls.Certificate{cert.KeyPair()}
	config.NextProtos = []string{"h2"} // Required by gRPC

	if cert.CA() != nil {
		pool := x509.NewCertPool()
		pool.AddCert(cert.CA())
		config.RootCAs = pool
		config.ClientCAs = pool

		logger.Infof("LXD is in CA mode, only CA-signed certificates will be allowed")
	}

	config.BuildNameToCertificate()
	return config
}

// NetworkInterfaceAddress returns the first non-loopback address of any of the
// system network interfaces.
//
// Return the empty string if none is found.
func NetworkInterfaceAddress() string {
	ifaces, err := net.Interfaces()
	if err != nil {
		return ""
	}

	for _, iface := range ifaces {
		addrs, err := iface.Addrs()
		if err != nil {
			continue
		}

		if len(addrs) == 0 {
			continue
		}

		for _, addr := range addrs {
			ipNet, ok := addr.(*net.IPNet)
			if !ok {
				continue
			}

			if !ipNet.IP.IsGlobalUnicast() {
				continue
			}

			return ipNet.IP.String()
		}
	}

	return ""
}

// IsAddressCovered detects if network address1 is actually covered by
// address2, in the sense that they are either the same address or address2 is
// specified using a wildcard with the same port of address1.
func IsAddressCovered(address1, address2 string) bool {
	address1 = CanonicalNetworkAddress(address1)
	address2 = CanonicalNetworkAddress(address2)

	if address1 == address2 {
		return true
	}

	host1, port1, err := net.SplitHostPort(address1)
	if err != nil {
		return false
	}

	host2, port2, err := net.SplitHostPort(address2)
	if err != nil {
		return false
	}

	// If the ports are different, then address1 is clearly not covered by
	// address2.
	if port2 != port1 {
		return false
	}

	// If address2 contains a host name, let's try to resolve it, in order
	// to compare the actual IPs.
	if host2 != "" {
		ip2 := net.ParseIP(host2)
		if ip2 == nil {
			ips, err := net.LookupHost(host2)
			if err == nil && len(ips) > 0 {
				host2 = ips[0]
			}
		}
	}

	if host2 == host1 {
		return true
	}

	// If address2 is using an IPv4 wildcard for the host, then address2 is
	// only covered if it's an IPv4 address.
	if host2 == "0.0.0.0" {
		ip1 := net.ParseIP(host1)
		if ip1 != nil && ip1.To4() != nil {
			return true
		}
		return false
	}

	// If address2 is using an IPv6 wildcard for the host, then address2 is
	// always covered.
	if host2 == "::" || host2 == "" {
		return true
	}

	return false
}

// SysctlGet retrieves the value of a sysctl file in /proc/sys.
func SysctlGet(path string) (string, error) {
	// Read the current content
	content, err := ioutil.ReadFile(fmt.Sprintf("/proc/sys/%s", path))
	if err != nil {
		return "", err
	}

	return string(content), nil
}

// SysctlSet writes a value to a sysctl file in /proc/sys.
func SysctlSet(path string, value string) error {
	// Get current value
	current, err := SysctlGet(path)
	if err == nil && current == value {
		// Nothing to update
		return nil
	}

	return ioutil.WriteFile(fmt.Sprintf("/proc/sys/%s", path), []byte(value), 0)
}
<commit_msg>lxd/net/util: Updates comment on NetworkInterfaceAddress behaviour change<commit_after>package util

import (
	"crypto/tls"
	"crypto/x509"
	"fmt"
	"io/ioutil"
	"net"

	"github.com/lxc/lxd/shared"
	"github.com/lxc/lxd/shared/logger"
)

// InMemoryNetwork creates a fully in-memory listener and dial function.
//
// Each time the dial function is invoked a new pair of net.Conn objects will
// be created using net.Pipe: the listener's Accept method will unblock and
// return one end of the pipe and the other end will be returned by the dial
// function.
func InMemoryNetwork() (net.Listener, func() net.Conn) {
	listener := &inMemoryListener{
		conns:  make(chan net.Conn, 16),
		closed: make(chan struct{}),
	}
	dialer := func() net.Conn {
		server, client := net.Pipe()
		listener.conns <- server
		return client
	}
	return listener, dialer
}

type inMemoryListener struct {
	conns  chan net.Conn
	closed chan struct{}
}

// Accept waits for and returns the next connection to the listener.
func (l *inMemoryListener) Accept() (net.Conn, error) {
	select {
	case conn := <-l.conns:
		return conn, nil
	case <-l.closed:
		return nil, fmt.Errorf("closed")
	}
}

// Close closes the listener.
// Any blocked Accept operations will be unblocked and return errors.
func (l *inMemoryListener) Close() error {
	close(l.closed)
	return nil
}

// Addr returns the listener's network address.
func (l *inMemoryListener) Addr() net.Addr {
	return &inMemoryAddr{}
}

type inMemoryAddr struct {
}

func (a *inMemoryAddr) Network() string {
	return "memory"
}

func (a *inMemoryAddr) String() string {
	return ""
}

// CanonicalNetworkAddress parses the given network address and returns a
// string of the form "host:port", possibly filling it with the default port if
// it's missing.
func CanonicalNetworkAddress(address string) string {
	_, _, err := net.SplitHostPort(address)
	if err != nil {
		ip := net.ParseIP(address)
		if ip != nil && ip.To4() == nil {
			address = fmt.Sprintf("[%s]:%s", address, shared.DefaultPort)
		} else {
			address = fmt.Sprintf("%s:%s", address, shared.DefaultPort)
		}
	}
	return address
}

// ServerTLSConfig returns a new server-side tls.Config generated from the give
// certificate info.
func ServerTLSConfig(cert *shared.CertInfo) *tls.Config {
	config := shared.InitTLSConfig()
	config.ClientAuth = tls.RequestClientCert
	config.Certificates = []tls.Certificate{cert.KeyPair()}
	config.NextProtos = []string{"h2"} // Required by gRPC

	if cert.CA() != nil {
		pool := x509.NewCertPool()
		pool.AddCert(cert.CA())
		config.RootCAs = pool
		config.ClientCAs = pool

		logger.Infof("LXD is in CA mode, only CA-signed certificates will be allowed")
	}

	config.BuildNameToCertificate()
	return config
}

// NetworkInterfaceAddress returns the first global unicast address of any of the system network interfaces.
// Return the empty string if none is found.
func NetworkInterfaceAddress() string {
	ifaces, err := net.Interfaces()
	if err != nil {
		return ""
	}

	for _, iface := range ifaces {
		addrs, err := iface.Addrs()
		if err != nil {
			continue
		}

		if len(addrs) == 0 {
			continue
		}

		for _, addr := range addrs {
			ipNet, ok := addr.(*net.IPNet)
			if !ok {
				continue
			}

			if !ipNet.IP.IsGlobalUnicast() {
				continue
			}

			return ipNet.IP.String()
		}
	}

	return ""
}

// IsAddressCovered detects if network address1 is actually covered by
// address2, in the sense that they are either the same address or address2 is
// specified using a wildcard with the same port of address1.
func IsAddressCovered(address1, address2 string) bool {
	address1 = CanonicalNetworkAddress(address1)
	address2 = CanonicalNetworkAddress(address2)

	if address1 == address2 {
		return true
	}

	host1, port1, err := net.SplitHostPort(address1)
	if err != nil {
		return false
	}

	host2, port2, err := net.SplitHostPort(address2)
	if err != nil {
		return false
	}

	// If the ports are different, then address1 is clearly not covered by
	// address2.
	if port2 != port1 {
		return false
	}

	// If address2 contains a host name, let's try to resolve it, in order
	// to compare the actual IPs.
	if host2 != "" {
		ip2 := net.ParseIP(host2)
		if ip2 == nil {
			ips, err := net.LookupHost(host2)
			if err == nil && len(ips) > 0 {
				host2 = ips[0]
			}
		}
	}

	if host2 == host1 {
		return true
	}

	// If address2 is using an IPv4 wildcard for the host, then address2 is
	// only covered if it's an IPv4 address.
	if host2 == "0.0.0.0" {
		ip1 := net.ParseIP(host1)
		if ip1 != nil && ip1.To4() != nil {
			return true
		}
		return false
	}

	// If address2 is using an IPv6 wildcard for the host, then address2 is
	// always covered.
	if host2 == "::" || host2 == "" {
		return true
	}

	return false
}

// SysctlGet retrieves the value of a sysctl file in /proc/sys.
func SysctlGet(path string) (string, error) {
	// Read the current content
	content, err := ioutil.ReadFile(fmt.Sprintf("/proc/sys/%s", path))
	if err != nil {
		return "", err
	}

	return string(content), nil
}

// SysctlSet writes a value to a sysctl file in /proc/sys.
func SysctlSet(path string, value string) error {
	// Get current value
	current, err := SysctlGet(path)
	if err == nil && current == value {
		// Nothing to update
		return nil
	}

	return ioutil.WriteFile(fmt.Sprintf("/proc/sys/%s", path), []byte(value), 0)
}
<|endoftext|>


<commit_before>package o

type HashSet struct {
	m    map[interface{}]struct{}
	size int
}

func NewHashSet() *HashSet {
	return NewHashSetCap(10)
}

func NewHashSetCap(size int) *HashSet {
	hashset := new(HashSet)
	hashset.m = make(map[interface{}]struct{}, size)
	return hashset
}

func (this *HashSet) Add(i interface{}) {
	this.m[i] = struct{}{}
	this.size++
}

func (this *HashSet) Remove(i interface{}) {
	delete(this.m, i)
	this.size--
}

func (this *HashSet) Has(i interface{}) bool {
	_, ok := this.m[i]
	return ok
}

func (this *HashSet) Size() int {
	return this.size
}

func (this *HashSet) Walk(f func(interface{}) error) error {
	for i := range this.m {
		err := f(i)
		if err != nil {
			return err
		}
	}
	return nil
}
<commit_msg>Modify hashset<commit_after>package o

type HashSet map[interface{}]struct{}

func NewHashSet() HashSet {
	return NewHashSetCap(1)
}

func NewHashSetCap(size int) HashSet {
	return make(map[interface{}]struct{}, size)
}

func (this HashSet) Add(i interface{}) {
	this[i] = struct{}{}
}

func (this HashSet) Remove(i interface{}) {
	delete(this, i)
}

func (this HashSet) Has(i interface{}) bool {
	_, ok := this[i]
	return ok
}

func (this HashSet) Size() int {
	return len(this)
}

func (this HashSet) Walk(f func(interface{}) error) error {
	for i := range this {
		err := f(i)
		if err != nil {
			return err
		}
	}
	return nil
}
<|endoftext|>


<commit_before>package fileservice

import "testing"

func TestPlaceholder(t *testing.T) {}
<commit_msg>Add test for NewLink() in fileservice package.<commit_after>package fileservice

import (
	"testing"
	"time"
)

func TestNewLink(t *testing.T) {
	now := time.Now()
	uri := "http://example.com"
	link := NewLink(uri, now)

	if link.Uri != uri {
		t.FailNow()
	}

	if link.Expire != now {
		t.FailNow()
	}
}
<|endoftext|>


<commit_before>// Copyright 2015, Joe Tsai. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE.md file.

package bits

import "io"

// Convert boolean false to 0 and true to 1.
func Btoi(b bool) uint {
	if b {
		return 1
	}
	return 0
}

// Convert integer to boolean false if equal to 0, otherwise true.
func Itob(i uint) bool {
	return i != 0
}

// Get the bit at a given offset.
func Get(buf []byte, ofs int) (val bool) {
	d := buf[ofs/8]
	m := byte(1 << uint(ofs%8))
	return d&m > 0
}

// Get the value of cnt bits at the given offset.
// Value is read in LSB-first order.
func GetN(buf []byte, cnt, ofs int) (val uint) {
	i := ofs / 8
	m := byte(1 << uint(ofs%8))
	for idx := 0; idx < cnt; idx++ {
		if buf[i]&m > 0 {
			val |= (0x01 << uint(idx))
		}
		m <<= 1
		if m == 0 {
			m = 0x01
			i++
		}
	}
	return
}

// Set the value of the bit at a given offset.
func Set(data []byte, val bool, ofs int) {
	if m := byte(1 << uint(ofs%8)); val {
		data[ofs/8] |= m
	} else {
		data[ofs/8] &= ^m
	}
}

// Set the value of cnt bits at the given offset.
// Value is written in LSB-first order.
func SetN(data []byte, val uint, cnt, ofs int) {
	i := ofs / 8
	m := byte(1 << uint(ofs%8))
	for idx := 0; idx < cnt; idx++ {
		if val&(0x01<<uint(idx)) > 0 {
			data[i] |= m
		} else {
			data[i] &= ^m
		}
		m <<= 1
		if m == 0 {
			m = 0x01
			i++
		}
	}
}

// Count the number of zeros and ones in the slice.
func Count(data []byte) (zeros, ones int) {
	for _, val := range data {
		for idx := 0; idx < 8 && val > 0; idx++ {
			ones += int(val & 1)
			val >>= 1
		}
	}
	return 8*len(data) - ones, ones
}

// Invert all bits in the slice.
func Invert(data []byte) {
	for idx := range data {
		data[idx] ^= 0xff
	}
}

// Reverse the bits of val.
func ReverseUint(val uint) uint {
	var w = uint(NumUintBits)
	var m = uint(MaxUint)
	for w > 1 {
		w >>= 1
		m ^= m >> w
		val = ((val & m) >> w) | ((val &^ m) << w)
	}
	return val
}

// Reverse the lower num bits of val. The upper bits will be zero.
func ReverseUintN(val uint, num int) uint {
	val = ReverseUint(val)
	val >>= uint(NumUintBits - num)
	return val
}

// Read num bits from br in LSB order. That is, the first bits read from br are
// packed into val as the LSB. The behavior is undefined if a read is attempted
// on more bits than fits in an uint.
//
// If an error is encountered while reading a bit, then an error will be
// returned along with the number of bits read thus far. The error returned will
// be io.EOF only if the cnt is 0. Otherwise, io.ErrUnexpectedEOF will be used.
// This is done to match the behavior of io.ReadFull.
func ReadBits(br BitReader, num int) (val uint, cnt int, err error) {
	var bit bool
	for cnt = 0; cnt < num; cnt++ {
		if bit, err = br.ReadBit(); err != nil {
			if err == io.EOF && cnt > 0 {
				err = io.ErrUnexpectedEOF
			}
			return
		}
		val |= (Btoi(bit) << uint(cnt))
	}
	return
}

// Write num bits to bw in LSB order. That is, the LSB bits of val will be the
// first bits to be written to bw. The behavior is undefined if a read is
// attempted on more bits than fits in an uint.
//
// If an error is encountered while writing a bit, then an error will be
// returned along with the number of bits written thus far.
func WriteBits(bw BitWriter, val uint, num int) (cnt int, err error) {
	var bit bool
	for cnt = 0; cnt < num; cnt++ {
		bit = Itob(val & (1 << uint(cnt)))
		if err = bw.WriteBit(bit); err != nil {
			return
		}
	}
	return
}
<commit_msg>Minor documentation changes for ReadBits and WriteBits<commit_after>// Copyright 2015, Joe Tsai. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE.md file.

package bits

import "io"

// Convert boolean false to 0 and true to 1.
func Btoi(b bool) uint {
	if b {
		return 1
	}
	return 0
}

// Convert integer to boolean false if equal to 0, otherwise true.
func Itob(i uint) bool {
	return i != 0
}

// Get the bit at a given offset.
func Get(buf []byte, ofs int) (val bool) {
	d := buf[ofs/8]
	m := byte(1 << uint(ofs%8))
	return d&m > 0
}

// Get the value of cnt bits at the given offset.
// Value is read in LSB-first order.
func GetN(buf []byte, cnt, ofs int) (val uint) {
	i := ofs / 8
	m := byte(1 << uint(ofs%8))
	for idx := 0; idx < cnt; idx++ {
		if buf[i]&m > 0 {
			val |= (0x01 << uint(idx))
		}
		m <<= 1
		if m == 0 {
			m = 0x01
			i++
		}
	}
	return
}

// Set the value of the bit at a given offset.
func Set(data []byte, val bool, ofs int) {
	if m := byte(1 << uint(ofs%8)); val {
		data[ofs/8] |= m
	} else {
		data[ofs/8] &= ^m
	}
}

// Set the value of cnt bits at the given offset.
// Value is written in LSB-first order.
func SetN(data []byte, val uint, cnt, ofs int) {
	i := ofs / 8
	m := byte(1 << uint(ofs%8))
	for idx := 0; idx < cnt; idx++ {
		if val&(0x01<<uint(idx)) > 0 {
			data[i] |= m
		} else {
			data[i] &= ^m
		}
		m <<= 1
		if m == 0 {
			m = 0x01
			i++
		}
	}
}

// Count the number of zeros and ones in the slice.
func Count(data []byte) (zeros, ones int) {
	for _, val := range data {
		for idx := 0; idx < 8 && val > 0; idx++ {
			ones += int(val & 1)
			val >>= 1
		}
	}
	return 8*len(data) - ones, ones
}

// Invert all bits in the slice.
func Invert(data []byte) {
	for idx := range data {
		data[idx] ^= 0xff
	}
}

// Reverse the bits of val.
func ReverseUint(val uint) uint {
	var w = uint(NumUintBits)
	var m = uint(MaxUint)
	for w > 1 {
		w >>= 1
		m ^= m >> w
		val = ((val & m) >> w) | ((val &^ m) << w)
	}
	return val
}

// Reverse the lower num bits of val. The upper bits will be zero.
func ReverseUintN(val uint, num int) uint {
	val = ReverseUint(val)
	val >>= uint(NumUintBits - num)
	return val
}

// This function allows a BitReader to easily satisfy the BitsReader interface.
func ReadBits(br BitReader, num int) (val uint, cnt int, err error) {
	// Since br.ReadBit is called fairly often and function calls become a
	// bottleneck, this logic is manually inlined in other bit readers.
	var bit bool
	for cnt = 0; cnt < num; cnt++ {
		if bit, err = br.ReadBit(); err != nil {
			if err == io.EOF && cnt > 0 {
				err = io.ErrUnexpectedEOF
			}
			return
		}
		val |= (Btoi(bit) << uint(cnt))
	}
	return
}

// This function allows a BitWriter to easily satisfy the BitsWriter interface.
func WriteBits(bw BitWriter, val uint, num int) (cnt int, err error) {
	// Since bw.WriteBit is called fairly often and function calls become a
	// bottleneck, this logic is manually inlined in other bit writers.
	var bit bool
	for cnt = 0; cnt < num; cnt++ {
		bit = Itob(val & (1 << uint(cnt)))
		if err = bw.WriteBit(bit); err != nil {
			return
		}
	}
	return
}
<|endoftext|>


<commit_before>package api

import (
	"encoding/json"
	"fmt"
	"github.com/PaluMacil/dwn/database"
	"github.com/PaluMacil/dwn/module/core"
	"github.com/PaluMacil/dwn/module/registration"
	"github.com/PaluMacil/dwn/webserver/errs"
	"net/http"
)

// POST /api/registration/user
func userHandler(
	db *database.Database,
	cur core.Current,
	vars map[string]string,
	w http.ResponseWriter,
	r *http.Request,
) error {
	var request registration.UserCreationRequest
	if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
		return err
	}
	exists, err := db.Users.VerifiedEmailExists(request.Email)
	if err != nil {
		return err
	}
	// TODO: finish validation
	validationErrors := request.Validate()
	if exists {
		alreadyExistsMessage := fmt.Sprintf("user with email %s already exists", request.Email)
		validationErrors = append(validationErrors, alreadyExistsMessage)
	}
	if len(validationErrors) > 0 {
		// TODO: fix 400 errors to hold more context, such as a string list
		return errs.StatusError{Code: 400, Err: fmt.Errorf("validation errors")}
	}
	userID, err := db.NextID()
	if err != nil {
		return err
	}
	user, err := request.User(userID)
	if err != nil {
		return err
	}
	err = db.Users.Set(user)
	if err != nil {
		return err
	}

	return json.NewEncoder(w).Encode(user.Info())
}
<commit_msg>enable creation of initial admin from registration form<commit_after>package api

import (
	"encoding/json"
	"fmt"
	"github.com/PaluMacil/dwn/database"
	"github.com/PaluMacil/dwn/module/core"
	"github.com/PaluMacil/dwn/module/registration"
	"github.com/PaluMacil/dwn/webserver/errs"
	"log"
	"net/http"
	"time"
)

// POST /api/registration/user
func userHandler(
	db *database.Database,
	cur core.Current,
	vars map[string]string,
	w http.ResponseWriter,
	r *http.Request,
) error {
	var request registration.UserCreationRequest
	if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
		return fmt.Errorf("decoding user creation request: %w", err)
	}
	exists, err := db.Users.VerifiedEmailExists(request.Email)
	if err != nil {
		return fmt.Errorf("checking if email %s exists and is verified: %w", request.Email, err)
	}
	// TODO: finish validation
	validationErrors := request.Validate()
	if exists {
		alreadyExistsMessage := fmt.Sprintf("user with email %s already exists", request.Email)
		validationErrors = append(validationErrors, alreadyExistsMessage)
	}
	if len(validationErrors) > 0 {
		// TODO: fix 400 errors to hold more context, such as a string list
		return errs.StatusError{Code: 400, Err: fmt.Errorf("validation errors")}
	}
	userID, err := db.NextID()
	if err != nil {
		return fmt.Errorf("getting next ID for user creation request: %w", err)
	}
	user, err := request.User(userID)
	if err != nil {
		return fmt.Errorf("converting user creation request into a user: %w", err)
	}
	config := db.Config.Get()
	err = db.Users.Set(user)
	if err != nil {
		return fmt.Errorf("saving newly created user: %w", err)
	}
	// if this is the initial admin, add it to the admin group; email and password must both match the one configured
	if config.Setup.InitialAdmin != "" && config.Setup.InitialAdmin == request.Email && config.Setup.InitialPassword == request.Password {
		err = db.UserGroups.Set(core.UserGroup{
			UserID:    userID,
			GroupName: core.BuiltInGroupAdmin,
		})
		if err != nil {
			return fmt.Errorf("saving initial admin user to admin group: %w", err)
		}
		user.Emails[0].Verified = true
		user.Emails[0].VerifiedDate = time.Now()
		err = db.Users.Set(user)
		if err != nil {
			return fmt.Errorf("updating admin user to verify email: %w", err)
		}
		log.Printf("user '%s' (%s) created as initial admin", user.DisplayName, user.PrimaryEmail)
	}

	return json.NewEncoder(w).Encode(user.Info())
}
<|endoftext|>


<commit_before>// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package backups_test

import (
	gc "launchpad.net/gocheck"

	"github.com/juju/juju/api/backups"
	"github.com/juju/juju/apiserver/params"
	backupstesting "github.com/juju/juju/state/backups/testing"
)

type createSuite struct {
	baseSuite
}

var _ = gc.Suite(&createSuite{})

func (s *createSuite) TestCreate(c *gc.C) {
	cleanup := backups.PatchClientFacadeCall(s.client,
		func(req string, paramsIn interface{}, resp interface{}) error {
			c.Check(req, gc.Equals, "Create")

			c.Assert(paramsIn, gc.FitsTypeOf, params.BackupsCreateArgs{})
			p := paramsIn.(params.BackupsCreateArgs)
			c.Check(p.Notes, gc.Equals, "important")

			if result, ok := resp.(*params.BackupsMetadataResult); ok {
				result.UpdateFromMetadata(s.Meta)
				result.Notes = p.Notes
			} else {
				c.Log("wrong output structure")
				c.Fail()
			}
			return nil
		},
	)
	defer cleanup()

	result, err := s.client.Create("important")
	c.Assert(err, gc.IsNil)

	meta := backupstesting.UpdateNotes(s.Meta, "important")
	s.checkMetadataResult(c, result, meta)
}
<commit_msg>Use Fatalf().<commit_after>// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package backups_test

import (
	gc "launchpad.net/gocheck"

	"github.com/juju/juju/api/backups"
	"github.com/juju/juju/apiserver/params"
	backupstesting "github.com/juju/juju/state/backups/testing"
)

type createSuite struct {
	baseSuite
}

var _ = gc.Suite(&createSuite{})

func (s *createSuite) TestCreate(c *gc.C) {
	cleanup := backups.PatchClientFacadeCall(s.client,
		func(req string, paramsIn interface{}, resp interface{}) error {
			c.Check(req, gc.Equals, "Create")

			c.Assert(paramsIn, gc.FitsTypeOf, params.BackupsCreateArgs{})
			p := paramsIn.(params.BackupsCreateArgs)
			c.Check(p.Notes, gc.Equals, "important")

			if result, ok := resp.(*params.BackupsMetadataResult); ok {
				result.UpdateFromMetadata(s.Meta)
				result.Notes = p.Notes
			} else {
				c.Fatalf("wrong output structure")
			}
			return nil
		},
	)
	defer cleanup()

	result, err := s.client.Create("important")
	c.Assert(err, gc.IsNil)

	meta := backupstesting.UpdateNotes(s.Meta, "important")
	s.checkMetadataResult(c, result, meta)
}
<|endoftext|>


<commit_before>package gossip

import "fmt"
import "net"

type SipHeader interface {
    String() (string)
}

type Uri interface {
    String() (string)
}

type SipUri interface {
    String() (string)
    HostPart() (string)
    DomainPart() (string)
}

type PlainSipUri struct {
    host string
    domain string
}
func (uri *PlainSipUri) String() (string) {
    return fmt.Sprintf("sip:%s@%s", uri.host, uri.domain)
}
func (uri *PlainSipUri) HostPart() (string) {
    return uri.host
}
func (uri *PlainSipUri) DomainPart() (string) {
    return uri.domain
}

type SipsUri struct {
    host string
    domain string
}
func (uri *SipsUri) String() (string) {
    return fmt.Sprintf("sips:%s@%s", uri.host, uri.domain)
}
func (uri *SipsUri) HostPart() (string) {
    return uri.host
}
func (uri *SipsUri) DomainPart() (string) {
    return uri.domain
}

type TelUri struct {
    // TODO
}

type ToHeader struct {
    displayName string
    uri Uri
}

type FromHeader struct {
    displayName string
    uri Uri
}

type CSeq uint32
func (cseq *CSeq) String() (string) {
    return fmt.Sprintf("CSeq: %d", ((int)(*cseq)))
}

type MaxForwards uint32
func (maxForwards *MaxForwards) Strip() (string) {
    return fmt.Sprintf("Max-Forwards: %d", ((int)(*maxForwards)))
}

type ViaHeader struct {
    transport string
    address net.Addr
    port uint8
    hasBranch bool
    branch string
    hasReceived bool
    received net.IP
}
func (via *ViaHeader) String() (string) {
    result := fmt.Sprintf("Via: %s %s:%d", via.transport,
                          via.address.String(), via.port)
    if (via.hasReceived) {
        result += ";received=" + via.received.String()
    }
    if (via.hasBranch) {
        result += ";branch=" + via.branch
    }

    return result
}

type ContactHeader struct  {
    uri SipUri
}
func (contactHeader *ContactHeader) String() (string) {
    return fmt.Sprintf("Contact: <%s>", contactHeader.uri.String())
}
<commit_msg>Add CallId header to headers.go.<commit_after>package gossip

import "fmt"
import "net"

type SipHeader interface {
    String() (string)
}

type Uri interface {
    String() (string)
}

type SipUri interface {
    String() (string)
    HostPart() (string)
    DomainPart() (string)
}

type PlainSipUri struct {
    host string
    domain string
}
func (uri *PlainSipUri) String() (string) {
    return fmt.Sprintf("sip:%s@%s", uri.host, uri.domain)
}
func (uri *PlainSipUri) HostPart() (string) {
    return uri.host
}
func (uri *PlainSipUri) DomainPart() (string) {
    return uri.domain
}

type SipsUri struct {
    host string
    domain string
}
func (uri *SipsUri) String() (string) {
    return fmt.Sprintf("sips:%s@%s", uri.host, uri.domain)
}
func (uri *SipsUri) HostPart() (string) {
    return uri.host
}
func (uri *SipsUri) DomainPart() (string) {
    return uri.domain
}

type TelUri struct {
    // TODO
}

type ToHeader struct {
    displayName string
    uri Uri
}

type FromHeader struct {
    displayName string
    uri Uri
}

type CallId string
func (callId *CallId) String() (string) {
    return (string)(*callId)
}

type CSeq uint32
func (cseq *CSeq) String() (string) {
    return fmt.Sprintf("CSeq: %d", ((int)(*cseq)))
}

type MaxForwards uint32
func (maxForwards *MaxForwards) Strip() (string) {
    return fmt.Sprintf("Max-Forwards: %d", ((int)(*maxForwards)))
}

type ViaHeader struct {
    transport string
    address net.Addr
    port uint8
    hasBranch bool
    branch string
    hasReceived bool
    received net.IP
}
func (via *ViaHeader) String() (string) {
    result := fmt.Sprintf("Via: %s %s:%d", via.transport,
                          via.address.String(), via.port)
    if (via.hasReceived) {
        result += ";received=" + via.received.String()
    }
    if (via.hasBranch) {
        result += ";branch=" + via.branch
    }

    return result
}

type ContactHeader struct  {
    uri SipUri
}
func (contactHeader *ContactHeader) String() (string) {
    return fmt.Sprintf("Contact: <%s>", contactHeader.uri.String())
}
<|endoftext|>


<commit_before>package main

import (
    "fmt"
    "time"
    "github.com/nitram509/led"
    "image/color"
    "math/rand"
    "os"
    "sort"
    "strings"
    "regexp"
    "encoding/hex"
    "gopkg.in/alecthomas/kingpin.v1"
)

var (
    VERSION = "0.0.1"
    flagSetColor = kingpin.Flag("set-color", "Set color for device. The format must be \"#rrggbb\" or \"random\" or an HTML color name, like \"green\"").String()
    flagListColors = kingpin.Flag("list-colors", "List all available HTML color names").Bool()
)

func printListColorNames() {
    var colorNames []string
    for k := range colors {
        colorNames = append(colorNames, k)
    }
    sort.Strings(colorNames)
    for k := range colorNames {
        if (k > 0) {
            fmt.Print(",")
        }
        fmt.Print(colorNames[k])
    }
}

func getFlagColor() color.Color {
    *flagSetColor = strings.ToLower(*flagSetColor)
    if (*flagSetColor == "random") {
        rand.Seed(time.Now().UnixNano())
        return color.RGBA{uint8(rand.Int()), uint8(rand.Int()), uint8(rand.Int()), 0xFF}
    }
    if (colors[*flagSetColor] != nil) {
        return colors[*flagSetColor]
    }
    validHexCode := regexp.MustCompile(`^#?([a-f0-9]{6})$`)
    if (validHexCode.MatchString(*flagSetColor)) {
        hexStr := validHexCode.FindStringSubmatch(*flagSetColor)
        bytes, err := hex.DecodeString(hexStr[1])
        if (err != nil) {
            fmt.Printf("invalid color code '%s'. use '#rrggbb' instead", hexStr[1])
            return nil
        }
        return color.RGBA{uint8(bytes[0]), uint8(bytes[1]), uint8(bytes[2]), 0xFF}
    }
    return nil
}

func main() {

    kingpin.Version(VERSION)
    kingpin.Parse()

    if (len(os.Args) <= 1) {
        kingpin.Usage()
        os.Exit(0)
    }

    if (flagListColors != nil && *flagListColors) {
        printListColorNames()
        os.Exit(0)
    }

    if (flagSetColor == nil) {
        os.Exit(0)
    }

    for devInfo := range led.Devices() {
        dev, err := devInfo.Open()
        if err != nil {
            fmt.Println(err)
            continue
        }
        defer func() {
            dev.Close()
        }()

        col := getFlagColor()
        if (col != nil) {
            dev.SetColor(col)
        }

    }

}
<commit_msg>added list-devices feature<commit_after>package main

import (
    "fmt"
    "time"
    "github.com/nitram509/led"
    "image/color"
    "math/rand"
    "os"
    "sort"
    "strings"
    "regexp"
    "encoding/hex"
    "gopkg.in/alecthomas/kingpin.v1"
)

var (
    VERSION = "0.0.1"
    flagSetColor = kingpin.Flag("set-color", "Set color for device. The format must be \"#rrggbb\" or \"random\" or an HTML color name, like \"green\"").String()
    flagListColors = kingpin.Flag("list-colors", "List all available HTML color names").Bool()
    flagListDevices = kingpin.Flag("list-devices", "List all connected devices").Short('l').Bool()
)

func printListColorNames() {
    var colorNames []string
    for k := range colors {
        colorNames = append(colorNames, k)
    }
    sort.Strings(colorNames)
    for k := range colorNames {
        if (k > 0) {
            fmt.Print(",")
        }
        fmt.Print(colorNames[k])
    }
}

func printListDevices() {
    var i int = 0
    for devInfo := range led.Devices() {
        fmt.Printf("%d\t%s", i,  devInfo.GetType().String())
    }
}

func getFlagColor() color.Color {
    col := strings.ToLower(*flagSetColor)
    if (col == "random") {
        rand.Seed(time.Now().UnixNano())
        return color.RGBA{uint8(rand.Int()), uint8(rand.Int()), uint8(rand.Int()), 0xFF}
    }
    if (colors[col] != nil) {
        return colors[col]
    }
    validHexCode := regexp.MustCompile(`^#?([a-f0-9]{6})$`)
    if (validHexCode.MatchString(col)) {
        hexStr := validHexCode.FindStringSubmatch(col)
        bytes, err := hex.DecodeString(hexStr[1])
        if (err != nil) {
            fmt.Printf("invalid color code '%s'. use '#rrggbb' instead", hexStr[1])
            return nil
        }
        return color.RGBA{uint8(bytes[0]), uint8(bytes[1]), uint8(bytes[2]), 0xFF}
    }
    return nil
}

func main() {

    kingpin.Version(VERSION)
    kingpin.Parse()

    if (len(os.Args) <= 1) {
        kingpin.Usage()
        os.Exit(0)
    }

    if (flagListColors != nil && *flagListColors) {
        printListColorNames()
        os.Exit(0)
    }

    if (flagListDevices != nil && *flagListDevices) {
        printListDevices()
        os.Exit(0)
    }

    if (flagSetColor == nil) {
        os.Exit(0)
    }

    for devInfo := range led.Devices() {
        dev, err := devInfo.Open()
        if err != nil {
            fmt.Println(err)
            continue
        }
        defer func() {
            dev.Close()
        }()

        col := getFlagColor()
        if (col != nil) {
            dev.SetColor(col)
        }

    }

}
<|endoftext|>


<commit_before>// Copyright 2015 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package api

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
	"time"

	"github.com/tsuru/tsuru/auth"
	"github.com/tsuru/tsuru/db"
	"github.com/tsuru/tsuru/errors"
	"github.com/tsuru/tsuru/io"
	"github.com/tsuru/tsuru/rec"
	"github.com/tsuru/tsuru/service"
	"gopkg.in/mgo.v2/bson"
)

func createServiceInstance(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	b, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return err
	}
	var body map[string]string
	err = json.Unmarshal(b, &body)
	if err != nil {
		return err
	}
	serviceName := body["service_name"]
	user, err := t.User()
	if err != nil {
		return err
	}
	rec.Log(user.Email, "create-service-instance", string(b))
	srv, err := getServiceOrError(serviceName, user)
	if err != nil {
		return &errors.HTTP{Code: http.StatusNotFound, Message: err.Error()}
	}
	instance := service.ServiceInstance{
		Name:      body["name"],
		PlanName:  body["plan"],
		TeamOwner: body["owner"],
	}
	return service.CreateServiceInstance(instance, &srv, user)
}

func removeServiceInstance(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	u, err := t.User()
	if err != nil {
		return err
	}
	unbindAll := r.URL.Query().Get("unbindall")
	name := r.URL.Query().Get(":name")
	rec.Log(u.Email, "remove-service-instance", name)
	si, err := getServiceInstanceOrError(name, u)
	if err != nil {
		return err
	}
	keepAliveWriter := io.NewKeepAliveWriter(w, 30*time.Second, "")
	defer keepAliveWriter.Stop()
	writer := &io.SimpleJsonMessageEncoderWriter{Encoder: json.NewEncoder(keepAliveWriter)}
	if unbindAll == "true" {
		if len(si.Apps) > 0 {
			for _, appName := range si.Apps {
				var app *app.App
				_, app, err = getServiceInstance(si.Name, appName, u)
				if err != nil {
					writer.Encode(io.SimpleJsonMessage{Error: err.Error()})
					return nil
				}
				fmt.Fprintf(writer, "Unbind app %q ...\n", app.GetName())
				err = si.UnbindApp(app, writer)
				if err != nil {
					writer.Encode(io.SimpleJsonMessage{Error: err.Error()})
					return nil
				}
				fmt.Fprintf(writer, "\nInstance %q is not bound to the app %q anymore.\n", si.Name, app.GetName())
			}
			si, err = getServiceInstanceOrError(name, u)
			if err != nil {
				return err
			}
		}
	}

	err = service.DeleteInstance(si)
	if err != nil {
		var msg string
		if err == service.ErrServiceInstanceBound {
			msg = strings.Join(si.Apps, ",")
		}
		writer.Encode(io.SimpleJsonMessage{Message: msg, Error: err.Error()})
		return nil
	}
	writer.Write([]byte("service instance successfuly removed"))
	return nil
}

func serviceInstances(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	u, err := t.User()
	if err != nil {
		return err
	}
	appName := r.URL.Query().Get("app")
	rec.Log(u.Email, "list-service-instances", "app="+appName)
	services, _ := service.GetServicesByTeamKindAndNoRestriction("teams", u)
	sInstances, _ := service.GetServiceInstancesByServicesAndTeams(services, u, appName)
	result := make([]service.ServiceModel, len(services))
	for i, s := range services {
		result[i].Service = s.Name
		result[i].Instances = []string{}
		for _, si := range sInstances {
			if si.ServiceName == s.Name {
				result[i].Instances = append(result[i].Instances, si.Name)
			}
		}
	}
	body, err := json.Marshal(result)
	if err != nil {
		return err
	}
	n, err := w.Write(body)
	if n != len(body) {
		return &errors.HTTP{Code: http.StatusInternalServerError, Message: "Failed to write the response body."}
	}
	return err
}

func serviceInstance(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	u, err := t.User()
	if err != nil {
		return err
	}
	instance, err := getServiceInstanceOrError(r.URL.Query().Get(":name"), u)
	if err != nil {
		return err
	}
	return json.NewEncoder(w).Encode(instance)
}

func serviceInstanceStatus(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	u, err := t.User()
	if err != nil {
		return err
	}
	siName := r.URL.Query().Get(":instance")
	si, err := getServiceInstanceOrError(siName, u)
	if err != nil {
		return err
	}
	rec.Log(u.Email, "service-instance-status", siName)
	var b string
	if b, err = si.Status(); err != nil {
		msg := fmt.Sprintf("Could not retrieve status of service instance, error: %s", err)
		return &errors.HTTP{Code: http.StatusInternalServerError, Message: msg}
	}
	b = fmt.Sprintf(`Service instance "%s" is %s`, siName, b)
	n, err := w.Write([]byte(b))
	if n != len(b) {
		return &errors.HTTP{Code: http.StatusInternalServerError, Message: "Failed to write response body"}
	}
	return nil
}

func serviceInfo(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	u, err := t.User()
	if err != nil {
		return err
	}
	serviceName := r.URL.Query().Get(":name")
	rec.Log(u.Email, "service-info", serviceName)
	_, err = getServiceOrError(serviceName, u)
	if err != nil {
		return err
	}
	instances := []service.ServiceInstance{}
	teams, err := u.Teams()
	if err != nil {
		return err
	}
	conn, err := db.Conn()
	if err != nil {
		return err
	}
	defer conn.Close()
	teamsNames := auth.GetTeamsNames(teams)
	q := bson.M{"service_name": serviceName, "teams": bson.M{"$in": teamsNames}}
	err = conn.ServiceInstances().Find(q).All(&instances)
	if err != nil {
		return err
	}
	b, err := json.Marshal(instances)
	if err != nil {
		return nil
	}
	w.Write(b)
	return nil
}

func serviceDoc(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	u, err := t.User()
	if err != nil {
		return err
	}
	sName := r.URL.Query().Get(":name")
	rec.Log(u.Email, "service-doc", sName)
	s, err := getServiceOrError(sName, u)
	if err != nil {
		return err
	}
	w.Write([]byte(s.Doc))
	return nil
}

func getServiceOrError(name string, u *auth.User) (service.Service, error) {
	s := service.Service{Name: name}
	err := s.Get()
	if err != nil {
		return s, &errors.HTTP{Code: http.StatusNotFound, Message: "Service not found"}
	}
	if !s.IsRestricted {
		return s, nil
	}
	if !auth.CheckUserAccess(s.Teams, u) {
		msg := "This user does not have access to this service"
		return s, &errors.HTTP{Code: http.StatusForbidden, Message: msg}
	}
	return s, err
}

func getServiceInstanceOrError(name string, u *auth.User) (*service.ServiceInstance, error) {
	si, err := service.GetServiceInstance(name, u)
	if err != nil {
		switch err {
		case service.ErrServiceInstanceNotFound:
			return nil, &errors.HTTP{
				Code:    http.StatusNotFound,
				Message: err.Error(),
			}
		case service.ErrAccessNotAllowed:
			return nil, &errors.HTTP{
				Code:    http.StatusForbidden,
				Message: err.Error(),
			}
		default:
			return nil, err
		}
	}
	return si, nil
}

func servicePlans(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	u, err := t.User()
	if err != nil {
		return err
	}
	serviceName := r.URL.Query().Get(":name")
	rec.Log(u.Email, "service-plans", serviceName)
	plans, err := service.GetPlansByServiceName(serviceName)
	if err != nil {
		return err
	}
	b, err := json.Marshal(plans)
	if err != nil {
		return nil
	}
	w.Write(b)
	return nil
}

func serviceInstanceProxy(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	u, err := t.User()
	if err != nil {
		return err
	}
	siName := r.URL.Query().Get(":instance")
	si, err := getServiceInstanceOrError(siName, u)
	if err != nil {
		return err
	}
	path := r.URL.Query().Get("callback")
	rec.Log(u.Email, "service-instance-proxy", siName, path)
	return service.Proxy(si.Service(), path, w, r)
}

func serviceInstanceGrantTeam(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	u, err := t.User()
	if err != nil {
		return err
	}
	siName := r.URL.Query().Get(":instance")
	si, err := getServiceInstanceOrError(siName, u)
	if err != nil {
		return err
	}
	teamName := r.URL.Query().Get(":team")
	rec.Log(u.Email, "service-grant-team", siName, teamName)
	return si.Grant(teamName)
}

func serviceInstanceRevokeTeam(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	u, err := t.User()
	if err != nil {
		return err
	}
	siName := r.URL.Query().Get(":instance")
	si, err := getServiceInstanceOrError(siName, u)
	if err != nil {
		return err
	}
	teamName := r.URL.Query().Get(":team")
	rec.Log(u.Email, "service-revoke-team", siName, teamName)
	return si.Revoke(teamName)
}
<commit_msg>api: improve handling of vet error<commit_after>// Copyright 2015 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package api

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
	"time"

	"github.com/tsuru/tsuru/auth"
	"github.com/tsuru/tsuru/db"
	"github.com/tsuru/tsuru/errors"
	"github.com/tsuru/tsuru/io"
	"github.com/tsuru/tsuru/rec"
	"github.com/tsuru/tsuru/service"
	"gopkg.in/mgo.v2/bson"
)

func createServiceInstance(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	b, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return err
	}
	var body map[string]string
	err = json.Unmarshal(b, &body)
	if err != nil {
		return err
	}
	serviceName := body["service_name"]
	user, err := t.User()
	if err != nil {
		return err
	}
	rec.Log(user.Email, "create-service-instance", string(b))
	srv, err := getServiceOrError(serviceName, user)
	if err != nil {
		return &errors.HTTP{Code: http.StatusNotFound, Message: err.Error()}
	}
	instance := service.ServiceInstance{
		Name:      body["name"],
		PlanName:  body["plan"],
		TeamOwner: body["owner"],
	}
	return service.CreateServiceInstance(instance, &srv, user)
}

func removeServiceInstance(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	u, err := t.User()
	if err != nil {
		return err
	}
	unbindAll := r.URL.Query().Get("unbindall")
	name := r.URL.Query().Get(":name")
	rec.Log(u.Email, "remove-service-instance", name)
	si, err := getServiceInstanceOrError(name, u)
	if err != nil {
		return err
	}
	keepAliveWriter := io.NewKeepAliveWriter(w, 30*time.Second, "")
	defer keepAliveWriter.Stop()
	writer := &io.SimpleJsonMessageEncoderWriter{Encoder: json.NewEncoder(keepAliveWriter)}
	if unbindAll == "true" {
		if len(si.Apps) > 0 {
			for _, appName := range si.Apps {
				_, app, instErr := getServiceInstance(si.Name, appName, u)
				if instErr != nil {
					writer.Encode(io.SimpleJsonMessage{Error: instErr.Error()})
					return nil
				}
				fmt.Fprintf(writer, "Unbind app %q ...\n", app.GetName())
				instErr = si.UnbindApp(app, writer)
				if instErr != nil {
					writer.Encode(io.SimpleJsonMessage{Error: instErr.Error()})
					return nil
				}
				fmt.Fprintf(writer, "\nInstance %q is not bound to the app %q anymore.\n", si.Name, app.GetName())
			}
			si, instErr = getServiceInstanceOrError(name, u)
			if instErr != nil {
				return instErr
			}
		}
	}

	err = service.DeleteInstance(si)
	if err != nil {
		var msg string
		if err == service.ErrServiceInstanceBound {
			msg = strings.Join(si.Apps, ",")
		}
		writer.Encode(io.SimpleJsonMessage{Message: msg, Error: err.Error()})
		return nil
	}
	writer.Write([]byte("service instance successfuly removed"))
	return nil
}

func serviceInstances(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	u, err := t.User()
	if err != nil {
		return err
	}
	appName := r.URL.Query().Get("app")
	rec.Log(u.Email, "list-service-instances", "app="+appName)
	services, _ := service.GetServicesByTeamKindAndNoRestriction("teams", u)
	sInstances, _ := service.GetServiceInstancesByServicesAndTeams(services, u, appName)
	result := make([]service.ServiceModel, len(services))
	for i, s := range services {
		result[i].Service = s.Name
		result[i].Instances = []string{}
		for _, si := range sInstances {
			if si.ServiceName == s.Name {
				result[i].Instances = append(result[i].Instances, si.Name)
			}
		}
	}
	body, err := json.Marshal(result)
	if err != nil {
		return err
	}
	n, err := w.Write(body)
	if n != len(body) {
		return &errors.HTTP{Code: http.StatusInternalServerError, Message: "Failed to write the response body."}
	}
	return err
}

func serviceInstance(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	u, err := t.User()
	if err != nil {
		return err
	}
	instance, err := getServiceInstanceOrError(r.URL.Query().Get(":name"), u)
	if err != nil {
		return err
	}
	return json.NewEncoder(w).Encode(instance)
}

func serviceInstanceStatus(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	u, err := t.User()
	if err != nil {
		return err
	}
	siName := r.URL.Query().Get(":instance")
	si, err := getServiceInstanceOrError(siName, u)
	if err != nil {
		return err
	}
	rec.Log(u.Email, "service-instance-status", siName)
	var b string
	if b, err = si.Status(); err != nil {
		msg := fmt.Sprintf("Could not retrieve status of service instance, error: %s", err)
		return &errors.HTTP{Code: http.StatusInternalServerError, Message: msg}
	}
	b = fmt.Sprintf(`Service instance "%s" is %s`, siName, b)
	n, err := w.Write([]byte(b))
	if n != len(b) {
		return &errors.HTTP{Code: http.StatusInternalServerError, Message: "Failed to write response body"}
	}
	return nil
}

func serviceInfo(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	u, err := t.User()
	if err != nil {
		return err
	}
	serviceName := r.URL.Query().Get(":name")
	rec.Log(u.Email, "service-info", serviceName)
	_, err = getServiceOrError(serviceName, u)
	if err != nil {
		return err
	}
	instances := []service.ServiceInstance{}
	teams, err := u.Teams()
	if err != nil {
		return err
	}
	conn, err := db.Conn()
	if err != nil {
		return err
	}
	defer conn.Close()
	teamsNames := auth.GetTeamsNames(teams)
	q := bson.M{"service_name": serviceName, "teams": bson.M{"$in": teamsNames}}
	err = conn.ServiceInstances().Find(q).All(&instances)
	if err != nil {
		return err
	}
	b, err := json.Marshal(instances)
	if err != nil {
		return nil
	}
	w.Write(b)
	return nil
}

func serviceDoc(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	u, err := t.User()
	if err != nil {
		return err
	}
	sName := r.URL.Query().Get(":name")
	rec.Log(u.Email, "service-doc", sName)
	s, err := getServiceOrError(sName, u)
	if err != nil {
		return err
	}
	w.Write([]byte(s.Doc))
	return nil
}

func getServiceOrError(name string, u *auth.User) (service.Service, error) {
	s := service.Service{Name: name}
	err := s.Get()
	if err != nil {
		return s, &errors.HTTP{Code: http.StatusNotFound, Message: "Service not found"}
	}
	if !s.IsRestricted {
		return s, nil
	}
	if !auth.CheckUserAccess(s.Teams, u) {
		msg := "This user does not have access to this service"
		return s, &errors.HTTP{Code: http.StatusForbidden, Message: msg}
	}
	return s, err
}

func getServiceInstanceOrError(name string, u *auth.User) (*service.ServiceInstance, error) {
	si, err := service.GetServiceInstance(name, u)
	if err != nil {
		switch err {
		case service.ErrServiceInstanceNotFound:
			return nil, &errors.HTTP{
				Code:    http.StatusNotFound,
				Message: err.Error(),
			}
		case service.ErrAccessNotAllowed:
			return nil, &errors.HTTP{
				Code:    http.StatusForbidden,
				Message: err.Error(),
			}
		default:
			return nil, err
		}
	}
	return si, nil
}

func servicePlans(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	u, err := t.User()
	if err != nil {
		return err
	}
	serviceName := r.URL.Query().Get(":name")
	rec.Log(u.Email, "service-plans", serviceName)
	plans, err := service.GetPlansByServiceName(serviceName)
	if err != nil {
		return err
	}
	b, err := json.Marshal(plans)
	if err != nil {
		return nil
	}
	w.Write(b)
	return nil
}

func serviceInstanceProxy(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	u, err := t.User()
	if err != nil {
		return err
	}
	siName := r.URL.Query().Get(":instance")
	si, err := getServiceInstanceOrError(siName, u)
	if err != nil {
		return err
	}
	path := r.URL.Query().Get("callback")
	rec.Log(u.Email, "service-instance-proxy", siName, path)
	return service.Proxy(si.Service(), path, w, r)
}

func serviceInstanceGrantTeam(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	u, err := t.User()
	if err != nil {
		return err
	}
	siName := r.URL.Query().Get(":instance")
	si, err := getServiceInstanceOrError(siName, u)
	if err != nil {
		return err
	}
	teamName := r.URL.Query().Get(":team")
	rec.Log(u.Email, "service-grant-team", siName, teamName)
	return si.Grant(teamName)
}

func serviceInstanceRevokeTeam(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	u, err := t.User()
	if err != nil {
		return err
	}
	siName := r.URL.Query().Get(":instance")
	si, err := getServiceInstanceOrError(siName, u)
	if err != nil {
		return err
	}
	teamName := r.URL.Query().Get(":team")
	rec.Log(u.Email, "service-revoke-team", siName, teamName)
	return si.Revoke(teamName)
}
<|endoftext|>


<commit_before>package Golf

import (
	"encoding/json"
	"errors"
	"net/http"
	"time"
)

type Context struct {
	// http.Request
	Request *http.Request

	// http.ResponseWriter
	Response http.ResponseWriter

	// URL Parameter
	Params map[string]string

	// HTTP status code
	StatusCode int

	// HTTP header as a map
	Header map[string]string

	// HTTP response body as a byte string
	Body []byte

	// The application
	App *Application

	// Data used for sharing values between middlewares
	Data map[string]interface{}

	// Indicating if the response is already sent.
	IsSent bool

	// Indicating loader of the template
	templateLoader string
}

func NewContext(req *http.Request, res http.ResponseWriter, app *Application) *Context {
	ctx := new(Context)
	ctx.Request = req
	ctx.Response = res
	ctx.App = app
	ctx.Header = make(map[string]string)
	ctx.StatusCode = 200
	ctx.Header["Content-Type"] = "text/html;charset=UTF-8"
	ctx.Request.ParseForm()
	ctx.IsSent = false
	ctx.Data = make(map[string]interface{})
	return ctx
}

// Retrieving the form data, return empty string if not found.
func (ctx *Context) Query(key string, index ...int) (string, error) {
	if val, ok := ctx.Request.Form[key]; ok {
		if len(index) == 1 {
			return val[index[0]], nil
		} else {
			return val[0], nil
		}
	} else {
		return "", errors.New("Query key not found.")
	}
}

// Retrieving the parameters from url
// If the url is /:id/, then id can be retrieved by calling `ctx.Param(id)`
func (ctx *Context) Param(key string) (string, error) {
	if val, ok := ctx.Params[key]; ok {
		return val, nil
	} else {
		return "", errors.New("Parameter not found.")
	}
}

// Make a 301 redirection
// If you want a 302 redirection, please do it by setting the Header
func (ctx *Context) Redirect(url string) {
	ctx.Header["Location"] = url
	ctx.StatusCode = 301
}

// Set Cookie for the request. If expire is 0, create a session cookie.
func (ctx *Context) SetCookie(key string, value string, expire int) {
	now := time.Now()
	cookie := &http.Cookie{
		Name:   key,
		Value:  value,
		Path:   "/",
		MaxAge: expire,
	}
	if expire != 0 {
		expireTime := now.Add(time.Duration(expire) * time.Second)
		cookie.Expires = expireTime
	}
	http.SetCookie(ctx.Response, cookie)
}

// Sends a JSON response.
func (ctx *Context) JSON(obj interface{}) {
	json, err := json.Marshal(obj)
	if err != nil {
		panic(err)
	}
	ctx.Body = json
	ctx.Header["Content-Type"] = "application/json"
}

// Send the response immediately. Set `ctx.IsSent` to `true` to make
// sure that the response won't be sent twice.
func (ctx *Context) Send() {
	if ctx.IsSent {
		return
	}
	for name, value := range ctx.Header {
		ctx.Response.Header().Set(name, value)
	}
	ctx.Response.WriteHeader(ctx.StatusCode)
	ctx.Response.Write(ctx.Body)
	ctx.IsSent = true
}

// Write text on the response body.
func (ctx *Context) Write(content string) {
	ctx.Body = []byte(content)
}

// Return a 404 not found response.
// Retuns an HTTP Error by indicating the status code, the corresponding
// handler inside `App.errorHandler` will be called, if user does not set
// the corresponding error handler, the defaultErrorHandler will be called.
func (ctx *Context) Error(statusCode int) {
	ctx.StatusCode = 404
	ctx.App.handleError(ctx, statusCode)
}

// Set the template loader for this context. This should be done before calling
// `ctx.Render`.
func (ctx *Context) Loader(name string) *Context {
	ctx.templateLoader = name
	return ctx
}

// Render a template file using the built-in Go template engine.
func (ctx *Context) Render(file string, data interface{}) {
	content, e := ctx.App.View.Render(ctx.templateLoader, file, data)
	if e != nil {
		panic(e)
	}
	ctx.Body = []byte(content)
}

func (ctx *Context) RenderFromString(tplSrc string, data interface{}) {
	content, e := ctx.App.View.RenderFromString(ctx.templateLoader, tplSrc, data)
	if e != nil {
		panic(e)
	}
	ctx.Body = []byte(content)
}
<commit_msg>Change `Context.Error` to `Context.Abort`, and fix the problem that context always set the status code to 404.<commit_after>package Golf

import (
	"encoding/json"
	"errors"
	"net/http"
	"time"
)

type Context struct {
	// http.Request
	Request *http.Request

	// http.ResponseWriter
	Response http.ResponseWriter

	// URL Parameter
	Params map[string]string

	// HTTP status code
	StatusCode int

	// HTTP header as a map
	Header map[string]string

	// HTTP response body as a byte string
	Body []byte

	// The application
	App *Application

	// Data used for sharing values between middlewares
	Data map[string]interface{}

	// Indicating if the response is already sent.
	IsSent bool

	// Indicating loader of the template
	templateLoader string
}

func NewContext(req *http.Request, res http.ResponseWriter, app *Application) *Context {
	ctx := new(Context)
	ctx.Request = req
	ctx.Response = res
	ctx.App = app
	ctx.Header = make(map[string]string)
	ctx.StatusCode = 200
	ctx.Header["Content-Type"] = "text/html;charset=UTF-8"
	ctx.Request.ParseForm()
	ctx.IsSent = false
	ctx.Data = make(map[string]interface{})
	return ctx
}

// Retrieving the form data, return empty string if not found.
func (ctx *Context) Query(key string, index ...int) (string, error) {
	if val, ok := ctx.Request.Form[key]; ok {
		if len(index) == 1 {
			return val[index[0]], nil
		} else {
			return val[0], nil
		}
	} else {
		return "", errors.New("Query key not found.")
	}
}

// Retrieving the parameters from url
// If the url is /:id/, then id can be retrieved by calling `ctx.Param(id)`
func (ctx *Context) Param(key string) (string, error) {
	if val, ok := ctx.Params[key]; ok {
		return val, nil
	} else {
		return "", errors.New("Parameter not found.")
	}
}

// Make a 301 redirection
// If you want a 302 redirection, please do it by setting the Header
func (ctx *Context) Redirect(url string) {
	ctx.Header["Location"] = url
	ctx.StatusCode = 301
}

// Set Cookie for the request. If expire is 0, create a session cookie.
func (ctx *Context) SetCookie(key string, value string, expire int) {
	now := time.Now()
	cookie := &http.Cookie{
		Name:   key,
		Value:  value,
		Path:   "/",
		MaxAge: expire,
	}
	if expire != 0 {
		expireTime := now.Add(time.Duration(expire) * time.Second)
		cookie.Expires = expireTime
	}
	http.SetCookie(ctx.Response, cookie)
}

// Sends a JSON response.
func (ctx *Context) JSON(obj interface{}) {
	json, err := json.Marshal(obj)
	if err != nil {
		panic(err)
	}
	ctx.Body = json
	ctx.Header["Content-Type"] = "application/json"
}

// Send the response immediately. Set `ctx.IsSent` to `true` to make
// sure that the response won't be sent twice.
func (ctx *Context) Send() {
	if ctx.IsSent {
		return
	}
	for name, value := range ctx.Header {
		ctx.Response.Header().Set(name, value)
	}
	ctx.Response.WriteHeader(ctx.StatusCode)
	ctx.Response.Write(ctx.Body)
	ctx.IsSent = true
}

// Write text on the response body.
func (ctx *Context) Write(content string) {
	ctx.Body = []byte(content)
}

// Retuns an HTTP Error by indicating the status code, the corresponding
// handler inside `App.errorHandler` will be called, if user does not set
// the corresponding error handler, the defaultErrorHandler will be called.
func (ctx *Context) Abort(statusCode int) {
	ctx.StatusCode = statusCode
	ctx.App.handleError(ctx, statusCode)
}

// Set the template loader for this context. This should be done before calling
// `ctx.Render`.
func (ctx *Context) Loader(name string) *Context {
	ctx.templateLoader = name
	return ctx
}

// Render a template file using the built-in Go template engine.
func (ctx *Context) Render(file string, data interface{}) {
	content, e := ctx.App.View.Render(ctx.templateLoader, file, data)
	if e != nil {
		panic(e)
	}
	ctx.Body = []byte(content)
}

func (ctx *Context) RenderFromString(tplSrc string, data interface{}) {
	content, e := ctx.App.View.RenderFromString(ctx.templateLoader, tplSrc, data)
	if e != nil {
		panic(e)
	}
	ctx.Body = []byte(content)
}
<|endoftext|>


<commit_before>package main

// Context is a connection context.
type Context struct {
	UserID         uint32 // should be in state
	IsDisconnected bool
	error          string
	Conn           *Conn
	State          interface{}
}

// SetError sets a connection error that will be written to the connection before it is closed right before the next read cycle.
func (c *Context) SetError(err string) {
	c.error = err
}
<commit_msg>fix type override<commit_after>package main

// Context is a connection context.
type Context struct {
	UserID         uint32 // should be in state
	IsDisconnected bool
	err            string
	Conn           *Conn
	State          interface{}
}

// SetError sets a connection error that will be written to the connection before it is closed right before the next read cycle.
func (c *Context) SetError(err string) {
	c.err = err
}
<|endoftext|>


<commit_before>package app

import (
	"fmt"

	"github.com/murlokswarm/log"
	"github.com/murlokswarm/markup"
	"github.com/murlokswarm/uid"
)

var (
	contexts = map[uid.ID]Contexter{}
)

// Contexter represents the support where a component can be mounted.
// eg a window.
type Contexter interface {
	// The ID of the context.
	ID() uid.ID

	// Mounts the component and renders it in the context.
	Mount(c markup.Componer)

	// Renders an element.
	Render(elem *markup.Element)

	// If applicable, returns the position of the context.
	Position() (x float64, y float64)

	// If applicable, moves the context.
	Move(x float64, y float64)

	// If applicable, returns the size of the context.
	Size() (width float64, height float64)

	// If applicable, resizes the context.
	Resize(width float64, height float64)

	// If applicable, set the icon targeted by path.
	SetIcon(path string)

	// If applicable, set the badge with v.
	SetBadge(v string)

	// If applicablex, closes the context.
	Close()
}

// Context returns the context of c.
// c must be mounted.
func Context(c markup.Componer) (ctx Contexter, err error) {
	var root *markup.Element

	if root, err = markup.ComponentRoot(c); err != nil {
		return
	}

	ctx, err = ContextByID(root.ContextID)
	return
}

// ContextByID returns the context registered under id.
func ContextByID(id uid.ID) (ctx Contexter, err error) {
	var registered bool

	if ctx, registered = contexts[id]; !registered {
		err = fmt.Errorf("context %v is not registered or has been closed", id)
	}

	return
}

// RegisterContext registers c.
// Should be used only in a driver implementation.
func RegisterContext(c Contexter) {
	if len(c.ID()) == 0 {
		log.Panicf("context %T is invalid. ID must be set", c)
	}

	if _, registered := contexts[c.ID()]; registered {
		log.Panicf("context %T with id %v is already registered", c, c.ID())
	}

	contexts[c.ID()] = c
}

// UnregisterContext unregisters c.
// Should be used only in a driver implementation.
func UnregisterContext(c Contexter) {
	delete(contexts, c.ID())
}

// ZeroContext is a placeholder context.
// It's used as a replacement for non available or non implemented features.
//
// Use of methods from a ZeroContext doesn't do anything.
type ZeroContext struct {
	id          uid.ID
	placeholder string
	root        markup.Componer
}

// NewZeroContext creates a ZeroContext.
func NewZeroContext(placeholder string) (ctx *ZeroContext) {
	ctx = &ZeroContext{
		id:          uid.Context(),
		placeholder: placeholder,
	}

	RegisterContext(ctx)
	return
}

// ID returns the ID of the context.
func (c *ZeroContext) ID() uid.ID {
	return c.id
}

// Mount is a placeholder method to satisfy the Contexter interface.
// It does nothing.
func (c *ZeroContext) Mount(component markup.Componer) {
	markup.Mount(component, c.ID())
	log.Infof("%T is mounted into %v (%v)", component, c.placeholder, c.ID())
}

// Render is a placeholder method to satisfy the Contexter interface.
// It does nothing.
func (c *ZeroContext) Render(elem *markup.Element) {
	log.Infof("rendering:\n\033[32m%v\033[00m", elem.HTML())
}

// Size is a placeholder method to satisfy the Contexter interface.
func (c *ZeroContext) Size() (width float64, height float64) {
	return
}

// Resize is a placeholder method to satisfy the Contexter interface.
// It does nothing.
func (c *ZeroContext) Resize(width float64, height float64) {
	log.Infof("%v (%v) simulates a resize of %v x %v", c.placeholder, c.ID(), width, height)
}

// Position is a placeholder method to satisfy the Contexter interface.
func (c *ZeroContext) Position() (x float64, y float64) {
	return
}

// Move is a placeholder method to satisfy the Contexter interface.
// It does nothing.
func (c *ZeroContext) Move(x float64, y float64) {
	log.Infof("%v (%v) simulates a move to (%v, %v)", c.placeholder, c.ID(), x, y)
}

// SetIcon is a placeholder method to satisfy the Contexter interface.
// It does nothing.
func (c *ZeroContext) SetIcon(path string) {
	log.Infof("%v (%v) simulates set icon with %v", c.placeholder, c.ID(), path)
}

func (c *ZeroContext) SetBadge(v string) {
	log.Infof("%v (%v) simulates set badge with %v", c.placeholder, c.ID(), v)

}

// Close is a closes the context.
func (c *ZeroContext) Close() {
	markup.Dismount(c.root)
	UnregisterContext(c)
	log.Infof("%v (%v) is closed", c.placeholder, c.ID())
}
<commit_msg>golint<commit_after>package app

import (
	"fmt"

	"github.com/murlokswarm/log"
	"github.com/murlokswarm/markup"
	"github.com/murlokswarm/uid"
)

var (
	contexts = map[uid.ID]Contexter{}
)

// Contexter represents the support where a component can be mounted.
// eg a window.
type Contexter interface {
	// The ID of the context.
	ID() uid.ID

	// Mounts the component and renders it in the context.
	Mount(c markup.Componer)

	// Renders an element.
	Render(elem *markup.Element)

	// If applicable, returns the position of the context.
	Position() (x float64, y float64)

	// If applicable, moves the context.
	Move(x float64, y float64)

	// If applicable, returns the size of the context.
	Size() (width float64, height float64)

	// If applicable, resizes the context.
	Resize(width float64, height float64)

	// If applicable, set the icon targeted by path.
	SetIcon(path string)

	// If applicable, set the badge with v.
	SetBadge(v string)

	// If applicablex, closes the context.
	Close()
}

// Context returns the context of c.
// c must be mounted.
func Context(c markup.Componer) (ctx Contexter, err error) {
	var root *markup.Element

	if root, err = markup.ComponentRoot(c); err != nil {
		return
	}

	ctx, err = ContextByID(root.ContextID)
	return
}

// ContextByID returns the context registered under id.
func ContextByID(id uid.ID) (ctx Contexter, err error) {
	var registered bool

	if ctx, registered = contexts[id]; !registered {
		err = fmt.Errorf("context %v is not registered or has been closed", id)
	}

	return
}

// RegisterContext registers c.
// Should be used only in a driver implementation.
func RegisterContext(c Contexter) {
	if len(c.ID()) == 0 {
		log.Panicf("context %T is invalid. ID must be set", c)
	}

	if _, registered := contexts[c.ID()]; registered {
		log.Panicf("context %T with id %v is already registered", c, c.ID())
	}

	contexts[c.ID()] = c
}

// UnregisterContext unregisters c.
// Should be used only in a driver implementation.
func UnregisterContext(c Contexter) {
	delete(contexts, c.ID())
}

// ZeroContext is a placeholder context.
// It's used as a replacement for non available or non implemented features.
//
// Use of methods from a ZeroContext doesn't do anything.
type ZeroContext struct {
	id          uid.ID
	placeholder string
	root        markup.Componer
}

// NewZeroContext creates a ZeroContext.
func NewZeroContext(placeholder string) (ctx *ZeroContext) {
	ctx = &ZeroContext{
		id:          uid.Context(),
		placeholder: placeholder,
	}

	RegisterContext(ctx)
	return
}

// ID returns the ID of the context.
func (c *ZeroContext) ID() uid.ID {
	return c.id
}

// Mount is a placeholder method to satisfy the Contexter interface.
// It does nothing.
func (c *ZeroContext) Mount(component markup.Componer) {
	markup.Mount(component, c.ID())
	log.Infof("%T is mounted into %v (%v)", component, c.placeholder, c.ID())
}

// Render is a placeholder method to satisfy the Contexter interface.
// It does nothing.
func (c *ZeroContext) Render(elem *markup.Element) {
	log.Infof("rendering:\n\033[32m%v\033[00m", elem.HTML())
}

// Size is a placeholder method to satisfy the Contexter interface.
func (c *ZeroContext) Size() (width float64, height float64) {
	return
}

// Resize is a placeholder method to satisfy the Contexter interface.
// It does nothing.
func (c *ZeroContext) Resize(width float64, height float64) {
	log.Infof("%v (%v) simulates a resize of %v x %v", c.placeholder, c.ID(), width, height)
}

// Position is a placeholder method to satisfy the Contexter interface.
func (c *ZeroContext) Position() (x float64, y float64) {
	return
}

// Move is a placeholder method to satisfy the Contexter interface.
// It does nothing.
func (c *ZeroContext) Move(x float64, y float64) {
	log.Infof("%v (%v) simulates a move to (%v, %v)", c.placeholder, c.ID(), x, y)
}

// SetIcon is a placeholder method to satisfy the Contexter interface.
// It does nothing.
func (c *ZeroContext) SetIcon(path string) {
	log.Infof("%v (%v) simulates set icon with %v", c.placeholder, c.ID(), path)
}

// SetBadge is a placeholder method to satisfy the Contexter interface.
// It does nothing.
func (c *ZeroContext) SetBadge(v string) {
	log.Infof("%v (%v) simulates set badge with %v", c.placeholder, c.ID(), v)

}

// Close is a closes the context.
func (c *ZeroContext) Close() {
	markup.Dismount(c.root)
	UnregisterContext(c)
	log.Infof("%v (%v) is closed", c.placeholder, c.ID())
}
<|endoftext|>


<commit_before>// bulk_load_es loads an ElasticSearch daemon with data from stdin.
//
// The caller is responsible for assuring that the database is empty before
// bulk load.
package main

import (
	"bufio"
	"bytes"
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"os"
	"sync"
	"time"
)

// Program option vars:
var (
	daemonUrl         string
	refreshEachBatch  bool
	workers           int
	batchSize         int
	indexTemplateName string
	doLoad            bool
)

// Global vars
var (
	bufPool      sync.Pool
	batchChan    chan *bytes.Buffer
	inputDone    chan struct{}
	workersGroup sync.WaitGroup
)

// Args parsing vars
var (
	indexTemplateChoices = map[string][]byte{
		"default": defaultTemplate,
		"aggregation":   aggregationTemplate,
	}
)

var defaultTemplate = []byte(`
{
  "template": "*",
  "settings": {
    "index": {
      "refresh_interval": "5s"
    }
  },
  "mappings": {
    "point": {
      "_all":            { "enabled": false },
      "_source":         { "enabled": true },
      "properties": {
        "timestamp":    { "type": "date", "doc_values": true }
      }
    }
  }
}
`)

var aggregationTemplate = []byte(`
{
  "template": "*",
  "settings": {
    "index": {
      "refresh_interval": "5s"
    }
  },
  "mappings": {
    "_default_": {
      "dynamic_templates": [
        {
          "strings_are_indexed_exactly_for_filtering": {
            "match": "*",
            "match_mapping_type": "string",
            "mapping": { "type": "string",  "doc_values": true, "index": "not_analyzed" }
          }
        },
	{
	  "all_other_types_are_in_column_index_but_not_searchable": {
            "match": "*",
            "mapping": { "doc_values": true, "index": "no" }
          }
        }
      ],
      "_all": { "enabled": false },
      "_source": { "enabled": false },
      "properties": {
        "timestamp": { "type": "date", "doc_values": true }
      }
    }
  }
}

`)

// Parse args:
func init() {
	flag.StringVar(&daemonUrl, "url", "http://localhost:9200", "ElasticSearch URL.")
	flag.BoolVar(&refreshEachBatch, "refresh", true, "Whether each batch is immediately indexed.")

	flag.IntVar(&batchSize, "batch-size", 5000, "Batch size (input items).")
	flag.IntVar(&workers, "workers", 1, "Number of parallel requests to make.")

	flag.StringVar(&indexTemplateName, "index-template", "default", "ElasticSearch index template to use (choices: default, aggregation).")

	flag.BoolVar(&doLoad, "do-load", true, "Whether to write data. Set this flag to false to check input read speed.")

	flag.Parse()

	if _, ok := indexTemplateChoices[indexTemplateName]; !ok {
		log.Fatalf("invalid index template type")
	}
}

func main() {
	if doLoad {
		// check that there are no pre-existing index templates:
		existingIndexTemplates, err := listIndexTemplates(daemonUrl)
		if err != nil {
			log.Fatal(err)
		}

		if len(existingIndexTemplates) > 0 {
			log.Fatal("There are index templates already in the data store. If you know what you are doing, clear them first with a command like:\ncurl -XDELETE 'http://localhost:9200/_template/*'")
		}

		// check that there are no pre-existing indices:
		existingIndices, err := listIndices(daemonUrl)
		if err != nil {
			log.Fatal(err)
		}

		if len(existingIndices) > 0 {
			log.Fatal("There are indices already in the data store. If you know what you are doing, clear them first with a command like:\ncurl -XDELETE 'http://localhost:9200/_all'")
		}

		// create the index template:
		indexTemplate := indexTemplateChoices[indexTemplateName]
		err = createESTemplate(daemonUrl, "measurements_template", indexTemplate)
		if err != nil {
			log.Fatal(err)
		}
	}
	bufPool = sync.Pool{
		New: func() interface{} {
			return bytes.NewBuffer(make([]byte, 0, 4*1024*1024))
		},
	}

	batchChan = make(chan *bytes.Buffer, workers)
	inputDone = make(chan struct{})

	for i := 0; i < workers; i++ {
		workersGroup.Add(1)
		cfg := HTTPWriterConfig{
			Host: daemonUrl,
		}
		go processBatches(NewHTTPWriter(cfg, refreshEachBatch))
	}

	start := time.Now()
	itemsRead := scan(batchSize)

	<-inputDone
	close(batchChan)
	workersGroup.Wait()
	end := time.Now()
	took := end.Sub(start)
	rate := float64(itemsRead) / float64(took.Seconds())

	fmt.Printf("loaded %d items in %fsec with %d workers (mean rate %f/sec)\n", itemsRead, took.Seconds(), workers, rate)
}

// scan reads lines from stdin. It expects input in the ElasticSearch bulk
// format: two line pairs, the first line being an 'action' and the second line
// being the payload.
func scan(itemsPerBatch int) int64 {
	buf := bufPool.Get().(*bytes.Buffer)

	var n int
	var linesRead int64
	scanner := bufio.NewScanner(os.Stdin)
	for scanner.Scan() {
		linesRead++

		buf.Write(scanner.Bytes())
		buf.Write([]byte("\n"))

		n++
		if n%2 == 0 && (n/2) >= itemsPerBatch {
			batchChan <- buf
			buf = bufPool.Get().(*bytes.Buffer)
			n = 0
		}
	}

	if err := scanner.Err(); err != nil {
		log.Fatalf("Error reading input: %s", err.Error())
	}

	// Finished reading input, make sure last batch goes out.
	if n > 0 {
		batchChan <- buf
	}

	// Closing inputDone signals to the application that we've read everything and can now shut down.
	close(inputDone)

	// The ES bulk format uses 2 lines per item:
	if linesRead % 2 != 0 {
		log.Fatalf("the number of lines read was not a multiple of 2, which indicates a bad bulk format for Elastic")
	}
	itemsRead := linesRead / 2

	return itemsRead
}

// processBatches reads byte buffers from batchChan and writes them to the target server, while tracking stats on the write.
func processBatches(w LineProtocolWriter) {
	for batch := range batchChan {
		if !doLoad {
			continue
		}

		// Write the batch.
		_, err := w.WriteLineProtocol(batch.Bytes())
		if err != nil {
			log.Fatalf("Error writing: %s\n", err.Error())
		}

		// Return the batch buffer to the pool.
		batch.Reset()
		bufPool.Put(batch)
	}
	workersGroup.Done()
}

func createESTemplate(daemonUrl, templateName string, templateBody []byte) error {
	u, err := url.Parse(daemonUrl)
	if err != nil {
		return err
	}

	u.Path = fmt.Sprintf("_template/%s", templateName)

	req, err := http.NewRequest("PUT", u.String(), bytes.NewReader(templateBody))
	if err != nil {
		return err
	}

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	// does the body need to be read into the void?

	if resp.StatusCode != 200 {
		return fmt.Errorf("bad mapping create")
	}
	return nil
}

func createDb(daemon_url, dbname string) error {
	u, err := url.Parse(daemon_url)
	if err != nil {
		return err
	}

	// serialize params the right way:
	u.Path = "query"
	v := u.Query()
	v.Set("q", fmt.Sprintf("CREATE DATABASE %s", dbname))
	u.RawQuery = v.Encode()

	req, err := http.NewRequest("GET", u.String(), nil)
	if err != nil {
		return err
	}

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	// does the body need to be read into the void?

	if resp.StatusCode != 200 {
		return fmt.Errorf("bad db create")
	}
	return nil
}

// listIndexTemplates lists the existing index templates in ElasticSearch.
func listIndexTemplates(daemonUrl string) (map[string]interface{}, error) {
	u := fmt.Sprintf("%s/_template", daemonUrl)
	resp, err := http.Get(u)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}

	var listing map[string]interface{}
	err = json.Unmarshal(body, &listing)
	if err != nil {
		return nil, err
	}

	return listing, nil
}

// listIndices lists the existing indices in ElasticSearch.
func listIndices(daemonUrl string) (map[string]interface{}, error) {
	u := fmt.Sprintf("%s/*", daemonUrl)
	resp, err := http.Get(u)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}

	var listing map[string]interface{}
	err = json.Unmarshal(body, &listing)
	if err != nil {
		return nil, err
	}

	return listing, nil
}
<commit_msg>gofmt<commit_after>// bulk_load_es loads an ElasticSearch daemon with data from stdin.
//
// The caller is responsible for assuring that the database is empty before
// bulk load.
package main

import (
	"bufio"
	"bytes"
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"os"
	"sync"
	"time"
)

// Program option vars:
var (
	daemonUrl         string
	refreshEachBatch  bool
	workers           int
	batchSize         int
	indexTemplateName string
	doLoad            bool
)

// Global vars
var (
	bufPool      sync.Pool
	batchChan    chan *bytes.Buffer
	inputDone    chan struct{}
	workersGroup sync.WaitGroup
)

// Args parsing vars
var (
	indexTemplateChoices = map[string][]byte{
		"default":     defaultTemplate,
		"aggregation": aggregationTemplate,
	}
)

var defaultTemplate = []byte(`
{
  "template": "*",
  "settings": {
    "index": {
      "refresh_interval": "5s"
    }
  },
  "mappings": {
    "point": {
      "_all":            { "enabled": false },
      "_source":         { "enabled": true },
      "properties": {
        "timestamp":    { "type": "date", "doc_values": true }
      }
    }
  }
}
`)

var aggregationTemplate = []byte(`
{
  "template": "*",
  "settings": {
    "index": {
      "refresh_interval": "5s"
    }
  },
  "mappings": {
    "_default_": {
      "dynamic_templates": [
        {
          "strings_are_indexed_exactly_for_filtering": {
            "match": "*",
            "match_mapping_type": "string",
            "mapping": { "type": "string",  "doc_values": true, "index": "not_analyzed" }
          }
        },
	{
	  "all_other_types_are_in_column_index_but_not_searchable": {
            "match": "*",
            "mapping": { "doc_values": true, "index": "no" }
          }
        }
      ],
      "_all": { "enabled": false },
      "_source": { "enabled": false },
      "properties": {
        "timestamp": { "type": "date", "doc_values": true }
      }
    }
  }
}

`)

// Parse args:
func init() {
	flag.StringVar(&daemonUrl, "url", "http://localhost:9200", "ElasticSearch URL.")
	flag.BoolVar(&refreshEachBatch, "refresh", true, "Whether each batch is immediately indexed.")

	flag.IntVar(&batchSize, "batch-size", 5000, "Batch size (input items).")
	flag.IntVar(&workers, "workers", 1, "Number of parallel requests to make.")

	flag.StringVar(&indexTemplateName, "index-template", "default", "ElasticSearch index template to use (choices: default, aggregation).")

	flag.BoolVar(&doLoad, "do-load", true, "Whether to write data. Set this flag to false to check input read speed.")

	flag.Parse()

	if _, ok := indexTemplateChoices[indexTemplateName]; !ok {
		log.Fatalf("invalid index template type")
	}
}

func main() {
	if doLoad {
		// check that there are no pre-existing index templates:
		existingIndexTemplates, err := listIndexTemplates(daemonUrl)
		if err != nil {
			log.Fatal(err)
		}

		if len(existingIndexTemplates) > 0 {
			log.Fatal("There are index templates already in the data store. If you know what you are doing, clear them first with a command like:\ncurl -XDELETE 'http://localhost:9200/_template/*'")
		}

		// check that there are no pre-existing indices:
		existingIndices, err := listIndices(daemonUrl)
		if err != nil {
			log.Fatal(err)
		}

		if len(existingIndices) > 0 {
			log.Fatal("There are indices already in the data store. If you know what you are doing, clear them first with a command like:\ncurl -XDELETE 'http://localhost:9200/_all'")
		}

		// create the index template:
		indexTemplate := indexTemplateChoices[indexTemplateName]
		err = createESTemplate(daemonUrl, "measurements_template", indexTemplate)
		if err != nil {
			log.Fatal(err)
		}
	}
	bufPool = sync.Pool{
		New: func() interface{} {
			return bytes.NewBuffer(make([]byte, 0, 4*1024*1024))
		},
	}

	batchChan = make(chan *bytes.Buffer, workers)
	inputDone = make(chan struct{})

	for i := 0; i < workers; i++ {
		workersGroup.Add(1)
		cfg := HTTPWriterConfig{
			Host: daemonUrl,
		}
		go processBatches(NewHTTPWriter(cfg, refreshEachBatch))
	}

	start := time.Now()
	itemsRead := scan(batchSize)

	<-inputDone
	close(batchChan)
	workersGroup.Wait()
	end := time.Now()
	took := end.Sub(start)
	rate := float64(itemsRead) / float64(took.Seconds())

	fmt.Printf("loaded %d items in %fsec with %d workers (mean rate %f/sec)\n", itemsRead, took.Seconds(), workers, rate)
}

// scan reads lines from stdin. It expects input in the ElasticSearch bulk
// format: two line pairs, the first line being an 'action' and the second line
// being the payload.
func scan(itemsPerBatch int) int64 {
	buf := bufPool.Get().(*bytes.Buffer)

	var n int
	var linesRead int64
	scanner := bufio.NewScanner(os.Stdin)
	for scanner.Scan() {
		linesRead++

		buf.Write(scanner.Bytes())
		buf.Write([]byte("\n"))

		n++
		if n%2 == 0 && (n/2) >= itemsPerBatch {
			batchChan <- buf
			buf = bufPool.Get().(*bytes.Buffer)
			n = 0
		}
	}

	if err := scanner.Err(); err != nil {
		log.Fatalf("Error reading input: %s", err.Error())
	}

	// Finished reading input, make sure last batch goes out.
	if n > 0 {
		batchChan <- buf
	}

	// Closing inputDone signals to the application that we've read everything and can now shut down.
	close(inputDone)

	// The ES bulk format uses 2 lines per item:
	if linesRead%2 != 0 {
		log.Fatalf("the number of lines read was not a multiple of 2, which indicates a bad bulk format for Elastic")
	}
	itemsRead := linesRead / 2

	return itemsRead
}

// processBatches reads byte buffers from batchChan and writes them to the target server, while tracking stats on the write.
func processBatches(w LineProtocolWriter) {
	for batch := range batchChan {
		if !doLoad {
			continue
		}

		// Write the batch.
		_, err := w.WriteLineProtocol(batch.Bytes())
		if err != nil {
			log.Fatalf("Error writing: %s\n", err.Error())
		}

		// Return the batch buffer to the pool.
		batch.Reset()
		bufPool.Put(batch)
	}
	workersGroup.Done()
}

func createESTemplate(daemonUrl, templateName string, templateBody []byte) error {
	u, err := url.Parse(daemonUrl)
	if err != nil {
		return err
	}

	u.Path = fmt.Sprintf("_template/%s", templateName)

	req, err := http.NewRequest("PUT", u.String(), bytes.NewReader(templateBody))
	if err != nil {
		return err
	}

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	// does the body need to be read into the void?

	if resp.StatusCode != 200 {
		return fmt.Errorf("bad mapping create")
	}
	return nil
}

func createDb(daemon_url, dbname string) error {
	u, err := url.Parse(daemon_url)
	if err != nil {
		return err
	}

	// serialize params the right way:
	u.Path = "query"
	v := u.Query()
	v.Set("q", fmt.Sprintf("CREATE DATABASE %s", dbname))
	u.RawQuery = v.Encode()

	req, err := http.NewRequest("GET", u.String(), nil)
	if err != nil {
		return err
	}

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	// does the body need to be read into the void?

	if resp.StatusCode != 200 {
		return fmt.Errorf("bad db create")
	}
	return nil
}

// listIndexTemplates lists the existing index templates in ElasticSearch.
func listIndexTemplates(daemonUrl string) (map[string]interface{}, error) {
	u := fmt.Sprintf("%s/_template", daemonUrl)
	resp, err := http.Get(u)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}

	var listing map[string]interface{}
	err = json.Unmarshal(body, &listing)
	if err != nil {
		return nil, err
	}

	return listing, nil
}

// listIndices lists the existing indices in ElasticSearch.
func listIndices(daemonUrl string) (map[string]interface{}, error) {
	u := fmt.Sprintf("%s/*", daemonUrl)
	resp, err := http.Get(u)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}

	var listing map[string]interface{}
	err = json.Unmarshal(body, &listing)
	if err != nil {
		return nil, err
	}

	return listing, nil
}
<|endoftext|>


<commit_before>package gore

import (
	"bytes"
	"fmt"
	"io"
	"strings"
	"text/scanner"

	"github.com/peterh/liner"
)

const (
	promptDefault  = "gore> "
	promptContinue = "..... "
	indent         = "    "
)

type contLiner struct {
	*liner.State
	buffer string
	depth  int
}

func newContLiner() *contLiner {
	rl := liner.NewLiner()
	rl.SetCtrlCAborts(true)
	return &contLiner{State: rl}
}

func (cl *contLiner) promptString() string {
	if cl.buffer != "" {
		return promptContinue + strings.Repeat(indent, cl.depth)
	}

	return promptDefault
}

func (cl *contLiner) Prompt() (string, error) {
	line, err := cl.State.Prompt(cl.promptString())
	if err == io.EOF {
		if cl.buffer != "" {
			// cancel line continuation
			cl.Accepted()
			fmt.Println()
			err = nil
		}
	} else if err == liner.ErrPromptAborted {
		err = nil
		if cl.buffer != "" {
			cl.Accepted()
		} else {
			fmt.Println("(^D to quit)")
		}
	} else if err == nil {
		if cl.buffer != "" {
			cl.buffer = cl.buffer + "\n" + line
		} else {
			cl.buffer = line
		}
	}

	return cl.buffer, err
}

func (cl *contLiner) Accepted() {
	cl.State.AppendHistory(cl.buffer)
	cl.buffer = ""
}

func (cl *contLiner) Clear() {
	cl.buffer = ""
	cl.depth = 0
}

var errUnmatchedBraces = fmt.Errorf("unmatched braces")

func (cl *contLiner) Reindent() error {
	oldDepth := cl.depth
	cl.depth = cl.countDepth()

	if cl.depth < 0 {
		return errUnmatchedBraces
	}

	if cl.depth < oldDepth {
		lines := strings.Split(cl.buffer, "\n")
		if len(lines) > 1 {
			lastLine := lines[len(lines)-1]

			cursorUp()
			fmt.Printf("\r%s%s", cl.promptString(), lastLine)
			eraseInLine()
			fmt.Print("\n")
		}
	}

	return nil
}

func (cl *contLiner) countDepth() int {
	reader := bytes.NewBufferString(cl.buffer)
	sc := new(scanner.Scanner)
	sc.Init(reader)
	sc.Error = func(_ *scanner.Scanner, msg string) {
		debugf("scanner: %s", msg)
	}

	depth := 0
	for {
		switch sc.Scan() {
		case '{':
			depth++
		case '}':
			depth--
		case scanner.EOF:
			return depth
		}
	}
}
<commit_msg>increate indentation depth with parenthesis (close #46)<commit_after>package gore

import (
	"bytes"
	"fmt"
	"io"
	"strings"
	"text/scanner"

	"github.com/peterh/liner"
)

const (
	promptDefault  = "gore> "
	promptContinue = "..... "
	indent         = "    "
)

type contLiner struct {
	*liner.State
	buffer string
	depth  int
}

func newContLiner() *contLiner {
	rl := liner.NewLiner()
	rl.SetCtrlCAborts(true)
	return &contLiner{State: rl}
}

func (cl *contLiner) promptString() string {
	if cl.buffer != "" {
		return promptContinue + strings.Repeat(indent, cl.depth)
	}

	return promptDefault
}

func (cl *contLiner) Prompt() (string, error) {
	line, err := cl.State.Prompt(cl.promptString())
	if err == io.EOF {
		if cl.buffer != "" {
			// cancel line continuation
			cl.Accepted()
			fmt.Println()
			err = nil
		}
	} else if err == liner.ErrPromptAborted {
		err = nil
		if cl.buffer != "" {
			cl.Accepted()
		} else {
			fmt.Println("(^D to quit)")
		}
	} else if err == nil {
		if cl.buffer != "" {
			cl.buffer = cl.buffer + "\n" + line
		} else {
			cl.buffer = line
		}
	}

	return cl.buffer, err
}

func (cl *contLiner) Accepted() {
	cl.State.AppendHistory(cl.buffer)
	cl.buffer = ""
}

func (cl *contLiner) Clear() {
	cl.buffer = ""
	cl.depth = 0
}

var errUnmatchedBraces = fmt.Errorf("unmatched braces")

func (cl *contLiner) Reindent() error {
	oldDepth := cl.depth
	cl.depth = cl.countDepth()

	if cl.depth < 0 {
		return errUnmatchedBraces
	}

	if cl.depth < oldDepth {
		lines := strings.Split(cl.buffer, "\n")
		if len(lines) > 1 {
			lastLine := lines[len(lines)-1]

			cursorUp()
			fmt.Printf("\r%s%s", cl.promptString(), lastLine)
			eraseInLine()
			fmt.Print("\n")
		}
	}

	return nil
}

func (cl *contLiner) countDepth() int {
	reader := bytes.NewBufferString(cl.buffer)
	sc := new(scanner.Scanner)
	sc.Init(reader)
	sc.Error = func(_ *scanner.Scanner, msg string) {
		debugf("scanner: %s", msg)
	}

	depth := 0
	for {
		switch sc.Scan() {
		case '{', '(':
			depth++
		case '}', ')':
			depth--
		case scanner.EOF:
			return depth
		}
	}
}
<|endoftext|>


<commit_before>package main

import "hello-go/src/mike-app/lib1" //这里引号内的是文件路径
import "fmt"

func main() {
	p := fmt.Println
	lib.User()	//这里lib是真正的导入的路径下的package名称
	p("end")

	/*
	import "xx1/xx2/xx3"

	func xxx() {
		xx3.xxx()
		//这里的xx3在import关键词后面的是文件路径
		//在方法xxx()前的是包名，原则上路径跟包名保持一致，但可以不同。
	}

	*/
}
<commit_msg>Delete main.go<commit_after><|endoftext|>