<commit_before>package debug

import (
	"fmt"
	"os"
	"strings"
	"time"
)

// Names list of which name has which color
var names map[string]int = make(map[string]int)

// List of available colors
var colors = []int{6, 2, 3, 4, 5, 1}

// The default color
var defaultColor = 6

// Previous milliseconds
var prevTime int64 = 0

// The type that is returned in Debug function.
type printType func(format string, a ...interface{})

// Get string with terminal color.
func getColorString(firstColor int, lastColor int, message string) string {
	if !useColors() {
		return message
	}

	return fmt.Sprintf("\u001b[%d%dm%s\u001b[0m", firstColor, lastColor, message)
}

// Print debug message with namespace and message.
func printDebug(namespace string, format string, a ...interface{}) {
	color := names[namespace]
	namespace = getColorString(9, color, namespace)
	message := getColorString(9, 0, fmt.Sprintf(format, a...))

	if timeFormat, ok := useMS(); !ok {
		now := time.Now()

		if timeFormat == "utc" {
			now = time.Now().UTC()
		}

		fmt.Println(fmt.Sprintf("%s %s %s", now, namespace, message))
	} else {
		ms := getColorString(3, color, fmt.Sprintf("+%dms", getMs()))
		fmt.Println(fmt.Sprintf("%s %s %s", namespace, message, ms))
	}

	// No support for logging debug to file, feel free to contribute!
}

// Check if string is in slice.
func stringInSlice(a string, list []string) bool {
	for _, b := range list {
		if b == a {
			return true
		}
	}
	return false
}

// Check if namespace is allowed to print the debug message or not.
func checkNamespaceStatus(namespace string) bool {
	value := os.Getenv("DEBUG")

	if len(value) == 0 {
		return false
	}

	values := strings.Split(value, ",")

	if len(values) == 1 && values[0] == "*" {
		return true
	}

	if (stringInSlice(namespace, values)) {
		return true
	}

	star := false

	for _, name := range values {
		ns := strings.Split(namespace, ":")

		if name == "*" && !star {
			star = true
		}

		if strings.Contains(name, ns[0]) {
			if string(name[0]) == "-" && (string(name[1:]) == namespace || string(name[len(name)-1]) == "*") {
				return false
			} else if string(name[len(name)-1]) == "*" {
				parent := name[0 : len(name)-2]
				return strings.Split(parent, ":")[0] == parent
			} else {
				return false
			}
		}
	}

	return star
}

// Get milliseconds between debug messages.
func getMs() int64 {
	curr := time.Now().UnixNano() % 1e6 / 1e3
	ms := curr

	if prevTime == 0 {
		ms = 0
	} else {
		ms -= prevTime
	}

	prevTime = curr

	return ms
}

// Check if we should use colors or not.
func useColors() bool {
	value := os.Getenv("DEBUG_COLORS")
	return value != "0" && value != "no" && value != "false" && value != "disabled"
}

// Check if we should print milliseconds or time.
func useMS() (string, bool) {
	value := os.Getenv("DEBUG_TIME")

	if (len(value) == 0 || value == "ms") {
		return "ms", true
	}

	return value, false
}

// Create a new namespace to debug from.
func Debug(namespace string) printType {
	enabled := checkNamespaceStatus(namespace)

	if !enabled {
		return func(format string, a ...interface{}) {}
	}

	if _, ok := names[namespace]; !ok {
		color := defaultColor

		// We are out of colors, default to green.
		if len(colors) != 0 {
			color = colors[0]
			colors = colors[1:]
		}

		names[namespace] = color
	}

	return func(format string, a ...interface{}) {
		printDebug(namespace, format, a...)
	}
}
<commit_msg>Go fmt<commit_after>package debug

import (
	"fmt"
	"os"
	"strings"
	"time"
)

// Names list of which name has which color
var names map[string]int = make(map[string]int)

// List of available colors
var colors = []int{6, 2, 3, 4, 5, 1}

// The default color
var defaultColor = 6

// Previous milliseconds
var prevTime int64 = 0

// The type that is returned in Debug function.
type printType func(format string, a ...interface{})

// Get string with terminal color.
func getColorString(firstColor int, lastColor int, message string) string {
	if !useColors() {
		return message
	}

	return fmt.Sprintf("\u001b[%d%dm%s\u001b[0m", firstColor, lastColor, message)
}

// Print debug message with namespace and message.
func printDebug(namespace string, format string, a ...interface{}) {
	color := names[namespace]
	namespace = getColorString(9, color, namespace)
	message := getColorString(9, 0, fmt.Sprintf(format, a...))

	if timeFormat, ok := useMS(); !ok {
		now := time.Now()

		if timeFormat == "utc" {
			now = time.Now().UTC()
		}

		fmt.Println(fmt.Sprintf("%s %s %s", now, namespace, message))
	} else {
		ms := getColorString(3, color, fmt.Sprintf("+%dms", getMs()))
		fmt.Println(fmt.Sprintf("%s %s %s", namespace, message, ms))
	}

	// No support for logging debug to file, feel free to contribute!
}

// Check if string is in slice.
func stringInSlice(a string, list []string) bool {
	for _, b := range list {
		if b == a {
			return true
		}
	}
	return false
}

// Check if namespace is allowed to print the debug message or not.
func checkNamespaceStatus(namespace string) bool {
	value := os.Getenv("DEBUG")

	if len(value) == 0 {
		return false
	}

	values := strings.Split(value, ",")

	if len(values) == 1 && values[0] == "*" {
		return true
	}

	if stringInSlice(namespace, values) {
		return true
	}

	star := false

	for _, name := range values {
		ns := strings.Split(namespace, ":")

		if name == "*" && !star {
			star = true
		}

		if strings.Contains(name, ns[0]) {
			if string(name[0]) == "-" && (string(name[1:]) == namespace || string(name[len(name)-1]) == "*") {
				return false
			} else if string(name[len(name)-1]) == "*" {
				parent := name[0 : len(name)-2]
				return strings.Split(parent, ":")[0] == parent
			} else {
				return false
			}
		}
	}

	return star
}

// Get milliseconds between debug messages.
func getMs() int64 {
	curr := time.Now().UnixNano() % 1e6 / 1e3
	ms := curr

	if prevTime == 0 {
		ms = 0
	} else {
		ms -= prevTime
	}

	prevTime = curr

	return ms
}

// Check if we should use colors or not.
func useColors() bool {
	value := os.Getenv("DEBUG_COLORS")
	return value != "0" && value != "no" && value != "false" && value != "disabled"
}

// Check if we should print milliseconds or time.
func useMS() (string, bool) {
	value := os.Getenv("DEBUG_TIME")

	if len(value) == 0 || value == "ms" {
		return "ms", true
	}

	return value, false
}

// Create a new namespace to debug from.
func Debug(namespace string) printType {
	enabled := checkNamespaceStatus(namespace)

	if !enabled {
		return func(format string, a ...interface{}) {}
	}

	if _, ok := names[namespace]; !ok {
		color := defaultColor

		// We are out of colors, default to green.
		if len(colors) != 0 {
			color = colors[0]
			colors = colors[1:]
		}

		names[namespace] = color
	}

	return func(format string, a ...interface{}) {
		printDebug(namespace, format, a...)
	}
}
<|endoftext|>


<commit_before>/**
 * @file db.go
 * @author Mikhail Klementyev jollheef<AT>riseup.net
 * @license GNU AGPLv3
 * @date October, 2015
 * @brief work with database
 *
 * Contain functions for work with database.
 */

package db

import (
	"database/sql"

	_ "github.com/lib/pq" // import postgresql db engine
)

// All table names
var tables = [...]string{"category", "flag", "score", "session", "task", "team"}

// Create tables
func createSchema(db *sql.DB) (err error) {

	_, err = db.Exec("CREATE SCHEMA IF NOT EXISTS public")
	if err != nil {
		return
	}

	err = createCategoryTable(db)
	if err != nil {
		return
	}

	err = createFlagTable(db)
	if err != nil {
		return
	}

	err = createScoreTable(db)
	if err != nil {
		return
	}

	err = createSessionTable(db)
	if err != nil {
		return
	}

	err = createTaskTable(db)
	if err != nil {
		return
	}

	err = createTeamTable(db)
	if err != nil {
		return
	}

	return
}

// OpenDatabase need defer db.Close() after open
func OpenDatabase(path string) (db *sql.DB, err error) {

	db, err = sql.Open("postgres", path)
	if err != nil {
		return
	}

	err = createSchema(db)
	if err != nil {
		return
	}

	return
}

// Clean all values in table
func cleanTable(db *sql.DB, table string) (err error) {
	_, err = db.Exec("DELETE FROM " + table)
	return
}

// Restart id sequence in table
func restartSequence(db *sql.DB, table string) (err error) {
	_, err = db.Exec("ALTER SEQUENCE " + table + "_id_seq RESTART WITH 1;")
	return
}

// CleanDatabase clean all values and restart sequences in database without
// drop tables
func CleanDatabase(db *sql.DB) (err error) {

	for _, table := range tables {

		err = cleanTable(db, table)
		if err != nil {
			return
		}

		err = restartSequence(db, table)
		if err != nil {
			return
		}
	}

	return
}

// Drop public schema in current database
func dropSchema(db *sql.DB) (err error) {

	_, err = db.Exec("DROP SCHEMA public CASCADE")
	if err != nil {
		return
	}
	return
}

// InitDatabase recreate all database tables
func InitDatabase(path string) (db *sql.DB, err error) {

	db, err = OpenDatabase(path)
	if err != nil {
		return
	}

	dropSchema(db) // No error checking because no schema not good, but ok

	err = createSchema(db)
	if err != nil {
		return
	}

	return
}
<commit_msg>Refactor error handling<commit_after>/**
 * @file db.go
 * @author Mikhail Klementyev jollheef<AT>riseup.net
 * @license GNU AGPLv3
 * @date October, 2015
 * @brief work with database
 *
 * Contain functions for work with database.
 */

package db

import (
	"database/sql"

	_ "github.com/lib/pq" // import postgresql db engine
)

// All table names
var tables = [...]string{"category", "flag", "score", "session", "task", "team"}

// Create tables
func createSchema(db *sql.DB) error {

	_, err := db.Exec("CREATE SCHEMA IF NOT EXISTS public")
	if err != nil {
		return err
	}

	var errs []error

	errs = append(errs, createCategoryTable(db))
	errs = append(errs, createFlagTable(db))
	errs = append(errs, createScoreTable(db))
	errs = append(errs, createSessionTable(db))
	errs = append(errs, createTaskTable(db))
	errs = append(errs, createTeamTable(db))

	for _, e := range errs {
		if e != nil {
			return e
		}
	}

	return nil
}

// OpenDatabase need defer db.Close() after open
func OpenDatabase(path string) (db *sql.DB, err error) {

	db, err = sql.Open("postgres", path)
	if err != nil {
		return
	}

	err = createSchema(db)
	if err != nil {
		return
	}

	return
}

// Clean all values in table
func cleanTable(db *sql.DB, table string) (err error) {
	_, err = db.Exec("DELETE FROM " + table)
	return
}

// Restart id sequence in table
func restartSequence(db *sql.DB, table string) (err error) {
	_, err = db.Exec("ALTER SEQUENCE " + table + "_id_seq RESTART WITH 1;")
	return
}

// CleanDatabase clean all values and restart sequences in database without
// drop tables
func CleanDatabase(db *sql.DB) (err error) {

	for _, table := range tables {

		err = cleanTable(db, table)
		if err != nil {
			return
		}

		err = restartSequence(db, table)
		if err != nil {
			return
		}
	}

	return
}

// Drop public schema in current database
func dropSchema(db *sql.DB) (err error) {

	_, err = db.Exec("DROP SCHEMA public CASCADE")
	if err != nil {
		return
	}
	return
}

// InitDatabase recreate all database tables
func InitDatabase(path string) (db *sql.DB, err error) {

	db, err = OpenDatabase(path)
	if err != nil {
		return
	}

	dropSchema(db) // No error checking because no schema not good, but ok

	err = createSchema(db)
	if err != nil {
		return
	}

	return
}
<|endoftext|>


<commit_before>package db

import "gopkg.in/mgo.v2"

// Mongo contains the mongo connection info
type Mongo struct {
	URL            string
	Database       string
	CollectionName string
}

// Collection returns the goreportcard mongo collection
func (db *Mongo) Collection() (*mgo.Collection, error) {
	session, err := mgo.Dial(db.URL)
	if err != nil {
		return nil, err
	}
	c := session.DB(db.Database).C(db.CollectionName)

	return c, nil
}
<commit_msg>remove db/db.go<commit_after><|endoftext|>


<commit_before>package db

import (
	"fmt"
	"io/ioutil"
	"log"
	"math/rand"
	"os"
	"strconv"
	"strings"
	"time"
)

const (
	TRADB      = ".trago.db"
	bytes      = "abcdefghijklmnopqrstuvwxyz1234567890"
	currentDir = "./"
)

type TraDb struct {
	ReplicaId  string
	VersionVec map[string]int
	Files      map[string]FileState
}

type FileState struct {
	Size    int
	MTime   int64
	Version int
	Replica string
	// TODO: use a hash as well
}

func Parse(data string) (TraDb, error) {
	tradb := TraDb{}
	versionVector := make(map[string]int)

	tradb.Files = make(map[string]FileState)

	for _, line := range strings.Split(data, "\n") {
		line = strings.TrimSpace(line)

		if strings.HasPrefix(line, "#") {
			continue
		}

		fields := strings.Fields(line)
		if len(fields) == 0 {
			continue
		}

		switch fields[0] {
		case "file": // file name size mtime replica:version
			if len(fields) != 5 {
				continue
			}

			size, err := strconv.Atoi(fields[2])
			checkError(err)
			mtime, err := strconv.ParseInt(fields[3], 10, 64)
			checkError(err)

			pair := strings.Split(fields[4], ":")
			replicaId := pair[0]
			ver, err := strconv.Atoi(pair[1])
			checkError(err)

			tradb.Files[fields[1]] = FileState{size, mtime, ver, replicaId}
		case "version": // version r1:v1 r2:v2 ...
			for _, entry := range fields[1:] {
				pair := strings.Split(entry, ":") // replica:version pair

				v, err := strconv.Atoi(pair[1])
				checkError(err)

				versionVector[pair[0]] = v
			}
			tradb.VersionVec = versionVector

		case "replica": // replica replica-id
			if len(fields) != 2 {
				continue
			}
			tradb.ReplicaId = fields[1]
		}
	}

	return tradb, nil
}

