<commit_before>package gads

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

const (
	baseUrl = "https://adwords.google.com/api/adwords/cm/v201509"
	//		baseUrl   = "http://127.0.0.1:4444"
	reportUrl = "https://adwords.google.com/api/adwords/reportdownload/v201509"
	//	reportUrl = "http://127.0.0.1:4444"
)

type ServiceUrl struct {
	Url  string
	Name string
}

// exceptions
var (
	ERROR_NOT_YET_IMPLEMENTED = fmt.Errorf("Not yet implemented")
)

var (
	configJson = flag.String("config_json", "./config.json", "API credentials")

	// service urls
	adGroupAdServiceUrl             = ServiceUrl{baseUrl, "AdGroupAdService"}
	adGroupBidModifierServiceUrl    = ServiceUrl{baseUrl, "AdGroupBidModifierService"}
	adGroupCriterionServiceUrl      = ServiceUrl{baseUrl, "AdGroupCriterionService"}
	adGroupFeedServiceUrl           = ServiceUrl{baseUrl, "AdGroupFeedService"}
	adGroupServiceUrl               = ServiceUrl{baseUrl, "AdGroupService"}
	adParamServiceUrl               = ServiceUrl{baseUrl, "AdParamService"}
	adwordsUserListServiceUrl       = ServiceUrl{baseUrl, "AdwordsUserListService"}
	biddingStrategyServiceUrl       = ServiceUrl{baseUrl, "BiddingStrategyService"}
	budgetOrderServiceUrl           = ServiceUrl{baseUrl, "BudgetOrderService"}
	budgetServiceUrl                = ServiceUrl{baseUrl, "BudgetService"}
	campaignAdExtensionServiceUrl   = ServiceUrl{baseUrl, "CampaignAdExtensionService"}
	campaignCriterionServiceUrl     = ServiceUrl{baseUrl, "CampaignCriterionService"}
	campaignFeedServiceUrl          = ServiceUrl{baseUrl, "CampaignFeedService"}
	campaignServiceUrl              = ServiceUrl{baseUrl, "CampaignService"}
	campaignSharedSetServiceUrl     = ServiceUrl{baseUrl, "CampaignSharedSetService"}
	constantDataServiceUrl          = ServiceUrl{baseUrl, "ConstantDataService"}
	conversionTrackerServiceUrl     = ServiceUrl{baseUrl, "ConversionTrackerService"}
	customerFeedServiceUrl          = ServiceUrl{baseUrl, "CustomerFeedService"}
	customerServiceUrl              = ServiceUrl{baseUrl, "CustomerService"}
	customerSyncServiceUrl          = ServiceUrl{baseUrl, "CustomerSyncService"}
	dataServiceUrl                  = ServiceUrl{baseUrl, "DataService"}
	experimentServiceUrl            = ServiceUrl{baseUrl, "ExperimentService"}
	feedItemServiceUrl              = ServiceUrl{baseUrl, "FeedItemService"}
	feedMappingServiceUrl           = ServiceUrl{baseUrl, "FeedMappingService"}
	feedServiceUrl                  = ServiceUrl{baseUrl, "FeedService"}
	geoLocationServiceUrl           = ServiceUrl{baseUrl, "GeoLocationService"}
	labelServiceUrl                 = ServiceUrl{baseUrl, "LabelService"}
	locationCriterionServiceUrl     = ServiceUrl{baseUrl, "LocationCriterionService"}
	managedCustomerServiceUrl       = ServiceUrl{baseUrl, "ManagedCustomerService"}
	mediaServiceUrl                 = ServiceUrl{baseUrl, "MediaService"}
	mutateJobServiceUrl             = ServiceUrl{baseUrl, "Mutate_JOB_Service"}
	offlineConversionFeedServiceUrl = ServiceUrl{baseUrl, "OfflineConversionFeedService"}
	reportDefinitionServiceUrl      = ServiceUrl{baseUrl, "ReportDefinitionService"}
	sharedCriterionServiceUrl       = ServiceUrl{baseUrl, "SharedCriterionService"}
	sharedSetServiceUrl             = ServiceUrl{baseUrl, "SharedSetService"}
	targetingIdeaServiceUrl         = ServiceUrl{baseUrl, "TargetingIdeaService"}
	trafficEstimatorServiceUrl      = ServiceUrl{baseUrl, "TrafficEstimatorService"}
)

func (s ServiceUrl) String() string {
	return s.Url + "/" + s.Name
}

type Auth struct {
	CustomerId     string
	DeveloperToken string
	UserAgent      string
	Testing        *testing.T   `json:"-"`
	Client         *http.Client `json:"-"`
}

//
// Selector structs
//
type DateRange struct {
	Min string `xml:"min"`
	Max string `xml:"max"`
}

type Predicate struct {
	Field    string   `xml:"field"`
	Operator string   `xml:"operator"`
	Values   []string `xml:"values"`
}

type OrderBy struct {
	Field     string `xml:"field"`
	SortOrder string `xml:"sortOrder"`
}

type Paging struct {
	Offset int64 `xml:"startIndex"`
	Limit  int64 `xml:"numberResults"`
}

type Selector struct {
	XMLName    xml.Name
	Fields     []string    `xml:"fields"`
	Predicates []Predicate `xml:"predicates"`
	DateRange  *DateRange  `xml:"dateRange,omitempty"`
	Ordering   []OrderBy   `xml:"ordering"`
	Paging     *Paging     `xml:"paging,omitempty"`
}

// error parsers
func selectorError() (err error) {
	return err
}

func (a *Auth) downloadReportRequest(body interface{}) (respBody []byte, err error) {
	reqBody, err := xml.MarshalIndent(body, "  ", "  ")

	if err != nil {
		return []byte{}, err
	}

	form := url.Values{}
	form.Add("__rdxml", string(reqBody))

	req, err := http.NewRequest("POST", reportUrl, bytes.NewBufferString(form.Encode()))
	req.Header.Add("User-Agent", a.UserAgent)
	req.Header.Add("clientCustomerId", a.CustomerId)
	req.Header.Add("developerToken", a.DeveloperToken)
	req.Header.Add("Accept", "*/*")
	req.Header.Add("Accept-Encoding", "gzip")
	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	contentLength := fmt.Sprintf("%d", len(reqBody))
	req.Header.Add("Content-length", contentLength)

	if a.Testing != nil {
		a.Testing.Logf("request ->\n%s\n%#v\n%s\n", req.URL.String(), req.Header, string(reqBody))
	}
	resp, err := a.Client.Do(req)
	if err != nil {
		return []byte{}, err
	}

	respBody, err = ioutil.ReadAll(resp.Body)
	if a.Testing != nil {
		a.Testing.Logf("respBody ->\n%s\n%s\n", string(respBody), resp.Status)
	}

	if resp.StatusCode == 400 || resp.StatusCode == 401 || resp.StatusCode == 403 || resp.StatusCode == 405 || resp.StatusCode == 500 {
		reportDownloadError := ReportDownloadError{}
		fmt.Printf("unknown error ->\n%s\n", string(respBody))
		err = xml.Unmarshal(respBody, &reportDownloadError)
		if err != nil {
			return respBody, fmt.Errorf("%v \nRespone:%s", err.Error(), respBody)
		}
		return respBody, reportDownloadError
	}

	return respBody, nil
}

func (a *Auth) request(serviceUrl ServiceUrl, action string, body interface{}) (respBody []byte, err error) {
	type devToken struct {
		XMLName xml.Name
	}
	type soapReqHeader struct {
		XMLName          xml.Name
		UserAgent        string `xml:"userAgent"`
		DeveloperToken   string `xml:"developerToken"`
		ClientCustomerId string `xml:"clientCustomerId"`
	}

	type soapReqBody struct {
		Body interface{}
	}

	type soapReqEnvelope struct {
		XMLName xml.Name
		Header  soapReqHeader `xml:"Header>RequestHeader"`
		Body    soapReqBody   `xml:"http://schemas.xmlsoap.org/soap/envelope/ Body"`
	}

	reqBody, err := xml.MarshalIndent(
		soapReqEnvelope{
			XMLName: xml.Name{"http://schemas.xmlsoap.org/soap/envelope/", "Envelope"},
			Header: soapReqHeader{
				XMLName:          xml.Name{serviceUrl.Url, "RequestHeader"},
				UserAgent:        a.UserAgent,
				DeveloperToken:   a.DeveloperToken,
				ClientCustomerId: a.CustomerId,
			},
			Body: soapReqBody{body},
		},
		"  ", "  ")

	if err != nil {
		return []byte{}, err
	}

	req, err := http.NewRequest("POST", serviceUrl.String(), bytes.NewReader(reqBody))
	req.Header.Add("Accept", "text/xml")
	req.Header.Add("Accept", "multipart/*")
	req.Header.Add("Content-Type", "text/xml;charset=UTF-8")
	contentLength := fmt.Sprintf("%d", len(reqBody))
	req.Header.Add("Content-length", contentLength)
	req.Header.Add("SOAPAction", action)
	if a.Testing != nil {
		a.Testing.Logf("request ->\n%s\n%#v\n%s\n", req.URL.String(), req.Header, string(reqBody))
	}
	resp, err := a.Client.Do(req)
	if err != nil {
		return []byte{}, err
	}

	respBody, err = ioutil.ReadAll(resp.Body)
	if a.Testing != nil {
		a.Testing.Logf("respBody ->\n%s\n%s\n", string(respBody), resp.Status)
	}

	type soapRespHeader struct {
		RequestId    string `xml:"requestId"`
		ServiceName  string `xml:"serviceName"`
		MethodName   string `xml:"methodName"`
		Operations   int64  `xml:"operations"`
		ResponseTime int64  `xml:"responseTime"`
	}

	type soapRespBody struct {
		Response []byte `xml:",innerxml"`
	}

	soapResp := struct {
		XMLName xml.Name       `xml:"http://schemas.xmlsoap.org/soap/envelope/ Envelope"`
		Header  soapRespHeader `xml:"Header>RequestHeader"`
		Body    soapRespBody   `xml:"http://schemas.xmlsoap.org/soap/envelope/ Body"`
	}{}

	err = xml.Unmarshal([]byte(respBody), &soapResp)
	if err != nil {
		return respBody, err
	}
	if resp.StatusCode == 400 || resp.StatusCode == 401 || resp.StatusCode == 403 || resp.StatusCode == 405 || resp.StatusCode == 500 {
		fault := Fault{}
		fmt.Printf("unknown error ->\n%s\n", string(soapResp.Body.Response))
		err = xml.Unmarshal(soapResp.Body.Response, &fault)
		if err != nil {
			return respBody, fmt.Errorf("%v \nRespone:%s", err.Error(), soapResp.Body.Response)
		}

		return soapResp.Body.Response, &fault.Errors
	}

	return soapResp.Body.Response, err
}
<commit_msg>Updated for report<commit_after>package gads

import (
	"bytes"
	"compress/gzip"
	"encoding/xml"
	"flag"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"testing"
)

const (
	baseUrl = "https://adwords.google.com/api/adwords/cm/v201509"
	//		baseUrl   = "http://127.0.0.1:4444"
	reportUrl = "https://adwords.google.com/api/adwords/reportdownload/v201509"
	//	reportUrl = "http://127.0.0.1:4444"
)

type ServiceUrl struct {
	Url  string
	Name string
}

// exceptions
var (
	ERROR_NOT_YET_IMPLEMENTED = fmt.Errorf("Not yet implemented")
)

var (
	configJson = flag.String("config_json", "./config.json", "API credentials")

	// service urls
	adGroupAdServiceUrl             = ServiceUrl{baseUrl, "AdGroupAdService"}
	adGroupBidModifierServiceUrl    = ServiceUrl{baseUrl, "AdGroupBidModifierService"}
	adGroupCriterionServiceUrl      = ServiceUrl{baseUrl, "AdGroupCriterionService"}
	adGroupFeedServiceUrl           = ServiceUrl{baseUrl, "AdGroupFeedService"}
	adGroupServiceUrl               = ServiceUrl{baseUrl, "AdGroupService"}
	adParamServiceUrl               = ServiceUrl{baseUrl, "AdParamService"}
	adwordsUserListServiceUrl       = ServiceUrl{baseUrl, "AdwordsUserListService"}
	biddingStrategyServiceUrl       = ServiceUrl{baseUrl, "BiddingStrategyService"}
	budgetOrderServiceUrl           = ServiceUrl{baseUrl, "BudgetOrderService"}
	budgetServiceUrl                = ServiceUrl{baseUrl, "BudgetService"}
	campaignAdExtensionServiceUrl   = ServiceUrl{baseUrl, "CampaignAdExtensionService"}
	campaignCriterionServiceUrl     = ServiceUrl{baseUrl, "CampaignCriterionService"}
	campaignFeedServiceUrl          = ServiceUrl{baseUrl, "CampaignFeedService"}
	campaignServiceUrl              = ServiceUrl{baseUrl, "CampaignService"}
	campaignSharedSetServiceUrl     = ServiceUrl{baseUrl, "CampaignSharedSetService"}
	constantDataServiceUrl          = ServiceUrl{baseUrl, "ConstantDataService"}
	conversionTrackerServiceUrl     = ServiceUrl{baseUrl, "ConversionTrackerService"}
	customerFeedServiceUrl          = ServiceUrl{baseUrl, "CustomerFeedService"}
	customerServiceUrl              = ServiceUrl{baseUrl, "CustomerService"}
	customerSyncServiceUrl          = ServiceUrl{baseUrl, "CustomerSyncService"}
	dataServiceUrl                  = ServiceUrl{baseUrl, "DataService"}
	experimentServiceUrl            = ServiceUrl{baseUrl, "ExperimentService"}
	feedItemServiceUrl              = ServiceUrl{baseUrl, "FeedItemService"}
	feedMappingServiceUrl           = ServiceUrl{baseUrl, "FeedMappingService"}
	feedServiceUrl                  = ServiceUrl{baseUrl, "FeedService"}
	geoLocationServiceUrl           = ServiceUrl{baseUrl, "GeoLocationService"}
	labelServiceUrl                 = ServiceUrl{baseUrl, "LabelService"}
	locationCriterionServiceUrl     = ServiceUrl{baseUrl, "LocationCriterionService"}
	managedCustomerServiceUrl       = ServiceUrl{baseUrl, "ManagedCustomerService"}
	mediaServiceUrl                 = ServiceUrl{baseUrl, "MediaService"}
	mutateJobServiceUrl             = ServiceUrl{baseUrl, "Mutate_JOB_Service"}
	offlineConversionFeedServiceUrl = ServiceUrl{baseUrl, "OfflineConversionFeedService"}
	reportDefinitionServiceUrl      = ServiceUrl{baseUrl, "ReportDefinitionService"}
	sharedCriterionServiceUrl       = ServiceUrl{baseUrl, "SharedCriterionService"}
	sharedSetServiceUrl             = ServiceUrl{baseUrl, "SharedSetService"}
	targetingIdeaServiceUrl         = ServiceUrl{baseUrl, "TargetingIdeaService"}
	trafficEstimatorServiceUrl      = ServiceUrl{baseUrl, "TrafficEstimatorService"}
)

