<commit_before>package dora

import (
	"github.com/bnagy/gapstone"
	"github.com/fatih/color"
	w "github.com/williballenthin/Lancelot/workspace"
	"log"
)

func check(e error) {
	if e != nil {
		panic(e)
	}
}

// dora the explora
type Dora struct {
	ws *w.Workspace
	ac ArtifactCollection
}

func New(ws *w.Workspace) (*Dora, error) {
	// TODO: get this from a real place
	ac, e := NewLoggingArtifactCollection()
	check(e)

	return &Dora{
		ws: ws,
		ac: ac,
	}, nil
}

func isBBEnd(insn gapstone.Instruction) bool {
	return w.DoesInstructionHaveGroup(insn, gapstone.X86_GRP_CALL) ||
		w.DoesInstructionHaveGroup(insn, gapstone.X86_GRP_JUMP) ||
		w.DoesInstructionHaveGroup(insn, gapstone.X86_GRP_RET) ||
		w.DoesInstructionHaveGroup(insn, gapstone.X86_GRP_IRET)
}

func GetNextInstructionPointer(emu *w.Emulator, sman *w.SnapshotManager) (w.VA, error) {
	var va w.VA
	e := sman.WithTempExcursion(func() error {
		e := emu.StepInto()
		if e != nil {
			return e
		}
		va = emu.GetInstructionPointer()
		return nil
	})
	return va, e
}

type todoPath struct {
	state w.SnapshotManagerCookie
	va    w.VA
}

func IsConditionalJump(insn gapstone.Instruction) bool {
	if w.DoesInstructionHaveGroup(insn, gapstone.X86_GRP_JUMP) && insn.Mnemonic != "jmp" {
		return true
	}
	if w.DoesInstructionHaveGroup(insn, gapstone.X86_GRP_JUMP) && insn.Mnemonic == "jmp" {
		if insn.Mnemonic == "jmp" && insn.X86.Operands[0].Type == gapstone.X86_OP_IMM {
			// jmp 0x1000
			return false
		} else {
			// jmp eax
			return true
		}
	}
	return false
}

func GetJumpTargets(emu *w.Emulator, insn gapstone.Instruction) ([]w.VA, error) {
	var ret []w.VA

	if w.DoesInstructionHaveGroup(insn, gapstone.X86_GRP_JUMP) && insn.Mnemonic == "jmp" {
		if insn.X86.Operands[0].Type == gapstone.X86_OP_IMM {
			// not a conditional jump???
			return ret, w.InvalidArgumentError
		}
		// jmp eax
		// don't know how to handle this case right now
		return ret, nil
	}

	// assume a two case situation
	falsePc := w.VA(uint64(emu.GetInstructionPointer()) + uint64(insn.Size))
	truePc := w.VA(insn.X86.Operands[0].Imm) // or .Mem???

	if truePc == 0 {
		// TODO
		panic("zero jump")
	}

	ret = append(ret, truePc, falsePc)
	return ret, nil
}

// things yet to discover:
//   OK: final stack delta
//   TODO: arguments passed in registers
//     insn.cs_detail.regs_read/regs_write
//   TODO: arguments passed on stack
//   TODO: all basic blocks
//   TODO: calling convention
//   TODO: no return functions
// TODO: ensure stack is set up with return pointer and some junk symbolic args
// TODO: track max hits
// this is going to be a pretty wild function :-(
func (dora *Dora) ExploreFunction(va w.VA) error {
	emu, e := dora.ws.GetEmulator()
	check(e)
	defer emu.Close()

	sman, e := w.NewSnapshotManager(emu)
	check(e)
	defer sman.Close()

	bbStart := va
	emu.SetInstructionPointer(va)
	check(e)

	beforeSp := emu.GetStackPointer()

	// TODO: how to disable these while on an excursion?
	rh, e := emu.HookMemRead(func(access int, addr w.VA, size int, value int64) {
		dora.ac.AddMemoryReadXref(MemoryReadCrossReference{emu.GetInstructionPointer(), addr})
	})
	check(e)
	defer rh.Close()

	wh, e := emu.HookMemWrite(func(access int, addr w.VA, size int, value int64) {
		dora.ac.AddMemoryWriteXref(MemoryWriteCrossReference{emu.GetInstructionPointer(), addr})
	})
	check(e)
	defer wh.Close()

	hitVas := make(map[w.VA]bool)

	var todoPaths = []todoPath{}
	here, e := sman.GetCurrentCookie()
	check(e)
	todoPaths = append(todoPaths, todoPath{state: here, va: va})

	for len(todoPaths) > 0 {
		path := todoPaths[len(todoPaths)-1]
		todoPaths = todoPaths[1:]
		log.Printf("exploring path %s: va=0x%x", path.state, path.va)
		check(sman.RevertUntil(path.state))
		emu.SetInstructionPointer(path.va)

		for {
			if _, ok := hitVas[emu.GetInstructionPointer()]; ok {
				break
			}

			s, _, e := emu.FormatAddress(emu.GetInstructionPointer())
			check(e)
			color.Set(color.FgHiBlack)
			log.Printf("ip:" + s)
			color.Unset()

			insn, e := emu.GetCurrentInstruction()
			check(e)

			if w.DoesInstructionHaveGroup(insn, gapstone.X86_GRP_CALL) {
				// TODO: have to wire up import detection
				nextPc, e := GetNextInstructionPointer(emu, sman)
				if e == nil {
					log.Printf("  call target: 0x%x", nextPc)
				}

				dora.ac.AddCallXref(CallCrossReference{emu.GetInstructionPointer(), nextPc})
			}

			if w.DoesInstructionHaveGroup(insn, gapstone.X86_GRP_RET) ||
				w.DoesInstructionHaveGroup(insn, gapstone.X86_GRP_IRET) {
				log.Printf("returning, done.")
				afterSp := emu.GetStackPointer()
				stackDelta := uint64(afterSp) - uint64(beforeSp)
				log.Printf("stack delta: 0x%x", stackDelta)
				break
			}

			if isBBEnd(insn) {
				e := dora.ac.AddBasicBlock(BasicBlock{Start: bbStart, End: emu.GetInstructionPointer()})
				check(e)
			}

			beforePc := emu.GetInstructionPointer()
			if IsConditionalJump(insn) {
				targets, e := GetJumpTargets(emu, insn)
				check(e)
				if len(targets) < 2 {
					// TODO: by definition, a conditional jump should have at least two cases...
					panic("len(targets) < 2")
				}

				here, e := sman.Push()
				check(e)

				nextPc := targets[0]
				for _, target := range targets[1:] {
					log.Printf("other target: 0x%x", target)
					todoPaths = append(todoPaths, todoPath{state: here, va: target})
				}
				emu.SetInstructionPointer(nextPc)

			} else if w.DoesInstructionHaveGroup(insn, gapstone.X86_GRP_CALL) {
				nextPc := w.VA(uint64(emu.GetInstructionPointer()) + uint64(insn.Size))
				emu.SetInstructionPointer(nextPc)

				// TODO: need to detect calling convention, and in the case of stdcall,
				//   cleanup the stack

			} else {
				e = emu.StepOver()
				if e != nil {
					log.Printf("error: %s", e.Error())
					break
				}
			}

			hitVas[beforePc] = true

			afterPc := emu.GetInstructionPointer()
			if isBBEnd(insn) {
				bbStart = emu.GetInstructionPointer()
				e := dora.ac.AddJumpXref(JumpCrossReference{beforePc, afterPc})
				check(e)
			}
		}
	}

	return nil
}
<commit_msg>dora: add doc/TODOs for main loop refactoring<commit_after>package dora

import (
	"github.com/bnagy/gapstone"
	"github.com/fatih/color"
	w "github.com/williballenthin/Lancelot/workspace"
	"log"
)

func check(e error) {
	if e != nil {
		panic(e)
	}
}

// dora the explora
type Dora struct {
	ws *w.Workspace
	ac ArtifactCollection
}

func New(ws *w.Workspace) (*Dora, error) {
	// TODO: get this from a real place
	ac, e := NewLoggingArtifactCollection()
	check(e)

	return &Dora{
		ws: ws,
		ac: ac,
	}, nil
}

func isBBEnd(insn gapstone.Instruction) bool {
	return w.DoesInstructionHaveGroup(insn, gapstone.X86_GRP_CALL) ||
		w.DoesInstructionHaveGroup(insn, gapstone.X86_GRP_JUMP) ||
		w.DoesInstructionHaveGroup(insn, gapstone.X86_GRP_RET) ||
		w.DoesInstructionHaveGroup(insn, gapstone.X86_GRP_IRET)
}

func GetNextInstructionPointer(emu *w.Emulator, sman *w.SnapshotManager) (w.VA, error) {
	var va w.VA
	e := sman.WithTempExcursion(func() error {
		e := emu.StepInto()
		if e != nil {
			return e
		}
		va = emu.GetInstructionPointer()
		return nil
	})
	return va, e
}

type todoPath struct {
	state w.SnapshotManagerCookie
	va    w.VA
}

func IsConditionalJump(insn gapstone.Instruction) bool {
	if w.DoesInstructionHaveGroup(insn, gapstone.X86_GRP_JUMP) && insn.Mnemonic != "jmp" {
		return true
	}
	if w.DoesInstructionHaveGroup(insn, gapstone.X86_GRP_JUMP) && insn.Mnemonic == "jmp" {
		if insn.Mnemonic == "jmp" && insn.X86.Operands[0].Type == gapstone.X86_OP_IMM {
			// jmp 0x1000
			return false
		} else {
			// jmp eax
			return true
		}
	}
	return false
}

func GetJumpTargets(emu *w.Emulator, insn gapstone.Instruction) ([]w.VA, error) {
	var ret []w.VA

	if w.DoesInstructionHaveGroup(insn, gapstone.X86_GRP_JUMP) && insn.Mnemonic == "jmp" {
		if insn.X86.Operands[0].Type == gapstone.X86_OP_IMM {
			// not a conditional jump???
			return ret, w.InvalidArgumentError
		}
		// jmp eax
		// don't know how to handle this case right now
		return ret, nil
	}

	// assume a two case situation
	falsePc := w.VA(uint64(emu.GetInstructionPointer()) + uint64(insn.Size))
	truePc := w.VA(insn.X86.Operands[0].Imm) // or .Mem???

	if truePc == 0 {
		// TODO
		panic("zero jump")
	}

	ret = append(ret, truePc, falsePc)
	return ret, nil
}

// things yet to discover:
//   OK: final stack delta
//   TODO: arguments passed in registers
//     insn.cs_detail.regs_read/regs_write
//   TODO: arguments passed on stack
//   OK: all basic blocks
//   TODO: calling convention
//   TODO: no return functions
// TODO: ensure stack is set up with return pointer and some junk symbolic args
// TODO: track max hits
// this is going to be a pretty wild function :-(
func (dora *Dora) ExploreFunction(va w.VA) error {
	emu, e := dora.ws.GetEmulator()
	check(e)
	defer emu.Close()

	sman, e := w.NewSnapshotManager(emu)
	check(e)
	defer sman.Close()

	bbStart := va
	emu.SetInstructionPointer(va)
	check(e)

	beforeSp := emu.GetStackPointer()

	// TODO: how to disable these while on an excursion?
	rh, e := emu.HookMemRead(func(access int, addr w.VA, size int, value int64) {
		dora.ac.AddMemoryReadXref(MemoryReadCrossReference{emu.GetInstructionPointer(), addr})
	})
	check(e)
	defer rh.Close()

	wh, e := emu.HookMemWrite(func(access int, addr w.VA, size int, value int64) {
		dora.ac.AddMemoryWriteXref(MemoryWriteCrossReference{emu.GetInstructionPointer(), addr})
	})
	check(e)
	defer wh.Close()

	hitVas := make(map[w.VA]bool)

	var todoPaths = []todoPath{}
	here, e := sman.GetCurrentCookie()
	check(e)
	todoPaths = append(todoPaths, todoPath{state: here, va: va})

	for len(todoPaths) > 0 {
		path := todoPaths[len(todoPaths)-1]
		todoPaths = todoPaths[1:]
		log.Printf("exploring path %s: va=0x%x", path.state, path.va)
		check(sman.RevertUntil(path.state))
		emu.SetInstructionPointer(path.va)

		for {
			// TODO: dora.checkVisitedVas()
			if _, ok := hitVas[emu.GetInstructionPointer()]; ok {
				break
			}

			// TODO: dora.tracePc()
			s, _, e := emu.FormatAddress(emu.GetInstructionPointer())
			check(e)
			color.Set(color.FgHiBlack)
			log.Printf("ip:" + s)
			color.Unset()

			insn, e := emu.GetCurrentInstruction()
			check(e)

			if isBBEnd(insn) {
				e := dora.ac.AddBasicBlock(BasicBlock{Start: bbStart, End: emu.GetInstructionPointer()})
				check(e)
			}

			beforePc := emu.GetInstructionPointer()
			// TODO: dora.handleRet()
			// TODO: dora.handleConditionalJump()
			// TODO: dora.handleJump()
			// TODO: dora.handleCall()
			// TODO: dora.handleStep()
			if w.DoesInstructionHaveGroup(insn, gapstone.X86_GRP_RET) ||
				w.DoesInstructionHaveGroup(insn, gapstone.X86_GRP_IRET) {
				log.Printf("returning, done.")
				afterSp := emu.GetStackPointer()
				stackDelta := uint64(afterSp) - uint64(beforeSp)
				log.Printf("stack delta: 0x%x", stackDelta)
				break
			} else if IsConditionalJump(insn) {
				targets, e := GetJumpTargets(emu, insn)
				check(e)
				if len(targets) < 2 {
					// TODO: by definition, a conditional jump should have at least two cases...
					panic("len(targets) < 2")
				}

				here, e := sman.Push()
				check(e)

				nextPc := targets[0]
				for _, target := range targets[1:] {
					log.Printf("other target: 0x%x", target)
					todoPaths = append(todoPaths, todoPath{state: here, va: target})
				}
				emu.SetInstructionPointer(nextPc)

			} else if w.DoesInstructionHaveGroup(insn, gapstone.X86_GRP_CALL) {
				// TODO: try to resolve imports before blindly emulating
				callPc, e := GetNextInstructionPointer(emu, sman)
				if e == nil {
					log.Printf("  call target: 0x%x", callPc)
				}
				dora.ac.AddCallXref(CallCrossReference{emu.GetInstructionPointer(), callPc})

				nextPc := w.VA(uint64(emu.GetInstructionPointer()) + uint64(insn.Size))
				emu.SetInstructionPointer(nextPc)

				// TODO: need to detect calling convention, and in the case of stdcall,
				//   cleanup the stack

			} else {
				e = emu.StepOver()
				if e != nil {
					log.Printf("error: %s", e.Error())
					break
				}
			}

			// TODO: fetch stack read/write set for arg/variable detection
			// TODO: fetch reg read/write set for arg detection

			// TODO: dora.updateVisitedVas()
			hitVas[beforePc] = true

			afterPc := emu.GetInstructionPointer()
			if isBBEnd(insn) {
				bbStart = emu.GetInstructionPointer()
				e := dora.ac.AddJumpXref(JumpCrossReference{beforePc, afterPc})
				check(e)
			}
		}
	}

	return nil
}
<|endoftext|>


