package main

func main() {
	Log("main.start")

	queryInterval := QueryInterval()
	databaseUrl := DatabaseUrl()
	libratoAuth := LibratoAuth()
	queryFiles := ReadQueryFiles("./queries/*.sql")

	metricBatches := make(chan []interface{}, 10)
	queryTicks := make(chan QueryFile, 10)
	globalStop := make(chan bool)
	monitorStop := make(chan bool)
	libratoStop := make(chan bool)
	postgresStop := make(chan bool)
	schedulerStop := make(chan bool)
	done := make(chan bool)

	go TrapStart(globalStop)
	go MonitorStart(queryTicks, metricBatches, monitorStop, done)
	go LibratoStart(libratoAuth, metricBatches, libratoStop, done)
	go PostgresStart(databaseUrl, queryTicks, queryInterval, metricBatches, postgresStop, done)
	go SchedulerStart(queryFiles, queryInterval, queryTicks, schedulerStop, done)

	Log("main.await")
	<-globalStop

	Log("main.stop")
	schedulerStop <- true
	<-done
	postgresStop <- true
	<-done
	libratoStop <- true
	<-done
	monitorStop <- true
	<-done

	Log("main.exit")
}

package main

func main() {
	Log("main.start")

	databaseUrl := DatabaseUrl()
	libratoAuth := LibratoAuth()
	queryInterval := QueryInterval()
	queryTimeout := queryInterval
	queryFiles := ReadQueryFiles("./queries/*.sql")

	metricBatches := make(chan []interface{}, 10)
	queryTicks := make(chan QueryFile, 10)
	globalStop := make(chan bool)
	monitorStop := make(chan bool)
	libratoStop := make(chan bool)
	postgresStop := make(chan bool)
	schedulerStop := make(chan bool)
	done := make(chan bool)

	go TrapStart(globalStop)
	go MonitorStart(queryTicks, metricBatches, monitorStop, done)
	go LibratoStart(libratoAuth, metricBatches, libratoStop, done)
	go PostgresStart(databaseUrl, queryTicks, queryTimeout, metricBatches, postgresStop, done)
	go SchedulerStart(queryFiles, queryInterval, queryTicks, schedulerStop, done)

	Log("main.await")
	<-globalStop

	Log("main.stop")
	schedulerStop <- true
	<-done
	postgresStop <- true
	<-done
	libratoStop <- true
	<-done
	monitorStop <- true
	<-done

	Log("main.exit")
}

Make timeout config more explicit


// Copyright 2013 Chris McGee <sirnewton_01@yahoo.ca>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build windows

package gdblib

import (
	"go/build"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
)

var (
	sendSignalPath string
)

func init() {
	gopath := build.Default.GOPATH
	gopaths := strings.Split(gopath, filepath.ListSeparator)
	for _,path := range(gopaths) {
		p := path + "\\src\\github.com\\sirnewton01\\gdblib\\SendSignal.exe"
		_,err := os.Stat(p)
		if err == nil {
			sendSignalPath = p
			break
		}
	}
}

func fixCmd(cmd *exec.Cmd) {
	// No process group separation is required on Windows.
	// Processes do not share signals like they can on Unix.
}

func interruptInferior(process *os.Process, pid string) {
	// Invoke the included "sendsignal" program to send the
	// Ctrl-break to the inferior process to interrupt it

	initCommand := exec.Command("cmd", "/c", "start", sendSignalPath, pid)
	initCommand.Run()
}

// Copyright 2013 Chris McGee <sirnewton_01@yahoo.ca>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build windows

package gdblib

import (
	"go/build"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
)

var (
	sendSignalPath string
)

func init() {
	gopath := build.Default.GOPATH
	gopaths := strings.Split(gopath, string(filepath.ListSeparator))
	for _,path := range(gopaths) {
		p := path + "\\src\\github.com\\sirnewton01\\gdblib\\SendSignal.exe"
		_,err := os.Stat(p)
		if err == nil {
			sendSignalPath = p
			break
		}
	}
}

func fixCmd(cmd *exec.Cmd) {
	// No process group separation is required on Windows.
	// Processes do not share signals like they can on Unix.
}

func interruptInferior(process *os.Process, pid string) {
	// Invoke the included "sendsignal" program to send the
	// Ctrl-break to the inferior process to interrupt it

	initCommand := exec.Command("cmd", "/c", "start", sendSignalPath, pid)
	initCommand.Run()
}

Convert ListSeparator into a string to use in Split.


// Copyright (C) 2017 Damon Revoe. All rights reserved.
// Use of this source code is governed by the MIT
// license, which can be found in the LICENSE file.

package main

import (
	"strings"
	"testing"
)

func substTestCase(t *testing.T, node *verbatim, expected string) {
	var result string

	for {
		result += node.text
		if node.next == nil {
			break
		}

		result += "[" + strings.Join(node.next.paramValues, ", ") + "]"

		node = &node.next.continuation
	}

	if result != expected {
		t.Error("Error: \"" + result + "\" != \"" + expected + "\"")
	}
}

func TestSubst(t *testing.T) {
	v := verbatim{"{Greetings}, {Who}!", nil}
	v.subst("Who", "Human")
	v.subst("Greetings", []string{"Hello", "Hi"})

	substTestCase(t, &v, "[Hello, Hi], Human!")

	v = verbatim{"{What}, {What} {Where}", nil}
	v.subst("What", "Mirror")
	v.subst("Where", "on the Wall")

	substTestCase(t, &v, "Mirror, Mirror on the Wall")
}

// Copyright (C) 2017 Damon Revoe. All rights reserved.
// Use of this source code is governed by the MIT
// license, which can be found in the LICENSE file.

package main

import (
	"fmt"
	"strings"
	"testing"
)

func substTestCase(t *testing.T, node *verbatim, expected string) {
	var result string

	for {
		result += node.text
		if node.next == nil {
			break
		}

		result += "[" + strings.Join(node.next.paramValues, ", ") + "]"

		node = &node.next.continuation
	}

	if result != expected {
		t.Error("Error: \"" + result + "\" != \"" + expected + "\"")
	}
}

func TestSubst(t *testing.T) {
	v := verbatim{"{Greetings}, {Who}!", nil}
	v.subst("Who", "Human")
	v.subst("Greetings", []string{"Hello", "Hi"})

	substTestCase(t, &v, "[Hello, Hi], Human!")

	v = verbatim{"{What}, {What} {Where}", nil}
	v.subst("What", "Mirror")
	v.subst("Where", "on the Wall")

	substTestCase(t, &v, "Mirror, Mirror on the Wall")
}

func TestExpandPathnameTemplate(t *testing.T) {
	fmt.Println(expandPathnameTemplate("{nil}{dir}/{name}.{ext}",
		map[string]interface{}{
			"nil":  []string{},
			"dir":  []string{"red", "blue", "yellow", "green"},
			"name": []string{"foo", "bar"},
			"ext":  []string{"js", "go", "rs"}}))
}

Add a Println-based test of expandPathnameTemplate


// hcl is a package for decoding HCL into usable Go structures.
//
// hcl input can come in either pure HCL format or JSON format.
// It can be parsed into an AST, and then decoded into a structure,
// or it can be decoded directly from a string into a structure.
//
// If you choose to parse HCL into a raw AST, the benefit is that you
// can write custom visitor implementations to implement custom
// semantic checks. By default, HCL does not perform any semantic
// checks.
package hcl

// Package hcl decodes HCL into usable Go structures.
//
// hcl input can come in either pure HCL format or JSON format.
// It can be parsed into an AST, and then decoded into a structure,
// or it can be decoded directly from a string into a structure.
//
// If you choose to parse HCL into a raw AST, the benefit is that you
// can write custom visitor implementations to implement custom
// semantic checks. By default, HCL does not perform any semantic
// checks.
package hcl

Use Go convention of docs with "Package" prefix


// Copyright 2020 The LUCI 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.

// +build !windows

package invoke

import (
	"os/exec"
	"syscall"

	"go.chromium.org/luci/common/errors"
)

func setSysProcAttr(_ *exec.Cmd) {}

func (s *Subprocess) terminate() error {
	if err := s.cmd.Process.Signal(syscall.SIGTERM); err != nil {
		return errors.Annotate(err, "send SIGTERM").Err()
	}
	return nil
}

// Copyright 2020 The LUCI 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.

// +build !windows

package invoke

import (
	"os/exec"
	"syscall"

	"go.chromium.org/luci/common/errors"
)

func setSysProcAttr(cmd *exec.Cmd) {
	cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
}

func (s *Subprocess) terminate() error {
	if err := syscall.Kill(-s.cmd.Process.Pid, syscall.SIGTERM); err != nil {
		return errors.Annotate(err, "send SIGTERM").Err()
	}
	return nil
}

Make and signal process group on *nix.


/*-
 * Copyright (c) 2016, Jörg Pernfuß <joerg.pernfuss@1und1.de>
 * All rights reserved
 *
 * Use of this source code is governed by a 2-clause BSD license
 * that can be found in the LICENSE file.
 */

package msg

import (
)

type Supervisor struct {
	Verdict    uint16
	RemoteAddr string
	// Fields for encrypted requests
	KexId string
	Data  []byte
	Kex   auth.Kex
	// Fields for basic authentication requests
	BasicAuthUser  string
	BasicAuthToken string
	Restricted     bool
	// Fields for permission authorization requests
	PermAction     string
	PermRepository string
	PermMonitoring string
	PermNode       string
	// Fields for map update notifications
	Action string
	Object string
	User   proto.User
	Team   proto.Team
}

// vim: ts=4 sw=4 sts=4 noet fenc=utf-8 ffs=unix

/*-
 * Copyright (c) 2016, Jörg Pernfuß <joerg.pernfuss@1und1.de>
 * All rights reserved
 *
 * Use of this source code is governed by a 2-clause BSD license
 * that can be found in the LICENSE file.
 */

package msg

import (
)

type Supervisor struct {
	Verdict     uint16
	VerictAdmin bool
	RemoteAddr  string
	// Fields for encrypted requests
	KexId string
	Data  []byte
	Kex   auth.Kex
	// Fields for basic authentication requests
	BasicAuthUser  string
	BasicAuthToken string
	Restricted     bool
	// Fields for permission authorization requests
	PermAction     string
	PermRepository string
	PermMonitoring string
	PermNode       string
	// Fields for map update notifications
	Action string
	Object string
	User   proto.User
	Team   proto.Team
}

// vim: ts=4 sw=4 sts=4 noet fenc=utf-8 ffs=unix

Add field to indicate admin permission


package main

import (
	"github.com/phalaaxx/cdb"
)

/* VerifyLocal checks if named mailbox exist in a local cdb database */
func VerifyLocal(name string) bool {
	var value *string
	err := cdb.Lookup(
		LocalCdb,
		func(db *cdb.Reader) (err error) {
			value, err = db.Get(name)
			return err
		},
	)
	if err == nil && value != nil && len(*value) != 0 {
		return true
	}
	return false
}

package main

import (
	"github.com/phalaaxx/godb"
)

/* VerifyLocal checks if named mailbox exist in a local cdb database */
func VerifyLocal(name string) bool {
	var value *string
	err := godb.CdbLookup(
		LocalCdb,
		func(db *godb.CdbReader) (err error) {
			value, err = db.Get(name)
			return err
		},
	)
	if err == nil && value != nil && len(*value) != 0 {
		return true
	}
	return false
}

Use godb library instead of cdb.


package main

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

	"github.com/billhathaway/webcounter"
)

const (
	defaultPort = "8080"
)

func main() {
	port := flag.String("p", defaultPort, "listen port")
	flag.Parse()
	counter, err := webcounter.New()
	if err != nil {
		log.Fatal(err)
	}
	http.ListenAndServe(":"+*port, counter)
}

package main

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

	"github.com/billhathaway/webcounter"
)

const (
	defaultPort = "8080"
)

func main() {
	port := flag.String("p", defaultPort, "listen port")
	pprofPort := flag.String("pprof", "", "listen port for profiling")
	flag.Parse()
	counter, err := webcounter.New()
	if err != nil {
		log.Fatal(err)
	}
	if *pprofPort != "" {
		go func() {
			log.Fatal(http.ListenAndServe(":"+*pprofPort, nil))
		}()
	}
	log.Fatal(http.ListenAndServe(":"+*port, counter))
}

Add -pprof flag to enable profiling


package of10

import (
	"bytes"
	"encoding/binary"
)

const MaxPortNameLength = 16
const EthernetAddressLength = 6

type PortNumber uint16
type PortConfig uint16
type PortState uint16
type PortFeature uint16

type PhysicalPort struct {
	PortNumber         PortNumber
	HardwareAddress    [EthernetAddressLength]uint8
	Name               [MaxPortNameLength]uint8
	Config             PortConfig
	State              PortState
	CurrentFeatures    PortFeature
	AdvertisedFeatures PortFeature
	SupportedFeatures  PortFeature
	PeerFeatures       PortFeature
}

