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

package gcbench

import (
	"fmt"
	"math"
	"os"
	"reflect"
	"sort"
	"time"
)

type RunInfo struct {
	Trace GCTrace
}

type Metric struct {
	Label string
	Fn    func(RunInfo) float64
	Check func(name string, value float64)
}

var metrics = []Metric{
	{"GCs/op", gcsPerOp, nil},
	{"GCs/sec", gcsPerSec, nil},
	{"95%ile-ns/sweepTerm", distMetric(nsPerSweepTerm, 0.95), warnIf(">=", 5e6)},
	{"95%ile-ns/markTerm", distMetric(nsPerMarkTerm, 0.95), warnIf(">=", 5e6)},
	{"MB-marked/CPU/sec", markedMBPerCPUSec, nil},
	{"95%ile-heap-overshoot", distMetric(heapOvershoot, 0.95), warnIf(">", 0)},
	{"5%ile-heap-overshoot", distMetric(heapOvershoot, 0.05), warnIf("<", -.2)},
	{"95%ile-CPU-util", distMetric(cpuUtil, 0.95), warnIf(">", .5)},
}

func gcsPerOp(run RunInfo) float64 {
	t := run.Trace.WithoutForced()
	return float64(len(t))
}

func gcsPerSec(run RunInfo) float64 {
	t := run.Trace.WithoutForced()
	if len(t) == 0 {
		return 0
	}
	return float64(len(t)) / t[len(t)-1].End.Seconds()
}

func nsPerSweepTerm(run RunInfo) distribution {
	t := run.Trace.WithoutForced()
	return distribution(float64s(extract(t, "ClockSweepTerm").([]time.Duration)))
}

func nsPerMarkTerm(run RunInfo) distribution {
	t := run.Trace.WithoutForced()
	return distribution(float64s(extract(t, "ClockMarkTerm").([]time.Duration)))
}

func markedMBPerCPUSec(run RunInfo) float64 {
	t := run.Trace.WithoutForced()
	// Compute average overall rate.
	markTotal := sum(float64s(extract(t, "CPUMark").([]time.Duration)))
	markedTotal := sum(float64s(extract(t, "HeapMarked").([]Bytes)))
	return markedTotal * 1e9 / (markTotal * 1024 * 1024)
}

func heapOvershoot(run RunInfo) distribution {
	t := run.Trace.WithoutForced()
	var over distribution
	actual := extract(t, "HeapActual").([]Bytes)
	goal := extract(t, "HeapGoal").([]Bytes)
	for i := range actual {
		// Ignore very small heaps.
		if goal[i] < 10*MB {
			continue
		}
		over = append(over, float64(actual[i])/float64(goal[i])-1)
	}
	return over
}

func cpuUtil(run RunInfo) distribution {
	t := run.Trace.WithoutForced()
	var util distribution
	cpuAssist := extract(t, "CPUAssist").([]time.Duration)
	cpuBackground := extract(t, "CPUBackground").([]time.Duration)
	clockMark := extract(t, "ClockMark").([]time.Duration)
	procs := extract(t, "Procs").([]int)
	for i := range cpuAssist {
		if clockMark[i] != 0 {
			util = append(util, (float64(cpuAssist[i])+float64(cpuBackground[i]))/(float64(clockMark[i])*float64(procs[i])))
		}
	}
	return util
}

type distribution []float64

// distMetric transforms a distribution metric into a point metric at
// the specified percentile.
func distMetric(f func(RunInfo) distribution, pct float64) func(RunInfo) float64 {
	return func(run RunInfo) float64 {
		return pctile([]float64(f(run)), pct)
	}
}

// extract takes a slice []T where T is a struct and returns a slice
// of T.name.
func extract(slice interface{}, name string) interface{} {
	sv := reflect.ValueOf(slice)
	len := sv.Len()

	field, ok := sv.Type().Elem().FieldByName(name)
	if !ok {
		panic("unknown field: " + name)
	}
	out := reflect.MakeSlice(reflect.SliceOf(field.Type), len, len)
	for i := 0; i < len; i++ {
		out.Index(i).Set(sv.Index(i).FieldByIndex(field.Index))
	}
	return out.Interface()
}

var float64Type = reflect.TypeOf(float64(0.0))

// float64s converts a slice of integer or floating-point values to
// []float64.
func float64s(slice interface{}) []float64 {
	sv := reflect.ValueOf(slice)
	len := sv.Len()
	out := make([]float64, len)
	for i := 0; i < len; i++ {
		out[i] = sv.Index(i).Convert(float64Type).Float()
	}
	return out
}

func pctile(xs []float64, pct float64) float64 {
	sort.Float64s(xs)
	if len(xs) == 0 {
		return math.NaN()
	}
	return xs[int(float64(len(xs)-1)*pct)]
}

func sum(xs []float64) float64 {
	sum := 0.0
	for _, x := range xs {
		sum += x
	}
	return sum
}

// warnIf returns a metric check function that compares the metric
// value to the threshold using the given comparison and prints a
// warning if the comparison is true.
func warnIf(compare string, threshold float64) func(string, float64) {
	var fn func(a, b float64) bool
	switch compare {
	case ">":
		fn = func(a, b float64) bool { return a > b }
	case ">=":
		fn = func(a, b float64) bool { return a >= b }
	case "<=":
		fn = func(a, b float64) bool { return a <= b }
	case "<":
		fn = func(a, b float64) bool { return a < b }
	default:
		panic(fmt.Sprintf("unknown comparison operator %q", compare))
	}
	return func(name string, value float64) {
		if fn(value, threshold) {
			fmt.Fprintf(os.Stderr, "Warning: %s %s %s %s\n", sigfigs(value), name, compare, sigfigs(threshold))
		}
	}
}
<commit_msg>gcbench: add an alert for GC count<commit_after>// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package gcbench

import (
	"fmt"
	"math"
	"os"
	"reflect"
	"sort"
	"time"
)

type RunInfo struct {
	Trace GCTrace
}

type Metric struct {
	Label string
	Fn    func(RunInfo) float64
	Check func(name string, value float64)
}

var metrics = []Metric{
	{"GCs/op", gcsPerOp, warnIf("<", 5)},
	{"GCs/sec", gcsPerSec, nil},
	{"95%ile-ns/sweepTerm", distMetric(nsPerSweepTerm, 0.95), warnIf(">=", 5e6)},
	{"95%ile-ns/markTerm", distMetric(nsPerMarkTerm, 0.95), warnIf(">=", 5e6)},
	{"MB-marked/CPU/sec", markedMBPerCPUSec, nil},
	{"95%ile-heap-overshoot", distMetric(heapOvershoot, 0.95), warnIf(">", 0)},
	{"5%ile-heap-overshoot", distMetric(heapOvershoot, 0.05), warnIf("<", -.2)},
	{"95%ile-CPU-util", distMetric(cpuUtil, 0.95), warnIf(">", .5)},
}

func gcsPerOp(run RunInfo) float64 {
	t := run.Trace.WithoutForced()
	return float64(len(t))
}

func gcsPerSec(run RunInfo) float64 {
	t := run.Trace.WithoutForced()
	if len(t) == 0 {
		return 0
	}
	return float64(len(t)) / t[len(t)-1].End.Seconds()
}

func nsPerSweepTerm(run RunInfo) distribution {
	t := run.Trace.WithoutForced()
	return distribution(float64s(extract(t, "ClockSweepTerm").([]time.Duration)))
}

func nsPerMarkTerm(run RunInfo) distribution {
	t := run.Trace.WithoutForced()
	return distribution(float64s(extract(t, "ClockMarkTerm").([]time.Duration)))
}

func markedMBPerCPUSec(run RunInfo) float64 {
	t := run.Trace.WithoutForced()
	// Compute average overall rate.
	markTotal := sum(float64s(extract(t, "CPUMark").([]time.Duration)))
	markedTotal := sum(float64s(extract(t, "HeapMarked").([]Bytes)))
	return markedTotal * 1e9 / (markTotal * 1024 * 1024)
}

func heapOvershoot(run RunInfo) distribution {
	t := run.Trace.WithoutForced()
	var over distribution
	actual := extract(t, "HeapActual").([]Bytes)
	goal := extract(t, "HeapGoal").([]Bytes)
	for i := range actual {
		// Ignore very small heaps.
		if goal[i] < 10*MB {
			continue
		}
		over = append(over, float64(actual[i])/float64(goal[i])-1)
	}
	return over
}

func cpuUtil(run RunInfo) distribution {
	t := run.Trace.WithoutForced()
	var util distribution
	cpuAssist := extract(t, "CPUAssist").([]time.Duration)
	cpuBackground := extract(t, "CPUBackground").([]time.Duration)
	clockMark := extract(t, "ClockMark").([]time.Duration)
	procs := extract(t, "Procs").([]int)
	for i := range cpuAssist {
		if clockMark[i] != 0 {
			util = append(util, (float64(cpuAssist[i])+float64(cpuBackground[i]))/(float64(clockMark[i])*float64(procs[i])))
		}
	}
	return util
}

type distribution []float64

// distMetric transforms a distribution metric into a point metric at
// the specified percentile.
func distMetric(f func(RunInfo) distribution, pct float64) func(RunInfo) float64 {
	return func(run RunInfo) float64 {
		return pctile([]float64(f(run)), pct)
	}
}

// extract takes a slice []T where T is a struct and returns a slice
// of T.name.
func extract(slice interface{}, name string) interface{} {
	sv := reflect.ValueOf(slice)
	len := sv.Len()

	field, ok := sv.Type().Elem().FieldByName(name)
	if !ok {
		panic("unknown field: " + name)
	}
	out := reflect.MakeSlice(reflect.SliceOf(field.Type), len, len)
	for i := 0; i < len; i++ {
		out.Index(i).Set(sv.Index(i).FieldByIndex(field.Index))
	}
	return out.Interface()
}

var float64Type = reflect.TypeOf(float64(0.0))

// float64s converts a slice of integer or floating-point values to
// []float64.
func float64s(slice interface{}) []float64 {
	sv := reflect.ValueOf(slice)
	len := sv.Len()
	out := make([]float64, len)
	for i := 0; i < len; i++ {
		out[i] = sv.Index(i).Convert(float64Type).Float()
	}
	return out
}

func pctile(xs []float64, pct float64) float64 {
	sort.Float64s(xs)
	if len(xs) == 0 {
		return math.NaN()
	}
	return xs[int(float64(len(xs)-1)*pct)]
}

func sum(xs []float64) float64 {
	sum := 0.0
	for _, x := range xs {
		sum += x
	}
	return sum
}

// warnIf returns a metric check function that compares the metric
// value to the threshold using the given comparison and prints a
// warning if the comparison is true.
func warnIf(compare string, threshold float64) func(string, float64) {
	var fn func(a, b float64) bool
	switch compare {
	case ">":
		fn = func(a, b float64) bool { return a > b }
	case ">=":
		fn = func(a, b float64) bool { return a >= b }
	case "<=":
		fn = func(a, b float64) bool { return a <= b }
	case "<":
		fn = func(a, b float64) bool { return a < b }
	default:
		panic(fmt.Sprintf("unknown comparison operator %q", compare))
	}
	return func(name string, value float64) {
		if fn(value, threshold) {
			fmt.Fprintf(os.Stderr, "Warning: %s %s %s %s\n", sigfigs(value), name, compare, sigfigs(threshold))
		}
	}
}
<|endoftext|>


<commit_before>// +build windows

package main

import (
	"context"
	"log"

	"github.com/knq/chromedp"
	"github.com/knq/chromedp/client"
	//"github.com/knq/chromedp/runner"
)