func ParseFile() (TraDb, error) {
	tradb := TraDb{}

	dbfile, err := os.Open(TRADB)
	if os.IsNotExist(err) {
		log.Println("didn't find .trago.db")
		tradb = *New()
		tradb.Write()

		return tradb, nil
	} else if err != nil {
		return tradb, err
	}

	defer dbfile.Close()

	bs, err := ioutil.ReadFile(TRADB)
	if err != nil {
		return TraDb{}, err
	}

	return Parse(string(bs))
}

func New() *TraDb {
	replicaId := make([]byte, 16)
	versionVector := make(map[string]int)

	rand.Seed(time.Now().UTC().UnixNano())
	for i, _ := range replicaId {
		replicaId[i] = bytes[rand.Intn(len(bytes))]
	}
	versionVector[string(replicaId)] = 1

	files, err := ioutil.ReadDir(currentDir)
	checkError(err)

	filemap := make(map[string]FileState)
	for _, file := range files {
		if file.IsDir() {
			continue // ignore directories for now
		}
		fs := FileState{
			Size:    int(file.Size()),
			MTime:   file.ModTime().UnixNano(),
			Version: 1,
			Replica: string(replicaId),
		}
		filemap[file.Name()] = fs
	}

	return &TraDb{string(replicaId), versionVector, filemap}
}

func (tradb *TraDb) Write() {
	var pairs []string

	for replicaId, version := range tradb.VersionVec {
		entry := strings.Join([]string{replicaId, strconv.Itoa(version)}, ":")
		pairs = append(pairs, entry)
	}

	versionVector := strings.Join(pairs, " ")

	preamble := fmt.Sprintf(
		"replica %s\nversion %s\n# files\n",
		tradb.ReplicaId,
		versionVector,
	)

	fileEntries := make([]string, len(tradb.Files))

	i := 0
	for filename, info := range tradb.Files {
		fileEntries[i] = fmt.Sprintf(
			"file %s %d %d %s:%d",
			filename,
			info.Size,
			info.MTime,
			info.Replica,
			info.Version,
		)
		i = i + 1
	}

	entryString := strings.Join(fileEntries, "\n")
	dataToWrite := []byte(preamble + entryString)

	err := ioutil.WriteFile(TRADB, dataToWrite, 0644)
	checkError(err)
}

func (db *TraDb) Update() {
	files, err := ioutil.ReadDir(currentDir)
	checkError(err)

	for _, file := range files {
		if file.IsDir() {
			continue
		}

		filename := file.Name()
		dbRecord := db.Files[filename]
		if dbRecord.MTime == 0 {
			log.Printf("found a new file: %s\n", filename)
		} else if dbRecord.MTime < file.ModTime().UnixNano() {
			log.Printf("found an updated file: %s\n", filename)
			dbRecord.MTime = file.ModTime().UnixNano()
			dbRecord.Version = db.VersionVec[db.ReplicaId]
		} else {
			log.Printf("file unchanged: %s\n", filename)
		}
	}
}

func (local *TraDb) Compare(remote *TraDb) {
	remoteFiles := remote.Files

	for file, state := range local.Files {
		remoteState := remoteFiles[file]

		if remoteState.Version == 0 { // file not present on server
			// TODO: download only if we have a more "recent" copy
			continue
		}

		if isFileChanged(state, remoteState) {
			if local.VersionVec[remoteState.Replica] >= remoteState.Version {
				continue // we already know about changes on remote
			} else if remote.VersionVec[state.Replica] >= state.Version {
				fmt.Printf("downloading: %s\n", file)
				continue
			} else {
				fmt.Printf("conflict: %s\n", file)
			}
		}
	}
}

func isFileChanged(fs1 FileState, fs2 FileState) bool {
	if fs1.MTime != fs2.MTime || fs1.Size != fs2.Size {
		return false
	}
	return true
}

func checkError(err error) {
	if err != nil {
		log.Fatal(err)
	}
}
<commit_msg>increment version on update<commit_after>package db

import (
	"fmt"
	"io/ioutil"
	"log"
	"math/rand"
	"os"
	"strconv"
	"strings"
	"time"
)

const (
	TRADB      = ".trago.db"
	bytes      = "abcdefghijklmnopqrstuvwxyz1234567890"
	currentDir = "./"
)

type TraDb struct {
	ReplicaId  string
	VersionVec map[string]int
	Files      map[string]FileState
}

type FileState struct {
	Size    int
	MTime   int64
	Version int
	Replica string
	// TODO: use a hash as well
}

func Parse(data string) (TraDb, error) {
	tradb := TraDb{}
	versionVector := make(map[string]int)

	tradb.Files = make(map[string]FileState)

	for _, line := range strings.Split(data, "\n") {
		line = strings.TrimSpace(line)

		if strings.HasPrefix(line, "#") {
			continue
		}

		fields := strings.Fields(line)
		if len(fields) == 0 {
			continue
		}

		switch fields[0] {
		case "file": // file name size mtime replica:version
			if len(fields) != 5 {
				continue
			}

			size, err := strconv.Atoi(fields[2])
			checkError(err)
			mtime, err := strconv.ParseInt(fields[3], 10, 64)
			checkError(err)

			pair := strings.Split(fields[4], ":")
			replicaId := pair[0]
			ver, err := strconv.Atoi(pair[1])
			checkError(err)

			tradb.Files[fields[1]] = FileState{size, mtime, ver, replicaId}
		case "version": // version r1:v1 r2:v2 ...
			for _, entry := range fields[1:] {
				pair := strings.Split(entry, ":") // replica:version pair

				v, err := strconv.Atoi(pair[1])
				checkError(err)

				versionVector[pair[0]] = v
			}
			tradb.VersionVec = versionVector

		case "replica": // replica replica-id
			if len(fields) != 2 {
				continue
			}
			tradb.ReplicaId = fields[1]
		}
	}

	return tradb, nil
}

func ParseFile() (TraDb, error) {
	tradb := TraDb{}

	dbfile, err := os.Open(TRADB)
	if os.IsNotExist(err) {
		log.Println("didn't find .trago.db")
		tradb = *New()
		tradb.Write()

		return tradb, nil
	} else if err != nil {
		return tradb, err
	}

	defer dbfile.Close()

	bs, err := ioutil.ReadFile(TRADB)
	if err != nil {
		return TraDb{}, err
	}

	return Parse(string(bs))
}

func New() *TraDb {
	replicaId := make([]byte, 16)
	versionVector := make(map[string]int)

	rand.Seed(time.Now().UTC().UnixNano())
	for i, _ := range replicaId {
		replicaId[i] = bytes[rand.Intn(len(bytes))]
	}
	versionVector[string(replicaId)] = 1

	files, err := ioutil.ReadDir(currentDir)
	checkError(err)

	filemap := make(map[string]FileState)
	for _, file := range files {
		if file.IsDir() {
			continue // ignore directories for now
		}
		fs := FileState{
			Size:    int(file.Size()),
			MTime:   file.ModTime().UnixNano(),
			Version: 1,
			Replica: string(replicaId),
		}
		filemap[file.Name()] = fs
	}

	return &TraDb{string(replicaId), versionVector, filemap}
}

func (tradb *TraDb) Write() {
	var pairs []string

	for replicaId, version := range tradb.VersionVec {
		entry := strings.Join([]string{replicaId, strconv.Itoa(version)}, ":")
		pairs = append(pairs, entry)
	}

	versionVector := strings.Join(pairs, " ")

	preamble := fmt.Sprintf(
		"replica %s\nversion %s\n# files\n",
		tradb.ReplicaId,
		versionVector,
	)

	fileEntries := make([]string, len(tradb.Files))

	i := 0
	for filename, info := range tradb.Files {
		fileEntries[i] = fmt.Sprintf(
			"file %s %d %d %s:%d",
			filename,
			info.Size,
			info.MTime,
			info.Replica,
			info.Version,
		)
		i = i + 1
	}

	entryString := strings.Join(fileEntries, "\n")
	dataToWrite := []byte(preamble + entryString)

	err := ioutil.WriteFile(TRADB, dataToWrite, 0644)
	checkError(err)
}

func (db *TraDb) Update() {
	files, err := ioutil.ReadDir(currentDir)
	checkError(err)

	for _, file := range files {
		if file.IsDir() {
			continue
		}

		filename := file.Name()
		dbRecord := db.Files[filename]
		if dbRecord.MTime == 0 {
			log.Printf("found a new file: %s\n", filename)
		} else if dbRecord.MTime < file.ModTime().UnixNano() {
			log.Printf("found an updated file: %s\n", filename)
			dbRecord.MTime = file.ModTime().UnixNano()
			dbRecord.Version = db.VersionVec[db.ReplicaId]
		} else {
			log.Printf("file unchanged: %s\n", filename)
		}
	}

	db.VersionVec[db.ReplicaId] += 1
}

func (local *TraDb) Compare(remote *TraDb) {
	remoteFiles := remote.Files

	for file, state := range local.Files {
		remoteState := remoteFiles[file]

		if remoteState.Version == 0 { // file not present on server
			// TODO: download only if we have a more "recent" copy
			continue
		}

		if isFileChanged(state, remoteState) {
			if local.VersionVec[remoteState.Replica] >= remoteState.Version {
				continue // we already know about changes on remote
			} else if remote.VersionVec[state.Replica] >= state.Version {
				fmt.Printf("downloading: %s\n", file)
				continue
			} else {
				fmt.Printf("conflict: %s\n", file)
			}
		}
	}
}

func isFileChanged(fs1 FileState, fs2 FileState) bool {
	if fs1.MTime != fs2.MTime || fs1.Size != fs2.Size {
		return false
	}
	return true
}

func checkError(err error) {
	if err != nil {
		log.Fatal(err)
	}
}
<|endoftext|>


<commit_before>package db

import (
	"fmt"
	"strconv"
	"time"

	xormcore "github.com/go-xorm/core"
	"github.com/go-xorm/xorm"
	ccc "github.com/heqzha/goutils/concurrency"
	_ "github.com/lib/pq"
)

type PGEngine struct {
	*xorm.Engine
	dsn string
}

type PGMasterEngine struct {
	*PGEngine
}

func (pg *PGMasterEngine) Init(user, password, dbName, host, port string, ssl bool, level xormcore.LogLevel, showSQL bool) error {
	dsn := fmt.Sprintf(
		"user=%s dbname=%s host=%s port=%s",
		user,
		dbName,
		host,
		port,
	)
	if password != "" {
		dsn += fmt.Sprintf(
			" password=%s",
			password,
		)
	}
	if ssl {
		dsn += " sslmode=enable"
	} else {
		dsn += " sslmode=disable"
	}
	pg = &PGMasterEngine{}
	engine, err := xorm.NewEngine("postgres", dsn)
	if err != nil {
		return err
	}
	pg.Engine = engine
	pg.dsn = dsn
	pg.SetMaxOpenConns(100)
	pg.SetMaxIdleConns(50)
	pg.Logger().SetLevel(level)
	pg.ShowSQL(showSQL)

	if err := pg.Ping(); err != nil {
		return err
	}
	return nil
}

func (pg *PGMasterEngine) SyncModels(models ...interface{}) error {
	return pg.Sync2(models...)
}

func (pg *PGMasterEngine) EnableEngineStatusChecker(t time.Duration) {
	ccc.TaskRunPeriodic(func() time.Duration {
		if err := pg.Ping(); err != nil {
			fmt.Println("Cannot connect to %s: %s", pg.dsn, err.Error())
		}
		return t
	}, "PGRunCheckMasterStatus", time.Second)
}

func (pg *PGMasterEngine) GetAutoCloseSession() *xorm.Session {
	s := pg.NewSession()
	s.IsAutoClose = true
	return s
}

func (pg *PGMasterEngine) NewSequence(sequence string) (int64, error) {
	results, err := pg.Query("select nextval(?) as next", sequence)
	if err != nil {
		return 0, fmt.Errorf("Generate sequence %s error: %s", sequence, err.Error())
	} else if results == nil || len(results) == 0 {
		return 0, fmt.Errorf("Generate sequence %s error: results is empty", sequence)
	}

	result, err := strconv.ParseInt(string(results[0]["next"]), 10, 64)
	if err != nil {
		return 0, fmt.Errorf("Generate sequence %s error: %s", sequence, err.Error())
	}

	return result, nil
}
<commit_msg>Fixed pg bug<commit_after>package db

import (
	"fmt"
	"strconv"
	"time"

	xormcore "github.com/go-xorm/core"
	"github.com/go-xorm/xorm"
	ccc "github.com/heqzha/goutils/concurrency"
	_ "github.com/lib/pq"
)

type PGMasterEngine struct {
	*xorm.Engine
	dsn string
}

func (pg *PGMasterEngine) Init(user, password, dbName, host, port string, ssl bool, level xormcore.LogLevel, showSQL bool) error {
	dsn := fmt.Sprintf(
		"user=%s dbname=%s host=%s port=%s",
		user,
		dbName,
		host,
		port,
	)
	if password != "" {
		dsn += fmt.Sprintf(
			" password=%s",
			password,
		)
	}
	if ssl {
		dsn += " sslmode=enable"
	} else {
		dsn += " sslmode=disable"
	}
	engine, err := xorm.NewEngine("postgres", dsn)
	if err != nil {
		return err
	}
	pg.Engine = engine
	pg.dsn = dsn
	pg.SetMaxOpenConns(100)
	pg.SetMaxIdleConns(50)
	pg.Logger().SetLevel(level)
	pg.ShowSQL(showSQL)

	if err := pg.Ping(); err != nil {
		return err
	}
	return nil
}

func (pg *PGMasterEngine) SyncModels(models ...interface{}) error {
	return pg.Sync2(models...)
}

func (pg *PGMasterEngine) EnableEngineStatusChecker(t time.Duration) {
	ccc.TaskRunPeriodic(func() time.Duration {
		if err := pg.Ping(); err != nil {
			fmt.Println("Cannot connect to %s: %s", pg.dsn, err.Error())
		}
		return t
	}, "PGRunCheckMasterStatus", time.Second)
}

func (pg *PGMasterEngine) GetAutoCloseSession() *xorm.Session {
	s := pg.NewSession()
	s.IsAutoClose = true
	return s
}

