// Copyright 2016 The Linux Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package v1

// ImageLayout is the structure in the "oci-layout" file, found in the root
// of an OCI Image-layout directory.
type ImageLayout struct {
	Version string `json:"imageLayoutVersion"`
}

// Copyright 2016 The Linux Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package v1

import "regexp"

// ImageLayout is the structure in the "oci-layout" file, found in the root
// of an OCI Image-layout directory.
type ImageLayout struct {
	Version string `json:"imageLayoutVersion"`
}

var (
	// RefsRegexp matches requirement of image-layout 'refs' charset.
	RefsRegexp = regexp.MustCompile(`^[a-zA-Z0-9-._]+$`)
)

Add regular expression for object names in 'refs'


package observers

import (
	"github.com/sirupsen/logrus"

	"github.com/ivan1993spb/snake-server/world"
)

type LoggerObserver struct{}

func (LoggerObserver) Observe(stop <-chan struct{}, w *world.World, logger logrus.FieldLogger) {
	go func() {
		for event := range w.Events(stop, chanSnakeObserverEventsBuffer) {
			switch event.Type {
			case world.EventTypeError:
				if err, ok := event.Payload.(error); ok {
					logger.WithError(err).Error("world error")
				}
			case world.EventTypeObjectCreate, world.EventTypeObjectDelete, world.EventTypeObjectUpdate, world.EventTypeObjectChecked:
				logger.WithFields(logrus.Fields{
					"payload": event.Payload,
					"type":    event.Type,
				}).Debug("world event")
			}
		}
	}()
}

package observers

import (
	"github.com/sirupsen/logrus"

	"github.com/ivan1993spb/snake-server/world"
)

const chanLoggerObserverEventsBuffer = 32

type LoggerObserver struct{}

func (LoggerObserver) Observe(stop <-chan struct{}, w *world.World, logger logrus.FieldLogger) {
	go func() {
		for event := range w.Events(stop, chanLoggerObserverEventsBuffer) {
			switch event.Type {
			case world.EventTypeError:
				if err, ok := event.Payload.(error); ok {
					logger.WithError(err).Error("world error")
				}
			case world.EventTypeObjectCreate, world.EventTypeObjectDelete, world.EventTypeObjectUpdate, world.EventTypeObjectChecked:
				logger.WithFields(logrus.Fields{
					"payload": event.Payload,
					"type":    event.Type,
				}).Debug("world event")
			}
		}
	}()
}

Fix logger observer: create const chanLoggerObserverEventsBuffer


package stripe

// TerminalConnectionTokenParams is the set of parameters that can be used when creating a terminal connection token.
type TerminalConnectionTokenParams struct {
	Params `form:"*"`

	// This feature has been deprecated and should not be used anymore.
	OperatorAccount *string `form:"operator_account"`
}

// TerminalConnectionToken is the resource representing a Stripe terminal connection token.
type TerminalConnectionToken struct {
	Object string `json:"object"`
	Secret string `json:"secret"`
}

package stripe

// TerminalConnectionTokenParams is the set of parameters that can be used when creating a terminal connection token.
type TerminalConnectionTokenParams struct {
	Params   `form:"*"`
	Location string `form:"location"`

	// This feature has been deprecated and should not be used anymore.
	OperatorAccount *string `form:"operator_account"`
}

// TerminalConnectionToken is the resource representing a Stripe terminal connection token.
type TerminalConnectionToken struct {
	Location string `json:"location"`
	Object   string `json:"object"`
	Secret   string `json:"secret"`
}

Add support for `location` on `ConnectionToken`


// +build windows

package uilive

import (
	"os"
	"unsafe"
)

func getTermSize() (int, int) {
	out, err := os.Open("CONOUT$")
	if err != nil {
		return 0, 0
	}

	var csbi consoleScreenBufferInfo
	ret, _, _ := procGetConsoleScreenBufferInfo.Call(out.Fd(), uintptr(unsafe.Pointer(&csbi)))
	if ret == 0 {
		return 0, 0
	}

	return int(csbi.window.right - csbi.window.left + 1), int(csbi.window.bottom - csbi.window.top + 1)
}

// +build windows

package uilive

import (
	"os"
	"unsafe"
)

func getTermSize() (int, int) {
	out, err := os.Open("CONOUT$")
	if err != nil {
		return 0, 0
	}
	defer out.Close()

	var csbi consoleScreenBufferInfo
	ret, _, _ := procGetConsoleScreenBufferInfo.Call(out.Fd(), uintptr(unsafe.Pointer(&csbi)))
	if ret == 0 {
		return 0, 0
	}

	return int(csbi.window.right - csbi.window.left + 1), int(csbi.window.bottom - csbi.window.top + 1)
}

Add forgotten Close() for opened console output


package grayt

import (
	"image/png"
	"log"
	"os"
)

// Runner is a convenience struct to help run grayt from a main() function.
type Runner struct {
	PxWide, PxHigh int
	BaseName       string
	Quality        float64
}

func NewRunner() *Runner {
	return &Runner{
		PxWide:   640,
		PxHigh:   480,
		BaseName: "default",
		Quality:  10,
	}
}

func (r *Runner) Run(scene Scene) {

	world := newWorld(scene.Entities)

	acc := newAccumulator(r.PxWide, r.PxHigh)
	for i := 0; i < int(r.Quality); i++ {
		log.Print(i)
		TracerImage(scene.Camera, world, acc)
	}
	img := acc.toImage(1.0) // XXX should be configurable
	f, err := os.Create(r.BaseName + ".png")
	r.checkErr(err)
	defer f.Close()
	err = png.Encode(f, img)
	r.checkErr(err)
}

func (r *Runner) checkErr(err error) {
	if err != nil {
		log.Fatal("Fatal: ", err)
	}
}

package grayt

import (
	"image/png"
	"log"
	"os"
)

// Runner is a convenience struct to help run grayt from a main() function.
type Runner struct {
	PxWide, PxHigh int
	BaseName       string
	Quality        float64
}

func NewRunner() *Runner {
	return &Runner{
		PxWide:   640,
		PxHigh:   480,
		BaseName: "default",
		Quality:  10,
	}
}

func (r *Runner) Run(scene Scene) {

	world := newWorld(scene.Entities)

	acc := newAccumulator(r.PxWide, r.PxHigh)
	for i := 0; i < int(r.Quality); i++ {
		log.Print(i)
		TraceImage(scene.Camera, world, acc)
	}
	img := acc.toImage(1.0) // XXX should be configurable
	f, err := os.Create(r.BaseName + ".png")
	r.checkErr(err)
	defer f.Close()
	err = png.Encode(f, img)
	r.checkErr(err)
}

func (r *Runner) checkErr(err error) {
	if err != nil {
		log.Fatal("Fatal: ", err)
	}
}

Fix rename of `TracerImage` to `TraceImage`


package main

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

type proxii struct {
	config *Config
	etcd   *etcdConnector
}

func (p *proxii) handler(w http.ResponseWriter, r *http.Request) {
	host := strings.Split(r.Host, ":")[0]

	uri, err := p.etcd.resolve(host)
	if err != nil {
		log.Println("Error while looking up host: ", err)
		return
	}

	proxy := newReverseProxy(uri)
	proxy.ServeHTTP(w, r)
}

func main() {
	p := newProxii(parseFlags())
	http.HandleFunc("/", p.handler)

	err := http.ListenAndServe(fmt.Sprintf(":%d", p.config.port), nil)
	if err != nil {
		log.Fatal(err)
	}
}

func newProxii(config *Config) *proxii {
	etcd, err := newEtcdConnector(config)
	if err != nil {
		panic(err)
	}

	etcd.init()
	return &proxii{config, etcd}
}

package main

import (
	"fmt"
	"log"
	"net/http"
	"net/http/httputil"
	"strings"
)

type proxii struct {
	config *Config
	etcd   *etcdConnector
}

func (p *proxii) handler(w http.ResponseWriter, r *http.Request) {
	host := strings.Split(r.Host, ":")[0]

	uri, err := p.etcd.resolve(host)
	if err != nil {
		log.Println("Error while looking up host: ", err)
		return
	}

	proxy := newReverseProxy(uri)
	proxy.ServeHTTP(w, r)
}

func main() {
	p := newProxii(parseFlags())
	http.HandleFunc("/", p.handler)

	err := http.ListenAndServe(fmt.Sprintf(":%d", p.config.port), nil)
	if err != nil {
		log.Fatal(err)
	}
}

func newProxii(config *Config) *proxii {
	etcd, err := newEtcdConnector(config)
	if err != nil {
		panic(err)
	}

	etcd.init()
	return &proxii{config, etcd}
}

Revert "Didn't run tests before"


// Graceful restarts and shutdowns are not supported
// on windows, so using usual http.Serve instead.
//
// +build windows

package main

import (
	"net/http"
)

// serve is a wrapper on standard http.Serve method.
func serve(s *http.Server) error {
	return http.Serve(s)
}

// Graceful restarts and shutdowns are not supported
// on windows, so using usual http.Serve instead.
//
// +build windows

package main

import (
	"net/http"
)

// serve is a wrapper on standard http.Serve method.
func serve(s *http.Server) error {
	return s.ListenAndServe()
}

Fix Win's version of serve


package grpcpool

import (
	"sync"

	"google.golang.org/grpc"
)

func newConnectionPool(activeCount int, dialFunc func() (*grpc.ClientConn, error)) (*ConnectionPool, error) {
	pool := &ConnectionPool{mu: sync.Mutex{}}
	for i := 0; i < activeCount; i++ {
		client, error := dialFunc()
		if error != nil {
			pool.Close()
			return nil, error
		}
		pool.put(client)
	}
	return pool, nil
}

package grpcpool

import (
	"sync"

	"google.golang.org/grpc"
)

func NewConnectionPool(activeCount int, dialFunc func() (*grpc.ClientConn, error)) (*ConnectionPool, error) {
	pool := &ConnectionPool{mu: sync.Mutex{}}
	for i := 0; i < activeCount; i++ {
		client, error := dialFunc()
		if error != nil {
			pool.Close()
			return nil, error
		}
		pool.put(client)
	}
	return pool, nil
}

Fix the visibility of initialization


/*
Fluux XMPP is a Go XMPP library, focusing on simplicity, simple automation, and IoT.

The goal is to make simple to write simple adhoc XMPP clients:

 - For automation (like for example monitoring of an XMPP service),
 - For building connected "things" by plugging them on an XMPP server,
 - For writing simple chatbots to control a service or a thing.

Fluux XMPP can be used to build XMPP clients or XMPP components.

Clients

Fluux XMPP can be use to create fully interactive XMPP clients (for
example console-based), but it is more commonly used to build automated
clients (connected devices, automation scripts, chatbots, etc.).

Components

XMPP components can typically be used to extends the features of an XMPP
server, in a portable way, using component protocol over persistent TCP
connections.

Compliance

Fluux XMPP has been primarily tested with ejabberd (https://www.ejabberd.im)
but it should work with any XMPP compliant server.

*/
package xmpp

/*
Fluux XMPP is a Go XMPP library, focusing on simplicity, simple automation, and IoT.

The goal is to make simple to write simple adhoc XMPP clients:

 - For automation (like for example monitoring of an XMPP service),
 - For building connected "things" by plugging them on an XMPP server,
 - For writing simple chatbots to control a service or a thing.

Fluux XMPP can be used to build XMPP clients or XMPP components.

Clients

Fluux XMPP can be use to create fully interactive XMPP clients (for
example console-based), but it is more commonly used to build automated
clients (connected devices, automation scripts, chatbots, etc.).

Components

XMPP components can typically be used to extends the features of an XMPP
server, in a portable way, using component protocol over persistent TCP
connections.

Component protocol is defined in XEP-114 (https://xmpp.org/extensions/xep-0114.html).

Compliance

Fluux XMPP has been primarily tested with ejabberd (https://www.ejabberd.im)
but it should work with any XMPP compliant server.

*/
package xmpp

Add link to component protocol


package mcstore

import (
	"net/http"

	r "github.com/dancannon/gorethink"
	"github.com/emicklei/go-restful"
)

type databaseSessionFilter struct {
	session func() (*r.Session, error)
}

func (f *databaseSessionFilter) Filter(req *restful.Request, resp *restful.Response, chain *restful.FilterChain) {
	if session, err := f.session(); err != nil {
		resp.WriteErrorString(http.StatusInternalServerError, "Unable to connect to database")
	} else {
		req.SetAttribute("session", session)
		chain.ProcessFilter(req, resp)
		session.Close()
	}
}

package mcstore

import (
	"net/http"

	r "github.com/dancannon/gorethink"
	"github.com/emicklei/go-restful"
)

// databaseSessionFilter is a filter than creates new database sessions. It takes a
// function that creates new instances of the session.
type databaseSessionFilter struct {
	session func() (*r.Session, error)
}

// Filter will create a new database session and place it in the session request attribute. When control
// returns to the filter it will close the session.
func (f *databaseSessionFilter) Filter(request *restful.Request, response *restful.Response, chain *restful.FilterChain) {
	if session, err := f.session(); err != nil {
		response.WriteErrorString(http.StatusInternalServerError, "Unable to connect to database")
	} else {
		request.SetAttribute("session", session)
		chain.ProcessFilter(request, response)
		session.Close()
	}
}

