<commit_before>package main

import (
	"bytes"
	"fmt"
	"os"
	"os/exec"
	"regexp"
	"runtime"
	"strconv"
	"strings"
	"sync"
	"time"

	"github.com/alecthomas/kingpin"
)

type Severity string

// Linter message severity levels.
const (
	Warning Severity = "warning"
	Error   Severity = "error"
)

type Linter string

func (l Linter) Command() string {
	return string(l[0:strings.Index(string(l), ":")])
}

func (l Linter) Pattern() string {
	return string(l[strings.Index(string(l), ":"):])
}

var (
	predefinedPatterns = map[string]string{
		"PATH:LINE:COL:MESSAGE": `(?P<path>[^:]+):(?P<line>\d+):(?P<col>\d+):\s*(?P<message>.*)`,
		"PATH:LINE:MESSAGE":     `(?P<path>[^:]+):(?P<line>\d+):\s*(?P<message>.*)`,
	}
	lintersFlag = map[string]string{
		// main.go:8:10: should omit type map[string]string from declaration of var linters; it will be inferred from the right-hand side
		"golint": "golint {path}:PATH:LINE:COL:MESSAGE",
		// test/stutter.go:19: missing argument for Printf("%d"): format reads arg 1, have only 0 args
		"vet":         "go tool vet {path}:PATH:LINE:MESSAGE",
		"gotype":      "gotype {path}:PATH:LINE:COL:MESSAGE",
		"errcheck":    `errcheck {path}:(?P<path>[^:]+):(?P<line>\d+):(?P<col>\d+)\t(?P<message>.*)`,
		"varcheck":    "varcheck {path}:PATH:LINE:MESSAGE",
		"structcheck": "structcheck {path}:PATH:LINE:MESSAGE",
		"defercheck":  "defercheck {path}:PATH:LINE:MESSAGE",
		"deadcode":    `deadcode {path}:deadcode: (?P<path>[^:]+):(?P<line>\d+):(?P<col>\d+):\s*(?P<message>.*)`,
	}
	linterMessageOverrideFlag = map[string]string{
		"errcheck":    "error return value not checked ({message})",
		"varcheck":    "unused global variable {message}",
		"structcheck": "unused struct field {message}",
	}
	linterSeverityFlag = map[string]string{
		"errcheck":    "warning",
		"golint":      "warning",
		"varcheck":    "warning",
		"structcheck": "warning",
		"deadcode":    "warning",
	}
	installMap = map[string]string{
		"golint":      "github.com/golang/lint/golint",
		"gotype":      "golang.org/x/tools/cmd/gotype",
		"errcheck":    "github.com/kisielk/errcheck",
		"defercheck":  "github.com/opennota/check/cmd/defercheck",
		"varcheck":    "github.com/opennota/check/cmd/varcheck",
		"structcheck": "github.com/opennota/check/cmd/structcheck",
		"vet":         "golang.org/x/tools/cmd/vet",
		"deadcode":    "github.com/remyoudompheng/go-misc/deadcode",
	}
	pathArg            = kingpin.Arg("path", "Directory to lint.").Default(".").String()
	fastFlag           = kingpin.Flag("fast", "Only run fast linters.").Bool()
	installFlag        = kingpin.Flag("install", "Attempt to install all known linters.").Short('i').Bool()
	updateFlag         = kingpin.Flag("update", "Pass -u to go tool when installing.").Short('u').Bool()
	disableLintersFlag = kingpin.Flag("disable", "List of linters to disable.").PlaceHolder("LINTER").Short('D').Strings()
	debugFlag          = kingpin.Flag("debug", "Display messages for failed linters, etc.").Short('d').Bool()
	concurrencyFlag    = kingpin.Flag("concurrency", "Number of concurrent linters to run.").Default("16").Short('j').Int()
	excludeFlag        = kingpin.Flag("exclude", "Exclude messages matching this regular expression.").PlaceHolder("REGEXP").String()
)

func init() {
	kingpin.Flag("linter", "Specify a linter.").PlaceHolder("NAME:COMMAND:PATTERN").StringMapVar(&lintersFlag)
	kingpin.Flag("message-overrides", "Override message from linter. {message} will be expanded to the original message.").PlaceHolder("LINTER:MESSAGE").StringMapVar(&linterMessageOverrideFlag)
	kingpin.Flag("severity", "Map of linter severities.").PlaceHolder("LINTER:SEVERITY").StringMapVar(&linterSeverityFlag)
}

type Issue struct {
	severity Severity
	path     string
	line     int
	col      int
	message  string
}

func (m *Issue) String() string {
	col := ""
	if m.col != 0 {
		col = fmt.Sprintf("%d", m.col)
	}
	return fmt.Sprintf("%s:%d:%s:%s: %s", m.path, m.line, col, m.severity, m.message)
}

type Issues []*Issue

func (m Issues) Len() int      { return len(m) }
func (m Issues) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
func (m Issues) Less(i, j int) bool {
	return m[i].path < m[j].path || m[i].line < m[j].line || m[i].col < m[j].col
}

func debug(format string, args ...interface{}) {
	if *debugFlag {
		fmt.Fprintf(os.Stderr, "DEBUG: "+format+"\n", args...)
	}
}

func formatLinters() string {
	w := bytes.NewBuffer(nil)
	for command, description := range lintersFlag {
		linter := Linter(description)
		fmt.Fprintf(w, "    %s -> %s -> %s\n", command, linter.Command(), linter.Pattern())
	}
	return w.String()
}

func formatSeverity() string {
	w := bytes.NewBuffer(nil)
	for name, severity := range linterSeverityFlag {
		fmt.Fprintf(w, "    %s -> %s\n", name, severity)
	}
	return w.String()
}

func exArgs() (arg0 string, arg1 string) {
	if runtime.GOOS == "windows" {
		arg0 = "cmd"
		arg1 = "/C"
	} else {
		arg0 = "/bin/sh"
		arg1 = "-c"
	}
	return
}

func main() {
	kingpin.CommandLine.Help = fmt.Sprintf(`Aggregate and normalise the output of a whole bunch of Go linters.

Default linters:

%s

Severity override map (default is "error"):

%s
`, formatLinters(), formatSeverity())
	kingpin.Parse()
	var filter *regexp.Regexp
	if *excludeFlag != "" {
		filter = regexp.MustCompile(*excludeFlag)
	}

	if *fastFlag {
		*disableLintersFlag = append(*disableLintersFlag, "structcheck", "varcheck", "errcheck")
	}

	if *installFlag {
		for name, target := range installMap {
			cmd := "go get"
			if *debugFlag {
				cmd += " -v"
			}
			if *updateFlag {
				cmd += " -u"
			}
			cmd += " " + target
			fmt.Printf("Installing %s -> %s\n", name, cmd)
			arg0, arg1 := exArgs()
			c := exec.Command(arg0, arg1, cmd)
			c.Stdout = os.Stdout
			c.Stderr = os.Stderr
			err := c.Run()
			if err != nil {
				kingpin.CommandLine.Errorf(os.Stderr, "failed to install %s: %s", name, err)
			}
		}
		return
	}

	runtime.GOMAXPROCS(*concurrencyFlag)

	disable := map[string]bool{}
	for _, linter := range *disableLintersFlag {
		disable[linter] = true
	}

	start := time.Now()
	paths := *pathArg
	concurrency := make(chan bool, *concurrencyFlag)
	issues := make(chan *Issue, 100000)
	wg := &sync.WaitGroup{}
	for name, description := range lintersFlag {
		if _, ok := disable[name]; ok {
			debug("linter %s disabled", name)
			continue
		}
		parts := strings.SplitN(description, ":", 2)
		command := parts[0]
		pattern := parts[1]

		wg.Add(1)
		go func(name, command, pattern string) {
			concurrency <- true
			executeLinter(issues, name, command, pattern, paths)
			<-concurrency
			wg.Done()
		}(name, command, pattern)
	}

	wg.Wait()
	close(issues)
	for issue := range issues {
		if filter != nil && filter.MatchString(issue.String()) {
			continue
		}
		fmt.Printf("%s\n", issue)
	}
	elapsed := time.Now().Sub(start)
	debug("total elapsed time %s", elapsed)
}

func executeLinter(issues chan *Issue, name, command, pattern, paths string) {
	debug("linting with %s: %s", name, command)

	start := time.Now()
	if p, ok := predefinedPatterns[pattern]; ok {
		pattern = p
	}
	regexp.Compile(pattern)
	re, err := regexp.Compile(pattern)
	kingpin.FatalIfError(err, "invalid pattern for '"+command+"'")

	command = strings.Replace(command, "{path}", paths, -1)
	debug("executing %s", command)
	arg0, arg1 := exArgs()
	cmd := exec.Command(arg0, arg1, command)
	out, err := cmd.CombinedOutput()
	if err != nil {
		if _, ok := err.(*exec.ExitError); !ok {
			debug("warning: %s failed: %s", command, err)
			return
		}
		debug("warning: %s returned %s", command, err)
	}

	for _, line := range bytes.Split(out, []byte("\n")) {
		groups := re.FindAllSubmatch(line, -1)
		if groups == nil {
			debug("%s (didn't match): '%s'", name, line)
			continue
		}
		issue := &Issue{}
		for i, name := range re.SubexpNames() {
			part := string(groups[0][i])
			switch name {
			case "path":
				issue.path = part

			case "line":
				n, err := strconv.ParseInt(part, 10, 32)
				kingpin.FatalIfError(err, "line matched invalid integer")
				issue.line = int(n)

			case "col":
				n, err := strconv.ParseInt(part, 10, 32)
				kingpin.FatalIfError(err, "col matched invalid integer")
				issue.col = int(n)

			case "message":
				issue.message = part

			case "":

			default:
				kingpin.Fatalf("invalid subgroup %s", name)
			}
		}
		if m, ok := linterMessageOverrideFlag[name]; ok {
			issue.message = strings.Replace(m, "{message}", issue.message, -1)
		}
		if sev, ok := linterSeverityFlag[name]; ok {
			issue.severity = Severity(sev)
		} else {
			issue.severity = "error"
		}
		issues <- issue
	}

	elapsed := time.Now().Sub(start)
	debug("%s linter took %s", name, elapsed)
}
<commit_msg>Add gocyclo support.<commit_after>package main

import (
	"bytes"
	"fmt"
	"os"
	"os/exec"
	"regexp"
	"runtime"
	"strconv"
	"strings"
	"sync"
	"time"

	"github.com/alecthomas/kingpin"
)

type Severity string

// Linter message severity levels.
const (
	Warning Severity = "warning"
	Error   Severity = "error"
)

type Linter string

func (l Linter) Command() string {
	return string(l[0:strings.Index(string(l), ":")])
}

func (l Linter) Pattern() string {
	return string(l[strings.Index(string(l), ":"):])
}

var (
	predefinedPatterns = map[string]string{
		"PATH:LINE:COL:MESSAGE": `(?P<path>[^:]+):(?P<line>\d+):(?P<col>\d+):\s*(?P<message>.*)`,
		"PATH:LINE:MESSAGE":     `(?P<path>[^:]+):(?P<line>\d+):\s*(?P<message>.*)`,
	}
	lintersFlag = map[string]string{
		// main.go:8:10: should omit type map[string]string from declaration of var linters; it will be inferred from the right-hand side
		"golint": "golint {path}:PATH:LINE:COL:MESSAGE",
		// test/stutter.go:19: missing argument for Printf("%d"): format reads arg 1, have only 0 args
		"vet":         "go vet {path}:PATH:LINE:MESSAGE",
		"gotype":      "gotype {path}:PATH:LINE:COL:MESSAGE",
		"errcheck":    `errcheck {path}:(?P<path>[^:]+):(?P<line>\d+):(?P<col>\d+)\t(?P<message>.*)`,
		"varcheck":    "varcheck {path}:PATH:LINE:MESSAGE",
		"structcheck": "structcheck {path}:PATH:LINE:MESSAGE",
		"defercheck":  "defercheck {path}:PATH:LINE:MESSAGE",
		"deadcode":    `deadcode {path}:deadcode: (?P<path>[^:]+):(?P<line>\d+):(?P<col>\d+):\s*(?P<message>.*)`,
		"gocyclo":     `gocyclo -over {mincyclo} {path}:(?P<cyclo>\d+)\s+\S+\s\S+\s+(?P<path>[^:]+):(?P<line>\d+):(?P<col>\d+)`,
	}
	linterMessageOverrideFlag = map[string]string{
		"errcheck":    "error return value not checked ({message})",
		"varcheck":    "unused global variable {message}",
		"structcheck": "unused struct field {message}",
		"gocyclo":     "cyclomatic complexity {cyclo} is high (> {mincyclo})",
	}
	linterSeverityFlag = map[string]string{
		"errcheck":    "warning",
		"golint":      "warning",
		"varcheck":    "warning",
		"structcheck": "warning",
		"deadcode":    "warning",
	}
	installMap = map[string]string{
		"golint":      "github.com/golang/lint/golint",
		"gotype":      "golang.org/x/tools/cmd/gotype",
		"errcheck":    "github.com/kisielk/errcheck",
		"defercheck":  "github.com/opennota/check/cmd/defercheck",
		"varcheck":    "github.com/opennota/check/cmd/varcheck",
		"structcheck": "github.com/opennota/check/cmd/structcheck",
		"vet":         "golang.org/x/tools/cmd/vet",
		"deadcode":    "github.com/remyoudompheng/go-misc/deadcode",
		"gocyclo":     "github.com/fzipp/gocyclo",
	}
	slowLinters = []string{"structcheck", "varcheck", "errcheck"}

	pathArg            = kingpin.Arg("path", "Directory to lint.").Default(".").String()
	fastFlag           = kingpin.Flag("fast", "Only run fast linters.").Bool()
	installFlag        = kingpin.Flag("install", "Attempt to install all known linters.").Short('i').Bool()
	updateFlag         = kingpin.Flag("update", "Pass -u to go tool when installing.").Short('u').Bool()
	disableLintersFlag = kingpin.Flag("disable", "List of linters to disable.").PlaceHolder("LINTER").Short('D').Strings()
	debugFlag          = kingpin.Flag("debug", "Display messages for failed linters, etc.").Short('d').Bool()
	concurrencyFlag    = kingpin.Flag("concurrency", "Number of concurrent linters to run.").Default("16").Short('j').Int()
	excludeFlag        = kingpin.Flag("exclude", "Exclude messages matching this regular expression.").PlaceHolder("REGEXP").String()
	cycloFlag          = kingpin.Flag("cyclo-over", "Report functions with cyclomatic complexity over N (using gocyclo).").Default("10").String()
)

