<commit_before>// Package c2go contains the main function for running the executable.
package main

import (
	"flag"
	"fmt"
	"os"

	"github.com/Konstantin8105/c2go/analyze"
)

// Version can be requested through the command line with:
//
//     c2go -v
//
// See https://github.com/elliotchance/c2go/wiki/Release-Process
const Version = "0.13.3"

func main() {
	var (
		versionFlag       = flag.Bool("v", false, "print the version and exit")
		transpileCommand  = flag.NewFlagSet("transpile", flag.ContinueOnError)
		verboseFlag       = transpileCommand.Bool("V", false, "print progress as comments")
		outputFlag        = transpileCommand.String("o", "", "output Go generated code to the specified file")
		packageFlag       = transpileCommand.String("p", "main", "set the name of the generated package")
		transpileHelpFlag = transpileCommand.Bool("h", false, "print help information")
		astCommand        = flag.NewFlagSet("ast", flag.ContinueOnError)
		astHelpFlag       = astCommand.Bool("h", false, "print help information")
	)

	flag.Usage = func() {
		usage := "Usage: %s [-v] [<command>] [<flags>] file.c\n\n"
		usage += "Commands:\n"
		usage += "  transpile\ttranspile an input C source file to Go\n"
		usage += "  ast\t\tprint AST before translated Go code\n\n"

		usage += "Flags:\n"
		fmt.Fprintf(os.Stderr, usage, os.Args[0])
		flag.PrintDefaults()
	}

	flag.Parse()

	if *versionFlag {
		// Simply print out the version and exit.
		fmt.Println(Version)
		return
	}

	if flag.NArg() < 1 {
		flag.Usage()
		os.Exit(1)
	}

	args := analyze.ProgramArgs{Verbose: *verboseFlag, Ast: false}

	switch os.Args[1] {
	case "ast":
		err := astCommand.Parse(os.Args[2:])
		if err != nil {
			fmt.Printf("Ast command cannot parse: %v", err)
			return
		}

		if *astHelpFlag || astCommand.NArg() == 0 {
			fmt.Fprintf(os.Stderr, "Usage: %s ast file.c\n", os.Args[0])
			astCommand.PrintDefaults()
			os.Exit(1)
		}

		args.Ast = true
		args.InputFile = astCommand.Arg(0)

		if err = analyze.Start(args); err != nil {
			fmt.Printf("Error: %v", err)
		}
	case "transpile":
		err := transpileCommand.Parse(os.Args[2:])
		if err != nil {
			fmt.Printf("Transpile command cannot parse: %v", err)
			return
		}

		if *transpileHelpFlag || transpileCommand.NArg() == 0 {
			fmt.Fprintf(os.Stderr, "Usage: %s transpile [-V] [-o file.go] [-p package] file.c\n", os.Args[0])
			transpileCommand.PrintDefaults()
			os.Exit(1)
		}

		args.InputFile = transpileCommand.Arg(0)
		args.OutputFile = *outputFlag
		args.PackageName = *packageFlag

		if err = analyze.Start(args); err != nil {
			fmt.Printf("Error: %v", err)
		}
	default:
		flag.Usage()
		os.Exit(1)
	}
}
<commit_msg>add change from return  to os.Exit<commit_after>// Package c2go contains the main function for running the executable.
package main

import (
	"flag"
	"fmt"
	"os"

	"github.com/Konstantin8105/c2go/analyze"
)

// Version can be requested through the command line with:
//
//     c2go -v
//
// See https://github.com/elliotchance/c2go/wiki/Release-Process
const Version = "0.13.3"

func main() {
	var (
		versionFlag       = flag.Bool("v", false, "print the version and exit")
		transpileCommand  = flag.NewFlagSet("transpile", flag.ContinueOnError)
		verboseFlag       = transpileCommand.Bool("V", false, "print progress as comments")
		outputFlag        = transpileCommand.String("o", "", "output Go generated code to the specified file")
		packageFlag       = transpileCommand.String("p", "main", "set the name of the generated package")
		transpileHelpFlag = transpileCommand.Bool("h", false, "print help information")
		astCommand        = flag.NewFlagSet("ast", flag.ContinueOnError)
		astHelpFlag       = astCommand.Bool("h", false, "print help information")
	)

	flag.Usage = func() {
		usage := "Usage: %s [-v] [<command>] [<flags>] file.c\n\n"
		usage += "Commands:\n"
		usage += "  transpile\ttranspile an input C source file to Go\n"
		usage += "  ast\t\tprint AST before translated Go code\n\n"

		usage += "Flags:\n"
		fmt.Fprintf(os.Stderr, usage, os.Args[0])
		flag.PrintDefaults()
	}

	flag.Parse()

	if *versionFlag {
		// Simply print out the version and exit.
		fmt.Println(Version)
		return
	}

	if flag.NArg() < 1 {
		flag.Usage()
		os.Exit(1)
	}

	args := analyze.ProgramArgs{Verbose: *verboseFlag, Ast: false}

	switch os.Args[1] {
	case "ast":
		err := astCommand.Parse(os.Args[2:])
		if err != nil {
			fmt.Printf("Ast command cannot parse: %v", err)
			os.Exit(1)
		}

		if *astHelpFlag || astCommand.NArg() == 0 {
			fmt.Fprintf(os.Stderr, "Usage: %s ast file.c\n", os.Args[0])
			astCommand.PrintDefaults()
			os.Exit(1)
		}

		args.Ast = true
		args.InputFile = astCommand.Arg(0)

		if err = analyze.Start(args); err != nil {
			fmt.Printf("Error: %v", err)
			os.Exit(1)
		}
	case "transpile":
		err := transpileCommand.Parse(os.Args[2:])
		if err != nil {
			fmt.Printf("Transpile command cannot parse: %v", err)
			os.Exit(1)
		}

		if *transpileHelpFlag || transpileCommand.NArg() == 0 {
			fmt.Fprintf(os.Stderr, "Usage: %s transpile [-V] [-o file.go] [-p package] file.c\n", os.Args[0])
			transpileCommand.PrintDefaults()
			os.Exit(1)
		}

		args.InputFile = transpileCommand.Arg(0)
		args.OutputFile = *outputFlag
		args.PackageName = *packageFlag

		if err = analyze.Start(args); err != nil {
			fmt.Printf("Error: %v", err)
			os.Exit(1)
		}
	default:
		flag.Usage()
		os.Exit(1)
	}
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"fmt"
	"os"

	"github.com/disiqueira/tindergo"
)

func main() {
	token := flag.String("token", "", "Your Facebook Token.")

	flag.Parse()

	if *token == "" {
		fmt.Println("You must provide a valid Facebook Token.")
		os.Exit(2)
	}

	t := tindergo.New()

	err := t.Authenticate(*token)
	checkError(err)

	profile, err := t.Profile()
	checkError(err)

	fmt.Println("Your Profile:")
	fmt.Println("Name: " + profile.Name)
	fmt.Println("")

	var allRecs map[string]tindergo.RecsCoreUser
	var countRecs map[string]int

	for j := 0; j <= 3; j++ {

		recs, err := t.RecsCore()
		checkError(err)

		for _, elem := range recs {
			_, exist := allRecs[elem.ID]
			if exist {
				countRecs[elem.ID] = countRecs[elem.ID] + 1
			} else {
				countRecs[elem.ID] = 1
				allRecs[elem.ID] = elem
			}
		}
	}

	for i, e := range allRecs {
		if countRecs[i] > 2 {
			fmt.Println(e.Name, countRecs[i], float64((countRecs[i]*100)/4), "%")
		}
	}
}

// checkError Panic application if has an error returned.
func checkError(err error) {
	if err != nil {
		panic(err)
	}
}
<commit_msg>formating files<commit_after>package main

import (
	"flag"
	"fmt"
	"os"

	"github.com/disiqueira/tindergo"
)

func main() {
	token := flag.String("token", "", "Your Facebook Token.")

	flag.Parse()

	if *token == "" {
		fmt.Println("You must provide a valid Facebook Token.")
		os.Exit(2)
	}

	t := tindergo.New()

	err := t.Authenticate(*token)
	checkError(err)

	profile, err := t.Profile()
	checkError(err)

	fmt.Println("You:")
	fmt.Println("Name: " + profile.Name)
	fmt.Println("")

	var allRecs map[string]tindergo.RecsCoreUser
	var countRecs map[string]int

	for j := 0; j <= 3; j++ {
		recs, err := t.RecsCore()
		checkError(err)

		for _, elem := range recs {
			_, exist := allRecs[elem.ID]
			if exist {
				countRecs[elem.ID] = countRecs[elem.ID] + 1
			} else {
				countRecs[elem.ID] = 1
				allRecs[elem.ID] = elem
			}
		}
	}

	for i, e := range allRecs {
		if countRecs[i] > 2 {
			fmt.Println(e.Name, countRecs[i], float64((countRecs[i]*100)/4), "%")
		}
	}
}

// checkError Panic application if has an error returned.
func checkError(err error) {
	if err != nil {
		panic(err)
	}
}
<|endoftext|>


<commit_before>package main

import (
	"chukuparser/NLP/Util/Conll"
	"chukuparser/NLP/Util/TaggedSentence"
	"log"

	"chukuparser/Algorithm/Model/Perceptron"
	"chukuparser/Algorithm/Transition"
	"chukuparser/NLP"
	"chukuparser/NLP/Parser/Dependency"
	. "chukuparser/NLP/Parser/Dependency/Transition"
	"runtime"

	"fmt"
	"os"
)

var (
	RICH_FEATURES []string = []string{
		"S0|w|p", "S0|w", "S0|p", "N0|w|p",
		"N0|w", "N0|p", "N1|w|p", "N1|w",
		"N1|p", "N2|w|p", "N2|w", "N2|p",
		"S0|w|p+N0|w|p", "S0|w|p+N0|w",
		"S0|w+N0|w|p", "S0|w|p+N0|p",
		"S0|p+N0|w|p", "S0|w+N0|w",
		"S0|p+N0|p", "N0|p+N1|p",
		"N0|p+N1|p+N2|p", "S0|p+N0|p+N1|p",
		"S0h|p+S0|p+N0|p", "S0|p+S0l|p+N0|p",
		"S0|p+S0r|p+N0|p", "S0|p+N0|p+N0l|p",
		"S0|w|d", "S0|p|d", "N0|w|d", "N0|p|d",
		"S0|w+N0|w|d", "S0|p+N0|p|d",
		"S0|w|vr", "S0|p|vr", "S0|w|vl", "S0|p|vl", "N0|w|vl", "N0|p|vl",
		"S0h|w", "S0h|p", "S0|l", "S0l|w",
		"S0l|p", "S0l|l", "S0r|w", "S0r|p",
		"S0r|l", "N0l|w", "N0l|p", "N0l|l",
		"S0h2|w", "S0h2|p", "S0h|l", "S0l2|w",
		"S0l2|p", "S0l2|l", "S0r2|w", "S0r2|p",
		"S0r2|l", "N0l2|w", "N0l2|p", "N0l2|l",
		"S0|p+S0l|p+S0l2|p", "S0|p+S0r|p+S0r2|p",
		"S0|p+S0h|p+S0h2|p", "N0|p+N0l|p+N0l2|p",
		"S0|w|sr", "S0|p|sr", "S0|w|sl", "S0|p|sl",
		"N0|w|sl", "N0|p|sl"}

	LABELS []string = []string{
		"NMOD",
		"OBJ",
		"P",
		"PMOD",
		"ROOT",
		"SBAR",
		"SUB",
		"VC",
		"VMOD",
	}
)

func TrainingSequences(trainingSet []NLP.LabeledDependencyGraph, features []string) []Perceptron.DecodedInstance {
	extractor := new(GenericExtractor)
	// verify feature load
	for _, feature := range features {
		if err := extractor.LoadFeature(feature); err != nil {
			log.Panicln("Failed to load feature", err.Error())
		}
	}
	arcSystem := &ArcEager{}
	arcSystem.Relations = LABELS
	arcSystem.AddDefaultOracle()

	transitionSystem := Transition.TransitionSystem(arcSystem)
	deterministic := &Deterministic{transitionSystem, extractor, true, true, false}

	decoder := Perceptron.EarlyUpdateInstanceDecoder(deterministic)
	updater := new(Perceptron.AveragedStrategy)

	perceptron := &Perceptron.LinearPerceptron{Decoder: decoder, Updater: updater}
	perceptron.Init()
	tempModel := Dependency.ParameterModel(&PerceptronModel{perceptron})

	instances := make([]Perceptron.DecodedInstance, len(trainingSet))
	for i, graph := range trainingSet {
		sent := graph.TaggedSentence()
		_, goldParams := deterministic.ParseOracle(graph, nil, tempModel)
		seq := goldParams.(*ParseResultParameters).Sequence
		decoded := &Perceptron.Decoded{sent, seq[0]}
		instances[i] = decoded
	}
	return instances
}

func Train(trainingSet []Perceptron.DecodedInstance, iterations, beamSize int, features []string) *Perceptron.LinearPerceptron {
	extractor := new(GenericExtractor)
	// verify feature load
	for _, feature := range features {
		if err := extractor.LoadFeature(feature); err != nil {
			log.Panicln("Failed to load feature", err.Error())
		}
	}
	arcSystem := &ArcEager{}
	arcSystem.Relations = LABELS
	arcSystem.AddDefaultOracle()

	transitionSystem := Transition.TransitionSystem(arcSystem)
	conf := DependencyConfiguration(new(SimpleConfiguration))

	beam := &Beam{
		TransFunc:      transitionSystem,
		FeatExtractor:  extractor,
		Base:           conf,
		NumRelations:   len(arcSystem.Relations),
		Size:           beamSize,
		ConcurrentExec: true}
	decoder := Perceptron.EarlyUpdateInstanceDecoder(beam)
	updater := new(Perceptron.AveragedStrategy)

	perceptron := &Perceptron.LinearPerceptron{Decoder: decoder, Updater: updater}
	perceptron.Init()
	perceptron.Log = true

	perceptron.Iterations = iterations

	perceptron.Train(trainingSet)

	return perceptron
}

func Parse(sents []NLP.TaggedSentence, beamSize int, model Dependency.ParameterModel, features []string) []NLP.LabeledDependencyGraph {
	extractor := new(GenericExtractor)
	// verify load
	for _, feature := range features {
		if err := extractor.LoadFeature(feature); err != nil {
			log.Panicln("Failed to load feature", err.Error())
		}
	}
	arcSystem := &ArcEager{}
	arcSystem.Relations = LABELS
	arcSystem.AddDefaultOracle()
	transitionSystem := Transition.TransitionSystem(arcSystem)

	conf := DependencyConfiguration(new(SimpleConfiguration))

	beam := &Beam{
		TransFunc:      transitionSystem,
		FeatExtractor:  extractor,
		Base:           conf,
		Size:           beamSize,
		NumRelations:   len(arcSystem.Relations),
		Model:          model,
		ConcurrentExec: true}

	parsedGraphs := make([]NLP.LabeledDependencyGraph, len(sents))
	for i, sent := range sents {
		log.Println("Parsing sent", i)
		graph, _ := beam.Parse(sent, nil, model)
		labeled := graph.(NLP.LabeledDependencyGraph)
		fmt.Println(labeled.(*SimpleConfiguration).Nodes)
		fmt.Println(labeled.(*SimpleConfiguration).Arcs())
		parsedGraphs[i] = labeled
	}
	return parsedGraphs
}