func (s ServiceUrl) String() string {
	return s.Url + "/" + s.Name
}

type Auth struct {
	CustomerId     string
	DeveloperToken string
	UserAgent      string
	Testing        *testing.T   `json:"-"`
	Client         *http.Client `json:"-"`
}

//
// Selector structs
//
type DateRange struct {
	Min string `xml:"min"`
	Max string `xml:"max"`
}

type Predicate struct {
	Field    string   `xml:"field"`
	Operator string   `xml:"operator"`
	Values   []string `xml:"values"`
}

type OrderBy struct {
	Field     string `xml:"field"`
	SortOrder string `xml:"sortOrder"`
}

type Paging struct {
	Offset int64 `xml:"startIndex"`
	Limit  int64 `xml:"numberResults"`
}

type Selector struct {
	XMLName    xml.Name
	Fields     []string    `xml:"fields"`
	Predicates []Predicate `xml:"predicates"`
	DateRange  *DateRange  `xml:"dateRange,omitempty"`
	Ordering   []OrderBy   `xml:"ordering"`
	Paging     *Paging     `xml:"paging,omitempty"`
}

// error parsers
func selectorError() (err error) {
	return err
}

func (a *Auth) downloadReportRequest(body interface{}) (respBody []byte, err error) {
	reqBody, err := xml.MarshalIndent(body, "  ", "  ")
	if err != nil {
		return []byte{}, err
	}

	form := url.Values{}
	form.Add("__rdxml", string(reqBody))

	req, err := http.NewRequest("POST", reportUrl, bytes.NewBufferString(form.Encode()))
	req.Header.Add("User-Agent", a.UserAgent)
	req.Header.Add("clientCustomerId", a.CustomerId)
	req.Header.Add("developerToken", a.DeveloperToken)
	req.Header.Add("Accept", "*/*")
	req.Header.Add("Accept-Encoding", "gzip")
	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	contentLength := fmt.Sprintf("%d", len(reqBody))
	req.Header.Add("Content-length", contentLength)

	if a.Testing != nil {
		a.Testing.Logf("request ->\n%s\n%#v\n%s\n", req.URL.String(), req.Header, string(reqBody))
	}

	resp, err := a.Client.Do(req)
	if err != nil {
		return []byte{}, err
	}

	reader, err := gzip.NewReader(resp.Body)
	if err != nil {
		return []byte{}, err
	}
	defer reader.Close()

	respBody, err = ioutil.ReadAll(reader)
	if a.Testing != nil {
		a.Testing.Logf("respBody ->\n%s\n%s\n", string(respBody), resp.Status)
	}

	if resp.StatusCode == 400 || resp.StatusCode == 401 || resp.StatusCode == 403 || resp.StatusCode == 405 || resp.StatusCode == 500 {
		reportDownloadError := ReportDownloadError{}
		fmt.Printf("unknown error ->\n%s\n", string(respBody))
		err = xml.Unmarshal(respBody, &reportDownloadError)
		if err != nil {
			return respBody, fmt.Errorf("%v \nRespone:%s", err.Error(), respBody)
		}
		return respBody, reportDownloadError
	}

	return respBody, nil
}

func (a *Auth) request(serviceUrl ServiceUrl, action string, body interface{}) (respBody []byte, err error) {
	type devToken struct {
		XMLName xml.Name
	}
	type soapReqHeader struct {
		XMLName          xml.Name
		UserAgent        string `xml:"userAgent"`
		DeveloperToken   string `xml:"developerToken"`
		ClientCustomerId string `xml:"clientCustomerId"`
	}

	type soapReqBody struct {
		Body interface{}
	}

	type soapReqEnvelope struct {
		XMLName xml.Name
		Header  soapReqHeader `xml:"Header>RequestHeader"`
		Body    soapReqBody   `xml:"http://schemas.xmlsoap.org/soap/envelope/ Body"`
	}

	reqBody, err := xml.MarshalIndent(
		soapReqEnvelope{
			XMLName: xml.Name{"http://schemas.xmlsoap.org/soap/envelope/", "Envelope"},
			Header: soapReqHeader{
				XMLName:          xml.Name{serviceUrl.Url, "RequestHeader"},
				UserAgent:        a.UserAgent,
				DeveloperToken:   a.DeveloperToken,
				ClientCustomerId: a.CustomerId,
			},
			Body: soapReqBody{body},
		},
		"  ", "  ")

	if err != nil {
		return []byte{}, err
	}

	req, err := http.NewRequest("POST", serviceUrl.String(), bytes.NewReader(reqBody))
	req.Header.Add("Accept", "text/xml")
	req.Header.Add("Accept", "multipart/*")
	req.Header.Add("Content-Type", "text/xml;charset=UTF-8")
	contentLength := fmt.Sprintf("%d", len(reqBody))
	req.Header.Add("Content-length", contentLength)
	req.Header.Add("SOAPAction", action)
	if a.Testing != nil {
		a.Testing.Logf("request ->\n%s\n%#v\n%s\n", req.URL.String(), req.Header, string(reqBody))
	}
	resp, err := a.Client.Do(req)
	if err != nil {
		return []byte{}, err
	}

	respBody, err = ioutil.ReadAll(resp.Body)
	if a.Testing != nil {
		a.Testing.Logf("respBody ->\n%s\n%s\n", string(respBody), resp.Status)
	}

	type soapRespHeader struct {
		RequestId    string `xml:"requestId"`
		ServiceName  string `xml:"serviceName"`
		MethodName   string `xml:"methodName"`
		Operations   int64  `xml:"operations"`
		ResponseTime int64  `xml:"responseTime"`
	}

	type soapRespBody struct {
		Response []byte `xml:",innerxml"`
	}

	soapResp := struct {
		XMLName xml.Name       `xml:"http://schemas.xmlsoap.org/soap/envelope/ Envelope"`
		Header  soapRespHeader `xml:"Header>RequestHeader"`
		Body    soapRespBody   `xml:"http://schemas.xmlsoap.org/soap/envelope/ Body"`
	}{}

	err = xml.Unmarshal([]byte(respBody), &soapResp)
	if err != nil {
		return respBody, err
	}
	if resp.StatusCode == 400 || resp.StatusCode == 401 || resp.StatusCode == 403 || resp.StatusCode == 405 || resp.StatusCode == 500 {
		fault := Fault{}
		fmt.Printf("unknown error ->\n%s\n", string(soapResp.Body.Response))
		err = xml.Unmarshal(soapResp.Body.Response, &fault)
		if err != nil {
			return respBody, fmt.Errorf("%v \nRespone:%s", err.Error(), soapResp.Body.Response)
		}

		return soapResp.Body.Response, &fault.Errors
	}

	return soapResp.Body.Response, err
}
<|endoftext|>


<commit_before>package rawhandler

import (
	"net/http"

	"github.com/mxmCherry/httpfsx/internal/filesystem"
)

type Handler struct {
	fs *filesystem.FS
}

func New(fs *filesystem.FS) *Handler {
	return &Handler{
		fs: fs,
	}
}

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	http.ServeFile(w, r, h.fs.Abs(r.URL.Path))
}
<commit_msg>Remove unused rawhandler<commit_after><|endoftext|>


<commit_before>// Copyright 2018 The OPA Authors.  All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.

package module

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

// PrettyOption defines options for controlling pretty printing.
type PrettyOption struct {
	Contents bool // show raw byte content of data+code sections.
}

// Pretty writes a human-readable representation of m to w.
func Pretty(w io.Writer, m *Module, opts ...PrettyOption) {
	fmt.Println("version:", m.Version)
	fmt.Println("types:")
	for _, fn := range m.Type.Functions {
		fmt.Println("  -", fn)
	}
	fmt.Println("imports:")
	for i, imp := range m.Import.Imports {
		if imp.Descriptor.Kind() == FunctionImportType {
			fmt.Printf("  - [%d] %v\n", i, imp)
		} else {
			fmt.Println("  -", imp)
		}
	}
	fmt.Println("functions:")
	for _, fn := range m.Function.TypeIndices {
		if fn >= uint32(len(m.Type.Functions)) {
			fmt.Println("  -", "???")
		} else {
			fmt.Println("  -", m.Type.Functions[fn])
		}
	}
	fmt.Println("exports:")
	for _, exp := range m.Export.Exports {
		fmt.Println("  -", exp)
	}
	fmt.Println("code:")
	for _, seg := range m.Code.Segments {
		fmt.Println("  -", seg)
	}
	fmt.Println("data:")
	for _, seg := range m.Data.Segments {
		fmt.Println("  -", seg)
	}
	if len(opts) == 0 {
		return
	}
	fmt.Println()
	for _, opt := range opts {
		if opt.Contents {
			newline := false
			if len(m.Data.Segments) > 0 {
				fmt.Println("data section:")
				for _, seg := range m.Data.Segments {
					if newline {
						fmt.Println()
					}
					fmt.Println(hex.Dump(seg.Init))
					newline = true
				}
				newline = false
			}
			if len(m.Code.Segments) > 0 {
				fmt.Println("code section:")
				for _, seg := range m.Code.Segments {
					if newline {
						fmt.Println()
					}
					fmt.Println(hex.Dump(seg.Code))
					newline = true
				}
				newline = false
			}
		}
	}
}
<commit_msg>wasm: Fix pretty printer writer usage<commit_after>// Copyright 2018 The OPA Authors.  All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.

package module

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

// PrettyOption defines options for controlling pretty printing.
type PrettyOption struct {
	Contents bool // show raw byte content of data+code sections.
}

// Pretty writes a human-readable representation of m to w.
func Pretty(w io.Writer, m *Module, opts ...PrettyOption) {
	fmt.Fprintln(w, "version:", m.Version)
	fmt.Fprintln(w, "types:")
	for _, fn := range m.Type.Functions {
		fmt.Fprintln(w, "  -", fn)
	}
	fmt.Fprintln(w, "imports:")
	for i, imp := range m.Import.Imports {
		if imp.Descriptor.Kind() == FunctionImportType {
			fmt.Printf("  - [%d] %v\n", i, imp)
		} else {
			fmt.Fprintln(w, "  -", imp)
		}
	}
	fmt.Fprintln(w, "functions:")
	for _, fn := range m.Function.TypeIndices {
		if fn >= uint32(len(m.Type.Functions)) {
			fmt.Fprintln(w, "  -", "???")
		} else {
			fmt.Fprintln(w, "  -", m.Type.Functions[fn])
		}
	}
	fmt.Fprintln(w, "exports:")
	for _, exp := range m.Export.Exports {
		fmt.Fprintln(w, "  -", exp)
	}
	fmt.Fprintln(w, "code:")
	for _, seg := range m.Code.Segments {
		fmt.Fprintln(w, "  -", seg)
	}
	fmt.Fprintln(w, "data:")
	for _, seg := range m.Data.Segments {
		fmt.Fprintln(w, "  -", seg)
	}
	if len(opts) == 0 {
		return
	}
	fmt.Fprintln(w)
	for _, opt := range opts {
		if opt.Contents {
			newline := false
			if len(m.Data.Segments) > 0 {
				fmt.Fprintln(w, "data section:")
				for _, seg := range m.Data.Segments {
					if newline {
						fmt.Fprintln(w)
					}
					fmt.Fprintln(w, hex.Dump(seg.Init))
					newline = true
				}
				newline = false
			}
			if len(m.Code.Segments) > 0 {
				fmt.Fprintln(w, "code section:")
				for _, seg := range m.Code.Segments {
					if newline {
						fmt.Fprintln(w)
					}
					fmt.Fprintln(w, hex.Dump(seg.Code))
					newline = true
				}
				newline = false
			}
		}
	}
}
<|endoftext|>


<commit_before>// Copyright © 2015 Steve Francia <spf@spf13.com>.
//
// 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 (
	"fmt"
	"os"

	"github.com/spf13/cobra"
	"github.com/spf13/viper"
)

