<commit_before>// Copyright (c) 2011, SoundCloud Ltd., Daniel Bornkessel
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Source code and contact info at http://github.com/kesselborn/go-getopt

package getopt

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

type Definitions []Option
type Description string

type Options struct {
	description Description
	definitions Definitions
}

func (optionsDefinition Options) setEnvAndConfigValues(options map[string]OptionValue, environment map[string]string) (err *GetOptError) {
	significantEnvVars := make(map[string]Option)

	for _, opt := range optionsDefinition.definitions {
		if value := opt.EnvVar(); value != "" {
			significantEnvVars[value] = opt
		}
	}

	for key, significantEnvVar := range significantEnvVars {
		if value := environment[key]; value != "" {
			options[significantEnvVar.Key()], err = assignValue(significantEnvVar.DefaultValue, value)
			if err != nil {
				break
			}
		}
	}

	return
}

func checkOptionsDefinitionConsistency(optionsDefinition Options) (err *GetOptError) {
	consistencyErrorPrefix := "wrong getopt usage: "

	foundOptionalArg := false
	for _, option := range optionsDefinition.definitions {
		switch {
		case option.Flags&IsArg > 0 && option.Flags&Required == 0 && option.Flags&Optional == 0:
			err = &GetOptError{ConsistencyError, consistencyErrorPrefix + "an argument must be explicitly set to be Optional or Required"}
		case option.Flags&IsArg > 0 && option.Flags&Optional > 0:
			foundOptionalArg = true
		case option.Flags&IsArg > 0 && option.Flags&Required > 0 && foundOptionalArg:
			err = &GetOptError{ConsistencyError, consistencyErrorPrefix + "a required argument can't come after an optional argument"}
		case option.Flags&Optional > 0 && option.Flags&Required > 0:
			err = &GetOptError{ConsistencyError, consistencyErrorPrefix + "an option can not be Required and Optional"}
		case option.Flags&Flag > 0 && option.Flags&ExampleIsDefault > 0:
			err = &GetOptError{ConsistencyError, consistencyErrorPrefix + "an option can not be a Flag and have ExampleIsDefault"}
		case option.Flags&Required > 0 && option.Flags&ExampleIsDefault > 0:
			err = &GetOptError{ConsistencyError, consistencyErrorPrefix + "an option can not be Required and have ExampleIsDefault"}
		case option.Flags&NoLongOpt > 0 && !option.HasShortOpt() && option.Flags&IsArg == 0:
			err = &GetOptError{ConsistencyError, consistencyErrorPrefix + "an option must have either NoLongOpt or a ShortOption"}
		case option.Flags&Flag > 0 && option.Flags&IsArg > 0:
			err = &GetOptError{ConsistencyError, consistencyErrorPrefix + "an option can not be a Flag and be an argument (IsArg)"}
		}
	}

	return
}

func (options Options) FindOption(optionString string) (option Option, found bool) {
	for _, cur := range options.definitions {
		if cur.ShortOpt() == optionString || cur.LongOpt() == optionString {
			option = cur
			found = true
			break
		}
	}

	return option, found
}

func (options Options) IsOptional(optionName string) (isRequired bool) {
	if option, found := options.FindOption(optionName); found && option.Flags&Optional != 0 {
		isRequired = true
	}

	return isRequired
}

func (options Options) IsRequired(optionName string) (isRequired bool) {
	if option, found := options.FindOption(optionName); found && option.Flags&Required != 0 {
		isRequired = true
	}

	return isRequired
}

func (options Options) IsFlag(optionName string) (isFlag bool) {
	if option, found := options.FindOption(optionName); found && option.Flags&Flag != 0 {
		isFlag = true
	}

	return isFlag
}

func (options Options) ConfigOptionKey() (key string) {
	for _, option := range options.definitions {
		if option.Flags&IsConfigFile > 0 {
			key = option.Key()
			break
		}
	}

	return
}

func (options Options) RequiredArguments() (requiredOptions Options) {
	for _, cur := range options.definitions {
		if cur.Flags&Required != 0 && cur.Flags&IsArg != 0 {
			requiredOptions.definitions = append(requiredOptions.definitions, cur)
		}
	}

	return
}

func (options Options) RequiredOptions() (requiredOptions []string) {
	for _, cur := range options.definitions {
		if cur.Flags&Required != 0 && cur.Flags&IsArg == 0 && cur.Flags&IsPassThrough == 0 {
			requiredOptions = append(requiredOptions, cur.LongOpt())
		}
	}

	return
}

func (options Options) UsageCustomArg0(arg0 string) (output string) {
	output = "Usage: " + arg0

	passThroughSeparatorPrinted := false
	for _, option := range options.definitions {
		if option.Flags&IsPassThrough > 0 && !passThroughSeparatorPrinted {
			output = output + " --"
			passThroughSeparatorPrinted = true
		}

		output = output + " " + option.Usage()
	}

	output = output + "\n\n"

	return
}

func (options Options) Usage() (output string) {
	return options.UsageCustomArg0(filepath.Base(os.Args[0]))
}

func (options Options) Help(description string) (output string) {
	return options.HelpCustomArg0(description, filepath.Base(os.Args[0]))
}

func (options Options) HelpCustomArg0(description string, arg0 string) (output string) {
	output = options.UsageCustomArg0(arg0)
	if options.description != "" {
		output = output + string(options.description) + "\n\n"
	}

	longOptTextLength := 0

	for _, option := range options.definitions {
		if length := len(option.LongOptString()); length > longOptTextLength {
			longOptTextLength = length
		}
	}

	longOptTextLength = longOptTextLength + 2

	var argumentsString string
	var optionsString string
	var passThroughString string

	usageOpt, helpOpt := options.usageHelpOptionNames()

	for _, option := range options.definitions {
		switch {
		case option.Flags&IsPassThrough > 0:
			passThroughString = passThroughString + option.HelpText(longOptTextLength) + "\n"
		case option.Flags&IsArg > 0:
			argumentsString = argumentsString + option.HelpText(longOptTextLength) + "\n"
		case option.LongOpt() != helpOpt:
			optionsString = optionsString + option.HelpText(longOptTextLength) + "\n"
		}
	}

	if optionsString != "" {
		helpHelp := fmt.Sprintf("usage (-%s) / detailed help text (--%s)", usageOpt, helpOpt)

		if option, found := options.FindOption(helpOpt); found {
			helpHelp = option.Description
		}

		usageHelpOption := Option{fmt.Sprintf("%s|%s", helpOpt, usageOpt),
			helpHelp,
			Usage | Help | Flag, ""}
		optionsString = optionsString + usageHelpOption.HelpText(longOptTextLength) + "\n"
		output = output + "Options:\n" + optionsString + "\n"
	}

	if argumentsString != "" {
		output = output + "Arguments:\n" + argumentsString + "\n"
	}

	if passThroughString != "" {
		output = output + "Pass through arguments:\n" + passThroughString + "\n"
	}

	return
}
<commit_msg>split Usage function for later use in sub command overview<commit_after>// Copyright (c) 2011, SoundCloud Ltd., Daniel Bornkessel
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Source code and contact info at http://github.com/kesselborn/go-getopt

package getopt

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

type Definitions []Option
type Description string

type Options struct {
	description Description
	definitions Definitions
}

func (optionsDefinition Options) setEnvAndConfigValues(options map[string]OptionValue, environment map[string]string) (err *GetOptError) {
	significantEnvVars := make(map[string]Option)

	for _, opt := range optionsDefinition.definitions {
		if value := opt.EnvVar(); value != "" {
			significantEnvVars[value] = opt
		}
	}

	for key, significantEnvVar := range significantEnvVars {
		if value := environment[key]; value != "" {
			options[significantEnvVar.Key()], err = assignValue(significantEnvVar.DefaultValue, value)
			if err != nil {
				break
			}
		}
	}

	return
}

func checkOptionsDefinitionConsistency(optionsDefinition Options) (err *GetOptError) {
	consistencyErrorPrefix := "wrong getopt usage: "

	foundOptionalArg := false
	for _, option := range optionsDefinition.definitions {
		switch {
		case option.Flags&IsArg > 0 && option.Flags&Required == 0 && option.Flags&Optional == 0:
			err = &GetOptError{ConsistencyError, consistencyErrorPrefix + "an argument must be explicitly set to be Optional or Required"}
		case option.Flags&IsArg > 0 && option.Flags&Optional > 0:
			foundOptionalArg = true
		case option.Flags&IsArg > 0 && option.Flags&Required > 0 && foundOptionalArg:
			err = &GetOptError{ConsistencyError, consistencyErrorPrefix + "a required argument can't come after an optional argument"}
		case option.Flags&Optional > 0 && option.Flags&Required > 0:
			err = &GetOptError{ConsistencyError, consistencyErrorPrefix + "an option can not be Required and Optional"}
		case option.Flags&Flag > 0 && option.Flags&ExampleIsDefault > 0:
			err = &GetOptError{ConsistencyError, consistencyErrorPrefix + "an option can not be a Flag and have ExampleIsDefault"}
		case option.Flags&Required > 0 && option.Flags&ExampleIsDefault > 0:
			err = &GetOptError{ConsistencyError, consistencyErrorPrefix + "an option can not be Required and have ExampleIsDefault"}
		case option.Flags&NoLongOpt > 0 && !option.HasShortOpt() && option.Flags&IsArg == 0:
			err = &GetOptError{ConsistencyError, consistencyErrorPrefix + "an option must have either NoLongOpt or a ShortOption"}
		case option.Flags&Flag > 0 && option.Flags&IsArg > 0:
			err = &GetOptError{ConsistencyError, consistencyErrorPrefix + "an option can not be a Flag and be an argument (IsArg)"}
		}
	}

	return
}

func (options Options) FindOption(optionString string) (option Option, found bool) {
	for _, cur := range options.definitions {
		if cur.ShortOpt() == optionString || cur.LongOpt() == optionString {
			option = cur
			found = true
			break
		}
	}

	return option, found
}

func (options Options) IsOptional(optionName string) (isRequired bool) {
	if option, found := options.FindOption(optionName); found && option.Flags&Optional != 0 {
		isRequired = true
	}

	return isRequired
}

func (options Options) IsRequired(optionName string) (isRequired bool) {
	if option, found := options.FindOption(optionName); found && option.Flags&Required != 0 {
		isRequired = true
	}

	return isRequired
}

func (options Options) IsFlag(optionName string) (isFlag bool) {
	if option, found := options.FindOption(optionName); found && option.Flags&Flag != 0 {
		isFlag = true
	}

	return isFlag
}

func (options Options) ConfigOptionKey() (key string) {
	for _, option := range options.definitions {
		if option.Flags&IsConfigFile > 0 {
			key = option.Key()
			break
		}
	}

	return
}

func (options Options) RequiredArguments() (requiredOptions Options) {
	for _, cur := range options.definitions {
		if cur.Flags&Required != 0 && cur.Flags&IsArg != 0 {
			requiredOptions.definitions = append(requiredOptions.definitions, cur)
		}
	}

	return
}

func (options Options) RequiredOptions() (requiredOptions []string) {
	for _, cur := range options.definitions {
		if cur.Flags&Required != 0 && cur.Flags&IsArg == 0 && cur.Flags&IsPassThrough == 0 {
			requiredOptions = append(requiredOptions, cur.LongOpt())
		}
	}

	return
}

