<commit_before>package main

import (
	"flag"
	"fmt"
	"log"
	"net"
	"net/http"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"time"

	"github.com/gorilla/mux"
	"github.com/lvdlvd/go-net-http-tmpl"
	"github.com/lvdlvd/go-rest"

	git "github.com/libgit2/git2go"
)

var (
	verbose  = flag.Bool("debug", false, "be extra verbose")
	refpfx   = flag.String("ref", "refs/notes/scrutinize", "Notes ref prefix to store review messages on.")
	webroot  = flag.String("webroot", filepath.Join(findHome(), "s"), "Path to dir with static webpages.")
	tmplroot = flag.String("tmplroot", filepath.Join(findHome(), "t"), "Path to dir with template webpages.")
)

var binHome string

func findHome() string {
	if binHome == "" {
		binHome = filepath.Dir(os.Args[0])
		// if we're running from a GOPATH/bin, home is src/github/lvdlvd/git-scrutinizer
		for _, v := range filepath.SplitList(os.Getenv("GOPATH")) {
			if binHome == filepath.Join(v, "bin") {
				binHome = filepath.Join(v, "src", "github.com", "lvdlvd", "git-scrutinizer")
				break
			}
		}
	}
	return binHome
}

var repository *git.Repository

func usage() {
	fmt.Fprintln(os.Stderr, "Usage: git-scrutinize [options] [repo]")
	flag.PrintDefaults()
	os.Exit(2)
}

func main() {
	log.SetFlags(0)
	flag.Usage = usage
	flag.Parse()

	var (
		repo string
		err  error
	)

	switch len(flag.Args()) {
	case 0:
		repo, err = os.Getwd()
		if err != nil {
			log.Fatal(err)
		}
	case 1:
		repo = flag.Arg(0)
	default:
		flag.Usage()
	}

	repository, err = git.OpenRepositoryExtended(repo, 0, "/")
	if err != nil {
		log.Fatal(err)
	}

	log.Println("Git repository", repository.Path())

	r := mux.NewRouter()
	r.KeepContext = true // cleared in loghandler

	th := tmpl.NewHandler(filepath.Join(*tmplroot, "*.html"), nil, tmplFuncs)

	r.Path("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		http.Redirect(w, r, "/commits", http.StatusMovedPermanently)
	})

	r.Path("/commits").Handler(substPath("commits.html", th))
	r.PathPrefix("/tree/").Handler(substPath("tree.html", th))
	r.Path("/blob/{oid}").Handler(substPath("blob.html", th)) // todo add pattern
	r.Path("/diffs").Handler(substPath("diffs.html", th))
	r.Path("/settings").Handler(substPath("settings.html", th))

	api := r.PathPrefix("/api/v1").Subrouter()
	all := rest.Everyone(rest.All)
	api.Path("/commits/{commit}/notes").Handler(&rest.Handler{Auth: all, Post: http.HandlerFunc(postNote)})

	exit := make(chan bool)
	r.Path("/quit").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "Bye...")
		close(exit)
	})

	// debug/vars and debug/pprof are served by the default mux
	r.PathPrefix("/debug/").Handler(http.DefaultServeMux)

	// all paths that haven't been matched will be served as static files out of the webroot.
	r.Methods("GET", "HEAD").Handler(http.FileServer(http.Dir(*webroot)))

	// :0 lets the OS choose a port
	ln, err := net.Listen("tcp", "localhost:0")
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("Opening http://%s", ln.Addr())

	go (&http.Server{
		Addr:    fmt.Sprint(ln.Addr()),
		Handler: logHandler(onlyOne(r, strings.Split(ln.Addr().String(), ":")[1]), *verbose),
	}).Serve(tcpKeepAliveListener{ln.(*net.TCPListener)})

	b, err := exec.Command(openCmd, fmt.Sprintf("http://%s", ln.Addr())).CombinedOutput()
	if len(b) > 0 {
		log.Println(string(b))
	}
	if err != nil {
		log.Fatal(err)
	}

	<-exit
	log.Println("Exiting.")

}

// Invoke h after setting request path to path.
// Saves the original path in mux var "path".
// This is useful because the template handler looks at the url
// path to invoke the template but we want to register under a path without the html.
// TODO automate?
func substPath(p string, h http.Handler) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		mux.Vars(r)["path"] = r.URL.Path
		r.URL.Path = p
		h.ServeHTTP(w, r)
	}
}

// helper copied from golang.org/pkg/http
type tcpKeepAliveListener struct{ *net.TCPListener }

func (ln tcpKeepAliveListener) Accept() (net.Conn, error) {
	c, err := ln.AcceptTCP()
	if err != nil {
		return nil, err
	}
	c.SetKeepAlive(true)
	c.SetKeepAlivePeriod(3 * time.Minute)
	return c, nil
}
<commit_msg>find myself better<commit_after>package main

import (
	"flag"
	"fmt"
	"log"
	"net"
	"net/http"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"time"

	"github.com/gorilla/mux"
	"github.com/lvdlvd/go-net-http-tmpl"
	"github.com/lvdlvd/go-rest"

	git "github.com/libgit2/git2go"
)

var (
	verbose  = flag.Bool("debug", false, "be extra verbose")
	refpfx   = flag.String("ref", "refs/notes/scrutinize", "Notes ref prefix to store review messages on.")
	webroot  = flag.String("webroot", filepath.Join(findHome(), "s"), "Path to dir with static webpages.")
	tmplroot = flag.String("tmplroot", filepath.Join(findHome(), "t"), "Path to dir with template webpages.")
)

var binHome string

func findHome() string {
	if binHome == "" {
		binHome = filepath.Dir(os.Args[0])
		if binHome == "." || binHome == "" {
			for _, v := range filepath.SplitList(os.Getenv("PATH")) {
				if _, err := os.Stat(filepath.Join(v, os.Args[0])); err == nil {
					binHome = v
					break
				}
			}

		}
		// if we're running from a GOPATH/bin, home is src/github/lvdlvd/git-scrutinizer
		for _, v := range filepath.SplitList(os.Getenv("GOPATH")) {
			if binHome == filepath.Join(v, "bin") {
				binHome = filepath.Join(v, "src", "github.com", "lvdlvd", "git-scrutinizer")
				break
			}
		}
	}
	return binHome
}

var repository *git.Repository

func usage() {
	fmt.Fprintln(os.Stderr, "Usage: git-scrutinize [options] [repo]")
	flag.PrintDefaults()
	os.Exit(2)
}

func main() {
	log.SetFlags(0)
	flag.Usage = usage
	flag.Parse()

	if _, err := os.Stat(*webroot); err != nil {
		log.Fatalf("%q can't find %s, probably mis-inferred %s as where i'm run from.", os.Args[0], *webroot, binHome)
	}

	var (
		repo string
		err  error
	)

	switch len(flag.Args()) {
	case 0:
		repo, err = os.Getwd()
		if err != nil {
			log.Fatal(err)
		}
	case 1:
		repo = flag.Arg(0)
	default:
		flag.Usage()
	}

	repository, err = git.OpenRepositoryExtended(repo, 0, "/")
	if err != nil {
		log.Fatal(err)
	}

	log.Println("Git repository", repository.Path())

	r := mux.NewRouter()
	r.KeepContext = true // cleared in loghandler

	th := tmpl.NewHandler(filepath.Join(*tmplroot, "*.html"), nil, tmplFuncs)

	r.Path("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		http.Redirect(w, r, "/commits", http.StatusMovedPermanently)
	})

	r.Path("/commits").Handler(substPath("commits.html", th))
	r.PathPrefix("/tree/").Handler(substPath("tree.html", th))
	r.Path("/blob/{oid}").Handler(substPath("blob.html", th)) // todo add pattern
	r.Path("/diffs").Handler(substPath("diffs.html", th))
	r.Path("/settings").Handler(substPath("settings.html", th))

	api := r.PathPrefix("/api/v1").Subrouter()
	all := rest.Everyone(rest.All)
	api.Path("/commits/{commit}/notes").Handler(&rest.Handler{Auth: all, Post: http.HandlerFunc(postNote)})

	exit := make(chan bool)
	r.Path("/quit").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "Bye...")
		close(exit)
	})

	// debug/vars and debug/pprof are served by the default mux
	r.PathPrefix("/debug/").Handler(http.DefaultServeMux)

	// all paths that haven't been matched will be served as static files out of the webroot.
	r.Methods("GET", "HEAD").Handler(http.FileServer(http.Dir(*webroot)))

	// :0 lets the OS choose a port
	ln, err := net.Listen("tcp", "localhost:0")
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("Opening http://%s", ln.Addr())

	go (&http.Server{
		Addr:    fmt.Sprint(ln.Addr()),
		Handler: logHandler(onlyOne(r, strings.Split(ln.Addr().String(), ":")[1]), *verbose),
	}).Serve(tcpKeepAliveListener{ln.(*net.TCPListener)})

	b, err := exec.Command(openCmd, fmt.Sprintf("http://%s", ln.Addr())).CombinedOutput()
	if len(b) > 0 {
		log.Println(string(b))
	}
	if err != nil {
		log.Fatal(err)
	}

	<-exit
	log.Println("Exiting.")

}

// Invoke h after setting request path to path.
// Saves the original path in mux var "path".
// This is useful because the template handler looks at the url
// path to invoke the template but we want to register under a path without the html.
// TODO automate?
func substPath(p string, h http.Handler) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		mux.Vars(r)["path"] = r.URL.Path
		r.URL.Path = p
		h.ServeHTTP(w, r)
	}
}

// helper copied from golang.org/pkg/http
type tcpKeepAliveListener struct{ *net.TCPListener }

func (ln tcpKeepAliveListener) Accept() (net.Conn, error) {
	c, err := ln.AcceptTCP()
	if err != nil {
		return nil, err
	}
	c.SetKeepAlive(true)
	c.SetKeepAlivePeriod(3 * time.Minute)
	return c, nil
}
<|endoftext|>


<commit_before>package main

import "rocketeer.be/sitegen/sitegen"

func main() {
	sitegen.Start()
}
<commit_msg>Fix import<commit_after>package main

import "github.com/rubenv/sitegen/sitegen"

func main() {
	sitegen.Start()
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"log"
	"net/http"
	"os"
	"runtime"
	"runtime/debug"
	"strings"

	"github.com/gorilla/rpc"
	rpcJson "github.com/gorilla/rpc/json"

	"github.com/dertseha/everoute/travel/capabilities/jumpdrive"
	"github.com/dertseha/everoute/travel/capabilities/jumpgate"
	"github.com/dertseha/everoute/travel/rules/security"
	"github.com/dertseha/everoute/travel/rules/transitcount"
	"github.com/dertseha/everoute/universe"

	"github.com/dertseha/everoute-web/data"
)

func buildSolarSystems(builder *universe.UniverseBuilder) {
	for _, system := range data.SolarSystems {
		trueSec := universe.TrueSecurity(system.Security)
		galaxyId := universe.NewEdenId

		if system.RegionId >= 11000000 {
			galaxyId = universe.WSpaceId
		}

		builder.AddSolarSystem(system.SolarSystemId, system.ConstellationId, system.RegionId, galaxyId,
			universe.NewSpecificLocation(system.X, system.Y, system.Z), trueSec)
	}
}

func getSolarSystemIdsByName() map[string]universe.Id {
	result := make(map[string]universe.Id)

	for _, system := range data.SolarSystems {
		result[system.Name] = system.SolarSystemId
	}

	return result
}

func getJumpGateDestinationName(gate data.JumpGateData) string {
	destNameStart := strings.Index(gate.Name, "(") + 1
	destNameEnd := strings.Index(gate.Name, ")")

	return gate.Name[destNameStart:destNameEnd]
}

func getJumpGateKey(fromSolarSystemId, toSolarSystemId universe.Id) string {
	return fmt.Sprintf("%d->%d", fromSolarSystemId, toSolarSystemId)
}

func getJumpGateLocations() map[string]universe.Location {
	result := make(map[string]universe.Location)
	solarSystemIdsByName := getSolarSystemIdsByName()

	for _, gate := range data.JumpGates {
		destName := getJumpGateDestinationName(gate)
		key := getJumpGateKey(gate.SolarSystemId, solarSystemIdsByName[destName])
		location := universe.NewSpecificLocation(gate.X, gate.Y, gate.Z)

		result[key] = location
	}

	return result
}

func buildJumpGates(builder *universe.UniverseBuilder) {
	jumpGateLocations := getJumpGateLocations()

	for _, jumpData := range data.SolarSystemJumps {
		extension := builder.ExtendSolarSystem(jumpData.FromSolarSystemId)
		jumpBuilder := extension.BuildJump(jumpgate.JumpType, jumpData.ToSolarSystemId)

		jumpBuilder.From(jumpGateLocations[getJumpGateKey(jumpData.FromSolarSystemId, jumpData.ToSolarSystemId)])
		jumpBuilder.To(jumpGateLocations[getJumpGateKey(jumpData.ToSolarSystemId, jumpData.FromSolarSystemId)])
	}
}

func dropUnusedData() {
	data.SolarSystems = nil
	data.SolarSystemJumps = nil
	data.JumpGates = nil
}

func prepareUniverse() *universe.UniverseBuilder {
	builder := universe.New().Extend()

	buildSolarSystems(builder)
	buildJumpGates(builder)
	transitcount.ExtendUniverse(builder)
	security.ExtendUniverse(builder)

	jumpdrive.ExtendUniverse(builder, 5.0)

	dropUnusedData()

	return builder
}

func initRuntime() {
	numCpu := runtime.NumCPU()
	maxThreads := 250 // Heroku limit: 256

	log.Printf("Initializing runtime to use %d CPUs and %d threads", numCpu, maxThreads)
	runtime.GOMAXPROCS(numCpu)
	debug.SetMaxThreads(maxThreads)
}

