<commit_before>/*
Copyright 2016 The Kubernetes Authors All rights reserved.

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

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

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

package rest

import (
	"k8s.io/kubernetes/cmd/libs/go2idl/client-gen/testdata/apis/testgroup"
	"k8s.io/kubernetes/pkg/api"
	"k8s.io/kubernetes/pkg/fields"
	"k8s.io/kubernetes/pkg/labels"
	"k8s.io/kubernetes/pkg/registry/generic"
	etcdgeneric "k8s.io/kubernetes/pkg/registry/generic/etcd"
	"k8s.io/kubernetes/pkg/runtime"
	"k8s.io/kubernetes/pkg/storage"
)

type REST struct {
	*etcdgeneric.Etcd
}

// NewREST returns a RESTStorage object that will work with testtype.
func NewREST(s storage.Interface, storageDecorator generic.StorageDecorator) *REST {
	prefix := "/testtype"
	newListFunc := func() runtime.Object { return &testgroup.TestTypeList{} }
	storageInterface := storageDecorator(
		s, 100, &testgroup.TestType{}, prefix, false, newListFunc)
	store := &etcdgeneric.Etcd{
		NewFunc: func() runtime.Object { return &testgroup.TestType{} },
		// NewListFunc returns an object capable of storing results of an etcd list.
		NewListFunc: newListFunc,
		// Produces a path that etcd understands, to the root of the resource
		// by combining the namespace in the context with the given prefix.
		KeyRootFunc: func(ctx api.Context) string {
			return etcdgeneric.NamespaceKeyRootFunc(ctx, prefix)
		},
		// Produces a path that etcd understands, to the resource by combining
		// the namespace in the context with the given prefix.
		KeyFunc: func(ctx api.Context, name string) (string, error) {
			return etcdgeneric.NamespaceKeyFunc(ctx, prefix, name)
		},
		// Retrieve the name field of the resource.
		ObjectNameFunc: func(obj runtime.Object) (string, error) {
			return obj.(*testgroup.TestType).Name, nil
		},
		// Used to match objects based on labels/fields for list.
		PredicateFunc: func(label labels.Selector, field fields.Selector) generic.Matcher {
			return generic.MatcherFunc(nil)
		},
		Storage: storageInterface,
	}
	return &REST{store}
}
<commit_msg>fix the namespaceScoped of cachers<commit_after>/*
Copyright 2016 The Kubernetes Authors All rights reserved.

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

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

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

package rest

import (
	"k8s.io/kubernetes/cmd/libs/go2idl/client-gen/testdata/apis/testgroup"
	"k8s.io/kubernetes/pkg/api"
	"k8s.io/kubernetes/pkg/fields"
	"k8s.io/kubernetes/pkg/labels"
	"k8s.io/kubernetes/pkg/registry/generic"
	etcdgeneric "k8s.io/kubernetes/pkg/registry/generic/etcd"
	"k8s.io/kubernetes/pkg/runtime"
	"k8s.io/kubernetes/pkg/storage"
)

type REST struct {
	*etcdgeneric.Etcd
}

// NewREST returns a RESTStorage object that will work with testtype.
func NewREST(s storage.Interface, storageDecorator generic.StorageDecorator) *REST {
	prefix := "/testtype"
	newListFunc := func() runtime.Object { return &testgroup.TestTypeList{} }
	// Usually you should reuse your RESTCreateStrategy.
	strategy := &NotNamespaceScoped{}
	storageInterface := storageDecorator(
		s, 100, &testgroup.TestType{}, prefix, strategy, newListFunc)
	store := &etcdgeneric.Etcd{
		NewFunc: func() runtime.Object { return &testgroup.TestType{} },
		// NewListFunc returns an object capable of storing results of an etcd list.
		NewListFunc: newListFunc,
		// Produces a path that etcd understands, to the root of the resource
		// by combining the namespace in the context with the given prefix.
		KeyRootFunc: func(ctx api.Context) string {
			return etcdgeneric.NamespaceKeyRootFunc(ctx, prefix)
		},
		// Produces a path that etcd understands, to the resource by combining
		// the namespace in the context with the given prefix.
		KeyFunc: func(ctx api.Context, name string) (string, error) {
			return etcdgeneric.NamespaceKeyFunc(ctx, prefix, name)
		},
		// Retrieve the name field of the resource.
		ObjectNameFunc: func(obj runtime.Object) (string, error) {
			return obj.(*testgroup.TestType).Name, nil
		},
		// Used to match objects based on labels/fields for list.
		PredicateFunc: func(label labels.Selector, field fields.Selector) generic.Matcher {
			return generic.MatcherFunc(nil)
		},
		Storage: storageInterface,
	}
	return &REST{store}
}

type NotNamespaceScoped struct {
}

func (*NotNamespaceScoped) NamespaceScoped() bool {
	return false
}
<|endoftext|>


<commit_before>// Copyright (c) 2019-2020 Leonid Kneller. All rights reserved.
// Licensed under the MIT license.
// See the LICENSE file for full license information.

package mym

import (
	"math"
)

// Vmedian3 -- computes the geometric median of u[0],u[1],...,u[len(u)-1].
// This function implements the Weiszfeld iterative algorithm as modified by
// Vardi and Zhang.
//
// Reference: Vardi and Zhang, The multivariate L1-median and associated data depth,
// Proceedings of the National Academy of Sciences Feb 2000, 97 (4) 1423-1426.
//
// DOI: https://doi.org/10.1073/pnas.97.4.1423
func Vmedian3(u [][3]float64) [3]float64 {
	n := len(u)
	if n == 0 {
		return [3]float64{0, 0, 0}
	}
	//
	// initial approximation
	mu := Vmean3(u)
	//
	// convergence test based on relative change of mu (L1-norm)
	convtest := func(mu1, mu2 [3]float64) bool {
		t1, _ := Vnrm3(mu1)
		t2, _ := Vnrm3(mu2)
		w := Vsub3(mu1, mu2)
		tw, _ := Vnrm3(w)
		return tw <= Epsilon*math.Max(t1, t2)
	}
	//
	for iter := 1; iter <= 5000; iter++ {
		eta := 0.0
		S1 := [3]float64{0, 0, 0}
		S2 := 0.0
		R := [3]float64{0, 0, 0}
		//
		for _, v := range u {
			w := Vsub3(v, mu)
			wabs := Vabs3(w)
			//
			if wabs < Epsilon {
				eta += 1
				continue
			}
			//
			S1 = Vadd3(S1, Vdiv3(v, wabs))
			S2 += 1 / wabs
			//
			R = Vadd3(R, Vdiv3(w, wabs))
		}
		//
		T := Vdiv3(S1, S2)
		gamma := math.Min(1, eta/Vabs3(R))
		munew := Vadd3(Vmul3(T, 1-gamma), Vmul3(mu, gamma))
		if convtest(mu, munew) {
			return munew
		}
		//
		mu = munew
	}
	//
	return mu
}
<commit_msg>Replace `Epsilon` with `SqrtEps` in the convergency test.<commit_after>// Copyright (c) 2019-2020 Leonid Kneller. All rights reserved.
// Licensed under the MIT license.
// See the LICENSE file for full license information.

package mym

import (
	"math"
)

// Vmedian3 -- computes the geometric median of u[0],u[1],...,u[len(u)-1].
// This function implements the Weiszfeld iterative algorithm as modified by
// Vardi and Zhang.
//
// Reference: Vardi and Zhang, The multivariate L1-median and associated data depth,
// Proceedings of the National Academy of Sciences Feb 2000, 97 (4) 1423-1426.
//
// DOI: https://doi.org/10.1073/pnas.97.4.1423
func Vmedian3(u [][3]float64) [3]float64 {
	n := len(u)
	if n == 0 {
		return [3]float64{0, 0, 0}
	}
	//
	// initial approximation
	mu := Vmean3(u)
	//
	// convergence test based on relative change of mu (L1-norm)
	convtest := func(mu1, mu2 [3]float64) bool {
		t1, _ := Vnrm3(mu1)
		t2, _ := Vnrm3(mu2)
		w := Vsub3(mu1, mu2)
		tw, _ := Vnrm3(w)
		return tw <= SqrtEps*math.Max(t1, t2)
	}
	//
	for iter := 1; iter <= 5000; iter++ {
		eta := 0.0
		S1 := [3]float64{0, 0, 0}
		S2 := 0.0
		R := [3]float64{0, 0, 0}
		//
		for _, v := range u {
			w := Vsub3(v, mu)
			wabs := Vabs3(w)
			//
			if wabs < Epsilon {
				eta += 1
				continue
			}
			//
			S1 = Vadd3(S1, Vdiv3(v, wabs))
			S2 += 1 / wabs
			//
			R = Vadd3(R, Vdiv3(w, wabs))
		}
		//
		T := Vdiv3(S1, S2)
		gamma := math.Min(1, eta/Vabs3(R))
		munew := Vadd3(Vmul3(T, 1-gamma), Vmul3(mu, gamma))
		if convtest(mu, munew) {
			return munew
		}
		//
		mu = munew
	}
	//
	return mu
}
<|endoftext|>


<commit_before>// Copyright 2017-2018 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://gitee.com/johng/gf.

package gdb

import (
    "bytes"
    "errors"
    "fmt"
    "gitee.com/johng/gf/g/os/glog"
    "gitee.com/johng/gf/g/os/gtime"
    "gitee.com/johng/gf/g/util/gconv"
    "gitee.com/johng/gf/g/util/gregex"
    "gitee.com/johng/gf/g/util/gstr"
    _ "gitee.com/johng/gf/third/github.com/go-sql-driver/mysql"
    "reflect"
    "strings"
)

// 格式化SQL查询条件
func formatCondition(where interface{}, args []interface{}) (string, []interface{}) {
    // 条件字符串处理
    buffer := bytes.NewBuffer(nil)
    if reflect.ValueOf(where).Kind() == reflect.Map {
        ks := reflect.ValueOf(where).MapKeys()
        vs := reflect.ValueOf(where)
        for _, k := range ks {
            key   := gconv.String(k.Interface())
            value := gconv.String(vs.MapIndex(k).Interface())
            if buffer.Len() > 0 {
                buffer.WriteString(" AND ")
            }
            if gstr.IsNumeric(value) || value == "?" {
                buffer.WriteString(key + "=" + value)
            } else {
                buffer.WriteString(key + "='" + value + "'")
            }
        }
    } else {
        buffer.Write(gconv.Bytes(where))
    }
    if buffer.Len() == 0 {
        buffer.WriteString("1")
    }
    // 查询条件处理
    newWhere := buffer.String()
    newArgs  := make([]interface{}, 0)
    if len(args) > 0 {
        for index, arg := range args {
            rv   := reflect.ValueOf(arg)
            kind := rv.Kind()
            if kind == reflect.Ptr {
                rv   = rv.Elem()
                kind = rv.Kind()
            }
            switch kind {
                case reflect.Slice: fallthrough
                case reflect.Array:
                    for i := 0; i < rv.Len(); i++ {
                        newArgs = append(newArgs, rv.Index(i).Interface())
                    }
                    counter    := 0
                    newWhere, _ = gregex.ReplaceStringFunc(`\?`, newWhere, func(s string) string {
                        counter++
                        if counter == index + 1 {
                            return "?" + strings.Repeat(",?", rv.Len() - 1)
                        }
                        return s
                    })
                default:
                    newArgs = append(newArgs, arg)
            }
        }
    }
    return newWhere, newArgs
}

// 打印SQL对象(仅在debug=true时有效)
func printSql(v *Sql) {
    s := fmt.Sprintf("%s, %v, %s, %s, %d ms, %s", v.Sql, v.Args,
        gtime.NewFromTimeStamp(v.Start).Format("Y-m-d H:i:s.u"),
        gtime.NewFromTimeStamp(v.End).Format("Y-m-d H:i:s.u"),
        v.End - v.Start,
        v.Func,
    )
    if v.Error != nil {
        s += "\nError: " + v.Error.Error()
        glog.Backtrace(true, 2).Error(s)
    } else {
        glog.Debug(s)
    }
}

// 格式化错误信息
func formatError(err error, query string, args ...interface{}) error {
    if err != nil {
        errstr := fmt.Sprintf("DB ERROR: %s\n", err.Error())
        errstr += fmt.Sprintf("DB QUERY: %s\n", query)
        if len(args) > 0 {
            errstr += fmt.Sprintf("DB PARAM: %v\n", args)
        }
        err = errors.New(errstr)
    }
    return err
}

// 根据insert选项获得操作名称
func getInsertOperationByOption(option int) string {
    oper := "INSERT"
    switch option {
        case OPTION_REPLACE:
            oper = "REPLACE"
        case OPTION_SAVE:
        case OPTION_IGNORE:
            oper = "INSERT IGNORE"
    }
    return oper
}
<commit_msg>!17 修复where为空时不是1=1的bug Merge pull request !17 from 张金富/master<commit_after>// Copyright 2017-2018 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://gitee.com/johng/gf.

package gdb

import (
    "bytes"
    "errors"
    "fmt"
    "gitee.com/johng/gf/g/os/glog"
    "gitee.com/johng/gf/g/os/gtime"
    "gitee.com/johng/gf/g/util/gconv"
    "gitee.com/johng/gf/g/util/gregex"
    "gitee.com/johng/gf/g/util/gstr"
    _ "gitee.com/johng/gf/third/github.com/go-sql-driver/mysql"
    "reflect"
    "strings"
)

// 格式化SQL查询条件
func formatCondition(where interface{}, args []interface{}) (string, []interface{}) {
    // 条件字符串处理
    buffer := bytes.NewBuffer(nil)
    if reflect.ValueOf(where).Kind() == reflect.Map {
        ks := reflect.ValueOf(where).MapKeys()
        vs := reflect.ValueOf(where)
        for _, k := range ks {
            key   := gconv.String(k.Interface())
            value := gconv.String(vs.MapIndex(k).Interface())
            if buffer.Len() > 0 {
                buffer.WriteString(" AND ")
            }
            if gstr.IsNumeric(value) || value == "?" {
                buffer.WriteString(key + "=" + value)
            } else {
                buffer.WriteString(key + "='" + value + "'")
            }
        }
    } else {
        buffer.Write(gconv.Bytes(where))
    }
    if buffer.Len() == 0 {
        buffer.WriteString("1=1")
    }
    // 查询条件处理
    newWhere := buffer.String()
    newArgs  := make([]interface{}, 0)
    if len(args) > 0 {
        for index, arg := range args {
            rv   := reflect.ValueOf(arg)
            kind := rv.Kind()
            if kind == reflect.Ptr {
                rv   = rv.Elem()
                kind = rv.Kind()
            }
            switch kind {
                case reflect.Slice: fallthrough
                case reflect.Array:
                    for i := 0; i < rv.Len(); i++ {
                        newArgs = append(newArgs, rv.Index(i).Interface())
                    }
                    counter    := 0
                    newWhere, _ = gregex.ReplaceStringFunc(`\?`, newWhere, func(s string) string {
                        counter++
                        if counter == index + 1 {
                            return "?" + strings.Repeat(",?", rv.Len() - 1)
                        }
                        return s
                    })
                default:
                    newArgs = append(newArgs, arg)
            }
        }
    }
    return newWhere, newArgs
}

// 打印SQL对象(仅在debug=true时有效)
func printSql(v *Sql) {
    s := fmt.Sprintf("%s, %v, %s, %s, %d ms, %s", v.Sql, v.Args,
        gtime.NewFromTimeStamp(v.Start).Format("Y-m-d H:i:s.u"),
        gtime.NewFromTimeStamp(v.End).Format("Y-m-d H:i:s.u"),
        v.End - v.Start,
        v.Func,
    )
    if v.Error != nil {
        s += "\nError: " + v.Error.Error()
        glog.Backtrace(true, 2).Error(s)
    } else {
        glog.Debug(s)
    }
}

// 格式化错误信息
func formatError(err error, query string, args ...interface{}) error {
    if err != nil {
        errstr := fmt.Sprintf("DB ERROR: %s\n", err.Error())
        errstr += fmt.Sprintf("DB QUERY: %s\n", query)
        if len(args) > 0 {
            errstr += fmt.Sprintf("DB PARAM: %v\n", args)
        }
        err = errors.New(errstr)
    }
    return err
}

// 根据insert选项获得操作名称
func getInsertOperationByOption(option int) string {
    oper := "INSERT"
    switch option {
        case OPTION_REPLACE:
            oper = "REPLACE"
        case OPTION_SAVE:
        case OPTION_IGNORE:
            oper = "INSERT IGNORE"
    }
    return oper
}
<|endoftext|>


<commit_before>package main

import (
	"bytes"
	"crypto/md5"
	"flag"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"syscall"

	"github.com/flynn/go-crypto-ssh"
	"github.com/flynn/go-shlex"
)

const PrereceiveHook = `#!/bin/bash
set -eo pipefail; while read oldrev newrev refname; do
[[ $refname = "refs/heads/master" ]] && git archive $newrev | {{RECEIVER}} "$RECEIVE_USER" "$RECEIVE_REPO" "$RECEIVE_KEYNAME" "$RECEIVE_FINGERPRINT" | sed -$([[ $(uname) == "Darwin" ]] && echo l || echo u) "s/^/"$'\e[1G'"/"
done
`

var port *string = flag.String("p", "22", "port to listen on")
var repoPath *string = flag.String("r", "/tmp/repos", "path to repo cache")
var keyPath *string = flag.String("k", "/tmp/keys", "path to named keys")
var noAuth *bool = flag.Boolean("n", false, "no client authentication")

var receiver string
var privateKey string
var keyNames = make(map[string]string)

func init() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage:  %v [options] <privatekey> <receiver>\n\n", os.Args[0])
		flag.PrintDefaults()
	}
}

func main() {
	flag.Parse()
	if len(os.Args) < 2 {
		flag.Usage()
		return
	}
	privateKey = flag.Arg(0)
	receiver = flag.Arg(1)

	var config *ssh.ServerConfig
	if *noAuth {
		config = &ssh.ServerConfig{
			NoClientAuth: true,
		}
	} else {
		config = &ssh.ServerConfig{
			PublicKeyCallback: func(conn *ssh.ServerConn, user, algo string, pubkey []byte) bool {
				clientkey, _, ok := ssh.ParsePublicKey(pubkey)
				if !ok {
					return false
				}
				os.MkdirAll(*keyPath, 0755)
				files, err := ioutil.ReadDir(*keyPath)
				if err != nil {
					panic(err)
				}
				for _, file := range files {
					if !file.IsDir() {
						data, err := ioutil.ReadFile(*keyPath + "/" + file.Name())
						if err != nil {
							panic(err)
						}
						filekey, _, _, _, ok := ssh.ParseAuthorizedKey(data)
						if !ok {
							continue
						}
						if bytes.Equal(clientkey.Marshal(), filekey.Marshal()) {
							keyNames[publicKeyFingerprint(clientkey)] = file.Name()
							return true
						}
					}
				}
				return false
			},
		}
	}

	pemBytes, err := ioutil.ReadFile(privateKey)
	if err != nil {
		log.Fatal("Failed to load private key:", err)
	}
	if err = config.SetRSAPrivateKey(pemBytes); err != nil {
		log.Fatal("Failed to parse private key:", err)
	}

	listener, err := ssh.Listen("tcp", "0.0.0.0:"+*port, config)
	if err != nil {
		log.Fatal("failed to listen for connection")
	}
	for {
		// SSH connections just house multiplexed connections
		conn, err := listener.Accept()
		if err != nil {
			log.Println("failed to accept incoming connection")
			continue
		}
		if err := conn.Handshake(); err != nil {
			log.Println("failed to handshake")
			continue
		}
		go handleConnection(conn)
	}
}

func handleConnection(conn *ssh.ServerConn) {
	defer conn.Close()
	for {
		// Accept reads from the connection, demultiplexes packets
		// to their corresponding channels and returns when a new
		// channel request is seen. Some goroutine must always be
		// calling Accept; otherwise no messages will be forwarded
		// to the channels.
		ch, err := conn.Accept()
		if err == io.EOF {
			return
		}
		if err != nil {
			log.Println("hanndleConnection Accept:", err)
			break
		}
		if ch.ChannelType() != "session" {
			ch.Reject(ssh.UnknownChannelType, "unknown channel type")
			break
		}
		go handleChannel(conn, ch)
	}
}

func handleChannel(conn *ssh.ServerConn, ch ssh.Channel) {
	err := ch.Accept()
	if err != nil {
		panic(err)
	}
	defer ch.Close()
	for {
		req, err := ch.ReadRequest()
		if err == io.EOF {
			return
		}
		if err != nil {
			log.Println("handleChannel read request err:", err)
			continue
		}
		switch req.Request {
		case "exec":
			if req.WantReply {
				ch.AckRequest(true)
			}
			cmdline := string(req.Payload[4:])
			cmdargs, err := shlex.Split(cmdline)
			if err != nil {
				panic(err)
			}
			ensureCacheRepo(cmdargs[1])
			var keyname, fingerprint string
			if *noAuth {
				fingerprint = ""
				keyname = ""
			} else {
				fingerprint = publicKeyFingerprint(conn.PublicKey)
				keyname = keyNames[fingerprint]
			}
			cmd := exec.Command(cmdargs[0], cmdargs[1:]...)
			cmd.Dir = *repoPath
			cmd.Env = []string{
				"RECEIVE_USER=" + conn.User,
				"RECEIVE_REPO=" + cmdargs[1],
				"RECEIVE_KEYNAME=" + keyname,
				"RECEIVE_FINGERPRINT=" + fingerprint,
			}
			errCh := attachCmd(cmd, ch, ch.Stderr(), ch)
			err = cmd.Start()
			if err != nil {
				panic(err)
			}
			exitCh := exitStatusCh(cmd)
			if err = <-errCh; err != nil {
				panic(err)
			}
			ch.Exit(<-exitCh)
		case "env":
			if req.WantReply {
				ch.AckRequest(true)
			}
		}
	}
}

func attachCmd(cmd *exec.Cmd, stdout, stderr io.Writer, stdin io.Reader) chan error {
	errCh := make(chan error)

	stdinIn, err := cmd.StdinPipe()
	if err != nil {
		panic(err)
	}
	stdoutOut, err := cmd.StdoutPipe()
	if err != nil {
		panic(err)
	}
	stderrOut, err := cmd.StderrPipe()
	if err != nil {
		panic(err)
	}

	go func() {
		_, e := io.Copy(stdinIn, stdin)
		errCh <- e
	}()
	go func() {
		_, e := io.Copy(stdout, stdoutOut)
		errCh <- e
	}()
	go func() {
		_, e := io.Copy(stderr, stderrOut)
		errCh <- e
	}()

	return errCh
}

func exitStatusCh(cmd *exec.Cmd) chan uint {
	exitCh := make(chan uint)
	go func() {
		err := cmd.Wait()
		if err != nil {
			if exiterr, ok := err.(*exec.ExitError); ok {
				// There is no plattform independent way to retrieve
				// the exit code, but the following will work on Unix
				if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
					exitCh <- uint(status.ExitStatus())
				}
			} else {
				panic(err)
			}
			return
		}
		exitCh <- uint(0)
	}()
	return exitCh
}

func ensureCacheRepo(path string) {
	cachePath := *repoPath + "/" + path
	if _, err := os.Stat(cachePath); os.IsNotExist(err) {
		os.MkdirAll(cachePath, 0755)
		cmd := exec.Command("git", "init", "--bare")
		cmd.Dir = cachePath
		err = cmd.Run()
		if err != nil {
			panic(err)
		}
	}
	receiver, err := filepath.Abs(receiver)
	if err != nil {
		panic(err)
	}
	ioutil.WriteFile(
		cachePath+"/hooks/pre-receive",
		[]byte(strings.Replace(PrereceiveHook, "{{RECEIVER}}", receiver, 1)),
		0755)
}

func publicKeyFingerprint(key ssh.PublicKey) string {
	var values []string
	h := md5.New()
	h.Write(ssh.MarshalPublicKey(key))
	for _, value := range h.Sum(nil) {
		values = append(values, fmt.Sprintf("%x", value))
	}
	return strings.Join(values, ":")
}
<commit_msg>wrong name<commit_after>package main

import (
	"bytes"
	"crypto/md5"
	"flag"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"syscall"

	"github.com/flynn/go-crypto-ssh"
	"github.com/flynn/go-shlex"
)

const PrereceiveHook = `#!/bin/bash
set -eo pipefail; while read oldrev newrev refname; do
[[ $refname = "refs/heads/master" ]] && git archive $newrev | {{RECEIVER}} "$RECEIVE_USER" "$RECEIVE_REPO" "$RECEIVE_KEYNAME" "$RECEIVE_FINGERPRINT" | sed -$([[ $(uname) == "Darwin" ]] && echo l || echo u) "s/^/"$'\e[1G'"/"
done
`

var port *string = flag.String("p", "22", "port to listen on")
var repoPath *string = flag.String("r", "/tmp/repos", "path to repo cache")
var keyPath *string = flag.String("k", "/tmp/keys", "path to named keys")
var noAuth *bool = flag.Bool("n", false, "no client authentication")

var receiver string
var privateKey string
var keyNames = make(map[string]string)

func init() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage:  %v [options] <privatekey> <receiver>\n\n", os.Args[0])
		flag.PrintDefaults()
	}
}

func main() {
	flag.Parse()
	if len(os.Args) < 2 {
		flag.Usage()
		return
	}
	privateKey = flag.Arg(0)
	receiver = flag.Arg(1)

	var config *ssh.ServerConfig
	if *noAuth {
		config = &ssh.ServerConfig{
			NoClientAuth: true,
		}
	} else {
		config = &ssh.ServerConfig{
			PublicKeyCallback: func(conn *ssh.ServerConn, user, algo string, pubkey []byte) bool {
				clientkey, _, ok := ssh.ParsePublicKey(pubkey)
				if !ok {
					return false
				}
				os.MkdirAll(*keyPath, 0755)
				files, err := ioutil.ReadDir(*keyPath)
				if err != nil {
					panic(err)
				}
				for _, file := range files {
					if !file.IsDir() {
						data, err := ioutil.ReadFile(*keyPath + "/" + file.Name())
						if err != nil {
							panic(err)
						}
						filekey, _, _, _, ok := ssh.ParseAuthorizedKey(data)
						if !ok {
							continue
						}
						if bytes.Equal(clientkey.Marshal(), filekey.Marshal()) {
							keyNames[publicKeyFingerprint(clientkey)] = file.Name()
							return true
						}
					}
				}
				return false
			},
		}
	}

	pemBytes, err := ioutil.ReadFile(privateKey)
	if err != nil {
		log.Fatal("Failed to load private key:", err)
	}
	if err = config.SetRSAPrivateKey(pemBytes); err != nil {
		log.Fatal("Failed to parse private key:", err)
	}

	listener, err := ssh.Listen("tcp", "0.0.0.0:"+*port, config)
	if err != nil {
		log.Fatal("failed to listen for connection")
	}
	for {
		// SSH connections just house multiplexed connections
		conn, err := listener.Accept()
		if err != nil {
			log.Println("failed to accept incoming connection")
			continue
		}
		if err := conn.Handshake(); err != nil {
			log.Println("failed to handshake")
			continue
		}
		go handleConnection(conn)
	}
}

func handleConnection(conn *ssh.ServerConn) {
	defer conn.Close()
	for {
		// Accept reads from the connection, demultiplexes packets
		// to their corresponding channels and returns when a new
		// channel request is seen. Some goroutine must always be
		// calling Accept; otherwise no messages will be forwarded
		// to the channels.
		ch, err := conn.Accept()
		if err == io.EOF {
			return
		}
		if err != nil {
			log.Println("hanndleConnection Accept:", err)
			break
		}
		if ch.ChannelType() != "session" {
			ch.Reject(ssh.UnknownChannelType, "unknown channel type")
			break
		}
		go handleChannel(conn, ch)
	}
}

func handleChannel(conn *ssh.ServerConn, ch ssh.Channel) {
	err := ch.Accept()
	if err != nil {
		panic(err)
	}
	defer ch.Close()
	for {
		req, err := ch.ReadRequest()
		if err == io.EOF {
			return
		}
		if err != nil {
			log.Println("handleChannel read request err:", err)
			continue
		}
		switch req.Request {
		case "exec":
			if req.WantReply {
				ch.AckRequest(true)
			}
			cmdline := string(req.Payload[4:])
			cmdargs, err := shlex.Split(cmdline)
			if err != nil {
				panic(err)
			}
			ensureCacheRepo(cmdargs[1])
			var keyname, fingerprint string
			if *noAuth {
				fingerprint = ""
				keyname = ""
			} else {
				fingerprint = publicKeyFingerprint(conn.PublicKey)
				keyname = keyNames[fingerprint]
			}
			cmd := exec.Command(cmdargs[0], cmdargs[1:]...)
			cmd.Dir = *repoPath
			cmd.Env = []string{
				"RECEIVE_USER=" + conn.User,
				"RECEIVE_REPO=" + cmdargs[1],
				"RECEIVE_KEYNAME=" + keyname,
				"RECEIVE_FINGERPRINT=" + fingerprint,
			}
			errCh := attachCmd(cmd, ch, ch.Stderr(), ch)
			err = cmd.Start()
			if err != nil {
				panic(err)
			}
			exitCh := exitStatusCh(cmd)
			if err = <-errCh; err != nil {
				panic(err)
			}
			ch.Exit(<-exitCh)
		case "env":
			if req.WantReply {
				ch.AckRequest(true)
			}
		}
	}
}

func attachCmd(cmd *exec.Cmd, stdout, stderr io.Writer, stdin io.Reader) chan error {
	errCh := make(chan error)

	stdinIn, err := cmd.StdinPipe()
	if err != nil {
		panic(err)
	}
	stdoutOut, err := cmd.StdoutPipe()
	if err != nil {
		panic(err)
	}
	stderrOut, err := cmd.StderrPipe()
	if err != nil {
		panic(err)
	}

	go func() {
		_, e := io.Copy(stdinIn, stdin)
		errCh <- e
	}()
	go func() {
		_, e := io.Copy(stdout, stdoutOut)
		errCh <- e
	}()
	go func() {
		_, e := io.Copy(stderr, stderrOut)
		errCh <- e
	}()

	return errCh
}

func exitStatusCh(cmd *exec.Cmd) chan uint {
	exitCh := make(chan uint)
	go func() {
		err := cmd.Wait()
		if err != nil {
			if exiterr, ok := err.(*exec.ExitError); ok {
				// There is no plattform independent way to retrieve
				// the exit code, but the following will work on Unix
				if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
					exitCh <- uint(status.ExitStatus())
				}
			} else {
				panic(err)
			}
			return
		}
		exitCh <- uint(0)
	}()
	return exitCh
}

func ensureCacheRepo(path string) {
	cachePath := *repoPath + "/" + path
	if _, err := os.Stat(cachePath); os.IsNotExist(err) {
		os.MkdirAll(cachePath, 0755)
		cmd := exec.Command("git", "init", "--bare")
		cmd.Dir = cachePath
		err = cmd.Run()
		if err != nil {
			panic(err)
		}
	}
	receiver, err := filepath.Abs(receiver)
	if err != nil {
		panic(err)
	}
	ioutil.WriteFile(
		cachePath+"/hooks/pre-receive",
		[]byte(strings.Replace(PrereceiveHook, "{{RECEIVER}}", receiver, 1)),
		0755)
}

func publicKeyFingerprint(key ssh.PublicKey) string {
	var values []string
	h := md5.New()
	h.Write(ssh.MarshalPublicKey(key))
	for _, value := range h.Sum(nil) {
		values = append(values, fmt.Sprintf("%x", value))
	}
	return strings.Join(values, ":")
}
<|endoftext|>


<commit_before>// +build linux,selinux

package selinux

import (
	"os"
	"testing"
)

func TestSetFileLabel(t *testing.T) {
	if GetEnabled() {
		tmp := "selinux_test"
		con := "system_u:object_r:bin_t:s0"
		out, _ := os.OpenFile(tmp, os.O_WRONLY|os.O_CREATE, 0)
		out.Close()
		err := SetFileLabel(tmp, con)
		if err != nil {
			t.Log("Setfilecon failed")
			t.Fatal(err)
		}
		filelabel, err := FileLabel(tmp)
		if err != nil {
			t.Log("FileLabel failed")
			t.Fatal(err)
		}
		if con != filelabel {
			t.Fatalf("FileLabel failed, returned %s expected %s", filelabel, con)
		}

		os.Remove(tmp)
	}
}

func TestSELinux(t *testing.T) {
	var (
		err            error
		plabel, flabel string
	)

	if GetEnabled() {
		t.Log("Enabled")
		plabel, flabel = ContainerLabels()
		t.Log(plabel)
		t.Log(flabel)
		ReleaseLabel(plabel)
		plabel, flabel = ContainerLabels()
		t.Log(plabel)
		t.Log(flabel)
		ReleaseLabel(plabel)
		t.Log("Enforcing Mode", EnforceMode())
		mode := DefaultEnforceMode()
		t.Log("Default Enforce Mode ", mode)

		defer SetEnforceMode(mode)
		if err := SetEnforceMode(Enforcing); err != nil {
			t.Fatalf("enforcing selinux failed: %v", err)
		}
		if err := SetEnforceMode(Permissive); err != nil {
			t.Fatalf("setting selinux mode to permissive failed: %v", err)
		}
		SetEnforceMode(mode)

		pid := os.Getpid()
		t.Logf("PID:%d MCS:%s\n", pid, intToMcs(pid, 1023))
		err = SetFSCreateLabel("unconfined_u:unconfined_r:unconfined_t:s0")
		if err == nil {
			t.Log(FSCreateLabel())
		} else {
			t.Log("SetFSCreateLabel failed", err)
			t.Fatal(err)
		}
		err = SetFSCreateLabel("")
		if err == nil {
			t.Log(FSCreateLabel())
		} else {
			t.Log("SetFSCreateLabel failed", err)
			t.Fatal(err)
		}
		t.Log(PidLabel(1))
	}
}

func TestCanonicalizeContext(t *testing.T) {
	if GetEnabled() {
		con := "system_u:object_r:bin_t:s0:c1,c2,c3"
		checkcon := "system_u:object_r:bin_t:s0:c1.c3"
		newcon, err := CanonicalizeContext(con)
		if err != nil {
			t.Fatal(err)
		}
		if newcon != checkcon {
			t.Fatalf("CanonicalizeContext(%s) returned %s expected %s", con, newcon, checkcon)
		}
		con = "system_u:object_r:bin_t:s0:c5,c2"
		checkcon = "system_u:object_r:bin_t:s0:c2,c5"
		newcon, err = CanonicalizeContext(con)
		if err != nil {
			t.Fatal(err)
		}
		if newcon != checkcon {
			t.Fatalf("CanonicalizeContext(%s) returned %s expected %s", con, newcon, checkcon)
		}
	}
}
<commit_msg>selinux_test.go: remove selinux build tag<commit_after>// +build linux

package selinux

import (
	"os"
	"testing"
)

func TestSetFileLabel(t *testing.T) {
	if GetEnabled() {
		tmp := "selinux_test"
		con := "system_u:object_r:bin_t:s0"
		out, _ := os.OpenFile(tmp, os.O_WRONLY|os.O_CREATE, 0)
		out.Close()
		err := SetFileLabel(tmp, con)
		if err != nil {
			t.Log("Setfilecon failed")
			t.Fatal(err)
		}
		filelabel, err := FileLabel(tmp)
		if err != nil {
			t.Log("FileLabel failed")
			t.Fatal(err)
		}
		if con != filelabel {
			t.Fatalf("FileLabel failed, returned %s expected %s", filelabel, con)
		}

		os.Remove(tmp)
	}
}

func TestSELinux(t *testing.T) {
	var (
		err            error
		plabel, flabel string
	)

	if GetEnabled() {
		t.Log("Enabled")
		plabel, flabel = ContainerLabels()
		t.Log(plabel)
		t.Log(flabel)
		ReleaseLabel(plabel)
		plabel, flabel = ContainerLabels()
		t.Log(plabel)
		t.Log(flabel)
		ReleaseLabel(plabel)
		t.Log("Enforcing Mode", EnforceMode())
		mode := DefaultEnforceMode()
		t.Log("Default Enforce Mode ", mode)

		defer SetEnforceMode(mode)
		if err := SetEnforceMode(Enforcing); err != nil {
			t.Fatalf("enforcing selinux failed: %v", err)
		}
		if err := SetEnforceMode(Permissive); err != nil {
			t.Fatalf("setting selinux mode to permissive failed: %v", err)
		}
		SetEnforceMode(mode)

		pid := os.Getpid()
		t.Logf("PID:%d MCS:%s\n", pid, intToMcs(pid, 1023))
		err = SetFSCreateLabel("unconfined_u:unconfined_r:unconfined_t:s0")
		if err == nil {
			t.Log(FSCreateLabel())
		} else {
			t.Log("SetFSCreateLabel failed", err)
			t.Fatal(err)
		}
		err = SetFSCreateLabel("")
		if err == nil {
			t.Log(FSCreateLabel())
		} else {
			t.Log("SetFSCreateLabel failed", err)
			t.Fatal(err)
		}
		t.Log(PidLabel(1))
	}
}

func TestCanonicalizeContext(t *testing.T) {
	if GetEnabled() {
		con := "system_u:object_r:bin_t:s0:c1,c2,c3"
		checkcon := "system_u:object_r:bin_t:s0:c1.c3"
		newcon, err := CanonicalizeContext(con)
		if err != nil {
			t.Fatal(err)
		}
		if newcon != checkcon {
			t.Fatalf("CanonicalizeContext(%s) returned %s expected %s", con, newcon, checkcon)
		}
		con = "system_u:object_r:bin_t:s0:c5,c2"
		checkcon = "system_u:object_r:bin_t:s0:c2,c5"
		newcon, err = CanonicalizeContext(con)
		if err != nil {
			t.Fatal(err)
		}
		if newcon != checkcon {
			t.Fatalf("CanonicalizeContext(%s) returned %s expected %s", con, newcon, checkcon)
		}
	}
}
<|endoftext|>


<commit_before>package speaker

import (
	"math"
	"sync"
	"time"

	"github.com/faiface/pixel/audio"
	"github.com/hajimehoshi/oto"
	"github.com/pkg/errors"
)

var (
	streamerMu sync.Mutex
	streamer   audio.Streamer
	samples    [][2]float64
	buf        []byte

	playerMu sync.Mutex
	player   *oto.Player
)

// Init initializes audio playback through speaker. Must be called before using this package. The
// value of audio.SampleRate must be set (or left to the default) before calling this function.
//
// The bufferSize argument specifies the length of the speaker's buffer. On calling Update, speaker
// pulls this amount of data from the playing Streamers and starts playing this data. Bigger
// bufferSize means lower CPU usage and more reliable playback. Lower bufferSize means better
// responsiveness and less delay.
func Init(bufferSize time.Duration) error {
	playerMu.Lock()
	defer playerMu.Unlock()

	if player != nil {
		player.Close()
		player = nil
	}

	numSamples := int(math.Ceil(bufferSize.Seconds() * audio.SampleRate))
	numBytes := numSamples * 4

	var err error
	player, err = oto.NewPlayer(int(audio.SampleRate), 2, 2, numBytes)
	if err != nil {
		return errors.Wrap(err, "failed to initialize speaker")
	}

	samples = make([][2]float64, numSamples)
	buf = make([]byte, numBytes)

	return nil
}

// Play starts playing the provided Streamer through the speaker.
func Play(s audio.Streamer) {
	streamerMu.Lock()
	streamer = s
	streamerMu.Unlock()
}

// Update pulls new data from the playing Streamers and sends it to the speaker. Blocks until the
// data is sent and started playing.
//
// This function should be called at least once the duration of bufferSize given in Init, but it's
// recommended to call it more frequently to avoid glitches.
func Update() error {
	streamerMu.Lock()
	// pull data from the streamer, if any
	n := 0
	if streamer != nil {
		var ok bool
		n, ok = streamer.Stream(samples)
		if !ok {
			streamer = nil
		}
	}
	// convert samples to bytes
	for i := range samples[:n] {
		for c := range samples[i] {
			val := samples[i][c]
			if val < -1 {
				val = -1
			}
			if val > +1 {
				val = +1
			}
			valInt16 := int16(val * (1 << 15))
			low := byte(valInt16 % (1 << 8))
			high := byte(valInt16 / (1 << 8))
			buf[i*4+c*2+0] = low
			buf[i*4+c*2+1] = high
		}
	}
	// fill the rest with silence
	for i := n * 4; i < len(buf); i++ {
		buf[i] = 0
	}
	streamerMu.Unlock()

	// send data to speaker
	playerMu.Lock()
	player.Write(buf)
	playerMu.Unlock()

	return nil
}
<commit_msg>fix edge sample value (-1 and +1) overflow<commit_after>package speaker

import (
	"math"
	"sync"
	"time"

	"github.com/faiface/pixel/audio"
	"github.com/hajimehoshi/oto"
	"github.com/pkg/errors"
)

var (
	streamerMu sync.Mutex
	streamer   audio.Streamer
	samples    [][2]float64
	buf        []byte

	playerMu sync.Mutex
	player   *oto.Player
)

// Init initializes audio playback through speaker. Must be called before using this package. The
// value of audio.SampleRate must be set (or left to the default) before calling this function.
//
// The bufferSize argument specifies the length of the speaker's buffer. On calling Update, speaker
// pulls this amount of data from the playing Streamers and starts playing this data. Bigger
// bufferSize means lower CPU usage and more reliable playback. Lower bufferSize means better
// responsiveness and less delay.
func Init(bufferSize time.Duration) error {
	playerMu.Lock()
	defer playerMu.Unlock()

	if player != nil {
		player.Close()
		player = nil
	}

	numSamples := int(math.Ceil(bufferSize.Seconds() * audio.SampleRate))
	numBytes := numSamples * 4

	var err error
	player, err = oto.NewPlayer(int(audio.SampleRate), 2, 2, numBytes)
	if err != nil {
		return errors.Wrap(err, "failed to initialize speaker")
	}

	samples = make([][2]float64, numSamples)
	buf = make([]byte, numBytes)

	return nil
}

// Play starts playing the provided Streamer through the speaker.
func Play(s audio.Streamer) {
	streamerMu.Lock()
	streamer = s
	streamerMu.Unlock()
}

// Update pulls new data from the playing Streamers and sends it to the speaker. Blocks until the
// data is sent and started playing.
//
// This function should be called at least once the duration of bufferSize given in Init, but it's
// recommended to call it more frequently to avoid glitches.
func Update() error {
	streamerMu.Lock()
	// pull data from the streamer, if any
	n := 0
	if streamer != nil {
		var ok bool
		n, ok = streamer.Stream(samples)
		if !ok {
			streamer = nil
		}
	}
	// convert samples to bytes
	for i := range samples[:n] {
		for c := range samples[i] {
			val := samples[i][c]
			if val < -1 {
				val = -1
			}
			if val > +1 {
				val = +1
			}
			valInt16 := int16(val * (1<<15 - 1))
			low := byte(valInt16 % (1 << 8))
			high := byte(valInt16 / (1 << 8))
			buf[i*4+c*2+0] = low
			buf[i*4+c*2+1] = high
		}
	}
	// fill the rest with silence
	for i := n * 4; i < len(buf); i++ {
		buf[i] = 0
	}
	streamerMu.Unlock()

	// send data to speaker
	playerMu.Lock()
	player.Write(buf)
	playerMu.Unlock()

	return nil
}
<|endoftext|>


<commit_before>// Copyright (c) 2013-2014 by Michael Dvorkin. All Rights Reserved.
// Use of this source code is governed by a MIT-style license that can
// be found in the LICENSE file.

package donna

import(
	`fmt`
	`io/ioutil`
	`regexp`
	`strconv`
	`strings`
	`time`
)

const (
	escRed   = "\033[0;31m"
	escGreen = "\033[0;32m"
	escTeal  = "\033[0;36m"
	escNone  = "\033[0m"
)

func (e *Engine) replBestMove(move Move) *Engine {
	fmt.Printf(escTeal + "Donna's move: %s", move)
	if game.nodes == 0 {
		fmt.Printf(" (book)")
	}
	fmt.Println(escNone + "\n")

	return e
}

func (e *Engine) replPrincipal(depth, score, status int, duration int64) {
	fmt.Printf(`%2d %s %10d %10d %9d   `, depth, ms(duration), game.nodes, game.qnodes, nps(duration))
	switch status {
	case WhiteWon:
		fmt.Println(`1-0 White Checkmates`)
	case BlackWon:
		fmt.Println(`0-1 Black Checkmates`)
	case Stalemate:
		fmt.Println(`1/2 Stalemate`)
	case Repetition:
		fmt.Println(`1/2 Repetition`)
	case FiftyMoves:
		fmt.Println(`1/2 Fifty Moves`)
	case WhiteWinning, BlackWinning: // Show moves till checkmate.
		fmt.Printf("%4dX   %v Checkmate\n", (Checkmate - abs(score)) / 2, game.rootpv)
	default:
		fmt.Printf("%5.2f   %v\n", float32(score) / float32(onePawn), game.rootpv)
	}
}

func (e *Engine) Repl() *Engine {
	var game *Game
	var position *Position

	setup := func() {
		if game == nil || position == nil {
			game = NewGame()
			position = game.start()
			fmt.Printf("%s\n", position)
		}
	}

	think := func() {
		if move := game.Think(); move != 0 {
			position = position.makeMove(move)
			fmt.Printf("%s\n", position)
		}
	}

	benchmark := func(fileName string) {
		maxDepth, moveTime := e.options.maxDepth, e.options.moveTime
		e.options.maxDepth, e.options.moveTime = 0, 10000
		defer func() {
			e.options.maxDepth, e.options.moveTime = maxDepth, moveTime
		}()

		content, err := ioutil.ReadFile(fileName)
		if err == nil {
			total, solved := 0, 0
			re := regexp.MustCompile(`[\+\?!]`)

			NextLine:
			for _, line := range strings.Split(string(content), "\n") {
				if len(line) > 0 && line[0] != '#' {
					total++
					game := NewGame(line)
					position := game.start()

					best := strings.Split(line, ` # `)[1] // TODO: add support for "am" (avoid move).
					fmt.Printf(escTeal + "%d) %s for %s" + escNone + "\n%s\n", total, best, C(position.color), position)
					move := game.Think()

					for _, nextBest := range strings.Split(best, ` `) {
						if move.str() == re.ReplaceAllLiteralString(nextBest, ``) {
							solved++
							fmt.Printf(escGreen + "%d) Solved (%d/%d %2.1f%%)\n\n\n" + escNone, total, solved, total - solved, float32(solved) * 100.0 / float32(total))
							continue NextLine
						}
					}
					fmt.Printf(escRed + "%d) Not solved (%d/%d %2.1f%%)\n\n\n" + escNone, total, solved, total - solved, float32(solved) * 100.0 / float32(total))
				}
			}
		} else {
			fmt.Printf("Could not open benchmark file '%s'\n", fileName)
		}
	}

	perft := func(parameter string) {
		if parameter == `` {
			parameter = `5`
		}
		if depth, err := strconv.Atoi(parameter); err == nil {
			position := NewGame().start()
			start := time.Now()
			total := position.Perft(depth)
			finish := since(start)
			fmt.Printf("  Depth: %d\n", depth)
			fmt.Printf("  Nodes: %d\n", total)
			fmt.Printf("Elapsed: %s\n", ms(finish))
			fmt.Printf("Nodes/s: %dK\n", total / finish)
		}
	}

	fmt.Printf("Donna v%s Copyright (c) 2014 by Michael Dvorkin. All Rights Reserved.\nType ? for help.\n\n", Version)
	for command, parameter := ``, ``; ; command, parameter = ``, `` {
		fmt.Print(`donna> `)
		fmt.Scanln(&command, &parameter)

		switch command {
		case ``:
		case `bench`:
			benchmark(parameter)
		case `exit`, `quit`:
			return e
		case `go`:
			setup()
			think()
		case `help`, `?`:
			fmt.Println("The commands are:\n\n" +
				"  bench <file>   Run benchmarks\n" +
				"  exit           Exit the program\n" +
				"  go             Take side and make a move\n" +
				"  help           Display this help\n" +
				"  new            Start new game\n" +
				"  perft [depth]  Run perft test\n" +
				"  score          Show evaluation summary\n" +
				"  undo           Undo last move\n\n" +
				"To make a move use algebraic notation, for example e2e4, Ng1f3, or e7e8Q\n")
		case `new`:
			game, position = nil, nil
			setup()
		case `perft`:
			perft(parameter)
		case `score`:
			setup()
			_, metrics := position.EvaluateWithTrace()
			Summary(metrics)
		case `undo`:
			if position != nil {
				position = position.undoLastMove()
				fmt.Printf("%s\n", position)
			}
		default:
			setup()
			if move, validMoves := NewMoveFromString(position, command); move != 0 {
				position = position.makeMove(move)
				think()
			} else { // Invalid move or non-evasion on check.
				fancy := e.fancy; e.fancy = false
				fmt.Printf("%s appears to be an invalid move; valid moves are %v\n", command, validMoves)
				e.fancy = fancy
			}
		}
	}
	return e
}
<commit_msg>Recover if benchmark file has wrong format<commit_after>// Copyright (c) 2013-2014 by Michael Dvorkin. All Rights Reserved.
// Use of this source code is governed by a MIT-style license that can
// be found in the LICENSE file.

package donna

import(
	`fmt`
	`io/ioutil`
	`regexp`
	`strconv`
	`strings`
	`time`
)

const (
	escRed   = "\033[0;31m"
	escGreen = "\033[0;32m"
	escTeal  = "\033[0;36m"
	escNone  = "\033[0m"
)

func (e *Engine) replBestMove(move Move) *Engine {
	fmt.Printf(escTeal + "Donna's move: %s", move)
	if game.nodes == 0 {
		fmt.Printf(" (book)")
	}
	fmt.Println(escNone + "\n")

	return e
}

func (e *Engine) replPrincipal(depth, score, status int, duration int64) {
	fmt.Printf(`%2d %s %10d %10d %9d   `, depth, ms(duration), game.nodes, game.qnodes, nps(duration))
	switch status {
	case WhiteWon:
		fmt.Println(`1-0 White Checkmates`)
	case BlackWon:
		fmt.Println(`0-1 Black Checkmates`)
	case Stalemate:
		fmt.Println(`1/2 Stalemate`)
	case Repetition:
		fmt.Println(`1/2 Repetition`)
	case FiftyMoves:
		fmt.Println(`1/2 Fifty Moves`)
	case WhiteWinning, BlackWinning: // Show moves till checkmate.
		fmt.Printf("%4dX   %v Checkmate\n", (Checkmate - abs(score)) / 2, game.rootpv)
	default:
		fmt.Printf("%5.2f   %v\n", float32(score) / float32(onePawn), game.rootpv)
	}
}

func (e *Engine) Repl() *Engine {
	var game *Game
	var position *Position

	setup := func() {
		if game == nil || position == nil {
			game = NewGame()
			position = game.start()
			fmt.Printf("%s\n", position)
		}
	}

	think := func() {
		if move := game.Think(); move != 0 {
			position = position.makeMove(move)
			fmt.Printf("%s\n", position)
		}
	}

	benchmark := func(fileName string) {
		maxDepth, moveTime := e.options.maxDepth, e.options.moveTime
		e.options.maxDepth, e.options.moveTime = 0, 10000
		defer func() {
			e.options.maxDepth, e.options.moveTime = maxDepth, moveTime
			if err := recover(); err != nil {
				fmt.Printf("Error loading %s\n", fileName)
			}
		}()

		content, err := ioutil.ReadFile(fileName)
		if err == nil {
			total, solved := 0, 0
			re := regexp.MustCompile(`[\+\?!]`)

			NextLine:
			for _, line := range strings.Split(string(content), "\n") {
				if len(line) > 0 && line[0] != '#' {
					total++
					game := NewGame(line)
					position := game.start()

					best := strings.Split(line, ` # `)[1] // TODO: add support for "am" (avoid move).
					fmt.Printf(escTeal + "%d) %s for %s" + escNone + "\n%s\n", total, best, C(position.color), position)
					move := game.Think()

					for _, nextBest := range strings.Split(best, ` `) {
						if move.str() == re.ReplaceAllLiteralString(nextBest, ``) {
							solved++
							fmt.Printf(escGreen + "%d) Solved (%d/%d %2.1f%%)\n\n\n" + escNone, total, solved, total - solved, float32(solved) * 100.0 / float32(total))
							continue NextLine
						}
					}
					fmt.Printf(escRed + "%d) Not solved (%d/%d %2.1f%%)\n\n\n" + escNone, total, solved, total - solved, float32(solved) * 100.0 / float32(total))
				}
			}
		} else {
			fmt.Printf("Could not open benchmark file '%s'\n", fileName)
		}
	}

	perft := func(parameter string) {
		if parameter == `` {
			parameter = `5`
		}
		if depth, err := strconv.Atoi(parameter); err == nil {
			position := NewGame().start()
			start := time.Now()
			total := position.Perft(depth)
			finish := since(start)
			fmt.Printf("  Depth: %d\n", depth)
			fmt.Printf("  Nodes: %d\n", total)
			fmt.Printf("Elapsed: %s\n", ms(finish))
			fmt.Printf("Nodes/s: %dK\n", total / finish)
		}
	}

	fmt.Printf("Donna v%s Copyright (c) 2014 by Michael Dvorkin. All Rights Reserved.\nType ? for help.\n\n", Version)
	for command, parameter := ``, ``; ; command, parameter = ``, `` {
		fmt.Print(`donna> `)
		fmt.Scanln(&command, &parameter)

		switch command {
		case ``:
		case `bench`:
			benchmark(parameter)
		case `exit`, `quit`:
			return e
		case `go`:
			setup()
			think()
		case `help`, `?`:
			fmt.Println("The commands are:\n\n" +
				"  bench <file>   Run benchmarks\n" +
				"  exit           Exit the program\n" +
				"  go             Take side and make a move\n" +
				"  help           Display this help\n" +
				"  new            Start new game\n" +
				"  perft [depth]  Run perft test\n" +
				"  score          Show evaluation summary\n" +
				"  undo           Undo last move\n\n" +
				"To make a move use algebraic notation, for example e2e4, Ng1f3, or e7e8Q\n")
		case `new`:
			game, position = nil, nil
			setup()
		case `perft`:
			perft(parameter)
		case `score`:
			setup()
			_, metrics := position.EvaluateWithTrace()
			Summary(metrics)
		case `undo`:
			if position != nil {
				position = position.undoLastMove()
				fmt.Printf("%s\n", position)
			}
		default:
			setup()
			if move, validMoves := NewMoveFromString(position, command); move != 0 {
				position = position.makeMove(move)
				think()
			} else { // Invalid move or non-evasion on check.
				fancy := e.fancy; e.fancy = false
				fmt.Printf("%s appears to be an invalid move; valid moves are %v\n", command, validMoves)
				e.fancy = fancy
			}
		}
	}
	return e
}
<|endoftext|>


<commit_before>package model

import (
	"crypto/sha256"
	"database/sql"
	"encoding/hex"
	"fmt"
	"os"

	"github.com/tikasan/eventory/formater"
)

func Insert(db *sql.DB, Events []Event) error {
	stmtIns, err := db.Prepare("INSERT INTO m_event (event_id, api_id, title, description, url, limit_count, waitlisted, accepted, address, place, start_at, end_at, data_hash) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
	if err != nil {
		fmt.Fprint(os.Stderr, err)
		return err
	}

	var dataHashAgo string
	for _, ev := range Events {
		dataHashAgo = formater.ConcatenateString(ev.Title, ev.Desc, ev.Url, ev.Address, ev.Place, ev.StratAt, ev.EndAt)
		dataHashed := sha256.Sum256([]byte(dataHashAgo))
		ev.DataHash = hex.EncodeToString(dataHashed[:])
		if _, err = stmtIns.Exec(
			fmt.Sprintf("%d-%d", ev.ApiId, ev.EventId),
			ev.ApiId,
			ev.Title,
			ev.Desc,
			ev.Url,
			ev.Limit,
			ev.Waitlisted,
			ev.Accepted,
			ev.Address,
			ev.Place,
			formater.DateTimeFormatter(ev.StratAt),
			formater.DateTimeFormatter(ev.EndAt),
			ev.DataHash,
		); err != nil {
			// insertに失敗したらアップデートをかける
			query := "UPDATE m_event SET title = ?, description = ?, url = ?, limit_count = ?, waitlisted = ?, accepted = ?, address = ?, place = ?, start_at = ?, end_at = ?, data_hash = ? WHERE event_id = ? AND data_hash <> ?"
			if _, err := db.Exec(query,
				ev.Title,
				ev.Desc,
				ev.Url,
				ev.Limit,
				ev.Waitlisted,
				ev.Accepted,
				ev.Address,
				ev.Place,
				formater.DateTimeFormatter(ev.StratAt),
				formater.DateTimeFormatter(ev.EndAt),
				ev.DataHash,
				fmt.Sprintf("%d-%d", ev.ApiId, ev.EventId),
				ev.DataHash,
			); err != nil {
				fmt.Fprint(os.Stderr, err)
				return err
			}

		}
	}
	return nil
}

func EventAll(db *sql.DB) ([]Event, error) {
	rows, err := db.Query(`select * from m_event`)
	if err != nil {
		return nil, err
	}
	return ScanEvents(rows)
}

func EventAllNew(db *sql.DB, updatedAt string) ([]EventJson, error) {
	fmt.Println(updatedAt)

	stmtIns, err := db.Prepare("select event_id, api_id,title, url, limit_count, accepted, address ,place, start_at, end_at, id from m_event where end_at > now() AND updated_at > ?")
	if err != nil {
		fmt.Fprint(os.Stderr, err)
		return nil, err
	}
	rows, err := stmtIns.Query(updatedAt);
	if err != nil {
		fmt.Fprint(os.Stderr, err)
		return nil, err
	}
	return ScanEventsJson(rows)
}
<commit_msg>[golang]update: 更新対象とする基準を増やした（参加人数など）<commit_after>package model

import (
	"crypto/sha256"
	"database/sql"
	"encoding/hex"
	"fmt"
	"os"

	"github.com/tikasan/eventory/formater"
)

func Insert(db *sql.DB, Events []Event) error {
	stmtIns, err := db.Prepare("INSERT INTO m_event (event_id, api_id, title, description, url, limit_count, waitlisted, accepted, address, place, start_at, end_at, data_hash) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
	if err != nil {
		fmt.Fprint(os.Stderr, err)
		return err
	}

	var dataHashAgo string
	for _, ev := range Events {
		dataHashAgo = formater.ConcatenateString(ev.Title, ev.Desc, ev.Url, ev.Address, string(ev.Limit), string(ev.Accepted), ev.Place, ev.StratAt, ev.EndAt)
		dataHashed := sha256.Sum256([]byte(dataHashAgo))
		ev.DataHash = hex.EncodeToString(dataHashed[:])
		if _, err = stmtIns.Exec(
			fmt.Sprintf("%d-%d", ev.ApiId, ev.EventId),
			ev.ApiId,
			ev.Title,
			ev.Desc,
			ev.Url,
			ev.Limit,
			ev.Waitlisted,
			ev.Accepted,
			ev.Address,
			ev.Place,
			formater.DateTimeFormatter(ev.StratAt),
			formater.DateTimeFormatter(ev.EndAt),
			ev.DataHash,
		); err != nil {
			// insertに失敗したらアップデートをかける
			query := "UPDATE m_event SET title = ?, description = ?, url = ?, limit_count = ?, waitlisted = ?, accepted = ?, address = ?, place = ?, start_at = ?, end_at = ?, data_hash = ? WHERE event_id = ? AND data_hash <> ?"
			if _, err := db.Exec(query,
				ev.Title,
				ev.Desc,
				ev.Url,
				ev.Limit,
				ev.Waitlisted,
				ev.Accepted,
				ev.Address,
				ev.Place,
				formater.DateTimeFormatter(ev.StratAt),
				formater.DateTimeFormatter(ev.EndAt),
				ev.DataHash,
				fmt.Sprintf("%d-%d", ev.ApiId, ev.EventId),
				ev.DataHash,
			); err != nil {
				fmt.Fprint(os.Stderr, err)
				return err
			}

		}
	}
	return nil
}

func EventAll(db *sql.DB) ([]Event, error) {
	rows, err := db.Query(`select * from m_event`)
	if err != nil {
		return nil, err
	}
	return ScanEvents(rows)
}

func EventAllNew(db *sql.DB, updatedAt string) ([]EventJson, error) {
	fmt.Println(updatedAt)

	stmtIns, err := db.Prepare("select event_id, api_id,title, url, limit_count, accepted, address ,place, start_at, end_at, id from m_event where end_at > now() AND updated_at > ?")
	if err != nil {
		fmt.Fprint(os.Stderr, err)
		return nil, err
	}
	rows, err := stmtIns.Query(updatedAt);
	if err != nil {
		fmt.Fprint(os.Stderr, err)
		return nil, err
	}
	return ScanEventsJson(rows)
}
<|endoftext|>


<commit_before>package model

import (
	"fmt"
	"time"

	"github.com/jinzhu/gorm"
	_ "github.com/lib/pq"
)

var DB gorm.DB

type User struct {
	Id             int64
	Email          string `sql:"type:text;"`
	HashedPassword string
	CreatedAt      time.Time
	UpdatedAt      time.Time
	DeletedAt      time.Time
}

type Client struct {
	Id         int64
	User_id    int64
	SessionKey string
	CreatedAt  time.Time
	UpdatedAt  time.Time
	DeletedAt  time.Time
}

type FileAction struct {
	Id        int64
	Client_id int64
	IsCreate  bool
	File_id   string
	CreatedAt time.Time
}

type File struct {
	Id        int64
	User_id   int64
	Name      string
	Hash      string
	Size      int64
	Path      string `sql:"type:text;"`
	CreatedAt time.Time
}

type FileSystemFile struct {
	Id      int64
	User_id int64
	File_id int64
	Path    string `sql:"type:text;"`
}

func main() {
	db, err := gorm.Open("postgres", "dbname=gobox sslmode=disable")
	if err != nil {
		fmt.Println(err)
	}
	query := db.AutoMigrate(&User{}, &Client{})
	if query.Error != nil {
		fmt.Println(query.Error)
	}
	fmt.Println(query)

	// meta, err := convertJsonStringToMetaStruct(testJsonString)
	// fmt.Println(meta)
	// file, err := convertMetaStructToFileStruct(meta)
	// fmt.Println(file)
}
<commit_msg>refined model definitions for use on client <commit_after>package model

import (
	"fmt"
	"time"

	"github.com/jinzhu/gorm"
	_ "github.com/lib/pq"
)

var DB gorm.DB

type User struct {
	Id             int64
	Email          string `sql:"type:text;"`
	HashedPassword string
	CreatedAt      time.Time
	UpdatedAt      time.Time
	DeletedAt      time.Time
}

type Client struct {
	Id         int64
	UserId     int64
	SessionKey string
	CreatedAt  time.Time
	UpdatedAt  time.Time
	DeletedAt  time.Time
}

type FileAction struct {
	Id        int64
	ClientId  int64
	IsCreate  bool
	CreatedAt time.Time
	File      File
}

type File struct {
	Id        int64
	UserId    int64
	Name      string
	Hash      string
	Size      int64
	Modified  time.Time
	Path      string `sql:"type:text;"`
	CreatedAt time.Time
}

type FileSystemFile struct {
	Id      int64
	User_id int64
	File_id int64
	Path    string `sql:"type:text;"`
}

func main() {
	db, err := gorm.Open("postgres", "dbname=gobox sslmode=disable")
	if err != nil {
		fmt.Println(err)
	}
	query := db.AutoMigrate(&User{}, &Client{})
	if query.Error != nil {
		fmt.Println(query.Error)
	}
	fmt.Println(query)

	// meta, err := convertJsonStringToMetaStruct(testJsonString)
	// fmt.Println(meta)
	// file, err := convertMetaStructToFileStruct(meta)
	// fmt.Println(file)
}
<|endoftext|>


<commit_before>// Copyright 2013 The go-logger Authors. All rights reserved.
// This code is MIT licensed. See the LICENSE file for more info.

// Package logger is a better logging system for Go than the generic log
// package in the Go Standard Library. The logger packages provides colored
// output, logging levels, custom log formatting, and simultaneous logging
// output stream to stdout, stderr, and os.File.
package logger

import (
	"fmt"
	"io"
	"os"
	"runtime"
	"sync"
	"text/template"
	"time"
)

// Used for string output of the logging object
var levels = [5]string{
	"DEBUG",
	"INFO",
	"WARNING",
	"ERROR",
	"CRITICAL",
}

type level int

// Returns the string representation of the level
func (l level) String() string { return levels[l] }

// The DEBUG level is the lowest possible output level. This is meant for
// development use. The default output level is WARNING.
const (
	// DEBUG level messages should be used for development logging instead
	// of Printf calls. When used in this manner, instead of sprinkling
	// Printf calls everywhere and then having to remove them once the bug
	// is fixed, the developer can simply change to a higher logging level
	// and the debug messages will not be sent to the output stream.
	DEBUG level = iota
	// Info level messages should be used to convey more informative output
	// than debug that could be used by a user.
	INFO
	// Warning messages should be used to notify the user that something
	// worked, but the expected value was not the result.
	WARNING
	// Error messages should be used when something just did not work at
	// all.
	ERROR
	// Critical messages are used when something is completely broken and
	// unrecoverable. Critical messages are usually followed by os.Exit().
	CRITICAL
)

// These flags define which text to prefix to each log entry generated by the Logger.
const (
	// Bits or'ed together to control what's printed.
	Ldate = 1 << iota
	// full file name and line number: /a/b/c/d.go:23
	Llongfile
	// base file name and line number: d.go:23. overrides Llongfile
	Lshortfile
	// Use ansi escape sequences
	Lansi
	// initial values for the standard logger
	LstdFlags = Ldate | Lansi
)

const (
	logFormat = "{{.Prefix}}: {{.Date}}: {{.File}}: {{.Text}}"
	defPrefix = ">>>"
)

var (
	defColorPrefix = AnsiEscape(BOLD, GREEN, ">>>", OFF)
	// funcMap contains the available functions to the log format template.
	funcMap = template.FuncMap{"ansiEscape": AnsiEscape}
	// std is the default logger object
	std = New(os.Stderr, time.RubyDate, logFormat, WARNING, LstdFlags)
)

// A Logger represents an active logging object that generates lines of output
// to an io.Writer. Each logging operation makes a single call to the Writer's
// Write method. A Logger can be used simultaneously from multiple goroutines;
// it guarantees to serialize access to the Writer.
type Logger struct {
	mu          sync.Mutex // Ensures atomic writes
	buf         []byte     // For marshaling output to write
	colors      bool       // Enable/Disable colored output
	dateFormat  string     // time.RubyDate is the default format
	flags       int        // Properties of the output
	level       level      // The default level is warning
	logTemplate string     // The format order of the output
	stream      io.Writer  // Destination for output
}

// formatOutput is used by Output() to apply the desired output format using
// the logTemplate. Using this template, an output string is built containing
// the desired structure such as prefix, date, and file + line number.
func (l *Logger) formatOutput(buf *[]byte, t time.Time, file string,
	line int, text string) {
	l.buf = append(l.buf, t.Format(l.dateFormat)...)
	if len(text) > 0 && text[len(text)-1] != '\n' {
		l.buf = append(l.buf, '\n')
	}
}

// Output is used by all of the logging functions to send output to the output
// stream.
//
// stream will be used as the output stream the text will be written to.
//
// calldepth is the number of stack frames to skip when getting the file
// name of original calling function for file name output.
//
// text is the string to append to the assembled log format output.
func (l *Logger) Output(stream io.Writer,
	calldepth int, text string) (n int, err error) {
	now := time.Now()
	var file string
	var line int
	l.mu.Lock()
	defer l.mu.Unlock()
	if l.flags&(Lshortfile|Llongfile) != 0 {
		// release lock while getting caller info - it's expensive.
		l.mu.Unlock()
		var ok bool
		_, file, line, ok = runtime.Caller(calldepth)
		if !ok {
			file = "???"
			line = 0
		}
		l.mu.Lock()
	}
	l.buf = l.buf[:0]
	l.formatOutput(&l.buf, now, file, line, text)
	n, err = stream.Write(l.buf)
	return int(n), err
}

// format is the possible values that can be used in a log output format
type format struct {
	Prefix string
	Date   string
	File   string
	Text   string
}

// New creates a new logger object.
func New(stream io.Writer, dateFormat string,
	logTemplate string, level level, flags int) *Logger {
	return &Logger{stream: stream, dateFormat: dateFormat,
		logTemplate: logTemplate, level: level, flags: flags}
}

// SetLevel sets the logging output level for the standard logger.
func SetLevel(level level) {
	std.level = level
}

// Level returns the logging level of the current logger object
func Level() level {
	return std.level
}

// SetStream sets the output stream for the standard logger object.
func SetStream(stream io.Writer) {
	std.stream = stream
}

// Stream gets the output stream for the standard logger object.
func Stream() io.Writer {
	return std.stream
}

// Print sends output to the standard logger output stream regardless of
// logging level including the logger format properties and flags. Spaces are
// added between operands when neither is a string. It returns the number of
// bytes written and any write error encountered.
func Print(v ...interface{}) (n int, err error) {
	return std.Output(os.Stdout, 2, fmt.Sprint(v...))
}

// Println formats using the default formats for its operands and writes to
// standard output. Spaces are always added between operands and a newline is
// appended. It returns the number of bytes written and any write error
// encountered.
func Println(v ...interface{}) (n int, err error) {
	return std.Output(os.Stdout, 2, fmt.Sprintln(v...))
}

// Printf formats according to a format specifier and writes to standard
// output. It returns the number of bytes written and any write error
// encountered.
func Printf(format string, v ...interface{}) (n int, err error) {
	return std.Output(os.Stdout, 2, fmt.Sprintf(format, v...))
}
<commit_msg>Add prefix arg to New()<commit_after>// Copyright 2013 The go-logger Authors. All rights reserved.
// This code is MIT licensed. See the LICENSE file for more info.

// Package logger is a better logging system for Go than the generic log
// package in the Go Standard Library. The logger packages provides colored
// output, logging levels, custom log formatting, and simultaneous logging
// output stream to stdout, stderr, and os.File.
package logger

import (
	"fmt"
	"io"
	"os"
	"runtime"
	"sync"
	"text/template"
	"time"
)

// Used for string output of the logging object
var levels = [5]string{
	"DEBUG",
	"INFO",
	"WARNING",
	"ERROR",
	"CRITICAL",
}

type level int

// Returns the string representation of the level
func (l level) String() string { return levels[l] }

// The DEBUG level is the lowest possible output level. This is meant for
// development use. The default output level is WARNING.
const (
	// DEBUG level messages should be used for development logging instead
	// of Printf calls. When used in this manner, instead of sprinkling
	// Printf calls everywhere and then having to remove them once the bug
	// is fixed, the developer can simply change to a higher logging level
	// and the debug messages will not be sent to the output stream.
	DEBUG level = iota
	// Info level messages should be used to convey more informative output
	// than debug that could be used by a user.
	INFO
	// Warning messages should be used to notify the user that something
	// worked, but the expected value was not the result.
	WARNING
	// Error messages should be used when something just did not work at
	// all.
	ERROR
	// Critical messages are used when something is completely broken and
	// unrecoverable. Critical messages are usually followed by os.Exit().
	CRITICAL
)

// These flags define which text to prefix to each log entry generated by the Logger.
const (
	// Bits or'ed together to control what's printed.
	Ldate = 1 << iota
	// full file name and line number: /a/b/c/d.go:23
	Llongfile
	// base file name and line number: d.go:23. overrides Llongfile
	Lshortfile
	// Use ansi escape sequences
	Lansi
	// initial values for the standard logger
	LstdFlags = Ldate | Lansi
)

const (
	logFormat = "{{.Prefix}}: {{.Date}}: {{.File}}: {{.Text}}"
	defPrefix = ">>>"
)

var (
	defColorPrefix = AnsiEscape(BOLD, GREEN, ">>>", OFF)
	// funcMap contains the available functions to the log format template.
	funcMap = template.FuncMap{"ansiEscape": AnsiEscape}
	// std is the default logger object
	std = New(os.Stderr, defColorPrefix, time.RubyDate, logFormat, WARNING, LstdFlags)
)

// A Logger represents an active logging object that generates lines of output
// to an io.Writer. Each logging operation makes a single call to the Writer's
// Write method. A Logger can be used simultaneously from multiple goroutines;
// it guarantees to serialize access to the Writer.
type Logger struct {
	mu          sync.Mutex // Ensures atomic writes
	buf         []byte     // For marshaling output to write
	colors      bool       // Enable/Disable colored output
	dateFormat  string     // time.RubyDate is the default format
	flags       int        // Properties of the output
	level       level      // The default level is warning
	logTemplate string     // The format order of the output
	prefix      string     // Inserted into every logging output
	stream      io.Writer  // Destination for output
}

// formatOutput is used by Output() to apply the desired output format using
// the logTemplate. Using this template, an output string is built containing
// the desired structure such as prefix, date, and file + line number.
func (l *Logger) formatOutput(buf *[]byte, t time.Time, file string,
	line int, text string) {
	l.buf = append(l.buf, t.Format(l.dateFormat)...)
	if len(text) > 0 && text[len(text)-1] != '\n' {
		l.buf = append(l.buf, '\n')
	}
}

// Output is used by all of the logging functions to send output to the output
// stream.
//
// stream will be used as the output stream the text will be written to.
//
// calldepth is the number of stack frames to skip when getting the file
// name of original calling function for file name output.
//
// text is the string to append to the assembled log format output.
func (l *Logger) Output(stream io.Writer,
	calldepth int, text string) (n int, err error) {
	now := time.Now()
	var file string
	var line int
	l.mu.Lock()
	defer l.mu.Unlock()
	if l.flags&(Lshortfile|Llongfile) != 0 {
		// release lock while getting caller info - it's expensive.
		l.mu.Unlock()
		var ok bool
		_, file, line, ok = runtime.Caller(calldepth)
		if !ok {
			file = "???"
			line = 0
		}
		l.mu.Lock()
	}
	l.buf = l.buf[:0]
	l.formatOutput(&l.buf, now, file, line, text)
	n, err = stream.Write(l.buf)
	return int(n), err
}

// format is the possible values that can be used in a log output format
type format struct {
	Prefix string
	Date   string
	File   string
	Text   string
}

// New creates a new logger object.
func New(stream io.Writer, prefix string, dateFormat string,
	logTemplate string, level level, flags int) *Logger {
	return &Logger{stream: stream, prefix: prefix, dateFormat: dateFormat,
		logTemplate: logTemplate, level: level, flags: flags}
}

// SetLevel sets the logging output level for the standard logger.
func SetLevel(level level) {
	std.level = level
}

// Level returns the logging level of the current logger object
func Level() level {
	return std.level
}

// SetStream sets the output stream for the standard logger object.
func SetStream(stream io.Writer) {
	std.stream = stream
}

// Prefix returns the output prefix.
func Prefix() string {
	return std.prefix
}

// SetPrefix set the output prefix.
func SetPrefix(prefix string) {
	std.prefix = prefix
}

// Print sends output to the standard logger output stream regardless of
// logging level including the logger format properties and flags. Spaces are
// added between operands when neither is a string. It returns the number of
// bytes written and any write error encountered.
func Print(v ...interface{}) (n int, err error) {
	return std.Output(os.Stdout, 2, fmt.Sprint(v...))
}

// Println formats using the default formats for its operands and writes to
// standard output. Spaces are always added between operands and a newline is
// appended. It returns the number of bytes written and any write error
// encountered.
func Println(v ...interface{}) (n int, err error) {
	return std.Output(os.Stdout, 2, fmt.Sprintln(v...))
}

// Printf formats according to a format specifier and writes to standard
// output. It returns the number of bytes written and any write error
// encountered.
func Printf(format string, v ...interface{}) (n int, err error) {
	return std.Output(os.Stdout, 2, fmt.Sprintf(format, v...))
}
<|endoftext|>


<commit_before>/*
 * Copyright 2014 Xuyuan Pang <xuyuanp@gmail.com>
 *
 * 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 glogger

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"
	"runtime"
	"time"
)

// gLogger is the default Logger
type gLogger struct {
	GroupFilter
	handlerGroup
	name  string
	level LogLevel
	ch    chan *Record
}

// NewLogger return a new Logger with debug level as default.
func NewLogger() *gLogger {
	l := &gLogger{
		level: DebugLevel,
		ch:    make(chan *Record, 100000),
	}
	go l.run()
	return l
}

// Debug see details in Logger interface
func (l *gLogger) Debug(f string, v ...interface{}) {
	l.log(DebugLevel, fmt.Sprintf(f, v...))
}

// Info see details in Logger interface
func (l *gLogger) Info(f string, v ...interface{}) {
	l.log(InfoLevel, fmt.Sprintf(f, v...))
}

// Warning see details in Logger interface
func (l *gLogger) Warning(f string, v ...interface{}) {
	l.log(WarnLevel, fmt.Sprintf(f, v...))
}

// Error see details in Logger interface
func (l *gLogger) Error(f string, v ...interface{}) {
	l.log(ErrorLevel, fmt.Sprintf(f, v...))
}

// Critical see details in Logger interface
func (l *gLogger) Critical(f string, v ...interface{}) {
	l.log(CriticalLevel, fmt.Sprintf(f, v...))
}

func (l *gLogger) log(level LogLevel, msg string) {
	if level < l.level {
		return
	}
	now := time.Now()
	pc, file, line, ok := runtime.Caller(2)
	var funcname string
	if !ok {
		file = "???"
		line = 0
		funcname = "???"
	} else {
		funcname = runtime.FuncForPC(pc).Name()
	}
	rec := NewRecord(l.name, now, level, file, funcname, line, msg)
	if !l.Filter(rec) {
		return
	}
	l.ch <- rec
}

func (l *gLogger) run() {
	for {
		rec := <-l.ch
		l.Handle(rec)
	}
}

func (l *gLogger) Level() LogLevel {
	return l.level
}

func (l *gLogger) SetLevel(level LogLevel) {
	l.level = level
}

func (l *gLogger) LoadConfig(config []byte) {
	var m map[string]interface{}
	if err := json.Unmarshal(config, &m); err == nil {
		l.LoadConfigFromMap(m)
	} else {
		panic(err)
	}
}

func (l *gLogger) LoadConfigFromMap(config map[string]interface{}) {
	if level, ok := config["level"]; ok {
		l.level = StringToLevel[level.(string)]
	} else {
		panic("'level' field is required")
	}
	if handlers, ok := config["handlers"]; ok {
		if len(handlers.([]interface{})) == 0 {
			panic("handler name is required")
		}
		for _, hname := range handlers.([]interface{}) {
			if handler := GetHandler(hname.(string)); handler != nil {
				l.AddHandler(handler)
			} else {
				panic("unknown handler name: " + hname.(string))
			}
		}
	} else {
		panic("'handlers' field is required")
	}
}

func (l *gLogger) LoadConfigFromFile(fileName string) {
	if file, err := os.Open(fileName); err == nil {
		defer file.Close()
		if code, err := ioutil.ReadAll(file); err == nil {
			l.LoadConfig(code)
		} else {
			panic(err)
		}
	} else {
		panic(err)
	}
}
<commit_msg>add select<commit_after>/*
 * Copyright 2014 Xuyuan Pang <xuyuanp@gmail.com>
 *
 * 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 glogger

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"
	"runtime"
	"time"
)

// gLogger is the default Logger
type gLogger struct {
	GroupFilter
	handlerGroup
	name  string
	level LogLevel
	ch    chan *Record
}

// NewLogger return a new Logger with debug level as default.
func NewLogger() *gLogger {
	l := &gLogger{
		level: DebugLevel,
		ch:    make(chan *Record, 100000),
	}
	go l.run()
	return l
}

// Debug see details in Logger interface
func (l *gLogger) Debug(f string, v ...interface{}) {
	l.log(DebugLevel, fmt.Sprintf(f, v...))
}

// Info see details in Logger interface
func (l *gLogger) Info(f string, v ...interface{}) {
	l.log(InfoLevel, fmt.Sprintf(f, v...))
}

// Warning see details in Logger interface
func (l *gLogger) Warning(f string, v ...interface{}) {
	l.log(WarnLevel, fmt.Sprintf(f, v...))
}

// Error see details in Logger interface
func (l *gLogger) Error(f string, v ...interface{}) {
	l.log(ErrorLevel, fmt.Sprintf(f, v...))
}

// Critical see details in Logger interface
func (l *gLogger) Critical(f string, v ...interface{}) {
	l.log(CriticalLevel, fmt.Sprintf(f, v...))
}

func (l *gLogger) log(level LogLevel, msg string) {
	if level < l.level {
		return
	}
	now := time.Now()
	pc, file, line, ok := runtime.Caller(2)
	var funcname string
	if !ok {
		file = "???"
		line = 0
		funcname = "???"
	} else {
		funcname = runtime.FuncForPC(pc).Name()
	}
	rec := NewRecord(l.name, now, level, file, funcname, line, msg)
	if !l.Filter(rec) {
		return
	}
	l.ch <- rec
}

func (l *gLogger) run() {
	for {
		select {
		case rec := <-l.ch:
			l.Handle(rec)
		}
	}
}

func (l *gLogger) Level() LogLevel {
	return l.level
}

func (l *gLogger) SetLevel(level LogLevel) {
	l.level = level
}

func (l *gLogger) LoadConfig(config []byte) {
	var m map[string]interface{}
	if err := json.Unmarshal(config, &m); err == nil {
		l.LoadConfigFromMap(m)
	} else {
		panic(err)
	}
}

func (l *gLogger) LoadConfigFromMap(config map[string]interface{}) {
	if level, ok := config["level"]; ok {
		l.level = StringToLevel[level.(string)]
	} else {
		panic("'level' field is required")
	}
	if handlers, ok := config["handlers"]; ok {
		if len(handlers.([]interface{})) == 0 {
			panic("handler name is required")
		}
		for _, hname := range handlers.([]interface{}) {
			if handler := GetHandler(hname.(string)); handler != nil {
				l.AddHandler(handler)
			} else {
				panic("unknown handler name: " + hname.(string))
			}
		}
	} else {
		panic("'handlers' field is required")
	}
}

func (l *gLogger) LoadConfigFromFile(fileName string) {
	if file, err := os.Open(fileName); err == nil {
		defer file.Close()
		if code, err := ioutil.ReadAll(file); err == nil {
			l.LoadConfig(code)
		} else {
			panic(err)
		}
	} else {
		panic(err)
	}
}
<|endoftext|>


<commit_before>package main

import (
	"github.com/op/go-logging"

	"os"
)

var (
	log    = logging.MustGetLogger("syaro")
	format = logging.MustStringFormatter(
		"%{time:2006/01/02 15:04:05.000000} %{shortpkg:-6.6s} %{shortfunc:-12.12s} | %{color:bold}%{level:.4s}%{color:reset} %{color}%{message}%{color:reset}",
	)
)

func SetupLogger() {
	backend := logging.NewLogBackend(os.Stdout, "", 0)
	backendFormatter := logging.NewBackendFormatter(backend, format)
	backendLeveled := logging.AddModuleLevel(backendFormatter)
	if setting.verbose {
		backendLeveled.SetLevel(logging.DEBUG, "")
	} else {
		backendLeveled.SetLevel(logging.INFO, "")
	}
	log.SetBackend(backendLeveled)
}
<commit_msg>[MOD] logger (color, quiet)<commit_after>package main

import (
	"github.com/op/go-logging"

	"os"
)

const (
	LOG_FORMAT       = "%{time:2006/01/02 15:04:05.000000} syaro %{shortfunc:-12.12s} | %{level:.4s} %{message}"
	COLOR_LOG_FORMAT = "%{time:2006/01/02 15:04:05.000000} syaro %{shortfunc:-12.12s} | %{color:bold}%{level:.4s}%{color:reset} %{color}%{message}%{color:reset}"
)

var (
	log = logging.MustGetLogger("syaro")
)

func setupLogger() {
	backend := logging.NewLogBackend(os.Stdout, "", 0)
	var format logging.Formatter
	if setting.color {
		format = logging.MustStringFormatter(COLOR_LOG_FORMAT)
	} else {
		format = logging.MustStringFormatter(LOG_FORMAT)
	}
	backendFormatter := logging.NewBackendFormatter(backend, format)
	backendLeveled := logging.AddModuleLevel(backendFormatter)
	if setting.verbose {
		backendLeveled.SetLevel(logging.DEBUG, "")
	} else if setting.quiet {
		backendLeveled.SetLevel(logging.ERROR, "")
	} else {
		backendLeveled.SetLevel(logging.INFO, "")
	}
	log.SetBackend(backendLeveled)
}
<|endoftext|>


<commit_before>package main

import (
	"testing"
)

func TestParseErrata(t *testing.T) {
	got, err := ParseRemoteErrata("http://ftp.openbsd.org/pub/OpenBSD/patches/6.6/common/")
	if err != nil {
		t.Error(err)
	}
	l := len(got.List)
	if l < 1 {
		t.Errorf("errata count %d; want > 1", l)
	}

	erratum := got.List[len(got.List)-1]

	err = erratum.Fetch()
	if err != nil {
		t.Errorf("can't fetch data for erratum\n%s", err)
	}
}
<commit_msg>test against 6.7 errata<commit_after>package main

import (
	"testing"
)

func TestParseErrata(t *testing.T) {
	got, err := ParseRemoteErrata("http://ftp.openbsd.org/pub/OpenBSD/patches/6.7/common/")
	if err != nil {
		t.Error(err)
	}
	l := len(got.List)
	if l < 1 {
		t.Errorf("errata count %d; want > 1", l)
	}

	erratum := got.List[len(got.List)-1]

	err = erratum.Fetch()
	if err != nil {
		t.Errorf("can't fetch data for erratum\n%s", err)
	}
}
<|endoftext|>


<commit_before>/*

errors is a package that implements FriendlyError. FriendlyError implements
the error interface, but also has a FriendlyError() method that returns a
message that is reasonable to show to end-users.

Every method that returns a FriendlyError also accepts 0 to n Fields objects
may be provided and they will be combined into a single Fields, with later
values overwriting newer ones.

FriendlyErrors also have With* methods that return a copy of the error with
the given changes. This allows a pattern where a method defines a default
FriendlyError with the friendly error message to use and then when it needs to
return an error uses WithError() to add the specific error message.

*/
package errors