Add comments and documentation comments to describe what filter does.


package backend

import (
	"net"
	"net/http"
	"restic/debug"
	"time"
)

// Transport returns a new http.RoundTripper with default settings applied.
func Transport() http.RoundTripper {
	// copied from net/http
	tr := &http.Transport{
		Proxy: http.ProxyFromEnvironment,
		DialContext: (&net.Dialer{
			Timeout:   30 * time.Second,
			KeepAlive: 30 * time.Second,
			DualStack: true,
		}).DialContext,
		MaxIdleConns:          100,
		IdleConnTimeout:       90 * time.Second,
		TLSHandshakeTimeout:   10 * time.Second,
		ExpectContinueTimeout: 1 * time.Second,
	}

	// wrap in the debug round tripper
	return debug.RoundTripper(tr)
}

package backend

import (
	"net"
	"net/http"
	"restic/debug"
	"time"
)

// Transport returns a new http.RoundTripper with default settings applied.
func Transport() http.RoundTripper {
	// copied from net/http
	tr := &http.Transport{
		Proxy: http.ProxyFromEnvironment,
		DialContext: (&net.Dialer{
			Timeout:   30 * time.Second,
			KeepAlive: 30 * time.Second,
			DualStack: true,
		}).DialContext,
		MaxIdleConns:          100,
		MaxIdleConnsPerHost:   100,
		IdleConnTimeout:       90 * time.Second,
		TLSHandshakeTimeout:   10 * time.Second,
		ExpectContinueTimeout: 1 * time.Second,
	}

	// wrap in the debug round tripper
	return debug.RoundTripper(tr)
}

Allow many idle connections per host


package utilities

import (
	"errors"
	"unicode/utf8"
)

// The string encoding options. Currently only support UTF-8. Don't really
// see the merit in supporting anything else at the moment.
const (
	UTF8 = iota
)

// ErrInvalidEncodingType is returned when the encoding type is not one that
// is supported.
var ErrInvalidEncodingType = errors.New("sdbot/utilities: invalid string encoding type")

// EncodeIncoming returns a strings that is encoded in the provided encoding
// type. If the encoding type is invalid then we return the original string,
// but also return an error.
func Encode(s string, encoding int) (string, error) {
	switch encoding {
	case UTF8:
		if !utf8.ValidString(s) {
			v := make([]rune, 0, len(s))
			for i, r := range s {
				if r == utf8.RuneError {
					_, size := utf8.DecodeRuneInString(s[i:])
					if size == 1 {
						continue
					}
				}
				v = append(v, r)
			}
			s = string(v)
		}
		return s, nil
	default:
		return s, ErrInvalidEncodingType
	}
}

package utilities

import (
	"errors"
	"unicode/utf8"
)

// The string encoding options. Currently only support UTF-8. Don't really
// see the merit in supporting anything else at the moment.
const (
	UTF8 = iota
)

// ErrInvalidEncodingType is returned when the encoding type is not one that
// is supported.
var ErrInvalidEncodingType = errors.New("sdbot/utilities: invalid string encoding type")

// Encode returns a strings that is encoded in the provided encoding
// type. If the encoding type is invalid then we return the original string,
// but also return an error.
func Encode(s string, encoding int) (string, error) {
	switch encoding {
	case UTF8:
		if !utf8.ValidString(s) {
			v := make([]rune, 0, len(s))
			for i, r := range s {
				if r == utf8.RuneError {
					_, size := utf8.DecodeRuneInString(s[i:])
					if size == 1 {
						continue
					}
				}
				v = append(v, r)
			}
			s = string(v)
		}
		return s, nil
	default:
		return s, ErrInvalidEncodingType
	}
}

Update Encode documentation for golint.


package celery

import (
	"fmt"
	"time"
)

type Receipt interface {
	Reply(string, interface{})
	Ack()
	Requeue()
	Reject()
}

type Task struct {
	Task string
	Id string
	Args []interface{}
	Kwargs map[string]interface{}
	Retries int
	Eta string
	Expires string
	Receipt Receipt
}

func (t *Task) Ack(result interface{}) {
	if result != nil {
		t.Receipt.Reply(t.Id, result)
	}
	t.Receipt.Ack()
}

func (t *Task) Requeue() {
	go func() {
		time.Sleep(time.Second)
		t.Receipt.Requeue()
	}()
}

func (t *Task) Reject() {
	t.Receipt.Reject()
}

func (t *Task) String() string {
	return fmt.Sprintf("%s[%s]", t.Task, t.Id)
}

package celery

import (
	"bytes"
	"fmt"
	"time"
)

const CELERY_FORMAT = "2006-01-02T15:04:05.999999999"

type celeryTime struct {
	time.Time
}

var null = []byte("null")

func (ct *celeryTime) UnmarshalJSON(data []byte) (err error) {
	if bytes.Equal(data, null) {
		return
	}
	t, err := time.Parse(`"`+CELERY_FORMAT+`"`, string(data))
	if err == nil {
		*ct = celeryTime{t}
	}
	return
}

func (ct *celeryTime) MarshalJSON() (data []byte, err error) {
	if ct.IsZero() {
		return null, nil
	}
	return []byte(ct.Format(`"`+CELERY_FORMAT+`"`)), nil
}

type Receipt interface {
	Reply(string, interface{})
	Ack()
	Requeue()
	Reject()
}

type Task struct {
	Task    string                 `json:"task"`
	Id      string                 `json:"id"`
	Args    []interface{}          `json:"args"`
	Kwargs  map[string]interface{} `json:"kwargs"`
	Retries int                    `json:"retries"`
	Eta     celeryTime             `json:"eta"`
	Expires celeryTime             `json:"expires"`
	Receipt Receipt                `json:"-"`
}

func (t *Task) Ack(result interface{}) {
	if result != nil {
		t.Receipt.Reply(t.Id, result)
	}
	t.Receipt.Ack()
}

func (t *Task) Requeue() {
	go func() {
		time.Sleep(time.Second)
		t.Receipt.Requeue()
	}()
}

func (t *Task) Reject() {
	t.Receipt.Reject()
}

func (t *Task) String() string {
	return fmt.Sprintf("%s[%s]", t.Task, t.Id)
}

Update Task to handle timestamps from Celery


package main

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

var (
	socketName   = "/tmp/dinit.sock"
	socketMaxLen = 512
)

func startCommand(c net.Conn) {
	buf := make([]byte, socketMaxLen)
	n, err := c.Read(buf)

	defer c.Close()
	if err != nil {
		logPrintf("socket: error reading data: %s", err)
		return
	}

	cmdargs := strings.Fields(string(buf[0:n]))
	commands := Args(cmdargs)
	run(commands, true)
}

func socket() {
	l, err := net.Listen("unix", socketName)
	if err != nil {
		logFatalf("socket: listen error: %s", err)
	}

	for {
		fd, err := l.Accept()
		if err != nil {
			logPrintf("socket: accept error: %s", err)
			continue
		}

		go startCommand(fd)
	}
}

func write(cmds []*exec.Cmd) error {
	c, err := net.Dial("unix", socketName)
	if err != nil {
		return err
	}
	str := String(cmds)
	_, err = c.Write([]byte(str))
	if err != nil {
		return err
	}
	return nil
}

package main

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

var (
	socketName   = "/tmp/dinit.sock"
	socketMaxLen = 512
)

func startCommand(c net.Conn) {
	buf := make([]byte, socketMaxLen)
	n, err := c.Read(buf)

	defer c.Close()
	if err != nil {
		logPrintf("socket: error reading data: %s", err)
		return
	}

	cmdargs := strings.Fields(string(buf[0:n]))
	commands := Args(cmdargs)
	run(commands, true)
}

func socket() {
	l, err := net.Listen("unix", socketName)
	if err != nil {
		logFatalf("socket: listen error: %s", err)
	}

	logPrintf("socket: successfully created")

	for {
		fd, err := l.Accept()
		if err != nil {
			logPrintf("socket: accept error: %s", err)
			continue
		}

		go startCommand(fd)
	}
}

func write(cmds []*exec.Cmd) error {
	c, err := net.Dial("unix", socketName)
	if err != nil {
		return err
	}
	str := String(cmds)
	_, err = c.Write([]byte(str))
	if err != nil {
		return err
	}
	return nil
}

Add log line when we successfully created the socket


// Copyright 2017 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.

// +build windows

package gps

import "os/exec"

func killProcess(cmd *exec.Cmd) error {
	// TODO it'd be great if this could be more sophisticated...
	return cmd.Process.Kill()
}

// Copyright 2017 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.

// +build windows

package gps

import "os/exec"

func killProcess(cmd *exec.Cmd, isDone *int32) error {
	// TODO it'd be great if this could be more sophisticated...
	return cmd.Process.Kill()
}

Update sig of killProcess() for windows


package setupclient

import (
	"crypto/tls"
	"github.com/Symantec/Dominator/lib/srpc"
	"os"
)

func setupTls(ignoreMissingCerts bool) error {
	if *certDirectory == "" {
		return nil
	}
	// Load certificates.
	certs, err := srpc.LoadCertificates(*certDirectory)
	if ignoreMissingCerts && os.IsNotExist(err) {
		return nil
	}
	if err != nil {
		return err
	}
	// Setup client.
	clientConfig := new(tls.Config)
	clientConfig.InsecureSkipVerify = true
	clientConfig.MinVersion = tls.VersionTLS12
	clientConfig.Certificates = certs
	srpc.RegisterClientTlsConfig(clientConfig)
	return nil
}

package setupclient

import (
	"crypto/tls"
	"github.com/Symantec/Dominator/lib/srpc"
)

func setupTls(ignoreMissingCerts bool) error {
	if *certDirectory == "" {
		return nil
	}
	// Load certificates.
	certs, err := srpc.LoadCertificates(*certDirectory)
	if err != nil {
		return err
	}
	if certs == nil {
		if ignoreMissingCerts {
			return nil
		}
		return srpc.ErrorMissingCertificate
	}
	// Setup client.
	clientConfig := new(tls.Config)
	clientConfig.InsecureSkipVerify = true
	clientConfig.MinVersion = tls.VersionTLS12
	clientConfig.Certificates = certs
	srpc.RegisterClientTlsConfig(clientConfig)
	return nil
}

Improve missing certificate handling in lib/srpc/setupclient.SetupTls().


package py

/*
#include "Python.h"
*/
import "C"
import (
	"runtime"
)

// Object is a bind of `*C.PyObject`
type Object struct {
	p *C.PyObject
}

// DecRef decrease reference counter of `C.PyObject`
// This function is public for API users and
// it acquires GIL of Python interpreter.
func (o *Object) DecRef() {
	ch := make(chan bool, 1)
	go func() {
		runtime.LockOSThread()
		state := GILState_Ensure()
		defer GILState_Release(state)

		C.Py_DecRef(o.p)
		ch <- true
	}()
	<-ch
}

// decRef decrease reference counter of `C.PyObject`
// This function doesn't acquire GIL.
func (o *Object) decRef() {
	C.Py_DecRef(o.p)
}

package py

/*
#include "Python.h"
void XDecRef(PyObject *o) {
  Py_XDECREF(o);
}
*/
import "C"
import (
	"runtime"
)

// Object is a bind of `*C.PyObject`
type Object struct {
	p *C.PyObject
}

// DecRef decrease reference counter of `C.PyObject`
// This function is public for API users and
// it acquires GIL of Python interpreter.
// User can call whenever target object is null.
func (o *Object) DecRef() {
	ch := make(chan bool, 1)
	go func() {
		runtime.LockOSThread()
		state := GILState_Ensure()
		defer GILState_Release(state)

		C.XDecRef(o.p)
		ch <- true
	}()
	<-ch
}

// decRef decrease reference counter of `C.PyObject`
// This function doesn't acquire GIL.
func (o *Object) decRef() {
	C.Py_DecRef(o.p)
}

Use Py_XDECREF instaed of Py_DecRef in DecRef


package k

import (
	"encoding/json"
	"math/rand"
)

// StringArray is an alias for an array of strings
// which has common operations defined as methods.
type StringArray []string

// Contains returns true if the receiver array contains
// an element equivalent to needle.
func (s StringArray) Contains(needle string) bool {
	for _, elem := range s {
		if elem == needle {
			return true
		}
	}
	return false
}

// String returns the JSON representation of the array.
func (s StringArray) String() string {
	a, _ := json.Marshal(s)
	return string(a)
}

var (
	alphaNum = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"
)

// RandomString generates a random alpha-numeric string
// of length n
func RandomString(n int) string {
	s := make([]byte, 0, n)
	for i := 0; i < n; i++ {
		s = append(s, alphaNum[rand.Int63n(int64(len(alphaNum)))])
	}
	return string(s)
}

package k

import (
	"encoding/json"
	"math/rand"
	"time"
)

// StringArray is an alias for an array of strings
// which has common operations defined as methods.
type StringArray []string

// Contains returns true if the receiver array contains
// an element equivalent to needle.
func (s StringArray) Contains(needle string) bool {
	for _, elem := range s {
		if elem == needle {
			return true
		}
	}
	return false
}

