<commit_before>// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package environment

import (
	"github.com/juju/errors"

	"github.com/juju/loggo"

	"github.com/juju/juju/apiserver/common"
	"github.com/juju/juju/environs"
	"github.com/juju/juju/environs/config"
	"github.com/juju/juju/environs/tools"
	"github.com/juju/juju/state"
	coretools "github.com/juju/juju/tools"
	"github.com/juju/juju/version"
)

var logger = loggo.GetLogger("juju.apiserver.environment")

var (
	findTools = tools.FindTools
)

// EnvironGetter represents a struct that can provide a state.Environment.
type EnvironGetter interface {
	Environment() (*state.Environment, error)
}

type toolsFinder func(environs.Environ, int, int, string, coretools.Filter) (coretools.List, error)
type envVersionUpdater func(*state.Environment, version.Number) error

var newEnvirons = environs.New

func checkToolsAvailability(cfg *config.Config, finder toolsFinder) (version.Number, error) {
	currentVersion, ok := cfg.AgentVersion()
	if !ok || currentVersion == version.Zero {
		return version.Zero, nil
	}

	env, err := newEnvirons(cfg)
	if err != nil {
		return version.Zero, errors.Annotatef(err, "cannot make environ")
	}

	// finder receives major and minor as parameters as it uses them to filter versions and
	// only return patches for the passed major.minor (from major.minor.patch).
	// We'll try the released stream first, then fall back to the current configured stream
	// if no released tools are found.
	vers, err := finder(env, currentVersion.Major, currentVersion.Minor, tools.ReleasedStream, coretools.Filter{})
	stream := cfg.AgentStream()
	if stream != tools.ReleasedStream && errors.Cause(err) == coretools.ErrNoMatches {
		vers, err = finder(env, currentVersion.Major, currentVersion.Minor, stream, coretools.Filter{})
	}
	if err != nil {
		return version.Zero, errors.Annotatef(err, "cannot find available tools")
	}
	// Newest also returns a list of the items in this list matching with the
	// newest version.
	newest, _ := vers.Newest()
	return newest, nil
}

var envConfig = func(e *state.Environment) (*config.Config, error) {
	return e.Config()
}

// Base implementation of envVersionUpdater
func envVersionUpdate(env *state.Environment, ver version.Number) error {
	return env.UpdateLatestToolsVersion(ver)
}

func updateToolsAvailability(st EnvironGetter, finder toolsFinder, update envVersionUpdater) error {
	env, err := st.Environment()
	if err != nil {
		return errors.Annotate(err, "cannot get environment")
	}
	cfg, err := envConfig(env)
	if err != nil {
		return errors.Annotate(err, "cannot get config")
	}
	ver, err := checkToolsAvailability(cfg, finder)
	if err != nil {
		if errors.Cause(err) == coretools.ErrNoMatches {
			// No newer tools, so exit silently.
			return nil
		}
		return errors.Annotate(err, "cannot get latest version")
	}
	if ver == version.Zero {
		logger.Debugf("tools lookup returned version Zero, this should only happen during bootstrap.")
		return nil
	}
	return update(env, ver)
}

// EnvironTools holds the required tools for an environ facade.
type EnvironTools struct {
	st         EnvironGetter
	authorizer common.Authorizer
	// tools lookup
	findTools        toolsFinder
	envVersionUpdate envVersionUpdater
}

// NewEnvironTools returns a new environ tools pointer with the passed attributes
// and some defaults that are only for changed during tests.
func NewEnvironTools(st EnvironGetter, authorizer common.Authorizer) *EnvironTools {
	return &EnvironTools{
		st:               st,
		authorizer:       authorizer,
		findTools:        findTools,
		envVersionUpdate: envVersionUpdate,
	}
}

// UpdateToolsAvailable invokes a lookup and further update in environ
// for new patches of the current tool versions.
func (e *EnvironTools) UpdateToolsAvailable() error {
	if !e.authorizer.AuthEnvironManager() {
		return common.ErrPerm
	}
	return updateToolsAvailability(e.st, e.findTools, e.envVersionUpdate)
}
<commit_msg>Use preferred stream for finding tools<commit_after>// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package environment

import (
	"github.com/juju/errors"

	"github.com/juju/loggo"

	"github.com/juju/juju/apiserver/common"
	"github.com/juju/juju/environs"
	"github.com/juju/juju/environs/config"
	"github.com/juju/juju/environs/tools"
	"github.com/juju/juju/state"
	coretools "github.com/juju/juju/tools"
	"github.com/juju/juju/version"
)

var logger = loggo.GetLogger("juju.apiserver.environment")

var (
	findTools = tools.FindTools
)

// EnvironGetter represents a struct that can provide a state.Environment.
type EnvironGetter interface {
	Environment() (*state.Environment, error)
}

type toolsFinder func(environs.Environ, int, int, string, coretools.Filter) (coretools.List, error)
type envVersionUpdater func(*state.Environment, version.Number) error

var newEnvirons = environs.New

func checkToolsAvailability(cfg *config.Config, finder toolsFinder) (version.Number, error) {
	currentVersion, ok := cfg.AgentVersion()
	if !ok || currentVersion == version.Zero {
		return version.Zero, nil
	}

	env, err := newEnvirons(cfg)
	if err != nil {
		return version.Zero, errors.Annotatef(err, "cannot make environ")
	}

	// finder receives major and minor as parameters as it uses them to filter versions and
	// only return patches for the passed major.minor (from major.minor.patch).
	// We'll try the released stream first, then fall back to the current configured stream
	// if no released tools are found.
	vers, err := finder(env, currentVersion.Major, currentVersion.Minor, tools.ReleasedStream, coretools.Filter{})
	preferredStream := tools.PreferredStream(&currentVersion, cfg.Development(), cfg.AgentStream())
	if preferredStream != tools.ReleasedStream && errors.Cause(err) == coretools.ErrNoMatches {
		vers, err = finder(env, currentVersion.Major, currentVersion.Minor, preferredStream, coretools.Filter{})
	}
	if err != nil {
		return version.Zero, errors.Annotatef(err, "cannot find available tools")
	}
	// Newest also returns a list of the items in this list matching with the
	// newest version.
	newest, _ := vers.Newest()
	return newest, nil
}

var envConfig = func(e *state.Environment) (*config.Config, error) {
	return e.Config()
}

// Base implementation of envVersionUpdater
func envVersionUpdate(env *state.Environment, ver version.Number) error {
	return env.UpdateLatestToolsVersion(ver)
}

func updateToolsAvailability(st EnvironGetter, finder toolsFinder, update envVersionUpdater) error {
	env, err := st.Environment()
	if err != nil {
		return errors.Annotate(err, "cannot get environment")
	}
	cfg, err := envConfig(env)
	if err != nil {
		return errors.Annotate(err, "cannot get config")
	}
	ver, err := checkToolsAvailability(cfg, finder)
	if err != nil {
		if errors.Cause(err) == coretools.ErrNoMatches {
			// No newer tools, so exit silently.
			return nil
		}
		return errors.Annotate(err, "cannot get latest version")
	}
	if ver == version.Zero {
		logger.Debugf("tools lookup returned version Zero, this should only happen during bootstrap.")
		return nil
	}
	return update(env, ver)
}

// EnvironTools holds the required tools for an environ facade.
type EnvironTools struct {
	st         EnvironGetter
	authorizer common.Authorizer
	// tools lookup
	findTools        toolsFinder
	envVersionUpdate envVersionUpdater
}

// NewEnvironTools returns a new environ tools pointer with the passed attributes
// and some defaults that are only for changed during tests.
func NewEnvironTools(st EnvironGetter, authorizer common.Authorizer) *EnvironTools {
	return &EnvironTools{
		st:               st,
		authorizer:       authorizer,
		findTools:        findTools,
		envVersionUpdate: envVersionUpdate,
	}
}

// UpdateToolsAvailable invokes a lookup and further update in environ
// for new patches of the current tool versions.
func (e *EnvironTools) UpdateToolsAvailable() error {
	if !e.authorizer.AuthEnvironManager() {
		return common.ErrPerm
	}
	return updateToolsAvailability(e.st, e.findTools, e.envVersionUpdate)
}
<|endoftext|>


