<commit_before>package pools

import (
	"context"
	"net"
	"sync"
	"time"
)

type tcpConnResource struct {
	c *net.TCPConn
}

func (t tcpConnResource) Close() {
	t.c.Close()
}

// AddrTCPConnPool is the connection pool based on the address, that's, when you
// need a connection, you only get the connection by the address.
type AddrTCPConnPool struct {
	lock *sync.Mutex

	size  int
	pools map[string]*ResourcePool

	// The connection timeout, and the default is 3s.
	Timeout time.Duration
}

// NewAddrTCPConnPool returns a new AddrTCPConnPool.
func NewAddrTCPConnPool(size int) AddrTCPConnPool {
	return AddrTCPConnPool{
		size:  size,
		lock:  new(sync.Mutex),
		pools: make(map[string]*ResourcePool),

		Timeout: 3 * time.Second,
	}
}

// Put places the TCP connection into the pool relating to the addr.
//
// For every successful Get, a corresponding Put is required. If you no longer
// need a resource, you will need to call Put(nil) instead of returning the
// closed resource.
func (p AddrTCPConnPool) Put(addr string, c *net.TCPConn) {
	p.lock.Lock()
	rp := p.pools[addr]
	p.lock.Unlock()

	rp.Put(tcpConnResource{c: c})
}

// Get returns a TCP connection by the addr from the pool.
func (p AddrTCPConnPool) Get(addr string) (c *net.TCPConn, err error) {
	var rp *ResourcePool

	p.lock.Lock()
	rp, ok := p.pools[addr]
	if !ok {
		rp = NewResourcePool(func() (Resource, error) {
			c, err := net.Dial("tcp", addr)
			if err != nil {
				return nil, err
			}
			return tcpConnResource{c: c.(*net.TCPConn)}, nil
		}, p.size, p.size, p.Timeout)
		p.pools[addr] = rp
	}
	p.lock.Unlock()

	r, err := rp.Get(context.TODO())
	if err != nil {
		return
	}
	c = r.(tcpConnResource).c
	return
}
<commit_msg>Use the wrapper function.Close instead<commit_after>package pools

import (
	"context"
	"net"
	"sync"
	"time"

	"github.com/xgfone/go-tools/function"
)

// AddrTCPConnPool is the connection pool based on the address, that's, when you
// need a connection, you only get the connection by the address.
type AddrTCPConnPool struct {
	lock *sync.Mutex

	size  int
	pools map[string]*ResourcePool

	// The connection timeout, and the default is 3s.
	Timeout time.Duration
}

// NewAddrTCPConnPool returns a new AddrTCPConnPool.
func NewAddrTCPConnPool(size int) AddrTCPConnPool {
	return AddrTCPConnPool{
		size:  size,
		lock:  new(sync.Mutex),
		pools: make(map[string]*ResourcePool),

		Timeout: 3 * time.Second,
	}
}

// Put places the TCP connection into the pool relating to the addr.
//
// For every successful Get, a corresponding Put is required. If you no longer
// need a resource, you will need to call Put(nil) instead of returning the
// closed resource.
func (p AddrTCPConnPool) Put(addr string, c *net.TCPConn) {
	p.lock.Lock()
	rp := p.pools[addr]
	p.lock.Unlock()

	rp.Put(function.NewClose(c))
}

// Get returns a TCP connection by the addr from the pool.
func (p AddrTCPConnPool) Get(addr string) (c *net.TCPConn, err error) {
	var rp *ResourcePool

	p.lock.Lock()
	rp, ok := p.pools[addr]
	if !ok {
		rp = NewResourcePool(func() (Resource, error) {
			c, err := net.Dial("tcp", addr)
			if err != nil {
				return nil, err
			}
			return function.NewClose(c.(*net.TCPConn)), nil
		}, p.size, p.size, p.Timeout)
		p.pools[addr] = rp
	}
	p.lock.Unlock()

	r, err := rp.Get(context.TODO())
	if err != nil {
		return
	}
	c = r.(function.Close).Value.(*net.TCPConn)
	return
}
<|endoftext|>


<commit_before>package api

import (
	"cf"
	"cf/configuration"
	"cf/net"
	"fmt"
	"strings"
)

type PaginatedSpaceResources struct {
	Resources []SpaceResource
}

type SpaceResource struct {
	Metadata Metadata
	Entity   SpaceEntity
}

type SpaceEntity struct {
	Name             string
	Organization     Resource
	Applications     []Resource `json:"apps"`
	Domains          []Resource
	ServiceInstances []Resource `json:"service_instances"`
}

type SpaceRepository interface {
	FindAll() (spaces []cf.Space, apiResponse net.ApiResponse)
	FindByName(name string) (space cf.Space, apiResponse net.ApiResponse)
	FindByNameInOrg(name string, org cf.Organization) (space cf.Space, apiResponse net.ApiResponse)
	Create(name string) (apiResponse net.ApiResponse)
	Rename(space cf.Space, newName string) (apiResponse net.ApiResponse)
	Delete(space cf.Space) (apiResponse net.ApiResponse)
}

type CloudControllerSpaceRepository struct {
	config  *configuration.Configuration
	gateway net.Gateway
}

func NewCloudControllerSpaceRepository(config *configuration.Configuration, gateway net.Gateway) (repo CloudControllerSpaceRepository) {
	repo.config = config
	repo.gateway = gateway
	return
}

func (repo CloudControllerSpaceRepository) FindAll() (spaces []cf.Space, apiResponse net.ApiResponse) {
	path := fmt.Sprintf("%s/v2/organizations/%s/spaces", repo.config.Target, repo.config.Organization.Guid)
	return repo.findAllWithPath(path)
}

func (repo CloudControllerSpaceRepository) FindByName(name string) (space cf.Space, apiResponse net.ApiResponse) {
	return repo.FindByNameInOrg(name, repo.config.Organization)
}

func (repo CloudControllerSpaceRepository) FindByNameInOrg(name string, org cf.Organization) (space cf.Space, apiResponse net.ApiResponse) {
	path := fmt.Sprintf("%s/v2/organizations/%s/spaces?q=name%s&inline-relations-depth=1",
		repo.config.Target, org.Guid, "%3A"+strings.ToLower(name))

	spaces, apiResponse := repo.findAllWithPath(path)
	if apiResponse.IsNotSuccessful() {
		return
	}

	if len(spaces) == 0 {
		apiResponse = net.NewNotFoundApiResponse("%s %s not found", "Space", name)
		return
	}

	space = spaces[0]
	return
}

func (repo CloudControllerSpaceRepository) findAllWithPath(path string) (spaces []cf.Space, apiResponse net.ApiResponse) {
	request, apiResponse := repo.gateway.NewRequest("GET", path, repo.config.AccessToken, nil)
	if apiResponse.IsNotSuccessful() {
		return
	}

	resources := new(PaginatedSpaceResources)
	_, apiResponse = repo.gateway.PerformRequestForJSONResponse(request, resources)
	if apiResponse.IsNotSuccessful() {
		return
	}

	for _, r := range resources.Resources {
		apps := []cf.Application{}
		for _, app := range r.Entity.Applications {
			apps = append(apps, cf.Application{Name: app.Entity.Name, Guid: app.Metadata.Guid})
		}

		domains := []cf.Domain{}
		for _, domain := range r.Entity.Domains {
			domains = append(domains, cf.Domain{Name: domain.Entity.Name, Guid: domain.Metadata.Guid})
		}

		services := []cf.ServiceInstance{}
		for _, service := range r.Entity.ServiceInstances {
			services = append(services, cf.ServiceInstance{Name: service.Entity.Name, Guid: service.Metadata.Guid})
		}
		space := cf.Space{
			Name: r.Entity.Name,
			Guid: r.Metadata.Guid,
			Organization: cf.Organization{
				Name: r.Entity.Organization.Entity.Name,
				Guid: r.Entity.Organization.Metadata.Guid,
			},
			Applications:     apps,
			Domains:          domains,
			ServiceInstances: services,
		}

		spaces = append(spaces, space)
	}
	return
}

func (repo CloudControllerSpaceRepository) Create(name string) (apiResponse net.ApiResponse) {
	path := fmt.Sprintf("%s/v2/spaces", repo.config.Target)
	body := fmt.Sprintf(`{"name":"%s","organization_guid":"%s"}`, name, repo.config.Organization.Guid)

	request, apiResponse := repo.gateway.NewRequest("POST", path, repo.config.AccessToken, strings.NewReader(body))
	if apiResponse.IsNotSuccessful() {
		return
	}

	apiResponse = repo.gateway.PerformRequest(request)
	return
}

func (repo CloudControllerSpaceRepository) Rename(space cf.Space, newName string) (apiResponse net.ApiResponse) {
	path := fmt.Sprintf("%s/v2/spaces/%s", repo.config.Target, space.Guid)
	body := fmt.Sprintf(`{"name":"%s"}`, newName)

	request, apiResponse := repo.gateway.NewRequest("PUT", path, repo.config.AccessToken, strings.NewReader(body))
	if apiResponse.IsNotSuccessful() {
		return
	}

	apiResponse = repo.gateway.PerformRequest(request)
	return
}

func (repo CloudControllerSpaceRepository) Delete(space cf.Space) (apiResponse net.ApiResponse) {
	path := fmt.Sprintf("%s/v2/spaces/%s?recursive=true", repo.config.Target, space.Guid)

	request, apiResponse := repo.gateway.NewRequest("DELETE", path, repo.config.AccessToken, nil)
	if apiResponse.IsNotSuccessful() {
		return
	}

	apiResponse = repo.gateway.PerformRequest(request)
	return
}
<commit_msg>Refactored spaces repo<commit_after>package api

import (
	"cf"
	"cf/configuration"
	"cf/net"
	"fmt"
	"strings"
	"io"
)

type PaginatedSpaceResources struct {
	Resources []SpaceResource
}

type SpaceResource struct {
	Metadata Metadata
	Entity   SpaceEntity
}

type SpaceEntity struct {
	Name             string
	Organization     Resource
	Applications     []Resource `json:"apps"`
	Domains          []Resource
	ServiceInstances []Resource `json:"service_instances"`
}

type SpaceRepository interface {
	FindAll() (spaces []cf.Space, apiResponse net.ApiResponse)
	FindByName(name string) (space cf.Space, apiResponse net.ApiResponse)
	FindByNameInOrg(name string, org cf.Organization) (space cf.Space, apiResponse net.ApiResponse)
	Create(name string) (apiResponse net.ApiResponse)
	Rename(space cf.Space, newName string) (apiResponse net.ApiResponse)
	Delete(space cf.Space) (apiResponse net.ApiResponse)
}

type CloudControllerSpaceRepository struct {
	config  *configuration.Configuration
	gateway net.Gateway
}

func NewCloudControllerSpaceRepository(config *configuration.Configuration, gateway net.Gateway) (repo CloudControllerSpaceRepository) {
	repo.config = config
	repo.gateway = gateway
	return
}

func (repo CloudControllerSpaceRepository) FindAll() (spaces []cf.Space, apiResponse net.ApiResponse) {
	path := fmt.Sprintf("%s/v2/organizations/%s/spaces", repo.config.Target, repo.config.Organization.Guid)
	return repo.findAllWithPath(path)
}

func (repo CloudControllerSpaceRepository) FindByName(name string) (space cf.Space, apiResponse net.ApiResponse) {
	return repo.FindByNameInOrg(name, repo.config.Organization)
}

func (repo CloudControllerSpaceRepository) FindByNameInOrg(name string, org cf.Organization) (space cf.Space, apiResponse net.ApiResponse) {
	path := fmt.Sprintf("%s/v2/organizations/%s/spaces?q=name%s&inline-relations-depth=1",
		repo.config.Target, org.Guid, "%3A"+strings.ToLower(name))

	spaces, apiResponse := repo.findAllWithPath(path)
	if apiResponse.IsNotSuccessful() {
		return
	}

	if len(spaces) == 0 {
		apiResponse = net.NewNotFoundApiResponse("%s %s not found", "Space", name)
		return
	}

	space = spaces[0]
	return
}

func (repo CloudControllerSpaceRepository) findAllWithPath(path string) (spaces []cf.Space, apiResponse net.ApiResponse) {
	request, apiResponse := repo.gateway.NewRequest("GET", path, repo.config.AccessToken, nil)
	if apiResponse.IsNotSuccessful() {
		return
	}

	resources := new(PaginatedSpaceResources)
	_, apiResponse = repo.gateway.PerformRequestForJSONResponse(request, resources)
	if apiResponse.IsNotSuccessful() {
		return
	}

	for _, r := range resources.Resources {
		apps := []cf.Application{}
		for _, app := range r.Entity.Applications {
			apps = append(apps, cf.Application{Name: app.Entity.Name, Guid: app.Metadata.Guid})
		}

		domains := []cf.Domain{}
		for _, domain := range r.Entity.Domains {
			domains = append(domains, cf.Domain{Name: domain.Entity.Name, Guid: domain.Metadata.Guid})
		}

		services := []cf.ServiceInstance{}
		for _, service := range r.Entity.ServiceInstances {
			services = append(services, cf.ServiceInstance{Name: service.Entity.Name, Guid: service.Metadata.Guid})
		}
		space := cf.Space{
			Name: r.Entity.Name,
			Guid: r.Metadata.Guid,
			Organization: cf.Organization{
				Name: r.Entity.Organization.Entity.Name,
				Guid: r.Entity.Organization.Metadata.Guid,
			},
			Applications:     apps,
			Domains:          domains,
			ServiceInstances: services,
		}

		spaces = append(spaces, space)
	}
	return
}

