<commit_before>package main

import(
	"io/ioutil"
	"encoding/json"
	"net/http"
	"os"
	"io"
	"text/template"
	"fmt"
	"strconv"
	"flag"
)

// Account Model
type AccountEntry struct {
	Username string
	Password string
	SuccessRate string `json:"success_rate"`
	Upvotes string
	Posted string
}

// Account Collection
type AccountBook struct {
	Accounts []*AccountEntry
}

func main(){

	// adding usage
	flag.Usage = usage
	flag.Parse()

	args:=flag.Args()

	if len(args) != 1 {
		usage()
	}

	query := args[0]

	accountBook,_ := bmn(query)
	if len(accountBook.Accounts) > 0 {
		printMessage(os.Stdout,strconv.Itoa(len(accountBook.Accounts))+" accounts for "+query)
	} else {
		printMessage(os.Stdout,"No accounts for this domain.")
	}

	for _,account := range accountBook.Accounts {
		printAccount(os.Stdout,account)
	}
}

// Usage

const usageTmpl = `BMN is a command-line utility to find logins/passwords for websites that force you to register.
Usage:
  bmn [website]
`

func usage() {
	printUsage(os.Stderr)
	os.Exit(2)
}

func printUsage(w io.Writer) {
	tmpl(w, usageTmpl, nil)
}

// Result

const accountTmpl = `
	Username: {{.Username}}
	Password: {{.Password}}
	SuccessRate: {{.SuccessRate}}
	Upvotes: {{.Upvotes}}
	Posted: {{.Posted}}

`

func printAccount(w io.Writer,a *AccountEntry){
	tmpl(w,accountTmpl,a)
}

// Helper Functions

// Messages

const messageTmpl = `
	{{.Message}}
`
// printing message on command line
func printMessage(w io.Writer,msg string) {
	tmpl(w,messageTmpl,struct {Message string}{msg})
}

func printErr(err error) {
	fmt.Fprintln(os.Stderr, err.Error())
	os.Exit(2)
}

const BASE_URL = "http://bugmenotapi.herokuapp.com/"

// Fetching data from bmn webservice
func bmn(website string)(*AccountBook,error) {

	res, err := http.Get(BASE_URL+website)
	if err != nil {
		return nil,err
	}
	body, err := ioutil.ReadAll(res.Body)
	res.Body.Close()
	if err != nil {
		return nil,err
	}
	var accountBook AccountBook
	json.Unmarshal(body,&accountBook)

	return &accountBook,nil
}

// Output template
func tmpl(w io.Writer, text string, data interface{}) {
	t := template.New("top")
	template.Must(t.Parse(text))
	if err := t.Execute(w, data); err != nil {
		panic(err)
	}
}
<commit_msg>updated comments<commit_after>package main

import(
	"io/ioutil"
	"encoding/json"
	"net/http"
	"os"
	"io"
	"text/template"
	"fmt"
	"strconv"
	"flag"
)

// Account Model
type AccountEntry struct {
	Username string
	Password string
	SuccessRate string `json:"success_rate"`
	Upvotes string
	Posted string
}

// Account Collection
type AccountBook struct {
	Accounts []*AccountEntry
}

func main(){

	// adding usage
	flag.Usage = usage
	flag.Parse()

	args:=flag.Args()

	if len(args) != 1 {
		usage()
	}

	query := args[0]

	accountBook,_ := bmn(query)
	if len(accountBook.Accounts) > 0 {
		printMessage(os.Stdout,strconv.Itoa(len(accountBook.Accounts))+" accounts for "+query)
	} else {
		printMessage(os.Stdout,"No accounts for this domain.")
	}

	for _,account := range accountBook.Accounts {
		printAccount(os.Stdout,account)
	}
}

// Usage

const usageTmpl = `BMN is a command-line utility to find logins/passwords for websites that force you to register.
Usage:
  bmn [website]
`

func usage() {
	printUsage(os.Stderr)
	os.Exit(2)
}

func printUsage(w io.Writer) {
	tmpl(w, usageTmpl, nil)
}

// Result

const accountTmpl = `
	Username: {{.Username}}
	Password: {{.Password}}
	SuccessRate: {{.SuccessRate}}
	Upvotes: {{.Upvotes}}
	Posted: {{.Posted}}

`

func printAccount(w io.Writer,a *AccountEntry){
	tmpl(w,accountTmpl,a)
}

// Helper Functions

// Messages

const messageTmpl = `
	{{.Message}}
`
// printing message on command line
func printMessage(w io.Writer,msg string) {
	tmpl(w,messageTmpl,struct {Message string}{msg})
}

// logging error
func printErr(err error) {
	fmt.Fprintln(os.Stderr, err.Error())
	os.Exit(2)
}

const BASE_URL = "http://bugmenotapi.herokuapp.com/"

// Fetching data from bmn webservice
func bmn(website string)(*AccountBook,error) {

	res, err := http.Get(BASE_URL+website)
	if err != nil {
		return nil,err
	}
	body, err := ioutil.ReadAll(res.Body)
	res.Body.Close()
	if err != nil {
		return nil,err
	}
	var accountBook AccountBook
	json.Unmarshal(body,&accountBook)

	return &accountBook,nil
}

// Output template
func tmpl(w io.Writer, text string, data interface{}) {
	t := template.New("top")
	template.Must(t.Parse(text))
	if err := t.Execute(w, data); err != nil {
		panic(err)
	}
}
<|endoftext|>


<commit_before>package main

import "image/png"
import "image/color"
import "os"
import "log"

var viewPoint ViewPoint
var light Light
var screen *Screen
var openingAngleOfCamera float64
var correctionOfSquareChessboard float64

/**
 * GetColorImagePixel
 *
 * Gets the color of a Pixel in Image. If there is a intersection with a item, it gets the
 * redirected ray and calls GetColorImagePixel again. So you can see reflections on the items.
 *
 * @param ray Ray to cross the items
 * @param items All items in the screen
 * @param itemBefore Item which was crossed before, is needed in recursion case
 * @param recursionDeepness How often reflextions are allowed
 * @param reflectionStrength How strong the reflection should be
 * @return color.RGBA Calculated color if there was a intersection
 * @return bool Says, if the returned color should be used or not.Happens, if the ray goes nowhere
 */

func GetColorOfPixelInImage(ray Ray, items []SceneObject, itemBefore int, recursionDeepness int, reflectionStrength float32) (color.RGBA, bool) {

	if recursionDeepness == 0 {
		return color.RGBA{0, 0, 0, 0}, false
	}

	var minDistanceOfAllItems float64 = 99999999999
	actualShortestInAllItems := 0
	intersectionWithAnyItem := false

outer:
	for index, item := range items { //Find nearest item
		if itemBefore == index { //Avoids crossing the same item again, could happen because of rounding errors
			continue outer
		}

		distance, _, _, intersection := item.RayIntersection(ray)

		if intersection == false { //No intersection with item
			continue outer
		}

		if distance < 0 { //If distance < 0, intersection behind viewpoint -> uninteresting intersection
			continue outer
		}

		if distance < minDistanceOfAllItems {
			intersectionWithAnyItem = true
			minDistanceOfAllItems = distance
			actualShortestInAllItems = index
		}

	}

	if intersectionWithAnyItem == false {
		return color.RGBA{0, 0, 0, 0}, false
	} else {
		_, redirectedRay, color, _ := items[actualShortestInAllItems].RayIntersection(ray) //Get detailed information of intersection item

		isShadow := false
		// is shadow?
		for _, _item := range items {
			_distance, _, _, _intersection := _item.RayIntersection(redirectedRay)

			if _intersection && _distance >= 0 {
				isShadow = true
				break
			}
		}
		if isShadow {
			//fmt.Println("IsShadow %v %v", ray, redirectedRay)
		}
		// func CalcBrightness(in float64, b, L, N, V Vector, m Material, n float64) float64 {
		// TO DO: L: Einheitsvektor in Richtung der Lichtquelle
		// TO DO: N: Normale des scenceobject
		//CalcBrightness(255, ray.direction, nil, nil, redirectedRay, Polyethylene, 20)

		//Starting recursion,
		colorOfNextIntersectionItem, useThisColor := GetColorOfPixelInImage(redirectedRay, items, actualShortestInAllItems, recursionDeepness-1, reflectionStrength)
		if useThisColor == false {
			return color, true

		} else {
			return MergeColors(color, colorOfNextIntersectionItem, reflectionStrength), true
		}
	}
}

func main() {
	log.Println("Starting programm Raytracing (Stefan B., Matthias F.)")

	//Setting global variables
	screen = NewScreen(1600, 1200) //width, height
	viewPoint = ViewPoint{Vector{17, 17, -60}}
	light = Light{Vector{30, 30, 30}}
	openingAngleOfCamera = 200
	correctionOfSquareChessboard = 4

	//Local variables
	recursionDeepness := 5         //Maximum number of reflextions between items
	var reflection float32 = 0.001 //Value between 0.0 and 1.0. Should be under 0.003, if not it creates wrong colors
	items := make([]SceneObject, 0)
	nameOfOutputFile := "test.png"

	//Add Balls
	items = append(items, NewSphere(NewVector(4, 9, 14), 9.0, color.RGBA{200, 100, 0, 255}))
	items = append(items, NewSphere(NewVector(25, 9, 10), 4.0, color.RGBA{50, 25, 80, 255}))
	//items = append(items, NewSphere(NewVector(22, 8, 5), 6.0, color.RGBA{80, 0, 255, 255}))
	//items = append(items, NewSphere(NewVector(20, 20, 20), 8.0, color.RGBA{255, 255, 25, 255}))
	items = append(items, NewPlane(NewVector(0, -2, 3), NewVector(0, 8, 1), color.RGBA{0, 0, 0, 255})) //Location, direction, color

	log.Println("Screen size:", screen.width, "x", screen.height)
	log.Println("Location of Viewpoint:", viewPoint.Point.x, viewPoint.Point.y, viewPoint.Point.z)
	log.Println("Nr of items to intersect with:", len(items))
	log.Println("Recursion deepness:", recursionDeepness)

	for index, item := range items {
		log.Println("Values of item", index, ":", item)
	}

	for x := 0; x <= screen.width; x++ {
		for y := 0; y <= screen.height; y++ {
			//x is the x axis, positiv is right
			//y is the y axis, positiv is top

			r := viewPoint.CreateRayFromViewPointThroughScreen(x, y)

			colorOfPixel, _ := GetColorOfPixelInImage(r, items, -1, recursionDeepness, reflection)

			RemoveTransparency(&colorOfPixel)
			screen.image.SetRGBA(x, y, colorOfPixel)
		}
	}

	log.Println("Creating output file", nameOfOutputFile)
	w, _ := os.Create(nameOfOutputFile)
	if err := png.Encode(w, screen.image); err != nil {
		log.Println("Error writing image on Disk")
		os.Exit(1)
	}
}
<commit_msg>... phong<commit_after>package main

import "image/png"
import "image/color"
import "os"
import "log"

var viewPoint ViewPoint
var light Light
var screen *Screen
var openingAngleOfCamera float64
var correctionOfSquareChessboard float64

/**
 * GetColorImagePixel
 *
 * Gets the color of a Pixel in Image. If there is a intersection with a item, it gets the
 * redirected ray and calls GetColorImagePixel again. So you can see reflections on the items.
 *
 * @param ray Ray to cross the items
 * @param items All items in the screen
 * @param itemBefore Item which was crossed before, is needed in recursion case
 * @param recursionDeepness How often reflextions are allowed
 * @param reflectionStrength How strong the reflection should be
 * @return color.RGBA Calculated color if there was a intersection
 * @return bool Says, if the returned color should be used or not.Happens, if the ray goes nowhere
 */

