<commit_before>package main

import (
	"fmt"
	"net/http"
	"strings"

	"github.com/facebookgo/grace/gracehttp"
	"github.com/facebookgo/pidfile"
	"github.com/gin-gonic/gin"

	"github.com/eirka/eirka-libs/config"
	"github.com/eirka/eirka-libs/cors"
	"github.com/eirka/eirka-libs/datadog"
	"github.com/eirka/eirka-libs/db"
	"github.com/eirka/eirka-libs/redis"
	"github.com/eirka/eirka-libs/status"
	"github.com/eirka/eirka-libs/user"
	"github.com/eirka/eirka-libs/validate"

	local "github.com/eirka/eirka-get/config"
	c "github.com/eirka/eirka-get/controllers"
	m "github.com/eirka/eirka-get/middleware"
)

func init() {

	var err error

	// create pid file
	pidfile.SetPidfilePath("/run/eirka/eirka-get.pid")

	err = pidfile.Write()
	if err != nil {
		panic("Could not write pid file")
	}

	// Database connection settings
	dbase := db.Database{

		User:           local.Settings.Database.User,
		Password:       local.Settings.Database.Password,
		Proto:          local.Settings.Database.Protocol,
		Host:           local.Settings.Database.Host,
		Database:       local.Settings.Database.Database,
		MaxIdle:        local.Settings.Get.DatabaseMaxIdle,
		MaxConnections: local.Settings.Get.DatabaseMaxConnections,
	}

	// Set up DB connection
	dbase.NewDb()

	// Get limits and stuff from database
	config.GetDatabaseSettings()

	// redis settings
	r := redis.Redis{
		// Redis address and max pool connections
		Protocol:       local.Settings.Redis.Protocol,
		Address:        local.Settings.Redis.Host,
		MaxIdle:        local.Settings.Get.RedisMaxIdle,
		MaxConnections: local.Settings.Get.RedisMaxConnections,
	}

	// Set up Redis connection
	r.NewRedisCache()

	// set auth middleware secret
	user.Secret = local.Settings.Session.Secret

	// set cors domains
	cors.SetDomains(local.Settings.CORS.Sites, strings.Split("GET", ","))

	// initialize datadog client
	err = datadog.New()
	if err != nil {
		panic("Could not initialize the dog")
	}

	// client namespace base
	datadog.Client.Namespace = "eirka.get."

}

func main() {
	r := gin.Default()

	// add CORS headers
	r.Use(cors.CORS())
	// validate all route parameters
	r.Use(validate.ValidateParams())

	r.GET("/status", status.StatusController)
	r.NoRoute(c.ErrorController)

	// public cached pages
	public := r.Group("/")
	public.Use(user.Auth(false))
	// send statistics to statsd
	public.Use(m.DataDog())
	public.Use(m.Analytics())
	public.Use(m.Cache())

	public.GET("/index/:ib/:page", c.IndexController)
	public.GET("/thread/:ib/:thread/:page", c.ThreadController)
	public.GET("/tag/:ib/:tag/:page", c.TagController)
	public.GET("/image/:ib/:id", c.ImageController)
	public.GET("/image/:ib/random", c.RandomController)
	public.GET("/post/:ib/:thread/:id", c.PostController)
	public.GET("/tags/:ib/:page", c.TagsController)
	public.GET("/tagsearch/:ib", c.TagSearchController)
	public.GET("/threadsearch/:ib", c.ThreadSearchController)
	public.GET("/directory/:ib/:page", c.DirectoryController)
	public.GET("/popular/:ib", c.PopularController)
	public.GET("/new/:ib", c.NewController)
	public.GET("/favorited/:ib", c.FavoritedController)
	public.GET("/tagtypes", c.TagTypesController)
	public.GET("/imageboards", c.ImageboardsController)
	public.GET("/whoami/:ib", c.WhoAmIController)

	// user pages
	users := r.Group("/user")
	users.Use(user.Auth(true))

	users.GET("/favorite/:id", c.FavoriteController)
	users.GET("/favorites/:ib/:page", c.FavoritesController)

	s := &http.Server{
		Addr:    fmt.Sprintf("%s:%d", local.Settings.Get.Host, local.Settings.Get.Port),
		Handler: r,
	}

	gracehttp.Serve(s)

}
<commit_msg>add random image handler<commit_after>package main

import (
	"fmt"
	"net/http"
	"strings"

	"github.com/facebookgo/grace/gracehttp"
	"github.com/facebookgo/pidfile"
	"github.com/gin-gonic/gin"

	"github.com/eirka/eirka-libs/config"
	"github.com/eirka/eirka-libs/cors"
	"github.com/eirka/eirka-libs/datadog"
	"github.com/eirka/eirka-libs/db"
	"github.com/eirka/eirka-libs/redis"
	"github.com/eirka/eirka-libs/status"
	"github.com/eirka/eirka-libs/user"
	"github.com/eirka/eirka-libs/validate"

	local "github.com/eirka/eirka-get/config"
	c "github.com/eirka/eirka-get/controllers"
	m "github.com/eirka/eirka-get/middleware"
)

func init() {

	var err error

	// create pid file
	pidfile.SetPidfilePath("/run/eirka/eirka-get.pid")

	err = pidfile.Write()
	if err != nil {
		panic("Could not write pid file")
	}

	// Database connection settings
	dbase := db.Database{

		User:           local.Settings.Database.User,
		Password:       local.Settings.Database.Password,
		Proto:          local.Settings.Database.Protocol,
		Host:           local.Settings.Database.Host,
		Database:       local.Settings.Database.Database,
		MaxIdle:        local.Settings.Get.DatabaseMaxIdle,
		MaxConnections: local.Settings.Get.DatabaseMaxConnections,
	}

	// Set up DB connection
	dbase.NewDb()

	// Get limits and stuff from database
	config.GetDatabaseSettings()

	// redis settings
	r := redis.Redis{
		// Redis address and max pool connections
		Protocol:       local.Settings.Redis.Protocol,
		Address:        local.Settings.Redis.Host,
		MaxIdle:        local.Settings.Get.RedisMaxIdle,
		MaxConnections: local.Settings.Get.RedisMaxConnections,
	}

	// Set up Redis connection
	r.NewRedisCache()

	// set auth middleware secret
	user.Secret = local.Settings.Session.Secret

	// set cors domains
	cors.SetDomains(local.Settings.CORS.Sites, strings.Split("GET", ","))

	// initialize datadog client
	err = datadog.New()
	if err != nil {
		panic("Could not initialize the dog")
	}

	// client namespace base
	datadog.Client.Namespace = "eirka.get."

}

func main() {
	r := gin.Default()

	// add CORS headers
	r.Use(cors.CORS())
	// validate all route parameters
	r.Use(validate.ValidateParams())

	r.GET("/status", status.StatusController)
	r.NoRoute(c.ErrorController)

	// public cached pages
	public := r.Group("/")
	public.Use(user.Auth(false))
	// send statistics to statsd
	public.Use(m.DataDog())
	public.Use(m.Analytics())
	public.Use(m.Cache())

	public.GET("/index/:ib/:page", c.IndexController)
	public.GET("/thread/:ib/:thread/:page", c.ThreadController)
	public.GET("/tag/:ib/:tag/:page", c.TagController)
	public.GET("/image/:ib/:id", c.ImageController)
	public.GET("/image/random/:ib", c.RandomController)
	public.GET("/post/:ib/:thread/:id", c.PostController)
	public.GET("/tags/:ib/:page", c.TagsController)
	public.GET("/tagsearch/:ib", c.TagSearchController)
	public.GET("/threadsearch/:ib", c.ThreadSearchController)
	public.GET("/directory/:ib/:page", c.DirectoryController)
	public.GET("/popular/:ib", c.PopularController)
	public.GET("/new/:ib", c.NewController)
	public.GET("/favorited/:ib", c.FavoritedController)
	public.GET("/tagtypes", c.TagTypesController)
	public.GET("/imageboards", c.ImageboardsController)
	public.GET("/whoami/:ib", c.WhoAmIController)

	// user pages
	users := r.Group("/user")
	users.Use(user.Auth(true))

	users.GET("/favorite/:id", c.FavoriteController)
	users.GET("/favorites/:ib/:page", c.FavoritesController)

	s := &http.Server{
		Addr:    fmt.Sprintf("%s:%d", local.Settings.Get.Host, local.Settings.Get.Port),
		Handler: r,
	}

	gracehttp.Serve(s)

}
<|endoftext|>


<commit_before>package main

import (
	"browserusage/webserver"
	"browserusage/dao"
	"flag"
)

var (
	port *int  = flag.Int("port", 9050, "Port number")
)

func main() {
	dao.Init()
	flag.Parse()
	webserver.Start(*port)
}<commit_msg>go fmt<commit_after>package main

import (
	"browserusage/dao"
	"browserusage/webserver"
	"flag"
)

var (
	port *int = flag.Int("port", 9050, "Port number")
)

func main() {
	dao.Init()
	flag.Parse()
	webserver.Start(*port)
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"fmt"
	"github.com/ConSol/sakuli-go-wrapper/execute"
	"github.com/ConSol/sakuli-go-wrapper/helper"
	"github.com/ConSol/sakuli-go-wrapper/input"
	"os"
	"path/filepath"
	"time"
)

