package main

import (
	"github.com/asdine/storm"
	"github.com/labstack/echo"

	"github.com/genesor/cochonou/http"
	"github.com/genesor/cochonou/os"
)

func main() {
	e := echo.New()

	db, err := storm.Open(os.GetEnvWithDefault("COCH_BOLT_DB_PATH", "cochonou_dev.db"))
	if err != nil {
		panic(err.Error())
	}

	defer db.Close()

	helloHandler := http.NewHelloHandler()
	e.GET("/", helloHandler.HandleHello)
	e.Logger.Fatal(e.Start(os.GetEnvWithDefault("COCH_HTTP_ADDR", ":9494")))
}

package main

import (
	"github.com/asdine/storm"
	"github.com/labstack/echo"

	"github.com/genesor/cochonou/http"
	"github.com/genesor/cochonou/os"
)

func main() {
	e := echo.New()

	db, err := storm.Open(os.GetEnvWithDefault("BOLT_DB_PATH", "cochonou_dev.db"))
	if err != nil {
		panic(err.Error())
	}

	defer db.Close()

	helloHandler := http.NewHelloHandler()
	e.GET("/", helloHandler.HandleHello)
	e.Logger.Fatal(e.Start(os.GetEnvWithDefault("HTTP_ADDR", ":9494")))
}

Remove COCH prefix from env vars


package main

import (
	"bufio"
	"flag"
	"fmt"
	"io/ioutil"
	"misc/calc/eval"
	"os"
)

var version = "0.1"

func main() {
	flag.Parse()
	if flag.NArg() >= 1 {
		data, err := ioutil.ReadFile(flag.Arg(0))
		if err != nil {
			fmt.Println(err)
		}
		eval.EvalExpr(string(data))
	} else {
		fmt.Println("Welcome to Calc REPL", version)

		for {
			fmt.Print(">>>")
			in := bufio.NewReader(os.Stdin)
			str, _ := in.ReadString('\n')
			if str[:len(str)-2] == "q" {
				fmt.Println("QUIT!")
				break
			}
			res := eval.EvalExpr(str)
			if res != nil {
				fmt.Println(res)
			}
		}
	}
}

package main

import (
	"bufio"
	"flag"
	"fmt"
	"io/ioutil"
	"misc/calc/eval"
	"os"
)

var version = "0.1"

func stripCR(in []byte) []byte {
	out := make([]byte, len(in))
	i := 0
	for _, ch := range in {
		if ch != '\r'{
			out[i] = ch
			i++
		}
	}
	return out[:i]
}

func main() {
	flag.Parse()
	if flag.NArg() >= 1 {
		data, err := ioutil.ReadFile(flag.Arg(0))
		if err != nil {
			fmt.Println(err)
		}
		eval.EvalExpr(string(stripCR(data)))
	} else {
		fmt.Println("Welcome to Calc REPL", version)

		for {
			fmt.Print(">>>")
			in := bufio.NewReader(os.Stdin)
			b, _ := in.ReadBytes('\n')
			b = stripCR(b)
			if len(b) <= 2 && b[0] == 'q' {
				fmt.Println("QUIT!")
				break
			}
			res := eval.EvalExpr(string(b))
			if res != nil {
				fmt.Println(res)
			}
		}
	}
}

Fix for line endings on all platforms in REPL


package main

import (
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
)

func rootVolPath() string {
	volumes, err := filepath.Glob("/Volumes/*")
	if err != nil || volumes == nil {
		log.Println("Unable to list /Volumes/*")
		log.Fatal(err)
	}

	for _, volume := range volumes {
		link_path, err := os.Readlink(volume)
		if err == nil && link_path == "/" {
			return volume
		}
	}

	log.Fatal("Could not find root volume")
	return ""
}

func volPath(target string) string {
	var err error

	target, err = filepath.Abs(target)
	if err != nil {
		log.Fatal(err)
	}
	target = filepath.Clean(target)

	target_list := strings.Split(target, string(filepath.Separator))
	if len(target_list) >= 2 &&
		target_list[0] == "" &&
		target_list[1] == "Volumes" {
		return target
	}

	return filepath.Join(rootVolPath(), target)
}

func getTMDir() (dirname string, err error) {
	dirname_bytes, err := exec.Command("tmutil", "machinedirectory").CombinedOutput()
	if err != nil {
		dirname = filepath.Join(strings.TrimSpace(string(dirname_bytes)), "Latest")
	}
	return
}

package main

import (
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
)

func rootVolPath() string {
	volumes, err := filepath.Glob("/Volumes/*")
	if err != nil || volumes == nil {
		log.Println("Unable to list /Volumes/*")
		log.Fatal(err)
	}

	for _, volume := range volumes {
		link_path, err := os.Readlink(volume)
		if err == nil && link_path == "/" {
			return volume
		}
	}

	log.Fatal("Could not find root volume")
	return ""
}

func volPath(target string) string {
	var err error

	target, err = filepath.Abs(target)
	if err != nil {
		log.Fatal(err)
	}
	target = filepath.Clean(target)

	target_list := strings.Split(target, string(filepath.Separator))
	if len(target_list) >= 2 &&
		target_list[0] == "" &&
		target_list[1] == "Volumes" {
		return target
	}

	return filepath.Join(rootVolPath(), target)
}

func getTMDir() (dirname string, err error) {
	dirname_bytes, err := exec.Command("tmutil", "machinedirectory").CombinedOutput()
	if err == nil {
		dirname = filepath.Join(strings.TrimSpace(string(dirname_bytes)), "Latest")
	}
	return
}

Fix bug in time machine logic


package main

import (
	_ "expvar"
	"net/http"
	"os"

	"github.com/bmizerany/pat"
	"github.com/codegangsta/negroni"
	"github.com/meatballhat/negroni-logrus"
	"github.com/unrolled/render"
)

func main() {
	Run()
}

func Run() {
	m := pat.New()
	n := negroni.New(negroni.NewRecovery(), negroni.NewStatic(http.Dir("assets")))
	l := negronilogrus.NewMiddleware()
	r := render.New(render.Options{
		Layout: "layout",
	})

	n.Use(l)
	n.UseHandler(m)

	m.Get("/debug/vars", http.DefaultServeMux)

	m.Get("/", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		r.HTML(w, http.StatusOK, "index", "world")
	}))

	var addr string
	if len(os.Getenv("PORT")) > 0 {
		addr = ":" + os.Getenv("PORT")
	} else {
		addr = ":3000"
	}

	l.Logger.Infof("Listening on %s", addr)
	l.Logger.Fatal(http.ListenAndServe(addr, n))
}

package main

import (
	_ "expvar"
	"net/http"
	"os"

	"github.com/bmizerany/pat"
	"github.com/codegangsta/negroni"
	"github.com/meatballhat/negroni-logrus"
	"github.com/unrolled/render"
)

func main() {
	Run()
}

func Run() {
	m := pat.New()
	n := negroni.New(negroni.NewRecovery(), negroni.NewStatic(http.Dir("assets")))
	l := negronilogrus.NewMiddleware()
	o := render.New(render.Options{
		Layout: "layout",
	})

	n.Use(l)
	n.UseHandler(m)

	m.Get("/debug/vars", http.DefaultServeMux)

	m.Get("/", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		o.HTML(w, http.StatusOK, "index", "world")
	}))

	var addr string
	if len(os.Getenv("PORT")) > 0 {
		addr = ":" + os.Getenv("PORT")
	} else {
		addr = ":3000"
	}

	l.Logger.Infof("Listening on %s", addr)
	l.Logger.Fatal(http.ListenAndServe(addr, n))
}

Rename `r` variable to `o`


package main

import (
	"fmt"
	"os"
	"io/ioutil"
	"flag"
	"log"
)

var (
	elim = flag.String("e", "", "Eliminate this (one-character) function.")
)

func main() {
	flag.Parse()
	var input []byte
	if flag.NArg() == 0 {
		var err os.Error
		input, err = ioutil.ReadAll(os.Stdin)
		if err != nil {
			log.Exitln(err)
		}
	} else {
		file, err := os.Open(flag.Arg(0), os.O_RDONLY, 0)
		if err != nil {
			log.Exitln(err)
		}
		defer file.Close()
		input, err = ioutil.ReadAll(file)
		if err != nil {
			log.Exitln(err)
		}
	}
	input = strip(input)
	if !valid(input) {
		log.Exitln("Syntax error.")
	}
	switch len(*elim) {
	case 0:
		fmt.Println(parse(input))
	case 1:
		fmt.Println(eliminate(dumbParse(input), char((*elim)[0])))
	default:
		log.Exitln("Argument to -e should be one-character.")
	}
}

package main

import (
	"fmt"
	"os"
	"io/ioutil"
	"flag"
	"log"
)

var (
	elim = flag.String("e", "", "Eliminate this (one-character) function.")
)

func main() {
	flag.Parse()
	var input []byte
	if flag.NArg() == 0 {
		var err os.Error
		input, err = ioutil.ReadAll(os.Stdin)
		if err != nil {
			log.Exitln(err)
		}
	} else {
		file, err := os.Open(flag.Arg(0), os.O_RDONLY, 0)
		if err != nil {
			log.Exitln(err)
		}
		defer file.Close()
		input, err = ioutil.ReadAll(file)
		if err != nil {
			log.Exitln(err)
		}
	}
	input = strip(input)
	if !valid(input) {
		log.Exitln("Syntax error.")
	}
	if len(*elim) == 0 {
		fmt.Println(parse(input))
	} else {
		input = []byte(parse(input).String())  //First we simplify.
		for i := len(*elim) - 1; i >= 0; i-- { //Iterate backwards.
			input = []byte(eliminate(dumbParse(input), char((*elim)[i])).String())
			input = []byte(parse(input).String()) //Simplify after every step.
		}
		fmt.Println(string(input))
	}
}

Deal with multiple vars to eliminate.


package main

import "C"
import (
	"github.com/unigornel/go-tcpip/ethernet"
	"github.com/unigornel/go-tcpip/icmp"
	"github.com/unigornel/go-tcpip/ipv4"
)

func main() {}

//export Main
func Main(unused int) {
	sourceIP := [4]byte{10, 0, 100, 2}

	nic := ethernet.NewNIC()
	eth := ethernet.NewLayer(nic)
	arp := ipv4.NewARP(nic.GetMAC(), sourceIP, eth)
	ip := ipv4.NewLayer(sourceIP, arp, eth)
	icmp.NewLayer(ip)

	nic.Start()

	m := make(chan int)
	m <- 0
}

package main

import "C"
import (
	"fmt"
	"net"

	"github.com/unigornel/go-tcpip/ethernet"
	"github.com/unigornel/go-tcpip/icmp"
	"github.com/unigornel/go-tcpip/ipv4"
)

func main() {}

var ipAddress string

//export Main
func Main(unused int) {
	if ipAddress == "" {
		ipAddress = "10.0.100.2"
		fmt.Printf("[*] warning: using default IP address (%v)\n", ipAddress)
	} else {
		fmt.Printf("[+] using IP address %v\n", ipAddress)
	}

	parseIP := net.ParseIP(ipAddress)
	if parseIP != nil {
		parseIP = parseIP.To4()
	}
	if parseIP == nil {
		panic("invalid IPv4 address")
	}

	var sourceIP ipv4.Address
	for i := 0; i < 4; i++ {
		sourceIP[i] = parseIP[i]
	}

	nic := ethernet.NewNIC()
	eth := ethernet.NewLayer(nic)
	arp := ipv4.NewARP(nic.GetMAC(), sourceIP, eth)
	ip := ipv4.NewLayer(sourceIP, arp, eth)
	icmp.NewLayer(ip)

	nic.Start()

	fmt.Printf("[+] network is ready")

	m := make(chan int)
	m <- 0
}

Use a ping test ip address that can be set at build time


package main

import "github.com/tomlanyon/toy-gobot/robot"

func main() {
	r := robot.NewRobot(0,5,0,5)
	r.Dump()
}

package main

import "os"
import "fmt"
import "github.com/tomlanyon/toy-gobot/robot"

func main() {
	r := robot.NewRobot(0,5,0,5)

	err := r.Place(1,1,robot.South)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	err = r.Move()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	r.Left()

	err = r.Move()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	err = r.Move()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	r.Report()

	r.Right()

	err = r.Move()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	r.Report()

}

Test new functionality in "robot" cmd tool.


package client

import (
	"github.com/ibrt/go-oauto/oauto/api"
	"net/http"
	"fmt"
	"encoding/json"
	"github.com/go-errors/errors"
	"bytes"
	"io/ioutil"
)

func Authenticate(baseURL string, request *api.AuthenticateRequest) (*api.AuthenticateResponse, error) {
	body, err := json.Marshal(request)
	if err != nil {
		return nil, errors.Wrap(err, 0)
	}

	resp, err := http.Post(fmt.Sprintf("%v/api/authenticate", baseURL), "application/json", bytes.NewBuffer(body))
	if err != nil {
		return nil, errors.Wrap(err, 0)
	}

	respBytes, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, errors.Wrap(err, 0)
	}

	if resp.StatusCode != http.StatusOK {
		return nil, errors.Errorf("Authenticate request failed with status %v: '%+v'.", resp.StatusCode, respBytes)
	}

	authResp := &api.AuthenticateResponse{}
	if err := json.Unmarshal(respBytes, &authResp); err != nil {
		return nil, errors.WrapPrefix(err, string(respBytes), 0)
	}

	return authResp, nil
}

package client

import (
	"github.com/ibrt/go-oauto/oauto/api"
	"net/http"
	"fmt"
	"encoding/json"
	"github.com/go-errors/errors"
	"bytes"
	"io/ioutil"
)