func (repo CloudControllerSpaceRepository) Create(name string) (apiResponse net.ApiResponse) {
	body := fmt.Sprintf(`{"name":"%s","organization_guid":"%s"}`, name, repo.config.Organization.Guid)
	return repo.createOrUpdate(cf.Space{Name: name}, strings.NewReader(body))
}

func (repo CloudControllerSpaceRepository) Rename(space cf.Space, newName string) (apiResponse net.ApiResponse) {
	body := fmt.Sprintf(`{"name":"%s"}`, newName)
	return repo.createOrUpdate(space, strings.NewReader(body))
}

func (repo CloudControllerSpaceRepository) createOrUpdate(space cf.Space, body io.Reader) (apiResponse net.ApiResponse) {
	verb := "POST"
	path := fmt.Sprintf("%s/v2/spaces", repo.config.Target)

	if space.Guid != "" {
		verb = "PUT"
		path = fmt.Sprintf("%s/v2/spaces/%s", repo.config.Target, space.Guid)
	}

	req, apiResponse := repo.gateway.NewRequest(verb, path, repo.config.AccessToken, body)
	if apiResponse.IsNotSuccessful() {
		return
	}

	apiResponse = repo.gateway.PerformRequest(req)
	return
}

func (repo CloudControllerSpaceRepository) Delete(space cf.Space) (apiResponse net.ApiResponse) {
	path := fmt.Sprintf("%s/v2/spaces/%s?recursive=true", repo.config.Target, space.Guid)

	request, apiResponse := repo.gateway.NewRequest("DELETE", path, repo.config.AccessToken, nil)
	if apiResponse.IsNotSuccessful() {
		return
	}

	apiResponse = repo.gateway.PerformRequest(request)
	return
}
<|endoftext|>


<commit_before>//  Copyright (c) 2015 Couchbase, Inc.
//  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 cmd

import (
	log "github.com/couchbase/clog"

	"github.com/couchbaselabs/cbgt"
)

func PlannerSteps(steps map[string]bool,
	cfg cbgt.Cfg, version, server string, nodesToRemove []string,
	dryRun bool) error {
	if steps == nil || steps["unregister"] {
		log.Printf("planner: step unregister")

		if !dryRun {
			err := cbgt.UnregisterNodes(cfg, cbgt.VERSION, nodesToRemove)
			if err != nil {
				return err
			}
		}
	}

	if steps == nil || steps["planner"] {
		log.Printf("planner: step planner")

		if !dryRun {
			_, err := cbgt.Plan(cfg, cbgt.VERSION, "", server)
			if err != nil {
				return err
			}
		}
	}

	return nil
}
<commit_msg>placeholder cmd.Failover func<commit_after>//  Copyright (c) 2015 Couchbase, Inc.
//  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 cmd

import (
	log "github.com/couchbase/clog"

	"github.com/couchbaselabs/cbgt"
)

// PlannerSteps helps command-line tools implement the planner steps:
// * "unregister" - unregisters nodesToRemove from the cfg.
// * "planner" - runs the planner to calculate a new plan into the cfg.
//
// The default steps are "unregister" and "planner".
//
// An additional composite step, "FAILOVER" (fully capitalized), is
// used to process the nodesToRemove as nodes to be failover'ed.
// "FAILOVER" is comprised of the more basic "unregister" and
// "failover" steps.
func PlannerSteps(steps map[string]bool,
	cfg cbgt.Cfg, version, server string, nodesToRemove []string,
	dryRun bool) error {
	if steps == nil || steps["unregister"] || steps["FAILOVER"] {
		log.Printf("planner: step unregister")

		if !dryRun {
			err := cbgt.UnregisterNodes(cfg, cbgt.VERSION, nodesToRemove)
			if err != nil {
				return err
			}
		}
	}

	if steps == nil || steps["planner"] {
		log.Printf("planner: step planner")

		if !dryRun {
			_, err := cbgt.Plan(cfg, cbgt.VERSION, "", server)
			if err != nil {
				return err
			}
		}
	}

	if steps["failover"] || steps["FAILOVER"] {
		log.Printf("planner: step failover")

		if !dryRun {
			err := Failover(cfg, cbgt.VERSION, nodesToRemove)
			if err != nil {
				return err
			}
		}
	}

	return nil
}

// ------------------------------------------------------

func Failover(cfg cbgt.Cfg, version string,
	nodesToFailover []string) error {
	// TODO.

	return nil
}
<|endoftext|>


<commit_before>package flux

import "testing"

func TestBlockPushSocket(t *testing.T) {
	sock := PushSocket(0)

	sock.Subscribe(func(v interface{}, r *Sub) {
		// defer r.Close()
		_, ok := v.(string)
		t.Log("blockpush:", v)
		if !ok {
			t.Fatal("value received is not a string", v, ok, r)
		}
	})

	sock.PushStream()
	sock.PushStream()
	sock.PushStream()

	sock.Emit("Token")
	sock.Emit("Bottle")
	sock.Emit("Rocket")

	sock.Close()
}

func TestPushSocket(t *testing.T) {
	sock := PushSocket(3)

	sock.Subscribe(func(v interface{}, r *Sub) {
		// defer r.Close()
		t.Log("testpusher:", v)
		_, ok := v.(string)
		if !ok {
			t.Fatal("value received is not a string", v, ok, r)
		}
	})

	sock.PushStream()
	sock.PushStream()
	sock.PushStream()

	sock.Emit("Token")
	sock.Emit("Bottle")
	sock.Emit("Rocket")

	sock.Close()
}

func TestConditionedPushPullSocket(t *testing.T) {
	sock := PushSocket(0)
	dsock := DoPushSocket(sock, func(v interface{}, s SocketInterface) {
		if v == "Bottle" {
			s.Emit(v)
		}
	})

	defer sock.Close()
	defer dsock.Close()

	dsock.Subscribe(func(v interface{}, s *Sub) {
		// defer s.Close()
		t.Log("cond:", v)
		_, ok := v.(string)
		if !ok {
			t.Fatal("value received is not a string", v, ok, s)
		}
	})

	sock.Emit("Token")
	sock.Emit("Bottle")
	sock.Emit("Beer")

}
<commit_msg>minimized to push only sockets<commit_after>package flux

import "testing"

func TestBlockPushSocket(t *testing.T) {
	sock := PushSocket(0)

	sock.Subscribe(func(v interface{}, r *Sub) {
		// defer r.Close()
		_, ok := v.(string)
		t.Log("blockpush:", v)
		if !ok {
			t.Fatal("value received is not a string", v, ok, r)
		}
	})

	sock.PushStream()
	sock.PushStream()
	sock.PushStream()

	sock.Emit("Token")
	sock.Emit("Bottle")
	sock.Emit("Rocket")

	sock.Close()
}

func TestPushSocket(t *testing.T) {
	sock := PushSocket(3)

	sock.Subscribe(func(v interface{}, r *Sub) {
		// defer r.Close()
		t.Log("testpusher:", v)
		_, ok := v.(string)
		if !ok {
			t.Fatal("value received is not a string", v, ok, r)
		}
	})

	sock.PushStream()
	sock.PushStream()
	sock.PushStream()

	sock.Emit("Token")
	sock.Emit("Bottle")
	sock.Emit("Rocket")

	sock.Close()
}

func TestConditionedPushPullSocket(t *testing.T) {
	sock := PushSocket(0)
	dsock := DoPushSocket(sock, func(v interface{}, s SocketInterface) {
		if v == "Bottle" {
			s.Emit(v)
		}
	})

	defer dsock.Close()
	defer sock.Close()

	dsock.Subscribe(func(v interface{}, s *Sub) {
		// defer s.Close()
		t.Log("cond:", v)
		_, ok := v.(string)
		if !ok {
			t.Fatal("value received is not a string", v, ok, s)
		}
	})

	sock.Emit("Token")
	sock.Emit("Bottle")
	sock.Emit("Beer")

}
<|endoftext|>


<commit_before>//go:build linux
// +build linux

package output

import (
	"fmt"
	"log"
	"time"

	"github.com/jfreymuth/pulse"
)

type output struct {
	client      *pulse.Client
	stream      *pulse.PlaybackStream
	samplesChan chan []float32
	samplesBuf  []float32
	sampleRate  uint32
	channels    uint8
}

func (o *output) init() {
	if o.client != nil {
		o.stream.Drain()
		o.stream.Close()
		o.stream = nil
	}
	var err error
	o.client, err = pulse.NewClient(pulse.ClientApplicationName("moggio"))
	if err != nil {
		log.Println(err)
		return
	}
	var channels pulse.PlaybackOption
	switch o.channels {
	case 1:
		channels = pulse.PlaybackMono
	case 2:
		channels = pulse.PlaybackStereo
	default:
		log.Println("unsupported channels")
		return
	}
	o.stream, err = o.client.NewPlayback(
		pulse.Float32Reader(o.reader),
		channels,
		pulse.PlaybackSampleRate(int(o.sampleRate)),
		pulse.PlaybackLatency(.1),
	)
	if err != nil {
		log.Println(err)
		return
	}
	o.samplesChan = make(chan []float32)
}

func (o *output) reader(out []float32) (int, error) {
	if len(o.samplesBuf) > 0 {
		n := copy(out, o.samplesBuf)
		o.samplesBuf = o.samplesBuf[n:]
		return n, nil
	}
	select {
	case samples := <-o.samplesChan:
		n := copy(out, samples)
		o.samplesBuf = samples[n:]
		return n, nil
	case <-time.After(100 * time.Millisecond):
		return 0, nil
	}
}

func get(sampleRate, channels int) (Output, error) {
	o := new(output)
	o.sampleRate = uint32(sampleRate)
	o.channels = uint8(channels)
	o.init()
	return o, nil
}

func (o *output) Push(samples []float32) {
	o.samplesChan <- samples
}

func (o *output) Start() {
	// This waits until the buffer is full, so fill it in the background.
	samples := make([]float32, o.stream.BufferSize())
	go o.Push(samples)
	o.stream.Start()
}

func (o *output) Stop() {
	o.stream.Stop()
}
<commit_msg>handle pulse errors and try to restart them<commit_after>//go:build linux
// +build linux

package output

import (
	"errors"
	"fmt"
	"log"
	"time"

	"github.com/jfreymuth/pulse"
)

type output struct {
	client      *pulse.Client
	stream      *pulse.PlaybackStream
	samplesChan chan []float32
	samplesBuf  []float32
	sampleRate  uint32
	channels    uint8
}

func (o *output) init() error {
	if o.stream != nil {
		o.stream.Drain()
		o.stream.Close()
		o.stream = nil
	}
	if o.client != nil {
		o.client.Close()
		o.stream = nil
	}
	var err error
	o.client, err = pulse.NewClient(pulse.ClientApplicationName("moggio"))
	if err != nil {
		return fmt.Errorf("pulse client: %w", err)
	}
	var channels pulse.PlaybackOption
	switch o.channels {
	case 1:
		channels = pulse.PlaybackMono
	case 2:
		channels = pulse.PlaybackStereo
	default:
		return errors.New("unsupported channels")
	}
	// The pulse package sometimes gets stuck here waiting on a recv, so allow it
	// to timeout and error (no idea what happens to the abandoned one).
	type newStream struct {
		stream *pulse.PlaybackStream
		err    error
	}
	ch := make(chan newStream, 1)
	go func() {
		stream, err := o.client.NewPlayback(
			pulse.Float32Reader(o.reader),
			channels,
			pulse.PlaybackSampleRate(int(o.sampleRate)),
			pulse.PlaybackLatency(.1),
		)
		ch <- newStream{stream, err}
	}()
	select {
	case newStream := <-ch:
		if newStream.err != nil {
			return fmt.Errorf("pulse stream new: %w", newStream.err)
		}
		o.stream = newStream.stream
		o.samplesChan = make(chan []float32)
		return nil
	case <-time.After(time.Second):
		return errors.New("pulse stream: timeout making new stream")
	}
}

func (o *output) reader(out []float32) (int, error) {
	if len(o.samplesBuf) > 0 {
		n := copy(out, o.samplesBuf)
		o.samplesBuf = o.samplesBuf[n:]
		return n, nil
	}
	select {
	case samples := <-o.samplesChan:
		n := copy(out, samples)
		o.samplesBuf = samples[n:]
		return n, nil
	case <-time.After(100 * time.Millisecond):
		return 0, nil
	}
}

func get(sampleRate, channels int) (Output, error) {
	o := new(output)
	o.sampleRate = uint32(sampleRate)
	o.channels = uint8(channels)
	err := o.init()
	return o, err
}

