<commit_before>package negroni

import (
	"log"
	"net/http"
	"os"
)

const (
	// DefaultAddress is used if no other is specified.
	DefaultAddress = ":8080"
)

// Handler handler is an interface that objects can implement to be registered to serve as middleware
// in the Negroni middleware stack.
// ServeHTTP should yield to the next middleware in the chain by invoking the next http.HandlerFunc
// passed in.
//
// If the Handler writes to the ResponseWriter, the next http.HandlerFunc should not be invoked.
type Handler interface {
	ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc)
}

// HandlerFunc is an adapter to allow the use of ordinary functions as Negroni handlers.
// If f is a function with the appropriate signature, HandlerFunc(f) is a Handler object that calls f.
type HandlerFunc func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc)

func (h HandlerFunc) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
	h(rw, r, next)
}

type middleware struct {
	handler Handler
	next    *middleware

	// nextfn stores the next.ServeHTTP to reduce memory allocate
	nextfn func(rw http.ResponseWriter, r *http.Request)
}

func newMiddleware(handler Handler, next *middleware) middleware {
	return middleware{
		handler: handler,
		next:    next,
		nextfn:  next.ServeHTTP,
	}
}

func (m middleware) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
	m.handler.ServeHTTP(rw, r, m.nextfn)
}

// Wrap converts a http.Handler into a negroni.Handler so it can be used as a Negroni
// middleware. The next http.HandlerFunc is automatically called after the Handler
// is executed.
func Wrap(handler http.Handler) Handler {
	return HandlerFunc(func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
		handler.ServeHTTP(rw, r)
		next(rw, r)
	})
}

// WrapFunc converts a http.HandlerFunc into a negroni.Handler so it can be used as a Negroni
// middleware. The next http.HandlerFunc is automatically called after the Handler
// is executed.
func WrapFunc(handlerFunc http.HandlerFunc) Handler {
	return HandlerFunc(func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
		handlerFunc(rw, r)
		next(rw, r)
	})
}

// Negroni is a stack of Middleware Handlers that can be invoked as an http.Handler.
// Negroni middleware is evaluated in the order that they are added to the stack using
// the Use and UseHandler methods.
type Negroni struct {
	middleware middleware
	handlers   []Handler
}

// New returns a new Negroni instance with no middleware preconfigured.
func New(handlers ...Handler) *Negroni {
	return &Negroni{
		handlers:   handlers,
		middleware: build(handlers),
	}
}

// With returns a new Negroni instance that is a combination of the negroni
// receiver's handlers and the provided handlers.
func (n *Negroni) With(handlers ...Handler) *Negroni {
	currentHandlers := make([]Handler, len(n.handlers))
	copy(currentHandlers, n.handlers)
	return New(
		append(currentHandlers, handlers...)...,
	)
}

// Classic returns a new Negroni instance with the default middleware already
// in the stack.
//
// Recovery - Panic Recovery Middleware
// Logger - Request/Response Logging
// Static - Static File Serving
func Classic() *Negroni {
	return New(NewRecovery(), NewLogger(), NewStatic(http.Dir("public")))
}

func (n *Negroni) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
	n.middleware.ServeHTTP(NewResponseWriter(rw), r)
}

// Use adds a Handler onto the middleware stack. Handlers are invoked in the order they are added to a Negroni.
func (n *Negroni) Use(handler Handler) {
	if handler == nil {
		panic("handler cannot be nil")
	}

	n.handlers = append(n.handlers, handler)
	n.middleware = build(n.handlers)
}

// UseFunc adds a Negroni-style handler function onto the middleware stack.
func (n *Negroni) UseFunc(handlerFunc func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc)) {
	n.Use(HandlerFunc(handlerFunc))
}

// UseHandler adds a http.Handler onto the middleware stack. Handlers are invoked in the order they are added to a Negroni.
func (n *Negroni) UseHandler(handler http.Handler) {
	n.Use(Wrap(handler))
}

// UseHandlerFunc adds a http.HandlerFunc-style handler function onto the middleware stack.
func (n *Negroni) UseHandlerFunc(handlerFunc func(rw http.ResponseWriter, r *http.Request)) {
	n.UseHandler(http.HandlerFunc(handlerFunc))
}

// Run is a convenience function that runs the negroni stack as an HTTP
// server. The addr string, if provided, takes the same format as http.ListenAndServe.
// If no address is provided but the PORT environment variable is set, the PORT value is used.
// If neither is provided, the address' value will equal the DefaultAddress constant.
func (n *Negroni) Run(addr ...string) {
	l := log.New(os.Stdout, "[negroni] ", 0)
	finalAddr := detectAddress(addr...)
	l.Printf("listening on %s", finalAddr)
	l.Fatal(http.ListenAndServe(finalAddr, n))
}

func detectAddress(addr ...string) string {
	if len(addr) > 0 {
		return addr[0]
	}
	if port := os.Getenv("PORT"); port != "" {
		return ":" + port
	}
	return DefaultAddress
}

// Returns a list of all the handlers in the current Negroni middleware chain.
func (n *Negroni) Handlers() []Handler {
	return n.handlers
}

func build(handlers []Handler) middleware {
	var next middleware

	switch {
	case len(handlers) == 0:
		return voidMiddleware()
	case len(handlers) > 1:
		next = build(handlers[1:])
	default:
		next = voidMiddleware()
	}

	return newMiddleware(handlers[0], &next)
}

func voidMiddleware() middleware {
	return newMiddleware(
		HandlerFunc(func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {}),
		&middleware{},
	)
}
<commit_msg>Remove unneeded `next` field from `middleware`<commit_after>package negroni

import (
	"log"
	"net/http"
	"os"
)

const (
	// DefaultAddress is used if no other is specified.
	DefaultAddress = ":8080"
)

// Handler handler is an interface that objects can implement to be registered to serve as middleware
// in the Negroni middleware stack.
// ServeHTTP should yield to the next middleware in the chain by invoking the next http.HandlerFunc
// passed in.
//
// If the Handler writes to the ResponseWriter, the next http.HandlerFunc should not be invoked.
type Handler interface {
	ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc)
}

// HandlerFunc is an adapter to allow the use of ordinary functions as Negroni handlers.
// If f is a function with the appropriate signature, HandlerFunc(f) is a Handler object that calls f.
type HandlerFunc func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc)

func (h HandlerFunc) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
	h(rw, r, next)
}

type middleware struct {
	handler Handler

	// nextfn stores the next.ServeHTTP to reduce memory allocate
	nextfn func(rw http.ResponseWriter, r *http.Request)
}

func newMiddleware(handler Handler, next *middleware) middleware {
	return middleware{
		handler: handler,
		nextfn:  next.ServeHTTP,
	}
}

func (m middleware) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
	m.handler.ServeHTTP(rw, r, m.nextfn)
}

// Wrap converts a http.Handler into a negroni.Handler so it can be used as a Negroni
// middleware. The next http.HandlerFunc is automatically called after the Handler
// is executed.
func Wrap(handler http.Handler) Handler {
	return HandlerFunc(func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
		handler.ServeHTTP(rw, r)
		next(rw, r)
	})
}

// WrapFunc converts a http.HandlerFunc into a negroni.Handler so it can be used as a Negroni
// middleware. The next http.HandlerFunc is automatically called after the Handler
// is executed.
func WrapFunc(handlerFunc http.HandlerFunc) Handler {
	return HandlerFunc(func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
		handlerFunc(rw, r)
		next(rw, r)
	})
}

// Negroni is a stack of Middleware Handlers that can be invoked as an http.Handler.
// Negroni middleware is evaluated in the order that they are added to the stack using
// the Use and UseHandler methods.
type Negroni struct {
	middleware middleware
	handlers   []Handler
}

// New returns a new Negroni instance with no middleware preconfigured.
func New(handlers ...Handler) *Negroni {
	return &Negroni{
		handlers:   handlers,
		middleware: build(handlers),
	}
}

// With returns a new Negroni instance that is a combination of the negroni
// receiver's handlers and the provided handlers.
func (n *Negroni) With(handlers ...Handler) *Negroni {
	currentHandlers := make([]Handler, len(n.handlers))
	copy(currentHandlers, n.handlers)
	return New(
		append(currentHandlers, handlers...)...,
	)
}

// Classic returns a new Negroni instance with the default middleware already
// in the stack.
//
// Recovery - Panic Recovery Middleware
// Logger - Request/Response Logging
// Static - Static File Serving
func Classic() *Negroni {
	return New(NewRecovery(), NewLogger(), NewStatic(http.Dir("public")))
}

func (n *Negroni) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
	n.middleware.ServeHTTP(NewResponseWriter(rw), r)
}

// Use adds a Handler onto the middleware stack. Handlers are invoked in the order they are added to a Negroni.
func (n *Negroni) Use(handler Handler) {
	if handler == nil {
		panic("handler cannot be nil")
	}

	n.handlers = append(n.handlers, handler)
	n.middleware = build(n.handlers)
}

// UseFunc adds a Negroni-style handler function onto the middleware stack.
func (n *Negroni) UseFunc(handlerFunc func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc)) {
	n.Use(HandlerFunc(handlerFunc))
}

// UseHandler adds a http.Handler onto the middleware stack. Handlers are invoked in the order they are added to a Negroni.
func (n *Negroni) UseHandler(handler http.Handler) {
	n.Use(Wrap(handler))
}

// UseHandlerFunc adds a http.HandlerFunc-style handler function onto the middleware stack.
func (n *Negroni) UseHandlerFunc(handlerFunc func(rw http.ResponseWriter, r *http.Request)) {
	n.UseHandler(http.HandlerFunc(handlerFunc))
}

// Run is a convenience function that runs the negroni stack as an HTTP
// server. The addr string, if provided, takes the same format as http.ListenAndServe.
// If no address is provided but the PORT environment variable is set, the PORT value is used.
// If neither is provided, the address' value will equal the DefaultAddress constant.
func (n *Negroni) Run(addr ...string) {
	l := log.New(os.Stdout, "[negroni] ", 0)
	finalAddr := detectAddress(addr...)
	l.Printf("listening on %s", finalAddr)
	l.Fatal(http.ListenAndServe(finalAddr, n))
}

func detectAddress(addr ...string) string {
	if len(addr) > 0 {
		return addr[0]
	}
	if port := os.Getenv("PORT"); port != "" {
		return ":" + port
	}
	return DefaultAddress
}

// Returns a list of all the handlers in the current Negroni middleware chain.
func (n *Negroni) Handlers() []Handler {
	return n.handlers
}

func build(handlers []Handler) middleware {
	var next middleware

	switch {
	case len(handlers) == 0:
		return voidMiddleware()
	case len(handlers) > 1:
		next = build(handlers[1:])
	default:
		next = voidMiddleware()
	}

	return newMiddleware(handlers[0], &next)
}

func voidMiddleware() middleware {
	return newMiddleware(
		HandlerFunc(func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {}),
		&middleware{},
	)
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"net"
	"os"
	"os/exec"
	"strings"

	"github.com/miekg/dns"
)

/*
// =============================================================
// just a few tips when starting the server
// the linux network manager generally wont let you
// start the server on 53 when dnsmasq is already running.
// to demo the functionality of the server properly we need
// to stop dnsmasq first. To do this we can use 'ss -ant' to
// verify that port 53 is in use and 'lsof -i tcp:53' to make
// sure that dnsmasq is the culprit and find its pid. After
// the pid is found we kill it like normal with 'kill'. When
// we finish testing we should probably start dnsmasq again.
// we do this with systemctl start dnsmasq (on systemd machines)
// =============================================================
// we also need to point /etc/resolve.conf to our custom nameserver
// we can do this by changing the 'nameserver' entry to loopback:
// 127.0.0.1
// =============================================================
*/

/*
// =============================================================
// general strategy:
// This is super simple since we do not have
// A ton of entries. We should have a directory in the same path
// that the DNS server runs and inside that directory is a set of
// files that are formatted as follows:
//
// filename: FQDM
// content:  ipv4 address
//
// this will allow us to easily see if we have an entry and pack
// any new content into the response RR.
// =============================================================
*/
var defaultServer string

func main() {
	defaultServerAsk := flag.String("upstream", "8.8.8.8:53", "upstream nameserver")
	rawPort := flag.String("p", "53", "port to listen on")
	chain := flag.Bool("chain", false, "chain with remote manager")
	flag.Parse()
	defaultServer = *defaultServerAsk

	if *chain {
		chainStart := exec.Command("./remotemanager")
		err := chainStart.Start()
		println("starting remotemanager on 8054")
		if err != nil {
			println(err)
			println("remote manager would not start, continuing..")
		}
	}

	//format the string to be :port
	port := fmt.Sprint(":", *rawPort)

	udpServer := &dns.Server{Addr: port, Net: "udp"}
	tcpServer := &dns.Server{Addr: port, Net: "tcp"}
	println("started server on", *rawPort)
	// miekg/dns forces use to have a function for the handler, I'll submit a pr so he fixes it
	dns.HandleFunc(".", route)

	go func() {
		log.Fatal(udpServer.ListenAndServe())
	}()
	log.Fatal(tcpServer.ListenAndServe())
}

func route(w dns.ResponseWriter, req *dns.Msg) {
	proxy(defaultServer, w, req)
}

