<commit_before>// config.go
//
// This file implements the configuration part for when you need the API
// key to modify things in the Mastodon configuration and manage measurements.

package gondole

import (
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"path/filepath"

	"github.com/naoina/toml"
)

/*
Assume the application is registered if $HOME/.config/<gondole>/config.toml already exist
We will store the per-instance token into $HOME/.config/<gondole>/<site>.token
*/

const (
	DefaultName = "config.toml"
)

var (
	baseDir = filepath.Join(os.Getenv("HOME"),
		".config",
		"gondole",
	)
)

// Config holds our parameters
type Server struct {
	ID          int64
	Name        string
	BearerToken string
}

type Config struct {
	Default string
}

func loadGlobal(file string) (c *Config, err error) {
	log.Printf("file=%s", file)
	// Check if there is any config file
	_, err = os.Stat(file)
	if err != nil {
		return
	}

	log.Printf("file=%s, found it", file)
	// Read it
	buf, err := ioutil.ReadFile(file)
	if err != nil {
		return c, fmt.Errorf("Can not read %s", file)
	}

	cnf := Config{}
	err = toml.Unmarshal(buf, &cnf)
	if err != nil {
		return c, fmt.Errorf("Error parsing toml %s: %v", file, err)
	}
	c = &cnf
	return
}

func loadInstance(name string) (s *Server, err error) {
	// Load instance-specific file
	file := filepath.Join(baseDir, name+".token")

	log.Printf("instance is %s", file)

	// Check if there is any config file
	if _, err := os.Stat(file); err == nil {
		// Read it
		buf, err := ioutil.ReadFile(file)
		if err != nil {
			return s, fmt.Errorf("Can not read %s", file)
		}

		err = toml.Unmarshal(buf, s)
		if err != nil {
			return s, fmt.Errorf("Error parsing toml %s: %v", file, err)
		}
	}
	return
}

func GetInstanceList() (list []string) {
	list, err := filepath.Glob(filepath.Join(baseDir + "*.token"))
	if err != nil {
		log.Printf("warning, no *.token files")
		list = nil
	}
	return
}

// LoadConfig reads a file as a TOML document and return the structure
func LoadConfig(name string) (s *Server, err error) {
	// Load global file
	gFile := filepath.Join(baseDir, DefaultName)

	log.Printf("global is %s", gFile)
	c, err := loadGlobal(gFile)
	if err != nil {
		return
	}
	if name == "" {
		s, err = loadInstance(c.Default)
	} else {
		s, err = loadInstance(name)
	}

	return s, err
}

func (c *Config) Write() (err error) {
	if err = os.MkdirAll(baseDir, 0700); err != nil {
		log.Fatalf("error creating configuration directory: %s", baseDir)
	}

	var sc []byte

	if sc, err = toml.Marshal(*c); err != nil {
		log.Fatalf("error saving configuration")
	}
	err = ioutil.WriteFile(filepath.Join(baseDir, DefaultName), sc, 0600)
	return
}

func (s *Server) WriteToken(instance string) (err error) {
	if err = os.MkdirAll(baseDir, 0700); err != nil {
		log.Fatalf("error creating configuration directory: %s", baseDir)
	}

	var sc []byte

	if sc, err = toml.Marshal(s); err != nil {
		log.Fatalf("error saving configuration")
	}

	full := instance + ".token"
	err = ioutil.WriteFile(filepath.Join(baseDir, full), sc, 0600)
	return
}
<commit_msg>Unshadow variable, fix test.<commit_after>// config.go
//
// This file implements the configuration part for when you need the API
// key to modify things in the Mastodon configuration and manage measurements.

package gondole

import (
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"path/filepath"

	"github.com/naoina/toml"
)

/*
Assume the application is registered if $HOME/.config/<gondole>/config.toml already exist
We will store the per-instance token into $HOME/.config/<gondole>/<site>.token
*/

const (
	DefaultName = "config.toml"
)

var (
	baseDir = filepath.Join(os.Getenv("HOME"),
		".config",
		"gondole",
	)
)

// Config holds our parameters
type Server struct {
	ID          int64
	Name        string
	BearerToken string
}

type Config struct {
	Default string
}

func loadGlobal(file string) (c *Config, err error) {
	log.Printf("file=%s", file)
	// Check if there is any config file
	_, err = os.Stat(file)
	if err != nil {
		return
	}

	log.Printf("file=%s, found it", file)
	// Read it
	buf, err := ioutil.ReadFile(file)
	if err != nil {
		return c, fmt.Errorf("Can not read %s", file)
	}

	cnf := Config{}
	err = toml.Unmarshal(buf, &cnf)
	if err != nil {
		return c, fmt.Errorf("Error parsing toml %s: %v", file, err)
	}
	c = &cnf
	return
}

func loadInstance(name string) (s *Server, err error) {
	// Load instance-specific file
	file := filepath.Join(baseDir, name+".token")

	log.Printf("instance is %s", file)

	// Check if there is any config file
	if _, err = os.Stat(file); err == nil {
		// Read it
		buf, err := ioutil.ReadFile(file)
		if err != nil {
			return s, fmt.Errorf("Can not read %s", file)
		}

		err = toml.Unmarshal(buf, s)
		if err != nil {
			return s, fmt.Errorf("Error parsing toml %s: %v", file, err)
		}
	}
	return
}

func GetInstanceList() (list []string) {
	list, err := filepath.Glob(filepath.Join(baseDir + "*.token"))
	if err != nil {
		log.Printf("warning, no *.token files")
		list = nil
	}
	return
}

// LoadConfig reads a file as a TOML document and return the structure
func LoadConfig(name string) (s *Server, err error) {
	// Load global file
	gFile := filepath.Join(baseDir, DefaultName)

	log.Printf("global is %s", gFile)
	c, err := loadGlobal(gFile)
	if err != nil {
		return
	}
	if name == "" {
		s, err = loadInstance(c.Default)
	} else {
		s, err = loadInstance(name)
	}

	return s, err
}

func (c *Config) Write() (err error) {
	if err = os.MkdirAll(baseDir, 0700); err != nil {
		log.Fatalf("error creating configuration directory: %s", baseDir)
	}

	var sc []byte

	if sc, err = toml.Marshal(*c); err != nil {
		log.Fatalf("error saving configuration")
	}
	err = ioutil.WriteFile(filepath.Join(baseDir, DefaultName), sc, 0600)
	return
}

func (s *Server) WriteToken(instance string) (err error) {
	if err = os.MkdirAll(baseDir, 0700); err != nil {
		log.Fatalf("error creating configuration directory: %s", baseDir)
	}

	var sc []byte

	if sc, err = toml.Marshal(s); err != nil {
		log.Fatalf("error saving configuration")
	}

	full := instance + ".token"
	err = ioutil.WriteFile(filepath.Join(baseDir, full), sc, 0600)
	return
}
<|endoftext|>


<commit_before>// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.

// +build !sqlite_omit_load_extension

package sqlite3

/*
#ifndef USE_LIBSQLITE3
#include <sqlite3-binding.h>
#else
#include <sqlite3.h>
#endif
#include <stdlib.h>
*/
import "C"
import (
	"errors"
	"unsafe"
)

func (c *SQLiteConn) loadExtensions(extensions []string) error {
	rv := C.sqlite3_enable_load_extension(c.db, 1)
	if rv != C.SQLITE_OK {
		return errors.New(C.GoString(C.sqlite3_errmsg(c.db)))
	}

	for _, extension := range extensions {
		if err := c.loadExtension(extension, nil); err != nil {
			C.sqlite3_enable_load_extension(c.db, 0)
			return err
		}
	}

	rv = C.sqlite3_enable_load_extension(c.db, 0)
	if rv != C.SQLITE_OK {
		return errors.New(C.GoString(C.sqlite3_errmsg(c.db)))
	}

	return nil
}

// LoadExtension load the sqlite3 extension.
func (c *SQLiteConn) LoadExtension(lib string, entry string) error {
	rv := C.sqlite3_enable_load_extension(c.db, 1)
	if rv != C.SQLITE_OK {
		return errors.New(C.GoString(C.sqlite3_errmsg(c.db)))
	}

	if err := c.loadExtension(lib, &entry); err != nil {
		C.sqlite3_enable_load_extension(c.db, 0)
		return err
	}

	rv = C.sqlite3_enable_load_extension(c.db, 0)
	if rv != C.SQLITE_OK {
		return errors.New(C.GoString(C.sqlite3_errmsg(c.db)))
	}

	return nil
}

func (c *SQLiteConn) loadExtension(lib string, entry *string) error {
	clib := C.CString(lib)
	defer C.free(unsafe.Pointer(clib))

	var centry *C.char
	if entry != nil {
		centry := C.CString(*entry)
		defer C.free(unsafe.Pointer(centry))
	}

	var errMsg *C.char
	defer C.sqlite3_free(unsafe.Pointer(errMsg))

	rv := C.sqlite3_load_extension(c.db, clib, centry, &errMsg)
	if rv != C.SQLITE_OK {
		return errors.New(C.GoString(errMsg))
	}

	return nil
}
<commit_msg>Fix #860 extenstion entry bug (#861)<commit_after>// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.

// +build !sqlite_omit_load_extension

package sqlite3

/*
#ifndef USE_LIBSQLITE3
#include <sqlite3-binding.h>
#else
#include <sqlite3.h>
#endif
#include <stdlib.h>
*/
import "C"
import (
	"errors"
	"unsafe"
)

func (c *SQLiteConn) loadExtensions(extensions []string) error {
	rv := C.sqlite3_enable_load_extension(c.db, 1)
	if rv != C.SQLITE_OK {
		return errors.New(C.GoString(C.sqlite3_errmsg(c.db)))
	}

	for _, extension := range extensions {
		if err := c.loadExtension(extension, nil); err != nil {
			C.sqlite3_enable_load_extension(c.db, 0)
			return err
		}
	}

	rv = C.sqlite3_enable_load_extension(c.db, 0)
	if rv != C.SQLITE_OK {
		return errors.New(C.GoString(C.sqlite3_errmsg(c.db)))
	}

	return nil
}

// LoadExtension load the sqlite3 extension.
func (c *SQLiteConn) LoadExtension(lib string, entry string) error {
	rv := C.sqlite3_enable_load_extension(c.db, 1)
	if rv != C.SQLITE_OK {
		return errors.New(C.GoString(C.sqlite3_errmsg(c.db)))
	}

	if err := c.loadExtension(lib, &entry); err != nil {
		C.sqlite3_enable_load_extension(c.db, 0)
		return err
	}

	rv = C.sqlite3_enable_load_extension(c.db, 0)
	if rv != C.SQLITE_OK {
		return errors.New(C.GoString(C.sqlite3_errmsg(c.db)))
	}

	return nil
}

func (c *SQLiteConn) loadExtension(lib string, entry *string) error {
	clib := C.CString(lib)
	defer C.free(unsafe.Pointer(clib))

	var centry *C.char
	if entry != nil {
		centry = C.CString(*entry)
		defer C.free(unsafe.Pointer(centry))
	}

	var errMsg *C.char
	defer C.sqlite3_free(unsafe.Pointer(errMsg))

	rv := C.sqlite3_load_extension(c.db, clib, centry, &errMsg)
	if rv != C.SQLITE_OK {
		return errors.New(C.GoString(errMsg))
	}

	return nil
}
<|endoftext|>


<commit_before>package file

import "errors"
import "strings"
import "regexp"

type Buffer []Line

func (buffer Buffer) Dup() Buffer {
	bufCopy := make(Buffer, len(buffer))
	for row, line := range buffer {
		bufCopy[row] = line
	}
	return bufCopy
}

func (buffer Buffer) DeepDup() Buffer {
	bufCopy := make(Buffer, len(buffer))
	for row, line := range buffer {
		bufCopy[row] = line.Dup()
	}
	return bufCopy
}

func MakeBuffer(stringBuf []string) Buffer {
	buffer := make(Buffer, len(stringBuf))
	for row, str := range stringBuf {
		buffer[row] = Line(str)
	}
	return buffer
}

func (buffer Buffer) ToString() string {
	str := ""
	for _, line := range buffer {
		str += string(line) + "\n"
	}
	return str[:len(str)-1]
}

func (buffer Buffer) Search(searchTerm string, cursor Cursor) (int, int, error) {
	var col int
	col, _ = buffer[cursor.row].Search(searchTerm, cursor.col+1, -1)
	if col >= 0 {
		return cursor.row, col, nil
	}
	for row := cursor.row + 1; row < len(buffer); row++ {
		col, _ = buffer[row].Search(searchTerm, 0, -1)
		if col >= 0 {
			return row, col, nil
		}
	}
	for row := 0; row < cursor.row; row++ {
		col, _ = buffer[row].Search(searchTerm, 0, -1)
		if col >= 0 {
			return row, col, nil
		}
	}
	col, _ = buffer[cursor.row].Search(searchTerm, 0, col)
	if col >= 0 {
		return cursor.row, col, nil
	}
	return cursor.row, cursor.col, errors.New("Not Found")
}

func (buffer *Buffer) Replace(searchTerm, replaceTerm string, row, col int) {
	startCol, endCol := (*buffer)[row].Search(searchTerm, col, -1)
	strLine := string((*buffer)[row])
	newStrLine := strLine[:startCol] + replaceTerm + strLine[endCol:]
	(*buffer)[row] = Line(newStrLine)
}

func (buffer Buffer) slice(startRow, endRow, startCol, endCol int) []string {
	slice := make([]string, endRow-startRow)
	for row := startRow; row < endRow; row++ {
		line := buffer[row].tabs2spaces()
		rowEndCol := endCol
		if rowEndCol > len(line) {
			rowEndCol = len(line)
		}
		if rowEndCol <= startCol {
			slice[row-startRow] = ""
		} else {
			slice[row-startRow] = string(line[startCol:rowEndCol])
		}
	}
	return slice
}

func (buffer Buffer) GetIndent() (string, bool) {
	spaceHisto := buffer.countLeadingSpacesAndTabs()
	tabCount := spaceHisto[0]
	nSpaces, spaceCount := buffer.scoreIndents(spaceHisto)
	clean := true
	if tabCount > 0 && spaceCount > 0 {
		clean = false
	}
	if tabCount > spaceCount {
		return "\t", clean
	} else {
		return strings.Repeat(" ", nSpaces), clean
	}
}