// String returns the JSON representation of the array.
func (s StringArray) String() string {
	a, _ := json.Marshal(s)
	return string(a)
}

var (
	alphaNum = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"
	r        = rand.New(rand.NewSource(time.Now().UnixNano()))
)

// RandomString generates a random alpha-numeric string
// of length n
func RandomString(n int) string {
	s := make([]byte, 0, n)
	for i := 0; i < n; i++ {
		s = append(s, alphaNum[r.Int63n(int64(len(alphaNum)))])
	}
	return string(s)
}

Use own seeded random generator for string generation


package onedrive

import (
	"net/http"
	"time"
)

const (
	version   = "0.1"
	baseURL   = "https://api.onedrive.com/v1.0"
	userAgent = "github.com/ggordan/go-onedrive; version " + version
)

// OneDrive is the entry point for the client. It manages the communication with
// Microsoft OneDrive API
type OneDrive struct {
	Client *http.Client
	// When debug is set to true, the JSON response is formatted for better readability
	Debug    bool
	BaseURL  string
	throttle time.Time
	// Services
	Drives *DriveService
	Items  *ItemService
}

// NewOneDrive returns a new OneDrive client to enable you to communicate with
// the API
func NewOneDrive(c *http.Client, debug bool) *OneDrive {
	drive := OneDrive{
		Client:   c,
		BaseURL:  baseURL,
		Debug:    debug,
		throttle: time.Now(),
	}
	drive.Drives = &DriveService{&drive}
	drive.Items = &ItemService{&drive}
	return &drive
}

func (od *OneDrive) throttleRequest(time time.Time) {
	od.throttle = time
}

package onedrive

import (
	"net/http"
	"time"
)

const (
	version   = "0.1"
	baseURL   = "https://api.onedrive.com/v1.0"
	userAgent = "github.com/ggordan/go-onedrive; version " + version
)

// OneDrive is the entry point for the client. It manages the communication with
// Microsoft OneDrive API
type OneDrive struct {
	Client *http.Client
	// When debug is set to true, the JSON response is formatted for better readability
	Debug   bool
	BaseURL string
	// Services
	Drives *DriveService
	Items  *ItemService
	// Private
	throttle time.Time
}

// NewOneDrive returns a new OneDrive client to enable you to communicate with
// the API
func NewOneDrive(c *http.Client, debug bool) *OneDrive {
	drive := OneDrive{
		Client:   c,
		BaseURL:  baseURL,
		Debug:    debug,
		throttle: time.Now(),
	}
	drive.Drives = &DriveService{&drive}
	drive.Items = &ItemService{&drive}
	return &drive
}

func (od *OneDrive) throttleRequest(time time.Time) {
	od.throttle = time
}

Move private variable to bottom of struct


package cfgfile

import (
	"flag"
	"fmt"
	"io/ioutil"

	"gopkg.in/yaml.v2"
)

// Command line flags
var configfile *string
var testConfig *bool

func CmdLineFlags(flags *flag.FlagSet, name string) {
	configfile = flags.String("c", fmt.Sprintf("/etc/%s/%s.yml", name, name), "Configuration file")
	testConfig = flags.Bool("test", false, "Test configuration and exit.")
}

// Reads config from yaml file into the given interface structure.
// In case the second param path is not set
func Read(out interface{}, path string) error {

	if path == "" {
		path = *configfile
	}

	filecontent, err := ioutil.ReadFile(path)

	if err != nil {
		return fmt.Errorf("Fail to read %s: %v. Exiting.", path, err)
	}
	if err = yaml.Unmarshal(filecontent, out); err != nil {
		return fmt.Errorf("YAML config parsing failed on %s: %v. Exiting.", path, err)
	}

	return nil
}

func IsTestConfig() bool {
	return *testConfig
}

package cfgfile

import (
	"flag"
	"fmt"
	"io/ioutil"

	"gopkg.in/yaml.v2"
)

// Command line flags
var configfile *string
var testConfig *bool

func CmdLineFlags(flags *flag.FlagSet, name string) {
	configfile = flags.String("c", fmt.Sprintf("/etc/%s/%s.yml", name, name), "Configuration file")
	testConfig = flags.Bool("test", false, "Test configuration and exit.")
}

// Reads config from yaml file into the given interface structure.
// In case path is not set this method reads from the default configuration file for the beat.
func Read(out interface{}, path string) error {

	if path == "" {
		path = *configfile
	}

	filecontent, err := ioutil.ReadFile(path)

	if err != nil {
		return fmt.Errorf("Failed to read %s: %v. Exiting.", path, err)
	}
	if err = yaml.Unmarshal(filecontent, out); err != nil {
		return fmt.Errorf("YAML config parsing failed on %s: %v. Exiting.", path, err)
	}

	return nil
}

func IsTestConfig() bool {
	return *testConfig
}

Fix documentation and log message issue


// Copyright 2016 Mender Software AS
//
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//
//        http://www.apache.org/licenses/LICENSE-2.0
//
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.

package mocks

import (
	"github.com/mendersoftware/deployments/integration"
	"github.com/stretchr/testify/mock"
)

// APIClient is an autogenerated mock type for the APIClient type
type APIClient struct {
	mock.Mock
}

// GetDeviceInventory provides a mock function with given fields: device
func (_m *APIClient) GetDeviceInventory(device integration.DeviceID) (*integration.Device, error) {
	ret := _m.Called(device)

	var r0 *integration.Device
	if rf, ok := ret.Get(0).(func(integration.DeviceID) *integration.Device); ok {
		r0 = rf(device)
	} else {
		if ret.Get(0) != nil {
			r0 = ret.Get(0).(*integration.Device)
		}
	}

	var r1 error
	if rf, ok := ret.Get(1).(func(integration.DeviceID) error); ok {
		r1 = rf(device)
	} else {
		r1 = ret.Error(1)
	}

	return r0, r1
}

Add missing license statement in mock file.


package s3gof3r

import (
	"net"
	"net/http"
	"time"
)

type deadlineConn struct {
	Timeout time.Duration
	net.Conn
}

func (c *deadlineConn) Read(b []byte) (n int, err error) {
	if err = c.Conn.SetDeadline(time.Now().Add(c.Timeout)); err != nil {
		return
	}
	return c.Conn.Read(b)
}

func (c *deadlineConn) Write(b []byte) (n int, err error) {
	if err = c.Conn.SetDeadline(time.Now().Add(c.Timeout)); err != nil {
		return
	}
	return c.Conn.Write(b)
}

func ClientWithTimeout(timeout time.Duration) *http.Client {
	transport := &http.Transport{
		Proxy: http.ProxyFromEnvironment,
		Dial: func(netw, addr string) (net.Conn, error) {
			c, err := net.DialTimeout(netw, addr, timeout)
			if err != nil {
				return nil, err
			}
			return &deadlineConn{timeout, c}, nil
		},
		ResponseHeaderTimeout: timeout,
	}
	return &http.Client{Transport: transport}
}

package s3gof3r

import (
	"net"
	"net/http"
	"time"
)

type deadlineConn struct {
	Timeout time.Duration
	net.Conn
}

func (c *deadlineConn) Read(b []byte) (n int, err error) {
	if err = c.Conn.SetDeadline(time.Now().Add(c.Timeout)); err != nil {
		return
	}
	return c.Conn.Read(b)
}

func (c *deadlineConn) Write(b []byte) (n int, err error) {
	if err = c.Conn.SetDeadline(time.Now().Add(c.Timeout)); err != nil {
		return
	}
	return c.Conn.Write(b)
}

func ClientWithTimeout(timeout time.Duration) *http.Client {
	transport := &http.Transport{
		Proxy: http.ProxyFromEnvironment,
		Dial: func(netw, addr string) (net.Conn, error) {
			c, err := net.DialTimeout(netw, addr, timeout)
			if err != nil {
				return nil, err
			}
			if tc, ok := c.(*net.TCPConn); ok {
				tc.SetKeepAlive(true)
				tc.SetKeepAlivePeriod(timeout)
			}
			return &deadlineConn{timeout, c}, nil
		},
		ResponseHeaderTimeout: timeout,
		MaxIdleConnsPerHost:   20,
	}
	return &http.Client{Transport: transport}
}

Enable KeepAlives and set timeout in http transport.


package syntax

import "github.com/ksco/slua/scanner"

type SyntaxTree interface{}

type (
	Chunk struct {
		Block SyntaxTree
	}

	Block struct {
		Stmts []SyntaxTree
	}

	DoStatement struct {
		Block SyntaxTree
	}

	WhileStatement struct {
		Exp   SyntaxTree
		Block SyntaxTree
	}

	IfStatement struct {
		Exp         SyntaxTree
		TrueBranch  SyntaxTree
		FalseBranch SyntaxTree
	}

	ElseifStatement struct {
		Exp         SyntaxTree
		TrueBranch  SyntaxTree
		FalseBranch SyntaxTree
	}

	ElseStatement struct {
		Block SyntaxTree
	}

	LocalNameListStatement struct {
		NameList SyntaxTree
		ExpList  SyntaxTree
	}

	AssignmentStatement struct {
		VarList SyntaxTree
		ExpList SyntaxTree
	}

	VarList struct {
		VarList []SyntaxTree
	}

	Terminator struct {
		Tok *scanner.Token
	}

	BinaryExpression struct {
		Left    SyntaxTree
		Right   SyntaxTree
		OpToken *scanner.Token
	}

	UnaryExpression struct {
		Exp     SyntaxTree
		OpToken *scanner.Token
	}

	NameList struct {
		Names []*scanner.Token
	}

	ExpressionList struct {
		ExpList []SyntaxTree
	}
)

package syntax

import "github.com/ksco/slua/scanner"

type SyntaxTree interface{}

type (
	Chunk struct {
		Block SyntaxTree
	}

	Block struct {
		Stmts []SyntaxTree
	}

	DoStatement struct {
		Block SyntaxTree
	}

	WhileStatement struct {
		Exp   SyntaxTree
		Block SyntaxTree
	}

	IfStatement struct {
		Exp         SyntaxTree
		TrueBranch  SyntaxTree
		FalseBranch SyntaxTree
	}

	ElseifStatement struct {
		Exp         SyntaxTree
		TrueBranch  SyntaxTree
		FalseBranch SyntaxTree
	}

	ElseStatement struct {
		Block SyntaxTree
	}

	LocalNameListStatement struct {
		NameList SyntaxTree
		ExpList  SyntaxTree
	}

	AssignmentStatement struct {
		VarList SyntaxTree
		ExpList SyntaxTree
	}

	VarList struct {
		VarList []SyntaxTree
	}

	Terminator struct {
		Token *scanner.Token
	}

	BinaryExpression struct {
		Left    SyntaxTree
		Right   SyntaxTree
		OpToken *scanner.Token
	}

	UnaryExpression struct {
		Exp     SyntaxTree
		OpToken *scanner.Token
	}

	NameList struct {
		Names []*scanner.Token
	}

	ExpressionList struct {
		ExpList []SyntaxTree
	}
)

Change Tok to Token for readability.


package db2

import (
	"github.com/Aptomi/aptomi/pkg/slinga/util"
	"github.com/stretchr/testify/assert"
	"testing"
)

func TestKey(t *testing.T) {
	correctKey := Key("72b062c1-7fcf-11e7-ab09-acde48001122$42")

	assert.Equal(t, util.UID("72b062c1-7fcf-11e7-ab09-acde48001122"), correctKey.GetUID(), "Correct UID expected")
	assert.Equal(t, Generation(42), correctKey.GetGeneration(), "Correct Generation expected")

	noGenerationKey := Key("72b062c1-7fcf-11e7-ab09-acde48001122")

	assert.Panics(t, func() { noGenerationKey.GetUID() }, "Panic expected if key is incorrect")
	assert.Panics(t, func() { noGenerationKey.GetGeneration() }, "Panic expected if key is incorrect")
}

package db2

import (
	"github.com/Aptomi/aptomi/pkg/slinga/util"
	"github.com/stretchr/testify/assert"
	"testing"
)

func TestKey(t *testing.T) {
	correctKey := Key("72b062c1-7fcf-11e7-ab09-acde48001122$42")

	assert.Equal(t, util.UID("72b062c1-7fcf-11e7-ab09-acde48001122"), correctKey.GetUID(), "Correct UID expected")
	assert.Equal(t, Generation(42), correctKey.GetGeneration(), "Correct Generation expected")

	noGenerationKey := Key("72b062c1-7fcf-11e7-ab09-acde48001122")

	assert.Panics(t, func() { noGenerationKey.GetUID() }, "Panic expected if key is incorrect")
	assert.Panics(t, func() { noGenerationKey.GetGeneration() }, "Panic expected if key is incorrect")

	invalidGenerationKey := Key("72b062c1-7fcf-11e7-ab09-acde48001122$bad")

	assert.Equal(t, util.UID("72b062c1-7fcf-11e7-ab09-acde48001122"), correctKey.GetUID(), "Correct UID expected")
	assert.Panics(t, func() { invalidGenerationKey.GetGeneration() }, "Panic expected if key is incorrect")
}