func proxy(addr string, w dns.ResponseWriter, req *dns.Msg) {
	hostname := req.Question[0].Name

	transport := "udp"
	if _, ok := w.RemoteAddr().(*net.TCPAddr); ok {
		transport = "tcp"
	}
	c := &dns.Client{Net: transport}
	resp, _, err := c.Exchange(req, addr)

	if err != nil {
		dns.HandleFailed(w, req)
		return
	}

	// we should be able to repack a custom answer if the fully qualified hostname
	// is found in our storage.
	//
	// if a record is found we parse the ipv4 address and build a new 'Answer' RR

	filepath := fmt.Sprintf("records/%s", hostname)

	if _, err := os.Stat(filepath); err == nil {
		//found a match; continue

		rr := new(dns.A)
		rr.Hdr = dns.RR_Header{Name: hostname, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 0}
		tmpaddr, _ := ioutil.ReadFile(filepath)
		addr := strings.TrimSuffix(string(tmpaddr), "\n")

		rr.A = net.ParseIP(addr)

		rep := new(dns.Msg)
		rep.SetReply(req)
		rep.Answer = append(rep.Answer, rr)

		println("serving", hostname, "from local record")
		w.WriteMsg(rep)
		return

	}

	println("serving", hostname, "from", defaultServer)
	w.WriteMsg(resp)
}
<commit_msg>slight doc change<commit_after>package main

import (
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"net"
	"os"
	"os/exec"
	"strings"

	"github.com/miekg/dns"
)

/*
// =============================================================
// just a few tips when starting the server on a workstation
// that is not designed to run a DNS server on port 53.
// the linux network manager generally wont let you
// start the server on 53 when dnsmasq is already running.
// to demo the functionality of the server properly we need
// to stop dnsmasq first. To do this we can use 'ss -ant' to
// verify that port 53 is in use and 'lsof -i tcp:53' to make
// sure that dnsmasq is the culprit and find its pid. After
// the pid is found we kill it like normal with 'kill'. When
// we finish testing we should probably start dnsmasq again.
// we do this with systemctl start dnsmasq (on systemd machines)
// =============================================================
// we also need to point /etc/resolve.conf to our custom nameserver
// we can do this by changing the 'nameserver' entry to loopback:
// 127.0.0.1
// =============================================================
*/

/*
// =============================================================
// general strategy:
// This is very simple as we are not dealing with
// a ton of entries. We should have a directory in the same path
// that the DNS server runs and inside that directory is a set of
// files that are formatted as follows:
//
// filename: FQDM
// content:  ipv4 address
//
// this will allow us to easily see if we have an entry and pack
// any new content into the response RR.
// =============================================================
*/
var defaultServer string

func main() {
	defaultServerAsk := flag.String("upstream", "8.8.8.8:53", "upstream nameserver")
	rawPort := flag.String("p", "53", "port to listen on")
	chain := flag.Bool("chain", false, "chain with remote manager")
	flag.Parse()
	defaultServer = *defaultServerAsk

	if *chain {
		chainStart := exec.Command("./remotemanager")
		err := chainStart.Start()
		println("starting remotemanager on 8054")
		if err != nil {
			println(err)
			println("remote manager would not start, continuing..")
		}
	}

	//format the string to be :port
	port := fmt.Sprint(":", *rawPort)

	udpServer := &dns.Server{Addr: port, Net: "udp"}
	tcpServer := &dns.Server{Addr: port, Net: "tcp"}
	println("started server on", *rawPort)
	// miekg/dns forces use to have a function for the handler, I'll submit a pr so he fixes it
	dns.HandleFunc(".", route)

	go func() {
		log.Fatal(udpServer.ListenAndServe())
	}()
	log.Fatal(tcpServer.ListenAndServe())
}

func route(w dns.ResponseWriter, req *dns.Msg) {
	proxy(defaultServer, w, req)
}

func proxy(addr string, w dns.ResponseWriter, req *dns.Msg) {
	hostname := req.Question[0].Name

	transport := "udp"
	if _, ok := w.RemoteAddr().(*net.TCPAddr); ok {
		transport = "tcp"
	}
	c := &dns.Client{Net: transport}
	resp, _, err := c.Exchange(req, addr)

	if err != nil {
		dns.HandleFailed(w, req)
		return
	}

	// we should be able to repack a custom answer if the fully qualified hostname
	// is found in our storage.
	//
	// if a record is found we parse the ipv4 address and build a new 'Answer' RR

	filepath := fmt.Sprintf("records/%s", hostname)

	if _, err := os.Stat(filepath); err == nil {
		//found a match; continue

		rr := new(dns.A)
		rr.Hdr = dns.RR_Header{Name: hostname, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 0}
		tmpaddr, _ := ioutil.ReadFile(filepath)
		addr := strings.TrimSuffix(string(tmpaddr), "\n")

		rr.A = net.ParseIP(addr)

		rep := new(dns.Msg)
		rep.SetReply(req)
		rep.Answer = append(rep.Answer, rr)

		println("serving", hostname, "from local record")
		w.WriteMsg(rep)
		return

	}

	println("serving", hostname, "from", defaultServer)
	w.WriteMsg(resp)
}
<|endoftext|>


<commit_before>package main

/*

infra.tf
payload.json (json description of vm)
resource.json (status tracking)
terraform.tfstate ()

*/

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

	"io/ioutil"

	"bytes"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/s3"
	"github.com/perigee/terrant/app"
	"github.com/perigee/terrant/design"
)

const (
	BUCKETNAME     = "autoterrarepostate"
	DEFAULTREGION  = "us-east-1"
	TerraformState = "infra.tf"
)

func downloadS3object(s3client *s3.S3, key string) ([]byte, error) {
	res, err := s3client.GetObject(&s3.GetObjectInput{
		Bucket: aws.String("autoterrarepostate"),
		Key:    aws.String(key),
	})

	if err != nil {
		return nil, err
	}

	defer res.Body.Close()

	buf := bytes.NewBuffer(nil)

	if _, err := io.Copy(buf, res.Body); err != nil {
		return nil, err

	}

	return buf.Bytes(), nil
}

// FetchObject fetch the object
func FetchObject(ctx *app.CreateChefContext) ([]byte, error) {

	//
	okey := make([]string, 3)

	okey[0] = ctx.Request.Header.Get(design.SPACEID)
	okey[1] = ctx.Payload.Vmuid
	okey[2] = "infra.tf"

	objectkey := strings.Join(okey, "/")

	sess, err := session.NewSession()

	if err != nil {
		panic(err)
	}

	svc := s3.New(sess, &aws.Config{Region: aws.String("us-east-1")})

	resp, err := downloadS3object(svc, objectkey)

	if err != nil {
		fmt.Println(err.Error())
		return nil, err
	}

	fmt.Println(resp)

	return resp, nil
}

// UpdateTerraFile updates
func UpdateTerraFile(s3obj *s3.GetObjectOutput) (*s3.GetObjectOutput, error) {
	return nil, nil
}

// UploadObject upload the object on s3
func UploadObject() error {
	return nil

}

// JSON2File creates the file based on given json
/*
    if err := JSON2File(ctx.Payload.NodeAttributes, "atrb.json"); err != nil {
		panic(err)
	}
*/
func JSON2File(in *interface{}, filename string) error {
	jsonStr, err := json.Marshal(*in)
	if err != nil {
		fmt.Println(err.Error())
		return err
	}
	ioutil.WriteFile(filename, jsonStr, 0644)
	return nil
}
<commit_msg>func for s3 key generation, using constant instead<commit_after>package main

/*

infra.tf
payload.json (json description of vm)
resource.json (status tracking)
terraform.tfstate ()

*/

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

	"io/ioutil"

	"bytes"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/s3"
	"github.com/perigee/terrant/app"
	"github.com/perigee/terrant/design"
)

const (
	terraBUCKETNAME     = "autoterrarepostate"
	terraDEFAULTREGION  = "us-east-1"
	terraTerraformState = "infra.tf"
	terraStatusFile     = "resource.json"
	terraAttributeFile  = "attributes.json"
)

func downloadS3object(s3client *s3.S3, key string) ([]byte, error) {
	res, err := s3client.GetObject(&s3.GetObjectInput{
		Bucket: aws.String(terraBUCKETNAME),
		Key:    aws.String(key),
	})

	if err != nil {
		return nil, err
	}

	defer res.Body.Close()

	buf := bytes.NewBuffer(nil)

	if _, err := io.Copy(buf, res.Body); err != nil {
		return nil, err

	}

	return buf.Bytes(), nil
}

func uploadS3object(s3client *s3.S3, key string, body []byte) error {

	_, err := s3client.PutObject(&s3.PutObjectInput{
		Bucket: aws.String(terraBUCKETNAME),
		Key:    aws.String(key),
		Body:   bytes.NewReader(body),
	})

	if err != nil {
		fmt.Println(err.Error())
		return err
	}

	return nil
}

func s3KeyGen(ctx *app.CreateChefContext, filename string) string {
	okey := make([]string, 3)

	okey[0] = ctx.Request.Header.Get(design.SPACEID)
	okey[1] = ctx.Payload.Vmuid
	okey[2] = filename
	return strings.Join(okey, "/")
}

// FetchObject fetch the object
func FetchObject(ctx *app.CreateChefContext) ([]byte, error) {
	sess, err := session.NewSession()

	if err != nil {
		panic(err)
	}

	svc := s3.New(sess, &aws.Config{Region: aws.String(terraDEFAULTREGION)})

	resp, err := downloadS3object(svc, s3KeyGen(ctx, terraStatusFile))

	if err != nil {
		fmt.Println(err.Error())
		return nil, err
	}

	if err := uploadS3object(svc, s3KeyGen(ctx, "jun_tmp_file"), resp); err != nil {
		fmt.Println(err.Error())
		return nil, err
	}

	return resp, nil
}

// CheckJobStatus verifies the status of current resource
func CheckJobStatus(ctx *app.CreateChefContext) (string, error) {

	return "", nil
}

// UpdateTerraFile updates
func UpdateTerraFile(s3obj *s3.GetObjectOutput) (*s3.GetObjectOutput, error) {
	return nil, nil
}

// UploadObject upload the object on s3
func UploadObject() error {
	return nil
}

// JSON2File creates the file based on given json
/*
    if err := JSON2File(ctx.Payload.NodeAttributes, "atrb.json"); err != nil {
		panic(err)
	}
*/
func JSON2File(in *interface{}, filename string) error {
	jsonStr, err := json.Marshal(*in)
	if err != nil {
		fmt.Println(err.Error())
		return err
	}
	ioutil.WriteFile(filename, jsonStr, 0644)
	return nil
}
<|endoftext|>


<commit_before>package main

// gdsh option parsing - a custom parser is implemented because the
// few packages I checked out didn't do what I want, or ended up being
// really clunky to do some of the uglier bits of what gdsh needs

import (
	"fmt"
	"log"
	"os"
	"os/user"
	"strings"
)

type GdshOptions struct {
	List         string            // --list/-l
	Next         bool              // --next
	Reset        bool              // --reset
	Verbose      bool              // --verbose/-v
	Comment      string            // --comment
	InclRe       string            // --include
	ExclRe       string            // --exclude
	Key          string            // --key/-i
	User         string            // --user
	Node         string            // --node/-h
	Command      string            // --command/-c
	Script       string            // --script/-s
	BgJob        bool              // --background/-b
	RemoteLog    string            // --remote-log/-r
	RemoteScript string            // --remote-script-path
	Env          map[string]string // --env/-e key=val
	Args         []string          // leftover arguments for subcommands
}

var usageText = `
HELP!
`

func printUsage() {
	fmt.Printf("%s", usageText)
}

func deleteArg(index int) (args []string, deleted string) {
	args = make([]string, len(os.Args)-1)

	offset := 0
	for i := range os.Args {
		if i == index {
			deleted = os.Args[i]
			offset = 1
			continue
		}
		args[i-offset] = os.Args[i]
	}

	return
}

func parseArgs(args []string, command string) (opt GdshOptions) {
	env := map[string]string{}

	// defaults
	opt = GdshOptions{
		List:         "default",
		Next:         false,
		Reset:        false,
		Verbose:      false,
		Comment:      "",
		InclRe:       "",
		ExclRe:       "",
		Key:          "",
		Node:         "",
		Command:      "",
		Script:       "",
		BgJob:        false,
		RemoteLog:    "",
		RemoteScript: "",
		Env:          env,
	}

	user, err := user.Current()
	if err == nil {
		opt.User = user.Username
	}

	skip := true
	cont := false
	for i, arg := range args {
		if skip {
			skip = false
			continue
		}

		switch arg {
		case "--list", "-l":
			opt.List = args[i+1]
			skip = true
		case "next", "--next":
			opt.Next = true
			cont = true
		case "reset", "--reset":
			opt.Reset = true
			cont = true
		case "--comment":
			opt.Comment = args[i+1]
			skip = true
		case "--verbose", "-v":
			opt.Verbose = true
			cont = true
		case "--key", "-i":
			opt.Key = args[i+1]
			skip = true
		case "--node", "-n":
			opt.Node = args[i+1]
			skip = true
		case "--incl":
			opt.InclRe = args[i+1]
			skip = true
		case "--excl":
			opt.ExclRe = args[i+1]
			skip = true
		case "--root":
			opt.User = "root"
			cont = true
		case "--user":
			opt.User = args[i+1]
			skip = true
		case "--help":
			printUsage()
			os.Exit(0)
		case "--env", "-e":
			parts := strings.SplitN(strings.Trim(args[i+1], " \t"), "=", 2)
			env[parts[0]] = parts[1]
			skip = true
		}

		switch command {
		case "run":
			switch arg {
			case "--command", "-c":
				opt.Command = args[i+1]
				skip = true
			case "--script", "-s":
				opt.Script = args[i+1]
				skip = true
			case "--background":
				opt.BgJob = true
				cont = true
			}
		}

		if skip || cont {
			continue
		}

		opt.Args = append(opt.Args, arg)
	}

	switch command {
	case "run":
		if opt.Command != "" && opt.Script != "" {
			log.Fatal("--script/-s and --command/-c are mutually exclusive!")
		}
	}

	log.Printf("Options: %s\n", opt)

	return
}