func GetColorOfPixelInImage(ray Ray, items []SceneObject, itemBefore int, recursionDeepness int, reflectionStrength float32) (color.RGBA, bool) {

	if recursionDeepness == 0 {
		return color.RGBA{0, 0, 0, 0}, false
	}

	var minDistanceOfAllItems float64 = 99999999999
	actualShortestInAllItems := 0
	intersectionWithAnyItem := false

outer:
	for index, item := range items { //Find nearest item
		if itemBefore == index { //Avoids crossing the same item again, could happen because of rounding errors
			continue outer
		}

		distance, _, _, intersection := item.RayIntersection(ray)

		if intersection == false { //No intersection with item
			continue outer
		}

		if distance < 0 { //If distance < 0, intersection behind viewpoint -> uninteresting intersection
			continue outer
		}

		if distance < minDistanceOfAllItems {
			intersectionWithAnyItem = true
			minDistanceOfAllItems = distance
			actualShortestInAllItems = index
		}

	}

	if intersectionWithAnyItem == false {
		return color.RGBA{0, 0, 0, 0}, false
	} else {
		_, redirectedRay, color, _ := items[actualShortestInAllItems].RayIntersection(ray) //Get detailed information of intersection item

		// TO DO: Calc ray from intersection to light pos
		rayToLight := redirectedRay
		isShadow := false
		// is shadow?
		for _, _item := range items {
			_distance, _, _, _intersection := _item.RayIntersection(rayToLight)

			if _intersection && _distance >= 0 {
				isShadow = true
				break
			}
		}
		if isShadow {
			//fmt.Println("IsShadow %v %v", ray, redirectedRay)
		}
		// func CalcBrightness(in float64, b, L, N, V Vector, m Material, n float64) float64 {
		// TO DO: L: Einheitsvektor in Richtung der Lichtquelle
		// TO DO: N: Normale des scenceobject
		//CalcBrightness(255, ray.direction, nil, nil, redirectedRay, Polyethylene, 20)

		//Starting recursion,
		colorOfNextIntersectionItem, useThisColor := GetColorOfPixelInImage(redirectedRay, items, actualShortestInAllItems, recursionDeepness-1, reflectionStrength)
		if useThisColor == false {
			return color, true

		} else {
			return MergeColors(color, colorOfNextIntersectionItem, reflectionStrength), true
		}
	}
}

func main() {
	log.Println("Starting programm Raytracing (Stefan B., Matthias F.)")

	//Setting global variables
	screen = NewScreen(1600, 1200) //width, height
	viewPoint = ViewPoint{Vector{17, 17, -60}}
	light = Light{Vector{30, 30, 30}}
	openingAngleOfCamera = 200
	correctionOfSquareChessboard = 4

	//Local variables
	recursionDeepness := 5         //Maximum number of reflextions between items
	var reflection float32 = 0.001 //Value between 0.0 and 1.0. Should be under 0.003, if not it creates wrong colors
	items := make([]SceneObject, 0)
	nameOfOutputFile := "test.png"

	//Add Balls
	items = append(items, NewSphere(NewVector(4, 9, 14), 9.0, color.RGBA{200, 100, 0, 255}))
	items = append(items, NewSphere(NewVector(25, 9, 10), 4.0, color.RGBA{50, 25, 80, 255}))
	//items = append(items, NewSphere(NewVector(22, 8, 5), 6.0, color.RGBA{80, 0, 255, 255}))
	//items = append(items, NewSphere(NewVector(20, 20, 20), 8.0, color.RGBA{255, 255, 25, 255}))
	items = append(items, NewPlane(NewVector(0, -2, 3), NewVector(0, 8, 1), color.RGBA{0, 0, 0, 255})) //Location, direction, color

	log.Println("Screen size:", screen.width, "x", screen.height)
	log.Println("Location of Viewpoint:", viewPoint.Point.x, viewPoint.Point.y, viewPoint.Point.z)
	log.Println("Nr of items to intersect with:", len(items))
	log.Println("Recursion deepness:", recursionDeepness)

	for index, item := range items {
		log.Println("Values of item", index, ":", item)
	}

	for x := 0; x <= screen.width; x++ {
		for y := 0; y <= screen.height; y++ {
			//x is the x axis, positiv is right
			//y is the y axis, positiv is top

			r := viewPoint.CreateRayFromViewPointThroughScreen(x, y)

			colorOfPixel, _ := GetColorOfPixelInImage(r, items, -1, recursionDeepness, reflection)

			RemoveTransparency(&colorOfPixel)
			screen.image.SetRGBA(x, y, colorOfPixel)
		}
	}

	log.Println("Creating output file", nameOfOutputFile)
	w, _ := os.Create(nameOfOutputFile)
	if err := png.Encode(w, screen.image); err != nil {
		log.Println("Error writing image on Disk")
		os.Exit(1)
	}
}
<|endoftext|>


<commit_before>package main //import "github.com/tutumcloud/events"

import (
	"bufio"
	"bytes"
	"encoding/json"
	"errors"
	"github.com/getsentry/raven-go"
	"log"
	"net/http"
	"os"
	"os/exec"
	"regexp"
	"runtime"
	"strconv"
	"strings"
	"time"
)

type Event struct {
	Status     string `json:"status"`
	ID         string `json:"id"`
	From       string `json:"from"`
	Time       int64  `json:"time"`
	HandleTime int64  `json:"handletime"`
	ExitCode   string `json:"exitcode"`
}

func init() {
	runtime.GOMAXPROCS(4)
}

const (
	VERSION    = "0.1"
	DockerPath = "/usr/bin/docker"
)

var (
	AutorestartEvents = make([]Event, 0)
	UserAgent         = "tutum-events/" + VERSION
	ReportInterval    int
	TutumAuth         string
	TutumUrl          string
	sentryClient      *raven.Client = nil
	DSN               string
)

func main() {
	TutumAuth = os.Getenv("TUTUM_AUTH")
	TutumUrl = os.Getenv("TUTUM_URL")
	if TutumAuth == "**None**" {
		log.Fatal("TUTUM_AUTH must be specified")
	}
	if TutumUrl == "**None**" {
		log.Fatal("TUTUM_URL must be specified")
	}

	DSN = os.Getenv("SENTRY_DSN")

	if !fileExist(DockerPath) {
		log.Fatal("docker client is not mounted to", DockerPath)
	}

	intervalStr := os.Getenv("REPORT_INTERVAL")

	interval, err := strconv.Atoi(intervalStr)
	if err != nil {
		ReportInterval = 30
	} else {
		ReportInterval = interval
	}

	log.Println("POST docker event to:", TutumUrl)

	cmd := exec.Command(DockerPath, "version")
	if err := cmd.Start(); err != nil {
		sendError(err, "Fatal: Failed to run docker version", nil)
		log.Println(err)
		return
	}
	cmd.Wait()

	monitorEvents()
}

func monitorEvents() {
	ticker := time.NewTicker(time.Second * time.Duration(ReportInterval))
	go func() {
		for {
			select {
			case <-ticker.C:
				events := AutorestartEvents
				AutorestartEvents = make([]Event, 0)
				if len(events) > 0 {
					go sendContainerAutoRestartEvents(events)
				}
			}
		}
	}()

	log.Println("docker events starts")
	cmd := exec.Command(DockerPath, "events")
	cmdReader, err := cmd.StdoutPipe()
	if err != nil {
		log.Fatal("Error creating StdoutPipe for Cmd", err)
	}

	scanner := bufio.NewScanner(cmdReader)
	go func() {
		for scanner.Scan() {
			eventStr := scanner.Text()
			if eventStr != "" {
				re := regexp.MustCompile("(.*) (.{64}): \\(from (.*)\\) (.*)")
				terms := re.FindStringSubmatch(eventStr)
				if len(terms) == 5 {
					var event Event
					eventTime, err := time.Parse(time.RFC3339Nano, terms[1])
					if err == nil {
						event.Time = eventTime.Unix()
					} else {
						event.Time = time.Now().Unix()
					}
					event.ID = terms[2]
					event.From = terms[3]
					event.Status = terms[4]
					event.HandleTime = time.Now().UnixNano()
					go eventHandler(event)
				}
			}
		}
		if scanner.Err() == nil {
			log.Fatal("The scanner returns an error:", "EOF")
		} else {
			log.Fatal("The scanner returns an error:", scanner.Err())
		}
	}()

	err = cmd.Start()
	if err != nil {
		log.Fatal("Error starting docker evets", err)
	}

	err = cmd.Wait()
	if err != nil {
		log.Fatal("Error waiting for docker events", err)
	}
	log.Println("docker events stops")
}

func eventHandler(event Event) {
	event.ExitCode = "0"
	isRestart := false
	if strings.ToLower(event.Status) == "start" ||
		strings.ToLower(event.Status) == "die" {

		result, err := exec.Command(DockerPath, "inspect", "-f",
			"{{index .HostConfig.RestartPolicy.Name}} {{index .State.ExitCode}}",
			event.ID).Output()

		if err == nil && len(result) > 0 {
			terms := strings.Split(string(result), " ")
			if len(terms) == 2 {
				if strings.HasPrefix(strings.ToLower(terms[0]), "on-failure") ||
					strings.HasPrefix(strings.ToLower(terms[0]), "always") {
					isRestart = true
				}
				event.ExitCode = strings.Trim(terms[1], "\n")
			}
		}
		if isRestart {
			AutorestartEvents = append(AutorestartEvents, event)
		} else {
			sendContainerEvent(event)
		}
	}
}

func sendContainerAutoRestartEvents(events []Event) {
	data, err := json.Marshal(events)
	if err != nil {
		log.Printf("Cannot marshal the posting data: %s\n", events)
	}
	sendData(data)
}

func sendContainerEvent(event Event) {
	data, err := json.Marshal(event)
	if err != nil {
		log.Printf("Cannot marshal the posting data: %s\n", event)
	}
	sendData(data)
}

func sendData(data []byte) {
	counter := 1
	for {
		log.Println("sending event: ", string(data))
		err := send(TutumUrl, data)
		if err == nil {
			break
		} else {
			if counter > 100 {
				log.Println("Too many reties, give up")
				break
			} else {
				counter *= 2
				log.Printf("%s: Retry in %d seconds", err, counter)
				time.Sleep(time.Duration(counter) * time.Second)
			}
		}
	}
}

func send(url string, data []byte) error {
	client := &http.Client{}
	req, err := http.NewRequest("POST", url, bytes.NewReader(data))
	if err != nil {
		sendError(err, "Failed to create http.NewRequest", nil)
		return err
	}
	req.Header.Add("Authorization", TutumAuth)
	req.Header.Add("User-Agent", UserAgent)
	resp, err := client.Do(req)
	if err != nil {
		extra := map[string]interface{}{"data": string(data)}
		sendError(err, "Failed to POST the http request", extra)
		return err
	}
	defer resp.Body.Close()

	if resp.StatusCode >= 400 {
		log.Printf("Send event failed: %s - %s", resp.Status, string(data))
		extra := map[string]interface{}{"data": string(data)}
		sendError(errors.New(resp.Status), "http error", extra)
		if resp.StatusCode >= 500 {
			return errors.New(resp.Status)
		}
	}
	return nil
}

func fileExist(file string) bool {
	if _, err := os.Stat(file); err != nil {
		if os.IsNotExist(err) {
			return false
		}
	}
	return true
}

func getSentryClient() *raven.Client {
	if sentryClient == nil && DSN != "" {
		client, _ := raven.NewClient(DSN, nil)
		sentryClient = client
	}
	return sentryClient
}

func sendError(err error, msg string, extra map[string]interface{}) {
	go func() {
		client := getSentryClient()
		if sentryClient != nil {
			packet := &raven.Packet{Message: msg, Interfaces: []raven.Interface{raven.NewException(err, raven.NewStacktrace(0, 5, nil))}}
			if extra != nil {
				packet.Extra = extra
			}
			_, ch := client.Capture(packet, nil)
			<-ch
		}
	}()
}
<commit_msg>bump ver<commit_after>package main //import "github.com/tutumcloud/events"

import (
	"bufio"
	"bytes"
	"encoding/json"
	"errors"
	"github.com/getsentry/raven-go"
	"log"
	"net/http"
	"os"
	"os/exec"
	"regexp"
	"runtime"
	"strconv"
	"strings"
	"time"
)

type Event struct {
	Status     string `json:"status"`
	ID         string `json:"id"`
	From       string `json:"from"`
	Time       int64  `json:"time"`
	HandleTime int64  `json:"handletime"`
	ExitCode   string `json:"exitcode"`
}

func init() {
	runtime.GOMAXPROCS(4)
}