Add test for incorrect generation


package target

import (
	"io"
	"path/filepath"

	"github.com/spf13/hugo/helpers"
	"github.com/spf13/hugo/hugofs"
)

type Publisher interface {
	Publish(string, io.Reader) error
}

type Translator interface {
	Translate(string) (string, error)
}

// TODO(bep) consider other ways to solve this.
type OptionalTranslator interface {
	TranslateRelative(string) (string, error)
}

type Output interface {
	Publisher
	Translator
}

type Filesystem struct {
	PublishDir string
}

func (fs *Filesystem) Publish(path string, r io.Reader) (err error) {
	translated, err := fs.Translate(path)
	if err != nil {
		return
	}

	return helpers.WriteToDisk(translated, r, hugofs.DestinationFS)
}

func (fs *Filesystem) Translate(src string) (dest string, err error) {
	return filepath.Join(fs.PublishDir, src), nil
}

func (fs *Filesystem) extension(ext string) string {
	return ext
}

func filename(f string) string {
	ext := filepath.Ext(f)
	if ext == "" {
		return f
	}

	return f[:len(f)-len(ext)]
}

package target

import (
	"io"
	"path/filepath"

	"github.com/spf13/hugo/helpers"
	"github.com/spf13/hugo/hugofs"
)

type Publisher interface {
	Publish(string, io.Reader) error
}

type Translator interface {
	Translate(string) (string, error)
}

// TODO(bep) consider other ways to solve this.
type OptionalTranslator interface {
	TranslateRelative(string) (string, error)
}

type Output interface {
	Publisher
	Translator
}

type Filesystem struct {
	PublishDir string
}

func (fs *Filesystem) Publish(path string, r io.Reader) (err error) {
	translated, err := fs.Translate(path)
	if err != nil {
		return
	}

	return helpers.WriteToDisk(translated, r, hugofs.DestinationFS)
}

func (fs *Filesystem) Translate(src string) (dest string, err error) {
	return filepath.Join(fs.PublishDir, filepath.FromSlash(src)), nil
}

func (fs *Filesystem) extension(ext string) string {
	return ext
}

func filename(f string) string {
	ext := filepath.Ext(f)
	if ext == "" {
		return f
	}

	return f[:len(f)-len(ext)]
}

Make sure target destination has the right path separator.


package command

import (
	"github.com/akaspin/bar/client/git"
	"github.com/akaspin/bar/client/model"
	"github.com/akaspin/bar/client/transport"
	"github.com/spf13/cobra"
	"github.com/tamtam-im/logx"
)

type GitInstallCmd struct {
	*Environment
	*CommonOptions

	// Installable logging level
	Log string
}

func (c *GitInstallCmd) Init(cc *cobra.Command) {
	cc.Use = "install"
	cc.Short = "install bar support into git repo"

	cc.Flags().StringVarP(&c.Log, "log", "", logx.DEBUG,
		"installable logging level")

	cc.Flags()
}

func (c *GitInstallCmd) Run(args ...string) (err error) {
	var mod *model.Model
	if mod, err = model.New(c.WD, true, c.ChunkSize, c.PoolSize); err != nil {
		return
	}
	defer mod.Close()

	trans := transport.NewTransport(mod, "", c.Endpoint, c.PoolSize)
	defer trans.Close()

	info, err := trans.ServerInfo()
	if err != nil {
		return
	}

	config := git.NewConfig(info, mod.Git)
	err = config.Install(c.Log)

	return
}

package command

import (
	"github.com/akaspin/bar/client/git"
	"github.com/akaspin/bar/client/model"
	"github.com/akaspin/bar/client/transport"
	"github.com/spf13/cobra"
	"github.com/tamtam-im/logx"
)

type GitInstallCmd struct {
	*Environment
	*CommonOptions

	// Installable logging level
	Log string
}

func (c *GitInstallCmd) Init(cc *cobra.Command) {
	cc.Use = "install"
	cc.Short = "install bar support into git repo"

	cc.Flags().StringVarP(&c.Log, "log", "", logx.INFO,
		"installable logging level")

	cc.Flags()
}

func (c *GitInstallCmd) Run(args ...string) (err error) {
	var mod *model.Model
	if mod, err = model.New(c.WD, true, c.ChunkSize, c.PoolSize); err != nil {
		return
	}
	defer mod.Close()

	trans := transport.NewTransport(mod, "", c.Endpoint, c.PoolSize)
	defer trans.Close()

	info, err := trans.ServerInfo()
	if err != nil {
		return
	}

	config := git.NewConfig(info, mod.Git)
	err = config.Install(c.Log)

	return
}

Use INFO logging level in `bar git install`


// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package fuse

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

var fEnableDebug = flag.Bool(
	"fuse.debug",
	false,
	"Write FUSE debugging messages to stderr.")

var gLogger *log.Logger
var gLoggerOnce sync.Once

func initLogger() {
	if !flag.Parsed() {
		panic("initLogger called before flags available.")
	}

	var writer io.Writer = ioutil.Discard
	if *fEnableDebug {
		writer = os.Stderr
	}

	const flags = log.Ldate | log.Ltime | log.Lmicroseconds
	gLogger = log.New(writer, "fuse: ", flags)
}

func getLogger() *log.Logger {
	gLoggerOnce.Do(initLogger)
	return gLogger
}

// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package fuse

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

var fEnableDebug = flag.Bool(
	"fuse.debug",
	false,
	"Write FUSE debugging messages to stderr.")

var gLogger *log.Logger
var gLoggerOnce sync.Once

func initLogger() {
	if !flag.Parsed() {
		panic("initLogger called before flags available.")
	}

	var writer io.Writer = ioutil.Discard
	if *fEnableDebug {
		writer = os.Stderr
	}

	const flags = log.Ldate | log.Ltime | log.Lmicroseconds
	gLogger = log.New(writer, "", flags)
}

func getLogger() *log.Logger {
	gLoggerOnce.Do(initLogger)
	return gLogger
}

Remove prefix to save some characters.


/*
   Copyright (c) 2014, Percona LLC and/or its affiliates. All rights reserved.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>
*/

package agent

const (
	DEFAULT_API_HOSTNAME = "cloud-api-v2.percona.com"
)

type Config struct {
	AgentUuid   string
	ApiHostname string
	ApiKey      string
	Links       map[string]string `json:",omitempty"`
}

/*
   Copyright (c) 2014, Percona LLC and/or its affiliates. All rights reserved.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>
*/

package agent

const (
	DEFAULT_API_HOSTNAME = "v2-cloud-api.percona.com"
)

type Config struct {
	AgentUuid   string
	ApiHostname string
	ApiKey      string
	Links       map[string]string `json:",omitempty"`
}

Set real default API hostname.


// +build !windows

package runtime

import (
	"io/ioutil"
	"syscall"

	"github.com/codahale/metrics"
)

func getFDLimit() (uint64, error) {
	var rlimit syscall.Rlimit
	if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlimit); err != nil {
		return 0, err
	}
	return uint64(rlimit.Cur), nil
}

func getFDUsage() (uint64, error) {
	fds, err := ioutil.ReadDir("/proc/self/fd")
	if err != nil {
		return 0, err
	}
	return uint64(len(fds)), nil
}

func init() {
	metrics.Gauge("FileDescriptors.Max").SetFunc(func() int64 {
		v, err := getFDLimit()
		if err != nil {
			return 0
		}
		return int64(v)
	})

	metrics.Gauge("FileDescriptors.Used").SetFunc(func() int64 {
		v, err := getFDUsage()
		if err != nil {
			return 0
		}
		return int64(v)
	})
}

// +build !windows

package runtime

import (
	"io/ioutil"
	"syscall"

	"github.com/codahale/metrics"
)

func getFDLimit() (uint64, error) {
	var rlimit syscall.Rlimit
	if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlimit); err != nil {
		return 0, err
	}
	// rlimit.Cur's type is platform-dependent, so here we widen it as far as Go
	// will allow by converting it to a uint64.
	return uint64(rlimit.Cur), nil
}

func getFDUsage() (uint64, error) {
	fds, err := ioutil.ReadDir("/proc/self/fd")
	if err != nil {
		return 0, err
	}
	return uint64(len(fds)), nil
}

func init() {
	metrics.Gauge("FileDescriptors.Max").SetFunc(func() int64 {
		v, err := getFDLimit()
		if err != nil {
			return 0
		}
		return int64(v)
	})

	metrics.Gauge("FileDescriptors.Used").SetFunc(func() int64 {
		v, err := getFDUsage()
		if err != nil {
			return 0
		}
		return int64(v)
	})
}

Document the fix for FreeBSD.


package logging

import (
	"github.com/sirupsen/logrus"
	"github.com/spf13/viper"
)

// ConfigureLogging sets up open match logrus instance using the logging section of the matchmaker_config.json
//  - log line format (text[default] or json)
//  - min log level to include (debug, info [default], warn, error, fatal, panic)
//  - include source file and line number for every event (false [default], true)
func ConfigureLogging(cfg *viper.Viper) {
	switch cfg.GetString("logging.format") {
	case "json":
		logrus.SetFormatter(&logrus.JSONFormatter{})
	case "text":
	default:
		logrus.SetFormatter(&logrus.TextFormatter{})
	}

	switch cfg.GetString("logging.level") {
	case "debug":
		logrus.SetLevel(logrus.DebugLevel)
		logrus.Warn("Debug logging level configured. Not recommended for production!")
	case "warn":
		logrus.SetLevel(logrus.WarnLevel)
	case "error":
		logrus.SetLevel(logrus.ErrorLevel)
	case "fatal":
		logrus.SetLevel(logrus.FatalLevel)
	case "panic":
		logrus.SetLevel(logrus.PanicLevel)
	case "info":
	default:
		logrus.SetLevel(logrus.InfoLevel)
	}

	switch cfg.GetBool("logging.source") {
	case true:
		logrus.SetReportCaller(true)
	}

}

package logging

import (
	stackdriver "github.com/TV4/logrus-stackdriver-formatter"
	"github.com/sirupsen/logrus"
	"github.com/spf13/viper"
)

// ConfigureLogging sets up open match logrus instance using the logging section of the matchmaker_config.json
//  - log line format (text[default] or json)
//  - min log level to include (debug, info [default], warn, error, fatal, panic)
//  - include source file and line number for every event (false [default], true)
func ConfigureLogging(cfg *viper.Viper) {
	switch cfg.GetString("logging.format") {
	case "stackdriver":
		logrus.SetFormatter(stackdriver.NewFormatter())
	case "json":
		logrus.SetFormatter(&logrus.JSONFormatter{})
	case "text":
	default:
		logrus.SetFormatter(&logrus.TextFormatter{})
	}

	switch cfg.GetString("logging.level") {
	case "debug":
		logrus.SetLevel(logrus.DebugLevel)
		logrus.Warn("Debug logging level configured. Not recommended for production!")
	case "warn":
		logrus.SetLevel(logrus.WarnLevel)
	case "error":
		logrus.SetLevel(logrus.ErrorLevel)
	case "fatal":
		logrus.SetLevel(logrus.FatalLevel)
	case "panic":
		logrus.SetLevel(logrus.PanicLevel)
	case "info":
	default:
		logrus.SetLevel(logrus.InfoLevel)
	}

	switch cfg.GetBool("logging.source") {
	case true:
		logrus.SetReportCaller(true)
	}

}

Add stackdriver format support via TV4/logrus-stackdriver-formatter. Simply set format in config to stackdriver


package postgresql

import (
	"os"
	"testing"

	"github.com/hashicorp/terraform/helper/schema"
	"github.com/hashicorp/terraform/terraform"
)

var testAccProviders map[string]terraform.ResourceProvider
var testAccProvider *schema.Provider

func init() {
	testAccProvider = Provider().(*schema.Provider)
	testAccProviders = map[string]terraform.ResourceProvider{
		"postgresql": testAccProvider,
	}
}

func TestProvider(t *testing.T) {
	if err := Provider().(*schema.Provider).InternalValidate(); err != nil {
		t.Fatalf("err: %s", err)
	}
}

func TestProvider_impl(t *testing.T) {
	var _ terraform.ResourceProvider = Provider()
}

func testAccPreCheck(t *testing.T) {
	var host string
	if host = os.Getenv("PGHOST"); host == "" {
		t.Fatal("PGHOST must be set for acceptance tests")
	}
	if v := os.Getenv("PGUSER"); v == "" {
		t.Fatal("PGUSER must be set for acceptance tests")
	}
	if v := os.Getenv("PGPASSWORD"); v == "" && host != "localhost" {
		t.Fatal("PGPASSWORD must be set for acceptance tests if PGHOST is not localhost")
	}
}

package postgresql

import (
	"os"
	"testing"

	"github.com/hashicorp/terraform/helper/schema"
	"github.com/hashicorp/terraform/terraform"
)

var testAccProviders map[string]terraform.ResourceProvider
var testAccProvider *schema.Provider

func init() {
	testAccProvider = Provider().(*schema.Provider)
	testAccProviders = map[string]terraform.ResourceProvider{
		"postgresql": testAccProvider,
	}
}