<commit_before>package main

import (
	"os"

	"github.com/codegangsta/cli"
	"github.com/fsouza/go-dockerclient"
	"github.com/litl/galaxy/log"
	"github.com/litl/galaxy/registry"
	"github.com/litl/galaxy/runtime"
	"github.com/litl/galaxy/utils"
)

const (
	ETCD_ENTRY_ALREADY_EXISTS = 105
)

var (
	client          *docker.Client
	serviceRegistry *registry.ServiceRegistry
	outputBuffer    *utils.OutputBuffer
	buildVersion    string
)

func initOrDie(c *cli.Context) {
	var err error
	endpoint := runtime.GetEndpoint()
	client, err = docker.NewClient(endpoint)

	if err != nil {
		panic(err)
	}

	serviceRegistry = registry.NewServiceRegistry(
		utils.GalaxyEnv(c),
		utils.GalaxyPool(c),
		c.GlobalString("hostIp"),
		uint64(c.Int("ttl")),
		c.GlobalString("sshAddr"),
	)

	serviceRegistry.Connect(utils.GalaxyRedisHost(c))

	outputBuffer = &utils.OutputBuffer{}
	serviceRegistry.OutputBuffer = outputBuffer

	// Don't log timestamps, etc. if running interactively
	if !c.Bool("loop") {
		log.DefaultLogger.SetFlags(0)
	}
}

func main() {

	app := cli.NewApp()
	app.Name = "discovery"
	app.Usage = "discovery service registration"
	app.Version = buildVersion
	app.Flags = []cli.Flag{
		cli.StringFlag{Name: "redis", Value: utils.DefaultRedisHost, Usage: "host:port[,host:port,..]"},
		cli.StringFlag{Name: "env", Value: utils.DefaultEnv, Usage: "environment (dev, test, prod, etc.)"},
		cli.StringFlag{Name: "pool", Value: utils.DefaultPool, Usage: "pool (web, worker, etc.)"},
		cli.StringFlag{Name: "hostIp", Value: "127.0.0.1", Usage: "hosts external IP"},
		cli.StringFlag{Name: "sshAddr", Value: "127.0.0.1:22", Usage: "hosts external ssh IP:port"},
		cli.StringFlag{Name: "shuttleAddr", Value: "127.0.0.1:9090", Usage: "shuttle http address"},
	}

	app.Commands = []cli.Command{
		{
			Name:        "register",
			Usage:       "discovers and registers running containers",
			Action:      register,
			Description: "register [options]",
			Flags: []cli.Flag{
				cli.IntFlag{Name: "ttl", Value: 60, Usage: "TTL (s) for service registrations"},
				cli.BoolFlag{Name: "loop", Usage: "Continuously register containers"},
			},
		},
		{
			Name:        "unregister",
			Usage:       "discovers and unregisters running containers",
			Action:      unregister,
			Description: "unregister [options]",
		},
		{
			Name:        "status",
			Usage:       "Lists the registration status of running containers",
			Action:      status,
			Description: "status",
		},
	}

	app.Run(os.Args)
}
<commit_msg>Don't start discovery if missing env or pool<commit_after>package main

import (
	"os"

	"github.com/codegangsta/cli"
	"github.com/fsouza/go-dockerclient"
	"github.com/litl/galaxy/log"
	"github.com/litl/galaxy/registry"
	"github.com/litl/galaxy/runtime"
	"github.com/litl/galaxy/utils"
)

const (
	ETCD_ENTRY_ALREADY_EXISTS = 105
)

var (
	client          *docker.Client
	serviceRegistry *registry.ServiceRegistry
	outputBuffer    *utils.OutputBuffer
	buildVersion    string
)

func initOrDie(c *cli.Context) {
	var err error
	endpoint := runtime.GetEndpoint()
	client, err = docker.NewClient(endpoint)

	if err != nil {
		log.Fatalf("ERROR: %s", err)
	}

	if utils.GalaxyEnv(c) == "" {
		log.Fatalln("ERROR: env not set.  Set GALAXY_ENV or pass -env.")
	}

	if utils.GalaxyPool(c) == "" {
		log.Fatalln("ERROR: pool not set.  Set GALAXY_POOL or pass -pool.")
	}

	serviceRegistry = registry.NewServiceRegistry(
		utils.GalaxyEnv(c),
		utils.GalaxyPool(c),
		c.GlobalString("hostIp"),
		uint64(c.Int("ttl")),
		c.GlobalString("sshAddr"),
	)

	serviceRegistry.Connect(utils.GalaxyRedisHost(c))

	outputBuffer = &utils.OutputBuffer{}
	serviceRegistry.OutputBuffer = outputBuffer

	// Don't log timestamps, etc. if running interactively
	if !c.Bool("loop") {
		log.DefaultLogger.SetFlags(0)
	}
}

func main() {

	app := cli.NewApp()
	app.Name = "discovery"
	app.Usage = "discovery service registration"
	app.Version = buildVersion
	app.Flags = []cli.Flag{
		cli.StringFlag{Name: "redis", Value: utils.DefaultRedisHost, Usage: "host:port[,host:port,..]"},
		cli.StringFlag{Name: "env", Value: utils.DefaultEnv, Usage: "environment (dev, test, prod, etc.)"},
		cli.StringFlag{Name: "pool", Value: utils.DefaultPool, Usage: "pool (web, worker, etc.)"},
		cli.StringFlag{Name: "hostIp", Value: "127.0.0.1", Usage: "hosts external IP"},
		cli.StringFlag{Name: "sshAddr", Value: "127.0.0.1:22", Usage: "hosts external ssh IP:port"},
		cli.StringFlag{Name: "shuttleAddr", Value: "127.0.0.1:9090", Usage: "shuttle http address"},
	}

	app.Commands = []cli.Command{
		{
			Name:        "register",
			Usage:       "discovers and registers running containers",
			Action:      register,
			Description: "register [options]",
			Flags: []cli.Flag{
				cli.IntFlag{Name: "ttl", Value: 60, Usage: "TTL (s) for service registrations"},
				cli.BoolFlag{Name: "loop", Usage: "Continuously register containers"},
			},
		},
		{
			Name:        "unregister",
			Usage:       "discovers and unregisters running containers",
			Action:      unregister,
			Description: "unregister [options]",
		},
		{
			Name:        "status",
			Usage:       "Lists the registration status of running containers",
			Action:      status,
			Description: "status",
		},
	}

	app.Run(os.Args)
}
<|endoftext|>


<commit_before>//go:build !linux || !cgo
// +build !linux !cgo

package nsenter
<commit_msg>Refuse to build runc without nsenter<commit_after><|endoftext|>


<commit_before>package xpath

import (
	"errors"
	"fmt"
)

type flag int

const (
	noneFlag flag = iota
	filterFlag
)

// builder provides building an XPath expressions.
type builder struct {
	depth      int
	flag       flag
	firstInput query
}

// axisPredicate creates a predicate to predicating for this axis node.
func axisPredicate(root *axisNode) func(NodeNavigator) bool {
	// get current axix node type.
	typ := ElementNode
	switch root.AxeType {
	case "attribute":
		typ = AttributeNode
	case "self", "parent":
		typ = allNode
	default:
		switch root.Prop {
		case "comment":
			typ = CommentNode
		case "text":
			typ = TextNode
			//	case "processing-instruction":
		//	typ = ProcessingInstructionNode
		case "node":
			typ = allNode
		}
	}
	predicate := func(n NodeNavigator) bool {
		if typ == n.NodeType() || typ == allNode || typ == TextNode {
			if root.LocalName == "" || (root.LocalName == n.LocalName() && (root.Prefix=="" || root.Prefix == n.Prefix())) {
				return true
			}
		}
		return false
	}

	return predicate
}

// processAxisNode processes a query for the XPath axis node.
func (b *builder) processAxisNode(root *axisNode) (query, error) {
	var (
		err       error
		qyInput   query
		qyOutput  query
		predicate = axisPredicate(root)
	)

	if root.Input == nil {
		qyInput = &contextQuery{}
	} else {
		if root.AxeType == "child" && (root.Input.Type() == nodeAxis) {
			if input := root.Input.(*axisNode); input.AxeType == "descendant-or-self" {
				var qyGrandInput query
				if input.Input != nil {
					qyGrandInput, _ = b.processNode(input.Input)
				} else {
					qyGrandInput = &contextQuery{}
				}
				qyOutput = &descendantQuery{Input: qyGrandInput, Predicate: predicate, Self: true}
				return qyOutput, nil
			}
		}
		qyInput, err = b.processNode(root.Input)
		if err != nil {
			return nil, err
		}
	}

	switch root.AxeType {
	case "ancestor":
		qyOutput = &ancestorQuery{Input: qyInput, Predicate: predicate}
	case "ancestor-or-self":
		qyOutput = &ancestorQuery{Input: qyInput, Predicate: predicate, Self: true}
	case "attribute":
		qyOutput = &attributeQuery{Input: qyInput, Predicate: predicate}
	case "child":
		filter := func(n NodeNavigator) bool {
			v := predicate(n)
			switch root.Prop {
			case "text":
				v = v && n.NodeType() == TextNode
			case "node":
				v = v && (n.NodeType() == ElementNode || n.NodeType() == TextNode)
			case "comment":
				v = v && n.NodeType() == CommentNode
			}
			return v
		}
		qyOutput = &childQuery{Input: qyInput, Predicate: filter}
	case "descendant":
		qyOutput = &descendantQuery{Input: qyInput, Predicate: predicate}
	case "descendant-or-self":
		qyOutput = &descendantQuery{Input: qyInput, Predicate: predicate, Self: true}
	case "following":
		qyOutput = &followingQuery{Input: qyInput, Predicate: predicate}
	case "following-sibling":
		qyOutput = &followingQuery{Input: qyInput, Predicate: predicate, Sibling: true}
	case "parent":
		qyOutput = &parentQuery{Input: qyInput, Predicate: predicate}
	case "preceding":
		qyOutput = &precedingQuery{Input: qyInput, Predicate: predicate}
	case "preceding-sibling":
		qyOutput = &precedingQuery{Input: qyInput, Predicate: predicate, Sibling: true}
	case "self":
		qyOutput = &selfQuery{Input: qyInput, Predicate: predicate}
	case "namespace":
		// haha,what will you do someting??
	default:
		err = fmt.Errorf("unknown axe type: %s", root.AxeType)
		return nil, err
	}
	return qyOutput, nil
}

// processFilterNode builds query for the XPath filter predicate.
func (b *builder) processFilterNode(root *filterNode) (query, error) {
	b.flag |= filterFlag

	qyInput, err := b.processNode(root.Input)
	if err != nil {
		return nil, err
	}
	qyCond, err := b.processNode(root.Condition)
	if err != nil {
		return nil, err
	}
	qyOutput := &filterQuery{Input: qyInput, Predicate: qyCond}
	return qyOutput, nil
}

// processFunctionNode processes query for the XPath function node.
func (b *builder) processFunctionNode(root *functionNode) (query, error) {
	var qyOutput query
	switch root.FuncName {
	case "starts-with":
		arg1, err := b.processNode(root.Args[0])
		if err != nil {
			return nil, err
		}
		arg2, err := b.processNode(root.Args[1])
		if err != nil {
			return nil, err
		}
		qyOutput = &functionQuery{Input: b.firstInput, Func: startwithFunc(arg1, arg2)}
	case "contains":
		arg1, err := b.processNode(root.Args[0])
		if err != nil {
			return nil, err
		}
		arg2, err := b.processNode(root.Args[1])
		if err != nil {
			return nil, err
		}

		qyOutput = &functionQuery{Input: b.firstInput, Func: containsFunc(arg1, arg2)}
	case "substring":
		//substring( string , start [, length] )
		if len(root.Args) < 2 {
			return nil, errors.New("xpath: substring function must have at least two parameter")
		}
		var (
			arg1, arg2, arg3 query
			err              error
		)
		if arg1, err = b.processNode(root.Args[0]); err != nil {
			return nil, err
		}
		if arg2, err = b.processNode(root.Args[1]); err != nil {
			return nil, err
		}
		if len(root.Args) == 3 {
			if arg3, err = b.processNode(root.Args[2]); err != nil {
				return nil, err
			}
		}
		qyOutput = &functionQuery{Input: b.firstInput, Func: substringFunc(arg1, arg2, arg3)}
	case "string-length":
		// string-length( [string] )
		if len(root.Args) < 1 {
			return nil, errors.New("xpath: string-length function must have at least one parameter")
		}
		arg1, err := b.processNode(root.Args[0])
		if err != nil {
			return nil, err
		}
		qyOutput = &functionQuery{Input: b.firstInput, Func: stringLengthFunc(arg1)}
	case "normalize-space":
		if len(root.Args) == 0 {
			return nil, errors.New("xpath: normalize-space function must have at least one parameter")
		}
		argQuery, err := b.processNode(root.Args[0])
		if err != nil {
			return nil, err
		}
		qyOutput = &functionQuery{Input: argQuery, Func: normalizespaceFunc}
	case "not":
		if len(root.Args) == 0 {
			return nil, errors.New("xpath: not function must have at least one parameter")
		}
		argQuery, err := b.processNode(root.Args[0])
		if err != nil {
			return nil, err
		}
		qyOutput = &functionQuery{Input: argQuery, Func: notFunc}
	case "name":
		qyOutput = &functionQuery{Input: b.firstInput, Func: nameFunc}
	case "last":
		qyOutput = &functionQuery{Input: b.firstInput, Func: lastFunc}
	case "position":
		qyOutput = &functionQuery{Input: b.firstInput, Func: positionFunc}
	case "count":
		//if b.firstInput == nil {
		//	return nil, errors.New("xpath: expression must evaluate to node-set")
		//}
		if len(root.Args) == 0 {
			return nil, fmt.Errorf("xpath: count(node-sets) function must with have parameters node-sets")
		}
		argQuery, err := b.processNode(root.Args[0])
		if err != nil {
			return nil, err
		}
		qyOutput = &functionQuery{Input: argQuery, Func: countFunc}
	case "sum":
		if len(root.Args) == 0 {
			return nil, fmt.Errorf("xpath: sum(node-sets) function must with have parameters node-sets")
		}
		argQuery, err := b.processNode(root.Args[0])
		if err != nil {
			return nil, err
		}
		qyOutput = &functionQuery{Input: argQuery, Func: sumFunc}
	case "concat":
		if len(root.Args) < 2 {
			return nil, fmt.Errorf("xpath: concat() must have at least two arguments")
		}
		var args []query
		for _, v := range root.Args {
			q, err := b.processNode(v)
			if err != nil {
				return nil, err
			}
			args = append(args, q)
		}
		qyOutput = &functionQuery{Input: b.firstInput, Func: concatFunc(args...)}
	default:
		return nil, fmt.Errorf("not yet support this function %s()", root.FuncName)
	}
	return qyOutput, nil
}