func init() {
	kingpin.Flag("linter", "Specify a linter.").PlaceHolder("NAME:COMMAND:PATTERN").StringMapVar(&lintersFlag)
	kingpin.Flag("message-overrides", "Override message from linter. {message} will be expanded to the original message.").PlaceHolder("LINTER:MESSAGE").StringMapVar(&linterMessageOverrideFlag)
	kingpin.Flag("severity", "Map of linter severities.").PlaceHolder("LINTER:SEVERITY").StringMapVar(&linterSeverityFlag)
}

type Issue struct {
	severity Severity
	path     string
	line     int
	col      int
	message  string
}

func (m *Issue) String() string {
	col := ""
	if m.col != 0 {
		col = fmt.Sprintf("%d", m.col)
	}
	return fmt.Sprintf("%s:%d:%s:%s: %s", m.path, m.line, col, m.severity, m.message)
}

type Issues []*Issue

func (m Issues) Len() int      { return len(m) }
func (m Issues) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
func (m Issues) Less(i, j int) bool {
	return m[i].path < m[j].path || m[i].line < m[j].line || m[i].col < m[j].col
}

func debug(format string, args ...interface{}) {
	if *debugFlag {
		fmt.Fprintf(os.Stderr, "DEBUG: "+format+"\n", args...)
	}
}

func formatLinters() string {
	w := bytes.NewBuffer(nil)
	for command, description := range lintersFlag {
		linter := Linter(description)
		fmt.Fprintf(w, "    %s -> %s -> %s\n", command, linter.Command(), linter.Pattern())
	}
	return w.String()
}

func formatSeverity() string {
	w := bytes.NewBuffer(nil)
	for name, severity := range linterSeverityFlag {
		fmt.Fprintf(w, "    %s -> %s\n", name, severity)
	}
	return w.String()
}

func exArgs() (arg0 string, arg1 string) {
	if runtime.GOOS == "windows" {
		arg0 = "cmd"
		arg1 = "/C"
	} else {
		arg0 = "/bin/sh"
		arg1 = "-c"
	}
	return
}

type Vars map[string]string

func (v Vars) Replace(s string) string {
	for k, v := range v {
		s = strings.Replace(s, fmt.Sprintf("{%s}", k), v, -1)
	}
	return s
}

func main() {
	kingpin.CommandLine.Help = fmt.Sprintf(`Aggregate and normalise the output of a whole bunch of Go linters.

Default linters:

%s

Severity override map (default is "error"):

%s
`, formatLinters(), formatSeverity())
	kingpin.Parse()
	var filter *regexp.Regexp
	if *excludeFlag != "" {
		filter = regexp.MustCompile(*excludeFlag)
	}

	if *fastFlag {
		*disableLintersFlag = append(*disableLintersFlag, slowLinters...)
	}

	if *installFlag {
		for name, target := range installMap {
			cmd := "go get"
			if *debugFlag {
				cmd += " -v"
			}
			if *updateFlag {
				cmd += " -u"
			}
			cmd += " " + target
			fmt.Printf("Installing %s -> %s\n", name, cmd)
			arg0, arg1 := exArgs()
			c := exec.Command(arg0, arg1, cmd)
			c.Stdout = os.Stdout
			c.Stderr = os.Stderr
			err := c.Run()
			if err != nil {
				kingpin.CommandLine.Errorf(os.Stderr, "failed to install %s: %s", name, err)
			}
		}
		return
	}

	runtime.GOMAXPROCS(*concurrencyFlag)

	disable := map[string]bool{}
	for _, linter := range *disableLintersFlag {
		disable[linter] = true
	}

	start := time.Now()
	paths := *pathArg
	concurrency := make(chan bool, *concurrencyFlag)
	issues := make(chan *Issue, 100000)
	wg := &sync.WaitGroup{}
	for name, description := range lintersFlag {
		if _, ok := disable[name]; ok {
			debug("linter %s disabled", name)
			continue
		}
		parts := strings.SplitN(description, ":", 2)
		command := parts[0]
		pattern := parts[1]

		wg.Add(1)
		vars := Vars{
			"mincyclo": *cycloFlag,
		}
		go func(name, command, pattern string) {
			concurrency <- true
			executeLinter(issues, name, command, pattern, paths, vars)
			<-concurrency
			wg.Done()
		}(name, command, pattern)
	}

	wg.Wait()
	close(issues)
	for issue := range issues {
		if filter != nil && filter.MatchString(issue.String()) {
			continue
		}
		fmt.Printf("%s\n", issue)
	}
	elapsed := time.Now().Sub(start)
	debug("total elapsed time %s", elapsed)
}

func executeLinter(issues chan *Issue, name, command, pattern, paths string, vars Vars) {
	debug("linting with %s: %s", name, command)

	start := time.Now()
	if p, ok := predefinedPatterns[pattern]; ok {
		pattern = p
	}
	regexp.Compile(pattern)
	re, err := regexp.Compile(pattern)
	kingpin.FatalIfError(err, "invalid pattern for '"+command+"'")

	vars["path"] = paths
	command = vars.Replace(command)
	debug("executing %s", command)
	arg0, arg1 := exArgs()
	cmd := exec.Command(arg0, arg1, command)
	out, err := cmd.CombinedOutput()
	if err != nil {
		if _, ok := err.(*exec.ExitError); !ok {
			debug("warning: %s failed: %s", command, err)
			return
		}
		debug("warning: %s returned %s", command, err)
	}

	for _, line := range bytes.Split(out, []byte("\n")) {
		groups := re.FindAllSubmatch(line, -1)
		if groups == nil {
			debug("%s (didn't match): '%s'", name, line)
			continue
		}
		issue := &Issue{}
		for i, name := range re.SubexpNames() {
			part := string(groups[0][i])
			if name != "" {
				vars[name] = part
			}
			switch name {
			case "path":
				issue.path = part

			case "line":
				n, err := strconv.ParseInt(part, 10, 32)
				kingpin.FatalIfError(err, "line matched invalid integer")
				issue.line = int(n)

			case "col":
				n, err := strconv.ParseInt(part, 10, 32)
				kingpin.FatalIfError(err, "col matched invalid integer")
				issue.col = int(n)

			case "message":
				issue.message = part

			case "":
			}
		}
		if m, ok := linterMessageOverrideFlag[name]; ok {
			issue.message = vars.Replace(m)
		}
		if sev, ok := linterSeverityFlag[name]; ok {
			issue.severity = Severity(sev)
		} else {
			issue.severity = "error"
		}
		issues <- issue
	}

	elapsed := time.Now().Sub(start)
	debug("%s linter took %s", name, elapsed)
}
<|endoftext|>


<commit_before>package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"strconv"

	"github.com/stripe/stripe-go"
	"github.com/teamwork/stripehook/events"
)

var (
	port int
	path string
)

func main() {

	flag.IntVar(&port, "port", 8080, "port number to bind the server to e.g. '8080' would be http://localhost:8080/<path>")
	flag.StringVar(&path, "path", "stripe", "the endpoint to recieve webhooks on e.g. 'stripe' would be http://localhost:<port>/stripe")
	flag.Parse()

	events.RegisterValidationHandler(events.VerifyEventWithStripe)
	events.RegisterEventHandlers()

	http.HandleFunc("/"+path, stripeHandler)

	fmt.Println("Listening...")
	log.Fatal(http.ListenAndServe(":"+strconv.Itoa(port), nil))
}

// stripeHandler is responsible for receiving the stripe webhook
// and then taking that request through verification and event handlers
// and return 200 OK to Stripe or some other HTTP response code in the event
// of an error. Note: Stripe will retry any events returned outside of 2XX.
func stripeHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		http.Error(w, "route only supports POST", http.StatusBadRequest)
		return
	}

	hook, err := parseBody(r.Body)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	handler, ok := events.EventHandlers[hook.Type]
	if !ok {
		fmt.Fprintf(w, "No handler for this event %s", hook.Type)
		return
	}

	event, err := events.ValidationHandler(hook)
	if err != nil {
		http.Error(w, fmt.Sprintf("Verifying with stripe failed for event %s", hook.ID), http.StatusInternalServerError)
		return
	}

	err = handler(event)
	if err != nil {
		http.Error(w, "Something went wrong", http.StatusInternalServerError)
		return
	}

	fmt.Fprintf(w, "Event handled successfully %s", event.ID)
	return
}

// parseBody unmarshals the payload from Stripe to our StripeWebhook struct
func parseBody(body io.Reader) (event *stripe.Event, err error) {
	b, err := ioutil.ReadAll(body)
	if err != nil {
		return
	}
	err = json.Unmarshal(b, &event)
	return event, err
}
<commit_msg>Fix path<commit_after>package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"strconv"

	"github.com/stripe/stripe-go"
	"github.com/teamwork/stripehooks/events"
)

var (
	port int
	path string
)

func main() {

	flag.IntVar(&port, "port", 8080, "port number to bind the server to e.g. '8080' would be http://localhost:8080/<path>")
	flag.StringVar(&path, "path", "stripe", "the endpoint to recieve webhooks on e.g. 'stripe' would be http://localhost:<port>/stripe")
	flag.Parse()

	events.RegisterValidationHandler(events.VerifyEventWithStripe)
	events.RegisterEventHandlers()

	http.HandleFunc("/"+path, stripeHandler)

	fmt.Println("Listening...")
	log.Fatal(http.ListenAndServe(":"+strconv.Itoa(port), nil))
}

// stripeHandler is responsible for receiving the stripe webhook
// and then taking that request through verification and event handlers
// and return 200 OK to Stripe or some other HTTP response code in the event
// of an error. Note: Stripe will retry any events returned outside of 2XX.
func stripeHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		http.Error(w, "route only supports POST", http.StatusBadRequest)
		return
	}

	hook, err := parseBody(r.Body)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	handler, ok := events.EventHandlers[hook.Type]
	if !ok {
		fmt.Fprintf(w, "No handler for this event %s", hook.Type)
		return
	}

	event, err := events.ValidationHandler(hook)
	if err != nil {
		http.Error(w, fmt.Sprintf("Verifying with stripe failed for event %s", hook.ID), http.StatusInternalServerError)
		return
	}

	err = handler(event)
	if err != nil {
		http.Error(w, "Something went wrong", http.StatusInternalServerError)
		return
	}

	fmt.Fprintf(w, "Event handled successfully %s", event.ID)
	return
}

// parseBody unmarshals the payload from Stripe to our StripeWebhook struct
func parseBody(body io.Reader) (event *stripe.Event, err error) {
	b, err := ioutil.ReadAll(body)
	if err != nil {
		return
	}
	err = json.Unmarshal(b, &event)
	return event, err
}
<|endoftext|>


<commit_before>package luar // import "layeh.com/gopher-luar"

import (
	"reflect"

	"github.com/yuin/gopher-lua"
)

// New creates and returns a new lua.LValue for the given value.
//
// The following table shows how Go types are converted to Lua types:
//  Kind            gopher-lua Type  Custom Metatable
//  --------------------------------------------------
//  nil             LNil             No
//  Bool            LBool            No
//  Int             LNumber          No
//  Int8            LNumber          No
//  Int16           LNumber          No
//  Int32           LNumber          No
//  Int64           LNumber          No
//  Uint            LNumber          No
//  Uint8           LNumber          No
//  Uint16          LNumber          No
//  Uint32          LNumber          No
//  Uint64          LNumber          No
//  Uintptr         *LUserData       No
//  Float32         LNumber          No
//  Float64         LNumber          No
//  Complex64       *LUserData       No
//  Complex128      *LUserData       No
//  Array           *LUserData       Yes
//  Chan            *LUserData       Yes
//  Func            *lua.LFunction   No
//  Interface       *LUserData       No
//  Map             *LUserData       Yes
//  Ptr             *LUserData       Yes
//  Slice           *LUserData       Yes
//  String          LString          No
//  Struct          *LUserData       Yes
//  UnsafePointer   *LUserData       No
func New(L *lua.LState, value interface{}) lua.LValue {
	if value == nil {
		return lua.LNil
	}
	if lval, ok := value.(lua.LValue); ok {
		return lval
	}

	val := reflect.ValueOf(value)
	switch val.Kind() {
	case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
		if val.IsNil() {
			return lua.LNil
		}
	}

	switch val.Kind() {
	case reflect.Bool:
		return lua.LBool(val.Bool())
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		return lua.LNumber(float64(val.Int()))
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		return lua.LNumber(float64(val.Uint()))
	case reflect.Float32, reflect.Float64:
		return lua.LNumber(val.Float())
	case reflect.Array, reflect.Chan, reflect.Map, reflect.Ptr, reflect.Slice, reflect.Struct:
		ud := L.NewUserData()
		ud.Value = val.Interface()
		ud.Metatable = getMetatableFromValue(L, val)
		return ud
	case reflect.Func:
		return funcWrapper(L, val, false)
	case reflect.Interface:
		ud := L.NewUserData()
		ud.Value = val.Interface()
		return ud
	case reflect.String:
		return lua.LString(val.String())
	default:
		ud := L.NewUserData()
		ud.Value = val.Interface()
		return ud
	}
}

