<commit_before>package cas

import "bytes"
import "math/big"

func (this *Expression) ToStringList() string {
	var buffer bytes.Buffer
	buffer.WriteString("{")
	for i, e := range this.Parts[1:] {
		buffer.WriteString(e.String())
		if i != len(this.Parts[1:])-1 {
			buffer.WriteString(", ")
		}
	}
	buffer.WriteString("}")
	return buffer.String()
}

func (this *Expression) EvalLength(es *EvalState) Ex {
	if len(this.Parts) != 2 {
		return this
	}

	list, isList := HeadAssertion(this.Parts[1], "List")
	if isList {
		return &Integer{big.NewInt(int64(len(list.Parts) - 1))}
	}
	return this
}

func (this *Expression) EvalTable(es *EvalState) Ex {
	if len(this.Parts) != 3 {
		return this
	}

	list, isList := HeadAssertion(this.Parts[2], "List")
	if isList {
		if len(list.Parts) == 4 {
			i, iOk := list.Parts[1].(*Symbol)
			iMin, iMinOk := list.Parts[2].(*Integer)
			iMax, iMaxOk := list.Parts[3].(*Integer)
			if iOk && iMinOk && iMaxOk {
				origDef, isOrigDef := es.GetDef(i.Name, i)
				toReturn := &Expression{[]Ex{&Symbol{"List"}}}
				iMaxInt := iMax.Val.Int64()
				for curr := iMin.Val.Int64(); curr <= iMaxInt; curr++ {
					es.Define(i.Name, i, &Integer{big.NewInt(curr)})
					toReturn.Parts = append(toReturn.Parts, this.Parts[1].DeepCopy().Eval(es))
				}
				if isOrigDef {
					es.Define(i.Name, i, origDef)
				} else {
					es.Clear(i.Name)
				}
				return toReturn
			}
		}
	}
	return this
}

type IterSpec struct {
	i    *Symbol
	iMin *Integer
	iMax *Integer
}

func IterSpecFromList(listEx Ex) (is IterSpec, isOk bool) {
	list, isList := HeadAssertion(listEx, "List")
	if isList {
		iOk, iMinOk, iMaxOk := false, false, false
		if len(list.Parts) == 3 {
			is.i, iOk = list.Parts[1].(*Symbol)
			is.iMin, iMinOk = &Integer{big.NewInt(1)}, true
			is.iMax, iMaxOk = list.Parts[2].(*Integer)
		} else if len(list.Parts) == 4 {
			is.i, iOk = list.Parts[1].(*Symbol)
			is.iMin, iMinOk = list.Parts[2].(*Integer)
			is.iMax, iMaxOk = list.Parts[3].(*Integer)
		}
		if iOk && iMinOk && iMaxOk {
			return is, true
		}
	}
	return is, false
}

func (this *Expression) EvalIterationFunc(es *EvalState, init Ex, op string) Ex {
	if len(this.Parts) != 3 {
		return this
	}

	// Retrieve variables of iteration
	is, isOk := IterSpecFromList(this.Parts[2])
	if isOk {
		origDef, isOrigDef := es.GetDef(is.i.Name, is.i)
		var toReturn Ex = init
		iMaxInt := is.iMax.Val.Int64()
		for curr := is.iMin.Val.Int64(); curr <= iMaxInt; curr++ {
			es.Define(is.i.Name, is.i, &Integer{big.NewInt(curr)})
			toReturn = (&Expression{[]Ex{&Symbol{op}, toReturn, this.Parts[1].DeepCopy().Eval(es)}}).Eval(es)
		}
		if isOrigDef {
			es.Define(is.i.Name, is.i, origDef)
		} else {
			es.Clear(is.i.Name)
		}
		return toReturn
	}
	return this
}

func (this *Expression) EvalSum(es *EvalState) Ex {
	return this.EvalIterationFunc(es, &Integer{big.NewInt(0)}, "Plus")
}

func (this *Expression) EvalProduct(es *EvalState) Ex {
	return this.EvalIterationFunc(es, &Integer{big.NewInt(1)}, "Times")
}

func MemberQ(components []Ex, item Ex, cl *CASLogger) bool {
	for _, part := range components {
		if matchq, _ := IsMatchQ(part, item, EmptyPD(), cl); matchq {
			return true
		}
	}
	return false
}

func (this *Expression) EvalMemberQ(es *EvalState) Ex {
	if len(this.Parts) != 3 {
		return this
	}
	list, isList := HeadAssertion(this.Parts[1], "List")
	if isList {
		if MemberQ(list.Parts, this.Parts[2], &es.CASLogger) {
			return &Symbol{"True"}
		}
	}
	return &Symbol{"False"}
}

func (this *Expression) EvalMap(es *EvalState) Ex {
	if len(this.Parts) != 3 {
		return this
	}

	list, isList := HeadAssertion(this.Parts[2], "List")
	if isList {
		toReturn := &Expression{[]Ex{&Symbol{"List"}}}
		for i := 1; i < len(list.Parts); i++ {
			toReturn.Parts = append(toReturn.Parts, &Expression{[]Ex{
				this.Parts[1].DeepCopy(),
				list.Parts[i].DeepCopy(),
			}})
		}
		return toReturn
	}
	return this.Parts[2]
}

func (this *Expression) EvalArray(es *EvalState) Ex {
	if len(this.Parts) != 3 {
		return this
	}

	nInt, nOk := this.Parts[2].(*Integer)
	if nOk {
		n := nInt.Val.Int64()
		toReturn := &Expression{[]Ex{&Symbol{"List"}}}
		for i := int64(1); i <= n; i++ {
			toReturn.Parts = append(toReturn.Parts, &Expression{[]Ex{
				this.Parts[1].DeepCopy(),
				&Integer{big.NewInt(i)},
			}})
		}
		return toReturn
	}
	return this.Parts[2]
}

func (this *Expression) EvalCases(es *EvalState) Ex {
	if len(this.Parts) != 3 {
		return this
	}

	list, isList := HeadAssertion(this.Parts[1], "List")
	if isList {
		toReturn := &Expression{[]Ex{&Symbol{"List"}}}

		for i := 1; i < len(list.Parts); i++ {
			if matchq, _ := IsMatchQ(list.Parts[i], this.Parts[2], EmptyPD(), &es.CASLogger); matchq {
				toReturn.Parts = append(toReturn.Parts, list.Parts[i])
			}
		}

		return toReturn
	}
	return this
}
<commit_msg>Support iteration in IterSpecs.<commit_after>package cas

import "bytes"
import "math/big"

func (this *Expression) ToStringList() string {
	var buffer bytes.Buffer
	buffer.WriteString("{")
	for i, e := range this.Parts[1:] {
		buffer.WriteString(e.String())
		if i != len(this.Parts[1:])-1 {
			buffer.WriteString(", ")
		}
	}
	buffer.WriteString("}")
	return buffer.String()
}

func (this *Expression) EvalLength(es *EvalState) Ex {
	if len(this.Parts) != 2 {
		return this
	}

	list, isList := HeadAssertion(this.Parts[1], "List")
	if isList {
		return &Integer{big.NewInt(int64(len(list.Parts) - 1))}
	}
	return this
}

func (this *Expression) EvalTable(es *EvalState) Ex {
	if len(this.Parts) != 3 {
		return this
	}

	list, isList := HeadAssertion(this.Parts[2], "List")
	if isList {
		if len(list.Parts) == 4 {
			i, iOk := list.Parts[1].(*Symbol)
			iMin, iMinOk := list.Parts[2].(*Integer)
			iMax, iMaxOk := list.Parts[3].(*Integer)
			if iOk && iMinOk && iMaxOk {
				origDef, isOrigDef := es.GetDef(i.Name, i)
				toReturn := &Expression{[]Ex{&Symbol{"List"}}}
				iMaxInt := iMax.Val.Int64()
				for curr := iMin.Val.Int64(); curr <= iMaxInt; curr++ {
					es.Define(i.Name, i, &Integer{big.NewInt(curr)})
					toReturn.Parts = append(toReturn.Parts, this.Parts[1].DeepCopy().Eval(es))
				}
				if isOrigDef {
					es.Define(i.Name, i, origDef)
				} else {
					es.Clear(i.Name)
				}
				return toReturn
			}
		}
	}
	return this
}

type IterSpec struct {
	i       *Symbol
	iMin    *Integer
	iMax    *Integer
	curr    int64
	iMaxInt int64
	cont    bool
}

func IterSpecFromList(listEx Ex) (is IterSpec, isOk bool) {
	list, isList := HeadAssertion(listEx, "List")
	if isList {
		iOk, iMinOk, iMaxOk := false, false, false
		if len(list.Parts) == 3 {
			is.i, iOk = list.Parts[1].(*Symbol)
			is.iMin, iMinOk = &Integer{big.NewInt(1)}, true
			is.iMax, iMaxOk = list.Parts[2].(*Integer)
		} else if len(list.Parts) == 4 {
			is.i, iOk = list.Parts[1].(*Symbol)
			is.iMin, iMinOk = list.Parts[2].(*Integer)
			is.iMax, iMaxOk = list.Parts[3].(*Integer)
		}
		if iOk && iMinOk && iMaxOk {
			is.Reset()
			return is, true
		}
	}
	return is, false
}

func (this *IterSpec) Reset() {
	this.curr = this.iMin.Val.Int64()
	this.iMaxInt = this.iMax.Val.Int64()
	this.cont = true
}

func (this *IterSpec) Next() {
	this.curr++
	this.cont = this.curr <= this.iMaxInt
}

func (this *Expression) EvalIterationFunc(es *EvalState, init Ex, op string) Ex {
	if len(this.Parts) == 3 {
		// Retrieve variables of iteration
		is, isOk := IterSpecFromList(this.Parts[2])
		if isOk {
			// Simulate evaluation within Block[]
			origDef, isOrigDef := es.GetDef(is.i.Name, is.i)
			var toReturn Ex = init
			for is.cont {
				es.Define(is.i.Name, is.i, &Integer{big.NewInt(is.curr)})
				toReturn = (&Expression{[]Ex{&Symbol{op}, toReturn, this.Parts[1].DeepCopy().Eval(es)}}).Eval(es)
				is.Next()
			}
			if isOrigDef {
				es.Define(is.i.Name, is.i, origDef)
			} else {
				es.Clear(is.i.Name)
			}
			return toReturn
		}
	}
	return this
}

func (this *Expression) EvalSum(es *EvalState) Ex {
	return this.EvalIterationFunc(es, &Integer{big.NewInt(0)}, "Plus")
}

func (this *Expression) EvalProduct(es *EvalState) Ex {
	return this.EvalIterationFunc(es, &Integer{big.NewInt(1)}, "Times")
}

func MemberQ(components []Ex, item Ex, cl *CASLogger) bool {
	for _, part := range components {
		if matchq, _ := IsMatchQ(part, item, EmptyPD(), cl); matchq {
			return true
		}
	}
	return false
}

func (this *Expression) EvalMemberQ(es *EvalState) Ex {
	if len(this.Parts) != 3 {
		return this
	}
	list, isList := HeadAssertion(this.Parts[1], "List")
	if isList {
		if MemberQ(list.Parts, this.Parts[2], &es.CASLogger) {
			return &Symbol{"True"}
		}
	}
	return &Symbol{"False"}
}

func (this *Expression) EvalMap(es *EvalState) Ex {
	if len(this.Parts) != 3 {
		return this
	}

	list, isList := HeadAssertion(this.Parts[2], "List")
	if isList {
		toReturn := &Expression{[]Ex{&Symbol{"List"}}}
		for i := 1; i < len(list.Parts); i++ {
			toReturn.Parts = append(toReturn.Parts, &Expression{[]Ex{
				this.Parts[1].DeepCopy(),
				list.Parts[i].DeepCopy(),
			}})
		}
		return toReturn
	}
	return this.Parts[2]
}

func (this *Expression) EvalArray(es *EvalState) Ex {
	if len(this.Parts) != 3 {
		return this
	}

	nInt, nOk := this.Parts[2].(*Integer)
	if nOk {
		n := nInt.Val.Int64()
		toReturn := &Expression{[]Ex{&Symbol{"List"}}}
		for i := int64(1); i <= n; i++ {
			toReturn.Parts = append(toReturn.Parts, &Expression{[]Ex{
				this.Parts[1].DeepCopy(),
				&Integer{big.NewInt(i)},
			}})
		}
		return toReturn
	}
	return this.Parts[2]
}

func (this *Expression) EvalCases(es *EvalState) Ex {
	if len(this.Parts) != 3 {
		return this
	}

	list, isList := HeadAssertion(this.Parts[1], "List")
	if isList {
		toReturn := &Expression{[]Ex{&Symbol{"List"}}}

		for i := 1; i < len(list.Parts); i++ {
			if matchq, _ := IsMatchQ(list.Parts[i], this.Parts[2], EmptyPD(), &es.CASLogger); matchq {
				toReturn.Parts = append(toReturn.Parts, list.Parts[i])
			}
		}

		return toReturn
	}
	return this
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"log"
	"os"
	"text/tabwriter"

	"github.com/mitchellh/cli"
)

func ListCommandFactory() (cli.Command, error) {
	return &ListCommand{}, nil
}

type ListCommand struct {
}

func (c *ListCommand) Run(_ []string) int {
	k, err := CreateKlientClient(NewKlientOptions())
	if err != nil {
		log.Fatal(err)
	}

	if err = k.Dial(); err != nil {
		log.Fatal(err)
	}

	res, err := k.Tell("remote.list")
	if err != nil {
		log.Fatal(err)
	}

	type kiteInfo struct {
		Ip       string
		VmName   string
		Hostname string
	}

	var infos []kiteInfo
	res.Unmarshal(&infos)

	w := tabwriter.NewWriter(os.Stdout, 2, 0, 2, ' ', 0)
	fmt.Fprintf(w, "\tNAME\tMACHINE IP\tHOSTNAME\n")
	for i, info := range infos {
		fmt.Fprintf(w, "  %d.\t%s\t%s\t%s\n",
			i+1, info.VmName, info.Ip, info.Hostname)
	}
	w.Flush()

	return 1
}