func (buffer Buffer) countLeadingSpacesAndTabs() []int {
	spaceHisto := make([]int, 33)
	re := regexp.MustCompile("^[ \t]*")
	for _, line := range buffer {
		indentStr := re.FindString(line.toString())
		nSpaces := strings.Count(indentStr, " ")
		nTabs := strings.Count(indentStr, "\t")
		if nSpaces > 0 && nSpaces <= 32 {
			spaceHisto[nSpaces]++
		}
		if nTabs > 0 {
			spaceHisto[0]++
		}
	}
	return spaceHisto
}

func (buffer Buffer) scoreIndents(spaceHisto []int) (int, int) {
	count := 0
	nSpaces := 0
	for indentSize := 1; indentSize < 9; indentSize++ {
		score := 0
		for n := 1; n <= 4; n++ {
			score += spaceHisto[n*indentSize]
		}
		if score > count && spaceHisto[indentSize] > 0 {
			nSpaces = indentSize
			count = score
		}
	}
	return nSpaces, count
}
<commit_msg>bugfix: if tabcount and spacecount are both zero, indent string = '\t'<commit_after>package file

import "errors"
import "strings"
import "regexp"

type Buffer []Line

func (buffer Buffer) Dup() Buffer {
	bufCopy := make(Buffer, len(buffer))
	for row, line := range buffer {
		bufCopy[row] = line
	}
	return bufCopy
}

func (buffer Buffer) DeepDup() Buffer {
	bufCopy := make(Buffer, len(buffer))
	for row, line := range buffer {
		bufCopy[row] = line.Dup()
	}
	return bufCopy
}

func MakeBuffer(stringBuf []string) Buffer {
	buffer := make(Buffer, len(stringBuf))
	for row, str := range stringBuf {
		buffer[row] = Line(str)
	}
	return buffer
}

func (buffer Buffer) ToString() string {
	str := ""
	for _, line := range buffer {
		str += string(line) + "\n"
	}
	return str[:len(str)-1]
}

func (buffer Buffer) Search(searchTerm string, cursor Cursor) (int, int, error) {
	var col int
	col, _ = buffer[cursor.row].Search(searchTerm, cursor.col+1, -1)
	if col >= 0 {
		return cursor.row, col, nil
	}
	for row := cursor.row + 1; row < len(buffer); row++ {
		col, _ = buffer[row].Search(searchTerm, 0, -1)
		if col >= 0 {
			return row, col, nil
		}
	}
	for row := 0; row < cursor.row; row++ {
		col, _ = buffer[row].Search(searchTerm, 0, -1)
		if col >= 0 {
			return row, col, nil
		}
	}
	col, _ = buffer[cursor.row].Search(searchTerm, 0, col)
	if col >= 0 {
		return cursor.row, col, nil
	}
	return cursor.row, cursor.col, errors.New("Not Found")
}

func (buffer *Buffer) Replace(searchTerm, replaceTerm string, row, col int) {
	startCol, endCol := (*buffer)[row].Search(searchTerm, col, -1)
	strLine := string((*buffer)[row])
	newStrLine := strLine[:startCol] + replaceTerm + strLine[endCol:]
	(*buffer)[row] = Line(newStrLine)
}

func (buffer Buffer) slice(startRow, endRow, startCol, endCol int) []string {
	slice := make([]string, endRow-startRow)
	for row := startRow; row < endRow; row++ {
		line := buffer[row].tabs2spaces()
		rowEndCol := endCol
		if rowEndCol > len(line) {
			rowEndCol = len(line)
		}
		if rowEndCol <= startCol {
			slice[row-startRow] = ""
		} else {
			slice[row-startRow] = string(line[startCol:rowEndCol])
		}
	}
	return slice
}

func (buffer Buffer) GetIndent() (string, bool) {
	spaceHisto := buffer.countLeadingSpacesAndTabs()
	tabCount := spaceHisto[0]
	nSpaces, spaceCount := buffer.scoreIndents(spaceHisto)
	clean := true
	if tabCount > 0 && spaceCount > 0 {
		clean = false
	}
	if tabCount >= spaceCount {
		return "\t", clean
	} else {
		return strings.Repeat(" ", nSpaces), clean
	}
}

func (buffer Buffer) countLeadingSpacesAndTabs() []int {
	spaceHisto := make([]int, 33)
	re := regexp.MustCompile("^[ \t]*")
	for _, line := range buffer {
		indentStr := re.FindString(line.toString())
		nSpaces := strings.Count(indentStr, " ")
		nTabs := strings.Count(indentStr, "\t")
		if nSpaces > 0 && nSpaces <= 32 {
			spaceHisto[nSpaces]++
		}
		if nTabs > 0 {
			spaceHisto[0]++
		}
	}
	return spaceHisto
}

func (buffer Buffer) scoreIndents(spaceHisto []int) (int, int) {
	count := 0
	nSpaces := 0
	for indentSize := 1; indentSize < 9; indentSize++ {
		score := 0
		for n := 1; n <= 4; n++ {
			score += spaceHisto[n*indentSize]
		}
		if score > count && spaceHisto[indentSize] > 0 {
			nSpaces = indentSize
			count = score
		}
	}
	return nSpaces, count
}
<|endoftext|>


<commit_before>package application_test

import (
	"io/ioutil"
	"os"
	"path/filepath"
	"runtime"
	"strings"
	"time"

	"github.com/cloudfoundry-incubator/cf-test-helpers/cf"
	"github.com/cloudfoundry-incubator/cf-test-helpers/generator"
	catshelpers "github.com/cloudfoundry-incubator/cf-test-helpers/helpers"
	"github.com/cloudfoundry/cli-acceptance-tests/helpers"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/gexec"
)

