package main

import (
	"encoding/json"
	"log"
	"net/http"
	"time"

	"github.com/go-martini/martini"
)

// CreateIssueType ...
type CreateIssueType struct {
	Title string `json:"title"`
	Body  string `json:"body"`
	Org   string `json:"owner"`
	Repo  string `json:"repo"`
}

// CreateIssueMsg ...
type CreateIssueMsg struct {
	Issue   CreateIssueType `json:"issue"`
	*Config `json:"config"`
}

// CreateAttr json to create an issue
type CreateAttr struct {
	Title string `json:"title"`
	Body  string `json:"body"`
	Org   string `json:"org"`
	Repo  string `json:"repo"`
}

func (i *CreateIssueMsg) toJSON() []byte {
	json, err := json.Marshal(i)
	if err != nil {
		log.Println(err)
	}
	return json

}

// CreateIssue is the POST /issue/:issue and updates an Issue status
func CreateIssue(r *http.Request, params martini.Params) string {
	var t CreateAttr

	decoder := json.NewDecoder(r.Body)
	err := decoder.Decode(&t)
	if err != nil {
		return "{\"error\":\"" + err.Error() + "\"}"
	}

	msg := CreateIssueMsg{
		Issue: CreateIssueType{
			Title: t.Title,
			Body:  t.Body,
			Repo:  t.Repo,
			Org:   t.Org,
		},
		Config: getConfig(),
	}

	issue, err := nc.Request("issues.create", msg.toJSON(), 10000*time.Millisecond)
	if err != nil {
		return "{\"error\":\"" + err.Error() + "\"}"
	}

	return string(issue.Data)
}

package main

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

	"github.com/go-martini/martini"
	"github.com/supu-io/messages"
)

// CreateAttr json to create an issue
type CreateAttr struct {
	Title string `json:"title"`
	Body  string `json:"body"`
	Org   string `json:"org"`
	Repo  string `json:"repo"`
}

// CreateIssue is the POST /issue/:issue and updates an Issue status
func CreateIssue(r *http.Request, params martini.Params) string {
	var t CreateAttr
	decoder := json.NewDecoder(r.Body)
	err := decoder.Decode(&t)

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

	msg := messages.CreateIssue{
		Issue: &messages.Issue{
			Title: t.Title,
			Body:  t.Body,
			Repo:  t.Repo,
			Org:   t.Org,
		},
		Config: config(),
	}

	return Request("issues.create", msg)
}

Use messages library on issues.create


package main

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

func sigChld() {
	var sigs = make(chan os.Signal, 1)
	signal.Notify(sigs, syscall.SIGCHLD)

	for {
		select {
		case <-sigs:
			go reap()
		default:
		}
	}

}

func reap() {
	var wstatus syscall.WaitStatus

	pid, err := syscall.Wait4(-1, &wstatus, 0, nil)
	switch err {
	case syscall.EINTR:
		pid, err = syscall.Wait4(-1, &wstatus, 0, nil)
	case syscall.ECHILD:
		return
	}
	logPrintf("pid %d, finished, wstatus: %+v", pid, wstatus)
}

package main

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

func sigChld() {
	var sigs = make(chan os.Signal, 10) // TODO(miek): buffered channel to fix races?
	signal.Notify(sigs, syscall.SIGCHLD)

	for {
		select {
		case <-sigs:
			go reap()
		}
	}
}

func reap() {
	var wstatus syscall.WaitStatus

	pid, err := syscall.Wait4(-1, &wstatus, 0, nil)
	switch err {
	case syscall.EINTR:
		pid, err = syscall.Wait4(-1, &wstatus, 0, nil)
	case syscall.ECHILD:
		return
	}
	logPrintf("pid %d, finished, wstatus: %+v", pid, wstatus)
}

Remove default and increase channel size


package user_parser

import (
	"log"
	"os"
)

func ParseUsername() ([]string, error) {
	args := os.Args

	if len(args) < 1 {
		log.Fatal("Please supply at least one GitHub username")
	}

	// username := args[0]
	// return username, nil
	return args, nil
}

package user_parser

import (
	"log"
	"os"
)

// ParseUsername returns an array of user strings or an error if no arguments provided
func ParseUsername() ([]string, error) {
	args := os.Args

	if len(args) < 1 {
		log.Fatal("Please supply at least one GitHub username")
	}

	return args, nil
}

Add comment, remove dead code


package fbmessenger_test

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

	"github.com/onsi/ginkgo/reporters"
	"testing"

	"os"
	"path/filepath"
)

func TestWebhooks(t *testing.T) {
	RegisterFailHandler(Fail)
	testReportsPath, _ := filepath.Abs("./test-reports")
	os.MkdirAll(testReportsPath, 0777)
	junitReporter := reporters.NewJUnitReporter(filepath.Join(testReportsPath, "lib-webhooks-junit.xml"))
	RunSpecsWithDefaultAndCustomReporters(t, "Webhooks Suite", []Reporter{junitReporter})
}

package fbmessenger_test

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

	"github.com/onsi/ginkgo/reporters"
	"testing"

	"os"
	"path/filepath"
)

func TestWebhooks(t *testing.T) {
	RegisterFailHandler(Fail)
	testReportsPath, _ := filepath.Abs("./test-reports")
	os.MkdirAll(testReportsPath, 0777)
	junitReporter := reporters.NewJUnitReporter(filepath.Join(testReportsPath, "fbmessenger-junit.xml"))
	RunSpecsWithDefaultAndCustomReporters(t, "Webhooks Suite", []Reporter{junitReporter})
}

Change the test report file name.


package queue

import (
	"time"

	"golang.org/x/time/rate"
)

type Storager interface {
	Exists(key string) bool
}

type Limiter interface {
	AllowRate(name string, limit rate.Limit) (delay time.Duration, allow bool)
}

type Queuer interface {
	Name() string
	Add(msg *Message) error
	Call(args ...interface{}) error
	AddAsync(msg *Message) error
	CallAsync(args ...interface{}) error
	ReserveN(n int) ([]Message, error)
	Release(*Message, time.Duration) error
	Delete(msg *Message) error
	DeleteBatch(msg []*Message) error
	Purge() error
}

package queue

import (
	"time"

	"golang.org/x/time/rate"
)

type Storager interface {
	Exists(key string) bool
}

type Limiter interface {
	AllowRate(name string, limit rate.Limit) (delay time.Duration, allow bool)
}

type Queuer interface {
	Name() string
	Add(msg *Message) error
	Call(args ...interface{}) error
	CallOnce(dur time.Duration, args ...interface{}) error
	AddAsync(msg *Message) error
	CallAsync(args ...interface{}) error
	CallOnceAsync(dur time.Duration, args ...interface{}) error
	ReserveN(n int) ([]Message, error)
	Release(*Message, time.Duration) error
	Delete(msg *Message) error
	DeleteBatch(msg []*Message) error
	Purge() error
}

Add missing methods to interface.


package worker

import (
	"fmt"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/sqs"
)

// NewSQSClient returns a SQS Client and a Queue URL for you you to connect to
func NewSQSClient(queueName string) (*sqs.SQS, string) {
	sess, err := session.NewSession()
	if err != nil {
		fmt.Println("failed to create session,", err)
		return nil, ""
	}
	svc := sqs.New(sess)
	// try and find the queue url

	params := &sqs.GetQueueUrlInput{
		QueueName:              aws.String(queueName), // Required
	}
	resp, err := svc.GetQueueUrl(params)

	if err != nil {
		// Print the error, cast err to aws err.Error to get the Code and
		// Message from an error.
		fmt.Println(err.Error())
		return nil, ""
	}

	return svc, aws.StringValue(resp.QueueUrl)
}

package worker

import (
	"fmt"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/sqs"
)

// NewSQSClient returns a SQS Client and a Queue URL for you you to connect to
func NewSQSClient(queueName string, cfgs ...*aws.Config) (*sqs.SQS, string) {
	sess, err := session.NewSession()
	if err != nil {
		fmt.Println("failed to create session,", err)
		return nil, ""
	}
	svc := sqs.New(sess, cfgs...)
	// try and find the queue url

	params := &sqs.GetQueueUrlInput{
		QueueName:              aws.String(queueName), // Required
	}
	resp, err := svc.GetQueueUrl(params)

	if err != nil {
		// Print the error, cast err to aws err.Error to get the Code and
		// Message from an error.
		fmt.Println(err.Error())
		return nil, ""
	}

	return svc, aws.StringValue(resp.QueueUrl)
}

Add the ability to pass in the an aws.Config object when creating a NewSQSClient


package dbio

import (
	"fmt"
)

type DataBlock struct {
	ID   uint16
	Data []byte
}

func (db *DataBlock) ReadUint16(startingAt int) uint16 {
	return DatablockByteOrder.Uint16(db.Data[startingAt : startingAt+2])
}

func (db *DataBlock) ReadUint32(startingAt int) uint32 {
	return DatablockByteOrder.Uint32(db.Data[startingAt : startingAt+4])
}

func (db *DataBlock) ReadString(startingAt, length int) string {
	return string(db.Data[startingAt : startingAt+length])
}

func (db *DataBlock) Write(position int, v interface{}) {
	switch x := v.(type) {
	case []byte:
		lastPosition := position + len(x)
		i := 0
		for target := position; target < lastPosition; target++ {
			db.Data[target] = x[i]
			i++
		}
	case uint16:
		DatablockByteOrder.PutUint16(db.Data[position:position+2], x)
	case uint32:
		DatablockByteOrder.PutUint32(db.Data[position:position+4], x)
	default:
		panic(fmt.Sprintf("Don't know how to write %+v", x))
	}
}

package dbio

import (
	"fmt"
)

type DataBlock struct {
	ID   uint16
	Data []byte
}

func (db *DataBlock) ReadUint8(startingAt int) uint8 {
	return uint8(db.Data[startingAt])
}

func (db *DataBlock) ReadUint16(startingAt int) uint16 {
	return DatablockByteOrder.Uint16(db.Data[startingAt : startingAt+2])
}

func (db *DataBlock) ReadUint32(startingAt int) uint32 {
	return DatablockByteOrder.Uint32(db.Data[startingAt : startingAt+4])
}

func (db *DataBlock) ReadString(startingAt, length int) string {
	return string(db.Data[startingAt : startingAt+length])
}

func (db *DataBlock) Write(position int, v interface{}) {
	switch x := v.(type) {
	case uint8:
		db.Data[position] = x
	case []byte:
		lastPosition := position + len(x)
		i := 0
		for target := position; target < lastPosition; target++ {
			db.Data[target] = x[i]
			i++
		}
	case uint16:
		DatablockByteOrder.PutUint16(db.Data[position:position+2], x)
	case uint32:
		DatablockByteOrder.PutUint32(db.Data[position:position+4], x)
	default:
		panic(fmt.Sprintf("Don't know how to write %+v", x))
	}
}

Add support for reading / writing uint8 from blocks (AKA a single byte, methods was introduced for consistency)


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

package main

var tmplDataUser = `
insert into {{case "SYS_USER (ID, NAME, CODE, PASSWD, GENRE, EMAIL, TEL, MOBILE, AREA_ID, ORG_ID, LOGIN_NAME, LOGIN_IP, LOGIN_TIME, MEMO, CREATES, CREATER, CREATED, MODIFIER, MODIFIED, VERSION, DELETION, ARTIFICAL, HISTORY)"}}
values ('admin', '{{message "tmpl.data.user.admin"}}', null, '92d55a4a6b07', '10', 'admin@goyy.org', null, null, 'root', 'root', 'admin', null, ` + now + `, null, null, null, ` + now + `, null, ` + now + `, 0, 0, 0, 0){{seperator}}
`

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

package main

var tmplDataUser = `
insert into {{case "SYS_USER (ID, NAME, CODE, PASSWD, GENRE, EMAIL, TEL, MOBILE, AREA_ID, ORG_ID, DIMISSION_TIME, FREEZE_TIME, LOGIN_NAME, LOGIN_IP, LOGIN_TIME, MEMO, CREATES, CREATER, CREATED, MODIFIER, MODIFIED, VERSION, DELETION, ARTIFICAL, HISTORY)"}}
values ('admin', '{{message "tmpl.data.user.admin"}}', null, '92d55a4a6b07', '10', 'admin@goyy.org', null, null, 'root', 'root', ` + now + `, ` + now + `, 'admin', null, ` + now + `, null, null, null, ` + now + `, null, ` + now + `, 0, 0, 0, 0){{seperator}}
`

Add support to new project on xgen


// Copyright 2015 The Cockroach 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. See the AUTHORS file
// for names of contributors.
//
// Author: Marc Berhault (marc@cockroachlabs.com)

package amazon

import "github.com/awslabs/aws-sdk-go/aws"

// LoadAWSCredentials loads the credentials using the AWS api. This automatically
// loads from ENV, or from the .aws/credentials file.
// Returns the key-id and secret-key.
func LoadAWSCredentials() (string, string, error) {
	creds, err := aws.DefaultCreds().Credentials()
	if err != nil {
		return "", "", err
	}

	return creds.AccessKeyID, creds.SecretAccessKey, nil
}

// Copyright 2015 The Cockroach 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. See the AUTHORS file
// for names of contributors.
//
// Author: Marc Berhault (marc@cockroachlabs.com)

package amazon

import "github.com/awslabs/aws-sdk-go/aws"

// LoadAWSCredentials loads the credentials using the AWS api. This automatically
// loads from ENV, or from the .aws/credentials file.
// Returns the key-id and secret-key.
func LoadAWSCredentials() (string, string, error) {
	creds, err := aws.DefaultChainCredentials.Get()
	if err != nil {
		return "", "", err
	}

	return creds.AccessKeyID, creds.SecretAccessKey, nil
}

