<commit_before>// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Contributor: Julien Vehent jvehent@mozilla.com [:ulfr]

package modules /* import "github.com/mozilla/mig/modules" */

import (
	"bytes"
	"compress/gzip"
	"encoding/base64"
	"encoding/json"
	"os"
	"strings"
	"testing"
	"time"
)

type testModule struct {
}

func (m *testModule) NewRun() Runner {
	return new(testRunner)
}

type testRunner struct {
	Parameters params
	Results    Result
}

func (r *testRunner) ValidateParameters() (err error) {
	return nil
}

func (r *testRunner) Run(in ModuleReader) (out string) {
	return ""
}

type params struct {
	SomeParam string `json:"someparam"`
}

func TestRegister(t *testing.T) {
	// test simple registration
	Register("testing", new(testModule))
	if _, ok := Available["testing"]; !ok {
		t.Fatalf("testing module registration failed")
	}
	// test availability of unregistered module
	if _, ok := Available["shouldnotberegistered"]; ok {
		t.Fatalf("testing module availability failed")
	}
	// test registration of already registered module
	defer func() {
		if r := recover(); r == nil {
			t.Fatalf("failed to panic on double registration of testing module")
		}
	}()
	Register("testing", new(testModule))
}

func TestMakeMessage(t *testing.T) {
	var p params
	p.SomeParam = "foo"
	raw, err := MakeMessage(MsgClassParameters, p, false)
	if err != nil {
		t.Fatalf(err.Error())
	}
	if string(raw) != `{"class":"parameters","parameters":{"someparam":"foo"}}` {
		t.Fatalf("Invalid module message class `parameters`")
	}

	// Test parameter decompression
	jb, err := json.Marshal(p)
	if err != nil {
		t.Fatalf(err.Error())
	}
	var b bytes.Buffer
	wb64 := base64.NewEncoder(base64.StdEncoding, &b)
	w := gzip.NewWriter(wb64)
	_, err = w.Write(jb)
	if err != nil {
		t.Fatalf(err.Error())
	}
	w.Close()
	wb64.Close()
	raw, err = MakeMessage(MsgClassParameters, string(b.Bytes()), true)
	if err != nil {
		t.Fatalf(err.Error())
	}
	if string(raw) != `{"class":"parameters","parameters":{"someparam":"foo"}}` {
		t.Fatalf("Invalid module message class `parameters`")
	}

	raw, err = MakeMessage(MsgClassStop, nil, false)
	if err != nil {
		t.Fatalf(err.Error())
	}
	if string(raw) != `{"class":"stop"}` {
		t.Fatalf("Invalid module message class `stop`")
	}
}

type element struct {
	SomeElement string `json:"someelement"`
}

func TestGetElements(t *testing.T) {
	var r Result
	r.Elements = struct {
		SomeElement string `json:"someelement"`
	}{
		SomeElement: "foo",
	}
	var el element
	err := r.GetElements(&el)
	if err != nil {
		t.Fatalf(err.Error())
	}
	if el.SomeElement != "foo" {
		t.Fatalf("failed to get element from module results")
	}

}

type statistics struct {
	SomeCounter float64 `json:"somecounter"`
}

func TestGetStatistics(t *testing.T) {
	var r Result
	r.Statistics = struct {
		SomeCounter float64 `json:"somecounter"`
	}{
		SomeCounter: 16.64,
	}
	var stats statistics
	err := r.GetStatistics(&stats)
	if err != nil {
		t.Fatalf(err.Error())
	}
	if stats.SomeCounter != 16.64 {
		t.Fatalf("failed to get statistics from module results")
	}
}

func TestReadInputParameters(t *testing.T) {
	var p params
	w := NewModuleReader(strings.NewReader(`{"class":"parameters","parameters":{"someparam":"foo"}}`))
	err := ReadInputParameters(w, &p)
	if err != nil {
		t.Fatalf(err.Error())
	}
	if p.SomeParam != "foo" {
		t.Fatalf("failed to read input parameters from stdin")
	}
	// test delayed write. use a pipe so that reader doesn't reach EOF on the first
	// read of the empty buffer.
	pr2, w2, err := os.Pipe()
	r2 := NewModuleReader(pr2)
	block := make(chan bool)
	go func() {
		err = ReadInputParameters(r2, &p)
		block <- true
	}()
	time.Sleep(100 * time.Millisecond)
	w2.WriteString(`{"class":"parameters","parameters":{"someparam":"bar"}}`)
	w2.Close() // close the pipe to trigger EOF on the reader
	select {
	case <-block:
	case <-time.After(2 * time.Second):
		t.Fatalf("input parameters read timed out")
	}
	if err != nil {
		t.Fatalf(err.Error())
	}
	if p.SomeParam != "bar" {
		t.Fatalf("failed to read input parameters")
	}
}

func TestWatchForStop(t *testing.T) {
	stopChan := make(chan bool)
	w := NewModuleReader(strings.NewReader(`{"class":"stop"}`))
	var err error
	go func() {
		err = WatchForStop(w, &stopChan)
	}()
	select {
	case <-stopChan:
		break
	case <-time.After(1 * time.Second):
		t.Fatalf("failed to catch stop message")
	}
}
<commit_msg>Removing unused variable<commit_after>// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Contributor: Julien Vehent jvehent@mozilla.com [:ulfr]

package modules /* import "github.com/mozilla/mig/modules" */

import (
	"bytes"
	"compress/gzip"
	"encoding/base64"
	"encoding/json"
	"os"
	"strings"
	"testing"
	"time"
)

type testModule struct {
}

func (m *testModule) NewRun() Runner {
	return new(testRunner)
}

type testRunner struct {
	Parameters params
	Results    Result
}

func (r *testRunner) ValidateParameters() (err error) {
	return nil
}

func (r *testRunner) Run(in ModuleReader) (out string) {
	return ""
}

type params struct {
	SomeParam string `json:"someparam"`
}

func TestRegister(t *testing.T) {
	// test simple registration
	Register("testing", new(testModule))
	if _, ok := Available["testing"]; !ok {
		t.Fatalf("testing module registration failed")
	}
	// test availability of unregistered module
	if _, ok := Available["shouldnotberegistered"]; ok {
		t.Fatalf("testing module availability failed")
	}
	// test registration of already registered module
	defer func() {
		if r := recover(); r == nil {
			t.Fatalf("failed to panic on double registration of testing module")
		}
	}()
	Register("testing", new(testModule))
}

func TestMakeMessage(t *testing.T) {
	var p params
	p.SomeParam = "foo"
	raw, err := MakeMessage(MsgClassParameters, p, false)
	if err != nil {
		t.Fatalf(err.Error())
	}
	if string(raw) != `{"class":"parameters","parameters":{"someparam":"foo"}}` {
		t.Fatalf("Invalid module message class `parameters`")
	}

	// Test parameter decompression
	jb, err := json.Marshal(p)
	if err != nil {
		t.Fatalf(err.Error())
	}
	var b bytes.Buffer
	wb64 := base64.NewEncoder(base64.StdEncoding, &b)
	w := gzip.NewWriter(wb64)
	_, err = w.Write(jb)
	if err != nil {
		t.Fatalf(err.Error())
	}
	w.Close()
	wb64.Close()
	raw, err = MakeMessage(MsgClassParameters, string(b.Bytes()), true)
	if err != nil {
		t.Fatalf(err.Error())
	}
	if string(raw) != `{"class":"parameters","parameters":{"someparam":"foo"}}` {
		t.Fatalf("Invalid module message class `parameters`")
	}

	raw, err = MakeMessage(MsgClassStop, nil, false)
	if err != nil {
		t.Fatalf(err.Error())
	}
	if string(raw) != `{"class":"stop"}` {
		t.Fatalf("Invalid module message class `stop`")
	}
}

type element struct {
	SomeElement string `json:"someelement"`
}

func TestGetElements(t *testing.T) {
	var r Result
	r.Elements = struct {
		SomeElement string `json:"someelement"`
	}{
		SomeElement: "foo",
	}
	var el element
	err := r.GetElements(&el)
	if err != nil {
		t.Fatalf(err.Error())
	}
	if el.SomeElement != "foo" {
		t.Fatalf("failed to get element from module results")
	}

}

type statistics struct {
	SomeCounter float64 `json:"somecounter"`
}

func TestGetStatistics(t *testing.T) {
	var r Result
	r.Statistics = struct {
		SomeCounter float64 `json:"somecounter"`
	}{
		SomeCounter: 16.64,
	}
	var stats statistics
	err := r.GetStatistics(&stats)
	if err != nil {
		t.Fatalf(err.Error())
	}
	if stats.SomeCounter != 16.64 {
		t.Fatalf("failed to get statistics from module results")
	}
}

func TestReadInputParameters(t *testing.T) {
	var p params
	w := NewModuleReader(strings.NewReader(`{"class":"parameters","parameters":{"someparam":"foo"}}`))
	err := ReadInputParameters(w, &p)
	if err != nil {
		t.Fatalf(err.Error())
	}
	if p.SomeParam != "foo" {
		t.Fatalf("failed to read input parameters from stdin")
	}
	// test delayed write. use a pipe so that reader doesn't reach EOF on the first
	// read of the empty buffer.
	pr2, w2, err := os.Pipe()
	r2 := NewModuleReader(pr2)
	block := make(chan bool)
	go func() {
		err = ReadInputParameters(r2, &p)
		block <- true
	}()
	time.Sleep(100 * time.Millisecond)
	w2.WriteString(`{"class":"parameters","parameters":{"someparam":"bar"}}`)
	w2.Close() // close the pipe to trigger EOF on the reader
	select {
	case <-block:
	case <-time.After(2 * time.Second):
		t.Fatalf("input parameters read timed out")
	}
	if err != nil {
		t.Fatalf(err.Error())
	}
	if p.SomeParam != "bar" {
		t.Fatalf("failed to read input parameters")
	}
}

func TestWatchForStop(t *testing.T) {
	stopChan := make(chan bool)
	w := NewModuleReader(strings.NewReader(`{"class":"stop"}`))
	go func() {
		WatchForStop(w, &stopChan)
	}()
	select {
	case <-stopChan:
		break
	case <-time.After(1 * time.Second):
		t.Fatalf("failed to catch stop message")
	}
}
<|endoftext|>


<commit_before>package main

import (
	"encoding/json"
	"fmt"
	"os"

	apex "github.com/apex/go-apex"
	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/lambda"
	"github.com/nabeken/aaa/slack"
	"github.com/pkg/errors"
)

func main() {
	sess := session.Must(session.NewSession())
	lambdaSvc := lambda.New(sess)

	executorFuncName := os.Getenv("AAA_EXECUTOR_FUNC_NAME")
	apex.HandleFunc(func(event json.RawMessage, ctx *apex.Context) (interface{}, error) {
		if executorFuncName == "" {
			return nil, errors.New("Please set AAA_EXECUTOR_FUNC_NAME environment variable.")
		}

		slcmd, err := slack.ParseCommand(event)
		if err != nil {
			return nil, errors.Wrap(err, "failed to parse the command")
		}

		req := &lambda.InvokeInput{
			FunctionName:   aws.String(executorFuncName),
			InvocationType: aws.String(lambda.InvocationTypeEvent),
			Payload:        event,
		}

		if _, err := lambdaSvc.Invoke(req); err != nil {
			return nil, errors.Wrap(err, "failed to invoke the executor")
		}

		return &slack.CommandResponse{
			ResponseType: "in_channel",
			Text:         fmt.Sprintf("@%s Your request has been accepted.", slcmd.UserName),
		}, nil
	})
}
<commit_msg>dispatcher: check the token<commit_after>package main

import (
	"encoding/json"
	"fmt"
	"os"

	apex "github.com/apex/go-apex"
	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/lambda"
	"github.com/nabeken/aaa/slack"
	"github.com/pkg/errors"
)

func main() {
	sess := session.Must(session.NewSession())
	lambdaSvc := lambda.New(sess)

	token := os.Getenv("SLACK_TOKEN")
	executorFuncName := os.Getenv("AAA_EXECUTOR_FUNC_NAME")
	apex.HandleFunc(func(event json.RawMessage, ctx *apex.Context) (interface{}, error) {
		if executorFuncName == "" {
			return nil, errors.New("Please set AAA_EXECUTOR_FUNC_NAME environment variable.")
		}

		slcmd, err := slack.ParseCommand(event)
		if err != nil {
			return nil, errors.Wrap(err, "failed to parse the command")
		}

		if slcmd.Token != token {
			return nil, errors.New("Who are you? Token does not match.")
		}

		req := &lambda.InvokeInput{
			FunctionName:   aws.String(executorFuncName),
			InvocationType: aws.String(lambda.InvocationTypeEvent),
			Payload:        event,
		}

		if _, err := lambdaSvc.Invoke(req); err != nil {
			return nil, errors.Wrap(err, "failed to invoke the executor")
		}

		return &slack.CommandResponse{
			ResponseType: "in_channel",
			Text:         fmt.Sprintf("@%s Your request has been accepted.", slcmd.UserName),
		}, nil
	})
}
<|endoftext|>


