<commit_before>package tendermint

import (
	"github.com/tendermint/tendermint/Godeps/_workspace/src/github.com/naoina/toml"
	"os"
	"path"
	"strings"

	. "github.com/tendermint/tendermint/common"
	cfg "github.com/tendermint/tendermint/config"
)

func getTMRoot(rootDir string) string {
	if rootDir == "" {
		rootDir = os.Getenv("TMROOT")
	}
	if rootDir == "" {
		rootDir = os.Getenv("HOME") + "/.tendermint"
	}
	return rootDir
}

func initTMRoot(rootDir string) {
	rootDir = getTMRoot(rootDir)
	EnsureDir(rootDir)

	configFilePath := path.Join(rootDir, "config.toml")
	genesisFilePath := path.Join(rootDir, "genesis.json")

	// Write default config file if missing.
	if !FileExists(configFilePath) {
		// Ask user for moniker
		// moniker := cfg.Prompt("Type hostname: ", "anonymous")
		MustWriteFile(configFilePath, []byte(defaultConfig("anonymous")))
	}
	if !FileExists(genesisFilePath) {
		MustWriteFile(genesisFilePath, []byte(defaultGenesis))
	}
}

func GetConfig(rootDir string) cfg.Config {
	rootDir = getTMRoot(rootDir)
	initTMRoot(rootDir)

	var mapConfig = cfg.MapConfig(make(map[string]interface{}))
	configFilePath := path.Join(rootDir, "config.toml")
	configFileBytes := MustReadFile(configFilePath)
	err := toml.Unmarshal(configFileBytes, mapConfig)
	if err != nil {
		Exit(Fmt("Could not read config: %v", err))
	}

	// Set defaults or panic
	if mapConfig.IsSet("chain_id") {
		Exit("Cannot set 'chain_id' via config.toml")
	}
	mapConfig.SetDefault("chain_id", "tendermint_testnet_10")
	mapConfig.SetDefault("genesis_file", rootDir+"/genesis.json")
	mapConfig.SetDefault("moniker", "anonymous")
	mapConfig.SetDefault("node_laddr", "0.0.0.0:46656")
	// mapConfig.SetDefault("seeds", "goldenalchemist.chaintest.net:46656")
	mapConfig.SetDefault("fast_sync", true)
	mapConfig.SetDefault("skip_upnp", false)
	mapConfig.SetDefault("addrbook_file", rootDir+"/addrbook.json")
	mapConfig.SetDefault("priv_validator_file", rootDir+"/priv_validator.json")
	mapConfig.SetDefault("db_backend", "leveldb")
	mapConfig.SetDefault("db_dir", rootDir+"/data")
	mapConfig.SetDefault("log_level", "info")
	mapConfig.SetDefault("rpc_laddr", "0.0.0.0:46657")
	mapConfig.SetDefault("revisions_file", rootDir+"/revisions")
	return mapConfig
}

func ensureDefault(mapConfig cfg.MapConfig, key string, value interface{}) {
	if !mapConfig.IsSet(key) {
		mapConfig[key] = value
	}
}

var defaultConfigTmpl = `# This is a TOML config file.
# For more information, see https://github.com/toml-lang/toml

moniker = "__MONIKER__"
node_laddr = "0.0.0.0:46656"
seeds = "goldenalchemist.chaintest.net:46656"
fast_sync = true
db_backend = "leveldb"
log_level = "notice"
rpc_laddr = "0.0.0.0:46657"
`

func defaultConfig(moniker string) (defaultConfig string) {
	defaultConfig = strings.Replace(defaultConfigTmpl, "__MONIKER__", moniker, -1)
	return
}

var defaultGenesis = `{
    "chain_id": "tendermint_testnet_11",
    "accounts": [
        {
            "address": "9FCBA7F840A0BFEBBE755E853C9947270A912D04",
            "amount": 1995999998000000
        },
        {
            "address": "964B1493BBE3312278B7DEB94C39149F7899A345",
            "amount": 100000000000000
        },
        {
            "address": "B9FA4AB462B9C6BF6A62DB4AE77C9E7087209A04",
            "amount": 1000000000000
        },
        {
            "address": "F171824590D69386F709E7B6704B369C5A370D60",
            "amount": 1000000000000
        },
        {
            "address": "56EFE746A13D9A6054AC89C3E2A361C2DB8B9EAE",
            "amount": 1000000000000
        },
        {
            "address": "7C2E032D8407EDF66A04D88CF0E1D9B15D98AE2D",
            "amount": 1000000000000
        },
        {
            "address": "A88A61069B6660F30F65E8786AFDD4F1D8F625E9",
            "amount": 1000000
        },
        {
            "address": "EE2EE9247973B4AFC3867CFE5F415410AC251B61",
            "amount": 1000000
        }
    ],
    "validators": [
        {
            "pub_key": [1, "178EC6008A4364508979C70CBF100BD4BCBAA12DDE6251F5F486B4FD09014F06"],
            "amount": 100000000000
        },
        {
            "pub_key": [1, "2A77777CC51467DE42350D4A8F34720D527734189BE64C7A930DD169E1FED3C6"],
            "amount": 100000000000
        },
        {
            "pub_key": [1, "3718E69D09B11B3AD3FA31AEF07EC416D2AEED241CACE7B0F30AE9803FFB0F08"],
            "amount": 100000000000
        },
        {
            "pub_key": [1, "C6B0440DEACD1E4CF1C736CEB8E38E788B700BA2B2045A55CB657A455CF5F889"],
            "amount": 100000000000
        },
        {
            "pub_key": [1, "3BA1190D54F91EFBF8B0125F7EC116AD4BA2894B6EE38564A5D5FD3230D91F7B"],
            "amount": 100000000000
        }
    ]
}`
<commit_msg>added testnet addresses<commit_after>package tendermint

import (
	"github.com/tendermint/tendermint/Godeps/_workspace/src/github.com/naoina/toml"
	"os"
	"path"
	"strings"

	. "github.com/tendermint/tendermint/common"
	cfg "github.com/tendermint/tendermint/config"
)

func getTMRoot(rootDir string) string {
	if rootDir == "" {
		rootDir = os.Getenv("TMROOT")
	}
	if rootDir == "" {
		rootDir = os.Getenv("HOME") + "/.tendermint"
	}
	return rootDir
}

func initTMRoot(rootDir string) {
	rootDir = getTMRoot(rootDir)
	EnsureDir(rootDir)

	configFilePath := path.Join(rootDir, "config.toml")
	genesisFilePath := path.Join(rootDir, "genesis.json")

	// Write default config file if missing.
	if !FileExists(configFilePath) {
		// Ask user for moniker
		// moniker := cfg.Prompt("Type hostname: ", "anonymous")
		MustWriteFile(configFilePath, []byte(defaultConfig("anonymous")))
	}
	if !FileExists(genesisFilePath) {
		MustWriteFile(genesisFilePath, []byte(defaultGenesis))
	}
}

func GetConfig(rootDir string) cfg.Config {
	rootDir = getTMRoot(rootDir)
	initTMRoot(rootDir)

	var mapConfig = cfg.MapConfig(make(map[string]interface{}))
	configFilePath := path.Join(rootDir, "config.toml")
	configFileBytes := MustReadFile(configFilePath)
	err := toml.Unmarshal(configFileBytes, mapConfig)
	if err != nil {
		Exit(Fmt("Could not read config: %v", err))
	}

	// Set defaults or panic
	if mapConfig.IsSet("chain_id") {
		Exit("Cannot set 'chain_id' via config.toml")
	}
	mapConfig.SetDefault("chain_id", "tendermint_testnet_10")
	mapConfig.SetDefault("genesis_file", rootDir+"/genesis.json")
	mapConfig.SetDefault("moniker", "anonymous")
	mapConfig.SetDefault("node_laddr", "0.0.0.0:46656")
	// mapConfig.SetDefault("seeds", "goldenalchemist.chaintest.net:46656")
	mapConfig.SetDefault("fast_sync", true)
	mapConfig.SetDefault("skip_upnp", false)
	mapConfig.SetDefault("addrbook_file", rootDir+"/addrbook.json")
	mapConfig.SetDefault("priv_validator_file", rootDir+"/priv_validator.json")
	mapConfig.SetDefault("db_backend", "leveldb")
	mapConfig.SetDefault("db_dir", rootDir+"/data")
	mapConfig.SetDefault("log_level", "info")
	mapConfig.SetDefault("rpc_laddr", "0.0.0.0:46657")
	mapConfig.SetDefault("revisions_file", rootDir+"/revisions")
	return mapConfig
}

func ensureDefault(mapConfig cfg.MapConfig, key string, value interface{}) {
	if !mapConfig.IsSet(key) {
		mapConfig[key] = value
	}
}

var defaultConfigTmpl = `# This is a TOML config file.
# For more information, see https://github.com/toml-lang/toml

moniker = "__MONIKER__"
node_laddr = "0.0.0.0:46656"
seeds = "goldenalchemist.chaintest.net:46656"
fast_sync = true
db_backend = "leveldb"
log_level = "notice"
rpc_laddr = "0.0.0.0:46657"
`

func defaultConfig(moniker string) (defaultConfig string) {
	defaultConfig = strings.Replace(defaultConfigTmpl, "__MONIKER__", moniker, -1)
	return
}

var defaultGenesis = `{
    "chain_id": "tendermint_testnet_11.b",
    "accounts": [
        {
            "address": "9FCBA7F840A0BFEBBE755E853C9947270A912D04",
            "amount": 1993999998000000
        },
        {
            "address": "964B1493BBE3312278B7DEB94C39149F7899A345",
            "amount": 100000000000000
        },
        {
            "address": "B9FA4AB462B9C6BF6A62DB4AE77C9E7087209A04",
            "amount": 1000000000000
        },
        {
            "address": "F171824590D69386F709E7B6704B369C5A370D60",
            "amount": 1000000000000
        },
        {
            "address": "56EFE746A13D9A6054AC89C3E2A361C2DB8B9EAE",
            "amount": 1000000000000
        },
        {
            "address": "7C2E032D8407EDF66A04D88CF0E1D9B15D98AE2D",
            "amount": 1000000000000
        },
        {
            "address": "636EF5823E082AD66EBC203FD4DFB1031F0C61CA",
            "amount": 1000000000000
        },
        {
            "address": "9008419E6351360A59B124E707E4CA2A5BFB9BE6",
            "amount": 1000000000000
        },
        {
            "address": "A88A61069B6660F30F65E8786AFDD4F1D8F625E9",
            "amount": 1000000
        },
        {
            "address": "EE2EE9247973B4AFC3867CFE5F415410AC251B61",
            "amount": 1000000
        }
    ],
    "validators": [
        {
            "pub_key": [1, "178EC6008A4364508979C70CBF100BD4BCBAA12DDE6251F5F486B4FD09014F06"],
            "amount": 100000000000
        },
        {
            "pub_key": [1, "2A77777CC51467DE42350D4A8F34720D527734189BE64C7A930DD169E1FED3C6"],
            "amount": 100000000000
        },
        {
            "pub_key": [1, "3718E69D09B11B3AD3FA31AEF07EC416D2AEED241CACE7B0F30AE9803FFB0F08"],
            "amount": 100000000000
        },
        {
            "pub_key": [1, "C6B0440DEACD1E4CF1C736CEB8E38E788B700BA2B2045A55CB657A455CF5F889"],
            "amount": 100000000000
        },
        {
            "pub_key": [1, "3BA1190D54F91EFBF8B0125F7EC116AD4BA2894B6EE38564A5D5FD3230D91F7B"],
            "amount": 100000000000
        }
    ]
}`
<|endoftext|>


<commit_before>package main

import (
	"encoding/json"
	"fmt"
	"regexp"
	"strings"

	"github.com/fatih/color"
	"golang.org/x/net/html"
	"golang.org/x/net/html/atom"
)

