<commit_before>package chipmunk

import "fmt"

var authEmails []string = []string{"derekmcquay@gmail.com", "colleenmmcquay@gmail.com"}

type user struct {
	Info  userInfo `json:"info"`
	admin bool     `json:"admin"`
}

type userInfo struct {
	Sub           string `json:"sub"`
	Name          string `json:"name"`
	GivenName     string `json:"given_name"`
	FamilyName    string `json:"family_name"`
	Profile       string `json:"profile"`
	Picture       string `json:"picture"`
	Email         string `json:"email"`
	EmailVerified bool   `json:"email_verified"`
}

//authorizedEmail checks whether the email coming in is in the preapproved list
func authorizedEmail(e string) bool {
	b := false
	for _, i := range authEmails {
		if i == e {
			b = true
		}
	}
	return b
}

// getUser returns index of user with given email, otherwise it returns an
// error that it could not find that user
func getUser(e string) (int, error) {
	for i, u := range users {
		if e == u.Info.Email {
			return i, nil
		}
	}
	return 0, fmt.Errorf("could not find user")
}

//addUser adds user to slice of users
func addUser(u userInfo) {
	_, err := getUser(u.Email)
	if err != nil {
		users = append(
			users,
			user{
				Info:  u,
				admin: true,
			},
		)
	}
}
<commit_msg>updated user type to fit psql<commit_after>package chipmunk

import "fmt"

var authEmails []string = []string{"derekmcquay@gmail.com", "colleenmmcquay@gmail.com"}

type user struct {
	ID    int    `json:"id"`
	Email string `json:"email"`
	Admin bool   `json:"admin"`
}

type userInfo struct {
	Sub           string `json:"sub"`
	Name          string `json:"name"`
	GivenName     string `json:"given_name"`
	FamilyName    string `json:"family_name"`
	Profile       string `json:"profile"`
	Picture       string `json:"picture"`
	Email         string `json:"email"`
	EmailVerified bool   `json:"email_verified"`
}

//authorizedEmail checks whether the email coming in is in the preapproved list
func authorizedEmail(e string) bool {
	b := false
	for _, i := range authEmails {
		if i == e {
			b = true
		}
	}
	return b
}

// getUser returns index of user with given email, otherwise it returns an
// error that it could not find that user
func getUser(e string) (int, error) {
	for i, u := range users {
		if e == u.Email {
			return i, nil
		}
	}
	return 0, fmt.Errorf("could not find user")
}
<|endoftext|>


<commit_before>package pointer

import (
	"crypto/sha256"
	"encoding/hex"
	"github.com/github/git-media/gitmedia"
	"io"
	"os"
)

type CleanedAsset struct {
	File          *os.File
	mediafilepath string
	*Pointer
}

func Clean(reader io.Reader) (*CleanedAsset, error) {
	tmp, err := gitmedia.TempFile()
	if err != nil {
		return nil, err
	}

	oidHash := sha256.New()
	writer := io.MultiWriter(oidHash, tmp)
	written, err := io.Copy(writer, reader)

	pointer := NewPointer(hex.EncodeToString(oidHash.Sum(nil)), written)
	return &CleanedAsset{tmp, "", pointer}, err
}

func (a *CleanedAsset) Close() error {
	return os.Remove(a.File.Name())
}
<commit_msg>dont expose pointer.CleanedAsset<commit_after>package pointer

import (
	"crypto/sha256"
	"encoding/hex"
	"github.com/github/git-media/gitmedia"
	"io"
	"os"
)

type cleanedAsset struct {
	File          *os.File
	mediafilepath string
	*Pointer
}

func Clean(reader io.Reader) (*cleanedAsset, error) {
	tmp, err := gitmedia.TempFile()
	if err != nil {
		return nil, err
	}

	oidHash := sha256.New()
	writer := io.MultiWriter(oidHash, tmp)
	written, err := io.Copy(writer, reader)

	pointer := NewPointer(hex.EncodeToString(oidHash.Sum(nil)), written)
	return &cleanedAsset{tmp, "", pointer}, err
}

func (a *cleanedAsset) Close() error {
	return os.Remove(a.File.Name())
}
<|endoftext|>


<commit_before>package arts

import (
	"bytes"
	"code.google.com/p/cascadia"
	"code.google.com/p/go.net/html"
	"mime"
	"strings"
	"unicode/utf8"
)

// some background:
// http://www.w3.org/International/questions/qa-html-encoding-declarations#quicklookup

var metaCharsetSelector = cascadia.MustCompile(`meta[charset], meta[http-equiv="Content-Type"]`)

// findCharset returns the character encoding to be used to interpret the
// page's content.
// by Andy Balholm
// https://groups.google.com/forum/?fromgroups=#!topic/golang-nuts/Qq5hTQyPuLg
// TODO: not happy that iso-8859-1 is always changed to windows-1252.
// Looks like there are some specific indicators we could check for. see:
// http://i18nqa.com/debug/table-iso8859-1-vs-windows-1252.html#compare
func findCharset(declaredContentType string, content []byte) (charset string) {
	defer func() {
		if ce := compatibilityEncodings[charset]; ce != "" {
			charset = ce
		}
	}()

	cs := charsetFromContentType(declaredContentType)
	if cs != "" {
		return cs
	}

	// NOTE (BenC): original limit was 1024: too small for some real-world cases
	// eg http://www.dailymail.co.uk/news/article-2314971/Freedom-press-Newspapers-launch-Royal-Charter-teeth-protect-public.html
	if len(content) > 2048 {
		content = content[:2048]
	}

	if len(content) >= 2 {
		if content[0] == 0xfe && content[1] == 0xff {
			return "utf-16be"
		}
		if content[0] == 0xff && content[1] == 0xfe {
			return "utf-16le"
		}
	}

	if len(content) >= 3 && content[0] == 0xef && content[1] == 0xbb && content[2] == 0xbf {
		return "utf-8"
	}

	if strings.Contains(declaredContentType, "html") || declaredContentType == "" {
		// Look for a <meta> tag giving the encoding.
		tree, err := html.Parse(bytes.NewBuffer(content))
		if err == nil {
			for _, n := range metaCharsetSelector.MatchAll(tree) {
				a := make(map[string]string)
				for _, attr := range n.Attr {
					a[attr.Key] = attr.Val
				}
				if charsetAttr := a["charset"]; charsetAttr != "" {
					return strings.ToLower(charsetAttr)
				}
				if strings.EqualFold(a["http-equiv"], "Content-Type") {
					cs = charsetFromContentType(a["content"])
					if cs != "" {
						return cs
					}
				}
			}
		}
	}

	// Try to detect UTF-8.
	// First eliminate any partial rune that may be split by the 1024-byte boundary.
	for i := len(content) - 1; i >= 0 && i > len(content)-4; i-- {
		b := content[i]
		if b < 128 {
			break
		}
		if utf8.RuneStart(b) {
			content = content[:i]
			break
		}
	}
	if utf8.Valid(content) {
		return "utf-8"
	}

	return "windows-1252"
}

func charsetFromContentType(t string) string {
	t = strings.ToLower(t)
	_, params, _ := mime.ParseMediaType(t)
	return params["charset"]
}

// compatibilityEncodings contains character sets that should be misinterpreted
// for compatibility. The encodings that are commented out are not yet
// implemented by the Mahonia library.
var compatibilityEncodings = map[string]string{
	//	"euc-kr":         "windows-949",
	//	"euc-jp":         "cp51932",
	"gb2312":     "gbk",
	"gb_2312-80": "gbk",
	//	"iso-2022-jp":    "cp50220",
	"iso-8859-1":  "windows-1252",
	"iso-8859-9":  "windows-1254",
	"iso-8859-11": "windows-874",
	//	"ks_c_5601-1987": "windows-949",
	//	"shift_jis":      "windows-31j",
	"tis-620":  "windows-874",
	"us-ascii": "windows-1252",
}
<commit_msg>tweak meta http-equiv parsing<commit_after>package arts

import (
	"bytes"
	"code.google.com/p/cascadia"
	"code.google.com/p/go.net/html"
	"mime"
	"strings"
	"unicode/utf8"
)

// some background:
// http://www.w3.org/International/questions/qa-html-encoding-declarations#quicklookup

var metaCharsetSelector = cascadia.MustCompile(`meta[charset], meta[http-equiv]`)

// findCharset returns the character encoding to be used to interpret the
// page's content.
// by Andy Balholm
// https://groups.google.com/forum/?fromgroups=#!topic/golang-nuts/Qq5hTQyPuLg
// TODO: not happy that iso-8859-1 is always changed to windows-1252.
// Looks like there are some specific indicators we could check for. see:
// http://i18nqa.com/debug/table-iso8859-1-vs-windows-1252.html#compare
func findCharset(declaredContentType string, content []byte) (charset string) {
	defer func() {
		if ce := compatibilityEncodings[charset]; ce != "" {
			charset = ce
		}
	}()

	cs := charsetFromContentType(declaredContentType)
	if cs != "" {
		return cs
	}

	// NOTE (BenC): original limit was 1024: too small for some real-world cases
	// eg http://www.dailymail.co.uk/news/article-2314971/Freedom-press-Newspapers-launch-Royal-Charter-teeth-protect-public.html
	if len(content) > 2048 {
		content = content[:2048]
	}

	if len(content) >= 2 {
		if content[0] == 0xfe && content[1] == 0xff {
			return "utf-16be"
		}
		if content[0] == 0xff && content[1] == 0xfe {
			return "utf-16le"
		}
	}

	if len(content) >= 3 && content[0] == 0xef && content[1] == 0xbb && content[2] == 0xbf {
		return "utf-8"
	}

	if strings.Contains(declaredContentType, "html") || declaredContentType == "" {
		// Look for a <meta> tag giving the encoding.
		tree, err := html.Parse(bytes.NewBuffer(content))
		if err == nil {
			for _, n := range metaCharsetSelector.MatchAll(tree) {
				a := make(map[string]string)
				for _, attr := range n.Attr {
					a[attr.Key] = attr.Val
				}
				if charsetAttr := a["charset"]; charsetAttr != "" {
					return strings.ToLower(charsetAttr)
				}
				if strings.EqualFold(a["http-equiv"], "Content-Type") {
					cs = charsetFromContentType(a["content"])
					if cs != "" {
						return cs
					}
				}
			}
		}
	}

	// Try to detect UTF-8.
	// First eliminate any partial rune that may be split by the 1024-byte boundary.
	for i := len(content) - 1; i >= 0 && i > len(content)-4; i-- {
		b := content[i]
		if b < 128 {
			break
		}
		if utf8.RuneStart(b) {
			content = content[:i]
			break
		}
	}
	if utf8.Valid(content) {
		return "utf-8"
	}

	return "windows-1252"
}

func charsetFromContentType(t string) string {
	t = strings.ToLower(t)
	_, params, _ := mime.ParseMediaType(t)
	return params["charset"]
}

// compatibilityEncodings contains character sets that should be misinterpreted
// for compatibility. The encodings that are commented out are not yet
// implemented by the Mahonia library.
var compatibilityEncodings = map[string]string{
	//	"euc-kr":         "windows-949",
	//	"euc-jp":         "cp51932",
	"gb2312":     "gbk",
	"gb_2312-80": "gbk",
	//	"iso-2022-jp":    "cp50220",
	"iso-8859-1":  "windows-1252",
	"iso-8859-9":  "windows-1254",
	"iso-8859-11": "windows-874",
	//	"ks_c_5601-1987": "windows-949",
	//	"shift_jis":      "windows-31j",
	"tis-620":  "windows-874",
	"us-ascii": "windows-1252",
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"io/ioutil"
	"os"
	"strconv"

	"github.com/cloudfoundry-incubator/route-registrar/config"
	. "github.com/cloudfoundry-incubator/route-registrar/healthchecker"
	. "github.com/cloudfoundry-incubator/route-registrar/registrar"
)

var (
	pidfile = flag.String("pidfile", "", "Location of file to write to.")
)

func main() {
	flag.Parse()
	if *pidfile != "" {
		pid := strconv.Itoa(os.Getpid())
		err := ioutil.WriteFile(*pidfile, []byte(pid), 0644)
		if err != nil {
			log.Fatal("error writing pid %s to file: %s\n", pid, err)
		}
	}

	config, err := config.InitConfigFromFile("registrar_settings.yml")
	if err != nil {
		log.Fatal("error parsing file: %s\n", err)
	}
	registrar := NewRegistrar(config)
	//add health check handler
	checker := InitHealthChecker(config)
	if checker != nil {
		registrar.AddHealthCheckHandler(checker)
	}
	registrar.RegisterRoutes()
	os.Exit(1)
}
<commit_msg>Add missing log import.<commit_after>package main

import (
	"flag"
	"io/ioutil"
	"log"
	"os"
	"strconv"

	"github.com/cloudfoundry-incubator/route-registrar/config"
	. "github.com/cloudfoundry-incubator/route-registrar/healthchecker"
	. "github.com/cloudfoundry-incubator/route-registrar/registrar"
)

var (
	pidfile = flag.String("pidfile", "", "Location of file to write to.")
)

func main() {
	flag.Parse()
	if *pidfile != "" {
		pid := strconv.Itoa(os.Getpid())
		err := ioutil.WriteFile(*pidfile, []byte(pid), 0644)
		if err != nil {
			log.Fatal("error writing pid %s to file: %s\n", pid, err)
		}
	}

	config, err := config.InitConfigFromFile("registrar_settings.yml")
	if err != nil {
		log.Fatal("error parsing file: %s\n", err)
	}
	registrar := NewRegistrar(config)
	//add health check handler
	checker := InitHealthChecker(config)
	if checker != nil {
		registrar.AddHealthCheckHandler(checker)
	}
	registrar.RegisterRoutes()
	os.Exit(1)
}
<|endoftext|>