func Authenticate(baseURL string, request *api.AuthenticateRequest) (*api.AuthenticateResponse, error) {
	body, err := json.Marshal(request)
	if err != nil {
		return nil, errors.Wrap(err, 0)
	}

	resp, err := http.Post(fmt.Sprintf("%v/api/authenticate", baseURL), "application/json", bytes.NewBuffer(body))
	if err != nil {
		return nil, errors.Wrap(err, 0)
	}

	respBytes, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, errors.Wrap(err, 0)
	}

	if resp.StatusCode != http.StatusOK {
		return nil, errors.Errorf("Authenticate request failed with status %v: '%s'.", resp.StatusCode, respBytes)
	}

	authResp := &api.AuthenticateResponse{}
	if err := json.Unmarshal(respBytes, &authResp); err != nil {
		return nil, errors.WrapPrefix(err, string(respBytes), 0)
	}

	return authResp, nil
}

Improve error reporting in client.


package dockerguard

type DockerInfo struct {
	// Infos from docker api /version
	APIVersion    string `json:"ApiVersion"`
	Arch          string `json:"Arch"`
	Experimental  bool   `json:"Experimental"`
	GitCommit     string `json:"GitCommit"`
	GoVersion     string `json:"GoVersion"`
	KernelVersion string `json:"KernelVersion"`
	Os            string `json:"Os"`
	Version       string `json:"Version"`

	// Infos from docker api /info
	ID              string `json:"ID"`
	Name            string `json:"Name"`
	Containers      int    `json:"Containers"`
	Images          int    `json:"Images"`
	Driver          string `json:"Driver"`
	SystemTime      string `json:"SystemTime"`
	OperatingSystem string `json:"OperatingSystem"`
	NCPU            int    `json:"NCPU"`
	MemTotal        int    `json:"MemTotal"`
}

type Container struct {
	ID         string  `json:"Id"`
	Hostname   string  `json:"Hostname"`
	Image      string  `json:"Image"`
	IPAddress  string  `json:"IPAddress"`
	MacAddress string  `json:"MacAddress"`
	SizeRootFs float64 `json:"SizeRootFs"`
	SizeRw     float64 `json:"SizeRw"`
	MemoryUsed float64 `json:"MemoryUsed"`
	Running    bool    `json:"Running"`
	Time       float64 `json:"Time"`
}

package dockerguard

type DockerInfo struct {
	// Infos from docker api /version
	APIVersion    string `json:"ApiVersion"`
	Arch          string `json:"Arch"`
	Experimental  bool   `json:"Experimental"`
	GitCommit     string `json:"GitCommit"`
	GoVersion     string `json:"GoVersion"`
	KernelVersion string `json:"KernelVersion"`
	Os            string `json:"Os"`
	Version       string `json:"Version"`

	// Infos from docker api /info
	ID              string `json:"ID"`
	Name            string `json:"Name"`
	Containers      int    `json:"Containers"`
	Images          int    `json:"Images"`
	Driver          string `json:"Driver"`
	SystemTime      string `json:"SystemTime"`
	OperatingSystem string `json:"OperatingSystem"`
	NCPU            int    `json:"NCPU"`
	MemTotal        int    `json:"MemTotal"`
}

type Container struct {
	ID          string  `json:"Id"`
	Hostname    string  `json:"Hostname"`
	Image       string  `json:"Image"`
	IPAddress   string  `json:"IPAddress"`
	MacAddress  string  `json:"MacAddress"`
	SizeRootFs  float64 `json:"SizeRootFs"`
	SizeRw      float64 `json:"SizeRw"`
	MemoryUsed  float64 `json:"MemoryUsed"`
	NetBandwith float64 `json:"NetBandwith"`
	CPUUsage    float64 `json:"CPUUsage"`
	Running     bool    `json:"Running"`
	Time        float64 `json:"Time"`
}

Add NetBandwith & CPUUsage in Container struct


package medtronic

import (
	"fmt"
)

const (
	bolus Command = 0x42

	maxBolus = 25000 // milliUnits
)

// Bolus delivers the given amount of insulin as a bolus.
// For safety, this command is not attempted more than once.
func (pump *Pump) Bolus(amount Insulin) {
	if amount < 0 {
		pump.SetError(fmt.Errorf("bolus amount (%d) is negative", amount))
	}
	if amount > maxBolus {
		pump.SetError(fmt.Errorf("bolus amount (%d) is too large", amount))
	}
	newer := pump.Family() >= 23
	strokes := int(amount / milliUnitsPerStroke(newer))
	n := pump.Retries()
	defer pump.SetRetries(n)
	pump.SetRetries(1)
	switch newer {
	case true:
		pump.Execute(bolus, marshalUint16(uint16(strokes))...)
	case false:
		pump.Execute(bolus, uint8(strokes))
	}
}

package medtronic

import (
	"fmt"
)

const (
	bolus Command = 0x42

	maxBolus = 25000 // milliUnits
)

// Bolus delivers the given amount of insulin as a bolus.
// For safety, this command is not attempted more than once.
func (pump *Pump) Bolus(amount Insulin) {
	if amount < 0 {
		pump.SetError(fmt.Errorf("bolus amount (%d) is negative", amount))
	}
	if amount > maxBolus {
		pump.SetError(fmt.Errorf("bolus amount (%d) is too large", amount))
	}
	newer := pump.Family() >= 23
	d := milliUnitsPerStroke(newer)
	if amount%d != 0 {
		pump.SetError(fmt.Errorf("bolus (%d) is not a multiple of %d milliUnits per hour", amount, d))
		return
	}
	strokes := int(amount / d)
	n := pump.Retries()
	defer pump.SetRetries(n)
	pump.SetRetries(1)
	switch newer {
	case true:
		pump.Execute(bolus, marshalUint16(uint16(strokes))...)
	case false:
		pump.Execute(bolus, uint8(strokes))
	}
}

Enforce exact multiple of milliUnits/stroke in Bolus function


package ws

import (
	"code.google.com/p/go.net/websocket"
	"time"
)

type Connection struct {
	RawConn          *websocket.Conn
	ReceivedMessages []string
	Timeout          time.Duration
	unreadMessages   []string
}

func newConnection(conn *websocket.Conn) *Connection {
	connection := &Connection{
		RawConn: conn,
		Timeout: 1 * time.Second,
	}
	return connection
}

func (connection *Connection) FlushMessages(number int) *TimeoutError {
	for i := 0; i < number; i++ {
		_, err := connection.ReceiveMessage()
		if err != nil {
			return err
		}
	}
	return nil
}

func (connection *Connection) ReceiveMessage() (string, *TimeoutError) {
	messageChan := make(chan string)

	go connection.receiveMessage(messageChan)

	select {
	case <-time.After(connection.Timeout):
		return "", &TimeoutError{}
	case message := <-messageChan:
		connection.ReceivedMessages = append(connection.ReceivedMessages, message)
		return message, nil
	}

	return "", nil
}

func (connection *Connection) SendMessage(message string) {
	websocket.Message.Send(connection.RawConn, message)
}

func (connection *Connection) receiveMessage(messageChan chan string) {
	var message string
	websocket.Message.Receive(connection.RawConn, &message)
	messageChan <- message
}

package ws

import (
	"code.google.com/p/go.net/websocket"
	"time"
)

type Connection struct {
	RawConn          *websocket.Conn
	ReceivedMessages []string
	Timeout          time.Duration
}

func newConnection(conn *websocket.Conn) *Connection {
	connection := &Connection{
		RawConn: conn,
		Timeout: 1 * time.Second,
	}
	return connection
}

func (connection *Connection) FlushMessages(number int) *TimeoutError {
	for i := 0; i < number; i++ {
		_, err := connection.ReceiveMessage()
		if err != nil {
			return err
		}
	}
	return nil
}

func (connection *Connection) ReceiveMessage() (string, *TimeoutError) {
	messageChan := make(chan string)

	go connection.receiveMessage(messageChan)

	select {
	case <-time.After(connection.Timeout):
		return "", &TimeoutError{}
	case message := <-messageChan:
		connection.ReceivedMessages = append(connection.ReceivedMessages, message)
		return message, nil
	}

	return "", nil
}

func (connection *Connection) SendMessage(message string) {
	websocket.Message.Send(connection.RawConn, message)
}

func (connection *Connection) receiveMessage(messageChan chan string) {
	var message string
	websocket.Message.Receive(connection.RawConn, &message)
	messageChan <- message
}

Remove unnecessary unreadMessages attribute on ws.Connection.


package lb

import (
	"math/rand"
	"time"
)

// Implement a load balancer that given a set of requests distributes those
// requests to an arbitrary number of weighted backends

type Backend struct {
	Name    string
	Weight  int
	Handled int
}

type LoadBalancer struct {
	w map[int]*Backend
}

func NewLoadBalancer(backends []*Backend) *LoadBalancer {
	rand.Seed(time.Now().UnixNano())
	lb := &LoadBalancer{w: make(map[int]*Backend)}
	i := 0
	for _, b := range backends {
		for j := 0; j < b.Weight; j++ {
			lb.w[i] = b
			i++
		}
	}
	return lb
}

func (lb *LoadBalancer) Next() *Backend {
	b, ok := lb.w[rand.Intn(len(lb.w)-1)]
	b.Handled++
	return b
}

package lb

import (
	"math/rand"
	"time"
)

// Implement a load balancer that given a set of requests distributes those
// requests to an arbitrary number of weighted backends

type Backend struct {
	Name    string
	Weight  int
	Handled int
}

type LoadBalancer struct {
	w map[int]*Backend
}

func NewLoadBalancer(backends []*Backend) *LoadBalancer {
	rand.Seed(time.Now().UnixNano())
	lb := &LoadBalancer{w: make(map[int]*Backend)}
	i := 0
	for _, b := range backends {
		for j := 0; j < b.Weight; j++ {
			lb.w[i] = b
			i++
		}
	}
	return lb
}

func (lb *LoadBalancer) Next() *Backend {
	b := lb.w[rand.Intn(len(lb.w)-1)]
	b.Handled++
	return b
}

Remove ok assertion in loadbalancer weight map lookup


// Copyright 2015, David Howden
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package index

import (
	"encoding/json"
	"io"
	"os"
)

// PersistStore is a type which defines a simple persistence store.
type PersistStore string

// NewPersistStore creates a new PersistStore.  By default PersistStore uses
// JSON to persist data.
func NewPersistStore(path string, data interface{}) (PersistStore, error) {
	f, err := os.Open(path)
	if err != nil {
		if !os.IsNotExist(err) {
			return "", err
		}
		f, err = os.Create(path)
		if err != nil {
			return "", err
		}
	}
	defer f.Close()

	dec := json.NewDecoder(f)
	err = dec.Decode(data)
	if err != nil && err != io.EOF {
		return "", err
	}
	return PersistStore(path), nil
}

func (p PersistStore) Persist(data interface{}) error {
	f, err := os.Create(string(p))
	if err != nil {
		return err
	}
	defer f.Close()

	b, err := json.Marshal(data)
	if err != nil {
		return err
	}

	_, err = f.Write(b)
	return err
}

// Copyright 2015, David Howden
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package index

import (
	"encoding/json"
	"io"
	"os"
)

// PersistStore is a type which defines a simple persistence store.
type PersistStore string

// NewPersistStore creates a new PersistStore.  By default PersistStore uses
// JSON to persist data.
func NewPersistStore(path string, data interface{}) (PersistStore, error) {
	f, err := os.Open(path)
	if err != nil {
		if !os.IsNotExist(err) {
			return "", err
		}
		f, err = os.Create(path)
		if err != nil {
			return "", err
		}
	}
	defer f.Close()

	dec := json.NewDecoder(f)
	err = dec.Decode(data)
	if err != nil && err != io.EOF {
		return "", err
	}
	return PersistStore(path), nil
}

// Persist writes the data to the underlying data store, overwriting any previous data.
func (p PersistStore) Persist(data interface{}) error {
	f, err := os.Create(string(p))
	if err != nil {
		return err
	}
	defer f.Close()

	b, err := json.Marshal(data)
	if err != nil {
		return err
	}

	_, err = f.Write(b)
	return err
}

Add godoc to Persist method.