// NewType returns a new type creator for the given value's type.
//
// When the lua.LValue is called, a new value will be created that is the
// same type as value's type.
func NewType(L *lua.LState, value interface{}) lua.LValue {
	val := reflect.TypeOf(value)
	ud := L.NewUserData()
	ud.Value = val
	ud.Metatable = getTypeMetatable(L, val)

	return ud
}

func lValueToReflect(L *lua.LState, v lua.LValue, hint reflect.Type, tryConvertPtr *bool) reflect.Value {
	if hint.Implements(refTypeLuaLValue) {
		return reflect.ValueOf(v)
	}

	isPtr := false

	switch converted := v.(type) {
	case lua.LBool:
		var val reflect.Value
		if hint.Kind() == reflect.String {
			val = reflect.ValueOf(converted.String())
		} else {
			val = reflect.ValueOf(bool(converted))
		}
		return val.Convert(hint)
	case lua.LChannel:
		return reflect.ValueOf(converted).Convert(hint)
	case lua.LNumber:
		var val reflect.Value
		if hint.Kind() == reflect.String {
			val = reflect.ValueOf(converted.String())
		} else {
			val = reflect.ValueOf(converted)
		}
		return val.Convert(hint)
	case *lua.LFunction:
		fn := func(args []reflect.Value) []reflect.Value {
			L.Push(converted)

			varadicCount := 0

			for i, arg := range args {
				if hint.IsVariadic() && i+1 == len(args) {
					// arg is the varadic slice
					varadicCount = arg.Len()
					for j := 0; j < varadicCount; j++ {
						arg := arg.Index(j)
						if !arg.CanInterface() {
							L.Pop(i + j + 1)
							L.RaiseError("unable to Interface argument %d", i+j)
						}
						L.Push(New(L, arg.Interface()))
					}
					// recount for varadic slice that appeared
					varadicCount--
					break
				}

				if !arg.CanInterface() {
					L.Pop(i + 1)
					L.RaiseError("unable to Interface argument %d", i)
				}
				L.Push(New(L, arg.Interface()))
			}

			L.Call(len(args)+varadicCount, hint.NumOut())
			defer L.Pop(hint.NumOut())

			ret := make([]reflect.Value, hint.NumOut())

			for i := 0; i < hint.NumOut(); i++ {
				outHint := hint.Out(i)
				ret[i] = lValueToReflect(L, L.Get(-hint.NumOut()+i), outHint, nil)
			}

			return ret
		}
		return reflect.MakeFunc(hint, fn)
	case *lua.LNilType:
		switch hint.Kind() {
		case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice, reflect.UnsafePointer:
			return reflect.Zero(hint)
		default:
			L.RaiseError("cannot convert nil to %s", hint.String())
		}
	case *lua.LState:
		return reflect.ValueOf(converted).Convert(hint)
	case lua.LString:
		return reflect.ValueOf(string(converted)).Convert(hint)
	case *lua.LTable:
		switch {
		case hint.Kind() == reflect.Slice:
			elemType := hint.Elem()
			length := converted.Len()
			s := reflect.MakeSlice(hint, length, length)

			for i := 0; i < length; i++ {
				value := converted.RawGetInt(i + 1)
				elemValue := lValueToReflect(L, value, elemType, nil)
				s.Index(i).Set(elemValue)
			}

			return s

		case hint.Kind() == reflect.Map:
			keyType := hint.Key()
			elemType := hint.Elem()
			s := reflect.MakeMap(hint)

			converted.ForEach(func(key, value lua.LValue) {
				if _, ok := key.(lua.LString); !ok {
					return
				}

				lKey := lValueToReflect(L, key, keyType, nil)
				lValue := lValueToReflect(L, value, elemType, nil)
				s.SetMapIndex(lKey, lValue)
			})

			return s

		case hint.Kind() == reflect.Ptr && hint.Elem().Kind() == reflect.Struct:
			hint = hint.Elem()
			isPtr = true
			fallthrough
		case hint.Kind() == reflect.Struct:
			s := reflect.New(hint)
			t := s.Elem()

			mt := &Metatable{
				LTable: getMetatable(L, hint),
			}

			converted.ForEach(func(key, value lua.LValue) {
				if _, ok := key.(lua.LString); !ok {
					return
				}

				fieldName := key.String()
				index := mt.fieldIndex(fieldName)
				if index == nil {
					L.RaiseError("invalid field %s", fieldName)
				}
				field := hint.FieldByIndex(index)

				lValue := lValueToReflect(L, value, field.Type, nil)
				t.FieldByIndex(field.Index).Set(lValue)
			})

			if isPtr {
				return s
			}

			return t

		default:
			return reflect.ValueOf(converted).Convert(hint)
		}
	case *lua.LUserData:
		val := reflect.ValueOf(converted.Value)
		if tryConvertPtr != nil && val.Kind() != reflect.Ptr && hint.Kind() == reflect.Ptr && val.Type() == hint.Elem() {
			newVal := reflect.New(hint.Elem())
			newVal.Elem().Set(val)
			val = newVal
			*tryConvertPtr = true
		} else {
			val = val.Convert(hint)
			if tryConvertPtr != nil {
				*tryConvertPtr = false
			}
		}
		return val
	}
	L.RaiseError("fatal lValueToReflect error")
	return reflect.Value{} // never returns
}
<commit_msg>remove unused code<commit_after>package luar // import "layeh.com/gopher-luar"

import (
	"reflect"

	"github.com/yuin/gopher-lua"
)

// New creates and returns a new lua.LValue for the given value.
//
// The following table shows how Go types are converted to Lua types:
//  Kind            gopher-lua Type  Custom Metatable
//  --------------------------------------------------
//  nil             LNil             No
//  Bool            LBool            No
//  Int             LNumber          No
//  Int8            LNumber          No
//  Int16           LNumber          No
//  Int32           LNumber          No
//  Int64           LNumber          No
//  Uint            LNumber          No
//  Uint8           LNumber          No
//  Uint16          LNumber          No
//  Uint32          LNumber          No
//  Uint64          LNumber          No
//  Uintptr         *LUserData       No
//  Float32         LNumber          No
//  Float64         LNumber          No
//  Complex64       *LUserData       No
//  Complex128      *LUserData       No
//  Array           *LUserData       Yes
//  Chan            *LUserData       Yes
//  Func            *lua.LFunction   No
//  Map             *LUserData       Yes
//  Ptr             *LUserData       Yes
//  Slice           *LUserData       Yes
//  String          LString          No
//  Struct          *LUserData       Yes
//  UnsafePointer   *LUserData       No
func New(L *lua.LState, value interface{}) lua.LValue {
	if value == nil {
		return lua.LNil
	}
	if lval, ok := value.(lua.LValue); ok {
		return lval
	}

	val := reflect.ValueOf(value)
	switch val.Kind() {
	case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
		if val.IsNil() {
			return lua.LNil
		}
	}

	switch val.Kind() {
	case reflect.Bool:
		return lua.LBool(val.Bool())
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		return lua.LNumber(float64(val.Int()))
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		return lua.LNumber(float64(val.Uint()))
	case reflect.Float32, reflect.Float64:
		return lua.LNumber(val.Float())
	case reflect.Array, reflect.Chan, reflect.Map, reflect.Ptr, reflect.Slice, reflect.Struct:
		ud := L.NewUserData()
		ud.Value = val.Interface()
		ud.Metatable = getMetatableFromValue(L, val)
		return ud
	case reflect.Func:
		return funcWrapper(L, val, false)
	case reflect.String:
		return lua.LString(val.String())
	default:
		ud := L.NewUserData()
		ud.Value = val.Interface()
		return ud
	}
}

// NewType returns a new type creator for the given value's type.
//
// When the lua.LValue is called, a new value will be created that is the
// same type as value's type.
func NewType(L *lua.LState, value interface{}) lua.LValue {
	val := reflect.TypeOf(value)
	ud := L.NewUserData()
	ud.Value = val
	ud.Metatable = getTypeMetatable(L, val)

	return ud
}

func lValueToReflect(L *lua.LState, v lua.LValue, hint reflect.Type, tryConvertPtr *bool) reflect.Value {
	if hint.Implements(refTypeLuaLValue) {
		return reflect.ValueOf(v)
	}

	isPtr := false

	switch converted := v.(type) {
	case lua.LBool:
		var val reflect.Value
		if hint.Kind() == reflect.String {
			val = reflect.ValueOf(converted.String())
		} else {
			val = reflect.ValueOf(bool(converted))
		}
		return val.Convert(hint)
	case lua.LChannel:
		return reflect.ValueOf(converted).Convert(hint)
	case lua.LNumber:
		var val reflect.Value
		if hint.Kind() == reflect.String {
			val = reflect.ValueOf(converted.String())
		} else {
			val = reflect.ValueOf(converted)
		}
		return val.Convert(hint)
	case *lua.LFunction:
		fn := func(args []reflect.Value) []reflect.Value {
			L.Push(converted)

			varadicCount := 0

			for i, arg := range args {
				if hint.IsVariadic() && i+1 == len(args) {
					// arg is the varadic slice
					varadicCount = arg.Len()
					for j := 0; j < varadicCount; j++ {
						arg := arg.Index(j)
						if !arg.CanInterface() {
							L.Pop(i + j + 1)
							L.RaiseError("unable to Interface argument %d", i+j)
						}
						L.Push(New(L, arg.Interface()))
					}
					// recount for varadic slice that appeared
					varadicCount--
					break
				}

				if !arg.CanInterface() {
					L.Pop(i + 1)
					L.RaiseError("unable to Interface argument %d", i)
				}
				L.Push(New(L, arg.Interface()))
			}

			L.Call(len(args)+varadicCount, hint.NumOut())
			defer L.Pop(hint.NumOut())

			ret := make([]reflect.Value, hint.NumOut())

			for i := 0; i < hint.NumOut(); i++ {
				outHint := hint.Out(i)
				ret[i] = lValueToReflect(L, L.Get(-hint.NumOut()+i), outHint, nil)
			}

			return ret
		}
		return reflect.MakeFunc(hint, fn)
	case *lua.LNilType:
		switch hint.Kind() {
		case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice, reflect.UnsafePointer:
			return reflect.Zero(hint)
		default:
			L.RaiseError("cannot convert nil to %s", hint.String())
		}
	case *lua.LState:
		return reflect.ValueOf(converted).Convert(hint)
	case lua.LString:
		return reflect.ValueOf(string(converted)).Convert(hint)
	case *lua.LTable:
		switch {
		case hint.Kind() == reflect.Slice:
			elemType := hint.Elem()
			length := converted.Len()
			s := reflect.MakeSlice(hint, length, length)

			for i := 0; i < length; i++ {
				value := converted.RawGetInt(i + 1)
				elemValue := lValueToReflect(L, value, elemType, nil)
				s.Index(i).Set(elemValue)
			}

			return s

		case hint.Kind() == reflect.Map:
			keyType := hint.Key()
			elemType := hint.Elem()
			s := reflect.MakeMap(hint)

			converted.ForEach(func(key, value lua.LValue) {
				if _, ok := key.(lua.LString); !ok {
					return
				}

				lKey := lValueToReflect(L, key, keyType, nil)
				lValue := lValueToReflect(L, value, elemType, nil)
				s.SetMapIndex(lKey, lValue)
			})

			return s

		case hint.Kind() == reflect.Ptr && hint.Elem().Kind() == reflect.Struct:
			hint = hint.Elem()
			isPtr = true
			fallthrough
		case hint.Kind() == reflect.Struct:
			s := reflect.New(hint)
			t := s.Elem()

			mt := &Metatable{
				LTable: getMetatable(L, hint),
			}

			converted.ForEach(func(key, value lua.LValue) {
				if _, ok := key.(lua.LString); !ok {
					return
				}

				fieldName := key.String()
				index := mt.fieldIndex(fieldName)
				if index == nil {
					L.RaiseError("invalid field %s", fieldName)
				}
				field := hint.FieldByIndex(index)

				lValue := lValueToReflect(L, value, field.Type, nil)
				t.FieldByIndex(field.Index).Set(lValue)
			})

			if isPtr {
				return s
			}

			return t

		default:
			return reflect.ValueOf(converted).Convert(hint)
		}
	case *lua.LUserData:
		val := reflect.ValueOf(converted.Value)
		if tryConvertPtr != nil && val.Kind() != reflect.Ptr && hint.Kind() == reflect.Ptr && val.Type() == hint.Elem() {
			newVal := reflect.New(hint.Elem())
			newVal.Elem().Set(val)
			val = newVal
			*tryConvertPtr = true
		} else {
			val = val.Convert(hint)
			if tryConvertPtr != nil {
				*tryConvertPtr = false
			}
		}
		return val
	}
	L.RaiseError("fatal lValueToReflect error")
	return reflect.Value{} // never returns
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"github.com/captncraig/github-webhooks"
	"log"
	"net/http"
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
	"strings"
)