<commit_before>package renter

import (
	"errors"
	"sync/atomic"

	"github.com/NebulousLabs/Sia/crypto"
	"github.com/NebulousLabs/Sia/modules"
	"github.com/NebulousLabs/Sia/types"
)

var (
	ErrUnknownNickname  = errors.New("no file known by that nickname")
	ErrNicknameOverload = errors.New("a file with the proposed nickname already exists")
)

// A file is a single file that has been uploaded to the network. Files are
// split into equal-length chunks, which are then erasure-coded into pieces.
// Each piece is separately encrypted, using a key derived from the file's
// master key. The pieces are uploaded to hosts in groups, such that one file
// contract covers many pieces.
type file struct {
	// NOTE: these fields are defined first to ensure 64-bit alignment, which
	// is required for atomic operations.
	bytesUploaded  uint64
	chunksUploaded uint64

	name        string
	size        uint64
	contracts   map[modules.NetAddress]fileContract
	masterKey   crypto.TwofishKey
	erasureCode modules.ErasureCoder
	pieceSize   uint64
	mode        uint32 // actually an os.FileMode
}

// A fileContract is a contract covering an arbitrary number of file pieces.
// Chunk/Piece metadata is used to split the raw contract data appropriately.
type fileContract struct {
	ID     types.FileContractID
	IP     modules.NetAddress
	Pieces []pieceData

	WindowStart types.BlockHeight
}

// pieceData contains the metadata necessary to request a piece from a
// fetcher.
type pieceData struct {
	Chunk  uint64 // which chunk the piece belongs to
	Piece  uint64 // the index of the piece in the chunk
	Offset uint64 // the offset of the piece in the file contract
}

// deriveKey derives the key used to encrypt and decrypt a specific file piece.
func deriveKey(masterKey crypto.TwofishKey, chunkIndex, pieceIndex uint64) crypto.TwofishKey {
	return crypto.TwofishKey(crypto.HashAll(masterKey, chunkIndex, pieceIndex))
}

// chunkSize returns the size of one chunk.
func (f *file) chunkSize() uint64 {
	return f.pieceSize * uint64(f.erasureCode.MinPieces())
}

// numChunks returns the number of chunks that f was split into.
func (f *file) numChunks() uint64 {
	n := f.size / f.chunkSize()
	if f.size%f.chunkSize() != 0 {
		n++
	}
	return n
}

// Available indicates whether the file is ready to be downloaded.
func (f *file) Available() bool {
	return atomic.LoadUint64(&f.chunksUploaded) >= f.numChunks()
}

// UploadProgress indicates what percentage of the file (plus redundancy) has
// been uploaded. Note that a file may be Available long before UploadProgress
// reaches 100%.
func (f *file) UploadProgress() float32 {
	totalBytes := f.pieceSize * uint64(f.erasureCode.NumPieces()) * f.numChunks()
	return 100 * float32(atomic.LoadUint64(&f.bytesUploaded)) / float32(totalBytes)
}

// Nickname returns the nickname of the file.
func (f *file) Nickname() string {
	return f.name
}

// Filesize returns the size of the file.
func (f *file) Filesize() uint64 {
	return f.size
}

// Expiration returns the lowest height at which any of the file's contracts
// will expire.
func (f *file) Expiration() types.BlockHeight {
	if len(f.contracts) == 0 {
		return 0
	}
	lowest := ^types.BlockHeight(0)
	for _, fc := range f.contracts {
		if fc.WindowStart < lowest {
			lowest = fc.WindowStart
		}
	}
	return lowest
}

// newFile creates a new file object.
func newFile(name string, code modules.ErasureCoder, pieceSize, fileSize uint64) *file {
	key, _ := crypto.GenerateTwofishKey()
	return &file{
		name:        name,
		size:        fileSize,
		contracts:   make(map[modules.NetAddress]fileContract),
		masterKey:   key,
		erasureCode: code,
		pieceSize:   pieceSize,
	}
}

// DeleteFile removes a file entry from the renter.
func (r *Renter) DeleteFile(nickname string) error {
	lockID := r.mu.Lock()
	defer r.mu.Unlock(lockID)

	_, exists := r.files[nickname]
	if !exists {
		return ErrUnknownNickname
	}
	delete(r.files, nickname)

	r.save()
	return nil
}

// FileList returns all of the files that the renter has.
func (r *Renter) FileList() (files []modules.FileInfo) {
	lockID := r.mu.RLock()
	defer r.mu.RUnlock(lockID)

	for _, f := range r.files {
		files = append(files, f)
	}
	return
}

// RenameFile takes an existing file and changes the nickname. The original
// file must exist, and there must not be any file that already has the
// replacement nickname.
func (r *Renter) RenameFile(currentName, newName string) error {
	lockID := r.mu.Lock()
	defer r.mu.Unlock(lockID)

	// Check that the currentName exists and the newName doesn't.
	file, exists := r.files[currentName]
	if !exists {
		return ErrUnknownNickname
	}
	_, exists = r.files[newName]
	if exists {
		return ErrNicknameOverload
	}

	// Do the renaming.
	delete(r.files, currentName)
	file.name = newName // make atomic?
	r.files[newName] = file

	r.save()
	return nil
}
<commit_msg>delete .sia files on disk<commit_after>package renter

import (
	"errors"
	"os"
	"path/filepath"
	"sync/atomic"

	"github.com/NebulousLabs/Sia/crypto"
	"github.com/NebulousLabs/Sia/modules"
	"github.com/NebulousLabs/Sia/types"
)

var (
	ErrUnknownNickname  = errors.New("no file known by that nickname")
	ErrNicknameOverload = errors.New("a file with the proposed nickname already exists")
)

// A file is a single file that has been uploaded to the network. Files are
// split into equal-length chunks, which are then erasure-coded into pieces.
// Each piece is separately encrypted, using a key derived from the file's
// master key. The pieces are uploaded to hosts in groups, such that one file
// contract covers many pieces.
type file struct {
	// NOTE: these fields are defined first to ensure 64-bit alignment, which
	// is required for atomic operations.
	bytesUploaded  uint64
	chunksUploaded uint64

	name        string
	size        uint64
	contracts   map[modules.NetAddress]fileContract
	masterKey   crypto.TwofishKey
	erasureCode modules.ErasureCoder
	pieceSize   uint64
	mode        uint32 // actually an os.FileMode
}

// A fileContract is a contract covering an arbitrary number of file pieces.
// Chunk/Piece metadata is used to split the raw contract data appropriately.
type fileContract struct {
	ID     types.FileContractID
	IP     modules.NetAddress
	Pieces []pieceData

	WindowStart types.BlockHeight
}

// pieceData contains the metadata necessary to request a piece from a
// fetcher.
type pieceData struct {
	Chunk  uint64 // which chunk the piece belongs to
	Piece  uint64 // the index of the piece in the chunk
	Offset uint64 // the offset of the piece in the file contract
}

// deriveKey derives the key used to encrypt and decrypt a specific file piece.
func deriveKey(masterKey crypto.TwofishKey, chunkIndex, pieceIndex uint64) crypto.TwofishKey {
	return crypto.TwofishKey(crypto.HashAll(masterKey, chunkIndex, pieceIndex))
}

// chunkSize returns the size of one chunk.
func (f *file) chunkSize() uint64 {
	return f.pieceSize * uint64(f.erasureCode.MinPieces())
}

// numChunks returns the number of chunks that f was split into.
func (f *file) numChunks() uint64 {
	n := f.size / f.chunkSize()
	if f.size%f.chunkSize() != 0 {
		n++
	}
	return n
}

// Available indicates whether the file is ready to be downloaded.
func (f *file) Available() bool {
	return atomic.LoadUint64(&f.chunksUploaded) >= f.numChunks()
}

// UploadProgress indicates what percentage of the file (plus redundancy) has
// been uploaded. Note that a file may be Available long before UploadProgress
// reaches 100%.
func (f *file) UploadProgress() float32 {
	totalBytes := f.pieceSize * uint64(f.erasureCode.NumPieces()) * f.numChunks()
	return 100 * float32(atomic.LoadUint64(&f.bytesUploaded)) / float32(totalBytes)
}

// Nickname returns the nickname of the file.
func (f *file) Nickname() string {
	return f.name
}

// Filesize returns the size of the file.
func (f *file) Filesize() uint64 {
	return f.size
}

// Expiration returns the lowest height at which any of the file's contracts
// will expire.
func (f *file) Expiration() types.BlockHeight {
	if len(f.contracts) == 0 {
		return 0
	}
	lowest := ^types.BlockHeight(0)
	for _, fc := range f.contracts {
		if fc.WindowStart < lowest {
			lowest = fc.WindowStart
		}
	}
	return lowest
}

// newFile creates a new file object.
func newFile(name string, code modules.ErasureCoder, pieceSize, fileSize uint64) *file {
	key, _ := crypto.GenerateTwofishKey()
	return &file{
		name:        name,
		size:        fileSize,
		contracts:   make(map[modules.NetAddress]fileContract),
		masterKey:   key,
		erasureCode: code,
		pieceSize:   pieceSize,
	}
}

// DeleteFile removes a file entry from the renter.
func (r *Renter) DeleteFile(nickname string) error {
	lockID := r.mu.Lock()
	defer r.mu.Unlock(lockID)

	f, exists := r.files[nickname]
	if !exists {
		return ErrUnknownNickname
	}
	delete(r.files, nickname)

	os.Remove(filepath.Join(r.saveDir, f.name+ShareExtension))

	r.save()
	return nil
}

// FileList returns all of the files that the renter has.
func (r *Renter) FileList() (files []modules.FileInfo) {
	lockID := r.mu.RLock()
	defer r.mu.RUnlock(lockID)

	for _, f := range r.files {
		files = append(files, f)
	}
	return
}

// RenameFile takes an existing file and changes the nickname. The original
// file must exist, and there must not be any file that already has the
// replacement nickname.
func (r *Renter) RenameFile(currentName, newName string) error {
	lockID := r.mu.Lock()
	defer r.mu.Unlock(lockID)

	// Check that the currentName exists and the newName doesn't.
	file, exists := r.files[currentName]
	if !exists {
		return ErrUnknownNickname
	}
	_, exists = r.files[newName]
	if exists {
		return ErrNicknameOverload
	}

	// Do the renaming.
	delete(r.files, currentName)
	file.name = newName // make atomic?
	r.files[newName] = file

	r.save()
	return nil
}
<|endoftext|>


<commit_before>package cliedit

import (
	"github.com/elves/elvish/cli"
	"github.com/elves/elvish/cli/addons/histwalk"
	"github.com/elves/elvish/cli/histutil"
	"github.com/elves/elvish/eval"
)

func initHistWalk(app *cli.App, ev *eval.Evaler, ns eval.Ns, fuser *histutil.Fuser) {
	bindingVar := newBindingVar(emptyBindingMap)
	binding := newMapBinding(app, ev, bindingVar)
	ns.AddNs("history",
		eval.Ns{
			"binding": bindingVar,
		}.AddGoFns("<edit:history>", map[string]interface{}{
			"start": func() {
				buf := app.CodeArea.CopyState().CodeBuffer
				walker := fuser.Walker(buf.Content[:buf.Dot])
				histwalk.Start(app, histwalk.Config{Binding: binding, Walker: walker})
			},
			"prev":  func() error { return histwalk.Prev(app) },
			"next":  func() error { return histwalk.Next(app) },
			"close": func() { histwalk.Close(app) },
		}))
}
<commit_msg>cliedit: Rename the functions in history: to be backward compatible.<commit_after>package cliedit

import (
	"github.com/elves/elvish/cli"
	"github.com/elves/elvish/cli/addons/histwalk"
	"github.com/elves/elvish/cli/el"
	"github.com/elves/elvish/cli/histutil"
	"github.com/elves/elvish/eval"
)