func (*ListCommand) Help() string {
	helpText := `
Usage: %s list

	List the available machines.
`
	return fmt.Sprintf(helpText, Name)
}

func (*ListCommand) Synopsis() string {
	return fmt.Sprintf("List the available machines")
}
<commit_msg>return mounted paths in `kd list`<commit_after>package main

import (
	"fmt"
	"log"
	"os"
	"strings"
	"text/tabwriter"

	"github.com/mitchellh/cli"
)

func ListCommandFactory() (cli.Command, error) {
	return &ListCommand{}, nil
}

type ListCommand struct {
}

func (c *ListCommand) Run(_ []string) int {
	k, err := CreateKlientClient(NewKlientOptions())
	if err != nil {
		log.Fatal(err)
	}

	if err = k.Dial(); err != nil {
		log.Fatal(err)
	}

	res, err := k.Tell("remote.list")
	if err != nil {
		log.Fatal(err)
	}

	type kiteInfo struct {
		Ip           string
		VmName       string
		Hostname     string
		MountedPaths []string
	}

	var infos []kiteInfo
	res.Unmarshal(&infos)

	w := tabwriter.NewWriter(os.Stdout, 2, 0, 2, ' ', 0)
	fmt.Fprintf(w, "\tNAME\tMACHINE IP\tHOSTNAME\tMOUNTED PATHS\n")
	for i, info := range infos {
		fmt.Fprintf(w, "  %d.\t%s\t%s\t%s\t%s\n",
			i+1, info.VmName, info.Ip, info.Hostname, strings.Join(info.MountedPaths, ", "))
	}
	w.Flush()

	return 1
}

func (*ListCommand) Help() string {
	helpText := `
Usage: %s list

	List the available machines.
`
	return fmt.Sprintf(helpText, Name)
}

func (*ListCommand) Synopsis() string {
	return fmt.Sprintf("List the available machines")
}
<|endoftext|>