/*
Copyright 2021 The Tekton 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 v1beta1

import (
	"context"
	"fmt"

	"github.com/tektoncd/pipeline/pkg/apis/config"
	"knative.dev/pkg/apis"
)

// ValidateEnabledAPIFields checks that the enable-api-fields feature gate is set
// to the wantVersion value and, if not, returns an error stating which feature
// is dependent on the version and what the current version actually is.
func ValidateEnabledAPIFields(ctx context.Context, featureName, wantVersion string) *apis.FieldError {
	currentVersion := config.FromContextOrDefaults(ctx).FeatureFlags.EnableAPIFields
	if currentVersion != wantVersion {
		var errs *apis.FieldError
		message := fmt.Sprintf(`%s requires "enable-api-fields" feature gate to be %q but it is %q`, featureName, wantVersion, currentVersion)
		return errs.Also(apis.ErrGeneric(message, "workspaces"))
	}
	var errs *apis.FieldError = nil
	return errs
}

/*
Copyright 2021 The Tekton 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 v1beta1

import (
	"context"
	"fmt"

	"github.com/tektoncd/pipeline/pkg/apis/config"
	"knative.dev/pkg/apis"
)

// ValidateEnabledAPIFields checks that the enable-api-fields feature gate is set
// to the wantVersion value and, if not, returns an error stating which feature
// is dependent on the version and what the current version actually is.
func ValidateEnabledAPIFields(ctx context.Context, featureName, wantVersion string) *apis.FieldError {
	currentVersion := config.FromContextOrDefaults(ctx).FeatureFlags.EnableAPIFields
	if currentVersion != wantVersion {
		var errs *apis.FieldError
		message := fmt.Sprintf(`%s requires "enable-api-fields" feature gate to be %q but it is %q`, featureName, wantVersion, currentVersion)
		return errs.Also(apis.ErrGeneric(message))
	}
	var errs *apis.FieldError = nil
	return errs
}

Remove field name from error returned during feature gate validation


// +build linux solaris

package sys

import (
	"syscall"
	"time"
)

// StatAtime returns the Atim
func StatAtime(st *syscall.Stat_t) syscall.Timespec {
	return st.Atim
}

// StatCtime returns the Ctim
func StatCtime(st *syscall.Stat_t) syscall.Timespec {
	return st.Ctim
}

// StatMtime returns the Mtim
func StatMtime(st *syscall.Stat_t) syscall.Timespec {
	return st.Mtim
}

// StatATimeAsTime returns st.Atim as a time.Time
func StatATimeAsTime(st *syscall.Stat_t) time.Time {
	return time.Unix(st.Atim.Sec, st.Atim.Nsec)
}

// +build linux solaris

package sys

import (
	"syscall"
	"time"
)

// StatAtime returns the Atim
func StatAtime(st *syscall.Stat_t) syscall.Timespec {
	return st.Atim
}

// StatCtime returns the Ctim
func StatCtime(st *syscall.Stat_t) syscall.Timespec {
	return st.Ctim
}

// StatMtime returns the Mtim
func StatMtime(st *syscall.Stat_t) syscall.Timespec {
	return st.Mtim
}

// StatATimeAsTime returns st.Atim as a time.Time
func StatATimeAsTime(st *syscall.Stat_t) time.Time {
	return time.Unix(int64(st.Atim.Sec), int64(st.Atim.Nsec)) // nolint: unconvert
}

Add time type conversion for 32bit Unix platforms


package main

import (
	"bufio"
	"fmt"
	"log"
	"net"
)

const PORT = "2525"

func main() {
	server := createServer(PORT)

	defer server.Close()

	for {
		conn, err := server.Accept()

		if err != nil {
			log.Fatalf("Error accepting: %v\n", err)
		}

		go handleConnection(conn)
	}
}

func createServer(port string) (net.Listener) {
	server, err := net.Listen("tcp", ":" + PORT)

	if err != nil {
		log.Fatalf("Error listening to port %s\n", PORT)
	}

	return server
}

func handleConnection(conn net.Conn) {
	defer conn.Close()

	conn.Write([]byte("220 OK\n"))

	buffer := bufio.NewReader(conn)

	for {
		str, _ := buffer.ReadString('\n')

		if str == "DATA\r\n" {
			break
		}

		conn.Write([]byte("250 OK\n"))
	}

	conn.Write([]byte("354 OK\n"))

	buffer = bufio.NewReader(conn)

	for {
		str, _ := buffer.ReadString('\n')

		if str == ".\r\n" {
			break
		}

		fmt.Printf(str)
	}

	conn.Write([]byte("250 OK\n"))
	conn.Write([]byte("221 OK\n"))
}

package main

import (
	"bufio"
	"fmt"
	"log"
	"net"
)

const PORT = "2525"

func main() {
	server := createServer(PORT)

	defer server.Close()

	for {
		conn, err := server.Accept()

		if err != nil {
			log.Fatalf("Error accepting: %v\n", err)
		}

		go handleConnection(conn)
	}
}

func createServer(port string) (net.Listener) {
	server, err := net.Listen("tcp", ":" + PORT)

	if err != nil {
		log.Fatalf("Error listening to port %s\n", PORT)
	}

	return server
}

func handleConnection(conn net.Conn) {
	defer conn.Close()

	conn.Write([]byte("220 OK\n"))

	buffer := bufio.NewReader(conn)

	for {
		str, _ := buffer.ReadString('\n')

		if str == "DATA\r\n" {
			break
		}

		conn.Write([]byte("250 OK\n"))
	}

	conn.Write([]byte("354 OK\n"))

	buffer = bufio.NewReader(conn)

	for {
		str, _ := buffer.ReadString('\n')

		if str == ".\r\n" {
			break
		}

		fmt.Print(str)
	}

	conn.Write([]byte("250 OK\n"))
	conn.Write([]byte("221 OK\n"))
}

Use Print instead of Printf.


package image

import (
	"github.com/Symantec/Dominator/lib/hash"
	"github.com/Symantec/Dominator/lib/objectserver"
)

func (image *Image) listObjects() []hash.Hash {
	hashes := make([]hash.Hash, 0, image.FileSystem.NumRegularInodes+2)
	image.forEachObject(func(hashVal hash.Hash) error {
		hashes = append(hashes, hashVal)
		return nil
	})
	return hashes
}

func (image *Image) listMissingObjects(
	objectsChecker objectserver.ObjectsChecker) ([]hash.Hash, error) {
	// TODO(rgooch): Implement an API that avoids copying hash lists.
	hashes := image.ListObjects()
	objectSizes, err := objectsChecker.CheckObjects(hashes)
	if err != nil {
		return nil, err
	}
	var missingObjects []hash.Hash
	for index, size := range objectSizes {
		if size < 1 {
			missingObjects = append(missingObjects, hashes[index])
		}
	}
	return missingObjects, nil
}

package image

import (
	"github.com/Symantec/Dominator/lib/hash"
	"github.com/Symantec/Dominator/lib/objectserver"
)

func (image *Image) listObjects() []hash.Hash {
	hashes := make([]hash.Hash, 0, image.FileSystem.NumRegularInodes+2)
	image.forEachObject(func(hashVal hash.Hash) error {
		hashes = append(hashes, hashVal)
		return nil
	})
	return hashes
}

func (image *Image) listMissingObjects(
	objectsChecker objectserver.ObjectsChecker) ([]hash.Hash, error) {
	hashBuffer := make([]hash.Hash, 1024)
	var missingObjects []hash.Hash
	index := 0
	image.forEachObject(func(hashVal hash.Hash) error {
		hashBuffer[index] = hashVal
		index++
		if index < len(hashBuffer) {
			return nil
		}
		var err error
		missingObjects, err = listMissingObjects(missingObjects, hashBuffer,
			objectsChecker)
		if err != nil {
			return err
		}
		index = 0
		return nil
	})
	return listMissingObjects(missingObjects, hashBuffer[:index],
		objectsChecker)
}

func listMissingObjects(missingObjects, hashes []hash.Hash,
	objectsChecker objectserver.ObjectsChecker) ([]hash.Hash, error) {
	objectSizes, err := objectsChecker.CheckObjects(hashes)
	if err != nil {
		return nil, err
	}
	for index, size := range objectSizes {
		if size < 1 {
			missingObjects = append(missingObjects, hashes[index])
		}
	}
	return missingObjects, nil
}

Reduce transient memory consumption of lib/image.Image.ListMissingObjects().


package dsbldr

import (
	"fmt"
)

// BasicOAuthHeader spits out a basic OAuth Header based on access token
func BasicOAuthHeader(consumerKey, nonce, signature, signatureMethod,
	timestamp, token string) string {
	return fmt.Sprintf(`OAuth oauth_consumer_key="%s",
		oauth_nonce="%s",
		oauth_signature="%s",
		oauth_signature_method="%s",
		oauth_timestamp="%s",
		oauth_token="%s`,
		consumerKey, nonce, signature, signatureMethod, timestamp, token)
}

func writeStringColumn(data *[][]string, columnName string, values []string) {
	var colIndex int
	for i := range (*data)[0] {
		// Find first empty column
		if (*data)[0][i] == "" {
			colIndex = i
			(*data)[0][i] = columnName
			break
		}
	}
	// Add all the values as well (remember that Builder.data is pre-allocated)
	for i := 1; i < len(*data); i++ {
		(*data)[i][colIndex] = values[i-1]
	}
}

package dsbldr

import (
	"fmt"
)

// BasicOAuthHeader spits out a basic OAuth Header based on access token
func BasicOAuthHeader(consumerKey, nonce, signature, signatureMethod,
	timestamp, token string) string {
	return fmt.Sprintf(`OAuth oauth_consumer_key="%s",
		oauth_nonce="%s",
		oauth_signature="%s",
		oauth_signature_method="%s",
		oauth_timestamp="%s",
		oauth_token="%s`,
		consumerKey, nonce, signature, signatureMethod, timestamp, token)
}

func writeStringColumn(data *[][]string, columnName string, values []string) {
	var colIndex int
	for i := range (*data)[0] {
		// Find first empty column or column with same header to overwrite
		if (*data)[0][i] == "" || (*data)[0][i] == columnName {
			colIndex = i
			(*data)[0][i] = columnName
			break
		}
	}
	// Add all the values as well (remember that Builder.data is pre-allocated)
	for i := 1; i < len(*data); i++ {
		(*data)[i][colIndex] = values[i-1]
	}
}

Modify utli.go writeStringColumn to overwrite existing columns


// Gockle simplifies and mocks github.com/gocql/gocql.
//
// Gockle provides the basic abilities to insert, query, and mutate data, as
// well as access to basic keyspace and table metadata.
//
// The entry point is NewSession or NewSimpleSession. Call it to get a Session.
//
// A Session is the connection to the database and the primary means to access
// it. It has all the query methods and the means to iterate result rows and
// batch together mutations. The Session implementation simply wraps
// gocql.Session and adapts a few things to have a simpler interface.
//
// Closing the Session closes the underlying gocql.Session, including the one
// passed in with NewSimpleSession.
//
// Mocks are provided for testing use of Batch, Iterator, and Session.
package gockle

// Package gockle simplifies and mocks github.com/gocql/gocql.
//
// Gockle provides the basic abilities to insert, query, and mutate data, as
// well as access to basic keyspace and table metadata.
//
// The entry point is NewSession or NewSimpleSession. Call it to get a Session.
//
// A Session is the connection to the database and the primary means to access
// it. It has all the query methods and the means to iterate result rows and
// batch together mutations. The Session implementation simply wraps
// gocql.Session and adapts a few things to have a simpler interface.
//
// Closing the Session closes the underlying gocql.Session, including the one
// passed in with NewSimpleSession.
//
// Mocks are provided for testing use of Batch, Iterator, and Session.
package gockle

Fix to use "Package gockle..." wording


package octokit

import (
	"github.com/lostisland/go-sawyer"
	"net/url"
)

type M map[string]interface{}

type Hyperlink string

// TODO: find out a way to not wrapping sawyer.Hyperlink like this
func (l *Hyperlink) Expand(m M) (u *url.URL, err error) {
	link := sawyer.Hyperlink(string(*l))
	sawyerM := sawyer.M{}
	for k, v := range m {
		sawyerM[k] = v
	}

	u, err = link.Expand(sawyerM)
	return
}

package octokit

import (
	"github.com/jtacoma/uritemplates"
	"net/url"
)

// TODO: use sawyer.Hyperlink

type M map[string]interface{}

type Hyperlink string

func (l *Hyperlink) Expand(m M) (u *url.URL, err error) {
	template, e := uritemplates.Parse(string(*l))
	if e != nil {
		err = e
		return
	}

	expanded, e := template.Expand(m)
	if e != nil {
		err = e
		return
	}

	u, err = url.Parse(expanded)
	return
}

Revert "Use sawyer.Hyperlink and sawyer.M"


package client

import (
	"net/url"
	"strconv"

	"github.com/docker/docker/api/types/swarm"
	"golang.org/x/net/context"
)

// SecretUpdate updates a Secret. Currently, the only part of a secret spec
// which can be updated is Labels.
func (cli *Client) SecretUpdate(ctx context.Context, id string, version swarm.Version, secret swarm.SecretSpec) error {
	query := url.Values{}
	query.Set("version", strconv.FormatUint(version.Index, 10))
	resp, err := cli.post(ctx, "/secrets/"+id+"/update", query, secret, nil)
	ensureReaderClosed(resp)
	return err
}

package client

import (
	"net/url"
	"strconv"

	"github.com/docker/docker/api/types/swarm"
	"golang.org/x/net/context"
)

// SecretUpdate attempts to updates a Secret
func (cli *Client) SecretUpdate(ctx context.Context, id string, version swarm.Version, secret swarm.SecretSpec) error {
	query := url.Values{}
	query.Set("version", strconv.FormatUint(version.Index, 10))
	resp, err := cli.post(ctx, "/secrets/"+id+"/update", query, secret, nil)
	ensureReaderClosed(resp)
	return err
}

Add integration test for stack deploy with secrets.


package sarama

import "encoding/binary"

// LengthField implements the PushEncoder and PushDecoder interfaces for calculating 4-byte lengths.
type lengthField struct {
	startOffset int
}

func (l *lengthField) saveOffset(in int) {
	l.startOffset = in
}

func (l *lengthField) reserveLength() int {
	return 4
}

func (l *lengthField) run(curOffset int, buf []byte) error {
	binary.BigEndian.PutUint32(buf[l.startOffset:], uint32(curOffset-l.startOffset-4))
	return nil
}

func (l *lengthField) check(curOffset int, buf []byte) error {
	if uint32(curOffset-l.startOffset-4) != binary.BigEndian.Uint32(buf[l.startOffset:]) {
		return PacketDecodingError{"length field invalid"}
	}

	return nil
}

type varintLengthField struct {
	startOffset int
	length      int64
}

func newVarintLengthField(pd packetDecoder) (*varintLengthField, error) {
	n, err := pd.getVarint()
	if err != nil {
		return nil, err
	}
	return &varintLengthField{length: n}, nil
}

func (l *varintLengthField) saveOffset(in int) {
	l.startOffset = in
}

func (l *varintLengthField) reserveLength() int {
	return 0
}

func (l *varintLengthField) check(curOffset int, buf []byte) error {
	if int64(curOffset-l.startOffset) != l.length {
		return PacketDecodingError{"length field invalid"}
	}

	return nil
}

Add support for length fields encoded as varints.


// errchk $G $D/$F.go

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Used to crash the compiler.
// http://code.google.com/p/go/issues/detail?id=158

package main

type A struct {
	a A;
}			// ERROR "recursive"
func foo()		{ new(A).bar() }
func (a A) bar()	{}

// errchk $G $D/$F.go

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Used to crash the compiler.
// http://code.google.com/p/go/issues/detail?id=158

package main

type A struct {	a A }	// ERROR "recursive"
func foo()		{ new(A).bar() }
func (a A) bar()	{}

Tweak test to work with both 6g and gccgo.


package stager

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

type backend struct {
	proxy *httputil.ReverseProxy
}

type backendManager struct {
	backends     map[string]*backend
	suffixLength int
}

func (m backendManager) get(domain string) *backend {
	suffix := domain[:-m.suffixLength]
	u, _ := url.Parse("http://www.example.com/" + suffix)
	if m.backends[suffix] == nil {
		fmt.Println("making new suffix %s", suffix)
		m.backends[suffix] = &backend{
			proxy: httputil.NewSingleHostReverseProxy(u),
		}
	}
	return m.backends[suffix]
}

func newBackendManager(config *Configuration) backendManager {
	manager := backendManager{
		backends:     *new(map[string]*backend),
		suffixLength: len(config.DomainSuffix),
	}
	return manager
}

package stager

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

type backend struct {
	port    int
	proxy   *httputil.ReverseProxy
	running bool
}

// This will do the setup of the backend at some point.
func (b *backend) initialize() {

}

type backendManager struct {
	backends     map[string]*backend
	suffixLength int
	currentPort  int
	availPorts   []int
}

func (m *backendManager) get(domain string) *backend {
	name := domain[:len(domain)-m.suffixLength]
	if m.backends[name] == nil {
		port := m.allocatePort()
		fmt.Printf("making new instance %s on port %d\n", name, port)
		u, _ := url.Parse("http://www.example.com/" + name)
		b := m.backends[name] = &backend{
			port:  port,
			proxy: httputil.NewSingleHostReverseProxy(u),
		}
		go b.initialize()

	}
	return m.backends[name]
}

// Ick, this is very old school and probably not concurrency friendly. But I need something now.
func (m *backendManager) allocatePort() int {
	l := len(m.availPorts)
	if l > 0 {
		port := m.availPorts[l-1]
		m.availPorts = m.availPorts[:l-1]
		return port
	} else {
		port := m.currentPort
		m.currentPort += 1
		return port
	}
}

func newBackendManager(config *Configuration) *backendManager {
	manager := &backendManager{
		backends:     make(map[string]*backend),
		suffixLength: len(config.DomainSuffix),
		currentPort:  config.BasePort,
	}
	return manager
}

Make a very basic allocation of backend ports


package client

import (
	"github.com/summerwind/h2spec/config"
	"github.com/summerwind/h2spec/spec"
	"golang.org/x/net/http2"
)

func StreamIdentifiers() *spec.ClientTestGroup {
	tg := NewTestGroup("5.1.1", "Stream Identifiers")

	// An endpoint that receives an unexpected stream identifier
	// MUST respond with a connection error (Section 5.4.1) of
	// type PROTOCOL_ERROR.
	tg.AddTestCase(&spec.ClientTestCase{
		Desc:        "Sends even-numbered stream identifier",
		Requirement: "The endpoint MUST respond with a connection error of type PROTOCOL_ERROR.",
		Run: func(c *config.Config, conn *spec.Conn) error {
			err := conn.Handshake()
			if err != nil {
				return err
			}

			headers := spec.CommonRespHeaders(c)
			hp := http2.HeadersFrameParam{
				StreamID:      101,
				EndStream:     true,
				EndHeaders:    true,
				BlockFragment: conn.EncodeHeaders(headers),
			}
			conn.WriteHeaders(hp)

			return spec.VerifyConnectionError(conn, http2.ErrCodeProtocol)
		},
	})

	return tg
}

package client

import (
	"github.com/summerwind/h2spec/config"
	"github.com/summerwind/h2spec/spec"
	"golang.org/x/net/http2"
)

func StreamIdentifiers() *spec.ClientTestGroup {
	tg := NewTestGroup("5.1.1", "Stream Identifiers")

	// An endpoint that receives an unexpected stream identifier
	// MUST respond with a connection error (Section 5.4.1) of
	// type PROTOCOL_ERROR.
	tg.AddTestCase(&spec.ClientTestCase{
		Desc:        "Sends odd-numbered stream identifier",
		Requirement: "The endpoint MUST respond with a connection error of type PROTOCOL_ERROR.",
		Run: func(c *config.Config, conn *spec.Conn) error {
			err := conn.Handshake()
			if err != nil {
				return err
			}

			headers := spec.CommonRespHeaders(c)
			hp := http2.HeadersFrameParam{
				StreamID:      101,
				EndStream:     true,
				EndHeaders:    true,
				BlockFragment: conn.EncodeHeaders(headers),
			}
			conn.WriteHeaders(hp)

			return spec.VerifyConnectionError(conn, http2.ErrCodeProtocol)
		},
	})

	return tg
}

Correct a client test description


package imageproxy

import (
	"bytes"
	"encoding/gob"
)

type Image struct {
	Type string
	Data []byte
}

func (image *Image) serialize() (serialized []byte, err error) {
	buffer := bytes.NewBuffer([]byte{})
	encoder := gob.NewEncoder(buffer)
	err = encoder.Encode(image)
	if err != nil {
		return
	}
	serialized = buffer.Bytes()
	return
}

func (image *Image) unserialize(data []byte) error {
	buffer := bytes.NewBuffer(data)
	decoder := gob.NewDecoder(buffer)
	err := decoder.Decode(image)
	return err
}

package imageproxy

import (
	"bytes"
	"encoding/gob"
)

type Image struct {
	Type string
	Data []byte
}

func (image *Image) Serialize() (serialized []byte, err error) {
	buffer := bytes.NewBuffer([]byte{})
	encoder := gob.NewEncoder(buffer)
	err = encoder.Encode(image)
	if err != nil {
		return
	}
	serialized = buffer.Bytes()
	return
}

func (image *Image) Unserialize(data []byte) error {
	buffer := bytes.NewBuffer(data)
	decoder := gob.NewDecoder(buffer)
	err := decoder.Decode(image)
	return err
}

Make public methods in Image


// Copyright 2019 The Ebiten 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 event

import (
	"github.com/hajimehoshi/ebiten/internal/driver"
)

type Modifier int

const (
	ModifierShift    Modifier = Modifier(driver.ModifierShift)
	ModifierControl  Modifier = Modifier(driver.ModifierControl)
	ModifierAlt      Modifier = Modifier(driver.ModifierAlt)
	ModifierCapsLock Modifier = Modifier(driver.ModifierCapsLock)
	ModifierNumLock  Modifier = Modifier(driver.ModifierNumLock)
)

// Copyright 2019 The Ebiten 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 event

import (
	"github.com/hajimehoshi/ebiten/internal/driver"
)

// Modifier is a bit set of modifier keys on a keyboard.
type Modifier int

const (
	ModifierShift    Modifier = Modifier(driver.ModifierShift)
	ModifierControl  Modifier = Modifier(driver.ModifierControl)
	ModifierAlt      Modifier = Modifier(driver.ModifierAlt)
	ModifierCapsLock Modifier = Modifier(driver.ModifierCapsLock)
	ModifierNumLock  Modifier = Modifier(driver.ModifierNumLock)
)

Add a comment at Modifier


package beam

type Verb uint32

const (
	Ack Verb = iota
	Attach
	Connect
	Error
	File
	Get
	Log
	Ls
	Set
	Spawn
	Start
	Stop
	Watch
)

func (v Verb) String() string {
	switch v {
	case Ack:
		return "Ack"
	case Attach:
		return "Attach"
	case Connect:
		return "Connect"
	case Error:
		return "Error"
	case File:
		return "File"
	case Get:
		return "Get"
	case Log:
		return "Log"
	case Ls:
		return "Ls"
	case Set:
		return "Set"
	case Spawn:
		return "Spawn"
	case Start:
		return "Start"
	case Stop:
		return "Stop"
	case Watch:
		return "Watch"
	}
	return ""
}

Add string representation of verb enum


package kwiscale

import (
	"crypto/md5"
	"fmt"
	"net/http"
	"os"
	"path/filepath"
)

// StaticHandler handle static files handlers. Use App.SetStatic(path) that create the static handler
type staticHandler struct {
	RequestHandler
}

// Use http.FileServer to serve file after adding ETag.
func (s *staticHandler) Get() {
	file := s.Vars["file"]
	file = filepath.Join(s.app.Config.StaticDir, file)

	// control or add etag
	if etag, err := eTag(file); err == nil {
		s.response.Header().Add("ETag", etag)
	}

	fs := http.FileServer(http.Dir(s.app.Config.StaticDir))
	fs.ServeHTTP(s.response, s.request)
}

// Get a etag for the file. It's constuct with a md5 sum of
// <filename> + "." + <modification-time>
func eTag(file string) (string, error) {
	stat, err := os.Stat(file)
	if err != nil {
		return "", err
	}

	s := md5.Sum([]byte(stat.Name() + "." + stat.ModTime().String()))
	return fmt.Sprintf("%x", s), nil
}

package kwiscale

import (
	"crypto/md5"
	"fmt"
	"net/http"
	"os"
	"path/filepath"
)

// StaticHandler handle static files handlers. Use App.SetStatic(path) that create the static handler
type staticHandler struct {
	RequestHandler
}

// Use http.FileServer to serve file after adding ETag.
func (s *staticHandler) Get() {
	file := s.Vars["file"]
	abs, _ := filepath.Abs(s.app.Config.StaticDir)
	file = filepath.Join(abs, file)

	// control or add etag
	if etag, err := eTag(file); err == nil {
		s.response.Header().Add("ETag", etag)
	}

	// create a fileserver for the static dir
	fs := http.FileServer(http.Dir(s.app.Config.StaticDir))
	// stip directory name and serve the file
	http.StripPrefix("/"+filepath.Base(s.app.Config.StaticDir), fs).
		ServeHTTP(s.Response(), s.Request())
}

// Get a etag for the file. It's constuct with a md5 sum of
// <filename> + "." + <modification-time>
func eTag(file string) (string, error) {
	stat, err := os.Stat(file)
	if err != nil {
		return "", err
	}

	s := md5.Sum([]byte(stat.Name() + "." + stat.ModTime().String()))
	return fmt.Sprintf("%x", s), nil
}

Fix prefix problem that made a 404 error


package prober

type Prober interface {
	Probe() error
}

package prober

// Prober defines a type that can be used to run a probe.
type Prober interface {
	Probe() error
}

Add a tiny bit of documentation for lib/prober package.


package cmd

import (
	"github.com/spf13/cobra"

	"vitess.io/vitess/go/cmd/rulesctl/common"
)

func List() *cobra.Command {
	var listOptName string
	var listOptNamesOnly bool
	listCmd := &cobra.Command{
		Use:   "list",
		Short: "Display the rules in the config file",
		Args:  cobra.NoArgs,
	}

	listCmd.Flags().StringVarP(
		&listOptName,
		"name", "n",
		"",
		"Display a named rule (optional)")
	listCmd.Flags().BoolVar(
		&listOptNamesOnly,
		"names-only",
		false,
		"Lists only the names of the rules in the config file")

	listCmd.Run = func(cmd *cobra.Command, args []string) {
		rules := common.GetRules(configFile)

		var out interface{}
		if listOptName == "" {
			if listOptNamesOnly {
				out = []string{}
				for _, r := range rules.CopyUnderlying() {
					out = append(out.([]string), r.Name)
				}
			} else {
				out = rules
			}
		} else {
			out = rules.Find(listOptName)
			if listOptNamesOnly && out != nil {
				out = listOptName
			} else if listOptNamesOnly {
				out = ""
			}
		}

		common.MustPrintJSON(out)
	}

	return listCmd
}

package cmd

import (
	"github.com/spf13/cobra"

	"vitess.io/vitess/go/cmd/rulesctl/common"
)

func List() *cobra.Command {
	var listOptName string
	var listOptNamesOnly bool
	listCmd := &cobra.Command{
		Use:   "list",
		Short: "Display the rules in the config file",
		Args:  cobra.NoArgs,
	}

	listCmd.Flags().StringVarP(
		&listOptName,
		"name", "n",
		"",
		"Display a named rule (optional)")
	listCmd.Flags().BoolVar(
		&listOptNamesOnly,
		"names-only",
		false,
		"Lists only the names of the rules in the config file")

	listCmd.Run = func(cmd *cobra.Command, args []string) {
		rules := common.GetRules(configFile)

		var out interface{}
		if listOptName == "" {
			if listOptNamesOnly {
				out = []string{}
				for _, r := range rules.CopyUnderlying() {
					out = append(out.([]string), r.Name)
				}
			} else {
				out = rules
			}
		} else {
			rule := rules.Find(listOptName)
			if listOptNamesOnly && rule != nil {
				out = listOptName
			} else if listOptNamesOnly {
				out = ""
			} else {
				out = rule
			}
		}

		common.MustPrintJSON(out)
	}

	return listCmd
}

Restructure `List` command to make nil check meaningful


package edn

import . "testing"

func TestEmptyGivesOnlyEOF(t *T) {
	lexer := Lex("")
	token, _ := lexer.Next()

	if token.kind != tEOF {
		t.Error("expecting EOF")
	}
}

// I suspect there's a potential race condition here first since
// the lexer is in a different thread. If `Next()` is called while the lexer
// is still in its main `for{}` loop, `done` could still be `false`
func TestEmptyIsDoneAfterFirstToken(t *T) {
	lexer := Lex("")
	_, done := lexer.Next()

	if !done {
		t.Error("expecting no more tokens")
	}
}

func TestOpenCloseParens(t *T) {
	lexer := Lex("()")

	token, _ := lexer.Next()
	if token.kind != tOpenParen || token.value != "(" {
		t.Error("expecting open parenthesis")
	}

	token, _ = lexer.Next()
	if token.kind != tCloseParen || token.value != ")"  {
		t.Error("expecting close parenthesis")
	}

	token, _ = lexer.Next()
	if token.kind != tEOF {
		t.Error("expecting EOF")
	}
}

package edn

import . "testing"

func assertLexerYieldsCorrectTokens(
	t *T,
	source string,
	types []tokenType,
	values []string) {

	tokens := make([]token, 0)

	for token := range Lex(source).tokens {
		tokens = append(tokens, token)
	}

	if len(tokens) != len(types) {
		t.Errorf("Got %d tokens, expecting %d", len(tokens), len(types))
	}

	for i, actual := range(tokens) {
		expected := token{
			kind: types[i],
			value: values[i],
		}

		if actual != expected {
			t.Errorf("Expecting %#v; actual %#v", expected, actual)
		}
	}
}

func tokens(tokens ...tokenType) []tokenType {
	return tokens
}
func values(values ...string) []string {
	return values
}

func TestEmptyGivesOnlyEOF(t *T) {
	assertLexerYieldsCorrectTokens(t,
		"",
		tokens(tEOF),
		values(""))
}

func TestOpenCloseParens(t *T) {
	assertLexerYieldsCorrectTokens(t,
		"()",
		tokens(tOpenParen, tCloseParen, tEOF),
		values("(", ")", ""))
}

Write test helper for testing lexer


package main

// Split the buffer by '\n' (0x0A) characters, return an byte[][] of
// indicating each metric, and byte[] of the remaining parts of the buffer
func ParseBuffer(buffer []byte) ([][]byte, []byte) {
	metrics := make([][]byte, 0)

	var metricBufferCapacity uint32 = 0xff
	metricBuffer := make([]byte, metricBufferCapacity)

	var metricSize uint32 =  0
	var metricBufferUsage uint32 = 0

	for _, b := range buffer {
		if b == '\n' {

			metrics = append(metrics, metricBuffer[metricBufferUsage - metricSize:metricBufferUsage])
			metricSize = 0;
		} else {

			if metricBufferUsage == metricBufferCapacity {
				newMetricBufferCapacity := (metricBufferCapacity + 1) * 2
				newBuffer := make([]byte, metricBufferCapacity, newMetricBufferCapacity)
				copy(newBuffer, metricBuffer)
				metricBuffer = newBuffer
				metricBufferCapacity = newMetricBufferCapacity
			}

			metricBuffer[metricBufferUsage] = b
			metricSize++
			metricBufferUsage++
		}
	}

	return metrics, metricBuffer[metricBufferUsage - metricSize:metricBufferUsage]
}

package main

// Split the buffer by '\n' (0x0A) characters, return an byte[][] of
// indicating each metric, and byte[] of the remaining parts of the buffer
func ParseBuffer(buffer []byte) ([][]byte, []byte) {
	metrics := make([][]byte, 8)

	var metricBufferCapacity uint = 0xff
	metricBuffer := make([]byte, metricBufferCapacity)

	var metricSize uint =  0
	var metricBufferUsage uint = 0
	var totalMetrics int = 0

	for _, b := range buffer {
		if b == '\n' {

			metrics[totalMetrics] = metricBuffer[metricBufferUsage - metricSize:metricBufferUsage]
			totalMetrics++

			if totalMetrics > cap(metrics) {
				newMetrics  := make([][]byte, cap(metrics), (cap(metrics) + 1) * 2)
				copy(newMetrics, metrics)
				metrics = newMetrics
			}

			metricSize = 0;
		} else {

			if metricBufferUsage == metricBufferCapacity {
				newMetricBufferCapacity := (metricBufferCapacity + 1) * 2
				newBuffer := make([]byte, metricBufferCapacity, newMetricBufferCapacity)
				copy(newBuffer, metricBuffer)
				metricBuffer = newBuffer
				metricBufferCapacity = newMetricBufferCapacity
			}

			metricBuffer[metricBufferUsage] = b
			metricSize++
			metricBufferUsage++
		}
	}

	return metrics[:totalMetrics], metricBuffer[metricBufferUsage - metricSize:metricBufferUsage]
}

Reduce allocations further by not appending


package client

import (
	"sync"

	ss "github.com/Jigsaw-Code/outline-ss-server/shadowsocks"
)

var pool = sync.Pool{
	New: func() interface{} {
		return make([]byte, ss.MaxUDPPacketSize)
	},
}

// newBuffer retrieves a UDP buffer from the pool.
func newUDPBuffer() []byte {
	return pool.Get().([]byte)
}

// freeBuffer returns a UDP buffer to the pool.
func freeUDPBuffer(b []byte) {
	pool.Put(b)
}

package client

import (
	"sync"
)

// maxUDPBufferSize is the maximum UDP packet size in bytes.
const maxUDPBufferSize = 16 * 1024

var pool = sync.Pool{
	New: func() interface{} {
		return make([]byte, maxUDPBufferSize)
	},
}

// newBuffer retrieves a UDP buffer from the pool.
func newUDPBuffer() []byte {
	return pool.Get().([]byte)
}

// freeBuffer returns a UDP buffer to the pool.
func freeUDPBuffer(b []byte) {
	pool.Put(b)
}

Revert UDP buffer size to 16KiB


package newedit

// Elvish code for default bindings, assuming the editor ns as the global ns.
const defaultBindingsElv = `
insert:binding = (binding-map [
  &Ctrl-D=  $commit-eof~
  &Default= $insert:default-handler~
])
`

// vi: set et:

package newedit

// Elvish code for default bindings, assuming the editor ns as the global ns.
const defaultBindingsElv = `
insert:binding = (binding-map [
  &Left=  $move-left~
  &Right= $move-right~

  &Ctrl-Left=  $move-left-word~
  &Ctrl-Right= $move-right-word~
  &Alt-Left=   $move-left-word~
  &Alt-Right=  $move-right-word~
  &Alt-b=      $move-left-word~
  &Alt-f=      $move-right-word~

  &Home= $move-sol~
  &End=  $move-eol~

  &Backspace= $kill-left~
  &Delete=    $kill-right~
  &Ctrl-W=    $kill-left-word~
  &Ctrl-U=    $kill-sol~
  &Ctrl-K=    $kill-eol~

  &Ctrl-D=  $commit-eof~
  &Default= $insert:default-handler~
])
`

// vi: set et:

Add common buffer builtins to the default binding.


package ntdll

import (
	"fmt"
)

type NtStatus uint32

func (s NtStatus) Error() error {
	if s == 0 {
		return nil
	}
	return fmt.Errorf("NtStatus %08x", s)
}

func (s NtStatus) IsSuccess() bool {
	return 0 <= s && s <= 0x3fffffff
}

func (s NtStatus) IsInofmational() bool {
	return 0x40000000 <= s && s <= 0x7FFFFFFF
}

func (s NtStatus) IsWarning() bool {
	return 0x80000000 <= s && s <= 0xBFFFFFFF
}

func (s NtStatus) IsError() bool {
	return 0xC0000000 <= s && s <= 0xFFFFFFFF
}

// FIXME: This needs to be autogenerated. See
// https://msdn.microsoft.com/en-us/library/cc704588.aspx

// select NTSTATUS codes.
const (
	STATUS_SUCCESS                NtStatus = 0x00000000
	STATUS_BUFFER_OVERFLOW                 = 0x80000005
	STATUS_NO_MORE_ENTRIES                 = 0x8000001A
	STATUS_INVALID_PARAMETER               = 0XC000000D
	STATUS_BUFFER_TOO_SMALL                = 0xC0000023
	STATUS_OBJECT_NAME_INVALID             = 0xC0000033
	STATUS_OBJECT_NAME_NOT_FOUND           = 0xC0000034
	STATUS_OBJECT_NAME_COLLISION           = 0xC0000035
	STATUS_OBJECT_PATH_INVALID             = 0xC0000039
	STATUS_OBJECT_PATH_NOT_FOUND           = 0xC000003A
	STATUS_OBJECT_PATH_SYNTAX_BAD          = 0xC000003B
)

Add NtStatus convenience functions and some select constants


package grayt

type Camera interface {
	MakeRay(x, y float64) ray
}

type RectilinearCamera struct {
	screenX, screenY Vect
	screenLoc        Vect
	eyeLoc           Vect
}

func NewRectilinearCamera() *RectilinearCamera {
	return nil
}

func (c *RectilinearCamera) MakeRay(x, y float64) ray {
	return ray{}
}

package grayt

// Cameras produce rays that go from an eye to a virtual screen. The rays
// produced are specified via a coordiate system on the virtual screen.  The
// left side of the virtual screen has x coordinate -1, the right side of the
// virtual screen has coordinate +1. The top of the virtual screen has y
// coordinate v and the bottom of the virtual screen has y coordinate -v (where
// the value of v depends on the aspect ratio of the screen).
type Camera interface {
	MakeRay(x, y float64) ray
}

type RectilinearCamera struct {
	screenX, screenY Vect
	screenLoc        Vect
	eyeLoc           Vect
}

func NewRectilinearCamera() Camera {
	return nil
}

func (c *RectilinearCamera) MakeRay(x, y float64) ray {
	return ray{}
}

Add coordinate system comment to the Camera interface


/* This example demonstrates reading a string from input, rather than a
 * single character. Note that only the 'n' versions of getstr have been
 * implemented in goncurses to ensure buffer overflows won't exist */