const (
	VERSION    = "0.2"
	DockerPath = "/usr/bin/docker"
)

var (
	AutorestartEvents = make([]Event, 0)
	UserAgent         = "tutum-events/" + VERSION
	ReportInterval    int
	TutumAuth         string
	TutumUrl          string
	sentryClient      *raven.Client = nil
	DSN               string
)

func main() {
	TutumAuth = os.Getenv("TUTUM_AUTH")
	TutumUrl = os.Getenv("TUTUM_URL")
	if TutumAuth == "**None**" {
		log.Fatal("TUTUM_AUTH must be specified")
	}
	if TutumUrl == "**None**" {
		log.Fatal("TUTUM_URL must be specified")
	}

	DSN = os.Getenv("SENTRY_DSN")

	if !fileExist(DockerPath) {
		log.Fatal("docker client is not mounted to", DockerPath)
	}

	intervalStr := os.Getenv("REPORT_INTERVAL")

	interval, err := strconv.Atoi(intervalStr)
	if err != nil {
		ReportInterval = 30
	} else {
		ReportInterval = interval
	}

	log.Println("POST docker event to:", TutumUrl)

	cmd := exec.Command(DockerPath, "version")
	if err := cmd.Start(); err != nil {
		sendError(err, "Fatal: Failed to run docker version", nil)
		log.Println(err)
		return
	}
	cmd.Wait()

	monitorEvents()
}

func monitorEvents() {
	ticker := time.NewTicker(time.Second * time.Duration(ReportInterval))
	go func() {
		for {
			select {
			case <-ticker.C:
				events := AutorestartEvents
				AutorestartEvents = make([]Event, 0)
				if len(events) > 0 {
					go sendContainerAutoRestartEvents(events)
				}
			}
		}
	}()

	log.Println("docker events starts")
	cmd := exec.Command(DockerPath, "events")
	cmdReader, err := cmd.StdoutPipe()
	if err != nil {
		log.Fatal("Error creating StdoutPipe for Cmd", err)
	}

	scanner := bufio.NewScanner(cmdReader)
	go func() {
		for scanner.Scan() {
			eventStr := scanner.Text()
			if eventStr != "" {
				re := regexp.MustCompile("(.*) (.{64}): \\(from (.*)\\) (.*)")
				terms := re.FindStringSubmatch(eventStr)
				if len(terms) == 5 {
					var event Event
					eventTime, err := time.Parse(time.RFC3339Nano, terms[1])
					if err == nil {
						event.Time = eventTime.Unix()
					} else {
						event.Time = time.Now().Unix()
					}
					event.ID = terms[2]
					event.From = terms[3]
					event.Status = terms[4]
					event.HandleTime = time.Now().UnixNano()
					go eventHandler(event)
				}
			}
		}
		if scanner.Err() == nil {
			log.Fatal("The scanner returns an error:", "EOF")
		} else {
			log.Fatal("The scanner returns an error:", scanner.Err())
		}
	}()

	err = cmd.Start()
	if err != nil {
		log.Fatal("Error starting docker evets", err)
	}

	err = cmd.Wait()
	if err != nil {
		log.Fatal("Error waiting for docker events", err)
	}
	log.Println("docker events stops")
}

func eventHandler(event Event) {
	event.ExitCode = "0"
	isRestart := false
	if strings.ToLower(event.Status) == "start" ||
		strings.ToLower(event.Status) == "die" {

		result, err := exec.Command(DockerPath, "inspect", "-f",
			"{{index .HostConfig.RestartPolicy.Name}} {{index .State.ExitCode}}",
			event.ID).Output()

		if err == nil && len(result) > 0 {
			terms := strings.Split(string(result), " ")
			if len(terms) == 2 {
				if strings.HasPrefix(strings.ToLower(terms[0]), "on-failure") ||
					strings.HasPrefix(strings.ToLower(terms[0]), "always") {
					isRestart = true
				}
				event.ExitCode = strings.Trim(terms[1], "\n")
			}
		}
		if isRestart {
			AutorestartEvents = append(AutorestartEvents, event)
		} else {
			sendContainerEvent(event)
		}
	}
}

func sendContainerAutoRestartEvents(events []Event) {
	data, err := json.Marshal(events)
	if err != nil {
		log.Printf("Cannot marshal the posting data: %s\n", events)
	}
	sendData(data)
}

func sendContainerEvent(event Event) {
	data, err := json.Marshal(event)
	if err != nil {
		log.Printf("Cannot marshal the posting data: %s\n", event)
	}
	sendData(data)
}

func sendData(data []byte) {
	counter := 1
	for {
		log.Println("sending event: ", string(data))
		err := send(TutumUrl, data)
		if err == nil {
			break
		} else {
			if counter > 100 {
				log.Println("Too many reties, give up")
				break
			} else {
				counter *= 2
				log.Printf("%s: Retry in %d seconds", err, counter)
				time.Sleep(time.Duration(counter) * time.Second)
			}
		}
	}
}

func send(url string, data []byte) error {
	client := &http.Client{}
	req, err := http.NewRequest("POST", url, bytes.NewReader(data))
	if err != nil {
		sendError(err, "Failed to create http.NewRequest", nil)
		return err
	}
	req.Header.Add("Authorization", TutumAuth)
	req.Header.Add("User-Agent", UserAgent)
	resp, err := client.Do(req)
	if err != nil {
		extra := map[string]interface{}{"data": string(data)}
		sendError(err, "Failed to POST the http request", extra)
		return err
	}
	defer resp.Body.Close()

	if resp.StatusCode >= 400 {
		log.Printf("Send event failed: %s - %s", resp.Status, string(data))
		extra := map[string]interface{}{"data": string(data)}
		sendError(errors.New(resp.Status), "http error", extra)
		if resp.StatusCode >= 500 {
			return errors.New(resp.Status)
		}
	}
	return nil
}

func fileExist(file string) bool {
	if _, err := os.Stat(file); err != nil {
		if os.IsNotExist(err) {
			return false
		}
	}
	return true
}

func getSentryClient() *raven.Client {
	if sentryClient == nil && DSN != "" {
		client, _ := raven.NewClient(DSN, nil)
		sentryClient = client
	}
	return sentryClient
}

func sendError(err error, msg string, extra map[string]interface{}) {
	go func() {
		client := getSentryClient()
		if sentryClient != nil {
			packet := &raven.Packet{Message: msg, Interfaces: []raven.Interface{raven.NewException(err, raven.NewStacktrace(0, 5, nil))}}
			if extra != nil {
				packet.Extra = extra
			}
			_, ch := client.Capture(packet, nil)
			<-ch
		}
	}()
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"fmt"
	"log"

	"code.google.com/p/gcfg"
)

var configPath = flag.String("class", "fighter", "Value of class to build.  Default is: fighter")

//You can put the flag definitions in the init function as well.
//Nothings gets called until you call flag.Parse()

func main() {
	var cfg Config
	flag.Parse()
	configpath := fmt.Sprintf("config/%v.gcfg", *configPath)
	if err := gcfg.ReadFileInto(&cfg, configpath); err != nil {
		log.Println("Error: ", err)
	} else {
		character := NewCharacter(cfg)
		fmt.Printf("Character Info...\n%v\n", character)
	}

}
<commit_msg>added error typing example<commit_after>package main

import (
	"flag"
	"fmt"
	"os"

	"code.google.com/p/gcfg"
)

var configPath = flag.String("class", "fighter", "Value of class to build.  Default is: fighter")

//You can put the flag definitions in the init function as well.
//Flag values are pulled in until you call flag.Parse()

func main() {
	var cfg Config
	flag.Parse()
	configpath := fmt.Sprintf("config/%v.gcfg", *configPath)
	if err := gcfg.ReadFileInto(&cfg, configpath); err != nil {
		if pathErr, ok := err.(*os.PathError); ok {
			fmt.Printf("PathError: %v\n", pathErr)
			fmt.Printf("Op: %v\nPath: %v\n", pathErr.Op, pathErr.Path)
		} else {
			fmt.Printf("Error Type: %T", err)
			fmt.Println("Error: ", err)
		}
	} else {
		character := NewCharacter(cfg)
		fmt.Printf("Character Info...\n%v\n", character)
	}
}
<|endoftext|>


<commit_before>package main

import "net/http"

func main() {
	http.HandleFunc("/hostname", hostnameHandler)
	http.HandleFunc("/time", timeHandler)
	http.HandleFunc("/issue", issueHandler)
	http.ListenAndServe(":3000", nil)
}

func hostnameHandler(w http.ResponseWriter, r *http.Request) {
	hostname, err := hostname()
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	w.Write([]byte(hostname))
}

func timeHandler(w http.ResponseWriter, r *http.Request) {
	time, err := time()
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	w.Write([]byte(time))
}

func issueHandler(w http.ResponseWriter, r *http.Request) {
	distro, kernel, err := issue()
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	w.Write([]byte(distro + " " + kernel))
}
<commit_msg>Add print statement to indicate server is running<commit_after>package main

import (
	"fmt"
	"net/http"
)

func main() {
	http.HandleFunc("/hostname", hostnameHandler)
	http.HandleFunc("/time", timeHandler)
	http.HandleFunc("/issue", issueHandler)
	fmt.Println("Serving on port 3000...")
	http.ListenAndServe(":3000", nil)
}

func hostnameHandler(w http.ResponseWriter, r *http.Request) {
	hostname, err := hostname()
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	w.Write([]byte(hostname))
}

func timeHandler(w http.ResponseWriter, r *http.Request) {
	time, err := time()
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	w.Write([]byte(time))
}

func issueHandler(w http.ResponseWriter, r *http.Request) {
	distro, kernel, err := issue()
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	w.Write([]byte(distro + " " + kernel))
}
<|endoftext|>


<commit_before>package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"strings"

	"github.com/alecthomas/chroma"
	"github.com/alecthomas/chroma/formatters/html"
	"github.com/alecthomas/chroma/lexers"
	"github.com/alecthomas/chroma/styles"
	"github.com/niklasfasching/go-org/blorg"
	"github.com/niklasfasching/go-org/org"
)

func main() {
	log := log.New(os.Stderr, "", 0)
	if len(os.Args) < 2 {
		log.Println("USAGE: org COMMAND [ARGS]")
		log.Println("- org render FILE OUTPUT_FORMAT")
		log.Println("  OUTPUT_FORMAT: org, html, html-chroma")
		log.Println("- org blorg init")
		log.Println("- org blorg build")
		log.Println("- org blorg serve")
		os.Exit(1)
	}

	switch cmd := strings.ToLower(os.Args[1]); cmd {
	case "render":
		if len(os.Args) < 4 {
			log.Fatal("USAGE: org render FILE OUTPUT_FORMAT")
		}
		out, err := render(os.Args[2], os.Args[3])
		if err != nil {
			log.Fatalf("Error: %v", err)
		}
		fmt.Fprint(os.Stdout, out)
	case "blorg":
		if err := runBlorg(os.Args[2]); err != nil {
			log.Fatalf("Error: %v", err)
		}
	default:
		log.Fatalf("Unsupported command: %s", cmd)
	}
}

func runBlorg(cmd string) error {
	switch strings.ToLower(cmd) {
	case "init":
		if _, err := os.Stat(blorg.DefaultConfigFile); !os.IsNotExist(err) {
			return err
		}
		if err := ioutil.WriteFile(blorg.DefaultConfigFile, []byte(blorg.DefaultConfig), os.ModePerm); err != nil {
			return err
		}
		log.Printf("blorg init finished: Wrote ./%s", blorg.DefaultConfigFile)
		return nil
	case "build":
		config, err := blorg.ReadConfig(blorg.DefaultConfigFile)
		if err != nil {
			return err
		}
		if err := config.Render(); err != nil {
			return err
		}
		log.Println("blorg build finished")
		return nil
	case "serve":
		config, err := blorg.ReadConfig(blorg.DefaultConfigFile)
		if err != nil {
			return err
		}
		return config.Serve()
	default:
		return fmt.Errorf("Supported commands: init build serve")
	}
}

