package main

import (
	"flag"
	"log"
	"net/http"

	"github.com/jvrplmlmn/nginx-requests-stats/handlers"
	"github.com/satyrius/gonx"
)

const version = "0.1.0"

var format string
var logFile string

func init() {
	flag.StringVar(&format, "format", `$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"`, "Log format")
	flag.StringVar(&logFile, "log", "/var/log/nginx/access.log", "Log file name to read.")
}

func main() {
	// Parse the command-line flags
	flag.Parse()

	// Always log when the application starts
	log.Println("Starting 'nginx-requests-stats' app...")

	// Create a parser based on a given format
	parser := gonx.NewParser(format)

	// This endpoint returns a JSON with the version of the application
	http.Handle("/version", handlers.VersionHandler(version))
	// This endpoint returns a JSON with the number of requests in the last 24h
	http.Handle("/count", handlers.CountHandler(parser, logFile))
	// Serve the endpoints
	log.Fatal(http.ListenAndServe("localhost:8080", nil))

}

package main

import (
	"flag"
	"log"
	"net/http"

	"github.com/jvrplmlmn/nginx-requests-stats/handlers"
	"github.com/satyrius/gonx"
)

const version = "0.1.0"

var format string
var logFile string

func init() {
	flag.StringVar(&format, "format", `$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" $upstream_addr $upstream_cache_status`, "Log format")
	flag.StringVar(&logFile, "log", "/var/log/nginx/access.log", "Log file name to read.")
}

func main() {
	// Parse the command-line flags
	flag.Parse()

	// Always log when the application starts
	log.Println("Starting 'nginx-requests-stats' app...")

	// Create a parser based on a given format
	parser := gonx.NewParser(format)

	// This endpoint returns a JSON with the version of the application
	http.Handle("/version", handlers.VersionHandler(version))
	// This endpoint returns a JSON with the number of requests in the last 24h
	http.Handle("/count", handlers.CountHandler(parser, logFile))
	// Serve the endpoints
	log.Fatal(http.ListenAndServe("localhost:8080", nil))

}

Update log format to parse logs with upstream and cache fields by default


package main

import (
	"fmt"
	"net/http"

	"github.com/aseure/jagger/query"
)

func handler(w http.ResponseWriter, r *http.Request) {
	query := query.NewQuery(r.Body)

	if query == nil {
		w.WriteHeader(400)
	} else {
		query.Execute()
		w.WriteHeader(200)
	}
}

func main() {
	fmt.Println("Listening on :8080...")
	http.HandleFunc("/", handler)
	http.ListenAndServe(":8080", nil)
}

package main

import (
	"fmt"
	"net/http"

	"github.com/aseure/jagger/query"
)

func handler(w http.ResponseWriter, r *http.Request) {
	query := query.NewQuery(r.Body)

	if query == nil {
		w.WriteHeader(400)
	} else {
		go query.Execute()
		w.WriteHeader(200)
	}
}

func main() {
	fmt.Println("Listening on :8080...")
	http.HandleFunc("/", handler)
	http.ListenAndServe(":8080", nil)
}

Make the query execution run on a separate goroutine


package main

import (
	"fmt"
	"os"

	"github.com/dcbishop/gim/cli"
)

func main() {
	options, err := cli.ParseArgs(os.Args)

	if err != nil {
		fmt.Println(cli.Usage())
		os.Exit(1)
	}

	if options.Help {
		fmt.Println(cli.Usage())
		os.Exit(0)
	}
}

package main

import (
	"fmt"
	"os"

	"github.com/dcbishop/gim/cli"
)

func main() {
	options, err := cli.ParseArgs(os.Args)

	if err != nil {
		fmt.Println("ERROR: Invalid arguments.", err)
		fmt.Println(cli.Usage())
		os.Exit(1)
	}

	if options.Help {
		fmt.Println(cli.Usage())
		os.Exit(0)
	}
}

Print error message on bad arguments.


package main

import (
	"fmt"
	"os"
	"os/exec"
	"strings"
)

func main() {
	extArgs, err := LookupExtCmd(os.Args[1:])

	if err != nil {
		fmt.Println(err)
	}

	extCmd := exec.Command(extArgs[0], extArgs[1:]...)
	extCmd.Stdin = os.Stdin
	extCmd.Stdout = os.Stdout
	extCmd.Stderr = os.Stderr
	extCmd.Run()
}

func LookupExtCmd(args []string) ([]string, error) {
	var err error
	for i := len(args); i > 0; i-- {
		extCmd := strings.Join(args[0:i], "-")
		bin, err := exec.LookPath(extCmd)

		if err == nil {
			extArgs := []string{bin}
			extArgs = append(extArgs, args[i:]...)
			return extArgs, nil
		}
	}
	return nil, err
}

package main

import (
	"fmt"
	"os"
	"os/exec"
	"strings"
)

var version = "0.1.0"
var helpMsg = `NAME:
    ext - An interface for command extensions
USAGE:
    ext commands...
`

func main() {
	if len(os.Args) < 2 {
		fmt.Println(helpMsg)
		os.Exit(1)
	}

	switch os.Args[1] {
	case "-h", "--help":
		fmt.Println(helpMsg)
		os.Exit(0)
	case "-v", "--version":
		fmt.Println(version)
		os.Exit(0)
	}

	extArgs, err := LookupExtCmd(os.Args[1:])

	if err != nil {
		fmt.Println(err)
	}

	extCmd := exec.Command(extArgs[0], extArgs[1:]...)
	extCmd.Stdin = os.Stdin
	extCmd.Stdout = os.Stdout
	extCmd.Stderr = os.Stderr
	extCmd.Run()
}

func LookupExtCmd(args []string) ([]string, error) {
	var err error
	for i := len(args); i > 0; i-- {
		extCmd := strings.Join(args[0:i], "-")
		bin, err := exec.LookPath(extCmd)

		if err == nil {
			extArgs := []string{bin}
			extArgs = append(extArgs, args[i:]...)
			return extArgs, nil
		}
	}
	return nil, err
}

Add -h and -v option


package commands

import (
	"fmt"

	"github.com/concourse/concourse/fly/rc"
)

type PruneWorkerCommand struct {
	Worker string `short:"w"  long:"worker" required:"true" description:"Worker to prune"`
}

func (command *PruneWorkerCommand) Execute(args []string) error {
	workerName := command.Worker

	target, err := rc.LoadTarget(Fly.Target, Fly.Verbose)
	if err != nil {
		return err
	}

	err = target.Validate()
	if err != nil {
		return err
	}

	err = target.Client().PruneWorker(workerName)
	if err != nil {
		return err
	}

	fmt.Printf("pruned '%s'\n", workerName)

	return nil
}

package commands

import (
	"fmt"

	"github.com/concourse/concourse/fly/commands/internal/displayhelpers"
	"github.com/concourse/concourse/fly/rc"
)

type PruneWorkerCommand struct {
	Worker     string `short:"w"  long:"worker" description:"Worker to prune"`
	AllStalled bool   `short:"a" long:"all-stalled" description:"Prune all stalled workers"`
}

func (command *PruneWorkerCommand) Execute(args []string) error {
	if command.Worker == "" && !command.AllStalled {
		displayhelpers.Failf("Either a worker name or --all-stalled are required")
	}

	var workersNames []string

	if command.Worker != "" {
		workersNames = append(workersNames, command.Worker)
	}

	target, err := rc.LoadTarget(Fly.Target, Fly.Verbose)
	if err != nil {
		return err
	}

	err = target.Validate()
	if err != nil {
		return err
	}

	if command.AllStalled {
		workers, err := target.Client().ListWorkers()
		if err != nil {
			return err
		}
		for _, worker := range workers {
			if worker.State == "stalled" {
				workersNames = append(workersNames, worker.Name)
			}
		}
		if workersNames == nil {
			displayhelpers.Failf("No stalled worker found.")
		}
	}

	for _, workerName := range workersNames {
		err = target.Client().PruneWorker(workerName)
		if err != nil {
			return err
		}

		fmt.Printf("pruned '%s'\n", workerName)
	}
	return nil
}

Add option to prune all stalled workers instead of just one at the time.


// +build !darwin

package tftp

import "net"

const udp = "udp"

func localSystem(c *net.UDPConn) string {
	return c.LocalAddr().String()
}

// +build !darwin

package tftp

import "net"
import "fmt"
import "strconv"

const udp = "udp"

var localhost string = determineLocalhost()

func determineLocalhost() string {
	l, err := net.ListenTCP("tcp", nil)
	if err != nil {
		panic(fmt.Sprintf("ListenTCP error: %s", err))
	}
	_, lport, _ := net.SplitHostPort(l.Addr().String())
	defer l.Close()

	lo := make(chan string)

	go func() {
		conn, err := l.Accept()
		if err != nil {
			panic(fmt.Sprintf("Accept error: %s", err))
		}
		defer conn.Close()
	}()

	go func() {
		port, _ := strconv.Atoi(lport)
		fmt.Println("connecting...")
		conn, err := net.DialTCP("tcp6", &net.TCPAddr{}, &net.TCPAddr{Port: port})
		if err == nil {
			conn.Close()
			lo <- "::1"
			return
		} else {
			conn, err = net.DialTCP("tcp4", &net.TCPAddr{}, &net.TCPAddr{Port: port})
			if err == nil {
				conn.Close()
				lo <- "127.0.0.1"
				return
			}
		}

		panic("could not determine address family")
	}()

	return <-lo
}

func localSystem(c *net.UDPConn) string {
	_, port, _ := net.SplitHostPort(c.LocalAddr().String())
	return net.JoinHostPort(localhost, port)
}

Determine correct localhost address for tests.


package mint

import "testing"
import "fmt"
import "os"

type ProxyTestee struct {
	t        *testing.T
	actual   interface{}
	expected interface{}
}

var (
	FailBase = 0
	FailType = 1
)
var Scolds = map[int]string{
	FailBase: "Expected to be `%+v`, but actual `%+v`\n",
	FailType: "Expectec type `%+v`, but actual `%T`\n",
}

func Expect(t *testing.T, actual interface{}) *ProxyTestee {
	return &ProxyTestee{t: t, actual: actual}
}
func (p *ProxyTestee) failed(fail ...int) {
	f := FailBase
	if 0 < len(fail) {
		f = fail[0]
	}
	p.failWith(f)
}
func (p *ProxyTestee) failWith(fail int) {
	fmt.Printf(
		Scolds[fail],
		p.expected,
		p.actual,
	)
	p.t.Fail()
	os.Exit(1)
}

package mint

import "testing"
import "fmt"
import "os"

type ProxyTestee struct {
	t        *testing.T
	actual   interface{}
	expected interface{}
}

var (
	FailBase = 0
	FailType = 1
	Scolds   = map[int]string{
		FailBase: "Expected to be `%+v`, but actual `%+v`\n",
		FailType: "Expectec type `%+v`, but actual `%T`\n",
	}
)

func Expect(t *testing.T, actual interface{}) *ProxyTestee {
	return &ProxyTestee{t: t, actual: actual}
}
func (p *ProxyTestee) failed(fail ...int) {
	f := FailBase
	if 0 < len(fail) {
		f = fail[0]
	}
	p.failWith(f)
}
func (p *ProxyTestee) failWith(fail int) {
	fmt.Printf(
		Scolds[fail],
		p.expected,
		p.actual,
	)
	p.t.Fail()
	os.Exit(1)
}

Fix coding style a bit


package messages

import (
	"github.com/Symantec/tricorder/go/tricorder/types"
)

type Range struct {
	Lower *float64 `json:"lower,omitempty"`
	Upper *float64 `json:"upper,omitempty"`
	Count uint64   `json:"count"`
}

type Distribution struct {
	Min    float64  `json:"min"`
	Max    float64  `json:"max"`
	Avg    float64  `json:"avg"`
	Median float64  `json:"median"`
	Count  uint64   `json:"count"`
	Ranges []*Range `json:"ranges,omitempty"`
}

type Value struct {
	Kind              types.Type    `json:"kind"`
	IntValue          *int64        `json:"intValue,omitempty"`
	UintValue         *uint64       `json:"uintValue,omitempty"`
	FloatValue        *float64      `json:"floatValue,omitempty"`
	StringValue       *string       `json:"stringValue,omitempty"`
	DistributionValue *Distribution `json:"distributionValue,omitempty"`
}

type PathResponse struct {
	Path        string  `json:"path"`
	Description *string `json:"description,omitempty"`
	Unit        *string `json:"unit,omitempty"`
	Value       *Value  `json:"value,omitempty"`
}

type JsonPathResponse struct {
	*PathResponse
	Url string `json:"url"`
}

type ListRequest struct {
	AbsPath string
}

type ListResponse struct {
	Items []*PathResponse
}

package messages

import (
	"github.com/Symantec/tricorder/go/tricorder/types"
)

type Range struct {
	Lower *float64 `json:"lower,omitempty"`
	Upper *float64 `json:"upper,omitempty"`
	Count uint64   `json:"count"`
}

type Distribution struct {
	Min    float64  `json:"min"`
	Max    float64  `json:"max"`
	Avg    float64  `json:"avg"`
	Median float64  `json:"median"`
	Count  uint64   `json:"count"`
	Ranges []*Range `json:"ranges,omitempty"`
}

type Value struct {
	Kind              types.Type    `json:"kind"`
	IntValue          *int64        `json:"intValue,omitempty"`
	UintValue         *uint64       `json:"uintValue,omitempty"`
	FloatValue        *float64      `json:"floatValue,omitempty"`
	StringValue       *string       `json:"stringValue,omitempty"`
	DistributionValue *Distribution `json:"distributionValue,omitempty"`
}

type PathResponse struct {
	Path        string `json:"path"`
	Description string `json:"description"`
	Unit        string `json:"unit"`
	Value       *Value `json:"value"`
}