func init() {
	RootCmd.AddCommand(initCmd)
}

// initialize Command
var initCmd = &cobra.Command{
	Use:     "init [name]",
	Aliases: []string{"initialize", "initalise", "create"},
	Short:   "Initalize a Cobra Application",
	Long: `Initialize (cobra init) will create a new application, with a license
and the appropriate structure for a Cobra-based CLI application.

  * If a name is provided, it will be created in the current directory;
  * If no name is provided, the current directory will be assumed;
  * If a relative path is provided, it will be created inside $GOPATH
    (e.g. github.com/spf13/hugo);
  * If an absolute path is provided, it will be created;
  * If the directory already exists but is empty, it will be used.

Init will not use an exiting directory with contents.`,

	Run: func(cmd *cobra.Command, args []string) {
		switch len(args) {
		case 0:
			inputPath = ""

		case 1:
			inputPath = args[0]

		default:
			er("init doesn't support more than 1 parameter")
		}
		guessProjectPath()
		initalizePath(projectPath)
	},
}

func initalizePath(path string) {
	b, err := exists(path)
	if err != nil {
		er(err)
	}

	if !b { // If path doesn't yet exist, create it
		err := os.MkdirAll(path, os.ModePerm)
		if err != nil {
			er(err)
		}
	} else { // If path exists and is not empty don't use it
		empty, err := exists(path)
		if err != nil {
			er(err)
		}
		if !empty {
			er("Cobra will not create a new project in a non empty directory")
		}
	}
	// We have a directory and it's empty.. Time to initialize it.

	createLicenseFile()
	createMainFile()
	createRootCmdFile()
}

func createLicenseFile() {
	lic := getLicense()
	err := writeStringToFile(ProjectPath(), "LICENSE", lic.Text)
	_ = err
	// if err != nil {
	// 	er(err)
	// }
}

func createMainFile() {
	lic := getLicense()

	template := `{{ comment .copyright }}
{{ comment .license }}

package main

import "{{ .importpath }}"

func main() {
	cmd.Execute()
}
`
	var data map[string]interface{}
	data = make(map[string]interface{})

	data["copyright"] = copyrightLine()
	data["license"] = lic.Header
	data["importpath"] = guessImportPath() + "/" + guessCmdDir()

	err := writeTemplateToFile(ProjectPath(), "main.go", template, data)
	_ = err
	// if err != nil {
	// 	er(err)
	// }
}

func createRootCmdFile() {
	lic := getLicense()

	template := `{{ comment .copyright }}
{{ comment .license }}

package cmd

import (
	"fmt"
	"os"

	"github.com/spf13/cobra"
{{ if .viper }}	"github.com/spf13/viper"
{{ end }})
{{if .viper}}
var cfgFile string
{{ end }}
// This represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
	Use:   "{{ .appName }}",
	Short: "A brief description of your application",
	Long: ` + "`" + `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:

Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.` + "`" + `,
// Uncomment the following line if your bare application
// has an action associated with it:
//	Run: func(cmd *cobra.Command, args []string) { },
}

// Execute adds all child commands to the root command sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
	if err := RootCmd.Execute(); err != nil {
		fmt.Println(err)
		os.Exit(-1)
	}
}

func init() {
{{ if .viper }}	cobra.OnInitialize(initConfig)

{{ end }}	// Here you will define your flags and configuration settings.
	// Cobra supports Persistent Flags, which, if defined here,
	// will be global for your application.
{{ if .viper }}
	RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.{{ .appName }}.yaml)")
{{ else }}
	// RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.{{ .appName }}.yaml)")
{{ end }}	// Cobra also supports local flags, which will only run
	// when this action is called directly.
	RootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
{{ if .viper }}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
	if cfgFile != "" { // enable ability to specify config file via flag
		viper.SetConfigFile(cfgFile)
	}

	viper.SetConfigName(".{{ .appName }}") // name of config file (without extension)
	viper.AddConfigPath("$HOME")  // adding home directory as first search path
	viper.AutomaticEnv()          // read in environment variables that match

	// If a config file is found, read it in.
	if err := viper.ReadInConfig(); err == nil {
		fmt.Println("Using config file:", viper.ConfigFileUsed())
	}
}
{{ end }}`

	var data map[string]interface{}
	data = make(map[string]interface{})

	data["copyright"] = copyrightLine()
	data["license"] = lic.Header
	data["appName"] = projectName()
	data["viper"] = viper.GetBool("useViper")

	err := writeTemplateToFile(ProjectPath()+string(os.PathSeparator)+guessCmdDir(), "root.go", template, data)
	if err != nil {
		er(err)
	}

	fmt.Println("Your Cobra application is ready at")
	fmt.Println(ProjectPath())
	fmt.Println("Give it a try by going there and running `go run main.go`")
	fmt.Println("Add commands to it by running `cobra add [cmdname]`")
}
<commit_msg>Parse the full license text as a template<commit_after>// Copyright © 2015 Steve Francia <spf@spf13.com>.
//
// 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 (
	"fmt"
	"os"
	"strings"

	"github.com/spf13/cobra"
	"github.com/spf13/viper"
)

func init() {
	RootCmd.AddCommand(initCmd)
}

// initialize Command
var initCmd = &cobra.Command{
	Use:     "init [name]",
	Aliases: []string{"initialize", "initalise", "create"},
	Short:   "Initalize a Cobra Application",
	Long: `Initialize (cobra init) will create a new application, with a license
and the appropriate structure for a Cobra-based CLI application.

  * If a name is provided, it will be created in the current directory;
  * If no name is provided, the current directory will be assumed;
  * If a relative path is provided, it will be created inside $GOPATH
    (e.g. github.com/spf13/hugo);
  * If an absolute path is provided, it will be created;
  * If the directory already exists but is empty, it will be used.

Init will not use an exiting directory with contents.`,

	Run: func(cmd *cobra.Command, args []string) {
		switch len(args) {
		case 0:
			inputPath = ""

		case 1:
			inputPath = args[0]

		default:
			er("init doesn't support more than 1 parameter")
		}
		guessProjectPath()
		initalizePath(projectPath)
	},
}

func initalizePath(path string) {
	b, err := exists(path)
	if err != nil {
		er(err)
	}

	if !b { // If path doesn't yet exist, create it
		err := os.MkdirAll(path, os.ModePerm)
		if err != nil {
			er(err)
		}
	} else { // If path exists and is not empty don't use it
		empty, err := exists(path)
		if err != nil {
			er(err)
		}
		if !empty {
			er("Cobra will not create a new project in a non empty directory")
		}
	}
	// We have a directory and it's empty.. Time to initialize it.

	createLicenseFile()
	createMainFile()
	createRootCmdFile()
}

func createLicenseFile() {
	lic := getLicense()

	template := lic.Text

	var data map[string]interface{}
	data = make(map[string]interface{})

	// Try to remove the email address, if any
	data["copyright"] = strings.Split(copyrightLine(), " <")[0]

	err := writeTemplateToFile(ProjectPath(), "LICENSE", template, data)
	_ = err
	// if err != nil {
	// 	er(err)
	// }
}

func createMainFile() {
	lic := getLicense()

	template := `{{ comment .copyright }}
{{ comment .license }}

package main

import "{{ .importpath }}"

func main() {
	cmd.Execute()
}
`
	var data map[string]interface{}
	data = make(map[string]interface{})

	data["copyright"] = copyrightLine()
	data["license"] = lic.Header
	data["importpath"] = guessImportPath() + "/" + guessCmdDir()

	err := writeTemplateToFile(ProjectPath(), "main.go", template, data)
	_ = err
	// if err != nil {
	// 	er(err)
	// }
}

func createRootCmdFile() {
	lic := getLicense()

	template := `{{ comment .copyright }}
{{ comment .license }}

package cmd

import (
	"fmt"
	"os"

	"github.com/spf13/cobra"
{{ if .viper }}	"github.com/spf13/viper"
{{ end }})
{{if .viper}}
var cfgFile string
{{ end }}
// This represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
	Use:   "{{ .appName }}",
	Short: "A brief description of your application",
	Long: ` + "`" + `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:

Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.` + "`" + `,
// Uncomment the following line if your bare application
// has an action associated with it:
//	Run: func(cmd *cobra.Command, args []string) { },
}

// Execute adds all child commands to the root command sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
	if err := RootCmd.Execute(); err != nil {
		fmt.Println(err)
		os.Exit(-1)
	}
}

func init() {
{{ if .viper }}	cobra.OnInitialize(initConfig)

{{ end }}	// Here you will define your flags and configuration settings.
	// Cobra supports Persistent Flags, which, if defined here,
	// will be global for your application.
{{ if .viper }}
	RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.{{ .appName }}.yaml)")
{{ else }}
	// RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.{{ .appName }}.yaml)")
{{ end }}	// Cobra also supports local flags, which will only run
	// when this action is called directly.
	RootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
{{ if .viper }}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
	if cfgFile != "" { // enable ability to specify config file via flag
		viper.SetConfigFile(cfgFile)
	}

	viper.SetConfigName(".{{ .appName }}") // name of config file (without extension)
	viper.AddConfigPath("$HOME")  // adding home directory as first search path
	viper.AutomaticEnv()          // read in environment variables that match

	// If a config file is found, read it in.
	if err := viper.ReadInConfig(); err == nil {
		fmt.Println("Using config file:", viper.ConfigFileUsed())
	}
}
{{ end }}`

	var data map[string]interface{}
	data = make(map[string]interface{})

	data["copyright"] = copyrightLine()
	data["license"] = lic.Header
	data["appName"] = projectName()
	data["viper"] = viper.GetBool("useViper")

	err := writeTemplateToFile(ProjectPath()+string(os.PathSeparator)+guessCmdDir(), "root.go", template, data)
	if err != nil {
		er(err)
	}

	fmt.Println("Your Cobra application is ready at")
	fmt.Println(ProjectPath())
	fmt.Println("Give it a try by going there and running `go run main.go`")
	fmt.Println("Add commands to it by running `cobra add [cmdname]`")
}
<|endoftext|>


<commit_before>package web

import (
	"encoding/json"
	"fmt"
	"github.com/gorilla/mux"
	"github.com/tgermain/grandRepositorySky/dataSet"
	"github.com/tgermain/grandRepositorySky/node"
	"github.com/tgermain/grandRepositorySky/shared"
	"net/http"

	//"time" // to set a timer
)

//Objects parts ---------------------------------------------------------

type NodeJson struct {
	Id          string
	Ip          string
	Port        string
	Successor   *shared.DistantNode
	Predecessor *shared.DistantNode
	Fingers     []*node.FingerEntry
	Datas       dataSet.DataSet
}

type MyServer struct {
	r *mux.Router
}

type DataReq struct {
	Key   string
	Value string
}

var node1 *node.DHTnode
var maxSizePost int64

//Method parts ----------------------------------------------------------

// Hello Handler
func HelloHandler(w http.ResponseWriter, req *http.Request) {
	shared.Logger.Notice("GET /")
	fmt.Fprintf(w, "Hello World too")
}

//TODO refactor handler url (not really a get nodes)
//TODO send datas informations too
func NodesHandler(w http.ResponseWriter, req *http.Request) {
	shared.Logger.Notice("GET /noeuds")

	node1Json := NodeJson{shared.LocalId, shared.LocalIp, shared.LocalPort, node1.GetSuccesor(), node1.GetPredecessor(), node1.GetFingerTable(), shared.Datas}
	js, err := json.Marshal(node1Json)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	w.Write(js)
}

func DataPostHandler(w http.ResponseWriter, req *http.Request) {

	decoder := json.NewDecoder(req.Body)
	var t DataReq
	err := decoder.Decode(&t)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	shared.Logger.Notice("POST new datakey %s value %s", t.Key, t.Value)

	node1.SetData(t.Key, t.Value)
	fmt.Fprintf(w, "ok")
}

func DataPutHandler(w http.ResponseWriter, req *http.Request) {

	decoder := json.NewDecoder(req.Body)
	var t DataReq
	err := decoder.Decode(&t)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	shared.Logger.Notice("PUT data key %s value %s", t.Key, t.Value)

	node1.ModifyData( /* t.Key, t.Value */)

	fmt.Fprintf(w, "ok")
}

func DataDeleteHandler(w http.ResponseWriter, req *http.Request) {
	//node1.DeleteData(req.URL.Path[9:])
	shared.Logger.Notice("DELETE data %s", key)
	fmt.Fprintf(w, "ok")
}

func DataGetHandler(w http.ResponseWriter, req *http.Request) {
	node1.GetData(req.URL.Path[9:])
	fmt.Fprintf(w, "ok")
	shared.Logger.Notice("GET data %s", key)
}

//TODO? launch lookup request

//TODO post data

//TODO update data

//TODO remove data

//TODO:POSTPONE disconnect a node

//TODO:POSTPONE manual areYouAlive request