<commit_before>package quotes

import (
	"database/sql"
	"errors"
	"fmt"
	"log"
	"math/rand"
	"strconv"
	"strings"
	"time"

	"github.com/doenietzomoeilijk/go-chat-bot-plugins/authorization"
	"github.com/go-chat-bot/bot"
	_ "github.com/mattn/go-sqlite3" // Use sqlite3
)

var (
	db *sql.DB
)

// Quote holds a singular quote
type Quote struct {
	ID        uint      `db:"id"`
	Channel   string    `db:"channel"`
	Author    string    `db:"author"`
	Timestamp time.Time `db:"timestamp"`
	Content   string    `db:"content"`
}

/**
 * Set up our DB - make sure it exists and that it has the proper table.
 */
func setupDb() {
	var err error

	db, err = sql.Open("sqlite3", "quotes.db")
	if err != nil {
		log.Fatal(err)
	}

	_, err = db.Exec(`CREATE TABLE IF NOT EXISTS quotes (
        id INTEGER NOT NULL PRIMARY KEY,
        channel VARCHAR(50) NOT NULL DEFAULT 'unknown',
        author VARCHAR(15) NOT NULL DEFAULT 'unknown',
        timestamp DATETIME NULL,
        deleted TINYINT UNSIGNED NOT NULL DEFAULT 0,
        content TEXT
    )`)
	_, err = db.Exec(`CREATE INDEX IF NOT EXISTS chan ON quotes(channel)`)
	_, err = db.Exec(`CREATE INDEX IF NOT EXISTS idel ON quotes(deleted)`)
	_, err = db.Exec(`CREATE INDEX IF NOT EXISTS cont ON quotes(content)`)
}

func addQuote(command *bot.Cmd) (msg string, err error) {
	var insertID int64

	if authorization.Authorize(command.User, command.Channel, "author") == false {
		return
	}

	quote := strings.Join(command.Args, " ")
	if quote == "" {
		return
	}

	res, err := db.Exec(
		`INSERT INTO quotes
        (channel, author, timestamp, content)
        VALUES (?, ?, CURRENT_TIMESTAMP, ?)`,
		command.Channel,
		command.User.Nick,
		quote)

	if err != nil {
		err = errors.New("could not insert quote")
		return
	}

	insertID, err = res.LastInsertId()
	if err == nil {
		msg = fmt.Sprintf("Quote inserted with id %d.", insertID)
	}

	return
}

func queryToQuote(w map[string]interface{}, num uint) (Q Quote) {
	fields := "id, channel, author, timestamp, content"
	query := "SELECT %s FROM quotes WHERE deleted = 0"
	var binds []interface{}

	for where, bind := range w {
		query += fmt.Sprintf(" AND %s", where)
		binds = append(binds, bind)
	}

	var count int
	countRow := db.QueryRow(fmt.Sprintf(query, "COUNT(*)"), binds...)
	err := countRow.Scan(&count)

	if count == 0 || err != nil {
		return
	}

	if count > 1 {
		if num == 0 {
			num = uint(rand.Intn(count)) + 1
		}

		query += fmt.Sprintf(" LIMIT 1 OFFSET %d", num-1)
	}

	query = fmt.Sprintf(query, fields)

	row := db.QueryRow(query, binds...)
	err = row.Scan(&Q.ID, &Q.Channel, &Q.Author, &Q.Timestamp, &Q.Content)
	if count > 1 {
		Q.Content += fmt.Sprintf(" (%d/%d)", num, count)
	}

	if err != nil {
		Q = Quote{}

	}

	return
}

// Fetch a quote from the database
func getQuote(command *bot.Cmd) (msg string, err error) {
	args := command.Args
	var lastArgNum uint

	if len(command.Args) > 0 {
		lastArg := args[len(args)-1]
		lan, err := strconv.Atoi(lastArg)
		if err != nil {
			lastArgNum = uint(0)
			err = nil
		} else {
			lastArgNum = uint(lan)
			args = args[:len(args)-1]

		}
	}

	m := make(map[string]interface{})
	m["channel = ?"] = command.Channel

	switch {
	case len(args) == 1 && args[0] == "-id":
		m["id = ?"] = lastArgNum
	case len(args) > 0:
		m["content LIKE ?"] = fmt.Sprintf("%%%s%%", strings.Join(args, " "))
	}

	Q := queryToQuote(m, lastArgNum)
	if Q.ID != 0 {
		msg = fmt.Sprintf("#%d: %s", Q.ID, Q.Content)
	}

	return
}

func init() {
	setupDb()

	bot.RegisterCommand(
		"addquote",
		"Add a quote to the bot",
		"",
		addQuote)

	bot.RegisterCommand(
		"quote",
		"Get a quote from the bot",
		"[-id <id>|<[querystring] [<nth match>]]",
		getQuote)

	bot.RegisterCommand(
		"q",
		"Alias for !quote",
		"",
		getQuote)

	// todo:
	// - lastquote
	// - delquote (passive, maybe?)
	// - quoteinfo
	// - quotehelp (passive, query only)
}
<commit_msg>Tweak output<commit_after>package quotes

import (
	"database/sql"
	"errors"
	"fmt"
	"log"
	"math/rand"
	"strconv"
	"strings"
	"time"

	"github.com/doenietzomoeilijk/go-chat-bot-plugins/authorization"
	"github.com/go-chat-bot/bot"
	_ "github.com/mattn/go-sqlite3" // Use sqlite3
)

var (
	db *sql.DB
)

// Quote holds a singular quote
type Quote struct {
	ID        uint      `db:"id"`
	Channel   string    `db:"channel"`
	Author    string    `db:"author"`
	Timestamp time.Time `db:"timestamp"`
	Content   string    `db:"content"`
}

/**
 * Set up our DB - make sure it exists and that it has the proper table.
 */
func setupDb() {
	var err error

	db, err = sql.Open("sqlite3", "quotes.db")
	if err != nil {
		log.Fatal(err)
	}

	_, err = db.Exec(`CREATE TABLE IF NOT EXISTS quotes (
        id INTEGER NOT NULL PRIMARY KEY,
        channel VARCHAR(50) NOT NULL DEFAULT 'unknown',
        author VARCHAR(15) NOT NULL DEFAULT 'unknown',
        timestamp DATETIME NULL,
        deleted TINYINT UNSIGNED NOT NULL DEFAULT 0,
        content TEXT
    )`)
	_, err = db.Exec(`CREATE INDEX IF NOT EXISTS chan ON quotes(channel)`)
	_, err = db.Exec(`CREATE INDEX IF NOT EXISTS idel ON quotes(deleted)`)
	_, err = db.Exec(`CREATE INDEX IF NOT EXISTS cont ON quotes(content)`)
}

func addQuote(command *bot.Cmd) (msg string, err error) {
	var insertID int64

	if authorization.Authorize(command.User, command.Channel, "author") == false {
		return
	}

	quote := strings.Join(command.Args, " ")
	if quote == "" {
		return
	}

	res, err := db.Exec(
		`INSERT INTO quotes
        (channel, author, timestamp, content)
        VALUES (?, ?, CURRENT_TIMESTAMP, ?)`,
		command.Channel,
		command.User.Nick,
		quote)

	if err != nil {
		err = errors.New("could not insert quote")
		return
	}

	insertID, err = res.LastInsertId()
	if err == nil {
		msg = fmt.Sprintf("Quote inserted with id %d.", insertID)
	}

	return
}

func queryToQuote(w map[string]interface{}, num uint) (Q Quote) {
	fields := "id, channel, author, timestamp, content"
	query := "SELECT %s FROM quotes WHERE deleted = 0"
	var binds []interface{}
	var usedLike bool

	for where, bind := range w {
		query += fmt.Sprintf(" AND %s", where)
		binds = append(binds, bind)
		if strings.Contains(where, "LIKE") {
			usedLike = true
		}
	}

	var count int
	countRow := db.QueryRow(fmt.Sprintf(query, "COUNT(*)"), binds...)
	err := countRow.Scan(&count)

	if count == 0 || err != nil {
		return
	}

	if count > 1 {
		if num == 0 {
			num = uint(rand.Intn(count)) + 1
		}

		query += fmt.Sprintf(" LIMIT 1 OFFSET %d", num-1)
	}

	query = fmt.Sprintf(query, fields)

	row := db.QueryRow(query, binds...)
	err = row.Scan(&Q.ID, &Q.Channel, &Q.Author, &Q.Timestamp, &Q.Content)
	if count > 1 && usedLike {
		Q.Content += fmt.Sprintf(" (%d/%d)", num, count)
	}

	if err != nil {
		Q = Quote{}

	}

	return
}

// Fetch a quote from the database
func getQuote(command *bot.Cmd) (msg string, err error) {
	args := command.Args
	var lastArgNum uint

	if len(command.Args) > 0 {
		lastArg := args[len(args)-1]
		lan, err := strconv.Atoi(lastArg)
		if err != nil {
			lastArgNum = uint(0)
			err = nil
		} else {
			lastArgNum = uint(lan)
			args = args[:len(args)-1]

		}
	}

	m := make(map[string]interface{})
	m["channel = ?"] = command.Channel

	switch {
	case len(args) == 1 && args[0] == "-id":
		m["id = ?"] = lastArgNum
	case len(args) > 0:
		m["content LIKE ?"] = fmt.Sprintf("%%%s%%", strings.Join(args, " "))
	}

	Q := queryToQuote(m, lastArgNum)
	if Q.ID != 0 {
		msg = fmt.Sprintf("#%d: %s", Q.ID, Q.Content)
	}

	return
}

func init() {
	setupDb()

	bot.RegisterCommand(
		"addquote",
		"Add a quote to the bot",
		"",
		addQuote)

	bot.RegisterCommand(
		"quote",
		"Get a quote from the bot",
		"[-id <id>|<[querystring] [<nth match>]]",
		getQuote)

	bot.RegisterCommand(
		"q",
		"Alias for !quote",
		"",
		getQuote)

	// todo:
	// - lastquote
	// - delquote (passive, maybe?)
	// - quoteinfo
	// - quotehelp (passive, query only)
}
<|endoftext|>


<commit_before>package rabbit

import (
	"os"
	"sync"
	"time"

	log "github.com/cihub/seelog"
	"github.com/streadway/amqp"
)

var (
	RabbitURL string
	Exchange  string
)

// Initialize rabbitmq connection from env vars. The defaults are expected to work
// in development. ci, dev-ci and mesos override these defaults
// @todo Refactor initialization of the connection to allow the "server" abstraction we'll introduce to inject the configuration
// @todo once config server has http interface, we should use values from the config
// server to connect to rabbit
func init() {
	RabbitURL = os.Getenv("RABBIT_URL")
	if RabbitURL == "" {
		RabbitURL = "amqp://admin:guest@192.168.59.103:5672"
		log.Infof("Setting RABBIT_URL to default value %s", RabbitURL)
	}
	log.Infof("Set RABBIT_URL to %s", RabbitURL)
	Exchange = os.Getenv("RABBIT_EXCHANGE")
	if Exchange == "" {
		Exchange = "b2a"
		log.Infof("Setting RABBIT_EXCHANGE to default value %s", Exchange)
	}
	log.Infof("Set RABBIT_EXCHANGE to %s", Exchange)
}

func NewRabbitConnection() *RabbitConnection {
	return &RabbitConnection{
		notify:    make(chan bool, 1),
		closeChan: make(chan struct{}),
	}
}

type RabbitConnection struct {
	Connection      *amqp.Connection
	Channel         *RabbitChannel
	ExchangeChannel *RabbitChannel
	notify          chan bool

	connected bool

	mtx       sync.Mutex
	closeChan chan struct{}
	closed    bool
}

func (r *RabbitConnection) Init() chan bool {
	go r.Connect(r.notify)
	return r.notify
}

func (r *RabbitConnection) Connect(connected chan bool) {
	for {
		log.Debugf("[Rabbit] Attempting to connect")
		if err := r.tryToConnect(); err != nil {
			log.Debugf("[Rabbit] Failed to connect, sleeping 1s")
			time.Sleep(1 * time.Second)
			continue
		}
		connected <- true
		r.connected = true
		notifyClose := make(chan *amqp.Error)
		r.Connection.NotifyClose(notifyClose)

		// Block until we get disconnected, or shut down
		select {
		case err := <-notifyClose:
			// Spin around and reconnect
			r.connected = false
			log.Debugf("[Rabbit] AMQP connection closed (notifyClose): %s", err.Error())
		case <-r.closeChan:
			// Shut down connection
			log.Debugf("[Rabbit] Closing AMQP connection (closeChan closed)")
			if err := r.Connection.Close(); err != nil {
				log.Errorf("Failed to close AMQP connection: %v", err)
			}
			r.connected = false
			return
		}
	}
}

func (r *RabbitConnection) IsConnected() bool {
	return r.connected
}

func (r *RabbitConnection) Close() {
	r.mtx.Lock()
	defer r.mtx.Unlock()

	if r.closed {
		return
	}

	close(r.closeChan)
	r.closed = true
}

func (r *RabbitConnection) tryToConnect() error {
	var err error
	r.Connection, err = amqp.Dial(RabbitURL)
	if err != nil {
		log.Errorf("[Rabbit] Failed to establish connection with RabbitMQ: %s", RabbitURL)
		return err
	}
	r.Channel, err = NewRabbitChannel(r.Connection)
	if err != nil {
		log.Error("[Rabbit] Failed to create Bunny Channel")
		return err
	}
	r.Channel.DeclareExchange(Exchange)
	r.ExchangeChannel, err = NewRabbitChannel(r.Connection)
	if err != nil {
		log.Error("[Rabbit] Failed to create default Channel")
		return err
	}
	log.Info("[Rabbit] Connected to RabbitMQ")
	return nil
}