func main() {
	initRuntime()

	log.Printf("Building universe...")
	universeBuilder := prepareUniverse()
	universe := universeBuilder.Build()

	log.Printf("Initializing server...")
	rpcServer := rpc.NewServer()
	rpcServer.RegisterCodec(rpcJson.NewCodec(), "application/json")
	service := NewRouteService(universe)
	rpcServer.RegisterService(service, "Route")

	http.Handle("/", rpcServer)
	serverPort := os.Getenv("PORT")
	if serverPort == "" {
		serverPort = "3000"
	}
	log.Printf("Starting server on port <%s>...", serverPort)
	http.ListenAndServe(":"+serverPort, nil)
}
<commit_msg>Maximum jump distance set to 10ly again (JF)<commit_after>package main

import (
	"fmt"
	"log"
	"net/http"
	"os"
	"runtime"
	"runtime/debug"
	"strings"

	"github.com/gorilla/rpc"
	rpcJson "github.com/gorilla/rpc/json"

	"github.com/dertseha/everoute/travel/capabilities/jumpdrive"
	"github.com/dertseha/everoute/travel/capabilities/jumpgate"
	"github.com/dertseha/everoute/travel/rules/security"
	"github.com/dertseha/everoute/travel/rules/transitcount"
	"github.com/dertseha/everoute/universe"

	"github.com/dertseha/everoute-web/data"
)

func buildSolarSystems(builder *universe.UniverseBuilder) {
	for _, system := range data.SolarSystems {
		trueSec := universe.TrueSecurity(system.Security)
		galaxyId := universe.NewEdenId

		if system.RegionId >= 11000000 {
			galaxyId = universe.WSpaceId
		}

		builder.AddSolarSystem(system.SolarSystemId, system.ConstellationId, system.RegionId, galaxyId,
			universe.NewSpecificLocation(system.X, system.Y, system.Z), trueSec)
	}
}

func getSolarSystemIdsByName() map[string]universe.Id {
	result := make(map[string]universe.Id)

	for _, system := range data.SolarSystems {
		result[system.Name] = system.SolarSystemId
	}

	return result
}

func getJumpGateDestinationName(gate data.JumpGateData) string {
	destNameStart := strings.Index(gate.Name, "(") + 1
	destNameEnd := strings.Index(gate.Name, ")")

	return gate.Name[destNameStart:destNameEnd]
}

func getJumpGateKey(fromSolarSystemId, toSolarSystemId universe.Id) string {
	return fmt.Sprintf("%d->%d", fromSolarSystemId, toSolarSystemId)
}

func getJumpGateLocations() map[string]universe.Location {
	result := make(map[string]universe.Location)
	solarSystemIdsByName := getSolarSystemIdsByName()

	for _, gate := range data.JumpGates {
		destName := getJumpGateDestinationName(gate)
		key := getJumpGateKey(gate.SolarSystemId, solarSystemIdsByName[destName])
		location := universe.NewSpecificLocation(gate.X, gate.Y, gate.Z)

		result[key] = location
	}

	return result
}

func buildJumpGates(builder *universe.UniverseBuilder) {
	jumpGateLocations := getJumpGateLocations()

	for _, jumpData := range data.SolarSystemJumps {
		extension := builder.ExtendSolarSystem(jumpData.FromSolarSystemId)
		jumpBuilder := extension.BuildJump(jumpgate.JumpType, jumpData.ToSolarSystemId)

		jumpBuilder.From(jumpGateLocations[getJumpGateKey(jumpData.FromSolarSystemId, jumpData.ToSolarSystemId)])
		jumpBuilder.To(jumpGateLocations[getJumpGateKey(jumpData.ToSolarSystemId, jumpData.FromSolarSystemId)])
	}
}

func dropUnusedData() {
	data.SolarSystems = nil
	data.SolarSystemJumps = nil
	data.JumpGates = nil
}

func prepareUniverse() *universe.UniverseBuilder {
	builder := universe.New().Extend()

	buildSolarSystems(builder)
	buildJumpGates(builder)
	transitcount.ExtendUniverse(builder)
	security.ExtendUniverse(builder)

	jumpdrive.ExtendUniverse(builder, 10.0)

	dropUnusedData()

	return builder
}

func initRuntime() {
	numCpu := runtime.NumCPU()
	maxThreads := 250 // Heroku limit: 256

	log.Printf("Initializing runtime to use %d CPUs and %d threads", numCpu, maxThreads)
	runtime.GOMAXPROCS(numCpu)
	debug.SetMaxThreads(maxThreads)
}

func main() {
	initRuntime()

	log.Printf("Building universe...")
	universeBuilder := prepareUniverse()
	universe := universeBuilder.Build()

	log.Printf("Initializing server...")
	rpcServer := rpc.NewServer()
	rpcServer.RegisterCodec(rpcJson.NewCodec(), "application/json")
	service := NewRouteService(universe)
	rpcServer.RegisterService(service, "Route")

	http.Handle("/", rpcServer)
	serverPort := os.Getenv("PORT")
	if serverPort == "" {
		serverPort = "3000"
	}
	log.Printf("Starting server on port <%s>...", serverPort)
	http.ListenAndServe(":"+serverPort, nil)
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"fmt"
	"github.com/quii/mockingjay"
	"io/ioutil"
	"log"
	"net/http"
	"os"
)

func main() {
	var port = flag.Int("port", 9090, "Port to listen on")
	var configPath = flag.String("config", "", "Path to config YAML")
	var realURL = flag.String("realURL", "", "Optional: Set this to a URL to check your config against a real server for compatibility")
	var monkeyConfigPath = flag.String("monkeyConfigPath", "", "Optional: Set this to add some monkey business")

	flag.Parse()

	if *configPath == "" {
		flag.Usage()
		os.Exit(1)
	}

	config, err := ioutil.ReadFile(*configPath)

	if err != nil {
		log.Fatalf("Problem occured when trying to open the config file: %v", err)
	}

	endpoints, err := mockingjay.NewFakeEndpoints(config)

	if err != nil {
		log.Fatalf("Problem occured when trying to create a server from the config: %v ", err)
	}

	if *realURL != "" {
		checkEndpoints(endpoints, *realURL)
	} else {
		log.Printf("Serving %d endpoints defined from %s on port %d", len(endpoints), *configPath, *port)
		makeFakeServer(endpoints, *port, loadMonkeyConfig(*monkeyConfigPath))
	}

}

func checkEndpoints(endpoints []mockingjay.FakeEndpoint, realURL string) {
	checker := NewCompatabilityChecker(endpoints)

	if checker.CheckCompatability(realURL) {
		log.Println("All endpoints are compatible")
	} else {
		log.Fatal("At least one endpoint was incompatible with the real URL supplied")
	}
}

func makeFakeServer(endpoints []mockingjay.FakeEndpoint, port int, monkeyConfig []behaviour) {

	var server http.Handler
	if len(monkeyConfig) > 0 {
		server = NewMonkeyServer(mockingjay.NewServer(endpoints), monkeyConfig)
	} else {
		server = mockingjay.NewServer(endpoints)
	}

	http.Handle("/", server)
	err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
	if err != nil {
		log.Fatalf("There was a problem starting the mockingjay server on port %d: %v", port, err)
	}
}
<commit_msg>More consistent command line flags<commit_after>package main

import (
	"flag"
	"fmt"
	"github.com/quii/mockingjay"
	"io/ioutil"
	"log"
	"net/http"
	"os"
)

func main() {
	var port = flag.Int("port", 9090, "Port to listen on")
	var configPath = flag.String("config", "", "Path to config YAML")
	var realURL = flag.String("realURL", "", "Optional: Set this to a URL to check your config against a real server for compatibility")
	var monkeyConfigPath = flag.String("monkeyConfig", "", "Optional: Set this to add some monkey business")

	flag.Parse()

	if *configPath == "" {
		flag.Usage()
		os.Exit(1)
	}

	config, err := ioutil.ReadFile(*configPath)

	if err != nil {
		log.Fatalf("Problem occured when trying to open the config file: %v", err)
	}

	endpoints, err := mockingjay.NewFakeEndpoints(config)

	if err != nil {
		log.Fatalf("Problem occured when trying to create a server from the config: %v ", err)
	}

	if *realURL != "" {
		checkEndpoints(endpoints, *realURL)
	} else {
		log.Printf("Serving %d endpoints defined from %s on port %d", len(endpoints), *configPath, *port)
		makeFakeServer(endpoints, *port, loadMonkeyConfig(*monkeyConfigPath))
	}

}

func checkEndpoints(endpoints []mockingjay.FakeEndpoint, realURL string) {
	checker := NewCompatabilityChecker(endpoints)

	if checker.CheckCompatability(realURL) {
		log.Println("All endpoints are compatible")
	} else {
		log.Fatal("At least one endpoint was incompatible with the real URL supplied")
	}
}

func makeFakeServer(endpoints []mockingjay.FakeEndpoint, port int, monkeyConfig []behaviour) {

	var server http.Handler
	if len(monkeyConfig) > 0 {
		server = NewMonkeyServer(mockingjay.NewServer(endpoints), monkeyConfig)
	} else {
		server = mockingjay.NewServer(endpoints)
	}

	http.Handle("/", server)
	err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
	if err != nil {
		log.Fatalf("There was a problem starting the mockingjay server on port %d: %v", port, err)
	}
}
<|endoftext|>


<commit_before>//go:generate lein compile-clojurescript
package main

import (
	"log"
	"os/exec"
)

import . "github.com/hraberg/cljs2go/cljs/core"

var _main = Fn(func(args ...interface{}) interface{} {
	Println.X_invoke_ArityVariadic("ClojureScript to Go [go]")
	goGet := exec.Command("go", "get", "code.google.com/p/go.tools/cmd/goimports")
	if out, err := goGet.CombinedOutput(); err != nil {
		log.Fatal(string(out[:]))
	}
	return nil
})

func init() {
	X_STAR_main_cli_fn_STAR_ = _main
}

func main() {
	Main_()
}
<commit_msg>This file will eventually be generated itself.<commit_after>//go:generate lein compile-clojurescript
package main

import (
	"log"
	"os/exec"
)

import . "github.com/hraberg/cljs2go/cljs/core"

var _main = Fn(func(args ...interface{}) interface{} {
	Println.X_invoke_ArityVariadic(Array_seq.X_invoke_Arity1([]interface{}{"ClojureScript to Go [go]"}))
	goGet := exec.Command("go", "get", "code.google.com/p/go.tools/cmd/goimports")
	if out, err := goGet.CombinedOutput(); err != nil {
		log.Fatal(string(out[:]))
	}
	return nil
})

func init() {
	X_STAR_main_cli_fn_STAR_ = _main
}

func main() {
	Main_()
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"os"
	"encoding/json"
	"github.com/vaughan0/go-ini"
	"io"
	"io/ioutil"
	"net/http"
	"strconv"
	"flag"

//	"errors"
//	"log"
//	"net"
//	"crypto/sha512"
//	"encoding/hex"

)

//	defaults:
var (
	WORKERS	int64	= 10	//Number of workers
	IMGDIR	string	= "img"	//default download directory
	)

func main() {

	fmt.Println("Derpiboo.ru Downloader version 0.0.5 \nWorking")

	config, err := ini.LoadFile("config.ini") // Loading default config file and checking for various errors.
	if os.IsNotExist(err) {
		panic("config.ini does not exist, create it")
	}

	if err != nil {
		panic(err)
	}

	//Getting stuff from config, overwriting defaults

	key, ok := config.Get("main", "key")
	if !ok {
		panic("'key' variable missing from 'main' section")
	}

	W_temp, _ := config.Get("main", "workers")
		if W_temp != "" {
		WORKERS, err = strconv.ParseInt( W_temp, 10, 0)
		if err != nil { fmt.Println("Wrong configuration: Amount of workers is not a number"); os.Exit(1) }
	}

	ID_temp, _ := config.Get("main", "downdir")
		if ID_temp != "" { IMGDIR = ID_temp }

	//here shall be flag parser

	flag.Parse()

	length := flag.NArg()
	if length == 0 {
		fmt.Println("Nothing to download, bye!")
		os.Exit(0)
	}

	//	creating directory for downloads if not yet done
    if err := os.MkdirAll(IMGDIR, 0777); err != nil {
         panic(err)
    }

	imgid := flag.Arg(length-1) //Last argument given presumed to be number of image on site. Temporally, because later would do with flags.

	//	fmt.Println(key) //Just checking that I am not wrong

	_, err = strconv.ParseInt(imgid, 10, 0)
	if err != nil { fmt.Println("Wrong input: can not parse", imgid, "as a number"); os.Exit(1) }

	fmt.Println("Processing image No " + imgid)

	imgdat := make (chan Image, 1)

	parseImg (imgdat, imgid, key)

	dlimage(imgdat)

}

type Image struct {
	url      string
	filename string
	hash     string
}

func parseImg(imgchan chan<- Image, imgid string, key string) {

	source := "http://derpiboo.ru/" + imgid + ".json?nofav=&nocomments="
	if (  key != "" ) { source = source + "&key=" + key }

//	fmt.Println(source)

	resp, err := http.Get(source) //Getting our nice http response. Needs checking for 404 and other responses that are... less expected
	if err != nil {
		panic(err)
	}

	defer resp.Body.Close() //and not forgetting to close it when it's done

	var dat map[string]interface{}

	body, err := ioutil.ReadAll(resp.Body) //stolen from official documentation
	if err != nil {
		panic(err)
	}

	if err := json.Unmarshal(body, &dat); //transforming json into native map

	err != nil {
		panic(err)
	}
	var imgdata Image
	imgdata.url = "http:" + dat["image"].(string)
	imgdata.hash = dat["sha512_hash"].(string) //for the future and checking that we got file right
	imgdata.filename = strconv.FormatFloat(dat["id_number"].(float64), 'f', -1, 64) + "." + dat["file_name"].(string) + "." + dat["original_format"].(string)

//	fmt.Println(strconv.FormatFloat(dat["id_number"].(float64), 'f', -1, 64))

//	fmt.Println(dat)

//	for now and troubleshooting
//	fmt.Println(imgdata.url)
//	fmt.Println(imgdata.hash)
//	fmt.Println(imgdata.filename)

	imgchan <- imgdata

	return
}