func WriteModel(model Perceptron.Model, filename string) {
	file, err := os.Create(filename)
	defer file.Close()
	if err != nil {
		panic(err)
	}
	model.Write(file)
}

func ReadModel(filename string) *Perceptron.LinearPerceptron {
	file, err := os.Open(filename)
	defer file.Close()
	if err != nil {
		panic(err)
	}
	model := new(Perceptron.LinearPerceptron)
	model.Read(file)
	return model
}

func main() {
	trainFile := "devr1.conll"
	inputFile := "devi1.txt"
	outputFile := "devo1.conll"
	iterations := 32
	beamSize := 32
	modelFile := fmt.Sprintf("model.b%d.i%d", beamSize, iterations)
	log.SetFlags(log.LstdFlags | log.Lmicroseconds)
	runtime.GOMAXPROCS(runtime.NumCPU())

	s, e := Conll.ReadFile(trainFile)
	log.Println("Read", len(s), "sentences from", trainFile)
	goldGraphs := Conll.Conll2GraphCorpus(s)
	log.Println("Converted from conll to internal format")
	if e != nil {
		log.Println(e)
		return
	}
	log.Println("Parsing with gold to get training sequences")
	goldSequences := TrainingSequences(goldGraphs, RICH_FEATURES)
	log.Println("Training")
	model := Train(goldSequences, iterations, beamSize, RICH_FEATURES)

	log.Println("Writing model to", modelFile)
	WriteModel(model, modelFile)
	// model := ReadModel(modelFile)
	// log.Println("Read model from", modelFile)
	sents, e2 := TaggedSentence.ReadFile(inputFile)
	log.Println("Read", len(sents), "from", inputFile)
	if e2 != nil {
		log.Println(e2)
		return
	}

	log.Print("Parsing")
	parsedGraphs := Parse(sents, beamSize, Dependency.ParameterModel(&PerceptronModel{model}), RICH_FEATURES)
	log.Println("Converted to conll")
	graphAsConll := Conll.Graph2ConllCorpus(parsedGraphs)
	log.Println("Wrote", len(graphAsConll), "in conll format to", outputFile)
	Conll.WriteFile(outputFile, graphAsConll)
}
<commit_msg>Final fix: used the wrong rel labels<commit_after>package main

import (
	"chukuparser/NLP/Util/Conll"
	"chukuparser/NLP/Util/TaggedSentence"
	"log"

	"chukuparser/Algorithm/Model/Perceptron"
	"chukuparser/Algorithm/Transition"
	"chukuparser/NLP"
	"chukuparser/NLP/Parser/Dependency"
	. "chukuparser/NLP/Parser/Dependency/Transition"
	"runtime"

	"fmt"
	"os"
)

var (
	RICH_FEATURES []string = []string{
		"S0|w|p", "S0|w", "S0|p", "N0|w|p",
		"N0|w", "N0|p", "N1|w|p", "N1|w",
		"N1|p", "N2|w|p", "N2|w", "N2|p",
		"S0|w|p+N0|w|p", "S0|w|p+N0|w",
		"S0|w+N0|w|p", "S0|w|p+N0|p",
		"S0|p+N0|w|p", "S0|w+N0|w",
		"S0|p+N0|p", "N0|p+N1|p",
		"N0|p+N1|p+N2|p", "S0|p+N0|p+N1|p",
		"S0h|p+S0|p+N0|p", "S0|p+S0l|p+N0|p",
		"S0|p+S0r|p+N0|p", "S0|p+N0|p+N0l|p",
		"S0|w|d", "S0|p|d", "N0|w|d", "N0|p|d",
		"S0|w+N0|w|d", "S0|p+N0|p|d",
		"S0|w|vr", "S0|p|vr", "S0|w|vl", "S0|p|vl", "N0|w|vl", "N0|p|vl",
		"S0h|w", "S0h|p", "S0|l", "S0l|w",
		"S0l|p", "S0l|l", "S0r|w", "S0r|p",
		"S0r|l", "N0l|w", "N0l|p", "N0l|l",
		"S0h2|w", "S0h2|p", "S0h|l", "S0l2|w",
		"S0l2|p", "S0l2|l", "S0r2|w", "S0r2|p",
		"S0r2|l", "N0l2|w", "N0l2|p", "N0l2|l",
		"S0|p+S0l|p+S0l2|p", "S0|p+S0r|p+S0r2|p",
		"S0|p+S0h|p+S0h2|p", "N0|p+N0l|p+N0l2|p",
		"S0|w|sr", "S0|p|sr", "S0|w|sl", "S0|p|sl",
		"N0|w|sl", "N0|p|sl"}

	LABELS []string = []string{
		"AMOD",
		"DEP",
		"NMOD",
		"OBJ",
		"P",
		"PMOD",
		"PRD",
		"ROOT",
		"SBAR",
		"SUB",
		"VC",
		"VMOD",
	}
)

func TrainingSequences(trainingSet []NLP.LabeledDependencyGraph, features []string) []Perceptron.DecodedInstance {
	extractor := new(GenericExtractor)
	// verify feature load
	for _, feature := range features {
		if err := extractor.LoadFeature(feature); err != nil {
			log.Panicln("Failed to load feature", err.Error())
		}
	}
	arcSystem := &ArcEager{}
	arcSystem.Relations = LABELS
	arcSystem.AddDefaultOracle()

	transitionSystem := Transition.TransitionSystem(arcSystem)
	deterministic := &Deterministic{transitionSystem, extractor, true, true, false}

	decoder := Perceptron.EarlyUpdateInstanceDecoder(deterministic)
	updater := new(Perceptron.AveragedStrategy)

	perceptron := &Perceptron.LinearPerceptron{Decoder: decoder, Updater: updater}
	perceptron.Init()
	tempModel := Dependency.ParameterModel(&PerceptronModel{perceptron})

	instances := make([]Perceptron.DecodedInstance, len(trainingSet))
	for i, graph := range trainingSet {
		sent := graph.TaggedSentence()
		_, goldParams := deterministic.ParseOracle(graph, nil, tempModel)
		seq := goldParams.(*ParseResultParameters).Sequence
		decoded := &Perceptron.Decoded{sent, seq[0]}
		instances[i] = decoded
	}
	return instances
}

func Train(trainingSet []Perceptron.DecodedInstance, iterations, beamSize int, features []string) *Perceptron.LinearPerceptron {
	extractor := new(GenericExtractor)
	// verify feature load
	for _, feature := range features {
		if err := extractor.LoadFeature(feature); err != nil {
			log.Panicln("Failed to load feature", err.Error())
		}
	}
	arcSystem := &ArcEager{}
	arcSystem.Relations = LABELS
	arcSystem.AddDefaultOracle()

	transitionSystem := Transition.TransitionSystem(arcSystem)
	conf := DependencyConfiguration(new(SimpleConfiguration))

	beam := &Beam{
		TransFunc:      transitionSystem,
		FeatExtractor:  extractor,
		Base:           conf,
		NumRelations:   len(arcSystem.Relations),
		Size:           beamSize,
		ConcurrentExec: true}
	decoder := Perceptron.EarlyUpdateInstanceDecoder(beam)
	updater := new(Perceptron.AveragedStrategy)

	perceptron := &Perceptron.LinearPerceptron{Decoder: decoder, Updater: updater}
	perceptron.Init()
	perceptron.Log = true

	perceptron.Iterations = iterations

	perceptron.Train(trainingSet)

	return perceptron
}

func Parse(sents []NLP.TaggedSentence, beamSize int, model Dependency.ParameterModel, features []string) []NLP.LabeledDependencyGraph {
	extractor := new(GenericExtractor)
	// verify load
	for _, feature := range features {
		if err := extractor.LoadFeature(feature); err != nil {
			log.Panicln("Failed to load feature", err.Error())
		}
	}
	arcSystem := &ArcEager{}
	arcSystem.Relations = LABELS
	arcSystem.AddDefaultOracle()
	transitionSystem := Transition.TransitionSystem(arcSystem)

	conf := DependencyConfiguration(new(SimpleConfiguration))

	beam := &Beam{
		TransFunc:      transitionSystem,
		FeatExtractor:  extractor,
		Base:           conf,
		Size:           beamSize,
		NumRelations:   len(arcSystem.Relations),
		Model:          model,
		ConcurrentExec: true}

	parsedGraphs := make([]NLP.LabeledDependencyGraph, len(sents))
	for i, sent := range sents {
		log.Println("Parsing sent", i)
		graph, _ := beam.Parse(sent, nil, model)
		labeled := graph.(NLP.LabeledDependencyGraph)
		parsedGraphs[i] = labeled
	}
	return parsedGraphs
}

func WriteModel(model Perceptron.Model, filename string) {
	file, err := os.Create(filename)
	defer file.Close()
	if err != nil {
		panic(err)
	}
	model.Write(file)
}

func ReadModel(filename string) *Perceptron.LinearPerceptron {
	file, err := os.Open(filename)
	defer file.Close()
	if err != nil {
		panic(err)
	}
	model := new(Perceptron.LinearPerceptron)
	model.Read(file)
	return model
}

func main() {
	trainFile := "train.conll"
	inputFile := "devi.txt"
	outputFile := "devo.conll"
	iterations := 20
	beamSize := 64
	modelFile := fmt.Sprintf("model.b%d.i%d", beamSize, iterations)
	log.SetFlags(log.LstdFlags | log.Lmicroseconds)
	runtime.GOMAXPROCS(runtime.NumCPU())

	s, e := Conll.ReadFile(trainFile)
	log.Println("Read", len(s), "sentences from", trainFile)
	goldGraphs := Conll.Conll2GraphCorpus(s)
	log.Println("Converted from conll to internal format")
	if e != nil {
		log.Println(e)
		return
	}
	log.Println("Parsing with gold to get training sequences")
	goldSequences := TrainingSequences(goldGraphs, RICH_FEATURES)
	log.Println("Training")
	model := Train(goldSequences, iterations, beamSize, RICH_FEATURES)

	log.Println("Writing model to", modelFile)
	WriteModel(model, modelFile)
	// model := ReadModel(modelFile)
	// log.Println("Read model from", modelFile)
	sents, e2 := TaggedSentence.ReadFile(inputFile)
	log.Println("Read", len(sents), "from", inputFile)
	if e2 != nil {
		log.Println(e2)
		return
	}

	log.Print("Parsing")
	parsedGraphs := Parse(sents, beamSize, Dependency.ParameterModel(&PerceptronModel{model}), RICH_FEATURES)
	log.Println("Converted to conll")
	graphAsConll := Conll.Graph2ConllCorpus(parsedGraphs)
	log.Println("Wrote", len(graphAsConll), "in conll format to", outputFile)
	Conll.WriteFile(outputFile, graphAsConll)
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"github.com/droundy/goopt"
	. "github.com/lkwg82/automatic-maven-pom-upgrade/lib"
	"log"
	"os"
)

var optVerbose = goopt.Flag([]string{
	"-v", "--verbose"},
	[]string{"--quiet"}, "output verbosely",
	"be quiet, instead")

var optType = goopt.Alternatives([]string{"--type"}, []string{"help", "parent"}, "type of upgrade")

func init() {
	log.SetOutput(os.Stdout)
	log.SetFlags(log.Lshortfile)
}

func main() {
	parseParameter()

	gitLog, _ := os.Create("git.log")
	git := NewGit(gitLog)

	assert(git.IsInstalled(), "need git to be installed or in the PATH")
	assert(git.HasRepo(), "need called from a directory, which has a repository")
	assert(!git.IsDirty(), "repository is dirty, plz commit or reset")

	mavenLog, _ := os.Create("maven.log")
	maven := NewMaven(mavenLog)
	err := maven.DetermineCommand()
	if err != nil {
		log.Fatal(err)
	}

	if *optType == "parent" {
		if message, err := maven.UpdateParent(); err != nil {
			git.CommitMessage = message
		}
	}

	git.Commit()
}

func assert(status bool, hint string) {
	if (!status) {
		log.Fatal("ERROR: " + hint)
	}
}

func parseParameter() {
	goopt.Summary = "automatic upgrade maven projects"
	goopt.Version = "0.1"
	goopt.Parse(nil)

	if *optType == "help" {
		fmt.Print(goopt.Usage())
		os.Exit(0)
	}
}
<commit_msg>cleanup<commit_after>package main

import (
	"fmt"
	"github.com/droundy/goopt"
	. "github.com/lkwg82/automatic-maven-pom-upgrade/lib"
	"log"
	"os"
)

var optVerbose = goopt.Flag([]string{
	"-v", "--verbose"},
	[]string{"--quiet"}, "output verbosely",
	"be quiet, instead")

var optType = goopt.Alternatives([]string{"--type"}, []string{"help", "parent"}, "type of upgrade")

func init() {
	log.SetOutput(os.Stdout)
	log.SetFlags(log.Lshortfile)
}

func main() {
	parseParameter()

	if *optType == "help" {
		fmt.Print(goopt.Usage())
		os.Exit(0)
	}

	gitLog, _ := os.Create("git.log")
	git := NewGit(gitLog)

	assert(git.IsInstalled(), "need git to be installed or in the PATH")
	assert(git.HasRepo(), "need called from a directory, which has a repository")
	assert(!git.IsDirty(), "repository is dirty, plz commit or reset")

	mavenLog, _ := os.Create("maven.log")
	maven := NewMaven(mavenLog)
	err := maven.DetermineCommand()
	if err != nil {
		log.Fatal(err)
	}

	if *optType == "parent" {
		if message, err := maven.UpdateParent(); err != nil {
			git.CommitMessage = message
		}
	}

	// git.Commit()
}

func assert(status bool, hint string) {
	if (!status) {
		log.Fatal("ERROR: " + hint)
	}
}

func parseParameter() {
	goopt.Summary = "automatic upgrade maven projects"
	goopt.Version = "0.1"
	goopt.Parse(nil)
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"fmt"
	"os"
	"os/exec"
	"os/signal"
	"path"
	"strings"
	"syscall"
	"time"

	log "github.com/golang/glog"
	"github.com/openshift/openshift-sdn/ovs-simple/controller"
	"github.com/openshift/openshift-sdn/pkg/registry"
)