func main() {
	var loop int
	var javaHome string
	var javaProperties input.StringSlice
	var javaOptions input.StringSlice
	var preHooks input.StringSlice
	var postHooks input.StringSlice
	var browser string
	var inter string
	var sahiHome string
	var version bool

	sakuliJars := filepath.Join(helper.GetSahiHome(), "libs", "java")
	myFlagSet := flag.NewFlagSet("", flag.ExitOnError)
	input.MyFlagSet = myFlagSet
	myFlagSet.Usage = func() {
		fmt.Fprintf(os.Stderr, `Generic Sakuli test starter.
%d - The Sakuli team <sakuli@consol.de>
http://www.sakuli.org
https://github.com/ConSol/sakuli

Usage: sakuli[.exe] COMMAND ARGUMENT [OPTIONS]

       sakuli -help
       sakuli -version
       sakuli run <sakuli suite path> [OPTIONS]
       sakuli encrypt <secret> [OPTIONS]

Commands:
       run 	   <sakuli suite path>
       encrypt 	   <secret>

Options:
       -loop	   <seconds>	  Loop this suite, wait n seconds between
                                  executions, 0 means no loops (default: 0)
       -javaHome   <folder>       Java bin dir (overrides PATH)
       -javaOption <java option>  JVM option parameter, e.g. '-agentlib:...'
       -preHook    <programpath>  A program which will be executed before a
                                  suite run (can be added multiple times)
       -postHook   <programpath>  A program which will be executed after a
                                  suite run (can be added multiple times)
       -D 	   <JVM option>   JVM option to set a property at runtime,
                                  overrides file based properties
       -browser    <browser>      Browser for the test execution
                                  (default: Firefox)
       -interface  <interface>    Network interface card name, used by
                                  command 'encrypt' as salt
       -sahiHome   <folder>       Sahi installation folder
       -version                   Version info
       -help                      This help text

Examples:
    * Run the test suite "example_windows":
    sakuli run "%SAKULI_HOME%\..\example_test_suites\example_windows"
    * Run "example_windows" in an infinite loop with 10 seconds pause between:
    sakuli run "%SAKULI_HOME%\..\example_test_suites\example_windows" -loop=10
    * Run "example_windows" with browser "chrome" (browser must be registered):
    sakuli run "%SAKULI_HOME%\..\example_test_suites\example_windows" -browser=chrome
    * Run "example_windows", kill hanging processes before:
    sakuli run "%SAKULI_HOME%\..\example_test_suites\example_windows" \
      -preHook='cscript.exe %SAKULI_HOME%\bin\helper\killproc.vbs     \
      -f %SAKULI_HOME%\bin\helper\procs_to_kill.txt'
    * Run "exmaple_windows", increase the logging level:
    sakuli run "%SAKULI_HOME%\..\example_test_suites\example_windows" \
      -D log.level.sakuli=DEBUG

    * Encrypt a secret using eth0 as salt NIC:
    sakuli encrypt topsecret -interface eth0
    * Show interfaces available for encryption:
    sakuli encrypt topsecret -interface list

    * Show version (use this information when submitting bugs):
    sakuli -version

`, time.Now().Year())
	}

	myFlagSet.IntVar(&loop, "loop", 0, "loop this suite, wait n seconds between executions, 0 means no loops (default: 0)")
	myFlagSet.StringVar(&javaHome, "javaHome", "", "Java bin dir (overrides PATH)")
	myFlagSet.Var(&preHooks, "preHook", "A program which will be executed before a suite run (can be added multiple times)")
	myFlagSet.Var(&postHooks, "postHook", "A program which will be executed after a suite run (can be added multiple times)")

	myFlagSet.Var(&javaProperties, "D", "JVM option to set a property at runtime, overrides file based properties")
	myFlagSet.Var(&javaOptions, "javaOption", "JVM option parameter, e.g. '-agentlib:...'")
	myFlagSet.StringVar(&browser, "browser", "", "browser for the test execution (default: Firefox)")
	myFlagSet.StringVar(&inter, "interface", "", "network interface icaed name, used by command 'encrypt' as salt")
	myFlagSet.StringVar(&sahiHome, "sahi_home", "", "Sahi installation folder")
	myFlagSet.BoolVar(&version, "version", false, "version info")

	if len(os.Args) > 2 {
		myFlagSet.Parse(os.Args[3:])
	} else {
		myFlagSet.Parse(os.Args[1:])
		if version {
			input.PrintVersion()
		}
		input.ExitWithHelp("\nOnly 'sakuli COMMAND ARGUMENT [OPTIONS]' is allowed, given: " + fmt.Sprint(os.Args))
	}

	sakuliProperties := map[string]string{"sakuli_home": helper.GetSahiHome()}
	typ, argument := input.ParseArgs(append(os.Args[1:3],myFlagSet.Args()...))
	switch typ {
	case input.RunMode:
		input.TestRun(argument)
		sakuliProperties[input.RunMode] = argument
	case input.EncryptMode:
		sakuliProperties[input.EncryptMode] = argument
	case input.Error:
		panic("can't pars args")
	}

	javaExecutable := input.TestJavaHome(javaHome)
	javaProperties = javaProperties.AddPrefix("-D")

	if browser != "" {
		sakuliProperties["browser"] = browser
	}
	if inter != "" {
		sakuliProperties["interface"] = inter
	}
	if sahiHome != "" {
		sakuliProperties["sahiHome"] = sahiHome
	}
	joinedSakuliProperties := genSakuliPropertiesList(sakuliProperties)

	if len(preHooks) > 0 {
		fmt.Println("=========== Starting Pre-Hooks ===========")
		for _, pre := range preHooks {
			execute.RunHandler(pre)
		}
		fmt.Println("=========== Finished Pre-Hooks ===========")
	}

	sakuliReturnCode := execute.RunSakuli(javaExecutable, sakuliJars, javaOptions, javaProperties, joinedSakuliProperties)
	for loop > 0 {
		fmt.Printf("*** Loop mode - sleeping for %d seconds... ***\n", loop)
		time.Sleep(time.Duration(loop) * time.Second)
		execute.RunSakuli(javaExecutable, sakuliJars, javaOptions, javaProperties, joinedSakuliProperties)
	}

	if len(postHooks) > 0 {
		fmt.Println("=========== Starting Post-Hooks ===========")
		for _, post := range postHooks {
			execute.RunHandler(post)
		}
		fmt.Println("=========== Finished Post-Hooks ===========")
	}
	os.Exit(sakuliReturnCode)
}

func genSakuliPropertiesList(properties map[string]string) input.StringSlice {
	propertiesString := []string{}
	for k, v := range properties {
		propertiesString = append(propertiesString, fmt.Sprintf("--%s", k))
		propertiesString = append(propertiesString, v)
	}
	return propertiesString
}
<commit_msg>Fixed string formatting in golang help<commit_after>package main

import (
	"flag"
	"fmt"
	"github.com/ConSol/sakuli-go-wrapper/execute"
	"github.com/ConSol/sakuli-go-wrapper/helper"
	"github.com/ConSol/sakuli-go-wrapper/input"
	"os"
	"path/filepath"
	"time"
)

func main() {
	var loop int
	var javaHome string
	var javaProperties input.StringSlice
	var javaOptions input.StringSlice
	var preHooks input.StringSlice
	var postHooks input.StringSlice
	var browser string
	var inter string
	var sahiHome string
	var version bool

	sakuliJars := filepath.Join(helper.GetSahiHome(), "libs", "java")
	myFlagSet := flag.NewFlagSet("", flag.ExitOnError)
	input.MyFlagSet = myFlagSet
	myFlagSet.Usage = func() {
		fmt.Fprintf(os.Stderr, `Generic Sakuli test starter.
%d - The Sakuli team <sakuli@consol.de>
http://www.sakuli.org
https://github.com/ConSol/sakuli

Usage: sakuli[.exe] COMMAND ARGUMENT [OPTIONS]

       sakuli -help
       sakuli -version
       sakuli run <sakuli suite path> [OPTIONS]
       sakuli encrypt <secret> [OPTIONS]

Commands:
       run 	   <sakuli suite path>
       encrypt 	   <secret>

Options:
       -loop	   <seconds>	  Loop this suite, wait n seconds between
                                  executions, 0 means no loops (default: 0)
       -javaHome   <folder>       Java bin dir (overrides PATH)
       -javaOption <java option>  JVM option parameter, e.g. '-agentlib:...'
       -preHook    <programpath>  A program which will be executed before a
                                  suite run (can be added multiple times)
       -postHook   <programpath>  A program which will be executed after a
                                  suite run (can be added multiple times)
       -D 	   <JVM option>   JVM option to set a property at runtime,
                                  overrides file based properties
       -browser    <browser>      Browser for the test execution
                                  (default: Firefox)
       -interface  <interface>    Network interface card name, used by
                                  command 'encrypt' as salt
       -sahiHome   <folder>       Sahi installation folder
       -version                   Version info
       -help                      This help text

Examples:
    * Run the test suite "example":
    sakuli run "$SAKULI_HOME\..\suites\example"
    * Run "example" in an infinite loop with 10 seconds pause between:
    sakuli run "$SAKULI_HOME\..\suites\example" -loop=10
    * Run "example" with browser "chrome" (browser must be registered):
    sakuli run "$SAKULI_HOME\..\suites\example" -browser=chrome
    * Run "example", kill hanging processes before:
    sakuli run "$SAKULI_HOME\..\suites\example" \
      -preHook='cscript.exe $SAKULI_HOME\bin\helper\killproc.vbs     \
      -f $SAKULI_HOME\bin\helper\procs_to_kill.txt'
    * Run "exmaple_windows", increase the logging level:
    sakuli run "$SAKULI_HOME\..\suites\example" -D log.level.sakuli=DEBUG

    * Encrypt a secret using eth0 as salt NIC:
    sakuli encrypt topsecret -interface eth0
    * Show interfaces available for encryption:
    sakuli encrypt topsecret -interface list

    * Show version (use this information when submitting bugs):
    sakuli -version

`, time.Now().Year())
	}

	myFlagSet.IntVar(&loop, "loop", 0, "loop this suite, wait n seconds between executions, 0 means no loops (default: 0)")
	myFlagSet.StringVar(&javaHome, "javaHome", "", "Java bin dir (overrides PATH)")
	myFlagSet.Var(&preHooks, "preHook", "A program which will be executed before a suite run (can be added multiple times)")
	myFlagSet.Var(&postHooks, "postHook", "A program which will be executed after a suite run (can be added multiple times)")

	myFlagSet.Var(&javaProperties, "D", "JVM option to set a property at runtime, overrides file based properties")
	myFlagSet.Var(&javaOptions, "javaOption", "JVM option parameter, e.g. '-agentlib:...'")
	myFlagSet.StringVar(&browser, "browser", "", "browser for the test execution (default: Firefox)")
	myFlagSet.StringVar(&inter, "interface", "", "network interface icaed name, used by command 'encrypt' as salt")
	myFlagSet.StringVar(&sahiHome, "sahi_home", "", "Sahi installation folder")
	myFlagSet.BoolVar(&version, "version", false, "version info")

	if len(os.Args) > 2 {
		myFlagSet.Parse(os.Args[3:])
	} else {
		myFlagSet.Parse(os.Args[1:])
		if version {
			input.PrintVersion()
		}
		input.ExitWithHelp("\nOnly 'sakuli COMMAND ARGUMENT [OPTIONS]' is allowed, given: " + fmt.Sprint(os.Args))
	}

	sakuliProperties := map[string]string{"sakuli_home": helper.GetSahiHome()}
	typ, argument := input.ParseArgs(append(os.Args[1:3],myFlagSet.Args()...))
	switch typ {
	case input.RunMode:
		input.TestRun(argument)
		sakuliProperties[input.RunMode] = argument
	case input.EncryptMode:
		sakuliProperties[input.EncryptMode] = argument
	case input.Error:
		panic("can't pars args")
	}

	javaExecutable := input.TestJavaHome(javaHome)
	javaProperties = javaProperties.AddPrefix("-D")

	if browser != "" {
		sakuliProperties["browser"] = browser
	}
	if inter != "" {
		sakuliProperties["interface"] = inter
	}
	if sahiHome != "" {
		sakuliProperties["sahiHome"] = sahiHome
	}
	joinedSakuliProperties := genSakuliPropertiesList(sakuliProperties)

	if len(preHooks) > 0 {
		fmt.Println("=========== Starting Pre-Hooks ===========")
		for _, pre := range preHooks {
			execute.RunHandler(pre)
		}
		fmt.Println("=========== Finished Pre-Hooks ===========")
	}

	sakuliReturnCode := execute.RunSakuli(javaExecutable, sakuliJars, javaOptions, javaProperties, joinedSakuliProperties)
	for loop > 0 {
		fmt.Printf("*** Loop mode - sleeping for %d seconds... ***\n", loop)
		time.Sleep(time.Duration(loop) * time.Second)
		execute.RunSakuli(javaExecutable, sakuliJars, javaOptions, javaProperties, joinedSakuliProperties)
	}

	if len(postHooks) > 0 {
		fmt.Println("=========== Starting Post-Hooks ===========")
		for _, post := range postHooks {
			execute.RunHandler(post)
		}
		fmt.Println("=========== Finished Post-Hooks ===========")
	}
	os.Exit(sakuliReturnCode)
}

func genSakuliPropertiesList(properties map[string]string) input.StringSlice {
	propertiesString := []string{}
	for k, v := range properties {
		propertiesString = append(propertiesString, fmt.Sprintf("--%s", k))
		propertiesString = append(propertiesString, v)
	}
	return propertiesString
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"fmt"
	"io/ioutil"
	"os"
	"path"
	"time"

	log "github.com/Sirupsen/logrus"
	"github.com/bitly/go-nsq"
	"github.com/crosbymichael/octokat"
)