func main() {
	var err error

	// create context
	ctxt, cancel := context.WithCancel(context.Background())
	defer cancel()

	// create edge instance -- FIXME: not able to launch separate process (yet)
	/*cdp, err := chromedp.New(ctxt, chromedp.WithRunnerOptions(
		runner.EdgeDiagnosticsAdapter(),
	))*/

	// create edge instance
	watch := client.New().WatchPageTargets(ctxt)
	if err != nil {
		log.Fatal(err)
	}
	cdp, err := chromedp.New(ctxt, chromedp.WithTargets(watch))
	if err != nil {
		log.Fatal(err)
	}

	// run task list
	var res string
	err = googleSearch(cdp, "site:brank.as", &res).Do(ctxt)
	if err != nil {
		log.Fatal(err)
	}

	log.Printf("got attributes: `%s`", res)

	// shutdown chrome
	return
	err = cdp.Shutdown(ctxt)
	if err != nil {
		log.Fatal(err)
	}

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

func googleSearch(c *chromedp.CDP, q string, res *string) chromedp.Tasks {
	return chromedp.Tasks{
		c.Navigate(`https://www.google.com`),
		c.WaitVisible(`#hplogo`),
		c.AttributeValue(`#hplogo`, "title", res, nil),
	}
}
<commit_msg>Updating edge-simple example (WIP)<commit_after>// +build windows

package main

import (
	"context"
	"fmt"
	"io/ioutil"
	"log"
	"time"

	cdp "github.com/knq/chromedp"
	cdptypes "github.com/knq/chromedp/cdp"
	"github.com/knq/chromedp/client"
)

func main() {
	var err error

	// create context
	ctxt, cancel := context.WithCancel(context.Background())
	defer cancel()

	// create edge instance -- FIXME: not able to launch separate process (yet)
	/*cdp, err := chromedp.New(ctxt, chromedp.WithRunnerOptions(
		runner.EdgeDiagnosticsAdapter(),
	))*/

	// create edge instance
	watch := client.New().WatchPageTargets(ctxt)
	c, err := chromedp.New(ctxt, chromedp.WithTargets(watch))
	if err != nil {
		log.Fatal(err)
	}

	// run task list
	var site, res string
	err = c.Run(ctxt, googleSearch("site:brank.as", "Easy Money Management", &site, &res))
	if err != nil {
		log.Fatal(err)
	}

	// shutdown chrome
	err = c.Shutdown(ctxt)
	if err != nil {
		log.Fatal(err)
	}

	// wait for chrome to finish
	err = c.Wait()
	if err != nil {
		log.Fatal(err)
	}

	log.Printf("saved screenshot of #testimonials from search result listing `%s` (%s)", res, site)
}

func googleSearch(q, text string, site, res *string) cdp.Tasks {
	var buf []byte
	sel := fmt.Sprintf(`//a[text()[contains(., '%s')]]`, text)
	return cdp.Tasks{
		cdp.Navigate(`https://www.google.com`),
		cdp.Sleep(2 * time.Second),
		cdp.WaitVisible(`#hplogo`, cdp.ByID),
		cdp.SendKeys(`#lst-ib`, q+"\n", cdp.ByID),
		cdp.WaitVisible(`#res`, cdp.ByID),
		cdp.Text(sel, res),
		cdp.Click(sel),
		cdp.Sleep(2 * time.Second),
		cdp.WaitVisible(`#footer`, cdp.ByQuery),
		cdp.WaitNotVisible(`div.v-middle > div.la-ball-clip-rotate`, cdp.ByQuery),
		cdp.Location(site),
		cdp.Screenshot(`#testimonials`, &buf, cdp.ByID),
		cdp.ActionFunc(func(context.Context, cdptypes.FrameHandler) error {
			return ioutil.WriteFile("testimonials.png", buf, 0644)
		}),
	}
}
<|endoftext|>


<commit_before>// +build !windows

package transport

import (
	"fmt"
	"os"
	"os/exec"

	"github.com/scrapli/scrapligo/logging"

	"github.com/creack/pty"
)

// System the "system" (pty subprocess wrapper) transport option for scrapligo.
type System struct {
	BaseTransportArgs   *BaseTransportArgs
	SystemTransportArgs *SystemTransportArgs
	fileObj             *os.File
	OpenCmd             []string
	ExecCmd             string
}

// SystemTransportArgs struct representing attributes required for the System transport.
type SystemTransportArgs struct {
	AuthPrivateKey    string
	AuthStrictKey     bool
	SSHConfigFile     string
	SSHKnownHostsFile string
}

func (t *System) buildOpenCmd() {
	// base ssh arguments; "ssh" itself passed in Open()
	// need to add user arguments could go here at some point
	t.OpenCmd = append(
		t.OpenCmd,
		t.BaseTransportArgs.Host,
		"-p",
		fmt.Sprintf("%d", t.BaseTransportArgs.Port),
		"-o",
		fmt.Sprintf("ConnectTimeout=%d", int(t.BaseTransportArgs.TimeoutSocket.Seconds())),
		"-o",
		fmt.Sprintf("ServerAliveInterval=%d", int(t.BaseTransportArgs.TimeoutTransport.Seconds())),
	)

	if t.SystemTransportArgs.AuthPrivateKey != "" {
		t.OpenCmd = append(
			t.OpenCmd,
			"-i",
			t.SystemTransportArgs.AuthPrivateKey,
		)
	}

	if t.BaseTransportArgs.AuthUsername != "" {
		t.OpenCmd = append(
			t.OpenCmd,
			"-l",
			t.BaseTransportArgs.AuthUsername,
		)
	}

	if !t.SystemTransportArgs.AuthStrictKey {
		t.OpenCmd = append(
			t.OpenCmd,
			"-o",
			"StrictHostKeyChecking=no",
			"-o",
			"UserKnownHostsFile=/dev/null",
		)
	} else {
		t.OpenCmd = append(
			t.OpenCmd,
			"-o",
			"StrictHostKeyChecking=yes",
		)

		if t.SystemTransportArgs.SSHKnownHostsFile != "" {
			t.OpenCmd = append(
				t.OpenCmd,
				"-o",
				fmt.Sprintf("UserKnownHostsFile=%s", t.SystemTransportArgs.SSHKnownHostsFile),
			)
		}
	}

	if t.SystemTransportArgs.SSHConfigFile != "" {
		t.OpenCmd = append(
			t.OpenCmd,
			"-F",
			t.SystemTransportArgs.SSHConfigFile,
		)
	} else {
		t.OpenCmd = append(
			t.OpenCmd,
			"-F",
			"/dev/null",
		)
	}
}

// Open open a standard ssh connection.
func (t *System) Open() error {
	if t.OpenCmd == nil {
		t.buildOpenCmd()
	}

	if t.ExecCmd == "" {
		t.ExecCmd = "ssh"
	}

	logging.LogDebug(
		t.FormatLogMessage(
			"debug",
			fmt.Sprintf(
				"\"attempting to open transport connection with the following command: %s",
				t.OpenCmd,
			),
		),
	)

	sshCommand := exec.Command(t.ExecCmd, t.OpenCmd...)
	fileObj, err := pty.StartWithSize(
		sshCommand,
		&pty.Winsize{
			Rows: uint16(t.BaseTransportArgs.PtyHeight),
			Cols: uint16(t.BaseTransportArgs.PtyWidth),
		},
	)

	if err != nil {
		logging.LogError(t.FormatLogMessage("error", "failed opening transport connection to host"))

		return err
	}

	logging.LogDebug(t.FormatLogMessage("debug", "transport connection to host opened"))

	t.fileObj = fileObj

	return err
}

// OpenNetconf open a netconf connection.
func (t *System) OpenNetconf() error {
	t.buildOpenCmd()

	t.OpenCmd = append(t.OpenCmd,
		"-tt",
		"-s",
		"netconf",
	)

	logging.LogDebug(
		t.FormatLogMessage(
			"debug",
			fmt.Sprintf(
				"\"attempting to open netconf transport connection with the following command: %s",
				t.OpenCmd,
			),
		),
	)

	sshCommand := exec.Command("ssh", t.OpenCmd...)
	fileObj, err := pty.Start(sshCommand)

	if err != nil {
		logging.LogError(
			t.FormatLogMessage("error", "failed opening netconf transport connection to host"),
		)

		return err
	}

	logging.LogDebug(t.FormatLogMessage("debug", "netconf transport connection to host opened"))

	t.fileObj = fileObj

	return err
}

// Close close the transport connection to the device.
func (t *System) Close() error {
	err := t.fileObj.Close()
	t.fileObj = nil
	logging.LogDebug(t.FormatLogMessage("debug", "transport connection to host closed"))

	return err
}

func (t *System) read(n int) *transportResult {
	b := make([]byte, n)
	_, err := t.fileObj.Read(b)

	if err != nil {
		return &transportResult{
			result: nil,
			error:  ErrTransportFailure,
		}
	}

	return &transportResult{
		result: b,
		error:  nil,
	}
}

// Read read bytes from the transport.
func (t *System) Read() ([]byte, error) {
	b, err := transportTimeout(
		*t.BaseTransportArgs.TimeoutTransport,
		t.read,
		ReadSize,
	)

	if err != nil {
		logging.LogError(t.FormatLogMessage("error", "timed out reading from transport"))
		return b, err
	}

	return b, nil
}

// ReadN read N bytes from the transport.
func (t *System) ReadN(n int) ([]byte, error) {
	b, err := transportTimeout(
		*t.BaseTransportArgs.TimeoutTransport,
		t.read,
		n,
	)

	if err != nil {
		logging.LogError(t.FormatLogMessage("error", "timed out reading from transport"))
		return b, err
	}

	return b, nil
}

// Write write bytes to the transport.
func (t *System) Write(channelInput []byte) error {
	_, err := t.fileObj.Write(channelInput)
	if err != nil {
		return err
	}

	return nil
}

// IsAlive indicate if the transport is alive or not.
func (t *System) IsAlive() bool {
	return t.fileObj != nil
}

// FormatLogMessage formats log message payload, adding contextual info about the host.
func (t *System) FormatLogMessage(level, msg string) string {
	return logging.FormatLogMessage(level, t.BaseTransportArgs.Host, t.BaseTransportArgs.Port, msg)
}
<commit_msg>fix some naming/docstrings<commit_after>// +build !windows

package transport

import (
	"fmt"
	"os"
	"os/exec"

	"github.com/scrapli/scrapligo/logging"

	"github.com/creack/pty"
)

// System the "system" (pty subprocess wrapper) transport option for scrapligo.
type System struct {
	BaseTransportArgs   *BaseTransportArgs
	SystemTransportArgs *SystemTransportArgs
	fileObj             *os.File
	OpenCmd             []string
	ExecCmd             string
}

// SystemTransportArgs struct representing attributes required for the System transport.
type SystemTransportArgs struct {
	AuthPrivateKey    string
	AuthStrictKey     bool
	SSHConfigFile     string
	SSHKnownHostsFile string
}

func (t *System) buildOpenCmd() {
	// base open command arguments; the exec command itself will be passed in Open()
	// need to add user arguments could go here at some point
	t.OpenCmd = append(
		t.OpenCmd,
		t.BaseTransportArgs.Host,
		"-p",
		fmt.Sprintf("%d", t.BaseTransportArgs.Port),
		"-o",
		fmt.Sprintf("ConnectTimeout=%d", int(t.BaseTransportArgs.TimeoutSocket.Seconds())),
		"-o",
		fmt.Sprintf("ServerAliveInterval=%d", int(t.BaseTransportArgs.TimeoutTransport.Seconds())),
	)

	if t.SystemTransportArgs.AuthPrivateKey != "" {
		t.OpenCmd = append(
			t.OpenCmd,
			"-i",
			t.SystemTransportArgs.AuthPrivateKey,
		)
	}

	if t.BaseTransportArgs.AuthUsername != "" {
		t.OpenCmd = append(
			t.OpenCmd,
			"-l",
			t.BaseTransportArgs.AuthUsername,
		)
	}

	if !t.SystemTransportArgs.AuthStrictKey {
		t.OpenCmd = append(
			t.OpenCmd,
			"-o",
			"StrictHostKeyChecking=no",
			"-o",
			"UserKnownHostsFile=/dev/null",
		)
	} else {
		t.OpenCmd = append(
			t.OpenCmd,
			"-o",
			"StrictHostKeyChecking=yes",
		)

		if t.SystemTransportArgs.SSHKnownHostsFile != "" {
			t.OpenCmd = append(
				t.OpenCmd,
				"-o",
				fmt.Sprintf("UserKnownHostsFile=%s", t.SystemTransportArgs.SSHKnownHostsFile),
			)
		}
	}

	if t.SystemTransportArgs.SSHConfigFile != "" {
		t.OpenCmd = append(
			t.OpenCmd,
			"-F",
			t.SystemTransportArgs.SSHConfigFile,
		)
	} else {
		t.OpenCmd = append(
			t.OpenCmd,
			"-F",
			"/dev/null",
		)
	}
}

// Open opens a standard connection -- typically `ssh`, but users can set the `ExecCommand` to spawn
// different types of programs such as `docker exec` or `kubectl exec`.
func (t *System) Open() error {
	if t.OpenCmd == nil {
		t.buildOpenCmd()
	}

	if t.ExecCmd == "" {
		t.ExecCmd = "ssh"
	}

	logging.LogDebug(
		t.FormatLogMessage(
			"debug",
			fmt.Sprintf(
				"\"attempting to open transport connection with the following command: %s",
				t.OpenCmd,
			),
		),
	)

	command := exec.Command(t.ExecCmd, t.OpenCmd...)
	fileObj, err := pty.StartWithSize(
		command,
		&pty.Winsize{
			Rows: uint16(t.BaseTransportArgs.PtyHeight),
			Cols: uint16(t.BaseTransportArgs.PtyWidth),
		},
	)

	if err != nil {
		logging.LogError(t.FormatLogMessage("error", "failed opening transport connection to host"))

		return err
	}

	logging.LogDebug(t.FormatLogMessage("debug", "transport connection to host opened"))

	t.fileObj = fileObj

	return err
}

// OpenNetconf opens a netconf connection.
func (t *System) OpenNetconf() error {
	t.buildOpenCmd()

	t.OpenCmd = append(t.OpenCmd,
		"-tt",
		"-s",
		"netconf",
	)

	logging.LogDebug(
		t.FormatLogMessage(
			"debug",
			fmt.Sprintf(
				"\"attempting to open netconf transport connection with the following command: %s",
				t.OpenCmd,
			),
		),
	)

	command := exec.Command("ssh", t.OpenCmd...)
	fileObj, err := pty.Start(command)

	if err != nil {
		logging.LogError(
			t.FormatLogMessage("error", "failed opening netconf transport connection to host"),
		)

		return err
	}

	logging.LogDebug(t.FormatLogMessage("debug", "netconf transport connection to host opened"))

	t.fileObj = fileObj

	return err
}

// Close closes the transport connection to the device.
func (t *System) Close() error {
	err := t.fileObj.Close()
	t.fileObj = nil
	logging.LogDebug(t.FormatLogMessage("debug", "transport connection to host closed"))

	return err
}

func (t *System) read(n int) *transportResult {
	b := make([]byte, n)
	_, err := t.fileObj.Read(b)

	if err != nil {
		return &transportResult{
			result: nil,
			error:  ErrTransportFailure,
		}
	}

	return &transportResult{
		result: b,
		error:  nil,
	}
}

// Read read bytes from the transport.
func (t *System) Read() ([]byte, error) {
	b, err := transportTimeout(
		*t.BaseTransportArgs.TimeoutTransport,
		t.read,
		ReadSize,
	)

	if err != nil {
		logging.LogError(t.FormatLogMessage("error", "timed out reading from transport"))
		return b, err
	}

	return b, nil
}

// ReadN reads N bytes from the transport.
func (t *System) ReadN(n int) ([]byte, error) {
	b, err := transportTimeout(
		*t.BaseTransportArgs.TimeoutTransport,
		t.read,
		n,
	)

	if err != nil {
		logging.LogError(t.FormatLogMessage("error", "timed out reading from transport"))
		return b, err
	}

	return b, nil
}

// Write writes bytes to the transport.
func (t *System) Write(channelInput []byte) error {
	_, err := t.fileObj.Write(channelInput)
	if err != nil {
		return err
	}

	return nil
}

// IsAlive indicates if the transport is alive or not.
func (t *System) IsAlive() bool {
	return t.fileObj != nil
}

// FormatLogMessage formats log message payload, adding contextual info about the host.
func (t *System) FormatLogMessage(level, msg string) string {
	return logging.FormatLogMessage(level, t.BaseTransportArgs.Host, t.BaseTransportArgs.Port, msg)
}
<|endoftext|>


<commit_before>package domain

// User is a type where the user attributes are stored
type User struct {
	ID          int
	Username    string
	AccessToken string
}

// Link defines the structure to the navigation links
type Link struct {
	Title string
	URL   string
}

type Repository struct {
	Name        *string `json:"name,omitempty"`
	FullName    *string `json:"full_name,omitempty"`
	Description *string `json:"description,omitempty"`
	Private     *bool   `json:"private,omitempty"`
	HTMLURL     *string `json:"html_url,omitempty"`
	CloneURL    *string `json:"clone_url,omitempty"`
	SSHURL      *string `json:"ssh_url,omitempty"`
}

type Key struct {
	ID    *int    `json:"id,omitempty"`
	Key   *string `json:"key,omitempty"`
	Title *string `json:"title,omitempty"`
	URL   *string `json:"url,omitempty"`
}
<commit_msg>Add File type in domain<commit_after>package domain

// User is a type where the user attributes are stored
type User struct {
	ID          int
	Username    string
	AccessToken string
}

// Link defines the structure to the navigation links
type Link struct {
	Title string
	URL   string
}

type Repository struct {
	Name        *string `json:"name,omitempty"`
	FullName    *string `json:"full_name,omitempty"`
	Description *string `json:"description,omitempty"`
	Private     *bool   `json:"private,omitempty"`
	HTMLURL     *string `json:"html_url,omitempty"`
	CloneURL    *string `json:"clone_url,omitempty"`
	SSHURL      *string `json:"ssh_url,omitempty"`
}

type Key struct {
	ID    *int    `json:"id,omitempty"`
	Key   *string `json:"key,omitempty"`
	Title *string `json:"title,omitempty"`
	URL   *string `json:"url,omitempty"`
}

type File struct {
	Path    string `json:"path"`
	Content []byte `json:"content"`
	Author  string `json:"author"`
	Message string `json:"message"`
	Branch  string `json:"branch,omitempty"`
	Email   string `json:"email"`
}
<|endoftext|>


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

// header.go provides generic checks of HTTP headers.
// Checks for cookie headers reside in cookie.go

package ht

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

func init() {
	RegisterCheck(&Header{})
	RegisterCheck(&FinalURL{})
	RegisterCheck(&ContentType{})
	RegisterCheck(&Redirect{})
}

// Header provides a textual test of single-valued HTTP headers.
type Header struct {
	// Header is the HTTP header to check.
	Header string

	// Condition is applied to the first header value. A zero value checks
	// for the existence of the given Header only.
	Condition `json:",omitempty"`

	// Absent indicates that no header Header shall be part of the response.
	Absent bool `json:",omitempty"`
}

// Execute implements Check's Execute method.
func (h Header) Execute(t *Test) error {
	key := http.CanonicalHeaderKey(h.Header)
	values := t.Response.Response.Header[key]
	if len(values) == 0 && h.Absent {
		return nil
	} else if len(values) == 0 && !h.Absent {
		return fmt.Errorf("header %s not received", h.Header)
	} else if len(values) > 0 && h.Absent {
		return fmt.Errorf("forbidden header %s received", h.Header)
	}
	return h.Fullfilled(values[0])
}

// Prepare implements Check's Prepare method.
func (h *Header) Prepare() error {
	return h.Condition.Compile()
}

// ----------------------------------------------------------------------------
// FinalURL

// FinalURL checks the last URL after following all redirects.
// This check is useful only for tests with Request.FollowRedirects=true
type FinalURL Condition

// Execute implements Check's Execute method.
func (f FinalURL) Execute(t *Test) error {
	if t.Response.Response == nil || t.Response.Response.Request == nil ||
		t.Response.Response.Request.URL == nil {
		return fmt.Errorf("no request URL to analyze")
	}
	return Condition(f).Fullfilled(t.Response.Response.Request.URL.String())
}

// Prepare implements Check's Prepare method.
func (f *FinalURL) Prepare() error {
	return ((*Condition)(f)).Compile()
}

// ----------------------------------------------------------------------------
// ContentType

// ContentType checks the Content-Type header.
type ContentType struct {
	// Is is the wanted content type. It may be abrevated, e.g.
	// "json" would match "application/json"
	Is string

	// Charset is an optional charset
	Charset string `json:",omitempty"`
}

// Execute implements Check's Execute method.
func (c ContentType) Execute(t *Test) error {
	if t.Response.Response == nil || t.Response.Response.Header == nil {
		return fmt.Errorf("no proper response available")
	}
	ct := t.Response.Response.Header["Content-Type"]
	if len(ct) == 0 {
		return fmt.Errorf("no Content-Type header received")
	}
	if len(ct) > 1 {
		// This is technically not a failure, but if someone sends
		// mutliple Content-Type headers something is a bit odd.
		return fmt.Errorf("received %d Content-Type headers", len(ct))
	}
	parts := strings.Split(ct[0], ";")
	got := strings.TrimSpace(parts[0])
	want := c.Is
	if strings.Index(want, "/") == -1 {
		want = "/" + want
	}
	if !strings.HasSuffix(got, want) {
		return fmt.Errorf("Content-Type is %s", ct[0])
	}

	if c.Charset != "" {
		if len(parts) < 2 {
			return fmt.Errorf("no charset in %s", ct[0])
		}
		got := strings.TrimSpace(parts[1])
		want := "charset=" + c.Charset
		if got != want {
			return fmt.Errorf("bad charset in %s", ct[0])
		}
	}

	return nil
}

// Prepare implements Check's Prepare method.
func (ContentType) Prepare() error { return nil }

// ----------------------------------------------------------------------------
// Redirect

// Redirect checks for HTTP redirections.
type Redirect struct {
	// To is matched against the Location header. It may begin with,
	// or end with contain three dots "..." which inicate that To should
	// match the end or the start or both ends of the Location header
	// value. (Note that only one occurence of "..." is supported."
	To string

	// If StatusCode is greater zero it is the required HTTP status code
	// expected in this response. If zero the valid status codes are
	// 301 (Moved Permanently), 302 (Found), 303 (See Other) and
	// 307 (Temporary Redirect)
	StatusCode int `json:",omitempty"`
}

// Execute implements Check's Execute method.
func (r Redirect) Execute(t *Test) error {
	err := []string{}

	if t.Response.Response == nil {
		return errors.New("no response to check")
	}

	sc := t.Response.Response.StatusCode
	if r.StatusCode > 0 {
		if sc != r.StatusCode {
			err = append(err, fmt.Sprintf("got status code %d", sc))
		}
	} else {
		if !(sc == 301 || sc == 302 || sc == 303 || sc == 307) {
			err = append(err, fmt.Sprintf("got status code %d", sc))
		}
	}

	if location, ok := t.Response.Response.Header["Location"]; !ok {
		err = append(err, "no Location header received")
	} else {
		if len(location) > 1 {
			err = append(err, fmt.Sprintf("got %d Location header", len(location)))
		}
		loc := location[0]
		if strings.HasPrefix(r.To, "...") {
			if !strings.HasSuffix(loc, r.To[3:]) {
				err = append(err, fmt.Sprintf("Location = %s", loc))
			}
		} else if strings.HasSuffix(r.To, "...") {
			if !strings.HasPrefix(loc, r.To[:len(r.To)-3]) {
				err = append(err, fmt.Sprintf("Location = %s", loc))
			}
		} else if i := strings.Index(r.To, "..."); i != -1 {
			a, e := r.To[:i], r.To[i+3:]
			if !(strings.HasPrefix(loc, a) && strings.HasSuffix(loc, e)) {
				err = append(err, fmt.Sprintf("Location = %s", loc))
			}
		} else if loc != r.To {
			err = append(err, fmt.Sprintf("Location = %s", loc))
		}
	}

	if len(err) > 0 {
		return errors.New(strings.Join(err, "; "))
	}

	return nil
}

// Prepare implements Check's Prepare method.
func (r Redirect) Prepare() error {
	if r.To == "" {
		return MalformedCheck{fmt.Errorf("To must not be empty")}
	}

	if r.StatusCode > 0 && (r.StatusCode < 300 || r.StatusCode > 399) {
		return MalformedCheck{fmt.Errorf("status code %d out of redirect range", r.StatusCode)}
	}
	return nil
}
<commit_msg>ht: improve redirect check<commit_after>// Copyright 2014 Volker Dobler.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// header.go provides generic checks of HTTP headers.
// Checks for cookie headers reside in cookie.go

package ht

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

func init() {
	RegisterCheck(&Header{})
	RegisterCheck(&FinalURL{})
	RegisterCheck(&ContentType{})
	RegisterCheck(&Redirect{})
}

// Header provides a textual test of single-valued HTTP headers.
type Header struct {
	// Header is the HTTP header to check.
	Header string

	// Condition is applied to the first header value. A zero value checks
	// for the existence of the given Header only.
	Condition `json:",omitempty"`

	// Absent indicates that no header Header shall be part of the response.
	Absent bool `json:",omitempty"`
}

// Execute implements Check's Execute method.
func (h Header) Execute(t *Test) error {
	key := http.CanonicalHeaderKey(h.Header)
	values := t.Response.Response.Header[key]
	if len(values) == 0 && h.Absent {
		return nil
	} else if len(values) == 0 && !h.Absent {
		return fmt.Errorf("header %s not received", h.Header)
	} else if len(values) > 0 && h.Absent {
		return fmt.Errorf("forbidden header %s received", h.Header)
	}
	return h.Fullfilled(values[0])
}

// Prepare implements Check's Prepare method.
func (h *Header) Prepare() error {
	return h.Condition.Compile()
}

// ----------------------------------------------------------------------------
// FinalURL

// FinalURL checks the last URL after following all redirects.
// This check is useful only for tests with Request.FollowRedirects=true
type FinalURL Condition

// Execute implements Check's Execute method.
func (f FinalURL) Execute(t *Test) error {
	if t.Response.Response == nil || t.Response.Response.Request == nil ||
		t.Response.Response.Request.URL == nil {
		return fmt.Errorf("no request URL to analyze")
	}
	return Condition(f).Fullfilled(t.Response.Response.Request.URL.String())
}

// Prepare implements Check's Prepare method.
func (f *FinalURL) Prepare() error {
	return ((*Condition)(f)).Compile()
}

// ----------------------------------------------------------------------------
// ContentType

// ContentType checks the Content-Type header.
type ContentType struct {
	// Is is the wanted content type. It may be abrevated, e.g.
	// "json" would match "application/json"
	Is string

	// Charset is an optional charset
	Charset string `json:",omitempty"`
}

// Execute implements Check's Execute method.
func (c ContentType) Execute(t *Test) error {
	if t.Response.Response == nil || t.Response.Response.Header == nil {
		return fmt.Errorf("no proper response available")
	}
	ct := t.Response.Response.Header["Content-Type"]
	if len(ct) == 0 {
		return fmt.Errorf("no Content-Type header received")
	}
	if len(ct) > 1 {
		// This is technically not a failure, but if someone sends
		// mutliple Content-Type headers something is a bit odd.
		return fmt.Errorf("received %d Content-Type headers", len(ct))
	}
	parts := strings.Split(ct[0], ";")
	got := strings.TrimSpace(parts[0])
	want := c.Is
	if strings.Index(want, "/") == -1 {
		want = "/" + want
	}
	if !strings.HasSuffix(got, want) {
		return fmt.Errorf("Content-Type is %s", ct[0])
	}

	if c.Charset != "" {
		if len(parts) < 2 {
			return fmt.Errorf("no charset in %s", ct[0])
		}
		got := strings.TrimSpace(parts[1])
		want := "charset=" + c.Charset
		if got != want {
			return fmt.Errorf("bad charset in %s", ct[0])
		}
	}

	return nil
}

// Prepare implements Check's Prepare method.
func (ContentType) Prepare() error { return nil }

// ----------------------------------------------------------------------------
// Redirect

// Redirect checks for a singe HTTP redirection.
//
// Note that this check cannot be used on tests with
//     Request.FollowRedirects = true
// as Redirect checks only the final response which will not be a
// redirection if redirections are followed automatically.
type Redirect struct {
	// To is matched against the Location header. It may begin with,
	// end with or contain three dots "..." which indicate that To should
	// match the end, the start or both ends of the Location header
	// value. (Note that only one occurence of "..." is supported."
	To string

	// If StatusCode is greater zero it is the required HTTP status code
	// expected in this response. If zero, the valid status codes are
	// 301 (Moved Permanently), 302 (Found), 303 (See Other) and
	// 307 (Temporary Redirect)
	StatusCode int `json:",omitempty"`
}

// Execute implements Check's Execute method.
func (r Redirect) Execute(t *Test) error {
	err := ErrorList{}

	if t.Response.Response == nil {
		return errors.New("no response to check")
	}

	sc := t.Response.Response.StatusCode
	if r.StatusCode > 0 {
		if sc != r.StatusCode {
			err = append(err, fmt.Errorf("got status code %d", sc))
		}
	} else {
		if !(sc == 301 || sc == 302 || sc == 303 || sc == 307) {
			err = append(err, fmt.Errorf("got status code %d", sc))
		}
	}

	if location, ok := t.Response.Response.Header["Location"]; !ok {
		err = append(err, fmt.Errorf("no Location header received"))
	} else {
		if len(location) > 1 {
			err = append(err, fmt.Errorf("got %d Location header", len(location)))
		}
		loc := location[0]
		if strings.HasPrefix(r.To, "...") {
			if !strings.HasSuffix(loc, r.To[3:]) {
				err = append(err, fmt.Errorf("Location = %s", loc))
			}
		} else if strings.HasSuffix(r.To, "...") {
			if !strings.HasPrefix(loc, r.To[:len(r.To)-3]) {
				err = append(err, fmt.Errorf("Location = %s", loc))
			}
		} else if i := strings.Index(r.To, "..."); i != -1 {
			a, e := r.To[:i], r.To[i+3:]
			if !(strings.HasPrefix(loc, a) && strings.HasSuffix(loc, e)) {
				err = append(err, fmt.Errorf("Location = %s", loc))
			}
		} else if loc != r.To {
			err = append(err, fmt.Errorf("Location = %s", loc))
		}
	}

	if len(err) > 0 {
		return err
	}
	return nil
}

// Prepare implements Check's Prepare method.
func (r Redirect) Prepare() error {
	if r.To == "" {
		return MalformedCheck{fmt.Errorf("To must not be empty")}
	}

	if r.StatusCode > 0 && (r.StatusCode < 300 || r.StatusCode > 399) {
		return MalformedCheck{fmt.Errorf("status code %d out of redirect range", r.StatusCode)}
	}
	return nil
}
<|endoftext|>


<commit_before>package geojson

import (
	"strconv"

	"github.com/tidwall/geojson/geo"
	"github.com/tidwall/geojson/geometry"
)

// Circle ...
type Circle struct {
	Object
	center    geometry.Point
	meters    float64
	haversine float64
	steps     int
	km        bool
	extra     *extra
}

// NewCircle returns an circle object
func NewCircle(center geometry.Point, meters float64, steps int) *Circle {
	if steps < 3 {
		steps = 3
	}
	g := new(Circle)
	g.center = center
	g.meters = meters
	g.steps = steps
	if meters <= 0 {
		g.Object = NewPoint(center)
	} else {
		meters = geo.NormalizeDistance(meters)
		var points []geometry.Point
		step := 360.0 / float64(steps)
		i := 0
		for deg := 360.0; deg > 0; deg -= step {
			lat, lon := geo.DestinationPoint(center.Y, center.X, meters, deg)
			points = append(points, geometry.Point{X: lon, Y: lat})
			i++
		}
		// TODO: account for the pole and antimerdian. In most cases only a
		// polygon is needed, but when the circle bounds passes the 90/180
		// lines, we need to create a multipolygon
		points = append(points, points[0])
		g.Object = NewPolygon(
			geometry.NewPoly(points, nil, geometry.DefaultIndexOptions),
		)
		g.haversine = geo.DistanceToHaversine(meters)
	}
	return g
}

// AppendJSON ...
func (g *Circle) AppendJSON(dst []byte) []byte {
	dst = append(dst, `{"type":"Feature","geometry":`...)
	dst = append(dst, `{"type":"Point","coordinates":[`...)
	dst = strconv.AppendFloat(dst, g.center.X, 'f', -1, 64)
	dst = append(dst, ',')
	dst = strconv.AppendFloat(dst, g.center.Y, 'f', -1, 64)
	dst = append(dst, `]},"properties":{"type":"Circle","radius":`...)
	dst = strconv.AppendFloat(dst, g.meters, 'f', -1, 64)
	dst = append(dst, `,"radius_units":"m"}}`...)
	return dst
}

// JSON ...
func (g *Circle) JSON() string {
	return string(g.AppendJSON(nil))
}

// String ...
func (g *Circle) String() string {
	return string(g.AppendJSON(nil))
}

// Meters returns the circle's radius
func (g *Circle) Meters() float64 {
	return g.meters
}

// Center returns the circle's center point
func (g *Circle) Center() geometry.Point {
	return g.center
}

// Haversine returns the haversine corresponding to circle's radius
func (g *Circle) Haversine() float64 {
	return g.haversine
}

// HaversineTo returns the haversine from a given point to circle's center
func (g *Circle) HaversineTo(p geometry.Point) float64 {
	return geo.Haversine(p.Y, p.X, g.center.Y, g.center.X)
}

// Within returns true if circle is contained inside object
func (g *Circle) Within(obj Object) bool {
	return obj.Contains(g)
}

// containsPoint returns true if circle contains a given point
func (g *Circle) containsPoint(p geometry.Point) bool {
	h := geo.Haversine(p.Y, p.X, g.center.Y, g.center.X)
	return h <= g.haversine
}

// Contains returns true if the circle contains other object
func (g *Circle) Contains(obj Object) bool {
	switch other := obj.(type) {
	case *Point:
		return g.containsPoint(other.Center())
	case *Circle:
		return other.Distance(g) < (other.meters + g.meters)
	case *LineString:
		for i := 0; i < other.base.NumPoints(); i++ {
			if !g.containsPoint(other.base.PointAt(i)) {
				return false
			}
		}
		return true
	case Collection:
		for _, p := range other.Children() {
			if !g.Contains(p) {
				return false
			}
		}
		return true
	default:
		// No simple cases, so using polygon approximation.
		return g.Object.Contains(other)
	}
}

// intersectsSegment returns true if the circle intersects a given segment
func (g *Circle) intersectsSegment(seg geometry.Segment) bool {
	start, end := seg.A, seg.B

	// These are faster checks.
	// If they succeed there's no need do complicate things.
	if g.containsPoint(start) || g.containsPoint(end) {
		return true
	}

	// Distance between start and end
	l := geo.DistanceTo(start.Y, start.X, end.Y, end.X)

	// Unit direction vector
	dx := (end.X - start.X) / l
	dy := (end.Y - start.Y) / l

	// Point of the line closest to the center
	t := dx*(g.center.X-start.X) + dy*(g.center.Y-start.Y)
	px := t*dx + start.X
	py := t*dy + start.Y
	if px < start.X || px > end.X || py < start.Y || py > end.Y {
		// closest point is outside the segment
		return false
	}

	// Distance from the closest point to the center
	return g.containsPoint(geometry.Point{X: px, Y: py})
}

// Intersects returns true the circle intersects other object
func (g *Circle) Intersects(obj Object) bool {
	switch other := obj.(type) {
	case *Point:
		return g.containsPoint(other.Center())
	case *Circle:
		return other.Distance(g) <= (other.meters + g.meters)
	case *LineString:
		for i := 0; i < other.base.NumSegments(); i++ {
			if g.intersectsSegment(other.base.SegmentAt(i)) {
				return true
			}
		}
		return false
	case Collection:
		for _, p := range other.Children() {
			if g.Intersects(p) {
				return true
			}
		}
		return false
	default:
		// No simple cases, so using polygon approximation.
		return g.Object.Intersects(obj)
	}
}
<commit_msg>Hack geojson circle.go<commit_after>package geojson

import (
	"strconv"
	"sync/atomic"
	"unsafe"

	"github.com/tidwall/geojson/geo"
	"github.com/tidwall/geojson/geometry"
)

// Circle ...
type Circle struct {
	object    *Object
	center    geometry.Point
	meters    float64
	haversine float64
	steps     int
	km        bool
	extra     *extra
}

// NewCircle returns an circle object
func NewCircle(center geometry.Point, meters float64, steps int) *Circle {
	if steps < 3 {
		steps = 3
	}
	g := new(Circle)
	g.center = center
	g.meters = meters
	g.steps = steps
	return g
}

// AppendJSON ...
func (g *Circle) AppendJSON(dst []byte) []byte {
	dst = append(dst, `{"type":"Feature","geometry":`...)
	dst = append(dst, `{"type":"Point","coordinates":[`...)
	dst = strconv.AppendFloat(dst, g.center.X, 'f', -1, 64)
	dst = append(dst, ',')
	dst = strconv.AppendFloat(dst, g.center.Y, 'f', -1, 64)
	dst = append(dst, `]},"properties":{"type":"Circle","radius":`...)
	dst = strconv.AppendFloat(dst, g.meters, 'f', -1, 64)
	dst = append(dst, `,"radius_units":"m"}}`...)
	return dst
}

// JSON ...
func (g *Circle) JSON() string {
	return string(g.AppendJSON(nil))
}

// String ...
func (g *Circle) String() string {
	return string(g.AppendJSON(nil))
}

// Meters returns the circle's radius
func (g *Circle) Meters() float64 {
	return g.meters
}

// Center returns the circle's center point
func (g *Circle) Center() geometry.Point {
	return g.center
}

// Haversine returns the haversine corresponding to circle's radius
func (g *Circle) Haversine() float64 {
	return g.haversine
}

// HaversineTo returns the haversine from a given point to circle's center
func (g *Circle) HaversineTo(p geometry.Point) float64 {
	return geo.Haversine(p.Y, p.X, g.center.Y, g.center.X)
}

// Within returns true if circle is contained inside object
func (g *Circle) Within(obj Object) bool {
	return obj.Contains(g)
}

// containsPoint returns true if circle contains a given point
func (g *Circle) containsPoint(p geometry.Point) bool {
	h := geo.Haversine(p.Y, p.X, g.center.Y, g.center.X)
	return h <= g.haversine
}

// Contains returns true if the circle contains other object
func (g *Circle) Contains(obj Object) bool {
	switch other := obj.(type) {
	case *Point:
		return g.containsPoint(other.Center())
	case *Circle:
		return other.Distance(g) < (other.meters + g.meters)
	case *LineString:
		for i := 0; i < other.base.NumPoints(); i++ {
			if !g.containsPoint(other.base.PointAt(i)) {
				return false
			}
		}
		return true
	case Collection:
		for _, p := range other.Children() {
			if !g.Contains(p) {
				return false
			}
		}
		return true
	default:
		// No simple cases, so using polygon approximation.
		return g.getObject().Contains(other)
	}
}

// intersectsSegment returns true if the circle intersects a given segment
func (g *Circle) intersectsSegment(seg geometry.Segment) bool {
	start, end := seg.A, seg.B

	// These are faster checks.
	// If they succeed there's no need do complicate things.
	if g.containsPoint(start) || g.containsPoint(end) {
		return true
	}

	// Distance between start and end
	l := geo.DistanceTo(start.Y, start.X, end.Y, end.X)

	// Unit direction vector
	dx := (end.X - start.X) / l
	dy := (end.Y - start.Y) / l

	// Point of the line closest to the center
	t := dx*(g.center.X-start.X) + dy*(g.center.Y-start.Y)
	px := t*dx + start.X
	py := t*dy + start.Y
	if px < start.X || px > end.X || py < start.Y || py > end.Y {
		// closest point is outside the segment
		return false
	}

	// Distance from the closest point to the center
	return g.containsPoint(geometry.Point{X: px, Y: py})
}

// Intersects returns true the circle intersects other object
func (g *Circle) Intersects(obj Object) bool {
	switch other := obj.(type) {
	case *Point:
		return g.containsPoint(other.Center())
	case *Circle:
		return other.Distance(g) <= (other.meters + g.meters)
	case *LineString:
		for i := 0; i < other.base.NumSegments(); i++ {
			if g.intersectsSegment(other.base.SegmentAt(i)) {
				return true
			}
		}
		return false
	case Collection:
		for _, p := range other.Children() {
			if g.Intersects(p) {
				return true
			}
		}
		return false
	default:
		// No simple cases, so using polygon approximation.
		return g.getObject().Intersects(obj)
	}
}

// Empty ...
func (g *Circle) Empty() bool {
	return false
}

// ForEach ...
func (g *Circle) ForEach(iter func(geom Object) bool) bool {
	return iter(g)
}

// NumPoints ...
func (g *Circle) NumPoints() int {
	return 1
}

// Distance ...
func (g *Circle) Distance(other Object) float64 {
	return g.getObject().Distance(other)
}

// Rect ...
func (g *Circle) Rect() geometry.Rect {
	return g.getObject().Rect()
}

// Spatial ...
func (g *Circle) Spatial() Spatial {
	return g.getObject().Spatial()
}

func (g *Circle) getObject() Object {
	for {
		object := (*Object)(atomic.LoadPointer(
			(*unsafe.Pointer)(unsafe.Pointer(&g.object))),
		)
		if object != nil {
			return *object
		}
		newObject := makeCircleObject(g.center, g.meters, g.steps)
		if atomic.CompareAndSwapPointer(
			(*unsafe.Pointer)(unsafe.Pointer(&g.object)),
			nil, unsafe.Pointer(&newObject),
		) {
			return *object
		}
	}
}

func makeCircleObject(center geometry.Point, meters float64, steps int) Object {
	if meters <= 0 {
		return NewPoint(center)
	}
	meters = geo.NormalizeDistance(meters)
	var points []geometry.Point
	step := 360.0 / float64(steps)
	i := 0
	for deg := 360.0; deg > 0; deg -= step {
		lat, lon := geo.DestinationPoint(center.Y, center.X, meters, deg)
		points = append(points, geometry.Point{X: lon, Y: lat})
		i++
	}
	// TODO: account for the pole and antimerdian. In most cases only a
	// polygon is needed, but when the circle bounds passes the 90/180
	// lines, we need to create a multipolygon
	points = append(points, points[0])
	return NewPolygon(
		geometry.NewPoly(points, nil, geometry.DefaultIndexOptions),
	)
}
<|endoftext|>


<commit_before>package toolkit

type Fault interface {
	error
	GetCode() string
	GetMessage() string
}

type Fail struct {
	Code    string
	Message string
}

func (this *Fail) GetCode() string {
	return this.Code
}

func (this *Fail) GetMessage() string {
	return this.Message
}

func (this *Fail) Error() string {
	sb := NewStrBuffer()
	if this.Code != "" {
		sb.Add("[", this.Code, "] ")
	}
	sb.Add(this.Message)
	return sb.String()
}
<commit_msg>json Fail<commit_after>package toolkit

type Fault interface {
	error
	GetCode() string
	GetMessage() string
}

type Fail struct {
	Code    string `json:"code"`
	Message string `json:"message"`
}

func (this *Fail) GetCode() string {
	return this.Code
}

func (this *Fail) GetMessage() string {
	return this.Message
}

func (this *Fail) Error() string {
	sb := NewStrBuffer()
	if this.Code != "" {
		sb.Add("[", this.Code, "] ")
	}
	sb.Add(this.Message)
	return sb.String()
}
<|endoftext|>


<commit_before>/*******************************************************************************
The MIT License (MIT)

Copyright (c) 2013-2014 Hajime Nakagami

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*******************************************************************************/

package firebirdsql

import (
	"database/sql"
	"testing"
	"time"
)

func TestBasic(t *testing.T) {
	conn, err := sql.Open("firebirdsql_createdb", "sysdba:masterkey@localhost:3050/tmp/go_test.fdb")
	if err != nil {
		t.Fatalf("Error connecting: %v", err)
	}
	var sql string
	var n int

	sql = "SELECT Count(*) FROM rdb$relations where rdb$relation_name='FOO'"
	err = conn.QueryRow(sql).Scan(&n)
	if err != nil {
		t.Fatalf("Error QueryRow: %v", err)
	}
	if n > 0 {
		conn.Exec("DROP TABLE foo")
	}

	sql = `
        CREATE TABLE foo (
            a INTEGER NOT NULL,
            b VARCHAR(30) NOT NULL UNIQUE,
            c VARCHAR(1024),
            d DECIMAL(16,3) DEFAULT -0.123,
            e DATE DEFAULT '1967-08-11',
            f TIMESTAMP DEFAULT '1967-08-11 23:45:01',
            g TIME DEFAULT '23:45:01',
            h BLOB SUB_TYPE 1,
            i DOUBLE PRECISION DEFAULT 0.0,
            j FLOAT DEFAULT 0.0,
            PRIMARY KEY (a),
            CONSTRAINT CHECK_A CHECK (a <> 0)
        )
    `
	conn.Exec(sql)
	_, err = conn.Exec("CREATE TABLE foo (a INTEGER)")
	if err == nil {
		t.Fatalf("Need metadata update error")
	}
	if err.Error() != "unsuccessful metadata update\nTable FOO already exists\n" {
		t.Fatalf("Bad message:%v", err.Error())
	}

	// 3 records insert
	conn.Exec("insert into foo(a, b, c,h) values (1, 'a', 'b','This is a memo')")
	conn.Exec("insert into foo(a, b, c, e, g, i, j) values (2, 'A', 'B', '1999-01-25', '00:00:01', 0.1, 0.1)")
	conn.Exec("insert into foo(a, b, c, e, g, i, j) values (3, 'X', 'Y', '2001-07-05', '00:01:02', 0.2, 0.2)")

	err = conn.QueryRow("select count(*) cnt from foo").Scan(&n)
	if err != nil {
		t.Fatalf("Error QueryRow: %v", err)
	}
	if n != 3 {
		t.Fatalf("Error bad record count: %v", n)
	}

	rows, err := conn.Query("select a, b, c, d, e, f, g, i, j from foo")
	var a int
	var b, c string
	var d float64
	var e time.Time
	var f time.Time
	var g time.Time
	var i float64
	var j float32

	for rows.Next() {
		rows.Scan(&a, &b, &c, &d, &e, &f, &g, &i, &j)
	}

	stmt, _ := conn.Prepare("select count(*) from foo where a=? and b=? and d=? and e=? and f=? and g=?")
	ep := time.Date(1967, 8, 11, 0, 0, 0, 0, time.UTC)
	fp := time.Date(1967, 8, 11, 23, 45, 1, 0, time.UTC)
	gp, err := time.Parse("15:04:05", "23:45:01")
	err = stmt.QueryRow(1, "a", -0.123, ep, fp, gp).Scan(&n)
	if err != nil {
		t.Fatalf("Error QueryRow: %v", err)
	}
	if n != 1 {
		t.Fatalf("Error bad record count: %v", n)
	}

	defer conn.Close()
}

func TestError(t *testing.T) {
	conn, err := sql.Open("firebirdsql_createdb", "sysdba:masterkey@localhost:3050/tmp/go_test.fdb")
	if err != nil {
		t.Fatalf("Error connecting: %v", err)
	}
	_, err = conn.Exec("incorrect sql statement")
	if err == nil || err.Error() != "Dynamic SQL Error\nSQL error code = -104\nToken unknown - line 1, column 1\nincorrect\n" {
		t.Fatalf("Incorrect error")
	}
}

/*
func TestFB3(t *testing.T) {
	conn, err := sql.Open("firebirdsql_createdb", "sysdba:masterkey@localhost:3050/tmp/go_test.fdb")
	if err != nil {
		t.Fatalf("Error connecting: %v", err)
	}
	var sql string
	var n int

	sql = "SELECT Count(*) FROM rdb$relations where rdb$relation_name='TEST_FB3'"
	err = conn.QueryRow(sql).Scan(&n)
	if err != nil {
		t.Fatalf("Error QueryRow: %v", err)
	}
	if n > 0 {
		conn.Exec("DROP TABLE test_fb3")
	}

	sql = `
        CREATE TABLE test_fb3 (
            b BOOLEAN
        )
    `
	conn.Exec(sql)
	conn.Exec("insert into test_fb3(b) values (true)")
	conn.Exec("insert into test_fb3(b) values (false)")
    var b bool
	err = conn.QueryRow("select * from test_fb3 where b is true").Scan(&b)
	if err != nil {
		t.Fatalf("Error QueryRow: %v", err)
	}
	if b != true{
		conn.Exec("Invalid boolean value")
	}
	err = conn.QueryRow("select * from test_fb3 where b is false").Scan(&b)
	if err != nil {
		t.Fatalf("Error QueryRow: %v", err)
	}
	if b != false{
		conn.Exec("Invalid boolean value")
	}

	stmt, _ := conn.Prepare("select * from test_fb3 where b=?")
	err = stmt.QueryRow(true).Scan(&b)
	if err != nil {
		t.Fatalf("Error QueryRow: %v", err)
	}
	if b != false{
		conn.Exec("Invalid boolean value")
	}

	defer conn.Close()
}
*/
<commit_msg>add test for issue2<commit_after>/*******************************************************************************
The MIT License (MIT)

Copyright (c) 2013-2014 Hajime Nakagami

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*******************************************************************************/

package firebirdsql

import (
	"database/sql"
	"testing"
	"time"
)

func TestBasic(t *testing.T) {
	conn, err := sql.Open("firebirdsql_createdb", "sysdba:masterkey@localhost:3050/tmp/go_test.fdb")
	if err != nil {
		t.Fatalf("Error connecting: %v", err)
	}
	var sql string
	var n int

	sql = "SELECT Count(*) FROM rdb$relations where rdb$relation_name='FOO'"
	err = conn.QueryRow(sql).Scan(&n)
	if err != nil {
		t.Fatalf("Error QueryRow: %v", err)
	}
	if n > 0 {
		conn.Exec("DROP TABLE foo")
	}

	sql = `
        CREATE TABLE foo (
            a INTEGER NOT NULL,
            b VARCHAR(30) NOT NULL UNIQUE,
            c VARCHAR(1024),
            d DECIMAL(16,3) DEFAULT -0.123,
            e DATE DEFAULT '1967-08-11',
            f TIMESTAMP DEFAULT '1967-08-11 23:45:01',
            g TIME DEFAULT '23:45:01',
            h BLOB SUB_TYPE 1,
            i DOUBLE PRECISION DEFAULT 0.0,
            j FLOAT DEFAULT 0.0,
            PRIMARY KEY (a),
            CONSTRAINT CHECK_A CHECK (a <> 0)
        )
    `
	conn.Exec(sql)
	_, err = conn.Exec("CREATE TABLE foo (a INTEGER)")
	if err == nil {
		t.Fatalf("Need metadata update error")
	}
	if err.Error() != "unsuccessful metadata update\nTable FOO already exists\n" {
		t.Fatalf("Bad message:%v", err.Error())
	}

	// 3 records insert
	conn.Exec("insert into foo(a, b, c,h) values (1, 'a', 'b','This is a memo')")
	conn.Exec("insert into foo(a, b, c, e, g, i, j) values (2, 'A', 'B', '1999-01-25', '00:00:01', 0.1, 0.1)")
	conn.Exec("insert into foo(a, b, c, e, g, i, j) values (3, 'X', 'Y', '2001-07-05', '00:01:02', 0.2, 0.2)")

	err = conn.QueryRow("select count(*) cnt from foo").Scan(&n)
	if err != nil {
		t.Fatalf("Error QueryRow: %v", err)
	}
	if n != 3 {
		t.Fatalf("Error bad record count: %v", n)
	}

	rows, err := conn.Query("select a, b, c, d, e, f, g, i, j from foo")
	var a int
	var b, c string
	var d float64
	var e time.Time
	var f time.Time
	var g time.Time
	var i float64
	var j float32

	for rows.Next() {
		rows.Scan(&a, &b, &c, &d, &e, &f, &g, &i, &j)
	}

	stmt, _ := conn.Prepare("select count(*) from foo where a=? and b=? and d=? and e=? and f=? and g=?")
	ep := time.Date(1967, 8, 11, 0, 0, 0, 0, time.UTC)
	fp := time.Date(1967, 8, 11, 23, 45, 1, 0, time.UTC)
	gp, err := time.Parse("15:04:05", "23:45:01")
	err = stmt.QueryRow(1, "a", -0.123, ep, fp, gp).Scan(&n)
	if err != nil {
		t.Fatalf("Error QueryRow: %v", err)
	}
	if n != 1 {
		t.Fatalf("Error bad record count: %v", n)
	}

	defer conn.Close()
}

func TestIssue2(t *testing.T) {
	conn, _ := sql.Open("firebirdsql_createdb", "sysdba:masterkey@localhost:3050/tmp/go_test.fdb")

	conn.Exec(`
        CREATE TABLE test_issue2
         (f1 integer NOT NULL,
          f2 integer,
          f3 integer NOT NULL,
          f4 integer NOT NULL,
          f5 integer NOT NULL,
          f6 integer NOT NULL,
          f7 varchar(255) NOT NULL,
          f8 varchar(255) NOT NULL,
          f9 varchar(255) NOT NULL,
          f10 varchar(255) NOT NULL,
          f11 varchar(255) NOT NULL,
          f12 varchar(255) NOT NULL,
          f13 varchar(255) NOT NULL,
          f14 varchar(255) NOT NULL,
          f15 integer,
          f16 integer,
          f17 integer,
          f18 integer,
          f19 integer,
          f20 integer,
          f21 integer,
          f22 varchar(1),
          f23 varchar(255),
          f24 integer,
          f25 varchar(64),
          f26 integer)`)
	conn.Exec(`
        INSERT INTO test_issue2 VALUES
        (1, 2, 3, 4, 5, 6, '7', '8', '9', '10', '11', '12', '13', '14',
          15, 16, 17, 18, 19, 20, 21, 'A', '23', 24, '25', '26')`)

	rows, err := conn.Query("SELECT * FROM test_issue2")
	if err != nil {
		t.Fatalf("Error Query: %v", err)
	}
	for rows.Next() {
	}
}

func TestError(t *testing.T) {
	conn, err := sql.Open("firebirdsql_createdb", "sysdba:masterkey@localhost:3050/tmp/go_test.fdb")
	if err != nil {
		t.Fatalf("Error connecting: %v", err)
	}
	_, err = conn.Exec("incorrect sql statement")
	if err == nil || err.Error() != "Dynamic SQL Error\nSQL error code = -104\nToken unknown - line 1, column 1\nincorrect\n" {
		t.Fatalf("Incorrect error")
	}
}

/*
func TestFB3(t *testing.T) {
	conn, err := sql.Open("firebirdsql_createdb", "sysdba:masterkey@localhost:3050/tmp/go_test.fdb")
	if err != nil {
		t.Fatalf("Error connecting: %v", err)
	}
	var sql string
	var n int

	sql = "SELECT Count(*) FROM rdb$relations where rdb$relation_name='TEST_FB3'"
	err = conn.QueryRow(sql).Scan(&n)
	if err != nil {
		t.Fatalf("Error QueryRow: %v", err)
	}
	if n > 0 {
		conn.Exec("DROP TABLE test_fb3")
	}

	sql = `
        CREATE TABLE test_fb3 (
            b BOOLEAN
        )
    `
	conn.Exec(sql)
	conn.Exec("insert into test_fb3(b) values (true)")
	conn.Exec("insert into test_fb3(b) values (false)")
    var b bool
	err = conn.QueryRow("select * from test_fb3 where b is true").Scan(&b)
	if err != nil {
		t.Fatalf("Error QueryRow: %v", err)
	}
	if b != true{
		conn.Exec("Invalid boolean value")
	}
	err = conn.QueryRow("select * from test_fb3 where b is false").Scan(&b)
	if err != nil {
		t.Fatalf("Error QueryRow: %v", err)
	}
	if b != false{
		conn.Exec("Invalid boolean value")
	}

	stmt, _ := conn.Prepare("select * from test_fb3 where b=?")
	err = stmt.QueryRow(true).Scan(&b)
	if err != nil {
		t.Fatalf("Error QueryRow: %v", err)
	}
	if b != false{
		conn.Exec("Invalid boolean value")
	}

	defer conn.Close()
}
*/
<|endoftext|>


<commit_before>package azurerm

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

	"strings"

	"github.com/Azure/azure-sdk-for-go/arm/cdn"
	"github.com/hashicorp/terraform/helper/schema"
)