type CmdLineOpts struct {
	etcdEndpoints string
	etcdPath      string
	etcdKeyfile   string
	etcdCertfile  string
	etcdCAFile    string
	ip            string
	hostname      string
	master        bool
	minion        bool
	skipsetup     bool
	sync          bool
	help          bool
}

var opts CmdLineOpts

func init() {
	flag.StringVar(&opts.etcdEndpoints, "etcd-endpoints", "http://127.0.0.1:4001", "a comma-delimited list of etcd endpoints")
	flag.StringVar(&opts.etcdPath, "etcd-path", "/registry/sdn/", "etcd path")
	flag.StringVar(&opts.etcdKeyfile, "etcd-keyfile", "", "SSL key file used to secure etcd communication")
	flag.StringVar(&opts.etcdCertfile, "etcd-certfile", "", "SSL certification file used to secure etcd communication")
	flag.StringVar(&opts.etcdCAFile, "etcd-cafile", "", "SSL Certificate Authority file used to secure etcd communication")

	flag.StringVar(&opts.ip, "public-ip", "", "Publicly reachable IP address of this host (for node mode).")
	flag.StringVar(&opts.hostname, "hostname", "", "Hostname as registered with master (for node mode), will default to 'hostname -f'")

	flag.BoolVar(&opts.master, "master", true, "Run in master mode")
	flag.BoolVar(&opts.minion, "minion", false, "Run in minion mode")
	flag.BoolVar(&opts.skipsetup, "skip-setup", false, "Skip the setup when in minion mode")
	flag.BoolVar(&opts.sync, "sync", false, "Sync the minions directly to etcd-path (Do not wait for PaaS to do so!)")

	flag.BoolVar(&opts.help, "help", false, "print this message")
}

func newNetworkManager() controller.Controller {
	sub := newSubnetRegistry()
	fqdn := opts.hostname
	if fqdn == "" {
		fqdn_bytes, _ := exec.Command("hostname", "-f").CombinedOutput()
		fqdn = strings.TrimSpace(string(fqdn_bytes))
	}
	return controller.NewController(sub, string(fqdn), opts.ip)
}

func newSubnetRegistry() registry.SubnetRegistry {
	peers := strings.Split(opts.etcdEndpoints, ",")

	subnetPath := path.Join(opts.etcdPath + "subnets")
	minionPath := "/registry/minions/"
	if opts.sync {
		minionPath = path.Join(opts.etcdPath + "minions")
	}

	cfg := &registry.EtcdConfig{
		Endpoints:  peers,
		Keyfile:    opts.etcdKeyfile,
		Certfile:   opts.etcdCertfile,
		CAFile:     opts.etcdCAFile,
		SubnetPath: subnetPath,
		MinionPath: minionPath,
	}

	for {
		esr, err := registry.NewEtcdSubnetRegistry(cfg)
		if err == nil {
			return esr
		}

		log.Error("Failed to create SubnetRegistry: %v ", err)
		time.Sleep(time.Second)
	}
}

func main() {
	// glog will log to tmp files by default. override so all entries
	// can flow into journald (if running under systemd)
	flag.Set("logtostderr", "true")

	// now parse command line args
	flag.Parse()

	if opts.help {
		fmt.Fprintf(os.Stderr, "Usage: %s [OPTION]...\n", os.Args[0])
		flag.PrintDefaults()
		os.Exit(0)
	}

	// Register for SIGINT and SIGTERM and wait for one of them to arrive
	log.Info("Installing signal handlers")
	sigs := make(chan os.Signal, 1)
	signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)

	be := newNetworkManager()
	if opts.minion {
		err := be.StartNode(opts.sync, opts.skipsetup)
		if err != nil {
			return
		}
	} else if opts.master {
		err := be.StartMaster(opts.sync)
		if err != nil {
			log.Errorf(err.Error())
			return
		}
	}

	select {
	case <-sigs:
		// unregister to get default OS nuke behaviour in case we don't exit cleanly
		signal.Stop(sigs)

		log.Info("Exiting...")
		be.Stop()
	}
}
<commit_msg>Just use Error when no formatting is needed<commit_after>package main

import (
	"flag"
	"fmt"
	"os"
	"os/exec"
	"os/signal"
	"path"
	"strings"
	"syscall"
	"time"

	log "github.com/golang/glog"
	"github.com/openshift/openshift-sdn/ovs-simple/controller"
	"github.com/openshift/openshift-sdn/pkg/registry"
)

type CmdLineOpts struct {
	etcdEndpoints string
	etcdPath      string
	etcdKeyfile   string
	etcdCertfile  string
	etcdCAFile    string
	ip            string
	hostname      string
	master        bool
	minion        bool
	skipsetup     bool
	sync          bool
	help          bool
}

var opts CmdLineOpts

func init() {
	flag.StringVar(&opts.etcdEndpoints, "etcd-endpoints", "http://127.0.0.1:4001", "a comma-delimited list of etcd endpoints")
	flag.StringVar(&opts.etcdPath, "etcd-path", "/registry/sdn/", "etcd path")
	flag.StringVar(&opts.etcdKeyfile, "etcd-keyfile", "", "SSL key file used to secure etcd communication")
	flag.StringVar(&opts.etcdCertfile, "etcd-certfile", "", "SSL certification file used to secure etcd communication")
	flag.StringVar(&opts.etcdCAFile, "etcd-cafile", "", "SSL Certificate Authority file used to secure etcd communication")

	flag.StringVar(&opts.ip, "public-ip", "", "Publicly reachable IP address of this host (for node mode).")
	flag.StringVar(&opts.hostname, "hostname", "", "Hostname as registered with master (for node mode), will default to 'hostname -f'")

	flag.BoolVar(&opts.master, "master", true, "Run in master mode")
	flag.BoolVar(&opts.minion, "minion", false, "Run in minion mode")
	flag.BoolVar(&opts.skipsetup, "skip-setup", false, "Skip the setup when in minion mode")
	flag.BoolVar(&opts.sync, "sync", false, "Sync the minions directly to etcd-path (Do not wait for PaaS to do so!)")

	flag.BoolVar(&opts.help, "help", false, "print this message")
}

func newNetworkManager() controller.Controller {
	sub := newSubnetRegistry()
	fqdn := opts.hostname
	if fqdn == "" {
		fqdn_bytes, _ := exec.Command("hostname", "-f").CombinedOutput()
		fqdn = strings.TrimSpace(string(fqdn_bytes))
	}
	return controller.NewController(sub, string(fqdn), opts.ip)
}

func newSubnetRegistry() registry.SubnetRegistry {
	peers := strings.Split(opts.etcdEndpoints, ",")

	subnetPath := path.Join(opts.etcdPath + "subnets")
	minionPath := "/registry/minions/"
	if opts.sync {
		minionPath = path.Join(opts.etcdPath + "minions")
	}

	cfg := &registry.EtcdConfig{
		Endpoints:  peers,
		Keyfile:    opts.etcdKeyfile,
		Certfile:   opts.etcdCertfile,
		CAFile:     opts.etcdCAFile,
		SubnetPath: subnetPath,
		MinionPath: minionPath,
	}

	for {
		esr, err := registry.NewEtcdSubnetRegistry(cfg)
		if err == nil {
			return esr
		}

		log.Error("Failed to create SubnetRegistry: %v ", err)
		time.Sleep(time.Second)
	}
}

func main() {
	// glog will log to tmp files by default. override so all entries
	// can flow into journald (if running under systemd)
	flag.Set("logtostderr", "true")

	// now parse command line args
	flag.Parse()

	if opts.help {
		fmt.Fprintf(os.Stderr, "Usage: %s [OPTION]...\n", os.Args[0])
		flag.PrintDefaults()
		os.Exit(0)
	}

	// Register for SIGINT and SIGTERM and wait for one of them to arrive
	log.Info("Installing signal handlers")
	sigs := make(chan os.Signal, 1)
	signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)

	be := newNetworkManager()
	if opts.minion {
		err := be.StartNode(opts.sync, opts.skipsetup)
		if err != nil {
			return
		}
	} else if opts.master {
		err := be.StartMaster(opts.sync)
		if err != nil {
			log.Error(err)
			return
		}
	}

	select {
	case <-sigs:
		// unregister to get default OS nuke behaviour in case we don't exit cleanly
		signal.Stop(sigs)

		log.Info("Exiting...")
		be.Stop()
	}
}
<|endoftext|>


<commit_before>package main

import (
	"os"
	"strings"

	"github.com/cloudfoundry/cli/cf/terminal"
	"github.com/cloudfoundry/cli/cf/trace"
	"github.com/cloudfoundry/cli/plugin"
	"github.com/kkellner/cloudfoundry-top-plugin/top"
	"github.com/kkellner/cloudfoundry-top-plugin/util"
	"github.com/simonleung8/flags"
)

type TopCmd struct {
	ui terminal.UI
}

func (c *TopCmd) GetMetadata() plugin.PluginMetadata {
	return plugin.PluginMetadata{
		Name: "top",
		Version: plugin.VersionType{
			Major: 0,
			Minor: 6,
			Build: 5,
		},
		MinCliVersion: plugin.VersionType{
			Major: 6,
			Minor: 17,
			Build: 0,
		},
		Commands: []plugin.Command{
			{
				Name:     "top",
				HelpText: "Displays top stats - by Kurt Kellner of ECS Team",
				UsageDetails: plugin.Usage{
					Usage: "cf top",
					Options: map[string]string{
						"debug":   "-d, enable debugging",
						"cygwin":  "-c, force run under cygwin (Use this to run: 'cmd /c start cf top -cygwin' )",
						"nozzles": "-n, specify the number of nozzle instances (default: 2)",
					},
				},
			},
		},
	}
}

func main() {
	plugin.Start(new(TopCmd))
}

func (c *TopCmd) Run(cliConnection plugin.CliConnection, args []string) {
	var options *top.ClientOptions

	traceLogger := trace.NewLogger(os.Stdout, true, os.Getenv("CF_TRACE"), "")
	c.ui = terminal.NewUI(os.Stdin, os.Stdout, terminal.NewTeePrinter(os.Stdout), traceLogger)

	switch args[0] {
	case "top":
		options = c.buildClientOptions(args)
	case "app-top":
		options = c.buildClientOptions(args)
		appModel, err := cliConnection.GetApp(args[1])
		if err != nil {
			c.ui.Warn(err.Error())
			return
		}
		options.AppGUID = appModel.Guid
	default:
		return
	}

	cfTrace := os.Getenv("CF_TRACE")
	if strings.ToLower(cfTrace) == "true" {
		c.ui.Failed("The cf top plugin will not run with CF_TRACE environment variable set to true")
		return
	}

	if !options.Cygwin && util.IsCygwin() {
		c.ui.Failed("The cf top plugin will not run under cygwin.  Use this to run: 'cmd /c start cf top -cygwin'")
		return
	}

	/***********************************************************
	Trying to find a way to detect cygwin but not detect cygwin if cmd.exe is spawned from cygwin

	values := os.Environ()
	for _, v := range values  {
		fmt.Printf("value: [%v]\n", v)
	}

	fmt.Printf("Separator: [%v]\n", os.PathSeparator)
	fmt.Printf("PathListSeparator: [%v]\n", os.PathListSeparator)
	fmt.Printf("Geteuid: %v\n", os.Geteuid())
	fmt.Printf("Getppid: %v\n", os.Getppid())
	p, e := os.FindProcess(os.Getppid())
	fmt.Printf("Getppid: %+v [Err:%v]\n", p, e)

	if strings.Contains(strings.ToLower(os.Getenv("OS")), "windows") {
		shell := os.Getenv("SHELL")
		if len(shell) > 0 {
			c.ui.Failed("The cf top plugin will not run under cygwin.  Use this to run: 'cmd /c start cf top'")
			return
		}
	}
	fmt.Printf("runtime.GOOS: [%v]\n", runtime.GOOS)
	*/

	/*
		if strings.ToLower(osType) == "cygwin" {
			c.ui.Failed("The cf top plugin will not run under cygwin.  Use this to run: 'cmd /c start cf top'")
			return
		}
	*/

	client := top.NewClient(cliConnection, options, c.ui)
	client.Start()
}

func (c *TopCmd) buildClientOptions(args []string) *top.ClientOptions {
	var debug bool
	var cygwin bool
	var nozzles int

	fc := flags.New()
	fc.NewBoolFlag("debug", "d", "used for debugging")
	fc.NewBoolFlag("cygwin", "c", "force run under cygwin (Use this to run: 'cmd /c start cf top -cygwin' )")
	fc.NewIntFlagWithDefault("nozzles", "n", "number of nozzles", 2)
	//fc.NewStringFlag("filter", "f", "specify message filter such as LogMessage, ValueMetric, CounterEvent, HttpStartStop")
	err := fc.Parse(args[1:]...)

	if err != nil {
		c.ui.Failed(err.Error())
	}
	if fc.IsSet("debug") {
		debug = fc.Bool("debug")
	}
	if fc.IsSet("cygwin") {
		cygwin = fc.Bool("cygwin")
	}

	nozzles = fc.Int("nozzles")

	/*
		if fc.IsSet("filter") {
			filter = fc.String("filter")
		}
	*/
	return &top.ClientOptions{
		Debug:   debug,
		Cygwin:  cygwin,
		Nozzles: nozzles,
	}
}
<commit_msg>Build version v0.6.6<commit_after>package main

import (
	"os"
	"strings"

	"github.com/cloudfoundry/cli/cf/terminal"
	"github.com/cloudfoundry/cli/cf/trace"
	"github.com/cloudfoundry/cli/plugin"
	"github.com/kkellner/cloudfoundry-top-plugin/top"
	"github.com/kkellner/cloudfoundry-top-plugin/util"
	"github.com/simonleung8/flags"
)

type TopCmd struct {
	ui terminal.UI
}

func (c *TopCmd) GetMetadata() plugin.PluginMetadata {
	return plugin.PluginMetadata{
		Name: "top",
		Version: plugin.VersionType{
			Major: 0,
			Minor: 6,
			Build: 6,
		},
		MinCliVersion: plugin.VersionType{
			Major: 6,
			Minor: 17,
			Build: 0,
		},
		Commands: []plugin.Command{
			{
				Name:     "top",
				HelpText: "Displays top stats - by Kurt Kellner of ECS Team",
				UsageDetails: plugin.Usage{
					Usage: "cf top",
					Options: map[string]string{
						"debug":   "-d, enable debugging",
						"cygwin":  "-c, force run under cygwin (Use this to run: 'cmd /c start cf top -cygwin' )",
						"nozzles": "-n, specify the number of nozzle instances (default: 2)",
					},
				},
			},
		},
	}
}

func main() {
	plugin.Start(new(TopCmd))
}