type JsonPathResponse struct {
	*PathResponse
	Url string `json:"url"`
}

type ListRequest struct {
	AbsPath string
}

type ListResponse struct {
	Items []*PathResponse
}

type JsonListResponse struct {
	Items []*JsonPathResponse
}

Make description and unit be required in PathResponse. Add JsonListResponse type.


package rpcd

import (
	"errors"
	"fmt"
	"github.com/Symantec/Dominator/lib/hash"
	"github.com/Symantec/Dominator/proto/imageserver"
)

func (t *rpcType) AddImage(request imageserver.AddImageRequest,
	reply *imageserver.AddImageResponse) error {
	if imageDataBase.CheckImage(request.ImageName) {
		return errors.New("image already exists")
	}
	if request.Image == nil {
		return errors.New("nil image")
	}
	if request.Image.FileSystem == nil {
		return errors.New("nil file-system")
	}
	// Verify all objects are available.
	hashes := make([]hash.Hash, len(request.Image.FileSystem.RegularInodeTable))
	for index, inode := range request.Image.FileSystem.RegularInodeTable {
		hashes[index] = inode.Hash
	}
	objectsPresent, err := imageDataBase.ObjectServer().CheckObjects(hashes)
	if err != nil {
		return err
	}
	for index, present := range objectsPresent {
		if !present {
			return errors.New(fmt.Sprintf("object: %x is not available",
				hashes[index]))
		}
	}
	// TODO(rgooch): Remove debugging output.
	fmt.Printf("AddImage(%s)\n", request.ImageName)
	return imageDataBase.AddImage(request.Image, request.ImageName)
}

package rpcd

import (
	"errors"
	"fmt"
	"github.com/Symantec/Dominator/lib/hash"
	"github.com/Symantec/Dominator/proto/imageserver"
)

func (t *rpcType) AddImage(request imageserver.AddImageRequest,
	reply *imageserver.AddImageResponse) error {
	if imageDataBase.CheckImage(request.ImageName) {
		return errors.New("image already exists")
	}
	if request.Image == nil {
		return errors.New("nil image")
	}
	if request.Image.FileSystem == nil {
		return errors.New("nil file-system")
	}
	// Verify all objects are available.
	hashes := make([]hash.Hash, 0,
		len(request.Image.FileSystem.RegularInodeTable))
	for _, inode := range request.Image.FileSystem.RegularInodeTable {
		if inode.Size > 0 {
			hashes = append(hashes, inode.Hash)
		}
	}
	objectsPresent, err := imageDataBase.ObjectServer().CheckObjects(hashes)
	if err != nil {
		return err
	}
	for index, present := range objectsPresent {
		if !present {
			return errors.New(fmt.Sprintf("object: %d %x is not available",
				index, hashes[index]))
		}
	}
	// TODO(rgooch): Remove debugging output.
	fmt.Printf("AddImage(%s)\n", request.ImageName)
	return imageDataBase.AddImage(request.Image, request.ImageName)
}

Fix bug in imageserver AddImage() RPC handler.


package goutils

import (
	"bytes"
	"text/template"

	"github.com/hoveychen/go-utils/gomap"
)

var (
	textTmplCache = gomap.New()
)

func Sprintt(textTmpl string, data interface{}) string {
	ret := textTmplCache.GetOrCreate(textTmpl, func() interface{} {
		tpl, err := template.New("test").Parse(textTmpl)
		if err != nil {
			LogError(err)
			return nil
		}
		return tpl
	})

	if ret == nil {
		// Not valid text template.
		return ""
	}

	tmpl := (ret).(*template.Template)
	buf := &bytes.Buffer{}
	err := tmpl.Execute(buf, data)
	if err != nil {
		LogError(err)
		return ""
	}

	return buf.String()
}

package goutils

import (
	"bytes"
	"text/template"

	"github.com/hoveychen/go-utils/gomap"
)

var (
	textTmplCache = gomap.New()
)

type Var map[string]interface{}

func Sprintt(textTmpl string, data interface{}) string {
	ret := textTmplCache.GetOrCreate(textTmpl, func() interface{} {
		tpl, err := template.New("test").Parse(textTmpl)
		if err != nil {
			LogError(err)
			return nil
		}
		return tpl
	})

	if ret == nil {
		// Not valid text template.
		return ""
	}

	tmpl := (ret).(*template.Template)
	buf := &bytes.Buffer{}
	err := tmpl.Execute(buf, data)
	if err != nil {
		LogError(err)
		return ""
	}

	return buf.String()
}

Add predefined type for temporary varibles.


// Copyright 2015 Google Inc. All Rights Reserved.
// This file is available under the Apache license.

package errors

import (
	"fmt"
	"strings"

	"github.com/google/mtail/internal/vm/position"
	"github.com/pkg/errors"
)

type compileError struct {
	pos position.Position
	msg string
}

func (e compileError) Error() string {
	return e.pos.String() + ": " + e.msg
}

// ErrorList contains a list of compile errors.
type ErrorList []*compileError

// Add appends an error at a position to the list of errors.
func (p *ErrorList) Add(pos *position.Position, msg string) {
	*p = append(*p, &compileError{*pos, msg})
}

// Append puts an ErrorList on the end of this ErrorList.
func (p *ErrorList) Append(l ErrorList) {
	*p = append(*p, l...)
}

// ErrorList implements the error interface.
func (p ErrorList) Error() string {
	switch len(p) {
	case 0:
		return "no errors"
	case 1:
		return p[0].Error()
	}
	var r strings.Builder
	for _, e := range p {
		r.WriteString(fmt.Sprintf("%s\n", e))
	}
	return r.String()
}

func Errorf(format string, args ...interface{}) error {
	return errors.Errorf(format, args...)
}

// Copyright 2015 Google Inc. All Rights Reserved.
// This file is available under the Apache license.

package errors

import (
	"fmt"

	"github.com/google/mtail/internal/vm/position"
	"github.com/pkg/errors"
)

type compileError struct {
	pos position.Position
	msg string
}

func (e compileError) Error() string {
	return e.pos.String() + ": " + e.msg
}

// ErrorList contains a list of compile errors.
type ErrorList []*compileError

// Add appends an error at a position to the list of errors.
func (p *ErrorList) Add(pos *position.Position, msg string) {
	*p = append(*p, &compileError{*pos, msg})
}

// Append puts an ErrorList on the end of this ErrorList.
func (p *ErrorList) Append(l ErrorList) {
	*p = append(*p, l...)
}

// ErrorList implements the error interface.
func (p ErrorList) Error() string {
	switch len(p) {
	case 0:
		return "no errors"
	case 1:
		return p[0].Error()
	}
	var r string
	for _, e := range p {
		r = r + fmt.Sprintf("%s\n", e)
	}
	return r[:len(r)-1]
}

func Errorf(format string, args ...interface{}) error {
	return errors.Errorf(format, args...)
}

Revert "Use strings.Builder instead of string concatenation."


package meep_test

import (
	"fmt"

	"."
)

func ExampleTraceableErr() {
	type Woop struct {
		meep.TraceableError
		error
	}
	err := meep.New(&Woop{})
	fmt.Println(err.(*Woop).StackString())

	/// Output:
	// FIXME this is hard to test because of the full local path that pops up :(
}

package meep_test

import (
	"fmt"
	"strings"

	"."
)

func ExampleTraceableErr() {
	type Woop struct {
		meep.TraceableError
		error
	}
	err := meep.New(&Woop{})
	str := err.(*Woop).StackString()

	// The *entire* output probably looks something like this:
	//		·> /your/build/path/meep/meep.go:8: meep.New
	//		·> /your/build/path/meep/traceable_test.go:15: meep_test.ExampleTraceableErr
	//		·> /usr/local/go/src/testing/example.go:98: testing.runExample
	//		·> /usr/local/go/src/testing/example.go:36: testing.RunExamplesa
	//		·> /usr/local/go/src/testing/testing.go:486: testing.(*M).Run
	//		·> _/your/build/path/meep/_test/_testmain.go:64: main.main
	//		·> /usr/local/go/src/runtime/proc.go:63: runtime.main
	//		·> /usr/local/go/src/runtime/asm_amd64.s:2232: runtime.goexit
	// We filter it down rather dramatically so as not to catch any line
	//  numbers from the stdlib we built against, etc.

	str = strings.Split(str, "\n")[1]      // yank the one interesting line
	str = strings.Replace(str, cwd, "", 1) // strip the local build path
	fmt.Println(str)

	// Output:
	//	·> /traceable_test.go:15: meep_test.ExampleTraceableErr
}

Build out example|test of TraceableError.


package main

import (
	"fmt"
	"net"

	"github.com/cmatsuoka/ouidb"
	"github.com/mostlygeek/arp"
)

var db *ouidb.OuiDB

func init() {
	db = ouidb.New("/etc/manuf")
	if db == nil {
		db = ouidb.New("manuf")
	}
}

func getMAC(s string) (string, error) {
	ifaces, err := net.Interfaces()
	checkError(err)
	for _, i := range ifaces {
		if i.Name == s {
			return i.HardwareAddr.String(), nil
		}
	}
	return "", fmt.Errorf("%s: no such interface", s)
}

func getName(addr string) string {
	names, err := net.LookupAddr(addr)
	if err != nil {
		return ""
	}
	return names[0]
}

func getMACFromIP(addr string) string {
	arp.CacheUpdate()
	return arp.Search(addr)
}

func getVendor(mac string) string {
	v, _ := db.Lookup(mac)
	return v

}

package main

import (
	"fmt"
	"net"

	"github.com/cmatsuoka/ouidb"
	"github.com/mostlygeek/arp"
)

var db *ouidb.OuiDB

func init() {
	db = ouidb.New("/etc/manuf")
	if db == nil {
		db = ouidb.New("manuf")
	}
}

func getMAC(s string) (string, error) {
	ifaces, err := net.Interfaces()
	checkError(err)
	for _, i := range ifaces {
		if i.Name == s {
			return i.HardwareAddr.String(), nil
		}
	}
	return "", fmt.Errorf("%s: no such interface", s)
}

func getName(addr string) string {
	names, err := net.LookupAddr(addr)
	if err != nil {
		return ""
	}
	return names[0]
}

func getMACFromIP(addr string) string {
	arp.CacheUpdate()
	mac := arp.Search(addr)
	if mac != "" {
		return mac
	}

	ip := net.ParseIP(addr)
	if ip == nil {
		return mac
	}

	conn, err := net.DialUDP("udp", nil, &net.UDPAddr{ip, 0, ""})
	if err != nil {
		return mac
	}
	conn.Write([]byte{0})
	conn.Close()

	arp.CacheUpdate()
	return arp.Search(addr)
}

func getVendor(mac string) string {
	v, _ := db.Lookup(mac)
	return v

}

Add host to ARP table if MAC not found


package xmmsclient

import (
	"bytes"
	"reflect"
	"testing"
)

func TestSerializeInt(t *testing.T) {
	var expected = []byte{
		0x00, 0x00, 0x00, 0x02,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2a,
	}
	var buffer bytes.Buffer

	var err = SerializeXmmsValue(XmmsInt(42), &buffer)
	if err != nil {
		t.Error(err)
	}

	if !reflect.DeepEqual(expected, buffer.Bytes()) {
		t.Errorf("\n\twant %+v\n\thave %+v", expected, buffer.Bytes())
	}
}

package xmmsclient

import (
	"bytes"
	"encoding/hex"
	"reflect"
	"testing"
)

func checkBuffer(t *testing.T, expected []byte, actual []byte) {
	if !reflect.DeepEqual(expected, actual) {
		t.Fatalf("\nwant:\n%s\nhave:\n%s", hex.Dump(expected), hex.Dump(actual))
	}
}

func TestSerializeInt(t *testing.T) {
	var expected = []byte{
		0x00, 0x00, 0x00, 0x02,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2a,
	}
	var buffer bytes.Buffer

	var err = SerializeXmmsValue(XmmsInt(42), &buffer)
	if err != nil {
		t.Fatal(err)
	}

	checkBuffer(t, expected, buffer.Bytes())
}

Break out buffer-check and use t.Fatal.


package main

import (
    "log"
    "fmt"
    "os"
    "path"

    "golang.org/x/net/context"

    "github.com/docker/libcompose/docker"
    "github.com/docker/libcompose/docker/ctx"
    "github.com/docker/libcompose/project"
    "github.com/docker/libcompose/project/options"
)

func main() {
    pwd, err := os.Getwd()
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    _, dir := path.Split(pwd)

    project, err := docker.NewProject(&ctx.Context{
        Context: project.Context{
            ComposeFiles: []string{"docker-compose.yml"},
            ProjectName:  dir,
        },
    }, nil)

    if err != nil {
        log.Fatal(err)
    }

    err = project.Up(context.Background(), options.Up{})

    if err != nil {
       log.Fatal(err)
    }
}

package main

import (
    "log"
    "fmt"
    "os"
    "path"

    "github.com/docker/libcompose/config"
    "github.com/docker/libcompose/project"
)

func main() {
    pwd, err := os.Getwd()
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    _, dir := path.Split(pwd)

    project := project.NewProject(&project.Context{
            ComposeFiles: []string{"docker-compose.yml"},
            ProjectName:  dir,
    }, nil, &config.ParseOptions{})

    if err := project.Parse(); err != nil {
        log.Fatal(err)
    }

    for name, _ := range project.NetworkConfigs {
        s := fmt.Sprintf("Network: %s", name)
        fmt.Println(s)
    }

}

Switch to compose parsing from Docker context


package medtronic

const (
	status Command = 0xCE
)

// StatusInfo represents the pump's status.
type StatusInfo struct {
	Normal    bool
	Bolusing  bool
	Suspended bool
}