func (r *RabbitConnection) Consume(serverName string) (<-chan amqp.Delivery, error) {
	consumerChannel, err := NewRabbitChannel(r.Connection)
	if err != nil {
		log.Errorf("[Rabbit] Failed to create new channel: %s", err.Error())
		return nil, err
	}

	err = consumerChannel.DeclareQueue(serverName)
	if err != nil {
		log.Errorf("[Rabbit] Failed to declare queue %s: %s", serverName, err.Error())
		return nil, err
	}

	deliveries, err := consumerChannel.ConsumeQueue(serverName)
	if err != nil {
		log.Errorf("[Rabbit] Failed to declare queue %s: %s", serverName, err.Error())
		return nil, err
	}

	err = consumerChannel.BindQueue(serverName, Exchange)
	if err != nil {
		log.Errorf("[Rabbit] Failed to bind %s to %s exchange: %s", serverName, Exchange, err.Error())
		return nil, err
	}

	return deliveries, nil
}

func (r *RabbitConnection) Publish(exchange, routingKey string, msg amqp.Publishing) error {
	return r.ExchangeChannel.Publish(exchange, routingKey, msg)
}
<commit_msg>initialise env vars inside new connection<commit_after>package rabbit

import (
	"os"
	"sync"
	"time"

	log "github.com/cihub/seelog"
	"github.com/streadway/amqp"
)

var (
	RabbitURL string
	Exchange  string
)

func NewRabbitConnection() *RabbitConnection {

	// Initialize rabbitmq connection from env vars. The defaults are expected to work
	// in development. ci, dev-ci and mesos override these defaults
	// @todo Refactor initialization of the connection to allow the "server" abstraction we'll introduce to inject the configuration
	// @todo once config server has http interface, we should use values from the config
	// server to connect to rabbit
	RabbitURL = os.Getenv("RABBIT_URL")
	if RabbitURL == "" {
		RabbitURL = "amqp://admin:guest@192.168.59.103:5672"
		log.Infof("Setting RABBIT_URL to default value %s", RabbitURL)
	}
	log.Infof("Set RABBIT_URL to %s", RabbitURL)
	Exchange = os.Getenv("RABBIT_EXCHANGE")
	if Exchange == "" {
		Exchange = "b2a"
		log.Infof("Setting RABBIT_EXCHANGE to default value %s", Exchange)
	}
	log.Infof("Set RABBIT_EXCHANGE to %s", Exchange)

	return &RabbitConnection{
		notify:    make(chan bool, 1),
		closeChan: make(chan struct{}),
	}
}

type RabbitConnection struct {
	Connection      *amqp.Connection
	Channel         *RabbitChannel
	ExchangeChannel *RabbitChannel
	notify          chan bool

	connected bool

	mtx       sync.Mutex
	closeChan chan struct{}
	closed    bool
}

func (r *RabbitConnection) Init() chan bool {
	go r.Connect(r.notify)
	return r.notify
}

func (r *RabbitConnection) Connect(connected chan bool) {
	for {
		log.Debugf("[Rabbit] Attempting to connect")
		if err := r.tryToConnect(); err != nil {
			log.Debugf("[Rabbit] Failed to connect, sleeping 1s")
			time.Sleep(1 * time.Second)
			continue
		}
		connected <- true
		r.connected = true
		notifyClose := make(chan *amqp.Error)
		r.Connection.NotifyClose(notifyClose)

		// Block until we get disconnected, or shut down
		select {
		case err := <-notifyClose:
			// Spin around and reconnect
			r.connected = false
			log.Debugf("[Rabbit] AMQP connection closed (notifyClose): %s", err.Error())
		case <-r.closeChan:
			// Shut down connection
			log.Debugf("[Rabbit] Closing AMQP connection (closeChan closed)")
			if err := r.Connection.Close(); err != nil {
				log.Errorf("Failed to close AMQP connection: %v", err)
			}
			r.connected = false
			return
		}
	}
}

func (r *RabbitConnection) IsConnected() bool {
	return r.connected
}

func (r *RabbitConnection) Close() {
	r.mtx.Lock()
	defer r.mtx.Unlock()

	if r.closed {
		return
	}

	close(r.closeChan)
	r.closed = true
}

func (r *RabbitConnection) tryToConnect() error {
	var err error
	r.Connection, err = amqp.Dial(RabbitURL)
	if err != nil {
		log.Errorf("[Rabbit] Failed to establish connection with RabbitMQ: %s", RabbitURL)
		return err
	}
	r.Channel, err = NewRabbitChannel(r.Connection)
	if err != nil {
		log.Error("[Rabbit] Failed to create Bunny Channel")
		return err
	}
	r.Channel.DeclareExchange(Exchange)
	r.ExchangeChannel, err = NewRabbitChannel(r.Connection)
	if err != nil {
		log.Error("[Rabbit] Failed to create default Channel")
		return err
	}
	log.Info("[Rabbit] Connected to RabbitMQ")
	return nil
}

func (r *RabbitConnection) Consume(serverName string) (<-chan amqp.Delivery, error) {
	consumerChannel, err := NewRabbitChannel(r.Connection)
	if err != nil {
		log.Errorf("[Rabbit] Failed to create new channel: %s", err.Error())
		return nil, err
	}

	err = consumerChannel.DeclareQueue(serverName)
	if err != nil {
		log.Errorf("[Rabbit] Failed to declare queue %s: %s", serverName, err.Error())
		return nil, err
	}

	deliveries, err := consumerChannel.ConsumeQueue(serverName)
	if err != nil {
		log.Errorf("[Rabbit] Failed to declare queue %s: %s", serverName, err.Error())
		return nil, err
	}

	err = consumerChannel.BindQueue(serverName, Exchange)
	if err != nil {
		log.Errorf("[Rabbit] Failed to bind %s to %s exchange: %s", serverName, Exchange, err.Error())
		return nil, err
	}

	return deliveries, nil
}

func (r *RabbitConnection) Publish(exchange, routingKey string, msg amqp.Publishing) error {
	return r.ExchangeChannel.Publish(exchange, routingKey, msg)
}
<|endoftext|>


<commit_before>package gorocksdb

import "C"
import (
	"reflect"
	"unsafe"
)

// btoi converts a bool value to int.
func btoi(b bool) int {
	if b {
		return 1
	}
	return 0
}

// boolToChar converts a bool value to C.uchar.
func boolToChar(b bool) C.uchar {
	if b {
		return 1
	}
	return 0
}

// charToByte converts a *C.char to a byte slice.
func charToByte(data *C.char, len C.size_t) []byte {
	var value []byte
	sH := (*reflect.SliceHeader)(unsafe.Pointer(&value))
	sH.Cap, sH.Len, sH.Data = int(len), int(len), uintptr(unsafe.Pointer(data))
	return value
}

// byteToChar returns *C.char from byte slice.
func byteToChar(b []byte) *C.char {
	var c *C.char
	if len(b) > 0 {
		c = (*C.char)(unsafe.Pointer(&b[0]))
	}
	return c
}

// Go []byte to C string
// The C string is allocated in the C heap using malloc.
func cByteSlice(b []byte) *C.char {
	var c *C.char
	if len(b) > 0 {
		cData := C.malloc(C.size_t(len(b)))
		copy((*[1 << 24]byte)(cData)[0:len(b)], b)
		c = (*C.char)(cData)
	}
	return c
}

// stringToChar returns *C.char from string.
func stringToChar(s string) *C.char {
	ptrStr := (*reflect.StringHeader)(unsafe.Pointer(&s))
	return (*C.char)(unsafe.Pointer(ptrStr.Data))
}

// charSlice converts a C array of *char to a []*C.char.
func charSlice(data **C.char, len C.int) []*C.char {
	var value []*C.char
	sH := (*reflect.SliceHeader)(unsafe.Pointer(&value))
	sH.Cap, sH.Len, sH.Data = int(len), int(len), uintptr(unsafe.Pointer(data))
	return value
}

// sizeSlice converts a C array of size_t to a []C.size_t.
func sizeSlice(data *C.size_t, len C.int) []C.size_t {
	var value []C.size_t
	sH := (*reflect.SliceHeader)(unsafe.Pointer(&value))
	sH.Cap, sH.Len, sH.Data = int(len), int(len), uintptr(unsafe.Pointer(data))
	return value
}
<commit_msg>cByteSlice fix<commit_after>package gorocksdb
//#include <string.h>
import "C"
import (
	"reflect"
	"unsafe"
)

// btoi converts a bool value to int.
func btoi(b bool) int {
	if b {
		return 1
	}
	return 0
}

// boolToChar converts a bool value to C.uchar.
func boolToChar(b bool) C.uchar {
	if b {
		return 1
	}
	return 0
}

// charToByte converts a *C.char to a byte slice.
func charToByte(data *C.char, len C.size_t) []byte {
	var value []byte
	sH := (*reflect.SliceHeader)(unsafe.Pointer(&value))
	sH.Cap, sH.Len, sH.Data = int(len), int(len), uintptr(unsafe.Pointer(data))
	return value
}

// byteToChar returns *C.char from byte slice.
func byteToChar(b []byte) *C.char {
	var c *C.char
	if len(b) > 0 {
		c = (*C.char)(unsafe.Pointer(&b[0]))
	}
	return c
}

// Go []byte to C string
// The C string is allocated in the C heap using malloc.
func cByteSlice(b []byte) *C.char {
	var c *C.char
	if len(b) > 0 {
		cData := C.malloc(C.size_t(len(b)))
		C.memcpy(unsafe.Pointer(&cData), unsafe.Pointer(&b[0]), C.size_t(len(b)))
		c = (*C.char)(cData)
	}
	return c
}

// stringToChar returns *C.char from string.
func stringToChar(s string) *C.char {
	ptrStr := (*reflect.StringHeader)(unsafe.Pointer(&s))
	return (*C.char)(unsafe.Pointer(ptrStr.Data))
}

// charSlice converts a C array of *char to a []*C.char.
func charSlice(data **C.char, len C.int) []*C.char {
	var value []*C.char
	sH := (*reflect.SliceHeader)(unsafe.Pointer(&value))
	sH.Cap, sH.Len, sH.Data = int(len), int(len), uintptr(unsafe.Pointer(data))
	return value
}

// sizeSlice converts a C array of size_t to a []C.size_t.
func sizeSlice(data *C.size_t, len C.int) []C.size_t {
	var value []C.size_t
	sH := (*reflect.SliceHeader)(unsafe.Pointer(&value))
	sH.Cap, sH.Len, sH.Data = int(len), int(len), uintptr(unsafe.Pointer(data))
	return value
}
<|endoftext|>


<commit_before>package sentinel

import (
	"github.com/mdevilliers/redishappy/services/logger"
	"github.com/mdevilliers/redishappy/services/redis"
	"github.com/mdevilliers/redishappy/types"
	"strconv"
	"time"
)

type SentinelClient struct {
	sentinel        types.Sentinel
	redisClient     redis.RedisClient
	sentinelManager Manager
	sleepInSeconds  int
}

func NewSentinelClient(sentinel types.Sentinel, manager Manager, redisConnection redis.RedisConnection) (*SentinelClient, error) {

	uri := sentinel.GetLocation()
	logger.Info.Printf("HealthChecker : connecting to %s", uri)

	redisclient, err := redisConnection.GetConnection("tcp", uri)

	if err != nil {
		logger.Info.Printf("HealthChecker : not connected to %s, %s", uri, err.Error())
		manager.Notify(&SentinelLost{Sentinel: sentinel})
		return nil, err
	}

	logger.Info.Printf("HealthChecker : connected to %s", uri)

	client := &SentinelClient{redisClient: redisclient,
		sentinel:        sentinel,
		sentinelManager: manager,
		sleepInSeconds:  1}
	return client, nil
}

func (m *SentinelClient) DiscoverMasterForCluster(clusterName string) (types.MasterDetails, error) {

	r := m.redisClient.Cmd("SENTINEL", "get-master-addr-by-name", clusterName)

	bits, err := r.List()

	if err == nil {
		port, _ := strconv.Atoi(bits[1])
		return types.MasterDetails{Name: clusterName, Ip: bits[0], Port: port}, nil
	}

	return types.MasterDetails{}, err
}

func (client *SentinelClient) Start() {
	go client.healthcheckloop()
	// TODO : check for other sentinels
	//go client.sentineldiscoveryloop()
}

func (client *SentinelClient) healthcheckloop() {

	for {

		r := client.redisClient.Cmd("PING")

		if r.Err() != nil {

			client.sentinelManager.Notify(&SentinelLost{Sentinel: client.sentinel})
			break
		}

		pingResult := r.String()

		if pingResult != "PONG" {

			client.sentinelManager.Notify(&SentinelLost{Sentinel: client.sentinel})
			break
		} else {

			client.sentinelManager.Notify(&SentinelPing{Sentinel: client.sentinel})

		}

		time.Sleep(time.Duration(client.sleepInSeconds) * time.Second)
	}
}

// TODO : check for other sentinels
// func (client *SentinelClient) sentineldiscoveryloop() {
// 	for {
// 		client.findConnectedSentinels("secure")
// 		time.Sleep(time.Duration(client.sleepInSeconds) * time.Second)
// 	}
// }
// func (client *SentinelClient) findConnectedSentinels(clustername string) (bool, error) {
// 	r := client.redisClient.Cmd("SENTINEL", "SENTINELS", clustername)
// 	for _, e := range r.Elems {
// 		  t,_ := e.Hash()
// 		  logger.Info.Printf("Sentinels : xxx : %s : %s",t["ip"], t["port"])
// 	}
// 	return false, nil
// }
<commit_msg>corrected comments<commit_after>package sentinel

import (
	"github.com/mdevilliers/redishappy/services/logger"
	"github.com/mdevilliers/redishappy/services/redis"
	"github.com/mdevilliers/redishappy/types"
	"strconv"
	"time"
)

