<commit_before>package core

import (
	"sync"

	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/core/types"
)

// BlockCache implements a caching mechanism specifically for blocks and uses FILO to pop
type BlockCache struct {
	size int

	hashes []common.Hash
	blocks map[common.Hash]*types.Block

	mu sync.RWMutex
}

// Creates and returns a `BlockCache` with `size`. If `size` is smaller than 1 it will panic
func NewBlockCache(size int) *BlockCache {
	if size < 1 {
		panic("block cache size not allowed to be smaller than 1")
	}

	bc := &BlockCache{size: size}
	bc.Clear()
	return bc
}

func (bc *BlockCache) Clear() {
	bc.blocks = make(map[common.Hash]*types.Block)
	bc.hashes = nil

}

func (bc *BlockCache) Push(block *types.Block) {
	bc.mu.Lock()
	defer bc.mu.Unlock()

	if len(bc.hashes) == bc.size {
		delete(bc.blocks, bc.hashes[0])

		// XXX There are a few other options on solving this
		// 1) use a poller / GC like mechanism to clean up untracked objects
		// 2) copy as below
		// re-use the slice and remove the reference to bc.hashes[0]
		// this will allow the element to be garbage collected.
		copy(bc.hashes, bc.hashes[1:])
	} else {
		bc.hashes = append(bc.hashes, common.Hash{})
	}

	hash := block.Hash()
	bc.blocks[hash] = block
	bc.hashes[len(bc.hashes)-1] = hash
}

func (bc *BlockCache) Delete(hash common.Hash) {
	bc.mu.Lock()
	defer bc.mu.Unlock()

	if _, ok := bc.blocks[hash]; ok {
		delete(bc.blocks, hash)
		for i, h := range bc.hashes {
			if hash == h {
				bc.hashes = bc.hashes[:i+copy(bc.hashes[i:], bc.hashes[i+1:])]
				// or ? => bc.hashes = append(bc.hashes[:i], bc.hashes[i+1]...)

				break
			}
		}
	}
}

func (bc *BlockCache) Get(hash common.Hash) *types.Block {
	bc.mu.RLock()
	defer bc.mu.RUnlock()

	if block, haz := bc.blocks[hash]; haz {
		return block
	}

	return nil
}

func (bc *BlockCache) Has(hash common.Hash) bool {
	_, ok := bc.blocks[hash]
	return ok
}

func (bc *BlockCache) Each(cb func(int, *types.Block)) {
	bc.mu.Lock()
	defer bc.mu.Unlock()

	i := 0
	for _, block := range bc.blocks {
		cb(i, block)
		i++
	}
}
<commit_msg>core: block cache Has method thread safe<commit_after>package core

import (
	"sync"

	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/core/types"
)

// BlockCache implements a caching mechanism specifically for blocks and uses FILO to pop
type BlockCache struct {
	size int

	hashes []common.Hash
	blocks map[common.Hash]*types.Block

	mu sync.RWMutex
}

// Creates and returns a `BlockCache` with `size`. If `size` is smaller than 1 it will panic
func NewBlockCache(size int) *BlockCache {
	if size < 1 {
		panic("block cache size not allowed to be smaller than 1")
	}

	bc := &BlockCache{size: size}
	bc.Clear()
	return bc
}

func (bc *BlockCache) Clear() {
	bc.blocks = make(map[common.Hash]*types.Block)
	bc.hashes = nil

}

func (bc *BlockCache) Push(block *types.Block) {
	bc.mu.Lock()
	defer bc.mu.Unlock()

	if len(bc.hashes) == bc.size {
		delete(bc.blocks, bc.hashes[0])

		// XXX There are a few other options on solving this
		// 1) use a poller / GC like mechanism to clean up untracked objects
		// 2) copy as below
		// re-use the slice and remove the reference to bc.hashes[0]
		// this will allow the element to be garbage collected.
		copy(bc.hashes, bc.hashes[1:])
	} else {
		bc.hashes = append(bc.hashes, common.Hash{})
	}

	hash := block.Hash()
	bc.blocks[hash] = block
	bc.hashes[len(bc.hashes)-1] = hash
}

func (bc *BlockCache) Delete(hash common.Hash) {
	bc.mu.Lock()
	defer bc.mu.Unlock()

	if _, ok := bc.blocks[hash]; ok {
		delete(bc.blocks, hash)
		for i, h := range bc.hashes {
			if hash == h {
				bc.hashes = bc.hashes[:i+copy(bc.hashes[i:], bc.hashes[i+1:])]
				// or ? => bc.hashes = append(bc.hashes[:i], bc.hashes[i+1]...)

				break
			}
		}
	}
}

func (bc *BlockCache) Get(hash common.Hash) *types.Block {
	bc.mu.RLock()
	defer bc.mu.RUnlock()

	if block, haz := bc.blocks[hash]; haz {
		return block
	}

	return nil
}

func (bc *BlockCache) Has(hash common.Hash) bool {
	bc.mu.RLock()
	defer bc.mu.RUnlock()

	_, ok := bc.blocks[hash]
	return ok
}

func (bc *BlockCache) Each(cb func(int, *types.Block)) {
	bc.mu.Lock()
	defer bc.mu.Unlock()

	i := 0
	for _, block := range bc.blocks {
		cb(i, block)
		i++
	}
}
<|endoftext|>


<commit_before>package index

import (
	"github.com/balzaczyy/golucene/core/index/model"
)

/*
Interface for internal atomic events. See DocumentsWriter fo details.
Events are executed concurrently and no order is guaranteed. Each
event should only rely on the serializeability within its process
method. All actions that must happen before or after a certain action
must be encoded inside the process() method.
*/
type Event func(writer *IndexWriter, triggerMerge, clearBuffers bool) error

var applyDeletesEvent = Event(func(writer *IndexWriter, triggerMerge, forcePurge bool) error {
	panic("not implemented yet")
})

var mergePendingEvent = Event(func(writer *IndexWriter, triggerMerge, forcePurge bool) error {
	panic("not implemented yet")
})

var forcedPurgeEvent = Event(func(writer *IndexWriter, triggerMerge, forcePurge bool) error {
	panic("not implemented yet")
})

func newFlushFailedEvent(info *model.SegmentInfo) Event {
	return Event(func(writer *IndexWriter, triggerMerge, forcePurge bool) error {
		return writer.flushFailed(info)
	})
}

func newDeleteNewFilesEvent(files map[string]bool) Event {
	return Event(func(writer *IndexWriter, triggerMerge, forcePurge bool) error {
		writer.Lock()
		defer writer.Unlock()
		var fileList []string
		for file, _ := range files {
			fileList = append(fileList, file)
		}
		writer.deleter.deleteNewFiles(fileList)
		return nil
	})
}
<commit_msg>implement ForcedPurgeEvent<commit_after>package index

import (
	"github.com/balzaczyy/golucene/core/index/model"
)

/*
Interface for internal atomic events. See DocumentsWriter fo details.
Events are executed concurrently and no order is guaranteed. Each
event should only rely on the serializeability within its process
method. All actions that must happen before or after a certain action
must be encoded inside the process() method.
*/
type Event func(writer *IndexWriter, triggerMerge, clearBuffers bool) error

var applyDeletesEvent = Event(func(writer *IndexWriter, triggerMerge, forcePurge bool) error {
	panic("not implemented yet")
})

var mergePendingEvent = Event(func(writer *IndexWriter, triggerMerge, forcePurge bool) error {
	panic("not implemented yet")
})

var forcedPurgeEvent = Event(func(writer *IndexWriter, triggerMerge, forcePurge bool) error {
	_, err := writer.purge(true)
	return err
})

func newFlushFailedEvent(info *model.SegmentInfo) Event {
	return Event(func(writer *IndexWriter, triggerMerge, forcePurge bool) error {
		return writer.flushFailed(info)
	})
}

func newDeleteNewFilesEvent(files map[string]bool) Event {
	return Event(func(writer *IndexWriter, triggerMerge, forcePurge bool) error {
		writer.Lock()
		defer writer.Unlock()
		var fileList []string
		for file, _ := range files {
			fileList = append(fileList, file)
		}
		writer.deleter.deleteNewFiles(fileList)
		return nil
	})
}
<|endoftext|>


<commit_before>package cli

import (
	"flag"
	"testing"

	rpcpb "github.com/google/shipshape/shipshape/proto/shipshape_rpc_proto"
)

var dockerTag = flag.String("shipshape_test_docker_tag", "", "the docker tag for the images to use for testing")

func countFailures(resp rpcpb.ShipshapeResponse) int {
	failures := 0
	for _, analyzeResp := range resp.AnalyzeResponse {
		failures += len(analyzeResp.Failure)
	}
	return failures
}

func countNotes(resp rpcpb.ShipshapeResponse) int {
	notes := 0
	for _, analyzeResp := range resp.AnalyzeResponse {
		notes += len(analyzeResp.Note)
	}
	return notes
}

func countCategoryNotes(resp rpcpb.ShipshapeResponse, category string) int {
	notes := 0
	for _, analyzeResp := range resp.AnalyzeResponse {
		for _, note := range analyzeResp.Note {
			if *note.Category == category {
				notes += 1
			}
		}
	}
	return notes
}

func TestExternalAnalyzers(t *testing.T) {
	// Replaces part of the e2e test
	// Create a fake maven project with android failures

	// Run CLI using a .shipshape file
}

func TestBuiltInAnalyzersPreBuild(t *testing.T) {
	options := Options{
		File:                "shipshape/cli/testdata/workspace1",
		ThirdPartyAnalyzers: []string{},
		Build:               "",
		TriggerCats:         []string{"PostMessage", "JSHint", "go vet", "PyLint"},
		Dind:                false,
		Event:               "manual", // TODO: const
		Repo:                "gcr.io/shipshape_releases", // TODO: const
		StayUp:              true,
		Tag:                 *dockerTag,
		// TODO(rsk): current e2e test can be run both with & without kythe.
		LocalKythe:          false,
	}
	var allResponses rpcpb.ShipshapeResponse
	options.HandleResponse = func(shipshapeResp *rpcpb.ShipshapeResponse, _ string) error {
		allResponses.AnalyzeResponse = append(allResponses.AnalyzeResponse, shipshapeResp.AnalyzeResponse...)
		return nil
	}
	returnedNotesCount, err := New(options).Run()
	if err != nil {
		t.Fatal(err)
	}
	testName := "TestBuiltInAnalyzerPreBuild"

	if got, want := countFailures(allResponses), 0; got != want {
		t.Errorf("%v: Wrong number of failures; got %v, want %v (proto data: %v)", testName, got, want, allResponses)
	}
	if countedNotes := countNotes(allResponses); returnedNotesCount != countedNotes {
		t.Errorf("%v: Inconsistent note count: returned %v, counted %v (proto data: %v", testName, returnedNotesCount, countedNotes, allResponses)
	}
	if got, want := returnedNotesCount, 21; got != want {
		t.Errorf("%v: Wrong number of notes; got %v, want %v (proto data: %v)", testName, got, want, allResponses)
	}
	if got, want := countCategoryNotes(allResponses, "PostMessage"), 2; got != want {
		t.Errorf("%v: Wrong number of PostMessage notes; got %v, want %v (proto data: %v)", testName, got, want, allResponses)
	}
	if got, want := countCategoryNotes(allResponses, "JSHint"), 8; got != want {
		t.Errorf("%v: Wrong number of JSHint notes; got %v, want %v (proto data: %v)", testName, got, want, allResponses)
	}
	if got, want := countCategoryNotes(allResponses, "go vet"), 0; got != want {
		t.Errorf("%v: Wrong number of go vet notes; got %v, want %v (proto data: %v)", testName, got, want, allResponses)
	}
	if got, want := countCategoryNotes(allResponses, "PyLint"), 10; got != want {
		t.Errorf("%v: Wrong number of PyLint notes; got %v, want %v (proto data: %v)", testName, got, want, allResponses)
	}
}

func TestBuiltInAnalyzersPostBuild(t *testing.T) {
	// Replaces part of the e2e test
	// Test with a kythe maven build
	// PostMessage and ErrorProne
}

func TestStreamsMode(t *testing.T) {
	// Test whether it works in streams mode
	// Before creating this, ensure that streams mode
	// is actually still something we need to support.
}

func TestChangingDirectories(t *testing.T) {
	// Replaces the changedir test
	// Make sure to test changing down, changing up, running on the same directory, running on a single file in the same directory, and changing to a sibling
}

func dumpLogs() {

}

func checkOutput(category string, numResults int) {

}
<commit_msg>Fixes test typo<commit_after>package cli

import (
	"flag"
	"testing"

	rpcpb "github.com/google/shipshape/shipshape/proto/shipshape_rpc_proto"
)

var dockerTag = flag.String("shipshape_test_docker_tag", "", "the docker tag for the images to use for testing")

func countFailures(resp rpcpb.ShipshapeResponse) int {
	failures := 0
	for _, analyzeResp := range resp.AnalyzeResponse {
		failures += len(analyzeResp.Failure)
	}
	return failures
}

func countNotes(resp rpcpb.ShipshapeResponse) int {
	notes := 0
	for _, analyzeResp := range resp.AnalyzeResponse {
		notes += len(analyzeResp.Note)
	}
	return notes
}