<commit_before>/*
Copyright IBM Corp. 2016 All Rights Reserved.

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

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

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

package utils

import (
	"encoding/base64"
	"io"
	"net"
	"os"
	"path/filepath"
	"strconv"
	"sync"
	"time"
)

// DirMissingOrEmpty checks is a directory is missin or empty
func DirMissingOrEmpty(path string) (bool, error) {
	dirExists, err := DirExists(path)
	if err != nil {
		return false, err
	}
	if !dirExists {
		return true, nil
	}

	dirEmpty, err := DirEmpty(path)
	if err != nil {
		return false, err
	}
	if dirEmpty {
		return true, nil
	}
	return false, nil
}

// DirExists checks if a directory exists
func DirExists(path string) (bool, error) {
	_, err := os.Stat(path)
	if err == nil {
		return true, nil
	}
	if os.IsNotExist(err) {
		return false, nil
	}
	return false, err
}

// DirEmpty checks if a directory is empty
func DirEmpty(path string) (bool, error) {
	f, err := os.Open(path)
	if err != nil {
		return false, err
	}
	defer f.Close()

	_, err = f.Readdir(1)
	if err == io.EOF {
		return true, nil
	}
	return false, err
}

// FileMissing checks if a file is missing
func FileMissing(path string, name string) (bool, error) {
	_, err := os.Stat(filepath.Join(path, name))
	if err != nil {
		return true, err
	}
	return false, nil
}

// FilePathMissing returns true if the path is missing, false otherwise.
func FilePathMissing(path string) (bool, error) {
	_, err := os.Stat(path)
	if err != nil {
		return true, err
	}
	return false, nil
}

// DecodeBase64 decodes from Base64
func DecodeBase64(in string) ([]byte, error) {
	return base64.StdEncoding.DecodeString(in)
}

// EncodeBase64 encodes to Base64
func EncodeBase64(in []byte) string {
	return base64.StdEncoding.EncodeToString(in)
}

// IntArrayEquals checks if the arrays of ints are the same
func IntArrayEquals(a []int, b []int) bool {
	if len(a) != len(b) {
		return false
	}
	for i, v := range a {
		if v != b[i] {
			return false
		}
	}
	return true
}

// IsTCPPortOpen checks the tcp port is open
func IsTCPPortOpen(laddr string) error {
	lis, err := net.Listen("tcp", laddr)
	if err != nil {
		return err
	}
	lis.Close()
	return nil
}

var seed uint32
var randmu sync.Mutex

func reseed() uint32 {
	return uint32(time.Now().UnixNano() + int64(os.Getpid()))
}

func nextSuffix() string {
	randmu.Lock()
	r := seed
	if r == 0 {
		r = reseed()
	}
	r = r*1664525 + 1013904223 // constants from Numerical Recipes
	seed = r
	randmu.Unlock()
	return strconv.Itoa(int(1e9 + r%1e9))[1:]
}

// TempFile returns a temporary file path with the respect the system temp dir.
func TempFile(dir, prefix string) (name string, err error) {
	if dir == "" {
		dir = os.TempDir()
	}

	nconflict := 0
	for i := 0; i < 10000; i++ {
		name = filepath.Join(dir, prefix+nextSuffix())
		f, err := os.Stat(name)
		if f != nil || os.IsExist(err) {
			if nconflict++; nconflict > 10 {
				randmu.Lock()
				seed = reseed()
				randmu.Unlock()
			}
			continue
		}
		break
	}
	return
}
<commit_msg>Removing  unused functions<commit_after>/*
Copyright IBM Corp. 2016 All Rights Reserved.

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

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

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

package utils

import (
	"encoding/base64"
	"io"
	"os"
	"path/filepath"
)

// DirMissingOrEmpty checks is a directory is missin or empty
func DirMissingOrEmpty(path string) (bool, error) {
	dirExists, err := DirExists(path)
	if err != nil {
		return false, err
	}
	if !dirExists {
		return true, nil
	}

	dirEmpty, err := DirEmpty(path)
	if err != nil {
		return false, err
	}
	if dirEmpty {
		return true, nil
	}
	return false, nil
}

// DirExists checks if a directory exists
func DirExists(path string) (bool, error) {
	_, err := os.Stat(path)
	if err == nil {
		return true, nil
	}
	if os.IsNotExist(err) {
		return false, nil
	}
	return false, err
}

// DirEmpty checks if a directory is empty
func DirEmpty(path string) (bool, error) {
	f, err := os.Open(path)
	if err != nil {
		return false, err
	}
	defer f.Close()

	_, err = f.Readdir(1)
	if err == io.EOF {
		return true, nil
	}
	return false, err
}

// FileMissing checks if a file is missing
func FileMissing(path string, name string) (bool, error) {
	_, err := os.Stat(filepath.Join(path, name))
	if err != nil {
		return true, err
	}
	return false, nil
}

// FilePathMissing returns true if the path is missing, false otherwise.
func FilePathMissing(path string) (bool, error) {
	_, err := os.Stat(path)
	if err != nil {
		return true, err
	}
	return false, nil
}

// DecodeBase64 decodes from Base64
func DecodeBase64(in string) ([]byte, error) {
	return base64.StdEncoding.DecodeString(in)
}

// EncodeBase64 encodes to Base64
func EncodeBase64(in []byte) string {
	return base64.StdEncoding.EncodeToString(in)
}

// IntArrayEquals checks if the arrays of ints are the same
func IntArrayEquals(a []int, b []int) bool {
	if len(a) != len(b) {
		return false
	}
	for i, v := range a {
		if v != b[i] {
			return false
		}
	}
	return true
}
<|endoftext|>


<commit_before>package lich_test

import (
	"fmt"
	"github.com/earthboundkid/lich"
	"testing"
)

func ExampleData() {
	data := lich.Data("Hello, World!")

	fmt.Println(data)
	//Output: 13<Hello, World!>
}

func ExampleArray() {
	array := lich.Array{lich.Data("apple"), lich.Data("banana"), lich.Data("orange")}

	fmt.Println(array)
	//Output: 26[5<apple>6<banana>6<orange>]
}

func ExampleArrayFromStrings() {
	array := lich.ArrayFromStrings("simple", "general", "human-sympathetic")

	fmt.Println(array)
	//Output: 40[6<simple>7<general>17<human-sympathetic>]

}

func ExampleDict() {
	d1 := lich.Dict{
		"selling points": lich.ArrayFromStrings("simple", "general", "human-sympathetic"),
		"greeting":       lich.Data([]byte{72, 101, 108, 108, 111}),
		"fruit":          lich.Array{lich.Data("apple"), lich.Data("banana"), lich.Data("orange")},
	}

	fmt.Println(d1)
	//Output: 119{5<fruit>26[5<apple>6<banana>6<orange>]8<greeting>5<Hello>14<selling points>40[6<simple>7<general>17<human-sympathetic>]}
}

func ExampleDictFromMap() {
	d2, _ := lich.DictFromMap(map[string]interface{}{
		"greeting": map[string]interface{}{
			"English":  []byte{72, 101, 108, 108, 111},
			"Japanese": "Konnichiwa",
		},
		"fruit":          []string{"apple", "banana", "orange"},
		"selling points": lich.ArrayFromStrings("simple", "general", "human-sympathetic"),
	})
	fmt.Println(d2)
	//Output: 158{5<fruit>26[5<apple>6<banana>6<orange>]8<greeting>43{7<English>5<Hello>8<Japanese>10<Konnichiwa>}14<selling points>40[6<simple>7<general>17<human-sympathetic>]}

}

func TestInvalidMap(t *testing.T) {
	d2, err := lich.DictFromMap(map[string]interface{}{
		"greeting": map[string]interface{}{
			"English":  []byte{72, 101, 108, 108, 111},
			"Japanese": "Konnichiwa",
			"Dog":      9.0,
		},
		"fruit":          []string{"apple", "banana", "orange"},
		"selling points": lich.ArrayFromStrings("simple", "general", "human-sympathetic"),
	})
	if (err != lich.UnknownTypeError{"Dog", 9.0}) {
		t.Fatal(d2, err)
	}
}

func TestSimpleDataParsing(t *testing.T) {
	s := "5<hello>"
	element, err := lich.Parse(s)
	if err != nil || element != lich.Data("hello") {
		t.Fatalf("Parsed %q\nGot element:\t%#v, %s\nError:\t%#v", s, element, element, err)
	}

}

func TestSimpleArrayParsing(t *testing.T) {
	s := "26[5<apple>6<banana>6<orange>]"
	array := lich.Array{lich.Data("apple"), lich.Data("banana"), lich.Data("orange")}

	element, err := lich.Parse(s)
	if err != nil || element.String() != array.String() {
		t.Fatalf("Parsed %q\nGot element:\t%#v, %s\nError:\t%#v", s, element, element, err)
	}
}
<commit_msg>Tests for empty Elements.<commit_after>package lich_test

import (
	"fmt"
	"github.com/earthboundkid/lich"
	"testing"
)

func ExampleData() {
	data := lich.Data("Hello, World!")

	fmt.Println(data)
	//Output: 13<Hello, World!>
}

func ExampleArray() {
	array := lich.Array{lich.Data("apple"), lich.Data("banana"), lich.Data("orange")}

	fmt.Println(array)
	//Output: 26[5<apple>6<banana>6<orange>]
}

func ExampleArrayFromStrings() {
	array := lich.ArrayFromStrings("simple", "general", "human-sympathetic")

	fmt.Println(array)
	//Output: 40[6<simple>7<general>17<human-sympathetic>]

}

func ExampleDict() {
	d1 := lich.Dict{
		"selling points": lich.ArrayFromStrings("simple", "general", "human-sympathetic"),
		"greeting":       lich.Data([]byte{72, 101, 108, 108, 111}),
		"fruit":          lich.Array{lich.Data("apple"), lich.Data("banana"), lich.Data("orange")},
	}

	fmt.Println(d1)
	//Output: 119{5<fruit>26[5<apple>6<banana>6<orange>]8<greeting>5<Hello>14<selling points>40[6<simple>7<general>17<human-sympathetic>]}
}

func ExampleDictFromMap() {
	d2, _ := lich.DictFromMap(map[string]interface{}{
		"greeting": map[string]interface{}{
			"English":  []byte{72, 101, 108, 108, 111},
			"Japanese": "Konnichiwa",
		},
		"fruit":          []string{"apple", "banana", "orange"},
		"selling points": lich.ArrayFromStrings("simple", "general", "human-sympathetic"),
	})
	fmt.Println(d2)
	//Output: 158{5<fruit>26[5<apple>6<banana>6<orange>]8<greeting>43{7<English>5<Hello>8<Japanese>10<Konnichiwa>}14<selling points>40[6<simple>7<general>17<human-sympathetic>]}

}

func TestEmptyData(t *testing.T) {
	data := lich.Data("")
	str := data.String()
	if str != "0<>" {
		t.Fatal(data, str)
	}
}

func TestEmptyArray(t *testing.T) {
	array := lich.Array{}
	str := array.String()
	if str != "0[]" {
		t.Fatal(array, str)
	}
}

func TestEmptyDict(t *testing.T) {
	d := lich.Dict{}
	str := d.String()
	if str != "0{}" {
		t.Fatal(d, str)
	}
}

func TestInvalidMap(t *testing.T) {
	d2, err := lich.DictFromMap(map[string]interface{}{
		"greeting": map[string]interface{}{
			"English":  []byte{72, 101, 108, 108, 111},
			"Japanese": "Konnichiwa",
			"Dog":      9.0,
		},
		"fruit":          []string{"apple", "banana", "orange"},
		"selling points": lich.ArrayFromStrings("simple", "general", "human-sympathetic"),
	})
	if (err != lich.UnknownTypeError{"Dog", 9.0}) {
		t.Fatal(d2, err)
	}
}

func TestSimpleDataParsing(t *testing.T) {
	s := "5<hello>"
	element, err := lich.Parse(s)
	if err != nil || element != lich.Data("hello") {
		t.Fatalf("Parsed %q\nGot element:\t%#v, %s\nError:\t%#v", s, element, element, err)
	}

}

func TestSimpleArrayParsing(t *testing.T) {
	s := "26[5<apple>6<banana>6<orange>]"
	array := lich.Array{lich.Data("apple"), lich.Data("banana"), lich.Data("orange")}

	element, err := lich.Parse(s)
	if err != nil || element.String() != array.String() {
		t.Fatalf("Parsed %q\nGot element:\t%#v, %s\nError:\t%#v", s, element, element, err)
	}
}
<|endoftext|>


<commit_before>// Load a wikipedia dump into CouchBase
package main

import (
	"flag"
	"fmt"
	"log"
	"os"
	"runtime"
	"sync"
	"time"

	"github.com/couchbaselabs/go-couchbase"
	"github.com/dustin/go-humanize"
	"github.com/dustin/go-wikiparse"
)

var numWorkers = flag.Int("numWorkers", 8, "Number of page workers")

var wg sync.WaitGroup

func init() {
	flag.Usage = usage
}

func usage() {
	fmt.Fprintf(os.Stderr,
		"Usage:\n  %s [opts] wikipedia.index.bz2 wikipedia.xml.bz2\n",
		os.Args[0])
	fmt.Fprintf(os.Stderr, "\nOptions:\n")
	flag.PrintDefaults()
	os.Exit(1)
}

type Geo struct {
	Geometry struct {
		Type        string    `json:"type"`
		Coordinates []float64 `json:"coordinates"`
	} `json:"geometry"`
	Type string `json:"type"`
}

type Article struct {
	RevInfo struct {
		ID            uint64 `json:"id"`
		Timestamp     string `json:"timestamp"`
		Contributor   string `json:"contributor"`
		ContributorId uint64 `json:"contributorid"`
		Comment       string `json:"comment"`
	} `json:"revinfo"`
	Text  string   `json:"text"`
	Geo   *Geo     `json:"geo,omitempty"`
	Files []string `json:"files,omitempty"`
}

func doPage(db *couchbase.Bucket, p *wikiparse.Page) {
	article := Article{Text: p.Revisions[0].Text}
	gl, err := wikiparse.ParseCoords(p.Revisions[0].Text)
	if err == nil {
		article.Geo = &Geo{Type: "Feature"}
		article.Geo.Geometry.Type = "Point"
		article.Geo.Geometry.Coordinates = []float64{gl.Lon, gl.Lat}
	}
	article.RevInfo.ID = p.Revisions[0].ID
	article.RevInfo.Timestamp = p.Revisions[0].Timestamp
	article.RevInfo.Contributor = p.Revisions[0].Contributor.Username
	article.RevInfo.ContributorId = p.Revisions[0].Contributor.ID
	article.RevInfo.Comment = p.Revisions[0].Comment
	article.Files = wikiparse.FindFiles(article.Text)

	err = db.Set(p.Title, 0, article)
	if err != nil {
		log.Printf("Error setting %v: %v", p.Title, err)
		return
	}
}

func pageHandler(db *couchbase.Bucket, ch <-chan *wikiparse.Page) {
	defer wg.Done()
	for p := range ch {
		doPage(db, p)
	}
}

func main() {
	couchbaseServer := flag.String("couchbase", "http://localhost:8091/",
		"Couchbase URL")
	couchbaseBucket := flag.String("bucket", "default", "Couchbase bucket")
	procs := flag.Int("cpus", runtime.NumCPU(), "Number of CPUS to use")
	flag.Parse()

	runtime.GOMAXPROCS(*procs)

	db, err := couchbase.GetBucket(*couchbaseServer,
		"default", *couchbaseBucket)
	if err != nil {
		log.Fatalf("Error connecting to couchbase: %v", err)
	}

	p, err := wikiparse.NewIndexedParser(flag.Arg(1), flag.Arg(2),
		runtime.GOMAXPROCS(0))
	if err != nil {
		log.Fatalf("Error initializing multistream parser: %v", err)
	}

	ch := make(chan *wikiparse.Page, 1000)

	for i := 0; i < *numWorkers; i++ {
		wg.Add(1)
		go pageHandler(db, ch)
	}

	pages := int64(0)
	start := time.Now()
	prev := start
	reportfreq := int64(1000)
	for err == nil {
		var page *wikiparse.Page
		page, err = p.Next()
		if err == nil {
			ch <- page
		}

		pages++
		if pages%reportfreq == 0 {
			now := time.Now()
			d := now.Sub(prev)
			log.Printf("Processed %s pages total (%.2f/s)",
				humanize.Comma(pages), float64(reportfreq)/d.Seconds())
			prev = now
		}
	}
	close(ch)
	wg.Wait()
	log.Printf("Ended with err after %v:  %v after %s pages",
		time.Now().Sub(start), err, humanize.Comma(pages))

}
<commit_msg>cbload arguments were offset by 1<commit_after>// Load a wikipedia dump into CouchBase
package main

import (
	"flag"
	"fmt"
	"log"
	"os"
	"runtime"
	"sync"
	"time"

	"github.com/couchbaselabs/go-couchbase"
	"github.com/dustin/go-humanize"
	"github.com/dustin/go-wikiparse"
)

var numWorkers = flag.Int("numWorkers", 8, "Number of page workers")

var wg sync.WaitGroup

func init() {
	flag.Usage = usage
}

func usage() {
	fmt.Fprintf(os.Stderr,
		"Usage:\n  %s [opts] wikipedia.index.bz2 wikipedia.xml.bz2\n",
		os.Args[0])
	fmt.Fprintf(os.Stderr, "\nOptions:\n")
	flag.PrintDefaults()
	os.Exit(1)
}

type Geo struct {
	Geometry struct {
		Type        string    `json:"type"`
		Coordinates []float64 `json:"coordinates"`
	} `json:"geometry"`
	Type string `json:"type"`
}

type Article struct {
	RevInfo struct {
		ID            uint64 `json:"id"`
		Timestamp     string `json:"timestamp"`
		Contributor   string `json:"contributor"`
		ContributorId uint64 `json:"contributorid"`
		Comment       string `json:"comment"`
	} `json:"revinfo"`
	Text  string   `json:"text"`
	Geo   *Geo     `json:"geo,omitempty"`
	Files []string `json:"files,omitempty"`
}

func doPage(db *couchbase.Bucket, p *wikiparse.Page) {
	article := Article{Text: p.Revisions[0].Text}
	gl, err := wikiparse.ParseCoords(p.Revisions[0].Text)
	if err == nil {
		article.Geo = &Geo{Type: "Feature"}
		article.Geo.Geometry.Type = "Point"
		article.Geo.Geometry.Coordinates = []float64{gl.Lon, gl.Lat}
	}
	article.RevInfo.ID = p.Revisions[0].ID
	article.RevInfo.Timestamp = p.Revisions[0].Timestamp
	article.RevInfo.Contributor = p.Revisions[0].Contributor.Username
	article.RevInfo.ContributorId = p.Revisions[0].Contributor.ID
	article.RevInfo.Comment = p.Revisions[0].Comment
	article.Files = wikiparse.FindFiles(article.Text)

	err = db.Set(p.Title, 0, article)
	if err != nil {
		log.Printf("Error setting %v: %v", p.Title, err)
		return
	}
}

func pageHandler(db *couchbase.Bucket, ch <-chan *wikiparse.Page) {
	defer wg.Done()
	for p := range ch {
		doPage(db, p)
	}
}

func main() {
	couchbaseServer := flag.String("couchbase", "http://localhost:8091/",
		"Couchbase URL")
	couchbaseBucket := flag.String("bucket", "default", "Couchbase bucket")
	procs := flag.Int("cpus", runtime.NumCPU(), "Number of CPUS to use")
	flag.Parse()

	runtime.GOMAXPROCS(*procs)

	db, err := couchbase.GetBucket(*couchbaseServer,
		"default", *couchbaseBucket)
	if err != nil {
		log.Fatalf("Error connecting to couchbase: %v", err)
	}

	p, err := wikiparse.NewIndexedParser(flag.Arg(0), flag.Arg(1),
		runtime.GOMAXPROCS(0))
	if err != nil {
		log.Fatalf("Error initializing multistream parser: %v", err)
	}

	ch := make(chan *wikiparse.Page, 1000)

	for i := 0; i < *numWorkers; i++ {
		wg.Add(1)
		go pageHandler(db, ch)
	}

	pages := int64(0)
	start := time.Now()
	prev := start
	reportfreq := int64(1000)
	for err == nil {
		var page *wikiparse.Page
		page, err = p.Next()
		if err == nil {
			ch <- page
		}

		pages++
		if pages%reportfreq == 0 {
			now := time.Now()
			d := now.Sub(prev)
			log.Printf("Processed %s pages total (%.2f/s)",
				humanize.Comma(pages), float64(reportfreq)/d.Seconds())
			prev = now
		}
	}
	close(ch)
	wg.Wait()
	log.Printf("Ended with err after %v:  %v after %s pages",
		time.Now().Sub(start), err, humanize.Comma(pages))

}
<|endoftext|>


<commit_before>package resource

import (
	"os"
	"os/exec"
	"strings"

	"github.com/hashicorp/hcl"
	"github.com/hashicorp/hcl/hcl/ast"
	"github.com/imdario/mergo"
)

// Name and description of the resource
const shellResourceType = "shell"
const shellResourceDesc = "executes shell commands"

// ShellResource type is a resource which executes shell commands
type ShellResource struct {
	BaseResource `hcl:",squash"`

	Command string `hcl:"command"`

	Creates string `hcl:"creates"`
}

// NewShellResource creates a new resource for executing shell commands
func NewShellResource(title string, obj *ast.ObjectItem, config *Config) (Resource, error) {
	// Resource defaults
	defaults := &ShellResource{
		BaseResource: BaseResource{
			Title:  title,
			Type:   shellResourceType,
			State:  StatePresent,
			Config: config,
		},
		Command: title,
	}

	var sr ShellResource
	err := hcl.DecodeObject(&sr, obj)
	if err != nil {
		return nil, err
	}

	// Merge the decoded object with the resource defaults
	err = mergo.Merge(&sr, defaults)

	return &sr, err
}

// Evaluate evaluates the state of the resource
func (sr *ShellResource) Evaluate() (State, error) {
	// Assumes that the command to be executed is idempotent
	//
	// Sets the current state to absent and wanted to be present,
	// which will cause the command to be executed.
	//
	// If the command to be executed is not idempotent on it's own,
	// in order to ensure idempotency we should specify a file,
	// that can be checked for existence.
	rs := State{
		Current: StateAbsent,
		Want:    sr.State,
		Update:  false,
	}

	if sr.Creates != "" {
		_, err := os.Stat(sr.Creates)
		if os.IsNotExist(err) {
			rs.Current = StateAbsent
		} else {
			rs.Current = StatePresent
		}
	}

	return rs, nil
}

// Create executes the shell command
func (sr *ShellResource) Create() error {
	sr.Printf("executing command\n")

	args := strings.Fields(sr.Command)
	cmd := exec.Command(args[0], args[1:]...)
	out, err := cmd.CombinedOutput()

	for _, line := range strings.Split(string(out), "\n") {
		sr.Printf("%s\n", line)
	}

	return err
}

// Delete is a no-op
func (sr *ShellResource) Delete() error {
	return nil
}

// Update is a no-op
func (sr *ShellResource) Update() error {
	return nil
}

func init() {
	item := RegistryItem{
		Name:        shellResourceType,
		Description: shellResourceDesc,
		Provider:    NewShellResource,
	}

	Register(item)
}
<commit_msg>resource: prepare Shell resource for Lua<commit_after>package resource

import (
	"os"
	"os/exec"
	"strings"
)

// Shell type is a resource which executes shell commands
type Shell struct {
	BaseResource

	// Command to be executed
	Command string `luar:"command"`

	// File to be checked for existence before running the command
	Creates string `luar:"creates"`
}

// NewShell creates a new resource for executing shell commands
func NewShell(title string) (Resource, error) {
	s := &Shell{
		BaseResource: BaseResource{
			Title: title,
			Type:  shellResourceType,
			State: StatePresent,
		},
		Command: title,
	}

	return &s, nil
}

// Evaluate evaluates the state of the resource
func (s *Shell) Evaluate() (State, error) {
	// Assumes that the command to be executed is idempotent
	//
	// Sets the current state to absent and wanted to be present,
	// which will cause the command to be executed.
	//
	// If the command to be executed is not idempotent on it's own,
	// in order to ensure idempotency we should specify a file,
	// that can be checked for existence.
	rs := State{
		Current: StateAbsent,
		Want:    s.State,
		Update:  false,
	}

	if s.Creates != "" {
		_, err := os.Stat(s.Creates)
		if os.IsNotExist(err) {
			rs.Current = StateAbsent
		} else {
			rs.Current = StatePresent
		}
	}

	return rs, nil
}

// Create executes the shell command
func (s *Shell) Create() error {
	s.Printf("executing command\n")

	args := strings.Fields(s.Command)
	cmd := exec.Command(args[0], args[1:]...)
	out, err := cmd.CombinedOutput()

	for _, line := range strings.Split(string(out), "\n") {
		s.Printf("%s\n", line)
	}

	return err
}

// Delete is a no-op
func (s *Shell) Delete() error {
	return nil
}

// Update is a no-op
func (s *Shell) Update() error {
	return nil
}

func init() {
	RegisterProvider("shell", NewShell)
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"strings"

	"github.com/hashicorp/terraform/helper/schema"
	"github.com/iron-io/iron_go3/config"
	"github.com/iron-io/iron_go3/mq"
)

func queueSchema() *schema.Resource {
	subscriber := &schema.Resource{
		Schema: map[string]*schema.Schema{
			"url": {
				Type:        schema.TypeString,
				Required:    true,
				ForceNew:    true,
				Description: "URL of a queue subscriber endpoint",
			},
		},
	}
	// This schema must be consistent to pushConfigHash.
	pushConfig := &schema.Resource{
		Schema: map[string]*schema.Schema{
			"retries_delay": {
				Type:     schema.TypeInt,
				Optional: true,
				Default:  60,
			},
			"retries": {
				Type:     schema.TypeInt,
				Optional: true,
				Default:  3,
			},
			"subscribers": {
				Type:     schema.TypeList,
				Required: true,
				Elem:     subscriber,
			},
			"error_queue": {
				Type:     schema.TypeString,
				Optional: true,
				Default:  "",
			},
		},
	}
	return &schema.Resource{
		Create: createQueue,
		Read:   readQueue,
		Update: updateQueue,
		Delete: deleteQueue,
		Exists: queueExists,

		Schema: map[string]*schema.Schema{
			"name": {
				Type:        schema.TypeString,
				Required:    true,
				ForceNew:    true,
				Description: "name of the queue",
			},
			"type": {
				Type:         schema.TypeString,
				Optional:     true,
				ForceNew:     true,
				Default:      "pull",
				Description:  "type of the queue",
				ValidateFunc: validateQueueType,
			},
			"push": {
				Type:     schema.TypeList,
				Optional: true,
				Elem:     pushConfig,
			},
		},
	}
}

func createQueue(data *schema.ResourceData, meta interface{}) error {
	cfg := meta.(config.Settings)
	info, err := mq.ConfigCreateQueue(queueInfoFromData(data), &cfg)
	if err != nil {
		return err
	}
	refreshState(data, cfg.ProjectId, info)
	return nil
}

func updateQueue(data *schema.ResourceData, meta interface{}) error {
	cfg := meta.(config.Settings)
	info := queueInfoFromData(data)
	q := mq.ConfigNew(info.Name, &cfg)
	info, err := q.Update(info)
	refreshState(data, cfg.ProjectId, info)
	return err
}

func readQueue(data *schema.ResourceData, meta interface{}) error {
	cfg := meta.(config.Settings)
	name := queueInfoFromData(data).Name
	q := mq.ConfigNew(name, &cfg)
	info, err := q.Info()
	if err != nil {
		return err
	}
	refreshState(data, cfg.ProjectId, info)
	return nil
}

func deleteQueue(data *schema.ResourceData, meta interface{}) error {
	cfg := meta.(config.Settings)
	q := mq.ConfigNew(queueInfoFromData(data).Name, &cfg)
	return q.Delete()
}

func queueExists(data *schema.ResourceData, meta interface{}) (bool, error) {
	cfg := meta.(config.Settings)
	q := mq.ConfigNew(queueInfoFromData(data).Name, &cfg)
	_, err := q.Info()
	if err != nil {
		// TODO: avoid this hacky detection once mq client library supports QueueNotExist err.
		if strings.Contains(err.Error(), "Queue not found") {
			return false, nil
		}
		return false, err
	}
	return true, nil
}

// queueInfoFromData constructs expected queueInfo based on a resource state given by Terraform.
//
// It assumes that "data" is valid against queueSchema.
func queueInfoFromData(data *schema.ResourceData) mq.QueueInfo {
	name := data.Get("name").(string)
	typ := data.Get("type").(string)
	var push *mq.PushInfo
	if typ != "pull" {
		push = new(mq.PushInfo)
		m := data.Get("push").([]interface{})[0].(map[string]interface{})

		push.RetriesDelay = m["retries_delay"].(int)
		push.Retries = m["retries"].(int)
		for _, item := range m["subscribers"].([]interface{}) {
			s := item.(map[string]interface{})
			push.Subscribers = append(push.Subscribers, mq.QueueSubscriber{
				URL: s["url"].(string),
			})
		}
		push.ErrorQueue = m["error_queue"].(string)
	}
	return mq.QueueInfo{
		Name: name,
		Type: &typ,
		Push: push,
	}
}

// refreshState reflects the current configuration of the queue to "data" based
// on "project" and "info".
func refreshState(data *schema.ResourceData, project string, info mq.QueueInfo) {
	data.SetId(fmt.Sprintf("%s/%s", project, info.Name))
	if info.Type == nil || *info.Type == "pull" {
		data.Set("type", "pull")
		data.Set("push", nil)
		return
	}
	data.Set("type", *info.Type)

	var subscribers []interface{}
	for _, s := range info.Push.Subscribers {
		subscribers = append(subscribers, map[string]interface{}{
			"url": s.URL,
		})
	}
	push := map[string]interface{}{
		"retries_delay": info.Push.RetriesDelay,
		"retries":       info.Push.Retries,
		"subscribers":   subscribers,
		"error_queue":   info.Push.ErrorQueue,
	}
	data.Set("push", []interface{}{push})
}

func validateQueueType(value interface{}, key string) ([]string, []error) {
	switch value.(string) {
	case "pull", "unicast", "multicast":
		break
	default:
		return nil, []error{
			fmt.Errorf("%q must be either pull, unicast or multicast", key),
		}
	}
	return nil, nil
}
<commit_msg>More validation to avoid runtime panic<commit_after>package main

import (
	"fmt"
	"strings"

	"github.com/hashicorp/terraform/helper/schema"
	"github.com/iron-io/iron_go3/config"
	"github.com/iron-io/iron_go3/mq"
)

func queueSchema() *schema.Resource {
	subscriber := &schema.Resource{
		Schema: map[string]*schema.Schema{
			"url": {
				Type:        schema.TypeString,
				Required:    true,
				ForceNew:    true,
				Description: "URL of a queue subscriber endpoint",
			},
		},
	}
	// This schema must be consistent to pushConfigHash.
	pushConfig := &schema.Resource{
		Schema: map[string]*schema.Schema{
			"retries_delay": {
				Type:     schema.TypeInt,
				Optional: true,
				Default:  60,
			},
			"retries": {
				Type:     schema.TypeInt,
				Optional: true,
				Default:  3,
			},
			"subscribers": {
				Type:     schema.TypeList,
				Required: true,
				Elem:     subscriber,
			},
			"error_queue": {
				Type:     schema.TypeString,
				Optional: true,
				Default:  "",
			},
		},
	}
	return &schema.Resource{
		Create: createQueue,
		Read:   readQueue,
		Update: updateQueue,
		Delete: deleteQueue,
		Exists: queueExists,

		Schema: map[string]*schema.Schema{
			"name": {
				Type:        schema.TypeString,
				Required:    true,
				ForceNew:    true,
				Description: "name of the queue",
			},
			"type": {
				Type:         schema.TypeString,
				Optional:     true,
				ForceNew:     true,
				Default:      "pull",
				Description:  "type of the queue",
				ValidateFunc: validateQueueType,
			},
			"push": {
				Type:     schema.TypeList,
				Optional: true,
				Elem:     pushConfig,
			},
		},
	}
}

func createQueue(data *schema.ResourceData, meta interface{}) error {
	cfg := meta.(config.Settings)
	info, err := queueInfoFromData(data)
	if err != nil {
		return err
	}
	info, err = mq.ConfigCreateQueue(info, &cfg)
	if err != nil {
		return err
	}
	refreshState(data, cfg.ProjectId, info)
	return nil
}

func updateQueue(data *schema.ResourceData, meta interface{}) error {
	cfg := meta.(config.Settings)
	info, err := queueInfoFromData(data)
	if err != nil {
		return err
	}
	q := mq.ConfigNew(info.Name, &cfg)
	info, err = q.Update(info)
	if err != nil {

		return err
	}
	refreshState(data, cfg.ProjectId, info)
	return nil
}

func readQueue(data *schema.ResourceData, meta interface{}) error {
	cfg := meta.(config.Settings)
	name := data.Get("name").(string)
	q := mq.ConfigNew(name, &cfg)
	info, err := q.Info()
	if err != nil {
		return err
	}
	refreshState(data, cfg.ProjectId, info)
	return nil
}

func deleteQueue(data *schema.ResourceData, meta interface{}) error {
	cfg := meta.(config.Settings)
	name := data.Get("name").(string)
	q := mq.ConfigNew(name, &cfg)
	return q.Delete()
}

func queueExists(data *schema.ResourceData, meta interface{}) (bool, error) {
	cfg := meta.(config.Settings)
	name := data.Get("name").(string)
	q := mq.ConfigNew(name, &cfg)
	_, err := q.Info()
	if err != nil {
		// TODO: avoid this hacky detection once mq client library supports QueueNotExist err.
		if strings.Contains(err.Error(), "Queue not found") {
			return false, nil
		}
		return false, err
	}
	return true, nil
}

// queueInfoFromData constructs expected queueInfo based on a resource state given by Terraform.
//
// It assumes that "data" is valid against queueSchema.
func queueInfoFromData(data *schema.ResourceData) (mq.QueueInfo, error) {
	name := data.Get("name").(string)
	typ := data.Get("type").(string)
	var push *mq.PushInfo
	if typ != "pull" {
		push = new(mq.PushInfo)
		ps := data.Get("push").([]interface{})
		// TODO: Do this validation earlier once schema.Schema supports ValidateFunc for compound types.
		switch len(ps) {
		case 0:
			return mq.QueueInfo{}, fmt.Errorf(`push queue must have argument "push"`)
		case 1:
			break
		default:
			return mq.QueueInfo{}, fmt.Errorf(`argument "push" cannot be a list`)
		}
		m := ps[0].(map[string]interface{})

		push.RetriesDelay = m["retries_delay"].(int)
		push.Retries = m["retries"].(int)
		for _, item := range m["subscribers"].([]interface{}) {
			s := item.(map[string]interface{})
			push.Subscribers = append(push.Subscribers, mq.QueueSubscriber{
				URL: s["url"].(string),
			})
		}
		push.ErrorQueue = m["error_queue"].(string)
	}
	return mq.QueueInfo{
		Name: name,
		Type: &typ,
		Push: push,
	}, nil
}

// refreshState reflects the current configuration of the queue to "data" based
// on "project" and "info".
func refreshState(data *schema.ResourceData, project string, info mq.QueueInfo) {
	data.SetId(fmt.Sprintf("%s/%s", project, info.Name))
	if info.Type == nil || *info.Type == "pull" {
		data.Set("type", "pull")
		data.Set("push", nil)
		return
	}
	data.Set("type", *info.Type)

	var subscribers []interface{}
	for _, s := range info.Push.Subscribers {
		subscribers = append(subscribers, map[string]interface{}{
			"url": s.URL,
		})
	}
	push := map[string]interface{}{
		"retries_delay": info.Push.RetriesDelay,
		"retries":       info.Push.Retries,
		"subscribers":   subscribers,
		"error_queue":   info.Push.ErrorQueue,
	}
	data.Set("push", []interface{}{push})
}

func validateQueueType(value interface{}, key string) ([]string, []error) {
	switch value.(string) {
	case "pull", "unicast", "multicast":
		break
	default:
		return nil, []error{
			fmt.Errorf("%q must be either pull, unicast or multicast", key),
		}
	}
	return nil, nil
}
<|endoftext|>


<commit_before>package list

// Iterator is an iterator for a list
type Iterator interface {
	Next() bool
	Set(v interface{})
	Value() interface{}
}

// List holds a list
type List interface {
	Clear()
	Len() int

	First() Iterator
	Last() Iterator
	Get(i int) (interface{}, error)
	GetFunc(m func(v interface{}) bool) (interface{}, bool)
	Set(i int, v interface{}) error
	SetFunc(m func(v interface{}) bool, v interface{}) bool

	Copy() List
	ToArray() []interface{}

	InsertAt(i int, v interface{}) error
	RemoveAt(i int) (interface{}, error)
	RemoveFirstOccurrence(v interface{}) bool
	RemoveLastOccurrence(v interface{}) bool
	Pop() (interface{}, bool)
	Push(v interface{})
	PushList(l2 List)
	Shift() (interface{}, bool)
	Unshift(v interface{})
	UnshiftList(l2 List)

	Contains(v interface{}) bool
	IndexOf(v interface{}) (int, bool)
	LastIndexOf(v interface{}) (int, bool)
}
<commit_msg>define the final interface of Iterator and List<commit_after>package list

// Iterator defines a list iterator
type Iterator interface {
	// Next iterates to the next element in the list and returns true or false if there is no next element
	Next() bool
	// Previous iterates to the previous element in the list and returns true or false if there is no previous element
	Previous() bool

	// Get returns the value of the current element
	Get() interface{}
	// Set sets the value of the current element
	Set(v interface{})
}

// List defines a list
type List interface {
	// Clear resets the list to zero elements
	Clear()
	// Len returns the current list length
	Len() int

	// Chan returns a channel which iterates from the front to the back of the list
	Chan(n int) <-chan interface{}
	// ChanBack returns a channel which iterates from the back to the front of the list
	ChanBack(n int) <-chan interface{}

	// Iter returns an iterator which iterates from the front to the back of the list
	Iter() Iterator
	// IterBack returns an iterator which iterates from the back to the front of the list
	IterBack() Iterator

	// First returns the first value of the list and true or false if there is no value
	First() (interface{}, bool)
	// Last returns the last value of the list and true or false if there is no value
	Last() (interface{}, bool)
	// Get returns the value of the given index and nil or an out of bound error
	Get(i int) (interface{}, error)
	// GetFunc returns the value of the first element selected by the given function and true or false if there is none
	GetFunc(m func(v interface{}) bool) (interface{}, bool)
	// Set sets the value of the given index and nil or an out of bound error
	Set(i int, v interface{}) error
	// SetFunc sets the value of the first element selected by the given function and true or false if there is none
	SetFunc(m func(v interface{}) bool, v interface{}) bool

	// Copy returns an exact copy of the list
	Copy() List
	// ToArray returns a copy of the list as a slice
	ToArray() []interface{}

	// InsertAt inserts a value into the list and returns nil or an out of bound error
	InsertAt(i int, v interface{}) error
	// RemoveAt removes and returns the value with the given index and nil or an out of bound error
	RemoveAt(i int) (interface{}, error)
	// RemoveFirstOccurrence removes the first occurrence of the given value in the list and returns true or false if there is none
	RemoveFirstOccurrence(v interface{}) bool
	// RemoveLastOccurrence removes the last occurrence of the given value in the list and returns true or false if there is none
	RemoveLastOccurrence(v interface{}) bool
	// Pop removes and returns the last element and true or false if there is none
	Pop() (interface{}, bool)
	// Push inserts the given value at the end of the list
	Push(v interface{})
	// PushList pushes the given list
	PushList(l2 List)
	// Shift removes and returns the first element and true or false if there is none
	Shift() (interface{}, bool)
	// Unshift inserts the given value at the beginning of the list
	Unshift(v interface{})
	// UnshiftList unshifts the given list
	UnshiftList(l2 List)

	// Contains returns true if the value exists in the list or false if it does not
	Contains(v interface{}) bool
	// IndexOf returns the first index of the given value and true or false if it does not exists
	IndexOf(v interface{}) (int, bool)
	// LastIndexOf returns the last index of the given value and true or false if it does not exists
	LastIndexOf(v interface{}) (int, bool)

	// MoveAfter moves the element at index i after the element at index m
	MoveAfter(i, m int) error
	// MoveToBack moves the element at index i to the back of the list
	MoveToBack(i int) error
	// MoveBefore moves the element at index i before the element at index m
	MoveBefore(i, m int) error
	// MoveToFron moves the element at index i to the front of the list
	MoveToFront(i int) error
}
<|endoftext|>


<commit_before>// Copyright © 2016 Aaron Longwell
//
// Use of this source code is governed by an MIT licese.
// Details in the LICENSE file.

package trello

import (
	"testing"
	"time"
)

func TestListCreatedAt(t *testing.T) {
	l := List{ID: "4d5ea62fd76aa1136000000c"}
	ts := l.CreatedAt()
	if ts.IsZero() {
		t.Error("Time shouldn't be zero.")
	}
	if ts.Unix() != 1298048559 {
		t.Errorf("Incorrect CreatedAt() time: '%s'.", ts.Format(time.RFC3339))
	}
}

func TestGetList(t *testing.T) {
	list := testList(t)
	if list.Name != "To Do Soon" {
		t.Errorf("Title incorrect. Got '%s'", list.Name)
	}
}

func TestGetListWithCards(t *testing.T) {
	c := testClient()
	c.BaseURL = mockResponse("lists", "list-api-example.json").URL
	list, err := c.GetList("4eea4ff", Defaults())
	if err != nil {
		t.Fatal(err)
	}
	if len(list.Cards) == 0 {
		t.Fatal("cannot test cards as non was available in lists mock response")
	}
	if list.Cards[0].client == nil {
		t.Fatal("client not set on cards")
	}
}

func TestGetListsOnBoard(t *testing.T) {
	board := testBoard(t)
	board.client.BaseURL = mockResponse("lists", "board-lists-api-example.json").URL
	lists, err := board.GetLists(Defaults())
	if err != nil {
		t.Fatal(err)
	}

	if len(lists) != 3 {
		t.Errorf("Expected 1 list, got %d", len(lists))
	}
}

func TestGetListsOnBoardWithCards(t *testing.T) {
	board := testBoard(t)
	board.client.BaseURL = mockResponse("lists", "board-lists-api-example.json").URL
	lists, err := board.GetLists(Defaults())
	if err != nil {
		t.Fatal(err)
	}
	for i := range lists {
		if len(lists[i].Cards) == 0 {
			t.Fatal("cannot test cards as non was available in lists mock response")
		}
		if lists[i].Cards[0].client == nil {
			t.Fatal("client not set on cards")
		}
	}
}

// Utility function to get the standard case Client.GetList() response
//
func testList(t *testing.T) *List {
	c := testClient()
	c.BaseURL = mockResponse("lists", "list-api-example.json").URL
	list, err := c.GetList("4eea4ff", Defaults())
	if err != nil {
		t.Fatal(err)
	}
	return list
}

func TestCreateList(t *testing.T) {
	c := testClient()
	c.BaseURL = mockResponse("lists", "create-list-example.json").URL

	board := Board{
		client: c,
		ID:     "5c41027ca9c378795b5a5036",
	}

	listName := "hello"

	list, err := board.CreateList(listName, Arguments{"pos": "35"})
	if err != nil {
		t.Error(err)
	}
	if list.ID != "5ccd793e91682684235c0b13" {
		t.Errorf("Expected list to pick up an ID. Instead got '%s'.", list.ID)
	}
	if list.IDBoard != "5c41027ca9c378795b5a5036" {
		t.Errorf("Expected list to pick up board ID. Instead got '%s'.", list.IDBoard)
	}
	if list.Name != listName {
		t.Errorf("Expected list to pick up name. Instead got '%s'", list.Name)
	}
	if list.Pos != 35 {
		t.Errorf("Expected the returned list to pick up a position. Instead got '%v'.", list.Pos)
	}
	if list.Closed != false {
		t.Errorf("Expected list to pick up Closed. Instead got '%v'", list.Closed)
	}
	if list.client == nil {
		t.Errorf("Expected list to pick up client. Instead got nil")
	}

}<commit_msg>Test for updating a list<commit_after>// Copyright © 2016 Aaron Longwell
//
// Use of this source code is governed by an MIT licese.
// Details in the LICENSE file.

package trello

import (
	"testing"
	"time"
)

func TestListCreatedAt(t *testing.T) {
	l := List{ID: "4d5ea62fd76aa1136000000c"}
	ts := l.CreatedAt()
	if ts.IsZero() {
		t.Error("Time shouldn't be zero.")
	}
	if ts.Unix() != 1298048559 {
		t.Errorf("Incorrect CreatedAt() time: '%s'.", ts.Format(time.RFC3339))
	}
}

func TestGetList(t *testing.T) {
	list := testList(t)
	if list.Name != "To Do Soon" {
		t.Errorf("Title incorrect. Got '%s'", list.Name)
	}
}

func TestGetListWithCards(t *testing.T) {
	c := testClient()
	c.BaseURL = mockResponse("lists", "list-api-example.json").URL
	list, err := c.GetList("4eea4ff", Defaults())
	if err != nil {
		t.Fatal(err)
	}
	if len(list.Cards) == 0 {
		t.Fatal("cannot test cards as non was available in lists mock response")
	}
	if list.Cards[0].client == nil {
		t.Fatal("client not set on cards")
	}
}

func TestGetListsOnBoard(t *testing.T) {
	board := testBoard(t)
	board.client.BaseURL = mockResponse("lists", "board-lists-api-example.json").URL
	lists, err := board.GetLists(Defaults())
	if err != nil {
		t.Fatal(err)
	}

	if len(lists) != 3 {
		t.Errorf("Expected 1 list, got %d", len(lists))
	}
}

func TestGetListsOnBoardWithCards(t *testing.T) {
	board := testBoard(t)
	board.client.BaseURL = mockResponse("lists", "board-lists-api-example.json").URL
	lists, err := board.GetLists(Defaults())
	if err != nil {
		t.Fatal(err)
	}
	for i := range lists {
		if len(lists[i].Cards) == 0 {
			t.Fatal("cannot test cards as non was available in lists mock response")
		}
		if lists[i].Cards[0].client == nil {
			t.Fatal("client not set on cards")
		}
	}
}

// Utility function to get the standard case Client.GetList() response
//
func testList(t *testing.T) *List {
	c := testClient()
	c.BaseURL = mockResponse("lists", "list-api-example.json").URL
	list, err := c.GetList("4eea4ff", Defaults())
	if err != nil {
		t.Fatal(err)
	}
	return list
}

func TestCreateList(t *testing.T) {
	c := testClient()
	c.BaseURL = mockResponse("lists", "create-list-example.json").URL

	board := Board{
		client: c,
		ID:     "5c41027ca9c378795b5a5036",
	}

	listName := "hello"

	list, err := board.CreateList(listName, Arguments{"pos": "35"})
	if err != nil {
		t.Error(err)
	}
	if list.ID != "5ccd793e91682684235c0b13" {
		t.Errorf("Expected list to pick up an ID. Instead got '%s'.", list.ID)
	}
	if list.IDBoard != "5c41027ca9c378795b5a5036" {
		t.Errorf("Expected list to pick up board ID. Instead got '%s'.", list.IDBoard)
	}
	if list.Name != listName {
		t.Errorf("Expected list to pick up name. Instead got '%s'", list.Name)
	}
	if list.Pos != 35 {
		t.Errorf("Expected the returned list to pick up a position. Instead got '%v'.", list.Pos)
	}
	if list.Closed != false {
		t.Errorf("Expected list to pick up Closed. Instead got '%v'", list.Closed)
	}
	if list.client == nil {
		t.Errorf("Expected list to pick up client. Instead got nil")
	}
}

func TestUpdateList(t *testing.T) {
	c := testClient()
	c.BaseURL = mockResponse("lists", "create-list-example.json").URL

	// preparation: create board
	board := Board{
		client: c,
		ID:     "5c41027ca9c378795b5a5036",
	}

	listName := "hello"

	list, err := board.CreateList(listName, Arguments{"pos": "35"})
	if err != nil {
		t.Error(err)
	}
	if list.ID != "5ccd793e91682684235c0b13" {
		t.Errorf("Expected list to pick up an ID. Instead got '%s'.", list.ID)
	}
	if list.IDBoard != "5c41027ca9c378795b5a5036" {
		t.Errorf("Expected list to pick up board ID. Instead got '%s'.", list.IDBoard)
	}
	if list.Name != listName {
		t.Errorf("Expected list to pick up name. Instead got '%s'", list.Name)
	}
	if list.Pos != 35 {
		t.Errorf("Expected the returned list to pick up a position. Instead got '%v'.", list.Pos)
	}

	// update
	listName = "updated-list-name"

	c.BaseURL = mockResponse("lists", "update-list-example.json").URL
	updateArgs := Arguments{"name": listName, "idBoard": "5d31c3d8615ae32928635a28"}

	err = list.Update(updateArgs)

	if err != nil {
		t.Error(err)
	}
	if list.ID != "5ccd793e91682684235c0b13" {
		t.Errorf("Expected list to pick up an ID. Instead got '%s'.", list.ID)
	}
	if list.IDBoard != "5d31c3d8615ae32928635a28" {
		t.Errorf("Expected list to pick up board ID. Instead got '%s'.", list.IDBoard)
	}
	if list.Name != listName {
		t.Errorf("Expected list to pick up name. Instead got '%s'", list.Name)
	}
	if list.Pos != 24576 {
		t.Errorf("Expected the returned list to pick up a position. Instead got '%v'.", list.Pos)
	}
}
<|endoftext|>


<commit_before>package glutton

import (
	"bufio"
	"log"
	"net"
	"strings"
	"sync"
	"time"

	"github.com/hectane/go-nonblockingchan"
)

var counters Connections

type bufferedConn struct {
	r        *bufio.Reader
	net.Conn // So that most methods are embedded
}

func newBufferedConn(c net.Conn) bufferedConn {
	return bufferedConn{bufio.NewReader(c), c}
}

func newBufferedConnSize(c net.Conn, n int) bufferedConn {
	return bufferedConn{bufio.NewReaderSize(c, n), c}
}

func (b bufferedConn) Peek(n int) ([]byte, error) {
	return b.r.Peek(n)
}

func (b bufferedConn) Read(p []byte) (int, error) {
	return b.r.Read(p)
}

func handleTCPClient(conn net.Conn, ch *nbc.NonBlockingChan, counter ConnCounter) {
	// Splitting address to compare with conntrack logs
	srcAddr := conn.RemoteAddr().String()
	if srcAddr == "<nil>" {
		log.Println("Error. Address:port == nil glutton_server.go conn.RemoteAddr().String()")
		return
	}

	addr := strings.Split(srcAddr, ":")

	dp := GetTCPDesPort(addr, ch)

	if dp == -1 {
		log.Println("Warning. Packet dropped! [TCP] glutton_server.go desPort == -1")
		return
	}

	// TCP client for destination server
	handler := GetHandler(dp)
	if len(handler) < 2 {
		log.Println("No explicit handler found")
		handler = GetDefaultHandler()
		if handler == "" {
			log.Println("No default handler found. Packet dropped!")
			return
		}

	}

	if strings.HasPrefix(handler, "handle") {
		if strings.HasSuffix(handler, "telnet") {
			log.Printf("New connection from %s to port %d -> glutton:telnet\n", addr[0], dp)
			counter.incrCon()
			handleTelnet(time.Now().Unix(), conn)
			counter.decrCon()
		}
		if strings.HasSuffix(handler, "default") {
			log.Printf("New connection from %s to port %d -> glutton:default\n", addr[0], dp)
			counter.incrCon()
			bufConn := newBufferedConn(conn)
			snip, err := bufConn.Peek(4)
			if err != nil {
				log.Println(err)
			}
			httpMap := map[string]bool{"GET ": true, "POST": true, "HEAD": true}
			if _, ok := httpMap[string(snip)]; ok == true {
				log.Println("Handling HTTP")
				handleHTTP(bufConn)
			} else {
				handleDefault(conn)
			}
			counter.decrCon()
		}
	}

	if strings.HasPrefix(handler, "proxy") {
		proxyConn := TCPClient(handler[6:])
		if proxyConn == nil {
			return
		}

		log.Printf("New connection from %s to port %d -> glutton:Proxy\n", addr[0], dp)
		counter.incrCon()

		// Data Transfer between Connections
		clossedBy, err := ProxyServer(time.Now().Unix(), conn.(*net.TCPConn), proxyConn)
		counter.connectionClosed(srcAddr, handler[6:], clossedBy, err)
	}
}

// TCPListener listens for new TCP connections
func TCPListener(ch *nbc.NonBlockingChan, wg *sync.WaitGroup) {
	defer wg.Done()

	counters = Connections{}

	service := ":5000"

	addr, err := net.ResolveTCPAddr("tcp", service)
	CheckError("[*] ResolveTCPAddr Error.", err)

	// Listener for incoming TCP connections
	listener, err := net.ListenTCP("tcp", addr)
	CheckError("[*] Error in net.ListenTCP", err)

	for {
		conn, err := listener.Accept()
		if err != nil {
			continue
		}

		// Goroutines to handle multiple connections
		go handleTCPClient(conn, ch, &counters)
	}
}

func handleUDPClient(conn *net.UDPConn, ch *nbc.NonBlockingChan) {

	for {
		var b [1500]byte
		n, addr, err := conn.ReadFromUDP(b[0:])
		if err != nil {
			return
		}

		c := UDPConn{conn, addr, ch, b, n}
		go c.UDPBroker(&counters)
	}
}

// UDPListener listens for new UDP connections
func UDPListener(ch *nbc.NonBlockingChan, wg *sync.WaitGroup) {
	defer wg.Done()

	service := ":5000"

	addr, err := net.ResolveUDPAddr("udp", service)
	CheckError("[*] Error in UDP address resolving", err)

	// Listener for incoming UDP connections
	conn, err := net.ListenUDP("udp", addr)
	CheckError("[*] Error in UDP listener", err)

	handleUDPClient(conn, ch)

}
<commit_msg>handler added<commit_after>package glutton

import (
	"bufio"
	"log"
	"net"
	"strings"
	"sync"
	"time"

	"github.com/hectane/go-nonblockingchan"
)

var counters Connections

type bufferedConn struct {
	r        *bufio.Reader
	net.Conn // So that most methods are embedded
}

func newBufferedConn(c net.Conn) bufferedConn {
	return bufferedConn{bufio.NewReader(c), c}
}

func newBufferedConnSize(c net.Conn, n int) bufferedConn {
	return bufferedConn{bufio.NewReaderSize(c, n), c}
}

func (b bufferedConn) Peek(n int) ([]byte, error) {
	return b.r.Peek(n)
}

func (b bufferedConn) Read(p []byte) (int, error) {
	return b.r.Read(p)
}

func handleTCPClient(conn net.Conn, ch *nbc.NonBlockingChan, counter ConnCounter) {
	// Splitting address to compare with conntrack logs
	srcAddr := conn.RemoteAddr().String()
	if srcAddr == "<nil>" {
		log.Println("Error. Address:port == nil glutton_server.go conn.RemoteAddr().String()")
		return
	}

	addr := strings.Split(srcAddr, ":")

	dp := GetTCPDesPort(addr, ch)

	if dp == -1 {
		log.Println("Warning. Packet dropped! [TCP] glutton_server.go desPort == -1")
		return
	}

	// TCP client for destination server
	handler := GetHandler(dp)
	if len(handler) < 2 {
		log.Println("No explicit handler found")
		handler = GetDefaultHandler()
		if handler == "" {
			log.Println("No default handler found. Packet dropped!")
			return
		}

	}

	if strings.HasPrefix(handler, "handle") {
		if strings.HasSuffix(handler, "telnet") {
			log.Printf("New connection from %s to port %d -> glutton:telnet\n", addr[0], dp)
			counter.incrCon()
			handleTelnet(time.Now().Unix(), conn)
			counter.decrCon()
		}
		if strings.HasSuffix(handler, "sip") {
			log.Printf("New connection from %s to port %d -> glutton:sip\n", addr[0], dp)
			counter.incrCon()
			handleSIP(conn)
			counter.decrCon()
		}
		if strings.HasSuffix(handler, "default") {
			log.Printf("New connection from %s to port %d -> glutton:default\n", addr[0], dp)
			counter.incrCon()
			bufConn := newBufferedConn(conn)
			snip, err := bufConn.Peek(4)
			if err != nil {
				log.Println(err)
			}
			httpMap := map[string]bool{"GET ": true, "POST": true, "HEAD": true}
			if _, ok := httpMap[string(snip)]; ok == true {
				log.Println("Handling HTTP")
				handleHTTP(bufConn)
			} else {
				handleDefault(conn)
			}
			counter.decrCon()
		}
	}

	if strings.HasPrefix(handler, "proxy") {
		proxyConn := TCPClient(handler[6:])
		if proxyConn == nil {
			return
		}

		log.Printf("New connection from %s to port %d -> glutton:Proxy\n", addr[0], dp)
		counter.incrCon()

		// Data Transfer between Connections
		clossedBy, err := ProxyServer(time.Now().Unix(), conn.(*net.TCPConn), proxyConn)
		counter.connectionClosed(srcAddr, handler[6:], clossedBy, err)
	}
}

// TCPListener listens for new TCP connections
func TCPListener(ch *nbc.NonBlockingChan, wg *sync.WaitGroup) {
	defer wg.Done()

	counters = Connections{}

	service := ":5000"

	addr, err := net.ResolveTCPAddr("tcp", service)
	CheckError("[*] ResolveTCPAddr Error.", err)

	// Listener for incoming TCP connections
	listener, err := net.ListenTCP("tcp", addr)
	CheckError("[*] Error in net.ListenTCP", err)

	for {
		conn, err := listener.Accept()
		if err != nil {
			continue
		}

		// Goroutines to handle multiple connections
		go handleTCPClient(conn, ch, &counters)
	}
}

func handleUDPClient(conn *net.UDPConn, ch *nbc.NonBlockingChan) {

	for {
		var b [1500]byte
		n, addr, err := conn.ReadFromUDP(b[0:])
		if err != nil {
			return
		}

		c := UDPConn{conn, addr, ch, b, n}
		go c.UDPBroker(&counters)
	}
}

// UDPListener listens for new UDP connections
func UDPListener(ch *nbc.NonBlockingChan, wg *sync.WaitGroup) {
	defer wg.Done()

	service := ":5000"

	addr, err := net.ResolveUDPAddr("udp", service)
	CheckError("[*] Error in UDP address resolving", err)

	// Listener for incoming UDP connections
	conn, err := net.ListenUDP("udp", addr)
	CheckError("[*] Error in UDP listener", err)

	handleUDPClient(conn, ch)

}
<|endoftext|>


<commit_before>package responses

import (
	"encoding/json"
	"fmt"
	"github.com/perthgophers/puddle/messagerouter"
	"io/ioutil"
	"net/http"
	"net/url"
)

type cookiejar struct {
	jar map[string][]*http.Cookie
}

// Used to set cookies into HTTP lib cookiejar
func (p *cookiejar) SetCookies(u *url.URL, cookies []*http.Cookie) {
	fmt.Printf("The URL is %s\n", u.String())
	fmt.Printf("The cookie being set is : %s\n", cookies)
	p.jar[u.Host] = cookies
}

// Used to get cookies from HTTP lib cookiejar
func (p *cookiejar) Cookies(u *url.URL) []*http.Cookie {
	fmt.Printf("The URL is %s\n", u.String())
	fmt.Printf("The cookie being returned is : %s\n", p.jar[u.Host])
	return p.jar[u.Host]
}

// MemeGenerator will post the latest meme from imgur meme gallery
func MemeGenerator(cr *messagerouter.CommandRequest, w messagerouter.ResponseWriter) error {
	// Create cookiejar so we can authenticate to imgur API
	// http://stackoverflow.com/questions/11361431/authenticated-http-client-requests-from-golang

	// HTTP object to capture imgur API responses
	type HTTPResponse struct {
		Data []struct {
			AccountID    int         `json:"account_id"`
			AccountURL   string      `json:"account_url"`
			CommentCount int         `json:"comment_count"`
			Cover        string      `json:"cover"`
			CoverHeight  int         `json:"cover_height"`
			CoverWidth   int         `json:"cover_width"`
			Datetime     int         `json:"datetime"`
			Description  interface{} `json:"description"`
			Downs        int         `json:"downs"`
			Favorite     bool        `json:"favorite"`
			ID           string      `json:"id"`
			ImagesCount  int         `json:"images_count"`
			InGallery    bool        `json:"in_gallery"`
			IsAd         bool        `json:"is_ad"`
			IsAlbum      bool        `json:"is_album"`
			Layout       string      `json:"layout"`
			Link         string      `json:"link"`
			Nsfw         bool        `json:"nsfw"`
			Points       int         `json:"points"`
			Privacy      string      `json:"privacy"`
			Score        int         `json:"score"`
			Section      string      `json:"section"`
			Title        string      `json:"title"`
			Topic        string      `json:"topic"`
			TopicID      int         `json:"topic_id"`
			Ups          int         `json:"ups"`
			Views        int         `json:"views"`
			Vote         interface{} `json:"vote"`
		} `json:"data"`
	}

	client := &http.Client{}
	cjar := &cookiejar{}
	cjar.jar = make(map[string][]*http.Cookie)
	client.Jar = cjar

	// Authenticate
	req, err := http.NewRequest("GET", "https://api.imgur.com/3/g/memes/", nil)
	req.Header.Set("Authorization", "Client-ID 91101181cd13628")
	resp, err := client.Do(req)

	if err != nil {
		fmt.Println("Unable to get auth token from imgur api", err)
	}

	defer resp.Body.Close()

	var httpResponse HTTPResponse
	b, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("Unable to read", err)
	}

	err = json.Unmarshal(b, &httpResponse)

	if err != nil {
		fmt.Println("Unable to unmarshall", err)
	}

	// Post meme to slack channel using RTM
	w.Write(string(httpResponse.Data[0].Link))

	return nil
}

func init() {
	Handle("!meme", MemeGenerator)
}
<commit_msg>fixups<commit_after>package responses

import (
	"encoding/json"
	"fmt"
	"github.com/perthgophers/puddle/messagerouter"
	"io/ioutil"
	"net/http"
	"net/url"
)

type cookiejar struct {
	jar map[string][]*http.Cookie
}

// SetCookies to set cookies into HTTP lib cookiejar
func (p *cookiejar) SetCookies(u *url.URL, cookies []*http.Cookie) {
	fmt.Printf("The URL is %s\n", u.String())
	fmt.Printf("The cookie being set is: %s\n", cookies)
	p.jar[u.Host] = cookies
}

// Cookies to get cookies from HTTP lib cookiejar
func (cj *cookiejar) Cookies(u *url.URL) []*http.Cookie {
	fmt.Printf("The URL is %s\n", u.String())
	fmt.Printf("The cookie being returned is : %s\n", cj.jar[u.Host])
	return cj.jar[u.Host]
}

// MemeGenerator will post the latest meme from imgur meme gallery
func MemeGenerator(cr *messagerouter.CommandRequest, w messagerouter.ResponseWriter) error {
	// Create cookiejar so we can authenticate to imgur API
	// http://stackoverflow.com/questions/11361431/authenticated-http-client-requests-from-golang

	// HTTP object to capture imgur API responses
	type HTTPResponse struct {
		Data []struct {
			AccountID    int         `json:"account_id"`
			AccountURL   string      `json:"account_url"`
			CommentCount int         `json:"comment_count"`
			Cover        string      `json:"cover"`
			CoverHeight  int         `json:"cover_height"`
			CoverWidth   int         `json:"cover_width"`
			Datetime     int         `json:"datetime"`
			Description  interface{} `json:"description"`
			Downs        int         `json:"downs"`
			Favorite     bool        `json:"favorite"`
			ID           string      `json:"id"`
			ImagesCount  int         `json:"images_count"`
			InGallery    bool        `json:"in_gallery"`
			IsAd         bool        `json:"is_ad"`
			IsAlbum      bool        `json:"is_album"`
			Layout       string      `json:"layout"`
			Link         string      `json:"link"`
			Nsfw         bool        `json:"nsfw"`
			Points       int         `json:"points"`
			Privacy      string      `json:"privacy"`
			Score        int         `json:"score"`
			Section      string      `json:"section"`
			Title        string      `json:"title"`
			Topic        string      `json:"topic"`
			TopicID      int         `json:"topic_id"`
			Ups          int         `json:"ups"`
			Views        int         `json:"views"`
			Vote         interface{} `json:"vote"`
		} `json:"data"`
	}

	client := &http.Client{}
	cjar := &cookiejar{}
	cjar.jar = make(map[string][]*http.Cookie)
	client.Jar = cjar

	// Authenticate
	req, err := http.NewRequest("GET", "https://api.imgur.com/3/g/memes/", nil)
	req.Header.Set("Authorization", "Client-ID 91101181cd13628")
	resp, err := client.Do(req)

	if err != nil {
		fmt.Println("Unable to get auth token from imgur api: ", err)
	}

	defer resp.Body.Close()

	var httpResponse HTTPResponse
	b, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("Unable to read", err)
	}

	err = json.Unmarshal(b, &httpResponse)

	if err != nil {
		fmt.Println("Unable to unmarshall", err)
	}

	// Post meme to slack channel using RTM
	w.Write(string(httpResponse.Data[0].Link))

	return nil
}

func init() {
	Handle("!meme", MemeGenerator)
}
<|endoftext|>


<commit_before>package rest_test

import (
	"os"
	"runtime"
	"testing"
	"time"

	"github.com/synapse-garden/sg-proto/admin"
	"github.com/synapse-garden/sg-proto/auth"
	"github.com/synapse-garden/sg-proto/convo"
	"github.com/synapse-garden/sg-proto/incept"
	"github.com/synapse-garden/sg-proto/store"
	"github.com/synapse-garden/sg-proto/stream"
	"github.com/synapse-garden/sg-proto/stream/river"
	sgt "github.com/synapse-garden/sg-proto/testing"
	"github.com/synapse-garden/sg-proto/users"

	"github.com/boltdb/bolt"
	uuid "github.com/satori/go.uuid"
	. "gopkg.in/check.v1"
)

func Test(t *testing.T) { TestingT(t) }

type RESTSuite struct {
	db      *bolt.DB
	tmpDir  string
	tickets []incept.Ticket
}

var (
	_ = Suite(&RESTSuite{})
)

func (s *RESTSuite) SetUpTest(c *C) {
	db, tmpDir, err := sgt.TempDB("sg-test")
	c.Assert(err, IsNil)
	c.Assert(db.Update(store.Wrap(
		store.Migrate(store.Version),
		store.SetupBuckets(
			admin.AdminBucket,
			incept.TicketBucket,
			users.UserBucket,
			auth.LoginBucket,
			auth.SessionBucket,
			auth.RefreshBucket,
			auth.ContextBucket,
			stream.StreamBucket,
			river.RiverBucket,
			convo.ConvoBucket,
			convo.MessageBucket,
			convo.ScribeBucket,
		),
	)), IsNil)
	s.db, s.tmpDir = db, tmpDir

	tkts := make([]incept.Ticket, 3)
	for i := range tkts {
		tkts[i] = incept.Ticket(uuid.NewV4())
	}
	c.Assert(s.db.Update(incept.NewTickets(tkts...)), IsNil)

	s.tickets = tkts
}

func (s *RESTSuite) TearDownTest(c *C) {
	runtime.Gosched()
	time.Sleep(sgt.CleanupWait)
	if db := s.db; db != nil {
		c.Assert(sgt.CleanupDB(db), IsNil)
		c.Assert(os.Remove(s.tmpDir), IsNil)
	}
}
<commit_msg>Update rest/rest_test for task API testing<commit_after>package rest_test

import (
	"os"
	"runtime"
	"testing"
	"time"

	"github.com/synapse-garden/sg-proto/admin"
	"github.com/synapse-garden/sg-proto/auth"
	"github.com/synapse-garden/sg-proto/convo"
	"github.com/synapse-garden/sg-proto/incept"
	"github.com/synapse-garden/sg-proto/store"
	"github.com/synapse-garden/sg-proto/stream"
	"github.com/synapse-garden/sg-proto/stream/river"
	"github.com/synapse-garden/sg-proto/task"
	sgt "github.com/synapse-garden/sg-proto/testing"
	"github.com/synapse-garden/sg-proto/text"
	"github.com/synapse-garden/sg-proto/users"

	"github.com/boltdb/bolt"
	uuid "github.com/satori/go.uuid"
	. "gopkg.in/check.v1"
)

func Test(t *testing.T) { TestingT(t) }

type RESTSuite struct {
	db      *bolt.DB
	tmpDir  string
	tickets []incept.Ticket
}

var (
	_ = Suite(&RESTSuite{})
)

func (s *RESTSuite) SetUpTest(c *C) {
	db, tmpDir, err := sgt.TempDB("sg-test")
	c.Assert(err, IsNil)
	c.Assert(db.Update(store.Wrap(
		store.Migrate(store.Version),
		store.SetupBuckets(
			admin.AdminBucket,
			incept.TicketBucket,
			users.UserBucket,
			auth.LoginBucket,
			auth.SessionBucket,
			auth.RefreshBucket,
			auth.ContextBucket,
			stream.StreamBucket,
			river.RiverBucket,
			convo.ConvoBucket,
			convo.MessageBucket,
			convo.ScribeBucket,
			text.TextBucket,
			task.TaskBucket,
		),
	)), IsNil)
	s.db, s.tmpDir = db, tmpDir

	tkts := make([]incept.Ticket, 3)
	for i := range tkts {
		tkts[i] = incept.Ticket(uuid.NewV4())
	}
	c.Assert(s.db.Update(incept.NewTickets(tkts...)), IsNil)

	s.tickets = tkts
}

func (s *RESTSuite) TearDownTest(c *C) {
	runtime.Gosched()
	time.Sleep(sgt.CleanupWait)
	if db := s.db; db != nil {
		c.Assert(sgt.CleanupDB(db), IsNil)
		c.Assert(os.Remove(s.tmpDir), IsNil)
	}
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"regexp"
	"strconv"
	"strings"
)

var FIELD = regexp.MustCompile(`(?:[^,\\]|\\.)*`)

type Selector interface {
	DropHeaders() bool
	ParseHeaders(headers []string) error
	Select(recode []string) ([]string, error)
}

type All struct {
}

func NewAll() *All {
	return &All{}
}

func (a *All) DropHeaders() bool {
	return false
}

func (a *All) ParseHeaders(headers []string) error {
	return nil
}

func (a *All) Select(recode []string) ([]string, error) {
	return recode, nil
}

type Indexes struct {
	indexes []int
}

func NewIndexes(list string) (*Indexes, error) {
	if list == "" {
		return &Indexes{
			indexes: []int{},
		}, nil
	}
	fields := FIELD.FindAllString(list, -1)

	indexes := make([]int, len(fields))
	for i, field := range fields {
		index, err := strconv.Atoi(field)
		if err != nil {
			return nil, err
		}
		if index < 1 {
			return nil, fmt.Errorf("indexes are numberd from 1")
		}
		indexes[i] = index - 1
	}
	return &Indexes{
		indexes: indexes,
	}, nil
}

func (i *Indexes) DropHeaders() bool {
	return false
}

func (i *Indexes) ParseHeaders(headers []string) error {
	return nil
}

func (i *Indexes) Select(recode []string) ([]string, error) {
	a := make([]string, len(i.indexes))
	for j, index := range i.indexes {
		if index >= 0 && index < len(recode) {
			a[j] = recode[index]
		}
	}
	return a, nil
}

type Headers struct {
	indexes []int
	headers []string
}

func NewHeaders(list string) (*Headers, error) {
	headers := FIELD.FindAllString(list, -1)
	for i := 0; i < len(headers); i++ {
		headers[i] = strings.Replace(headers[i], `\,`, `,`, -1)
	}
	return &Headers{
		headers: headers,
	}, nil
}

func (h *Headers) DropHeaders() bool {
	return true
}

func (h *Headers) ParseHeaders(headers []string) error {
	indexMap := make(map[string]int)
	for i, header := range headers {
		if _, ok := indexMap[header]; ok {
			return fmt.Errorf("%q: duplicated header", header)
		}
		indexMap[header] = i
	}

	h.indexes = make([]int, len(h.headers))
	for i, header := range h.headers {
		if index, ok := indexMap[header]; ok {
			h.indexes[i] = index
		} else {
			h.indexes[i] = -1
		}
	}
	return nil
}

func (h *Headers) Select(recode []string) ([]string, error) {
	a := make([]string, len(h.indexes))
	for i, index := range h.indexes {
		if index != -1 {
			a[i] = recode[index]
		}
	}
	return a, nil
}
<commit_msg>Allow empty header<commit_after>package main

import (
	"fmt"
	"regexp"
	"strconv"
	"strings"
)

var FIELD = regexp.MustCompile(`(?:[^,\\]|\\.)*`)

type Selector interface {
	DropHeaders() bool
	ParseHeaders(headers []string) error
	Select(recode []string) ([]string, error)
}

type All struct {
}

func NewAll() *All {
	return &All{}
}

func (a *All) DropHeaders() bool {
	return false
}

func (a *All) ParseHeaders(headers []string) error {
	return nil
}

func (a *All) Select(recode []string) ([]string, error) {
	return recode, nil
}

type Indexes struct {
	indexes []int
}

func NewIndexes(list string) (*Indexes, error) {
	if list == "" {
		return &Indexes{
			indexes: []int{},
		}, nil
	}
	fields := FIELD.FindAllString(list, -1)

	indexes := make([]int, len(fields))
	for i, field := range fields {
		index, err := strconv.Atoi(field)
		if err != nil {
			return nil, err
		}
		if index < 1 {
			return nil, fmt.Errorf("indexes are numberd from 1")
		}
		indexes[i] = index - 1
	}
	return &Indexes{
		indexes: indexes,
	}, nil
}

func (i *Indexes) DropHeaders() bool {
	return false
}

func (i *Indexes) ParseHeaders(headers []string) error {
	return nil
}

func (i *Indexes) Select(recode []string) ([]string, error) {
	a := make([]string, len(i.indexes))
	for j, index := range i.indexes {
		if index >= 0 && index < len(recode) {
			a[j] = recode[index]
		}
	}
	return a, nil
}

type Headers struct {
	indexes []int
	headers []string
}

func NewHeaders(list string) (*Headers, error) {
	if list == "" {
		return &Headers{
			headers: []string{},
		}, nil
	}

	headers := FIELD.FindAllString(list, -1)
	for i := 0; i < len(headers); i++ {
		headers[i] = strings.Replace(headers[i], `\,`, `,`, -1)
	}
	return &Headers{
		headers: headers,
	}, nil
}

func (h *Headers) DropHeaders() bool {
	return true
}

func (h *Headers) ParseHeaders(headers []string) error {
	indexMap := make(map[string]int)
	for i, header := range headers {
		if _, ok := indexMap[header]; ok {
			return fmt.Errorf("%q: duplicated header", header)
		}
		indexMap[header] = i
	}

	h.indexes = make([]int, len(h.headers))
	for i, header := range h.headers {
		if index, ok := indexMap[header]; ok {
			h.indexes[i] = index
		} else {
			h.indexes[i] = -1
		}
	}
	return nil
}

func (h *Headers) Select(recode []string) ([]string, error) {
	a := make([]string, len(h.indexes))
	for i, index := range h.indexes {
		if index != -1 {
			a[i] = recode[index]
		}
	}
	return a, nil
}
<|endoftext|>


<commit_before>package common

import (
	"fmt"
	"log"
	"os/exec"
	"strings"

	"github.com/skeswa/gophr/common/models"
)

var (
	folderName              string
	gitHubRemoteOrigin      = "git@github.com:gophr-packages/%s.git"
	navigateToPackageFolder = "cd /tmp/%s"
)

var (
	initalizeRepo    = "cd /tmp && mkdir %s && cd %s && git init"
	createBranch     = "%s && git checkout -b %s"
	setRemoteCommand = "%s && git remote add origin %s"
	fetchRepoArchive = "%s && wget https://github.com/%s/%s/archive/%s.zip"
	unzipRepoArchive = "%s && unzip %s.zip && cd %s && mv * ../ && cd .. && rm %s.zip && rm -rf %s"
	addFiles         = "%s && git add . "
	commitFiles      = "%s && git commit -m \" %s \""
	pushFiles        = "%s && git push --set-upstream origin %s"
)

// SubVersionPackageModel creates a github repo for the packageModel on gophr-packages
// versioned a the speicifed ref
func SubVersionPackageModel(packageModel *models.PackageModel, ref string) {
	// If no ref is present or it's master we need the current Master SHA
	if len(ref) == 0 || ref == "master" {
		curretRef, err := FetchRefs(*packageModel.Author, *packageModel.Repo)
		if err != nil || len(curretRef.MasterRefHash) == 0 {
			// TODO: return error here
			return
		}
		ref = curretRef.MasterRefHash
	}
	// First check if this ref has already been versioned for this packageModel
	log.Println("Checking if ref has been versioned before")
	exists, err := CheckIfRefExists(*packageModel.Author, *packageModel.Repo, ref)
	if exists == true && err == nil {
		log.Println("That ref has already been versioned")
		return
	}

	if err != nil {
		log.Printf("Error occured in checking if ref exists %s", err)
	}

	log.Printf("%s/%s@%s has not been versioned yet",
		GitHubGophrPackageOrgName,
		BuildNewGitHubRepoName(*packageModel.Author, *packageModel.Repo),
		BuildGitHubBranch(ref),
	)
	// Set working folderName for package
	folderName = BuildNewGitHubRepoName(*packageModel.Author, *packageModel.Repo)

	// Instantiate New Github Request Service
	log.Println("Initializing gitHub component")

	gitHubRequestService := NewGitHubRequestService()

	log.Printf("Creating new Github repo for %s/%s at %s",
		*packageModel.Author,
		*packageModel.Repo,
		ref,
	)
	err = gitHubRequestService.CreateNewGitHubRepo(*packageModel)
	log.Printf("%s", err)

	log.Printf("Initializing folder and initializing git repo for %s \n", folderName)
	err = initializeRepoCMD(packageModel)
	checkError(err, folderName)

	log.Printf("Creating branch %s \n", BuildGitHubBranch(ref))
	err = createBranchCMD(packageModel, ref)
	checkError(err, folderName)

	log.Printf("Setting remote branch url %s \n", BuildGitHubBranch(ref))
	err = setRemoteCMD(packageModel, ref)
	checkError(err, folderName)

	log.Printf("Fetching github archive for %s/%s with tag %s \n",
		*packageModel.Author,
		*packageModel.Repo,
		ref,
	)
	err = fetchArchiveCMD(packageModel, ref)
	checkError(err, folderName)

	log.Printf("Fetching github archive for %s/%s with tag %s \n",
		*packageModel.Author,
		*packageModel.Repo,
		ref,
	)
	err = unzipArchiveCMD(packageModel, ref)
	checkError(err, folderName)

	// TODO subverisoning
	// Create array of all sub-dependencies

	log.Println("Adding unarchived repo data to branch")
	err = addFilesCMD()
	checkError(err, folderName)

	log.Println("Commiting repo data to branch")
	err = commitFilesCMD(packageModel, ref)
	checkError(err, folderName)

	log.Printf("Pushing files to branch %s \n", BuildRemoteURL(packageModel, ref))
	err = pushFilesCMD(packageModel, ref)
	checkError(err, folderName)

	cleanUpExitHook(folderName)
}

func initializeRepoCMD(packageModel *models.PackageModel) error {
	log.Println("Initializing folder and repo commmand")
	cmd := fmt.Sprintf(initalizeRepo, folderName, folderName)
	log.Println(cmd)
	out, err := exec.Command("sh", "-c", cmd).Output()
	log.Printf("Output: %s \n", out)
	return err
}

func createBranchCMD(packageModel *models.PackageModel, ref string) error {
	log.Println("Initializing folder and repo commmand")
	navigateFolderCMD := fmt.Sprintf(navigateToPackageFolder, folderName)
	cmd := fmt.Sprintf(createBranch, navigateFolderCMD, BuildGitHubBranch(ref))
	log.Println(cmd)
	out, err := exec.Command("sh", "-c", cmd).Output()
	log.Printf("Output: %s \n", out)
	return err
}

func setRemoteCMD(packageModel *models.PackageModel, ref string) error {
	log.Println("Initializing folder and repo commmand")
	navigateFolderCMD := fmt.Sprintf(navigateToPackageFolder, folderName)
	remoteURL := BuildRemoteURL(packageModel, ref)
	cmd := fmt.Sprintf(setRemoteCommand, navigateFolderCMD, remoteURL)
	log.Println(cmd)
	out, err := exec.Command("sh", "-c", cmd).Output()
	log.Printf("Output: %s \n", out)
	return err
}
func fetchArchiveCMD(packageModel *models.PackageModel, ref string) error {
	log.Println("Fetching and Unzipping Archive for tag")
	navigateFolderCMD := fmt.Sprintf(navigateToPackageFolder, folderName)
	cmd := fmt.Sprintf(fetchRepoArchive, navigateFolderCMD, *packageModel.Author, *packageModel.Repo, ref)
	log.Println(cmd)
	out, err := exec.Command("sh", "-c", cmd).Output()
	log.Printf("Output: %s \n", out)

	return err
}

func unzipArchiveCMD(packageModel *models.PackageModel, ref string) error {
	log.Println("Fetching and Unzipping Archive for tag")
	navigateFolderCMD := fmt.Sprintf(navigateToPackageFolder, folderName)
	zipFolder := *packageModel.Repo + "-" + ref
	cmd := fmt.Sprintf(unzipRepoArchive, navigateFolderCMD, ref, zipFolder, ref, zipFolder)
	log.Println(cmd)
	out, err := exec.Command("sh", "-c", cmd).Output()
	log.Printf("Output: %s \n", out)

	return err
}

func addFilesCMD() error {
	navigateFolderCMD := fmt.Sprintf(navigateToPackageFolder, folderName)
	cmd := fmt.Sprintf(addFiles, navigateFolderCMD)
	log.Println(cmd)
	out, err := exec.Command("sh", "-c", cmd).Output()
	log.Printf("Output: %s \n", out)

	return err
}

// TODO add version
func commitFilesCMD(packageModel *models.PackageModel, ref string) error {
	navigateFolderCMD := fmt.Sprintf(navigateToPackageFolder, folderName)
	commitMessage := fmt.Sprintf("Gophr versioned repo of %s / %s @ %s", *packageModel.Author, *packageModel.Repo, ref)
	cmd := fmt.Sprintf(commitFiles, navigateFolderCMD, commitMessage)
	log.Println(cmd)
	out, err := exec.Command("sh", "-c", cmd).Output()
	log.Printf("Output: %s \n", out)

	return err
}

func pushFilesCMD(packageModel *models.PackageModel, ref string) error {
	navigateFolderCMD := fmt.Sprintf(navigateToPackageFolder, folderName)
	cmd := fmt.Sprintf(pushFiles, navigateFolderCMD, BuildGitHubBranch(ref))
	log.Println(cmd)
	out, err := exec.Command("sh", "-c", cmd).Output()
	log.Printf("Output: %s 		\n", out)
	return err
}

// Helper functions

// Exit Hook to clean up files
func cleanUpExitHook(folderName string) {
	log.Printf("Exit hook initiated deleting folder %s \n", folderName)
	// TODO:(Shikkic)
	if strings.HasPrefix(folderName, "/") == true {
		//cmd := fmt.Sprintf("cd /tmp && rm -rf %s", folderName)
		//out, err := exec.Command("sh", "-c", cmd).Output()
		/*if err != nil {
			log.Println("Could not properly engage exit hook")
			log.Fatalln(err)
		}*/
		//log.Printf("Output: %s", out)
	} else {
		log.Println("Cowardly refusing to initiate exit hook. Will not rm -rf folder names that contains any leading '/'")
	}
}