func TestProvider(t *testing.T) {
	if err := Provider().(*schema.Provider).InternalValidate(); err != nil {
		t.Fatalf("err: %s", err)
	}
}

func TestProvider_impl(t *testing.T) {
	var _ terraform.ResourceProvider = Provider()
}

func testAccPreCheck(t *testing.T) {
	var host string
	if host = os.Getenv("PGHOST"); host == "" {
		t.Fatal("PGHOST must be set for acceptance tests")
	}
	if v := os.Getenv("PGUSER"); v == "" {
		t.Fatal("PGUSER must be set for acceptance tests")
	}
}

Remove the PGPASSWORD requirement for tests.


package main

import (
	"fmt"
	"io/ioutil"

	"./lexer"
	"./parser"
	"./print"
)

func main() {
	dat, err := ioutil.ReadFile("sample.dtodo")
	if err != nil {
		panic(err)
	}

	todo(string(dat))
}

func todo(src string) {
	tokenChan := make(chan lexer.Token)
	nodeChan := make(chan parser.Todo)
	go parser.Run(nodeChan, tokenChan)
	go lexer.Run(tokenChan, &src)

	root := <-nodeChan
	fmt.Printf("%s\n", print.Stringify(root))
	return
}

package main

import (
	"fmt"
	"io/ioutil"

	"./lexer"
	"./parser"
	"./print"
)

func main() {
	dat, err := ioutil.ReadFile("sample.dtodo")
	if err != nil {
		panic(err)
	}

	todo(string(dat))
}

func todo(src string) {
	tokenChan := make(chan lexer.Token)
	nodeChan := make(chan parser.Todo)
	go lexer.Run(tokenChan, &src)
	go parser.Run(nodeChan, tokenChan)
	root := <-nodeChan
	fmt.Printf("%s\n", print.Stringify(root))
	return
}

Change calling sequence of go routines.


package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/julienschmidt/httprouter"
)

var data map[string]string = map[string]string{}

func Get(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	key := params.ByName("key")
	fmt.Fprint(w, data[key])
}

func Put(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	key := params.ByName("key")
	value := r.FormValue("data")
	data[key] = value
}

func main() {
	router := httprouter.New()

	router.GET("/:key", Get)
	router.POST("/:key", Put)

	log.Println("Running server on port 3000")
	log.Fatal(http.ListenAndServe(":3000", router))
}

package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/julienschmidt/httprouter"
)

var data map[string]string = map[string]string{}

func Get(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	key := params.ByName("key")
	value, ok := data[key]

	if !ok {
		w.Header().Set("Status", "404")
		fmt.Fprint(w, "Not found")
		return
	} else {
		fmt.Fprint(w, value)
	}
}

func Put(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
	key := params.ByName("key")
	value := r.FormValue("data")
	data[key] = value
}

func main() {
	router := httprouter.New()

	router.GET("/:key", Get)
	router.POST("/:key", Put)

	log.Println("Running server on port 3000")
	log.Fatal(http.ListenAndServe(":3000", router))
}

Handle 404 in get requests


package hostsfile

import (
	"testing"
)

func TestNewHost(t *testing.T) {
	host := NewHost("IP", "hostname")

	id := createHostId("hostname")
	stub := Host{"IP", "hostname", id}

	if *host != stub {
		t.Error("Ordering of Host fields do not match.")
	}
}

Add a test for NewHost.


/*
 *
 * k6 - a next-generation load testing tool
 * Copyright (C) 2016 Load Impact
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package js2

import (
	"github.com/dop251/goja"
	"github.com/spf13/afero"
)

// Provides APIs for use in the init context.
type InitContext struct {
	// Filesystem to load files and scripts from.
	Fs  afero.Fs
	Pwd string

	// Cache of loaded modules.
	Modules map[string]*goja.Program
}

func (i *InitContext) Require(mod string) goja.Value {
	return goja.Undefined()
}

/*
 *
 * k6 - a next-generation load testing tool
 * Copyright (C) 2016 Load Impact
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package js2

import (
	"github.com/dop251/goja"
	"github.com/spf13/afero"
)

// Provides APIs for use in the init context.
type InitContext struct {
	// Filesystem to load files and scripts from.
	Fs  afero.Fs `js:"-"`
	Pwd string   `js:"-"`

	// Cache of loaded modules.
	Modules map[string]*goja.Program `js:"-"`
}

func (i *InitContext) Require(mod string) goja.Value {
	return goja.Undefined()
}

Hide stuff user code shouldn't access


package main

import (
	"time"
)

type (
	InstanceTime     int64
	InstanceDuration int64
)

const (
	RealGameTick         = time.Second / 20
	RealPeriodicalTick   = time.Second / 2
	RealRegenerationTick = time.Second * 5

	GameTick         = InstanceDuration(RealGameTick / RealGameTick)
	PeriodicalTick   = InstanceDuration(RealPeriodicalTick / RealGameTick)
	RegenerationTick = InstanceDuration(RealRegenerationTick / RealGameTick)

	Second = InstanceDuration(time.Second / RealGameTick)
)

type InstanceClock interface {
	Now() InstanceTime
	Add(InstanceDuration) InstanceTime
	Before(InstanceTime) bool
	After(InstanceTime) bool
}

package main

import (
	"time"
)

type (
	InstanceTime     int64
	InstanceDuration int64
)

const (
	RealGameTick         = time.Second / 20
	RealPeriodicalTick   = time.Second / 2
	RealRegenerationTick = time.Second * 5

	GameTick         = InstanceDuration(RealGameTick / RealGameTick)
	PeriodicalTick   = InstanceDuration(RealPeriodicalTick / RealGameTick)
	RegenerationTick = InstanceDuration(RealRegenerationTick / RealGameTick)

	Second = InstanceDuration(time.Second / RealGameTick)
)

type InstanceClock interface {
	Now() InstanceTime
	Add(InstanceDuration) InstanceTime
	After(InstanceTime) bool
	Before(InstanceTime) bool
}

// Now returns the InstanceTime
func (t InstanceTime) Now() InstanceTime {
	return t
}

// Add returns the InstanceTime t+d
func (t InstanceTime) Add(d InstanceDuration) InstanceTime {
	return t + InstanceTime(d)
}

// Before returns true if t is after u
func (t InstanceTime) After(u InstanceTime) bool {
	return t > u
}

// After returns true if t is before u
func (t InstanceTime) Before(u InstanceTime) bool {
	return t < u
}

Add implementation of the InstanceClock interface


// +build !go1.5

package bom

import "bufio"

func discardBytes(buf *bufio.Reader, n int) {
	// cannot use the buf.Discard method as it was introduced in Go 1.5
	if n <= 0 {
		return
	}
	b := make([]byte, n)
	buf.Read(b)
}

// +build !go1.5

package bom

import "bufio"

func discardBytes(buf *bufio.Reader, n int) {
	// cannot use the buf.Discard method as it was introduced in Go 1.5
	for i := 0; i < n; i++ {
		_, err := buf.ReadByte()
		if err != nil {
			return
		}
	}
}

Remove allocation in discard 1.4


package main

import (
	"strings"

	"github.com/sheenobu/go-xco"
)

func main() {
	opts := xco.Options{
		Name:         "sms.example.com",
		SharedSecret: "secret shared with the XMPP server",
		Address:      "127.0.0.1:5347",
	}
	c, err := xco.NewComponent(opts)
	if err != nil {
		panic(err)
	}

	// Uppercase Echo Component
	c.MessageHandler = xco.BodyResponseHandler(func(msg *xco.Message) (string, error) {
		return strings.ToUpper(msg.Body), nil
	})

	c.Run()
}

package main

import (
	"fmt"
	"os"
	"strings"

	"github.com/BurntSushi/toml"
	"github.com/sheenobu/go-xco"
)

type StaticConfig struct {
	Xmpp StaticConfigXmpp `toml:"xmpp"`
}

type StaticConfigXmpp struct {
	Host   string `toml:"host"`
	Name   string `toml:"name"`
	Port   int    `toml:"port"`
	Secret string `toml:"secret"`
}

func main() {
	config := new(StaticConfig)
	_, err := toml.DecodeFile(os.Args[1], &config)
	if err != nil {
		panic(err)
	}

	opts := xco.Options{
		Name:         config.Xmpp.Name,
		SharedSecret: config.Xmpp.Secret,
		Address:      fmt.Sprintf("%s:%d", config.Xmpp.Host, config.Xmpp.Port),
	}
	c, err := xco.NewComponent(opts)
	if err != nil {
		panic(err)
	}

	// Uppercase Echo Component
	c.MessageHandler = xco.BodyResponseHandler(func(msg *xco.Message) (string, error) {
		return strings.ToUpper(msg.Body), nil
	})

	c.Run()
}

Move configuration to a file


package bindmount

import (
	"fmt"
	"os"
	"path/filepath"
	"syscall"

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

type Mounter struct{}

func (m *Mounter) IdempotentlyMount(source, target string) error {
	err := os.MkdirAll(filepath.Dir(target), 0600)
	if err != nil {
		return fmt.Errorf("os.MkdirAll failed: %s", err)
	}

	fd, err := os.Create(target)
	if err != nil {
		return fmt.Errorf("os.Create failed: %s", err)
	}
	defer fd.Close()

	err = unix.Mount(source, target, "none", unix.MS_BIND, "")
	if err != nil {
		return fmt.Errorf("mount failed: %s", err)
	}

	return nil
}

func (m *Mounter) RemoveMount(target string) error {
	err := unix.Unmount(target, unix.MNT_DETACH)
	if err != nil && err != syscall.ENOENT {
		return fmt.Errorf("unmount failed: %s", err)
	}

	err = os.RemoveAll(target)
	if err != nil {
		return fmt.Errorf("removeall failed: %s", err) // not tested
	}

	return nil
}

package bindmount

import (
	"fmt"
	"os"
	"path/filepath"
	"syscall"

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

type Mounter struct{}

func (m *Mounter) IdempotentlyMount(source, target string) error {
	err := os.MkdirAll(filepath.Dir(target), 0700)
	if err != nil {
		return fmt.Errorf("os.MkdirAll failed: %s", err)
	}

	fd, err := os.Create(target)
	if err != nil {
		return fmt.Errorf("os.Create failed: %s", err)
	}
	defer fd.Close()

	err = unix.Mount(source, target, "none", unix.MS_BIND, "")
	if err != nil {
		return fmt.Errorf("mount failed: %s", err)
	}

	return nil
}

func (m *Mounter) RemoveMount(target string) error {
	err := unix.Unmount(target, unix.MNT_DETACH)
	if err != nil && err != syscall.ENOENT {
		return fmt.Errorf("unmount failed: %s", err)
	}

	err = os.RemoveAll(target)
	if err != nil {
		return fmt.Errorf("removeall failed: %s", err) // not tested
	}

	return nil
}

Make owner of directory be able to access its content


package deploy

import (
	"fmt"
	"strings"

	"github.com/yuuki/droot/mounter"
	"github.com/yuuki/droot/osutil"
)

var RsyncDefaultOpts = []string{"-av", "--delete", "--exclude=/proc/", "--exclude=/sys/"}

func Rsync(from, to string, arg ...string) error {
	from = from + "/"
	// append "/" when not terminated by "/"
	if strings.LastIndex(to, "/") != len(to)-1 {
		to = to + "/"
	}

	rsyncArgs := []string{}
	rsyncArgs = append(rsyncArgs, RsyncDefaultOpts...)

	// Exclude bind-mounted directory by droot run
	mnt := mounter.NewMounter(to)
	mounts, err := mnt.GetMountsRoot()
	if err != nil {
		return err
	}
	for _, m := range mounts {
		mp := strings.TrimPrefix(m.Mountpoint, to)
		rsyncArgs = append(rsyncArgs, fmt.Sprintf("--exclude=/%s", mp))
	}

	rsyncArgs = append(rsyncArgs, from, to)

	if err := osutil.RunCmd("rsync", rsyncArgs...); err != nil {
		return err
	}

	return nil
}

package deploy

import (
	"fmt"
	"strings"

	"github.com/yuuki/droot/mounter"
	"github.com/yuuki/droot/osutil"
)

var RsyncDefaultOpts = []string{"-av", "--delete"}

func Rsync(from, to string, arg ...string) error {
	from = from + "/"
	// append "/" when not terminated by "/"
	if strings.LastIndex(to, "/") != len(to)-1 {
		to = to + "/"
	}

	rsyncArgs := []string{}
	rsyncArgs = append(rsyncArgs, RsyncDefaultOpts...)

	// Exclude bind-mounted directory by droot run
	mnt := mounter.NewMounter(to)
	mounts, err := mnt.GetMountsRoot()
	if err != nil {
		return err
	}
	for _, m := range mounts {
		mp := strings.TrimPrefix(m.Mountpoint, to)
		rsyncArgs = append(rsyncArgs, fmt.Sprintf("--exclude=/%s", mp))
	}

	rsyncArgs = append(rsyncArgs, from, to)

	if err := osutil.RunCmd("rsync", rsyncArgs...); err != nil {
		return err
	}

	return nil
}

Fix loss of /proc and /sys when symlink deploy


package logbuf

import (
	"fmt"
	"io"
)

func (lb *LogBuffer) write(p []byte) (n int, err error) {
	lb.rwMutex.Lock()
	defer lb.rwMutex.Unlock()
	val := make([]byte, len(p))
	copy(val, p)
	lb.buffer.Value = val
	lb.buffer = lb.buffer.Next()
	return len(p), nil
}

func (lb *LogBuffer) dump(writer io.Writer, prefix, postfix string) error {
	lb.rwMutex.RLock()
	defer lb.rwMutex.RUnlock()
	lb.buffer.Do(func(p interface{}) {
		if p != nil {
			writer.Write([]byte(prefix))
			writer.Write(p.([]byte))
			writer.Write([]byte(postfix))
		}
	})
	return nil
}

func (lb *LogBuffer) writeHtml(writer io.Writer) {
	fmt.Fprintln(writer, "Logs:
")
	fmt.Fprintln(writer, "
")
	lb.Dump(writer, "", "")
	fmt.Fprintln(writer, "
")
}

package logbuf

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

var (
	alsoLogToStderr = flag.Bool("alsoLogToStderr", false,
		"If true, also write logs to stderr")
)

func (lb *LogBuffer) write(p []byte) (n int, err error) {
	if *alsoLogToStderr {
		os.Stderr.Write(p)
	}
	lb.rwMutex.Lock()
	defer lb.rwMutex.Unlock()
	val := make([]byte, len(p))
	copy(val, p)
	lb.buffer.Value = val
	lb.buffer = lb.buffer.Next()
	return len(p), nil
}

func (lb *LogBuffer) dump(writer io.Writer, prefix, postfix string) error {
	lb.rwMutex.RLock()
	defer lb.rwMutex.RUnlock()
	lb.buffer.Do(func(p interface{}) {
		if p != nil {
			writer.Write([]byte(prefix))
			writer.Write(p.([]byte))
			writer.Write([]byte(postfix))
		}
	})
	return nil
}

func (lb *LogBuffer) writeHtml(writer io.Writer) {
	fmt.Fprintln(writer, "Logs:
")
	fmt.Fprintln(writer, "
")
	lb.Dump(writer, "", "")
	fmt.Fprintln(writer, "
")
}

Add -alsoLogToStderr flag to logbug package.


package cmd

import (
	"fmt"
	"os"

	"github.com/ligato/vpp-agent/cmd/agentctl2/restapi"
	"github.com/spf13/cobra"
)

// RootCmd represents the base command when called without any subcommands.
var cliConfig = &cobra.Command{
	Use:     "cli",
	Aliases: []string{"c"},
	Short:   "CLI command for VPP",
	Long: `
	Run CLI command for VPP
`,
	Args: cobra.MinimumNArgs(1),
	Run:  cliFunction,
}

func init() {
	RootCmd.AddCommand(cliConfig)
}

func cliFunction(cmd *cobra.Command, args []string) {
	var cli string

	for _, str := range args {
		cli = cli + " " + str
	}

	msg := fmt.Sprintf("{\"vppclicommand\":\"%v\"}", cli)

	fmt.Fprintf(os.Stdout, "%s\n", msg)

	resp := restapi.PostMsg(globalFlags.Endpoints, "/vpp/command", msg)

	//TODO: Need format
	fmt.Fprintf(os.Stdout, "%s\n", resp)
}

package cmd

import (
	"fmt"
	"os"

	"github.com/ligato/vpp-agent/cmd/agentctl2/restapi"
	"github.com/spf13/cobra"
)

// RootCmd represents the base command when called without any subcommands.
var cliConfig = &cobra.Command{
	Use:   "vppcli",
	Short: "CLI command for VPP",
	Long: `
	Run CLI command for VPP
`,
	Args: cobra.MinimumNArgs(1),
	Run:  cliFunction,
}

func init() {
	RootCmd.AddCommand(cliConfig)
}

func cliFunction(cmd *cobra.Command, args []string) {
	var cli string

	for _, str := range args {
		cli = cli + " " + str
	}

	msg := fmt.Sprintf("{\"vppclicommand\":\"%v\"}", cli)

	fmt.Fprintf(os.Stdout, "%s\n", msg)

	resp := restapi.PostMsg(globalFlags.Endpoints, "/vpp/command", msg)

	//TODO: Need format
	fmt.Fprintf(os.Stdout, "%s\n", resp)
}

Rename CLI command to vppcli


/*
Package "dense" provides an implementation of "Matrix" which stores elements in a slide.
*/
package dense