var scriptDir string
var scriptExt = ".sh"

func init() {
	scriptDir = os.Getenv("TINYCI-SCRIPT-DIR")

	if scriptDir == "" {
		var err error
		scriptDir, err = os.Getwd()
		if err != nil {
			log.Fatal(err)
		}
		scriptDir = filepath.Join(scriptDir, "scripts")
	}
	if runtime.GOOS == "windows" {
		scriptExt = ".bat"
	}
}

func main() {

	gitHooks := webhooks.WebhookListener{}
	gitHooks.OnPush = githubHook
	http.HandleFunc("/gh", gitHooks.GetHttpListener())
	http.ListenAndServe(":4567", nil)
}

func githubHook(event *webhooks.PushEvent, _ *webhooks.WebhookContext) {
	repo := strings.Replace(event.Repository.FullName, "/", ".", -1)
	refPath := strings.Split(event.Ref, "/")
	ref := refPath[len(refPath)-1]
	runScriptIfExists(fmt.Sprintf("%s", repo))
	runScriptIfExists(fmt.Sprintf("%s:%s", repo, ref))
}

func runScriptIfExists(name string) {
	filename := filepath.Join(scriptDir, name+scriptExt)
	if _, err := os.Stat(filename); os.IsNotExist(err) {
		fmt.Println("Script does not exist: %s. Skipping\n", filename)
		return
	}
	cmd := exec.Command(filename)
	output, err := cmd.CombinedOutput()
	fmt.Println(output, err)
}
<commit_msg>f instead on ln<commit_after>package main

import (
	"fmt"
	"github.com/captncraig/github-webhooks"
	"log"
	"net/http"
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
	"strings"
)

var scriptDir string
var scriptExt = ".sh"

func init() {
	scriptDir = os.Getenv("TINYCI-SCRIPT-DIR")

	if scriptDir == "" {
		var err error
		scriptDir, err = os.Getwd()
		if err != nil {
			log.Fatal(err)
		}
		scriptDir = filepath.Join(scriptDir, "scripts")
	}
	if runtime.GOOS == "windows" {
		scriptExt = ".bat"
	}
}

func main() {

	gitHooks := webhooks.WebhookListener{}
	gitHooks.OnPush = githubHook
	http.HandleFunc("/gh", gitHooks.GetHttpListener())
	http.ListenAndServe(":4567", nil)
}

func githubHook(event *webhooks.PushEvent, _ *webhooks.WebhookContext) {
	repo := strings.Replace(event.Repository.FullName, "/", ".", -1)
	refPath := strings.Split(event.Ref, "/")
	ref := refPath[len(refPath)-1]
	runScriptIfExists(fmt.Sprintf("%s", repo))
	runScriptIfExists(fmt.Sprintf("%s:%s", repo, ref))
}

func runScriptIfExists(name string) {
	filename := filepath.Join(scriptDir, name+scriptExt)
	if _, err := os.Stat(filename); os.IsNotExist(err) {
		fmt.Printf("Script does not exist: %s. Skipping.\n", filename)
		return
	}
	cmd := exec.Command(filename)
	output, err := cmd.CombinedOutput()
	fmt.Println(output, err)
}
<|endoftext|>


<commit_before>package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func readFile(filename string) {
	f, err := os.Open(filename)
	if err != nil {
		fmt.Fprintln(os.Stderr, "Error: ", err)
		return
	}
	reader := NewReader(bufio.NewReader(f))
	for {
		_, err := TryRead(reader)
		switch {
		case err == io.EOF:
			return
		case err != nil:
			fmt.Fprintln(os.Stderr, "Error: ", err)
			return
		}
	}
}

func repl() {
	fmt.Println("Welcome to gclojure. Use ctrl-c to exit.")
	reader := NewReader(bufio.NewReader(os.Stdin))
	for {
		fmt.Print("> ")
		obj, err := TryRead(reader)
		switch {
		case err == io.EOF:
			return
		case err != nil:
			fmt.Fprintln(os.Stderr, "Error: ", err)
		default:
			fmt.Println(obj.ToString(false))
		}
	}
}

func main() {
	if len(os.Args) > 1 {
		readFile(os.Args[1])
	} else {
		repl()
	}
}
<commit_msg>Better error handling in repl<commit_after>package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func readFile(filename string) {
	f, err := os.Open(filename)
	if err != nil {
		fmt.Fprintln(os.Stderr, "Error: ", err)
		return
	}
	reader := NewReader(bufio.NewReader(f))
	for {
		_, err := TryRead(reader)
		switch {
		case err == io.EOF:
			return
		case err != nil:
			fmt.Fprintln(os.Stderr, "Error: ", err)
			return
		}
	}
}

func skipRestOfLine(reader *Reader) {
	for {
		switch reader.Get() {
		case EOF, '\n':
			return
		}
	}
}

func repl() {
	fmt.Println("Welcome to gclojure. Use ctrl-c to exit.")
	reader := NewReader(bufio.NewReader(os.Stdin))
	for {
		fmt.Print("> ")
		obj, err := TryRead(reader)
		switch {
		case err == io.EOF:
			return
		case err != nil:
			fmt.Fprintln(os.Stderr, "Error: ", err)
			skipRestOfLine(reader)
		default:
			fmt.Println(obj.ToString(false))
		}
	}
}

func main() {
	if len(os.Args) > 1 {
		readFile(os.Args[1])
	} else {
		repl()
	}
}
<|endoftext|>


<commit_before>// Package cqueue implement circular buffer/queue operations
package cqueue

import (
  "container/list"
  "fmt"
)

// CQueue represents a circular buffer data structure
// TODO: overflow middleware function also
type CQueue struct {
  // size limit of the buffer
  size int
  // occupied size of the buffer
  Length int

  // list containing all the buffer elements
  Queue *list.List
  // element at the front-side of the buffer
  Head *list.Element
  // element at the back-side of the buffer
  Tail *list.Element
}

// New function returns a CQueue instance of custom size
func New(size int) *CQueue {
  return &CQueue{size: size, Queue: list.New()}
}

// Push adds an element to the circular buffer
func (cq *CQueue) Push(element interface{}) {
  if (cq.Length < cq.size) {
    cq.Tail = cq.Queue.PushBack(element)
    if (cq.Length == 0) {
      cq.Head = cq.Tail
    }

    cq.updateLength(1)
  } else {
    fmt.Println("full buffer")
    cq.Pop()
    cq.Push(element)
  }
}

// Pop removes the element at the front in circular buffer
func (cq *CQueue) Pop() {
  if (cq.Length > 0) {
    head := cq.Head.Next()
    cq.Queue.Remove(cq.Head)
    cq.Head = head

    cq.updateLength(-1)
  } else {
    fmt.Println("empty buffer")
  }
}

// Whether the circular buffer is full or not
func (cq *CQueue) isFull() bool {
  return (cq.Length == cq.size)
}

// Whether the circular buffer is empty or not
func (cq *CQueue) isEmpty() bool {
  return (cq.Length == 0)
}

// Updates the occupied length of the circular buffer
func (cq *CQueue) updateLength(update int) {
  cq.Length += update
}
<commit_msg>Add errors and Value method for CQueue<commit_after>// Package cqueue implement circular buffer/queue operations
package cqueue

import (
  "container/list"
  "errors"
)

// CQueue represents a circular buffer data structure
// TODO: overflow middleware function also
type CQueue struct {
  // size limit of the buffer
  size int
  // occupied size of the buffer
  Length int

  // list containing all the buffer elements
  Queue *list.List
  // element at the front-side of the buffer
  Head *list.Element
  // element at the back-side of the buffer
  Tail *list.Element
}

// New function returns a CQueue instance of custom size
func New(size int) *CQueue {
  return &CQueue{size: size, Queue: list.New()}
}

// Push adds an element to the circular buffer
func (cq *CQueue) Push(element interface{}) (el *list.Element, err error) {
  if (cq.Length < cq.size) {
    cq.Tail = cq.Queue.PushBack(element)
    if (cq.Length == 0) {
      cq.Head = cq.Tail
    }

    cq.updateLength(1)
  } else {
    cq.Pop()
    cq.Push(element)
  }

  return cq.Tail, nil
}

// Pop removes the element at the front in circular buffer
// It returns the popped element and error, if any.
func (cq *CQueue) Pop() (el *list.Element, err error) {
  if (cq.Length > 0) {
    el, err = cq.Head, nil
    newHead := cq.Head.Next()

    cq.Queue.Remove(cq.Head)
    cq.Head = newHead
    if (cq.Length == 1) {
      cq.Tail = nil
    }

    cq.updateLength(-1)
  } else {
    el, err = nil, errors.New("cqueue: buffer is empty")
  }

  return el, err
}

// Value returns the value of a list element
func (cq *CQueue) Value(element *list.Element) interface{} {
  if (element == nil) {
    return nil
  }
  return element.Value
}

// Whether the circular buffer is full or not
func (cq *CQueue) isFull() bool {
  return (cq.Length == cq.size)
}

// Whether the circular buffer is empty or not
func (cq *CQueue) isEmpty() bool {
  return (cq.Length == 0)
}

// Updates the occupied length of the circular buffer
func (cq *CQueue) updateLength(update int) {
  cq.Length += update
}
<|endoftext|>


<commit_before>// Copyright 2015 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.

// Compilebench benchmarks the speed of the Go compiler.
//
// Usage:
//
//	compilebench [options]
//
// It times the compilation of various packages and prints results in
// the format used by package testing (and expected by rsc.io/benchstat).
//
// The options are:
//
//	-alloc
//		Report allocations.
//
//	-compile exe
//		Use exe as the path to the cmd/compile binary.
//
//	-compileflags 'list'
//		Pass the space-separated list of flags to the compilation.
//
//	-count n
//		Run each benchmark n times (default 1).
//
//	-cpuprofile file
//		Write a CPU profile of the compiler to file.
//
//	-memprofile file
//		Write a memory profile of the compiler to file.
//
//	-memprofilerate rate
//		Set runtime.MemProfileRate during compilation.
//
//	-run regexp
//		Only run benchmarks with names matching regexp.
//
// Although -cpuprofile and -memprofile are intended to write a
// combined profile for all the executed benchmarks to file,
// today they write only the profile for the last benchmark executed.
//
// The default memory profiling rate is one profile sample per 512 kB
// allocated (see ``go doc runtime.MemProfileRate'').
// Lowering the rate (for example, -memprofilerate 64000) produces
// a more fine-grained and therefore accurate profile, but it also incurs
// execution cost. For benchmark comparisons, never use timings
// obtained with a low -memprofilerate option.
//
// Example
//
// Assuming the base version of the compiler has been saved with
// ``toolstash save,'' this sequence compares the old and new compiler:
//
//	compilebench -count 10 -compile $(toolstash -n compile) >old.txt
//	compilebench -count 10 >new.txt
//	benchstat old.txt new.txt
//
package main

import (
	"flag"
	"fmt"
	"go/build"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"regexp"
	"runtime"
	"strconv"
	"strings"
	"time"
)

var (
	goroot   = runtime.GOROOT()
	compiler string
	runRE    *regexp.Regexp
	is6g     bool
)

var (
	flagAlloc          = flag.Bool("alloc", false, "report allocations")
	flagCompiler       = flag.String("compile", "", "use `exe` as the cmd/compile binary")
	flagCompilerFlags  = flag.String("compileflags", "", "additional `flags` to pass to compile")
	flagRun            = flag.String("run", "", "run benchmarks matching `regexp`")
	flagCount          = flag.Int("count", 1, "run benchmarks `n` times")
	flagCpuprofile     = flag.String("cpuprofile", "", "write CPU profile to `file`")
	flagMemprofile     = flag.String("memprofile", "", "write memory profile to `file`")
	flagMemprofilerate = flag.Int64("memprofilerate", -1, "set memory profile `rate`")
)

var tests = []struct {
	name string
	dir  string
}{
	{"BenchmarkTemplate", "html/template"},
	{"BenchmarkGoTypes", "go/types"},
	{"BenchmarkCompiler", "cmd/compile/internal/gc"},
	{"BenchmarkMakeBash", ""},
	{"BenchmarkHelloSize", ""},
	{"BenchmarkCmdGoSize", ""},
}

func usage() {
	fmt.Fprintf(os.Stderr, "usage: compilebench [options]\n")
	fmt.Fprintf(os.Stderr, "options:\n")
	flag.PrintDefaults()
	os.Exit(2)
}

func main() {
	log.SetFlags(0)
	log.SetPrefix("compilebench: ")
	flag.Usage = usage
	flag.Parse()
	if flag.NArg() != 0 {
		usage()
	}

	compiler = *flagCompiler
	if compiler == "" {
		out, err := exec.Command("go", "tool", "-n", "compile").CombinedOutput()
		if err != nil {
			out, err = exec.Command("go", "tool", "-n", "6g").CombinedOutput()
			is6g = true
			if err != nil {
				out, err = exec.Command("go", "tool", "-n", "compile").CombinedOutput()
				log.Fatalf("go tool -n compiler: %v\n%s", err, out)
			}
		}
		compiler = strings.TrimSpace(string(out))
	}

	if *flagRun != "" {
		r, err := regexp.Compile(*flagRun)
		if err != nil {
			log.Fatalf("invalid -run argument: %v", err)
		}
		runRE = r
	}

	for i := 0; i < *flagCount; i++ {
		for _, tt := range tests {
			if runRE == nil || runRE.MatchString(tt.name) {
				runBuild(tt.name, tt.dir)
			}
		}
	}
}

