<commit_before>// Copyright 2015 The Serviced Authors.
// 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.

// +build unit

package utils

import (
	"strconv"
	"strings"
)

type TestConfigReader map[string]string

func (r TestConfigReader) StringVal(name string, dflt string) string {
	if val, _ := r[name]; val != "" {
		return val
	} else {
		return dflt
	}
}

func (r TestConfigReader) StringSlice(name string, dflt []string) []string {
	if val, _ := r[name]; val != "" {
		return strings.Split(val, ",")
	}
	return dflt
}

func (r TestConfigReader) StringNumberedList(name string, dflt []string) []string {
    values := ""
    i := 0
    for {
        if strval, ok := r[name + "_" + strconv.Itoa(i)]; ok {
            if values == "" {
                values = strval
            } else {
                values += "," + strval
            }
        } else {
            if values == "" {
                r[name] = strings.Join(dflt, ",")
                return dflt
            } else {
                r[name] = values
                return strings.Split(values, ",")
            }
        }

        i += 1
    }
}

func (r TestConfigReader) IntVal(name string, dflt int) int {
	if val, _ := r[name]; val != "" {
		if intval, err := strconv.Atoi(val); err != nil {
			return intval
		}
	}
	return dflt
}

func (r TestConfigReader) BoolVal(name string, dflt bool) bool {
	if val, _ := r[name]; val != "" {
		val = strings.ToLower(val)

		trues := []string{"1", "true", "t", "yes"}
		for _, t := range trues {
			if val == t {
				return true
			}
		}

		falses := []string{"0", "false", "f", "no"}
		for _, f := range falses {
			if val == f {
				return false
			}
		}
	}
	return dflt
}

func (p TestConfigReader) GetConfigValues() map[string]ConfigValue {
	return map[string]ConfigValue{}
}

func (p TestConfigReader) Float64Val(name string, dflt float64) float64 {
	if val, _ := r[name]; val != "" {
		if floatval, err := strconv.ParseFloat(val, 64); err != nil {
			return floatval
		}
	}
	return dflt
}

<commit_msg>fix the fix for the fix<commit_after>// Copyright 2015 The Serviced Authors.
// 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.

// +build unit

package utils

import (
	"strconv"
	"strings"
)

type TestConfigReader map[string]string

func (r TestConfigReader) StringVal(name string, dflt string) string {
	if val, _ := r[name]; val != "" {
		return val
	} else {
		return dflt
	}
}

func (r TestConfigReader) StringSlice(name string, dflt []string) []string {
	if val, _ := r[name]; val != "" {
		return strings.Split(val, ",")
	}
	return dflt
}

func (r TestConfigReader) StringNumberedList(name string, dflt []string) []string {
    values := ""
    i := 0
    for {
        if strval, ok := r[name + "_" + strconv.Itoa(i)]; ok {
            if values == "" {
                values = strval
            } else {
                values += "," + strval
            }
        } else {
            if values == "" {
                r[name] = strings.Join(dflt, ",")
                return dflt
            } else {
                r[name] = values
                return strings.Split(values, ",")
            }
        }

        i += 1
    }
}

func (r TestConfigReader) IntVal(name string, dflt int) int {
	if val, _ := r[name]; val != "" {
		if intval, err := strconv.Atoi(val); err != nil {
			return intval
		}
	}
	return dflt
}

func (r TestConfigReader) BoolVal(name string, dflt bool) bool {
	if val, _ := r[name]; val != "" {
		val = strings.ToLower(val)

		trues := []string{"1", "true", "t", "yes"}
		for _, t := range trues {
			if val == t {
				return true
			}
		}

		falses := []string{"0", "false", "f", "no"}
		for _, f := range falses {
			if val == f {
				return false
			}
		}
	}
	return dflt
}

func (p TestConfigReader) GetConfigValues() map[string]ConfigValue {
	return map[string]ConfigValue{}
}

func (r TestConfigReader) Float64Val(name string, dflt float64) float64 {
	if val, _ := r[name]; val != "" {
		if floatval, err := strconv.ParseFloat(val, 64); err != nil {
			return floatval
		}
	}
	return dflt
}

<|endoftext|>


<commit_before>/*
 * Datadog API for Go
 *
 * Please see the included LICENSE file for licensing information.
 *
 * Copyright 2018 by authors and contributors.
 */

package datadog

import (
	"fmt"
)

const (
	DashboardListItemCustomTimeboard        = "custom_timeboard"
	DashboardListItemCustomScreenboard      = "custom_screenboard"
	DashboardListItemIntegerationTimeboard  = "integration_timeboard"
	DashboardListItemIntegrationScreenboard = "integration_screenboard"
	DashboardListItemHostTimeboard          = "host_timeboard"
)

// DashboardList represents a dashboard list.
// struct when we load the summaries.
type DashboardList struct {
	Id             *int    `json:"id,omitempty"`
	Name           *string `json:"name,omitempty"`
	DashboardCount *int    `json:"dashboard_count,omitempty"`
}

type DashboardListItem struct {
	Id   *int    `json:"id,omitempty"`
	Type *string `json:"type,omitempty"`
}

type reqDashboardListItems struct {
	Dashboards []DashboardListItem `json:"dashboards,omitempty"`
}

type reqAddedDashboardListItems struct {
	Dashboards []DashboardListItem `json:"added_dashboards_to_list,omitempty"`
}

type reqDeletedDashboardListItems struct {
	Dashboards []DashboardListItem `json:"deleted_dashboards_from_list,omitempty"`
}

type reqUpdateDashboardList struct {
	Name string `json:"name,omitempty"`
}

type reqGetDashboardLists struct {
	DashboardLists []DashboardList `json:"dashboard_lists,omitempty"`
}

// GetDashboardList returns a single dashboard list created on this account.
func (client *Client) GetDashboardList(id int) (*DashboardList, error) {
	var out DashboardList
	if err := client.doJsonRequest("GET", fmt.Sprintf("/v1/dashboard/lists/manual/%d", id), nil, &out); err != nil {
		return nil, err
	}
	return &out, nil
}

// GetDashboardLists returns a list of all dashboard lists created on this account.
func (client *Client) GetDashboardLists() ([]DashboardList, error) {
	var out reqGetDashboardLists
	if err := client.doJsonRequest("GET", "/v1/dashboard/lists/manual", nil, &out); err != nil {
		return nil, err
	}
	return out.DashboardLists, nil
}

// CreateDashboardList returns a single dashboard list created on this account.
func (client *Client) CreateDashboardList(list *DashboardList) (*DashboardList, error) {
	var out DashboardList
	if err := client.doJsonRequest("POST", "/v1/dashboard/lists/manual", list, &out); err != nil {
		return nil, err
	}
	return &out, nil
}

// UpdateDashboardList returns a single dashboard list created on this account.
func (client *Client) UpdateDashboardList(list *DashboardList) error {
	req := reqUpdateDashboardList{list.GetName()}
	return client.doJsonRequest("PUT", fmt.Sprintf("/v1/dashboard/lists/manual/%d", *list.Id), req, nil)
}

// DeleteDashboardList deletes a dashboard list by the identifier.
func (client *Client) DeleteDashboardList(id int) error {
	return client.doJsonRequest("DELETE", fmt.Sprintf("/v1/dashboard/lists/manual/%d", id), nil, nil)
}

// GetDashboardListItems fetches the dashboard list's dashboard definitions.
func (client *Client) GetDashboardListItems(id int) ([]DashboardListItem, error) {
	var out reqDashboardListItems
	if err := client.doJsonRequest("GET", fmt.Sprintf("/v1/dashboard/lists/manual/%d/dashboards", id), nil, &out); err != nil {
		return nil, err
	}
	return out.Dashboards, nil
}

// AddDashboardListItems adds dashboards to an existing dashboard list.
//
// Any items already in the list are ignored (not added twice).
func (client *Client) AddDashboardListItems(dashboardListId int, items []DashboardListItem) ([]DashboardListItem, error) {
	req := reqDashboardListItems{items}
	var out reqAddedDashboardListItems
	if err := client.doJsonRequest("POST", fmt.Sprintf("/v1/dashboard/lists/manual/%d/dashboards", dashboardListId), req, &out); err != nil {
		return nil, err
	}
	return out.Dashboards, nil
}

// UpdateDashboardListItems updates dashboards of an existing dashboard list.
//
// This will set the list of dashboards to contain only the items in items.
func (client *Client) UpdateDashboardListItems(dashboardListId int, items []DashboardListItem) ([]DashboardListItem, error) {
	req := reqDashboardListItems{items}
	var out reqDashboardListItems
	if err := client.doJsonRequest("PUT", fmt.Sprintf("/v1/dashboard/lists/manual/%d/dashboards", dashboardListId), req, &out); err != nil {
		return nil, err
	}
	return out.Dashboards, nil
}

// DeleteDashboardListItems deletes dashboards from an existing dashboard list.
//
// Deletes any dashboards in the list of items from the dashboard list.
func (client *Client) DeleteDashboardListItems(dashboardListId int, items []DashboardListItem) ([]DashboardListItem, error) {
	req := reqDashboardListItems{items}
	var out reqDeletedDashboardListItems
	if err := client.doJsonRequest("DELETE", fmt.Sprintf("/v1/dashboard/lists/manual/%d/dashboards", dashboardListId), req, &out); err != nil {
		return nil, err
	}
	return out.Dashboards, nil
}
<commit_msg>Clean up docstrings for dashboard lists<commit_after>/*
 * Datadog API for Go
 *
 * Please see the included LICENSE file for licensing information.
 *
 * Copyright 2018 by authors and contributors.
 */

package datadog

import (
	"fmt"
)

const (
	DashboardListItemCustomTimeboard        = "custom_timeboard"
	DashboardListItemCustomScreenboard      = "custom_screenboard"
	DashboardListItemIntegerationTimeboard  = "integration_timeboard"
	DashboardListItemIntegrationScreenboard = "integration_screenboard"
	DashboardListItemHostTimeboard          = "host_timeboard"
)

// DashboardList represents a dashboard list.
type DashboardList struct {
	Id             *int    `json:"id,omitempty"`
	Name           *string `json:"name,omitempty"`
	DashboardCount *int    `json:"dashboard_count,omitempty"`
}

// DashboardListItem represents a single dashboard in a dashboard list.
type DashboardListItem struct {
	Id   *int    `json:"id,omitempty"`
	Type *string `json:"type,omitempty"`
}

type reqDashboardListItems struct {
	Dashboards []DashboardListItem `json:"dashboards,omitempty"`
}

type reqAddedDashboardListItems struct {
	Dashboards []DashboardListItem `json:"added_dashboards_to_list,omitempty"`
}

type reqDeletedDashboardListItems struct {
	Dashboards []DashboardListItem `json:"deleted_dashboards_from_list,omitempty"`
}

type reqUpdateDashboardList struct {
	Name string `json:"name,omitempty"`
}

type reqGetDashboardLists struct {
	DashboardLists []DashboardList `json:"dashboard_lists,omitempty"`
}

// GetDashboardList returns a single dashboard list created on this account.
func (client *Client) GetDashboardList(id int) (*DashboardList, error) {
	var out DashboardList
	if err := client.doJsonRequest("GET", fmt.Sprintf("/v1/dashboard/lists/manual/%d", id), nil, &out); err != nil {
		return nil, err
	}
	return &out, nil
}

// GetDashboardLists returns a list of all dashboard lists created on this account.
func (client *Client) GetDashboardLists() ([]DashboardList, error) {
	var out reqGetDashboardLists
	if err := client.doJsonRequest("GET", "/v1/dashboard/lists/manual", nil, &out); err != nil {
		return nil, err
	}
	return out.DashboardLists, nil
}

// CreateDashboardList returns a single dashboard list created on this account.
func (client *Client) CreateDashboardList(list *DashboardList) (*DashboardList, error) {
	var out DashboardList
	if err := client.doJsonRequest("POST", "/v1/dashboard/lists/manual", list, &out); err != nil {
		return nil, err
	}
	return &out, nil
}

// UpdateDashboardList returns a single dashboard list created on this account.
func (client *Client) UpdateDashboardList(list *DashboardList) error {
	req := reqUpdateDashboardList{list.GetName()}
	return client.doJsonRequest("PUT", fmt.Sprintf("/v1/dashboard/lists/manual/%d", *list.Id), req, nil)
}

// DeleteDashboardList deletes a dashboard list by the identifier.
func (client *Client) DeleteDashboardList(id int) error {
	return client.doJsonRequest("DELETE", fmt.Sprintf("/v1/dashboard/lists/manual/%d", id), nil, nil)
}

// GetDashboardListItems fetches the dashboard list's dashboard definitions.
func (client *Client) GetDashboardListItems(id int) ([]DashboardListItem, error) {
	var out reqDashboardListItems
	if err := client.doJsonRequest("GET", fmt.Sprintf("/v1/dashboard/lists/manual/%d/dashboards", id), nil, &out); err != nil {
		return nil, err
	}
	return out.Dashboards, nil
}

// AddDashboardListItems adds dashboards to an existing dashboard list.
//
// Any items already in the list are ignored (not added twice).
func (client *Client) AddDashboardListItems(dashboardListId int, items []DashboardListItem) ([]DashboardListItem, error) {
	req := reqDashboardListItems{items}
	var out reqAddedDashboardListItems
	if err := client.doJsonRequest("POST", fmt.Sprintf("/v1/dashboard/lists/manual/%d/dashboards", dashboardListId), req, &out); err != nil {
		return nil, err
	}
	return out.Dashboards, nil
}

// UpdateDashboardListItems updates dashboards of an existing dashboard list.
//
// This will set the list of dashboards to contain only the items in items.
func (client *Client) UpdateDashboardListItems(dashboardListId int, items []DashboardListItem) ([]DashboardListItem, error) {
	req := reqDashboardListItems{items}
	var out reqDashboardListItems
	if err := client.doJsonRequest("PUT", fmt.Sprintf("/v1/dashboard/lists/manual/%d/dashboards", dashboardListId), req, &out); err != nil {
		return nil, err
	}
	return out.Dashboards, nil
}