func dlimage(imgchan <-chan Image) {
//	fmt.Println("reading channel")

	imgdata := <-imgchan

	fmt.Println("Saving as ", imgdata.filename)
	PathSep, _ := strconv.Unquote(strconv.QuoteRune(os.PathSeparator))

	output, err := os.Create(IMGDIR + PathSep + imgdata.filename)
	if err !=err { panic(err)}
	defer output.Close()

	response, err := http.Get(imgdata.url)
	if err != nil {
		panic(err)
		fmt.Println("Error while downloading", imgdata.url, "-", err)
		return
	}
	defer response.Body.Close()

//	hash := sha512.New()

	io.Copy(output, response.Body)

/*	io.Copy(hash, response.Body)
	b := make([]byte, hash.Size())
	hash.Sum(b[:0])

	fmt.Println("\n", hex.EncodeToString(b), "\n", imgdata.hash )

	if hex.EncodeToString(b) == imgdata.hash {
		fmt.Println("Hash correct")
	}	else {
		fmt.Println("Hash wrong")
	}

	fmt.Println("\n", hex.EncodeToString(hash.Sum(nil)), "\n", imgdata.hash )
*/

}

func parseTag ( imgchan chan<- Image, tag string, key string) {

}

<commit_msg>now I can input tags!<commit_after>package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"github.com/vaughan0/go-ini"
	"io"
	"io/ioutil"
	"net/http"
	"os"
	"strconv"

	//	"errors"
	//	"log"
	//	"net"
	//	"crypto/sha512"
	//	"encoding/hex"
)

//	defaults:
var (
	WORKERS 	int64	= 10    //Number of workers
	IMGDIR  	string	= "img" //default download directory
	TAG     	string	= ""    //default string is empty, it can only ge extracted from command line
	STARTPAGE	int		= 1		//default start page, derpiboo.ru 1-indexed
	STOPPAGE	int		= 0		//default stop page, would stop parsing json when it ends
	)

func main() {

	fmt.Println("Derpiboo.ru Downloader version 0.0.5 \nWorking")

	config, err := ini.LoadFile("config.ini") // Loading default config file and checking for various errors.
	if os.IsNotExist(err) {
		panic("config.ini does not exist, create it")
	}

	if err != nil {
		panic(err)
	}

	//Getting stuff from config, overwriting defaults

	key, ok := config.Get("main", "key")
	if !ok {
		panic("'key' variable missing from 'main' section")
	}

	W_temp, _ := config.Get("main", "workers")
	if W_temp != "" {
		WORKERS, err = strconv.ParseInt(W_temp, 10, 0)
		if err != nil {
			fmt.Println("Wrong configuration: Amount of workers is not a number")
			os.Exit(1)
		}
	}

	ID_temp, _ := config.Get("main", "downdir")
	if ID_temp != "" {
		IMGDIR = ID_temp
	}

	//here shall be flag parser

	flag.StringVar(&TAG, "t", TAG, "Tag to download: Replace spaces with \"+\".")
	flag.Parse()

	length := flag.NArg()
	if length == 0 {
		fmt.Println("Nothing to download, bye!")
		os.Exit(0)
	}

	//	creating directory for downloads if not yet done
	if err := os.MkdirAll(IMGDIR, 0777); err != nil {
		panic(err)
	}

	imgdat := make(chan Image, 10)

	if TAG == "" {

		imgid := flag.Arg(length - 1)

		//	fmt.Println(key) //Just checking that I am not wrong

		_, err = strconv.ParseInt(imgid, 10, 0)
		if err != nil {
			fmt.Println("Wrong input: can not parse", imgid, "as a number")
			os.Exit(1)
		}

		fmt.Println("Processing image No " + imgid)

		parseImg(imgdat, imgid, key)

	}	else	{
	//	and here we parse tag and abuse stuff horribly

		fmt.Println("trying to process tags")
		parseTag(imgdat, TAG, key)
	}

	dlimage(imgdat)

}

type Image struct {
	url      string
	filename string
	hash     string
}

func parseImg(imgchan chan<- Image, imgid string, key string) {

	source := "http://derpiboo.ru/" + imgid + ".json?nofav=&nocomments="
	if key != "" {
		source = source + "&key=" + key
	}

	//	fmt.Println(source)

	resp, err := http.Get(source) //Getting our nice http response. Needs checking for 404 and other responses that are... less expected
	if err != nil {
		panic(err)
	}

	defer resp.Body.Close() //and not forgetting to close it when it's done

	var dat map[string]interface{}

	body, err := ioutil.ReadAll(resp.Body) //stolen from official documentation
	if err != nil {
		panic(err)
	}

	if err := json.Unmarshal(body, &dat); //transforming json into native map

	err != nil {
		panic(err)
	}
	var imgdata Image
	imgdata.url = "http:" + dat["image"].(string)
	imgdata.hash = dat["sha512_hash"].(string) //for the future and checking that we got file right
	imgdata.filename = strconv.FormatFloat(dat["id_number"].(float64), 'f', -1, 64) + "." + dat["file_name"].(string) + "." + dat["original_format"].(string)

	//	fmt.Println(strconv.FormatFloat(dat["id_number"].(float64), 'f', -1, 64))

	//	fmt.Println(dat)

	//	for now and troubleshooting
	//	fmt.Println(imgdata.url)
	//	fmt.Println(imgdata.hash)
	//	fmt.Println(imgdata.filename)

	imgchan <- imgdata

	return
}

func dlimage(imgchan <-chan Image) {
	//	fmt.Println("reading channel")

	imgdata := <-imgchan

	fmt.Println("Saving as ", imgdata.filename)
	PathSep, _ := strconv.Unquote(strconv.QuoteRune(os.PathSeparator))

	output, err := os.Create(IMGDIR + PathSep + imgdata.filename)
	if err != err {
		panic(err)
	}
	defer output.Close()

	response, err := http.Get(imgdata.url)
	if err != nil {
		panic(err)
		fmt.Println("Error while downloading", imgdata.url, "-", err)
		return
	}
	defer response.Body.Close()

	//	hash := sha512.New()

	io.Copy(output, response.Body)

	/*	io.Copy(hash, response.Body)
		b := make([]byte, hash.Size())
		hash.Sum(b[:0])

		fmt.Println("\n", hex.EncodeToString(b), "\n", imgdata.hash )

		if hex.EncodeToString(b) == imgdata.hash {
			fmt.Println("Hash correct")
		}	else {
			fmt.Println("Hash wrong")
		}

		fmt.Println("\n", hex.EncodeToString(hash.Sum(nil)), "\n", imgdata.hash )
	*/

}

func parseTag(imgchan chan<- Image, tag string, key string) {

}
<|endoftext|>


<commit_before>// Program envjson provides a mechanism for managing an environment before
// launching a program. It uses a JSON spec to ensure that all of the
// expected variables are in the environment (reading from either STDIN, or
// the actual environment itself).
//
// See the README for more information.
//
// Released under the Modified BSD license. See LICENSE for more information.
package main

import (
	"fmt"
	"os"
	"os/exec"
	"os/signal"
	"strings"
	"syscall"
)

func forwardSignals(cmd *exec.Cmd) {
	sigc := make(chan os.Signal, 1)
	signal.Notify(sigc)
	go func() {
		for {
			s := <-sigc
			cmd.Process.Signal(s)
		}
	}()
}

func run(e env, c config) {
	if len(c.cmd) == 0 {
		os.Exit(0)
	}

	cmd := exec.Command(c.cmd[0], c.cmd[1:]...)
	cmd.Env = e.Environ()
	cmd.Stdin = os.Stdin
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr

	err := cmd.Start()
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s: fatal: failed to run command: %s\n", c.prog, err)
		os.Exit(1)
	}

	forwardSignals(cmd)

	err = cmd.Wait()
	if err == nil {
		os.Exit(0) // Successful.
	} else if exit, ok := err.(*exec.ExitError); ok {
		if s, ok := exit.Sys().(syscall.WaitStatus); ok {
			os.Exit(s.ExitStatus())
		}
	}

	fmt.Fprintf(os.Stderr, "%s: fatal: failed to run command: %s\n", os.Args[0], err)
	os.Exit(1)
}

func usage() {
	fmt.Fprintf(os.Stderr, "usage: %s [--stdin | --help] JSONfile <cmd> <cmd args>\n", os.Args[0])
}

type config struct {
	displayHelp bool
	displayEnv  bool
	fromStdin   bool
	cleanEnv    bool
	prog        string
	specFile    string
	cmd         []string
}

func configure(args []string) (c config, err error) {
	c.prog = args[0]

	args = args[1:]

	if len(args) == 0 { // by default just display the environment
		c.displayEnv = true
		return
	}

	i := 0

loop:
	for ; i < len(args); i++ {
		switch {
		case os.Args[i] == "--help":
			c.displayHelp = true
			return
		case args[i] == "--stdin" || args[i] == "-":
			c.fromStdin = true
		case args[i] == "-i" || args[i] == "--ignore-environment":
			c.cleanEnv = true
		case strings.HasPrefix(args[i], "--") || strings.HasPrefix(args[i], "-"):
			err = fmt.Errorf("invalid argument %q", args[i])
			return
		default:
			break loop
		}
	}

	if i < len(args) {
		c.specFile = args[i] // save off the spec file for loading later.

		i++
		if i < len(args) {
			c.cmd = args[i:]
		} else {
			c.displayEnv = true
		}
	} else {
		c.displayEnv = true
	}

	return
}

func main() {
	config, err := configure(os.Args)
	if config.displayHelp {
		usage()
		os.Exit(1)
	} else if err != nil {
		fmt.Fprintf(os.Stderr,
			"%s: error: %s\n",
			config.prog, err)
		usage()
		os.Exit(1)
	}

	// The config we *want* to launch command with
	local := make(env)

	// The config we originally *have*
	parent := make(env)

	if !config.cleanEnv {
		parent.FromEnviron(os.Environ())
	}

	if config.fromStdin {
		err := parent.Read(os.Stdin)
		if err != nil {
			fmt.Fprintf(os.Stderr,
				"%s: error: Unable to read parent env from STDIN: %s\n",
				config.prog, err)
			os.Exit(1)
		}
	}

	// Is there a spec file? If so, read it.
	if config.specFile != "" {
		err := local.FromFile(config.specFile)
		if err != nil {
			fmt.Fprintf(os.Stderr,
				"%s: error: Unable to read env from %q: %s\n",
				config.prog, config.specFile, err)
			os.Exit(1)
		}
	}

	// Validate that we have what we need.
	err = local.Merge(parent)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s: error: Environment check failed: %s\n",
			config.prog, err)
		os.Exit(1)
	}

	// We're just dumping to the screen.
	if config.displayEnv {
		err = local.Dump(os.Stdout)
		if err != nil {
			fmt.Fprintf(os.Stderr,
				"%s: error: Unable to dump env to JSON: %s\n",
				config.prog, err)
			os.Exit(1)
		}
		return
	}

	run(local, config)
}
<commit_msg>Update usage, and add new `-h`<commit_after>// Program envjson provides a mechanism for managing an environment before
// launching a program. It uses a JSON spec to ensure that all of the
// expected variables are in the environment (reading from either STDIN, or
// the actual environment itself).
//
// See the README for more information.
//
// Released under the Modified BSD license. See LICENSE for more information.
package main

import (
	"fmt"
	"os"
	"os/exec"
	"os/signal"
	"strings"
	"syscall"
)

func forwardSignals(cmd *exec.Cmd) {
	sigc := make(chan os.Signal, 1)
	signal.Notify(sigc)
	go func() {
		for {
			s := <-sigc
			cmd.Process.Signal(s)
		}
	}()
}

func run(e env, c config) {
	if len(c.cmd) == 0 {
		os.Exit(0)
	}

	cmd := exec.Command(c.cmd[0], c.cmd[1:]...)
	cmd.Env = e.Environ()
	cmd.Stdin = os.Stdin
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr

	err := cmd.Start()
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s: fatal: failed to run command: %s\n", c.prog, err)
		os.Exit(1)
	}

	forwardSignals(cmd)

	err = cmd.Wait()
	if err == nil {
		os.Exit(0) // Successful.
	} else if exit, ok := err.(*exec.ExitError); ok {
		if s, ok := exit.Sys().(syscall.WaitStatus); ok {
			os.Exit(s.ExitStatus())
		}
	}

	fmt.Fprintf(os.Stderr, "%s: fatal: failed to run command: %s\n", os.Args[0], err)
	os.Exit(1)
}

func usage() {
	fmt.Fprintf(os.Stderr, "usage: %s [OPTIONS]... JSONfile [COMMAND [ARG]...]\n", os.Args[0])
	fmt.Fprintf(os.Stderr, `
Ensure that the environment meets JSONfile requirements and run COMMAND.

  -h  --help     display this help and exit
  -i, --ignore-environment  start with an empty environment
  -   --stdin    insert read JSON key-value pairs into environment

If no COMMAND, print the resulting environment as JSON.
`)
}

type config struct {
	displayHelp bool
	displayEnv  bool
	fromStdin   bool
	cleanEnv    bool
	prog        string
	specFile    string
	cmd         []string
}

func configure(args []string) (c config, err error) {
	c.prog = args[0]

	args = args[1:]

	if len(args) == 0 { // by default just display the environment
		c.displayEnv = true
		return
	}

	i := 0

loop:
	for ; i < len(args); i++ {
		switch {
		case args[i] == "--help" || args[i] == "-h":
			c.displayHelp = true
			return
		case args[i] == "--stdin" || args[i] == "-":
			c.fromStdin = true
		case args[i] == "-i" || args[i] == "--ignore-environment":
			c.cleanEnv = true
		case strings.HasPrefix(args[i], "--") || strings.HasPrefix(args[i], "-"):
			err = fmt.Errorf("invalid argument %q", args[i])
			return
		default:
			break loop
		}
	}

	if i < len(args) {
		c.specFile = args[i] // save off the spec file for loading later.

		i++
		if i < len(args) {
			c.cmd = args[i:]
		} else {
			c.displayEnv = true
		}
	} else {
		c.displayEnv = true
	}

	return
}