<commit_before>/*
 Copyright 2016 Padduck, LLC

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

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

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

package environments

import (
	"errors"
	"github.com/gorilla/websocket"
	"github.com/pufferpanel/pufferd/logging"
	"github.com/pufferpanel/pufferd/utils"
	"github.com/shirou/gopsutil/process"
	"io"
	"os"
	"os/exec"
	"strconv"
	"sync"
	"syscall"
	"time"
)

type System struct {
	mainProcess   *exec.Cmd
	RootDirectory string
	ConsoleBuffer utils.Cache
	WSManager     utils.WebSocketManager
	stdInWriter   io.Writer
	wait          sync.WaitGroup
}

func (s *System) Execute(cmd string, args []string) (stdOut []byte, err error) {
	err = s.ExecuteAsync(cmd, args)
	if err != nil {
		return
	}
	err = s.WaitForMainProcess()
	return
}

func (s *System) ExecuteAsync(cmd string, args []string) (err error) {
	if s.IsRunning() {
		err = errors.New("A process is already running (" + strconv.Itoa(s.mainProcess.Process.Pid) + ")")
		return
	}
	s.mainProcess = exec.Command(cmd, args...)
	s.mainProcess.Dir = s.RootDirectory
	if s.mainProcess.Env == nil {
		s.mainProcess.Env = make([]string, 0)
	}
	s.mainProcess.Env = append(s.mainProcess.Env, "HOME="+s.RootDirectory)
	wrapper := s.createWrapper()
	s.mainProcess.Stdout = wrapper
	s.mainProcess.Stderr = wrapper
	pipe, err := s.mainProcess.StdinPipe()
	if err != nil {
		logging.Error("Error starting process", err)
	}
	s.stdInWriter = pipe
	s.wait = sync.WaitGroup{}
	s.wait.Add(1)
	err = s.mainProcess.Start()
	go func() {
		s.mainProcess.Wait()
		s.wait.Done()
	}()
	if err != nil && err.Error() != "exit status 1" {
		logging.Error("Error starting process", err)
	}
	return
}

func (s *System) ExecuteInMainProcess(cmd string) (err error) {
	if !s.IsRunning() {
		err = errors.New("Main process has not been started")
		return
	}
	stdIn := s.stdInWriter
	_, err = io.WriteString(stdIn, cmd+"\r")
	return
}

func (s *System) Kill() (err error) {
	if !s.IsRunning() {
		return
	}
	err = s.mainProcess.Process.Kill()
	s.mainProcess.Process.Release()
	s.mainProcess = nil
	return
}

func (s *System) Create() (err error) {
	os.Mkdir(s.RootDirectory, 0755)
	return
}

func (s *System) Delete() (err error) {
	return
}

func (s *System) IsRunning() (isRunning bool) {
	isRunning = s.mainProcess != nil && s.mainProcess.Process != nil
	if isRunning {
		process, pErr := os.FindProcess(s.mainProcess.Process.Pid)
		if process == nil || pErr != nil {
			isRunning = false
		} else if process.Signal(syscall.Signal(0)) != nil {
			isRunning = false
		}
	}
	return
}

func (s *System) WaitForMainProcess() (err error) {
	return s.WaitForMainProcessFor(0)
}

func (s *System) WaitForMainProcessFor(timeout int) (err error) {
	if s.IsRunning() {
		if timeout > 0 {
			var timer = time.AfterFunc(time.Duration(timeout)*time.Millisecond, func() {
				err = s.Kill()
			})
			s.wait.Wait()
			timer.Stop()
		} else {
			s.wait.Wait()
		}
	}
	return
}

func (s *System) GetRootDirectory() string {
	return s.RootDirectory
}

func (s *System) GetConsole() []string {
	return s.ConsoleBuffer.Read()
}

func (s *System) AddListener(ws *websocket.Conn) {
	s.WSManager.Register(ws)
}

func (s *System) GetStats() (map[string]interface{}, error) {
	if !s.IsRunning() {
		return nil, errors.New("Server not running")
	}
	process, err := process.NewProcess(int32(s.mainProcess.Process.Pid))
	if err != nil {
		return nil, err
	}
	resultMap := make(map[string]interface{})
	memMap, _ := process.MemoryInfo()
	resultMap["memory"] = memMap.RSS
	cpu, _ := process.Percent(time.Millisecond * 50)
	resultMap["cpu"] = cpu
	return resultMap, nil
}

func (s *System) createWrapper() io.Writer {
	return io.MultiWriter(s.ConsoleBuffer, s.WSManager)
}
<commit_msg>Fix env loading<commit_after>/*
 Copyright 2016 Padduck, LLC

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

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

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

package environments

import (
	"errors"
	"github.com/gorilla/websocket"
	"github.com/pufferpanel/pufferd/logging"
	"github.com/pufferpanel/pufferd/utils"
	"github.com/shirou/gopsutil/process"
	"io"
	"os"
	"os/exec"
	"strconv"
	"sync"
	"syscall"
	"time"
)

type System struct {
	mainProcess   *exec.Cmd
	RootDirectory string
	ConsoleBuffer utils.Cache
	WSManager     utils.WebSocketManager
	stdInWriter   io.Writer
	wait          sync.WaitGroup
}

func (s *System) Execute(cmd string, args []string) (stdOut []byte, err error) {
	err = s.ExecuteAsync(cmd, args)
	if err != nil {
		return
	}
	err = s.WaitForMainProcess()
	return
}

func (s *System) ExecuteAsync(cmd string, args []string) (err error) {
	if s.IsRunning() {
		err = errors.New("A process is already running (" + strconv.Itoa(s.mainProcess.Process.Pid) + ")")
		return
	}
	s.mainProcess = exec.Command(cmd, args...)
	s.mainProcess.Dir = s.RootDirectory
	s.mainProcess.Env = append(os.Environ(), "HOME="+s.RootDirectory)
	wrapper := s.createWrapper()
	s.mainProcess.Stdout = wrapper
	s.mainProcess.Stderr = wrapper
	pipe, err := s.mainProcess.StdinPipe()
	if err != nil {
		logging.Error("Error starting process", err)
	}
	s.stdInWriter = pipe
	s.wait = sync.WaitGroup{}
	s.wait.Add(1)
	err = s.mainProcess.Start()
	go func() {
		s.mainProcess.Wait()
		s.wait.Done()
	}()
	if err != nil && err.Error() != "exit status 1" {
		logging.Error("Error starting process", err)
	}
	return
}

func (s *System) ExecuteInMainProcess(cmd string) (err error) {
	if !s.IsRunning() {
		err = errors.New("Main process has not been started")
		return
	}
	stdIn := s.stdInWriter
	_, err = io.WriteString(stdIn, cmd+"\r")
	return
}

func (s *System) Kill() (err error) {
	if !s.IsRunning() {
		return
	}
	err = s.mainProcess.Process.Kill()
	s.mainProcess.Process.Release()
	s.mainProcess = nil
	return
}

func (s *System) Create() (err error) {
	os.Mkdir(s.RootDirectory, 0755)
	return
}

func (s *System) Delete() (err error) {
	return
}

func (s *System) IsRunning() (isRunning bool) {
	isRunning = s.mainProcess != nil && s.mainProcess.Process != nil
	if isRunning {
		process, pErr := os.FindProcess(s.mainProcess.Process.Pid)
		if process == nil || pErr != nil {
			isRunning = false
		} else if process.Signal(syscall.Signal(0)) != nil {
			isRunning = false
		}
	}
	return
}

func (s *System) WaitForMainProcess() (err error) {
	return s.WaitForMainProcessFor(0)
}

func (s *System) WaitForMainProcessFor(timeout int) (err error) {
	if s.IsRunning() {
		if timeout > 0 {
			var timer = time.AfterFunc(time.Duration(timeout)*time.Millisecond, func() {
				err = s.Kill()
			})
			s.wait.Wait()
			timer.Stop()
		} else {
			s.wait.Wait()
		}
	}
	return
}

func (s *System) GetRootDirectory() string {
	return s.RootDirectory
}

func (s *System) GetConsole() []string {
	return s.ConsoleBuffer.Read()
}

func (s *System) AddListener(ws *websocket.Conn) {
	s.WSManager.Register(ws)
}

func (s *System) GetStats() (map[string]interface{}, error) {
	if !s.IsRunning() {
		return nil, errors.New("Server not running")
	}
	process, err := process.NewProcess(int32(s.mainProcess.Process.Pid))
	if err != nil {
		return nil, err
	}
	resultMap := make(map[string]interface{})
	memMap, _ := process.MemoryInfo()
	resultMap["memory"] = memMap.RSS
	cpu, _ := process.Percent(time.Millisecond * 50)
	resultMap["cpu"] = cpu
	return resultMap, nil
}

func (s *System) createWrapper() io.Writer {
	return io.MultiWriter(s.ConsoleBuffer, s.WSManager)
}
<|endoftext|>


<commit_before>// Copyright 2015 Dorival de Moraes Pedroso. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package goga

import (
	"math/rand"
	"testing"

	"github.com/cpmech/gosl/chk"
	"github.com/cpmech/gosl/io"
	"github.com/cpmech/gosl/rnd"
	"github.com/cpmech/gosl/utl"
)

func Test_evo01(tst *testing.T) {

	verbose()
	chk.PrintTitle("evo01")

	// initialise random numbers generator
	rnd.Init(0) // 0 => use current time as seed

	// objective function
	ovfunc := func(ind *Individual, time int, best *Individual) {
		ind.ObjValue = 1.0 / (1.0 + (ind.GetFloat(0)+ind.GetFloat(1)+ind.GetFloat(2))/3.0)
	}

	// reference population
	nbases := 8
	pop := NewPopFloatChromo(nbases, [][]float64{
		{11, 21, 31},
		{12, 22, 32},
		{13, 23, 33},
		{14, 24, 34},
		{15, 25, 35},
		{16, 26, 36},
	})

	// evolver
	evo := NewEvolverPop([]Population{pop}, ovfunc)

	// run
	tf := 100
	dtout := 10
	dtmig := 20
	io.Pf("\n")
	evo.Run(tf, dtout, dtmig, true)

	// plot
	if true {
		evo.Islands[0].PlotOvs("/tmp", "fig_evo01", "", tf, true, "%.6f", true, true)
	}
}

func Test_evo02(tst *testing.T) {

	verbose()
	chk.PrintTitle("evo02. organise sequence of ints")

	// initialise random numbers generator
	rnd.Init(0) // 0 => use current time as seed

	// mutation function
	mtfunc := func(A []int, nchanges int, pm float64, extra interface{}) {
		size := len(A)
		if !rnd.FlipCoin(pm) || size < 1 {
			return
		}
		pos := rnd.IntGetUniqueN(0, size, nchanges)
		for _, i := range pos {
			if A[i] == 1 {
				A[i] = 0
			}
			if A[i] == 0 {
				A[i] = 1
			}
		}
	}

	// objective function
	ovfunc := func(ind *Individual, time int, best *Individual) {
		score := 0.0
		count := 0
		for _, val := range ind.Ints {
			if val == 0 && count%2 == 0 {
				score += 1.0
			}
			if val == 1 && count%2 != 0 {
				score += 1.0
			}
			count++
		}
		ind.ObjValue = 1.0 / (1.0 + score)
	}

	// reference individual
	nvals := 20
	ref := NewIndividual(1, utl.IntVals(nvals, 1))
	for i := 0; i < nvals; i++ {
		ref.Ints[i] = rand.Intn(2)
	}

	// bingo
	bingo := NewBingoInts(utl.IntVals(nvals, 0), utl.IntVals(nvals, 1))
	bingo.UseIntRnd = true

	// evolver
	nislands := 3
	ninds := 10
	evo := NewEvolver(nislands, ninds, ref, bingo, ovfunc)
	for _, isl := range evo.Islands {
		isl.MtProbs = make(map[string]float64)
		isl.MtProbs["int"] = 0.01
		isl.MtIntFunc = mtfunc
		io.Pforan("\n%v\n", isl.Pop.Output(nil))
	}

	// saving files
	evo.FnKey = "evo02"

	// run
	tf := 100
	dtout := 20
	dtmig := 400
	evo.Run(tf, dtout, dtmig, true)

	if true {
		// results
		io.Pf("\n")
		for _, isl := range evo.Islands {
			isl.MtProbs = make(map[string]float64)
			isl.MtProbs["int"] = 0.01
			isl.MtIntFunc = mtfunc
			io.Pfgreen("%v\n", isl.Pop.Output(nil))
		}
		ideal := 1.0 / (1.0 + float64(nvals))
		io.PfGreen("\nBest = %v\nBestOV = %v  (ideal=%v)\n", evo.Best.Ints, evo.Best.ObjValue, ideal)
	}

	// plot
	if true {
		for i, isl := range evo.Islands {
			first := i == 0
			last := i == nislands-1
			isl.PlotOvs("/tmp", "fig_evo02", "", tf, true, "%.6f", first, last)
		}
	}
}
<commit_msg>test improved: using fewer individuals to illustrate advantage of islands. (evo02 seems alright now)<commit_after>// Copyright 2015 Dorival de Moraes Pedroso. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package goga

import (
	"math/rand"
	"testing"

	"github.com/cpmech/gosl/chk"
	"github.com/cpmech/gosl/io"
	"github.com/cpmech/gosl/rnd"
	"github.com/cpmech/gosl/utl"
)

func Test_evo01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("evo01")

	// initialise random numbers generator
	rnd.Init(0) // 0 => use current time as seed

	// objective function
	ovfunc := func(ind *Individual, time int, best *Individual) {
		ind.ObjValue = 1.0 / (1.0 + (ind.GetFloat(0)+ind.GetFloat(1)+ind.GetFloat(2))/3.0)
	}

	// reference population
	nbases := 8
	pop := NewPopFloatChromo(nbases, [][]float64{
		{11, 21, 31},
		{12, 22, 32},
		{13, 23, 33},
		{14, 24, 34},
		{15, 25, 35},
		{16, 26, 36},
	})

	// evolver
	evo := NewEvolverPop([]Population{pop}, ovfunc)

	// run
	tf := 100
	dtout := 10
	dtmig := 20
	io.Pf("\n")
	evo.Run(tf, dtout, dtmig, true)

	// plot
	//if true {
	if false {
		evo.Islands[0].PlotOvs("/tmp", "fig_evo01", "", tf, true, "%.6f", true, true)
	}
}

func Test_evo02(tst *testing.T) {

	//verbose()
	chk.PrintTitle("evo02. organise sequence of ints")
	io.Pf("\n")

	// initialise random numbers generator
	rnd.Init(0) // 0 => use current time as seed

	// mutation function
	mtfunc := func(A []int, nchanges int, pm float64, extra interface{}) {
		size := len(A)
		if !rnd.FlipCoin(pm) || size < 1 {
			return
		}
		pos := rnd.IntGetUniqueN(0, size, nchanges)
		for _, i := range pos {
			if A[i] == 1 {
				A[i] = 0
			}
			if A[i] == 0 {
				A[i] = 1
			}
		}
	}

	// objective function
	ovfunc := func(ind *Individual, time int, best *Individual) {
		score := 0.0
		count := 0
		for _, val := range ind.Ints {
			if val == 0 && count%2 == 0 {
				score += 1.0
			}
			if val == 1 && count%2 != 0 {
				score += 1.0
			}
			count++
		}
		ind.ObjValue = 1.0 / (1.0 + score)
	}

	// reference individual
	nvals := 20
	ref := NewIndividual(1, utl.IntVals(nvals, 1))
	for i := 0; i < nvals; i++ {
		ref.Ints[i] = rand.Intn(2)
	}

	// bingo
	bingo := NewBingoInts(utl.IntVals(nvals, 0), utl.IntVals(nvals, 1))
	bingo.UseIntRnd = true

	// evolver
	nislands := 3
	ninds := 6
	evo := NewEvolver(nislands, ninds, ref, bingo, ovfunc)
	for _, isl := range evo.Islands {
		isl.MtProbs = make(map[string]float64)
		isl.MtProbs["int"] = 0.01
		isl.MtIntFunc = mtfunc
	}

	// saving files
	evo.FnKey = "evo02"

	// run
	tf := 100
	dtout := 20
	dtmig := 40
	evo.Run(tf, dtout, dtmig, true)

	// results
	ideal := 1.0 / (1.0 + float64(nvals))
	io.PfGreen("\nBest = %v\nBestOV = %v  (ideal=%v)\n", evo.Best.Ints, evo.Best.ObjValue, ideal)

	// plot
	//if true {
	if false {
		for i, isl := range evo.Islands {
			first := i == 0
			last := i == nislands-1
			isl.PlotOvs("/tmp", "fig_evo02", "", tf, true, "%.6f", first, last)
		}
	}
}
<|endoftext|>


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

package tail

import (
	"bytes"
	"context"
	"io/ioutil"
	"math/rand"
	"os"
	"testing"
	"time"

	"github.com/prometheus/tsdb/wal"
)

func TestTailFuzz(t *testing.T) {
	dir, err := ioutil.TempDir("", "test_tail")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(dir)
	ctx, cancel := context.WithCancel(context.Background())

	rc, err := Tail(ctx, dir)
	if err != nil {
		t.Fatal(err)
	}
	defer rc.Close()

	w, err := wal.NewSize(nil, nil, dir, 2*1024*1024)
	if err != nil {
		t.Fatal(err)
	}
	defer w.Close()

	var written [][]byte
	var read [][]byte

	// Start background writer.
	const count = 50000
	go func() {
		for i := 0; i < count; i++ {
			if i%100 == 0 {
				time.Sleep(time.Duration(rand.Intn(10 * int(time.Millisecond))))
			}
			rec := make([]byte, rand.Intn(5337))
			if _, err := rand.Read(rec); err != nil {
				t.Fatal(err)
			}
			if err := w.Log(rec); err != nil {
				t.Fatal(err)
			}
			written = append(written, rec)
		}
		time.Sleep(time.Second)
		cancel()
	}()

	wr := wal.NewReader(rc)

	for wr.Next() {
		read = append(read, append([]byte(nil), wr.Record()...))
	}
	if wr.Err() != nil {
		t.Fatal(wr.Err())
	}
	if len(written) != len(read) {
		t.Fatal("didn't read all records")
	}
	for i, r := range read {
		if !bytes.Equal(r, written[i]) {
			t.Fatalf("record %d doesn't match", i)
		}
	}
}

func BenchmarkTailFuzz(t *testing.B) {
	dir, err := ioutil.TempDir("", "test_tail")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(dir)

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	rc, err := Tail(ctx, dir)
	if err != nil {
		t.Fatal(err)
	}
	defer rc.Close()

	w, err := wal.NewSize(nil, nil, dir, 32*1024*1024)
	if err != nil {
		t.Fatal(err)
	}
	defer w.Close()

	t.SetBytes(4 * 2000) // Average record size times worker count.
	t.ResetTimer()

	var rec [4000]byte
	count := t.N * 4
	for k := 0; k < 4; k++ {
		go func() {
			for i := 0; i < count/4; i++ {
				if err := w.Log(rec[:rand.Intn(4000)]); err != nil {
					t.Fatal(err)
				}
			}
		}()
	}

	wr := wal.NewReader(rc)

	for i := 1; wr.Next(); i++ {
		if i == t.N*4 {
			break
		}
	}
}
<commit_msg>Use panic instead of t.Fatal for test fixture error handling. This addresses staticcheck warnings: the goroutine calls T.Fatal, which must be called in the same goroutine as the test (SA2002). (#13)<commit_after>// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package tail

import (
	"bytes"
	"context"
	"io/ioutil"
	"math/rand"
	"os"
	"testing"
	"time"

	"github.com/prometheus/tsdb/wal"
)

func TestTailFuzz(t *testing.T) {
	dir, err := ioutil.TempDir("", "test_tail")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(dir)
	ctx, cancel := context.WithCancel(context.Background())

	rc, err := Tail(ctx, dir)
	if err != nil {
		t.Fatal(err)
	}
	defer rc.Close()

	w, err := wal.NewSize(nil, nil, dir, 2*1024*1024)
	if err != nil {
		t.Fatal(err)
	}
	defer w.Close()

	var written [][]byte
	var read [][]byte

	// Start background writer.
	const count = 50000
	go func() {
		for i := 0; i < count; i++ {
			if i%100 == 0 {
				time.Sleep(time.Duration(rand.Intn(10 * int(time.Millisecond))))
			}
			rec := make([]byte, rand.Intn(5337))
			if _, err := rand.Read(rec); err != nil {
				panic(err)
			}
			if err := w.Log(rec); err != nil {
				panic(err)
			}
			written = append(written, rec)
		}
		time.Sleep(time.Second)
		cancel()
	}()

	wr := wal.NewReader(rc)

	for wr.Next() {
		read = append(read, append([]byte(nil), wr.Record()...))
	}
	if wr.Err() != nil {
		t.Fatal(wr.Err())
	}
	if len(written) != len(read) {
		t.Fatal("didn't read all records")
	}
	for i, r := range read {
		if !bytes.Equal(r, written[i]) {
			t.Fatalf("record %d doesn't match", i)
		}
	}
}

func BenchmarkTailFuzz(t *testing.B) {
	dir, err := ioutil.TempDir("", "test_tail")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(dir)

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	rc, err := Tail(ctx, dir)
	if err != nil {
		t.Fatal(err)
	}
	defer rc.Close()

	w, err := wal.NewSize(nil, nil, dir, 32*1024*1024)
	if err != nil {
		t.Fatal(err)
	}
	defer w.Close()

	t.SetBytes(4 * 2000) // Average record size times worker count.
	t.ResetTimer()

	var rec [4000]byte
	count := t.N * 4
	for k := 0; k < 4; k++ {
		go func() {
			for i := 0; i < count/4; i++ {
				if err := w.Log(rec[:rand.Intn(4000)]); err != nil {
					panic(err)
				}
			}
		}()
	}

	wr := wal.NewReader(rc)

	for i := 1; wr.Next(); i++ {
		if i == t.N*4 {
			break
		}
	}
}
<|endoftext|>


<commit_before>package human_test

import (
	"fmt"
	"github.com/anexia-it/go-human"
	"net"
	"os"
)

type SimpleChild struct {
	Name      string  // no tag
	Property1 uint64  `human:"-"`          // Ignored
	Property2 float64 `human:",omitempty"` // Omitted if empty
}

type SimpleTest struct {
	Var1  string //no tag
	Var2  int    `human:"variable_2"`
	Child SimpleChild
}

type MapTest struct {
	Val1      uint64
	Map       map[string]SimpleChild
	StructMap map[SimpleChild]uint8
}

type SliceTest struct {
	IntSlice    []int
	StructSlice []SimpleChild
}

type MapSliceTest struct {
	StructMapSlice []map[string]int
}

type address struct {
	Ip net.IP
}

type TagFailTest struct {
	Test int `human:"&§/$"`
}

type AnonymousFieldTest struct {
	int
	Text string
}

func ExampleEncoder_Encode_SimpleOmitEmpty() {
	enc, err := human.NewEncoder(os.Stdout)
	if err != nil {
		return
	}
	testStruct := SimpleTest{
		Var1: "v1",
		Var2: 2,
		Child: SimpleChild{
			Name:      "theChild",
			Property1: 3, // should be ignored
			Property2: 0, // empty, should be omitted
		},
	}

	// Output: Var1: v1
	// variable_2: 2
	// Child:
	//   Name: theChild

	if err := enc.Encode(testStruct); err != nil {
		fmt.Printf("ERROR: %s\n", err.Error())
		return
	}
}

func ExampleEncoder_Encode_Simple() {
	enc, err := human.NewEncoder(os.Stdout)
	if err != nil {
		return
	}
	testStruct := SimpleTest{
		Var1: "v1",
		Var2: 2,
		Child: SimpleChild{
			Name:      "theChild",
			Property1: 3, // should be ignored
			Property2: 4.5,
		},
	}

	// Output: Var1: v1
	// variable_2: 2
	// Child:
	//   Name: theChild
	//   Property2: 4.5

	if err := enc.Encode(testStruct); err != nil {
		fmt.Printf("ERROR: %s\n", err.Error())
		return
	}
}

func ExampleEncoder_Encode_SimpleMap() {
	enc, err := human.NewEncoder(os.Stdout)
	if err != nil {
		return
	}

	child1 := SimpleChild{
		Name:      "Person1",
		Property2: 4.5,
		Property1: 0, // should be ignored
	}

	child2 := SimpleChild{
		Name: "Person2",
	}
	stringMap := map[string]SimpleChild{
		"One": child1,
		"Two": child2,
	}
	structMap := map[SimpleChild]uint8{
		child1: 1,
		child2: 2,
	}
	testStruct := MapTest{
		Map:       stringMap,
		StructMap: structMap,
	}

	// Output: Val1: 0
	// Map:
	//   * One: Name: Person1
	//     Property2: 4.5
	//   * Two: Name: Person2
	// StructMap:
	//   * {Person1 0 4.5}: 1
	//   * {Person2 0 0}: 2

	if err := enc.Encode(testStruct); err != nil {
		fmt.Printf("ERROR: %s\n", err.Error())
		return
	}
}

func ExampleEncoder_Encode_SimpleSlice() {
	enc, err := human.NewEncoder(os.Stdout)
	if err != nil {
		return
	}

	child1 := SimpleChild{
		Name:      "Person1",
		Property2: 4.5,
		Property1: 0, // should be ignored
	}

	child2 := SimpleChild{
		Name: "Person2",
	}
	structSlice := []SimpleChild{child1, child2}
	testStruct := SliceTest{
		IntSlice:    []int{1, 2, 3, 4, 5},
		StructSlice: structSlice,
	}

	// Output: IntSlice:
	//   * 1
	//   * 2
	//   * 3
	//   * 4
	//   * 5
	// StructSlice:
	//   * Name: Person1
	//     Property2: 4.5
	//   * Name: Person2

	if err := enc.Encode(testStruct); err != nil {
		fmt.Printf("ERROR: %s\n", err.Error())
		return
	}
}

func ExampleEncoder_Encode_StructMapSlice() {
	enc, err := human.NewEncoder(os.Stdout, human.OptionListSymbols("+", "-"))
	if err != nil {
		return
	}

	mapSliceElement1 := map[string]int{
		"one": 1,
		"two": 2,
		"tenthousandonehundredfourtytwo": 10142,
	}
	slice := []map[string]int{mapSliceElement1, mapSliceElement1}
	testStruct := MapSliceTest{
		StructMapSlice: slice,
	}

	//Output: StructMapSlice:
	//   +
	//     - one: 1
	//     - tenthousandonehundredfourtytwo: 10142
	//     - two: 2
	//   +
	//     - one: 1
	//     - tenthousandonehundredfourtytwo: 10142
	//     - two: 2
	if err := enc.Encode(testStruct); err != nil {
		fmt.Printf("ERROR: %s\n", err.Error())
		return
	}

}

func ExampleEncoder_Encode_TextMarshaler() {
	enc, err := human.NewEncoder(os.Stdout)
	if err != nil {
		return
	}

	// Output: ip: 127.0.0.1

	addr := address{
		Ip: net.ParseIP("127.0.0.1"),
	}
	if err := enc.Encode(addr); err != nil {
		fmt.Printf("ERROR: %s\n", err.Error())
		return
	}

}

func ExampleEncoder_Encode_MapFieldError() {
	enc, err := human.NewEncoder(os.Stdout)
	if err != nil {
		return
	}

	testStruct := TagFailTest{
		Test: 1,
	}

	// Output: ERROR: 1 error occurred:
	//
	// * Invalid tag: '&§/$'
	//
	if err := enc.Encode(testStruct); err != nil {
		fmt.Printf("ERROR: %s\n", err.Error())
		return
	}

}

func ExampleEncoder_Encode_AnonymousFiled() {
	enc, err := human.NewEncoder(os.Stdout)
	if err != nil {
		return
	}

	//anonymous int field is ignored
	testStruct := AnonymousFieldTest{
		Text: "test",
	}
	// Output: Text: test

	if err := enc.Encode(testStruct); err != nil {
		fmt.Printf("ERROR: %s\n", err.Error())
		return
	}
}
<commit_msg>fixed expected output<commit_after>package human_test

import (
	"fmt"
	"github.com/anexia-it/go-human"
	"net"
	"os"
)

type SimpleChild struct {
	Name      string  // no tag
	Property1 uint64  `human:"-"`          // Ignored
	Property2 float64 `human:",omitempty"` // Omitted if empty
}

type SimpleTest struct {
	Var1  string //no tag
	Var2  int    `human:"variable_2"`
	Child SimpleChild
}

type MapTest struct {
	Val1      uint64
	Map       map[string]SimpleChild
	StructMap map[SimpleChild]uint8
}

type SliceTest struct {
	IntSlice    []int
	StructSlice []SimpleChild
}

type MapSliceTest struct {
	StructMapSlice []map[string]int
}

type address struct {
	Ip net.IP
}

type TagFailTest struct {
	Test int `human:"&§/$"`
}

type AnonymousFieldTest struct {
	int
	Text string
}

func ExampleEncoder_Encode_SimpleOmitEmpty() {
	enc, err := human.NewEncoder(os.Stdout)
	if err != nil {
		return
	}
	testStruct := SimpleTest{
		Var1: "v1",
		Var2: 2,
		Child: SimpleChild{
			Name:      "theChild",
			Property1: 3, // should be ignored
			Property2: 0, // empty, should be omitted
		},
	}

	// Output: Var1: v1
	// variable_2: 2
	// Child:
	//   Name: theChild

	if err := enc.Encode(testStruct); err != nil {
		fmt.Printf("ERROR: %s\n", err.Error())
		return
	}
}

func ExampleEncoder_Encode_Simple() {
	enc, err := human.NewEncoder(os.Stdout)
	if err != nil {
		return
	}
	testStruct := SimpleTest{
		Var1: "v1",
		Var2: 2,
		Child: SimpleChild{
			Name:      "theChild",
			Property1: 3, // should be ignored
			Property2: 4.5,
		},
	}

	// Output: Var1: v1
	// variable_2: 2
	// Child:
	//   Name: theChild
	//   Property2: 4.5

	if err := enc.Encode(testStruct); err != nil {
		fmt.Printf("ERROR: %s\n", err.Error())
		return
	}
}

func ExampleEncoder_Encode_SimpleMap() {
	enc, err := human.NewEncoder(os.Stdout)
	if err != nil {
		return
	}

	child1 := SimpleChild{
		Name:      "Person1",
		Property2: 4.5,
		Property1: 0, // should be ignored
	}

	child2 := SimpleChild{
		Name: "Person2",
	}
	stringMap := map[string]SimpleChild{
		"One": child1,
		"Two": child2,
	}
	structMap := map[SimpleChild]uint8{
		child1: 1,
		child2: 2,
	}
	testStruct := MapTest{
		Map:       stringMap,
		StructMap: structMap,
	}

	// Output: Val1: 0
	// Map:
	//   * One: Name: Person1
	//     Property2: 4.5
	//   * Two: Name: Person2
	// StructMap:
	//   * {Person1 0 4.5}: 1
	//   * {Person2 0 0}: 2

	if err := enc.Encode(testStruct); err != nil {
		fmt.Printf("ERROR: %s\n", err.Error())
		return
	}
}

func ExampleEncoder_Encode_SimpleSlice() {
	enc, err := human.NewEncoder(os.Stdout)
	if err != nil {
		return
	}

	child1 := SimpleChild{
		Name:      "Person1",
		Property2: 4.5,
		Property1: 0, // should be ignored
	}

	child2 := SimpleChild{
		Name: "Person2",
	}
	structSlice := []SimpleChild{child1, child2}
	testStruct := SliceTest{
		IntSlice:    []int{1, 2, 3, 4, 5},
		StructSlice: structSlice,
	}

	// Output: IntSlice:
	//   * 1
	//   * 2
	//   * 3
	//   * 4
	//   * 5
	// StructSlice:
	//   * Name: Person1
	//     Property2: 4.5
	//   * Name: Person2

	if err := enc.Encode(testStruct); err != nil {
		fmt.Printf("ERROR: %s\n", err.Error())
		return
	}
}

func ExampleEncoder_Encode_StructMapSlice() {
	enc, err := human.NewEncoder(os.Stdout, human.OptionListSymbols("+", "-"))
	if err != nil {
		return
	}

	mapSliceElement1 := map[string]int{
		"one": 1,
		"two": 2,
		"tenthousandonehundredfourtytwo": 10142,
	}
	slice := []map[string]int{mapSliceElement1, mapSliceElement1}
	testStruct := MapSliceTest{
		StructMapSlice: slice,
	}

	//Output: StructMapSlice:
	//   +
	//     - one: 1
	//     - tenthousandonehundredfourtytwo: 10142
	//     - two: 2
	//   +
	//     - one: 1
	//     - tenthousandonehundredfourtytwo: 10142
	//     - two: 2
	if err := enc.Encode(testStruct); err != nil {
		fmt.Printf("ERROR: %s\n", err.Error())
		return
	}

}

func ExampleEncoder_Encode_TextMarshaler() {
	enc, err := human.NewEncoder(os.Stdout)
	if err != nil {
		return
	}

	// Output: Ip:[49 50 55 46 48 46 48 46 49]

	addr := address{
		Ip: net.ParseIP("127.0.0.1"),
	}
	if err := enc.Encode(addr); err != nil {
		fmt.Printf("ERROR: %s\n", err.Error())
		return
	}

}

func ExampleEncoder_Encode_MapFieldError() {
	enc, err := human.NewEncoder(os.Stdout)
	if err != nil {
		return
	}

	testStruct := TagFailTest{
		Test: 1,
	}

	// Output: ERROR: 1 error occurred:
	//
	// * Invalid tag: '&§/$'
	//
	if err := enc.Encode(testStruct); err != nil {
		fmt.Printf("ERROR: %s\n", err.Error())
		return
	}

}

func ExampleEncoder_Encode_AnonymousFiled() {
	enc, err := human.NewEncoder(os.Stdout)
	if err != nil {
		return
	}

	//anonymous int field is ignored
	testStruct := AnonymousFieldTest{
		Text: "test",
	}
	// Output: Text: test

	if err := enc.Encode(testStruct); err != nil {
		fmt.Printf("ERROR: %s\n", err.Error())
		return
	}
}
<|endoftext|>


<commit_before>package repository

import (
	"runtime"
	"io/ioutil"
	"path/filepath"

	"github.com/hoffie/larasync/helpers/path"
	. "gopkg.in/check.v1"
)

var _ = Suite(&RepositoryAddItemTests{})

type RepositoryAddItemTests struct {
	dir string
	r   *ClientRepository
}

func (t *RepositoryAddItemTests) SetUpTest(c *C) {
	t.dir = c.MkDir()
	t.r = NewClient(t.dir)
	err := t.r.CreateManagementDir()
	c.Assert(err, IsNil)
	err = t.r.keys.CreateSigningKey()
	c.Assert(err, IsNil)

	err = t.r.keys.CreateEncryptionKey()
	c.Assert(err, IsNil)

	err = t.r.keys.CreateHashingKey()
	c.Assert(err, IsNil)
}

func (t *RepositoryAddItemTests) TestWriteFileToChunks(c *C) {
	fullpath := filepath.Join(t.dir, "foo.txt")
	err := ioutil.WriteFile(fullpath, []byte("foo"), 0600)
	c.Assert(err, IsNil)
	numFiles, err := path.NumFilesInDir(filepath.Join(t.dir, ".lara", "objects"))
	c.Assert(err, IsNil)
	c.Assert(numFiles, Equals, 0)
	err = t.r.AddItem(fullpath)
	c.Assert(err, IsNil)
	numFiles, err = path.NumFilesInDir(filepath.Join(t.dir, ".lara", "objects"))
	c.Assert(err, IsNil)
	c.Assert(numFiles, Equals, 2)
}

// TestExistingFileNIBReuse ensures that pre-existing NIBs for a path are
// re-used upon updates.
func (t *RepositoryAddItemTests) TestExistingFileNIBReuse(c *C) {
	nibsPath := filepath.Join(t.dir, ".lara", "nibs")
	filename := "foo.txt"
	fullpath := filepath.Join(t.dir, filename)
	err := ioutil.WriteFile(fullpath, []byte("foo"), 0600)
	c.Assert(err, IsNil)

	numFiles, err := path.NumFilesInDir(nibsPath)
	c.Assert(err, IsNil)
	c.Assert(numFiles, Equals, 0)

	err = t.r.AddItem(fullpath)
	c.Assert(err, IsNil)

	numFiles, err = path.NumFilesInDir(nibsPath)
	c.Assert(err, IsNil)
	c.Assert(numFiles, Equals, 1)

	err = ioutil.WriteFile(fullpath, []byte("foo2"), 0600)
	c.Assert(err, IsNil)

	err = t.r.AddItem(fullpath)
	c.Assert(err, IsNil)

	numFiles, err = path.NumFilesInDir(nibsPath)
	c.Assert(err, IsNil)
	c.Assert(numFiles, Equals, 1)

	nibID, err := t.r.pathToNIBID(filename)
	c.Assert(err, IsNil)
	nib, err := t.r.nibStore.Get(nibID)
	c.Assert(err, IsNil)
	c.Assert(len(nib.Revisions), Equals, 2)
	c.Assert(nib.Revisions[0].UTCTimestamp, Not(Equals), int64(0))
	c.Assert(nib.Revisions[0].UTCTimestamp <= nib.Revisions[1].UTCTimestamp,
		Equals, true)
}

// TestExistingFileNoChange ensures that no unnecessary updates
// are recorded.
func (t *RepositoryAddItemTests) TestExistingFileNoChange(c *C) {
	nibsPath := filepath.Join(t.dir, ".lara", "nibs")
	filename := "foo.txt"
	fullpath := filepath.Join(t.dir, filename)
	err := ioutil.WriteFile(fullpath, []byte("foo"), 0600)
	c.Assert(err, IsNil)

	numFiles, err := path.NumFilesInDir(nibsPath)
	c.Assert(err, IsNil)
	c.Assert(numFiles, Equals, 0)

	err = t.r.AddItem(fullpath)
	c.Assert(err, IsNil)

	numFiles, err = path.NumFilesInDir(nibsPath)
	c.Assert(err, IsNil)
	c.Assert(numFiles, Equals, 1)

	err = t.r.AddItem(fullpath)
	c.Assert(err, IsNil)

	numFiles, err = path.NumFilesInDir(nibsPath)
	c.Assert(err, IsNil)
	c.Assert(numFiles, Equals, 1)

	nibID, err := t.r.pathToNIBID(filename)
	c.Assert(err, IsNil)
	nib, err := t.r.nibStore.Get(nibID)
	c.Assert(err, IsNil)
	c.Assert(len(nib.Revisions), Equals, 1)
}

func (t *RepositoryAddItemTests) TestAddDotLara(c *C) {
	err := t.r.AddItem(filepath.Join(t.r.Path, managementDirName))
	c.Assert(err, Equals, ErrRefusingWorkOnDotLara)
}

func (t *RepositoryAddItemTests) TestAddDotLaraModified(c *C) {
	path := string(filepath.Separator) + filepath.Join(t.r.Path, managementDirName)

	err := t.r.AddItem(path)
	if runtime.GOOS != "windows" {
		c.Assert(err, Equals, ErrRefusingWorkOnDotLara)
	} else {
		c.Assert(err, NotNil)
	}
}

func (t *RepositoryAddItemTests) TestAddDotLaraSubdir(c *C) {
	path := filepath.Join(t.r.Path, managementDirName, nibsDirName)
	err := t.r.AddItem(path)
	c.Assert(err, Equals, ErrRefusingWorkOnDotLara)
}
<commit_msg>go fmt<commit_after>package repository

import (
	"io/ioutil"
	"path/filepath"
	"runtime"

	"github.com/hoffie/larasync/helpers/path"
	. "gopkg.in/check.v1"
)

var _ = Suite(&RepositoryAddItemTests{})

type RepositoryAddItemTests struct {
	dir string
	r   *ClientRepository
}

func (t *RepositoryAddItemTests) SetUpTest(c *C) {
	t.dir = c.MkDir()
	t.r = NewClient(t.dir)
	err := t.r.CreateManagementDir()
	c.Assert(err, IsNil)
	err = t.r.keys.CreateSigningKey()
	c.Assert(err, IsNil)

	err = t.r.keys.CreateEncryptionKey()
	c.Assert(err, IsNil)

	err = t.r.keys.CreateHashingKey()
	c.Assert(err, IsNil)
}

func (t *RepositoryAddItemTests) TestWriteFileToChunks(c *C) {
	fullpath := filepath.Join(t.dir, "foo.txt")
	err := ioutil.WriteFile(fullpath, []byte("foo"), 0600)
	c.Assert(err, IsNil)
	numFiles, err := path.NumFilesInDir(filepath.Join(t.dir, ".lara", "objects"))
	c.Assert(err, IsNil)
	c.Assert(numFiles, Equals, 0)
	err = t.r.AddItem(fullpath)
	c.Assert(err, IsNil)
	numFiles, err = path.NumFilesInDir(filepath.Join(t.dir, ".lara", "objects"))
	c.Assert(err, IsNil)
	c.Assert(numFiles, Equals, 2)
}

// TestExistingFileNIBReuse ensures that pre-existing NIBs for a path are
// re-used upon updates.
func (t *RepositoryAddItemTests) TestExistingFileNIBReuse(c *C) {
	nibsPath := filepath.Join(t.dir, ".lara", "nibs")
	filename := "foo.txt"
	fullpath := filepath.Join(t.dir, filename)
	err := ioutil.WriteFile(fullpath, []byte("foo"), 0600)
	c.Assert(err, IsNil)

	numFiles, err := path.NumFilesInDir(nibsPath)
	c.Assert(err, IsNil)
	c.Assert(numFiles, Equals, 0)

	err = t.r.AddItem(fullpath)
	c.Assert(err, IsNil)

	numFiles, err = path.NumFilesInDir(nibsPath)
	c.Assert(err, IsNil)
	c.Assert(numFiles, Equals, 1)

	err = ioutil.WriteFile(fullpath, []byte("foo2"), 0600)
	c.Assert(err, IsNil)

	err = t.r.AddItem(fullpath)
	c.Assert(err, IsNil)

	numFiles, err = path.NumFilesInDir(nibsPath)
	c.Assert(err, IsNil)
	c.Assert(numFiles, Equals, 1)

	nibID, err := t.r.pathToNIBID(filename)
	c.Assert(err, IsNil)
	nib, err := t.r.nibStore.Get(nibID)
	c.Assert(err, IsNil)
	c.Assert(len(nib.Revisions), Equals, 2)
	c.Assert(nib.Revisions[0].UTCTimestamp, Not(Equals), int64(0))
	c.Assert(nib.Revisions[0].UTCTimestamp <= nib.Revisions[1].UTCTimestamp,
		Equals, true)
}

// TestExistingFileNoChange ensures that no unnecessary updates
// are recorded.
func (t *RepositoryAddItemTests) TestExistingFileNoChange(c *C) {
	nibsPath := filepath.Join(t.dir, ".lara", "nibs")
	filename := "foo.txt"
	fullpath := filepath.Join(t.dir, filename)
	err := ioutil.WriteFile(fullpath, []byte("foo"), 0600)
	c.Assert(err, IsNil)

	numFiles, err := path.NumFilesInDir(nibsPath)
	c.Assert(err, IsNil)
	c.Assert(numFiles, Equals, 0)

	err = t.r.AddItem(fullpath)
	c.Assert(err, IsNil)

	numFiles, err = path.NumFilesInDir(nibsPath)
	c.Assert(err, IsNil)
	c.Assert(numFiles, Equals, 1)

	err = t.r.AddItem(fullpath)
	c.Assert(err, IsNil)

	numFiles, err = path.NumFilesInDir(nibsPath)
	c.Assert(err, IsNil)
	c.Assert(numFiles, Equals, 1)

	nibID, err := t.r.pathToNIBID(filename)
	c.Assert(err, IsNil)
	nib, err := t.r.nibStore.Get(nibID)
	c.Assert(err, IsNil)
	c.Assert(len(nib.Revisions), Equals, 1)
}

func (t *RepositoryAddItemTests) TestAddDotLara(c *C) {
	err := t.r.AddItem(filepath.Join(t.r.Path, managementDirName))
	c.Assert(err, Equals, ErrRefusingWorkOnDotLara)
}

func (t *RepositoryAddItemTests) TestAddDotLaraModified(c *C) {
	path := string(filepath.Separator) + filepath.Join(t.r.Path, managementDirName)

	err := t.r.AddItem(path)
	if runtime.GOOS != "windows" {
		c.Assert(err, Equals, ErrRefusingWorkOnDotLara)
	} else {
		c.Assert(err, NotNil)
	}
}

func (t *RepositoryAddItemTests) TestAddDotLaraSubdir(c *C) {
	path := filepath.Join(t.r.Path, managementDirName, nibsDirName)
	err := t.r.AddItem(path)
	c.Assert(err, Equals, ErrRefusingWorkOnDotLara)
}
<|endoftext|>


<commit_before>//// +build examples

package main

import (
	"encoding/hex"
	"fmt"
	"gopkg.in/jcmturner/gokrb5.v5/credentials"
	"gopkg.in/jcmturner/gokrb5.v5/keytab"
	"gopkg.in/jcmturner/gokrb5.v5/service"
	"gopkg.in/jcmturner/gokrb5.v5/testdata"
	"log"
	"net/http"
	"os"
)

func main() {
	// Create logger
	l := log.New(os.Stderr, "GOKRB5 Service: ", log.Ldate|log.Ltime|log.Lshortfile)

	// Load the service's keytab
	b, _ := hex.DecodeString(testdata.HTTP_KEYTAB)
	kt, _ := keytab.Parse(b)

	// Create the application's specific handler
	th := http.HandlerFunc(testAppHandler)

	// Set up handler mappings wrapping in the SPNEGOKRB5Authenticate handler wrapper
	mux := http.NewServeMux()
	mux.Handle("/", service.SPNEGOKRB5Authenticate(th, kt, "", false, l))

	// Start up the web server
	log.Fatal(http.ListenAndServe(":9080", mux))
}

// Simple application specific handler
func testAppHandler(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusOK)
	ctx := r.Context()
	fmt.Fprintf(w, "\nTEST.GOKRB5 Handler\nAuthenticed user: %s\nUser's realm: %s\n",
		ctx.Value(service.CTXKeyCredentials).(credentials.Credentials).Username,
		ctx.Value(service.CTXKeyCredentials).(credentials.Credentials).Realm)
	return
}
<commit_msg>fix comment<commit_after>// +build examples

package main

import (
	"encoding/hex"
	"fmt"
	"gopkg.in/jcmturner/gokrb5.v5/credentials"
	"gopkg.in/jcmturner/gokrb5.v5/keytab"
	"gopkg.in/jcmturner/gokrb5.v5/service"
	"gopkg.in/jcmturner/gokrb5.v5/testdata"
	"log"
	"net/http"
	"os"
)

func main() {
	// Create logger
	l := log.New(os.Stderr, "GOKRB5 Service: ", log.Ldate|log.Ltime|log.Lshortfile)

	// Load the service's keytab
	b, _ := hex.DecodeString(testdata.HTTP_KEYTAB)
	kt, _ := keytab.Parse(b)

	// Create the application's specific handler
	th := http.HandlerFunc(testAppHandler)

	// Set up handler mappings wrapping in the SPNEGOKRB5Authenticate handler wrapper
	mux := http.NewServeMux()
	mux.Handle("/", service.SPNEGOKRB5Authenticate(th, kt, "", false, l))

	// Start up the web server
	log.Fatal(http.ListenAndServe(":9080", mux))
}

// Simple application specific handler
func testAppHandler(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusOK)
	ctx := r.Context()
	fmt.Fprintf(w, "\nTEST.GOKRB5 Handler\nAuthenticed user: %s\nUser's realm: %s\n",
		ctx.Value(service.CTXKeyCredentials).(credentials.Credentials).Username,
		ctx.Value(service.CTXKeyCredentials).(credentials.Credentials).Realm)
	return
}
<|endoftext|>


<commit_before>package main

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

	"github.com/jsgoecke/tesla"
)

func main() {
	client, err := tesla.NewClient(
		&tesla.Auth{
			ClientID:     os.Getenv("TESLA_CLIENT_ID"),
			ClientSecret: os.Getenv("TESLA_CLIENT_SECRET"),
			Email:        os.Getenv("TESLA_USERNAME"),
			Password:     os.Getenv("TESLA_PASSWORD"),
		})
	if err != nil {
		panic(err)
	}

	vehicles, err := client.Vehicles()
	if err != nil {
		panic(err)
	}
	vehicle := vehicles[0]
	status, err := vehicle.MobileEnabled()

	if err != nil {
		panic(err)
	}

	fmt.Println(status)
	fmt.Println(vehicle.ChargeState())
	fmt.Println(vehicle.ClimateState())
	fmt.Println(vehicle.DriveState())
	fmt.Println(vehicle.GuiSettings())
	fmt.Println(vehicle.VehicleState())
	fmt.Println(vehicle.HonkHorn())
	fmt.Println(vehicle.FlashLights())
	fmt.Println(vehicle.Wakeup())
	fmt.Println(vehicle.OpenChargePort())
	fmt.Println(vehicle.ResetValetPIN())
	fmt.Println(vehicle.SetChargeLimitStandard())
	fmt.Println(vehicle.SetChargeLimit(50))
	fmt.Println(vehicle.StartCharging())
	fmt.Println(vehicle.StopCharging())
	fmt.Println(vehicle.SetChargeLimitMax())
	fmt.Println(vehicle.StartAirConditioning())
	fmt.Println(vehicle.StopAirConditioning())
	fmt.Println(vehicle.UnlockDoors())
	fmt.Println(vehicle.LockDoors())
	fmt.Println(vehicle.SetTemprature(72.0, 72.0))
	fmt.Println(vehicle.Start(os.Getenv("TESLA_PASSWORD")))
	fmt.Println(vehicle.OpenTrunk("rear"))
	fmt.Println(vehicle.OpenTrunk("front"))
	fmt.Println(vehicle.MovePanoRoof("vent", 0))
	fmt.Println(vehicle.MovePanoRoof("open", 0))
	fmt.Println(vehicle.MovePanoRoof("move", 50))
	fmt.Println(vehicle.MovePanoRoof("close", 0))
	fmt.Println(vehicle.TriggerHomelink())

	// Take care with these, as the car will move
	fmt.Println(vehicle.AutoparkForward())
	fmt.Println(vehicle.AutoparkReverse())
	// Take care with these, as the car will move

	// Stream vehicle events
	eventChan, errChan, err := vehicle.Stream()
	if err != nil {
		fmt.Println(err)
	} else {
		for {
			select {
			case event := <-eventChan:
				eventJSON, _ := json.Marshal(event)
				fmt.Println(string(eventJSON))
			case err = <-errChan:
				fmt.Println("HTTP Stream timeout!")
				break
			}
		}
	}
}
<commit_msg>Fixed a problem with the example that was breaking the build<commit_after>package main

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

	"github.com/jsgoecke/tesla"
)

func main() {
	client, err := tesla.NewClient(
		&tesla.Auth{
			ClientID:     os.Getenv("TESLA_CLIENT_ID"),
			ClientSecret: os.Getenv("TESLA_CLIENT_SECRET"),
			Email:        os.Getenv("TESLA_USERNAME"),
			Password:     os.Getenv("TESLA_PASSWORD"),
		})
	if err != nil {
		panic(err)
	}

	vehicles, err := client.Vehicles()
	if err != nil {
		panic(err)
	}
	vehicle := vehicles[0]
	status, err := vehicle.MobileEnabled()

	if err != nil {
		panic(err)
	}

	fmt.Println(status)
	fmt.Println(vehicle.ChargeState())
	fmt.Println(vehicle.ClimateState())
	fmt.Println(vehicle.DriveState())
	fmt.Println(vehicle.GuiSettings())
	fmt.Println(vehicle.VehicleState())
	fmt.Println(vehicle.HonkHorn())
	fmt.Println(vehicle.FlashLights())
	fmt.Println(vehicle.Wakeup())
	fmt.Println(vehicle.OpenChargePort())
	fmt.Println(vehicle.ResetValetPIN())
	fmt.Println(vehicle.SetChargeLimitStandard())
	fmt.Println(vehicle.SetChargeLimit(50))
	fmt.Println(vehicle.StartCharging())
	fmt.Println(vehicle.StopCharging())
	fmt.Println(vehicle.SetChargeLimitMax())
	fmt.Println(vehicle.StartAirConditioning())
	fmt.Println(vehicle.StopAirConditioning())
	fmt.Println(vehicle.UnlockDoors())
	fmt.Println(vehicle.LockDoors())
	fmt.Println(vehicle.SetTemprature(72.0, 72.0))
	fmt.Println(vehicle.Start(os.Getenv("TESLA_PASSWORD")))
	fmt.Println(vehicle.OpenTrunk("rear"))
	fmt.Println(vehicle.OpenTrunk("front"))
	fmt.Println(vehicle.MovePanoRoof("vent", 0))
	fmt.Println(vehicle.MovePanoRoof("open", 0))
	fmt.Println(vehicle.MovePanoRoof("move", 50))
	fmt.Println(vehicle.MovePanoRoof("close", 0))
	fmt.Println(vehicle.TriggerHomelink())

	// // Take care with these, as the car will move
	fmt.Println(vehicle.AutoparkForward())
	fmt.Println(vehicle.AutoparkReverse())
	// Take care with these, as the car will move

	// Stream vehicle events
	eventChan, errChan, err := vehicle.Stream()
	if err != nil {
		fmt.Println(err)
	} else {
		for {
			select {
			case event := <-eventChan:
				eventJSON, _ := json.Marshal(event)
				fmt.Println(string(eventJSON))
			case err = <-errChan:
				fmt.Println(err)
				fmt.Println("Reconnecting!")
				eventChan, errChan, err = vehicle.Stream()
				if err != nil {
					break
				}
			}
		}
	}
}
<|endoftext|>


<commit_before>package pg

import (
	"database/sql"
	"errors"

	"golang.org/x/net/context"
)

// DB holds methods common to the DB, Tx, and Stmt types
// in package sql.
type DB interface {
	Query(string, ...interface{}) (*sql.Rows, error)
	QueryRow(string, ...interface{}) *sql.Row
	Exec(string, ...interface{}) (sql.Result, error)
}

// Committer provides methods to commit or roll back a single transaction.
type Committer interface {
	Commit() error
	Rollback() error
}

// Tx represents a SQL transaction.
// Type sql.Tx satisfies this interface.
type Tx interface {
	DB
	Committer
}

// Beginner is used by Begin to create a new transaction.
// It is an optional alternative to the Begin signature provided by
// package sql.
type Beginner interface {
	Begin() (Tx, error)
}

// key is an unexported type for keys defined in this package.
// This prevents collisions with keys defined in other packages.
type key int

// dbKey is the key for DB values in Contexts.  It is
// unexported; clients use pg.NewContext and pg.FromContext
// instead of using this key directly.
var dbKey key

// Begin opens a new transaction on the database
// stored in ctx. The stored database must
// provide a Begin method like sql.DB or satisfy
// the interface Beginner.
// Begin returns the new transaction and
// a new context with the transaction as its
// associated database.
func Begin(ctx context.Context) (Committer, context.Context, error) {
	tx, err := begin(ctx)
	if err != nil {
		return nil, nil, err
	}
	ctx = NewContext(ctx, tx)
	return tx, ctx, nil
}

func begin(ctx context.Context) (Tx, error) {
	type beginner interface {
		Begin() (*sql.Tx, error)
	}
	switch db := FromContext(ctx).(type) {
	case beginner: // e.g. *sql.DB
		return db.Begin()
	case Beginner: // e.g. pgtest.noCommitDB
		return db.Begin()
	}
	return nil, errors.New("unknown db type")
}

// NewContext returns a new Context that carries value db.
func NewContext(ctx context.Context, db DB) context.Context {
	return context.WithValue(ctx, dbKey, db)
}

// FromContext returns the DB value stored in ctx.
// If there is no DB value, FromContext panics.
func FromContext(ctx context.Context) DB {
	return ctx.Value(dbKey).(DB)
}
<commit_msg>database/pg: include invalid type in Begin error<commit_after>package pg

import (
	"database/sql"
	"fmt"

	"golang.org/x/net/context"
)

// DB holds methods common to the DB, Tx, and Stmt types
// in package sql.
type DB interface {
	Query(string, ...interface{}) (*sql.Rows, error)
	QueryRow(string, ...interface{}) *sql.Row
	Exec(string, ...interface{}) (sql.Result, error)
}

// Committer provides methods to commit or roll back a single transaction.
type Committer interface {
	Commit() error
	Rollback() error
}

// Tx represents a SQL transaction.
// Type sql.Tx satisfies this interface.
type Tx interface {
	DB
	Committer
}

// Beginner is used by Begin to create a new transaction.
// It is an optional alternative to the Begin signature provided by
// package sql.
type Beginner interface {
	Begin() (Tx, error)
}

// key is an unexported type for keys defined in this package.
// This prevents collisions with keys defined in other packages.
type key int

// dbKey is the key for DB values in Contexts.  It is
// unexported; clients use pg.NewContext and pg.FromContext
// instead of using this key directly.
var dbKey key

// Begin opens a new transaction on the database
// stored in ctx. The stored database must
// provide a Begin method like sql.DB or satisfy
// the interface Beginner.
// Begin returns the new transaction and
// a new context with the transaction as its
// associated database.
func Begin(ctx context.Context) (Committer, context.Context, error) {
	tx, err := begin(ctx)
	if err != nil {
		return nil, nil, err
	}
	ctx = NewContext(ctx, tx)
	return tx, ctx, nil
}

func begin(ctx context.Context) (Tx, error) {
	type beginner interface {
		Begin() (*sql.Tx, error)
	}
	switch db := FromContext(ctx).(type) {
	case beginner: // e.g. *sql.DB
		return db.Begin()
	case Beginner: // e.g. pgtest.noCommitDB
		return db.Begin()
	}
	return nil, fmt.Errorf("unknown db type %T", FromContext(ctx))
}

// NewContext returns a new Context that carries value db.
func NewContext(ctx context.Context, db DB) context.Context {
	return context.WithValue(ctx, dbKey, db)
}

// FromContext returns the DB value stored in ctx.
// If there is no DB value, FromContext panics.
func FromContext(ctx context.Context) DB {
	return ctx.Value(dbKey).(DB)
}
<|endoftext|>


<commit_before>/*
	Go Language Raspberry Pi Interface
	(c) Copyright David Thorpe 2016-2017
	All Rights Reserved
	Documentation http://djthorpe.github.io/gopi/
	For Licensing and Usage information, please see LICENSE.md
*/