func init() {
	color.Output = colorable.NewColorableStdout()
}

type Displayer interface {
	Display([]*html.Node)
}

func ParseDisplayer(cmd string) error {
	attrRe := regexp.MustCompile(`attr\{([a-zA-Z\-]+)\}`)
	if cmd == "text{}" {
		pupDisplayer = TextDisplayer{}
	} else if cmd == "json{}" {
		pupDisplayer = JSONDisplayer{}
	} else if match := attrRe.FindAllStringSubmatch(cmd, -1); len(match) == 1 {
		pupDisplayer = AttrDisplayer{
			Attr: match[0][1],
		}
	} else {
		return fmt.Errorf("Unknown displayer")
	}
	return nil
}

// Is this node a tag with no end tag such as <meta> or
?
// http://www.w3.org/TR/html-markup/syntax.html#syntax-elements
func isVoidElement(n *html.Node) bool {
	switch n.DataAtom {
	case atom.Area, atom.Base, atom.Br, atom.Col, atom.Command, atom.Embed,
		atom.Hr, atom.Img, atom.Input, atom.Keygen, atom.Link,
		atom.Meta, atom.Param, atom.Source, atom.Track, atom.Wbr:
		return true
	}
	return false
}

var (
	// Colors
	tagColor     *color.Color = color.New(color.FgCyan)
	tokenColor                = color.New(color.FgCyan)
	attrKeyColor              = color.New(color.FgMagenta)
	quoteColor                = color.New(color.FgBlue)
	commentColor              = color.New(color.FgYellow)
)

type TreeDisplayer struct {
}

func (t TreeDisplayer) Display(nodes []*html.Node) {
	for _, node := range nodes {
		t.printNode(node, 0)
	}
}

// The
 tag indicates that the text within it should always be formatted
// as is. See https://github.com/ericchiang/pup/issues/33
func (t TreeDisplayer) printPre(n *html.Node) {
	switch n.Type {
	case html.TextNode:
		s := n.Data
		if pupEscapeHTML {
			// don't escape javascript
			if n.Parent == nil || n.Parent.DataAtom != atom.Script {
				s = html.EscapeString(s)
			}
		}
		fmt.Print(s)
		for c := n.FirstChild; c != nil; c = c.NextSibling {
			t.printPre(c)
		}
	case html.ElementNode:
		fmt.Printf("<%s", n.Data)
		for _, a := range n.Attr {
			val := a.Val
			if pupEscapeHTML {
				val = html.EscapeString(val)
			}
			fmt.Printf(` %s="%s"`, a.Key, val)
		}
		fmt.Print(">")
		if !isVoidElement(n) {
			for c := n.FirstChild; c != nil; c = c.NextSibling {
				t.printPre(c)
			}
			fmt.Printf("</%s>", n.Data)
		}
	case html.CommentNode:
		data := n.Data
		if pupEscapeHTML {
			data = html.EscapeString(data)
		}
		fmt.Printf("\n", data)
		for c := n.FirstChild; c != nil; c = c.NextSibling {
			t.printPre(c)
		}
	case html.DoctypeNode, html.DocumentNode:
		for c := n.FirstChild; c != nil; c = c.NextSibling {
			t.printPre(c)
		}
	}
}

// Print a node and all of it's children to `maxlevel`.
func (t TreeDisplayer) printNode(n *html.Node, level int) {
	switch n.Type {
	case html.TextNode:
		s := n.Data
		if pupEscapeHTML {
			// don't escape javascript
			if n.Parent == nil || n.Parent.DataAtom != atom.Script {
				s = html.EscapeString(s)
			}
		}
		s = strings.TrimSpace(s)
		if s != "" {
			t.printIndent(level)
			fmt.Println(s)
		}
	case html.ElementNode:
		t.printIndent(level)
		// TODO: allow pre with color
		if n.DataAtom == atom.Pre && !pupPrintColor && pupPreformatted {
			t.printPre(n)
			fmt.Println()
			return
		}
		if pupPrintColor {
			tokenColor.Print("<")
			tagColor.Printf("%s", n.Data)
		} else {
			fmt.Printf("<%s", n.Data)
		}
		for _, a := range n.Attr {
			val := a.Val
			if pupEscapeHTML {
				val = html.EscapeString(val)
			}
			if pupPrintColor {
				fmt.Print(" ")
				attrKeyColor.Printf("%s", a.Key)
				tokenColor.Print("=")
				quoteColor.Printf(`"%s"`, val)
			} else {
				fmt.Printf(` %s="%s"`, a.Key, val)
			}
		}
		if pupPrintColor {
			tokenColor.Println(">")
		} else {
			fmt.Println(">")
		}
		if !isVoidElement(n) {
			t.printChildren(n, level+1)
			t.printIndent(level)
			if pupPrintColor {
				tokenColor.Print("</")
				tagColor.Printf("%s", n.Data)
				tokenColor.Println(">")
			} else {
				fmt.Printf("</%s>\n", n.Data)
			}
		}
	case html.CommentNode:
		t.printIndent(level)
		data := n.Data
		if pupEscapeHTML {
			data = html.EscapeString(data)
		}
		if pupPrintColor {
			commentColor.Printf("\n", data)
		} else {
			fmt.Printf("\n", data)
		}
		t.printChildren(n, level)
	case html.DoctypeNode, html.DocumentNode:
		t.printChildren(n, level)
	}
}

func (t TreeDisplayer) printChildren(n *html.Node, level int) {
	if pupMaxPrintLevel > -1 {
		if level >= pupMaxPrintLevel {
			t.printIndent(level)
			fmt.Println("...")
			return
		}
	}
	child := n.FirstChild
	for child != nil {
		t.printNode(child, level)
		child = child.NextSibling
	}
}

func (t TreeDisplayer) printIndent(level int) {
	for ; level > 0; level-- {
		fmt.Print(pupIndentString)
	}
}

// Print the text of a node
type TextDisplayer struct{}

func (t TextDisplayer) Display(nodes []*html.Node) {
	for _, node := range nodes {
		if node.Type == html.TextNode {
			data := node.Data
			if pupEscapeHTML {
				// don't escape javascript
				if node.Parent == nil || node.Parent.DataAtom != atom.Script {
					data = html.EscapeString(data)
				}
			}
			fmt.Println(data)
		}
		children := []*html.Node{}
		child := node.FirstChild
		for child != nil {
			children = append(children, child)
			child = child.NextSibling
		}
		t.Display(children)
	}
}

// Print the attribute of a node
type AttrDisplayer struct {
	Attr string
}

func (a AttrDisplayer) Display(nodes []*html.Node) {
	for _, node := range nodes {
		attributes := node.Attr
		for _, attr := range attributes {
			if attr.Key == a.Attr {
				val := attr.Val
				if pupEscapeHTML {
					val = html.EscapeString(val)
				}
				fmt.Printf("%s\n", val)
			}
		}
	}
}

// Print nodes as a JSON list
type JSONDisplayer struct{}

// returns a jsonifiable struct
func jsonify(node *html.Node) map[string]interface{} {
	vals := map[string]interface{}{}
	if len(node.Attr) > 0 {
		for _, attr := range node.Attr {
			if pupEscapeHTML {
				vals[attr.Key] = html.EscapeString(attr.Val)
			} else {
				vals[attr.Key] = attr.Val
			}
		}
	}
	vals["tag"] = node.DataAtom.String()
	children := []interface{}{}
	for child := node.FirstChild; child != nil; child = child.NextSibling {
		switch child.Type {
		case html.ElementNode:
			children = append(children, jsonify(child))
		case html.TextNode:
			text := strings.TrimSpace(child.Data)
			if text != "" {
				if pupEscapeHTML {
					// don't escape javascript
					if node.DataAtom != atom.Script {
						text = html.EscapeString(text)
					}
				}
				// if there is already text we'll append it
				currText, ok := vals["text"]
				if ok {
					text = fmt.Sprintf("%s %s", currText, text)
				}
				vals["text"] = text
			}
		case html.CommentNode:
			comment := strings.TrimSpace(child.Data)
			if pupEscapeHTML {
				comment = html.EscapeString(comment)
			}
			currComment, ok := vals["comment"]
			if ok {
				comment = fmt.Sprintf("%s %s", currComment, comment)
			}
			vals["comment"] = comment
		}
	}
	if len(children) > 0 {
		vals["children"] = children
	}
	return vals
}

func (j JSONDisplayer) Display(nodes []*html.Node) {
	var data []byte
	var err error
	jsonNodes := []map[string]interface{}{}
	for _, node := range nodes {
		jsonNodes = append(jsonNodes, jsonify(node))
	}
	data, err = json.MarshalIndent(&jsonNodes, "", pupIndentString)
	if err != nil {
		panic("Could not jsonify nodes")
	}
	fmt.Printf("%s\n", data)
}

// Print the number of features returned
type NumDisplayer struct{}

func (d NumDisplayer) Display(nodes []*html.Node) {
	fmt.Println(len(nodes))
}
<commit_msg>fix missing import<commit_after>package main

import (
	"encoding/json"
	"fmt"
	"regexp"
	"strings"

	"github.com/fatih/color"
	colorable "github.com/mattn/go-colorable"
	"golang.org/x/net/html"
	"golang.org/x/net/html/atom"
)

func init() {
	color.Output = colorable.NewColorableStdout()
}

type Displayer interface {
	Display([]*html.Node)
}

func ParseDisplayer(cmd string) error {
	attrRe := regexp.MustCompile(`attr\{([a-zA-Z\-]+)\}`)
	if cmd == "text{}" {
		pupDisplayer = TextDisplayer{}
	} else if cmd == "json{}" {
		pupDisplayer = JSONDisplayer{}
	} else if match := attrRe.FindAllStringSubmatch(cmd, -1); len(match) == 1 {
		pupDisplayer = AttrDisplayer{
			Attr: match[0][1],
		}
	} else {
		return fmt.Errorf("Unknown displayer")
	}
	return nil
}

// Is this node a tag with no end tag such as <meta> or
?
// http://www.w3.org/TR/html-markup/syntax.html#syntax-elements
func isVoidElement(n *html.Node) bool {
	switch n.DataAtom {
	case atom.Area, atom.Base, atom.Br, atom.Col, atom.Command, atom.Embed,
		atom.Hr, atom.Img, atom.Input, atom.Keygen, atom.Link,
		atom.Meta, atom.Param, atom.Source, atom.Track, atom.Wbr:
		return true
	}
	return false
}

var (
	// Colors
	tagColor     *color.Color = color.New(color.FgCyan)
	tokenColor                = color.New(color.FgCyan)
	attrKeyColor              = color.New(color.FgMagenta)
	quoteColor                = color.New(color.FgBlue)
	commentColor              = color.New(color.FgYellow)
)

type TreeDisplayer struct {
}

func (t TreeDisplayer) Display(nodes []*html.Node) {
	for _, node := range nodes {
		t.printNode(node, 0)
	}
}

// The
 tag indicates that the text within it should always be formatted
// as is. See https://github.com/ericchiang/pup/issues/33
func (t TreeDisplayer) printPre(n *html.Node) {
	switch n.Type {
	case html.TextNode:
		s := n.Data
		if pupEscapeHTML {
			// don't escape javascript
			if n.Parent == nil || n.Parent.DataAtom != atom.Script {
				s = html.EscapeString(s)
			}
		}
		fmt.Print(s)
		for c := n.FirstChild; c != nil; c = c.NextSibling {
			t.printPre(c)
		}
	case html.ElementNode:
		fmt.Printf("<%s", n.Data)
		for _, a := range n.Attr {
			val := a.Val
			if pupEscapeHTML {
				val = html.EscapeString(val)
			}
			fmt.Printf(` %s="%s"`, a.Key, val)
		}
		fmt.Print(">")
		if !isVoidElement(n) {
			for c := n.FirstChild; c != nil; c = c.NextSibling {
				t.printPre(c)
			}
			fmt.Printf("</%s>", n.Data)
		}
	case html.CommentNode:
		data := n.Data
		if pupEscapeHTML {
			data = html.EscapeString(data)
		}
		fmt.Printf("\n", data)
		for c := n.FirstChild; c != nil; c = c.NextSibling {
			t.printPre(c)
		}
	case html.DoctypeNode, html.DocumentNode:
		for c := n.FirstChild; c != nil; c = c.NextSibling {
			t.printPre(c)
		}
	}
}