type SentinelClient struct {
	sentinel        types.Sentinel
	redisClient     redis.RedisClient
	sentinelManager Manager
	sleepInSeconds  int
}

func NewSentinelClient(sentinel types.Sentinel, manager Manager, redisConnection redis.RedisConnection) (*SentinelClient, error) {

	uri := sentinel.GetLocation()
	logger.Info.Printf("SentinelClient : connecting to %s", uri)

	redisclient, err := redisConnection.GetConnection("tcp", uri)

	if err != nil {
		logger.Info.Printf("SentinelClient : not connected to %s, %s", uri, err.Error())
		manager.Notify(&SentinelLost{Sentinel: sentinel})
		return nil, err
	}

	logger.Info.Printf("SentinelClient : connected to %s", uri)

	client := &SentinelClient{redisClient: redisclient,
		sentinel:        sentinel,
		sentinelManager: manager,
		sleepInSeconds:  1}
	return client, nil
}

func (m *SentinelClient) DiscoverMasterForCluster(clusterName string) (types.MasterDetails, error) {

	r := m.redisClient.Cmd("SENTINEL", "get-master-addr-by-name", clusterName)

	bits, err := r.List()

	if err == nil {
		port, _ := strconv.Atoi(bits[1])
		return types.MasterDetails{Name: clusterName, Ip: bits[0], Port: port}, nil
	}

	return types.MasterDetails{}, err
}

func (client *SentinelClient) Start() {
	go client.healthcheckloop()
	// TODO : check for other sentinels
	//go client.sentineldiscoveryloop()
}

func (client *SentinelClient) healthcheckloop() {

	for {

		r := client.redisClient.Cmd("PING")

		if r.Err() != nil {

			client.sentinelManager.Notify(&SentinelLost{Sentinel: client.sentinel})
			break
		}

		pingResult := r.String()

		if pingResult != "PONG" {

			client.sentinelManager.Notify(&SentinelLost{Sentinel: client.sentinel})
			break
		} else {

			client.sentinelManager.Notify(&SentinelPing{Sentinel: client.sentinel})

		}

		time.Sleep(time.Duration(client.sleepInSeconds) * time.Second)
	}
}

// TODO : check for other sentinels
// func (client *SentinelClient) sentineldiscoveryloop() {
// 	for {
// 		client.findConnectedSentinels("secure")
// 		time.Sleep(time.Duration(client.sleepInSeconds) * time.Second)
// 	}
// }
// func (client *SentinelClient) findConnectedSentinels(clustername string) (bool, error) {
// 	r := client.redisClient.Cmd("SENTINEL", "SENTINELS", clustername)
// 	for _, e := range r.Elems {
// 		  t,_ := e.Hash()
// 		  logger.Info.Printf("Sentinels : xxx : %s : %s",t["ip"], t["port"])
// 	}
// 	return false, nil
// }
<|endoftext|>


<commit_before>// Provides utility functions.

package zoossh

import (
	"bufio"
	"encoding/base64"
	"encoding/hex"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"strconv"
	"strings"
)

type QueueUnit struct {
	Blurb string
	Err   error
}

type Annotation struct {
	Type  string
	Major string
	Minor string
}

// Extracts a string unit from an archive file that can be readily thrown into
// the respective parser.
type StringExtractor func(string) (string, bool, error)

func (a *Annotation) String() string {

	return fmt.Sprintf("@type %s %s.%s", a.Type, a.Major, a.Minor)
}

// Equals checks whether the two given annotations have the same content.
func (a *Annotation) Equals(b *Annotation) bool {

	return (*a).Type == (*b).Type && (*a).Major == (*b).Major && (*a).Minor == (*b).Minor
}

// Decodes the given Base64-encoded string and returns the resulting string.
// If there are errors during decoding, an error string is returned.
func Base64ToString(encoded string) (string, error) {

	// dir-spec.txt says that Base64 padding is removed so we have to account
	// for that here.
	if rem := len(encoded) % 4; rem != 0 {
		encoded += strings.Repeat("=", 4-rem)
	}

	decoded, err := base64.StdEncoding.DecodeString(encoded)
	if err != nil {
		return "", err
	}

	return hex.EncodeToString(decoded), nil
}

// GetAnnotation obtains and returns the given file's annotation.  If anything
// fails in the process, an error string is returned.
func GetAnnotation(fileName string) (*Annotation, error) {

	fd, err := os.Open(fileName)
	if err != nil {
		return nil, err
	}
	defer fd.Close()

	// Fetch the file's first line which should be the annotation.

	scanner := bufio.NewScanner(fd)
	scanner.Scan()
	annotationText := scanner.Text()

	annotation := new(Annotation)

	// We expect "@type TYPE VERSION".
	words := strings.Split(annotationText, " ")
	if len(words) != 3 {
		return nil, fmt.Errorf("Could not parse file annotation for \"%s\".", fileName)
	}
	annotation.Type = words[1]

	// We expect "MAJOR.MINOR".
	version := strings.Split(words[2], ".")
	if len(version) != 2 {
		return nil, fmt.Errorf("Could not parse file annotation for \"%s\".", fileName)
	}
	annotation.Major = version[0]
	annotation.Minor = version[1]

	return annotation, nil
}

// Checks the type annotation in the given file.  The Annotation struct
// determines what we want to see in the file.  If we don't see the expected
// annotation, an error string is returned.
func CheckAnnotation(fd *os.File, expected map[Annotation]bool) error {

	before, err := fd.Seek(0, os.SEEK_CUR)
	if err != nil {
		return err
	}

	// The annotation is placed in the first line of the file.  See the
	// following URL for details:
	// <https://collector.torproject.org/formats.html>
	scanner := bufio.NewScanner(fd)
	scanner.Scan()
	annotation := scanner.Text()

	// Set file descriptor back because NewScanner() reads and buffers large
	// chunks of data.
	fd.Seek(before + int64(len(annotation)), os.SEEK_SET)

	invalidFormat := fmt.Errorf("Unexpected file annotation: %s", annotation)

	// We expect "@type TYPE VERSION".
	words := strings.Split(annotation, " ")
	if len(words) != 3 {
		return invalidFormat
	}

	// We expect "MAJOR.MINOR".
	version := strings.Split(words[2], ".")
	if len(version) != 2 {
		return invalidFormat
	}
	observed := Annotation{words[1], version[0], version[1]}

	for annotation, _ := range expected {
		// We support the observed annotation.
		if annotation.Equals(&observed) {
			return nil
		}
	}

	return invalidFormat
}

// Dissects the given file into string chunks by using the given string
// extraction function.  The resulting string chunks are then written to the
// given queue where the receiving end parses them.
func DissectFile(fd *os.File, extractor StringExtractor, queue chan QueueUnit) {

	defer close(queue)

	blurb, err := ioutil.ReadAll(fd)
	if err != nil {
		queue <- QueueUnit{"", err}
	}

	rawContent := string(blurb)

	for {
		unit, done, err := extractor(rawContent)
		if err != nil {
			log.Println("Error in extraction function: ", err)
			break
		}

		queue <- QueueUnit{unit, nil}
		rawContent = rawContent[len(unit):]

		if done {
			break
		}
	}
}

// Convert the given port string to an unsigned 16-bit integer.  If the
// conversion fails or the number cannot be represented in 16 bits, 0 is
// returned.
func StringToPort(portStr string) uint16 {

	portNum, err := strconv.ParseUint(portStr, 10, 16)
	if err != nil {
		return uint16(0)
	}

	return uint16(portNum)
}
<commit_msg>Fix code formatting issue.<commit_after>// Provides utility functions.

package zoossh

import (
	"bufio"
	"encoding/base64"
	"encoding/hex"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"strconv"
	"strings"
)

type QueueUnit struct {
	Blurb string
	Err   error
}

type Annotation struct {
	Type  string
	Major string
	Minor string
}

// Extracts a string unit from an archive file that can be readily thrown into
// the respective parser.
type StringExtractor func(string) (string, bool, error)

func (a *Annotation) String() string {

	return fmt.Sprintf("@type %s %s.%s", a.Type, a.Major, a.Minor)
}

// Equals checks whether the two given annotations have the same content.
func (a *Annotation) Equals(b *Annotation) bool {

	return (*a).Type == (*b).Type && (*a).Major == (*b).Major && (*a).Minor == (*b).Minor
}

// Decodes the given Base64-encoded string and returns the resulting string.
// If there are errors during decoding, an error string is returned.
func Base64ToString(encoded string) (string, error) {

	// dir-spec.txt says that Base64 padding is removed so we have to account
	// for that here.
	if rem := len(encoded) % 4; rem != 0 {
		encoded += strings.Repeat("=", 4-rem)
	}

	decoded, err := base64.StdEncoding.DecodeString(encoded)
	if err != nil {
		return "", err
	}

	return hex.EncodeToString(decoded), nil
}

// GetAnnotation obtains and returns the given file's annotation.  If anything
// fails in the process, an error string is returned.
func GetAnnotation(fileName string) (*Annotation, error) {

	fd, err := os.Open(fileName)
	if err != nil {
		return nil, err
	}
	defer fd.Close()

	// Fetch the file's first line which should be the annotation.

	scanner := bufio.NewScanner(fd)
	scanner.Scan()
	annotationText := scanner.Text()

	annotation := new(Annotation)

	// We expect "@type TYPE VERSION".
	words := strings.Split(annotationText, " ")
	if len(words) != 3 {
		return nil, fmt.Errorf("Could not parse file annotation for \"%s\".", fileName)
	}
	annotation.Type = words[1]

	// We expect "MAJOR.MINOR".
	version := strings.Split(words[2], ".")
	if len(version) != 2 {
		return nil, fmt.Errorf("Could not parse file annotation for \"%s\".", fileName)
	}
	annotation.Major = version[0]
	annotation.Minor = version[1]

	return annotation, nil
}

// Checks the type annotation in the given file.  The Annotation struct
// determines what we want to see in the file.  If we don't see the expected
// annotation, an error string is returned.
func CheckAnnotation(fd *os.File, expected map[Annotation]bool) error {

	before, err := fd.Seek(0, os.SEEK_CUR)
	if err != nil {
		return err
	}

	// The annotation is placed in the first line of the file.  See the
	// following URL for details:
	// <https://collector.torproject.org/formats.html>
	scanner := bufio.NewScanner(fd)
	scanner.Scan()
	annotation := scanner.Text()

	// Set file descriptor back because NewScanner() reads and buffers large
	// chunks of data.
	fd.Seek(before+int64(len(annotation)), os.SEEK_SET)

	invalidFormat := fmt.Errorf("Unexpected file annotation: %s", annotation)

	// We expect "@type TYPE VERSION".
	words := strings.Split(annotation, " ")
	if len(words) != 3 {
		return invalidFormat
	}

	// We expect "MAJOR.MINOR".
	version := strings.Split(words[2], ".")
	if len(version) != 2 {
		return invalidFormat
	}
	observed := Annotation{words[1], version[0], version[1]}

	for annotation, _ := range expected {
		// We support the observed annotation.
		if annotation.Equals(&observed) {
			return nil
		}
	}

	return invalidFormat
}

// Dissects the given file into string chunks by using the given string
// extraction function.  The resulting string chunks are then written to the
// given queue where the receiving end parses them.
func DissectFile(fd *os.File, extractor StringExtractor, queue chan QueueUnit) {

	defer close(queue)

	blurb, err := ioutil.ReadAll(fd)
	if err != nil {
		queue <- QueueUnit{"", err}
	}

	rawContent := string(blurb)

	for {
		unit, done, err := extractor(rawContent)
		if err != nil {
			log.Println("Error in extraction function: ", err)
			break
		}

		queue <- QueueUnit{unit, nil}
		rawContent = rawContent[len(unit):]

		if done {
			break
		}
	}
}

// Convert the given port string to an unsigned 16-bit integer.  If the
// conversion fails or the number cannot be represented in 16 bits, 0 is
// returned.
func StringToPort(portStr string) uint16 {

	portNum, err := strconv.ParseUint(portStr, 10, 16)
	if err != nil {
		return uint16(0)
	}

	return uint16(portNum)
}
<|endoftext|>


<commit_before>package pget

import (
	"context"
	"fmt"
	"io"
	"os"
	"path/filepath"
	"runtime"
	"strings"
	"time"

	"github.com/cheggaaa/pb/v3"
	"github.com/pkg/errors"
	"github.com/ricochet2200/go-disk-usage/du"
)

// Data struct has file of relational data
type Data struct {
	filename     string
	filesize     uint
	dirname      string
	fullfilename string
}

// Utils interface indicate function
type Utils interface {
	ProgressBar(context.Context) error
	BindwithFiles(int) error
	IsFree(uint) error
	Progress(string) (int64, error)
	URLFileName(string, string) string

	// like setter
	SetFileName(string)
	SetFullFileName(string, string)
	SetDirName(string, string, int)
	SetFileSize(uint)

	// like getter
	FileName() string
	FullFileName() string
	FileSize() uint
	DirName() string
}

func isDos() bool {
	return runtime.GOOS == "windows"
}

// FileName get from Data structs member
func (d Data) FileName() string {
	return d.filename
}

// FullFileName get from Data structs member
func (d Data) FullFileName() string {
	return d.fullfilename
}

// FileSize get from Data structs member
func (d Data) FileSize() uint {
	return d.filesize
}

// DirName get from Data structs member
func (d Data) DirName() string {
	return d.dirname
}

// SetFileSize set to Data structs member
func (d *Data) SetFileSize(size uint) {
	d.filesize = size
}

// SetFullFileName set to Data structs member
func (d *Data) SetFullFileName(directory, filename string) {
	if directory == "" {
		d.fullfilename = fmt.Sprintf("%s", filename)
	} else {
		d.fullfilename = fmt.Sprintf("%s/%s", directory, filename)
	}
}