const DefaultFriendlyError = "An error occurred"

type Fields map[string]interface{}

type Friendly struct {
	msg         string
	friendlyMsg string
	secureMsg   string
	fields      Fields
}

//New creates a new errors.Friendly with the given msg.
func New(msg string, fields ...Fields) *Friendly {
	return &Friendly{
		msg:    msg,
		fields: combineFields(fields...),
	}
}

//NewFriendly returns a new errors.Friendly with the given FriendlyError.
func NewFriendly(friendlyMsg string, fields ...Fields) *Friendly {
	return &Friendly{
		friendlyMsg: friendlyMsg,
		fields:      combineFields(fields...),
	}
}

func combineFields(fields ...Fields) Fields {
	var result = make(Fields)
	for _, field := range fields {
		for key, val := range field {
			result[key] = val
		}
	}
	return result
}

//SecureError returns the error message that should only be shown in secure
//contexts, because it may include secret information. If no SecureError
//message has been provided, will return Error()
func (f *Friendly) SecureError() string {
	if f.secureMsg == "" {
		return f.Error()
	}
	return f.secureMsg
}

//Error returns the error message, implementing the error interface. If no
//specific message has been provided, will fall back on FriendlyError(). The
//Error() value is OK to show in insecure contexts (i.e. on the client) but it
//just might be confusing to users.
func (f *Friendly) Error() string {
	if f.msg == "" {
		return f.FriendlyError()
	}
	return f.msg
}