// The client connects to a remote server
package main

//go:generate protoc helloworld/helloworld.proto --go_out=plugins=grpc:.

import (
	"context"
	"fmt"
	"os"

	// Frameworks
	gopi "github.com/djthorpe/gopi"

	// Modules
	_ "github.com/djthorpe/gopi/sys/logger"
	_ "github.com/djthorpe/gopi/sys/rpc"
)

var (
	cancel context.CancelFunc
)

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

func MainLoop(app *gopi.AppInstance, done chan struct{}) error {
	if client := app.ModuleInstance("rpc/client").(gopi.RPCClient); client == nil {
		return fmt.Errorf("Missing module: rpc/client")
	} else if err := client.Connect(); err != nil {
		return err
	} else {
		defer client.Disconnect()

		// Do things here
		if modules, err := client.Modules(); err != nil {
			return err
		} else {
			app.Logger.Info("client=%v modules=%v", client, modules)
		}
	}

	app.WaitForSignal()

	if cancel != nil {
		cancel()
	}

	// Finish gracefully
	done <- gopi.DONE
	return nil
}

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

func DiscoveryLoop(app *gopi.AppInstance, done chan struct{}) error {
	var ctx context.Context

	if discovery := app.ModuleInstance("mdns").(gopi.RPCServiceDiscovery); discovery == nil {
		return fmt.Errorf("Missing module: mdns")
	} else {
		ctx, cancel = context.WithCancel(context.Background())
		app.Logger.Debug("DiscoveryLoop: Discovery.Browse started")
		discovery.Browse(ctx, "_gopi._tcp", func(service *gopi.RPCService) {
			if service != nil {
				fmt.Println("service=", service)
			}
		})
	}
	// Wait for done
	app.Logger.Debug("DiscoveryLoop: WAIT FOR DONE")
	_ = <-done
	app.Logger.Debug("DiscoveryLoop: DONE")
	return nil
}

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