func runCmd(name string, cmd *exec.Cmd) {
	start := time.Now()
	out, err := cmd.CombinedOutput()
	if err != nil {
		log.Printf("%v: %v\n%s", name, err, out)
		return
	}
	fmt.Printf("%s 1 %d ns/op\n", name, time.Since(start).Nanoseconds())
}

func runMakeBash() {
	cmd := exec.Command("./make.bash")
	cmd.Dir = filepath.Join(runtime.GOROOT(), "src")
	runCmd("BenchmarkMakeBash", cmd)
}

func runCmdGoSize() {
	runSize("BenchmarkCmdGoSize", filepath.Join(runtime.GOROOT(), "bin/go"))
}

func runHelloSize() {
	cmd := exec.Command("go", "build", "-o", "_hello_", filepath.Join(runtime.GOROOT(), "test/helloworld.go"))
	cmd.Stdout = os.Stderr
	cmd.Stderr = os.Stderr
	if err := cmd.Run(); err != nil {
		log.Print(err)
		return
	}
	runSize("BenchmarkHelloSize", "_hello_")
}

func runSize(name, file string) {
	info, err := os.Stat(file)
	if err != nil {
		log.Print(err)
		return
	}
	out, err := exec.Command("size", file).CombinedOutput()
	if err != nil {
		log.Printf("size: %v\n%s", err, out)
		return
	}
	lines := strings.Split(string(out), "\n")
	if len(lines) < 2 {
		log.Printf("not enough output from size: %s", out)
		return
	}
	f := strings.Fields(lines[1])
	if strings.HasPrefix(lines[0], "__TEXT") && len(f) >= 2 { // OS X
		fmt.Printf("%s 1 %s text-bytes %s data-bytes %v exe-bytes\n", name, f[0], f[1], info.Size())
	} else if strings.Contains(lines[0], "bss") && len(f) >= 3 {
		fmt.Printf("%s 1 %s text-bytes %s data-bytes %s bss-bytes %v exe-bytes\n", name, f[0], f[1], f[2], info.Size())
	}
}

func runBuild(name, dir string) {
	switch name {
	case "BenchmarkMakeBash":
		runMakeBash()
		return
	case "BenchmarkCmdGoSize":
		runCmdGoSize()
		return
	case "BenchmarkHelloSize":
		runHelloSize()
		return
	}

	pkg, err := build.Import(dir, ".", 0)
	if err != nil {
		log.Print(err)
		return
	}
	args := []string{"-o", "_compilebench_.o"}
	if is6g {
		*flagMemprofilerate = 0
		*flagAlloc = false
		*flagCpuprofile = ""
		*flagMemprofile = ""
	}
	if *flagMemprofilerate >= 0 {
		args = append(args, "-memprofilerate", fmt.Sprint(*flagMemprofilerate))
	}
	args = append(args, strings.Fields(*flagCompilerFlags)...)
	if *flagAlloc || *flagCpuprofile != "" || *flagMemprofile != "" {
		if *flagAlloc || *flagMemprofile != "" {
			args = append(args, "-memprofile", "_compilebench_.memprof")
		}
		if *flagCpuprofile != "" {
			args = append(args, "-cpuprofile", "_compilebench_.cpuprof")
		}
	}
	args = append(args, pkg.GoFiles...)
	cmd := exec.Command(compiler, args...)
	cmd.Dir = pkg.Dir
	cmd.Stdout = os.Stderr
	cmd.Stderr = os.Stderr
	start := time.Now()
	err = cmd.Run()
	if err != nil {
		log.Printf("%v: %v", name, err)
		return
	}
	end := time.Now()

	var allocs, bytes int64
	if *flagAlloc || *flagMemprofile != "" {
		out, err := ioutil.ReadFile(pkg.Dir + "/_compilebench_.memprof")
		if err != nil {
			log.Print("cannot find memory profile after compilation")
		}
		for _, line := range strings.Split(string(out), "\n") {
			f := strings.Fields(line)
			if len(f) < 4 || f[0] != "#" || f[2] != "=" {
				continue
			}
			val, err := strconv.ParseInt(f[3], 0, 64)
			if err != nil {
				continue
			}
			switch f[1] {
			case "TotalAlloc":
				bytes = val
			case "Mallocs":
				allocs = val
			}
		}

		if *flagMemprofile != "" {
			if err := ioutil.WriteFile(*flagMemprofile, out, 0666); err != nil {
				log.Print(err)
			}
		}
		os.Remove(pkg.Dir + "/_compilebench_.memprof")
	}

	if *flagCpuprofile != "" {
		out, err := ioutil.ReadFile(pkg.Dir + "/_compilebench_.cpuprof")
		if err != nil {
			log.Print(err)
		}
		if err := ioutil.WriteFile(*flagCpuprofile, out, 0666); err != nil {
			log.Print(err)
		}
		os.Remove(pkg.Dir + "/_compilebench_.cpuprof")
	}

	if *flagAlloc {
		fmt.Printf("%s 1 %d ns/op %d B/op %d allocs/op\n", name, end.Sub(start).Nanoseconds(), bytes, allocs)
	} else {
		fmt.Printf("%s 1 %d ns/op\n", name, end.Sub(start).Nanoseconds())
	}

	os.Remove(pkg.Dir + "/_compilebench_.o")
}
<commit_msg>compilebench: fix 6g mode<commit_after>// Copyright 2015 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.

// Compilebench benchmarks the speed of the Go compiler.
//
// Usage:
//
//	compilebench [options]
//
// It times the compilation of various packages and prints results in
// the format used by package testing (and expected by rsc.io/benchstat).
//
// The options are:
//
//	-alloc
//		Report allocations.
//
//	-compile exe
//		Use exe as the path to the cmd/compile binary.
//
//	-compileflags 'list'
//		Pass the space-separated list of flags to the compilation.
//
//	-count n
//		Run each benchmark n times (default 1).
//
//	-cpuprofile file
//		Write a CPU profile of the compiler to file.
//
//	-memprofile file
//		Write a memory profile of the compiler to file.
//
//	-memprofilerate rate
//		Set runtime.MemProfileRate during compilation.
//
//	-run regexp
//		Only run benchmarks with names matching regexp.
//
// Although -cpuprofile and -memprofile are intended to write a
// combined profile for all the executed benchmarks to file,
// today they write only the profile for the last benchmark executed.
//
// The default memory profiling rate is one profile sample per 512 kB
// allocated (see ``go doc runtime.MemProfileRate'').
// Lowering the rate (for example, -memprofilerate 64000) produces
// a more fine-grained and therefore accurate profile, but it also incurs
// execution cost. For benchmark comparisons, never use timings
// obtained with a low -memprofilerate option.
//
// Example
//
// Assuming the base version of the compiler has been saved with
// ``toolstash save,'' this sequence compares the old and new compiler:
//
//	compilebench -count 10 -compile $(toolstash -n compile) >old.txt
//	compilebench -count 10 >new.txt
//	benchstat old.txt new.txt
//
package main

import (
	"flag"
	"fmt"
	"go/build"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"regexp"
	"runtime"
	"strconv"
	"strings"
	"time"
)

var (
	goroot   = runtime.GOROOT()
	compiler string
	runRE    *regexp.Regexp
	is6g     bool
)

var (
	flagAlloc          = flag.Bool("alloc", false, "report allocations")
	flagCompiler       = flag.String("compile", "", "use `exe` as the cmd/compile binary")
	flagCompilerFlags  = flag.String("compileflags", "", "additional `flags` to pass to compile")
	flagRun            = flag.String("run", "", "run benchmarks matching `regexp`")
	flagCount          = flag.Int("count", 1, "run benchmarks `n` times")
	flagCpuprofile     = flag.String("cpuprofile", "", "write CPU profile to `file`")
	flagMemprofile     = flag.String("memprofile", "", "write memory profile to `file`")
	flagMemprofilerate = flag.Int64("memprofilerate", -1, "set memory profile `rate`")
)

var tests = []struct {
	name string
	dir  string
}{
	{"BenchmarkTemplate", "html/template"},
	{"BenchmarkGoTypes", "go/types"},
	{"BenchmarkCompiler", "cmd/compile/internal/gc"},
	{"BenchmarkMakeBash", ""},
	{"BenchmarkHelloSize", ""},
	{"BenchmarkCmdGoSize", ""},
}

func usage() {
	fmt.Fprintf(os.Stderr, "usage: compilebench [options]\n")
	fmt.Fprintf(os.Stderr, "options:\n")
	flag.PrintDefaults()
	os.Exit(2)
}

func main() {
	log.SetFlags(0)
	log.SetPrefix("compilebench: ")
	flag.Usage = usage
	flag.Parse()
	if flag.NArg() != 0 {
		usage()
	}

	compiler = *flagCompiler
	if compiler == "" {
		out, err := exec.Command("go", "tool", "-n", "compile").CombinedOutput()
		if err != nil {
			out, err = exec.Command("go", "tool", "-n", "6g").CombinedOutput()
			is6g = true
			if err != nil {
				out, err = exec.Command("go", "tool", "-n", "compile").CombinedOutput()
				log.Fatalf("go tool -n compiler: %v\n%s", err, out)
			}
		}
		compiler = strings.TrimSpace(string(out))
	}

	if *flagRun != "" {
		r, err := regexp.Compile(*flagRun)
		if err != nil {
			log.Fatalf("invalid -run argument: %v", err)
		}
		runRE = r
	}

	for i := 0; i < *flagCount; i++ {
		for _, tt := range tests {
			if runRE == nil || runRE.MatchString(tt.name) {
				runBuild(tt.name, tt.dir)
			}
		}
	}
}

func runCmd(name string, cmd *exec.Cmd) {
	start := time.Now()
	out, err := cmd.CombinedOutput()
	if err != nil {
		log.Printf("%v: %v\n%s", name, err, out)
		return
	}
	fmt.Printf("%s 1 %d ns/op\n", name, time.Since(start).Nanoseconds())
}

func runMakeBash() {
	cmd := exec.Command("./make.bash")
	cmd.Dir = filepath.Join(runtime.GOROOT(), "src")
	runCmd("BenchmarkMakeBash", cmd)
}

func runCmdGoSize() {
	runSize("BenchmarkCmdGoSize", filepath.Join(runtime.GOROOT(), "bin/go"))
}

func runHelloSize() {
	cmd := exec.Command("go", "build", "-o", "_hello_", filepath.Join(runtime.GOROOT(), "test/helloworld.go"))
	cmd.Stdout = os.Stderr
	cmd.Stderr = os.Stderr
	if err := cmd.Run(); err != nil {
		log.Print(err)
		return
	}
	runSize("BenchmarkHelloSize", "_hello_")
}

func runSize(name, file string) {
	info, err := os.Stat(file)
	if err != nil {
		log.Print(err)
		return
	}
	out, err := exec.Command("size", file).CombinedOutput()
	if err != nil {
		log.Printf("size: %v\n%s", err, out)
		return
	}
	lines := strings.Split(string(out), "\n")
	if len(lines) < 2 {
		log.Printf("not enough output from size: %s", out)
		return
	}
	f := strings.Fields(lines[1])
	if strings.HasPrefix(lines[0], "__TEXT") && len(f) >= 2 { // OS X
		fmt.Printf("%s 1 %s text-bytes %s data-bytes %v exe-bytes\n", name, f[0], f[1], info.Size())
	} else if strings.Contains(lines[0], "bss") && len(f) >= 3 {
		fmt.Printf("%s 1 %s text-bytes %s data-bytes %s bss-bytes %v exe-bytes\n", name, f[0], f[1], f[2], info.Size())
	}
}

func runBuild(name, dir string) {
	switch name {
	case "BenchmarkMakeBash":
		runMakeBash()
		return
	case "BenchmarkCmdGoSize":
		runCmdGoSize()
		return
	case "BenchmarkHelloSize":
		runHelloSize()
		return
	}

	pkg, err := build.Import(dir, ".", 0)
	if err != nil {
		log.Print(err)
		return
	}
	args := []string{"-o", "_compilebench_.o"}
	if is6g {
		*flagMemprofilerate = -1
		*flagAlloc = false
		*flagCpuprofile = ""
		*flagMemprofile = ""
	}
	if *flagMemprofilerate >= 0 {
		args = append(args, "-memprofilerate", fmt.Sprint(*flagMemprofilerate))
	}
	args = append(args, strings.Fields(*flagCompilerFlags)...)
	if *flagAlloc || *flagCpuprofile != "" || *flagMemprofile != "" {
		if *flagAlloc || *flagMemprofile != "" {
			args = append(args, "-memprofile", "_compilebench_.memprof")
		}
		if *flagCpuprofile != "" {
			args = append(args, "-cpuprofile", "_compilebench_.cpuprof")
		}
	}
	args = append(args, pkg.GoFiles...)
	cmd := exec.Command(compiler, args...)
	cmd.Dir = pkg.Dir
	cmd.Stdout = os.Stderr
	cmd.Stderr = os.Stderr
	start := time.Now()
	err = cmd.Run()
	if err != nil {
		log.Printf("%v: %v", name, err)
		return
	}
	end := time.Now()

	var allocs, bytes int64
	if *flagAlloc || *flagMemprofile != "" {
		out, err := ioutil.ReadFile(pkg.Dir + "/_compilebench_.memprof")
		if err != nil {
			log.Print("cannot find memory profile after compilation")
		}
		for _, line := range strings.Split(string(out), "\n") {
			f := strings.Fields(line)
			if len(f) < 4 || f[0] != "#" || f[2] != "=" {
				continue
			}
			val, err := strconv.ParseInt(f[3], 0, 64)
			if err != nil {
				continue
			}
			switch f[1] {
			case "TotalAlloc":
				bytes = val
			case "Mallocs":
				allocs = val
			}
		}

		if *flagMemprofile != "" {
			if err := ioutil.WriteFile(*flagMemprofile, out, 0666); err != nil {
				log.Print(err)
			}
		}
		os.Remove(pkg.Dir + "/_compilebench_.memprof")
	}

	if *flagCpuprofile != "" {
		out, err := ioutil.ReadFile(pkg.Dir + "/_compilebench_.cpuprof")
		if err != nil {
			log.Print(err)
		}
		if err := ioutil.WriteFile(*flagCpuprofile, out, 0666); err != nil {
			log.Print(err)
		}
		os.Remove(pkg.Dir + "/_compilebench_.cpuprof")
	}

	if *flagAlloc {
		fmt.Printf("%s 1 %d ns/op %d B/op %d allocs/op\n", name, end.Sub(start).Nanoseconds(), bytes, allocs)
	} else {
		fmt.Printf("%s 1 %d ns/op\n", name, end.Sub(start).Nanoseconds())
	}

	os.Remove(pkg.Dir + "/_compilebench_.o")
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"log"
	"net/http"
	"os"

	k8sclient "github.com/GoogleCloudPlatform/kubernetes/pkg/client"
	restful "github.com/emicklei/go-restful"
	"github.com/emicklei/go-restful/swagger"
	flags "github.com/jessevdk/go-flags"
)