func (c *TopCmd) Run(cliConnection plugin.CliConnection, args []string) {
	var options *top.ClientOptions

	traceLogger := trace.NewLogger(os.Stdout, true, os.Getenv("CF_TRACE"), "")
	c.ui = terminal.NewUI(os.Stdin, os.Stdout, terminal.NewTeePrinter(os.Stdout), traceLogger)

	switch args[0] {
	case "top":
		options = c.buildClientOptions(args)
	case "app-top":
		options = c.buildClientOptions(args)
		appModel, err := cliConnection.GetApp(args[1])
		if err != nil {
			c.ui.Warn(err.Error())
			return
		}
		options.AppGUID = appModel.Guid
	default:
		return
	}

	cfTrace := os.Getenv("CF_TRACE")
	if strings.ToLower(cfTrace) == "true" {
		c.ui.Failed("The cf top plugin will not run with CF_TRACE environment variable set to true")
		return
	}

	if !options.Cygwin && util.IsCygwin() {
		c.ui.Failed("The cf top plugin will not run under cygwin.  Use this to run: 'cmd /c start cf top -cygwin'")
		return
	}

	/***********************************************************
	Trying to find a way to detect cygwin but not detect cygwin if cmd.exe is spawned from cygwin

	values := os.Environ()
	for _, v := range values  {
		fmt.Printf("value: [%v]\n", v)
	}

	fmt.Printf("Separator: [%v]\n", os.PathSeparator)
	fmt.Printf("PathListSeparator: [%v]\n", os.PathListSeparator)
	fmt.Printf("Geteuid: %v\n", os.Geteuid())
	fmt.Printf("Getppid: %v\n", os.Getppid())
	p, e := os.FindProcess(os.Getppid())
	fmt.Printf("Getppid: %+v [Err:%v]\n", p, e)

	if strings.Contains(strings.ToLower(os.Getenv("OS")), "windows") {
		shell := os.Getenv("SHELL")
		if len(shell) > 0 {
			c.ui.Failed("The cf top plugin will not run under cygwin.  Use this to run: 'cmd /c start cf top'")
			return
		}
	}
	fmt.Printf("runtime.GOOS: [%v]\n", runtime.GOOS)
	*/

	/*
		if strings.ToLower(osType) == "cygwin" {
			c.ui.Failed("The cf top plugin will not run under cygwin.  Use this to run: 'cmd /c start cf top'")
			return
		}
	*/

	client := top.NewClient(cliConnection, options, c.ui)
	client.Start()
}

func (c *TopCmd) buildClientOptions(args []string) *top.ClientOptions {
	var debug bool
	var cygwin bool
	var nozzles int

	fc := flags.New()
	fc.NewBoolFlag("debug", "d", "used for debugging")
	fc.NewBoolFlag("cygwin", "c", "force run under cygwin (Use this to run: 'cmd /c start cf top -cygwin' )")
	fc.NewIntFlagWithDefault("nozzles", "n", "number of nozzles", 2)
	//fc.NewStringFlag("filter", "f", "specify message filter such as LogMessage, ValueMetric, CounterEvent, HttpStartStop")
	err := fc.Parse(args[1:]...)

	if err != nil {
		c.ui.Failed(err.Error())
	}
	if fc.IsSet("debug") {
		debug = fc.Bool("debug")
	}
	if fc.IsSet("cygwin") {
		cygwin = fc.Bool("cygwin")
	}

	nozzles = fc.Int("nozzles")

	/*
		if fc.IsSet("filter") {
			filter = fc.String("filter")
		}
	*/
	return &top.ClientOptions{
		Debug:   debug,
		Cygwin:  cygwin,
		Nozzles: nozzles,
	}
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"log"
	"net"
	"strings"
	"sync"

	"github.com/UniversityRadioYork/baps3-go"
	"github.com/docopt/docopt-go"
	_ "github.com/lib/pq"
)

type Request struct {
	contents *baps3.Message
	response chan<- *baps3.Message
}

func main() {
	usage := `trackd - track resolving server for BAPS3

Usage:
    trackd HOSTPORT

Options:
    HOSTPORT       The host and port on which trackd should listen (host:port).
    -h, --help     Show this message.
    -v, --version  Show version.
`
	arguments, err := docopt.Parse(usage, nil, true, "trackd 0.0", true)
	if err != nil {
		log.Fatal(err)
	}

	ln, err := net.Listen("tcp", arguments["HOSTPORT"].(string))
	if err != nil {
		log.Fatal(err)
	}
	defer func() {
		if err := ln.Close(); err != nil {
			log.Fatal(err)
		}
	}()

	cpQuit := make(chan struct{})

	var wg sync.WaitGroup

	clientPoolHandle := NewClientPool(cpQuit)
	wg.Add(1)
	go clientPoolHandle.Pool.Run(&wg)

	requests := make(chan *Request)

	wg.Add(1)

	go AcceptLoop(ln, requests, &clientPoolHandle, &wg)

	RequestLoop(requests, clientPoolHandle.Broadcast, &wg)
	log.Println("main loop closing")

	// The client pool will tell all the connected clients to quit.
	cpQuit <- struct{}{}
	log.Println("main loop sent quit signal to client pool")

	// To close the accept loop, we have to kill off the acceptor.
	if err := ln.Close(); err != nil {
		log.Fatal(err)
	}

	wg.Wait()
	log.Println("trackd closing")
}

func AcceptLoop(ln net.Listener, requests chan<- *Request, clientPoolHandle *ClientPoolHandle, wg *sync.WaitGroup) {
	if wg != nil {
		defer wg.Done()
	}
	defer func() { log.Println("accept loop closing") }()

	for {
		conn, err := ln.Accept()
		if err != nil {
			log.Println(err)
			break
		}

		// Two goroutines: read and write
		wg.Add(2)
		go handleConnection(conn, requests, clientPoolHandle, wg)
	}
}

func RequestLoop(requests <-chan *Request, broadcast chan<- *baps3.Message, wg *sync.WaitGroup) {
	db, err := getDB()
	if err != nil {
		log.Fatal(err)
	}
	defer func() {
		if err := db.Close(); err != nil {
			log.Fatal(err)
		}
	}()

	t := NewTrackDB(db, `M:\%d\%d`)

	for {
		select {
		case r, more := <-requests:
			if !more {
				return
			}
			log.Printf("received request: %q", r.contents)
			if finished := handleRequest(broadcast, t, r); finished {
				return
			}
		}
	}
}

type CmdTable map[baps3.MessageWord]func(chan<- *baps3.Message, chan<- *baps3.Message, *TrackDB, []string) (bool, error)

var cmds CmdTable = CmdTable{
	baps3.RqRead: handleRead,
	baps3.RqQuit: handleQuit,
}

func handleQuit(_, _ chan<- *baps3.Message, _ *TrackDB, _ []string) (bool, error) {
	return true, nil
}

func handleRequest(broadcast chan<- *baps3.Message, t *TrackDB, request *Request) bool {
	var lerr error
	finished := false

	msg := request.contents

	// TODO: handle quit
	// TODO: handle bad command
	cmdfunc, ok := cmds[msg.Word()]
	if ok {
		finished, lerr = cmdfunc(broadcast, request.response, t, msg.Args())
	} else {
		lerr = fmt.Errorf("FIXME: unknown command %q", msg.Word())
	}

	acktype := "???"
	lstr := "Success"
	if lerr == nil {
		acktype = "OK"
	} else {
		// TODO: proper error distinguishment
		acktype = "FAIL"
		lstr = lerr.Error()
	}

	log.Printf("Sending ack: %q, %q", acktype, lstr)

	request.response <- baps3.NewMessage(baps3.RsAck).AddArg(acktype).AddArg(lstr)
	return finished
}

func handleRead(_ chan<- *baps3.Message, response chan<- *baps3.Message, t *TrackDB, args []string) (bool, error) {
	// read TAG(ignored) PATH
	if 2 == len(args) {
		resources := strings.Split(strings.Trim(args[1], "/"), "/")
		if len(resources) == 2 && resources[0] == "tracks" {
			log.Printf("LOOKUP %q", resources[1])
			t.LookupTrack(response, resources[1])
		} else {
			return false, fmt.Errorf("FIXME: unknown read %q", resources)
		}
	} else {
		// TODO: send failure here
		return false, fmt.Errorf("FIXME: bad read %q", args)
	}

	return false, nil
}
<commit_msg>Start separating out trackd-specific code.<commit_after>package main

import (
	"fmt"
	"log"
	"net"
	"strings"
	"sync"

	"github.com/UniversityRadioYork/baps3-go"
	"github.com/docopt/docopt-go"
	_ "github.com/lib/pq"
)

type Request struct {
	contents *baps3.Message
	response chan<- *baps3.Message
}

func main() {
	usage := `trackd - track resolving server for BAPS3

Usage:
    trackd HOSTPORT

Options:
    HOSTPORT       The host and port on which trackd should listen (host:port).
    -h, --help     Show this message.
    -v, --version  Show version.
`
	arguments, err := docopt.Parse(usage, nil, true, "trackd 0.0", true)
	if err != nil {
		log.Fatal(err)
	}

	db, err := getDB()
	if err != nil {
		log.Fatal(err)
	}
	defer func() {
		if err := db.Close(); err != nil {
			log.Fatal(err)
		}
	}()

	t := NewTrackDB(db, `M:\%d\%d`)

	RunTcpServer(RequestMap{
		baps3.RqRead: func(b, r chan<- *baps3.Message, s []string) (bool, error) { return handleRead(b, r, t, s) },
		baps3.RqQuit: func(_, _ chan<- *baps3.Message, _ []string) (bool, error) { return true, nil },
	}, arguments["HOSTPORT"].(string))
}

// RequestHandler is the type of handlers added to a RequestMap.
type RequestHandler func(chan<- *baps3.Message, chan<- *baps3.Message, []string) (bool, error)

// RequestMap is a map from requests (as message words) to RequestHandlers.
type RequestMap map[baps3.MessageWord]RequestHandler

// RunTcpServer creates and runs a Bifrost server using TCP as a transport.
// It will respond to requests using the functions in requestMap
func RunTcpServer(requestMap RequestMap, hostport string) {
	ln, err := net.Listen("tcp", hostport)
	if err != nil {
		log.Fatal(err)
	}
	defer func() {
		if err := ln.Close(); err != nil {
			log.Fatal(err)
		}
	}()

	cpQuit := make(chan struct{})

	var wg sync.WaitGroup

	clientPoolHandle := NewClientPool(cpQuit)
	wg.Add(1)
	go clientPoolHandle.Pool.Run(&wg)

	requests := make(chan *Request)

	wg.Add(1)

	go AcceptLoop(ln, requests, &clientPoolHandle, &wg)

	RequestLoop(requests, clientPoolHandle.Broadcast, requestMap, &wg)
	log.Println("main loop closing")

	// The client pool will tell all the connected clients to quit.
	cpQuit <- struct{}{}
	log.Println("main loop sent quit signal to client pool")

	// To close the accept loop, we have to kill off the acceptor.
	if err := ln.Close(); err != nil {
		log.Fatal(err)
	}

	wg.Wait()
	log.Println("trackd closing")
}

func AcceptLoop(ln net.Listener, requests chan<- *Request, clientPoolHandle *ClientPoolHandle, wg *sync.WaitGroup) {
	if wg != nil {
		defer wg.Done()
	}
	defer func() { log.Println("accept loop closing") }()

	for {
		conn, err := ln.Accept()
		if err != nil {
			log.Println(err)
			break
		}

		// Two goroutines: read and write
		wg.Add(2)
		go handleConnection(conn, requests, clientPoolHandle, wg)
	}
}

func RequestLoop(requests <-chan *Request, broadcast chan<- *baps3.Message, requestMap RequestMap, wg *sync.WaitGroup) {
	for {
		select {
		case r, more := <-requests:
			if !more {
				return
			}
			log.Printf("received request: %q", r.contents)
			if finished := handleRequest(broadcast, requestMap, r); finished {
				return
			}
		}
	}
}

func handleRequest(broadcast chan<- *baps3.Message, requestMap RequestMap, request *Request) bool {
	var lerr error
	finished := false

	msg := request.contents

	// TODO: handle bad command
	cmdfunc, ok := requestMap[msg.Word()]
	if ok {
		finished, lerr = cmdfunc(broadcast, request.response, msg.Args())
	} else {
		lerr = fmt.Errorf("FIXME: unknown command %q", msg.Word())
	}

	acktype := "???"
	lstr := "Success"
	if lerr == nil {
		acktype = "OK"
	} else {
		// TODO: proper error distinguishment
		acktype = "FAIL"
		lstr = lerr.Error()
	}

	log.Printf("Sending ack: %q, %q", acktype, lstr)

	request.response <- baps3.NewMessage(baps3.RsAck).AddArg(acktype).AddArg(lstr)
	return finished
}

func handleRead(_ chan<- *baps3.Message, response chan<- *baps3.Message, t *TrackDB, args []string) (bool, error) {
	// read TAG(ignored) PATH
	if 2 == len(args) {
		resources := strings.Split(strings.Trim(args[1], "/"), "/")
		if len(resources) == 2 && resources[0] == "tracks" {
			log.Printf("LOOKUP %q", resources[1])
			t.LookupTrack(response, resources[1])
		} else {
			return false, fmt.Errorf("FIXME: unknown read %q", resources)
		}
	} else {
		// TODO: send failure here
		return false, fmt.Errorf("FIXME: bad read %q", args)
	}

	return false, nil
}
<|endoftext|>


<commit_before>/* Copyright (c) 2015, Daniel Martí <mvdan@mvdan.cc> */
/* See LICENSE for licensing information */

package main

import (
	"flag"
	"fmt"
	"go/ast"
	"go/build"
	"go/importer"
	"go/parser"
	"go/token"
	"go/types"
	"io"
	"os"
	"path/filepath"
)

var (
	verbose = flag.Bool("v", false, "print the names of packages as they are checked")
)

func init() {
	if err := typesInit(); err != nil {
		errExit(err)
	}
}

func main() {
	flag.Parse()
	if err := checkPaths(flag.Args(), os.Stdout); err != nil {
		errExit(err)
	}
}

func errExit(err error) {
	fmt.Fprintf(os.Stderr, "%v\n", err)
	os.Exit(1)
}

func typesMatch(wanted, got []types.Type) bool {
	if len(wanted) != len(got) {
		return false
	}
	for i, w := range wanted {
		g := got[i]
		if !types.ConvertibleTo(g, w) {
			return false
		}
	}
	return true
}

func resultsMatch(wanted, got []types.Type) bool {
	if len(got) == 0 {
		return true
	}
	return typesMatch(wanted, got)
}

func interfaceMatching(calls map[string]funcSign) string {
	matchesIface := func(decls map[string]funcSign) bool {
		if len(calls) > len(decls) {
			return false
		}
		for n, d := range decls {
			c, e := calls[n]
			if !e {
				return false
			}
			if !typesMatch(d.params, c.params) {
				return false
			}
			if !resultsMatch(d.results, c.results) {
				return false
			}
		}
		return true
	}
	for name, decls := range parsed {
		if matchesIface(decls) {
			return name
		}
	}
	return ""
}