// Check Error and Engage Exit Hook if fatal error occured
func checkError(err error, folderName string) {
	if err != nil {
		cleanUpExitHook(folderName)
		log.Fatalf("Error occured %s \n", err)
	}
}
<commit_msg>uncomment exit hook<commit_after>package common

import (
	"fmt"
	"log"
	"os/exec"
	"strings"

	"github.com/skeswa/gophr/common/models"
)

var (
	folderName              string
	gitHubRemoteOrigin      = "git@github.com:gophr-packages/%s.git"
	navigateToPackageFolder = "cd /tmp/%s"
)

var (
	initalizeRepo    = "cd /tmp && mkdir %s && cd %s && git init"
	createBranch     = "%s && git checkout -b %s"
	setRemoteCommand = "%s && git remote add origin %s"
	fetchRepoArchive = "%s && wget https://github.com/%s/%s/archive/%s.zip"
	unzipRepoArchive = "%s && unzip %s.zip && cd %s && mv * ../ && cd .. && rm %s.zip && rm -rf %s"
	addFiles         = "%s && git add . "
	commitFiles      = "%s && git commit -m \" %s \""
	pushFiles        = "%s && git push --set-upstream origin %s"
)

// SubVersionPackageModel creates a github repo for the packageModel on gophr-packages
// versioned a the speicifed ref
func SubVersionPackageModel(packageModel *models.PackageModel, ref string) {
	// If no ref is present or it's master we need the current Master SHA
	if len(ref) == 0 || ref == "master" {
		curretRef, err := FetchRefs(*packageModel.Author, *packageModel.Repo)
		if err != nil || len(curretRef.MasterRefHash) == 0 {
			// TODO: return error here
			return
		}
		ref = curretRef.MasterRefHash
	}
	// First check if this ref has already been versioned for this packageModel
	log.Println("Checking if ref has been versioned before")
	exists, err := CheckIfRefExists(*packageModel.Author, *packageModel.Repo, ref)
	if exists == true && err == nil {
		log.Println("That ref has already been versioned")
		return
	}

	if err != nil {
		log.Printf("Error occured in checking if ref exists %s", err)
	}

	log.Printf("%s/%s@%s has not been versioned yet",
		GitHubGophrPackageOrgName,
		BuildNewGitHubRepoName(*packageModel.Author, *packageModel.Repo),
		BuildGitHubBranch(ref),
	)
	// Set working folderName for package
	folderName = BuildNewGitHubRepoName(*packageModel.Author, *packageModel.Repo)

	// Instantiate New Github Request Service
	log.Println("Initializing gitHub component")

	gitHubRequestService := NewGitHubRequestService()

	log.Printf("Creating new Github repo for %s/%s at %s",
		*packageModel.Author,
		*packageModel.Repo,
		ref,
	)
	err = gitHubRequestService.CreateNewGitHubRepo(*packageModel)
	log.Printf("%s", err)

	log.Printf("Initializing folder and initializing git repo for %s \n", folderName)
	err = initializeRepoCMD(packageModel)
	checkError(err, folderName)

	log.Printf("Creating branch %s \n", BuildGitHubBranch(ref))
	err = createBranchCMD(packageModel, ref)
	checkError(err, folderName)

	log.Printf("Setting remote branch url %s \n", BuildGitHubBranch(ref))
	err = setRemoteCMD(packageModel, ref)
	checkError(err, folderName)

	log.Printf("Fetching github archive for %s/%s with tag %s \n",
		*packageModel.Author,
		*packageModel.Repo,
		ref,
	)
	err = fetchArchiveCMD(packageModel, ref)
	checkError(err, folderName)

	log.Printf("Fetching github archive for %s/%s with tag %s \n",
		*packageModel.Author,
		*packageModel.Repo,
		ref,
	)
	err = unzipArchiveCMD(packageModel, ref)
	checkError(err, folderName)

	// TODO subverisoning
	// Create array of all sub-dependencies

	log.Println("Adding unarchived repo data to branch")
	err = addFilesCMD()
	checkError(err, folderName)

	log.Println("Commiting repo data to branch")
	err = commitFilesCMD(packageModel, ref)
	checkError(err, folderName)

	log.Printf("Pushing files to branch %s \n", BuildRemoteURL(packageModel, ref))
	err = pushFilesCMD(packageModel, ref)
	checkError(err, folderName)

	cleanUpExitHook(folderName)
}