func main() {
	config, err := configure(os.Args)
	if config.displayHelp {
		usage()
		os.Exit(1)
	} else if err != nil {
		fmt.Fprintf(os.Stderr,
			"%s: error: %s\n",
			config.prog, err)
		usage()
		os.Exit(1)
	}

	// The config we *want* to launch command with
	local := make(env)

	// The config we originally *have*
	parent := make(env)

	if !config.cleanEnv {
		parent.FromEnviron(os.Environ())
	}

	if config.fromStdin {
		err := parent.Read(os.Stdin)
		if err != nil {
			fmt.Fprintf(os.Stderr,
				"%s: error: Unable to read parent env from STDIN: %s\n",
				config.prog, err)
			os.Exit(1)
		}
	}

	// Is there a spec file? If so, read it.
	if config.specFile != "" {
		err := local.FromFile(config.specFile)
		if err != nil {
			fmt.Fprintf(os.Stderr,
				"%s: error: Unable to read env from %q: %s\n",
				config.prog, config.specFile, err)
			os.Exit(1)
		}
	}

	// Validate that we have what we need.
	err = local.Merge(parent)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s: error: Environment check failed: %s\n",
			config.prog, err)
		os.Exit(1)
	}

	// We're just dumping to the screen.
	if config.displayEnv {
		err = local.Dump(os.Stdout)
		if err != nil {
			fmt.Fprintf(os.Stderr,
				"%s: error: Unable to dump env to JSON: %s\n",
				config.prog, err)
			os.Exit(1)
		}
		return
	}

	run(local, config)
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"github.com/miekg/dns"
	"log"
	"os"
	"os/signal"
	"sync"
	"syscall"
	"time"
)

var (
	zones = &ZoneStore{
		store: make(map[string]Zone),
		m:     new(sync.RWMutex),
	}
	zoneUrl     string
	listenOn    string
	recurseTo   string
	apiKey      string
	buildtime   string
	buildcommit string
)

type ZoneStore struct {
	store map[string]Zone
	m     *sync.RWMutex
}

type Zone map[dns.RR_Header][]dns.RR

func (zs *ZoneStore) match(q string, t uint16) (*Zone, string) {
	zs.m.RLock()
	defer zs.m.RUnlock()
	var zone *Zone
	var name string
	b := make([]byte, len(q)) // worst case, one label of length q
	off := 0
	end := false
	for {
		l := len(q[off:])
		for i := 0; i < l; i++ {
			b[i] = q[off+i]
			if b[i] >= 'A' && b[i] <= 'Z' {
				b[i] |= ('a' - 'A')
			}
		}
		if z, ok := zs.store[string(b[:l])]; ok { // 'causes garbage, might want to change the map key
			if t != dns.TypeDS {
				return &z, string(b[:l])
			} else {
				// Continue for DS to see if we have a parent too, if so delegeate to the parent
				zone = &z
				name = string(b[:l])
			}
		}
		off, end = dns.NextLabel(q, off)
		if end {
			break
		}
	}
	return zone, name
}

func main() {
	flag.StringVar(&zoneUrl, "z", "http://localhost/zones.json", "The URL of zones in JSON format")
	flag.StringVar(&listenOn, "l", "", "The IP to listen on (default = blank = ALL)")
	flag.StringVar(&recurseTo, "r", "", "Pass-through requests that we can't answer to other DNS server (address:port or empty=disabled)")
	flag.StringVar(&apiKey, "k", "", "API key for http notifications")
	flag.Parse()

	log.Println("godnsagent (2014) by Daniel Speichert is starting...")
	log.Println("https://github.com/DevelopersPL/godnsagent")
	log.Printf("bult %s from commit %s", buildtime, buildcommit)

	prefetch(zones, true)

	server := &Server{
		host:     listenOn,
		port:     53,
		rTimeout: 5 * time.Second,
		wTimeout: 5 * time.Second,
		zones:    zones,
	}

	server.Run()

	go StartHTTP()

	sig := make(chan os.Signal)
	signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
	for {
		select {
		case s := <-sig:
			log.Fatalf("Signal (%d) received, stopping\n", s)
		}
	}
}
<commit_msg>Logger flags are customizable<commit_after>package main

import (
	"flag"
	"github.com/miekg/dns"
	"log"
	"os"
	"os/signal"
	"sync"
	"syscall"
	"time"
)

var (
	zones = &ZoneStore{
		store: make(map[string]Zone),
		m:     new(sync.RWMutex),
	}
	zoneUrl     string
	listenOn    string
	recurseTo   string
	apiKey      string
	buildtime   string
	buildcommit string
	loggerFlags int
)

type ZoneStore struct {
	store map[string]Zone
	m     *sync.RWMutex
}

type Zone map[dns.RR_Header][]dns.RR

func (zs *ZoneStore) match(q string, t uint16) (*Zone, string) {
	zs.m.RLock()
	defer zs.m.RUnlock()
	var zone *Zone
	var name string
	b := make([]byte, len(q)) // worst case, one label of length q
	off := 0
	end := false
	for {
		l := len(q[off:])
		for i := 0; i < l; i++ {
			b[i] = q[off+i]
			if b[i] >= 'A' && b[i] <= 'Z' {
				b[i] |= ('a' - 'A')
			}
		}
		if z, ok := zs.store[string(b[:l])]; ok { // 'causes garbage, might want to change the map key
			if t != dns.TypeDS {
				return &z, string(b[:l])
			} else {
				// Continue for DS to see if we have a parent too, if so delegate to the parent
				zone = &z
				name = string(b[:l])
			}
		}
		off, end = dns.NextLabel(q, off)
		if end {
			break
		}
	}
	return zone, name
}

func main() {
	flag.StringVar(&zoneUrl, "z", "http://localhost/zones.json", "The URL of zones in JSON format")
	flag.StringVar(&listenOn, "l", "", "The IP to listen on (default = blank = ALL)")
	flag.StringVar(&recurseTo, "r", "", "Pass-through requests that we can't answer to other DNS server (address:port or empty=disabled)")
	flag.StringVar(&apiKey, "k", "", "API key for http notifications")
	flag.IntVar(&loggerFlags, "lf", 0, "logger flags")
	flag.Parse()

	// remove timestamps from log outputs (journald adds them)
	log.SetFlags(loggerFlags)

	log.Println("godnsagent (2015) by Daniel Speichert is starting...")
	log.Println("https://github.com/DevelopersPL/godnsagent")
	log.Printf("built %s from commit %s", buildtime, buildcommit)

	prefetch(zones, true)

	server := &Server{
		host:     listenOn,
		port:     53,
		rTimeout: 5 * time.Second,
		wTimeout: 5 * time.Second,
		zones:    zones,
	}

	server.Run()

	go StartHTTP()

	sig := make(chan os.Signal)
	signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
	for {
		select {
		case s := <-sig:
			log.Fatalf("Signal (%d) received, stopping\n", s)
		}
	}
}
<|endoftext|>


<commit_before>package main

import (
	"log"
	"net/http"

	"github.com/kaakaa/matterpoll-emoji/poll"
)

func main() {
	c, err := poll.LoadConf("config.json")
	if err != nil {
		log.Fatal(err)
	}
	poll.Conf = c
	http.HandleFunc("/poll", poll.PollCmd)
	http.ListenAndServe(":8081", nil)
}
<commit_msg>add port option<commit_after>package main

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

	"github.com/kaakaa/matterpoll-emoji/poll"
)

var port = flag.Int("p", 8080, "port number")

func main() {
	flag.Parse()

	c, err := poll.LoadConf("config.json")
	if err != nil {
		log.Fatal(err)
	}
	poll.Conf = c
	http.HandleFunc("/poll", poll.PollCmd)
	if err := http.ListenAndServe(fmt.Sprintf(":%d", *port), nil); err != nil {
		log.Fatal(err)
	}
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"github.com/docopt/docopt-go"
	"os"
)

func main() {
	usage := `Git Substitute.

Usage:
  git-substitute <search-pattern> <replace-pattern> [<path> ...]
  git-substitute -h | --help
  git-substitute -V | --version

Options:
  -h --help     Show this screen.
  --version     Show version.`

	arguments, _ := docopt.Parse(usage, nil, true, "Git Substitute 1.0", false)
	command := &Substitute{
		searchPattern:  arguments["<search-pattern>"].(string),
		replacePattern: arguments["<replace-pattern>"].(string),
		paths:          arguments["<path>"].([]string),
	}
	out, err := command.Run()
	if err != nil {
		fmt.Print(string(out))
		os.Exit(1)
	}
}
<commit_msg>make -V option not cause a panic<commit_after>package main

import (
	"fmt"
	"github.com/docopt/docopt-go"
	"os"
)

func main() {
	usage := `Git Substitute.

Usage:
  git-substitute <search-pattern> <replace-pattern> [<path> ...]
  git-substitute -h | --help
  git-substitute -V | --version

Options:
  -h --help        Show this screen.
  -V --version     Show version.`

	arguments, _ := docopt.Parse(usage, nil, true, "Git Substitute 1.0", false)
	command := &Substitute{
		searchPattern:  arguments["<search-pattern>"].(string),
		replacePattern: arguments["<replace-pattern>"].(string),
		paths:          arguments["<path>"].([]string),
	}
	out, err := command.Run()
	if err != nil {
		fmt.Print(string(out))
		os.Exit(1)
	}
}
<|endoftext|>


<commit_before>package main

import (
	"os"
	"os/signal"

	"github.com/ninjasphere/go-ninja/api"
	"github.com/ninjasphere/go-ninja/logger"
	"github.com/ninjasphere/sphere-go-homecloud/homecloud"
)

var log = logger.GetLogger("HomeCloud")

func main() {

	log.Infof("Welcome home, Ninja.")

	conn, err := ninja.Connect("sphere-go-homecloud")
	if err != nil {
		log.Fatalf("Failed to connect to sphere: %s", err)
	}

	NewWebsocketServer(conn)

	homecloud.Start(conn)

	restServer := NewRestServer(conn) // TODO: Should we reuse the persistance layer from homecloud?

	go restServer.Listen()

	blah := make(chan os.Signal, 1)
	signal.Notify(blah, os.Interrupt, os.Kill)

	// Block until a signal is received.
	x := <-blah
	log.Infof("Got signal: %v", x)

}
<commit_msg>Don't start the web socket server then do nothing forever!<commit_after>package main

import (
	"os"
	"os/signal"

	"github.com/ninjasphere/go-ninja/api"
	"github.com/ninjasphere/go-ninja/logger"
	"github.com/ninjasphere/sphere-go-homecloud/homecloud"
)

var log = logger.GetLogger("HomeCloud")

func main() {

	log.Infof("Welcome home, Ninja.")

	conn, err := ninja.Connect("sphere-go-homecloud")
	if err != nil {
		log.Fatalf("Failed to connect to sphere: %s", err)
	}

	go NewWebsocketServer(conn)

	homecloud.Start(conn)

	restServer := NewRestServer(conn) // TODO: Should we reuse the persistance layer from homecloud?

	go restServer.Listen()

	blah := make(chan os.Signal, 1)
	signal.Notify(blah, os.Interrupt, os.Kill)

	// Block until a signal is received.
	x := <-blah
	log.Infof("Got signal: %v", x)

}
<|endoftext|>


<commit_before>package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"os"
	"path/filepath"
	"time"
)

func KelvinToFahr(kelvin float64) float64 {
	return (kelvin-273.15)*1.8 + 32
}

type TickList struct {
	List []*Tick
}

type Tick struct {
	Kelvin float64 `json:"temp"`
	Fahr   float64
	Dt     int64
	Date   time.Time
}

func NewTickList(stationID string) (list *TickList, err error) {
	url := "http://api.openweathermap.org/data/2.1/history/station/" +
		stationID + "?type=tick"
	resp, err := http.Get(url)
	if err != nil {
		return
	}
	buf, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return
	}
	err = json.Unmarshal(buf, &list)
	if err != nil {
		return
	}
	list.init()
	return
}

func (tick *Tick) init() {
	tick.Fahr = KelvinToFahr(tick.Kelvin)
	tick.Date = time.Unix(tick.Dt, 0)
}

func (list *TickList) init() {
	for _, tick := range list.List {
		tick.init()
	}
}

func (list *TickList) Len() int {
	return len(list.List)
}

func (list *TickList) Tick(i int) *Tick {
	return list.List[i]
}

func (tick *Tick) Print() {
	fmt.Printf("% 3.1f %v\n", tick.Fahr, tick.Date)
}

func (list *TickList) Print() {
	if len(list.List) < 0 {
		fmt.Println("no data from station!")
		return
	}
	fmt.Printf("%5v %v\n", "Temp", "Time")
	for _, tick := range list.List {
		tick.Print()
	}
}

func errMain() (err error) {
	id := "1348"

	list, err := NewTickList(id)
	if err != nil {
		return err
	}
	list.Print()

	return nil
}

func main() {
	err := errMain()
	if err == nil {
		os.Exit(0)
	}
	fmt.Fprintf(os.Stderr, "%v: Error: %v\n", filepath.Base(os.Args[0]), err)
	os.Exit(1)
}
<commit_msg>Add reporting to StatHat<commit_after>package main

import (
	"encoding/json"
	"errors"
	"fmt"
	stathat "github.com/stathat/go"
	"io/ioutil"
	"net/http"
	"os"
	"path/filepath"
	"time"
)

func KelvinToFahr(kelvin float64) float64 {
	return (kelvin-273.15)*1.8 + 32
}

type TickList struct {
	List []*Tick
}

type Tick struct {
	Kelvin float64 `json:"temp"`
	Fahr   float64
	Dt     int64
	Date   time.Time
}

func NewTickList(stationID string) (list *TickList, err error) {
	url := "http://api.openweathermap.org/data/2.1/history/station/" +
		stationID + "?type=tick"
	resp, err := http.Get(url)
	if err != nil {
		return
	}
	buf, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return
	}
	err = json.Unmarshal(buf, &list)
	if err != nil {
		return
	}
	list.init()
	return
}