func main_inner() int {

	// Create the configuration
	config := gopi.NewAppConfig("mdns", "rpc/client")

	// Create the application
	app, err := gopi.NewAppInstance(config)
	if err != nil {
		if err != gopi.ErrHelp {
			fmt.Fprintln(os.Stderr, err)
			return -1
		}
		return 0
	}
	defer app.Close()

	// Run the application
	if err := app.Run(MainLoop, DiscoveryLoop); err != nil {
		fmt.Fprintln(os.Stderr, err)
		return -1
	}
	return 0
}

func main() {
	os.Exit(main_inner())
}
<commit_msg>Updated<commit_after>/*
	Go Language Raspberry Pi Interface
	(c) Copyright David Thorpe 2016-2017
	All Rights Reserved
	Documentation http://djthorpe.github.io/gopi/
	For Licensing and Usage information, please see LICENSE.md
*/

// The client connects to a remote server
package main

//go:generate protoc helloworld/helloworld.proto --go_out=plugins=grpc:.

import (
	"context"
	"fmt"
	"os"

	// Frameworks
	gopi "github.com/djthorpe/gopi"

	// Modules
	_ "github.com/djthorpe/gopi/sys/logger"
	_ "github.com/djthorpe/gopi/sys/rpc"
)

var (
	cancel context.CancelFunc
)

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