import (
	"errors"
	"fmt"

	"github.com/mitsuse/matrix-go"
)

type matrixImpl struct {
	rows     int
	columns  int
	elements []float64
}

func New(rows, columns int) func(elements ...float64) (matrix.Matrix, error) {
	rowsShouldBePositiveNumber(rows)
	columnShouldBePositiveNumber(rows)

	constructor := func(elements ...float64) (matrix.Matrix, error) {
		size := rows * columns

		if len(elements) != size {
			template := "The number of %q should equal to %q * %q."
			message := fmt.Sprintf(template, "elements", "rows", "columns")

			return nil, errors.New(message)
		}

		m := &matrixImpl{
			rows:     rows,
			columns:  columns,
			elements: make([]float64, size),
		}
		copy(m.elements, elements)

		return m, nil
	}

	return constructor
}

func (m *matrixImpl) Shape() (rows, columns int) {
	return m.rows, m.columns
}

func (m *matrixImpl) Rows() (rows int) {
	return m.rows
}

func (m *matrixImpl) Columns() (columns int) {
	return m.columns
}

/*
Package "dense" provides an implementation of "Matrix" which stores elements in a slide.
*/
package dense

import (
	"errors"
	"fmt"

	"github.com/mitsuse/matrix-go"
)

type matrixImpl struct {
	rows     int
	columns  int
	elements []float64
}

func New(rows, columns int) func(elements ...float64) (matrix.Matrix, error) {
	rowsShouldBePositiveNumber(rows)
	columnShouldBePositiveNumber(rows)

	constructor := func(elements ...float64) (matrix.Matrix, error) {
		size := rows * columns

		if len(elements) != size {
			template := "The number of %q should equal to %q * %q."
			message := fmt.Sprintf(template, "elements", "rows", "columns")

			return nil, errors.New(message)
		}

		m := &matrixImpl{
			rows:     rows,
			columns:  columns,
			elements: make([]float64, size),
		}
		copy(m.elements, elements)

		return m, nil
	}

	return constructor
}

func (m *matrixImpl) Shape() (rows, columns int) {
	return m.Rows(), m.Columns()
}

func (m *matrixImpl) Rows() (rows int) {
	return m.rows
}

func (m *matrixImpl) Columns() (columns int) {
	return m.columns
}

Implement "(*matrixImpl).Shape()" with "(*matrixImple).Rows()" and "(*matrixImple).Columns()".


package librarian

import (
	"github.com/mlafeldt/chef-runner/exec"
	"github.com/mlafeldt/chef-runner/util"
)

func Command(path string) []string {
	var cmd []string
	if util.FileExist("Gemfile") {
		cmd = []string{"bundle", "exec"}
	}
	cmd = append(cmd, "librarian-chef", "install", "--path", path)
	return cmd
}

func InstallCookbooks(path string) error {
	return exec.RunCommand(Command(path))
}

package librarian

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

	"github.com/mlafeldt/chef-runner/exec"
	"github.com/mlafeldt/chef-runner/util"
)

func Command(dst string) []string {
	var cmd []string
	if util.FileExist("Gemfile") {
		cmd = []string{"bundle", "exec"}
	}
	cmd = append(cmd, "librarian-chef", "install", "--path", dst)
	return cmd
}

func removeTempFiles(dst string) error {
	tmpDirs, err := filepath.Glob(path.Join(dst, "*", "tmp", "librarian"))
	if err != nil {
		return err
	}
	for _, dir := range tmpDirs {
		if err := os.RemoveAll(dir); err != nil {
			return err
		}
	}
	return nil
}

func InstallCookbooks(dst string) error {
	if err := exec.RunCommand(Command(dst)); err != nil {
		return err
	}
	return removeTempFiles(dst)
}

Remove temporary Librarian-Chef files from destination


package regressiontests

import (
	"reflect"
	"testing"
)

type Empty struct{}

func TestStructcheck(t *testing.T) {
	t.Parallel()
	source := `package test

type test struct {
	unused int
}
`
	pkgName := reflect.TypeOf(Empty{}).PkgPath()
	expected := Issues{
		{Linter: "structcheck", Severity: "warning", Path: "test.go", Line: 4, Col: 2, Message: "unused struct field " + pkgName + "/.test.unused"},
	}
	ExpectIssues(t, "structcheck", source, expected)
}

package regressiontests

import "testing"

func TestStructcheck(t *testing.T) {
	t.Parallel()
	source := `package test

type test struct {
	unused int
}
`
	expected := Issues{
		{Linter: "structcheck", Severity: "warning", Path: "test.go", Line: 4, Col: 2, Message: "unused struct field github.com/alecthomas/gometalinter/regressiontests/.test.unused"},
	}
	ExpectIssues(t, "structcheck", source, expected)
}

Revert "make tests compatible with forks"


package route

import (
    "log"
    "sync/atomic"
)

// HTML Wrapper struct so we can store the html string in an atomic.Value
type HTML struct {
    value string
}

// html stores the no route html string
var store atomic.Value

func init() {
    store.Store(HTML{""})
}

// GetHTML returns the HTML for not found routes. The function is safe to be
// called from multiple goroutines.
func GetHTML() string {
    return store.Load().(HTML).value
}

// SetHTML sets the current noroute html. The function is safe to be called from
// multiple goroutines.
func SetHTML(h string) {
    html := HTML{h}
    store.Store(html)

    if h == "" {
        log.Print("[INFO] Unset noroute HTML")
    } else {
        log.Printf("[INFO] Set noroute HTML (%d bytes)", len(h))
    }
}

package route

import (
    "log"
    "sync/atomic"
)

var store atomic.Value // string

func init() {
    store.Store("")
}

// GetHTML returns the HTML for not found routes.
func GetHTML() string {
    return store.Load().(string)
}

// SetHTML sets the current noroute html.
func SetHTML(h string) {
    // html := HTML{h}
    store.Store(h)

    if h == "" {
        log.Print("[INFO] Unset noroute HTML")
    } else {
        log.Printf("[INFO] Set noroute HTML (%d bytes)", len(h))
    }
}

Remove superfluous constructs of store


package main

var (
	Version   = "0.0.0"
	CommitSHA = "none"
)

package main

var (
	Version   = "0.0.0"
	CommitSHA = "unknown"
)

Use 'unknown' for unknown commit revisions. We need to find a nicer way to handle this on Windows, where we don't have 'make'