func (opt *GdshOptions) sshAddressList() (list []string) {
	list = []string{}
	nodes := loadListByName(opt.List)

	for _, node := range nodes {
		list = append(list, fmt.Sprintf("%s:22", node.Address))
	}

	return list
}

// vim: ts=4 sw=4 noet tw=120 softtabstop=4
<commit_msg>document sshAddressList helper<commit_after>package main

// gdsh option parsing - a custom parser is implemented because the
// few packages I checked out didn't do what I want, or ended up being
// really clunky to do some of the uglier bits of what gdsh needs

import (
	"fmt"
	"log"
	"os"
	"os/user"
	"strings"
)

type GdshOptions struct {
	List         string            // --list/-l
	Next         bool              // --next
	Reset        bool              // --reset
	Verbose      bool              // --verbose/-v
	Comment      string            // --comment
	InclRe       string            // --include
	ExclRe       string            // --exclude
	Key          string            // --key/-i
	User         string            // --user
	Node         string            // --node/-h
	Command      string            // --command/-c
	Script       string            // --script/-s
	BgJob        bool              // --background/-b
	RemoteLog    string            // --remote-log/-r
	RemoteScript string            // --remote-script-path
	Env          map[string]string // --env/-e key=val
	Args         []string          // leftover arguments for subcommands
}

var usageText = `
HELP!
`

func printUsage() {
	fmt.Printf("%s", usageText)
}

func deleteArg(index int) (args []string, deleted string) {
	args = make([]string, len(os.Args)-1)

	offset := 0
	for i := range os.Args {
		if i == index {
			deleted = os.Args[i]
			offset = 1
			continue
		}
		args[i-offset] = os.Args[i]
	}

	return
}

func parseArgs(args []string, command string) (opt GdshOptions) {
	env := map[string]string{}

	// defaults
	opt = GdshOptions{
		List:         "default",
		Next:         false,
		Reset:        false,
		Verbose:      false,
		Comment:      "",
		InclRe:       "",
		ExclRe:       "",
		Key:          "",
		Node:         "",
		Command:      "",
		Script:       "",
		BgJob:        false,
		RemoteLog:    "",
		RemoteScript: "",
		Env:          env,
	}

	user, err := user.Current()
	if err == nil {
		opt.User = user.Username
	}

	skip := true
	cont := false
	for i, arg := range args {
		if skip {
			skip = false
			continue
		}

		switch arg {
		case "--list", "-l":
			opt.List = args[i+1]
			skip = true
		case "next", "--next":
			opt.Next = true
			cont = true
		case "reset", "--reset":
			opt.Reset = true
			cont = true
		case "--comment":
			opt.Comment = args[i+1]
			skip = true
		case "--verbose", "-v":
			opt.Verbose = true
			cont = true
		case "--key", "-i":
			opt.Key = args[i+1]
			skip = true
		case "--node", "-n":
			opt.Node = args[i+1]
			skip = true
		case "--incl":
			opt.InclRe = args[i+1]
			skip = true
		case "--excl":
			opt.ExclRe = args[i+1]
			skip = true
		case "--root":
			opt.User = "root"
			cont = true
		case "--user":
			opt.User = args[i+1]
			skip = true
		case "--help":
			printUsage()
			os.Exit(0)
		case "--env", "-e":
			parts := strings.SplitN(strings.Trim(args[i+1], " \t"), "=", 2)
			env[parts[0]] = parts[1]
			skip = true
		}

		switch command {
		case "run":
			switch arg {
			case "--command", "-c":
				opt.Command = args[i+1]
				skip = true
			case "--script", "-s":
				opt.Script = args[i+1]
				skip = true
			case "--background":
				opt.BgJob = true
				cont = true
			}
		}

		if skip || cont {
			continue
		}

		opt.Args = append(opt.Args, arg)
	}

	switch command {
	case "run":
		if opt.Command != "" && opt.Script != "" {
			log.Fatal("--script/-s and --command/-c are mutually exclusive!")
		}
	}

	log.Printf("Options: %s\n", opt)

	return
}

// sshAddressList loads the requested node list and returns a string list
// of address:port strings that can be used with ssh.go
func (opt *GdshOptions) sshAddressList() (list []string) {
	list = []string{}
	nodes := loadListByName(opt.List)

	for _, node := range nodes {
		list = append(list, fmt.Sprintf("%s:22", node.Address))
	}

	return list
}

// vim: ts=4 sw=4 noet tw=120 softtabstop=4
<|endoftext|>


<commit_before>package gmf

/*

#cgo pkg-config: libavutil

#include <stdlib.h>
#include "libavutil/opt.h"

*/
import "C"

import (
	"log"
	"reflect"
	"unsafe"
)

type Option struct {
	Key string
	Val interface{}
}

func (this Option) Set(ctx interface{}) {
	ckey := C.CString(this.Key)
	defer C.free(unsafe.Pointer(ckey))

	var ret int = 0

	switch t := this.Val.(type) {
	case int:
		log.Printf("===set as int\n")
		ret = int(C.av_opt_set_int(unsafe.Pointer(reflect.ValueOf(ctx).Pointer()), ckey, C.int64_t(this.Val.(int)), C.AV_OPT_SEARCH_CHILDREN))

	case SampleFmt:
		ret = int(C.av_opt_set_sample_fmt(unsafe.Pointer(reflect.ValueOf(ctx).Pointer()), ckey, (int32)(this.Val.(SampleFmt)), C.AV_OPT_SEARCH_CHILDREN))

	case float64:
		log.Printf("===set as float\n")
		ret = int(C.av_opt_set_double(unsafe.Pointer(reflect.ValueOf(ctx).Pointer()), ckey, (C.double)(this.Val.(float64)), C.AV_OPT_SEARCH_CHILDREN))

	case AVR:
		ret = int(C.av_opt_set_q(unsafe.Pointer(reflect.ValueOf(ctx).Pointer()), ckey, C.AVRational(this.Val.(AVR).AVRational()), C.AV_OPT_SEARCH_CHILDREN))

	case []byte:
		ret = int(C.av_opt_set_bin(unsafe.Pointer(reflect.ValueOf(ctx).Pointer()), ckey, (*C.uint8_t)(unsafe.Pointer(&this.Val.([]byte)[0])), C.int(len(ctx.([]byte))), C.AV_OPT_SEARCH_CHILDREN))

	case string:
		cval := C.CString(this.Val.(string))
		defer C.free(unsafe.Pointer(cval))
		ret = int(C.av_opt_set(unsafe.Pointer(reflect.ValueOf(ctx).Pointer()), ckey, cval, C.AV_OPT_SEARCH_CHILDREN))

	case *Dict:
		ret = int(C.av_opt_set_dict(unsafe.Pointer(reflect.ValueOf(ctx).Pointer()), &this.Val.(*Dict).avDict))

	default:
		log.Println("unsupported type:", t)
	}

	if ret < 0 {
		log.Printf("unable to set key '%s' value '%v', error: %s\n", this.Key, this.Val, AvError(int(ret)))
	}
}
<commit_msg>info messages removed<commit_after>package gmf

/*

#cgo pkg-config: libavutil

#include <stdlib.h>
#include "libavutil/opt.h"

*/
import "C"

import (
	"log"
	"reflect"
	"unsafe"
)

type Option struct {
	Key string
	Val interface{}
}

func (this Option) Set(ctx interface{}) {
	ckey := C.CString(this.Key)
	defer C.free(unsafe.Pointer(ckey))

	var ret int = 0

	switch t := this.Val.(type) {
	case int:
		ret = int(C.av_opt_set_int(unsafe.Pointer(reflect.ValueOf(ctx).Pointer()), ckey, C.int64_t(this.Val.(int)), C.AV_OPT_SEARCH_CHILDREN))

	case SampleFmt:
		ret = int(C.av_opt_set_sample_fmt(unsafe.Pointer(reflect.ValueOf(ctx).Pointer()), ckey, (int32)(this.Val.(SampleFmt)), C.AV_OPT_SEARCH_CHILDREN))

	case float64:
		ret = int(C.av_opt_set_double(unsafe.Pointer(reflect.ValueOf(ctx).Pointer()), ckey, (C.double)(this.Val.(float64)), C.AV_OPT_SEARCH_CHILDREN))

	case AVR:
		ret = int(C.av_opt_set_q(unsafe.Pointer(reflect.ValueOf(ctx).Pointer()), ckey, C.AVRational(this.Val.(AVR).AVRational()), C.AV_OPT_SEARCH_CHILDREN))

	case []byte:
		ret = int(C.av_opt_set_bin(unsafe.Pointer(reflect.ValueOf(ctx).Pointer()), ckey, (*C.uint8_t)(unsafe.Pointer(&this.Val.([]byte)[0])), C.int(len(ctx.([]byte))), C.AV_OPT_SEARCH_CHILDREN))

	case string:
		cval := C.CString(this.Val.(string))
		defer C.free(unsafe.Pointer(cval))
		ret = int(C.av_opt_set(unsafe.Pointer(reflect.ValueOf(ctx).Pointer()), ckey, cval, C.AV_OPT_SEARCH_CHILDREN))

	case *Dict:
		ret = int(C.av_opt_set_dict(unsafe.Pointer(reflect.ValueOf(ctx).Pointer()), &this.Val.(*Dict).avDict))

	default:
		log.Println("unsupported type:", t)
	}

	if ret < 0 {
		log.Printf("unable to set key '%s' value '%v', error: %s\n", this.Key, this.Val, AvError(int(ret)))
	}
}
<|endoftext|>


<commit_before>package reedsolomon

import (
	"runtime"

	"github.com/klauspost/cpuid"
)

// Option allows to override processing parameters.
type Option func(*options)

type options struct {
	maxGoroutines     int
	minSplitSize      int
	useAVX2, useSSSE3 bool
}

var defaultOptions = options{
	maxGoroutines: 50,
	minSplitSize:  512,
}

func init() {
	if runtime.GOMAXPROCS(0) <= 1 {
		defaultOptions.maxGoroutines = 1
	}
	// Detect CPU capabilities.
	defaultOptions.useSSSE3 = cpuid.CPU.SSSE3()
	defaultOptions.useAVX2 = cpuid.CPU.AVX2()
}

// WithMaxGoroutines is the maximum number of goroutines number for encoding & decoding.
// Jobs will be split into this many parts, unless each goroutine would have to process
// less than minSplitSize bytes (set with WithMinSplitSize).
// For the best speed, keep this well above the GOMAXPROCS number for more fine grained
// scheduling.
// If n <= 0, it is ignored.
func WithMaxGoroutines(n int) Option {
	return func(o *options) {
		if n > 0 {
			o.maxGoroutines = n
		}
	}
}

// MinSplitSize Is the minimum encoding size in bytes per goroutine.
// See WithMaxGoroutines on how jobs are split.
// If n <= 0, it is ignored.
func WithMinSplitSize(n int) Option {
	return func(o *options) {
		if n > 0 {
			o.maxGoroutines = n
		}
	}
}

func withSSE3(enabled bool) Option {
	return func(o *options) {
		o.useSSSE3 = enabled
	}
}

func withAVX2(enabled bool) Option {
	return func(o *options) {
		o.useAVX2 = enabled
	}
}
<commit_msg>Set correct field in WithMinSplitSize<commit_after>package reedsolomon

import (
	"runtime"

	"github.com/klauspost/cpuid"
)

// Option allows to override processing parameters.
type Option func(*options)

type options struct {
	maxGoroutines     int
	minSplitSize      int
	useAVX2, useSSSE3 bool
}

var defaultOptions = options{
	maxGoroutines: 50,
	minSplitSize:  512,
}

func init() {
	if runtime.GOMAXPROCS(0) <= 1 {
		defaultOptions.maxGoroutines = 1
	}
	// Detect CPU capabilities.
	defaultOptions.useSSSE3 = cpuid.CPU.SSSE3()
	defaultOptions.useAVX2 = cpuid.CPU.AVX2()
}

// WithMaxGoroutines is the maximum number of goroutines number for encoding & decoding.
// Jobs will be split into this many parts, unless each goroutine would have to process
// less than minSplitSize bytes (set with WithMinSplitSize).
// For the best speed, keep this well above the GOMAXPROCS number for more fine grained
// scheduling.
// If n <= 0, it is ignored.
func WithMaxGoroutines(n int) Option {
	return func(o *options) {
		if n > 0 {
			o.maxGoroutines = n
		}
	}
}

// MinSplitSize Is the minimum encoding size in bytes per goroutine.
// See WithMaxGoroutines on how jobs are split.
// If n <= 0, it is ignored.
func WithMinSplitSize(n int) Option {
	return func(o *options) {
		if n > 0 {
			o.minSplitSize = n
		}
	}
}

func withSSE3(enabled bool) Option {
	return func(o *options) {
		o.useSSSE3 = enabled
	}
}

func withAVX2(enabled bool) Option {
	return func(o *options) {
		o.useAVX2 = enabled
	}
}
<|endoftext|>


<commit_before>package redis

import (
	"crypto/tls"
	"errors"
	"fmt"
	"net"
	"net/url"
	"strconv"
	"strings"
	"time"

	"github.com/go-redis/redis/internal/pool"
)