func (tick *Tick) init() {
	tick.Fahr = KelvinToFahr(tick.Kelvin)
	tick.Date = time.Unix(tick.Dt, 0)
}

func (list *TickList) init() {
	for _, tick := range list.List {
		tick.init()
	}
}

func (list *TickList) Len() int {
	return len(list.List)
}

func (list *TickList) Tick(i int) *Tick {
	return list.List[i]
}

func (tick *Tick) Print() {
	fmt.Printf("% 3.1f %v\n", tick.Fahr, tick.Date)
}

func (list *TickList) Print() {
	if len(list.List) < 0 {
		fmt.Println("no data from station!")
		return
	}
	fmt.Printf("%5v %v\n", "Temp", "Time")
	for _, tick := range list.List {
		tick.Print()
	}
}

func errMain() (err error) {
	verbose := true
	email := ""
	id := "1348"
	name := "KBGR"
	interval := 60
	interval = 60 * 3

	prev := time.Now().Add(time.Minute * -time.Duration(interval))

	list, err := NewTickList(id)
	if err != nil {
		return err
	}

	if list.Len() < 1 {
		// BUG(sk): likely there's useful data in the json on error
		return errors.New("no weather data returned")
	}

	recent := list.Tick(list.Len() - 1)
	if recent.Date.Before(prev) {
		if verbose {
			fmt.Println("already reported latest data")
		}
		return nil
	}

	name = "weather-temp" + name
	if verbose {
		fmt.Println(name, email, recent.Fahr)
	}
	// the return value is a farce
	_ = stathat.PostEZValue(name, email, recent.Fahr)
	// if you make anything print in here, convert the whole thing to use
	// log instead of fmt.
	finished := stathat.WaitUntilFinished(time.Second * 5)
	if !finished {
		return errors.New("stathat timed out")
	}

	return nil
}

func main() {
	err := errMain()
	if err == nil {
		os.Exit(0)
	}
	fmt.Fprintf(os.Stderr, "%v: Error: %v\n", filepath.Base(os.Args[0]), err)
	os.Exit(1)
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"strconv"

	"github.com/ogier/pflag"
	"github.com/yuya-takeyama/argf"
)

var (
	name    = "csvp"
	version = "0.8.1"

	flagset         = pflag.NewFlagSet(name, pflag.ContinueOnError)
	indexesList     = flagset.StringP("indexes", "i", "", "")
	headersList     = flagset.StringP("headers", "h", "", "")
	isTSV           = flagset.BoolP("tsv", "t", false, "")
	delimiter       = flagset.StringP("delimiter", "d", ",", "")
	outputDelimiter = flagset.StringP("output-delimiter", "D", "\t", "")
	isHelp          = flagset.BoolP("help", "", false, "")
	isVersion       = flagset.BoolP("version", "", false, "")
)

func printUsage() {
	fmt.Fprintf(os.Stderr, `
Usage: %s [OPTION]... [FILE]...
Print selected parts of CSV from each FILE to standard output.

Options:
  -i, --indexes=LIST
                 select only these indexes
  -h, --headers=LIST
                 select only these headers
  -t, --tsv
                 equivalent to -d'\t'
  -d, --delimiter=DELIM
                 use DELIM instead of comma for field delimiter
  -D, --output-delimiter=STRING
                 use STRING as the output delimiter (default: \t)
  --help
                 display this help text and exit
  --version
                 output version information and exit
`[1:], name)
}

func printVersion() {
	fmt.Fprintln(os.Stderr, version)
}

func printErr(err interface{}) {
	fmt.Fprintf(os.Stderr, "%s: %s\n", name, err)
}

func guideToHelp() {
	fmt.Fprintf(os.Stderr, "Try '%s --help' for more information.\n", name)
}

func toDelimiter(s string) (r rune, err error) {
	s, err = strconv.Unquote(`"` + s + `"`)
	if err != nil {
		return 0, err
	}

	runes := []rune(s)
	if len(runes) != 1 {
		return 0, fmt.Errorf("the delimiter must be a single character")
	}
	return runes[0], nil
}

func do(c *CSVScanner) error {
	for c.Scan() {
		fmt.Println(c.Text())
	}
	return c.Err()
}

func _main() int {
	flagset.SetOutput(ioutil.Discard)
	if err := flagset.Parse(os.Args[1:]); err != nil {
		printErr(err)
		guideToHelp()
		return 2
	}
	switch {
	case *isHelp:
		printUsage()
		return 0
	case *isVersion:
		printVersion()
		return 0
	}

	var selector Selector
	switch {
	case *indexesList != "" && *headersList != "":
		printErr("only one type of list may be specified")
		guideToHelp()
		return 2
	case *indexesList != "":
		selector = NewIndexes(*indexesList)
	case *headersList != "":
		selector = NewHeaders(*headersList)
	default:
		selector = NewAll()
	}

	r, err := argf.From(flagset.Args())
	if err != nil {
		printErr(err)
		guideToHelp()
		return 2
	}

	c := NewCSVScanner(selector, r)
	c.SetOutputDelimiter(*outputDelimiter)
	switch {
	case *isTSV:
		c.SetDelimiter('\t')
	default:
		ch, err := toDelimiter(*delimiter)
		if err != nil {
			printErr(err)
			guideToHelp()
			return 2
		}
		c.SetDelimiter(ch)
	}

	if err = do(c); err != nil {
		printErr(err)
		return 1
	}
	return 0
}

func main() {
	e := _main()
	os.Exit(e)
}
<commit_msg>Upgrade to interpret "\n" as "n" in headers<commit_after>package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"strconv"

	"github.com/ogier/pflag"
	"github.com/yuya-takeyama/argf"
)

var (
	name    = "csvp"
	version = "0.9.0"

	flagset         = pflag.NewFlagSet(name, pflag.ContinueOnError)
	indexesList     = flagset.StringP("indexes", "i", "", "")
	headersList     = flagset.StringP("headers", "h", "", "")
	isTSV           = flagset.BoolP("tsv", "t", false, "")
	delimiter       = flagset.StringP("delimiter", "d", ",", "")
	outputDelimiter = flagset.StringP("output-delimiter", "D", "\t", "")
	isHelp          = flagset.BoolP("help", "", false, "")
	isVersion       = flagset.BoolP("version", "", false, "")
)

func printUsage() {
	fmt.Fprintf(os.Stderr, `
Usage: %s [OPTION]... [FILE]...
Print selected parts of CSV from each FILE to standard output.

Options:
  -i, --indexes=LIST
                 select only these indexes
  -h, --headers=LIST
                 select only these headers
  -t, --tsv
                 equivalent to -d'\t'
  -d, --delimiter=DELIM
                 use DELIM instead of comma for field delimiter
  -D, --output-delimiter=STRING
                 use STRING as the output delimiter (default: \t)
  --help
                 display this help text and exit
  --version
                 output version information and exit
`[1:], name)
}

func printVersion() {
	fmt.Fprintln(os.Stderr, version)
}

func printErr(err interface{}) {
	fmt.Fprintf(os.Stderr, "%s: %s\n", name, err)
}

func guideToHelp() {
	fmt.Fprintf(os.Stderr, "Try '%s --help' for more information.\n", name)
}

func toDelimiter(s string) (r rune, err error) {
	s, err = strconv.Unquote(`"` + s + `"`)
	if err != nil {
		return 0, err
	}

	runes := []rune(s)
	if len(runes) != 1 {
		return 0, fmt.Errorf("the delimiter must be a single character")
	}
	return runes[0], nil
}

func do(c *CSVScanner) error {
	for c.Scan() {
		fmt.Println(c.Text())
	}
	return c.Err()
}

func _main() int {
	flagset.SetOutput(ioutil.Discard)
	if err := flagset.Parse(os.Args[1:]); err != nil {
		printErr(err)
		guideToHelp()
		return 2
	}
	switch {
	case *isHelp:
		printUsage()
		return 0
	case *isVersion:
		printVersion()
		return 0
	}

	var selector Selector
	switch {
	case *indexesList != "" && *headersList != "":
		printErr("only one type of list may be specified")
		guideToHelp()
		return 2
	case *indexesList != "":
		selector = NewIndexes(*indexesList)
	case *headersList != "":
		selector = NewHeaders(*headersList)
	default:
		selector = NewAll()
	}

	r, err := argf.From(flagset.Args())
	if err != nil {
		printErr(err)
		guideToHelp()
		return 2
	}

	c := NewCSVScanner(selector, r)
	c.SetOutputDelimiter(*outputDelimiter)
	switch {
	case *isTSV:
		c.SetDelimiter('\t')
	default:
		ch, err := toDelimiter(*delimiter)
		if err != nil {
			printErr(err)
			guideToHelp()
			return 2
		}
		c.SetDelimiter(ch)
	}

	if err = do(c); err != nil {
		printErr(err)
		return 1
	}
	return 0
}

func main() {
	e := _main()
	os.Exit(e)
}
<|endoftext|>


<commit_before>package main

import (
	"os"
	"github.com/codegangsta/cli"
	"fmt"
	"errors"
	"path"
)

// This variable is set at build time using -ldflags parameters. For more info, see:
// http://stackoverflow.com/a/11355611/483528
var VERSION string

type FetchOptions struct {
	RepoUrl string
	CommitSha string
	BranchName string
	TagConstraint string
	GithubToken string
	SourcePaths []string
	ReleaseAssets []string
	LocalDownloadPath string
}

const OPTION_REPO = "repo"
const OPTION_COMMIT = "commit"
const OPTION_BRANCH = "branch"
const OPTION_TAG = "tag"
const OPTION_GITHUB_TOKEN = "github-oauth-token"
const OPTION_SOURCE_PATH = "source-path"
const OPTION_RELEASE_ASSET = "release-asset"

const ENV_VAR_GITHUB_TOKEN = "GITHUB_OAUTH_TOKEN"

func main() {
	app := cli.NewApp()
	app.Name = "fetch"
	app.Usage = "fetch makes it easy to download files, folders, and release assets from a specific git commit, branch, or tag of public and private GitHub repos."
	app.UsageText = "fetch [global options] <local-download-path>\n   (See https://github.com/gruntwork-io/fetch for examples, argument definitions, and additional docs.)"
	app.Version = VERSION

	app.Flags = []cli.Flag{
		cli.StringFlag{
			Name: OPTION_REPO,
			Usage: "Required. Fully qualified URL of the GitHub repo.",
		},
		cli.StringFlag{
			Name: OPTION_COMMIT,
			Usage: "The specific git commit SHA to download. If specified, will override --branch and --tag.",
		},
		cli.StringFlag{
			Name: OPTION_BRANCH,
			Usage: "The git branch from which to download the commit; the latest commit in th branch will be used. If specified, will override --tag.",
		},
		cli.StringFlag{
			Name: OPTION_TAG,
			Usage: "The specific git tag to download, expressed with Version Constraint Operators.\n\tIf left blank, fetch will download the latest git tag.\n\tSee https://github.com/gruntwork-io/fetch#version-constraint-operators for examples.",
		},
		cli.StringFlag{
			Name: OPTION_GITHUB_TOKEN,
			Usage: "A GitHub Personal Access Token, which is required for downloading from private repos.",
			EnvVar: ENV_VAR_GITHUB_TOKEN,
		},
		cli.StringSliceFlag{
			Name: OPTION_SOURCE_PATH,
			Usage: "The source path to download from the repo. If this or --release-asset aren't specified, all files are downloaded. Can be specified more than once.",
		},
		cli.StringSliceFlag{
			Name: OPTION_RELEASE_ASSET,
			Usage: "The name of a release asset--that is, a binary uploaded to a GitHub Release--to download. Only works with --tag. Can be specified more than once.",
		},
	}

	app.Action = runFetchWrapper

	// Run the definition of App.Action
	app.Run(os.Args)
}

// We just want to call runFetch(), but app.Action won't permit us to return an error, so call a wrapper function instead.
func runFetchWrapper (c *cli.Context) {
	err := runFetch(c)
	if err != nil {
		fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
		os.Exit(1)
	}
}

// Run the fetch program
func runFetch (c *cli.Context) error {
	options := parseOptions(c)
	if err := validateOptions(options); err != nil {
		return err
	}

	// Get the tags for the given repo
	tags, err := FetchTags(options.RepoUrl, options.GithubToken)
	if err != nil {
		if err.errorCode == INVALID_GITHUB_TOKEN_OR_ACCESS_DENIED {
			return errors.New(getErrorMessage(INVALID_GITHUB_TOKEN_OR_ACCESS_DENIED, err.details))
		} else if err.errorCode == REPO_DOES_NOT_EXIST_OR_ACCESS_DENIED {
			return errors.New(getErrorMessage(REPO_DOES_NOT_EXIST_OR_ACCESS_DENIED, err.details))
		} else {
			return fmt.Errorf("Error occurred while getting tags from GitHub repo: %s", err)
		}
	}

	specific, desiredTag := isTagConstraintSpecificTag(options.TagConstraint)
	if !specific {
		// Find the specific release that matches the latest version constraint
		latestTag, err := getLatestAcceptableTag(options.TagConstraint, tags)
		if err != nil {
			if err.errorCode == INVALID_TAG_CONSTRAINT_EXPRESSION {
				return errors.New(getErrorMessage(INVALID_TAG_CONSTRAINT_EXPRESSION, err.details))
			} else {
				return fmt.Errorf("Error occurred while computing latest tag that satisfies version contraint expression: %s", err)
			}
		}
		desiredTag = latestTag
	}

	// Prepare the vars we'll need to download
	repo, err := ParseUrlIntoGitHubRepo(options.RepoUrl, options.GithubToken)
	if err != nil {
		return fmt.Errorf("Error occurred while parsing GitHub URL: %s", err)
	}

	// If no release assets and no source paths are specified, then by default, download all the source files from
	// the repo
	if len(options.SourcePaths) == 0 && len(options.ReleaseAssets) == 0 {
		options.SourcePaths = []string{"/"}
	}

	// Download any requested source files
	if err := downloadSourcePaths(options.SourcePaths, options.LocalDownloadPath, repo, desiredTag, options.BranchName, options.CommitSha); err != nil {
		return err
	}

	// Download any requested release assets
	if err := downloadReleaseAssets(options.ReleaseAssets, options.LocalDownloadPath, repo, desiredTag); err != nil {
		return err
	}

	return nil
}