func (options Options) commandDefinition(arg0 string) (output string) {
  output = arg0

	passThroughSeparatorPrinted := false
	for _, option := range options.definitions {
		if option.Flags&IsPassThrough > 0 && !passThroughSeparatorPrinted {
			output = output + " --"
			passThroughSeparatorPrinted = true
		}

		output = output + " " + option.Usage()
	}

  return
}

func (options Options) UsageCustomArg0(arg0 string) (output string) {
  return "Usage: " + options.commandDefinition(arg0)  + "\n\n"
}

func (options Options) Usage() (output string) {
	return options.UsageCustomArg0(filepath.Base(os.Args[0]))
}

func (options Options) Help(description string) (output string) {
	return options.HelpCustomArg0(description, filepath.Base(os.Args[0]))
}

func (options Options) HelpCustomArg0(description string, arg0 string) (output string) {
	output = options.UsageCustomArg0(arg0)
	if options.description != "" {
		output = output + string(options.description) + "\n\n"
	}

	longOptTextLength := 0

	for _, option := range options.definitions {
		if length := len(option.LongOptString()); length > longOptTextLength {
			longOptTextLength = length
		}
	}

	longOptTextLength = longOptTextLength + 2

	var argumentsString string
	var optionsString string
	var passThroughString string

	usageOpt, helpOpt := options.usageHelpOptionNames()

	for _, option := range options.definitions {
		switch {
		case option.Flags&IsPassThrough > 0:
			passThroughString = passThroughString + option.HelpText(longOptTextLength) + "\n"
		case option.Flags&IsArg > 0:
			argumentsString = argumentsString + option.HelpText(longOptTextLength) + "\n"
		case option.LongOpt() != helpOpt:
			optionsString = optionsString + option.HelpText(longOptTextLength) + "\n"
		}
	}

	if optionsString != "" {
		helpHelp := fmt.Sprintf("usage (-%s) / detailed help text (--%s)", usageOpt, helpOpt)

		if option, found := options.FindOption(helpOpt); found {
			helpHelp = option.Description
		}

		usageHelpOption := Option{fmt.Sprintf("%s|%s", helpOpt, usageOpt),
			helpHelp,
			Usage | Help | Flag, ""}
		optionsString = optionsString + usageHelpOption.HelpText(longOptTextLength) + "\n"
		output = output + "Options:\n" + optionsString + "\n"
	}

	if argumentsString != "" {
		output = output + "Arguments:\n" + argumentsString + "\n"
	}

	if passThroughString != "" {
		output = output + "Pass through arguments:\n" + passThroughString + "\n"
	}

	return
}
<|endoftext|>


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

package main

import (
	"github.com/codegangsta/cli"
	//	"github.com/minio-io/mc/pkg/s3"
)

var Options = []cli.Command{
	Get,
	Put,
	List,
}

var Get = cli.Command{
	Name:        "get",
	Usage:       "",
	Description: "",
	Action:      doGet,
}

var Put = cli.Command{
	Name:        "put",
	Usage:       "",
	Description: "",
	Action:      doPut,
}

var List = cli.Command{
	Name:        "list",
	Usage:       "",
	Description: "",
	Action:      doList,
}

func doGet(c *cli.Context) {
}

func doPut(c *cli.Context) {
}