package main

import gc "code.google.com/p/goncurses"

func main() {
	stdscr, _ := gc.Init()
	defer gc.End()

	row, col := stdscr.Maxyx()
	msg := "Enter a string: "
	stdscr.Print(row/2, (col-len(msg)-8)/2, msg)

	str, _ := stdscr.GetString(10)
	stdscr.Print(row-2, 0, "You entered: %s", str)

	stdscr.Refresh()
	stdscr.GetChar()
}

/* This example demonstrates reading a string from input, rather than a
 * single character. Note that only the 'n' versions of getstr have been
 * implemented in goncurses to ensure buffer overflows won't exist */

package main

import gc "code.google.com/p/goncurses"

func main() {
	stdscr, _ := gc.Init()
	defer gc.End()

	row, col := stdscr.Maxyx()
	msg := "Enter a string: "
	stdscr.MovePrint(row/2, (col-len(msg)-8)/2, msg)

	str, _ := stdscr.GetString(10)
	stdscr.MovePrint(row-2, 0, "You entered: %s", str)

	stdscr.Refresh()
	stdscr.GetChar()
}

Update form example for MovePrint


package text

import "unicode"

type Query interface {
	Match(string) int
}

// Word matches complete words only.
// The "complete words" of a string s is defined as the result of
// splitting the string on every single Unicode whitespace character.
type Word struct {
	W string
}