/*
Copyright 2016 The Kubernetes Authors All rights reserved.

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

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

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

package clouddns

import (
	"k8s.io/kubernetes/federation/pkg/dnsprovider"
	"k8s.io/kubernetes/federation/pkg/dnsprovider/providers/google/clouddns/internal/interfaces"
)

type Zones struct {
	impl       interfaces.ManagedZonesService
	interface_ *Interface
}

func (zones Zones) List() ([]dnsprovider.Zone, error) {
	response, err := zones.impl.List(zones.project()).Do()
	if err != nil {
		return []dnsprovider.Zone{}, nil
	}
	managedZones := response.ManagedZones()
	zoneList := make([]dnsprovider.Zone, len(managedZones))
	for i, zone := range managedZones {
		zoneList[i] = &Zone{zone, &zones}
	}
	return zoneList, nil
}

func (zones Zones) project() string {
	return zones.interface_.project()
}

/*
Copyright 2016 The Kubernetes Authors All rights reserved.

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

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

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

package clouddns

import (
	"k8s.io/kubernetes/federation/pkg/dnsprovider"
	"k8s.io/kubernetes/federation/pkg/dnsprovider/providers/google/clouddns/internal/interfaces"
)

type Zones struct {
	impl       interfaces.ManagedZonesService
	interface_ *Interface
}

func (zones Zones) List() ([]dnsprovider.Zone, error) {
	response, err := zones.impl.List(zones.project()).Do()
	if err != nil {
		return nil, err
	}
	managedZones := response.ManagedZones()
	zoneList := make([]dnsprovider.Zone, len(managedZones))
	for i, zone := range managedZones {
		zoneList[i] = &Zone{zone, &zones}
	}
	return zoneList, nil
}

func (zones Zones) project() string {
	return zones.interface_.project()
}

Return error not empty list when dnsprovider returns an error.


// Package errors contains middlewares for converting HTTP response codes to GoLang errors.
package errors // import "go.delic.rs/cliware-middlewares/errors"

import (
	"fmt"
	"net/http"

	"io/ioutil"

	c "go.delic.rs/cliware"
)

type HTTPError struct {
	Name       string
	StatusCode int
	Body       string
}

func (e *HTTPError) Error() string {
	return fmt.Sprintf("HTTP error: %s (%d)", e.Name, e.StatusCode)
}

func createError(resp *http.Response) error {
	if resp.StatusCode < 400 {
		return nil
	}
	defer resp.Body.Close()
	rawData, _ := ioutil.ReadAll(resp.Body)
	return &HTTPError{
		Name:       resp.Status,
		StatusCode: resp.StatusCode,
		Body:       string(rawData),
	}
}

// Errors convert HTTP status codes that represent errors to HTTPError.
func Errors() c.Middleware {
	return c.ResponseProcessor(func(resp *http.Response, err error) error {
		// if we already got error just send it down the chain
		if err != nil {
			return err
		}
		return createError(resp)
	})
}

// Package errors contains middlewares for converting HTTP response codes to GoLang errors.
package errors // import "go.delic.rs/cliware-middlewares/errors"

import (
	"fmt"
	"net/http"

	"io/ioutil"

	c "go.delic.rs/cliware"
)

type HTTPError struct {
	Name       string
	StatusCode int
	Body       []byte
}

func (e *HTTPError) Error() string {
	return fmt.Sprintf("HTTP error: %s (%d)", e.Name, e.StatusCode)
}

func createError(resp *http.Response) error {
	if resp.StatusCode < 400 {
		return nil
	}
	defer resp.Body.Close()
	rawData, _ := ioutil.ReadAll(resp.Body)
	return &HTTPError{
		Name:       resp.Status,
		StatusCode: resp.StatusCode,
		Body:       rawData,
	}
}

// Errors convert HTTP status codes that represent errors to HTTPError.
func Errors() c.Middleware {
	return c.ResponseProcessor(func(resp *http.Response, err error) error {
		// if we already got error just send it down the chain
		if err != nil {
			return err
		}
		return createError(resp)
	})
}

Return []byte from http error instead of string.


package azure

import (
	"io/ioutil"
	"log"

	"github.com/hashicorp/go-retryablehttp"
)

type Client struct {
	logger *log.Logger

	BaseURL        string
	subscriptionID string

	tokenRequester *tokenRequester
	httpClient     *retryablehttp.Client
}

func NewClient(creds *AzureResourceManagerCredentials) (*Client, error) {
	defaultLogger := log.New(ioutil.Discard, "", 0)

	httpClient := retryablehttp.NewClient()
	httpClient.Logger = defaultLogger

	tr := newTokenRequester(httpClient, creds.ClientID, creds.ClientSecret, creds.TenantID)

	return &Client{
		BaseURL:        "https://management.azure.com",
		subscriptionID: creds.SubscriptionID,
		httpClient:     httpClient,
		tokenRequester: tr,
		logger:         defaultLogger,
	}, nil
}

func (c *Client) SetLogger(newLogger *log.Logger) {
	c.logger = newLogger
	c.httpClient.Logger = newLogger
}

func (c *Client) NewRequest() *Request {
	return &Request{
		client: c,
	}
}

func (c *Client) NewRequestForURI(resourceURI string) *Request {
	return &Request{
		URI:    &resourceURI,
		client: c,
	}
}

package azure

import (
	"io/ioutil"
	"log"

	"github.com/hashicorp/go-retryablehttp"
	"net/http"
)

type Client struct {
	logger *log.Logger

	BaseURL        string
	subscriptionID string

	tokenRequester *tokenRequester
	httpClient     *retryablehttp.Client
}

func NewClient(creds *AzureResourceManagerCredentials) (*Client, error) {
	defaultLogger := log.New(ioutil.Discard, "", 0)

	httpClient := retryablehttp.NewClient()
	httpClient.Logger = defaultLogger

	tr := newTokenRequester(httpClient, creds.ClientID, creds.ClientSecret, creds.TenantID)

	return &Client{
		BaseURL:        "https://management.azure.com",
		subscriptionID: creds.SubscriptionID,
		httpClient:     httpClient,
		tokenRequester: tr,
		logger:         defaultLogger,
	}, nil
}

func (c *Client) SetRequestLoggingHook(hook func (*log.Logger, *http.Request, int)) {
	c.httpClient.RequestLogHook = hook
}

func (c *Client) SetLogger(newLogger *log.Logger) {
	c.logger = newLogger
	c.httpClient.Logger = newLogger
}

func (c *Client) NewRequest() *Request {
	return &Request{
		client: c,
	}
}

func (c *Client) NewRequestForURI(resourceURI string) *Request {
	return &Request{
		URI:    &resourceURI,
		client: c,
	}
}

Add support for hooking retryablehttp request logs


package main

import "fmt"

func commandCleanupChecks(done chan bool, printOnly bool) {
	if !printOnly {
		dbOpen()
	}

	stmts := []string{
		`DELETE FROM check_instance_configurations;`,
		`DELETE FROM check_instance_configuration_dependencies;`,
		`DELETE FROM check_instances;`,
		`DELETE FROM checks;`,
		`DELETE FROM configuration_thresholds;`,
		`DELETE FROM constraints_custom_property;`,
		`DELETE FROM constraints_native_property;`,
		`DELETE FROM constraints_oncall_property;`,
		`DELETE FROM constraints_service_attribute;`,
		`DELETE FROM constraints_service_property;`,
		`DELETE FROM constraints_system_property;`,
		`DELETE FROM check_configurations;`,
	}

	for _, stmt := range stmts {
		if printOnly {
			fmt.Println(stmt)
			continue
		}
		db.Exec(stmt)
	}
}

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

package main

import "fmt"

func commandCleanupChecks(done chan bool, printOnly bool) {
	if !printOnly {
		dbOpen()
	}

	stmts := []string{
		`DELETE FROM check_instance_configurations;`,
		`DELETE FROM check_instance_configuration_dependencies;`,
		`DELETE FROM check_instances;`,
		`DELETE FROM checks;`,
		`DELETE FROM configuration_thresholds;`,
		`DELETE FROM constraints_custom_property;`,
		`DELETE FROM constraints_native_property;`,
		`DELETE FROM constraints_oncall_property;`,
		`DELETE FROM constraints_service_attribute;`,
		`DELETE FROM constraints_service_property;`,
		`DELETE FROM constraints_system_property;`,
		`DELETE FROM check_configurations;`,
	}

	for _, stmt := range stmts {
		if printOnly {
			fmt.Println(stmt)
			continue
		}
		db.Exec(stmt)
	}

	done <- true
}

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

Make cleanup checks command terminate


/*
Package gitio shortens github urls using the git.io service.
*/
package gitio

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
)

// Shorten a long github url.
func Shorten(longurl string) (string, error) {
	resp, err := http.PostForm(`https://git.io/create`, url.Values{`url`: {longurl}})
	if err != nil {
		return "", err
	}
	if resp.StatusCode != 200 {
		return "", fmt.Errorf("Expected 200 response, got: %d", resp.StatusCode)
	}

	text, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return "", fmt.Errorf("Error reading response from gitio: %v", err)
	}

	return fmt.Sprintf(`https://git.io/%s`, text), nil
}

/*
Package gitio shortens github urls using the git.io service.
*/
package gitio

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"time"
)

// Shorten a long github url.
func Shorten(longurl string) (string, error) {
	client := new(http.Client)
	client.Timeout = 5 * time.Second

	resp, err := client.PostForm(`https://git.io/create`, url.Values{`url`: {longurl}})
	if err != nil {
		return "", err
	}
	if resp.StatusCode != 200 {
		return "", fmt.Errorf("Expected 200 response, got: %d", resp.StatusCode)
	}

	text, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return "", fmt.Errorf("Error reading response from gitio: %v", err)
	}

	return fmt.Sprintf(`https://git.io/%s`, text), nil
}

Add 5s timeout to http call


package main

import (
	"log"
	"net"
	"os"
	"os/signal"
	"sync"
	"syscall"
)

var listenerChan = make(chan net.Listener)

var activeConnections sync.WaitGroup

func init() {
	go watchForSIGTERM()
}

func watchForSIGTERM() {
	var listeners []net.Listener
	sigChan := make(chan os.Signal, 1)
	signal.Notify(sigChan, syscall.SIGTERM)

	for {
		select {
		case l := <-listenerChan:
			listeners = append(listeners, l)

		case <-sigChan:
			log.Println("Received SIGTERM")
			for _, ln := range listeners {
				ln.Close()
			}
			if *pidfile != "" {
				os.Remove(*pidfile)
			}
			activeConnections.Wait()
			os.Exit(0)
		}
	}
}

package main

import (
	"log"
	"net"
	"os"
	"os/signal"
	"sync"
	"syscall"
	"time"
)

var listenerChan = make(chan net.Listener)

var activeConnections sync.WaitGroup

func init() {
	go watchForSIGTERM()
}

func watchForSIGTERM() {
	var listeners []net.Listener
	sigChan := make(chan os.Signal, 1)
	signal.Notify(sigChan, syscall.SIGTERM)

	for {
		select {
		case l := <-listenerChan:
			listeners = append(listeners, l)

		case <-sigChan:
			log.Println("Received SIGTERM")
			for _, ln := range listeners {
				ln.Close()
			}
			if *pidfile != "" {
				os.Remove(*pidfile)
			}
			go func() {
				// Stop after 24 hours even if the connections aren't closed.
				time.Sleep(24 * time.Hour)
				os.Exit(0)
			}()
			activeConnections.Wait()
			os.Exit(0)
		}
	}
}

Put a time limit on smart shutdown.


package ec2

import (
	"errors"

	goaws "github.com/aws/aws-sdk-go/aws"
	awsec2 "github.com/aws/aws-sdk-go/service/ec2"
)

type AvailabilityZoneRetriever struct {
	ec2ClientProvider ec2ClientProvider
}

func NewAvailabilityZoneRetriever(ec2ClientProvider ec2ClientProvider) AvailabilityZoneRetriever {
	return AvailabilityZoneRetriever{
		ec2ClientProvider: ec2ClientProvider,
	}
}

func (r AvailabilityZoneRetriever) Retrieve(region string) ([]string, error) {
	output, err := r.ec2ClientProvider.GetEC2Client().DescribeAvailabilityZones(&awsec2.DescribeAvailabilityZonesInput{
		Filters: []*awsec2.Filter{{
			Name:   goaws.String("region-name"),
			Values: []*string{goaws.String(region)},
		}},
	})
	if err != nil {
		return []string{}, err
	}

	azList := []string{}
	for _, az := range output.AvailabilityZones {
		if az == nil {
			return []string{}, errors.New("aws returned nil availability zone")
		}
		if az.ZoneName == nil {
			return []string{}, errors.New("aws returned availability zone with nil zone name")
		}

		if *az.ZoneName != "us-east-1d" {
			azList = append(azList, *az.ZoneName)
		}
	}

	return azList, nil
}

package ec2

import (
	"errors"

	goaws "github.com/aws/aws-sdk-go/aws"
	awsec2 "github.com/aws/aws-sdk-go/service/ec2"
)

type AvailabilityZoneRetriever struct {
	ec2ClientProvider ec2ClientProvider
}

func NewAvailabilityZoneRetriever(ec2ClientProvider ec2ClientProvider) AvailabilityZoneRetriever {
	return AvailabilityZoneRetriever{
		ec2ClientProvider: ec2ClientProvider,
	}
}

func (r AvailabilityZoneRetriever) Retrieve(region string) ([]string, error) {
	output, err := r.ec2ClientProvider.GetEC2Client().DescribeAvailabilityZones(&awsec2.DescribeAvailabilityZonesInput{
		Filters: []*awsec2.Filter{{
			Name:   goaws.String("region-name"),
			Values: []*string{goaws.String(region)},
		}},
	})
	if err != nil {
		return []string{}, err
	}

	azList := []string{}
	for _, az := range output.AvailabilityZones {
		if az == nil {
			return []string{}, errors.New("aws returned nil availability zone")
		}
		if az.ZoneName == nil {
			return []string{}, errors.New("aws returned availability zone with nil zone name")
		}

		azList = append(azList, *az.ZoneName)
	}

	return azList, nil
}

Revert "Fix bbl up for AWS"


package main

import (
	"fmt"
	"unicode/utf8"
)

func main() {
	text := "abcdefg"
	fmt.Println(reverseString1(text))
	fmt.Println(reverseString2(text))
}

func reverseString1(s string) string {
	reversed := make([]rune, utf8.RuneCountInString(s))
	i := len(reversed) - 1

	for _, c := range s {
		reversed[i] = c
		i--
	}

	return string(reversed)
}

func reverseString2(s string) string {
	reversed := []rune(s)
	for i, j := 0, len(reversed)-1; i < j; i, j = i+1, j-1 {
		reversed[i], reversed[j] = reversed[j], reversed[i]
	}

	return string(reversed)
}

package main

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

func main() {
	text := "abcdefg 456789"
	fmt.Println(text)
	fmt.Println(reverseString1(text))
	fmt.Println(reverseString2(text))
	fmt.Println(reverseWord(text))
}

func reverseString1(s string) string {
	reversed := make([]rune, utf8.RuneCountInString(s))
	i := len(reversed) - 1

	for _, c := range s {
		reversed[i] = c
		i--
	}

	return string(reversed)
}

func reverseString2(s string) string {
	reversed := []rune(s)
	for i, j := 0, len(reversed)-1; i < j; i, j = i+1, j-1 {
		reversed[i], reversed[j] = reversed[j], reversed[i]
	}

	return string(reversed)
}