func doList(c *cli.Context) {
}
<commit_msg>Implement new options<commit_after>/*
 * Mini Object Storage, (C) 2014 Minio, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package main

import (
	"log"
	"os"
	"strings"

	"github.com/codegangsta/cli"
	"github.com/minio-io/mc/pkg/s3"
)

type MinioClient struct {
	bucketName string
	keyName    string
	body       string
	bucketAcls string
	policy     string
	region     string
	query      string // TODO
}

var Options = []cli.Command{
	GetObject,
	PutObject,
	ListObjects,
	ListBuckets,
	Configure,
}

var GetObject = cli.Command{
	Name:        "get-object",
	Usage:       "",
	Description: "",
	Action:      doGetObject,
}

var PutObject = cli.Command{
	Name:        "put-object",
	Usage:       "",
	Description: "",
	Action:      doPutObject,
}

var ListObjects = cli.Command{
	Name:        "list-objects",
	Usage:       "",
	Description: "",
	Action:      doListObjects,
}

var ListBuckets = cli.Command{
	Name:        "list-buckets",
	Usage:       "",
	Description: "",
	Action:      doListBuckets,
}

var Configure = cli.Command{
	Name:        "configure",
	Usage:       "",
	Description: "",
	Action:      doConfigure,
}

func parseInput(c *cli.Context) string {
	var commandName string
	switch len(c.Args()) {
	case 1:
		commandName = c.Args()[0]
	default:
		log.Fatal("command name must not be blank\n")
	}

	var inputOptions []string
	if c.String("bucket") != "" {
		inputOptions = strings.Split(c.String("options"), ",")
	}

	if inputOptions[0] == "" {
		log.Fatal("options cannot be empty with a command name")
	}
	return commandName
}

func doGetObject(c *cli.Context) {
	var bucket, key string
	accessKey := os.Getenv("AWS_ACCESS_KEY_ID")
	secretKey := os.Getenv("AWS_ACCESS_SECRET_KEY")
	if accessKey == "" || secretKey == "" {
		log.Fatal("no AWS_ACCESS_KEY_ID or AWS_ACCESS_KEY_SECRET set in environment")
	}
	s3c := s3.NewS3Client(accessKey, secretKey)
	_, _, err := s3c.Get(bucket, key)
	if err != nil {
		log.Fatal(err)
	}
}

func doPutObject(c *cli.Context) {
}

func doListObject(c *cli.Context) {
}

func doListObjects(c *cli.Context) {
}

func doListBuckets(c *cli.Context) {
}

func doConfigure(c *cli.Context) {
}
<|endoftext|>


<commit_before>package es

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

const (
	OK  = '2'
	AND = "AND"
	OR  = "OR"
)

type Term map[string]interface{}

type Terms map[string][]interface{}

type Filter struct {
	And   []*Filter         `json:"and,omitempty`
	Term  *Term             `json:"term,omitempty"`
	Terms *Terms            `json:"term,omitempty"`
	Range map[string]*Range `json:"range,omitempty"`
}

type Range struct {
	From interface{}
	To   interface{}
}

type Filtered struct {
	Filter *Filter
}

type QueryString struct {
	Query           string `json:"query,omitempty"`
	DefaultOperator string `json:"default_operator,omitempty"`
}

type Query struct {
	Filtered    *Filtered    `json:"filtered,omitempty"`
	QueryString *QueryString `json:"query_string,omitempty"`
}

var query = Query{
	Filtered: &Filtered{
		Filter: &Filter{
			And: []*Filter{
				{
					Term: &Term{
						"Device": "Anrdoi",
					},
				},
				{
					Terms: &Terms{
						"Action": []interface{}{
							"api/v1/my/photos#create",
							"api/v1/photos#create",
						},
					},
					Range: map[string]*Range{
						"Time": {
							From: "",
						},
					},
				},
			},
		},
	},
}

type BulkIndexJob struct {
	Key    string
	Record interface{}
}

type Index struct {
	Host          string
	Port          int
	Index         string
	Type          string
	bulkIndexJobs []*BulkIndexJob
	BatchSize     int
	Debug         bool
}

func (index *Index) EnqueueBulkIndex(key string, record interface{}) (bool, error) {
	if index.BatchSize == 0 {
		index.BatchSize = 100
	}
	if cap(index.bulkIndexJobs) == 0 {
		index.ResetQueue()
	}
	index.bulkIndexJobs = append(index.bulkIndexJobs, &BulkIndexJob{
		Key: key, Record: record,
	})
	if len(index.bulkIndexJobs) >= index.BatchSize {
		return true, index.RunBatchIndex()
	}
	return false, nil
}

func (index *Index) RunBatchIndex() error {
	started := time.Now()
	buf := &bytes.Buffer{}
	enc := json.NewEncoder(buf)
	for _, r := range index.bulkIndexJobs {
		enc.Encode(map[string]map[string]string{
			"index": map[string]string{
				"_index": index.Index,
				"_type":  index.Type,
				"_id":    r.Key,
			},
		})
		enc.Encode(r.Record)
	}
	rsp, e := http.Post(index.BaseUrl()+"/_bulk", "application/json", buf)
	if e != nil {
		return e
	}
	defer rsp.Body.Close()
	b, _ := ioutil.ReadAll(rsp.Body)
	if rsp.Status[0] != OK {
		return fmt.Errorf("Error sending bulk request: %s %s", rsp.Status, string(b))
	}
	perSecond := float64(len(index.bulkIndexJobs)) / time.Now().Sub(started).Seconds()
	if index.Debug {
		fmt.Printf("indexed %d, %.1f/second\n", len(index.bulkIndexJobs), perSecond)
	}
	index.ResetQueue()
	return nil
}

func (index *Index) ResetQueue() {
	index.bulkIndexJobs = make([]*BulkIndexJob, 0, index.BatchSize)
}

type IndexStatus struct {
	Index  string `json:"_index"`
	Type   string `json:"_type"`
	Id     string `json:"_id"`
	Exists bool   `json:"exists"`
}

func (index *Index) Status() (status *IndexStatus, e error) {
	rsp, e := http.Get(index.BaseUrl() + "/_status")
	if e != nil {
		return nil, e
	}
	defer rsp.Body.Close()
	b, e := ioutil.ReadAll(rsp.Body)
	if e != nil {
		return nil, e
	}
	status = &IndexStatus{}
	e = json.Unmarshal(b, status)
	return status, e
}

func (index *Index) Mapping() (i interface{}, e error) {
	rsp, e := index.request("GET", index.TypeUrl()+"/_mapping", i)
	if e != nil {
		if rsp.StatusCode == 404 {
			return nil, nil
		}
		return nil, e
	}
	e = json.Unmarshal(rsp.Body, &i)
	if e != nil {
		return nil, e
	}
	return i, nil
}

func (index *Index) PutMapping(mapping interface{}) (rsp *HttpResponse, e error) {
	return index.request("PUT", index.IndexUrl()+"/", mapping)
}

func (index *Index) BaseUrl() string {
	if index.Port == 0 {
		index.Port = 9200
	}
	return fmt.Sprintf("http://%s:%d", index.Host, index.Port)
}

func (index *Index) IndexUrl() string {
	if index.Index != "" {
		return index.BaseUrl() + "/" + index.Index
	}
	return ""
}

func (index *Index) TypeUrl() string {
	if base := index.IndexUrl(); base != "" && index.Type != "" {
		return base + "/" + index.Type
	}
	return ""
	return ""
}

func (index *Index) Search(req *Request) (rsp *Response, e error) {
	writer := &bytes.Buffer{}
	js := json.NewEncoder(writer)
	e = js.Encode(req)
	if e != nil {
		return nil, e
	}
	u := index.TypeUrl()
	if !strings.HasSuffix(u, "/") {
		u += "/"
	}
	u += "/_search"
	httpRequest, e := http.NewRequest("POST", u, writer)
	if e != nil {
		return nil, e
	}
	httpResponse, e := http.DefaultClient.Do(httpRequest)
	if e != nil {
		return nil, e
	}
	defer httpResponse.Body.Close()
	dec := json.NewDecoder(httpResponse.Body)
	rsp = &Response{}
	e = dec.Decode(rsp)
	if e != nil {
		return nil, e
	}
	return rsp, nil
}

func (index *Index) Post(u string, i interface{}) (*HttpResponse, error) {
	return index.request("POST", u, i)
}

func (index *Index) PutObject(id string, i interface{}) (*HttpResponse, error) {
	return index.request("PUT", index.TypeUrl()+"/"+id, i)
}

func (index *Index) Put(u string, i interface{}) (*HttpResponse, error) {
	return index.request("PUT", u, i)
}

type HttpResponse struct {
	*http.Response
	Body []byte
}

func (index *Index) request(method string, u string, i interface{}) (httpResponse *HttpResponse, e error) {
	var req *http.Request
	if i != nil {
		buf := &bytes.Buffer{}
		encoder := json.NewEncoder(buf)
		if e := encoder.Encode(i); e != nil {
			return nil, e
		}
		req, e = http.NewRequest(method, u, buf)
	} else {
		req, e = http.NewRequest(method, u, nil)
	}
	if e != nil {
		return nil, e
	}
	rsp, e := http.DefaultClient.Do(req)
	if e != nil {
		return nil, e
	}
	defer rsp.Body.Close()
	b, e := ioutil.ReadAll(rsp.Body)
	if e != nil {
		return nil, e
	}
	httpResponse = &HttpResponse{
		Response: rsp,
		Body:     b,
	}
	if e != nil {
		return httpResponse, e
	}
	if rsp.Status[0] != OK {
		return httpResponse, fmt.Errorf("error indexing: %s %s", rsp.Status, string(b))
	}
	return httpResponse, nil
}
<commit_msg>use index_url instead of type_url to check for mapping<commit_after>package es

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

const (
	OK  = '2'
	AND = "AND"
	OR  = "OR"
)

type Term map[string]interface{}

type Terms map[string][]interface{}

type Filter struct {
	And   []*Filter         `json:"and,omitempty`
	Term  *Term             `json:"term,omitempty"`
	Terms *Terms            `json:"term,omitempty"`
	Range map[string]*Range `json:"range,omitempty"`
}

type Range struct {
	From interface{}
	To   interface{}
}

type Filtered struct {
	Filter *Filter
}

type QueryString struct {
	Query           string `json:"query,omitempty"`
	DefaultOperator string `json:"default_operator,omitempty"`
}

type Query struct {
	Filtered    *Filtered    `json:"filtered,omitempty"`
	QueryString *QueryString `json:"query_string,omitempty"`
}

var query = Query{
	Filtered: &Filtered{
		Filter: &Filter{
			And: []*Filter{
				{
					Term: &Term{
						"Device": "Anrdoi",
					},
				},
				{
					Terms: &Terms{
						"Action": []interface{}{
							"api/v1/my/photos#create",
							"api/v1/photos#create",
						},
					},
					Range: map[string]*Range{
						"Time": {
							From: "",
						},
					},
				},
			},
		},
	},
}

type BulkIndexJob struct {
	Key    string
	Record interface{}
}

type Index struct {
	Host          string
	Port          int
	Index         string
	Type          string
	bulkIndexJobs []*BulkIndexJob
	BatchSize     int
	Debug         bool
}

func (index *Index) EnqueueBulkIndex(key string, record interface{}) (bool, error) {
	if index.BatchSize == 0 {
		index.BatchSize = 100
	}
	if cap(index.bulkIndexJobs) == 0 {
		index.ResetQueue()
	}
	index.bulkIndexJobs = append(index.bulkIndexJobs, &BulkIndexJob{
		Key: key, Record: record,
	})
	if len(index.bulkIndexJobs) >= index.BatchSize {
		return true, index.RunBatchIndex()
	}
	return false, nil
}

func (index *Index) RunBatchIndex() error {
	started := time.Now()
	buf := &bytes.Buffer{}
	enc := json.NewEncoder(buf)
	for _, r := range index.bulkIndexJobs {
		enc.Encode(map[string]map[string]string{
			"index": map[string]string{
				"_index": index.Index,
				"_type":  index.Type,
				"_id":    r.Key,
			},
		})
		enc.Encode(r.Record)
	}
	rsp, e := http.Post(index.BaseUrl()+"/_bulk", "application/json", buf)
	if e != nil {
		return e
	}
	defer rsp.Body.Close()
	b, _ := ioutil.ReadAll(rsp.Body)
	if rsp.Status[0] != OK {
		return fmt.Errorf("Error sending bulk request: %s %s", rsp.Status, string(b))
	}
	perSecond := float64(len(index.bulkIndexJobs)) / time.Now().Sub(started).Seconds()
	if index.Debug {
		fmt.Printf("indexed %d, %.1f/second\n", len(index.bulkIndexJobs), perSecond)
	}
	index.ResetQueue()
	return nil
}

func (index *Index) ResetQueue() {
	index.bulkIndexJobs = make([]*BulkIndexJob, 0, index.BatchSize)
}

type IndexStatus struct {
	Index  string `json:"_index"`
	Type   string `json:"_type"`
	Id     string `json:"_id"`
	Exists bool   `json:"exists"`
}

func (index *Index) Status() (status *IndexStatus, e error) {
	rsp, e := http.Get(index.BaseUrl() + "/_status")
	if e != nil {
		return nil, e
	}
	defer rsp.Body.Close()
	b, e := ioutil.ReadAll(rsp.Body)
	if e != nil {
		return nil, e
	}
	status = &IndexStatus{}
	e = json.Unmarshal(b, status)
	return status, e
}

func (index *Index) Mapping() (i interface{}, e error) {
	rsp, e := index.request("GET", index.IndexUrl()+"/_mapping", i)
	if e != nil {
		if rsp.StatusCode == 404 {
			return nil, nil
		}
		return nil, e
	}
	e = json.Unmarshal(rsp.Body, &i)
	if e != nil {
		return nil, e
	}
	return i, nil
}

func (index *Index) PutMapping(mapping interface{}) (rsp *HttpResponse, e error) {
	return index.request("PUT", index.IndexUrl()+"/", mapping)
}

func (index *Index) BaseUrl() string {
	if index.Port == 0 {
		index.Port = 9200
	}
	return fmt.Sprintf("http://%s:%d", index.Host, index.Port)
}

func (index *Index) IndexUrl() string {
	if index.Index != "" {
		return index.BaseUrl() + "/" + index.Index
	}
	return ""
}

func (index *Index) TypeUrl() string {
	if base := index.IndexUrl(); base != "" && index.Type != "" {
		return base + "/" + index.Type
	}
	return ""
	return ""
}

func (index *Index) Search(req *Request) (rsp *Response, e error) {
	writer := &bytes.Buffer{}
	js := json.NewEncoder(writer)
	e = js.Encode(req)
	if e != nil {
		return nil, e
	}
	u := index.TypeUrl()
	if !strings.HasSuffix(u, "/") {
		u += "/"
	}
	u += "/_search"
	httpRequest, e := http.NewRequest("POST", u, writer)
	if e != nil {
		return nil, e
	}
	httpResponse, e := http.DefaultClient.Do(httpRequest)
	if e != nil {
		return nil, e
	}
	defer httpResponse.Body.Close()
	dec := json.NewDecoder(httpResponse.Body)
	rsp = &Response{}
	e = dec.Decode(rsp)
	if e != nil {
		return nil, e
	}
	return rsp, nil
}

func (index *Index) Post(u string, i interface{}) (*HttpResponse, error) {
	return index.request("POST", u, i)
}

func (index *Index) PutObject(id string, i interface{}) (*HttpResponse, error) {
	return index.request("PUT", index.TypeUrl()+"/"+id, i)
}

func (index *Index) Put(u string, i interface{}) (*HttpResponse, error) {
	return index.request("PUT", u, i)
}

type HttpResponse struct {
	*http.Response
	Body []byte
}

func (index *Index) request(method string, u string, i interface{}) (httpResponse *HttpResponse, e error) {
	var req *http.Request
	if i != nil {
		buf := &bytes.Buffer{}
		encoder := json.NewEncoder(buf)
		if e := encoder.Encode(i); e != nil {
			return nil, e
		}
		req, e = http.NewRequest(method, u, buf)
	} else {
		req, e = http.NewRequest(method, u, nil)
	}
	if e != nil {
		return nil, e
	}
	rsp, e := http.DefaultClient.Do(req)
	if e != nil {
		return nil, e
	}
	defer rsp.Body.Close()
	b, e := ioutil.ReadAll(rsp.Body)
	if e != nil {
		return nil, e
	}
	httpResponse = &HttpResponse{
		Response: rsp,
		Body:     b,
	}
	if e != nil {
		return httpResponse, e
	}
	if rsp.Status[0] != OK {
		return httpResponse, fmt.Errorf("error indexing: %s %s", rsp.Status, string(b))
	}
	return httpResponse, nil
}
<|endoftext|>


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

package cmd

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

	"github.com/spf13/cobra"
	"github.com/spf13/pflag"

	v2 "istio.io/istio/pilot/pkg/proxy/envoy/v2"

	istioVersion "istio.io/pkg/version"
)

type sidecarSyncStatus struct {
	// nolint: structcheck, unused
	pilot string
	v2.SyncStatus
}

func newVersionCommand() *cobra.Command {
	versionCmd := istioVersion.CobraCommandWithOptions(istioVersion.CobraOptions{
		GetRemoteVersion: getRemoteInfo,
		GetProxyVersions: getProxyInfo,
	})
	versionCmd.Flags().VisitAll(func(flag *pflag.Flag) {
		if flag.Name == "short" {
			err := flag.Value.Set("true")
			if err != nil {
				fmt.Fprint(os.Stdout, fmt.Sprintf("set flag %q as true failed due to error %v", flag.Name, err))
			}
		}
		if flag.Name == "remote" {
			err := flag.Value.Set("true")
			if err != nil {
				fmt.Fprint(os.Stdout, fmt.Sprintf("set flag %q as true failed due to error %v", flag.Name, err))
			}
		}
	})
	return versionCmd
}

func getRemoteInfo() (*istioVersion.MeshInfo, error) {
	kubeClient, err := clientExecFactory(kubeconfig, configContext)
	if err != nil {
		return nil, err
	}

	return kubeClient.GetIstioVersions(istioNamespace)
}

func getProxyInfo() (*[]istioVersion.ProxyInfo, error) {
	kubeClient, err := clientExecFactory(kubeconfig, configContext)
	if err != nil {
		return nil, err
	}

	// Ask Pilot for the Envoy sidecar sync status, which includes the sidecar version info
	allSyncz, err := kubeClient.AllPilotsDiscoveryDo(istioNamespace, "GET", "/debug/syncz", nil)
	if err != nil {
		return nil, err
	}

	var pi []istioVersion.ProxyInfo
	for _, syncz := range allSyncz {
		var sss []*sidecarSyncStatus
		err = json.Unmarshal(syncz, &sss)
		if err != nil {
			return nil, err
		}
		pi = make([]istioVersion.ProxyInfo, 0, len(sss))
		for _, ss := range sss {
			pi = append(pi, istioVersion.ProxyInfo{
				ID:           ss.ProxyID,
				IstioVersion: ss.SyncStatus.IstioVersion,
			})
		}
	}

	return &pi, nil
}
<commit_msg>Revert "create slice with capacity, when capacity is known (#18504)" (#18508)<commit_after>// Copyright 2019 Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

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

	"github.com/spf13/cobra"
	"github.com/spf13/pflag"

	v2 "istio.io/istio/pilot/pkg/proxy/envoy/v2"

	istioVersion "istio.io/pkg/version"
)

type sidecarSyncStatus struct {
	// nolint: structcheck, unused
	pilot string
	v2.SyncStatus
}

func newVersionCommand() *cobra.Command {
	versionCmd := istioVersion.CobraCommandWithOptions(istioVersion.CobraOptions{
		GetRemoteVersion: getRemoteInfo,
		GetProxyVersions: getProxyInfo,
	})
	versionCmd.Flags().VisitAll(func(flag *pflag.Flag) {
		if flag.Name == "short" {
			err := flag.Value.Set("true")
			if err != nil {
				fmt.Fprint(os.Stdout, fmt.Sprintf("set flag %q as true failed due to error %v", flag.Name, err))
			}
		}
		if flag.Name == "remote" {
			err := flag.Value.Set("true")
			if err != nil {
				fmt.Fprint(os.Stdout, fmt.Sprintf("set flag %q as true failed due to error %v", flag.Name, err))
			}
		}
	})
	return versionCmd
}

func getRemoteInfo() (*istioVersion.MeshInfo, error) {
	kubeClient, err := clientExecFactory(kubeconfig, configContext)
	if err != nil {
		return nil, err
	}

	return kubeClient.GetIstioVersions(istioNamespace)
}

func getProxyInfo() (*[]istioVersion.ProxyInfo, error) {
	kubeClient, err := clientExecFactory(kubeconfig, configContext)
	if err != nil {
		return nil, err
	}

	// Ask Pilot for the Envoy sidecar sync status, which includes the sidecar version info
	allSyncz, err := kubeClient.AllPilotsDiscoveryDo(istioNamespace, "GET", "/debug/syncz", nil)
	if err != nil {
		return nil, err
	}

	pi := []istioVersion.ProxyInfo{}
	for _, syncz := range allSyncz {
		var sss []*sidecarSyncStatus
		err = json.Unmarshal(syncz, &sss)
		if err != nil {
			return nil, err
		}

		for _, ss := range sss {
			pi = append(pi, istioVersion.ProxyInfo{
				ID:           ss.ProxyID,
				IstioVersion: ss.SyncStatus.IstioVersion,
			})
		}
	}

	return &pi, nil
}
<|endoftext|>


<commit_before>package cc

import (
	"sync"
)

// Solve populates solutions with valid boards of the dimensions columns*rows
// with valid configurations for pieces.
// Returns a map with all solutions as key, in common notation.
func Solve(columns, rows uint8, pieces []Piece) map[string]bool {
	np := len(pieces)

	// No possible solutions
	if np != 1 && np >= int(columns*rows) {
		return make(map[string]bool)
	}

	wg := &sync.WaitGroup{}
	ch := make(chan string)

	// Start a goroutine for each possible starting position
	for j := uint8(0); j < rows; j++ {
		for i := uint8(0); i < columns; i++ {
			wg.Add(1)
			go func(i, j uint8) {
				b := NewBoard(columns, rows)
				// Shift the pieces to get the first
				p, pieces := pieces[0], pieces[1:]
				cc := cell(0)
				switch p {
				case King:
					cc = cell(King)
				case Rook:
					cc = cell(Rook)
				case Queen:
					cc = cell(Queen)
				case Bishop:
					cc = cell(Bishop)
				case Knight:
					cc = cell(Knight)
				}
				b.cells[j][i] = cc // Place the piece

				// Mark all dead cells
				tr := p.Threatening(&b, i, j)
				for _, t := range tr {
					b.cells[t.y][t.x] = cell(Dead)
				}
				place(b, pieces, ch)
				wg.Done()
			}(i, j)
		}
	}

	// Syncronize the go routines by closing the channel when they are finished
	go func(wg *sync.WaitGroup, ch chan string) {
		wg.Wait()
		close(ch)
	}(wg, ch)

	// Syncronize the read from the channel so we dont exit to fast
	done := make(chan bool, 1)
	solutions := make(map[string]bool)
	go func(ch <-chan string, done chan<- bool) {
		for s := range ch {
			solutions[s] = true
		}
		done <- true
	}(ch, done)

	<-done

	return solutions
}

// place tries to place the next piece on the board and recurse down
// the search tree. Appends the board and returns when a valid configuration
// is found.
func place(board Board, pieces []Piece, ch chan<- string) {
	if len(pieces) == 0 {
		//(*solutions)[board.Notation()] = true
		ch <- board.Notation()
		return
	}

	// Shift the pieces to get the first
	p, pieces := pieces[0], pieces[1:]

	for j := uint8(0); j < board.rows; j++ {
		for i := uint8(0); i < board.columns; i++ {
			c := board.cells[j][i]
			if c == cell(Dead) || c != cell(Blank) {
				continue
			}

			// Check so we don't threaten a placed piece
			canPlace := true
			tr := p.Threatening(&board, i, j)
			for _, t := range tr {
				tc := board.cells[t.y][t.x]
				if tc > cell(Dead) {
					canPlace = false
				}
			}

			if canPlace {

				// Create a copy of the current board to use when reqursing down
				b2 := Board{
					columns: board.columns,
					rows:    board.rows,
					cells:   make([][]cell, len(board.cells)),
				}
				for r := uint8(0); r < board.rows; r++ {
					b2.cells[r] = make([]cell, len(board.cells[r]))
					copy(b2.cells[r], board.cells[r])
				}

				cc := cell(0)
				switch p {
				case King:
					cc = cell(King)
				case Rook:
					cc = cell(Rook)
				case Queen:
					cc = cell(Queen)
				case Bishop:
					cc = cell(Bishop)
				case Knight:
					cc = cell(Knight)
				}
				b2.cells[j][i] = cc // Place the piece

				// Mark all dead cells
				for _, t := range tr {
					b2.cells[t.y][t.x] = cell(Dead)
				}

				// Recurse down with new board
				place(b2, pieces, ch)
			}
		}
	}
}
<commit_msg>Remove commented code<commit_after>package cc

import (
	"sync"
)

// Solve populates solutions with valid boards of the dimensions columns*rows
// with valid configurations for pieces.
// Returns a map with all solutions as key, in common notation.
func Solve(columns, rows uint8, pieces []Piece) map[string]bool {
	np := len(pieces)

	// No possible solutions
	if np != 1 && np >= int(columns*rows) {
		return make(map[string]bool)
	}

	wg := &sync.WaitGroup{}
	ch := make(chan string)

	// Start a goroutine for each possible starting position
	for j := uint8(0); j < rows; j++ {
		for i := uint8(0); i < columns; i++ {
			wg.Add(1)
			go func(i, j uint8) {
				b := NewBoard(columns, rows)
				// Shift the pieces to get the first
				p, pieces := pieces[0], pieces[1:]
				cc := cell(0)
				switch p {
				case King:
					cc = cell(King)
				case Rook:
					cc = cell(Rook)
				case Queen:
					cc = cell(Queen)
				case Bishop:
					cc = cell(Bishop)
				case Knight:
					cc = cell(Knight)
				}
				b.cells[j][i] = cc // Place the piece

				// Mark all dead cells
				tr := p.Threatening(&b, i, j)
				for _, t := range tr {
					b.cells[t.y][t.x] = cell(Dead)
				}
				place(b, pieces, ch)
				wg.Done()
			}(i, j)
		}
	}

	// Syncronize the go routines by closing the channel when they are finished
	go func(wg *sync.WaitGroup, ch chan string) {
		wg.Wait()
		close(ch)
	}(wg, ch)

	// Syncronize the read from the channel so we dont exit to fast
	done := make(chan bool, 1)
	solutions := make(map[string]bool)
	go func(ch <-chan string, done chan<- bool) {
		for s := range ch {
			solutions[s] = true
		}
		done <- true
	}(ch, done)

	<-done

	return solutions
}

// place tries to place the next piece on the board and recurse down
// the search tree. Appends the board and returns when a valid configuration
// is found.
func place(board Board, pieces []Piece, ch chan<- string) {
	if len(pieces) == 0 {
		ch <- board.Notation()
		return
	}

	// Shift the pieces to get the first
	p, pieces := pieces[0], pieces[1:]

	for j := uint8(0); j < board.rows; j++ {
		for i := uint8(0); i < board.columns; i++ {
			c := board.cells[j][i]
			if c == cell(Dead) || c != cell(Blank) {
				continue
			}

			// Check so we don't threaten a placed piece
			canPlace := true
			tr := p.Threatening(&board, i, j)
			for _, t := range tr {
				tc := board.cells[t.y][t.x]
				if tc > cell(Dead) {
					canPlace = false
				}
			}

			if canPlace {

				// Create a copy of the current board to use when reqursing down
				b2 := Board{
					columns: board.columns,
					rows:    board.rows,
					cells:   make([][]cell, len(board.cells)),
				}
				for r := uint8(0); r < board.rows; r++ {
					b2.cells[r] = make([]cell, len(board.cells[r]))
					copy(b2.cells[r], board.cells[r])
				}

				cc := cell(0)
				switch p {
				case King:
					cc = cell(King)
				case Rook:
					cc = cell(Rook)
				case Queen:
					cc = cell(Queen)
				case Bishop:
					cc = cell(Bishop)
				case Knight:
					cc = cell(Knight)
				}
				b2.cells[j][i] = cc // Place the piece

				// Mark all dead cells
				for _, t := range tr {
					b2.cells[t.y][t.x] = cell(Dead)
				}

				// Recurse down with new board
				place(b2, pieces, ch)
			}
		}
	}
}
<|endoftext|>


<commit_before>package qfy

import (
	"encoding/json"
	"fmt"
	"hash/crc64"

	"github.com/bsm/intset"
)

var crcTable = crc64.MakeTable(crc64.ECMA)

// Fact is an interface of a fact that may be passed to a qualifier. Each fact must implement
// a Get(string) method which receives the attribute name and must return either a string or
// an int slice, depending on the attribute definition.
type Fact interface {
	Get(string) interface{}
}

// --------------------------------------------------------------------

type AttrType uint8

const (
	TypeUnknown AttrType = iota
	TypeStringSlice
	TypeIntSlice
)

// Attribute defines a qualifiable fact attribute;
// must have a name and a type
type Attribute struct {
	Name string
	Type AttrType
}

// --------------------------------------------------------------------

// RuleDef contains a JSON-parseable rule definition
type RuleDef struct {
	Attr string      // the attribute name
	Op   string      // the operation code, either '+' or '-'
	Vals interface{} // the rule values, can be either an array of strings or ints
}

// UnmarshalJSON decodes JSON
func (r *RuleDef) UnmarshalJSON(data []byte) error {
	var temp *ruleDef
	err := json.Unmarshal(data, &temp)
	if err != nil {
		return err
	}

	rdef := RuleDef{Attr: temp.Attr, Op: temp.Op, Vals: nil}
	if rdef.Vals, err = temp.DecodeVals(); err != nil {
		return err
	}

	*r = rdef
	_, err = r.DetectType()
	return err
}

// DetectType attempts to detect the type of the values
func (r *RuleDef) DetectType() (AttrType, error) {
	switch r.Vals.(type) {
	case []string, string:
		return TypeStringSlice, nil
	case []int, int:
		return TypeIntSlice, nil
	}
	return TypeUnknown, r.invalid()
}

func (r *RuleDef) invalid() error {
	return fmt.Errorf("qfy: invalid rule: %s %s%v", r.Attr, r.Op, r.Vals)
}

func (r *RuleDef) toRule(dict strDict) (Rule, error) {
	var vals []int

	switch vv := r.Vals.(type) {
	case []string:
		vals = dict.FetchSlice(vv...)
	case []int:
		vals = vv
	case string:
		vals = dict.FetchSlice(vv)
	case int:
		vals = []int{vv}
	}

	if len(vals) == 0 {
		return nil, r.invalid()
	}

	switch r.Op {
	case "+":
		return newPlusRule(vals), nil
	case "-":
		return newMinusRule(vals), nil
	}
	return nil, r.invalid()
}

// --------------------------------------------------------------------

type ruleDef struct {
	Attr string          `json:"attr"`
	Op   string          `json:"op"`
	Vals json.RawMessage `json:"values"`
}

func (r *ruleDef) DecodeVals() (interface{}, error) {
	raw := r.Vals
	if len(raw) == 0 {
		return nil, nil
	}

	switch raw[0] {
	case '[': // slice
		for _, c := range raw[1:] {

			switch c {
			case '"': // string slice
				var vals []string
				err := json.Unmarshal(raw, &vals)
				return vals, err
			case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': // int slice
				var vals []int
				err := json.Unmarshal(raw, &vals)
				return vals, err
			}
		}
	case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': // int
		var val int
		err := json.Unmarshal(raw, &val)
		return val, err
	case '"': // string
		var val string
		err := json.Unmarshal(raw, &val)
		return val, err
	}
	return nil, nil
}

// --------------------------------------------------------------------

type converter interface {
	convert(interface{}) *intset.Set
}

// the qualification lookup process abstraction
type lookup struct {
	results   []int
	converter converter
	ruleCache map[uint64]bool
	factCache map[string]*intset.Set
}

func newLookup(cvt converter) *lookup {
	return &lookup{
		results:   make([]int, 0, 100),
		converter: cvt,
		ruleCache: make(map[uint64]bool, 1000),
		factCache: make(map[string]*intset.Set, 20),
	}
}

func (l *lookup) Clear() {
	l.results = l.results[:0]
	for k, _ := range l.ruleCache {
		delete(l.ruleCache, k)
	}
	for k, _ := range l.factCache {
		delete(l.factCache, k)
	}
}
<commit_msg>Remove attributes<commit_after>package qfy

import (
	"encoding/json"
	"fmt"
	"hash/crc64"

	"github.com/bsm/intset"
)

var crcTable = crc64.MakeTable(crc64.ECMA)

// Fact is an interface of a fact that may be passed to a qualifier. Each fact must implement
// a Get(string) method which receives the attribute name and must return either a string or
// an int slice, depending on the attribute definition.
type Fact interface {
	Get(string) interface{}
}

// --------------------------------------------------------------------

type AttrType uint8

const (
	TypeUnknown AttrType = iota
	TypeStringSlice
	TypeIntSlice
)

// --------------------------------------------------------------------

// RuleDef contains a JSON-parseable rule definition
type RuleDef struct {
	Attr string      // the attribute name
	Op   string      // the operation code, either '+' or '-'
	Vals interface{} // the rule values, can be either an array of strings or ints
}

// UnmarshalJSON decodes JSON
func (r *RuleDef) UnmarshalJSON(data []byte) error {
	var temp *ruleDef
	err := json.Unmarshal(data, &temp)
	if err != nil {
		return err
	}

	rdef := RuleDef{Attr: temp.Attr, Op: temp.Op, Vals: nil}
	if rdef.Vals, err = temp.DecodeVals(); err != nil {
		return err
	}

	*r = rdef
	_, err = r.DetectType()
	return err
}

// DetectType attempts to detect the type of the values
func (r *RuleDef) DetectType() (AttrType, error) {
	switch r.Vals.(type) {
	case []string, string:
		return TypeStringSlice, nil
	case []int, int:
		return TypeIntSlice, nil
	}
	return TypeUnknown, r.invalid()
}

func (r *RuleDef) invalid() error {
	return fmt.Errorf("qfy: invalid rule: %s %s%v", r.Attr, r.Op, r.Vals)
}

func (r *RuleDef) toRule(dict strDict) (Rule, error) {
	var vals []int

	switch vv := r.Vals.(type) {
	case []string:
		vals = dict.FetchSlice(vv...)
	case []int:
		vals = vv
	case string:
		vals = dict.FetchSlice(vv)
	case int:
		vals = []int{vv}
	}

	if len(vals) == 0 {
		return nil, r.invalid()
	}

	switch r.Op {
	case "+":
		return newPlusRule(vals), nil
	case "-":
		return newMinusRule(vals), nil
	}
	return nil, r.invalid()
}

// --------------------------------------------------------------------

type ruleDef struct {
	Attr string          `json:"attr"`
	Op   string          `json:"op"`
	Vals json.RawMessage `json:"values"`
}

func (r *ruleDef) DecodeVals() (interface{}, error) {
	raw := r.Vals
	if len(raw) == 0 {
		return nil, nil
	}

	switch raw[0] {
	case '[': // slice
		for _, c := range raw[1:] {

			switch c {
			case '"': // string slice
				var vals []string
				err := json.Unmarshal(raw, &vals)
				return vals, err
			case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': // int slice
				var vals []int
				err := json.Unmarshal(raw, &vals)
				return vals, err
			}
		}
	case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': // int
		var val int
		err := json.Unmarshal(raw, &val)
		return val, err
	case '"': // string
		var val string
		err := json.Unmarshal(raw, &val)
		return val, err
	}
	return nil, nil
}

// --------------------------------------------------------------------

type converter interface {
	convert(interface{}) *intset.Set
}

// the qualification lookup process abstraction
type lookup struct {
	results   []int
	converter converter
	ruleCache map[uint64]bool
	factCache map[string]*intset.Set
}

func newLookup(cvt converter) *lookup {
	return &lookup{
		results:   make([]int, 0, 100),
		converter: cvt,
		ruleCache: make(map[uint64]bool, 1000),
		factCache: make(map[string]*intset.Set, 20),
	}
}

func (l *lookup) Clear() {
	l.results = l.results[:0]
	for k, _ := range l.ruleCache {
		delete(l.ruleCache, k)
	}
	for k, _ := range l.factCache {
		delete(l.factCache, k)
	}
}
<|endoftext|>


<commit_before>// Copyright 2014 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package cloudstack

import (
	"bytes"
	"crypto/hmac"
	"crypto/sha1"
	"encoding/base64"
	"encoding/json"
	"fmt"
	"github.com/tsuru/config"
	"github.com/tsuru/tsuru/iaas"
	"io/ioutil"
	"net/http"
	"net/url"
	"sort"
	"strings"
)

func init() {
	iaas.RegisterIaasProvider("cloudstack", &CloudstackIaaS{})
}

type CloudstackIaaS struct{}

type NetInterface struct {
	IpAddress string
}

type CloudstackVirtualMachine struct {
	Nic []NetInterface
}

func (cs *CloudstackVirtualMachine) IsAvailable() bool {
	return true
}

func (i *CloudstackIaaS) DeleteMachine(machine *iaas.Machine) error {
	return nil
}

func (i *CloudstackIaaS) CreateMachine(params map[string]string) (*iaas.Machine, error) {
	url, err := buildUrl("deployVirtualMachine", params)
	if err != nil {
		return nil, err
	}
	resp, err := http.Get(url)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}
	var vmStatus map[string]string
	err = json.Unmarshal(body, &vmStatus)
	if err != nil {
		return nil, err
	}
	csVm, err := waitVMIsCreated(vmStatus)
	if err != nil {
		return nil, err
	}
	m := &iaas.Machine{
		Id:      csVm.Nic[0].IpAddress,
		Address: csVm.Nic[0].IpAddress,
		Status:  "running",
	}
	return m, nil
}

func buildUrl(command string, params map[string]string) (string, error) {
	apiKey, err := config.GetString("iaas:cloudstack:api-key")
	if err != nil {
		return "", err
	}
	secretKey, err := config.GetString("iaas:cloudstack:secret-key")
	if err != nil {
		return "", err
	}
	params["command"] = command
	params["response"] = "json"
	params["apiKey"] = apiKey
	var sorted_keys []string
	for k := range params {
		sorted_keys = append(sorted_keys, k)
	}
	sort.Strings(sorted_keys)
	var string_params []string
	for _, key := range sorted_keys {
		queryStringParam := fmt.Sprintf("%s=%s", key, url.QueryEscape(params[key]))
		string_params = append(string_params, queryStringParam)
	}
	queryString := strings.Join(string_params, "&")
	digest := hmac.New(sha1.New, []byte(secretKey))
	digest.Write([]byte(queryString))
	signature := base64.StdEncoding.EncodeToString(digest.Sum(nil))
	cloudstackUrl, err := config.GetString("iaas:cloudstack:url")
	if err != nil {
		return "", err
	}
	return fmt.Sprintf("%s?%s&signature=%s", cloudstackUrl, queryString, signature), nil
}

func waitVMIsCreated(vmStatus map[string]string) (*CloudstackVirtualMachine, error) {
	vmJson := `{"nic": [{"ipaddress": "0.0.0.0"}]}`
	vmJsonBuffer := bytes.NewBufferString(vmJson)
	var vm CloudstackVirtualMachine
	err := json.Unmarshal(vmJsonBuffer.Bytes(), &vm)
	if err != nil {
		return nil, err
	}
	if vm.IsAvailable() {
		return &vm, nil
	}
	return &vm, nil
}
<commit_msg>fix signature to cloudstack api<commit_after>// Copyright 2014 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package cloudstack

import (
	"bytes"
	"crypto/hmac"
	"crypto/sha1"
	"encoding/base64"
	"encoding/json"
	"fmt"
	"github.com/tsuru/config"
	"github.com/tsuru/tsuru/iaas"
	"io/ioutil"
	"net/http"
	"net/url"
	"sort"
	"strings"
)

func init() {
	iaas.RegisterIaasProvider("cloudstack", &CloudstackIaaS{})
}

type CloudstackIaaS struct{}

type NetInterface struct {
	IpAddress string
}

type CloudstackVirtualMachine struct {
	Nic []NetInterface
}

func (cs *CloudstackVirtualMachine) IsAvailable() bool {
	return true
}

func (i *CloudstackIaaS) DeleteMachine(machine *iaas.Machine) error {
	return nil
}

func (i *CloudstackIaaS) CreateMachine(params map[string]string) (*iaas.Machine, error) {
	url, err := buildUrl("deployVirtualMachine", params)
	if err != nil {
		return nil, err
	}
	resp, err := http.Get(url)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}
	var vmStatus map[string]interface{}
	err = json.Unmarshal(body, &vmStatus)
	if err != nil {
		return nil, err
	}
	csVm, err := waitVMIsCreated(vmStatus)
	if err != nil {
		return nil, err
	}
	m := &iaas.Machine{
		Id:      csVm.Nic[0].IpAddress,
		Address: csVm.Nic[0].IpAddress,
		Status:  "running",
	}
	return m, nil
}

func buildUrl(command string, params map[string]string) (string, error) {
	apiKey, err := config.GetString("iaas:cloudstack:api-key")
	if err != nil {
		return "", err
	}
	secretKey, err := config.GetString("iaas:cloudstack:secret-key")
	if err != nil {
		return "", err
	}
	params["command"] = command
	params["response"] = "json"
	params["apiKey"] = apiKey
	var sorted_keys []string
	for k := range params {
		sorted_keys = append(sorted_keys, k)
	}
	sort.Strings(sorted_keys)
	var string_params []string
	for _, key := range sorted_keys {
		queryStringParam := fmt.Sprintf("%s=%s", key, url.QueryEscape(params[key]))
		string_params = append(string_params, queryStringParam)
	}
	queryString := strings.Join(string_params, "&")
	digest := hmac.New(sha1.New, []byte(secretKey))
	digest.Write([]byte(strings.ToLower(queryString)))
	signature := base64.StdEncoding.EncodeToString(digest.Sum(nil))
	cloudstackUrl, err := config.GetString("iaas:cloudstack:url")
	if err != nil {
		return "", err
	}
	return fmt.Sprintf("%s?%s&signature=%s", cloudstackUrl, queryString, url.QueryEscape(signature)), nil
}

func waitVMIsCreated(vmStatus map[string]string) (*CloudstackVirtualMachine, error) {
	vmJson := `{"nic": [{"ipaddress": "0.0.0.0"}]}`
	vmJsonBuffer := bytes.NewBufferString(vmJson)
	var vm CloudstackVirtualMachine
	err := json.Unmarshal(vmJsonBuffer.Bytes(), &vm)
	if err != nil {
		return nil, err
	}
	if vm.IsAvailable() {
		return &vm, nil
	}
	return &vm, nil
}
<|endoftext|>


<commit_before>package esso

import (
	"github.com/gorilla/mux"
	"html/template"
	"net/http"
)

var articleSlice Articles
var articleHash ArticleMap

func App() *mux.Router {
	var err error
	app := mux.NewRouter()
	articleSlice, err = LoadArticles("articles/*.html")
	if err != nil {
		panic(err)
	}
	articleHash = articleSlice.ArticleMap()
	app.HandleFunc("/", ArticlesHandler)
	app.HandleFunc("/articles/", ArticlesHandler)
	app.HandleFunc("/articles/{slug}", ArticleHandler)
	app.Handle("/static/{page:.*}", http.FileServer(http.Dir("public")))
	return app
}

var baseTpl = template.Must(template.ParseFiles("templates/base.html"))

var articlesTpl = template.Must(template.Must(baseTpl.Clone()).ParseFiles("templates/article.html"))

func ArticlesHandler(w http.ResponseWriter, r *http.Request) {
	data := Page{Articles: articleSlice, Title: "Essocony: All Articles"}
	articleTpl.Execute(w, data)
}

var articleTpl = template.Must(template.Must(baseTpl.Clone()).ParseFiles("templates/article.html"))

func ArticleHandler(w http.ResponseWriter, r *http.Request) {
	slug := mux.Vars(r)["slug"]
	article, found := articleHash[slug]
	if !found {
		http.NotFound(w, r)
		return
	}
	data := Page{Articles: Articles{article}, Title: "Essocony: " + article.Title}
	articlesTpl.Execute(w, data)
}
<commit_msg>update<commit_after>package esso

import (
	"github.com/gorilla/mux"
	"html/template"
	"net/http"
)

var articleSlice Articles
var articleHash ArticleMap

func App() *mux.Router {
	var err error
	baseTpl = template.Must(template.ParseFiles("templates/base.html"))
	articleTpl = template.Must(template.Must(baseTpl.Clone()).ParseFiles("templates/article.html"))
	articlesTpl = template.Must(template.Must(baseTpl.Clone()).ParseFiles("templates/article.html"))
	app := mux.NewRouter()
	articleSlice, err = LoadArticles("articles/*.html")
	if err != nil {
		panic(err)
	}
	articleHash = articleSlice.ArticleMap()
	app.HandleFunc("/", ArticlesHandler)
	app.HandleFunc("/articles/", ArticlesHandler)
	app.HandleFunc("/articles/{slug}", ArticleHandler)
	app.Handle("/static/{page:.*}", http.FileServer(http.Dir("public")))
	return app
}

var baseTpl, articlesTpl, articleTpl *template.Template

func ArticlesHandler(w http.ResponseWriter, r *http.Request) {
	data := Page{Articles: articleSlice, Title: "Essocony: All Articles"}
	articleTpl.Execute(w, data)
}

func ArticleHandler(w http.ResponseWriter, r *http.Request) {
	slug := mux.Vars(r)["slug"]
	article, found := articleHash[slug]
	if !found {
		http.NotFound(w, r)
		return
	}
	data := Page{Articles: Articles{article}, Title: "Essocony: " + article.Title}
	articlesTpl.Execute(w, data)
}
<|endoftext|>


<commit_before>package eth

import (
	"math"
	"sync/atomic"
	"time"

	"github.com/ethereum/go-ethereum/eth/downloader"
	"github.com/ethereum/go-ethereum/logger"
	"github.com/ethereum/go-ethereum/logger/glog"
)

// Sync contains all synchronisation code for the eth protocol

func (pm *ProtocolManager) update() {
	forceSync := time.Tick(forceSyncCycle)
	blockProc := time.Tick(blockProcCycle)
	blockProcPend := int32(0)

	for {
		select {
		case <-pm.newPeerCh:
			// Meet the `minDesiredPeerCount` before we select our best peer
			if len(pm.peers) < minDesiredPeerCount {
				break
			}
			// Find the best peer and synchronise with it
			peer := getBestPeer(pm.peers)
			if peer == nil {
				glog.V(logger.Debug).Infoln("Sync attempt canceled. No peers available")
			}
			go pm.synchronise(peer)

		case <-forceSync:
			// Force a sync even if not enough peers are present
			if peer := getBestPeer(pm.peers); peer != nil {
				go pm.synchronise(peer)
			}
		case <-blockProc:
			// Try to pull some blocks from the downloaded
			if atomic.CompareAndSwapInt32(&blockProcPend, 0, 1) {
				go func() {
					pm.processBlocks()
					atomic.StoreInt32(&blockProcPend, 0)
				}()
			}

		case <-pm.quitSync:
			return
		}
	}
}

// processBlocks will attempt to reconstruct a chain by checking the first item and check if it's
// a known parent. The first block in the chain may be unknown during downloading. When the
// downloader isn't downloading blocks will be dropped with an unknown parent until either it
// has depleted the list or found a known parent.
func (pm *ProtocolManager) processBlocks() error {
	pm.wg.Add(1)
	defer pm.wg.Done()

	// Short circuit if no blocks are available for insertion
	blocks := pm.downloader.TakeBlocks()
	if len(blocks) == 0 {
		return nil
	}
	glog.V(logger.Debug).Infof("Inserting chain with %d blocks (#%v - #%v)\n", len(blocks), blocks[0].Number(), blocks[len(blocks)-1].Number())

	for len(blocks) != 0 && !pm.quit {
		max := int(math.Min(float64(len(blocks)), float64(blockProcAmount)))
		_, err := pm.chainman.InsertChain(blocks[:max])
		if err != nil {
			glog.V(logger.Warn).Infof("Block insertion failed: %v", err)
			pm.downloader.Cancel()
			return err
		}
		blocks = blocks[max:]
	}
	return nil
}

func (pm *ProtocolManager) synchronise(peer *peer) {
	// Make sure the peer's TD is higher than our own. If not drop.
	if peer.td.Cmp(pm.chainman.Td()) <= 0 {
		return
	}
	// FIXME if we have the hash in our chain and the TD of the peer is
	// much higher than ours, something is wrong with us or the peer.
	// Check if the hash is on our own chain
	if pm.chainman.HasBlock(peer.recentHash) {
		return
	}
	// Get the hashes from the peer (synchronously)
	glog.V(logger.Debug).Infof("Attempting synchronisation: %v, 0x%x", peer.id, peer.recentHash)

	err := pm.downloader.Synchronise(peer.id, peer.recentHash)
	switch err {
	case nil:
		glog.V(logger.Debug).Infof("Synchronisation completed")

	case downloader.ErrBusy:
		glog.V(logger.Debug).Infof("Synchronisation already in progress")

	case downloader.ErrTimeout, downloader.ErrBadPeer:
		glog.V(logger.Debug).Infof("Removing peer %v: %v", peer.id, err)
		pm.removePeer(peer)

	case downloader.ErrPendingQueue:
		glog.V(logger.Debug).Infoln("Synchronisation aborted:", err)

	default:
		glog.V(logger.Warn).Infof("Synchronisation failed: %v", err)
	}
}
<commit_msg>eth: drop a sync peer if it sends an invalid hash chain<commit_after>package eth

import (
	"math"
	"sync/atomic"
	"time"

	"github.com/ethereum/go-ethereum/eth/downloader"
	"github.com/ethereum/go-ethereum/logger"
	"github.com/ethereum/go-ethereum/logger/glog"
)

// Sync contains all synchronisation code for the eth protocol

func (pm *ProtocolManager) update() {
	forceSync := time.Tick(forceSyncCycle)
	blockProc := time.Tick(blockProcCycle)
	blockProcPend := int32(0)

	for {
		select {
		case <-pm.newPeerCh:
			// Meet the `minDesiredPeerCount` before we select our best peer
			if len(pm.peers) < minDesiredPeerCount {
				break
			}
			// Find the best peer and synchronise with it
			peer := getBestPeer(pm.peers)
			if peer == nil {
				glog.V(logger.Debug).Infoln("Sync attempt canceled. No peers available")
			}
			go pm.synchronise(peer)

		case <-forceSync:
			// Force a sync even if not enough peers are present
			if peer := getBestPeer(pm.peers); peer != nil {
				go pm.synchronise(peer)
			}
		case <-blockProc:
			// Try to pull some blocks from the downloaded
			if atomic.CompareAndSwapInt32(&blockProcPend, 0, 1) {
				go func() {
					pm.processBlocks()
					atomic.StoreInt32(&blockProcPend, 0)
				}()
			}

		case <-pm.quitSync:
			return
		}
	}
}

// processBlocks will attempt to reconstruct a chain by checking the first item and check if it's
// a known parent. The first block in the chain may be unknown during downloading. When the
// downloader isn't downloading blocks will be dropped with an unknown parent until either it
// has depleted the list or found a known parent.
func (pm *ProtocolManager) processBlocks() error {
	pm.wg.Add(1)
	defer pm.wg.Done()

	// Short circuit if no blocks are available for insertion
	blocks := pm.downloader.TakeBlocks()
	if len(blocks) == 0 {
		return nil
	}
	glog.V(logger.Debug).Infof("Inserting chain with %d blocks (#%v - #%v)\n", len(blocks), blocks[0].Number(), blocks[len(blocks)-1].Number())

	for len(blocks) != 0 && !pm.quit {
		max := int(math.Min(float64(len(blocks)), float64(blockProcAmount)))
		_, err := pm.chainman.InsertChain(blocks[:max])
		if err != nil {
			glog.V(logger.Warn).Infof("Block insertion failed: %v", err)
			pm.downloader.Cancel()
			return err
		}
		blocks = blocks[max:]
	}
	return nil
}

func (pm *ProtocolManager) synchronise(peer *peer) {
	// Make sure the peer's TD is higher than our own. If not drop.
	if peer.td.Cmp(pm.chainman.Td()) <= 0 {
		return
	}
	// FIXME if we have the hash in our chain and the TD of the peer is
	// much higher than ours, something is wrong with us or the peer.
	// Check if the hash is on our own chain
	if pm.chainman.HasBlock(peer.recentHash) {
		return
	}
	// Get the hashes from the peer (synchronously)
	glog.V(logger.Debug).Infof("Attempting synchronisation: %v, 0x%x", peer.id, peer.recentHash)

	err := pm.downloader.Synchronise(peer.id, peer.recentHash)
	switch err {
	case nil:
		glog.V(logger.Debug).Infof("Synchronisation completed")

	case downloader.ErrBusy:
		glog.V(logger.Debug).Infof("Synchronisation already in progress")

	case downloader.ErrTimeout, downloader.ErrBadPeer, downloader.ErrInvalidChain:
		glog.V(logger.Debug).Infof("Removing peer %v: %v", peer.id, err)
		pm.removePeer(peer)

	case downloader.ErrPendingQueue:
		glog.V(logger.Debug).Infoln("Synchronisation aborted:", err)

	default:
		glog.V(logger.Warn).Infof("Synchronisation failed: %v", err)
	}
}
<|endoftext|>


<commit_before>// Copyright 2014 shiena Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

// +build windows

package ansicolor

import (
	"bytes"
	"io"
	"strings"
	"syscall"
	"unsafe"
)

type csiState int

const (
	outsideCsiCode csiState = iota
	firstCsiCode
	secondeCsiCode
)

type ansiColorWriter struct {
	w        io.Writer
	state    csiState
	paramBuf bytes.Buffer
}

const (
	firstCsiChar   byte = '\x1b'
	secondeCsiChar byte = '['
	separatorChar  byte = ';'
	sgrCode        byte = 'm'
)

const (
	foregroundBlue      = uint16(0x0001)
	foregroundGreen     = uint16(0x0002)
	foregroundRed       = uint16(0x0004)
	foregroundIntensity = uint16(0x0008)
	backgroundBlue      = uint16(0x0010)
	backgroundGreen     = uint16(0x0020)
	backgroundRed       = uint16(0x0040)
	backgroundIntensity = uint16(0x0080)
	underscore          = uint16(0x8000)

	foregroundMask = foregroundBlue | foregroundGreen | foregroundRed | foregroundIntensity
	backgroundMask = backgroundBlue | backgroundGreen | backgroundRed | backgroundIntensity
)

const (
	ansiReset        = "0"
	ansiIntensityOn  = "1"
	ansiIntensityOff = "21"
	ansiUnderlineOn  = "4"
	ansiUnderlineOff = "24"
	ansiBlinkOn      = "5"
	ansiBlinkOff     = "25"

	ansiForegroundBlack   = "30"
	ansiForegroundRed     = "31"
	ansiForegroundGreen   = "32"
	ansiForegroundYellow  = "33"
	ansiForegroundBlue    = "34"
	ansiForegroundMagenta = "35"
	ansiForegroundCyan    = "36"
	ansiForegroundWhite   = "37"
	ansiForegroundDefault = "39"

	ansiBackgroundBlack   = "40"
	ansiBackgroundRed     = "41"
	ansiBackgroundGreen   = "42"
	ansiBackgroundYellow  = "43"
	ansiBackgroundBlue    = "44"
	ansiBackgroundMagenta = "45"
	ansiBackgroundCyan    = "46"
	ansiBackgroundWhite   = "47"
	ansiBackgroundDefault = "49"
)

type drawType int

const (
	foreground drawType = iota
	background
)

type winColor struct {
	code     uint16
	drawType drawType
}

var colorMap = map[string]winColor{
	ansiForegroundBlack:   {0, foreground},
	ansiForegroundRed:     {foregroundRed, foreground},
	ansiForegroundGreen:   {foregroundGreen, foreground},
	ansiForegroundYellow:  {foregroundRed | foregroundGreen, foreground},
	ansiForegroundBlue:    {foregroundBlue, foreground},
	ansiForegroundMagenta: {foregroundRed | foregroundBlue, foreground},
	ansiForegroundCyan:    {foregroundGreen | foregroundBlue, foreground},
	ansiForegroundWhite:   {foregroundRed | foregroundGreen | foregroundBlue, foreground},
	ansiForegroundDefault: {foregroundRed | foregroundGreen | foregroundBlue, foreground},

	ansiBackgroundBlack:   {0, background},
	ansiBackgroundRed:     {backgroundRed, background},
	ansiBackgroundGreen:   {backgroundGreen, background},
	ansiBackgroundYellow:  {backgroundRed | backgroundGreen, background},
	ansiBackgroundBlue:    {backgroundBlue, background},
	ansiBackgroundMagenta: {backgroundRed | backgroundBlue, background},
	ansiBackgroundCyan:    {backgroundGreen | backgroundBlue, background},
	ansiBackgroundWhite:   {backgroundRed | backgroundGreen | backgroundBlue, background},
	ansiBackgroundDefault: {0, background},
}

var (
	kernel32                       = syscall.NewLazyDLL("kernel32.dll")
	procSetConsoleTextAttribute    = kernel32.NewProc("SetConsoleTextAttribute")
	procGetConsoleScreenBufferInfo = kernel32.NewProc("GetConsoleScreenBufferInfo")
	defaultAttr                    *textAttributes
)

func init() {
	screenInfo := getConsoleScreenBufferInfo(uintptr(syscall.Stdout))
	if screenInfo != nil {
		colorMap[ansiForegroundDefault] = winColor{
			screenInfo.WAttributes & (foregroundRed | foregroundGreen | foregroundBlue),
			foreground,
		}
		colorMap[ansiBackgroundDefault] = winColor{
			screenInfo.WAttributes & (backgroundRed | backgroundGreen | backgroundBlue),
			background,
		}
		defaultAttr = convertTextAttr(screenInfo.WAttributes)
	}
}

type coord struct {
	X, Y int16
}

type smallRect struct {
	Left, Top, Right, Bottom int16
}

type consoleScreenBufferInfo struct {
	DwSize              coord
	DwCursorPosition    coord
	WAttributes         uint16
	SrWindow            smallRect
	DwMaximumWindowSize coord
}

func getConsoleScreenBufferInfo(hConsoleOutput uintptr) *consoleScreenBufferInfo {
	var csbi consoleScreenBufferInfo
	ret, _, _ := procGetConsoleScreenBufferInfo.Call(
		hConsoleOutput,
		uintptr(unsafe.Pointer(&csbi)))
	if ret == 0 {
		return nil
	}
	return &csbi
}

func setConsoleTextAttribute(hConsoleOutput uintptr, wAttributes uint16) bool {
	ret, _, _ := procSetConsoleTextAttribute.Call(
		hConsoleOutput,
		uintptr(wAttributes))
	return ret != 0
}

type textAttributes struct {
	foregroundColor     uint16
	backgroundColor     uint16
	foregroundIntensity uint16
	backgroundIntensity uint16
	underscore          uint16
	otherAttributes     uint16
}

func convertTextAttr(winAttr uint16) *textAttributes {
	fgColor := winAttr & (foregroundRed | foregroundGreen | foregroundBlue)
	bgColor := winAttr & (backgroundRed | backgroundGreen | backgroundBlue)
	fgIntensity := winAttr & foregroundIntensity
	bgIntensity := winAttr & backgroundIntensity
	underline := winAttr & underscore
	otherAttributes := winAttr &^ (foregroundMask | backgroundMask | underscore)
	return &textAttributes{fgColor, bgColor, fgIntensity, bgIntensity, underline, otherAttributes}
}

func convertWinAttr(textAttr *textAttributes) uint16 {
	var winAttr uint16 = 0
	winAttr |= textAttr.foregroundColor
	winAttr |= textAttr.backgroundColor
	winAttr |= textAttr.foregroundIntensity
	winAttr |= textAttr.backgroundIntensity
	winAttr |= textAttr.underscore
	winAttr |= textAttr.otherAttributes
	return winAttr
}

func changeColor(param []byte) {
	if defaultAttr == nil {
		return
	}

	screenInfo := getConsoleScreenBufferInfo(uintptr(syscall.Stdout))
	if screenInfo == nil {
		return
	}

	winAttr := convertTextAttr(screenInfo.WAttributes)
	strParam := string(param)
	if len(strParam) <= 0 {
		strParam = "0"
	}
	csiParam := strings.Split(strParam, string(separatorChar))
	for _, p := range csiParam {
		c, ok := colorMap[p]
		switch {
		case !ok:
			switch p {
			case ansiReset:
				winAttr.foregroundColor = defaultAttr.foregroundColor
				winAttr.backgroundColor = defaultAttr.backgroundColor
				winAttr.foregroundIntensity = defaultAttr.foregroundIntensity
				winAttr.backgroundIntensity = defaultAttr.backgroundIntensity
				winAttr.underscore = 0
				winAttr.otherAttributes = 0
			case ansiIntensityOn:
				winAttr.foregroundIntensity = foregroundIntensity
			case ansiIntensityOff:
				winAttr.foregroundIntensity = 0
			case ansiUnderlineOn:
				winAttr.underscore = underscore
			case ansiUnderlineOff:
				winAttr.underscore = 0
			case ansiBlinkOn:
				winAttr.backgroundIntensity = backgroundIntensity
			case ansiBlinkOff:
				winAttr.backgroundIntensity = 0
			default:
				// unknown code
			}
		case c.drawType == foreground:
			winAttr.foregroundColor = c.code
		case c.drawType == background:
			winAttr.backgroundColor = c.code
		}
	}
	winTextAttribute := convertWinAttr(winAttr)
	setConsoleTextAttribute(uintptr(syscall.Stdout), winTextAttribute)
}

func parseEscapeSequence(command byte, param []byte) {
	switch command {
	case sgrCode:
		changeColor(param)
	}
}

func isParameterChar(b byte) bool {
	return ('0' <= b && b <= '9') || b == separatorChar
}

func (cw *ansiColorWriter) Write(p []byte) (int, error) {
	r, nw, nc, first, last := 0, 0, 0, 0, 0
	var err error
	for i, ch := range p {
		switch cw.state {
		case outsideCsiCode:
			if ch == firstCsiChar {
				nc++
				cw.state = firstCsiCode
			}
		case firstCsiCode:
			switch ch {
			case firstCsiChar:
				nc++
				break
			case secondeCsiChar:
				nc++
				cw.state = secondeCsiCode
				last = i - 1
			default:
				cw.state = outsideCsiCode
			}
		case secondeCsiCode:
			nc++
			if isParameterChar(ch) {
				cw.paramBuf.WriteByte(ch)
			} else {
				nw, err = cw.w.Write(p[first:last])
				r += nw
				if err != nil {
					return r, err
				}
				first = i + 1
				param := cw.paramBuf.Bytes()
				cw.paramBuf.Reset()
				parseEscapeSequence(ch, param)
				cw.state = outsideCsiCode
			}
		default:
			cw.state = outsideCsiCode
		}
	}

	if cw.state == outsideCsiCode {
		nw, err = cw.w.Write(p[first:len(p)])
	}

	return r + nw + nc, err
}
<commit_msg>Add  high intensity colors<commit_after>// Copyright 2014 shiena Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

// +build windows

package ansicolor

import (
	"bytes"
	"io"
	"strings"
	"syscall"
	"unsafe"
)

type csiState int

const (
	outsideCsiCode csiState = iota
	firstCsiCode
	secondeCsiCode
)

type ansiColorWriter struct {
	w        io.Writer
	state    csiState
	paramBuf bytes.Buffer
}

const (
	firstCsiChar   byte = '\x1b'
	secondeCsiChar byte = '['
	separatorChar  byte = ';'
	sgrCode        byte = 'm'
)

const (
	foregroundBlue      = uint16(0x0001)
	foregroundGreen     = uint16(0x0002)
	foregroundRed       = uint16(0x0004)
	foregroundIntensity = uint16(0x0008)
	backgroundBlue      = uint16(0x0010)
	backgroundGreen     = uint16(0x0020)
	backgroundRed       = uint16(0x0040)
	backgroundIntensity = uint16(0x0080)
	underscore          = uint16(0x8000)

	foregroundMask = foregroundBlue | foregroundGreen | foregroundRed | foregroundIntensity
	backgroundMask = backgroundBlue | backgroundGreen | backgroundRed | backgroundIntensity
)

const (
	ansiReset        = "0"
	ansiIntensityOn  = "1"
	ansiIntensityOff = "21"
	ansiUnderlineOn  = "4"
	ansiUnderlineOff = "24"
	ansiBlinkOn      = "5"
	ansiBlinkOff     = "25"

	ansiForegroundBlack   = "30"
	ansiForegroundRed     = "31"
	ansiForegroundGreen   = "32"
	ansiForegroundYellow  = "33"
	ansiForegroundBlue    = "34"
	ansiForegroundMagenta = "35"
	ansiForegroundCyan    = "36"
	ansiForegroundWhite   = "37"
	ansiForegroundDefault = "39"

	ansiBackgroundBlack   = "40"
	ansiBackgroundRed     = "41"
	ansiBackgroundGreen   = "42"
	ansiBackgroundYellow  = "43"
	ansiBackgroundBlue    = "44"
	ansiBackgroundMagenta = "45"
	ansiBackgroundCyan    = "46"
	ansiBackgroundWhite   = "47"
	ansiBackgroundDefault = "49"

	ansiLightForegroundGray    = "90"
	ansiLightForegroundRed     = "91"
	ansiLightForegroundGreen   = "92"
	ansiLightForegroundYellow  = "93"
	ansiLightForegroundBlue    = "94"
	ansiLightForegroundMagenta = "95"
	ansiLightForegroundCyan    = "96"
	ansiLightForegroundWhite   = "97"
	ansiLightForegroundDefault = "99"
)

type drawType int

const (
	foreground drawType = iota
	background
)

type winColor struct {
	code     uint16
	drawType drawType
}

var colorMap = map[string]winColor{
	ansiForegroundBlack:   {0, foreground},
	ansiForegroundRed:     {foregroundRed, foreground},
	ansiForegroundGreen:   {foregroundGreen, foreground},
	ansiForegroundYellow:  {foregroundRed | foregroundGreen, foreground},
	ansiForegroundBlue:    {foregroundBlue, foreground},
	ansiForegroundMagenta: {foregroundRed | foregroundBlue, foreground},
	ansiForegroundCyan:    {foregroundGreen | foregroundBlue, foreground},
	ansiForegroundWhite:   {foregroundRed | foregroundGreen | foregroundBlue, foreground},
	ansiForegroundDefault: {foregroundRed | foregroundGreen | foregroundBlue, foreground},

	ansiBackgroundBlack:   {0, background},
	ansiBackgroundRed:     {backgroundRed, background},
	ansiBackgroundGreen:   {backgroundGreen, background},
	ansiBackgroundYellow:  {backgroundRed | backgroundGreen, background},
	ansiBackgroundBlue:    {backgroundBlue, background},
	ansiBackgroundMagenta: {backgroundRed | backgroundBlue, background},
	ansiBackgroundCyan:    {backgroundGreen | backgroundBlue, background},
	ansiBackgroundWhite:   {backgroundRed | backgroundGreen | backgroundBlue, background},
	ansiBackgroundDefault: {0, background},

	ansiLightForegroundGray:    {foregroundIntensity, foreground},
	ansiLightForegroundRed:     {foregroundIntensity | foregroundRed, foreground},
	ansiLightForegroundGreen:   {foregroundIntensity | foregroundGreen, foreground},
	ansiLightForegroundYellow:  {foregroundIntensity | foregroundRed | foregroundGreen, foreground},
	ansiLightForegroundBlue:    {foregroundIntensity | foregroundBlue, foreground},
	ansiLightForegroundMagenta: {foregroundIntensity | foregroundRed | foregroundBlue, foreground},
	ansiLightForegroundCyan:    {foregroundIntensity | foregroundGreen | foregroundBlue, foreground},
	ansiLightForegroundWhite:   {foregroundIntensity | foregroundRed | foregroundGreen | foregroundBlue, foreground},
}

var (
	kernel32                       = syscall.NewLazyDLL("kernel32.dll")
	procSetConsoleTextAttribute    = kernel32.NewProc("SetConsoleTextAttribute")
	procGetConsoleScreenBufferInfo = kernel32.NewProc("GetConsoleScreenBufferInfo")
	defaultAttr                    *textAttributes
)

func init() {
	screenInfo := getConsoleScreenBufferInfo(uintptr(syscall.Stdout))
	if screenInfo != nil {
		colorMap[ansiForegroundDefault] = winColor{
			screenInfo.WAttributes & (foregroundRed | foregroundGreen | foregroundBlue),
			foreground,
		}
		colorMap[ansiBackgroundDefault] = winColor{
			screenInfo.WAttributes & (backgroundRed | backgroundGreen | backgroundBlue),
			background,
		}
		defaultAttr = convertTextAttr(screenInfo.WAttributes)
	}
}

type coord struct {
	X, Y int16
}

type smallRect struct {
	Left, Top, Right, Bottom int16
}

type consoleScreenBufferInfo struct {
	DwSize              coord
	DwCursorPosition    coord
	WAttributes         uint16
	SrWindow            smallRect
	DwMaximumWindowSize coord
}

func getConsoleScreenBufferInfo(hConsoleOutput uintptr) *consoleScreenBufferInfo {
	var csbi consoleScreenBufferInfo
	ret, _, _ := procGetConsoleScreenBufferInfo.Call(
		hConsoleOutput,
		uintptr(unsafe.Pointer(&csbi)))
	if ret == 0 {
		return nil
	}
	return &csbi
}

func setConsoleTextAttribute(hConsoleOutput uintptr, wAttributes uint16) bool {
	ret, _, _ := procSetConsoleTextAttribute.Call(
		hConsoleOutput,
		uintptr(wAttributes))
	return ret != 0
}

type textAttributes struct {
	foregroundColor     uint16
	backgroundColor     uint16
	foregroundIntensity uint16
	backgroundIntensity uint16
	underscore          uint16
	otherAttributes     uint16
}

func convertTextAttr(winAttr uint16) *textAttributes {
	fgColor := winAttr & (foregroundRed | foregroundGreen | foregroundBlue)
	bgColor := winAttr & (backgroundRed | backgroundGreen | backgroundBlue)
	fgIntensity := winAttr & foregroundIntensity
	bgIntensity := winAttr & backgroundIntensity
	underline := winAttr & underscore
	otherAttributes := winAttr &^ (foregroundMask | backgroundMask | underscore)
	return &textAttributes{fgColor, bgColor, fgIntensity, bgIntensity, underline, otherAttributes}
}

func convertWinAttr(textAttr *textAttributes) uint16 {
	var winAttr uint16 = 0
	winAttr |= textAttr.foregroundColor
	winAttr |= textAttr.backgroundColor
	winAttr |= textAttr.foregroundIntensity
	winAttr |= textAttr.backgroundIntensity
	winAttr |= textAttr.underscore
	winAttr |= textAttr.otherAttributes
	return winAttr
}

func changeColor(param []byte) {
	if defaultAttr == nil {
		return
	}

	screenInfo := getConsoleScreenBufferInfo(uintptr(syscall.Stdout))
	if screenInfo == nil {
		return
	}

	winAttr := convertTextAttr(screenInfo.WAttributes)
	strParam := string(param)
	if len(strParam) <= 0 {
		strParam = "0"
	}
	csiParam := strings.Split(strParam, string(separatorChar))
	for _, p := range csiParam {
		c, ok := colorMap[p]
		switch {
		case !ok:
			switch p {
			case ansiReset:
				winAttr.foregroundColor = defaultAttr.foregroundColor
				winAttr.backgroundColor = defaultAttr.backgroundColor
				winAttr.foregroundIntensity = defaultAttr.foregroundIntensity
				winAttr.backgroundIntensity = defaultAttr.backgroundIntensity
				winAttr.underscore = 0
				winAttr.otherAttributes = 0
			case ansiIntensityOn:
				winAttr.foregroundIntensity = foregroundIntensity
			case ansiIntensityOff:
				winAttr.foregroundIntensity = 0
			case ansiUnderlineOn:
				winAttr.underscore = underscore
			case ansiUnderlineOff:
				winAttr.underscore = 0
			case ansiBlinkOn:
				winAttr.backgroundIntensity = backgroundIntensity
			case ansiBlinkOff:
				winAttr.backgroundIntensity = 0
			default:
				// unknown code
			}
		case c.drawType == foreground:
			winAttr.foregroundColor = c.code
		case c.drawType == background:
			winAttr.backgroundColor = c.code
		}
	}
	winTextAttribute := convertWinAttr(winAttr)
	setConsoleTextAttribute(uintptr(syscall.Stdout), winTextAttribute)
}

func parseEscapeSequence(command byte, param []byte) {
	switch command {
	case sgrCode:
		changeColor(param)
	}
}

func isParameterChar(b byte) bool {
	return ('0' <= b && b <= '9') || b == separatorChar
}

func (cw *ansiColorWriter) Write(p []byte) (int, error) {
	r, nw, nc, first, last := 0, 0, 0, 0, 0
	var err error
	for i, ch := range p {
		switch cw.state {
		case outsideCsiCode:
			if ch == firstCsiChar {
				nc++
				cw.state = firstCsiCode
			}
		case firstCsiCode:
			switch ch {
			case firstCsiChar:
				nc++
				break
			case secondeCsiChar:
				nc++
				cw.state = secondeCsiCode
				last = i - 1
			default:
				cw.state = outsideCsiCode
			}
		case secondeCsiCode:
			nc++
			if isParameterChar(ch) {
				cw.paramBuf.WriteByte(ch)
			} else {
				nw, err = cw.w.Write(p[first:last])
				r += nw
				if err != nil {
					return r, err
				}
				first = i + 1
				param := cw.paramBuf.Bytes()
				cw.paramBuf.Reset()
				parseEscapeSequence(ch, param)
				cw.state = outsideCsiCode
			}
		default:
			cw.state = outsideCsiCode
		}
	}

	if cw.state == outsideCsiCode {
		nw, err = cw.w.Write(p[first:len(p)])
	}

	return r + nw + nc, err
}
<|endoftext|>