const (
	VERSION = "v0.1.0"
)

var (
	lookupd string
	topic   string
	channel string
	bucket  string
	region  string
	debug   bool
	version bool
)

func init() {
	// parse flags
	flag.BoolVar(&version, "version", false, "print version and exit")
	flag.BoolVar(&version, "v", false, "print version and exit (shorthand)")
	flag.BoolVar(&debug, "d", false, "run in debug mode")
	flag.StringVar(&lookupd, "lookupd-addr", "nsqlookupd:4161", "nsq lookupd address")
	flag.StringVar(&topic, "topic", "hooks-docker", "nsq topic")
	flag.StringVar(&channel, "channel", "binaries", "nsq channel")
	flag.StringVar(&bucket, "s3bucket", "s3://master.dockerproject.com/", "s3 bucket to push binaries")
	flag.StringVar(&region, "s3region", "us-east-1", "s3 region where bucket lives")
	flag.Parse()
}

type Handler struct {
}

func (h *Handler) HandleMessage(m *nsq.Message) error {
	hook, err := octokat.ParseHook(m.Body)
	if err != nil {
		// Errors will most likely occur because not all GH
		// hooks are the same format
		// we care about those that are pushes to master
		log.Debugf("Error parsing hook: %v", err)
		return nil
	}

	shortSha := hook.After[0:7]
	// checkout the code in a temp dir
	temp, err := ioutil.TempDir("", fmt.Sprintf("commit-%s", shortSha))
	if err != nil {
		return err
	}
	defer os.RemoveAll(temp)

	if err := checkout(temp, hook.Repo.URL, hook.After); err != nil {
		log.Warn(err)
		return err
	}
	log.Debugf("Checked out %s for %s", hook.After, hook.Repo.URL)

	var (
		image     = fmt.Sprintf("docker:commit-%s", shortSha)
		container = fmt.Sprintf("build-%s", shortSha)
	)
	log.Infof("image=%s container=%s\n", image, container)

	// build the image
	if err := build(temp, image); err != nil {
		log.Warn(err)
		return err
	}
	log.Debugf("Successfully built image %s", image)

	// make the binary
	defer removeContainer(container)
	if err = makeBinary(temp, image, container, 20*time.Minute); err != nil {
		log.Warn(err)
		return err
	}
	log.Debugf("Successfully built binaries for %s", hook.After)

	// read the version
	version, err := getBinaryVersion(temp)
	if err != nil {
		log.Warnf("Getting binary version failed: %v", err)
		return err
	}

	bundlesPath := path.Join(temp, "bundles", version, "cross")

	// create commit file
	if err := ioutil.WriteFile(path.Join(bundlesPath, "commit"), []byte(hook.After), 0755); err != nil {
		return err
	}

	// create version file
	if err := ioutil.WriteFile(path.Join(bundlesPath, "version"), []byte(version), 0755); err != nil {
		return err
	}

	// push to s3
	if err = pushToS3(bundlesPath); err != nil {
		log.Warn(err)
		return err
	}
	return nil
}

func main() {
	// set log level
	if debug {
		log.SetLevel(log.DebugLevel)
	}

	if version {
		fmt.Println(VERSION)
		return
	}

	bb := &Handler{}
	if err := ProcessQueue(bb, QueueOptsFromContext(topic, channel, lookupd)); err != nil {
		log.Fatal(err)
	}
}
<commit_msg>fix<commit_after>package main

import (
	"flag"
	"fmt"
	"io/ioutil"
	"os"
	"path"
	"time"

	log "github.com/Sirupsen/logrus"
	"github.com/bitly/go-nsq"
	"github.com/drone/go-github/github"
)

const (
	VERSION = "v0.1.0"
)

var (
	lookupd string
	topic   string
	channel string
	bucket  string
	region  string
	debug   bool
	version bool
)

func init() {
	// parse flags
	flag.BoolVar(&version, "version", false, "print version and exit")
	flag.BoolVar(&version, "v", false, "print version and exit (shorthand)")
	flag.BoolVar(&debug, "d", false, "run in debug mode")
	flag.StringVar(&lookupd, "lookupd-addr", "nsqlookupd:4161", "nsq lookupd address")
	flag.StringVar(&topic, "topic", "hooks-docker", "nsq topic")
	flag.StringVar(&channel, "channel", "binaries", "nsq channel")
	flag.StringVar(&bucket, "s3bucket", "s3://master.dockerproject.com/", "s3 bucket to push binaries")
	flag.StringVar(&region, "s3region", "us-east-1", "s3 region where bucket lives")
	flag.Parse()
}

type Handler struct {
}

func (h *Handler) HandleMessage(m *nsq.Message) error {
	hook, err := github.ParseHook(m.Body)
	if err != nil {
		// Errors will most likely occur because not all GH
		// hooks are the same format
		// we care about those that are pushes to master
		log.Debugf("Error parsing hook: %v", err)
		return nil
	}

	shortSha := hook.After[0:7]
	// checkout the code in a temp dir
	temp, err := ioutil.TempDir("", fmt.Sprintf("commit-%s", shortSha))
	if err != nil {
		return err
	}
	defer os.RemoveAll(temp)

	if err := checkout(temp, hook.Repo.Url, hook.After); err != nil {
		log.Warn(err)
		return err
	}
	log.Debugf("Checked out %s for %s", hook.After, hook.Repo.Url)

	var (
		image     = fmt.Sprintf("docker:commit-%s", shortSha)
		container = fmt.Sprintf("build-%s", shortSha)
	)
	log.Infof("image=%s container=%s\n", image, container)

	// build the image
	if err := build(temp, image); err != nil {
		log.Warn(err)
		return err
	}
	log.Debugf("Successfully built image %s", image)

	// make the binary
	defer removeContainer(container)
	if err = makeBinary(temp, image, container, 20*time.Minute); err != nil {
		log.Warn(err)
		return err
	}
	log.Debugf("Successfully built binaries for %s", hook.After)

	// read the version
	version, err := getBinaryVersion(temp)
	if err != nil {
		log.Warnf("Getting binary version failed: %v", err)
		return err
	}

	bundlesPath := path.Join(temp, "bundles", version, "cross")

	// create commit file
	if err := ioutil.WriteFile(path.Join(bundlesPath, "commit"), []byte(hook.After), 0755); err != nil {
		return err
	}

	// create version file
	if err := ioutil.WriteFile(path.Join(bundlesPath, "version"), []byte(version), 0755); err != nil {
		return err
	}

	// push to s3
	if err = pushToS3(bundlesPath); err != nil {
		log.Warn(err)
		return err
	}
	return nil
}

func main() {
	// set log level
	if debug {
		log.SetLevel(log.DebugLevel)
	}

	if version {
		fmt.Println(VERSION)
		return
	}

	bb := &Handler{}
	if err := ProcessQueue(bb, QueueOptsFromContext(topic, channel, lookupd)); err != nil {
		log.Fatal(err)
	}
}
<|endoftext|>


<commit_before>package main

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

var config Configuration

func main() {
	log.Println("Starting legate...")
	defer func() {
		if r := recover(); r != nil {
			log.Fatalf("fatal error: %s", r)
		}
	}()

	config = LoadConfiguration("legate.yml")

	http.HandleFunc("/", forwardHandler)

	log.Printf("Listening: http://%s:%d\n", config.Bind, config.Port)
	http.ListenAndServe(fmt.Sprintf(":%d", config.Port), nil)
}
<commit_msg>print configuration information on start<commit_after>package main

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

var config Configuration

func main() {
	defer func() {
		if r := recover(); r != nil {
			log.Fatalf("fatal error: %s", r)
		}
	}()

	config = LoadConfiguration("legate.yml")

	http.HandleFunc("/", forwardHandler)

	config.Print()
	http.ListenAndServe(fmt.Sprintf(":%d", config.Port), nil)
}

func (c Configuration) Print() {
	log.Printf("Listening: http://%s:%d\n", config.Bind, config.Port)
	log.Printf("Consul:\n")
	log.Printf("  Address: %s\n", c.Consul.Address)
	log.Printf("  Datacenter: %s\n", c.Consul.Datacenter)
}<|endoftext|>


<commit_before>package main

import (
	"crypto/tls"
	"encoding/json"
	"flag"
	"fmt"
	"html/template"
	"io"
	"log"
	"mime"
	"net"
	"net/http"
	"net/url"
	"os"
	"os/user"
	"path"
	"path/filepath"
	"runtime"
	"strconv"
	"strings"
	"time"

	"golang.org/x/crypto/acme/autocert"
)

const version = "0.1"
const name = "simple-httpd"
const pathSeperator = "/"

var indexHTMLFiles = []string{
	"index.html",
	"index.htm",
}

const (
	cert    = "cert.pem"
	key     = "key.pem"
	certDir = ".autocert"
)

// gitSHA is populated at build time from
// `-ldflags "-X main.gitSHA=$(shell git rev-parse HEAD)"`
var gitSHA string

// Data holds the data passed to the template engine
type Data struct {
	Name         string
	LastModified string
	URI          string
	Size         int64
}

// httpServer holds the relavent info/state
type httpServer struct {
	Directory string
	Port      int
	TLSPort   int
	HTTPS     bool
	template  *template.Template
}

// requestData holds data about the request for logging
type requestData struct {
	Timestamp   string `json:"timestamp,omitempty"`
	Method      string `json:"method,omitempty"`
	HTTPVersion string `json:"http_version,omitempty"`
	RemoteAddr  string `json:"remote_addr,omitempty"`
	Path        string `json:"path,omitempty"`
	Status      int    `json:"status,omitempty"`
	UserAgent   string `json:"user_agent,omitempty"`
	Error       string `json:"error,omitempty,omitempty"`
}

func (r requestData) Format(f fmt.State, c rune) {
	switch c {
	case 'v', 's':
		enc := json.NewEncoder(f)
		enc.Encode(r)
	}
}

// setHeaders sets the base headers for all requests
func setHeaders(w http.ResponseWriter) {
	w.Header().Set("Server", name+pathSeperator+version)
	w.Header().Add("Date", time.Now().Format(time.RFC822))
}

func isIndexFile(file string) bool {
	for _, s := range indexHTMLFiles {
		if s == file {
			return true
		}
	}
	return false
}