func parseOptions(c *cli.Context) FetchOptions {
	localDownloadPath := c.Args().First()
	sourcePaths := c.StringSlice(OPTION_SOURCE_PATH)

	// Maintain backwards compatibility with older versions of fetch that passed source paths as an optional first
	// command-line arg
	if c.NArg() == 2 {
		fmt.Printf("DEPRECATION WARNING: passing source paths via command-line args is deprecated. Please use the --%s option instead!\n", OPTION_SOURCE_PATH)
		sourcePaths = []string{c.Args().First()}
		localDownloadPath = c.Args().Get(1)
	}

	return FetchOptions{
		RepoUrl: c.String(OPTION_REPO),
		CommitSha: c.String(OPTION_COMMIT),
		BranchName: c.String(OPTION_BRANCH),
		TagConstraint: c.String(OPTION_TAG),
		GithubToken: c.String(OPTION_GITHUB_TOKEN),
		SourcePaths: sourcePaths,
		ReleaseAssets: c.StringSlice(OPTION_RELEASE_ASSET),
		LocalDownloadPath: localDownloadPath,
	}
}

func validateOptions(options FetchOptions) error {
	if options.RepoUrl == "" {
		return fmt.Errorf("The --%s flag is required. Run \"fetch --help\" for full usage info.", OPTION_REPO)
	}

	if options.LocalDownloadPath == "" {
		return fmt.Errorf("Missing required arguments specifying the local download path. Run \"fetch --help\" for full usage info.")
	}

	if options.TagConstraint == "" && options.CommitSha == "" && options.BranchName == "" {
		return fmt.Errorf("You must specify exactly one of --%s, --%s, or --%s. Run \"fetch --help\" for full usage info.", OPTION_TAG, OPTION_COMMIT, OPTION_BRANCH)
	}

	if len(options.ReleaseAssets) > 0 && options.TagConstraint == "" {
		return fmt.Errorf("The --%s flag can only be used with --%s. Run \"fetch --help\" for full usage info.", OPTION_RELEASE_ASSET, OPTION_TAG)
	}

	return nil
}

// Download the specified source files from the given repo
func downloadSourcePaths(sourcePaths []string, destPath string, githubRepo GitHubRepo, latestTag string, branchName string, commitSha string) error {
	if len(sourcePaths) == 0 {
		return  nil
	}

	// We want to respect the GitHubCommit Hierarchy of "CommitSha > GitTag > BranchName"
	// Note that CommitSha or BranchName may be blank here if the user did not specify values for these.
	// If the user specified no value for GitTag, our call to getLatestAcceptableTag() above still gave us some value
	// So we can guarantee (at least logically) that this struct instance is in a valid state right now.
	gitHubCommit := GitHubCommit{
		Repo: githubRepo,
		GitTag: latestTag,
		BranchName: branchName,
		CommitSha: commitSha,
	}

	// Download that release as a .zip file
	if gitHubCommit.CommitSha != "" {
		fmt.Printf("Downloading git commit \"%s\" of %s ...\n", gitHubCommit.CommitSha, githubRepo.Url)
	} else if gitHubCommit.BranchName != "" {
		fmt.Printf("Downloading latest commit from branch \"%s\" of %s ...\n", gitHubCommit.BranchName, githubRepo.Url)
	} else if gitHubCommit.GitTag != "" {
		fmt.Printf("Downloading tag \"%s\" of %s ...\n", latestTag, githubRepo.Url)
	} else {
		return fmt.Errorf("The commit sha, tag, and branch name are all empty.")
	}

	localZipFilePath, err := downloadGithubZipFile(gitHubCommit, githubRepo.Token)
	if err != nil {
		return fmt.Errorf("Error occurred while downloading zip file from GitHub repo: %s", err)
	}
	defer cleanupZipFile(localZipFilePath)

	// Unzip and move the files we need to our destination
	for _, sourcePath := range sourcePaths {
		fmt.Printf("Extracting files from <repo>%s to %s ...\n", sourcePath, destPath)
		if err := extractFiles(localZipFilePath, sourcePath, destPath); err != nil {
			return fmt.Errorf("Error occurred while extracting files from GitHub zip file: %s", err.Error())
		}
	}

	fmt.Println("Download and file extraction complete.")
	return nil
}

// Download the specified binary files that were uploaded as release assets to the specified GitHub release
func downloadReleaseAssets(releaseAssets []string, destPath string, githubRepo GitHubRepo, latestTag string) error {
	if len(releaseAssets) == 0 {
		return nil
	}

	release, err := GetGitHubReleaseInfo(githubRepo, latestTag)
	if err != nil {
		return err
	}

	for _, assetName := range releaseAssets {
		asset := findAssetInRelease(assetName, release)
		if asset == nil {
			return fmt.Errorf("Could not find asset %s in release %s", assetName, latestTag)
		}

		assetPath := path.Join(destPath, asset.Name)
		fmt.Printf("Downloading release asset %s to %s\n", asset.Name, assetPath)
		if err := DownloadReleaseAsset(githubRepo, asset.Id, assetPath); err != nil {
			return err
		}
	}

	fmt.Println("Download of release assets complete.")
	return nil
}

func findAssetInRelease(assetName string, release GitHubReleaseApiResponse) *GitHubReleaseAsset {
	for _, asset := range release.Assets {
		if asset.Name == assetName {
			return &asset
		}
	}

	return nil
}

// Delete the given zip file.
func cleanupZipFile(localZipFilePath string) error {
	err := os.Remove(localZipFilePath)
	if err != nil {
		return fmt.Errorf("Failed to delete local zip file at %s", localZipFilePath)
	}

	return nil
}

func getErrorMessage(errorCode int, errorDetails string) string {
	switch errorCode {
	case INVALID_TAG_CONSTRAINT_EXPRESSION:
		return fmt.Sprintf(`
The --tag value you entered is not a valid constraint expression.
See https://github.com/gruntwork-io/fetch#version-constraint-operators for examples.

Underlying error message:
%s
`, errorDetails)
	case INVALID_GITHUB_TOKEN_OR_ACCESS_DENIED:
		return fmt.Sprintf(`
Received an HTTP 401 Response when attempting to query the repo for its tags.

This means that either your GitHub oAuth Token is invalid, or that the token is valid but is being used to request access
to either a public repo or a private repo to which you don't have access.

Underlying error message:
%s
`, errorDetails)
	case REPO_DOES_NOT_EXIST_OR_ACCESS_DENIED:
		return fmt.Sprintf(`
Received an HTTP 404 Response when attempting to query the repo for its tags.

This means that either no GitHub repo exists at the URL provided, or that you don't have permission to access it.
If the URL is correct, you may need to pass in a --github-oauth-token.

Underlying error message:
%s
`, errorDetails)
	}

	return ""
}<commit_msg>Fetch tweaks.<commit_after>package main

import (
	"os"
	"github.com/codegangsta/cli"
	"fmt"
	"errors"
	"path"
)

// This variable is set at build time using -ldflags parameters. For more info, see:
// http://stackoverflow.com/a/11355611/483528
var VERSION string

type FetchOptions struct {
	RepoUrl string
	CommitSha string
	BranchName string
	TagConstraint string
	GithubToken string
	SourcePaths []string
	ReleaseAssets []string
	LocalDownloadPath string
}

const OPTION_REPO = "repo"
const OPTION_COMMIT = "commit"
const OPTION_BRANCH = "branch"
const OPTION_TAG = "tag"
const OPTION_GITHUB_TOKEN = "github-oauth-token"
const OPTION_SOURCE_PATH = "source-path"
const OPTION_RELEASE_ASSET = "release-asset"

const ENV_VAR_GITHUB_TOKEN = "GITHUB_OAUTH_TOKEN"

func main() {
	app := cli.NewApp()
	app.Name = "fetch"
	app.Usage = "fetch makes it easy to download files, folders, and release assets from a specific git commit, branch, or tag of public and private GitHub repos."
	app.UsageText = "fetch [global options] <local-download-path>\n   (See https://github.com/gruntwork-io/fetch for examples, argument definitions, and additional docs.)"
	app.Version = VERSION

	app.Flags = []cli.Flag{
		cli.StringFlag{
			Name: OPTION_REPO,
			Usage: "Required. Fully qualified URL of the GitHub repo.",
		},
		cli.StringFlag{
			Name: OPTION_COMMIT,
			Usage: "The specific git commit SHA to download. If specified, will override --branch and --tag.",
		},
		cli.StringFlag{
			Name: OPTION_BRANCH,
			Usage: "The git branch from which to download the commit; the latest commit in the branch will be used. If specified, will override --tag.",
		},
		cli.StringFlag{
			Name: OPTION_TAG,
			Usage: "The specific git tag to download, expressed with Version Constraint Operators.\n\tIf left blank, fetch will download the latest git tag.\n\tSee https://github.com/gruntwork-io/fetch#version-constraint-operators for examples.",
		},
		cli.StringFlag{
			Name: OPTION_GITHUB_TOKEN,
			Usage: "A GitHub Personal Access Token, which is required for downloading from private repos.",
			EnvVar: ENV_VAR_GITHUB_TOKEN,
		},
		cli.StringSliceFlag{
			Name: OPTION_SOURCE_PATH,
			Usage: "The source path to download from the repo. If this or --release-asset aren't specified, all files are downloaded. Can be specified more than once.",
		},
		cli.StringSliceFlag{
			Name: OPTION_RELEASE_ASSET,
			Usage: "The name of a release asset--that is, a binary uploaded to a GitHub Release--to download. Only works with --tag. Can be specified more than once.",
		},
	}

	app.Action = runFetchWrapper

	// Run the definition of App.Action
	app.Run(os.Args)
}

// We just want to call runFetch(), but app.Action won't permit us to return an error, so call a wrapper function instead.
func runFetchWrapper (c *cli.Context) {
	err := runFetch(c)
	if err != nil {
		fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
		os.Exit(1)
	}
}

// Run the fetch program
func runFetch (c *cli.Context) error {
	options := parseOptions(c)
	if err := validateOptions(options); err != nil {
		return err
	}

	// Get the tags for the given repo
	tags, err := FetchTags(options.RepoUrl, options.GithubToken)
	if err != nil {
		if err.errorCode == INVALID_GITHUB_TOKEN_OR_ACCESS_DENIED {
			return errors.New(getErrorMessage(INVALID_GITHUB_TOKEN_OR_ACCESS_DENIED, err.details))
		} else if err.errorCode == REPO_DOES_NOT_EXIST_OR_ACCESS_DENIED {
			return errors.New(getErrorMessage(REPO_DOES_NOT_EXIST_OR_ACCESS_DENIED, err.details))
		} else {
			return fmt.Errorf("Error occurred while getting tags from GitHub repo: %s", err)
		}
	}

	specific, desiredTag := isTagConstraintSpecificTag(options.TagConstraint)
	if !specific {
		// Find the specific release that matches the latest version constraint
		latestTag, err := getLatestAcceptableTag(options.TagConstraint, tags)
		if err != nil {
			if err.errorCode == INVALID_TAG_CONSTRAINT_EXPRESSION {
				return errors.New(getErrorMessage(INVALID_TAG_CONSTRAINT_EXPRESSION, err.details))
			} else {
				return fmt.Errorf("Error occurred while computing latest tag that satisfies version contraint expression: %s", err)
			}
		}
		desiredTag = latestTag
	}

	// Prepare the vars we'll need to download
	repo, err := ParseUrlIntoGitHubRepo(options.RepoUrl, options.GithubToken)
	if err != nil {
		return fmt.Errorf("Error occurred while parsing GitHub URL: %s", err)
	}

	// If no release assets and no source paths are specified, then by default, download all the source files from
	// the repo
	if len(options.SourcePaths) == 0 && len(options.ReleaseAssets) == 0 {
		options.SourcePaths = []string{"/"}
	}

	// Download any requested source files
	if err := downloadSourcePaths(options.SourcePaths, options.LocalDownloadPath, repo, desiredTag, options.BranchName, options.CommitSha); err != nil {
		return err
	}

	// Download any requested release assets
	if err := downloadReleaseAssets(options.ReleaseAssets, options.LocalDownloadPath, repo, desiredTag); err != nil {
		return err
	}

	return nil
}

func parseOptions(c *cli.Context) FetchOptions {
	localDownloadPath := c.Args().First()
	sourcePaths := c.StringSlice(OPTION_SOURCE_PATH)

	// Maintain backwards compatibility with older versions of fetch that passed source paths as an optional first
	// command-line arg
	if c.NArg() == 2 {
		fmt.Printf("DEPRECATION WARNING: passing source paths via command-line args is deprecated. Please use the --%s option instead!\n", OPTION_SOURCE_PATH)
		sourcePaths = []string{c.Args().First()}
		localDownloadPath = c.Args().Get(1)
	}

	return FetchOptions{
		RepoUrl: c.String(OPTION_REPO),
		CommitSha: c.String(OPTION_COMMIT),
		BranchName: c.String(OPTION_BRANCH),
		TagConstraint: c.String(OPTION_TAG),
		GithubToken: c.String(OPTION_GITHUB_TOKEN),
		SourcePaths: sourcePaths,
		ReleaseAssets: c.StringSlice(OPTION_RELEASE_ASSET),
		LocalDownloadPath: localDownloadPath,
	}
}