func (b *builder) processOperatorNode(root *operatorNode) (query, error) {
	left, err := b.processNode(root.Left)
	if err != nil {
		return nil, err
	}
	right, err := b.processNode(root.Right)
	if err != nil {
		return nil, err
	}
	var qyOutput query
	switch root.Op {
	case "+", "-", "div", "mod": // Numeric operator
		var exprFunc func(interface{}, interface{}) interface{}
		switch root.Op {
		case "+":
			exprFunc = plusFunc
		case "-":
			exprFunc = minusFunc
		case "div":
			exprFunc = divFunc
		case "mod":
			exprFunc = modFunc
		}
		qyOutput = &numericQuery{Left: left, Right: right, Do: exprFunc}
	case "=", ">", ">=", "<", "<=", "!=":
		var exprFunc func(iterator, interface{}, interface{}) interface{}
		switch root.Op {
		case "=":
			exprFunc = eqFunc
		case ">":
			exprFunc = gtFunc
		case ">=":
			exprFunc = geFunc
		case "<":
			exprFunc = ltFunc
		case "<=":
			exprFunc = leFunc
		case "!=":
			exprFunc = neFunc
		}
		qyOutput = &logicalQuery{Left: left, Right: right, Do: exprFunc}
	case "or", "and":
		isOr := false
		if root.Op == "or" {
			isOr = true
		}
		qyOutput = &booleanQuery{Left: left, Right: right, IsOr: isOr}
	case "|":
		qyOutput = &unionQuery{Left: left, Right: right}
	}
	return qyOutput, nil
}

func (b *builder) processNode(root node) (q query, err error) {
	if b.depth = b.depth + 1; b.depth > 1024 {
		err = errors.New("the xpath expressions is too complex")
		return
	}

	switch root.Type() {
	case nodeConstantOperand:
		n := root.(*operandNode)
		q = &constantQuery{Val: n.Val}
	case nodeRoot:
		q = &contextQuery{Root: true}
	case nodeAxis:
		q, err = b.processAxisNode(root.(*axisNode))
		b.firstInput = q
	case nodeFilter:
		q, err = b.processFilterNode(root.(*filterNode))
	case nodeFunction:
		q, err = b.processFunctionNode(root.(*functionNode))
	case nodeOperator:
		q, err = b.processOperatorNode(root.(*operatorNode))
	}
	return
}

// build builds a specified XPath expressions expr.
func build(expr string) (q query, err error) {
	defer func() {
		if e := recover(); e != nil {
			switch x := e.(type) {
			case string:
				err = errors.New(x)
			case error:
				err = x
			default:
				err = errors.New("unknown panic")
			}
		}
	}()
	root := parse(expr)
	b := &builder{}
	return b.processNode(root)
}
<commit_msg>fix predicate https://github.com/antchfx/xmlquery/issues/1<commit_after>package xpath

import (
	"errors"
	"fmt"
)

type flag int

const (
	noneFlag flag = iota
	filterFlag
)

// builder provides building an XPath expressions.
type builder struct {
	depth      int
	flag       flag
	firstInput query
}

// axisPredicate creates a predicate to predicating for this axis node.
func axisPredicate(root *axisNode) func(NodeNavigator) bool {
	// get current axix node type.
	typ := ElementNode
	switch root.AxeType {
	case "attribute":
		typ = AttributeNode
	case "self", "parent":
		typ = allNode
	default:
		switch root.Prop {
		case "comment":
			typ = CommentNode
		case "text":
			typ = TextNode
			//	case "processing-instruction":
		//	typ = ProcessingInstructionNode
		case "node":
			typ = allNode
		}
	}
	nametest := root.LocalName != "" || root.Prefix != ""
	predicate := func(n NodeNavigator) bool {
		if typ == n.NodeType() || typ == allNode || typ == TextNode {
			if nametest {
				if root.LocalName == n.LocalName() && root.Prefix == n.Prefix() {
					return true
				}
			} else {
				return true
			}
		}
		return false
	}

	return predicate
}

// processAxisNode processes a query for the XPath axis node.
func (b *builder) processAxisNode(root *axisNode) (query, error) {
	var (
		err       error
		qyInput   query
		qyOutput  query
		predicate = axisPredicate(root)
	)

	if root.Input == nil {
		qyInput = &contextQuery{}
	} else {
		if root.AxeType == "child" && (root.Input.Type() == nodeAxis) {
			if input := root.Input.(*axisNode); input.AxeType == "descendant-or-self" {
				var qyGrandInput query
				if input.Input != nil {
					qyGrandInput, _ = b.processNode(input.Input)
				} else {
					qyGrandInput = &contextQuery{}
				}
				qyOutput = &descendantQuery{Input: qyGrandInput, Predicate: predicate, Self: true}
				return qyOutput, nil
			}
		}
		qyInput, err = b.processNode(root.Input)
		if err != nil {
			return nil, err
		}
	}

	switch root.AxeType {
	case "ancestor":
		qyOutput = &ancestorQuery{Input: qyInput, Predicate: predicate}
	case "ancestor-or-self":
		qyOutput = &ancestorQuery{Input: qyInput, Predicate: predicate, Self: true}
	case "attribute":
		qyOutput = &attributeQuery{Input: qyInput, Predicate: predicate}
	case "child":
		filter := func(n NodeNavigator) bool {
			v := predicate(n)
			switch root.Prop {
			case "text":
				v = v && n.NodeType() == TextNode
			case "node":
				v = v && (n.NodeType() == ElementNode || n.NodeType() == TextNode)
			case "comment":
				v = v && n.NodeType() == CommentNode
			}
			return v
		}
		qyOutput = &childQuery{Input: qyInput, Predicate: filter}
	case "descendant":
		qyOutput = &descendantQuery{Input: qyInput, Predicate: predicate}
	case "descendant-or-self":
		qyOutput = &descendantQuery{Input: qyInput, Predicate: predicate, Self: true}
	case "following":
		qyOutput = &followingQuery{Input: qyInput, Predicate: predicate}
	case "following-sibling":
		qyOutput = &followingQuery{Input: qyInput, Predicate: predicate, Sibling: true}
	case "parent":
		qyOutput = &parentQuery{Input: qyInput, Predicate: predicate}
	case "preceding":
		qyOutput = &precedingQuery{Input: qyInput, Predicate: predicate}
	case "preceding-sibling":
		qyOutput = &precedingQuery{Input: qyInput, Predicate: predicate, Sibling: true}
	case "self":
		qyOutput = &selfQuery{Input: qyInput, Predicate: predicate}
	case "namespace":
		// haha,what will you do someting??
	default:
		err = fmt.Errorf("unknown axe type: %s", root.AxeType)
		return nil, err
	}
	return qyOutput, nil
}

// processFilterNode builds query for the XPath filter predicate.
func (b *builder) processFilterNode(root *filterNode) (query, error) {
	b.flag |= filterFlag

	qyInput, err := b.processNode(root.Input)
	if err != nil {
		return nil, err
	}
	qyCond, err := b.processNode(root.Condition)
	if err != nil {
		return nil, err
	}
	qyOutput := &filterQuery{Input: qyInput, Predicate: qyCond}
	return qyOutput, nil
}

// processFunctionNode processes query for the XPath function node.
func (b *builder) processFunctionNode(root *functionNode) (query, error) {
	var qyOutput query
	switch root.FuncName {
	case "starts-with":
		arg1, err := b.processNode(root.Args[0])
		if err != nil {
			return nil, err
		}
		arg2, err := b.processNode(root.Args[1])
		if err != nil {
			return nil, err
		}
		qyOutput = &functionQuery{Input: b.firstInput, Func: startwithFunc(arg1, arg2)}
	case "contains":
		arg1, err := b.processNode(root.Args[0])
		if err != nil {
			return nil, err
		}
		arg2, err := b.processNode(root.Args[1])
		if err != nil {
			return nil, err
		}

		qyOutput = &functionQuery{Input: b.firstInput, Func: containsFunc(arg1, arg2)}
	case "substring":
		//substring( string , start [, length] )
		if len(root.Args) < 2 {
			return nil, errors.New("xpath: substring function must have at least two parameter")
		}
		var (
			arg1, arg2, arg3 query
			err              error
		)
		if arg1, err = b.processNode(root.Args[0]); err != nil {
			return nil, err
		}
		if arg2, err = b.processNode(root.Args[1]); err != nil {
			return nil, err
		}
		if len(root.Args) == 3 {
			if arg3, err = b.processNode(root.Args[2]); err != nil {
				return nil, err
			}
		}
		qyOutput = &functionQuery{Input: b.firstInput, Func: substringFunc(arg1, arg2, arg3)}
	case "string-length":
		// string-length( [string] )
		if len(root.Args) < 1 {
			return nil, errors.New("xpath: string-length function must have at least one parameter")
		}
		arg1, err := b.processNode(root.Args[0])
		if err != nil {
			return nil, err
		}
		qyOutput = &functionQuery{Input: b.firstInput, Func: stringLengthFunc(arg1)}
	case "normalize-space":
		if len(root.Args) == 0 {
			return nil, errors.New("xpath: normalize-space function must have at least one parameter")
		}
		argQuery, err := b.processNode(root.Args[0])
		if err != nil {
			return nil, err
		}
		qyOutput = &functionQuery{Input: argQuery, Func: normalizespaceFunc}
	case "not":
		if len(root.Args) == 0 {
			return nil, errors.New("xpath: not function must have at least one parameter")
		}
		argQuery, err := b.processNode(root.Args[0])
		if err != nil {
			return nil, err
		}
		qyOutput = &functionQuery{Input: argQuery, Func: notFunc}
	case "name":
		qyOutput = &functionQuery{Input: b.firstInput, Func: nameFunc}
	case "last":
		qyOutput = &functionQuery{Input: b.firstInput, Func: lastFunc}
	case "position":
		qyOutput = &functionQuery{Input: b.firstInput, Func: positionFunc}
	case "count":
		//if b.firstInput == nil {
		//	return nil, errors.New("xpath: expression must evaluate to node-set")
		//}
		if len(root.Args) == 0 {
			return nil, fmt.Errorf("xpath: count(node-sets) function must with have parameters node-sets")
		}
		argQuery, err := b.processNode(root.Args[0])
		if err != nil {
			return nil, err
		}
		qyOutput = &functionQuery{Input: argQuery, Func: countFunc}
	case "sum":
		if len(root.Args) == 0 {
			return nil, fmt.Errorf("xpath: sum(node-sets) function must with have parameters node-sets")
		}
		argQuery, err := b.processNode(root.Args[0])
		if err != nil {
			return nil, err
		}
		qyOutput = &functionQuery{Input: argQuery, Func: sumFunc}
	case "concat":
		if len(root.Args) < 2 {
			return nil, fmt.Errorf("xpath: concat() must have at least two arguments")
		}
		var args []query
		for _, v := range root.Args {
			q, err := b.processNode(v)
			if err != nil {
				return nil, err
			}
			args = append(args, q)
		}
		qyOutput = &functionQuery{Input: b.firstInput, Func: concatFunc(args...)}
	default:
		return nil, fmt.Errorf("not yet support this function %s()", root.FuncName)
	}
	return qyOutput, nil
}

func (b *builder) processOperatorNode(root *operatorNode) (query, error) {
	left, err := b.processNode(root.Left)
	if err != nil {
		return nil, err
	}
	right, err := b.processNode(root.Right)
	if err != nil {
		return nil, err
	}
	var qyOutput query
	switch root.Op {
	case "+", "-", "div", "mod": // Numeric operator
		var exprFunc func(interface{}, interface{}) interface{}
		switch root.Op {
		case "+":
			exprFunc = plusFunc
		case "-":
			exprFunc = minusFunc
		case "div":
			exprFunc = divFunc
		case "mod":
			exprFunc = modFunc
		}
		qyOutput = &numericQuery{Left: left, Right: right, Do: exprFunc}
	case "=", ">", ">=", "<", "<=", "!=":
		var exprFunc func(iterator, interface{}, interface{}) interface{}
		switch root.Op {
		case "=":
			exprFunc = eqFunc
		case ">":
			exprFunc = gtFunc
		case ">=":
			exprFunc = geFunc
		case "<":
			exprFunc = ltFunc
		case "<=":
			exprFunc = leFunc
		case "!=":
			exprFunc = neFunc
		}
		qyOutput = &logicalQuery{Left: left, Right: right, Do: exprFunc}
	case "or", "and":
		isOr := false
		if root.Op == "or" {
			isOr = true
		}
		qyOutput = &booleanQuery{Left: left, Right: right, IsOr: isOr}
	case "|":
		qyOutput = &unionQuery{Left: left, Right: right}
	}
	return qyOutput, nil
}

func (b *builder) processNode(root node) (q query, err error) {
	if b.depth = b.depth + 1; b.depth > 1024 {
		err = errors.New("the xpath expressions is too complex")
		return
	}

	switch root.Type() {
	case nodeConstantOperand:
		n := root.(*operandNode)
		q = &constantQuery{Val: n.Val}
	case nodeRoot:
		q = &contextQuery{Root: true}
	case nodeAxis:
		q, err = b.processAxisNode(root.(*axisNode))
		b.firstInput = q
	case nodeFilter:
		q, err = b.processFilterNode(root.(*filterNode))
	case nodeFunction:
		q, err = b.processFunctionNode(root.(*functionNode))
	case nodeOperator:
		q, err = b.processOperatorNode(root.(*operatorNode))
	}
	return
}

// build builds a specified XPath expressions expr.
func build(expr string) (q query, err error) {
	defer func() {
		if e := recover(); e != nil {
			switch x := e.(type) {
			case string:
				err = errors.New(x)
			case error:
				err = x
			default:
				err = errors.New("unknown panic")
			}
		}
	}()
	root := parse(expr)
	b := &builder{}
	return b.processNode(root)
}
<|endoftext|>