// ServeHTTP handles inbound requests
func (h *httpServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	if h.HTTPS && req.TLS == nil {
		url := "https://" + strings.Split(req.Host, ":")[0]
		if h.TLSPort != 443 {
			url = url + ":" + strconv.FormatInt(int64(h.TLSPort), 10)
		}
		url += req.URL.String()
		http.Redirect(w, req, url, 302)
		return
	}
	defer func() {
		if err := recover(); err != nil {
			http.Error(w, fmt.Sprintln(err), http.StatusInternalServerError)
			log.Printf("recovering from error: %s\n", err)
		}
	}()

	rd := requestData{
		Timestamp:  time.Now().Format("2006-01-02 15:04:05"),
		RemoteAddr: req.RemoteAddr,
		Method:     req.Method,
		Path:       req.RequestURI,
		UserAgent:  req.UserAgent(),
	}

	parsedURL, err := url.Parse(req.RequestURI)
	if err != nil {
		rd.Error = err.Error()
		rd.Status = http.StatusInternalServerError
		fmt.Println(rd)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	escapedPath := parsedURL.EscapedPath()
	fullpath := filepath.Join(h.Directory, escapedPath[1:])

	file, err := os.Open(fullpath)
	if err != nil {
		rd.Error = err.Error()
		rd.Status = http.StatusNotFound
		fmt.Println(rd)
		http.NotFound(w, req)
		return
	}
	defer file.Close()

	stat, err := file.Stat()
	if err != nil {
		rd.Error = err.Error()
		rd.Status = http.StatusInternalServerError
		fmt.Println(rd)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	setHeaders(w)

	if stat.IsDir() {
		contents, err := file.Readdir(-1)
		if err != nil {
			rd.Status = http.StatusInternalServerError
			rd.Error = err.Error()
			fmt.Println(rd)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		files := make([]Data, 0, len(contents))
		for _, entry := range contents {
			if isIndexFile(entry.Name()) {
				w.Header().Set("Content-type", "text/html; charset=UTF-8")
				w.Header().Set("Content-Length", fmt.Sprintf("%v", entry.Size()))

				hf, err := os.Open(filepath.Join(fullpath, entry.Name()))
				if err != nil {
					fmt.Println(err)
					return
				}
				io.Copy(w, hf)

				rd.Status = http.StatusOK
				fmt.Println(rd)
				return
			}
			file := Data{
				Name:         entry.Name(),
				LastModified: entry.ModTime().Format(time.RFC1123),
				URI:          path.Join(escapedPath, entry.Name()),
			}
			if entry.IsDir() {
				file.Name = entry.Name() + pathSeperator
				file.Size = entry.Size()
			}
			files = append(files, file)
		}

		rd.Status = http.StatusOK

		w.Header().Set("Content-type", "text/html; charset=UTF-8")

		h.template.Execute(w, map[string]interface{}{
			"files":           files,
			"version":         gitSHA,
			"port":            h.Port,
			"relativePath":    escapedPath,
			"goVersion":       runtime.Version(),
			"parentDirectory": path.Dir(escapedPath),
		})

		fmt.Println(rd)

		return
	}

	if mimetype := mime.TypeByExtension(path.Ext(file.Name())); mimetype != "" {
		fmt.Println(mimetype)
		w.Header().Set("Content-type", mimetype)
	} else {
		w.Header().Set("Content-type", "application/octet-stream")
	}

	io.Copy(w, file)

	rd.Status = http.StatusOK
	fmt.Println(rd)
}

// keepAliveListener
type keepAliveListener struct {
	*net.TCPListener
}

// Accept
func (k keepAliveListener) Accept() (net.Conn, error) {
	tc, err := k.AcceptTCP()
	if err != nil {
		return nil, err
	}

	tc.SetKeepAlive(true)
	tc.SetKeepAlivePeriod(time.Minute * 3)

	return tc, nil
}

func getpwd() string {
	pwd, err := os.Getwd()
	if err != nil {
		log.Fatal(err)
	}

	return pwd
}

func homeDir() string {
	u, err := user.Current()
	if err != nil {
		return ""
	}

	return u.HomeDir
}

func main() {
	var port int
	var le string
	var gs bool
	var tlsPort int
	var tlsCert string
	var vers bool
	pwd := getpwd()

	flag.Usage = func() {
		w := os.Stderr
		for _, arg := range os.Args {
			if arg == "-?" || arg == "-h" {
				w = os.Stdout
				break
			}
		}
		fmt.Fprintf(w, "simple-httpd version: %s\n", name+pathSeperator+version)
		fmt.Fprintf(w, "Usage: simple-httpd [-p port] [-l domain]\n")
		fmt.Fprintf(w, "\n")
		fmt.Fprintf(w, "Examples: simple-httpd                        start server. http://localhost:8000\n")
		fmt.Fprintf(w, "      or: simple-httpd -p 80                  use HTTP port 80. http://localhost\n")
		fmt.Fprintf(w, "      or: simple-httpd -g                     enable HTTPS generated certificate. https://localhost:4433\n")
		fmt.Fprintf(w, "      or: simple-httpd -p 80 -l example.com   enable HTTPS with Let's Encrypt. https://example.com\n")
		fmt.Fprintf(w, "\n")
		fmt.Fprintf(w, "Options:\n")
		fmt.Fprintf(w, "  -?,-h        : this help\n")
		fmt.Fprintf(w, "  -v           : show version and exit\n")
		fmt.Fprintf(w, "  -g           : enable TLS/HTTPS generate and use a self signed certificate\n")
		fmt.Fprintf(w, "  -p port      : bind HTTP port (default: 8000)\n")
		fmt.Fprintf(w, "  -l domain    : enable TLS/HTTPS with Let's Encrypt for the given domain name.\n")
		fmt.Fprintf(w, "  -c path      : enable TLS/HTTPS use a predefined HTTPS certificate\n")
		fmt.Fprintf(w, "  -t port      : bind HTTPS port (default: 443, 4433 for -g)\n")
		fmt.Fprintf(w, "\n")
	}

	flag.BoolVar(&vers, "v", false, "")
	flag.IntVar(&port, "p", 8000, "")
	flag.StringVar(&le, "l", "", "")
	flag.StringVar(&tlsCert, "c", "", "")
	flag.BoolVar(&gs, "g", false, "")
	flag.IntVar(&tlsPort, "t", -1, "")
	flag.Parse()

	if vers {
		fmt.Fprintf(os.Stdout, "simple-httpd version: %s\n", name+pathSeperator+version)
		return
	}
	if tlsPort == -1 {
		if gs {
			tlsPort = 4433
		} else {
			tlsPort = 443
		}
	}
	h := &httpServer{
		Port:      port,
		TLSPort:   tlsPort,
		Directory: pwd,
		template:  template.Must(template.New("listing").Parse(htmlTemplate)),
	}

	if le != "" || tlsCert != "" || gs {
		h.HTTPS = true
		var tlsServer *http.Server
		var certPath, keyPath string
		switch {
		case tlsCert != "":
			if gs {
				log.Fatal("cannot specify both -tls-cert and -g")
			}
			certPath, keyPath = tlsCert, tlsCert // assume a single PEM format
		case gs:
			hd := homeDir()
			certPath = filepath.Join(hd, certDir, cert)
			keyPath = filepath.Join(hd, certDir, key)
			if err := generateCertificates(certPath, keyPath); err != nil {
				log.Fatalln(err)
			}
		default:
			if tlsPort != 443 {
				log.Fatal("invalid -tls-port. It must be 443 when LetsEncrypt is specified.")
			}
			cacheDir := filepath.Join(homeDir(), certDir)
			if err := os.MkdirAll(cacheDir, 0700); err != nil {
				log.Fatalf("could not create cache directory: %s" + err.Error())
			}
			certManager := autocert.Manager{
				Cache:      autocert.DirCache(cacheDir),
				Prompt:     autocert.AcceptTOS,
				HostPolicy: autocert.HostWhitelist(le),
			}
			tlsServer = &http.Server{
				Addr: fmt.Sprintf(":%d", tlsPort),
				TLSConfig: &tls.Config{
					GetCertificate: certManager.GetCertificate,
				},
				Handler: h,
			}
		}
		go func() {
			var err error
			if tlsServer == nil {
				err = http.ListenAndServeTLS(fmt.Sprintf("0.0.0.0:%d", tlsPort), certPath, keyPath, h)
			} else {
				err = tlsServer.ListenAndServeTLS("", "")
			}
			if err != nil {
				log.Fatal(err)
			}
		}()
		time.Sleep(time.Millisecond * 10) // give a little warmup time to the TLS
		fmt.Printf("Serving HTTP on 0.0.0.0 port %v, HTTPS on port %v...\n", h.Port, tlsPort)
	} else {
		go func() {
			time.Sleep(time.Millisecond * 10) // give a little warmup time to the HTTP
			fmt.Printf("Serving HTTP on 0.0.0.0 port %v ...\n", h.Port)
		}()
	}
	log.Fatal(http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", port), h))
}

const htmlTemplate = `
<!DOCTYPE html>

    <meta charset="utf-8">
    <title>simple-httpd</title>

Directory listing for {{.relativePath}}

        Name
		Last Modified
		Size

	    {{.parentDirectory}}

      {{range .files}}

	    {{.Name}}
		{{.LastModified}}
		{{.Size}}

      {{end}}

simple-httpd - {{.version}} / {{.goVersion}}

`
<commit_msg>remove unnecessary content type console output<commit_after>package main

import (
	"crypto/tls"
	"encoding/json"
	"flag"
	"fmt"
	"html/template"
	"io"
	"log"
	"mime"
	"net"
	"net/http"
	"net/url"
	"os"
	"os/user"
	"path"
	"path/filepath"
	"runtime"
	"strconv"
	"strings"
	"time"

	"golang.org/x/crypto/acme/autocert"
)

const version = "0.1"
const name = "simple-httpd"
const pathSeperator = "/"

var indexHTMLFiles = []string{
	"index.html",
	"index.htm",
}

const (
	cert    = "cert.pem"
	key     = "key.pem"
	certDir = ".autocert"
)

// gitSHA is populated at build time from
// `-ldflags "-X main.gitSHA=$(shell git rev-parse HEAD)"`
var gitSHA string

// Data holds the data passed to the template engine
type Data struct {
	Name         string
	LastModified string
	URI          string
	Size         int64
}

// httpServer holds the relavent info/state
type httpServer struct {
	Directory string
	Port      int
	TLSPort   int
	HTTPS     bool
	template  *template.Template
}

// requestData holds data about the request for logging
type requestData struct {
	Timestamp   string `json:"timestamp,omitempty"`
	Method      string `json:"method,omitempty"`
	HTTPVersion string `json:"http_version,omitempty"`
	RemoteAddr  string `json:"remote_addr,omitempty"`
	Path        string `json:"path,omitempty"`
	Status      int    `json:"status,omitempty"`
	UserAgent   string `json:"user_agent,omitempty"`
	Error       string `json:"error,omitempty,omitempty"`
}

func (r requestData) Format(f fmt.State, c rune) {
	switch c {
	case 'v', 's':
		enc := json.NewEncoder(f)
		enc.Encode(r)
	}
}

// setHeaders sets the base headers for all requests
func setHeaders(w http.ResponseWriter) {
	w.Header().Set("Server", name+pathSeperator+version)
	w.Header().Add("Date", time.Now().Format(time.RFC822))
}

func isIndexFile(file string) bool {
	for _, s := range indexHTMLFiles {
		if s == file {
			return true
		}
	}
	return false
}

// ServeHTTP handles inbound requests
func (h *httpServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	if h.HTTPS && req.TLS == nil {
		url := "https://" + strings.Split(req.Host, ":")[0]
		if h.TLSPort != 443 {
			url = url + ":" + strconv.FormatInt(int64(h.TLSPort), 10)
		}
		url += req.URL.String()
		http.Redirect(w, req, url, 302)
		return
	}
	defer func() {
		if err := recover(); err != nil {
			http.Error(w, fmt.Sprintln(err), http.StatusInternalServerError)
			log.Printf("recovering from error: %s\n", err)
		}
	}()

	rd := requestData{
		Timestamp:  time.Now().Format("2006-01-02 15:04:05"),
		RemoteAddr: req.RemoteAddr,
		Method:     req.Method,
		Path:       req.RequestURI,
		UserAgent:  req.UserAgent(),
	}

	parsedURL, err := url.Parse(req.RequestURI)
	if err != nil {
		rd.Error = err.Error()
		rd.Status = http.StatusInternalServerError
		fmt.Println(rd)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	escapedPath := parsedURL.EscapedPath()
	fullpath := filepath.Join(h.Directory, escapedPath[1:])

	file, err := os.Open(fullpath)
	if err != nil {
		rd.Error = err.Error()
		rd.Status = http.StatusNotFound
		fmt.Println(rd)
		http.NotFound(w, req)
		return
	}
	defer file.Close()

	stat, err := file.Stat()
	if err != nil {
		rd.Error = err.Error()
		rd.Status = http.StatusInternalServerError
		fmt.Println(rd)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	setHeaders(w)

	if stat.IsDir() {
		contents, err := file.Readdir(-1)
		if err != nil {
			rd.Status = http.StatusInternalServerError
			rd.Error = err.Error()
			fmt.Println(rd)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		files := make([]Data, 0, len(contents))
		for _, entry := range contents {
			if isIndexFile(entry.Name()) {
				w.Header().Set("Content-type", "text/html; charset=UTF-8")
				w.Header().Set("Content-Length", fmt.Sprintf("%v", entry.Size()))

				hf, err := os.Open(filepath.Join(fullpath, entry.Name()))
				if err != nil {
					fmt.Println(err)
					return
				}
				io.Copy(w, hf)

				rd.Status = http.StatusOK
				fmt.Println(rd)
				return
			}
			file := Data{
				Name:         entry.Name(),
				LastModified: entry.ModTime().Format(time.RFC1123),
				URI:          path.Join(escapedPath, entry.Name()),
			}
			if entry.IsDir() {
				file.Name = entry.Name() + pathSeperator
				file.Size = entry.Size()
			}
			files = append(files, file)
		}

		rd.Status = http.StatusOK

		w.Header().Set("Content-type", "text/html; charset=UTF-8")

		h.template.Execute(w, map[string]interface{}{
			"files":           files,
			"version":         gitSHA,
			"port":            h.Port,
			"relativePath":    escapedPath,
			"goVersion":       runtime.Version(),
			"parentDirectory": path.Dir(escapedPath),
		})

		fmt.Println(rd)

		return
	}

	if mimetype := mime.TypeByExtension(path.Ext(file.Name())); mimetype != "" {
		w.Header().Set("Content-type", mimetype)
	} else {
		w.Header().Set("Content-type", "application/octet-stream")
	}

	io.Copy(w, file)

	rd.Status = http.StatusOK
	fmt.Println(rd)
}

// keepAliveListener
type keepAliveListener struct {
	*net.TCPListener
}

// Accept
func (k keepAliveListener) Accept() (net.Conn, error) {
	tc, err := k.AcceptTCP()
	if err != nil {
		return nil, err
	}

	tc.SetKeepAlive(true)
	tc.SetKeepAlivePeriod(time.Minute * 3)

	return tc, nil
}

func getpwd() string {
	pwd, err := os.Getwd()
	if err != nil {
		log.Fatal(err)
	}

	return pwd
}

func homeDir() string {
	u, err := user.Current()
	if err != nil {
		return ""
	}

	return u.HomeDir
}

func main() {
	var port int
	var le string
	var gs bool
	var tlsPort int
	var tlsCert string
	var vers bool
	pwd := getpwd()

	flag.Usage = func() {
		w := os.Stderr
		for _, arg := range os.Args {
			if arg == "-?" || arg == "-h" {
				w = os.Stdout
				break
			}
		}
		fmt.Fprintf(w, "simple-httpd version: %s\n", name+pathSeperator+version)
		fmt.Fprintf(w, "Usage: simple-httpd [-p port] [-l domain]\n")
		fmt.Fprintf(w, "\n")
		fmt.Fprintf(w, "Examples: simple-httpd                        start server. http://localhost:8000\n")
		fmt.Fprintf(w, "      or: simple-httpd -p 80                  use HTTP port 80. http://localhost\n")
		fmt.Fprintf(w, "      or: simple-httpd -g                     enable HTTPS generated certificate. https://localhost:4433\n")
		fmt.Fprintf(w, "      or: simple-httpd -p 80 -l example.com   enable HTTPS with Let's Encrypt. https://example.com\n")
		fmt.Fprintf(w, "\n")
		fmt.Fprintf(w, "Options:\n")
		fmt.Fprintf(w, "  -?,-h        : this help\n")
		fmt.Fprintf(w, "  -v           : show version and exit\n")
		fmt.Fprintf(w, "  -g           : enable TLS/HTTPS generate and use a self signed certificate\n")
		fmt.Fprintf(w, "  -p port      : bind HTTP port (default: 8000)\n")
		fmt.Fprintf(w, "  -l domain    : enable TLS/HTTPS with Let's Encrypt for the given domain name.\n")
		fmt.Fprintf(w, "  -c path      : enable TLS/HTTPS use a predefined HTTPS certificate\n")
		fmt.Fprintf(w, "  -t port      : bind HTTPS port (default: 443, 4433 for -g)\n")
		fmt.Fprintf(w, "\n")
	}

	flag.BoolVar(&vers, "v", false, "")
	flag.IntVar(&port, "p", 8000, "")
	flag.StringVar(&le, "l", "", "")
	flag.StringVar(&tlsCert, "c", "", "")
	flag.BoolVar(&gs, "g", false, "")
	flag.IntVar(&tlsPort, "t", -1, "")
	flag.Parse()

	if vers {
		fmt.Fprintf(os.Stdout, "simple-httpd version: %s\n", name+pathSeperator+version)
		return
	}
	if tlsPort == -1 {
		if gs {
			tlsPort = 4433
		} else {
			tlsPort = 443
		}
	}
	h := &httpServer{
		Port:      port,
		TLSPort:   tlsPort,
		Directory: pwd,
		template:  template.Must(template.New("listing").Parse(htmlTemplate)),
	}

	if le != "" || tlsCert != "" || gs {
		h.HTTPS = true
		var tlsServer *http.Server
		var certPath, keyPath string
		switch {
		case tlsCert != "":
			if gs {
				log.Fatal("cannot specify both -tls-cert and -g")
			}
			certPath, keyPath = tlsCert, tlsCert // assume a single PEM format
		case gs:
			hd := homeDir()
			certPath = filepath.Join(hd, certDir, cert)
			keyPath = filepath.Join(hd, certDir, key)
			if err := generateCertificates(certPath, keyPath); err != nil {
				log.Fatalln(err)
			}
		default:
			if tlsPort != 443 {
				log.Fatal("invalid -tls-port. It must be 443 when LetsEncrypt is specified.")
			}
			cacheDir := filepath.Join(homeDir(), certDir)
			if err := os.MkdirAll(cacheDir, 0700); err != nil {
				log.Fatalf("could not create cache directory: %s" + err.Error())
			}
			certManager := autocert.Manager{
				Cache:      autocert.DirCache(cacheDir),
				Prompt:     autocert.AcceptTOS,
				HostPolicy: autocert.HostWhitelist(le),
			}
			tlsServer = &http.Server{
				Addr: fmt.Sprintf(":%d", tlsPort),
				TLSConfig: &tls.Config{
					GetCertificate: certManager.GetCertificate,
				},
				Handler: h,
			}
		}
		go func() {
			var err error
			if tlsServer == nil {
				err = http.ListenAndServeTLS(fmt.Sprintf("0.0.0.0:%d", tlsPort), certPath, keyPath, h)
			} else {
				err = tlsServer.ListenAndServeTLS("", "")
			}
			if err != nil {
				log.Fatal(err)
			}
		}()
		time.Sleep(time.Millisecond * 10) // give a little warmup time to the TLS
		fmt.Printf("Serving HTTP on 0.0.0.0 port %v, HTTPS on port %v...\n", h.Port, tlsPort)
	} else {
		go func() {
			time.Sleep(time.Millisecond * 10) // give a little warmup time to the HTTP
			fmt.Printf("Serving HTTP on 0.0.0.0 port %v ...\n", h.Port)
		}()
	}
	log.Fatal(http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", port), h))
}

const htmlTemplate = `
<!DOCTYPE html>

    <meta charset="utf-8">
    <title>simple-httpd</title>

Directory listing for {{.relativePath}}

        Name
		Last Modified
		Size

	    {{.parentDirectory}}

      {{range .files}}

	    {{.Name}}
		{{.LastModified}}
		{{.Size}}

      {{end}}

simple-httpd - {{.version}} / {{.goVersion}}

`
<|endoftext|>


<commit_before>//go:generate esc -o static.go -prefix static static

package main

import (
	"encoding/json"
	"flag"
	"github.com/fatih/structs"
	"github.com/nfnt/resize"
	"gopkg.in/yaml.v2"
	"image/jpeg"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"path/filepath"
	"sort"
	"sync"
	"time"
)

var photosMutex = &sync.Mutex{}
var photos = NewSyncMap()
var dirToMetadata = NewSyncMap()
var photoDir string
var cacheDir string

type metadata struct {
	Event        string
	Photographer string
	Date         string
	Location     string
	Directory    string `json:"-" structs:"directory"`
}

func handleFilters(w http.ResponseWriter, r *http.Request) {
	keys := []string{"Event", "Photographer", "Date", "Location"}

	filters := make(map[string]map[string]struct{})
	for _, k := range keys {
		filters[k] = make(map[string]struct{})
	}

	for meta := range dirToMetadata.Values() {
		m := structs.Map(meta)
		for _, k := range keys {
			if _, ok := m[k]; ok {
				filters[k][m[k].(string)] = struct{}{}
			}
		}
	}

	jFilters := make(map[string][]string)
	for _, k := range keys {
		jFilters[k] = make([]string, 0)
	}

	for i := range filters {
		for j := range filters[i] {
			jFilters[i] = append(jFilters[i], j)
		}
		sort.Strings(jFilters[i])
		if len(jFilters[i]) == 0 {
			delete(jFilters, i)
		}
	}

	w.Header().Set("Content-Type", "application/json")
	enc := json.NewEncoder(w)
	if err := enc.Encode(jFilters); err != nil {
		log.Print(err)
		return
	}
}

func handleFilter(w http.ResponseWriter, r *http.Request) {
	filterToEligible := make(map[string][]string)

	filters := make(map[string][]string)
	for filterKey := range r.URL.Query() {
		filters[filterKey] = r.URL.Query()[filterKey]
	}

	// find all directories with matching filters
	for filterKey, filterValues := range filters {
		for dir := range dirToMetadata.Keys() {
			meta := dirToMetadata.Get(dir)
			m := structs.Map(meta)
			for k, v := range m {
				if filterKey == k {
					for _, filterValue := range filterValues {
						if filterValue == v {
							filterToEligible[filterKey] = append(filterToEligible[filterKey], dir)
						}
					}
				}
			}
		}
	}

	// find intersection of all filter matches
	eligible := make(map[string]bool)

	for _, dirs := range filterToEligible {
		for _, dir := range dirs {
			count := 0
			for _, dirTests := range filterToEligible {
				for _, dirTest := range dirTests {
					if dir == dirTest {
						count += 1
					}
				}
			}
			if count == len(filterToEligible) {
				eligible[dir] = true
			}
		}
	}

	eligiblePhotos := make([]string, 0)
	for photo := range photos.Keys() {
		dir := filepath.Dir(photo)
		if _, ok := eligible[dir]; ok {
			eligiblePhotos = append(eligiblePhotos, photo)
		}
	}
	sort.Strings(eligiblePhotos)

	w.Header().Set("Content-Type", "application/json")
	enc := json.NewEncoder(w)
	if err := enc.Encode(eligiblePhotos); err != nil {
		log.Print(err)
		return
	}
}

func handlePhoto(w http.ResponseWriter, r *http.Request) {
	if filepath.Ext(r.URL.Path) != ".jpg" {
		http.Error(w, http.StatusText(404), 404)
		return
	}

	// serve photo
	photoFilepath := filepath.Join(photoDir, r.URL.Path[len("/photo/"):])
	http.ServeFile(w, r, photoFilepath)
}

func handleThumb(w http.ResponseWriter, r *http.Request) {
	if filepath.Ext(r.URL.Path) != ".jpg" {
		http.Error(w, http.StatusText(404), 404)
		return
	}

	photoFilepath := filepath.Join(photoDir, r.URL.Path[len("/cache/"):])
	cacheFilepath := filepath.Join(cacheDir, r.URL.Path[len("/cache/"):])

	if _, err := os.Stat(cacheFilepath); err != nil {
		if os.IsNotExist(err) {
			// create thumbnail

			file, err := os.Open(photoFilepath)
			if err != nil {
				log.Print(err)
				return
			}

			img, err := jpeg.Decode(file)
			if err != nil {
				log.Print(err)
				return
			}
			file.Close()

			m := resize.Thumbnail(500, 1000, img, resize.Bicubic)

			parentDir, _ := filepath.Split(cacheFilepath)
			err = os.MkdirAll(parentDir, os.FileMode(0764))
			if err != nil {
				log.Print(err)
				return
			}

			out, err := os.Create(cacheFilepath)
			if err != nil {
				log.Print(err)
				return
			}
			defer out.Close()

			jpeg.Encode(out, m, nil)
		} else {
			log.Print(err)
			return
		}
	}

	// serve thumbnail
	http.ServeFile(w, r, cacheFilepath)
}

func walkFunc(path string, info os.FileInfo, err error) error {
	if err != nil {
		return err
	}

	dir, filename := filepath.Split(path)
	dir, err = filepath.Rel(photoDir, dir)
	if err != nil {
		return err
	}

	if filename == "metadata.yaml" {
		m := metadata{Directory: dir}

		data, err := ioutil.ReadFile(path)
		if err != nil {
			return err
		}

		err = yaml.Unmarshal(data, &m)
		dirToMetadata.Put(dir, m)
	} else if filepath.Ext(filename) == ".jpg" {
		filename = filepath.Join(dir, filename)
		photos.Put(filename, struct{}{})
	}
	return nil
}

func walk() {
	for {
		filepath.Walk(photoDir, walkFunc)
		time.Sleep(5 * time.Second)
	}
}

func main() {
	flag.StringVar(&photoDir, "photoDir", "photos", "path to photos")
	flag.StringVar(&cacheDir, "cacheDir", "cache", "path to photo cache directory")
	flag.Parse()

	go walk()

	// esc for static content. true uses local files, false uses embedded
	http.Handle("/", http.FileServer(FS(false)))

	http.HandleFunc("/photo/", handlePhoto)
	http.HandleFunc("/thumb/", handleThumb)

	http.HandleFunc("/api/filters", handleFilters)
	http.HandleFunc("/api/filter", handleFilter)

	http.ListenAndServe("127.0.0.1:8000", nil)
}
<commit_msg>Add flag for bind interface and port<commit_after>//go:generate esc -o static.go -prefix static static

package main

import (
	"encoding/json"
	"flag"
	"github.com/fatih/structs"
	"github.com/nfnt/resize"
	"gopkg.in/yaml.v2"
	"image/jpeg"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"path/filepath"
	"sort"
	"sync"
	"time"
)

var photosMutex = &sync.Mutex{}
var photos = NewSyncMap()
var dirToMetadata = NewSyncMap()
var photoDir string
var cacheDir string
var bind string

type metadata struct {
	Event        string
	Photographer string
	Date         string
	Location     string
	Directory    string `json:"-" structs:"directory"`
}

func handleFilters(w http.ResponseWriter, r *http.Request) {
	keys := []string{"Event", "Photographer", "Date", "Location"}

	filters := make(map[string]map[string]struct{})
	for _, k := range keys {
		filters[k] = make(map[string]struct{})
	}

	for meta := range dirToMetadata.Values() {
		m := structs.Map(meta)
		for _, k := range keys {
			if _, ok := m[k]; ok {
				filters[k][m[k].(string)] = struct{}{}
			}
		}
	}

	jFilters := make(map[string][]string)
	for _, k := range keys {
		jFilters[k] = make([]string, 0)
	}

	for i := range filters {
		for j := range filters[i] {
			jFilters[i] = append(jFilters[i], j)
		}
		sort.Strings(jFilters[i])
		if len(jFilters[i]) == 0 {
			delete(jFilters, i)
		}
	}

	w.Header().Set("Content-Type", "application/json")
	enc := json.NewEncoder(w)
	if err := enc.Encode(jFilters); err != nil {
		log.Print(err)
		return
	}
}

func handleFilter(w http.ResponseWriter, r *http.Request) {
	filterToEligible := make(map[string][]string)

	filters := make(map[string][]string)
	for filterKey := range r.URL.Query() {
		filters[filterKey] = r.URL.Query()[filterKey]
	}

	// find all directories with matching filters
	for filterKey, filterValues := range filters {
		for dir := range dirToMetadata.Keys() {
			meta := dirToMetadata.Get(dir)
			m := structs.Map(meta)
			for k, v := range m {
				if filterKey == k {
					for _, filterValue := range filterValues {
						if filterValue == v {
							filterToEligible[filterKey] = append(filterToEligible[filterKey], dir)
						}
					}
				}
			}
		}
	}

	// find intersection of all filter matches
	eligible := make(map[string]bool)

	for _, dirs := range filterToEligible {
		for _, dir := range dirs {
			count := 0
			for _, dirTests := range filterToEligible {
				for _, dirTest := range dirTests {
					if dir == dirTest {
						count += 1
					}
				}
			}
			if count == len(filterToEligible) {
				eligible[dir] = true
			}
		}
	}

	eligiblePhotos := make([]string, 0)
	for photo := range photos.Keys() {
		dir := filepath.Dir(photo)
		if _, ok := eligible[dir]; ok {
			eligiblePhotos = append(eligiblePhotos, photo)
		}
	}
	sort.Strings(eligiblePhotos)

	w.Header().Set("Content-Type", "application/json")
	enc := json.NewEncoder(w)
	if err := enc.Encode(eligiblePhotos); err != nil {
		log.Print(err)
		return
	}
}

func handlePhoto(w http.ResponseWriter, r *http.Request) {
	if filepath.Ext(r.URL.Path) != ".jpg" {
		http.Error(w, http.StatusText(404), 404)
		return
	}

	// serve photo
	photoFilepath := filepath.Join(photoDir, r.URL.Path[len("/photo/"):])
	http.ServeFile(w, r, photoFilepath)
}

func handleThumb(w http.ResponseWriter, r *http.Request) {
	if filepath.Ext(r.URL.Path) != ".jpg" {
		http.Error(w, http.StatusText(404), 404)
		return
	}

	photoFilepath := filepath.Join(photoDir, r.URL.Path[len("/cache/"):])
	cacheFilepath := filepath.Join(cacheDir, r.URL.Path[len("/cache/"):])

	if _, err := os.Stat(cacheFilepath); err != nil {
		if os.IsNotExist(err) {
			// create thumbnail

			file, err := os.Open(photoFilepath)
			if err != nil {
				log.Print(err)
				return
			}

			img, err := jpeg.Decode(file)
			if err != nil {
				log.Print(err)
				return
			}
			file.Close()

			m := resize.Thumbnail(500, 1000, img, resize.Bicubic)

			parentDir, _ := filepath.Split(cacheFilepath)
			err = os.MkdirAll(parentDir, os.FileMode(0764))
			if err != nil {
				log.Print(err)
				return
			}

			out, err := os.Create(cacheFilepath)
			if err != nil {
				log.Print(err)
				return
			}
			defer out.Close()

			jpeg.Encode(out, m, nil)
		} else {
			log.Print(err)
			return
		}
	}

	// serve thumbnail
	http.ServeFile(w, r, cacheFilepath)
}

func walkFunc(path string, info os.FileInfo, err error) error {
	if err != nil {
		return err
	}

	dir, filename := filepath.Split(path)
	dir, err = filepath.Rel(photoDir, dir)
	if err != nil {
		return err
	}

	if filename == "metadata.yaml" {
		m := metadata{Directory: dir}

		data, err := ioutil.ReadFile(path)
		if err != nil {
			return err
		}

		err = yaml.Unmarshal(data, &m)
		dirToMetadata.Put(dir, m)
	} else if filepath.Ext(filename) == ".jpg" {
		filename = filepath.Join(dir, filename)
		photos.Put(filename, struct{}{})
	}
	return nil
}

func walk() {
	for {
		filepath.Walk(photoDir, walkFunc)
		time.Sleep(5 * time.Second)
	}
}

func main() {
	flag.StringVar(&photoDir, "photoDir", "photos", "path to photos")
	flag.StringVar(&cacheDir, "cacheDir", "cache", "path to photo cache directory")
	flag.StringVar(&bind, "bind", "127.0.0.1:8000", "interface and port to bind to")
	flag.Parse()

	go walk()

	// esc for static content. true uses local files, false uses embedded
	http.Handle("/", http.FileServer(FS(false)))

	http.HandleFunc("/photo/", handlePhoto)
	http.HandleFunc("/thumb/", handleThumb)

	http.HandleFunc("/api/filters", handleFilters)
	http.HandleFunc("/api/filter", handleFilter)

	log.Print("listening on ", bind)
	if err := http.ListenAndServe(bind, nil); err != nil {
		log.Print(err)
	}
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"os"
)

var (
	//load config
	config = Cfg()

	// api endpoints
	apibase        = "http://" + config.Sensu + ":" + config.Port
	clientapi      = apibase + "/clients"
	checksapi      = apibase + "/checks"
	resultssapi    = apibase + "/results"
	aggregatessapi = apibase + "/aggregates"
	eventssapi     = apibase + "/events"
	silencedsapi   = apibase + "/silenced"
	stashesapi     = apibase + "/stashes"
	healthapi      = apibase + "/health"
	infoapi        = apibase + "/info"

	// generic
	client    bool
	checks    bool
	events    bool
	silence   bool
	results   bool
	aggregate bool
	stash     bool
	name      []string
	bulkFile  string
	// silence command specific
	silenceClear        bool
	silenceList         bool
	silenceSubscription bool
	// check & resolve command specific
	checkName string
	checkAll  bool
)

func main() {

	// list subcommand
	listCmd := flag.NewFlagSet("list", flag.ExitOnError)
	// list flags
	listCmd.BoolVar(&client, "client", false, "use to list client(s)")
	listCmd.BoolVar(&check, "check", false, "use to list check(s)")
	listCmd.BoolVar(&event, "event", false, "use to list event(s)")
	listCmd.BoolVar(&silence, "silence", false, "use to list silence entr(y)(ies)")
	listCmd.BoolVar(&result, "result", false, "use to list result(s)")
	listCmd.BoolVar(&aggregate, "aggregate", false, "luse to ist aggregate(s)")
	listCmd.BoolVar(&stash, "stash", false, "use to list stash(es)")
	listCmd.StringVar(&name, "name", "", "specify the name(s) of the object(s) to list")

	// create subcommand
	createCmd := flag.NewFlagSet("create", flag.ExitOnError)
	// create flags
	createCmd.BoolVar(&client, "client", false, "use to create client(s)")
	createCmd.BoolVar(&result, "result", false, "use to create result(s)")
	createCmd.BoolVar(&stash, "stash", false, "use to create stash(es)")
	createCmd.BoolVar(&bulkFile, "file", false, "a valid json file for creation of objects")

	// delete subcommand
	deleteCmd := flag.NewFlagSet("delete", flag.ExitOnError)
	// delete flags
	deleteCmd.BoolVar(&client, "client", false, "use to delete client(s)")
	deleteCmd.BoolVar(&event, "events", false, "use to delete event(s)")
	deleteCmd.BoolVar(&result, "result", false, "use to delete result(s)")
	deleteCmd.BoolVar(&aggregate, "aggregate", false, "use to delete aggregate(s)")
	deleteCmd.BoolVar(&stash, "stash", false, "use to delete stash(es)")
	deleteCmd.StringVar(&name, "name", "", "specify the name(s) of the object(s) to delete")

	// silence subcommand
	silenceCmd := flag.NewFlagSet("silence", flag.ExitOnError)
	// silence flags
	silenceCmd.BoolVar(&silenceClear, "clear", false, "use to clear silenced entr(y)(ies)")
	silenceCmd.BoolVar(&silenceList, "list", false, "use to list silenced entr(y(ies)")
	silenceCmd.BoolVar(&client, "client", false, "use to target client(s)")
	silenceCmd.BoolVar(&silenceSubscription, "subscription", false, "use to target subscription(s)")
	silenceCmd.StringVar(&name, "name", "", "specify the name(s) of the client(s) or subscription(s)")

	//check subcommand
	checkCmd := flag.NewFlagSet("check", flag.ExitOnError)
	//check flags
	checkCmd.StringVar(&name, "client-name", "", "specify the name of the client")
	checkCmd.StringVar(&checkName, "check-name", "", "specify the name of the check")
	checkCmd.BoolVar(&checkAll, "all", false, "use to target all checks")
	checkCmd.BoolVar(&checkResult, "result", false, "use to get the result back from the requested check")

	//resolve subcommand
	resolveCmd := flag.NewFlagSet("check", flag.ExitOnError)
	//resolve flags
	cresolveCmd.StringVar(&name, "client-name", "", "specify the name of the client")
	resolveCmd.StringVar(&checkName, "check-name", "", "specify the name of the check")
	resolveCmd.BoolVar(&checkAll, "all", false, "use to target all events")

	switch os.Args[1] {
	case "list":
		listCmd.Parse(os.Args[2:])
	case "create":
		listCmd.Parse(os.Args[2:])
	case "delete":
		listCmd.Parse(os.Args[2:])
	case "silence":
		listCmd.Parse(os.Args[2:])
	case "check":
		listCmd.Parse(os.Args[2:])
	case "resolve":
		listCmd.Parse(os.Args[2:])
	default:
		flag.PrintDefaults()
		os.Exit(1)
	}

	if listCmd.Parsed() {
		cmdController("list")
	}
	if createCmd.Parsed() {
		cmdController("create")
	}
	if deleteCmd.Parsed() {
		cmdController("delete")
	}
	if silenceCmd.Parsed() {
		cmdController("silence")
	}
	if checkCmd.Parsed() {
		cmdController("check")
	}
	if checkCmd.Parsed() {
		cmdController("resolve")
	}
	// app := cli.NewApp()
	// app.Name = "kaze"
	// app.Version = "1.0"
	// app.Usage = "control sensu from a cli"
	// app.EnableBashCompletion = true

	// app.Commands = []cli.Command{
	// 	{
	// 		Name:  "list",
	// 		Usage: "use to add a client to sensu (most likely a proxy client)",
	// 		Flags: []cli.Flag{
	// 			cli.BoolFlag{
	// 				Name:        "l, list",
	// 				Usage:       "list clients",
	// 				Destination: &clientList,
	// 			},
	// 			cli.BoolFlag{
	// 				Name:        "c, create",
	// 				Usage:       "create clients",
	// 				Destination: &clientCreate,
	// 			},
	// 			cli.BoolFlag{
	// 				Name:        "d, delete",
	// 				Usage:       "delete clients",
	// 				Destination: &clientDelete,
	// 			},
	// 			cli.StringFlag{
	// 				Name:        "f, file",
	// 				Usage:       "specify when creating clients to do a bulk operation. Has to be a correctly formatted json file.",
	// 				Destination: &clientBulkFile,
	// 			},
	// 			cli.StringFlag{
	// 				Name:        "name",
	// 				Usage:       "name of the client (required for create)",
	// 				Destination: &clientName,
	// 			},
	// 			cli.StringFlag{
	// 				Name:        "environment, env",
	// 				Usage:       "environment of the client (required for create)",
	// 				Destination: &clientEnvironment,
	// 			},
	// 			cli.StringFlag{
	// 				Name:        "address",
	// 				Usage:       "address of the client (required for create)",
	// 				Destination: &clientAddress,
	// 			},
	// 			cli.StringSliceFlag{
	// 				Name:  "subscriptions",
	// 				Usage: "subcriptions of the client (required for create)",
	// 			},
	// 		},
	// 		Action: manageClient,
	// 	},
	// }
	// app.Run(os.Args)
}
<commit_msg>update<commit_after>package main

import (
	"flag"
	"os"
)

var (
	//load config
	config = Cfg()

	// api endpoints
	apibase        = "http://" + config.Sensu + ":" + config.Port
	clientapi      = apibase + "/clients"
	checksapi      = apibase + "/checks"
	resultssapi    = apibase + "/results"
	aggregatessapi = apibase + "/aggregates"
	eventssapi     = apibase + "/events"
	silencedsapi   = apibase + "/silenced"
	stashesapi     = apibase + "/stashes"
	healthapi      = apibase + "/health"
	infoapi        = apibase + "/info"

	// generic
	client    bool
	checks    bool
	events    bool
	silence   bool
	results   bool
	aggregate bool
	stash     bool
	name      []string
	bulkFile  string
	// silence command specific
	silenceClear        bool
	silenceList         bool
	silenceSubscription bool
	// check & resolve command specific
	checkName string
	checkAll  bool
)

func main() {

	// list subcommand
	listCmd := flag.NewFlagSet("list", flag.ExitOnError)
	// list flags
	listCmd.BoolVar(&client, "client", false, "use to list client(s)")
	listCmd.BoolVar(&check, "check", false, "use to list check(s)")
	listCmd.BoolVar(&event, "event", false, "use to list event(s)")
	listCmd.BoolVar(&silence, "silence", false, "use to list silence entr(y)(ies)")
	listCmd.BoolVar(&result, "result", false, "use to list result(s)")
	listCmd.BoolVar(&aggregate, "aggregate", false, "luse to ist aggregate(s)")
	listCmd.BoolVar(&stash, "stash", false, "use to list stash(es)")
	listCmd.StringVar(&name, "name", "", "specify the name(s) of the object(s) to list")

	// create subcommand
	createCmd := flag.NewFlagSet("create", flag.ExitOnError)
	// create flags
	createCmd.BoolVar(&client, "client", false, "use to create client(s)")
	createCmd.BoolVar(&result, "result", false, "use to create result(s)")
	createCmd.BoolVar(&stash, "stash", false, "use to create stash(es)")
	createCmd.BoolVar(&bulkFile, "file", false, "a valid json file for creation of objects")

	// delete subcommand
	deleteCmd := flag.NewFlagSet("delete", flag.ExitOnError)
	// delete flags
	deleteCmd.BoolVar(&client, "client", false, "use to delete client(s)")
	deleteCmd.BoolVar(&event, "events", false, "use to delete event(s)")
	deleteCmd.BoolVar(&result, "result", false, "use to delete result(s)")
	deleteCmd.BoolVar(&aggregate, "aggregate", false, "use to delete aggregate(s)")
	deleteCmd.BoolVar(&stash, "stash", false, "use to delete stash(es)")
	deleteCmd.StringVar(&name, "name", "", "specify the name(s) of the object(s) to delete")

	// silence subcommand
	silenceCmd := flag.NewFlagSet("silence", flag.ExitOnError)
	// silence flags
	silenceCmd.BoolVar(&silenceClear, "clear", false, "use to clear silenced entr(y)(ies)")
	silenceCmd.BoolVar(&silenceList, "list", false, "use to list silenced entr(y(ies)")
	silenceCmd.BoolVar(&client, "client", false, "use to target client(s)")
	silenceCmd.BoolVar(&silenceSubscription, "subscription", false, "use to target subscription(s)")
	silenceCmd.StringVar(&name, "name", "", "specify the name(s) of the client(s) or subscription(s)")

	//check subcommand
	checkCmd := flag.NewFlagSet("check", flag.ExitOnError)
	//check flags
	checkCmd.StringVar(&name, "client-name", "", "specify the name of the client")
	checkCmd.StringVar(&checkName, "check-name", "", "specify the name of the check")
	checkCmd.BoolVar(&checkAll, "all", false, "use to target all checks")
	checkCmd.BoolVar(&checkResult, "result", false, "use to get the result back from the requested check")

	//resolve subcommand
	resolveCmd := flag.NewFlagSet("check", flag.ExitOnError)
	//resolve flags
	resolveCmd.StringVar(&name, "client-name", "", "specify the name of the client")
	resolveCmd.StringVar(&checkName, "check-name", "", "specify the name of the check")
	resolveCmd.BoolVar(&checkAll, "all", false, "use to target all events")

	switch os.Args[1] {
	case "list":
		listCmd.Parse(os.Args[2:])
	case "create":
		listCmd.Parse(os.Args[2:])
	case "delete":
		listCmd.Parse(os.Args[2:])
	case "silence":
		listCmd.Parse(os.Args[2:])
	case "check":
		listCmd.Parse(os.Args[2:])
	case "resolve":
		listCmd.Parse(os.Args[2:])
	default:
		flag.PrintDefaults()
		os.Exit(1)
	}

	if listCmd.Parsed() {
		cmdController("list")
	}
	if createCmd.Parsed() {
		cmdController("create")
	}
	if deleteCmd.Parsed() {
		cmdController("delete")
	}
	if silenceCmd.Parsed() {
		cmdController("silence")
	}
	if checkCmd.Parsed() {
		cmdController("check")
	}
	if checkCmd.Parsed() {
		cmdController("resolve")
	}
	// app := cli.NewApp()
	// app.Name = "kaze"
	// app.Version = "1.0"
	// app.Usage = "control sensu from a cli"
	// app.EnableBashCompletion = true

	// app.Commands = []cli.Command{
	// 	{
	// 		Name:  "list",
	// 		Usage: "use to add a client to sensu (most likely a proxy client)",
	// 		Flags: []cli.Flag{
	// 			cli.BoolFlag{
	// 				Name:        "l, list",
	// 				Usage:       "list clients",
	// 				Destination: &clientList,
	// 			},
	// 			cli.BoolFlag{
	// 				Name:        "c, create",
	// 				Usage:       "create clients",
	// 				Destination: &clientCreate,
	// 			},
	// 			cli.BoolFlag{
	// 				Name:        "d, delete",
	// 				Usage:       "delete clients",
	// 				Destination: &clientDelete,
	// 			},
	// 			cli.StringFlag{
	// 				Name:        "f, file",
	// 				Usage:       "specify when creating clients to do a bulk operation. Has to be a correctly formatted json file.",
	// 				Destination: &clientBulkFile,
	// 			},
	// 			cli.StringFlag{
	// 				Name:        "name",
	// 				Usage:       "name of the client (required for create)",
	// 				Destination: &clientName,
	// 			},
	// 			cli.StringFlag{
	// 				Name:        "environment, env",
	// 				Usage:       "environment of the client (required for create)",
	// 				Destination: &clientEnvironment,
	// 			},
	// 			cli.StringFlag{
	// 				Name:        "address",
	// 				Usage:       "address of the client (required for create)",
	// 				Destination: &clientAddress,
	// 			},
	// 			cli.StringSliceFlag{
	// 				Name:  "subscriptions",
	// 				Usage: "subcriptions of the client (required for create)",
	// 			},
	// 		},
	// 		Action: manageClient,
	// 	},
	// }
	// app.Run(os.Args)
}
<|endoftext|>


<commit_before>package main

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

	"github.com/ogier/pflag"
)

var (
	name    = "csvp"
	version = "0.10.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) (ch rune, err error) {
	s, err = strconv.Unquote(`"` + s + `"`)
	if err != nil {
		return 0, err
	}

	a := []rune(s)
	if len(a) != 1 {
		return 0, fmt.Errorf("the delimiter must be a single character")
	}
	return a[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()
	}

	c := NewCSVScanner(selector, nil)
	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)
	}

	for _, file := range flagset.Args() {
		f, err := os.Open(file)
		if err != nil {
			printErr(err)
			return 1
		}
		defer f.Close()

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

func main() {
	e := _main()
	os.Exit(e)
}
<commit_msg>Use "if" instead of "switch" to check another one<commit_after>package main

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

	"github.com/ogier/pflag"
)

var (
	name    = "csvp"
	version = "0.10.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) (ch rune, err error) {
	s, err = strconv.Unquote(`"` + s + `"`)
	if err != nil {
		return 0, err
	}

	a := []rune(s)
	if len(a) != 1 {
		return 0, fmt.Errorf("the delimiter must be a single character")
	}
	return a[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
	}
	if *isHelp {
		printUsage()
		return 0
	}
	if *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()
	}

	c := NewCSVScanner(selector, nil)
	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)
	}

	for _, file := range flagset.Args() {
		f, err := os.Open(file)
		if err != nil {
			printErr(err)
			return 1
		}
		defer f.Close()

		c.InitializeReader(f)
		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 (
	"database/sql"
	"flag"
	"log"
	"log/syslog"
	"net/smtp"
	"os"
	"os/signal"
	"strconv"
	"strings"
	"syscall"
	"time"

	_ "github.com/go-sql-driver/mysql"
	"github.com/johansundell/cocapi"
)

var db *sql.DB
var mysqlUser, mysqlPass, mysqlDb, mysqlHost string
var queryInsertUpdateMember = `INSERT INTO members (tag, name, created, last_updated, active) VALUES (?, ?, null, null, 1) ON DUPLICATE KEY UPDATE member_id=LAST_INSERT_ID(member_id), last_updated = NOW(), active = 1`
var isCocUnderUpdate bool
var failedTries int
var emailTo, emailFrom string

func init() {

	mysqlDb = "cocsniffer"
	mysqlHost = os.Getenv("MYSQL_COC_HOST")
	mysqlUser = os.Getenv("MYSQL_USER")
	mysqlPass = os.Getenv("MYSQL_PASS")

	emailTo = os.Getenv("EMAIL_TO")
	emailFrom = os.Getenv("EMAIL_FROM")
}

func main() {
	useSyslog := flag.Bool("syslog", false, "Use syslog")
	flag.Parse()
	if *useSyslog {
		logwriter, e := syslog.New(syslog.LOG_NOTICE, "cocsniffer")
		if e == nil {
			log.SetOutput(logwriter)
		}
	}
	db, _ = sql.Open("mysql", mysqlUser+":"+mysqlPass+"@tcp("+mysqlHost+":3306)/"+mysqlDb)
	defer db.Close()

	isCocUnderUpdate = false
	failedTries = 0
	getMembersData()
	ticker := time.NewTicker(1 * time.Minute)
	quit := make(chan struct{})
	go func() {
		for {
			select {
			case <-ticker.C:
				getMembersData()
			case <-quit:
				ticker.Stop()
				return
			}
		}
	}()

	// Wait for SIGINT and SIGTERM (HIT CTRL-C)
	ch := make(chan os.Signal)
	signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
	log.Println(<-ch)

	close(quit)
	log.Println("Bye ;)")
}

func getMembersData() {
	members, err := cocapi.GetMemberInfo()
	if err != nil {
		reportError(err)
		return
	}

	if isCocUnderUpdate {
		isCocUnderUpdate = false
		sendEmail("COC Alert", "Servers are up again")
	}
	failedTries = 0

	var ids = make([]string, 0)
	for _, m := range members.Items {
		if result, err := db.Exec(queryInsertUpdateMember, m.Tag, m.Name); err != nil {
			log.Println(err)
		} else {
			if id, err := result.LastInsertId(); err != nil {
				log.Println(err)
			} else {
				ids = append(ids, strconv.Itoa(int(id)))
			}
		}
		if m.Role == "member" && m.Donations >= 1000 {
			log.Println("Found member that should be upgraded", m.Name)
			var alerted int
			db.QueryRow("SELECT alert_sent_donations FROM members WHERE tag = ?", m.Tag).Scan(&alerted)
			if alerted == 0 {
				sendEmail("Member "+m.Name+" should be upgraded", "Member "+m.Name+" should be upgraded")
				db.Exec("UPDATE members SET alert_sent_donations = 1 WHERE tag = ?", m.Tag)
			}
		}
	}
	db.Exec("UPDATE members SET exited = NOW() WHERE member_id NOT IN (" + strings.Join(ids, ", ") + ") AND active = 1")
	db.Exec("UPDATE members SET active = 0 WHERE member_id NOT IN (" + strings.Join(ids, ", ") + ")")
	//log.Println("done members func")
}

func reportError(err error) {
	switch t := err.(type) {
	case *cocapi.ServerError:
		if t.ErrorCode == 503 {
			failedTries++
			if failedTries > 3 {
				if !isCocUnderUpdate {
					isCocUnderUpdate = true
					sendEmail("COC Alert", "Servers under update")
				}
			}
		}
		break
	default:
		log.Println("Fatal error coc:", t)
		break
	}
}

func sendEmail(subject, message string) bool {
	body := "To: " + emailTo + "\r\nSubject: " + subject + "\r\n\r\n" + message
	if err := smtp.SendMail("127.0.0.1:25", nil, emailFrom, []string{emailTo}, []byte(body)); err != nil {
		log.Println(err)
		return false
	}
	return true
}
<commit_msg>Refactoring<commit_after>package main

import (
	"database/sql"
	"flag"
	"log"
	"log/syslog"
	"net/smtp"
	"os"
	"os/signal"
	"strconv"
	"strings"
	"syscall"
	"time"

	_ "github.com/go-sql-driver/mysql"
	"github.com/johansundell/cocapi"
)

var db *sql.DB
var mysqlUser, mysqlPass, mysqlDb, mysqlHost string
var queryInsertUpdateMember = `INSERT INTO members (tag, name, created, last_updated, active) VALUES (?, ?, null, null, 1) ON DUPLICATE KEY UPDATE member_id=LAST_INSERT_ID(member_id), last_updated = NOW(), active = 1`
var isCocUnderUpdate bool
var failedTries int
var emailTo, emailFrom string
var myClanTag string

func init() {

	mysqlDb = "cocsniffer"
	mysqlHost = os.Getenv("MYSQL_COC_HOST")
	mysqlUser = os.Getenv("MYSQL_USER")
	mysqlPass = os.Getenv("MYSQL_PASS")

	emailTo = os.Getenv("EMAIL_TO")
	emailFrom = os.Getenv("EMAIL_FROM")

	myClanTag = os.Getenv("COC_CLANTAG")
}

func main() {
	useSyslog := flag.Bool("syslog", false, "Use syslog")
	flag.Parse()
	if *useSyslog {
		logwriter, e := syslog.New(syslog.LOG_NOTICE, "cocsniffer")
		if e == nil {
			log.SetOutput(logwriter)
		}
	}
	db, _ = sql.Open("mysql", mysqlUser+":"+mysqlPass+"@tcp("+mysqlHost+":3306)/"+mysqlDb)
	defer db.Close()

	isCocUnderUpdate = false
	failedTries = 0
	getMembersData(myClanTag)
	ticker := time.NewTicker(1 * time.Minute)
	quit := make(chan struct{})
	go func() {
		for {
			select {
			case <-ticker.C:
				getMembersData(myClanTag)
			case <-quit:
				ticker.Stop()
				return
			}
		}
	}()

	// Wait for SIGINT and SIGTERM (HIT CTRL-C)
	ch := make(chan os.Signal)
	signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
	log.Println(<-ch)

	close(quit)
	log.Println("Bye ;)")
}

func getMembersData(clan string) error {
	members, err := cocapi.GetMemberInfo(clan)
	if err != nil {
		reportError(err)
		return err
	}

	if isCocUnderUpdate {
		isCocUnderUpdate = false
		sendEmail("COC Alert", "Servers are up again")
	}
	failedTries = 0

	var ids = make([]string, 0)
	for _, m := range members.Items {
		if result, err := db.Exec(queryInsertUpdateMember, m.Tag, m.Name); err != nil {
			log.Println(err)
		} else {
			if id, err := result.LastInsertId(); err != nil {
				log.Println(err)
			} else {
				ids = append(ids, strconv.Itoa(int(id)))
			}
		}
		if m.Role == "member" && m.Donations >= 1000 {
			log.Println("Found member that should be upgraded", m.Name)
			var alerted int
			db.QueryRow("SELECT alert_sent_donations FROM members WHERE tag = ?", m.Tag).Scan(&alerted)
			if alerted == 0 {
				sendEmail("Member "+m.Name+" should be upgraded", "Member "+m.Name+" should be upgraded")
				db.Exec("UPDATE members SET alert_sent_donations = 1 WHERE tag = ?", m.Tag)
			}
		}
	}
	db.Exec("UPDATE members SET exited = NOW() WHERE member_id NOT IN (" + strings.Join(ids, ", ") + ") AND active = 1")
	db.Exec("UPDATE members SET active = 0 WHERE member_id NOT IN (" + strings.Join(ids, ", ") + ")")
	//log.Println("done members func")
	return nil
}

func reportError(err error) {
	switch t := err.(type) {
	case *cocapi.ServerError:
		if t.ErrorCode == 503 {
			failedTries++
			if failedTries > 3 {
				if !isCocUnderUpdate {
					isCocUnderUpdate = true
					sendEmail("COC Alert", "Servers under update")
				}
			}
		}
		break
	default:
		log.Println("Fatal error coc:", t)
		break
	}
}

func sendEmail(subject, message string) bool {
	body := "To: " + emailTo + "\r\nSubject: " + subject + "\r\n\r\n" + message
	if err := smtp.SendMail("127.0.0.1:25", nil, emailFrom, []string{emailTo}, []byte(body)); err != nil {
		log.Println(err)
		return false
	}
	return true
}
<|endoftext|>