func validateOptions(options FetchOptions) error {
	if options.RepoUrl == "" {
		return fmt.Errorf("The --%s flag is required. Run \"fetch --help\" for full usage info.", OPTION_REPO)
	}

	if options.LocalDownloadPath == "" {
		return fmt.Errorf("Missing required arguments specifying the local download path. Run \"fetch --help\" for full usage info.")
	}

	if options.TagConstraint == "" && options.CommitSha == "" && options.BranchName == "" {
		return fmt.Errorf("You must specify exactly one of --%s, --%s, or --%s. Run \"fetch --help\" for full usage info.", OPTION_TAG, OPTION_COMMIT, OPTION_BRANCH)
	}

	if len(options.ReleaseAssets) > 0 && options.TagConstraint == "" {
		return fmt.Errorf("The --%s flag can only be used with --%s. Run \"fetch --help\" for full usage info.", OPTION_RELEASE_ASSET, OPTION_TAG)
	}

	return nil
}

// Download the specified source files from the given repo
func downloadSourcePaths(sourcePaths []string, destPath string, githubRepo GitHubRepo, latestTag string, branchName string, commitSha string) error {
	if len(sourcePaths) == 0 {
		return  nil
	}

	// We want to respect the GitHubCommit Hierarchy of "CommitSha > GitTag > BranchName"
	// Note that CommitSha or BranchName may be blank here if the user did not specify values for these.
	// If the user specified no value for GitTag, our call to getLatestAcceptableTag() above still gave us some value
	// So we can guarantee (at least logically) that this struct instance is in a valid state right now.
	gitHubCommit := GitHubCommit{
		Repo: githubRepo,
		GitTag: latestTag,
		BranchName: branchName,
		CommitSha: commitSha,
	}

	// Download that release as a .zip file
	if gitHubCommit.CommitSha != "" {
		fmt.Printf("Downloading git commit \"%s\" of %s ...\n", gitHubCommit.CommitSha, githubRepo.Url)
	} else if gitHubCommit.BranchName != "" {
		fmt.Printf("Downloading latest commit from branch \"%s\" of %s ...\n", gitHubCommit.BranchName, githubRepo.Url)
	} else if gitHubCommit.GitTag != "" {
		fmt.Printf("Downloading tag \"%s\" of %s ...\n", latestTag, githubRepo.Url)
	} else {
		return fmt.Errorf("The commit sha, tag, and branch name are all empty.")
	}

	localZipFilePath, err := downloadGithubZipFile(gitHubCommit, githubRepo.Token)
	if err != nil {
		return fmt.Errorf("Error occurred while downloading zip file from GitHub repo: %s", err)
	}
	defer cleanupZipFile(localZipFilePath)

	// Unzip and move the files we need to our destination
	for _, sourcePath := range sourcePaths {
		fmt.Printf("Extracting files from <repo>%s to %s ...\n", sourcePath, destPath)
		if err := extractFiles(localZipFilePath, sourcePath, destPath); err != nil {
			return fmt.Errorf("Error occurred while extracting files from GitHub zip file: %s", err.Error())
		}
	}

	fmt.Println("Download and file extraction complete.")
	return nil
}

// Download the specified binary files that were uploaded as release assets to the specified GitHub release
func downloadReleaseAssets(releaseAssets []string, destPath string, githubRepo GitHubRepo, latestTag string) error {
	if len(releaseAssets) == 0 {
		return nil
	}

	release, err := GetGitHubReleaseInfo(githubRepo, latestTag)
	if err != nil {
		return err
	}

	for _, assetName := range releaseAssets {
		asset := findAssetInRelease(assetName, release)
		if asset == nil {
			return fmt.Errorf("Could not find asset %s in release %s", assetName, latestTag)
		}

		assetPath := path.Join(destPath, asset.Name)
		fmt.Printf("Downloading release asset %s to %s\n", asset.Name, assetPath)
		if err := DownloadReleaseAsset(githubRepo, asset.Id, assetPath); err != nil {
			return err
		}
	}

	fmt.Println("Download of release assets complete.")
	return nil
}

func findAssetInRelease(assetName string, release GitHubReleaseApiResponse) *GitHubReleaseAsset {
	for _, asset := range release.Assets {
		if asset.Name == assetName {
			return &asset
		}
	}

	return nil
}

// Delete the given zip file.
func cleanupZipFile(localZipFilePath string) error {
	err := os.Remove(localZipFilePath)
	if err != nil {
		return fmt.Errorf("Failed to delete local zip file at %s", localZipFilePath)
	}

	return nil
}

func getErrorMessage(errorCode int, errorDetails string) string {
	switch errorCode {
	case INVALID_TAG_CONSTRAINT_EXPRESSION:
		return fmt.Sprintf(`
The --tag value you entered is not a valid constraint expression.
See https://github.com/gruntwork-io/fetch#version-constraint-operators for examples.

Underlying error message:
%s
`, errorDetails)
	case INVALID_GITHUB_TOKEN_OR_ACCESS_DENIED:
		return fmt.Sprintf(`
Received an HTTP 401 Response when attempting to query the repo for its tags.

This means that either your GitHub oAuth Token is invalid, or that the token is valid but is being used to request access
to either a public repo or a private repo to which you don't have access.

Underlying error message:
%s
`, errorDetails)
	case REPO_DOES_NOT_EXIST_OR_ACCESS_DENIED:
		return fmt.Sprintf(`
Received an HTTP 404 Response when attempting to query the repo for its tags.

This means that either no GitHub repo exists at the URL provided, or that you don't have permission to access it.
If the URL is correct, you may need to pass in a --github-oauth-token.

Underlying error message:
%s
`, errorDetails)
	}

	return ""
}
<|endoftext|>


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

package main

import (
	"fmt"
	"os"
	"os/user"
	"runtime"
	"strconv"
	"time"

	"github.com/dustin/go-humanize"
	"github.com/minio/cli"
	"github.com/minio/minio/pkg/iodine"
	"github.com/minio/minio/pkg/server/httpserver"
)

var globalDebugFlag = false

var flags = []cli.Flag{
	cli.StringFlag{
		Name:  "address",
		Value: ":9000",
		Usage: "ADDRESS:PORT for object storage access",
	},
	/*
		cli.StringFlag{
			Name:  "address-mgmt",
			Value: ":9001",
			Usage: "ADDRESS:PORT for management console access",
		},
	*/
	cli.IntFlag{
		Name:  "ratelimit",
		Value: 16,
		Usage: "Limit for total concurrent requests: [DEFAULT: 16]",
	},
	cli.StringFlag{
		Name:  "cert",
		Hide:  true,
		Value: "",
		Usage: "cert.pem",
	},
	cli.StringFlag{
		Name:  "key",
		Hide:  true,
		Value: "",
		Usage: "key.pem",
	},
	cli.BoolFlag{
		Name:  "debug",
		Usage: "print debug information",
	},
}

func init() {
	// Check for the environment early on and gracefuly report.
	_, err := user.Current()
	if err != nil {
		Fatalf("Unable to obtain user's home directory. \nError: %s\n", err)
	}
}

func getAPIServerConfig(c *cli.Context) httpserver.Config {
	certFile := c.String("cert")
	keyFile := c.String("key")
	if (certFile != "" && keyFile == "") || (certFile == "" && keyFile != "") {
		Fatalln("Both certificate and key are required to enable https.")
	}
	tls := (certFile != "" && keyFile != "")
	return httpserver.Config{
		Address:   c.GlobalString("address"),
		TLS:       tls,
		CertFile:  certFile,
		KeyFile:   keyFile,
		RateLimit: c.GlobalInt("ratelimit"),
	}
}

/*
func getWebServerConfigFunc(c *cli.Context) server.StartServerFunc {
	config := httpserver.Config{
		Address:  c.GlobalString("address-mgmt"),
		TLS:      false,
		CertFile: "",
		KeyFile:  "",
	}
	webDrivers := server.WebFactory{
		Config: config,
	}
	return webDrivers.GetStartServerFunc()
}
*/

// Build date
var BuildDate string

// getBuildDate -
func getBuildDate() string {
	t, _ := time.Parse(time.RFC3339Nano, BuildDate)
	if t.IsZero() {
		return ""
	}
	return t.Format(time.RFC1123)
}

// Tries to get os/arch/platform specific information
// Returns a map of current os/arch/platform/memstats
func getSystemData() map[string]string {
	host, err := os.Hostname()
	if err != nil {
		host = ""
	}
	memstats := &runtime.MemStats{}
	runtime.ReadMemStats(memstats)
	mem := fmt.Sprintf("Used: %s | Allocated: %s | Used-Heap: %s | Allocated-Heap: %s",
		humanize.Bytes(memstats.Alloc),
		humanize.Bytes(memstats.TotalAlloc),
		humanize.Bytes(memstats.HeapAlloc),
		humanize.Bytes(memstats.HeapSys))
	platform := fmt.Sprintf("Host: %s | OS: %s | Arch: %s",
		host,
		runtime.GOOS,
		runtime.GOARCH)
	goruntime := fmt.Sprintf("Version: %s | CPUs: %s", runtime.Version(), strconv.Itoa(runtime.NumCPU()))
	return map[string]string{
		"PLATFORM": platform,
		"RUNTIME":  goruntime,
		"MEM":      mem,
	}
}

// Version is based on MD5SUM of its binary
var Version = mustHashBinarySelf()

// Help template
var minioHelpTemplate = `NAME:
  {{.Name}} - {{.Usage}}

USAGE:
  {{.Name}} {{if .Flags}}[global flags] {{end}}command{{if .Flags}} [command flags]{{end}} [arguments...]

COMMANDS:
  {{range .Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
  {{end}}{{if .Flags}}
GLOBAL FLAGS:
  {{range .Flags}}{{.}}
  {{end}}{{end}}
VERSION:
  {{.Version}}
  {{if .Compiled}}
BUILD:
  {{.Compiled}}{{end}}
  {{range $key, $value := .ExtraInfo}}
{{$key}}:
  {{$value}}
{{end}}`

func main() {
	// set up iodine
	iodine.SetGlobalState("minio.version", Version)
	iodine.SetGlobalState("minio.starttime", time.Now().Format(time.RFC3339))

	// set up go max processes
	runtime.GOMAXPROCS(runtime.NumCPU())

	// set up app
	app := cli.NewApp()
	app.Name = "minio"
	app.Version = Version
	app.Compiled = getBuildDate()
	app.Author = "Minio.io"
	app.Usage = "Minimalist Object Storage"
	app.Flags = flags
	app.Commands = commands
	app.Before = func(c *cli.Context) error {
		if c.GlobalBool("debug") {
			app.ExtraInfo = getSystemData()
		}
		return nil
	}
	app.CustomAppHelpTemplate = minioHelpTemplate
	app.RunAndExitOnError()
}
<commit_msg>Fix erroneous new line in helptemplate<commit_after>/*
 * Minimalist Object Storage, (C) 2015 Minio, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package main

import (
	"fmt"
	"os"
	"os/user"
	"runtime"
	"strconv"
	"time"

	"github.com/dustin/go-humanize"
	"github.com/minio/cli"
	"github.com/minio/minio/pkg/iodine"
	"github.com/minio/minio/pkg/server/httpserver"
)

var globalDebugFlag = false

var flags = []cli.Flag{
	cli.StringFlag{
		Name:  "address",
		Value: ":9000",
		Usage: "ADDRESS:PORT for object storage access",
	},
	/*
		cli.StringFlag{
			Name:  "address-mgmt",
			Value: ":9001",
			Usage: "ADDRESS:PORT for management console access",
		},
	*/
	cli.IntFlag{
		Name:  "ratelimit",
		Value: 16,
		Usage: "Limit for total concurrent requests: [DEFAULT: 16]",
	},
	cli.StringFlag{
		Name:  "cert",
		Hide:  true,
		Value: "",
		Usage: "cert.pem",
	},
	cli.StringFlag{
		Name:  "key",
		Hide:  true,
		Value: "",
		Usage: "key.pem",
	},
	cli.BoolFlag{
		Name:  "debug",
		Usage: "print debug information",
	},
}

func init() {
	// Check for the environment early on and gracefuly report.
	_, err := user.Current()
	if err != nil {
		Fatalf("Unable to obtain user's home directory. \nError: %s\n", err)
	}
}

func getAPIServerConfig(c *cli.Context) httpserver.Config {
	certFile := c.String("cert")
	keyFile := c.String("key")
	if (certFile != "" && keyFile == "") || (certFile == "" && keyFile != "") {
		Fatalln("Both certificate and key are required to enable https.")
	}
	tls := (certFile != "" && keyFile != "")
	return httpserver.Config{
		Address:   c.GlobalString("address"),
		TLS:       tls,
		CertFile:  certFile,
		KeyFile:   keyFile,
		RateLimit: c.GlobalInt("ratelimit"),
	}
}

/*
func getWebServerConfigFunc(c *cli.Context) server.StartServerFunc {
	config := httpserver.Config{
		Address:  c.GlobalString("address-mgmt"),
		TLS:      false,
		CertFile: "",
		KeyFile:  "",
	}
	webDrivers := server.WebFactory{
		Config: config,
	}
	return webDrivers.GetStartServerFunc()
}
*/

// Build date
var BuildDate string

// getBuildDate -
func getBuildDate() string {
	t, _ := time.Parse(time.RFC3339Nano, BuildDate)
	if t.IsZero() {
		return ""
	}
	return t.Format(time.RFC1123)
}

// Tries to get os/arch/platform specific information
// Returns a map of current os/arch/platform/memstats
func getSystemData() map[string]string {
	host, err := os.Hostname()
	if err != nil {
		host = ""
	}
	memstats := &runtime.MemStats{}
	runtime.ReadMemStats(memstats)
	mem := fmt.Sprintf("Used: %s | Allocated: %s | Used-Heap: %s | Allocated-Heap: %s",
		humanize.Bytes(memstats.Alloc),
		humanize.Bytes(memstats.TotalAlloc),
		humanize.Bytes(memstats.HeapAlloc),
		humanize.Bytes(memstats.HeapSys))
	platform := fmt.Sprintf("Host: %s | OS: %s | Arch: %s",
		host,
		runtime.GOOS,
		runtime.GOARCH)
	goruntime := fmt.Sprintf("Version: %s | CPUs: %s", runtime.Version(), strconv.Itoa(runtime.NumCPU()))
	return map[string]string{
		"PLATFORM": platform,
		"RUNTIME":  goruntime,
		"MEM":      mem,
	}
}