func (o *output) Push(samples []float32) {
	select {
	case o.samplesChan <- samples:
	case <-time.After(100 * time.Millisecond):
		if o.stream != nil {
			if err := o.stream.Error(); err != nil {
				log.Println("pulse stream error:", err)
			}
		}
		// Restart the stream.
		log.Println("restarting pulse client")
		if err := o.init(); err != nil {
			log.Println("could not restart pulse:", err)
			return
		}
		go func() {
			o.samplesChan <- samples
		}()
		o.stream.Start()
		log.Println("restarted pulse")
	}
}

func (o *output) Start() {
	// This waits until the buffer is full, so fill it in the background.
	samples := make([]float32, o.stream.BufferSize())
	go o.Push(samples)
	o.stream.Start()
}

func (o *output) Stop() {
	o.stream.Stop()
}
<|endoftext|>


<commit_before>package mnemosyne

import (
	"bytes"
	"database/sql/driver"
	"encoding/hex"
	"errors"
	"strings"
	"time"

	"google.golang.org/grpc"

	"golang.org/x/crypto/sha3"
	"golang.org/x/net/context"
)

const (
	contextKeySession = "context_key_mnemosyne_session"
)

// NewContext returns a new Context that carries Session value.
func NewContext(ctx context.Context, ses Session) context.Context {
	return context.WithValue(ctx, contextKeySession, ses)
}

// FromContext returns the Session value stored in context, if any.
func FromContext(ctx context.Context) (Session, bool) {
	c, ok := ctx.Value(contextKeySession).(Session)
	return c, ok
}

// Mnemosyne ...
type Mnemosyne interface {
	Get(context.Context, *Token) (*Session, error)
	Exists(context.Context, *Token) (bool, error)
	Create(context.Context, map[string]string) (*Session, error)
	Abandon(context.Context, *Token) (bool, error)
	SetData(context.Context, *Token, string, string) (*Session, error)
}

type mnemosyne struct {
	client RPCClient
}

// MnemosyneOpts ...
type MnemosyneOpts struct {
}

// New allocates new mnemosyne instance.
func New(conn *grpc.ClientConn, options MnemosyneOpts) Mnemosyne {
	return &mnemosyne{
		client: NewRPCClient(conn),
	}
}

// Get implements Mnemosyne interface.
func (m *mnemosyne) Get(ctx context.Context, token *Token) (*Session, error) {
	res, err := m.client.Get(ctx, &GetRequest{Token: token})
	if err != nil {
		return nil, err
	}

	return res.Session, nil
}

// Exists implements Mnemosyne interface.
func (m *mnemosyne) Exists(ctx context.Context, token *Token) (bool, error) {
	res, err := m.client.Exists(ctx, &ExistsRequest{Token: token})

	if err != nil {
		return false, err
	}

	return res.Exists, nil
}

// Create implements Mnemosyne interface.
func (m *mnemosyne) Create(ctx context.Context, data map[string]string) (*Session, error) {
	res, err := m.client.Create(ctx, &CreateRequest{Data: data})

	if err != nil {
		return nil, err
	}

	return res.Session, nil
}

// Abandon implements Mnemosyne interface.
func (m *mnemosyne) Abandon(ctx context.Context, token *Token) (bool, error) {
	res, err := m.client.Abandon(ctx, &AbandonRequest{Token: token})

	if err != nil {
		return false, err
	}

	return res.Abandoned, nil
}

// SetData implements Mnemosyne interface.
func (m *mnemosyne) SetData(ctx context.Context, token *Token, key, value string) (*Session, error) {
	res, err := m.client.SetData(ctx, &SetDataRequest{
		Token: token,
		Key:   key,
		Value: value,
	})

	if err != nil {
		return nil, err
	}

	return res.Session, nil
}

// Context implements sklog.Contexter interface.
func (gr *GetRequest) Context() []interface{} {
	return []interface{}{"token", gr.Token}
}

// Context implements sklog.Contexter interface.
func (lr *ListRequest) Context() []interface{} {
	return []interface{}{
		"offset", lr.Offset,
		"limit", lr.Limit,
		"expire_at_from", lr.ExpireAtFrom,
		"expire_at_to", lr.ExpireAtTo,
	}
}

// ExpireAtFromTime ...
func (lr *ListRequest) ExpireAtFromTime() time.Time {
	return TimestampToTime(lr.ExpireAtFrom)
}

// ExpireAtToTime ...
func (lr *ListRequest) ExpireAtToTime() time.Time {
	return TimestampToTime(lr.ExpireAtTo)
}

// Context implements sklog.Contexter interface.
func (er *ExistsRequest) Context() []interface{} {
	return []interface{}{"token", er.Token}
}

// Context implements sklog.Contexter interface.
func (er *CreateRequest) Context() (ctx []interface{}) {
	for key, value := range er.Data {
		ctx = append(ctx, "data_"+key, value)
	}

	return
}

// Context implements sklog.Contexter interface.
func (ar *AbandonRequest) Context() []interface{} {
	return []interface{}{
		"token", ar.Token,
	}
}

// Context implements sklog.Contexter interface.
func (sdr *SetDataRequest) Context() []interface{} {
	return []interface{}{
		"token", sdr.Token,
		"key", sdr.Key,
		"value", sdr.Value,
	}
}

// Context implements sklog.Contexter interface.
func (dr *DeleteRequest) Context() []interface{} {
	return []interface{}{
		"token", dr.Token,
		"expire_at_from", dr.ExpireAtFrom,
		"expire_at_to", dr.ExpireAtTo,
	}
}

// ExpireAtFromTime ...
func (dr *DeleteRequest) ExpireAtFromTime() time.Time {
	return TimestampToTime(dr.ExpireAtFrom)
}

// ExpireAtToTime ...
func (dr *DeleteRequest) ExpireAtToTime() time.Time {
	return TimestampToTime(dr.ExpireAtTo)
}

// SetValue ...
func (s *Session) SetValue(key, value string) {
	if s.Data == nil {
		s.Data = make(map[string]string)
	}

	s.Data[key] = value
}

// Value ...
func (s *Session) Value(key string) string {
	if s.Data == nil {
		s.Data = make(map[string]string)
	}

	return s.Data[key]
}

// ExpireAtTime ...
func (s *Session) ExpireAtTime() time.Time {
	return TimestampToTime(s.ExpireAt)
}

// ParseTime ...
func ParseTime(s string) (time.Time, error) {
	return time.Parse(time.RFC3339, s)
}

// Value implements driver.Valuer interface.
func (t Token) Value() (driver.Value, error) {
	return t.Key + ":" + t.Hash, nil
}

// Scan implements sql.Scanner interface.
func (t *Token) Scan(src interface{}) error {
	var token *Token
	var err error

	switch s := src.(type) {
	case []byte:
		token, err = NewTokenFromBytes(s)
	case string:
		token, err = NewTokenFromString(s)
	default:
		return errors.New("mnemosyne: token supports scan only from slice of bytes and string")
	}
	if err != nil {
		return err
	}

	*t = *token

	return nil
}

// NewTokenFromString parse string and allocates new token instance if ok.
func NewTokenFromString(s string) (*Token, error) {
	parts := strings.Split(s, ":")

	if len(parts) != 2 {
		return nil, errors.New("mnemosyne: token cannot be allocated, given string has wrong format")
	}

	return NewToken(parts[0], parts[1]), nil
}

// NewToken allocates new Token instance.
func NewToken(key, hash string) *Token {
	return &Token{
		Key:  key,
		Hash: hash,
	}
}

// NewTokenFromBytes ...
func NewTokenFromBytes(b []byte) (*Token, error) {
	parts := bytes.Split(b, []byte{':'})

	if len(parts) != 2 {
		return nil, errors.New("mnemosyne: token cannot be allocated, given byte slice has wrong format")
	}

	return NewToken(string(parts[0]), string(parts[1])), nil
}

// NewTokenRandom ...
func NewTokenRandom(g RandomBytesGenerator, k string) (*Token, error) {
	buf, err := g.GenerateRandomBytes(128)
	if err != nil {
		return nil, err
	}

	// A hash needs to be 64 bytes long to have 256-bit collision resistance.
	hash := make([]byte, 64)
	// Compute a 64-byte hash of buf and put it in h.
	sha3.ShakeSum256(hash, buf)

	return NewToken(k, hex.EncodeToString(hash)), nil
}
<commit_msg>context aware functions reformat<commit_after>package mnemosyne

import (
	"bytes"
	"database/sql/driver"
	"encoding/hex"
	"errors"
	"strings"
	"time"

	"google.golang.org/grpc"

	"golang.org/x/crypto/sha3"
	"golang.org/x/net/context"
)

const (
	contextKeyToken = "context_key_mnemosyne_token"
)

// NewTokenContext returns a new Context that carries Token value.
func NewTokenContext(ctx context.Context, t Token) context.Context {
	return context.WithValue(ctx, contextKeyToken, t)
}

// TokenFromContext returns the Token value stored in context, if any.
func TokenFromContext(ctx context.Context) (Token, bool) {
	t, ok := ctx.Value(contextKeyToken).(Token)
	return t, ok
}

// Mnemosyne ...
type Mnemosyne interface {
	Get(context.Context, *Token) (*Session, error)
	GetArbitrarily(context.Context) (*Session, error)
	Exists(context.Context, *Token) (bool, error)
	Create(context.Context, map[string]string) (*Session, error)
	Abandon(context.Context, *Token) (bool, error)
	SetData(context.Context, *Token, string, string) (*Session, error)
}

type mnemosyne struct {
	client RPCClient
}

// MnemosyneOpts ...
type MnemosyneOpts struct {
}

// New allocates new mnemosyne instance.
func New(conn *grpc.ClientConn, options MnemosyneOpts) Mnemosyne {
	return &mnemosyne{
		client: NewRPCClient(conn),
	}
}

// Get implements Mnemosyne interface.
func (m *mnemosyne) Get(ctx context.Context, token *Token) (*Session, error) {
	res, err := m.client.Get(ctx, &GetRequest{Token: token})
	if err != nil {
		return nil, err
	}

	return res.Session, nil
}

// GetArbitrarily implements Mnemosyne interface.
func (m *mnemosyne) GetArbitrarily(ctx context.Context) (*Session, error) {
	token, ok := TokenFromContext(ctx)
	if !ok {
		return nil, errors.New("mnemosyne: session cannot be retrieved, missing session token in the context")
	}

	return m.Get(ctx, &token)
}

// Exists implements Mnemosyne interface.
func (m *mnemosyne) Exists(ctx context.Context, token *Token) (bool, error) {
	res, err := m.client.Exists(ctx, &ExistsRequest{Token: token})

	if err != nil {
		return false, err
	}

	return res.Exists, nil
}

// Create implements Mnemosyne interface.
func (m *mnemosyne) Create(ctx context.Context, data map[string]string) (*Session, error) {
	res, err := m.client.Create(ctx, &CreateRequest{Data: data})

	if err != nil {
		return nil, err
	}

	return res.Session, nil
}

// Abandon implements Mnemosyne interface.
func (m *mnemosyne) Abandon(ctx context.Context, token *Token) (bool, error) {
	res, err := m.client.Abandon(ctx, &AbandonRequest{Token: token})

	if err != nil {
		return false, err
	}

	return res.Abandoned, nil
}

// SetData implements Mnemosyne interface.
func (m *mnemosyne) SetData(ctx context.Context, token *Token, key, value string) (*Session, error) {
	res, err := m.client.SetData(ctx, &SetDataRequest{
		Token: token,
		Key:   key,
		Value: value,
	})

	if err != nil {
		return nil, err
	}

	return res.Session, nil
}

// Context implements sklog.Contexter interface.
func (gr *GetRequest) Context() []interface{} {
	return []interface{}{"token", gr.Token}
}

// Context implements sklog.Contexter interface.
func (lr *ListRequest) Context() []interface{} {
	return []interface{}{
		"offset", lr.Offset,
		"limit", lr.Limit,
		"expire_at_from", lr.ExpireAtFrom,
		"expire_at_to", lr.ExpireAtTo,
	}
}

// ExpireAtFromTime ...
func (lr *ListRequest) ExpireAtFromTime() time.Time {
	return TimestampToTime(lr.ExpireAtFrom)
}

// ExpireAtToTime ...
func (lr *ListRequest) ExpireAtToTime() time.Time {
	return TimestampToTime(lr.ExpireAtTo)
}

// Context implements sklog.Contexter interface.
func (er *ExistsRequest) Context() []interface{} {
	return []interface{}{"token", er.Token}
}

// Context implements sklog.Contexter interface.
func (er *CreateRequest) Context() (ctx []interface{}) {
	for key, value := range er.Data {
		ctx = append(ctx, "data_"+key, value)
	}

	return
}

// Context implements sklog.Contexter interface.
func (ar *AbandonRequest) Context() []interface{} {
	return []interface{}{
		"token", ar.Token,
	}
}

// Context implements sklog.Contexter interface.
func (sdr *SetDataRequest) Context() []interface{} {
	return []interface{}{
		"token", sdr.Token,
		"key", sdr.Key,
		"value", sdr.Value,
	}
}

// Context implements sklog.Contexter interface.
func (dr *DeleteRequest) Context() []interface{} {
	return []interface{}{
		"token", dr.Token,
		"expire_at_from", dr.ExpireAtFrom,
		"expire_at_to", dr.ExpireAtTo,
	}
}

// ExpireAtFromTime ...
func (dr *DeleteRequest) ExpireAtFromTime() time.Time {
	return TimestampToTime(dr.ExpireAtFrom)
}