// Print a node and all of it's children to `maxlevel`.
func (t TreeDisplayer) printNode(n *html.Node, level int) {
	switch n.Type {
	case html.TextNode:
		s := n.Data
		if pupEscapeHTML {
			// don't escape javascript
			if n.Parent == nil || n.Parent.DataAtom != atom.Script {
				s = html.EscapeString(s)
			}
		}
		s = strings.TrimSpace(s)
		if s != "" {
			t.printIndent(level)
			fmt.Println(s)
		}
	case html.ElementNode:
		t.printIndent(level)
		// TODO: allow pre with color
		if n.DataAtom == atom.Pre && !pupPrintColor && pupPreformatted {
			t.printPre(n)
			fmt.Println()
			return
		}
		if pupPrintColor {
			tokenColor.Print("<")
			tagColor.Printf("%s", n.Data)
		} else {
			fmt.Printf("<%s", n.Data)
		}
		for _, a := range n.Attr {
			val := a.Val
			if pupEscapeHTML {
				val = html.EscapeString(val)
			}
			if pupPrintColor {
				fmt.Print(" ")
				attrKeyColor.Printf("%s", a.Key)
				tokenColor.Print("=")
				quoteColor.Printf(`"%s"`, val)
			} else {
				fmt.Printf(` %s="%s"`, a.Key, val)
			}
		}
		if pupPrintColor {
			tokenColor.Println(">")
		} else {
			fmt.Println(">")
		}
		if !isVoidElement(n) {
			t.printChildren(n, level+1)
			t.printIndent(level)
			if pupPrintColor {
				tokenColor.Print("</")
				tagColor.Printf("%s", n.Data)
				tokenColor.Println(">")
			} else {
				fmt.Printf("</%s>\n", n.Data)
			}
		}
	case html.CommentNode:
		t.printIndent(level)
		data := n.Data
		if pupEscapeHTML {
			data = html.EscapeString(data)
		}
		if pupPrintColor {
			commentColor.Printf("\n", data)
		} else {
			fmt.Printf("\n", data)
		}
		t.printChildren(n, level)
	case html.DoctypeNode, html.DocumentNode:
		t.printChildren(n, level)
	}
}

func (t TreeDisplayer) printChildren(n *html.Node, level int) {
	if pupMaxPrintLevel > -1 {
		if level >= pupMaxPrintLevel {
			t.printIndent(level)
			fmt.Println("...")
			return
		}
	}
	child := n.FirstChild
	for child != nil {
		t.printNode(child, level)
		child = child.NextSibling
	}
}

func (t TreeDisplayer) printIndent(level int) {
	for ; level > 0; level-- {
		fmt.Print(pupIndentString)
	}
}

// Print the text of a node
type TextDisplayer struct{}

func (t TextDisplayer) Display(nodes []*html.Node) {
	for _, node := range nodes {
		if node.Type == html.TextNode {
			data := node.Data
			if pupEscapeHTML {
				// don't escape javascript
				if node.Parent == nil || node.Parent.DataAtom != atom.Script {
					data = html.EscapeString(data)
				}
			}
			fmt.Println(data)
		}
		children := []*html.Node{}
		child := node.FirstChild
		for child != nil {
			children = append(children, child)
			child = child.NextSibling
		}
		t.Display(children)
	}
}

// Print the attribute of a node
type AttrDisplayer struct {
	Attr string
}

func (a AttrDisplayer) Display(nodes []*html.Node) {
	for _, node := range nodes {
		attributes := node.Attr
		for _, attr := range attributes {
			if attr.Key == a.Attr {
				val := attr.Val
				if pupEscapeHTML {
					val = html.EscapeString(val)
				}
				fmt.Printf("%s\n", val)
			}
		}
	}
}

// Print nodes as a JSON list
type JSONDisplayer struct{}

// returns a jsonifiable struct
func jsonify(node *html.Node) map[string]interface{} {
	vals := map[string]interface{}{}
	if len(node.Attr) > 0 {
		for _, attr := range node.Attr {
			if pupEscapeHTML {
				vals[attr.Key] = html.EscapeString(attr.Val)
			} else {
				vals[attr.Key] = attr.Val
			}
		}
	}
	vals["tag"] = node.DataAtom.String()
	children := []interface{}{}
	for child := node.FirstChild; child != nil; child = child.NextSibling {
		switch child.Type {
		case html.ElementNode:
			children = append(children, jsonify(child))
		case html.TextNode:
			text := strings.TrimSpace(child.Data)
			if text != "" {
				if pupEscapeHTML {
					// don't escape javascript
					if node.DataAtom != atom.Script {
						text = html.EscapeString(text)
					}
				}
				// if there is already text we'll append it
				currText, ok := vals["text"]
				if ok {
					text = fmt.Sprintf("%s %s", currText, text)
				}
				vals["text"] = text
			}
		case html.CommentNode:
			comment := strings.TrimSpace(child.Data)
			if pupEscapeHTML {
				comment = html.EscapeString(comment)
			}
			currComment, ok := vals["comment"]
			if ok {
				comment = fmt.Sprintf("%s %s", currComment, comment)
			}
			vals["comment"] = comment
		}
	}
	if len(children) > 0 {
		vals["children"] = children
	}
	return vals
}

func (j JSONDisplayer) Display(nodes []*html.Node) {
	var data []byte
	var err error
	jsonNodes := []map[string]interface{}{}
	for _, node := range nodes {
		jsonNodes = append(jsonNodes, jsonify(node))
	}
	data, err = json.MarshalIndent(&jsonNodes, "", pupIndentString)
	if err != nil {
		panic("Could not jsonify nodes")
	}
	fmt.Printf("%s\n", data)
}

// Print the number of features returned
type NumDisplayer struct{}

func (d NumDisplayer) Display(nodes []*html.Node) {
	fmt.Println(len(nodes))
}
<|endoftext|>


<commit_before>package testhelper

import "testing"

func Compare(t *testing.T, expected interface{}, actual interface{}) {
	if expected != actual {
		t.Fatalf("Expected [%#v] but got [%#v]", expected, actual)
	}
}

func CheckErr(t *testing.T, e error) {
	if e != nil {
		t.Fatalf("Unexpected error: %#v", e)
	}
}
<commit_msg>Add comments<commit_after>package testhelper

import "testing"

// This function compares two arbitrary values and performs a comparison. If the
// comparison fails, a fatal error is raised that will fail the test
func Compare(t *testing.T, expected interface{}, actual interface{}) {
	if expected != actual {
		t.Fatalf("Expected [%#v] but got [%#v]", expected, actual)
	}
}

// A convenience function for checking whether an error value is an actual error
func CheckErr(t *testing.T, e error) {
	if e != nil {
		t.Fatalf("Unexpected error: %#v", e)
	}
}
<|endoftext|>


<commit_before>package io_test