// Version is based on MD5SUM of its binary
var Version = mustHashBinarySelf()

// Help template
var minioHelpTemplate = `NAME:
  {{.Name}} - {{.Usage}}

USAGE:
  {{.Name}} {{if .Flags}}[global flags] {{end}}command{{if .Flags}} [command flags]{{end}} [arguments...]

COMMANDS:
  {{range .Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
  {{end}}{{if .Flags}}
GLOBAL FLAGS:
  {{range .Flags}}{{.}}
  {{end}}{{end}}
VERSION:
  {{.Version}}
  {{if .Compiled}}
BUILD:
  {{.Compiled}}{{end}}
  {{range $key, $value := .ExtraInfo}}
{{$key}}:
  {{$value}}
{{end}}
`

func main() {
	// set up iodine
	iodine.SetGlobalState("minio.version", Version)
	iodine.SetGlobalState("minio.starttime", time.Now().Format(time.RFC3339))

	// set up go max processes
	runtime.GOMAXPROCS(runtime.NumCPU())

	// set up app
	app := cli.NewApp()
	app.Name = "minio"
	app.Version = Version
	app.Compiled = getBuildDate()
	app.Author = "Minio.io"
	app.Usage = "Minimalist Object Storage"
	app.Flags = flags
	app.Commands = commands
	app.Before = func(c *cli.Context) error {
		if c.GlobalBool("debug") {
			app.ExtraInfo = getSystemData()
		}
		return nil
	}
	app.CustomAppHelpTemplate = minioHelpTemplate
	app.RunAndExitOnError()
}
<|endoftext|>


<commit_before>package main

import (
	"encoding/binary"
	"fmt"
	"net"
	"os"
	"unsafe"

	bpf "github.com/kinvolk/go-ebpf-kprobe-example/bpf"
)

/*
#cgo CFLAGS: -Wall -Wno-unused-variable
#cgo LDFLAGS: -lelf

#include <stdlib.h>
#include <stdint.h>
#include <sys/ioctl.h>
#include <linux/perf_event.h>
#include <poll.h>
#include <errno.h>
#include <linux/bpf.h>

#define TASK_COMM_LEN 16

struct tcp_event_t {
    char ev_type[12];
    __u32 pid;
    char comm[TASK_COMM_LEN];
    __u32 saddr;
    __u32 daddr;
    __u16 sport;
    __u16 dport;
    __u32 netns;
};
*/
import "C"

//export tcpEventCb
func tcpEventCb(data []byte) {
	tcpEvent := (*C.struct_tcp_event_t)(unsafe.Pointer(&data[0]))

	typ := C.GoString(&tcpEvent.ev_type[0])
	pid := tcpEvent.pid & 0xffffffff

	saddrbuf := make([]byte, 4)
	daddrbuf := make([]byte, 4)

	binary.LittleEndian.PutUint32(saddrbuf, uint32(tcpEvent.saddr))
	binary.LittleEndian.PutUint32(daddrbuf, uint32(tcpEvent.daddr))

	sIP := net.IPv4(saddrbuf[0], saddrbuf[1], saddrbuf[2], saddrbuf[3])
	dIP := net.IPv4(daddrbuf[0], daddrbuf[1], daddrbuf[2], daddrbuf[3])

	sport := tcpEvent.sport
	dport := tcpEvent.dport
	netns := tcpEvent.netns

	fmt.Println(typ)
	fmt.Println(pid)
	fmt.Println(sIP)
	fmt.Println(dIP)
	fmt.Println(sport)
	fmt.Println(dport)
	fmt.Println(netns)
	fmt.Println()
}

func main() {
	fmt.Println("Ready.\n")

	b := bpf.NewBpfPerfEvent("kernel/trace_output_kern.o")

	err := b.Load()
	if err != nil {
		fmt.Fprintf(os.Stderr, "%v\n", err)
		os.Exit(1)
	}

	b.Poll("tcp_event", tcpEventCb)

	select {}
}
<commit_msg>Print tcpEvents in one line<commit_after>package main

import (
	"encoding/binary"
	"fmt"
	"net"
	"os"
	"unsafe"

	bpf "github.com/kinvolk/go-ebpf-kprobe-example/bpf"
)

/*
#cgo CFLAGS: -Wall -Wno-unused-variable

#include <stdlib.h>
#include <stdint.h>
//#include <sys/ioctl.h>
//#include <linux/perf_event.h>
#include <poll.h>
#include <errno.h>
//#include <linux/bpf.h>

#define TASK_COMM_LEN 16

struct tcp_event_t {
    char ev_type[12];
    uint32_t pid;
    char comm[TASK_COMM_LEN];
    uint32_t saddr;
    uint32_t daddr;
    uint16_t sport;
    uint16_t dport;
    uint32_t netns;
};
*/
import "C"

//export tcpEventCb
func tcpEventCb(data []byte) {
	tcpEvent := (*C.struct_tcp_event_t)(unsafe.Pointer(&data[0]))

	typ := C.GoString(&tcpEvent.ev_type[0])
	pid := tcpEvent.pid & 0xffffffff

	saddrbuf := make([]byte, 4)
	daddrbuf := make([]byte, 4)

	binary.LittleEndian.PutUint32(saddrbuf, uint32(tcpEvent.saddr))
	binary.LittleEndian.PutUint32(daddrbuf, uint32(tcpEvent.daddr))

	sIP := net.IPv4(saddrbuf[0], saddrbuf[1], saddrbuf[2], saddrbuf[3])
	dIP := net.IPv4(daddrbuf[0], daddrbuf[1], daddrbuf[2], daddrbuf[3])

	sport := tcpEvent.sport
	dport := tcpEvent.dport
	netns := tcpEvent.netns

	fmt.Printf("%s %v %v:%v %v:%v %v\n", typ, pid, sIP, sport, dIP, dport, netns)
}

func main() {
	b := bpf.NewBpfPerfEvent("kernel/trace_output_kern.o")

	err := b.Load()
	if err != nil {
		fmt.Fprintf(os.Stderr, "%v\n", err)
		os.Exit(1)
	}

	fmt.Printf("Ready.\n")
	b.Poll("tcp_event", tcpEventCb)

	select {}
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"os"

	"github.com/goodmustache/pt/commands"
	flags "github.com/jessevdk/go-flags"
)

func main() {
	parser := flags.NewParser(&commands.PT, flags.HelpFlag)

	_, err := parser.Parse()
	if err != nil {
		fmt.Fprintf(os.Stderr, err.Error())
		os.Exit(1)
	}
}
<commit_msg>Add new line for main command if error<commit_after>package main

import (
	"fmt"
	"os"

	"github.com/goodmustache/pt/commands"
	flags "github.com/jessevdk/go-flags"
)

func main() {
	parser := flags.NewParser(&commands.PT, flags.HelpFlag)

	_, err := parser.Parse()
	if err != nil {
		fmt.Fprintln(os.Stderr, err.Error())
		os.Exit(1)
	}
}
<|endoftext|>


<commit_before>package main

import (
	"encoding/json"
	"fmt"
	"net/http"

	log "github.com/Sirupsen/logrus"
)

func main() {
	c, err := NewContext()
	if err != nil {
		log.WithFields(log.Fields{
			"error": err,
		}).Fatal("Unable to load application context.")
		return
	}

	log.WithFields(log.Fields{
		"address": c.ListenAddr(),
	}).Info("Auth API listening.")

	// v1 routes
	http.HandleFunc("/v1/style", BindContext(c, StyleHandler))
	http.HandleFunc("/v1/validate", BindContext(c, ValidateHandler))

	http.HandleFunc("/v1/accounts", BindContext(c, AccountHandler))
	http.HandleFunc("/v1/keys", BindContext(c, KeyHandler))

	err = http.ListenAndServeTLS(c.ListenAddr(), c.Cert, c.Key, nil)
	if err != nil {
		log.WithFields(log.Fields{
			"error": err,
		}).Error("Unable to launch auth API.")
	}
}

// ContextHandler is an HTTP HandlerFunc that accepts an additional parameter containing the
// server context.
type ContextHandler func(c *Context, w http.ResponseWriter, r *http.Request)

// BindContext returns an http.HandlerFunc that binds a ContextHandler to a specific Context.
func BindContext(c *Context, handler ContextHandler) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) { handler(c, w, r) }
}

// APIError consistently renders error conditions as a JSON payload.
type APIError struct {
	// If nonzero, this message will be displayed to the user in the generated response payload.
	UserMessage string `json:"message"`

	// If nonzero, this message will be displayed to operators in the process log.
	LogMessage string `json:"-"`

	// Used as both UserMessage and LogMessage if either are missing.
	Message string `json:"-"`
}

// Log emits a log message for an error.
func (err APIError) Log(username string) APIError {
	if err.LogMessage == "" {
		err.LogMessage = err.Message
	}

	f := log.Fields{}
	if username != "" {
		f["username"] = username
	}
	log.WithFields(f).Error(err.LogMessage)
	return err
}

// Report renders an error as an HTTP response with the correct content-type and HTTP status code.
func (err APIError) Report(w http.ResponseWriter, status int) APIError {
	if err.UserMessage == "" {
		err.UserMessage = err.Message
	}

	w.WriteHeader(status)
	w.Header().Set("Content-Type", "application/json")

	encodeErr := json.NewEncoder(w).Encode(err)
	if encodeErr != nil {
		fmt.Fprintf(w, `{"message":"Unable to encode error: %v"}`, encodeErr)
	}
	return err
}

// MethodOk tests the HTTP request method. If the method is correct, it does nothing and
// returns true. If it's incorrect, it generates a JSON error and returns false.
func MethodOk(w http.ResponseWriter, r *http.Request, method string) bool {
	if r.Method == method {
		return true
	}

	APIError{
		Message: fmt.Sprintf("Unsupported method %s. Only %s is accepted for this resource.",
			r.Method, method),
	}.Log("").Report(w, http.StatusMethodNotAllowed)

	return false
}
<commit_msg>Extract a function to standardize accountName/apiKey.<commit_after>package main

import (
	"encoding/json"
	"fmt"
	"net/http"

	log "github.com/Sirupsen/logrus"
)

func main() {
	c, err := NewContext()
	if err != nil {
		log.WithFields(log.Fields{
			"error": err,
		}).Fatal("Unable to load application context.")
		return
	}

	log.WithFields(log.Fields{
		"address": c.ListenAddr(),
	}).Info("Auth API listening.")

	// v1 routes
	http.HandleFunc("/v1/style", BindContext(c, StyleHandler))
	http.HandleFunc("/v1/validate", BindContext(c, ValidateHandler))

	http.HandleFunc("/v1/accounts", BindContext(c, AccountHandler))
	http.HandleFunc("/v1/keys", BindContext(c, KeyHandler))

	err = http.ListenAndServeTLS(c.ListenAddr(), c.Cert, c.Key, nil)
	if err != nil {
		log.WithFields(log.Fields{
			"error": err,
		}).Error("Unable to launch auth API.")
	}
}

// ContextHandler is an HTTP HandlerFunc that accepts an additional parameter containing the
// server context.
type ContextHandler func(c *Context, w http.ResponseWriter, r *http.Request)

// BindContext returns an http.HandlerFunc that binds a ContextHandler to a specific Context.
func BindContext(c *Context, handler ContextHandler) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) { handler(c, w, r) }
}

// APIError consistently renders error conditions as a JSON payload.
type APIError struct {
	// If nonzero, this message will be displayed to the user in the generated response payload.
	UserMessage string `json:"message"`

	// If nonzero, this message will be displayed to operators in the process log.
	LogMessage string `json:"-"`

	// Used as both UserMessage and LogMessage if either are missing.
	Message string `json:"-"`
}

// Log emits a log message for an error.
func (err APIError) Log(username string) APIError {
	if err.LogMessage == "" {
		err.LogMessage = err.Message
	}

	f := log.Fields{}
	if username != "" {
		f["username"] = username
	}
	log.WithFields(f).Error(err.LogMessage)
	return err
}

// Report renders an error as an HTTP response with the correct content-type and HTTP status code.
func (err APIError) Report(w http.ResponseWriter, status int) APIError {
	if err.UserMessage == "" {
		err.UserMessage = err.Message
	}

	w.WriteHeader(status)
	w.Header().Set("Content-Type", "application/json")

	encodeErr := json.NewEncoder(w).Encode(err)
	if encodeErr != nil {
		fmt.Fprintf(w, `{"message":"Unable to encode error: %v"}`, encodeErr)
	}
	return err
}

// MethodOk tests the HTTP request method. If the method is correct, it does nothing and
// returns true. If it's incorrect, it generates a JSON error and returns false.
func MethodOk(w http.ResponseWriter, r *http.Request, method string) bool {
	if r.Method == method {
		return true
	}

	APIError{
		Message: fmt.Sprintf("Unsupported method %s. Only %s is accepted for this resource.",
			r.Method, method),
	}.Log("").Report(w, http.StatusMethodNotAllowed)

	return false
}

// ExtractCredentials attempts to read an account name and API key from the request.
func ExtractCredentials(w http.ResponseWriter, r *http.Request) (accountName, apiKey string, ok bool) {
	if err := r.ParseForm(); err != nil {
		APIError{
			Message: fmt.Sprintf("Unable to parse URL parameters: %v", err),
		}.Log("").Report(w, http.StatusBadRequest)
		return "", "", false
	}

	accountName, apiKey = r.FormValue("accountName"), r.FormValue("apiKey")
	if accountName == "" || apiKey == "" {
		APIError{
			UserMessage: `Missing required query parameters "accountName" and "apiKey".`,
			LogMessage:  "Key validation request missing required query parameters.",
		}.Log("").Report(w, http.StatusBadRequest)
		return "", "", false
	}
	return accountName, apiKey, true
}
<|endoftext|>