<commit_before>package cmdtest

import (
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
)

func Build(mainPath string, args ...string) (string, error) {
	return BuildIn(os.Getenv("GOPATH"), mainPath, args...)
}

func BuildIn(gopath string, mainPath string, args ...string) (string, error) {
	if len(gopath) == 0 {
		panic("$GOPATH not provided when building " + mainPath)
	}

	tmpdir, err := ioutil.TempDir("", "test_cmd_main")
	if err != nil {
		return "", err
	}

	executable := filepath.Join(tmpdir, filepath.Base(mainPath))

	cmdArgs := append([]string{"build"}, args...)
	cmdArgs = append(cmdArgs, "-o", executable, mainPath)

	build := exec.Command("go", cmdArgs...)
	build.Stdout = os.Stdout
	build.Stderr = os.Stderr
	build.Stdin = os.Stdin
	build.Env = []string{"GOPATH=" + gopath}

	err = build.Run()
	if err != nil {
		return "", err
	}

	return executable, nil
}
<commit_msg>Include existing env vars in Build<commit_after>package cmdtest

import (
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
)

func Build(mainPath string, args ...string) (string, error) {
	return BuildIn(os.Getenv("GOPATH"), mainPath, args...)
}

func BuildIn(gopath string, mainPath string, args ...string) (string, error) {
	if len(gopath) == 0 {
		panic("$GOPATH not provided when building " + mainPath)
	}

	tmpdir, err := ioutil.TempDir("", "test_cmd_main")
	if err != nil {
		return "", err
	}

	executable := filepath.Join(tmpdir, filepath.Base(mainPath))

	cmdArgs := append([]string{"build"}, args...)
	cmdArgs = append(cmdArgs, "-o", executable, mainPath)

	build := exec.Command("go", cmdArgs...)
	build.Stdout = os.Stdout
	build.Stderr = os.Stderr
	build.Stdin = os.Stdin
	build.Env = append(os.Environ(), "GOPATH="+gopath)

	err = build.Run()
	if err != nil {
		return "", err
	}

	return executable, nil
}
<|endoftext|>


<commit_before>package main

import (
	"bytes"
	"crypto/sha256"
	"encoding/hex"
	"encoding/json"
	"fmt"
	"io"
	"os"
	"path"
	"strings"

	"github.com/mitchellh/goamz/aws"
	"github.com/mitchellh/goamz/s3"
	"github.com/mitchellh/packer/common"
	"github.com/mitchellh/packer/helper/config"
	"github.com/mitchellh/packer/packer"
	"github.com/mitchellh/packer/template/interpolate"
)

type Config struct {
	Region       string `mapstructure:"region"`
	Bucket       string `mapstructure:"bucket"`
	ManifestPath string `mapstructure:"manifest"`
	BoxName      string `mapstructure:"box_name"`
	BoxDir       string `mapstructure:"box_dir"`
	Version      string `mapstructure:"version"`
	ACL          s3.ACL `mapstructure:"acl"`
	AccessKey    string `mapstructure:"access_key_id"`
	SecretKey    string `mapstructure:"secret_key"`
	common.PackerConfig    `mapstructure:",squash"`

	ctx          interpolate.Context
}

type PostProcessor struct {
	config Config
	s3     *s3.Bucket
}

func (p *PostProcessor) Configure(raws ...interface{}) error {
	err := config.Decode(&p.config, &config.DecodeOpts{
		Interpolate:        true,
		InterpolateContext: &p.config.ctx,
		InterpolateFilter: &interpolate.RenderFilter{
			Exclude: []string{"output"},
		},
	}, raws...)
	if err != nil {
		return err
	}

	errs := new(packer.MultiError)
	// required configuration
	templates := map[string]*string{
		"region":   &p.config.Region,
		"bucket":   &p.config.Bucket,
		"manifest": &p.config.ManifestPath,
		"box_name": &p.config.BoxName,
		"box_dir":  &p.config.BoxDir,
		"version":  &p.config.Version,
	}

	for key, ptr := range templates {
		if *ptr == "" {
			errs = packer.MultiErrorAppend(errs, fmt.Errorf("vagrant-s3 %s must be set", key))
		}
	}

	// Template process
	for key, ptr := range templates {
		if err = interpolate.Validate(*ptr, &p.config.ctx); err != nil {
			errs = packer.MultiErrorAppend(
				errs, fmt.Errorf("Error parsing %s template: %s", key, err))
		}
	}

	auth, err := aws.GetAuth(p.config.AccessKey, p.config.SecretKey)
	if err != nil {
		errs = packer.MultiErrorAppend(errs, fmt.Errorf("Unable to create Aws Authentication. Try providing keys 'access_key_id' and 'secret_key'"))
	}

	// determine region
	region, valid := aws.Regions[p.config.Region]
	if valid {
		p.s3 = s3.New(auth, region).Bucket(p.config.Bucket)
	} else {
		errs = packer.MultiErrorAppend(errs, fmt.Errorf("Invalid region specified: %s", p.config.Region))
	}

	if p.config.ACL == "" {
		p.config.ACL = "public-read"
	}

	if len(errs.Errors) > 0 {
		return errs
	}

	return nil
}

func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
	// Only accept input from the vagrant post-processor
	if artifact.BuilderId() != "mitchellh.post-processor.vagrant" {
		return nil, false, fmt.Errorf("Unknown artifact type, requires box from vagrant post-processor: %s", artifact.BuilderId())
	}

	// Assume there is only one .box file to upload
	box := artifact.Files()[0]
	if !strings.HasSuffix(box, ".box") {
		return nil, false, fmt.Errorf("Unknown files in artifact from vagrant post-processor: %s", artifact.Files())
	}

	provider := providerFromBuilderName(artifact.Id())
	ui.Say(fmt.Sprintf("Preparing to upload box for '%s' provider to S3 bucket '%s'", provider, p.config.Bucket))

	// open the box so we can upload to S3 and calculate checksum for the manifest
	file, err := os.Open(box)
	if err != nil {
		return nil, false, err
	}
	defer file.Close()

	// get the file's size
	info, err := file.Stat()
	if err != nil {
		return nil, false, err
	}
	size := info.Size()
	ui.Message(fmt.Sprintf("Box to upload: %s (%d bytes)", box, size))

	// get the latest manifest so we can add to it
	ui.Message("Fetching latest manifest")
	manifest, err := p.getManifest()
	if err != nil {
		return nil, false, err
	}

	// generate the path to store the box in S3
	boxPath := fmt.Sprintf("%s/%s/%s", p.config.BoxDir, p.config.Version, path.Base(box))

	ui.Message("Generating checksum")
	checksum, err := sum256(file)
	if err != nil {
		return nil, false, err
	}
	ui.Message(fmt.Sprintf("Checksum is %s", checksum))

	ui.Message(fmt.Sprintf("Adding %s %s box to manifest", provider, p.config.Version))
	if err := manifest.add(p.config.Version, &Provider{
		Name:         provider,
		Url:          p.s3.URL(boxPath),
		ChecksumType: "sha256",
		Checksum:     checksum,
	}); err != nil {
		return nil, false, err
	}

	// upload the box to S3 (rewinding as we already read the file to generate the checksum)
	ui.Message(fmt.Sprintf("Uploading box to S3: %s", boxPath))
	if _, err := file.Seek(0, 0); err != nil {
		return nil, false, err
	}
	if size > 100*1024*1024 {
		ui.Message("File size > 100MB. Initiating multipart upload")
		multi, err := p.s3.Multi(boxPath, "application/octet-stream", p.config.ACL)
		if err != nil {
			return nil, false, err
		}
		parts, err := multi.PutAll(file, 5*1024*1024)
		if err != nil {
			return nil, false, err
		}
		if err := multi.Complete(parts); err != nil {
			return nil, false, err
		}
	} else {
		if err := p.s3.PutReader(boxPath, file, size, "application/octet-stream", p.config.ACL); err != nil {
			return nil, false, err
		}
	}

	ui.Message(fmt.Sprintf("Uploading the manifest: %s", p.config.ManifestPath))
	if err := p.putManifest(manifest); err != nil {
		return nil, false, err
	}

	return &Artifact{p.s3.URL(p.config.ManifestPath)}, true, nil
}

func (p *PostProcessor) getManifest() (*Manifest, error) {
	body, err := p.s3.GetReader(p.config.ManifestPath)
	if err != nil {
		s3Err, ok := err.(*s3.Error);
		if  ok && s3Err.Message == "404 Not Found" {
			return &Manifest{Name: p.config.BoxName}, nil
		}
		return nil, err
	}
	defer body.Close()

	manifest := &Manifest{}
	if err := json.NewDecoder(body).Decode(manifest); err != nil {
		return nil, err
	}
	return manifest, nil
}

func (p *PostProcessor) putManifest(manifest *Manifest) error {
	var buf bytes.Buffer
	if err := json.NewEncoder(&buf).Encode(manifest); err != nil {
		return err
	}
	if err := p.s3.Put(p.config.ManifestPath, buf.Bytes(), "application/json", p.config.ACL); err != nil {
		return err
	}
	return nil
}

// calculates a sha256 checksum of the file
func sum256(file *os.File) (string, error) {
	h := sha256.New()
	if _, err := io.Copy(h, file); err != nil {
		return "", err
	}
	return hex.EncodeToString(h.Sum(nil)), nil
}

// converts a packer builder name to the corresponding vagrant provider
func providerFromBuilderName(name string) string {
	switch name {
	case "aws":
		return "aws"
	case "digitalocean":
		return "digitalocean"
	case "virtualbox":
		return "virtualbox"
	case "vmware":
		return "vmware_desktop"
	case "parallels":
		return "parallels"
	default:
		return name
	}
}
<commit_msg>Fixed the error with non-existent manifest.<commit_after>package main

import (
	"bytes"
	"crypto/sha256"
	"encoding/hex"
	"encoding/json"
	"fmt"
	"io"
	"os"
	"path"
	"strings"

	"github.com/mitchellh/goamz/aws"
	"github.com/mitchellh/goamz/s3"
	"github.com/mitchellh/packer/common"
	"github.com/mitchellh/packer/helper/config"
	"github.com/mitchellh/packer/packer"
	"github.com/mitchellh/packer/template/interpolate"
)

type Config struct {
	Region       string `mapstructure:"region"`
	Bucket       string `mapstructure:"bucket"`
	ManifestPath string `mapstructure:"manifest"`
	BoxName      string `mapstructure:"box_name"`
	BoxDir       string `mapstructure:"box_dir"`
	Version      string `mapstructure:"version"`
	ACL          s3.ACL `mapstructure:"acl"`
	AccessKey    string `mapstructure:"access_key_id"`
	SecretKey    string `mapstructure:"secret_key"`
	common.PackerConfig    `mapstructure:",squash"`

	ctx          interpolate.Context
}

type PostProcessor struct {
	config Config
	s3     *s3.Bucket
}

func (p *PostProcessor) Configure(raws ...interface{}) error {
	err := config.Decode(&p.config, &config.DecodeOpts{
		Interpolate:        true,
		InterpolateContext: &p.config.ctx,
		InterpolateFilter: &interpolate.RenderFilter{
			Exclude: []string{"output"},
		},
	}, raws...)
	if err != nil {
		return err
	}

	errs := new(packer.MultiError)
	// required configuration
	templates := map[string]*string{
		"region":   &p.config.Region,
		"bucket":   &p.config.Bucket,
		"manifest": &p.config.ManifestPath,
		"box_name": &p.config.BoxName,
		"box_dir":  &p.config.BoxDir,
		"version":  &p.config.Version,
	}

	for key, ptr := range templates {
		if *ptr == "" {
			errs = packer.MultiErrorAppend(errs, fmt.Errorf("vagrant-s3 %s must be set", key))
		}
	}

	// Template process
	for key, ptr := range templates {
		if err = interpolate.Validate(*ptr, &p.config.ctx); err != nil {
			errs = packer.MultiErrorAppend(
				errs, fmt.Errorf("Error parsing %s template: %s", key, err))
		}
	}

	auth, err := aws.GetAuth(p.config.AccessKey, p.config.SecretKey)
	if err != nil {
		errs = packer.MultiErrorAppend(errs, fmt.Errorf("Unable to create Aws Authentication. Try providing keys 'access_key_id' and 'secret_key'"))
	}

	// determine region
	region, valid := aws.Regions[p.config.Region]
	if valid {
		p.s3 = s3.New(auth, region).Bucket(p.config.Bucket)
	} else {
		errs = packer.MultiErrorAppend(errs, fmt.Errorf("Invalid region specified: %s", p.config.Region))
	}

	if p.config.ACL == "" {
		p.config.ACL = "public-read"
	}

	if len(errs.Errors) > 0 {
		return errs
	}

	return nil
}

func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (packer.Artifact, bool, error) {
	// Only accept input from the vagrant post-processor
	if artifact.BuilderId() != "mitchellh.post-processor.vagrant" {
		return nil, false, fmt.Errorf("Unknown artifact type, requires box from vagrant post-processor: %s", artifact.BuilderId())
	}

	// Assume there is only one .box file to upload
	box := artifact.Files()[0]
	if !strings.HasSuffix(box, ".box") {
		return nil, false, fmt.Errorf("Unknown files in artifact from vagrant post-processor: %s", artifact.Files())
	}

	provider := providerFromBuilderName(artifact.Id())
	ui.Say(fmt.Sprintf("Preparing to upload box for '%s' provider to S3 bucket '%s'", provider, p.config.Bucket))

	// open the box so we can upload to S3 and calculate checksum for the manifest
	file, err := os.Open(box)
	if err != nil {
		return nil, false, err
	}
	defer file.Close()

	// get the file's size
	info, err := file.Stat()
	if err != nil {
		return nil, false, err
	}
	size := info.Size()
	ui.Message(fmt.Sprintf("Box to upload: %s (%d bytes)", box, size))

	// get the latest manifest so we can add to it
	ui.Message("Fetching latest manifest")
	manifest, err := p.getManifest()
	if err != nil {
		return nil, false, err
	}

	// generate the path to store the box in S3
	boxPath := fmt.Sprintf("%s/%s/%s", p.config.BoxDir, p.config.Version, path.Base(box))

	ui.Message("Generating checksum")
	checksum, err := sum256(file)
	if err != nil {
		return nil, false, err
	}
	ui.Message(fmt.Sprintf("Checksum is %s", checksum))

	ui.Message(fmt.Sprintf("Adding %s %s box to manifest", provider, p.config.Version))
	if err := manifest.add(p.config.Version, &Provider{
		Name:         provider,
		Url:          p.s3.URL(boxPath),
		ChecksumType: "sha256",
		Checksum:     checksum,
	}); err != nil {
		return nil, false, err
	}

	// upload the box to S3 (rewinding as we already read the file to generate the checksum)
	ui.Message(fmt.Sprintf("Uploading box to S3: %s", boxPath))
	if _, err := file.Seek(0, 0); err != nil {
		return nil, false, err
	}
	if size > 100*1024*1024 {
		ui.Message("File size > 100MB. Initiating multipart upload")
		multi, err := p.s3.Multi(boxPath, "application/octet-stream", p.config.ACL)
		if err != nil {
			return nil, false, err
		}
		parts, err := multi.PutAll(file, 5*1024*1024)
		if err != nil {
			return nil, false, err
		}
		if err := multi.Complete(parts); err != nil {
			return nil, false, err
		}
	} else {
		if err := p.s3.PutReader(boxPath, file, size, "application/octet-stream", p.config.ACL); err != nil {
			return nil, false, err
		}
	}

	ui.Message(fmt.Sprintf("Uploading the manifest: %s", p.config.ManifestPath))
	if err := p.putManifest(manifest); err != nil {
		return nil, false, err
	}

	return &Artifact{p.s3.URL(p.config.ManifestPath)}, true, nil
}