func countCategoryNotes(resp rpcpb.ShipshapeResponse, category string) int {
	notes := 0
	for _, analyzeResp := range resp.AnalyzeResponse {
		for _, note := range analyzeResp.Note {
			if *note.Category == category {
				notes += 1
			}
		}
	}
	return notes
}

func TestExternalAnalyzers(t *testing.T) {
	// Replaces part of the e2e test
	// Create a fake maven project with android failures

	// Run CLI using a .shipshape file
}

func TestBuiltInAnalyzersPreBuild(t *testing.T) {
	options := Options{
		File:                "shipshape/cli/testdata/workspace1",
		ThirdPartyAnalyzers: []string{},
		Build:               "",
		TriggerCats:         []string{"PostMessage", "JSHint", "go vet", "PyLint"},
		Dind:                false,
		Event:               "manual", // TODO: const
		Repo:                "gcr.io/shipshape_releases", // TODO: const
		StayUp:              true,
		Tag:                 *dockerTag,
		// TODO(rsk): current e2e test can be run both with & without kythe.
		LocalKythe:          false,
	}
	var allResponses rpcpb.ShipshapeResponse
	options.HandleResponse = func(shipshapeResp *rpcpb.ShipshapeResponse, _ string) error {
		allResponses.AnalyzeResponse = append(allResponses.AnalyzeResponse, shipshapeResp.AnalyzeResponse...)
		return nil
	}
	returnedNotesCount, err := New(options).Run()
	if err != nil {
		t.Fatal(err)
	}
	testName := "TestBuiltInAnalyzerPreBuild"

	if got, want := countFailures(allResponses), 0; got != want {
		t.Errorf("%v: Wrong number of failures; got %v, want %v (proto data: %v)", testName, got, want, allResponses)
	}
	if countedNotes := countNotes(allResponses); returnedNotesCount != countedNotes {
		t.Errorf("%v: Inconsistent note count: returned %v, counted %v (proto data: %v", testName, returnedNotesCount, countedNotes, allResponses)
	}
	if got, want := returnedNotesCount, 21; got != want {
		t.Errorf("%v: Wrong number of notes; got %v, want %v (proto data: %v)", testName, got, want, allResponses)
	}
	if got, want := countCategoryNotes(allResponses, "PostMessage"), 2; got != want {
		t.Errorf("%v: Wrong number of PostMessage notes; got %v, want %v (proto data: %v)", testName, got, want, allResponses)
	}
	if got, want := countCategoryNotes(allResponses, "JSHint"), 8; got != want {
		t.Errorf("%v: Wrong number of JSHint notes; got %v, want %v (proto data: %v)", testName, got, want, allResponses)
	}
	if got, want := countCategoryNotes(allResponses, "go vet"), 0; got != want {
		t.Errorf("%v: Wrong number of go vet notes; got %v, want %v (proto data: %v)", testName, got, want, allResponses)
	}
	if got, want := countCategoryNotes(allResponses, "PyLint"), 11; got != want {
		t.Errorf("%v: Wrong number of PyLint notes; got %v, want %v (proto data: %v)", testName, got, want, allResponses)
	}
}

func TestBuiltInAnalyzersPostBuild(t *testing.T) {
	// Replaces part of the e2e test
	// Test with a kythe maven build
	// PostMessage and ErrorProne
}

func TestStreamsMode(t *testing.T) {
	// Test whether it works in streams mode
	// Before creating this, ensure that streams mode
	// is actually still something we need to support.
}

func TestChangingDirectories(t *testing.T) {
	// Replaces the changedir test
	// Make sure to test changing down, changing up, running on the same directory, running on a single file in the same directory, and changing to a sibling
}

func dumpLogs() {

}

func checkOutput(category string, numResults int) {

}
<|endoftext|>


<commit_before>package gitManip

import (
	"fmt"

	"github.com/fatih/color"

	"bytes"

	"github.com/k0pernicus/goyave/traces"
	git "gopkg.in/libgit2/git2go.v26"
)

/*Map to match the RepositoryState enum type with a string
 */
var repositoryStateToString = map[git.RepositoryState]string{
	git.RepositoryStateNone:                 "None",
	git.RepositoryStateMerge:                "Merge",
	git.RepositoryStateRevert:               "Revert",
	git.RepositoryStateCherrypick:           "Cherrypick",
	git.RepositoryStateBisect:               "Bisect",
	git.RepositoryStateRebase:               "Rebase",
	git.RepositoryStateRebaseInteractive:    "Rebase Interactive",
	git.RepositoryStateRebaseMerge:          "Rebase Merge",
	git.RepositoryStateApplyMailbox:         "Apply Mailbox",
	git.RepositoryStateApplyMailboxOrRebase: "Apply Mailbox or Rebase",
}

/*Global variable to set the StatusOption parameter, in order to list each file status
 */
var statusOption = git.StatusOptions{
	Show:     git.StatusShowIndexAndWorkdir,
	Flags:    git.StatusOptIncludeUntracked,
	Pathspec: []string{},
}

/*GitObject contains informations about the current git repository
 *
 *The structure is:
 *  accessible:
 *		Is the repository still exists in the hard drive?
 *	path:
 *		The path file.
 *	repository:
 *		The object repository.
 */
type GitObject struct {
	accessible error
	path       string
	repository git.Repository
}

/*New is a constructor for GitObject
 *
 * It neeeds:
 *	path:
 *		The path of the current repository.
 */
func New(path string) *GitObject {
	r, err := git.OpenRepository(path)
	return &GitObject{accessible: err, path: path, repository: *r}
}

/*Clone is cloning a given repository, from a public URL
 *
 * It needs:
 * path:
 *		The local path to clone the repository.
 *	URL:
 *		The remote URL to fetch the repository.
 */
func Clone(path, URL string) error {
	_, err := git.Clone(URL, path, &git.CloneOptions{})
	return err
}

/*GetRemoteURL returns the associated remote URL of a given local path repository
 *
 * It needs:
 *	path
 *		The local path of a git repository
 */
func GetRemoteURL(path string) string {
	r, err := git.OpenRepository(path)
	if err != nil {
		fmt.Println("The repository can't be opened")
		return ""
	}
	remoteCollection := r.Remotes
	originRemote, err := remoteCollection.Lookup("origin")
	if err != nil {
		traces.WarningTracer.Printf("can't lookup origin remote URL for %s", path)
		return ""
	}
	return originRemote.Url()
}

/*isAccesible returns the information that is the current git repository is existing or not.
 *This method returns a boolean value: true if the git repository is still accesible (still exists), or false if not.
 */
func (g *GitObject) isAccessible() bool {
	return g.accessible == nil
}

/*Status prints the current status of the repository, accessible via the structure path field.
 *This method works only if the repository is accessible.
 */
func (g *GitObject) Status() {
	if g.isAccessible() {
		if err := g.printChanges(); err != nil {
			color.RedString("Impossible to get stats from %s, due to error %s", g.path, err)
		}
	} else {
		color.RedString("Repository %s not found!", g.path)
	}
}

/*getDiffWithWT returns the difference between the working tree and the index, for the current git repository.
 *If there is an error processing the request, it returns an error.
 */
func (g *GitObject) getDiffWithWT() (*git.Diff, error) {
	// Get the index of the repository
	currentIndex, err := g.repository.Index()
	if err != nil {
		return nil, err
	}
	// Get the default diff options, and add it custom flags
	defaultDiffOptions, err := git.DefaultDiffOptions()
	if err != nil {
		return nil, err
	}
	defaultDiffOptions.Flags = defaultDiffOptions.Flags | git.DiffIncludeUntracked | git.DiffIncludeTypeChange
	// Check the difference between the working directory and the index
	diff, err := g.repository.DiffIndexToWorkdir(currentIndex, &defaultDiffOptions)
	if err != nil {
		return nil, err
	}
	return diff, nil
}

/*printChanges prints out all changes for the current git repository.
 *If there is an error processing the request, it returns this one.
 */
func (g *GitObject) printChanges() error {
	diff, err := g.getDiffWithWT()
	var buffer bytes.Buffer
	if err != nil {
		return err
	}
	numDeltas, err := diff.NumDeltas()
	if err != nil {
		return err
	}
	headDetached, err := g.repository.IsHeadDetached()
	if err != nil {
		return err
	}
	if headDetached {
		outputHead := fmt.Sprintf("%s", color.RedString("\t/!\\ The repository's HEAD is detached! /!\\\n"))
		buffer.WriteString(outputHead)
	}
	if numDeltas > 0 {
		buffer.WriteString(fmt.Sprintf("%s %s\t[%d modification(s)]\n", color.RedString("✘"), g.path, numDeltas))
		for i := 0; i < numDeltas; i++ {
			delta, _ := diff.GetDelta(i)
			currentStatus := delta.Status
			newFile := delta.NewFile.Path
			oldFile := delta.OldFile.Path
			switch currentStatus {
			case git.DeltaAdded:
				buffer.WriteString(fmt.Sprintf("\t===> %s has been added!\n", color.MagentaString(newFile)))
			case git.DeltaDeleted:
				buffer.WriteString(fmt.Sprintf("\t===> %s has been deleted!\n", color.MagentaString(newFile)))
			case git.DeltaModified:
				buffer.WriteString(fmt.Sprintf("\t===> %s has been modified!\n", color.MagentaString(newFile)))
			case git.DeltaRenamed:
				buffer.WriteString(fmt.Sprintf("\t===> %s has been renamed to %s!\n", color.MagentaString(oldFile), color.MagentaString(newFile)))
			case git.DeltaUntracked:
				buffer.WriteString(fmt.Sprintf("\t===> %s is untracked - please to add it or update the gitignore file!\n", color.MagentaString(newFile)))
			case git.DeltaTypeChange:
				buffer.WriteString(fmt.Sprintf("\t===> the type of %s has been changed from %d to %d!", color.MagentaString(newFile), delta.OldFile.Mode, delta.NewFile.Mode))
			}
		}
	} else {
		buffer.WriteString(fmt.Sprintf("%s %s\n", color.GreenString("✔"), g.path))
	}
	fmt.Print(buffer.String())
	return nil
}
<commit_msg>Add needToCommit? feature<commit_after>package gitManip

import (
	"fmt"

	"github.com/fatih/color"

	"bytes"

	"github.com/k0pernicus/goyave/traces"
	git "gopkg.in/libgit2/git2go.v26"
)

/*Map to match the RepositoryState enum type with a string
 */
var repositoryStateToString = map[git.RepositoryState]string{
	git.RepositoryStateNone:                 "None",
	git.RepositoryStateMerge:                "Merge",
	git.RepositoryStateRevert:               "Revert",
	git.RepositoryStateCherrypick:           "Cherrypick",
	git.RepositoryStateBisect:               "Bisect",
	git.RepositoryStateRebase:               "Rebase",
	git.RepositoryStateRebaseInteractive:    "Rebase Interactive",
	git.RepositoryStateRebaseMerge:          "Rebase Merge",
	git.RepositoryStateApplyMailbox:         "Apply Mailbox",
	git.RepositoryStateApplyMailboxOrRebase: "Apply Mailbox or Rebase",
}

/*Global variable to set the StatusOption parameter, in order to list each file status
 */
var statusOption = git.StatusOptions{
	Show:     git.StatusShowIndexAndWorkdir,
	Flags:    git.StatusOptIncludeUntracked,
	Pathspec: []string{},
}

/*GitObject contains informations about the current git repository
 *
 *The structure is:
 *  accessible:
 *		Is the repository still exists in the hard drive?
 *	path:
 *		The path file.
 *	repository:
 *		The object repository.
 */
type GitObject struct {
	accessible error
	path       string
	repository git.Repository
}

/*New is a constructor for GitObject
 *
 * It neeeds:
 *	path:
 *		The path of the current repository.
 */
func New(path string) *GitObject {
	r, err := git.OpenRepository(path)
	return &GitObject{accessible: err, path: path, repository: *r}
}

/*Clone is cloning a given repository, from a public URL
 *
 * It needs:
 * path:
 *		The local path to clone the repository.
 *	URL:
 *		The remote URL to fetch the repository.
 */
func Clone(path, URL string) error {
	_, err := git.Clone(URL, path, &git.CloneOptions{})
	return err
}

/*GetRemoteURL returns the associated remote URL of a given local path repository
 *
 * It needs:
 *	path
 *		The local path of a git repository
 */
func GetRemoteURL(path string) string {
	r, err := git.OpenRepository(path)
	if err != nil {
		fmt.Println("The repository can't be opened")
		return ""
	}
	remoteCollection := r.Remotes
	originRemote, err := remoteCollection.Lookup("origin")
	if err != nil {
		traces.WarningTracer.Printf("can't lookup origin remote URL for %s", path)
		return ""
	}
	return originRemote.Url()
}

/*isAccesible returns the information that is the current git repository is existing or not.
 *This method returns a boolean value: true if the git repository is still accesible (still exists), or false if not.
 */
func (g *GitObject) isAccessible() bool {
	return g.accessible == nil
}

/*Status prints the current status of the repository, accessible via the structure path field.
 *This method works only if the repository is accessible.
 */
func (g *GitObject) Status() {
	if g.isAccessible() {
		if err := g.printChanges(); err != nil {
			color.RedString("Impossible to get stats from %s, due to error %s", g.path, err)
		}
	} else {
		color.RedString("Repository %s not found!", g.path)
	}
}