// ExpireAtToTime ...
func (dr *DeleteRequest) ExpireAtToTime() time.Time {
	return TimestampToTime(dr.ExpireAtTo)
}

// SetValue ...
func (s *Session) SetValue(key, value string) {
	if s.Data == nil {
		s.Data = make(map[string]string)
	}

	s.Data[key] = value
}

// Value ...
func (s *Session) Value(key string) string {
	if s.Data == nil {
		s.Data = make(map[string]string)
	}

	return s.Data[key]
}

// ExpireAtTime ...
func (s *Session) ExpireAtTime() time.Time {
	return TimestampToTime(s.ExpireAt)
}

// ParseTime ...
func ParseTime(s string) (time.Time, error) {
	return time.Parse(time.RFC3339, s)
}

// Value implements driver.Valuer interface.
func (t Token) Value() (driver.Value, error) {
	return t.Key + ":" + t.Hash, nil
}

// Scan implements sql.Scanner interface.
func (t *Token) Scan(src interface{}) error {
	var token *Token
	var err error

	switch s := src.(type) {
	case []byte:
		token, err = NewTokenFromBytes(s)
	case string:
		token, err = NewTokenFromString(s)
	default:
		return errors.New("mnemosyne: token supports scan only from slice of bytes and string")
	}
	if err != nil {
		return err
	}

	*t = *token

	return nil
}

// NewTokenFromString parse string and allocates new token instance if ok.
func NewTokenFromString(s string) (*Token, error) {
	parts := strings.Split(s, ":")

	if len(parts) != 2 {
		return nil, errors.New("mnemosyne: token cannot be allocated, given string has wrong format")
	}

	return NewToken(parts[0], parts[1]), nil
}

// NewToken allocates new Token instance.
func NewToken(key, hash string) *Token {
	return &Token{
		Key:  key,
		Hash: hash,
	}
}

// NewTokenFromBytes ...
func NewTokenFromBytes(b []byte) (*Token, error) {
	parts := bytes.Split(b, []byte{':'})

	if len(parts) != 2 {
		return nil, errors.New("mnemosyne: token cannot be allocated, given byte slice has wrong format")
	}

	return NewToken(string(parts[0]), string(parts[1])), nil
}

// NewTokenRandom ...
func NewTokenRandom(g RandomBytesGenerator, k string) (*Token, error) {
	buf, err := g.GenerateRandomBytes(128)
	if err != nil {
		return nil, err
	}

	// A hash needs to be 64 bytes long to have 256-bit collision resistance.
	hash := make([]byte, 64)
	// Compute a 64-byte hash of buf and put it in h.
	sha3.ShakeSum256(hash, buf)

	return NewToken(k, hex.EncodeToString(hash)), nil
}
<|endoftext|>


<commit_before>package cmd

import (
	"fmt"

	"github.com/spf13/cobra"
	"github.com/voidint/gbb/build"
)

var (
	// Version 版本号
	Version = "0.6.0"
)

var versionCmd = &cobra.Command{
	Use:   "version",
	Short: "Print version information",
	Long:  ``,
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println(build.Version(fmt.Sprintf("gbb version %s", Version)))
	},
}

func init() {
	RootCmd.AddCommand(versionCmd)
}
<commit_msg>Upgrade version: 0.6.0 -> 0.6.1<commit_after>package cmd

import (
	"fmt"

	"github.com/spf13/cobra"
	"github.com/voidint/gbb/build"
)

var (
	// Version 版本号
	Version = "0.6.1"
)

var versionCmd = &cobra.Command{
	Use:   "version",
	Short: "Print version information",
	Long:  ``,
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println(build.Version(fmt.Sprintf("gbb version %s", Version)))
	},
}

func init() {
	RootCmd.AddCommand(versionCmd)
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"regexp"
	"strconv"
	"strings"
	"sync"
	"time"
)

var (
	urlRE      = regexp.MustCompile(`https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()!@:%_\+.~#?&\/\/=]*)`)
	skipStatus = flag.String("a", "", "-a 500,400")
	timeout    = flag.Duration("t", 5*time.Second, "-t 10")
	whitelist  = flag.String("w", "", "-w server1.com,server2.com")
)

const (
	OKColor      = "\033[1;34m%s\033[0m\n"
	WarningColor = "\033[1;33m%s\033[0m\n"
	ErrorColor   = "\033[1;31m%s\033[0m\n"
	DebugColor   = "\033[0;36m%s\033[0m\n"
)

func worker(wg *sync.WaitGroup, url string, skipped []int, client *http.Client) {
	defer wg.Done()

	req, err := http.NewRequest("OPTIONS", url, nil)
	if err != nil {
		log.Fatalf("error on creating request: %v", err)
	}

	resp, err := client.Do(req)
	if err != nil {
		fmt.Printf(ErrorColor, "[ERROR] "+url)
		return
	}

	shouldSkipURL := len(skipped) > 0 && isIn(resp.StatusCode, skipped)
	isError := resp.StatusCode > 400
	if !shouldSkipURL {
		statusColor := OKColor
		if isError {
			statusColor = ErrorColor
		}
		fmt.Printf(statusColor, fmt.Sprintf("[%d] %s", resp.StatusCode, url))
	}
}

func isIn(item int, items []int) bool {
	for _, i := range items {
		if i == item {
			return true
		}
	}
	return false
}

func isInStr(item string, items []string) bool {
	for _, i := range items {
		if i == item {
			return true
		}
	}
	return false
}

func main() {
	flag.Parse()

	args := flag.Args()
	if len(args) == 0 {
		log.Fatal("filename is required")
	}

	// read file
	file, err := ioutil.ReadFile(args[0])
	if err != nil {
		log.Fatalf("error on reading file: %v", err)
	}

	// validate skipStatus
	var skipped []int
	if len(*skipStatus) > 0 {
		splitted := strings.Split(*skipStatus, ",")
		for _, item := range splitted {
			val, err := strconv.Atoi(item)
			if err != nil {
				log.Fatalf("could not parse skip status value: %v \n", err)
			}
			skipped = append(skipped, val)
		}
	}

	// validate whitelist
	var whitelisted []string
	if len(*whitelist) > 0 {
		whitelisted = strings.Split(*whitelist, ",")
	}

	matches := urlRE.FindAllString(string(file), -1)
	client := &http.Client{
		Timeout: *timeout,
	}
	wg := &sync.WaitGroup{}

	for _, url := range matches {
		wg.Add(1)
		if isInStr(url, whitelisted) {
			continue
		}
		go worker(wg, url, skipped, client)
	}
	wg.Wait()
}
<commit_msg>adding workers to make concurrent requests<commit_after>package main

import (
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"regexp"
	"strconv"
	"strings"
	"sync"
	"time"
)

var (
	urlRE      = regexp.MustCompile(`https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()!@:%_\+.~#?&\/\/=]*)`)
	skipStatus = flag.String("a", "", "-a 500,400")
	timeout    = flag.Duration("t", 5*time.Second, "-t 10")
	whitelist  = flag.String("w", "", "-w server1.com,server2.com")
)

const (
	okColor      = "\033[1;34m%s\033[0m\n"
	warningColor = "\033[1;33m%s\033[0m\n"
	errorColor   = "\033[1;31m%s\033[0m\n"
	debugColor   = "\033[0;36m%s\033[0m\n"
)

type response struct {
	URL      string
	Response *http.Response
	Err      error
}

func main() {
	flag.Parse()

	args := flag.Args()
	if len(args) == 0 {
		log.Fatal("filename is required")
	}

	// read file
	file, err := ioutil.ReadFile(args[0])
	if err != nil {
		log.Fatalf("error on reading file: %v", err)
	}

	// validate skipStatus
	var skipped []int
	if len(*skipStatus) > 0 {
		splitted := strings.Split(*skipStatus, ",")
		for _, item := range splitted {
			val, err := strconv.Atoi(item)
			if err != nil {
				log.Fatalf("could not parse skip status value: %v \n", err)
			}
			skipped = append(skipped, val)
		}
	}

	// validate whitelist
	var whitelisted []string
	if len(*whitelist) > 0 {
		whitelisted = strings.Split(*whitelist, ",")
	}

	matches := urlRE.FindAllString(string(file), -1)
	client := &http.Client{
		Timeout: *timeout,
	}
	wg := &sync.WaitGroup{}

	results := make(chan *response)
	requests := make(chan string)

	// spawn workers
	for i := 0; i <= 50; i++ {
		wg.Add(1)
		go worker(wg, requests, results, client)
	}

	go func() {
		for _, url := range matches {
			if isInStr(url, whitelisted) {
				continue
			}
			requests <- url
		}
		close(requests)
		wg.Wait()
		close(results)
	}()

	for i := range results {
		shouldSkipURL := len(skipped) > 0 && isIn(i.Response.StatusCode, skipped)
		if i.Err != nil {
			fmt.Printf(errorColor, fmt.Sprintf("[ERROR] %s", i.URL))
			continue
		}

		statusColor := okColor
		if i.Response.StatusCode > 400 {
			statusColor = errorColor
		}

		if !shouldSkipURL {
			fmt.Printf(statusColor, fmt.Sprintf("[%d] %s", i.Response.StatusCode, i.URL))
		} else {
			fmt.Printf(debugColor, fmt.Sprintf("[%d] %s", i.Response.StatusCode, i.URL))
		}
	}
}

func worker(wg *sync.WaitGroup, requests <-chan string, results chan<- *response, client *http.Client) {
	wg.Done()
	for url := range requests {
		response := &response{
			URL: url,
		}
		req, err := http.NewRequest("HEAD", url, nil)
		if err != nil {
			response.Err = err
			results <- response
			continue
		}

		resp, err := client.Do(req)
		if err != nil {
			response.Err = err
			results <- response
			continue
		}

		response.Response = resp
		results <- response
	}
}

func isIn(item int, items []int) bool {
	for _, i := range items {
		if i == item {
			return true
		}
	}
	return false
}

func isInStr(item string, items []string) bool {
	for _, i := range items {
		if i == item {
			return true
		}
	}
	return false
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"

	"github.com/gonuts/commander"
	"github.com/gonuts/flag"
)

func hwaf_make_cmd_version() *commander.Command {
	cmd := &commander.Command{
		Run:       hwaf_run_cmd_version,
		UsageLine: "version",
		Short:     "print version and exit",
		Long: `
print version and exit.

ex:
 $ hwaf version
 hwaf-20121212
`,
		Flag: *flag.NewFlagSet("hwaf-version", flag.ExitOnError),
	}
	return cmd
}

func hwaf_run_cmd_version(cmd *commander.Command, args []string) {
	fmt.Printf("hwaf-20130125\n")
}

// EOF
<commit_msg>version: 20130128<commit_after>package main

import (
	"fmt"

	"github.com/gonuts/commander"
	"github.com/gonuts/flag"
)

func hwaf_make_cmd_version() *commander.Command {
	cmd := &commander.Command{
		Run:       hwaf_run_cmd_version,
		UsageLine: "version",
		Short:     "print version and exit",
		Long: `
print version and exit.

ex:
 $ hwaf version
 hwaf-20121212
`,
		Flag: *flag.NewFlagSet("hwaf-version", flag.ExitOnError),
	}
	return cmd
}

func hwaf_run_cmd_version(cmd *commander.Command, args []string) {
	fmt.Printf("hwaf-20130128\n")
}

// EOF
<|endoftext|>


<commit_before>// Copyright ©2014 The gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package stat

import (
	"math/rand"
	"testing"

	"github.com/gonum/blas/cblas"
	"github.com/gonum/floats"
	"github.com/gonum/matrix/mat64"
)

func init() {
	mat64.Register(cblas.Blas{})
}

func TestCovarianceMatrix(t *testing.T) {
	for i, test := range []struct {
		mat  mat64.Matrix
		r, c int
		x    []float64
	}{
		{
			mat: mat64.NewDense(5, 2, []float64{
				-2, -4,
				-1, 2,
				0, 0,
				1, -2,
				2, 4,
			}),
			r: 2,
			c: 2,
			x: []float64{
				2.5, 3,
				3, 10,
			},
		},
	} {
		c := CovarianceMatrix(test.mat).RawMatrix()
		if c.Rows != test.r {
			t.Errorf("%d: expected rows %d, found %d", i, test.r, c.Rows)
		}
		if c.Cols != test.c {
			t.Errorf("%d: expected cols %d, found %d", i, test.c, c.Cols)
		}
		if !floats.Equal(test.x, c.Data) {
			t.Errorf("%d: expected data %#q, found %#q", i, test.x, c.Data)
		}
	}
}

// benchmarks

func randMat(r, c int) mat64.Matrix {
	x := make([]float64, r*c)
	for i := range x {
		x[i] = rand.Float64()
	}
	return mat64.NewDense(r, c, x)
}

func benchmarkCovarianceMatrix(b *testing.B, m mat64.Matrix) {
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		CovarianceMatrix(m)
	}
}

func BenchmarkCovarianceMatrixSmallxSmall(b *testing.B) {
	// 10 * 10 elements
	x := randMat(SMALL, SMALL)
	benchmarkCovarianceMatrix(b, x)
}
func BenchmarkCovarianceMatrixSmallxMedium(b *testing.B) {
	// 10 * 1000 elements
	x := randMat(SMALL, MEDIUM)
	benchmarkCovarianceMatrix(b, x)
}