func (p *PostProcessor) getManifest() (*Manifest, error) {
	objExists, err := p.s3.Exists(p.config.ManifestPath)
	if !objExists {
		return &Manifest{Name: p.config.BoxName}, nil
	}

	body, err := p.s3.GetReader(p.config.ManifestPath)
	if err != nil {
		return nil, err
	}
	defer body.Close()

	manifest := &Manifest{}
	if err := json.NewDecoder(body).Decode(manifest); err != nil {
		return nil, err
	}
	return manifest, nil
}

func (p *PostProcessor) putManifest(manifest *Manifest) error {
	var buf bytes.Buffer
	if err := json.NewEncoder(&buf).Encode(manifest); err != nil {
		return err
	}
	if err := p.s3.Put(p.config.ManifestPath, buf.Bytes(), "application/json", p.config.ACL); err != nil {
		return err
	}
	return nil
}

// calculates a sha256 checksum of the file
func sum256(file *os.File) (string, error) {
	h := sha256.New()
	if _, err := io.Copy(h, file); err != nil {
		return "", err
	}
	return hex.EncodeToString(h.Sum(nil)), nil
}

// converts a packer builder name to the corresponding vagrant provider
func providerFromBuilderName(name string) string {
	switch name {
	case "aws":
		return "aws"
	case "digitalocean":
		return "digitalocean"
	case "virtualbox":
		return "virtualbox"
	case "vmware":
		return "vmware_desktop"
	case "parallels":
		return "parallels"
	default:
		return name
	}
}
<|endoftext|>


<commit_before>package predicate

<commit_msg>Added TestTrue<commit_after>package predicate

import "testing"

func TestTrue(t *testing.T) {
	if !True().P(nil) {
		t.Fail()
	}
}
<|endoftext|>


<commit_before>// Copyright 2014, Hǎiliàng Wáng. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package query

import (
	"regexp"

	. "h12.me/html-query/expr"
)

func (n *Node) Is(cs ...Checker) bool {
	return And(cs...)(n.InternalNode()) != nil
}

func (n *Node) Find(cs ...Checker) *Node {
	return NewNode(Find(cs...)(&n.n))
}

func (n *Node) FindChild(cs ...Checker) *Node {
	return NewNode(FindChild(cs...)(&n.n))
}

func (n *Node) find(c Checker, cs []Checker) *Node {
	if n == nil {
		return nil
	}
	return n.Find(append([]Checker{c}, cs...)...)
}

func (n *Node) NextSibling() *Node {
	if n == nil {
		return nil
	}
	return NewNode(NextSibling(&n.n))
}

func (n *Node) PrevSibling() *Node {
	if n == nil {
		return nil
	}
	return NewNode(PrevSibling(&n.n))
}

func (n *Node) Parent() *Node {
	if n == nil {
		return nil
	}
	return NewNode(Parent(&n.n))
}

func (n *Node) Children(cs ...Checker) NodeIter {
	if n == nil {
		return NodeIter{nil}
	}
	return NodeIter{Children(&n.n, cs...)}
}

func (n *Node) Descendants(cs ...Checker) NodeIter {
	if n == nil {
		return NodeIter{nil}
	}
	return NodeIter{Descendants(&n.n, cs...)}
}

func (n *Node) Ahref(cs ...Checker) *Node {
	if n == nil {
		return nil
	}
	return n.find(Ahref, cs)
}

func (n *Node) TextNode(pat string) *TextNodeNode {
	if n == nil {
		return nil
	}
	rx := regexp.MustCompile(pat)
	cs := []Checker{Text_(rx)}
	return NewTextNodeNode(n.find(TextNode, cs), rx)
}

func also(c Checker, cs []Checker) []Checker {
	return append([]Checker{c}, cs...)
}
<commit_msg>add FindNext<commit_after>// Copyright 2014, Hǎiliàng Wáng. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package query

import (
	"regexp"

	. "h12.me/html-query/expr"
)

func (n *Node) Is(cs ...Checker) bool {
	return And(cs...)(n.InternalNode()) != nil
}

func (n *Node) Find(cs ...Checker) *Node {
	return NewNode(Find(cs...)(&n.n))
}

func (n *Node) FindNext(cs ...Checker) *Node {
	return NewNode(FindSibling(cs...)(&n.n))
}

func (n *Node) FindChild(cs ...Checker) *Node {
	return NewNode(FindChild(cs...)(&n.n))
}

func (n *Node) find(c Checker, cs []Checker) *Node {
	if n == nil {
		return nil
	}
	return n.Find(append([]Checker{c}, cs...)...)
}

func (n *Node) NextSibling() *Node {
	if n == nil {
		return nil
	}
	return NewNode(NextSibling(&n.n))
}

func (n *Node) PrevSibling() *Node {
	if n == nil {
		return nil
	}
	return NewNode(PrevSibling(&n.n))
}

func (n *Node) Parent() *Node {
	if n == nil {
		return nil
	}
	return NewNode(Parent(&n.n))
}

func (n *Node) Children(cs ...Checker) NodeIter {
	if n == nil {
		return NodeIter{nil}
	}
	return NodeIter{Children(&n.n, cs...)}
}

func (n *Node) Descendants(cs ...Checker) NodeIter {
	if n == nil {
		return NodeIter{nil}
	}
	return NodeIter{Descendants(&n.n, cs...)}
}

func (n *Node) Ahref(cs ...Checker) *Node {
	if n == nil {
		return nil
	}
	return n.find(Ahref, cs)
}

func (n *Node) TextNode(pat string) *TextNodeNode {
	if n == nil {
		return nil
	}
	rx := regexp.MustCompile(pat)
	cs := []Checker{Text_(rx)}
	return NewTextNodeNode(n.find(TextNode, cs), rx)
}

func also(c Checker, cs []Checker) []Checker {
	return append([]Checker{c}, cs...)
}
<|endoftext|>


<commit_before>package objc

// #include <stdlib.h>
// #include <objc/runtime.h>
//
// static Ivar *ivar_offset(Ivar *p, size_t n) {
//   return p + n;
// }
//
// static objc_property_t *property_offset(objc_property_t *p, size_t n) {
//   return p + n;
// }
import "C"
import "unsafe"

type Class C.Class

type Ivar C.Ivar

type Property C.objc_property_t

func Class_getName(cls Class) string {
	cname := C.class_getName(cls)
	return C.GoString(cname)
}

func Class_getSuperclass(cls Class) Class {
	return Class(C.class_getSuperclass(cls))
}

func Class_isMetaClass(cls Class) bool {
	return C.class_isMetaClass(cls) == 1
}

func Class_getInstanceSize(cls Class) uint {
	return uint(C.class_getInstanceSize(cls))
}

func Class_getInstanceVariable(cls Class, name string) Ivar {
	cname := C.CString(name)
	defer C.free(unsafe.Pointer(cname))

	return Ivar(C.class_getInstanceVariable(cls, cname))
}

func Class_getClassVariable(cls Class, name string) Ivar {
	cname := C.CString(name)
	defer C.free(unsafe.Pointer(cname))

	return Ivar(C.class_getClassVariable(cls, cname))
}

func Class_addIvar(cls Class, name string, size uint, alignment uint8, types string) bool {
	cname := C.CString(name)
	defer C.free(unsafe.Pointer(cname))

	ctypes := C.CString(types)
	defer C.free(unsafe.Pointer(ctypes))

	return C.class_addIvar(cls, cname, C.size_t(size), C.uint8_t(alignment), ctypes) == 1
}

func Class_copyIvarList(cls Class) (ivarList []Ivar) {
	var coutCount C.uint

	ivarListPtr := C.class_copyIvarList(cls, &coutCount)
	defer C.free(unsafe.Pointer(ivarListPtr))

	if outCount := uint(coutCount); outCount > 0 {
		ivarList = make([]Ivar, outCount)

		for i := uint(0); i < outCount; i++ {
			ivarOffset := C.ivar_offset(ivarListPtr, C.size_t(i))
			ivarList[i] = Ivar(*ivarOffset)
		}
	}

	return
}

func Class_getIvarLayout(cls Class) string {
	clayout := unsafe.Pointer(C.class_getIvarLayout(cls))
	return C.GoString((*C.char)(clayout))
}

func Class_setIvarLayout(cls Class, layout string) {
	clayout := unsafe.Pointer(C.CString(layout))
	defer C.free(clayout)

	C.class_setIvarLayout(cls, (*C.uint8_t)(clayout))
}

func Class_getWeakIvarLayout(cls Class) string {
	clayout := unsafe.Pointer(C.class_getWeakIvarLayout(cls))
	return C.GoString((*C.char)(clayout))
}

func Class_setWeakIvarLayout(cls Class, layout string) {
	clayout := unsafe.Pointer(C.CString(layout))
	defer C.free(clayout)

	C.class_setWeakIvarLayout(cls, (*C.uint8_t)(clayout))
}

func Class_getProperty(cls Class, name string) Property {
	cname := C.CString(name)
	defer C.free(unsafe.Pointer(cname))

	return Property(C.class_getProperty(cls, cname))
}

func Class_copyPropertyList(cls Class) (properties []Property) {
	var coutCount C.uint

	propertiesPtr := C.class_copyPropertyList(cls, &coutCount)
	defer C.free(unsafe.Pointer(propertiesPtr))

	if outCount := uint(coutCount); outCount > 0 {
		properties := make([]Property, outCount)

		for i := uint(0); i < outCount; i++ {
			propertyOffset := C.property_offset(propertiesPtr, C.size_t(i))
			properties[i] = Property(*propertyOffset)
		}
	}

	return
}

//BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types)
<commit_msg>refract bis<commit_after>package objc

// #include <stdlib.h>
// #include <objc/runtime.h>
//
// static Ivar *ivar_offset(Ivar *p, size_t n) {
//   return p + n;
// }
//
// static objc_property_t *property_offset(objc_property_t *p, size_t n) {
//   return p + n;
// }
import "C"
import "unsafe"

type Class C.Class

type Ivar C.Ivar

type Property C.objc_property_t

func Class_getName(cls Class) string {
	cname := C.class_getName(cls)
	return C.GoString(cname)
}

func Class_getSuperclass(cls Class) Class {
	return Class(C.class_getSuperclass(cls))
}

func Class_isMetaClass(cls Class) bool {
	return C.class_isMetaClass(cls) != 0
}

func Class_getInstanceSize(cls Class) uint {
	return uint(C.class_getInstanceSize(cls))
}

func Class_getInstanceVariable(cls Class, name string) Ivar {
	cname := C.CString(name)
	defer C.free(unsafe.Pointer(cname))

	return Ivar(C.class_getInstanceVariable(cls, cname))
}

func Class_getClassVariable(cls Class, name string) Ivar {
	cname := C.CString(name)
	defer C.free(unsafe.Pointer(cname))

	return Ivar(C.class_getClassVariable(cls, cname))
}

func Class_addIvar(cls Class, name string, size uint, alignment uint8, types string) bool {
	cname := C.CString(name)
	defer C.free(unsafe.Pointer(cname))

	ctypes := C.CString(types)
	defer C.free(unsafe.Pointer(ctypes))

	return C.class_addIvar(cls, cname, C.size_t(size), C.uint8_t(alignment), ctypes) != 0
}

func Class_copyIvarList(cls Class) (ivarList []Ivar) {
	var coutCount C.uint

	ivarListPtr := C.class_copyIvarList(cls, &coutCount)
	defer C.free(unsafe.Pointer(ivarListPtr))

	if outCount := uint(coutCount); outCount > 0 {
		ivarList = make([]Ivar, outCount)

		for i := uint(0); i < outCount; i++ {
			ivarOffset := C.ivar_offset(ivarListPtr, C.size_t(i))
			ivarList[i] = Ivar(*ivarOffset)
		}
	}

	return
}

func Class_getIvarLayout(cls Class) uintptr {
	return uintptr(unsafe.Pointer(C.class_getIvarLayout(cls)))
}

func Class_setIvarLayout(cls Class, layout uintptr) {
	C.class_setIvarLayout(cls, (*C.uint8_t)(unsafe.Pointer(layout)))
}

func Class_getWeakIvarLayout(cls Class) string {
	clayout := unsafe.Pointer(C.class_getWeakIvarLayout(cls))
	return C.GoString((*C.char)(clayout))
}

func Class_setWeakIvarLayout(cls Class, layout string) {
	clayout := unsafe.Pointer(C.CString(layout))
	defer C.free(clayout)

	C.class_setWeakIvarLayout(cls, (*C.uint8_t)(clayout))
}

func Class_getProperty(cls Class, name string) Property {
	cname := C.CString(name)
	defer C.free(unsafe.Pointer(cname))

	return Property(C.class_getProperty(cls, cname))
}

func Class_copyPropertyList(cls Class) (properties []Property) {
	var coutCount C.uint

	propertiesPtr := C.class_copyPropertyList(cls, &coutCount)
	defer C.free(unsafe.Pointer(propertiesPtr))

	if outCount := uint(coutCount); outCount > 0 {
		properties := make([]Property, outCount)

		for i := uint(0); i < outCount; i++ {
			propertyOffset := C.property_offset(propertiesPtr, C.size_t(i))
			properties[i] = Property(*propertyOffset)
		}
	}

	return
}

//BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types)
<|endoftext|>


<commit_before>package multiaddr

import (
	"bytes"
	"fmt"
	"strings"
)