/*getDiffWithWT returns the difference between the working tree and the index, for the current git repository.
 *If there is an error processing the request, it returns an error.
 */
func (g *GitObject) getDiffWithWT() (*git.Diff, error) {
	// Get the index of the repository
	currentIndex, err := g.repository.Index()
	if err != nil {
		return nil, err
	}
	// Get the default diff options, and add it custom flags
	defaultDiffOptions, err := git.DefaultDiffOptions()
	if err != nil {
		return nil, err
	}
	defaultDiffOptions.Flags = defaultDiffOptions.Flags | git.DiffIncludeUntracked | git.DiffIncludeTypeChange
	// Check the difference between the working directory and the index
	diff, err := g.repository.DiffIndexToWorkdir(currentIndex, &defaultDiffOptions)
	if err != nil {
		return nil, err
	}
	return diff, nil
}

/*printChanges prints out all changes for the current git repository.
 *If there is an error processing the request, it returns this one.
 */
func (g *GitObject) printChanges() error {
	diff, err := g.getDiffWithWT()
	var buffer bytes.Buffer
	if err != nil {
		return err
	}
	numDeltas, err := diff.NumDeltas()
	if err != nil {
		return err
	}
	headDetached, err := g.repository.IsHeadDetached()
	if err != nil {
		return err
	}
	if headDetached {
		outputHead := fmt.Sprintf("%s", color.RedString("\t/!\\ The repository's HEAD is detached! /!\\\n"))
		buffer.WriteString(outputHead)
	}
	if numDeltas > 0 {
		buffer.WriteString(fmt.Sprintf("%s %s\t[%d modification(s)]\n", color.RedString("✘"), g.path, numDeltas))
		for i := 0; i < numDeltas; i++ {
			delta, _ := diff.GetDelta(i)
			currentStatus := delta.Status
			newFile := delta.NewFile.Path
			oldFile := delta.OldFile.Path
			switch currentStatus {
			case git.DeltaAdded:
				buffer.WriteString(fmt.Sprintf("\t===> %s has been added!\n", color.MagentaString(newFile)))
			case git.DeltaDeleted:
				buffer.WriteString(fmt.Sprintf("\t===> %s has been deleted!\n", color.MagentaString(newFile)))
			case git.DeltaModified:
				buffer.WriteString(fmt.Sprintf("\t===> %s has been modified!\n", color.MagentaString(newFile)))
			case git.DeltaRenamed:
				buffer.WriteString(fmt.Sprintf("\t===> %s has been renamed to %s!\n", color.MagentaString(oldFile), color.MagentaString(newFile)))
			case git.DeltaUntracked:
				buffer.WriteString(fmt.Sprintf("\t===> %s is untracked - please to add it or update the gitignore file!\n", color.MagentaString(newFile)))
			case git.DeltaTypeChange:
				buffer.WriteString(fmt.Sprintf("\t===> the type of %s has been changed from %d to %d!", color.MagentaString(newFile), delta.OldFile.Mode, delta.NewFile.Mode))
			}
		}
	} else {
		buffer.WriteString(fmt.Sprintf("%s %s\n", color.GreenString("✔"), g.path))
	}
	repository_head, err := g.repository.Head()
	if err == nil {
		repository_id := repository_head.Target()
		commits_ahead, _, err := g.repository.AheadBehind(repository_id, repository_id)
		if err != nil {
			buffer.WriteString(fmt.Sprintf("%s", color.RedString("\tAn error occured checking the ahead/behind commits...\n")))
		} else if commits_ahead != 0 {
			buffer.WriteString(fmt.Sprintf("\tYou need to push the last modifications!\n"))
		}
	}
	fmt.Print(buffer.String())
	return nil
}
<|endoftext|>


<commit_before>package ac

import "testing"

func TestAlienOrder(t *testing.T) {
	st := []struct {
		name  string
		words []string
		exp   string
	}{
		{"empty words", []string{}, ""},
		{"test1", []string{"wrt", "wrf", "er", "ett", "rftt"}, "wertf"},
		{"test2", []string{"z", "x"}, "zx"},
		{"test3", []string{"z", "x", "z"}, ""},
		{"failed1", []string{"za", "zb", "ca", "cb"}, "abzc"},
	}
	for _, tt := range st {
		t.Run(tt.name, func(t *testing.T) {
			out := alienOrder(tt.words)
			if out != tt.exp {
				t.Fatalf("with input words: %v wanted %s but got %s", tt.words, tt.exp, out)
			}
			t.Log("pass")
		})
	}
}
<commit_msg>fix failed test:<commit_after>package ac

import "testing"

func TestAlienOrder(t *testing.T) {
	st := []struct {
		name  string
		words []string
		exp   string
	}{
		{"empty words", []string{}, ""},
		{"test1", []string{"wrt", "wrf", "er", "ett", "rftt"}, "wertf"},
		{"test2", []string{"z", "x"}, "zx"},
		{"test3", []string{"z", "x", "z"}, ""},
		{"failed1", []string{"za", "zb", "ca", "cb"}, "zacb"},
	}
	for _, tt := range st {
		t.Run(tt.name, func(t *testing.T) {
			out := alienOrder(tt.words)
			if out != tt.exp {
				t.Fatalf("with input words: %v wanted %s but got %s", tt.words, tt.exp, out)
			}
			t.Log("pass")
		})
	}
}
<|endoftext|>


<commit_before>package cmd

import "testing"

func TestCreate(t *testing.T) {

}

func TestGitCloneCreate(t *testing.T) {

}
<commit_msg>Created test cases for Migration creation<commit_after>package cmd

import (
	"fmt"
	"testing"

	"github.com/freneticmonkey/migrate/go/exec"
	"github.com/freneticmonkey/migrate/go/metadata"
	"github.com/freneticmonkey/migrate/go/migration"
	"github.com/freneticmonkey/migrate/go/mysql"
	"github.com/freneticmonkey/migrate/go/table"
	"github.com/freneticmonkey/migrate/go/test"
	"github.com/freneticmonkey/migrate/go/util"
	"github.com/urfave/cli"
)

func TestCreateFailNoProject(t *testing.T) {
	testName := "TestCreateFailNoProject"

	util.LogAlert(testName)

	// Test Configuration
	testConfig := test.GetTestConfig()

	project := ""

	testConfig.Project.Name = project

	result := create(project, "", testConfig)

	if result.ExitCode() < 1 {
		t.Errorf("%s succeeded when it should have failed.", testName)
		return
	}
}

func TestCreateFailNoProjectVersion(t *testing.T) {
	testName := "TestCreateFailNoProjectVersion"

	util.LogAlert(testName)

	// Test Configuration
	testConfig := test.GetTestConfig()

	project := "UnitTestProject"
	version := ""

	testConfig.Project.Name = project
	testConfig.Project.Schema.Version = version

	result := create(project, version, testConfig)

	if result.ExitCode() < 1 {
		t.Errorf("%s succeeded when it should have failed.", testName)
		return
	}
}

func TestCreate(t *testing.T) {
	testName := "TestCreate"
	util.LogAlert(testName)

	var err error
	var result *cli.ExitError
	var projectDB test.ProjectDB
	var mgmtDB test.ManagementDB

	util.SetConfigTesting()

	project := "UnitTestProject"
	version := "abc123"

	// Configure testing data
	testConfig := test.GetTestConfig()
	dogsTbl := GetTableDogs()
	dogsAddTbl := GetTableAddressDogs()

	// Configuring the expected MDID for the new Column
	expectedAddressMetadata := dogsAddTbl.Columns[1].Metadata
	expectedAddressMetadata.MDID = 4

	////////////////////////////////////////////////////////
	// Configure Git Checkout

	var sparseExists bool
	var data []byte

	expectedSparseFile := `schema/*
schemaTwo/*`

	// Configure unit test shell
	util.Config(testConfig)

	checkoutPath := util.WorkingSubDir(testConfig.Project.Name)

	// Build Git Commands

	shell := util.GetShell().(*util.MockShellExecutor)

	// init
	params := []string{
		"-C",
		checkoutPath,
		"init",
	}
	shell.ExpectExec("git", params, "", nil)

	// git add remote
	params = []string{
		"-C",
		checkoutPath,
		"remote",
		"add",
		"-f",
		"origin",
		testConfig.Project.Schema.Url,
	}
	shell.ExpectExec("git", params, "", nil)

	// git config sparse checkout
	params = []string{
		"-C",
		checkoutPath,
		"config",
		"core.sparseCheckout",
		"true",
	}
	shell.ExpectExec("git", params, "", nil)

	// Checkout
	// git config sparse checkout
	params = []string{
		"-C",
		checkoutPath,
		"checkout",
		testConfig.Project.Schema.Version,
	}

	shell.ExpectExecWithLambda(
		"git",
		params,
		"",
		nil,
		func(cmd string, args []string) error {

			////////////////////////////////////////////////////////
			// Configure source YAML files for the 'checked out' Schema
			//

			test.WriteFile(
				"UnitTestProject/dogs.yml",
				GetYAMLTableAddressDogs(),
				0644,
				false,
			)

			//
			////////////////////////////////////////////////////////
			return nil
		},
	)

	//
	////////////////////////////////////////////////////////

	////////////////////////////////////////////////////////
	// Configure MySQL db reads for MySQL Schema
	//

	// Configure the test databases
	// Setup the mock project database
	projectDB, err = test.CreateProjectDB(testName, t)

	if err == nil {
		// Connect to Project DB
		exec.SetProjectDB(projectDB.Db)
		mysql.Setup(testConfig)

		// Connect to Project DB
		mysql.SetProjectDB(projectDB.Db.Db)
	}

	// Configure the Mock Managment DB
	mgmtDB, err = test.CreateManagementDB(testName, t)

	if err == nil {
		// migration.Setup(mgmtDB.Db, 1)
		exec.Setup(mgmtDB.Db, 1, testConfig.Project.DB.ConnectString())
		migration.Setup(mgmtDB.Db, 1)
		metadata.Setup(mgmtDB.Db, 1)
	}

	// SHOW TABLES Query
	projectDB.ShowTables([]test.DBRow{{dogsTbl.Name}}, false)

	// SHOW CREATE TABLE Query
	projectDB.ShowCreateTable(dogsTbl.Name, GetMySQLCreateTableDogs())

	// ParseCreateTable which includes a Table.LoadDBMetadata() call
	mgmtDB.MetadataSelectName(
		dogsTbl.Name,
		test.GetDBRowMetadata(dogsTbl.Metadata),
		false,
	)

	mgmtDB.MetadataLoadAllTableMetadata(dogsTbl.Metadata.PropertyID,
		1,
		[]test.DBRow{
			test.GetDBRowMetadata(dogsTbl.Metadata),
			test.GetDBRowMetadata(dogsTbl.Columns[0].Metadata),
			test.GetDBRowMetadata(dogsTbl.PrimaryIndex.Metadata),
		},
		false,
	)

	// STARTING Diff Queries - forwards

	mgmtDB.MetadataSelectName(
		dogsAddTbl.Name,
		test.GetDBRowMetadata(dogsAddTbl.Metadata),
		false,
	)

	//Diff will also sync metadata for the YAML Schema
	mgmtDB.MetadataLoadAllTableMetadata(dogsAddTbl.Metadata.PropertyID,
		1,
		[]test.DBRow{
			test.GetDBRowMetadata(dogsAddTbl.Metadata),
			test.GetDBRowMetadata(dogsAddTbl.Columns[0].Metadata),
			test.GetDBRowMetadata(dogsAddTbl.Columns[1].Metadata),
			test.GetDBRowMetadata(dogsAddTbl.PrimaryIndex.Metadata),
		},
		false,
	)

	// Expect an insert for Metadata for the new column
	mgmtDB.MetadataInsert(
		test.DBRow{
			dogsAddTbl.Columns[1].Metadata.DB,
			dogsAddTbl.Columns[1].Metadata.PropertyID,
			dogsAddTbl.Columns[1].Metadata.ParentID,
			dogsAddTbl.Columns[1].Metadata.Type,
			dogsAddTbl.Columns[1].Metadata.Name,
			false,
		},
		expectedAddressMetadata.MDID,
		1,
	)

	//
	////////////////////////////////////////////////////////

	////////////////////////////////////////////////////////
	// Git requests to pull back state of current checkout

	// GitVersionTime
	gitTime := "2016-07-12T22:04:05+10:00"
	mysqlTime := "2016-07-12 12:04:05"

	params = []string{
		"-C",
		util.WorkingSubDir(project),
		"show",
		"-s",
		"--format=%%cI",
	}
	shell.ExpectExec("git", params, gitTime, nil)

	// GitVersionDetails
	gitDetails := `commit abc123
    Author: Scott Porter <sporter@ea.com>
    Date:   Tue Jul 12 22:04:05 2016 +1000

    An example git commit for unit testing`

	params = []string{
		"-C",
		util.WorkingSubDir(project),
		"show",
		"-s",
		"--pretty=medium",
	}
	shell.ExpectExec("git", params, gitDetails, nil)

	//
	////////////////////////////////////////////////////////

	////////////////////////////////////////////////////////
	// New migration being inserted into the database

	// Helper operation
	forward := mysql.SQLOperation{
		Statement: "ALTER TABLE `unittestproject_dogs` COLUMN `address` varchar(128) NOT NULL;",
		Op:        table.Add,
		Name:      "address",
		Metadata: metadata.Metadata{
			MDID:       4,
			DB:         1,
			PropertyID: "unittestproject_dogs_col_address",
			ParentID:   "unittestproject_dogs",
			Name:       "address",
		},
	}
	backwardsStatement := "ALTER TABLE `unittestproject_dogs` DROP COLUMN `address`;"

	// Pulling table metadata - diff backwards
	mgmtDB.MetadataSelectName(
		dogsAddTbl.Name,
		test.GetDBRowMetadata(dogsAddTbl.Metadata),
		false,
	)

	//Diff will also sync metadata for the YAML Schema
	mgmtDB.MetadataLoadAllTableMetadata(dogsAddTbl.Metadata.PropertyID,
		1,
		[]test.DBRow{
			test.GetDBRowMetadata(dogsAddTbl.Metadata),
			test.GetDBRowMetadata(dogsAddTbl.Columns[0].Metadata),
			test.GetDBRowMetadata(dogsAddTbl.Columns[1].Metadata),
			test.GetDBRowMetadata(dogsAddTbl.PrimaryIndex.Metadata),
		},
		false,
	)

	// Counting the Migrations
	mgmtDB.MigrationCount(test.DBRow{0}, false)

	// Inserting a new Migration
	mgmtDB.MigrationInsert(
		test.DBRow{
			1,
			testConfig.Project.Name,
			testConfig.Project.Schema.Version,
			mysqlTime,
			gitDetails,
			0,
		},
		1,
		1,
	)

	// Inserting the Migration Step
	mgmtDB.MigrationInsertStep(
		test.DBRow{
			1,
			forward.Op,
			forward.Metadata.MDID,
			forward.Name,
			forward.Statement,
			backwardsStatement,
			"",
			0,
		},
		1,
		1,
	)

	//
	////////////////////////////////////////////////////////

	result = create(project, version, testConfig)

	if result.ExitCode() > 0 {
		t.Errorf("%s failed with error: %v", testName, result)
		return
	}

	projectDB.ExpectionsMet(testName, t)

	mgmtDB.ExpectionsMet(testName, t)

	////////////////////////////////////////////////////////
	// Git was checked out correctly

	if err != nil {
		t.Errorf("%s FAILED with error: %v", testName, err)
		return
	}

	// Validate that the sparse checkout file was created correctly
	sparseFile := fmt.Sprintf("%s/.git/info/sparse-checkout", checkoutPath)
	sparseExists, err = util.FileExists(sparseFile)

	if err != nil {
		t.Errorf("%s FAILED: there was an error while reading the sparse checkut file at path: [%s] with error: [%v]", testName, sparseFile, err)
		return
	}

	if !sparseExists {
		t.Errorf("%s FAILED: sparse checkut file missing from path: [%s]", testName, sparseFile)
		return
	}

	data, err = util.ReadFile(sparseFile)
	sparseData := string(data)

	if sparseData != expectedSparseFile {
		t.Errorf("%s FAILED: sparse data file contents: [%s] doesn't match expected contents: [%s]", testName, sparseData, expectedSparseFile)
		return
	}

	// Ensure that all of the anticipated shell calls were made
	if err = shell.ExpectationsWereMet(); err != nil {
		t.Errorf("%s FAILED: Not all shell commands were executed: error [%v]", testName, err)
	}

	Teardown()
}
<|endoftext|>