func render(path, format string) (string, error) {
	bs, err := ioutil.ReadFile(path)
	if err != nil {
		return "", err
	}
	d := org.New().Parse(bytes.NewReader(bs), path)
	switch strings.ToLower(format) {
	case "org":
		return d.Write(org.NewOrgWriter())
	case "html":
		return d.Write(org.NewHTMLWriter())
	case "html-chroma":
		writer := org.NewHTMLWriter()
		writer.HighlightCodeBlock = highlightCodeBlock
		return d.Write(writer)
	default:
		return "", fmt.Errorf("unsupported output format: %s", format)
	}
}

func highlightCodeBlock(source, lang string, inline bool) string {
	var w strings.Builder
	l := lexers.Get(lang)
	if l == nil {
		l = lexers.Fallback
	}
	l = chroma.Coalesce(l)
	it, _ := l.Tokenise(nil, source)
	_ = html.New().Format(&w, styles.Get("friendly"), it)
	if inline {
		return `<div class="highlight-inline">` + "\n" + w.String() + "\n" + `</div>`
	}
	return `<div class="highlight">` + "\n" + w.String() + "\n" + `</div>`
}
<commit_msg>Refactor main cmd: Improve error messages and code layout<commit_after>package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"strings"

	"github.com/alecthomas/chroma"
	"github.com/alecthomas/chroma/formatters/html"
	"github.com/alecthomas/chroma/lexers"
	"github.com/alecthomas/chroma/styles"
	"github.com/niklasfasching/go-org/blorg"
	"github.com/niklasfasching/go-org/org"
)

var usage = `Usage: go-org COMMAND [ARGS]...
Commands:
- render FILE FORMAT
  FORMAT: org, html, html-chroma
- blorg
  - blorg init
  - blorg build
  - blorg serve
`

func main() {
	log.SetFlags(0)
	if len(os.Args) < 2 {
		log.Fatal(usage)
	}
	switch cmd, args := os.Args[1], os.Args[2:]; cmd {
	case "render":
		render(args)
	case "blorg":
		runBlorg(args)
	default:
		log.Fatal(usage)
	}
}

func runBlorg(args []string) {
	if len(args) == 0 {
		log.Fatal(usage)
	}
	switch strings.ToLower(args[0]) {
	case "init":
		if _, err := os.Stat(blorg.DefaultConfigFile); !os.IsNotExist(err) {
			log.Fatalf("%s already exists", blorg.DefaultConfigFile)
		}
		if err := ioutil.WriteFile(blorg.DefaultConfigFile, []byte(blorg.DefaultConfig), os.ModePerm); err != nil {
			log.Fatal(err)
		}
		if err := os.MkdirAll("content", os.ModePerm); err != nil {
			log.Fatal(err)
		}

	case "build":
		config, err := blorg.ReadConfig(blorg.DefaultConfigFile)
		if err != nil {
			log.Fatal(err)
		}
		if err := config.Render(); err != nil {
			log.Fatal(err)
		}
		log.Println("blorg build finished")
	case "serve":
		config, err := blorg.ReadConfig(blorg.DefaultConfigFile)
		if err != nil {
			log.Fatal(err)
		}
		log.Fatal(config.Serve())
	default:
		log.Fatal(usage)
	}
}

func render(args []string) {
	if len(args) < 2 {
		log.Fatal(usage)
	}
	path, format := args[0], args[1]
	bs, err := ioutil.ReadFile(path)
	if err != nil {
		log.Fatal(err)
	}
	d := org.New().Parse(bytes.NewReader(bs), path)
	write := func(w org.Writer) {
		out, err := d.Write(w)
		if err != nil {
			log.Fatal(err)
		}
		fmt.Fprint(os.Stdout, out)
	}
	switch strings.ToLower(format) {
	case "org":
		write(org.NewOrgWriter())
	case "html":
		write(org.NewHTMLWriter())
	case "html-chroma":
		writer := org.NewHTMLWriter()
		writer.HighlightCodeBlock = highlightCodeBlock
		write(writer)
	default:
		log.Fatal(usage)
	}
}

func highlightCodeBlock(source, lang string, inline bool) string {
	var w strings.Builder
	l := lexers.Get(lang)
	if l == nil {
		l = lexers.Fallback
	}
	l = chroma.Coalesce(l)
	it, _ := l.Tokenise(nil, source)
	_ = html.New().Format(&w, styles.Get("friendly"), it)
	if inline {
		return `<div class="highlight-inline">` + "\n" + w.String() + "\n" + `</div>`
	}
	return `<div class="highlight">` + "\n" + w.String() + "\n" + `</div>`
}
<|endoftext|>


<commit_before>package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"io"
	"log"
	"net/http"
	"os"
	"os/signal"
	"time"

	openrtb "gopkg.in/bsm/openrtb.v2"
)

const (
	ACSIp       string = "127.0.0.1"
	ACSPort            = 9986
	BankerIp           = "127.0.0.1"
	BankerPort         = 9985
	BidderPort         = 7654
	BidderWin          = 7653
	BidderEvent        = 7652
)

func timeTrack(start time.Time, name string) {
	// elapsed := time.Since(start)
	// log.Printf("%s request took %s", name, elapsed)
}

func track(fn http.HandlerFunc, name string) http.HandlerFunc {
	return func(w http.ResponseWriter, req *http.Request) {
		defer timeTrack(time.Now(), name)
		fn(w, req)
	}
}

func printPortConfigs() {
	log.Printf("Bidder port: %d", BidderPort)
	log.Printf("Win port: %d", BidderWin)
	log.Printf("Event port: %d", BidderEvent)
}

func main() {
	var agentsConfigFile = flag.String("config", "", "Configuration file in JSON.")
	flag.Parse()
	if *agentsConfigFile == "" {
		log.Fatal("You should provide a configuration file.")
	}

	printPortConfigs()

	// http client to pace agents (note that it's pointer)
	client := &http.Client{}

	// load configuration
	agents, err := LoadAgentsFromFile(*agentsConfigFile)
	if err != nil {
		log.Fatal(err)
	}
	for _, agent := range agents {
		agent.RegisterAgent(client, ACSIp, ACSPort)
		agent.StartPacer(client, BankerIp, BankerPort)
	}

	StartStatOutput()

	mux := http.NewServeMux()

	mux.HandleFunc("/auctions", track(func(w http.ResponseWriter, r *http.Request) {
		defer r.Body.Close()
		var (
			ok    bool = true
			tmpOk bool = true
		)
		enc := json.NewEncoder(w)
		// body, _ := ioutil.ReadAll(r.Body)
		// fmt.Println(string(body))
		var req *openrtb.BidRequest
		err = json.NewDecoder(r.Body).Decode(&req)
		// req, err := openrtb.ParseRequest(r.Body)

		if err != nil {
			log.Println("ERROR", err.Error())
			w.WriteHeader(204) // respond with 'no bid'
			return
		}

		// log.Println("INFO Received bid request", req.ID)

		ids := externalIdsFromRequest(req)
		res := emptyResponseWithOneSeat(req)

		for _, agent := range agents {
			res, tmpOk = agent.DoBid(req, res, ids)
			ok = tmpOk || ok
		}

		BidIncoming()

		if ok {
			w.Header().Set("Content-type", "application/json")
			w.Header().Add("x-openrtb-version", "2.1")
			w.WriteHeader(http.StatusOK)
			enc.Encode(res)
			return
		}
		log.Println("No bid.")
		w.WriteHeader(204)

	}, "bidding"))

	go http.ListenAndServe(fmt.Sprintf(":%d", BidderPort), mux)

	evemux := http.NewServeMux()
	evemux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		defer r.Body.Close()
		w.WriteHeader(http.StatusOK)
		io.WriteString(w, "")
		// log.Println("Event!")
		BidEvent()
	})
	go http.ListenAndServe(fmt.Sprintf(":%d", BidderEvent), evemux)

	winmux := http.NewServeMux()
	winmux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		defer r.Body.Close()
		w.WriteHeader(http.StatusOK)
		io.WriteString(w, "")
		// log.Println("Win!")
		BidWin()
	})
	go http.ListenAndServe(fmt.Sprintf(":%d", BidderWin), winmux)

	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt, os.Kill)
	select {
	case <-c:
		// Implement remove agent from ACS
		for _, agent := range agents {
			agent.UnregisterAgent(client, ACSIp, ACSPort)
		}
		fmt.Println("Leaving...")
	}
}
<commit_msg>Switched over to fasthttp in go to test out performance.<commit_after>package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"io"
	"log"
	"net/http"
	"os"
	"os/signal"
	"time"

	"github.com/valyala/fasthttp"

	openrtb "gopkg.in/bsm/openrtb.v2"
)

const (
	ACSIp       string = "127.0.0.1"
	ACSPort            = 9986
	BankerIp           = "127.0.0.1"
	BankerPort         = 9985
	BidderPort         = 7654
	BidderWin          = 7653
	BidderEvent        = 7652
)

func timeTrack(start time.Time, name string) {
	// elapsed := time.Since(start)
	// log.Printf("%s request took %s", name, elapsed)
}

func track(fn http.HandlerFunc, name string) http.HandlerFunc {
	return func(w http.ResponseWriter, req *http.Request) {
		defer timeTrack(time.Now(), name)
		fn(w, req)
	}
}

func printPortConfigs() {
	log.Printf("Bidder port: %d", BidderPort)
	log.Printf("Win port: %d", BidderWin)
	log.Printf("Event port: %d", BidderEvent)
}

func fastHandleAuctions(ctx *fasthttp.RequestCtx, agents []Agent) {
	var (
		ok    bool = true
		tmpOk bool = true
	)

	// enc := json.NewEncoder(w)
	// body, _ := ioutil.ReadAll(r.Body)
	// fmt.Println(string(body))
	var req *openrtb.BidRequest
	err := json.Unmarshal(ctx.PostBody(), &req)
	// req, err := openrtb.ParseRequest(r.Body)

	if err != nil {
		log.Println("ERROR", err.Error())
		ctx.SetStatusCode(204)
		return
	}

	// log.Println("INFO Received bid request", req.ID)

	ids := externalIdsFromRequest(req)
	res := emptyResponseWithOneSeat(req)

	for _, agent := range agents {
		res, tmpOk = agent.DoBid(req, res, ids)
		ok = tmpOk || ok
	}

	BidIncoming()

	if ok {
		ctx.Response.Header.Set("Content-type", "application/json")
		ctx.Response.Header.Set("x-openrtb-version", "2.1")
		ctx.SetStatusCode(http.StatusOK)

		bytes, _ := json.Marshal(res)
		ctx.SetBody(bytes)

		return
	}
	log.Println("No bid.")
	ctx.SetStatusCode(204)
}