func MainLoop(app *gopi.AppInstance, done chan struct{}) error {
	if client := app.ModuleInstance("rpc/client").(gopi.RPCClient); client == nil {
		return fmt.Errorf("Missing module: rpc/client")
	} else if err := client.Connect(); err != nil {
		return err
	} else {
		defer client.Disconnect()

		// Do things here
		if modules, err := client.Modules(); err != nil {
			return err
		} else {
			app.Logger.Info("client=%v modules=%v", client, modules)
		}
	}

	app.WaitForSignal()

	if cancel != nil {
		cancel()
	}

	// Finish gracefully
	done <- gopi.DONE
	return nil
}

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

func DiscoveryLoop(app *gopi.AppInstance, done chan struct{}) error {
	var ctx context.Context

	if discovery := app.ModuleInstance("mdns").(gopi.RPCServiceDiscovery); discovery == nil {
		return fmt.Errorf("Missing module: mdns")
	} else {
		ctx, cancel = context.WithCancel(context.Background())
		app.Logger.Debug("DiscoveryLoop: Discovery.Browse started")
		discovery.Browse(ctx, "_gopi._tcp", func(service *gopi.RPCService) {
			if service != nil {
				fmt.Println("service=", service)
			}
		})
	}
	// Wait for done
	_ = <-done
	return nil
}

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