func (pg *PGMasterEngine) NewSequence(sequence string) (int64, error) {
	results, err := pg.Query("select nextval(?) as next", sequence)
	if err != nil {
		return 0, fmt.Errorf("Generate sequence %s error: %s", sequence, err.Error())
	} else if results == nil || len(results) == 0 {
		return 0, fmt.Errorf("Generate sequence %s error: results is empty", sequence)
	}

	result, err := strconv.ParseInt(string(results[0]["next"]), 10, 64)
	if err != nil {
		return 0, fmt.Errorf("Generate sequence %s error: %s", sequence, err.Error())
	}

	return result, nil
}
<|endoftext|>


<commit_before>package raygun4go

import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"net/url"
	"testing"

	"github.com/pborman/uuid"

	. "github.com/smartystreets/goconvey/convey"
)

func TestClient(t *testing.T) {
	Convey("Client", t, func() {
		c, _ := New("app", "key")
		So(c.appName, ShouldEqual, "app")
		So(c.apiKey, ShouldEqual, "key")
		So(c.context.Request, ShouldBeNil)
		So(c.context.Identifier(), ShouldHaveSameTypeAs, uuid.New())

		Convey("#New", func() {
			c, err := New("", "test")
			So(c, ShouldEqual, nil)
			So(err, ShouldNotEqual, nil)

			c, err = New("test", "")
			So(c, ShouldEqual, nil)
			So(err, ShouldNotEqual, nil)

			c, err = New("test", "test")
			So(c, ShouldHaveSameTypeAs, &Client{})
			So(err, ShouldEqual, nil)
		})

		Convey("#Clone", func() {
			t := []string{"Critical", "Urgent", "Fix it now!"}
			c.Tags(t)

			cd := "foo"
			c.CustomData(cd)

			r := &http.Request{}
			c.Request(r)

			v := "1.2.3"
			c.Version(v)

			u := "user"
			c.User(u)

			clone := c.Clone()

			So(clone.appName, ShouldResemble, c.appName)
			So(clone.apiKey, ShouldResemble, c.apiKey)
			So(clone.silent, ShouldResemble, c.silent)
			So(clone.logToStdOutlogToStdOut, ShouldResemble, c.logToStdOut)
			So(clone.context.Request, ShouldResemble, c.context.Request)
			So(clone.context.Version, ShouldResemble, c.context.Version)
			So(clone.context.Tags, ShouldResemble, c.context.Tags)
			So(clone.context.CustomData, ShouldResemble, c.context.CustomData)
			So(clone.context.User, ShouldResemble, c.context.User)
			So(clone.context.identifier, ShouldResemble, c.context.identifier)
		})

		Convey("#Request", func() {
			r := &http.Request{}
			c.Request(r)
			So(c.context.Request, ShouldResemble, r)
		})

		Convey("#Version", func() {
			v := "version"
			c.Version(v)
			So(c.context.Version, ShouldResemble, v)
		})

		Convey("#Tags", func() {
			t := []string{"foo", "bar"}
			c.Tags(t)
			So(c.context.Tags, ShouldResemble, t)
		})

		Convey("#CustomData", func() {
			cd := "foo"
			c.CustomData(cd)
			So(c.context.CustomData, ShouldResemble, cd)
		})

		Convey("#User", func() {
			u := "user"
			c.User(u)
			So(c.context.User, ShouldResemble, u)
		})

		Convey("#Silent", func() {
			So(c.silent, ShouldBeFalse)
			c.Silent(true)
			So(c.silent, ShouldBeTrue)
		})

		Convey("#LogToStdOut", func() {
			So(c.logToStdOut, ShouldBeFalse)
			c.LogToStdOut(true)
			So(c.logToStdOut, ShouldBeTrue)
		})

		Convey("#Asynchronous", func() {
			So(c.asynchronous, ShouldBeFalse)
			c.Asynchronous(true)
			So(c.asynchronous, ShouldBeTrue)
		})

		Convey("#HandleError", func() {
			u := "http://www.example.com?foo=bar&fizz[]=buzz&fizz[]=buzz2"
			r, _ := http.NewRequest("GET", u, nil)
			r.RemoteAddr = "1.2.3.4"
			r.PostForm = url.Values{
				"foo":  []string{"bar"},
				"fizz": []string{"buzz", "buzz2"},
			}
			r.Header.Add("Cookie", "cookie1=value1; cookie2=value2")
			c.Silent(true)
			c.Request(r)
			c.apiKey = "key"
			c.context.Version = "goconvey"
			c.context.Tags = []string{"golang", "test"}
			c.context.CustomData = map[string]string{"foo": "bar"}
			c.context.User = "Test User"
			defer c.HandleError()
			panic("Test: See if this works with Raygun")
		})

		Convey("#CreateError", func() {
			ts := raygunEndpointStub()
			defer ts.Close()
			raygunEndpoint = ts.URL
			c, _ := New("app", "key")
			c.Silent(false)
			c.apiKey = "key"
			c.CreateError("Test: See if this works with Raygun")
		})
	})
}

func raygunEndpointStub() *httptest.Server {
	return httptest.NewServer(
		http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
			w.Header().Set("Content-Type", "application/json")

			if req.Method != "POST" || req.RequestURI != "/entries" {
				fmt.Println("raygunEndpointStub: URI not implemented")
				http.Error(w, "Not Found", http.StatusNotFound)
				return
			}

			// 403 Invalid API Key
			// The value specified in the header X-ApiKey did not match with a user.
			if req.Header.Get("X-ApiKey") == "" {
				w.WriteHeader(http.StatusForbidden)
				return
			}
			// 202 OK - Message accepted.
			w.WriteHeader(http.StatusAccepted)
		}))
}
<commit_msg>Unit test fix<commit_after>package raygun4go

import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"net/url"
	"testing"

	"github.com/pborman/uuid"

	. "github.com/smartystreets/goconvey/convey"
)

func TestClient(t *testing.T) {
	Convey("Client", t, func() {
		c, _ := New("app", "key")
		So(c.appName, ShouldEqual, "app")
		So(c.apiKey, ShouldEqual, "key")
		So(c.context.Request, ShouldBeNil)
		So(c.context.Identifier(), ShouldHaveSameTypeAs, uuid.New())

		Convey("#New", func() {
			c, err := New("", "test")
			So(c, ShouldEqual, nil)
			So(err, ShouldNotEqual, nil)

			c, err = New("test", "")
			So(c, ShouldEqual, nil)
			So(err, ShouldNotEqual, nil)

			c, err = New("test", "test")
			So(c, ShouldHaveSameTypeAs, &Client{})
			So(err, ShouldEqual, nil)
		})

		Convey("#Clone", func() {
			t := []string{"Critical", "Urgent", "Fix it now!"}
			c.Tags(t)

			cd := "foo"
			c.CustomData(cd)

			r := &http.Request{}
			c.Request(r)

			v := "1.2.3"
			c.Version(v)

			u := "user"
			c.User(u)

			clone := c.Clone()

			So(clone.appName, ShouldResemble, c.appName)
			So(clone.apiKey, ShouldResemble, c.apiKey)
			So(clone.silent, ShouldResemble, c.silent)
			So(clone.logToStdOut, ShouldResemble, c.logToStdOut)
			So(clone.context.Request, ShouldResemble, c.context.Request)
			So(clone.context.Version, ShouldResemble, c.context.Version)
			So(clone.context.Tags, ShouldResemble, c.context.Tags)
			So(clone.context.CustomData, ShouldResemble, c.context.CustomData)
			So(clone.context.User, ShouldResemble, c.context.User)
			So(clone.context.identifier, ShouldResemble, c.context.identifier)
		})

		Convey("#Request", func() {
			r := &http.Request{}
			c.Request(r)
			So(c.context.Request, ShouldResemble, r)
		})

		Convey("#Version", func() {
			v := "version"
			c.Version(v)
			So(c.context.Version, ShouldResemble, v)
		})

		Convey("#Tags", func() {
			t := []string{"foo", "bar"}
			c.Tags(t)
			So(c.context.Tags, ShouldResemble, t)
		})

		Convey("#CustomData", func() {
			cd := "foo"
			c.CustomData(cd)
			So(c.context.CustomData, ShouldResemble, cd)
		})

		Convey("#User", func() {
			u := "user"
			c.User(u)
			So(c.context.User, ShouldResemble, u)
		})

		Convey("#Silent", func() {
			So(c.silent, ShouldBeFalse)
			c.Silent(true)
			So(c.silent, ShouldBeTrue)
		})

		Convey("#LogToStdOut", func() {
			So(c.logToStdOut, ShouldBeFalse)
			c.LogToStdOut(true)
			So(c.logToStdOut, ShouldBeTrue)
		})

		Convey("#Asynchronous", func() {
			So(c.asynchronous, ShouldBeFalse)
			c.Asynchronous(true)
			So(c.asynchronous, ShouldBeTrue)
		})

		Convey("#HandleError", func() {
			u := "http://www.example.com?foo=bar&fizz[]=buzz&fizz[]=buzz2"
			r, _ := http.NewRequest("GET", u, nil)
			r.RemoteAddr = "1.2.3.4"
			r.PostForm = url.Values{
				"foo":  []string{"bar"},
				"fizz": []string{"buzz", "buzz2"},
			}
			r.Header.Add("Cookie", "cookie1=value1; cookie2=value2")
			c.Silent(true)
			c.Request(r)
			c.apiKey = "key"
			c.context.Version = "goconvey"
			c.context.Tags = []string{"golang", "test"}
			c.context.CustomData = map[string]string{"foo": "bar"}
			c.context.User = "Test User"
			defer c.HandleError()
			panic("Test: See if this works with Raygun")
		})

		Convey("#CreateError", func() {
			ts := raygunEndpointStub()
			defer ts.Close()
			raygunEndpoint = ts.URL
			c, _ := New("app", "key")
			c.Silent(false)
			c.apiKey = "key"
			c.CreateError("Test: See if this works with Raygun")
		})
	})
}

func raygunEndpointStub() *httptest.Server {
	return httptest.NewServer(
		http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
			w.Header().Set("Content-Type", "application/json")

			if req.Method != "POST" || req.RequestURI != "/entries" {
				fmt.Println("raygunEndpointStub: URI not implemented")
				http.Error(w, "Not Found", http.StatusNotFound)
				return
			}

			// 403 Invalid API Key
			// The value specified in the header X-ApiKey did not match with a user.
			if req.Header.Get("X-ApiKey") == "" {
				w.WriteHeader(http.StatusForbidden)
				return
			}
			// 202 OK - Message accepted.
			w.WriteHeader(http.StatusAccepted)
		}))
}
<|endoftext|>


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

package matrix

import (
	"math/rand"
)

/*
A matrix backed by a flat array of all elements.
*/
type DenseMatrix struct {
	matrix
	// flattened matrix data. elements[i*step+j] is row i, col j
	elements []float64
	// actual offset between rows
	step int
}

/*
Returns an array of slices referencing the matrix data. Changes to
the slices effect changes to the matrix.
*/
func (A *DenseMatrix) Arrays() [][]float64 {
	a := make([][]float64, A.rows)
	for i := 0; i < A.rows; i++ {
		a[i] = A.elements[i*A.step : i*A.step+A.cols]
	}
	return a
}

/*
Returns the contents of this matrix stored into a flat array (row-major).
*/
func (A *DenseMatrix) Array() []float64 {
	if A.step == A.rows {
		return A.elements[0 : A.rows*A.cols]
	}
	a := make([]float64, A.rows*A.cols)
	for i := 0; i < A.rows; i++ {
		for j := 0; j < A.cols; j++ {
			a[i*A.cols+j] = A.elements[i*A.step+j]
		}
	}
	return a
}

func (A *DenseMatrix) rowSlice(row int) []float64 {
	return A.elements[row*A.step : row*A.step+A.cols]
}

/*
Get the element in the ith row and jth column.
*/
func (A *DenseMatrix) Get(i int, j int) (v float64) {
	/*
		i = i % A.rows
		if i < 0 {
			i = A.rows - i
		}
		j = j % A.cols
		if j < 0 {
			j = A.cols - j
		}
	*/

	// reslicing like this does efficient range checks, perhaps
	v = A.elements[i*A.step : i*A.step+A.cols][j]
	//v = A.elements[i*A.step+j]
	return
}

/*
Set the element in the ith row and jth column to v.
*/
func (A *DenseMatrix) Set(i int, j int, v float64) {
	/*
		i = i % A.rows
		if i < 0 {
			i = A.rows - i
		}
		j = j % A.cols
		if j < 0 {
			j = A.cols - j
		}
	*/
	// reslicing like this does efficient range checks, perhaps
	A.elements[i*A.step : i*A.step+A.cols][j] = v
	//A.elements[i*A.step+j] = v
}

/*
Get a submatrix starting at i,j with rows rows and cols columns. Changes to
the returned matrix show up in the original.
*/
func (A *DenseMatrix) GetMatrix(i, j, rows, cols int) *DenseMatrix {
	B := new(DenseMatrix)
	B.elements = A.elements[i*A.step+j : i*A.step+j+(rows-1)*A.step+cols]
	B.rows = rows
	B.cols = cols
	B.step = A.step
	return B
}

/*
Copy B into A, with B's 0, 0 aligning with A's i, j
*/
func (A *DenseMatrix) SetMatrix(i, j int, B *DenseMatrix) {
	for r := 0; r < B.rows; r++ {
		for c := 0; c < B.cols; c++ {
			A.Set(i+r, j+c, B.Get(r, c))
		}
	}
}

func (A *DenseMatrix) GetColVector(j int) *DenseMatrix {
	return A.GetMatrix(0, j, A.rows, 1)
}

func (A *DenseMatrix) GetRowVector(i int) *DenseMatrix {
	return A.GetMatrix(i, 0, 1, A.cols)
}

/*
Get a copy of this matrix with 0s above the diagonal.
*/
func (A *DenseMatrix) L() *DenseMatrix {
	B := A.Copy()
	for i := 0; i < A.rows; i++ {
		for j := i + 1; j < A.cols; j++ {
			B.Set(i, j, 0)
		}
	}
	return B
}

/*
Get a copy of this matrix with 0s below the diagonal.
*/
func (A *DenseMatrix) U() *DenseMatrix {
	B := A.Copy()
	for i := 0; i < A.rows; i++ {
		for j := 0; j < i && j < A.cols; j++ {
			B.Set(i, j, 0)
		}
	}
	return B
}