// SetFileName set to Data structs member
func (d *Data) SetFileName(filename string) {
	d.filename = filename
}

// URLFileName set to Data structs member using url
func (d *Data) URLFileName(targetDir, url string) string {
	token := strings.Split(url, "/")

	// get of filename from url
	var original string
	for i := 1; original == ""; i++ {
		original = token[len(token)-i]
	}

	filename := original

	// create unique filename
	for i := 1; true; i++ {
		var filePath string
		if targetDir == "" {
			filePath = filename
		} else {
			filePath = fmt.Sprintf("%s/%s", targetDir, filename)
		}

		if _, err := os.Stat(filePath); err == nil {
			filename = fmt.Sprintf("%s-%d", original, i)
		} else {
			break
		}
	}

	return filename
}

// SetDirName set to Data structs member
func (d *Data) SetDirName(path, filename string, procs int) {
	d.dirname = GetDirname(path, filename, procs)
}

func GetDirname(targetDir, filename string, procs int) string {
	if targetDir == "" {
		return fmt.Sprintf("_%s.%d", filename, procs)
	}
	return fmt.Sprintf("%s/_%s.%d", targetDir, filename, procs)
}

func (d Data) freeSpace() (freespace uint) {

	if isDos() {
		freespace = uint(du.NewDiskUsage("C:\\").Free())
	} else {
		freespace = uint(du.NewDiskUsage("/").Free())
	}

	return
}

// IsFree is check your disk space for size needed to download
func (d Data) IsFree(split uint) error {
	want := d.filesize + split
	if d.freeSpace() < want {
		return errors.Errorf("there is not sufficient free space in a disk")
	}

	return nil
}

// Progress In order to confirm the degree of progress
func Progress(dirname string) (int64, error) {
	return subDirsize(dirname)
}

func subDirsize(dirname string) (int64, error) {
	var size int64
	err := filepath.Walk(dirname, func(_ string, info os.FileInfo, err error) error {
		if !info.IsDir() {
			size += info.Size()
		}
		return err
	})

	return size, err
}

func makeRange(i, procs int, rangeSize, contentLength int64) Range {
	low := rangeSize * int64(i)
	if i == procs-1 {
		return Range{
			low:  low,
			high: contentLength,
		}
	}
	return Range{
		low:  low,
		high: low + rangeSize - 1,
	}
}

func (r Range) BytesRange() string {
	return fmt.Sprintf("bytes=%d-%d", r.low, r.high)
}

// ProgressBar is to show progressbar
func (d Data) ProgressBar(ctx context.Context) error {
	filesize := int64(d.filesize)
	dirname := d.dirname

	bar := pb.New64(filesize)
	bar.Start()

	for {
		select {
		case <-ctx.Done():
			return nil
		default:
			size, err := Progress(dirname)
			if err != nil {
				return errors.Wrap(err, "failed to get directory size")
			}

			if size < filesize {
				bar.Add64(size)
			} else {
				bar.Add64(filesize)
				bar.Finish()
				return nil
			}

			// To save cpu resource
			time.Sleep(100 * time.Millisecond)
		}
	}
}

func ProgressBar(ctx context.Context, contentLength int64, dirname string) error {
	bar := pb.Start64(contentLength)

	for {
		select {
		case <-ctx.Done():
			return nil
		case <-time.After(100 * time.Millisecond): // To save cpu resource
			size, err := Progress(dirname)
			if err != nil {
				return errors.Wrap(err, "failed to get directory size")
			}

			if size < contentLength {
				bar.SetCurrent(size)
			} else {
				bar.SetCurrent(contentLength)
				bar.Finish()
				return nil
			}
		}
	}
}

// BindwithFiles function for file binding after split download
func (d *Data) BindwithFiles(procs int) error {

	fmt.Println("\nbinding with files...")

	filesize := d.filesize
	filename := d.filename
	dirname := d.dirname

	fh, err := os.Create(d.fullfilename)
	if err != nil {
		return errors.Wrap(err, "failed to create a file in download location")
	}
	defer fh.Close()

	bar := pb.New64(int64(filesize))
	bar.Start()

	var f string
	for i := 0; i < procs; i++ {
		f = fmt.Sprintf("%s/%s.%d.%d", dirname, filename, procs, i)
		subfp, err := os.Open(f)
		if err != nil {
			return errors.Wrap(err, "failed to open "+f+" in download location")
		}

		proxy := bar.NewProxyReader(subfp)
		io.Copy(fh, proxy)

		// Not use defer
		subfp.Close()

		// remove a file in download location for join
		if err := os.Remove(f); err != nil {
			return errors.Wrap(err, "failed to remove a file in download location")
		}
	}

	bar.Finish()

	// remove download location
	// RemoveAll reason: will create .DS_Store in download location if execute on mac
	if err := os.RemoveAll(dirname); err != nil {
		return errors.Wrap(err, "failed to remove download location")
	}

	fmt.Println("Complete")

	return nil
}
<commit_msg>removed unused functions<commit_after>package pget

import (
	"context"
	"fmt"
	"os"
	"path/filepath"
	"time"

	"github.com/cheggaaa/pb/v3"
	"github.com/pkg/errors"
)

func GetDirname(targetDir, filename string, procs int) string {
	if targetDir == "" {
		return fmt.Sprintf("_%s.%d", filename, procs)
	}
	return fmt.Sprintf("%s/_%s.%d", targetDir, filename, procs)
}

// Progress In order to confirm the degree of progress
func Progress(dirname string) (int64, error) {
	return subDirsize(dirname)
}

func subDirsize(dirname string) (int64, error) {
	var size int64
	err := filepath.Walk(dirname, func(_ string, info os.FileInfo, err error) error {
		if !info.IsDir() {
			size += info.Size()
		}
		return err
	})

	return size, err
}

func makeRange(i, procs int, rangeSize, contentLength int64) Range {
	low := rangeSize * int64(i)
	if i == procs-1 {
		return Range{
			low:  low,
			high: contentLength,
		}
	}
	return Range{
		low:  low,
		high: low + rangeSize - 1,
	}
}

func (r Range) BytesRange() string {
	return fmt.Sprintf("bytes=%d-%d", r.low, r.high)
}

func ProgressBar(ctx context.Context, contentLength int64, dirname string) error {
	bar := pb.Start64(contentLength)

	for {
		select {
		case <-ctx.Done():
			return nil
		case <-time.After(100 * time.Millisecond): // To save cpu resource
			size, err := Progress(dirname)
			if err != nil {
				return errors.Wrap(err, "failed to get directory size")
			}

			if size < contentLength {
				bar.SetCurrent(size)
			} else {
				bar.SetCurrent(contentLength)
				bar.Finish()
				return nil
			}
		}
	}
}
<|endoftext|>


<commit_before>package gson

import "reflect"
import "unsafe"
import "fmt"
import "encoding/json"
import "strconv"

func bytes2str(bytes []byte) string {
	if bytes == nil {
		return ""
	}
	sl := (*reflect.SliceHeader)(unsafe.Pointer(&bytes))
	st := &reflect.StringHeader{Data: sl.Data, Len: sl.Len}
	return *(*string)(unsafe.Pointer(st))
}

func str2bytes(str string) []byte {
	if str == "" {
		return nil
	}
	st := (*reflect.StringHeader)(unsafe.Pointer(&str))
	sl := &reflect.SliceHeader{Data: st.Data, Len: st.Len, Cap: st.Len}
	return *(*[]byte)(unsafe.Pointer(sl))
}

// CborMap2golangMap transforms [][2]interface{} to map[string]interface{}
// that is required for converting golang to cbor and vice-versa.
func CborMap2golangMap(value interface{}) interface{} {
	switch items := value.(type) {
	case []interface{}:
		for i, item := range items {
			items[i] = CborMap2golangMap(item)
		}
		return items
	case [][2]interface{}:
		m := make(map[string]interface{})
		for _, item := range items {
			m[item[0].(string)] = CborMap2golangMap(item[1])
		}
		return m
	}
	return value
}

// GolangMap2cborMap transforms map[string]interface{} to [][2]interface{}
// that is required for converting golang to cbor and vice-versa.
func GolangMap2cborMap(value interface{}) interface{} {
	switch items := value.(type) {
	case []interface{}:
		for i, item := range items {
			items[i] = GolangMap2cborMap(item)
		}
		return items
	case map[string]interface{}:
		sl := make([][2]interface{}, 0, len(items))
		for k, v := range items {
			sl = append(sl, [2]interface{}{k, GolangMap2cborMap(v)})
		}
		return sl
	}
	return value
}

func Fixtojson(config *Config, val interface{}) interface{} {
	var err error
	if s, ok := val.(json.Number); ok {
		val, err = strconv.ParseFloat(string(s), 64)
		if err != nil {
			panic(err)
		}
	}
	switch v := val.(type) {
	case int8:
		return float64(v)
	case uint8:
		return float64(v)
	case int16:
		return float64(v)
	case uint16:
		return float64(v)
	case int32:
		return float64(v)
	case uint32:
		return float64(v)
	case int64:
		if config.nk == FloatNumber {
			return float64(v)
		} else if config.nk == SmartNumber {
			return v
		}
	case uint64:
		if config.nk == FloatNumber {
			return float64(v)
		} else if config.nk == SmartNumber {
			return v
		}
	case float32:
		return float64(v)
	case float64:
		return v
	case map[string]interface{}:
		for key, x := range v {
			v[key] = Fixtojson(config, x)
		}
		return v
	case [][2]interface{}:
		m := make(map[string]interface{})
		for _, item := range v {
			m[item[0].(string)] = Fixtojson(config, item[1])
		}
		return m
	case []interface{}:
		for i, x := range v {
			v[i] = Fixtojson(config, x)
		}
		return v
	}
	panic("unreachable code")
}

func collateFloat64(value float64, code []byte) int {
	var num [64]byte
	bs := strconv.AppendFloat(num[:0], value, 'e', -1, 64)
	return collateFloat(bs, code)
}

func collateUint64(value uint64, code []byte) int {
	var num, fltx [64]byte
	if value > 9007199254740991 {
		intx := strconv.AppendUint(num[:0], value, 10)
		fltx[0], fltx[1] = intx[0], '.'
		n := 2 + copy(fltx[2:], intx[1:])
		fltx[n], fltx[n+1], n = 'e', '+', n+2
		tmp := strconv.AppendInt(fltx[n:n], int64(len(intx[1:])), 10)
		ln := n + len(tmp)
		return collateFloat(fltx[:ln], code)

	} else {
		bs := strconv.AppendFloat(num[:0], float64(value), 'e', -1, 64)
		return collateFloat(bs, code)
	}
}

func collateInt64(value int64, code []byte) int {
	var num, fltx [64]byte
	if value > 9007199254740991 {
		intx := strconv.AppendInt(num[:0], value, 10)
		fltx[0], fltx[1] = intx[0], '.'
		n := 2 + copy(fltx[2:], intx[1:])
		fltx[n], fltx[n+1], n = 'e', '+', n+2
		tmp := strconv.AppendInt(fltx[n:n], int64(len(intx[1:])), 10)
		ln := n + len(tmp)
		return collateFloat(fltx[:ln], code)

	} else if value < -9007199254740992 {
		intx := strconv.AppendInt(num[:0], value, 10)
		fltx[0], fltx[1], fltx[2] = intx[0], intx[1], '.'
		n := 3 + copy(fltx[3:], intx[2:])
		fltx[n], fltx[n+1], n = 'e', '+', n+2
		tmp := strconv.AppendInt(fltx[n:n], int64(len(intx[2:])), 10)
		ln := n + len(tmp)
		return collateFloat(fltx[:ln], code)

	} else {
		bs := strconv.AppendFloat(num[:0], float64(value), 'e', -1, 64)
		return collateFloat(bs, code)
	}
}

func collated2Number(code []byte, nk NumberKind) (uint64, int64, float64, int) {
	var mantissa, scratch [64]byte
	_, y := collated2Float(code, scratch[:])
	if nk == SmartNumber {
		_, _, exp, mant := parseFloat(scratch[:y], mantissa[:0])
		if exp >= 15 {
			x := len(mant) - exp - 1
			if mant[0] == '+' || mant[0] == '-' {
				x -= 1
			}
			if x > 0 {
				for i := 0; i < x; i++ {
					mant = append(mant, 0)
				}
			}
			ui, err := strconv.ParseUint(bytes2str(mant), 10, 64)
			if err == nil {
				return ui, 0, 0, 1
			}
			i, err := strconv.ParseInt(bytes2str(mant), 10, 64)
			if err == nil {
				return 0, i, 0, 2
			}
			panic(fmt.Errorf("unexpected number %v", string(scratch[:y])))
		}
	}
	f, err := strconv.ParseFloat(bytes2str(scratch[:y]), 64)
	if err != nil {
		panic(err)
	}
	return 0, 0, f, 3

}

func parseFloat(text []byte, m []byte) (int, int, int, []byte) {
	var err error
	var exp int

	dotat, expat := -1, -1
	for i, ch := range text {
		if ch == '.' {
			dotat = i
		} else if ch == 'e' || ch == 'E' {
			expat = i
		} else if expat > -1 && ch == '+' {
			expat = i
		} else if expat == -1 {
			if len(m) > 0 || (ch != '0' && ch != '+') {
				m = append(m, ch)
			}
		}
	}
	if expat > -1 {
		exp, err = strconv.Atoi(bytes2str(text[expat+1:]))
		if err != nil {
			panic(err)
		}
	}
	return dotat, expat, exp, m
}