func initHistWalk(app *cli.App, ev *eval.Evaler, ns eval.Ns, fuser *histutil.Fuser) {
	bindingVar := newBindingVar(emptyBindingMap)
	binding := newMapBinding(app, ev, bindingVar)
	ns.AddNs("history",
		eval.Ns{
			"binding": bindingVar,
		}.AddGoFns("<edit:history>", map[string]interface{}{
			"start": func() { histWalkStart(app, fuser, binding) },
			"up":    func() error { return histwalk.Prev(app) },
			"down":  func() error { return histwalk.Next(app) },
			"close": func() { histwalk.Close(app) },
		}))
}

func histWalkStart(app *cli.App, fuser *histutil.Fuser, binding el.Handler) {
	buf := app.CodeArea.CopyState().CodeBuffer
	walker := fuser.Walker(buf.Content[:buf.Dot])
	histwalk.Start(app, histwalk.Config{Binding: binding, Walker: walker})
}
<|endoftext|>


<commit_before>package main

import (
"fmt"
"bytes"
"net/http"
"./variables"
)

func checkUser(username string) bool{
    var userPresent bool

    url := variables.AWSEndPoint + "/checkUser"

    var jsonprep string = "{\"username\":\"" + username + "\"}"
    var jsonStr = []byte(jsonprep)

    req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
    req.Header.Set("Content-Type", "application/json")

    client := http.Client{}
    resp, err := client.Do(req)

    if err != nil {
        //panic(err)
    }
    buf := new(bytes.Buffer)
    buf.ReadFrom(resp.Body)
    response := buf.String()

    //fmt.Println(response)
    defer resp.Body.Close()
    userPresent = response

    return userPresent
}

func main() {
    var username,password string
    fmt.Println("Enter your username:")
    fmt.Scanf("%s", &username)

    fmt.Println("Enter your password:")
    fmt.Scanf("%s", &password)

    checkUser(username)
}
<commit_msg>go apihelper for registerUser api<commit_after>package main

import (
"fmt"
"bytes"
"strings"
"net/http"
"./variables"
)

func checkUser(username string) bool{
    var userPresent bool

    url := variables.AWSEndPoint + "/checkUser"

    var jsonprep string = "{\"username\":\"" + username + "\"}"
    var jsonStr = []byte(jsonprep)

    req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
    req.Header.Set("Content-Type", "application/json")

    client := http.Client{}
    resp, err := client.Do(req)

    if err != nil {
        //panic(err)
    }

    buf := new(bytes.Buffer)
    buf.ReadFrom(resp.Body)
    response := buf.String()

    //fmt.Println(response)
    defer resp.Body.Close()
    if response == "true" {
        userPresent = true
    } else {
        userPresent = false
    }

    return userPresent
}

func registerUser(username string, password string) bool{
    var success bool

    url := variables.AWSEndPoint + "/registerUser"

    var jsonprep string = "{\"username\":\"" + username + "\",\"password\":\"" + password + "\"}"
    var jsonStr = []byte(jsonprep)

    req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
    req.Header.Set("Content-Type", "application/json")

    client := http.Client{}
    resp, err := client.Do(req)

    if err != nil {
        //panic(err)
    }

    buf := new(bytes.Buffer)
    buf.ReadFrom(resp.Body)
    response := buf.String()

    success = strings.Contains(response, "created")

    defer resp.Body.Close()

    return success
}

func main() {
    var username,password string
    fmt.Println("Enter your username:")
    fmt.Scanf("%s", &username)
    fmt.Println("Enter your password:")
    fmt.Scanf("%s", &password)

    fmt.Println(registerUser(username, password))
    fmt.Println(checkUser(username))
}
<|endoftext|>


<commit_before>// Copyright 2015 ThoughtWorks, Inc.

// This file is part of Gauge.

// Gauge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Gauge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Gauge.  If not, see <http://www.gnu.org/licenses/>.

package execution

import (
	"time"

	"github.com/getgauge/gauge/execution/result"
	"github.com/getgauge/gauge/gauge"
	"github.com/getgauge/gauge/gauge_messages"
	"github.com/getgauge/gauge/logger"
	"github.com/getgauge/gauge/reporter"

	"github.com/getgauge/gauge/manifest"
	"github.com/getgauge/gauge/plugin"
	"github.com/getgauge/gauge/runner"
)

var ExecuteTags = ""
var TableRows = ""

type simpleExecution struct {
	manifest             *manifest.Manifest
	runner               *runner.TestRunner
	specStore            *specStore
	pluginHandler        *plugin.Handler
	currentExecutionInfo *gauge_messages.ExecutionInfo
	suiteResult          *result.SuiteResult
	consoleReporter      reporter.Reporter
	errMaps              *validationErrMaps
	startTime            time.Time
}

func newSimpleExecution(executionInfo *executionInfo) *simpleExecution {
	return &simpleExecution{manifest: executionInfo.manifest, specStore: executionInfo.specStore,
		runner: executionInfo.runner, pluginHandler: executionInfo.pluginHandler, consoleReporter: executionInfo.consoleReporter, errMaps: executionInfo.errMaps}
}

func (e *simpleExecution) result() *result.SuiteResult {
	return e.suiteResult
}

func (e *simpleExecution) startExecution() *(gauge_messages.ProtoExecutionResult) {
	message := &gauge_messages.Message{MessageType: gauge_messages.Message_ExecutionStarting.Enum(),
		ExecutionStartingRequest: &gauge_messages.ExecutionStartingRequest{}}
	return e.executeHook(message)
}

func (e *simpleExecution) initializeSuiteDataStore() *(gauge_messages.ProtoExecutionResult) {
	initSuiteDataStoreMessage := &gauge_messages.Message{MessageType: gauge_messages.Message_SuiteDataStoreInit.Enum(),
		SuiteDataStoreInitRequest: &gauge_messages.SuiteDataStoreInitRequest{}}
	initResult := executeAndGetStatus(e.runner, initSuiteDataStoreMessage)
	return initResult
}

func (e *simpleExecution) endExecution() *(gauge_messages.ProtoExecutionResult) {
	message := &gauge_messages.Message{MessageType: gauge_messages.Message_ExecutionEnding.Enum(),
		ExecutionEndingRequest: &gauge_messages.ExecutionEndingRequest{CurrentExecutionInfo: e.currentExecutionInfo}}
	return e.executeHook(message)
}

func (e *simpleExecution) executeHook(message *gauge_messages.Message) *(gauge_messages.ProtoExecutionResult) {
	e.pluginHandler.NotifyPlugins(message)
	executionResult := executeAndGetStatus(e.runner, message)
	e.addExecTime(executionResult.GetExecutionTime())
	return executionResult
}

func (e *simpleExecution) addExecTime(execTime int64) {
	e.suiteResult.ExecutionTime += execTime
}

func (e *simpleExecution) notifyExecutionResult() {
	message := &gauge_messages.Message{MessageType: gauge_messages.Message_SuiteExecutionResult.Enum(),
		SuiteExecutionResult: &gauge_messages.SuiteExecutionResult{SuiteResult: gauge.ConvertToProtoSuiteResult(e.suiteResult)}}
	e.pluginHandler.NotifyPlugins(message)
}

func (e *simpleExecution) notifyExecutionStop() {
	message := &gauge_messages.Message{MessageType: gauge_messages.Message_KillProcessRequest.Enum(),
		KillProcessRequest: &gauge_messages.KillProcessRequest{}}
	e.pluginHandler.NotifyPlugins(message)
	e.pluginHandler.GracefullyKillPlugins()
}

func (e *simpleExecution) start() {
	e.startTime = time.Now()
	e.pluginHandler = plugin.StartPlugins(e.manifest)
}

func (e *simpleExecution) run() {
	e.start()
	e.suiteResult = result.NewSuiteResult(ExecuteTags, e.startTime)
	setResult := func() {
		e.suiteResult.ExecutionTime = int64(time.Since(e.startTime) / 1e6)
		e.suiteResult.SpecsSkippedCount = len(e.errMaps.specErrs)
	}

	initSuiteDataStoreResult := e.initializeSuiteDataStore()
	if initSuiteDataStoreResult.GetFailed() {
		e.consoleReporter.Error("Failed to initialize suite datastore. Error: %s", initSuiteDataStoreResult.GetErrorMessage())
		setResult()
		return
	}

	beforeSuiteHookExecResult := e.startExecution()
	if beforeSuiteHookExecResult.GetFailed() {
		handleHookFailure(e.suiteResult, beforeSuiteHookExecResult, result.AddPreHook, e.consoleReporter)
		setResult()
		return
	}

	for e.specStore.hasNext() {
		e.executeSpec(e.specStore.next())
	}

	afterSuiteHookExecResult := e.endExecution()
	if afterSuiteHookExecResult.GetFailed() {
		handleHookFailure(e.suiteResult, afterSuiteHookExecResult, result.AddPostHook, e.consoleReporter)
	}
	setResult()
	e.finish()
}

func (e *simpleExecution) finish() {
	e.notifyExecutionResult()
	e.stopAllPlugins()
}

func (e *simpleExecution) stopAllPlugins() {
	e.notifyExecutionStop()
	if err := e.runner.Kill(); err != nil {
		e.consoleReporter.Error("Failed to kill Runner: %s", err.Error())
	}
}

func (e *simpleExecution) executeSpec(specificationToExecute *gauge.Specification) {
	executor := newSpecExecutor(specificationToExecute, e.runner, e.pluginHandler, getDataTableRows(specificationToExecute.DataTable.Table.GetRowCount()), e.consoleReporter, e.errMaps)
	protoSpecResult := executor.execute()
	e.suiteResult.AddSpecResult(protoSpecResult)
}

func handleHookFailure(result result.Result, execResult *gauge_messages.ProtoExecutionResult, predicate func(result.Result, *gauge_messages.ProtoExecutionResult), reporter reporter.Reporter) {
	predicate(result, execResult)
	printStatus(execResult, reporter)
}

func getDataTableRows(rowCount int) indexRange {
	if TableRows == "" {
		return indexRange{start: 0, end: rowCount - 1}
	}
	indexes, err := getDataTableRowsRange(TableRows, rowCount)
	if err != nil {
		logger.Errorf("Table rows validation failed. %s\n", err.Error())
	}
	return indexes
}
<commit_msg>reorganized simple execution<commit_after>// Copyright 2015 ThoughtWorks, Inc.

// This file is part of Gauge.

// Gauge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Gauge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Gauge.  If not, see <http://www.gnu.org/licenses/>.

package execution

import (
	"time"

	"github.com/getgauge/gauge/execution/result"
	"github.com/getgauge/gauge/gauge"
	"github.com/getgauge/gauge/gauge_messages"
	"github.com/getgauge/gauge/logger"
	"github.com/getgauge/gauge/reporter"

	"github.com/getgauge/gauge/manifest"
	"github.com/getgauge/gauge/plugin"
	"github.com/getgauge/gauge/runner"
)

var ExecuteTags = ""
var TableRows = ""

type simpleExecution struct {
	manifest             *manifest.Manifest
	runner               *runner.TestRunner
	specStore            *specStore
	pluginHandler        *plugin.Handler
	currentExecutionInfo *gauge_messages.ExecutionInfo
	suiteResult          *result.SuiteResult
	consoleReporter      reporter.Reporter
	errMaps              *validationErrMaps
	startTime            time.Time
}

func newSimpleExecution(executionInfo *executionInfo) *simpleExecution {
	return &simpleExecution{manifest: executionInfo.manifest, specStore: executionInfo.specStore,
		runner: executionInfo.runner, pluginHandler: executionInfo.pluginHandler, consoleReporter: executionInfo.consoleReporter, errMaps: executionInfo.errMaps}
}

func (e *simpleExecution) run() {
	e.start()
	e.suiteResult = result.NewSuiteResult(ExecuteTags, e.startTime)
	setResult := func() {
		e.suiteResult.ExecutionTime = int64(time.Since(e.startTime) / 1e6)
		e.suiteResult.SpecsSkippedCount = len(e.errMaps.specErrs)
	}

	initSuiteDataStoreResult := e.initializeSuiteDataStore()
	if initSuiteDataStoreResult.GetFailed() {
		e.consoleReporter.Error("Failed to initialize suite datastore. Error: %s", initSuiteDataStoreResult.GetErrorMessage())
		setResult()
		return
	}

	beforeSuiteHookExecResult := e.notifyBeforeSuite()
	if beforeSuiteHookExecResult.GetFailed() {
		handleHookFailure(e.suiteResult, beforeSuiteHookExecResult, result.AddPreHook, e.consoleReporter)
		setResult()
		return
	}

	for e.specStore.hasNext() {
		r := e.executeSpec(e.specStore.next())
		e.suiteResult.AddSpecResult(r)
	}

	afterSuiteHookExecResult := e.notifyAfterSuite()
	if afterSuiteHookExecResult.GetFailed() {
		handleHookFailure(e.suiteResult, afterSuiteHookExecResult, result.AddPostHook, e.consoleReporter)
	}
	setResult()
	e.finish()
}