type Options struct {
	// The network type, either tcp or unix.
	// Default is tcp.
	Network string
	// host:port address.
	Addr string

	// Dialer creates new network connection and has priority over
	// Network and Addr options.
	Dialer func() (net.Conn, error)

	// Hook that is called when new connection is established.
	OnConnect func(*Conn) error

	// Optional password. Must match the password specified in the
	// requirepass server configuration option.
	Password string
	// Database to be selected after connecting to the server.
	DB int

	// Maximum number of retries before giving up.
	// Default is to not retry failed commands.
	MaxRetries int

	// Retry using exponential backoff wait algorithm between each retry
	// Default is 512 milliseconds; set to -1 to disable any backoff sleep
	MaxRetryBackoff time.Duration

	// Dial timeout for establishing new connections.
	// Default is 5 seconds.
	DialTimeout time.Duration
	// Timeout for socket reads. If reached, commands will fail
	// with a timeout instead of blocking.
	// Default is 3 seconds.
	ReadTimeout time.Duration
	// Timeout for socket writes. If reached, commands will fail
	// with a timeout instead of blocking.
	// Default is ReadTimeout.
	WriteTimeout time.Duration

	// Maximum number of socket connections.
	// Default is 10 connections.
	PoolSize int
	// Amount of time client waits for connection if all connections
	// are busy before returning an error.
	// Default is ReadTimeout + 1 second.
	PoolTimeout time.Duration
	// Amount of time after which client closes idle connections.
	// Should be less than server's timeout.
	// Default is 5 minutes.
	IdleTimeout time.Duration
	// Frequency of idle checks.
	// Default is 1 minute.
	// When minus value is set, then idle check is disabled.
	IdleCheckFrequency time.Duration

	// Enables read only queries on slave nodes.
	ReadOnly bool

	// TLS Config to use. When set TLS will be negotiated.
	TLSConfig *tls.Config
}

func (opt *Options) init() {
	if opt.Network == "" {
		opt.Network = "tcp"
	}
	if opt.Dialer == nil {
		opt.Dialer = func() (net.Conn, error) {
			conn, err := net.DialTimeout(opt.Network, opt.Addr, opt.DialTimeout)
			if opt.TLSConfig == nil || err != nil {
				return conn, err
			}
			t := tls.Client(conn, opt.TLSConfig)
			return t, t.Handshake()
		}
	}
	if opt.PoolSize == 0 {
		opt.PoolSize = 10
	}
	if opt.DialTimeout == 0 {
		opt.DialTimeout = 5 * time.Second
	}
	switch opt.ReadTimeout {
	case -1:
		opt.ReadTimeout = 0
	case 0:
		opt.ReadTimeout = 3 * time.Second
	}
	switch opt.WriteTimeout {
	case -1:
		opt.WriteTimeout = 0
	case 0:
		opt.WriteTimeout = opt.ReadTimeout
	}
	if opt.PoolTimeout == 0 {
		opt.PoolTimeout = opt.ReadTimeout + time.Second
	}
	if opt.IdleTimeout == 0 {
		opt.IdleTimeout = 5 * time.Minute
	}
	if opt.IdleCheckFrequency == 0 {
		opt.IdleCheckFrequency = time.Minute
	}
	switch opt.MaxRetryBackoff {
	case -1:
		opt.MaxRetryBackoff = 0
	case 0:
		opt.MaxRetryBackoff = 512 * time.Millisecond
	}
}

// ParseURL parses a redis URL into options that can be used to connect to redis
func ParseURL(redisURL string) (*Options, error) {
	o := &Options{Network: "tcp"}
	u, err := url.Parse(redisURL)
	if err != nil {
		return nil, err
	}

	if u.Scheme != "redis" && u.Scheme != "rediss" {
		return nil, errors.New("invalid redis URL scheme: " + u.Scheme)
	}

	if u.User != nil {
		if p, ok := u.User.Password(); ok {
			o.Password = p
		}
	}

	if len(u.Query()) > 0 {
		return nil, errors.New("no options supported")
	}

	h, p, err := net.SplitHostPort(u.Host)
	if err != nil {
		h = u.Host
	}
	if h == "" {
		h = "localhost"
	}
	if p == "" {
		p = "6379"
	}
	o.Addr = net.JoinHostPort(h, p)

	f := strings.FieldsFunc(u.Path, func(r rune) bool {
		return r == '/'
	})
	switch len(f) {
	case 0:
		o.DB = 0
	case 1:
		if o.DB, err = strconv.Atoi(f[0]); err != nil {
			return nil, fmt.Errorf("invalid redis database number: %q", f[0])
		}
	default:
		return nil, errors.New("invalid redis URL path: " + u.Path)
	}

	if u.Scheme == "rediss" {
		o.TLSConfig = &tls.Config{ServerName: h}
	}
	return o, nil
}

func newConnPool(opt *Options) *pool.ConnPool {
	return pool.NewConnPool(
		opt.Dialer,
		opt.PoolSize,
		opt.PoolTimeout,
		opt.IdleTimeout,
		opt.IdleCheckFrequency,
	)
}

// PoolStats contains pool state information and accumulated stats.
type PoolStats struct {
	Requests uint32 // number of times a connection was requested by the pool
	Hits     uint32 // number of times free connection was found in the pool
	Timeouts uint32 // number of times a wait timeout occurred

	TotalConns uint32 // the number of total connections in the pool
	FreeConns  uint32 // the number of free connections in the pool
}
<commit_msg>Simplify doc wording<commit_after>package redis

import (
	"crypto/tls"
	"errors"
	"fmt"
	"net"
	"net/url"
	"strconv"
	"strings"
	"time"

	"github.com/go-redis/redis/internal/pool"
)

type Options struct {
	// The network type, either tcp or unix.
	// Default is tcp.
	Network string
	// host:port address.
	Addr string

	// Dialer creates new network connection and has priority over
	// Network and Addr options.
	Dialer func() (net.Conn, error)

	// Hook that is called when new connection is established.
	OnConnect func(*Conn) error

	// Optional password. Must match the password specified in the
	// requirepass server configuration option.
	Password string
	// Database to be selected after connecting to the server.
	DB int

	// Maximum number of retries before giving up.
	// Default is to not retry failed commands.
	MaxRetries int

	// Maximum backoff between each retry.
	// Default is 512 seconds; -1 disables backoff.
	MaxRetryBackoff time.Duration

	// Dial timeout for establishing new connections.
	// Default is 5 seconds.
	DialTimeout time.Duration
	// Timeout for socket reads. If reached, commands will fail
	// with a timeout instead of blocking.
	// Default is 3 seconds.
	ReadTimeout time.Duration
	// Timeout for socket writes. If reached, commands will fail
	// with a timeout instead of blocking.
	// Default is ReadTimeout.
	WriteTimeout time.Duration

	// Maximum number of socket connections.
	// Default is 10 connections.
	PoolSize int
	// Amount of time client waits for connection if all connections
	// are busy before returning an error.
	// Default is ReadTimeout + 1 second.
	PoolTimeout time.Duration
	// Amount of time after which client closes idle connections.
	// Should be less than server's timeout.
	// Default is 5 minutes.
	IdleTimeout time.Duration
	// Frequency of idle checks.
	// Default is 1 minute.
	// When minus value is set, then idle check is disabled.
	IdleCheckFrequency time.Duration

	// Enables read only queries on slave nodes.
	ReadOnly bool

	// TLS Config to use. When set TLS will be negotiated.
	TLSConfig *tls.Config
}

func (opt *Options) init() {
	if opt.Network == "" {
		opt.Network = "tcp"
	}
	if opt.Dialer == nil {
		opt.Dialer = func() (net.Conn, error) {
			conn, err := net.DialTimeout(opt.Network, opt.Addr, opt.DialTimeout)
			if opt.TLSConfig == nil || err != nil {
				return conn, err
			}
			t := tls.Client(conn, opt.TLSConfig)
			return t, t.Handshake()
		}
	}
	if opt.PoolSize == 0 {
		opt.PoolSize = 10
	}
	if opt.DialTimeout == 0 {
		opt.DialTimeout = 5 * time.Second
	}
	switch opt.ReadTimeout {
	case -1:
		opt.ReadTimeout = 0
	case 0:
		opt.ReadTimeout = 3 * time.Second
	}
	switch opt.WriteTimeout {
	case -1:
		opt.WriteTimeout = 0
	case 0:
		opt.WriteTimeout = opt.ReadTimeout
	}
	if opt.PoolTimeout == 0 {
		opt.PoolTimeout = opt.ReadTimeout + time.Second
	}
	if opt.IdleTimeout == 0 {
		opt.IdleTimeout = 5 * time.Minute
	}
	if opt.IdleCheckFrequency == 0 {
		opt.IdleCheckFrequency = time.Minute
	}
	switch opt.MaxRetryBackoff {
	case -1:
		opt.MaxRetryBackoff = 0
	case 0:
		opt.MaxRetryBackoff = 512 * time.Millisecond
	}
}

// ParseURL parses a redis URL into options that can be used to connect to redis
func ParseURL(redisURL string) (*Options, error) {
	o := &Options{Network: "tcp"}
	u, err := url.Parse(redisURL)
	if err != nil {
		return nil, err
	}

	if u.Scheme != "redis" && u.Scheme != "rediss" {
		return nil, errors.New("invalid redis URL scheme: " + u.Scheme)
	}

	if u.User != nil {
		if p, ok := u.User.Password(); ok {
			o.Password = p
		}
	}

	if len(u.Query()) > 0 {
		return nil, errors.New("no options supported")
	}

	h, p, err := net.SplitHostPort(u.Host)
	if err != nil {
		h = u.Host
	}
	if h == "" {
		h = "localhost"
	}
	if p == "" {
		p = "6379"
	}
	o.Addr = net.JoinHostPort(h, p)

	f := strings.FieldsFunc(u.Path, func(r rune) bool {
		return r == '/'
	})
	switch len(f) {
	case 0:
		o.DB = 0
	case 1:
		if o.DB, err = strconv.Atoi(f[0]); err != nil {
			return nil, fmt.Errorf("invalid redis database number: %q", f[0])
		}
	default:
		return nil, errors.New("invalid redis URL path: " + u.Path)
	}

	if u.Scheme == "rediss" {
		o.TLSConfig = &tls.Config{ServerName: h}
	}
	return o, nil
}

func newConnPool(opt *Options) *pool.ConnPool {
	return pool.NewConnPool(
		opt.Dialer,
		opt.PoolSize,
		opt.PoolTimeout,
		opt.IdleTimeout,
		opt.IdleCheckFrequency,
	)
}

// PoolStats contains pool state information and accumulated stats.
type PoolStats struct {
	Requests uint32 // number of times a connection was requested by the pool
	Hits     uint32 // number of times free connection was found in the pool
	Timeouts uint32 // number of times a wait timeout occurred

	TotalConns uint32 // the number of total connections in the pool
	FreeConns  uint32 // the number of free connections in the pool
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"go/build"
	"os"
	"path/filepath"
	"strings"
	"text/template"
)

// A Package holds the typedef, function, and enum definitions for a Go package.
type Package struct {
	Name    string
	API     string
	Version Version
	Profile string

	Typedefs  []*Typedef
	Enums     map[string]*Enum
	Functions map[string]*PackageFunction

	SpecRev string
}

// A PackageFunction is a package-specific Function wrapper.
type PackageFunction struct {
	Function
	Required bool
	Doc      string
}

// Dir returns the directory to which the Go package files are written.
func (pkg *Package) Dir() string {
	apiPrefix := pkg.API
	if pkg.Profile != "" {
		apiPrefix = pkg.API + "-" + pkg.Profile
	}
	return filepath.Join(apiPrefix, pkg.Version.String(), pkg.Name)
}

// UniqueName returns a globally unique Go-compatible name for thie package.
func (pkg *Package) UniqueName() string {
	version := strings.Replace(pkg.Version.String(), ".", "", -1)
	return fmt.Sprintf("%s%s%s", pkg.API, pkg.Profile, version)
}

// GeneratePackage writes a Go package file.
func (pkg *Package) GeneratePackage() error {
	dir := pkg.Dir()
	if err := os.MkdirAll(dir, 0755); err != nil {
		return err
	}

	if err := pkg.generateFile("package", dir); err != nil {
		return err
	}
	if err := pkg.generateFile("conversions", dir); err != nil {
		return err
	}
	if pkg.HasDebugCallbackFeature() {
		if err := pkg.generateFile("debug", dir); err != nil {
			return err
		}
	}

	return nil
}

func (pkg *Package) generateFile(file, dir string) error {
	out, err := os.Create(filepath.Join(dir, file+".go"))
	if err != nil {
		return err
	}
	defer out.Close()

	fns := template.FuncMap{
		"replace": Replace,
		"toUpper": strings.ToUpper,
	}
	filePath := pkgPath(file + ".tmpl")
	tmpl := template.Must(template.New(file + ".tmpl").Funcs(fns).ParseFiles(filePath))
	return tmpl.Execute(out, pkg)
}

// HasDebugCallbackFeature returns whether this package exposes the ability to
// set a debug callback. Used to determine whether to include the necessary
// GL-specific callback code.
func (pkg *Package) HasDebugCallbackFeature() bool {
	for _, fn := range pkg.Functions {
		for _, param := range fn.Parameters {
			if param.Type.IsDebugProc() {
				return true
			}
		}
	}
	return false
}