// Status returns the pump's status.
func (pump *Pump) Status() StatusInfo {
	data := pump.Execute(status)
	if pump.Error() != nil {
		return StatusInfo{}
	}
	if len(data) < 4 || data[0] != 3 {
		pump.BadResponse(status, data)
		return StatusInfo{}
	}
	// Observed values for data[1]:
	//   0: rewinding
	//   1: preparing to prime
	//   2: priming
	//   3: normal
	return StatusInfo{
		Normal:    data[1] == 0x03,
		Bolusing:  data[2] == 1,
		Suspended: data[3] == 1,
	}
}

package medtronic

const (
	status Command = 0xCE
)

// StatusInfo represents the pump's status.
type StatusInfo struct {
	Code      byte
	Bolusing  bool
	Suspended bool
}

// Normal returns true if the status code indicates normal pump operation.
// Observed values:
//   0: rewinding
//   1: preparing to prime
//   2: priming
//   3: normal
func (s StatusInfo) Normal() bool {
	return s.Code == 0x03
}

// Status returns the pump's status.
func (pump *Pump) Status() StatusInfo {
	data := pump.Execute(status)
	if pump.Error() != nil {
		return StatusInfo{}
	}
	if len(data) < 4 || data[0] != 3 {
		pump.BadResponse(status, data)
		return StatusInfo{}
	}
	return StatusInfo{
		Code:      data[1],
		Bolusing:  data[2] == 1,
		Suspended: data[3] == 1,
	}
}

Add Code field and Normal method to StatusInfo


package main

import (
	"flag"
	"fmt"
	"os"
)

var (
	// Flags
	helpShort = flag.Bool("h", false, "Show usage text (same as --help).")
	helpLong = flag.Bool("help", false, "Show usage text (same as -h).")
)

func main() {
	flag.Usage = usage
	flag.Parse()
	if *helpShort || *helpLong || flag.NArg() == 0 {
		flag.Usage()
	}
}

func usage() {
	fmt.Fprintf(os.Stderr, "Usage:  %s [options] <foo.proto> ...\n", os.Args[0])
	flag.PrintDefaults()
}

package main

import (
	"flag"
	"fmt"
	"os"

	_ "goprotobuf.googlecode.com/hg/compiler/descriptor"
	_ "goprotobuf.googlecode.com/hg/compiler/plugin"
)

var (
	// Flags
	helpShort = flag.Bool("h", false, "Show usage text (same as --help).")
	helpLong = flag.Bool("help", false, "Show usage text (same as -h).")

	pluginBinary = flag.String("plugin", "protoc-gen-go", "The code generator plugin to use.")
)

func main() {
	flag.Usage = usage
	flag.Parse()
	if *helpShort || *helpLong || flag.NArg() == 0 {
		flag.Usage()
	}
}

func usage() {
	fmt.Fprintf(os.Stderr, "Usage:  %s [options] <foo.proto> ...\n", os.Args[0])
	flag.PrintDefaults()
}

Add --plugin flag. Link to goprotobuf libraries.


package xyebot

// Response to Telegram
type Response struct {
	Chatid int64  `json:"chat_id"`
	Text   string `json:"text"`
	Method string `json:"method"`
}

// Chat Telegram structure
type Chat struct {
	ID int64 `json: "chat_id"`
}

// Message Telegram structure
type Message struct {
	Chat *Chat  `json:"chat"`
	Text string `json:"text"`
}

// Update - outer Telegram structure
type Update struct {
	Message *Message `json:"message"`
}

// DatastoreDelay type for DataStore
type DatastoreDelay struct {
	Delay int
}

// DatastoreBool type for DataStore
type DatastoreBool struct {
	Value bool
}

package xyebot

// Response to Telegram
type Response struct {
	Chatid int64  `json:"chat_id"`
	Text   string `json:"text"`
	Method string `json:"method"`
}

// Chat Telegram structure
type Chat struct {
	ID int64 `json: "id"`
}

// Message Telegram structure
type Message struct {
	Chat *Chat  `json:"chat"`
	Text string `json:"text"`
}

// Update - outer Telegram structure
type Update struct {
	Message *Message `json:"message"`
}

// DatastoreDelay type for DataStore
type DatastoreDelay struct {
	Delay int
}

// DatastoreBool type for DataStore
type DatastoreBool struct {
	Value bool
}

Update `chat_id` to be just `id`


package main

import "encoding/json"

// StrSlice representes a string or an array of strings.
// We need to override the json decoder to accept both options.
type StrSlice struct {
	parts []string
}

// UnmarshalJSON decodes the byte slice whether it's a string or an array of strings.
// This method is needed to implement json.Unmarshaler.
func (e *StrSlice) UnmarshalJSON(b []byte) error {
	if len(b) == 0 {
		return nil
	}

	p := make([]string, 0, 1)
	if err := json.Unmarshal(b, &p); err != nil {
		var s string
		if err := json.Unmarshal(b, &s); err != nil {
			return err
		}
		p = append(p, s)
	}

	e.parts = p
	return nil
}

// Len returns the number of parts of the StrSlice.
func (e *StrSlice) Len() int {
	if e == nil {
		return 0
	}
	return len(e.parts)
}

// Slice gets the parts of the StrSlice as a Slice of string.
func (e *StrSlice) Slice() []string {
	if e == nil {
		return nil
	}
	return e.parts
}

package main

import (
	"github.com/drone/drone-go/drone"
)

type Params struct {
	Commands []string          `json:"commands"`
	Login    string            `json:"user"`
	Port     int               `json:"port"`
	Host     drone.StringSlice `json:"host"`
	Sleep    int               `json:"sleep"`
}

Use the drone-go StringSlice instead of custom implementation


package bytebufferpool

import "sync"

const (
	minBitSize = 8
	steps      = 20

	minSize = 1 << minBitSize
	maxSize = 1 << (minBitSize + steps - 1)
)

type byteBufferPool struct {
	// Pools are segemented into power-of-two sized buffers
	// from minSize bytes to maxSize.
	//
	// This allows reducing fragmentation of ByteBuffer objects.
	pools [steps]sync.Pool
}

func (p *byteBufferPool) Acquire() *ByteBuffer {
	pools := &p.pools
	for i := 0; i < steps; i++ {
		v := pools[i].Get()
		if v != nil {
			return v.(*ByteBuffer)
		}
	}

	return &ByteBuffer{
		B: make([]byte, 0, minSize),
	}
}

func (p *byteBufferPool) Release(b *ByteBuffer) {
	n := cap(b.B)
	if n > maxSize {
		// Just drop oversized buffers.
		return
	}
	b.B = b.B[:0]
	idx := bitsize(n-1) >> minBitSize
	p.pools[idx].Put(b)
}

func bitsize(n int) int {
	s := 0
	for n > 0 {
		n >>= 1
		s++
	}
	return s
}

package bytebufferpool

import "sync"

const (
	minBitSize = 8
	steps      = 20

	minSize = 1 << minBitSize
	maxSize = 1 << (minBitSize + steps - 1)
)

type byteBufferPool struct {
	// Pools are segemented into power-of-two sized buffers
	// from minSize bytes to maxSize.
	//
	// This allows reducing fragmentation of ByteBuffer objects.
	pools [steps]sync.Pool
}

func (p *byteBufferPool) Acquire() *ByteBuffer {
	pools := &p.pools
	for i := 0; i < steps; i++ {
		v := pools[i].Get()
		if v != nil {
			return v.(*ByteBuffer)
		}
	}

	return &ByteBuffer{
		B: make([]byte, 0, minSize),
	}
}

func (p *byteBufferPool) Release(b *ByteBuffer) {
	n := cap(b.B)
	if n > maxSize {
		// Oversized buffer.
		// Drop it.
		return
	}
	if (n >> 2) > len(b.B) {
		// Under-used buffer capacity.
		// Drop it.
		return
	}

	b.B = b.B[:0]
	idx := bitSize(n-1) >> minBitSize
	p.pools[idx].Put(b)
}

func bitSize(n int) int {
	s := 0
	for n > 0 {
		n >>= 1
		s++
	}
	return s
}

Drop buffers with under-used capacity in order to reduce memory waste


package dht

import (
	"testing"

	assert "github.com/stretchr/testify/assert"
)

func TestFindKeysNearestToNotEqual(t *testing.T) {
	s, err := newStore()
	assert.Nil(t, err)

	s.Put(KeyPrefixPeer+"a1", "0.0.0.0", true)
	s.Put(KeyPrefixPeer+"a2", "0.0.0.1", true)
	s.Put(KeyPrefixPeer+"a3", "0.0.0.3", true)
	s.Put(KeyPrefixPeer+"a4", "0.0.0.4", true)
	s.Put(KeyPrefixPeer+"a5", "0.0.0.5", true)

	k1, err := s.FindKeysNearestTo(KeyPrefixPeer, KeyPrefixPeer+"a1", 1)
	assert.Nil(t, err)

	k2, err := s.FindKeysNearestTo(KeyPrefixPeer, KeyPrefixPeer+"a2", 1)
	assert.Nil(t, err)

	assert.NotEqual(t, k1, k2)
}

package dht

import (
	"testing"

	assert "github.com/stretchr/testify/assert"
)

func TestFindKeysNearestTo(t *testing.T) {
	s, err := newStore()
	assert.Nil(t, err)

	s.Put(KeyPrefixPeer+"a1", "0.0.0.0", true)
	s.Put(KeyPrefixPeer+"a2", "0.0.0.1", true)
	s.Put(KeyPrefixPeer+"a3", "0.0.0.3", true)
	s.Put(KeyPrefixPeer+"a4", "0.0.0.4", true)
	s.Put(KeyPrefixPeer+"a5", "0.0.0.5", true)

	k1, err := s.FindKeysNearestTo(KeyPrefixPeer, KeyPrefixPeer+"a1", 1)
	assert.Nil(t, err)

	k2, err := s.FindKeysNearestTo(KeyPrefixPeer, KeyPrefixPeer+"a2", 1)
	assert.Nil(t, err)

	assert.NotEqual(t, k1[0], k2[0])
	assert.Equal(t, trimKey(k1[0], KeyPrefixPeer), "a1")
	assert.Equal(t, trimKey(k2[0], KeyPrefixPeer), "a2")

}

Add more assertions find key near


package integration_test

import (
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	. "github.com/sclevine/agouti/dsl"
	. "github.com/sclevine/agouti/internal/integration"
	"testing"
)

func TestIntegration(t *testing.T) {
	RegisterFailHandler(Fail)
	RunSpecs(t, "Integration Suite")
}

var _ = BeforeSuite(func() {
	StartChrome()
	Server.Start()
})

var _ = AfterSuite(func() {
	Server.Close()
	StopWebdriver()
})

package integration_test

import (
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	. "github.com/sclevine/agouti/dsl"
	. "github.com/sclevine/agouti/internal/integration"
	"testing"
)

func TestIntegration(t *testing.T) {
	RegisterFailHandler(Fail)
	RunSpecs(t, "Integration Suite")
}

var _ = BeforeSuite(func() {
	StartPhantomJS()
	Server.Start()
})

var _ = AfterSuite(func() {
	Server.Close()
	StopWebdriver()
})

Switch integration specs back to PhantomJS


// +build integration
package codedeploy_test

import (
	"strconv"
	"testing"
	"time"

	"github.com/awslabs/aws-sdk-go/aws"
	"github.com/awslabs/aws-sdk-go/service/codedeploy"
)

func TestCreateDeleteApplication(t *testing.T) {

	client := codedeploy.New(aws.DefaultCreds(), "us-east-1", nil)
	applicationName := "awsgosdk-" + strconv.FormatInt(time.Now().UnixNano(), 10)
	createOutput, err := client.CreateApplication(&codedeploy.CreateApplicationInput{
		ApplicationName: &applicationName,
	})
	if err != nil {
		t.Fatal("Failed to create application: ", err)
	}
	if *(createOutput.ApplicationID) == "" {
		t.Fatal("Failed to marshall create response")
	}

	client.DeleteApplication(&codedeploy.DeleteApplicationInput{
		ApplicationName: &applicationName,
	})
	if err != nil {
		t.Fatal("Failed to delete application: ", err)
	}
}

Fix build flags integration test


package alice

import "net/http"

// A constructor for a piece of middleware.
// Most middleware use this constructor out of the box,
// so in most cases you can just pass somepackage.New
type Constructor func(http.Handler) http.Handler

type Chain struct {
	constructors []Constructor
}

// Creates a new chain, memorizing the given middleware constructors
func New(constructors ...Constructor) Chain {
	c := Chain{}
	c.constructors = append(c.constructors, constructors...)

	return c
}

// Chains the middleware and returns the final http.Handler
//     New(m1, m2, m3).Then(h)
// is equivalent to:
//     m1(m2(m3(h)))
// When the request comes in, it will be passed to m1, then m2, then m3
// and finally, the given handler
// (assuming every middleware calls the following one)
func (c Chain) Then(h http.Handler) http.Handler {
	var final http.Handler
	if h != nil {
		final = h
	} else {
		final = http.DefaultServeMux
	}

	for i := len(c.constructors) - 1; i >= 0; i-- {
		final = c.constructors[i](final)
	}

	return final
}

package alice

import "net/http"

// A constructor for a piece of middleware.
// Most middleware use this constructor out of the box,
// so in most cases you can just pass somepackage.New
type Constructor func(http.Handler) http.Handler

type Chain struct {
	constructors []Constructor
}

// Creates a new chain, memorizing the given middleware constructors
func New(constructors ...Constructor) Chain {
	c := Chain{}
	c.constructors = append(c.constructors, constructors...)

	return c
}