<commit_before>/*
Copyright 2017 Google Inc.

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 mysql

import (
	"bytes"
	"fmt"
	"regexp"
	"strconv"

	"vitess.io/vitess/go/vt/sqlparser"
)

// SQLError is the error structure returned from calling a db library function
type SQLError struct {
	Num     int
	State   string
	Message string
	Query   string
}

// NewSQLError creates a new SQLError.
// If sqlState is left empty, it will default to "HY000" (general error).
func NewSQLError(number int, sqlState string, format string, args ...interface{}) *SQLError {
	if sqlState == "" {
		sqlState = SSUnknownSQLState
	}
	return &SQLError{
		Num:     number,
		State:   sqlState,
		Message: fmt.Sprintf(format, args...),
	}
}

// Error implements the error interface
func (se *SQLError) Error() string {
	buf := &bytes.Buffer{}
	buf.WriteString(se.Message)

	// Add MySQL errno and SQLSTATE in a format that we can later parse.
	// There's no avoiding string parsing because all errors
	// are converted to strings anyway at RPC boundaries.
	// See NewSQLErrorFromError.
	fmt.Fprintf(buf, " (errno %v) (sqlstate %v)", se.Num, se.State)

	if se.Query != "" {
		fmt.Fprintf(buf, " during query: %s", sqlparser.TruncateForLog(se.Query))
	}

	return buf.String()
}

// Number returns the internal MySQL error code.
func (se *SQLError) Number() int {
	return se.Num
}

// SQLState returns the SQLSTATE value.
func (se *SQLError) SQLState() string {
	return se.State
}

var errExtract = regexp.MustCompile(`.*\(errno ([0-9]*)\) \(sqlstate ([0-9a-zA-Z]{5})\).*`)

// NewSQLErrorFromError returns a *SQLError from the provided error.
// If it's not the right type, it still tries to get it from a regexp.
func NewSQLErrorFromError(err error) error {
	if err == nil {
		return nil
	}

	if serr, ok := err.(*SQLError); ok {
		return serr
	}

	msg := err.Error()
	match := errExtract.FindStringSubmatch(msg)
	if len(match) < 2 {
		// Not found, build a generic SQLError.
		// TODO(alainjobart) maybe we can also check the canonical
		// error code, and translate that into the right error.
		return &SQLError{
			Num:     ERUnknownError,
			State:   SSUnknownSQLState,
			Message: msg,
		}
	}

	num, err := strconv.Atoi(match[1])
	if err != nil {
		return &SQLError{
			Num:     ERUnknownError,
			State:   SSUnknownSQLState,
			Message: msg,
		}
	}

	serr := &SQLError{
		Num:     num,
		State:   match[2],
		Message: msg,
	}
	return serr
}
<commit_msg>set the most appropriate mysql error code when creating a SQLError<commit_after>/*
Copyright 2017 Google Inc.

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 mysql

import (
	"bytes"
	"fmt"
	"regexp"
	"strconv"

	"vitess.io/vitess/go/vt/sqlparser"
	"vitess.io/vitess/go/vt/vterrors"

	vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
)

// SQLError is the error structure returned from calling a db library function
type SQLError struct {
	Num     int
	State   string
	Message string
	Query   string
}

// NewSQLError creates a new SQLError.
// If sqlState is left empty, it will default to "HY000" (general error).
func NewSQLError(number int, sqlState string, format string, args ...interface{}) *SQLError {
	if sqlState == "" {
		sqlState = SSUnknownSQLState
	}
	return &SQLError{
		Num:     number,
		State:   sqlState,
		Message: fmt.Sprintf(format, args...),
	}
}

// Error implements the error interface
func (se *SQLError) Error() string {
	buf := &bytes.Buffer{}
	buf.WriteString(se.Message)

	// Add MySQL errno and SQLSTATE in a format that we can later parse.
	// There's no avoiding string parsing because all errors
	// are converted to strings anyway at RPC boundaries.
	// See NewSQLErrorFromError.
	fmt.Fprintf(buf, " (errno %v) (sqlstate %v)", se.Num, se.State)

	if se.Query != "" {
		fmt.Fprintf(buf, " during query: %s", sqlparser.TruncateForLog(se.Query))
	}

	return buf.String()
}

// Number returns the internal MySQL error code.
func (se *SQLError) Number() int {
	return se.Num
}

// SQLState returns the SQLSTATE value.
func (se *SQLError) SQLState() string {
	return se.State
}

var errExtract = regexp.MustCompile(`.*\(errno ([0-9]*)\) \(sqlstate ([0-9a-zA-Z]{5})\).*`)

// NewSQLErrorFromError returns a *SQLError from the provided error.
// If it's not the right type, it still tries to get it from a regexp.
func NewSQLErrorFromError(err error) error {
	if err == nil {
		return nil
	}

	if serr, ok := err.(*SQLError); ok {
		return serr
	}

	msg := err.Error()
	match := errExtract.FindStringSubmatch(msg)
	if len(match) < 2 {
		// Map vitess error codes into the mysql equivalent
		code := vterrors.Code(err)
		num := ERUnknownError
		switch code {
		case vtrpcpb.Code_CANCELED:
			num = ERQueryInterrupted
		case vtrpcpb.Code_UNKNOWN:
			num = ERUnknownError
		case vtrpcpb.Code_INVALID_ARGUMENT:
			// TODO/demmer there are several more appropriate mysql error
			// codes for the various invalid argument cases.
			// it would be better to change the call sites to use
			// the mysql style "(errno X) (sqlstate Y)" format rather than
			// trying to add vitess error codes for all these cases
			num = ERUnknownError
		case vtrpcpb.Code_DEADLINE_EXCEEDED:
			num = ERQueryInterrupted
		case vtrpcpb.Code_NOT_FOUND:
			num = ERUnknownError
		case vtrpcpb.Code_ALREADY_EXISTS:
			num = ERUnknownError
		case vtrpcpb.Code_PERMISSION_DENIED:
			num = ERAccessDeniedError
		case vtrpcpb.Code_UNAUTHENTICATED:
			num = ERAccessDeniedError
		case vtrpcpb.Code_RESOURCE_EXHAUSTED:
			num = ERTooManyUserConnections
		case vtrpcpb.Code_FAILED_PRECONDITION:
			num = ERUnknownError
		case vtrpcpb.Code_ABORTED:
			num = ERQueryInterrupted
		case vtrpcpb.Code_OUT_OF_RANGE:
			num = ERUnknownError
		case vtrpcpb.Code_UNIMPLEMENTED:
			num = ERNotSupportedYet
		case vtrpcpb.Code_INTERNAL:
			num = ERUnknownError
		case vtrpcpb.Code_UNAVAILABLE:
			num = ERUnknownError
		case vtrpcpb.Code_DATA_LOSS:
			num = ERUnknownError
		}

		// Not found, build a generic SQLError.
		return &SQLError{
			Num:     num,
			State:   SSUnknownSQLState,
			Message: msg,
		}
	}

	num, err := strconv.Atoi(match[1])
	if err != nil {
		return &SQLError{
			Num:     ERUnknownError,
			State:   SSUnknownSQLState,
			Message: msg,
		}
	}

	serr := &SQLError{
		Num:     num,
		State:   match[2],
		Message: msg,
	}
	return serr
}
<|endoftext|>


<commit_before>package provision

import (
	"bytes"
	"fmt"
	"text/template"

	"github.com/docker/machine/libmachine/auth"
	"github.com/docker/machine/libmachine/drivers"
	"github.com/docker/machine/libmachine/engine"
	"github.com/docker/machine/libmachine/log"
	"github.com/docker/machine/libmachine/provision/pkgaction"
	"github.com/docker/machine/libmachine/swarm"
)

const (
	hostTmpl = `sudo tee /var/tmp/hostname.yml << EOF
#cloud-config

hostname: %s
EOF
`
)

func init() {
	Register("CoreOS", &RegisteredProvisioner{
		New: NewCoreOSProvisioner,
	})
}

func NewCoreOSProvisioner(d drivers.Driver) Provisioner {
	return &CoreOSProvisioner{
		NewSystemdProvisioner("coreos", d),
	}
}

type CoreOSProvisioner struct {
	SystemdProvisioner
}

func (provisioner *CoreOSProvisioner) String() string {
	return "coreOS"
}

func (provisioner *CoreOSProvisioner) SetHostname(hostname string) error {
	log.Debugf("SetHostname: %s", hostname)

	if _, err := provisioner.SSHCommand(fmt.Sprintf(hostTmpl, hostname)); err != nil {
		return err
	}

	if _, err := provisioner.SSHCommand("sudo systemctl start system-cloudinit@var-tmp-hostname.yml.service"); err != nil {
		return err
	}

	return nil
}

func (provisioner *CoreOSProvisioner) GenerateDockerOptions(dockerPort int) (*DockerOptions, error) {
	var (
		engineCfg bytes.Buffer
	)

	driverNameLabel := fmt.Sprintf("provider=%s", provisioner.Driver.DriverName())
	provisioner.EngineOptions.Labels = append(provisioner.EngineOptions.Labels, driverNameLabel)

	engineConfigTmpl := `[Unit]
Description=Docker Socket for the API
After=docker.socket early-docker.target network.target
Requires=docker.socket early-docker.target

[Service]
Environment=TMPDIR=/var/tmp
EnvironmentFile=-/run/flannel_docker_opts.env
MountFlags=slave
LimitNOFILE=1048576
LimitNPROC=1048576
ExecStart=/usr/lib/coreos/dockerd --daemon --host=unix:///var/run/docker.sock --host=tcp://0.0.0.0:{{.DockerPort}} --tlsverify --tlscacert {{.AuthOptions.CaCertRemotePath}} --tlscert {{.AuthOptions.ServerCertRemotePath}} --tlskey {{.AuthOptions.ServerKeyRemotePath}}{{ range .EngineOptions.Labels }} --label {{.}}{{ end }}{{ range .EngineOptions.InsecureRegistry }} --insecure-registry {{.}}{{ end }}{{ range .EngineOptions.RegistryMirror }} --registry-mirror {{.}}{{ end }}{{ range .EngineOptions.ArbitraryFlags }} --{{.}}{{ end }} \$DOCKER_OPTS \$DOCKER_OPT_BIP \$DOCKER_OPT_MTU \$DOCKER_OPT_IPMASQ

[Install]
WantedBy=multi-user.target
`

	t, err := template.New("engineConfig").Parse(engineConfigTmpl)
	if err != nil {
		return nil, err
	}

	engineConfigContext := EngineConfigContext{
		DockerPort:    dockerPort,
		AuthOptions:   provisioner.AuthOptions,
		EngineOptions: provisioner.EngineOptions,
	}

	t.Execute(&engineCfg, engineConfigContext)

	return &DockerOptions{
		EngineOptions:     engineCfg.String(),
		EngineOptionsPath: provisioner.DaemonOptionsFile,
	}, nil
}

func (provisioner *CoreOSProvisioner) Package(name string, action pkgaction.PackageAction) error {
	return nil
}

func (provisioner *CoreOSProvisioner) Provision(swarmOptions swarm.Options, authOptions auth.Options, engineOptions engine.Options) error {
	provisioner.SwarmOptions = swarmOptions
	provisioner.AuthOptions = authOptions
	provisioner.EngineOptions = engineOptions

	if err := provisioner.SetHostname(provisioner.Driver.GetMachineName()); err != nil {
		return err
	}

	if err := makeDockerOptionsDir(provisioner); err != nil {
		return err
	}

	log.Debugf("Preparing certificates")
	provisioner.AuthOptions = setRemoteAuthOptions(provisioner)

	log.Debugf("Setting up certificates")
	if err := ConfigureAuth(provisioner); err != nil {
		return err
	}

	log.Debug("Configuring swarm")
	if err := configureSwarm(provisioner, swarmOptions, provisioner.AuthOptions); err != nil {
		return err
	}

	return nil
}
<commit_msg>Update CoreOS provisioner to use 'docker daemon'<commit_after>package provision

import (
	"bytes"
	"fmt"
	"text/template"

	"github.com/docker/machine/libmachine/auth"
	"github.com/docker/machine/libmachine/drivers"
	"github.com/docker/machine/libmachine/engine"
	"github.com/docker/machine/libmachine/log"
	"github.com/docker/machine/libmachine/provision/pkgaction"
	"github.com/docker/machine/libmachine/swarm"
)

const (
	hostTmpl = `sudo tee /var/tmp/hostname.yml << EOF
#cloud-config

hostname: %s
EOF
`
)

func init() {
	Register("CoreOS", &RegisteredProvisioner{
		New: NewCoreOSProvisioner,
	})
}

func NewCoreOSProvisioner(d drivers.Driver) Provisioner {
	return &CoreOSProvisioner{
		NewSystemdProvisioner("coreos", d),
	}
}

type CoreOSProvisioner struct {
	SystemdProvisioner
}

func (provisioner *CoreOSProvisioner) String() string {
	return "coreOS"
}

func (provisioner *CoreOSProvisioner) SetHostname(hostname string) error {
	log.Debugf("SetHostname: %s", hostname)

	if _, err := provisioner.SSHCommand(fmt.Sprintf(hostTmpl, hostname)); err != nil {
		return err
	}

	if _, err := provisioner.SSHCommand("sudo systemctl start system-cloudinit@var-tmp-hostname.yml.service"); err != nil {
		return err
	}

	return nil
}

func (provisioner *CoreOSProvisioner) GenerateDockerOptions(dockerPort int) (*DockerOptions, error) {
	var (
		engineCfg bytes.Buffer
	)

	driverNameLabel := fmt.Sprintf("provider=%s", provisioner.Driver.DriverName())
	provisioner.EngineOptions.Labels = append(provisioner.EngineOptions.Labels, driverNameLabel)

	engineConfigTmpl := `[Unit]
Description=Docker Socket for the API
After=docker.socket early-docker.target network.target
Requires=docker.socket early-docker.target

[Service]
Environment=TMPDIR=/var/tmp
EnvironmentFile=-/run/flannel_docker_opts.env
MountFlags=slave
LimitNOFILE=1048576
LimitNPROC=1048576
ExecStart=/usr/lib/coreos/dockerd daemon --host=unix:///var/run/docker.sock --host=tcp://0.0.0.0:{{.DockerPort}} --tlsverify --tlscacert {{.AuthOptions.CaCertRemotePath}} --tlscert {{.AuthOptions.ServerCertRemotePath}} --tlskey {{.AuthOptions.ServerKeyRemotePath}}{{ range .EngineOptions.Labels }} --label {{.}}{{ end }}{{ range .EngineOptions.InsecureRegistry }} --insecure-registry {{.}}{{ end }}{{ range .EngineOptions.RegistryMirror }} --registry-mirror {{.}}{{ end }}{{ range .EngineOptions.ArbitraryFlags }} --{{.}}{{ end }} \$DOCKER_OPTS \$DOCKER_OPT_BIP \$DOCKER_OPT_MTU \$DOCKER_OPT_IPMASQ

[Install]
WantedBy=multi-user.target
`

	t, err := template.New("engineConfig").Parse(engineConfigTmpl)
	if err != nil {
		return nil, err
	}

	engineConfigContext := EngineConfigContext{
		DockerPort:    dockerPort,
		AuthOptions:   provisioner.AuthOptions,
		EngineOptions: provisioner.EngineOptions,
	}

	t.Execute(&engineCfg, engineConfigContext)

	return &DockerOptions{
		EngineOptions:     engineCfg.String(),
		EngineOptionsPath: provisioner.DaemonOptionsFile,
	}, nil
}

func (provisioner *CoreOSProvisioner) Package(name string, action pkgaction.PackageAction) error {
	return nil
}

func (provisioner *CoreOSProvisioner) Provision(swarmOptions swarm.Options, authOptions auth.Options, engineOptions engine.Options) error {
	provisioner.SwarmOptions = swarmOptions
	provisioner.AuthOptions = authOptions
	provisioner.EngineOptions = engineOptions

	if err := provisioner.SetHostname(provisioner.Driver.GetMachineName()); err != nil {
		return err
	}

	if err := makeDockerOptionsDir(provisioner); err != nil {
		return err
	}

	log.Debugf("Preparing certificates")
	provisioner.AuthOptions = setRemoteAuthOptions(provisioner)

	log.Debugf("Setting up certificates")
	if err := ConfigureAuth(provisioner); err != nil {
		return err
	}

	log.Debug("Configuring swarm")
	if err := configureSwarm(provisioner, swarmOptions, provisioner.AuthOptions); err != nil {
		return err
	}

	return nil
}
<|endoftext|>


<commit_before>package provision

import (
	"bytes"
	"fmt"
	"text/template"

	"github.com/docker/machine/drivers"
	"github.com/docker/machine/libmachine/auth"
	"github.com/docker/machine/libmachine/engine"
	"github.com/docker/machine/libmachine/provision/pkgaction"
	"github.com/docker/machine/libmachine/swarm"
	"github.com/docker/machine/log"
	"github.com/docker/machine/utils"
)

func init() {
	Register("Debian", &RegisteredProvisioner{
		New: NewDebianProvisioner,
	})
}

func NewDebianProvisioner(d drivers.Driver) Provisioner {
	return &DebianProvisioner{
		GenericProvisioner{
			DockerOptionsDir:  "/etc/docker",
			DaemonOptionsFile: "/etc/systemd/system/docker.service",
			OsReleaseId:       "debian",
			Packages: []string{
				"curl",
			},
			Driver: d,
		},
	}
}

type DebianProvisioner struct {
	GenericProvisioner
}

func (provisioner *DebianProvisioner) Service(name string, action pkgaction.ServiceAction) error {
	// daemon-reload to catch config updates; systemd -- ugh
	if _, err := provisioner.SSHCommand("sudo systemctl daemon-reload"); err != nil {
		return err
	}

	command := fmt.Sprintf("sudo systemctl %s %s", action.String(), name)

	if _, err := provisioner.SSHCommand(command); err != nil {
		return err
	}

	return nil
}

func (provisioner *DebianProvisioner) Package(name string, action pkgaction.PackageAction) error {
	var packageAction string

	updateMetadata := true

	switch action {
	case pkgaction.Install:
		packageAction = "install"
	case pkgaction.Remove:
		packageAction = "remove"
		updateMetadata = false
	case pkgaction.Upgrade:
		packageAction = "upgrade"
	}

	switch name {
	case "docker":
		name = "lxc-docker"
	}

	if updateMetadata {
		if _, err := provisioner.SSHCommand("sudo apt-get update"); err != nil {
			return err
		}
	}

	command := fmt.Sprintf("DEBIAN_FRONTEND=noninteractive sudo -E apt-get %s -y  %s", packageAction, name)

	log.Debugf("package: action=%s name=%s", action.String(), name)

	if _, err := provisioner.SSHCommand(command); err != nil {
		return err
	}

	return nil
}

func (provisioner *DebianProvisioner) dockerDaemonResponding() bool {
	if _, err := provisioner.SSHCommand("sudo docker version"); err != nil {
		log.Warnf("Error getting SSH command to check if the daemon is up: %s", err)
		return false
	}

	// The daemon is up if the command worked.  Carry on.
	return true
}

func (provisioner *DebianProvisioner) Provision(swarmOptions swarm.SwarmOptions, authOptions auth.AuthOptions, engineOptions engine.EngineOptions) error {
	provisioner.SwarmOptions = swarmOptions
	provisioner.AuthOptions = authOptions
	provisioner.EngineOptions = engineOptions

	if provisioner.EngineOptions.StorageDriver == "" {
		provisioner.EngineOptions.StorageDriver = "aufs"
	}

	// HACK: since debian does not come with sudo by default we install
	log.Debug("installing sudo")
	if _, err := provisioner.SSHCommand("if ! type sudo; then apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y sudo; fi"); err != nil {
		return err
	}

	log.Debug("setting hostname")
	if err := provisioner.SetHostname(provisioner.Driver.GetMachineName()); err != nil {
		return err
	}

	log.Debug("installing base packages")
	for _, pkg := range provisioner.Packages {
		if err := provisioner.Package(pkg, pkgaction.Install); err != nil {
			return err
		}
	}

	log.Debug("installing docker")
	if err := installDockerGeneric(provisioner); err != nil {
		return err
	}

	log.Debug("waiting for docker daemon")
	if err := utils.WaitFor(provisioner.dockerDaemonResponding); err != nil {
		return err
	}

	provisioner.AuthOptions = setRemoteAuthOptions(provisioner)

	log.Debug("configuring auth")
	if err := ConfigureAuth(provisioner); err != nil {
		return err
	}

	log.Debug("configuring swarm")
	if err := configureSwarm(provisioner, swarmOptions); err != nil {
		return err
	}

	// enable in systemd
	log.Debug("enabling docker in systemd")
	if err := provisioner.Service("docker", pkgaction.Enable); err != nil {
		return err
	}

	return nil
}

func (provisioner *DebianProvisioner) GenerateDockerOptions(dockerPort int) (*DockerOptions, error) {
	var (
		engineCfg bytes.Buffer
	)

	driverNameLabel := fmt.Sprintf("provider=%s", provisioner.Driver.DriverName())
	provisioner.EngineOptions.Labels = append(provisioner.EngineOptions.Labels, driverNameLabel)

	engineConfigTmpl := `[Service]
ExecStart=/usr/bin/docker -d -H tcp://0.0.0.0:{{.DockerPort}} -H unix:///var/run/docker.sock --storage-driver {{.EngineOptions.StorageDriver}} --tlsverify --tlscacert {{.AuthOptions.CaCertRemotePath}} --tlscert {{.AuthOptions.ServerCertRemotePath}} --tlskey {{.AuthOptions.ServerKeyRemotePath}} {{ range .EngineOptions.Labels }}--label {{.}} {{ end }}{{ range .EngineOptions.InsecureRegistry }}--insecure-registry {{.}} {{ end }}{{ range .EngineOptions.RegistryMirror }}--registry-mirror {{.}} {{ end }}{{ range .EngineOptions.ArbitraryFlags }}--{{.}} {{ end }}
MountFlags=slave
LimitNOFILE=1048576
LimitNPROC=1048576
LimitCORE=infinity
`
	t, err := template.New("engineConfig").Parse(engineConfigTmpl)
	if err != nil {
		return nil, err
	}

	engineConfigContext := EngineConfigContext{
		DockerPort:    dockerPort,
		AuthOptions:   provisioner.AuthOptions,
		EngineOptions: provisioner.EngineOptions,
	}

	t.Execute(&engineCfg, engineConfigContext)

	return &DockerOptions{
		EngineOptions:     engineCfg.String(),
		EngineOptionsPath: provisioner.DaemonOptionsFile,
	}, nil
}
<commit_msg>debian: rebase and fix configureSwarm<commit_after>package provision

import (
	"bytes"
	"fmt"
	"text/template"

	"github.com/docker/machine/drivers"
	"github.com/docker/machine/libmachine/auth"
	"github.com/docker/machine/libmachine/engine"
	"github.com/docker/machine/libmachine/provision/pkgaction"
	"github.com/docker/machine/libmachine/swarm"
	"github.com/docker/machine/log"
	"github.com/docker/machine/utils"
)

func init() {
	Register("Debian", &RegisteredProvisioner{
		New: NewDebianProvisioner,
	})
}

func NewDebianProvisioner(d drivers.Driver) Provisioner {
	return &DebianProvisioner{
		GenericProvisioner{
			DockerOptionsDir:  "/etc/docker",
			DaemonOptionsFile: "/etc/systemd/system/docker.service",
			OsReleaseId:       "debian",
			Packages: []string{
				"curl",
			},
			Driver: d,
		},
	}
}

type DebianProvisioner struct {
	GenericProvisioner
}

func (provisioner *DebianProvisioner) Service(name string, action pkgaction.ServiceAction) error {
	// daemon-reload to catch config updates; systemd -- ugh
	if _, err := provisioner.SSHCommand("sudo systemctl daemon-reload"); err != nil {
		return err
	}

	command := fmt.Sprintf("sudo systemctl %s %s", action.String(), name)

	if _, err := provisioner.SSHCommand(command); err != nil {
		return err
	}

	return nil
}

func (provisioner *DebianProvisioner) Package(name string, action pkgaction.PackageAction) error {
	var packageAction string

	updateMetadata := true

	switch action {
	case pkgaction.Install:
		packageAction = "install"
	case pkgaction.Remove:
		packageAction = "remove"
		updateMetadata = false
	case pkgaction.Upgrade:
		packageAction = "upgrade"
	}

	switch name {
	case "docker":
		name = "lxc-docker"
	}

	if updateMetadata {
		if _, err := provisioner.SSHCommand("sudo apt-get update"); err != nil {
			return err
		}
	}

	command := fmt.Sprintf("DEBIAN_FRONTEND=noninteractive sudo -E apt-get %s -y  %s", packageAction, name)

	log.Debugf("package: action=%s name=%s", action.String(), name)

	if _, err := provisioner.SSHCommand(command); err != nil {
		return err
	}

	return nil
}

func (provisioner *DebianProvisioner) dockerDaemonResponding() bool {
	if _, err := provisioner.SSHCommand("sudo docker version"); err != nil {
		log.Warnf("Error getting SSH command to check if the daemon is up: %s", err)
		return false
	}

	// The daemon is up if the command worked.  Carry on.
	return true
}

func (provisioner *DebianProvisioner) Provision(swarmOptions swarm.SwarmOptions, authOptions auth.AuthOptions, engineOptions engine.EngineOptions) error {
	provisioner.SwarmOptions = swarmOptions
	provisioner.AuthOptions = authOptions
	provisioner.EngineOptions = engineOptions

	if provisioner.EngineOptions.StorageDriver == "" {
		provisioner.EngineOptions.StorageDriver = "aufs"
	}

	// HACK: since debian does not come with sudo by default we install
	log.Debug("installing sudo")
	if _, err := provisioner.SSHCommand("if ! type sudo; then apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y sudo; fi"); err != nil {
		return err
	}

	log.Debug("setting hostname")
	if err := provisioner.SetHostname(provisioner.Driver.GetMachineName()); err != nil {
		return err
	}

	log.Debug("installing base packages")
	for _, pkg := range provisioner.Packages {
		if err := provisioner.Package(pkg, pkgaction.Install); err != nil {
			return err
		}
	}

	log.Debug("installing docker")
	if err := installDockerGeneric(provisioner); err != nil {
		return err
	}

	log.Debug("waiting for docker daemon")
	if err := utils.WaitFor(provisioner.dockerDaemonResponding); err != nil {
		return err
	}

	provisioner.AuthOptions = setRemoteAuthOptions(provisioner)

	log.Debug("configuring auth")
	if err := ConfigureAuth(provisioner); err != nil {
		return err
	}

	log.Debug("configuring swarm")
	if err := configureSwarm(provisioner, swarmOptions, provisioner.AuthOptions); err != nil {
		return err
	}

	// enable in systemd
	log.Debug("enabling docker in systemd")
	if err := provisioner.Service("docker", pkgaction.Enable); err != nil {
		return err
	}

	return nil
}

func (provisioner *DebianProvisioner) GenerateDockerOptions(dockerPort int) (*DockerOptions, error) {
	var (
		engineCfg bytes.Buffer
	)

	driverNameLabel := fmt.Sprintf("provider=%s", provisioner.Driver.DriverName())
	provisioner.EngineOptions.Labels = append(provisioner.EngineOptions.Labels, driverNameLabel)

	engineConfigTmpl := `[Service]
ExecStart=/usr/bin/docker -d -H tcp://0.0.0.0:{{.DockerPort}} -H unix:///var/run/docker.sock --storage-driver {{.EngineOptions.StorageDriver}} --tlsverify --tlscacert {{.AuthOptions.CaCertRemotePath}} --tlscert {{.AuthOptions.ServerCertRemotePath}} --tlskey {{.AuthOptions.ServerKeyRemotePath}} {{ range .EngineOptions.Labels }}--label {{.}} {{ end }}{{ range .EngineOptions.InsecureRegistry }}--insecure-registry {{.}} {{ end }}{{ range .EngineOptions.RegistryMirror }}--registry-mirror {{.}} {{ end }}{{ range .EngineOptions.ArbitraryFlags }}--{{.}} {{ end }}
MountFlags=slave
LimitNOFILE=1048576
LimitNPROC=1048576
LimitCORE=infinity
`
	t, err := template.New("engineConfig").Parse(engineConfigTmpl)
	if err != nil {
		return nil, err
	}

	engineConfigContext := EngineConfigContext{
		DockerPort:    dockerPort,
		AuthOptions:   provisioner.AuthOptions,
		EngineOptions: provisioner.EngineOptions,
	}

	t.Execute(&engineCfg, engineConfigContext)

	return &DockerOptions{
		EngineOptions:     engineCfg.String(),
		EngineOptionsPath: provisioner.DaemonOptionsFile,
	}, nil
}
<|endoftext|>


<commit_before>package provision

import (
	"github.com/docker/machine/drivers"
)

const (
	// TODO: eventually the RPM install process will be integrated
	// into the get.docker.com install script; for now
	// we install via vendored RPMs
	dockerFedoraRPMPath = "https://docker-mcn.s3.amazonaws.com/public/fedora/rpms/docker-engine-1.6.1-0.0.20150511.171646.git1b47f9f.fc21.x86_64.rpm"
)

func init() {
	Register("Fedora", &RegisteredProvisioner{
		New: NewFedoraProvisioner,
	})
}

func NewFedoraProvisioner(d drivers.Driver) Provisioner {
	g := GenericProvisioner{
		DockerOptionsDir:  "/etc/docker",
		DaemonOptionsFile: "/etc/systemd/system/docker.service",
		OsReleaseId:       "fedora",
		Packages: []string{
			"curl",
			"yum-utils",
		},
		Driver: d,
	}
	p := &FedoraProvisioner{
		RedHatProvisioner{
			g,
			dockerFedoraRPMPath,
		},
	}
	return p
}

type FedoraProvisioner struct {
	RedHatProvisioner
}
<commit_msg>fedora: remove configure repo<commit_after>package provision

import (
	"github.com/docker/machine/drivers"
)

const (
	// TODO: eventually the RPM install process will be integrated
	// into the get.docker.com install script; for now
	// we install via vendored RPMs
	dockerFedoraRPMPath = "https://docker-mcn.s3.amazonaws.com/public/fedora/rpms/docker-engine-1.6.1-0.0.20150511.171646.git1b47f9f.fc21.x86_64.rpm"
)

func init() {
	Register("Fedora", &RegisteredProvisioner{
		New: NewFedoraProvisioner,
	})
}

func NewFedoraProvisioner(d drivers.Driver) Provisioner {
	g := GenericProvisioner{
		DockerOptionsDir:  "/etc/docker",
		DaemonOptionsFile: "/etc/systemd/system/docker.service",
		OsReleaseId:       "fedora",
		Packages:          []string{},
		Driver:            d,
	}
	p := &FedoraProvisioner{
		RedHatProvisioner{
			g,
			dockerFedoraRPMPath,
		},
	}
	return p
}

type FedoraProvisioner struct {
	RedHatProvisioner
}
<|endoftext|>


<commit_before>package gopherjs_http

import (
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"os"
	"time"

	"code.google.com/p/go.net/html"
	"code.google.com/p/go.net/html/atom"
	"github.com/shurcooL/go/pipe_util"
	"gopkg.in/pipe.v2"
)

// HtmlFile returns a handler that serves the given .html file, with the "text/go" scripts compiled to JavaScript via GopherJS.
func HtmlFile(name string) http.Handler {
	return &htmlFile{name: name}
}

type htmlFile struct {
	name string
}

func (this *htmlFile) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	file, err := os.Open(this.name)
	if err != nil {
		panic(err)
	}
	defer file.Close()

	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	ProcessHtml(file).WriteTo(w)
}

// TODO: Write into writer, no need for buffer (unless want to be able to abort on error?). Or, alternatively, parse html and serve minified version?
func ProcessHtml(r io.Reader) *bytes.Buffer {
	insideTextGo := false
	tokenizer := html.NewTokenizer(r)
	var buf bytes.Buffer

	for {
		if tokenizer.Next() == html.ErrorToken {
			err := tokenizer.Err()
			if err == io.EOF {
				return &buf
			}

			return &bytes.Buffer{}
		}

		token := tokenizer.Token()
		switch token.Type {
		case html.DoctypeToken:
			buf.WriteString(token.String())
		case html.CommentToken:
			buf.WriteString(token.String())
		case html.StartTagToken:
			if token.DataAtom == atom.Script && getType(token.Attr) == "text/go" {
				insideTextGo = true

				buf.WriteString(`<script type="text/javascript">`)

				if src := getSrc(token.Attr); src != "" {
					if data, err := ioutil.ReadFile(src); err == nil {
						buf.WriteString(goToJs(string(data)))
					}
				}
			} else {
				buf.WriteString(token.String())
			}
		case html.EndTagToken:
			if token.DataAtom == atom.Script && insideTextGo {
				insideTextGo = false
			}
			buf.WriteString(token.String())
		case html.SelfClosingTagToken:
			// TODO: Support <script type="text/go" src="..." />.
			buf.WriteString(token.String())
		case html.TextToken:
			if insideTextGo {
				buf.WriteString(goToJs(token.Data))
			} else {
				buf.WriteString(token.Data)
			}
		default:
			panic("unknown token type")
		}
	}
}

func getType(attrs []html.Attribute) string {
	for _, attr := range attrs {
		if attr.Key == "type" {
			return attr.Val
		}
	}
	return ""
}

func getSrc(attrs []html.Attribute) string {
	for _, attr := range attrs {
		if attr.Key == "src" {
			return attr.Val
		}
	}
	return ""
}

func goToJs(goCode string) (jsCode string) {
	started := time.Now()
	defer func() { fmt.Println("goToJs taken:", time.Since(started)) }()

	// TODO: Don't shell out, and avoid having to write/read temporary files, instead
	//       use http://godoc.org/github.com/gopherjs/gopherjs/compiler directly, etc.
	p := pipe.Script(
		pipe.Line(
			pipe.Print(goCode),
			pipe.WriteFile("tmp.go", 0666),
		),
		pipe.Exec("gopherjs", "build", "tmp.go"),
		pipe.ReadFile("tmp.js"),
	)

	// Use a temporary dir.
	tempDir, err := ioutil.TempDir("", "gopherjs_")
	if err != nil {
		panic(err)
	}
	defer func() {
		err := os.RemoveAll(tempDir)
		if err != nil {
			fmt.Fprintln(os.Stderr, "warning: error removing temp dir:", err)
		}
	}()

	stdout, stderr, err := pipe_util.DividedOutputDir(p, tempDir)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Stderr.Write(stderr)
	}

	// TODO: Serve the .map too, somehow?
	stdout = bytes.Replace(stdout, []byte("//# sourceMappingURL=tmp.js.map\n"), []byte("\n"), -1)

	return string(stdout)
}
<commit_msg>gopherjs_http: Don't shell out, use gopherjslib to compile in-process.<commit_after>package gopherjs_http

import (
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"os"
	"strings"
	"time"

	"code.google.com/p/go.net/html"
	"code.google.com/p/go.net/html/atom"
	"github.com/go-on/gopherjslib"
)

// HtmlFile returns a handler that serves the given .html file, with the "text/go" scripts compiled to JavaScript via GopherJS.
func HtmlFile(name string) http.Handler {
	return &htmlFile{name: name}
}

type htmlFile struct {
	name string
}

func (this *htmlFile) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	file, err := os.Open(this.name)
	if err != nil {
		panic(err)
	}
	defer file.Close()

	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	ProcessHtml(file).WriteTo(w)
}

// TODO: Write into writer, no need for buffer (unless want to be able to abort on error?). Or, alternatively, parse html and serve minified version?
func ProcessHtml(r io.Reader) *bytes.Buffer {
	insideTextGo := false
	tokenizer := html.NewTokenizer(r)
	var buf bytes.Buffer

	for {
		if tokenizer.Next() == html.ErrorToken {
			err := tokenizer.Err()
			if err == io.EOF {
				return &buf
			}

			return &bytes.Buffer{}
		}

		token := tokenizer.Token()
		switch token.Type {
		case html.DoctypeToken:
			buf.WriteString(token.String())
		case html.CommentToken:
			buf.WriteString(token.String())
		case html.StartTagToken:
			if token.DataAtom == atom.Script && getType(token.Attr) == "text/go" {
				insideTextGo = true

				buf.WriteString(`<script type="text/javascript">`)

				if src := getSrc(token.Attr); src != "" {
					if data, err := ioutil.ReadFile(src); err == nil {
						buf.WriteString(goToJs(string(data)))
					}
				}
			} else {
				buf.WriteString(token.String())
			}
		case html.EndTagToken:
			if token.DataAtom == atom.Script && insideTextGo {
				insideTextGo = false
			}
			buf.WriteString(token.String())
		case html.SelfClosingTagToken:
			// TODO: Support <script type="text/go" src="..." />.
			buf.WriteString(token.String())
		case html.TextToken:
			if insideTextGo {
				buf.WriteString(goToJs(token.Data))
			} else {
				buf.WriteString(token.Data)
			}
		default:
			panic("unknown token type")
		}
	}
}

func getType(attrs []html.Attribute) string {
	for _, attr := range attrs {
		if attr.Key == "type" {
			return attr.Val
		}
	}
	return ""
}

func getSrc(attrs []html.Attribute) string {
	for _, attr := range attrs {
		if attr.Key == "src" {
			return attr.Val
		}
	}
	return ""
}

func goToJs(goCode string) (jsCode string) {
	started := time.Now()
	defer func() { fmt.Println("goToJs taken:", time.Since(started)) }()

	code := strings.NewReader(goCode)

	var out bytes.Buffer
	err := gopherjslib.Build(code, &out, nil)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return goCode
	}

	return out.String()
}
<|endoftext|>


<commit_before>package commands

import (
	"fmt"
	"io/ioutil"
	"errors"
	"time"
	"strconv"
	//"github.com/shirou/gopsutil/mem"
	"github.com/synw/microb/libmicrob/db"
	"github.com/synw/microb/libmicrob/datatypes"
	"github.com/synw/microb/libmicrob/datatypes/encoding"
	"github.com/synw/microb/libmicrob/events"
	"github.com/synw/microb/libmicrob/events/format"
	"github.com/synw/microb/libmicrob/state"
	"github.com/synw/microb/libmicrob/state/mutate"
	"github.com/synw/microb/libmicrob/commands/methods"
	"github.com/synw/microb/libmicrob/http_handlers"

)

func Run(command *datatypes.Command) {
	c := make(chan *datatypes.Command)
	go runCommand(command, c)
	select {
		case cmd := <- c:
			close(c)
			// process command results
			HandleCommandFeedback(cmd)
	}
}

func HandleCommandFeedback(command *datatypes.Command) {
	if command.Status == "error" {
		events.Error("command_execution", command.Error)
	}
	if state.Verbosity > 0 {
		events.PrintCommandReport(command)
	}
}

func GetCommandFromPayload(message *datatypes.WsIncomingMessage, from string) *datatypes.Command {
	data := message.Data
	name := data["name"].(string)
	reason := ""
	if data["from"] != nil {
		from = data["from"].(string)
	}
	var command *datatypes.Command
	if data["reason"].(string) != "" {
		reason = data["reason"].(string)
	}
	if data["args"] != nil {
		args := data["args"].([]interface{})
		command = NewWithArgs(name, from, reason, args)
	} else {
		command = New(name, from, reason)
	}
	return command
}

// constructors
func New(name string, from string, reason string) *datatypes.Command {
	now := time.Now()
	var args[]interface{}
	var rv []interface{}
	id := encoding.GenerateId()
	cmd := &datatypes.Command{id, name, from, reason, now, args, "pending", nil, rv}
	return cmd
}

func NewWithArgs(name string, from string, reason string, args []interface{}) *datatypes.Command {
	now := time.Now()
	var rv []interface{}
	id := encoding.GenerateId()
	cmd := &datatypes.Command{id, name, from, reason, now, args, "pending", nil, rv}
	return cmd
}

// commands
func reparseTemplates() error {
	http_handlers.ReparseTemplates()
	return nil
}

func updateRoutes() error {
	// hit db
	routestab, err := db.GetRoutes()
	if err != nil {
		events.Error("commands.updateRoutes", err)
	}
	var routestr string
	var route string
	for i := range(routestab) {
		route = routestab[i]
		routestr = routestr+fmt.Sprintf("page('%s', function(ctx, next) { loadPage('/x%s') } );", route, route)
	}
    str := []byte(routestr)
    err = ioutil.WriteFile("templates/routes.js", str, 0644)
    if err != nil {
        events.Error("commands.updateRoutes", err)
        return err
    }
    // auto reparse templates if the routes change
   	cmd := New("reparse_templates", "commands.updateRoutes()", "Routes update")
    go Run(cmd)
 	return nil
}

func dbStatus() (map[string]interface{}, error) {
	status, err := db.ReportStatus()
	if err != nil {
		return status, err
	}
	return status, nil
}
/*
func sysInfo(*datatypes.Command) *datatypes.Command {
	v, _ := mem.VirtualMemory()

}*/