func (e *simpleExecution) start() {
	e.startTime = time.Now()
	e.pluginHandler = plugin.StartPlugins(e.manifest)
}

func (e *simpleExecution) finish() {
	e.notifyExecutionResult()
	e.stopAllPlugins()
}

func (e *simpleExecution) stopAllPlugins() {
	e.notifyExecutionStop()
	if err := e.runner.Kill(); err != nil {
		e.consoleReporter.Error("Failed to kill Runner: %s", err.Error())
	}
}

func (e *simpleExecution) executeSpec(specificationToExecute *gauge.Specification) *result.SpecResult {
	executor := newSpecExecutor(specificationToExecute, e.runner, e.pluginHandler, getDataTableRows(specificationToExecute.DataTable.Table.GetRowCount()), e.consoleReporter, e.errMaps)
	return executor.execute()
}

func (e *simpleExecution) result() *result.SuiteResult {
	return e.suiteResult
}

func (e *simpleExecution) notifyBeforeSuite() *(gauge_messages.ProtoExecutionResult) {
	m := &gauge_messages.Message{MessageType: gauge_messages.Message_ExecutionStarting.Enum(),
		ExecutionStartingRequest: &gauge_messages.ExecutionStartingRequest{}}
	return e.executeHook(m)
}
func (e *simpleExecution) notifyAfterSuite() *(gauge_messages.ProtoExecutionResult) {
	m := &gauge_messages.Message{MessageType: gauge_messages.Message_ExecutionEnding.Enum(),
		ExecutionEndingRequest: &gauge_messages.ExecutionEndingRequest{CurrentExecutionInfo: e.currentExecutionInfo}}
	return e.executeHook(m)
}

func (e *simpleExecution) initializeSuiteDataStore() *(gauge_messages.ProtoExecutionResult) {
	m := &gauge_messages.Message{MessageType: gauge_messages.Message_SuiteDataStoreInit.Enum(),
		SuiteDataStoreInitRequest: &gauge_messages.SuiteDataStoreInitRequest{}}
	return executeAndGetStatus(e.runner, m)
}

func (e *simpleExecution) executeHook(m *gauge_messages.Message) *(gauge_messages.ProtoExecutionResult) {
	e.pluginHandler.NotifyPlugins(m)
	return executeAndGetStatus(e.runner, m)
}

func (e *simpleExecution) notifyExecutionResult() {
	m := &gauge_messages.Message{MessageType: gauge_messages.Message_SuiteExecutionResult.Enum(),
		SuiteExecutionResult: &gauge_messages.SuiteExecutionResult{SuiteResult: gauge.ConvertToProtoSuiteResult(e.suiteResult)}}
	e.pluginHandler.NotifyPlugins(m)
}

func (e *simpleExecution) notifyExecutionStop() {
	m := &gauge_messages.Message{MessageType: gauge_messages.Message_KillProcessRequest.Enum(),
		KillProcessRequest: &gauge_messages.KillProcessRequest{}}
	e.pluginHandler.NotifyPlugins(m)
	e.pluginHandler.GracefullyKillPlugins()
}

func handleHookFailure(result result.Result, execResult *gauge_messages.ProtoExecutionResult, f func(result.Result, *gauge_messages.ProtoExecutionResult), reporter reporter.Reporter) {
	f(result, execResult)
	printStatus(execResult, reporter)
}

func getDataTableRows(rowCount int) indexRange {
	if TableRows == "" {
		return indexRange{start: 0, end: rowCount - 1}
	}
	indexes, err := getDataTableRowsRange(TableRows, rowCount)
	if err != nil {
		logger.Errorf("Table rows validation failed. %s\n", err.Error())
	}
	return indexes
}
<|endoftext|>


<commit_before>package executor_test