//wrap server handler function to activate CORS
func (s *MyServer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
	//if origin := req.Header.Get("Origin"); origin != "" {
	rw.Header().Set("Content-Type", "application/json, text/html")
	rw.Header().Set("Accept", "application/json")
	rw.Header().Set("Accept-Charset", "utf-8")
	rw.Header().Set("Access-Control-Allow-Credentials", "true")
	rw.Header().Set("Access-Control-Allow-Origin", "*")
	rw.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
	rw.Header().Set("Access-Control-Allow-Headers",
		"Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
	//}
	// Stop here if its Preflighted OPTIONS request
	if req.Method == "OPTIONS" {
		return
	}
	//the real library handler
	s.r.ServeHTTP(rw, req)
}

//create a functionnal server
func MakeServer(ip string, port string, nod *node.DHTnode) {
	receive := ip + ":" + port
	node1 = nod
	shared.Logger.Notice("server listen on : %s\n", receive)
	shared.Logger.Warning("datas : %s\n", shared.Datas)

	r := mux.NewRouter()
	// r.HandleFunc("/", HelloHandler)
	//serv staticly index.html
	fs := http.FileServer(http.Dir("web/client"))
	r.Handle("/", fs)

	r.HandleFunc("/nodes", NodesHandler)
	r.HandleFunc("/storage", DataPostHandler).Methods("POST")
	r.HandleFunc("/storage/{key}", DataPutHandler).Methods("PUT")
	r.HandleFunc("/storage/{key}", DataDeleteHandler).Methods("DELETE")
	r.HandleFunc("/storage/{key}", DataGetHandler).Methods("GET")
	http.Handle("/", &MyServer{r})

	http.ListenAndServe(receive, nil)
}
<commit_msg>get the url parameter the right way<commit_after>package web

import (
	"encoding/json"
	"fmt"
	"github.com/gorilla/mux"
	"github.com/tgermain/grandRepositorySky/dataSet"
	"github.com/tgermain/grandRepositorySky/node"
	"github.com/tgermain/grandRepositorySky/shared"
	"net/http"

	//"time" // to set a timer
)

//Objects parts ---------------------------------------------------------

type NodeJson struct {
	Id          string
	Ip          string
	Port        string
	Successor   *shared.DistantNode
	Predecessor *shared.DistantNode
	Fingers     []*node.FingerEntry
	Datas       dataSet.DataSet
}

type MyServer struct {
	r *mux.Router
}

type DataReq struct {
	Key   string
	Value string
}

var node1 *node.DHTnode
var maxSizePost int64

//Method parts ----------------------------------------------------------

// Hello Handler
func HelloHandler(w http.ResponseWriter, req *http.Request) {
	shared.Logger.Notice("GET /")
	fmt.Fprintf(w, "Hello World too")
}

//TODO refactor handler url (not really a get nodes)
//TODO send datas informations too
func NodesHandler(w http.ResponseWriter, req *http.Request) {
	shared.Logger.Notice("GET /noeuds")

	node1Json := NodeJson{shared.LocalId, shared.LocalIp, shared.LocalPort, node1.GetSuccesor(), node1.GetPredecessor(), node1.GetFingerTable(), shared.Datas}
	js, err := json.Marshal(node1Json)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	w.Write(js)
}

func DataPostHandler(w http.ResponseWriter, req *http.Request) {

	decoder := json.NewDecoder(req.Body)
	var t DataReq
	err := decoder.Decode(&t)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	shared.Logger.Notice("POST new datakey %s value %s", t.Key, t.Value)

	node1.SetData(t.Key, t.Value)
	fmt.Fprintf(w, "ok")
}

func DataPutHandler(w http.ResponseWriter, req *http.Request) {

	decoder := json.NewDecoder(req.Body)
	var t DataReq
	err := decoder.Decode(&t)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	shared.Logger.Notice("PUT data key %s value %s", t.Key, t.Value)

	node1.ModifyData( /* t.Key, t.Value */)

	fmt.Fprintf(w, "ok")
}

func DataDeleteHandler(w http.ResponseWriter, req *http.Request) {
	//get the url param
	params := mux.Vars(req)
	key := params["key"]

	shared.Logger.Notice("DELETE data %s", key)
	fmt.Fprintf(w, "ok")
}

func DataGetHandler(w http.ResponseWriter, req *http.Request) {
	//get the url param
	params := mux.Vars(req)
	key := params["key"]
	shared.Logger.Notice("GET data %s", key)
}

//TODO? launch lookup request

//TODO post data

//TODO update data

//TODO remove data

//TODO:POSTPONE disconnect a node

//TODO:POSTPONE manual areYouAlive request

//wrap server handler function to activate CORS
func (s *MyServer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
	//if origin := req.Header.Get("Origin"); origin != "" {
	rw.Header().Set("Content-Type", "application/json, text/html")
	rw.Header().Set("Accept", "application/json")
	rw.Header().Set("Accept-Charset", "utf-8")
	rw.Header().Set("Access-Control-Allow-Credentials", "true")
	rw.Header().Set("Access-Control-Allow-Origin", "*")
	rw.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
	rw.Header().Set("Access-Control-Allow-Headers",
		"Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
	//}
	// Stop here if its Preflighted OPTIONS request
	if req.Method == "OPTIONS" {
		return
	}
	//the real library handler
	s.r.ServeHTTP(rw, req)
}

//create a functionnal server
func MakeServer(ip string, port string, nod *node.DHTnode) {
	receive := ip + ":" + port
	node1 = nod
	shared.Logger.Notice("server listen on : %s\n", receive)
	shared.Logger.Warning("datas : %s\n", shared.Datas)

	r := mux.NewRouter()
	// r.HandleFunc("/", HelloHandler)
	//serv staticly index.html
	fs := http.FileServer(http.Dir("web/client"))
	r.Handle("/", fs)

	r.HandleFunc("/nodes", NodesHandler)
	r.HandleFunc("/storage", DataPostHandler).Methods("POST")
	r.HandleFunc("/storage/{key}", DataPutHandler).Methods("PUT")
	r.HandleFunc("/storage/{key}", DataDeleteHandler).Methods("DELETE")
	r.HandleFunc("/storage/{key}", DataGetHandler).Methods("GET")
	http.Handle("/", &MyServer{r})

	http.ListenAndServe(receive, nil)
}
<|endoftext|>


<commit_before>package tensor3

type Vectors []Vector

func NewVectors(cs ...BaseType) (vs Vectors) {
	vs = make(Vectors, (len(cs)+2)/3)
	for i := range vs {
		vs[i] = NewVector(cs[i*3:]...)
	}
	return
}

func (vs Vectors) Cross(v Vector) {
	vs.ForEach((*Vector).Cross, v)
}

func (vs Vectors) Add(v Vector) {
	vs.ForEach((*Vector).Add, v)
}

func (vs Vectors) Subtract(v Vector) {
	vs.ForEach((*Vector).Subtract, v)
}

func (vs Vectors) Project(v Vector) {
	vs.ForEach((*Vector).Project, v)
}

func (vs Vectors) Sum() (v Vector) {
	v.ForAll(vs, (*Vector).Add)
	return
}

func (vs Vectors) Multiply(s BaseType) {
	var multiply func(*Vector)
	multiply = func(v *Vector) {
		v.Multiply(s)
	}
	vs.ForEachNoParameter(multiply)
}

func (vs Vectors) Max() (v Vector) {
	v.Set(vs[0])
	v.ForAll(vs[1:], (*Vector).Max)
	return
}

func (vs Vectors) Min() (v Vector) {
	v.Set(vs[0])
	v.ForAll(vs[1:], (*Vector).Min)
	return
}

func (vs Vectors) Middle() (v Vector) {
	v = vs.Max()
	v.Mid(vs.Min())
	return
}

func (vs Vectors) Interpolate(v Vector, f float64) {
	f1 := Base64(1 - f)
	f2 := Base64(f)
	var interpolate func(*Vector, Vector)
	interpolate = func(v *Vector, v2 Vector) {
		v2.Multiply(f1)
		v.Multiply(f2)
		v.Add(v2)
	}
	vs.ForEach(interpolate, v)
}

func (vs Vectors) ForEach(fn func(*Vector, Vector), v Vector) {
	if !Parallel {
		vectorsApply(vs, fn, v)
	} else {
		vectorsApplyChunked(vs, fn, v)
	}
}

func vectorsApply(vs Vectors, fn func(*Vector, Vector), v Vector) {
	for i := range vs {
		fn(&vs[i], v)
	}
}

func vectorsApplyChunked(vs Vectors, fn func(*Vector, Vector), v Vector) {
	done := make(chan struct{}, 1)
	var running uint
	for chunk := range vectorsInChunks(vs, chunkSize(len(vs))) {
		running++
		go func(c Vectors) {
			vectorsApply(c, fn, v)
			done <- struct{}{}
		}(chunk)
	}
	for ; running > 0; running-- {
		<-done
	}
}

// for each vector apply a function with no parameters
func (vs Vectors) ForEachNoParameter(fn func(*Vector)) {
	var inner func(*Vector, Vector)
	inner = func(v *Vector, _ Vector) {
		fn(v)
	}
	vs.ForEach(inner, Vector{})
}

func (vs Vectors) CrossAll(vs2 Vectors) {
	vs.ForAll((*Vector).Cross, vs2)
}

func (vs Vectors) AddAll(vs2 Vectors) {
	vs.ForAll((*Vector).Add, vs2)
}

func (vs Vectors) SubtractAll(vs2 Vectors) {
	vs.ForAll((*Vector).Subtract, vs2)
}

func (vs Vectors) ProjectAll(vs2 Vectors) {
	vs.ForAll((*Vector).Project, vs2)
}

func (vs Vectors) ForAll(fn func(*Vector, Vector), vs2 Vectors) {
	if !Parallel {
		vectorsApplyAll(vs, fn, vs2)
	} else {
		vectorsApplyAllChunked(vs, fn, vs2)
	}
}

func vectorsApplyAll(vs Vectors, fn func(*Vector, Vector), vs2 Vectors) {
	for i := range vs {
		fn(&vs[i], vs2[i])
	}
}

func vectorsApplyAllChunked(vs Vectors, fn func(*Vector, Vector), vs2 Vectors) {
	done := make(chan struct{}, 1)
	var running uint
	// shorten vs to use only what we have in vs2
	if len(vs) > len(vs2) {
		vs = vs[:len(vs2)]
	}
	cs := chunkSize(len(vs))
	chunks2 := vectorsInChunks(vs2, cs)
	for chunk := range vectorsInChunks(vs, cs) {
		running++
		go func(c Vectors) {
			vectorsApplyAll(c, fn, <-chunks2)
			done <- struct{}{}
		}(chunk)
	}
	for ; running > 0; running-- {
		<-done
	}
}

// search Vectors for the two Vector's that return the minimum value from the provided function
func (vs Vectors) SearchMin(toMin func(Vector, Vector) BaseType) (i, j int, value BaseType) {
	// TODO search in chunks
	value = toMin(vs[0], vs[1])
	var v1, v2 Vector
	var il, jl int = 0, 1
	for jl, v2 = range vs[2:] {
		nl := toMin(vs[0], v2)
		if nl < value {
			value, j = nl, jl+2
		}
	}

	for il, v1 = range vs[1:] {
		for jl, v2 = range vs[il+2:] {
			nl := toMin(v1, v2)
			if nl < value {
				value, i, j = nl, il+1, jl+il+2
			}
		}
	}
	return
}

func (vs Vectors) SearchMinregionally(toMin func(Vector, Vector) BaseType) (i, j int, value BaseType) {
	return vs.SearchMin(toMin)
}
/*
func (b *BaseType) Aggregate(vs Vectors,length,stride int, fn func(*BaseType, Vectors)) {
	for vsc := range vectorSlicesInChunks(vs,chunkSize(len(vs)),length,stride,false) {
		for _,vs:=range(vsc){
			fn(b, vs)
		}
	}
}
*/

// for each vector apply a function with no parameters
func (vs Vectors) ForEachInSlices(length,stride int,wrap bool,fn func(Vectors)) {
	if !Parallel {
		var i int
		for ;i<len(vs)-length+1;i+=stride{
			fn(vs[i:i+length])
		}
		if wrap{
			joinSlice:=make(Vectors,length,length)
			for ;i<len(vs);i+=stride{
				copy(joinSlice,vs[i:])
				copy(joinSlice[len(vs)-i:],vs)
				fn(joinSlice)
			}
		}
	} else {
		vectorsInSlicesApplyChunked(vs,length,stride,wrap, fn)
	}
}

func vectorsInSlicesApply(vss []Vectors,fn func(Vectors)) {
	for _,vs := range vss {
		fn(vs)
	}
}

func vectorsInSlicesApplyChunked(vs Vectors,length,stride int,wrap bool, fn func(Vectors)) {
	done := make(chan struct{}, 1)
	var running uint
	for chunk := range vectorSlicesInChunks(vs, chunkSize(len(vs)),length,stride,wrap) {
		running++
		go func(c []Vectors) {
			vectorsInSlicesApply(c, fn)
			done <- struct{}{}
		}(chunk)
	}
	for ; running > 0; running-- {
		<-done
	}
}

// return a VectorRefs referencing those Vector's that return true from the provided function.
func (vs Vectors) Select(fn func(*Vector)bool) (svs VectorRefs) {
	for i := range vs {
		if fn(&vs[i]){
			svs=append(svs,&vs[i])
		}
	}
	return
}

// return a VectorRefs referecing in Vectors those that are at equal spaced strides.
func (vs Vectors) Stride(s uint) (svs VectorRefs) {
	if s==0 {return}
	is:=int(s)
	svs=make(VectorRefs,len(vs)/is+1)
	for i:= range(svs) {
		svs[i]=&vs[i*is]
	}
	return
}

// return a slice of VectorRefs each referencing those Vector's that returned the slices index value from the provided function.
// or put another way;
// bin Vector's by the functions returned value.
// bins start at 1, a function returning a value of 0 causes the VecRef not to be in any of the returned bins.
func (vs Vectors) Split(fn func(Vector)uint) (ssvs []VectorRefs) {
	for i := range vs {
		ind:= fn(vs[i])
		if ind>0 {
			// pad, if needed, with a series of new VectorRefs to fill up to index. (max index not preknown)
			if ind > uint(len(ssvs)){
				ssvs=append(ssvs,make([]VectorRefs,ind-uint(len(ssvs)))...)
			}
			ssvs[ind-1]=append(ssvs[ind-1],&vs[i])
		}
	}
	return
}

<commit_msg>regional search min typo<commit_after>package tensor3

type Vectors []Vector

func NewVectors(cs ...BaseType) (vs Vectors) {
	vs = make(Vectors, (len(cs)+2)/3)
	for i := range vs {
		vs[i] = NewVector(cs[i*3:]...)
	}
	return
}

func (vs Vectors) Cross(v Vector) {
	vs.ForEach((*Vector).Cross, v)
}

func (vs Vectors) Add(v Vector) {
	vs.ForEach((*Vector).Add, v)
}

func (vs Vectors) Subtract(v Vector) {
	vs.ForEach((*Vector).Subtract, v)
}

func (vs Vectors) Project(v Vector) {
	vs.ForEach((*Vector).Project, v)
}

func (vs Vectors) Sum() (v Vector) {
	v.ForAll(vs, (*Vector).Add)
	return
}

func (vs Vectors) Multiply(s BaseType) {
	var multiply func(*Vector)
	multiply = func(v *Vector) {
		v.Multiply(s)
	}
	vs.ForEachNoParameter(multiply)
}

func (vs Vectors) Max() (v Vector) {
	v.Set(vs[0])
	v.ForAll(vs[1:], (*Vector).Max)
	return
}

func (vs Vectors) Min() (v Vector) {
	v.Set(vs[0])
	v.ForAll(vs[1:], (*Vector).Min)
	return
}

func (vs Vectors) Middle() (v Vector) {
	v = vs.Max()
	v.Mid(vs.Min())
	return
}

func (vs Vectors) Interpolate(v Vector, f float64) {
	f1 := Base64(1 - f)
	f2 := Base64(f)
	var interpolate func(*Vector, Vector)
	interpolate = func(v *Vector, v2 Vector) {
		v2.Multiply(f1)
		v.Multiply(f2)
		v.Add(v2)
	}
	vs.ForEach(interpolate, v)
}

func (vs Vectors) ForEach(fn func(*Vector, Vector), v Vector) {
	if !Parallel {
		vectorsApply(vs, fn, v)
	} else {
		vectorsApplyChunked(vs, fn, v)
	}
}

func vectorsApply(vs Vectors, fn func(*Vector, Vector), v Vector) {
	for i := range vs {
		fn(&vs[i], v)
	}
}

func vectorsApplyChunked(vs Vectors, fn func(*Vector, Vector), v Vector) {
	done := make(chan struct{}, 1)
	var running uint
	for chunk := range vectorsInChunks(vs, chunkSize(len(vs))) {
		running++
		go func(c Vectors) {
			vectorsApply(c, fn, v)
			done <- struct{}{}
		}(chunk)
	}
	for ; running > 0; running-- {
		<-done
	}
}

// for each vector apply a function with no parameters
func (vs Vectors) ForEachNoParameter(fn func(*Vector)) {
	var inner func(*Vector, Vector)
	inner = func(v *Vector, _ Vector) {
		fn(v)
	}
	vs.ForEach(inner, Vector{})
}

func (vs Vectors) CrossAll(vs2 Vectors) {
	vs.ForAll((*Vector).Cross, vs2)
}

func (vs Vectors) AddAll(vs2 Vectors) {
	vs.ForAll((*Vector).Add, vs2)
}

func (vs Vectors) SubtractAll(vs2 Vectors) {
	vs.ForAll((*Vector).Subtract, vs2)
}

func (vs Vectors) ProjectAll(vs2 Vectors) {
	vs.ForAll((*Vector).Project, vs2)
}

func (vs Vectors) ForAll(fn func(*Vector, Vector), vs2 Vectors) {
	if !Parallel {
		vectorsApplyAll(vs, fn, vs2)
	} else {
		vectorsApplyAllChunked(vs, fn, vs2)
	}
}

func vectorsApplyAll(vs Vectors, fn func(*Vector, Vector), vs2 Vectors) {
	for i := range vs {
		fn(&vs[i], vs2[i])
	}
}

func vectorsApplyAllChunked(vs Vectors, fn func(*Vector, Vector), vs2 Vectors) {
	done := make(chan struct{}, 1)
	var running uint
	// shorten vs to use only what we have in vs2
	if len(vs) > len(vs2) {
		vs = vs[:len(vs2)]
	}
	cs := chunkSize(len(vs))
	chunks2 := vectorsInChunks(vs2, cs)
	for chunk := range vectorsInChunks(vs, cs) {
		running++
		go func(c Vectors) {
			vectorsApplyAll(c, fn, <-chunks2)
			done <- struct{}{}
		}(chunk)
	}
	for ; running > 0; running-- {
		<-done
	}
}

// search Vectors for the two Vector's that return the minimum value from the provided function
func (vs Vectors) SearchMin(toMin func(Vector, Vector) BaseType) (i, j int, value BaseType) {
	// TODO search in chunks
	value = toMin(vs[0], vs[1])
	var v1, v2 Vector
	var il, jl int = 0, 1
	for jl, v2 = range vs[2:] {
		nl := toMin(vs[0], v2)
		if nl < value {
			value, j = nl, jl+2
		}
	}

	for il, v1 = range vs[1:] {
		for jl, v2 = range vs[il+2:] {
			nl := toMin(v1, v2)
			if nl < value {
				value, i, j = nl, il+1, jl+il+2
			}
		}
	}
	return
}

func (vs Vectors) SearchMinRegionally(toMin func(Vector, Vector) BaseType) (i, j int, value BaseType) {
	return vs.SearchMin(toMin)
}
/*
func (b *BaseType) Aggregate(vs Vectors,length,stride int, fn func(*BaseType, Vectors)) {
	for vsc := range vectorSlicesInChunks(vs,chunkSize(len(vs)),length,stride,false) {
		for _,vs:=range(vsc){
			fn(b, vs)
		}
	}
}
*/

// for each vector apply a function with no parameters
func (vs Vectors) ForEachInSlices(length,stride int,wrap bool,fn func(Vectors)) {
	if !Parallel {
		var i int
		for ;i<len(vs)-length+1;i+=stride{
			fn(vs[i:i+length])
		}
		if wrap{
			joinSlice:=make(Vectors,length,length)
			for ;i<len(vs);i+=stride{
				copy(joinSlice,vs[i:])
				copy(joinSlice[len(vs)-i:],vs)
				fn(joinSlice)
			}
		}
	} else {
		vectorsInSlicesApplyChunked(vs,length,stride,wrap, fn)
	}
}

func vectorsInSlicesApply(vss []Vectors,fn func(Vectors)) {
	for _,vs := range vss {
		fn(vs)
	}
}

func vectorsInSlicesApplyChunked(vs Vectors,length,stride int,wrap bool, fn func(Vectors)) {
	done := make(chan struct{}, 1)
	var running uint
	for chunk := range vectorSlicesInChunks(vs, chunkSize(len(vs)),length,stride,wrap) {
		running++
		go func(c []Vectors) {
			vectorsInSlicesApply(c, fn)
			done <- struct{}{}
		}(chunk)
	}
	for ; running > 0; running-- {
		<-done
	}
}

// return a VectorRefs referencing those Vector's that return true from the provided function.
func (vs Vectors) Select(fn func(*Vector)bool) (svs VectorRefs) {
	for i := range vs {
		if fn(&vs[i]){
			svs=append(svs,&vs[i])
		}
	}
	return
}

// return a VectorRefs referecing in Vectors those that are at equal spaced strides.
func (vs Vectors) Stride(s uint) (svs VectorRefs) {
	if s==0 {return}
	is:=int(s)
	svs=make(VectorRefs,len(vs)/is+1)
	for i:= range(svs) {
		svs[i]=&vs[i*is]
	}
	return
}

// return a slice of VectorRefs each referencing those Vector's that returned the slices index value from the provided function.
// or put another way;
// bin Vector's by the functions returned value.
// bins start at 1, a function returning a value of 0 causes the VecRef not to be in any of the returned bins.
func (vs Vectors) Split(fn func(Vector)uint) (ssvs []VectorRefs) {
	for i := range vs {
		ind:= fn(vs[i])
		if ind>0 {
			// pad, if needed, with a series of new VectorRefs to fill up to index. (max index not preknown)
			if ind > uint(len(ssvs)){
				ssvs=append(ssvs,make([]VectorRefs,ind-uint(len(ssvs)))...)
			}
			ssvs[ind-1]=append(ssvs[ind-1],&vs[i])
		}
	}
	return
}

<|endoftext|>


<commit_before>package litebrite

import (
	"bytes"
	"go/scanner"
	"go/token"
	"html/template"
)

type codeSegment struct {
	Code  string      // a segment of source code with the same style
	Pos   int         // the position of the segment in the source
	Tok   token.Token // the token type of the segment
	Class string
}

// getSegments splits the source into same-token-type chunks.
func getSegments(src []byte) []*codeSegment {
	segments := make([]*codeSegment, 0)

	// find the starting positions of all segments
	var s scanner.Scanner
	fset := token.NewFileSet()
	file := fset.AddFile("", fset.Base(), len(src))
	s.Init(file, src, nil, 1)
	for {
		filePos, tok, _ := s.Scan()
		if tok == token.EOF {
			break
		}

		pos := int(filePos) - file.Base() // pos in this source only
		segment := &codeSegment{Pos: pos, Tok: tok}
		segments = append(segments, segment)
	}

	// split the source at each position to get segments
	for i, segment := range segments {
		if i+1 == len(segments) {
			segment.Code = string(src[segment.Pos:])
		} else {
			next := segments[i+1]
			segment.Code = string(src[segment.Pos:next.Pos])
		}
	}

	return segments
}

const ELEM = `{{with .Class}}<div class="{{.}}">{{end}}{{.Code}}{{with .Class}}</div>{{end}}`

var elemT = template.Must(template.New("golang-elem").Parse(ELEM))

// buildHTML constructs an HTML string of elements from the segments in src.
func buildHTML(src []*codeSegment) []byte {
	var b bytes.Buffer
	for _, segment := range src {
		elemT.Execute(&b, segment)
	}

	return b.Bytes()
}

// Highlighter contains the CSS class names that are applied to the
// corresponding source code token types and the desired width of tabs.
type Highlighter struct {
	OperatorClass string
	IdentClass    string
	LiteralClass  string
	KeywordClass  string
	CommentClass  string
}

// highlightSegment adds the CSS class name specified by h to the segment.
func (h *Highlighter) highlightSegment(s *codeSegment) {
	switch {
	case s.Tok.IsKeyword():
		s.Class = h.KeywordClass
	case s.Tok.IsLiteral():
		if s.Tok == token.IDENT {
			s.Class = h.IdentClass
		} else {
			s.Class = h.LiteralClass
		}
	case s.Tok.IsOperator():
		s.Class = h.OperatorClass
	case s.Tok == token.COMMENT:
		s.Class = h.CommentClass
	default:
		panic("unknown token type!")
	}
}

// Highlight returns an HTML fragment containing elements for all Go tokens in
// src.  The elements will be of the form `<div class="TYPE_CLASS">CODE</div>`
// where `TYPE_CLASS` is the CSS class name provided in `h` corresponding to
// the token type of `CODE`.  For instance, if `CODE` is a keyword, then
// `TYPE_CLASS` will be `h.keywordClass`.
func (h *Highlighter) Highlight(src []byte) []byte {
	segments := getSegments(src)
	for _, segment := range segments {
		h.highlightSegment(segment)
	}
	return buildHTML(segments)
}
<commit_msg>leading/trailing whitespace no longer in <div>s; also, now using channels<commit_after>package litebrite

import (
	"bytes"
	"fmt"
	"go/scanner"
	"go/token"
	"html/template"
	"strings"
)

// ## Source tokenizing

// sep represents a split in source code due to a token occurrence.
type sep struct {
	pos int
	tok token.Token
}

// tokenize scans src and emits a sep on every token occurrence.
func tokenize(src string, tc chan *sep) {
	var s scanner.Scanner
	fset := token.NewFileSet() // boilerplate stuff for scanner...
	file := fset.AddFile("", fset.Base(), len(src))
	s.Init(file, []byte(src), nil, 1)
	for {
		filePos, tok, _ := s.Scan()
		tc <- &sep{int(filePos) - file.Base(), tok}
		if tok == token.EOF {
			close(tc)
			break
		}
	}
}

// tokens returns a channel that emits seps from tokenizing src.
func tokens(src string) <-chan *sep {
	tc := make(chan *sep)
	go tokenize(src, tc)
	return tc
}

// trim splits a source chunk into three pieces: the leading whitespace, the
// source code, and the trailing whitespace.
func trim(chunk string) (string, string, string) {
	code := strings.TrimSpace(chunk)
	wsl := chunk[:strings.Index(chunk, code)]
	wsr := chunk[len(wsl)+len(code):]
	return wsl, code, wsr
}

// ## HTML templating

type div struct {
	Code  string
	Class string
}

const ELEM = `{{with .Class}}<div class="{{.}}">{{end}}{{.Code}}{{with .Class}}</div>{{end}}`

var elemT = template.Must(template.New("golang-elem").Parse(ELEM))

// ## Highlighting

// Highlighter contains the CSS class names that are applied to the
// corresponding source code token types and the desired width of tabs.
type Highlighter struct {
	OperatorClass string
	IdentClass    string
	LiteralClass  string
	KeywordClass  string
	CommentClass  string
}

// getClass returns the CSS class name associated with tok.
func (h *Highlighter) getClass(tok token.Token) string {
	switch {
	case tok.IsKeyword():
		return h.KeywordClass
	case tok.IsLiteral():
		if tok == token.IDENT {
			return h.IdentClass
		} else {
			return h.LiteralClass
		}
	case tok.IsOperator():
		return h.OperatorClass
	case tok == token.COMMENT:
		return h.CommentClass
	case tok == token.ILLEGAL:
		break
	default:
		panic(fmt.Sprintf("unknown token type: %v", tok))
	}
	return ""
}

// Highlight returns an HTML fragment containing elements for all Go tokens in
// src.  The elements will be of the form `<div class="TYPE_CLASS">CODE</div>`
// where `TYPE_CLASS` is the CSS class name provided in `h` corresponding to
// the token type of `CODE`.  For instance, if `CODE` is a keyword, then
// `TYPE_CLASS` will be `h.keywordClass`.
func (h *Highlighter) Highlight(src string) string {
	var b bytes.Buffer
	tc := tokens(src)
	prev := <-tc
	for cur := <-tc; cur != nil; prev, cur = cur, <-tc {
		wsl, code, wsr := trim(src[prev.pos:cur.pos])
		b.WriteString(wsl)
		elemT.Execute(&b, &div{code, h.getClass(prev.tok)})
		b.WriteString(wsr)
	}
	return b.String()
}
<|endoftext|>


<commit_before>package test

import (
	"bytes"
	"net"
	"testing"
)

type TestServer struct {
	t *testing.T
	n net.Conn
}

func NewTestServer(t *testing.T, n net.Conn) *TestServer {
	var s = new(TestServer)

	s.t = t
	s.n = n

	return s
}

func (s *TestServer) AssertRead(v string) bool {
	var buf []byte
	var n int
	var e error

	buf = make([]byte, len(v))
	if n, e = s.n.Read(buf); e != nil {
		s.t.Errorf("\nerror: %#v\n", e)
		return false
	}

	var a []byte = []byte(v)
	var b []byte = buf[0:n]

	if !bytes.Equal(a, b) {
		s.t.Errorf("\nexpected: %#v\ngot: %#v\n", string(a), string(b))
		return false
	}

	return true
}

func (s *TestServer) AssertWrite(v string) bool {
	var e error

	if _, e = s.n.Write([]byte(v)); e != nil {
		s.t.Errorf("\nerror: %#v\n", e)
		return false
	}

	return true
}

func (s *TestServer) Close() {
	var e error

	if e = s.n.Close(); e != nil {
		panic(e)
	}
}
<commit_msg>Embed net.Conn in test server<commit_after>package test

import (
	"bytes"
	"net"
	"testing"
)

type TestServer struct {
	net.Conn
	t *testing.T
}

func NewTestServer(t *testing.T, n net.Conn) *TestServer {
	var s = new(TestServer)

	s.t = t
	s.Conn = n

	return s
}

func (s *TestServer) AssertRead(v string) bool {
	var buf []byte
	var n int
	var e error

	buf = make([]byte, len(v))
	if n, e = s.Conn.Read(buf); e != nil {
		s.t.Errorf("\nerror: %#v\n", e)
		return false
	}

	var a []byte = []byte(v)
	var b []byte = buf[0:n]

	if !bytes.Equal(a, b) {
		s.t.Errorf("\nexpected: %#v\ngot: %#v\n", string(a), string(b))
		return false
	}

	return true
}

func (s *TestServer) AssertWrite(v string) bool {
	var e error

	if _, e = s.Conn.Write([]byte(v)); e != nil {
		s.t.Errorf("\nerror: %#v\n", e)
		return false
	}

	return true
}

func (s *TestServer) Close() {
	var e error

	if e = s.Conn.Close(); e != nil {
		panic(e)
	}
}
<|endoftext|>


<commit_before>// +build fvtests

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

package fv_test

import (
	"context"
	"fmt"
	"math/rand"
	"strconv"
	"sync"
	"time"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"

	"github.com/projectcalico/felix/fv/utils"
	"github.com/projectcalico/libcalico-go/lib/options"

	log "github.com/sirupsen/logrus"

	"github.com/projectcalico/felix/fv/containers"
	"github.com/projectcalico/felix/fv/workload"
	api "github.com/projectcalico/libcalico-go/lib/apis/v3"
	client "github.com/projectcalico/libcalico-go/lib/clientv3"
	"github.com/projectcalico/libcalico-go/lib/net"
)

const (
	numPolicies    = 20
	numNetworkSets = 100
)

var _ = Context("Network sets churn test with initialized Felix and etcd datastore", func() {

	var (
		etcd     *containers.Container
		felix    *containers.Felix
		felixPID int
		client   client.Interface
	)

	BeforeEach(func() {
		topologyOptions := containers.DefaultTopologyOptions()
		topologyOptions.FelixLogSeverity = "debug"
		felix, etcd, client = containers.StartSingleNodeEtcdTopology(topologyOptions)
		felixPID = felix.GetFelixPID()
	})

	AfterEach(func() {
		if CurrentGinkgoTestDescription().Failed {
			felix.Exec("iptables-save", "-c")
		}
		felix.Stop()

		if CurrentGinkgoTestDescription().Failed {
			etcd.Exec("etcdctl", "ls", "--recursive", "/")
		}
		etcd.Stop()
	})

	Describe("connectivity tests", func() {
		var (
			w  [4]*workload.Workload
			cc *workload.ConnectivityChecker
		)

		createPolicy := func(policy *api.GlobalNetworkPolicy) {
			log.WithField("policy", dumpResource(policy)).Info("Creating policy")
			_, err := client.GlobalNetworkPolicies().Create(utils.Ctx, policy, utils.NoOptions)
			Expect(err).NotTo(HaveOccurred())
		}

		BeforeEach(func() {
			for ii := range w {
				iiStr := strconv.Itoa(ii)
				var ports string

				ports = "3000"
				w[ii] = workload.Run(
					felix,
					"w"+iiStr,
					"cali0"+iiStr,
					"10.65."+iiStr+".1", // IPs each in their own /24
					ports,
					"tcp",
				)

				w[ii].DefaultPort = "3000"
				w[ii].Configure(client)
			}

			cc = &workload.ConnectivityChecker{
				Protocol: "tcp",
			}

			pol := api.NewGlobalNetworkPolicy()
			pol.Namespace = "fv"
			pol.Name = "policy-1"
			pol.Spec.Ingress = []api.Rule{
				{
					Action: "Allow",
					Source: api.EntityRule{
						Selector: "has(allow-as-source)",
					},
					Destination: api.EntityRule{
						Selector: "has(allow-as-dest)",
					},
				},
			}
			pol.Spec.Egress = []api.Rule{
				{
					Action: "Allow",
				},
			}
			pol.Spec.Selector = "all()"

			createPolicy(pol)
		})

		It("with no matching network sets, should deny all", func() {
			cc.ExpectNone(w[0], w[1])
			cc.ExpectNone(w[0], w[2])
			cc.ExpectNone(w[0], w[3])
			cc.ExpectNone(w[1], w[0])
			cc.ExpectNone(w[1], w[2])
			cc.ExpectNone(w[1], w[3])
			cc.ExpectNone(w[2], w[0])
			cc.ExpectNone(w[2], w[1])
			cc.ExpectNone(w[2], w[3])
			cc.ExpectNone(w[3], w[0])
			cc.ExpectNone(w[3], w[1])
			cc.ExpectNone(w[3], w[2])
			cc.CheckConnectivity()
		})

		It("with network sets matching some workloads, should have expected connectivity", func() {
			srcNS := api.NewGlobalNetworkSet()
			srcNS.Name = "ns-1"
			srcNS.Spec.Nets = []string{
				"10.65.0.0/24",
				"10.65.1.0/24",
				"10.65.2.0/24",
			}
			srcNS.Labels = map[string]string{
				"allow-as-source": "",
			}
			_, err := client.GlobalNetworkSets().Create(utils.Ctx, srcNS, utils.NoOptions)
			Expect(err).NotTo(HaveOccurred())

			destNS := api.NewGlobalNetworkSet()
			destNS.Name = "ns-2"
			destNS.Spec.Nets = []string{
				"10.65.2.0/24",
				"10.65.3.0/24",
			}
			destNS.Labels = map[string]string{
				"allow-as-dest": "",
			}
			_, err = client.GlobalNetworkSets().Create(utils.Ctx, destNS, utils.NoOptions)
			Expect(err).NotTo(HaveOccurred())

			cc.ExpectNone(w[0], w[1]) // not in dest net set
			cc.ExpectSome(w[0], w[2])
			cc.ExpectSome(w[0], w[3])
			cc.ExpectNone(w[1], w[0]) // not in dest net set
			cc.ExpectSome(w[1], w[2])
			cc.ExpectSome(w[1], w[3])
			cc.ExpectNone(w[2], w[0]) // not in dest net set
			cc.ExpectNone(w[2], w[1]) // not in dest net set
			cc.ExpectSome(w[2], w[3])
			// 3 isn't in the source net set so it can't talk to anyone.
			cc.ExpectNone(w[3], w[0])
			cc.ExpectNone(w[3], w[1])
			cc.ExpectNone(w[3], w[2])
			cc.CheckConnectivity()
		})

		AfterEach(func() {
			for ii := range w {
				w[ii].Stop()
			}
		})
	})

	Describe("churn tests", func() {
		var (
			policies    []*api.GlobalNetworkPolicy
			networkSets []*api.GlobalNetworkSet
		)

		BeforeEach(func() {
			// Start a workload so we have something to add policy to
			w := workload.Run(
				felix,
				"w",
				"cali12345",
				"10.65.0.2",
				"8055",
				"tcp",
			)
			w.Configure(client)

			// Generate policies and network sets.
			for i := 0; i < numPolicies; i++ {
				pol := api.NewGlobalNetworkPolicy()
				pol.Name = fmt.Sprintf("policy-%d", i)
				pol.Spec = api.GlobalNetworkPolicySpec{
					Selector: "all()",
					Ingress: []api.Rule{
						{
							Action: "Allow",
							Source: api.EntityRule{
								Selector: fmt.Sprintf("label == '%d'", i),
							}},
					},
				}
				policies = append(policies, pol)
			}
			for i := 0; i < numNetworkSets; i++ {
				ns := api.NewGlobalNetworkSet()
				ns.Name = fmt.Sprintf("netset-%d", i)
				ns.Labels = map[string]string{
					"foo":   "bar",
					"baz":   "biff",
					"label": fmt.Sprintf("%d", i%numPolicies),
				}
				nets := generateNets(i * 100)
				ns.Spec = api.GlobalNetworkSetSpec{
					Nets: nets,
				}
				networkSets = append(networkSets, ns)
			}
			log.SetLevel(log.InfoLevel)
		})

		churnPolicies := func(iterations int) {
			log.Info("Churning policies...")
			created := false
			for i := 0; i < iterations; i++ {
				if created {
					for _, pol := range policies {
						log.WithField("policy", pol.Name).Info("Deleting policy")
						cxt, cancel := context.WithTimeout(context.Background(), 10*time.Second)
						_, err := client.GlobalNetworkPolicies().Delete(cxt, pol.Name, options.DeleteOptions{})
						cancel()
						if err != nil {
							log.WithError(err).Panic("Failed to delete policy")
						}
						time.Sleep(13 * time.Millisecond)
					}
				}
				for _, pol := range policies {
					log.WithField("policy", pol.Name).Info("Creating policy")
					cxt, cancel := context.WithTimeout(context.Background(), 10*time.Second)
					_, err := client.GlobalNetworkPolicies().Create(cxt, pol, options.SetOptions{})
					cancel()
					if err != nil {
						log.WithError(err).WithField("name", pol.Name).Panic("Failed to add policy")
					}
					time.Sleep(17 * time.Millisecond)
				}
				created = true
			}
		}

		churnNetworkSets := func(iterations int) {
			log.Info("Churning network sets...")
			created := false
			for i := 0; i < iterations; i++ {
				if created {
					for _, ns := range networkSets {
						log.WithField("name", ns.Name).Info("Deleting network set")
						cxt, cancel := context.WithTimeout(context.Background(), 10*time.Second)
						_, err := client.GlobalNetworkSets().Delete(cxt, ns.Name, options.DeleteOptions{})
						cancel()
						if err != nil {
							log.WithError(err).Panic("Failed to delete network set")
						}
						time.Sleep(19 * time.Millisecond)
					}
				}
				for _, ns := range networkSets {
					log.WithField("name", ns.Name).Info("Creating network set")
					cxt, cancel := context.WithTimeout(context.Background(), 10*time.Second)
					_, err := client.GlobalNetworkSets().Create(cxt, ns, options.SetOptions{})
					cancel()
					if err != nil {
						log.WithError(err).Panic("Failed to add network set")
					}
					time.Sleep(29 * time.Millisecond)
				}
				created = true
			}
		}

		It("should withstand churn", func() {
			var wg sync.WaitGroup
			wg.Add(2)

			go func() {
				defer wg.Done()
				defer log.Info("Finished churning policies")
				churnPolicies(6)
			}()
			go func() {
				RegisterFailHandler(Fail)
				defer wg.Done()
				defer log.Info("Finished churning network sets")
				churnNetworkSets(3)
			}()

			wg.Wait()

			// getFelixPIDs may return more than one PID, transiently due to Felix calling fork().
			Expect(felix.GetFelixPID()).To(Equal(felixPID))
		})
	})
})

func generateNets(n int) []string {
	var nets []string
	for i := 0; i < n; i++ {
		a := rand.Intn(255)
		b := rand.Intn(255)
		pr := rand.Intn(16) + 16
		netStr := fmt.Sprintf("%d.%d.0.0/%d", a, b, pr)
		_, cidr, err := net.ParseCIDR(netStr)
		Expect(err).NotTo(HaveOccurred(), fmt.Sprintf("Failed to parse %s", netStr))
		nets = append(nets, cidr.String())
	}
	return nets
}
<commit_msg>Avoid debug logs, they clog the pipe.<commit_after>// +build fvtests

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

package fv_test

import (
	"context"
	"fmt"
	"math/rand"
	"strconv"
	"sync"
	"time"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"

	"github.com/projectcalico/felix/fv/utils"
	"github.com/projectcalico/libcalico-go/lib/options"

	log "github.com/sirupsen/logrus"

	"github.com/projectcalico/felix/fv/containers"
	"github.com/projectcalico/felix/fv/workload"
	api "github.com/projectcalico/libcalico-go/lib/apis/v3"
	client "github.com/projectcalico/libcalico-go/lib/clientv3"
	"github.com/projectcalico/libcalico-go/lib/net"
)

const (
	numPolicies    = 20
	numNetworkSets = 100
)

var _ = Context("Network sets churn test with initialized Felix and etcd datastore", func() {

	var (
		etcd     *containers.Container
		felix    *containers.Felix
		felixPID int
		client   client.Interface
	)

	BeforeEach(func() {
		topologyOptions := containers.DefaultTopologyOptions()
		felix, etcd, client = containers.StartSingleNodeEtcdTopology(topologyOptions)
		felixPID = felix.GetFelixPID()
	})

	AfterEach(func() {
		if CurrentGinkgoTestDescription().Failed {
			felix.Exec("iptables-save", "-c")
		}
		felix.Stop()

		if CurrentGinkgoTestDescription().Failed {
			etcd.Exec("etcdctl", "ls", "--recursive", "/")
		}
		etcd.Stop()
	})

	Describe("connectivity tests", func() {
		var (
			w  [4]*workload.Workload
			cc *workload.ConnectivityChecker
		)

		createPolicy := func(policy *api.GlobalNetworkPolicy) {
			log.WithField("policy", dumpResource(policy)).Info("Creating policy")
			_, err := client.GlobalNetworkPolicies().Create(utils.Ctx, policy, utils.NoOptions)
			Expect(err).NotTo(HaveOccurred())
		}

		BeforeEach(func() {
			for ii := range w {
				iiStr := strconv.Itoa(ii)
				var ports string

				ports = "3000"
				w[ii] = workload.Run(
					felix,
					"w"+iiStr,
					"cali0"+iiStr,
					"10.65."+iiStr+".1", // IPs each in their own /24
					ports,
					"tcp",
				)

				w[ii].DefaultPort = "3000"
				w[ii].Configure(client)
			}

			cc = &workload.ConnectivityChecker{
				Protocol: "tcp",
			}

			pol := api.NewGlobalNetworkPolicy()
			pol.Namespace = "fv"
			pol.Name = "policy-1"
			pol.Spec.Ingress = []api.Rule{
				{
					Action: "Allow",
					Source: api.EntityRule{
						Selector: "has(allow-as-source)",
					},
					Destination: api.EntityRule{
						Selector: "has(allow-as-dest)",
					},
				},
			}
			pol.Spec.Egress = []api.Rule{
				{
					Action: "Allow",
				},
			}
			pol.Spec.Selector = "all()"

			createPolicy(pol)
		})

		It("with no matching network sets, should deny all", func() {
			cc.ExpectNone(w[0], w[1])
			cc.ExpectNone(w[0], w[2])
			cc.ExpectNone(w[0], w[3])
			cc.ExpectNone(w[1], w[0])
			cc.ExpectNone(w[1], w[2])
			cc.ExpectNone(w[1], w[3])
			cc.ExpectNone(w[2], w[0])
			cc.ExpectNone(w[2], w[1])
			cc.ExpectNone(w[2], w[3])
			cc.ExpectNone(w[3], w[0])
			cc.ExpectNone(w[3], w[1])
			cc.ExpectNone(w[3], w[2])
			cc.CheckConnectivity()
		})

		It("with network sets matching some workloads, should have expected connectivity", func() {
			srcNS := api.NewGlobalNetworkSet()
			srcNS.Name = "ns-1"
			srcNS.Spec.Nets = []string{
				"10.65.0.0/24",
				"10.65.1.0/24",
				"10.65.2.0/24",
			}
			srcNS.Labels = map[string]string{
				"allow-as-source": "",
			}
			_, err := client.GlobalNetworkSets().Create(utils.Ctx, srcNS, utils.NoOptions)
			Expect(err).NotTo(HaveOccurred())

			destNS := api.NewGlobalNetworkSet()
			destNS.Name = "ns-2"
			destNS.Spec.Nets = []string{
				"10.65.2.0/24",
				"10.65.3.0/24",
			}
			destNS.Labels = map[string]string{
				"allow-as-dest": "",
			}
			_, err = client.GlobalNetworkSets().Create(utils.Ctx, destNS, utils.NoOptions)
			Expect(err).NotTo(HaveOccurred())

			cc.ExpectNone(w[0], w[1]) // not in dest net set
			cc.ExpectSome(w[0], w[2])
			cc.ExpectSome(w[0], w[3])
			cc.ExpectNone(w[1], w[0]) // not in dest net set
			cc.ExpectSome(w[1], w[2])
			cc.ExpectSome(w[1], w[3])
			cc.ExpectNone(w[2], w[0]) // not in dest net set
			cc.ExpectNone(w[2], w[1]) // not in dest net set
			cc.ExpectSome(w[2], w[3])
			// 3 isn't in the source net set so it can't talk to anyone.
			cc.ExpectNone(w[3], w[0])
			cc.ExpectNone(w[3], w[1])
			cc.ExpectNone(w[3], w[2])
			cc.CheckConnectivity()
		})

		AfterEach(func() {
			for ii := range w {
				w[ii].Stop()
			}
		})
	})

	Describe("churn tests", func() {
		var (
			policies    []*api.GlobalNetworkPolicy
			networkSets []*api.GlobalNetworkSet
		)

		BeforeEach(func() {
			// Start a workload so we have something to add policy to
			w := workload.Run(
				felix,
				"w",
				"cali12345",
				"10.65.0.2",
				"8055",
				"tcp",
			)
			w.Configure(client)

			// Generate policies and network sets.
			for i := 0; i < numPolicies; i++ {
				pol := api.NewGlobalNetworkPolicy()
				pol.Name = fmt.Sprintf("policy-%d", i)
				pol.Spec = api.GlobalNetworkPolicySpec{
					Selector: "all()",
					Ingress: []api.Rule{
						{
							Action: "Allow",
							Source: api.EntityRule{
								Selector: fmt.Sprintf("label == '%d'", i),
							}},
					},
				}
				policies = append(policies, pol)
			}
			for i := 0; i < numNetworkSets; i++ {
				ns := api.NewGlobalNetworkSet()
				ns.Name = fmt.Sprintf("netset-%d", i)
				ns.Labels = map[string]string{
					"foo":   "bar",
					"baz":   "biff",
					"label": fmt.Sprintf("%d", i%numPolicies),
				}
				nets := generateNets(i * 100)
				ns.Spec = api.GlobalNetworkSetSpec{
					Nets: nets,
				}
				networkSets = append(networkSets, ns)
			}
			log.SetLevel(log.InfoLevel)
		})

		churnPolicies := func(iterations int) {
			log.Info("Churning policies...")
			created := false
			for i := 0; i < iterations; i++ {
				if created {
					for _, pol := range policies {
						log.WithField("policy", pol.Name).Info("Deleting policy")
						cxt, cancel := context.WithTimeout(context.Background(), 10*time.Second)
						_, err := client.GlobalNetworkPolicies().Delete(cxt, pol.Name, options.DeleteOptions{})
						cancel()
						if err != nil {
							log.WithError(err).Panic("Failed to delete policy")
						}
						time.Sleep(13 * time.Millisecond)
					}
				}
				for _, pol := range policies {
					log.WithField("policy", pol.Name).Info("Creating policy")
					cxt, cancel := context.WithTimeout(context.Background(), 10*time.Second)
					_, err := client.GlobalNetworkPolicies().Create(cxt, pol, options.SetOptions{})
					cancel()
					if err != nil {
						log.WithError(err).WithField("name", pol.Name).Panic("Failed to add policy")
					}
					time.Sleep(17 * time.Millisecond)
				}
				created = true
			}
		}

		churnNetworkSets := func(iterations int) {
			log.Info("Churning network sets...")
			created := false
			for i := 0; i < iterations; i++ {
				if created {
					for _, ns := range networkSets {
						log.WithField("name", ns.Name).Info("Deleting network set")
						cxt, cancel := context.WithTimeout(context.Background(), 10*time.Second)
						_, err := client.GlobalNetworkSets().Delete(cxt, ns.Name, options.DeleteOptions{})
						cancel()
						if err != nil {
							log.WithError(err).Panic("Failed to delete network set")
						}
						time.Sleep(19 * time.Millisecond)
					}
				}
				for _, ns := range networkSets {
					log.WithField("name", ns.Name).Info("Creating network set")
					cxt, cancel := context.WithTimeout(context.Background(), 10*time.Second)
					_, err := client.GlobalNetworkSets().Create(cxt, ns, options.SetOptions{})
					cancel()
					if err != nil {
						log.WithError(err).Panic("Failed to add network set")
					}
					time.Sleep(29 * time.Millisecond)
				}
				created = true
			}
		}

		It("should withstand churn", func() {
			var wg sync.WaitGroup
			wg.Add(2)

			go func() {
				defer wg.Done()
				defer log.Info("Finished churning policies")
				churnPolicies(6)
			}()
			go func() {
				RegisterFailHandler(Fail)
				defer wg.Done()
				defer log.Info("Finished churning network sets")
				churnNetworkSets(3)
			}()

			wg.Wait()

			// getFelixPIDs may return more than one PID, transiently due to Felix calling fork().
			Expect(felix.GetFelixPID()).To(Equal(felixPID))
		})
	})
})

func generateNets(n int) []string {
	var nets []string
	for i := 0; i < n; i++ {
		a := rand.Intn(255)
		b := rand.Intn(255)
		pr := rand.Intn(16) + 16
		netStr := fmt.Sprintf("%d.%d.0.0/%d", a, b, pr)
		_, cidr, err := net.ParseCIDR(netStr)
		Expect(err).NotTo(HaveOccurred(), fmt.Sprintf("Failed to parse %s", netStr))
		nets = append(nets, cidr.String())
	}
	return nets
}
<|endoftext|>


<commit_before>//: ----------------------------------------------------------------------------
//: Copyright (C) 2017 Verizon.  All Rights Reserved.
//: All Rights Reserved
//:
//: file:    mirror.go
//: details: TODO
//: author:  Mehrdad Arshad Rad
//: date:    02/01/2017
//:
//: 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 mirror

import (
	"net"
	"syscall"
)

const (
	IPv4HLen = 20
	IPv6HLen = 40
	UDPHLen  = 8
	UDPProto = 17
)

type Conn struct {
	family int
	sotype int
	proto  int
	fd     int
	raddr  syscall.Sockaddr
}

func NewRawConn(raddr net.IP) (Conn, error) {
	var err error
	conn := Conn{
		sotype: syscall.SOCK_RAW,
		proto:  syscall.IPPROTO_RAW,
	}

	if ipv4 := raddr.To4(); ipv4 != nil {
		ip := [4]byte{}
		copy(ip[:], ipv4)

		conn.family = syscall.AF_INET
		conn.raddr = &syscall.SockaddrInet4{
			Port: 0,
			Addr: ip,
		}
	} else if ipv6 := raddr.To16(); ipv6 != nil {
		ip := [16]byte{}
		copy(ip[:], ipv6)

		conn.family = syscall.AF_INET6
		conn.raddr = &syscall.SockaddrInet6{
			Addr: ip,
		}

	}

	conn.fd, err = syscall.Socket(conn.family, conn.sotype, conn.proto)

	return conn, err
}

func (c *Conn) Send(b []byte) error {
	return syscall.Sendto(c.fd, b, 0, c.raddr)
}

func (c *Conn) Close(b []byte) error {
	return syscall.Close(c.fd)
}
<commit_msg>new IP interface: IPv4+IPv6<commit_after>//: ----------------------------------------------------------------------------
//: Copyright (C) 2017 Verizon.  All Rights Reserved.
//: All Rights Reserved
//:
//: file:    mirror.go
//: details: TODO
//: author:  Mehrdad Arshad Rad
//: date:    02/01/2017
//:
//: 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 mirror

import (
	"net"
	"syscall"
)

const (
	IPv4HLen = 20
	IPv6HLen = 40
	UDPHLen  = 8
	UDPProto = 17
)

type Conn struct {
	family int
	sotype int
	proto  int
	fd     int
	raddr  syscall.Sockaddr
}

type IP interface {
	Marshal() []byte
	SetLen([]byte, int)
	SetAddrs([]byte, net.IP, net.IP)
}

func NewRawConn(raddr net.IP) (Conn, error) {
	var err error
	conn := Conn{
		sotype: syscall.SOCK_RAW,
		proto:  syscall.IPPROTO_RAW,
	}

	if ipv4 := raddr.To4(); ipv4 != nil {
		ip := [4]byte{}
		copy(ip[:], ipv4)

		conn.family = syscall.AF_INET
		conn.raddr = &syscall.SockaddrInet4{
			Port: 0,
			Addr: ip,
		}
	} else if ipv6 := raddr.To16(); ipv6 != nil {
		ip := [16]byte{}
		copy(ip[:], ipv6)

		conn.family = syscall.AF_INET6
		conn.raddr = &syscall.SockaddrInet6{
			Addr: ip,
		}

	}

	conn.fd, err = syscall.Socket(conn.family, conn.sotype, conn.proto)

	return conn, err
}

func (c *Conn) Send(b []byte) error {
	return syscall.Sendto(c.fd, b, 0, c.raddr)
}

func (c *Conn) Close(b []byte) error {
	return syscall.Close(c.fd)
}
<|endoftext|>


<commit_before>// Copyright 2010 The Go 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 main

import (
	"fmt"
	"html/template"
	"io/ioutil"
	"net/http"
	"os/exec"
	"path/filepath"
	"regexp"

	"github.com/russross/blackfriday"
)

type Page struct {
	Title string
	Body  []byte
}

func (p *Page) save() error {
	filename := p.Title + ".txt"
	return ioutil.WriteFile(filename, p.Body, 0600)
}

func loadPage(title string) (*Page, error) {
	filename := title + ".txt"
	body, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil, err
	}
	html := blackfriday.MarkdownCommon(body)
	fmt.Println(string(html)) // TODO remove
	return &Page{Title: title, Body: html}, nil
}

func viewHandler(w http.ResponseWriter, r *http.Request, title string) {
	p, err := loadPage(title)
	if err != nil {
		http.Redirect(w, r, "/edit/"+title, http.StatusFound)
		return
	}
	renderTemplate(w, "view", p)
}

func editHandler(w http.ResponseWriter, r *http.Request, title string) {
	p, err := loadPage(title)
	if err != nil {
		p = &Page{Title: title}
	}
	renderTemplate(w, "edit", p)
}

func saveHandler(w http.ResponseWriter, r *http.Request, title string) {
	body := r.FormValue("body")
	p := &Page{Title: title, Body: []byte(body)}
	err := p.save()
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	http.Redirect(w, r, "/view/"+title, http.StatusFound)
}

var templateDir string = "templates"
var templateFiles []string = []string{
	filepath.Join(templateDir, "edit.html"),
	filepath.Join(templateDir, "view.html")}

var templates = template.Must(template.ParseFiles(templateFiles...))

func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
	// TODO http://stackoverflow.com/questions/18589973/html-template-how-to-get-javascript-json-escaping-without-script-tag
	err := templates.ExecuteTemplate(w, tmpl+".html", p)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}