// Chains the middleware and returns the final http.Handler
//     New(m1, m2, m3).Then(h)
// is equivalent to:
//     m1(m2(m3(h)))
// When the request comes in, it will be passed to m1, then m2, then m3
// and finally, the given handler
// (assuming every middleware calls the following one)
//
// Then() treats nil as http.DefaultServeMux.
func (c Chain) Then(h http.Handler) http.Handler {
	var final http.Handler
	if h != nil {
		final = h
	} else {
		final = http.DefaultServeMux
	}

	for i := len(c.constructors) - 1; i >= 0; i-- {
		final = c.constructors[i](final)
	}

	return final
}

Document the handling of nil.


package phases

import (
	"github.com/Everlane/evan/common"

	"github.com/nlopes/slack"
)

type SlackNotifierPhase struct {
	Client  *slack.Client
	Channel string
	Format  func(common.Deployment) (string, error)
}

func (snp *SlackNotifierPhase) CanPreload() bool {
	return false
}

func (snp *SlackNotifierPhase) Execute(deployment common.Deployment, _ interface{}) error {
	message, err := snp.Format(deployment)
	if err != nil {
		return err
	}

	// If the `Format` function returned an empty strings that means we
	// shouldn't send a message to Slack.
	if message == "" {
		return nil
	}

	params := slack.NewPostMessageParameters()
	_, _, err = snp.Client.PostMessage(snp.Channel, message, params)
	if err != nil {
		return err
	}

	return nil
}

package phases

import (
	"github.com/Everlane/evan/common"

	"github.com/nlopes/slack"
)

type SlackNotifierPhase struct {
	Client  *slack.Client
	Channel string
	Format  func(common.Deployment) (*string, *slack.PostMessageParameters, error)
}

func (snp *SlackNotifierPhase) CanPreload() bool {
	return false
}

func (snp *SlackNotifierPhase) Execute(deployment common.Deployment, _ interface{}) error {
	message, params, err := snp.Format(deployment)
	if err != nil {
		return err
	}

	// Don't send a message to Slack if the format function didn't return
	// a message to send
	if message == nil {
		return nil
	}

	if params == nil {
		defaultParams := slack.NewPostMessageParameters()
		params = &defaultParams
	}

	_, _, err = snp.Client.PostMessage(snp.Channel, *message, *params)
	if err != nil {
		return err
	}

	return nil
}

Add more options to Slack notifier phase


package main

import (
	twodee "../libs/twodee"
)

const (
	UpLayer twodee.GameEventType = iota
	DownLayer
	UpWaterLevel
	DownWaterLevel
	PlayerMove
	GameIsClosing
	PlayExploreMusic
	PauseMusic
	ResumeMusic
	PlayerPickedUpItem
	sentinel
)

const (
	NumGameEventTypes = int(sentinel)
)

type MoveDirection int

const (
	North MoveDirection = iota
	East
	South
	West
	None
)

type PlayerMoveEvent struct {
	*twodee.BasicGameEvent
	Dir MoveDirection
}

func NewPlayerMoveEvent(direction MoveDirection) (e *PlayerMoveEvent) {
	e = &PlayerMoveEvent{
		twodee.NewBasicGameEvent(PlayerMove),
		direction,
	}
	return
}

type PlayerPickedUpItemEvent struct {
	*twodee.BasicGameEvent
	Item *Item
}

func NewPlayerPickedUpItemEvent(i *Item) (e *PlayerPickedUpItemEvent) {
	e = &PlayerPickedUpItemEvent{
		twodee.NewBasicGameEvent(PlayerPickedUpItem),
		i,
	}
	return
}

package main

import (
	twodee "../libs/twodee"
)

const (
	UpLayer twodee.GameEventType = iota
	DownLayer
	UpWaterLevel
	DownWaterLevel
	PlayerMove
	GameIsClosing
	PlayExploreMusic
	PauseMusic
	ResumeMusic
	PlayerPickedUpItem
	sentinel
)

const (
	NumGameEventTypes = int(sentinel)
)

type MoveDirection byte

const (
	None  MoveDirection = iota
	North MoveDirection = 1 << (iota - 1)
	East
	South
	West
)

type PlayerMoveEvent struct {
	*twodee.BasicGameEvent
	Dir MoveDirection
}

func NewPlayerMoveEvent(direction MoveDirection) (e *PlayerMoveEvent) {
	e = &PlayerMoveEvent{
		twodee.NewBasicGameEvent(PlayerMove),
		direction,
	}
	return
}

type PlayerPickedUpItemEvent struct {
	*twodee.BasicGameEvent
	Item *Item
}

func NewPlayerPickedUpItemEvent(i *Item) (e *PlayerPickedUpItemEvent) {
	e = &PlayerPickedUpItemEvent{
		twodee.NewBasicGameEvent(PlayerPickedUpItem),
		i,
	}
	return
}

Make MoveDirections each a byte.


// Copyright © 2013-2017 Pierre Neidhardt <ambrevar@gmail.com>
// Use of this file is governed by the license that can be found in LICENSE.

package main

import (
	"regexp"
	"strings"

	"github.com/jhprks/damerau"
)

var (
	reNorm = regexp.MustCompile(`\b0+|[^\pL\pN]`)
)

// Remove punctuation and padding zeros for number comparisons. Return the
// result in lowercase. This is useful to make string relations more relevant.
func stringNorm(s string) string {
	return strings.ToLower(reNorm.ReplaceAllString(s, ""))
}

// Return the Damerau-Levenshtein distance divided by the length of the longest
// string, so that two identical strings return 1, and two completely unrelated
// strings return 0.
func stringRel(a, b string) float64 {
	max := len([]rune(a))
	if len([]rune(b)) > max {
		max = len([]rune(b))
	} else if max == 0 {
		return 1
	}

	distance := damerau.DamerauLevenshteinDistance(a, b)
	return 1 - float64(distance)/float64(max)
}

// Copyright © 2013-2017 Pierre Neidhardt <ambrevar@gmail.com>
// Use of this file is governed by the license that can be found in LICENSE.

package main

import (
	"regexp"
	"strings"

	"github.com/ambrevar/damerau"
)

var (
	reNorm = regexp.MustCompile(`\b0+|[^\pL\pN]`)
)

// Remove punctuation and padding zeros for number comparisons. Return the
// result in lowercase. This is useful to make string relations more relevant.
func stringNorm(s string) string {
	return strings.ToLower(reNorm.ReplaceAllString(s, ""))
}

// Return the Damerau-Levenshtein distance divided by the length of the longest
// string, so that two identical strings return 1, and two completely unrelated
// strings return 0.
func stringRel(a, b string) float64 {
	max := len([]rune(a))
	if len([]rune(b)) > max {
		max = len([]rune(b))
	} else if max == 0 {
		return 1
	}

	distance := damerau.DamerauLevenshteinDistance(a, b)
	return 1 - float64(distance)/float64(max)
}

Update "damerau" repository URL to github.com/ambrevar


package main

import (
    "net/http"
    "io/ioutil"
    "os"
)

func saveHandler(w http.ResponseWriter, r *http.Request) {
	folder := "/PRODUCTION/EXPERIMENT/web/savedfiles/"
    filename := generateRandomURL()
    path := folder + filename

    if _, err := os.Stat(path); err != nil {
	    if os.IsNotExist(err) {
		    http.Error(w, err.Error(), http.StatusInternalServerError)
		    return
	    }
    }

    r.ParseForm()
    text := r.Form.Get("text")
	ioutil.WriteFile(path, []byte(text), 0400)

	http.Redirect(w, r, "http://experiment.safkanyazilim.com/"+filename, http.StatusTemporaryRedirect)
}

func generateRandomURL() string {
	return "1234556"
}

func main() {
	http.HandleFunc("/save", saveHandler)
    http.ListenAndServe(":8080", nil)
}

package main

import (
    "net/http"
    "io/ioutil"
    "os"
)

func saveHandler(w http.ResponseWriter, r *http.Request) {
	folder := "/PRODUCTION/EXPERIMENT/web/savedfiles/"
    filename := generateRandomURL()
    path := folder + filename

    if _, err := os.Stat(path); err != nil {
	    if os.IsNotExist(err) {
		    http.Error(w, err.Error(), http.StatusInternalServerError)
		    return
	    }
    }

    r.ParseForm()
    text := r.Form.Get("text")
	ioutil.WriteFile(path, []byte(text), 0400)
	os.Chown(path, 995, 994)

	http.Redirect(w, r, "http://experiment.safkanyazilim.com/"+filename, http.StatusTemporaryRedirect)
}

func generateRandomURL() string {
	return "1234556"
}

func main() {
	http.HandleFunc("/save", saveHandler)
    http.ListenAndServe(":8080", nil)
}

Change owner of saved file


// Copyright (c) 2016 by Richard A. Wilkes. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, version 2.0. If a copy of the MPL was not distributed with
// this file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// This Source Code Form is "Incompatible With Secondary Licenses", as
// defined by the Mozilla Public License, version 2.0.

package app

import (
	"github.com/richardwilkes/ui/menu/custom"
)

func platformStartUserInterface() {
	custom.Install()
	// RAW: Implement for Windows
}

func platformAppName() string {
	// RAW: Implement platformAppName for Windows
	return "<unknown>"
}

func platformHideApp() {
	// RAW: Implement for Windows
}

func platformHideOtherApps() {
	// RAW: Implement for Windows
}

func platformShowAllApps() {
	// RAW: Implement for Windows
}

func platformAttemptQuit() {
	// RAW: Implement for Windows
}

func platformAppMayQuitNow(quit bool) {
	// RAW: Implement for Windows
}

// Copyright (c) 2016 by Richard A. Wilkes. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, version 2.0. If a copy of the MPL was not distributed with
// this file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// This Source Code Form is "Incompatible With Secondary Licenses", as
// defined by the Mozilla Public License, version 2.0.

package app

import (
	"github.com/richardwilkes/ui/menu/custom"
	"os"
	"path/filepath"
)

func platformStartUserInterface() {
	custom.Install()
	// RAW: Implement for Windows
}

func platformAppName() string {
	return filepath.Base(os.Args[0])
}

func platformHideApp() {
	for _, wnd := range window.Windows() {
		wnd.Minimize()
	}
}

func platformHideOtherApps() {
	panic("platformHideOtherApps() is not implemented")
}

func platformShowAllApps() {
	panic("platformShowAllApps() is not implemented")
}

Implement platformAppName() and platformHideApp(), panic on use of platformHideOtherApps() or platformShowAllApps().


package server

import (
	"crypto/hmac"
	"crypto/sha1"
	"encoding/base64"
	"fmt"
	"time"
)

type ICEAuthServer struct {
	URLs       []string `json:"urls"`
	Username   string   `json:"username,omitempty"`
	Credential string   `json:"credential,omitempty"`
}

func GetICEAuthServers(servers []ICEServer) (result []ICEAuthServer) {
	for _, server := range servers {
		result = append(result, newICEServer(server))
	}
	return
}

func newICEServer(server ICEServer) ICEAuthServer {
	switch server.AuthType {
	case AuthTypeSecret:
		return getICEStaticAuthSecretCredentials(server)
	default:
		return ICEAuthServer{URLs: server.URLs}
	}
}

func getICEStaticAuthSecretCredentials(server ICEServer) ICEAuthServer {
	timestamp := time.Now().UnixNano() / 1_000_000
	username := fmt.Sprintf("%d:%s", timestamp, server.AuthSecret.Username)
	h := hmac.New(sha1.New, []byte(server.AuthSecret.Secret))
	h.Write([]byte(username))
	credential := base64.StdEncoding.EncodeToString(h.Sum(nil))

	return ICEAuthServer{
		URLs:       server.URLs,
		Username:   username,
		Credential: credential,
	}
}

package server

import (
	"crypto/hmac"
	"crypto/sha1"
	"encoding/base64"
	"fmt"
	"time"
)

type ICEAuthServer struct {
	URLs       []string `json:"urls"`
	Username   string   `json:"username,omitempty"`
	Credential string   `json:"credential,omitempty"`
}

func GetICEAuthServers(servers []ICEServer) (result []ICEAuthServer) {
	for _, server := range servers {
		result = append(result, newICEServer(server))
	}
	return
}

func newICEServer(server ICEServer) ICEAuthServer {
	switch server.AuthType {
	case AuthTypeSecret:
		return getICEStaticAuthSecretCredentials(server)
	default:
		return ICEAuthServer{URLs: server.URLs}
	}
}

func getICEStaticAuthSecretCredentials(server ICEServer) ICEAuthServer {
	timestamp := time.Now().Unix() + 24*3600
	username := fmt.Sprintf("%d:%s", timestamp, server.AuthSecret.Username)
	h := hmac.New(sha1.New, []byte(server.AuthSecret.Secret))
	h.Write([]byte(username))
	credential := base64.StdEncoding.EncodeToString(h.Sum(nil))

	return ICEAuthServer{
		URLs:       server.URLs,
		Username:   username,
		Credential: credential,
	}
}

Fix turnserver auth timestamp generation


package metrics

type Histogram interface {
	Clear()
	Update(value int64)
	Count() uint64
	Sum() int64
	Min() int64
	Max() int64
	Mean() float64
	Percentiles([]float64) []int64
}

package metrics

// import "encoding/json"
import (
	"bytes"
	"fmt"
	"strconv"
)

type Histogram interface {
	Clear()
	Update(value int64)
	Count() uint64
	Sum() int64
	Min() int64
	Max() int64
	Mean() float64
	Percentiles([]float64) []int64
}

type HistogramExport struct {
	Histogram       Histogram
	Percentiles     []float64
	PercentileNames []string
}

type histogramValues struct {
	count       uint64
	sum         int64
	min         int64
	max         int64
	mean        float64
	percentiles map[string]int64
}