func reverseWord(s string) string {
	words := strings.Split(s, " ")
	reversed := make([]string, len(words))
	copy(reversed, words)

	for i, j := 0, len(reversed)-1; i < j; i, j = i+1, j-1 {
		reversed[i], reversed[j] = reversed[j], reversed[i]
	}

	return strings.Join(reversed, " ")
}

Add a solution of Chapter 1.7 to reverse words in a string


// Copyright (C) 2016 AppNeta, Inc. All rights reserved.

package tv_test

import (
	"time"

	"github.com/appneta/go-appneta/v1/tv"
	"golang.org/x/net/context"
)

func slowFunc(ctx context.Context) {
	defer tv.BeginProfile(ctx, "slowFunc").End()
	// ... do something else ...
	time.Sleep(1 * time.Second)
}

func Example() {
	ctx := tv.NewContext(context.Background(), tv.NewTrace("myLayer"))
	slowFunc(ctx)
	tv.EndTrace(ctx)
}

// Copyright (C) 2016 AppNeta, Inc. All rights reserved.

package tv_test

import (
	"database/sql"
	"fmt"
	"time"

	"github.com/appneta/go-appneta/v1/tv"
	"golang.org/x/net/context"
)

// measure a DB query
func dbQuery(ctx context.Context, host, query string, args ...interface{}) *sql.Rows {
	// Begin a TraceView layer for this DB query
	l, _ := tv.BeginLayer(ctx, "dbQuery", "Query", query, "RemoteHost", host)
	defer l.End()

	db, err := sql.Open("mysql", fmt.Sprintf("user:password@tcp(%s:3306)/db", host))
	if err != nil {
		l.Err(err) // Report error & stack trace on Layer span
		return nil
	}
	defer db.Close()
	rows, err := db.Query(query, args...)
	if err != nil {
		l.Err(err)
	}
	return rows
}

// measure a slow function
func slowFunc(ctx context.Context) {
	defer tv.BeginProfile(ctx, "slowFunc").End()
	time.Sleep(1 * time.Second)
}

func Example() {
	ctx := tv.NewContext(context.Background(), tv.NewTrace("myLayer"))
	_ = dbQuery(ctx, "dbhost.net", "SELECT * from tbl LIMIT 1")
	slowFunc(ctx)
	tv.EndTrace(ctx)
}

Add DB query to whole-file example


package commands

import (
	"github.com/spf13/cobra"
)

var outCmd = &cobra.Command{
	Use:   "out",
	Short: "Write a file based on key data.",
	Long:  `out is for writing a file based on a Consul key.`,
	Run:   outRun,
}

func outRun(cmd *cobra.Command, args []string) {
	// Stuff goes here.
}

var KeyLocation string
var FiletoWrite string
var MinFileLength int

func init() {
	RootCmd.AddCommand(outCmd)
	outCmd.Flags().StringVarP(&FiletoWrite, "key", "k", "", "key to pull data from")
	outCmd.Flags().StringVarP(&FiletoWrite, "file", "f", "", "where to write the data")
	outCmd.Flags().IntVarP(&MinFileLength, "length", "l", 10, "minimum amount of lines in the file")
}

package commands

import (
	"github.com/spf13/cobra"
  "fmt"
)

var outCmd = &cobra.Command{
	Use:   "out",
	Short: "Write a file based on key data.",
	Long:  `out is for writing a file based on a Consul key.`,
	Run:   outRun,
}

func outRun(cmd *cobra.Command, args []string) {
  checkFlags()
}

func checkFlags() {
  if KeyLocation == "" {
    fmt.Println("Need a key location in -k")
  }
  if FiletoWrite == "" {
    fmt.Println("Need a file to write in -f")
  }
}

var KeyLocation string
var FiletoWrite string
var MinFileLength int

func init() {
	RootCmd.AddCommand(outCmd)
	outCmd.Flags().StringVarP(&KeyLocation, "key", "k", "", "key to pull data from")
	outCmd.Flags().StringVarP(&FiletoWrite, "file", "f", "", "where to write the data")
	outCmd.Flags().IntVarP(&MinFileLength, "length", "l", 10, "minimum amount of lines in the file")
}

Fix variable name and check that required flags are set.


package atlas

import (
	"errors"
)

var (
	allTypes = []string{
		"dns",
		"ntp",
		"ping",
		"sslcert",
		"traceroute",
		"wifi",
	}
)

var ErrInvalidMeasurementType = errors.New("invalid measurement type")

// checkType verify that the type is valid
func checkType(d Definition) (valid bool) {
	valid = false
	for _, t := range allTypes {
		if d.Type == t {
			valid = true
			break
		}
	}
	return
}

// DNS creates a measurement
func DNS(d Definition) (m *Measurement, err error) {
	if checkType(d) || d.Type != "dns" {
		err = ErrInvalidMeasurementType
		return
	}
	return
}

// NTP creates a measurement
func NTP(d Definition) (m *Measurement, err error) {
	return
}

// Ping creates a measurement
func Ping(d Definition) (m *Measurement, err error) {
	return
}

// SSLCert creates a measurement
func SSLCert(d Definition) (m *Measurement, err error) {
	return
}

// Traceroute creates a measurement
func Traceroute(d Definition) (m *Measurement, err error) {
	return
}

package atlas

import (
	"errors"
)

var (
	allTypes = []string{
		"dns",
		"ntp",
		"ping",
		"sslcert",
		"traceroute",
		"wifi",
	}
)

var ErrInvalidMeasurementType = errors.New("invalid measurement type")

// checkType verify that the type is valid
func checkType(d Definition) (valid bool) {
	valid = false
	for _, t := range allTypes {
		if d.Type == t {
			valid = true
			break
		}
	}
	return
}

// checkTypeAs is a shortcut
func checkTypeAs(d Definition, t string) (valid bool) {
	valid = true
	if checkType(d) && d.Type != t {
		valid = false
	}
	return
}

// DNS creates a measurement
func DNS(d Definition) (m *Measurement, err error) {
	if checkType(d) || d.Type != "dns" {
		err = ErrInvalidMeasurementType
		return
	}
	return
}

// NTP creates a measurement
func NTP(d Definition) (m *Measurement, err error) {
	return
}

// Ping creates a measurement
func Ping(d Definition) (m *Measurement, err error) {
	return
}

// SSLCert creates a measurement
func SSLCert(d Definition) (m *Measurement, err error) {
	return
}

// Traceroute creates a measurement
func Traceroute(d Definition) (m *Measurement, err error) {
	return
}

Add checkTypeAs() to simplify the code/tests.


package index

import (
	"context"
)

// Exister allows to check for existence of the index.
type Exister interface {
	Exists(ctx context.Context) (bool, error)
}

// Creator allows to create the index.
type Creator interface {
	Create(ctx context.Context) error
}

// ConfigUpdater represents an index with configuration.
type ConfigUpdater interface {
	ConfigUpToDate(context.Context) (bool, error)
	ConfigUpdate(context.Context) error
}

// ManagedIndex is an index which allows management
type ManagedIndex interface {
	Index
	Exister
	Creator
	ConfigUpdater
}

package index

import (
	"context"
)

// Exister allows to check for existence of the index.
type Exister interface {
	Exists(ctx context.Context) (bool, error)
}

// Creator allows to create the index.
type Creator interface {
	Create(ctx context.Context) error
}

// ConfigUpdater represents an index with configuration.
type ConfigUpdater interface {
	ConfigUpToDate(context.Context) (bool, error)
	ConfigUpdate(context.Context) error
}

// ManagedIndex is an index which allows management
// TODO: Factor this into the index initialiser (automate it away) or add as specific -single- Bootstrap() or Prepare() method.
type ManagedIndex interface {
	Index
	Exister
	Creator
	ConfigUpdater
}

Add TODO for future refactor.


package reception

import (
	"testing"

	"github.com/aphistic/sweet"
	"github.com/aphistic/sweet-junit"
	. "github.com/onsi/gomega"
)

func TestMain(m *testing.M) {
	RegisterFailHandler(sweet.GomegaFail)

	sweet.Run(m, func(s *sweet.S) {
		s.RegisterPlugin(junit.NewPlugin())

		s.AddSuite(&ZkSuite{})
	})
}

package reception

import (
	"testing"

	"github.com/aphistic/sweet"
	"github.com/aphistic/sweet-junit"
	. "github.com/onsi/gomega"
)

func TestMain(m *testing.M) {
	RegisterFailHandler(sweet.GomegaFail)

	sweet.Run(m, func(s *sweet.S) {
		s.RegisterPlugin(junit.NewPlugin())

		s.AddSuite(&ElectionSuite{})
		s.AddSuite(&ZkSuite{})
	})
}

Add election suite to test runner.


package view

import (
	"bytes"
	"crypto/sha1"
	"fmt"
	"log"
	"sort"
)

type ViewRef struct {
	Digest [sha1.Size]byte
}

func ViewToViewRef(v *View) ViewRef {
	var updates byUpdate
	updates = v.GetUpdates()

	// sort it to make it a canonical view
	sort.Sort(updates)

	buf := new(bytes.Buffer)
	for _, loopUpdate := range updates {
		fmt.Fprintf(buf, "%v", loopUpdate)
	}

	return ViewRef{sha1.Sum(buf.Bytes())}
}

type byUpdate []Update

func (s byUpdate) Len() int           { return len(s) }
func (s byUpdate) Less(i, j int) bool { return s[i].Less(s[j]) }
func (s byUpdate) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }

package view

import (
	"bytes"
	"crypto/sha1"
	"fmt"
	"sort"
)

type ViewRef struct {
	Digest [sha1.Size]byte
}

func ViewToViewRef(v *View) ViewRef {
	var updates byUpdate
	updates = v.GetUpdates()

	// sort it to make it a canonical view
	sort.Sort(updates)

	buf := new(bytes.Buffer)
	for _, loopUpdate := range updates {
		fmt.Fprintf(buf, "%v", loopUpdate)
	}

	return ViewRef{sha1.Sum(buf.Bytes())}
}

type byUpdate []Update

func (s byUpdate) Len() int           { return len(s) }
func (s byUpdate) Less(i, j int) bool { return s[i].Less(s[j]) }
func (s byUpdate) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }

Fix last commit, remove unused package


package orm

import (
	"gnd.la/orm/operation"
	"gnd.la/orm/query"
)

func (o *Orm) Operate(q query.Q, table *Table, op *operation.Operation) (Result, error) {
	return o.conn.Operate(table.model, q, op)
}

func (o *Orm) MustOperate(q query.Q, table *Table, op *operation.Operation) Result {
	res, err := o.Operate(q, table, op)
	if err != nil {
		panic(err)
	}
	return res
}

package orm

import (
	"gnd.la/orm/operation"
	"gnd.la/orm/query"
)

func (o *Orm) Operate(table *Table, q query.Q, op *operation.Operation) (Result, error) {
	return o.conn.Operate(table.model, q, op)
}

func (o *Orm) MustOperate(table *Table, q query.Q, op *operation.Operation) Result {
	res, err := o.Operate(table, q, op)
	if err != nil {
		panic(err)
	}
	return res
}

Make the *Table the 1st argument to Operate()


package db

import (
	"database/sql"
	"encoding/json"
	"fmt"
	sq "github.com/lann/squirrel"
)

var EffectRecordSelect sq.SelectBuilder = sq.
	Select("heff.*").
	From("history_effects heff")

type EffectRecord struct {
	HistoryRecord
	HistoryAccountID   int64          `db:"history_account_id"`
	HistoryOperationID int64          `db:"history_operation_id"`
	Order              int32          `db:"order"`
	Type               int32          `db:"type"`
	DetailsString      sql.NullString `db:"details"`
}

func (r EffectRecord) Details() (result map[string]interface{}, err error) {
	if !r.DetailsString.Valid {
		return
	}

	err = json.Unmarshal([]byte(r.DetailsString.String), &result)

	return
}

// ID returns a lexically ordered id for this effect record
func (r EffectRecord) ID() string {
	return fmt.Sprintf("%019d-%010d", r.HistoryOperationID, r.Order)
}

package db

import (
	"database/sql"
	"encoding/json"
	"fmt"
	sq "github.com/lann/squirrel"
)

var EffectRecordSelect sq.SelectBuilder = sq.
	Select("heff.*").
	From("history_effects heff")

type EffectRecord struct {
	HistoryAccountID   int64          `db:"history_account_id"`
	HistoryOperationID int64          `db:"history_operation_id"`
	Order              int32          `db:"order"`
	Type               int32          `db:"type"`
	DetailsString      sql.NullString `db:"details"`
}

func (r EffectRecord) Details() (result map[string]interface{}, err error) {
	if !r.DetailsString.Valid {
		return
	}

	err = json.Unmarshal([]byte(r.DetailsString.String), &result)

	return
}

// ID returns a lexically ordered id for this effect record
func (r EffectRecord) ID() string {
	return fmt.Sprintf("%019d-%010d", r.HistoryOperationID, r.Order)
}

func (r EffectRecord) PagingToken() string {
	return fmt.Sprintf("%d-%d", r.HistoryOperationID, r.Order)
}

Add paging token to EffectRecord