func getDirs(d string, recursive bool) ([]string, error) {
	var dirs []string
	walkFn := func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if info.Name() == "testdata" {
			return filepath.SkipDir
		}
		if info.IsDir() {
			dirs = append(dirs, path)
			if !recursive {
				return filepath.SkipDir
			}
		}
		return nil
	}
	if err := filepath.Walk(d, walkFn); err != nil {
		return nil, err
	}
	return dirs, nil
}

func getPkgs(p string) ([]*build.Package, []string, error) {
	recursive := filepath.Base(p) == "..."
	if !recursive {
		info, err := os.Stat(p)
		if err != nil {
			return nil, nil, err
		}
		if !info.IsDir() {
			pkg := &build.Package{
				Name:    "stdin",
				GoFiles: []string{p},
			}
			return []*build.Package{pkg}, []string{"."}, nil
		}
	}
	d := p
	if recursive {
		d = p[:len(p)-4]
	}
	dirs, err := getDirs(d, recursive)
	if err != nil {
		return nil, nil, err
	}
	wd, err := os.Getwd()
	if err != nil {
		return nil, nil, err
	}
	var pkgs []*build.Package
	var basedirs []string
	for _, d := range dirs {
		pkg, err := build.Import("./"+d, wd, 0)
		if _, ok := err.(*build.NoGoError); ok {
			continue
		}
		if err != nil {
			return nil, nil, err
		}
		pkgs = append(pkgs, pkg)
		basedirs = append(basedirs, d)
	}
	return pkgs, basedirs, nil
}

func checkPaths(paths []string, w io.Writer) error {
	conf := &types.Config{Importer: importer.Default()}
	for _, p := range paths {
		pkgs, basedirs, err := getPkgs(p)
		if err != nil {
			return err
		}
		for i, pkg := range pkgs {
			basedir := basedirs[i]
			if err := checkPkg(conf, pkg, basedir, w); err != nil {
				return err
			}
		}
	}
	return nil
}

func checkPkg(conf *types.Config, pkg *build.Package, basedir string, w io.Writer) error {
	if *verbose {
		fmt.Fprintln(w, basedir)
	}
	gp := &goPkg{
		Package: pkg,
		fset:    token.NewFileSet(),
	}
	for _, p := range pkg.GoFiles {
		fp := filepath.Join(basedir, p)
		if err := gp.parsePath(fp); err != nil {
			return err
		}
	}
	if err := gp.check(conf, w); err != nil {
		return err
	}
	return nil
}

type goPkg struct {
	*build.Package

	fset  *token.FileSet
	files []*ast.File
}

func (gp *goPkg) parsePath(fp string) error {
	f, err := os.Open(fp)
	if err != nil {
		return err
	}
	defer f.Close()
	if err := gp.parseReader(fp, f); err != nil {
		return err
	}
	return nil
}

func (gp *goPkg) parseReader(name string, r io.Reader) error {
	f, err := parser.ParseFile(gp.fset, name, r, 0)
	if err != nil {
		return err
	}
	gp.files = append(gp.files, f)
	return nil
}

func (gp *goPkg) check(conf *types.Config, w io.Writer) error {
	info := &types.Info{
		Types: make(map[ast.Expr]types.TypeAndValue),
		Defs:  make(map[*ast.Ident]types.Object),
	}
	_, err := conf.Check(gp.Name, gp.fset, gp.files, info)
	if err != nil {
		return err
	}
	v := &Visitor{
		Info: info,
		w:    w,
		fset: gp.fset,
	}
	for _, f := range gp.files {
		ast.Walk(v, f)
	}
	return nil
}

type Visitor struct {
	*types.Info

	w    io.Writer
	fset *token.FileSet

	nodes []ast.Node

	params map[string]types.Type
	used   map[string]map[string]funcSign

	// TODO: don't just discard params with untracked usage
	unknown       map[string]struct{}
	recordUnknown bool
}

func typeMap(t *types.Tuple) map[string]types.Type {
	m := make(map[string]types.Type, t.Len())
	for i := 0; i < t.Len(); i++ {
		p := t.At(i)
		m[p.Name()] = p.Type()
	}
	return m
}

func (v *Visitor) Visit(node ast.Node) ast.Visitor {
	var top ast.Node
	if len(v.nodes) > 0 {
		top = v.nodes[len(v.nodes)-1]
	}
	switch x := node.(type) {
	case *ast.File:
	case *ast.FuncDecl:
		f := v.Defs[x.Name].(*types.Func)
		sign := f.Type().(*types.Signature)
		v.params = typeMap(sign.Params())
		v.used = make(map[string]map[string]funcSign)
		v.unknown = make(map[string]struct{})
	case *ast.CallExpr:
		if wasParamCall := v.onCall(x); wasParamCall {
			return nil
		}
	case *ast.BlockStmt:
		v.recordUnknown = true
	case *ast.Ident:
		if !v.recordUnknown {
			break
		}
		if _, e := v.params[x.Name]; e {
			v.unknown[x.Name] = struct{}{}
		}
	case nil:
		v.nodes = v.nodes[:len(v.nodes)-1]
		if _, ok := top.(*ast.FuncDecl); ok {
			v.funcEnded(top.Pos())
			v.params = nil
			v.used = nil
			v.unknown = nil
			v.recordUnknown = false
		}
	}
	if node != nil {
		v.nodes = append(v.nodes, node)
	}
	return v
}

func funcSignature(t types.Type) *types.Signature {
	switch x := t.(type) {
	case *types.Signature:
		return x
	default:
		return funcSignature(t.Underlying())
	}
}

func (v *Visitor) onCall(ce *ast.CallExpr) bool {
	if v.used == nil {
		return false
	}
	sel, ok := ce.Fun.(*ast.SelectorExpr)
	if !ok {
		return false
	}
	left, ok := sel.X.(*ast.Ident)
	if !ok {
		return false
	}
	vname := left.Name
	if _, e := v.params[vname]; !e {
		return false
	}
	sign := funcSignature(v.Types[ce.Fun].Type)
	c := funcSign{}
	results := sign.Results()
	for i := 0; i < results.Len(); i++ {
		v := results.At(i)
		c.results = append(c.results, v.Type())
	}
	for _, a := range ce.Args {
		c.params = append(c.params, v.Types[a].Type)
	}
	if _, e := v.used[vname]; !e {
		v.used[vname] = make(map[string]funcSign)
	}
	fname := sel.Sel.Name
	v.used[vname][fname] = c
	return true
}

func (v *Visitor) funcEnded(pos token.Pos) {
	for name, methods := range v.used {
		if _, e := v.unknown[name]; e {
			continue
		}
		iface := interfaceMatching(methods)
		if iface == "" {
			continue
		}
		param, e := v.params[name]
		if !e {
			continue
		}
		if iface == param.String() {
			continue
		}
		pos := v.fset.Position(pos)
		fmt.Fprintf(v.w, "%s:%d: %s can be %s\n",
			pos.Filename, pos.Line, name, iface)
	}
}
<commit_msg>Also skip vendor/ and dirs beginning with '_'<commit_after>/* Copyright (c) 2015, Daniel Martí <mvdan@mvdan.cc> */
/* See LICENSE for licensing information */

package main

import (
	"flag"
	"fmt"
	"go/ast"
	"go/build"
	"go/importer"
	"go/parser"
	"go/token"
	"go/types"
	"io"
	"os"
	"path/filepath"
	"regexp"
)

var (
	verbose = flag.Bool("v", false, "print the names of packages as they are checked")
)

func init() {
	if err := typesInit(); err != nil {
		errExit(err)
	}
}

func main() {
	flag.Parse()
	if err := checkPaths(flag.Args(), os.Stdout); err != nil {
		errExit(err)
	}
}

func errExit(err error) {
	fmt.Fprintf(os.Stderr, "%v\n", err)
	os.Exit(1)
}

func typesMatch(wanted, got []types.Type) bool {
	if len(wanted) != len(got) {
		return false
	}
	for i, w := range wanted {
		g := got[i]
		if !types.ConvertibleTo(g, w) {
			return false
		}
	}
	return true
}

func resultsMatch(wanted, got []types.Type) bool {
	if len(got) == 0 {
		return true
	}
	return typesMatch(wanted, got)
}

func interfaceMatching(calls map[string]funcSign) string {
	matchesIface := func(decls map[string]funcSign) bool {
		if len(calls) > len(decls) {
			return false
		}
		for n, d := range decls {
			c, e := calls[n]
			if !e {
				return false
			}
			if !typesMatch(d.params, c.params) {
				return false
			}
			if !resultsMatch(d.results, c.results) {
				return false
			}
		}
		return true
	}
	for name, decls := range parsed {
		if matchesIface(decls) {
			return name
		}
	}
	return ""
}

var skipDir = regexp.MustCompile(`^(testdata|vendor|_.*)$`)

func getDirs(d string, recursive bool) ([]string, error) {
	var dirs []string
	walkFn := func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		if skipDir.MatchString(info.Name()) {
			return filepath.SkipDir
		}
		if info.IsDir() {
			dirs = append(dirs, path)
			if !recursive {
				return filepath.SkipDir
			}
		}
		return nil
	}
	if err := filepath.Walk(d, walkFn); err != nil {
		return nil, err
	}
	return dirs, nil
}

func getPkgs(p string) ([]*build.Package, []string, error) {
	recursive := filepath.Base(p) == "..."
	if !recursive {
		info, err := os.Stat(p)
		if err != nil {
			return nil, nil, err
		}
		if !info.IsDir() {
			pkg := &build.Package{
				Name:    "stdin",
				GoFiles: []string{p},
			}
			return []*build.Package{pkg}, []string{"."}, nil
		}
	}
	d := p
	if recursive {
		d = p[:len(p)-4]
	}
	dirs, err := getDirs(d, recursive)
	if err != nil {
		return nil, nil, err
	}
	wd, err := os.Getwd()
	if err != nil {
		return nil, nil, err
	}
	var pkgs []*build.Package
	var basedirs []string
	for _, d := range dirs {
		pkg, err := build.Import("./"+d, wd, 0)
		if _, ok := err.(*build.NoGoError); ok {
			continue
		}
		if err != nil {
			return nil, nil, err
		}
		pkgs = append(pkgs, pkg)
		basedirs = append(basedirs, d)
	}
	return pkgs, basedirs, nil
}

func checkPaths(paths []string, w io.Writer) error {
	conf := &types.Config{Importer: importer.Default()}
	for _, p := range paths {
		pkgs, basedirs, err := getPkgs(p)
		if err != nil {
			return err
		}
		for i, pkg := range pkgs {
			basedir := basedirs[i]
			if err := checkPkg(conf, pkg, basedir, w); err != nil {
				return err
			}
		}
	}
	return nil
}

func checkPkg(conf *types.Config, pkg *build.Package, basedir string, w io.Writer) error {
	if *verbose {
		fmt.Fprintln(w, basedir)
	}
	gp := &goPkg{
		Package: pkg,
		fset:    token.NewFileSet(),
	}
	for _, p := range pkg.GoFiles {
		fp := filepath.Join(basedir, p)
		if err := gp.parsePath(fp); err != nil {
			return err
		}
	}
	if err := gp.check(conf, w); err != nil {
		return err
	}
	return nil
}

type goPkg struct {
	*build.Package

	fset  *token.FileSet
	files []*ast.File
}

func (gp *goPkg) parsePath(fp string) error {
	f, err := os.Open(fp)
	if err != nil {
		return err
	}
	defer f.Close()
	if err := gp.parseReader(fp, f); err != nil {
		return err
	}
	return nil
}

func (gp *goPkg) parseReader(name string, r io.Reader) error {
	f, err := parser.ParseFile(gp.fset, name, r, 0)
	if err != nil {
		return err
	}
	gp.files = append(gp.files, f)
	return nil
}

func (gp *goPkg) check(conf *types.Config, w io.Writer) error {
	info := &types.Info{
		Types: make(map[ast.Expr]types.TypeAndValue),
		Defs:  make(map[*ast.Ident]types.Object),
	}
	_, err := conf.Check(gp.Name, gp.fset, gp.files, info)
	if err != nil {
		return err
	}
	v := &Visitor{
		Info: info,
		w:    w,
		fset: gp.fset,
	}
	for _, f := range gp.files {
		ast.Walk(v, f)
	}
	return nil
}

type Visitor struct {
	*types.Info

	w    io.Writer
	fset *token.FileSet

	nodes []ast.Node

	params map[string]types.Type
	used   map[string]map[string]funcSign

	// TODO: don't just discard params with untracked usage
	unknown       map[string]struct{}
	recordUnknown bool
}

func typeMap(t *types.Tuple) map[string]types.Type {
	m := make(map[string]types.Type, t.Len())
	for i := 0; i < t.Len(); i++ {
		p := t.At(i)
		m[p.Name()] = p.Type()
	}
	return m
}

func (v *Visitor) Visit(node ast.Node) ast.Visitor {
	var top ast.Node
	if len(v.nodes) > 0 {
		top = v.nodes[len(v.nodes)-1]
	}
	switch x := node.(type) {
	case *ast.File:
	case *ast.FuncDecl:
		f := v.Defs[x.Name].(*types.Func)
		sign := f.Type().(*types.Signature)
		v.params = typeMap(sign.Params())
		v.used = make(map[string]map[string]funcSign)
		v.unknown = make(map[string]struct{})
	case *ast.CallExpr:
		if wasParamCall := v.onCall(x); wasParamCall {
			return nil
		}
	case *ast.BlockStmt:
		v.recordUnknown = true
	case *ast.Ident:
		if !v.recordUnknown {
			break
		}
		if _, e := v.params[x.Name]; e {
			v.unknown[x.Name] = struct{}{}
		}
	case nil:
		v.nodes = v.nodes[:len(v.nodes)-1]
		if _, ok := top.(*ast.FuncDecl); ok {
			v.funcEnded(top.Pos())
			v.params = nil
			v.used = nil
			v.unknown = nil
			v.recordUnknown = false
		}
	}
	if node != nil {
		v.nodes = append(v.nodes, node)
	}
	return v
}

func funcSignature(t types.Type) *types.Signature {
	switch x := t.(type) {
	case *types.Signature:
		return x
	default:
		return funcSignature(t.Underlying())
	}
}