// Return a JSON encoded version of the Histgram output
func (e *HistogramExport) String() string {
	h := e.Histogram
	b := &bytes.Buffer{}
	fmt.Fprintf(b, "{\"count\":%d,\"sum\":%d,\"min\":%d,\"max\":%d,\"mean\":%s",
		h.Count(), h.Sum(), h.Min(), h.Max(), strconv.FormatFloat(h.Mean(), 'g', -1, 64))
	perc := h.Percentiles(e.Percentiles)
	for i, p := range perc {
		fmt.Fprintf(b, ",\"%s\":%d", e.PercentileNames[i], p)
	}
	fmt.Fprintf(b, "}")
	return b.String()
}

Add HistogramExport to work with expvars


/*
Copyright 2015 The Kubernetes Authors.

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 cinder contains the internal representation of cinder volumes.
package cinder // import "k8s.io/kubernetes/pkg/volume/cinder"

Use Go canonical import paths


package qbot

import (
	"strings"

	"github.com/doozr/guac"
	"github.com/doozr/jot"
	"github.com/doozr/qbot/command"
	"github.com/doozr/qbot/queue"
	"github.com/doozr/qbot/util"
)

// MessageHandler handles an incoming message event.
type MessageHandler func(queue.Queue, guac.MessageEvent) (queue.Queue, error)

// CommandMap is a dictionary of command strings to functions.
type CommandMap map[string]command.Command

// CreateMessageHandler creates a message handler that calls a command function.
func CreateMessageHandler(commands CommandMap, notify Notifier) MessageHandler {
	return func(oq queue.Queue, m guac.MessageEvent) (q queue.Queue, err error) {
		text := strings.Trim(m.Text, " \t\r\n")

		var response command.Notification

		cmd, args := util.StringPop(text)
		cmd = strings.ToLower(cmd)

		jot.Printf("message dispatch: message %s with cmd %s and args %v", m.Text, cmd, args)
		fn, ok := commands[cmd]
		if !ok {
			q = oq
			return
		}

		q, response = fn(oq, m.Channel, m.User, args)

		err = notify(response)
		return
	}
}

package qbot

import (
	"strings"

	"github.com/doozr/guac"
	"github.com/doozr/jot"
	"github.com/doozr/qbot/command"
	"github.com/doozr/qbot/queue"
	"github.com/doozr/qbot/util"
)

// MessageHandler handles an incoming message event.
type MessageHandler func(queue.Queue, guac.MessageEvent) (queue.Queue, error)

// CommandMap is a dictionary of command strings to functions.
type CommandMap map[string]command.Command

// CreateMessageHandler creates a message handler that calls a command function.
func CreateMessageHandler(commands CommandMap, notify Notifier) MessageHandler {
	return func(oq queue.Queue, m guac.MessageEvent) (q queue.Queue, err error) {
		text := strings.Trim(m.Text, " \t\r\n")

		var response command.Notification

		cmd, args := util.StringPop(text)
		cmd = strings.ToLower(cmd)

		jot.Printf("message dispatch: message %s with cmd %s and args %v", m.Text, cmd, args)
		fn, ok := commands[cmd]
		if !ok {
			fn, ok = commands["help"]
			if !ok {
				q = oq
				return
			}
		}

		q, response = fn(oq, m.Channel, m.User, args)

		err = notify(response)
		return
	}
}

Send help if command not recognised


package main

import (
	"context"
	"fmt"
	"os"

	"github.com/docker/engine-api/client"
)

func main() {
	cli, err := client.NewEnvClient()
	quitOnErr(err)
	ver, err := cli.ServerVersion(context.Background())
	quitOnErr(err)
	fmt.Println("Was able to connect to docker daemon")
	fmt.Println(ver)
}

func quitOnErr(err error) {
	if err != nil {
		fmt.Printf("error ocurred: %v", err)
		os.Exit(1)
	}
}

package main

import (
	"context"
	"fmt"
	"os"

	"github.com/docker/engine-api/client"
	"github.com/docker/engine-api/types"
	"github.com/docker/engine-api/types/container"
	"github.com/docker/engine-api/types/strslice"
)

func main() {
	// Create docker client
	cli, err := client.NewEnvClient()
	quitOnErr(err)

	// Let's see if we can even talk to it
	ver, err := cli.ServerVersion(context.Background())
	quitOnErr(err)
	fmt.Println("Was able to connect to docker daemon")
	fmt.Println(ver)

	// Let's create a container
	cmd := strslice.StrSlice{"echo", "hello", "world"}
	containerConf := &container.Config{
		Image: "busybox",
		Cmd:   cmd,
	}
	resp, err := cli.ContainerCreate(context.Background(), containerConf, nil, nil, "")
	quitOnErr(err)
	err = cli.ContainerStart(context.Background(), resp.ID, types.ContainerStartOptions{})
	quitOnErr(err)
	fmt.Println("Was able to run a container")
}

func quitOnErr(err error) {
	if err != nil {
		fmt.Printf("error ocurred: %v", err)
		os.Exit(1)
	}
}

Create and start a container


package dbg

import (
	"fmt"
	"io/ioutil"
	"os"
	"runtime"
	"runtime/pprof"
	"time"

	"github.com/Sirupsen/logrus"
)

const (
	path = "/var/cores/"
)

// DumpGoMemoryTrace output memory profile to logs.
func DumpGoMemoryTrace() {
	m := &runtime.MemStats{}
	runtime.ReadMemStats(m)
	res := fmt.Sprintf("%#v", m)
	logrus.Infof("==== Dumping Memory Profile ===")
	logrus.Infof(res)
}

// DumpGoProfile output goroutines to file.
func DumpGoProfile() error {
	trace := make([]byte, 1024*1024)
	len := runtime.Stack(trace, true)
	return ioutil.WriteFile(path+time.Now().String()+".stack", trace[:len], 0644)
}

func DumpHeap() {
	f, err := os.Create(path + time.Now().String() + ".heap")
	if err != nil {
		logrus.Errorf("could not create memory profile: %v", err)
		return
	}
	defer f.Close()
	if err := pprof.WriteHeapProfile(f); err != nil {
		logrus.Errorf("could not write memory profile: %v", err)
	}
}

package dbg

import (
	"fmt"
	"io/ioutil"
	"os"
	"runtime"
	"runtime/pprof"
	"time"

	"github.com/Sirupsen/logrus"
)

const (
	path = "/var/cores/"
)

// DumpGoMemoryTrace output memory profile to logs.
func DumpGoMemoryTrace() {
	m := &runtime.MemStats{}
	runtime.ReadMemStats(m)
	res := fmt.Sprintf("%#v", m)
	logrus.Infof("==== Dumping Memory Profile ===")
	logrus.Infof(res)
}

// DumpGoProfile output goroutines to file.
func DumpGoProfile() error {
	trace := make([]byte, 5120*1024)
	len := runtime.Stack(trace, true)
	return ioutil.WriteFile(path+time.Now().String()+".stack", trace[:len], 0644)
}

func DumpHeap() {
	f, err := os.Create(path + time.Now().String() + ".heap")
	if err != nil {
		logrus.Errorf("could not create memory profile: %v", err)
		return
	}
	defer f.Close()
	if err := pprof.WriteHeapProfile(f); err != nil {
		logrus.Errorf("could not write memory profile: %v", err)
	}
}

Increase the goprofile trace limit to 5M.


package version

import "testing"

func TestFullversion(t *testing.T) {
	tests := []struct {
		name string
		want string
	}{
		{
			name: "Get correct name",
			want: Version + "-" + Revision,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := fullversion(); got != tt.want {
				t.Errorf("fullversion() = %v, want %v", got, tt.want)
			}
		})
	}
}

package version

import "testing"

func TestFullversion(t *testing.T) {
	tests := []struct {
		name string
		want string
	}{
		{
			name: "Get correct name",
			want: Version + "-" + Revision,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := Fullversion(); got != tt.want {
				t.Errorf("fullversion() = %v, want %v", got, tt.want)
			}
		})
	}
}

Rename method in test code


package model_test

import (
	"encoding/json"
	"github.com/crezam/actions-on-google-golang/internal/test"
	"github.com/crezam/actions-on-google-golang/model"
	"os"
	"testing"
	"time"
)

func TestRequestParsing(t *testing.T) {

	var req model.ApiAiRequest

	file, _ := os.Open("./data/sample_request1.json")
	dec := json.NewDecoder(file)

	err := dec.Decode(&req)

	// test if any issues decoding file
	test.Ok(t, err)

	// assert correct parsing
	test.Equals(t, "209eefa7-adb5-4d03-a8b9-9f7ae68a0c11", req.Id)

	expectedTimestamp, _ := time.Parse(time.RFC3339Nano, "2016-10-10T07:41:40.098Z")
	test.Equals(t, expectedTimestamp, req.Timestamp)

	test.Equals(t, "Hi, my name is Sam!", req.Result.ResolvedQuery)
	test.Equals(t, "agent", req.Result.Source)
	test.Equals(t, "greetings", req.Result.Action)
	test.Equals(t, false, req.Result.ActionIncomplete)
	test.Equals(t, "Sam", req.Result.Parameters.Parameters["user_name"])

}

package model_test

import (
	"encoding/json"
	"github.com/crezam/actions-on-google-golang/internal/test"
	"github.com/crezam/actions-on-google-golang/model"
	"os"
	"testing"
	"time"
)

func TestRequestParsing(t *testing.T) {

	var req model.ApiAiRequest

	file, _ := os.Open("./data/sample_request1.json")
	dec := json.NewDecoder(file)

	err := dec.Decode(&req)

	// test if any issues decoding file
	test.Ok(t, err)

	// assert correct parsing
	test.Equals(t, "209eefa7-adb5-4d03-a8b9-9f7ae68a0c11", req.Id)

	expectedTimestamp, _ := time.Parse(time.RFC3339Nano, "2016-10-10T07:41:40.098Z")
	test.Equals(t, expectedTimestamp, req.Timestamp)

	test.Equals(t, "Hi, my name is Sam!", req.Result.ResolvedQuery)
	test.Equals(t, "agent", req.Result.Source)
	test.Equals(t, "greetings", req.Result.Action)
	test.Equals(t, false, req.Result.ActionIncomplete)
	test.Equals(t, "Sam", req.Result.Parameters["user_name"])
	test.Equals(t, "", req.Result.Parameters["school"])

}

Test a non existent parameter


// +build !windows,!plan9,!js

package sys

import (
	"os"
	"os/signal"
	"syscall"
)

func NotifySignals() chan os.Signal {
	// This catches every signal regardless of whether it is ignored.
	sigCh := make(chan os.Signal, sigsChanBufferSize)
	signal.Notify(sigCh)
	// TODO: Remove this if, and when, job control is implemented. This
	// handles the case of running an external command from an interactive
	// prompt.
	//
	// See https://github.com/elves/elvish/issues/988.
	signal.Ignore(syscall.SIGTTIN, syscall.SIGTTOU, syscall.SIGTSTP)
	return sigCh
}

// +build !windows,!plan9,!js

package sys

import (
	"os"
	"os/signal"
	"syscall"
)

func NotifySignals() chan os.Signal {
	// This catches every signal regardless of whether it is ignored.
	sigCh := make(chan os.Signal, sigsChanBufferSize)
	signal.Notify(sigCh)
	// Calling signal.Notify will reset the signal ignore status, so we need to
	// call signal.Ignore every time we call signal.Notify.
	//
	// TODO: Remove this if, and when, job control is implemented. This
	// handles the case of running an external command from an interactive
	// prompt.
	//
	// See https://github.com/elves/elvish/issues/988.
	signal.Ignore(syscall.SIGTTIN, syscall.SIGTTOU, syscall.SIGTSTP)
	return sigCh
}

Document why it's necessary to call signal.Ignore.


package ipjogli

import (
	"fmt"
	"net/http"
)

func init() {
	http.HandleFunc("/", handler)
}

func handler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, r.RemoteAddr+"\n")
}

package ipjogli

import (
	"fmt"
	"net"
	"net/http"
)

func init() {
	http.HandleFunc("/", handler)
}

func handler(w http.ResponseWriter, r *http.Request) {
	addr, _, err := net.SplitHostPort(r.RemoteAddr)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		return
	}
	fmt.Fprintf(w, addr+"\n")
}

Split IP and port from RemoteAddr


package main

import (
	"flag"
	"fmt"
	"net/http"
)

var (
	port int
	dir  string
)

func init() {
	flag.IntVar(&port, "port", 8080, "Port to run server on.")
	flag.StringVar(&dir, "dir", ".", "Directory to serve.")
	flag.Parse()
}

func main() {
	http.ListenAndServe(fmt.Sprintf(":%d", port), http.FileServer(http.Dir(dir)))
}

package main

import (
	"flag"
	"fmt"
	"net/http"
)

var (
	port int
	dir  string
)

func init() {
	flag.IntVar(&port, "port", 8080, "Port to run server on.")
	flag.StringVar(&dir, "dir", ".", "Directory to serve.")
	flag.Parse()
}

func main() {
	fmt.Printf("Serving files from %s, running on http://localhost:%d\n", dir, port)
	http.ListenAndServe(fmt.Sprintf(":%d", port), http.FileServer(http.Dir(dir)))

}

Print out the URL when it starts


package dmesg // import "github.com/docker/docker/pkg/dmesg"

import (
	"unsafe"

	"golang.org/x/sys/unix"
)

// Dmesg returns last messages from the kernel log, up to size bytes
func Dmesg(size int) []byte {
	t := uintptr(3) // SYSLOG_ACTION_READ_ALL
	b := make([]byte, size)
	amt, _, err := unix.Syscall(unix.SYS_SYSLOG, t, uintptr(unsafe.Pointer(&b[0])), uintptr(len(b)))
	if err != 0 {
		return []byte{}
	}
	return b[:amt]
}

package dmesg // import "github.com/docker/docker/pkg/dmesg"

import (
	"golang.org/x/sys/unix"
)

// Dmesg returns last messages from the kernel log, up to size bytes
func Dmesg(size int) []byte {
	t := 3 // SYSLOG_ACTION_READ_ALL
	b := make([]byte, size)
	amt, err := unix.Klogctl(t, b)
	if err != nil {
		return []byte{}
	}
	return b[:amt]
}

Use Klogctl from x/sys/unix to read Linux kernel log


package limiter

// -----------------------------------------------------------------
// Store
// -----------------------------------------------------------------

// Store is the common interface for limiter stores.
type Store interface {
	Get(key string, rate Rate) (Context, error)
}

// -----------------------------------------------------------------
// Context
// -----------------------------------------------------------------

// Context is the limit context.
type Context struct {
	Limit     int64
	Remaining int64
	Used      int64
	Reset     int64
	Reached   bool
}

// -----------------------------------------------------------------
// Limiter
// -----------------------------------------------------------------

// Limiter is the limiter instance.
type Limiter struct {
	Store Store
	Rate  Rate
}

// NewLimiter returns an instance of ratelimit.
func NewLimiter(store Store, rate Rate) *Limiter {
	return &Limiter{
		Store: store,
		Rate:  rate,
	}
}

// Get returns the limit for the identifier.
func (l *Limiter) Get(key string) (Context, error) {
	return l.Store.Get(key, l.Rate)
}

Remove useless Used field in Limiter struct.


package problem_2_test

import (
  . "github.com/onsi/ginkgo"
  . "github.com/onsi/gomega"
  "testing"
)

func TestProblem_2(t *testing.T) {
  RegisterFailHandler(Fail)
  RunSpecs(t, "Problem 2")
}

var _ = Describe("fibGenerator", func() {
  It("gives the first few Fibonacci numbers", func() {
    c := fibGenerator()

    Expect(<-c).To(Equal(1))
    Expect(<-c).To(Equal(2))
    Expect(<-c).To(Equal(3))
    Expect(<-c).To(Equal(5))
    Expect(<-c).To(Equal(8))
    Expect(<-c).To(Equal(13))
  })
})

var _ = Describe("EvenFibonacciSum", func() {
  It("works with a limit of 1", func() {
    Expect(EvenFibonacciSum(1)).To(Equal(0))
  })

  It("works with a limit of 2", func() {
    Expect(EvenFibonacciSum(2)).To(Equal(2))
  })

  It("works with a limit of 3", func() {
    Expect(EvenFibonacciSum(3)).To(Equal(2))
  })

  It("works with a limit of 8", func() {
    Expect(EvenFibonacciSum(8)).To(Equal(10))
  })
})

package problem_2

import (
  . "github.com/onsi/ginkgo"
  . "github.com/onsi/gomega"
  "testing"
)

func TestProblem_2(t *testing.T) {
  RegisterFailHandler(Fail)
  RunSpecs(t, "Problem 2")
}

var _ = Describe("fibGenerator", func() {
  It("gives the first few Fibonacci numbers", func() {
    c := fibGenerator()

    Expect(<-c).To(Equal(1))
    Expect(<-c).To(Equal(2))
    Expect(<-c).To(Equal(3))
    Expect(<-c).To(Equal(5))
    Expect(<-c).To(Equal(8))
    Expect(<-c).To(Equal(13))
  })
})

var _ = Describe("EvenFibonacciSum", func() {
  It("works with a limit of 1", func() {
    Expect(EvenFibonacciSum(1)).To(Equal(0))
  })

  It("works with a limit of 2", func() {
    Expect(EvenFibonacciSum(2)).To(Equal(2))
  })

  It("works with a limit of 3", func() {
    Expect(EvenFibonacciSum(3)).To(Equal(2))
  })

  It("works with a limit of 8", func() {
    Expect(EvenFibonacciSum(8)).To(Equal(10))
  })
})

Fix pachage in problem 2


// Copyright 2015, Mike Houston, see LICENSE for details.
package bloompw

import (
	"github.com/AndreasBriese/bbloom"
)

type BloomPW struct {
	Filter *bbloom.Bloom
}

// New will return a new Database interface that stores entries in
// a bloom filter
func New(filter *bbloom.Bloom) (*BloomPW, error) {
	b := &BloomPW{Filter: filter}

	return b, nil
}

// Has satisfies the password.DB interface
func (b BloomPW) Has(s string) (bool, error) {
	return b.Filter.Has([]byte(s)), nil
}

// Has satisfies the password.DbWriter interface.
// It writes a single password to the database
func (b BloomPW) Add(s string) error {
	b.Filter.Add([]byte(s))
	return nil
}

// AddMultiple satisfies the password.BulkWriter interface.
// It writes a number of passwords to the database
func (b BloomPW) AddMultiple(s []string) error {
	for _, v := range s {
		if err := b.Add(v); err != nil {
			return err
		}
	}
	return nil
}

// Copyright 2015, Mike Houston, see LICENSE for details.

// A bitset Bloom filter for a reduced memory password representation.
//
// See https://github.com/AndreasBriese/bbloom
package bloompw

import (
	"github.com/AndreasBriese/bbloom"
)

type BloomPW struct {
	Filter *bbloom.Bloom
}

// New will return a new Database interface that stores entries in
// a bloom filter
func New(filter *bbloom.Bloom) (*BloomPW, error) {
	b := &BloomPW{Filter: filter}

	return b, nil
}

// Has satisfies the password.DB interface
func (b BloomPW) Has(s string) (bool, error) {
	return b.Filter.Has([]byte(s)), nil
}

// Has satisfies the password.DbWriter interface.
// It writes a single password to the database
func (b BloomPW) Add(s string) error {
	b.Filter.Add([]byte(s))
	return nil
}

// AddMultiple satisfies the password.BulkWriter interface.
// It writes a number of passwords to the database
func (b BloomPW) AddMultiple(s []string) error {
	for _, v := range s {
		if err := b.Add(v); err != nil {
			return err
		}
	}
	return nil
}

Add a little bit of documentation.


package main

import (
	"reflect"
	"regexp"
	"testing"
)

var genMatcherTests = []struct {
	src string
	dst *regexp.Regexp
}{
	{"abc", regexp.MustCompile(`abc`)},
}

func TestGenMatcher(t *testing.T) {
	for _, test := range genMatcherTests {
		expect := test.dst
		actual, err := newMatcher(test.src)
		if err != nil {
			t.Errorf("NewSubvert(%q) returns %q, want nil",
				test.src, err)
		}
		if !reflect.DeepEqual(actual, expect) {
			t.Errorf("%q: got %q, want %q",
				test.src, actual, expect)
		}
	}
}

package main

import (
	"reflect"
	"regexp"
	"testing"
)

var genMatcherTests = []struct {
	src string
	dst *regexp.Regexp
}{
	{"abc", regexp.MustCompile(`abc`)},

	{"a,b", regexp.MustCompile(`(a|b)`)},
}

func TestGenMatcher(t *testing.T) {
	for _, test := range genMatcherTests {
		expect := test.dst
		actual, err := newMatcher(test.src)
		if err != nil {
			t.Errorf("NewSubvert(%q) returns %q, want nil",
				test.src, err)
		}
		if !reflect.DeepEqual(actual, expect) {
			t.Errorf("%q: got %q, want %q",
				test.src, actual, expect)
		}
	}
}

Add case of branch for newMatcher


package main

import (
	"github.com/drone/drone-go/drone"
)

type Params struct {
	AccessKey    string            `json:"access_key"`
	SecretKey    string            `json:"secret_key"`
	Region       string            `json:"region"`
	Family       string            `json:"family"`
	Image        string            `json:"image_name"`
	Tag          string            `json:"image_tag"`
	Service      string            `json:"service"`
	Memory       int64             `json:"memory"`
	Environment  drone.StringSlice `json:"environment_variables"`
	PortMappings drone.StringSlice `json:"port_mappings"`
}

package main

import (
	"github.com/drone/drone-go/drone"
)

type Params struct {
	AccessKey    string            `json:"access_key"`
	SecretKey    string            `json:"secret_key"`
	Region       string            `json:"region"`
	Family       string            `json:"family"`
	Image        string            `json:"image_name"`
	Tag          string            `json:"image_tag"`
	Service      string            `json:"service"`
	Cluster      string            `json:"cluster"`
	Memory       int64             `json:"memory"`
	Environment  drone.StringSlice `json:"environment_variables"`
	PortMappings drone.StringSlice `json:"port_mappings"`
}

Add Cluster to Params struct.


package Publish

import (
	"runtime"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
)

func TestShare(t *testing.T) {
	scheduler := NewGoroutine()
	ch := make(chan int, 30)
	s := FromChanInt(ch).Publish().RefCount().SubscribeOn(scheduler)
	a := []int{}
	b := []int{}
	asub := s.SubscribeNext(func(n int) { a = append(a, n) }, SubscribeOn(scheduler))
	bsub := s.SubscribeNext(func(n int) { b = append(b, n) }, SubscribeOn(scheduler))
	ch <- 1
	ch <- 2
	ch <- 3
	// make sure the channel gets enough time to be fully processed.
	for i := 0; i < 10; i++ {
		time.Sleep(20 * time.Millisecond)
		runtime.Gosched()
	}
	asub.Unsubscribe()
	assert.True(t, asub.Closed())
	ch <- 4
	close(ch)
	bsub.Wait()
	assert.Equal(t, []int{1, 2, 3}, a)
	assert.Equal(t, []int{1, 2, 3, 4}, b)
	//assert.True(t, false, "force fail")
}

package Publish

import (
	"runtime"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
)

func TestPublishRefCount(t *testing.T) {
	scheduler := NewGoroutine()
	ch := make(chan int, 30)
	s := FromChanInt(ch).Publish().RefCount().SubscribeOn(scheduler)
	a := []int{}
	b := []int{}
	asub := s.SubscribeNext(func(n int) { a = append(a, n) }, SubscribeOn(scheduler))
	bsub := s.SubscribeNext(func(n int) { b = append(b, n) }, SubscribeOn(scheduler))
	ch <- 1
	ch <- 2
	ch <- 3
	// make sure the channel gets enough time to be fully processed.
	for i := 0; i < 10; i++ {
		time.Sleep(20 * time.Millisecond)
		runtime.Gosched()
	}
	asub.Unsubscribe()
	assert.True(t, asub.Closed())
	ch <- 4
	close(ch)
	bsub.Wait()
	assert.Equal(t, []int{1, 2, 3}, a)
	assert.Equal(t, []int{1, 2, 3, 4}, b)
	//assert.True(t, false, "force fail")
}

Rename test for Publish operator.


package dense

type nonZerosCursor struct {
	matrix  *denseMatrix
	element float64
	row     int
	column  int
	index   int
}

func newNonZerosCursor(matrix *denseMatrix) *nonZerosCursor {
	c := &nonZerosCursor{
		matrix:  matrix,
		element: 0,
		row:     0,
		column:  0,
		index:   0,
	}

	return c
}

func (c *nonZerosCursor) HasNext() bool {
	for c.index < len(c.matrix.elements) {
		if element := c.matrix.elements[c.index]; element != 0 {
			c.element = element

			c.row = c.index / c.matrix.columns
			c.column = c.index % c.matrix.columns

			c.index++

			return true
		} else {
			c.index++
		}
	}

	return false
}

func (c *nonZerosCursor) Get() (element float64, row, column int) {
	row, column = c.matrix.rewriter.Rewrite(c.row, c.column)
	return c.element, row, column
}

package dense

import (
	"github.com/mitsuse/matrix-go/internal/types"
)

type nonZerosCursor struct {
	cursor types.Cursor
}

func newNonZerosCursor(matrix *denseMatrix) *nonZerosCursor {
	c := &nonZerosCursor{
		cursor: matrix.All(),
	}

	return c
}

func (c *nonZerosCursor) HasNext() bool {
	for c.cursor.HasNext() {
		if element, _, _ := c.cursor.Get(); element != 0 {
			return true
		}
	}

	return false
}

func (c *nonZerosCursor) Get() (element float64, row, column int) {
	return c.cursor.Get()
}

Support base and view in "NonZeros" cursor.


package termite

import (
	"io/ioutil"
	"os"
	"syscall"
	"testing"
)

func TestFileAttrReadFrom(t *testing.T) {
	dir, _ := ioutil.TempDir("", "termite")
	ioutil.WriteFile(dir+"/file.txt", []byte{42}, 0644)

	attr := FileAttr{FileInfo: &os.FileInfo{Mode: syscall.S_IFDIR}}
	attr.ReadFromFs(dir)
	if attr.NameModeMap == nil {
		t.Fatalf("should have NameModeMap: %v", attr)
	}

	if attr.NameModeMap["file.txt"].IsRegular() {
		t.Fatalf("unexpected mode: %v", attr.NameModeMap["file.txt"])
	}
}

func TestFileMode(t *testing.T) {
	sock := FileMode(syscall.S_IFSOCK)
	if sock.IsDirectory() {
		t.Error("Socket should not be directory")
	}
}

package termite

import (
	"io/ioutil"
	"os"
	"syscall"
	"testing"
)

func TestFileAttrReadFrom(t *testing.T) {
	dir, _ := ioutil.TempDir("", "termite")
	ioutil.WriteFile(dir+"/file.txt", []byte{42}, 0644)

	attr := FileAttr{FileInfo: &os.FileInfo{Mode: syscall.S_IFDIR}}
	attr.ReadFromFs(dir)
	if attr.NameModeMap == nil {
		t.Fatalf("should have NameModeMap: %v", attr)
	}

	m := attr.NameModeMap["file.txt"]
	if !m.IsRegular() {
		t.Fatalf("unexpected mode: %o, want IsRegular()", m)
	}
}

func TestFileMode(t *testing.T) {
	sock := FileMode(syscall.S_IFSOCK)
	if sock.IsDirectory() {
		t.Error("Socket should not be directory")
	}
}

Fix thinko in file mode test.


// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package process

// ComponentName is the name of the Juju component for workload
// process management.
const ComponentName = "process"

// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

// The process package (and subpackages) contain the implementation of
// the charm workload process feature component. The various pieces are
// connected to the Juju machinery in component/all/process.go.
package process

// ComponentName is the name of the Juju component for workload
// process management.
const ComponentName = "process"

Add a package doc comment.


package raft

import (
	"time"
)

// Config provides any necessary configuraiton to
// the Raft server
type Config struct {
	// Time in follower state without a leader before we attempt an election
	HeartbeatTimeout time.Duration

	// Time in candidate state without a leader before we attempt an election
	ElectionTimeout time.Duration

	// Time without an Apply() operation before we heartbeat to ensure
	// a timely commit. Should be far less than HeartbeatTimeout to ensure
	// we don't lose leadership.
	CommitTimeout time.Duration

	// MaxAppendEntries controls the maximum number of append entries
	// to send at once. We want to strike a balance between efficiency
	// and avoiding waste if the follower is going to reject because of
	// an inconsistent log
	MaxAppendEntries int

	// If we are a member of a cluster, and RemovePeer is invoked for the
	// local node, then we forget all peers and transition into the follower state.
	// If ShutdownOnRemove is is set, we additional shutdown Raft. Otherwise,
	// we can become a leader of a cluster containing only this node.
	ShutdownOnRemove bool
}

func DefaultConfig() *Config {
	return &Config{
		HeartbeatTimeout: 200 * time.Millisecond,
		ElectionTimeout:  250 * time.Millisecond,
		CommitTimeout:    10 * time.Millisecond,
		MaxAppendEntries: 16,
		ShutdownOnRemove: true,
	}
}

package raft

import (
	"time"
)

// Config provides any necessary configuraiton to
// the Raft server
type Config struct {
	// Time in follower state without a leader before we attempt an election
	HeartbeatTimeout time.Duration

	// Time in candidate state without a leader before we attempt an election
	ElectionTimeout time.Duration

	// Time without an Apply() operation before we heartbeat to ensure
	// a timely commit. Should be far less than HeartbeatTimeout to ensure
	// we don't lose leadership.
	CommitTimeout time.Duration

	// MaxAppendEntries controls the maximum number of append entries
	// to send at once. We want to strike a balance between efficiency
	// and avoiding waste if the follower is going to reject because of
	// an inconsistent log
	MaxAppendEntries int

	// If we are a member of a cluster, and RemovePeer is invoked for the
	// local node, then we forget all peers and transition into the follower state.
	// If ShutdownOnRemove is is set, we additional shutdown Raft. Otherwise,
	// we can become a leader of a cluster containing only this node.
	ShutdownOnRemove bool
}

func DefaultConfig() *Config {
	return &Config{
		HeartbeatTimeout: 200 * time.Millisecond,
		ElectionTimeout:  250 * time.Millisecond,
		CommitTimeout:    10 * time.Millisecond,
		MaxAppendEntries: 32,
		ShutdownOnRemove: true,
	}
}

Increase max append to 32


package main

import (
	"bytes"
	"log"
	"os/exec"

	"github.com/shizeeg/xmpp"
)

// RunPlugin is a generic interface for external commands.
func (s *Session) RunPlugin(stanza xmpp.Stanza, filename string, tonick bool, params ...string) {
	message, ok := stanza.Value.(*xmpp.ClientMessage)
	if !ok {
		log.Println("Wrong Stanza type!")
		return
	}
	lang := "-lang=en"
	if len(message.Lang) > 0 {
		lang = "-lang=" + message.Lang
	}
	plugin := exec.Command(filename, lang)
	plugin.Args = append(plugin.Args, params...)

	var out bytes.Buffer
	plugin.Stdout = &out
	if err := plugin.Run(); err != nil {
		log.Println(err)
		return
	}
	s.Say(stanza, out.String(), tonick, false)
}

package main

import (
	"bytes"
	"log"
	"os/exec"
	"strings"

	"github.com/shizeeg/xmpp"
)

// RunPlugin is a generic interface for external commands.
func (s *Session) RunPlugin(stanza xmpp.Stanza, filename string, tonick bool, params ...string) {
	message, ok := stanza.Value.(*xmpp.ClientMessage)
	if !ok {
		log.Println("Wrong Stanza type!")
		return
	}
	lang := "-lang=en"
	if len(message.Lang) >= 2 {
		lang = "-lang=" + message.Lang[:2]
	}
	plugin := exec.Command(filename, lang)
	plugin.Args = append(plugin.Args, params...)

	var out bytes.Buffer
	plugin.Stdout = &out
	if err := plugin.Run(); err != nil {
		log.Println(err)
		return
	}
	s.Say(stanza, strings.TrimSpace(out.String()), tonick, false)
}

FIX language detection. Some clients send incorrect data in xml:lang="en-US" Deal with that


package main

import(
	"net"
	"os"
)

const(
	RECV_BUF_LEN = 1024
)

func main() {
	println("Starting the server")
	listener, err := net.Listen("tcp", "127.0.0.1:8377")
	if err != nil {
		println("error Listen: ", err.Error())
		os.Exit(1)
	}

	for {
		conn, err := listener.Accept()
		if err != nil {
			println("error Accept: ", err.Error())
			return
		}

		go HandleConnection(conn)
	}
}

func HandleConnection(conn net.Conn) {
	buf := make([]byte, RECV_BUF_LEN)
	n, err := conn.Read(buf)
	if err != nil {
		println("error Read: ", err.Error())
		return
	}
	println("Received ", n, " bytes of data: ", string(buf))
	_, err = conn.Write(buf)
	if err != nil {
		println("error Write: ", err.Error())
		return
	} else {
		println("Reply echoed")
	}
}

package main

import(
	"log"
	"net"
)

const(
	RECV_BUF_LEN = 1024
)

func main() {
	log.Print("Starting the server")
	listener, err := net.Listen("tcp", "127.0.0.1:8377")
	if err != nil {
		log.Fatal("Listen: ", err.Error())
	}

	for {
		conn, err := listener.Accept()
		if err != nil {
			log.Print("Accept: ", err.Error())
			return
		}

		go HandleConnection(conn)
	}
}

func HandleConnection(conn net.Conn) {
	buf := make([]byte, RECV_BUF_LEN)
	n, err := conn.Read(buf)
	if err != nil {
		log.Print("Read: ", err.Error())
		return
	}

	log.Print("Received ", n, " bytes of data")
	_, err = conn.Write(buf)
	if err != nil {
		log.Print("Write: ", err.Error())
		return
	} else {
		log.Print("Reply echoed")
		conn.Close()
	}
}

Use log package instead of println


package schedule

import (
	"fmt"
	"math/rand"
)

func RandomSchedule(min, max) {
	fmt.Println("Start random scheduler")

	// representation of the past 365 days
	// for each day
	// get random value between --min and --max
	rnd := getRandomNumber(min, max)
	// save into structure representing the commits over the last year
	// start worker, which will execute all commits using some sort of
	// commit generator
}

// getRandomNumber returns a number in the range of min and max.
func getRandomNumber(min, max int) int {
	return rand.Intn(max-min) + min
}

package schedule

import (
	"math/rand"
	"time"
)

// RandomSchedule creates random commits over the past 365 days.
// These commits will be created in the location specified in the command.
func RandomSchedule(min, max) {

	days := getDaysSinceThisDayLastYear()
	for day := range days {
		rnd := getRandomNumber(min, max)
		// save into structure representing the commits over the last year
		// start worker, which will execute all commits using some sort of
		// commit generator
	}
}

// getRandomNumber returns a number in the range of min and max.
func getRandomNumber(min, max int) int {
	return rand.Intn(max-min) + min
}

// getDaysSinceThisDayLastYear returns a slice of days since todays date
// last year. E.g. 01.01.2015 starts at the 01.01.2014.
// Every day maps to itself minus one year except the 29.02 will map to 28.02.
func getDaysSinceThisDayLastYear() []time.Date {
	return
}

Add docs and further layout implementation.


package faker

import (
	"math/rand"
	"time"
)

func getRandValueInRange(rangeSize int, epsilon float64) float64 {
	rand.Seed(time.Now().UTC().UnixNano())

	minDataWithError := rand.Float64()*float64(rangeSize) - epsilon
	maxDataWithError := rand.Float64()*float64(rangeSize) + epsilon

	dataInRange := rand.Float64()*maxDataWithError + minDataWithError

	return dataInRange
}

func GenerateAngleVelocity(epsilon float64) float64 {
	return getRandValueInRange(360, epsilon)
}

func GenerateGravityAcceleration(epsilon float64) float64 {
	return getRandValueInRange(1023, epsilon)
}

func GenerateTemperature(epsilon float64) float64 {
	return getRandValueInRange(104, epsilon)
}

func GenerateHumidity(epsilon float64) float64 {
	return getRandValueInRange(100, epsilon)
}

package faker

import (
	"math/rand"
	"time"
)

func getRandValueInRange(rangeSize int, epsilon float64) float64 {
	rand.Seed(time.Now().UTC().UnixNano())

	maxDataWithError := float64(rangeSize) + 2*epsilon

	dataInRange := rand.Float64()*maxDataWithError - epsilon

	return dataInRange
}

func GenerateAngleVelocity(epsilon float64) float64 {
	return getRandValueInRange(360, epsilon)
}

func GenerateGravityAcceleration(epsilon float64) float64 {
	return getRandValueInRange(1023, epsilon)
}

func GenerateTemperature(epsilon float64) float64 {
	return getRandValueInRange(104, epsilon)
}

func GenerateHumidity(epsilon float64) float64 {
	return getRandValueInRange(100, epsilon)
}

Modify formular of generating random value


package tests

import (
	"fmt"
	"testing"
	"github.com/xrash/smetrics"
)

func TestHamming(t *testing.T) {
	cases := []hammingcase{
		{"a", "a", 0},
		{"a", "b", 1},
		{"AAAA", "AABB", 2},
		{"BAAA", "AAAA", 1},
		{"BAAA", "CCCC", 4},
	}

	for _, c := range cases {
		r, err := smetrics.Hamming(c.a, c.b)
		if err != nil {
			t.Fatalf("got error from hamming err=%s", err)
		}
		if r != c.diff {
			fmt.Println(r, "instead of", c.diff)
			t.Fail()
		}
	}
}

func TestHammingError(t *testing.T) {
	res, err := smetrics.Hamming("a", "bbb")
	if err == nil {
		t.Fatalf("expected error from 'a' and 'bbb' on hamming")
	}
	if res != -1 {
		t.Fatalf("erroring response wasn't -1, but %d", res)
	}
}

package tests

import (
	"fmt"
	"testing"
	"github.com/xrash/smetrics"
)

func TestHamming(t *testing.T) {
	cases := []hammingcase{
		{"a", "a", 0},
		{"a", "b", 1},
		{"AAAA", "AABB", 2},
		{"BAAA", "AAAA", 1},
		{"BAAA", "CCCC", 4},
		{"karolin", "kathrin", 3},
		{"karolin", "kerstin", 3},
		{"1011101", "1001001", 2},
		{"2173896", "2233796", 3},
	}

	for _, c := range cases {
		r, err := smetrics.Hamming(c.a, c.b)
		if err != nil {
			t.Fatalf("got error from hamming err=%s", err)
		}
		if r != c.diff {
			fmt.Println(r, "instead of", c.diff)
			t.Fail()
		}
	}
}

func TestHammingError(t *testing.T) {
	res, err := smetrics.Hamming("a", "bbb")
	if err == nil {
		t.Fatalf("expected error from 'a' and 'bbb' on hamming")
	}
	if res != -1 {
		t.Fatalf("erroring response wasn't -1, but %d", res)
	}
}

Add more tests to the Hamming function.


// Copyright 2014 The Dename Authors.
//
// 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 main

import (
	"crypto/rand"
	"github.com/agl/ed25519"
	. "github.com/andres-erbsen/dename/protocol"
	"os"
)

func main() {
	pk, sk, err := ed25519.GenerateKey(rand.Reader)
	if err != nil {
		panic(err)
	}
	if _, err := os.Stderr.Write(sk[:]); err != nil {
		panic(err)
	}
	if _, err := os.Stdout.Write(PBEncode(&Profile_PublicKey{Ed25519: pk[:]})); err != nil {
		panic(err)
	}
}

// Copyright 2014 The Dename Authors.
//
// 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 main

import (
	"crypto/rand"
	"github.com/agl/ed25519"
	. "github.com/andres-erbsen/dename/protocol"
	"fmt"
	"os"
)

func main() {
	pk, sk, err := ed25519.GenerateKey(rand.Reader)
	if err != nil {
		fmt.Fprintf(os.Stderr, "unable to create ed25519 keys")
		os.Exit(1)
	}
	if _, err := os.Stderr.Write(sk[:]); err != nil {
		fmt.Fprintf(os.Stderr, "unable to write secret key")
		os.Exit(1)
	}
	if _, err := os.Stdout.Write(PBEncode(&Profile_PublicKey{Ed25519: pk[:]})); err != nil {
		fmt.Fprintf(os.Stderr, "unable to write public key")
		os.Exit(1)
	}
}

Use os.Exit() in order to make this tool more usable for scripts, than panic() does.


package core

import (
	"fmt"
)

type sharedStateSink struct {
	writer Writer
}

// NewSharedStateSink creates a sink that writes to SharedState.
func NewSharedStateSink(ctx *Context, name string) (Sink, error) {
	registry := ctx.SharedStates

	// Get SharedState by name
	state, err := registry.Get(name)
	if err != nil {
		return nil, err
	}

	// It fails if the shared state cannot be written
	writer, ok := state.(Writer)
	if !ok {
		return nil, fmt.Errorf("'%v' state cannot be written")
	}

	s := &sharedStateSink{
		writer: writer,
	}
	return s, nil
}

func (s *sharedStateSink) Write(ctx *Context, t *Tuple) error {
	return s.writer.Write(ctx, t)
}

func (s *sharedStateSink) Close(ctx *Context) error {
	closer, ok := s.writer.(WriteCloser)
	if !ok {
		return nil
	}
	return closer.Close(ctx)
}

package core

import (
	"fmt"
)

// writer points a shared state. sharedStateSink will point to the same shared state
// even after the state is removed from the context.
type sharedStateSink struct {
	writer Writer
}

// NewSharedStateSink creates a sink that writes to SharedState.
func NewSharedStateSink(ctx *Context, name string) (Sink, error) {
	registry := ctx.SharedStates

	// Get SharedState by name
	state, err := registry.Get(name)
	if err != nil {
		return nil, err
	}

	// It fails if the shared state cannot be written
	writer, ok := state.(Writer)
	if !ok {
		return nil, fmt.Errorf("'%v' state cannot be written")
	}

	s := &sharedStateSink{
		writer: writer,
	}
	return s, nil
}

func (s *sharedStateSink) Write(ctx *Context, t *Tuple) error {
	return s.writer.Write(ctx, t)
}

func (s *sharedStateSink) Close(ctx *Context) error {
	closer, ok := s.writer.(WriteCloser)
	if !ok {
		return nil
	}
	return closer.Close(ctx)
}

Add godoc comment to sharedStateSink type


package daemon

import (
	"fmt"

	"github.com/docker/docker/container"
	"github.com/docker/docker/libcontainerd"
)

// platformConstructExitStatus returns a platform specific exit status structure
func platformConstructExitStatus(e libcontainerd.StateInfo) *container.ExitStatus {
	return &container.ExitStatus{
		ExitCode: int(e.ExitCode),
	}
}

// postRunProcessing perfoms any processing needed on the container after it has stopped.
func (daemon *Daemon) postRunProcessing(container *container.Container, e libcontainerd.StateInfo) error {
	if e.UpdatePending {
		spec, err := daemon.createSpec(container)
		if err != nil {
			return err
		}

		servicingOption := &libcontainerd.ServicingOption{
			IsServicing: true,
		}

		// Create a new servicing container, which will start, complete the update, and merge back the
		// results if it succeeded, all as part of the below function call.
		if err := daemon.containerd.Create((container.ID + "_servicing"), *spec, servicingOption); err != nil {
			return fmt.Errorf("Post-run update servicing failed: %s", err)
		}
	}
	return nil
}

package daemon

import (
	"fmt"

	"github.com/docker/docker/container"
	"github.com/docker/docker/libcontainerd"
)

// platformConstructExitStatus returns a platform specific exit status structure
func platformConstructExitStatus(e libcontainerd.StateInfo) *container.ExitStatus {
	return &container.ExitStatus{
		ExitCode: int(e.ExitCode),
	}
}

// postRunProcessing perfoms any processing needed on the container after it has stopped.
func (daemon *Daemon) postRunProcessing(container *container.Container, e libcontainerd.StateInfo) error {
	if e.ExitCode == 0 && e.UpdatePending {
		spec, err := daemon.createSpec(container)
		if err != nil {
			return err
		}

		servicingOption := &libcontainerd.ServicingOption{
			IsServicing: true,
		}

		// Create a new servicing container, which will start, complete the update, and merge back the
		// results if it succeeded, all as part of the below function call.
		if err := daemon.containerd.Create((container.ID + "_servicing"), *spec, servicingOption); err != nil {
			container.ExitCode = -1
			return fmt.Errorf("Post-run update servicing failed: %s", err)
		}
	}
	return nil
}

Fix postRunProcessing behavior during docker build


// +build !linux

package lxc

func finalizeNamespace(args *InitArgs) error {
	panic("Not supported on this platform")
}

// +build !linux

package lxc

// InitArgs contains args provided to the init function for a driver
type InitArgs struct {
}

func finalizeNamespace(args *InitArgs) error {
	panic("Not supported on this platform")
}

Fix go test in execdriver\lxc


package server

import (
	"time"
)

const (
	ServerVersion                 = "0.2"
	MDBInitialSize                = 1048576
	TwoToTheSixtyThree            = 9223372036854775808
	SubmissionInitialAttempts     = 5
	SubmissionInitialBackoff      = 2 * time.Microsecond
	SubmissionMaxSubmitDelay      = 2 * time.Second
	VarIdleTimeoutMin             = 50 * time.Millisecond
	VarIdleTimeoutRange           = 250
	FrameLockMinExcessSize        = 100
	FrameLockMinRatio             = 2
	ConnectionRestartDelayRangeMS = 5000
	ConnectionRestartDelayMin     = 3 * time.Second
	MostRandomByteIndex           = 7 // will be the lsb of a big-endian client-n in the txnid.
	MigrationBatchElemCount       = 64
)

package server

import (
	"time"
)

const (
	ServerVersion                 = "dev"
	MDBInitialSize                = 1048576
	TwoToTheSixtyThree            = 9223372036854775808
	SubmissionInitialAttempts     = 5
	SubmissionInitialBackoff      = 2 * time.Microsecond
	SubmissionMaxSubmitDelay      = 2 * time.Second
	VarIdleTimeoutMin             = 50 * time.Millisecond
	VarIdleTimeoutRange           = 250
	FrameLockMinExcessSize        = 100
	FrameLockMinRatio             = 2
	ConnectionRestartDelayRangeMS = 5000
	ConnectionRestartDelayMin     = 3 * time.Second
	MostRandomByteIndex           = 7 // will be the lsb of a big-endian client-n in the txnid.
	MigrationBatchElemCount       = 64
)

Put server version back to dev.


package main

import (
	"sync"
	"text/template"
)

// Per IP address rate limit in seconds
const rateLimitSeconds = 15

var templates = template.Must(template.ParseFiles("tmpl/header.tmpl", "tmpl/footer.tmpl", "tmpl/homepage.tmpl", "tmpl/results.tmpl", "tmpl/checkForm.tmpl"))

var rateLimit map[string]int64
var rateLimitMux sync.Mutex

type affectedStages struct {
	Chrome39, Chrome40, Chrome41 chromeWarnings
	Expiry                       bool
	SHA1                         bool
	ExpiryDate                   string
	Certificate                  certificate
	RootCertificate              certificate
	Intermediates                []certificate
}

type certificate struct {
	ExpiryDate string
	ValidFor   string
	SigAlg     string
}

type chromeWarnings struct {
	MinorErrors, NoSecurity, Insecure bool
}

// Pages

type ResultsPage struct {
	PageTitle,
	ServerName string
	Affected affectedStages
}

package main

import (
	"sync"
	"text/template"
)

// Per IP address rate limit in seconds
const rateLimitSeconds = 3

var templates = template.Must(template.ParseFiles("tmpl/header.tmpl", "tmpl/footer.tmpl", "tmpl/homepage.tmpl", "tmpl/results.tmpl", "tmpl/checkForm.tmpl"))

var rateLimit map[string]int64
var rateLimitMux sync.Mutex

type affectedStages struct {
	Chrome39, Chrome40, Chrome41 chromeWarnings
	Expiry                       bool
	SHA1                         bool
	ExpiryDate                   string
	Certificate                  certificate
	RootCertificate              certificate
	Intermediates                []certificate
}

type certificate struct {
	ExpiryDate string
	ValidFor   string
	SigAlg     string
}

type chromeWarnings struct {
	MinorErrors, NoSecurity, Insecure bool
}

// Pages

type ResultsPage struct {
	PageTitle,
	ServerName string
	Affected affectedStages
}

Decrease rate limit from 15 seconds to 3 seconds per IP


package main

import (
	"os"
	"os/exec"
)

// docker runs docker command
func docker(args ...string) error {
	cmd := exec.Command("docker", args...)
	cmd.Stderr = os.Stdout // ioutil.Discard
	cmd.Stdout = os.Stdout // ioutil.Discard

	if err := cmd.Run(); err != nil {
		return err
	}

	return nil
}

package main

import (
	"io"
	"io/ioutil"
	"os/exec"
)

type Docker struct {
	OutStream io.Writer
	Discard   bool
}

// docker runs docker command
func (d *Docker) execute(args ...string) error {
	cmd := exec.Command("docker", args...)
	cmd.Stderr = d.OutStream
	cmd.Stdout = d.OutStream

	if d.Discard {
		cmd.Stderr = ioutil.Discard
		cmd.Stdout = ioutil.Discard
	}

	if err := cmd.Run(); err != nil {
		return err
	}

	return nil
}

Allow to output to Discard


package bucket
/*
 *  Filename:    bucket.go
 *  Package:     bucket
 *  Author:      Bryan Matsuo <bmatsuo@soe.ucsc.edu>
 *  Created:     Sat Jul 30 19:48:17 PDT 2011
 *  Description:
 */