func initializeRepoCMD(packageModel *models.PackageModel) error {
	log.Println("Initializing folder and repo commmand")
	cmd := fmt.Sprintf(initalizeRepo, folderName, folderName)
	log.Println(cmd)
	out, err := exec.Command("sh", "-c", cmd).Output()
	log.Printf("Output: %s \n", out)
	return err
}

func createBranchCMD(packageModel *models.PackageModel, ref string) error {
	log.Println("Initializing folder and repo commmand")
	navigateFolderCMD := fmt.Sprintf(navigateToPackageFolder, folderName)
	cmd := fmt.Sprintf(createBranch, navigateFolderCMD, BuildGitHubBranch(ref))
	log.Println(cmd)
	out, err := exec.Command("sh", "-c", cmd).Output()
	log.Printf("Output: %s \n", out)
	return err
}

func setRemoteCMD(packageModel *models.PackageModel, ref string) error {
	log.Println("Initializing folder and repo commmand")
	navigateFolderCMD := fmt.Sprintf(navigateToPackageFolder, folderName)
	remoteURL := BuildRemoteURL(packageModel, ref)
	cmd := fmt.Sprintf(setRemoteCommand, navigateFolderCMD, remoteURL)
	log.Println(cmd)
	out, err := exec.Command("sh", "-c", cmd).Output()
	log.Printf("Output: %s \n", out)
	return err
}
func fetchArchiveCMD(packageModel *models.PackageModel, ref string) error {
	log.Println("Fetching and Unzipping Archive for tag")
	navigateFolderCMD := fmt.Sprintf(navigateToPackageFolder, folderName)
	cmd := fmt.Sprintf(fetchRepoArchive, navigateFolderCMD, *packageModel.Author, *packageModel.Repo, ref)
	log.Println(cmd)
	out, err := exec.Command("sh", "-c", cmd).Output()
	log.Printf("Output: %s \n", out)

	return err
}