/*func BenchmarkCovarianceMatrixSmallxLarge(b *testing.B) {
	x := randMat(SMALL, LARGE)
	benchmarkCovarianceMatrix(b, x)
}
func BenchmarkCovarianceMatrixSmallxHuge(b *testing.B) {
	x := randMat(SMALL, HUGE)
	benchmarkCovarianceMatrix(b, x)
}*/

func BenchmarkCovarianceMatrixMediumxSmall(b *testing.B) {
	// 1000 * 10 elements
	x := randMat(MEDIUM, SMALL)
	benchmarkCovarianceMatrix(b, x)
}
func BenchmarkCovarianceMatrixMediumxMedium(b *testing.B) {
	// 1000 * 1000 elements
	x := randMat(MEDIUM, MEDIUM)
	benchmarkCovarianceMatrix(b, x)
}

/*func BenchmarkCovarianceMatrixMediumxLarge(b *testing.B) {
	x := randMat(MEDIUM, LARGE)
	benchmarkCovarianceMatrix(b, x)
}
func BenchmarkCovarianceMatrixMediumxHuge(b *testing.B) {
	x := randMat(MEDIUM, HUGE)
	benchmarkCovarianceMatrix(b, x)
}*/

func BenchmarkCovarianceMatrixLargexSmall(b *testing.B) {
	// 1e5 * 10 elements
	x := randMat(LARGE, SMALL)
	benchmarkCovarianceMatrix(b, x)
}

/*func BenchmarkCovarianceMatrixLargexMedium(b *testing.B) {
	x := randMat(LARGE, MEDIUM)
	benchmarkCovarianceMatrix(b, x)
}
func BenchmarkCovarianceMatrixLargexLarge(b *testing.B) {
	x := randMat(LARGE, LARGE)
	benchmarkCovarianceMatrix(b, x)
}
func BenchmarkCovarianceMatrixLargexHuge(b *testing.B) {
	x := randMat(LARGE, HUGE)
	benchmarkCovarianceMatrix(b, x)
}*/

func BenchmarkCovarianceMatrixHugexSmall(b *testing.B) {
	// 1e7 * 10 elements
	x := randMat(HUGE, SMALL)
	benchmarkCovarianceMatrix(b, x)
}

/*func BenchmarkCovarianceMatrixHugexMedium(b *testing.B) {
	x := randMat(HUGE, MEDIUM)
	benchmarkCovarianceMatrix(b, x)
}
func BenchmarkCovarianceMatrixHugexLarge(b *testing.B) {
	x := randMat(HUGE, LARGE)
	benchmarkCovarianceMatrix(b, x)
}
func BenchmarkCovarianceMatrixHugexHuge(b *testing.B) {
	x := randMat(HUGE, HUGE)
	benchmarkCovarianceMatrix(b, x)
}*/
<commit_msg>only perform shorter benchmarks<commit_after>// Copyright ©2014 The gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package stat

import (
	"math/rand"
	"testing"

	"github.com/gonum/blas/cblas"
	"github.com/gonum/floats"
	"github.com/gonum/matrix/mat64"
)

func init() {
	mat64.Register(cblas.Blas{})
}

func TestCovarianceMatrix(t *testing.T) {
	for i, test := range []struct {
		mat  mat64.Matrix
		r, c int
		x    []float64
	}{
		{
			mat: mat64.NewDense(5, 2, []float64{
				-2, -4,
				-1, 2,
				0, 0,
				1, -2,
				2, 4,
			}),
			r: 2,
			c: 2,
			x: []float64{
				2.5, 3,
				3, 10,
			},
		},
	} {
		c := CovarianceMatrix(test.mat).RawMatrix()
		if c.Rows != test.r {
			t.Errorf("%d: expected rows %d, found %d", i, test.r, c.Rows)
		}
		if c.Cols != test.c {
			t.Errorf("%d: expected cols %d, found %d", i, test.c, c.Cols)
		}
		if !floats.Equal(test.x, c.Data) {
			t.Errorf("%d: expected data %#q, found %#q", i, test.x, c.Data)
		}
	}
}

// benchmarks

func randMat(r, c int) mat64.Matrix {
	x := make([]float64, r*c)
	for i := range x {
		x[i] = rand.Float64()
	}
	return mat64.NewDense(r, c, x)
}

func benchmarkCovarianceMatrix(b *testing.B, m mat64.Matrix) {
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		CovarianceMatrix(m)
	}
}

func BenchmarkCovarianceMatrixSmallxSmall(b *testing.B) {
	// 10 * 10 elements
	x := randMat(SMALL, SMALL)
	benchmarkCovarianceMatrix(b, x)
}
func BenchmarkCovarianceMatrixSmallxMedium(b *testing.B) {
	// 10 * 1000 elements
	x := randMat(SMALL, MEDIUM)
	benchmarkCovarianceMatrix(b, x)
}

/*func BenchmarkCovarianceMatrixSmallxLarge(b *testing.B) {
	x := randMat(SMALL, LARGE)
	benchmarkCovarianceMatrix(b, x)
}
func BenchmarkCovarianceMatrixSmallxHuge(b *testing.B) {
	x := randMat(SMALL, HUGE)
	benchmarkCovarianceMatrix(b, x)
}*/

func BenchmarkCovarianceMatrixMediumxSmall(b *testing.B) {
	// 1000 * 10 elements
	x := randMat(MEDIUM, SMALL)
	benchmarkCovarianceMatrix(b, x)
}
func BenchmarkCovarianceMatrixMediumxMedium(b *testing.B) {
	// 1000 * 1000 elements
	x := randMat(MEDIUM, MEDIUM)
	benchmarkCovarianceMatrix(b, x)
}

/*func BenchmarkCovarianceMatrixMediumxLarge(b *testing.B) {
	x := randMat(MEDIUM, LARGE)
	benchmarkCovarianceMatrix(b, x)
}
func BenchmarkCovarianceMatrixMediumxHuge(b *testing.B) {
	x := randMat(MEDIUM, HUGE)
	benchmarkCovarianceMatrix(b, x)
}*/

func BenchmarkCovarianceMatrixLargexSmall(b *testing.B) {
	// 1e5 * 10 elements
	x := randMat(LARGE, SMALL)
	benchmarkCovarianceMatrix(b, x)
}

/*func BenchmarkCovarianceMatrixLargexMedium(b *testing.B) {
	// 1e5 * 1000 elements
    x := randMat(LARGE, MEDIUM)
	benchmarkCovarianceMatrix(b, x)
}
func BenchmarkCovarianceMatrixLargexLarge(b *testing.B) {
	x := randMat(LARGE, LARGE)
	benchmarkCovarianceMatrix(b, x)
}
func BenchmarkCovarianceMatrixLargexHuge(b *testing.B) {
	x := randMat(LARGE, HUGE)
	benchmarkCovarianceMatrix(b, x)
}*/

func BenchmarkCovarianceMatrixHugexSmall(b *testing.B) {
	// 1e7 * 10 elements
	x := randMat(HUGE, SMALL)
	benchmarkCovarianceMatrix(b, x)
}

/*func BenchmarkCovarianceMatrixHugexMedium(b *testing.B) {
	// 1e7 * 1000 elements
    x := randMat(HUGE, MEDIUM)
	benchmarkCovarianceMatrix(b, x)
}
func BenchmarkCovarianceMatrixHugexLarge(b *testing.B) {
	x := randMat(HUGE, LARGE)
	benchmarkCovarianceMatrix(b, x)
}
func BenchmarkCovarianceMatrixHugexHuge(b *testing.B) {
	x := randMat(HUGE, HUGE)
	benchmarkCovarianceMatrix(b, x)
}*/
<|endoftext|>


<commit_before>// Copyright 2015 Jonathan Auer. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import "html/template"

// basic page is for redirect-to-godoc-only pages
var basicPackagePageHTML string = `
<meta http-equiv="refresh" content="0; URL='{{ .Godoc }}'">
<meta name="go-import" content="{{ .ImportPath }} {{ .VCS }} {{ .Repo }}">

`
var basicPackagePage = template.Must(template.New("basicPackagePage").Parse(basicPackagePageHTML))

// detail page is human readable
var detailPackagePageHTML string = `

<meta name="go-import" content="{{ .ImportPath }} {{ .VCS }} {{ .Repo }}">
<title>{{ .Name }}{{ if .Descr }} - {{ .Descr }}{{ end }}</title>

{{ .Name }}: {{ .ImportPath }}

{{ .Descr }}

{{ .VCS }}: {{ .Repo }}

Reference

{{ if .DocURL }}See also: {{ .DocURL }}{{ end }}

`
var detailPackagePage = template.Must(template.New("detailPackagePage").Parse(detailPackagePageHTML))
<commit_msg>detail template touchup<commit_after>// Copyright 2015 Jonathan Auer. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import "html/template"

// basic page is for redirect-to-godoc-only pages
var basicPackagePageHTML string = `
<meta http-equiv="refresh" content="0; URL='{{ .Godoc }}'">
<meta name="go-import" content="{{ .ImportPath }} {{ .VCS }} {{ .Repo }}">

`
var basicPackagePage = template.Must(template.New("basicPackagePage").Parse(basicPackagePageHTML))

// detail page is human readable
var detailPackagePageHTML string = `

<meta name="go-import" content="{{ .ImportPath }} {{ .VCS }} {{ .Repo }}">
<title>{{ .Name }}{{ if .Descr }} - {{ .Descr }}{{ end }}</title>

{{ .Name }}

{{ .Descr }}

{{ .VCS }}: {{ .Repo }}

Reference

{{ if .DocURL }}See also: {{ .DocURL }}{{ end }}

`
var detailPackagePage = template.Must(template.New("detailPackagePage").Parse(detailPackagePageHTML))
<|endoftext|>


<commit_before>// Binary ptyshell sets up a listening shell on a pty.
// Connect with: socat FILE:`tty`,raw,echo=0 TCP:localhost:1234
package main

import (
	"flag"
	"io"
	"log"
	"net"
	"os/exec"

	pty "github.com/tianon/debian-golang-pty"
)

var address = flag.String("listen", ":1234", "Address to listen on ([ip]:port).")

func main() {
	ln, err := net.Listen("tcp", *address)
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("listening on %s", *address)
	for {
		conn, err := ln.Accept()
		if err != nil {
			log.Printf("[%s] accept error: %v", conn.RemoteAddr().String(), err)
			continue
		}
		go handle(conn)
	}
}

func handle(conn net.Conn) {
	remote := conn.RemoteAddr().String()
	shell, err := pty.Start(exec.Command("bash"))
	if err != nil {
		log.Printf("[%s] pty error: %v", remote, err)
		return
	}
	done := make(chan struct{})
	cp := func(dst io.Writer, src io.Reader) {
		io.Copy(dst, src)
		done <- struct{}{}
	}
	go cp(conn, shell)
	go cp(shell, conn)
	// wait for one copy to signal termination and close both socket/pty
	// this will make the other copy stop as well
	<-done
	conn.Close()
	shell.Close()
	// wait for the other copy to finish before closing channel
	<-done
	close(done)
	log.Printf("[%s] done", remote)
}
<commit_msg>misc/net/ptyshell: use kr/pty<commit_after>// Binary ptyshell sets up a listening shell on a pty.
// Connect with: socat FILE:`tty`,raw,echo=0 TCP:localhost:1234
package main

import (
	"flag"
	"io"
	"log"
	"net"
	"os/exec"

	"github.com/kr/pty"
)

var address = flag.String("listen", ":1234", "Address to listen on ([ip]:port).")

func main() {
	ln, err := net.Listen("tcp", *address)
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("listening on %s", *address)
	for {
		conn, err := ln.Accept()
		if err != nil {
			log.Printf("[%s] accept error: %v", conn.RemoteAddr().String(), err)
			continue
		}
		go handle(conn)
	}
}

func handle(conn net.Conn) {
	remote := conn.RemoteAddr().String()
	shell, err := pty.Start(exec.Command("bash"))
	if err != nil {
		log.Printf("[%s] pty error: %v", remote, err)
		return
	}
	done := make(chan struct{})
	cp := func(dst io.Writer, src io.Reader) {
		io.Copy(dst, src)
		done <- struct{}{}
	}
	go cp(conn, shell)
	go cp(shell, conn)
	// wait for one copy to signal termination and close both socket/pty
	// this will make the other copy stop as well
	<-done
	conn.Close()
	shell.Close()
	// wait for the other copy to finish before closing channel
	<-done
	close(done)
	log.Printf("[%s] done", remote)
}
<|endoftext|>


<commit_before>// Geometry Rendering
// Adapted from http://lazyfoo.net/tutorials/SDL/08_geometry_rendering/index.php

package main

import (
	"log"

	"github.com/veandco/go-sdl2/sdl"
	"github.com/veandco/go-sdl2/img"
)

const (
	screenWidth  = 640
	screenHeight = 480
)

var (
	err      error
	window   *sdl.Window
	renderer *sdl.Renderer
	texture  *sdl.Texture

	quit  bool
	event sdl.Event
)