import ()

type Elem struct {
    id    int
    Value interface{}
}

type Bucket struct {
    held   []bool
    elems  []Elem
    bucket chan int
}

func New(n int) *Bucket {
    b := new(Bucket)
    b.bucket = make(chan int, n)
    b.elems = make([]Elem, n)
    b.held = make([]bool, n)
    return b
}

func (b *Bucket) Size() int {
    return len(b.elems)
}

func (b *Bucket) Retain() (int, interface{}) {
    if i, ok := <-b.bucket; ok {
        if b.held[i] {
            panic("held")
        }
        b.held[i] = true
        return i, b.elems[i].Value
    }
    panic("closed")
}

func (b *Bucket) Release(i int) {
    if !b.held[i] {
        panic("free")
    }
    b.bucket<-i
    b.held[i] = false
}

package bucket
/*
 *  Filename:    bucket.go
 *  Package:     bucket
 *  Author:      Bryan Matsuo <bmatsuo@soe.ucsc.edu>
 *  Created:     Sat Jul 30 19:48:17 PDT 2011
 *  Description:
 */
import ()

type Elem struct {
    id    int
    Value interface{}
}

type Bucket struct {
    held   []bool
    elems  []Elem
    bucket chan int
}

func New(n int) *Bucket {
    b := new(Bucket)
    b.elems = make([]Elem, n)
    b.held = make([]bool, n)
    b.bucket = make(chan int, n)
    for i := 0; i < n; i++ {
        b.bucket<-i
    }
    return b
}