// DeleteDashboardListItems deletes dashboards from an existing dashboard list.
//
// Deletes any dashboards in the list of items from the dashboard list.
func (client *Client) DeleteDashboardListItems(dashboardListId int, items []DashboardListItem) ([]DashboardListItem, error) {
	req := reqDashboardListItems{items}
	var out reqDeletedDashboardListItems
	if err := client.doJsonRequest("DELETE", fmt.Sprintf("/v1/dashboard/lists/manual/%d/dashboards", dashboardListId), req, &out); err != nil {
		return nil, err
	}
	return out.Dashboards, nil
}
<|endoftext|>


<commit_before>package gokeepasslib

import (
	"crypto/aes"
	"crypto/cipher"
	"crypto/sha256"
	"errors"
	"fmt"
)

// ErrUnsupportedStreamType is retured if no streamManager can be created
// due to an unsupported InnerRandomStreamID value
var ErrUnsupportedStreamType = errors.New("Type of stream manager unsupported")

type ErrRequiredAttributeMissing string

func (e ErrRequiredAttributeMissing) Error() string {
	return fmt.Sprintf("gokeepasslib: operation can not be performed if database does not have %s", e)
}

// Database stores all contents nessesary for a keepass database file
type Database struct {
	Signature   *FileSignature
	Headers     *FileHeaders
	Credentials *DBCredentials
	Content     *DBContent
}

// NewDatabase creates a new database with some sensable default settings. To create a database with no settigns per-set, use gokeepasslib.Database{}
func NewDatabase() *Database {
	return &Database{
		Signature:   &DefaultSig,
		Headers:     NewFileHeaders(),
		Credentials: new(DBCredentials),
		Content:     NewDBContent(),
	}
}

func (db *Database) String() string {
	return fmt.Sprintf("Database:\nSignature: %s\n"+
		"Headers: %s\nCredentials: %s\nContent:\n%+v\n",
		db.Signature,
		db.Headers,
		db.Credentials,
		db.Content,
	)
}

// StreamManager returns a ProtectedStreamManager bassed on the db headers, or nil if the type is unsupported
// Can be used to lock only certain entries instead of calling
func (db *Database) StreamManager() ProtectedStreamManager {
	if db.Headers == nil {
		return nil
	}
	switch db.Headers.InnerRandomStreamID {
	case NoStreamID:
		return new(InsecureStreamManager)
	case SalsaStreamID:
		key := sha256.Sum256(db.Headers.ProtectedStreamKey)
		return NewSalsaManager(key)
	default:
		return nil
	}
}

// UnlockProtectedEntries goes through the entire database and encrypts
// any Values in entries with protected=true set.
// This should be called after decoding if you want to view plaintext password in an entry
// Warning: If you call this when entry values are already unlocked, it will cause them to be unreadable
func (db *Database) UnlockProtectedEntries() error {
	manager := db.StreamManager()
	if manager == nil {
		return ErrUnsupportedStreamType
	}
	UnlockProtectedGroups(manager, db.Content.Root.Groups)
	return nil
}

// LockProtectedEntries goes through the entire database and decrypts
// any Values in entries with protected=true set.
// Warning: Do not call this if entries are already locked
// Warning: Encoding a database calls LockProtectedEntries automatically
func (db *Database) LockProtectedEntries() error {
	manager := db.StreamManager()
	if manager == nil {
		return ErrUnsupportedStreamType
	}
	LockProtectedGroups(manager, db.Content.Root.Groups)
	return nil
}

func (db *Database) Decrypter() (cipher.BlockMode, error) {
	block, err := db.Cipher()
	if err != nil {
		return nil, err
	}
	return cipher.NewCBCDecrypter(block, db.Headers.EncryptionIV), nil
}
func (db *Database) Encrypter() (cipher.BlockMode, error) {
	if db.Headers == nil {
		return nil, ErrRequiredAttributeMissing("Headers")
	}
	if db.Headers.EncryptionIV == nil {
		return nil, ErrRequiredAttributeMissing("Headers.EncryptionIV")
	}
	block, err := db.Cipher()
	if err != nil {
		return nil, err
	}
	//Encrypts block data using AES block with initialization vector from header
	return cipher.NewCBCEncrypter(block, db.Headers.EncryptionIV), nil
}
func (db *Database) Cipher() (cipher.Block, error) {
	if db.Credentials == nil {
		return nil, ErrRequiredAttributeMissing("Credentials")
	}
	masterKey, err := db.Credentials.buildMasterKey(db)
	if err != nil {
		return nil, err
	}
	return aes.NewCipher(masterKey)
}
<commit_msg>added some comments<commit_after>package gokeepasslib

import (
	"crypto/aes"
	"crypto/cipher"
	"crypto/sha256"
	"errors"
	"fmt"
)

// ErrUnsupportedStreamType is retured if no streamManager can be created
// due to an unsupported InnerRandomStreamID value
var ErrUnsupportedStreamType = errors.New("Type of stream manager unsupported")

// ErrRequiredAttributeMissing is returned if a required value is not given
type ErrRequiredAttributeMissing string

func (e ErrRequiredAttributeMissing) Error() string {
	return fmt.Sprintf("gokeepasslib: operation can not be performed if database does not have %s", e)
}

// Database stores all contents nessesary for a keepass database file
type Database struct {
	Signature   *FileSignature
	Headers     *FileHeaders
	Credentials *DBCredentials
	Content     *DBContent
}

// NewDatabase creates a new database with some sensable default settings. To create a database with no settigns per-set, use gokeepasslib.Database{}
func NewDatabase() *Database {
	return &Database{
		Signature:   &DefaultSig,
		Headers:     NewFileHeaders(),
		Credentials: new(DBCredentials),
		Content:     NewDBContent(),
	}
}

func (db *Database) String() string {
	return fmt.Sprintf("Database:\nSignature: %s\n"+
		"Headers: %s\nCredentials: %s\nContent:\n%+v\n",
		db.Signature,
		db.Headers,
		db.Credentials,
		db.Content,
	)
}

// StreamManager returns a ProtectedStreamManager bassed on the db headers, or nil if the type is unsupported
// Can be used to lock only certain entries instead of calling
func (db *Database) StreamManager() ProtectedStreamManager {
	if db.Headers == nil {
		return nil
	}
	switch db.Headers.InnerRandomStreamID {
	case NoStreamID:
		return new(InsecureStreamManager)
	case SalsaStreamID:
		key := sha256.Sum256(db.Headers.ProtectedStreamKey)
		return NewSalsaManager(key)
	default:
		return nil
	}
}

// UnlockProtectedEntries goes through the entire database and encrypts
// any Values in entries with protected=true set.
// This should be called after decoding if you want to view plaintext password in an entry
// Warning: If you call this when entry values are already unlocked, it will cause them to be unreadable
func (db *Database) UnlockProtectedEntries() error {
	manager := db.StreamManager()
	if manager == nil {
		return ErrUnsupportedStreamType
	}
	UnlockProtectedGroups(manager, db.Content.Root.Groups)
	return nil
}

// LockProtectedEntries goes through the entire database and decrypts
// any Values in entries with protected=true set.
// Warning: Do not call this if entries are already locked
// Warning: Encoding a database calls LockProtectedEntries automatically
func (db *Database) LockProtectedEntries() error {
	manager := db.StreamManager()
	if manager == nil {
		return ErrUnsupportedStreamType
	}
	LockProtectedGroups(manager, db.Content.Root.Groups)
	return nil
}

// Decrypter initializes a CBC decrypter for the database
func (db *Database) Decrypter() (cipher.BlockMode, error) {
	block, err := db.Cipher()
	if err != nil {
		return nil, err
	}
	return cipher.NewCBCDecrypter(block, db.Headers.EncryptionIV), nil
}

// Encrypter initializes a CBC encrypter for the database
func (db *Database) Encrypter() (cipher.BlockMode, error) {
	if db.Headers == nil {
		return nil, ErrRequiredAttributeMissing("Headers")
	}
	if db.Headers.EncryptionIV == nil {
		return nil, ErrRequiredAttributeMissing("Headers.EncryptionIV")
	}
	block, err := db.Cipher()
	if err != nil {
		return nil, err
	}
	//Encrypts block data using AES block with initialization vector from header
	return cipher.NewCBCEncrypter(block, db.Headers.EncryptionIV), nil
}

// Cipher returns a new aes cipher initialized with the master key
func (db *Database) Cipher() (cipher.Block, error) {
	if db.Credentials == nil {
		return nil, ErrRequiredAttributeMissing("Credentials")
	}
	masterKey, err := db.Credentials.buildMasterKey(db)
	if err != nil {
		return nil, err
	}
	return aes.NewCipher(masterKey)
}
<|endoftext|>


<commit_before>package gopaste

import (
	"database/sql"
	"fmt"
	_ "github.com/mattn/go-sqlite3"
)

const createTableSql = `
	CREATE TABLE IF NOT EXISTS pastes (
		id         INTEGER NOT NULL PRIMARY KEY,
		title      TEXT,
		content    TEXT NOT NULL,
		author     TEXT,
		language   TEXT,
		channel    TEXT,
		annotates  INTEGER,
		private    INTEGER NOT NULL,
		created    TEXT NOT NULL
	);
`

type languageName struct {
	Name string
	Code string
}

// LanguageNamesSorted is a list of known languages, sorted by name.
var LanguageNamesSorted = []languageName{
	{"1C", "1c"},
	{"ActionScript", "actionscript"},
	{"Apache", "apache"},
	{"AppleScript", "applescript"},
	{"AsciiDoc", "asciidoc"},
	{"AutoHotkey", "autohotkey"},
	{"AVR assembler", "avrasm"},
	{"Axapta", "axapta"},
	{"Bash", "bash"},
	{"Brainfuck", "brainfuck"},
	{"C", "c"},
	{"C#", "cs"},
	{"C++", "cpp"},
	{"Cap'n Proto", "capnproto"},
	{"Clojure", "clojure"},
	{"CMake", "cmake"},
	{"CoffeeScript", "coffeescript"},
	{"CSS", "css"},
	{"D", "d"},
	{"Dart", "dart"},
	{"Delphi", "delphi"},
	{"Diff", "diff"},
	{"Django", "django"},
	{"DOS .bat", "dos"},
	{"Dust", "dust"},
	{"Elixir", "elixir"},
	{"Erlang", "erlang"},
	{"F#", "fsharp"},
	{"FIX", "fix"},
	{"G-Code", "gcode"},
	{"Gherkin", "gherkin"},
	{"Go", "go"},
	{"Gradle", "gradle"},
	{"Groovy", "groovy"},
	{"Haml", "haml"},
	{"Handlebars", "handlebars"},
	{"Haskell", "haskell"},
	{"Haxe", "haxe"},
	{"HTML", "html"},
	{"HTTP", "http"},
	{"INI", "ini"},
	{"Java", "java"},
	{"JavaScript", "javascript"},
	{"JSON", "json"},
	{"Lasso", "lasso"},
	{"Lisp", "lisp"},
	{"LiveCode Server", "livecodeserver"},
	{"Lua", "lua"},
	{"Makefile", "makefile"},
	{"Markdown", "markdown"},
	{"Mathematica", "mathematica"},
	{"Matlab", "matlab"},
	{"Maya Embedded Language", "mel"},
	{"Mizar", "mizar"},
	{"Monkey", "monkey"},
	{"Nginx", "nginx"},
	{"Nimrod", "nimrod"},
	{"Nix", "nix"},
	{"NSIS", "nsis"},
	{"Objective C", "objectivec"},
	{"OCaml", "ocaml"},
	{"OpenGL Shading Language", "glsl"},
	{"Oracle Rules Language", "ruleslanguage"},
	{"Oxygene", "oxygene"},
	{"Parser3", "parser3"},
	{"Perl", "perl"},
	{"PHP", "php"},
	{"Protocol Buffers", "protobuf"},
	{"Python", "python"},
	{"Python profiler", "profile"},
	{"Q", "q"},
	{"R", "r"},
	{"RenderMan RIB", "rib"},
	{"RenderMan RSL", "rsl"},
	{"Ruby", "ruby"},
	{"Rust", "rust"},
	{"Scala", "scala"},
	{"Scheme", "scheme"},
	{"Scilab", "scilab"},
	{"SCSS", "scss"},
	{"Smalltalk", "smalltalk"},
	{"SQL", "sql"},
	{"Swift", "swift"},
	{"TeX", "tex"},
	{"Thrift", "thrift"},
	{"TypeScript", "typescript"},
	{"Vala", "vala"},
	{"VB.Net", "vbnet"},
	{"VBScript", "vbscript"},
	{"VHDL", "vhdl"},
	{"Vim Script", "vim"},
	{"x86 Assembly", "x86asm"},
	{"XML", "xml"},
}

// LanguageNames maps language identifers to the human-readable names of the
// languages supported by highlightjs.
var LanguageNames = map[string]string{}

func init() {
	for _, v := range LanguageNamesSorted {
		LanguageNames[v.Code] = v.Name
	}
}

// initDb establishes Gopaste's database connection and creates the pastes table
// if necessary.
func (s *Server) initDb() error {
	dbh, err := sql.Open(s.Config.DbDriver, s.Config.DbSource)
	if err != nil {
		return fmt.Errorf("Error opening %s %s: %v\n", s.Config.DbDriver, s.Config.DbSource, err)
	}

	if _, err := dbh.Exec(createTableSql); err != nil {
		return err
	}

	s.Database = dbh
	return nil
}
<commit_msg>Actually, do that the other way around<commit_after>package gopaste

import (
	"database/sql"
	"fmt"
	_ "github.com/mattn/go-sqlite3"
	"sort"
	"strings"
)

const createTableSql = `
	CREATE TABLE IF NOT EXISTS pastes (
		id         INTEGER NOT NULL PRIMARY KEY,
		title      TEXT,
		content    TEXT NOT NULL,
		author     TEXT,
		language   TEXT,
		channel    TEXT,
		annotates  INTEGER,
		private    INTEGER NOT NULL,
		created    TEXT NOT NULL
	);
`