import (
	"os/exec"

	"github.com/JulzDiverse/aviator"
	. "github.com/JulzDiverse/aviator/executor"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("Flyexecutor", func() {
	var (
		flyExecutor *FlyExecutor
		fly         aviator.Fly
		args        []string
		exposeArgs  []string
		cmds        []*exec.Cmd
		err         error
	)

	Context("When generating commands", func() {
		JustBeforeEach(func() {
			flyExecutor = &FlyExecutor{}
			cmds, err = flyExecutor.Command(fly)
			args = cmds[0].Args
			exposeArgs = cmds[1].Args
		})

		Context("for a given fly config", func() {
			BeforeEach(func() {
				fly = aviator.Fly{
					Name:   "pipeline-name",
					Target: "target-name",
					Config: "pipeline.yml",
					Expose: true,
					Vars:   []string{"credentials.yml", "props.yml"},
				}
			})

			It("shouldn't error", func() {
				Expect(err).ToNot(HaveOccurred())
			})

			It("should generate two commands", func() {
				Expect(cmds).To(HaveLen(2))
			})

			It("generates the set-pipeline command with the 'target' flag and the right target", func() {
				Expect(args).To(ContainElement("--target"))
				Expect(args).To(ContainElement("target-name"))
			})

			It("generates the set-pipeline command with the 'pipeline' flag and the right pipeline", func() {
				Expect(args).To(ContainElement("--pipeline"))
				Expect(args).To(ContainElement("pipeline-name"))
			})

			It("generates the set-pipeline command with the 'config' flag and the right config file", func() {
				Expect(args).To(ContainElement("--config"))
				Expect(args).To(ContainElement("pipeline.yml"))
			})

			It("generates the set-pipeline command with the 'load-vars-from' flag and the right files", func() {
				Expect(args).To(ContainElement("--load-vars-from"))
				Expect(args).To(ContainElement("credentials.yml"))
				Expect(args).To(ContainElement("props.yml"))
			})

			It("should create the expose command with pipeline name", func() {
				Expect(exposeArgs).To(ContainElement("expose-pipeline"))
				Expect(exposeArgs).To(ContainElement("--target"))
				Expect(exposeArgs).To(ContainElement("target-name"))
				Expect(exposeArgs).To(ContainElement("--pipeline"))
				Expect(exposeArgs).To(ContainElement("pipeline-name"))
			})
		})

		Context("When expose is not set (or false)", func() {
			BeforeEach(func() {
				fly = aviator.Fly{
					Name:   "pipeline-name",
					Target: "target-name",
					Config: "pipeline.yml",
				}
			})

			It("should generate two commands", func() {
				Expect(cmds).To(HaveLen(2))
			})

			It("should generate a hide-pipeline command", func() {
				Expect(exposeArgs).To(ContainElement("hide-pipeline"))
			})

			It("should add the --target flag following by the right target", func() {
				Expect(exposeArgs).To(ContainElement("--target"))
				Expect(exposeArgs).To(ContainElement("target-name"))
			})

			It("should add the pipeline flag following by the right pipeline name", func() {
				Expect(exposeArgs).To(ContainElement("--pipeline"))
				Expect(exposeArgs).To(ContainElement("pipeline-name"))
			})
		})
	})
})
<commit_msg>add test case for --check-creds flag<commit_after>package executor_test

import (
	"os/exec"

	"github.com/JulzDiverse/aviator"
	. "github.com/JulzDiverse/aviator/executor"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("Flyexecutor", func() {
	var (
		flyExecutor *FlyExecutor
		fly         aviator.Fly
		args        []string
		exposeArgs  []string
		cmds        []*exec.Cmd
		err         error
	)

	Context("When generating commands", func() {
		JustBeforeEach(func() {
			flyExecutor = &FlyExecutor{}
			cmds, err = flyExecutor.Command(fly)
			args = cmds[0].Args
			exposeArgs = cmds[1].Args
		})

		Context("for a given fly config", func() {
			BeforeEach(func() {
				fly = aviator.Fly{
					Name:       "pipeline-name",
					Target:     "target-name",
					Config:     "pipeline.yml",
					CheckCreds: true,
					Expose:     true,
					Vars:       []string{"credentials.yml", "props.yml"},
				}
			})

			It("shouldn't error", func() {
				Expect(err).ToNot(HaveOccurred())
			})

			It("should generate two commands", func() {
				Expect(cmds).To(HaveLen(2))
			})

			It("generates the set-pipeline command with the 'target' flag and the right target", func() {
				Expect(args).To(ContainElement("--target"))
				Expect(args).To(ContainElement("target-name"))
			})

			It("generates the set-pipeline command with the 'pipeline' flag and the right pipeline", func() {
				Expect(args).To(ContainElement("--pipeline"))
				Expect(args).To(ContainElement("pipeline-name"))
			})

			It("generates the set-pipeline command with the 'config' flag and the right config file", func() {
				Expect(args).To(ContainElement("--config"))
				Expect(args).To(ContainElement("pipeline.yml"))
			})

			It("generates the set-pipeline command with the 'load-vars-from' flag and the right files", func() {
				Expect(args).To(ContainElement("--load-vars-from"))
				Expect(args).To(ContainElement("credentials.yml"))
				Expect(args).To(ContainElement("props.yml"))
			})

			It("generates the set-pipeline command including the '--check-creds' flag", func() {
				Expect(args).To(ContainElement("--check-creds"))
			})

			It("should create the expose command with pipeline name", func() {
				Expect(exposeArgs).To(ContainElement("expose-pipeline"))
				Expect(exposeArgs).To(ContainElement("--target"))
				Expect(exposeArgs).To(ContainElement("target-name"))
				Expect(exposeArgs).To(ContainElement("--pipeline"))
				Expect(exposeArgs).To(ContainElement("pipeline-name"))
			})
		})

		Context("When expose is not set (or false)", func() {
			BeforeEach(func() {
				fly = aviator.Fly{
					Name:   "pipeline-name",
					Target: "target-name",
					Config: "pipeline.yml",
				}
			})

			It("should generate two commands", func() {
				Expect(cmds).To(HaveLen(2))
			})

			It("should generate a hide-pipeline command", func() {
				Expect(exposeArgs).To(ContainElement("hide-pipeline"))
			})

			It("should add the --target flag following by the right target", func() {
				Expect(exposeArgs).To(ContainElement("--target"))
				Expect(exposeArgs).To(ContainElement("target-name"))
			})

			It("should add the pipeline flag following by the right pipeline name", func() {
				Expect(exposeArgs).To(ContainElement("--pipeline"))
				Expect(exposeArgs).To(ContainElement("pipeline-name"))
			})
		})
	})
})
<|endoftext|>


<commit_before>package cli

import (
	"os"
	"testing"

	"github.com/giantswarm/formica/controller"
	"github.com/giantswarm/formica/file-system/fake"
)

type testFileSystemSetup struct {
	FileName    string
	FileContent []byte
	FilePerm    os.FileMode
}

func Test_Common_createRequestWithContent(t *testing.T) {
	testCases := []struct {
		Setup    []testFileSystemSetup
		Input    []string
		Error    error
		Expected controller.Request
	}{
		// This test ensures that loading a single unit from a directory results in
		// the expected controller request.
		{
			Setup: []testFileSystemSetup{
				{
					FileName:    "dirname/dirname_unit.service",
					FileContent: []byte("some unit content"),
					FilePerm:    os.FileMode(0644),
				},
			},
			Input: []string{"dirname"},
			Error: nil,
			Expected: controller.Request{
				SliceIDs: []string{},
				Units: []controller.Unit{
					{
						Name:    "dirname_unit.service",
						Content: "some unit content",
					},
				},
			},
		},

		// This test ensures that trying to load unit files with invalid input
		// throws an error.
		{
			Setup:    []testFileSystemSetup{},
			Input:    []string{},
			Error:    invalidArgumentsError,
			Expected: controller.Request{},
		},

		// This test ensures that loading a single unit from a directory with the
		// slice expression "@1" results in the expected controller request.
		{
			Setup: []testFileSystemSetup{
				{
					FileName:    "dirname/dirname_unit@.service",
					FileContent: []byte("some unit content"),
					FilePerm:    os.FileMode(0644),
				},
			},
			Input: []string{"dirname@1"},
			Error: nil,
			Expected: controller.Request{
				SliceIDs: []string{"1"},
				Units: []controller.Unit{
					{
						Name:    "dirname_unit@.service",
						Content: "some unit content",
					},
				},
			},
		},

		// This test ensures that loading a single unit from a directory with the
		// slice expression "@1", "@foo" and "@5" results in the expected
		// controller request.
		{
			Setup: []testFileSystemSetup{
				{
					FileName:    "dirname/dirname_unit@.service",
					FileContent: []byte("some unit content"),
					FilePerm:    os.FileMode(0644),
				},
			},
			Input: []string{"dirname@1", "dirname@foo", "dirname@5"},
			Error: nil,
			Expected: controller.Request{
				SliceIDs: []string{"1", "foo", "5"},
				Units: []controller.Unit{
					{
						Name:    "dirname_unit@.service",
						Content: "some unit content",
					},
				},
			},
		},
	}

	for i, testCase := range testCases {
		newFileSystem = filesystemfake.NewFileSystem()

		for _, setup := range testCase.Setup {
			err := newFileSystem.WriteFile(setup.FileName, setup.FileContent, setup.FilePerm)
			if err != nil {
				t.Fatalf("FileSystem.WriteFile returned error: %#v", err)
			}
		}

		output, err := createRequestWithContent(testCase.Input)
		if testCase.Error != nil && err.Error() != testCase.Error.Error() {
			t.Fatalf("createRequestWithContent was expected to return error: %#v", testCase.Error)
		}

		if len(output.SliceIDs) != len(testCase.Expected.SliceIDs) {
			t.Fatalf("(test case %d) sliceIDs of generated output differs from expected sliceIDs", i+1)
		}

		for i, outputUnit := range output.Units {
			if outputUnit.Name != testCase.Expected.Units[i].Name {
				t.Fatalf("output unit name '%s' is not equal to expected unit name '%s'", outputUnit.Name, testCase.Expected.Units[i].Name)
			}
		}
	}
}

func Test_Common_createRequest(t *testing.T) {
	testCases := []struct {
		Input    []string
		Error    error
		Expected controller.Request
	}{
		// This test ensures that loading a single unit from a directory results in
		// the expected controller request.
		{
			Input: []string{"dirname"},
			Error: nil,
			Expected: controller.Request{
				SliceIDs: []string{},
				Units: []controller.Unit{
					{
						Name:    "dirname_unit.service",
						Content: "some unit content",
					},
				},
			},
		},

		// This test ensures that trying to load unit files with invalid input
		// throws an error.
		{
			Input:    []string{},
			Error:    invalidArgumentsError,
			Expected: controller.Request{},
		},

		// This test ensures that loading a single unit from a directory with the
		// slice expression "@1" results in the expected controller request.
		{
			Input: []string{"dirname@1"},
			Error: nil,
			Expected: controller.Request{
				SliceIDs: []string{"1"},
				Units: []controller.Unit{
					{
						Name:    "dirname_unit@.service",
						Content: "some unit content",
					},
				},
			},
		},

		// This test ensures that loading a single unit from a directory with the
		// slice expression "@1", "@foo" and "@5" results in the expected
		// controller request.
		{
			Input: []string{"dirname@1", "dirname@foo", "dirname@5"},
			Expected: controller.Request{
				SliceIDs: []string{"1", "foo", "5"},
				Units: []controller.Unit{
					{
						Name:    "dirname_unit@.service",
						Content: "some unit content",
					},
				},
			},
		},
	}

	for i, testCase := range testCases {
		output, err := createRequest(testCase.Input)
		if testCase.Error != nil && err.Error() != testCase.Error.Error() {
			t.Fatalf("createRequest was expected to return error: %#v", testCase.Error)
		}

		if len(output.SliceIDs) != len(testCase.Expected.SliceIDs) {
			t.Fatalf("(test case %d) sliceIDs of generated output differs from expected sliceIDs", i+1)
		}

		for i, outputUnit := range output.Units {
			if outputUnit.Name != testCase.Expected.Units[i].Name {
				t.Fatalf("output unit name '%s' is not equal to expected unit name '%s'", outputUnit.Name, testCase.Expected.Units[i].Name)
			}
		}
	}
}
<commit_msg>tests<commit_after>package cli

import (
	"fmt"
	"os"
	"testing"

	"github.com/juju/errgo"

	"github.com/giantswarm/formica/controller"
	"github.com/giantswarm/formica/file-system/fake"
)

type testFileSystemSetup struct {
	FileName    string
	FileContent []byte
	FilePerm    os.FileMode
}

func Test_Common_createRequestWithContent(t *testing.T) {
	testCases := []struct {
		Setup    []testFileSystemSetup
		Input    []string
		Error    error
		Expected controller.Request
	}{
		// This test ensures that loading a single unit from a directory results in
		// the expected controller request.
		{
			Setup: []testFileSystemSetup{
				{
					FileName:    "dirname/dirname_unit.service",
					FileContent: []byte("some unit content"),
					FilePerm:    os.FileMode(0644),
				},
			},
			Input: []string{"dirname"},
			Error: nil,
			Expected: controller.Request{
				SliceIDs: []string{},
				Units: []controller.Unit{
					{
						Name:    "dirname_unit.service",
						Content: "some unit content",
					},
				},
			},
		},

		// This test ensures that trying to load unit files with invalid input
		// throws an error.
		{
			Setup:    []testFileSystemSetup{},
			Input:    []string{},
			Error:    invalidArgumentsError,
			Expected: controller.Request{},
		},

		// This test ensures that trying to load unit files when no files are in
		// the file system throws an error.
		{
			Setup: []testFileSystemSetup{},
			Input: []string{"dirname"},
			Error: &os.PathError{
				Op:   "open",
				Path: "dirname",
				Err:  errgo.New("no such file or directory"),
			},
			Expected: controller.Request{},
		},

		// This test ensures that loading a single unit from a directory with the
		// slice expression "@1" results in the expected controller request.
		{
			Setup: []testFileSystemSetup{
				{
					FileName:    "dirname/dirname_unit@.service",
					FileContent: []byte("some unit content"),
					FilePerm:    os.FileMode(0644),
				},
			},
			Input: []string{"dirname@1"},
			Error: nil,
			Expected: controller.Request{
				SliceIDs: []string{"1"},
				Units: []controller.Unit{
					{
						Name:    "dirname_unit@.service",
						Content: "some unit content",
					},
				},
			},
		},

		// This test ensures that loading a single unit from a directory with the
		// slice expression "@1", "@foo" and "@5" results in the expected
		// controller request.
		{
			Setup: []testFileSystemSetup{
				{
					FileName:    "dirname/dirname_unit@.service",
					FileContent: []byte("some unit content"),
					FilePerm:    os.FileMode(0644),
				},
			},
			Input: []string{"dirname@1", "dirname@foo", "dirname@5"},
			Error: nil,
			Expected: controller.Request{
				SliceIDs: []string{"1", "foo", "5"},
				Units: []controller.Unit{
					{
						Name:    "dirname_unit@.service",
						Content: "some unit content",
					},
				},
			},
		},
	}

	for i, testCase := range testCases {
		newFileSystem = filesystemfake.NewFileSystem()

		for _, setup := range testCase.Setup {
			err := newFileSystem.WriteFile(setup.FileName, setup.FileContent, setup.FilePerm)
			if err != nil {
				t.Fatalf("FileSystem.WriteFile returned error: %#v", err)
			}
		}

		output, err := createRequestWithContent(testCase.Input)
		fmt.Printf("output: %#v\n", output)
		fmt.Printf("err: %#v\n", err)
		if testCase.Error != nil && err.Error() != testCase.Error.Error() {
			t.Fatalf("(test case %d) createRequestWithContent was expected to return error: %#v", i+1, testCase.Error)
		}

		if len(output.SliceIDs) != len(testCase.Expected.SliceIDs) {
			t.Fatalf("(test case %d) sliceIDs of generated output differs from expected sliceIDs", i+1)
		}

		for i, outputUnit := range output.Units {
			if outputUnit.Name != testCase.Expected.Units[i].Name {
				t.Fatalf("output unit name '%s' is not equal to expected unit name '%s'", outputUnit.Name, testCase.Expected.Units[i].Name)
			}
		}
	}
}

func Test_Common_createRequest(t *testing.T) {
	testCases := []struct {
		Input    []string
		Error    error
		Expected controller.Request
	}{
		// This test ensures that loading a single unit from a directory results in
		// the expected controller request.
		{
			Input: []string{"dirname"},
			Error: nil,
			Expected: controller.Request{
				SliceIDs: []string{},
				Units: []controller.Unit{
					{
						Name:    "dirname_unit.service",
						Content: "some unit content",
					},
				},
			},
		},

		// This test ensures that trying to load unit files with invalid input
		// throws an error.
		{
			Input:    []string{},
			Error:    invalidArgumentsError,
			Expected: controller.Request{},
		},

		// This test ensures that loading a single unit from a directory with the
		// slice expression "@1" results in the expected controller request.
		{
			Input: []string{"dirname@1"},
			Error: nil,
			Expected: controller.Request{
				SliceIDs: []string{"1"},
				Units: []controller.Unit{
					{
						Name:    "dirname_unit@.service",
						Content: "some unit content",
					},
				},
			},
		},

		// This test ensures that loading a single unit from a directory with the
		// slice expression "@1", "@foo" and "@5" results in the expected
		// controller request.
		{
			Input: []string{"dirname@1", "dirname@foo", "dirname@5"},
			Expected: controller.Request{
				SliceIDs: []string{"1", "foo", "5"},
				Units: []controller.Unit{
					{
						Name:    "dirname_unit@.service",
						Content: "some unit content",
					},
				},
			},
		},
	}

	for i, testCase := range testCases {
		output, err := createRequest(testCase.Input)
		if testCase.Error != nil && err.Error() != testCase.Error.Error() {
			t.Fatalf("createRequest was expected to return error: %#v", testCase.Error)
		}

		if len(output.SliceIDs) != len(testCase.Expected.SliceIDs) {
			t.Fatalf("(test case %d) sliceIDs of generated output differs from expected sliceIDs", i+1)
		}

		for i, outputUnit := range output.Units {
			if outputUnit.Name != testCase.Expected.Units[i].Name {
				t.Fatalf("output unit name '%s' is not equal to expected unit name '%s'", outputUnit.Name, testCase.Expected.Units[i].Name)
			}
		}
	}
}
<|endoftext|>


<commit_before>package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func readStreamer() <-chan string {
	stream := make(chan string)
	reader := bufio.NewReader(os.Stdin)
	go func() {
		for {
			fmt.Print("Enter message to send: ")
			text, err := reader.ReadString('\n')
			if err == io.EOF {
				log.Fatal("DONE")
			} else if err != nil {
				log.Fatal("Unable to read from stdin: %s", err)
			}

			// Never send an empty string since that's what closed channels do
			if text != "" {
				stream <- text
			}
		}
	}()

	return stream
}
<commit_msg>remove trailing newlines on messages<commit_after>package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strings"
)

func readStreamer() <-chan string {
	stream := make(chan string)
	reader := bufio.NewReader(os.Stdin)
	go func() {
		for {
			fmt.Print("Enter message to send: ")
			text, err := reader.ReadString('\n')
			if err == io.EOF {
				log.Fatal("DONE")
			} else if err != nil {
				log.Fatal("Unable to read from stdin: %s", err)
			}

			text = strings.TrimSpace(text)

			// Never send an empty string since that's what closed channels do
			if text != "" {
				stream <- text
			}
		}
	}()

	return stream
}
<|endoftext|>


<commit_before>// Package mock provides a mock client for testing
package mock

import (
	"context"
	"fmt"
	"reflect"
	"sync"

	"github.com/micro/go-micro/client"
	"github.com/micro/go-micro/errors"
)

var (
	_ client.Client = NewClient()
)

type MockResponse struct {
	Endpoint string
	Response interface{}
	Error    error
}

type MockClient struct {
	Client client.Client
	Opts   client.Options

	sync.Mutex
	Response map[string][]MockResponse
}

func (m *MockClient) Init(opts ...client.Option) error {
	m.Lock()
	defer m.Unlock()

	for _, opt := range opts {
		opt(&m.Opts)
	}

	r, ok := fromContext(m.Opts.Context)
	if !ok {
		r = make(map[string][]MockResponse)
	}
	m.Response = r

	return nil
}

func (m *MockClient) Options() client.Options {
	return m.Opts
}

func (m *MockClient) NewMessage(topic string, msg interface{}, opts ...client.MessageOption) client.Message {
	return m.Client.NewMessage(topic, msg, opts...)
}

func (m *MockClient) NewRequest(service, endpoint string, req interface{}, reqOpts ...client.RequestOption) client.Request {
	return m.Client.NewRequest(service, endpoint, req, reqOpts...)
}

func (m *MockClient) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
	m.Lock()
	defer m.Unlock()

	response, ok := m.Response[req.Service()]
	if !ok {
		return errors.NotFound("go.micro.client.mock", "service not found")
	}

	for _, r := range response {
		if r.Endpoint != req.Endpoint() {
			continue
		}

		if r.Error != nil {
			return r.Error
		}

		v := reflect.ValueOf(rsp)

		if t := reflect.TypeOf(rsp); t.Kind() == reflect.Ptr {
			v = reflect.Indirect(v)
		}
		response := r.Response
		if t := reflect.TypeOf(r.Response); t.Kind() == reflect.Func {
			response = reflect.ValueOf(r.Response).Call([]reflect.Value{})[0].Interface()
		}

		v.Set(reflect.ValueOf(response))

		return nil
	}

	return fmt.Errorf("rpc: can't find service %s", req.Endpoint())
}