// HasRequiredFunctions returns true if at least one function in this package
// is required.
func (pkg *Package) HasRequiredFunctions() bool {
	for _, fn := range pkg.Functions {
		if fn.Required {
			return true
		}
	}
	return false
}

// This package's directory (relative to $GOPATH).
const pkgDir = "src/github.com/go-gl/glow"

// Cached absolute path to this package's directory (determined in the pkgPath
// function below).
var absPkgDir string

// pkgPath resolves a path relative to the package directory (see above).
func pkgPath(relPath string) string {
	if len(absPkgDir) == 0 {
		// Find absolute path to this package's directory.
		for _, path := range filepath.SplitList(build.Default.GOPATH) {
			path = filepath.Join(path, pkgDir)
			if _, err := os.Stat(path); err == nil {
				absPkgDir = path
				break
			}
		}
	}
	return filepath.Join(absPkgDir, relPath)
}

func Replace(s, old, new string) string {
	if s[:len(s)] != "\n" {
		s+="\n"
	}
	return strings.Replace(s, old, new, -1)
}
<commit_msg>Fix formatting in package.go<commit_after>package main

import (
	"fmt"
	"go/build"
	"os"
	"path/filepath"
	"strings"
	"text/template"
)

// A Package holds the typedef, function, and enum definitions for a Go package.
type Package struct {
	Name    string
	API     string
	Version Version
	Profile string

	Typedefs  []*Typedef
	Enums     map[string]*Enum
	Functions map[string]*PackageFunction

	SpecRev string
}

// A PackageFunction is a package-specific Function wrapper.
type PackageFunction struct {
	Function
	Required bool
	Doc      string
}

// Dir returns the directory to which the Go package files are written.
func (pkg *Package) Dir() string {
	apiPrefix := pkg.API
	if pkg.Profile != "" {
		apiPrefix = pkg.API + "-" + pkg.Profile
	}
	return filepath.Join(apiPrefix, pkg.Version.String(), pkg.Name)
}

// UniqueName returns a globally unique Go-compatible name for thie package.
func (pkg *Package) UniqueName() string {
	version := strings.Replace(pkg.Version.String(), ".", "", -1)
	return fmt.Sprintf("%s%s%s", pkg.API, pkg.Profile, version)
}

// GeneratePackage writes a Go package file.
func (pkg *Package) GeneratePackage() error {
	dir := pkg.Dir()
	if err := os.MkdirAll(dir, 0755); err != nil {
		return err
	}

	if err := pkg.generateFile("package", dir); err != nil {
		return err
	}
	if err := pkg.generateFile("conversions", dir); err != nil {
		return err
	}
	if pkg.HasDebugCallbackFeature() {
		if err := pkg.generateFile("debug", dir); err != nil {
			return err
		}
	}

	return nil
}

func (pkg *Package) generateFile(file, dir string) error {
	out, err := os.Create(filepath.Join(dir, file+".go"))
	if err != nil {
		return err
	}
	defer out.Close()

	fns := template.FuncMap{
		"replace": Replace,
		"toUpper": strings.ToUpper,
	}
	filePath := pkgPath(file + ".tmpl")
	tmpl := template.Must(template.New(file + ".tmpl").Funcs(fns).ParseFiles(filePath))
	return tmpl.Execute(out, pkg)
}

// HasDebugCallbackFeature returns whether this package exposes the ability to
// set a debug callback. Used to determine whether to include the necessary
// GL-specific callback code.
func (pkg *Package) HasDebugCallbackFeature() bool {
	for _, fn := range pkg.Functions {
		for _, param := range fn.Parameters {
			if param.Type.IsDebugProc() {
				return true
			}
		}
	}
	return false
}

// HasRequiredFunctions returns true if at least one function in this package
// is required.
func (pkg *Package) HasRequiredFunctions() bool {
	for _, fn := range pkg.Functions {
		if fn.Required {
			return true
		}
	}
	return false
}

// This package's directory (relative to $GOPATH).
const pkgDir = "src/github.com/go-gl/glow"

// Cached absolute path to this package's directory (determined in the pkgPath
// function below).
var absPkgDir string

// pkgPath resolves a path relative to the package directory (see above).
func pkgPath(relPath string) string {
	if len(absPkgDir) == 0 {
		// Find absolute path to this package's directory.
		for _, path := range filepath.SplitList(build.Default.GOPATH) {
			path = filepath.Join(path, pkgDir)
			if _, err := os.Stat(path); err == nil {
				absPkgDir = path
				break
			}
		}
	}
	return filepath.Join(absPkgDir, relPath)
}

func Replace(s, old, new string) string {
	if s[:len(s)] != "\n" {
		s += "\n"
	}
	return strings.Replace(s, old, new, -1)
}
<|endoftext|>


<commit_before>package main

import (
	"archive/zip"
	"fmt"
	"io"
	"log"
	"os"
	"path"
)

var authorCertificateFile string      // author certificate file
var authorCertificatePass string      // author certificate password
var distributorCertificateFile string // distributor certificate file
var distributorCertificatePass string // distributor certificate passowrd

var packageCmd = &Command{
	Run:       MakePkg,
	Name:      "package",
	Short:     "build & package Tizen project.",
	UsageLine: "",
	Long: `
	`,
}

const (
	BinDir    = "bin"
	SharedDir = "shared"
)

func init() {
	packageCmd.Flag.StringVar(&authorCertificateFile, "author-cert", "", "Security profile used to sign package")
	packageCmd.Flag.StringVar(&authorCertificatePass, "author-passwd", "", "Security profile used to sign package")
	packageCmd.Flag.StringVar(&distributorCertificateFile, "dist-cert", "", "Security profile used to sign package")
	packageCmd.Flag.StringVar(&distributorCertificatePass, "dist-passwd", "", "Security profile used to sign package")
}

// makeFileListFromAppsList parses list of Application structure
// and constructs list of DiskFiles
func makeFileListFromAppsList(list []Application) (files []PackageFile, err error) {
	for _, p := range list {
		// 1. Binary files
		if p.Exec != "" {
			df, err := NewDiskFile(path.Join(BinDir, p.Exec))
			if err != nil {
				return nil, err
			}
			files = append(files, df)
		}
		// 2. Icons
		if p.Icon != "" {
			df, err := NewDiskFile(path.Join(SharedDir, p.Icon))
			if err != nil {
				return nil, err
			}
			files = append(files, df)
		}
	}
	return files, nil
}

// makeFileList creates a list of files to be packed into tpk package.
// gotizen do not use any hand-crafted build configuration files like Makfile,
// CMakeList.txt or similar, so only source of information about package files
// is tizen-manifest.xml
func makeFileList(manifest *TizenManifest) (files []PackageFile, err error) {
	list, err := makeFileListFromAppsList(manifest.UIAppEntries)
	if err != nil {
		return nil, err
	}
	files = append(files, list...)
	list, err = makeFileListFromAppsList(manifest.ServiceAppEntries)
	if err != nil {
		return nil, err
	}
	files = append(files, list...)

	// append manifest itself
	files = append(files, manifest)

	// append author signature
	authorSignature, err := createSignature(AuthorSignature, files)
	if err != nil {
		return nil, err
	}
	files = append(files, authorSignature)

	// append distributor signature
	distributorSignature, err := createSignature(DistributorSignature, files)
	if err != nil {
		return nil, err
	}
	files = append(files, distributorSignature)

	return files, nil
}

// writePackageFiles creates new zip package
// and writes raw byte content if files into 'out' writer.
func writePackageFiles(files []PackageFile, out io.Writer) error {
	arch := zip.NewWriter(out)
	defer arch.Close()

	for _, file := range files {
		w, err := arch.Create(file.PackagePath())
		if err != nil {
			return fmt.Errorf("Unable to create archive\n%v", err)
		}
		bytes, err := file.MarshalBinary()
		if err != nil {
			return fmt.Errorf("Unable to get binary data\n%v", err)
		}
		_, err = w.Write(bytes)
		if err != nil {
			return fmt.Errorf("Write failed\n%v", err)
		}
	}
	return nil
}

func createSignature(name SignatureType, files []PackageFile) (*Signature, error) {
	s, err := NewSignature(name, files)
	if err != nil {
		return nil, err
	}

	if name == AuthorSignature {
		s.AuthorCertificate = authorCertificateFile
		s.AuthorPass = authorCertificatePass
	} else if name == DistributorSignature {
		s.AuthorCertificate = distributorCertificateFile
		s.AuthorPass = distributorCertificatePass
	}

	return s, nil
}

func MakePkg(context *Context) {
	if context.Manifest == nil {
		log.Fatal("No manifest file found in ", context.ProjectRootPath)
	}
	zip, err := os.OpenFile(context.Manifest.PackageName+".tpk", os.O_CREATE|os.O_RDWR, 0644)
	if err != nil {
		log.Fatal("Unable to create 'tpk' file: ", err)
	}
	defer zip.Close()

	all_files, err := makeFileList(context.Manifest)
	if err != nil {
		log.Fatal("Failed to create files list\n", err)
	}

	err = writePackageFiles(all_files, zip)
	if err != nil {
		log.Fatalf("Unable to create '%s' file\n%v", zip.Name(), err)
	}
	fmt.Printf("Created %s in %s\n", zip.Name(), context.ProjectRootPath)
}
<commit_msg>pacakge: fix file access<commit_after>package main

import (
	"archive/zip"
	"fmt"
	"io"
	"log"
	"os"
	"path"
)

var authorCertificateFile string      // author certificate file
var authorCertificatePass string      // author certificate password
var distributorCertificateFile string // distributor certificate file
var distributorCertificatePass string // distributor certificate passowrd

var packageCmd = &Command{
	Run:       MakePkg,
	Name:      "package",
	Short:     "build & package Tizen project.",
	UsageLine: "",
	Long: `
	`,
}

const (
	BinDir    = "bin"
	SharedDir = "shared"
)

func init() {
	packageCmd.Flag.StringVar(&authorCertificateFile, "author-cert", "", "Security profile used to sign package")
	packageCmd.Flag.StringVar(&authorCertificatePass, "author-passwd", "", "Security profile used to sign package")
	packageCmd.Flag.StringVar(&distributorCertificateFile, "dist-cert", "", "Security profile used to sign package")
	packageCmd.Flag.StringVar(&distributorCertificatePass, "dist-passwd", "", "Security profile used to sign package")
}

// makeFileListFromAppsList parses list of Application structure
// and constructs list of DiskFiles
func makeFileListFromAppsList(list []Application) (files []PackageFile, err error) {
	for _, p := range list {
		// 1. Binary files
		if p.Exec != "" {
			df, err := NewDiskFile(path.Join(BinDir, p.Exec))
			if err != nil {
				return nil, err
			}
			files = append(files, df)
		}
		// 2. Icons
		if p.Icon != "" {
			df, err := NewDiskFile(path.Join(SharedDir, p.Icon))
			if err != nil {
				return nil, err
			}
			files = append(files, df)
		}
	}
	return files, nil
}

// makeFileList creates a list of files to be packed into tpk package.
// gotizen do not use any hand-crafted build configuration files like Makfile,
// CMakeList.txt or similar, so only source of information about package files
// is tizen-manifest.xml
func makeFileList(manifest *TizenManifest) (files []PackageFile, err error) {
	list, err := makeFileListFromAppsList(manifest.UIAppEntries)
	if err != nil {
		return nil, err
	}
	files = append(files, list...)
	list, err = makeFileListFromAppsList(manifest.ServiceAppEntries)
	if err != nil {
		return nil, err
	}
	files = append(files, list...)

	// append manifest itself
	files = append(files, manifest)

	// append author signature
	authorSignature, err := createSignature(AuthorSignature, files)
	if err != nil {
		return nil, err
	}
	files = append(files, authorSignature)

	// append distributor signature
	distributorSignature, err := createSignature(DistributorSignature, files)
	if err != nil {
		return nil, err
	}
	files = append(files, distributorSignature)

	return files, nil
}

// writePackageFiles creates new zip package
// and writes raw byte content if files into 'out' writer.
func writePackageFiles(files []PackageFile, out io.Writer) error {
	arch := zip.NewWriter(out)
	defer arch.Close()

	for _, file := range files {
		w, err := arch.Create(file.PackagePath())
		if err != nil {
			return fmt.Errorf("Unable to create archive\n%v", err)
		}
		bytes, err := file.MarshalBinary()
		if err != nil {
			return fmt.Errorf("Unable to get binary data\n%v", err)
		}
		_, err = w.Write(bytes)
		if err != nil {
			return fmt.Errorf("Write failed\n%v", err)
		}
	}
	return nil
}

func createSignature(name SignatureType, files []PackageFile) (*Signature, error) {
	s, err := NewSignature(name, files)
	if err != nil {
		return nil, err
	}

	if name == AuthorSignature {
		s.AuthorCertificate = authorCertificateFile
		s.AuthorPass = authorCertificatePass
	} else if name == DistributorSignature {
		s.AuthorCertificate = distributorCertificateFile
		s.AuthorPass = distributorCertificatePass
	}

	return s, nil
}

func MakePkg(context *Context) {
	if context.Manifest == nil {
		log.Fatal("No manifest file found in ", context.ProjectRootPath)
	}
	zip, err := os.Create(context.Manifest.PackageName + ".tpk")
	if err != nil {
		log.Fatal("Unable to create 'tpk' file: ", err)
	}
	defer zip.Close()

	all_files, err := makeFileList(context.Manifest)
	if err != nil {
		log.Fatal("Failed to create files list\n", err)
	}

	err = writePackageFiles(all_files, zip)
	if err != nil {
		log.Fatalf("Unable to create '%s' file\n%v", zip.Name(), err)
	}
	fmt.Printf("Created %s in %s\n", zip.Name(), context.ProjectRootPath)
}
<|endoftext|>