func (q Word) Match(s string) int {
	if s == q.W {
		return 0
	}

	sr := []rune(s)
	qr := []rune(q.W)

	if len(sr) < len(qr) {
		return -1
	}

	var nMatch int

	for i := 0; i < len(sr); i++ {
		if nMatch < len(qr) && sr[i] != qr[nMatch] {
			nMatch = 0
			continue
		}

		nMatch++

		if nMatch == len(qr) {
			// Check that any immediately preceding or following
			// characters are spaces.

			next := i + 1
			prev := i - nMatch
			hasNext := len(sr) > next
			hasPrev := i-nMatch >= 0

			if (hasNext && (!unicode.IsSpace(sr[next]) || sr[next] != ',')) || (hasPrev && !unicode.IsSpace(sr[prev])) {
				nMatch = 0
				continue
			}

			return i - nMatch + 1
		}
	}

	return -1
}

package text

import (
	"unicode"
)

type Query interface {
	Match(string) int
}

// Word matches complete words only.
// The "complete words" of a string s is defined as the result of
// splitting the string on every single Unicode whitespace character.
type Word struct {
	W string
}

func (q Word) Match(s string) int {
	if s == q.W {
		return 0
	}

	sr := []rune(s)
	qr := []rune(q.W)

	if len(sr) < len(qr) {
		return -1
	}

	var nMatch int

	for i := 0; i < len(sr); i++ {
		if nMatch < len(qr) && sr[i] != qr[nMatch] {
			nMatch = 0
			continue
		}

		nMatch++

		if nMatch == len(qr) {
			// Check that any immediately preceding or following
			// characters are spaces.

			next := i + 1
			prev := i - nMatch
			hasNext := len(sr) > next
			hasPrev := i-nMatch >= 0

			if hasNext && !(unicode.IsSpace(sr[next]) || sr[next] == ',') {
				nMatch = 0
				continue
			}

			if hasPrev && !(unicode.IsSpace(sr[prev]) || sr[prev] == '\n') {
				nMatch = 0
				continue
			}

			return i - nMatch + 1
		}
	}

	return -1
}