func stringToBytes(s string) ([]byte, error) {

	// consume trailing slashes
	s = strings.TrimRight(s, "/")

	b := new(bytes.Buffer)
	sp := strings.Split(s, "/")

	if sp[0] != "" {
		return nil, fmt.Errorf("invalid multiaddr, must begin with /")
	}

	// consume first empty elem
	sp = sp[1:]

	for len(sp) > 0 {
		p := ProtocolWithName(sp[0])
		if p.Code == 0 {
			return nil, fmt.Errorf("no protocol with name %s", sp[0])
		}
		b.Write(CodeToVarint(p.Code))
		sp = sp[1:]

		if p.Size == 0 { // no length.
			continue
		}

		if len(sp) < 1 {
			return nil, fmt.Errorf("protocol requires address, none given: %s", p.Name)
		}

		if p.Path {
			// it's a path protocol (terminal).
			// consume the rest of the address as the next component.
			sp = []string{"/" + strings.Join(sp, "/")}
		}

		if p.Transcoder == nil {
			return nil, fmt.Errorf("no transcoder for %s protocol", p.Name)
		}
		a, err := p.Transcoder.StringToBytes(sp[0])
		if err != nil {
			return nil, fmt.Errorf("failed to parse %s: %s %s", p.Name, sp[0], err)
		}
		if p.Size < 0 { // varint size.
			b.Write(CodeToVarint(len(a)))
		}
		b.Write(a)
		sp = sp[1:]
	}

	return b.Bytes(), nil
}

func validateBytes(b []byte) (err error) {
	for len(b) > 0 {
		code, n, err := ReadVarintCode(b)
		if err != nil {
			return err
		}

		b = b[n:]
		p := ProtocolWithCode(code)
		if p.Code == 0 {
			return fmt.Errorf("no protocol with code %d", code)
		}

		if p.Size == 0 {
			continue
		}

		n, size, err := sizeForAddr(p, b)
		if err != nil {
			return err
		}

		b = b[n:]

		if len(b) < size || size < 0 {
			return fmt.Errorf("invalid value for size")
		}

		err = p.Transcoder.ValidateBytes(b[:size])
		if err != nil {
			return err
		}

		b = b[size:]
	}

	return nil
}

func bytesToString(b []byte) (ret string, err error) {
	s := ""

	for len(b) > 0 {
		code, n, err := ReadVarintCode(b)
		if err != nil {
			return "", err
		}

		b = b[n:]
		p := ProtocolWithCode(code)
		if p.Code == 0 {
			return "", fmt.Errorf("no protocol with code %d", code)
		}
		s += "/" + p.Name

		if p.Size == 0 {
			continue
		}

		n, size, err := sizeForAddr(p, b)
		if err != nil {
			return "", err
		}

		b = b[n:]

		if len(b) < size || size < 0 {
			return "", fmt.Errorf("invalid value for size")
		}

		if p.Transcoder == nil {
			return "", fmt.Errorf("no transcoder for %s protocol", p.Name)
		}
		a, err := p.Transcoder.BytesToString(b[:size])
		if err != nil {
			return "", err
		}
		if p.Path && len(a) > 0 && a[0] == '/' {
			a = a[1:]
		}
		if len(a) > 0 {
			s += "/" + a
		}
		b = b[size:]
	}

	return s, nil
}

func sizeForAddr(p Protocol, b []byte) (skip, size int, err error) {
	switch {
	case p.Size > 0:
		return 0, (p.Size / 8), nil
	case p.Size == 0:
		return 0, 0, nil
	case p.Path:
		size, n, err := ReadVarintCode(b)
		if err != nil {
			return 0, 0, err
		}
		return n, size, nil
	default:
		size, n, err := ReadVarintCode(b)
		if err != nil {
			return 0, 0, err
		}
		return n, size, nil
	}
}

func bytesSplit(b []byte) ([][]byte, error) {
	var ret [][]byte
	for len(b) > 0 {
		code, n, err := ReadVarintCode(b)
		if err != nil {
			return nil, err
		}

		p := ProtocolWithCode(code)
		if p.Code == 0 {
			return nil, fmt.Errorf("no protocol with code %d", b[0])
		}

		n2, size, err := sizeForAddr(p, b[n:])
		if err != nil {
			return nil, err
		}

		length := n + n2 + size
		ret = append(ret, b[:length])
		b = b[length:]
	}

	return ret, nil
}
<commit_msg>explicitly throw away impossible write errors<commit_after>package multiaddr

import (
	"bytes"
	"fmt"
	"strings"
)

func stringToBytes(s string) ([]byte, error) {

	// consume trailing slashes
	s = strings.TrimRight(s, "/")

	b := new(bytes.Buffer)
	sp := strings.Split(s, "/")

	if sp[0] != "" {
		return nil, fmt.Errorf("invalid multiaddr, must begin with /")
	}

	// consume first empty elem
	sp = sp[1:]

	for len(sp) > 0 {
		p := ProtocolWithName(sp[0])
		if p.Code == 0 {
			return nil, fmt.Errorf("no protocol with name %s", sp[0])
		}
		_, _ = b.Write(CodeToVarint(p.Code))
		sp = sp[1:]

		if p.Size == 0 { // no length.
			continue
		}

		if len(sp) < 1 {
			return nil, fmt.Errorf("protocol requires address, none given: %s", p.Name)
		}

		if p.Path {
			// it's a path protocol (terminal).
			// consume the rest of the address as the next component.
			sp = []string{"/" + strings.Join(sp, "/")}
		}

		if p.Transcoder == nil {
			return nil, fmt.Errorf("no transcoder for %s protocol", p.Name)
		}
		a, err := p.Transcoder.StringToBytes(sp[0])
		if err != nil {
			return nil, fmt.Errorf("failed to parse %s: %s %s", p.Name, sp[0], err)
		}
		if p.Size < 0 { // varint size.
			_, _ = b.Write(CodeToVarint(len(a)))
		}
		b.Write(a)
		sp = sp[1:]
	}

	return b.Bytes(), nil
}

func validateBytes(b []byte) (err error) {
	for len(b) > 0 {
		code, n, err := ReadVarintCode(b)
		if err != nil {
			return err
		}

		b = b[n:]
		p := ProtocolWithCode(code)
		if p.Code == 0 {
			return fmt.Errorf("no protocol with code %d", code)
		}

		if p.Size == 0 {
			continue
		}

		n, size, err := sizeForAddr(p, b)
		if err != nil {
			return err
		}

		b = b[n:]

		if len(b) < size || size < 0 {
			return fmt.Errorf("invalid value for size")
		}

		err = p.Transcoder.ValidateBytes(b[:size])
		if err != nil {
			return err
		}

		b = b[size:]
	}

	return nil
}

func bytesToString(b []byte) (ret string, err error) {
	s := ""

	for len(b) > 0 {
		code, n, err := ReadVarintCode(b)
		if err != nil {
			return "", err
		}

		b = b[n:]
		p := ProtocolWithCode(code)
		if p.Code == 0 {
			return "", fmt.Errorf("no protocol with code %d", code)
		}
		s += "/" + p.Name

		if p.Size == 0 {
			continue
		}

		n, size, err := sizeForAddr(p, b)
		if err != nil {
			return "", err
		}

		b = b[n:]

		if len(b) < size || size < 0 {
			return "", fmt.Errorf("invalid value for size")
		}

		if p.Transcoder == nil {
			return "", fmt.Errorf("no transcoder for %s protocol", p.Name)
		}
		a, err := p.Transcoder.BytesToString(b[:size])
		if err != nil {
			return "", err
		}
		if p.Path && len(a) > 0 && a[0] == '/' {
			a = a[1:]
		}
		if len(a) > 0 {
			s += "/" + a
		}
		b = b[size:]
	}

	return s, nil
}

func sizeForAddr(p Protocol, b []byte) (skip, size int, err error) {
	switch {
	case p.Size > 0:
		return 0, (p.Size / 8), nil
	case p.Size == 0:
		return 0, 0, nil
	case p.Path:
		size, n, err := ReadVarintCode(b)
		if err != nil {
			return 0, 0, err
		}
		return n, size, nil
	default:
		size, n, err := ReadVarintCode(b)
		if err != nil {
			return 0, 0, err
		}
		return n, size, nil
	}
}

func bytesSplit(b []byte) ([][]byte, error) {
	var ret [][]byte
	for len(b) > 0 {
		code, n, err := ReadVarintCode(b)
		if err != nil {
			return nil, err
		}

		p := ProtocolWithCode(code)
		if p.Code == 0 {
			return nil, fmt.Errorf("no protocol with code %d", b[0])
		}

		n2, size, err := sizeForAddr(p, b[n:])
		if err != nil {
			return nil, err
		}

		length := n + n2 + size
		ret = append(ret, b[:length])
		b = b[length:]
	}

	return ret, nil
}
<|endoftext|>


<commit_before>package main

import "strings"

var colorPalette = [][]string{
	{
		`"#ffbb00"`, `"#f90085"`, `"#00bfb2"`, `"#008ffc"`, `"#fc4f00"`, `"#9d00ff"`, `"#ff0000"`, `"#00b515"`,
		`"#c7f400"`, `"#f9a2a2"`, `"#007072"`, `"#e6beff"`, `"#aa5b00"`, `"#fff266"`, `"#7f0000"`, `"#aaffc3"`,
		`"#7f7f00"`, `"#ffe0c1"`, `"#000080"`, `"#808080"`, `"#000000"`},
	{
		`"#f44336"`, `"#9c27b0"`, `"#3f51b5"`, `"#03a9f4"`, `"#009688"`, `"#8bc34a"`, `"#ffeb3b"`, `"#ff9800"`,
		`"#795548"`, `"#607d8b"`, `"#e91e63"`, `"#673ab7"`, `"#2196f3"`, `"#00bcd4"`, `"#4caf50"`, `"#cddc39"`,
		`"#ffc107"`, `"#ff5722"`, `"#9e9e9e"`},
	{
		`"#08306b"`, `"#08519c"`, `"#2171b5"`, `"#4292c6"`, `"#6baed6"`, `"#9ecae1"`, `"#c6dbef"`, `"#deebf7"`,
		`"#00441b"`, `"#006d2c"`, `"#238b45"`, `"#41ab5d"`, `"#74c476"`, `"#a1d99b"`, `"#c7e9c0"`, `"#e5f5e0"`,
		`"#7f2704"`, `"#a63603"`, `"#d94801"`, `"#f16913"`, `"#fd8d3c"`, `"#fdae6b"`, `"#fdd0a2"`, `"#fee6ce"`,
		`"#3f007d"`, `"#54278f"`, `"#6a51a3"`, `"#807dba"`, `"#9e9ac8"`, `"#bcbddc"`, `"#dadaeb"`, `"#efedf5"`,
		`"#67001f"`, `"#980043"`, `"#ce1256"`, `"#e7298a"`, `"#df65b0"`, `"#c994c7"`, `"#d4b9da"`, `"#e7e1ef"`,
		`"#000000"`, `"#252525"`, `"#525252"`, `"#737373"`, `"#969696"`, `"#bdbdbd"`, `"#d9d9d9"`, `"#f0f0f0"`},
}

var colorI = 0

// Reset restarts the palette iterator. Following Reset(), invoking Next() returns the first color in the palette.
func colorReset() {
	colorI = 0
}

// Next iterates through the color palette.
func colorNext(i int) string {
	result := colorPalette[i][colorI]
	colorI++
	if colorI > len(colorPalette[i])-1 {
		colorI = 0
	}

	return result
}

func colorIndex(i, j int) string {
	return colorPalette[i][j%len(colorPalette[i])]
}

// FirstN returns a comma-separated string of the first n colors in the palette.
func colorFirstN(i, n int) string {
	k := 0
	var cs []string
	for j := 0; j < n; j++ {
		cs = append(cs, colorPalette[i][k])
		k++
		if k > len(colorPalette[i])-1 {
			k = 0
		}
	}
	return strings.Join(cs, ",")
}

func colorRepeat(i, j, n int) string {
	return strings.Repeat(colorIndex(i, j)+",", n)
}
<commit_msg>Updates following PR comments - Increase default colour palette to 60 colours<commit_after>package main

import "strings"

var colorPalette = [][]string{
	{
		`"#ffbb00"`, `"#f90085"`, `"#00bfb2"`, `"#008ffc"`, `"#fc4f00"`, `"#9d00ff"`, `"#ff0000"`, `"#00b515"`,
		`"#c7f400"`, `"#f9a2a2"`, `"#007072"`, `"#e6beff"`, `"#aa5b00"`, `"#fff266"`, `"#7f0000"`, `"#aaffc3"`,
		`"#7f7f00"`, `"#ffe0c1"`, `"#000080"`, `"#808080"`, `"#000000"`,
		`"#ffe08b"`, `"#fc8bc7"`, `"#8be1dc"`, `"#8bccfd"`, `"#fdaf8b"`, `"#dba2ff"`, `"#ff7373"`, `"#a2e4a9"`,
		`"#eafba2"`, `"#fcdddd"`, `"#a2cbcb"`, `"#f3e1ff"`, `"#e0c3a2"`, `"#fffac7"`, `"#d0a2a2"`, `"#d8ffe3"`,
		`"#d0d0a2"`, `"#fff3e8"`, `"#a2a2d0"`, `"#d0d0d0"`,
		`"#a37700"`, `"#9f0055"`, `"#007a72"`, `"#005ca1"`, `"#a13300"`, `"#56008c"`, `"#8c0000"`, `"#00630c"`,
		`"#5b6f00"`, `"#885959"`, `"#003e3f"`, `"#7e688c"`, `"#5d3200"`, `"#746e2f"`, `"#460000"`, `"#4e7459"`,
		`"#3a3a00"`, `"#746658"`, `"#000052"`},
	{
		`"#f44336"`, `"#9c27b0"`, `"#3f51b5"`, `"#03a9f4"`, `"#009688"`, `"#8bc34a"`, `"#ffeb3b"`, `"#ff9800"`,
		`"#795548"`, `"#607d8b"`, `"#e91e63"`, `"#673ab7"`, `"#2196f3"`, `"#00bcd4"`, `"#4caf50"`, `"#cddc39"`,
		`"#ffc107"`, `"#ff5722"`, `"#9e9e9e"`},
	{
		`"#08306b"`, `"#08519c"`, `"#2171b5"`, `"#4292c6"`, `"#6baed6"`, `"#9ecae1"`, `"#c6dbef"`, `"#deebf7"`,
		`"#00441b"`, `"#006d2c"`, `"#238b45"`, `"#41ab5d"`, `"#74c476"`, `"#a1d99b"`, `"#c7e9c0"`, `"#e5f5e0"`,
		`"#7f2704"`, `"#a63603"`, `"#d94801"`, `"#f16913"`, `"#fd8d3c"`, `"#fdae6b"`, `"#fdd0a2"`, `"#fee6ce"`,
		`"#3f007d"`, `"#54278f"`, `"#6a51a3"`, `"#807dba"`, `"#9e9ac8"`, `"#bcbddc"`, `"#dadaeb"`, `"#efedf5"`,
		`"#67001f"`, `"#980043"`, `"#ce1256"`, `"#e7298a"`, `"#df65b0"`, `"#c994c7"`, `"#d4b9da"`, `"#e7e1ef"`,
		`"#000000"`, `"#252525"`, `"#525252"`, `"#737373"`, `"#969696"`, `"#bdbdbd"`, `"#d9d9d9"`, `"#f0f0f0"`},
}