var _ = Describe("Push", func() {
	var (
		assets        helpers.Assets
		setupTimeout  time.Duration
		targetTimeout time.Duration
		pushTimeout   time.Duration
		context       *catshelpers.ConfiguredContext
		env           *catshelpers.Environment
	)

	BeforeEach(func() {
		assets = helpers.NewAssets()
		setupTimeout = 20 * time.Second
		targetTimeout = 10 * time.Second
		pushTimeout = 1 * time.Minute

		config := catshelpers.LoadConfig()
		context = catshelpers.NewContext(config)
		env = catshelpers.NewEnvironment(context)

		env.Setup()
	})

	AfterEach(func() {
		env.Teardown()
	})

	Context("when pushing an app with >248 character paths, which are .cfignored", func() {
		var (
			longPath     string
			cfIgnorePath string
		)

		BeforeEach(func() {
			longDirName := strings.Repeat("i", 247)
			longPath = filepath.Join(assets.ServiceBroker, longDirName)

			if runtime.GOOS == "windows" {
				cwd, err := os.Getwd()
				Expect(err).NotTo(HaveOccurred())

				// `\\?\` is used to skip Windows' file name processor, which imposes
				// length limits. Search MSDN for 'Maximum Path Length Limitation' for
				// more.
				err := os.MkdirAll(`\\?\`+filepath.Join(cwd, longPath), os.ModeDir|os.ModePerm)
				Expect(err).NotTo(HaveOccurred())
			} else {
				err := os.MkdirAll(longPath, os.ModeDir|os.ModePerm)
				Expect(err).NotTo(HaveOccurred())
			}

			cfIgnorePath = filepath.Join(assets.ServiceBroker, ".cfignore")
			cfIgnoreContents := []byte(longDirName + "\n")
			err = ioutil.WriteFile(cfIgnorePath, cfIgnoreContents, 0644)
			Expect(err).NotTo(HaveOccurred())
		})

		AfterEach(func() {
			err := os.RemoveAll(cfIgnorePath)
			Expect(err).NotTo(HaveOccurred())
			if runtime.GOOS == "windows" {
				// `\\?\` is used to skip Windows' file name processor, which imposes
				// length limits. Search MSDN for 'Maximum Path Length Limitation' for
				// more.
				err = os.RemoveAll(`\\?\` + filepath.Join(cwd, longPath))
				Expect(err).NotTo(HaveOccurred())
			} else {
				err = os.RemoveAll(longPath)
				Expect(err).NotTo(HaveOccurred())
			}
		})

		It("is successful", func() {
			cf.AsUser(context.RegularUserContext(), setupTimeout, func() {
				space := context.RegularUserContext().Space
				org := context.RegularUserContext().Org

				target := cf.Cf("target", "-o", org, "-s", space).Wait(targetTimeout)
				Expect(target.ExitCode()).To(Equal(0))

				appName := generator.RandomName()
				session := cf.Cf("push", appName, "-p", assets.ServiceBroker).Wait(pushTimeout)
				Expect(session).To(gexec.Exit(0))
			})
		})
	})
})
<commit_msg>Fix compilation error from previous commit<commit_after>package application_test

import (
	"io/ioutil"
	"os"
	"path/filepath"
	"runtime"
	"strings"
	"time"

	"github.com/cloudfoundry-incubator/cf-test-helpers/cf"
	"github.com/cloudfoundry-incubator/cf-test-helpers/generator"
	catshelpers "github.com/cloudfoundry-incubator/cf-test-helpers/helpers"
	"github.com/cloudfoundry/cli-acceptance-tests/helpers"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/gexec"
)

var _ = Describe("Push", func() {
	var (
		assets        helpers.Assets
		setupTimeout  time.Duration
		targetTimeout time.Duration
		pushTimeout   time.Duration
		context       *catshelpers.ConfiguredContext
		env           *catshelpers.Environment
	)

	BeforeEach(func() {
		assets = helpers.NewAssets()
		setupTimeout = 20 * time.Second
		targetTimeout = 10 * time.Second
		pushTimeout = 1 * time.Minute

		config := catshelpers.LoadConfig()
		context = catshelpers.NewContext(config)
		env = catshelpers.NewEnvironment(context)

		env.Setup()
	})

	AfterEach(func() {
		env.Teardown()
	})

	Context("when pushing an app with >248 character paths, which are .cfignored", func() {
		var (
			longPath     string
			cfIgnorePath string
			cwd          string
		)

		BeforeEach(func() {
			longDirName := strings.Repeat("i", 247)
			longPath = filepath.Join(assets.ServiceBroker, longDirName)

			if runtime.GOOS == "windows" {
				var err error
				cwd, err = os.Getwd()
				Expect(err).NotTo(HaveOccurred())

				// `\\?\` is used to skip Windows' file name processor, which imposes
				// length limits. Search MSDN for 'Maximum Path Length Limitation' for
				// more.
				err = os.MkdirAll(`\\?\`+filepath.Join(cwd, longPath), os.ModeDir|os.ModePerm)
				Expect(err).NotTo(HaveOccurred())
			} else {
				err := os.MkdirAll(longPath, os.ModeDir|os.ModePerm)
				Expect(err).NotTo(HaveOccurred())
			}

			cfIgnorePath = filepath.Join(assets.ServiceBroker, ".cfignore")
			cfIgnoreContents := []byte(longDirName + "\n")
			err := ioutil.WriteFile(cfIgnorePath, cfIgnoreContents, 0644)
			Expect(err).NotTo(HaveOccurred())
		})

		AfterEach(func() {
			err := os.RemoveAll(cfIgnorePath)
			Expect(err).NotTo(HaveOccurred())
			if runtime.GOOS == "windows" {
				// `\\?\` is used to skip Windows' file name processor, which imposes
				// length limits. Search MSDN for 'Maximum Path Length Limitation' for
				// more.
				err := os.RemoveAll(`\\?\` + filepath.Join(cwd, longPath))
				Expect(err).NotTo(HaveOccurred())
			} else {
				err = os.RemoveAll(longPath)
				Expect(err).NotTo(HaveOccurred())
			}
		})

		It("is successful", func() {
			cf.AsUser(context.RegularUserContext(), setupTimeout, func() {
				space := context.RegularUserContext().Space
				org := context.RegularUserContext().Org

				target := cf.Cf("target", "-o", org, "-s", space).Wait(targetTimeout)
				Expect(target.ExitCode()).To(Equal(0))

				appName := generator.RandomName()
				session := cf.Cf("push", appName, "-p", assets.ServiceBroker).Wait(pushTimeout)
				Expect(session).To(gexec.Exit(0))
			})
		})
	})
})
<|endoftext|>


<commit_before>package filter

import (
	"bufio"
	"bytes"
	"io"
)

// Base is base of filters.  It provides common features for filter.
type Base struct {
	Reader *bufio.Reader
	buf    bytes.Buffer
	closed bool
	raw    io.ReadCloser
	rn     BaseReadNext
}

// BaseReadNext is callback to read next data hunk to buf
type BaseReadNext func(buf *bytes.Buffer) error

// Init initializes Base object.
func (b *Base) Init(r io.ReadCloser, readNext BaseReadNext) {
	b.Reader = bufio.NewReader(r)
	b.raw = r
	b.rn = readNext
}

func (b *Base) Read(buf []byte) (int, error) {
	if b.buf.Len() == 0 {
		if b.closed {
			return 0, io.EOF
		}
		// read next data to b.buf
		b.buf.Reset()
		err := b.rn(&b.buf)
		if err == io.EOF {
			b.Close()
		} else if err != nil {
			return 0, err
		}
	}
	return b.buf.Read(buf)
}

// ReadLine reads a line as []byte.
func (b *Base) ReadLine() ([]byte, error) {
	d, err := b.Reader.ReadSlice('\n')
	if err == nil || err == io.EOF {
		return d, err
	} else if err != bufio.ErrBufferFull {
		return nil, err
	}
	bb := bytes.NewBuffer(d)
	for {
		b2, err := b.Reader.ReadSlice('\n')
		if len(b2) > 0 {
			if _, err := bb.Write(b2); err != nil {
				return nil, err
			}
		}
		if err == nil || err == io.EOF {
			return bb.Bytes(), nil
		}
		if err != bufio.ErrBufferFull {
			return nil, err
		}
	}
}

// Close closes head filter.
func (b *Base) Close() error {
	if b.closed {
		return nil
	}
	b.closed = true
	return b.raw.Close()
}
<commit_msg>don't pass []byte from bufio.Reader to bytes.Buffer<commit_after>package filter

import (
	"bufio"
	"bytes"
	"io"
)

// Base is base of filters.  It provides common features for filter.
type Base struct {
	Reader *bufio.Reader
	buf    bytes.Buffer
	closed bool
	raw    io.ReadCloser
	rn     BaseReadNext
}

// BaseReadNext is callback to read next data hunk to buf
type BaseReadNext func(buf *bytes.Buffer) error

// Init initializes Base object.
func (b *Base) Init(r io.ReadCloser, readNext BaseReadNext) {
	b.Reader = bufio.NewReader(r)
	b.raw = r
	b.rn = readNext
}

func (b *Base) Read(buf []byte) (int, error) {
	if b.buf.Len() == 0 {
		if b.closed {
			return 0, io.EOF
		}
		// read next data to b.buf
		b.buf.Reset()
		err := b.rn(&b.buf)
		if err == io.EOF {
			b.Close()
		} else if err != nil {
			return 0, err
		}
	}
	return b.buf.Read(buf)
}

// ReadLine reads a line as []byte.
func (b *Base) ReadLine() ([]byte, error) {
	d, err := b.Reader.ReadSlice('\n')
	if err == nil || err == io.EOF {
		return d, err
	} else if err != bufio.ErrBufferFull {
		return nil, err
	}
	bb := bytes.Buffer{}
	if _, err := bb.Write(d); err != nil {
		return nil, err
	}
	for {
		b2, err := b.Reader.ReadSlice('\n')
		if len(b2) > 0 {
			if _, err := bb.Write(b2); err != nil {
				return nil, err
			}
		}
		if err == nil || err == io.EOF {
			return bb.Bytes(), nil
		}
		if err != bufio.ErrBufferFull {
			return nil, err
		}
	}
}

// Close closes head filter.
func (b *Base) Close() error {
	if b.closed {
		return nil
	}
	b.closed = true
	return b.raw.Close()
}
<|endoftext|>


<commit_before>//
// Copyright (c) 2016 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package uuid

import "testing"

// Test UUID parsing and string conversation.
//
// This test simply converts a set of strings to UUIDs and back again.
//
// The original strings and the strings generated from the UUIDs match.
func TestUUID(t *testing.T) {
	testUUIDs := []string{
		"f81d4fae-7dec-11d0-a765-00a0c91e6bf6",
		"30dedd5c-48d9-45d3-8b44-f973e4f35e48",
		"69e84267-ed01-4738-b15f-b47de06b62e7",
		"e35ed972-c46c-4aad-a1e7-ef103ae079a2",
		"eba04826-62a5-48bd-876f-9119667b1487",
		"ca957444-fa46-11e5-94f9-38607786d9ec",
		"ab68111c-03a6-11e6-87de-001320fb6e31",
	}

	for _, s := range testUUIDs {
		uuid, err := Parse(s)
		if err != nil {
			t.Fatalf("Unable to parse %s: %s", s, err)
		}
		s2 := uuid.String()
		if s != s2 {
			t.Fatalf("%s and %s do not match", s, s2)
		}
	}
}

// Test UUID generation.
//
// This test generates 100 new UUIDs and then verifies that those UUIDs
// can be parsed.
//
// The UUIDs are generated correctly, their version number is correct,
// and they can be parsed.
func TestGenUUID(t *testing.T) {
	for i := 0; i < 100; i++ {
		u := Generate()
		s := u.String()
		if s[14] != '4' {
			t.Fatalf("Invalid UUID.  Version number is incorrect")
		}
		u2, err := Parse(s)
		if err != nil {
			t.Fatalf("Failed to parse UUID %s : %s", s, err)
		}
		if u != u2 {
			t.Fatalf("Generated and Parsed UUIDs are not equal")
		}
	}
}
<commit_msg>ssntp/uuid: Add a negative test case.<commit_after>//
// Copyright (c) 2016 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package uuid

import "testing"

// Test UUID parsing and string conversation.
//
// This test simply converts a set of strings to UUIDs and back again.
//
// The original strings and the strings generated from the UUIDs match.
func TestUUID(t *testing.T) {
	testUUIDs := []string{
		"f81d4fae-7dec-11d0-a765-00a0c91e6bf6",
		"30dedd5c-48d9-45d3-8b44-f973e4f35e48",
		"69e84267-ed01-4738-b15f-b47de06b62e7",
		"e35ed972-c46c-4aad-a1e7-ef103ae079a2",
		"eba04826-62a5-48bd-876f-9119667b1487",
		"ca957444-fa46-11e5-94f9-38607786d9ec",
		"ab68111c-03a6-11e6-87de-001320fb6e31",
	}

	for _, s := range testUUIDs {
		uuid, err := Parse(s)
		if err != nil {
			t.Fatalf("Unable to parse %s: %s", s, err)
		}
		s2 := uuid.String()
		if s != s2 {
			t.Fatalf("%s and %s do not match", s, s2)
		}
	}
}

// Test UUID generation.
//
// This test generates 100 new UUIDs and then verifies that those UUIDs
// can be parsed.
//
// The UUIDs are generated correctly, their version number is correct,
// and they can be parsed.
func TestGenUUID(t *testing.T) {
	for i := 0; i < 100; i++ {
		u := Generate()
		s := u.String()
		if s[14] != '4' {
			t.Fatalf("Invalid UUID.  Version number is incorrect")
		}
		u2, err := Parse(s)
		if err != nil {
			t.Fatalf("Failed to parse UUID %s : %s", s, err)
		}
		if u != u2 {
			t.Fatalf("Generated and Parsed UUIDs are not equal")
		}
	}
}

// Test uuid.Parse on invalid input.
//
// This test attempts to parse a set of invalid UUIDs.
//
// uuid.Parse should return an error for each invalid UUID.
func TestBadUUID(t *testing.T) {
	badTestUUIDs := []string{
		"",
		"48d9-45d3-8b44-f973e4f35e48",
		"69e8426--ed01-4738-b15f-b47de06b62e7",
		"e35ed972-46c-4aad-a1e7-ef103ae079a2",
		"sba04826-62a5-48bd-876f-9119667b1487",
		"ca957444fa4611e594f938607786d9ec0000",
		"ab68111c-03a6-11e6-87de-001320fb6e31a",
	}

	for _, s := range badTestUUIDs {
		_, err := Parse(s)
		if err == nil {
			t.Fatalf("uuid.Parse should fail to parse %s", s)
		}
	}
}
<|endoftext|>


<commit_before>package osl

import (
	"bytes"
	"fmt"
	"net"

	"github.com/vishvananda/netlink"
)

// NeighOption is a function option type to set interface options
type NeighOption func(nh *neigh)

type neigh struct {
	dstIP    net.IP
	dstMac   net.HardwareAddr
	linkName string
	linkDst  string
	family   int
}

func (n *networkNamespace) findNeighbor(dstIP net.IP, dstMac net.HardwareAddr) *neigh {
	n.Lock()
	defer n.Unlock()

	for _, nh := range n.neighbors {
		if nh.dstIP.Equal(dstIP) && bytes.Equal(nh.dstMac, dstMac) {
			return nh
		}
	}

	return nil
}

func (n *networkNamespace) DeleteNeighbor(dstIP net.IP, dstMac net.HardwareAddr, osDelete bool) error {
	var (
		iface netlink.Link
		err   error
	)

	nh := n.findNeighbor(dstIP, dstMac)
	if nh == nil {
		return fmt.Errorf("could not find the neighbor entry to delete")
	}

	if osDelete {
		n.Lock()
		nlh := n.nlHandle
		n.Unlock()

		if nh.linkDst != "" {
			iface, err = nlh.LinkByName(nh.linkDst)
			if err != nil {
				return fmt.Errorf("could not find interface with destination name %s: %v",
					nh.linkDst, err)
			}
		}

		nlnh := &netlink.Neigh{
			IP:     dstIP,
			State:  netlink.NUD_PERMANENT,
			Family: nh.family,
		}

		if nlnh.Family > 0 {
			nlnh.HardwareAddr = dstMac
			nlnh.Flags = netlink.NTF_SELF
		}

		if nh.linkDst != "" {
			nlnh.LinkIndex = iface.Attrs().Index
		}

		if err := nlh.NeighDel(nlnh); err != nil {
			return fmt.Errorf("could not delete neighbor entry: %v", err)
		}
	}

	n.Lock()
	for i, nh := range n.neighbors {
		if nh.dstIP.Equal(dstIP) && bytes.Equal(nh.dstMac, dstMac) {
			n.neighbors = append(n.neighbors[:i], n.neighbors[i+1:]...)
		}
	}
	n.Unlock()

	return nil
}

func (n *networkNamespace) AddNeighbor(dstIP net.IP, dstMac net.HardwareAddr, options ...NeighOption) error {
	var (
		iface netlink.Link
		err   error
	)

	nh := n.findNeighbor(dstIP, dstMac)
	if nh != nil {
		// If it exists silently return
		return nil
	}

	nh = &neigh{
		dstIP:  dstIP,
		dstMac: dstMac,
	}

	nh.processNeighOptions(options...)

	if nh.linkName != "" {
		nh.linkDst = n.findDst(nh.linkName, false)
		if nh.linkDst == "" {
			return fmt.Errorf("could not find the interface with name %s", nh.linkName)
		}
	}

	n.Lock()
	nlh := n.nlHandle
	n.Unlock()

	if nh.linkDst != "" {
		iface, err = nlh.LinkByName(nh.linkDst)
		if err != nil {
			return fmt.Errorf("could not find interface with destination name %s: %v",
				nh.linkDst, err)
		}
	}

	nlnh := &netlink.Neigh{
		IP:           dstIP,
		HardwareAddr: dstMac,
		State:        netlink.NUD_PERMANENT,
		Family:       nh.family,
	}

	if nlnh.Family > 0 {
		nlnh.Flags = netlink.NTF_SELF
	}

	if nh.linkDst != "" {
		nlnh.LinkIndex = iface.Attrs().Index
	}

	if err := nlh.NeighSet(nlnh); err != nil {
		return fmt.Errorf("could not add neighbor entry: %v", err)
	}

	n.neighbors = append(n.neighbors, nh)

	return nil
}
<commit_msg>Fix bug in DeleteNeighbor<commit_after>package osl

import (
	"bytes"
	"fmt"
	"net"

	"github.com/vishvananda/netlink"
)

// NeighOption is a function option type to set interface options
type NeighOption func(nh *neigh)

type neigh struct {
	dstIP    net.IP
	dstMac   net.HardwareAddr
	linkName string
	linkDst  string
	family   int
}

func (n *networkNamespace) findNeighbor(dstIP net.IP, dstMac net.HardwareAddr) *neigh {
	n.Lock()
	defer n.Unlock()

	for _, nh := range n.neighbors {
		if nh.dstIP.Equal(dstIP) && bytes.Equal(nh.dstMac, dstMac) {
			return nh
		}
	}

	return nil
}

func (n *networkNamespace) DeleteNeighbor(dstIP net.IP, dstMac net.HardwareAddr, osDelete bool) error {
	var (
		iface netlink.Link
		err   error
	)

	nh := n.findNeighbor(dstIP, dstMac)
	if nh == nil {
		return fmt.Errorf("could not find the neighbor entry to delete")
	}

	if osDelete {
		n.Lock()
		nlh := n.nlHandle
		n.Unlock()

		if nh.linkDst != "" {
			iface, err = nlh.LinkByName(nh.linkDst)
			if err != nil {
				return fmt.Errorf("could not find interface with destination name %s: %v",
					nh.linkDst, err)
			}
		}

		nlnh := &netlink.Neigh{
			IP:     dstIP,
			State:  netlink.NUD_PERMANENT,
			Family: nh.family,
		}

		if nlnh.Family > 0 {
			nlnh.HardwareAddr = dstMac
			nlnh.Flags = netlink.NTF_SELF
		}

		if nh.linkDst != "" {
			nlnh.LinkIndex = iface.Attrs().Index
		}

		if err := nlh.NeighDel(nlnh); err != nil {
			return fmt.Errorf("could not delete neighbor entry: %v", err)
		}
	}

	n.Lock()
	for i, nh := range n.neighbors {
		if nh.dstIP.Equal(dstIP) && bytes.Equal(nh.dstMac, dstMac) {
			n.neighbors = append(n.neighbors[:i], n.neighbors[i+1:]...)
			break
		}
	}
	n.Unlock()

	return nil
}

func (n *networkNamespace) AddNeighbor(dstIP net.IP, dstMac net.HardwareAddr, options ...NeighOption) error {
	var (
		iface netlink.Link
		err   error
	)

	nh := n.findNeighbor(dstIP, dstMac)
	if nh != nil {
		// If it exists silently return
		return nil
	}

	nh = &neigh{
		dstIP:  dstIP,
		dstMac: dstMac,
	}

	nh.processNeighOptions(options...)

	if nh.linkName != "" {
		nh.linkDst = n.findDst(nh.linkName, false)
		if nh.linkDst == "" {
			return fmt.Errorf("could not find the interface with name %s", nh.linkName)
		}
	}

	n.Lock()
	nlh := n.nlHandle
	n.Unlock()

	if nh.linkDst != "" {
		iface, err = nlh.LinkByName(nh.linkDst)
		if err != nil {
			return fmt.Errorf("could not find interface with destination name %s: %v",
				nh.linkDst, err)
		}
	}

	nlnh := &netlink.Neigh{
		IP:           dstIP,
		HardwareAddr: dstMac,
		State:        netlink.NUD_PERMANENT,
		Family:       nh.family,
	}

	if nlnh.Family > 0 {
		nlnh.Flags = netlink.NTF_SELF
	}

	if nh.linkDst != "" {
		nlnh.LinkIndex = iface.Attrs().Index
	}

	if err := nlh.NeighSet(nlnh); err != nil {
		return fmt.Errorf("could not add neighbor entry: %v", err)
	}

	n.neighbors = append(n.neighbors, nh)

	return nil
}
<|endoftext|>


<commit_before>package main

import (
       "io"
       "flag"
       "log"
       "net/http"
       "strconv"
)

// Serve the HTML form to upload a file
func FormServer(w http.ResponseWriter, req *http.Request) {
     if "GET" != req.Method {
        log.Fatal("unsupported request method")
     }
     io.WriteString(w, "\n")
     io.WriteString(w, "<form action=\"http://localhost:8080/receive\" method=\"post\" enctype=\"multipart/form-data\">")
     io.WriteString(w, "filename:")
     io.WriteString(w, "<input type=\"file\" name=\"file\" id=\"upfile\">")
     io.WriteString(w, "<input type=\"submit\" name=\"submit\" value=\"submit\">")
     io.WriteString(w, "</form>\n")
}

func main() {
     var port = flag.Int("port", 8080, "port to listen on (default: 8080)")
     flag.Parse()
     log.Println("listening on port " + strconv.Itoa(*port))
     http.HandleFunc("/", FormServer)
	 err := http.ListenAndServe(":" + strconv.Itoa(*port), nil)
	 if err != nil {
        log.Fatal("server failed: ", err)
     }
}
<commit_msg>added upload handler<commit_after>package main

import (
	"flag"
	"io"
	"log"
	"net/http"
	"os"
	"strconv"
)

// Serve the HTML form to upload a file
func FormServer(w http.ResponseWriter, req *http.Request) {
	if "GET" != req.Method {
		log.Fatal("unsupported request method")
	}
	io.WriteString(w, "\n")
	io.WriteString(w, "<form action=\"http://localhost:8080/receive\" method=\"post\" enctype=\"multipart/form-data\">")
	io.WriteString(w, "filename:")
	io.WriteString(w, "<input type=\"file\" name=\"file\" id=\"file\">")
	io.WriteString(w, "<input type=\"submit\" name=\"submit\" value=\"submit\">")
	io.WriteString(w, "</form>\n")
}

// Handle the file upload
func uploadHandler(w http.ResponseWriter, req *http.Request) {
	if "POST" != req.Method {
		log.Fatal("unsupported request method for file upload")
	}

	// get file by ID from POST request
	file, header, err := req.FormFile("file")
	if err != nil {
		log.Fatal("error getting file from form data: ", err)
	}
	defer file.Close()

	// try to create file handler
	out, err := os.Create("/tmp/uploaded_file")
	if err != nil {
		log.Fatal("Could not create file. Maybe wrong privileges?")
	}
	defer out.Close()

	// write content of file to disk
	_, err = io.Copy(out, file)
	if err != nil {
		log.Fatal(err)
	}

	// indicate success
	io.WriteString(w, "sucessfully uploaded ")
	io.WriteString(w, header.Filename)
	io.WriteString(w, "")
}

func main() {
	var port = flag.Int("port", 8080, "port to listen on (default: 8080)")
	flag.Parse()
	log.Println("listening on port " + strconv.Itoa(*port))
	http.HandleFunc("/", FormServer)
	http.HandleFunc("/receive", uploadHandler)
	err := http.ListenAndServe(":"+strconv.Itoa(*port), nil)
	if err != nil {
		log.Fatal("server failed: ", err)
	}
}
<|endoftext|>


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

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

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

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

// kube2consul is a bridge between Kubernetes and Consul.  It watches the
// Kubernetes master for changes in Services and creates new DNS records on the
// consul agent.

package main // import "github.com/jmccarty3/kube2consul"

import (
	"flag"
	"fmt"
	"strings"
	"net/url"
	"os"
	"time"

	kapi "k8s.io/kubernetes/pkg/api"
	kclient "k8s.io/kubernetes/pkg/client/unversioned"
	kcache "k8s.io/kubernetes/pkg/client/unversioned/cache"
	kclientcmd "k8s.io/kubernetes/pkg/client/unversioned/clientcmd"
	kframework "k8s.io/kubernetes/pkg/controller/framework"
	kcontrollerFramework "k8s.io/kubernetes/pkg/controller/framework"
	kSelector "k8s.io/kubernetes/pkg/fields"
	"k8s.io/kubernetes/pkg/util"
	"github.com/golang/glog"
	consulapi "github.com/hashicorp/consul/api"

  "k8s.io/kubernetes/pkg/api"
  "k8s.io/kubernetes/pkg/fields"
  "k8s.io/kubernetes/pkg/labels"
)

var (
	argConsulAgent         = flag.String("consul-agent", "http://127.0.0.1:8500", "URL to consul agent")
	argKubecfgFile         = flag.String("kubecfg_file", "", "Location of kubecfg file for access to kubernetes service")
	argKubeMasterUrl       = flag.String("kube_master_url", "https://${KUBERNETES_SERVICE_HOST}:${KUBERNETES_SERVICE_PORT}", "Url to reach kubernetes master. Env variables in this flag will be expanded.")
)

const (
	// Maximum number of attempts to connect to consul server.
	maxConnectAttempts = 12
	// Resync period for the kube controller loop.
	resyncPeriod = 5 * time.Second
)

type nodeInformation struct {
	name string

	address string

	ready bool
	// map[service] DNS IDs
	ids map[string][]string
}

type kube2consul struct {
	// Consul client.
	consulClient *consulapi.Client

	// DNS domain name.
	domain string

	//Nodes Name / valid
	nodes map[string]nodeInformation

	//All Services.
	services map[string]*kapi.Service
}

func Newkube2consul() *kube2consul {
	var k kube2consul
	k.nodes = make(map[string]nodeInformation)
	k.services = make(map[string]*kapi.Service)

	return &k
}

func NewnodeInformation() *nodeInformation {
	var n nodeInformation
	n.ready = false
	n.ids = make(map[string][]string)

	return &n
}

func Contains(s []string, e string) bool {
	for _,i := range s {
		if i == e {
			return true
		}
	}
	return false
}

func (ks *kube2consul) removeDNS(recordID string) error {
	glog.Infof("Removing %s from DNS", recordID)
	ks.consulClient.Agent().ServiceDeregister(recordID)
	return nil
}

func (ks *kube2consul) createDNS(record string, service *kapi.Service, node *nodeInformation) error {
	if strings.Contains(record, ".") {
		glog.Infof("Service names containing '.' are not supported: %s\n", service.Name)
		return nil
	}

	// if ClusterIP is not set, do not create a DNS records
	if !kapi.IsServiceIPSet(service) {
		glog.Infof("Skipping dns record for headless service: %s\n", service.Name)
		return nil
	}

	for i := range service.Spec.Ports {
			newId := node.name+record + service.Spec.Ports[i].Name

			asr := &consulapi.AgentServiceRegistration{
				ID:			 newId,
				Name: 	 record + "-" + service.Spec.Ports[i].Name,
				Address: node.address,
				Port:    service.Spec.Ports[i].NodePort,
				Tags: []string{"Kube"},
			}

			if Contains(node.ids[record], newId) == false {
				glog.Infof("Setting DNS record: %v -> %v:%d\n", asr.Name, asr.Address, asr.Port)
				if err := ks.consulClient.Agent().ServiceRegister(asr); err != nil {
					return err
				}

				node.ids[record] = append(node.ids[record], newId)
		}
	}
	return nil
}

func newConsulClient(consulAgent string) (*consulapi.Client, error) {
	var (
		client *consulapi.Client
		err    error
	)

	consulConfig := consulapi.DefaultConfig()
	consulAgentUrl, err := url.Parse(consulAgent)
	if err != nil {
			glog.Infof("Error parsing Consul url")
			return nil, err
	}

	if consulAgentUrl.Host != "" {
	  consulConfig.Address = consulAgentUrl.Host
	}

	if consulAgentUrl.Scheme != "" {
		consulConfig.Scheme = consulAgentUrl.Scheme
	}

	client, err = consulapi.NewClient(consulConfig)
	if err != nil {
			glog.Infof("Error creating Consul client")
			return nil, err
	}

	for attempt := 1; attempt <= maxConnectAttempts; attempt++ {
		if _, err = client.Agent().Self(); err == nil {
			break
		}

		if attempt == maxConnectAttempts {
			break
		}

		glog.Infof("[Attempt: %d] Attempting access to Consul after 5 second sleep", attempt)
		time.Sleep(5 * time.Second)
	}

	if err != nil {
		return nil, fmt.Errorf("failed to connect to Consul agent: %v, error: %v", consulAgent, err)
	}
	glog.Infof("Consul agent found: %v", consulAgent)

	return client, nil
}

func getKubeMasterUrl() (string, error) {
	if *argKubeMasterUrl == "" {
		return "", fmt.Errorf("no --kube_master_url specified")
	}
	parsedUrl, err := url.Parse(os.ExpandEnv(*argKubeMasterUrl))
	if err != nil {
		return "", fmt.Errorf("failed to parse --kube_master_url %s - %v", *argKubeMasterUrl, err)
	}
	if parsedUrl.Scheme == "" || parsedUrl.Host == "" || parsedUrl.Host == ":" {
		return "", fmt.Errorf("invalid --kube_master_url specified %s", *argKubeMasterUrl)
	}
	return parsedUrl.String(), nil
}

// TODO: evaluate using pkg/client/clientcmd
func newKubeClient() (*kclient.Client, error) {
	var config *kclient.Config
	masterUrl, err := getKubeMasterUrl()
	if err != nil {
		return nil, err
	}
	if *argKubecfgFile == "" {
		config = &kclient.Config{
			Host:    masterUrl,
			Version: "v1",
		}
	} else {
		var err error
		overrides := &kclientcmd.ConfigOverrides{}
		overrides.ClusterInfo.Server = masterUrl                                     // might be "", but that is OK
		rules := &kclientcmd.ClientConfigLoadingRules{ExplicitPath: *argKubecfgFile} // might be "", but that is OK
		if config, err = kclientcmd.NewNonInteractiveDeferredLoadingClientConfig(rules, overrides).ClientConfig(); err != nil {
			return nil, err
		}
	}
	glog.Infof("Using %s for kubernetes master", config.Host)
	glog.Infof("Using kubernetes API %s", config.Version)
	return kclient.New(config)
}

func buildNameString(service, namespace string) string {
	//glog.Infof("Name String: %s  %s", service, namespace)
	//return fmt.Sprintf("%s.%s", service, namespace)
	return fmt.Sprintf("%s", service)
}

// Returns a cache.ListWatch that gets all changes to services.
func createServiceLW(kubeClient *kclient.Client) *kcache.ListWatch {
	return kcache.NewListWatchFromClient(kubeClient, "services", kapi.NamespaceAll, kSelector.Everything())
}

// Returns a cache.ListWatch that gets all changes to services.
func createNodeLW(kubeClient *kclient.Client) *kcache.ListWatch {
	return kcache.NewListWatchFromClient(kubeClient, "nodes", kapi.NamespaceAll, kSelector.Everything())
}

func (ks *kube2consul) newService(obj interface{}) {
	if s, ok := obj.(*kapi.Service); ok {
		name := buildNameString(s.Name, s.Namespace)
		ks.services[name] = s

		glog.V(2).Info("Creating Service: ", name)
		//Add to all existing nodes
		for _,node := range ks.nodes {
			if node.ready {
				ks.createDNS(name, s, &node)
			}
		}
	}
}

func (ks *kube2consul) removeService(obj interface{}) {
	if s, ok := obj.(*kapi.Service); ok {
		name := buildNameString(s.Name, s.Namespace)
		//Remove service from node
		for _,node := range ks.nodes {
			for _,id := range node.ids[name] {
				ks.removeDNS(id)
			}
			delete(node.ids, name)
		}
		delete(ks.services,name)
	}
}

func (ks *kube2consul) updateNode(oldObj, newObj interface{}) {
	if n, ok := newObj.(*kapi.Node); ok {
		ready := n.Status.Conditions[0].Status == kapi.ConditionTrue
		nodeInfo := ks.nodes[n.Name]

		if nodeInfo.ready != ready {
			nodeInfo.ready = ready
			if ready {
				for serviceName, service := range ks.services {
					ks.createDNS(serviceName, service, &nodeInfo)
				}
			}	else {
				for _,serviceIDs := range nodeInfo.ids {
					for _,serviceID := range serviceIDs {
						ks.removeDNS(serviceID)
					}
				}
				nodeInfo.ids = make(map[string][]string) //Clear tha map
			}
		}
	}
}

func (ks *kube2consul) newNode(newObj interface{}) {
	if node, ok := newObj.(*kapi.Node); ok {
		if _, ok := ks.nodes[node.Name]; !ok {
			glog.Info("Adding Node: ", node.Name)
			var newNode = *NewnodeInformation()
			newNode.name = node.Name
			newNode.address = node.Status.Addresses[0].Address

			ks.nodes[node.Name] = newNode
		}
	}
}

func (ks *kube2consul) removeNode(oldObj interface{}) {
	if node, ok := oldObj.(*kapi.Node); ok {
		if info, ok := ks.nodes[node.Name]; ok {
			glog.Info("Removing Node: ", node.Name)
			for _,idSet := range info.ids {
				for _,id := range idSet {
					ks.removeDNS(id)
				}
			}

			delete(ks.nodes, node.Name)
		}
	}
}

func watchForServices(kubeClient *kclient.Client, ks *kube2consul) {
	var serviceController *kcontrollerFramework.Controller
	_, serviceController = kframework.NewInformer(
		createServiceLW(kubeClient),
		&kapi.Service{},
		resyncPeriod,
		kframework.ResourceEventHandlerFuncs{
			AddFunc:    ks.newService,
			DeleteFunc: ks.removeService,
			UpdateFunc: func(oldObj, newObj interface{}) {
				ks.newService(newObj)
			},
		},
	)
	go serviceController.Run(util.NeverStop)
}

func watchForNodes(kubeClient *kclient.Client, ks *kube2consul) kcache.Store {
	store, serviceController := kframework.NewInformer(
		createNodeLW(kubeClient),
		&kapi.Node{},
		resyncPeriod,
		kframework.ResourceEventHandlerFuncs{
			AddFunc: ks.newNode,
			DeleteFunc: ks.removeNode,
			UpdateFunc: ks.updateNode,
		},
	)
	glog.Info("About to call run!")
	go serviceController.Run(util.NeverStop)
	return store
}

func main() {
	flag.Parse()
	var err error
	// TODO: Validate input flags.
	ks := Newkube2consul()

	if ks.consulClient, err = newConsulClient(*argConsulAgent); err != nil {
		glog.Fatalf("Failed to create Consul client - %v", err)
	}

	kubeClient, err := newKubeClient()
	if err != nil {
		glog.Fatalf("Failed to create a kubernetes client: %v", err)
	}

	glog.Info(kubeClient.ServerVersion())
	glog.Info(kubeClient.Services(kapi.NamespaceAll).Get("sensu-core"))

	pods, err := kubeClient.Pods(api.NamespaceDefault).List(labels.Everything(), fields.Everything())
	if err != nil {
	  for pod := range pods.Items {
			glog.Info(pod)
		}
	}

	watchForServices(kubeClient, ks)
	watchForNodes(kubeClient, ks)
	glog.Info("Watchers running")
	select {}
}
<commit_msg>Removing old service on service update is it has changed<commit_after>/*
Copyright 2014 The Kubernetes Authors All rights reserved.

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

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

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

// kube2consul is a bridge between Kubernetes and Consul.  It watches the
// Kubernetes master for changes in Services and creates new DNS records on the
// consul agent.

package main // import "github.com/jmccarty3/kube2consul"

import (
	"flag"
	"fmt"
	"strings"
	"net/url"
	"os"
	"time"

	kapi "k8s.io/kubernetes/pkg/api"
	kclient "k8s.io/kubernetes/pkg/client/unversioned"
	kcache "k8s.io/kubernetes/pkg/client/unversioned/cache"
	kclientcmd "k8s.io/kubernetes/pkg/client/unversioned/clientcmd"
	kframework "k8s.io/kubernetes/pkg/controller/framework"
	kcontrollerFramework "k8s.io/kubernetes/pkg/controller/framework"
	kSelector "k8s.io/kubernetes/pkg/fields"
	"k8s.io/kubernetes/pkg/util"
	"github.com/golang/glog"
	consulapi "github.com/hashicorp/consul/api"

  "k8s.io/kubernetes/pkg/api"
  "k8s.io/kubernetes/pkg/fields"
  "k8s.io/kubernetes/pkg/labels"
)

var (
	argConsulAgent         = flag.String("consul-agent", "http://127.0.0.1:8500", "URL to consul agent")
	argKubecfgFile         = flag.String("kubecfg_file", "", "Location of kubecfg file for access to kubernetes service")
	argKubeMasterUrl       = flag.String("kube_master_url", "https://${KUBERNETES_SERVICE_HOST}:${KUBERNETES_SERVICE_PORT}", "Url to reach kubernetes master. Env variables in this flag will be expanded.")
)

const (
	// Maximum number of attempts to connect to consul server.
	maxConnectAttempts = 12
	// Resync period for the kube controller loop.
	resyncPeriod = 5 * time.Second
)

type nodeInformation struct {
	name string

	address string

	ready bool
	// map[service] DNS IDs
	ids map[string][]string
}

type kube2consul struct {
	// Consul client.
	consulClient *consulapi.Client

	// DNS domain name.
	domain string

	//Nodes Name / valid
	nodes map[string]nodeInformation

	//All Services.
	services map[string]*kapi.Service
}

func Newkube2consul() *kube2consul {
	var k kube2consul
	k.nodes = make(map[string]nodeInformation)
	k.services = make(map[string]*kapi.Service)

	return &k
}

func NewnodeInformation() *nodeInformation {
	var n nodeInformation
	n.ready = false
	n.ids = make(map[string][]string)

	return &n
}

func Contains(s []string, e string) bool {
	for _,i := range s {
		if i == e {
			return true
		}
	}
	return false
}

func (ks *kube2consul) removeDNS(recordID string) error {
	glog.Infof("Removing %s from DNS", recordID)
	ks.consulClient.Agent().ServiceDeregister(recordID)
	return nil
}

func (ks *kube2consul) createDNS(record string, service *kapi.Service, node *nodeInformation) error {
	if strings.Contains(record, ".") {
		glog.Infof("Service names containing '.' are not supported: %s\n", service.Name)
		return nil
	}

	// if ClusterIP is not set, do not create a DNS records
	if !kapi.IsServiceIPSet(service) {
		glog.Infof("Skipping dns record for headless service: %s\n", service.Name)
		return nil
	}

	for i := range service.Spec.Ports {
			newId := node.name+record + service.Spec.Ports[i].Name

			asr := &consulapi.AgentServiceRegistration{
				ID:			 newId,
				Name: 	 record + "-" + service.Spec.Ports[i].Name,
				Address: node.address,
				Port:    service.Spec.Ports[i].NodePort,
				Tags: []string{"Kube"},
			}

			if Contains(node.ids[record], newId) == false {
				glog.Infof("Setting DNS record: %v -> %v:%d\n", asr.Name, asr.Address, asr.Port)
				if err := ks.consulClient.Agent().ServiceRegister(asr); err != nil {
					return err
				}

				node.ids[record] = append(node.ids[record], newId)
		}
	}
	return nil
}

func newConsulClient(consulAgent string) (*consulapi.Client, error) {
	var (
		client *consulapi.Client
		err    error
	)

	consulConfig := consulapi.DefaultConfig()
	consulAgentUrl, err := url.Parse(consulAgent)
	if err != nil {
			glog.Infof("Error parsing Consul url")
			return nil, err
	}

	if consulAgentUrl.Host != "" {
	  consulConfig.Address = consulAgentUrl.Host
	}

	if consulAgentUrl.Scheme != "" {
		consulConfig.Scheme = consulAgentUrl.Scheme
	}

	client, err = consulapi.NewClient(consulConfig)
	if err != nil {
			glog.Infof("Error creating Consul client")
			return nil, err
	}

	for attempt := 1; attempt <= maxConnectAttempts; attempt++ {
		if _, err = client.Agent().Self(); err == nil {
			break
		}

		if attempt == maxConnectAttempts {
			break
		}

		glog.Infof("[Attempt: %d] Attempting access to Consul after 5 second sleep", attempt)
		time.Sleep(5 * time.Second)
	}

	if err != nil {
		return nil, fmt.Errorf("failed to connect to Consul agent: %v, error: %v", consulAgent, err)
	}
	glog.Infof("Consul agent found: %v", consulAgent)

	return client, nil
}

func getKubeMasterUrl() (string, error) {
	if *argKubeMasterUrl == "" {
		return "", fmt.Errorf("no --kube_master_url specified")
	}
	parsedUrl, err := url.Parse(os.ExpandEnv(*argKubeMasterUrl))
	if err != nil {
		return "", fmt.Errorf("failed to parse --kube_master_url %s - %v", *argKubeMasterUrl, err)
	}
	if parsedUrl.Scheme == "" || parsedUrl.Host == "" || parsedUrl.Host == ":" {
		return "", fmt.Errorf("invalid --kube_master_url specified %s", *argKubeMasterUrl)
	}
	return parsedUrl.String(), nil
}

// TODO: evaluate using pkg/client/clientcmd
func newKubeClient() (*kclient.Client, error) {
	var config *kclient.Config
	masterUrl, err := getKubeMasterUrl()
	if err != nil {
		return nil, err
	}
	if *argKubecfgFile == "" {
		config = &kclient.Config{
			Host:    masterUrl,
			Version: "v1",
		}
	} else {
		var err error
		overrides := &kclientcmd.ConfigOverrides{}
		overrides.ClusterInfo.Server = masterUrl                                     // might be "", but that is OK
		rules := &kclientcmd.ClientConfigLoadingRules{ExplicitPath: *argKubecfgFile} // might be "", but that is OK
		if config, err = kclientcmd.NewNonInteractiveDeferredLoadingClientConfig(rules, overrides).ClientConfig(); err != nil {
			return nil, err
		}
	}
	glog.Infof("Using %s for kubernetes master", config.Host)
	glog.Infof("Using kubernetes API %s", config.Version)
	return kclient.New(config)
}

func buildNameString(service, namespace string) string {
	//glog.Infof("Name String: %s  %s", service, namespace)
	//return fmt.Sprintf("%s.%s", service, namespace)
	return fmt.Sprintf("%s", service)
}

// Returns a cache.ListWatch that gets all changes to services.
func createServiceLW(kubeClient *kclient.Client) *kcache.ListWatch {
	return kcache.NewListWatchFromClient(kubeClient, "services", kapi.NamespaceAll, kSelector.Everything())
}

// Returns a cache.ListWatch that gets all changes to services.
func createNodeLW(kubeClient *kclient.Client) *kcache.ListWatch {
	return kcache.NewListWatchFromClient(kubeClient, "nodes", kapi.NamespaceAll, kSelector.Everything())
}

func (ks *kube2consul) newService(obj interface{}) {
	if s, ok := obj.(*kapi.Service); ok {
		name := buildNameString(s.Name, s.Namespace)
		ks.services[name] = s

		glog.V(2).Info("Creating Service: ", name)
		//Add to all existing nodes
		for _,node := range ks.nodes {
			if node.ready {
				ks.createDNS(name, s, &node)
			}
		}
	}
}

func (ks *kube2consul) removeService(obj interface{}) {
	if s, ok := obj.(*kapi.Service); ok {
		name := buildNameString(s.Name, s.Namespace)
		//Remove service from node
		for _,node := range ks.nodes {
			for _,id := range node.ids[name] {
				ks.removeDNS(id)
			}
			delete(node.ids, name)
		}
		delete(ks.services,name)
	}
}

func (ks *kube2consul) updateNode(oldObj, newObj interface{}) {
	if n, ok := newObj.(*kapi.Node); ok {
		ready := n.Status.Conditions[0].Status == kapi.ConditionTrue
		nodeInfo := ks.nodes[n.Name]

		if nodeInfo.ready != ready {
			nodeInfo.ready = ready
			if ready {
				for serviceName, service := range ks.services {
					ks.createDNS(serviceName, service, &nodeInfo)
				}
			}	else {
				for _,serviceIDs := range nodeInfo.ids {
					for _,serviceID := range serviceIDs {
						ks.removeDNS(serviceID)
					}
				}
				nodeInfo.ids = make(map[string][]string) //Clear tha map
			}
		}
	}
}

func (ks *kube2consul) newNode(newObj interface{}) {
	if node, ok := newObj.(*kapi.Node); ok {
		if _, ok := ks.nodes[node.Name]; !ok {
			glog.Info("Adding Node: ", node.Name)
			var newNode = *NewnodeInformation()
			newNode.name = node.Name
			newNode.address = node.Status.Addresses[0].Address

			ks.nodes[node.Name] = newNode
		}
	}
}

func (ks *kube2consul) removeNode(oldObj interface{}) {
	if node, ok := oldObj.(*kapi.Node); ok {
		if info, ok := ks.nodes[node.Name]; ok {
			glog.Info("Removing Node: ", node.Name)
			for _,idSet := range info.ids {
				for _,id := range idSet {
					ks.removeDNS(id)
				}
			}

			delete(ks.nodes, node.Name)
		}
	}
}

func watchForServices(kubeClient *kclient.Client, ks *kube2consul) {
	var serviceController *kcontrollerFramework.Controller
	_, serviceController = kframework.NewInformer(
		createServiceLW(kubeClient),
		&kapi.Service{},
		resyncPeriod,
		kframework.ResourceEventHandlerFuncs{
			AddFunc:    ks.newService,
			DeleteFunc: ks.removeService,
			UpdateFunc: func(oldObj, newObj interface{}) {
				if oldObj != newObj {
					ks.removeService(oldObj)
					ks.newService(newObj)
				}
			},
		},
	)
	go serviceController.Run(util.NeverStop)
}

func watchForNodes(kubeClient *kclient.Client, ks *kube2consul) kcache.Store {
	store, serviceController := kframework.NewInformer(
		createNodeLW(kubeClient),
		&kapi.Node{},
		resyncPeriod,
		kframework.ResourceEventHandlerFuncs{
			AddFunc: ks.newNode,
			DeleteFunc: ks.removeNode,
			UpdateFunc: ks.updateNode,
		},
	)
	glog.Info("About to call run!")
	go serviceController.Run(util.NeverStop)
	return store
}

func main() {
	flag.Parse()
	var err error
	// TODO: Validate input flags.
	ks := Newkube2consul()

	if ks.consulClient, err = newConsulClient(*argConsulAgent); err != nil {
		glog.Fatalf("Failed to create Consul client - %v", err)
	}

	kubeClient, err := newKubeClient()
	if err != nil {
		glog.Fatalf("Failed to create a kubernetes client: %v", err)
	}

	glog.Info(kubeClient.ServerVersion())
	glog.Info(kubeClient.Services(kapi.NamespaceAll).Get("sensu-core"))

	pods, err := kubeClient.Pods(api.NamespaceDefault).List(labels.Everything(), fields.Everything())
	if err != nil {
	  for pod := range pods.Items {
			glog.Info(pod)
		}
	}

	watchForServices(kubeClient, ks)
	watchForNodes(kubeClient, ks)
	glog.Info("Watchers running")
	select {}
}
<|endoftext|>


<commit_before>package fizz

type Table struct {
	Name    string `db:"name"`
	Columns []Column
	Indexes []Index
}

func (t *Table) Column(name string, colType string, options map[string]interface{}) {
	var primary bool
	if _, ok := options["primary"]; ok {
		primary = true
	}
	c := Column{
		Name:    name,
		ColType: colType,
		Options: options,
		Primary: primary,
	}
	t.Columns = append(t.Columns, c)
}

func (t *Table) ColumnNames() []string {
	cols := make([]string, len(t.Columns))
	for i, c := range t.Columns {
		cols[i] = c.Name
	}
	return cols
}

func (t *Table) HasColumns(args ...string) bool {
	keys := map[string]struct{}{}
	for _, k := range t.ColumnNames() {
		keys[k] = struct{}{}
	}
	for _, a := range args {
		if _, ok := keys[a]; !ok {
			return false
		}
	}
	return true
}

func (f fizzer) CreateTable() interface{} {
	return func(name string, fn func(t *Table), options map[string]interface{}) {
		t := Table{
			Name:    name,
			Columns: []Column{},
		}
		fn(&t)
		var foundPrimary bool
		for _, c := range t.Columns {
			if c.Primary {
				foundPrimary = true
				break
			}
		}

		if !foundPrimary {
			t.Columns = append([]Column{INT_ID_COL}, t.Columns...)
		}

		if value, exists := options["timestamps"]; !exists || true == value {
			t.Columns = append(t.Columns, CREATED_COL, UPDATED_COL)
		}

		f.add(f.Bubbler.CreateTable(t))
	}
}

func (f fizzer) DropTable() interface{} {
	return func(name string) {
		f.add(f.Bubbler.DropTable(Table{Name: name}))
	}
}

func (f fizzer) RenameTable() interface{} {
	return func(old, new string) {
		f.add(f.Bubbler.RenameTable([]Table{
			{Name: old},
			{Name: new},
		}))
	}
}
<commit_msg>add useful Timestampt(s) methods<commit_after>package fizz

type Table struct {
	Name    string `db:"name"`
	Columns []Column
	Indexes []Index
}

func (t *Table) Column(name string, colType string, options map[string]interface{}) {
	var primary bool
	if _, ok := options["primary"]; ok {
		primary = true
	}
	c := Column{
		Name:    name,
		ColType: colType,
		Options: options,
		Primary: primary,
	}
	t.Columns = append(t.Columns, c)
}

func (t *Table) Timestamp(name string) {
	c := Column{
		Name:    name,
		ColType: "timestamp",
		Options: Options{},
	}

	t.Columns = append(t.Columns, c)
}

func (t *Table) Timestamps() {
	t.Columns = append(t.Columns, []Column{CREATED_COL, UPDATED_COL}...)
}

func (t *Table) ColumnNames() []string {
	cols := make([]string, len(t.Columns))
	for i, c := range t.Columns {
		cols[i] = c.Name
	}
	return cols
}

func (t *Table) HasColumns(args ...string) bool {
	keys := map[string]struct{}{}
	for _, k := range t.ColumnNames() {
		keys[k] = struct{}{}
	}
	for _, a := range args {
		if _, ok := keys[a]; !ok {
			return false
		}
	}
	return true
}

func (f fizzer) CreateTable() interface{} {
	return func(name string, fn func(t *Table), options map[string]interface{}) {
		t := Table{
			Name:    name,
			Columns: []Column{},
		}
		fn(&t)
		var foundPrimary bool
		for _, c := range t.Columns {
			if c.Primary {
				foundPrimary = true
				break
			}
		}

		if !foundPrimary {
			t.Columns = append([]Column{INT_ID_COL}, t.Columns...)
		}

		if value, exists := options["timestamps"]; !exists || true == value {
			t.Columns = append(t.Columns, CREATED_COL, UPDATED_COL)
		}

		f.add(f.Bubbler.CreateTable(t))
	}
}

func (f fizzer) DropTable() interface{} {
	return func(name string) {
		f.add(f.Bubbler.DropTable(Table{Name: name}))
	}
}

func (f fizzer) RenameTable() interface{} {
	return func(old, new string) {
		f.add(f.Bubbler.RenameTable([]Table{
			{Name: old},
			{Name: new},
		}))
	}
}
<|endoftext|>


<commit_before>package flags

import (
	"flag"
	"strings"
	"time"
)

var (
	stringPtrs   = map[string]*string{}
	boolPtrs     = map[string]*bool{}
	durationPtrs = map[string]*time.Duration{}
	float64Ptrs  = map[string]*float64{}
	intPtrs      = map[string]*int{}
	slicePtrs    = map[string]*[]string{}
)

type sliceContainer struct {
	S *[]string
}

func (s *sliceContainer) String() string {
	if s == nil || s.S == nil {
		return ""
	}
	return strings.Join(*s.S, ",")
}

func (s *sliceContainer) Set(value string) error {
	*s.S = strings.Split(value, ",")
	return nil
}

func String(name string, value string, usage string) *string {
	if _, exists := stringPtrs[name]; !exists {
		stringPtrs[name] = flag.String(name, value, usage)
	}

	return stringPtrs[name]
}

func Int(name string, value int, usage string) *int {
	if _, exists := intPtrs[name]; !exists {
		intPtrs[name] = flag.Int(name, value, usage)
	}

	return intPtrs[name]
}

func Bool(name string, value bool, usage string) *bool {
	if _, exists := boolPtrs[name]; !exists {
		boolPtrs[name] = flag.Bool(name, value, usage)
	}

	return boolPtrs[name]
}

func Float64(name string, value float64, usage string) *float64 {
	if _, exists := float64Ptrs[name]; !exists {
		float64Ptrs[name] = flag.Float64(name, value, usage)
	}

	return float64Ptrs[name]
}

func Slice(name string, value []string, usage string) *[]string {
	if _, exists := slicePtrs[name]; !exists {
		var s []string
		if value == nil {
			s = []string{}
		} else {
			s = value
		}
		slicePtrs[name] = &s

		container := sliceContainer{S: &s}
		flag.Var(&container, name, usage)
	}
	return slicePtrs[name]
}

func Duration(name string, value time.Duration, usage string) *time.Duration {
	if _, exists := float64Ptrs[name]; !exists {
		durationPtrs[name] = flag.Duration(name, value, usage)
	}

	return durationPtrs[name]
}
<commit_msg>[Flags] Add helper function ValidateNonZero to reduce boilerplate codes to check flags.<commit_after>package flags

import (
	"flag"
	"log"
	"strings"
	"time"
)

var (
	stringPtrs   = map[string]*string{}
	boolPtrs     = map[string]*bool{}
	durationPtrs = map[string]*time.Duration{}
	float64Ptrs  = map[string]*float64{}
	intPtrs      = map[string]*int{}
	slicePtrs    = map[string]*[]string{}
)

func ValidateNonZero(names ...string) {
	for _, name := range names {
		// NOTE: Bool flags not supported.
		if stringPtrs[name] != nil && *stringPtrs[name] == "" {
			log.Fatalf("Missing --%s", name)
		}
		if durationPtrs[name] != nil && int64(*durationPtrs[name]) == 0 {
			log.Fatalf("Missing --%s", name)
		}
		if float64Ptrs[name] != nil && *float64Ptrs[name] == 0 {
			log.Fatalf("Missing --%s", name)
		}
		if intPtrs[name] != nil && *intPtrs[name] == 0 {
			log.Fatalf("Missing --%s", name)
		}
		if slicePtrs[name] != nil && len(*slicePtrs[name]) == 0 {
			log.Fatalf("Missing --%s", name)
		}
	}
}

type sliceContainer struct {
	S *[]string
}

func (s *sliceContainer) String() string {
	if s == nil || s.S == nil {
		return ""
	}
	return strings.Join(*s.S, ",")
}

func (s *sliceContainer) Set(value string) error {
	*s.S = strings.Split(value, ",")
	return nil
}

func String(name string, value string, usage string) *string {
	if _, exists := stringPtrs[name]; !exists {
		stringPtrs[name] = flag.String(name, value, usage)
	}

	return stringPtrs[name]
}

func Int(name string, value int, usage string) *int {
	if _, exists := intPtrs[name]; !exists {
		intPtrs[name] = flag.Int(name, value, usage)
	}

	return intPtrs[name]
}

func Bool(name string, value bool, usage string) *bool {
	if _, exists := boolPtrs[name]; !exists {
		boolPtrs[name] = flag.Bool(name, value, usage)
	}

	return boolPtrs[name]
}

func Float64(name string, value float64, usage string) *float64 {
	if _, exists := float64Ptrs[name]; !exists {
		float64Ptrs[name] = flag.Float64(name, value, usage)
	}

	return float64Ptrs[name]
}

func Slice(name string, value []string, usage string) *[]string {
	if _, exists := slicePtrs[name]; !exists {
		var s []string
		if value == nil {
			s = []string{}
		} else {
			s = value
		}
		slicePtrs[name] = &s

		container := sliceContainer{S: &s}
		flag.Var(&container, name, usage)
	}
	return slicePtrs[name]
}

func Duration(name string, value time.Duration, usage string) *time.Duration {
	if _, exists := float64Ptrs[name]; !exists {
		durationPtrs[name] = flag.Duration(name, value, usage)
	}

	return durationPtrs[name]
}
<|endoftext|>


<commit_before>/*
 *   Copyright 2016 Rémy MATHIEU
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package content

import (
	"fmt"
	"strings"
	"github.com/PuerkitoBio/goquery"
)

type RedditContent struct {
	Url string
}

func (reddit RedditContent) callAPI() ([]Content, error) {
	doc, err := goquery.NewDocument(reddit.Url)
	if err != nil {
		fmt.Println("Error while calling API")
		return nil, err
	}

	rv := make([]Content, 0)

	doc.Find("div.search-result,div.entry").Each(func(i int, selec *goquery.Selection) {
		// ignore sticky posts
		if selec.HasClass("stickied") {
			return
		}

		if len(rv) > 20 {
			return
		}

		t := selec.Find("a.search-title,a.title")
		title := t.Text()

		l := selec.Find("a.search-link,a.link")
		externalLink, _ := l.Attr("href")

		// self posts
		if strings.HasPrefix(externalLink, "/r/") {
 			externalLink = "https://reddit.com" + externalLink
 		}

		rv = append(rv, Content{
			Text: title,
			Url:  externalLink,
		})
	})

	return rv, nil
}
<commit_msg>Reddit content provider impl fix<commit_after>/*
 *   Copyright 2016 Rémy MATHIEU
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package content

import (
	"fmt"
	"strings"
	"github.com/PuerkitoBio/goquery"
)

type RedditContent struct {
	Url string
}

func (reddit RedditContent) callAPI() ([]Content, error) {
	doc, err := goquery.NewDocument(reddit.Url)
	if err != nil {
		fmt.Println("Error while calling API")
		return nil, err
	}

	rv := make([]Content, 0)

	doc.Find("div.search-result,div.entry").Each(func(i int, selec *goquery.Selection) {
		// ignore sticky posts
		if selec.HasClass("stickied") {
			return
		}

		if len(rv) > 20 {
			return
		}

		t := selec.Find("a.search-title,a.title")
		title := t.First().Text()

		l := selec.Find("a.search-link,a.link")
		externalLink, _ := l.First().Attr("href")

		// self posts
		if strings.HasPrefix(externalLink, "/r/") {
 			externalLink = "https://reddit.com" + externalLink
 		}

		rv = append(rv, Content{
			Text: title,
			Url:  externalLink,
		})
	})

	return rv, nil
}
<|endoftext|>


<commit_before><commit_msg>support http code 422 UNPROCESSABLE_ENTITY<commit_after><|endoftext|>


<commit_before>// +build darwin freebsd linux netbsd openbsd

package godirwalk

// On unix we don't need to fixup pathnames with symlinks when doing a Stat().
func evalSymlinksHelper(pathname string) (string, error) {
	return pathname, nil
}
<commit_msg>follow_unix.go builds on everything except Windows<commit_after>// +build !windows

package godirwalk

// On unix we don't need to fixup pathnames with symlinks when doing a Stat().
func evalSymlinksHelper(pathname string) (string, error) {
	return pathname, nil
}
<|endoftext|>


<commit_before>package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"os"
	"path"
	"runtime/pprof"
	"sort"
	"text/tabwriter"
)

const VERSION = `0.1.2`

var languages = []Language{
	Language{"Thrift", mExt(".thrift"), cComments},

	Language{"C", mExt(".c", ".h"), cComments},
	Language{"C++", mExt(".cc", ".cpp", ".cxx", ".hh", ".hpp", ".hxx"), cComments},
	Language{"Go", mExt(".go"), cComments},
	Language{"Scala", mExt(".scala"), cComments},
	Language{"Java", mExt(".java"), cComments},

	Language{"YACC", mExt(".y"), cComments},
	Language{"Lex", mExt(".l"), cComments},

	Language{"SQL", mExt(".sql"), sqlComments},

	Language{"Haskell", mExt(".hs", ".lhs"), hsComments},

	Language{"Perl", mExt(".pl", ".pm"), perlComments},
	Language{"PHP", mExt(".php"), cComments},

	Language{"Shell", mExt(".sh"), shComments},
	Language{"Bash", mExt(".bash"), shComments},

	Language{"Ruby", mExt(".rb"), shComments},
	Language{"Python", mExt(".py"), pyComments},
	Language{"Assembly", mExt(".asm", ".s"), semiComments},
	Language{"Lisp", mExt(".lsp", ".lisp"), semiComments},
	Language{"Scheme", mExt(".scm", ".scheme"), semiComments},

	Language{"Make", mName("makefile", "Makefile", "MAKEFILE"), shComments},
	Language{"CMake", mName("CMakeLists.txt"), shComments},
	Language{"Jam", mName("Jamfile", "Jamrules"), shComments},

	Language{"Markdown", mExt(".md"), noComments},

	Language{"HAML", mExt(".haml"), noComments},
	Language{"SASS", mExt(".sass"), cssComments},
	Language{"SCSS", mExt(".scss"), cssComments},

	Language{"HTML", mExt(".htm", ".html", ".xhtml"), xmlComments},
	Language{"XML", mExt(".xml"), xmlComments},
	Language{"CSS", mExt(".css"), cssComments},
	Language{"JavaScript", mExt(".js"), cComments},
}

type Commenter struct {
	LineComment  string
	StartComment string
	EndComment   string
	Nesting      bool
}

var (
	noComments   = Commenter{"\000", "\000", "\000", false}
	xmlComments  = Commenter{"\000", `<!--`, `-->`, false}
	cComments    = Commenter{`//`, `/*`, `*/`, false}
	cssComments  = Commenter{"\000", `/*`, `*/`, false}
	shComments   = Commenter{`#`, "\000", "\000", false}
	semiComments = Commenter{`;`, "\000", "\000", false}
	hsComments   = Commenter{`--`, `{-`, `-}`, true}
	sqlComments  = Commenter{`--`, "\000", "\000", false}
	pyComments   = Commenter{`#`, `"""`, `"""`, false}

	// TODO support POD and __END__
	perlComments = Commenter{`#`, "\000", "\000", false}
)

type Language struct {
	Namer
	Matcher
	Commenter
}

// TODO work properly with unicode
func (l Language) Update(c []byte, s *Stats) {
	s.FileCount++

	inComment := 0 // this is an int for nesting
	inLComment := false
	blank := true
	lc := []byte(l.LineComment)
	sc := []byte(l.StartComment)
	ec := []byte(l.EndComment)
	lp, sp, ep := 0, 0, 0

	for _, b := range c {
		if inComment == 0 && b == lc[lp] {
			lp++
			if lp == len(lc) {
				inLComment = true
				lp = 0
			}
		} else {
			lp = 0
		}
		if !inLComment && b == sc[sp] {
			sp++
			if sp == len(sc) {
				inComment++
				if inComment > 1 && !l.Nesting {
					inComment = 1
				}
				sp = 0
			}
		} else {
			sp = 0
		}
		if !inLComment && inComment > 0 && b == ec[ep] {
			ep++
			if ep == len(ec) {
				if inComment > 0 {
					inComment--
				}
				ep = 0
			}
		} else {
			ep = 0
		}

		if b != byte(' ') && b != byte('\t') && b != byte('\n') {
			blank = false
		}

		// BUG(srl): lines with comment don't count towards code
		// Note that lines with both code and comment count towards
		// each, but are not counted twice in the total.
		if b == byte('\n') {
			s.TotalLines++
			if inComment > 0 || inLComment {
				inLComment = false
				s.CommentLines++
			} else if blank {
				s.BlankLines++
			} else {
				s.CodeLines++
			}
			blank = true
			continue
		}
	}
}

type Namer string

func (l Namer) Name() string { return string(l) }

type Matcher func(string) bool

func (m Matcher) Match(fname string) bool { return m(fname) }

func mExt(exts ...string) Matcher {
	return func(fname string) bool {
		for _, ext := range exts {
			if ext == path.Ext(fname) {
				return true
			}
		}
		return false
	}
}

func mName(names ...string) Matcher {
	return func(fname string) bool {
		for _, name := range names {
			if name == path.Base(fname) {
				return true
			}
		}
		return false
	}
}

type Stats struct {
	FileCount    int
	TotalLines   int
	CodeLines    int
	BlankLines   int
	CommentLines int
}

var info = map[string]*Stats{}

func handleFileLang(fname string, l Language) {
	i, ok := info[l.Name()]
	if !ok {
		i = &Stats{}
		info[l.Name()] = i
	}
	c, err := ioutil.ReadFile(fname)
	if err != nil {
		fmt.Fprintf(os.Stderr, "  ! %s\n", fname)
		return
	}
	l.Update(c, i)
}

func handleFile(fname string) {
	for _, lang := range languages {
		if lang.Match(fname) {
			handleFileLang(fname, lang)
			return
		}
	}
	// TODO No recognized extension - check for hashbang
}

var files []string

func add(n string) {
	fi, err := os.Stat(n)
	if err != nil {
		goto invalid
	}
	if fi.IsDir() {
		fs, err := ioutil.ReadDir(n)
		if err != nil {
			goto invalid
		}
		for _, f := range fs {
			if f.Name()[0] != '.' {
				add(path.Join(n, f.Name()))
			}
		}
		return
	}
	if fi.Mode()&os.ModeType == 0 {
		files = append(files, n)
		return
	}

	println(fi.Mode())

invalid:
	fmt.Fprintf(os.Stderr, "  ! %s\n", n)
}

type LData []LResult

func (d LData) Len() int { return len(d) }

func (d LData) Less(i, j int) bool {
	if d[i].CodeLines == d[j].CodeLines {
		return d[i].Name > d[j].Name
	}
	return d[i].CodeLines > d[j].CodeLines
}

func (d LData) Swap(i, j int) {
	d[i], d[j] = d[j], d[i]
}

type LResult struct {
	Name         string
	FileCount    int
	CodeLines    int
	CommentLines int
	BlankLines   int
	TotalLines   int
}

func (r *LResult) Add(a LResult) {
	r.FileCount += a.FileCount
	r.CodeLines += a.CodeLines
	r.CommentLines += a.CommentLines
	r.BlankLines += a.BlankLines
	r.TotalLines += a.TotalLines
}

func printJSON() {
	bs, err := json.MarshalIndent(info, "", "  ")
	if err != nil {
		panic(err)
	}
	fmt.Println(string(bs))
}

func printInfo() {
	w := tabwriter.NewWriter(os.Stdout, 2, 8, 2, ' ', tabwriter.AlignRight)
	fmt.Fprintln(w, "Language\tFiles\tCode\tComment\tBlank\tTotal\t")
	d := LData([]LResult{})
	total := &LResult{}
	total.Name = "Total"
	for n, i := range info {
		r := LResult{n, i.FileCount, i.CodeLines, i.CommentLines, i.BlankLines, i.TotalLines}
		d = append(d, r)
		total.Add(r)
	}
	d = append(d, *total)
	sort.Sort(d)
	for _, i := range d {
		fmt.Fprintf(w, "%s\t%d\t%d\t%d\t%d\t%d\t\n", i.Name, i.FileCount, i.CodeLines, i.CommentLines, i.BlankLines, i.TotalLines)
	}

	w.Flush()
}

var (
	cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
	useJson    = flag.Bool("json", false, "JSON-format output")
	version    = flag.Bool("V", false, "display version info and exit")
)

func main() {
	flag.Parse()
	if *version {
		fmt.Printf("sloc %s\n", VERSION)
		return
	}
	if *cpuprofile != "" {
		f, err := os.Create(*cpuprofile)
		if err != nil {
			fmt.Fprintf(os.Stderr, "error: %s\n", err.Error())
			return
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	args := flag.Args()
	if len(args) == 0 {
		args = append(args, `.`)
	}

	for _, n := range args {
		add(n)
	}

	for _, f := range files {
		handleFile(f)
	}

	if *useJson {
		printJSON()
	} else {
		printInfo()
	}
}
<commit_msg>Updating version<commit_after>package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"os"
	"path"
	"runtime/pprof"
	"sort"
	"text/tabwriter"
)

const VERSION = `0.2.0`

var languages = []Language{
	Language{"Thrift", mExt(".thrift"), cComments},

	Language{"C", mExt(".c", ".h"), cComments},
	Language{"C++", mExt(".cc", ".cpp", ".cxx", ".hh", ".hpp", ".hxx"), cComments},
	Language{"Go", mExt(".go"), cComments},
	Language{"Scala", mExt(".scala"), cComments},
	Language{"Java", mExt(".java"), cComments},

	Language{"YACC", mExt(".y"), cComments},
	Language{"Lex", mExt(".l"), cComments},

	Language{"SQL", mExt(".sql"), sqlComments},

	Language{"Haskell", mExt(".hs", ".lhs"), hsComments},

	Language{"Perl", mExt(".pl", ".pm"), perlComments},
	Language{"PHP", mExt(".php"), cComments},

	Language{"Shell", mExt(".sh"), shComments},
	Language{"Bash", mExt(".bash"), shComments},

	Language{"Ruby", mExt(".rb"), shComments},
	Language{"Python", mExt(".py"), pyComments},
	Language{"Assembly", mExt(".asm", ".s"), semiComments},
	Language{"Lisp", mExt(".lsp", ".lisp"), semiComments},
	Language{"Scheme", mExt(".scm", ".scheme"), semiComments},

	Language{"Make", mName("makefile", "Makefile", "MAKEFILE"), shComments},
	Language{"CMake", mName("CMakeLists.txt"), shComments},
	Language{"Jam", mName("Jamfile", "Jamrules"), shComments},

	Language{"Markdown", mExt(".md"), noComments},

	Language{"HAML", mExt(".haml"), noComments},
	Language{"SASS", mExt(".sass"), cssComments},
	Language{"SCSS", mExt(".scss"), cssComments},

	Language{"HTML", mExt(".htm", ".html", ".xhtml"), xmlComments},
	Language{"XML", mExt(".xml"), xmlComments},
	Language{"CSS", mExt(".css"), cssComments},
	Language{"JavaScript", mExt(".js"), cComments},
}

type Commenter struct {
	LineComment  string
	StartComment string
	EndComment   string
	Nesting      bool
}

var (
	noComments   = Commenter{"\000", "\000", "\000", false}
	xmlComments  = Commenter{"\000", `<!--`, `-->`, false}
	cComments    = Commenter{`//`, `/*`, `*/`, false}
	cssComments  = Commenter{"\000", `/*`, `*/`, false}
	shComments   = Commenter{`#`, "\000", "\000", false}
	semiComments = Commenter{`;`, "\000", "\000", false}
	hsComments   = Commenter{`--`, `{-`, `-}`, true}
	sqlComments  = Commenter{`--`, "\000", "\000", false}
	pyComments   = Commenter{`#`, `"""`, `"""`, false}

	// TODO support POD and __END__
	perlComments = Commenter{`#`, "\000", "\000", false}
)

type Language struct {
	Namer
	Matcher
	Commenter
}

// TODO work properly with unicode
func (l Language) Update(c []byte, s *Stats) {
	s.FileCount++

	inComment := 0 // this is an int for nesting
	inLComment := false
	blank := true
	lc := []byte(l.LineComment)
	sc := []byte(l.StartComment)
	ec := []byte(l.EndComment)
	lp, sp, ep := 0, 0, 0

	for _, b := range c {
		if inComment == 0 && b == lc[lp] {
			lp++
			if lp == len(lc) {
				inLComment = true
				lp = 0
			}
		} else {
			lp = 0
		}
		if !inLComment && b == sc[sp] {
			sp++
			if sp == len(sc) {
				inComment++
				if inComment > 1 && !l.Nesting {
					inComment = 1
				}
				sp = 0
			}
		} else {
			sp = 0
		}
		if !inLComment && inComment > 0 && b == ec[ep] {
			ep++
			if ep == len(ec) {
				if inComment > 0 {
					inComment--
				}
				ep = 0
			}
		} else {
			ep = 0
		}

		if b != byte(' ') && b != byte('\t') && b != byte('\n') {
			blank = false
		}

		// BUG(srl): lines with comment don't count towards code
		// Note that lines with both code and comment count towards
		// each, but are not counted twice in the total.
		if b == byte('\n') {
			s.TotalLines++
			if inComment > 0 || inLComment {
				inLComment = false
				s.CommentLines++
			} else if blank {
				s.BlankLines++
			} else {
				s.CodeLines++
			}
			blank = true
			continue
		}
	}
}

type Namer string

func (l Namer) Name() string { return string(l) }

type Matcher func(string) bool

func (m Matcher) Match(fname string) bool { return m(fname) }

func mExt(exts ...string) Matcher {
	return func(fname string) bool {
		for _, ext := range exts {
			if ext == path.Ext(fname) {
				return true
			}
		}
		return false
	}
}

func mName(names ...string) Matcher {
	return func(fname string) bool {
		for _, name := range names {
			if name == path.Base(fname) {
				return true
			}
		}
		return false
	}
}

type Stats struct {
	FileCount    int
	TotalLines   int
	CodeLines    int
	BlankLines   int
	CommentLines int
}

var info = map[string]*Stats{}

func handleFileLang(fname string, l Language) {
	i, ok := info[l.Name()]
	if !ok {
		i = &Stats{}
		info[l.Name()] = i
	}
	c, err := ioutil.ReadFile(fname)
	if err != nil {
		fmt.Fprintf(os.Stderr, "  ! %s\n", fname)
		return
	}
	l.Update(c, i)
}

func handleFile(fname string) {
	for _, lang := range languages {
		if lang.Match(fname) {
			handleFileLang(fname, lang)
			return
		}
	}
	// TODO No recognized extension - check for hashbang
}

var files []string

func add(n string) {
	fi, err := os.Stat(n)
	if err != nil {
		goto invalid
	}
	if fi.IsDir() {
		fs, err := ioutil.ReadDir(n)
		if err != nil {
			goto invalid
		}
		for _, f := range fs {
			if f.Name()[0] != '.' {
				add(path.Join(n, f.Name()))
			}
		}
		return
	}
	if fi.Mode()&os.ModeType == 0 {
		files = append(files, n)
		return
	}

	println(fi.Mode())

invalid:
	fmt.Fprintf(os.Stderr, "  ! %s\n", n)
}

type LData []LResult

func (d LData) Len() int { return len(d) }

func (d LData) Less(i, j int) bool {
	if d[i].CodeLines == d[j].CodeLines {
		return d[i].Name > d[j].Name
	}
	return d[i].CodeLines > d[j].CodeLines
}

func (d LData) Swap(i, j int) {
	d[i], d[j] = d[j], d[i]
}

type LResult struct {
	Name         string
	FileCount    int
	CodeLines    int
	CommentLines int
	BlankLines   int
	TotalLines   int
}

func (r *LResult) Add(a LResult) {
	r.FileCount += a.FileCount
	r.CodeLines += a.CodeLines
	r.CommentLines += a.CommentLines
	r.BlankLines += a.BlankLines
	r.TotalLines += a.TotalLines
}

func printJSON() {
	bs, err := json.MarshalIndent(info, "", "  ")
	if err != nil {
		panic(err)
	}
	fmt.Println(string(bs))
}

func printInfo() {
	w := tabwriter.NewWriter(os.Stdout, 2, 8, 2, ' ', tabwriter.AlignRight)
	fmt.Fprintln(w, "Language\tFiles\tCode\tComment\tBlank\tTotal\t")
	d := LData([]LResult{})
	total := &LResult{}
	total.Name = "Total"
	for n, i := range info {
		r := LResult{n, i.FileCount, i.CodeLines, i.CommentLines, i.BlankLines, i.TotalLines}
		d = append(d, r)
		total.Add(r)
	}
	d = append(d, *total)
	sort.Sort(d)
	for _, i := range d {
		fmt.Fprintf(w, "%s\t%d\t%d\t%d\t%d\t%d\t\n", i.Name, i.FileCount, i.CodeLines, i.CommentLines, i.BlankLines, i.TotalLines)
	}

	w.Flush()
}

var (
	cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
	useJson    = flag.Bool("json", false, "JSON-format output")
	version    = flag.Bool("V", false, "display version info and exit")
)

func main() {
	flag.Parse()
	if *version {
		fmt.Printf("sloc %s\n", VERSION)
		return
	}
	if *cpuprofile != "" {
		f, err := os.Create(*cpuprofile)
		if err != nil {
			fmt.Fprintf(os.Stderr, "error: %s\n", err.Error())
			return
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	args := flag.Args()
	if len(args) == 0 {
		args = append(args, `.`)
	}

	for _, n := range args {
		add(n)
	}

	for _, f := range files {
		handleFile(f)
	}

	if *useJson {
		printJSON()
	} else {
		printInfo()
	}
}
<|endoftext|>


<commit_before>package goarmorcachekeys

import (
	"bytes"
	"fmt"
)

type KeyType int

func (t KeyType) Sprint(a ...interface{}) string {
	var b bytes.Buffer

	for _, i := range a {
		b.WriteString(fmt.Sprint(i))
	}

	return fmt.Sprintf("%d%v", t, b.String())
}
<commit_msg>minor<commit_after>package goarmorcachekeys

import (
	"bytes"
	"fmt"
)

type KeyType int

func (t KeyType) Sprint(a ...interface{}) string {
	var b bytes.Buffer

	for _, v := range a {
		b.WriteString(fmt.Sprint(v))
	}

	return fmt.Sprintf("%d%v", t, b.String())
}
<|endoftext|>


<commit_before>package vegeta

import (
	"reflect"
	"testing"
	"time"
)

func TestMetrics_Add(t *testing.T) {
	t.Parallel()

	codes := []uint16{500, 200, 302}
	errors := []string{"Internal server error", ""}

	var got Metrics
	for i := 1; i <= 10000; i++ {
		got.Add(&Result{
			Code:      codes[i%len(codes)],
			Timestamp: time.Unix(int64(i-1), 0),
			Latency:   time.Duration(i) * time.Microsecond,
			BytesIn:   1024,
			BytesOut:  512,
			Error:     errors[i%len(errors)],
		})
	}
	got.Close()

	duration := func(s string) time.Duration {
		d, err := time.ParseDuration(s)
		if err != nil {
			panic(err)
		}
		return d
	}

	want := Metrics{
		Latencies: LatencyMetrics{
			Total: duration("50.005s"),
			Mean:  duration("5.0005ms"),
			P50:   duration("4.991ms"),
			P95:   duration("9.509ms"),
			P99:   duration("9.898ms"),
			Max:   duration("10ms"),
		},
		BytesIn:     ByteMetrics{Total: 10240000, Mean: 1024},
		BytesOut:    ByteMetrics{Total: 5120000, Mean: 512},
		Earliest:    time.Unix(0, 0),
		Latest:      time.Unix(9999, 0),
		End:         time.Unix(9999, 0).Add(10000 * time.Microsecond),
		Duration:    duration("2h46m39s"),
		Wait:        duration("10ms"),
		Requests:    10000,
		Rate:        1.000100010001,
		Success:     0.6667,
		StatusCodes: map[string]int{"500": 3333, "200": 3334, "302": 3333},
		Errors:      []string{"Internal server error"},

		errors:    got.errors,
		success:   got.success,
		latencies: got.latencies,
	}

	if !reflect.DeepEqual(got, want) {
		t.Errorf("\ngot:  %+v\nwant: %+v", got, want)
	}
}

// https://github.com/tsenart/vegeta/issues/208
func TestMetrics_NoInfiniteRate(t *testing.T) {
	t.Parallel()

	m := Metrics{Requests: 1, Duration: time.Microsecond}
	m.Close()

	if got, want := m.Rate, 1.0; got != want {
		t.Errorf("got rate %f, want %f", got, want)
	}
}

// https://github.com/tsenart/vegeta/pull/277
func TestMetrics_NilErrorOnClose(t *testing.T) {
	t.Parallel()

	m := Metrics{Errors: nil}
	m.Close()

	got, want := m.Errors, []string{}

	if !reflect.DeepEqual(got, want) {
		t.Errorf("\ngot:  %+v\nwant: %+v", got, want)
	}
}
<commit_msg>s/NilErrorOnClose/NonNilErrorsOnClose/<commit_after>package vegeta

import (
	"reflect"
	"testing"
	"time"
)

func TestMetrics_Add(t *testing.T) {
	t.Parallel()

	codes := []uint16{500, 200, 302}
	errors := []string{"Internal server error", ""}

	var got Metrics
	for i := 1; i <= 10000; i++ {
		got.Add(&Result{
			Code:      codes[i%len(codes)],
			Timestamp: time.Unix(int64(i-1), 0),
			Latency:   time.Duration(i) * time.Microsecond,
			BytesIn:   1024,
			BytesOut:  512,
			Error:     errors[i%len(errors)],
		})
	}
	got.Close()

	duration := func(s string) time.Duration {
		d, err := time.ParseDuration(s)
		if err != nil {
			panic(err)
		}
		return d
	}

	want := Metrics{
		Latencies: LatencyMetrics{
			Total: duration("50.005s"),
			Mean:  duration("5.0005ms"),
			P50:   duration("4.991ms"),
			P95:   duration("9.509ms"),
			P99:   duration("9.898ms"),
			Max:   duration("10ms"),
		},
		BytesIn:     ByteMetrics{Total: 10240000, Mean: 1024},
		BytesOut:    ByteMetrics{Total: 5120000, Mean: 512},
		Earliest:    time.Unix(0, 0),
		Latest:      time.Unix(9999, 0),
		End:         time.Unix(9999, 0).Add(10000 * time.Microsecond),
		Duration:    duration("2h46m39s"),
		Wait:        duration("10ms"),
		Requests:    10000,
		Rate:        1.000100010001,
		Success:     0.6667,
		StatusCodes: map[string]int{"500": 3333, "200": 3334, "302": 3333},
		Errors:      []string{"Internal server error"},

		errors:    got.errors,
		success:   got.success,
		latencies: got.latencies,
	}

	if !reflect.DeepEqual(got, want) {
		t.Errorf("\ngot:  %+v\nwant: %+v", got, want)
	}
}

// https://github.com/tsenart/vegeta/issues/208
func TestMetrics_NoInfiniteRate(t *testing.T) {
	t.Parallel()

	m := Metrics{Requests: 1, Duration: time.Microsecond}
	m.Close()

	if got, want := m.Rate, 1.0; got != want {
		t.Errorf("got rate %f, want %f", got, want)
	}
}

// https://github.com/tsenart/vegeta/pull/277
func TestMetrics_NonNilErrorsOnClose(t *testing.T) {
	t.Parallel()

	m := Metrics{Errors: nil}
	m.Close()

	got, want := m.Errors, []string{}

	if !reflect.DeepEqual(got, want) {
		t.Errorf("\ngot:  %+v\nwant: %+v", got, want)
	}
}
<|endoftext|>


<commit_before>package module

import (
	"fmt"
	"github.com/davidscholberg/irkbot/lib/configure"
	"github.com/davidscholberg/irkbot/lib/message"
	"math/rand"
	"strings"
	"time"
)

func ConfigDoing(cfg *configure.Config) {
	rand.Seed(time.Now().Unix())
}
func HelpDoing() []string {
	s := "doing [subject] - sun is not doing, [subject] is doing; defaults to linux"
	return []string{s}
}

func Doing(cfg *configure.Config, in *message.InboundMsg, actions *Actions) {
	doingNot := [...]string{
		"sun is",
		"stars are",
		"trees are",
		"moon is",
		"planets are",
		"galaxies are"}
	doingSub := "linux"
	if len(in.MsgArgs[1:]) > 0 {
		doingSub = strings.Join(in.MsgArgs[1:], " ")
	}
	msg := fmt.Sprintf("%s not doing, %s is doing", doingNot[rand.Intn(len(doingNot))], doingSub)
	actions.Say(msg)
}
<commit_msg>doing: updated default doer to command invoker<commit_after>package module

import (
	"fmt"
	"github.com/davidscholberg/irkbot/lib/configure"
	"github.com/davidscholberg/irkbot/lib/message"
	"math/rand"
	"strings"
	"time"
)

func ConfigDoing(cfg *configure.Config) {
	rand.Seed(time.Now().Unix())
}
func HelpDoing() []string {
	s := "doing [subject] - sun is not doing, [subject] is doing; defaults to command invoker"
	return []string{s}
}

func Doing(cfg *configure.Config, in *message.InboundMsg, actions *Actions) {
	doingNot := [...]string{
		"sun is",
		"stars are",
		"trees are",
		"moon is",
		"planets are",
		"galaxies are"}
	doingSub := in.Event.Nick
	if len(in.MsgArgs[1:]) > 0 {
		doingSub = strings.Join(in.MsgArgs[1:], " ")
	}
	msg := fmt.Sprintf("%s not doing, %s is doing", doingNot[rand.Intn(len(doingNot))], doingSub)
	actions.Say(msg)
}
<|endoftext|>


<commit_before>package module

import (
	"fmt"
	"github.com/davidscholberg/go-urbandict"
	"github.com/davidscholberg/irkbot/lib/message"
	"strings"
)

func HelpUrban() []string {
	s := []string{
		"urban [search phrase] - search urban dictionary for given phrase" +
			" (or get random definition if none given)",
	}
	return s
}

func HelpUrbanWotd() []string {
	s := []string{
		"urban_wotd - get the urban dictionary word of the day",
	}
	return s
}

func HelpUrbanTrending() []string {
	s := []string{
		"urban_trending - get the current urban dictionary trending list",
	}
	return s
}

func Urban(p *message.Privmsg) {
	showDefinition(p, false)
}

func UrbanWotd(p *message.Privmsg) {
	showDefinition(p, true)
}

func UrbanTrending(p *message.Privmsg) {
	showTrending(p)
}

func showDefinition(p *message.Privmsg, isWotd bool) {
	var def *urbandict.Definition
	var err error
	nick := p.Event.Nick
	if isWotd {
		def, err = urbandict.WordOfTheDay()
	} else if len(p.MsgArgs) == 1 {
		def, err = urbandict.Random()
	} else {
		def, err = urbandict.Define(strings.Join(p.MsgArgs[1:], " "))
	}
	if err != nil {
		message.Say(p, fmt.Sprintf("%s: %s", nick, err.Error()))
		return
	}

	// TODO: implement max message length handling

	if isWotd {
		message.Say(p, fmt.Sprintf("%s: Word of the day: \"%s\"", nick, def.Word))
	} else {
		message.Say(p, fmt.Sprintf("%s: Top definition for \"%s\"", nick, def.Word))
	}
	for _, line := range strings.Split(def.Definition, "\r\n") {
		message.Say(p, fmt.Sprintf("%s: %s", nick, line))
	}
	message.Say(p, fmt.Sprintf("%s: Example:", nick))
	for _, line := range strings.Split(def.Example, "\r\n") {
		message.Say(p, fmt.Sprintf("%s: %s", nick, line))
	}
	message.Say(p, fmt.Sprintf("%s: permalink: %s", nick, def.Permalink))
}

func showTrending(p *message.Privmsg) {
	nick := p.Event.Nick

	trendingWords, err := urbandict.Trending()
	if err != nil {
		message.Say(p, fmt.Sprintf("%s: %s", nick, err.Error()))
		return
	}

	message.Say(p, fmt.Sprintf("%s: Top %d trending words:",
		nick,
		len(trendingWords)))

	for i, word := range trendingWords {
		message.Say(p, fmt.Sprintf("%s: %d. %s", nick, i+1, word))
	}

	return
}
<commit_msg>made urban module less spammy<commit_after>package module

import (
	"fmt"
	"github.com/davidscholberg/go-urbandict"
	"github.com/davidscholberg/irkbot/lib/message"
	"strings"
)

func HelpUrban() []string {
	s := []string{
		"urban [search phrase] - search urban dictionary for given phrase" +
			" (or get random word if none given)",
	}
	return s
}

func HelpUrbanWotd() []string {
	s := []string{
		"urban_wotd - get the urban dictionary word of the day",
	}
	return s
}

func HelpUrbanTrending() []string {
	s := []string{
		"urban_trending - get the current urban dictionary trending list",
	}
	return s
}

func Urban(p *message.Privmsg) {
	showSearchResult(p)
}

func UrbanWotd(p *message.Privmsg) {
	showDefinition(p, true)
}

func UrbanTrending(p *message.Privmsg) {
	showTrending(p)
}

func showSearchResult(p *message.Privmsg) {
	var def *urbandict.Definition
	var err error
	var isRandom bool
	nick := p.Event.Nick
	if len(p.MsgArgs) == 1 {
		def, err = urbandict.Random()
		isRandom = true
	} else {
		def, err = urbandict.Define(strings.Join(p.MsgArgs[1:], " "))
		isRandom = false
	}
	if err != nil {
		message.Say(p, fmt.Sprintf("%s: %s", nick, err.Error()))
		return
	}

	if isRandom {
		message.Say(p, fmt.Sprintf("%s: random word: \"%s\" - %s", nick, def.Word, def.Permalink))
	} else {
		message.Say(p, fmt.Sprintf("%s: top result for \"%s\" - %s", nick, def.Word, def.Permalink))
	}
}

func showDefinition(p *message.Privmsg, isWotd bool) {
	var def *urbandict.Definition
	var err error
	nick := p.Event.Nick
	if isWotd {
		def, err = urbandict.WordOfTheDay()
	} else if len(p.MsgArgs) == 1 {
		def, err = urbandict.Random()
	} else {
		def, err = urbandict.Define(strings.Join(p.MsgArgs[1:], " "))
	}
	if err != nil {
		message.Say(p, fmt.Sprintf("%s: %s", nick, err.Error()))
		return
	}

	// TODO: implement max message length handling

	if isWotd {
		message.Say(p, fmt.Sprintf("%s: Word of the day: \"%s\"", nick, def.Word))
	} else {
		message.Say(p, fmt.Sprintf("%s: Top definition for \"%s\"", nick, def.Word))
	}
	for _, line := range strings.Split(def.Definition, "\r\n") {
		message.Say(p, fmt.Sprintf("%s: %s", nick, line))
	}
	message.Say(p, fmt.Sprintf("%s: Example:", nick))
	for _, line := range strings.Split(def.Example, "\r\n") {
		message.Say(p, fmt.Sprintf("%s: %s", nick, line))
	}
	message.Say(p, fmt.Sprintf("%s: permalink: %s", nick, def.Permalink))
}

func showTrending(p *message.Privmsg) {
	nick := p.Event.Nick

	trendingWords, err := urbandict.Trending()
	if err != nil {
		message.Say(p, fmt.Sprintf("%s: %s", nick, err.Error()))
		return
	}

	message.Say(p, fmt.Sprintf("%s: Top %d trending words:",
		nick,
		len(trendingWords)))

	for i, word := range trendingWords {
		message.Say(p, fmt.Sprintf("%s: %d. %s", nick, i+1, word))
	}

	return
}
<|endoftext|>