func readPhysicalPort(b []byte) ([]PhysicalPort, error) {
	var port PhysicalPort
	count := len(b) / binary.Size(port)
	ports := make([]PhysicalPort, count)

	buf := bytes.NewBuffer(b)
	if err := binary.Read(buf, binary.BigEndian, port); err != nil {
		return nil, err
	}

	return ports, nil
}

package of10

import (
	"bytes"
	"encoding/binary"
)

const MaxPortNameLength = 16
const EthernetAddressLength = 6

type PortNumber uint16
type PortConfig uint16
type PortState uint16
type PortFeature uint16

type PhysicalPort struct {
	PortNumber         PortNumber
	HardwareAddress    [EthernetAddressLength]uint8
	Name               [MaxPortNameLength]uint8
	Config             PortConfig
	State              PortState
	CurrentFeatures    PortFeature
	AdvertisedFeatures PortFeature
	SupportedFeatures  PortFeature
	PeerFeatures       PortFeature
}

func readPhysicalPort(b []byte) ([]PhysicalPort, error) {
	var port PhysicalPort
	count := len(b) / binary.Size(port)
	ports := make([]PhysicalPort, 0, count)

	buf := bytes.NewBuffer(b)
	if err := binary.Read(buf, binary.BigEndian, port); err != nil {
		return nil, err
	}

	return ports, nil
}

Fix wrong slice length in initialization


package main

import (
	"fmt"
	"github.com/pierrre/mangadownloader"
)

func main() {
	var service mangadownloader.Service = &mangadownloader.MangaReaderService{}
	mangas, err := service.Mangas()
	if err != nil {
		panic(err)
	}
	for _, manga := range mangas {
		fmt.Println(manga)
		chapters, err := manga.Chapters()
		if err != nil {
			panic(err)
		}
		for _, chapter := range chapters {
			fmt.Println(chapter)
		}
	}
}

package main

import (
	"fmt"
	"github.com/pierrre/mangadownloader"
)

func main() {
	var service mangadownloader.Service = &mangadownloader.MangaReaderService{}
	mangas, err := service.Mangas()
	if err != nil {
		panic(err)
	}
	for _, manga := range mangas {
		fmt.Println(manga)
		chapters, err := manga.Chapters()
		if err != nil {
			panic(err)
		}
		for _, chapter := range chapters {
			fmt.Println("	" + fmt.Sprint(chapter))
		}
	}
}

Add formatting in test command


package templates

const (
	/* Provider template */
	providerTemplate = `
provider "aws" {
	region  = "{{ .Region }}"
	profile = "{{ .Account }}"
}
`

	/* Terraform Backend */
	backendTemplate = `
terraform {
	required_version = ">= v0.11.7"
	backend "s3" {
		region     = "{{ .Region }}"
		profile    = "{{ .Account }}"
		bucket     = "{{ .Region }}-{{ .Account }}-ilm-state"
		key     = "terraform-states/s3backends/{{ .Region }}-{{ .Account }}-ilm-state.tfstate"
		encrypt    = "true"
		dynamodb_table = "{{ .Region }}-{{ .Account }}-dynamodb-table-tf-lock"
		acl        = "bucket-owner-full-control"
	}
}
`
)

/*
 * Return the provider template
 */
func GetProviderTemplate() string {
	return providerTemplate
}

/* Return Backend template */
func GetBackendTemplate() string {
	return backendTemplate
}

package templates

const (
	/* Provider template */
	providerTemplate = `
provider "aws" {
	region  = "{{ .Region }}"
	profile = "{{ .Account }}"
}
`

	/* Terraform Backend */
	backendTemplate = `
terraform {
	required_version = ">= v0.11.7"
	backend "s3" {
		region     = "{{ .Region }}"
		profile    = "{{ .Account }}"
		bucket     = "{{ .Region }}-{{ .Account }}-tfstate-state"
		key     = "terraform-states/s3backends/{{ .Region }}-{{ .Account }}-tfstate-state.tfstate"
		encrypt    = "true"
		dynamodb_table = "{{ .Region }}-{{ .Account }}-dynamodb-table-tf-lock"
		acl        = "bucket-owner-full-control"
	}
}
`
/ * TF State S3 Backend Definition */
	tfstateBackendTemplate = `
	resource "aws_dynamodb_table" "terraform_statelock" {
		name           = "{{ .Region }}-{{ .Account }}-dynamodb-table-tf-lock"
		read_capacity  = 1
		write_capacity = 1
		hash_key       = "LockID"

		attribute {
			name = "LockID"
			type = "S"
		}

		lifecycle {
			prevent_destroy = false
		}
	}

	resource "aws_s3_bucket" "tfstate_state_bucket" {
	  bucket = "{{ .Region }}-{{ .Account }}-tfstate-state"

	}

	`
)

/*
 * Return the provider template
 */
func GetProviderTemplate() string {
	return providerTemplate
}

/* Return Backend template */
func GetBackendTemplate() string {
	return backendTemplate
}

Add S3 backend - dynamodb for lock


// +build linux darwin

package instance

// DirName returns the name of the subdirectory where instance data are stored.
// On Posix systems, it's the instance domain name.
func (i *Instance) DirName() string {
	return i.Domain
}

// +build !windows

package instance

// DirName returns the name of the subdirectory where instance data are stored.
// On Posix systems, it's the instance domain name.
func (i *Instance) DirName() string {
	return i.Domain
}

Fix building cozy-stack on FreeBSD, again


package main

import (
	"flag"
	"fmt"
	"github.com/HearthSim/stove/bnet"
	"github.com/HearthSim/stove/pegasus"
	_ "github.com/mattn/go-sqlite3"
	"strings"
)

const (
	CONN_DEFAULT_HOST = "localhost"
	CONN_DEFAULT_PORT = 1119
)

func main() {
	addr := fmt.Sprintf("%s:%d", CONN_DEFAULT_HOST, CONN_DEFAULT_PORT)
	flag.StringVar(&addr, "bind", addr, "The address to run on")
	runMigrate := flag.Bool("migrate", false, "Perform a database migration and exit")
	flag.Parse()

	if !strings.Contains(addr, ":") {
		addr = fmt.Sprintf("%s:%d", addr, CONN_DEFAULT_PORT)
	}

	if *runMigrate {
		fmt.Printf("Performing database migration\n")
		pegasus.Migrate()
		return
	}

	serv := bnet.NewServer()
	serv.RegisterGameServer("WTCG", pegasus.NewServer(serv))

	fmt.Printf("Listening on %s ...\n", addr)
	err := serv.ListenAndServe(addr)
	if err != nil {
		fmt.Println(err)
	}
}

package main

import (
	"flag"
	"fmt"
	"github.com/HearthSim/stove/bnet"
	"github.com/HearthSim/stove/pegasus"
	_ "github.com/rakyll/gom/http"
	"net/http"
	"strings"
)

const (
	CONN_DEFAULT_HOST = "localhost"
	CONN_DEFAULT_PORT = 1119
)

func main() {
	addr := fmt.Sprintf("%s:%d", CONN_DEFAULT_HOST, CONN_DEFAULT_PORT)
	flag.StringVar(&addr, "bind", addr, "The address to run on")
	runMigrate := flag.Bool("migrate", false, "Perform a database migration and exit")
	flag.Parse()

	if !strings.Contains(addr, ":") {
		addr = fmt.Sprintf("%s:%d", addr, CONN_DEFAULT_PORT)
	}

	if *runMigrate {
		fmt.Printf("Performing database migration\n")
		pegasus.Migrate()
		return
	}

	go func() {
		httpAddr := "localhost:6060"
		fmt.Printf("Debug http server listening on %s ...\n", httpAddr)
		fmt.Println(http.ListenAndServe(httpAddr, nil))
	}()

	serv := bnet.NewServer()
	serv.RegisterGameServer("WTCG", pegasus.NewServer(serv))

	fmt.Printf("Listening on %s ...\n", addr)
	err := serv.ListenAndServe(addr)
	if err != nil {
		fmt.Println(err)
	}
}

Add pprof server and gom support


package httpd

import (
	"bufio"
	"fmt"
	"github.com/Symantec/Dominator/lib/html"
	"net/http"
)