var logger *log.Logger = log.New(os.Stdout, "", 0)

const prefix = "/api"

type Options struct {
	Port                 uint16 `short:"p" long:"port" description:"The port to listen on" default:"9090"`
	KubernetesMaster     string `short:"k" long:"kubernetes-master" description:"The URL to the Kubernetes master"`
	KubernetesApiVersion string `short:"v" long:"kubernetes-api-version" description:"The version of the Kubernetes API to use" default:"v1beta2"`
}

func main() {
	var options Options
	var parser = flags.NewParser(&options, flags.Default)

	if _, err := parser.Parse(); err != nil {
		if e, ok := err.(*flags.Error); !ok || e.Type != flags.ErrHelp {
			parser.WriteHelp(os.Stderr)
			os.Exit(1)
		}
		os.Exit(0)
	}

	k8sConfig := &k8sclient.Config{
		Host:    options.KubernetesMaster,
		Version: options.KubernetesApiVersion,
	}

	k8sClient, err := k8sclient.New(k8sConfig)
	if err != nil {
		log.Panic(err)
	}

	if serverVersion, err := k8sClient.ServerVersion(); err != nil {
		log.Panic("Couldn't retrieve Kubernetes server version - incorrect URL possibly? ", err)
	} else {
		log.Printf("Connecting to Kubernetes server at version %v", serverVersion.String())
	}

	restful.DefaultContainer.Filter(NCSACommonLogFormatLogger())

	config := swagger.Config{
		WebServices: restful.RegisteredWebServices(),
		ApiPath:     prefix + "/apidocs.json",
	}
	swagger.InstallSwaggerService(config)

	resources := []SelfRegisteringResource{
		PingResource{},
	}
	for _, resource := range resources {
		resource.Register(prefix)
	}

	log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", options.Port), nil))
}
<commit_msg>Use k8s env vars for k8s master service<commit_after>package main

import (
	"fmt"
	"log"
	"net/http"
	"os"

	k8sclient "github.com/GoogleCloudPlatform/kubernetes/pkg/client"
	restful "github.com/emicklei/go-restful"
	"github.com/emicklei/go-restful/swagger"
	flags "github.com/jessevdk/go-flags"
)

var logger *log.Logger = log.New(os.Stdout, "", 0)

const prefix = "/api"

type Options struct {
	Port                 uint16 `short:"p" long:"port" description:"The port to listen on" default:"9090"`
	KubernetesMaster     string `short:"k" long:"kubernetes-master" description:"The URL to the Kubernetes master"`
	KubernetesApiVersion string `short:"v" long:"kubernetes-api-version" description:"The version of the Kubernetes API to use" default:"v1beta2"`
}

func main() {
	var options Options
	var parser = flags.NewParser(&options, flags.Default)

	if _, err := parser.Parse(); err != nil {
		if e, ok := err.(*flags.Error); !ok || e.Type != flags.ErrHelp {
			parser.WriteHelp(os.Stderr)
			os.Exit(1)
		}
		os.Exit(0)
	}

	if len(options.KubernetesMaster) == 0 && len(os.Getenv("KUBERNETES_RO_SERVICE_HOST")) > 0 {
		options.KubernetesMaster = os.ExpandEnv("http://${KUBERNETES_RO_SERVICE_HOST}:${KUBERNETES_RO_SERVICE_PORT}")
	}

	k8sConfig := &k8sclient.Config{
		Host:    options.KubernetesMaster,
		Version: options.KubernetesApiVersion,
	}

	k8sClient, err := k8sclient.New(k8sConfig)
	if err != nil {
		log.Panic(err)
	}

	if serverVersion, err := k8sClient.ServerVersion(); err != nil {
		log.Panic("Couldn't retrieve Kubernetes server version - incorrect URL possibly?", err)
	} else {
		log.Printf("Connecting to Kubernetes master at %v running version %v", options.KubernetesMaster, serverVersion.String())
	}

	restful.DefaultContainer.Filter(NCSACommonLogFormatLogger())

	config := swagger.Config{
		WebServices: restful.RegisteredWebServices(),
		ApiPath:     prefix + "/apidocs.json",
	}
	swagger.InstallSwaggerService(config)

	resources := []SelfRegisteringResource{
		PingResource{},
	}
	for _, resource := range resources {
		resource.Register(prefix)
	}

	log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", options.Port), nil))
}
<|endoftext|>


<commit_before>// Copyright 2016 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 (
	"bytes"
	"flag"
	"fmt"
	"os"
	"path/filepath"
	"strings"
	"text/tabwriter"

	"github.com/pkg/errors"
	"github.com/sdboyer/gps"
)

const (
	manifestName = "manifest.json"
	lockName     = "lock.json"
)

var (
	depContext *ctx
	verbose    = flag.Bool("v", false, "enable verbose logging")
)

type command interface {
	Name() string           // "foobar"
	Args() string           // "<baz> [quux...]"
	ShortHelp() string      // "Foo the first bar"
	LongHelp() string       // "Foo the first bar meeting the following conditions..."
	Register(*flag.FlagSet) // command-specific flags
	Hidden() bool           // indicates whether the command should be hidden from help output
	Run([]string) error
}

func main() {

	// Build the list of available commands.
	commands := []command{
		&initCommand{},
		&statusCommand{},
		&ensureCommand{},
		&removeCommand{},
		&hashinCommand{},
	}

	usage := func() {
		fmt.Fprintln(os.Stderr, "Usage: dep <command>")
		fmt.Fprintln(os.Stderr)
		fmt.Fprintln(os.Stderr, "Commands:")
		fmt.Fprintln(os.Stderr)
		w := tabwriter.NewWriter(os.Stderr, 0, 4, 2, ' ', 0)
		for _, command := range commands {
			if !command.Hidden() {
				fmt.Fprintf(w, "\t%s\t%s\n", command.Name(), command.ShortHelp())
			}
		}
		w.Flush()
		fmt.Fprintln(os.Stderr)
	}

	if len(os.Args) <= 1 || len(os.Args) == 2 && (strings.Contains(strings.ToLower(os.Args[1]), "help") || strings.ToLower(os.Args[1]) == "-h") {
		usage()
		os.Exit(1)
	}

	// Set up the dep context.
	// TODO(pb): can this be deglobalized, pretty please?
	hc, err := newContext()
	if err != nil {
		fmt.Fprint(os.Stderr, err.Error())
		os.Exit(1)
	}
	depContext = hc

	for _, command := range commands {
		if name := command.Name(); os.Args[1] == name {
			// Build flag set with global flags in there.
			// TODO(pb): can we deglobalize verbose, pretty please?
			fs := flag.NewFlagSet(name, flag.ExitOnError)
			fs.BoolVar(verbose, "v", false, "enable verbose logging")

			// Register the subcommand flags in there, too.
			command.Register(fs)

			// Override the usage text to something nicer.
			resetUsage(fs, command.Name(), command.Args(), command.LongHelp())

			// Parse the flags the user gave us.
			if err := fs.Parse(os.Args[2:]); err != nil {
				fs.Usage()
				os.Exit(1)
			}

			// Run the command with the post-flag-processing args.
			if err := command.Run(fs.Args()); err != nil {
				fmt.Fprintf(os.Stderr, "%v\n", err)
				os.Exit(1)
			}

			// Easy peasy livin' breezy.
			return
		}
	}

	fmt.Fprintf(os.Stderr, "%s: no such command\n", os.Args[1])
	usage()
	os.Exit(1)
}

func resetUsage(fs *flag.FlagSet, name, args, longHelp string) {
	var (
		hasFlags   bool
		flagBlock  bytes.Buffer
		flagWriter = tabwriter.NewWriter(&flagBlock, 0, 4, 2, ' ', 0)
	)
	fs.VisitAll(func(f *flag.Flag) {
		hasFlags = true
		// Default-empty string vars should read "(default: <none>)"
		// rather than the comparatively ugly "(default: )".
		defValue := f.DefValue
		if defValue == "" {
			defValue = "<none>"
		}
		fmt.Fprintf(flagWriter, "\t-%s\t%s (default: %s)\n", f.Name, f.Usage, defValue)
	})
	flagWriter.Flush()
	fs.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage: dep %s %s\n", name, args)
		fmt.Fprintln(os.Stderr)
		fmt.Fprintln(os.Stderr, strings.TrimSpace(longHelp))
		fmt.Fprintln(os.Stderr)
		if hasFlags {
			fmt.Fprintln(os.Stderr, "Flags:")
			fmt.Fprintln(os.Stderr)
			fmt.Fprintln(os.Stderr, flagBlock.String())
		}
	}
}

var (
	errProjectNotFound = errors.New("could not find project manifest.json")
)

func findProjectRootFromWD() (string, error) {
	path, err := os.Getwd()
	if err != nil {
		return "", fmt.Errorf("could not get working directory: %s", err)
	}
	return findProjectRoot(path)
}

// search upwards looking for a manifest file until we get to the root of the
// filesystem
func findProjectRoot(from string) (string, error) {
	for {
		mp := filepath.Join(from, manifestName)

		_, err := os.Stat(mp)
		if err == nil {
			return from, nil
		}
		if !os.IsNotExist(err) {
			// Some err other than non-existence - return that out
			return "", err
		}

		parent := filepath.Dir(from)
		if parent == from {
			return "", errProjectNotFound
		}
		from = parent
	}
}

type project struct {
	// absroot is the absolute path to the root directory of the project.
	absroot string
	// importroot is the import path of the project's root directory.
	importroot gps.ProjectRoot
	m          *manifest
	l          *lock
}

// makeParams is a simple helper to create a gps.SolveParameters without setting
// any nils incorrectly.
func (p *project) makeParams() gps.SolveParameters {
	params := gps.SolveParameters{
		RootDir: p.absroot,
	}

	if p.m != nil {
		params.Manifest = p.m
	}

	if p.l != nil {
		params.Lock = p.l
	}

	return params
}

func logf(format string, args ...interface{}) {
	// TODO: something else?
	fmt.Fprintf(os.Stderr, "dep: "+format+"\n", args...)
}

func vlogf(format string, args ...interface{}) {
	if !*verbose {
		return
	}
	logf(format, args...)
}
<commit_msg>suggest user to init when no manifest is found<commit_after>// Copyright 2016 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 (
	"bytes"
	"flag"
	"fmt"
	"os"
	"path/filepath"
	"strings"
	"text/tabwriter"

	"github.com/pkg/errors"
	"github.com/sdboyer/gps"
)

const (
	manifestName = "manifest.json"
	lockName     = "lock.json"
)

var (
	depContext *ctx
	verbose    = flag.Bool("v", false, "enable verbose logging")
)

type command interface {
	Name() string           // "foobar"
	Args() string           // "<baz> [quux...]"
	ShortHelp() string      // "Foo the first bar"
	LongHelp() string       // "Foo the first bar meeting the following conditions..."
	Register(*flag.FlagSet) // command-specific flags
	Hidden() bool           // indicates whether the command should be hidden from help output
	Run([]string) error
}