<commit_before>package ai

import (
	"bytes"
	"log"
	"math/rand"
	"time"

	"github.com/nelhage/taktician/bitboard"
	"github.com/nelhage/taktician/ptn"
	"github.com/nelhage/taktician/tak"
)

const (
	maxEval      int64 = 1 << 30
	minEval            = -maxEval
	winThreshold       = 1 << 29

	tableSize uint64 = (1 << 20)
)

type EvaluationFunc func(m *MinimaxAI, p *tak.Position) int64

type MinimaxAI struct {
	cfg  MinimaxConfig
	rand *rand.Rand

	st      Stats
	c       bitboard.Constants
	regions []uint64

	evaluate EvaluationFunc

	table []tableEntry
}

type tableEntry struct {
	hash  uint64
	depth int
	value int64
	bound boundType
	m     tak.Move
	p     *tak.Position
}

type boundType byte

const (
	lowerBound = iota
	exactBound = iota
	upperBound = iota
)

type Stats struct {
	Depth     int
	Generated uint64
	Evaluated uint64
	Cutoffs   uint64
	TTHits    uint64
}

type MinimaxConfig struct {
	Size  int
	Depth int
	Debug int
	Seed  int64

	Evaluate EvaluationFunc
}

func NewMinimax(cfg MinimaxConfig) *MinimaxAI {
	m := &MinimaxAI{cfg: cfg}
	m.precompute()
	m.evaluate = cfg.Evaluate
	if m.evaluate == nil {
		m.evaluate = DefaultEvaluate
	}
	m.table = make([]tableEntry, tableSize)
	return m
}

func (m *MinimaxAI) ttGet(h uint64) *tableEntry {
	te := &m.table[h%tableSize]
	if te.hash != h {
		return nil
	}
	return te
}

func (m *MinimaxAI) ttPut(t *tableEntry) {
	m.table[t.hash%tableSize] = *t
}

func (m *MinimaxAI) precompute() {
	s := uint(m.cfg.Size)
	m.c = bitboard.Precompute(s)
}

func formatpv(ms []tak.Move) string {
	var out bytes.Buffer
	out.WriteString("[")
	for i, m := range ms {
		if i != 0 {
			out.WriteString(" ")
		}
		out.WriteString(ptn.FormatMove(&m))
	}
	out.WriteString("]")
	return out.String()
}

func (m *MinimaxAI) GetMove(p *tak.Position, limit time.Duration) tak.Move {
	ms, _, _ := m.Analyze(p, limit)
	return ms[0]
}

func (m *MinimaxAI) Analyze(p *tak.Position, limit time.Duration) ([]tak.Move, int64, Stats) {
	if m.cfg.Size != p.Size() {
		panic("Analyze: wrong size")
	}

	var seed = m.cfg.Seed
	if seed == 0 {
		seed = time.Now().Unix()
	}
	m.rand = rand.New(rand.NewSource(seed))
	if m.cfg.Debug > 0 {
		log.Printf("seed=%d", seed)
	}

	var ms []tak.Move
	var v int64
	top := time.Now()
	var prevEval uint64
	var branchSum uint64
	base := 0
	te := m.ttGet(p.Hash())
	if te != nil && te.bound == exactBound {
		base = te.depth
		ms = []tak.Move{te.m}
	}

	for i := 1; i+base <= m.cfg.Depth; i++ {
		m.st = Stats{Depth: i + base}
		start := time.Now()
		ms, v = m.minimax(p, 0, i+base, ms, minEval-1, maxEval+1)
		timeUsed := time.Now().Sub(top)
		timeMove := time.Now().Sub(start)
		if m.cfg.Debug > 0 {
			log.Printf("[minimax] deepen: depth=%d val=%d pv=%s time=%s total=%s evaluated=%d tt=%d branch=%d",
				base+i, v, formatpv(ms),
				timeMove,
				timeUsed,
				m.st.Evaluated,
				m.st.TTHits,
				m.st.Evaluated/(prevEval+1),
			)
		}
		if i > 1 {
			branchSum += m.st.Evaluated / (prevEval + 1)
		}
		prevEval = m.st.Evaluated
		if v > winThreshold || v < -winThreshold {
			break
		}
		if i > 2 && i != m.cfg.Depth && limit != 0 {
			estimate := timeUsed + time.Now().Sub(start)*time.Duration(branchSum/uint64(i-1))
			if estimate > limit {
				if m.cfg.Debug > 0 {
					log.Printf("[minimax] time cutoff: depth=%d used=%s estimate=%s",
						i, timeUsed, estimate)
				}
				break
			}
		}
	}
	return ms, v, m.st
}

const debugTable = false

func (ai *MinimaxAI) minimax(
	p *tak.Position,
	ply, depth int,
	pv []tak.Move,
	α, β int64) ([]tak.Move, int64) {
	over, _ := p.GameOver()
	if depth == 0 || over {
		ai.st.Evaluated++
		return nil, ai.evaluate(ai, p)
	}

	moves := p.AllMoves()
	ai.st.Generated += uint64(len(moves))
	if ply == 0 {
		for i := len(moves) - 1; i > 0; i-- {
			j := ai.rand.Int31n(int32(i))
			moves[j], moves[i] = moves[i], moves[j]
		}
	}
	te := ai.ttGet(p.Hash())
	if te != nil {
		if te.depth >= depth {
			if te.bound == exactBound ||
				(te.value < α && te.bound == upperBound) ||
				(te.value > β && te.bound == lowerBound) {
				ai.st.TTHits++
				return []tak.Move{te.m}, te.value
			}
		}

		if te.bound == exactBound &&
			(te.value > winThreshold || te.value < -winThreshold) {
			ai.st.TTHits++
			return []tak.Move{te.m}, te.value
		}
	}
	if len(pv) > 0 {
		j := 1
		for i, m := range moves {
			if m.Equal(&pv[0]) {
				moves[0], moves[i] = moves[i], moves[0]
				if m.Type < tak.SlideLeft {
					break
				}

			} else if te != nil && j < len(moves) && te.m.Equal(&m) {
				moves[j], moves[i] = moves[i], moves[j]
				j++
			} else if j < len(moves) && m.X == pv[0].X && m.Y == pv[0].Y {
				moves[j], moves[i] = moves[i], moves[j]
				j++
			}
		}
	}

	best := make([]tak.Move, 0, depth)
	best = append(best, pv...)
	improved := false
	for _, m := range moves {
		child, e := p.Move(&m)
		if e != nil {
			continue
		}
		var ms []tak.Move
		var newpv []tak.Move
		var v int64
		if len(best) != 0 {
			newpv = best[1:]
		}
		ms, v = ai.minimax(child, ply+1, depth-1, newpv, -β, -α)
		v = -v
		if ai.cfg.Debug > 2 && ply == 0 {
			log.Printf("[minimax] search: depth=%d ply=%d m=%s pv=%s window=(%d,%d) ms=%s v=%d evaluated=%d",
				depth, ply, ptn.FormatMove(&m), formatpv(newpv), α, β, formatpv(ms), v, ai.st.Evaluated)
		}

		if len(best) == 0 {
			best = append(best[:0], m)
			best = append(best, ms...)
		}
		if v > α {
			improved = true
			best = append(best[:0], m)
			best = append(best, ms...)
			α = v
			if α >= β {
				ai.st.Cutoffs++
				break
			}
		}
	}

	if debugTable && te != nil &&
		te.depth == depth &&
		te.bound == exactBound &&
		!best[0].Equal(&te.m) {
		log.Printf("? ply=%d depth=%d found=[%s, %v] t=[%s, %v]",
			ply, depth,
			ptn.FormatMove(&best[0]), α,
			ptn.FormatMove(&te.m), te.value,
		)
		log.Printf(" p> %#v", p)
		log.Printf("tp> %#v", te.p)
	}

	if depth > 1 {
		te := tableEntry{
			hash:  p.Hash(),
			depth: depth,
			m:     best[0],
			value: α,
		}
		if debugTable {
			te.p = p
		}
		if !improved {
			te.bound = upperBound
		} else if α >= β {
			te.bound = lowerBound
		} else {
			te.bound = exactBound
		}
		ai.ttPut(&te)
	}

	return best, α
}
<commit_msg>conservative time estimate<commit_after>package ai

import (
	"bytes"
	"log"
	"math/rand"
	"time"

	"github.com/nelhage/taktician/bitboard"
	"github.com/nelhage/taktician/ptn"
	"github.com/nelhage/taktician/tak"
)

const (
	maxEval      int64 = 1 << 30
	minEval            = -maxEval
	winThreshold       = 1 << 29

	tableSize uint64 = (1 << 20)
)

type EvaluationFunc func(m *MinimaxAI, p *tak.Position) int64

type MinimaxAI struct {
	cfg  MinimaxConfig
	rand *rand.Rand

	st      Stats
	c       bitboard.Constants
	regions []uint64

	evaluate EvaluationFunc

	table []tableEntry
}

type tableEntry struct {
	hash  uint64
	depth int
	value int64
	bound boundType
	m     tak.Move
	p     *tak.Position
}

type boundType byte

const (
	lowerBound = iota
	exactBound = iota
	upperBound = iota
)

type Stats struct {
	Depth     int
	Generated uint64
	Evaluated uint64
	Cutoffs   uint64
	TTHits    uint64
}

type MinimaxConfig struct {
	Size  int
	Depth int
	Debug int
	Seed  int64

	Evaluate EvaluationFunc
}

func NewMinimax(cfg MinimaxConfig) *MinimaxAI {
	m := &MinimaxAI{cfg: cfg}
	m.precompute()
	m.evaluate = cfg.Evaluate
	if m.evaluate == nil {
		m.evaluate = DefaultEvaluate
	}
	m.table = make([]tableEntry, tableSize)
	return m
}

func (m *MinimaxAI) ttGet(h uint64) *tableEntry {
	te := &m.table[h%tableSize]
	if te.hash != h {
		return nil
	}
	return te
}

func (m *MinimaxAI) ttPut(t *tableEntry) {
	m.table[t.hash%tableSize] = *t
}

func (m *MinimaxAI) precompute() {
	s := uint(m.cfg.Size)
	m.c = bitboard.Precompute(s)
}

func formatpv(ms []tak.Move) string {
	var out bytes.Buffer
	out.WriteString("[")
	for i, m := range ms {
		if i != 0 {
			out.WriteString(" ")
		}
		out.WriteString(ptn.FormatMove(&m))
	}
	out.WriteString("]")
	return out.String()
}

func (m *MinimaxAI) GetMove(p *tak.Position, limit time.Duration) tak.Move {
	ms, _, _ := m.Analyze(p, limit)
	return ms[0]
}

func (m *MinimaxAI) Analyze(p *tak.Position, limit time.Duration) ([]tak.Move, int64, Stats) {
	if m.cfg.Size != p.Size() {
		panic("Analyze: wrong size")
	}

	var seed = m.cfg.Seed
	if seed == 0 {
		seed = time.Now().Unix()
	}
	m.rand = rand.New(rand.NewSource(seed))
	if m.cfg.Debug > 0 {
		log.Printf("seed=%d", seed)
	}

	var ms []tak.Move
	var v int64
	top := time.Now()
	var prevEval uint64
	var branchSum uint64
	base := 0
	te := m.ttGet(p.Hash())
	if te != nil && te.bound == exactBound {
		base = te.depth
		ms = []tak.Move{te.m}
	}

	for i := 1; i+base <= m.cfg.Depth; i++ {
		m.st = Stats{Depth: i + base}
		start := time.Now()
		ms, v = m.minimax(p, 0, i+base, ms, minEval-1, maxEval+1)
		timeUsed := time.Now().Sub(top)
		timeMove := time.Now().Sub(start)
		if m.cfg.Debug > 0 {
			log.Printf("[minimax] deepen: depth=%d val=%d pv=%s time=%s total=%s evaluated=%d tt=%d branch=%d",
				base+i, v, formatpv(ms),
				timeMove,
				timeUsed,
				m.st.Evaluated,
				m.st.TTHits,
				m.st.Evaluated/(prevEval+1),
			)
		}
		if i > 1 {
			branchSum += m.st.Evaluated / (prevEval + 1)
		}
		prevEval = m.st.Evaluated
		if v > winThreshold || v < -winThreshold {
			break
		}
		if i+base != m.cfg.Depth && limit != 0 {
			var branch uint64
			if i > 2 {
				branch = branchSum / uint64(i-1)
			} else {
				// conservative estimate if we haven't
				// run enough plies to have one
				// yet. This can matter if the table
				// returns a deep move
				branch = 20
			}
			estimate := timeUsed + time.Now().Sub(start)*time.Duration(branch)
			if estimate > limit {
				if m.cfg.Debug > 0 {
					log.Printf("[minimax] time cutoff: depth=%d used=%s estimate=%s",
						i, timeUsed, estimate)
				}
				break
			}
		}
	}
	return ms, v, m.st
}

const debugTable = false