func (A *DenseMatrix) Copy() *DenseMatrix {
	B := new(DenseMatrix)
	B.rows = A.rows
	B.cols = A.cols
	B.step = A.cols
	B.elements = make([]float64, B.rows*B.cols)
	for row := 0; row < B.rows; row++ {
		copy(B.rowSlice(row), A.rowSlice(row))
	}
	return B
}

/*
Get a new matrix [A B].
*/
func (A *DenseMatrix) Augment(B *DenseMatrix) (C *DenseMatrix, err error) {
	if A.rows != B.rows {
		err = ErrorDimensionMismatch
		return
	}
	C = Zeros(A.rows, A.cols+B.cols)
	err = A.AugmentFill(B, C)
	return
}
func (A *DenseMatrix) AugmentFill(B, C *DenseMatrix) (err error) {
	if A.rows != B.rows || C.rows != A.rows || C.cols != A.cols+B.cols {
		err = ErrorDimensionMismatch
		return
	}
	C.SetMatrix(0, 0, A)
	C.SetMatrix(0, A.cols, B)
	/*
		for i := 0; i < C.Rows(); i++ {
			for j := 0; j < A.Cols(); j++ {
				C.Set(i, j, A.Get(i, j))
			}
			for j := 0; j < B.Cols(); j++ {
				C.Set(i, j+A.Cols(), B.Get(i, j))
			}
		}*/
	return
}

/*
Get a new matrix [A; B], with A above B.
*/
func (A *DenseMatrix) Stack(B *DenseMatrix) (C *DenseMatrix, err error) {
	if A.cols != B.cols {
		err = ErrorDimensionMismatch
		return
	}
	C = Zeros(A.rows+B.rows, A.cols)
	err = A.StackFill(B, C)
	return
}
func (A *DenseMatrix) StackFill(B, C *DenseMatrix) (err error) {
	if A.cols != B.cols || C.cols != A.cols || C.rows != A.rows+B.rows {
		err = ErrorDimensionMismatch
		return
	}
	C.SetMatrix(0, 0, A)
	C.SetMatrix(A.rows, 0, B)
	/*
		for j := 0; j < A.cols; j++ {
			for i := 0; i < A.Rows(); i++ {
				C.Set(i, j, A.Get(i, j))
			}
			for i := 0; i < B.cols; i++ {
				C.Set(i+A.rows, j, B.Get(i, j))
			}
		}
	*/
	return
}

/*
Create a sparse matrix copy.
*/
func (A *DenseMatrix) SparseMatrix() *SparseMatrix {
	B := ZerosSparse(A.rows, A.cols)
	for i := 0; i < A.rows; i++ {
		for j := 0; j < A.cols; j++ {
			v := A.Get(i, j)
			if v != 0 {
				B.Set(i, j, v)
			}
		}
	}
	return B
}

func (A *DenseMatrix) DenseMatrix() *DenseMatrix {
	return A.Copy()
}

func Zeros(rows, cols int) *DenseMatrix {
	A := new(DenseMatrix)
	A.elements = make([]float64, rows*cols)
	A.rows = rows
	A.cols = cols
	A.step = cols
	return A
}

func Ones(rows, cols int) *DenseMatrix {
	A := new(DenseMatrix)
	A.elements = make([]float64, rows*cols)
	A.rows = rows
	A.cols = cols
	A.step = cols

	for i := 0; i < len(A.elements); i++ {
		A.elements[i] = 1
	}

	return A
}

func Numbers(rows, cols int, num float64) *DenseMatrix {
	A := Zeros(rows, cols)

	for i := 0; i < A.Rows(); i++ {
		for j := 0; j < A.Cols(); j++ {
			A.Set(i, j, num)
		}
	}

	return A
}

/*
Create an identity matrix with span rows and span columns.
*/
func Eye(span int) *DenseMatrix {
	A := Zeros(span, span)
	for i := 0; i < span; i++ {
		A.Set(i, i, 1)
	}
	return A
}

func Normals(rows, cols int) *DenseMatrix {
	A := Zeros(rows, cols)

	for i := 0; i < A.Rows(); i++ {
		for j := 0; j < A.Cols(); j++ {
			A.Set(i, j, rand.NormFloat64())
		}
	}

	return A
}

func Diagonal(d []float64) *DenseMatrix {
	n := len(d)
	A := Zeros(n, n)
	for i := 0; i < n; i++ {
		A.Set(i, i, d[i])
	}
	return A
}

func MakeDenseCopy(A MatrixRO) *DenseMatrix {
	B := Zeros(A.Rows(), A.Cols())
	for i := 0; i < B.rows; i++ {
		for j := 0; j < B.cols; j++ {
			B.Set(i, j, A.Get(i, j))
		}
	}
	return B
}

func MakeDenseMatrix(elements []float64, rows, cols int) *DenseMatrix {
	A := new(DenseMatrix)
	A.elements = make([]float64, rows*cols)
	A.rows = rows
	A.cols = cols
	A.step = cols
	A.elements = elements
	return A
}

func MakeDenseMatrixStacked(data [][]float64) *DenseMatrix {
	rows := len(data)
	cols := len(data[0])
	elements := make([]float64, rows*cols)
	for i := 0; i < rows; i++ {
		for j := 0; j < cols; j++ {
			elements[i*cols+j] = data[i][j]
		}
	}
	return MakeDenseMatrix(elements, rows, cols)
}

func (A *DenseMatrix) String() string { return String(A) }
<commit_msg>don't allocate memory only to throw it away<commit_after>// Copyright 2009 The GoMatrix Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package matrix

import (
	"math/rand"
)

/*
A matrix backed by a flat array of all elements.
*/
type DenseMatrix struct {
	matrix
	// flattened matrix data. elements[i*step+j] is row i, col j
	elements []float64
	// actual offset between rows
	step int
}

/*
Returns an array of slices referencing the matrix data. Changes to
the slices effect changes to the matrix.
*/
func (A *DenseMatrix) Arrays() [][]float64 {
	a := make([][]float64, A.rows)
	for i := 0; i < A.rows; i++ {
		a[i] = A.elements[i*A.step : i*A.step+A.cols]
	}
	return a
}

/*
Returns the contents of this matrix stored into a flat array (row-major).
*/
func (A *DenseMatrix) Array() []float64 {
	if A.step == A.rows {
		return A.elements[0 : A.rows*A.cols]
	}
	a := make([]float64, A.rows*A.cols)
	for i := 0; i < A.rows; i++ {
		for j := 0; j < A.cols; j++ {
			a[i*A.cols+j] = A.elements[i*A.step+j]
		}
	}
	return a
}

func (A *DenseMatrix) rowSlice(row int) []float64 {
	return A.elements[row*A.step : row*A.step+A.cols]
}

/*
Get the element in the ith row and jth column.
*/
func (A *DenseMatrix) Get(i int, j int) (v float64) {
	/*
		i = i % A.rows
		if i < 0 {
			i = A.rows - i
		}
		j = j % A.cols
		if j < 0 {
			j = A.cols - j
		}
	*/

	// reslicing like this does efficient range checks, perhaps
	v = A.elements[i*A.step : i*A.step+A.cols][j]
	//v = A.elements[i*A.step+j]
	return
}

/*
Set the element in the ith row and jth column to v.
*/
func (A *DenseMatrix) Set(i int, j int, v float64) {
	/*
		i = i % A.rows
		if i < 0 {
			i = A.rows - i
		}
		j = j % A.cols
		if j < 0 {
			j = A.cols - j
		}
	*/
	// reslicing like this does efficient range checks, perhaps
	A.elements[i*A.step : i*A.step+A.cols][j] = v
	//A.elements[i*A.step+j] = v
}

/*
Get a submatrix starting at i,j with rows rows and cols columns. Changes to
the returned matrix show up in the original.
*/
func (A *DenseMatrix) GetMatrix(i, j, rows, cols int) *DenseMatrix {
	B := new(DenseMatrix)
	B.elements = A.elements[i*A.step+j : i*A.step+j+(rows-1)*A.step+cols]
	B.rows = rows
	B.cols = cols
	B.step = A.step
	return B
}

/*
Copy B into A, with B's 0, 0 aligning with A's i, j
*/
func (A *DenseMatrix) SetMatrix(i, j int, B *DenseMatrix) {
	for r := 0; r < B.rows; r++ {
		for c := 0; c < B.cols; c++ {
			A.Set(i+r, j+c, B.Get(r, c))
		}
	}
}

func (A *DenseMatrix) GetColVector(j int) *DenseMatrix {
	return A.GetMatrix(0, j, A.rows, 1)
}

func (A *DenseMatrix) GetRowVector(i int) *DenseMatrix {
	return A.GetMatrix(i, 0, 1, A.cols)
}

/*
Get a copy of this matrix with 0s above the diagonal.
*/
func (A *DenseMatrix) L() *DenseMatrix {
	B := A.Copy()
	for i := 0; i < A.rows; i++ {
		for j := i + 1; j < A.cols; j++ {
			B.Set(i, j, 0)
		}
	}
	return B
}

/*
Get a copy of this matrix with 0s below the diagonal.
*/
func (A *DenseMatrix) U() *DenseMatrix {
	B := A.Copy()
	for i := 0; i < A.rows; i++ {
		for j := 0; j < i && j < A.cols; j++ {
			B.Set(i, j, 0)
		}
	}
	return B
}

func (A *DenseMatrix) Copy() *DenseMatrix {
	B := new(DenseMatrix)
	B.rows = A.rows
	B.cols = A.cols
	B.step = A.cols
	B.elements = make([]float64, B.rows*B.cols)
	for row := 0; row < B.rows; row++ {
		copy(B.rowSlice(row), A.rowSlice(row))
	}
	return B
}

/*
Get a new matrix [A B].
*/
func (A *DenseMatrix) Augment(B *DenseMatrix) (C *DenseMatrix, err error) {
	if A.rows != B.rows {
		err = ErrorDimensionMismatch
		return
	}
	C = Zeros(A.rows, A.cols+B.cols)
	err = A.AugmentFill(B, C)
	return
}
func (A *DenseMatrix) AugmentFill(B, C *DenseMatrix) (err error) {
	if A.rows != B.rows || C.rows != A.rows || C.cols != A.cols+B.cols {
		err = ErrorDimensionMismatch
		return
	}
	C.SetMatrix(0, 0, A)
	C.SetMatrix(0, A.cols, B)
	/*
		for i := 0; i < C.Rows(); i++ {
			for j := 0; j < A.Cols(); j++ {
				C.Set(i, j, A.Get(i, j))
			}
			for j := 0; j < B.Cols(); j++ {
				C.Set(i, j+A.Cols(), B.Get(i, j))
			}
		}*/
	return
}

/*
Get a new matrix [A; B], with A above B.
*/
func (A *DenseMatrix) Stack(B *DenseMatrix) (C *DenseMatrix, err error) {
	if A.cols != B.cols {
		err = ErrorDimensionMismatch
		return
	}
	C = Zeros(A.rows+B.rows, A.cols)
	err = A.StackFill(B, C)
	return
}
func (A *DenseMatrix) StackFill(B, C *DenseMatrix) (err error) {
	if A.cols != B.cols || C.cols != A.cols || C.rows != A.rows+B.rows {
		err = ErrorDimensionMismatch
		return
	}
	C.SetMatrix(0, 0, A)
	C.SetMatrix(A.rows, 0, B)
	/*
		for j := 0; j < A.cols; j++ {
			for i := 0; i < A.Rows(); i++ {
				C.Set(i, j, A.Get(i, j))
			}
			for i := 0; i < B.cols; i++ {
				C.Set(i+A.rows, j, B.Get(i, j))
			}
		}
	*/
	return
}

/*
Create a sparse matrix copy.
*/
func (A *DenseMatrix) SparseMatrix() *SparseMatrix {
	B := ZerosSparse(A.rows, A.cols)
	for i := 0; i < A.rows; i++ {
		for j := 0; j < A.cols; j++ {
			v := A.Get(i, j)
			if v != 0 {
				B.Set(i, j, v)
			}
		}
	}
	return B
}

func (A *DenseMatrix) DenseMatrix() *DenseMatrix {
	return A.Copy()
}

func Zeros(rows, cols int) *DenseMatrix {
	A := new(DenseMatrix)
	A.elements = make([]float64, rows*cols)
	A.rows = rows
	A.cols = cols
	A.step = cols
	return A
}

func Ones(rows, cols int) *DenseMatrix {
	A := new(DenseMatrix)
	A.elements = make([]float64, rows*cols)
	A.rows = rows
	A.cols = cols
	A.step = cols

	for i := 0; i < len(A.elements); i++ {
		A.elements[i] = 1
	}

	return A
}

func Numbers(rows, cols int, num float64) *DenseMatrix {
	A := Zeros(rows, cols)

	for i := 0; i < A.Rows(); i++ {
		for j := 0; j < A.Cols(); j++ {
			A.Set(i, j, num)
		}
	}

	return A
}

/*
Create an identity matrix with span rows and span columns.
*/
func Eye(span int) *DenseMatrix {
	A := Zeros(span, span)
	for i := 0; i < span; i++ {
		A.Set(i, i, 1)
	}
	return A
}

func Normals(rows, cols int) *DenseMatrix {
	A := Zeros(rows, cols)

	for i := 0; i < A.Rows(); i++ {
		for j := 0; j < A.Cols(); j++ {
			A.Set(i, j, rand.NormFloat64())
		}
	}

	return A
}

func Diagonal(d []float64) *DenseMatrix {
	n := len(d)
	A := Zeros(n, n)
	for i := 0; i < n; i++ {
		A.Set(i, i, d[i])
	}
	return A
}

func MakeDenseCopy(A MatrixRO) *DenseMatrix {
	B := Zeros(A.Rows(), A.Cols())
	for i := 0; i < B.rows; i++ {
		for j := 0; j < B.cols; j++ {
			B.Set(i, j, A.Get(i, j))
		}
	}
	return B
}

func MakeDenseMatrix(elements []float64, rows, cols int) *DenseMatrix {
	A := new(DenseMatrix)
	A.rows = rows
	A.cols = cols
	A.step = cols
	A.elements = elements
	return A
}