var validPath = regexp.MustCompile("^/(edit|save|view)/([a-zA-Z0-9]+)$")

func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		m := validPath.FindStringSubmatch(r.URL.Path)
		if m == nil {
			http.NotFound(w, r)
			return
		}
		fn(w, r, m[2])
	}
}

func main() {
	var server = "localhost:8080"

	browser := exec.Command(`C:\Windows\System32\rundll32.exe`, "url.dll,FileProtocolHandler", "http://"+server+"/view/Home")
	if err := browser.Start(); err != nil {
		panic(err)
	}

	http.HandleFunc("/view/", makeHandler(viewHandler))
	http.HandleFunc("/edit/", makeHandler(editHandler))
	http.HandleFunc("/save/", makeHandler(saveHandler))
	http.ListenAndServe(server, nil)
}
<commit_msg>Made the opening of the Web browser portable.<commit_after>// Copyright 2010 The Go 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 main

import (
	"fmt"
	"html/template"
	"io/ioutil"
	"net/http"
	"os/exec"
	"path/filepath"
	"regexp"
	"runtime"

	"github.com/russross/blackfriday"
)

type Page struct {
	Title string
	Body  []byte
}

func (p *Page) save() error {
	filename := p.Title + ".txt"
	return ioutil.WriteFile(filename, p.Body, 0600)
}