func (b *Bucket) Init(f func(int) interface{}) {
    for _, p := range b.held {
        if p {
            panic("held")
        }
    }
    for i, _ := range b.elems {
        b.elems[i] = Elem{i, f(i)}
    }
}

func (b *Bucket) Size() int {
    return len(b.elems)
}

func (b *Bucket) Retain() (int, interface{}) {
    if i, ok := <-b.bucket; ok {
        if b.held[i] {
            panic("held")
        }
        b.held[i] = true
        return i, b.elems[i].Value
    }
    panic("closed")
}

func (b *Bucket) Release(i int) {
    if !b.held[i] {
        panic("free")
    }
    b.bucket <- i
    b.held[i] = false
}

Add initialization to the Bucket object.


package triton

import (
	"fmt"
	"time"

	"github.com/mitchellh/multistep"
	"github.com/mitchellh/packer/packer"
)

// StepDeleteMachine deletes the machine with the ID specified in state["machine"]
type StepDeleteMachine struct{}

func (s *StepDeleteMachine) Run(state multistep.StateBag) multistep.StepAction {
	driver := state.Get("driver").(Driver)
	ui := state.Get("ui").(packer.Ui)

	machineId := state.Get("machine").(string)

	ui.Say("Deleting source machine...")
	err := driver.DeleteMachine(machineId)
	if err != nil {
		state.Put("error", fmt.Errorf("Problem deleting source machine: %s", err))
		return multistep.ActionHalt
	}

	ui.Say("Waiting for source machine to be deleted...")
	err = driver.WaitForMachineDeletion(machineId, 10*time.Minute)
	if err != nil {
		state.Put("error", fmt.Errorf("Problem waiting for source machine to be deleted: %s", err))
		return multistep.ActionHalt
	}

	return multistep.ActionContinue
}