func main_inner() int {

	// Create the configuration
	config := gopi.NewAppConfig("mdns", "rpc/client")

	// Create the application
	app, err := gopi.NewAppInstance(config)
	if err != nil {
		if err != gopi.ErrHelp {
			fmt.Fprintln(os.Stderr, err)
			return -1
		}
		return 0
	}
	defer app.Close()

	// Run the application
	if err := app.Run(MainLoop, DiscoveryLoop); err != nil {
		fmt.Fprintln(os.Stderr, err)
		return -1
	}
	return 0
}

func main() {
	os.Exit(main_inner())
}
<|endoftext|>


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

// +build example jsgo

// This is an example to implement an audio player.
// See examples/wav for a simpler example to play a sound file.

package main

import (
	"fmt"
	"image/color"
	"io/ioutil"
	"log"
	"time"

	"github.com/hajimehoshi/ebiten"
	"github.com/hajimehoshi/ebiten/audio"
	"github.com/hajimehoshi/ebiten/audio/vorbis"
	"github.com/hajimehoshi/ebiten/audio/wav"
	"github.com/hajimehoshi/ebiten/ebitenutil"
	raudio "github.com/hajimehoshi/ebiten/examples/resources/audio"
	"github.com/hajimehoshi/ebiten/inpututil"
)

const (
	screenWidth  = 320
	screenHeight = 240

	sampleRate = 22050
)

var (
	playerBarColor     = color.RGBA{0x80, 0x80, 0x80, 0xff}
	playerCurrentColor = color.RGBA{0xff, 0xff, 0xff, 0xff}
)

// Player represents the current audio state.
type Player struct {
	audioContext *audio.Context
	audioPlayer  *audio.Player
	current      time.Duration
	total        time.Duration
	seBytes      []byte
	seCh         chan []byte
	volume128    int
}

func playerBarRect() (x, y, w, h int) {
	w, h = 300, 4
	x = (screenWidth - w) / 2
	y = screenHeight - h - 16
	return
}

func NewPlayer(audioContext *audio.Context) (*Player, error) {
	const bytesPerSample = 4 // TODO: This should be defined in audio package
	s, err := vorbis.Decode(audioContext, audio.BytesReadSeekCloser(raudio.Ragtime_ogg))
	if err != nil {
		return nil, err
	}
	p, err := audio.NewPlayer(audioContext, s)
	if err != nil {
		return nil, err
	}
	player := &Player{
		audioContext: audioContext,
		audioPlayer:  p,
		total:        time.Second * time.Duration(s.Length()) / bytesPerSample / sampleRate,
		volume128:    128,
		seCh:         make(chan []byte),
	}
	if player.total == 0 {
		player.total = 1
	}
	player.audioPlayer.Play()
	go func() {
		s, err := wav.Decode(audioContext, audio.BytesReadSeekCloser(raudio.Jab_wav))
		if err != nil {
			log.Fatal(err)
			return
		}
		b, err := ioutil.ReadAll(s)
		if err != nil {
			log.Fatal(err)
			return
		}
		player.seCh <- b
	}()
	return player, nil
}

func (p *Player) update() error {
	select {
	case p.seBytes = <-p.seCh:
		close(p.seCh)
		p.seCh = nil
	default:
	}

	if p.audioPlayer.IsPlaying() {
		p.current = p.audioPlayer.Current()
	}
	p.seekBarIfNeeded()
	p.switchPlayStateIfNeeded()
	p.playSEIfNeeded()
	p.updateVolumeIfNeeded()

	if inpututil.IsKeyJustPressed(ebiten.KeyB) {
		b := ebiten.IsRunnableInBackground()
		ebiten.SetRunnableInBackground(!b)
	}
	return nil
}

func (p *Player) playSEIfNeeded() {
	if p.seBytes == nil {
		// Bytes for the SE is not loaded yet.
		return
	}

	if !inpututil.IsKeyJustPressed(ebiten.KeyP) {
		return
	}
	sePlayer, _ := audio.NewPlayerFromBytes(p.audioContext, p.seBytes)
	sePlayer.Play()
}

func (p *Player) updateVolumeIfNeeded() {
	if ebiten.IsKeyPressed(ebiten.KeyZ) {
		p.volume128--
	}
	if ebiten.IsKeyPressed(ebiten.KeyX) {
		p.volume128++
	}
	if p.volume128 < 0 {
		p.volume128 = 0
	}
	if 128 < p.volume128 {
		p.volume128 = 128
	}
	p.audioPlayer.SetVolume(float64(p.volume128) / 128)
}

func (p *Player) switchPlayStateIfNeeded() {
	if !inpututil.IsKeyJustPressed(ebiten.KeyS) {
		return
	}
	if p.audioPlayer.IsPlaying() {
		p.audioPlayer.Pause()
		return
	}
	p.audioPlayer.Play()
}

func (p *Player) seekBarIfNeeded() {
	if !inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
		return
	}

	// Calculate the next seeking position from the current cursor position.
	x, y := ebiten.CursorPosition()
	bx, by, bw, bh := playerBarRect()
	const padding = 4
	if y < by-padding || by+bh+padding <= y {
		return
	}
	if x < bx || bx+bw <= x {
		return
	}
	pos := time.Duration(x-bx) * p.total / time.Duration(bw)
	p.current = pos
	p.audioPlayer.Seek(pos)
}

func (p *Player) close() error {
	return p.audioPlayer.Close()
}

func (p *Player) draw(screen *ebiten.Image) {
	// Draw the bar.
	x, y, w, h := playerBarRect()
	ebitenutil.DrawRect(screen, float64(x), float64(y), float64(w), float64(h), playerBarColor)

	// Draw the cursor on the bar.
	c := p.current
	cw, ch := 4, 10
	cx := int(time.Duration(w)*c/p.total) + x - cw/2
	cy := y - (ch-h)/2
	ebitenutil.DrawRect(screen, float64(cx), float64(cy), float64(cw), float64(ch), playerCurrentColor)

	// Compose the curren time text.
	m := (c / time.Minute) % 100
	s := (c / time.Second) % 60
	currentTimeStr := fmt.Sprintf("%02d:%02d", m, s)

	// Draw the debug message.
	msg := fmt.Sprintf(`TPS: %0.2f
Press S to toggle Play/Pause
Press P to play SE
Press Z or X to change volume of the music
Press B to switch the run-in-background state
Current Time: %s`, ebiten.CurrentTPS(), currentTimeStr)
	ebitenutil.DebugPrint(screen, msg)
}

var (
	musicPlayer *Player
)

func update(screen *ebiten.Image) error {
	if err := musicPlayer.update(); err != nil {
		return err
	}

	if ebiten.IsDrawingSkipped() {
		return nil
	}

	musicPlayer.draw(screen)
	return nil
}