func resourceArmCdnProfile() *schema.Resource {
	return &schema.Resource{
		Create: resourceArmCdnProfileCreate,
		Read:   resourceArmCdnProfileRead,
		Update: resourceArmCdnProfileUpdate,
		Delete: resourceArmCdnProfileDelete,
		Importer: &schema.ResourceImporter{
			State: schema.ImportStatePassthrough,
		},

		Schema: map[string]*schema.Schema{
			"name": {
				Type:     schema.TypeString,
				Required: true,
				ForceNew: true,
			},

			"location": locationSchema(),

			"resource_group_name": {
				Type:     schema.TypeString,
				Required: true,
				ForceNew: true,
			},

			"sku": {
				Type:         schema.TypeString,
				Required:     true,
				ForceNew:     true,
				ValidateFunc: validateCdnProfileSku,
			},

			"tags": tagsSchema(),
		},
	}
}

func resourceArmCdnProfileCreate(d *schema.ResourceData, meta interface{}) error {
	client := meta.(*ArmClient)
	cdnProfilesClient := client.cdnProfilesClient

	log.Printf("[INFO] preparing arguments for Azure ARM CDN Profile creation.")

	name := d.Get("name").(string)
	location := d.Get("location").(string)
	resGroup := d.Get("resource_group_name").(string)
	sku := d.Get("sku").(string)
	tags := d.Get("tags").(map[string]interface{})

	cdnProfile := cdn.Profile{
		Location: &location,
		Tags:     expandTags(tags),
		Sku: &cdn.Sku{
			Name: cdn.SkuName(sku),
		},
	}

	_, err := cdnProfilesClient.Create(resGroup, name, cdnProfile, make(chan struct{}))
	if err != nil {
		return err
	}

	read, err := cdnProfilesClient.Get(resGroup, name)
	if err != nil {
		return err
	}
	if read.ID == nil {
		return fmt.Errorf("Cannot read CND Profile %s (resource group %s) ID", name, resGroup)
	}

	d.SetId(*read.ID)

	return resourceArmCdnProfileRead(d, meta)
}