// LanguageNames maps language identifers to the human-readable names of the
// languages supported by highlightjs.
var LanguageNames = map[string]string{
	"1c":             "1C",
	"actionscript":   "ActionScript",
	"apache":         "Apache",
	"applescript":    "AppleScript",
	"asciidoc":       "AsciiDoc",
	"autohotkey":     "AutoHotkey",
	"avrasm":         "AVR assembler",
	"axapta":         "Axapta",
	"bash":           "Bash",
	"brainfuck":      "Brainfuck",
	"c":              "C",
	"capnproto":      "Cap'n Proto",
	"clojure":        "Clojure",
	"cmake":          "CMake",
	"coffeescript":   "CoffeeScript",
	"cpp":            "C++",
	"cs":             "C#",
	"css":            "CSS",
	"d":              "D",
	"dart":           "Dart",
	"delphi":         "Delphi",
	"diff":           "Diff",
	"django":         "Django",
	"dos":            "DOS .bat",
	"dust":           "Dust",
	"elixir":         "Elixir",
	"erlang":         "Erlang",
	"fix":            "FIX",
	"fsharp":         "F#",
	"gcode":          "G-Code",
	"gherkin":        "Gherkin",
	"glsl":           "OpenGL Shading Language",
	"go":             "Go",
	"gradle":         "Gradle",
	"groovy":         "Groovy",
	"haml":           "Haml",
	"handlebars":     "Handlebars",
	"haskell":        "Haskell",
	"haxe":           "Haxe",
	"html":           "HTML",
	"http":           "HTTP",
	"ini":            "INI",
	"java":           "Java",
	"javascript":     "JavaScript",
	"json":           "JSON",
	"lasso":          "Lasso",
	"lisp":           "Lisp",
	"livecodeserver": "LiveCode Server",
	"lua":            "Lua",
	"makefile":       "Makefile",
	"markdown":       "Markdown",
	"mathematica":    "Mathematica",
	"matlab":         "Matlab",
	"mel":            "Maya Embedded Language",
	"mizar":          "Mizar",
	"monkey":         "Monkey",
	"nginx":          "Nginx",
	"nimrod":         "Nimrod",
	"nix":            "Nix",
	"nsis":           "NSIS",
	"objectivec":     "Objective C",
	"ocaml":          "OCaml",
	"oxygene":        "Oxygene",
	"parser3":        "Parser3",
	"perl":           "Perl",
	"php":            "PHP",
	"profile":        "Python profiler",
	"protobuf":       "Protocol Buffers",
	"python":         "Python",
	"q":              "Q",
	"r":              "R",
	"rib":            "RenderMan RIB",
	"rsl":            "RenderMan RSL",
	"ruby":           "Ruby",
	"ruleslanguage":  "Oracle Rules Language",
	"rust":           "Rust",
	"scala":          "Scala",
	"scheme":         "Scheme",
	"scilab":         "Scilab",
	"scss":           "SCSS",
	"smalltalk":      "Smalltalk",
	"sql":            "SQL",
	"swift":          "Swift",
	"tex":            "TeX",
	"thrift":         "Thrift",
	"typescript":     "TypeScript",
	"vala":           "Vala",
	"vbnet":          "VB.Net",
	"vbscript":       "VBScript",
	"vhdl":           "VHDL",
	"vim":            "Vim Script",
	"x86asm":         "x86 Assembly",
	"xml":            "XML",
}

type languageName struct {
	Name string
	Code string
}

// LanguageNamesSorted is a list of known languages, sorted by name.
var LanguageNamesSorted = []languageName{}

type byName []languageName

func (a byName) Len() int      { return len(a) }
func (a byName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byName) Less(i, j int) bool {
	return strings.ToUpper(a[i].Name) < strings.ToUpper(a[j].Name)
}

func init() {
	for c, n := range LanguageNames {
		LanguageNamesSorted = append(LanguageNamesSorted, languageName{Code: c, Name: n})
	}
	sort.Sort(byName(LanguageNamesSorted))
}

// initDb establishes Gopaste's database connection and creates the pastes table
// if necessary.
func (s *Server) initDb() error {
	dbh, err := sql.Open(s.Config.DbDriver, s.Config.DbSource)
	if err != nil {
		return fmt.Errorf("Error opening %s %s: %v\n", s.Config.DbDriver, s.Config.DbSource, err)
	}

	if _, err := dbh.Exec(createTableSql); err != nil {
		return err
	}

	s.Database = dbh
	return nil
}
<|endoftext|>


<commit_before>package main

import (
	"database/sql"
	"encoding/json"
	"fmt"
	"log"
	"os"
	"strings"

	"github.com/kyleconroy/migrator"
	_ "github.com/lib/pq"
)

func getDatabase() (*sql.DB, error) {
	url := os.Getenv("DATABASE_URL")

	if url == "" {
		return nil, fmt.Errorf("connection requires DATABASE_URL environment variable")
	}

	db, err := sql.Open("postgres", url)

	if err != nil {
		return db, err
	}

	return db, nil
}

func FetchSet(db *sql.DB, id string) (Set, error) {
	var set Set
	row := db.QueryRow("SELECT id,name,border,type FROM sets WHERE id = $1", id)
	err := row.Scan(&set.Id, &set.Name, &set.Border, &set.Type)
	return set, err
}

func FetchSets(db *sql.DB) ([]Set, error) {
	sets := []Set{}
	rows, err := db.Query("SELECT id,name,border,type FROM sets ORDER BY name")
	if err != nil {
		return sets, err
	}
	defer rows.Close()
	for rows.Next() {
		var set Set
		if err := rows.Scan(&set.Id, &set.Name, &set.Border, &set.Type); err != nil {
			return sets, err
		}
		set.Fill()
		sets = append(sets, set)
	}
	return sets, nil
}

func FetchTerms(db *sql.DB, term string) ([]string, error) {
	result := []string{}

	rows, err := db.Query("select distinct unnest(" + term + ") as t from cards WHERE NOT sets && '{unh,ugl}' ORDER BY t ASC")
	if err != nil {
		return result, err
	}
	defer rows.Close()
	for rows.Next() {
		var term string

		if err := rows.Scan(&term); err != nil {
			return result, err
		}

		result = append(result, term)
	}

	return result, rows.Err()
}

func scanCards(rows *sql.Rows) ([]Card, error) {
	cards := []Card{}

	defer rows.Close()

	for rows.Next() {
		var blob []byte
		var card Card

		if err := rows.Scan(&blob); err != nil {
			return cards, err
		}

		err := json.Unmarshal(blob, &card)

		if err != nil {
			return cards, err
		}

		cards = append(cards, card)
	}
	if err := rows.Err(); err != nil {
		return cards, err
	}

	return cards, nil
}

func FetchTypeahead(db *sql.DB, search string) ([]Card, error) {
	if strings.ContainsAny(search, "%_") {
		return []Card{}, fmt.Errorf("Search string can't contain '%%' or '_'")
	}

	rows, err := db.Query("SELECT record FROM cards WHERE name ILIKE $1 ORDER BY name LIMIT 10", search+"%")

	if err != nil {
		return []Card{}, err
	}

	return scanCards(rows)
}

func FetchCards(db *sql.DB, cond Condition, page int) ([]Card, error) {
	query := Select("record").From("cards").Where(cond).OrderBy("name", true)
	limit := query.Limit(100).Offset(page * 100)

	ql, items, err := limit.ToSql()

	if err != nil {
		return []Card{}, err
	}

	log.Println(ql, items)

	rows, err := db.Query(ql, items...)

	if err != nil {
		return []Card{}, err
	}

	return scanCards(rows)
}

func FetchCardIDs(db *sql.DB) ([]string, error) {
	ids := []string{}

	rows, err := db.Query("SELECT id FROM cards")
	if err != nil {
		return ids, err
	}

	defer rows.Close()
	for rows.Next() {
		var id string
		if err := rows.Scan(&id); err != nil {
			return ids, err
		}
		ids = append(ids, id)
	}
	return ids, rows.Err()
}

func FetchCard(db *sql.DB, id string) (Card, error) {
	var blob []byte
	var card Card
	err := db.QueryRow("SELECT record FROM cards WHERE id = $1", id).Scan(&blob)
	if err == sql.ErrNoRows {
		return card, fmt.Errorf("No card with ID %s", id)
	}
	if err != nil {
		return card, err
	}
	return card, json.Unmarshal(blob, &card)
}

func FetchPrices(db *sql.DB) (map[string]Price, error) {
	prices := map[string]Price{}

	rows, err := db.Query(`
    SELECT DISTINCT ON (multiverse_id) multiverse_id, low, high, median
    FROM prices
    ORDER BY multiverse_id, created DESC
    `)

	if err != nil {
		return prices, err
	}

	defer rows.Close()
	for rows.Next() {
		var id string
		var price Price
		if err := rows.Scan(&id, &price.Low, &price.High, &price.Average); err != nil {
			return prices, err
		}
		prices[id] = price
	}
	return prices, rows.Err()
}

func InsertPrice(db *sql.DB, id string, price Price) error {
	_, err := db.Exec(`
    INSERT INTO prices (multiverse_id, low, high, median)
    VALUES ($1, $2, $3, $4)
    `, id, price.Low, price.High, price.Average)
	return err
}

func MigrateDatabase() error {
	db, err := getDatabase()
	if err != nil {
		return err
	}
	return migrator.Run(db, "migrations")
}
<commit_msg>Remove query logging from app logs<commit_after>package main

import (
	"database/sql"
	"encoding/json"
	"fmt"
	"os"
	"strings"

	"github.com/kyleconroy/migrator"
	_ "github.com/lib/pq"
)

func getDatabase() (*sql.DB, error) {
	url := os.Getenv("DATABASE_URL")

	if url == "" {
		return nil, fmt.Errorf("connection requires DATABASE_URL environment variable")
	}

	db, err := sql.Open("postgres", url)

	if err != nil {
		return db, err
	}

	return db, nil
}

func FetchSet(db *sql.DB, id string) (Set, error) {
	var set Set
	row := db.QueryRow("SELECT id,name,border,type FROM sets WHERE id = $1", id)
	err := row.Scan(&set.Id, &set.Name, &set.Border, &set.Type)
	return set, err
}

func FetchSets(db *sql.DB) ([]Set, error) {
	sets := []Set{}
	rows, err := db.Query("SELECT id,name,border,type FROM sets ORDER BY name")
	if err != nil {
		return sets, err
	}
	defer rows.Close()
	for rows.Next() {
		var set Set
		if err := rows.Scan(&set.Id, &set.Name, &set.Border, &set.Type); err != nil {
			return sets, err
		}
		set.Fill()
		sets = append(sets, set)
	}
	return sets, nil
}

func FetchTerms(db *sql.DB, term string) ([]string, error) {
	result := []string{}

	rows, err := db.Query("select distinct unnest(" + term + ") as t from cards WHERE NOT sets && '{unh,ugl}' ORDER BY t ASC")
	if err != nil {
		return result, err
	}
	defer rows.Close()
	for rows.Next() {
		var term string

		if err := rows.Scan(&term); err != nil {
			return result, err
		}

		result = append(result, term)
	}

	return result, rows.Err()
}

func scanCards(rows *sql.Rows) ([]Card, error) {
	cards := []Card{}

	defer rows.Close()

	for rows.Next() {
		var blob []byte
		var card Card

		if err := rows.Scan(&blob); err != nil {
			return cards, err
		}

		err := json.Unmarshal(blob, &card)

		if err != nil {
			return cards, err
		}

		cards = append(cards, card)
	}
	if err := rows.Err(); err != nil {
		return cards, err
	}

	return cards, nil
}

func FetchTypeahead(db *sql.DB, search string) ([]Card, error) {
	if strings.ContainsAny(search, "%_") {
		return []Card{}, fmt.Errorf("Search string can't contain '%%' or '_'")
	}

	rows, err := db.Query("SELECT record FROM cards WHERE name ILIKE $1 ORDER BY name LIMIT 10", search+"%")

	if err != nil {
		return []Card{}, err
	}

	return scanCards(rows)
}

func FetchCards(db *sql.DB, cond Condition, page int) ([]Card, error) {
	query := Select("record").From("cards").Where(cond).OrderBy("name", true)
	limit := query.Limit(100).Offset(page * 100)

	ql, items, err := limit.ToSql()

	if err != nil {
		return []Card{}, err
	}

	rows, err := db.Query(ql, items...)

	if err != nil {
		return []Card{}, err
	}

	return scanCards(rows)
}

func FetchCardIDs(db *sql.DB) ([]string, error) {
	ids := []string{}

	rows, err := db.Query("SELECT id FROM cards")
	if err != nil {
		return ids, err
	}

	defer rows.Close()
	for rows.Next() {
		var id string
		if err := rows.Scan(&id); err != nil {
			return ids, err
		}
		ids = append(ids, id)
	}
	return ids, rows.Err()
}

func FetchCard(db *sql.DB, id string) (Card, error) {
	var blob []byte
	var card Card
	err := db.QueryRow("SELECT record FROM cards WHERE id = $1", id).Scan(&blob)
	if err == sql.ErrNoRows {
		return card, fmt.Errorf("No card with ID %s", id)
	}
	if err != nil {
		return card, err
	}
	return card, json.Unmarshal(blob, &card)
}

func FetchPrices(db *sql.DB) (map[string]Price, error) {
	prices := map[string]Price{}

	rows, err := db.Query(`
    SELECT DISTINCT ON (multiverse_id) multiverse_id, low, high, median
    FROM prices
    ORDER BY multiverse_id, created DESC
    `)

	if err != nil {
		return prices, err
	}

	defer rows.Close()
	for rows.Next() {
		var id string
		var price Price
		if err := rows.Scan(&id, &price.Low, &price.High, &price.Average); err != nil {
			return prices, err
		}
		prices[id] = price
	}
	return prices, rows.Err()
}

func InsertPrice(db *sql.DB, id string, price Price) error {
	_, err := db.Exec(`
    INSERT INTO prices (multiverse_id, low, high, median)
    VALUES ($1, $2, $3, $4)
    `, id, price.Low, price.High, price.Average)
	return err
}

func MigrateDatabase() error {
	db, err := getDatabase()
	if err != nil {
		return err
	}
	return migrator.Run(db, "migrations")
}
<|endoftext|>


<commit_before>package main

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

	"github.com/ark-lang/ark/src/util/log"

	"gopkg.in/alecthomas/kingpin.v2"

	"github.com/ark-lang/ark/src/codegen"
	"github.com/ark-lang/ark/src/codegen/LLVMCodegen"
	"github.com/ark-lang/ark/src/doc"
	"github.com/ark-lang/ark/src/lexer"
	"github.com/ark-lang/ark/src/parser"
	"github.com/ark-lang/ark/src/util"
)

const (
	VERSION = "0.0.2"
	AUTHOR  = "The Ark Authors"
)

var startTime time.Time