func collated2Json(code []byte, text []byte, nk NumberKind) int {
	var num [64]byte

	ui, i, f, what := collated2Number(code, nk)
	switch what {
	case 1:
		nm := strconv.AppendUint(num[:0], ui, 10)
		return copy(text, nm)
	case 2:
		nm := strconv.AppendInt(num[:0], i, 10)
		return copy(text, nm)
	case 3:
		nm := strconv.AppendFloat(num[:0], f, 'e', -1, 64)
		return copy(text, nm)
	}
	panic("unreachable code")
}

// sort JSON property objects based on property names.
func sortProps(props map[string]interface{}, keys []string) []string {
	for k := range props {
		keys = append(keys, k)
	}

	return sortStrings(keys)
}

// bubble sort, moving to qsort should be atleast 40% faster.
func sortStrings(strs []string) []string {
	for ln := len(strs) - 1; ; ln-- {
		changed := false
		for i := 0; i < ln; i++ {
			if strs[i] > strs[i+1] {
				strs[i], strs[i+1] = strs[i+1], strs[i]
				changed = true
			}
		}
		if changed == false {
			break
		}
	}
	return strs
}

//---- data modelling to sort and collate JSON property items.

type kvref struct {
	key  string
	code []byte
}

type kvrefs []kvref

func (kv kvrefs) Len() int {
	return len(kv)
}

func (kv kvrefs) Less(i, j int) bool {
	return kv[i].key < kv[j].key
}

func (kv kvrefs) Swap(i, j int) {
	tmp := kv[i]
	kv[i] = kv[j]
	kv[j] = tmp
}

// bubble sort, moving to qsort should be atleast 40% faster.
func (kv kvrefs) sort() {
	for ln := len(kv) - 1; ; ln-- {
		changed := false
		for i := 0; i < ln; i++ {
			if kv[i].key > kv[i+1].key {
				kv[i], kv[i+1] = kv[i+1], kv[i]
				changed = true
			}
		}
		if changed == false {
			break
		}
	}
}
<commit_msg>Fixtojson: handle all types<commit_after>package gson

import "reflect"
import "unsafe"
import "fmt"
import "encoding/json"
import "strconv"

func bytes2str(bytes []byte) string {
	if bytes == nil {
		return ""
	}
	sl := (*reflect.SliceHeader)(unsafe.Pointer(&bytes))
	st := &reflect.StringHeader{Data: sl.Data, Len: sl.Len}
	return *(*string)(unsafe.Pointer(st))
}

func str2bytes(str string) []byte {
	if str == "" {
		return nil
	}
	st := (*reflect.StringHeader)(unsafe.Pointer(&str))
	sl := &reflect.SliceHeader{Data: st.Data, Len: st.Len, Cap: st.Len}
	return *(*[]byte)(unsafe.Pointer(sl))
}

// CborMap2golangMap transforms [][2]interface{} to map[string]interface{}
// that is required for converting golang to cbor and vice-versa.
func CborMap2golangMap(value interface{}) interface{} {
	switch items := value.(type) {
	case []interface{}:
		for i, item := range items {
			items[i] = CborMap2golangMap(item)
		}
		return items
	case [][2]interface{}:
		m := make(map[string]interface{})
		for _, item := range items {
			m[item[0].(string)] = CborMap2golangMap(item[1])
		}
		return m
	}
	return value
}

// GolangMap2cborMap transforms map[string]interface{} to [][2]interface{}
// that is required for converting golang to cbor and vice-versa.
func GolangMap2cborMap(value interface{}) interface{} {
	switch items := value.(type) {
	case []interface{}:
		for i, item := range items {
			items[i] = GolangMap2cborMap(item)
		}
		return items
	case map[string]interface{}:
		sl := make([][2]interface{}, 0, len(items))
		for k, v := range items {
			sl = append(sl, [2]interface{}{k, GolangMap2cborMap(v)})
		}
		return sl
	}
	return value
}

func Fixtojson(config *Config, val interface{}) interface{} {
	var err error

	if val == nil {
		return nil
	}

	if s, ok := val.(json.Number); ok {
		val, err = strconv.ParseFloat(string(s), 64)
		if err != nil {
			panic(err)
		}
	}

	switch v := val.(type) {
	case bool:
		return v
	case string:
		return v
	case int8:
		return float64(v)
	case uint8:
		return float64(v)
	case int16:
		return float64(v)
	case uint16:
		return float64(v)
	case int32:
		return float64(v)
	case uint32:
		return float64(v)
	case int64:
		if config.nk == FloatNumber {
			return float64(v)
		} else if config.nk == SmartNumber {
			return v
		}
	case uint64:
		if config.nk == FloatNumber {
			return float64(v)
		} else if config.nk == SmartNumber {
			return v
		}
	case float32:
		return float64(v)
	case float64:
		return v
	case map[string]interface{}:
		for key, x := range v {
			v[key] = Fixtojson(config, x)
		}
		return v
	case [][2]interface{}:
		m := make(map[string]interface{})
		for _, item := range v {
			m[item[0].(string)] = Fixtojson(config, item[1])
		}
		return m
	case []interface{}:
		for i, x := range v {
			v[i] = Fixtojson(config, x)
		}
		return v
	}
	panic(fmt.Errorf("unreachable code, unexpected %T", val))
}

func collateFloat64(value float64, code []byte) int {
	var num [64]byte
	bs := strconv.AppendFloat(num[:0], value, 'e', -1, 64)
	return collateFloat(bs, code)
}

func collateUint64(value uint64, code []byte) int {
	var num, fltx [64]byte
	if value > 9007199254740991 {
		intx := strconv.AppendUint(num[:0], value, 10)
		fltx[0], fltx[1] = intx[0], '.'
		n := 2 + copy(fltx[2:], intx[1:])
		fltx[n], fltx[n+1], n = 'e', '+', n+2
		tmp := strconv.AppendInt(fltx[n:n], int64(len(intx[1:])), 10)
		ln := n + len(tmp)
		return collateFloat(fltx[:ln], code)

	} else {
		bs := strconv.AppendFloat(num[:0], float64(value), 'e', -1, 64)
		return collateFloat(bs, code)
	}
}

func collateInt64(value int64, code []byte) int {
	var num, fltx [64]byte
	if value > 9007199254740991 {
		intx := strconv.AppendInt(num[:0], value, 10)
		fltx[0], fltx[1] = intx[0], '.'
		n := 2 + copy(fltx[2:], intx[1:])
		fltx[n], fltx[n+1], n = 'e', '+', n+2
		tmp := strconv.AppendInt(fltx[n:n], int64(len(intx[1:])), 10)
		ln := n + len(tmp)
		return collateFloat(fltx[:ln], code)

	} else if value < -9007199254740992 {
		intx := strconv.AppendInt(num[:0], value, 10)
		fltx[0], fltx[1], fltx[2] = intx[0], intx[1], '.'
		n := 3 + copy(fltx[3:], intx[2:])
		fltx[n], fltx[n+1], n = 'e', '+', n+2
		tmp := strconv.AppendInt(fltx[n:n], int64(len(intx[2:])), 10)
		ln := n + len(tmp)
		return collateFloat(fltx[:ln], code)

	} else {
		bs := strconv.AppendFloat(num[:0], float64(value), 'e', -1, 64)
		return collateFloat(bs, code)
	}
}

func collated2Number(code []byte, nk NumberKind) (uint64, int64, float64, int) {
	var mantissa, scratch [64]byte
	_, y := collated2Float(code, scratch[:])
	if nk == SmartNumber {
		_, _, exp, mant := parseFloat(scratch[:y], mantissa[:0])
		if exp >= 15 {
			x := len(mant) - exp - 1
			if mant[0] == '+' || mant[0] == '-' {
				x -= 1
			}
			if x > 0 {
				for i := 0; i < x; i++ {
					mant = append(mant, 0)
				}
			}
			ui, err := strconv.ParseUint(bytes2str(mant), 10, 64)
			if err == nil {
				return ui, 0, 0, 1
			}
			i, err := strconv.ParseInt(bytes2str(mant), 10, 64)
			if err == nil {
				return 0, i, 0, 2
			}
			panic(fmt.Errorf("unexpected number %v", string(scratch[:y])))
		}
	}
	f, err := strconv.ParseFloat(bytes2str(scratch[:y]), 64)
	if err != nil {
		panic(err)
	}
	return 0, 0, f, 3

}

func parseFloat(text []byte, m []byte) (int, int, int, []byte) {
	var err error
	var exp int

	dotat, expat := -1, -1
	for i, ch := range text {
		if ch == '.' {
			dotat = i
		} else if ch == 'e' || ch == 'E' {
			expat = i
		} else if expat > -1 && ch == '+' {
			expat = i
		} else if expat == -1 {
			if len(m) > 0 || (ch != '0' && ch != '+') {
				m = append(m, ch)
			}
		}
	}
	if expat > -1 {
		exp, err = strconv.Atoi(bytes2str(text[expat+1:]))
		if err != nil {
			panic(err)
		}
	}
	return dotat, expat, exp, m
}

func collated2Json(code []byte, text []byte, nk NumberKind) int {
	var num [64]byte

	ui, i, f, what := collated2Number(code, nk)
	switch what {
	case 1:
		nm := strconv.AppendUint(num[:0], ui, 10)
		return copy(text, nm)
	case 2:
		nm := strconv.AppendInt(num[:0], i, 10)
		return copy(text, nm)
	case 3:
		nm := strconv.AppendFloat(num[:0], f, 'e', -1, 64)
		return copy(text, nm)
	}
	panic("unreachable code")
}

// sort JSON property objects based on property names.
func sortProps(props map[string]interface{}, keys []string) []string {
	for k := range props {
		keys = append(keys, k)
	}

	return sortStrings(keys)
}

// bubble sort, moving to qsort should be atleast 40% faster.
func sortStrings(strs []string) []string {
	for ln := len(strs) - 1; ; ln-- {
		changed := false
		for i := 0; i < ln; i++ {
			if strs[i] > strs[i+1] {
				strs[i], strs[i+1] = strs[i+1], strs[i]
				changed = true
			}
		}
		if changed == false {
			break
		}
	}
	return strs
}

//---- data modelling to sort and collate JSON property items.

type kvref struct {
	key  string
	code []byte
}

type kvrefs []kvref

func (kv kvrefs) Len() int {
	return len(kv)
}

func (kv kvrefs) Less(i, j int) bool {
	return kv[i].key < kv[j].key
}

func (kv kvrefs) Swap(i, j int) {
	tmp := kv[i]
	kv[i] = kv[j]
	kv[j] = tmp
}

// bubble sort, moving to qsort should be atleast 40% faster.
func (kv kvrefs) sort() {
	for ln := len(kv) - 1; ; ln-- {
		changed := false
		for i := 0; i < ln; i++ {
			if kv[i].key > kv[i+1].key {
				kv[i], kv[i+1] = kv[i+1], kv[i]
				changed = true
			}
		}
		if changed == false {
			break
		}
	}
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"strings"
	"time"

	"github.com/Sirupsen/logrus"
	"github.com/pkg/browser"
)

func copyToClipboard(data string) error {

	logrus.Infof("copy data: %s", strings.Replace(truncate(data, 50), "\n", "\\n", -1))

	var cmd *exec.Cmd

	// TODO support more than OSX and tmux
	if _, err := exec.LookPath("pbcopy"); err == nil {
		cmd = exec.Command("pbcopy")
	} else if _, err := exec.LookPath("clip"); err == nil {
		cmd = exec.Command("clip")
	} else if _, err := exec.LookPath("xclip"); err == nil {
		cmd = exec.Command("xclip", "-selection", "c")
	} else if _, err := exec.LookPath("tmux"); err == nil {
		cmd = exec.Command("tmux", "load-buffer", "-")
	}
	cmd.Stdin = strings.NewReader(data)

	err := cmd.Run()
	if err != nil {
		return err
	}
	return nil
}

func openURL(data string, isHTML bool) error {
	if isHTML {
		tmpfile, err := ioutil.TempFile("", "pmbopenurl")
		if err != nil {
			return err
		}

		_, err = tmpfile.Write([]byte(data))
		if err != nil {
			return err
		}

		err = tmpfile.Close()
		if err != nil {
			return err
		}

		nameWithSuffix := fmt.Sprintf("%s.html", tmpfile.Name())
		err = os.Rename(tmpfile.Name(), nameWithSuffix)
		if err != nil {
			return err
		}

		go func() {
			time.Sleep(15 * time.Second)
			logrus.Infof("cleaning up temporary file: %s", nameWithSuffix)
			os.Remove(nameWithSuffix)
		}()

		logrus.Infof("opening file: %s", nameWithSuffix)
		return browser.OpenFile(nameWithSuffix)
	}

	logrus.Infof("opening url: %s", data)
	return browser.OpenURL(data)
}

func truncate(data string, length int) string {
	if len(data) > length {
		return fmt.Sprintf("%s (truncated)", data[0:length])
	}
	return data
}

func displayNotice(message string, sticky bool) error {
	stickyText := "sticky"
	if !sticky {
		stickyText = "not sticky"
	}
	logrus.Infof("display message: %s (%s)", message, stickyText)

	var cmd *exec.Cmd

	path := os.Getenv("PATH")
	logrus.Debugf("looking for notifiers in path: %s", path)
	if _, err := exec.LookPath("growlnotify"); err == nil {
		cmdParts := []string{"growlnotify", "-m", message}
		if sticky {
			cmdParts = append(cmdParts, "-s")
		}

		logrus.Debugf("Using growlnotify for notification.")
		cmd = exec.Command(cmdParts[0], cmdParts[1:]...)
	} else if _, err := exec.LookPath("terminal-notifier"); err == nil {
		cmd = exec.Command("terminal-notifier", "-message", message)
		logrus.Debugf("Using terminal-notifier for notification.")
	} else if _, err := exec.LookPath("SnoreToast"); err == nil {
		cmd = exec.Command("SnoreToast", "-t", "PMB", "-m", message)
		logrus.Debugf("Using SnoreToast for notification.")
	} else if _, err := exec.LookPath("notify-send"); err == nil {
		cmdParts := []string{"notify-send", "pmb", message}
		if sticky {
			cmdParts = append(cmdParts, "-t", "60")
		} else {
			cmdParts = append(cmdParts, "-t", "3")
		}

		logrus.Infof("Using notify-send for notification.")
		cmd = exec.Command(cmdParts[0], cmdParts[1:]...)
	} else if _, err := exec.LookPath("tmux"); err == nil {
		cmd = exec.Command("tmux", "display-message", message)
		logrus.Debugf("Using tmux for notification.")
	} else {
		logrus.Warningf("Unable to display notice.")
		return nil
	}

	err := cmd.Run()
	if err != nil {
		return err
	}
	return nil
}
<commit_msg>silence notifications on windows<commit_after>package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"strings"
	"time"

	"github.com/Sirupsen/logrus"
	"github.com/pkg/browser"
)