// utilities
func handleCommandError(command *datatypes.Command, err error, c chan *datatypes.Command) {
	if err != nil {
		command.Status = "error"
		command.Error = err
		c <- command
	}
	return
}
/*
func NewWithData(name string, from string, reason string, data []interface{}) *datatypes.Command {
	now := time.Now()
	cmd := &datatypes.Command{name, from, reason, now, "pending", nil, data}
	return cmd
}
*/
// handlers
func runCommand(command *datatypes.Command, c chan *datatypes.Command) {
	events.New("command", "RunCommand()", command.Name)
	if  commands_methods.IsValid(command) == false {
		msg := "Unknown command: "+command.Name
		err := errors.New(msg)
		handleCommandError(command, err, c)
		return
	}
	if (command.Name == "update_routes") {
		// UPDATE ROUTES
		err := updateRoutes()
		if err != nil {
			handleCommandError(command, err, c)
			return
		} else {
			go updateRoutes()
			command.Status = "success"
		}
	} else if (command.Name == "reparse_templates") {
		// REPARSE TEMPLATES
		err := reparseTemplates()
		if err != nil {
			handleCommandError(command, err, c)
			return
		} else {
			go reparseTemplates()
			command.Status = "success"
		}
	} else if (command.Name == "ping") {
		// PING
		command.ReturnValues = append(command.ReturnValues, "PONG")
		command.Status = "success"
	} else if (command.Name == "db_status") {
		status, err := db.ReportStatus()
		if err != nil {
			handleCommandError(command, err, c)
			return
		}
		line := "------------------------------"
		version := "\n "+line+"\n - Version: "+status["version"].(string)
		cache_size_mb := "- Cache size: "+strconv.FormatFloat(status["cache_size_mb"].(float64), 'f', 2, 64)+" Mb"
		time_started := line+"\nStarted since "+format.FormatTime(status["time_started"].(time.Time))
		command.ReturnValues = append(command.ReturnValues, version)
		command.ReturnValues = append(command.ReturnValues, cache_size_mb)
		command.ReturnValues = append(command.ReturnValues, time_started)
		command.Status = "success"
	} else if (command.Name == "routes") {
		// ROUTES
		routes, err := db.GetRoutes()
		if err != nil {
			handleCommandError(command, err, c)
			return
		}
		var rvs []interface{}
		for _, route := range(routes) {
			rvs = append(rvs, route)
		}
		command.ReturnValues = rvs
		command.Status = "success"
	} else if command.Name == "state" {
		// STATE
		msg := state.FormatState()
		var rvs []interface{}
		rvs = append(rvs, msg)
		command.ReturnValues = rvs
		command.Status = "success"
	} else if command.Name == "set" {
		// SET
		num_args := len(command.Args)
		if  num_args == 0 {
			msg := "Command set incoming from "+command.From+" with no arguments"
			events.ErrMsg("commands.run", msg)
		} else if num_args == 1 {
			msg := "Command set incoming from "+command.From+" with only one argument: "+command.Args[0].(string)
			events.ErrMsg("commands.run", msg)
		} else if num_args > 2 {
			var arguments string
			for _, a := range(command.Args) {
				arguments = arguments+" "+a.(string)
			}
			msg := "Command set incoming from "+command.From+" with more than two arguments:"+arguments
			events.ErrMsg("commands.run", msg)
		} else {
			cmd := command.Args[0]
			arg := command.Args[1]
			if cmd == "verbosity" {
				msg := mutate.Verbosity(arg.(string))
				var rvs []interface{}
				rvs = append(rvs, msg)
				command.ReturnValues = rvs
				command.Status = "success"
			} else if cmd == "debug" {
				msg, err := mutate.Debug(arg.(string))
				if err != nil {
					command.Status = "error"
					command.Error = err
				} else {
					var rvs []interface{}
					rvs = append(rvs, msg)
					command.Status = "success"
				}
			}
		}
	}
	/*else if (command.Name == "db_list") {
		for role, db := range metadata.GetDatabasesAndRoles() {
			msg := database.Name
			command.ReturnValues = append(command.ReturnValues, )
		}
		command.Status = "success"
	}*/
	c <- command
	/*else if (command.Name == "syncdb") {
		go db.ImportPagesFromMainDb(command.Values.(string))
	}*/
	// save in db
	//go db.SaveCommand(command)
	return
}
<commit_msg>State db command<commit_after>package commands