//FriendlyError is the error string that is OK to show in insecure contexts to
//end-users. It is generally a much simplified version of the message. If no
//specific FriendlyMessage has been provided, will return DefaultFriendlyError.
func (f *Friendly) FriendlyError() string {
	if f.friendlyMsg == "" {
		return DefaultFriendlyError
	}
	return f.friendlyMsg
}

//Fields returns the Fields object for this error.
func (f *Friendly) Fields() Fields {
	return f.fields
}

//Extend returns a new FriendlyError where the Error() message is prepended
//with this new message and a delimiter. The SecureError and FriendlyError
//message are left untouched.
func (f *Friendly) Extend(msg string, fields ...Fields) *Friendly {
	return &Friendly{
		secureMsg:   f.secureMsg,
		friendlyMsg: f.friendlyMsg,
		msg:         msg + " : " + f.Error(),
		fields:      combineFields(append([]Fields{f.fields}, fields...)...),
	}
}

//WithFriendly returns a copy of err where the friendlyMsg is set to friendlyMsg
func (f *Friendly) WithFriendly(err *Friendly, friendlyMsg string, fields ...Fields) *Friendly {
	if err == nil {
		return NewFriendly(friendlyMsg, fields...)
	}
	return &Friendly{
		secureMsg:   f.secureMsg,
		msg:         f.msg,
		friendlyMsg: friendlyMsg,
		fields:      combineFields(append([]Fields{f.fields}, fields...)...),
	}
}