func initSDL() error {
	err = sdl.Init(sdl.INIT_VIDEO)

	if err != nil {
		return err
	}

	window, err = sdl.CreateWindow("SDL Tutorial", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, screenWidth, screenHeight, sdl.WINDOW_SHOWN)
	if err != nil {
		return err
	}

	renderer, err = sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED)
	if err != nil {
		return err
	}

	renderer.SetDrawColor(0xFF, 0xFF, 0xFF, 0xFF)

	imgFlags := img.INIT_PNG
	imgInitResult := img.Init(imgFlags)
	if (imgInitResult & imgFlags) != imgFlags {
		return img.GetError()
	}

	return nil
}

func loadTexture(path string) (*sdl.Texture, error) {
	var newTexture *sdl.Texture

	loadedSurface, err := img.Load(path)
	if err != nil {
		return nil, err
	}

	newTexture, err = renderer.CreateTextureFromSurface(loadedSurface)
	if err != nil {
		return nil, err
	}

	loadedSurface.Free()

	return newTexture, nil
}

func loadMedia() error {
	// Nothing to load
	return nil
}

func close() {
	texture.Destroy()
	renderer.Destroy()
	window.Destroy()
	img.Quit()
	sdl.Quit()
}

func main() {
	err = initSDL()
	if err != nil {
		log.Fatal("Error initializing SDL:", err)
	}

	err = loadMedia()
	if err != nil {
		log.Fatal("Error loading Media:", err)
	}

	quit = false
	for !quit {
		for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
			switch event.(type) {
			case *sdl.QuitEvent:
				quit = true
			}
		}

		renderer.SetDrawColor(0xFF, 0xFF, 0xFF, 0xFF)
		renderer.Clear()

		fillRect := sdl.Rect{X: screenWidth / 4, Y: screenHeight / 4, W: screenWidth / 2, H: screenHeight / 2}
		renderer.SetDrawColor(0xFF, 0x00, 0x00, 0xFF)
		renderer.FillRect(&fillRect)

		outlineRect := sdl.Rect{X: screenWidth / 6, Y: screenHeight / 6, W: screenWidth * 2 / 3, H: screenHeight * 2 / 3}
		renderer.SetDrawColor(0x00, 0xFF, 0x00, 0xFF)
		renderer.DrawRect(&outlineRect)

		renderer.SetDrawColor(0x00, 0x00, 0xFF, 0xFF)
		renderer.DrawLine(0, screenHeight/2, screenWidth, screenHeight/2)

		renderer.SetDrawColor(0xFF, 0xFF, 0x00, 0xFF)
		for i := 0; i < screenHeight; i += 4 {
			renderer.DrawPoint(screenWidth/2, i)
		}

		renderer.Present()
	}

	close()
}
<commit_msg>change integer type to make example 08 build<commit_after>// Geometry Rendering
// Adapted from http://lazyfoo.net/tutorials/SDL/08_geometry_rendering/index.php

package main

import (
	"log"

	"github.com/veandco/go-sdl2/sdl"
	"github.com/veandco/go-sdl2/img"
)

const (
	screenWidth  = 640
	screenHeight = 480
)

var (
	err      error
	window   *sdl.Window
	renderer *sdl.Renderer
	texture  *sdl.Texture

	quit  bool
	event sdl.Event
)

func initSDL() error {
	err = sdl.Init(sdl.INIT_VIDEO)

	if err != nil {
		return err
	}

	window, err = sdl.CreateWindow("SDL Tutorial", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, screenWidth, screenHeight, sdl.WINDOW_SHOWN)
	if err != nil {
		return err
	}

	renderer, err = sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED)
	if err != nil {
		return err
	}

	renderer.SetDrawColor(0xFF, 0xFF, 0xFF, 0xFF)

	imgFlags := img.INIT_PNG
	imgInitResult := img.Init(imgFlags)
	if (imgInitResult & imgFlags) != imgFlags {
		return img.GetError()
	}

	return nil
}

func loadTexture(path string) (*sdl.Texture, error) {
	var newTexture *sdl.Texture

	loadedSurface, err := img.Load(path)
	if err != nil {
		return nil, err
	}

	newTexture, err = renderer.CreateTextureFromSurface(loadedSurface)
	if err != nil {
		return nil, err
	}

	loadedSurface.Free()

	return newTexture, nil
}

func loadMedia() error {
	// Nothing to load
	return nil
}

func close() {
	texture.Destroy()
	renderer.Destroy()
	window.Destroy()
	img.Quit()
	sdl.Quit()
}

func main() {
	err = initSDL()
	if err != nil {
		log.Fatal("Error initializing SDL:", err)
	}

	err = loadMedia()
	if err != nil {
		log.Fatal("Error loading Media:", err)
	}

	quit = false
	for !quit {
		for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
			switch event.(type) {
			case *sdl.QuitEvent:
				quit = true
			}
		}

		renderer.SetDrawColor(0xFF, 0xFF, 0xFF, 0xFF)
		renderer.Clear()

		fillRect := sdl.Rect{X: screenWidth / 4, Y: screenHeight / 4, W: screenWidth / 2, H: screenHeight / 2}
		renderer.SetDrawColor(0xFF, 0x00, 0x00, 0xFF)
		renderer.FillRect(&fillRect)

		outlineRect := sdl.Rect{X: screenWidth / 6, Y: screenHeight / 6, W: screenWidth * 2 / 3, H: screenHeight * 2 / 3}
		renderer.SetDrawColor(0x00, 0xFF, 0x00, 0xFF)
		renderer.DrawRect(&outlineRect)

		renderer.SetDrawColor(0x00, 0x00, 0xFF, 0xFF)
		renderer.DrawLine(0, screenHeight/2, screenWidth, screenHeight/2)

		renderer.SetDrawColor(0xFF, 0xFF, 0x00, 0xFF)
		for i := int32(0); i < screenHeight; i += 4 {
			renderer.DrawPoint(screenWidth/2, i)
		}

		renderer.Present()
	}

	close()
}
<|endoftext|>


<commit_before>package main

import (
	"os"
	"os/exec"
	"path/filepath"
	"reflect"
	"testing"
)

var sourceURITests = []struct {
	src string
	dst string
}{
	//Full URI
	{
		src: "https://github.com/sunaku/vim-unbundle",
		dst: "https://github.com/sunaku/vim-unbundle",
	},

	//Short GitHub URI
	{
		src: "Shougo/neobundle.vim",
		dst: "https://github.com/Shougo/neobundle.vim",
	},
	{
		src: "thinca/vim-quickrun",
		dst: "https://github.com/thinca/vim-quickrun",
	},

	//GitHub URI
	{
		src: "github.com/Shougo/neobundle.vim",
		dst: "https://github.com/Shougo/neobundle.vim",
	},
	{
		src: "github.com/thinca/vim-quickrun",
		dst: "https://github.com/thinca/vim-quickrun",
	},

	//Bitbucket URI
	{
		src: "bitbucket.org/anyakichi/vim-textobj-xbrackets",
		dst: "https://bitbucket.org/anyakichi/vim-textobj-xbrackets",
	},
	{
		src: "bitbucket.org/ns9tks/vim-fuzzyfinder",
		dst: "https://bitbucket.org/ns9tks/vim-fuzzyfinder",
	},
}

func TestSourceURI(t *testing.T) {
	for _, test := range sourceURITests {
		expect := test.dst
		actual := ToSourceURI(test.src)
		if actual != expect {
			t.Errorf("%q: got %q, want %q",
				test.src, actual, expect)
		}
	}
}

var destinationPathTests = []struct {
	filetype string
	src      string
	dst      string
}{
	//No filetype
	{
		filetype: "",
		src:      "https://github.com/sunaku/vim-unbundle",
		dst:      filepath.Join(dotvim, "bundle", "vim-unbundle"),
	},
	{
		filetype: "",
		src:      "sunaku/vim-unbundle",
		dst:      filepath.Join(dotvim, "bundle", "vim-unbundle"),
	},

	//Filetype specified
	{
		filetype: "go",
		src:      "https://github.com/fatih/vim-go",
		dst:      filepath.Join(dotvim, "ftbundle", "go", "vim-go"),
	},
	{
		filetype: "perl",
		src:      "https://github.com/hotchpotch/perldoc-vim",
		dst:      filepath.Join(dotvim, "ftbundle", "perl", "perldoc-vim"),
	},
}

func TestDestinationPath(t *testing.T) {
	for _, test := range destinationPathTests {
		expect := test.dst
		actual := ToDestinationPath(test.src, test.filetype)
		if actual != expect {
			t.Errorf("(uri=%q, filetype=%q): got %q, want %q",
				test.filetype, test.src, actual, expect)
		}
	}
}

func TestPackage(t *testing.T) {
	src, filetype := "sunaku/vim-unbundle", ""

	expect := &Package{
		src: "https://github.com/sunaku/vim-unbundle",
		dst: filepath.Join(dotvim, "bundle", "vim-unbundle"),
	}
	actual := NewPackage(src, filetype)
	if !reflect.DeepEqual(actual, expect) {
		t.Errorf("(uri=%q, filetype=%q): got %q, want %q",
			filetype, src, actual, expect)
	}
}

func TestInstalled(t *testing.T) {
	src, filetype := "sunaku/vim-unbundle", ""
	p := NewPackage(src, filetype)

	_, err := os.Stat(p.dst)
	expect := err == nil
	actual := p.installed()
	if actual != expect {
		t.Errorf("got %v, want %v",
			actual, expect)
	}
}
<commit_msg>Enable running test<commit_after>package main

import (
	"os"
	"path/filepath"
	"reflect"
	"testing"
)

var sourceURITests = []struct {
	src string
	dst string
}{
	//Full URI
	{
		src: "https://github.com/sunaku/vim-unbundle",
		dst: "https://github.com/sunaku/vim-unbundle",
	},

	//Short GitHub URI
	{
		src: "Shougo/neobundle.vim",
		dst: "https://github.com/Shougo/neobundle.vim",
	},
	{
		src: "thinca/vim-quickrun",
		dst: "https://github.com/thinca/vim-quickrun",
	},

	//GitHub URI
	{
		src: "github.com/Shougo/neobundle.vim",
		dst: "https://github.com/Shougo/neobundle.vim",
	},
	{
		src: "github.com/thinca/vim-quickrun",
		dst: "https://github.com/thinca/vim-quickrun",
	},

	//Bitbucket URI
	{
		src: "bitbucket.org/anyakichi/vim-textobj-xbrackets",
		dst: "https://bitbucket.org/anyakichi/vim-textobj-xbrackets",
	},
	{
		src: "bitbucket.org/ns9tks/vim-fuzzyfinder",
		dst: "https://bitbucket.org/ns9tks/vim-fuzzyfinder",
	},
}

func TestSourceURI(t *testing.T) {
	for _, test := range sourceURITests {
		expect := test.dst
		actual := ToSourceURI(test.src)
		if actual != expect {
			t.Errorf("%q: got %q, want %q",
				test.src, actual, expect)
		}
	}
}

var destinationPathTests = []struct {
	filetype string
	src      string
	dst      string
}{
	//No filetype
	{
		filetype: "",
		src:      "https://github.com/sunaku/vim-unbundle",
		dst:      filepath.Join(dotvim, "bundle", "vim-unbundle"),
	},
	{
		filetype: "",
		src:      "sunaku/vim-unbundle",
		dst:      filepath.Join(dotvim, "bundle", "vim-unbundle"),
	},

	//Filetype specified
	{
		filetype: "go",
		src:      "https://github.com/fatih/vim-go",
		dst:      filepath.Join(dotvim, "ftbundle", "go", "vim-go"),
	},
	{
		filetype: "perl",
		src:      "https://github.com/hotchpotch/perldoc-vim",
		dst:      filepath.Join(dotvim, "ftbundle", "perl", "perldoc-vim"),
	},
}

func TestDestinationPath(t *testing.T) {
	for _, test := range destinationPathTests {
		expect := test.dst
		actual := ToDestinationPath(test.src, test.filetype)
		if actual != expect {
			t.Errorf("(uri=%q, filetype=%q): got %q, want %q",
				test.filetype, test.src, actual, expect)
		}
	}
}

func TestPackage(t *testing.T) {
	src, filetype := "sunaku/vim-unbundle", ""

	expect := &Package{
		src: "https://github.com/sunaku/vim-unbundle",
		dst: filepath.Join(dotvim, "bundle", "vim-unbundle"),
	}
	actual := NewPackage(src, filetype)
	if !reflect.DeepEqual(actual, expect) {
		t.Errorf("(uri=%q, filetype=%q): got %q, want %q",
			filetype, src, actual, expect)
	}
}

func TestInstalled(t *testing.T) {
	src, filetype := "sunaku/vim-unbundle", ""
	p := NewPackage(src, filetype)

	_, err := os.Stat(p.dst)
	expect := err == nil
	actual := p.installed()
	if actual != expect {
		t.Errorf("got %v, want %v",
			actual, expect)
	}
}
<|endoftext|>


<commit_before>// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package rpcdb

import (
	"log"
	"net"
	"testing"

	"golang.org/x/net/context"

	"google.golang.org/grpc"
	"google.golang.org/grpc/test/bufconn"

	"github.com/ava-labs/avalanchego/database"
	"github.com/ava-labs/avalanchego/database/memdb"
	"github.com/ava-labs/avalanchego/database/rpcdb/rpcdbproto"
)