func unzipArchiveCMD(packageModel *models.PackageModel, ref string) error {
	log.Println("Fetching and Unzipping Archive for tag")
	navigateFolderCMD := fmt.Sprintf(navigateToPackageFolder, folderName)
	zipFolder := *packageModel.Repo + "-" + ref
	cmd := fmt.Sprintf(unzipRepoArchive, navigateFolderCMD, ref, zipFolder, ref, zipFolder)
	log.Println(cmd)
	out, err := exec.Command("sh", "-c", cmd).Output()
	log.Printf("Output: %s \n", out)

	return err
}

func addFilesCMD() error {
	navigateFolderCMD := fmt.Sprintf(navigateToPackageFolder, folderName)
	cmd := fmt.Sprintf(addFiles, navigateFolderCMD)
	log.Println(cmd)
	out, err := exec.Command("sh", "-c", cmd).Output()
	log.Printf("Output: %s \n", out)

	return err
}

// TODO add version
func commitFilesCMD(packageModel *models.PackageModel, ref string) error {
	navigateFolderCMD := fmt.Sprintf(navigateToPackageFolder, folderName)
	commitMessage := fmt.Sprintf("Gophr versioned repo of %s / %s @ %s", *packageModel.Author, *packageModel.Repo, ref)
	cmd := fmt.Sprintf(commitFiles, navigateFolderCMD, commitMessage)
	log.Println(cmd)
	out, err := exec.Command("sh", "-c", cmd).Output()
	log.Printf("Output: %s \n", out)

	return err
}