//WithFriendly returns a copy of err where the Error() is set to msg. See a;so
//Extend, which prepends a new message to the front of the existing message.
func (f *Friendly) WithError(err *Friendly, msg string, fields ...Fields) *Friendly {
	if err == nil {
		return New(msg, fields...)
	}
	return &Friendly{
		secureMsg:   f.secureMsg,
		msg:         msg,
		friendlyMsg: f.friendlyMsg,
		fields:      combineFields(append([]Fields{f.fields}, fields...)...),
	}
}
<commit_msg>Updated the doc about what Fields represents and where they should be used. Part of #243.<commit_after>/*

errors is a package that implements FriendlyError. FriendlyError implements
the error interface, but also has a FriendlyError() method that returns a
message that is reasonable to show to end-users.

Every method that returns a FriendlyError also accepts 0 to n Fields objects
may be provided and they will be combined into a single Fields, with later
values overwriting newer ones.

FriendlyErrors also have With* methods that return a copy of the error with
the given changes. This allows a pattern where a method defines a default
FriendlyError with the friendly error message to use and then when it needs to
return an error uses WithError() to add the specific error message.

*/
package errors

const DefaultFriendlyError = "An error occurred"

type Fields map[string]interface{}

type Friendly struct {
	msg         string
	friendlyMsg string
	secureMsg   string
	fields      Fields
}