func copyToClipboard(data string) error {

	logrus.Infof("copy data: %s", strings.Replace(truncate(data, 50), "\n", "\\n", -1))

	var cmd *exec.Cmd

	// TODO support more than OSX and tmux
	if _, err := exec.LookPath("pbcopy"); err == nil {
		cmd = exec.Command("pbcopy")
	} else if _, err := exec.LookPath("clip"); err == nil {
		cmd = exec.Command("clip")
	} else if _, err := exec.LookPath("xclip"); err == nil {
		cmd = exec.Command("xclip", "-selection", "c")
	} else if _, err := exec.LookPath("tmux"); err == nil {
		cmd = exec.Command("tmux", "load-buffer", "-")
	}
	cmd.Stdin = strings.NewReader(data)

	err := cmd.Run()
	if err != nil {
		return err
	}
	return nil
}

func openURL(data string, isHTML bool) error {
	if isHTML {
		tmpfile, err := ioutil.TempFile("", "pmbopenurl")
		if err != nil {
			return err
		}

		_, err = tmpfile.Write([]byte(data))
		if err != nil {
			return err
		}

		err = tmpfile.Close()
		if err != nil {
			return err
		}

		nameWithSuffix := fmt.Sprintf("%s.html", tmpfile.Name())
		err = os.Rename(tmpfile.Name(), nameWithSuffix)
		if err != nil {
			return err
		}

		go func() {
			time.Sleep(15 * time.Second)
			logrus.Infof("cleaning up temporary file: %s", nameWithSuffix)
			os.Remove(nameWithSuffix)
		}()

		logrus.Infof("opening file: %s", nameWithSuffix)
		return browser.OpenFile(nameWithSuffix)
	}

	logrus.Infof("opening url: %s", data)
	return browser.OpenURL(data)
}

func truncate(data string, length int) string {
	if len(data) > length {
		return fmt.Sprintf("%s (truncated)", data[0:length])
	}
	return data
}

func displayNotice(message string, sticky bool) error {
	stickyText := "sticky"
	if !sticky {
		stickyText = "not sticky"
	}
	logrus.Infof("display message: %s (%s)", message, stickyText)

	var cmd *exec.Cmd

	path := os.Getenv("PATH")
	logrus.Debugf("looking for notifiers in path: %s", path)
	if _, err := exec.LookPath("growlnotify"); err == nil {
		cmdParts := []string{"growlnotify", "-m", message}
		if sticky {
			cmdParts = append(cmdParts, "-s")
		}

		logrus.Debugf("Using growlnotify for notification.")
		cmd = exec.Command(cmdParts[0], cmdParts[1:]...)
	} else if _, err := exec.LookPath("terminal-notifier"); err == nil {
		cmd = exec.Command("terminal-notifier", "-message", message)
		logrus.Debugf("Using terminal-notifier for notification.")
	} else if _, err := exec.LookPath("SnoreToast"); err == nil {
		cmd = exec.Command("SnoreToast", "-silent", "-t", "PMB", "-m", message)
		logrus.Debugf("Using SnoreToast for notification.")
	} else if _, err := exec.LookPath("notify-send"); err == nil {
		cmdParts := []string{"notify-send", "pmb", message}
		if sticky {
			cmdParts = append(cmdParts, "-t", "60")
		} else {
			cmdParts = append(cmdParts, "-t", "3")
		}

		logrus.Infof("Using notify-send for notification.")
		cmd = exec.Command(cmdParts[0], cmdParts[1:]...)
	} else if _, err := exec.LookPath("tmux"); err == nil {
		cmd = exec.Command("tmux", "display-message", message)
		logrus.Debugf("Using tmux for notification.")
	} else {
		logrus.Warningf("Unable to display notice.")
		return nil
	}

	err := cmd.Run()
	if err != nil {
		return err
	}
	return nil
}
<|endoftext|>


<commit_before>package cryptdo

import (
	"crypto/aes"
	"crypto/cipher"
	"crypto/rand"
	"crypto/sha512"
	"errors"
	"fmt"
	"io"

	"github.com/golang/protobuf/proto"
	"golang.org/x/crypto/pbkdf2"

	cryptdopb "github.com/xoebus/cryptdo/proto"
)

const (
	// Key Derivation
	iterations = 100000
	saltSize   = 32

	// Encryption
	keySize   = 32
	nonceSize = 12
)

var ErrEmptyMessage = errors.New("cryptdo: empty message")

func Encrypt(plaintext []byte, passphrase string) ([]byte, error) {
	salt, err := randomBytes(saltSize)
	if err != nil {
		return nil, err
	}

	key := derivedKey(passphrase, salt, iterations)
	block, err := aes.NewCipher(key)
	if err != nil {
		return nil, err
	}

	nonce, err := randomBytes(nonceSize)
	if err != nil {
		return nil, err
	}

	aesgcm, err := cipher.NewGCM(block)
	if err != nil {
		return nil, err
	}

	ciphertext := aesgcm.Seal(nil, nonce, plaintext, nil)

	message := &cryptdopb.Message{
		Iterations: iterations,
		Salt:       salt,
		Nonce:      nonce,
		Ciphertext: ciphertext,
	}

	return proto.Marshal(message)
}

func Decrypt(ciphertext []byte, passphrase string) ([]byte, error) {
	if len(ciphertext) == 0 {
		return nil, ErrEmptyMessage
	}

	message := &cryptdopb.Message{}
	if err := proto.Unmarshal(ciphertext, message); err != nil {
		return nil, err
	}

	key := derivedKey(passphrase, message.Salt, int(message.Iterations))
	block, err := aes.NewCipher(key)
	if err != nil {
		return nil, err
	}

	aesgcm, err := cipher.NewGCM(block)
	if err != nil {
		return nil, err
	}

	if aesgcm.NonceSize() != len(message.Nonce) {
		return nil, &InvalidNonceError{
			expected: aesgcm.NonceSize(),
			actual:   len(message.Nonce),
		}
	}

	return aesgcm.Open(nil, message.Nonce, message.Ciphertext, nil)
}

func derivedKey(passphrase string, salt []byte, iters int) []byte {
	return pbkdf2.Key([]byte(passphrase), salt, iters, keySize, sha512.New384)
}

func randomBytes(count int) ([]byte, error) {
	bs := make([]byte, count)

	if _, err := io.ReadFull(rand.Reader, bs); err != nil {
		return nil, err
	}

	return bs, nil
}

type InvalidNonceError struct {
	expected int
	actual   int
}

func (i *InvalidNonceError) Error() string {
	return fmt.Sprintf("cryptdo: message nonce has incorrect size (expected %d, got: %d)", i.expected, i.actual)
}
<commit_msg>increase salt size to the size of the hash output<commit_after>package cryptdo

import (
	"crypto/aes"
	"crypto/cipher"
	"crypto/rand"
	"crypto/sha512"
	"errors"
	"fmt"
	"io"

	"github.com/golang/protobuf/proto"
	"golang.org/x/crypto/pbkdf2"

	cryptdopb "github.com/xoebus/cryptdo/proto"
)

const (
	// Key Derivation
	iterations = 100000

	// Encryption
	keySize   = 32
	nonceSize = 12
)

var (
	hashAlg  = sha512.New384
	saltSize = hashAlg().Size()
)

var ErrEmptyMessage = errors.New("cryptdo: empty message")

func Encrypt(plaintext []byte, passphrase string) ([]byte, error) {
	salt, err := randomBytes(saltSize)
	if err != nil {
		return nil, err
	}

	key := derivedKey(passphrase, salt, iterations)
	block, err := aes.NewCipher(key)
	if err != nil {
		return nil, err
	}

	nonce, err := randomBytes(nonceSize)
	if err != nil {
		return nil, err
	}

	aesgcm, err := cipher.NewGCM(block)
	if err != nil {
		return nil, err
	}

	ciphertext := aesgcm.Seal(nil, nonce, plaintext, nil)

	message := &cryptdopb.Message{
		Iterations: iterations,
		Salt:       salt,
		Nonce:      nonce,
		Ciphertext: ciphertext,
	}

	return proto.Marshal(message)
}

func Decrypt(ciphertext []byte, passphrase string) ([]byte, error) {
	if len(ciphertext) == 0 {
		return nil, ErrEmptyMessage
	}

	message := &cryptdopb.Message{}
	if err := proto.Unmarshal(ciphertext, message); err != nil {
		return nil, err
	}

	key := derivedKey(passphrase, message.Salt, int(message.Iterations))
	block, err := aes.NewCipher(key)
	if err != nil {
		return nil, err
	}

	aesgcm, err := cipher.NewGCM(block)
	if err != nil {
		return nil, err
	}

	if aesgcm.NonceSize() != len(message.Nonce) {
		return nil, &InvalidNonceError{
			expected: aesgcm.NonceSize(),
			actual:   len(message.Nonce),
		}
	}

	return aesgcm.Open(nil, message.Nonce, message.Ciphertext, nil)
}

func derivedKey(passphrase string, salt []byte, iters int) []byte {
	return pbkdf2.Key([]byte(passphrase), salt, iters, keySize, hashAlg)
}

func randomBytes(count int) ([]byte, error) {
	bs := make([]byte, count)

	if _, err := io.ReadFull(rand.Reader, bs); err != nil {
		return nil, err
	}

	return bs, nil
}

type InvalidNonceError struct {
	expected int
	actual   int
}

func (i *InvalidNonceError) Error() string {
	return fmt.Sprintf("cryptdo: message nonce has incorrect size (expected %d, got: %d)", i.expected, i.actual)
}
<|endoftext|>


<commit_before>package aws

import (
	"fmt"
	"os"
	"testing"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/service/ec2"
	"github.com/hashicorp/terraform/helper/resource"
	"github.com/hashicorp/terraform/terraform"
)

func TestAccAWSVPCPeeringConnection_basic(t *testing.T) {
	var connection ec2.VpcPeeringConnection

	resource.Test(t, resource.TestCase{
		PreCheck: func() {
			testAccPreCheck(t)
			if os.Getenv("AWS_ACCOUNT_ID") == "" {
				t.Fatal("AWS_ACCOUNT_ID must be set")
			}
		},
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckAWSVpcPeeringConnectionDestroy,
		Steps: []resource.TestStep{
			resource.TestStep{
				Config: testAccVpcPeeringConfig,
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAWSVpcPeeringConnectionExists("aws_vpc_peering_connection.foo", &connection),
				),
			},
		},
	})
}

func TestAccAWSVPCPeeringConnection_tags(t *testing.T) {
	var connection ec2.VpcPeeringConnection

	resource.Test(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckVpcDestroy,
		Steps: []resource.TestStep{
			resource.TestStep{
				Config: testAccVpcPeeringConfigTags,
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAWSVpcPeeringConnectionExists("aws_vpc_peering_connection.foo", &connection),
					testAccCheckTags(&connection.Tags, "foo", "bar"),
				),
			},
		},
	})
}

func testAccCheckAWSVpcPeeringConnectionDestroy(s *terraform.State) error {
	conn := testAccProvider.Meta().(*AWSClient).ec2conn

	for _, rs := range s.RootModule().Resources {
		if rs.Type != "aws_vpc_peering_connection" {
			continue
		}

		describe, err := conn.DescribeVpcPeeringConnections(
			&ec2.DescribeVpcPeeringConnectionsInput{
				VpcPeeringConnectionIds: []*string{aws.String(rs.Primary.ID)},
			})

		if err == nil {
			if len(describe.VpcPeeringConnections) != 0 {
				return fmt.Errorf("vpc peering connection still exists")
			}
		}
	}

	return nil
}

func testAccCheckAWSVpcPeeringConnectionExists(n string, connection *ec2.VpcPeeringConnection) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		rs, ok := s.RootModule().Resources[n]
		if !ok {
			return fmt.Errorf("Not found: %s", n)
		}

		if rs.Primary.ID == "" {
			return fmt.Errorf("No vpc peering connection id is set")
		}

		conn := testAccProvider.Meta().(*AWSClient).ec2conn
		resp, err := conn.DescribeVpcPeeringConnections(
			&ec2.DescribeVpcPeeringConnectionsInput{
				VpcPeeringConnectionIds: []*string{aws.String(rs.Primary.ID)},
			})
		if err != nil {
			return err
		}
		if len(resp.VpcPeeringConnections) == 0 {
			return fmt.Errorf("VPC peering connection not found")
		}

		*connection = *resp.VpcPeeringConnections[0]

		return nil
	}
}

const testAccVpcPeeringConfig = `
resource "aws_vpc" "foo" {
		cidr_block = "10.0.0.0/16"
}

resource "aws_vpc" "bar" {
		cidr_block = "10.1.0.0/16"
}

resource "aws_vpc_peering_connection" "foo" {
		vpc_id = "${aws_vpc.foo.id}"
		peer_vpc_id = "${aws_vpc.bar.id}"
		auto_accept = true
}
`