const (
	bufSize = 1 << 20
)

func TestInterface(t *testing.T) {
	for _, test := range database.Tests {
		listener := bufconn.Listen(bufSize)
		server := grpc.NewServer()
		rpcdbproto.RegisterDatabaseServer(server, NewServer(memdb.New()))
		go func() {
			if err := server.Serve(listener); err != nil {
				log.Fatalf("Server exited with error: %v", err)
			}
		}()

		dialer := grpc.WithContextDialer(
			func(context.Context, string) (net.Conn, error) {
				return listener.Dial()
			})

		ctx := context.Background()
		conn, err := grpc.DialContext(ctx, "", dialer, grpc.WithInsecure())
		if err != nil {
			t.Fatalf("Failed to dial: %s", err)
		}

		db := NewClient(rpcdbproto.NewDatabaseClient(conn))
		test(t, db)
		conn.Close()
	}
}

func BenchmarkInterface(b *testing.B) {
	listener := bufconn.Listen(bufSize)
	server := grpc.NewServer()
	rpcdbproto.RegisterDatabaseServer(server, NewServer(memdb.New()))
	go func() {
		if err := server.Serve(listener); err != nil {
			log.Fatalf("Server exited with error: %v", err)
		}
	}()

	dialer := grpc.WithContextDialer(
		func(context.Context, string) (net.Conn, error) {
			return listener.Dial()
		})

	ctx := context.Background()
	conn, err := grpc.DialContext(ctx, "", dialer, grpc.WithInsecure())
	if err != nil {
		b.Fatalf("Failed to dial: %s", err)
	}

	defer conn.Close()

	for _, bench := range database.Benchmarks {
		for _, size := range []int{32, 64, 128, 256, 512, 1024, 2048, 4096} {
			db := NewClient(rpcdbproto.NewDatabaseClient(conn))
			bench(b, db, "rpcdb", 1000, size)
		}
	}
}
<commit_msg>database: Potential fix for rpcdb recreation, seems heavy.<commit_after>// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package rpcdb

import (
	"log"
	"net"
	"testing"

	"golang.org/x/net/context"

	"google.golang.org/grpc"
	"google.golang.org/grpc/test/bufconn"

	"github.com/ava-labs/avalanchego/database"
	"github.com/ava-labs/avalanchego/database/memdb"
	"github.com/ava-labs/avalanchego/database/rpcdb/rpcdbproto"
)

const (
	bufSize = 1 << 20
)

func TestInterface(t *testing.T) {
	for _, test := range database.Tests {
		listener := bufconn.Listen(bufSize)
		server := grpc.NewServer()
		rpcdbproto.RegisterDatabaseServer(server, NewServer(memdb.New()))
		go func() {
			if err := server.Serve(listener); err != nil {
				log.Fatalf("Server exited with error: %v", err)
			}
		}()

		dialer := grpc.WithContextDialer(
			func(context.Context, string) (net.Conn, error) {
				return listener.Dial()
			})

		ctx := context.Background()
		conn, err := grpc.DialContext(ctx, "", dialer, grpc.WithInsecure())
		if err != nil {
			t.Fatalf("Failed to dial: %s", err)
		}

		db := NewClient(rpcdbproto.NewDatabaseClient(conn))
		test(t, db)
		conn.Close()
	}
}

func BenchmarkInterface(b *testing.B) {
	for _, bench := range database.Benchmarks {
		for _, size := range []int{32, 64, 128, 256, 512, 1024, 2048, 4096} {
			listener := bufconn.Listen(bufSize)
			server := grpc.NewServer()
			rpcdbproto.RegisterDatabaseServer(server, NewServer(memdb.New()))
			go func() {
				if err := server.Serve(listener); err != nil {
					log.Fatalf("Server exited with error: %v", err)
				}
			}()

			dialer := grpc.WithContextDialer(
				func(context.Context, string) (net.Conn, error) {
					return listener.Dial()
				})

			ctx := context.Background()
			conn, err := grpc.DialContext(ctx, "", dialer, grpc.WithInsecure())
			if err != nil {
				b.Fatalf("Failed to dial: %s", err)
			}

			db := NewClient(rpcdbproto.NewDatabaseClient(conn))

			bench(b, db, "rpcdb", 1000, size)

			conn.Close()
		}
	}
}
<|endoftext|>


<commit_before>/*
   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 instance

import (
	"encoding/json"
	"errors"
	"fmt"
	"strings"

	"github.com/percona/cloud-protocol/proto"
	"github.com/percona/percona-agent/mrms"
	"github.com/percona/percona-agent/mysql"
	"github.com/percona/percona-agent/pct"
)

type empty struct{}

type Manager struct {
	logger    *pct.Logger
	configDir string
	// --
	status   *pct.Status
	repo     *Repo
	stopChan chan empty
	mrm      mrms.Monitor
}

func NewManager(logger *pct.Logger, configDir string, api pct.APIConnector, mrm mrms.Monitor) *Manager {
	repo := NewRepo(pct.NewLogger(logger.LogChan(), "instance-repo"), configDir, api)
	m := &Manager{
		logger:    logger,
		configDir: configDir,
		// --
		status: pct.NewStatus([]string{"instance", "instance-repo"}),
		repo:   repo,
		mrm:    mrm,
	}
	return m
}

/////////////////////////////////////////////////////////////////////////////
// Interface
/////////////////////////////////////////////////////////////////////////////

// @goroutine[0]
func (m *Manager) Start() error {
	m.status.Update("instance", "Starting")
	if err := m.repo.Init(); err != nil {
		return err
	}
	fmt.Printf("Repo: %+v\n", m.Repo().List())
	m.logger.Info("Started")
	m.status.Update("instance", "Running")
	return nil
}

// @goroutine[0]
func (m *Manager) Stop() error {
	// Can't stop the instance manager.
	return nil
}

// @goroutine[0]
func (m *Manager) Handle(cmd *proto.Cmd) *proto.Reply {
	m.status.UpdateRe("instance", "Handling", cmd)
	defer m.status.Update("instance", "Running")

	it := &proto.ServiceInstance{}
	if err := json.Unmarshal(cmd.Data, it); err != nil {
		return cmd.Reply(nil, err)
	}

	switch cmd.Cmd {
	case "Add":
		err := m.repo.Add(it.Service, it.InstanceId, it.Instance, true) // true = write to disk
		return cmd.Reply(nil, err)
	case "Remove":
		err := m.repo.Remove(it.Service, it.InstanceId)
		return cmd.Reply(nil, err)
	case "GetInfo":
		info, err := m.handleGetInfo(it.Service, it.Instance)
		return cmd.Reply(info, err)
	default:
		return cmd.Reply(nil, pct.UnknownCmdError{Cmd: cmd.Cmd})
	}
}

func (m *Manager) Status() map[string]string {
	m.status.Update("instance-repo", strings.Join(m.repo.List(), " "))
	return m.status.All()
}

func (m *Manager) GetConfig() ([]proto.AgentConfig, []error) {
	return nil, nil
}

func (m *Manager) Repo() *Repo {
	return m.repo
}

/////////////////////////////////////////////////////////////////////////////
// Implementation
/////////////////////////////////////////////////////////////////////////////

func (m *Manager) handleGetInfo(service string, data []byte) (interface{}, error) {
	switch service {
	case "mysql":
		it := &proto.MySQLInstance{}
		if err := json.Unmarshal(data, it); err != nil {
			return nil, errors.New("instance.Repo:json.Unmarshal:" + err.Error())
		}
		if it.DSN == "" {
			return nil, fmt.Errorf("MySQL instance DSN is not set")
		}
		if err := GetMySQLInfo(it); err != nil {
			return nil, err
		}
		return it, nil
	default:
		return nil, fmt.Errorf("Don't know how to get info for %s service", service)
	}
}

func GetMySQLInfo(it *proto.MySQLInstance) error {
	conn := mysql.NewConnection(it.DSN)
	if err := conn.Connect(1); err != nil {
		return err
	}
	sql := "SELECT /* percona-agent */" +
		" CONCAT_WS('.', @@hostname, IF(@@port='3306',NULL,@@port)) AS Hostname," +
		" @@version_comment AS Distro," +
		" @@version AS Version"
	err := conn.DB().QueryRow(sql).Scan(
		&it.Hostname,
		&it.Distro,
		&it.Version,
	)
	if err != nil {
		return err
	}
	conn.Close()
	return nil
}
<commit_msg>PCT-562<commit_after>/*
   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 instance

import (
	"encoding/json"
	"errors"
	"fmt"
	"strings"

	"strconv"

	"github.com/percona/cloud-protocol/proto"
	"github.com/percona/percona-agent/mrms"
	"github.com/percona/percona-agent/mysql"
	"github.com/percona/percona-agent/pct"
)

type empty struct{}

type Manager struct {
	logger    *pct.Logger
	configDir string
	// --
	status   *pct.Status
	repo     *Repo
	stopChan chan empty
	mrm      mrms.Monitor
}

func NewManager(logger *pct.Logger, configDir string, api pct.APIConnector, mrm mrms.Monitor) *Manager {
	repo := NewRepo(pct.NewLogger(logger.LogChan(), "instance-repo"), configDir, api)
	m := &Manager{
		logger:    logger,
		configDir: configDir,
		// --
		status: pct.NewStatus([]string{"instance", "instance-repo"}),
		repo:   repo,
		mrm:    mrm,
	}
	return m
}

/////////////////////////////////////////////////////////////////////////////
// Interface
/////////////////////////////////////////////////////////////////////////////

// @goroutine[0]
func (m *Manager) Start() error {
	m.status.Update("instance", "Starting")
	if err := m.repo.Init(); err != nil {
		return err
	}
	fmt.Printf("Repo: %+v\n", m.Repo().List())
	m.logger.Info("Started")
	m.status.Update("instance", "Running")
	instances, err := m.getMySQLInstances()
	for _, instance := range instances {
		fmt.Printf("Instances: %+v\n", *instance)
		m.mrm.Add(instance.DSN)
	}
	return err
}

// @goroutine[0]
func (m *Manager) Stop() error {
	// Can't stop the instance manager.
	return nil
}

// @goroutine[0]
func (m *Manager) Handle(cmd *proto.Cmd) *proto.Reply {
	m.status.UpdateRe("instance", "Handling", cmd)
	defer m.status.Update("instance", "Running")

	it := &proto.ServiceInstance{}
	if err := json.Unmarshal(cmd.Data, it); err != nil {
		return cmd.Reply(nil, err)
	}

	switch cmd.Cmd {
	case "Add":
		err := m.repo.Add(it.Service, it.InstanceId, it.Instance, true) // true = write to disk
		fmt.Printf("Add: %+v\n", it)
		return cmd.Reply(nil, err)
	case "Remove":
		err := m.repo.Remove(it.Service, it.InstanceId)
		return cmd.Reply(nil, err)
	case "GetInfo":
		info, err := m.handleGetInfo(it.Service, it.Instance)
		return cmd.Reply(info, err)
	default:
		return cmd.Reply(nil, pct.UnknownCmdError{Cmd: cmd.Cmd})
	}
}

func (m *Manager) Status() map[string]string {
	m.status.Update("instance-repo", strings.Join(m.repo.List(), " "))
	return m.status.All()
}

func (m *Manager) GetConfig() ([]proto.AgentConfig, []error) {
	return nil, nil
}

func (m *Manager) Repo() *Repo {
	return m.repo
}

/////////////////////////////////////////////////////////////////////////////
// Implementation
/////////////////////////////////////////////////////////////////////////////

func (m *Manager) handleGetInfo(service string, data []byte) (interface{}, error) {
	switch service {
	case "mysql":
		it := &proto.MySQLInstance{}
		if err := json.Unmarshal(data, it); err != nil {
			return nil, errors.New("instance.Repo:json.Unmarshal:" + err.Error())
		}
		if it.DSN == "" {
			return nil, fmt.Errorf("MySQL instance DSN is not set")
		}
		if err := GetMySQLInfo(it); err != nil {
			return nil, err
		}
		return it, nil
	default:
		return nil, fmt.Errorf("Don't know how to get info for %s service", service)
	}
}

func GetMySQLInfo(it *proto.MySQLInstance) error {
	conn := mysql.NewConnection(it.DSN)
	if err := conn.Connect(1); err != nil {
		return err
	}
	sql := "SELECT /* percona-agent */" +
		" CONCAT_WS('.', @@hostname, IF(@@port='3306',NULL,@@port)) AS Hostname," +
		" @@version_comment AS Distro," +
		" @@version AS Version"
	err := conn.DB().QueryRow(sql).Scan(
		&it.Hostname,
		&it.Distro,
		&it.Version,
	)
	if err != nil {
		return err
	}
	conn.Close()
	return nil
}