Fix use of AWS library


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

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

package main

const none = 0  // same const identifier declared twice should not be accepted
const none = 1  // ERROR "redeclared"

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

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

package main

// same const identifier declared twice should not be accepted
const none = 0  // GCCGO_ERROR "previous"
const none = 1  // ERROR "redeclared|redef"

Tweak comments so that this test passes with gccgo.


package network

import (
	"net"
	"testing"
)

func TestDeviceCreate(t *testing.T) {
	iface := "tun0"
	_, err := CreateTunInterface(iface)
	if err != nil {
		t.Error(err)
	}
}

func TestCreateDeviceWithIpAddr(t *testing.T) {
	iface := "tun0"
	IpAddr := "10.0.0.1/24"
	_, err := CreateTunInterfaceWithIp(iface, IpAddr)
	if err != nil {
		t.Error(err)
	}
	ifce, err := net.InterfaceByName(iface)

	if err != nil {
		t.Error(err)
	}
	addr, err := ifce.Addrs()
	if err != nil {
		t.Error(err)
	}
	if addr[0].String() != IpAddr {
		t.Error("Wrong Ip address on device")
	}
}

package network

import (
	"net"
	"strings"
	"testing"
)

func TestDeviceCreate(t *testing.T) {
	iface := "tun0"
	_, err := CreateTunInterface(iface)
	if err != nil {
		t.Error(err)
	}
	niface, err := net.InterfaceByName(iface)
	if err != nil {
		t.Error(err)
	}
	if !strings.Contains(niface.Flags.String(), "up") {
		t.Error("Interface not up")
	}
}

func TestCreateDeviceWithIpAddr(t *testing.T) {
	iface := "tun0"
	IpAddr := "10.0.0.1/24"
	_, err := CreateTunInterfaceWithIp(iface, IpAddr)
	if err != nil {
		t.Error(err)
	}
	ifce, err := net.InterfaceByName(iface)

	if err != nil {
		t.Error(err)
	}
	addr, err := ifce.Addrs()
	if err != nil {
		t.Error(err)
	}
	if addr[0].String() != IpAddr {
		t.Error("Wrong Ip address on device")
	}
}

Test if we create interface with UP status


package v1alpha1

import "time"

const (
	RoleMaster    = "master"
	RoleNode      = "node"
	RoleKeyPrefix = "node-role.kubernetes.io/"
	RoleMasterKey = RoleKeyPrefix + RoleMaster
	RoleNodeKey   = RoleKeyPrefix + RoleNode

	KubeadmVersionKey = "cloud.appscode.com/kubeadm-version"
	NodePoolKey       = "cloud.appscode.com/pool"
	KubeSystem_App    = "k8s-app"

	HostnameKey     = "kubernetes.io/hostname"
	ArchKey         = "beta.kubernetes.io/arch"
	InstanceTypeKey = "beta.kubernetes.io/instance-type"
	OSKey           = "beta.kubernetes.io/os"
	RegionKey       = "failure-domain.beta.kubernetes.io/region"
	ZoneKey         = "failure-domain.beta.kubernetes.io/zone"

	TokenDuration_10yr = 10 * 365 * 24 * time.Hour

	// ref: https://github.com/kubernetes/kubeadm/issues/629
	DeprecatedV19AdmissionControl = "NamespaceLifecycle,LimitRanger,ServiceAccount,PersistentVolumeLabel,DefaultStorageClass,ValidatingAdmissionWebhook,ResourceQuota,DefaultTolerationSeconds,MutatingAdmissionWebhook"
	DefaultV19AdmissionControl    = "NamespaceLifecycle,LimitRanger,ServiceAccount,DefaultStorageClass,ValidatingAdmissionWebhook,ResourceQuota,DefaultTolerationSeconds,MutatingAdmissionWebhook"
)

package v1alpha1

import "time"

const (
	RoleMaster    = "master"
	RoleNode      = "node"
	RoleKeyPrefix = "node-role.kubernetes.io/"
	RoleMasterKey = RoleKeyPrefix + RoleMaster
	RoleNodeKey   = RoleKeyPrefix + RoleNode

	KubeadmVersionKey = "cloud.appscode.com/kubeadm-version"
	NodePoolKey       = "cloud.appscode.com/pool"
	KubeSystem_App    = "k8s-app"

	HostnameKey     = "kubernetes.io/hostname"
	ArchKey         = "beta.kubernetes.io/arch"
	InstanceTypeKey = "beta.kubernetes.io/instance-type"
	OSKey           = "beta.kubernetes.io/os"
	RegionKey       = "failure-domain.beta.kubernetes.io/region"
	ZoneKey         = "failure-domain.beta.kubernetes.io/zone"

	TokenDuration_10yr = 10 * 365 * 24 * time.Hour

	// ref: https://github.com/kubernetes/kubeadm/issues/629
	DeprecatedV19AdmissionControl = "NamespaceLifecycle,LimitRanger,ServiceAccount,PersistentVolumeLabel,DefaultStorageClass,ValidatingAdmissionWebhook,DefaultTolerationSeconds,MutatingAdmissionWebhook,ResourceQuota"
	DefaultV19AdmissionControl    = "NamespaceLifecycle,LimitRanger,ServiceAccount,DefaultStorageClass,ValidatingAdmissionWebhook,DefaultTolerationSeconds,MutatingAdmissionWebhook,ResourceQuota"
)

Update admission controller list order


package config

import "go.skia.org/infra/go/buildskia"

const (
	// BUILD_TYPE is the type of build we use throughout fiddle.
	BUILD_TYPE = buildskia.RELEASE_BUILD
)

var (
	// GN_FLAGS are the flags to pass to GN.
	GN_FLAGS = []string{"is_debug=false", "skia_use_egl=true", "extra_cflags_cc=[\"-Wno-error\" \"-DEGL_NO_IMAGE_EXTERNAL=1\"]"}

	// EGL_LIB_PATH is the path where the correct libEGL.so can be found.
	EGL_LIB_PATH = "/usr/lib/nvidia-367/"
)

package config

import "go.skia.org/infra/go/buildskia"

const (
	// BUILD_TYPE is the type of build we use throughout fiddle.
	BUILD_TYPE = buildskia.RELEASE_BUILD
)

var (
	// GN_FLAGS are the flags to pass to GN.
	GN_FLAGS = []string{"is_debug=false", "skia_use_egl=true", "extra_cflags_cc=[\"-Wno-error\"]"}

	// EGL_LIB_PATH is the path where the correct libEGL.so can be found.
	EGL_LIB_PATH = "/usr/lib/nvidia-367/"
)

Revert "[fiddle] Add flag to work around the GL calls in CreatePlatformGLTestContext_egl.cpp."


package bot

// Handle SIGINT and SIGTERM with a graceful shutdown

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

func init() {
	sigs := make(chan os.Signal, 1)

	// `signal.Notify` registers the given channel to
	// receive notifications of the specified signals.
	signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)

	// This goroutine executes a blocking receive for
	// signals. When it gets one it'll print it out
	// and then notify the program that it can finish.
	go func() {
		sig := <-sigs
		shutdownMutex.Lock()
		shuttingDown = true
		shutdownMutex.Unlock()
		Log(Info, fmt.Sprintf("Received signal: %s, shutting down gracefully", sig))
		// Wait for all plugins to stop running
		plugRunningWaitGroup.Wait()
		// Get the dataLock to make sure the brain is in a consistent state
		dataLock.Lock()
		Log(Info, fmt.Sprintf("Exiting on signal: %s", sig))
		// How long does it _actually_ take for the message to go out?
		time.Sleep(time.Second)
		os.Exit(0)
	}()
}

package bot

// Handle SIGINT and SIGTERM with a graceful shutdown

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

func init() {
	sigs := make(chan os.Signal, 1)

	signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)

	go func() {
		sig := <-sigs
		shutdownMutex.Lock()
		shuttingDown = true
		shutdownMutex.Unlock()
		Log(Info, fmt.Sprintf("Received signal: %s, shutting down gracefully", sig))
		// Wait for all plugins to stop running
		plugRunningWaitGroup.Wait()
		// Get the dataLock to make sure the brain is in a consistent state
		dataLock.Lock()
		Log(Info, fmt.Sprintf("Exiting on signal: %s", sig))
		// How long does it _actually_ take for the message to go out?
		time.Sleep(time.Second)
		os.Exit(0)
	}()
}

Remove bogus comments from sample code


package io_test