func (s *StepDeleteMachine) Cleanup(state multistep.StateBag) {
	// No clean up to do here...
}

package triton

import (
	"fmt"
	"time"

	"github.com/mitchellh/multistep"
	"github.com/mitchellh/packer/packer"
)

// StepDeleteMachine deletes the machine with the ID specified in state["machine"]
type StepDeleteMachine struct{}

func (s *StepDeleteMachine) Run(state multistep.StateBag) multistep.StepAction {
	driver := state.Get("driver").(Driver)
	ui := state.Get("ui").(packer.Ui)

	machineId := state.Get("machine").(string)

	ui.Say("Deleting source machine...")
	err := driver.DeleteMachine(machineId)
	if err != nil {
		state.Put("error", fmt.Errorf("Problem deleting source machine: %s", err))
		return multistep.ActionHalt
	}

	ui.Say("Waiting for source machine to be deleted...")
	err = driver.WaitForMachineDeletion(machineId, 10*time.Minute)
	if err != nil {
		state.Put("error", fmt.Errorf("Problem waiting for source machine to be deleted: %s", err))
		return multistep.ActionHalt
	}

	state.Put("machine", "")

	return multistep.ActionContinue
}

func (s *StepDeleteMachine) Cleanup(state multistep.StateBag) {
	// No clean up to do here...
}

Remove the machine id from state when the machine is deleted