func pushFilesCMD(packageModel *models.PackageModel, ref string) error {
	navigateFolderCMD := fmt.Sprintf(navigateToPackageFolder, folderName)
	cmd := fmt.Sprintf(pushFiles, navigateFolderCMD, BuildGitHubBranch(ref))
	log.Println(cmd)
	out, err := exec.Command("sh", "-c", cmd).Output()
	log.Printf("Output: %s 		\n", out)
	return err
}

// Helper functions

// Exit Hook to clean up files
func cleanUpExitHook(folderName string) {
	log.Printf("Exit hook initiated deleting folder %s \n", folderName)
	// TODO:(Shikkic)
	if strings.HasPrefix(folderName, "/") == true {
		cmd := fmt.Sprintf("cd /tmp && rm -rf %s", folderName)
		out, err := exec.Command("sh", "-c", cmd).Output()
		if err != nil {
			log.Println("Could not properly engage exit hook")
			log.Fatalln(err)
		}
		log.Printf("Output: %s", out)
	} else {
		log.Println("Cowardly refusing to initiate exit hook. Will not rm -rf folder names that contains any leading '/'")
	}
}

// Check Error and Engage Exit Hook if fatal error occured
func checkError(err error, folderName string) {
	if err != nil {
		cleanUpExitHook(folderName)
		log.Fatalf("Error occured %s \n", err)
	}
}
<|endoftext|>