func main() {
	startTime = time.Now()

	command := kingpin.MustParse(app.Parse(os.Args[1:]))
	log.SetLevel(*logLevel)
	log.SetTags(*logTags)

	switch command {
	case buildCom.FullCommand():
		ccArgs := []string{}
		if *buildStatic {
			ccArgs = append(ccArgs, "-static")
		}

		outputType := parseOutputType(*buildOutputType)
		build(*buildInputs, *buildOutput, *buildCodegen, ccArgs, outputType)
		printFinishedMessage(startTime, buildCom.FullCommand(), len(*buildInputs))
		if *buildRun {
			if outputType != LLVMCodegen.OUTPUT_EXECUTABLE {
				setupErr("Can only use --run flag when building executable")
			}
			run(*buildOutput)
		}

	case docgenCom.FullCommand():
		docgen(*docgenInputs, *docgenDir)
		printFinishedMessage(startTime, docgenCom.FullCommand(), len(*docgenInputs))
	}
}

func printFinishedMessage(startTime time.Time, command string, numFiles int) {
	dur := time.Since(startTime)
	log.Info("main", "%s (%d file(s), %.2fms)\n",
		util.TEXT_GREEN+util.TEXT_BOLD+fmt.Sprintf("Finished %s", command)+util.TEXT_RESET,
		numFiles, float32(dur.Nanoseconds())/1000000)
}

func setupErr(err string, stuff ...interface{}) {
	log.Error("main", util.TEXT_RED+util.TEXT_BOLD+"Setup error:"+util.TEXT_RESET+" %s\n",
		fmt.Sprintf(err, stuff...))
	os.Exit(util.EXIT_FAILURE_SETUP)
}

func parseFiles(files []string) ([]*parser.Module, map[string]*parser.Module) {
	sourcefiles := make([]*lexer.Sourcefile, 0)

	for _, file := range files {
		input, err := lexer.NewSourcefile(file)
		if err != nil {
			setupErr("%s", err.Error())
		}
		sourcefiles = append(sourcefiles, input)
	}

	for _, file := range sourcefiles {
		file.Tokens = lexer.Lex(file.Contents, file.Name)
	}

	parsedFiles := make([]*parser.Module, 0)
	modules := make(map[string]*parser.Module, 0)

	for _, file := range sourcefiles {
		parsedFiles = append(parsedFiles, parser.Parse(file, modules))
	}

	return parsedFiles, modules
}

func build(files []string, outputFile string, cg string, ccArgs []string, outputType LLVMCodegen.OutputType) {
	// read source files
	var sourcefiles []*lexer.Sourcefile

	timed("reading sourcefiles", func() {
		for _, file := range files {
			sourcefile, err := lexer.NewSourcefile(file)
			if err != nil {
				setupErr("%s", err.Error())
			}
			sourcefiles = append(sourcefiles, sourcefile)
		}
	})

	// lexing
	timed("lexing phase", func() {
		for _, file := range sourcefiles {
			file.Tokens = lexer.Lex(file.Contents, file.Name)
		}
	})

	// parsing
	var parsedFiles []*parser.Module
	modules := make(map[string]*parser.Module)

	timed("parsing phase", func() {
		for _, file := range sourcefiles {
			parsedFiles = append(parsedFiles, parser.Parse(file, modules))
		}
	})

	// resolve
	timed("resolve phase", func() {
		// TODO: We're looping over a map, the order we get is thus random
		for _, module := range modules {
			res := &parser.Resolver{Module: module}
			res.Resolve(modules)
		}
	})

	// semantic analysis
	timed("semantic analysis phase", func() {
		// TODO: We're looping over a map, the order we get is thus random
		for _, module := range modules {
			sem := &parser.SemanticAnalyzer{Module: module}
			sem.Analyze(modules)
		}
	})

	// codegen
	if cg != "none" {
		var gen codegen.Codegen

		switch cg {
		case "llvm":
			gen = &LLVMCodegen.Codegen{
				OutputName:   outputFile,
				CompilerArgs: ccArgs,
				OutputType:   outputType,
			}
		default:
			log.Error("main", util.Red("error: ")+"Invalid backend choice `"+cg+"`")
			os.Exit(1)
		}

		timed("codegen phase", func() {
			gen.Generate(parsedFiles, modules)
		})
	}

}

func timed(title string, fn func()) {
	log.Verboseln("main", util.TEXT_BOLD+util.TEXT_GREEN+"Started "+title+util.TEXT_RESET)
	start := time.Now()

	fn()

	duration := time.Since(start)
	log.Verboseln("main", util.TEXT_BOLD+util.TEXT_GREEN+"Ended "+title+util.TEXT_RESET+" (%.2fms)", float32(duration)/1000000)
}

func run(output string) {
	cmd := exec.Command("./" + output)
	cmd.Stdin = os.Stdin
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	cmd.Run()
}

func docgen(input []string, dir string) {
	files, _ := parseFiles(input)

	gen := &doc.Docgen{
		Input: files,
		Dir:   dir,
	}

	gen.Generate()
}
<commit_msg>Error when no input files are passed<commit_after>package main

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

	"github.com/ark-lang/ark/src/util/log"

	"gopkg.in/alecthomas/kingpin.v2"

	"github.com/ark-lang/ark/src/codegen"
	"github.com/ark-lang/ark/src/codegen/LLVMCodegen"
	"github.com/ark-lang/ark/src/doc"
	"github.com/ark-lang/ark/src/lexer"
	"github.com/ark-lang/ark/src/parser"
	"github.com/ark-lang/ark/src/util"
)

const (
	VERSION = "0.0.2"
	AUTHOR  = "The Ark Authors"
)

var startTime time.Time

func main() {
	startTime = time.Now()

	command := kingpin.MustParse(app.Parse(os.Args[1:]))
	log.SetLevel(*logLevel)
	log.SetTags(*logTags)

	switch command {
	case buildCom.FullCommand():
		if len(*buildInputs) == 0 {
			setupErr("No input files passed.")
		}

		ccArgs := []string{}
		if *buildStatic {
			ccArgs = append(ccArgs, "-static")
		}

		outputType := parseOutputType(*buildOutputType)
		build(*buildInputs, *buildOutput, *buildCodegen, ccArgs, outputType)
		printFinishedMessage(startTime, buildCom.FullCommand(), len(*buildInputs))
		if *buildRun {
			if outputType != LLVMCodegen.OUTPUT_EXECUTABLE {
				setupErr("Can only use --run flag when building executable")
			}
			run(*buildOutput)
		}

	case docgenCom.FullCommand():
		docgen(*docgenInputs, *docgenDir)
		printFinishedMessage(startTime, docgenCom.FullCommand(), len(*docgenInputs))
	}
}

func printFinishedMessage(startTime time.Time, command string, numFiles int) {
	dur := time.Since(startTime)
	log.Info("main", "%s (%d file(s), %.2fms)\n",
		util.TEXT_GREEN+util.TEXT_BOLD+fmt.Sprintf("Finished %s", command)+util.TEXT_RESET,
		numFiles, float32(dur.Nanoseconds())/1000000)
}

func setupErr(err string, stuff ...interface{}) {
	log.Error("main", util.TEXT_RED+util.TEXT_BOLD+"Setup error:"+util.TEXT_RESET+" %s\n",
		fmt.Sprintf(err, stuff...))
	os.Exit(util.EXIT_FAILURE_SETUP)
}

func parseFiles(files []string) ([]*parser.Module, map[string]*parser.Module) {
	sourcefiles := make([]*lexer.Sourcefile, 0)

	for _, file := range files {
		input, err := lexer.NewSourcefile(file)
		if err != nil {
			setupErr("%s", err.Error())
		}
		sourcefiles = append(sourcefiles, input)
	}

	for _, file := range sourcefiles {
		file.Tokens = lexer.Lex(file.Contents, file.Name)
	}

	parsedFiles := make([]*parser.Module, 0)
	modules := make(map[string]*parser.Module, 0)

	for _, file := range sourcefiles {
		parsedFiles = append(parsedFiles, parser.Parse(file, modules))
	}

	return parsedFiles, modules
}

func build(files []string, outputFile string, cg string, ccArgs []string, outputType LLVMCodegen.OutputType) {
	// read source files
	var sourcefiles []*lexer.Sourcefile

	timed("reading sourcefiles", func() {
		for _, file := range files {
			sourcefile, err := lexer.NewSourcefile(file)
			if err != nil {
				setupErr("%s", err.Error())
			}
			sourcefiles = append(sourcefiles, sourcefile)
		}
	})

	// lexing
	timed("lexing phase", func() {
		for _, file := range sourcefiles {
			file.Tokens = lexer.Lex(file.Contents, file.Name)
		}
	})

	// parsing
	var parsedFiles []*parser.Module
	modules := make(map[string]*parser.Module)

	timed("parsing phase", func() {
		for _, file := range sourcefiles {
			parsedFiles = append(parsedFiles, parser.Parse(file, modules))
		}
	})

	// resolve
	timed("resolve phase", func() {
		// TODO: We're looping over a map, the order we get is thus random
		for _, module := range modules {
			res := &parser.Resolver{Module: module}
			res.Resolve(modules)
		}
	})

	// semantic analysis
	timed("semantic analysis phase", func() {
		// TODO: We're looping over a map, the order we get is thus random
		for _, module := range modules {
			sem := &parser.SemanticAnalyzer{Module: module}
			sem.Analyze(modules)
		}
	})

	// codegen
	if cg != "none" {
		var gen codegen.Codegen

		switch cg {
		case "llvm":
			gen = &LLVMCodegen.Codegen{
				OutputName:   outputFile,
				CompilerArgs: ccArgs,
				OutputType:   outputType,
			}
		default:
			log.Error("main", util.Red("error: ")+"Invalid backend choice `"+cg+"`")
			os.Exit(1)
		}

		timed("codegen phase", func() {
			gen.Generate(parsedFiles, modules)
		})
	}

}

func timed(title string, fn func()) {
	log.Verboseln("main", util.TEXT_BOLD+util.TEXT_GREEN+"Started "+title+util.TEXT_RESET)
	start := time.Now()

	fn()

	duration := time.Since(start)
	log.Verboseln("main", util.TEXT_BOLD+util.TEXT_GREEN+"Ended "+title+util.TEXT_RESET+" (%.2fms)", float32(duration)/1000000)
}

func run(output string) {
	cmd := exec.Command("./" + output)
	cmd.Stdin = os.Stdin
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	cmd.Run()
}

func docgen(input []string, dir string) {
	files, _ := parseFiles(input)

	gen := &doc.Docgen{
		Input: files,
		Dir:   dir,
	}

	gen.Generate()
}
<|endoftext|>


<commit_before>package main

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

	"gopkg.in/alecthomas/kingpin.v2"

	"github.com/ark-lang/ark/src/codegen"
	"github.com/ark-lang/ark/src/codegen/LLVMCodegen"
	"github.com/ark-lang/ark/src/doc"
	"github.com/ark-lang/ark/src/lexer"
	"github.com/ark-lang/ark/src/parser"
	"github.com/ark-lang/ark/src/semantic"
	"github.com/ark-lang/ark/src/util"
	"github.com/ark-lang/ark/src/util/log"
)

const (
	VERSION = "0.0.2"
	AUTHOR  = "The Ark Authors"
)

var startTime time.Time

func main() {
	startTime = time.Now()

	command := kingpin.MustParse(app.Parse(os.Args[1:]))
	log.SetLevel(*logLevel)
	log.SetTags(*logTags)

	switch command {
	case buildCom.FullCommand():
		if len(*buildInputs) == 0 {
			setupErr("No input files passed.")
		}

		ccArgs := []string{}
		if *buildStatic {
			ccArgs = append(ccArgs, "-static")
		}

		outputType := parseOutputType(*buildOutputType)
		build(*buildInputs, *buildOutput, *buildCodegen, ccArgs, outputType, *buildOptLevel)
		printFinishedMessage(startTime, buildCom.FullCommand(), len(*buildInputs))
		if *buildRun {
			if outputType != LLVMCodegen.OUTPUT_EXECUTABLE {
				setupErr("Can only use --run flag when building executable")
			}
			run(*buildOutput)
		}

	case docgenCom.FullCommand():
		docgen(*docgenInputs, *docgenDir)
		printFinishedMessage(startTime, docgenCom.FullCommand(), len(*docgenInputs))
	}
}

func printFinishedMessage(startTime time.Time, command string, numFiles int) {
	dur := time.Since(startTime)
	log.Info("main", "%s (%d file(s), %.2fms)\n",
		util.TEXT_GREEN+util.TEXT_BOLD+fmt.Sprintf("Finished %s", command)+util.TEXT_RESET,
		numFiles, float32(dur.Nanoseconds())/1000000)
}

func setupErr(err string, stuff ...interface{}) {
	log.Error("main", util.TEXT_RED+util.TEXT_BOLD+"Setup error:"+util.TEXT_RESET+" %s\n",
		fmt.Sprintf(err, stuff...))
	os.Exit(util.EXIT_FAILURE_SETUP)
}

func parseFiles(files []string) ([]*parser.Module, map[string]*parser.Module) {
	// read source files
	var sourcefiles []*lexer.Sourcefile
	log.Timed("reading sourcefiles", func() {
		for _, file := range files {
			sourcefile, err := lexer.NewSourcefile(file)
			if err != nil {
				setupErr("%s", err.Error())
			}
			sourcefiles = append(sourcefiles, sourcefile)
		}
	})

	// lexing
	log.Timed("lexing phase", func() {
		for _, file := range sourcefiles {
			file.Tokens = lexer.Lex(file)
		}
	})

	// parsing
	var parsedFiles []*parser.ParseTree
	parsedFileMap := make(map[string]*parser.ParseTree)
	log.Timed("parsing phase", func() {
		for _, file := range sourcefiles {
			parsedFile := parser.Parse(file)
			parsedFiles = append(parsedFiles, parsedFile)
			parsedFileMap[parsedFile.Source.Name] = parsedFile
		}
	})

	// construction
	var constructedModules []*parser.Module
	modules := make(map[string]*parser.Module)
	log.Timed("construction phase", func() {
		for _, file := range parsedFiles {
			constructedModules = append(constructedModules, parser.Construct(file, parsedFileMap, modules))
		}
	})

	return constructedModules, modules
}