//New creates a new errors.Friendly with the given msg.
func New(msg string, fields ...Fields) *Friendly {
	return &Friendly{
		msg:    msg,
		fields: combineFields(fields...),
	}
}

//NewFriendly returns a new errors.Friendly with the given FriendlyError.
func NewFriendly(friendlyMsg string, fields ...Fields) *Friendly {
	return &Friendly{
		friendlyMsg: friendlyMsg,
		fields:      combineFields(fields...),
	}
}

func combineFields(fields ...Fields) Fields {
	var result = make(Fields)
	for _, field := range fields {
		for key, val := range field {
			result[key] = val
		}
	}
	return result
}

//SecureError returns the error message that should only be shown in secure
//contexts, because it may include secret information. If no SecureError
//message has been provided, will return Error()
func (f *Friendly) SecureError() string {
	if f.secureMsg == "" {
		return f.Error()
	}
	return f.secureMsg
}

//Error returns the error message, implementing the error interface. If no
//specific message has been provided, will fall back on FriendlyError(). The
//Error() value is OK to show in insecure contexts (i.e. on the client) but it
//just might be confusing to users.
func (f *Friendly) Error() string {
	if f.msg == "" {
		return f.FriendlyError()
	}
	return f.msg
}

//FriendlyError is the error string that is OK to show in insecure contexts to
//end-users. It is generally a much simplified version of the message. If no
//specific FriendlyMessage has been provided, will return DefaultFriendlyError.
func (f *Friendly) FriendlyError() string {
	if f.friendlyMsg == "" {
		return DefaultFriendlyError
	}
	return f.friendlyMsg
}