func (v *Visitor) onCall(ce *ast.CallExpr) bool {
	if v.used == nil {
		return false
	}
	sel, ok := ce.Fun.(*ast.SelectorExpr)
	if !ok {
		return false
	}
	left, ok := sel.X.(*ast.Ident)
	if !ok {
		return false
	}
	vname := left.Name
	if _, e := v.params[vname]; !e {
		return false
	}
	sign := funcSignature(v.Types[ce.Fun].Type)
	c := funcSign{}
	results := sign.Results()
	for i := 0; i < results.Len(); i++ {
		v := results.At(i)
		c.results = append(c.results, v.Type())
	}
	for _, a := range ce.Args {
		c.params = append(c.params, v.Types[a].Type)
	}
	if _, e := v.used[vname]; !e {
		v.used[vname] = make(map[string]funcSign)
	}
	fname := sel.Sel.Name
	v.used[vname][fname] = c
	return true
}

func (v *Visitor) funcEnded(pos token.Pos) {
	for name, methods := range v.used {
		if _, e := v.unknown[name]; e {
			continue
		}
		iface := interfaceMatching(methods)
		if iface == "" {
			continue
		}
		param, e := v.params[name]
		if !e {
			continue
		}
		if iface == param.String() {
			continue
		}
		pos := v.fset.Position(pos)
		fmt.Fprintf(v.w, "%s:%d: %s can be %s\n",
			pos.Filename, pos.Line, name, iface)
	}
}
<|endoftext|>


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

// File contains Bind functionality
package ldap

import (
	"errors"
	"github.com/hsoj/asn1-ber"
)

func (l *Conn) Bind(username, password string) *Error {
	messageID := l.nextMessageID()

	packet := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "LDAP Request")
	packet.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimative, ber.TagInteger, messageID, "MessageID"))
	bindRequest := ber.Encode(ber.ClassApplication, ber.TypeConstructed, ApplicationBindRequest, nil, "Bind Request")
	bindRequest.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimative, ber.TagInteger, 3, "Version"))
	bindRequest.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimative, ber.TagOctetString, username, "User Name"))
	bindRequest.AppendChild(ber.NewString(ber.ClassContext, ber.TypePrimative, 0, password, "Password"))
	packet.AppendChild(bindRequest)

	if l.Debug {
		ber.PrintPacket(packet)
	}

	channel, err := l.sendMessage(packet)
	if err != nil {
		return err
	}
	if channel == nil {
		return NewError(ErrorNetwork, errors.New("Could not send message"))
	}
	defer l.finishMessage(messageID)
	packet = <-channel

	if packet == nil {
		return NewError(ErrorNetwork, errors.New("Could not retrieve response"))
	}

	if l.Debug {
		if err := addLDAPDescriptions(packet); err != nil {
			return NewError(ErrorDebugging, err)
		}
		ber.PrintPacket(packet)
	}

	result_code, result_description := getLDAPResultCode(packet)
	if result_code != 0 {
		return NewError(result_code, errors.New(result_description))
	}

	return nil
}
<commit_msg>Changed ber lib.<commit_after>// Copyright 2011 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.

// File contains Bind functionality
package ldap

import (
	"errors"
	"github.com/mavricknz/asn1-ber"
)

func (l *Conn) Bind(username, password string) *Error {
	messageID := l.nextMessageID()

	packet := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSequence, nil, "LDAP Request")
	packet.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimative, ber.TagInteger, messageID, "MessageID"))
	bindRequest := ber.Encode(ber.ClassApplication, ber.TypeConstructed, ApplicationBindRequest, nil, "Bind Request")
	bindRequest.AppendChild(ber.NewInteger(ber.ClassUniversal, ber.TypePrimative, ber.TagInteger, 3, "Version"))
	bindRequest.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimative, ber.TagOctetString, username, "User Name"))
	bindRequest.AppendChild(ber.NewString(ber.ClassContext, ber.TypePrimative, 0, password, "Password"))
	packet.AppendChild(bindRequest)

	if l.Debug {
		ber.PrintPacket(packet)
	}

	channel, err := l.sendMessage(packet)
	if err != nil {
		return err
	}
	if channel == nil {
		return NewError(ErrorNetwork, errors.New("Could not send message"))
	}
	defer l.finishMessage(messageID)
	packet = <-channel

	if packet == nil {
		return NewError(ErrorNetwork, errors.New("Could not retrieve response"))
	}

	if l.Debug {
		if err := addLDAPDescriptions(packet); err != nil {
			return NewError(ErrorDebugging, err)
		}
		ber.PrintPacket(packet)
	}

	result_code, result_description := getLDAPResultCode(packet)
	if result_code != 0 {
		return NewError(result_code, errors.New(result_description))
	}

	return nil
}
<|endoftext|>


<commit_before>package main

import (
	"time"
	"encoding/json"
	"github.com/julienschmidt/httprouter"
	"net/http"
	"net/http/httputil"
	"log"
	"os"
	//"io"
	"fmt"
	"database/sql"
	_ "github.com/lib/pq"
	"gopkg.in/antage/eventsource.v1"
)

var conn *sql.DB
var es eventsource.EventSource

func SetHeaders(w *http.ResponseWriter) {
	(*w).Header().Set("Access-Control-Allow-Origin", "*")
	(*w).Header().Set("Content-Type", "application/json")
}

func GetBlog(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	rows,_ := conn.Query("SELECT * FROM blog")
	data := []BlogPost{}
	for rows.Next() {
		post := BlogPost{}
		rows.Scan(&post.Id, &post.Titel, &post.Text, &post.Auteur, &post.Img_url, &post.Ctime, &post.Image)
		data = append(data, post)
	}
	buf,_ := json.Marshal(data)
	SetHeaders(&w)
	w.Write(buf)
}

func GetPost(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	row := conn.QueryRow("SELECT * FROM blog WHERE id = $1 LIMIT 1", ps.ByName("id"))
	data := BlogPost{}
	row.Scan(&data.Id, &data.Titel, &data.Text, &data.Auteur, &data.Img_url, &data.Ctime, &data.Image)
	buf,_ := json.Marshal(data)
	SetHeaders(&w)
	w.Write(buf)
}

func GetLatestSpinData(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	rows,_ := conn.Query("SELECT * FROM spindata ORDER BY tijd DESC LIMIT 1")
	spin := SpinData{}
	rows.Next()
	rows.Scan(&spin.Id, &spin.Tijd, &spin.Mode, &spin.Hellingsgraad, &spin.Snelheid, &spin.Batterij, &spin.BallonCount)
	buf,_ := json.Marshal(spin)
	SetHeaders(&w)
	w.Write(buf)
}

func GetLatestSpinBatterij(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	row := conn.QueryRow("SELECT batterij FROM spindata ORDER BY tijd DESC LIMIT 1")
	var data int
	row.Scan(&data)
	buf,_ := json.Marshal(data)
	SetHeaders(&w)
	w.Write(buf)
}

func GetLatestSpinMode(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	row := conn.QueryRow("SELECT mode FROM spindata ORDER BY tijd DESC LIMIT 1")
	var data string
	row.Scan(&data)
	buf,_ := json.Marshal(data)
	SetHeaders(&w)
	w.Write(buf)
}

func GetArchivedSpinData(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	rows,_ := conn.Query("SELECT * FROM spindata")
	data := []SpinData{}
	for rows.Next() {
		spin := SpinData{}
		rows.Scan(&spin.Id, &spin.Tijd, &spin.Mode, &spin.Hellingsgraad, &spin.Snelheid, &spin.Batterij, &spin.BallonCount)
		data = append(data, spin)
	}
	buf,_ := json.Marshal(data)
	SetHeaders(&w)
	w.Write(buf)
}

func GetArchivedSpinBatterij(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	rows,_ := conn.Query("SELECT batterij FROM spindata")
	data := make([]int, 0)
	var scanInt int
	for rows.Next() {
		rows.Scan(&scanInt)
		data = append(data, scanInt)
	}
	buf,_ := json.Marshal(data)
	fmt.Printf(string(buf))
	SetHeaders(&w)
	w.Write(buf)
}

func GetArchivedSpinMode(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	rows,_ := conn.Query("SELECT mode FROM spindata")
	data := make([]string, 0)
	var scanStr string
	for rows.Next() {
		rows.Scan(&scanStr)
		data = append(data, scanStr)
	}
	buf,_ := json.Marshal(data)
	fmt.Printf(string(buf))
	SetHeaders(&w)
	w.Write(buf)
}

func GetLatestServoData(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	row := conn.QueryRow("SELECT * FROM servodata ORDER BY tijd DESC LIMIT 1")
	servo := ServoData{}
	row.Scan(&servo.Id, &servo.ServoId, &servo.Tijd, &servo.Voltage, &servo.Positie, &servo.Load, &servo.Temperatuur)
	buf,_ := json.Marshal(servo)
	SetHeaders(&w)
	w.Write(buf)
}

func GetArchivedServoData(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	rows,_ := conn.Query("SELECT * FROM servodata")
	data := []ServoData{}
	for rows.Next() {
		servo := ServoData{}
		rows.Scan(&servo.Id, &servo.ServoId, &servo.Tijd, &servo.Voltage, &servo.Positie, &servo.Load, &servo.Temperatuur)
		data = append(data, servo)
	}
	buf,_ := json.Marshal(data)
	SetHeaders(&w)
	w.Write(buf)
}

func GetLogs(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	rows,_ := conn.Query("SELECT * FROM logs")
	data := []LogData{}
	for rows.Next() {
		log := LogData{}
		rows.Scan(&log.Id, &log.Log)
		data = append(data, log)
	}
	buf,_ := json.Marshal(data)
	SetHeaders(&w)
	w.Write(buf)
}

func Test(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	buf,_ := json.Marshal("test")
	SetHeaders(&w)
	w.Write(buf)
}

func PostBlog(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	//r.ParseMultipartForm(32 << 20)
	/*file, handler, err := r.FormFile("uploadfile")
	defer file.Close()
	if err == nil {
		fmt.Fprintf(w, "%v", handler.Header)
		f, err := os.OpenFile("./img/"+handler.Filename, os.O_WRONLY|os.O_CREATE, 0666)
		if err != nil {
			w.WriteHeader(500)
			return
		}
		defer f.Close()
		io.Copy(f, file)
	}

	err = nil*/

	//_,err := conn.Query("INSERT INTO blog (titel, text, auteur, ctime, image) VALUES ($1, $2, $3, $4, $5)", r.FormValue("titel"), r.FormValue("text"), r.FormValue("auteur"), time.Now(), "http://idp-api.herokuapp.com/img/"+handler.Filename)
	/*_,err := conn.Query("INSERT INTO blog (titel, text, auteur, ctime) VALUES ($1, $2, $3, $4)", r.FormValue("onderwerp"), r.FormValue("bericht"), r.FormValue("naam"), time.Now())
	if err != nil {
		w.WriteHeader(500)
		w.Write([]byte(err.Error()))
		return
	}*/
	w.Header().Set("Content-Type", "text/html")
	w.WriteHeader(201)
	reqStr, _ := httputil.DumpRequest(r,true)
	w.Write(reqStr)
	//w.Write([]byte("<meta http-equiv=\"refresh\" content=\"1; url=http://knightspider.herokuapp.com/#/blog\">successful"))
}

func PostSpinData(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	//reqStr, _ := httputil.DumpRequest(r, true)
	w.Write([]byte(r.FormValue("mode")))
	//r.ParseForm()
	/*mode := r.FormValue("mode")
	hellingsgraad := r.FormValue("hellingsgraad")
	snelheid := r.FormValue("snelheid")
	batterij := r.FormValue("batterij")
	balloncount := r.FormValue("ballonCount")*/
	/*_,err := conn.Query("INSERT INTO spindata (tijd, mode, hellingsgraad, snelheid, batterij, balloncount) VALUES ($1, $2, $3, $4, $5, $6)", time.Now(),
		mode, hellingsgraad, snelheid, batterij, balloncount)
	if err != nil {
		w.WriteHeader(500)
		w.Write([]byte(fmt.Sprintf("mode = %s, hellingsgraad = %s, batterij = %s, balloncount = %s", mode, hellingsgraad, batterij, balloncount)))
		w.Write([]byte(err.Error()))
		return
	}*/
	/*w.WriteHeader(201)
	w.Write([]byte(fmt.Sprintf("mode = %s, hellingsgraad = %s, snelheid = %s, batterij = %s, balloncount = %s", mode, hellingsgraad, snelheid, batterij, balloncount)))*/
	//w.Write([]byte("successful"))
}

func PostServoData(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	_,err := conn.Query("INSERT INTO servodata (servo_id, tijd, voltage, positie, load, temperatuur) VALUES ($1, $2, $3, $4, $5, $6)",
		r.FormValue("servo_id"), time.Now(), r.FormValue("voltage"), r.FormValue("positie"), r.FormValue("load"), r.FormValue("Temperatuur"))
	if err != nil {
		w.WriteHeader(500)
		w.Write([]byte(err.Error()))
		return
	}
	w.WriteHeader(201)
	w.Write([]byte("successful"))
}

func PostLog(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	_,err := conn.Query("INSERT INTO logs (log) VALUES ($1)",
		r.FormValue("log"))
	if err != nil {
		w.WriteHeader(500)
		w.Write([]byte(err.Error()))
		return
	}
	es.SendEventMessage(r.FormValue("log"), "log", "")
	w.WriteHeader(201)
	w.Write([]byte(r.FormValue("log")))
}

func Head(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	SetHeaders(&w)
	w.WriteHeader(204)
}

func main() {
	conn,_ = sql.Open("postgres", os.Getenv("DATABASE_URL"))
	defer conn.Close()

	es = eventsource.New(
		&eventsource.Settings{
			Timeout: 5 * time.Second,
			CloseOnTimeout: false,
			IdleTimeout: 30 * time.Minute,
		},
		func(req *http.Request) [][]byte {
			return [][]byte{
				[]byte("X-Accel-Buffering: no"),
				[]byte("Access-Control-Allow-Origin: *"),
			}
		},
	)
	defer es.Close()

	router := httprouter.New()
	router.HEAD("/*path", Head)
	router.GET("/test", Test)
	router.GET("/blog", GetBlog)
	router.GET("/blog/:id", GetPost)
	router.GET("/spin/latest", GetLatestSpinData)
	router.GET("/spin/latest/batterij", GetLatestSpinBatterij)
	router.GET("/spin/latest/mode", GetLatestSpinMode)
	router.GET("/spin/archive", GetArchivedSpinData)
	router.GET("/spin/archive/batterij", GetArchivedSpinBatterij)
	router.GET("/spin/archive/mode", GetArchivedSpinMode)
	router.GET("/servo/latest", GetLatestServoData)
	router.GET("/servo/archive", GetArchivedServoData)
	router.GET("/log", GetLogs)
	router.POST("/blog", PostBlog)
	router.POST("/spin", PostSpinData)
	router.POST("/servo", PostServoData)
	router.POST("/log", PostLog)

	http.Handle("/subscribe", es)
	http.Handle("/", router)

	port := os.Getenv("PORT")
	if port == "" {
		port = "3000"
	}
	fmt.Printf("Starting server at localhost:%s...", port)
	log.Fatal(http.ListenAndServe(":"+port, nil))
}<commit_msg>postformvalue<commit_after>package main