func build(files []string, outputFile string, cg string, ccArgs []string, outputType LLVMCodegen.OutputType, optLevel int) {
	constructedModules, modules := parseFiles(files)

	// resolve
	log.Timed("resolve phase", func() {
		// TODO: We're looping over a map, the order we get is thus random
		for _, module := range modules {
			res := &parser.Resolver{Module: module}
			res.Resolve(modules)
		}
	})

	// type inference
	log.Timed("inference phase", func() {
		// TODO: We're looping over a map, the order we get is thus random
		for _, module := range modules {
			inf := &parser.TypeInferer{Module: module}
			inf.Infer(modules)

			// Dump AST
			log.Debugln("main", "AST of module `%s`:", module.Name)
			for _, node := range module.Nodes {
				log.Debugln("main", node.String())
			}
		}
	})

	// semantic analysis
	log.Timed("semantic analysis phase", func() {
		// TODO: We're looping over a map, the order we get is thus random
		for _, module := range modules {
			sem := &semantic.SemanticAnalyzer{Module: module}
			sem.Analyze(modules)
		}
	})

	// codegen
	if cg != "none" {
		var gen codegen.Codegen

		switch cg {
		case "llvm":
			gen = &LLVMCodegen.Codegen{
				OutputName:   outputFile,
				CompilerArgs: ccArgs,
				OutputType:   outputType,
				OptLevel:     optLevel,
			}
		default:
			log.Error("main", util.Red("error: ")+"Invalid backend choice `"+cg+"`")
			os.Exit(1)
		}

		log.Timed("codegen phase", func() {
			gen.Generate(constructedModules, modules)
		})
	}

}

func run(output string) {
	cmd := exec.Command("./" + output)
	cmd.Stdin = os.Stdin
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	cmd.Run()
}

func docgen(input []string, dir string) {
	constructedModules, _ := parseFiles(input)

	gen := &doc.Docgen{
		Input: constructedModules,
		Dir:   dir,
	}

	gen.Generate()
}
<commit_msg>fix ast output<commit_after>package main

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

	"gopkg.in/alecthomas/kingpin.v2"

	"github.com/ark-lang/ark/src/codegen"
	"github.com/ark-lang/ark/src/codegen/LLVMCodegen"
	"github.com/ark-lang/ark/src/doc"
	"github.com/ark-lang/ark/src/lexer"
	"github.com/ark-lang/ark/src/parser"
	"github.com/ark-lang/ark/src/semantic"
	"github.com/ark-lang/ark/src/util"
	"github.com/ark-lang/ark/src/util/log"
)

const (
	VERSION = "0.0.2"
	AUTHOR  = "The Ark Authors"
)

var startTime time.Time

func main() {
	startTime = time.Now()

	command := kingpin.MustParse(app.Parse(os.Args[1:]))
	log.SetLevel(*logLevel)
	log.SetTags(*logTags)

	switch command {
	case buildCom.FullCommand():
		if len(*buildInputs) == 0 {
			setupErr("No input files passed.")
		}

		ccArgs := []string{}
		if *buildStatic {
			ccArgs = append(ccArgs, "-static")
		}

		outputType := parseOutputType(*buildOutputType)
		build(*buildInputs, *buildOutput, *buildCodegen, ccArgs, outputType, *buildOptLevel)
		printFinishedMessage(startTime, buildCom.FullCommand(), len(*buildInputs))
		if *buildRun {
			if outputType != LLVMCodegen.OUTPUT_EXECUTABLE {
				setupErr("Can only use --run flag when building executable")
			}
			run(*buildOutput)
		}

	case docgenCom.FullCommand():
		docgen(*docgenInputs, *docgenDir)
		printFinishedMessage(startTime, docgenCom.FullCommand(), len(*docgenInputs))
	}
}

func printFinishedMessage(startTime time.Time, command string, numFiles int) {
	dur := time.Since(startTime)
	log.Info("main", "%s (%d file(s), %.2fms)\n",
		util.TEXT_GREEN+util.TEXT_BOLD+fmt.Sprintf("Finished %s", command)+util.TEXT_RESET,
		numFiles, float32(dur.Nanoseconds())/1000000)
}

func setupErr(err string, stuff ...interface{}) {
	log.Error("main", util.TEXT_RED+util.TEXT_BOLD+"Setup error:"+util.TEXT_RESET+" %s\n",
		fmt.Sprintf(err, stuff...))
	os.Exit(util.EXIT_FAILURE_SETUP)
}

func parseFiles(files []string) ([]*parser.Module, map[string]*parser.Module) {
	// read source files
	var sourcefiles []*lexer.Sourcefile
	log.Timed("reading sourcefiles", func() {
		for _, file := range files {
			sourcefile, err := lexer.NewSourcefile(file)
			if err != nil {
				setupErr("%s", err.Error())
			}
			sourcefiles = append(sourcefiles, sourcefile)
		}
	})

	// lexing
	log.Timed("lexing phase", func() {
		for _, file := range sourcefiles {
			file.Tokens = lexer.Lex(file)
		}
	})

	// parsing
	var parsedFiles []*parser.ParseTree
	parsedFileMap := make(map[string]*parser.ParseTree)
	log.Timed("parsing phase", func() {
		for _, file := range sourcefiles {
			parsedFile := parser.Parse(file)
			parsedFiles = append(parsedFiles, parsedFile)
			parsedFileMap[parsedFile.Source.Name] = parsedFile
		}
	})

	// construction
	var constructedModules []*parser.Module
	modules := make(map[string]*parser.Module)
	log.Timed("construction phase", func() {
		for _, file := range parsedFiles {
			constructedModules = append(constructedModules, parser.Construct(file, parsedFileMap, modules))
		}
	})

	return constructedModules, modules
}

func build(files []string, outputFile string, cg string, ccArgs []string, outputType LLVMCodegen.OutputType, optLevel int) {
	constructedModules, modules := parseFiles(files)

	// resolve
	log.Timed("resolve phase", func() {
		// TODO: We're looping over a map, the order we get is thus random
		for _, module := range modules {
			res := &parser.Resolver{Module: module}
			res.Resolve(modules)
		}
	})

	// type inference
	log.Timed("inference phase", func() {
		// TODO: We're looping over a map, the order we get is thus random
		for _, module := range modules {
			inf := &parser.TypeInferer{Module: module}
			inf.Infer(modules)

			// Dump AST
			log.Debugln("main", "AST of module `%s`:", module.Name)
			for _, node := range module.Nodes {
				log.Debugln("main", "%s", node.String())
			}
		}
	})

	// semantic analysis
	log.Timed("semantic analysis phase", func() {
		// TODO: We're looping over a map, the order we get is thus random
		for _, module := range modules {
			sem := &semantic.SemanticAnalyzer{Module: module}
			sem.Analyze(modules)
		}
	})

	// codegen
	if cg != "none" {
		var gen codegen.Codegen

		switch cg {
		case "llvm":
			gen = &LLVMCodegen.Codegen{
				OutputName:   outputFile,
				CompilerArgs: ccArgs,
				OutputType:   outputType,
				OptLevel:     optLevel,
			}
		default:
			log.Error("main", util.Red("error: ")+"Invalid backend choice `"+cg+"`")
			os.Exit(1)
		}

		log.Timed("codegen phase", func() {
			gen.Generate(constructedModules, modules)
		})
	}

}

func run(output string) {
	cmd := exec.Command("./" + output)
	cmd.Stdin = os.Stdin
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	cmd.Run()
}

func docgen(input []string, dir string) {
	constructedModules, _ := parseFiles(input)

	gen := &doc.Docgen{
		Input: constructedModules,
		Dir:   dir,
	}

	gen.Generate()
}
<|endoftext|>


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

package main

import (
	"fmt"
	"go/token"
	"io/ioutil"
	"os"
	"os/exec"
)

// run runs the command argv, feeding in stdin on standard input.
// It returns the output to standard output and standard error.
// ok indicates whether the command exited successfully.
func run(stdin []byte, argv []string) (stdout, stderr []byte, ok bool) {
	cmd, err := exec.LookPath(argv[0])
	if err != nil {
		fatalf("exec %s: %s", argv[0], err)
	}
	r0, w0, err := os.Pipe()
	if err != nil {
		fatalf("%s", err)
	}
	r1, w1, err := os.Pipe()
	if err != nil {
		fatalf("%s", err)
	}
	r2, w2, err := os.Pipe()
	if err != nil {
		fatalf("%s", err)
	}
	p, err := os.StartProcess(cmd, argv, &os.ProcAttr{Files: []*os.File{r0, w1, w2}})
	if err != nil {
		fatalf("%s", err)
	}
	r0.Close()
	w1.Close()
	w2.Close()
	c := make(chan bool)
	go func() {
		w0.Write(stdin)
		w0.Close()
		c <- true
	}()
	go func() {
		stdout, _ = ioutil.ReadAll(r1)
		r1.Close()
		c <- true
	}()
	stderr, _ = ioutil.ReadAll(r2)
	r2.Close()
	<-c
	<-c

	state, err := p.Wait()
	if err != nil {
		fatalf("%s", err)
	}
	ok = state.Success()
	return
}

func lineno(pos token.Pos) string {
	return fset.Position(pos).String()
}

// Die with an error message.
func fatalf(msg string, args ...interface{}) {
	// If we've already printed other errors, they might have
	// caused the fatal condition.  Assume they're enough.
	if nerrors == 0 {
		fmt.Fprintf(os.Stderr, msg+"\n", args...)
	}
	os.Exit(2)
}

var nerrors int

func error_(pos token.Pos, msg string, args ...interface{}) {
	nerrors++
	if pos.IsValid() {
		fmt.Fprintf(os.Stderr, "%s: ", fset.Position(pos).String())
	}
	fmt.Fprintf(os.Stderr, msg, args...)
	fmt.Fprintf(os.Stderr, "\n")
}

// isName returns true if s is a valid C identifier
func isName(s string) bool {
	for i, v := range s {
		if v != '_' && (v < 'A' || v > 'Z') && (v < 'a' || v > 'z') && (v < '0' || v > '9') {
			return false
		}
		if i == 0 && '0' <= v && v <= '9' {
			return false
		}
	}
	return s != ""
}

func creat(name string) *os.File {
	f, err := os.Create(name)
	if err != nil {
		fatalf("%s", err)
	}
	return f
}

func slashToUnderscore(c rune) rune {
	if c == '/' || c == '\\' || c == ':' {
		c = '_'
	}
	return c
}
<commit_msg>cmd/cgo: don't reimplement os/exec in util.go.<commit_after>// Copyright 2009 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"bytes"
	"fmt"
	"go/token"
	"os"
	"os/exec"
)

// run runs the command argv, feeding in stdin on standard input.
// It returns the output to standard output and standard error.
// ok indicates whether the command exited successfully.
func run(stdin []byte, argv []string) (stdout, stderr []byte, ok bool) {
	p := exec.Command(argv[0], argv[1:]...)
	p.Stdin = bytes.NewReader(stdin)
	var bout, berr bytes.Buffer
	p.Stdout = &bout
	p.Stderr = &berr
	err := p.Run()
	if _, ok := err.(*exec.ExitError); err != nil && !ok {
		fatalf("%s", err)
	}
	ok = p.ProcessState.Success()
	stdout, stderr = bout.Bytes(), berr.Bytes()
	return
}

func lineno(pos token.Pos) string {
	return fset.Position(pos).String()
}

// Die with an error message.
func fatalf(msg string, args ...interface{}) {
	// If we've already printed other errors, they might have
	// caused the fatal condition.  Assume they're enough.
	if nerrors == 0 {
		fmt.Fprintf(os.Stderr, msg+"\n", args...)
	}
	os.Exit(2)
}

var nerrors int

func error_(pos token.Pos, msg string, args ...interface{}) {
	nerrors++
	if pos.IsValid() {
		fmt.Fprintf(os.Stderr, "%s: ", fset.Position(pos).String())
	}
	fmt.Fprintf(os.Stderr, msg, args...)
	fmt.Fprintf(os.Stderr, "\n")
}

// isName returns true if s is a valid C identifier
func isName(s string) bool {
	for i, v := range s {
		if v != '_' && (v < 'A' || v > 'Z') && (v < 'a' || v > 'z') && (v < '0' || v > '9') {
			return false
		}
		if i == 0 && '0' <= v && v <= '9' {
			return false
		}
	}
	return s != ""
}

func creat(name string) *os.File {
	f, err := os.Create(name)
	if err != nil {
		fatalf("%s", err)
	}
	return f
}

func slashToUnderscore(c rune) rune {
	if c == '/' || c == '\\' || c == ':' {
		c = '_'
	}
	return c
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"net"
	"time"
)

func (this *Server) Rollback(conn net.Conn, applicationName, version string) error {
	return this.WithApplication(applicationName, func(app *Application, cfg *Config) error {
		deployLock.start()
		defer deployLock.finish()

		if app.LastDeploy == "" {
			return fmt.Errorf("Automatic rollback version detection is impossible because this app has not had any releases")
		}
		if app.LastDeploy == "v1" {
			return fmt.Errorf("Automatic rollback version detection is impossible because this app has only had 1 release")
		}
		if version == "" {
			// Get release before current.
			var err error = nil
			version, err = app.CalcPreviousVersion()
			if err != nil {
				return err
			}
		}
		logger := NewLogger(NewTimeLogger(NewMessageLogger(conn)), "[rollback] ")
		fmt.Fprintf(logger, "Rolling back to %v\n", version)

		// Get the next version.
		app, cfg, err := this.IncrementAppVersion(app)
		if err != nil {
			return err
		}

		deployment := &Deployment{
			Server:      this,
			Logger:      logger,
			Config:      cfg,
			Application: app,
			Version:     app.LastDeploy,
			StartedTs:   time.Now(),
		}

		// Cleanup any hanging chads upon error.
		defer func() {
			if err != nil {
				deployment.undoVersionBump()
			}
		}()

		err = deployment.extract(version)
		if err != nil {
			return err
		}
		err = deployment.deploy()
		if err != nil {
			return err
		}
		return nil
	})
}
<commit_msg>`rollback` command now also triggers an archival to S3.<commit_after>package main

import (
	"fmt"
	"net"
	"time"
)