const testAccVpcPeeringConfigTags = `
resource "aws_vpc" "foo" {
		cidr_block = "10.0.0.0/16"
}

resource "aws_vpc" "bar" {
		cidr_block = "10.1.0.0/16"
}

resource "aws_vpc_peering_connection" "foo" {
		vpc_id = "${aws_vpc.foo.id}"
		peer_vpc_id = "${aws_vpc.bar.id}"
		tags {
			foo = "bar"
		}
}
`
<commit_msg>update requirement for peer test<commit_after>package aws

import (
	"fmt"
	"os"
	"testing"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/service/ec2"
	"github.com/hashicorp/terraform/helper/resource"
	"github.com/hashicorp/terraform/terraform"
)

func TestAccAWSVPCPeeringConnection_basic(t *testing.T) {
	var connection ec2.VpcPeeringConnection

	resource.Test(t, resource.TestCase{
		PreCheck: func() {
			testAccPreCheck(t)
			if os.Getenv("AWS_ACCOUNT_ID") == "" {
				t.Fatal("AWS_ACCOUNT_ID must be set")
			}
		},
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckAWSVpcPeeringConnectionDestroy,
		Steps: []resource.TestStep{
			resource.TestStep{
				Config: testAccVpcPeeringConfig,
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAWSVpcPeeringConnectionExists("aws_vpc_peering_connection.foo", &connection),
				),
			},
		},
	})
}

func TestAccAWSVPCPeeringConnection_tags(t *testing.T) {
	var connection ec2.VpcPeeringConnection
	peerId := os.Getenv("TF_PEER_ID")

	resource.Test(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckVpcDestroy,
		Steps: []resource.TestStep{
			resource.TestStep{
				Config: fmt.Sprintf(testAccVpcPeeringConfigTags, peerId),
				Check: resource.ComposeTestCheckFunc(
					testAccCheckAWSVpcPeeringConnectionExists("aws_vpc_peering_connection.foo", &connection),
					testAccCheckTags(&connection.Tags, "foo", "bar"),
				),
			},
		},
	})
}

func testAccCheckAWSVpcPeeringConnectionDestroy(s *terraform.State) error {
	conn := testAccProvider.Meta().(*AWSClient).ec2conn

	for _, rs := range s.RootModule().Resources {
		if rs.Type != "aws_vpc_peering_connection" {
			continue
		}

		describe, err := conn.DescribeVpcPeeringConnections(
			&ec2.DescribeVpcPeeringConnectionsInput{
				VpcPeeringConnectionIds: []*string{aws.String(rs.Primary.ID)},
			})

		if err == nil {
			if len(describe.VpcPeeringConnections) != 0 {
				return fmt.Errorf("vpc peering connection still exists")
			}
		}
	}

	return nil
}

func testAccCheckAWSVpcPeeringConnectionExists(n string, connection *ec2.VpcPeeringConnection) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		rs, ok := s.RootModule().Resources[n]
		if !ok {
			return fmt.Errorf("Not found: %s", n)
		}

		if rs.Primary.ID == "" {
			return fmt.Errorf("No vpc peering connection id is set")
		}

		conn := testAccProvider.Meta().(*AWSClient).ec2conn
		resp, err := conn.DescribeVpcPeeringConnections(
			&ec2.DescribeVpcPeeringConnectionsInput{
				VpcPeeringConnectionIds: []*string{aws.String(rs.Primary.ID)},
			})
		if err != nil {
			return err
		}
		if len(resp.VpcPeeringConnections) == 0 {
			return fmt.Errorf("VPC peering connection not found")
		}

		*connection = *resp.VpcPeeringConnections[0]

		return nil
	}
}

const testAccVpcPeeringConfig = `
resource "aws_vpc" "foo" {
		cidr_block = "10.0.0.0/16"
}

resource "aws_vpc" "bar" {
		cidr_block = "10.1.0.0/16"
}

resource "aws_vpc_peering_connection" "foo" {
		vpc_id = "${aws_vpc.foo.id}"
		peer_vpc_id = "${aws_vpc.bar.id}"
		auto_accept = true
}
`

const testAccVpcPeeringConfigTags = `
resource "aws_vpc" "foo" {
		cidr_block = "10.0.0.0/16"
}

resource "aws_vpc" "bar" {
		cidr_block = "10.1.0.0/16"
}

resource "aws_vpc_peering_connection" "foo" {
		vpc_id = "${aws_vpc.foo.id}"
		peer_vpc_id = "${aws_vpc.bar.id}"
		peer_owner_id = "%s"
		tags {
			foo = "bar"
		}
}
`
<|endoftext|>


<commit_before>package spawn

import (
	"testing"
)

func test(t *testing.T, expected bool, messages ...interface{}) {
	if !expected {
		t.Error(messages)
	}
}

func TestIsAlphaNumeric(t *testing.T) {
	str1 := "abcd1234.35_df-12"
	test(t, isAlphaNumeric(str1), "Expected "+str1+" is alpha numeric, got false")
	str2 := "abcd1234.%*35_df-12"
	test(t, !isAlphaNumeric(str2), "Expected "+str2+" is not alpha numeric, got true")
}
<commit_msg>move method 'loadFixtures' into helper<commit_after>package spawn

import (
	"bufio"
	"encoding/json"
	"os"
	"testing"
)

// loadFixtures - loads fixtures
func loadFixtures(path string) (nodes []Node, err error) {
	_, err = os.Stat(path)
	if err != nil {
		return
	}
	file, err := os.Open(path)
	if err != nil {
		return
	}
	defer file.Close()
	if err = json.NewDecoder(bufio.NewReader(file)).Decode(&nodes); err != nil {
		return
	}

	return
}

func test(t *testing.T, expected bool, messages ...interface{}) {
	if !expected {
		t.Error(messages)
	}
}

func TestIsAlphaNumeric(t *testing.T) {
	str1 := "abcd1234.35_df-12"
	test(t, isAlphaNumeric(str1), "Expected "+str1+" is alpha numeric, got false")
	str2 := "abcd1234.%*35_df-12"
	test(t, !isAlphaNumeric(str2), "Expected "+str2+" is not alpha numeric, got true")
}
<|endoftext|>


<commit_before>package main

import (
	"errors"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"text/template"

	"github.com/gorilla/mux"
)

func (b *blog) customHandler(response http.ResponseWriter, request *http.Request) {
	vars := mux.Vars(request)
	custom := vars["custom"]

	if custom == "login.html" || custom == "edit.html" || custom == "index.html" || custom == "login.html" || custom == "post.html" {
		response.WriteHeader(http.StatusNotFound)
		log.Printf("%v \n", errors.New("Not found"))
		fmt.Fprintf(response, "%v \n", errors.New("Not found"))
		return
	}

	bufIndexPage, err := ioutil.ReadFile("skins/" + b.theme + "/" + custom)
	if err == nil {
		indexPage := string(bufIndexPage)

		t := template.Must(template.New("indexPage").Parse(indexPage))

		t.Execute(response, nil)
	} else {
		response.WriteHeader(http.StatusNotFound)
		log.Printf("%v \n", errors.New("Not found"))
		fmt.Fprintf(response, "%v \n", errors.New("Not found"))
		return
	}
}
<commit_msg>handlers: adding blog attributes to custom pages<commit_after>package main

import (
	"errors"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"text/template"

	"github.com/gorilla/mux"
)

type customPage struct {
	BlogTitle       string
	BlogDescription string
	Author          string
}

func (b *blog) customHandler(response http.ResponseWriter, request *http.Request) {
	vars := mux.Vars(request)
	custom := vars["custom"]

	if custom == "login.html" || custom == "edit.html" || custom == "index.html" || custom == "login.html" || custom == "post.html" {
		response.WriteHeader(http.StatusNotFound)
		log.Printf("%v \n", errors.New("Not found"))
		fmt.Fprintf(response, "%v \n", errors.New("Not found"))
		return
	}

	bufCustomPage, err := ioutil.ReadFile("skins/" + b.theme + "/" + custom)
	if err == nil {
		var page = customPage{
			BlogTitle:       b.blogTitle,
			BlogDescription: b.blogDescription,
			Author:          b.authorName}

		customPage := string(bufCustomPage)

		t := template.Must(template.New("customPage").Parse(customPage))

		t.Execute(response, page)
	} else {
		response.WriteHeader(http.StatusNotFound)
		log.Printf("%v \n", errors.New("Not found"))
		fmt.Fprintf(response, "%v \n", errors.New("Not found"))
		return
	}
}
<|endoftext|>


<commit_before>package server

import (
	"github.com/nightexcessive/excessiveircd/protocol"
	"github.com/sorcix/irc"
)

// CommandError is an error returned by a Command Func.
type CommandError struct {
	// Numeric is the numeric to use when sending the CommandError to the
	// Client.
	// If Numeric is an empty string, Param[0] is sent as a server notification
	// instead of a numeric.
	Numeric string
	Params  []string
}

// Command represents a command specification.
type Command struct {
	Func func(client *Client, message *irc.Message) *CommandError

	MinimumParams int

	Registered   bool
	Unregistered bool
}

var commands = map[string]*Command{
	//irc.PASS: {cmdRegistration, 1, false, true},
	irc.NICK: {cmdRegistration, 1, true, true},
	irc.USER: {cmdRegistration, 4, false, true},
}

// cmdChangeNick is called when an already registered user uses the NICK command.
func cmdChangeNick(c *Client, m *irc.Message, nick string) *CommandError {
	reply := make(chan bool)
	c.Server.Events <- &SReregisterClient{nick, c, reply}
	if !<-reply {
		return &CommandError{irc.ERR_NICKNAMEINUSE, []string{nick, "Nickname is already in use"}}
	}
	c.writeMessage(&irc.Message{
		Prefix:   c.Info.Prefix,
		Command:  "NICK",
		Params:   nil,
		Trailing: nick,
	})
	c.Info.Name = nick
	return nil
}

func cmdRegistration(c *Client, m *irc.Message) *CommandError {
	switch m.Command {
	case irc.NICK:
		nick := m.Params[0]

		if !protocol.IsValid(nick, protocol.Nickname) {
			return &CommandError{irc.ERR_ERRONEUSNICKNAME, []string{nick, "Erroneous nickname"}}
		}

		if c.Registered {
			return cmdChangeNick(c, m, nick)
		}

		c.Info.Name = nick
	case irc.USER:
		user := m.Params[0]

		if !protocol.IsValid(user, protocol.Username) {
			c.error("Invalid user name given")
			return nil
		}

		c.Info.User = "~" + user
	default:
		c.Logger.Printf("Unexpected registration command: %q", m.Command)
		return nil
	}

	// Registered connections should never reach this section.

	if c.Info.Name != "*" && c.Info.User != "*" {
		reply := make(chan bool)
		c.Server.Events <- &SRegisterClient{c, reply}

		if !<-reply {
			return &CommandError{irc.ERR_NICKNAMEINUSE, []string{c.Info.Name, "Nickname is already in use"}}
		}

		c.Registered = true
		c.numeric(irc.RPL_WELCOME, "Welcome to the Internet Relay Network "+c.Info.String())
	}

	return nil
}
<commit_msg>User irc.NICK instead of "NICK".<commit_after>package server

import (
	"github.com/nightexcessive/excessiveircd/protocol"
	"github.com/sorcix/irc"
)

// CommandError is an error returned by a Command Func.
type CommandError struct {
	// Numeric is the numeric to use when sending the CommandError to the
	// Client.
	// If Numeric is an empty string, Param[0] is sent as a server notification
	// instead of a numeric.
	Numeric string
	Params  []string
}

// Command represents a command specification.
type Command struct {
	Func func(client *Client, message *irc.Message) *CommandError

	MinimumParams int

	Registered   bool
	Unregistered bool
}

var commands = map[string]*Command{
	//irc.PASS: {cmdRegistration, 1, false, true},
	irc.NICK: {cmdRegistration, 1, true, true},
	irc.USER: {cmdRegistration, 4, false, true},
}

// cmdChangeNick is called when an already registered user uses the NICK command.
func cmdChangeNick(c *Client, m *irc.Message, nick string) *CommandError {
	reply := make(chan bool)
	c.Server.Events <- &SReregisterClient{nick, c, reply}
	if !<-reply {
		return &CommandError{irc.ERR_NICKNAMEINUSE, []string{nick, "Nickname is already in use"}}
	}
	c.writeMessage(&irc.Message{
		Prefix:   c.Info.Prefix,
		Command:  irc.NICK,
		Params:   nil,
		Trailing: nick,
	})
	c.Info.Name = nick
	return nil
}

func cmdRegistration(c *Client, m *irc.Message) *CommandError {
	switch m.Command {
	case irc.NICK:
		nick := m.Params[0]

		if !protocol.IsValid(nick, protocol.Nickname) {
			return &CommandError{irc.ERR_ERRONEUSNICKNAME, []string{nick, "Erroneous nickname"}}
		}

		if c.Registered {
			return cmdChangeNick(c, m, nick)
		}

		c.Info.Name = nick
	case irc.USER:
		user := m.Params[0]

		if !protocol.IsValid(user, protocol.Username) {
			c.error("Invalid user name given")
			return nil
		}

		c.Info.User = "~" + user
	default:
		c.Logger.Printf("Unexpected registration command: %q", m.Command)
		return nil
	}

	// Registered connections should never reach this section.

	if c.Info.Name != "*" && c.Info.User != "*" {
		reply := make(chan bool)
		c.Server.Events <- &SRegisterClient{c, reply}

		if !<-reply {
			return &CommandError{irc.ERR_NICKNAMEINUSE, []string{c.Info.Name, "Nickname is already in use"}}
		}

		c.Registered = true
		c.numeric(irc.RPL_WELCOME, "Welcome to the Internet Relay Network "+c.Info.String())
	}

	return nil
}
<|endoftext|>