func loadPage(title string) (*Page, error) {
	filename := title + ".txt"
	body, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil, err
	}
	html := blackfriday.MarkdownCommon(body)
	fmt.Println(string(html)) // TODO remove
	return &Page{Title: title, Body: html}, nil
}

func viewHandler(w http.ResponseWriter, r *http.Request, title string) {
	p, err := loadPage(title)
	if err != nil {
		http.Redirect(w, r, "/edit/"+title, http.StatusFound)
		return
	}
	renderTemplate(w, "view", p)
}

func editHandler(w http.ResponseWriter, r *http.Request, title string) {
	p, err := loadPage(title)
	if err != nil {
		p = &Page{Title: title}
	}
	renderTemplate(w, "edit", p)
}

func saveHandler(w http.ResponseWriter, r *http.Request, title string) {
	body := r.FormValue("body")
	p := &Page{Title: title, Body: []byte(body)}
	err := p.save()
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	http.Redirect(w, r, "/view/"+title, http.StatusFound)
}

var templateDir string = "templates"
var templateFiles []string = []string{
	filepath.Join(templateDir, "edit.html"),
	filepath.Join(templateDir, "view.html")}

var templates = template.Must(template.ParseFiles(templateFiles...))

func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
	// TODO http://stackoverflow.com/questions/18589973/html-template-how-to-get-javascript-json-escaping-without-script-tag
	err := templates.ExecuteTemplate(w, tmpl+".html", p)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}