import (
	"time"
	"encoding/json"
	"github.com/julienschmidt/httprouter"
	"net/http"
	"net/http/httputil"
	"log"
	"os"
	//"io"
	"fmt"
	"database/sql"
	_ "github.com/lib/pq"
	"gopkg.in/antage/eventsource.v1"
)

var conn *sql.DB
var es eventsource.EventSource

func SetHeaders(w *http.ResponseWriter) {
	(*w).Header().Set("Access-Control-Allow-Origin", "*")
	(*w).Header().Set("Content-Type", "application/json")
}

func GetBlog(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	rows,_ := conn.Query("SELECT * FROM blog")
	data := []BlogPost{}
	for rows.Next() {
		post := BlogPost{}
		rows.Scan(&post.Id, &post.Titel, &post.Text, &post.Auteur, &post.Img_url, &post.Ctime, &post.Image)
		data = append(data, post)
	}
	buf,_ := json.Marshal(data)
	SetHeaders(&w)
	w.Write(buf)
}

func GetPost(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	row := conn.QueryRow("SELECT * FROM blog WHERE id = $1 LIMIT 1", ps.ByName("id"))
	data := BlogPost{}
	row.Scan(&data.Id, &data.Titel, &data.Text, &data.Auteur, &data.Img_url, &data.Ctime, &data.Image)
	buf,_ := json.Marshal(data)
	SetHeaders(&w)
	w.Write(buf)
}

func GetLatestSpinData(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	rows,_ := conn.Query("SELECT * FROM spindata ORDER BY tijd DESC LIMIT 1")
	spin := SpinData{}
	rows.Next()
	rows.Scan(&spin.Id, &spin.Tijd, &spin.Mode, &spin.Hellingsgraad, &spin.Snelheid, &spin.Batterij, &spin.BallonCount)
	buf,_ := json.Marshal(spin)
	SetHeaders(&w)
	w.Write(buf)
}

func GetLatestSpinBatterij(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	row := conn.QueryRow("SELECT batterij FROM spindata ORDER BY tijd DESC LIMIT 1")
	var data int
	row.Scan(&data)
	buf,_ := json.Marshal(data)
	SetHeaders(&w)
	w.Write(buf)
}

func GetLatestSpinMode(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	row := conn.QueryRow("SELECT mode FROM spindata ORDER BY tijd DESC LIMIT 1")
	var data string
	row.Scan(&data)
	buf,_ := json.Marshal(data)
	SetHeaders(&w)
	w.Write(buf)
}

func GetArchivedSpinData(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	rows,_ := conn.Query("SELECT * FROM spindata")
	data := []SpinData{}
	for rows.Next() {
		spin := SpinData{}
		rows.Scan(&spin.Id, &spin.Tijd, &spin.Mode, &spin.Hellingsgraad, &spin.Snelheid, &spin.Batterij, &spin.BallonCount)
		data = append(data, spin)
	}
	buf,_ := json.Marshal(data)
	SetHeaders(&w)
	w.Write(buf)
}

func GetArchivedSpinBatterij(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	rows,_ := conn.Query("SELECT batterij FROM spindata")
	data := make([]int, 0)
	var scanInt int
	for rows.Next() {
		rows.Scan(&scanInt)
		data = append(data, scanInt)
	}
	buf,_ := json.Marshal(data)
	fmt.Printf(string(buf))
	SetHeaders(&w)
	w.Write(buf)
}

func GetArchivedSpinMode(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	rows,_ := conn.Query("SELECT mode FROM spindata")
	data := make([]string, 0)
	var scanStr string
	for rows.Next() {
		rows.Scan(&scanStr)
		data = append(data, scanStr)
	}
	buf,_ := json.Marshal(data)
	fmt.Printf(string(buf))
	SetHeaders(&w)
	w.Write(buf)
}

func GetLatestServoData(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	row := conn.QueryRow("SELECT * FROM servodata ORDER BY tijd DESC LIMIT 1")
	servo := ServoData{}
	row.Scan(&servo.Id, &servo.ServoId, &servo.Tijd, &servo.Voltage, &servo.Positie, &servo.Load, &servo.Temperatuur)
	buf,_ := json.Marshal(servo)
	SetHeaders(&w)
	w.Write(buf)
}

func GetArchivedServoData(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	rows,_ := conn.Query("SELECT * FROM servodata")
	data := []ServoData{}
	for rows.Next() {
		servo := ServoData{}
		rows.Scan(&servo.Id, &servo.ServoId, &servo.Tijd, &servo.Voltage, &servo.Positie, &servo.Load, &servo.Temperatuur)
		data = append(data, servo)
	}
	buf,_ := json.Marshal(data)
	SetHeaders(&w)
	w.Write(buf)
}

func GetLogs(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	rows,_ := conn.Query("SELECT * FROM logs")
	data := []LogData{}
	for rows.Next() {
		log := LogData{}
		rows.Scan(&log.Id, &log.Log)
		data = append(data, log)
	}
	buf,_ := json.Marshal(data)
	SetHeaders(&w)
	w.Write(buf)
}

func Test(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	buf,_ := json.Marshal("test")
	SetHeaders(&w)
	w.Write(buf)
}

func PostBlog(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	//r.ParseMultipartForm(32 << 20)
	/*file, handler, err := r.FormFile("uploadfile")
	defer file.Close()
	if err == nil {
		fmt.Fprintf(w, "%v", handler.Header)
		f, err := os.OpenFile("./img/"+handler.Filename, os.O_WRONLY|os.O_CREATE, 0666)
		if err != nil {
			w.WriteHeader(500)
			return
		}
		defer f.Close()
		io.Copy(f, file)
	}

	err = nil*/

	//_,err := conn.Query("INSERT INTO blog (titel, text, auteur, ctime, image) VALUES ($1, $2, $3, $4, $5)", r.FormValue("titel"), r.FormValue("text"), r.FormValue("auteur"), time.Now(), "http://idp-api.herokuapp.com/img/"+handler.Filename)
	/*_,err := conn.Query("INSERT INTO blog (titel, text, auteur, ctime) VALUES ($1, $2, $3, $4)", r.FormValue("onderwerp"), r.FormValue("bericht"), r.FormValue("naam"), time.Now())
	if err != nil {
		w.WriteHeader(500)
		w.Write([]byte(err.Error()))
		return
	}*/
	w.Header().Set("Content-Type", "text/html")
	w.WriteHeader(201)
	reqStr, _ := httputil.DumpRequest(r,true)
	w.Write(reqStr)
	w.Write([]byte(r.FormValue("title")))
	//w.Write([]byte("<meta http-equiv=\"refresh\" content=\"1; url=http://knightspider.herokuapp.com/#/blog\">successful"))
}

func PostSpinData(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	//reqStr, _ := httputil.DumpRequest(r, true)
	w.Write([]byte(r.FormValue("mode")))
	//r.ParseForm()
	/*mode := r.FormValue("mode")
	hellingsgraad := r.FormValue("hellingsgraad")
	snelheid := r.FormValue("snelheid")
	batterij := r.FormValue("batterij")
	balloncount := r.FormValue("ballonCount")*/
	/*_,err := conn.Query("INSERT INTO spindata (tijd, mode, hellingsgraad, snelheid, batterij, balloncount) VALUES ($1, $2, $3, $4, $5, $6)", time.Now(),
		mode, hellingsgraad, snelheid, batterij, balloncount)
	if err != nil {
		w.WriteHeader(500)
		w.Write([]byte(fmt.Sprintf("mode = %s, hellingsgraad = %s, batterij = %s, balloncount = %s", mode, hellingsgraad, batterij, balloncount)))
		w.Write([]byte(err.Error()))
		return
	}*/
	/*w.WriteHeader(201)
	w.Write([]byte(fmt.Sprintf("mode = %s, hellingsgraad = %s, snelheid = %s, batterij = %s, balloncount = %s", mode, hellingsgraad, snelheid, batterij, balloncount)))*/
	//w.Write([]byte("successful"))
}

func PostServoData(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	_,err := conn.Query("INSERT INTO servodata (servo_id, tijd, voltage, positie, load, temperatuur) VALUES ($1, $2, $3, $4, $5, $6)",
		r.FormValue("servo_id"), time.Now(), r.FormValue("voltage"), r.FormValue("positie"), r.FormValue("load"), r.FormValue("Temperatuur"))
	if err != nil {
		w.WriteHeader(500)
		w.Write([]byte(err.Error()))
		return
	}
	w.WriteHeader(201)
	w.Write([]byte("successful"))
}

func PostLog(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	_,err := conn.Query("INSERT INTO logs (log) VALUES ($1)",
		r.FormValue("log"))
	if err != nil {
		w.WriteHeader(500)
		w.Write([]byte(err.Error()))
		return
	}
	es.SendEventMessage(r.FormValue("log"), "log", "")
	w.WriteHeader(201)
	w.Write([]byte(r.FormValue("log")))
}

func Head(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	SetHeaders(&w)
	w.WriteHeader(204)
}

func main() {
	conn,_ = sql.Open("postgres", os.Getenv("DATABASE_URL"))
	defer conn.Close()

	es = eventsource.New(
		&eventsource.Settings{
			Timeout: 5 * time.Second,
			CloseOnTimeout: false,
			IdleTimeout: 30 * time.Minute,
		},
		func(req *http.Request) [][]byte {
			return [][]byte{
				[]byte("X-Accel-Buffering: no"),
				[]byte("Access-Control-Allow-Origin: *"),
			}
		},
	)
	defer es.Close()

	router := httprouter.New()
	router.HEAD("/*path", Head)
	router.GET("/test", Test)
	router.GET("/blog", GetBlog)
	router.GET("/blog/:id", GetPost)
	router.GET("/spin/latest", GetLatestSpinData)
	router.GET("/spin/latest/batterij", GetLatestSpinBatterij)
	router.GET("/spin/latest/mode", GetLatestSpinMode)
	router.GET("/spin/archive", GetArchivedSpinData)
	router.GET("/spin/archive/batterij", GetArchivedSpinBatterij)
	router.GET("/spin/archive/mode", GetArchivedSpinMode)
	router.GET("/servo/latest", GetLatestServoData)
	router.GET("/servo/archive", GetArchivedServoData)
	router.GET("/log", GetLogs)
	router.POST("/blog", PostBlog)
	router.POST("/spin", PostSpinData)
	router.POST("/servo", PostServoData)
	router.POST("/log", PostLog)

	http.Handle("/subscribe", es)
	http.Handle("/", router)

	port := os.Getenv("PORT")
	if port == "" {
		port = "3000"
	}
	fmt.Printf("Starting server at localhost:%s...", port)
	log.Fatal(http.ListenAndServe(":"+port, nil))
}<|endoftext|>


<commit_before>package main

import (
	"os"

	"github.com/dynport/dgtk/cli"
)

func main() {
	Run()
}

func Run() {
	callArgs, _ := ConfigCallArgs()
	err := router(callArgs).RunWithArgs()
	switch err {
	case cli.ErrorHelpRequested, cli.ErrorNoRoute:
		os.Exit(1)
	case nil:
		os.Exit(0)
	default:
		printErr(err)
		os.Exit(1)
	}
}
<commit_msg>client: add update notification on outdated version<commit_after>package main

import (
	"os"

	"github.com/dynport/dgtk/cli"
	"time"
	"io/ioutil"
	"fmt"
	"net/http"
	"strings"
)

func main() {
	Run()
}

func Run() {
	validateVersion()

	callArgs, _ := ConfigCallArgs()
	err := router(callArgs).RunWithArgs()
	switch err {
	case cli.ErrorHelpRequested, cli.ErrorNoRoute:
		os.Exit(1)
	case nil:
		os.Exit(0)
	default:
		printErr(err)
		os.Exit(1)
	}
}

const PHRASEAPP_VERSION_TMP_FILE = "/tmp/.phraseapp.version"

func validateVersion() {
	var version string
	stat, err := os.Stat(PHRASEAPP_VERSION_TMP_FILE)
	if PHRASEAPP_CLIENT_VERSION == "test" {
		// do nothing, we're in development mode
	} else if os.IsNotExist(err) || time.Now().Sub(stat.ModTime()) > time.Hour {
		// fetch new version, if not done so or over an hour ago
		version, err = getCurrentVersion()
		if err == nil { // persist the version for the next hour
			err = ioutil.WriteFile(PHRASEAPP_VERSION_TMP_FILE, []byte(version), 0600)
		}
	} else if (err == nil) {
		// otherwise load the version (fetched less than an hour ago) from the temp file
		var buf []byte
		buf, err = ioutil.ReadFile(PHRASEAPP_VERSION_TMP_FILE)
		if err == nil {
			version = string(buf)
		}
	}

	switch {
	case PHRASEAPP_CLIENT_VERSION == "test":
		fmt.Fprintf(os.Stderr, "You're running a development version of the PhraseApp CLI tool!\n\n")
	case err == nil && version != PHRASEAPP_CLIENT_VERSION:
		fmt.Fprintf(os.Stderr, "Please consider updating the PhraseApp CLI client (%s < %s)\nSee https://phraseapp.com/en/cli\n\n", PHRASEAPP_CLIENT_VERSION, version)
	default:
		// ignore errors (and up to date versions of course).
	}
}

func getCurrentVersion() (string, error) {
	req, err := http.NewRequest("HEAD", "https://github.com/phrase/phraseapp-client/releases/latest", nil)
	if err != nil { return "", err }

	transport := http.Transport{}
	resp, err := transport.RoundTrip(req)
	if err != nil {
		return "", err
	}
	defer resp.Body.Close() // body is empty as it is only a HEAD request

	if resp.StatusCode != 302 {
		return "", fmt.Errorf("failed to request the file")
	}

	url, err := resp.Location()
	if err != nil {
		return "", err
	}

	segments := strings.Split(url.Path, "/")
	for i := len(segments) - 1; i >= 0; i-- {
		if segments[i] != "" {
			return segments[i], nil
		}
	}
	return "", fmt.Errorf("no valid version segment found")
}

<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"reflect"
	"sync"

	"github.com/cactus/go-statsd-client/statsd"
	"github.com/docker/docker/pkg/units"
	"github.com/fsouza/go-dockerclient"
	"github.com/namsral/flag"
)