import (
	"fmt"
	"io/ioutil"
	"errors"
	"time"
	"strconv"
	//"github.com/shirou/gopsutil/mem"
	"github.com/synw/microb/libmicrob/db"
	"github.com/synw/microb/libmicrob/datatypes"
	"github.com/synw/microb/libmicrob/datatypes/encoding"
	"github.com/synw/microb/libmicrob/events"
	"github.com/synw/microb/libmicrob/events/format"
	"github.com/synw/microb/libmicrob/state"
	"github.com/synw/microb/libmicrob/state/mutate"
	"github.com/synw/microb/libmicrob/commands/methods"
	"github.com/synw/microb/libmicrob/http_handlers"

)

func Run(command *datatypes.Command) {
	c := make(chan *datatypes.Command)
	go runCommand(command, c)
	select {
		case cmd := <- c:
			close(c)
			// process command results
			HandleCommandFeedback(cmd)
	}
}

func HandleCommandFeedback(command *datatypes.Command) {
	if command.Status == "error" {
		events.Error("command_execution", command.Error)
	}
	if state.Verbosity > 0 {
		events.PrintCommandReport(command)
	}
}

func GetCommandFromPayload(message *datatypes.WsIncomingMessage, from string) *datatypes.Command {
	data := message.Data
	name := data["name"].(string)
	reason := ""
	if data["from"] != nil {
		from = data["from"].(string)
	}
	var command *datatypes.Command
	if data["reason"].(string) != "" {
		reason = data["reason"].(string)
	}
	if data["args"] != nil {
		args := data["args"].([]interface{})
		command = NewWithArgs(name, from, reason, args)
	} else {
		command = New(name, from, reason)
	}
	return command
}