func MakeDenseMatrixStacked(data [][]float64) *DenseMatrix {
	rows := len(data)
	cols := len(data[0])
	elements := make([]float64, rows*cols)
	for i := 0; i < rows; i++ {
		for j := 0; j < cols; j++ {
			elements[i*cols+j] = data[i][j]
		}
	}
	return MakeDenseMatrix(elements, rows, cols)
}

func (A *DenseMatrix) String() string { return String(A) }
<|endoftext|>


<commit_before>package redistest

import (
	"fmt"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"gopkg.in/redis.v5"
)

func TestRunMasterServer(t *testing.T) {
	s, err := RunServer(DefaultMasterPort)
	defer s.Stop()
	assert.NoError(t, err)

	s2, err := RunServer(DefaultMasterPort)
	assert.Nil(t, s2)
	assert.Error(t, err)
}

func TestServer_RunAndStop(t *testing.T) {
	s, err := RunServer(DefaultMasterPort)
	assert.NoError(t, err)

	err = s.Run()
	assert.Error(t, err)

	err = s.Stop()
	assert.NoError(t, err)

	err = s.Stop()
	assert.Error(t, err)

	err = s.Run()
	assert.NoError(t, err)

	err = s.Stop()
	assert.NoError(t, err)
}

func TestServer_RunSlaveServer(t *testing.T) {
	m, err := RunServer(DefaultMasterPort)
	defer m.Stop()
	assert.NoError(t, err)

	s, err := m.RunSlaveServer(DefaultSlavePort)
	defer s.Stop()
	assert.NoError(t, err)
	assert.True(t, s.IsRunning())

	c := redis.NewClient(&redis.Options{Addr: fmt.Sprintf(":%d", DefaultMasterPort)})
	err = c.Set("foo", "bar", 0).Err()
	assert.NoError(t, err)
	c.Close()

	// Wait for the replication sync
	time.Sleep(500 * time.Millisecond)

	c = redis.NewClient(&redis.Options{Addr: fmt.Sprintf(":%d", DefaultSlavePort)})
	bar, err := c.Get("foo").Result()
	assert.NoError(t, err)
	assert.Equal(t, "bar", bar)
	c.Close()
}

func TestServer_RunSentinelServer(t *testing.T) {
	m, err := RunServer(DefaultMasterPort)
	defer m.Stop()
	assert.NoError(t, err)

	s, err := m.RunSentinelServer(DefaultSentinelPort, DefaultSentinelMaster)
	defer s.Stop()
	assert.NoError(t, err)
	assert.True(t, s.IsRunning())

	c := redis.NewFailoverClient(&redis.FailoverOptions{
		MasterName:    DefaultSentinelMaster,
		SentinelAddrs: []string{fmt.Sprintf("localhost:%d", DefaultSentinelPort)},
	})
	err = c.Set("foo", "bar", 0).Err()
	assert.NoError(t, err)
	c.Close()

	c = redis.NewClient(&redis.Options{Addr: fmt.Sprintf(":%d", DefaultMasterPort)})
	bar, err := c.Get("foo").Result()
	assert.NoError(t, err)
	assert.Equal(t, "bar", bar)
	c.Close()
}

func TestServer_IsRunning(t *testing.T) {
	s, err := RunServer(DefaultMasterPort)
	assert.NoError(t, err)
	assert.True(t, s.IsRunning())

	err = s.Stop()
	assert.NoError(t, err)
	assert.False(t, s.IsRunning())
}

func TestServer_Port(t *testing.T) {
	s, err := RunServer(DefaultMasterPort)
	defer s.Stop()

	assert.NoError(t, err)
	assert.Equal(t, DefaultMasterPort, s.Port())
}

func TestServer_Flush(t *testing.T) {
	s, err := RunServer(DefaultMasterPort)
	defer s.Stop()

	c := s.NewClient()
	defer c.Close()

	c.Set("foo", "bar", 0)

	n, err := c.DbSize().Result()
	assert.NoError(t, err)
	assert.Equal(t, 1, int(n))

	err = s.Flush()
	assert.NoError(t, err)

	n, err = c.DbSize().Result()
	assert.NoError(t, err)
	assert.Zero(t, n)
}
<commit_msg>Add test for Addr<commit_after>package redistest

import (
	"fmt"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"gopkg.in/redis.v5"
)

func TestRunMasterServer(t *testing.T) {
	s, err := RunServer(DefaultMasterPort)
	defer s.Stop()
	assert.NoError(t, err)

	s2, err := RunServer(DefaultMasterPort)
	assert.Nil(t, s2)
	assert.Error(t, err)
}

func TestServer_RunAndStop(t *testing.T) {
	s, err := RunServer(DefaultMasterPort)
	assert.NoError(t, err)

	err = s.Run()
	assert.Error(t, err)

	err = s.Stop()
	assert.NoError(t, err)

	err = s.Stop()
	assert.Error(t, err)

	err = s.Run()
	assert.NoError(t, err)

	err = s.Stop()
	assert.NoError(t, err)
}

func TestServer_RunSlaveServer(t *testing.T) {
	m, err := RunServer(DefaultMasterPort)
	defer m.Stop()
	assert.NoError(t, err)

	s, err := m.RunSlaveServer(DefaultSlavePort)
	defer s.Stop()
	assert.NoError(t, err)
	assert.True(t, s.IsRunning())

	c := redis.NewClient(&redis.Options{Addr: fmt.Sprintf(":%d", DefaultMasterPort)})
	err = c.Set("foo", "bar", 0).Err()
	assert.NoError(t, err)
	c.Close()

	// Wait for the replication sync
	time.Sleep(500 * time.Millisecond)

	c = redis.NewClient(&redis.Options{Addr: fmt.Sprintf(":%d", DefaultSlavePort)})
	bar, err := c.Get("foo").Result()
	assert.NoError(t, err)
	assert.Equal(t, "bar", bar)
	c.Close()
}

func TestServer_RunSentinelServer(t *testing.T) {
	m, err := RunServer(DefaultMasterPort)
	defer m.Stop()
	assert.NoError(t, err)

	s, err := m.RunSentinelServer(DefaultSentinelPort, DefaultSentinelMaster)
	defer s.Stop()
	assert.NoError(t, err)
	assert.True(t, s.IsRunning())

	c := redis.NewFailoverClient(&redis.FailoverOptions{
		MasterName:    DefaultSentinelMaster,
		SentinelAddrs: []string{fmt.Sprintf("localhost:%d", DefaultSentinelPort)},
	})
	err = c.Set("foo", "bar", 0).Err()
	assert.NoError(t, err)
	c.Close()

	c = redis.NewClient(&redis.Options{Addr: fmt.Sprintf(":%d", DefaultMasterPort)})
	bar, err := c.Get("foo").Result()
	assert.NoError(t, err)
	assert.Equal(t, "bar", bar)
	c.Close()
}

func TestServer_IsRunning(t *testing.T) {
	s, err := RunServer(DefaultMasterPort)
	assert.NoError(t, err)
	assert.True(t, s.IsRunning())

	err = s.Stop()
	assert.NoError(t, err)
	assert.False(t, s.IsRunning())
}

func TestServer_Port(t *testing.T) {
	s, err := RunServer(DefaultMasterPort)
	defer s.Stop()

	assert.NoError(t, err)
	assert.Equal(t, DefaultMasterPort, s.Port())
}

func TestServer_Addr(t *testing.T) {
	s, err := RunServer(DefaultMasterPort)
	defer s.Stop()

	assert.NoError(t, err)
	assert.Equal(t, fmt.Sprintf("localhost:%d", DefaultMasterPort), s.Addr())
}

func TestServer_Flush(t *testing.T) {
	s, err := RunServer(DefaultMasterPort)
	defer s.Stop()

	c := s.NewClient()
	defer c.Close()

	c.Set("foo", "bar", 0)

	n, err := c.DbSize().Result()
	assert.NoError(t, err)
	assert.Equal(t, 1, int(n))

	err = s.Flush()
	assert.NoError(t, err)

	n, err = c.DbSize().Result()
	assert.NoError(t, err)
	assert.Zero(t, n)
}
<|endoftext|>


<commit_before>package registry

import (
	"path"
	"time"

	"github.com/coreos/fleet/etcd"
)

const (
	leasePrefix = "lease"
)

func (r *EtcdRegistry) AcquireLease(name, machID string, period time.Duration) (Lease, error) {
	key := path.Join(r.keyPrefix, leasePrefix, name)
	req := etcd.Create{
		Key:   key,
		Value: machID,
		TTL:   period,
	}

	var lease Lease
	resp, err := r.etcd.Do(&req)
	if err == nil {
		lease = &etcdLease{
			key:   key,
			value: machID,
			idx:   resp.Node.ModifiedIndex,
			etcd:  r.etcd,
		}
	} else if isNodeExist(err) {
		err = nil
	}

	return lease, err
}

// etcdLease implements the Lease interface
type etcdLease struct {
	key   string
	value string
	idx   uint64
	etcd  etcd.Client
}

func (l *etcdLease) Release() error {
	req := etcd.Delete{
		Key:           l.key,
		PreviousIndex: l.idx,
	}
	_, err := l.etcd.Do(&req)
	return err
}

func (l *etcdLease) Renew(period time.Duration) error {
	req := etcd.Set{
		Key:           l.key,
		Value:         l.value,
		PreviousIndex: l.idx,
		TTL:           period,
	}

	resp, err := l.etcd.Do(&req)
	if err == nil {
		l.idx = resp.Node.ModifiedIndex
	}

	return err
}
<commit_msg>registry: break out leasePath helper<commit_after>package registry

import (
	"path"
	"time"

	"github.com/coreos/fleet/etcd"
)

const (
	leasePrefix = "lease"
)

func (r *EtcdRegistry) leasePath(name string) string {
	return path.Join(r.keyPrefix, leasePrefix, name)
}

func (r *EtcdRegistry) AcquireLease(name, machID string, period time.Duration) (Lease, error) {
	key := r.leasePath(name)
	req := etcd.Create{
		Key:   key,
		Value: machID,
		TTL:   period,
	}

	var lease Lease
	resp, err := r.etcd.Do(&req)
	if err == nil {
		lease = &etcdLease{
			key:   key,
			value: machID,
			idx:   resp.Node.ModifiedIndex,
			etcd:  r.etcd,
		}
	} else if isNodeExist(err) {
		err = nil
	}

	return lease, err
}

// etcdLease implements the Lease interface
type etcdLease struct {
	key   string
	value string
	idx   uint64
	etcd  etcd.Client
}

func (l *etcdLease) Release() error {
	req := etcd.Delete{
		Key:           l.key,
		PreviousIndex: l.idx,
	}
	_, err := l.etcd.Do(&req)
	return err
}

func (l *etcdLease) Renew(period time.Duration) error {
	req := etcd.Set{
		Key:           l.key,
		Value:         l.value,
		PreviousIndex: l.idx,
		TTL:           period,
	}

	resp, err := l.etcd.Do(&req)
	if err == nil {
		l.idx = resp.Node.ModifiedIndex
	}

	return err
}
<|endoftext|>


<commit_before>package repo

import (
	"os"
	"testing"

	"github.com/sahib/brig/backend/mock"
	"github.com/stretchr/testify/require"
)

func TestRepoInit(t *testing.T) {
	testDir := "/tmp/.brig-repo-test"
	require.Nil(t, os.RemoveAll(testDir))

	err := Init(testDir, "alice", "klaus", "mock")
	require.Nil(t, err)

	rp, err := Open(testDir, "klaus")
	require.Nil(t, err)

	bk := mock.NewMockBackend()
	fs, err := rp.FS(rp.CurrentUser(), bk)
	require.Nil(t, err)

	// TODO: Assert a bit more that fs is working.
	require.NotNil(t, fs)
	require.Nil(t, fs.Close())

	require.Nil(t, rp.Close("klaus"))
}
<commit_msg>repo: add test to assert that deduplication works<commit_after>package repo

import (
	"bytes"
	"os"
	"path/filepath"
	"testing"

	"github.com/sahib/brig/backend/ipfs"
	"github.com/sahib/brig/backend/mock"
	"github.com/sahib/brig/util/testutil"
	"github.com/stretchr/testify/require"
)

func TestRepoInit(t *testing.T) {
	testDir := "/tmp/.brig-repo-test"
	require.Nil(t, os.RemoveAll(testDir))

	err := Init(testDir, "alice", "klaus", "mock")
	require.Nil(t, err)

	rp, err := Open(testDir, "klaus")
	require.Nil(t, err)

	bk := mock.NewMockBackend()
	fs, err := rp.FS(rp.CurrentUser(), bk)
	require.Nil(t, err)

	// TODO: Assert a bit more that fs is working.
	require.NotNil(t, fs)
	require.Nil(t, fs.Close())

	require.Nil(t, rp.Close("klaus"))

}

func dirSize(t *testing.T, path string) int64 {
	var size int64
	err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
		if !info.IsDir() {
			size += info.Size()
		}

		return err
	})

	if err != nil {
		t.Fatalf("Failed to get directory size of `%s`: %v", path, err)
	}

	return size
}

func TestRepoDeduplication(t *testing.T) {
	testDir := "/tmp/.brig-repo-test"
	require.Nil(t, os.RemoveAll(testDir))
	err := Init(testDir, "alice", "klaus", "ipfs")
	require.Nil(t, err)

	rp, err := Open(testDir, "klaus")
	require.Nil(t, err)

	ipfsPath := filepath.Join(testDir, "data/ipfs")
	require.Nil(t, ipfs.Init(ipfsPath, 1024))

	bk, err := ipfs.New(ipfsPath)
	require.Nil(t, err)

	fs, err := rp.FS(rp.CurrentUser(), bk)
	require.Nil(t, err)
	require.NotNil(t, fs)

	size := dirSize(t, testDir)
	require.True(t, size < 1*1024*1024)

	data := testutil.CreateDummyBuf(8 * 1024 * 1024)

	// Adding a 8MB file should put the size of the repo
	// at somewhere around this size (+ init bytes)
	fs.Stage("/x", bytes.NewReader(data))

	size = dirSize(t, testDir)
	require.True(t, size < 9*1024*1024)
	require.True(t, size > 7*1024*1024)

	// Adding the same file under a different path should
	// not add to the total size of the repository
	// (except a few bytes for storing the metadata)
	fs.Stage("/y", bytes.NewReader(data))

	size = dirSize(t, testDir)
	require.True(t, size < 9*1024*1024)
	require.True(t, size > 7*1024*1024)

	// Modify the beginning of the data,
	// key did not change so there should be only a minimal
	// size increase in the first block (~+64k)
	data[0] += 1
	fs.Stage("/x", bytes.NewReader(data))

	size = dirSize(t, testDir)
	require.True(t, size < 9*1024*1024)
	require.True(t, size > 7*1024*1024)

	// This case is not covered yet:
	// (i.e. adding the same file as "/x" has anew,
	//  this will cause brig to generate a new key,
	//  resulting in a totally data stream)
	fs.Stage("/z", bytes.NewReader(data))
	size = dirSize(t, testDir)
	require.True(t, size < 18*1024*1024)
	require.True(t, size > 16*1024*1024)

	require.Nil(t, fs.Close())
	require.Nil(t, rp.Close("klaus"))
}
<|endoftext|>