func main() {
	var dockerHost string
	var dockerTLSVerify bool
	var dockerCertPath string

	flag.StringVar(&dockerHost, "docker-host", "unix:///var/run/docker.sock", "address of Docker host")
	flag.BoolVar(&dockerTLSVerify, "docker-tls-verify", false, "use TLS client for Docker")
	flag.StringVar(&dockerCertPath, "docker-cert-path", "", "path to the cert.pem, key.pem, and ca.pem for authenticating to Docker")
	flag.Parse()

	sd, err := statsd.NewClient("127.0.0.1:8125", "docker.containers")
	if err != nil {
		panic(err)
	}
	defer sd.Close()

	client := dockerClient(dockerHost, dockerTLSVerify, dockerCertPath)

	s := newSelector(sd)

	fmt.Println("Querying for running containers...")

	containers, err := client.ListContainers(docker.ListContainersOptions{
		All: true,
		Filters: map[string][]string{
			"status": []string{"running"},
		},
	})
	if err != nil {
		panic(err)
	}
	listenChan := make(chan *docker.APIEvents)
	client.AddEventListener(listenChan)
	go func() {
		for {
			event := <-listenChan

			if event.Status == "start" {
				w := newWatcher(event.ID[:12], client)
				s.Add(w)
				go w.Watch()
			}
		}
	}()

	for _, cont := range containers {
		w := newWatcher(cont.ID[:12], client)
		s.Add(w)
		go w.Watch()
	}

	fmt.Println("Waiting for stats...")
	s.Select()
}

type watcher struct {
	Name    string
	Stats   *stats
	Updates chan *docker.Stats
	client  *docker.Client
}

func newWatcher(name string, client *docker.Client) *watcher {
	return &watcher{
		Name:    name,
		Stats:   newStats(),
		Updates: make(chan *docker.Stats, 0),
		client:  client,
	}
}

func (w *watcher) Watch() {
	fmt.Printf("Watching %s...\n", w.Name)
	w.client.Stats(docker.StatsOptions{
		ID:    w.Name,
		Stats: w.Updates,
	})
}

type selector struct {
	cases        []reflect.SelectCase
	watchers     []*watcher
	statsdClient statsd.Statter
	mu           sync.RWMutex
}

func newSelector(statsdClient statsd.Statter) *selector {
	return &selector{
		cases:        make([]reflect.SelectCase, 0),
		watchers:     make([]*watcher, 0),
		statsdClient: statsdClient,
	}
}

func (s *selector) Add(w *watcher) {
	s.mu.Lock()
	defer s.mu.Unlock()

	s.cases = append(s.cases, reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(w.Updates)})
	s.watchers = append(s.watchers, w)
}

func (s *selector) remove(i int) {
	s.mu.Lock()
	defer s.mu.Unlock()

	s.cases = append(s.cases[:i], s.cases[i+1:]...)
	s.watchers = append(s.watchers[:i], s.watchers[i+1:]...)
}

func (s *selector) Select() {
	for {
		chosen, value, ok := reflect.Select(s.cases)
		s.mu.Lock()
		w := s.watchers[chosen]
		s.mu.Unlock()

		if !ok {
			fmt.Printf("Closing %s...\n", w.Name)
			s.remove(chosen)
			continue
		}

		switch value.Kind() {
		case reflect.Ptr:
			ds := value.Elem().Interface().(docker.Stats)
			w.Stats.Update(&ds)

			prefix := fmt.Sprintf("%s.memory", w.Name)
			s.statsdClient.Gauge(fmt.Sprintf("%s.used", prefix), int64(w.Stats.Memory), 1.0)
			s.statsdClient.Gauge(fmt.Sprintf("%s.limit", prefix), int64(w.Stats.MemoryLimit), 1.0)
			s.statsdClient.Gauge(fmt.Sprintf("%s.percent", prefix), int64(w.Stats.MemoryPercentage), 1.0)

			prefix = fmt.Sprintf("%s.cpu", w.Name)
			s.statsdClient.Gauge(fmt.Sprintf("%s.percent", prefix), int64(w.Stats.CPUPercentage), 1.0)

			prefix = fmt.Sprintf("%s.network", w.Name)
			s.statsdClient.Gauge(fmt.Sprintf("%s.rx", prefix), int64(w.Stats.NetworkRx), 1.0)
			s.statsdClient.Gauge(fmt.Sprintf("%s.tx", prefix), int64(w.Stats.NetworkTx), 1.0)

			w.Display()
		}

	}
}

type stats struct {
	CPUPercentage    float64
	Memory           float64
	MemoryLimit      float64
	MemoryPercentage float64
	NetworkRx        float64
	NetworkTx        float64

	previousCPUUsage       float64
	previousSystemCPUUsage float64
	cpuUsage               float64
	numberCPUs             float64
	systemCPUUsage         float64
	mu                     sync.RWMutex
}

func newStats() *stats {
	return &stats{
		cpuUsage:       0.0,
		systemCPUUsage: 0.0,
		CPUPercentage:  0.0,
	}
}

func (s *stats) Update(d *docker.Stats) {
	s.mu.Lock()
	s.previousCPUUsage = s.cpuUsage
	s.previousSystemCPUUsage = s.systemCPUUsage
	s.numberCPUs = float64(len(d.CPUStats.CPUUsage.PercpuUsage))
	s.cpuUsage = float64(d.CPUStats.CPUUsage.TotalUsage)
	s.systemCPUUsage = float64(d.CPUStats.SystemCPUUsage)
	s.calculateCPUPercentage()
	s.Memory = float64(d.MemoryStats.Usage)
	s.MemoryLimit = float64(d.MemoryStats.Limit)
	s.calculateMemoryPercentage()
	s.NetworkRx = float64(d.Network.RxBytes)
	s.NetworkTx = float64(d.Network.TxBytes)
	s.mu.Unlock()
}

func (s *stats) String() string {
	s.mu.RLock()
	defer s.mu.RUnlock()

	return fmt.Sprintf("%.2f%%\t%s/%s\t%.2f%%\t%s/%s",
		s.CPUPercentage,
		units.HumanSize(s.Memory), units.HumanSize(s.MemoryLimit),
		s.MemoryPercentage,
		units.HumanSize(s.NetworkRx), units.HumanSize(s.NetworkTx))
}

func (w *watcher) Display() {
	fmt.Printf("%s\t%s\n", w.Name, w.Stats.String())
}

func (s *stats) calculateMemoryPercentage() {
	s.MemoryPercentage = s.Memory / s.MemoryLimit * 100.0
}

func (s *stats) calculateCPUPercentage() {
	var (
		cpuPercent = 0.0
		// calculate the change for the cpu usage of the container in between readings
		cpuDelta = float64(s.cpuUsage - s.previousCPUUsage)
		// calculate the change for the entire system between readings
		systemDelta = float64(s.systemCPUUsage - s.previousSystemCPUUsage)
	)

	if systemDelta > 0.0 && cpuDelta > 0.0 {
		cpuPercent = (cpuDelta / systemDelta) * s.numberCPUs * 100.0
	}

	s.CPUPercentage = cpuPercent
}

func dockerClient(host string, tls bool, certPath string) *docker.Client {
	var client *docker.Client
	var err error

	if tls {
		cert := fmt.Sprintf("%s/cert.pem", certPath)
		key := fmt.Sprintf("%s/key.pem", certPath)
		ca := fmt.Sprintf("%s/ca.pem", certPath)
		client, err = docker.NewTLSClient(host, cert, key, ca)
	} else {
		client, err = docker.NewClient(host)
	}

	if err != nil {
		panic(err)
	}

	return client
}
<commit_msg>Add labels to display.<commit_after>package main

import (
	"fmt"
	"reflect"
	"sync"

	"github.com/cactus/go-statsd-client/statsd"
	"github.com/docker/docker/pkg/units"
	"github.com/fsouza/go-dockerclient"
	"github.com/namsral/flag"
)

func main() {
	var dockerHost string
	var dockerTLSVerify bool
	var dockerCertPath string

	flag.StringVar(&dockerHost, "docker-host", "unix:///var/run/docker.sock", "address of Docker host")
	flag.BoolVar(&dockerTLSVerify, "docker-tls-verify", false, "use TLS client for Docker")
	flag.StringVar(&dockerCertPath, "docker-cert-path", "", "path to the cert.pem, key.pem, and ca.pem for authenticating to Docker")
	flag.Parse()

	sd, err := statsd.NewClient("127.0.0.1:8125", "docker.containers")
	if err != nil {
		panic(err)
	}
	defer sd.Close()

	client := dockerClient(dockerHost, dockerTLSVerify, dockerCertPath)

	s := newSelector(sd)

	fmt.Println("Querying for running containers...")

	containers, err := client.ListContainers(docker.ListContainersOptions{
		All: true,
		Filters: map[string][]string{
			"status": []string{"running"},
		},
	})
	if err != nil {
		panic(err)
	}
	listenChan := make(chan *docker.APIEvents)
	client.AddEventListener(listenChan)
	go func() {
		for {
			event := <-listenChan

			if event.Status == "start" {
				w := newWatcher(event.ID[:12], client)
				s.Add(w)
				go w.Watch()
			}
		}
	}()

	for _, cont := range containers {
		w := newWatcher(cont.ID[:12], client)
		s.Add(w)
		go w.Watch()
	}

	fmt.Println("Waiting for stats...")
	s.Select()
}

type watcher struct {
	Name    string
	Stats   *stats
	Updates chan *docker.Stats
	client  *docker.Client
}

func newWatcher(name string, client *docker.Client) *watcher {
	return &watcher{
		Name:    name,
		Stats:   newStats(),
		Updates: make(chan *docker.Stats, 0),
		client:  client,
	}
}

func (w *watcher) Watch() {
	fmt.Printf("Watching %s...\n", w.Name)
	w.client.Stats(docker.StatsOptions{
		ID:    w.Name,
		Stats: w.Updates,
	})
}

type selector struct {
	cases        []reflect.SelectCase
	watchers     []*watcher
	statsdClient statsd.Statter
	mu           sync.RWMutex
}

func newSelector(statsdClient statsd.Statter) *selector {
	return &selector{
		cases:        make([]reflect.SelectCase, 0),
		watchers:     make([]*watcher, 0),
		statsdClient: statsdClient,
	}
}

func (s *selector) Add(w *watcher) {
	s.mu.Lock()
	defer s.mu.Unlock()

	s.cases = append(s.cases, reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(w.Updates)})
	s.watchers = append(s.watchers, w)
}

func (s *selector) remove(i int) {
	s.mu.Lock()
	defer s.mu.Unlock()

	s.cases = append(s.cases[:i], s.cases[i+1:]...)
	s.watchers = append(s.watchers[:i], s.watchers[i+1:]...)
}

func (s *selector) Select() {
	for {
		chosen, value, ok := reflect.Select(s.cases)
		s.mu.Lock()
		w := s.watchers[chosen]
		s.mu.Unlock()

		if !ok {
			fmt.Printf("Closing %s...\n", w.Name)
			s.remove(chosen)
			continue
		}

		switch value.Kind() {
		case reflect.Ptr:
			ds := value.Elem().Interface().(docker.Stats)
			w.Stats.Update(&ds)

			prefix := fmt.Sprintf("%s.memory", w.Name)
			s.statsdClient.Gauge(fmt.Sprintf("%s.used", prefix), int64(w.Stats.Memory), 1.0)
			s.statsdClient.Gauge(fmt.Sprintf("%s.limit", prefix), int64(w.Stats.MemoryLimit), 1.0)
			s.statsdClient.Gauge(fmt.Sprintf("%s.percent", prefix), int64(w.Stats.MemoryPercentage), 1.0)

			prefix = fmt.Sprintf("%s.cpu", w.Name)
			s.statsdClient.Gauge(fmt.Sprintf("%s.percent", prefix), int64(w.Stats.CPUPercentage), 1.0)

			prefix = fmt.Sprintf("%s.network", w.Name)
			s.statsdClient.Gauge(fmt.Sprintf("%s.rx", prefix), int64(w.Stats.NetworkRx), 1.0)
			s.statsdClient.Gauge(fmt.Sprintf("%s.tx", prefix), int64(w.Stats.NetworkTx), 1.0)

			w.Display()
		}

	}
}

type stats struct {
	CPUPercentage    float64
	Memory           float64
	MemoryLimit      float64
	MemoryPercentage float64
	NetworkRx        float64
	NetworkTx        float64

	previousCPUUsage       float64
	previousSystemCPUUsage float64
	cpuUsage               float64
	numberCPUs             float64
	systemCPUUsage         float64
	mu                     sync.RWMutex
}

func newStats() *stats {
	return &stats{
		cpuUsage:       0.0,
		systemCPUUsage: 0.0,
		CPUPercentage:  0.0,
	}
}

func (s *stats) Update(d *docker.Stats) {
	s.mu.Lock()
	s.previousCPUUsage = s.cpuUsage
	s.previousSystemCPUUsage = s.systemCPUUsage
	s.numberCPUs = float64(len(d.CPUStats.CPUUsage.PercpuUsage))
	s.cpuUsage = float64(d.CPUStats.CPUUsage.TotalUsage)
	s.systemCPUUsage = float64(d.CPUStats.SystemCPUUsage)
	s.calculateCPUPercentage()
	s.Memory = float64(d.MemoryStats.Usage)
	s.MemoryLimit = float64(d.MemoryStats.Limit)
	s.calculateMemoryPercentage()
	s.NetworkRx = float64(d.Network.RxBytes)
	s.NetworkTx = float64(d.Network.TxBytes)
	s.mu.Unlock()
}

func (s *stats) String() string {
	s.mu.RLock()
	defer s.mu.RUnlock()

	return fmt.Sprintf("CPU: %.2f%%\tMemory: %s/%s (%.2f%%)\tNetwork: %s in, %s out",
		s.CPUPercentage,
		units.HumanSize(s.Memory), units.HumanSize(s.MemoryLimit),
		s.MemoryPercentage,
		units.HumanSize(s.NetworkRx), units.HumanSize(s.NetworkTx))
}

func (w *watcher) Display() {
	fmt.Printf("%s\t%s\n", w.Name, w.Stats.String())
}

func (s *stats) calculateMemoryPercentage() {
	s.MemoryPercentage = s.Memory / s.MemoryLimit * 100.0
}

func (s *stats) calculateCPUPercentage() {
	var (
		cpuPercent = 0.0
		// calculate the change for the cpu usage of the container in between readings
		cpuDelta = float64(s.cpuUsage - s.previousCPUUsage)
		// calculate the change for the entire system between readings
		systemDelta = float64(s.systemCPUUsage - s.previousSystemCPUUsage)
	)

	if systemDelta > 0.0 && cpuDelta > 0.0 {
		cpuPercent = (cpuDelta / systemDelta) * s.numberCPUs * 100.0
	}

	s.CPUPercentage = cpuPercent
}

func dockerClient(host string, tls bool, certPath string) *docker.Client {
	var client *docker.Client
	var err error

	if tls {
		cert := fmt.Sprintf("%s/cert.pem", certPath)
		key := fmt.Sprintf("%s/key.pem", certPath)
		ca := fmt.Sprintf("%s/ca.pem", certPath)
		client, err = docker.NewTLSClient(host, cert, key, ca)
	} else {
		client, err = docker.NewClient(host)
	}

	if err != nil {
		panic(err)
	}

	return client
}
<|endoftext|>