func main() {

	// Build the list of available commands.
	commands := []command{
		&initCommand{},
		&statusCommand{},
		&ensureCommand{},
		&removeCommand{},
		&hashinCommand{},
	}

	usage := func() {
		fmt.Fprintln(os.Stderr, "Usage: dep <command>")
		fmt.Fprintln(os.Stderr)
		fmt.Fprintln(os.Stderr, "Commands:")
		fmt.Fprintln(os.Stderr)
		w := tabwriter.NewWriter(os.Stderr, 0, 4, 2, ' ', 0)
		for _, command := range commands {
			if !command.Hidden() {
				fmt.Fprintf(w, "\t%s\t%s\n", command.Name(), command.ShortHelp())
			}
		}
		w.Flush()
		fmt.Fprintln(os.Stderr)
	}

	if len(os.Args) <= 1 || len(os.Args) == 2 && (strings.Contains(strings.ToLower(os.Args[1]), "help") || strings.ToLower(os.Args[1]) == "-h") {
		usage()
		os.Exit(1)
	}

	// Set up the dep context.
	// TODO(pb): can this be deglobalized, pretty please?
	hc, err := newContext()
	if err != nil {
		fmt.Fprint(os.Stderr, err.Error())
		os.Exit(1)
	}
	depContext = hc

	for _, command := range commands {
		if name := command.Name(); os.Args[1] == name {
			// Build flag set with global flags in there.
			// TODO(pb): can we deglobalize verbose, pretty please?
			fs := flag.NewFlagSet(name, flag.ExitOnError)
			fs.BoolVar(verbose, "v", false, "enable verbose logging")

			// Register the subcommand flags in there, too.
			command.Register(fs)

			// Override the usage text to something nicer.
			resetUsage(fs, command.Name(), command.Args(), command.LongHelp())

			// Parse the flags the user gave us.
			if err := fs.Parse(os.Args[2:]); err != nil {
				fs.Usage()
				os.Exit(1)
			}

			// Run the command with the post-flag-processing args.
			if err := command.Run(fs.Args()); err != nil {
				fmt.Fprintf(os.Stderr, "%v\n", err)
				os.Exit(1)
			}

			// Easy peasy livin' breezy.
			return
		}
	}

	fmt.Fprintf(os.Stderr, "%s: no such command\n", os.Args[1])
	usage()
	os.Exit(1)
}

func resetUsage(fs *flag.FlagSet, name, args, longHelp string) {
	var (
		hasFlags   bool
		flagBlock  bytes.Buffer
		flagWriter = tabwriter.NewWriter(&flagBlock, 0, 4, 2, ' ', 0)
	)
	fs.VisitAll(func(f *flag.Flag) {
		hasFlags = true
		// Default-empty string vars should read "(default: <none>)"
		// rather than the comparatively ugly "(default: )".
		defValue := f.DefValue
		if defValue == "" {
			defValue = "<none>"
		}
		fmt.Fprintf(flagWriter, "\t-%s\t%s (default: %s)\n", f.Name, f.Usage, defValue)
	})
	flagWriter.Flush()
	fs.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage: dep %s %s\n", name, args)
		fmt.Fprintln(os.Stderr)
		fmt.Fprintln(os.Stderr, strings.TrimSpace(longHelp))
		fmt.Fprintln(os.Stderr)
		if hasFlags {
			fmt.Fprintln(os.Stderr, "Flags:")
			fmt.Fprintln(os.Stderr)
			fmt.Fprintln(os.Stderr, flagBlock.String())
		}
	}
}

var (
	errProjectNotFound = errors.New("could not find project manifest.json, use dep init to initiate a manifest")
)

func findProjectRootFromWD() (string, error) {
	path, err := os.Getwd()
	if err != nil {
		return "", fmt.Errorf("could not get working directory: %s", err)
	}
	return findProjectRoot(path)
}

// search upwards looking for a manifest file until we get to the root of the
// filesystem
func findProjectRoot(from string) (string, error) {
	for {
		mp := filepath.Join(from, manifestName)

		_, err := os.Stat(mp)
		if err == nil {
			return from, nil
		}
		if !os.IsNotExist(err) {
			// Some err other than non-existence - return that out
			return "", err
		}

		parent := filepath.Dir(from)
		if parent == from {
			return "", errProjectNotFound
		}
		from = parent
	}
}

type project struct {
	// absroot is the absolute path to the root directory of the project.
	absroot string
	// importroot is the import path of the project's root directory.
	importroot gps.ProjectRoot
	m          *manifest
	l          *lock
}

// makeParams is a simple helper to create a gps.SolveParameters without setting
// any nils incorrectly.
func (p *project) makeParams() gps.SolveParameters {
	params := gps.SolveParameters{
		RootDir: p.absroot,
	}

	if p.m != nil {
		params.Manifest = p.m
	}

	if p.l != nil {
		params.Lock = p.l
	}

	return params
}

func logf(format string, args ...interface{}) {
	// TODO: something else?
	fmt.Fprintf(os.Stderr, "dep: "+format+"\n", args...)
}

func vlogf(format string, args ...interface{}) {
	if !*verbose {
		return
	}
	logf(format, args...)
}
<|endoftext|>


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

package main

import (
	"log"

	"github.com/venicegeo/pz-gocommon/elasticsearch"
	piazza "github.com/venicegeo/pz-gocommon/gocommon"
	pzsyslog "github.com/venicegeo/pz-gocommon/syslog"
	pzuuidgen "github.com/venicegeo/pz-uuidgen/uuidgen"
	pzworkflow "github.com/venicegeo/pz-workflow/workflow"
)

func main() {
	sys, logger, uuidgen := makeSystem()

	log.Printf("pz-workflow starting...")

	indices := makeIndexes(sys)

	workflowServer := makeWorkflow(sys, indices, logger, uuidgen)

	serverLoop(sys, workflowServer)
}

func makeWorkflow(sys *piazza.SystemConfig,
	indices []*elasticsearch.Index,
	logger *pzsyslog.Logger,
	uuidgen pzuuidgen.IClient) *pzworkflow.Server {
	workflowService := &pzworkflow.Service{}
	err := workflowService.Init(
		sys,
		logger,
		uuidgen,
		indices[0],
		indices[1],
		indices[2],
		indices[3],
		indices[4],
		indices[5])
	if err != nil {
		log.Fatal(err)
	}

	err = workflowService.InitCron()
	if err != nil {
		log.Fatal(err)
	}

	workflowServer := &pzworkflow.Server{}
	err = workflowServer.Init(workflowService)
	if err != nil {
		log.Fatal(err)
	}

	return workflowServer
}

func makeSystem() (
	*piazza.SystemConfig,
	*pzsyslog.Logger,
	*pzuuidgen.Client) {

	required := []piazza.ServiceName{
		piazza.PzElasticSearch,
		piazza.PzLogger,
		piazza.PzUuidgen,
		piazza.PzKafka,
		piazza.PzServiceController,
		piazza.PzIdam,
	}

	sys, err := piazza.NewSystemConfig(piazza.PzWorkflow, required)
	if err != nil {
		log.Fatal(err)
	}

	logUrl, err := sys.GetURL(piazza.PzLogger)
	if err != nil {
		log.Fatal(err)
	}
	logWriter, err := pzsyslog.NewHttpWriter(logUrl)
	if err != nil {
		log.Fatal(err)
	}
	//logWriter := &pzsyslog.NilWriter{}
	logger := pzsyslog.NewLogger(logWriter, "pz-workflow")

	uuidgen, err := pzuuidgen.NewClient(sys)
	if err != nil {
		log.Fatal(err)
	}

	return sys, logger, uuidgen
}

func serverLoop(sys *piazza.SystemConfig,
	workflowServer *pzworkflow.Server) {
	genericServer := piazza.GenericServer{Sys: sys}

	err := genericServer.Configure(workflowServer.Routes)
	if err != nil {
		log.Fatal(err)
	}

	done, err := genericServer.Start()
	if err != nil {
		log.Fatal(err)
	}

	err = <-done
	if err != nil {
		log.Fatal(err)
	}
}

func makeIndexes(sys *piazza.SystemConfig) []*elasticsearch.Index {
	eventtypesIndex, err := elasticsearch.NewIndex(sys, "eventtypes003", pzworkflow.EventTypeIndexSettings)
	if err != nil {
		log.Fatal(err)
	}
	eventsIndex, err := elasticsearch.NewIndex(sys, "events004", pzworkflow.EventIndexSettings)
	if err != nil {
		log.Fatal(err)
	}
	triggersIndex, err := elasticsearch.NewIndex(sys, "triggers003", pzworkflow.TriggerIndexSettings)
	if err != nil {
		log.Fatal(err)
	}
	alertsIndex, err := elasticsearch.NewIndex(sys, "alerts003", pzworkflow.AlertIndexSettings)
	if err != nil {
		log.Fatal(err)
	}
	cronIndex, err := elasticsearch.NewIndex(sys, "crons003", pzworkflow.CronIndexSettings)
	if err != nil {
		log.Fatal(err)
	}
	testElasticsearchIndex, err := elasticsearch.NewIndex(sys, "testelasticsearch003", pzworkflow.TestElasticsearchSettings)
	if err != nil {
		log.Fatal(err)
	}

	ret := []*elasticsearch.Index{
		eventtypesIndex, eventsIndex, triggersIndex,
		alertsIndex, cronIndex, testElasticsearchIndex,
	}
	return ret
}
<commit_msg>sync w/ gocommon, from Fridayish<commit_after>// Copyright 2016, RadiantBlue Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
	"log"

	"github.com/venicegeo/pz-gocommon/elasticsearch"
	piazza "github.com/venicegeo/pz-gocommon/gocommon"
	pzsyslog "github.com/venicegeo/pz-gocommon/syslog"
	pzuuidgen "github.com/venicegeo/pz-uuidgen/uuidgen"
	pzworkflow "github.com/venicegeo/pz-workflow/workflow"
)

func main() {
	sys, logger, uuidgen := makeSystem()

	log.Printf("pz-workflow starting...")

	indices := makeIndexes(sys)

	workflowServer := makeWorkflow(sys, indices, logger, uuidgen)

	serverLoop(sys, workflowServer)
}

func makeWorkflow(sys *piazza.SystemConfig,
	indices []*elasticsearch.Index,
	logger *pzsyslog.Logger,
	uuidgen pzuuidgen.IClient) *pzworkflow.Server {
	workflowService := &pzworkflow.Service{}
	err := workflowService.Init(
		sys,
		logger,
		uuidgen,
		indices[0],
		indices[1],
		indices[2],
		indices[3],
		indices[4],
		indices[5])
	if err != nil {
		log.Fatal(err)
	}

	err = workflowService.InitCron()
	if err != nil {
		log.Fatal(err)
	}

	workflowServer := &pzworkflow.Server{}
	err = workflowServer.Init(workflowService)
	if err != nil {
		log.Fatal(err)
	}

	return workflowServer
}

func makeSystem() (
	*piazza.SystemConfig,
	*pzsyslog.Logger,
	*pzuuidgen.Client) {

	required := []piazza.ServiceName{
		piazza.PzElasticSearch,
		piazza.PzLogger,
		piazza.PzUuidgen,
		piazza.PzKafka,
		piazza.PzServiceController,
		piazza.PzIdam,
	}

	sys, err := piazza.NewSystemConfig(piazza.PzWorkflow, required)
	if err != nil {
		log.Fatal(err)
	}

	logUrl, err := sys.GetURL(piazza.PzLogger)
	if err != nil {
		log.Fatal(err)
	}
	logWriter, err := pzsyslog.NewHttpWriter(logUrl, "")
	if err != nil {
		log.Fatal(err)
	}
	//logWriter := &pzsyslog.NilWriter{}
	logger := pzsyslog.NewLogger(logWriter, "pz-workflow")

	uuidgen, err := pzuuidgen.NewClient(sys)
	if err != nil {
		log.Fatal(err)
	}

	return sys, logger, uuidgen
}

func serverLoop(sys *piazza.SystemConfig,
	workflowServer *pzworkflow.Server) {
	genericServer := piazza.GenericServer{Sys: sys}

	err := genericServer.Configure(workflowServer.Routes)
	if err != nil {
		log.Fatal(err)
	}

	done, err := genericServer.Start()
	if err != nil {
		log.Fatal(err)
	}

	err = <-done
	if err != nil {
		log.Fatal(err)
	}
}

func makeIndexes(sys *piazza.SystemConfig) []*elasticsearch.Index {
	eventtypesIndex, err := elasticsearch.NewIndex(sys, "eventtypes003", pzworkflow.EventTypeIndexSettings)
	if err != nil {
		log.Fatal(err)
	}
	eventsIndex, err := elasticsearch.NewIndex(sys, "events004", pzworkflow.EventIndexSettings)
	if err != nil {
		log.Fatal(err)
	}
	triggersIndex, err := elasticsearch.NewIndex(sys, "triggers003", pzworkflow.TriggerIndexSettings)
	if err != nil {
		log.Fatal(err)
	}
	alertsIndex, err := elasticsearch.NewIndex(sys, "alerts003", pzworkflow.AlertIndexSettings)
	if err != nil {
		log.Fatal(err)
	}
	cronIndex, err := elasticsearch.NewIndex(sys, "crons003", pzworkflow.CronIndexSettings)
	if err != nil {
		log.Fatal(err)
	}
	testElasticsearchIndex, err := elasticsearch.NewIndex(sys, "testelasticsearch003", pzworkflow.TestElasticsearchSettings)
	if err != nil {
		log.Fatal(err)
	}

	ret := []*elasticsearch.Index{
		eventtypesIndex, eventsIndex, triggersIndex,
		alertsIndex, cronIndex, testElasticsearchIndex,
	}
	return ret
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"sort"
	"sync"
)

// TODO: thorough spell checking run

// TODO: in our preprocess run, we should blank out sections that we shouldn't check, like URL's

// TODO: atom editor plugin for linter (should work with our JSON mode)