func (m *MockClient) Stream(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Stream, error) {
	m.Lock()
	defer m.Unlock()

	// TODO: mock stream
	return nil, nil
}

func (m *MockClient) Publish(ctx context.Context, p client.Message, opts ...client.PublishOption) error {
	return nil
}

func (m *MockClient) String() string {
	return "mock"
}

func NewClient(opts ...client.Option) *MockClient {
	options := client.Options{
		Context: context.TODO(),
	}

	for _, opt := range opts {
		opt(&options)
	}

	r, ok := fromContext(options.Context)
	if !ok {
		r = make(map[string][]MockResponse)
	}

	return &MockClient{
		Client:   client.DefaultClient,
		Opts:     options,
		Response: r,
	}
}
<commit_msg>send requestBody to mock function if it can handle it<commit_after>// Package mock provides a mock client for testing
package mock

import (
	"context"
	"fmt"
	"reflect"
	"sync"

	"github.com/micro/go-micro/client"
	"github.com/micro/go-micro/errors"
)

var (
	_ client.Client = NewClient()
)

type MockResponse struct {
	Endpoint string
	Response interface{}
	Error    error
}

type MockClient struct {
	Client client.Client
	Opts   client.Options

	sync.Mutex
	Response map[string][]MockResponse
}

func (m *MockClient) Init(opts ...client.Option) error {
	m.Lock()
	defer m.Unlock()

	for _, opt := range opts {
		opt(&m.Opts)
	}

	r, ok := fromContext(m.Opts.Context)
	if !ok {
		r = make(map[string][]MockResponse)
	}
	m.Response = r

	return nil
}

func (m *MockClient) Options() client.Options {
	return m.Opts
}

func (m *MockClient) NewMessage(topic string, msg interface{}, opts ...client.MessageOption) client.Message {
	return m.Client.NewMessage(topic, msg, opts...)
}

func (m *MockClient) NewRequest(service, endpoint string, req interface{}, reqOpts ...client.RequestOption) client.Request {
	return m.Client.NewRequest(service, endpoint, req, reqOpts...)
}

func (m *MockClient) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
	m.Lock()
	defer m.Unlock()

	response, ok := m.Response[req.Service()]
	if !ok {
		return errors.NotFound("go.micro.client.mock", "service not found")
	}

	for _, r := range response {
		if r.Endpoint != req.Endpoint() {
			continue
		}

		if r.Error != nil {
			return r.Error
		}

		v := reflect.ValueOf(rsp)

		if t := reflect.TypeOf(rsp); t.Kind() == reflect.Ptr {
			v = reflect.Indirect(v)
		}
		response := r.Response
		if t := reflect.TypeOf(r.Response); t.Kind() == reflect.Func {
			var reqBody []reflect.Value
			if t.NumIn() == 0 {
				reqBody = append(reqBody, reflect.ValueOf(req.Body()))
			}
			response = reflect.ValueOf(r.Response).Call(reqBody)[0].Interface()
		}

		v.Set(reflect.ValueOf(response))

		return nil
	}

	return fmt.Errorf("rpc: can't find service %s", req.Endpoint())
}

func (m *MockClient) Stream(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Stream, error) {
	m.Lock()
	defer m.Unlock()

	// TODO: mock stream
	return nil, nil
}

func (m *MockClient) Publish(ctx context.Context, p client.Message, opts ...client.PublishOption) error {
	return nil
}

func (m *MockClient) String() string {
	return "mock"
}

func NewClient(opts ...client.Option) *MockClient {
	options := client.Options{
		Context: context.TODO(),
	}

	for _, opt := range opts {
		opt(&options)
	}

	r, ok := fromContext(options.Context)
	if !ok {
		r = make(map[string][]MockResponse)
	}

	return &MockClient{
		Client:   client.DefaultClient,
		Opts:     options,
		Response: r,
	}
}
<|endoftext|>


<commit_before>package window

/*
#include <stdlib.h>
#include <SDL2/SDL.h>

int getKeyState(SDL_Keycode key) {
  const Uint8 *state = SDL_GetKeyboardState(NULL);
  SDL_Scancode scancode = SDL_GetScancodeFromKey(key);

  if (state[scancode]){
    return SDL_PRESSED;
  }
  return SDL_RELEASED;
}
*/
import "C"

var listenerList = map[int]*listener{}

type listener struct {
	callback func(event int)
}

// AddListener creates a new key listener, only the last listener for a button will be honored
//	input.AddListener(input.KeyEscape, func(event int) {
//		if event == input.Release {
//			fmt.Println("Escape button released!")
//		}
//	})
func AddListener(key int, callback func(event int)) {
	listenerList[key] = &listener{callback}
}

// DestroyListener removes listener for a key
func DestroyListener(key int) {
	if _, ok := listenerList[key]; ok {
		listenerList[key].callback = func(event int) {}
	}
}

// GetKeyState will return the event state for a key
func GetKeyState(key int) int {
	return int(C.getKeyState(C.SDL_Keycode(key)))
}
<commit_msg>remove useless include<commit_after>package window

/*
#include <SDL2/SDL.h>

int getKeyState(SDL_Keycode key) {
  const Uint8 *state = SDL_GetKeyboardState(NULL);
  SDL_Scancode scancode = SDL_GetScancodeFromKey(key);

  if (state[scancode]){
    return SDL_PRESSED;
  }
  return SDL_RELEASED;
}
*/
import "C"

var listenerList = map[int]*listener{}

type listener struct {
	callback func(event int)
}

// AddListener creates a new key listener, only the last listener for a button will be honored
//	input.AddListener(input.KeyEscape, func(event int) {
//		if event == input.Release {
//			fmt.Println("Escape button released!")
//		}
//	})
func AddListener(key int, callback func(event int)) {
	listenerList[key] = &listener{callback}
}

// DestroyListener removes listener for a key
func DestroyListener(key int) {
	if _, ok := listenerList[key]; ok {
		listenerList[key].callback = func(event int) {}
	}
}

// GetKeyState will return the event state for a key
func GetKeyState(key int) int {
	return int(C.getKeyState(C.SDL_Keycode(key)))
}
<|endoftext|>


<commit_before>//
// addr.go -- address tools
//
package samtun

import (
  "bitbucket.org/majestrate/sam3"
  "log"
  "net"
)
// maps b32 -> ip
type addrMap map[string]string

// given ip get b32
func (m addrMap) IP(b32 string) (ip string) {
  ip, _ = m[b32]
  return
}

// given b32 get ip
func (m addrMap) B32(ip string) string {
  for k, v := range m {
    if v == ip {
      return k
    }
  }
  return ""
}

// take a link message and filter the packets
// return a link frame that has corrected addresses
// returns nil if we have a packet from someone unmapped
func (m addrMap) filterMessage(msg linkMessage, ourAddr sam3.I2PAddr) (pkt ipPacket) {
  dst := net.ParseIP(m.IP(ourAddr.Base32()))
  src := net.ParseIP(m.IP(msg.addr.Base32()))
  if dst == nil || src == nil {
    // bad address
    return
  } else {

    if msg.pkt == nil || len(msg.pkt) < 20 {
      // back packet
      log.Println("short packet from", src, len(pkt), "bytes")
      return
    } else {
      log.Println(src, "to", dst)
      msg.pkt.setDst(dst)
      msg.pkt.setSrc(src)
      return msg.pkt
    }
  }
  return
}
<commit_msg>fix check<commit_after>//
// addr.go -- address tools
//
package samtun

import (
  "bitbucket.org/majestrate/sam3"
  "log"
  "net"
)
// maps b32 -> ip
type addrMap map[string]string

// given ip get b32
func (m addrMap) IP(b32 string) (ip string) {
  ip, _ = m[b32]
  return
}

// given b32 get ip
func (m addrMap) B32(ip string) string {
  for k, v := range m {
    if v == ip {
      return k
    }
  }
  return ""
}

// take a link message and filter the packets
// return a link frame that has corrected addresses
// returns nil if we have a packet from someone unmapped
func (m addrMap) filterMessage(msg linkMessage, ourAddr sam3.I2PAddr) (pkt ipPacket) {
  dst := net.ParseIP(m.IP(ourAddr.Base32()))
  src := net.ParseIP(m.IP(msg.addr.Base32()))
  if dst == nil || src == nil {
    // bad address
    return
  } else {

    if msg.pkt == nil || len(msg.pkt) < 20 {
      // back packet
      log.Println("short packet from", src, len(pkt), "bytes")
      return
    } else {
      log.Println(src, "to", dst)
      if msg.pkt.Dst().Equal(dst) && msg.pkt.Src().Equal(src) {
        return msg.pkt
      }
    }
  }
  return
}
<|endoftext|>


<commit_before>// Copyright 2011 Xing Xing <mikespook@gmail.com> All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.

package worker

import (
    "io"
    "net"
    "github.com/mikespook/gearman-go/common"
)

// The agent of job server.
type agent struct {
    conn        net.Conn
    worker      *Worker
    in chan     []byte
    out chan    *Job
    addr string
}

// Create the agent of job server.
func newAgent(addr string, worker *Worker) (a *agent, err error) {
    conn, err := net.Dial(common.NETWORK, addr)
    if err != nil {
        return
    }
    a = &agent{
        conn: conn,
        worker: worker,
        addr: addr,
        in: make(chan []byte, common.QUEUE_SIZE),
        out: make(chan *Job, common.QUEUE_SIZE),
    }
    // reset abilities
    a.WriteJob(newJob(common.REQ, common.RESET_ABILITIES, nil))
    return
}

// outputing loop
func (a *agent) outLoop() {
    ok := true
    var job *Job
    for ok {
        if job, ok = <-a.out; ok {
            if err := a.write(job.Encode()); err != nil {
                a.worker.err(err)
            }
        }
    }
}

// inputing loop
func (a *agent) inLoop() {
    defer func() {
        if r := recover(); r != nil {
            a.worker.err(common.Errorf("Exiting: %s", r))
        }
        close(a.in)
        close(a.out)
        a.worker.removeAgent(a)
    }()
    for a.worker.running {
        a.WriteJob(newJob(common.REQ, common.PRE_SLEEP, nil))
        RESTART:
        // got noop msg and in queue is zero, grab job
        rel, err := a.read()
        if err != nil {
            if err == common.ErrConnection {
                for i:= 0; i < 3 && a.worker.running; i++ {
                    if conn, err := net.Dial(common.NETWORK, a.addr); err != nil {
                        a.worker.err(common.Errorf("Reconnection: %d faild", i))
                        continue
                    } else {
                        a.conn = conn
                        goto RESTART
                    }
                }
                a.worker.err(err)
                break
            }
            a.worker.err(err)
            continue
        }
        job, err := decodeJob(rel)
        if err != nil {
            a.worker.err(err)
            continue
        }
        switch job.DataType {
        case common.NOOP:
            a.WriteJob(newJob(common.REQ, common.GRAB_JOB_UNIQ, nil))
        case common.ERROR, common.ECHO_RES, common.JOB_ASSIGN_UNIQ, common.JOB_ASSIGN:
            job.agent = a
            a.worker.in <- job
        }
    }
}

func (a *agent) Close() {
    a.conn.Close()
}

func (a *agent) Work() {
    go a.outLoop()
    go a.inLoop()
}