Fix comma and newline text.Word matching


package worker

import (
	"github.com/pachyderm/pachyderm/src/client/pps"
)

// MatchDatum checks if a datum matches a filter.  To match each string in
// filter must correspond match at least 1 datum's Path or Hash. Order of
// filter and data is irrelevant.
func MatchDatum(filter []string, data []*pps.Datum) bool {
	// All paths in request.DataFilters must appear somewhere in the log
	// line's inputs, or it's filtered
	matchesData := true
dataFilters:
	for _, dataFilter := range filter {
		for _, datum := range data {
			if dataFilter == datum.Path || dataFilter == string(datum.Hash) {
				continue dataFilters // Found, move to next filter
			}
		}
		matchesData = false
		break
	}
	return matchesData
}

package worker

import (
	"encoding/hex"
	"github.com/pachyderm/pachyderm/src/client/pps"
)

// MatchDatum checks if a datum matches a filter.  To match each string in
// filter must correspond match at least 1 datum's Path or Hash. Order of
// filter and data is irrelevant.
func MatchDatum(filter []string, data []*pps.Datum) bool {
	// All paths in request.DataFilters must appear somewhere in the log
	// line's inputs, or it's filtered
	matchesData := true
dataFilters:
	for _, dataFilter := range filter {
		for _, datum := range data {
			if dataFilter == datum.Path || dataFilter == hex.Dump(datum.Hash) {
				continue dataFilters // Found, move to next filter
			}
		}
		matchesData = false
		break
	}
	return matchesData
}

Convert datum hash to hex before comparing it with request filter


package client

import (
	"testing"

	"github.com/akamai/AkamaiOPEN-edgegrid-golang/jsonhooks-v1"
	"github.com/stretchr/testify/assert"
)

type Test struct {
	Resource
	Foo string `json:"foo"`
}

func (test *Test) PreMarshalJSON() error {
	test.Foo = "bat"

	return nil
}

func TestResourceUnmarshal(t *testing.T) {
	body := []byte(`{"foo":"bar"}`)

	test := &Test{}
	err := jsonhooks.Unmarshal(body, test)

	assert.NoError(t, err)
	assert.True(t, <-test.Complete)
}

func TestResourceMarshal(t *testing.T) {
	test := &Test{Foo: "bar"}

	body, err := jsonhooks.Marshal(test)

	assert.NoError(t, err)
	assert.Equal(t, []byte(`{"foo":"bat"}`), body)
}

Add some basic tests for the base `Resource`


// Copyright 2013 Apcera Inc. All rights reserved.

package graft

import (
	"errors"
)

var (
	ClusterNameErr  = errors.New("grafty: Cluster name can not be empty")
	ClusterSizeErr  = errors.New("grafty: Cluster size can not be 0")
	HandlerReqErr   = errors.New("grafty: Handler is required")
	RpcDriverReqErr = errors.New("grafty: RPCDriver is required")
	LogReqErr       = errors.New("grafty: Log is required")
	LogNoExistErr   = errors.New("grafty: Log file does not exist")
	LogNoStateErr   = errors.New("grafty: Log file does not have any state")
	LogCorruptErr   = errors.New("grafty: Encountered corrupt log file")
	NotImplErr      = errors.New("grafty: Not implemented")
)

// Copyright 2013 Apcera Inc. All rights reserved.

package graft

import (
	"errors"
)

var (
	ClusterNameErr  = errors.New("graft: Cluster name can not be empty")
	ClusterSizeErr  = errors.New("graft: Cluster size can not be 0")
	HandlerReqErr   = errors.New("graft: Handler is required")
	RpcDriverReqErr = errors.New("graft: RPCDriver is required")
	LogReqErr       = errors.New("graft: Log is required")
	LogNoExistErr   = errors.New("graft: Log file does not exist")
	LogNoStateErr   = errors.New("graft: Log file does not have any state")
	LogCorruptErr   = errors.New("graft: Encountered corrupt log file")
	NotImplErr      = errors.New("graft: Not implemented")
)

Move prefix to graft vs grafty


package regressions

import (
	"math"
	"testing"
)

func TestMaxCombinedLogExp(t *testing.T) {
	r := NewCombined(math.Max, NewLogarithmic(), NewExponential())
	err := r.Fit(testDataPoints...)
	if err != nil {
		t.Fatal(err)
	}

	t.Logf("R-squared: %+v", r.GetR2())
	evaluateRegression(t, r)
}

func TestMeanCombinedLogLogExp(t *testing.T) {
	mean := func(x, y float64) float64 {
		return (x + y) / 2
	}

	r := NewCombined(mean, NewLogLog(), NewExponential())
	err := r.Fit(testDataPoints...)
	if err != nil {
		t.Fatal(err)
	}

	t.Logf("R-squared: %+v", r.GetR2())
	evaluateRegression(t, r)
}

Add test for another combination


package commands

import (
	"fmt"

	"github.com/evandbrown/dm/googlecloud"
	"github.com/evandbrown/dm/util"
	"github.com/spf13/cobra"
)

var statCmd = &cobra.Command{
	Use:   "stat",
	Short: "Stat a deployment, listing its resources",
}

func init() {
	statCmd.PreRun = func(cmd *cobra.Command, args []string) {
		requireName()
	}
	statCmd.Run = func(cmd *cobra.Command, args []string) {
		util.Check(stat(cmd, args))
	}
}

func stat(cmd *cobra.Command, args []string) error {
	service, err := googlecloud.GetService()
	util.Check(err)

	call := service.Resources.List(Project, Name)
	resources, error := call.Do()
	util.Check(error)
	for _, r := range resources.Resources {
		fmt.Printf("%s\t%s\n", r.Type, r.Name)
	}
	return nil
}

package commands

import (
	"fmt"
	"os"
	"text/tabwriter"

	"github.com/evandbrown/dm/googlecloud"
	"github.com/evandbrown/dm/util"
	"github.com/spf13/cobra"
)

var statCmd = &cobra.Command{
	Use:   "stat",
	Short: "Stat a deployment, listing its resources",
}

func init() {
	statCmd.PreRun = func(cmd *cobra.Command, args []string) {
		requireName()
	}
	statCmd.Run = func(cmd *cobra.Command, args []string) {
		util.Check(stat(cmd, args))
	}
}

func stat(cmd *cobra.Command, args []string) error {
	service, err := googlecloud.GetService()
	util.Check(err)

	call := service.Resources.List(Project, Name)
	resources, error := call.Do()
	util.Check(error)

	w := new(tabwriter.Writer)
	w.Init(os.Stdout, 0, 8, 2, '\t', 0)
	fmt.Fprintln(w, "Resource Type\tName\t")
	for _, r := range resources.Resources {
		fmt.Fprintf(w, "%s\t%s\t\n", r.Type, r.Name)
	}
	w.Flush()
	return nil
}

Write deployment resources in tab format


package fizzbuzz

const (
	fizz = "Fizz"
	buzz = "Buzz"
)

func Run(limit int) []string {
	v := make([]string, 0)
	for i := 1; i <= limit; i++ {
		threes := i%3 == 0
		fives := i%5 == 0
		switch {
		case threes && fives:
			v = append(v, fizz+buzz)
		case threes:
			v = append(v, fizz)
		case fives:
			v = append(v, buzz)
		}
	}
	return v
}

package fizzbuzz

const (
	fizz = "Fizz"
	buzz = "Buzz"
)

func Run(limit int) []string {
	v := []string{}
	for i := 1; i <= limit; i++ {
		threes := i%3 == 0
		fives := i%5 == 0
		switch {
		case threes && fives:
			v = append(v, fizz+buzz)
		case threes:
			v = append(v, fizz)
		case fives:
			v = append(v, buzz)
		}
	}
	return v
}

Remove unnecessary make to create a slice


package sarama

import (
	"encoding/binary"
	"hash/crc32"
)

// crc32Field implements the pushEncoder and pushDecoder interfaces for calculating CRC32s.
type crc32Field struct {
	startOffset int
}

func (c *crc32Field) saveOffset(in int) {
	c.startOffset = in
}

func (c *crc32Field) reserveLength() int {
	return 4
}

func (c *crc32Field) run(curOffset int, buf []byte) error {
	crc := crc32.ChecksumIEEE(buf[c.startOffset+4 : curOffset])
	binary.BigEndian.PutUint32(buf[c.startOffset:], crc)
	return nil
}

func (c *crc32Field) check(curOffset int, buf []byte) error {
	crc := crc32.ChecksumIEEE(buf[c.startOffset+4 : curOffset])

	if crc != binary.BigEndian.Uint32(buf[c.startOffset:]) {
		return PacketDecodingError{"CRC didn't match"}
	}

	return nil
}

package sarama

import (
	"encoding/binary"

	"github.com/klauspost/crc32"
)

// crc32Field implements the pushEncoder and pushDecoder interfaces for calculating CRC32s.
type crc32Field struct {
	startOffset int
}

func (c *crc32Field) saveOffset(in int) {
	c.startOffset = in
}

func (c *crc32Field) reserveLength() int {
	return 4
}

func (c *crc32Field) run(curOffset int, buf []byte) error {
	crc := crc32.ChecksumIEEE(buf[c.startOffset+4 : curOffset])
	binary.BigEndian.PutUint32(buf[c.startOffset:], crc)
	return nil
}

func (c *crc32Field) check(curOffset int, buf []byte) error {
	crc := crc32.ChecksumIEEE(buf[c.startOffset+4 : curOffset])

	if crc != binary.BigEndian.Uint32(buf[c.startOffset:]) {
		return PacketDecodingError{"CRC didn't match"}
	}

	return nil
}

Use an optimized crc32 library which is faster


package metadata

// Restrict to Linux because, although omreport runs fine on Windows, the
// Windows metadata uses WMI to fetch this information.

import (
	"strings"

	"bosun.org/util"
)

func init() {
	metafuncs = append(metafuncs, collectMetadataOmreport)
}

func collectMetadataOmreport() {
	_ = util.ReadCommand(func(line string) error {
		fields := strings.Split(line, ";")
		if len(fields) != 2 {
			return nil
		}
		switch fields[0] {
		case "Chassis Service Tag":
			AddMeta("", nil, "svctag", fields[1], true)
		case "Chassis Model":
			AddMeta("", nil, "model", fields[1], true)
		}
		return nil
	}, "omreport", "chassis", "info", "-fmt", "ssv")
}

package metadata

// Restrict to Linux because, although omreport runs fine on Windows, the
// Windows metadata uses WMI to fetch this information.

import (
	"strings"

	"bosun.org/util"
)

func init() {
	metafuncs = append(metafuncs, collectMetadataOmreport)
}

func collectMetadataOmreport() {
	_ = util.ReadCommand(func(line string) error {
		fields := strings.Split(line, ";")
		if len(fields) != 2 {
			return nil
		}
		switch fields[0] {
		case "Chassis Service Tag":
			AddMeta("", nil, "serialNumber", fields[1], true)
		case "Chassis Model":
			AddMeta("", nil, "model", fields[1], true)
		}
		return nil
	}, "omreport", "chassis", "info", "-fmt", "ssv")
}

Change dell svctag metakey to be serialNumber


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

package lxc

// SetContainerDir allows tests in other packages to override the
// containerDir.
func SetContainerDir(dir string) (old string) {
	old, containerDir = containerDir, dir
	return
}

// SetLxcContainerDir allows tests in other packages to override the
// lxcContainerDir.
func SetLxcContainerDir(dir string) (old string) {
	old, lxcContainerDir = lxcContainerDir, dir
	return
}

// SetRemovedContainerDir allows tests in other packages to override the
// removedContainerDir.
func SetRemovedContainerDir(dir string) (old string) {
	old, removedContainerDir = removedContainerDir, dir
	return
}

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

// Functions defined in this file should *ONLY* be used for testing.  These
// functions are exported for testing purposes only, and shouldn't be called
// from code that isn't in a test file.

package lxc

// SetContainerDir allows tests in other packages to override the
// containerDir.
func SetContainerDir(dir string) (old string) {
	old, containerDir = containerDir, dir
	return
}

// SetLxcContainerDir allows tests in other packages to override the
// lxcContainerDir.
func SetLxcContainerDir(dir string) (old string) {
	old, lxcContainerDir = lxcContainerDir, dir
	return
}

// SetRemovedContainerDir allows tests in other packages to override the
// removedContainerDir.
func SetRemovedContainerDir(dir string) (old string) {
	old, removedContainerDir = removedContainerDir, dir
	return
}

Add a file level comment.


package main

import (
	"board"
	"color"
	"fmt"
	"point"
)

func scanMove() (*point.Move, error) {
	var file byte
	var rank int

	_, err := fmt.Scanf("%c%d", &file, &rank)
	return point.NewMove(file, rank), err
}

func main() {
	chessboard := board.NewBoard()
	finish := false
	now := color.White

	for finish == false {
		success := false

		chessboard.Print()
		for success == false {
			from, err := scanMove()
			if err != nil {
				finish = true
				break
			}
			to, err := scanMove()
			if err != nil {
				finish = true
				break
			}
			err = chessboard.Move(from.ToPoint(), to.ToPoint(), now)
			success = err == nil
			if success == false {
				fmt.Println(err)
			}
		}
		fmt.Println()
		now = now.Enemy()
	}
}

package main

import (
	"board"
	"color"
	"fmt"
	"point"
)

