<commit_before>package faker

import "github.com/raitucarp/faker/locales"

type Gender int

const (
	Male Gender = iota
	Female
	RandomGender
)

var gender Gender

type Faker struct {
	Name     Name
	Address  Address
	Commerce Commerce
	Company  Company

	// temp data handling
	data interface{}
}

type Definitions map[string]interface{}

var data interface{}

func (f *Faker) generate() {
	err := SetLocale(locales.EN)
	if err != nil {
		panic(err)
	}
}

func New() (f Faker) {
	f.generate()

	return
}

/*
func SetGender(g Gender) {
	gender = g
}*/
<commit_msg>add date, finance, hacker, image<commit_after>package faker

import "github.com/raitucarp/faker/locales"

type Gender int

const (
	Male Gender = iota
	Female
	RandomGender
)

var gender Gender

type Faker struct {
	Name     Name
	Address  Address
	Commerce Commerce
	Company  Company
	Date     Date
	Finance  Finance
	Hacker   Hacker
	Image    Image

	// temp data handling
	data interface{}
}

type Definitions map[string]interface{}

var data interface{}

func (f *Faker) generate() {
	err := SetLocale(locales.EN)
	if err != nil {
		panic(err)
	}
}

func New() (f Faker) {
	f.generate()

	return
}

/*
func SetGender(g Gender) {
	gender = g
}*/
<|endoftext|>


<commit_before>package spdy

import (
	"bytes"
	"compress/zlib"
	"errors"
	"io"
	"net/http"
	"strings"
	"sync"
)

var versionError = errors.New("Version not supported.")

// Decompressor is used to decompress name/value header blocks.
// Decompressors retain their state, so a single Decompressor
// should be used for each direction of a particular connection.
type decompressor struct {
	m       sync.Mutex
	in      *bytes.Buffer
	out     io.ReadCloser
	version uint16
}

// NewDecompressor is used to create a new decompressor.
// It takes the SPDY version to use.
func NewDecompressor(version uint16) Decompressor {
	out := new(decompressor)
	out.version = version
	return out
}