func main() {
	var agentsConfigFile = flag.String("config", "", "Configuration file in JSON.")
	flag.Parse()
	if *agentsConfigFile == "" {
		log.Fatal("You should provide a configuration file.")
	}

	printPortConfigs()

	// http client to pace agents (note that it's pointer)
	client := &http.Client{}

	// load configuration
	agents, err := LoadAgentsFromFile(*agentsConfigFile)

	if err != nil {
		log.Fatal(err)
	}
	// for _, agent := range agents {
	// 	// agent.RegisterAgent(client, ACSIp, ACSPort)
	// 	// agent.StartPacer(client, BankerIp, BankerPort)
	// }

	StartStatOutput()

	m := func(ctx *fasthttp.RequestCtx) {
		switch string(ctx.Path()) {
		case "/auctions":
			fastHandleAuctions(ctx, agents)
		default:
			ctx.Error("not found", fasthttp.StatusNotFound)
		}
	}

	go fasthttp.ListenAndServe(fmt.Sprintf(":%d", BidderPort), m)

	// mux := http.NewServeMux()
	//
	// mux.HandleFunc("/auctions", track(func(w http.ResponseWriter, r *http.Request) {
	// 	defer r.Body.Close()
	// 	var (
	// 		ok    bool = true
	// 		tmpOk bool = true
	// 	)
	// 	enc := json.NewEncoder(w)
	// 	// body, _ := ioutil.ReadAll(r.Body)
	// 	// fmt.Println(string(body))
	// 	var req *openrtb.BidRequest
	// 	err = json.NewDecoder(r.Body).Decode(&req)
	// 	// req, err := openrtb.ParseRequest(r.Body)
	//
	// 	if err != nil {
	// 		log.Println("ERROR", err.Error())
	// 		w.WriteHeader(204) // respond with 'no bid'
	// 		return
	// 	}
	//
	// 	// log.Println("INFO Received bid request", req.ID)
	//
	// 	ids := externalIdsFromRequest(req)
	// 	res := emptyResponseWithOneSeat(req)
	//
	// 	for _, agent := range agents {
	// 		res, tmpOk = agent.DoBid(req, res, ids)
	// 		ok = tmpOk || ok
	// 	}
	//
	// 	BidIncoming()
	//
	// 	if ok {
	// 		w.Header().Set("Content-type", "application/json")
	// 		w.Header().Add("x-openrtb-version", "2.1")
	// 		w.WriteHeader(http.StatusOK)
	// 		enc.Encode(res)
	// 		return
	// 	}
	// 	log.Println("No bid.")
	// 	w.WriteHeader(204)
	//
	// }, "bidding"))

	evemux := http.NewServeMux()
	evemux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		defer r.Body.Close()
		w.WriteHeader(http.StatusOK)
		io.WriteString(w, "")
		// log.Println("Event!")
		BidEvent()
	})
	go http.ListenAndServe(fmt.Sprintf(":%d", BidderEvent), evemux)

	winmux := http.NewServeMux()
	winmux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		defer r.Body.Close()
		w.WriteHeader(http.StatusOK)
		io.WriteString(w, "")
		// log.Println("Win!")
		BidWin()
	})
	go http.ListenAndServe(fmt.Sprintf(":%d", BidderWin), winmux)

	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt, os.Kill)
	select {
	case <-c:
		// Implement remove agent from ACS
		for _, agent := range agents {
			agent.UnregisterAgent(client, ACSIp, ACSPort)
		}
		fmt.Println("Leaving...")
	}
}
<|endoftext|>


<commit_before>package main

import (
	"code.google.com/p/gcfg"
	"compress/gzip"
	"flag"
	"fmt"
	"github.com/boredomist/mixport/exports"
	"github.com/boredomist/mixport/mixpanel"
	kinesis "github.com/sendgridlabs/go-kinesis"
	"io"
	"log"
	"os"
	"path"
	"runtime"
	"runtime/pprof"
	"strings"
	"sync"
	"syscall"
	"time"
)

// Mixpanel API credentials, used by the configuration parser.
type mixpanelCredentials struct {
	Key    string
	Secret string
	Token  string
}

// Configuration options common to the file export streams (CSV and JSON)
type fileExportConfig struct {
	State     bool
	Gzip      bool
	Fifo      bool
	Directory string
}

// configFormat is the in-memory representation of the mixport configuration
// file.
//
// - `Product` is Mixpanel API credential information for each product that will be
//   exported.
// - `Kinesis` is access keys and configuration for Amazon Kinesis exporter.
// - `JSON` and `CSV` are the configuration setups for the `JSON` and `CSV`
//   exporters, respectively.
type configFormat struct {
	Product map[string]*mixpanelCredentials
	Kinesis struct {
		State     bool
		Keyid     string
		Secretkey string
		Stream    string
		Region    string
	}

	JSON fileExportConfig
	CSV  fileExportConfig
}

var (
	configFile  string
	dateString  string
	rangeString string
	cpuProfile  *string
	maxProcs    int
)

func init() {
	const (
		confUsage  = "path to configuration file"
		dateUsage  = "date (YYYY/MM/DD) of data to pull, default is yesterday"
		rangeUsage = "date range (YYYY/MM/DD-YYYY/MM/DD) of data to pull."
		procUsage  = "maximum number of OS threads to spawn. These will be IO bound."
	)

	// TODO: Tune this.
	defaultProcs := runtime.NumCPU() * 4
	defaultConfig := "./mixport.conf"

	flag.StringVar(&configFile, "config", defaultConfig, confUsage)
	flag.StringVar(&configFile, "c", defaultConfig, confUsage)
	flag.StringVar(&dateString, "date", "", dateUsage)
	flag.StringVar(&dateString, "d", "", dateUsage)
	flag.StringVar(&rangeString, "range", "", rangeUsage)
	flag.StringVar(&rangeString, "r", "", rangeUsage)
	flag.IntVar(&maxProcs, "procs", defaultProcs, procUsage)
	flag.IntVar(&maxProcs, "p", defaultProcs, procUsage)

	cpuProfile = flag.String("prof", "", "dump pprof info to a file.")
}

var (
	cfg = configFormat{}
	// This variable will be set to true when an export goroutine fails.
	exportFailed = false
)