func (s *state) statusHandler(w http.ResponseWriter, req *http.Request) {
	writer := bufio.NewWriter(w)
	defer writer.Flush()
	fmt.Fprintln(writer, "<title>Basic filegen server status page</title>")
	fmt.Fprintln(writer, "")
	fmt.Fprintln(writer, "")
	fmt.Fprintln(writer, "
Basic filegen server status page
")
	fmt.Fprintln(writer, "")
	html.WriteHeaderWithRequest(writer, req)
	fmt.Fprintln(writer, "
")
	for _, htmlWriter := range htmlWriters {
		htmlWriter.WriteHtml(writer)
	}
	fmt.Fprintln(writer, "
")
	fmt.Fprintln(writer, "
")
	html.WriteFooter(writer)
	fmt.Fprintln(writer, "")
}

package httpd

import (
	"bufio"
	"fmt"
	"github.com/Symantec/Dominator/lib/html"
	"github.com/Symantec/Dominator/lib/srpc"
	"net/http"
)

func (s *state) statusHandler(w http.ResponseWriter, req *http.Request) {
	writer := bufio.NewWriter(w)
	defer writer.Flush()
	fmt.Fprintln(writer, "<title>Basic filegen server status page</title>")
	fmt.Fprintln(writer, "")
	fmt.Fprintln(writer, "")
	fmt.Fprintln(writer, "
Basic filegen server status page
")
	if !srpc.CheckTlsRequired() {
		fmt.Fprintln(writer,
			`<h1><font color="red">Running in insecure mode. Anyone can get your files!</font></h1>`)
	}
	fmt.Fprintln(writer, "")
	html.WriteHeaderWithRequest(writer, req)
	fmt.Fprintln(writer, "
")
	for _, htmlWriter := range htmlWriters {
		htmlWriter.WriteHtml(writer)
	}
	fmt.Fprintln(writer, "
")
	fmt.Fprintln(writer, "
")
	html.WriteFooter(writer)
	fmt.Fprintln(writer, "")
}

Add warning banner to filegen HTTP server if in insecure mode.


//----------------------------------------
//
// Copyright © ying32. All Rights Reserved.
//
// Licensed under Apache License 2.0
//
//----------------------------------------

// +build windows linux
// +build !tempdll
// +build !memorydll

// 指令为：target == windows || target == linux && !tempdll && !memorydll

package api

func checkAndReleaseDLL() (bool, string) {
	return false, ""
}

//----------------------------------------
//
// Copyright © ying32. All Rights Reserved.
//
// Licensed under Apache License 2.0
//
//----------------------------------------

// +build !tempdll
// +build !memorydll

// 指令为：!tempdll && !memorydll

package api

func checkAndReleaseDLL() (bool, string) {
	return false, ""
}

Fix the error of `tempdll` constraint under macOS


package command

import (
	"github.com/cloudfoundry/cli/cf/command_metadata"
	"github.com/cloudfoundry/cli/cf/requirements"
	"github.com/codegangsta/cli"
)

type Command interface {
	Metadata() command_metadata.CommandMetadata
	GetRequirements(requirementsFactory requirements.Factory, c *cli.Context) (reqs []requirements.Requirement, err error)
	Run(c *cli.Context)
}

package command

import (
	"github.com/cloudfoundry/cli/cf/command_metadata"
	"github.com/cloudfoundry/cli/cf/requirements"
	"github.com/codegangsta/cli"
)

type Command interface {
	Metadata() command_metadata.CommandMetadata
	GetRequirements(requirementsFactory requirements.Factory, context *cli.Context) (reqs []requirements.Requirement, err error)
	Run(context *cli.Context)
}

Rename some variables on Command interface


package mapper

import (
	"fmt"

	"github.com/wanelo/image-server/core"
	"github.com/wanelo/image-server/encoders/base62"
)

type SourceMapper struct {
	ServerConfiguration *core.ServerConfiguration
}

// RemoteImageURL returns a URL string for original image
func (m *SourceMapper) RemoteImageURL(ic *core.ImageConfiguration) string {
	if ic.Source != "" {
		return ic.Source
	}
	url := ic.ServerConfiguration.SourceDomain + "/" + m.imageDirectory(ic) + "/original.jpg"
	return url
}

func (m *SourceMapper) imageDirectory(ic *core.ImageConfiguration) string {
	id := base62.Decode("ofrA")
	// fmt.Printf("Decoded %s to %d", ic.ID, id)
	return fmt.Sprintf("%s/%d", m.ServerConfiguration.NamespaceMappings[ic.Namespace], id)
}

package mapper

import (
	"fmt"

	"github.com/wanelo/image-server/core"
	"github.com/wanelo/image-server/encoders/base62"
)

type SourceMapper struct {
	ServerConfiguration *core.ServerConfiguration
}

// RemoteImageURL returns a URL string for original image
func (m *SourceMapper) RemoteImageURL(ic *core.ImageConfiguration) string {
	if ic.Source != "" {
		return ic.Source
	}
	url := ic.ServerConfiguration.SourceDomain + "/" + m.imageDirectory(ic) + "/original.jpg"
	return url
}

func (m *SourceMapper) imageDirectory(ic *core.ImageConfiguration) string {
	id := base62.Decode(ic.ID)
	return fmt.Sprintf("%s/%d", m.ServerConfiguration.NamespaceMappings[ic.Namespace], id)
}

Use real image id, instead of placeholder base62 string, oops!


package osin

import (
	"encoding/base64"
	"strings"

	"github.com/satori/go.uuid"
)

// AuthorizeTokenGenDefault is the default authorization token generator
type AuthorizeTokenGenDefault struct {
}

func removePadding(token string) string {
	return strings.TrimRight(token, "=")
}

// GenerateAuthorizeToken generates a base64-encoded UUID code
func (a *AuthorizeTokenGenDefault) GenerateAuthorizeToken(data *AuthorizeData) (ret string, err error) {
	token := uuid.NewV4()
	return removePadding(base64.URLEncoding.EncodeToString(token.Bytes())), nil
}

// AccessTokenGenDefault is the default authorization token generator
type AccessTokenGenDefault struct {
}

// GenerateAccessToken generates base64-encoded UUID access and refresh tokens
func (a *AccessTokenGenDefault) GenerateAccessToken(data *AccessData, generaterefresh bool) (accesstoken string, refreshtoken string, err error) {
	token := uuid.NewV4()
	accesstoken = removePadding(base64.URLEncoding.EncodeToString(token.Bytes()))

	if generaterefresh {
		rtoken := uuid.NewV4()
		refreshtoken = removePadding(base64.URLEncoding.EncodeToString(rtoken.Bytes()))
	}
	return
}

package osin

import (
	"encoding/base64"
	"strings"

	"github.com/pborman/uuid"
)

// AuthorizeTokenGenDefault is the default authorization token generator
type AuthorizeTokenGenDefault struct {
}

func removePadding(token string) string {
	return strings.TrimRight(token, "=")
}

// GenerateAuthorizeToken generates a base64-encoded UUID code
func (a *AuthorizeTokenGenDefault) GenerateAuthorizeToken(data *AuthorizeData) (ret string, err error) {
	token := uuid.NewRandom()
	return removePadding(base64.URLEncoding.EncodeToString([]byte(token))), nil
}

// AccessTokenGenDefault is the default authorization token generator
type AccessTokenGenDefault struct {
}

// GenerateAccessToken generates base64-encoded UUID access and refresh tokens
func (a *AccessTokenGenDefault) GenerateAccessToken(data *AccessData, generaterefresh bool) (accesstoken string, refreshtoken string, err error) {
	token := uuid.NewRandom()
	return removePadding(base64.URLEncoding.EncodeToString([]byte(token))), nil

	if generaterefresh {
		rtoken := uuid.NewRandom()
		refreshtoken = removePadding(base64.URLEncoding.EncodeToString([]byte(rtoken)))
	}
	return
}

Switch uuid to original implementation


// Copyright 2012 Aaron Jacobs. All Rights Reserved.
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
//
// 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 auth

import (
	"errors"
	"github.com/jacobsa/aws"
	"github.com/jacobsa/aws/s3/http"
)

// A Signer knows how to create signatures suitable for inclusion in an HTTP
// request to S3.
type Signer interface {
	// Add an appropriate signature header to the supplied HTTP request.
	Sign(r *http.Request) error
}

// NewSigner creates a Signer using the supplied access key.
func NewSigner(key aws.AccessKey) (Signer, error) {
	return nil, errors.New("TODO: Implement NewSigner.")
}

// Copyright 2012 Aaron Jacobs. All Rights Reserved.
// Author: aaronjjacobs@gmail.com (Aaron Jacobs)
//
// 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 auth

import (
	"errors"
	"github.com/jacobsa/aws"
	"github.com/jacobsa/aws/s3/http"
)

// A Signer knows how to create signatures suitable for inclusion in an HTTP
// request to S3.
type Signer interface {
	// Add an appropriate signature header to the supplied HTTP request.
	Sign(r *http.Request) error
}

// NewSigner creates a Signer using the supplied access key.
func NewSigner(key aws.AccessKey) (Signer, error) {
	return newSigner(stringToSign, key)
}

// newSigner is a helper used by NewSigner, split out for testability. It
// allows you to inject the function that is used to determine the string to
// sign for any given request.
func newSigner(
	sts func(*http.Request) (string, error),
	key aws.AccessKey) (Signer, error) {
	return nil, errors.New("TODO: Implement newSigner.")
}

Split out a helper function.


//
// openzwave provides a thin Go wrapper around the openzwave library.
//
// The following shows a simple use of the API which will log every notification received.
//
//        var loop = func(api openzwave.API) {
//                fmt.Printf("event loop starts\n")
//                for {
//                        select {
//                        case notification := <-api.Notifications():
//                                fmt.Printf("notification received - %v", notification)
//                                api.FreeNotification(notification)
//                        case quitNow := <-api.QuitSignal():
//                                _ = quitNow
//                                fmt.Printf("event loop ends\n")
//                                return
//                        }
//                }
//        }
//
//        os.Exit(openzwave.
//                BuildAPI("../go-openzwave/openzwave/config", "", "").
//                AddIntOption("SaveLogLevel", LOG_LEVEL.NONE).
//                AddIntOption("QueueLogLevel", LOG_LEVEL.NONE).
//                AddIntOption("DumpTrigger", LOG_LEVEL.NONE).
//                AddIntOption("PollInterval", 500).
//                AddBoolOption("IntervalBetweenPolls", true).
//                AddBoolOption("ValidateValueChanges", true).
//                Run(loop))
package openzwave

//
// openzwave provides a thin Go wrapper around the openzwave library.
//
// The following shows a simple use of the API which will log every notification received.
//
//        var loop = func(api openzwave.API) {
//                fmt.Printf("event loop starts\n")
//                for {
//                        select {
//                        case quitNow := <-api.QuitSignal():
//                                _ = quitNow
//                                fmt.Printf("event loop ends\n")
//                                return
//                        }
//                }
//        }
//
//        os.Exit(openzwave.
//                BuildAPI("../go-openzwave/openzwave/config", "", "").
//                AddIntOption("SaveLogLevel", LOG_LEVEL.NONE).
//                AddIntOption("QueueLogLevel", LOG_LEVEL.NONE).
//                AddIntOption("DumpTrigger", LOG_LEVEL.NONE).
//                AddIntOption("PollInterval", 500).
//                AddBoolOption("IntervalBetweenPolls", true).
//                AddBoolOption("ValidateValueChanges", true).
//                Run(loop))
package openzwave

Remove misleading example - api.Notifications() was removed from the API in favour of a callback.


package devd

import (
	"io/ioutil"
	"os"
	"strings"
	"sync"
	"testing"
	"time"

	"github.com/cortesi/termlog"
)

func addTempFile(wg *sync.WaitGroup, t *testing.T, tmpFolder string, fname string, content string) {
	if err := ioutil.WriteFile(tmpFolder+"/"+fname, []byte(content), 0644); err != nil {
		t.Error(err)
	}
	wg.Add(1)
}

// waitTimeout waits for the waitgroup for the specified max timeout.
// Returns true if waiting timed out.
func waitTimeout(wg *sync.WaitGroup, timeout time.Duration) bool {
	c := make(chan struct{})
	go func() {
		defer close(c)
		wg.Wait()
	}()
	select {
	case <-c:
		return false // completed normally
	case <-time.After(timeout):
		return true // timed out
	}
}

func TestRouteWatch(t *testing.T) {
	logger := termlog.NewLog()
	logger.Quiet()

	tmpFolder, err := ioutil.TempDir("", "")
	if err != nil {
		t.Error(err)
	}
	defer os.RemoveAll(tmpFolder)

  // Ensure that using . for the path works:
	os.Chdir(tmpFolder)
	routes := make(RouteCollection)
	routes.Add(".", nil)

	changedFiles := make(map[string]int)

	ch := make(chan []string, 1)
	var wg sync.WaitGroup
	go func() {
		for {
			data, more := <-ch
			if more {
				t.Log("received notification for changed file(s):", strings.Join(data, ", "))
				for i := range data {
					changedFiles[data[i]] = 1
					wg.Done()
				}
			} else {
				t.Log("No more changes are expected")
				return
			}
		}
	}()
	for i := range routes {
		err := routes[i].Watch(ch, nil, logger)
		if err != nil {
			t.Error(err)
		}
	}
	//////////////////////////////////
	t.Log("Temp folder:", tmpFolder)
	addTempFile(&wg, t, tmpFolder, "a.txt", "foo\n")
	addTempFile(&wg, t, tmpFolder, "c.txt", "bar\n")
	addTempFile(&wg, t, tmpFolder, "another.file.txt", "bar\n")
	//////////////////////////////////
	waitTimeout(&wg, 700*time.Millisecond)
	close(ch)
	if len(changedFiles) != 3 {
		t.Error("The watch should have been notified about 3 changed files")
	}
}

Add test for the Route.Watch/3 method.


package moka

import "testing"

type Register struct{}

func New(testingT *testing.T) Register {
	return Register{}
}

func (r Register) Allow(double Double) Subject {
	return Subject{double: double}
}

func (r Register) Expect(double Double) Subject {
	return Subject{}
}

func (r Register) Verify() {}

Remove Register.Expect and Register.Verify for now


package integration_test

import (
	"path/filepath"

	"github.com/cloudfoundry/libbuildpack/cutlass"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("CF Dotnet Buildpack", func() {
	var (
		app         *cutlass.App
		fixtureName string
	)
	AfterEach(func() { app = DestroyApp(app) })

	JustBeforeEach(func() {
		app = cutlass.New(filepath.Join(bpDir, "fixtures", fixtureName))
	})

	Context("Deploying an app with multiple projects", func() {
		BeforeEach(func() {
			fixtureName = "multiple_projects_msbuild"
		})
		It("compiles both apps", func() {
			PushAppAndConfirm(app)
			Expect(app.GetBody("/")).To(ContainSubstring("Hello, I'm a string!"))
			Expect(app.Stdout.String()).To(ContainSubstring("Hello from a secondary project!"))
		})
	})
})

package integration_test

import (
	"path/filepath"
	"time"

	"github.com/cloudfoundry/libbuildpack/cutlass"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("CF Dotnet Buildpack", func() {
	var (
		app         *cutlass.App
		fixtureName string
	)
	AfterEach(func() { app = DestroyApp(app) })

	JustBeforeEach(func() {
		app = cutlass.New(filepath.Join(bpDir, "fixtures", fixtureName))
	})

	Context("Deploying an app with multiple projects", func() {
		BeforeEach(func() {
			fixtureName = "multiple_projects_msbuild"
		})
		It("compiles both apps", func() {
			PushAppAndConfirm(app)
			Expect(app.GetBody("/")).To(ContainSubstring("Hello, I'm a string!"))
			Eventually(app.Stdout.String, 10*time.Second).Should(ContainSubstring("Hello from a secondary project!"))
		})
	})
})

Test correctly waits for expected log output


package scwversion

import (
	"testing"

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

func TestInit(t *testing.T) {
	Convey("Testing init()", t, func() {
		So(VERSION, ShouldNotEqual, "")
		So(GITCOMMIT, ShouldNotEqual, "")
	})
}

package scwversion

import (
	"testing"

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

func TestInit(t *testing.T) {
	Convey("Testing init()", t, func() {
		So(VERSION, ShouldNotEqual, "")
		// So(GITCOMMIT, ShouldNotEqual, "")
	})
}

Disable version test for GITCOMMIT


//go:build openbsd
// +build openbsd

package kernel

import (
	"fmt"
	"runtime'
)

// A stub called by kernel_unix.go .
func uname() (*Utsname, error) {
	return nil, fmt.Errorf("Kernel version detection is not available on %s", runtime.GOOS)
}

//go:build openbsd
// +build openbsd

package kernel

import (
	"fmt"
	"runtime"
)

// A stub called by kernel_unix.go .
func uname() (*Utsname, error) {
	return nil, fmt.Errorf("Kernel version detection is not available on %s", runtime.GOOS)
}

Fix a typo that breaks build / vedoring


package middleware

import (
	"context"
	"net/http"

	"github.com/nats-io/nuid"
)

type ctxRequestIDKeyType int

const CtxRequestIDKey int = 0

const (
	headerXRequestID = "X-Request-ID"
)

type RequestID struct {
	SetHeader bool
	n         *nuid.NUID
}

func NewRequestID() *RequestID {
	return &RequestID{
		SetHeader: true,
		n:         nuid.New(),
	}
}

func (rid *RequestID) ServeNext(next http.Handler) http.Handler {
	fn := func(w http.ResponseWriter, r *http.Request) {
		requestid := rid.n.Next()
		if rid.SetHeader {
			w.Header().Set(headerXRequestID, requestid)
		}
		ctx := context.WithValue(r.Context(), CtxRequestIDKey, requestid)
		next.ServeHTTP(w, r.WithContext(ctx))
	}
	return http.HandlerFunc(fn)
}

package middleware

import (
	"context"
	"net/http"

	"github.com/nats-io/nuid"
)

type ctxRequestIDKeyType int

const CtxRequestIDKey ctxRequestIDKeyType = 0

const (
	headerXRequestID = "X-Request-ID"
)

type RequestID struct {
	SetHeader bool
	n         *nuid.NUID
}

func NewRequestID() *RequestID {
	return &RequestID{
		SetHeader: true,
		n:         nuid.New(),
	}
}

func (rid *RequestID) ServeNext(next http.Handler) http.Handler {
	fn := func(w http.ResponseWriter, r *http.Request) {
		requestid := rid.n.Next()
		if rid.SetHeader {
			w.Header().Set(headerXRequestID, requestid)
		}
		ctx := context.WithValue(r.Context(), CtxRequestIDKey, requestid)
		next.ServeHTTP(w, r.WithContext(ctx))
	}
	return http.HandlerFunc(fn)
}

Fix request id ctx key


package main

import (
	"encoding/base64"
	"net/http"
	"strings"
)

func BasicAuth(handler http.HandlerFunc, username, password string) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		authError := func() {
			w.Header().Set("WWW-Authenticate", "Basic realm=\"Zork\"")
			http.Error(w, "authorization failed", http.StatusUnauthorized)
		}

		auth := strings.SplitN(r.Header.Get("Authorization"), " ", 2)
		if len(auth) != 2 || auth[0] != "Basic" {
			authError()
			return
		}

		payload, err := base64.StdEncoding.DecodeString(auth[1])
		if err != nil {
			authError()
			return
		}

		pair := strings.SplitN(string(payload), ":", 2)
		if len(pair) != 2 || !(pair[0] == username && pair[1] == password) {
			authError()
			return
		}

		handler(w, r)
	}
}

package main

import (
	"net/http"
)

func BasicAuth(handler http.Handler, username, password string) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		if u, p, ok := r.BasicAuth(); !ok || !(u == username && p == password) {
			w.Header().Set("WWW-Authenticate", "Basic realm=\"Zork\"")
			http.Error(w, "authorization failed", http.StatusUnauthorized)
			return
		}

		handler.ServeHTTP(w, r)
	}
}

Simplify basic auth code signifigantly


// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package api

import (
	"github.com/dotcominternet/platform/model"
)

type MeProvider struct {
}

const (
	CMD_ME = "me"
)

func init() {
	RegisterCommandProvider(&MeProvider{})
}

func (me *MeProvider) GetTrigger() string {
	return CMD_ME
}

func (me *MeProvider) GetCommand(c *Context) *model.Command {
	return &model.Command{
		Trigger:          CMD_ME,
		AutoComplete:     true,
		AutoCompleteDesc: c.T("api.command_me.desc"),
		AutoCompleteHint: c.T("api.command_me.hint"),
		DisplayName:      c.T("api.command_me.name"),
	}
}

func (me *MeProvider) DoCommand(c *Context, channelId string, message string) *model.CommandResponse {
	return &model.CommandResponse{ResponseType: model.COMMAND_RESPONSE_TYPE_IN_CHANNEL, Text: "*" + message + "*"}
}

// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package api

import (
	"github.com/dotcominternet/platform/model"
)

type MeProvider struct {
}

const (
	CMD_ME = "me"
)

func init() {
	RegisterCommandProvider(&MeProvider{})
}

func (me *MeProvider) GetTrigger() string {
	return CMD_ME
}

func (me *MeProvider) GetCommand(c *Context) *model.Command {
	return &model.Command{
		Trigger:          CMD_ME,
		AutoComplete:     true,
		AutoCompleteDesc: c.T("api.command_me.desc"),
		AutoCompleteHint: c.T("api.command_me.hint"),
		DisplayName:      c.T("api.command_me.name"),
	}
}

func (me *MeProvider) DoCommand(c *Context, channelId string, message string) *model.CommandResponse {
	userChan := Srv.Store.User().Get(c.Session.UserId)
	var user *model.User
	if ur := <-userChan; ur.Err != nil {
		c.Err = ur.Err
		return nil
	} else {
		user = ur.Data.(*model.User)
	}

	var name = user.Username
	if len(user.Nickname) > 0 {
		name = user.Nickname
	} else if len(user.FirstName) > 0 {
		name = user.FirstName
	}

	return &model.CommandResponse{
		ResponseType: model.COMMAND_RESPONSE_TYPE_IN_CHANNEL,
		Text: "*" + name + " " + message + "*",
		Props: model.StringInterface{
			"class": "action",
		},
	}
}

Make the /me command prefix the action with the invoking user's name, and then assign an "action" class property to alter the appearance of the post.


package main

import "fmt"

func main() {
	loop(100000)
}

func loop(duration int) {
	for i := 0; i < duration; i++ {
		fmt.Println(fmt.Sprintf("Loop number %d", i))
	}
}

package main

import "fmt"

func main() {
	loop(100000)
}

func loop(duration int) {
	for i := 0; i < duration; i++ {
		fmt.Println(fmt.Sprintf("Loop number %d", i+1))
	}
}

Use the same number operation in go as in hack.


// Copyright 2013 Landon Wainwright. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Starts up the blog system using the default values
package main

import (
	"flag"
	"github.com/landonia/simplegoblog/blog"
	"log"
	"os"
)

// Starts a new simple go blog server
func main() {

	// Define flags
	var postsdir, templatesdir, assetsdir string
	flag.StringVar(&postsdir, "pdir", "../posts", "the directory for storing the posts")
	flag.StringVar(&templatesdir, "tdir", "../templates", "the directory containing the templates")
	flag.StringVar(&assetsdir, "adir", "../assets", "the directory containing the assets")
	flag.Parse()

	// Create a new configuration containing the info
	config := &blog.Configuration{Title: "Life thru a Lando", DevelopmentMode: true, Postsdir: postsdir, Templatesdir: templatesdir, Assetsdir: assetsdir}

	// Create a new data structure for storing the data
	b := blog.New(config)

	// Start the blog server
	err := b.Start(":8080")
	if err != nil {
		log.Println(err)
		os.Exit(1)
	}
}

// Copyright 2013 Landon Wainwright. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Starts up the blog system using the default values
package main

import (
	"flag"
	"github.com/landonia/simplegoblog/blog"
	"log"
	"os"
)

// Starts a new simple go blog server
func main() {

	// Define flags
	var postsdir, templatesdir, assetsdir, port string
	flag.StringVar(&postsdir, "pdir", "../posts", "the directory for storing the posts")
	flag.StringVar(&templatesdir, "tdir", "../templates", "the directory containing the templates")
	flag.StringVar(&assetsdir, "adir", "../assets", "the directory containing the assets")
	flag.StringVar(&port, "port", "8080", "the port to run the blog on")
	flag.Parse()

	// Create a new configuration containing the info
	config := &blog.Configuration{Title: "Life thru a Lando", DevelopmentMode: true, Postsdir: postsdir, Templatesdir: templatesdir, Assetsdir: assetsdir}

	// Create a new data structure for storing the data
	b := blog.New(config)

	// Start the blog server
	err := b.Start(port)
	if err != nil {
		log.Println(err)
		os.Exit(1)
	}
}

Allow the port to be defined


package main

import "net/http"

// Wiki represents the entire Wiki, contains the db
type Wiki struct {
	store Storage
}

// NewWiki creates a new Wiki
func NewWiki(s Storage) *Wiki {
	// Setup the wiki.
	w := &Wiki{store: s}

	return w
}

// DB returns the database associated with the handler.
func (w *Wiki) Store() Storage {
	return w.store
}

func (wiki Wiki) Route(w http.ResponseWriter, r *http.Request) {
	queryValues := r.URL.Query()
	path := r.URL.Path
	action := queryValues.Get("action")

	switch {
	case r.PostFormValue("update") == "update":
		wiki.Update(w, r)
	case action == "edit":
		wiki.Edit(w, r)
	default:
		if len(wiki.store.GetPageList(path)) > 0 || len(wiki.store.DirList(path)) > 0 {
			wiki.Dir(w, r)
		} else {
			wiki.Show(w, r)
		}
	}
}

package main

import (
	"net/http"
	"strings"
)

// Wiki represents the entire Wiki, contains the db
type Wiki struct {
	store Storage
}

// NewWiki creates a new Wiki
func NewWiki(s Storage) *Wiki {
	// Setup the wiki.
	w := &Wiki{store: s}

	return w
}

// DB returns the database associated with the handler.
func (w *Wiki) Store() Storage {
	return w.store
}

func (wiki Wiki) Route(w http.ResponseWriter, r *http.Request) {
	queryValues := r.URL.Query()
	path := r.URL.Path
	action := queryValues.Get("action")

	isDir := len(wiki.store.GetPageList(path)) > 0 || len(wiki.store.DirList(path)) > 0

	if isDir && !strings.HasSuffix(path, "/") {
		http.Redirect(w, r, path+"/", http.StatusSeeOther)
		return
	}

	switch {
	case r.PostFormValue("update") == "update":
		wiki.Update(w, r)
	case action == "edit":
		wiki.Edit(w, r)
	case isDir:
		wiki.Dir(w, r)
	default:
		wiki.Show(w, r)
	}
}

Make / required for dirs


package faker

import (
	"math/rand"
	"time"
)

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

	maxDataWithError := float64(rangeSize) + 2*epsilon

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

	return dataInRange
}

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

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

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

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

package faker

import (
	"math/rand"
	"time"
)

// getRandValueInRange creates random value in (range size - error, range size + error)
func getRandValueInRange(rangeSize int, epsilon float64) float64 {
	rand.Seed(time.Now().UTC().UnixNano())

	maxDataWithError := float64(rangeSize) + 2*epsilon

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

	return dataInRange
}

// GenerateAngleVelocity generates random value in (0 - epsilon, 360 + epsilon)
func GenerateAngleVelocity(epsilon float64) float64 {
	return getRandValueInRange(360, epsilon)
}

// GenerateGravityAcceleration generates random value in (0 - epsilon, 1023 + epsilon)
func GenerateGravityAcceleration(epsilon float64) float64 {
	return getRandValueInRange(1023, epsilon)
}

// GenerateTemperature generates random value in (0 - epsilon, 104 + epsilon)
func GenerateTemperature(epsilon float64) float64 {
	return getRandValueInRange(104, epsilon)
}

// GenerateHumidity generates random value in (0 - epsilon, 100 + epsilon)
func GenerateHumidity(epsilon float64) float64 {
	return getRandValueInRange(100, epsilon)
}

Add comments to all functions


package pubsub

type Subscriber interface {
	Subscribe(name, topic string) (<-chan Event, error)
}

func (p *Inmem) Subscribe(name, topic string) (<-chan Event, error) {
	events := make(chan Event)
	sub := subscription{
		name:      name,
		topic:     topic,
		eventChan: events,
	}
	p.mtx.Lock()
	p.subscriptions[topic] = append(p.subscriptions[topic], sub)
	p.mtx.Unlock()

	return events, nil
}

func (p *Inmem) dispatch() {
	for {
		select {
		case ev := <-p.publish:
			for _, sub := range p.subscriptions[ev.Topic] {
				go func(s subscription) { s.eventChan <- ev }(sub)
			}
		}
	}
}

package pubsub

type Subscriber interface {
	Subscribe(name, topic string) (<-chan Event, error)
}

func (p *Inmem) Subscribe(name, topic string) (<-chan Event, error) {
	events := make(chan Event)
	sub := subscription{
		name:      name,
		topic:     topic,
		eventChan: events,
	}
	p.mtx.Lock()
	p.subscriptions[topic] = append(p.subscriptions[topic], sub)
	p.mtx.Unlock()

	return events, nil
}

func (p *Inmem) dispatch() {
	for {
		select {
		case ev := <-p.publish:
			p.mtx.Lock()
			for _, sub := range p.subscriptions[ev.Topic] {
				go func(s subscription) { s.eventChan <- ev }(sub)
			}
			p.mtx.Unlock()
		}
	}
}

Fix race condition in pubsub package.


package chuper

import (
	"net/url"

	"github.com/PuerkitoBio/fetchbot"
)

type Context struct {
	*fetchbot.Context
	C Cache
}

func (c *Context) Cache() Cache {
	return c.C
}

func (c *Context) Queue() *fetchbot.Queue {
	return c.Q
}

func (c *Context) URL() *url.URL {
	return c.Cmd.URL()
}

func (c *Context) SourceURL() *url.URL {
	switch cmd := c.Cmd.(type) {
	case Cmd:
		return cmd.SourceURL()
	default:
		return nil
	}
}

package chuper

import (
	"net/url"

	"github.com/PuerkitoBio/fetchbot"
)

type Context struct {
	*fetchbot.Context
	C Cache
}

func (c *Context) Cache() Cache {
	return c.C
}

func (c *Context) Queue() *fetchbot.Queue {
	return c.Q
}

func (c *Context) URL() *url.URL {
	return c.Cmd.URL()
}

func (c *Context) SourceURL() *url.URL {
	switch cmd := c.Cmd.(type) {
	case Cmd:
		return cmd.SourceURL()
	case CmdBasicAuth:
		return cmd.SourceURL()
	default:
		return nil
	}
}

Add SourceUrl to Context when it's a command with BasicAuth.


package metafora

// CoordinatorContext is the context passed to coordinators by the core
// consumer.
type CoordinatorContext interface {
	Logger
}

// Coordinator is the core interface Metafora uses to discover, claim, and
// tasks as well as receive commands.
type Coordinator interface {
	// Init is called once by the consumer to provide a Logger to Coordinator
	// implementations.
	Init(CoordinatorContext)

	// Watch should do a blocking watch on the broker and return a task ID that
	// can be claimed.
	Watch() (taskID string, err error)

	// Claim is called by the Consumer when a Balancer has determined that a task
	// ID can be claimed. Claim returns false if another consumer has already
	// claimed the ID.
	Claim(taskID string) bool

	// Release a task for other consumers to claim.
	Release(taskID string)

	// Command blocks until a command for this node is received from the broker
	// by the coordinator.
	Command() (cmd string, err error)

	// Close indicates the Coordinator should stop watching and receiving
	// commands. It is called during Consumer.Shutdown().
	Close() error
}

package metafora

// CoordinatorContext is the context passed to coordinators by the core
// consumer.
type CoordinatorContext interface {
	Logger
}

// Coordinator is the core interface Metafora uses to discover, claim, and
// tasks as well as receive commands.
type Coordinator interface {
	// Init is called once by the consumer to provide a Logger to Coordinator
	// implementations.
	Init(CoordinatorContext)

	// Watch should do a blocking watch on the broker and return a task ID that
	// can be claimed. Watch must return ("", nil) when Close is called.
	Watch() (taskID string, err error)

	// Claim is called by the Consumer when a Balancer has determined that a task
	// ID can be claimed. Claim returns false if another consumer has already
	// claimed the ID.
	Claim(taskID string) bool

	// Release a task for other consumers to claim.
	Release(taskID string)

	// Command blocks until a command for this node is received from the broker
	// by the coordinator. Command must return ("", nil) when Close is called.
	Command() (cmd string, err error)

	// Close indicates the Coordinator should stop watching and receiving
	// commands. It is called during Consumer.Shutdown().
	Close() error
}

Make return values for Watch/Command clear in interface docs


package env

import "io/ioutil"

func WriteScriptTo(fileName string, contents string) error {
	return ioutil.WriteFile(fileName, []byte(contents), 0744)
}

func ExecuteScript(scriptPath string) (string, error) {
	return ExecuteCommand("bash", scriptPath)
}

package env

import "io/ioutil"

func WriteScriptTo(filename string, contents string) error {
	return ioutil.WriteFile(filename, []byte(contents), 0744)
}

func ExecuteScript(scriptPath string) (string, error) {
	return ExecuteCommand("bash", scriptPath)
}

Change fileName to filename in pkg/env


package service

import "fmt"

//Builder is a simple implementation of ContainerBuilder
type Builder struct {
	definitions []Definition
}

//Insert a new definition into the Builder
func (b *Builder) Insert(def Definition) {
	b.definitions = append(b.definitions, def)
}

//Build builds the container once all definitions have been place in it
//dependencies need to be inserted in order for now
func (b *Builder) Build() (Container, error) {
	numDefs := len(b.definitions)
	servs := make(map[string]interface{}, numDefs)
	for _, def := range b.definitions {
		numDeps := len(def.Dependencies)
		deps := make(map[string]interface{}, numDeps)
		for _, name := range def.Dependencies {
			dep, ok := servs[name]
			if !ok {
				return nil, fmt.Errorf("service: Could not find"+
					"dependency %q for service %q. Please make"+
					"sure to insert them in order", name, def.Name)
			}
			deps[name] = dep
		}
		service, err := def.Initializer.Init(deps, def.Configuration)
		if err != nil {
			return nil, err
		}
		servs[def.Name] = service
	}

	return SimpleContainer{
		services: servs,
	}, nil
}

package service

import "fmt"

//Builder is a simple implementation of ContainerBuilder
type Builder struct {
	definitions []Definition
}

//Insert a new definition into the Builder
func (b *Builder) Insert(def Definition) {
	b.definitions = append(b.definitions, def)
}

//Build builds the container once all definitions have been place in it
//dependencies need to be inserted in order for now
func (b *Builder) Build() (Container, error) {
	numDefs := len(b.definitions)
	servs := make(map[string]interface{}, numDefs)
	for _, def := range b.definitions {
		numDeps := len(def.Dependencies)
		deps := make(map[string]interface{}, numDeps)
		for _, name := range def.Dependencies {
			dep, ok := servs[name]
			if !ok {
				return nil, fmt.Errorf("service: Could not find "+
					"dependency %q for service %q. Please make "+
					"sure to insert them in order", name, def.Name)
			}
			deps[name] = dep
		}
		service, err := def.Initializer.Init(deps, def.Configuration)
		if err != nil {
			return nil, err
		}
		servs[def.Name] = service
	}

	return SimpleContainer{
		services: servs,
	}, nil
}

Fix spaces in error message


// +build gofuzz

package types

import "encoding/json"

func FuzzPlaceCountainer(data []byte) int {
	var pc = &PlaceCountainer{}

	// Let's unmarshal, this is not our job so "bleh"
	err := json.Unmarshal(data, pc)
	if err != nil {
		return 0
	}

	// Now that it is unmarshalled, let's test the Place method !
	place, err := pc.Place()
	if err != nil {
		return 0
	}

	_ = place.PlaceName()
	_ = place.PlaceID()
	_ = place.PlaceType()

	return 1
}

// +build gofuzz

package types

import "encoding/json"

func FuzzPlaceCountainer(data []byte) int {
	var pc = &PlaceCountainer{}

	// Let's unmarshal, this is not our job so "bleh"
	err := json.Unmarshal(data, pc)
	if err != nil {
		return 0
	}

	// Now that it is unmarshalled, let's test the Place method !
	place, err := pc.Place()
	if err != nil {
		return 0
	}

	_ = place.PlaceName()
	_ = place.PlaceID()
	_ = place.PlaceType()
	_ = place.String()

	return 1
}

Add String method testing to FuzzJourney


package qemuengine

import (
	"fmt"

	"github.com/taskcluster/taskcluster-worker/runtime"
	"github.com/taskcluster/taskcluster-worker/runtime/fetcher"
)

// A fetcher for downloading images.
var imageFetcher = fetcher.Combine(
	// Allow fetching images from URL
	fetcher.URL,
	// Allow fetching images from queue artifacts
	fetcher.Artifact,
)

type fetchImageContext struct {
	*runtime.TaskContext
}

func (c fetchImageContext) Progress(description string, percent float64) {
	c.Log(fmt.Sprintf("Fetching image: %s - %f %%", description, percent))
}

package qemuengine

import (
	"fmt"

	"github.com/taskcluster/taskcluster-worker/runtime"
	"github.com/taskcluster/taskcluster-worker/runtime/fetcher"
)

// A fetcher for downloading images.
var imageFetcher = fetcher.Combine(
	// Allow fetching images from URL
	fetcher.URL,
	// Allow fetching images from queue artifacts
	fetcher.Artifact,
	// Allow fetching images from queue referenced by index namespace
	fetcher.Index,
)

type fetchImageContext struct {
	*runtime.TaskContext
}

func (c fetchImageContext) Progress(description string, percent float64) {
	c.Log(fmt.Sprintf("Fetching image: %s - %f %%", description, percent))
}

Allow referencing QEMU images by index namespace and artifact name


package crypto

import (
	"io"

	boshsys "github.com/cloudfoundry/bosh-utils/system"
)

type Digest interface {
	Verify(io.Reader) error
	VerifyFilePath(filePath string, fs boshsys.FileSystem) error
	Algorithm() Algorithm
	String() string
}

var _ Digest = digestImpl{}

type Algorithm interface {
	CreateDigest(io.Reader) (Digest, error)
	Name() string
}

var _ Algorithm = algorithmSHAImpl{}
var _ Algorithm = unknownAlgorithmImpl{}

package crypto

import (
	"io"

	boshsys "github.com/cloudfoundry/bosh-utils/system"
)

type Digest interface {
	Verify(io.Reader) error
	VerifyFilePath(filePath string, fs boshsys.FileSystem) error
	Algorithm() Algorithm
	String() string
}

var _ Digest = digestImpl{}
var _ Digest = MultipleDigest{}

type Algorithm interface {
	CreateDigest(io.Reader) (Digest, error)
	Name() string
}

var _ Algorithm = algorithmSHAImpl{}
var _ Algorithm = unknownAlgorithmImpl{}

Update crypto interface to ensure MultipleDigest conforms


package gister

import (
	"bytes"
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"strings"
)

func Test_main() {

}

Move test to package main to make gister go gettable


// Package rules contains specific rules as subpackages to check a Makefile against
package rules

import (
	"github.com/mrtazz/checkmake/parser"
)

// Rule is the type of a rule function
type Rule interface {
	Name() string
	Description() string
	Run(parser.Makefile, RuleConfig) RuleViolationList
}

// RuleViolation represents a basic validation failure
type RuleViolation struct {
	Rule       string
	Violation  string
	LineNumber int
}

// RuleViolationList is a list of Violation types and the return type of a
// Rule function
type RuleViolationList []RuleViolation

// RuleConfig is a simple string/string map to hold key/value configuration
// for rules.
type RuleConfig map[string]string

// RuleConfigMap is a map that stores RuleConfig maps keyed by the rule name
type RuleConfigMap map[string]RuleConfig

// RuleRegistry is the type to hold rules keyed by their name
type RuleRegistry map[string]Rule

var (
	ruleRegistry RuleRegistry
)

func init() {
	ruleRegistry = make(RuleRegistry)
}

// RegisterRule let's you register a rule for inclusion in the validator
func RegisterRule(r Rule) {
	ruleRegistry[r.Name()] = r
}

// GetRegisteredRules returns the internal ruleRegistry
func GetRegisteredRules() RuleRegistry {
	return ruleRegistry
}

// Package rules contains specific rules as subpackages to check a Makefile against
package rules

import (
	"github.com/mrtazz/checkmake/parser"
)

// Rule is the type of a rule function
type Rule interface {
	Name() string
	Description() string
	Run(parser.Makefile, RuleConfig) RuleViolationList
}

// RuleViolation represents a basic validation failure
type RuleViolation struct {
	Rule       string
	Violation  string
	FileName   string
	LineNumber int
}

// RuleViolationList is a list of Violation types and the return type of a
// Rule function
type RuleViolationList []RuleViolation

// RuleConfig is a simple string/string map to hold key/value configuration
// for rules.
type RuleConfig map[string]string

// RuleConfigMap is a map that stores RuleConfig maps keyed by the rule name
type RuleConfigMap map[string]RuleConfig

// RuleRegistry is the type to hold rules keyed by their name
type RuleRegistry map[string]Rule

var (
	ruleRegistry RuleRegistry
)

func init() {
	ruleRegistry = make(RuleRegistry)
}

// RegisterRule let's you register a rule for inclusion in the validator
func RegisterRule(r Rule) {
	ruleRegistry[r.Name()] = r
}

// GetRegisteredRules returns the internal ruleRegistry
func GetRegisteredRules() RuleRegistry {
	return ruleRegistry
}

Add FileName string to RuleViolation struct


package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"os"
)

var (
	format string
)

func main() {
	var jsonObjects []map[string]interface{}

	enc := json.NewDecoder(os.Stdin)
	_ = enc.Decode(&jsonObjects)

	for _, obj := range jsonObjects {
		if val, ok := obj[format]; ok {
			fmt.Println(val)
		}
	}
}

func init() {
	flag.StringVar(&format, "path", ".", "Argument to extract from input")
	flag.Parse()
	if args := flag.Args(); len(args) > 0 {
		format = args[0]
	}
}

package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"os"
	"text/template"
)

const (
	baseTemplate = "{{ if .errors }}{{ range .errors }}{{ . }}\n{{ end }}{{ else }}{{ range .columns }}{{ bold .name }}\t{{end}}\n{{ range .data }}{{ range . }}{{ . }}\t{{ end }}\n{{ end }}{{ end }}"

	BoldCode  = "\033[1m"
	ResetCode = "\033[0m"

)

var (
	format string
)

func bold(s string) string {
	return fmt.Sprintf("%s%s%s", BoldCode, s, ResetCode)
}

func main() {
	var jsonObjects map[string]interface{}

	enc := json.NewDecoder(os.Stdin)
	_ = enc.Decode(&jsonObjects)

	t, err := template.New("json-formatter").
		Funcs(template.FuncMap{"bold": bold}).
		Parse(baseTemplate)
	if err != nil {
		panic(err)
	}
	err = t.Execute(os.Stdout, jsonObjects)
	if err != nil {
		panic(err)
	}
}

func init() {
	flag.StringVar(&format, "path", ".", "Argument to extract from input")
	flag.Parse()
	if args := flag.Args(); len(args) > 0 {
		format = args[0]
	}
}

Use a go template (buggy b/c maps are unordered)


package commands

import (
	"log"
	"os"
	"os/exec"
	"path"
	"syscall"

	"github.com/khlieng/name_pending/Godeps/_workspace/src/github.com/spf13/cobra"

	"github.com/khlieng/name_pending/storage"
)

var (
	editors = []string{"nano", "notepad", "vi", "emacs"}

	configCmd = &cobra.Command{
		Use:   "config",
		Short: "Edit config file",
		Run: func(cmd *cobra.Command, _ []string) {
			if editor := findEditor(); editor != "" {
				args := []string{editor, path.Join(storage.AppDir, "config.toml")}
				syscall.Exec(editor, args, os.Environ())
			} else {
				log.Println("Unable to locate editor")
			}
		},
	}
)

func findEditor() string {
	if editor := os.Getenv("EDITOR"); editor != "" {
		editor, err := exec.LookPath(editor)
		if err == nil {
			return editor
		}
	}

	for _, editor := range editors {
		editor, err := exec.LookPath(editor)
		if err == nil {
			return editor
		}
	}

	return ""
}

package commands

import (
	"log"
	"os"
	"os/exec"
	"path"

	"github.com/khlieng/name_pending/Godeps/_workspace/src/github.com/spf13/cobra"

	"github.com/khlieng/name_pending/storage"
)

var (
	editors = []string{"nano", "notepad", "vi", "emacs"}

	configCmd = &cobra.Command{
		Use:   "config",
		Short: "Edit config file",
		Run: func(cmd *cobra.Command, args []string) {
			if editor := findEditor(); editor != "" {
				process := exec.Command(editor, path.Join(storage.AppDir, "config.toml"))
				process.Stdin = os.Stdin
				process.Stdout = os.Stdout
				process.Stderr = os.Stderr
				process.Run()
			} else {
				log.Println("Unable to locate editor")
			}
		},
	}
)

func findEditor() string {
	if editor := os.Getenv("EDITOR"); editor != "" {
		editor, err := exec.LookPath(editor)
		if err == nil {
			return editor
		}
	}

	for _, editor := range editors {
		editor, err := exec.LookPath(editor)
		if err == nil {
			return editor
		}
	}

	return ""
}

Use os/exec to run editor


package commands

import (
	"fmt"
	"github.com/codegangsta/cli"
	"github.com/brooklyncentral/brooklyn-cli/api/application"
	"github.com/brooklyncentral/brooklyn-cli/command_metadata"
	"github.com/brooklyncentral/brooklyn-cli/net"
	"github.com/brooklyncentral/brooklyn-cli/scope"
)

type Deploy struct {
	network *net.Network
}

func NewDeploy(network *net.Network) (cmd *Deploy) {
	cmd = new(Deploy)
	cmd.network = network
	return
}

func (cmd *Deploy) Metadata() command_metadata.CommandMetadata {
	return command_metadata.CommandMetadata{
		Name:        "deploy",
		Description: "Create a new brooklyn application from the supplied YAML",
		Usage:       "BROOKLYN_NAME [ SCOPE ] deploy FILEPATH",
		Flags:       []cli.Flag{},
	}
}

func (cmd *Deploy) Run(scope scope.Scope, c *cli.Context) {
	create := application.Create(cmd.network, c.Args().First())
	fmt.Println(create)
}

package commands

import (
	"fmt"
	"github.com/codegangsta/cli"
	"github.com/brooklyncentral/brooklyn-cli/api/application"
	"github.com/brooklyncentral/brooklyn-cli/command_metadata"
	"github.com/brooklyncentral/brooklyn-cli/net"
	"github.com/brooklyncentral/brooklyn-cli/scope"
)

type Deploy struct {
	network *net.Network
}

func NewDeploy(network *net.Network) (cmd *Deploy) {
	cmd = new(Deploy)
	cmd.network = network
	return
}

func (cmd *Deploy) Metadata() command_metadata.CommandMetadata {
	return command_metadata.CommandMetadata{
		Name:        "deploy",
		Description: "Deploy a new brooklyn application from the supplied YAML",
		Usage:       "BROOKLYN_NAME [ SCOPE ] deploy FILEPATH",
		Flags:       []cli.Flag{},
	}
}

func (cmd *Deploy) Run(scope scope.Scope, c *cli.Context) {
	create := application.Create(cmd.network, c.Args().First())
	fmt.Println(create)
}

Fix description in help text


package main

import (
	"errors"
)

type row []int
type matrix []row

// check returns an error if matrix is irregular
// and cannot be transposed.
func (m matrix) check() (err error) {
	l := len(m[0])
	for _, x := range m {
		// as long as first line is the longest we're fine
		if len(x) > l {
			return errors.New("first lane is not longest")
		}
	}
	return nil
}

func (m matrix) transpose() matrix {
	r := make(matrix, len(m[0]))
	for x := range r {
		r[x] = make(row, len(m))
	}
	for y, s := range m {
		for x, e := range s {
			r[x][y] = e
		}
	}
	return r
}

package main

import (
	"errors"
)

type row []int
type matrix []row

func (r1 row) eq(r2 row) bool {
	if len(r1) != len(r2) {
		return false
	}
	for i := range r1 {
		if r1[i] != r2[i] {
			return false
		}
	}
	return true
}

// check returns an error if matrix is irregular
// and cannot be transposed.
func (m matrix) check() (err error) {
	l := len(m[0])
	for _, x := range m {
		// as long as first line is the longest we're fine
		if len(x) > l {
			return errors.New("first lane is not longest")
		}
	}
	return nil
}

func (m matrix) transpose() matrix {
	r := make(matrix, len(m[0]))
	for x := range r {
		r[x] = make(row, len(m))
	}
	for y, s := range m {
		for x, e := range s {
			r[x][y] = e
		}
	}
	return r
}

Add row equality method, at least needed for tests


package meta

import (
	"errors"
	"io"
)

// VerifyPadding verifies that the padding metadata block only contains 0 bits.
// The provided io.Reader should limit the amount of data that can be read to
// header.Length bytes.
func VerifyPadding(r io.Reader) (err error) {
	// Verify up to 4 kb of padding each iteration.
	buf := make([]byte, 4096)
	for {
		n, err := r.Read(buf)
		if err != nil {
			if err == io.EOF {
				break
			}
			return err
		}
		if !isAllZero(buf[:n]) {
			return errors.New("meta.VerifyPadding: invalid padding; must contain only zeroes")
		}
	}
	return nil
}

/// ### [ note ] ###
///    - Might trigger unnecessary errors.
/// ### [/ note ] ###

// isAllZero returns true if the value of each byte in the provided slice is 0,
// and false otherwise.
func isAllZero(buf []byte) bool {
	for _, b := range buf {
		if b != 0 {
			return false
		}
	}
	return true
}

package meta

import (
	"errors"
	"io"
)

// VerifyPadding verifies that the padding metadata block only contains 0 bits.
// The provided io.Reader should limit the amount of data that can be read to
// header.Length bytes.
func VerifyPadding(r io.Reader) (err error) {
	// Verify up to 4 kb of padding each iteration.
	var buf [4096]byte
	for {
		n, err := r.Read(buf[:])
		if err != nil {
			if err == io.EOF {
				break
			}
			return err
		}
		if !isAllZero(buf[:n]) {
			return errors.New("meta.VerifyPadding: invalid padding; must contain only zeroes")
		}
	}
	return nil
}

// isAllZero returns true if the value of each byte in the provided slice is 0,
// and false otherwise.
func isAllZero(buf []byte) bool {
	for _, b := range buf {
		if b != 0 {
			return false
		}
	}
	return true
}

Use array instead of slice in VerifyPadding.


/*
Copyright 2016 The Fission 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 router

import (
	"fmt"
	"testing"
	"time"

	"github.com/fission/fission"
)

func TestRouter(t *testing.T) {
	fmap := makeFunctionServiceMap(0)
	fn := &fission.Metadata{Name: "foo", Uid: "xxx"}

	testResponseString := "hi"
	testServiceUrl := createBackendService(testResponseString)

	fmap.assign(fn, testServiceUrl)

	triggers := makeHTTPTriggerSet(fmap, nil, nil)
	triggerUrl := "/foo"
	triggers.triggers = append(triggers.triggers, fission.HTTPTrigger{UrlPattern: triggerUrl, Function: *fn})

	port := 4242
	go serve(port, triggers)
	time.Sleep(100 * time.Millisecond)

	testUrl := fmt.Sprintf("http://localhost:%v%v", port, triggerUrl)
	testRequest(testUrl, testResponseString)
}

/*
Copyright 2016 The Fission 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 router

import (
	"fmt"
	"testing"
	"time"

	"github.com/fission/fission"
)

func TestRouter(t *testing.T) {
	fmap := makeFunctionServiceMap(0)
	fn := &fission.Metadata{Name: "foo", Uid: "xxx"}

	testResponseString := "hi"
	testServiceUrl := createBackendService(testResponseString)

	fmap.assign(fn, testServiceUrl)

	triggers := makeHTTPTriggerSet(fmap, nil, nil)
	triggerUrl := "/foo"
	triggers.triggers = append(triggers.triggers, fission.HTTPTrigger{UrlPattern: triggerUrl, Function: *fn, Method: "GET"})

	port := 4242
	go serve(port, triggers)
	time.Sleep(100 * time.Millisecond)

	testUrl := fmt.Sprintf("http://localhost:%v%v", port, triggerUrl)
	testRequest(testUrl, testResponseString)
}

Update test to include method


package lexers

import (
	. "github.com/alecthomas/chroma/v2" // nolint
)

// FortranFixed lexer.
var FortranFixed = Register(MustNewLexer(
	&Config{
		Name:            "FortranFixed",
		Aliases:         []string{"fortranfixed"},
		Filenames:       []string{"*.f", "*.F"},
		MimeTypes:       []string{"text/x-fortran"},
		NotMultiline:    true,
		CaseInsensitive: true,
	},
	func() Rules {
		return Rules{
			"root": {
				{`[C*].*\n`, Comment, nil},
				{`#.*\n`, CommentPreproc, nil},
				{`[\t ]*!.*\n`, Comment, nil},
				{`(.{5})`, NameLabel, Push("cont-char")},
				{`.*\n`, Using("Fortran"), nil},
			},
			"cont-char": {
				{` `, Text, Push("code")},
				{`0`, Comment, Push("code")},
				{`.`, GenericStrong, Push("code")},
			},
			"code": {
				{`(.{66})(.*)(\n)`, ByGroups(Using("Fortran"), Comment, Text), Push("root")},
				{`.*\n`, Using("Fortran"), Push("root")},
				Default(Push("root")),
			},
		}
	},
))

package lexers

import (
	. "github.com/alecthomas/chroma/v2" // nolint
)

// FortranFixed lexer.
var FortranFixed = Register(MustNewLexer(
	&Config{
		Name:            "FortranFixed",
		Aliases:         []string{"fortranfixed"},
		Filenames:       []string{"*.f", "*.F"},
		MimeTypes:       []string{"text/x-fortran"},
		NotMultiline:    true,
		CaseInsensitive: true,
	},
	func() Rules {
		return Rules{
			"root": {
				{`[C*].*\n`, Comment, nil},
				{`#.*\n`, CommentPreproc, nil},
				{` {0,4}!.*\n`, Comment, nil},
				{`(.{5})`, NameLabel, Push("cont-char")},
				{`.*\n`, Using("Fortran"), nil},
			},
			"cont-char": {
				{` `, TextWhitespace, Push("code")},
				{`.`, GenericStrong, Push("code")},
			},
			"code": {
				{`(.{66})(.*)(\n)`, ByGroups(Using("Fortran"), Comment, TextWhitespace), Push("root")},
				{`(.*)(!.*)(\n)`, ByGroups(Using("Fortran"), Comment, TextWhitespace), Push("root")},
				{`(.*)(\n)`, ByGroups(Using("Fortran"), TextWhitespace), Push("root")},
				Default(Push("root")),
			},
		}
	},
))

Revise fortran fixed format lexer to recognize comments using the "!" mark in columns 1-5 and columns > 6. Remove incorrect "0" label being a comment.


package client

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

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)
	}
	if resp.StatusCode != http.StatusOK {
		return nil, errors.Errorf("Authenticate request failed with status %v.", resp.StatusCode)
	}

	authResp := &api.AuthenticateResponse{}
	if err := json.NewDecoder(resp.Body).Decode(&authResp); err != nil {
		return nil, errors.Wrap(err, 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"
)

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)
	}

	authResp := &api.AuthenticateResponse{}
	if err := json.NewDecoder(resp.Body).Decode(&authResp); 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, authResp)
	}

	return authResp, nil
}

Improve error reporting in client.


package common

const (
	THRUST_VERSION    = "0.7.5"
	THRUST_GO_VERSION = "0.2.7"
)

package common

const (
	THRUST_VERSION    = "0.7.5"
	THRUST_GO_VERSION = "0.3.0"
)

Update Version to 0.3.0 - Stable Windows


package etcdutil

import (
	"path"

	"github.com/coreos/go-etcd/etcd"
)

// Creates the given dir (and all of its parent directories if they don't
// already exist). Will not return an error if the given directory already
// exists
func MkDirP(ec *etcd.Client, dir string) error {
    parts := make([]string, 0, 4)
    for {
        parts = append(parts, dir)
        dir = path.Dir(dir)
        if dir == "/" {
            break
        }
    }

    for i := range parts {
        ai := len(parts) - i - 1
        _, err := ec.CreateDir(parts[ai], 0)
        if err != nil {
			if err.(*etcd.EtcdError).ErrorCode == 105 {
				return nil
			}
            return err
        }
    }
    return nil
}

// Returns the contents of a directory as a list of absolute paths
func Ls(ec *etcd.Client, dir string) ([]string, error) {
    r, err := ec.Get(dir, false, false)
    if err != nil {
        return nil, err
    }

    dirNode := r.Node
    ret := make([]string, len(dirNode.Nodes))
    for i, node := range dirNode.Nodes {
        ret[i] = node.Key
    }

    return ret, nil
}

package etcdutil

import (
	"path"

	"github.com/coreos/go-etcd/etcd"
)

// Creates the given dir (and all of its parent directories if they don't
// already exist). Will not return an error if the given directory already
// exists
func MkDirP(ec *etcd.Client, dir string) error {
    parts := make([]string, 0, 4)
    for {
        parts = append(parts, dir)
        dir = path.Dir(dir)
        if dir == "/" {
            break
        }
    }

    for i := range parts {
        ai := len(parts) - i - 1
        _, err := ec.CreateDir(parts[ai], 0)
        if err != nil && err.(*etcd.EtcdError).ErrorCode != 105 {
            return err
        }
    }
    return nil
}

// Returns the contents of a directory as a list of absolute paths
func Ls(ec *etcd.Client, dir string) ([]string, error) {
    r, err := ec.Get(dir, false, false)
    if err != nil {
        return nil, err
    }

    dirNode := r.Node
    ret := make([]string, len(dirNode.Nodes))
    for i, node := range dirNode.Nodes {
        ret[i] = node.Key
    }

    return ret, nil
}

Revert "make MkDirP a bit faster"


package irc

import(
	"testing"
)

func TestParseMessage(t *testing.T) {
	msg_string := ":kyle!~kyle@localhost PRIVMSG #tenyks :tenyks: messages are awesome"
	msg := ParseMessage(msg_string)
	if msg == nil {
		t.Error("Expected", Message{}, "got", msg)
	}

	if msg.Command != "PRIVMSG" {
		t.Error("Expected", Message{}, "got", msg)
	}

	if msg.Trail != "tenyks: messages are awesome" {
		t.Error("Expected", "tenyks: messages are awesome", "got", msg)
	}
}

package irc

import(
	"testing"
)

func TestParseMessage(t *testing.T) {
	msg_string := ":kyle!~kyle@localhost PRIVMSG #tenyks :tenyks: messages are awesome"
	msg := ParseMessage(msg_string)
	if msg == nil {
		t.Error("Expected", Message{}, "got", msg)
	}

	if msg.Command != "PRIVMSG" {
		t.Error("Expected", Message{}, "got", msg)
	}

	if msg.Trail != "tenyks: messages are awesome" {
		t.Error("Expected", "tenyks: messages are awesome", "got", msg)
	}

	if msg.Params[0] != "#tenyks" {
		t.Error("Expected", "#tenyks", "got", msg.Params[0])
	}
}

Test params for things like target


// (c) Copyright IBM Corp. 2021
// (c) Copyright Instana Inc. 2020

package internal_test

import (
	"sync/atomic"
	"testing"
	"time"

	"github.com/instana/go-sensor/autoprofile/internal"
	"github.com/instana/testify/assert"
)

func TestTimer_Stop(t *testing.T) {
	var fired int64
	timer := internal.NewTimer(0, 60*time.Millisecond, func() {
		atomic.AddInt64(&fired, 1)
	})

	time.Sleep(100 * time.Millisecond)
	timer.Stop()

	assert.EqualValues(t, 1, atomic.LoadInt64(&fired))

	time.Sleep(200 * time.Millisecond)
	assert.EqualValues(t, 1, atomic.LoadInt64(&fired))
}

func TestTimer_Sleep_Stopped(t *testing.T) {
	timer := internal.NewTimer(20*time.Millisecond, 0, func() {
		t.Error("stopped timer has fired")
	})

	timer.Stop()
	time.Sleep(30 * time.Millisecond)
}

// (c) Copyright IBM Corp. 2021
// (c) Copyright Instana Inc. 2020

package internal_test

import (
	"sync/atomic"
	"testing"
	"time"

	"github.com/instana/go-sensor/autoprofile/internal"
	"github.com/instana/testify/assert"
)

func TestTimer_Stop_Restart(t *testing.T) {
	var fired int64
	timer := internal.NewTimer(0, 60*time.Millisecond, func() {
		atomic.AddInt64(&fired, 1)
	})

	time.Sleep(100 * time.Millisecond)
	timer.Stop()

	assert.EqualValues(t, 1, atomic.LoadInt64(&fired))

	time.Sleep(200 * time.Millisecond)
	assert.EqualValues(t, 1, atomic.LoadInt64(&fired), "a stopped timer should not be restarted")
}

func TestTimer_Sleep_Stopped(t *testing.T) {
	timer := internal.NewTimer(20*time.Millisecond, 0, func() {
		t.Error("stopped timer has fired")
	})

	timer.Stop()
	time.Sleep(30 * time.Millisecond)
}

Make the test failure message for timer restart more explicit


package command

import (
	"os"
	"strings"

	"github.com/hashicorp/go-plugin"

	"github.com/hashicorp/nomad/client/driver"
)

type SyslogPluginCommand struct {
	Meta
}

func (e *SyslogPluginCommand) Help() string {
	helpText := `
	This is a command used by Nomad internally to launch a syslog collector"
	`
	return strings.TrimSpace(helpText)
}

func (s *SyslogPluginCommand) Synopsis() string {
	return "internal - lanch a syslog collector plugin"
}

func (s *SyslogPluginCommand) Run(args []string) int {
	if len(args) == 0 {
		s.Ui.Error("log output file isn't provided")
	}
	logFileName := args[0]
	stdo, err := os.OpenFile(logFileName, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666)
	if err != nil {
		s.Ui.Error(err.Error())
		return 1
	}
	plugin.Serve(&plugin.ServeConfig{
		HandshakeConfig: driver.HandshakeConfig,
		Plugins:         driver.GetPluginMap(stdo),
	})

	return 0
}

package command

import (
	"os"
	"strings"

	"github.com/hashicorp/go-plugin"

	"github.com/hashicorp/nomad/client/driver"
)

type SyslogPluginCommand struct {
	Meta
}

func (e *SyslogPluginCommand) Help() string {
	helpText := `
	This is a command used by Nomad internally to launch a syslog collector"
	`
	return strings.TrimSpace(helpText)
}

func (s *SyslogPluginCommand) Synopsis() string {
	return "internal - lanch a syslog collector plugin"
}

func (s *SyslogPluginCommand) Run(args []string) int {
	if len(args) == 0 {
		s.Ui.Error("log output file isn't provided")
		return 1
	}
	logFileName := args[0]
	stdo, err := os.OpenFile(logFileName, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666)
	if err != nil {
		s.Ui.Error(err.Error())
		return 1
	}
	plugin.Serve(&plugin.ServeConfig{
		HandshakeConfig: driver.HandshakeConfig,
		Plugins:         driver.GetPluginMap(stdo),
	})

	return 0
}

Return when nomad syslog command has invalid number of argument


package main

import (
	"fmt"
	"net/http"

	"github.com/ymichael/sessions"
	"github.com/zenazn/goji"
	"github.com/zenazn/goji/web"
)

var (
	Sessions = sessions.NewSessionOptions("thisismysecret.", &sessions.MemoryStore{})
)

func hello(c web.C, w http.ResponseWriter, r *http.Request) {
	x := Sessions.GetSession(&c)
	if val, ok := x["count"]; ok {
		x["count"] = val.(int) + 1
	} else {
		x["count"] = 1
	}
	fmt.Fprintf(w, "Hello, %d!", x["count"])
}

func main() {
	goji.Use(Sessions.Middleware())
	goji.Get("/", hello)
	goji.Serve()
}

package main

import (
	"fmt"
	"net/http"

	"github.com/ymichael/sessions"
	"github.com/zenazn/goji"
	"github.com/zenazn/goji/web"
)

var (
	Sessions = sessions.NewSessionOptions("thisismysecret.", &sessions.MemoryStore{})
)

func hello(c web.C, w http.ResponseWriter, r *http.Request) {
	x := Sessions.GetSessionObject(&c)
	if val, ok := x["count"]; ok {
		x["count"] = val.(int) + 1
	} else {
		x["count"] = 1
	}
	fmt.Fprintf(w, "Hello, %d!", x["count"])
}

func destroy(c web.C, w http.ResponseWriter, r *http.Request) {
	Sessions.DestroySession(&c, w)
	http.Redirect(w, r, "/", 301)
}

func main() {
	goji.Use(Sessions.Middleware())
	goji.Get("/", hello)
	goji.Get("/destroy", destroy)
	goji.Serve()
}

Add ability to delete sessions.


package state

import ()

type actionDoc struct {
	Id      string `bson:"_id"`
	Name    string
	Payload map[string]interface{}
}

// Action represents an instruction to do some "action" and is expected to match
// an action definition in a charm.
type Action struct {
	st  *State
	doc actionDoc
}

func newAction(st *State, adoc actionDoc) *Action {
	return &Action{
		st:  st,
		doc: adoc,
	}
}

// Name returns the name of the Action
func (a *Action) Name() string {
	return a.doc.Name
}

// Id returns the id of the Action
func (a *Action) Id() string {
	return a.doc.Id
}

// Payload will contain a structure representing arguments or parameters to
// an action, and is expected to be validated by the Unit using the Charm
// definition of the Action
func (a *Action) Payload() map[string]interface{} {
	return a.doc.Payload
}

package state

import ()

type actionDoc struct {
	Id string `bson:"_id"`
	//Name    string
	//Payload map[string]interface{}
}

// Action represents an instruction to do some "action" and is expected to match
// an action definition in a charm.
type Action struct {
	st  *State
	doc actionDoc
}

func newAction(st *State, adoc actionDoc) *Action {
	return &Action{
		st:  st,
		doc: adoc,
	}
}

// Name returns the name of the Action
//func (a *Action) Name() string {
//	return a.doc.Name
//}

// Id returns the id of the Action
func (a *Action) Id() string {
	return a.doc.Id
}

// Payload will contain a structure representing arguments or parameters to
// an action, and is expected to be validated by the Unit using the Charm
// definition of the Action
//func (a *Action) Payload() map[string]interface{} {
//	return a.doc.Payload
//}

Comment Action members 'Name', and 'Payload', pending clarification on these


package nativesequence

import (
	"testing"
	"github.com/johnny-morrice/godelbrot/base"
	"github.com/johnny-morrice/godelbrot/nativebase"
)

func TestSequence(t *testing.T) {
	if testing.Short() {
		panic("nativesequence testing impossible in short mode")
	}
	const iterateLimit = 10
	app := &nativebase.MockRenderApplication{
		MockRenderApplication: base.MockRenderApplication{
			PictureWidth: 10,
			PictureHeight: 10,
			Base: base.BaseConfig{DivergeLimit: 4.0, IterateLimit: iterateLimit},
		},
	}
	app.PlaneMin = complex(0.0, 0.0)
	app.PlaneMax = complex(10.0, 10.0)
	numerics := Make(app)
	out := numerics.Sequence()

	const expectedCount = 100
	actualArea := numerics.Area()

	if expectedCount != actualArea {
		t.Error("Expected area of", expectedCount,
			"but received", actualArea)
	}

	members := make([]base.PixelMember, actualArea)

	i := 0
	for point := range out {
		members[i] = point
		i++
	}
	actualCount := len(members)

	if expectedCount != actualCount {
		t.Error("Expected", expectedCount, "members but there were", actualCount)
	}
}

package nativesequence

import (
	"testing"
	"github.com/johnny-morrice/godelbrot/base"
	"github.com/johnny-morrice/godelbrot/nativebase"
)

func TestSequence(t *testing.T) {
	if testing.Short() {
		panic("nativesequence testing impossible in short mode")
	}
	const iterateLimit = 10
	app := &nativebase.MockRenderApplication{
		MockRenderApplication: base.MockRenderApplication{
			PictureWidth: 10,
			PictureHeight: 10,
			Base: base.BaseConfig{DivergeLimit: 4.0, IterateLimit: iterateLimit},
		},
	}
	app.PlaneMin = complex(0.0, 0.0)
	app.PlaneMax = complex(10.0, 10.0)
	numerics := Make(app)
	out := numerics.Sequence()

	const expectedCount = 100
	actualCount := len(out)

	if expectedCount != actualCount {
		t.Error("Expected", expectedCount, "members but there were", actualCount)
	}
}

Fix failing unit tests for nativesequence


package cypress

import "sync"

var pbBufPool sync.Pool

func init() {
	pbBufPool.New = newBuf
}

func newBuf() interface{} {
	return make([]byte, 128)
}

package cypress

import "sync"

var pbBufPool sync.Pool

func init() {
	pbBufPool.New = func() interface{} {
		return make([]byte, 128)
	}
}

Use a closure rather than a standalone func


package main

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

const (
	VERSION = "0.1.0"
)

var options struct {
	Path  string
	Port  int
	Token string
	Auth  bool
}

var services []Service

func main() {
	flag.StringVar(&options.Path, "c", "", "Path to config directory")
	flag.IntVar(&options.Port, "p", 3050, "Port to listen on")
	flag.StringVar(&options.Token, "t", "", "Authentication token")

	flag.Parse()

	if options.Path == "" {
		options.Path = "./config"
	}

	// Do not require authentication if token is not set
	if options.Token == "" {
		options.Auth = false
	} else {
		options.Auth = true
	}

	var err error
	services, err = readServices()

	if err != nil {
		fmt.Println("Error:", err.Error())
		os.Exit(1)
	}

	fmt.Printf("envd v%s\n", VERSION)
	fmt.Println("config path:", options.Path)
	fmt.Println("services detected:", len(services))

	startServer()
}

package main

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

const (
	VERSION = "0.1.0"
)

var options struct {
	Path  string
	Port  int
	Token string
	Auth  bool
}

var services []Service

func main() {
	flag.StringVar(&options.Path, "c", "", "Path to config directory")
	flag.IntVar(&options.Port, "p", 3050, "Port to listen on")
	flag.StringVar(&options.Token, "t", "", "Authentication token")

	flag.Parse()

	if options.Path == "" {
		options.Path = "./config"
	}

	// Load token from environment variable if not set
	if options.Token == "" {
		options.Token = os.Getenv("TOKEN")
	}

	// Do not require authentication if token is not set
	if options.Token == "" {
		options.Auth = false
	} else {
		options.Auth = true
	}

	var err error
	services, err = readServices()

	if err != nil {
		fmt.Println("Error:", err.Error())
		os.Exit(1)
	}

	fmt.Printf("envd v%s\n", VERSION)
	fmt.Println("config path:", options.Path)
	fmt.Println("services detected:", len(services))

	startServer()
}

Set authentication from env variable if provided


package main

import (
	"compress/gzip"
	"io"
	"io/ioutil"
	"sync"
)

type gzipPool struct {
	mutex sync.Mutex
	top   *gzipPoolEntry
}

type gzipPoolEntry struct {
	gz   *gzip.Writer
	next *gzipPoolEntry
}

func newGzipPool(n int) *gzipPool {
	pool := new(gzipPool)

	for i := 0; i < n; i++ {
		pool.grow()
	}

	return pool
}

func (p *gzipPool) grow() {
	gz, err := gzip.NewWriterLevel(ioutil.Discard, conf.GZipCompression)
	if err != nil {
		logFatal("Can't init GZip compression: %s", err)
	}

	p.top = &gzipPoolEntry{
		gz:   gz,
		next: p.top,
	}
}

func (p *gzipPool) Get(w io.Writer) *gzip.Writer {
	p.mutex.Lock()
	defer p.mutex.Unlock()

	if p.top == nil {
		p.grow()
	}

	gz := p.top.gz
	gz.Reset(w)

	p.top = p.top.next

	return gz
}

func (p *gzipPool) Put(gz *gzip.Writer) {
	p.mutex.Lock()
	defer p.mutex.Unlock()

	p.top = &gzipPoolEntry{gz: gz, next: p.top}
}

package main

import (
	"compress/gzip"
	"io"
	"io/ioutil"
	"sync"
)

type gzipPool struct {
	mutex sync.Mutex
	top   *gzipPoolEntry
}

type gzipPoolEntry struct {
	gz   *gzip.Writer
	next *gzipPoolEntry
}

func newGzipPool(n int) *gzipPool {
	pool := new(gzipPool)

	for i := 0; i < n; i++ {
		pool.grow()
	}

	return pool
}

func (p *gzipPool) grow() {
	gz, err := gzip.NewWriterLevel(ioutil.Discard, conf.GZipCompression)
	if err != nil {
		logFatal("Can't init GZip compression: %s", err)
	}

	p.top = &gzipPoolEntry{
		gz:   gz,
		next: p.top,
	}
}

func (p *gzipPool) Get(w io.Writer) *gzip.Writer {
	p.mutex.Lock()
	defer p.mutex.Unlock()

	if p.top == nil {
		p.grow()
	}

	gz := p.top.gz
	gz.Reset(w)

	p.top = p.top.next

	return gz
}

func (p *gzipPool) Put(gz *gzip.Writer) {
	p.mutex.Lock()
	defer p.mutex.Unlock()

	gz.Reset(ioutil.Discard)

	p.top = &gzipPoolEntry{gz: gz, next: p.top}
}

Reset gzip encoder on put to pool


package images

import (
	"encoding/json"
	"errors"
)

type IdentityImage struct {
	KeyUID       string
	Name         string
	Payload      []byte
	Width        int
	Height       int
	FileSize     int
	ResizeTarget int
}

func (i IdentityImage) GetType() (ImageType, error) {
	it := GetType(i.Payload)
	if it == UNKNOWN {
		return it, errors.New("unsupported file type")
	}

	return it, nil
}

func (i IdentityImage) GetDataURI() (string, error) {
	return GetPayloadDataURI(i.Payload)
}

func (i IdentityImage) MarshalJSON() ([]byte, error) {
	uri, err := i.GetDataURI()
	if err != nil {
		return nil, err
	}

	temp := struct {
		KeyUID       string `json:"key_uid"`
		Name         string `json:"type"`
		URI          string `json:"uri"`
		Width        int    `json:"width"`
		Height       int    `json:"height"`
		FileSize     int    `json:"file_size"`
		ResizeTarget int    `json:"resize_target"`
	}{
		KeyUID:       i.KeyUID,
		Name:         i.Name,
		URI:          uri,
		Width:        i.Width,
		Height:       i.Height,
		FileSize:     i.FileSize,
		ResizeTarget: i.ResizeTarget,
	}

	return json.Marshal(temp)
}

package images

import (
	"encoding/json"
	"errors"
)

type IdentityImage struct {
	KeyUID       string
	Name         string
	Payload      []byte
	Width        int
	Height       int
	FileSize     int
	ResizeTarget int
}

func (i IdentityImage) GetType() (ImageType, error) {
	it := GetType(i.Payload)
	if it == UNKNOWN {
		return it, errors.New("unsupported file type")
	}

	return it, nil
}

func (i IdentityImage) GetDataURI() (string, error) {
	return GetPayloadDataURI(i.Payload)
}

func (i IdentityImage) MarshalJSON() ([]byte, error) {
	uri, err := i.GetDataURI()
	if err != nil {
		return nil, err
	}

	temp := struct {
		KeyUID       string `json:"keyUid"`
		Name         string `json:"type"`
		URI          string `json:"uri"`
		Width        int    `json:"width"`
		Height       int    `json:"height"`
		FileSize     int    `json:"fileSize"`
		ResizeTarget int    `json:"resizeTarget"`
	}{
		KeyUID:       i.KeyUID,
		Name:         i.Name,
		URI:          uri,
		Width:        i.Width,
		Height:       i.Height,
		FileSize:     i.FileSize,
		ResizeTarget: i.ResizeTarget,
	}

	return json.Marshal(temp)
}

Address consistent json field case convention - use camelCase


package fastly

import (
	"bytes"
	"encoding"
)

type statusResp struct {
	Status string
	Msg    string
}

func (t *statusResp) Ok() bool {
	return t.Status == "ok"
}

// Ensure Compatibool implements the proper interfaces.
var (
	_ encoding.TextMarshaler   = new(Compatibool)
	_ encoding.TextUnmarshaler = new(Compatibool)
)

// Compatibool is a boolean value that marshalls to 0/1 instead of true/false
// for compatability with Fastly's API.
type Compatibool bool

// MarshalText implements the encoding.TextMarshaler interface.
func (b Compatibool) MarshalText() ([]byte, error) {
	if b {
		return []byte("1"), nil
	}
	return []byte("0"), nil
}

// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (b Compatibool) UnmarshalText(t []byte) error {
	if bytes.Equal(t, []byte("1")) {
		b = Compatibool(true)
	}
	return nil
}

package fastly

import (
	"bytes"
	"encoding"
)

type statusResp struct {
	Status string
	Msg    string
}

func (t *statusResp) Ok() bool {
	return t.Status == "ok"
}

// Ensure Compatibool implements the proper interfaces.
var (
	_ encoding.TextMarshaler   = new(Compatibool)
	_ encoding.TextUnmarshaler = new(Compatibool)
)

// Compatibool is a boolean value that marshalls to 0/1 instead of true/false
// for compatability with Fastly's API.
type Compatibool bool

// MarshalText implements the encoding.TextMarshaler interface.
func (b Compatibool) MarshalText() ([]byte, error) {
	if b {
		return []byte("1"), nil
	}
	return []byte("0"), nil
}

// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (b *Compatibool) UnmarshalText(t []byte) error {
	if bytes.Equal(t, []byte("1")) {
		*b = Compatibool(true)
	}
	return nil
}

Fix UnmarshalText implementation on Compatibools.


package providers

import (
	. "bitbucket.org/sinbad/git-lob/Godeps/_workspace/src/github.com/onsi/ginkgo"
	. "bitbucket.org/sinbad/git-lob/Godeps/_workspace/src/github.com/onsi/gomega"
	. "bitbucket.org/sinbad/git-lob/util"
	"testing"
)

func TestAll(t *testing.T) {
	// Connect Ginkgo to Gomega
	RegisterFailHandler(Fail)

	// Set manual logging off
	loggingOff := true
	//loggingOff = false
	if loggingOff {
		LogSuppressAllConsoleOutput()
	}

	// Run everything
	RunSpecs(t, "Git Lob Util Test Suite")
}

package providers

import (
	. "bitbucket.org/sinbad/git-lob/Godeps/_workspace/src/github.com/onsi/ginkgo"
	. "bitbucket.org/sinbad/git-lob/Godeps/_workspace/src/github.com/onsi/gomega"
	. "bitbucket.org/sinbad/git-lob/util"
	"testing"
)

func TestAll(t *testing.T) {
	// Connect Ginkgo to Gomega
	RegisterFailHandler(Fail)

	// Set manual logging off
	loggingOff := true
	//loggingOff = false
	if loggingOff {
		LogSuppressAllConsoleOutput()
	}

	// Run everything
	RunSpecs(t, "Git Lob Providers Test Suite")
}

Fix name of providers test suite


/*
Package response provides helpers and utils for working with HTTP response
*/
package response

import (
	"context"
	"encoding/json"
	"net/http"
)

// JSON returns data as json response
func JSON(ctx context.Context, w http.ResponseWriter, statusCode int, payload interface{}) error {
	w.Header().Set("Content-Type", "application/json")

	// If there is nothing to marshal then set status code and return.
	if payload == nil {
		_, err := w.Write([]byte("{}"))
		return err
	}

	w.WriteHeader(statusCode)

	encoder := json.NewEncoder(w)
	encoder.SetEscapeHTML(true)
	encoder.SetIndent("", "")

	if err := encoder.Encode(payload); err != nil {
		return err
	}

	Flush(w)

	return nil
}

// MustJSON returns data as json response
// will panic if unable to marshal payload into JSON object
// uses JSON internally
func MustJSON(ctx context.Context, w http.ResponseWriter, statusCode int, payload interface{}) {
	if err := JSON(ctx, w, statusCode, payload); err != nil {
		panic(err)
	}
}

/*
Package response provides helpers and utils for working with HTTP response
*/
package response

import (
	"context"
	"encoding/json"
	"net/http"
)

// JSON returns data as json response
func JSON(ctx context.Context, w http.ResponseWriter, statusCode int, payload interface{}) error {
	w.Header().Set("Content-Type", "application/json")

	// If there is nothing to marshal then set status code and return.
	if payload == nil {
		_, err := w.Write([]byte("{}"))
		return err
	}

	if statusCode != http.StatusOK {
		w.WriteHeader(statusCode)
	}

	encoder := json.NewEncoder(w)
	encoder.SetEscapeHTML(true)
	encoder.SetIndent("", "")

	if err := encoder.Encode(payload); err != nil {
		return err
	}

	Flush(w)

	return nil
}

// MustJSON returns data as json response
// will panic if unable to marshal payload into JSON object
// uses JSON internally
func MustJSON(ctx context.Context, w http.ResponseWriter, statusCode int, payload interface{}) {
	if err := JSON(ctx, w, statusCode, payload); err != nil {
		panic(err)
	}
}

Set header only if not OK