<commit_before>// +build !windows

package cli

import (
	"os/exec"

	"github.com/gopasspw/gopass/pkg/debug"
	"github.com/gopasspw/pinentry/gpgconf"
)

func detectBinary(name string) (string, error) {
	// user supplied binaries take precedence
	if name != "" {
		return exec.LookPath(name)
	}
	// try to get the proper binary from gpgconf(1)
	p, err := gpgconf.Path("gpg")
	if err != nil || p == "" {
		debug.Log("gpgconf failed, falling back to path lookup: %q", err)
		// otherwise fall back to the default and try
		// to look up "gpg"
		return exec.LookPath("gpg")
	}

	debug.Log("gpgconf returned %q for gpg", p)
	return p, nil
}
<commit_msg>Only use gpgconf result if the file exists (#1903)<commit_after>// +build !windows

package cli

import (
	"os/exec"

	"github.com/gopasspw/gopass/pkg/debug"
	"github.com/gopasspw/gopass/pkg/fsutil"
	"github.com/gopasspw/pinentry/gpgconf"
)

func detectBinary(name string) (string, error) {
	// user supplied binaries take precedence
	if name != "" {
		return exec.LookPath(name)
	}
	// try to get the proper binary from gpgconf(1)
	p, err := gpgconf.Path("gpg")
	if err != nil || p == "" || !fsutil.IsFile(p) {
		debug.Log("gpgconf failed (%q), falling back to path lookup: %q", p, err)
		// otherwise fall back to the default and try
		// to look up "gpg"
		return exec.LookPath("gpg")
	}

	debug.Log("gpgconf returned %q for gpg", p)
	return p, nil
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"github.com/bmizerany/pat"
	"github.com/timeredbull/tsuru/api/app"
	"github.com/timeredbull/tsuru/api/auth"
	"github.com/timeredbull/tsuru/api/service/consumption"
	"github.com/timeredbull/tsuru/api/service/provision"
	"github.com/timeredbull/tsuru/config"
	"github.com/timeredbull/tsuru/db"
	"github.com/timeredbull/tsuru/log"
	"github.com/timeredbull/tsuru/repository"
	stdlog "log"
	"log/syslog"
	"net/http"
)

func main() {
	var err error
	log.Target, err = syslog.NewLogger(syslog.LOG_INFO, stdlog.LstdFlags)
	if err != nil {
		panic(err)
	}
	configFile := flag.String("config", "/etc/tsuru/tsuru.conf", "tsuru config file")
	dry := flag.Bool("dry", false, "dry-run: does not start the server (for testing purpose)")
	flag.Parse()
	err = config.ReadConfigFile(*configFile)
	if err != nil {
		log.Panic(err.Error())
	}
	connString, err := config.GetString("database:url")
	if err != nil {
		panic(err)
	}
	dbName, err := config.GetString("database:name")
	if err != nil {
		panic(err)
	}
	db.Session, err = db.Open(connString, dbName)
	if err != nil {
		log.Panic(err.Error())
	}
	defer db.Session.Close()

	repository.RunAgent()
	m := pat.New()

	m.Get("/services/instances", AuthorizationRequiredHandler(consumption.ServicesInstancesHandler))
	m.Post("/services/instances", AuthorizationRequiredHandler(consumption.CreateInstanceHandler))
	m.Put("/services/instances/:instance/:app", AuthorizationRequiredHandler(app.BindHandler))
	m.Del("/services/instances/:instance/:app", AuthorizationRequiredHandler(app.UnbindHandler))
	m.Del("/services/c/instances/:name", AuthorizationRequiredHandler(consumption.RemoveServiceInstanceHandler))
	m.Get("/services/instances/:instance/status", AuthorizationRequiredHandler(consumption.ServiceInstanceStatusHandler))

	m.Get("/services", AuthorizationRequiredHandler(provision.ServicesHandler))
	m.Post("/services", AuthorizationRequiredHandler(provision.CreateHandler))
	m.Put("/services", AuthorizationRequiredHandler(provision.UpdateHandler))
	m.Del("/services/:name", AuthorizationRequiredHandler(provision.DeleteHandler))
	m.Get("/services/:name", AuthorizationRequiredHandler(consumption.ServiceInfoHandler))
	m.Get("/services/c/:name/doc", AuthorizationRequiredHandler(consumption.Doc))
	m.Get("/services/:name/doc", AuthorizationRequiredHandler(provision.GetDocHandler))
	m.Put("/services/:name/doc", AuthorizationRequiredHandler(provision.AddDocHandler))
	m.Put("/services/:service/:team", AuthorizationRequiredHandler(provision.GrantAccessToTeamHandler))
	m.Del("/services/:service/:team", AuthorizationRequiredHandler(provision.RevokeAccessFromTeamHandler))

	m.Del("/apps/:name", AuthorizationRequiredHandler(app.AppDelete))
	m.Get("/apps/:name/repository/clone", Handler(app.CloneRepositoryHandler))
	m.Get("/apps/:name", AuthorizationRequiredHandler(app.AppInfo))
	m.Post("/apps/:name/run", AuthorizationRequiredHandler(app.RunCommand))
	m.Get("/apps/:name/restart", AuthorizationRequiredHandler(app.AppInfo))
	m.Get("/apps/:name/env", AuthorizationRequiredHandler(app.GetEnv))
	m.Post("/apps/:name/env", AuthorizationRequiredHandler(app.SetEnv))
	m.Del("/apps/:name/env", AuthorizationRequiredHandler(app.UnsetEnv))
	m.Get("/apps", AuthorizationRequiredHandler(app.AppList))
	m.Post("/apps", AuthorizationRequiredHandler(app.CreateAppHandler))
	m.Put("/apps/:app/:team", AuthorizationRequiredHandler(app.GrantAccessToTeamHandler))
	m.Del("/apps/:app/:team", AuthorizationRequiredHandler(app.RevokeAccessFromTeamHandler))
	m.Get("/apps/:name/log", AuthorizationRequiredHandler(app.AppLog))

	m.Post("/users", Handler(auth.CreateUser))
	m.Post("/users/:email/tokens", Handler(auth.Login))
	m.Post("/users/keys", AuthorizationRequiredHandler(auth.AddKeyToUser))
	m.Del("/users/keys", AuthorizationRequiredHandler(auth.RemoveKeyFromUser))

	m.Get("/teams", AuthorizationRequiredHandler(auth.ListTeams))
	m.Post("/teams", AuthorizationRequiredHandler(auth.CreateTeam))
	m.Put("/teams/:team/:user", AuthorizationRequiredHandler(auth.AddUserToTeam))
	m.Del("/teams/:team/:user", AuthorizationRequiredHandler(auth.RemoveUserFromTeam))

	if !*dry {
		listen, err := config.GetString("listen")
		if err != nil {
			panic(err)
		}
		log.Fatal(http.ListenAndServe(listen, m))
	}
}
<commit_msg>api/webserver: register the right handler<commit_after>package main

import (
	"flag"
	"github.com/bmizerany/pat"
	"github.com/timeredbull/tsuru/api/app"
	"github.com/timeredbull/tsuru/api/auth"
	"github.com/timeredbull/tsuru/api/service/consumption"
	"github.com/timeredbull/tsuru/api/service/provision"
	"github.com/timeredbull/tsuru/config"
	"github.com/timeredbull/tsuru/db"
	"github.com/timeredbull/tsuru/log"
	"github.com/timeredbull/tsuru/repository"
	stdlog "log"
	"log/syslog"
	"net/http"
)

func main() {
	var err error
	log.Target, err = syslog.NewLogger(syslog.LOG_INFO, stdlog.LstdFlags)
	if err != nil {
		panic(err)
	}
	configFile := flag.String("config", "/etc/tsuru/tsuru.conf", "tsuru config file")
	dry := flag.Bool("dry", false, "dry-run: does not start the server (for testing purpose)")
	flag.Parse()
	err = config.ReadConfigFile(*configFile)
	if err != nil {
		log.Panic(err.Error())
	}
	connString, err := config.GetString("database:url")
	if err != nil {
		panic(err)
	}
	dbName, err := config.GetString("database:name")
	if err != nil {
		panic(err)
	}
	db.Session, err = db.Open(connString, dbName)
	if err != nil {
		log.Panic(err.Error())
	}
	defer db.Session.Close()

	repository.RunAgent()
	m := pat.New()

	m.Get("/services/instances", AuthorizationRequiredHandler(consumption.ServicesInstancesHandler))
	m.Post("/services/instances", AuthorizationRequiredHandler(consumption.CreateInstanceHandler))
	m.Put("/services/instances/:instance/:app", AuthorizationRequiredHandler(app.BindHandler))
	m.Del("/services/instances/:instance/:app", AuthorizationRequiredHandler(app.UnbindHandler))
	m.Del("/services/c/instances/:name", AuthorizationRequiredHandler(consumption.RemoveServiceInstanceHandler))
	m.Get("/services/instances/:instance/status", AuthorizationRequiredHandler(consumption.ServiceInstanceStatusHandler))

	m.Get("/services", AuthorizationRequiredHandler(provision.ServicesHandler))
	m.Post("/services", AuthorizationRequiredHandler(provision.CreateHandler))
	m.Put("/services", AuthorizationRequiredHandler(provision.UpdateHandler))
	m.Del("/services/:name", AuthorizationRequiredHandler(provision.DeleteHandler))
	m.Get("/services/:name", AuthorizationRequiredHandler(consumption.ServiceInfoHandler))
	m.Get("/services/c/:name/doc", AuthorizationRequiredHandler(consumption.Doc))
	m.Get("/services/:name/doc", AuthorizationRequiredHandler(provision.GetDocHandler))
	m.Put("/services/:name/doc", AuthorizationRequiredHandler(provision.AddDocHandler))
	m.Put("/services/:service/:team", AuthorizationRequiredHandler(provision.GrantAccessToTeamHandler))
	m.Del("/services/:service/:team", AuthorizationRequiredHandler(provision.RevokeAccessFromTeamHandler))

	m.Del("/apps/:name", AuthorizationRequiredHandler(app.AppDelete))
	m.Get("/apps/:name/repository/clone", Handler(app.CloneRepositoryHandler))
	m.Get("/apps/:name", AuthorizationRequiredHandler(app.AppInfo))
	m.Post("/apps/:name/run", AuthorizationRequiredHandler(app.RunCommand))
	m.Get("/apps/:name/restart", AuthorizationRequiredHandler(app.RestartHandler))
	m.Get("/apps/:name/env", AuthorizationRequiredHandler(app.GetEnv))
	m.Post("/apps/:name/env", AuthorizationRequiredHandler(app.SetEnv))
	m.Del("/apps/:name/env", AuthorizationRequiredHandler(app.UnsetEnv))
	m.Get("/apps", AuthorizationRequiredHandler(app.AppList))
	m.Post("/apps", AuthorizationRequiredHandler(app.CreateAppHandler))
	m.Put("/apps/:app/:team", AuthorizationRequiredHandler(app.GrantAccessToTeamHandler))
	m.Del("/apps/:app/:team", AuthorizationRequiredHandler(app.RevokeAccessFromTeamHandler))
	m.Get("/apps/:name/log", AuthorizationRequiredHandler(app.AppLog))

	m.Post("/users", Handler(auth.CreateUser))
	m.Post("/users/:email/tokens", Handler(auth.Login))
	m.Post("/users/keys", AuthorizationRequiredHandler(auth.AddKeyToUser))
	m.Del("/users/keys", AuthorizationRequiredHandler(auth.RemoveKeyFromUser))

	m.Get("/teams", AuthorizationRequiredHandler(auth.ListTeams))
	m.Post("/teams", AuthorizationRequiredHandler(auth.CreateTeam))
	m.Put("/teams/:team/:user", AuthorizationRequiredHandler(auth.AddUserToTeam))
	m.Del("/teams/:team/:user", AuthorizationRequiredHandler(auth.RemoveUserFromTeam))

	if !*dry {
		listen, err := config.GetString("listen")
		if err != nil {
			panic(err)
		}
		log.Fatal(http.ListenAndServe(listen, m))
	}
}
<|endoftext|>


<commit_before>/*                          _       _
 *__      _____  __ ___   ___  __ _| |_ ___
 *\ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
 * \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
 *  \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
 *
 * Copyright © 2016 - 2018 Weaviate. All rights reserved.
 * LICENSE: https://github.com/creativesoftwarefdn/weaviate/blob/develop/LICENSE.md
 * AUTHOR: Bob van Luijt (bob@kub.design)
 * See www.creativesoftwarefdn.org for details
 * Contact: @CreativeSofwFdn / bob@kub.design
 */

package connutils

import (
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"log"
	"net"
	"net/http"
	"regexp"
	"runtime"
	"time"

	"github.com/go-openapi/strfmt"
	gouuid "github.com/satori/go.uuid"
	"golang.org/x/crypto/bcrypt"

	"github.com/creativesoftwarefdn/weaviate/config"
	"github.com/creativesoftwarefdn/weaviate/models"
)

// NewDatabaseObjectFromPrincipal creates a new object with default values, out of principle object
// func NewDatabaseObjectFromPrincipal(principal interface{}, refType string) *DatabaseObject {
// 	// Get user object
// 	Key, _ := PrincipalMarshalling(principal)

// 	// Generate DatabaseObject without JSON-object in it.
// 	key := NewDatabaseObject(Key.Uuid, refType)

// 	return key
// }

// CreateRootKeyObject creates a new user with new API key when none exists when starting server
func CreateRootKeyObject(key *models.Key) (hashedToken string, UUID strfmt.UUID) {
	// Create key token and UUID
	token := GenerateUUID()
	UUID = GenerateUUID()

	// Do not set any parent

	// Set expiry to unlimited
	key.KeyExpiresUnix = -1

	// Get ips as v6
	var ips []string
	ifaces, _ := net.Interfaces()
	for _, i := range ifaces {
		addrs, _ := i.Addrs()
		for _, addr := range addrs {
			var ip net.IP
			switch v := addr.(type) {
			case *net.IPNet:
				ip = v.IP
			case *net.IPAddr:
				ip = v.IP
			}

			ipv6 := ip.To16()
			ips = append(ips, ipv6.String())
		}
	}

	key.IPOrigin = ips

	// Set chmod variables
	key.Read = true
	key.Write = true
	key.Delete = true
	key.Execute = true

	// Set Mail
	key.Email = "weaviate@weaviate.nl"

	// Print the key
	log.Println("INFO: No root key was found, a new root key is created. More info: https://github.com/creativesoftwarefdn/weaviate/blob/develop/README.md#authentication")
	log.Println("INFO: Auto set allowed IPs to: ", key.IPOrigin)
	log.Println("ROOTTOKEN=" + token)
	log.Println("ROOTKEY=" + string(UUID))

	hashedToken = TokenHasher(token)

	return
}

// TokenHasher is the function used to hash the UUID token
func TokenHasher(UUID strfmt.UUID) string {
	hashed, _ := bcrypt.GenerateFromPassword([]byte(UUID), bcrypt.DefaultCost)
	return string(hashed)
}

// TokenHashCompare is the function used to compare the hash with given UUID
func TokenHashCompare(hashed string, token strfmt.UUID) bool {
	err := bcrypt.CompareHashAndPassword([]byte(hashed), []byte(token))
	return err == nil
}

// Trace is used to display the running function in a connector
func Trace() {
	pc := make([]uintptr, 10) // at least 1 entry needed
	runtime.Callers(2, pc)
	f2 := runtime.FuncForPC(pc[0])
	//file, line := f2.FileLine(pc[0])
	fmt.Printf("THIS FUNCTION RUNS: %s\n", f2.Name())
}

// NowUnix returns the current Unix time
func NowUnix() int64 {
	return MakeUnixMillisecond(time.Now())
}

// MakeUnixMillisecond returns the millisecond unix-version of the given time
func MakeUnixMillisecond(t time.Time) int64 {
	return t.UnixNano() / int64(time.Millisecond)
}

// GenerateUUID returns a new UUID
func GenerateUUID() strfmt.UUID {

	// generate the uuid
	uuid, err := gouuid.NewV4()

	// panic, can't create uuid
	if err != nil {
		panic("PANIC: Can't create UUID")
	}

	// return the uuid and the error
	return strfmt.UUID(fmt.Sprintf("%v", uuid))
}

// Must panics if error, otherwise returns value
func Must(i interface{}, err error) interface{} {
	if err != nil {
		panic(err)
	}
	return i
}

// WhereStringToStruct is the 'compiler' for converting the filter/where query-string into a struct
func WhereStringToStruct(prop string, where string) (WhereQuery, error) {
	whereQuery := WhereQuery{}

	// Make a regex which can compile a string like 'firstName>=~John'
	re1, _ := regexp.Compile(`^([a-zA-Z0-9]*)([:<>!=]*)([~]*)([^~]*)$`)
	result := re1.FindStringSubmatch(where)

	// Set which property
	whereQuery.Property = prop
	if len(result[1]) > 1 && len(result[4]) != 0 {
		whereQuery.Property = fmt.Sprintf("%s.%s", prop, result[1])
	}

	// Set the operator
	switch result[2] {
	// When operator is "", put in 'Equal' as operator
	case ":", "", "=":
		whereQuery.Value.Operator = Equal
	case "!:", "!=":
		whereQuery.Value.Operator = NotEqual
	// TODO: https://github.com/creativesoftwarefdn/weaviate/issues/202
	// case ">":
	// 	whereQuery.Value.Operator = GreaterThan
	// case ">:", ">=":
	// 	whereQuery.Value.Operator = GreaterThanEqual
	// case "<":
	// 	whereQuery.Value.Operator = LessThan
	// case "<:", "<=":
	// 	whereQuery.Value.Operator = LessThanEqual
	default:
		return whereQuery, errors.New("invalid operator set in query")
	}

	// The wild cards
	// TODO: Wildcard search is disabled for now https://github.com/creativesoftwarefdn/weaviate/issues/202
	whereQuery.Value.Contains = false //result[3] == "~"

	// Set the value itself
	if len(result[4]) == 0 {
		if len(result[1]) > 0 && len(result[2]) == 0 && len(result[3]) == 0 {
			// If only result[1] is set, just use that as search term.
			whereQuery.Value.Value = result[1]
		} else {
			// When value is "", throw error
			return whereQuery, errors.New("no value is set in the query")
		}
	} else {
		whereQuery.Value.Value = result[4]
	}

	return whereQuery, nil
}

// DoExternalRequest does a request to an external Weaviate Instance based on given parameters
func DoExternalRequest(instance config.Instance, endpoint string, uuid strfmt.UUID) (response *http.Response, err error) {
	// Create the transport and HTTP client
	client := &http.Client{Transport: &http.Transport{
		MaxIdleConns:       10,
		IdleConnTimeout:    30 * time.Second,
		DisableCompression: true,
	}}

	// Create the request with basic headers
	req, _ := http.NewRequest("GET", fmt.Sprintf("%s/weaviate/v1/%s/%s", instance.URL, endpoint, uuid), nil)
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Accept", "application/json")
	req.Header.Set("X-API-KEY", instance.APIKey)
	req.Header.Set("X-API-TOKEN", instance.APIToken)

	// Do the request
	response, err = client.Do(req)

	if err != nil {
		return
	}

	// Check the status-code to determine existence
	if response.StatusCode != 200 {
		err = fmt.Errorf("status code is not 200, but %d with status '%s'", response.StatusCode, response.Status)
	}

	return
}

// ResolveExternalCrossRef resolves an object on an external instance using the given parameters and the Weaviate REST-API of the external instance
func ResolveExternalCrossRef(instance config.Instance, endpoint string, uuid strfmt.UUID, responseObject interface{}) (err error) {
	// Do the request
	response, err := DoExternalRequest(instance, endpoint, uuid)

	// Return error
	if err != nil {
		return
	}

	// Close the body on the end of the function
	defer response.Body.Close()

	// Read the body and fill the object with the data from the response
	body, _ := ioutil.ReadAll(response.Body)
	json.Unmarshal(body, responseObject)

	return
}
<commit_msg>gh-432: Needed to enable creating root key from initial settings<commit_after>/*                          _       _
 *__      _____  __ ___   ___  __ _| |_ ___
 *\ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
 * \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
 *  \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
 *
 * Copyright © 2016 - 2018 Weaviate. All rights reserved.
 * LICENSE: https://github.com/creativesoftwarefdn/weaviate/blob/develop/LICENSE.md
 * AUTHOR: Bob van Luijt (bob@kub.design)
 * See www.creativesoftwarefdn.org for details
 * Contact: @CreativeSofwFdn / bob@kub.design
 */

package connutils

import (
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"log"
	"net"
	"net/http"
	"regexp"
	"runtime"
	"time"

	"github.com/go-openapi/strfmt"
	gouuid "github.com/satori/go.uuid"
	"golang.org/x/crypto/bcrypt"

	"github.com/creativesoftwarefdn/weaviate/config"
	"github.com/creativesoftwarefdn/weaviate/models"
)

// NewDatabaseObjectFromPrincipal creates a new object with default values, out of principle object
// func NewDatabaseObjectFromPrincipal(principal interface{}, refType string) *DatabaseObject {
// 	// Get user object
// 	Key, _ := PrincipalMarshalling(principal)

// 	// Generate DatabaseObject without JSON-object in it.
// 	key := NewDatabaseObject(Key.Uuid, refType)

// 	return key
// }

// CreateRootKeyObject creates a new user with new API key when none exists when starting server
func CreateRootKeyObject(key *models.Key) (hashedToken string, UUID strfmt.UUID) {
	// Create key token and UUID
	token := GenerateUUID()
	UUID = GenerateUUID()

	hashedToken = CreateRootKeyObjectFromTokenAndUUID(key, UUID, token)

	return
}

func CreateRootKeyObjectFromTokenAndUUID(key *models.Key, UUID strfmt.UUID, token strfmt.UUID) (hashedToken string) {
	// Do not set any parent

	// Set expiry to unlimited
	key.KeyExpiresUnix = -1

	// Get ips as v6
	var ips []string
	ifaces, _ := net.Interfaces()
	for _, i := range ifaces {
		addrs, _ := i.Addrs()
		for _, addr := range addrs {
			var ip net.IP
			switch v := addr.(type) {
			case *net.IPNet:
				ip = v.IP
			case *net.IPAddr:
				ip = v.IP
			}

			ipv6 := ip.To16()
			ips = append(ips, ipv6.String())
		}
	}

	key.IPOrigin = ips

	// Set chmod variables
	key.Read = true
	key.Write = true
	key.Delete = true
	key.Execute = true

	// Set Mail
	key.Email = "weaviate@weaviate.nl"

	// Print the key
	log.Println("INFO: No root key was found, a new root key is created. More info: https://github.com/creativesoftwarefdn/weaviate/blob/develop/README.md#authentication")
	log.Println("INFO: Auto set allowed IPs to: ", key.IPOrigin)
	log.Println("ROOTTOKEN=" + token)
	log.Println("ROOTKEY=" + string(UUID))

	hashedToken = TokenHasher(token)

	return
}

// TokenHasher is the function used to hash the UUID token
func TokenHasher(UUID strfmt.UUID) string {
	hashed, _ := bcrypt.GenerateFromPassword([]byte(UUID), bcrypt.DefaultCost)
	return string(hashed)
}

// TokenHashCompare is the function used to compare the hash with given UUID
func TokenHashCompare(hashed string, token strfmt.UUID) bool {
	err := bcrypt.CompareHashAndPassword([]byte(hashed), []byte(token))
	return err == nil
}

// Trace is used to display the running function in a connector
func Trace() {
	pc := make([]uintptr, 10) // at least 1 entry needed
	runtime.Callers(2, pc)
	f2 := runtime.FuncForPC(pc[0])
	//file, line := f2.FileLine(pc[0])
	fmt.Printf("THIS FUNCTION RUNS: %s\n", f2.Name())
}

// NowUnix returns the current Unix time
func NowUnix() int64 {
	return MakeUnixMillisecond(time.Now())
}

// MakeUnixMillisecond returns the millisecond unix-version of the given time
func MakeUnixMillisecond(t time.Time) int64 {
	return t.UnixNano() / int64(time.Millisecond)
}

// GenerateUUID returns a new UUID
func GenerateUUID() strfmt.UUID {

	// generate the uuid
	uuid, err := gouuid.NewV4()

	// panic, can't create uuid
	if err != nil {
		panic("PANIC: Can't create UUID")
	}

	// return the uuid and the error
	return strfmt.UUID(fmt.Sprintf("%v", uuid))
}

// Must panics if error, otherwise returns value
func Must(i interface{}, err error) interface{} {
	if err != nil {
		panic(err)
	}
	return i
}

// WhereStringToStruct is the 'compiler' for converting the filter/where query-string into a struct
func WhereStringToStruct(prop string, where string) (WhereQuery, error) {
	whereQuery := WhereQuery{}

	// Make a regex which can compile a string like 'firstName>=~John'
	re1, _ := regexp.Compile(`^([a-zA-Z0-9]*)([:<>!=]*)([~]*)([^~]*)$`)
	result := re1.FindStringSubmatch(where)

	// Set which property
	whereQuery.Property = prop
	if len(result[1]) > 1 && len(result[4]) != 0 {
		whereQuery.Property = fmt.Sprintf("%s.%s", prop, result[1])
	}

	// Set the operator
	switch result[2] {
	// When operator is "", put in 'Equal' as operator
	case ":", "", "=":
		whereQuery.Value.Operator = Equal
	case "!:", "!=":
		whereQuery.Value.Operator = NotEqual
	// TODO: https://github.com/creativesoftwarefdn/weaviate/issues/202
	// case ">":
	// 	whereQuery.Value.Operator = GreaterThan
	// case ">:", ">=":
	// 	whereQuery.Value.Operator = GreaterThanEqual
	// case "<":
	// 	whereQuery.Value.Operator = LessThan
	// case "<:", "<=":
	// 	whereQuery.Value.Operator = LessThanEqual
	default:
		return whereQuery, errors.New("invalid operator set in query")
	}

	// The wild cards
	// TODO: Wildcard search is disabled for now https://github.com/creativesoftwarefdn/weaviate/issues/202
	whereQuery.Value.Contains = false //result[3] == "~"

	// Set the value itself
	if len(result[4]) == 0 {
		if len(result[1]) > 0 && len(result[2]) == 0 && len(result[3]) == 0 {
			// If only result[1] is set, just use that as search term.
			whereQuery.Value.Value = result[1]
		} else {
			// When value is "", throw error
			return whereQuery, errors.New("no value is set in the query")
		}
	} else {
		whereQuery.Value.Value = result[4]
	}

	return whereQuery, nil
}

// DoExternalRequest does a request to an external Weaviate Instance based on given parameters
func DoExternalRequest(instance config.Instance, endpoint string, uuid strfmt.UUID) (response *http.Response, err error) {
	// Create the transport and HTTP client
	client := &http.Client{Transport: &http.Transport{
		MaxIdleConns:       10,
		IdleConnTimeout:    30 * time.Second,
		DisableCompression: true,
	}}

	// Create the request with basic headers
	req, _ := http.NewRequest("GET", fmt.Sprintf("%s/weaviate/v1/%s/%s", instance.URL, endpoint, uuid), nil)
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Accept", "application/json")
	req.Header.Set("X-API-KEY", instance.APIKey)
	req.Header.Set("X-API-TOKEN", instance.APIToken)

	// Do the request
	response, err = client.Do(req)

	if err != nil {
		return
	}

	// Check the status-code to determine existence
	if response.StatusCode != 200 {
		err = fmt.Errorf("status code is not 200, but %d with status '%s'", response.StatusCode, response.Status)
	}

	return
}

// ResolveExternalCrossRef resolves an object on an external instance using the given parameters and the Weaviate REST-API of the external instance
func ResolveExternalCrossRef(instance config.Instance, endpoint string, uuid strfmt.UUID, responseObject interface{}) (err error) {
	// Do the request
	response, err := DoExternalRequest(instance, endpoint, uuid)

	// Return error
	if err != nil {
		return
	}

	// Close the body on the end of the function
	defer response.Body.Close()

	// Read the body and fill the object with the data from the response
	body, _ := ioutil.ReadAll(response.Body)
	json.Unmarshal(body, responseObject)

	return
}
<|endoftext|>


<commit_before>package wxlsx

import (
	"fmt"
	"github.com/tealeg/xlsx"
)

// same as GetSheetByIndex
func GetSheet(file string, sheetIndex int) (*Sheet, error) {
	return GetSheetByIndex(file, sheetIndex)
}

// GetSheetByIndex
// index from 0
func GetSheetByIndex(file string, sheetIndex int) (*Sheet, error) {
	book, err := xlsx.OpenFile(file)
	if nil != err {
		return nil, err
	}
	sheetNums := len(book.Sheets)
	if sheetIndex > sheetNums {
		return nil, fmt.Errorf("Open %s index %d out of sheet index, max sheet index is %d", file, sheetIndex, sheetNums)
	}
	return NewSheet(book.Sheets[sheetIndex]), nil
}

// GetSheetByName
func GetSheetByName(file string, sheetName string) (*Sheet, error) {
	book, err := xlsx.OpenFile(file)
	if nil != err {
		return nil, err
	}

	sheetNums := len(book.Sheets)
	for sheetIndex := 0; sheetIndex < sheetNums; sheetIndex++ {
		curSheet := book.Sheets[sheetIndex]
		curName := curSheet.Name
		if curName == sheetName {
			return NewSheet(curSheet), nil
		}
	}
	//not found sheet
	return nil, fmt.Errorf("SheetName %s not found in %s.", sheetName, file)
}
<commit_msg>add func<commit_after>package wxlsx

import (
	"fmt"
	"github.com/tealeg/xlsx"
)

// same as GetSheetByIndex
func GetSheet(file string, sheetIndex int) (*Sheet, error) {
	return GetSheetByIndex(file, sheetIndex)
}

// GetSheetByIndex
// index from 0
func GetSheetByIndex(file string, sheetIndex int) (*Sheet, error) {
	book, err := xlsx.OpenFile(file)
	if nil != err {
		return nil, err
	}
	sheetNums := len(book.Sheets)
	if sheetIndex > sheetNums {
		return nil, fmt.Errorf("Open %s index %d out of sheet index, max sheet index is %d", file, sheetIndex, sheetNums)
	}
	return NewSheet(book.Sheets[sheetIndex]), nil
}

// GetSheetNameByIndex
func GetSheetNameByIndex(file string, index int) (string, error) {
	sheet, err := GetSheetByIndex(file, index)
	if err != nil {
		return "", err
	}
	return sheet.Name, nil
}

// GetSheetByName
func GetSheetByName(file string, sheetName string) (*Sheet, error) {
	book, err := xlsx.OpenFile(file)
	if nil != err {
		return nil, err
	}

	sheetNums := len(book.Sheets)
	for sheetIndex := 0; sheetIndex < sheetNums; sheetIndex++ {
		curSheet := book.Sheets[sheetIndex]
		curName := curSheet.Name
		if curName == sheetName {
			return NewSheet(curSheet), nil
		}
	}
	//not found sheet
	return nil, fmt.Errorf("SheetName %s not found in %s.", sheetName, file)
}
<|endoftext|>


<commit_before>// Copyright 2020 DSR Corporation
//
// 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 x509

import (
	"bytes"
	"crypto/x509"
	"encoding/hex"
	"encoding/pem"
	"fmt"
	"strings"

	"github.com/zigbee-alliance/distributed-compliance-ledger/x/pki/types"
)

type X509Certificate struct {
	Issuer         string
	SerialNumber   string
	Subject        string
	SubjectKeyID   string
	AuthorityKeyID string
	Certificate    *x509.Certificate
}

func DecodeX509Certificate(pemCertificate string) (*X509Certificate, error) {
	block, _ := pem.Decode([]byte(pemCertificate))
	if block == nil {
		return nil, types.NewErrInvalidCertificate("Could not decode pem certificate")
	}

	cert, err := x509.ParseCertificate(block.Bytes)
	if err != nil {
		return nil, types.NewErrInvalidCertificate(fmt.Sprintf("Could not parse certificate: %v", err.Error()))
	}

	oldVID := []byte("1.3.6.1.4.1.37244.2.1")
	oldPID := []byte("1.3.6.1.4.1.37244.2.2")

	newVIDKey := []byte("vid=0x")
	newPIDKey := []byte("pid=0x")

	issuer := cert.Issuer.String()
	issuer = formatOID([]byte(issuer), oldVID, newVIDKey)
	issuer = formatOID([]byte(issuer), oldPID, newPIDKey)

	subject := cert.Subject.String()
	subject = formatOID([]byte(subject), oldVID, newVIDKey)
	subject = formatOID([]byte(subject), oldPID, newPIDKey)

	certificate := X509Certificate{
		Issuer:         issuer,
		SerialNumber:   cert.SerialNumber.String(),
		Subject:        subject,
		SubjectKeyID:   BytesToHex(cert.SubjectKeyId),
		AuthorityKeyID: BytesToHex(cert.AuthorityKeyId),
		Certificate:    cert,
	}

	return &certificate, nil
}

func FormatOID(header, oldKey, newKey string) string {
	subjectValues := strings.Split(header, ",")

	for index, value := range subjectValues {
		if i := strings.Index(value, oldKey); i >= 0 {
		// get value from header
		value = value[len(value)-8:]

			decoded, _ := hex.DecodeString(value)
			hexStr := "=0x" + string(decoded)

			value = newKey + hexStr
			subjectValues[index] = value
		}
	}

	return strings.Join(subjectValues, ",")
}

func BytesToHex(bytes []byte) string {
	if bytes == nil {
		return ""
	}

	bytesHex := make([]string, len(bytes))
	for i, b := range bytes {
		bytesHex[i] = fmt.Sprintf("%X", b)
	}

	return strings.Join(bytesHex, ":")
}

func (c X509Certificate) Verify(parent *X509Certificate) error {
	roots := x509.NewCertPool()
	roots.AddCert(parent.Certificate)

	opts := x509.VerifyOptions{Roots: roots}

	if _, err := c.Certificate.Verify(opts); err != nil {
		return types.NewErrInvalidCertificate(fmt.Sprintf("Certificate verification failed. Error: %v", err))
	}

	return nil
}

func (c X509Certificate) IsSelfSigned() bool {
	if len(c.AuthorityKeyID) > 0 {
		return c.Issuer == c.Subject && c.AuthorityKeyID == c.SubjectKeyID
	} else {
		return c.Issuer == c.Subject
	}
}
<commit_msg>Helper function to patch certificate<commit_after>// Copyright 2020 DSR Corporation
//
// 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 x509

import (
	"crypto/x509"
	"encoding/hex"
	"encoding/pem"
	"fmt"
	"strings"

	"github.com/zigbee-alliance/distributed-compliance-ledger/x/pki/types"
)

type X509Certificate struct {
	Issuer         string
	SerialNumber   string
	Subject        string
	SubjectKeyID   string
	AuthorityKeyID string
	Certificate    *x509.Certificate
}

func DecodeX509Certificate(pemCertificate string) (*X509Certificate, error) {
	block, _ := pem.Decode([]byte(pemCertificate))
	if block == nil {
		return nil, types.NewErrInvalidCertificate("Could not decode pem certificate")
	}

	cert, err := x509.ParseCertificate(block.Bytes)
	if err != nil {
		return nil, types.NewErrInvalidCertificate(fmt.Sprintf("Could not parse certificate: %v", err.Error()))
	}

	certificate := X509Certificate{
		Issuer:         cert.Issuer.String(),
		SerialNumber:   cert.SerialNumber.String(),
		Subject:        cert.Subject.String(),
		SubjectKeyID:   BytesToHex(cert.SubjectKeyId),
		AuthorityKeyID: BytesToHex(cert.AuthorityKeyId),
		Certificate:    cert,
	}

	certificate = PatchCertificate(certificate)

	return &certificate, nil
}

func PatchCertificate(certificate X509Certificate) X509Certificate {
	oldVIDKey := "1.3.6.1.4.1.37244.2.1"
	oldPIDKey := "1.3.6.1.4.1.37244.2.2"

	newVIDKey := "vid"
	newPIDKey := "pid"

	issuer := certificate.Issuer
	issuer = FormatOID(issuer, oldVIDKey, newVIDKey)
	issuer = FormatOID(issuer, oldPIDKey, newPIDKey)

	subject := certificate.Subject
	subject = FormatOID(subject, oldVIDKey, newVIDKey)
	subject = FormatOID(subject, oldPIDKey, newPIDKey)

	certificate.Issuer = issuer
	certificate.Subject = subject

	return certificate
}

func FormatOID(header, oldKey, newKey string) string {
	subjectValues := strings.Split(header, ",")

	for index, value := range subjectValues {
		if i := strings.Index(value, oldKey); i >= 0 {
			// get value from header
			value = value[len(value)-8:]

			decoded, _ := hex.DecodeString(value)
			hexStr := "=0x" + string(decoded)

			value = newKey + hexStr
			subjectValues[index] = value
		}
	}

	return strings.Join(subjectValues, ",")
}

func BytesToHex(bytes []byte) string {
	if bytes == nil {
		return ""
	}

	bytesHex := make([]string, len(bytes))
	for i, b := range bytes {
		bytesHex[i] = fmt.Sprintf("%X", b)
	}

	return strings.Join(bytesHex, ":")
}

func (c X509Certificate) Verify(parent *X509Certificate) error {
	roots := x509.NewCertPool()
	roots.AddCert(parent.Certificate)

	opts := x509.VerifyOptions{Roots: roots}

	if _, err := c.Certificate.Verify(opts); err != nil {
		return types.NewErrInvalidCertificate(fmt.Sprintf("Certificate verification failed. Error: %v", err))
	}

	return nil
}

func (c X509Certificate) IsSelfSigned() bool {
	if len(c.AuthorityKeyID) > 0 {
		return c.Issuer == c.Subject && c.AuthorityKeyID == c.SubjectKeyID
	} else {
		return c.Issuer == c.Subject
	}
}
<|endoftext|>


<commit_before>// +build OMIT

package main

import (
	"fmt"
	"sync"
	"time"
)

// SafeCounter is safe to use concurrently.
type SafeCounter struct {
	v   map[string]int
	mux sync.Mutex
}

// Inc increments the counter for the given key.
func (c *SafeCounter) Inc(key string) {
	c.mux.Lock()
	// Lock so only one goroutine at a time can access the map c.v.
	c.v[key]++
	c.mux.Unlock()
}

// Value returns the current value of the counter for the given key.
func (c *SafeCounter) Value(key string) int {
	c.mux.Lock()
	// Lock so only one goroutine at a time can access the map c.v.
	defer c.mux.Unlock()
	return c.v[key]
}

func main() {
	c := SafeCounter{v: make(map[string]int)}
	for i := 0; i < 1000; i++ {
		go c.Inc("somekey")
	}

	time.Sleep(time.Second)
	fmt.Println(c.Value("somekey"))
}
<commit_msg>tour: Rename mux -> mu to follow convention<commit_after>// +build OMIT

package main

import (
	"fmt"
	"sync"
	"time"
)

// SafeCounter is safe to use concurrently.
type SafeCounter struct {
	mu sync.Mutex
	v  map[string]int
}

// Inc increments the counter for the given key.
func (c *SafeCounter) Inc(key string) {
	c.mu.Lock()
	// Lock so only one goroutine at a time can access the map c.v.
	c.v[key]++
	c.mu.Unlock()
}

// Value returns the current value of the counter for the given key.
func (c *SafeCounter) Value(key string) int {
	c.mu.Lock()
	// Lock so only one goroutine at a time can access the map c.v.
	defer c.mu.Unlock()
	return c.v[key]
}

func main() {
	c := SafeCounter{v: make(map[string]int)}
	for i := 0; i < 1000; i++ {
		go c.Inc("somekey")
	}

	time.Sleep(time.Second)
	fmt.Println(c.Value("somekey"))
}
<|endoftext|>


<commit_before>package agent

import (
	"testing"

	"github.com/hashicorp/go-syslog"
	"github.com/hashicorp/logutils"
)

func TestSyslogFilter(t *testing.T) {
	l, err := gsyslog.NewLogger(gsyslog.LOG_NOTICE, "LOCAL0", "consul")
	if err != nil {
		t.Fatalf("err: %s", err)
	}

	filt := LevelFilter()
	filt.MinLevel = logutils.LogLevel("INFO")

	s := &SyslogWrapper{l, filt}
	n, err := s.Write([]byte("[INFO] test"))
	if err != nil {
		t.Fatalf("err: %s", err)
	}
	if n == 0 {
		t.Fatalf("should have logged")
	}

	n, err = s.Write([]byte("[DEBUG] test"))
	if err != nil {
		t.Fatalf("err: %s", err)
	}
	if n != 0 {
		t.Fatalf("should not have logged")
	}
}
<commit_msg>agent: skip syslog test on windows<commit_after>package agent

import (
	"runtime"
	"testing"

	"github.com/hashicorp/go-syslog"
	"github.com/hashicorp/logutils"
)

func TestSyslogFilter(t *testing.T) {
	if runtime.GOOS == "windows" {
		t.SkipNow()
	}
	l, err := gsyslog.NewLogger(gsyslog.LOG_NOTICE, "LOCAL0", "consul")
	if err != nil {
		t.Fatalf("err: %s", err)
	}

	filt := LevelFilter()
	filt.MinLevel = logutils.LogLevel("INFO")

	s := &SyslogWrapper{l, filt}
	n, err := s.Write([]byte("[INFO] test"))
	if err != nil {
		t.Fatalf("err: %s", err)
	}
	if n == 0 {
		t.Fatalf("should have logged")
	}

	n, err = s.Write([]byte("[DEBUG] test"))
	if err != nil {
		t.Fatalf("err: %s", err)
	}
	if n != 0 {
		t.Fatalf("should not have logged")
	}
}
<|endoftext|>