func (ai *MinimaxAI) minimax(
	p *tak.Position,
	ply, depth int,
	pv []tak.Move,
	α, β int64) ([]tak.Move, int64) {
	over, _ := p.GameOver()
	if depth == 0 || over {
		ai.st.Evaluated++
		return nil, ai.evaluate(ai, p)
	}

	moves := p.AllMoves()
	ai.st.Generated += uint64(len(moves))
	if ply == 0 {
		for i := len(moves) - 1; i > 0; i-- {
			j := ai.rand.Int31n(int32(i))
			moves[j], moves[i] = moves[i], moves[j]
		}
	}
	te := ai.ttGet(p.Hash())
	if te != nil {
		if te.depth >= depth {
			if te.bound == exactBound ||
				(te.value < α && te.bound == upperBound) ||
				(te.value > β && te.bound == lowerBound) {
				ai.st.TTHits++
				return []tak.Move{te.m}, te.value
			}
		}

		if te.bound == exactBound &&
			(te.value > winThreshold || te.value < -winThreshold) {
			ai.st.TTHits++
			return []tak.Move{te.m}, te.value
		}
	}
	if len(pv) > 0 {
		j := 1
		for i, m := range moves {
			if m.Equal(&pv[0]) {
				moves[0], moves[i] = moves[i], moves[0]
				if m.Type < tak.SlideLeft {
					break
				}

			} else if te != nil && j < len(moves) && te.m.Equal(&m) {
				moves[j], moves[i] = moves[i], moves[j]
				j++
			} else if j < len(moves) && m.X == pv[0].X && m.Y == pv[0].Y {
				moves[j], moves[i] = moves[i], moves[j]
				j++
			}
		}
	}

	best := make([]tak.Move, 0, depth)
	best = append(best, pv...)
	improved := false
	for _, m := range moves {
		child, e := p.Move(&m)
		if e != nil {
			continue
		}
		var ms []tak.Move
		var newpv []tak.Move
		var v int64
		if len(best) != 0 {
			newpv = best[1:]
		}
		ms, v = ai.minimax(child, ply+1, depth-1, newpv, -β, -α)
		v = -v
		if ai.cfg.Debug > 2 && ply == 0 {
			log.Printf("[minimax] search: depth=%d ply=%d m=%s pv=%s window=(%d,%d) ms=%s v=%d evaluated=%d",
				depth, ply, ptn.FormatMove(&m), formatpv(newpv), α, β, formatpv(ms), v, ai.st.Evaluated)
		}

		if len(best) == 0 {
			best = append(best[:0], m)
			best = append(best, ms...)
		}
		if v > α {
			improved = true
			best = append(best[:0], m)
			best = append(best, ms...)
			α = v
			if α >= β {
				ai.st.Cutoffs++
				break
			}
		}
	}

	if debugTable && te != nil &&
		te.depth == depth &&
		te.bound == exactBound &&
		!best[0].Equal(&te.m) {
		log.Printf("? ply=%d depth=%d found=[%s, %v] t=[%s, %v]",
			ply, depth,
			ptn.FormatMove(&best[0]), α,
			ptn.FormatMove(&te.m), te.value,
		)
		log.Printf(" p> %#v", p)
		log.Printf("tp> %#v", te.p)
	}

	if depth > 1 {
		te := tableEntry{
			hash:  p.Hash(),
			depth: depth,
			m:     best[0],
			value: α,
		}
		if debugTable {
			te.p = p
		}
		if !improved {
			te.bound = upperBound
		} else if α >= β {
			te.bound = lowerBound
		} else {
			te.bound = exactBound
		}
		ai.ttPut(&te)
	}

	return best, α
}
<|endoftext|>