// constructors
func New(name string, from string, reason string) *datatypes.Command {
	now := time.Now()
	var args[]interface{}
	var rv []interface{}
	id := encoding.GenerateId()
	cmd := &datatypes.Command{id, name, from, reason, now, args, "pending", nil, rv}
	return cmd
}

func NewWithArgs(name string, from string, reason string, args []interface{}) *datatypes.Command {
	now := time.Now()
	var rv []interface{}
	id := encoding.GenerateId()
	cmd := &datatypes.Command{id, name, from, reason, now, args, "pending", nil, rv}
	return cmd
}

// commands
func reparseTemplates() error {
	http_handlers.ReparseTemplates()
	return nil
}

func updateRoutes() error {
	// hit db
	routestab, err := db.GetRoutes()
	if err != nil {
		events.Error("commands.updateRoutes", err)
	}
	var routestr string
	var route string
	for i := range(routestab) {
		route = routestab[i]
		routestr = routestr+fmt.Sprintf("page('%s', function(ctx, next) { loadPage('/x%s') } );", route, route)
	}
    str := []byte(routestr)
    err = ioutil.WriteFile("templates/routes.js", str, 0644)
    if err != nil {
        events.Error("commands.updateRoutes", err)
        return err
    }
    // auto reparse templates if the routes change
   	cmd := New("reparse_templates", "commands.updateRoutes()", "Routes update")
    go Run(cmd)
 	return nil
}

func dbStatus() (map[string]interface{}, error) {
	status, err := db.ReportStatus()
	if err != nil {
		return status, err
	}
	return status, nil
}
/*
func sysInfo(*datatypes.Command) *datatypes.Command {
	v, _ := mem.VirtualMemory()

}*/