// Internal read
func (a *agent) read() (data []byte, err error) {
    if len(a.in) > 0 {
        // in queue is not empty
        data = <-a.in
    } else {
        for {
            buf := make([]byte, common.BUFFER_SIZE)
            var n int
            if n, err = a.conn.Read(buf); err != nil {
                if err == io.EOF && n == 0 {
                    if data == nil {
                        err = common.ErrConnection
                        return
                    }
                    break
                }
                return
            }
            data = append(data, buf[0:n]...)
            if n < common.BUFFER_SIZE {
                break
            }
        }
    }
    // split package
    tl := len(data)
    start := 0
    for i := 0; i < tl; i++ {
        if string(data[start:start+4]) == common.RES_STR {
            l := int(common.BytesToUint32([4]byte{data[start+8],
                data[start+9], data[start+10], data[start+11]}))
            total := l + 12
            if total == tl {
                return
            } else {
                a.in <- data[total:]
                data = data[:total]
                return
            }
        } else {
            start++
        }
    }
    return nil, common.Errorf("Invalid data: %V", data)
}

// Send a job to the job server.
func (a *agent) WriteJob(job *Job) {
    a.out <- job
}

// Internal write the encoded job.
func (a *agent) write(buf []byte) (err error) {
    var n int
    for i := 0; i < len(buf); i += n {
        n, err = a.conn.Write(buf[i:])
        if err != nil {
            return err
        }
    }
    return
}
<commit_msg>check the size of byte array, greater or lesser<commit_after>// Copyright 2011 Xing Xing <mikespook@gmail.com> All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.

package worker

import (
    "io"
    "net"
    "github.com/mikespook/gearman-go/common"
)

// The agent of job server.
type agent struct {
    conn        net.Conn
    worker      *Worker
    in chan     []byte
    out chan    *Job
    addr string
}

// Create the agent of job server.
func newAgent(addr string, worker *Worker) (a *agent, err error) {
    conn, err := net.Dial(common.NETWORK, addr)
    if err != nil {
        return
    }
    a = &agent{
        conn: conn,
        worker: worker,
        addr: addr,
        in: make(chan []byte, common.QUEUE_SIZE),
        out: make(chan *Job, common.QUEUE_SIZE),
    }
    // reset abilities
    a.WriteJob(newJob(common.REQ, common.RESET_ABILITIES, nil))
    return
}

// outputing loop
func (a *agent) outLoop() {
    ok := true
    var job *Job
    for ok {
        if job, ok = <-a.out; ok {
            if err := a.write(job.Encode()); err != nil {
                a.worker.err(err)
            }
        }
    }
}

// inputing loop
func (a *agent) inLoop() {
    defer func() {
        if r := recover(); r != nil {
            a.worker.err(common.Errorf("Exiting: %s", r))
        }
        close(a.in)
        close(a.out)
        a.worker.removeAgent(a)
    }()
    for a.worker.running {
        a.WriteJob(newJob(common.REQ, common.PRE_SLEEP, nil))
        RESTART:
        // got noop msg and in queue is zero, grab job
        rel, err := a.read()
        if err != nil {
            if err == common.ErrConnection {
                for i:= 0; i < 3 && a.worker.running; i++ {
                    if conn, err := net.Dial(common.NETWORK, a.addr); err != nil {
                        a.worker.err(common.Errorf("Reconnection: %d faild", i))
                        continue
                    } else {
                        a.conn = conn
                        goto RESTART
                    }
                }
                a.worker.err(err)
                break
            }
            a.worker.err(err)
            continue
        }
        job, err := decodeJob(rel)
        if err != nil {
            a.worker.err(err)
            continue
        }
        switch job.DataType {
        case common.NOOP:
            a.WriteJob(newJob(common.REQ, common.GRAB_JOB_UNIQ, nil))
        case common.ERROR, common.ECHO_RES, common.JOB_ASSIGN_UNIQ, common.JOB_ASSIGN:
            job.agent = a
            a.worker.in <- job
        }
    }
}

func (a *agent) Close() {
    a.conn.Close()
}

func (a *agent) Work() {
    go a.outLoop()
    go a.inLoop()
}

// Internal read
func (a *agent) read() (data []byte, err error) {
    if len(a.in) > 0 {
        // in queue is not empty
        data = <-a.in
    } else {
        for {
            buf := make([]byte, common.BUFFER_SIZE)
            var n int
            if n, err = a.conn.Read(buf); err != nil {
                if err == io.EOF && n == 0 {
                    if data == nil {
                        err = common.ErrConnection
                        return
                    }
                    break
                }
                return
            }
            data = append(data, buf[0:n]...)
            if n < common.BUFFER_SIZE {
                break
            }
        }
    }
    // split package
    tl := len(data)
    start := 0
    for i := 0; i < tl; i++ {
        if string(data[start:start+4]) == common.RES_STR {
            l := int(common.BytesToUint32([4]byte{data[start+8],
                data[start+9], data[start+10], data[start+11]}))
            total := l + 12
            if total == tl { // data is what we want
                return
            } else if total < tl{ // data[:total] is what we want, data[total:] is the more
                a.in <- data[total:]
                data = data[:total]
                return
            } else { // ops!
                break
            }
        } else {
            start++
        }
    }
    return nil, common.Errorf("Invalid data: %V", data)
}

// Send a job to the job server.
func (a *agent) WriteJob(job *Job) {
    a.out <- job
}

// Internal write the encoded job.
func (a *agent) write(buf []byte) (err error) {
    var n int
    for i := 0; i < len(buf); i += n {
        n, err = a.conn.Write(buf[i:])
        if err != nil {
            return err
        }
    }
    return
}
<|endoftext|>


<commit_before>package client

import (
	"sync"

	"github.com/micro/go-micro/transport"
)

type pool struct {
	tr transport.Transport

	sync.Mutex
	conns map[string][]*poolConn
}

type poolConn struct {
	transport.Client
}

var (
	maxIdleConn = 2
)

func newPool() *pool {
	return &pool{
		conns: make(map[string][]*poolConn),
	}
}

// NoOp the Close since we manage it
func (p *poolConn) Close() error {
	return nil
}

func (p *pool) getConn(addr string, tr transport.Transport, opts ...transport.DialOption) (*poolConn, error) {
	p.Lock()
	conns, ok := p.conns[addr]
	// no free conn
	if !ok || len(conns) == 0 {
		p.Unlock()
		// create new conn
		c, err := tr.Dial(addr, opts...)
		if err != nil {
			return nil, err
		}
		return &poolConn{c}, nil
	}

	conn := conns[len(conns)-1]
	p.conns[addr] = conns[:len(conns)-1]
	p.Unlock()
	return conn, nil
}

func (p *pool) release(addr string, conn *poolConn, err error) {
	// don't store the conn
	if err != nil {
		conn.Client.Close()
		return
	}

	// otherwise put it back
	p.Lock()
	conns := p.conns[addr]
	if len(conns) >= maxIdleConn {
		p.Unlock()
		conn.Client.Close()
		return
	}
	p.conns[addr] = append(conns, conn)
	p.Unlock()
}
<commit_msg>Add a conn lifetime for the pool<commit_after>package client

import (
	"sync"
	"time"

	"github.com/micro/go-micro/transport"
)

type pool struct {
	tr transport.Transport

	sync.Mutex
	conns map[string][]*poolConn
}

type poolConn struct {
	transport.Client
	created int64
}

var (
	// only hold on to this many conns
	maxIdleConn = 2
	// only hold on to the conn for this period
	maxLifeTime = int64(60)
)

func newPool() *pool {
	return &pool{
		conns: make(map[string][]*poolConn),
	}
}

// NoOp the Close since we manage it
func (p *poolConn) Close() error {
	return nil
}

func (p *pool) getConn(addr string, tr transport.Transport, opts ...transport.DialOption) (*poolConn, error) {
	p.Lock()
	conns := p.conns[addr]
	now := time.Now().Unix()

	// while we have conns check age and then return one
	// otherwise we'll create a new conn
	for len(conns) > 0 {
		conn := conns[len(conns)-1]
		conns = conns[:len(conns)-1]
		p.conns[addr] = conns

		// if conn is old kill it and move on
		if d := now - conn.created; d > maxLifeTime {
			conn.Client.Close()
			continue
		}

		// we got a good conn, lets unlock and return it
		p.Unlock()

		return conn, nil
	}

	p.Unlock()

	// create new conn
	c, err := tr.Dial(addr, opts...)
	if err != nil {
		return nil, err
	}
	return &poolConn{c, time.Now().Unix()}, nil
}

func (p *pool) release(addr string, conn *poolConn, err error) {
	// don't store the conn if it has errored
	if err != nil {
		conn.Client.Close()
		return
	}

	// otherwise put it back for reuse
	p.Lock()
	conns := p.conns[addr]
	if len(conns) >= maxIdleConn {
		p.Unlock()
		conn.Client.Close()
		return
	}
	p.conns[addr] = append(conns, conn)
	p.Unlock()
}
<|endoftext|>


<commit_before>package schema

import (
	"bufio"
	"bytes"
	"camli/blobref"
	"crypto/sha1"
	"fmt"
	"io"
	"json"
	"log"
	"os"
	"rand"
	"strconv"
	"strings"
	"sync"
	"time"
)

var NoCamliVersionError = os.NewError("No camliVersion key in map")
var UnimplementedError = os.NewError("Unimplemented")

type StatHasher interface {
	Lstat(fileName string) (*os.FileInfo, os.Error)
	Hash(fileName string) (*blobref.BlobRef, os.Error)
}

var DefaultStatHasher = &defaultStatHasher{}

type defaultStatHasher struct{}

func (d *defaultStatHasher) Lstat(fileName string) (*os.FileInfo, os.Error) {
	return os.Lstat(fileName)
}

func (d *defaultStatHasher) Hash(fileName string) (*blobref.BlobRef, os.Error) {
	s1 := sha1.New()
	file, err := os.Open(fileName, os.O_RDONLY, 0)
	if err != nil {
		return nil, err
	}
	defer file.Close()
	_, err = io.Copy(s1, file)
        if err != nil {
                return nil, err
        }
	return blobref.FromHash("sha1", s1), nil
}

type StaticSet struct {
	l  sync.Mutex
	refs []*blobref.BlobRef
}

func (ss *StaticSet) Add(ref *blobref.BlobRef) {
	ss.l.Lock()
	defer ss.l.Unlock()
	ss.refs = append(ss.refs, ref)
}

func newCamliMap(version int, ctype string) map[string]interface{} {
	m := make(map[string]interface{})
        m["camliVersion"] = version
        m["camliType"] = ctype
	return m
}

func NewUnsignedPermanode() string {
	m := newCamliMap(1, "permanode")
	chars := make([]byte, 20)
	// Don't need cryptographically secure random here, as this
	// will be GPG signed anyway.
	for idx, _ := range chars {
		chars[idx] = byte(32 + rand.Intn(126 - 32))
	}
	m["random"] = string(chars)
	unsigned, err := MapToCamliJson(m)
	if err != nil {
		log.Panicf("Unexpected error: %v", err)
	}
	return unsigned
}

// Map returns a Camli map of camliType "static-set"
func (ss *StaticSet) Map() map[string]interface{} {
	m := newCamliMap(1, "static-set")
	ss.l.Lock()
	defer ss.l.Unlock()

	members := make([]string, 0, len(ss.refs))
	if ss.refs != nil {
		for _, ref := range ss.refs {
			members = append(members, ref.String())
		}
	}
	m["members"] = members
	return m
}

func MapToCamliJson(m map[string]interface{}) (string, os.Error) {
	version, hasVersion := m["camliVersion"]
	if !hasVersion {
		return "", NoCamliVersionError
	}
	m["camliVersion"] = 0, false
	jsonBytes, err := json.MarshalIndent(m, "", "  ")
	if err != nil {
		return "", err
	}
	m["camliVersion"] = version
	buf := new(bytes.Buffer)
	fmt.Fprintf(buf, "{\"camliVersion\": %v,\n", version)
	buf.Write(jsonBytes[2:])
	return string(buf.Bytes()), nil
}