var validPath = regexp.MustCompile("^/(edit|save|view)/([a-zA-Z0-9]+)$")

func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		m := validPath.FindStringSubmatch(r.URL.Path)
		if m == nil {
			http.NotFound(w, r)
			return
		}
		fn(w, r, m[2])
	}
}

func main() {
	var server = "localhost:8080"

	var browser *exec.Cmd
	var url string = "http://" + server + "/view/Home"

	switch runtime.GOOS {
	case "windows":
		browser = exec.Command(`C:\Windows\System32\rundll32.exe`, "url.dll,FileProtocolHandler", url)
	case "darwin":
		browser = exec.Command("open", url)
	default:
		browser = exec.Command("xdg-open", url)
	}
	if err := browser.Start(); err != nil {
		panic(err)
	}

	http.HandleFunc("/view/", makeHandler(viewHandler))
	http.HandleFunc("/edit/", makeHandler(editHandler))
	http.HandleFunc("/save/", makeHandler(saveHandler))
	http.ListenAndServe(server, nil)
}
<|endoftext|>


<commit_before>// Copyright (c) 2021 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package fxevent

import (
	"fmt"
	"io"
	"strings"
)

// ConsoleLogger is an Fx event logger that attempts to write human-readable
// mesasges to the console.
//
// Use this during development.
type ConsoleLogger struct {
	W io.Writer
}