func (this *Server) Rollback(conn net.Conn, applicationName, version string) error {
	return this.WithApplication(applicationName, func(app *Application, cfg *Config) error {
		deployLock.start()
		defer deployLock.finish()

		if app.LastDeploy == "" {
			return fmt.Errorf("Automatic rollback version detection is impossible because this app has not had any releases")
		}
		if app.LastDeploy == "v1" {
			return fmt.Errorf("Automatic rollback version detection is impossible because this app has only had 1 release")
		}
		if version == "" {
			// Get release before current.
			var err error = nil
			version, err = app.CalcPreviousVersion()
			if err != nil {
				return err
			}
		}
		logger := NewLogger(NewTimeLogger(NewMessageLogger(conn)), "[rollback] ")
		fmt.Fprintf(logger, "Rolling back to %v\n", version)

		// Get the next version.
		app, cfg, err := this.IncrementAppVersion(app)
		if err != nil {
			return err
		}

		deployment := &Deployment{
			Server:      this,
			Logger:      logger,
			Config:      cfg,
			Application: app,
			Version:     app.LastDeploy,
			StartedTs:   time.Now(),
		}

		// Cleanup any hanging chads upon error.
		defer func() {
			if err != nil {
				deployment.undoVersionBump()
			}
		}()

		err = deployment.extract(version)
		if err != nil {
			return err
		}
		err = deployment.archive()
		if err != nil {
			return err
		}
		err = deployment.deploy()
		if err != nil {
			return err
		}
		return nil
	})
}
<|endoftext|>


<commit_before>package wspr

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
	"net/http/httptest"
	"testing"

	"veyron.io/veyron/veyron2"
	"veyron.io/veyron/veyron2/context"
	"veyron.io/veyron/veyron2/ipc"
	"veyron.io/veyron/veyron2/security"
	"veyron.io/veyron/veyron2/vdl/vdlutil"

	"veyron.io/veyron/veyron/profiles"
)

const topLevelName = "mock-blesser"

// BEGIN MOCK BLESSER SERVICE
// TODO(nlacasse): Is there a better way to mock this?!
type mockBlesserService struct {
	p     security.Principal
	count int
}

func newMockBlesserService(p security.Principal) *mockBlesserService {
	return &mockBlesserService{
		p:     p,
		count: 0,
	}
}

func (m *mockBlesserService) BlessUsingAccessToken(c context.T, accessToken string, co ...ipc.CallOpt) (vdlutil.Any, []string, error) {
	m.count = m.count + 1
	name := fmt.Sprintf("%s%s%d", topLevelName, security.ChainSeparator, m.count)
	blessing, err := m.p.BlessSelf(name)
	if err != nil {
		return nil, nil, err
	}
	return security.MarshalBlessings(blessing), []string{name}, nil
}

// END MOCK BLESSER SERVICE

func setup(t *testing.T) (*WSPR, func()) {
	spec := *profiles.LocalListenSpec
	spec.Proxy = "/mock/proxy"
	wspr := NewWSPR(0, spec, "/mock/identd", veyron2.ForceNewSecurityModel{})
	wspr.blesser = newMockBlesserService(wspr.rt.Principal())
	return wspr, func() {
		wspr.Shutdown()
	}
}

func TestHandleCreateAccount(t *testing.T) {
	wspr, teardown := setup(t)
	defer teardown()

	method := "POST"
	path := "/create-account"

	// Add one account
	data1 := createAccountInput{
		AccessToken: "mock-access-token-1",
	}
	data1Json, err := json.Marshal(data1)
	if err != nil {
		t.Fatalf("json.Marshal(%v) failed: %v", data1, err)
	}

	data1JsonReader := bytes.NewReader(data1Json)
	req, err := http.NewRequest(method, path, (data1JsonReader))
	if err != nil {
		t.Fatalf("http.NewRequest(%v, %v, %v,) failed: %v", method, path, data1JsonReader, err)
	}

	resp1 := httptest.NewRecorder()
	wspr.handleCreateAccount(resp1, req)
	if resp1.Code != 200 {
		t.Fatalf("Expected handleCreateAccount to return 200 OK, instead got %v", resp1)
	}

	// Verify that principalManager has the new account
	expectedAccountName := fmt.Sprintf("%s%s%d", topLevelName, security.ChainSeparator, 1)
	gotAccounts := wspr.principalManager.AccountsMatching(security.BlessingPattern(expectedAccountName))
	if len(gotAccounts) != 1 {
		t.Fatalf("Expected to have 1 account with name %v, but got %v: %v", expectedAccountName, len(gotAccounts), gotAccounts)
	}

	// Add another account
	data2 := createAccountInput{
		AccessToken: "mock-access-token-2",
	}
	data2Json, err := json.Marshal(data2)
	if err != nil {
		t.Fatalf("json.Marshal(%v) failed: %v", data2, err)
	}
	data2JsonReader := bytes.NewReader(data2Json)
	req, err = http.NewRequest(method, path, data2JsonReader)
	if err != nil {
		t.Fatalf("http.NewRequest(%v, %v, %v,) failed: %v", method, path, data2JsonReader, err)
	}

	resp2 := httptest.NewRecorder()
	wspr.handleCreateAccount(resp2, req)
	if resp2.Code != 200 {
		t.Fatalf("Expected handleCreateAccount to return 200 OK, instead got %v", resp2)
	}

	// Verify that principalManager has both accounts
	gotAccounts = wspr.principalManager.AccountsMatching(security.BlessingPattern(fmt.Sprintf("%s%s%v", topLevelName, security.ChainSeparator, security.AllPrincipals)))
	if len(gotAccounts) != 2 {
		t.Fatalf("Expected to have 2 accounts, but got %v: %v", len(gotAccounts), gotAccounts)
	}
}

func TestHandleAssocAccount(t *testing.T) {
	wspr, teardown := setup(t)
	defer teardown()

	// First create an account.
	accountName := "mock-account"
	blessing, err := wspr.rt.Principal().BlessSelf(accountName)
	if err != nil {
		t.Fatalf("wspr.rt.Principal.BlessSelf(%v) failed: %v", accountName, err)
	}
	if err := wspr.principalManager.AddAccount(accountName, blessing); err != nil {
		t.Fatalf("wspr.principalManager.AddAccount(%v, %v) failed; %v", accountName, blessing, err)
	}

	// Associate with that account
	method := "POST"
	path := "/assoc-account"

	origin := "https://my.webapp.com:443"
	data := assocAccountInput{
		Name:   accountName,
		Origin: origin,
	}

	dataJson, err := json.Marshal(data)
	if err != nil {
		t.Fatalf("json.Marshal(%v) failed: %v", data, err)
	}

	dataJsonReader := bytes.NewReader(dataJson)
	req, err := http.NewRequest(method, path, (dataJsonReader))
	if err != nil {
		t.Fatalf("http.NewRequest(%v, %v, %v,) failed: %v", method, path, dataJsonReader, err)
	}

	resp := httptest.NewRecorder()
	wspr.handleAssocAccount(resp, req)
	if resp.Code != 200 {
		t.Fatalf("Expected handleAssocAccount to return 200 OK, instead got %v", resp)
	}

	// Verify that principalManager has the correct principal for the origin
	got, err := wspr.principalManager.Principal(origin)
	if err != nil {
		t.Fatalf("wspr.principalManager.Principal(%v) failed: %v", origin, err)
	}

	if got == nil {
		t.Fatalf("Expected wspr.principalManager.Principal(%v) to return a valid principal, but got %v", origin, got)
	}
}

func TestHandleAssocAccountWithMissingAccount(t *testing.T) {
	wspr, teardown := setup(t)
	defer teardown()

	method := "POST"
	path := "/assoc-account"

	accountName := "mock-account"
	origin := "https://my.webapp.com:443"
	data := assocAccountInput{
		Name:   accountName,
		Origin: origin,
	}

	dataJson, err := json.Marshal(data)
	if err != nil {
		t.Fatalf("json.Marshal(%v) failed: %v", data, err)
	}

	dataJsonReader := bytes.NewReader(dataJson)
	req, err := http.NewRequest(method, path, (dataJsonReader))
	if err != nil {
		t.Fatalf("http.NewRequest(%v, %v, %v,) failed: %v", method, path, dataJsonReader, err)
	}

	// Verify that the request fails with 400 Bad Request error
	resp := httptest.NewRecorder()
	wspr.handleAssocAccount(resp, req)
	if resp.Code != 400 {
		t.Fatalf("Expected handleAssocAccount to return 400 error, but got %v", resp)
	}

	// Verify that principalManager creates no principal for the origin
	got, err := wspr.principalManager.Principal(origin)
	if err == nil {
		t.Fatalf("Expected wspr.principalManager.Principal(%v) to fail, but got: %v", origin, got)
	}

	if got != nil {
		t.Fatalf("Expected wspr.principalManager.Principal(%v) not to return a principal, but got %v", origin, got)
	}
}
<commit_msg>"veyron/services/wsprd/wspr": Bug fix<commit_after>package wspr

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
	"net/http/httptest"
	"testing"

	"veyron.io/veyron/veyron2"
	"veyron.io/veyron/veyron2/context"
	"veyron.io/veyron/veyron2/ipc"
	"veyron.io/veyron/veyron2/security"
	"veyron.io/veyron/veyron2/vdl/vdlutil"

	"veyron.io/veyron/veyron/profiles"
)

const topLevelName = "mock-blesser"

// BEGIN MOCK BLESSER SERVICE
// TODO(nlacasse): Is there a better way to mock this?!
type mockBlesserService struct {
	p     security.Principal
	count int
}

func newMockBlesserService(p security.Principal) *mockBlesserService {
	return &mockBlesserService{
		p:     p,
		count: 0,
	}
}

func (m *mockBlesserService) BlessUsingAccessToken(c context.T, accessToken string, co ...ipc.CallOpt) (vdlutil.Any, []string, error) {
	m.count = m.count + 1
	name := fmt.Sprintf("%s%s%d", topLevelName, security.ChainSeparator, m.count)
	blessing, err := m.p.BlessSelf(name)
	if err != nil {
		return nil, nil, err
	}
	return security.MarshalBlessings(blessing), []string{name}, nil
}

// END MOCK BLESSER SERVICE

func setup(t *testing.T) (*WSPR, func()) {
	spec := *profiles.LocalListenSpec
	spec.Proxy = "/mock/proxy"
	wspr := NewWSPR(0, spec, "/mock/identd", veyron2.ForceNewSecurityModel{})
	wspr.blesser = newMockBlesserService(wspr.rt.Principal())
	return wspr, func() {
		wspr.Shutdown()
	}
}

func TestHandleCreateAccount(t *testing.T) {
	wspr, teardown := setup(t)
	defer teardown()

	method := "POST"
	path := "/create-account"

	// Add one account
	data1 := createAccountInput{
		AccessToken: "mock-access-token-1",
	}
	data1Json, err := json.Marshal(data1)
	if err != nil {
		t.Fatalf("json.Marshal(%v) failed: %v", data1, err)
	}

	data1JsonReader := bytes.NewReader(data1Json)
	req, err := http.NewRequest(method, path, (data1JsonReader))
	if err != nil {
		t.Fatalf("http.NewRequest(%v, %v, %v,) failed: %v", method, path, data1JsonReader, err)
	}

	resp1 := httptest.NewRecorder()
	wspr.handleCreateAccount(resp1, req)
	if resp1.Code != 200 {
		t.Fatalf("Expected handleCreateAccount to return 200 OK, instead got %v", resp1)
	}

	// Verify that principalManager has the new account
	account1 := fmt.Sprintf("%s%s%d", topLevelName, security.ChainSeparator, 1)
	if b, err := wspr.principalManager.BlessingsForAccount(account1); err != nil || b == nil {
		t.Fatalf("Failed to get Blessings for account %v: got %v, %v", account1, b, err)
	}

	// Add another account
	data2 := createAccountInput{
		AccessToken: "mock-access-token-2",
	}
	data2Json, err := json.Marshal(data2)
	if err != nil {
		t.Fatalf("json.Marshal(%v) failed: %v", data2, err)
	}
	data2JsonReader := bytes.NewReader(data2Json)
	req, err = http.NewRequest(method, path, data2JsonReader)
	if err != nil {
		t.Fatalf("http.NewRequest(%v, %v, %v,) failed: %v", method, path, data2JsonReader, err)
	}

	resp2 := httptest.NewRecorder()
	wspr.handleCreateAccount(resp2, req)
	if resp2.Code != 200 {
		t.Fatalf("Expected handleCreateAccount to return 200 OK, instead got %v", resp2)
	}

	// Verify that principalManager has both accounts
	if b, err := wspr.principalManager.BlessingsForAccount(account1); err != nil || b == nil {
		t.Fatalf("Failed to get Blessings for account %v: got %v, %v", account1, b, err)
	}
	account2 := fmt.Sprintf("%s%s%d", topLevelName, security.ChainSeparator, 2)
	if b, err := wspr.principalManager.BlessingsForAccount(account2); err != nil || b == nil {
		t.Fatalf("Failed to get Blessings for account %v: got %v, %v", account2, b, err)
	}
}

func TestHandleAssocAccount(t *testing.T) {
	wspr, teardown := setup(t)
	defer teardown()

	// First create an account.
	accountName := "mock-account"
	blessing, err := wspr.rt.Principal().BlessSelf(accountName)
	if err != nil {
		t.Fatalf("wspr.rt.Principal.BlessSelf(%v) failed: %v", accountName, err)
	}
	if err := wspr.principalManager.AddAccount(accountName, blessing); err != nil {
		t.Fatalf("wspr.principalManager.AddAccount(%v, %v) failed; %v", accountName, blessing, err)
	}

	// Associate with that account
	method := "POST"
	path := "/assoc-account"

	origin := "https://my.webapp.com:443"
	data := assocAccountInput{
		Name:   accountName,
		Origin: origin,
	}

	dataJson, err := json.Marshal(data)
	if err != nil {
		t.Fatalf("json.Marshal(%v) failed: %v", data, err)
	}

	dataJsonReader := bytes.NewReader(dataJson)
	req, err := http.NewRequest(method, path, (dataJsonReader))
	if err != nil {
		t.Fatalf("http.NewRequest(%v, %v, %v,) failed: %v", method, path, dataJsonReader, err)
	}

	resp := httptest.NewRecorder()
	wspr.handleAssocAccount(resp, req)
	if resp.Code != 200 {
		t.Fatalf("Expected handleAssocAccount to return 200 OK, instead got %v", resp)
	}

	// Verify that principalManager has the correct principal for the origin
	got, err := wspr.principalManager.Principal(origin)
	if err != nil {
		t.Fatalf("wspr.principalManager.Principal(%v) failed: %v", origin, err)
	}

	if got == nil {
		t.Fatalf("Expected wspr.principalManager.Principal(%v) to return a valid principal, but got %v", origin, got)
	}
}