var colorI = 0

// Reset restarts the palette iterator. Following Reset(), invoking Next() returns the first color in the palette.
func colorReset() {
	colorI = 0
}

// Next iterates through the color palette.
func colorNext(i int) string {
	result := colorPalette[i][colorI]
	colorI++
	if colorI > len(colorPalette[i])-1 {
		colorI = 0
	}

	return result
}

func colorIndex(i, j int) string {
	return colorPalette[i][j%len(colorPalette[i])]
}

// FirstN returns a comma-separated string of the first n colors in the palette.
func colorFirstN(i, n int) string {
	k := 0
	var cs []string
	for j := 0; j < n; j++ {
		cs = append(cs, colorPalette[i][k])
		k++
		if k > len(colorPalette[i])-1 {
			k = 0
		}
	}
	return strings.Join(cs, ",")
}

func colorRepeat(i, j, n int) string {
	return strings.Repeat(colorIndex(i, j)+",", n)
}
<|endoftext|>


<commit_before>package figlet4go

import (
	"encoding/hex"
	"errors"
	"fmt"
)

// Escape char
const escape string = "\x1b"

// Terminal AnsiColors
var (
	ColorBlack   AnsiColor = AnsiColor{30}
	ColorRed     AnsiColor = AnsiColor{31}
	ColorGreen   AnsiColor = AnsiColor{32}
	ColorYellow  AnsiColor = AnsiColor{33}
	ColorBlue    AnsiColor = AnsiColor{34}
	ColorMagenta AnsiColor = AnsiColor{35}
	ColorCyan    AnsiColor = AnsiColor{36}
	ColorWhite   AnsiColor = AnsiColor{37}
)

// TrueColor lookalikes for displaying AnsiColor f.e. with the HTML parser
// Colors based on http://clrs.cc/
// "TrueColorForAnsiColor"
var tcfac map[*AnsiColor]TrueColor = map[*AnsiColor]TrueColor{
	&ColorBlack:   {0, 0, 0},
	&ColorRed:     {255, 65, 54},
	&ColorGreen:   {149, 189, 64},
	&ColorYellow:  {255, 220, 0},
	&ColorBlue:    {0, 116, 217},
	&ColorMagenta: {177, 13, 201},
	&ColorCyan:    {105, 206, 245},
	&ColorWhite:   {255, 255, 255},
}

// Color has a pre- and a suffix
type Color interface {
	getPrefix(p Parser) string
	getSuffix(p Parser) string
}

// AnsiColor representation
type AnsiColor struct {
	code int
}

// TrueColor with rgb Attributes
type TrueColor struct {
	r int
	g int
	b int
}

// Prefix for ansi color
func (tc TrueColor) getPrefix(p Parser) string {
	switch p.Name {

	case "terminal":
		return fmt.Sprintf("%v[38;2;%d;%d;%dm", escape, tc.r, tc.g, tc.b)

	case "html":
		return fmt.Sprintf("", tc.r, tc.g, tc.b)
	}

	return ""
}

// Suffix for ansi color
func (tc TrueColor) getSuffix(p Parser) string {
	switch p.Name {

	case "terminal":
		return fmt.Sprintf("%v[0m", escape)

	case "html":
		return ""
	}

	return ""
}

// NewTrueColorFromHexString returns a Truecolor object based on a hexadezimal string
func NewTrueColorFromHexString(c string) (*TrueColor, error) {
	rgb, err := hex.DecodeString(c)
	if err != nil {
		return nil, errors.New("Invalid color given (" + c + ")")
	}

	return &TrueColor{
		int(rgb[0]),
		int(rgb[1]),
		int(rgb[2]),
	}, nil
}

// Prefix for ansi color
func (ac AnsiColor) getPrefix(p Parser) string {
	switch p.Name {

	case "terminal":
		return fmt.Sprintf("%v[0;%dm", escape, ac.code)

	case "html":
		// Get the TrueColor for the AnsiColor
		tc := tcfac[&ac]
		return tc.getPrefix(p)
	}

	return ""

}

// Suffix for ansi color
func (ac AnsiColor) getSuffix(p Parser) string {
	switch p.Name {

	case "terminal":
		return fmt.Sprintf("%v[0m", escape)

	case "html":
		// Get the TrueColor for the AnsiColor
		tc := tcfac[&ac]
		return tc.getSuffix(p)
	}

	return ""
}
<commit_msg>bugfix<commit_after>package figlet4go

import (
	"encoding/hex"
	"errors"
	"fmt"
)

// Escape char
const escape string = "\x1b"

// Terminal AnsiColors
var (
	ColorBlack   AnsiColor = AnsiColor{30}
	ColorRed     AnsiColor = AnsiColor{31}
	ColorGreen   AnsiColor = AnsiColor{32}
	ColorYellow  AnsiColor = AnsiColor{33}
	ColorBlue    AnsiColor = AnsiColor{34}
	ColorMagenta AnsiColor = AnsiColor{35}
	ColorCyan    AnsiColor = AnsiColor{36}
	ColorWhite   AnsiColor = AnsiColor{37}
)

// TrueColor lookalikes for displaying AnsiColor f.e. with the HTML parser
// Colors based on http://clrs.cc/
// "TrueColorForAnsiColor"
var tcfac map[AnsiColor]TrueColor = map[AnsiColor]TrueColor{
	ColorBlack:   {0, 0, 0},
	ColorRed:     {255, 65, 54},
	ColorGreen:   {149, 189, 64},
	ColorYellow:  {255, 220, 0},
	ColorBlue:    {0, 116, 217},
	ColorMagenta: {177, 13, 201},
	ColorCyan:    {105, 206, 245},
	ColorWhite:   {255, 255, 255},
}

// Color has a pre- and a suffix
type Color interface {
	getPrefix(p Parser) string
	getSuffix(p Parser) string
}

// AnsiColor representation
type AnsiColor struct {
	code int
}

// TrueColor with rgb Attributes
type TrueColor struct {
	r int
	g int
	b int
}

// Prefix for ansi color
func (tc TrueColor) getPrefix(p Parser) string {
	switch p.Name {

	case "terminal":
		return fmt.Sprintf("%v[38;2;%d;%d;%dm", escape, tc.r, tc.g, tc.b)

	case "html":
		return fmt.Sprintf("", tc.r, tc.g, tc.b)
	}

	return ""
}

// Suffix for ansi color
func (tc TrueColor) getSuffix(p Parser) string {
	switch p.Name {

	case "terminal":
		return fmt.Sprintf("%v[0m", escape)

	case "html":
		return ""
	}

	return ""
}

// NewTrueColorFromHexString returns a Truecolor object based on a hexadezimal string
func NewTrueColorFromHexString(c string) (*TrueColor, error) {
	rgb, err := hex.DecodeString(c)
	if err != nil {
		return nil, errors.New("Invalid color given (" + c + ")")
	}

	return &TrueColor{
		int(rgb[0]),
		int(rgb[1]),
		int(rgb[2]),
	}, nil
}

// Prefix for ansi color
func (ac AnsiColor) getPrefix(p Parser) string {
	switch p.Name {

	case "terminal":
		return fmt.Sprintf("%v[0;%dm", escape, ac.code)

	case "html":
		// Get the TrueColor for the AnsiColor
		tc := tcfac[ac]
		return tc.getPrefix(p)
	}

	return ""

}

// Suffix for ansi color
func (ac AnsiColor) getSuffix(p Parser) string {
	switch p.Name {

	case "terminal":
		return fmt.Sprintf("%v[0m", escape)

	case "html":
		// Get the TrueColor for the AnsiColor
		tc := tcfac[ac]
		return tc.getSuffix(p)
	}

	return ""
}
<|endoftext|>


<commit_before>package main

const NGINX_BUILD_VERSION = "0.4.2"

// nginx
const (
	NGINX_VERSION             = "1.9.2"
	NGINX_DOWNLOAD_URL_PREFIX = "http://nginx.org/download"
)

// pcre
const (
	PCRE_VERSION             = "8.37"
	PCRE_DOWNLOAD_URL_PREFIX = "http://ftp.csx.cam.ac.uk/pub/software/programming/pcre"
)

// openssl
const (
	OPENSSL_VERSION             = "1.0.2c"
	OPENSSL_DOWNLOAD_URL_PREFIX = "http://www.openssl.org/source"
)

// zlib
const (
	ZLIB_VERSION             = "1.2.8"
	ZLIB_DOWNLOAD_URL_PREFIX = "http://zlib.net"
)

// openResty
const (
	OPENRESTY_VERSION             = "1.7.10.1"
	OPENRESTY_DOWNLOAD_URL_PREFIX = "http://openresty.org/download"
)

// tengine
const (
	TENGINE_VERSION             = "2.1.0"
	TENGINE_DOWNLOAD_URL_PREFIX = "http://tengine.taobao.org/download"
)

// component enumerations
const (
	COMPONENT_NGINX = iota
	COMPONENT_OPENRESTY
	COMPONENT_TENGINE
	COMPONENT_PCRE
	COMPONENT_OPENSSL
	COMPONENT_ZLIB
	COMPONENT_MAX
)
<commit_msg>bumped openresty version to 1.7.10.2.<commit_after>package main

const NGINX_BUILD_VERSION = "0.4.2"

// nginx
const (
	NGINX_VERSION             = "1.9.2"
	NGINX_DOWNLOAD_URL_PREFIX = "http://nginx.org/download"
)

// pcre
const (
	PCRE_VERSION             = "8.37"
	PCRE_DOWNLOAD_URL_PREFIX = "http://ftp.csx.cam.ac.uk/pub/software/programming/pcre"
)

// openssl
const (
	OPENSSL_VERSION             = "1.0.2c"
	OPENSSL_DOWNLOAD_URL_PREFIX = "http://www.openssl.org/source"
)

// zlib
const (
	ZLIB_VERSION             = "1.2.8"
	ZLIB_DOWNLOAD_URL_PREFIX = "http://zlib.net"
)

// openResty
const (
	OPENRESTY_VERSION             = "1.7.10.2"
	OPENRESTY_DOWNLOAD_URL_PREFIX = "http://openresty.org/download"
)

// tengine
const (
	TENGINE_VERSION             = "2.1.0"
	TENGINE_DOWNLOAD_URL_PREFIX = "http://tengine.taobao.org/download"
)

// component enumerations
const (
	COMPONENT_NGINX = iota
	COMPONENT_OPENRESTY
	COMPONENT_TENGINE
	COMPONENT_PCRE
	COMPONENT_OPENSSL
	COMPONENT_ZLIB
	COMPONENT_MAX
)
<|endoftext|>


<commit_before>package paypal

type SubscriptionPlanStatus string

const (
	SubscriptionPlanStatusCreated  SubscriptionPlanStatus = "CREATED"
	SubscriptionPlanStatusInactive SubscriptionPlanStatus = "INACTIVE"
	SubscriptionPlanStatusActive   SubscriptionPlanStatus = "ACTIVE"
)

type BillingPlanStatus string

const (
	BillingPlanStatusActive BillingPlanStatus = "ACTIVE"
)

type IntervalUnit string

const (
	IntervalUnitDay   IntervalUnit = "DAY"
	IntervalUnitWeek  IntervalUnit = "WEEK"
	IntervalUnitMonth IntervalUnit = "MONTH"
	IntervalUnitYear  IntervalUnit = "YEAR"
)

type TenureType string

const (
	TenureTypeRegular TenureType = "REGULAR"
	TenureTypeTrial   TenureType = "TRIAL"
)

type SetupFeeFailureAction string

const (
	SetupFeeFailureActionContinue SetupFeeFailureAction = "CONTINUE"
	SetupFeeFailureActionCancel   SetupFeeFailureAction = "CANCEL"
)

type ShippingPreference string

const (
	ShippingPreferenceGetFromFile        ShippingPreference = "GET_FROM_FILE"
	ShippingPreferenceNoShipping         ShippingPreference = "NO_SHIPPING"
	ShippingPreferenceSetProvidedAddress ShippingPreference = "SET_PROVIDED_ADDRESS"
)

type UserAction string

const (
	UserActionContinue     UserAction = "CONTINUE"
	UserActionSubscribeNow UserAction = "SUBSCRIBE_NOW"
)

type SubscriptionStatus string

const (
	SubscriptionStatusApprovalPending SubscriptionStatus = "APPROVAL_PENDING"
	SubscriptionStatusApproved        SubscriptionStatus = "APPROVED"
	SubscriptionStatusActive          SubscriptionStatus = "ACTIVE"
	SubscriptionStatusSuspended       SubscriptionStatus = "SUSPENDED"
	SubscriptionStatusCancelled       SubscriptionStatus = "CANCELLED"
	SubscriptionStatusExpired         SubscriptionStatus = "EXPIRED"
)

//Doc: https://developer.paypal.com/docs/api/subscriptions/v1/#definition-transaction
type SubscriptionTransactionStatus string

const (
	SubscriptionCaptureStatusCompleted         SubscriptionTransactionStatus = "COMPLETED"
	SubscriptionCaptureStatusDeclined          SubscriptionTransactionStatus = "DECLINED"
	SubscriptionCaptureStatusPartiallyRefunded SubscriptionTransactionStatus = "PARTIALLY_REFUNDED"
	SubscriptionCaptureStatusPending           SubscriptionTransactionStatus = "PENDING"
	SubscriptionCaptureStatusRefunded          SubscriptionTransactionStatus = "REFUNDED"
)

type CaptureType string

const (
	CaptureTypeOutstandingBalance CaptureType = "OUTSTANDING_BALANCE"
)