func scanMove() (*point.Move, error) {
	var file byte
	var rank int

	_, err := fmt.Scanf("%c%d", &file, &rank)
	return point.NewMove(file, rank), err
}

func main() {
	chessboard := board.NewBoard()
	finish := false
	now := color.White

	for finish == false {
		success := false

		chessboard.Print()
		if chessboard.IsChecked(now) {
			fmt.Println("Your king is checked")
		}

		for success == false {
			from, err := scanMove()
			if err != nil {
				finish = true
				break
			}
			to, err := scanMove()
			if err != nil {
				finish = true
				break
			}
			err = chessboard.Move(from.ToPoint(), to.ToPoint(), now)
			success = err == nil
			if success == false {
				fmt.Println(err)
			}
		}
		fmt.Println()
		now = now.Enemy()
	}
}

Print a warning if your king is checked


package main

import (
	"flag"
	"github.com/phss/fcal/calendar"
	"log"
	"time"
)

func main() {
	date := parseOptions()
	c := calendar.CalendarMonthFrom(date)
	calendar.PrintMonth(c)
}

func parseOptions() time.Time {
	var dateStr string
	flag.StringVar(&dateStr, "d", time.Now().Format("2006-01-02"), "ISO date of the day or month to be displayed")
	flag.Parse()

	date, err := time.Parse("2006-01-02", dateStr)

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

	return date
}

package main

import (
	"flag"
	"github.com/phss/fcal/calendar"
	"log"
	"strings"
	"time"
)

func main() {
	date := parseOptions()
	c := calendar.CalendarMonthFrom(date)
	calendar.PrintMonth(c)
}

func parseOptions() time.Time {
	format := "2006-01-02"

	var dateStr string
	flag.StringVar(&dateStr, "d", time.Now().Format(format), "ISO date of the day or month to be displayed")
	flag.Parse()

	if strings.Count(dateStr, "-") == 1 {
		format = "2006-01"
	}

	date, err := time.Parse(format, dateStr)

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

	return date
}

Allow date to be year and month


package middleware

import (
	"time"

	log "github.com/sirupsen/logrus"
	"gopkg.in/gin-gonic/gin.v1"
)

func Logger() gin.HandlerFunc {
	return func(c *gin.Context) {
		requestID, _ := c.Get("request_id")
		logger := log.WithField("request_id", requestID)
		c.Set("logger", logger)

		start := time.Now()
		c.Next()
		end := time.Now()

		method := c.Request.Method
		path := c.Request.URL.Path
		latency := end.Sub(start)
		logger.WithFields(log.Fields{
			"method":    method,
			"path":      path,
			"status":    c.Writer.Status(),
			"client_ip": c.ClientIP(),
			"latency":   latency,
			"bytes":     c.Writer.Size(),
		}).Infof("%s %s", method, path)
	}
}

package middleware

import (
	"time"

	"github.com/sirupsen/logrus"
	"gopkg.in/gin-gonic/gin.v1"
)

func Logger() gin.HandlerFunc {
	return func(c *gin.Context) {
		requestID, _ := c.Get("request_id")
		logger := logrus.WithField("request_id", requestID)
		c.Set("logger", logger)

		start := time.Now()
		c.Next()
		end := time.Now()

		method := c.Request.Method
		path := c.Request.URL.Path
		latency := end.Sub(start)
		logger.WithFields(logrus.Fields{
			"method":    method,
			"path":      path,
			"status":    c.Writer.Status(),
			"client_ip": c.ClientIP(),
			"latency":   latency,
			"bytes":     c.Writer.Size(),
		}).Infof("%s %s", method, path)
	}
}

Use `logrus` instead of `log` in import


package nsinit

import (
	"os"
	"testing"

	. "github.com/smartystreets/goconvey/convey"
	"polydawn.net/repeatr/executor/tests"
	"polydawn.net/repeatr/testutil"
)

func Test(t *testing.T) {
	Convey("Spec Compliance: nsinit Executor", t,
		testutil.Requires(
			testutil.RequiresRoot,
			testutil.WithTmpdir(func() {
				execEng := &Executor{}
				execEng.Configure("nsinit_workspace")
				So(os.Mkdir(execEng.workspacePath, 0755), ShouldBeNil)

				//tests.CheckBasicExecution(execEng) // correct error reporting sections fail spec compliance
				tests.CheckFilesystemContainment(execEng)
				//tests.CheckPwdBehavior(execEng) // correct error reporting sections fail spec compliance
				tests.CheckEnvBehavior(execEng)
			}),
		),
	)
}

package nsinit

import (
	"os"
	"testing"

	. "github.com/smartystreets/goconvey/convey"
	"polydawn.net/repeatr/executor/tests"
	"polydawn.net/repeatr/testutil"
)

func Test(t *testing.T) {
	Convey("Spec Compliance: nsinit Executor", t,
		testutil.Requires(
			testutil.RequiresRoot,
			testutil.RequiresNamespaces,
			testutil.WithTmpdir(func() {
				execEng := &Executor{}
				execEng.Configure("nsinit_workspace")
				So(os.Mkdir(execEng.workspacePath, 0755), ShouldBeNil)

				//tests.CheckBasicExecution(execEng) // correct error reporting sections fail spec compliance
				tests.CheckFilesystemContainment(execEng)
				//tests.CheckPwdBehavior(execEng) // correct error reporting sections fail spec compliance
				tests.CheckEnvBehavior(execEng)
			}),
		),
	)
}

Disable nsinit tests in travis.


package main

import (
	"os/exec"
	"testing"
)

func TestDmesg(t *testing.T) {

	out, err := exec.Command("go", "run", "dmesg.go", "-c").Output()
	if err != nil {
		t.Fatalf("can't run dmesg: %v", err)
	}

	out, err = exec.Command("go", "run", "dmesg.go").Output()
	if err != nil {
		t.Fatalf("can't run dmesg: %v", err)
	}

	if len(out) > 0 {
		t.Fatalf("The log wasn't cleared, got %v", out)
	}
}

// Copyright 2016-2017 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"os/exec"
	"os/user"
	"testing"
)

// Test reading from the buffer.
// dmesg
func TestDmesg(t *testing.T) {
	_, err := exec.Command("go", "run", "dmesg.go").Output()
	if err != nil {
		t.Fatalf("Error running dmesg: %v", err)
	}
	// FIXME: How can the test verify the output is correct?
}

// Test clearing the buffer.
// dmesg -c
func TestClearDmesg(t *testing.T) {
	// Test requies root priviledges or CAP_SYSLOG capability.
	// FIXME: preferably unit tests do not require root priviledges
	if u, err := user.Current(); err != nil {
		t.Fatal("Cannot get current user", err)
	} else if u.Uid != "0" {
		t.Skipf("Test requires root priviledges (uid == 0), uid = %s", u.Uid)
	}

	// Clear
	out, err := exec.Command("go", "run", "dmesg.go", "-c").Output()
	if err != nil {
		t.Fatalf("Error running dmesg -c: %v", err)
	}

	// Read
	out, err = exec.Command("go", "run", "dmesg.go").Output()
	if err != nil {
		t.Fatalf("Error running dmesg: %v", err)
	}

	// Second run of dmesg.go should be cleared.
	// FIXME: This is actually non-determinstic as the system is free (but
	// unlikely) to write more messages inbetween the syscalls.
	if len(out) > 0 {
		t.Fatalf("The log was not cleared, got %v", out)
	}
}

Allow dmesg tests to pass


package logmatic

import (
	"encoding/json"
	"fmt"
	"time"
	log "github.com/Sirupsen/logrus"
	"os"
)

const defaultTimestampFormat = time.RFC3339
var markers = [2]string{"sourcecode", "golang"}

type JSONFormatter struct {
}

func (f *JSONFormatter) Format(entry *log.Entry) ([]byte, error) {

	data := make(log.Fields, len(entry.Data) + 3)
	for k, v := range entry.Data {
		switch v := v.(type) {
		case error:
			// Otherwise errors are ignored by `encoding/json`
			// https://github.com/Sirupsen/logrus/issues/137
			data[k] = v.Error()
		default:
			data[k] = v
		}
	}
	//prefixFieldClashes(data)

	data["date"] = entry.Time.Format(defaultTimestampFormat)
	data["message"] = entry.Message
	data["level"] = entry.Level.String()
	data["@marker"] = markers
	data["appname"] = os.Args[0]
	h, err := os.Hostname()
	if err == nil {
		data["hostname"] = h
	}

	serialized, err := json.Marshal(data)
	if err != nil {
		return nil, fmt.Errorf("Failed to marshal fields to JSON, %v", err)
	}
	return append(serialized, '\n'), nil
}

package logmatic

import (
	"encoding/json"
	"fmt"
	"time"
	log "github.com/Sirupsen/logrus"
)

const defaultTimestampFormat = time.RFC3339
var markers = [2]string{"sourcecode", "golang"}

type JSONFormatter struct {
}

func (f *JSONFormatter) Format(entry *log.Entry) ([]byte, error) {

	data := make(log.Fields, len(entry.Data) + 3)
	for k, v := range entry.Data {
		switch v := v.(type) {
		case error:
			// Otherwise errors are ignored by `encoding/json`
			// https://github.com/Sirupsen/logrus/issues/137
			data[k] = v.Error()
		default:
			data[k] = v
		}
	}
	//prefixFieldClashes(data)

	data["date"] = entry.Time.Format(defaultTimestampFormat)
	data["message"] = entry.Message
	data["level"] = entry.Level.String()
	data["@marker"] = markers

	serialized, err := json.Marshal(data)
	if err != nil {
		return nil, fmt.Errorf("Failed to marshal fields to JSON, %v", err)
	}
	return append(serialized, '\n'), nil
}

Revert "adding source code compatibility"


/*
Copyright 2018 The Skaffold 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 config

import (
	"strings"
)

// SkaffoldOptions are options that are set by command line arguments not included
// in the config file itself
type SkaffoldOptions struct {
	ConfigurationFile string
	Cleanup           bool
	Notification      bool
	Tail              bool
	Profiles          []string
	CustomTag         string
	Namespace         string
}

// Labels returns a map of labels to be applied to all deployed
// k8s objects during the duration of the run
func (opts *SkaffoldOptions) Labels() map[string]string {
	labels := map[string]string{}

	if opts.Cleanup {
		labels["cleanup"] = "true"
	}
	if opts.Namespace != "" {
		labels["namespace"] = opts.Namespace
	}
	if len(opts.Profiles) > 0 {
		labels["profiles"] = strings.Join(opts.Profiles, ",")
	}
	return labels
}

/*
Copyright 2018 The Skaffold 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 config

import (
	"strings"
)

// SkaffoldOptions are options that are set by command line arguments not included
// in the config file itself
type SkaffoldOptions struct {
	ConfigurationFile string
	Cleanup           bool
	Notification      bool
	Tail              bool
	Profiles          []string
	CustomTag         string
	Namespace         string
}

// Labels returns a map of labels to be applied to all deployed
// k8s objects during the duration of the run
func (opts *SkaffoldOptions) Labels() map[string]string {
	labels := map[string]string{}

	if opts.Cleanup {
		labels["cleanup"] = "true"
	}
	if opts.Tail {
		labels["tail"] = "true"
	}
	if opts.Namespace != "" {
		labels["namespace"] = opts.Namespace
	}
	if len(opts.Profiles) > 0 {
		labels["profiles"] = strings.Join(opts.Profiles, ",")
	}
	return labels
}

Add label for tail usage


package main

import (
	"fmt"
	"holux"
)

func main() {
	c, err := holux.Connect()

	if err != nil {
		// TODO LOG
		fmt.Println(err)
		return
	}
	c.Hello()
	defer c.Bye()

	index, err := c.GetIndex()
	if err != nil {
		fmt.Printf("Got error %v, arborting", err)
	}

	for _, row := range index {
		fmt.Println(row)
	}
}

package main

import (
	"fmt"
	"holux"
	"log"
)

func main() {
	c, err := holux.Connect()

	if err != nil {
		log.Println(err)
		return
	}
	c.Hello()
	defer c.Bye()

	index, err := c.GetIndex()
	if err != nil {
		log.Fatalf("Got error %v, arborting", err)
	}

	for _, row := range index {
		fmt.Println(row)
	}
}

Use log for debug printing


// +build !appengine

package main

import (
	"fmt"
	"net/http"
)

func helloWorld(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello World!")
}

func startPage(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, "Hello, test server started on 8080 port.\n - /helloworld - show title page\n - /showinfo - show information about this thing")
}

func showInfo(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, "Inforamtion page for test project.\nLanguage - Go\nPlatform - Google Application Engine")
}

func init() {

	http.HandleFunc("/", startPage)
	http.HandleFunc("/helloworld", helloWorld)
	http.HandleFunc("/showinfo", showInfo)
	http.ListenAndServe(":80", nil)
}

/*
func main() {
	fmt.Println("Hello, test server started on 80 port.\n - /helloworld - show title page\n - /showinfo - show information about this thing")
	http.HandleFunc("/", startPage)
	http.HandleFunc("/helloworld", helloWorld)
	http.HandleFunc("/showinfo", showInfo)
	http.ListenAndServe(":80", nil)
}
*/
//goapp serve app.yaml
//goapp deploy -application golangnode0 -version 0

package main

import (
	"fmt"
	"net/http"
)

func helloWorld(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello World!")
}

func startPage(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, "Hello, test server started on 8080 port.\n - /helloworld - show title page\n - /showinfo - show information about this thing")
}

func showInfo(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, "Inforamtion page for test project.\nLanguage - Go\nPlatform - Google Application Engine")
}