func TestHandleAssocAccountWithMissingAccount(t *testing.T) {
	wspr, teardown := setup(t)
	defer teardown()

	method := "POST"
	path := "/assoc-account"

	accountName := "mock-account"
	origin := "https://my.webapp.com:443"
	data := assocAccountInput{
		Name:   accountName,
		Origin: origin,
	}

	dataJson, err := json.Marshal(data)
	if err != nil {
		t.Fatalf("json.Marshal(%v) failed: %v", data, err)
	}

	dataJsonReader := bytes.NewReader(dataJson)
	req, err := http.NewRequest(method, path, (dataJsonReader))
	if err != nil {
		t.Fatalf("http.NewRequest(%v, %v, %v,) failed: %v", method, path, dataJsonReader, err)
	}

	// Verify that the request fails with 400 Bad Request error
	resp := httptest.NewRecorder()
	wspr.handleAssocAccount(resp, req)
	if resp.Code != 400 {
		t.Fatalf("Expected handleAssocAccount to return 400 error, but got %v", resp)
	}

	// Verify that principalManager creates no principal for the origin
	got, err := wspr.principalManager.Principal(origin)
	if err == nil {
		t.Fatalf("Expected wspr.principalManager.Principal(%v) to fail, but got: %v", origin, got)
	}

	if got != nil {
		t.Fatalf("Expected wspr.principalManager.Principal(%v) not to return a principal, but got %v", origin, got)
	}
}
<|endoftext|>


<commit_before>package gitsync

import (
	"fmt"
	"os/exec"
	"regexp"
)

// match: '* master 98ce09c goo'
var branchLineRE = regexp.MustCompile("([* ]) ((?:[a-zA-Z0-9/-_]+)|(?:[(]no branch[)]))[ ]+([0-9a-f]{40}) .*")

// Repo represents a git repository. It provides the basic interrogation
// abilities.
type Repo interface {
	fmt.Stringer

	Branches() (branches []*GitChange, err error)
}

// cliReader is a Repo that shells out to the git CLI to interrogate the git
// repo
type cliReader struct {
	path string // directory containing repo
}

func NewCliRepo(path string) (repo *cliReader, err error) {
	return &cliReader{path: path}, nil
}

func (repo *cliReader) String() string {
	return repo.path
}

// Branches reads all branches in a git repo
func (repo *cliReader) Branches() (branches []*GitChange, err error) {
	cmd := exec.Command("git", "branch", "-av", "--abbrev=40")
	cmd.Dir = repo.path
	out, err := cmd.CombinedOutput()
	if err != nil {
		return nil, err
	}

	for _, m := range branchLineRE.FindAllStringSubmatch(string(out), -1) {
		branches = append(branches, &GitChange{
			RefName:    m[2],
			Current:    m[3],
			CheckedOut: m[1] == "*",
		})
	}

	return
}
<commit_msg>Fix branch line regex bug<commit_after>package gitsync

import (
	"fmt"
	"os/exec"
	"regexp"
)

// match: '* master 98ce09c goo'
var branchLineRE = regexp.MustCompile(`([* ]) ((?:[a-zA-Z0-9/\-_]+)|(?:[(]no branch[)]))[ ]+([0-9a-f]{40}) .*`)

// Repo represents a git repository. It provides the basic interrogation
// abilities.
type Repo interface {
	fmt.Stringer

	Branches() (branches []*GitChange, err error)
}

// cliReader is a Repo that shells out to the git CLI to interrogate the git
// repo
type cliReader struct {
	path string // directory containing repo
}

func NewCliRepo(path string) (repo *cliReader, err error) {
	return &cliReader{path: path}, nil
}

func (repo *cliReader) String() string {
	return repo.path
}

// Branches reads all branches in a git repo
func (repo *cliReader) Branches() (branches []*GitChange, err error) {
	cmd := exec.Command("git", "branch", "-av", "--abbrev=40")
	cmd.Dir = repo.path
	out, err := cmd.CombinedOutput()
	if err != nil {
		return nil, err
	}

	for _, m := range branchLineRE.FindAllStringSubmatch(string(out), -1) {
		branches = append(branches, &GitChange{
			RefName:    m[2],
			Current:    m[3],
			CheckedOut: m[1] == "*",
		})
	}

	return
}
<|endoftext|>


<commit_before>package forms

import (
	"fmt"
	"github.com/stretchr/testify/assert"
	"testing"
)

type ValidatorResults []struct {
	validator Validator
	test      string
	result    bool
}

type ValidatorTestsSet struct {
	name    string
	results ValidatorResults
}

func executeValidatorTests(t *testing.T, results ValidatorTestsSet) {
	for _, result := range results.results {
		r, _ := result.validator.IsValid([]string{result.test})
		if result.result {
			assert.True(
				t, r, fmt.Sprintf(
					"%s validator for %s should pass",
					results.name, result.validator))
		} else {
			assert.False(
				t, r, fmt.Sprintf(
					"%s validator for %s shouldn't pass",
					results.name, result.validator))
		}
	}
}

func TestRequiredValidator(t *testing.T) {
	var results = ValidatorTestsSet{
		name: "Required",
		results: ValidatorResults{
			{&Required{}, "asd", true},
			{&Required{}, "", false},
		},
	}

	executeValidatorTests(t, results)
}

func TestRegexpValidator(t *testing.T) {
	var results = ValidatorTestsSet{
		name: "Regexp",
		results: ValidatorResults{
			{&Regexp{}, "asd", false},
			{&Regexp{""}, "asd", false},
			{&Regexp{"^[a-d]*$"}, "accdddaabbcc", true},
			{&Regexp{"^[a-d]*$"}, "accdddaabbcce", false},
			{&Regexp{"[0-9]*"}, "accddd123aabbcce", true},
			{&Regexp{"^[0-9]*$"}, "accddd123aabbcce", false},
		},
	}

	executeValidatorTests(t, results)
}

func TestEmailValidator(t *testing.T) {
	var results = ValidatorTestsSet{
		name: "Email",
		results: ValidatorResults{
			{&Email{}, "foo", false},
			{&Email{}, "foo@ham", false},
			{&Email{}, "foo@ham.p", false},
			{&Email{}, "foo@ham.pl", true},
			{&Email{}, "foo+bar@ham", false},
			{&Email{}, "foo+bar@ham.pl", true},

			{&Email{}, "foo@h_am.pl", false},
			{&Email{}, "foo+bar@h_am.pl", false},
		},
	}

	executeValidatorTests(t, results)
}

func TestMinLengthValidator(t *testing.T) {
	var results = ValidatorTestsSet{
		name: "MinLength",
		results: ValidatorResults{
			{&MinLength{Min: 2}, "foo", true},
			{&MinLength{Min: 3}, "foo", true},
			{&MinLength{Min: 4}, "foo", false},
		},
	}

	executeValidatorTests(t, results)
}

func TestMaxLengthValidator(t *testing.T) {
	var results = ValidatorTestsSet{
		name: "MaxLength",
		results: ValidatorResults{
			{&MaxLength{Max: 2}, "foo", false},
			{&MaxLength{Max: 3}, "foo", true},
			{&MaxLength{Max: 4}, "foo", true},
		},
	}

	executeValidatorTests(t, results)
}
<commit_msg>Now check also returned message with checking validator<commit_after>package forms

import (
	"fmt"
	"github.com/stretchr/testify/assert"
	"testing"
)

type ValidatorResults []struct {
	validator Validator
	test      string
	result    bool
	message   string
}

type ValidatorTestsSet struct {
	name    string
	results ValidatorResults
}

func executeValidatorTests(t *testing.T, results ValidatorTestsSet) {
	for _, result := range results.results {
		r, m := result.validator.IsValid([]string{result.test})
		assert.Equal(t, m, result.message)
		if result.result {
			assert.True(
				t, r, fmt.Sprintf(
					"%s validator for %s should pass",
					results.name, result.validator))
		} else {
			assert.False(
				t, r, fmt.Sprintf(
					"%s validator for %s shouldn't pass",
					results.name, result.validator))
		}
	}
}

func TestRequiredValidator(t *testing.T) {
	var results = ValidatorTestsSet{
		name: "Required",
		results: ValidatorResults{
			{&Required{}, "asd", true, ""},
			{&Required{}, "", false, translations["REQUIRED"]},
		},
	}

	executeValidatorTests(t, results)
}

func TestRegexpValidator(t *testing.T) {
	var results = ValidatorTestsSet{
		name: "Regexp",
		results: ValidatorResults{
			{&Regexp{}, "asd", false, fmt.Sprintf(translations["NO_MATCH_PATTERN"], "")},
			{&Regexp{""}, "asd", false, fmt.Sprintf(translations["NO_MATCH_PATTERN"], "")},
			{&Regexp{"^[a-d]*$"}, "accdddaabbcc", true, ""},
			{&Regexp{"^[a-d]*$"}, "accdddaabbcce", false, fmt.Sprintf(translations["NO_MATCH_PATTERN"], "^[a-d]*$")},
			{&Regexp{"[0-9]*"}, "accddd123aabbcce", true, ""},
			{&Regexp{"^[0-9]*$"}, "accddd123aabbcce", false, fmt.Sprintf(translations["NO_MATCH_PATTERN"], "^[0-9]*$")},
		},
	}

	executeValidatorTests(t, results)
}

func TestEmailValidator(t *testing.T) {
	var results = ValidatorTestsSet{
		name: "Email",
		results: ValidatorResults{
			{&Email{}, "foo", false, translations["INCORRECT_EMAIL"]},
			{&Email{}, "foo@ham", false, translations["INCORRECT_EMAIL"]},
			{&Email{}, "foo@ham.p", false, translations["INCORRECT_EMAIL"]},
			{&Email{}, "foo@ham.pl", true, ""},
			{&Email{}, "foo+bar@ham", false, translations["INCORRECT_EMAIL"]},
			{&Email{}, "foo+bar@ham.pl", true, ""},

			{&Email{}, "foo@h_am.pl", false, translations["INCORRECT_EMAIL"]},
			{&Email{}, "foo+bar@h_am.pl", false, translations["INCORRECT_EMAIL"]},
		},
	}

	executeValidatorTests(t, results)
}

func TestMinLengthValidator(t *testing.T) {
	var results = ValidatorTestsSet{
		name: "MinLength",
		results: ValidatorResults{
			{&MinLength{Min: 2}, "foo", true, ""},
			{&MinLength{Min: 3}, "foo", true, ""},
			{&MinLength{Min: 4}, "foo", false, fmt.Sprintf(translations["INCORRECT_MIN_LENGTH"], 4)},
		},
	}

	executeValidatorTests(t, results)
}

func TestMaxLengthValidator(t *testing.T) {
	var results = ValidatorTestsSet{
		name: "MaxLength",
		results: ValidatorResults{
			{&MaxLength{Max: 2}, "foo", false, fmt.Sprintf(translations["INCORRECT_MAX_LENGTH"], 2)},
			{&MaxLength{Max: 3}, "foo", true, ""},
			{&MaxLength{Max: 4}, "foo", true, ""},
		},
	}

	executeValidatorTests(t, results)
}
<|endoftext|>


<commit_before>/*
run:
	docker run --name raphanus --rm --publish 8771:8771 msoap/raphanus
	docker run --name redis --rm --publish 6379:6379 redis
	docker run --name memcache --rm --publish 11211:11211 memcached
	make run-benchmark
*/
package raphanus_test

import (
	"os"
	"strconv"
	"testing"

	"github.com/boltdb/bolt"
	"github.com/bradfitz/gomemcache/memcache"
	"github.com/mediocregopher/radix.v2/redis"
	"github.com/msoap/raphanus"
	"github.com/msoap/raphanus/client"
	"github.com/stretchr/testify/require"
)

func Benchmark_raphanusServer(b *testing.B) {
	raph := raphanusclient.New(raphanusclient.Cfg{Address: "http://localhost:8771"})

	for i := 0; i < b.N; i++ {
		strI := strconv.Itoa(i)
		err := raph.SetStr("key_"+strI, "bar_"+strI, 0)
		require.NoError(b, err)

		newVal, err := raph.GetStr("key_" + strI)
		require.NoError(b, err)
		require.Equal(b, newVal, "bar_"+strI)
	}
}

func Benchmark_raphanusEmbed(b *testing.B) {
	raph := raphanus.New()

	for i := 0; i < b.N; i++ {
		strI := strconv.Itoa(i)
		err := raph.SetStr("key_"+strI, "bar_"+strI, 0)
		require.NoError(b, err)

		newVal, err := raph.GetStr("key_" + strI)
		require.NoError(b, err)
		require.Equal(b, newVal, "bar_"+strI)
	}
}

func Benchmark_boltdb(b *testing.B) {
	db, err := bolt.Open("bolt_bench_tmp.db", 0600, nil)
	require.NoError(b, err)
	defer func() {
		require.NoError(b, db.Close())
		require.NoError(b, os.Remove("bolt_bench_tmp.db"))
	}()

	err = db.Update(func(tx *bolt.Tx) error {
		_, err = tx.CreateBucketIfNotExists([]byte("bucket"))
		return err
	})
	require.NoError(b, err)

	for i := 0; i < b.N; i++ {
		strI := strconv.Itoa(i)

		err := db.Update(func(tx *bolt.Tx) error {
			return tx.Bucket([]byte("bucket")).Put([]byte("key_"+strI), []byte("bar_"+strI))
		})
		require.NoError(b, err)

		err = db.View(func(tx *bolt.Tx) error {
			newVal := tx.Bucket([]byte("bucket")).Get([]byte("key_" + strI))
			require.Equal(b, string(newVal), "bar_"+strI)
			return nil
		})
		require.NoError(b, err)
	}
}

func Benchmark_redis(b *testing.B) {
	redisCli, err := redis.Dial("tcp", "localhost:6379")
	require.NoError(b, err)

	for i := 0; i < b.N; i++ {
		strI := strconv.Itoa(i)
		err = redisCli.Cmd("SET", "key_"+strI, "bar_"+strI).Err
		require.NoError(b, err)

		newVal, err := redisCli.Cmd("GET", "key_"+strI).Str()
		require.NoError(b, err)
		require.Equal(b, newVal, "bar_"+strI)
	}
}