type ProductType string
type ProductCategory string //Doc: https://developer.paypal.com/docs/api/catalog-products/v1/#definition-product_category
const (
	ProductTypePhysical ProductType = "PHYSICAL"
	ProductTypeDigital  ProductType = "DIGITAL"
	ProductTypeService  ProductType = "SERVICE"

	ProductCategorySoftware                                  ProductCategory = "SOFTWARE"
	ProductCategorySoftwareComputerAndDataProcessingServices ProductCategory = "COMPUTER_AND_DATA_PROCESSING_SERVICES"
	ProductCategorySoftwareDigitalGames                      ProductCategory = "DIGITAL_GAMES"
	ProductCategorySoftwareGameSoftware                      ProductCategory = "GAME_SOFTWARE"
	ProductCategorySoftwareGames                             ProductCategory = "GAMES"
	ProductCategorySoftwareGeneral                           ProductCategory = "GENERAL"
	ProductCategorySoftwareGraphicAndCommercialDesign        ProductCategory = "GRAPHIC_AND_COMMERCIAL_DESIGN"
	ProductCategorySoftwareOemSoftware                       ProductCategory = "OEM_SOFTWARE"
	ProductCategorySoftwareOnlineGaming                      ProductCategory = "ONLINE_GAMING"
	ProductCategorySoftwareOnlineGamingCurrency              ProductCategory = "ONLINE_GAMING_CURRENCY"
	ProductCategorySoftwareOnlineServices                    ProductCategory = "ONLINE_SERVICES"
	ProductCategorySoftwareOther                             ProductCategory = "OTHER"
	ProductCategorySoftwareServices                          ProductCategory = "SERVICES"
)
<commit_msg>#160: add PAY_NOW user action<commit_after>package paypal

type SubscriptionPlanStatus string

const (
	SubscriptionPlanStatusCreated  SubscriptionPlanStatus = "CREATED"
	SubscriptionPlanStatusInactive SubscriptionPlanStatus = "INACTIVE"
	SubscriptionPlanStatusActive   SubscriptionPlanStatus = "ACTIVE"
)

type BillingPlanStatus string

const (
	BillingPlanStatusActive BillingPlanStatus = "ACTIVE"
)

type IntervalUnit string

const (
	IntervalUnitDay   IntervalUnit = "DAY"
	IntervalUnitWeek  IntervalUnit = "WEEK"
	IntervalUnitMonth IntervalUnit = "MONTH"
	IntervalUnitYear  IntervalUnit = "YEAR"
)

type TenureType string

const (
	TenureTypeRegular TenureType = "REGULAR"
	TenureTypeTrial   TenureType = "TRIAL"
)

type SetupFeeFailureAction string

const (
	SetupFeeFailureActionContinue SetupFeeFailureAction = "CONTINUE"
	SetupFeeFailureActionCancel   SetupFeeFailureAction = "CANCEL"
)

type ShippingPreference string

const (
	ShippingPreferenceGetFromFile        ShippingPreference = "GET_FROM_FILE"
	ShippingPreferenceNoShipping         ShippingPreference = "NO_SHIPPING"
	ShippingPreferenceSetProvidedAddress ShippingPreference = "SET_PROVIDED_ADDRESS"
)

type UserAction string

const (
	UserActionContinue UserAction = "CONTINUE"
	UserActionPayNow   UserAction = "PAY_NOW"
)

type SubscriptionStatus string

const (
	SubscriptionStatusApprovalPending SubscriptionStatus = "APPROVAL_PENDING"
	SubscriptionStatusApproved        SubscriptionStatus = "APPROVED"
	SubscriptionStatusActive          SubscriptionStatus = "ACTIVE"
	SubscriptionStatusSuspended       SubscriptionStatus = "SUSPENDED"
	SubscriptionStatusCancelled       SubscriptionStatus = "CANCELLED"
	SubscriptionStatusExpired         SubscriptionStatus = "EXPIRED"
)

//Doc: https://developer.paypal.com/docs/api/subscriptions/v1/#definition-transaction
type SubscriptionTransactionStatus string

const (
	SubscriptionCaptureStatusCompleted         SubscriptionTransactionStatus = "COMPLETED"
	SubscriptionCaptureStatusDeclined          SubscriptionTransactionStatus = "DECLINED"
	SubscriptionCaptureStatusPartiallyRefunded SubscriptionTransactionStatus = "PARTIALLY_REFUNDED"
	SubscriptionCaptureStatusPending           SubscriptionTransactionStatus = "PENDING"
	SubscriptionCaptureStatusRefunded          SubscriptionTransactionStatus = "REFUNDED"
)

type CaptureType string

const (
	CaptureTypeOutstandingBalance CaptureType = "OUTSTANDING_BALANCE"
)

type ProductType string
type ProductCategory string //Doc: https://developer.paypal.com/docs/api/catalog-products/v1/#definition-product_category
const (
	ProductTypePhysical ProductType = "PHYSICAL"
	ProductTypeDigital  ProductType = "DIGITAL"
	ProductTypeService  ProductType = "SERVICE"

	ProductCategorySoftware                                  ProductCategory = "SOFTWARE"
	ProductCategorySoftwareComputerAndDataProcessingServices ProductCategory = "COMPUTER_AND_DATA_PROCESSING_SERVICES"
	ProductCategorySoftwareDigitalGames                      ProductCategory = "DIGITAL_GAMES"
	ProductCategorySoftwareGameSoftware                      ProductCategory = "GAME_SOFTWARE"
	ProductCategorySoftwareGames                             ProductCategory = "GAMES"
	ProductCategorySoftwareGeneral                           ProductCategory = "GENERAL"
	ProductCategorySoftwareGraphicAndCommercialDesign        ProductCategory = "GRAPHIC_AND_COMMERCIAL_DESIGN"
	ProductCategorySoftwareOemSoftware                       ProductCategory = "OEM_SOFTWARE"
	ProductCategorySoftwareOnlineGaming                      ProductCategory = "ONLINE_GAMING"
	ProductCategorySoftwareOnlineGamingCurrency              ProductCategory = "ONLINE_GAMING_CURRENCY"
	ProductCategorySoftwareOnlineServices                    ProductCategory = "ONLINE_SERVICES"
	ProductCategorySoftwareOther                             ProductCategory = "OTHER"
	ProductCategorySoftwareServices                          ProductCategory = "SERVICES"
)
<|endoftext|>


<commit_before>package	password

import	(
	"fmt"
)

type	(
	Definition	interface {
		String()		string
		CrypterFound(string)	(Crypter,bool)
		Options()		map[string]interface{}
		Default()		Crypter

		SetOptions(map[string]interface{})	Definition

		Crypt(pwd, salt []byte, options map[string]interface{})	string
	}

	Crypter	interface {
		Salt(salt []byte)		Crypter
		Hashed(pwd []byte)		Crypter
		Set(pwd string) 		error
		Crypt(pwd []byte)		string
		Verify(pwd []byte)		bool
		Options()			map[string]interface{}
		Definition()			Definition
	}

	Crypt	struct {
		index	[]Definition
		deflt	Crypter
		found	Crypter
	}
)

var	(
	NoMatchingDef	error	= fmt.Errorf("No Matching Definition Found")

	crypt	*Crypt	= &Crypt{
	}
)

func register(def Definition) Definition {
	crypt.Register(def)
	return	def
}

func Register(def ...Definition) {
	crypt.Register(def...)
}

func SetDefault(def Definition) {
	crypt.SetDefault(def)
}

func Set(pwd string) error {
	return	crypt.Set(pwd)
}

func (c *Crypt)Register(def ...Definition) {
	c.index = append(c.index, def...)
}

func (c *Crypt)SetDefault(def Definition) {
	c.deflt	= def.Default()
}

func (c *Crypt)Set(pwd string) error {
	for _,i := range c.index {
		if crypter, ok := i.CrypterFound(pwd); ok {
			c.found = crypter
			return nil
		}
	}

	if c.deflt != nil {
		c.found = c.deflt
		return nil
	}

	return	NoMatchingDef
}
<commit_msg>forgot factory Getter<commit_after>package	password

import	(
	"fmt"
)

type	(
	Definition	interface {
		String()		string
		CrypterFound(string)	(Crypter,bool)
		Options()		map[string]interface{}
		Default()		Crypter

		SetOptions(map[string]interface{})	Definition

		Crypt(pwd, salt []byte, options map[string]interface{})	string
	}

	Crypter	interface {
		Salt(salt []byte)		Crypter
		Hashed(pwd []byte)		Crypter
		Set(pwd string) 		error
		Crypt(pwd []byte)		string
		Verify(pwd []byte)		bool
		Options()			map[string]interface{}
		Definition()			Definition
	}

	Crypt	struct {
		index	[]Definition
		deflt	Crypter
		found	Crypter
	}
)

var	(
	NoMatchingDef	error	= fmt.Errorf("No Matching Definition Found")

	crypt	*Crypt	= &Crypt{
	}
)

func register(def Definition) Definition {
	crypt.Register(def)
	return	def
}

func Register(def ...Definition) {
	crypt.Register(def...)
}

func SetDefault(def Definition) {
	crypt.SetDefault(def)
}

func Set(pwd string) error {
	return	crypt.Set(pwd)
}

func (c *Crypt)Register(def ...Definition) {
	c.index = append(c.index, def...)
}

func (c *Crypt)SetDefault(def Definition) {
	c.deflt	= def.Default()
}

func (c *Crypt)Set(pwd string) error {
	for _,i := range c.index {
		if crypter, ok := i.CrypterFound(pwd); ok {
			c.found = crypter
			return nil
		}
	}

	if c.deflt != nil {
		c.found = c.deflt
		return nil
	}

	return	NoMatchingDef
}

func (c *Crypt)Crypter() Crypter {
	return	c.found
}
<|endoftext|>


<commit_before>
// LoL Cruncher - A Historical League of Legends Statistics Tracker
// Copyright (C) 2015  Jason Chu (1lann) 1lanncontact@gmail.com

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

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

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

package database

import (
	"github.com/revel/revel"
	"gopkg.in/mgo.v2"
	"time"
)

var IsConnected bool
var isConnecting bool
var activeSession *mgo.Session
var players *mgo.Collection
var playerIds *mgo.Collection

func isDisconnected(err string) bool {
	if err == "EOF" || err == "no reachable servers" ||
		err == "Closed explicitly" {
		return true
	} else {
		return false
	}
}

func Connect() {
	if !isConnecting {
		IsConnected = false

		if activeSession != nil {
			activeSession.Close()
		}

		isConnecting = true
		revel.INFO.Println("Connecting...")

		databaseIp, found := revel.Config.String("database.ip")

		if !found {
			revel.ERROR.Println("Missing database.ip in conf/app.conf!")
			panic("Missing database.ip in conf/app.conf!")
			return
		}

		session, err := mgo.DialWithTimeout(databaseIp, time.Second*3)
		if err != nil {
			isConnecting = false
			IsConnected = false
			revel.ERROR.Println(err)
			return
		}

		session.SetMode(mgo.Monotonic, true)
		session.SetSafe(&mgo.Safe{})
		session.SetSyncTimeout(time.Second*3)
		session.SetSocketTimeout(time.Second*3)

		activeSession = session

		players = session.DB("cruncher").C("players")
		playerIds = session.DB("cruncher").C("playerids")

		IsConnected = true
		isConnecting = false
	}
}
<commit_msg>Added database authentication<commit_after>
// LoL Cruncher - A Historical League of Legends Statistics Tracker
// Copyright (C) 2015  Jason Chu (1lann) 1lanncontact@gmail.com

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

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

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

package database

import (
	"github.com/revel/revel"
	"gopkg.in/mgo.v2"
	"time"
)

var IsConnected bool
var isConnecting bool
var activeSession *mgo.Session
var players *mgo.Collection
var playerIds *mgo.Collection

func isDisconnected(err string) bool {
	if err == "EOF" || err == "no reachable servers" ||
		err == "Closed explicitly" {
		return true
	} else {
		return false
	}
}

func Connect() {
	if !isConnecting {
		IsConnected = false

		if activeSession != nil {
			activeSession.Close()
		}

		isConnecting = true
		revel.INFO.Println("Connecting...")

		databaseIp, found := revel.Config.String("database.ip")

		if !found {
			revel.ERROR.Println("Missing database.ip in conf/app.conf!")
			panic("Missing database.ip in conf/app.conf!")
			return
		}

		databasePassword, hasPassword := revel.Config.String("database.password")

		if !hasPassword {
			revel.WARN.Println("No database.password in conf/app.conf, " +
				"assuming development mode with no login.")
		}

		session, err := mgo.DialWithTimeout(databaseIp, time.Second*3)
		if err != nil {
			isConnecting = false
			IsConnected = false
			revel.ERROR.Println(err)
			return
		}

		session.SetMode(mgo.Monotonic, true)
		session.SetSafe(&mgo.Safe{})
		session.SetSyncTimeout(time.Second*3)
		session.SetSocketTimeout(time.Second*3)

		activeSession = session

		if hasPassword {
			err = session.DB("cruncher").Login("webapp", databasePassword)
			if err != nil {
				revel.ERROR.Println("Database authentication failed! " +
					"Assuming database is down.")
				return
			}
		}

		players = session.DB("cruncher").C("players")
		playerIds = session.DB("cruncher").C("playerids")

		IsConnected = true
		isConnecting = false
	}
}
<|endoftext|>


<commit_before>package main

import (
	"path/filepath"
	"fmt"
	"os"
)

var workingDir string

func init() {
	var err error
	workingDir, err = filepath.Abs(".")
	if err != nil {
		panic(err)
	}
	workingDir = filepath.Clean(workingDir)
	fmt.Println(workingDir)
	mkv, err := filepath.Glob(workingDir + "/*.mkv")
	m4v, err := filepath.Glob(workingDir + "/*.m4v")
	files := append(mkv, m4v...)
	if len(files) == 0 {
		fmt.Fprintf(os.Stderr, "No mkv/m4v files found in path. Exiting.\n")
		os.Exit(1)
	}
}

func main() {
}
<commit_msg>Flesh out gomkv cli tool<commit_after>package main

import (
	"path/filepath"
	"fmt"
	"os"
	"exec"
	"handbrake"
)

var workingDir string
var files []string

func init() {
	var err error
	workingDir, err = filepath.Abs(".")
	if err != nil {
		panic(err)
	}
}

func main() {
	workingDir = filepath.Clean(workingDir)
	fmt.Println(workingDir)
	mkv, err := filepath.Glob(workingDir + "/*.mkv")
	if err != nil {
		panic(err)
	}
	m4v, err := filepath.Glob(workingDir + "/*.m4v")
	if err != nil {
		panic(err)
	}
	files = append(mkv, m4v...)
	if len(files) == 0 {
		fmt.Fprintf(os.Stderr, "No mkv/m4v files found in path. Exiting.\n")
		os.Exit(1)
	}
	std, err := exec.Command("HandBrakeCLI", "-t0", "-i", files[0])
	meta := handbrake.ParseOutput(std.Err)
	fmt.Print(meta)
}
<|endoftext|>