func NewCommonFileMap(fileName string, fi *os.FileInfo) map[string]interface{} {
	m := newCamliMap(1, "" /* no type yet */)

	lastSlash := strings.LastIndex(fileName, "/")
	baseName := fileName[lastSlash+1:]
	if isValidUtf8(baseName) {
		m["fileName"] = baseName
	} else {
		m["fileNameBytes"] = []uint8(baseName)
	}

	// Common elements (from file-common.txt)
	m["unixPermission"] = fmt.Sprintf("0%o", fi.Permission())
	if fi.Uid != -1 {
		m["unixOwnerId"] = fi.Uid
		if user := getUserFromUid(fi.Uid); user != "" {
			m["unixOwner"] = user
		}
	}
	if fi.Gid != -1 {
		m["unixGroupId"] = fi.Gid
		if group := getGroupFromGid(fi.Gid); group != "" {
			m["unixGroup"] = group
		}
	}
	if mtime := fi.Mtime_ns; mtime != 0 {
		m["unixMtime"] = rfc3339FromNanos(mtime)
	}
	// Include the ctime too, if it differs.
	if ctime := fi.Ctime_ns; ctime != 0 && fi.Mtime_ns != fi.Ctime_ns {
		m["unixCtime"] = rfc3339FromNanos(ctime)
	}

	return m
}

type ContentPart struct {
	BlobRef   *blobref.BlobRef
	Size      int64
	Offset    int64
}

type InvalidContentPartsError struct {
	StatSize   int64
	SumOfParts int64
}

func (e *InvalidContentPartsError) String() string {
	return fmt.Sprintf("Invalid ContentPart slice in PopulateRegularFileMap; file stat size is %d but sum of parts was %d", e.StatSize, e.SumOfParts)
}

func PopulateRegularFileMap(m map[string]interface{}, fi *os.FileInfo, parts []ContentPart) os.Error {
	m["camliType"] = "file"
	m["size"] = fi.Size

	sumSize := int64(0)
	mparts := make([]map[string]interface{}, len(parts))
	for idx, part := range parts {
		mpart := make(map[string]interface{})
		mparts[idx] = mpart
		mpart["blobRef"] = part.BlobRef.String()
		mpart["size"] = part.Size
		sumSize += part.Size
		if part.Offset != 0 {
			mpart["offset"] = part.Offset
		}
	}
	if sumSize != fi.Size {
		return &InvalidContentPartsError{fi.Size, sumSize}
	}
	m["contentParts"] = mparts
	return nil
}

func PopulateSymlinkMap(m map[string]interface{}, fileName string) os.Error {
	m["camliType"] = "symlink"
	target, err := os.Readlink(fileName)
	if err != nil {
		return err
	}
	if isValidUtf8(target) {
		m["symlinkTarget"] = target
	} else {
		m["symlinkTargetBytes"] = []uint8(target)
	}
	return nil
}

func PopulateDirectoryMap(m map[string]interface{}, staticSetRef *blobref.BlobRef) {
	m["camliType"] = "directory"
	m["entries"] = staticSetRef.String()
}

func rfc3339FromNanos(epochnanos int64) string {
	nanos := epochnanos % 1e9
	esec := epochnanos / 1e9
	t := time.SecondsToUTC(esec)
	timeStr := t.Format(time.RFC3339)
	if nanos == 0 {
		return timeStr
	}
	nanoStr := fmt.Sprintf("%09d", nanos)
	nanoStr = strings.TrimRight(nanoStr, "0")
	return timeStr[:len(timeStr)-1] + "." + nanoStr + "Z"
}

func populateMap(m map[int]string, file string) {
	f, err := os.Open(file, os.O_RDONLY, 0)
	if err != nil {
		return
	}
	bufr := bufio.NewReader(f)
	for {
		line, err := bufr.ReadString('\n')
		if err != nil {
			return
		}
		parts := strings.Split(line, ":", 4)
		if len(parts) >= 3 {
			idstr := parts[2]
			id, err := strconv.Atoi(idstr)
			if err == nil {
				m[id] = parts[0]
			}
		}
	}
}

var uidToUsernameMap map[int]string
var getUserFromUidOnce sync.Once
func getUserFromUid(uid int) string {
	getUserFromUidOnce.Do(func() {
		uidToUsernameMap = make(map[int]string)
		populateMap(uidToUsernameMap, "/etc/passwd")
	})
	return uidToUsernameMap[uid]
}

var gidToUsernameMap map[int]string
var getGroupFromGidOnce sync.Once
func getGroupFromGid(uid int) string {
	getGroupFromGidOnce.Do(func() {
		gidToUsernameMap = make(map[int]string)
		populateMap(gidToUsernameMap, "/etc/group")
	})
	return gidToUsernameMap[uid]
}

func isValidUtf8(s string) bool {
	for _, rune := range []int(s) {
		if rune == 0xfffd {
			return false
		}
	}
	return true
}
<commit_msg>seed PRNG on time<commit_after>package schema

import (
	"bufio"
	"bytes"
	"camli/blobref"
	"crypto/sha1"
	"fmt"
	"io"
	"json"
	"log"
	"os"
	"rand"
	"strconv"
	"strings"
	"sync"
	"time"
)

var NoCamliVersionError = os.NewError("No camliVersion key in map")
var UnimplementedError = os.NewError("Unimplemented")

type StatHasher interface {
	Lstat(fileName string) (*os.FileInfo, os.Error)
	Hash(fileName string) (*blobref.BlobRef, os.Error)
}

var DefaultStatHasher = &defaultStatHasher{}

type defaultStatHasher struct{}

func (d *defaultStatHasher) Lstat(fileName string) (*os.FileInfo, os.Error) {
	return os.Lstat(fileName)
}

func (d *defaultStatHasher) Hash(fileName string) (*blobref.BlobRef, os.Error) {
	s1 := sha1.New()
	file, err := os.Open(fileName, os.O_RDONLY, 0)
	if err != nil {
		return nil, err
	}
	defer file.Close()
	_, err = io.Copy(s1, file)
        if err != nil {
                return nil, err
        }
	return blobref.FromHash("sha1", s1), nil
}

type StaticSet struct {
	l  sync.Mutex
	refs []*blobref.BlobRef
}

func (ss *StaticSet) Add(ref *blobref.BlobRef) {
	ss.l.Lock()
	defer ss.l.Unlock()
	ss.refs = append(ss.refs, ref)
}

func newCamliMap(version int, ctype string) map[string]interface{} {
	m := make(map[string]interface{})
        m["camliVersion"] = version
        m["camliType"] = ctype
	return m
}

func NewUnsignedPermanode() string {
	m := newCamliMap(1, "permanode")
	chars := make([]byte, 20)
	// Don't need cryptographically secure random here, as this
	// will be GPG signed anyway.
	rnd := rand.New(rand.NewSource(time.Nanoseconds()))
	for idx, _ := range chars {
		chars[idx] = byte(32 + rnd.Intn(126 - 32))
	}
	m["random"] = string(chars)
	unsigned, err := MapToCamliJson(m)
	if err != nil {
		log.Panicf("Unexpected error: %v", err)
	}
	return unsigned
}

// Map returns a Camli map of camliType "static-set"
func (ss *StaticSet) Map() map[string]interface{} {
	m := newCamliMap(1, "static-set")
	ss.l.Lock()
	defer ss.l.Unlock()

	members := make([]string, 0, len(ss.refs))
	if ss.refs != nil {
		for _, ref := range ss.refs {
			members = append(members, ref.String())
		}
	}
	m["members"] = members
	return m
}

func MapToCamliJson(m map[string]interface{}) (string, os.Error) {
	version, hasVersion := m["camliVersion"]
	if !hasVersion {
		return "", NoCamliVersionError
	}
	m["camliVersion"] = 0, false
	jsonBytes, err := json.MarshalIndent(m, "", "  ")
	if err != nil {
		return "", err
	}
	m["camliVersion"] = version
	buf := new(bytes.Buffer)
	fmt.Fprintf(buf, "{\"camliVersion\": %v,\n", version)
	buf.Write(jsonBytes[2:])
	return string(buf.Bytes()), nil
}

func NewCommonFileMap(fileName string, fi *os.FileInfo) map[string]interface{} {
	m := newCamliMap(1, "" /* no type yet */)

	lastSlash := strings.LastIndex(fileName, "/")
	baseName := fileName[lastSlash+1:]
	if isValidUtf8(baseName) {
		m["fileName"] = baseName
	} else {
		m["fileNameBytes"] = []uint8(baseName)
	}

	// Common elements (from file-common.txt)
	m["unixPermission"] = fmt.Sprintf("0%o", fi.Permission())
	if fi.Uid != -1 {
		m["unixOwnerId"] = fi.Uid
		if user := getUserFromUid(fi.Uid); user != "" {
			m["unixOwner"] = user
		}
	}
	if fi.Gid != -1 {
		m["unixGroupId"] = fi.Gid
		if group := getGroupFromGid(fi.Gid); group != "" {
			m["unixGroup"] = group
		}
	}
	if mtime := fi.Mtime_ns; mtime != 0 {
		m["unixMtime"] = rfc3339FromNanos(mtime)
	}
	// Include the ctime too, if it differs.
	if ctime := fi.Ctime_ns; ctime != 0 && fi.Mtime_ns != fi.Ctime_ns {
		m["unixCtime"] = rfc3339FromNanos(ctime)
	}

	return m
}

type ContentPart struct {
	BlobRef   *blobref.BlobRef
	Size      int64
	Offset    int64
}

type InvalidContentPartsError struct {
	StatSize   int64
	SumOfParts int64
}

func (e *InvalidContentPartsError) String() string {
	return fmt.Sprintf("Invalid ContentPart slice in PopulateRegularFileMap; file stat size is %d but sum of parts was %d", e.StatSize, e.SumOfParts)
}

func PopulateRegularFileMap(m map[string]interface{}, fi *os.FileInfo, parts []ContentPart) os.Error {
	m["camliType"] = "file"
	m["size"] = fi.Size

	sumSize := int64(0)
	mparts := make([]map[string]interface{}, len(parts))
	for idx, part := range parts {
		mpart := make(map[string]interface{})
		mparts[idx] = mpart
		mpart["blobRef"] = part.BlobRef.String()
		mpart["size"] = part.Size
		sumSize += part.Size
		if part.Offset != 0 {
			mpart["offset"] = part.Offset
		}
	}
	if sumSize != fi.Size {
		return &InvalidContentPartsError{fi.Size, sumSize}
	}
	m["contentParts"] = mparts
	return nil
}

func PopulateSymlinkMap(m map[string]interface{}, fileName string) os.Error {
	m["camliType"] = "symlink"
	target, err := os.Readlink(fileName)
	if err != nil {
		return err
	}
	if isValidUtf8(target) {
		m["symlinkTarget"] = target
	} else {
		m["symlinkTargetBytes"] = []uint8(target)
	}
	return nil
}

func PopulateDirectoryMap(m map[string]interface{}, staticSetRef *blobref.BlobRef) {
	m["camliType"] = "directory"
	m["entries"] = staticSetRef.String()
}

func rfc3339FromNanos(epochnanos int64) string {
	nanos := epochnanos % 1e9
	esec := epochnanos / 1e9
	t := time.SecondsToUTC(esec)
	timeStr := t.Format(time.RFC3339)
	if nanos == 0 {
		return timeStr
	}
	nanoStr := fmt.Sprintf("%09d", nanos)
	nanoStr = strings.TrimRight(nanoStr, "0")
	return timeStr[:len(timeStr)-1] + "." + nanoStr + "Z"
}

func populateMap(m map[int]string, file string) {
	f, err := os.Open(file, os.O_RDONLY, 0)
	if err != nil {
		return
	}
	bufr := bufio.NewReader(f)
	for {
		line, err := bufr.ReadString('\n')
		if err != nil {
			return
		}
		parts := strings.Split(line, ":", 4)
		if len(parts) >= 3 {
			idstr := parts[2]
			id, err := strconv.Atoi(idstr)
			if err == nil {
				m[id] = parts[0]
			}
		}
	}
}

var uidToUsernameMap map[int]string
var getUserFromUidOnce sync.Once
func getUserFromUid(uid int) string {
	getUserFromUidOnce.Do(func() {
		uidToUsernameMap = make(map[int]string)
		populateMap(uidToUsernameMap, "/etc/passwd")
	})
	return uidToUsernameMap[uid]
}

var gidToUsernameMap map[int]string
var getGroupFromGidOnce sync.Once
func getGroupFromGid(uid int) string {
	getGroupFromGidOnce.Do(func() {
		gidToUsernameMap = make(map[int]string)
		populateMap(gidToUsernameMap, "/etc/group")
	})
	return gidToUsernameMap[uid]
}

func isValidUtf8(s string) bool {
	for _, rune := range []int(s) {
		if rune == 0xfffd {
			return false
		}
	}
	return true
}
<|endoftext|>