// utilities
func handleCommandError(command *datatypes.Command, err error, c chan *datatypes.Command) {
	if err != nil {
		command.Status = "error"
		command.Error = err
		c <- command
	}
	return
}
/*
func NewWithData(name string, from string, reason string, data []interface{}) *datatypes.Command {
	now := time.Now()
	cmd := &datatypes.Command{name, from, reason, now, "pending", nil, data}
	return cmd
}
*/
// handlers
func runCommand(command *datatypes.Command, c chan *datatypes.Command) {
	events.New("command", "RunCommand()", command.Name)
	if  commands_methods.IsValid(command) == false {
		msg := "Unknown command: "+command.Name
		err := errors.New(msg)
		handleCommandError(command, err, c)
		return
	}
	if (command.Name == "update_routes") {
		// UPDATE ROUTES
		err := updateRoutes()
		if err != nil {
			handleCommandError(command, err, c)
			return
		} else {
			go updateRoutes()
			command.Status = "success"
		}
	} else if (command.Name == "reparse_templates") {
		// REPARSE TEMPLATES
		err := reparseTemplates()
		if err != nil {
			handleCommandError(command, err, c)
			return
		} else {
			go reparseTemplates()
			command.Status = "success"
		}
	} else if (command.Name == "ping") {
		// PING
		command.ReturnValues = append(command.ReturnValues, "PONG")
		command.Status = "success"
	} else if (command.Name == "db_status") {
		status, err := db.ReportStatus()
		if err != nil {
			handleCommandError(command, err, c)
			return
		}
		line := "------------------------------"
		version := "\n "+line+"\n - Version: "+status["version"].(string)
		cache_size_mb := "- Cache size: "+strconv.FormatFloat(status["cache_size_mb"].(float64), 'f', 2, 64)+" Mb"
		time_started := line+"\nStarted since "+format.FormatTime(status["time_started"].(time.Time))
		command.ReturnValues = append(command.ReturnValues, version)
		command.ReturnValues = append(command.ReturnValues, cache_size_mb)
		command.ReturnValues = append(command.ReturnValues, time_started)
		command.Status = "success"
	} else if (command.Name == "routes") {
		// ROUTES
		routes, err := db.GetRoutes()
		if err != nil {
			handleCommandError(command, err, c)
			return
		}
		var rvs []interface{}
		for _, route := range(routes) {
			rvs = append(rvs, route)
		}
		command.ReturnValues = rvs
		command.Status = "success"
	} else if command.Name == "state" {
		// STATE
		num_args := len(command.Args)
		if  num_args == 0 {
			msg := state.FormatState()
			var rvs []interface{}
			rvs = append(rvs, msg)
			command.ReturnValues = rvs
			command.Status = "success"
		} else if num_args > 1 {
			var rvs []interface{}
			msg := "Too many arguments to command state"
			rvs = append(rvs, msg)
			command.ReturnValues = rvs
			command.Status = "error"
		} else {
			if command.Args[0].(string) == "db" {
				// STATE DB
				var rvs []interface{}
				msg := "Database state:\n"
				msg = msg+" - Pages database is "+state.Server.PagesDb.Name+" ("+state.Server.PagesDb.Type+") at "+state.Server.PagesDb.Host+"\n"
				msg = msg+" - Hits database is "+state.Server.HitsDb.Name+" ("+state.Server.HitsDb.Type+") at "+state.Server.HitsDb.Host+"\n"
				msg = msg+" - Commands database is "+state.Server.CommandsDb.Name+" ("+state.Server.CommandsDb.Type+") at "+state.Server.CommandsDb.Host
				rvs = append(rvs, msg)
				command.ReturnValues = rvs
				command.Status = "success"
			}
		}
	} else if command.Name == "set" {
		// SET
		num_args := len(command.Args)
		if  num_args == 0 {
			msg := "Command set incoming from "+command.From+" with no arguments"
			events.ErrMsg("commands.run", msg)
		} else if num_args == 1 {
			msg := "Command set incoming from "+command.From+" with only one argument: "+command.Args[0].(string)
			events.ErrMsg("commands.run", msg)
		} else if num_args > 2 {
			var arguments string
			for _, a := range(command.Args) {
				arguments = arguments+" "+a.(string)
			}
			msg := "Command set incoming from "+command.From+" with more than two arguments:"+arguments
			events.ErrMsg("commands.run", msg)
		} else {
			cmd := command.Args[0]
			arg := command.Args[1]
			if cmd == "verbosity" {
				msg := mutate.Verbosity(arg.(string))
				var rvs []interface{}
				rvs = append(rvs, msg)
				command.ReturnValues = rvs
				command.Status = "success"
			} else if cmd == "debug" {
				msg, err := mutate.Debug(arg.(string))
				if err != nil {
					command.Status = "error"
					command.Error = err
				} else {
					var rvs []interface{}
					rvs = append(rvs, msg)
					command.Status = "success"
				}
			}
		}
	}
	/*else if (command.Name == "db_list") {
		for role, db := range metadata.GetDatabasesAndRoles() {
			msg := database.Name
			command.ReturnValues = append(command.ReturnValues, )
		}
		command.Status = "success"
	}*/
	c <- command
	/*else if (command.Name == "syncdb") {
		go db.ImportPagesFromMainDb(command.Values.(string))
	}*/
	// save in db
	//go db.SaveCommand(command)
	return
}
<|endoftext|>


<commit_before>package csv_test

import (
	"fmt"
	"simonwaldherr.de/go/golibs/csv"
	"sort"
)

var userdata string = `id;name;email
0;John Doe;jDoe@example.org
1;Jane Doe;jane.doe@example.com
2;Max Mustermann;m.mustermann@alpha.tld`

var userdata2 string = `id;name;email
0;John Doe;jDoe@example.org`

type kv struct {
	Key   int
	Value []string
}

func sorting(m map[int][]string) []kv {
	var ss []kv
	for k, v := range m {
		ss = append(ss, kv{k, v})
	}
	sort.Slice(ss, func(i, j int) bool {
		return ss[i].Value[1] > ss[j].Value[1]
	})
	return ss
}

func Example() {
	csvmap, _ := csv.LoadCSVfromString(userdata)
	sortedmap := sorting(csvmap)

	for _, user := range sortedmap {
		fmt.Println(string(user.Value[1]))
	}

	// Output: Max Mustermann
	// John Doe
	// Jane Doe
}

func Example_second() {
	csvmap, _ := csv.LoadCSVfromFile("./test.csv")
	sortedmap := sorting(csvmap)

	for _, user := range sortedmap {
		fmt.Println(string(user.Value[1]))
	}

	// Output: Max Mustermann
	// John Doe
	// Jane Doe
}

func Example_third() {
	csvmap, k := csv.LoadCSVfromString(userdata2)
	for _, user := range csvmap {
		fmt.Println(user[k["email"]])
	}

	// Output: jDoe@example.org
}
<commit_msg>change example tests to fix error at semaphoreci.com<commit_after>package csv_test

import (
	"fmt"
	"simonwaldherr.de/go/golibs/csv"
	"sort"
)

var userdata string = `id;name;email
0;John Doe;jDoe@example.org
1;Jane Doe;jane.doe@example.com
2;Max Mustermann;m.mustermann@alpha.tld`

var userdata2 string = `id;name;email
0;John Doe;jDoe@example.org`

type kv struct {
	Key   int
	Value []string
}

func sorting(m map[int][]string) []kv {
	var ss []kv
	for k, v := range m {
		ss = append(ss, kv{k, v})
	}
	sort.Slice(ss, func(i, j int) bool {
		return ss[i].Value[1] > ss[j].Value[1]
	})
	return ss
}

func Example() {
	csvmap, k := csv.LoadCSVfromString(userdata)

	for _, user := range csvmap {
		fmt.Println(user[k["email"]])
	}
}

func Example_second() {
	csvmap, k := csv.LoadCSVfromFile("./test.csv")

	for _, user := range csvmap {
		fmt.Println(user[k["email"]])
	}
}

func Example_third() {
	csvmap, k := csv.LoadCSVfromString(userdata2)
	for _, user := range csvmap {
		fmt.Println(user[k["email"]])
	}

	// Output: jDoe@example.org
}
<|endoftext|>


<commit_before>package exec

import (
	"sync"
	"time"

	"github.com/docker/docker/daemon/execdriver"
	derr "github.com/docker/docker/errors"
	"github.com/docker/docker/pkg/stringid"
	"github.com/docker/docker/runconfig"
)

// Config holds the configurations for execs. The Daemon keeps
// track of both running and finished execs so that they can be
// examined both during and after completion.
type Config struct {
	sync.Mutex
	*runconfig.StreamConfig
	ID            string
	Running       bool
	ExitCode      *int
	ProcessConfig *execdriver.ProcessConfig
	OpenStdin     bool
	OpenStderr    bool
	OpenStdout    bool
	CanRemove     bool
	ContainerID   string
	DetachKeys    []byte

	// waitStart will be closed immediately after the exec is really started.
	waitStart chan struct{}
}

// NewConfig initializes the a new exec configuration
func NewConfig() *Config {
	return &Config{
		ID:           stringid.GenerateNonCryptoID(),
		StreamConfig: runconfig.NewStreamConfig(),
		waitStart:    make(chan struct{}),
	}
}

// Store keeps track of the exec configurations.
type Store struct {
	commands map[string]*Config
	sync.RWMutex
}

// NewStore initializes a new exec store.
func NewStore() *Store {
	return &Store{commands: make(map[string]*Config, 0)}
}

// Commands returns the exec configurations in the store.
func (e *Store) Commands() map[string]*Config {
	return e.commands
}

// Add adds a new exec configuration to the store.
func (e *Store) Add(id string, Config *Config) {
	e.Lock()
	e.commands[id] = Config
	e.Unlock()
}

// Get returns an exec configuration by its id.
func (e *Store) Get(id string) *Config {
	e.RLock()
	res := e.commands[id]
	e.RUnlock()
	return res
}

// Delete removes an exec configuration from the store.
func (e *Store) Delete(id string) {
	e.Lock()
	delete(e.commands, id)
	e.Unlock()
}

// List returns the list of exec ids in the store.
func (e *Store) List() []string {
	var IDs []string
	e.RLock()
	for id := range e.commands {
		IDs = append(IDs, id)
	}
	e.RUnlock()
	return IDs
}

// Wait waits until the exec process finishes or there is an error in the error channel.
func (c *Config) Wait(cErr chan error) error {
	// Exec should not return until the process is actually running
	select {
	case <-c.waitStart:
	case err := <-cErr:
		return err
	}
	return nil
}

// Close closes the wait channel for the progress.
func (c *Config) Close() {
	close(c.waitStart)
}

// Resize changes the size of the terminal for the exec process.
func (c *Config) Resize(h, w int) error {
	select {
	case <-c.waitStart:
	case <-time.After(time.Second):
		return derr.ErrorCodeExecResize.WithArgs(c.ID)
	}
	return c.ProcessConfig.Terminal.Resize(h, w)
}
<commit_msg>Fix race condition in execCommandGC<commit_after>package exec

import (
	"sync"
	"time"

	"github.com/docker/docker/daemon/execdriver"
	derr "github.com/docker/docker/errors"
	"github.com/docker/docker/pkg/stringid"
	"github.com/docker/docker/runconfig"
)

// Config holds the configurations for execs. The Daemon keeps
// track of both running and finished execs so that they can be
// examined both during and after completion.
type Config struct {
	sync.Mutex
	*runconfig.StreamConfig
	ID            string
	Running       bool
	ExitCode      *int
	ProcessConfig *execdriver.ProcessConfig
	OpenStdin     bool
	OpenStderr    bool
	OpenStdout    bool
	CanRemove     bool
	ContainerID   string
	DetachKeys    []byte

	// waitStart will be closed immediately after the exec is really started.
	waitStart chan struct{}
}

// NewConfig initializes the a new exec configuration
func NewConfig() *Config {
	return &Config{
		ID:           stringid.GenerateNonCryptoID(),
		StreamConfig: runconfig.NewStreamConfig(),
		waitStart:    make(chan struct{}),
	}
}

// Store keeps track of the exec configurations.
type Store struct {
	commands map[string]*Config
	sync.RWMutex
}

// NewStore initializes a new exec store.
func NewStore() *Store {
	return &Store{commands: make(map[string]*Config, 0)}
}

// Commands returns the exec configurations in the store.
func (e *Store) Commands() map[string]*Config {
	e.RLock()
	commands := make(map[string]*Config, len(e.commands))
	for id, config := range e.commands {
		commands[id] = config
	}
	e.RUnlock()
	return commands
}

// Add adds a new exec configuration to the store.
func (e *Store) Add(id string, Config *Config) {
	e.Lock()
	e.commands[id] = Config
	e.Unlock()
}

// Get returns an exec configuration by its id.
func (e *Store) Get(id string) *Config {
	e.RLock()
	res := e.commands[id]
	e.RUnlock()
	return res
}

// Delete removes an exec configuration from the store.
func (e *Store) Delete(id string) {
	e.Lock()
	delete(e.commands, id)
	e.Unlock()
}

// List returns the list of exec ids in the store.
func (e *Store) List() []string {
	var IDs []string
	e.RLock()
	for id := range e.commands {
		IDs = append(IDs, id)
	}
	e.RUnlock()
	return IDs
}

// Wait waits until the exec process finishes or there is an error in the error channel.
func (c *Config) Wait(cErr chan error) error {
	// Exec should not return until the process is actually running
	select {
	case <-c.waitStart:
	case err := <-cErr:
		return err
	}
	return nil
}

// Close closes the wait channel for the progress.
func (c *Config) Close() {
	close(c.waitStart)
}

// Resize changes the size of the terminal for the exec process.
func (c *Config) Resize(h, w int) error {
	select {
	case <-c.waitStart:
	case <-time.After(time.Second):
		return derr.ErrorCodeExecResize.WithArgs(c.ID)
	}
	return c.ProcessConfig.Terminal.Resize(h, w)
}
<|endoftext|>