import (
	"os"

	. "github.com/cloudfoundry/cli/testhelpers/io"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("io helpers", func() {
	It("will never overflow the pipe", func() {
		str := ""
		for i := 0; i < 75000; i++ {
			str += "abc"
		}

		output := CaptureOutput(func() {
			os.Stdout.Write([]byte(str))
		})

		Expect(output).To(Equal([]string{str}))
	})
})
<commit_msg>Speed up this test by a factor of a lot<commit_after>package io_test

import (
	"os"
	"strings"

	. "github.com/cloudfoundry/cli/testhelpers/io"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("io helpers", func() {
	It("will never overflow the pipe", func() {
		characters := make([]string, 0, 75000)
		for i := 0; i < 75000; i++ {
			characters = append(characters, "z")
		}

		str := strings.Join(characters, "")

		output := CaptureOutput(func() {
			os.Stdout.Write([]byte(str))
		})

		Expect(output).To(Equal([]string{str}))
	})
})
<|endoftext|>


<commit_before>package tests

import (
	"fmt"
	"testing"
	"time"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/portworx/torpedo/drivers/node"
	"github.com/portworx/torpedo/drivers/scheduler"
	. "github.com/portworx/torpedo/tests"
)

func TestBasic(t *testing.T) {
	RegisterFailHandler(Fail)
	RunSpecs(t, "Torpedo : Basic")
}

var _ = BeforeSuite(func() {
	InitInstance()
})

// This test performs basic test of starting an application and destroying it (along with storage)
var _ = Describe("Setup and teardown", func() {
	It("has to setup, validate and teardown apps", func() {
		var contexts []*scheduler.Context
		contexts = ScheduleAndValidate("setupteardown")

		opts := make(map[string]bool)
		opts[scheduler.OptionsWaitForResourceLeakCleanup] = true

		for _, ctx := range contexts {
			TearDownContext(ctx, opts)
		}
	})
})

// Volume Driver Plugin is down, unavailable - and the client container should not be impacted.
var _ = Describe("Volume driver down", func() {
	It("has to schedule apps and stop volume driver on app nodes", func() {
		var err error
		contexts := ScheduleAndValidate("voldriverdown")

		Step("get nodes for all apps in test and bounce volume driver", func() {
			for _, ctx := range contexts {
				var appNodes []node.Node
				Step(fmt.Sprintf("get nodes for %s app", ctx.App.Key), func() {
					appNodes, err = Inst().S.GetNodesForApp(ctx)
					Expect(err).NotTo(HaveOccurred())
					Expect(appNodes).NotTo(BeEmpty())
				})

				Step(fmt.Sprintf("stop volume driver %s on app %s's nodes: %v",
					Inst().V.String(), ctx.App.Key, appNodes), func() {
					for _, n := range appNodes {
						err = Inst().V.StopDriver(n)
						Expect(err).NotTo(HaveOccurred())
					}

					Step("wait for few seconds for driver to go down", func() {
						time.Sleep(10 * time.Second)
					})
				})

				Step("restarting volume driver", func() {
					StartVolDriverAndWait(appNodes)
				})
			}
		})

		Step("destroy apps", func() {
			opts := make(map[string]bool)
			opts[scheduler.OptionsWaitForResourceLeakCleanup] = true
			for _, ctx := range contexts {
				TearDownContext(ctx, opts)
			}
		})

	})
})

// Volume driver plugin is down and the client container gets terminated.
// There is a lost unmount call in this case. When the volume driver is
// back up, we should be able to detach and delete the volume.
var _ = PDescribe("Volume driver and app down", func() {
	It("has to schedule apps, stop volume driver on app nodes and destroy apps", func() {
		var err error
		contexts := ScheduleAndValidate("voldriverappdown")

		Step("get nodes for all apps in test and bounce volume driver", func() {
			for _, ctx := range contexts {
				var appNodes []node.Node
				Step(fmt.Sprintf("get nodes for %s app", ctx.App.Key), func() {
					appNodes, err = Inst().S.GetNodesForApp(ctx)
					Expect(err).NotTo(HaveOccurred())
					Expect(appNodes).NotTo(BeEmpty())
				})

				Step(fmt.Sprintf("stop volume driver %s on app %s's nodes: %v",
					Inst().V.String(), ctx.App.Key, appNodes), func() {
					for _, n := range appNodes {
						err = Inst().V.StopDriver(n)
						Expect(err).NotTo(HaveOccurred())
					}

					Step("wait for few seconds for driver to go down", func() {
						time.Sleep(20 * time.Second)
					})
				})

				Step(fmt.Sprintf("destroy app: %s", ctx.App.Key), func() {
					err = Inst().S.Destroy(ctx, nil)
					Expect(err).NotTo(HaveOccurred())

					Step("wait for few seconds for app destroy to trigger", func() {
						time.Sleep(10 * time.Second)
					})
				})

				Step("restarting volume driver", func() {
					StartVolDriverAndWait(appNodes)
				})
			}
		})

		Step("destroy apps", func() {
			opts := make(map[string]bool)
			opts[scheduler.OptionsWaitForDestroy] = true
			for _, ctx := range contexts {
				TearDownContext(ctx, opts)
			}
		})

	})
})

// This test deletes all tasks of an application and checks if app converges back to desired state
var _ = Describe("App tasks down", func() {
	It("has to schedule app and delete app tasks", func() {
		var err error
		var contexts []*scheduler.Context
		contexts = ScheduleAndValidate("apptasksdown")

		Step("delete all application tasks", func() {
			for _, ctx := range contexts {
				Step(fmt.Sprintf("delete tasks for app: %s", ctx.App.Key), func() {
					err = Inst().S.DeleteTasks(ctx)
					Expect(err).NotTo(HaveOccurred())
				})

				ValidateAndDestroy(ctx, nil)
			}
		})
	})
})

var _ = AfterSuite(func() {
	ValidateCleanup()
})

func init() {
	ParseFlags()
}
<commit_msg>Re-enable Volume driver and app down<commit_after>package tests

import (
	"fmt"
	"testing"
	"time"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/portworx/torpedo/drivers/node"
	"github.com/portworx/torpedo/drivers/scheduler"
	. "github.com/portworx/torpedo/tests"
)

func TestBasic(t *testing.T) {
	RegisterFailHandler(Fail)
	RunSpecs(t, "Torpedo : Basic")
}

var _ = BeforeSuite(func() {
	InitInstance()
})

// This test performs basic test of starting an application and destroying it (along with storage)
var _ = Describe("Setup and teardown", func() {
	It("has to setup, validate and teardown apps", func() {
		var contexts []*scheduler.Context
		contexts = ScheduleAndValidate("setupteardown")

		opts := make(map[string]bool)
		opts[scheduler.OptionsWaitForResourceLeakCleanup] = true

		for _, ctx := range contexts {
			TearDownContext(ctx, opts)
		}
	})
})

// Volume Driver Plugin is down, unavailable - and the client container should not be impacted.
var _ = Describe("Volume driver down", func() {
	It("has to schedule apps and stop volume driver on app nodes", func() {
		var err error
		contexts := ScheduleAndValidate("voldriverdown")

		Step("get nodes for all apps in test and bounce volume driver", func() {
			for _, ctx := range contexts {
				var appNodes []node.Node
				Step(fmt.Sprintf("get nodes for %s app", ctx.App.Key), func() {
					appNodes, err = Inst().S.GetNodesForApp(ctx)
					Expect(err).NotTo(HaveOccurred())
					Expect(appNodes).NotTo(BeEmpty())
				})

				Step(fmt.Sprintf("stop volume driver %s on app %s's nodes: %v",
					Inst().V.String(), ctx.App.Key, appNodes), func() {
					for _, n := range appNodes {
						err = Inst().V.StopDriver(n)
						Expect(err).NotTo(HaveOccurred())
					}

					Step("wait for few seconds for driver to go down", func() {
						time.Sleep(10 * time.Second)
					})
				})

				Step("restarting volume driver", func() {
					StartVolDriverAndWait(appNodes)
				})
			}
		})

		Step("destroy apps", func() {
			opts := make(map[string]bool)
			opts[scheduler.OptionsWaitForResourceLeakCleanup] = true
			for _, ctx := range contexts {
				TearDownContext(ctx, opts)
			}
		})

	})
})

// Volume driver plugin is down and the client container gets terminated.
// There is a lost unmount call in this case. When the volume driver is
// back up, we should be able to detach and delete the volume.
var _ = Describe("Volume driver and app down", func() {
	It("has to schedule apps, stop volume driver on app nodes and destroy apps", func() {
		var err error
		contexts := ScheduleAndValidate("voldriverappdown")

		Step("get nodes for all apps in test and bounce volume driver", func() {
			for _, ctx := range contexts {
				var appNodes []node.Node
				Step(fmt.Sprintf("get nodes for %s app", ctx.App.Key), func() {
					appNodes, err = Inst().S.GetNodesForApp(ctx)
					Expect(err).NotTo(HaveOccurred())
					Expect(appNodes).NotTo(BeEmpty())
				})

				Step(fmt.Sprintf("stop volume driver %s on app %s's nodes: %v",
					Inst().V.String(), ctx.App.Key, appNodes), func() {
					for _, n := range appNodes {
						err = Inst().V.StopDriver(n)
						Expect(err).NotTo(HaveOccurred())
					}

					Step("wait for few seconds for driver to go down", func() {
						time.Sleep(20 * time.Second)
					})
				})

				Step(fmt.Sprintf("destroy app: %s", ctx.App.Key), func() {
					err = Inst().S.Destroy(ctx, nil)
					Expect(err).NotTo(HaveOccurred())

					Step("wait for few seconds for app destroy to trigger", func() {
						time.Sleep(10 * time.Second)
					})
				})

				Step("restarting volume driver", func() {
					StartVolDriverAndWait(appNodes)
				})
			}
		})

		Step("destroy apps", func() {
			opts := make(map[string]bool)
			opts[scheduler.OptionsWaitForDestroy] = true
			for _, ctx := range contexts {
				TearDownContext(ctx, opts)
			}
		})

	})
})

// This test deletes all tasks of an application and checks if app converges back to desired state
var _ = Describe("App tasks down", func() {
	It("has to schedule app and delete app tasks", func() {
		var err error
		var contexts []*scheduler.Context
		contexts = ScheduleAndValidate("apptasksdown")

		Step("delete all application tasks", func() {
			for _, ctx := range contexts {
				Step(fmt.Sprintf("delete tasks for app: %s", ctx.App.Key), func() {
					err = Inst().S.DeleteTasks(ctx)
					Expect(err).NotTo(HaveOccurred())
				})

				ValidateAndDestroy(ctx, nil)
			}
		})
	})
})

var _ = AfterSuite(func() {
	ValidateCleanup()
})

func init() {
	ParseFlags()
}
<|endoftext|>


<commit_before>package browsertests

import "testing"

func TestViewContactform(t *testing.T) {
	b := setup(t)
	Must(b.VisitLink("Contact"), "Could not open contact formular", t)
	Must(b.Contains("A contact form!"), "Contactform body not present", t)
}
<commit_msg>Fix selenium tests.<commit_after>package browsertests

import "testing"

func TestViewContactform(t *testing.T) {
	b := setup(t)
	Must(b.VisitLink("Node Types"), "Could not open node types", t)
	Must(b.VisitLink("Contact Form"), "Could not open contact formular", t)
	Must(b.Contains("A contact form!"), "Contactform body not present", t)
}
<|endoftext|>


<commit_before>package cl11

import (
	"fmt"
	"reflect"
	"unsafe"

	clw "github.com/rdwilliamson/clw11"
)

type Kernel struct {
	id            clw.Kernel
	Arguments     int
	FunctionName  string
	Context       *Context
	Program       *Program
	WorkGroupInfo []KernelWorkGroupInfo
	argScratch    [][8]byte
}

type KernelWorkGroupInfo struct {
	Device                         *Device
	WorkGroupSize                  int
	CompileWorkGroupSize           [3]int
	LocalMemSize                   int
	PreferredWorkGroupSizeMultiple int
	PrivateMemSize                 int
}

type LocalSpaceArg int

func (p *Program) CreateKernel(name string) (*Kernel, error) {

	kernel, err := clw.CreateKernel(p.id, name)
	if err != nil {
		return nil, err
	}

	k := &Kernel{id: kernel, FunctionName: name, Context: p.Context, Program: p,
		WorkGroupInfo: make([]KernelWorkGroupInfo, len(p.Devices))}
	for i := range p.Devices {
		k.WorkGroupInfo[i].Device = p.Devices[i]
	}

	err = k.getAllInfo()
	if err != nil {
		return nil, err
	}

	return k, nil
}

func (k *Kernel) getAllInfo() (err error) {
	defer func() {
		if r := recover(); r != nil {
			err = r.(error)
		}
	}()

	k.Arguments = int(k.getUint(clw.KernelNumArgs))
	k.argScratch = make([][8]byte, k.Arguments)

	for i := range k.WorkGroupInfo {
		wgi := &k.WorkGroupInfo[i]

		wgi.WorkGroupSize = int(k.getWorkGroupSize(wgi.Device, clw.KernelWorkGroupSize))
		wgi.LocalMemSize = int(k.getWorkGroupUlong(wgi.Device, clw.KernelLocalMemSize))
		wgi.PreferredWorkGroupSizeMultiple =
			int(k.getWorkGroupSize(wgi.Device, clw.KernelPreferredWorkGroupSizeMultiple))
		wgi.PrivateMemSize = int(k.getWorkGroupUlong(wgi.Device, clw.KernelPrivateMemSize))

		cwgs := k.getWorkGroupSize3(wgi.Device, clw.KernelCompileWorkGroupSize)
		wgi.CompileWorkGroupSize[0] = int(cwgs[0])
		wgi.CompileWorkGroupSize[1] = int(cwgs[1])
		wgi.CompileWorkGroupSize[2] = int(cwgs[2])
	}

	return
}

func (k *Kernel) getUint(paramName clw.KernelInfo) clw.Uint {
	var param clw.Uint
	err := clw.GetKernelInfo(k.id, clw.KernelNumArgs, clw.Size(unsafe.Sizeof(param)), unsafe.Pointer(&param), nil)
	if err != nil {
		panic(err)
	}
	return param
}

func (k *Kernel) getWorkGroupSize(d *Device, paramName clw.KernelWorkGroupInfo) clw.Size {
	var param clw.Size
	err := clw.GetKernelWorkGroupInfo(k.id, d.id, paramName, clw.Size(unsafe.Sizeof(param)), unsafe.Pointer(&param),
		nil)
	if err != nil {
		panic(err)
	}
	return param
}

func (k *Kernel) getWorkGroupSize3(d *Device, paramName clw.KernelWorkGroupInfo) [3]clw.Size {
	var param [3]clw.Size
	err := clw.GetKernelWorkGroupInfo(k.id, d.id, paramName, clw.Size(unsafe.Sizeof(param)), unsafe.Pointer(&param),
		nil)
	if err != nil {
		panic(err)
	}
	return param
}

func (k *Kernel) getWorkGroupUlong(d *Device, paramName clw.KernelWorkGroupInfo) clw.Ulong {
	var param clw.Ulong
	err := clw.GetKernelWorkGroupInfo(k.id, d.id, paramName, clw.Size(unsafe.Sizeof(param)), unsafe.Pointer(&param),
		nil)
	if err != nil {
		panic(err)
	}
	return param
}

func (k *Kernel) SetArgument(index int, arg interface{}) error {

	var size uintptr
	var pointer unsafe.Pointer

	switch v := arg.(type) {

	case *Buffer:
		pointer = unsafe.Pointer(&v.id)
		size = unsafe.Sizeof(v.id)

	case LocalSpaceArg:
		pointer = nil
		size = uintptr(v)

	default:
		value := reflect.ValueOf(arg)
		kind := value.Kind()
		for kind == reflect.Ptr || kind == reflect.Interface {
			value = value.Elem()
			kind = value.Kind()
		}

		switch kind {

		case reflect.Bool:
			pointer = unsafe.Pointer(&k.argScratch[index][0])
			size = int32Size

			localCopy := reflect.NewAt(int32Type, pointer)
			if value.Bool() {
				localCopy.SetInt(1)
			} else {
				localCopy.SetInt(0)
			}

		case reflect.Int:
			pointer = unsafe.Pointer(&k.argScratch[index][0])
			size = int32Size

			localCopy := reflect.NewAt(int32Type, pointer)
			localCopy.SetInt(value.Int())

		case reflect.Uint:
			pointer = unsafe.Pointer(&k.argScratch[index][0])
			size = uint32Size

			localCopy := reflect.NewAt(uint32Type, pointer)
			localCopy.SetUint(value.Uint())

		case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint8, reflect.Uint16, reflect.Uint32,
			reflect.Uint64, reflect.Float32, reflect.Float64:

			localType := value.Type()

			pointer = unsafe.Pointer(&k.argScratch[index][0])
			size = localType.Size()

			localCopy := reflect.NewAt(localType, pointer)
			localCopy.Set(value)

		default:
			return wrapError(fmt.Errorf("invaild argument kind: %s", kind.String()))
		}
	}

	return clw.SetKernelArg(k.id, clw.Uint(index), clw.Size(size), pointer)
}

func (cq *CommandQueue) EnqueueNDRangeKernel(k *Kernel, globalOffset, globalSize, localSize []int,
	waitList []*Event, e *Event) error {

	var event *clw.Event
	if e != nil {
		event = &e.id
		e.Context = cq.Context
		e.CommandType = CommandNDRangeKernel
		e.CommandQueue = cq
	}

	dims := len(globalSize)
	sizes := make([]clw.Size, dims*3)
	if globalOffset != nil {
		for i := 0; i < dims; i++ {
			sizes[i] = clw.Size(globalOffset[i])
		}
	}
	for i := 0; i < dims; i++ {
		sizes[dims+i] = clw.Size(globalSize[i])
		sizes[2*dims+i] = clw.Size(localSize[i])
	}

	return clw.EnqueueNDRangeKernel(cq.id, k.id, sizes[:dims], sizes[dims:2*dims], sizes[2*dims:],
		cq.toEvents(waitList), event)
}
<commit_msg>Cleaned up a bit.<commit_after>package cl11

import (
	"fmt"
	"reflect"
	"unsafe"

	clw "github.com/rdwilliamson/clw11"
)

type Kernel struct {
	id            clw.Kernel
	Arguments     int
	FunctionName  string
	Context       *Context
	Program       *Program
	WorkGroupInfo []KernelWorkGroupInfo
	argScratch    [][8]byte
}

type KernelWorkGroupInfo struct {
	Device                         *Device
	WorkGroupSize                  int
	CompileWorkGroupSize           [3]int
	LocalMemSize                   int
	PreferredWorkGroupSizeMultiple int
	PrivateMemSize                 int
}

type LocalSpaceArg int

func (p *Program) CreateKernel(name string) (*Kernel, error) {

	kernel, err := clw.CreateKernel(p.id, name)
	if err != nil {
		return nil, err
	}

	k := &Kernel{id: kernel, FunctionName: name, Context: p.Context, Program: p,
		WorkGroupInfo: make([]KernelWorkGroupInfo, len(p.Devices))}
	for i := range p.Devices {
		k.WorkGroupInfo[i].Device = p.Devices[i]
	}

	err = k.getAllInfo()
	if err != nil {
		return nil, err
	}

	return k, nil
}

func (k *Kernel) getAllInfo() (err error) {
	defer func() {
		if r := recover(); r != nil {
			err = r.(error)
		}
	}()

	k.Arguments = int(k.getUint(clw.KernelNumArgs))
	k.argScratch = make([][8]byte, k.Arguments)

	for i := range k.WorkGroupInfo {
		wgi := &k.WorkGroupInfo[i]

		wgi.WorkGroupSize = int(k.getWorkGroupSize(wgi.Device, clw.KernelWorkGroupSize))
		wgi.LocalMemSize = int(k.getWorkGroupUlong(wgi.Device, clw.KernelLocalMemSize))
		wgi.PreferredWorkGroupSizeMultiple =
			int(k.getWorkGroupSize(wgi.Device, clw.KernelPreferredWorkGroupSizeMultiple))
		wgi.PrivateMemSize = int(k.getWorkGroupUlong(wgi.Device, clw.KernelPrivateMemSize))

		cwgs := k.getWorkGroupSize3(wgi.Device, clw.KernelCompileWorkGroupSize)
		wgi.CompileWorkGroupSize[0] = int(cwgs[0])
		wgi.CompileWorkGroupSize[1] = int(cwgs[1])
		wgi.CompileWorkGroupSize[2] = int(cwgs[2])
	}

	return
}

func (k *Kernel) getUint(paramName clw.KernelInfo) clw.Uint {
	var param clw.Uint
	err := clw.GetKernelInfo(k.id, clw.KernelNumArgs, clw.Size(unsafe.Sizeof(param)), unsafe.Pointer(&param), nil)
	if err != nil {
		panic(err)
	}
	return param
}

func (k *Kernel) getWorkGroupSize(d *Device, paramName clw.KernelWorkGroupInfo) clw.Size {
	var param clw.Size
	err := clw.GetKernelWorkGroupInfo(k.id, d.id, paramName, clw.Size(unsafe.Sizeof(param)), unsafe.Pointer(&param),
		nil)
	if err != nil {
		panic(err)
	}
	return param
}

func (k *Kernel) getWorkGroupSize3(d *Device, paramName clw.KernelWorkGroupInfo) [3]clw.Size {
	var param [3]clw.Size
	err := clw.GetKernelWorkGroupInfo(k.id, d.id, paramName, clw.Size(unsafe.Sizeof(param)), unsafe.Pointer(&param),
		nil)
	if err != nil {
		panic(err)
	}
	return param
}

func (k *Kernel) getWorkGroupUlong(d *Device, paramName clw.KernelWorkGroupInfo) clw.Ulong {
	var param clw.Ulong
	err := clw.GetKernelWorkGroupInfo(k.id, d.id, paramName, clw.Size(unsafe.Sizeof(param)), unsafe.Pointer(&param),
		nil)
	if err != nil {
		panic(err)
	}
	return param
}

func (k *Kernel) SetArgument(index int, arg interface{}) error {

	var size uintptr
	var pointer unsafe.Pointer

	switch v := arg.(type) {

	case *Buffer:
		pointer = unsafe.Pointer(&v.id)
		size = unsafe.Sizeof(v.id)

	case LocalSpaceArg:
		pointer = nil
		size = uintptr(v)

	default:
		value := reflect.ValueOf(arg)
		kind := value.Kind()
		for kind == reflect.Ptr || kind == reflect.Interface {
			value = value.Elem()
			kind = value.Kind()
		}

		pointer = unsafe.Pointer(&k.argScratch[index][0])

		switch kind {

		case reflect.Bool:

			localCopy := reflect.NewAt(int32Type, pointer)
			if value.Bool() {
				localCopy.SetInt(1)
			} else {
				localCopy.SetInt(0)
			}

			size = int32Size

		case reflect.Int:

			localCopy := reflect.NewAt(int32Type, pointer)
			localCopy.SetInt(value.Int())

			size = int32Size

		case reflect.Uint:

			localCopy := reflect.NewAt(uint32Type, pointer)
			localCopy.SetUint(value.Uint())

			size = uint32Size

		case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint8, reflect.Uint16, reflect.Uint32,
			reflect.Uint64, reflect.Float32, reflect.Float64:

			localType := value.Type()
			localCopy := reflect.NewAt(localType, pointer)
			localCopy.Set(value)

			size = localType.Size()

		default:
			return wrapError(fmt.Errorf("invaild argument kind: %s", kind.String()))
		}
	}

	return clw.SetKernelArg(k.id, clw.Uint(index), clw.Size(size), pointer)
}

func (cq *CommandQueue) EnqueueNDRangeKernel(k *Kernel, globalOffset, globalSize, localSize []int,
	waitList []*Event, e *Event) error {

	var event *clw.Event
	if e != nil {
		event = &e.id
		e.Context = cq.Context
		e.CommandType = CommandNDRangeKernel
		e.CommandQueue = cq
	}

	dims := len(globalSize)
	sizes := make([]clw.Size, dims*3)
	if globalOffset != nil {
		for i := 0; i < dims; i++ {
			sizes[i] = clw.Size(globalOffset[i])
		}
	}
	for i := 0; i < dims; i++ {
		sizes[dims+i] = clw.Size(globalSize[i])
		sizes[2*dims+i] = clw.Size(localSize[i])
	}

	return clw.EnqueueNDRangeKernel(cq.id, k.id, sizes[:dims], sizes[dims:2*dims], sizes[2*dims:],
		cq.toEvents(waitList), event)
}
<|endoftext|>


<commit_before>//An easier way to use http.Client
package com

import (
	"bytes"
	"errors"
	"fmt"
	"github.com/axgle/mahonia"
	"io/ioutil"
	"mime/multipart"
	"net/http"
	"net/http/cookiejar"
	//	"strings"
	"compress/gzip"
	"io"
	"log"
)

type HttpClient struct {
	c       *http.Client
	cookies []*http.Cookie
	jar     *cookiejar.Jar

	Header http.Header
	ua     string

	//编码转换相关处理
	conv    bool //conv between utf-8 and charset
	charset string
	enc     mahonia.Encoder
	dec     mahonia.Decoder

	//链接转向相关处理
	redirect    bool   //是否转向了。每次Get之前置为false
	redirectUrl string //转向后的链接

	Debug bool //调试开关，打开输出日志
}

func NewHttpClient() (this *HttpClient) {

	this = &HttpClient{}

	this.cookies = nil
	this.jar, _ = cookiejar.New(nil)

	this.c = &http.Client{Jar: this.jar, CheckRedirect: func(req *http.Request, via []*http.Request) error {
		this.redirect = true
		this.redirectUrl = req.URL.String()
		//return errors.New("Redirected!")
		return nil
	}}
	this.ua = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36"

	this.ResetHeader()
	this.conv = false

	return this
}

func (this *HttpClient) ResetHeader() {
	this.Header = make(http.Header)
	this.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
	this.Header.Add("Accept-Encoding", "gzip,deflate,sdch")
	this.Header.Add("Accept-Language", "zh-CN,zh;q=0.8")
	this.Header.Add("Connection", "keep-alive")
	this.Header.Add("User-Agent", this.ua)
}

//you should set it only once!
func (this *HttpClient) SetCharSet(charset string) {
	this.conv = true
	this.charset = charset
	this.enc = mahonia.NewEncoder(charset)
	this.dec = mahonia.NewDecoder(charset)
}

func (this *HttpClient) SetUa(ua string) {
	this.ua = ua
	this.Header.Set("User-Agent", ua)
}

func (this *HttpClient) Enc(in string) string {
	if this.conv {
		return this.enc.ConvertString(in)
	}
	return in
}

func (this *HttpClient) Dec(in string) string {
	if this.conv {
		return this.dec.ConvertString(in)
	}
	return in
}

func (this *HttpClient) Get(url string) (page string, err error) {
	this.clearRedirect()
	//自动转化为GB2312编码
	gbkUrl := this.Enc(url)

	req, err := http.NewRequest("GET", gbkUrl, nil)
	if err != nil {
		err = errors.New(fmt.Sprintf("HttpClient.Get(%s),NewRequest error:%s", url, err.Error()))
		return
	}

	for k, v := range this.Header {
		req.Header.Add(k, v[0])
	}

	resp, err := this.c.Do(req)

	if err != nil {
		err = errors.New(fmt.Sprintf("HttpClient.Get(%s),Response error:%s", url, err.Error()))
		return
	}

	defer resp.Body.Close()

	var body string

	if resp.StatusCode == 200 {

		switch resp.Header.Get("Content-Encoding") {
		case "gzip":
			reader, e := gzip.NewReader(resp.Body)
			if e != nil {
				err = errors.New(fmt.Sprintf("HttpClient.Get(%s),Read gzip body error:%s", url, e.Error()))
				return
			}
			for {
				buf := make([]byte, 1024)
				n, err := reader.Read(buf)

				if err != nil && err != io.EOF {
					panic(err)
				}

				if n == 0 {
					break
				}
				body += string(buf)
			}
		default:
			bodyByte, e := ioutil.ReadAll(resp.Body)
			if e != nil {
				err = errors.New(fmt.Sprintf("HttpClient.Get(%s),Read body error:%s", url, e.Error()))
				return
			}
			body = string(bodyByte)
		}
	}

	//log.Println(body)

	this.cookies = this.jar.Cookies(req.URL)

	page = this.Dec(body)
	if this.Debug {
		log.Printf("================\nStatusCode:%d\nContent:\n%s\n================\n", resp.StatusCode, page)
	}
	return
}

func (this *HttpClient) Post(url, postdata string) (page string, err error) {
	this.clearRedirect()
	//自动转化为GB2312编码
	gbkUrl := this.Enc(url)
	gbkPostdata := this.Enc(postdata)

	req, _ := http.NewRequest("POST", gbkUrl, bytes.NewReader([]byte(gbkPostdata)))
	for k, v := range this.Header {
		req.Header.Add(k, v[0])
	}
	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")

	resp, err := this.c.Do(req)

	if err != nil {
		err = errors.New(fmt.Sprintf("HttpClient.Post(%s,%s),Response error:%s", url, postdata, err.Error()))
		return
	}
	//	log4go.Debug("Response code:%d status:%s", resp.StatusCode, resp.Status)

	defer resp.Body.Close()

	var body string

	if resp.StatusCode == 200 {

		switch resp.Header.Get("Content-Encoding") {
		case "gzip":
			reader, e := gzip.NewReader(resp.Body)
			if e != nil {
				err = errors.New(fmt.Sprintf("HttpClient.Post(%s),Read gzip body error:%s", url, e.Error()))
				return
			}
			for {
				buf := make([]byte, 1024)
				n, err := reader.Read(buf)

				if err != nil && err != io.EOF {
					panic(err)
				}

				if n == 0 {
					break
				}
				body += string(buf)
			}
		default:
			bodyByte, e := ioutil.ReadAll(resp.Body)
			if e != nil {
				err = errors.New(fmt.Sprintf("HttpClient.Post(%s),Read body error:%s", url, e.Error()))
				return
			}
			body = string(bodyByte)
		}
	}

	this.cookies = this.jar.Cookies(req.URL)

	page = this.Dec(body)

	if this.Debug {
		log.Printf("================\nHttpClient.Post(%s,%s)\nStatusCode:%d\nContent:\n%s\n================\n", url, postdata, resp.StatusCode, page)
	}
	return
}

/*
///尚未测试
func (this *HttpClient) PostValues(surl string, postDict map[string]string) (page string, err error) {
	log4go.Debug("HttpClient.PostValues(%s,%T)", surl, postDict)
	postValues := url.Values{}
	for postKey, PostValue := range postDict {
		postValues.Set(this.Enc(postKey), this.Enc(PostValue))
	}
	postDataStr := postValues.Encode()

	return this.Post(surl, postDataStr)
}
*/
func (this *HttpClient) PostMultipart(u string, w *multipart.Writer, b *bytes.Buffer) (page string, err error) {
	this.clearRedirect()
	//log4go.Debug("HttpClient.PostMultipart(%s,w)", u)
	//自动转化为GB2312编码
	gbkUrl := this.Enc(u)

	req, _ := http.NewRequest("POST", gbkUrl, b)
	for k, v := range this.Header {
		req.Header.Add(k, v[0])
	}
	req.Header.Add("Content-Type", w.FormDataContentType())
	//log4go.Finest("PostMultipart Content-Type: %s", w.FormDataContentType())
	resp, err := this.c.Do(req)

	if err != nil {
		err = errors.New(fmt.Sprintf("HttpClient.PostMultipart ,Response error:%s", err.Error()))
		return
	}
	//log4go.Debug("Response code:%d status:%s", resp.StatusCode, resp.Status)

	defer resp.Body.Close()

	var body string

	if resp.StatusCode == 200 {

		switch resp.Header.Get("Content-Encoding") {
		case "gzip":
			reader, e := gzip.NewReader(resp.Body)
			if e != nil {
				err = errors.New(fmt.Sprintf("HttpClient.PostMultipart(%s),Read gzip body error:%s", u, e.Error()))
				return
			}
			for {
				buf := make([]byte, 1024)
				n, err := reader.Read(buf)

				if err != nil && err != io.EOF {
					panic(err)
				}

				if n == 0 {
					break
				}
				body += string(buf)
			}
		default:
			bodyByte, e := ioutil.ReadAll(resp.Body)
			if e != nil {
				err = errors.New(fmt.Sprintf("HttpClient.PostMultipart(%s),Read body error:%s", u, e.Error()))
				return
			}
			body = string(bodyByte)
		}
	}

	this.cookies = this.jar.Cookies(req.URL)

	page = this.Dec(body)

	//log4go.Finest("HttpClient.PostMultipart to url :%s returns :", u)
	//log4go.Finest(page)

	if this.Debug {
		log.Printf("================\nHttpClient.PostMultipart(%s)\nStatusCode:%d\nContent:\n%s\n================\n", u, resp.StatusCode, page)
	}
	return

}

func (p *HttpClient) clearRedirect() {
	p.redirect = false
	p.redirectUrl = ""
}
func (p *HttpClient) CheckRedirect() (b bool, url string) {
	return p.redirect, p.redirectUrl
}
<commit_msg>更改参数 url 名称，避免与 net/url 包命名冲突<commit_after>//An easier way to use http.Client
package com

import (
	"bytes"
	"errors"
	"fmt"
	"github.com/axgle/mahonia"
	"io/ioutil"
	"mime/multipart"
	"net/http"
	"net/http/cookiejar"
	//	"net/url"
	//	"strings"
	"compress/gzip"
	"io"
	"log"
)

type HttpClient struct {
	c       *http.Client
	cookies []*http.Cookie
	jar     *cookiejar.Jar

	Header http.Header
	ua     string

	//编码转换相关处理
	conv    bool //conv between utf-8 and charset
	charset string
	enc     mahonia.Encoder
	dec     mahonia.Decoder

	//链接转向相关处理
	redirect    bool   //是否转向了。每次Get之前置为false
	redirectUrl string //转向后的链接

	Debug bool //调试开关，打开输出日志
}

func NewHttpClient() (this *HttpClient) {

	this = &HttpClient{}

	this.cookies = nil
	this.jar, _ = cookiejar.New(nil)

	this.c = &http.Client{Jar: this.jar, CheckRedirect: func(req *http.Request, via []*http.Request) error {
		this.redirect = true
		this.redirectUrl = req.URL.String()
		//return errors.New("Redirected!")
		return nil
	}}
	this.ua = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36"

	this.ResetHeader()
	this.conv = false

	return this
}

func (this *HttpClient) ResetHeader() {
	this.Header = make(http.Header)
	this.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
	this.Header.Add("Accept-Encoding", "gzip,deflate,sdch")
	this.Header.Add("Accept-Language", "zh-CN,zh;q=0.8")
	this.Header.Add("Connection", "keep-alive")
	this.Header.Add("User-Agent", this.ua)
}

//you should set it only once!
func (this *HttpClient) SetCharSet(charset string) {
	this.conv = true
	this.charset = charset
	this.enc = mahonia.NewEncoder(charset)
	this.dec = mahonia.NewDecoder(charset)
}

func (this *HttpClient) SetUa(ua string) {
	this.ua = ua
	this.Header.Set("User-Agent", ua)
}

func (this *HttpClient) Enc(in string) string {
	if this.conv {
		return this.enc.ConvertString(in)
	}
	return in
}

func (this *HttpClient) Dec(in string) string {
	if this.conv {
		return this.dec.ConvertString(in)
	}
	return in
}

func (this *HttpClient) Get(surl string) (page string, err error) {
	this.clearRedirect()
	//自动转化为GB2312编码
	gbkUrl := this.Enc(surl)

	req, err := http.NewRequest("GET", gbkUrl, nil)
	if err != nil {
		err = errors.New(fmt.Sprintf("HttpClient.Get(%s),NewRequest error:%s", surl, err.Error()))
		return
	}

	for k, v := range this.Header {
		req.Header.Add(k, v[0])
	}

	resp, err := this.c.Do(req)

	if err != nil {
		err = errors.New(fmt.Sprintf("HttpClient.Get(%s),Response error:%s", surl, err.Error()))
		return
	}

	defer resp.Body.Close()

	var body string

	if resp.StatusCode == 200 {

		switch resp.Header.Get("Content-Encoding") {
		case "gzip":
			reader, e := gzip.NewReader(resp.Body)
			if e != nil {
				err = errors.New(fmt.Sprintf("HttpClient.Get(%s),Read gzip body error:%s", surl, e.Error()))
				return
			}
			for {
				buf := make([]byte, 1024)
				n, err := reader.Read(buf)

				if err != nil && err != io.EOF {
					panic(err)
				}

				if n == 0 {
					break
				}
				body += string(buf)
			}
		default:
			bodyByte, e := ioutil.ReadAll(resp.Body)
			if e != nil {
				err = errors.New(fmt.Sprintf("HttpClient.Get(%s),Read body error:%s", surl, e.Error()))
				return
			}
			body = string(bodyByte)
		}
	}

	//log.Println(body)

	this.cookies = this.jar.Cookies(req.URL)

	page = this.Dec(body)
	if this.Debug {
		log.Printf("================\nStatusCode:%d\nContent:\n%s\n================\n", resp.StatusCode, page)
	}
	return
}

func (this *HttpClient) Post(surl, postdata string) (page string, err error) {
	this.clearRedirect()
	//自动转化为GB2312编码
	gbkUrl := this.Enc(surl)
	gbkPostdata := this.Enc(postdata)

	req, _ := http.NewRequest("POST", gbkUrl, bytes.NewReader([]byte(gbkPostdata)))
	for k, v := range this.Header {
		req.Header.Add(k, v[0])
	}
	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")

	resp, err := this.c.Do(req)

	if err != nil {
		err = errors.New(fmt.Sprintf("HttpClient.Post(%s,%s),Response error:%s", surl, postdata, err.Error()))
		return
	}
	//	log4go.Debug("Response code:%d status:%s", resp.StatusCode, resp.Status)

	defer resp.Body.Close()

	var body string

	if resp.StatusCode == 200 {

		switch resp.Header.Get("Content-Encoding") {
		case "gzip":
			reader, e := gzip.NewReader(resp.Body)
			if e != nil {
				err = errors.New(fmt.Sprintf("HttpClient.Post(%s),Read gzip body error:%s", surl, e.Error()))
				return
			}
			for {
				buf := make([]byte, 1024)
				n, err := reader.Read(buf)

				if err != nil && err != io.EOF {
					panic(err)
				}

				if n == 0 {
					break
				}
				body += string(buf)
			}
		default:
			bodyByte, e := ioutil.ReadAll(resp.Body)
			if e != nil {
				err = errors.New(fmt.Sprintf("HttpClient.Post(%s),Read body error:%s", surl, e.Error()))
				return
			}
			body = string(bodyByte)
		}
	}

	this.cookies = this.jar.Cookies(req.URL)

	page = this.Dec(body)

	if this.Debug {
		log.Printf("================\nHttpClient.Post(%s,%s)\nStatusCode:%d\nContent:\n%s\n================\n", surl, postdata, resp.StatusCode, page)
	}
	return
}

/*
///尚未测试
func (this *HttpClient) PostValues(surl string, postDict map[string]string) (page string, err error) {
	log4go.Debug("HttpClient.PostValues(%s,%T)", surl, postDict)
	postValues := url.Values{}
	for postKey, PostValue := range postDict {
		postValues.Set(this.Enc(postKey), this.Enc(PostValue))
	}
	postDataStr := postValues.Encode()

	return this.Post(surl, postDataStr)
}
*/
func (this *HttpClient) PostMultipart(u string, w *multipart.Writer, b *bytes.Buffer) (page string, err error) {
	this.clearRedirect()
	//log4go.Debug("HttpClient.PostMultipart(%s,w)", u)
	//自动转化为GB2312编码
	gbkUrl := this.Enc(u)

	req, _ := http.NewRequest("POST", gbkUrl, b)
	for k, v := range this.Header {
		req.Header.Add(k, v[0])
	}
	req.Header.Add("Content-Type", w.FormDataContentType())
	//log4go.Finest("PostMultipart Content-Type: %s", w.FormDataContentType())
	resp, err := this.c.Do(req)

	if err != nil {
		err = errors.New(fmt.Sprintf("HttpClient.PostMultipart ,Response error:%s", err.Error()))
		return
	}
	//log4go.Debug("Response code:%d status:%s", resp.StatusCode, resp.Status)

	defer resp.Body.Close()

	var body string

	if resp.StatusCode == 200 {

		switch resp.Header.Get("Content-Encoding") {
		case "gzip":
			reader, e := gzip.NewReader(resp.Body)
			if e != nil {
				err = errors.New(fmt.Sprintf("HttpClient.PostMultipart(%s),Read gzip body error:%s", u, e.Error()))
				return
			}
			for {
				buf := make([]byte, 1024)
				n, err := reader.Read(buf)

				if err != nil && err != io.EOF {
					panic(err)
				}

				if n == 0 {
					break
				}
				body += string(buf)
			}
		default:
			bodyByte, e := ioutil.ReadAll(resp.Body)
			if e != nil {
				err = errors.New(fmt.Sprintf("HttpClient.PostMultipart(%s),Read body error:%s", u, e.Error()))
				return
			}
			body = string(bodyByte)
		}
	}

	this.cookies = this.jar.Cookies(req.URL)

	page = this.Dec(body)

	//log4go.Finest("HttpClient.PostMultipart to url :%s returns :", u)
	//log4go.Finest(page)

	if this.Debug {
		log.Printf("================\nHttpClient.PostMultipart(%s)\nStatusCode:%d\nContent:\n%s\n================\n", u, resp.StatusCode, page)
	}
	return

}

func (p *HttpClient) clearRedirect() {
	p.redirect = false
	p.redirectUrl = ""
}
func (p *HttpClient) CheckRedirect() (b bool, surl string) {
	return p.redirect, p.redirectUrl
}
<|endoftext|>


<commit_before>package httpclient

import (
	"bytes"
	"encoding/json"
	"encoding/xml"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"net/url"
)

type Encoding string

const (
	EncodingJSON = "JSON"
	EncodingXML  = "XML"
)

type RequestData struct {
	Method          string
	Path            string
	Params          url.Values
	FullURL         string // optional
	Headers         http.Header
	ReqReader       io.Reader
	ReqEncoding     Encoding
	ReqValue        interface{}
	ExpectedStatus  []int
	IgnoreRedirects bool
	RespEncoding    Encoding
	RespValue       interface{}
	RespConsume     bool
}

type InvalidStatusError struct {
	Expected []int
	Got      int
	Headers  map[string][]string
	Content  string
}

func (e InvalidStatusError) Error() string {
	return fmt.Sprintf("Invalid response status! Got %d, expected %d; headers: %s, content: %s", e.Got, e.Expected, e.Headers, e.Content)
}

type HTTPClient struct {
	BaseURL   *url.URL
	Headers   http.Header
	Client    *http.Client
	PostHooks map[int]func(*http.Request, *http.Response) error
}

func New() (httpClient *HTTPClient) {
	return &HTTPClient{
		Client:    HttpClient,
		Headers:   make(http.Header),
		PostHooks: make(map[int]func(*http.Request, *http.Response) error),
	}
}

func Insecure() (httpClient *HTTPClient) {
	return &HTTPClient{
		Client:    InsecureHttpClient,
		Headers:   make(http.Header),
		PostHooks: make(map[int]func(*http.Request, *http.Response) error),
	}
}

func (c *HTTPClient) SetPostHook(onStatus int, hook func(*http.Request, *http.Response) error) {
	c.PostHooks[onStatus] = hook
}

func (c *HTTPClient) buildURL(req *RequestData) string {
	if req.FullURL != "" {
		return req.FullURL
	}

	bu := c.BaseURL

	u := url.URL{
		Scheme: bu.Scheme,
		Host:   bu.Host,
		Path:   bu.Path + req.Path,
	}

	if req.Params != nil {
		u.RawQuery = req.Params.Encode()
	}

	return u.String()
}

func (c *HTTPClient) setHeaders(req *RequestData, httpReq *http.Request) {

	switch req.ReqEncoding {
	case EncodingJSON:
		httpReq.Header.Set("Content-Type", "application/json")
	case EncodingXML:
		httpReq.Header.Set("Content-Type", "application/xml")
	}

	switch req.RespEncoding {
	case EncodingJSON:
		httpReq.Header.Set("Accept", "application/json")
	case EncodingXML:
		httpReq.Header.Set("Accept", "application/xml")
	}

	if c.Headers != nil {
		for key, values := range c.Headers {
			for _, value := range values {
				httpReq.Header.Set(key, value)
			}
		}
	}

	if req.Headers != nil {
		for key, values := range req.Headers {
			for _, value := range values {
				httpReq.Header.Set(key, value)
			}
		}
	}
}

func (c *HTTPClient) checkStatus(req *RequestData, response *http.Response) (err error) {
	if req.ExpectedStatus != nil {
		statusOk := false

		for _, status := range req.ExpectedStatus {
			if response.StatusCode == status {
				statusOk = true
			}
		}

		if !statusOk {
			lr := io.LimitReader(response.Body, 10*1024)
			contentBytes, _ := ioutil.ReadAll(lr)
			content := string(contentBytes)

			err = InvalidStatusError{req.ExpectedStatus, response.StatusCode, response.Header, content}
			return
		}
	}

	return
}

func (c *HTTPClient) unmarshalResponse(req *RequestData, response *http.Response) (err error) {
	var buf []byte

	switch req.RespEncoding {
	case EncodingJSON:
		defer response.Body.Close()

		if buf, err = ioutil.ReadAll(response.Body); err != nil {
			return
		}

		err = json.Unmarshal(buf, req.RespValue)

		return

	case EncodingXML:
		defer response.Body.Close()

		if buf, err = ioutil.ReadAll(response.Body); err != nil {
			return
		}

		err = xml.Unmarshal(buf, req.RespValue)

		return
	}

	switch req.RespValue.(type) {
	case *[]byte:
		defer response.Body.Close()

		if buf, err = ioutil.ReadAll(response.Body); err != nil {
			return
		}

		respVal := req.RespValue.(*[]byte)
		*respVal = buf

		return
	}

	if req.RespConsume {
		defer response.Body.Close()
		ioutil.ReadAll(response.Body)
	}

	return
}

func (c *HTTPClient) marshalRequest(req *RequestData) (err error) {
	if req.ReqReader != nil || req.ReqValue == nil {
		return
	}

	var buf []byte

	switch req.ReqEncoding {
	case EncodingJSON:
		buf, err = json.Marshal(req.ReqValue)

		if err != nil {
			return
		}

		req.ReqReader = bytes.NewReader(buf)

		return

	case EncodingXML:
		buf, err = xml.Marshal(req.ReqValue)

		if err != nil {
			return
		}

		req.ReqReader = bytes.NewReader(buf)

		return
	}

	return
}

func (c *HTTPClient) runPostHook(req *http.Request, response *http.Response) (err error) {
	hook, ok := c.PostHooks[response.StatusCode]

	if ok {
		err = hook(req, response)
	}

	return
}

func (c *HTTPClient) Request(req *RequestData) (response *http.Response, err error) {
	reqURL := c.buildURL(req)

	err = c.marshalRequest(req)

	if err != nil {
		return
	}

	r, err := http.NewRequest(req.Method, reqURL, req.ReqReader)

	if err != nil {
		return
	}

	c.setHeaders(req, r)

	if req.IgnoreRedirects {
		transport := c.Client.Transport

		if transport == nil {
			transport = http.DefaultTransport
		}

		response, err = transport.RoundTrip(r)
	} else {
		response, err = c.Client.Do(r)
	}

	if err != nil {
		return
	}

	if err = c.runPostHook(r, response); err != nil {
		return
	}

	if err = c.checkStatus(req, response); err != nil {
		defer response.Body.Close()
		return
	}

	if err = c.unmarshalResponse(req, response); err != nil {
		return
	}

	return
}
<commit_msg>Proper path escaping<commit_after>package httpclient

import (
	"bytes"
	"encoding/json"
	"encoding/xml"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"net/url"
	"strings"
)

type Encoding string

const (
	EncodingJSON = "JSON"
	EncodingXML  = "XML"
)

type RequestData struct {
	Method          string
	Path            string
	Params          url.Values
	FullURL         string // optional
	Headers         http.Header
	ReqReader       io.Reader
	ReqEncoding     Encoding
	ReqValue        interface{}
	ExpectedStatus  []int
	IgnoreRedirects bool
	RespEncoding    Encoding
	RespValue       interface{}
	RespConsume     bool
}

type InvalidStatusError struct {
	Expected []int
	Got      int
	Headers  map[string][]string
	Content  string
}

func (e InvalidStatusError) Error() string {
	return fmt.Sprintf("Invalid response status! Got %d, expected %d; headers: %s, content: %s", e.Got, e.Expected, e.Headers, e.Content)
}

type HTTPClient struct {
	BaseURL   *url.URL
	Headers   http.Header
	Client    *http.Client
	PostHooks map[int]func(*http.Request, *http.Response) error
}

func New() (httpClient *HTTPClient) {
	return &HTTPClient{
		Client:    HttpClient,
		Headers:   make(http.Header),
		PostHooks: make(map[int]func(*http.Request, *http.Response) error),
	}
}

func Insecure() (httpClient *HTTPClient) {
	return &HTTPClient{
		Client:    InsecureHttpClient,
		Headers:   make(http.Header),
		PostHooks: make(map[int]func(*http.Request, *http.Response) error),
	}
}

func (c *HTTPClient) SetPostHook(onStatus int, hook func(*http.Request, *http.Response) error) {
	c.PostHooks[onStatus] = hook
}

func (c *HTTPClient) buildURL(req *RequestData) *url.URL {
	bu := c.BaseURL

	opaque := EscapePath(bu.Path + req.Path)

	u := &url.URL{
		Scheme: bu.Scheme,
		Host:   bu.Host,
		Opaque: opaque,
	}

	if req.Params != nil {
		u.RawQuery = req.Params.Encode()
	}

	return u
}

func (c *HTTPClient) setHeaders(req *RequestData, httpReq *http.Request) {
	switch req.ReqEncoding {
	case EncodingJSON:
		httpReq.Header.Set("Content-Type", "application/json")
	case EncodingXML:
		httpReq.Header.Set("Content-Type", "application/xml")
	}

	switch req.RespEncoding {
	case EncodingJSON:
		httpReq.Header.Set("Accept", "application/json")
	case EncodingXML:
		httpReq.Header.Set("Accept", "application/xml")
	}

	if c.Headers != nil {
		for key, values := range c.Headers {
			for _, value := range values {
				httpReq.Header.Set(key, value)
			}
		}
	}

	if req.Headers != nil {
		for key, values := range req.Headers {
			for _, value := range values {
				httpReq.Header.Set(key, value)
			}
		}
	}
}

func (c *HTTPClient) checkStatus(req *RequestData, response *http.Response) (err error) {
	if req.ExpectedStatus != nil {
		statusOk := false

		for _, status := range req.ExpectedStatus {
			if response.StatusCode == status {
				statusOk = true
			}
		}

		if !statusOk {
			lr := io.LimitReader(response.Body, 10*1024)
			contentBytes, _ := ioutil.ReadAll(lr)
			content := string(contentBytes)

			err = InvalidStatusError{req.ExpectedStatus, response.StatusCode, response.Header, content}
			return
		}
	}

	return
}

func (c *HTTPClient) unmarshalResponse(req *RequestData, response *http.Response) (err error) {
	var buf []byte

	switch req.RespEncoding {
	case EncodingJSON:
		defer response.Body.Close()

		if buf, err = ioutil.ReadAll(response.Body); err != nil {
			return
		}

		err = json.Unmarshal(buf, req.RespValue)

		return

	case EncodingXML:
		defer response.Body.Close()

		if buf, err = ioutil.ReadAll(response.Body); err != nil {
			return
		}

		err = xml.Unmarshal(buf, req.RespValue)

		return
	}

	switch req.RespValue.(type) {
	case *[]byte:
		defer response.Body.Close()

		if buf, err = ioutil.ReadAll(response.Body); err != nil {
			return
		}

		respVal := req.RespValue.(*[]byte)
		*respVal = buf

		return
	}

	if req.RespConsume {
		defer response.Body.Close()
		ioutil.ReadAll(response.Body)
	}

	return
}

func (c *HTTPClient) marshalRequest(req *RequestData) (err error) {
	if req.ReqReader != nil || req.ReqValue == nil {
		return
	}

	var buf []byte

	switch req.ReqEncoding {
	case EncodingJSON:
		buf, err = json.Marshal(req.ReqValue)

		if err != nil {
			return
		}

		req.ReqReader = bytes.NewReader(buf)

		return

	case EncodingXML:
		buf, err = xml.Marshal(req.ReqValue)

		if err != nil {
			return
		}

		req.ReqReader = bytes.NewReader(buf)

		return
	}

	return
}

func (c *HTTPClient) runPostHook(req *http.Request, response *http.Response) (err error) {
	hook, ok := c.PostHooks[response.StatusCode]

	if ok {
		err = hook(req, response)
	}

	return
}

func (c *HTTPClient) Request(req *RequestData) (response *http.Response, err error) {
	err = c.marshalRequest(req)

	if err != nil {
		return
	}

	r, err := http.NewRequest(req.Method, req.FullURL, req.ReqReader)

	if err != nil {
		return
	}

	if req.FullURL == "" {
		r.URL = c.buildURL(req)
		r.Host = r.URL.Host
	}

	c.setHeaders(req, r)

	if req.IgnoreRedirects {
		transport := c.Client.Transport

		if transport == nil {
			transport = http.DefaultTransport
		}

		response, err = transport.RoundTrip(r)
	} else {
		response, err = c.Client.Do(r)
	}

	if err != nil {
		return
	}

	if err = c.runPostHook(r, response); err != nil {
		return
	}

	if err = c.checkStatus(req, response); err != nil {
		defer response.Body.Close()
		return
	}

	if err = c.unmarshalResponse(req, response); err != nil {
		return
	}

	return
}

func EscapePath(path string) string {
	u := url.URL{
		Path: path,
	}

	return strings.Replace(u.String(), "+", "%2b", -1)
}
<|endoftext|>


<commit_before>package isolated

import (
	"code.cloudfoundry.org/cli/integration/helpers"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	. "github.com/onsi/gomega/gbytes"
	. "github.com/onsi/gomega/gexec"
)

var _ = Describe("routes command", func() {
	Context("Help", func() {
		It("displays the help information", func() {
			session := helpers.CF("routes", "--help")
			Eventually(session).Should(Say(`NAME:`))
			Eventually(session).Should(Say(`routes - List all routes in the current space or the current organization\n`))
			Eventually(session).Should(Say(`\n`))

			Eventually(session).Should(Say(`USAGE:`))
			Eventually(session).Should(Say(`cf routes \[--orglevel\]\n`))
			Eventually(session).Should(Say(`\n`))

			Eventually(session).Should(Say(`OPTIONS:`))
			Eventually(session).Should(Say(`--orglevel\s+List all the routes for all spaces of current organization`))
			Eventually(session).Should(Say(`\n`))

			Eventually(session).Should(Say(`SEE ALSO:`))
			Eventually(session).Should(Say(`check-route, domains, map-route, unmap-route`))

			Eventually(session).Should(Exit(0))
		})
	})

	When("the environment is not setup correctly", func() {
		It("fails with the appropriate errors", func() {
			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "routes")
		})
	})

	When("the environment is set up correctly", func() {
		var (
			orgName   string
			spaceName string
		)

		BeforeEach(func() {
			orgName = helpers.NewOrgName()
			spaceName = helpers.NewSpaceName()

			helpers.SetupCF(orgName, spaceName)
		})

		AfterEach(func() {
			helpers.QuickDeleteOrg(orgName)
		})

		var (
			userName string
		)

		BeforeEach(func() {
			userName, _ = helpers.GetCredentials()
		})

		When("routes exist", func() {
			var (
				userName   string
				domainName string
				domain     helpers.Domain

				otherSpaceName string
			)

			BeforeEach(func() {
				otherSpaceName = helpers.NewSpaceName()

				domainName = helpers.NewDomainName()
				userName, _ = helpers.GetCredentials()

				domain = helpers.NewDomain(orgName, domainName)
				domain.CreatePrivate()
				Eventually(helpers.CF("create-route", domainName, "--hostname", "route1")).Should(Exit(0))
				helpers.SetupCF(orgName, otherSpaceName)
				Eventually(helpers.CF("create-route", domainName, "--hostname", "route2")).Should(Exit(0))
				helpers.SetupCF(orgName, spaceName)
			})

			AfterEach(func() {
				domain.Delete()
				helpers.QuickDeleteSpace(otherSpaceName)
			})

			It("lists all the routes", func() {
				session := helpers.CF("routes")
				Eventually(session).Should(Say(`Getting routes for org %s / space %s as %s\.\.\.`, orgName, spaceName, userName))
				Eventually(session).Should(Say(`%s\s+route1\s+%s`, spaceName, domainName))
				Eventually(session).Should(Exit(0))
			})

			When("fetching routes by org", func() {
				It("lists all the routes in the org", func() {
					session := helpers.CF("routes", "--orglevel")
					Eventually(session).Should(Say(`Getting routes for org %s as %s\.\.\.`, orgName, userName))
					Eventually(session).Should(Say(`%s\s+route1\s+%s`, spaceName, domainName))
					Eventually(session).Should(Say(`%s\s+route2\s+%s`, otherSpaceName, domainName))
					Eventually(session).Should(Exit(0))
				})
			})
		})

		When("no routes exist", func() {
			It("outputs a message about no routes existing", func() {
				session := helpers.CF("routes")
				Eventually(session).Should(Say(`Getting routes for org %s / space %s as %s\.\.\.`, orgName, spaceName, userName))
				Eventually(session).Should(Say("No routes found"))
				Eventually(session).Should(Exit(0))
			})
		})
	})
})
<commit_msg>Fix list routes integration test<commit_after>package isolated

import (
	"code.cloudfoundry.org/cli/integration/helpers"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	. "github.com/onsi/gomega/gbytes"
	. "github.com/onsi/gomega/gexec"
)

var _ = Describe("routes command", func() {
	Context("Help", func() {
		It("displays the help information", func() {
			session := helpers.CF("routes", "--help")
			Eventually(session).Should(Say(`NAME:`))
			Eventually(session).Should(Say(`routes - List all routes in the current space or the current organization\n`))
			Eventually(session).Should(Say(`\n`))

			Eventually(session).Should(Say(`USAGE:`))
			Eventually(session).Should(Say(`cf routes \[--orglevel\]\n`))
			Eventually(session).Should(Say(`\n`))

			Eventually(session).Should(Say(`OPTIONS:`))
			Eventually(session).Should(Say(`--orglevel\s+List all the routes for all spaces of current organization`))
			Eventually(session).Should(Say(`\n`))

			Eventually(session).Should(Say(`SEE ALSO:`))
			Eventually(session).Should(Say(`check-route, domains, map-route, unmap-route`))

			Eventually(session).Should(Exit(0))
		})
	})

	When("the environment is not setup correctly", func() {
		It("fails with the appropriate errors", func() {
			helpers.CheckEnvironmentTargetedCorrectly(true, true, ReadOnlyOrg, "routes")
		})
	})

	When("the environment is set up correctly", func() {
		var (
			orgName   string
			spaceName string
			userName  string
		)

		BeforeEach(func() {
			orgName = helpers.NewOrgName()
			spaceName = helpers.NewSpaceName()

			helpers.SetupCF(orgName, spaceName)
			userName, _ = helpers.GetCredentials()
		})

		AfterEach(func() {
			helpers.QuickDeleteOrg(orgName)
		})

		When("routes exist", func() {
			var (
				domainName string
				domain     helpers.Domain

				otherSpaceName string
			)

			BeforeEach(func() {
				otherSpaceName = helpers.GenerateHigherName(helpers.NewSpaceName, spaceName)

				domainName = helpers.NewDomainName()

				domain = helpers.NewDomain(orgName, domainName)
				domain.CreatePrivate()
				Eventually(helpers.CF("create-route", domainName, "--hostname", "route1")).Should(Exit(0))
				helpers.SetupCF(orgName, otherSpaceName)
				Eventually(helpers.CF("create-route", domainName, "--hostname", "route2")).Should(Exit(0))
				helpers.SetupCF(orgName, spaceName)
			})

			AfterEach(func() {
				domain.Delete()
				helpers.QuickDeleteSpace(otherSpaceName)
			})

			It("lists all the routes", func() {
				session := helpers.CF("routes")
				Eventually(session).Should(Say(`Getting routes for org %s / space %s as %s\.\.\.`, orgName, spaceName, userName))
				Eventually(session).Should(Say(`%s\s+route1\s+%s`, spaceName, domainName))
				Eventually(session).Should(Exit(0))
			})

			When("fetching routes by org", func() {
				It("lists all the routes in the org", func() {
					session := helpers.CF("routes", "--orglevel")
					Eventually(session).Should(Say(`Getting routes for org %s as %s\.\.\.`, orgName, userName))
					Eventually(session).Should(Say(`%s\s+route1\s+%s`, spaceName, domainName))
					Eventually(session).Should(Say(`%s\s+route2\s+%s`, otherSpaceName, domainName))
					Eventually(session).Should(Exit(0))
				})
			})
		})

		When("no routes exist", func() {
			It("outputs a message about no routes existing", func() {
				session := helpers.CF("routes")
				Eventually(session).Should(Say(`Getting routes for org %s / space %s as %s\.\.\.`, orgName, spaceName, userName))
				Eventually(session).Should(Say("No routes found"))
				Eventually(session).Should(Exit(0))
			})
		})
	})
})
<|endoftext|>


<commit_before>package gorocksdb

// #cgo LDFLAGS: -lrocksdb -lstdc++ -lm -lz -lbz2 -lsnappy
import "C"
<commit_msg>Add -ldl needed apparently<commit_after>package gorocksdb

// #cgo LDFLAGS: -lrocksdb -lstdc++ -lm -lz -lbz2 -lsnappy -ldl
import "C"
<|endoftext|>