func processFile(filename string, report chan *Warning, maxWarnings int) int {
	fm, err := NewFileMap(filename)
	check(err)

	// We launch a goroutine per list of TextChecks and write any warnings to
	// our gather channel. Then we pump from the gather channel to our caller.
	// There are many things we go do as we gather the warnings, but currently
	// this is only used to handle the max warnings per file feature.
	wg := sync.WaitGroup{}
	gather := make(chan *Warning, 64)

	launch := func(checks []TextCheck) {
		wg.Add(1)

		go func(fm *FileMap, checks []TextCheck) {
			defer wg.Done()

			count := 0
			for _, c := range checks {
				for _, warn := range c.Match(fm) {
					gather <- warn
					count += 1
					if maxWarnings > 0 && count >= maxWarnings {
						// optimization: see below for actual maxWarnings work
						return
					}
				}
			}
		}(fm, checks)
	}

	launch(ShouldNotExist())
	launch(ShouldNotCliche())
	launch(ShouldNotRedundant())
	launch(ShouldNotProfane())
	launch(ShouldNotBias())
	launch(ShouldNotPassive())

	go func() {
		wg.Wait()
		close(gather)
	}()

	// Actually funnel warnings back to caller
	finalCount := 0

	for warn := range gather {
		report <- warn
		finalCount += 1
		if maxWarnings > 0 && finalCount >= maxWarnings {
			break
		}
	}

	return finalCount
}

func main() {
	flags := flag.NewFlagSet("tint", flag.ExitOnError)
	verbosePtr := flags.Bool("v", false, "Verbose output (written to stderr")
	sortPtr := flags.Bool("s", false, "Sort output (by file name, line number, column number")
	jsonPtr := flags.Bool("j", false, "Use JSON output instead of default")
	maxAllPtr := flags.Int("max", 0, "Maximum warnings to output")
	maxFilePtr := flags.Int("filemax", 0, "Maximum warnings per file")
	check(flags.Parse(os.Args[1:]))
	args := flags.Args()

	// If it should always be printed, we use log. If it should only be printed when
	// verbose=true, then we use verb
	var verb *log.Logger
	if *verbosePtr {
		verb = log.New(os.Stderr, "", 0)
	} else {
		verb = log.New(ioutil.Discard, "", 0)
	}

	verb.Printf("Verbose: %v\n", *verbosePtr)
	verb.Printf("Sort: %v\n", *sortPtr)

	wg := sync.WaitGroup{}
	report := make(chan *Warning, 64)

	for _, filename := range args {
		verb.Printf("FILE: %s\n", filename)
		wg.Add(1)
		go func(fn string) {
			defer wg.Done()
			processFile(fn, report, *maxFilePtr)
		}(filename)
	}

	go func() {
		wg.Wait()
		close(report)
	}()

	// Final output channel might be filtered by sort or count options
	output := report

	// Sort filter
	if *sortPtr {
		sortInput := output
		output = make(chan *Warning, 64)

		go func(out chan *Warning) {
			defer close(out)

			warnings := make([]*Warning, 0, 128)
			for warn := range sortInput {
				warnings = append(warnings, warn)
			}

			verb.Printf("Sorting %d warnings\n", len(warnings))
			sort.Sort(WarningDefSort(warnings))

			for _, warn := range warnings {
				out <- warn
			}
		}(output)
	}

	// Output all warnings
	var outputter func(w *Warning)
	if *jsonPtr {
		outputter = func(w *Warning) {
			fmt.Printf("%s\n", w.CreateJSON())
		}
	} else {
		outputter = func(w *Warning) {
			fmt.Printf("%s:%d:%d:warning: %s\n",
				w.Filename, w.Row+1, w.Col+1, msgEscape(w.Msg),
			)
		}
	}

	written := 0
	for warn := range output {
		outputter(warn)
		written += 1
		if *maxAllPtr > 0 && written >= *maxAllPtr {
			verb.Printf("Maximum warnings reached (%d)\n", written)
			break
		}
	}
}
<commit_msg>add some ideas for text skipping<commit_after>package main

import (
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"sort"
	"sync"
)

// TODO: thorough spell checking run

// TODO: in our preprocess run, we should blank out sections that we shouldn't
//       check, like URL's. Maybe also strings in quotes?

// TODO: atom editor plugin for linter (should work with our JSON mode)

func processFile(filename string, report chan *Warning, maxWarnings int) int {
	fm, err := NewFileMap(filename)
	check(err)

	// We launch a goroutine per list of TextChecks and write any warnings to
	// our gather channel. Then we pump from the gather channel to our caller.
	// There are many things we go do as we gather the warnings, but currently
	// this is only used to handle the max warnings per file feature.
	wg := sync.WaitGroup{}
	gather := make(chan *Warning, 64)

	launch := func(checks []TextCheck) {
		wg.Add(1)

		go func(fm *FileMap, checks []TextCheck) {
			defer wg.Done()

			count := 0
			for _, c := range checks {
				for _, warn := range c.Match(fm) {
					gather <- warn
					count += 1
					if maxWarnings > 0 && count >= maxWarnings {
						// optimization: see below for actual maxWarnings work
						return
					}
				}
			}
		}(fm, checks)
	}

	launch(ShouldNotExist())
	launch(ShouldNotCliche())
	launch(ShouldNotRedundant())
	launch(ShouldNotProfane())
	launch(ShouldNotBias())
	launch(ShouldNotPassive())

	go func() {
		wg.Wait()
		close(gather)
	}()

	// Actually funnel warnings back to caller
	finalCount := 0

	for warn := range gather {
		report <- warn
		finalCount += 1
		if maxWarnings > 0 && finalCount >= maxWarnings {
			break
		}
	}

	return finalCount
}

func main() {
	flags := flag.NewFlagSet("tint", flag.ExitOnError)
	verbosePtr := flags.Bool("v", false, "Verbose output (written to stderr")
	sortPtr := flags.Bool("s", false, "Sort output (by file name, line number, column number")
	jsonPtr := flags.Bool("j", false, "Use JSON output instead of default")
	maxAllPtr := flags.Int("max", 0, "Maximum warnings to output")
	maxFilePtr := flags.Int("filemax", 0, "Maximum warnings per file")
	check(flags.Parse(os.Args[1:]))
	args := flags.Args()

	// If it should always be printed, we use log. If it should only be printed when
	// verbose=true, then we use verb
	var verb *log.Logger
	if *verbosePtr {
		verb = log.New(os.Stderr, "", 0)
	} else {
		verb = log.New(ioutil.Discard, "", 0)
	}

	verb.Printf("Verbose: %v\n", *verbosePtr)
	verb.Printf("Sort: %v\n", *sortPtr)

	wg := sync.WaitGroup{}
	report := make(chan *Warning, 64)

	for _, filename := range args {
		verb.Printf("FILE: %s\n", filename)
		wg.Add(1)
		go func(fn string) {
			defer wg.Done()
			processFile(fn, report, *maxFilePtr)
		}(filename)
	}

	go func() {
		wg.Wait()
		close(report)
	}()

	// Final output channel might be filtered by sort or count options
	output := report

	// Sort filter
	if *sortPtr {
		sortInput := output
		output = make(chan *Warning, 64)

		go func(out chan *Warning) {
			defer close(out)

			warnings := make([]*Warning, 0, 128)
			for warn := range sortInput {
				warnings = append(warnings, warn)
			}

			verb.Printf("Sorting %d warnings\n", len(warnings))
			sort.Sort(WarningDefSort(warnings))

			for _, warn := range warnings {
				out <- warn
			}
		}(output)
	}

	// Output all warnings
	var outputter func(w *Warning)
	if *jsonPtr {
		outputter = func(w *Warning) {
			fmt.Printf("%s\n", w.CreateJSON())
		}
	} else {
		outputter = func(w *Warning) {
			fmt.Printf("%s:%d:%d:warning: %s\n",
				w.Filename, w.Row+1, w.Col+1, msgEscape(w.Msg),
			)
		}
	}

	written := 0
	for warn := range output {
		outputter(warn)
		written += 1
		if *maxAllPtr > 0 && written >= *maxAllPtr {
			verb.Printf("Maximum warnings reached (%d)\n", written)
			break
		}
	}
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"os"
	"runtime"
	"syscall"
	"time"
	"unsafe"
)

var (
	py          = syscall.NewLazyDLL("Python34.dll")
	Init        = py.NewProc("Py_Initialize")
	Finalize    = py.NewProc("Py_Finalize")
	SetPath     = py.NewProc("Py_SetPath")
	SetProgName = py.NewProc("Py_SetProgramName")
	SetArgvEx   = py.NewProc("PySys_SetArgvEx")
	SetArgv     = py.NewProc("PySys_SetArgv")
	Run         = py.NewProc("PyRun_SimpleString")
	Main        = py.NewProc("Py_Main")
	Free        = py.NewProc("PyMem_RawFree")
	RawMalloc   = py.NewProc("PyMem_RawMalloc")
	Decode      = py.NewProc("Py_DecodeLocale")
)

func main() {
	runtime.GOMAXPROCS(runtime.NumCPU()) // use all physical cores. for in your case of using go routines

	//prog := uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("python")))
	//Call(SetProgName, prog)

	/*  Example : Imply target script and pass every argument to there */
	/*argc := len(os.Args) + 1
	argv := make([]uintptr, argc+1)
	argv[1] = uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("main.py")))
	for i, v := range os.Args[1:] {
		argv[i+2] = uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(v)))
	}*/

	/*  Example : Simplest wrapper */
	argc := len(os.Args)
	argv := make([]uintptr, argc)
	for i, v := range os.Args[1:] {
		argv[i+1] = uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(v)))
	}

	Call(Init)
	s := time.Now()
	Call(Main, uintptr(argc), uintptr(unsafe.Pointer(&argv[0])))
	//updatepath := 0
	e := time.Since(s)
	fmt.Println("Execution time :", e)
	/* Example : inline script instead of external script execution */
	//script := syscall.StringByteSlice(`from sys import argv; print(argv)`)
	//script := syscall.StringByteSlice(`import sys; sys.argv=['idea.sh']; from imp import load_module, PY_SOURCE; f='main.py'; load_module('__main__', open(f), f, ('.py', 'r', PY_SOURCE))`)
	//Call(Run, uintptr(unsafe.Pointer(&script[0])))

	Call(Finalize)
	time.Sleep(1 * time.Microsecond)
}

func Call(proc *syscall.LazyProc, args ...uintptr) uintptr {
	r1, r2, le := proc.Call(args...)
	//fmt.Println(r1, r2, le) // Debug purpose
	_, _ = r2, le
	return r1
}
<commit_msg>readme<commit_after>package main

import (
	"fmt"
	"os"
	"runtime"
	"syscall"
	"time"
	"unsafe"
)

var (
	py          = syscall.NewLazyDLL("Python34.dll")
	Init        = py.NewProc("Py_Initialize")
	Finalize    = py.NewProc("Py_Finalize")
	SetPath     = py.NewProc("Py_SetPath")
	SetProgName = py.NewProc("Py_SetProgramName")
	SetArgvEx   = py.NewProc("PySys_SetArgvEx")
	SetArgv     = py.NewProc("PySys_SetArgv")
	Run         = py.NewProc("PyRun_SimpleString")
	Main        = py.NewProc("Py_Main")
	Free        = py.NewProc("PyMem_RawFree")
	RawMalloc   = py.NewProc("PyMem_RawMalloc")
	Decode      = py.NewProc("Py_DecodeLocale")
)

func main() {
	runtime.GOMAXPROCS(runtime.NumCPU()) // use all physical cores. for in your case of using go routines

	//prog := uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("python")))
	//Call(SetProgName, prog)

	/*  Example : Imply target script and pass every argument to there */
	/*						You could also use batch shortcut file bin bin folder as replacement */
	/*argc := len(os.Args) + 1
	argv := make([]uintptr, argc+1)
	argv[1] = uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr("main.py")))
	for i, v := range os.Args[1:] {
		argv[i+2] = uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(v)))
	}*/

	/*  Example : Simplest wrapper */
	argc := len(os.Args)
	argv := make([]uintptr, argc)
	for i, v := range os.Args[1:] {
		argv[i+1] = uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(v)))
	}

	Call(Init)
	s := time.Now()
	Call(Main, uintptr(argc), uintptr(unsafe.Pointer(&argv[0])))
	//updatepath := 0
	e := time.Since(s)
	fmt.Println("Execution time :", e)
	/* Example : inline script instead of external script execution */
	//script := syscall.StringByteSlice(`from sys import argv; print(argv)`)
	//script := syscall.StringByteSlice(`import sys; sys.argv=['idea.sh']; from imp import load_module, PY_SOURCE; f='main.py'; load_module('__main__', open(f), f, ('.py', 'r', PY_SOURCE))`)
	//Call(Run, uintptr(unsafe.Pointer(&script[0])))

	Call(Finalize)
	time.Sleep(1 * time.Microsecond)
}

func Call(proc *syscall.LazyProc, args ...uintptr) uintptr {
	r1, r2, le := proc.Call(args...)
	//fmt.Println(r1, r2, le) // Debug purpose
	_, _ = r2, le
	return r1
}
<|endoftext|>


<commit_before>package main

import macaron "gopkg.in/macaron.v1"
import "github.com/bruferrari/go-fbmessenger/fblib"

func macaronSetUp() {
	m := macaron.Classic()

	m.Get("/", func() string {
		return "1951683431"
	})

	m.Run()
}

func main() {
	fblib.DefineAccessToken("")
	macaronSetUp()
}
<commit_msg>implementing...<commit_after>package main

import (
	"log"

	"github.com/bruferrari/go-fbmessenger/fblib"
	macaron "gopkg.in/macaron.v1"
)

func macaronSetUp() {
	m := macaron.Classic()

	m.Get("/", func(ctx *macaron.Context) {
		url := ctx.Req.URL
		params := url.Query()

		log.Println(url)
		log.Println(params["hub.mode"])

		if params["hub.mode"][0] == "subscribe" {
			log.Println("HELL YEAH!")
		}
		log.Println(":(")
	})

	m.Run()
}

func main() {
	fblib.DefineAccessToken("")
	macaronSetUp()
}
<|endoftext|>