func resourceArmCdnProfileRead(d *schema.ResourceData, meta interface{}) error {
	cdnProfilesClient := meta.(*ArmClient).cdnProfilesClient

	id, err := parseAzureResourceID(d.Id())
	if err != nil {
		return err
	}
	resGroup := id.ResourceGroup
	name := id.Path["profiles"]

	resp, err := cdnProfilesClient.Get(resGroup, name)
	if err != nil {
		if resp.StatusCode == http.StatusNotFound {
			d.SetId("")
			return nil
		}
		return fmt.Errorf("Error making Read request on Azure CDN Profile %s: %s", name, err)
	}

	d.Set("name", name)
	d.Set("resource_group_name", resGroup)
	d.Set("location", azureRMNormalizeLocation(*resp.Location))

	if resp.Sku != nil {
		d.Set("sku", string(resp.Sku.Name))
	}

	flattenAndSetTags(d, resp.Tags)

	return nil
}

func resourceArmCdnProfileUpdate(d *schema.ResourceData, meta interface{}) error {
	cdnProfilesClient := meta.(*ArmClient).cdnProfilesClient

	if !d.HasChange("tags") {
		return nil
	}

	name := d.Get("name").(string)
	resGroup := d.Get("resource_group_name").(string)
	newTags := d.Get("tags").(map[string]interface{})

	props := cdn.ProfileUpdateParameters{
		Tags: expandTags(newTags),
	}

	_, err := cdnProfilesClient.Update(resGroup, name, props, make(chan struct{}))
	if err != nil {
		return fmt.Errorf("Error issuing Azure ARM update request to update CDN Profile %q: %s", name, err)
	}

	return resourceArmCdnProfileRead(d, meta)
}