// Decompress uses zlib decompression to decompress the provided
// data, according to the SPDY specification of the given version.
func (d *decompressor) Decompress(data []byte) (headers http.Header, err error) {
	d.m.Lock()
	defer d.m.Unlock()

	if d.in == nil {
		d.in = bytes.NewBuffer(data)
	} else {
		d.in.Reset()
		d.in.Write(data)
	}

	// Initialise the decompressor with the appropriate
	// dictionary, depending on SPDY version.
	if d.out == nil {
		switch d.version {
		case 2:
			d.out, err = zlib.NewReaderDict(d.in, headerDictionaryV2)
		case 3:
			d.out, err = zlib.NewReaderDict(d.in, headerDictionaryV3)
		default:
			err = versionError
		}

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

	var chunk []byte
	var dechunk func([]byte) int

	// SPDY/2 uses 16-bit fixed fields, where SPDY/3 uses 32-bit fields.
	switch d.version {
	case 2:
		chunk = make([]byte, 2)
		dechunk = func(b []byte) int {
			return int(bytesToUint16(b))
		}
	case 3:
		chunk = make([]byte, 4)
		dechunk = func(b []byte) int {
			return int(bytesToUint32(b))
		}
	default:
		return nil, versionError
	}

	// Read in the number of name/value pairs.
	if _, err = d.out.Read(chunk); err != nil {
		return nil, err
	}
	numNameValuePairs := dechunk(chunk)

	headers = make(http.Header)
	length := 0
	bounds := MAX_FRAME_SIZE - 12 // Maximum frame size minus maximum non-headers data (SYN_STREAM)
	for i := 0; i < numNameValuePairs; i++ {
		var nameLength, valueLength int

		// Get the name.
		if _, err = d.out.Read(chunk); err != nil {
			return nil, err
		}
		nameLength = dechunk(chunk)

		if nameLength > bounds {
			return nil, errors.New("Error: Incorrect header name length.")
		}
		bounds -= nameLength

		name := make([]byte, nameLength)
		if _, err = d.out.Read(name); err != nil {
			return nil, err
		}

		// Get the value.
		if _, err = d.out.Read(chunk); err != nil {
			return nil, err
		}
		valueLength = dechunk(chunk)

		if valueLength > bounds {
			return nil, errors.New("Error: Incorrect header values length.")
		}
		bounds -= valueLength

		values := make([]byte, valueLength)
		if _, err = d.out.Read(values); err != nil {
			return nil, err
		}

		// Count name and ': '.
		length += nameLength + 2

		// Split the value on null boundaries.
		for _, value := range bytes.Split(values, []byte{'\x00'}) {
			headers.Add(string(name), string(value))
			length += len(value) + 2 // count value and ', ' or '\n\r'.
		}
	}

	return headers, nil
}

// Compressor is used to compress name/value header blocks.
// Compressors retain their state, so a single Compressor
// should be used for each direction of a particular
// connection.
type compressor struct {
	m       sync.Mutex
	buf     *bytes.Buffer
	w       *zlib.Writer
	version uint16
}

// NewCompressor is used to create a new compressor.
// It takes the SPDY version to use.
func NewCompressor(version uint16) Compressor {
	out := new(compressor)
	out.version = version
	return out
}

// Compress uses zlib compression to compress the provided
// data, according to the SPDY specification of the given version.
func (c *compressor) Compress(h http.Header) ([]byte, error) {
	c.m.Lock()
	defer c.m.Unlock()

	var err error
	if c.buf == nil {
		c.buf = new(bytes.Buffer)

		if c.w == nil {
			switch c.version {
			case 2:
				c.w, err = zlib.NewWriterLevelDict(c.buf, zlib.BestCompression, headerDictionaryV2)
			case 3:
				c.w, err = zlib.NewWriterLevelDict(c.buf, zlib.BestCompression, headerDictionaryV3)
			default:
				err = versionError
			}
		}

		if err != nil {
			return nil, err
		}
	} else {
		c.buf.Reset()
	}

	h.Del("Connection")
	h.Del("Keep-Alive")
	h.Del("Proxy-Connection")
	h.Del("Transfer-Encoding")

	length := 4
	num := len(h)
	lens := make(map[string]int)
	for name, values := range h {
		length += len(name) + 8
		lens[name] = len(values) - 1
		for _, value := range values {
			length += len(value)
			lens[name] += len(value)
		}
	}

	out := make([]byte, length)
	switch c.version {
	case 3:
		out[0] = byte(num >> 24)
		out[1] = byte(num >> 16)
		out[2] = byte(num >> 8)
		out[3] = byte(num)
	case 2:
		out[0] = byte(num >> 8)
		out[1] = byte(num)
	}

	offset := 4
	if c.version == 2 {
		offset = 2
	}
	for name, values := range h {
		nLen := len(name)
		switch c.version {
		case 3:
			out[offset+0] = byte(nLen >> 24)
			out[offset+1] = byte(nLen >> 16)
			out[offset+2] = byte(nLen >> 8)
			out[offset+3] = byte(nLen)
			offset += 4
		case 2:
			out[offset+0] = byte(nLen >> 8)
			out[offset+1] = byte(nLen)
			offset += 2
		}

		for i, b := range []byte(strings.ToLower(name)) {
			out[offset+i] = b
		}

		offset += nLen

		vLen := lens[name]
		switch c.version {
		case 3:
			out[offset+0] = byte(vLen >> 24)
			out[offset+1] = byte(vLen >> 16)
			out[offset+2] = byte(vLen >> 8)
			out[offset+3] = byte(vLen)
			offset += 4
		case 2:
			out[offset+0] = byte(vLen >> 8)
			out[offset+1] = byte(vLen)
			offset += 2
		}

		for n, value := range values {
			for i, b := range []byte(value) {
				out[offset+i] = b
			}
			offset += len(value)
			if n < len(values)-1 {
				out[offset] = '\x00'
				offset += 1
			}
		}
	}

	_, err = c.w.Write(out)
	if err != nil {
		return nil, err
	}

	c.w.Flush()
	return c.buf.Bytes(), nil
}

func (c *compressor) Close() error {
	if c.w == nil {
		return nil
	}
	err := c.w.Close()
	if err != nil {
		return err
	}
	c.w = nil
	return nil
}
<commit_msg>Improved debug info for invalid header lengths<commit_after>package spdy

import (
	"bytes"
	"compress/zlib"
	"errors"
	"io"
	"net/http"
	"strings"
	"sync"
)

var versionError = errors.New("Version not supported.")

// Decompressor is used to decompress name/value header blocks.
// Decompressors retain their state, so a single Decompressor
// should be used for each direction of a particular connection.
type decompressor struct {
	m       sync.Mutex
	in      *bytes.Buffer
	out     io.ReadCloser
	version uint16
}

// NewDecompressor is used to create a new decompressor.
// It takes the SPDY version to use.
func NewDecompressor(version uint16) Decompressor {
	out := new(decompressor)
	out.version = version
	return out
}

// Decompress uses zlib decompression to decompress the provided
// data, according to the SPDY specification of the given version.
func (d *decompressor) Decompress(data []byte) (headers http.Header, err error) {
	d.m.Lock()
	defer d.m.Unlock()

	if d.in == nil {
		d.in = bytes.NewBuffer(data)
	} else {
		d.in.Reset()
		d.in.Write(data)
	}

	// Initialise the decompressor with the appropriate
	// dictionary, depending on SPDY version.
	if d.out == nil {
		switch d.version {
		case 2:
			d.out, err = zlib.NewReaderDict(d.in, headerDictionaryV2)
		case 3:
			d.out, err = zlib.NewReaderDict(d.in, headerDictionaryV3)
		default:
			err = versionError
		}

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

	var chunk []byte
	var dechunk func([]byte) int

	// SPDY/2 uses 16-bit fixed fields, where SPDY/3 uses 32-bit fields.
	switch d.version {
	case 2:
		chunk = make([]byte, 2)
		dechunk = func(b []byte) int {
			return int(bytesToUint16(b))
		}
	case 3:
		chunk = make([]byte, 4)
		dechunk = func(b []byte) int {
			return int(bytesToUint32(b))
		}
	default:
		return nil, versionError
	}

	// Read in the number of name/value pairs.
	if _, err = d.out.Read(chunk); err != nil {
		return nil, err
	}
	numNameValuePairs := dechunk(chunk)

	headers = make(http.Header)
	length := 0
	bounds := MAX_FRAME_SIZE - 12 // Maximum frame size minus maximum non-headers data (SYN_STREAM)
	for i := 0; i < numNameValuePairs; i++ {
		var nameLength, valueLength int

		// Get the name.
		if _, err = d.out.Read(chunk); err != nil {
			return nil, err
		}
		nameLength = dechunk(chunk)

		if nameLength > bounds {
			debug.Printf("Error: Maximum header length is %d. Received name length %d.\n", bounds, nameLength)
			return nil, errors.New("Error: Incorrect header name length.")
		}
		bounds -= nameLength

		name := make([]byte, nameLength)
		if _, err = d.out.Read(name); err != nil {
			return nil, err
		}

		// Get the value.
		if _, err = d.out.Read(chunk); err != nil {
			return nil, err
		}
		valueLength = dechunk(chunk)

		if valueLength > bounds {
			debug.Printf("Error: Maximum remaining header length is %d. Received values length %d.\n",
				bounds, valueLength)
			return nil, errors.New("Error: Incorrect header values length.")
		}
		bounds -= valueLength

		values := make([]byte, valueLength)
		if _, err = d.out.Read(values); err != nil {
			return nil, err
		}

		// Count name and ': '.
		length += nameLength + 2

		// Split the value on null boundaries.
		for _, value := range bytes.Split(values, []byte{'\x00'}) {
			headers.Add(string(name), string(value))
			length += len(value) + 2 // count value and ', ' or '\n\r'.
		}
	}

	return headers, nil
}

// Compressor is used to compress name/value header blocks.
// Compressors retain their state, so a single Compressor
// should be used for each direction of a particular
// connection.
type compressor struct {
	m       sync.Mutex
	buf     *bytes.Buffer
	w       *zlib.Writer
	version uint16
}

// NewCompressor is used to create a new compressor.
// It takes the SPDY version to use.
func NewCompressor(version uint16) Compressor {
	out := new(compressor)
	out.version = version
	return out
}

// Compress uses zlib compression to compress the provided
// data, according to the SPDY specification of the given version.
func (c *compressor) Compress(h http.Header) ([]byte, error) {
	c.m.Lock()
	defer c.m.Unlock()

	var err error
	if c.buf == nil {
		c.buf = new(bytes.Buffer)

		if c.w == nil {
			switch c.version {
			case 2:
				c.w, err = zlib.NewWriterLevelDict(c.buf, zlib.BestCompression, headerDictionaryV2)
			case 3:
				c.w, err = zlib.NewWriterLevelDict(c.buf, zlib.BestCompression, headerDictionaryV3)
			default:
				err = versionError
			}
		}

		if err != nil {
			return nil, err
		}
	} else {
		c.buf.Reset()
	}

	h.Del("Connection")
	h.Del("Keep-Alive")
	h.Del("Proxy-Connection")
	h.Del("Transfer-Encoding")

	length := 4
	num := len(h)
	lens := make(map[string]int)
	for name, values := range h {
		length += len(name) + 8
		lens[name] = len(values) - 1
		for _, value := range values {
			length += len(value)
			lens[name] += len(value)
		}
	}

	out := make([]byte, length)
	switch c.version {
	case 3:
		out[0] = byte(num >> 24)
		out[1] = byte(num >> 16)
		out[2] = byte(num >> 8)
		out[3] = byte(num)
	case 2:
		out[0] = byte(num >> 8)
		out[1] = byte(num)
	}

	offset := 4
	if c.version == 2 {
		offset = 2
	}
	for name, values := range h {
		nLen := len(name)
		switch c.version {
		case 3:
			out[offset+0] = byte(nLen >> 24)
			out[offset+1] = byte(nLen >> 16)
			out[offset+2] = byte(nLen >> 8)
			out[offset+3] = byte(nLen)
			offset += 4
		case 2:
			out[offset+0] = byte(nLen >> 8)
			out[offset+1] = byte(nLen)
			offset += 2
		}

		for i, b := range []byte(strings.ToLower(name)) {
			out[offset+i] = b
		}

		offset += nLen

		vLen := lens[name]
		switch c.version {
		case 3:
			out[offset+0] = byte(vLen >> 24)
			out[offset+1] = byte(vLen >> 16)
			out[offset+2] = byte(vLen >> 8)
			out[offset+3] = byte(vLen)
			offset += 4
		case 2:
			out[offset+0] = byte(vLen >> 8)
			out[offset+1] = byte(vLen)
			offset += 2
		}

		for n, value := range values {
			for i, b := range []byte(value) {
				out[offset+i] = b
			}
			offset += len(value)
			if n < len(values)-1 {
				out[offset] = '\x00'
				offset += 1
			}
		}
	}

	_, err = c.w.Write(out)
	if err != nil {
		return nil, err
	}

	c.w.Flush()
	return c.buf.Bytes(), nil
}

func (c *compressor) Close() error {
	if c.w == nil {
		return nil
	}
	err := c.w.Close()
	if err != nil {
		return err
	}
	c.w = nil
	return nil
}
<|endoftext|>


<commit_before>// Benchmark concatenation of slices using append vs using copy

/* Results

$ go test -v -bench . concat_test.go
=== RUN   TestConcatAppend
--- PASS: TestConcatAppend (0.00s)
=== RUN   TestConcatCopy
--- PASS: TestConcatCopy (0.00s)
BenchmarkAppend-4   	20000000	       111 ns/op
BenchmarkCopy-4     	30000000	        46.0 ns/op
PASS
ok  	command-line-arguments	3.774s
*/

package main

import "testing"

var (
	// Test parameters
	x = []int{1, 2}
	y = []int{3}
	z = []int{4, 5}

	// Expected result
	res = []int{1, 2, 3, 4, 5}
)

// concatAppend concatenates slices using built-in append
func concatAppend(slices ...[]int) []int {
	var result []int
	for _, slice := range slices {
		result = append(result, slice...)
	}
	return result
}

// concatAppend concatinates slices using built-in copy
func concatCopy(slices ...[]int) []int {
	size := 0
	for _, slice := range slices {
		size += len(slice)
	}
	result := make([]int, size)

	i := 0
	for _, slice := range slices {
		copy(result[i:], slice)
		i += len(slice)
	}
	return result
}

// eq return true if two slices are equal
func eq(a, b []int) bool {
	if a == nil && b == nil {
		return true
	}

	if a == nil || b == nil {
		return false
	}

	if len(a) != len(b) {
		return false
	}

	for i, v := range a {
		if v != b[i] {
			return false
		}
	}

	return true
}

func TestConcatAppend(t *testing.T) {
	out := concatAppend(x, y, z)
	if !eq(out, res) {
		t.Fatalf("concatAppend - got %v, expected %v\n", out, res)
	}
}

func TestConcatCopy(t *testing.T) {
	out := concatCopy(x, y, z)
	if !eq(out, res) {
		t.Fatalf("concatAppend - got %v, expected %v\n", out, res)
	}
}

func BenchmarkAppend(b *testing.B) {
	for i := 0; i < b.N; i++ {
		concatAppend(x, y, z)
	}
}

func BenchmarkCopy(b *testing.B) {
	for i := 0; i < b.N; i++ {
		concatCopy(x, y, z)
	}
}
<commit_msg>typo in message<commit_after>// Benchmark concatenation of slices using append vs using copy

/* Results

$ go test -v -bench . concat_test.go
=== RUN   TestConcatAppend
--- PASS: TestConcatAppend (0.00s)
=== RUN   TestConcatCopy
--- PASS: TestConcatCopy (0.00s)
BenchmarkAppend-4   	20000000	       111 ns/op
BenchmarkCopy-4     	30000000	        46.0 ns/op
PASS
ok  	command-line-arguments	3.774s
*/

package main

import "testing"

var (
	// Test parameters
	x = []int{1, 2}
	y = []int{3}
	z = []int{4, 5}

	// Expected result
	res = []int{1, 2, 3, 4, 5}
)

// concatAppend concatenates slices using built-in append
func concatAppend(slices ...[]int) []int {
	var result []int
	for _, slice := range slices {
		result = append(result, slice...)
	}
	return result
}

// concatAppend concatinates slices using built-in copy
func concatCopy(slices ...[]int) []int {
	size := 0
	for _, slice := range slices {
		size += len(slice)
	}
	result := make([]int, size)

	i := 0
	for _, slice := range slices {
		copy(result[i:], slice)
		i += len(slice)
	}
	return result
}

// eq return true if two slices are equal
func eq(a, b []int) bool {
	if a == nil && b == nil {
		return true
	}

	if a == nil || b == nil {
		return false
	}

	if len(a) != len(b) {
		return false
	}

	for i, v := range a {
		if v != b[i] {
			return false
		}
	}

	return true
}

func TestConcatAppend(t *testing.T) {
	out := concatAppend(x, y, z)
	if !eq(out, res) {
		t.Fatalf("concatAppend - got %v, expected %v\n", out, res)
	}
}

func TestConcatCopy(t *testing.T) {
	out := concatCopy(x, y, z)
	if !eq(out, res) {
		t.Fatalf("concatCopy - got %v, expected %v\n", out, res)
	}
}

func BenchmarkAppend(b *testing.B) {
	for i := 0; i < b.N; i++ {
		concatAppend(x, y, z)
	}
}

func BenchmarkCopy(b *testing.B) {
	for i := 0; i < b.N; i++ {
		concatCopy(x, y, z)
	}
}
<|endoftext|>


<commit_before>package config

import (
	"errors"
	"os"
	"strings"

	"github.com/ondevice/ondevice/config/internal"
	"github.com/sirupsen/logrus"
)

// Auth -- contains API server credentials
type Auth = internal.Auth

func _getAuth(section string) (Auth, error) {
	if os.Getenv("ONDEVICE_USER") != "" || os.Getenv("ONDEVICE_AUTH") != "" {
		return internal.NewAuth(os.Getenv("ONDEVICE_USER"), os.Getenv("ONDEVICE_AUTH"), ""), nil
	}

	var cfg, err = Read()
	if err != nil {
		return nil, err
	}

	var username, auth string
	if username, err = cfg.GetString(section, "user"); err != nil {
		return nil, err
	}
	if auth, err = cfg.GetString(section, "auth"); err != nil {
		return nil, err
	}

	return internal.NewAuth(username, auth, ""), nil
}

// GetClientAuth -- Get the default client authentication information
func GetClientAuth() (Auth, error) {
	return _getAuth("client")
}

// GetClientUserAuth -- get the authentication credentials for a specific client user
func GetClientUserAuth(username string) (Auth, error) {
	var auth, err = GetClientAuth()
	if err == nil && strings.ToLower(auth.User()) == strings.ToLower(username) {
		return auth, nil
	}

	var cfg Config
	if cfg, err = Read(); err != nil {
		return nil, errors.New("failed to read ondevice.conf")
	}

	var key string
	if key, err = cfg.GetString("client", "auth_"+username); err == nil {
		return internal.NewAuth(username, key, ""), nil
	}

	return nil, errors.New("no client credentials found for user")
}

// GetDeviceAuth -- Get the device authentication
func GetDeviceAuth() (Auth, error) {
	return _getAuth("device")
}

// ListAuthenticatedUsers -- returns the names of users we have client auth for
func ListAuthenticatedUsers() []string {
	// TODO this is messy but will do for now - we'll improve this once we have a separate auth file

	var rc []string
	var uniqueUsers = make(map[string]bool)

	var cfg, err = Read()
	if err != nil {
		logrus.WithError(err).Fatal("failed to fetch configuration")
	}

	if mainUser, err := cfg.GetString("client", "user"); err != nil && mainUser != "" {
		rc = append(rc, mainUser)
		uniqueUsers[strings.ToLower(mainUser)] = true
	}

	for k := range cfg.AllValues() {
		if strings.HasPrefix(k, "client.auth_") {
			var name = k[12:]
			var lowerName = strings.ToLower(name)
			if !uniqueUsers[lowerName] {
				rc = append(rc, k[12:])
				uniqueUsers[lowerName] = true
			}
		}
	}

	return rc
}
<commit_msg>added config.NewAuth() (alias to config.internal.NewAuth()<commit_after>package config

import (
	"errors"
	"os"
	"strings"

	"github.com/ondevice/ondevice/config/internal"
	"github.com/sirupsen/logrus"
)

// Auth -- contains API server credentials
type Auth = internal.Auth

// NewAuth -- creates new API server credentials
func NewAuth(user, auth, apiServer string) Auth {
	return internal.NewAuth(user, auth, apiServer)
}

func _getAuth(section string) (Auth, error) {
	if os.Getenv("ONDEVICE_USER") != "" || os.Getenv("ONDEVICE_AUTH") != "" {
		return internal.NewAuth(os.Getenv("ONDEVICE_USER"), os.Getenv("ONDEVICE_AUTH"), ""), nil
	}

	var cfg, err = Read()
	if err != nil {
		return nil, err
	}

	var username, auth string
	if username, err = cfg.GetString(section, "user"); err != nil {
		return nil, err
	}
	if auth, err = cfg.GetString(section, "auth"); err != nil {
		return nil, err
	}

	return internal.NewAuth(username, auth, ""), nil
}

// GetClientAuth -- Get the default client authentication information
func GetClientAuth() (Auth, error) {
	return _getAuth("client")
}

// GetClientUserAuth -- get the authentication credentials for a specific client user
func GetClientUserAuth(username string) (Auth, error) {
	var auth, err = GetClientAuth()
	if err == nil && strings.ToLower(auth.User()) == strings.ToLower(username) {
		return auth, nil
	}

	var cfg Config
	if cfg, err = Read(); err != nil {
		return nil, errors.New("failed to read ondevice.conf")
	}

	var key string
	if key, err = cfg.GetString("client", "auth_"+username); err == nil {
		return internal.NewAuth(username, key, ""), nil
	}

	return nil, errors.New("no client credentials found for user")
}

// GetDeviceAuth -- Get the device authentication
func GetDeviceAuth() (Auth, error) {
	return _getAuth("device")
}

// ListAuthenticatedUsers -- returns the names of users we have client auth for
func ListAuthenticatedUsers() []string {
	// TODO this is messy but will do for now - we'll improve this once we have a separate auth file

	var rc []string
	var uniqueUsers = make(map[string]bool)

	var cfg, err = Read()
	if err != nil {
		logrus.WithError(err).Fatal("failed to fetch configuration")
	}

	if mainUser, err := cfg.GetString("client", "user"); err != nil && mainUser != "" {
		rc = append(rc, mainUser)
		uniqueUsers[strings.ToLower(mainUser)] = true
	}

	for k := range cfg.AllValues() {
		if strings.HasPrefix(k, "client.auth_") {
			var name = k[12:]
			var lowerName = strings.ToLower(name)
			if !uniqueUsers[lowerName] {
				rc = append(rc, k[12:])
				uniqueUsers[lowerName] = true
			}
		}
	}

	return rc
}
<|endoftext|>


<commit_before>package config

import (
	"io/ioutil"

	"github.com/arschles/gci/log"
	"gopkg.in/yaml.v2"
)

const (
	defaultFileNameYaml = "gci.yaml"
	defaultFileNameYml  = "gci.yml"
)

// GetFile attempts to get and decode the File at name. If name is empty,
// tries defaultFileNameYaml and then defaultFileNameYml. If no file at name exists,
// or name was empty and neither defaultFileNameYaml nor defaultFileNameYml exists,
// returns ErrNoFile
func Read(name string) (*File, error) {
	var fileBytes []byte
	var err error
	fileNames := []string{name, defaultFileNameYaml, defaultFileNameYml}
	for _, fileName := range fileNames {
		b, err := ioutil.ReadFile(fileName)
		if err == nil {
			fileBytes = b
			break
		}
	}
	if err != nil {
		return nil, err
	}
	cf := &File{}
	if err := yaml.Unmarshal(fileBytes, cf); err != nil {
		return nil, err
	}
	return cf, nil
}

// ReadOrDie calls Read and if it returned an error, logs and exits
func ReadOrDie(name string) *File {
	cf, err := Read(name)
	if err != nil {
		log.Die("Reading config file %s [%s]", name, err)
		return nil
	}
	return cf
}

type File struct {
	Version string `yaml:"version"`
	Build   Build  `yaml:"build"`
	Test    Test   `yaml:"test"`
	Docker  Docker `yaml:"docker"`
}

type Build struct {
	OutputBinary string `yaml:"output-binary"`
}

func (b Build) GetOutputBinary(pathBase string) string {
	if b.OutputBinary == "" {
		return pathBase
	}
	return b.OutputBinary
}

type Test struct {
	Paths []string `yaml:"paths"`
}

func (t Test) GetPaths() []string {
	if len(t.Paths) == 0 {
		return []string{"./..."}
	}
	return t.Paths
}
<commit_msg>adding cross compile flag to the yaml parser<commit_after>package config

import (
	"io/ioutil"

	"github.com/arschles/gci/log"
	"gopkg.in/yaml.v2"
)

const (
	defaultFileNameYaml = "gci.yaml"
	defaultFileNameYml  = "gci.yml"
)

// GetFile attempts to get and decode the File at name. If name is empty,
// tries defaultFileNameYaml and then defaultFileNameYml. If no file at name exists,
// or name was empty and neither defaultFileNameYaml nor defaultFileNameYml exists,
// returns ErrNoFile
func Read(name string) (*File, error) {
	var fileBytes []byte
	var err error
	fileNames := []string{name, defaultFileNameYaml, defaultFileNameYml}
	for _, fileName := range fileNames {
		b, err := ioutil.ReadFile(fileName)
		if err == nil {
			fileBytes = b
			break
		}
	}
	if err != nil {
		return nil, err
	}
	cf := &File{}
	if err := yaml.Unmarshal(fileBytes, cf); err != nil {
		return nil, err
	}
	return cf, nil
}

// ReadOrDie calls Read and if it returned an error, logs and exits
func ReadOrDie(name string) *File {
	cf, err := Read(name)
	if err != nil {
		log.Die("Reading config file %s [%s]", name, err)
		return nil
	}
	return cf
}

type File struct {
	Version string `yaml:"version"`
	Build   Build  `yaml:"build"`
	Test    Test   `yaml:"test"`
	Docker  Docker `yaml:"docker"`
}

type Build struct {
	OutputBinary string `yaml:"output-binary"`
	CrossCompile bool   `yaml:"cross-compile"`
}

func (b Build) GetOutputBinary(pathBase string) string {
	if b.OutputBinary == "" {
		return pathBase
	}
	return b.OutputBinary
}

type Test struct {
	Paths []string `yaml:"paths"`
}

func (t Test) GetPaths() []string {
	if len(t.Paths) == 0 {
		return []string{"./..."}
	}
	return t.Paths
}
<|endoftext|>


<commit_before>package main

import (
	"github.com/stretchr/testify/assert"
	"io/ioutil"
	"os"
	"testing"
)

func TestConfigFile(t *testing.T) {
	tmpFile, err := ioutil.TempFile(os.TempDir(), "mamid_test")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("[smtp]\n")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("relay_host=localhost:25\n")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("mail_from=test@localhost\n")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("[notifier]\n")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("api_host=localhost:8080\n")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("contacts=contacts.ini\n")
	assert.NoError(t, err)
	tmpFile.Sync()
	tmpFile.Close()

	var p Parser
	relay, apiHost, contactsFile, err := p.ParseConfig(tmpFile.Name())
	assert.NoError(t, err)
	assert.Equal(t, contactsFile, "contacts.ini")
	assert.Equal(t, apiHost, "localhost:8080")
	assert.Equal(t, relay.Hostname, "localhost:25")
	assert.Equal(t, relay.MailFrom, "test@localhost")
}

func TestConfigFileMissingFile(t *testing.T) {
	tmpFile, err := ioutil.TempFile(os.TempDir(), "mamid_test")
	assert.NoError(t, err)
	os.Remove(tmpFile.Name())

	var p Parser
	_, _, _, err = p.ParseConfig(tmpFile.Name())
	assert.Error(t, err)
}

func TestConfigFileMissingConf(t *testing.T) {
	tmpFile, err := ioutil.TempFile(os.TempDir(), "mamid_test")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("[smtp]\n")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("relay_host=localhost:25\n")
	assert.NoError(t, err)
	//_, err = tmpFile.WriteString("mail_form=test@localhost\n")
	//assert.NoError(t, err)
	_, err = tmpFile.WriteString("[notifier]\n")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("api_host=localhost:8080\n")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("contacts=contacts.ini\n")
	assert.NoError(t, err)
	tmpFile.Sync()
	tmpFile.Close()

	var p Parser
	_, _, _, err = p.ParseConfig(tmpFile.Name())
	assert.Error(t, err)
}

func TestConfigFileMissingSection(t *testing.T) {
	tmpFile, err := ioutil.TempFile(os.TempDir(), "mamid_test")
	assert.NoError(t, err)
	//_, err = tmpFile.WriteString("[smtp]\n")
	//assert.NoError(t, err)
	_, err = tmpFile.WriteString("relay_host=localhost:25\n")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("mail_from=test@localhost\n")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("[notifier]\n")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("api_host=localhost:8080\n")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("contacts=contacts.ini\n")
	assert.NoError(t, err)
	tmpFile.Sync()
	tmpFile.Close()

	var p Parser
	_, _, _, err = p.ParseConfig(tmpFile.Name())
	assert.Error(t, err)
}

func TestConfigFileNoFile(t *testing.T) {
	var p Parser
	_, _, _, err := p.ParseConfig("")
	assert.Error(t, err)
}

func TestContactsFile(t *testing.T) {
	tmpFile, err := ioutil.TempFile(os.TempDir(), "mamid_test")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("[hans]\n")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("email=hans@localhost\n")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("[peter]\n")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("email=peter@localhost\n\n")
	assert.NoError(t, err)
	tmpFile.Sync()
	tmpFile.Close()

	var p Parser
	contacts, err := p.Parse(tmpFile.Name())
	assert.NoError(t, err)
	assert.Equal(t, len(contacts), 2)
	for i := 0; i < len(contacts); i++ {
		assert.IsType(t, EmailContact{}, contacts[i])
		assert.NotEmpty(t, contacts[i])
	}
}

func TestContactsFileAdditionalType(t *testing.T) {
	tmpFile, err := ioutil.TempFile(os.TempDir(), "mamid_test")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("[hans]\n")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("email=hans@localhost\n")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("[peter]\n")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("email=peter@localhost\n\n")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("jabber=peter@localhost\n\n")
	assert.NoError(t, err)
	tmpFile.Sync()
	tmpFile.Close()

	var p Parser
	contacts, err := p.Parse(tmpFile.Name())
	assert.NoError(t, err)
	assert.Equal(t, len(contacts), 2)
	for i := 0; i < len(contacts); i++ {
		assert.IsType(t, EmailContact{}, contacts[i])
		assert.NotEmpty(t, contacts[i])
	}
}

func TestContactsFileNoFile(t *testing.T) {
	var p Parser
	_, err := p.Parse("")
	assert.Error(t, err)
}
<commit_msg>FIX: notifier: parser test: delete tempfiles after execution<commit_after>package main

import (
	"github.com/stretchr/testify/assert"
	"io/ioutil"
	"os"
	"testing"
)

func TestConfigFile(t *testing.T) {
	tmpFile, err := ioutil.TempFile(os.TempDir(), "mamid_test")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("[smtp]\n")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("relay_host=localhost:25\n")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("mail_from=test@localhost\n")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("[notifier]\n")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("api_host=localhost:8080\n")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("contacts=contacts.ini\n")
	assert.NoError(t, err)
	tmpFile.Sync()
	tmpFile.Close()

	var p Parser
	relay, apiHost, contactsFile, err := p.ParseConfig(tmpFile.Name())
	assert.NoError(t, err)
	assert.Equal(t, contactsFile, "contacts.ini")
	assert.Equal(t, apiHost, "localhost:8080")
	assert.Equal(t, relay.Hostname, "localhost:25")
	assert.Equal(t, relay.MailFrom, "test@localhost")
}

func TestConfigFileMissingFile(t *testing.T) {
	tmpFile, err := ioutil.TempFile(os.TempDir(), "mamid_test")
	assert.NoError(t, err)
	os.Remove(tmpFile.Name())

	var p Parser
	_, _, _, err = p.ParseConfig(tmpFile.Name())
	assert.Error(t, err)
}

func TestConfigFileMissingConf(t *testing.T) {
	tmpFile, err := ioutil.TempFile(os.TempDir(), "mamid_test")
	assert.NoError(t, err)
	defer os.Remove(tmpFile.Name())
	_, err = tmpFile.WriteString("[smtp]\n")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("relay_host=localhost:25\n")
	assert.NoError(t, err)
	//_, err = tmpFile.WriteString("mail_form=test@localhost\n")
	//assert.NoError(t, err)
	_, err = tmpFile.WriteString("[notifier]\n")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("api_host=localhost:8080\n")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("contacts=contacts.ini\n")
	assert.NoError(t, err)
	tmpFile.Sync()
	tmpFile.Close()

	var p Parser
	_, _, _, err = p.ParseConfig(tmpFile.Name())
	assert.Error(t, err)
}

func TestConfigFileMissingSection(t *testing.T) {
	tmpFile, err := ioutil.TempFile(os.TempDir(), "mamid_test")
	assert.NoError(t, err)
	defer os.Remove(tmpFile.Name())
	//_, err = tmpFile.WriteString("[smtp]\n")
	//assert.NoError(t, err)
	_, err = tmpFile.WriteString("relay_host=localhost:25\n")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("mail_from=test@localhost\n")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("[notifier]\n")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("api_host=localhost:8080\n")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("contacts=contacts.ini\n")
	assert.NoError(t, err)
	tmpFile.Sync()
	tmpFile.Close()

	var p Parser
	_, _, _, err = p.ParseConfig(tmpFile.Name())
	assert.Error(t, err)
}

func TestConfigFileNoFile(t *testing.T) {
	var p Parser
	_, _, _, err := p.ParseConfig("")
	assert.Error(t, err)
}

func TestContactsFile(t *testing.T) {
	tmpFile, err := ioutil.TempFile(os.TempDir(), "mamid_test")
	assert.NoError(t, err)
	defer os.Remove(tmpFile.Name())
	_, err = tmpFile.WriteString("[hans]\n")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("email=hans@localhost\n")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("[peter]\n")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("email=peter@localhost\n\n")
	assert.NoError(t, err)
	tmpFile.Sync()
	tmpFile.Close()

	var p Parser
	contacts, err := p.Parse(tmpFile.Name())
	assert.NoError(t, err)
	assert.Equal(t, len(contacts), 2)
	for i := 0; i < len(contacts); i++ {
		assert.IsType(t, EmailContact{}, contacts[i])
		assert.NotEmpty(t, contacts[i])
	}
}

func TestContactsFileAdditionalType(t *testing.T) {
	tmpFile, err := ioutil.TempFile(os.TempDir(), "mamid_test")
	assert.NoError(t, err)
	defer os.Remove(tmpFile.Name())
	_, err = tmpFile.WriteString("[hans]\n")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("email=hans@localhost\n")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("[peter]\n")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("email=peter@localhost\n\n")
	assert.NoError(t, err)
	_, err = tmpFile.WriteString("jabber=peter@localhost\n\n")
	assert.NoError(t, err)
	tmpFile.Sync()
	tmpFile.Close()

	var p Parser
	contacts, err := p.Parse(tmpFile.Name())
	assert.NoError(t, err)
	assert.Equal(t, len(contacts), 2)
	for i := 0; i < len(contacts); i++ {
		assert.IsType(t, EmailContact{}, contacts[i])
		assert.NotEmpty(t, contacts[i])
	}
}

func TestContactsFileNoFile(t *testing.T) {
	var p Parser
	_, err := p.Parse("")
	assert.Error(t, err)
}
<|endoftext|>


<commit_before>// find the intersection of two arrays.
// e.x. a1 = [1 2 2 4 6]; a2 = [2 4 5]
// Intersect(a1, a2) >> [2 4]
func IntersectString(args ...[]string) []string {
	// hold the values of the arrays;
	// the first map of uint64 represents the index of args
	// the second map of strings represents that values in the arg arrays
	tempMap := make(map[uint64]map[string]uint8)
	for n, arg := range args {
		tempMap[uint64(n)] = make(map[string]uint8)
		for idx := range arg {
			tempMap[uint64(n)][arg[idx]] = 0
		}
	}

	// hold the interesctions; incremental keys are successive intersections
	intersectMap := make(map[uint64]map[string]uint8)
	intersectMap[0] = make(map[string]uint8)
	// we need to write the first one
	for key := range tempMap[0] {
		intersectMap[0][key] = 0
	}

	for i := 1; i < len(tempMap); i++ {
		intersectMap[uint64(i)] = make(map[string]uint8)
		for key := range tempMap[uint64(i)] {
			if _, ok := intersectMap[uint64(i-1)][key]; ok {
				intersectMap[uint64(i)][key] = 0
			}
		}
	}

	// write the final val of the intersectMap to an array and return
	tempArray := make([]string, 0)
	for key := range intersectMap[uint64(len(intersectMap)-1)] {
		tempArray = append(tempArray, key)
	}

	return tempArray
}

// find the union of two arrays.
// e.x. a1 = [1 2 2 4 6]; a2 = [2 4 5]
// Union(a1, a2) >> [1 2 4 5 6]
func UnionString(args ...[]string) []string {
	tempMap := make(map[string]uint8)

	for _, arg := range args {
		for idx := range arg {
			tempMap[arg[idx]] = 0
		}
	}

	tempArray := make([]string, 0)
	for key := range tempMap {
		tempArray = append(tempArray, key)
	}
	return tempArray
}

// find the difference of two arrays.
// e.x. a1 = [1 2 2 4 6]; a2 = [2 4 5]
// Difference(a1, a2) >> [5 6]
func DifferenceString(args ...[]string) []string {
	// hold the values of the arrays;
	// the first map of uint64 represents the index of args
	// the second map of strings represents that values in the arg arrays
	tempMap := make(map[uint64]map[string]uint8)
	for n, arg := range args {
		tempMap[uint64(n)] = make(map[string]uint8)
		for idx := range arg {
			tempMap[uint64(n)][arg[idx]] = 0
		}
	}

	// hold the differences
	diffMap := make(map[uint64]map[string]uint8)
	diffMap[0] = make(map[string]uint8)
	// we need to start with one of the arrays
	for key := range tempMap[uint64(0)] {
		diffMap[0][key] = 0
	}

	for i := 1; i < len(tempMap); i++ {
		diffMap[uint64(i)] = make(map[string]uint8)
		for key := range diffMap[uint64(i-1)] {
			if _, ok := tempMap[uint64(i)][key]; !ok {
				diffMap[uint64(i)][key] = 0
			}
		}
		for key := range tempMap[uint64(i)] {
			if _, ok := diffMap[uint64(i-1)][key]; !ok {
				diffMap[uint64(i)][key] = 0
			}
		}
	}

	// write the final val of the diffMap to an array and return
	tempArray := make([]string, 0)
	for key := range diffMap[uint64(len(diffMap)-1)] {
		tempArray = append(tempArray, key)
	}

	return tempArray
}

// Remove duplicate values from one array.
// e.x. a1 = [1 2 2 4 6]
// Distinct(a1) >> [1 2 4 6]
func Distinct(arg []string) []string {
	tempMap := make(map[string]uint8)

	for idx := range arg {
		tempMap[arg[idx]] = 0
	}

	tempArray := make([]string, 0)
	for key := range tempMap {
		tempArray = append(tempArray, key)
	}
	return tempArray
}
<commit_msg>fixed broken difference algorithm. Made improvements to intersection algorithm<commit_after>// find the intersection of two arrays.
// e.x. a1 = [1 2 2 4 6]; a2 = [2 4 5]
// Intersect(a1, a2) >> [2 4]
func IntersectString(args ...[]string) []string {
	// create a map to count all the instances of the strings
	tempMap := make(map[string]int)
	for _, arg := range args {
		for idx := range arg {
			if _, ok := tempMap[arg[idx]]; ok {
				tempMap[arg[idx]]++
			} else {
				tempMap[arg[idx]] = 1
			}
		}
	}

	// find the keys equal to the length of the input args
	tempArray := make([]string, 0)
	for key := range tempMap {
		if tempMap[key] == len(args) {
			tempArray = append(tempArray, key)
		}
	}

	return tempArray
}

// find the union of two arrays.
// e.x. a1 = [1 2 2 4 6]; a2 = [2 4 5]
// Union(a1, a2) >> [1 2 4 5 6]
func UnionString(args ...[]string) []string {
	tempMap := make(map[string]uint8)

	for _, arg := range args {
		for idx := range arg {
			tempMap[arg[idx]] = 0
		}
	}

	tempArray := make([]string, 0)
	for key := range tempMap {
		tempArray = append(tempArray, key)
	}
	return tempArray
}

// find the difference of two arrays.
// e.x. a1 = [1 2 2 4 6]; a2 = [2 4 5]
// Difference(a1, a2) >> [5 6]
func DifferenceString(args ...[]string) []string {
	// craete a map to count all the instances of the strings
	tempMap := make(map[string]int)
	for _, arg := range args {
		for idx := range arg {
			if _, ok := tempMap[arg[idx]]; ok {
				tempMap[arg[idx]]++
			} else {
				tempMap[arg[idx]] = 1
			}
		}
	}

	// find the keys with only 1 instance
	tempArray := make([]string, 0)
	for key := range tempMap {
		if tempMap[key] == 1 {
			tempArray = append(tempArray, key)
		}
	}

	return tempArray
}

// Remove duplicate values from one array.
// e.x. a1 = [1 2 2 4 6]
// Distinct(a1) >> [1 2 4 6]
func Distinct(arg []string) []string {
	tempMap := make(map[string]uint8)

	for idx := range arg {
		tempMap[arg[idx]] = 0
	}

	tempArray := make([]string, 0)
	for key := range tempMap {
		tempArray = append(tempArray, key)
	}
	return tempArray
}
<|endoftext|>


<commit_before>package config

import (
	"encoding/json"
	"os"
	"time"

	"github.com/coreos/etcd/client"
	"golang.org/x/net/context"
)

var (
	etcdEndpoint = "localhost:2379"
	configPath   = "/config/example_api.json"
	configLoaded bool
)

// Cnf ...
// Let's start with some sensible defaults
var Cnf = &Config{
	Database: DatabaseConfig{
		Type:         "postgres",
		Host:         "localhost",
		Port:         5432,
		User:         "example_api",
		Password:     "",
		DatabaseName: "example_api",
		MaxIdleConns: 5,
		MaxOpenConns: 5,
	},
	Oauth: OauthConfig{
		AccessTokenLifetime:  3600,    // 1 hour
		RefreshTokenLifetime: 1209600, // 14 days
		AuthCodeLifetime:     3600,    // 1 hour
	},
	Facebook: FacebookConfig{
		AppID:     "facebook_app_id",
		AppSecret: "facebook_app_secret",
	},
	Sendgrid: SendgridConfig{
		APIKey: "sendgrid_api_key",
	},
	Web: WebConfig{
		Scheme:    "http",
		Host:      "localhost:8080",
		AppScheme: "http",
		AppHost:   "localhost:8000",
	},
	AppSpecific: AppSpecificConfig{
		PasswordResetLifetime: 604800, // 7 days
		CompanyName:           "Your Company Name",
		CompanyEmail:          "contact@example.com",
	},
	IsDevelopment: true,
}

func init() {
	// Overwrite default values with environment variables if they are set
	if os.Getenv("ETCD_ENDPOINT") != "" {
		etcdEndpoint = os.Getenv("ETCD_ENDPOINT")
	}
	if os.Getenv("ETCD_CONFIG_PATH") != "" {
		configPath = os.Getenv("ETCD_CONFIG_PATH")
	}
}

// NewConfig loads configuration from etcd and returns *Config struct
// It also starts a goroutine in the background to keep config up-to-date
func NewConfig(mustLoadOnce bool, keepReloading bool) *Config {
	if configLoaded {
		return Cnf
	}

	// Log the etcd endpoint for debugging purposes
	logger.Infof("ETCD Endpoint: %s", etcdEndpoint)

	// ETCD config
	etcdClientConfig := client.Config{
		Endpoints: []string{etcdEndpoint},
		Transport: client.DefaultTransport,
		// set timeout per request to fail fast when the target endpoint is unavailable
		HeaderTimeoutPerRequest: time.Second,
	}

	// ETCD client
	etcdClient, err := client.New(etcdClientConfig)
	if err != nil {
		logger.Fatal(err)
		os.Exit(1)
	}

	// ETCD keys API
	kapi := client.NewKeysAPI(etcdClient)

	// If the config must be loaded once successfully
	if mustLoadOnce {
		// Read from remote config the first time
		newCnf, err := LoadConfig(kapi)
		if err != nil {
			logger.Fatal(err)
			os.Exit(1)
		}

		// Refresh the config
		RefreshConfig(newCnf)

		// Set configLoaded to true
		configLoaded = true
		logger.Info("Successfully loaded config for the first time")
	}

	if keepReloading {
		// Open a goroutine to watch remote changes forever
		go func() {
			for {
				// Delay after each request
				time.Sleep(time.Second * 10)

				// Attempt to reload the config
				newCnf, err := LoadConfig(kapi)
				if err != nil {
					logger.Error(err)
					continue
				}

				// Refresh the config
				RefreshConfig(newCnf)

				// Set configLoaded to true
				configLoaded = true
				logger.Info("Successfully reloaded config")
			}
		}()
	}

	return Cnf
}

// LoadConfig gets the JSON from ETCD and unmarshals it to the config object
func LoadConfig(kapi client.KeysAPI) (*Config, error) {
	// Read from remote config the first time
	resp, err := kapi.Get(context.Background(), configPath, nil)
	if err != nil {
		return nil, err
	}

	// Unmarshal the config JSON into the cnf object
	newCnf := new(Config)
	if err := json.Unmarshal([]byte(resp.Node.Value), newCnf); err != nil {
		return nil, err
	}

	return newCnf, nil
}

// RefreshConfig sets config through the pointer so config actually gets refreshed
func RefreshConfig(newCnf *Config) {
	*Cnf = *newCnf
}
<commit_msg>Added support for encrypted etcd client connection.<commit_after>package config

import (
	"crypto/tls"
	"crypto/x509"
	"encoding/json"
	"io/ioutil"
	"net/http"
	"os"
	"time"

	"github.com/coreos/etcd/client"
	"golang.org/x/net/context"
)

var (
	etcdEndpoint                          = "http://localhost:2379"
	etcdCertFile, etcdKeyFile, etcdCaFile string
	etcdConfigPath                        = "/config/example_api.json"
	configLoaded                          bool
)

// Cnf ...
// Let's start with some sensible defaults
var Cnf = &Config{
	Database: DatabaseConfig{
		Type:         "postgres",
		Host:         "localhost",
		Port:         5432,
		User:         "example_api",
		Password:     "",
		DatabaseName: "example_api",
		MaxIdleConns: 5,
		MaxOpenConns: 5,
	},
	Oauth: OauthConfig{
		AccessTokenLifetime:  3600,    // 1 hour
		RefreshTokenLifetime: 1209600, // 14 days
		AuthCodeLifetime:     3600,    // 1 hour
	},
	Facebook: FacebookConfig{
		AppID:     "facebook_app_id",
		AppSecret: "facebook_app_secret",
	},
	Sendgrid: SendgridConfig{
		APIKey: "sendgrid_api_key",
	},
	Web: WebConfig{
		Scheme:    "http",
		Host:      "localhost:8080",
		AppScheme: "http",
		AppHost:   "localhost:8000",
	},
	AppSpecific: AppSpecificConfig{
		PasswordResetLifetime: 604800, // 7 days
		CompanyName:           "Your Company Name",
		CompanyEmail:          "contact@example.com",
	},
	IsDevelopment: true,
}

func init() {
	// Overwrite default values with environment variables if they are set
	if os.Getenv("ETCD_ENDPOINT") != "" {
		etcdEndpoint = os.Getenv("ETCD_ENDPOINT")
	}
	if os.Getenv("ETCD_CERT_FILE") != "" {
		etcdCertFile = os.Getenv("ETCD_CERT_FILE")
	}
	if os.Getenv("ETCD_KEY_FILE") != "" {
		etcdKeyFile = os.Getenv("ETCD_KEY_FILE")
	}
	if os.Getenv("ETCD_CA_FILE") != "" {
		etcdCaFile = os.Getenv("ETCD_CA_FILE")
	}
	if os.Getenv("ETCD_CONFIG_PATH") != "" {
		etcdConfigPath = os.Getenv("ETCD_CONFIG_PATH")
	}
}

// NewConfig loads configuration from etcd and returns *Config struct
// It also starts a goroutine in the background to keep config up-to-date
func NewConfig(mustLoadOnce bool, keepReloading bool) *Config {
	if configLoaded {
		return Cnf
	}

	// Init ETCD client
	etcdClient, err := newEtcdClient(etcdEndpoint, etcdCertFile, etcdKeyFile, etcdCaFile)
	if err != nil {
		logger.Fatal(err)
		os.Exit(1)
	}

	// ETCD keys API
	kapi := client.NewKeysAPI(*etcdClient)

	// If the config must be loaded once successfully
	if mustLoadOnce {
		// Read from remote config the first time
		newCnf, err := LoadConfig(kapi)
		if err != nil {
			logger.Fatal(err)
			os.Exit(1)
		}

		// Refresh the config
		RefreshConfig(newCnf)

		// Set configLoaded to true
		configLoaded = true
		logger.Info("Successfully loaded config for the first time")
	}

	if keepReloading {
		// Open a goroutine to watch remote changes forever
		go func() {
			for {
				// Delay after each request
				time.Sleep(time.Second * 10)

				// Attempt to reload the config
				newCnf, err := LoadConfig(kapi)
				if err != nil {
					logger.Error(err)
					continue
				}

				// Refresh the config
				RefreshConfig(newCnf)

				// Set configLoaded to true
				configLoaded = true
				logger.Info("Successfully reloaded config")
			}
		}()
	}

	return Cnf
}

// LoadConfig gets the JSON from ETCD and unmarshals it to the config object
func LoadConfig(kapi client.KeysAPI) (*Config, error) {
	// Read from remote config the first time
	resp, err := kapi.Get(context.Background(), etcdConfigPath, nil)
	if err != nil {
		return nil, err
	}

	// Unmarshal the config JSON into the cnf object
	newCnf := new(Config)
	if err := json.Unmarshal([]byte(resp.Node.Value), newCnf); err != nil {
		return nil, err
	}

	return newCnf, nil
}

// RefreshConfig sets config through the pointer so config actually gets refreshed
func RefreshConfig(newCnf *Config) {
	*Cnf = *newCnf
}

func newEtcdClient(theEndpoint, certFile, keyFile, caFile string) (*client.Client, error) {
	// Log the etcd endpoint for debugging purposes
	logger.Infof("ETCD Endpoint: %s", etcdEndpoint)

	// Start with the default HTTP transport
	var transport = client.DefaultTransport

	// Optionally, configure TLS transport
	if certFile != "" && keyFile != "" && caFile != "" {
		// Load client cert
		cert, err := tls.LoadX509KeyPair(certFile, keyFile)
		if err != nil {
			return nil, err
		}

		// Load CA cert
		caCert, err := ioutil.ReadFile(caFile)
		if err != nil {
			return nil, err
		}
		caCertPool := x509.NewCertPool()
		caCertPool.AppendCertsFromPEM(caCert)

		// Setup HTTPS client
		tlsConfig := &tls.Config{
			Certificates: []tls.Certificate{cert},
			RootCAs:      caCertPool,
		}
		tlsConfig.BuildNameToCertificate()
		transport = &http.Transport{TLSClientConfig: tlsConfig}
	}

	// ETCD config
	etcdClientConfig := client.Config{
		Endpoints: []string{theEndpoint},
		Transport: transport,
		// set timeout per request to fail fast when the target endpoint is unavailable
		HeaderTimeoutPerRequest: time.Second,
	}

	// ETCD client
	etcdClient, err := client.New(etcdClientConfig)
	if err != nil {
		return nil, err
	}

	return &etcdClient, nil
}
<|endoftext|>


<commit_before>/*
Copyright 2014 Google Inc. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package controller

import (
	"fmt"
	"time"

	"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
	"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
	"github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider"
	"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
	"github.com/golang/glog"
)

type MinionController struct {
	cloud           cloudprovider.Interface
	matchRE         string
	staticResources *api.NodeResources
	minions         []string
	kubeClient      client.Interface
}

// NewMinionController returns a new minion controller to sync instances from cloudprovider.
func NewMinionController(
	cloud cloudprovider.Interface,
	matchRE string,
	minions []string,
	staticResources *api.NodeResources,
	kubeClient client.Interface) *MinionController {
	return &MinionController{
		cloud:           cloud,
		matchRE:         matchRE,
		minions:         minions,
		staticResources: staticResources,
		kubeClient:      kubeClient,
	}
}

// Run starts syncing instances from cloudprovider periodically, or create initial minion list.
func (s *MinionController) Run(period time.Duration) {
	if s.cloud != nil && len(s.matchRE) > 0 {
		go util.Forever(func() { s.SyncCloud() }, period)
	} else {
		go s.SyncStatic(period)
	}
}

// SyncStatic registers list of machines from command line flag. It returns after successful
// registration of all machines.
func (s *MinionController) SyncStatic(period time.Duration) error {
	registered := util.NewStringSet()
	for {
		for _, minionID := range s.minions {
			if registered.Has(minionID) {
				continue
			}
			_, err := s.kubeClient.Minions().Create(&api.Minion{
				ObjectMeta: api.ObjectMeta{Name: minionID},
				Spec: api.NodeSpec{
					Capacity: s.staticResources.Capacity,
				},
			})
			if err == nil {
				registered.Insert(minionID)
			}
		}
		if registered.Len() == len(s.minions) {
			return nil
		}
		time.Sleep(period)
	}
	return nil
}

// SyncCloud syncs list of instances from cloudprovider to master etcd registry.
func (s *MinionController) SyncCloud() error {
	matches, err := s.cloudMinions()
	if err != nil {
		return err
	}
	minions, err := s.kubeClient.Minions().List()
	if err != nil {
		return err
	}
	minionMap := make(map[string]*api.Minion)
	for _, minion := range minions.Items {
		minionMap[minion.Name] = &minion
	}

	// Create or delete minions from registry.
	for _, minion := range matches.Items {
		if _, ok := minionMap[minion.Name]; !ok {
			glog.Infof("Create minion in registry: %s", minion.Name)
			_, err = s.kubeClient.Minions().Create(&minion)
			if err != nil {
				glog.Errorf("Create minion error: %s", minion.Name)
			}
		}
		delete(minionMap, minion.Name)
	}

	for minionID := range minionMap {
		glog.Infof("Delete minion from registry: %s", minionID)
		err = s.kubeClient.Minions().Delete(minionID)
		if err != nil {
			glog.Errorf("Delete minion error: %s", minionID)
		}
	}
	return nil
}

// cloudMinions constructs and returns api.MinionList from cloudprovider.
func (s *MinionController) cloudMinions() (*api.MinionList, error) {
	instances, ok := s.cloud.Instances()
	if !ok {
		return nil, fmt.Errorf("cloud doesn't support instances")
	}
	matches, err := instances.List(s.matchRE)
	if err != nil {
		return nil, err
	}
	result := &api.MinionList{
		Items: make([]api.Minion, len(matches)),
	}
	for i := range matches {
		result.Items[i].Name = matches[i]
		resources, err := instances.GetNodeResources(matches[i])
		if err != nil {
			return nil, err
		}
		if resources == nil {
			resources = s.staticResources
		}
		if resources != nil {
			result.Items[i].Spec.Capacity = resources.Capacity
		}
	}
	return result, nil
}
<commit_msg>Print SyncCloud errors<commit_after>/*
Copyright 2014 Google Inc. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package controller

import (
	"fmt"
	"time"

	"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
	"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
	"github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider"
	"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
	"github.com/golang/glog"
)

type MinionController struct {
	cloud           cloudprovider.Interface
	matchRE         string
	staticResources *api.NodeResources
	minions         []string
	kubeClient      client.Interface
}

// NewMinionController returns a new minion controller to sync instances from cloudprovider.
func NewMinionController(
	cloud cloudprovider.Interface,
	matchRE string,
	minions []string,
	staticResources *api.NodeResources,
	kubeClient client.Interface) *MinionController {
	return &MinionController{
		cloud:           cloud,
		matchRE:         matchRE,
		minions:         minions,
		staticResources: staticResources,
		kubeClient:      kubeClient,
	}
}

// Run starts syncing instances from cloudprovider periodically, or create initial minion list.
func (s *MinionController) Run(period time.Duration) {
	if s.cloud != nil && len(s.matchRE) > 0 {
		go util.Forever(func() {
			if err := s.SyncCloud(); err != nil {
				glog.Errorf("Error syncing cloud: %v", err)
			}
		}, period)
	} else {
		go s.SyncStatic(period)
	}
}

// SyncStatic registers list of machines from command line flag. It returns after successful
// registration of all machines.
func (s *MinionController) SyncStatic(period time.Duration) error {
	registered := util.NewStringSet()
	for {
		for _, minionID := range s.minions {
			if registered.Has(minionID) {
				continue
			}
			_, err := s.kubeClient.Minions().Create(&api.Minion{
				ObjectMeta: api.ObjectMeta{Name: minionID},
				Spec: api.NodeSpec{
					Capacity: s.staticResources.Capacity,
				},
			})
			if err == nil {
				registered.Insert(minionID)
			}
		}
		if registered.Len() == len(s.minions) {
			return nil
		}
		time.Sleep(period)
	}
	return nil
}

// SyncCloud syncs list of instances from cloudprovider to master etcd registry.
func (s *MinionController) SyncCloud() error {
	matches, err := s.cloudMinions()
	if err != nil {
		return err
	}
	minions, err := s.kubeClient.Minions().List()
	if err != nil {
		return err
	}
	minionMap := make(map[string]*api.Minion)
	for _, minion := range minions.Items {
		minionMap[minion.Name] = &minion
	}

	// Create or delete minions from registry.
	for _, minion := range matches.Items {
		if _, ok := minionMap[minion.Name]; !ok {
			glog.Infof("Create minion in registry: %s", minion.Name)
			_, err = s.kubeClient.Minions().Create(&minion)
			if err != nil {
				glog.Errorf("Create minion error: %s", minion.Name)
			}
		}
		delete(minionMap, minion.Name)
	}

	for minionID := range minionMap {
		glog.Infof("Delete minion from registry: %s", minionID)
		err = s.kubeClient.Minions().Delete(minionID)
		if err != nil {
			glog.Errorf("Delete minion error: %s", minionID)
		}
	}
	return nil
}

// cloudMinions constructs and returns api.MinionList from cloudprovider.
func (s *MinionController) cloudMinions() (*api.MinionList, error) {
	instances, ok := s.cloud.Instances()
	if !ok {
		return nil, fmt.Errorf("cloud doesn't support instances")
	}
	matches, err := instances.List(s.matchRE)
	if err != nil {
		return nil, err
	}
	result := &api.MinionList{
		Items: make([]api.Minion, len(matches)),
	}
	for i := range matches {
		result.Items[i].Name = matches[i]
		resources, err := instances.GetNodeResources(matches[i])
		if err != nil {
			return nil, err
		}
		if resources == nil {
			resources = s.staticResources
		}
		if resources != nil {
			result.Items[i].Spec.Capacity = resources.Capacity
		}
	}
	return result, nil
}
<|endoftext|>


<commit_before>package consensus

import (
	"bufio"
	"bytes"
	"fmt"
	"math/rand"
	"os"
	"path/filepath"
	"strings"
	"time"

	"github.com/pkg/errors"
	"github.com/tendermint/abci/example/dummy"
	bc "github.com/tendermint/tendermint/blockchain"
	cfg "github.com/tendermint/tendermint/config"
	"github.com/tendermint/tendermint/proxy"
	sm "github.com/tendermint/tendermint/state"
	"github.com/tendermint/tendermint/types"
	auto "github.com/tendermint/tmlibs/autofile"
	"github.com/tendermint/tmlibs/db"
	"github.com/tendermint/tmlibs/log"
)

// WALWithNBlocks generates a consensus WAL. It does this by spining up a
// stripped down version of node (proxy app, event bus, consensus state) with a
// persistent dummy application and special consensus wal instance
// (byteBufferWAL) and waits until numBlocks are created. Then it returns a WAL
// content.
func WALWithNBlocks(numBlocks int) (data []byte, err error) {
	config := getConfig()

	app := dummy.NewPersistentDummyApplication(filepath.Join(config.DBDir(), "wal_generator"))

	logger := log.NewNopLogger() // log.TestingLogger().With("wal_generator", "wal_generator")

	/////////////////////////////////////////////////////////////////////////////
	// COPY PASTE FROM node.go WITH A FEW MODIFICATIONS
	// NOTE: we can't import node package because of circular dependency
	privValidatorFile := config.PrivValidatorFile()
	privValidator := types.LoadOrGenPrivValidatorFS(privValidatorFile)
	genDoc, err := types.GenesisDocFromFile(config.GenesisFile())
	if err != nil {
		return nil, errors.Wrap(err, "failed to read genesis file")
	}
	stateDB := db.NewMemDB()
	blockStoreDB := db.NewMemDB()
	state, err := sm.MakeGenesisState(stateDB, genDoc)
	state.SetLogger(logger.With("module", "state"))
	if err != nil {
		return nil, errors.Wrap(err, "failed to make genesis state")
	}
	blockStore := bc.NewBlockStore(blockStoreDB)
	handshaker := NewHandshaker(state, blockStore)
	proxyApp := proxy.NewAppConns(proxy.NewLocalClientCreator(app), handshaker)
	proxyApp.SetLogger(logger.With("module", "proxy"))
	if err := proxyApp.Start(); err != nil {
		return nil, errors.Wrap(err, "failed to start proxy app connections")
	}
	defer proxyApp.Stop()
	eventBus := types.NewEventBus()
	eventBus.SetLogger(logger.With("module", "events"))
	if err := eventBus.Start(); err != nil {
		return nil, errors.Wrap(err, "failed to start event bus")
	}
	mempool := types.MockMempool{}
	consensusState := NewConsensusState(config.Consensus, state.Copy(), proxyApp.Consensus(), blockStore, mempool)
	consensusState.SetLogger(logger)
	consensusState.SetEventBus(eventBus)
	if privValidator != nil {
		consensusState.SetPrivValidator(privValidator)
	}
	// END OF COPY PASTE
	/////////////////////////////////////////////////////////////////////////////

	// set consensus wal to buffered WAL, which will write all incoming msgs to buffer
	var b bytes.Buffer
	wr := bufio.NewWriter(&b)
	numBlocksWritten := make(chan struct{})
	wal := newByteBufferWAL(NewWALEncoder(wr), int64(numBlocks), numBlocksWritten)
	// see wal.go#103
	wal.Save(EndHeightMessage{0})
	consensusState.wal = wal

	if err := consensusState.Start(); err != nil {
		return nil, errors.Wrap(err, "failed to start consensus state")
	}
	defer consensusState.Stop()

	select {
	case <-numBlocksWritten:
		wr.Flush()
		return b.Bytes(), nil
	case <-time.After(time.Duration(5*numBlocks) * time.Second):
		return b.Bytes(), fmt.Errorf("waited too long for tendermint to produce %d blocks", numBlocks)
	}
}

// f**ing long, but unique for each test
func makePathname() string {
	// get path
	p, err := os.Getwd()
	if err != nil {
		panic(err)
	}
	// fmt.Println(p)
	sep := string(filepath.Separator)
	return strings.Replace(p, sep, "_", -1)
}

func randPort() int {
	// returns between base and base + spread
	base, spread := 20000, 20000
	return base + rand.Intn(spread)
}

func makeAddrs() (string, string, string) {
	start := randPort()
	return fmt.Sprintf("tcp://0.0.0.0:%d", start),
		fmt.Sprintf("tcp://0.0.0.0:%d", start+1),
		fmt.Sprintf("tcp://0.0.0.0:%d", start+2)
}

// getConfig returns a config for test cases
func getConfig() *cfg.Config {
	pathname := makePathname()
	c := cfg.ResetTestRoot(pathname)

	// and we use random ports to run in parallel
	tm, rpc, grpc := makeAddrs()
	c.P2P.ListenAddress = tm
	c.RPC.ListenAddress = rpc
	c.RPC.GRPCListenAddress = grpc
	return c
}

// byteBufferWAL is a WAL which writes all msgs to a byte buffer. Writing stops
// when the heightToStop is reached. Client will be notified via
// signalWhenStopsTo channel.
type byteBufferWAL struct {
	enc               *WALEncoder
	stopped           bool
	heightToStop      int64
	signalWhenStopsTo chan struct{}
}

// needed for determinism
var fixedTime, _ = time.Parse(time.RFC3339, "2017-01-02T15:04:05Z")

func newByteBufferWAL(enc *WALEncoder, nBlocks int64, signalStop chan struct{}) *byteBufferWAL {
	return &byteBufferWAL{
		enc:               enc,
		heightToStop:      nBlocks,
		signalWhenStopsTo: signalStop,
	}
}

// Save writes message to the internal buffer except when heightToStop is
// reached, in which case it will signal the caller via signalWhenStopsTo and
// skip writing.
func (w *byteBufferWAL) Save(m WALMessage) {
	if w.stopped {
		return
	}

	if endMsg, ok := m.(EndHeightMessage); ok {
		if endMsg.Height == w.heightToStop {
			w.signalWhenStopsTo <- struct{}{}
			w.stopped = true
			return
		}
	}

	err := w.enc.Encode(&TimedWALMessage{fixedTime, m})
	if err != nil {
		panic(fmt.Sprintf("failed to encode the msg %v", m))
	}
}

func (w *byteBufferWAL) Group() *auto.Group {
	panic("not implemented")
}
func (w *byteBufferWAL) SearchForEndHeight(height int64, options *WALSearchOptions) (gr *auto.GroupReader, found bool, err error) {
	return nil, false, nil
}

func (w *byteBufferWAL) Start() error { return nil }
func (w *byteBufferWAL) Stop() error  { return nil }
func (w *byteBufferWAL) Wait()        {}
<commit_msg>enable logging for wal_generator and set timeout to 1 min<commit_after>package consensus

import (
	"bufio"
	"bytes"
	"fmt"
	"math/rand"
	"os"
	"path/filepath"
	"strings"
	"time"

	"github.com/pkg/errors"
	"github.com/tendermint/abci/example/dummy"
	bc "github.com/tendermint/tendermint/blockchain"
	cfg "github.com/tendermint/tendermint/config"
	"github.com/tendermint/tendermint/proxy"
	sm "github.com/tendermint/tendermint/state"
	"github.com/tendermint/tendermint/types"
	auto "github.com/tendermint/tmlibs/autofile"
	"github.com/tendermint/tmlibs/db"
	"github.com/tendermint/tmlibs/log"
)

// WALWithNBlocks generates a consensus WAL. It does this by spining up a
// stripped down version of node (proxy app, event bus, consensus state) with a
// persistent dummy application and special consensus wal instance
// (byteBufferWAL) and waits until numBlocks are created. Then it returns a WAL
// content.
func WALWithNBlocks(numBlocks int) (data []byte, err error) {
	config := getConfig()

	app := dummy.NewPersistentDummyApplication(filepath.Join(config.DBDir(), "wal_generator"))

	logger := log.TestingLogger().With("wal_generator", "wal_generator")

	/////////////////////////////////////////////////////////////////////////////
	// COPY PASTE FROM node.go WITH A FEW MODIFICATIONS
	// NOTE: we can't import node package because of circular dependency
	privValidatorFile := config.PrivValidatorFile()
	privValidator := types.LoadOrGenPrivValidatorFS(privValidatorFile)
	genDoc, err := types.GenesisDocFromFile(config.GenesisFile())
	if err != nil {
		return nil, errors.Wrap(err, "failed to read genesis file")
	}
	stateDB := db.NewMemDB()
	blockStoreDB := db.NewMemDB()
	state, err := sm.MakeGenesisState(stateDB, genDoc)
	state.SetLogger(logger.With("module", "state"))
	if err != nil {
		return nil, errors.Wrap(err, "failed to make genesis state")
	}
	blockStore := bc.NewBlockStore(blockStoreDB)
	handshaker := NewHandshaker(state, blockStore)
	proxyApp := proxy.NewAppConns(proxy.NewLocalClientCreator(app), handshaker)
	proxyApp.SetLogger(logger.With("module", "proxy"))
	if err := proxyApp.Start(); err != nil {
		return nil, errors.Wrap(err, "failed to start proxy app connections")
	}
	defer proxyApp.Stop()
	eventBus := types.NewEventBus()
	eventBus.SetLogger(logger.With("module", "events"))
	if err := eventBus.Start(); err != nil {
		return nil, errors.Wrap(err, "failed to start event bus")
	}
	mempool := types.MockMempool{}
	consensusState := NewConsensusState(config.Consensus, state.Copy(), proxyApp.Consensus(), blockStore, mempool)
	consensusState.SetLogger(logger)
	consensusState.SetEventBus(eventBus)
	if privValidator != nil {
		consensusState.SetPrivValidator(privValidator)
	}
	// END OF COPY PASTE
	/////////////////////////////////////////////////////////////////////////////

	// set consensus wal to buffered WAL, which will write all incoming msgs to buffer
	var b bytes.Buffer
	wr := bufio.NewWriter(&b)
	numBlocksWritten := make(chan struct{})
	wal := newByteBufferWAL(NewWALEncoder(wr), int64(numBlocks), numBlocksWritten)
	// see wal.go#103
	wal.Save(EndHeightMessage{0})
	consensusState.wal = wal

	if err := consensusState.Start(); err != nil {
		return nil, errors.Wrap(err, "failed to start consensus state")
	}
	defer consensusState.Stop()

	select {
	case <-numBlocksWritten:
		wr.Flush()
		return b.Bytes(), nil
	case <-time.After(1 * time.Minute):
		return b.Bytes(), fmt.Errorf("waited too long for tendermint to produce %d blocks (grep logs for `wal_generator`)", numBlocks)
	}
}

// f**ing long, but unique for each test
func makePathname() string {
	// get path
	p, err := os.Getwd()
	if err != nil {
		panic(err)
	}
	// fmt.Println(p)
	sep := string(filepath.Separator)
	return strings.Replace(p, sep, "_", -1)
}

func randPort() int {
	// returns between base and base + spread
	base, spread := 20000, 20000
	return base + rand.Intn(spread)
}

func makeAddrs() (string, string, string) {
	start := randPort()
	return fmt.Sprintf("tcp://0.0.0.0:%d", start),
		fmt.Sprintf("tcp://0.0.0.0:%d", start+1),
		fmt.Sprintf("tcp://0.0.0.0:%d", start+2)
}

// getConfig returns a config for test cases
func getConfig() *cfg.Config {
	pathname := makePathname()
	c := cfg.ResetTestRoot(pathname)

	// and we use random ports to run in parallel
	tm, rpc, grpc := makeAddrs()
	c.P2P.ListenAddress = tm
	c.RPC.ListenAddress = rpc
	c.RPC.GRPCListenAddress = grpc
	return c
}

// byteBufferWAL is a WAL which writes all msgs to a byte buffer. Writing stops
// when the heightToStop is reached. Client will be notified via
// signalWhenStopsTo channel.
type byteBufferWAL struct {
	enc               *WALEncoder
	stopped           bool
	heightToStop      int64
	signalWhenStopsTo chan struct{}
}

// needed for determinism
var fixedTime, _ = time.Parse(time.RFC3339, "2017-01-02T15:04:05Z")

func newByteBufferWAL(enc *WALEncoder, nBlocks int64, signalStop chan struct{}) *byteBufferWAL {
	return &byteBufferWAL{
		enc:               enc,
		heightToStop:      nBlocks,
		signalWhenStopsTo: signalStop,
	}
}

// Save writes message to the internal buffer except when heightToStop is
// reached, in which case it will signal the caller via signalWhenStopsTo and
// skip writing.
func (w *byteBufferWAL) Save(m WALMessage) {
	if w.stopped {
		return
	}

	if endMsg, ok := m.(EndHeightMessage); ok {
		if endMsg.Height == w.heightToStop {
			w.signalWhenStopsTo <- struct{}{}
			w.stopped = true
			return
		}
	}

	err := w.enc.Encode(&TimedWALMessage{fixedTime, m})
	if err != nil {
		panic(fmt.Sprintf("failed to encode the msg %v", m))
	}
}

func (w *byteBufferWAL) Group() *auto.Group {
	panic("not implemented")
}
func (w *byteBufferWAL) SearchForEndHeight(height int64, options *WALSearchOptions) (gr *auto.GroupReader, found bool, err error) {
	return nil, false, nil
}

func (w *byteBufferWAL) Start() error { return nil }
func (w *byteBufferWAL) Stop() error  { return nil }
func (w *byteBufferWAL) Wait()        {}
<|endoftext|>


<commit_before>package main

import (
	"github.com/cadena-monde/slackbot"
	"os"
	"flag"
)

var (
	channel, message string
	slackURL        = os.Getenv("SLACKBOT_URL")
	token           = os.Getenv("SLACKBOT_TOKEN")
)

func main() {
	parseFlags()
	postMessage();
}

func parseFlags() {
	flag.StringVar(&channel, "channel", "", "Channel. Ex: #random")
	flag.StringVar(&message, "message", "", "Message to be sent to the channel")
	flag.Parse()

	if channel == "" || message == "" {
		flag.Usage()
		os.Exit(1)
	}
}

func postMessage() {
	b := slackbot.New(slackURL, token)
	b.PostMessage(channel, message)
}
<commit_msg>Adds -stdin option to read messages from the stdin<commit_after>package main

import (
	"bytes"
	"flag"
	"fmt"
	"github.com/cadena-monde/slackbot"
	"os"
	"strings"
)

var (
	channel, message string
	stdin            bool
	slackURL         = os.Getenv("SLACKBOT_URL")
	token            = os.Getenv("SLACKBOT_TOKEN")
)

func main() {
	parseFlags()
	if stdin {
		readFromStdin()
	}
	postMessage()
}

func readFromStdin() {
	input := bytes.NewBuffer([]byte{})
	input.ReadFrom(os.Stdin)

	inputString := input.String()
	if inputString != "" {
		message = fmt.Sprintf("%s\n```%s```", message, input.String())
		message = strings.Replace(message, "\r", "", -1)
	}
}

func parseFlags() {
	flag.StringVar(&channel, "channel", "", "Channel. Ex: #random")
	flag.StringVar(&message, "message", "", "Message to be sent to the channel")
	flag.BoolVar(&stdin, "stdin", false, "Read message from stdin")
	flag.Parse()

	if channel == "" || (message == "" && !stdin) {
		flag.Usage()
		os.Exit(1)
	}
}

func postMessage() {
	b := slackbot.New(slackURL, token)
	b.PostMessage(channel, message)
}
<|endoftext|>


<commit_before>package main

import (
	"bytes"
	"testing"

	"github.com/Sirupsen/logrus"
)

var configTestInstance *machine

func TestInstanceName(t *testing.T) {
	n := NewNova()
	n.Init(false)

	machines, _ := n.List()
	if len(machines) == 0 {
		t.Skipf("No machines found. Skip this test.")
	}

	configTestInstance = machines[0]
}

// ---------------------------------------

// Only instance name in the arguments.
func TestParseArgs1(t *testing.T) {
	if configTestInstance == nil {
		t.Skipf("No servers found. Skip this test.")
	}

	args := []string{
		configTestInstance.Name,
	}

	c := Config{Args: args}
	cmd, err := c.ParseArgs()
	if cmd != CMD_CONNECT {
		t.Errorf("Command should be CMD_CONNECT: command=%d", cmd)
	} else if err != nil {
		t.Errorf("%v", err)
	}

	if c.ConnType != CON_SSH {
		t.Errorf("ConnType should be CON_SSH: type=%d", c.ConnType)
	} else if err != nil {
		t.Errorf("%v", err)
	}

	if c.SshHost != configTestInstance.Ipaddr {
		t.Errorf("hostname is not match: %v", c)
	}
	if c.SshUser != "" {
		t.Errorf("username is not match: %v", c)
	}
	if c.SshRemoteCommand != "" {
		t.Errorf("remote-command is not match: %v", c)
	}
}

// Instance name with user in the arguments.
func TestParseArgs2(t *testing.T) {
	if configTestInstance == nil {
		t.Skipf("No servers found. Skip this test.")
	}

	args := []string{
		"root@" + configTestInstance.Name,
	}

	c := Config{Args: args}
	cmd, err := c.ParseArgs()
	if cmd != CMD_CONNECT {
		t.Errorf("Command should be CMD_CONNECT: command=%d", cmd)
	} else if err != nil {
		t.Errorf("%v", err)
	}

	if c.ConnType != CON_SSH {
		t.Errorf("ConnType should be CON_SSH: type=%d", c.ConnType)
	} else if err != nil {
		t.Errorf("%v", err)
	}

	if c.SshHost != configTestInstance.Ipaddr {
		t.Errorf("hostname is not match: %v", c)
	}
	if c.SshUser != "root" {
		t.Errorf("username is not match: %v", c)
	}
	if c.SshRemoteCommand != "" {
		t.Errorf("remote-command is not match: %v", c)
	}
}

// Instance name with user and remote commands in the arguments
func TestParseArgs3(t *testing.T) {
	if configTestInstance == nil {
		t.Skipf("No servers found. Skip this test.")
	}

	args := []string{
		"root@" + configTestInstance.Name,
		"test-command",
	}

	c := Config{Args: args}
	cmd, err := c.ParseArgs()
	if cmd != CMD_CONNECT {
		t.Errorf("Command should be CMD_CONNECT: command=%d", cmd)
	} else if err != nil {
		t.Errorf("%v", err)
	}

	if c.ConnType != CON_SSH {
		t.Errorf("ConnType should be CON_SSH: type=%d", c.ConnType)
	} else if err != nil {
		t.Errorf("%v", err)
	}

	if c.SshHost != configTestInstance.Ipaddr {
		t.Errorf("hostname is not match: %v", c)
	}
	if c.SshUser != "root" {
		t.Errorf("username is not match: %v", c)
	}
	if c.SshRemoteCommand != "test-command" {
		t.Errorf("remote-command is not match: %v", c)
	}
}

// With SSH options
func TestParseArgs4(t *testing.T) {
	if configTestInstance == nil {
		t.Skipf("No servers found. Skip this test.")
	}

	args := []string{
		// Port fowarding option for ssh
		"-L",
		"54321:localhost:54321",
		configTestInstance.Name,
	}

	c := Config{Args: args}
	cmd, err := c.ParseArgs()
	if cmd != CMD_CONNECT {
		t.Errorf("Command should be CMD_CONNECT: command=%d", cmd)
	} else if err != nil {
		t.Errorf("%v", err)
	}

	if c.ConnType != CON_SSH {
		t.Errorf("ConnType should be CON_SSH: type=%d", c.ConnType)
	} else if err != nil {
		t.Errorf("%v", err)
	}

	if c.SshOptions[0] != "-L" || c.SshOptions[1] != "54321:localhost:54321" {
		t.Errorf("ssh options are not match: %v", c)
	}
	if c.SshHost != configTestInstance.Ipaddr {
		t.Errorf("hostname is not match: %v", c)
	}
	if c.SshUser != "" {
		t.Errorf("username is not match: %v", c)
	}
	if c.SshRemoteCommand != "" {
		t.Errorf("remote-command is not match: %v", c)
	}
}

func TestHelp(t *testing.T) {
	args := []string{
		"--help",
	}

	c := Config{Args: args}
	cmd, err := c.ParseArgs()
	if cmd != CMD_HELP {
		t.Errorf("Command should be CMD_CONNECT: command=%d", cmd)
	} else if err != nil {
		t.Errorf("%v", err)
	}
}

func TestList(t *testing.T) {
	args := []string{
		"--list",
	}

	c := Config{
		Stdout: new(bytes.Buffer),
		Stdin:  nil,
		Stderr: nil,
		Args:   args,
	}
	cmd, err := c.ParseArgs()
	if cmd != CMD_LIST {
		t.Errorf("Command should be CMD_CONNECT: command=%d", cmd)
	} else if err != nil {
		t.Errorf("%v", err)
	}
}

func TestDeauth(t *testing.T) {
	args := []string{
		"--deauth",
	}

	c := Config{Args: args}
	cmd, err := c.ParseArgs()
	if cmd != CMD_DEAUTH {
		t.Errorf("Command should be CMD_CONNECT: command=%d", cmd)
	} else if err != nil {
		t.Errorf("%v", err)
	}
}

func TestDebug(t *testing.T) {
	args := []string{
		"--debug",
	}

	c := Config{
		Stdout: new(bytes.Buffer),
		Stdin:  nil,
		Stderr: nil,
		Args:   args,
	}
	_, err := c.ParseArgs()
	if err != nil {
		t.Errorf("%v", err)
	}

	// disable debug
	disableDebugTransport()
	logrus.SetLevel(logrus.InfoLevel)
}

func TestConsole(t *testing.T) {
	if configTestInstance == nil {
		t.Skipf("No servers found. Skip this test.")
	}

	args := []string{
		"--console",
		configTestInstance.Name,
	}

	c := Config{Args: args}
	_, err := c.ParseArgs()
	if c.ConnType != CON_CONSOLE {
		t.Errorf("ConnType should be CON_CONSOLE: type=%d", c.ConnType)
	} else if err != nil {
		t.Errorf("%v", err)
	}
}
<commit_msg>Serial Consoleをサポートしていない環境でエラーになっていたのを修正<commit_after>package main

import (
	"bytes"
	"testing"

	"strings"

	"github.com/Sirupsen/logrus"
)

var configTestInstance *machine

func TestInstanceName(t *testing.T) {
	n := NewNova()
	n.Init(false)

	machines, _ := n.List()
	if len(machines) == 0 {
		t.Skipf("No machines found. Skip this test.")
	}

	configTestInstance = machines[0]
}

// ---------------------------------------

// Only instance name in the arguments.
func TestParseArgs1(t *testing.T) {
	if configTestInstance == nil {
		t.Skipf("No servers found. Skip this test.")
	}

	args := []string{
		configTestInstance.Name,
	}

	c := Config{Args: args}
	cmd, err := c.ParseArgs()
	if cmd != CMD_CONNECT {
		t.Errorf("Command should be CMD_CONNECT: command=%d", cmd)
	} else if err != nil {
		t.Errorf("%v", err)
	}

	if c.ConnType != CON_SSH {
		t.Errorf("ConnType should be CON_SSH: type=%d", c.ConnType)
	} else if err != nil {
		t.Errorf("%v", err)
	}

	if c.SshHost != configTestInstance.Ipaddr {
		t.Errorf("hostname is not match: %v", c)
	}
	if c.SshUser != "" {
		t.Errorf("username is not match: %v", c)
	}
	if c.SshRemoteCommand != "" {
		t.Errorf("remote-command is not match: %v", c)
	}
}

// Instance name with user in the arguments.
func TestParseArgs2(t *testing.T) {
	if configTestInstance == nil {
		t.Skipf("No servers found. Skip this test.")
	}

	args := []string{
		"root@" + configTestInstance.Name,
	}

	c := Config{Args: args}
	cmd, err := c.ParseArgs()
	if cmd != CMD_CONNECT {
		t.Errorf("Command should be CMD_CONNECT: command=%d", cmd)
	} else if err != nil {
		t.Errorf("%v", err)
	}

	if c.ConnType != CON_SSH {
		t.Errorf("ConnType should be CON_SSH: type=%d", c.ConnType)
	} else if err != nil {
		t.Errorf("%v", err)
	}

	if c.SshHost != configTestInstance.Ipaddr {
		t.Errorf("hostname is not match: %v", c)
	}
	if c.SshUser != "root" {
		t.Errorf("username is not match: %v", c)
	}
	if c.SshRemoteCommand != "" {
		t.Errorf("remote-command is not match: %v", c)
	}
}

// Instance name with user and remote commands in the arguments
func TestParseArgs3(t *testing.T) {
	if configTestInstance == nil {
		t.Skipf("No servers found. Skip this test.")
	}

	args := []string{
		"root@" + configTestInstance.Name,
		"test-command",
	}

	c := Config{Args: args}
	cmd, err := c.ParseArgs()
	if cmd != CMD_CONNECT {
		t.Errorf("Command should be CMD_CONNECT: command=%d", cmd)
	} else if err != nil {
		t.Errorf("%v", err)
	}

	if c.ConnType != CON_SSH {
		t.Errorf("ConnType should be CON_SSH: type=%d", c.ConnType)
	} else if err != nil {
		t.Errorf("%v", err)
	}

	if c.SshHost != configTestInstance.Ipaddr {
		t.Errorf("hostname is not match: %v", c)
	}
	if c.SshUser != "root" {
		t.Errorf("username is not match: %v", c)
	}
	if c.SshRemoteCommand != "test-command" {
		t.Errorf("remote-command is not match: %v", c)
	}
}

// With SSH options
func TestParseArgs4(t *testing.T) {
	if configTestInstance == nil {
		t.Skipf("No servers found. Skip this test.")
	}

	args := []string{
		// Port fowarding option for ssh
		"-L",
		"54321:localhost:54321",
		configTestInstance.Name,
	}

	c := Config{Args: args}
	cmd, err := c.ParseArgs()
	if cmd != CMD_CONNECT {
		t.Errorf("Command should be CMD_CONNECT: command=%d", cmd)
	} else if err != nil {
		t.Errorf("%v", err)
	}

	if c.ConnType != CON_SSH {
		t.Errorf("ConnType should be CON_SSH: type=%d", c.ConnType)
	} else if err != nil {
		t.Errorf("%v", err)
	}

	if c.SshOptions[0] != "-L" || c.SshOptions[1] != "54321:localhost:54321" {
		t.Errorf("ssh options are not match: %v", c)
	}
	if c.SshHost != configTestInstance.Ipaddr {
		t.Errorf("hostname is not match: %v", c)
	}
	if c.SshUser != "" {
		t.Errorf("username is not match: %v", c)
	}
	if c.SshRemoteCommand != "" {
		t.Errorf("remote-command is not match: %v", c)
	}
}

func TestHelp(t *testing.T) {
	args := []string{
		"--help",
	}

	c := Config{Args: args}
	cmd, err := c.ParseArgs()
	if cmd != CMD_HELP {
		t.Errorf("Command should be CMD_CONNECT: command=%d", cmd)
	} else if err != nil {
		t.Errorf("%v", err)
	}
}

func TestList(t *testing.T) {
	args := []string{
		"--list",
	}

	c := Config{
		Stdout: new(bytes.Buffer),
		Stdin:  nil,
		Stderr: nil,
		Args:   args,
	}
	cmd, err := c.ParseArgs()
	if cmd != CMD_LIST {
		t.Errorf("Command should be CMD_CONNECT: command=%d", cmd)
	} else if err != nil {
		t.Errorf("%v", err)
	}
}

func TestDeauth(t *testing.T) {
	args := []string{
		"--deauth",
	}

	c := Config{Args: args}
	cmd, err := c.ParseArgs()
	if cmd != CMD_DEAUTH {
		t.Errorf("Command should be CMD_CONNECT: command=%d", cmd)
	} else if err != nil {
		t.Errorf("%v", err)
	}
}

func TestDebug(t *testing.T) {
	args := []string{
		"--debug",
	}

	c := Config{
		Stdout: new(bytes.Buffer),
		Stdin:  nil,
		Stderr: nil,
		Args:   args,
	}
	_, err := c.ParseArgs()
	if err != nil {
		t.Errorf("%v", err)
	}

	// disable debug
	disableDebugTransport()
	logrus.SetLevel(logrus.InfoLevel)
}

func TestConsole(t *testing.T) {
	if configTestInstance == nil {
		t.Skipf("No servers found. Skip this test.")
	}

	args := []string{
		"--console",
		configTestInstance.Name,
	}

	c := Config{Args: args}
	_, err := c.ParseArgs()
	if c.ConnType != CON_CONSOLE {
		t.Errorf("ConnType should be CON_CONSOLE: type=%d", c.ConnType)
	} else if err != nil {
		msg := err.Error()
		if strings.Index(msg, "Unavailable console type serial") >= 0 {
			t.Skip("Sserial console is not supported.")
		}
		t.Errorf("%v", err)
	}
}
<|endoftext|>


<commit_before>package main

import (
	"os"
	"path/filepath"
	"testing"

	. "github.com/smartystreets/goconvey/convey"
)

func TestConfigFolder(t *testing.T) {
	// cleanup any artificats from old runs
	os.RemoveAll(filepath.Join(ConfigFolder, "_test"))

	Convey("It should create folder at home", t, func() {
		folder, err := createFolderAtHome("_test/koding")
		So(err, ShouldBeNil)

		_, err = os.Stat(folder)
		So(err, ShouldBeNil)

		Convey("It should do nothing if folder already exists", func() {
			folder, err := createFolderAtHome("_test/koding")
			So(err, ShouldBeNil)

			_, err = os.Stat(folder)
			So(err, ShouldBeNil)

			defer func() {
				err := os.RemoveAll(folder)
				So(err, ShouldBeNil)
			}()
		})
	})
}
<commit_msg>remove config folder added in tests properly<commit_after>package main

import (
	"os"
	"path/filepath"
	"testing"

	. "github.com/smartystreets/goconvey/convey"
)

func TestConfigFolder(t *testing.T) {
	// cleanup any artificats from old runs
	os.RemoveAll(filepath.Join(ConfigFolder, "_test"))

	Convey("It should create folder at home", t, func() {
		folder, err := createFolderAtHome("_test/koding")
		So(err, ShouldBeNil)

		_, err = os.Stat(folder)
		So(err, ShouldBeNil)

		Convey("It should do nothing if folder already exists", func() {
			folder, err := createFolderAtHome("_test/koding")
			So(err, ShouldBeNil)

			_, err = os.Stat(folder)
			So(err, ShouldBeNil)

			defer func() {
				err := os.RemoveAll(filepath.Join(ConfigFolder, "_test"))
				So(err, ShouldBeNil)
			}()
		})
	})
}
<|endoftext|>


<commit_before>package main

import (
	"path/filepath"
	"reflect"
	"testing"
)

// This is the directory where our test fixtures are.
const fixtureDir = "./test-fixtures"

func TestLoadConfig(t *testing.T) {
	c, err := LoadConfig(filepath.Join(fixtureDir, "config"))
	if err != nil {
		t.Fatalf("err: %s", err)
	}

	expected := &Config{
		Providers: map[string]string{
			"aws": "foo",
			"do":  "bar",
		},
	}

	if !reflect.DeepEqual(c, expected) {
		t.Fatalf("bad: %#v", c)
	}
}

func TestConfig_Merge(t *testing.T) {
	c1 := &Config{
		Providers: map[string]string{
			"foo": "bar",
			"bar": "blah",
		},
	}

	c2 := &Config{
		Providers: map[string]string{
			"bar": "baz",
			"baz": "what",
		},
	}

	expected := &Config{
		Providers: map[string]string{
			"foo": "bar",
			"bar": "baz",
			"baz": "what",
		},
	}

	actual := c1.Merge(c2)
	if !reflect.DeepEqual(actual, expected) {
		t.Fatalf("bad: %#v", actual)
	}
}
<commit_msg>Update config test to handle provisioners<commit_after>package main

import (
	"path/filepath"
	"reflect"
	"testing"
)

// This is the directory where our test fixtures are.
const fixtureDir = "./test-fixtures"

func TestLoadConfig(t *testing.T) {
	c, err := LoadConfig(filepath.Join(fixtureDir, "config"))
	if err != nil {
		t.Fatalf("err: %s", err)
	}

	expected := &Config{
		Providers: map[string]string{
			"aws": "foo",
			"do":  "bar",
		},
	}

	if !reflect.DeepEqual(c, expected) {
		t.Fatalf("bad: %#v", c)
	}
}

func TestConfig_Merge(t *testing.T) {
	c1 := &Config{
		Providers: map[string]string{
			"foo": "bar",
			"bar": "blah",
		},
		Provisioners: map[string]string{
			"local":  "local",
			"remote": "bad",
		},
	}

	c2 := &Config{
		Providers: map[string]string{
			"bar": "baz",
			"baz": "what",
		},
		Provisioners: map[string]string{
			"remote": "remote",
		},
	}

	expected := &Config{
		Providers: map[string]string{
			"foo": "bar",
			"bar": "baz",
			"baz": "what",
		},
		Provisioners: map[string]string{
			"local":  "local",
			"remote": "remote",
		},
	}

	actual := c1.Merge(c2)
	if !reflect.DeepEqual(actual, expected) {
		t.Fatalf("bad: %#v", actual)
	}
}
<|endoftext|>


<commit_before>package main // import "github.com/crosbymichael/dockerui"

import (
	"flag"
	"io"
	"log"
	"net"
	"net/http"
	"net/http/httputil"
	"net/url"
	"os"
	"strings"
	"github.com/gorilla/csrf"
	"github.com/gorilla/securecookie"
)

var (
	endpoint = flag.String("e", "/var/run/docker.sock", "Dockerd endpoint")
	addr     = flag.String("p", ":9000", "Address and port to serve dockerui")
	assets   = flag.String("a", ".", "Path to the assets")
	CSRF = csrf.Protect(
		[]byte(securecookie.GenerateRandomKey(32)),
		csrf.HttpOnly(false),
		csrf.Secure(false),
	)
)

type UnixHandler struct {
	path string
}

func (h *UnixHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	conn, err := net.Dial("unix", h.path)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		log.Println(err)
		return
	}
	c := httputil.NewClientConn(conn, nil)
	defer c.Close()

	res, err := c.Do(r)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		log.Println(err)
		return
	}
	defer res.Body.Close()

	copyHeader(w.Header(), res.Header)
	if _, err := io.Copy(w, res.Body); err != nil {
		log.Println(err)
	}
}

func copyHeader(dst, src http.Header) {
	for k, vv := range src {
		for _, v := range vv {
			dst.Add(k, v)
		}
	}
}

func createTcpHandler(e string) http.Handler {
	u, err := url.Parse(e)
	if err != nil {
		log.Fatal(err)
	}
	return httputil.NewSingleHostReverseProxy(u)
}

func createUnixHandler(e string) http.Handler {
	return &UnixHandler{e}
}

func createHandler(dir string, e string) http.Handler {
	var (
		mux         = http.NewServeMux()
		fileHandler = http.FileServer(http.Dir(dir))
		h           http.Handler
	)

	if strings.Contains(e, "http") {
		h = createTcpHandler(e)
	} else {
		if _, err := os.Stat(e); err != nil {
			if os.IsNotExist(err) {
				log.Fatalf("unix socket %s does not exist", e)
			}
			log.Fatal(err)
		}
		h = createUnixHandler(e)
	}

	mux.Handle("/dockerapi/", http.StripPrefix("/dockerapi", h))
	mux.Handle("/", fileHandler)
	return logWrapper(CSRF(mux))
}

func logWrapper(h http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		log.Println("Request starting: " + r.URL.Path)
		c, err := r.Cookie ("_gorilla_csrf")
		if err != nil {
			log.Println("Unable to find session cookie _gorilla_csrf")
			h.ServeHTTP(w, r)
		} else {
			log.Println("Cookie:" + c.Value)
			log.Println("Header:" + r.Header.Get("X-CSRF-Token"))
			h.ServeHTTP(w, r)
			log.Println("Request ending")
		}
	})
}

func main() {
	flag.Parse()

	handler := createHandler(*assets, *endpoint)
	if err := http.ListenAndServe(*addr, handler); err != nil {
		log.Fatal(err)
	}
}
<commit_msg>Don't re-generate token on every startup for now.<commit_after>package main // import "github.com/crosbymichael/dockerui"

import (
	"flag"
	"io"
	"log"
	"net"
	"net/http"
	"net/http/httputil"
	"net/url"
	"os"
	"strings"
	"github.com/gorilla/csrf"
)

var (
	endpoint = flag.String("e", "/var/run/docker.sock", "Dockerd endpoint")
	addr     = flag.String("p", ":9000", "Address and port to serve dockerui")
	assets   = flag.String("a", ".", "Path to the assets")
	CSRF = csrf.Protect(
		[]byte("32-byte-long-auth-key"), // FIXME: generate once, reuse on restarts
		csrf.HttpOnly(false),
		csrf.Secure(false),
	)
)

type UnixHandler struct {
	path string
}

func (h *UnixHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	conn, err := net.Dial("unix", h.path)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		log.Println(err)
		return
	}
	c := httputil.NewClientConn(conn, nil)
	defer c.Close()

	res, err := c.Do(r)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		log.Println(err)
		return
	}
	defer res.Body.Close()

	copyHeader(w.Header(), res.Header)
	if _, err := io.Copy(w, res.Body); err != nil {
		log.Println(err)
	}
}

func copyHeader(dst, src http.Header) {
	for k, vv := range src {
		for _, v := range vv {
			dst.Add(k, v)
		}
	}
}

func createTcpHandler(e string) http.Handler {
	u, err := url.Parse(e)
	if err != nil {
		log.Fatal(err)
	}
	return httputil.NewSingleHostReverseProxy(u)
}

func createUnixHandler(e string) http.Handler {
	return &UnixHandler{e}
}

func createHandler(dir string, e string) http.Handler {
	var (
		mux         = http.NewServeMux()
		fileHandler = http.FileServer(http.Dir(dir))
		h           http.Handler
	)

	if strings.Contains(e, "http") {
		h = createTcpHandler(e)
	} else {
		if _, err := os.Stat(e); err != nil {
			if os.IsNotExist(err) {
				log.Fatalf("unix socket %s does not exist", e)
			}
			log.Fatal(err)
		}
		h = createUnixHandler(e)
	}

	mux.Handle("/dockerapi/", http.StripPrefix("/dockerapi", h))
	mux.Handle("/", fileHandler)
	return logWrapper(CSRF(mux))
}

func logWrapper(h http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		log.Println("Request starting: " + r.URL.Path)
		c, err := r.Cookie ("_gorilla_csrf")
		if err != nil {
			log.Println("Unable to find session cookie _gorilla_csrf")
			h.ServeHTTP(w, r)
		} else {
			log.Println("Cookie:" + c.Value)
			log.Println("Header:" + r.Header.Get("X-CSRF-Token"))
			h.ServeHTTP(w, r)
			log.Println("Request ending")
		}
	})
}

func main() {
	flag.Parse()

	handler := createHandler(*assets, *endpoint)
	if err := http.ListenAndServe(*addr, handler); err != nil {
		log.Fatal(err)
	}
}
<|endoftext|>


<commit_before>package commands

import (
	"flag"
	"fmt"
	"os"
	"strings"

	"github.com/jaxi/koderunr/cli/client"
)

// Run is the command struct for running code
type Run struct {
}

// Help command of the run
func (r Run) Help() string {
	helpText := `
Usage: kode run [options] [filename]

  Auto detect the programming language of the file and run it remoted.
  The result will be displayed onto the terminal asynchronously.

Options:

  -v=<version> Version of the programming language you want to use

  -endpoint=<url> The endpoint that you want the code to be run on

Examples:

  $ kode run main.go
  $ kode run -v=2.3.0 foo.rb
`
	return strings.TrimSpace(helpText)
}

// ShortDescription for the Run command
func (r Run) ShortDescription() string {
	return "Run the code remotely on runner and returns the result asynchronously"
}

// Exec is the command that will execute the Run command
func (r Run) Exec(args []string) int {
	var endpoint string
	var langVersion string

	flag.StringVar(&endpoint, "endpoint", "http://127.0.0.1:8080/", "Endpoint of the API")
	flag.StringVar(&langVersion, "version", "", "Version of the language")
	flag.Parse()

	runner, err := client.NewRunner(langVersion, args[0])
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error: %v\n", err)
		return 1
	}

	err = runner.FetchUUID(endpoint)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error: Failed to fetch UUID - %v\n", err)
		return 1
	}

	err = runner.Run(endpoint)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error: Failed to execute the code - %v\n", err)
		return 1
	}

	return 0
}
<commit_msg>Parse the params like an adult.<commit_after>package commands

import (
	"flag"
	"fmt"
	"os"
	"strings"

	"github.com/jaxi/koderunr/cli/client"
)

// Run is the command struct for running code
type Run struct {
}

// Help command of the run
func (r Run) Help() string {
	helpText := `
Usage: kode run [filename] [options]

  Auto detect the programming language of the file and run it remoted.
  The result will be displayed onto the terminal asynchronously.

filename:

	The file that contains the source code you want to run

options:

  -version=<version> Version of the programming language you want to use

  -endpoint=<url> The endpoint that you want the code to be run on

Examples:

  $ kode run main.go
  $ kode run -version=2.3.0 foo.rb
`
	return strings.TrimSpace(helpText)
}

// ShortDescription for the Run command
func (r Run) ShortDescription() string {
	return "Run the code remotely on runner and returns the result asynchronously"
}

// Exec is the command that will execute the Run command
func (r Run) Exec(args []string) int {
	// Parse the version and endpoint from the arguments passed in
	flagargs := args[1:]

	fmt.Println(flagargs)

	runFlagSet := flag.NewFlagSet("run", flag.ExitOnError)
	endpoint := runFlagSet.String("endpoint", "http://127.0.0.1:8080/", "Endpoint of the API")
	langVersion := runFlagSet.String("version", "", "Version of the language")

	runFlagSet.Parse(flagargs)

	// Started running the code
	runner, err := client.NewRunner(*langVersion, args[0])
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error: %v\n", err)
		return 1
	}

	err = runner.FetchUUID(*endpoint)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error: Failed to fetch UUID - %v\n", err)
		return 1
	}

	err = runner.Run(*endpoint)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error: Failed to execute the code - %v\n", err)
		return 1
	}

	return 0
}
<|endoftext|>