func init() {

	http.HandleFunc("/", startPage)
	http.HandleFunc("/helloworld", helloWorld)
	http.HandleFunc("/showinfo", showInfo)
	//Wrong code for App Enine - server cant understand what it need to show
	//http.ListenAndServe(":80", nil)
}

/*
func main() {
	fmt.Println("Hello, test server started on 80 port.\n - /helloworld - show title page\n - /showinfo - show information about this thing")
	http.HandleFunc("/", startPage)
	http.HandleFunc("/helloworld", helloWorld)
	http.HandleFunc("/showinfo", showInfo)
	http.ListenAndServe(":80", nil)
}
*/
//goapp serve app.yaml
//goapp deploy -application golangnode0 -version 0

Correct version for deploy to GAE


package main

import (
	"bytes"
	"fmt"
	"io"
	"log"
	"os"

	"github.com/ChimeraCoder/gitgo"
)

func main() {
	args := os.Args
	module := args[1]
	switch module {
	case "cat-file":
		hash := args[2]
		result, err := gitgo.CatFile(gitgo.SHA(hash))
		if err != nil {
			log.Fatal(err)
		}
		io.Copy(os.Stdout, result)

	case "log":
		hash := gitgo.SHA(args[2])
		commits, err := gitgo.Log(hash, "")
		if err != nil {
			log.Fatal(err)
		}
		b := bytes.NewBuffer(nil)
		for _, commit := range commits {
			fmt.Fprintf(b, "commit %s\nAuthor: %s\nDate:   %s\n\n    %s\n", commit.Name, commit.Author, commit.AuthorDate.Format(gitgo.RFC2822), bytes.Replace(commit.Message, []byte("\n"), []byte("\n    "), -1))
		}
		io.Copy(os.Stdout, b)
	default:
		log.Fatalf("no such command: %s", module)
	}
}

package main

import (
	"bytes"
	"fmt"
	"io"
	"log"
	"os"

	"github.com/ChimeraCoder/gitgo"
)

func main() {
	args := os.Args
	module := args[1]
	switch module {
	case "cat-file":
		hash := args[2]
		result, err := gitgo.CatFile(gitgo.SHA(hash))
		if err != nil {
			log.Fatal(err)
		}
		io.Copy(os.Stdout, result)

	case "log":
		if len(args) < 3 {
			fmt.Println("must specify commit name with `log`")
			os.Exit(1)
		}
		hash := gitgo.SHA(args[2])
		commits, err := gitgo.Log(hash, "")
		if err != nil {
			log.Fatal(err)
		}
		b := bytes.NewBuffer(nil)
		for _, commit := range commits {
			fmt.Fprintf(b, "commit %s\nAuthor: %s\nDate:   %s\n\n    %s\n", commit.Name, commit.Author, commit.AuthorDate.Format(gitgo.RFC2822), bytes.Replace(commit.Message, []byte("\n"), []byte("\n    "), -1))
		}
		io.Copy(os.Stdout, b)
	default:
		log.Fatalf("no such command: %s", module)
	}
}

Print error message when `git log` is not given a SHA


package main

import (
	"log"
	"net/http"

	"github.com/gorilla/mux"
	"github.com/shurcooL/go/github_flavored_markdown"
)

func DiffHandler(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)

	file := vars["file"] + ".md"

	diff, err := Diff(file, vars["hash"])
	if err != nil {
		log.Println("ERROR", "Failed to get commit hash", vars["hash"])
	}

	// XXX: This could probably be done in a nicer way
	wrapped_diff := []byte("```diff\n" + string(diff) + "```")
	// md := blackfriday.MarkdownCommon(wrapped_diff)
	md := github_flavored_markdown.Markdown(wrapped_diff)

	w.Header().Set("Content-Type", "text/html")
	w.Write(md)
}

package main

import (
	"log"
	"net/http"

	"github.com/gorilla/mux"
	"github.com/shurcooL/github_flavored_markdown"
)

func DiffHandler(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)

	file := vars["file"] + ".md"

	diff, err := Diff(file, vars["hash"])
	if err != nil {
		log.Println("ERROR", "Failed to get commit hash", vars["hash"])
	}

	// XXX: This could probably be done in a nicer way
	wrapped_diff := []byte("```diff\n" + string(diff) + "```")
	// md := blackfriday.MarkdownCommon(wrapped_diff)
	md := github_flavored_markdown.Markdown(wrapped_diff)

	w.Header().Set("Content-Type", "text/html")
	w.Write(md)
}

Update import path of github_flavored_markdown package.


package repo

type SettingsData struct {
	PaymentDataInQR    *bool              `json:"paymentDataInQR"`
	ShowNotifications  *bool              `json:"showNotificatons"`
	ShowNsfw           *bool              `json:"showNsfw"`
	ShippingAddresses  *[]ShippingAddress `json:"shippingAddresses"`
	LocalCurrency      *string            `json:"localCurrency"`
	Country            *string            `json:"country"`
	Language           *string            `json:"language"`
	TermsAndConditions *string            `json:"termsAndConditions"`
	RefundPolicy       *string            `json:"refundPolicy"`
	BlockedNodes       *[]string          `json:"blockedNodes"`
	StoreModerators    *[]string          `json:"storeModerators"`
	SMTPSettings       *SMTPSettings      `json:"smtpSettings"`
}

type ShippingAddress struct {
	Name           string `json:"name"`
	Company        string `json:"company"`
	AddressLineOne string `json:"addressLineOne"`
	AddressLineTwo string `json:"addressLineTwo"`
	City           string `json:"city"`
	State          string `json:"state"`
	Country        string `json:"country"`
	PostalCode     string `json:"postalCode"`
	AddressNotes   string `json:"addressNotes"`
}

type SMTPSettings struct {
	Notifications  bool   `json:"notifications"`
	ServerAddress  string `json:"serverAddress"`
	Username       string `json:"username"`
	Password       string `json:"password"`
	SenderEmail    string `json:"senderEmail"`
	RecipientEmail string `json:"recipientEmail"`
}

package repo

type SettingsData struct {
	PaymentDataInQR    *bool              `json:"paymentDataInQR"`
	ShowNotifications  *bool              `json:"showNotifications"`
	ShowNsfw           *bool              `json:"showNsfw"`
	ShippingAddresses  *[]ShippingAddress `json:"shippingAddresses"`
	LocalCurrency      *string            `json:"localCurrency"`
	Country            *string            `json:"country"`
	Language           *string            `json:"language"`
	TermsAndConditions *string            `json:"termsAndConditions"`
	RefundPolicy       *string            `json:"refundPolicy"`
	BlockedNodes       *[]string          `json:"blockedNodes"`
	StoreModerators    *[]string          `json:"storeModerators"`
	SMTPSettings       *SMTPSettings      `json:"smtpSettings"`
}

type ShippingAddress struct {
	Name           string `json:"name"`
	Company        string `json:"company"`
	AddressLineOne string `json:"addressLineOne"`
	AddressLineTwo string `json:"addressLineTwo"`
	City           string `json:"city"`
	State          string `json:"state"`
	Country        string `json:"country"`
	PostalCode     string `json:"postalCode"`
	AddressNotes   string `json:"addressNotes"`
}

type SMTPSettings struct {
	Notifications  bool   `json:"notifications"`
	ServerAddress  string `json:"serverAddress"`
	Username       string `json:"username"`
	Password       string `json:"password"`
	SenderEmail    string `json:"senderEmail"`
	RecipientEmail string `json:"recipientEmail"`
}

Fix typo 'showNotificatons' -> 'showNotifications'.


package synchk

import (
	"go/parser"
	"go/scanner"
	"go/token"
	"gosubli.me/mg"
)

type SynChk struct {
	Files []FileRef
}

type FileRef struct {
	Fn  string
	Src string
}

type Error struct {
	Fn      string
	Line    int
	Column  int
	Message string
}

type Res struct {
	Errors []Error
}

func (s *SynChk) Call() (interface{}, string) {
	fset := token.NewFileSet()
	res := Res{
		Errors: []Error{},
	}

	for _, f := range s.Files {
		if f.Fn == "" && f.Src == "" {
			continue
		}

		var src []byte
		if f.Src != "" {
			src = []byte(f.Src)
		}

		_, err := parser.ParseFile(fset, f.Fn, src, parser.DeclarationErrors)
		if el, ok := err.(scanner.ErrorList); ok {
			for _, e := range el {
				res.Errors = append(res.Errors, Error{
					Fn:      e.Pos.Filename,
					Line:    e.Pos.Line,
					Column:  e.Pos.Column,
					Message: e.Msg,
				})
			}
		}
	}

	return res, ""
}

func init() {
	mg.Register("synchk", func(_ *mg.Broker) mg.Caller {
		return &SynChk{}
	})
}

package synchk

import (
	"gosubli.me/mg"
	"gosubli.me/mg/sa"
)

type SynChk struct {
	Files []FileRef
}

type FileRef struct {
	Fn  string
	Src string
}

type Res struct {
	Errors []*sa.Error
}

func (s *SynChk) Call() (interface{}, string) {
	res := Res{}
	for _, f := range s.Files {
		if f, _ := sa.Parse(f.Fn, []byte(f.Src)); f != nil {
			res.Errors = append(res.Errors, f.Errors...)
		}
	}
	return res, ""
}

func init() {
	mg.Register("synchk", func(_ *mg.Broker) mg.Caller {
		return &SynChk{}
	})
}

Use pkg sa to parse the files being syntax checked. It gives us cached parsing and error lists for free.


package bot

import (
	"regexp"
)

type Bot struct {
	adapter  Adapter
	Handlers map[*regexp.Regexp]func(msg *Message)
}

func New(adapter Adapter) *Bot {
	return &Bot{
		adapter,
		map[*regexp.Regexp]func(msg *Message){},
	}
}

func (b *Bot) Handle(expr string, handler func(msg *Message)) {
	b.Handlers[regexp.MustCompile(expr)] = handler
}

func (b *Bot) Listen() {
	for {
		msg := b.adapter.Listen()
		for expr, handler := range b.Handlers {
			if expr.MatchString(msg.Text) {
				handler(msg)
				break
			}
		}
	}
}

func (b *Bot) Reply(msg *Message, text string) {
	b.adapter.Reply(msg, text)
}

package bot

import (
	"regexp"
)

type Bot struct {
	adapter  Adapter
	Handlers map[*regexp.Regexp]func(msg *Message)
}

func New(adapter Adapter) *Bot {
	return &Bot{
		adapter,
		map[*regexp.Regexp]func(msg *Message){},
	}
}

func (b *Bot) Handle(expr string, handler func(msg *Message)) {
	b.Handlers[regexp.MustCompile(`(?i)`+expr)] = handler
}

func (b *Bot) Listen() {
	for {
		msg := b.adapter.Listen()
		for re, handler := range b.Handlers {
			if re.MatchString(msg.Text) {
				handler(msg)
				break
			}
		}
	}
}

func (b *Bot) Reply(msg *Message, text string) {
	b.adapter.Reply(msg, text)
}

Make handlers expr case insensitive


package torrent

import (
	"os"

	"bitbucket.org/anacrolix/go.torrent/testutil"

	"testing"
)

func TestAddTorrentNoSupportedTrackerSchemes(t *testing.T) {
	t.SkipNow()
}

func TestAddTorrentNoUsableURLs(t *testing.T) {
	t.SkipNow()
}

func TestAddPeersToUnknownTorrent(t *testing.T) {
	t.SkipNow()
}

func TestPieceHashSize(t *testing.T) {
	if PieceHash.Size() != 20 {
		t.FailNow()
	}
}

func TestTorrentInitialState(t *testing.T) {
	dir, mi := testutil.GreetingTestTorrent()
	defer os.RemoveAll(dir)
	tor, err := newTorrent(mi, "")
	if err != nil {
		t.Fatal(err)
	}
	if len(tor.Pieces) != 1 {
		t.Fatal("wrong number of pieces")
	}
	p := tor.Pieces[0]
	if len(p.PendingChunkSpecs) != 1 {
		t.Fatalf("should only be 1 chunk: %s", p.PendingChunkSpecs)
	}
	if _, ok := p.PendingChunkSpecs[ChunkSpec{
		Length: 13,
	}]; !ok {
		t.Fatal("pending chunk spec is incorrect")
	}
}

package torrent

import (
	"os"

	"bitbucket.org/anacrolix/go.torrent/testutil"

	"testing"
)

func TestAddTorrentNoSupportedTrackerSchemes(t *testing.T) {
	t.SkipNow()
}

func TestAddTorrentNoUsableURLs(t *testing.T) {
	t.SkipNow()
}

func TestAddPeersToUnknownTorrent(t *testing.T) {
	t.SkipNow()
}

func TestPieceHashSize(t *testing.T) {
	if PieceHash.Size() != 20 {
		t.FailNow()
	}
}

func TestTorrentInitialState(t *testing.T) {
	dir, mi := testutil.GreetingTestTorrent()
	defer os.RemoveAll(dir)
	tor, err := newTorrent(mi, dir)
	if err != nil {
		t.Fatal(err)
	}
	if len(tor.Pieces) != 1 {
		t.Fatal("wrong number of pieces")
	}
	p := tor.Pieces[0]
	if len(p.PendingChunkSpecs) != 1 {
		t.Fatalf("should only be 1 chunk: %s", p.PendingChunkSpecs)
	}
	if _, ok := p.PendingChunkSpecs[ChunkSpec{
		Length: 13,
	}]; !ok {
		t.Fatal("pending chunk spec is incorrect")
	}
}

Test created torrent structure in current directory