//Fields returns the Fields object for this error. Fields should only be used
//in secure contexts (e.g. logging on a server) because they may contain
//private information.
func (f *Friendly) Fields() Fields {
	return f.fields
}

//Extend returns a new FriendlyError where the Error() message is prepended
//with this new message and a delimiter. The SecureError and FriendlyError
//message are left untouched.
func (f *Friendly) Extend(msg string, fields ...Fields) *Friendly {
	return &Friendly{
		secureMsg:   f.secureMsg,
		friendlyMsg: f.friendlyMsg,
		msg:         msg + " : " + f.Error(),
		fields:      combineFields(append([]Fields{f.fields}, fields...)...),
	}
}

//WithFriendly returns a copy of err where the friendlyMsg is set to friendlyMsg
func (f *Friendly) WithFriendly(err *Friendly, friendlyMsg string, fields ...Fields) *Friendly {
	if err == nil {
		return NewFriendly(friendlyMsg, fields...)
	}
	return &Friendly{
		secureMsg:   f.secureMsg,
		msg:         f.msg,
		friendlyMsg: friendlyMsg,
		fields:      combineFields(append([]Fields{f.fields}, fields...)...),
	}
}

//WithFriendly returns a copy of err where the Error() is set to msg. See a;so
//Extend, which prepends a new message to the front of the existing message.
func (f *Friendly) WithError(err *Friendly, msg string, fields ...Fields) *Friendly {
	if err == nil {
		return New(msg, fields...)
	}
	return &Friendly{
		secureMsg:   f.secureMsg,
		msg:         msg,
		friendlyMsg: f.friendlyMsg,
		fields:      combineFields(append([]Fields{f.fields}, fields...)...),
	}
}
<|endoftext|>