func (m *Manager) getMySQLInstances() ([]*proto.MySQLInstance, error) {
	var instances []*proto.MySQLInstance
	for _, name := range m.Repo().List() {
		parts := strings.Split(name, "-") // mysql-1 or server-12
		if len(parts) != 2 {
			return nil, fmt.Errorf("Invalid instance name: %+v", name)
		}
		if parts[0] == "mysql" {
			id, err := strconv.ParseInt(parts[1], 10, 64)
			if err != nil {
				return nil, err
			}
			it := &proto.MySQLInstance{}
			err = m.Repo().Get(parts[0], uint(id), it)
			if err != nil {
				return nil, err
			}
			err = GetMySQLInfo(it)
			instances = append(instances, it)
		}
	}
	return instances, nil
}
<|endoftext|>


<commit_before>// +build experimental

package client

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

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

// PluginInstall installs a plugin
func (cli *Client) PluginInstall(ctx context.Context, name string, options types.PluginInstallOptions) error {
	// FIXME(vdemeester) name is a ref, we might want to parse/validate it here.
	query := url.Values{}
	query.Set("name", name)
	resp, err := cli.tryPluginPull(ctx, query, options.RegistryAuth)
	if resp.statusCode == http.StatusUnauthorized && options.PrivilegeFunc != nil {
		newAuthHeader, privilegeErr := options.PrivilegeFunc()
		if privilegeErr != nil {
			ensureReaderClosed(resp)
			return privilegeErr
		}
		resp, err = cli.tryPluginPull(ctx, query, newAuthHeader)
	}
	if err != nil {
		ensureReaderClosed(resp)
		return err
	}
	var privileges types.PluginPrivileges
	if err := json.NewDecoder(resp.body).Decode(&privileges); err != nil {
		return err
	}
	ensureReaderClosed(resp)

	if !options.AcceptAllPermissions && options.AcceptPermissionsFunc != nil && len(privileges) > 0 {
		accept, err := options.AcceptPermissionsFunc(privileges)
		if err != nil {
			return err
		}
		if !accept {
			resp, _ := cli.delete(ctx, "/plugins/"+name, nil, nil)
			ensureReaderClosed(resp)
			return pluginPermissionDenied{name}
		}
	}
	if options.Disabled {
		return nil
	}
	return cli.PluginEnable(ctx, name)
}

func (cli *Client) tryPluginPull(ctx context.Context, query url.Values, registryAuth string) (*serverResponse, error) {
	headers := map[string][]string{"X-Registry-Auth": {registryAuth}}
	return cli.post(ctx, "/plugins/pull", query, nil, headers)
}
<commit_msg>add ensureReaderClosed when return in PluginInstall<commit_after>// +build experimental

package client

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

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

// PluginInstall installs a plugin
func (cli *Client) PluginInstall(ctx context.Context, name string, options types.PluginInstallOptions) error {
	// FIXME(vdemeester) name is a ref, we might want to parse/validate it here.
	query := url.Values{}
	query.Set("name", name)
	resp, err := cli.tryPluginPull(ctx, query, options.RegistryAuth)
	if resp.statusCode == http.StatusUnauthorized && options.PrivilegeFunc != nil {
		newAuthHeader, privilegeErr := options.PrivilegeFunc()
		if privilegeErr != nil {
			ensureReaderClosed(resp)
			return privilegeErr
		}
		resp, err = cli.tryPluginPull(ctx, query, newAuthHeader)
	}
	if err != nil {
		ensureReaderClosed(resp)
		return err
	}
	var privileges types.PluginPrivileges
	if err := json.NewDecoder(resp.body).Decode(&privileges); err != nil {
		ensureReaderClosed(resp)
		return err
	}
	ensureReaderClosed(resp)

	if !options.AcceptAllPermissions && options.AcceptPermissionsFunc != nil && len(privileges) > 0 {
		accept, err := options.AcceptPermissionsFunc(privileges)
		if err != nil {
			return err
		}
		if !accept {
			resp, _ := cli.delete(ctx, "/plugins/"+name, nil, nil)
			ensureReaderClosed(resp)
			return pluginPermissionDenied{name}
		}
	}
	if options.Disabled {
		return nil
	}
	return cli.PluginEnable(ctx, name)
}

func (cli *Client) tryPluginPull(ctx context.Context, query url.Values, registryAuth string) (*serverResponse, error) {
	headers := map[string][]string{"X-Registry-Auth": {registryAuth}}
	return cli.post(ctx, "/plugins/pull", query, nil, headers)
}
<|endoftext|>


<commit_before>package som

import (
	"encoding/xml"
	"fmt"
	"io"
	"math"

	"github.com/gonum/matrix/mat64"
)

type rowWithDist struct {
	Row  int
	Dist float64
}

type h1 struct {
	XMLName xml.Name `xml:"h1"`
	Title   string   `xml:",innerxml"`
}

type polygon struct {
	XMLName xml.Name `xml:"polygon"`
	Points  []byte   `xml:"points,attr"`
	Style   string   `xml:"style,attr"`
}

type svgElement struct {
	XMLName  xml.Name `xml:"svg"`
	Width    float64  `xml:"width,attr"`
	Height   float64  `xml:"height,attr"`
	Polygons []polygon
}

// Creates an SVG representation of the U-Matrix of the given codebook.
// codebook is the codebook we're displaying the U-Matrix for,
// coordsDims are the dimensions of the grid,
// uShape is the shape of the grid,
// title is the title of the output SVG,
// and writer is the io.Writter to write the output SVG to.
func UMatrixSVG(codebook *mat64.Dense, coordsDims []int, uShape string, title string, writer io.Writer) error {
	xmlEncoder := xml.NewEncoder(writer)
	// array to hold the xml elements
	elems := []interface{}{h1{Title: title}}

	rows, _ := codebook.Dims()
	distMat, err := DistanceMx("euclidean", codebook)
	if err != nil {
		return err
	}
	coords, err := GridCoords(uShape, coordsDims)
	if err != nil {
		return err
	}
	coordsDistMat, err := DistanceMx("euclidean", coords)
	if err != nil {
		return err
	}

	// get maximum distance between codebook vectors
	// maximum distance will be rgb(0,0,0)
	maxDistance := mat64.Max(distMat)

	// function to scale the coord grid to something visible
	const MUL = 20.0
	const OFF = 10.0
	scale := func(x float64) float64 { return MUL*x + OFF }

	svgElem := svgElement{
		Width:    float64(coordsDims[1])*MUL + 2*OFF,
		Height:   float64(coordsDims[0])*MUL + 2*OFF,
		Polygons: make([]polygon, rows),
	}
	elems = append(elems, svgElem)
	for row := 0; row < rows; row++ {
		coord := coords.RowView(row)
		cbVec := codebook.RowView(row)
		avgDistance := 0.0
		// this is a rough approximation of the notion of neighbor grid coords
		allRowsInRadius := allRowsInRadius(row, math.Sqrt2*1.01, coordsDistMat)
		for _, rwd := range allRowsInRadius {
			if rwd.Dist > 0.0 {
				otherMu := codebook.RowView(rwd.Row)
				cbvDist, err := Distance("euclidean", cbVec, otherMu)
				if err != nil {
					return err
				}

				avgDistance += cbvDist
			}
		}
		avgDistance /= float64(len(allRowsInRadius) - 1)
		color := int((1.0 - avgDistance/maxDistance) * 255.0)
		polygonCoords := ""
		x := scale(coord.At(0, 0))
		y := scale(coord.At(1, 0))
		xOffset := 0.5 * MUL
		yOffset := 0.5 * MUL
		// hexagon has a different yOffset
		if uShape == "hexagon" {
			yOffset = math.Sqrt(0.75) / 2.0 * MUL
		}
		// draw a box around the current coord
		polygonCoords += fmt.Sprintf("%f,%f ", x+xOffset, y+yOffset)
		polygonCoords += fmt.Sprintf("%f,%f ", x+xOffset, y-yOffset)
		polygonCoords += fmt.Sprintf("%f,%f ", x-xOffset, y-yOffset)
		polygonCoords += fmt.Sprintf("%f,%f ", x-xOffset, y+yOffset)
		polygonCoords += fmt.Sprintf("%f,%f ", x+xOffset, y+yOffset)

		svgElem.Polygons[row] = polygon{
			Points: []byte(polygonCoords),
			Style:  fmt.Sprintf("fill:rgb(%d,%d,%d);stroke:black;stroke-width:1", color, color, color),
		}
	}

	xmlEncoder.Encode(elems)
	xmlEncoder.Flush()

	return nil
}

func allRowsInRadius(selectedRow int, radius float64, distMatrix *mat64.Dense) []rowWithDist {
	rowsInRadius := []rowWithDist{}
	for i, dist := range distMatrix.RowView(selectedRow).RawVector().Data {
		if dist < radius {
			rowsInRadius = append(rowsInRadius, rowWithDist{Row: i, Dist: dist})
		}
	}
	return rowsInRadius
}
<commit_msg>Shades of gray now calculated correctly<commit_after>package som

import (
	"encoding/xml"
	"fmt"
	"io"
	"math"

	"github.com/gonum/matrix/mat64"
)

type rowWithDist struct {
	Row  int
	Dist float64
}

type h1 struct {
	XMLName xml.Name `xml:"h1"`
	Title   string   `xml:",innerxml"`
}

type polygon struct {
	XMLName xml.Name `xml:"polygon"`
	Points  []byte   `xml:"points,attr"`
	Style   string   `xml:"style,attr"`
}

type svgElement struct {
	XMLName  xml.Name `xml:"svg"`
	Width    float64  `xml:"width,attr"`
	Height   float64  `xml:"height,attr"`
	Polygons []interface{}
}

// Creates an SVG representation of the U-Matrix of the given codebook.
// codebook is the codebook we're displaying the U-Matrix for,
// coordsDims are the dimensions of the grid,
// uShape is the shape of the grid,
// title is the title of the output SVG,
// and writer is the io.Writter to write the output SVG to.
func UMatrixSVG(codebook *mat64.Dense, coordsDims []int, uShape string, title string, writer io.Writer) error {
	xmlEncoder := xml.NewEncoder(writer)
	// array to hold the xml elements
	elems := []interface{}{h1{Title: title}}

	rows, _ := codebook.Dims()
	distMat, err := DistanceMx("euclidean", codebook)
	if err != nil {
		return err
	}
	coords, err := GridCoords(uShape, coordsDims)
	if err != nil {
		return err
	}
	coordsDistMat, err := DistanceMx("euclidean", coords)
	if err != nil {
		return err
	}

	umatrix := make([]float64, rows)
	maxDistance := -math.MaxFloat64
	for row := 0; row < rows; row++ {
		avgDistance := 0.0
		// this is a rough approximation of the notion of neighbor grid coords
		allRowsInRadius := allRowsInRadius(row, math.Sqrt2*1.01, coordsDistMat)
		for _, rwd := range allRowsInRadius {
			if rwd.Dist > 0.0 {
				avgDistance += distMat.At(row, rwd.Row)
			}
		}
		avgDistance /= float64(len(allRowsInRadius) - 1)
		umatrix[row] = avgDistance
		if avgDistance > maxDistance {
			maxDistance = avgDistance
		}
	}

	// function to scale the coord grid to something visible
	const MUL = 50.0
	const OFF = 10.0
	scale := func(x float64) float64 { return MUL*x + OFF }

	svgElem := svgElement{
		Width:    float64(coordsDims[1])*MUL + 2*OFF,
		Height:   float64(coordsDims[0])*MUL + 2*OFF,
		Polygons: make([]interface{}, rows),
	}
	for row := 0; row < rows; row++ {
		coord := coords.RowView(row)
		// this is here for the future when we have more colours
		colorMask := []int{255, 255, 255}
		r := int(colorMul * float64(colorMask[0]))
		g := int(colorMul * float64(colorMask[1]))
		b := int(colorMul * float64(colorMask[2]))
		polygonCoords := ""
		x := scale(coord.At(0, 0))
		y := scale(coord.At(1, 0))
		xOffset := 0.5 * MUL
		yOffset := 0.5 * MUL
		// hexagon has a different yOffset
		if uShape == "hexagon" {
			yOffset = math.Sqrt(0.75) / 2.0 * MUL
		}
		// draw a box around the current coord
		polygonCoords += fmt.Sprintf("%f,%f ", x+xOffset, y+yOffset)
		polygonCoords += fmt.Sprintf("%f,%f ", x+xOffset, y-yOffset)
		polygonCoords += fmt.Sprintf("%f,%f ", x-xOffset, y-yOffset)
		polygonCoords += fmt.Sprintf("%f,%f ", x-xOffset, y+yOffset)
		polygonCoords += fmt.Sprintf("%f,%f ", x+xOffset, y+yOffset)

		svgElem.Polygons[row] = polygon{
			Points: []byte(polygonCoords),
			Style:  fmt.Sprintf("fill:rgb(%d,%d,%d);stroke:black;stroke-width:1", r, g, b),
		}
	}

	elems = append(elems, svgElem)

	xmlEncoder.Encode(elems)
	xmlEncoder.Flush()

	return nil
}

func allRowsInRadius(selectedRow int, radius float64, distMatrix *mat64.Dense) []rowWithDist {
	rowsInRadius := []rowWithDist{}
	for i, dist := range distMatrix.RowView(selectedRow).RawVector().Data {
		if dist < radius {
			rowsInRadius = append(rowsInRadius, rowWithDist{Row: i, Dist: dist})
		}
	}
	return rowsInRadius
}
<|endoftext|>