import (
	"os"
	"strings"

	. "github.com/cloudfoundry/cli/testhelpers/io"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("io helpers", func() {
	It("will never overflow the pipe", func() {
		characters := make([]string, 0, 75000)
		for i := 0; i < 75000; i++ {
			characters = append(characters, "z")
		}

		str := strings.Join(characters, "")

		output := CaptureOutput(func() {
			os.Stdout.Write([]byte(str))
		})

		Expect(output).To(Equal([]string{str}))
	})
})

package io_test

import (
	"os"
	"strings"

	. "github.com/cloudfoundry/cli/testhelpers/io"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("io helpers", func() {
	It("will never overflow the pipe", func() {
		str := strings.Repeat("z", 75000)
		output := CaptureOutput(func() {
			os.Stdout.Write([]byte(str))
		})

		Expect(output).To(Equal([]string{str}))
	})
})

Speed up this test by a factor of a little bit more


package models

import "encoding/json"

type DesireAppRequestFromCC struct {
	AppId           string                `json:"app_id"`
	AppVersion      string                `json:"app_version"`
	DropletUri      string                `json:"droplet_uri"`
	Stack           string                `json:"stack"`
	StartCommand    string                `json:"start_command"`
	Environment     []EnvironmentVariable `json:"environment"`
	MemoryMB        int                   `json:"memory_mb"`
	DiskMB          int                   `json:"disk_mb"`
	FileDescriptors uint64                `json:"file_descriptors"`
	NumInstances    int                   `json:"num_instances"`
	Routes          []string              `json:"routes"`
}

func (d DesireAppRequestFromCC) ToJSON() []byte {
	encoded, _ := json.Marshal(d)
	return encoded
}

package models

import "encoding/json"

type DesireAppRequestFromCC struct {
	ProcessGuid     string                `json:"process_guid"`
	DropletUri      string                `json:"droplet_uri"`
	Stack           string                `json:"stack"`
	StartCommand    string                `json:"start_command"`
	Environment     []EnvironmentVariable `json:"environment"`
	MemoryMB        int                   `json:"memory_mb"`
	DiskMB          int                   `json:"disk_mb"`
	FileDescriptors uint64                `json:"file_descriptors"`
	NumInstances    int                   `json:"num_instances"`
	Routes          []string              `json:"routes"`
	LogGuid         string                `json:"log_guid"`
}

func (d DesireAppRequestFromCC) ToJSON() []byte {
	encoded, _ := json.Marshal(d)
	return encoded
}

Change NATS desire app message format.


package errors_test

import (
	"fmt"

	"github.com/vardius/go-api-boilerplate/pkg/errors"
)

func ExampleNew() {
	err := errors.New("example")

	fmt.Printf("%s\n", err)
}

func ExampleWrap() {
	subErr := errors.New("example")
	err := errors.Wrap(subErr)

	fmt.Printf("%s\n", err)
}

package errors_test

import (
	"fmt"

	"github.com/vardius/go-api-boilerplate/pkg/errors"
)

func ExampleNew() {
	err := errors.New("example")

	fmt.Printf("%s\n", err)

	// Output:
	// example:
	// 	/home/travis/gopath/src/github.com/vardius/go-api-boilerplate/pkg/errors/example_test.go:10
}

func ExampleWrap() {
	subErr := errors.New("example")
	err := errors.Wrap(subErr)

	fmt.Printf("%s\n", err)

	// Output:
	// /home/travis/gopath/src/github.com/vardius/go-api-boilerplate/pkg/errors/example_test.go:20
	// example:
	// 	/home/travis/gopath/src/github.com/vardius/go-api-boilerplate/pkg/errors/example_test.go:19
}

Fix example tests for travis


// goslow is a slow HTTP server that responds with errors.
// Visit https://github.com/alexandershov/goslow for more details.
package main

import (
	"log"
	"runtime"
)

// main starts a slow HTTP server that responds with errors.
func main() {
	// GOMAXPROCS call is ignored if NumCPU returns 1 (GOMAXPROCS(0) doesn't change anything)
	runtime.GOMAXPROCS(runtime.NumCPU() / 2)

	config := NewConfigFromArgs()
	server := NewServer(config)

	log.Fatal(server.ListenAndServe())
}

// goslow is a slow HTTP server that responds with errors.
// Visit https://github.com/alexandershov/goslow for more details.
package main

import (
	"log"
	"runtime"
)

// main starts a server.
func main() {
	useSeveralCPU()

	config := NewConfigFromArgs()
	server := NewServer(config)

	log.Fatal(server.ListenAndServe())
}

func useSeveralCPU() {
	if runtime.NumCPU() > 1 {
		runtime.GOMAXPROCS(runtime.NumCPU() / 2)
	}
}

Move parallelism decision to a separate function


package machineid

import (
	"crypto/hmac"
	"crypto/sha256"
	"fmt"
	"io"
	"os"
	"os/exec"
)

// run wraps `exec.Command` with easy access to stdout and stderr.
func run(stdout, stderr io.Writer, cmd string, args ...string) error {
	c := exec.Command(cmd, args...)
	c.Stdin = os.Stdin
	c.Stdout = stdout
	c.Stderr = stderr
	return c.Run()
}

func protect(appID, id string) string {
	mac := hmac.New(sha256.New, []byte(id))
	mac.Write([]byte(appID))
	return fmt.Sprintf("%x", mac.Sum(nil))
}

package machineid

import (
	"crypto/hmac"
	"crypto/sha256"
	"encoding/hex"
	"io"
	"os"
	"os/exec"
)

// run wraps `exec.Command` with easy access to stdout and stderr.
func run(stdout, stderr io.Writer, cmd string, args ...string) error {
	c := exec.Command(cmd, args...)
	c.Stdin = os.Stdin
	c.Stdout = stdout
	c.Stderr = stderr
	return c.Run()
}

// protect calculates HMAC-SHA256 of the application ID, keyed by the machine ID and returns a hex-encoded string.
func protect(appID, id string) string {
	mac := hmac.New(sha256.New, []byte(id))
	mac.Write([]byte(appID))
	return hex.EncodeToString(mac.Sum(nil))
}

Make output encoding more specific by using hex encoding.


/* For license and copyright information please see LEGAL file in repository */

package srpc

import "../syllab"

/*
	********************PAY ATTENTION:*******************
	We don't suggest use these 2 func instead use chaparkhane to autogenerate needed code before compile time
	and reduce runtime proccess to improve performance of the app and gain max performance from this protocol!
*/

// MarshalPacket use to encode automatically the value of s to the payload buffer.
func MarshalPacket(p []byte, id uint32, s interface{}) (err error) {
	// Set ServiceID to payload
	SetID(p, id)

	// encode s to p by syllab encoder
	err = syllab.MarshalSyllab(p[4:], s)

	return err
}

// UnMarshalPacket use to decode automatically payload and stores the result
// in the value pointed to by s.
func UnMarshalPacket(p []byte, expectedMinLen int, s interface{}) (id uint32, err error) {
	err = CheckPacket(p, expectedMinLen)
	if err != nil {
		return 0, err
	}

	// Get ErrorID from payload
	id = GetID(p)

	// decode payload to s by syllab encoder
	err = syllab.UnMarshalSyllab(p[4:], s)

	return id, err
}

/* For license and copyright information please see LEGAL file in repository */

package srpc

import "../syllab"

/*
	********************PAY ATTENTION:*******************
	We don't suggest use these 2 func instead use chaparkhane to autogenerate needed code before compile time
	and reduce runtime proccess to improve performance of the app and gain max performance from this protocol!
*/

// MarshalPacket use to encode automatically the value of s to the payload buffer.
func MarshalPacket(id uint32, s interface{}) (p []byte, err error) {
	// encode s to p by syllab encoder
	p, err = syllab.Marshal(s, 4)

	// Set ServiceID to first of payload
	SetID(p, id)

	return
}

// UnMarshalPacket use to decode automatically payload and stores the result
// in the value pointed to by s.
func UnMarshalPacket(p []byte, expectedMinLen int, s interface{}) (id uint32, err error) {
	err = CheckPacket(p, expectedMinLen)
	if err != nil {
		return 0, err
	}

	// Get ErrorID from payload
	id = GetID(p)

	// decode payload to s by syllab encoder
	err = syllab.UnMarshal(p[4:], s)

	return
}

Fix logic and typo in sRPC


package numbers

import (
	"errors"
	"reflect"
	"testing"
)

// TestFactorsList unit test FactorsList function.
func TestFactorsList(t *testing.T) {
	testCases := []struct {
		input    int
		expected []int
		err      error
	}{
		{2, []int{2}, nil},
		{15, []int{3, 5}, nil},
		{26, []int{2, 13}, nil},
		{37, []int{37}, nil},
		{42, []int{2, 3, 7}, nil},
		{-1, []int(nil), errors.New("no prime factors for numbers below two. received '-1'")},
	}
	for _, test := range testCases {
		observed, err := FactorsList(test.input)
		if err != nil {
			if test.err.Error() != err.Error() {
				t.Error(err)
			}
		}
		if !reflect.DeepEqual(observed, test.expected) {
			t.Errorf("for input '%d', expected '%v', got '%v'",
				test.input, test.expected, observed)
		}
	}
}

// BenchmarkFactorList benchmark FactorsList function.
func BenchmarkFactorList(b *testing.B) {
	for i := 0; i <= b.N; i++ {
		_, err := FactorsList(1000)
		if err != nil {
			b.Error(err)
		}
	}
}

Add benchmark test for FactorsList function


package main

import (
  "os"
  "fmt"
)

func main() {
  oauth_key := os.Getenv("BITBUCKET_ENFORCER_KEY")
  oauth_pass := os.Getenv("BITBUCKET_ENFORCER_PASS")

  fmt.Println("key:", oauth_key)
  fmt.Println("pass:", oauth_pass)
}

package main

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

type RepositorySettings struct {
  LandingPage string // TODO unmarshal into enum like type
  Private bool
  MainBranch string
  Forks string // TODO: Unmarshal 'forks' into an enum like type
  DeployKeys []struct {
    Name string
    Key string
  }
  PostHooks []string
  BranchManagement struct {
    PreventDelete []string
    PreventRebase []string
    AllowPushes []struct {
      BranchName string
      Groups []string
      Users []string
    }
  }

  AccessManagement struct {
    Users []struct {
      User string
      Permission string // TODO unmarshal permission into an enum like type (read, write, adming)
    }
    Groups []struct {
      Group string
      Permission string // TODO unmarshal permission into an enum like type (read, write, adming)
    }
  }
}

var configFile = flag.String("config", "golive.json", "the configfile to read")
var verbose = flag.Bool("v", false, "print more output")

func main() {
  oauth_key := os.Getenv("BITBUCKET_ENFORCER_KEY")
  oauth_pass := os.Getenv("BITBUCKET_ENFORCER_PASS")

  fmt.Println("key:", oauth_key)
  fmt.Println("pass:", oauth_pass)
}

func parseConfig(configFile string) RepositorySettings {
    config_raw, err := ioutil.ReadFile(configFile)
    if err != nil {
      log.Fatal(err)
    }

    var config RepositorySettings
    json.Unmarshal(config_raw, &config)

    if *verbose {
      log.Print("Loaded config: ", config)
    }

    return config
}

Add struct that should represent the repo setting JSON format


package logger

import (
	"os"
	"strconv"

	"github.com/Sirupsen/logrus"
	"github.com/polds/logrus/hooks/papertrail"
)

var __l *logrus.Logger

// Logger returns an instance of
// a logger or creates a new one.
func Logger() *logrus.Logger {
	if __l == nil {
		__l = NewLogger()
	}

	return __l
}

// NewLogger creates a new instances of a
// logrus logger and returns the instance.
func NewLogger() *logrus.Logger {
	app := os.Getenv("APPNAME")
	if app == "" {
		app, _ = os.Hostname()
		os.Setenv("APPNAME", app)
	}

	host := os.Getenv("PAPERTRAIL_HOST")
	port, _ := strconv.Atoi(os.Getenv("PAPERTRAIL_PORT"))

	log := logrus.New()
	hook, err := logrus_papertrail.NewPapertrailHook(host, port, app)
	hook.UseHostname()

	// Register the PaperTrail hook
	if err == nil {
		log.Hooks.Add(hook)
	}

	return log
}

package logger

import (
	"os"
	"strconv"

	"github.com/Sirupsen/logrus"
	"github.com/polds/logrus/hooks/papertrail"
)

var __l *logrus.Logger

type Config struct {
	Appname string
	Host    string
	Port    int

	*logrus.Logger
}

// Logger returns an instance of
// a logger or creates a new one.
func Logger() *logrus.Logger {
	if __l == nil {
		__l = NewLogger()
	}

	return __l
}

// New creates a new instance of a
// logger based on provided config data
func New(config Config) Config {
	config.Logger = logrus.New()
	hook, err := logrus_papertrail.NewPapertrailHook(config.Host, config.Port, config.Appname)
	hook.UseHostname()

	// Register the PaperTrail hook
	if err == nil {
		config.Logger.Hooks.Add(hook)
	}

	return config
}

// DefaultConfig makes certain assumptions about your environment variables
// and can be used to create a new basic instance.
func DefaultConfig() Config {
	app := os.Getenv("APPNAME")
	if app == "" {
		app, _ = os.Hostname()
		os.Setenv("APPNAME", app)
	}

	port, _ := strconv.Atoi(os.Getenv("PAPERTRAIL_PORT"))
	return Config{
		Appname: os.Getenv("APPNAME"),
		Host:    os.Getenv("PAPERTRAIL_HOST"),
		Port:    port,
	}
}

// @TODO polds deprecate this function
func NewLogger() *logrus.Logger {
	return New(DefaultConfig()).Logger
}

Add a "New()" function that allows for custom conf


package util

import (
	"testing"
)

func TestNormalizeName(t *testing.T) {
	packages := map[string]string{
		"github.com/Masterminds/cookoo/web/io/foo": "github.com/Masterminds/cookoo",
		`github.com\Masterminds\cookoo\web\io\foo`: "github.com/Masterminds/cookoo",
		"golang.org/x/crypto/ssh":                  "golang.org/x/crypto",
		"incomplete/example":                       "incomplete/example",
		"net":                                      "net",
	}
	for start, expected := range packages {
		if finish, extra := NormalizeName(start); expected != finish {
			t.Errorf("Expected '%s', got '%s'", expected, finish)
		} else if start != finish && start != finish+"/"+extra {
			t.Errorf("Expected %s to end with %s", finish, extra)
		}
	}
}

package util

import (
	"testing"
)

func TestNormalizeName(t *testing.T) {
	packages := []struct {
		input string
		root  string
		extra string
	}{
		{
			input: "github.com/Masterminds/cookoo/web/io/foo",
			root:  "github.com/Masterminds/cookoo",
			extra: "web/io/foo",
		},
		{
			input: `github.com\Masterminds\cookoo\web\io\foo`,
			root:  "github.com/Masterminds/cookoo",
			extra: "web/io/foo",
		},
		{
			input: "golang.org/x/crypto/ssh",
			root:  "golang.org/x/crypto",
			extra: "ssh",
		},
		{
			input: "incomplete/example",
			root:  "incomplete/example",
			extra: "",
		},
		{
			input: "net",
			root:  "net",
			extra: "",
		},
	}
	for _, test := range packages {
		root, extra := NormalizeName(test.input)
		switch {
		case root != test.root:
			t.Errorf("%s: Expected root '%s', got '%s'", test.input, test.root, root)
		case extra != test.extra:
			t.Errorf("%s: Expected extra '%s', got '%s'", test.input, test.extra, extra)
		}
	}
}

Rewrite test so it tests the right thing.


package hpack

// TODO: headers of arbitrary length with integer encoding algorithm
// TODO: hpack test cases https://github.com/http2jp/hpack-test-case

func Decode(headers string, table *HeaderTable) ([]HeaderField, int) {
	var decodedHeaders []HeaderField

	return decodedHeaders, 0
}

package hpack

func Decode(headers string, table *HeaderTable) ([]HeaderField, int) {
	var decodedHeaders []HeaderField

	return decodedHeaders, 0
}

Remove TODOs that have been done or moved to Github issues


// Copyright 2016 Marcel Gotsch. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package goserv

import (
	"net/http"
)

type TLS struct {
	CertFile, KeyFile string
}

type Server struct {
	*Router
	Addr string
	TLS  *TLS
}

func (s *Server) Listen(addr string, tls *TLS) error {
	if tls != nil {
		s.TLS = tls
		return http.ListenAndServeTLS(addr, tls.CertFile, tls.KeyFile, s)
	}

	return http.ListenAndServe(addr, s)
}

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	res := &responseWriter{w: w}
	req := &Request{r, &Context{}, nil, nil, sanitizePath(r.URL.Path)}

	s.Router.serveHTTP(res, req)
}

func NewServer() *Server {
	s := &Server{NewRouter(), "", nil}
	s.ErrorHandler = StdErrorHandler

	return s
}

// Copyright 2016 Marcel Gotsch. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package goserv

import (
	"net/http"
)

type TLS struct {
	CertFile, KeyFile string
}

type Server struct {
	*Router
	Addr string
	TLS  *TLS
}

func (s *Server) Listen(addr string, tls *TLS) error {
	if tls != nil {
		s.TLS = tls
		return http.ListenAndServeTLS(addr, tls.CertFile, tls.KeyFile, s)
	}

	return http.ListenAndServe(addr, s)
}

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	s.Router.serveHTTP(newResponseWriter(w), newRequest(r))
}

func NewServer() *Server {
	s := &Server{NewRouter(), "", nil}
	s.ErrorHandler = StdErrorHandler

	return s
}

Use Request and ResponseWriter creators in Server


// Copyright (c) 2010 AFP Authors
// This source code is released under the terms of the
// MIT license. Please see the file LICENSE for license details.

//This is not a legal Go program, rather it provides a skeletal
//filter to serve as a minimal base for developing filters.

package <packagename>

import (
	"afp"
	"os"
)

type SkeletonFilter struct {
	ctx *afp.Context
}

func (self *SkeletonFilter) Init(ctx *afp.Context, args []string) os.Error {
	self.ctx = ctx

	parser := flags.FlagParser(args)
	var a *int = parser.Int("a", DEFAULT_VALUE, "Argument Description")
	parser.Parse()

	return nil
}

func (self *SkeletonFilter) Stop() os.Error {
	return nil
}

func (self *SkeletonFilter) GetType() int {
	return afp.PIPE_< SOURCE | LINK | SINK >
}

func (self *SkeletonFilter) Start() {
	//The first thing Start should do is store
	//and pass on the header info.
	header := <-self.ctx.HeaderSource
	self.ctx.HeaderSink <- header

	for frame := range self.ctx.Source {
		//Process frame
	}
}

func NewSkeleton() afp.Filter {
	return &SkeletonFilter{}
}