func main() {
	flag.Parse()

	runtime.GOMAXPROCS(maxProcs)

	if *cpuProfile != "" {
		f, err := os.Create(*cpuProfile)
		if err != nil {
			log.Fatal(err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	if err := gcfg.ReadFileInto(&cfg, configFile); err != nil {
		log.Fatalf("Failed to load %s: %s", configFile, err)
	}

	var exportStart, exportEnd time.Time

	// Default to yesterday (should be newest available data)
	if dateString == "" {
		year, month, day := time.Now().UTC().AddDate(0, 0, -1).Date()

		exportStart = time.Date(year, month, day, 0, 0, 0, 0, time.UTC)
		exportEnd = exportStart
	} else {
		if d, err := time.Parse("2006/01/02", dateString); err != nil {
			log.Fatalf("Invalid date: %s, should be in YYYY/MM/DD format",
				dateString)
		} else {
			exportStart = d
			exportEnd = exportStart
		}
	}

	if rangeString != "" {
		formatError := func() {
			log.Fatalf("Invalid range: %s, should be YYYY/MM/DD-YYYY/MM/DD.", rangeString)
		}

		parts := strings.Split(rangeString, "-")

		if len(parts) != 2 {
			formatError()
		}

		var err error

		if exportStart, err = time.Parse("2006/01/02", parts[0]); err != nil {
			formatError()
		}

		if exportEnd, err = time.Parse("2006/01/02", parts[1]); err != nil {
			formatError()
		}

	}

	// WaitGroup will hold the process open until all of the child
	// goroutines have completed execution.
	var wg sync.WaitGroup
	wg.Add(len(cfg.Product))

	for product, creds := range cfg.Product {
		// Run each individual product in a new thread.
		go exportProduct(exportStart, exportEnd, product, *creds, &wg)
	}

	// Wait for all our goroutines to finish up
	wg.Wait()

	if exportFailed {
		log.Fatalf("Export finished with errors.")
	}
}

func exportProduct(start, end time.Time, product string, creds mixpanelCredentials, wg *sync.WaitGroup) {
	defer wg.Done()

	client := mixpanel.New(product, creds.Key, creds.Secret)
	eventData := make(chan mixpanel.EventData)

	// We need to mux eventData into multiple channels to
	// ensure all export funcs have a chance to see each event
	// instance.
	var chans []chan mixpanel.EventData

	if cfg.Kinesis.State {
		ch := make(chan mixpanel.EventData)
		chans = append(chans, ch)

		ksis := kinesis.New(cfg.Kinesis.Keyid, cfg.Kinesis.Secretkey)
		if cfg.Kinesis.Region != "" {
			ksis.Region = "us-east-1"
		}

		wg.Add(1)
		go func() {
			exports.KinesisStreamer(ksis, cfg.Kinesis.Stream, ch)
			defer wg.Done()
		}()
	}

	type streamFunc func(io.Writer, <-chan mixpanel.EventData)

	// Generalize setup of JSON and CSV streams into a single function.
	setupFileExportStream := func(conf fileExportConfig, ext string, streamer streamFunc) {
		wg.Add(1)
		defer wg.Done()

		ch := make(chan mixpanel.EventData)
		chans = append(chans, ch)

		if conf.Gzip {
			ext += ".gz"
		}

		timeFmt := "20060102"
		stamp := start.Format(timeFmt)

		// Append end date to timestamp if we're using a date range.
		if start != end {
			stamp += fmt.Sprintf("-%s", end.Format(timeFmt))
		}

		name := path.Join(conf.Directory, fmt.Sprintf("%s-%s.%s", product, stamp, ext))

		if conf.Fifo {
			if err := syscall.Mkfifo(name, syscall.S_IRWXU); err != nil {
				log.Fatalf("Couldn't create named pipe: %s", err)
			}
		}

		fp, err := os.Create(name)
		if err != nil {
			log.Fatalf("Couldn't create file: %s", err)
		}

		if conf.Fifo {
			defer os.Remove(name)
		}
		defer fp.Close()

		writer := (io.Writer)(fp)
		if conf.Gzip {
			writer = gzip.NewWriter(fp)
			defer (writer).(*gzip.Writer).Close()
		} else {
			writer = fp
		}

		streamer(writer, ch)
	}

	if cfg.JSON.State {
		go setupFileExportStream(cfg.JSON, "json", exports.JSONStreamer)
	}

	if cfg.CSV.State {
		go setupFileExportStream(cfg.CSV, "csv", exports.CSVStreamer)
	}

	go func() {
		defer close(eventData)

		// If the export goroutine panics for some reason, don't tear
		// down the whole process with it.
		defer func() {
			if r := recover(); r != nil {
				log.Printf("%s failed: %v", product, r)
				exportFailed = true
			}
		}()

		// We want it to be start-end inclusive, so add one day to end date.
		end = end.AddDate(0, 0, 1)

		for date := start; date.Before(end); date = date.AddDate(0, 0, 1) {
			client.ExportDate(date, eventData, nil)
		}
	}()

	// Multiplex each received event to each of the active export
	// functions.
	for data := range eventData {
		for _, ch := range chans {
			ch <- data
		}
	}

	// Closing all the channels will signal the streaming export functions
	// that they've reached the end of the stream and should terminate as
	// soon as the channel is drained.
	for _, ch := range chans {
		close(ch)
	}
}
<commit_msg>Switch to pflag library for argument parsing<commit_after>package main

import (
	"code.google.com/p/gcfg"
	"compress/gzip"
	"fmt"
	"github.com/boredomist/mixport/exports"
	"github.com/boredomist/mixport/mixpanel"
	flag "github.com/ogier/pflag"
	kinesis "github.com/sendgridlabs/go-kinesis"
	"io"
	"log"
	"os"
	"path"
	"runtime"
	"runtime/pprof"
	"strings"
	"sync"
	"syscall"
	"time"
)

// Mixpanel API credentials, used by the configuration parser.
type mixpanelCredentials struct {
	Key    string
	Secret string
	Token  string
}

// Configuration options common to the file export streams (CSV and JSON)
type fileExportConfig struct {
	State     bool
	Gzip      bool
	Fifo      bool
	Directory string
}

// configFormat is the in-memory representation of the mixport configuration
// file.
//
// - `Product` is Mixpanel API credential information for each product that will be
//   exported.
// - `Kinesis` is access keys and configuration for Amazon Kinesis exporter.
// - `JSON` and `CSV` are the configuration setups for the `JSON` and `CSV`
//   exporters, respectively.
type configFormat struct {
	Product map[string]*mixpanelCredentials
	Kinesis struct {
		State     bool
		Keyid     string
		Secretkey string
		Stream    string
		Region    string
	}

	JSON fileExportConfig
	CSV  fileExportConfig
}

var configFile = flag.StringP("config", "c", "./mixport.conf", "path to configuration file")
var dateString = flag.StringP("date", "d", "", "date of data to pull in YYYY/MM/DD, default is yesterday")
var rangeString = flag.StringP("range", "r", "", "date range to pull in YYYY/MM/DD-YYYY/MM/DD")
var cpuProfile = flag.String("prof", "", "dump pprof info to a file.")
var maxProcs *int

func init() {
	// TODO: Tune this.
	defaultProcs := runtime.NumCPU() * 4

	maxProcs = flag.IntP("procs", "p", defaultProcs, "maximum number of OS threads to spawn.")
}

var (
	cfg = configFormat{}
	// This variable will be set to true when an export goroutine fails.
	exportFailed = false
)

func main() {
	flag.Parse()

	runtime.GOMAXPROCS(*maxProcs)

	if *cpuProfile != "" {
		f, err := os.Create(*cpuProfile)
		if err != nil {
			log.Fatal(err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	if err := gcfg.ReadFileInto(&cfg, *configFile); err != nil {
		log.Fatalf("Failed to load %s: %s", *configFile, err)
	}

	var exportStart, exportEnd time.Time

	// Default to yesterday (should be newest available data)
	if *dateString == "" {
		year, month, day := time.Now().UTC().AddDate(0, 0, -1).Date()

		exportStart = time.Date(year, month, day, 0, 0, 0, 0, time.UTC)
		exportEnd = exportStart
	} else {
		if d, err := time.Parse("2006/01/02", *dateString); err != nil {
			log.Fatalf("Invalid date: %s, should be in YYYY/MM/DD format",
				*dateString)
		} else {
			exportStart = d
			exportEnd = exportStart
		}
	}

	if *rangeString != "" {
		formatError := func() {
			log.Fatalf("Invalid range: %s, should be YYYY/MM/DD-YYYY/MM/DD.", *rangeString)
		}

		parts := strings.Split(*rangeString, "-")

		if len(parts) != 2 {
			formatError()
		}

		var err error

		if exportStart, err = time.Parse("2006/01/02", parts[0]); err != nil {
			formatError()
		}

		if exportEnd, err = time.Parse("2006/01/02", parts[1]); err != nil {
			formatError()
		}

	}

	// WaitGroup will hold the process open until all of the child
	// goroutines have completed execution.
	var wg sync.WaitGroup
	wg.Add(len(cfg.Product))

	for product, creds := range cfg.Product {
		// Run each individual product in a new thread.
		go exportProduct(exportStart, exportEnd, product, *creds, &wg)
	}

	// Wait for all our goroutines to finish up
	wg.Wait()

	if exportFailed {
		log.Fatalf("Export finished with errors.")
	}
}

func exportProduct(start, end time.Time, product string, creds mixpanelCredentials, wg *sync.WaitGroup) {
	defer wg.Done()

	client := mixpanel.New(product, creds.Key, creds.Secret)
	eventData := make(chan mixpanel.EventData)

	// We need to mux eventData into multiple channels to
	// ensure all export funcs have a chance to see each event
	// instance.
	var chans []chan mixpanel.EventData

	if cfg.Kinesis.State {
		ch := make(chan mixpanel.EventData)
		chans = append(chans, ch)

		ksis := kinesis.New(cfg.Kinesis.Keyid, cfg.Kinesis.Secretkey)
		if cfg.Kinesis.Region != "" {
			ksis.Region = "us-east-1"
		}

		wg.Add(1)
		go func() {
			exports.KinesisStreamer(ksis, cfg.Kinesis.Stream, ch)
			defer wg.Done()
		}()
	}

	type streamFunc func(io.Writer, <-chan mixpanel.EventData)

	// Generalize setup of JSON and CSV streams into a single function.
	setupFileExportStream := func(conf fileExportConfig, ext string, streamer streamFunc) {
		wg.Add(1)
		defer wg.Done()

		ch := make(chan mixpanel.EventData)
		chans = append(chans, ch)

		if conf.Gzip {
			ext += ".gz"
		}

		timeFmt := "20060102"
		stamp := start.Format(timeFmt)

		// Append end date to timestamp if we're using a date range.
		if start != end {
			stamp += fmt.Sprintf("-%s", end.Format(timeFmt))
		}

		name := path.Join(conf.Directory, fmt.Sprintf("%s-%s.%s", product, stamp, ext))

		if conf.Fifo {
			if err := syscall.Mkfifo(name, syscall.S_IRWXU); err != nil {
				log.Fatalf("Couldn't create named pipe: %s", err)
			}
		}

		fp, err := os.Create(name)
		if err != nil {
			log.Fatalf("Couldn't create file: %s", err)
		}

		if conf.Fifo {
			defer os.Remove(name)
		}
		defer fp.Close()

		writer := (io.Writer)(fp)
		if conf.Gzip {
			writer = gzip.NewWriter(fp)
			defer (writer).(*gzip.Writer).Close()
		} else {
			writer = fp
		}

		streamer(writer, ch)
	}

	if cfg.JSON.State {
		go setupFileExportStream(cfg.JSON, "json", exports.JSONStreamer)
	}

	if cfg.CSV.State {
		go setupFileExportStream(cfg.CSV, "csv", exports.CSVStreamer)
	}

	go func() {
		defer close(eventData)

		// If the export goroutine panics for some reason, don't tear
		// down the whole process with it.
		defer func() {
			if r := recover(); r != nil {
				log.Printf("%s failed: %v", product, r)
				exportFailed = true
			}
		}()

		// We want it to be start-end inclusive, so add one day to end date.
		end = end.AddDate(0, 0, 1)

		for date := start; date.Before(end); date = date.AddDate(0, 0, 1) {
			client.ExportDate(date, eventData, nil)
		}
	}()

	// Multiplex each received event to each of the active export
	// functions.
	for data := range eventData {
		for _, ch := range chans {
			ch <- data
		}
	}

	// Closing all the channels will signal the streaming export functions
	// that they've reached the end of the stream and should terminate as
	// soon as the channel is drained.
	for _, ch := range chans {
		close(ch)
	}
}
<|endoftext|>


<commit_before>package main

import "io/ioutil"
import "log"
import "os/exec"
import "flag"
import "strings"
import "sync"
import "strconv"
import "fmt"
import "bytes"
import "os"

var wg sync.WaitGroup

func img2video(imgName string, imgDuration string, outputName string) {
	defer wg.Done()
	var stderr bytes.Buffer
	cmd := exec.Command("ffmpeg", "-loop", "1", "-i", imgName, "-t", imgDuration, "-pix_fmt", "yuv420p", outputName)
	cmd.Stderr = &stderr
	err := cmd.Start()
	if err != nil {
		log.Fatal(err)
	} else {
		log.Printf("Creating " + outputName)
	}
	err = cmd.Wait()
	if err != nil {
		log.Printf(fmt.Sprint(err) + ": " + stderr.String())
		log.Fatal(err)
	} else {
		log.Printf("Created " + outputName)
	}
}

func concatVideos(numberOfVideos int, listFilename string, outputName string, done chan bool) {
	var stderr bytes.Buffer
	cmd := exec.Command("ffmpeg", "-f", "concat", "-i", listFilename, "-c", "copy", outputName)
	cmd.Stderr = &stderr
	err := cmd.Start()
	if err != nil {
		log.Fatal(err)
	} else {
		log.Printf("Creating " + outputName + " using " + listFilename)
	}
	err = cmd.Wait()
	if err != nil {
		log.Printf(fmt.Sprint(err) + ": " + stderr.String())
		log.Fatal(err)
	} else {
		log.Printf("Created " + outputName)
	}
	done <- true
}

func addAudio(silentFilename string, audioFilename string, outputName string, done chan bool) {
	var stderr bytes.Buffer
	cmd := exec.Command("ffmpeg", "-i", silentFilename, "-i", audioFilename, "-map", "0:0", "-map", "1:0", "-codec", "copy", "-shortest", outputName)
	cmd.Stderr = &stderr
	err := cmd.Start()
	if err != nil {
		log.Fatal(err)
	} else {
		log.Printf("Creating " + outputName + " from " + silentFilename + " and " + audioFilename)
	}
	err = cmd.Wait()
	if err != nil {
		log.Printf(fmt.Sprint(err) + ": " + stderr.String())
		log.Fatal(err)
	} else {
		log.Printf("Created " + outputName)
	}
	done <- true
}

func main() {
	timestampsFilenamePtr := flag.String("t", "timestamps.txt", "a file with timestamps and image names")
	audioFilenamePtr := flag.String("a", "audio.mp3", "audio file name")
	outputFilenamePtr := flag.String("o", "finalOut.mp4", "output file name")
	flag.Parse()

	videoListFilename := "videoList.txt"
	silentFilename := "silent.mp4"

	dat, err := ioutil.ReadFile(*timestampsFilenamePtr)
	if err != nil {
		log.Fatal(err)
	}

	lines := strings.Split(string(dat), "\n")
	outLines := make([]string, len(lines))

	for i, line := range lines {
		if line != "" {
			wg.Add(1)

			lineSplit := strings.Split(lines[i], " ")
			imgName := lineSplit[1]
			timestamp, err := strconv.Atoi(lineSplit[0])
			if err != nil {
				log.Fatal(err)
			}

			var imgDuration float64
			if (i == len(lines)-1) || (lines[i+1] == "") {
				imgDuration = float64(timestamp) / 1000
			} else {
				nextLineSplit := strings.Split(lines[i+1], " ")
				nextTimestamp, err := strconv.Atoi(nextLineSplit[0])
				if err != nil {
					log.Fatal(err)
				}
				imgDuration = float64(nextTimestamp - timestamp) / 1000
			}

			imgDurationString := strconv.FormatFloat(imgDuration, 'f', 3, 64)
			outputName := "out" + strconv.Itoa(i+1) + ".mp4"
			outLines[i] = "file '" + outputName + "'"

			go img2video(imgName, imgDurationString, outputName)
		}
	}

	outData := []byte(strings.Join(outLines, "\n"))
	err = ioutil.WriteFile(videoListFilename, outData, 0644)
	if err != nil {
		log.Fatal(err)
	}

	wg.Wait()
	done := make(chan bool, 1)

	go concatVideos(len(lines), videoListFilename, silentFilename, done)

	<-done

	go addAudio(silentFilename, *audioFilenamePtr, *outputFilenamePtr, done)

	for i, line := range lines {
		if line != "" {
			outputFilename := "out" + strconv.Itoa(i+1) + ".mp4"
			err = os.Remove(outputFilename)
			if err != nil {
				log.Fatal(err)
			} else {
				log.Printf("Deleted " + outputFilename)
			}
		}
	}

	<-done
}
<commit_msg>re encode audio<commit_after>package main

import "io/ioutil"
import "log"
import "os/exec"
import "flag"
import "strings"
import "sync"
import "strconv"
import "fmt"
import "bytes"
import "os"

var wg sync.WaitGroup

func img2video(imgName string, imgDuration string, outputName string) {
	defer wg.Done()
	var stderr bytes.Buffer
	cmd := exec.Command("ffmpeg", "-loop", "1", "-i", imgName, "-t", imgDuration, "-pix_fmt", "yuv420p", outputName)
	cmd.Stderr = &stderr
	err := cmd.Start()
	if err != nil {
		log.Fatal(err)
	} else {
		log.Printf("Creating " + outputName)
	}
	err = cmd.Wait()
	if err != nil {
		log.Printf(fmt.Sprint(err) + ": " + stderr.String())
		log.Fatal(err)
	} else {
		log.Printf("Created " + outputName)
	}
}

func concatVideos(numberOfVideos int, listFilename string, outputName string, done chan bool) {
	var stderr bytes.Buffer
	cmd := exec.Command("ffmpeg", "-f", "concat", "-i", listFilename, "-c", "copy", outputName)
	cmd.Stderr = &stderr
	err := cmd.Start()
	if err != nil {
		log.Fatal(err)
	} else {
		log.Printf("Creating " + outputName + " using " + listFilename)
	}
	err = cmd.Wait()
	if err != nil {
		log.Printf(fmt.Sprint(err) + ": " + stderr.String())
		log.Fatal(err)
	} else {
		log.Printf("Created " + outputName)
	}
	done <- true
}

func addAudio(silentFilename string, audioFilename string, outputName string, done chan bool) {
	var stderr bytes.Buffer
	cmd := exec.Command("ffmpeg", "-i", silentFilename, "-i", audioFilename, "-map", "0:0", "-map", "1:0", "-c:v", "copy", "-c:a", "libfdk_aac", "-b:a", "128k", "-shortest", outputName)
	cmd.Stderr = &stderr
	err := cmd.Start()
	if err != nil {
		log.Fatal(err)
	} else {
		log.Printf("Creating " + outputName + " from " + silentFilename + " and " + audioFilename)
	}
	err = cmd.Wait()
	if err != nil {
		log.Printf(fmt.Sprint(err) + ": " + stderr.String())
		log.Fatal(err)
	} else {
		log.Printf("Created " + outputName)
	}
	done <- true
}

func main() {
	timestampsFilenamePtr := flag.String("t", "timestamps.txt", "a file with timestamps and image names")
	audioFilenamePtr := flag.String("a", "audio.mp3", "audio file name")
	outputFilenamePtr := flag.String("o", "finalOut.mp4", "output file name")
	flag.Parse()

	videoListFilename := "videoList.txt"
	silentFilename := "silent.mp4"

	dat, err := ioutil.ReadFile(*timestampsFilenamePtr)
	if err != nil {
		log.Fatal(err)
	}

	lines := strings.Split(string(dat), "\n")
	outLines := make([]string, len(lines))

	for i, line := range lines {
		if line != "" {
			wg.Add(1)

			lineSplit := strings.Split(lines[i], " ")
			imgName := lineSplit[1]
			timestamp, err := strconv.Atoi(lineSplit[0])
			if err != nil {
				log.Fatal(err)
			}

			var imgDuration float64
			if (i == len(lines)-1) || (lines[i+1] == "") {
				imgDuration = float64(timestamp) / 1000
			} else {
				nextLineSplit := strings.Split(lines[i+1], " ")
				nextTimestamp, err := strconv.Atoi(nextLineSplit[0])
				if err != nil {
					log.Fatal(err)
				}
				imgDuration = float64(nextTimestamp - timestamp) / 1000
			}

			imgDurationString := strconv.FormatFloat(imgDuration, 'f', 3, 64)
			outputName := "out" + strconv.Itoa(i+1) + ".mp4"
			outLines[i] = "file '" + outputName + "'"

			go img2video(imgName, imgDurationString, outputName)
		}
	}

	outData := []byte(strings.Join(outLines, "\n"))
	err = ioutil.WriteFile(videoListFilename, outData, 0644)
	if err != nil {
		log.Fatal(err)
	}

	wg.Wait()
	done := make(chan bool, 1)

	go concatVideos(len(lines), videoListFilename, silentFilename, done)

	<-done

	go addAudio(silentFilename, *audioFilenamePtr, *outputFilenamePtr, done)

	for i, line := range lines {
		if line != "" {
			outputFilename := "out" + strconv.Itoa(i+1) + ".mp4"
			err = os.Remove(outputFilename)
			if err != nil {
				log.Fatal(err)
			} else {
				log.Printf("Deleted " + outputFilename)
			}
		}
	}

	<-done
}
<|endoftext|>


<commit_before>package main

func main() {
}
<commit_msg>Add signal handling<commit_after>package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"os/signal"
)

func main() {
	log.SetFlags(0)
	log.SetPrefix(fmt.Sprintf("%s: ", os.Args[0]))
	os.Exit(run())
}

func run() int {
	sigCh := make(chan os.Signal, 1)
	signal.Notify(sigCh, os.Interrupt)
	_, cancel := context.WithCancel(context.Background())
	go func() {
		<-sigCh
		cancel()
	}()

	return 0
}
<|endoftext|>


<commit_before>package main

import (
	"bytes"
	"crypto/tls"
	"encoding/json"
	"fmt"
	"github.com/drone/drone-go/drone"
	"github.com/drone/drone-go/plugin"
	"io/ioutil"
	"log"
	"net/http"
	"strings"
	"time"
)

var debug bool
var deployments []string

func main() {

	system := drone.System{}
	repo := drone.Repo{}
	build := drone.Build{}
	vargs := Params{}
	workspace := drone.Workspace{}

	plugin.Param("system", &system)
	plugin.Param("workspace", &workspace)
	plugin.Param("repo", &repo)
	plugin.Param("build", &build)
	plugin.Param("vargs", &vargs)
	plugin.MustParse()

	debug = true
	if vargs.Debug == "true" {
		debug = true
	}

	if debug {
		log.Println("Workspace Root: " + workspace.Root)
		log.Println("Workspace Path: " + workspace.Path)

		log.Println("Tag: " + vargs.Tag)
	}

	// Iterate over rcs and svcs
	for _, rc := range vargs.ReplicationControllers {
		artifact, err := readArtifactFromFile(workspace.Path, rc, vargs.ApiServer, vargs.Namespace, vargs.Tag)
		if err != nil {
			log.Fatal(err)
		}
		if debug {
			log.Println("Artifact loaded: " + artifact.Url)
		}
		if b, _ := existsArtifact(artifact, vargs.Token); b {
			deleteArtifact(artifact, vargs.Token)
			time.Sleep(time.Second * 5)
		}
		createArtifact(artifact, vargs.Token)
	}
	for _, rc := range vargs.Services {
		artifact, err := readArtifactFromFile(workspace.Path, rc, vargs.ApiServer, vargs.Namespace, vargs.Tag)
		if err != nil {
			log.Fatal(err)
		}
		createArtifact(artifact, vargs.Token)
	}
}

func doRequest(param ReqEnvelope) (bool, error) {
	if debug {
		log.Println("doRequest ")
	}
	tr := &http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
	}
	client := &http.Client{Transport: tr}
	var req *http.Request
	var err error
	// post payload to each artifact
	if param.Json == nil {
		req, err = http.NewRequest(param.Verb, param.Url, nil)
	} else {
		req, err = http.NewRequest(param.Verb, param.Url, bytes.NewBuffer(param.Json))
	}

	if param.Verb == "PATCH" {
		req.Header.Set("Content-Type", "application/strategic-merge-patch+json ")
	} else {
		req.Header.Set("Content-Type", "application/json ")
	}

	if debug {
		log.Println("HTTP Request %s", param.Verb)
		log.Println("HTTP Request %s", param.Url)
		log.Println("HTTP Request %s", string(param.Json))
	}

	req.Header.Set("Authorization", "Bearer "+param.Token)
	response, err := client.Do(req)
	if debug {
		contents, err := ioutil.ReadAll(response.Body)
		if err != nil {
			log.Fatal(err)
		}
		log.Printf("%s\n", string(contents))
	}

	if err != nil {
		log.Fatal(err)
	} else {
		defer response.Body.Close()

		if response.StatusCode == 200 {
			return true, err
		}
	}
	return false, err
}

func readArtifactFromFile(workspace string, artifactFile string, apiserver string, namespace string, tag string) (Artifact, error) {
	artifactFilename := workspace + "/" + artifactFile
	if debug {
		log.Println("readArtifactFromFile " + artifactFilename)
	}
	file, err := ioutil.ReadFile(artifactFilename)
	if err != nil {
		log.Fatal(err)
	}
	artifact := Artifact{}
	if strings.HasSuffix(artifactFilename, ".yaml") {
		file = yaml2Json(file, tag)
	}

	json.Unmarshal(file, &artifact)
	artifact.Data = file

	if artifact.Kind == "ReplicationController" {
		artifact.Url = fmt.Sprintf("%s/api/v1/namespaces/%s/replicationcontrollers", apiserver, namespace)
	}
	if artifact.Kind == "Service" {
		artifact.Url = fmt.Sprintf("%s/api/v1/namespaces/%s/services", apiserver, namespace)
	}

	return artifact, err
}

func makeTimestamp() int64 {
	return time.Now().UnixNano() / int64(time.Millisecond)
}
<commit_msg>debug to bool<commit_after>package main

import (
	"bytes"
	"crypto/tls"
	"encoding/json"
	"fmt"
	"github.com/drone/drone-go/drone"
	"github.com/drone/drone-go/plugin"
	"io/ioutil"
	"log"
	"net/http"
	"strings"
	"time"
)

var debug bool
var deployments []string

func main() {

	system := drone.System{}
	repo := drone.Repo{}
	build := drone.Build{}
	vargs := Params{}
	workspace := drone.Workspace{}

	plugin.Param("system", &system)
	plugin.Param("workspace", &workspace)
	plugin.Param("repo", &repo)
	plugin.Param("build", &build)
	plugin.Param("vargs", &vargs)
	plugin.MustParse()

	if vargs.Debug {
		debug = true
	}

	if debug {
		log.Println("Workspace Root: " + workspace.Root)
		log.Println("Workspace Path: " + workspace.Path)

		log.Println("Tag: " + vargs.Tag)
	}

	// Iterate over rcs and svcs
	for _, rc := range vargs.ReplicationControllers {
		artifact, err := readArtifactFromFile(workspace.Path, rc, vargs.ApiServer, vargs.Namespace, vargs.Tag)
		if err != nil {
			log.Fatal(err)
		}
		if debug {
			log.Println("Artifact loaded: " + artifact.Url)
		}
		if b, _ := existsArtifact(artifact, vargs.Token); b {
			deleteArtifact(artifact, vargs.Token)
			time.Sleep(time.Second * 5)
		}
		createArtifact(artifact, vargs.Token)
	}
	for _, rc := range vargs.Services {
		artifact, err := readArtifactFromFile(workspace.Path, rc, vargs.ApiServer, vargs.Namespace, vargs.Tag)
		if err != nil {
			log.Fatal(err)
		}
		createArtifact(artifact, vargs.Token)
	}
}

func doRequest(param ReqEnvelope) (bool, error) {
	if debug {
		log.Println("doRequest ")
	}
	tr := &http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
	}
	client := &http.Client{Transport: tr}
	var req *http.Request
	var err error
	// post payload to each artifact
	if param.Json == nil {
		req, err = http.NewRequest(param.Verb, param.Url, nil)
	} else {
		req, err = http.NewRequest(param.Verb, param.Url, bytes.NewBuffer(param.Json))
	}

	if param.Verb == "PATCH" {
		req.Header.Set("Content-Type", "application/strategic-merge-patch+json ")
	} else {
		req.Header.Set("Content-Type", "application/json ")
	}

	if debug {
		log.Println("HTTP Request %s", param.Verb)
		log.Println("HTTP Request %s", param.Url)
		log.Println("HTTP Request %s", string(param.Json))
	}

	req.Header.Set("Authorization", "Bearer "+param.Token)
	response, err := client.Do(req)
	if debug {
		contents, err := ioutil.ReadAll(response.Body)
		if err != nil {
			log.Fatal(err)
		}
		log.Printf("%s\n", string(contents))
	}

	if err != nil {
		log.Fatal(err)
	} else {
		defer response.Body.Close()

		if response.StatusCode == 200 {
			return true, err
		}
	}
	return false, err
}

func readArtifactFromFile(workspace string, artifactFile string, apiserver string, namespace string, tag string) (Artifact, error) {
	artifactFilename := workspace + "/" + artifactFile
	if debug {
		log.Println("readArtifactFromFile " + artifactFilename)
	}
	file, err := ioutil.ReadFile(artifactFilename)
	if err != nil {
		log.Fatal(err)
	}
	artifact := Artifact{}
	if strings.HasSuffix(artifactFilename, ".yaml") {
		file = yaml2Json(file, tag)
	}

	json.Unmarshal(file, &artifact)
	artifact.Data = file

	if artifact.Kind == "ReplicationController" {
		artifact.Url = fmt.Sprintf("%s/api/v1/namespaces/%s/replicationcontrollers", apiserver, namespace)
	}
	if artifact.Kind == "Service" {
		artifact.Url = fmt.Sprintf("%s/api/v1/namespaces/%s/services", apiserver, namespace)
	}

	return artifact, err
}

func makeTimestamp() int64 {
	return time.Now().UnixNano() / int64(time.Millisecond)
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"fmt"
	"os"

	"github.com/codegangsta/cli"
	"github.com/golang/glog"

	"bitbucket.org/moovie/renderer/cmd"
)

func main() {
	defer glog.Flush()
	flag.CommandLine.Parse([]string{"-logtostderr"})

	app := cli.NewApp()
	app.Name = "renderer"
	app.Usage = "components compiler, renderer and command line tool with web interface"
	app.Version = ""
	app.HideVersion = true
	app.Commands = cmd.Commands
	app.Flags = []cli.Flag{
		cli.IntFlag{
			Name:   "v",
			Usage:  "verbosity (disables logs if < 0)",
			EnvVar: "VERBOSITY",
		},
	}
	app.Before = glogFlags
	app.Run(os.Args)
}

func glogFlags(c *cli.Context) error {
	v := c.Int("v")
	if v < 0 {
		flag.Set("logtostderr", "false")
	} else if v > 0 {
		flag.Set("v", fmt.Sprintf("%d", v))
	}
	glog.Info("verbosity set")
	return nil
}
<commit_msg>main: clean<commit_after>package main

import (
	"flag"
	"fmt"
	"os"

	"github.com/codegangsta/cli"
	"github.com/golang/glog"

	"bitbucket.org/moovie/renderer/cmd"
)

func main() {
	defer glog.Flush()
	flag.CommandLine.Parse([]string{"-logtostderr"})

	app := cli.NewApp()
	app.Name = "renderer"
	app.Usage = "components compiler, renderer and command line tool with web interface"
	app.Version = ""
	app.HideVersion = true
	app.Commands = cmd.Commands
	app.Flags = []cli.Flag{
		cli.IntFlag{
			Name:   "v",
			Usage:  "verbosity (disables logs if < 0)",
			EnvVar: "VERBOSITY",
		},
	}
	app.Before = glogFlags
	app.Run(os.Args)
}

func glogFlags(c *cli.Context) error {
	v := c.Int("v")
	if v < 0 {
		flag.Set("logtostderr", "false")
	} else if v > 0 {
		flag.Set("v", fmt.Sprintf("%d", v))
	}
	return nil
}
<|endoftext|>


<commit_before>package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"os"

	"github.com/BurntSushi/toml"
	"github.com/clbanning/mxj"
	"github.com/docopt/docopt-go"
	"github.com/ghodss/yaml"
	"gopkg.in/ini.v1"
)

var (
	version = "dev"
)

func readData(filename string) (bytes []byte, err error) {
	if filename == "-" {
		bytes, err = ioutil.ReadAll(os.Stdin)
	} else {
		bytes, err = ioutil.ReadFile(filename)
	}
	return bytes, err
}

func loadIni(input_data []byte) interface{} {
	cfg, err := ini.Load(input_data)
	if err != nil {
		panic(err)
	}
	output := map[string]interface{}{}
	for k, v := range cfg.Section("").KeysHash() {
		output[k] = v
	}
	for _, section := range cfg.Sections() {
		if section.Name() == ini.DefaultSection {
			continue
		}
		output[section.Name()] = section.KeysHash()
	}
	return output
}

func parseData(input_data []byte, format string) (parsed_data interface{}, err error) {
	if format == "json" {
		err = json.Unmarshal(input_data, &parsed_data)
	} else if format == "yaml" || format == "yml" {
		err = yaml.Unmarshal(input_data, &parsed_data)
	} else if format == "toml" {
		err = toml.Unmarshal(input_data, &parsed_data)
	} else if format == "xml" {
		// TODO: Is this the right way to handle xml?
		m, err := mxj.NewMapXml(input_data)
		return m, err
	} else if format == "ini" {
		parsed_data = loadIni(input_data)
	} else {
		panic("Non support inport format")
	}
	return parsed_data, err
}

func outputData(input_data interface{}, format string) (output_data []byte, err error) {
	if format == "json" {
		output_data, err = json.MarshalIndent(input_data, "", "  ")
	} else if format == "yaml" || format == "yml" {
		output_data, err = yaml.Marshal(input_data)
	} else if format == "toml" {
		var buf bytes.Buffer
		toml_enc := toml.NewEncoder(&buf)
		err = toml_enc.Encode(input_data)
		output_data = buf.Bytes()
	} else if format == "xml" {
		output_data, err = mxj.AnyXmlIndent(input_data, "", "\t")
	} else {
		panic("Non supported output format")
	}

	return output_data, err
}

func detectInputFormat(data []byte) (detected_format string) {
	// TODO: Some sort of input detection
	return "yaml"
}

func parseArgs() map[string]interface{} {
	usage := `uq - Universal (de)serializer

Usage:
  uq [-v] [-s FORMAT] [-t FORMAT] [FILE]

Options:
  -s <FORMAT>, --source <FORMAT>  Specify input format. [default: auto]
  -t <FORMAT>, --target <FORMAT>  Specify output format. [default: json]
  -v, --verbose                   Be more verbose [default: false]
  -h, --help     Show this screen
  --version     Show version

Formats:
  * json
  * yaml|yml
  * toml
  * ini
  * xml (Note: xml won't be a perfect conversion)
`

	arguments, _ := docopt.Parse(usage, nil, true, version, false)
	return arguments
}

func main() {
	args := parseArgs()
	verbose := args["--verbose"].(bool)

	if verbose {
		log.Printf("Debug: args - %q", args)
	}

	var filename string
	if args["FILE"] == nil {
		filename = "-"
	} else {
		filename = args["FILE"].(string)
	}
	data, _ := readData(filename)

	var input_format string
	if args["--source"].(string) == "auto" {
		input_format = detectInputFormat(data)
	} else {
		input_format = args["--source"].(string)
	}
	parsed_data, err := parseData(data, input_format)
	if err != nil {
		log.Fatal(err)
	}

	output_data, _ := outputData(parsed_data, args["--target"].(string))

	fmt.Println(string(output_data))
}
<commit_msg>Not really working ini write support<commit_after>package main

import (
	"bufio"
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"os"

	"github.com/BurntSushi/toml"
	"github.com/clbanning/mxj"
	"github.com/docopt/docopt-go"
	"github.com/ghodss/yaml"
	"gopkg.in/ini.v1"
)

var (
	version = "dev"
)

func readData(filename string) (bytes []byte, err error) {
	if filename == "-" {
		bytes, err = ioutil.ReadAll(os.Stdin)
	} else {
		bytes, err = ioutil.ReadFile(filename)
	}
	return bytes, err
}

func loadIni(input_data []byte) interface{} {
	cfg, err := ini.Load(input_data)
	if err != nil {
		panic(err)
	}
	output := map[string]interface{}{}
	for k, v := range cfg.Section("").KeysHash() {
		output[k] = v
	}
	for _, section := range cfg.Sections() {
		if section.Name() == ini.DefaultSection {
			continue
		}
		output[section.Name()] = section.KeysHash()
	}
	return output
}

func saveIni(input interface{}) ([]byte, error) {
	i := ini.Empty()
	err := i.Append(map[string]string{"foo": "bar"})
	if err != nil {
		panic(err)
	}
	w := new(bufio.Writer)
	r := new(bufio.Reader)
	b := bufio.NewReadWriter(r, w)
	_, err = i.WriteTo(b)
	if err != nil {
		panic(err)
	}
	return ioutil.ReadAll(b)
}

func parseData(input_data []byte, format string) (parsed_data interface{}, err error) {
	if format == "json" {
		err = json.Unmarshal(input_data, &parsed_data)
	} else if format == "yaml" || format == "yml" {
		err = yaml.Unmarshal(input_data, &parsed_data)
	} else if format == "toml" {
		err = toml.Unmarshal(input_data, &parsed_data)
	} else if format == "xml" {
		// TODO: Is this the right way to handle xml?
		m, err := mxj.NewMapXml(input_data)
		return m, err
	} else if format == "ini" {
		parsed_data = loadIni(input_data)
	} else {
		panic("Non support inport format")
	}
	return parsed_data, err
}

func outputData(input_data interface{}, format string) (output_data []byte, err error) {
	if format == "json" {
		output_data, err = json.MarshalIndent(input_data, "", "  ")
	} else if format == "yaml" || format == "yml" {
		output_data, err = yaml.Marshal(input_data)
	} else if format == "toml" {
		var buf bytes.Buffer
		toml_enc := toml.NewEncoder(&buf)
		err = toml_enc.Encode(input_data)
		output_data = buf.Bytes()
	} else if format == "xml" {
		output_data, err = mxj.AnyXmlIndent(input_data, "", "\t")
	} else if format == "ini" {
		output_data, err = saveIni(input_data)
	} else {
		panic("Non supported output format")
	}

	return output_data, err
}

func detectInputFormat(data []byte) (detected_format string) {
	// TODO: Some sort of input detection
	return "yaml"
}

func parseArgs() map[string]interface{} {
	usage := `uq - Universal (de)serializer

Usage:
  uq [-v] [-s FORMAT] [-t FORMAT] [FILE]

Options:
  -s <FORMAT>, --source <FORMAT>  Specify input format. [default: auto]
  -t <FORMAT>, --target <FORMAT>  Specify output format. [default: json]
  -v, --verbose                   Be more verbose [default: false]
  -h, --help     Show this screen
  --version     Show version

Formats:
  * json
  * yaml|yml
  * toml
  * ini
  * xml (Note: xml won't be a perfect conversion)
`

	arguments, _ := docopt.Parse(usage, nil, true, version, false)
	return arguments
}

func main() {
	args := parseArgs()
	verbose := args["--verbose"].(bool)

	if verbose {
		log.Printf("Debug: args - %q", args)
	}

	var filename string
	if args["FILE"] == nil {
		filename = "-"
	} else {
		filename = args["FILE"].(string)
	}
	data, _ := readData(filename)

	var input_format string
	if args["--source"].(string) == "auto" {
		input_format = detectInputFormat(data)
	} else {
		input_format = args["--source"].(string)
	}
	parsed_data, err := parseData(data, input_format)
	if err != nil {
		log.Fatal(err)
	}

	output_data, _ := outputData(parsed_data, args["--target"].(string))

	fmt.Println(string(output_data))
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"os"

	"github.com/neowaylabs/clinit-cfn-tool/cfnextract"
	"github.com/neowaylabs/clinit-cfn-tool/cfninject"
)

var BANNER string = `
         ,_---~~~~~----._
  _,,_,*^____      _____` + "``" + `*g*\"*,
 / __/ /'     ^.  /      \ ^@q   f
[  @f | @))    |  | @))   l  0 _/
 \` + "`" + `/   \~____ / __ \_____/    \
  |           _l__l_           I
  }          [______]           I
  ]            | | |            |
  ]             ~ ~             |
  |                             | cloudinit-aws-tools v0.1
  |                             | Author: Tiago Natel de Moura