func main() {
	audioContext, err := audio.NewContext(sampleRate)
	if err != nil {
		log.Fatal(err)
	}

	musicPlayer, err = NewPlayer(audioContext)
	if err != nil {
		log.Fatal(err)
	}
	if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Audio (Ebiten Demo)"); err != nil {
		log.Fatal(err)
	}
	if err := musicPlayer.close(); err != nil {
		log.Fatal(err)
	}
}
<commit_msg>examples/audio: Show the current volume (#730)<commit_after>// Copyright 2016 Hajime Hoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// +build example jsgo

// This is an example to implement an audio player.
// See examples/wav for a simpler example to play a sound file.

package main

import (
	"fmt"
	"image/color"
	"io/ioutil"
	"log"
	"time"

	"github.com/hajimehoshi/ebiten"
	"github.com/hajimehoshi/ebiten/audio"
	"github.com/hajimehoshi/ebiten/audio/vorbis"
	"github.com/hajimehoshi/ebiten/audio/wav"
	"github.com/hajimehoshi/ebiten/ebitenutil"
	raudio "github.com/hajimehoshi/ebiten/examples/resources/audio"
	"github.com/hajimehoshi/ebiten/inpututil"
)

const (
	screenWidth  = 320
	screenHeight = 240

	sampleRate = 22050
)

var (
	playerBarColor     = color.RGBA{0x80, 0x80, 0x80, 0xff}
	playerCurrentColor = color.RGBA{0xff, 0xff, 0xff, 0xff}
)

// Player represents the current audio state.
type Player struct {
	audioContext *audio.Context
	audioPlayer  *audio.Player
	current      time.Duration
	total        time.Duration
	seBytes      []byte
	seCh         chan []byte
	volume128    int
}

func playerBarRect() (x, y, w, h int) {
	w, h = 300, 4
	x = (screenWidth - w) / 2
	y = screenHeight - h - 16
	return
}

func NewPlayer(audioContext *audio.Context) (*Player, error) {
	const bytesPerSample = 4 // TODO: This should be defined in audio package
	s, err := vorbis.Decode(audioContext, audio.BytesReadSeekCloser(raudio.Ragtime_ogg))
	if err != nil {
		return nil, err
	}
	p, err := audio.NewPlayer(audioContext, s)
	if err != nil {
		return nil, err
	}
	player := &Player{
		audioContext: audioContext,
		audioPlayer:  p,
		total:        time.Second * time.Duration(s.Length()) / bytesPerSample / sampleRate,
		volume128:    128,
		seCh:         make(chan []byte),
	}
	if player.total == 0 {
		player.total = 1
	}
	player.audioPlayer.Play()
	go func() {
		s, err := wav.Decode(audioContext, audio.BytesReadSeekCloser(raudio.Jab_wav))
		if err != nil {
			log.Fatal(err)
			return
		}
		b, err := ioutil.ReadAll(s)
		if err != nil {
			log.Fatal(err)
			return
		}
		player.seCh <- b
	}()
	return player, nil
}

func (p *Player) update() error {
	select {
	case p.seBytes = <-p.seCh:
		close(p.seCh)
		p.seCh = nil
	default:
	}

	if p.audioPlayer.IsPlaying() {
		p.current = p.audioPlayer.Current()
	}
	p.seekBarIfNeeded()
	p.switchPlayStateIfNeeded()
	p.playSEIfNeeded()
	p.updateVolumeIfNeeded()

	if inpututil.IsKeyJustPressed(ebiten.KeyB) {
		b := ebiten.IsRunnableInBackground()
		ebiten.SetRunnableInBackground(!b)
	}
	return nil
}

func (p *Player) playSEIfNeeded() {
	if p.seBytes == nil {
		// Bytes for the SE is not loaded yet.
		return
	}

	if !inpututil.IsKeyJustPressed(ebiten.KeyP) {
		return
	}
	sePlayer, _ := audio.NewPlayerFromBytes(p.audioContext, p.seBytes)
	sePlayer.Play()
}

func (p *Player) updateVolumeIfNeeded() {
	if ebiten.IsKeyPressed(ebiten.KeyZ) {
		p.volume128--
	}
	if ebiten.IsKeyPressed(ebiten.KeyX) {
		p.volume128++
	}
	if p.volume128 < 0 {
		p.volume128 = 0
	}
	if 128 < p.volume128 {
		p.volume128 = 128
	}
	p.audioPlayer.SetVolume(float64(p.volume128) / 128)
}

func (p *Player) switchPlayStateIfNeeded() {
	if !inpututil.IsKeyJustPressed(ebiten.KeyS) {
		return
	}
	if p.audioPlayer.IsPlaying() {
		p.audioPlayer.Pause()
		return
	}
	p.audioPlayer.Play()
}

func (p *Player) seekBarIfNeeded() {
	if !inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
		return
	}

	// Calculate the next seeking position from the current cursor position.
	x, y := ebiten.CursorPosition()
	bx, by, bw, bh := playerBarRect()
	const padding = 4
	if y < by-padding || by+bh+padding <= y {
		return
	}
	if x < bx || bx+bw <= x {
		return
	}
	pos := time.Duration(x-bx) * p.total / time.Duration(bw)
	p.current = pos
	p.audioPlayer.Seek(pos)
}

func (p *Player) close() error {
	return p.audioPlayer.Close()
}

func (p *Player) draw(screen *ebiten.Image) {
	// Draw the bar.
	x, y, w, h := playerBarRect()
	ebitenutil.DrawRect(screen, float64(x), float64(y), float64(w), float64(h), playerBarColor)

	// Draw the cursor on the bar.
	c := p.current
	cw, ch := 4, 10
	cx := int(time.Duration(w)*c/p.total) + x - cw/2
	cy := y - (ch-h)/2
	ebitenutil.DrawRect(screen, float64(cx), float64(cy), float64(cw), float64(ch), playerCurrentColor)

	// Compose the curren time text.
	m := (c / time.Minute) % 100
	s := (c / time.Second) % 60
	currentTimeStr := fmt.Sprintf("%02d:%02d", m, s)

	// Draw the debug message.
	msg := fmt.Sprintf(`TPS: %0.2f
Press S to toggle Play/Pause
Press P to play SE
Press Z or X to change volume of the music
Press B to switch the run-in-background state
Current Time: %s
Current Volume: %d/128`, ebiten.CurrentTPS(), currentTimeStr, int(p.audioPlayer.Volume()*128))
	ebitenutil.DebugPrint(screen, msg)
}

var (
	musicPlayer *Player
)

func update(screen *ebiten.Image) error {
	if err := musicPlayer.update(); err != nil {
		return err
	}

	if ebiten.IsDrawingSkipped() {
		return nil
	}

	musicPlayer.draw(screen)
	return nil
}

func main() {
	audioContext, err := audio.NewContext(sampleRate)
	if err != nil {
		log.Fatal(err)
	}

	musicPlayer, err = NewPlayer(audioContext)
	if err != nil {
		log.Fatal(err)
	}
	if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Audio (Ebiten Demo)"); err != nil {
		log.Fatal(err)
	}
	if err := musicPlayer.close(); err != nil {
		log.Fatal(err)
	}
}
<|endoftext|>


<commit_before>package main

import (
        "fmt"
        "log"
        "net/http"
        "github.com/gorilla/mux"
        "encoding/json"
        "encoding/csv"
    "io"
    "os"
    "io/ioutil"
    "strconv"
    "bufio"
    )

type RequestMessage struct {
    CarMaker string
    CarModel string
    NumDays string
    NumUnits string
}

type ResponseMessage struct {
    Field1 string
    Field2 string
}

func main() {

router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/", Index)
router.HandleFunc("/listCar", endpointFunc)
router.HandleFunc("/registerCar/{param}", endpointFunc2JSONInput)

log.Fatal(http.ListenAndServe(":8081", router))
}

func Index(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln(w, "Service OK")
}

func endpointFunc(w http.ResponseWriter, r *http.Request) {
    readToFile(w)
}

func endpointFunc2JSONInput(w http.ResponseWriter, r *http.Request) {
    var requestMessage RequestMessage
    body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
    if err != nil {
        panic(err)
    }
    if err := r.Body.Close(); err != nil {
        panic(err)
    }
    if err := json.Unmarshal(body, &requestMessage); err != nil {
        w.Header().Set("Content-Type", "application/json; charset=UTF-8")
        w.WriteHeader(422) // unprocessable entity
        if err := json.NewEncoder(w).Encode(err); err != nil {
            panic(err)
        }
    } else {
        fmt.Fprintln(w, "Successfully received car registration. Here is your rental:\n",requestMessage)
        writeToFile(w,requestMessage)
    }
}