// Copyright (c) 2010 AFP Authors
// This source code is released under the terms of the
// MIT license. Please see the file LICENSE for license details.

//This is not a legal Go program, rather it provides a skeletal
//filter to serve as a minimal base for developing filters.

package <packagename>

import (
	"afp"
	"afp/flags"
	"os"
)

type SkeletonFilter struct {
	ctx *afp.Context
}

func (self *SkeletonFilter) Init(ctx *afp.Context, args []string) os.Error {
	self.ctx = ctx

	parser := flags.FlagParser(args)
	a := parser.Int("a", DEFAULT_VALUE, "Argument Description")
	parser.Parse()

	return nil
}

func (self *SkeletonFilter) Stop() os.Error {
	return nil
}

func (self *SkeletonFilter) GetType() int {
	return afp.PIPE_< SOURCE | LINK | SINK >
}

func (self *SkeletonFilter) Start() {
	//The first thing Start should do is store
	//and pass on the header info.
	header := <-self.ctx.HeaderSource
	self.ctx.HeaderSink <- header

	//Then process the content til there's no more to be had
	for frame := range self.ctx.Source {
		//Process frame
	}
}

func NewSkeleton() afp.Filter {
	return &SkeletonFilter{}
}

Make flag parsing example a bit more idiomatic


/*

jwthelper is a Golang package that provides JWT(JSON Web Token) functions based on jwt-go.

*/
package jwthelper

/*

Package jwthelper is a Golang package that provides JWT(JSON Web Token) functions based on jwt-go.

*/
package jwthelper

Insert 'Package' before package description to remove golint warnings.


package utils

import (
    "regexp"
    "strings"
    "unicode"
    "unicode/utf8"
)

const (
    RE_NOT_SEPARATORS = `[^[\s\.,:\*\+;\?\\\-\(\)\[\]{}<>'"#«»№\/!]+`
)

var (
    re = regexp.MustCompile(RE_NOT_SEPARATORS)
)

func UpperInitial(str string) string {
    if len(str) > 0 {
        process := strings.ToLower(str)
        r, size := utf8.DecodeRuneInString(process)
        return string(unicode.ToUpper(r)) + process[size:]
    }
    return ""
}

func UpperInitialAll(src string) string {
    return re.ReplaceAllStringFunc(src, func(str string) string {
        return UpperInitial(str)
    })
}

func SplitBySeparators(src string) []string {
    return re.FindAllString(src, -1)
}

package utils

import (
    "regexp"
    "strings"
    "unicode"
    "unicode/utf8"
)

const (
    RE_NOT_SEPARATORS = `[^[\s\.,:\*\+;\?\\\-—_\(\)\[\]{}<>'"#«»№\/!]+`
)

var (
    re = regexp.MustCompile(RE_NOT_SEPARATORS)
)

func UpperInitial(str string) string {
    if len(str) > 0 {
        process := strings.ToLower(str)
        r, size := utf8.DecodeRuneInString(process)
        return string(unicode.ToUpper(r)) + process[size:]
    }
    return ""
}

func UpperInitialAll(src string) string {
    return re.ReplaceAllStringFunc(src, func(str string) string {
        return UpperInitial(str)
    })
}

func SplitBySeparators(src string) []string {
    return re.FindAllString(src, -1)
}

Add more dividers to the regexp


package orm

import (
	"github.com/go-pg/pg/v10/types"
)

type mapModel struct {
	hookStubs
	ptr *map[string]interface{}
	m   map[string]interface{}
}

var _ Model = (*mapModel)(nil)

func newMapModel(ptr *map[string]interface{}) *mapModel {
	return &mapModel{
		ptr: ptr,
	}
}

func (mapModel) Init() error {
	return nil
}

func (m mapModel) NextColumnScanner() ColumnScanner {
	return m
}

func (m mapModel) AddColumnScanner(ColumnScanner) error {
	return nil
}

func (m mapModel) ScanColumn(col types.ColumnInfo, rd types.Reader, n int) error {
	val, err := types.ReadColumnValue(col, rd, n)
	if err != nil {
		return err
	}

	if m.m == nil {
		m.m = make(map[string]interface{})
		*m.ptr = m.m
	}

	m.m[col.Name] = val
	return nil
}

func (mapModel) useQueryOne() bool {
	return true
}

package orm

import (
	"github.com/go-pg/pg/v10/types"
)

type mapModel struct {
	hookStubs
	ptr *map[string]interface{}
	m   map[string]interface{}
}

var _ Model = (*mapModel)(nil)

func newMapModel(ptr *map[string]interface{}) *mapModel {
	model := &mapModel{
		ptr: ptr,
	}
	if ptr != nil {
		model.m = *ptr
	}
	return model
}

func (mapModel) Init() error {
	return nil
}

func (m *mapModel) NextColumnScanner() ColumnScanner {
	return m
}

func (m mapModel) AddColumnScanner(ColumnScanner) error {
	return nil
}

func (m *mapModel) ScanColumn(col types.ColumnInfo, rd types.Reader, n int) error {
	val, err := types.ReadColumnValue(col, rd, n)
	if err != nil {
		return err
	}

	if m.m == nil {
		m.m = make(map[string]interface{})
		*m.ptr = m.m
	}

	m.m[col.Name] = val
	return nil
}

func (mapModel) useQueryOne() bool {
	return true
}

Fix map update and insert


/*

The plist command parses a plist file and indicates whether the parse was
successful.

*/
package main

import (
	"os"

	"github.com/jason0x43/go-plist"
)

func main() {
	if len(os.Args) != 2 {
		println("usage:", os.Args[0], "PLIST")
		os.Exit(1)
	}

	pl, err := plist.UnmarshalFile(os.Args[1])
	if err != nil {
		println("error unmarshalling plist:", err.Error())
		os.Exit(1)
	}

	println("Read plist of version", pl.Version)

	switch pl.Root.(type) {
	case plist.Dict:
		println("Root is a dict")
	case plist.Array:
		println("Root is an array")
	}

	if err != nil {
		println("error:", err.Error())
	} else {
		println("Parsed plist")
	}
}

Add description for plist command.


package clair

import (
	"fmt"
	"net/http"
	"os"
	"strconv"
	"strings"
)

func IsHealthy() bool {
	Config()
	healthUri := strings.Replace(uri, "6060", strconv.Itoa(healthPort), 1)
	response, err := http.Get(healthUri + "/health")

	if err != nil {

		fmt.Fprintf(os.Stderr, "requesting Clair health: %v", err)
		return false
	}
	defer response.Body.Close()

	if response.StatusCode != http.StatusOK {
		return false
	}

	return true
}

package clair

import (
	"fmt"
	"net/http"
	"os"
	"strconv"
	"strings"
)

func IsHealthy() bool {
	Config()
	healthURI := strings.Replace(uri, "6060/v1", strconv.Itoa(healthPort), 1) + "/health"
	response, err := http.Get(healthURI)
	if err != nil {

		fmt.Fprintf(os.Stderr, "requesting Clair health: %v", err)
		return false
	}
	defer response.Body.Close()

	if response.StatusCode != http.StatusOK {
		return false
	}

	return true
}

Fix Healthcheck, by removing v1 in url


package jwt