func resourceArmCdnProfileDelete(d *schema.ResourceData, meta interface{}) error {
	cdnProfilesClient := meta.(*ArmClient).cdnProfilesClient

	id, err := parseAzureResourceID(d.Id())
	if err != nil {
		return err
	}
	resGroup := id.ResourceGroup
	name := id.Path["profiles"]

	_, err = cdnProfilesClient.Delete(resGroup, name, make(chan struct{}))
	// TODO: check the status code

	return err
}

func validateCdnProfileSku(v interface{}, k string) (ws []string, errors []error) {
	value := strings.ToLower(v.(string))
	skus := map[string]bool{
		"standard_akamai":  true,
		"premium_verizon":  true,
		"standard_verizon": true,
	}

	if !skus[value] {
		errors = append(errors, fmt.Errorf("CDN Profile SKU can only be Premium_Verizon, Standard_Verizon or Standard_Akamai"))
	}
	return
}
<commit_msg>New Provider: OpsGenie (#11012)<commit_after>package azurerm

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

	"strings"

	"github.com/Azure/azure-sdk-for-go/arm/cdn"
	"github.com/hashicorp/terraform/helper/schema"
)

func resourceArmCdnProfile() *schema.Resource {
	return &schema.Resource{
		Create: resourceArmCdnProfileCreate,
		Read:   resourceArmCdnProfileRead,
		Update: resourceArmCdnProfileUpdate,
		Delete: resourceArmCdnProfileDelete,
		Importer: &schema.ResourceImporter{
			State: schema.ImportStatePassthrough,
		},

		Schema: map[string]*schema.Schema{
			"name": {
				Type:     schema.TypeString,
				Required: true,
				ForceNew: true,
			},

			"location": locationSchema(),

			"resource_group_name": {
				Type:     schema.TypeString,
				Required: true,
				ForceNew: true,
			},

			"sku": {
				Type:         schema.TypeString,
				Required:     true,
				ForceNew:     true,
				ValidateFunc: validateCdnProfileSku,
			},

			"tags": tagsSchema(),
		},
	}
}