---------------------------------------------------------------
`
var USAGE string = `Usage: %s <command> arg1, argN
For each command you can use the --help flag to obtain details about
the usage.
    %s <command> --help

Available commands:
  inject    - Inject a cloudinit file into a AWS CloudFormation at UserData
              section
  extract   - Extract the UserData section of the CloudFormation into a output file
  help      - Displays this help information.

`

func main() {
	var command string

	if len(os.Args) <= 1 {
		fmt.Println(BANNER)
		fmt.Printf(USAGE, os.Args[0], os.Args[0])
		os.Exit(1)
	}

	command = os.Args[1]

	switch command {
	case "inject":
		cfninject.Inject()
	case "extract":
		cfnextract.Extract()
	default:
		fmt.Println("Unknown command: ", command)
	}
}
<commit_msg>[CHANGE] Added help command<commit_after>package main

import (
	"fmt"
	"os"

	"github.com/neowaylabs/clinit-cfn-tool/cfnextract"
	"github.com/neowaylabs/clinit-cfn-tool/cfninject"
)

var BANNER string = `
         ,_---~~~~~----._
  _,,_,*^____      _____` + "``" + `*g*\"*,
 / __/ /'     ^.  /      \ ^@q   f
[  @f | @))    |  | @))   l  0 _/
 \` + "`" + `/   \~____ / __ \_____/    \
  |           _l__l_           I
  }          [______]           I
  ]            | | |            |
  ]             ~ ~             |
  |                             | cloudinit-aws-tools v0.1
  |                             | Author: Tiago Natel de Moura
---------------------------------------------------------------
`
var USAGE string = `Usage: %s <command> arg1, argN
For each command you can use the --help flag to obtain details about
the usage.
    %s <command> --help

Available commands:
  inject    - Inject a cloudinit file into a AWS CloudFormation at UserData
              section
  extract   - Extract the UserData section of the CloudFormation into a output file
  help      - Displays this help information.

`

func help() {
	fmt.Println(BANNER)
	fmt.Printf(USAGE, os.Args[0], os.Args[0])
	os.Exit(1)
}

func main() {
	var command string

	if len(os.Args) <= 1 {
		help()
	}

	command = os.Args[1]

	switch command {
	case "inject":
		cfninject.Inject()
	case "extract":
		cfnextract.Extract()
	case "help":
		help()
	default:
		fmt.Println("Unknown command: ", command)
	}
}
<|endoftext|>