func Benchmark_memcache(b *testing.B) {
	mc := memcache.New("localhost:11211")

	for i := 0; i < b.N; i++ {
		strI := strconv.Itoa(i)
		err := mc.Set(&memcache.Item{Key: "key_" + strI, Value: []byte("bar_" + strI)})
		require.NoError(b, err)

		item, err := mc.Get("key_" + strI)
		require.NoError(b, err)
		newVal := item.Value
		require.Equal(b, string(newVal), "bar_"+strI)
	}
}

func Benchmark_raphanusServerTTL(b *testing.B) {
	raph := raphanusclient.New(raphanusclient.Cfg{Address: "http://localhost:8771"})

	for i := 0; i < b.N; i++ {
		strI := strconv.Itoa(i)
		err := raph.SetStr("key_"+strI, "bar_"+strI, 2)
		require.NoError(b, err)

		newVal, err := raph.GetStr("key_" + strI)
		if err != nil {
			// skip deleted keys error
			continue
		}
		require.Equal(b, newVal, "bar_"+strI)
	}
}

func Benchmark_raphanusEmbedTTL(b *testing.B) {
	raph := raphanus.New()

	for i := 0; i < b.N; i++ {
		strI := strconv.Itoa(i)
		err := raph.SetStr("key_"+strI, "bar_"+strI, 2)
		require.NoError(b, err)

		newVal, err := raph.GetStr("key_" + strI)
		require.NoError(b, err)
		require.Equal(b, newVal, "bar_"+strI)
	}
}

func Benchmark_redisTTL(b *testing.B) {
	redisCli, err := redis.Dial("tcp", "localhost:6379")
	require.NoError(b, err)

	for i := 0; i < b.N; i++ {
		strI := strconv.Itoa(i)
		err = redisCli.Cmd("SET", "key_"+strI, "bar_"+strI, "EX", 2).Err
		require.NoError(b, err)

		newVal, err := redisCli.Cmd("GET", "key_"+strI).Str()
		require.NoError(b, err)
		require.Equal(b, newVal, "bar_"+strI)
	}
}

func Benchmark_memcacheTTL(b *testing.B) {
	mc := memcache.New("localhost:11211")

	for i := 0; i < b.N; i++ {
		strI := strconv.Itoa(i)
		err := mc.Set(&memcache.Item{Key: "key_" + strI, Value: []byte("bar_" + strI), Expiration: 2})
		require.NoError(b, err)

		item, err := mc.Get("key_" + strI)
		require.NoError(b, err)
		newVal := item.Value
		require.Equal(b, string(newVal), "bar_"+strI)
	}
}
<commit_msg>gofmt with Go 1.19<commit_after>/*
run:

	docker run --name raphanus --rm --publish 8771:8771 msoap/raphanus
	docker run --name redis --rm --publish 6379:6379 redis
	docker run --name memcache --rm --publish 11211:11211 memcached
	make run-benchmark
*/
package raphanus_test

import (
	"os"
	"strconv"
	"testing"

	"github.com/boltdb/bolt"
	"github.com/bradfitz/gomemcache/memcache"
	"github.com/mediocregopher/radix.v2/redis"
	"github.com/msoap/raphanus"
	raphanusclient "github.com/msoap/raphanus/client"
	"github.com/stretchr/testify/require"
)

func Benchmark_raphanusServer(b *testing.B) {
	raph := raphanusclient.New(raphanusclient.Cfg{Address: "http://localhost:8771"})

	for i := 0; i < b.N; i++ {
		strI := strconv.Itoa(i)
		err := raph.SetStr("key_"+strI, "bar_"+strI, 0)
		require.NoError(b, err)

		newVal, err := raph.GetStr("key_" + strI)
		require.NoError(b, err)
		require.Equal(b, newVal, "bar_"+strI)
	}
}

func Benchmark_raphanusEmbed(b *testing.B) {
	raph := raphanus.New()

	for i := 0; i < b.N; i++ {
		strI := strconv.Itoa(i)
		err := raph.SetStr("key_"+strI, "bar_"+strI, 0)
		require.NoError(b, err)

		newVal, err := raph.GetStr("key_" + strI)
		require.NoError(b, err)
		require.Equal(b, newVal, "bar_"+strI)
	}
}

func Benchmark_boltdb(b *testing.B) {
	db, err := bolt.Open("bolt_bench_tmp.db", 0600, nil)
	require.NoError(b, err)
	defer func() {
		require.NoError(b, db.Close())
		require.NoError(b, os.Remove("bolt_bench_tmp.db"))
	}()

	err = db.Update(func(tx *bolt.Tx) error {
		_, err = tx.CreateBucketIfNotExists([]byte("bucket"))
		return err
	})
	require.NoError(b, err)

	for i := 0; i < b.N; i++ {
		strI := strconv.Itoa(i)

		err := db.Update(func(tx *bolt.Tx) error {
			return tx.Bucket([]byte("bucket")).Put([]byte("key_"+strI), []byte("bar_"+strI))
		})
		require.NoError(b, err)

		err = db.View(func(tx *bolt.Tx) error {
			newVal := tx.Bucket([]byte("bucket")).Get([]byte("key_" + strI))
			require.Equal(b, string(newVal), "bar_"+strI)
			return nil
		})
		require.NoError(b, err)
	}
}

func Benchmark_redis(b *testing.B) {
	redisCli, err := redis.Dial("tcp", "localhost:6379")
	require.NoError(b, err)

	for i := 0; i < b.N; i++ {
		strI := strconv.Itoa(i)
		err = redisCli.Cmd("SET", "key_"+strI, "bar_"+strI).Err
		require.NoError(b, err)

		newVal, err := redisCli.Cmd("GET", "key_"+strI).Str()
		require.NoError(b, err)
		require.Equal(b, newVal, "bar_"+strI)
	}
}

func Benchmark_memcache(b *testing.B) {
	mc := memcache.New("localhost:11211")

	for i := 0; i < b.N; i++ {
		strI := strconv.Itoa(i)
		err := mc.Set(&memcache.Item{Key: "key_" + strI, Value: []byte("bar_" + strI)})
		require.NoError(b, err)

		item, err := mc.Get("key_" + strI)
		require.NoError(b, err)
		newVal := item.Value
		require.Equal(b, string(newVal), "bar_"+strI)
	}
}

func Benchmark_raphanusServerTTL(b *testing.B) {
	raph := raphanusclient.New(raphanusclient.Cfg{Address: "http://localhost:8771"})

	for i := 0; i < b.N; i++ {
		strI := strconv.Itoa(i)
		err := raph.SetStr("key_"+strI, "bar_"+strI, 2)
		require.NoError(b, err)

		newVal, err := raph.GetStr("key_" + strI)
		if err != nil {
			// skip deleted keys error
			continue
		}
		require.Equal(b, newVal, "bar_"+strI)
	}
}

func Benchmark_raphanusEmbedTTL(b *testing.B) {
	raph := raphanus.New()

	for i := 0; i < b.N; i++ {
		strI := strconv.Itoa(i)
		err := raph.SetStr("key_"+strI, "bar_"+strI, 2)
		require.NoError(b, err)

		newVal, err := raph.GetStr("key_" + strI)
		require.NoError(b, err)
		require.Equal(b, newVal, "bar_"+strI)
	}
}

func Benchmark_redisTTL(b *testing.B) {
	redisCli, err := redis.Dial("tcp", "localhost:6379")
	require.NoError(b, err)

	for i := 0; i < b.N; i++ {
		strI := strconv.Itoa(i)
		err = redisCli.Cmd("SET", "key_"+strI, "bar_"+strI, "EX", 2).Err
		require.NoError(b, err)

		newVal, err := redisCli.Cmd("GET", "key_"+strI).Str()
		require.NoError(b, err)
		require.Equal(b, newVal, "bar_"+strI)
	}
}

func Benchmark_memcacheTTL(b *testing.B) {
	mc := memcache.New("localhost:11211")

	for i := 0; i < b.N; i++ {
		strI := strconv.Itoa(i)
		err := mc.Set(&memcache.Item{Key: "key_" + strI, Value: []byte("bar_" + strI), Expiration: 2})
		require.NoError(b, err)

		item, err := mc.Get("key_" + strI)
		require.NoError(b, err)
		newVal := item.Value
		require.Equal(b, string(newVal), "bar_"+strI)
	}
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"github.com/codegangsta/cli"
	"github.com/docker/libswarm/backends"
	"github.com/docker/libswarm/beam"
	_ "github.com/dotcloud/docker/api/server"
	"github.com/flynn/go-shlex"
	"io"
	"log"
	"os"
	"strings"
)

func main() {
	app := cli.NewApp()
	app.Name = "swarmd"
	app.Usage = "Control a heterogenous distributed system with the Docker API"
	app.Version = "0.0.1"
	app.Flags = []cli.Flag{
		cli.StringFlag{"backend", "debug", "load a backend"},
	}
	app.Action = cmdDaemon
	app.Run(os.Args)
}

func cmdDaemon(c *cli.Context) {
	app := beam.NewServer()
	app.OnLog(beam.Handler(func(msg *beam.Message) error {
		log.Printf("%s\n", strings.Join(msg.Args, " "))
		return nil
	}))
	app.OnError(beam.Handler(func(msg *beam.Message) error {
		Fatalf("Fatal: %v", strings.Join(msg.Args[:1], ""))
		return nil
	}))
	back := backends.New()
	if len(c.Args()) == 0 {
		names, err := back.Ls()
		if err != nil {
			Fatalf("ls: %v", err)
		}
		fmt.Println(strings.Join(names, "\n"))
		return
	}
	bName, bArgs, err := parseCmd(c.Args()[0])
	if err != nil {
		Fatalf("parse: %v", err)
	}
	fmt.Printf("---> Loading backend '%s'\n", strings.Join(append([]string{bName}, bArgs...), " "))
	events, backend, err := back.Attach(bName)
	if err != nil {
		Fatalf("%s: %v\n", bName, err)
	}
	instance, err := backend.Spawn(bArgs...)
	if err != nil {
		Fatalf("spawn %s: %v\n", bName, err)
	}
	if err != nil {
		Fatalf("attach: %v", err)
	}
	if err := instance.Start(); err != nil {
		Fatalf("start: %v", err)
	}
	beam.Copy(app, events)
}

func parseCmd(txt string) (string, []string, error) {
	l, err := shlex.NewLexer(strings.NewReader(txt))
	if err != nil {
		return "", nil, err
	}
	var cmd []string
	for {
		word, err := l.NextWord()
		if err == io.EOF {
			break
		}
		if err != nil {
			return "", nil, err
		}
		cmd = append(cmd, word)
	}
	if len(cmd) == 0 {
		return "", nil, fmt.Errorf("parse error: empty command")
	}
	return cmd[0], cmd[1:], nil
}

func Fatalf(msg string, args ...interface{}) {
	if !strings.HasSuffix(msg, "\n") {
		msg = msg + "\n"
	}
	fmt.Fprintf(os.Stderr, msg, args...)
	os.Exit(1)
}
<commit_msg>Attach to backend instance before starting it<commit_after>package main

import (
	"fmt"
	"github.com/codegangsta/cli"
	"github.com/docker/libswarm/backends"
	"github.com/docker/libswarm/beam"
	_ "github.com/dotcloud/docker/api/server"
	"github.com/flynn/go-shlex"
	"io"
	"log"
	"os"
	"strings"
)

func main() {
	app := cli.NewApp()
	app.Name = "swarmd"
	app.Usage = "Control a heterogenous distributed system with the Docker API"
	app.Version = "0.0.1"
	app.Flags = []cli.Flag{
		cli.StringFlag{"backend", "debug", "load a backend"},
	}
	app.Action = cmdDaemon
	app.Run(os.Args)
}

func cmdDaemon(c *cli.Context) {
	app := beam.NewServer()
	app.OnLog(beam.Handler(func(msg *beam.Message) error {
		log.Printf("%s\n", strings.Join(msg.Args, " "))
		return nil
	}))
	app.OnError(beam.Handler(func(msg *beam.Message) error {
		Fatalf("Fatal: %v", strings.Join(msg.Args[:1], ""))
		return nil
	}))
	back := backends.New()
	if len(c.Args()) == 0 {
		names, err := back.Ls()
		if err != nil {
			Fatalf("ls: %v", err)
		}
		fmt.Println(strings.Join(names, "\n"))
		return
	}
	bName, bArgs, err := parseCmd(c.Args()[0])
	if err != nil {
		Fatalf("parse: %v", err)
	}
	fmt.Printf("---> Loading backend '%s'\n", strings.Join(append([]string{bName}, bArgs...), " "))
	_, backend, err := back.Attach(bName)
	if err != nil {
		Fatalf("%s: %v\n", bName, err)
	}
	fmt.Printf("---> Spawning\n")
	instance, err := backend.Spawn(bArgs...)
	if err != nil {
		Fatalf("spawn %s: %v\n", bName, err)
	}
	fmt.Printf("---> Attaching\n")
	instanceIn, _, err := instance.Attach("")
	if err != nil {
		Fatalf("attach: %v", err)
	}
	fmt.Printf("---> Starting\n")
	if err := instance.Start(); err != nil {
		Fatalf("start: %v", err)
	}
	_, err = beam.Copy(app, instanceIn)
	if err != nil {
		Fatalf("copy: %v", err)
	}
}

func parseCmd(txt string) (string, []string, error) {
	l, err := shlex.NewLexer(strings.NewReader(txt))
	if err != nil {
		return "", nil, err
	}
	var cmd []string
	for {
		word, err := l.NextWord()
		if err == io.EOF {
			break
		}
		if err != nil {
			return "", nil, err
		}
		cmd = append(cmd, word)
	}
	if len(cmd) == 0 {
		return "", nil, fmt.Errorf("parse error: empty command")
	}
	return cmd[0], cmd[1:], nil
}

func Fatalf(msg string, args ...interface{}) {
	if !strings.HasSuffix(msg, "\n") {
		msg = msg + "\n"
	}
	fmt.Fprintf(os.Stderr, msg, args...)
	os.Exit(1)
}
<|endoftext|>