func resourceArmCdnProfileCreate(d *schema.ResourceData, meta interface{}) error {
	client := meta.(*ArmClient)
	cdnProfilesClient := client.cdnProfilesClient

	log.Printf("[INFO] preparing arguments for Azure ARM CDN Profile creation.")

	name := d.Get("name").(string)
	location := d.Get("location").(string)
	resGroup := d.Get("resource_group_name").(string)
	sku := d.Get("sku").(string)
	tags := d.Get("tags").(map[string]interface{})

	cdnProfile := cdn.Profile{
		Location: &location,
		Tags:     expandTags(tags),
		Sku: &cdn.Sku{
			Name: cdn.SkuName(sku),
		},
	}

	_, err := cdnProfilesClient.Create(resGroup, name, cdnProfile, make(chan struct{}))
	if err != nil {
		return err
	}

	read, err := cdnProfilesClient.Get(resGroup, name)
	if err != nil {
		return err
	}
	if read.ID == nil {
		return fmt.Errorf("Cannot read CDN Profile %s (resource group %s) ID", name, resGroup)
	}

	d.SetId(*read.ID)

	return resourceArmCdnProfileRead(d, meta)
}

func resourceArmCdnProfileRead(d *schema.ResourceData, meta interface{}) error {
	cdnProfilesClient := meta.(*ArmClient).cdnProfilesClient

	id, err := parseAzureResourceID(d.Id())
	if err != nil {
		return err
	}
	resGroup := id.ResourceGroup
	name := id.Path["profiles"]

	resp, err := cdnProfilesClient.Get(resGroup, name)
	if err != nil {
		if resp.StatusCode == http.StatusNotFound {
			d.SetId("")
			return nil
		}
		return fmt.Errorf("Error making Read request on Azure CDN Profile %s: %s", name, err)
	}

	d.Set("name", name)
	d.Set("resource_group_name", resGroup)
	d.Set("location", azureRMNormalizeLocation(*resp.Location))

	if resp.Sku != nil {
		d.Set("sku", string(resp.Sku.Name))
	}

	flattenAndSetTags(d, resp.Tags)

	return nil
}

func resourceArmCdnProfileUpdate(d *schema.ResourceData, meta interface{}) error {
	cdnProfilesClient := meta.(*ArmClient).cdnProfilesClient

	if !d.HasChange("tags") {
		return nil
	}

	name := d.Get("name").(string)
	resGroup := d.Get("resource_group_name").(string)
	newTags := d.Get("tags").(map[string]interface{})

	props := cdn.ProfileUpdateParameters{
		Tags: expandTags(newTags),
	}

	_, err := cdnProfilesClient.Update(resGroup, name, props, make(chan struct{}))
	if err != nil {
		return fmt.Errorf("Error issuing Azure ARM update request to update CDN Profile %q: %s", name, err)
	}

	return resourceArmCdnProfileRead(d, meta)
}

func resourceArmCdnProfileDelete(d *schema.ResourceData, meta interface{}) error {
	cdnProfilesClient := meta.(*ArmClient).cdnProfilesClient

	id, err := parseAzureResourceID(d.Id())
	if err != nil {
		return err
	}
	resGroup := id.ResourceGroup
	name := id.Path["profiles"]

	_, err = cdnProfilesClient.Delete(resGroup, name, make(chan struct{}))
	// TODO: check the status code

	return err
}

func validateCdnProfileSku(v interface{}, k string) (ws []string, errors []error) {
	value := strings.ToLower(v.(string))
	skus := map[string]bool{
		"standard_akamai":  true,
		"premium_verizon":  true,
		"standard_verizon": true,
	}

	if !skus[value] {
		errors = append(errors, fmt.Errorf("CDN Profile SKU can only be Premium_Verizon, Standard_Verizon or Standard_Akamai"))
	}
	return
}
<|endoftext|>


<commit_before>// Copyright 2020 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 spannerdriver

import (
	"cloud.google.com/go/spanner"
	"context"
	"database/sql"
	"os"
	"reflect"
	"testing"
)

var (
	dsn string
)

type Connector struct {
	ctx    context.Context
	client *spanner.Client
}

func NewConnector() (*Connector, error) {

	ctx := context.Background()

	dataClient, err := spanner.NewClient(ctx, dsn)
	if err != nil {
		return nil, err
	}

	conn := &Connector{
		ctx:    ctx,
		client: dataClient,
	}
	return conn, nil
}

func (c *Connector) Close() {
	c.client.Close()
}

func init() {

	var projectId, instanceId, databaseId string
	var ok bool

	// Get environment variables or set to default.
	if instanceId, ok = os.LookupEnv("SPANNER_TEST_INSTANCE"); !ok {
		instanceId = "test-instance"
	}
	if projectId, ok = os.LookupEnv("SPANNER_TEST_PROJECT"); !ok {
		projectId = "test-project"
	}
	if databaseId, ok = os.LookupEnv("SPANNER_TEST_DBID"); !ok {
		databaseId = "gotest"
	}

	// Derive data source name.
	dsn = "projects/" + projectId + "/instances/" + instanceId + "/databases/" + databaseId
}

// Executes DML using the client library.
func ExecuteDMLClientLib(dml []string) error {

	// Open client/
	ctx := context.Background()
	client, err := spanner.NewClient(ctx, dsn)
	if err != nil {
		return err
	}
	defer client.Close()

	// Put strings into spanner.Statement structure.
	var stmts []spanner.Statement
	for _, line := range dml {
		stmts = append(stmts, spanner.NewStatement(line))
	}

	// Execute statements.
	_, err = client.ReadWriteTransaction(ctx, func(ctx context.Context, txn *spanner.ReadWriteTransaction) error {
		_, err := txn.BatchUpdate(ctx, stmts)
		if err != nil {
			return err
		}
		return nil
	})

	return err
}

func TestQueryContext(t *testing.T) {

	// Open db.
	ctx := context.Background()
	db, err := sql.Open("spanner", dsn)
	if err != nil {
		t.Fatal(err)
	}
	defer db.Close()

	// Set up test table.
	_, err = db.ExecContext(ctx, `CREATE TABLE TestQueryContext (
		A   STRING(1024),
		B  STRING(1024),
		C   STRING(1024)
	)	 PRIMARY KEY (A)`)
	if err != nil {
		t.Fatal(err)
	}

	conn, err := NewConnector()
	if err != nil {
		t.Fatal(err)
	}
	defer conn.Close()

	err = ExecuteDMLClientLib([]string{`INSERT INTO TestQueryContext (A, B, C)
		VALUES ("a1", "b1", "c1"), ("a2", "b2", "c2") , ("a3", "b3", "c3") `})
	if err != nil {
		t.Fatal(err)
	}

	type testQueryContextRow struct {
		A, B, C string
	}

	tests := []struct {
		name           string
		input          string
		want           []testQueryContextRow
		wantErrorQuery bool
		wantErrorScan  bool
		wantErrorClose bool
	}{
		{
			name:           "empty query",
			wantErrorClose: true,
			input:          "",
			want:           []testQueryContextRow{},
		},
		{
			name:           "syntax error",
			wantErrorClose: true,
			input:          "SELECT SELECT * FROM TestQueryContext",
			want:           []testQueryContextRow{},
		},
		{
			name:  "return nothing",
			input: "SELECT * FROM TestQueryContext WHERE A = \"hihihi\"",
			want:  []testQueryContextRow{},
		},
		{
			name:  "select one tuple",
			input: "SELECT * FROM TestQueryContext WHERE A = \"a1\"",
			want: []testQueryContextRow{
				{A: "a1", B: "b1", C: "c1"},
			},
		},
		{
			name:  "select subset of tuples",
			input: "SELECT * FROM TestQueryContext WHERE A = \"a1\" OR A = \"a2\"",
			want: []testQueryContextRow{
				{A: "a1", B: "b1", C: "c1"},
				{A: "a2", B: "b2", C: "c2"},
			},
		},
		{
			name:  "select subset of tuples with !=",
			input: "SELECT * FROM TestQueryContext WHERE A != \"a3\"",
			want: []testQueryContextRow{
				{A: "a1", B: "b1", C: "c1"},
				{A: "a2", B: "b2", C: "c2"},
			},
		},
		{
			name:  "select entire table",
			input: "SELECT * FROM TestQueryContext ORDER BY A",
			want: []testQueryContextRow{
				{A: "a1", B: "b1", C: "c1"},
				{A: "a2", B: "b2", C: "c2"},
				{A: "a3", B: "b3", C: "c3"},
			},
		},
		{
			name:           "query non existent table",
			wantErrorClose: true,
			input:          "SELECT * FROM TestQueryContexta",
			want: []testQueryContextRow{},
		},
	}

	// Run tests
	for _, tc := range tests {

		rows, err := db.QueryContext(ctx, tc.input)
		if (err != nil) && (!tc.wantErrorQuery) {
			t.Errorf("%s: unexpected query error: %v", tc.name, err)
		}
		if (err == nil) && (tc.wantErrorQuery) {
			t.Errorf("%s: expected query error but error was %v", tc.name, err)
		}

		got := []testQueryContextRow{}
		for rows.Next() {
			var curr testQueryContextRow
			err := rows.Scan(&curr.A, &curr.B, &curr.C)
			if (err != nil) && (!tc.wantErrorScan) {
				t.Errorf("%s: unexpected query error: %v", tc.name, err)
			}
			if (err == nil) && (tc.wantErrorScan) {
				t.Errorf("%s: expected query error but error was %v", tc.name, err)
			}

			got = append(got, curr)
		}

		rows.Close()
		err = rows.Err()
		if (err != nil) && (!tc.wantErrorClose) {
			t.Errorf("%s: unexpected query error: %v", tc.name, err)
		}
		if (err == nil) && (tc.wantErrorClose) {
			t.Errorf("%s: expected query error but error was %v", tc.name, err)
		}
		if !reflect.DeepEqual(tc.want, got) {
			t.Errorf("Test failed: %s. want: %v, got: %v", tc.name, tc.want, got)
		}

	}

	// Drop table.
	_, err = db.ExecContext(ctx, `DROP TABLE TestQueryContext`)
	if err != nil {
		t.Error(err)
	}
}