func writeToFile(w http.ResponseWriter,r RequestMessage) {

    var precio int = 100
    numDays,err := strconv.Atoi(r.NumDays)
    numUnits,err := strconv.Atoi(r.NumUnits)
    precio = numUnits*numDays*precio
    fmt.Fprintln(w, "precio",precio)
    file, err := os.OpenFile("rentals.csv", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
    if err!=nil {
        json.NewEncoder(w).Encode(err)
        return
    }
    writer := csv.NewWriter(file)

    var data1 = []string{r.CarMaker, r.CarModel,r.NumDays,r.NumUnits,strconv.Itoa(precio)}
    writer.Write(data1)
    writer.Flush()
    file.Close()
}

func readToFile(w http.ResponseWriter) {
    file, err := os.Open("rentals.csv")
    if err!=nil {
    json.NewEncoder(w).Encode(err)
    return
    }
    reader := csv.NewReader(bufio.NewReader(file))
    for {
        record, err := reader.Read()
        if err == io.EOF {
                break
            }
        fmt.Fprintln(w,"Lloguer:", record[0])
        fmt.Fprintln(w,"CarMaker:", record[0])
        fmt.Fprintln(w,"CarModel:", record[1])
        fmt.Fprintln(w,"NumDays:", record[2])
        fmt.Fprintln(w,"NumUnits:", record[3])
        fmt.Fprintln(w,"Precio:", record[4])
        fmt.Fprintln(w,"-------------------------------\n")

    }
  }
<commit_msg>petitas modificacions<commit_after>package main

import (
        "fmt"
        "log"
        "net/http"
        "github.com/gorilla/mux"
        "encoding/json"
        "encoding/csv"
    "io"
    "os"
    "io/ioutil"
    "strconv"
    "bufio"
    )

type RequestMessage struct {
    CarMaker string
    CarModel string
    NumDays string
    NumUnits string
}

type ResponseMessage struct {
    Field1 string
    Field2 string
}

func main() {

router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/", Index)
router.HandleFunc("/listCar", endpointFunc)
router.HandleFunc("/registerCar/{param}", endpointFunc2JSONInput)

log.Fatal(http.ListenAndServe(":8081", router))
}

func Index(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln(w, "Service OK")
}

func endpointFunc(w http.ResponseWriter, r *http.Request) {
    readToFile(w)
}

func endpointFunc2JSONInput(w http.ResponseWriter, r *http.Request) {
    var requestMessage RequestMessage
    body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
    if err != nil {
        panic(err)
    }
    if err := r.Body.Close(); err != nil {
        panic(err)
    }
    if err := json.Unmarshal(body, &requestMessage); err != nil {
        w.Header().Set("Content-Type", "application/json; charset=UTF-8")
        w.WriteHeader(422) // unprocessable entity
        if err := json.NewEncoder(w).Encode(err); err != nil {
            panic(err)
        }
    } else {
        fmt.Fprintln(w, "Successfully received car registration. Here is your rental:\n",requestMessage)
        writeToFile(w,requestMessage)
    }
}

func writeToFile(w http.ResponseWriter,r RequestMessage) {

    var precio int = 100
    numDays,err := strconv.Atoi(r.NumDays)
    numUnits,err := strconv.Atoi(r.NumUnits)
    precio = numUnits*numDays*precio
    fmt.Fprintln(w, "precio",precio)
    file, err := os.OpenFile("rentals.csv", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
    if err!=nil {
        json.NewEncoder(w).Encode(err)
        return
    }
    writer := csv.NewWriter(file)

    var data1 = []string{r.CarMaker, r.CarModel,r.NumDays,r.NumUnits,strconv.Itoa(precio)}
    writer.Write(data1)
    writer.Flush()
    file.Close()
}

func readToFile(w http.ResponseWriter) {
    file, err := os.Open("rentals.csv")
    if err!=nil {
    json.NewEncoder(w).Encode(err)
    return
    }
    reader := csv.NewReader(bufio.NewReader(file))
    fmt.Fprintln(w,"{")
    for {
        record, err := reader.Read()
        if err == io.EOF {
                break
            }
        fmt.Fprintln(w,"  {")
        fmt.Fprintln(w,"    CarMaker:", record[0])
        fmt.Fprintln(w,"    CarModel:", record[1])
        fmt.Fprintln(w,"    NumDays:", record[2])
        fmt.Fprintln(w,"    NumUnits:", record[3])
        fmt.Fprintln(w,"    Precio:", record[4])
        fmt.Fprintln(w,"  }")
    }
    fmt.Fprintln(w,"}")
  }
<|endoftext|>


<commit_before>package execution

import (
	"fmt"
	"os"
	"path/filepath"
	"strconv"
	"time"
	"webup/backoops/config"
	"webup/backoops/domain"
	"webup/backoops/options"

	"github.com/ncw/swift"
)

const (
	containerName = "backups"
)

type Executor interface {
	GetOutputFileExtension() string
	Execute(workingDir string, output string) error
}

func ExecuteBackup(project domain.Project, backup domain.Backup, options options.Options) error {

	tmpDir := "._tmp"

	if _, err := os.Stat(tmpDir); os.IsNotExist(err) {
		os.MkdirAll(tmpDir, os.ModePerm)
	}

	executor := Pliz{}

	outputFile := fmt.Sprintf("%d.%s", time.Now().Unix(), executor.GetOutputFileExtension())
	output, err := filepath.Abs(filepath.Join(tmpDir, outputFile))
	if err != nil {
		return err
	}

	err = executor.Execute(project.Dir, output)
	if err != nil {
		return err
	}

	err = uploadToSwift(project, backup, output, executor.GetOutputFileExtension(), options)
	if err != nil {
		return err
	}

	return err
}

func uploadToSwift(project domain.Project, backup domain.Backup, file string, fileExt string, options options.Options) error {
	// Create a connection
	c, err := config.GetSwiftConnection(options.Swift)
	if err != nil {
		return err
	}

	// Check if the container for backups is created. If not, create it
	containers, err := c.ContainerNames(nil)
	if err != nil {
		return err
	}
	found := false
	for _, container := range containers {
		if container == containerName {
			found = true
			break
		}
	}
	if !found {
		err = c.ContainerCreate(containerName, nil)
		if err != nil {
			return err
		}
	}

	filename := fmt.Sprintf("%s/%s.%s", project.Name, time.Now().Format(time.RFC3339), fileExt)

	reader, _ := os.Open(file)
	defer reader.Close()
	headers := swift.Headers{
		"X-Delete-After": strconv.Itoa(backup.TimeToLive * 86400),
	}
	_, err = c.ObjectPut(containerName, filename, reader, true, "", "", headers)
	if err != nil {
		return err
	}

	return nil
}
<commit_msg>Remove the backup output after upload<commit_after>package execution

import (
	"fmt"
	"os"
	"path/filepath"
	"strconv"
	"time"
	"webup/backoops/config"
	"webup/backoops/domain"
	"webup/backoops/options"

	"github.com/ncw/swift"
)

const (
	containerName = "backups"
)

type Executor interface {
	GetOutputFileExtension() string
	Execute(workingDir string, output string) error
}

func ExecuteBackup(project domain.Project, backup domain.Backup, options options.Options) error {

	tmpDir := "._tmp"

	if _, err := os.Stat(tmpDir); os.IsNotExist(err) {
		os.MkdirAll(tmpDir, os.ModePerm)
	}

	executor := Pliz{}

	outputFile := fmt.Sprintf("%d.%s", time.Now().Unix(), executor.GetOutputFileExtension())
	output, err := filepath.Abs(filepath.Join(tmpDir, outputFile))
	if err != nil {
		return err
	}

	err = executor.Execute(project.Dir, output)
	if err != nil {
		return err
	}

	err = uploadToSwift(project, backup, output, executor.GetOutputFileExtension(), options)
	if err != nil {
		return err
	}

	// delete the file
	os.Remove(output)

	return nil
}

func uploadToSwift(project domain.Project, backup domain.Backup, file string, fileExt string, options options.Options) error {
	// Create a connection
	c, err := config.GetSwiftConnection(options.Swift)
	if err != nil {
		return err
	}

	// Check if the container for backups is created. If not, create it
	containers, err := c.ContainerNames(nil)
	if err != nil {
		return err
	}
	found := false
	for _, container := range containers {
		if container == containerName {
			found = true
			break
		}
	}
	if !found {
		err = c.ContainerCreate(containerName, nil)
		if err != nil {
			return err
		}
	}

	filename := fmt.Sprintf("%s/%s.%s", project.Name, time.Now().Format(time.RFC3339), fileExt)

	reader, _ := os.Open(file)
	defer reader.Close()
	headers := swift.Headers{
		"X-Delete-After": strconv.Itoa(backup.TimeToLive * 86400),
	}
	_, err = c.ObjectPut(containerName, filename, reader, true, "", "", headers)
	if err != nil {
		return err
	}

	return nil
}
<|endoftext|>


<commit_before>package deprecate

import (
	"encoding/json"
	"log"
	"net/http"

	"github.com/parkr/auto-reply/Godeps/_workspace/src/github.com/google/go-github/github"
)

const (
	closedState = `closed`
)

type RepoDeprecation struct {
	// Name with organization, e.g. "jekyll/jekyll-help"
	Nwo string

	// Comment to send when closing the issue.
	Message string
}

type DeprecateHandler struct {
	client   *github.Client
	repos    []RepoDeprecation
	messages map[string]string
}

func deprecationsToMap(deprecations []RepoDeprecation) map[string]string {
	deps := map[string]string{}
	for _, dep := range deprecations {
		deps[dep.Nwo] = dep.Message
	}
	return deps
}

// NewHandler returns an HTTP handler which deprecates repositories
// by closing new issues with a comment directing attention elsewhere.
func NewHandler(client *github.Client, deprecations []RepoDeprecation) *DeprecateHandler {
	return &DeprecateHandler{
		client:   client,
		repos:    deprecations,
		messages: deprecationsToMap(deprecations),
	}
}

func (dh *DeprecateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	var issue github.IssueActivityEvent
	err := json.NewDecoder(r.Body).Decode(&issue)
	if err != nil {
		log.Println("error unmarshalling issue stuffs:", err)
		http.Error(w, "bad json", 400)
		return
	}

	if *issue.Action != "opened" {
		http.Error(w, "ignored", 200)
		return
	}

	if msg, ok := dh.messages[*issue.Repo.FullName]; ok {
		err = dh.leaveComment(issue, msg)
		if err != nil {
			log.Println("error leaving comment:", err)
			http.Error(w, "couldnt leave comment", 500)
			return
		}
		err = dh.closeIssue(issue)
		if err != nil {
			log.Println("error closing comment:", err)
			http.Error(w, "couldnt close comment", 500)
			return
		}
	} else {
		log.Printf("looks like '%s' repo isn't deprecated", *issue.Repo.FullName)
		http.Error(w, "non-deprecated repo", 404)
		return
	}

	w.Write([]byte(`sorry ur deprecated`))
}

func (dh *DeprecateHandler) leaveComment(issue github.IssueActivityEvent, msg string) error {
	_, _, err := dh.client.Issues.CreateComment(
		*issue.Repo.Owner.Login,
		*issue.Repo.Name,
		*issue.Issue.Number,
		&github.IssueComment{Body: github.String(msg)},
	)
	return err
}

func (dh *DeprecateHandler) closeIssue(issue github.IssueActivityEvent) error {
	_, _, err := dh.client.Issues.Edit(
		*issue.Repo.Owner.Login,
		*issue.Repo.Name,
		*issue.Issue.Number,
		&github.IssueRequest{State: github.String(closedState)},
	)
	return err
}
<commit_msg>Return 200 if not 'issues' event or if not deprecated repo<commit_after>package deprecate

import (
	"encoding/json"
	"log"
	"net/http"

	"github.com/parkr/auto-reply/Godeps/_workspace/src/github.com/google/go-github/github"
)

const (
	closedState = `closed`
)

type RepoDeprecation struct {
	// Name with organization, e.g. "jekyll/jekyll-help"
	Nwo string

	// Comment to send when closing the issue.
	Message string
}

type DeprecateHandler struct {
	client   *github.Client
	repos    []RepoDeprecation
	messages map[string]string
}

func deprecationsToMap(deprecations []RepoDeprecation) map[string]string {
	deps := map[string]string{}
	for _, dep := range deprecations {
		deps[dep.Nwo] = dep.Message
	}
	return deps
}

// NewHandler returns an HTTP handler which deprecates repositories
// by closing new issues with a comment directing attention elsewhere.
func NewHandler(client *github.Client, deprecations []RepoDeprecation) *DeprecateHandler {
	return &DeprecateHandler{
		client:   client,
		repos:    deprecations,
		messages: deprecationsToMap(deprecations),
	}
}

func (dh *DeprecateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	if r.Header.Get("X-GitHub-Event") != "issues" {
		log.Println("received non-issues event for deprecate. sending pong.")
		http.Error(w, "ignored this one.", 200)
		return
	}

	var issue github.IssueActivityEvent
	err := json.NewDecoder(r.Body).Decode(&issue)
	if err != nil {
		log.Println("error unmarshalling issue stuffs:", err)
		http.Error(w, "bad json", 400)
		return
	}

	if *issue.Action != "opened" {
		http.Error(w, "ignored", 200)
		return
	}

	if msg, ok := dh.messages[*issue.Repo.FullName]; ok {
		err = dh.leaveComment(issue, msg)
		if err != nil {
			log.Println("error leaving comment:", err)
			http.Error(w, "couldnt leave comment", 500)
			return
		}
		err = dh.closeIssue(issue)
		if err != nil {
			log.Println("error closing comment:", err)
			http.Error(w, "couldnt close comment", 500)
			return
		}
	} else {
		log.Printf("looks like '%s' repo isn't deprecated", *issue.Repo.FullName)
		http.Error(w, "non-deprecated repo", 200)
		return
	}

	w.Write([]byte(`sorry ur deprecated`))
}

func (dh *DeprecateHandler) leaveComment(issue github.IssueActivityEvent, msg string) error {
	_, _, err := dh.client.Issues.CreateComment(
		*issue.Repo.Owner.Login,
		*issue.Repo.Name,
		*issue.Issue.Number,
		&github.IssueComment{Body: github.String(msg)},
	)
	return err
}

func (dh *DeprecateHandler) closeIssue(issue github.IssueActivityEvent) error {
	_, _, err := dh.client.Issues.Edit(
		*issue.Repo.Owner.Login,
		*issue.Repo.Name,
		*issue.Issue.Number,
		&github.IssueRequest{State: github.String(closedState)},
	)
	return err
}
<|endoftext|>


<commit_before>package main

import (
	"bufio"
	"fmt"
	"log"
	"os"
	"strconv"
	"strings"

	"github.com/docker/engine-api/client"
	"github.com/docker/engine-api/types"
	"github.com/optiopay/kafka"
	"github.com/optiopay/kafka/proto"
	"golang.org/x/net/context"
)

const (
	topic     = "test-topic"
	partition = 0
)

// printConsumed read messages from kafka and print them out
func printConsumed(broker kafka.Client) {
	conf := kafka.NewConsumerConf(topic, partition)
	conf.StartOffset = kafka.StartOffsetOldest
	consumer, err := broker.Consumer(conf)
	if err != nil {
		log.Fatalf("cannot create kafka consumer for %s:%d: %s", topic, partition, err)
	}

	for {
		msg, err := consumer.Consume()
		if err != nil {
			if err != kafka.ErrNoData {
				log.Printf("cannot consume %q topic message: %s", topic, err)
			}
			break
		}
		log.Printf("message %d: %s", msg.Offset, msg.Value)
	}
	log.Print("consumer quit")
}

// produceStdin read stdin and send every non empty line as message
func produceStdin(broker kafka.Client) {
	producer := broker.Producer(kafka.NewProducerConf())
	input := bufio.NewReader(os.Stdin)
	for {
		line, err := input.ReadString('\n')
		if err != nil {
			log.Fatalf("input error: %s", err)
		}
		line = strings.TrimSpace(line)
		if line == "" {
			continue
		}

		msg := &proto.Message{Value: []byte(line)}
		if _, err := producer.Produce(topic, partition, msg); err != nil {
			log.Fatalf("cannot produce message to %s:%d: %s", topic, partition, err)
		}
	}
}

func main() {
	var hostIP string

	if len(os.Args) > 0 {
		hostIP = os.Args[1]
	} else {
		log.Fatalln("Please supply Host IP.")
	}

	cli, err := client.NewEnvClient()

	options := types.ContainerListOptions{All: true}
	containers, err := cli.ContainerList(context.Background(), options)
	if err != nil {
		log.Fatal(err)
	}
	kafkaAddrs := []string{}

	for _, container := range containers {
		if strings.Contains(container.Names[0], "kafka") {
			ports := container.Ports
			kafkaAddrs = append(kafkaAddrs, hostIP+":"+strconv.Itoa(ports[0].PublicPort))
		}
	}

	fmt.Println("Kafka Hosts: ", kafkaAddrs)

	// connect to kafka cluster
	broker, err := kafka.Dial(kafkaAddrs, kafka.NewBrokerConf("go-client"))
	if err != nil {
		log.Fatalf("cannot connect to kafka cluster: %s", err)
	}
	defer broker.Close()

	go printConsumed(broker)
	produceStdin(broker)
}
<commit_msg>update kafka.go<commit_after>package main

import (
	"bufio"
	"fmt"
	"log"
	"os"
	"strconv"
	"strings"

	"github.com/docker/engine-api/client"
	"github.com/docker/engine-api/types"
	"github.com/optiopay/kafka"
	"github.com/optiopay/kafka/proto"
	"golang.org/x/net/context"
)

const (
	topic     = "test-topic"
	partition = 0
)

// printConsumed read messages from kafka and print them out
func printConsumed(broker kafka.Client) {
	conf := kafka.NewConsumerConf(topic, partition)
	conf.StartOffset = kafka.StartOffsetOldest
	consumer, err := broker.Consumer(conf)
	if err != nil {
		log.Fatalf("cannot create kafka consumer for %s:%d: %s", topic, partition, err)
	}

	for {
		msg, err := consumer.Consume()
		if err != nil {
			if err != kafka.ErrNoData {
				log.Printf("cannot consume %q topic message: %s", topic, err)
			}
			break
		}
		log.Printf("message %d: %s", msg.Offset, msg.Value)
	}
	log.Print("consumer quit")
}

// produceStdin read stdin and send every non empty line as message
func produceStdin(broker kafka.Client) {
	producer := broker.Producer(kafka.NewProducerConf())
	input := bufio.NewReader(os.Stdin)
	for {
		line, err := input.ReadString('\n')
		if err != nil {
			log.Fatalf("input error: %s", err)
		}
		line = strings.TrimSpace(line)
		if line == "" {
			continue
		}

		msg := &proto.Message{Value: []byte(line)}
		if _, err := producer.Produce(topic, partition, msg); err != nil {
			log.Fatalf("cannot produce message to %s:%d: %s", topic, partition, err)
		}
	}
}

func main() {
	var hostIP string

	if len(os.Args) > 1 {
		hostIP = os.Args[1]
	} else {
		log.Fatalln("[ERROR] Please supply Host IP.")
	}

	cli, err := client.NewEnvClient()

	options := types.ContainerListOptions{All: true}
	containers, err := cli.ContainerList(context.Background(), options)
	if err != nil {
		log.Fatal(err)
	}

	kafkaAddrs := []string{}
	var kafkaPort string

	for _, container := range containers {
		ports := container.Ports
		for _, port := range ports {
			if port.PrivatePort == 9092 {
				kafkaPort = strconv.Itoa(port.PublicPort)
			}
		}
		fmt.Println("Container: ", container.Names[0])
		fmt.Println("Ports: ", ports)
		kafkaAddrs = append(kafkaAddrs, hostIP+":"+kafkaPort)
	}

	fmt.Println("Kafka Hosts: ", kafkaAddrs)

	// connect to kafka cluster
	broker, err := kafka.Dial(kafkaAddrs, kafka.NewBrokerConf("go-client"))
	if err != nil {
		log.Fatalf("cannot connect to kafka cluster: %s", err)
	}
	defer broker.Close()

	fmt.Print("Subscribed to topic: ", topic)
	fmt.Printf("\n\nType something and hit [enter]...\n\n")

	go printConsumed(broker)
	produceStdin(broker)
}
<|endoftext|>