var _ Logger = (*ConsoleLogger)(nil)

func (l *ConsoleLogger) logf(msg string, args ...interface{}) {
	fmt.Fprintf(l.W, "[Fx] "+msg+"\n", args...)
}

// LogEvent logs the given event to the provided Zap logger.
func (l *ConsoleLogger) LogEvent(event Event) {
	switch e := event.(type) {
	case *OnStartExecuting:
		l.logf("HOOK OnStart\t\t%s executing (caller: %s)", e.FunctionName, e.CallerName)
	case *OnStartExecuted:
		if e.Err != nil {
			l.logf("HOOK OnStart\t\t%s called by %s failed in %s: %v", e.FunctionName, e.CallerName, e.Runtime, e.Err)
		} else {
			l.logf("HOOK OnStart\t\t%s called by %s ran successfully in %s", e.FunctionName, e.CallerName, e.Runtime)
		}
	case *OnStopExecuting:
		l.logf("HOOK OnStop\t\t%s executing (caller: %s)", e.FunctionName, e.CallerName)
	case *OnStopExecuted:
		if e.Err != nil {
			l.logf("HOOK OnStop\t\t%s called by %s failed in %s: %v", e.FunctionName, e.CallerName, e.Runtime, e.Err)
		} else {
			l.logf("HOOK OnStop\t\t%s called by %s ran successfully in %s", e.FunctionName, e.CallerName, e.Runtime)
		}
	case *Supplied:
		if e.Err != nil {
			l.logf("ERROR\tFailed to supply %v: %v", e.TypeName, e.Err)
		} else {
			l.logf("SUPPLY\t%v", e.TypeName)
		}
	case *Provided:
		for _, rtype := range e.OutputTypeNames {
			l.logf("PROVIDE\t%v <= %v", rtype, e.ConstructorName)
		}
		if e.Err != nil {
			l.logf("Error after options were applied: %v", e.Err)
		}
	case *Invoking:
		l.logf("INVOKE\t\t%s", e.FunctionName)
	case *Invoked:
		if e.Err != nil {
			l.logf("ERROR\t\tfx.Invoke(%v) called from:\n%+vFailed: %v", e.FunctionName, e.Trace, e.Err)
		}
	case *Stopping:
		l.logf("%v", strings.ToUpper(e.Signal.String()))
	case *Stopped:
		if e.Err != nil {
			l.logf("ERROR\t\tFailed to stop cleanly: %v", e.Err)
		}
	case *RollingBack:
		l.logf("ERROR\t\tStart failed, rolling back: %v", e.StartErr)
	case *RolledBack:
		if e.Err != nil {
			l.logf("ERROR\t\tCouldn't roll back cleanly: %v", e.Err)
		}
	case *Started:
		if e.Err != nil {
			l.logf("ERROR\t\tFailed to start: %v", e.Err)
		} else {
			l.logf("RUNNING")
		}
	case *LoggerInitialized:
		if e.Err != nil {
			l.logf("ERROR\t\tFailed to initialize custom logger: %+v", e.Err)
		} else {
			l.logf("LOGGER\tInitialized custom logger from %v", e.ConstructorName)
		}
	}
}
<commit_msg>Fix Typo for ConsoleLogger godoc (#842)<commit_after>// Copyright (c) 2021 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package fxevent

import (
	"fmt"
	"io"
	"strings"
)

// ConsoleLogger is an Fx event logger that attempts to write human-readable
// messages to the console.
//
// Use this during development.
type ConsoleLogger struct {
	W io.Writer
}

var _ Logger = (*ConsoleLogger)(nil)

func (l *ConsoleLogger) logf(msg string, args ...interface{}) {
	fmt.Fprintf(l.W, "[Fx] "+msg+"\n", args...)
}

// LogEvent logs the given event to the provided Zap logger.
func (l *ConsoleLogger) LogEvent(event Event) {
	switch e := event.(type) {
	case *OnStartExecuting:
		l.logf("HOOK OnStart\t\t%s executing (caller: %s)", e.FunctionName, e.CallerName)
	case *OnStartExecuted:
		if e.Err != nil {
			l.logf("HOOK OnStart\t\t%s called by %s failed in %s: %v", e.FunctionName, e.CallerName, e.Runtime, e.Err)
		} else {
			l.logf("HOOK OnStart\t\t%s called by %s ran successfully in %s", e.FunctionName, e.CallerName, e.Runtime)
		}
	case *OnStopExecuting:
		l.logf("HOOK OnStop\t\t%s executing (caller: %s)", e.FunctionName, e.CallerName)
	case *OnStopExecuted:
		if e.Err != nil {
			l.logf("HOOK OnStop\t\t%s called by %s failed in %s: %v", e.FunctionName, e.CallerName, e.Runtime, e.Err)
		} else {
			l.logf("HOOK OnStop\t\t%s called by %s ran successfully in %s", e.FunctionName, e.CallerName, e.Runtime)
		}
	case *Supplied:
		if e.Err != nil {
			l.logf("ERROR\tFailed to supply %v: %v", e.TypeName, e.Err)
		} else {
			l.logf("SUPPLY\t%v", e.TypeName)
		}
	case *Provided:
		for _, rtype := range e.OutputTypeNames {
			l.logf("PROVIDE\t%v <= %v", rtype, e.ConstructorName)
		}
		if e.Err != nil {
			l.logf("Error after options were applied: %v", e.Err)
		}
	case *Invoking:
		l.logf("INVOKE\t\t%s", e.FunctionName)
	case *Invoked:
		if e.Err != nil {
			l.logf("ERROR\t\tfx.Invoke(%v) called from:\n%+vFailed: %v", e.FunctionName, e.Trace, e.Err)
		}
	case *Stopping:
		l.logf("%v", strings.ToUpper(e.Signal.String()))
	case *Stopped:
		if e.Err != nil {
			l.logf("ERROR\t\tFailed to stop cleanly: %v", e.Err)
		}
	case *RollingBack:
		l.logf("ERROR\t\tStart failed, rolling back: %v", e.StartErr)
	case *RolledBack:
		if e.Err != nil {
			l.logf("ERROR\t\tCouldn't roll back cleanly: %v", e.Err)
		}
	case *Started:
		if e.Err != nil {
			l.logf("ERROR\t\tFailed to start: %v", e.Err)
		} else {
			l.logf("RUNNING")
		}
	case *LoggerInitialized:
		if e.Err != nil {
			l.logf("ERROR\t\tFailed to initialize custom logger: %+v", e.Err)
		} else {
			l.logf("LOGGER\tInitialized custom logger from %v", e.ConstructorName)
		}
	}
}
<|endoftext|>


<commit_before><commit_msg>Fix redundant error checking.<commit_after><|endoftext|>