<commit_before>/*
 Copyright 2016 Padduck, LLC

 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 sftp

import (
	"os"
	"time"
	"path/filepath"
	"github.com/pufferpanel/pufferd/utils"
	"github.com/pufferpanel/sftp"
	"github.com/pkg/errors"
)

type PrefixFileSystem struct {
	sftp.FileSystem
	prefix string
}

func CreateVirtualFs(prefix string) sftp.FileSystem {
	return PrefixFileSystem{prefix: prefix}
}

func (fs PrefixFileSystem) Stat(path string) (os.FileInfo, error) {
	p, err := fs.validate(path)
	if err != nil {
		return nil, err
	}
	return os.Stat(p)
}

func (fs PrefixFileSystem) Lstat(path string) (os.FileInfo, error) {
	p, err := fs.validate(path)
	if err != nil {
		return nil, err
	}
	return os.Lstat(p)
}

func (fs PrefixFileSystem) Mkdir(path string, mode os.FileMode) error {
	p, err := fs.validate(path)
	if err != nil {
		return err
	}
	return os.Mkdir(p, mode)
}

func (fs PrefixFileSystem) Remove(path string) error {
	p, err := fs.validate(path)
	if err != nil {
		return err
	}
	return os.Remove(p)
}

func (fs PrefixFileSystem) Symlink(target string, link string) error {
	t, err := fs.validate(target)
	if err != nil {
		return err
	}
	l, err := fs.validate(link)
	if err != nil {
		return err
	}
	return os.Symlink(t, l)
}

func (fs PrefixFileSystem) Readlink(path string) (string, error) {
	p, err := fs.validate(path)
	if err != nil {
		return "", err
	}
	return os.Readlink(p)
}

func (fs PrefixFileSystem) OpenFile(path string, flag int, mode os.FileMode) (*os.File, error) {
	p, err := fs.validate(path)
	if err != nil {
		return nil, err
	}
	return os.OpenFile(p, flag, mode)
}

func (fs PrefixFileSystem) Truncate(path string, size int64) error {
	p, err := fs.validate(path)
	if err != nil {
		return err
	}
	return os.Truncate(p, size)
}

func (fs PrefixFileSystem) Chmod(path string, mode os.FileMode) error {
	p, err := fs.validate(path)
	if err != nil {
		return err
	}
	return os.Chmod(p, mode)
}

func (fs PrefixFileSystem) Chtimes(path string, aTime, mTime time.Time) error {
	p, err := fs.validate(path)
	if err != nil {
		return err
	}
	return os.Chtimes(p, aTime, mTime)
}

func (fs PrefixFileSystem) Chown(path string, uid, gid int) error {
	return nil
}

func (fs PrefixFileSystem) Rename(oldPath string, newPath string) error {
	path1, err := fs.validate(oldPath)
	if err != nil {
		return err
	}
	path2, err := fs.validate(newPath)
	if err != nil {
		return err
	}
	return os.Rename(path1, path2)
}

func (fs PrefixFileSystem) validate(path string) (string, error) {
	ok, path := fs.tryPrefix(path)
	if !ok {
		return "", errors.New("Invalid path provided")
	}
	return path, nil
}

func (fs PrefixFileSystem) tryPrefix(path string) (bool, string) {
	if len(path) > 0 && path[0] == '/' {
		path = path[1:]
	}
	newPath := filepath.Clean(filepath.Join(fs.prefix, path))
	if utils.EnsureAccess(newPath, fs.prefix) {
		return true, newPath
	} else {
		return false, ""
	}
}<commit_msg>Add masking on true route when errors occur<commit_after>/*
 Copyright 2016 Padduck, LLC

 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 sftp

import (
	"os"
	"time"
	"path/filepath"
	"github.com/pufferpanel/pufferd/utils"
	"github.com/pufferpanel/sftp"
	"github.com/pkg/errors"
	"strings"
)

type PrefixFileSystem struct {
	sftp.FileSystem
	prefix string
}

func CreateVirtualFs(prefix string) sftp.FileSystem {
	return PrefixFileSystem{prefix: prefix}
}

func (fs PrefixFileSystem) Stat(path string) (os.FileInfo, error) {
	p, err := fs.validate(path)
	if err != nil {
		return nil, err
	}
	fi, e := os.Stat(p)
	if e != nil {
		e = fs.maskError(e)
	}
	return fi, e
}

func (fs PrefixFileSystem) Lstat(path string) (os.FileInfo, error) {
	p, err := fs.validate(path)
	if err != nil {
		return nil, err
	}
	fi, e := os.Lstat(p)
	if e != nil {
		e = fs.maskError(e)
	}
	return fi, e
}

func (fs PrefixFileSystem) Mkdir(path string, mode os.FileMode) error {
	p, err := fs.validate(path)
	if err != nil {
		return err
	}
	e := os.Mkdir(p, mode)
	if e != nil {
		e = fs.maskError(e)
	}
	return e
}

func (fs PrefixFileSystem) Remove(path string) error {
	p, err := fs.validate(path)
	if err != nil {
		return err
	}
	e := os.Remove(p)
	if e != nil {
		e = fs.maskError(e)
	}
	return e
}

func (fs PrefixFileSystem) Symlink(target string, link string) error {
	t, err := fs.validate(target)
	if err != nil {
		return err
	}
	l, err := fs.validate(link)
	if err != nil {
		return err
	}
	e := os.Symlink(t, l)
	if e != nil {
		e = fs.maskError(e)
	}
	return e
}

func (fs PrefixFileSystem) Readlink(path string) (string, error) {
	p, err := fs.validate(path)
	if err != nil {
		return "", err
	}
	return os.Readlink(p)
}

func (fs PrefixFileSystem) OpenFile(path string, flag int, mode os.FileMode) (*os.File, error) {
	p, err := fs.validate(path)
	if err != nil {
		return nil, err
	}
	return os.OpenFile(p, flag, mode)
}

func (fs PrefixFileSystem) Truncate(path string, size int64) error {
	p, err := fs.validate(path)
	if err != nil {
		return err
	}
	return os.Truncate(p, size)
}

func (fs PrefixFileSystem) Chmod(path string, mode os.FileMode) error {
	p, err := fs.validate(path)
	if err != nil {
		return err
	}
	e := os.Chmod(p, mode)
	if e != nil {
		e = fs.maskError(e)
	}
	return e
}

func (fs PrefixFileSystem) Chtimes(path string, aTime, mTime time.Time) error {
	p, err := fs.validate(path)
	if err != nil {
		return err
	}
	e := os.Chtimes(p, aTime, mTime)
	if e != nil {
		e = fs.maskError(e)
	}
	return e
}

func (fs PrefixFileSystem) Chown(path string, uid, gid int) error {
	return nil
}

func (fs PrefixFileSystem) Rename(oldPath string, newPath string) error {
	path1, err := fs.validate(oldPath)
	if err != nil {
		return err
	}
	path2, err := fs.validate(newPath)
	if err != nil {
		return err
	}
	e := os.Rename(path1, path2)
	if e != nil {
		e = fs.maskError(e)
	}
	return e
}

func (fs PrefixFileSystem) validate(path string) (string, error) {
	ok, path := fs.tryPrefix(path)
	if !ok {
		return "", errors.New("Invalid path provided")
	}
	return path, nil
}

func (fs PrefixFileSystem) tryPrefix(path string) (bool, string) {
	newPath := filepath.Clean(filepath.Join(fs.prefix, path))
	if utils.EnsureAccess(newPath, fs.prefix) {
		return true, newPath
	} else {
		return false, ""
	}
}

func (fs PrefixFileSystem) maskError(err error) error{
	return errors.New(strings.Replace(err.Error(), fs.prefix, "", -1))
}<|endoftext|>


<commit_before>package main

import (
	"flag"
	"log"
	"os"

	"github.com/Shopify/sarama"
	"github.com/wvanbergen/kazoo-go"
)

var (
	zookeeper = flag.String("zookeeper", os.Getenv("ZOOKEEPER"), "The zookeeper connection string")
	groupName = flag.String("group", "", "The consumer group to transfer offsets for")
)

func main() {
	flag.Parse()

	if *zookeeper == "" {
		log.Fatal("The -zookeeper command line argument is required")
	}

	if *groupName == "" {
		log.Fatal("The -group command line argument is required")
	}

	var (
		config         = kazoo.NewConfig()
		zookeeperNodes []string
	)

	zookeeperNodes, config.Chroot = kazoo.ParseConnectionString(*zookeeper)
	kz, err := kazoo.NewKazoo(zookeeperNodes, config)
	if err != nil {
		log.Fatal("Failed to connect to the zookeeper cluster:", err)
	}
	defer kz.Close()

	brokerList, err := kz.BrokerList()
	if err != nil {
		log.Fatal("Failed to retrieve Kafka broker list from zookeeper:", err)
	}

	group := kz.Consumergroup(*groupName)
	if exists, err := group.Exists(); err != nil {
		log.Fatal(err)
	} else if !exists {
		log.Fatalf("The consumergroup %s is not registered in Zookeeper", groupName)
	}

	offsets, err := group.FetchAllOffsets()
	if err != nil {
		log.Fatal("Failed to retrieve offsets from zookeeper:", err)
	}

	client, err := sarama.NewClient(brokerList, nil)
	if err != nil {
		log.Fatal("Failed to connect to Kafka cluster:", err)
	}
	defer client.Close()

	coordinator, err := client.Coordinator(group.Name)
	if err != nil {
		log.Fatal("Failed to obtain coordinator for consumer group:", err)
	}

	request := &sarama.OffsetCommitRequest{
		Version:       1,
		ConsumerGroup: group.Name,
	}

	for topic, partitionOffsets := range offsets {
		for partition, nextOffset := range partitionOffsets {
			// In Zookeeper, we store the next offset to process.
			// In Kafka, we store the last offset that was processed.
			// So we have to fix an off by one error.
			lastOffset := offset - 1
			log.Printf("Last processed offset for %s/%d: %d", topic, partition, lastOffset)
			request.AddBlock(topic, partition, lastOffset, 0, "")
		}
	}

	response, err := coordinator.CommitOffset(request)
	if err != nil {
		log.Fatal("Failed to commit offsets to Kafka:", err)
	}

	var errorsFound = false
	for topic, partitionErrors := range response.Errors {
		for partition, err := range partitionErrors {
			if err != sarama.ErrNoError {
				errorsFound = true
				log.Printf("WARNING: offset for %s/%d was not committed: %s", topic, partition, err)
			}
		}
	}

	if !errorsFound {
		log.Print("Offsets successfully committed to Kafka!")
	}
}
<commit_msg>Code review fixes.<commit_after>package main

import (
	"flag"
	"log"
	"os"

	"github.com/Shopify/sarama"
	"github.com/wvanbergen/kazoo-go"
)

var (
	zookeeper = flag.String("zookeeper", os.Getenv("ZOOKEEPER_PEERS"), "The zookeeper connection string")
	groupName = flag.String("group", "", "The consumer group to transfer offsets for")
)

func main() {
	flag.Parse()

	if *zookeeper == "" {
		log.Fatal("The -zookeeper command line argument is required")
	}

	if *groupName == "" {
		log.Fatal("The -group command line argument is required")
	}

	var (
		config         = kazoo.NewConfig()
		zookeeperNodes []string
	)

	zookeeperNodes, config.Chroot = kazoo.ParseConnectionString(*zookeeper)
	kz, err := kazoo.NewKazoo(zookeeperNodes, config)
	if err != nil {
		log.Fatal("[ERROR] Failed to connect to the zookeeper cluster:", err)
	}
	defer kz.Close()

	brokerList, err := kz.BrokerList()
	if err != nil {
		log.Fatal("[ERROR] Failed to retrieve Kafka broker list from zookeeper:", err)
	}

	group := kz.Consumergroup(*groupName)
	if exists, err := group.Exists(); err != nil {
		log.Fatal(err)
	} else if !exists {
		log.Fatalf("[ERROR] The consumergroup %s is not registered in Zookeeper", *groupName)
	}

	if instances, err := group.Instances(); err != nil {
		log.Fatal("[ERROR] Failed to get running instances from Zookeeper:", err)
	} else if len(instances) > 0 {
		log.Printf("[WARNING] This consumergroup has %d running instances. You should probably stop them before transferring offsets.", len(instances))
	}

	offsets, err := group.FetchAllOffsets()
	if err != nil {
		log.Fatal("[ERROR] Failed to retrieve offsets from zookeeper:", err)
	}

	client, err := sarama.NewClient(brokerList, nil)
	if err != nil {
		log.Fatal("[ERROR] Failed to connect to Kafka cluster:", err)
	}
	defer client.Close()

	coordinator, err := client.Coordinator(group.Name)
	if err != nil {
		log.Fatal("[ERROR] Failed to obtain coordinator for consumer group:", err)
	}

	request := &sarama.OffsetCommitRequest{
		Version:       1,
		ConsumerGroup: group.Name,
	}

	var requestBlocks int
	for topic, partitionOffsets := range offsets {
		for partition, nextOffset := range partitionOffsets {
			// In Zookeeper, we store the next offset to process.
			// In Kafka, we store the last offset that was processed.
			// So we have to fix an off by one error.
			lastOffset := nextOffset - 1
			request.AddBlock(topic, partition, lastOffset, 0, "")
			requestBlocks++
		}
	}

	response, err := coordinator.CommitOffset(request)
	if err != nil {
		log.Fatal("[ERROR] Failed to commit offsets to Kafka:", err)
	}

	var errorsFound bool
	for topic, partitionOffsets := range offsets {
		for partition, nextOffset := range partitionOffsets {
			if err := response.Errors[topic][partition]; err == sarama.ErrNoError {
				log.Printf("%s/%d: %d committed as last processed offset", topic, partition, nextOffset-1)
			} else {
				errorsFound = true
				log.Printf("[WARNING] %s/%d: offset %d was not committed: %s", topic, partition, nextOffset-1, err)
			}
		}
	}

	if !errorsFound {
		log.Print("[SUCCESS] Offsets successfully committed to Kafka!")
	}
}
<|endoftext|>


<commit_before>//
//  sed_test.go
//  sed
//
// Copyright (c) 2009 Geoffrey Clements
//
// 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 sed

import (
  "testing";
)

func TestProcessLine(t *testing.T) {
  pieces := [...]string{"s", "o", "0", "g"};
  c, _ := NewCmd(pieces[0:len(pieces)]);
  s, stop, err := c.(Cmd).processLine("good");
  if stop {
    t.Error("Got stop when we shouldn't have")
  }
  if err != nil {
    t.Errorf("Got and error when we shouldn't have %v", err)
  }
  checkString(t, "bad global s command", "g00d", s);

  pieces = [...]string{"s", "o", "0", "1"};
  c, _ = NewCmd(pieces[0:len(pieces)]);
  s, stop, err = c.(Cmd).processLine("good");
  if stop {
    t.Error("Got stop when we shouldn't have")
  }
  if err != nil {
    t.Errorf("Got and error when we shouldn't have %v", err)
  }
  checkString(t, "bad global s command", "g0od", s);
}

func checkString(t *testing.T, message, expected, actual string) {
  if expected != actual {
    t.Errorf("%s: '%s' != '%s'", message, expected, actual)
  }
}
<commit_msg>Fix tests<commit_after>//
//  sed_test.go
//  sed
//
// Copyright (c) 2009 Geoffrey Clements
//
// 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 sed

import (
  "testing";
)

func TestProcessLine(t *testing.T) {
  _s := new(Sed);
  _s.Init();
  pieces := [...]string{"s", "o", "0", "g"};
  c, _ := NewCmd(pieces[0:len(pieces)]);
  _s.patternSpace = "good";
  stop, err := c.(Cmd).processLine(_s);
  if stop {
    t.Error("Got stop when we shouldn't have")
  }
  if err != nil {
    t.Errorf("Got and error when we shouldn't have %v", err)
  }
  checkString(t, "bad global s command", "g00d", _s.patternSpace);

  pieces = [...]string{"s", "o", "0", "1"};
  c, _ = NewCmd(pieces[0:len(pieces)]);
  _s.patternSpace = "good";
  stop, err = c.(Cmd).processLine(_s);
  if stop {
    t.Error("Got stop when we shouldn't have")
  }
  if err != nil {
    t.Errorf("Got and error when we shouldn't have %v", err)
  }
  checkString(t, "bad global s command", "g0od", _s.patternSpace);
}

func checkString(t *testing.T, message, expected, actual string) {
  if expected != actual {
    t.Errorf("%s: '%s' != '%s'", message, expected, actual)
  }
}
<|endoftext|>


<commit_before>package saml

import (
	"fmt"
	"io/ioutil"
)

func GenerateAuthnRequest(publicCertificate, privateKey, idpSSOUrl, idpSSODescriptorUrl,
idpPublicCertificate, assertionConsumerServiceUrl string) (string, error) {

	//Create a temporary file for the public certificate
	publicCertificateFile, err := ioutil.TempFile(os.TempDir(), "publicCertificate")
	if err != nil {
		return "", err
	}
	publicCertificatePath := publicCertificateFile.Name()
	defer deleteTempFile(publicCertificatePath)

	//Create a temporary file for the private key
	privateKeyFile, err := ioutil.TempFile(os.TempDir(), "privateKey")
	if err != nil {
		return "", err
	}
	privateKeyPath := privateKeyFile.Name()
	defer deleteTempFile(privateKeyPath)

	//Create a temporary file for the idp public certificate
	idpPublicCertificateFile, err := ioutil.TempFile(os.TempDir(), "privateKey")
	if err != nil {
		return "", err
	}
	idpPublicCertificatePath := idpPublicCertificateFile.Name()
	defer deleteTempFile(idpPublicCertificatePath)

	sp := ServiceProviderSettings{
		PublicCertPath:              publicCertificatePath,
		PrivateKeyPath:              privateKeyPath,
		IDPSSOURL:                   idpSSOUrl,
		IDPSSODescriptorURL:         idpSSODescriptorUrl,
		IDPPublicCertPath:           idpPublicCertificatePath,
		AssertionConsumerServiceURL: assertionConsumerServiceUrl,
	}
	err = sp.Init()
	if err != nil {
		return "", err
	}

	// generate the AuthnRequest and then get a base64 encoded string of the XML
	authnRequest := sp.GetAuthnRequest()
	b64XML, err := authnRequest.EncodedSignedString(sp.PrivateKeyPath)
	if err != nil {
		return "", err
	}
	return b64XML, nil
}

func GenerateSamlResponse(response *Response, publicCertificate, privateKey, idpSSOUrl,
idpSSODescriptorUrl, idpPublicCertificate, assertionConsumerServiceUrl string) (error) {

	//Create a temporary file for the public certificate
	publicCertificateFile, err := ioutil.TempFile(os.TempDir(), "publicCertificate")
	if err != nil {
		return "", err
	}
	publicCertificatePath := publicCertificateFile.Name()
	defer deleteTempFile(publicCertificatePath)

	//Create a temporary file for the private key
	privateKeyFile, err := ioutil.TempFile(os.TempDir(), "privateKey")
	if err != nil {
		return "", err
	}
	privateKeyPath := privateKeyFile.Name()
	defer deleteTempFile(privateKeyPath)

	//Create a temporary file for the idp public certificate
	idpPublicCertificateFile, err := ioutil.TempFile(os.TempDir(), "privateKey")
	if err != nil {
		return "", err
	}
	idpPublicCertificatePath := idpPublicCertificateFile.Name()
	defer deleteTempFile(idpPublicCertificatePath)

	sp := ServiceProviderSettings{
		PublicCertPath:              publicCertificatePath,
		PrivateKeyPath:              privateKeyPath,
		IDPSSOURL:                   idpSSOUrl,
		IDPSSODescriptorURL:         idpSSODescriptorUrl,
		IDPPublicCertPath:           idpPublicCertificatePath,
		AssertionConsumerServiceURL: assertionConsumerServiceUrl,
	}
	sp.Init()
	if err != nil {
		return "", err
	}

	err = response.Validate(&sp)
	if err != nil {
		return fmt.Errorf("SAMLResponse validation: "+err.Error())
	}

}<commit_msg>fixed typos<commit_after>package saml

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

func GenerateAuthnRequest(publicCertificate, privateKey, idpSSOUrl, idpSSODescriptorUrl,
idpPublicCertificate, assertionConsumerServiceUrl string) (string, error) {

	//Create a temporary file for the public certificate
	publicCertificateFile, err := ioutil.TempFile(os.TempDir(), "publicCertificate")
	if err != nil {
		return "", err
	}
	publicCertificatePath := publicCertificateFile.Name()
	defer deleteTempFile(publicCertificatePath)

	//Create a temporary file for the private key
	privateKeyFile, err := ioutil.TempFile(os.TempDir(), "privateKey")
	if err != nil {
		return "", err
	}
	privateKeyPath := privateKeyFile.Name()
	defer deleteTempFile(privateKeyPath)

	//Create a temporary file for the idp public certificate
	idpPublicCertificateFile, err := ioutil.TempFile(os.TempDir(), "privateKey")
	if err != nil {
		return "", err
	}
	idpPublicCertificatePath := idpPublicCertificateFile.Name()
	defer deleteTempFile(idpPublicCertificatePath)

	sp := ServiceProviderSettings{
		PublicCertPath:              publicCertificatePath,
		PrivateKeyPath:              privateKeyPath,
		IDPSSOURL:                   idpSSOUrl,
		IDPSSODescriptorURL:         idpSSODescriptorUrl,
		IDPPublicCertPath:           idpPublicCertificatePath,
		AssertionConsumerServiceURL: assertionConsumerServiceUrl,
	}
	err = sp.Init()
	if err != nil {
		return "", err
	}

	// generate the AuthnRequest and then get a base64 encoded string of the XML
	authnRequest := sp.GetAuthnRequest()
	b64XML, err := authnRequest.EncodedSignedString(sp.PrivateKeyPath)
	if err != nil {
		return "", err
	}
	return b64XML, nil
}

func GenerateSamlResponse(response *Response, publicCertificate, privateKey, idpSSOUrl,
idpSSODescriptorUrl, idpPublicCertificate, assertionConsumerServiceUrl string) (error) {

	//Create a temporary file for the public certificate
	publicCertificateFile, err := ioutil.TempFile(os.TempDir(), "publicCertificate")
	if err != nil {
		return err
	}
	publicCertificatePath := publicCertificateFile.Name()
	defer deleteTempFile(publicCertificatePath)

	//Create a temporary file for the private key
	privateKeyFile, err := ioutil.TempFile(os.TempDir(), "privateKey")
	if err != nil {
		return err
	}
	privateKeyPath := privateKeyFile.Name()
	defer deleteTempFile(privateKeyPath)

	//Create a temporary file for the idp public certificate
	idpPublicCertificateFile, err := ioutil.TempFile(os.TempDir(), "privateKey")
	if err != nil {
		return  err
	}
	idpPublicCertificatePath := idpPublicCertificateFile.Name()
	defer deleteTempFile(idpPublicCertificatePath)

	sp := ServiceProviderSettings{
		PublicCertPath:              publicCertificatePath,
		PrivateKeyPath:              privateKeyPath,
		IDPSSOURL:                   idpSSOUrl,
		IDPSSODescriptorURL:         idpSSODescriptorUrl,
		IDPPublicCertPath:           idpPublicCertificatePath,
		AssertionConsumerServiceURL: assertionConsumerServiceUrl,
	}
	sp.Init()
	if err != nil {
		return err
	}

	err = response.Validate(&sp)
	if err != nil {
		return fmt.Errorf("SAMLResponse validation: "+err.Error())
	}
	return nil
}<|endoftext|>