import (
	"testing"
	"time"

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

func TestVerifyExp(t *testing.T) {
	assert := assert.New(t)
	now := time.Now()

	{
		exp := now.Unix()
		assert.False(VerifyExp(0, exp), "False if now == exp")
	}
	{
		exp := now.Add(time.Second).Unix()
		assert.True(VerifyExp(0, exp), "True if now < exp")
	}
	{
		skew := time.Second
		assert.True(VerifyExp(skew, now.Unix()), "True if now == exp + 1s")
	}
}

package jwt

import (
	"testing"
	"time"

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

func TestVerifyExp(t *testing.T) {
	assert := assert.New(t)
	now := time.Now()

	{
		exp := now.Unix()
		assert.False(verifyExp(now, 0, exp), "False if now == exp")
	}
	{
		exp := now.Add(time.Second).Unix()
		assert.True(verifyExp(now, 0, exp), "True if now < exp")
	}
	{
		skew := time.Second
		assert.True(verifyExp(now, skew, now.Unix()), "True if now == exp + 1s")
	}
}

Use verifyExp to inject now


package base

import "fmt"

// EvaluatedCandidate is an immutable wrapper for associating a candidate
// solution with its fitness score.
type EvaluatedCandidate struct {
	candidate Candidate
	fitness   float64
}

// NewEvaluatedCandidate returns an EvaluatedCandidate
func NewEvaluatedCandidate(candidate Candidate, fitness float64) (*EvaluatedCandidate, error) {
	if fitness < 0 {
		return nil, fmt.Errorf("fitness score must be >= 0, got %v", fitness)
	}
	return &EvaluatedCandidate{
		candidate: candidate,
		fitness:   fitness,
	}, nil
}

// Candidate returns the evolved candidate solution.
func (ec *EvaluatedCandidate) Candidate() Candidate {
	return ec.candidate
}

// Fitness returns the fitness score for the associated candidate.
func (ec *EvaluatedCandidate) Fitness() float64 {
	return ec.fitness
}

// Equals returns true If this object is logically equivalent to {code o}.
func (ec *EvaluatedCandidate) Equals(o *EvaluatedCandidate) bool {
	if ec == o {
		return true
	} else if o == nil {
		return false
	}
	return ec.Fitness() == o.Fitness()
}

// CompareTo compares this candidate's fitness score with that of the specified
// candidate.
//
// Returns -1, 0 or 1 if this candidate's score is less than, equal to, or
// greater than that of the specified candidate. The comparison applies to the
// raw numerical score and does not consider whether that score is a natural
// fitness score or not.
func (ec *EvaluatedCandidate) CompareTo(o *EvaluatedCandidate) int {
	switch {
	case ec.Fitness() < o.Fitness():
		return -1
	case ec.Fitness() > o.Fitness():
		return 1
	}
	return 0
}

Add comparison method to EvaluatedCandidate


package vault

// BarrierEncryptorAccess is a wrapper around BarrierEncryptor that allows Core
// to expose its barrier encrypt/decrypt operations through BarrierEncryptorAccess()
// while restricting the ability to modify Core.barrier itself.
type BarrierEncryptorAccess struct {
	barrierEncryptor BarrierEncryptor
}

var _ BarrierEncryptor = (*BarrierEncryptorAccess)(nil)

func NewBarrierEncryptorAccess(barrierEncryptor BarrierEncryptor) *BarrierEncryptorAccess {
	return &BarrierEncryptorAccess{barrierEncryptor: barrierEncryptor}
}

func (b *BarrierEncryptorAccess) Encrypt(key string, plaintext []byte) ([]byte, error) {
	return b.barrierEncryptor.Encrypt(key, plaintext)
}

func (b *BarrierEncryptorAccess) Decrypt(key string, ciphertext []byte) ([]byte, error) {
	return b.barrierEncryptor.Decrypt(key, ciphertext)
}

package vault

import "context"

// BarrierEncryptorAccess is a wrapper around BarrierEncryptor that allows Core
// to expose its barrier encrypt/decrypt operations through BarrierEncryptorAccess()
// while restricting the ability to modify Core.barrier itself.
type BarrierEncryptorAccess struct {
	barrierEncryptor BarrierEncryptor
}

var _ BarrierEncryptor = (*BarrierEncryptorAccess)(nil)

func NewBarrierEncryptorAccess(barrierEncryptor BarrierEncryptor) *BarrierEncryptorAccess {
	return &BarrierEncryptorAccess{barrierEncryptor: barrierEncryptor}
}

func (b *BarrierEncryptorAccess) Encrypt(ctx context.Context, key string, plaintext []byte) ([]byte, error) {
	return b.barrierEncryptor.Encrypt(key, plaintext)
}

func (b *BarrierEncryptorAccess) Decrypt(ctx context.Context, key string, ciphertext []byte) ([]byte, error) {
	return b.barrierEncryptor.Decrypt(key, ciphertext)
}

Add context to barrier encryptor access


package qb

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

func TestLogger(t *testing.T) {
	db, err := New("sqlite3", ":memory:")
	actors := Table("actors",
		Column("id", BigInt().NotNull()),
		PrimaryKey("id"),
	)
	db.Metadata().AddTable(actors)
	db.CreateAll()
	defer db.DropAll()
	db.Engine().SetLogger(DefaultLogger{LQuery | LBindings, log.New(os.Stdout, "", log.LstdFlags)})
	db.Engine().Logger().SetLogFlags(LQuery)

	_, err = db.Engine().Exec(actors.Insert().Values(map[string]interface{}{"id": 5}))
	assert.Nil(t, err)

	db.Engine().Logger().SetLogFlags(LBindings)
	_, err = db.Engine().Exec(actors.Insert().Values(map[string]interface{}{"id": 10}))
	assert.Nil(t, err)

	assert.Equal(t, db.Engine().Logger().LogFlags(), LQuery|LBindings)
}

package qb

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

func TestLogger(t *testing.T) {
	db, err := New("sqlite3", ":memory:")
	actors := Table("actors",
		Column("id", BigInt().NotNull()),
		PrimaryKey("id"),
	)
	db.Metadata().AddTable(actors)
	db.CreateAll()
	defer db.DropAll()
	db.Engine().SetLogger(DefaultLogger{LQuery | LBindings, log.New(ioutil.Discard, "", log.LstdFlags)})
	db.Engine().Logger().SetLogFlags(LQuery)

	_, err = db.Engine().Exec(actors.Insert().Values(map[string]interface{}{"id": 5}))
	assert.Nil(t, err)

	db.Engine().Logger().SetLogFlags(LBindings)
	_, err = db.Engine().Exec(actors.Insert().Values(map[string]interface{}{"id": 10}))
	assert.Nil(t, err)

	assert.Equal(t, db.Engine().Logger().LogFlags(), LQuery|LBindings)
}

Test logger on ioutil.Discard (not stdout)


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

package main

import (
	"flag"
	"log"

	"github.com/lionkov/go9p/p/srv/ufs"
	"github.com/lionkov/go9p/p/clnt"
)

var (
	debug = flag.Int("d", 0, "print debug messages")
	addr = flag.String("addr", ":5640", "network address")
)

func main() {
	flag.Parse()
	ufs := new(ufs.Ufs)
	ufs.Dotu = true
	ufs.Id = "ufs"
	ufs.Debuglevel = *debug
	ufs.Start(ufs)

	err := ufs.StartNetListener("tcp", *addr)
	if err != nil {
		log.Println(err)
	}
}

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

package main

import (
	"flag"
	"log"

	"github.com/lionkov/go9p/p/srv/ufs"
)

var (
	debug = flag.Int("d", 0, "print debug messages")
	addr = flag.String("addr", ":5640", "network address")
)

func main() {
	flag.Parse()
	ufs := new(ufs.Ufs)
	ufs.Dotu = true
	ufs.Id = "ufs"
	ufs.Debuglevel = *debug
	ufs.Start(ufs)

	err := ufs.StartNetListener("tcp", *addr)
	if err != nil {
		log.Println(err)
	}
}

Fix an error that made it not go-buildable.


package main

import (
	"flag"
	"io/ioutil"
	"log"
	"os"
	"os/signal"
	"strings"

	"github.com/localhots/satan"
	"github.com/localhots/satan/example/daemons"
	"github.com/localhots/satan/example/kafka"
)

func main() {
	var debug bool
	var brokers string

	flag.BoolVar(&debug, "v", false, "Verbose mode")
	flag.StringVar(&brokers, "brokers", "127.0.0.1:9092", "Kafka broker addresses separated by space")
	flag.Parse()

	log.SetOutput(ioutil.Discard)
	if debug {
		log.SetOutput(os.Stderr)
	}

	kafka.Initialize(strings.Split(brokers, " "))
	defer kafka.Shutdown()

	s := satan.Summon()
	s.SubscribeFunc = kafka.Subscribe
	s.AddDaemon(&daemons.NumberPrinter{})
	s.AddDaemon(&daemons.PriceConsumer{})

	s.StartDaemons()
	defer s.StopDaemons()

	sig := make(chan os.Signal)
	signal.Notify(sig, os.Interrupt)
	<-sig
}

package main

import (
	"flag"
	"io/ioutil"
	"log"
	"os"
	"os/signal"
	"strings"

	"github.com/localhots/satan"
	"github.com/localhots/satan/example/daemons"
	"github.com/localhots/satan/example/kafka"
	"github.com/localhots/satan/stats"
)

func main() {
	var debug bool
	var brokers string

	flag.BoolVar(&debug, "v", false, "Verbose mode")
	flag.StringVar(&brokers, "brokers", "127.0.0.1:9092", "Kafka broker addresses separated by space")
	flag.Parse()

	log.SetOutput(ioutil.Discard)
	if debug {
		log.SetOutput(os.Stderr)
	}

	kafka.Initialize(strings.Split(brokers, " "))
	defer kafka.Shutdown()

	logger := stats.NewStdoutLogger(0)
	defer logger.Print()

	s := satan.Summon()
	s.SubscribeFunc = kafka.Subscribe
	s.Statistics = logger

	s.AddDaemon(&daemons.NumberPrinter{})
	s.AddDaemon(&daemons.PriceConsumer{})

	s.StartDaemons()
	defer s.StopDaemons()

	sig := make(chan os.Signal)
	signal.Notify(sig, os.Interrupt)
	<-sig
}

Use statistics logger in example app


package grid

type Square bool

type Clue []int

const (
	White Square = iota
	Black
)

type Grid struct {
	rowClues, columnClues []Clue
	squares               []Square
}

package grid

type Square bool

type Clue []int

const (
	White Square = false
	Black Square = true
)

type Grid struct {
	rowClues, columnClues []Clue
	squares               []Square
}

Change Squares from ints to bools


package stream

import "github.com/synapse-garden/sg-proto/store"

// Removed is a notification Resourcer that can inform a user they have
// been removed from the Stream without informing them of any other
// information about the Stream.
type Removed string

// Resource implements Resourcer.Resource on Removed.
func (Removed) Resource() store.Resource { return "removed" }

// Connected is a notification Resourcer that can inform a user someone
// has joined the Stream.
type Connected string

// Resource implements Resourcer.Resource on Connected.
func (Connected) Resource() store.Resource { return "connected" }

// Disconnected is a notification Resourcer that can inform a user
// someone has left the Stream.
type Disconnected string

// Resource implements Resourcer.Resource on Disconnected.
func (Disconnected) Resource() store.Resource { return "disconnected" }

// Deleted is a notification Resourcer that notifies the user a resource
// has been deleted.
type Deleted string

// Resource implements Resourcer.Resource on Deleted.
func (Deleted) Resource() store.Resource { return "deleted" }

Add stream Deleted and Disconnected Resourcers


package main

import "fmt"

func main() {
    fmt.Println("Ding!")
}

package main

import "fmt"
import "os/exec"

func main() {
    fmt.Println("Ding!")
    cmd := exec.Command("paplay", "/usr/share/sounds/freedesktop/stereo/complete.oga")
    cmd.Start()
}

Use os/exec and paplay to play sound


package sprockets

import (
	"html/template"
)

func NullSprockets() ViewHelper {
	return ViewHelper{nullSprockets{}}
}

type nullSprockets struct{}

func (s nullSprockets) GetAssetUrl(name string) (template.HTMLAttr, error) {
	panic("Null sprockets in use, cannot get asset urls")
}

func (s nullSprockets) GetAssetContents(name string) ([]byte, error) {
	panic("Null sprockets in use, cannot get asset contents")
}

package sprockets

import (
	"html/template"
)

func NullSprockets() ViewHelper {
	return ViewHelper{nullSprockets{}}
}

type nullSprockets struct{}

func (s nullSprockets) GetAssetUrl(name string) (template.HTMLAttr, error) {
	return "", nil
}

func (s nullSprockets) GetAssetContents(name string) ([]byte, error) {
	return make([]byte, 0), nil
}

Allow null sprockets to be used for templating


package main

import (
	ccli "github.com/codegangsta/cli"
	"github.com/myodc/go-micro/cmd"
	"github.com/myodc/micro/api"
	"github.com/myodc/micro/car"
	"github.com/myodc/micro/cli"
)

func main() {
	app := ccli.NewApp()
	app.Name = "micro"
	app.Usage = "A microservices toolchain"
	app.Version = "0.0.1"
	app.Commands = append(app.Commands, api.Commands()...)
	app.Commands = append(app.Commands, cli.Commands()...)
	app.Commands = append(app.Commands, car.Commands()...)
	app.Flags = cmd.Flags
	app.Before = cmd.Setup
	app.RunAndExitOnError()
}

package main

import (
	ccli "github.com/codegangsta/cli"
	"github.com/myodc/go-micro/cmd"
	"github.com/myodc/micro/api"
	"github.com/myodc/micro/car"
	"github.com/myodc/micro/cli"
)

func main() {
	app := ccli.NewApp()
	app.Name = "micro"
	app.Usage = "A microservices toolchain"
	app.HideVersion = true
	app.Commands = append(app.Commands, api.Commands()...)
	app.Commands = append(app.Commands, cli.Commands()...)
	app.Commands = append(app.Commands, car.Commands()...)
	app.Flags = cmd.Flags
	app.Before = cmd.Setup
	app.RunAndExitOnError()
}

Fix panic caused by redeclaration of v flag


package main

import (
	"github.com/alex1sz/shotcharter-go/db"
	_ "github.com/lib/pq"
)

func main() {
	// fmt.Printf("Hello, world.\n")
	db.Init()
}

package main

import (
	"github.com/alex1sz/shotcharter-go/db"
)

func main() {
	db.Db.Ping()
}

Move lib/pq into db package


package main

import (
	"fmt"
	"os"

	"github.com/open-falcon/falcon-plus/cmd"
	"github.com/spf13/cobra"
	flag "github.com/spf13/pflag"
)

var versionFlag bool

var RootCmd = &cobra.Command{
	Use: "open-falcon",
}

func init() {
	RootCmd.AddCommand(cmd.Start)
	RootCmd.AddCommand(cmd.Stop)
	RootCmd.AddCommand(cmd.Restart)
	RootCmd.AddCommand(cmd.Check)
	RootCmd.AddCommand(cmd.Monitor)
	RootCmd.AddCommand(cmd.Reload)
	cmd.Start.Flags().BoolVar(&cmd.PreqOrderFlag, "preq-order", false, "start modules in the order of prerequisites")
	cmd.Start.Flags().BoolVar(&cmd.ConsoleOutputFlag, "console-output", false, "print the module's output to the console")
	flag.BoolVarP(&versionFlag, "version", "v", false, "show version")
	flag.Parse()
}

func main() {
	if versionFlag {
		fmt.Printf("Open-Falcon version %s, build %s\n", Version, GitCommit)
		os.Exit(0)
	}
	if err := RootCmd.Execute(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}

package main

import (
	"fmt"
	"os"

	"github.com/open-falcon/falcon-plus/cmd"
	"github.com/spf13/cobra"
)

var versionFlag bool

var RootCmd = &cobra.Command{
	Use: "open-falcon",
	Run: func(cmd *cobra.Command, args []string) {
		if versionFlag {
			fmt.Printf("Open-Falcon version %s, build %s\n", Version, GitCommit)
			os.Exit(0)
		}
	},
}

func init() {
	RootCmd.AddCommand(cmd.Start)
	RootCmd.AddCommand(cmd.Stop)
	RootCmd.AddCommand(cmd.Restart)
	RootCmd.AddCommand(cmd.Check)
	RootCmd.AddCommand(cmd.Monitor)
	RootCmd.AddCommand(cmd.Reload)

	RootCmd.Flags().BoolVarP(&versionFlag, "version", "v", false, "show version")
	cmd.Start.Flags().BoolVar(&cmd.PreqOrderFlag, "preq-order", false, "start modules in the order of prerequisites")
	cmd.Start.Flags().BoolVar(&cmd.ConsoleOutputFlag, "console-output", false, "print the module's output to the console")
}

func main() {
	if err := RootCmd.Execute(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}

Fix the flags of subcommands


package ratelimit

import "time"

type basicLimiter struct {
	t   *time.Ticker
	bc  ByteCount
	cbc chan ByteCount
}

func (bl *basicLimiter) Start() {
	for {
		<-bl.t.C
		bl.cbc <- bl.bc
	}
}

func (bl basicLimiter) GetLimit() <-chan ByteCount {
	return bl.cbc
}

const timeSlice = 20 * time.Millisecond

//BasicLimiter will divvy up the bytes into 100 smaller parts to spread the load
//across time
func BasicLimiter(b ByteCount, t time.Duration) Limiter {
	bl := &basicLimiter{
		t:   time.NewTicker(timeSlice),
		bc:  b / ByteCount(t/timeSlice),
		cbc: make(chan ByteCount),
	}
	go bl.Start()
	return bl
}

package ratelimit

import "time"

type basicLimiter struct {
	t   *time.Ticker
	bc  ByteCount
	cbc []chan ByteCount
}

func (bl *basicLimiter) Start() {
	for {
		<-bl.t.C

		perChan := bl.bc / ByteCount(len(bl.cbc))

		for i := range bl.cbc {
			go func(i int) {
				bl.cbc[i] <- perChan
			}(i)
		}
	}
}

func (bl *basicLimiter) GetLimit() <-chan ByteCount {
	ch := make(chan ByteCount)
	bl.cbc = append(bl.cbc, ch)
	return ch
}

const timeSlice = 20 * time.Millisecond

//NewBasicLimiter will appropriately distribute the rate given across 20ms
//windows. If used to create multiple LimitedReaders (or if GetLimit called
//multiple times), it will divvy up the rate across all the readers, at the same
//rate.
func NewBasicLimiter(b ByteCount, t time.Duration) Limiter {
	bl := &basicLimiter{
		t:   time.NewTicker(timeSlice),
		bc:  b / ByteCount(t/timeSlice),
		cbc: make([]chan ByteCount, 0, 1),
	}
	go bl.Start()
	return bl
}

Update BasicLimiter to spread limit across many readers


package main

import (
	"github.com/dimfeld/glog"
	"net"
)

const (
	WhiteList = iota
	BlackList
)

type ListenFilter struct {
	net.Listener
	// BlackList or WhiteList.
	Behavior   int
	FilterAddr map[string]bool
}

func (f *ListenFilter) Accept() (c net.Conn, err error) {
	for {
		c, err = f.Accept()
		if err != nil {
			return
		}

		addr := c.RemoteAddr().String()
		configured := f.FilterAddr[addr]

		if (configured && f.Behavior == WhiteList) ||
			(!configured && f.Behavior == BlackList) {

			return
		}

		c.Close()

		glog.Infoln("Denied connection from", addr)
	}
}

func NewListenFilter(l net.Listener, behavior int) *ListenFilter {
	return &ListenFilter{
		Listener:   l,
		Behavior:   behavior,
		FilterAddr: make(map[string]bool),
	}
}

package main

import (
	"github.com/dimfeld/glog"
	"net"
)

const (
	WhiteList = iota
	BlackList
)

type ListenFilter struct {
	net.Listener
	// BlackList or WhiteList.
	Behavior   int
	FilterAddr map[string]bool
}

func (f *ListenFilter) Accept() (c net.Conn, err error) {
	for {
		c, err = f.Listener.Accept()
		if err != nil {
			return
		}

		addr := c.RemoteAddr().String()
		addr, _, err = net.SplitHostPort(addr)
		configured := f.FilterAddr[addr]

		if (configured && f.Behavior == WhiteList) ||
			(!configured && f.Behavior == BlackList) {

			return
		}

		c.Close()

		glog.Infoln("Denied connection from", addr)
	}
}

func NewListenFilter(l net.Listener, behavior int) *ListenFilter {
	return &ListenFilter{
		Listener:   l,
		Behavior:   behavior,
		FilterAddr: make(map[string]bool),
	}
}

Fix infinite recursion. Compare addresses properly.


package util

import "io"

// A BlackHole is an io.ReadWriter that accepts an infinite
// amount of input and produces no output.
type BlackHole struct{}

func (BlackHole) Read(p []byte) (int, error)  { return 0, io.EOF }
func (BlackHole) Write(p []byte) (int, error) { return len(p), nil }

package util

import "io"

// A BlackHole is an io.ReadWriteCloser that accepts an infinite
// amount of input and produces no output.
type BlackHole struct{}

func (BlackHole) Read(p []byte) (int, error)  { return 0, io.EOF }
func (BlackHole) Write(p []byte) (int, error) { return len(p), nil }
func (BlackHole) Close() error                { return nil }

Implement the `io.ReadWriteCloser` interface for `util.Blackhole`


package memalpha

import (
	"bufio"
	"bytes"
	"strings"
	"testing"
)

func TestHandleServerError(t *testing.T) {
	errorMessage := "test fake"
	response := bytes.NewReader([]byte("SERVER_ERROR " + errorMessage))
	request := bytes.NewBuffer([]byte{})

	serverReadWriter := bufio.NewReadWriter(bufio.NewReader(response), bufio.NewWriter(request))

	c := &Client{rw: serverReadWriter}

	err := c.Set("foo", []byte("bar"))
	e, ok := err.(ServerError)
	if ok && strings.Contains(e.Error(), "server error: "+errorMessage) {
		return
	}

	t.Fatalf("set(foo): Error = %v, want ServerError: test fake", err)
}

package memalpha

import (
	"bufio"
	"bytes"
	"strings"
	"testing"

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

func TestServerError(t *testing.T) {
	errorMessage := "test fake"
	response := bytes.NewReader([]byte("SERVER_ERROR " + errorMessage))
	request := bytes.NewBuffer([]byte{})

	serverReadWriter := bufio.NewReadWriter(bufio.NewReader(response), bufio.NewWriter(request))

	c := &Client{rw: serverReadWriter}

	err := c.Set("foo", []byte("bar"))
	e, ok := err.(ServerError)
	if ok && strings.Contains(e.Error(), "server error: "+errorMessage) {
		return
	}

	t.Fatalf("set(foo): Error = %v, want ServerError: test fake", err)
}

func TestClientError(t *testing.T) {
	errorMessage := "test fake"
	response := bytes.NewReader([]byte("CLIENT_ERROR " + errorMessage))
	request := bytes.NewBuffer([]byte{})

	serverReadWriter := bufio.NewReadWriter(bufio.NewReader(response), bufio.NewWriter(request))

	c := &Client{rw: serverReadWriter}

	err := c.Set("foo", []byte("bar"))
	e, ok := err.(ClientError)
	if ok && strings.Contains(e.Error(), "client error: "+errorMessage) {
		return
	}

	t.Fatalf("set(foo): Error = %v, want ClientError: test fake", err)
}

func TestReplyError(t *testing.T) {
	response := bytes.NewReader([]byte("ERROR"))
	request := bytes.NewBuffer([]byte{})

	serverReadWriter := bufio.NewReadWriter(bufio.NewReader(response), bufio.NewWriter(request))

	c := &Client{rw: serverReadWriter}

	err := c.Set("foo", []byte("bar"))
	assert.Equal(t, err, ErrReplyError)
}

Add test of ClientError and ReplyError


package main

import (
	"gopkg.in/urfave/cli.v1"
	"os"
)

const version = "0.2.0"

func main() {
	app := cli.NewApp()
	app.Name = "ustat"
	app.Version = version
	app.Usage = "Unified system statistics collector"
	app.Authors = []cli.Author{
		cli.Author{
			Name:  "Pekka Enberg",
			Email: "penberg@iki.fi",
		},
	}
	app.HideHelp = true
	app.Commands = []cli.Command{
		recordCommand,
		reportCommand,
	}
	app.Run(os.Args)
}

package main

import (
	"fmt"
	"gopkg.in/urfave/cli.v1"
	"os"
)

const version = "0.2.0"

func main() {
	app := cli.NewApp()
	app.Name = "ustat"
	app.Version = version
	app.Usage = "Unified system statistics collector"
	app.Authors = []cli.Author{
		cli.Author{
			Name:  "Pekka Enberg",
			Email: "penberg@iki.fi",
		},
	}
	app.HideHelp = true
	app.Commands = []cli.Command{
		recordCommand,
		reportCommand,
	}
	if err := app.Run(os.Args); err != nil {
		fmt.Fprintf(os.Stderr, "error: %v\n", err)
		os.Exit(1)
	}
}

Exit proces with error message and error code


package selector

import (
	"math/rand"
	"sync"
	"time"

	"github.com/micro/go-micro/registry"
)

func init() {
	rand.Seed(time.Now().UnixNano())
}

// Random is a random strategy algorithm for node selection
func Random(services []*registry.Service) Next {
	var nodes []*registry.Node

	for _, service := range services {
		nodes = append(nodes, service.Nodes...)
	}

	return func() (*registry.Node, error) {
		if len(nodes) == 0 {
			return nil, ErrNoneAvailable
		}

		i := rand.Int() % len(nodes)
		return nodes[i], nil
	}
}

// RoundRobin is a roundrobin strategy algorithm for node selection
func RoundRobin(services []*registry.Service) Next {
	var nodes []*registry.Node

	for _, service := range services {
		nodes = append(nodes, service.Nodes...)
	}

	var i int
	var mtx sync.Mutex

	return func() (*registry.Node, error) {
		if len(nodes) == 0 {
			return nil, ErrNoneAvailable
		}

		mtx.Lock()
		node := nodes[i%len(nodes)]
		i++
		mtx.Unlock()

		return node, nil
	}
}

package selector

import (
	"math/rand"
	"sync"
	"time"

	"github.com/micro/go-micro/registry"
)

func init() {
	rand.Seed(time.Now().UnixNano())
}

// Random is a random strategy algorithm for node selection
func Random(services []*registry.Service) Next {
	var nodes []*registry.Node

	for _, service := range services {
		nodes = append(nodes, service.Nodes...)
	}

	return func() (*registry.Node, error) {
		if len(nodes) == 0 {
			return nil, ErrNoneAvailable
		}

		i := rand.Int() % len(nodes)
		return nodes[i], nil
	}
}

// RoundRobin is a roundrobin strategy algorithm for node selection
func RoundRobin(services []*registry.Service) Next {
	var nodes []*registry.Node

	for _, service := range services {
		nodes = append(nodes, service.Nodes...)
	}

	var i = rand.Int()
	var mtx sync.Mutex

	return func() (*registry.Node, error) {
		if len(nodes) == 0 {
			return nil, ErrNoneAvailable
		}

		mtx.Lock()
		node := nodes[i%len(nodes)]
		i++
		mtx.Unlock()

		return node, nil
	}
}

Use random starting index for roundrobin


// Package id is an authentication package that is
// sessionless, and passwordless.
package id

// Copyright 2015 Ben Tranter. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.

/*
Package id is an authentication package that is sessionless, and passwordless.

*/
package id

Add copyright notice to see if renders on GoDoc.org


package projects

import (
	"github.com/DVI-GI-2017/Jira__backend/models"
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
)

const collection = "labels"

func CheckExistence(mongo *mgo.Database, label *models.Label) (bool, error) {
	c, err := mongo.C(collection).Find(bson.M{"name": label.Name}).Count()
	return c != 0, err
}

func Create(mongo *mgo.Database, label interface{}) (result interface{}, err error) {
	return label, mongo.C(collection).Insert(label)
}

func All(mongo *mgo.Database) (result models.LabelsList, err error) {
	const defaultSize = 100
	result = make(models.LabelsList, defaultSize)

	err = mongo.C(collection).Find(bson.M{}).All(&result)
	return
}

func FindById(mongo *mgo.Database, id bson.ObjectId) (*models.Label, error) {
	label := new(models.Label)
	err := mongo.C(collection).FindId(id).One(label)
	return label, err
}

package labels

import (
	"github.com/DVI-GI-2017/Jira__backend/models"
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
)

const collection = "labels"

func CheckExistence(mongo *mgo.Database, label *models.Label) (bool, error) {
	c, err := mongo.C(collection).Find(bson.M{"name": label.Name}).Count()
	return c != 0, err
}

func Create(mongo *mgo.Database, label interface{}) (result interface{}, err error) {
	return label, mongo.C(collection).Insert(label)
}

func All(mongo *mgo.Database) (result models.LabelsList, err error) {
	const defaultSize = 100
	result = make(models.LabelsList, defaultSize)

	err = mongo.C(collection).Find(bson.M{}).All(&result)
	return
}

func FindById(mongo *mgo.Database, id bson.ObjectId) (*models.Label, error) {
	label := new(models.Label)
	err := mongo.C(collection).FindId(id).One(label)
	return label, err
}

Fix typo in package name.


package main

import (
	"os"

	"github.com/urfave/cli"
)

func runApp() {
	app := cli.NewApp()

	app.Name = "alpinepass"
	app.Version = "0.0.0"
	app.Author = "appPlant GmbH"
	app.Usage = "Manage system environment information."

	app.Flags = []cli.Flag{
		cli.StringSliceFlag{
			Name:  "filter, f",
			Usage: "Filter configurations by name, type and more.",
		},
		cli.StringFlag{
			Name:  "input, i",
			Usage: "Specify the input file path.",
		},
		cli.StringFlag{
			Name:  "output, o",
			Usage: "Specify the output format.",
		},
		cli.BoolFlag{
			Name:  "passwords, p",
			Usage: "Include passwords in the output.",
		},
		cli.BoolFlag{
			Name:  "show, s",
			Usage: "Show the output in the console. An output file will not be written.",
		},
	}

	app.Action = func(context *cli.Context) error {
		if context.GlobalBool("show") {
			return runShowCommand(context)
		} else {
			return runOutCommand(context)
		}
	}

	app.Run(os.Args)
}

package main

import (
	"os"

	a "github.com/appPlant/alpinepass/app"
	"github.com/urfave/cli"
)

func runApp() {
	app := cli.NewApp()

	app.Name = "alpinepass"
	app.Version = "0.0.0"
	app.Author = "appPlant GmbH"
	app.Usage = "Manage system environment information."

	app.Flags = []cli.Flag{
		cli.StringSliceFlag{
			Name:  "filter, f",
			Usage: "Filter configurations by name, type and more.",
		},
		cli.StringFlag{
			Name:  "input, i",
			Usage: "Specify the input file path.",
		},
		cli.StringFlag{
			Name:  "output, o",
			Usage: "Specify the output format.",
		},
		cli.BoolFlag{
			Name:  "passwords, p",
			Usage: "Include passwords in the output.",
		},
		cli.BoolFlag{
			Name:  "show, s",
			Usage: "Show the output in the console. An output file will not be written.",
		},
	}

	app.Action = func(context *cli.Context) error {
		if context.GlobalBool("show") {
			return a.RunShowCommand(context)
		}
		return a.RunOutCommand(context)
	}

	app.Run(os.Args)
}

Use commands defined in app package


package command

import (
	"fmt"
	"time"

	"github.com/igungor/tlbot"
)

func init() {
	register(cmdToday)
}

var cmdToday = &Command{
	Name:      "bugun",
	ShortLine: "bugun gunlerden ne?",
	Run:       runToday,
}

type weekday time.Weekday

var days = [...]string{
	"pazar",
	"pazartesi",
	"sali",
	"carsamba",
	"persembe",
	"cuma",
	"cumartesi",
}

func (w weekday) String() string {
	return days[w]
}

func runToday(b *tlbot.Bot, msg *tlbot.Message) {
	txt := fmt.Sprintf("bugün %v", weekday(time.Now().Weekday()).String())
	b.SendMessage(msg.Chat, txt, tlbot.ModeNone, false, nil)
}

package command

import (
	"fmt"
	"time"

	"github.com/igungor/tlbot"
)

func init() {
	register(cmdToday)
}

var cmdToday = &Command{
	Name:      "bugun",
	ShortLine: "bugün günlerden ne?",
	Run:       runToday,
}

type weekday time.Weekday

var days = [...]string{
	"pazar",
	"pazartesi",
	"sali",
	"carsamba",
	"persembe",
	"cuma",
	"cumartesi",
}

func (w weekday) String() string {
	return days[w]
}

func runToday(b *tlbot.Bot, msg *tlbot.Message) {
	txt := fmt.Sprintf("bugün %v", weekday(time.Now().Weekday()).String())
	b.SendMessage(msg.Chat, txt, tlbot.ModeNone, false, nil)
}

Use proper characters for command descriptions


package config

import (
	"flag"

	"github.com/vharitonsky/iniflags"
)

var (
	Name     = flag.String("name", "tad", "Nick to use in IRC")
	Server   = flag.String("server", "127.0.0.1:6668", "Host:Port to connect to")
	Channels = flag.String("chan", "#tad", "Channels to join")
	Ssl      = flag.Bool("ssl", false, "Use SSL/TLS")
	Listen   = flag.Bool("listenChannel", false, "Listen for command on public channels")
)

const (
	HostInfoReport = "./data/va_host_info_report.json"
)

func init() {
	iniflags.Parse()
}

package config

import (
	"flag"

	"github.com/vharitonsky/iniflags"
)

var (
	Name     = flag.String("name", "tad", "Nick to use in IRC")
	Server   = flag.String("server", "127.0.0.1:6668", "Host:Port to connect to")
	Channels = flag.String("chan", "#tad", "Channels to join")
	Ssl      = flag.Bool("ssl", false, "Use SSL/TLS")
	Listen   = flag.Bool("listenChannel", false, "Listen for command on public channels")
	HostInfo = flag.String("hostInfo", "./data/va_host_info_report.json", "Path to host info report")
	Promises = flag.String("promises", "./data/promises.csv", "Path to promises report")
	Classes  = flag.String("classes", "./data/classes.txt", "Path to classes report")
)

const (
	HostInfoReport = "./data/va_host_info_report.json"
)

func init() {
	iniflags.Parse()
}

Add missing flags for reports


package musicbrainz

import (
	"encoding/xml"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
)

func MakeQuery(url string) []byte {
	res, err := http.Get(url)
	if err != nil {
		log.Fatal(err)
	}
	bytes, err := ioutil.ReadAll(res.Body)

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

	return bytes
}

func SearchArtist(artist string) ArtistResult {
	result := ArtistResult{}

	bytes := MakeQuery("http://musicbrainz.org/ws/2/artist/?limit=10&query=artist:" + url.QueryEscape(artist))

	xml.Unmarshal(bytes, &result)

	return result
}

func GetReleases(artistId string) ReleaseResult {
	result := ReleaseResult{}
	bytes := MakeQuery("http://musicbrainz.org/ws/2/release?artist=" + artistId)

	xml.Unmarshal(bytes, result)

	return result
}

package musicbrainz

import (
	"encoding/xml"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
)

func MakeQuery(url string) []byte {
	log.Printf("Getting URL %s", url)
	res, err := http.Get(url)
	if err != nil {
		log.Fatal(err)
	}
	bytes, err := ioutil.ReadAll(res.Body)

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

	return bytes
}

func SearchArtist(artist string) ArtistResult {
	result := ArtistResult{}

	bytes := MakeQuery("http://musicbrainz.org/ws/2/artist/?limit=10&query=artist:" + url.QueryEscape(artist))

	xml.Unmarshal(bytes, &result)

	return result
}

func GetReleases(artistId string) ReleaseResult {
	result := ReleaseResult{}
	bytes := MakeQuery("http://musicbrainz.org/ws/2/release?artist=" + artistId)

	xml.Unmarshal(bytes, result)

	return result
}

Add logging of the url.


package flow

// Group represents a specified collection of users.
//
// A user belongs to zero or more groups.  Groups can have associated
// privileges, too.
type Group struct {
	id    uint16
	name  string
	privs []*Privilege
}

package flow

import "fmt"

// Group represents a specified collection of users.
//
// A user belongs to zero or more groups.  Groups can have associated
// privileges, too.
type Group struct {
	id    uint16
	name  string
	privs []*Privilege
}

// NewGroup creates and initialises a group.
//
// Usually, all available groups should be loaded during system
// initialization.  Only groups created during runtime should be added
// dynamically.
func NewGroup(id uint16, name string) (*Group, error) {
	if id == 0 || name == "" {
		return nil, fmt.Errorf("invalid group data -- id: %d, name: %s", id, name)
	}

	return &Group{id: id, name: name}, nil
}

// AddPrivilege includes the given privilege in the set of privileges
// assigned to this group.
func (g *Group) AddPrivilege(p *Privilege) bool {
	for _, el := range g.privs {
		if el.IsOnSameTargetAs(p) {
			return false
		}
	}

	g.privs = append(g.privs, p)
	return true
}

// RemovePrivilegesOn removes the privileges that this group has on the
// given target.
func (g *Group) RemovePrivilegesOn(res *Resource, doc *Document) bool {
	found := false
	idx := -1
	for i, el := range g.privs {
		if el.IsOnTarget(res, doc) {
			found = true
			idx = i
			break
		}
	}
	if !found {
		return false
	}

	g.privs = append(g.privs[:idx], g.privs[idx+1:]...)
	return true
}

// ReplacePrivilege any current privilege on the given target, with
// the given privilege.
func (g *Group) ReplacePrivilege(p *Privilege) bool {
	if !g.RemovePrivilegesOn(p.resource, p.doc) {
		return false
	}

	g.privs = append(g.privs, p)
	return true
}

Add convenience methods to `Group`


package storage

import (
	"time"

	"github.com/docker/distribution"
	"github.com/docker/distribution/digest"
)

// layerReadSeeker implements Layer and provides facilities for reading and
// seeking.
type layerReader struct {
	fileReader

	digest digest.Digest
}

var _ distribution.Layer = &layerReader{}

func (lrs *layerReader) Digest() digest.Digest {
	return lrs.digest
}

func (lrs *layerReader) CreatedAt() time.Time {
	return lrs.modtime
}

// Close the layer. Should be called when the resource is no longer needed.
func (lrs *layerReader) Close() error {
	return lrs.closeWithErr(distribution.ErrLayerClosed)
}

package storage

import (
	"time"

	"github.com/docker/distribution"
	"github.com/docker/distribution/digest"
)

// layerReadSeeker implements Layer and provides facilities for reading and
// seeking.
type layerReader struct {
	fileReader

	digest digest.Digest
}

var _ distribution.Layer = &layerReader{}

func (lrs *layerReader) Digest() digest.Digest {
	return lrs.digest
}

func (lrs *layerReader) Length() int64 {
	return lrs.size
}

func (lrs *layerReader) CreatedAt() time.Time {
	return lrs.modtime
}

// Close the layer. Should be called when the resource is no longer needed.
func (lrs *layerReader) Close() error {
	return lrs.closeWithErr(distribution.ErrLayerClosed)
}

Update notification event Target fields


package clusterupstreamrefresher

import (
	"context"

	gkecontroller "github.com/rancher/gke-operator/controller"
	gkev1 "github.com/rancher/gke-operator/pkg/apis/gke.cattle.io/v1"
	mgmtv3 "github.com/rancher/rancher/pkg/generated/norman/management.cattle.io/v3"
	wranglerv1 "github.com/rancher/wrangler/pkg/generated/controllers/core/v1"
)

func BuildGKEUpstreamSpec(secretsCache wranglerv1.SecretCache, cluster *mgmtv3.Cluster) (*gkev1.GKEClusterConfigSpec, error) {
	ctx := context.Background()
	upstreamCluster, err := gkecontroller.GetCluster(ctx, secretsCache, cluster.Spec.GKEConfig)
	if err != nil {
		return nil, err
	}
	upstreamSpec, err := gkecontroller.BuildUpstreamClusterState(upstreamCluster)
	if err != nil {
		return nil, err
	}

	upstreamSpec.ClusterName = cluster.Spec.GKEConfig.ClusterName
	upstreamSpec.Region = cluster.Spec.GKEConfig.Region
	upstreamSpec.Zone = cluster.Spec.GKEConfig.Zone
	upstreamSpec.GoogleCredentialSecret = cluster.Spec.GKEConfig.GoogleCredentialSecret
	upstreamSpec.ProjectID = cluster.Spec.GKEConfig.ProjectID

	return upstreamSpec, nil
}

package clusterupstreamrefresher

import (
	"context"

	gkecontroller "github.com/rancher/gke-operator/controller"
	gkev1 "github.com/rancher/gke-operator/pkg/apis/gke.cattle.io/v1"
	mgmtv3 "github.com/rancher/rancher/pkg/generated/norman/management.cattle.io/v3"
	wranglerv1 "github.com/rancher/wrangler/pkg/generated/controllers/core/v1"
)

func BuildGKEUpstreamSpec(secretsCache wranglerv1.SecretCache, cluster *mgmtv3.Cluster) (*gkev1.GKEClusterConfigSpec, error) {
	ctx := context.Background()
	upstreamCluster, err := gkecontroller.GetCluster(ctx, secretsCache, cluster.Spec.GKEConfig)
	if err != nil {
		return nil, err
	}
	upstreamSpec, err := gkecontroller.BuildUpstreamClusterState(upstreamCluster)
	if err != nil {
		return nil, err
	}

	upstreamSpec.ClusterName = cluster.Spec.GKEConfig.ClusterName
	upstreamSpec.Region = cluster.Spec.GKEConfig.Region
	upstreamSpec.Zone = cluster.Spec.GKEConfig.Zone
	upstreamSpec.GoogleCredentialSecret = cluster.Spec.GKEConfig.GoogleCredentialSecret
	upstreamSpec.ProjectID = cluster.Spec.GKEConfig.ProjectID
	upstreamSpec.Imported = cluster.Spec.GKEConfig.Imported

	return upstreamSpec, nil
}

Add Imported field to GKE upstream spec builder


// Copyright 2013-2016 Adam Presley. All rights reserved
// Use of this source code is governed by the MIT license
// that can be found in the LICENSE file.

package global

import "github.com/mailslurper/libmailslurper/storage"

const (
	// Version of the MailSlurper Server application
	SERVER_VERSION string = "1.9"
	DEBUG_ASSETS   bool   = false
)

var Database storage.IStorage

// Copyright 2013-2016 Adam Presley. All rights reserved
// Use of this source code is governed by the MIT license
// that can be found in the LICENSE file.

package global

import "github.com/mailslurper/libmailslurper/storage"

const (
	// Version of the MailSlurper Server application
	SERVER_VERSION string = "1.11.1"
	DEBUG_ASSETS   bool   = false
)

var Database storage.IStorage

Update to version string for 1.11.1


package main

import (
	"bytes"
	"github.com/bmizerany/assert"
	"testing"
)

func TestHtpasswd(t *testing.T) {
	file := bytes.NewBuffer([]byte("testuser:{SHA}PaVBVZkYqAjCQCu6UBL2xgsnZhw=\n"))
	h, err := NewHtpasswd(file)
	assert.Equal(t, err, nil)

	valid := h.Validate("testuser", "asdf")
	assert.Equal(t, valid, true)
}

package main

import (
	"bytes"
	"github.com/stretchr/testify/assert"
	"testing"
)

func TestHtpasswd(t *testing.T) {
	file := bytes.NewBuffer([]byte("testuser:{SHA}PaVBVZkYqAjCQCu6UBL2xgsnZhw=\n"))
	h, err := NewHtpasswd(file)
	assert.Equal(t, err, nil)

	valid := h.Validate("testuser", "asdf")
	assert.Equal(t, valid, true)
}

Use the testify assert lib.


package eventsource

import (
  "fmt"
  "net/http"
)

var header string = `HTTP/1.1 200 OK
Content-Type: text/event-stream
Cache-Control: no-cache
Connection: keep-alive
Access-Control-Allow-Origin: %s
Access-Control-Allow-Credentials: true

retry: 2000

`
func Handler (res http.ResponseWriter, req *http.Request) {
  hj, ok := res.(http.Hijacker)
  if !ok {
    http.Error(res, "webserver doesn't support hijacking", http.StatusInternalServerError)
    return
  }

  conn, _, err := hj.Hijack()
  if err != nil {
    http.Error(res, err.Error(), http.StatusInternalServerError)
    return
  }

  origin := req.Header.Get("origin")
  h := fmt.Sprintf(header, origin)
  _, err = conn.Write([]byte(h))

  if err != nil {
    conn.Close()
  }
}

package eventsource

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

const header string = `HTTP/1.1 200 OK
Content-Type: text/event-stream
Cache-Control: no-cache
Connection: keep-alive
Access-Control-Allow-Credentials: true`

const body string = "\n\nretry: 2000\n"

func Handler (res http.ResponseWriter, req *http.Request) {
  hj, ok := res.(http.Hijacker)
  if !ok {
    http.Error(res, "webserver doesn't support hijacking", http.StatusInternalServerError)
    return
  }

  conn, _, err := hj.Hijack()
  if err != nil {
    http.Error(res, err.Error(), http.StatusInternalServerError)
    return
  }

  _, err = conn.Write(initialResponse(req))

  if err != nil {
    conn.Close()
  }
}

func initialResponse(req *http.Request) []byte {
  var buf bytes.Buffer
  buf.WriteString(header)
  if origin := req.Header.Get("origin"); origin != "" {
    cors:= fmt.Sprintf("Access-Control-Allow-Origin: %s", origin)
    buf.WriteString(cors)
  }
  buf.WriteString(body)
  return buf.Bytes()
}

Write initial response using bytes.Buffer


package helper

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

func WithDummyCredentials(fn func(dir string)) {
	if _, err := ioutil.ReadDir("temp"); err != nil {
		if err := os.Mkdir("temp", 0755); err != nil {
			panic(err)
		}
	}

	dir, err := ioutil.TempDir("temp", "dummy-credentials")

	if err != nil {
		panic(err)
	}

	// Remove all the contents in the dir including *.pem.enc created by ReadOrUpdateCompactTLSAssets()
	// Otherwise we end up with a lot of garbage directories we failed to remove as they aren't empty in
	// config/temp, nodepool/config/temp, test/integration/temp
	defer os.RemoveAll(dir)

	for _, pairName := range []string{"ca", "apiserver", "worker", "admin", "etcd", "etcd-client"} {
		certFile := fmt.Sprintf("%s/%s.pem", dir, pairName)

		if err := ioutil.WriteFile(certFile, []byte("dummycert"), 0644); err != nil {
			panic(err)
		}

		defer os.Remove(certFile)

		keyFile := fmt.Sprintf("%s/%s-key.pem", dir, pairName)

		if err := ioutil.WriteFile(keyFile, []byte("dummykey"), 0644); err != nil {
			panic(err)
		}

		defer os.Remove(keyFile)
	}

	fn(dir)
}

package helper

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

func WithDummyCredentials(fn func(dir string)) {
	dir, err := ioutil.TempDir("", "dummy-credentials")

	if err != nil {
		panic(err)
	}

	// Remove all the contents in the dir including *.pem.enc created by ReadOrUpdateCompactTLSAssets()
	// Otherwise we end up with a lot of garbage directories we failed to remove as they aren't empty in
	// config/temp, nodepool/config/temp, test/integration/temp
	defer os.RemoveAll(dir)

	for _, pairName := range []string{"ca", "apiserver", "worker", "admin", "etcd", "etcd-client"} {
		certFile := fmt.Sprintf("%s/%s.pem", dir, pairName)

		if err := ioutil.WriteFile(certFile, []byte("dummycert"), 0644); err != nil {
			panic(err)
		}

		defer os.Remove(certFile)

		keyFile := fmt.Sprintf("%s/%s-key.pem", dir, pairName)

		if err := ioutil.WriteFile(keyFile, []byte("dummykey"), 0644); err != nil {
			panic(err)
		}

		defer os.Remove(keyFile)
	}

	fn(dir)
}

Stop polluting git workspace with temporary files created while running unit tests


package main

import (
	"os"

	"github.com/remind101/empire/pkg/heroku"
)

var cmdCertAttach = &Command{
	Run:      runCertAttach,
	Usage:    "cert-attach <aws_cert_name>",
	NeedsApp: true,
	Category: "certs",
	Short:    "attach a certificate to an app",
	Long: `
Attaches an SSL certificate to an applications web process. When using the ECS backend, this will attach an IAM server certificate to the applications ELB.

Before running this command, you should upload your SSL certificate and key to IAM using the AWS CLI.

Examples:

    $ aws iam upload-server-certificate --server-certificate-name myServerCertificate --certificate-body file://public_key_cert_file.pem --private-key file://my_private_key.pem --certificate-chain file://my_certificate_chain_file.pem
    $ emp cert-attach myServerCertificate -a myapp
`,
}

func runCertAttach(cmd *Command, args []string) {
	if len(args) == 0 {
		cmd.PrintUsage()
		os.Exit(2)
	}

	cert := args[0]

	_, err := client.AppUpdate(mustApp(), &heroku.AppUpdateOpts{
		Cert: &cert,
	})
	must(err)
}

package main

import (
	"os"

	"github.com/remind101/empire/pkg/heroku"
)

var cmdCertAttach = &Command{
	Run:      runCertAttach,
	Usage:    "cert-attach <aws_cert_arn>",
	NeedsApp: true,
	Category: "certs",
	Short:    "attach a certificate to an app",
	Long: `
Attaches an SSL certificate to an applications web process. When using the ECS backend, this will attach an IAM server certificate to the applications ELB.

Before running this command, you should upload your SSL certificate and key to IAM using the AWS CLI.

Examples:

    $ aws iam upload-server-certificate --server-certificate-name myServerCertificate --certificate-body file://public_key_cert_file.pem --private-key file://my_private_key.pem --certificate-chain file://my_certificate_chain_file.pem
	# ^^ The above command will return the ARN of the certificate, you'll need that for the command below
	# Say it returns the arn arn:aws:iam::123456789012:server-certificate/myServerCertificate, you'd use that like this:
	$ emp cert-attach arn:aws:iam::123456789012:server-certificate/myServerCertificate -a myapp
`,
}

func runCertAttach(cmd *Command, args []string) {
	if len(args) == 0 {
		cmd.PrintUsage()
		os.Exit(2)
	}

	cert := args[0]

	_, err := client.AppUpdate(mustApp(), &heroku.AppUpdateOpts{
		Cert: &cert,
	})
	must(err)
}

Update to indicate the ARN is used


package spider

import "io"

type spinFunc func(*Context) error

type spiderFunc struct {
	method string
	url    string
	body   io.Reader
	fn     spinFunc
}

func (s *spiderFunc) Setup(parent *Context) (*Context, error) {
	return NewHTTPContext(s.method, s.url, s.body)
}
func (s *spiderFunc) Spin(ctx *Context) error { return s.fn(ctx) }

func NewHTTPSpider(method, url string, body io.Reader, fn spinFunc) *spiderFunc {
	return &spiderFunc{
		method: method,
		url:    url,
		body:   body,
		fn:     fn,
	}
}

func NewGETSpider(url string, fn spinFunc) *spiderFunc {
	return NewHTTPSpider("GET", url, nil, fn)
}

func NewPOSTSpider(url string, body io.Reader, fn spinFunc) *spiderFunc {
	return NewHTTPSpider("POST", url, body, fn)
}

func NewPUTSpider(url string, body io.Reader, fn spinFunc) *spiderFunc {
	return NewHTTPSpider("PUT", url, body, fn)
}

func NewDELETESpider(url string, fn spinFunc) *spiderFunc {
	return NewHTTPSpider("DELETE", url, nil, fn)
}

package spider

import "io"

type spinFunc func(*Context) error

type spiderFunc struct {
	method string
	url    string
	body   io.Reader
	fn     spinFunc
}

func (s *spiderFunc) Setup(parent *Context) (*Context, error) {
	return NewHTTPContext(s.method, s.url, s.body)
}
func (s *spiderFunc) Spin(ctx *Context) error { return s.fn(ctx) }

func NewHTTPSpider(method, url string, body io.Reader, fn spinFunc) *spiderFunc {
	return &spiderFunc{
		method: method,
		url:    url,
		body:   body,
		fn:     fn,
	}
}

func Get(url string, fn spinFunc) *spiderFunc {
	return NewHTTPSpider("GET", url, nil, fn)
}

func Post(url string, body io.Reader, fn spinFunc) *spiderFunc {
	return NewHTTPSpider("POST", url, body, fn)
}

func Put(url string, body io.Reader, fn spinFunc) *spiderFunc {
	return NewHTTPSpider("PUT", url, body, fn)
}

func Delete(url string, fn spinFunc) *spiderFunc {
	return NewHTTPSpider("DELETE", url, nil, fn)
}

Rename methods for creating spiders to be more clear and simple


package schema

import "errors"

// AnyOf validates if any of the sub field validators validates.
type AnyOf []FieldValidator

// Compile implements Compiler interface.
func (v *AnyOf) Compile() (err error) {
	for _, sv := range *v {
		if c, ok := sv.(Compiler); ok {
			if err = c.Compile(); err != nil {
				return
			}
		}
	}
	return
}

// Validate ensures that at least one sub-validator validates.
func (v AnyOf) Validate(value interface{}) (interface{}, error) {
	for _, validator := range v {
		var err error
		if value, err = validator.Validate(value); err == nil {
			return value, nil
		}
	}
	// TODO: combine errors.
	return nil, errors.New("invalid")
}

package schema

import "errors"

// AnyOf validates if any of the sub field validators validates.
type AnyOf []FieldValidator

// Compile implements Compiler interface.
func (v *AnyOf) Compile() (err error) {
	for _, sv := range *v {
		if c, ok := sv.(Compiler); ok {
			if err = c.Compile(); err != nil {
				return
			}
		}
	}
	return
}

// Validate ensures that at least one sub-validator validates.
func (v AnyOf) Validate(value interface{}) (interface{}, error) {
	for _, validator := range v {
		if value, err := validator.Validate(value); err == nil {
			return value, nil
		}
	}
	// TODO: combine errors.
	return nil, errors.New("invalid")
}

Fix incorrect behaviour for AnyOf validator


// Simple use of the tuntap package that prints packets received by the interface.
package main

import (
	"fmt"
	"os"

	"code.google.com/p/tuntap"
)

func main() {
	if len(os.Args) != 3 {
		fmt.Println("syntax:", os.Args[0], "tun|tap", "<device name>")
		return
	}

	var typ tuntap.DevKind
	switch os.Args[1] {
	case "tun":
		typ = tuntap.DevTun
	case "tap":
		typ = tuntap.DevTap
	default:
		fmt.Println("Unknown device type", os.Args[1])
		return
	}

	tun, err := tuntap.Open(os.Args[2], typ)
	if err != nil {
		fmt.Println("Error opening tun/tap device:", err)
		return
	}

	fmt.Println("Listening on", tun.Name())
	for {
		pkt, err := tun.ReadPacket()
		if err != nil {
			fmt.Println("Read error:", err)
		} else {
			if pkt.Truncated {
				fmt.Printf("!")
			} else {
				fmt.Printf(" ")
			}
			fmt.Printf("%x %x\n", pkt.Protocol, pkt.Packet)
		}
	}
}

// Simple use of the tuntap package that prints packets received by the interface.
package main

import (
	"fmt"
	"os"

	"github.com/mistsys/tuntap"
)

func main() {
	if len(os.Args) != 3 {
		fmt.Println("syntax:", os.Args[0], "tun|tap", "<device name>")
		return
	}

	var typ tuntap.DevKind
	switch os.Args[1] {
	case "tun":
		typ = tuntap.DevTun
	case "tap":
		typ = tuntap.DevTap
	default:
		fmt.Println("Unknown device type", os.Args[1])
		return
	}

	tun, err := tuntap.Open(os.Args[2], typ)
	if err != nil {
		fmt.Println("Error opening tun/tap device:", err)
		return
	}

	fmt.Println("Listening on", tun.Name())
	for {
		buf := make([]byte, 1536)
		pkt, err := tun.ReadPacket(buf)
		if err != nil {
			fmt.Println("Read error:", err)
		} else {
			if pkt.Truncated {
				fmt.Printf("!")
			} else {
				fmt.Printf(" ")
			}
			fmt.Printf("%x %x\n", pkt.Protocol, pkt.Body)
		}
	}
}

Kill more code.google.com/p/ references, and update example code to new API


package proxy

import (
	"net/url"
	"testing"
)

func TestEmptyBlacklist(t *testing.T) {
	bl := NewEmptyBlacklist()

	hostnames := []string{"http://localhost", "http://google.com"}

	for i := range hostnames {
		url, err := url.Parse(hostnames[i])
		if err != nil {
			t.Fatalf("error with parsing url '%s'", err)
		}

		req := Request{
			URL: *url,
			Method: GET,
		}

		e := bl.IsBlacklisted(req)
		if e != nil {
			t.Fatalf("EmptyBlacklist should never reject anything hostname - %s", req)
		}
	}
}

package proxy

import (
	"net/url"
	"testing"
)

func TestEmptyBlacklist(t *testing.T) {
	bl := NewEmptyBlacklist()

	hostnames := []string{"http://localhost", "http://google.com"}

	for i := range hostnames {
		url, err := url.Parse(hostnames[i])
		if err != nil {
			t.Fatalf("error with parsing url '%s'", err)
		}

		req := Request{
			URL: *url,
			Method: GET,
		}

		e := bl.IsBlacklisted(req)
		if e != nil {
			t.Fatalf("EmptyBlacklist should never reject anything hostname - %s", req.URL.String())
		}
	}
}

Fix 'f' format (from vet)


//
// Copyright (c) 2017 Cavium
//
// SPDX-License-Identifier: Apache-2.0
//

package distro

import (
	"io"
	"net/http"

	"github.com/go-zoo/bone"
)

func replyPing(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/text; charset=utf-8")
	w.WriteHeader(http.StatusOK)
	str := `pong`
	io.WriteString(w, str)
}

func replyNotifyRegistrations(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/text; charset=utf-8")
	w.WriteHeader(http.StatusOK)
	io.WriteString(w, "")
	RefreshRegistrations()
}

// HTTPServer function
func HTTPServer() http.Handler {
	mux := bone.New()

	mux.Get("/api/v1/ping", http.HandlerFunc(replyPing))
	mux.Get("/api/v1/notify/registrations", http.HandlerFunc(replyNotifyRegistrations))

	return mux
}

//
// Copyright (c) 2017 Cavium
//
// SPDX-License-Identifier: Apache-2.0
//

package distro

import (
	"io"
	"net/http"

	"github.com/go-zoo/bone"
)

func replyPing(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/text; charset=utf-8")
	w.WriteHeader(http.StatusOK)
	str := `pong`
	io.WriteString(w, str)
}

func replyNotifyRegistrations(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/text; charset=utf-8")
	w.WriteHeader(http.StatusOK)
	io.WriteString(w, "")
	RefreshRegistrations()
}

// HTTPServer function
func HTTPServer() http.Handler {
	mux := bone.New()

	mux.Get("/api/v1/ping", http.HandlerFunc(replyPing))
	mux.Put("/api/v1/notify/registrations", http.HandlerFunc(replyNotifyRegistrations))

	return mux
}

Update notification update api to PUT


package universal

import (
	"testing"
	"time"

	"github.com/cloudflare/cfssl/config"
)

var expiry = 1 * time.Minute
var validLocalConfig = &config.Config{
	Signing: &config.Signing{
		Profiles: map[string]*config.SigningProfile{
			"valid": {
				Usage:  []string{"digital signature"},
				Expiry: expiry,
			},
		},
		Default: &config.SigningProfile{
			Usage:  []string{"digital signature"},
			Expiry: expiry,
		},
	},
}

func TestNewSigner(t *testing.T) {
	h := map[string]string{
		"key-file":  "../local/testdata/ca_key.pem",
		"cert-file": "../local/testdata/ca.pem",
	}

	r := &Root{
		Config:      h,
		ForceRemote: false,
	}

	_, err := NewSigner(*r, validLocalConfig.Signing)
	if err != nil {
		t.Fatal(err)
	}

}

Add a test for the signer/universal package