func TestDdl(t *testing.T) {

}
<commit_msg>test: format & spelling fix<commit_after>// Copyright 2020 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 spannerdriver

import (
	"cloud.google.com/go/spanner"
	"context"
	"database/sql"
	"os"
	"reflect"
	"testing"
)

var (
	dsn string
)

type Connector struct {
	ctx    context.Context
	client *spanner.Client
}

func NewConnector() (*Connector, error) {

	ctx := context.Background()

	dataClient, err := spanner.NewClient(ctx, dsn)
	if err != nil {
		return nil, err
	}

	conn := &Connector{
		ctx:    ctx,
		client: dataClient,
	}
	return conn, nil
}

func (c *Connector) Close() {
	c.client.Close()
}

func init() {

	var projectId, instanceId, databaseId string
	var ok bool

	// Get environment variables or set to default.
	if instanceId, ok = os.LookupEnv("SPANNER_TEST_INSTANCE"); !ok {
		instanceId = "test-instance"
	}
	if projectId, ok = os.LookupEnv("SPANNER_TEST_PROJECT"); !ok {
		projectId = "test-project"
	}
	if databaseId, ok = os.LookupEnv("SPANNER_TEST_DBID"); !ok {
		databaseId = "gotest"
	}

	// Derive data source name.
	dsn = "projects/" + projectId + "/instances/" + instanceId + "/databases/" + databaseId
}

// Executes DML using the client library.
func ExecuteDMLClientLib(dml []string) error {

	// Open client/
	ctx := context.Background()
	client, err := spanner.NewClient(ctx, dsn)
	if err != nil {
		return err
	}
	defer client.Close()

	// Put strings into spanner.Statement structure.
	var stmts []spanner.Statement
	for _, line := range dml {
		stmts = append(stmts, spanner.NewStatement(line))
	}

	// Execute statements.
	_, err = client.ReadWriteTransaction(ctx, func(ctx context.Context, txn *spanner.ReadWriteTransaction) error {
		_, err := txn.BatchUpdate(ctx, stmts)
		if err != nil {
			return err
		}
		return nil
	})

	return err
}

func TestQueryContext(t *testing.T) {

	// Open db.
	ctx := context.Background()
	db, err := sql.Open("spanner", dsn)
	if err != nil {
		t.Fatal(err)
	}
	defer db.Close()

	// Set up test table.
	_, err = db.ExecContext(ctx, `CREATE TABLE TestQueryContext (
		A   STRING(1024),
		B  STRING(1024),
		C   STRING(1024)
	)	 PRIMARY KEY (A)`)
	if err != nil {
		t.Fatal(err)
	}

	conn, err := NewConnector()
	if err != nil {
		t.Fatal(err)
	}
	defer conn.Close()

	err = ExecuteDMLClientLib([]string{`INSERT INTO TestQueryContext (A, B, C)
		VALUES ("a1", "b1", "c1"), ("a2", "b2", "c2") , ("a3", "b3", "c3") `})
	if err != nil {
		t.Fatal(err)
	}

	type testQueryContextRow struct {
		A, B, C string
	}

	tests := []struct {
		name           string
		input          string
		want           []testQueryContextRow
		wantErrorQuery bool
		wantErrorScan  bool
		wantErrorClose bool
	}{
		{
			name:           "empty query",
			wantErrorClose: true,
			input:          "",
			want:           []testQueryContextRow{},
		},
		{
			name:           "syntax error",
			wantErrorClose: true,
			input:          "SELECT SELECT * FROM TestQueryContext",
			want:           []testQueryContextRow{},
		},
		{
			name:  "return nothing",
			input: "SELECT * FROM TestQueryContext WHERE A = \"hihihi\"",
			want:  []testQueryContextRow{},
		},
		{
			name:  "select one tuple",
			input: "SELECT * FROM TestQueryContext WHERE A = \"a1\"",
			want: []testQueryContextRow{
				{A: "a1", B: "b1", C: "c1"},
			},
		},
		{
			name:  "select subset of tuples",
			input: "SELECT * FROM TestQueryContext WHERE A = \"a1\" OR A = \"a2\"",
			want: []testQueryContextRow{
				{A: "a1", B: "b1", C: "c1"},
				{A: "a2", B: "b2", C: "c2"},
			},
		},
		{
			name:  "select subset of tuples with !=",
			input: "SELECT * FROM TestQueryContext WHERE A != \"a3\"",
			want: []testQueryContextRow{
				{A: "a1", B: "b1", C: "c1"},
				{A: "a2", B: "b2", C: "c2"},
			},
		},
		{
			name:  "select entire table",
			input: "SELECT * FROM TestQueryContext ORDER BY A",
			want: []testQueryContextRow{
				{A: "a1", B: "b1", C: "c1"},
				{A: "a2", B: "b2", C: "c2"},
				{A: "a3", B: "b3", C: "c3"},
			},
		},
		{
			name:           "query non existent table",
			wantErrorClose: true,
			input:          "SELECT * FROM TestQueryContexta",
			want:           []testQueryContextRow{},
		},
	}

	// Run tests
	for _, tc := range tests {

		rows, err := db.QueryContext(ctx, tc.input)
		if (err != nil) && (!tc.wantErrorQuery) {
			t.Errorf("%s: unexpected query error: %v", tc.name, err)
		}
		if (err == nil) && (tc.wantErrorQuery) {
			t.Errorf("%s: expected query error but error was %v", tc.name, err)
		}

		got := []testQueryContextRow{}
		for rows.Next() {
			var curr testQueryContextRow
			err := rows.Scan(&curr.A, &curr.B, &curr.C)
			if (err != nil) && (!tc.wantErrorScan) {
				t.Errorf("%s: unexpected query error: %v", tc.name, err)
			}
			if (err == nil) && (tc.wantErrorScan) {
				t.Errorf("%s: expected query error but error was %v", tc.name, err)
			}

			got = append(got, curr)
		}

		rows.Close()
		err = rows.Err()
		if (err != nil) && (!tc.wantErrorClose) {
			t.Errorf("%s: unexpected query error: %v", tc.name, err)
		}
		if (err == nil) && (tc.wantErrorClose) {
			t.Errorf("%s: expected query error but error was %v", tc.name, err)
		}
		if !reflect.DeepEqual(tc.want, got) {
			t.Errorf("Test failed: %s. want: %v, got: %v", tc.name, tc.want, got)
		}

	}

	// Drop table.
	_, err = db.ExecContext(ctx, `DROP TABLE TestQueryContext`)
	if err != nil {
		t.Error(err)
	}
}

func TestDdl(t *testing.T) {

}
<|endoftext|>


<commit_before>package http

import (
	"context"
	// "crypto/md5"
	// "crypto/sha1"
	// "crypto/sha256"
	// "crypto/sha512"
	"fmt"
	"github.com/aptly-dev/aptly/utils"
	"github.com/cavaliercoder/grab"
	"net/http"
	"time"

	"github.com/aptly-dev/aptly/aptly"
)

type GrabDownloader struct {
	client   *grab.Client
	maxTries int
	progress aptly.Progress
}

// Check interface
var (
	_ aptly.Downloader = (*GrabDownloader)(nil)
)

// NewGrabDownloader creates new expected downloader
func NewGrabDownloader(downLimit int64, maxTries int, progress aptly.Progress) *GrabDownloader {
	// TODO rate limiting and progress
	client := grab.NewClient()
	return &GrabDownloader{
		client:   client,
		maxTries: maxTries,
	}
}

func (d *GrabDownloader) Download(ctx context.Context, url string, destination string) error {
	return d.DownloadWithChecksum(ctx, url, destination, nil, false)
}

func (d *GrabDownloader) DownloadWithChecksum(ctx context.Context, url string, destination string, expected *utils.ChecksumInfo, ignoreMismatch bool) error {
	maxTries := d.maxTries
	const delayMax = time.Duration(5 * time.Minute)
	delay := time.Duration(1 * time.Second)
	const delayMultiplier = 2
	err := fmt.Errorf("No tries available")
	for maxTries > 0 {
		err = d.download(ctx, url, destination, expected, ignoreMismatch)
		if err == nil {
			// Success
			break
		}
		d.log("Error downloading %s: %v\n", url, err)
		if retryableError(err) {
			maxTries--
			d.log("Retrying download %s: %d\n", url, maxTries)
			time.Sleep(delay)
		} else {
			// Can't retry
			d.log("Cannot retry download %s\n", url)
			break
		}
	}
	return err
}

func (d *GrabDownloader) log(msg string, a ...interface{}) {
	// TODO don't long to stdout
	fmt.Printf(msg, a...)
	if d.progress != nil {
		d.progress.Printf(msg, a...)
	}
}

func (d *GrabDownloader) download(ctx context.Context, url string, destination string, expected *utils.ChecksumInfo, ignoreMismatch bool) error {
	// TODO clean up dest dir on permanent failure
	d.log("Starting download %s -> %s\n", url, destination)
	req, _ := grab.NewRequest(destination, url)

	// TODO ignoreMismatch
	// if expected != nil {
	// 	if expected.MD5 != "" {
	// 		req.SetChecksum(md5.New(), []byte(expected.MD5), true)
	// 	} else if expected.SHA1 != "" {
	// 		req.SetChecksum(sha1.New(), []byte(expected.SHA1), true)
	// 	} else if expected.SHA256 != "" {
	// 		req.SetChecksum(sha256.New(), []byte(expected.SHA256), true)
	// 	} else if expected.SHA512 != "" {
	// 		req.SetChecksum(sha512.New(), []byte(expected.SHA512), true)
	// 	}
	// }

	resp := d.client.Do(req)

Loop:
	for {
		select {
		case <-resp.Done:
			// download is complete
			break Loop
		}
	}
	return resp.Err()
}

func (d *GrabDownloader) GetProgress() aptly.Progress {
	return d.progress
}

func (f *GrabDownloader) GetLength(ctx context.Context, url string) (int64, error) {
	resp, err := http.Head(url)
	if err != nil {
		return -1, err
	}

	if resp.StatusCode < 200 || resp.StatusCode > 299 {
		return -1, &Error{Code: resp.StatusCode, URL: url}
	}

	if resp.ContentLength < 0 {
		return -1, fmt.Errorf("could not determine length of %s", url)
	}

	return resp.ContentLength, nil
}
<commit_msg>Reenable checksums<commit_after>package http

import (
	"context"
	"crypto/md5"
	"crypto/sha1"
	"crypto/sha256"
	"crypto/sha512"
	"fmt"
	"github.com/aptly-dev/aptly/utils"
	"github.com/cavaliercoder/grab"
	"net/http"
	"time"

	"github.com/aptly-dev/aptly/aptly"
)

type GrabDownloader struct {
	client   *grab.Client
	maxTries int
	progress aptly.Progress
}

// Check interface
var (
	_ aptly.Downloader = (*GrabDownloader)(nil)
)

// NewGrabDownloader creates new expected downloader
func NewGrabDownloader(downLimit int64, maxTries int, progress aptly.Progress) *GrabDownloader {
	// TODO rate limiting and progress
	client := grab.NewClient()
	return &GrabDownloader{
		client:   client,
		maxTries: maxTries,
	}
}

func (d *GrabDownloader) Download(ctx context.Context, url string, destination string) error {
	return d.DownloadWithChecksum(ctx, url, destination, nil, false)
}

func (d *GrabDownloader) DownloadWithChecksum(ctx context.Context, url string, destination string, expected *utils.ChecksumInfo, ignoreMismatch bool) error {
	maxTries := d.maxTries
	const delayMax = time.Duration(5 * time.Minute)
	delay := time.Duration(1 * time.Second)
	const delayMultiplier = 2
	err := fmt.Errorf("No tries available")
	for maxTries > 0 {
		err = d.download(ctx, url, destination, expected, ignoreMismatch)
		if err == nil {
			// Success
			break
		}
		d.log("Error downloading %s: %v\n", url, err)
		if retryableError(err) {
			maxTries--
			d.log("Retrying download %s: %d\n", url, maxTries)
			time.Sleep(delay)
		} else {
			// Can't retry
			d.log("Cannot retry download %s\n", url)
			break
		}
	}
	return err
}

func (d *GrabDownloader) log(msg string, a ...interface{}) {
	// TODO don't long to stdout
	fmt.Printf(msg, a...)
	if d.progress != nil {
		d.progress.Printf(msg, a...)
	}
}

func (d *GrabDownloader) download(ctx context.Context, url string, destination string, expected *utils.ChecksumInfo, ignoreMismatch bool) error {
	// TODO clean up dest dir on permanent failure
	d.log("Starting download %s -> %s\n", url, destination)
	req, _ := grab.NewRequest(destination, url)

	// TODO ignoreMismatch
	if expected != nil {
		if expected.MD5 != "" {
			req.SetChecksum(md5.New(), []byte(expected.MD5), true)
		} else if expected.SHA1 != "" {
			req.SetChecksum(sha1.New(), []byte(expected.SHA1), true)
		} else if expected.SHA256 != "" {
			req.SetChecksum(sha256.New(), []byte(expected.SHA256), true)
		} else if expected.SHA512 != "" {
			req.SetChecksum(sha512.New(), []byte(expected.SHA512), true)
		}
	}

	resp := d.client.Do(req)

Loop:
	for {
		select {
		case <-resp.Done:
			// download is complete
			break Loop
		}
	}
	return resp.Err()
}

func (d *GrabDownloader) GetProgress() aptly.Progress {
	return d.progress
}

func (f *GrabDownloader) GetLength(ctx context.Context, url string) (int64, error) {
	resp, err := http.Head(url)
	if err != nil {
		return -1, err
	}

	if resp.StatusCode < 200 || resp.StatusCode > 299 {
		return -1, &Error{Code: resp.StatusCode, URL: url}
	}

	if resp.ContentLength < 0 {
		return -1, fmt.Errorf("could not determine length of %s", url)
	}

	return resp.ContentLength, nil
}
<|endoftext|>


<commit_before>package geom

import (
	"math"
)

type LineString struct {
	layout     Layout
	stride     int
	flatCoords []float64
}

var _ T = &LineString{}

func NewLineString(layout Layout) *LineString {
	return &LineString{
		layout:     layout,
		stride:     layout.Stride(),
		flatCoords: nil,
	}
}

func NewLineStringFlat(layout Layout, flatCoords []float64) *LineString {
	return &LineString{
		layout:     layout,
		stride:     layout.Stride(),
		flatCoords: flatCoords,
	}
}

func (ls *LineString) Bounds() *Bounds {
	return NewBounds().extendFlatCoords(ls.flatCoords, 0, len(ls.flatCoords), ls.stride)
}

func (ls *LineString) Clone() *LineString {
	flatCoords := make([]float64, len(ls.flatCoords))
	copy(flatCoords, ls.flatCoords)
	return &LineString{
		layout:     ls.layout,
		stride:     ls.stride,
		flatCoords: flatCoords,
	}
}

func (ls *LineString) Coord(i int) []float64 {
	return ls.flatCoords[i*ls.stride : (i+1)*ls.stride]
}

func (ls *LineString) Coords() [][]float64 {
	return inflate1(ls.flatCoords, 0, len(ls.flatCoords), ls.stride)
}

func (ls *LineString) Ends() []int {
	return nil
}

func (ls *LineString) Endss() [][]int {
	return nil
}

func (ls *LineString) FlatCoords() []float64 {
	return ls.flatCoords
}

func (ls *LineString) LastCoord() []float64 {
	if len(ls.flatCoords) == 0 {
		return nil
	} else {
		return ls.flatCoords[len(ls.flatCoords)-ls.stride:]
	}
}

func (ls *LineString) Layout() Layout {
	return ls.layout
}

func (ls *LineString) Length() float64 {
	length := 0.0
	for i := ls.stride; i < len(ls.flatCoords); i += ls.stride {
		dx := ls.flatCoords[i] - ls.flatCoords[i-ls.stride]
		dy := ls.flatCoords[i+1] - ls.flatCoords[i+1-ls.stride]
		length += math.Sqrt(dx*dx + dy*dy)
	}
	return length
}

func (ls *LineString) Interpolate(val float64, dim int) (int, float64) {
	n := len(ls.flatCoords)
	if n == 0 {
		panic("geom: empty linestring")
	}
	if val <= ls.flatCoords[dim] {
		return 0, 0
	}
	if ls.flatCoords[n-ls.stride+dim] <= val {
		return (n - 1) / ls.stride, 0
	}
	low := 0
	high := n / ls.stride
	for low < high {
		mid := (low + high) / 2
		if val < ls.flatCoords[mid*ls.stride+dim] {
			high = mid
		} else {
			low = mid + 1
		}
	}
	low--
	val0 := ls.flatCoords[low*ls.stride+dim]
	if val == val0 {
		return low, 0
	}
	val1 := ls.flatCoords[(low+1)*ls.stride+dim]
	return low, (val - val0) / (val1 - val0)
}

func (ls *LineString) NumCoords() int {
	return len(ls.flatCoords) / ls.stride
}

func (ls *LineString) Push(coord0 []float64) error {
	if len(coord0) != ls.stride {
		return ErrStrideMismatch{Got: len(coord0), Want: ls.stride}
	}
	ls.flatCoords = append(ls.flatCoords, coord0...)
	return nil
}

func (ls *LineString) SetCoords(coords1 [][]float64) error {
	var err error
	if ls.flatCoords, err = deflate1(nil, coords1, ls.stride); err != nil {
		return err
	}
	return nil
}

func (ls *LineString) Stride() int {
	return ls.stride
}

func (ls *LineString) SubLineString(start, stop int) *LineString {
	return &LineString{
		layout:     ls.layout,
		stride:     ls.stride,
		flatCoords: ls.flatCoords[start*ls.stride : stop*ls.stride],
	}
}
<commit_msg>Add LineString.MustSetCoords<commit_after>package geom

import (
	"math"
)

type LineString struct {
	layout     Layout
	stride     int
	flatCoords []float64
}

var _ T = &LineString{}

func NewLineString(layout Layout) *LineString {
	return &LineString{
		layout:     layout,
		stride:     layout.Stride(),
		flatCoords: nil,
	}
}

func NewLineStringFlat(layout Layout, flatCoords []float64) *LineString {
	return &LineString{
		layout:     layout,
		stride:     layout.Stride(),
		flatCoords: flatCoords,
	}
}

func (ls *LineString) Bounds() *Bounds {
	return NewBounds().extendFlatCoords(ls.flatCoords, 0, len(ls.flatCoords), ls.stride)
}

func (ls *LineString) Clone() *LineString {
	flatCoords := make([]float64, len(ls.flatCoords))
	copy(flatCoords, ls.flatCoords)
	return &LineString{
		layout:     ls.layout,
		stride:     ls.stride,
		flatCoords: flatCoords,
	}
}

func (ls *LineString) Coord(i int) []float64 {
	return ls.flatCoords[i*ls.stride : (i+1)*ls.stride]
}

func (ls *LineString) Coords() [][]float64 {
	return inflate1(ls.flatCoords, 0, len(ls.flatCoords), ls.stride)
}

func (ls *LineString) Ends() []int {
	return nil
}

func (ls *LineString) Endss() [][]int {
	return nil
}

func (ls *LineString) FlatCoords() []float64 {
	return ls.flatCoords
}

func (ls *LineString) LastCoord() []float64 {
	if len(ls.flatCoords) == 0 {
		return nil
	} else {
		return ls.flatCoords[len(ls.flatCoords)-ls.stride:]
	}
}

func (ls *LineString) Layout() Layout {
	return ls.layout
}

func (ls *LineString) Length() float64 {
	length := 0.0
	for i := ls.stride; i < len(ls.flatCoords); i += ls.stride {
		dx := ls.flatCoords[i] - ls.flatCoords[i-ls.stride]
		dy := ls.flatCoords[i+1] - ls.flatCoords[i+1-ls.stride]
		length += math.Sqrt(dx*dx + dy*dy)
	}
	return length
}

func (ls *LineString) Interpolate(val float64, dim int) (int, float64) {
	n := len(ls.flatCoords)
	if n == 0 {
		panic("geom: empty linestring")
	}
	if val <= ls.flatCoords[dim] {
		return 0, 0
	}
	if ls.flatCoords[n-ls.stride+dim] <= val {
		return (n - 1) / ls.stride, 0
	}
	low := 0
	high := n / ls.stride
	for low < high {
		mid := (low + high) / 2
		if val < ls.flatCoords[mid*ls.stride+dim] {
			high = mid
		} else {
			low = mid + 1
		}
	}
	low--
	val0 := ls.flatCoords[low*ls.stride+dim]
	if val == val0 {
		return low, 0
	}
	val1 := ls.flatCoords[(low+1)*ls.stride+dim]
	return low, (val - val0) / (val1 - val0)
}

func (ls *LineString) MustSetCoords(coords1 [][]float64) *LineString {
	if err := ls.SetCoords(coords1); err != nil {
		panic(err)
	}
	return ls
}

func (ls *LineString) NumCoords() int {
	return len(ls.flatCoords) / ls.stride
}

func (ls *LineString) Push(coord0 []float64) error {
	if len(coord0) != ls.stride {
		return ErrStrideMismatch{Got: len(coord0), Want: ls.stride}
	}
	ls.flatCoords = append(ls.flatCoords, coord0...)
	return nil
}

func (ls *LineString) SetCoords(coords1 [][]float64) error {
	var err error
	if ls.flatCoords, err = deflate1(nil, coords1, ls.stride); err != nil {
		return err
	}
	return nil
}

func (ls *LineString) Stride() int {
	return ls.stride
}

func (ls *LineString) SubLineString(start, stop int) *LineString {
	return &LineString{
		layout:     ls.layout,
		stride:     ls.stride,
		flatCoords: ls.flatCoords[start*ls.stride : stop*ls.stride],
	}
}
<|endoftext|>


<commit_before>package main

import (
	"log"

	"github.com/AsynkronIT/gam/actor"
	"github.com/AsynkronIT/gam/couchbase_persistence"
	"github.com/AsynkronIT/gam/examples/persistence/messages"
	"github.com/AsynkronIT/gam/persistence"
	"github.com/AsynkronIT/goconsole"
)

type persistentActor struct {
	name  string
	items []string
}

//CQRS style messages
func (state *persistentActor) Receive(context actor.Context) {
	switch msg := context.Message().(type) {
	case *messages.RenameCommand: //command handler, you can have side effects here
		event := &messages.RenamedEvent{Name: msg.Name}
		log.Printf("Rename %v\n", msg.Name)
		context.Receive(event)
	case *messages.RenamedEvent: //event handler, only mutate state here
		state.name = msg.Name
	case *messages.AddItemCommand:
		event := &messages.AddedItemEvent{Item: msg.Item}
		log.Printf("Add item %v", msg.Item)
		context.Receive(event)
	case *messages.AddedItemEvent:
		state.items = append(state.items, msg.Item)
	case *messages.DumpCommand: //just so we can manually trigger a console dump of state
		log.Printf("%+v", state)
	case *persistence.ReplayComplete: //will be triggered once the persistence plugin have replayed all events
		log.Println("Replay Complete")
		context.Receive(&messages.DumpCommand{})
	}
}

func newPersistentActor() actor.Actor {
	return &persistentActor{
		name: "Initial Name",
	}
}

func main() {

	props := actor.
		FromProducer(newPersistentActor).
		WithReceivers(persistence.Using(couchbase_persistence.New("labb", "couchbase://localhost")))

	pid := actor.Spawn(props)
	pid.Tell(&messages.AddItemCommand{Item: "Banana"})
	pid.Tell(&messages.AddItemCommand{Item: "Apple"})
	pid.Tell(&messages.AddItemCommand{Item: "Orange"})
	pid.Tell(&messages.RenameCommand{Name: "Acme Inc"})
	pid.Tell(&messages.DumpCommand{})
	pid.Tell(&actor.PoisonPill{})
	pid.Tell(&messages.DumpCommand{})
	console.ReadLine()
}
<commit_msg>updates<commit_after>package main

import (
	"log"

	"github.com/AsynkronIT/gam/actor"
	"github.com/AsynkronIT/gam/couchbase_persistence"
	"github.com/AsynkronIT/gam/examples/persistence/messages"
	"github.com/AsynkronIT/gam/persistence"
	"github.com/AsynkronIT/goconsole"
)

type persistentActor struct {
	name  string
	items []string
}

//CQRS style messages
func (state *persistentActor) Receive(context actor.Context) {
	switch msg := context.Message().(type) {
	case *messages.RenameCommand: //command handler, you can have side effects here
		event := &messages.RenamedEvent{Name: msg.Name}
		log.Printf("Rename %v\n", msg.Name)
		context.Receive(event)
	case *messages.RenamedEvent: //event handler, only mutate state here
		state.name = msg.Name
	case *messages.AddItemCommand:
		event := &messages.AddedItemEvent{Item: msg.Item}
		log.Printf("Add item %v", msg.Item)
		context.Receive(event)
	case *messages.AddedItemEvent:
		state.items = append(state.items, msg.Item)
	case *messages.DumpCommand: //just so we can manually trigger a console dump of state
		log.Printf("%+v", state)
	case *persistence.ReplayComplete: //will be triggered once the persistence plugin have replayed all events
		log.Println("Replay Complete")
		context.Receive(&messages.DumpCommand{})
	}
}

func newPersistentActor() actor.Actor {
	return &persistentActor{
		name: "Initial Name",
	}
}

func main() {

	props := actor.
		FromProducer(newPersistentActor).
		WithReceivers(persistence.Using(couchbase_persistence.New("labb", "couchbase://localhost")))

	pid := actor.Spawn(props)
	pid.Tell(&messages.AddItemCommand{Item: "Banana"})
	pid.Tell(&messages.AddItemCommand{Item: "Apple"})
	pid.Tell(&messages.AddItemCommand{Item: "Orange"})
	pid.Tell(&messages.RenameCommand{Name: "Acme Inc"})
	pid.Tell(&messages.DumpCommand{}) //dump current state to console
	pid.Tell(&actor.PoisonPill{})     //force restart of actor to show that we can handle failure
	pid.Tell(&messages.DumpCommand{})
	console.ReadLine()
}
<|endoftext|>