<commit_before>// Copyright (c) 2017 Vicente Romero. All rights reserved.
// Licensed under the MIT License.
// See LICENSE file in the project root for full license information.

package main

import (
	"errors"
	"flag"
	"fmt"
	"io"
	"os"

	"github.com/vteromero/byte-hist/bytehist"
)

const (
	formatBinary = iota
	formatDecimal
	formatHexadecimal
)

type config struct {
	File       *os.File
	ByteFormat int
}

func newConfig() config {
	return config{nil, formatDecimal}
}

func byteFormat(formatStr string) (int, error) {
	switch formatStr {
	case "b":
		return formatBinary, nil
	case "d":
		return formatDecimal, nil
	case "x":
		return formatHexadecimal, nil
	default:
		return 0, errors.New("invalid byte format")
	}
}

func openFile(fileName string) (*os.File, error) {
	// if there is no fileName, we read from stdin
	if len(fileName) == 0 {
		// checking that stdin is not a terminal
		fi, err := os.Stdin.Stat()
		if err != nil {
			return nil, err
		}

		if fi.Mode()&os.ModeCharDevice != 0 {
			return nil, errors.New("the data won't be read from a terminal")
		}

		return os.Stdin, nil
	}

	// only read regular files
	fi, err := os.Stat(fileName)
	if err != nil {
		return nil, err
	}

	mode := fi.Mode()
	if !mode.IsRegular() {
		return nil, fmt.Errorf("'%s' is not a regular file", fileName)
	}

	return os.Open(fileName)
}

func closeFile(file *os.File) {
	// do not close stdin
	if file == os.Stdin {
		return
	}

	if err := file.Close(); err != nil {
		panic(err)
	}
}

func readBytes(file *os.File, bhist *bytehist.ByteHistogram) {
	buf := make([]byte, 1024)

	for {
		n, err := file.Read(buf)
		if err != nil && err != io.EOF {
			panic(err)
		}
		if n == 0 {
			break
		}

		bhist.Update(buf, n)
	}
}

func doByteHistogram(cfg config) error {
	bhist := bytehist.NewByteHistogram()

	readBytes(cfg.File, bhist)

	if bhist.DataSize == 0 {
		return errors.New("no data to process")
	}

	printByteHistogram(cfg, bhist)

	return nil
}

func main() {
	flag.Usage = func() {
		fmt.Println("usage: byte-hist [-help] [-format={d|x|b}] [FILE]")
		flag.PrintDefaults()
	}

	helpPtr := flag.Bool("help", false, "print this message")
	formatPtr := flag.String("format", "d", "byte format {\"d\"ecimal | he\"x\"adecimal | \"b\"inary}")

	flag.Parse()

	if *helpPtr {
		flag.Usage()
		os.Exit(0)
	}

	bytefmt, err := byteFormat(*formatPtr)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	if flag.NArg() > 1 {
		fmt.Println("too many arguments")
		os.Exit(1)
	}

	file, err := openFile(flag.Arg(0))
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	defer closeFile(file)

	cfg := newConfig()
	cfg.File = file
	cfg.ByteFormat = bytefmt

	if err := doByteHistogram(cfg); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}
<commit_msg>added version flag<commit_after>// Copyright (c) 2017 Vicente Romero. All rights reserved.
// Licensed under the MIT License.
// See LICENSE file in the project root for full license information.

package main

import (
	"errors"
	"flag"
	"fmt"
	"io"
	"os"

	"github.com/vteromero/byte-hist/bytehist"
)

const VERSION = "0.1"

const (
	formatBinary = iota
	formatDecimal
	formatHexadecimal
)

type config struct {
	File       *os.File
	ByteFormat int
}

func newConfig() config {
	return config{nil, formatDecimal}
}

func byteFormat(formatStr string) (int, error) {
	switch formatStr {
	case "b":
		return formatBinary, nil
	case "d":
		return formatDecimal, nil
	case "x":
		return formatHexadecimal, nil
	default:
		return 0, errors.New("invalid byte format")
	}
}

func openFile(fileName string) (*os.File, error) {
	// if there is no fileName, we read from stdin
	if len(fileName) == 0 {
		// checking that stdin is not a terminal
		fi, err := os.Stdin.Stat()
		if err != nil {
			return nil, err
		}

		if fi.Mode()&os.ModeCharDevice != 0 {
			return nil, errors.New("the data won't be read from a terminal")
		}

		return os.Stdin, nil
	}

	// only read regular files
	fi, err := os.Stat(fileName)
	if err != nil {
		return nil, err
	}

	mode := fi.Mode()
	if !mode.IsRegular() {
		return nil, fmt.Errorf("'%s' is not a regular file", fileName)
	}

	return os.Open(fileName)
}

func closeFile(file *os.File) {
	// do not close stdin
	if file == os.Stdin {
		return
	}

	if err := file.Close(); err != nil {
		panic(err)
	}
}

func readBytes(file *os.File, bhist *bytehist.ByteHistogram) {
	buf := make([]byte, 1024)

	for {
		n, err := file.Read(buf)
		if err != nil && err != io.EOF {
			panic(err)
		}
		if n == 0 {
			break
		}

		bhist.Update(buf, n)
	}
}

func doByteHistogram(cfg config) error {
	bhist := bytehist.NewByteHistogram()

	readBytes(cfg.File, bhist)

	if bhist.DataSize == 0 {
		return errors.New("no data to process")
	}

	printByteHistogram(cfg, bhist)

	return nil
}

func main() {
	flag.Usage = func() {
		fmt.Println("usage: byte-hist [-help] [-format={d|x|b}] [FILE]")
		flag.PrintDefaults()
	}

	helpPtr := flag.Bool("help", false, "print this message")
	versionPtr := flag.Bool("version", false, "print the version")
	formatPtr := flag.String("format", "d", "byte format {\"d\"ecimal | he\"x\"adecimal | \"b\"inary}")

	flag.Parse()

	if *helpPtr {
		flag.Usage()
		os.Exit(0)
	}

	if *versionPtr {
		fmt.Printf("byte-hist %s\n", VERSION)
		os.Exit(0)
	}

	bytefmt, err := byteFormat(*formatPtr)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	if flag.NArg() > 1 {
		fmt.Println("too many arguments")
		os.Exit(1)
	}

	file, err := openFile(flag.Arg(0))
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	defer closeFile(file)

	cfg := newConfig()
	cfg.File = file
	cfg.ByteFormat = bytefmt

	if err := doByteHistogram(cfg); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}
<|endoftext|>


<commit_before>package main

import (
	"bufio"
	"fmt"
	"math/rand"
	"os"
	"strings"
	"time"

	"github.com/codegangsta/cli"
	"github.com/exercism/arkov/chain"
)

func main() {
	rand.Seed(time.Now().UnixNano())

	app := cli.NewApp()
	app.Name = "exercismarkov"
	app.Usage = "Create fake exercism nitpicks"

	app.Commands = []cli.Command{
		{
			Name:      "build",
			ShortName: "b",
			Usage:     "build a markov chain",
			Flags: []cli.Flag{
				cli.StringFlag{"infile, f", "", "File containing original text"},
				cli.StringFlag{"outfile, o", "", "File to store markov chain"},
				cli.IntFlag{"prefix, p", 2, "Prefix length"},
			},
			Action: func(c *cli.Context) {
				markov := chain.NewChain(c.Int("prefix"))

				file, err := os.Open(c.String("infile"))
				if err != nil {
					println(err)
					return
				}
				scanner := bufio.NewScanner(file)
				for scanner.Scan() {
					markov.Build(strings.NewReader(scanner.Text()))
				}

				markov.ToFile(c.String("outfile"))
			},
		},
		{
			Name:      "generate",
			ShortName: "g",
			Usage:     "generate a new comment",
			Flags: []cli.Flag{
				cli.StringFlag{"infile, f", "", "File containing chain data"},
			},
			Action: func(c *cli.Context) {
				markov := chain.FromFile(c.String("infile"))
				text := markov.Generate()
				fmt.Println(text)
			},
		},
	}

	app.Run(os.Args)
}
<commit_msg>Update dependency<commit_after>package main

import (
	"bufio"
	"fmt"
	"math/rand"
	"os"
	"strings"
	"time"

	"github.com/codegangsta/cli"
	"github.com/exercism/arkov/chain"
)

func main() {
	rand.Seed(time.Now().UnixNano())

	app := cli.NewApp()
	app.Name = "exercismarkov"
	app.Usage = "Create fake exercism nitpicks"

	app.Commands = []cli.Command{
		{
			Name:      "build",
			ShortName: "b",
			Usage:     "build a markov chain",
			Flags: []cli.Flag{
				cli.StringFlag{
					Name:  "infile, f",
					Usage: "File containing original text",
				},
				cli.StringFlag{
					Name:  "outfile, o",
					Usage: "File to store markov chain",
				},
				cli.IntFlag{
					Name:  "prefix, p",
					Value: 2,
					Usage: "Prefix length",
				},
			},
			Action: func(c *cli.Context) {
				markov := chain.NewChain(c.Int("prefix"))

				file, err := os.Open(c.String("infile"))
				if err != nil {
					println(err)
					return
				}
				scanner := bufio.NewScanner(file)
				for scanner.Scan() {
					markov.Build(strings.NewReader(scanner.Text()))
				}

				markov.ToFile(c.String("outfile"))
			},
		},
		{
			Name:      "generate",
			ShortName: "g",
			Usage:     "generate a new comment",
			Flags: []cli.Flag{
				cli.StringFlag{
					Name:  "infile, f",
					Usage: "File containing chain data",
				},
			},
			Action: func(c *cli.Context) {
				markov := chain.FromFile(c.String("infile"))
				text := markov.Generate()
				fmt.Println(text)
			},
		},
	}

	if err := app.Run(os.Args); err != nil {
		os.Exit(1)
	}
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"io/ioutil"
	"gopkg.in/yaml.v2"
)

var config = []string{"kubelet.yml"}

type kubeletConf struct {
	Kubelet_opts string `yaml:"cluster_hostname"`
}

func main(){
	fmt.Println("Hello World!")
	fmt.Println(config[0])
	var data,_ = ioutil.ReadFile(config[0])
	kc := &kubeletConf{}
	yaml.Unmarshal(data,kc)
  	fmt.Printf("----test\n %s",(*kc).Kubelet_opts)

}

<commit_msg>draft<commit_after>package main

import (
	"fmt"
	"gopkg.in/yaml.v2"
	"io/ioutil"
	"regexp"
	"reflect"
	"strings"
)

type config_paths struct {
	flannel_conf_path        string
	master_conf_path         string
	node_conf_path           string
	validity_check_conf_path string
}

type DefaultOpts struct {
	Flannel_opts         []string `yaml:"flannel_options"`
	Kubelet_opts         []string `yaml:"kubelet_options"`
	Kube_proxy_opts      []string `yaml:"kube_proxy_options"`
	Kube_apiserver_opts  []string `yaml:"kube_apiserver_options"`
	Kube_controller_opts []string `yaml:"kube_controller_manager_options"`
	Kube_scheduller_opts []string `yaml:"kube_scheduler_options"`
}

type ValidityOpts struct {
	Args_check struct {
			   Flannel_opts         []string `yaml:"flannel_options"`
			   Kubelet_opts         []string `yaml:"kubelet_options"`
			   Kube_proxy_opts      []string `yaml:"kube_proxy_options"`
			   Kube_apiserver_opts  []string `yaml:"kube_apiserver_options"`
			   Kube_controller_opts []string `yaml:"kube_controller_manager_options"`
			   Kube_scheduller_opts []string `yasml:"kube_scheduler_options"`
		   } `yaml: args_check`
}

const relative_prefix_path = "/home/k8s_kdt/kdt/"

var Conf_paths = config_paths{
	flannel_conf_path:        "roles/flannel/defaults/main.yaml",
	master_conf_path:         "roles/master/defaults/main.yml",
	node_conf_path:           "roles/node/defaults/main.yml",
	validity_check_conf_path: "roles/validity-check/defaults/main.yml",
}

func main() {
	paths := [2]string{relative_prefix_path + Conf_paths.validity_check_conf_path, relative_prefix_path + Conf_paths.flannel_conf_path}
	CompareArgs(paths, ValidityOpts{}, DefaultOpts{}, "flannel")

}

func CompareArgs(file_path [2]string, validity_opts ValidityOpts, default_opts DefaultOpts, component_name string) (bool bool, err error) {
	fmt.Printf("Check %s args in path %s and path %s.\n", component_name, file_path[0], file_path[1])
	filed_name := strings.Title(component_name + "_opts")
	validity_value, default_value, err := yaml_unmarshal(file_path, validity_opts, default_opts, filed_name)
	if err != nil {
		return false, err
	}

	fmt.Printf("validity_value is %v", validity_value)
	fmt.Printf("default_value is %v", default_value)
	if isInclude(validity_value, default_value) && isInclude(default_value, validity_value) {
		return true, nil
	}
	return false, nil
}

func yaml_unmarshal(file_path [2]string, validity_opts ValidityOpts, default_opts DefaultOpts, filed_name string) ([]string, []string, error) {
	data, err := ioutil.ReadFile(file_path[0])
	if err != nil {
		return []string{}, []string{}, fmt.Errorf("Reading config file failed: %v", err)
	}
	yaml.Unmarshal(data, &validity_opts)

	data, err = ioutil.ReadFile(file_path[1])
	if err != nil {
		return []string{}, []string{}, fmt.Errorf("Reading config file failed: %v", err)
	}
	yaml.Unmarshal(data, &default_opts)

	validity_value := reflect.ValueOf(validity_opts).FieldByName("Args_check").FieldByName(filed_name)
	default_value:= reflect.ValueOf(default_opts).FieldByName(filed_name)
	val_inf := reflect.MakeSlice(validity_value.Type(),validity_value.Len(),validity_value.Len()).Interface()
	def_inf := reflect.MakeSlice(default_value.Type(),default_value.Len(),default_value.Len()).Interface()
	val, _ := val_inf.([]string)
	def, _ := def_inf.([]string)
	fmt.Printf("%v",val)
	fmt.Printf("%v",def)

	return []string,[]string, nil
}

func isInclude(args0, args1 []string) bool {
	regx := regexp.MustCompile(`\s*-+([^\s=]+).*`)
	include := false
	for _, arg0 := range args0 {
		for _, arg1 := range args1 {
			if regx.FindString(arg0) == regx.FindString(arg1) {
				include = true
				break
			}
		}
		if !include {
			return include
		}
	}
	return include
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"net"
	"os"
	"os/signal"
	"strconv"
	"strings"
	"sync"
	"syscall"
	"time"
)

type Victims struct {
	sync.RWMutex
	procs map[int]int64
}

var (
	debug       bool
	logFile     *os.File
	logPath     string
	showVersion bool
	socketPath  string
	stdout      bool
	version     string
	victims     = &Victims{procs: make(map[int]int64)}

	Debug    *log.Logger
	Info     *log.Logger
	Warning  *log.Logger
	Error    *log.Logger
	Critical *log.Logger
)

func init() {
	flag.BoolVar(&debug, "debug", false, "Debug mode.")
	flag.StringVar(&socketPath, "socket", "/tmp/GrimReaper.socket", "Path to the Unix Domain Socket.")
	flag.StringVar(&logPath, "logpath", "/var/log/GrimReaper.log", "Path to the log file.")
	flag.BoolVar(&stdout, "stdout", false, "Log to stdout/stderr instead of to the log file.")
	flag.BoolVar(&showVersion, "version", false, "print the GrimReaper version information and exit")
}

func setupLoggers(logFile *os.File) {
	format := log.Ldate | log.Ltime | log.Lshortfile
	debugWriter := ioutil.Discard

	if stdout {
		if debug {
			debugWriter = os.Stdout
		}
		Debug = log.New(debugWriter, "DEBUG: ", format)
		Info = log.New(os.Stdout, "INFO: ", format)
		Warning = log.New(os.Stdout, "WARNING: ", format)
		Error = log.New(os.Stderr, "Error: ", format)
		Critical = log.New(os.Stderr, "CRITICAL: ", format)
	} else {
		if debug {
			debugWriter = logFile
		}
		Debug = log.New(debugWriter, "DEBUG: ", format)
		Info = log.New(logFile, "INFO: ", format)
		Warning = log.New(os.Stdout, "WARNING: ", format)
		Error = log.New(logFile, "ERROR: ", format)
		Critical = log.New(logFile, "CRITICAL: ", format)
	}
}

func oldSocketExists(socket string) bool {
	_, err := os.Stat(socket)
	if os.IsNotExist(err) {
		return false
	} else {
		return true
	}
}

func main() {
	var err error

	flag.Parse()
	if showVersion {
		fmt.Println(version)
		os.Exit(0)
	}

	if !stdout {
		logFile, err = os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
		if err != nil {
			fmt.Printf("Unable to open the log file(%v): %v", logPath, err)
			os.Exit(1)
		}
		defer logFile.Close()
	}
	setupLoggers(logFile)

	if oldSocketExists(socketPath) {
		Critical.Fatalf("Socket still exists: %s", socketPath)
	}

	listener, err := net.Listen("unix", socketPath)
	if err != nil {
		panic(err)
	}

	defer Info.Println("Shutting down...")
	defer listener.Close()
	defer os.Remove(socketPath)

	go reaper(victims)
	Info.Printf("Ready to accept connections (%s).", socketPath)
	go acceptConnections(listener, victims)

	// Handle SIGINT and SIGTERM.
	ch := make(chan os.Signal)
	signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
	log.Println(<-ch)
}

func acceptConnections(listener net.Listener, victims *Victims) {
	for {
		conn, err := listener.Accept()
		if err != nil {
			Critical.Fatalf("Got the connection's error: %v", err)
		}
		go handleConnection(conn)
	}
}

func reaper(victims *Victims) {
	for {
		now := time.Now().Unix()

		for pid, deadline := range victims.procs {
			if now > deadline {
				Warning.Printf("Terminating PID %d", pid)
				unregisterProcess(pid)
				go syscall.Kill(pid, syscall.SIGTERM)
			}
		}

		time.Sleep(300 * time.Millisecond)
	}
}

func registerProcess(pid int, timestamp int64, timeout int64) {
	victims.Lock()
	defer victims.Unlock()

	victims.procs[pid] = timestamp + timeout
}

func unregisterProcess(pid int) {
	victims.Lock()
	defer victims.Unlock()

	Debug.Printf("map: %v", victims.procs)
	delete(victims.procs, pid)

}

func handleConnection(conn net.Conn) {
	for {

		buf := make([]byte, 32)
		nr, err := conn.Read(buf)
		if err == io.EOF {
			return
		} else if err != nil {
			Error.Printf("Got data error: %v", err)
		}

		raw := string(buf[0:nr])
		data := strings.Split(raw, ":")
		length := len(data)
		if length < 2 && length > 3 {
			Error.Printf("Invalid message: %v", raw)
		}

		state := data[0]
		pid, err := strconv.ParseInt(data[1], 10, 32)
		if err != nil {
			Error.Printf("Invalid PID: %v", data[1])
		}

		if state == "register" && length == 3 {
			timeout, err := strconv.ParseInt(data[2], 10, 32)
			if err != nil {
				Error.Printf("Invalid timeout: %v", data[2])
			}
			registerProcess(int(pid), time.Now().Unix(), timeout)
		} else if state == "unregister" && length == 2 {
			unregisterProcess(int(pid))
		} else {
			Error.Printf("Invalid message: %v", raw)
		}

		Debug.Printf("Received message: %#v", raw)
	}
}
<commit_msg>-verbosity flag added - closes #1<commit_after>package main

import (
	"flag"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"net"
	"os"
	"os/signal"
	"strconv"
	"strings"
	"sync"
	"syscall"
	"time"
)

// Victims represents a collection of PIDs with their timestamps.
type Victims struct {
	sync.RWMutex
	procs map[int]int64
}

// LogLevel describes how verbose logs are.
type LogLevel uint8

// LogLevels definitions.
const (
	CRITICAL LogLevel = 0 << iota
	ERROR    LogLevel = 1
	WARNING  LogLevel = 2
	INFO     LogLevel = 3
	DEBUG    LogLevel = 4
)

var (
	logFile     *os.File
	verbose     string
	logPath     string
	showVersion bool
	socketPath  string
	stdout      bool
	version     string
	victims     = &Victims{procs: make(map[int]int64)}
	logWriters  map[LogLevel]io.Writer

	// Debug is a logger with "DEBUG" level.
	Debug *log.Logger
	// Info is a logger with "INFO" level.
	Info *log.Logger
	// Warning is a logger with "WARNING" level.
	Warning *log.Logger
	// Error is a logger with "ERROR" level.
	Error *log.Logger
	// Critical is a logger with "CRITICAL" level.
	Critical *log.Logger
)

func init() {
	flag.StringVar(&verbose, "verbose", "", "Increase verbosity by passing one or more letters.")
	flag.StringVar(&socketPath, "socket", "/tmp/GrimReaper.socket", "Path to the Unix Domain Socket.")
	flag.StringVar(&logPath, "logpath", "/var/log/GrimReaper.log", "Path to the log file.")
	flag.BoolVar(&stdout, "stdout", false, "Log to stdout/stderr instead of to the log file.")
	flag.BoolVar(&showVersion, "version", false, "print the GrimReaper version information and exit")

	logWriters = map[LogLevel]io.Writer{
		CRITICAL: ioutil.Discard,
		ERROR:    ioutil.Discard,
		WARNING:  ioutil.Discard,
		INFO:     ioutil.Discard,
		DEBUG:    ioutil.Discard,
	}

}

func configureLoggers(writer io.Writer) {
	format := log.Ldate | log.Ltime | log.Lshortfile

	if !stdout {
		writer = logFile
	}

	// Overwrite default writers
	for level := range logWriters {
		if len(verbose) >= int(level) {
			logWriters[level] = writer
		}
	}

	Critical = log.New(logWriters[INFO], "CRITICAL: ", format)
	Error = log.New(logWriters[INFO], "ERROR: ", format)
	Warning = log.New(logWriters[WARNING], "WARNING: ", format)
	Info = log.New(logWriters[INFO], "INFO: ", format)
	Debug = log.New(logWriters[DEBUG], "DEBUG: ", format)
}

func oldSocketExists(socket string) bool {
	if _, err := os.Stat(socket); os.IsNotExist(err) {
		return false
	}
	return true
}

func main() {
	var err error
	flag.Parse()

	if showVersion {
		fmt.Println(version)
		os.Exit(0)
	}

	var logWriter io.Writer
	if stdout {
		logWriter = os.Stdout
	} else {
		logFile, err = os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
		if err != nil {
			fmt.Printf("Unable to open the log file(%v): %v", logPath, err)
			os.Exit(1)
		}

		logWriter = logFile
		defer logFile.Close()
	}
	configureLoggers(logWriter)

	if oldSocketExists(socketPath) {
		Critical.Fatalf("Socket still exists: %s", socketPath)
	}

	listener, err := net.Listen("unix", socketPath)
	if err != nil {
		panic(err)
	}

	defer Info.Println("Shutting down...")
	defer listener.Close()
	defer os.Remove(socketPath)

	go reaper(victims)
	Info.Printf("Ready to accept connections (%s).", socketPath)
	go acceptConnections(listener, victims)

	// Handle SIGINT and SIGTERM.
	ch := make(chan os.Signal)
	signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
	log.Println(<-ch)
}

func acceptConnections(listener net.Listener, victims *Victims) {
	for {
		conn, err := listener.Accept()
		if err != nil {
			Critical.Fatalf("Got the connection's error: %v", err)
		}
		go handleConnection(conn)
	}
}

func reaper(victims *Victims) {
	for {
		now := time.Now().Unix()

		for pid, deadline := range victims.procs {
			if now > deadline {
				Warning.Printf("Terminating PID %d", pid)
				unregisterProcess(pid)
				go syscall.Kill(pid, syscall.SIGTERM)
			}
		}

		time.Sleep(300 * time.Millisecond)
	}
}

func registerProcess(pid int, timestamp int64, timeout int64) {
	victims.Lock()
	defer victims.Unlock()

	victims.procs[pid] = timestamp + timeout
}

func unregisterProcess(pid int) {
	victims.Lock()
	defer victims.Unlock()

	Debug.Printf("map: %v", victims.procs)
	delete(victims.procs, pid)

}

func handleConnection(conn net.Conn) {
	for {

		buf := make([]byte, 32)
		nr, err := conn.Read(buf)
		if err == io.EOF {
			return
		} else if err != nil {
			Error.Printf("Got data error: %v", err)
		}

		raw := string(buf[0:nr])
		data := strings.Split(raw, ":")
		length := len(data)
		if length < 2 && length > 3 {
			Error.Printf("Invalid message: %v", raw)
		}

		state := data[0]
		pid, err := strconv.ParseInt(data[1], 10, 32)
		if err != nil {
			Error.Printf("Invalid PID: %v", data[1])
		}

		if state == "register" && length == 3 {
			timeout, err := strconv.ParseInt(data[2], 10, 32)
			if err != nil {
				Error.Printf("Invalid timeout: %v", data[2])
			}
			registerProcess(int(pid), time.Now().Unix(), timeout)
		} else if state == "unregister" && length == 2 {
			unregisterProcess(int(pid))
		} else {
			Error.Printf("Invalid message: %v", raw)
		}

		Debug.Printf("Received message: %#v", raw)
	}
}
<|endoftext|>


<commit_before>package main

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

// indexTemplate is the HTML template we use to present the index page.
var (
	indexTemplate   = template.Must(template.ParseFiles("index.min.html"))
)

// index serves the index page
func index(w http.ResponseWriter, r *http.Request) *appError {
	// Fill in the missing fields in index.html
	var data = struct {
		Title, ApplicationName string
	}{"MyTitle", "2TE Example"}

	// Render and serve the HTML
	err := indexTemplate.Execute(w, data)
	if err != nil {
		log.Println("error rendering template:", err)
		return &appError{err, "Error rendering template", 500}
	}
	return nil
}

// appHandler is to be used in error handling
type appHandler func(http.ResponseWriter, *http.Request) *appError

type appError struct {
	Err     error
	Message string
	Code    int
}

// serveHTTP formats and passes up an error
func (fn appHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	if e := fn(w, r); e != nil { // e is *appError, not os.Error.
		log.Println(e.Err)
		http.Error(w, e.Message, e.Code)
	}
}

// Here everything starts
func main() {
	// Serve the index.html page
	http.Handle("/", appHandler(index))

	// Starting server
	port := "8080"
	fmt.Println("Starting server on port: " + port + "...")
	err := http.ListenAndServe("0.0.0.0:"+port, nil)
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}
<commit_msg>go fmt<commit_after>package main

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

// indexTemplate is the HTML template we use to present the index page.
var (
	indexTemplate = template.Must(template.ParseFiles("index.min.html"))
)

// index serves the index page
func index(w http.ResponseWriter, r *http.Request) *appError {
	// Fill in the missing fields in index.html
	var data = struct {
		Title, ApplicationName string
	}{"MyTitle", "2TE Example"}

	// Render and serve the HTML
	err := indexTemplate.Execute(w, data)
	if err != nil {
		log.Println("error rendering template:", err)
		return &appError{err, "Error rendering template", 500}
	}
	return nil
}

// appHandler is to be used in error handling
type appHandler func(http.ResponseWriter, *http.Request) *appError

type appError struct {
	Err     error
	Message string
	Code    int
}

// serveHTTP formats and passes up an error
func (fn appHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	if e := fn(w, r); e != nil { // e is *appError, not os.Error.
		log.Println(e.Err)
		http.Error(w, e.Message, e.Code)
	}
}

// Here everything starts
func main() {
	// Serve the index.html page
	http.Handle("/", appHandler(index))

	// Starting server
	port := "8080"
	fmt.Println("Starting server on port: " + port + "...")
	err := http.ListenAndServe("0.0.0.0:"+port, nil)
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}
<|endoftext|>


<commit_before>package main

import (
	"bufio"
	"errors"
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"sort"
	"strings"
)

const (
	cormFilename = "Cormfile"
	cormDir      = "_corm"
)

var (
	currentDir     string
	dirtyVendorDir string
)

var errCannotParseLine = errors.New("cannot parse line")

var vcsMetaDir = []string{".svn", ".git", ".hg"}

type repository struct {
	Path   string
	Commit string
}

func parse(s string) (*repository, error) {
	fields := strings.Fields(s)
	switch len(fields) {
	case 1:
		return &repository{Path: fields[0]}, nil
	case 2:
		return &repository{Path: fields[0], Commit: fields[1]}, nil
	default:
		return nil, errCannotParseLine
	}
}

func exists(filepath string) bool {
	_, err := os.Stat(filepath)
	return err == nil
}

func readCorm(filepath string) ([]*repository, error) {
	f, err := os.Open(filepath)
	if err != nil {
		return nil, fmt.Errorf("cannot open Cormfile: %s", err)
	}
	defer f.Close()

	repos := make([]*repository, 0, 30)

	s := bufio.NewScanner(f)
	for s.Scan() {
		line := s.Text()
		if line == "" {
			continue
		}

		repo, err := parse(line)
		if err != nil {
			fmt.Fprintf(os.Stderr, "SKIPPED: %s: %s\n", err, line)
			continue
		}
		repos = append(repos, repo)
	}

	return repos, nil
}

func goGet(repo *repository) error {
	fmt.Printf("go get %s\n", repo.Path)
	err := exec.Command("go", "get", repo.Path).Run()
	// Commit が指定されている場合の処理は後で実装する
	return err
}

func newCopyFileFun(srcBase string, destBase string) filepath.WalkFunc {

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

		relPath, err := filepath.Rel(srcBase, srcPath)
		if err != nil {
			return err
		}

		destPath := filepath.Join(destBase, relPath)

		if info.IsDir() {
			index := sort.SearchStrings(vcsMetaDir, info.Name())
			if index < len(vcsMetaDir) {
				return filepath.SkipDir
			}

			return os.MkdirAll(destPath, info.Mode())
		}

		if exists(destPath) {
			return nil
		}

		return os.Link(srcPath, destPath)
	}

}

func export(src string, dest string) error {
	copyFile := newCopyFileFun(src, dest)
	err := filepath.Walk(src, copyFile)
	return err
}

func init() {
	var err error
	currentDir, err = os.Getwd()
	if err != nil {
		fmt.Fprintln(os.Stderr, "cannot get current directory")
		os.Exit(1)
	}

	dirtyVendorDir = filepath.Join(currentDir, cormDir)
}

func fakeGopath() {

	gopath := os.Getenv("GOPATH")
	os.Setenv("GOPATH", fmt.Sprintf("%s:%s", dirtyVendorDir, gopath))

}

func mainCmd() int {
	cormfile := filepath.Join(currentDir, cormFilename)
	if !exists(cormfile) {
		fmt.Fprintf(os.Stderr, "%s does not exists\n", cormfile)
		return 1
	}

	repos, err := readCorm(cormfile)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return 1
	}

	if len(repos) == 0 {
		fmt.Fprintln(os.Stderr, "no repositories in Cormfile")
		return 1
	}

	fakeGopath()
	for _, repo := range repos {
		err := goGet(repo)
		if err != nil {
			fmt.Fprintf(os.Stderr, "cannot exec go get: %s, %s\n", repo.Path, err)
		}
	}

	return 0
}

func exportCmd() int {
	dirtyVendorSrcDir := filepath.Join(dirtyVendorDir, "src")
	cleanVendorDir := filepath.Join(currentDir, "vendor")
	err := export(dirtyVendorSrcDir, cleanVendorDir)
	if err != nil {
		fmt.Fprintln(os.Stderr, "export error:", err)
	}

	return 0
}

func usage() int {
	fmt.Println(`Usage: corm command
	install	:	install packages from Cormfile.
	export	:	export packages to vendor directory.`)

	return 1
}

func main() {
	sort.Strings(vcsMetaDir)

	if len(os.Args) == 1 {
		os.Exit(usage())
	}

	command := os.Args[1]

	switch command {
	case "install":
		os.Exit(mainCmd())
	case "export":
		os.Exit(exportCmd())
	default:
		os.Exit(usage())
	}
}
<commit_msg>サブコマンドのexecを追加<commit_after>package main

import (
	"bufio"
	"errors"
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"sort"
	"strings"
)

const (
	cormFilename = "Cormfile"
	cormDir      = "_corm"
)

var (
	currentDir     string
	dirtyVendorDir string
)

var errCannotParseLine = errors.New("cannot parse line")

var vcsMetaDir = []string{".svn", ".git", ".hg"}

type repository struct {
	Path   string
	Commit string
}

func parse(s string) (*repository, error) {
	fields := strings.Fields(s)
	switch len(fields) {
	case 1:
		return &repository{Path: fields[0]}, nil
	case 2:
		return &repository{Path: fields[0], Commit: fields[1]}, nil
	default:
		return nil, errCannotParseLine
	}
}

func exists(filepath string) bool {
	_, err := os.Stat(filepath)
	return err == nil
}

func readCorm(filepath string) ([]*repository, error) {
	f, err := os.Open(filepath)
	if err != nil {
		return nil, fmt.Errorf("cannot open Cormfile: %s", err)
	}
	defer f.Close()

	repos := make([]*repository, 0, 30)

	s := bufio.NewScanner(f)
	for s.Scan() {
		line := s.Text()
		if line == "" {
			continue
		}

		repo, err := parse(line)
		if err != nil {
			fmt.Fprintf(os.Stderr, "SKIPPED: %s: %s\n", err, line)
			continue
		}
		repos = append(repos, repo)
	}

	return repos, nil
}

func goGet(repo *repository) error {
	fmt.Printf("go get %s\n", repo.Path)
	err := exec.Command("go", "get", repo.Path).Run()
	// Commit が指定されている場合の処理は後で実装する
	return err
}

func newCopyFileFun(srcBase string, destBase string) filepath.WalkFunc {

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

		relPath, err := filepath.Rel(srcBase, srcPath)
		if err != nil {
			return err
		}

		destPath := filepath.Join(destBase, relPath)

		if info.IsDir() {
			index := sort.SearchStrings(vcsMetaDir, info.Name())
			if index < len(vcsMetaDir) {
				return filepath.SkipDir
			}

			return os.MkdirAll(destPath, info.Mode())
		}

		if exists(destPath) {
			return nil
		}

		return os.Link(srcPath, destPath)
	}

}

func export(src string, dest string) error {
	copyFile := newCopyFileFun(src, dest)
	err := filepath.Walk(src, copyFile)
	return err
}

func init() {
	var err error
	currentDir, err = os.Getwd()
	if err != nil {
		fmt.Fprintln(os.Stderr, "cannot get current directory")
		os.Exit(1)
	}

	dirtyVendorDir = filepath.Join(currentDir, cormDir)
}

func fakeGopath() {

	gopath := os.Getenv("GOPATH")
	os.Setenv("GOPATH", fmt.Sprintf("%s:%s", dirtyVendorDir, gopath))

}

func mainCmd() int {
	cormfile := filepath.Join(currentDir, cormFilename)
	if !exists(cormfile) {
		fmt.Fprintf(os.Stderr, "%s does not exists\n", cormfile)
		return 1
	}

	repos, err := readCorm(cormfile)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return 1
	}

	if len(repos) == 0 {
		fmt.Fprintln(os.Stderr, "no repositories in Cormfile")
		return 1
	}

	fakeGopath()
	for _, repo := range repos {
		err := goGet(repo)
		if err != nil {
			fmt.Fprintf(os.Stderr, "cannot exec go get: %s, %s\n", repo.Path, err)
		}
	}

	return 0
}

func exportCmd() int {
	dirtyVendorSrcDir := filepath.Join(dirtyVendorDir, "src")
	cleanVendorDir := filepath.Join(currentDir, "vendor")
	err := export(dirtyVendorSrcDir, cleanVendorDir)
	if err != nil {
		fmt.Fprintln(os.Stderr, "export error:", err)
	}

	return 0
}

func execCmd() int {
	fakeGopath()
	return 0
}

func usage() int {
	fmt.Println(`Usage: corm command
	install	:	install packages from Cormfile.
	export	:	export packages to vendor directory.`)

	return 1
}

func main() {
	sort.Strings(vcsMetaDir)

	if len(os.Args) == 1 {
		os.Exit(usage())
	}

	command := os.Args[1]

	switch command {
	case "install":
		os.Exit(mainCmd())
	case "export":
		os.Exit(exportCmd())
	case "exec":
		os.Exit(execCmd())
	default:
		os.Exit(usage())
	}
}
<|endoftext|>


<commit_before>// Copyright 2015 Google Inc. All Rights Reserved.
//
// 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 (
	"flag"
	"fmt"
	"log"
	"os"
	"os/signal"
	"time"

	"github.com/googlecloudplatform/gcsfuse/fs"
	"github.com/googlecloudplatform/gcsfuse/mount"
	"github.com/googlecloudplatform/gcsfuse/perms"
	"github.com/googlecloudplatform/gcsfuse/timeutil"
	"github.com/jacobsa/fuse"
	"github.com/jacobsa/fuse/fsutil"
	"github.com/jacobsa/gcloud/gcs"
	"github.com/jacobsa/gcloud/gcs/gcscaching"
	"golang.org/x/net/context"
)

var fBucketName = flag.String("bucket", "", "Name of GCS bucket to mount.")
var fMountPoint = flag.String("mount_point", "", "File system location.")

var fMountOptions = mount.OptionFlag(
	"o",
	"Additional system-specific mount options. Be careful!")

var fUid = flag.Int64(
	"uid",
	-1,
	"If non-negative, the UID that owns all inodes. The default is the UID of "+
		"the gcsfuse process.")

var fGid = flag.Int64(
	"gid",
	-1,
	"If non-negative, the GID that owns all inodes. The default is the GID of "+
		"the gcsfuse process.")

var fFileMode = flag.Uint(
	"file_mode",
	0644,
	"Permissions bits for files. Default is 0644.")

var fDirMode = flag.Uint(
	"dir_mode",
	0755,
	"Permissions bits for directories. Default is 0755.")

var fTempDir = flag.String(
	"temp_dir", "",
	"The temporary directory in which to store local copies of GCS objects. "+
		"If empty, the system default (probably /tmp) will be used.")

var fTempDirLimit = flag.Int64(
	"temp_dir_bytes", 1<<31,
	"A desired limit on the number of bytes used in --temp_dir. May be exceeded "+
		"for dirty files that have not been flushed or closed.")

var fGCSChunkSize = flag.Uint64(
	"gcs_chunk_size", 1<<24,
	"If set to a non-zero value N, split up GCS objects into multiple chunks of "+
		"size at most N when reading, and do not read or cache unnecessary chunks.")

var fImplicitDirs = flag.Bool(
	"implicit_dirs",
	false,
	"Implicitly define directories based on their content. See "+
		"docs/semantics.md.")

var fSupportNlink = flag.Bool(
	"support_nlink",
	false,
	"Return meaningful values for nlink from fstat(2). See docs/semantics.md.")

var fStatCacheTTL = flag.Duration(
	"stat_cache_ttl",
	time.Minute,
	"How long to cache StatObject results from GCS.")

var fTypeCacheTTL = flag.Duration(
	"type_cache_ttl",
	time.Minute,
	"How long to cache name -> file/dir type mappings in directory inodes.")

func getBucketName() string {
	s := *fBucketName
	if s == "" {
		fmt.Println("You must set --bucket.")
		os.Exit(1)
	}

	return s
}

func registerSIGINTHandler(mountPoint string) {
	// Register for SIGINT.
	signalChan := make(chan os.Signal, 1)
	signal.Notify(signalChan, os.Interrupt)

	// Start a goroutine that will unmount when the signal is received.
	go func() {
		for {
			<-signalChan
			log.Println("Received SIGINT, attempting to unmount...")

			err := fuse.Unmount(mountPoint)
			if err != nil {
				log.Printf("Failed to unmount in response to SIGINT: %v", err)
			} else {
				log.Printf("Successfully unmounted in response to SIGINT.")
				return
			}
		}
	}()
}

func getBucket() (b gcs.Bucket) {
	// Set up a GCS connection.
	log.Println("Initializing GCS connection.")
	conn, err := getConn()
	if err != nil {
		log.Fatal("Couldn't get GCS connection: ", err)
	}

	// Extract the appropriate bucket.
	b = conn.GetBucket(getBucketName())

	// Enable cached StatObject results, if appropriate.
	if *fStatCacheTTL != 0 {
		const cacheCapacity = 4096
		b = gcscaching.NewFastStatBucket(
			*fStatCacheTTL,
			gcscaching.NewStatCache(cacheCapacity),
			timeutil.RealClock(),
			b)
	}

	return
}

// Attempt to raise the rlimit for the number of open files to a decent value.
func raiseRlimit() (err error)

func main() {
	// Make logging output better.
	log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds)

	// Set up flags.
	flag.Parse()

	// Check --mount_point.
	if *fMountPoint == "" {
		log.Fatalf("You must set --mount_point.")
	}

	mountPoint := *fMountPoint

	// Sanity check: make sure the temporary directory exists and is writable
	// currently. This gives a better user experience than harder to debug EIO
	// errors when reading files in the future.
	if *fTempDir != "" {
		f, err := fsutil.AnonymousFile(*fTempDir)
		f.Close()

		if err != nil {
			log.Fatalf(
				"Error writing to temporary directory (%q); are you sure it exists "+
					"with the correct permissions?",
				err.Error())
		}
	}

	// Give the process as much latitude as possible for keeping around temporary
	// files by raising the rlimit. This is necessary especially on OS X, which
	// has a crazy low default limit (256 as of OS X 10.10.3).
	err := raiseRlimit()
	if err != nil {
		log.Fatalf("raiseRlimit: %v", err)
	}

	// Choose UID and GID.
	uid, gid, err := perms.MyUserAndGroup()
	if err != nil {
		err = fmt.Errorf("MyUserAndGroup: %v", err)
		return
	}

	if *fUid >= 0 {
		uid = uint32(*fUid)
	}

	if *fGid >= 0 {
		gid = uint32(*fGid)
	}

	// Create a file system server.
	bucket := getBucket()
	serverCfg := &fs.ServerConfig{
		Clock:               timeutil.RealClock(),
		Bucket:              bucket,
		TempDir:             *fTempDir,
		TempDirLimit:        *fTempDirLimit,
		GCSChunkSize:        *fGCSChunkSize,
		ImplicitDirectories: *fImplicitDirs,
		SupportNlink:        *fSupportNlink,
		DirTypeCacheTTL:     *fTypeCacheTTL,
		Uid:                 uid,
		Gid:                 gid,
		FilePerms:           os.FileMode(*fFileMode),
		DirPerms:            os.FileMode(*fDirMode),
	}

	server, err := fs.NewServer(serverCfg)
	if err != nil {
		log.Fatal("fs.NewServer:", err)
	}

	// Mount the file system.
	mountCfg := &fuse.MountConfig{
		FSName:  bucket.Name(),
		Options: fMountOptions,
	}

	mountedFS, err := fuse.Mount(mountPoint, server, mountCfg)
	if err != nil {
		log.Fatal("Mount:", err)
	}

	log.Println("File system has been successfully mounted.")

	// Let the user unmount with Ctrl-C (SIGINT).
	registerSIGINTHandler(mountedFS.Dir())

	// Wait for it to be unmounted.
	if err := mountedFS.Join(context.Background()); err != nil {
		log.Fatal("MountedFileSystem.Join:", err)
	}

	log.Println("Successfully exiting.")
}
<commit_msg>Print a warning on low rlimit.<commit_after>// Copyright 2015 Google Inc. All Rights Reserved.
//
// 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 (
	"flag"
	"fmt"
	"log"
	"os"
	"os/signal"
	"time"

	"github.com/googlecloudplatform/gcsfuse/fs"
	"github.com/googlecloudplatform/gcsfuse/mount"
	"github.com/googlecloudplatform/gcsfuse/perms"
	"github.com/googlecloudplatform/gcsfuse/timeutil"
	"github.com/jacobsa/fuse"
	"github.com/jacobsa/fuse/fsutil"
	"github.com/jacobsa/gcloud/gcs"
	"github.com/jacobsa/gcloud/gcs/gcscaching"
	"golang.org/x/net/context"
	"golang.org/x/sys/unix"
)

var fBucketName = flag.String("bucket", "", "Name of GCS bucket to mount.")
var fMountPoint = flag.String("mount_point", "", "File system location.")

var fMountOptions = mount.OptionFlag(
	"o",
	"Additional system-specific mount options. Be careful!")

var fUid = flag.Int64(
	"uid",
	-1,
	"If non-negative, the UID that owns all inodes. The default is the UID of "+
		"the gcsfuse process.")

var fGid = flag.Int64(
	"gid",
	-1,
	"If non-negative, the GID that owns all inodes. The default is the GID of "+
		"the gcsfuse process.")

var fFileMode = flag.Uint(
	"file_mode",
	0644,
	"Permissions bits for files. Default is 0644.")

var fDirMode = flag.Uint(
	"dir_mode",
	0755,
	"Permissions bits for directories. Default is 0755.")

var fTempDir = flag.String(
	"temp_dir", "",
	"The temporary directory in which to store local copies of GCS objects. "+
		"If empty, the system default (probably /tmp) will be used.")

var fTempDirLimit = flag.Int64(
	"temp_dir_bytes", 1<<31,
	"A desired limit on the number of bytes used in --temp_dir. May be exceeded "+
		"for dirty files that have not been flushed or closed.")

var fGCSChunkSize = flag.Uint64(
	"gcs_chunk_size", 1<<24,
	"If set to a non-zero value N, split up GCS objects into multiple chunks of "+
		"size at most N when reading, and do not read or cache unnecessary chunks.")

var fImplicitDirs = flag.Bool(
	"implicit_dirs",
	false,
	"Implicitly define directories based on their content. See "+
		"docs/semantics.md.")

var fSupportNlink = flag.Bool(
	"support_nlink",
	false,
	"Return meaningful values for nlink from fstat(2). See docs/semantics.md.")

var fStatCacheTTL = flag.Duration(
	"stat_cache_ttl",
	time.Minute,
	"How long to cache StatObject results from GCS.")

var fTypeCacheTTL = flag.Duration(
	"type_cache_ttl",
	time.Minute,
	"How long to cache name -> file/dir type mappings in directory inodes.")

func getBucketName() string {
	s := *fBucketName
	if s == "" {
		fmt.Println("You must set --bucket.")
		os.Exit(1)
	}

	return s
}

func registerSIGINTHandler(mountPoint string) {
	// Register for SIGINT.
	signalChan := make(chan os.Signal, 1)
	signal.Notify(signalChan, os.Interrupt)

	// Start a goroutine that will unmount when the signal is received.
	go func() {
		for {
			<-signalChan
			log.Println("Received SIGINT, attempting to unmount...")

			err := fuse.Unmount(mountPoint)
			if err != nil {
				log.Printf("Failed to unmount in response to SIGINT: %v", err)
			} else {
				log.Printf("Successfully unmounted in response to SIGINT.")
				return
			}
		}
	}()
}

func getBucket() (b gcs.Bucket) {
	// Set up a GCS connection.
	log.Println("Initializing GCS connection.")
	conn, err := getConn()
	if err != nil {
		log.Fatal("Couldn't get GCS connection: ", err)
	}

	// Extract the appropriate bucket.
	b = conn.GetBucket(getBucketName())

	// Enable cached StatObject results, if appropriate.
	if *fStatCacheTTL != 0 {
		const cacheCapacity = 4096
		b = gcscaching.NewFastStatBucket(
			*fStatCacheTTL,
			gcscaching.NewStatCache(cacheCapacity),
			timeutil.RealClock(),
			b)
	}

	return
}

func main() {
	// Make logging output better.
	log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds)

	// Set up flags.
	flag.Parse()

	// Check --mount_point.
	if *fMountPoint == "" {
		log.Fatalf("You must set --mount_point.")
	}

	mountPoint := *fMountPoint

	// Sanity check: make sure the temporary directory exists and is writable
	// currently. This gives a better user experience than harder to debug EIO
	// errors when reading files in the future.
	if *fTempDir != "" {
		f, err := fsutil.AnonymousFile(*fTempDir)
		f.Close()

		if err != nil {
			log.Fatalf(
				"Error writing to temporary directory (%q); are you sure it exists "+
					"with the correct permissions?",
				err.Error())
		}
	}

	// The file leaser used by the file system sizes its limit on number of
	// temporary files based on the process's rlimit. If this is too low, we'll
	// throw away cached content unnecessarily often. This is particularly a
	// problem on OS X, which has a crazy low default limit (256 as of OS X
	// 10.10.3). So print a warning if the limit is low.
	var rlimit unix.Rlimit
	if err := unix.Getrlimit(unix.RLIMIT_NOFILE, &rlimit); err == nil {
		// This appears to be the maximum that `ulimit -n` will accept on OS X.
		const reasonableLimit = 10240

		if rlimit.Cur < reasonableLimit {
			log.Printf(
				"Warning: low file rlimit of %d will cause cached content to be "+
					"frequently evicted. Consider raising with `ulimit -n`.",
				rlimit.Cur)
		}
	}

	// Choose UID and GID.
	uid, gid, err := perms.MyUserAndGroup()
	if err != nil {
		err = fmt.Errorf("MyUserAndGroup: %v", err)
		return
	}

	if *fUid >= 0 {
		uid = uint32(*fUid)
	}

	if *fGid >= 0 {
		gid = uint32(*fGid)
	}

	// Create a file system server.
	bucket := getBucket()
	serverCfg := &fs.ServerConfig{
		Clock:               timeutil.RealClock(),
		Bucket:              bucket,
		TempDir:             *fTempDir,
		TempDirLimit:        *fTempDirLimit,
		GCSChunkSize:        *fGCSChunkSize,
		ImplicitDirectories: *fImplicitDirs,
		SupportNlink:        *fSupportNlink,
		DirTypeCacheTTL:     *fTypeCacheTTL,
		Uid:                 uid,
		Gid:                 gid,
		FilePerms:           os.FileMode(*fFileMode),
		DirPerms:            os.FileMode(*fDirMode),
	}

	server, err := fs.NewServer(serverCfg)
	if err != nil {
		log.Fatal("fs.NewServer:", err)
	}

	// Mount the file system.
	mountCfg := &fuse.MountConfig{
		FSName:  bucket.Name(),
		Options: fMountOptions,
	}

	mountedFS, err := fuse.Mount(mountPoint, server, mountCfg)
	if err != nil {
		log.Fatal("Mount:", err)
	}

	log.Println("File system has been successfully mounted.")

	// Let the user unmount with Ctrl-C (SIGINT).
	registerSIGINTHandler(mountedFS.Dir())

	// Wait for it to be unmounted.
	if err := mountedFS.Join(context.Background()); err != nil {
		log.Fatal("MountedFileSystem.Join:", err)
	}

	log.Println("Successfully exiting.")
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"log"
	"os"
	"os/signal"
	"time"

	"github.com/nsf/termbox-go"
)

type Point struct {
	X, Y int
}

var (
	x, y, w, h int
	autoRun    = false
)

const (
	empty = 0x0020
)

func setDimensions(width, height int) {
	w = width
	h = height
}

func print_tb(x, y int, fg, bg termbox.Attribute, msg string) {
	for _, c := range msg {
		termbox.SetCell(x, y, c, fg, bg)
		x++
	}
}

func printf_tb(x, y int, fg, bg termbox.Attribute, format string, args ...interface{}) {
	s := fmt.Sprintf(format, args...)
	print_tb(x, y, fg, bg, s)
}

func showCursor() {
	termbox.SetCursor(x, y)
}

func pressLeft() {
	if x > 0 {
		x = x - 1
	}

	showCursor()
}

func pressUp() {
	if y > 0 {
		y = y - 1
	}

	showCursor()
}

func pressRight() {
	if x < w {
		x = x + 1
	}

	showCursor()
}

func pressDown() {
	if y < h {
		y = y + 1
	}

	showCursor()
}

func getCell(x, y int) (termbox.Cell, error) {
	w, h := termbox.Size()

	if x < 0 || x >= w {
		return termbox.Cell{}, fmt.Errorf("ERROR")
	}

	if y < 0 || y >= h {
		return termbox.Cell{}, fmt.Errorf("ERROR")
	}

	return termbox.CellBuffer()[y*w+x], nil
}

func spawnCell(x, y int) {
	termbox.SetCell(x, y, empty, termbox.ColorDefault, termbox.ColorGreen)
}

func killCell(x, y int) {
	termbox.SetCell(x, y, empty, termbox.ColorDefault, termbox.ColorBlack)
}

func clearCell(x, y int) {
	termbox.SetCell(x, y, empty, termbox.ColorDefault, termbox.ColorBlack)
}

func toggleCell(x, y int) {
	showCursor()

	if isAlive(x, y) {
		killCell(x, y)
	} else {
		spawnCell(x, y)
	}

	termbox.HideCursor()
}

func main() {
	// initialize termbox
	err := termbox.Init()
	if err != nil {
		os.Exit(1)
	}
	defer termbox.Close()

	// setup termbox
	termbox.SetInputMode(termbox.InputEsc | termbox.InputMouse)
	termbox.Clear(termbox.ColorWhite, termbox.ColorBlack)

	// set initial values
	w, h = termbox.Size()

	// put cursor in the middle
	x = w / 2
	y = h / 2
	showCursor()

	// set fps
	fpsSleepTime := time.Duration(1000000/25) * time.Microsecond

	go func() {
		for {
			time.Sleep(fpsSleepTime)
			termbox.Flush()

			if autoRun {
				tick()
			}
		}
	}()

	eventChan := make(chan termbox.Event)
	go func() {
		for {
			event := termbox.PollEvent()
			eventChan <- event
		}
	}()

	// register signals to channel
	sigChan := make(chan os.Signal)

	signal.Notify(sigChan, os.Interrupt)
	signal.Notify(sigChan, os.Kill)

	func() {
		for {
			select {
			case event := <-eventChan:
				switch event.Type {
				case termbox.EventKey:
					switch event.Key {
					case termbox.KeyCtrlZ, termbox.KeyCtrlC, termbox.KeyEsc:
						return
					case termbox.KeySpace: // Step forward one generation
						autoRun = false
						tick()
					case termbox.KeyEnter: // Toggle auto run
						toggleAutoRun()
					case termbox.KeyArrowLeft:
						pressLeft()
					case termbox.KeyArrowUp:
						pressUp()
					case termbox.KeyArrowRight:
						pressRight()
					case termbox.KeyArrowDown:
						pressDown()
					}

					// Keyboard shortcuts
					switch event.Ch {
					case 'a':
						drawPattern(&acorn)
					case 'd':
						drawPattern(&dieHard)
					case 'g':
						drawPattern(&glider)
					case 'G':
						drawPattern(&gosperGliderGun)
					case 'h':
						pressLeft()
					case 'j':
						pressDown()
					case 'k':
						pressUp()
					case 'l':
						pressRight()
					case 'c':
						clearGrid()
					case 'x':
						toggleCell(x, y)
					case 's':
						autoRun = false
						tick()
					case 'r':
						toggleAutoRun()
					case 'q':
						return
					}

				case termbox.EventResize: // set new terminal dimensions
					setDimensions(event.Width, event.Height)

				case termbox.EventMouse:
					x = event.MouseX
					y = event.MouseY

					toggleCell(x, y)

				case termbox.EventError: // quit
					log.Fatalf("Quitting because of termbox error: \n%s\n", event.Err)
				}

			case <-sigChan:
				return
			}
		}
	}()
}

func isAlive(x, y int) bool {
	if x < 0 || y < 0 || x > w || y > h {
		return false
	}

	c, err := getCell(x, y)

	if err != nil {
		return false
	}

	return c.Bg == termbox.ColorGreen
}

func aliveNeighbours(x, y int) int {
	a := 0

	for ay := y - 1; ay <= y+1; ay++ {
		for ax := x - 1; ax <= x+1; ax++ {
			if !(ay == y && ax == x) {
				if isAlive(ax, ay) {
					a++
				}
			}
		}
	}

	return a
}

func tick() {
	aliveCells := 0

	var killList = make([]Point, 0)
	var spawnList = make([]Point, 0)

	for y := 0; y < h; y++ {
		for x := 0; x < w; x++ {
			n := aliveNeighbours(x, y)

			if isAlive(x, y) {
				aliveCells++

				if n < 2 || n > 3 {
					killList = append(killList, Point{x, y})
				}
			} else {
				if n == 3 {
					spawnList = append(spawnList, Point{x, y})
				}
			}
		}
	}

	// Stop the simulation when all cells are dead
	if aliveCells == 0 {
		autoRun = false
	}

	for _, pt := range killList {
		killCell(pt.X, pt.Y)
	}

	for _, pt := range spawnList {
		spawnCell(pt.X, pt.Y)
	}
}

func toggleAutoRun() {
	autoRun = !autoRun
}

func clearGrid() {
	termbox.Clear(termbox.ColorBlack, termbox.ColorBlack)
}

func drawPattern(pattern *[][]int) {
	termbox.HideCursor()

	for i, row := range *pattern {
		for j, pixel := range row {
			if pixel == 1 {
				spawnCell(x+j, y+i)
			} else {
				clearCell(x+j, y+i)
			}
		}
	}
}
<commit_msg>Hide cursor if autoRun<commit_after>package main

import (
	"fmt"
	"log"
	"os"
	"os/signal"
	"time"

	"github.com/nsf/termbox-go"
)

type Point struct {
	X, Y int
}

var (
	x, y, w, h int
	autoRun    = false
)

const (
	empty = 0x0020
)

func setDimensions(width, height int) {
	w = width
	h = height
}

func print_tb(x, y int, fg, bg termbox.Attribute, msg string) {
	for _, c := range msg {
		termbox.SetCell(x, y, c, fg, bg)
		x++
	}
}

func printf_tb(x, y int, fg, bg termbox.Attribute, format string, args ...interface{}) {
	s := fmt.Sprintf(format, args...)
	print_tb(x, y, fg, bg, s)
}

func showCursor() {
	termbox.SetCursor(x, y)
}

func pressLeft() {
	if x > 0 {
		x = x - 1
	}

	showCursor()
}

func pressUp() {
	if y > 0 {
		y = y - 1
	}

	showCursor()
}

func pressRight() {
	if x < w {
		x = x + 1
	}

	showCursor()
}

func pressDown() {
	if y < h {
		y = y + 1
	}

	showCursor()
}

func getCell(x, y int) (termbox.Cell, error) {
	w, h := termbox.Size()

	if x < 0 || x >= w {
		return termbox.Cell{}, fmt.Errorf("ERROR")
	}

	if y < 0 || y >= h {
		return termbox.Cell{}, fmt.Errorf("ERROR")
	}

	return termbox.CellBuffer()[y*w+x], nil
}

func spawnCell(x, y int) {
	termbox.SetCell(x, y, empty, termbox.ColorDefault, termbox.ColorGreen)
}

func killCell(x, y int) {
	termbox.SetCell(x, y, empty, termbox.ColorDefault, termbox.ColorBlack)
}

func clearCell(x, y int) {
	termbox.SetCell(x, y, empty, termbox.ColorDefault, termbox.ColorBlack)
}

func toggleCell(x, y int) {
	showCursor()

	if isAlive(x, y) {
		killCell(x, y)
	} else {
		spawnCell(x, y)
	}

	termbox.HideCursor()
}

func main() {
	// initialize termbox
	err := termbox.Init()
	if err != nil {
		os.Exit(1)
	}
	defer termbox.Close()

	// setup termbox
	termbox.SetInputMode(termbox.InputEsc | termbox.InputMouse)
	termbox.Clear(termbox.ColorWhite, termbox.ColorBlack)

	// set initial values
	w, h = termbox.Size()

	// put cursor in the middle
	x = w / 2
	y = h / 2
	showCursor()

	// set fps
	fpsSleepTime := time.Duration(1000000/25) * time.Microsecond

	go func() {
		for {
			time.Sleep(fpsSleepTime)
			termbox.Flush()

			if autoRun {
				tick()
			}
		}
	}()

	eventChan := make(chan termbox.Event)
	go func() {
		for {
			event := termbox.PollEvent()
			eventChan <- event
		}
	}()

	// register signals to channel
	sigChan := make(chan os.Signal)

	signal.Notify(sigChan, os.Interrupt)
	signal.Notify(sigChan, os.Kill)

	func() {
		for {
			select {
			case event := <-eventChan:
				switch event.Type {
				case termbox.EventKey:
					switch event.Key {
					case termbox.KeyCtrlZ, termbox.KeyCtrlC, termbox.KeyEsc:
						return
					case termbox.KeySpace: // Step forward one generation
						autoRun = false
						tick()
					case termbox.KeyEnter: // Toggle auto run
						toggleAutoRun()
					case termbox.KeyArrowLeft:
						pressLeft()
					case termbox.KeyArrowUp:
						pressUp()
					case termbox.KeyArrowRight:
						pressRight()
					case termbox.KeyArrowDown:
						pressDown()
					}

					// Keyboard shortcuts
					switch event.Ch {
					case 'a':
						drawPattern(&acorn)
					case 'd':
						drawPattern(&dieHard)
					case 'g':
						drawPattern(&glider)
					case 'G':
						drawPattern(&gosperGliderGun)
					case 'h':
						pressLeft()
					case 'j':
						pressDown()
					case 'k':
						pressUp()
					case 'l':
						pressRight()
					case 'c':
						clearGrid()
					case 'x':
						toggleCell(x, y)
					case 's':
						autoRun = false
						tick()
					case 'r':
						toggleAutoRun()
					case 'q':
						return
					}

				case termbox.EventResize: // set new terminal dimensions
					setDimensions(event.Width, event.Height)

				case termbox.EventMouse:
					x = event.MouseX
					y = event.MouseY

					toggleCell(x, y)

				case termbox.EventError: // quit
					log.Fatalf("Quitting because of termbox error: \n%s\n", event.Err)
				}

			case <-sigChan:
				return
			}
		}
	}()
}

func isAlive(x, y int) bool {
	if x < 0 || y < 0 || x > w || y > h {
		return false
	}

	c, err := getCell(x, y)

	if err != nil {
		return false
	}

	return c.Bg == termbox.ColorGreen
}

func aliveNeighbours(x, y int) int {
	a := 0

	for ay := y - 1; ay <= y+1; ay++ {
		for ax := x - 1; ax <= x+1; ax++ {
			if !(ay == y && ax == x) {
				if isAlive(ax, ay) {
					a++
				}
			}
		}
	}

	return a
}

func tick() {
	aliveCells := 0

	var killList = make([]Point, 0)
	var spawnList = make([]Point, 0)

	for y := 0; y < h; y++ {
		for x := 0; x < w; x++ {
			n := aliveNeighbours(x, y)

			if isAlive(x, y) {
				aliveCells++

				if n < 2 || n > 3 {
					killList = append(killList, Point{x, y})
				}
			} else {
				if n == 3 {
					spawnList = append(spawnList, Point{x, y})
				}
			}
		}
	}

	// Stop the simulation when all cells are dead
	if aliveCells == 0 {
		autoRun = false
	}

	for _, pt := range killList {
		killCell(pt.X, pt.Y)
	}

	for _, pt := range spawnList {
		spawnCell(pt.X, pt.Y)
	}
}

func toggleAutoRun() {
	autoRun = !autoRun

	if autoRun {
		termbox.HideCursor()
	}
}

func clearGrid() {
	termbox.Clear(termbox.ColorBlack, termbox.ColorBlack)
}

func drawPattern(pattern *[][]int) {
	termbox.HideCursor()

	for i, row := range *pattern {
		for j, pixel := range row {
			if pixel == 1 {
				spawnCell(x+j, y+i)
			} else {
				clearCell(x+j, y+i)
			}
		}
	}
}
<|endoftext|>


<commit_before>//go:generate fileb0x ./b0x.json
package main

import (
	"context"
	"fmt"
	"os"

	"io/ioutil"

	"github.com/FederationOfFathers/dashboard/api"
	"github.com/FederationOfFathers/dashboard/bot"
	"github.com/FederationOfFathers/dashboard/bridge"
	"github.com/FederationOfFathers/dashboard/config"
	"github.com/FederationOfFathers/dashboard/db"
	"github.com/FederationOfFathers/dashboard/environment"
	"github.com/FederationOfFathers/dashboard/events"
	"github.com/FederationOfFathers/dashboard/messaging"
	"github.com/FederationOfFathers/dashboard/metrics"
	"github.com/FederationOfFathers/dashboard/store"
	"github.com/FederationOfFathers/dashboard/streams"
	"github.com/apokalyptik/cfg"
	"github.com/bearcherian/rollzap"
	"github.com/honeycombio/beeline-go"
	rollbar "github.com/rollbar/rollbar-go"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	"google.golang.org/api/option"
	"google.golang.org/api/youtube/v3"
	"gopkg.in/yaml.v2"
)

var twitchClientID = ""
var twitchClientSecret = ""
var youtubeAPIKey = ""
var discordCfg = bot.DiscordCfg{}
var rollbarCfg = metrics.RollbarConfig{}
var logger *zap.Logger
var devPort = 0
var DB *db.DB
var mysqlURI string
var streamChannel = "-fof-dashboard"
var mindStreams bool
var honeycombToken string
var honeycombDataset string = "unknown"

func init() {

	if environment.IsProd {
		logger, _ = zap.NewProduction()
	} else {
		logger, _ = zap.NewDevelopment()
	}
	logger = logger.Named("main")

	// ROLLBAR
	rollbarCore := rollzap.NewRollbarCore(zapcore.WarnLevel)
	if err := unmarshalConfig("cfg-rollbar.yml", &rollbarCfg); err != nil {
		logger.Error("Unable to unmarshal rollbar config", zap.Error(err))
	} else if rollbarCfg.Token != "" {
		rollbarCfg.Init()
		logger.Info("Rollbar initialized")
		logger = logger.WithOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core {
			return zapcore.NewTee(core, rollbarCore)
		}))
	}

	bot.Logger = logger.Named("bot")
	api.Logger = logger.Named("api")
	config.Logger = logger.Named("config")
	events.Logger = logger.Named("events")
	streams.Logger = logger.Named("streams")
	db.Logger = logger.Named("db")
	bridge.Logger = logger.Named("bridge")
	messaging.Logger = logger.Named("messaging")

	scfg := cfg.New("cfg-slack")
	scfg.BoolVar(&mindStreams, "mindStreams", mindStreams, "should we mind streaming?")

	err := unmarshalConfig("cfg-discord.yml", &discordCfg)
	if err != nil {
		logger.Error("Unable to load discord config", zap.Error(err))
	}

	acfg := cfg.New("cfg-api")
	acfg.StringVar(&api.ListenOn, "listen", api.ListenOn, "API bind address (env: API_LISTEN)")
	acfg.StringVar(&api.AuthSecret, "secret", api.AuthSecret, "Authentication secret for use in generating login tokens")
	acfg.StringVar(&api.JWTSecret, "hmac", api.JWTSecret, "Authentication secret used for JWT tokens")

	ecfg := cfg.New("cfg-events")
	ecfg.StringVar(&events.SaveFile, "savefile", events.SaveFile, "path to the file in which events should be persisted")
	ecfg.DurationVar(&events.SaveInterval, "saveinterval", events.SaveInterval, "how often to check and see if we need to save data")
	ecfg.StringVar(&events.OldEventLinkHMAC, "hmackey", events.OldEventLinkHMAC, "hmac key for generating team tool login links")

	dcfg := cfg.New("cfg-db")
	dcfg.StringVar(&mysqlURI, "mysql", mysqlURI, "MySQL Connection URI")
	dcfg.StringVar(&store.DBPath, "path", store.DBPath, "Path to the database file")

	tcfg := cfg.New("cfg-twitch")
	tcfg.StringVar(&twitchClientID, "clientID", "", "Twitch Client ID")
	tcfg.StringVar(&twitchClientSecret, "clientSecret", "", "Twitch Client Secret")

	ytcfg := cfg.New("cfg-youtube")
	ytcfg.StringVar(&youtubeAPIKey, "apiKey", "", "YouTube API Key")

	hcfg := cfg.New("cfg-honeycomb")
	hcfg.StringVar(&honeycombToken, "token", honeycombToken, "Token for Honeycomb project reporting")
	hcfg.StringVar(&honeycombDataset, "dataset", honeycombDataset, "Dataset for Honeycomb project reporting")
}

func main() {

	// panic recovery/reporting/logging
	defer func() {
		if err := recover(); err != nil {
			logger.With(zap.Any("error", err)).Error("APPLICATION PANIC")
		}
	}()
	cfg.Parse()

	if honeycombToken != "" {
		logger.Info("setting up honeycomb")
		beeline.Init(beeline.Config{
			WriteKey:    honeycombToken,
			Dataset:     honeycombDataset,
			ServiceName: "dashboard",
		})
	}

	store.Mind()

	DB = db.New("mysql", mysqlURI)
	streams.DB = DB
	api.DB = DB
	bot.DB = DB
	events.DB = DB

	bridge.DiscordCoreDataUpdated = bot.DiscordCoreDataUpdated
	bridge.OldEventToolLink = events.OldEventToolLink
	bridge.OldEventToolAuthorization = events.OldEventToolAuthorization

	var yt *youtube.Service
	if youtubeAPIKey != "" {
		y, err := youtube.NewService(context.Background(), option.WithAPIKey(youtubeAPIKey))
		if err != nil {
			logger.Error("YouTube service creation failed", zap.Error(err))
		}
		yt = y
		logger.Info("YouTube service created")
	}

	streams.YouTube = yt

	// start discord bot
	if discordCfg.Token != "" {
		if err := startDiscord(discordCfg, yt); err != nil {
			logger.Error(fmt.Sprintf("Discord failed to start: %s", err))
		}
	}

	if mindStreams {
		go func() {
			defer func() {
				if r := recover(); r != nil {
					logger.Error("Unable to mind streams", zap.String("reocvered", fmt.Sprintf("%v", r)))
				}
			}()
			logger.Info("Minding streams", zap.String("channel", streamChannel))
			if err := streams.Twitch(twitchClientID, twitchClientSecret); err != nil {
				logger.Error("unable to init Twitch client", zap.Error(err))
			}
			streams.Mind()
		}()

	} else {
		logger.Info("Not minding streams")
	}

	events.Start() //TODO still needed? old events?
	events.MindEvents()

	rollbar.Info("starting up")
	api.Run()

}

func startDiscord(discordCfg bot.DiscordCfg, yt *youtube.Service) error {
	logger.Info("Starting discord")
	discordApi, err := bot.StartDiscord(discordCfg, yt)
	if err != nil {
		return err
	}
	discordApi.MindGuild()
	defer discordApi.Shutdown()

	messaging.AddMsgAPI(discordApi)

	return nil
}

// unmarshal a config YML file into an interface
func unmarshalConfig(fileName string, cfgObject interface{}) error {
	// exit quietly if no file. assume we are not configuring that portion
	if _, err := os.Stat(fileName); err != nil {
		logger.Info("File does not exist", zap.String("file", fileName))
		return nil
	}

	// read file data
	fileData, err := ioutil.ReadFile(fileName)
	if err != nil {
		return err
	}

	// unmarshal into interface object
	err2 := yaml.Unmarshal(fileData, cfgObject)
	if err2 != nil {
		return err2
	}

	return nil
}
<commit_msg>return api for later handling in main<commit_after>//go:generate fileb0x ./b0x.json
package main

import (
	"context"
	"fmt"
	"os"

	"io/ioutil"

	"github.com/FederationOfFathers/dashboard/api"
	"github.com/FederationOfFathers/dashboard/bot"
	"github.com/FederationOfFathers/dashboard/bridge"
	"github.com/FederationOfFathers/dashboard/config"
	"github.com/FederationOfFathers/dashboard/db"
	"github.com/FederationOfFathers/dashboard/environment"
	"github.com/FederationOfFathers/dashboard/events"
	"github.com/FederationOfFathers/dashboard/messaging"
	"github.com/FederationOfFathers/dashboard/metrics"
	"github.com/FederationOfFathers/dashboard/store"
	"github.com/FederationOfFathers/dashboard/streams"
	"github.com/apokalyptik/cfg"
	"github.com/bearcherian/rollzap"
	"github.com/honeycombio/beeline-go"
	rollbar "github.com/rollbar/rollbar-go"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	"google.golang.org/api/option"
	"google.golang.org/api/youtube/v3"
	"gopkg.in/yaml.v2"
)

var twitchClientID = ""
var twitchClientSecret = ""
var youtubeAPIKey = ""
var discordCfg = bot.DiscordCfg{}
var rollbarCfg = metrics.RollbarConfig{}
var logger *zap.Logger
var devPort = 0
var DB *db.DB
var mysqlURI string
var streamChannel = "-fof-dashboard"
var mindStreams bool
var honeycombToken string
var honeycombDataset string = "unknown"

func init() {

	if environment.IsProd {
		logger, _ = zap.NewProduction()
	} else {
		logger, _ = zap.NewDevelopment()
	}
	logger = logger.Named("main")

	// ROLLBAR
	rollbarCore := rollzap.NewRollbarCore(zapcore.WarnLevel)
	if err := unmarshalConfig("cfg-rollbar.yml", &rollbarCfg); err != nil {
		logger.Error("Unable to unmarshal rollbar config", zap.Error(err))
	} else if rollbarCfg.Token != "" {
		rollbarCfg.Init()
		logger.Info("Rollbar initialized")
		logger = logger.WithOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core {
			return zapcore.NewTee(core, rollbarCore)
		}))
	}

	bot.Logger = logger.Named("bot")
	api.Logger = logger.Named("api")
	config.Logger = logger.Named("config")
	events.Logger = logger.Named("events")
	streams.Logger = logger.Named("streams")
	db.Logger = logger.Named("db")
	bridge.Logger = logger.Named("bridge")
	messaging.Logger = logger.Named("messaging")

	scfg := cfg.New("cfg-slack")
	scfg.BoolVar(&mindStreams, "mindStreams", mindStreams, "should we mind streaming?")

	err := unmarshalConfig("cfg-discord.yml", &discordCfg)
	if err != nil {
		logger.Error("Unable to load discord config", zap.Error(err))
	}

	acfg := cfg.New("cfg-api")
	acfg.StringVar(&api.ListenOn, "listen", api.ListenOn, "API bind address (env: API_LISTEN)")
	acfg.StringVar(&api.AuthSecret, "secret", api.AuthSecret, "Authentication secret for use in generating login tokens")
	acfg.StringVar(&api.JWTSecret, "hmac", api.JWTSecret, "Authentication secret used for JWT tokens")

	ecfg := cfg.New("cfg-events")
	ecfg.StringVar(&events.SaveFile, "savefile", events.SaveFile, "path to the file in which events should be persisted")
	ecfg.DurationVar(&events.SaveInterval, "saveinterval", events.SaveInterval, "how often to check and see if we need to save data")
	ecfg.StringVar(&events.OldEventLinkHMAC, "hmackey", events.OldEventLinkHMAC, "hmac key for generating team tool login links")

	dcfg := cfg.New("cfg-db")
	dcfg.StringVar(&mysqlURI, "mysql", mysqlURI, "MySQL Connection URI")
	dcfg.StringVar(&store.DBPath, "path", store.DBPath, "Path to the database file")

	tcfg := cfg.New("cfg-twitch")
	tcfg.StringVar(&twitchClientID, "clientID", "", "Twitch Client ID")
	tcfg.StringVar(&twitchClientSecret, "clientSecret", "", "Twitch Client Secret")

	ytcfg := cfg.New("cfg-youtube")
	ytcfg.StringVar(&youtubeAPIKey, "apiKey", "", "YouTube API Key")

	hcfg := cfg.New("cfg-honeycomb")
	hcfg.StringVar(&honeycombToken, "token", honeycombToken, "Token for Honeycomb project reporting")
	hcfg.StringVar(&honeycombDataset, "dataset", honeycombDataset, "Dataset for Honeycomb project reporting")
}

func main() {

	// panic recovery/reporting/logging
	defer func() {
		if err := recover(); err != nil {
			logger.With(zap.Any("error", err)).Error("APPLICATION PANIC")
		}
	}()
	cfg.Parse()

	if honeycombToken != "" {
		logger.Info("setting up honeycomb")
		beeline.Init(beeline.Config{
			WriteKey:    honeycombToken,
			Dataset:     honeycombDataset,
			ServiceName: "dashboard",
		})
	}

	store.Mind()

	DB = db.New("mysql", mysqlURI)
	streams.DB = DB
	api.DB = DB
	bot.DB = DB
	events.DB = DB

	bridge.DiscordCoreDataUpdated = bot.DiscordCoreDataUpdated
	bridge.OldEventToolLink = events.OldEventToolLink
	bridge.OldEventToolAuthorization = events.OldEventToolAuthorization

	var yt *youtube.Service
	if youtubeAPIKey != "" {
		y, err := youtube.NewService(context.Background(), option.WithAPIKey(youtubeAPIKey))
		if err != nil {
			logger.Error("YouTube service creation failed", zap.Error(err))
		}
		yt = y
		logger.Info("YouTube service created")
	}

	streams.YouTube = yt

	// start discord bot
	if discordCfg.Token != "" {
		if dApi, err := startDiscord(discordCfg, yt); err != nil {
			logger.Error(fmt.Sprintf("Discord failed to start: %s", err))
		} else {
			defer dApi.Shutdown()
		}
	}

	if mindStreams {
		go func() {
			defer func() {
				if r := recover(); r != nil {
					logger.Error("Unable to mind streams", zap.String("reocvered", fmt.Sprintf("%v", r)))
				}
			}()
			logger.Info("Minding streams", zap.String("channel", streamChannel))
			if err := streams.Twitch(twitchClientID, twitchClientSecret); err != nil {
				logger.Error("unable to init Twitch client", zap.Error(err))
			}
			streams.Mind()
		}()

	} else {
		logger.Info("Not minding streams")
	}

	events.Start() //TODO still needed? old events?
	events.MindEvents()

	rollbar.Info("starting up")
	api.Run()

}

func startDiscord(discordCfg bot.DiscordCfg, yt *youtube.Service) (*bot.DiscordAPI, error) {
	logger.Info("Starting discord")
	discordApi, err := bot.StartDiscord(discordCfg, yt)
	if err != nil {
		return nil, err
	}
	discordApi.MindGuild()

	messaging.AddMsgAPI(discordApi)

	return discordApi, nil
}

// unmarshal a config YML file into an interface
func unmarshalConfig(fileName string, cfgObject interface{}) error {
	// exit quietly if no file. assume we are not configuring that portion
	if _, err := os.Stat(fileName); err != nil {
		logger.Info("File does not exist", zap.String("file", fileName))
		return nil
	}

	// read file data
	fileData, err := ioutil.ReadFile(fileName)
	if err != nil {
		return err
	}

	// unmarshal into interface object
	err2 := yaml.Unmarshal(fileData, cfgObject)
	if err2 != nil {
		return err2
	}

	return nil
}
<|endoftext|>


<commit_before>package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/nlopes/slack"

	"github.com/ShevaXu/gru/ui"
)

const TokenEnv = "BOT_TOKEN"

func main() {
	// Obtain token from environment variable
	token := os.Getenv(TokenEnv)
	if token == "" {
		panic("No token, please set BOT_TOKEN")
	}
	// When it gets serious
	//os.Unsetenv(TokenEnv)

	lg := log.New(os.Stdout, "[SLACK] ", log.Lshortfile|log.LstdFlags)
	chat := ui.NewSlackChat(token, lg)

	fmt.Println(chat.Listen(func(ctx context.Context, msg interface{}) {
		if m, ok := msg.(slack.Msg); ok {
			//params := slack.NewPostMessageParameters()
			//params.AsUser = true
			//err := chat.Talk(m.Channel, fmt.Sprintf("Echo: %s", m.Text), params)
			err := chat.Talk(m.Channel, fmt.Sprintf("Echo: %s", m.Text), nil)
			if err != nil {
				fmt.Println(err)
			}
		}
	}))
}
<commit_msg>feat (main): demo utilized NLP<commit_after>package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/nlopes/slack"

	"github.com/ShevaXu/gru/nlp/textrazor"
	"github.com/ShevaXu/gru/ui"
	"github.com/ShevaXu/gru/utils"
)

const (
	TokenEnv          = "BOT_TOKEN"
	TextRazorTokenEnv = "TEXTRAZOR_TOKEN"
)

func Echo(chat ui.Chatter) ui.ChatMsgHandler {
	return func(ctx context.Context, msg interface{}) {
		if m, ok := msg.(slack.Msg); ok {
			//params := slack.NewPostMessageParameters()
			//params.AsUser = true
			//err := chat.Talk(m.Channel, fmt.Sprintf("Echo: %s", m.Text), params)
			err := chat.Talk(m.Channel, fmt.Sprintf("Echo: %s", m.Text), nil)
			if err != nil {
				log.Println(err)
			}
		}
	}
}

func AnalyzeText(chat ui.Chatter, cl *textrazor.Client) ui.ChatMsgHandler {
	return func(ctx context.Context, msg interface{}) {
		if m, ok := msg.(slack.Msg); ok {
			text := m.Text
			res, err := cl.Query(textrazor.EntitiesWordsRelations, text)
			if err != nil {
				log.Println(err)
				return
			}
			output := res.Resp.Sentences[0].OneLine()
			if len(res.Resp.Entities) > 0 {
				output += (" - " + res.Resp.Entities[0].WikiLink)
			}
			err = chat.Talk(m.Channel, output, nil)
			if err != nil {
				log.Println(err)
			}
		}
	}
}

func main() {
	// Obtain token from environment variable
	token := os.Getenv(TokenEnv)
	if token == "" {
		panic("No token, please set BOT_TOKEN")
	}
	// Optional nlp API
	nlpToken := os.Getenv(TextRazorTokenEnv)
	// When it gets serious
	//os.Unsetenv(TokenEnv)
	//os.Unsetenv(TextRazorTokenEnv)

	// init chatter
	lg := log.New(os.Stdout, "[SLACK] ", log.Lshortfile|log.LstdFlags)
	chat := ui.NewSlackChat(token, lg)

	var f ui.ChatMsgHandler
	if nlpToken != "" {
		cl := textrazor.NewClient(nlpToken, utils.DefaultClient)
		f = AnalyzeText(chat, cl)
	} else {
		f = Echo(chat)
	}

	log.Println(chat.Listen(f))
}
<|endoftext|>


<commit_before>package main

import (
	"bytes"
	"fmt"
	"log"
	"net/http"
	"net/url"
	"os"
	"strconv"
	"strings"
	"text/tabwriter"
	"time"

	"github.com/basgys/alterego/router"
)

var (
	rt                 *router.Router // Let's make it global
	requestLogging     bool
	redirectStatusCode int
)

func main() {
	// Basic configuration
	requestLogging = os.Getenv("REQUEST_LOGGING") == "true"
	rsc := os.Getenv("REDIRECT_STATUS_CODE")
	if rsc != "" {
		code, err := strconv.Atoi(rsc)
		if err != nil {
			fmt.Println("Cannot parse REDIRECT_STATUS_CODE", err)
			os.Exit(1)
		}
		redirectStatusCode = code
	} else {
		redirectStatusCode = http.StatusPermanentRedirect
	}

	// Build HTTP server addr
	ip := os.Getenv("IP")
	if ip == "" {
		ip = "127.0.0.1"
	}
	port := os.Getenv("PORT")
	if port == "" {
		port = "5080"
	}
	addr := strings.Join([]string{ip, port}, ":")

	// Build router
	rules, err := buildRules()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	printRules(rules)

	rt = &router.Router{
		Rules: rules,
	}

	// Attach routes
	mux := http.NewServeMux()
	mux.HandleFunc("/__health__", health)
	mux.HandleFunc("/", redirect)

	// Boot
	s := &http.Server{
		Addr:           addr,
		Handler:        mux,
		ReadTimeout:    5 * time.Second,
		WriteTimeout:   5 * time.Second,
		MaxHeaderBytes: 1 << 20,
	}

	fmt.Printf("\nRunning on %s...\n", addr)
	log.Fatal(s.ListenAndServe())
}

// health is called by the container health check
func health(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusOK)
}

func redirect(w http.ResponseWriter, r *http.Request) {
	uri, err := url.ParseRequestURI(r.RequestURI)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		return
	}
	req := url.URL{
		Scheme:   "http",
		Host:     r.Host,
		Path:     uri.Path,
		RawQuery: uri.RawQuery,
	}

	// Find matching route
	redir, ok := rt.Match(&req)
	if !ok {
		if requestLogging {
			fmt.Printf("[%s] No match %s -> %d\n",
				time.Now().UTC().Format(time.RFC3339Nano),
				req.String(),
				http.StatusNotFound,
			)
		}
		w.WriteHeader(http.StatusNotFound)
		return
	}

	if requestLogging {
		fmt.Printf("[%s] Redirect %s -> %s\n",
			time.Now().UTC().Format(time.RFC3339Nano),
			req.String(),
			redir.String(),
		)
	}
	http.Redirect(w, r, redir.String(), redirectStatusCode)
}

func buildRules() ([]router.Rule, error) {
	var rules []router.Rule
	items := strings.Split(os.Getenv("REDIRECTS"), ",")
	for i, item := range items {
		if item == "" {
			return nil, fmt.Errorf("empty redirect #%d", i)
		}

		env := strings.TrimSpace(item)
		l := strings.Split(os.Getenv(env), ",")
		if len(l) != 2 {
			return nil, fmt.Errorf("malformed redirection %s from env var %s", l, env)
		}
		src, err := url.Parse(l[0])
		if err != nil {
			return nil, fmt.Errorf("bad source url %s", l[0])
		}
		dst, err := url.Parse(l[1])
		if err != nil {
			return nil, fmt.Errorf("bad destination url %s", l[1])
		}

		rules = append(rules, router.Rule{
			Src: *src,
			Dst: *dst,
		})
	}
	return rules, nil
}

func printRules(rules []router.Rule) {
	w := new(tabwriter.Writer)
	buf := bytes.NewBuffer([]byte{})
	w.Init(buf, 0, 8, 0, '\t', 0)
	fmt.Fprintln(w, "Source \t Destination")

	for _, r := range rules {
		fmt.Fprintf(w, "%s \t %s\n", r.Src.String(), r.Dst.String())
	}

	w.Flush()
	fmt.Println("Rules:")
	fmt.Println(buf.String())
}
<commit_msg>Log status code<commit_after>package main

import (
	"bytes"
	"fmt"
	"log"
	"net/http"
	"net/url"
	"os"
	"strconv"
	"strings"
	"text/tabwriter"
	"time"

	"github.com/basgys/alterego/router"
)

var (
	rt                 *router.Router // Let's make it global
	requestLogging     bool
	redirectStatusCode int
)

func main() {
	// Basic configuration
	requestLogging = os.Getenv("REQUEST_LOGGING") == "true"
	rsc := os.Getenv("REDIRECT_STATUS_CODE")
	if rsc != "" {
		code, err := strconv.Atoi(rsc)
		if err != nil {
			fmt.Println("Cannot parse REDIRECT_STATUS_CODE", err)
			os.Exit(1)
		}
		redirectStatusCode = code
	} else {
		redirectStatusCode = http.StatusPermanentRedirect
	}

	// Build HTTP server addr
	ip := os.Getenv("IP")
	if ip == "" {
		ip = "127.0.0.1"
	}
	port := os.Getenv("PORT")
	if port == "" {
		port = "5080"
	}
	addr := strings.Join([]string{ip, port}, ":")

	// Build router
	rules, err := buildRules()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	printRules(rules)

	rt = &router.Router{
		Rules: rules,
	}

	// Attach routes
	mux := http.NewServeMux()
	mux.HandleFunc("/__health__", health)
	mux.HandleFunc("/", redirect)

	// Boot
	s := &http.Server{
		Addr:           addr,
		Handler:        mux,
		ReadTimeout:    5 * time.Second,
		WriteTimeout:   5 * time.Second,
		MaxHeaderBytes: 1 << 20,
	}

	fmt.Printf("\nRunning on %s...\n", addr)
	log.Fatal(s.ListenAndServe())
}

// health is called by the container health check
func health(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusOK)
}

func redirect(w http.ResponseWriter, r *http.Request) {
	uri, err := url.ParseRequestURI(r.RequestURI)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		return
	}
	req := url.URL{
		Scheme:   "http",
		Host:     r.Host,
		Path:     uri.Path,
		RawQuery: uri.RawQuery,
	}

	// Find matching route
	redir, ok := rt.Match(&req)
	if !ok {
		if requestLogging {
			fmt.Printf("[%s] No match %s -> %d\n",
				time.Now().UTC().Format(time.RFC3339Nano),
				req.String(),
				http.StatusNotFound,
			)
		}
		w.WriteHeader(http.StatusNotFound)
		return
	}

	if requestLogging {
		fmt.Printf("[%s] Redirect (%d) %s -> %s\n",
			time.Now().UTC().Format(time.RFC3339Nano),
			redirectStatusCode,
			req.String(),
			redir.String(),
		)
	}
	http.Redirect(w, r, redir.String(), redirectStatusCode)
}

func buildRules() ([]router.Rule, error) {
	var rules []router.Rule
	items := strings.Split(os.Getenv("REDIRECTS"), ",")
	for i, item := range items {
		if item == "" {
			return nil, fmt.Errorf("empty redirect #%d", i)
		}

		env := strings.TrimSpace(item)
		l := strings.Split(os.Getenv(env), ",")
		if len(l) != 2 {
			return nil, fmt.Errorf("malformed redirection %s from env var %s", l, env)
		}
		src, err := url.Parse(l[0])
		if err != nil {
			return nil, fmt.Errorf("bad source url %s", l[0])
		}
		dst, err := url.Parse(l[1])
		if err != nil {
			return nil, fmt.Errorf("bad destination url %s", l[1])
		}

		rules = append(rules, router.Rule{
			Src: *src,
			Dst: *dst,
		})
	}
	return rules, nil
}

func printRules(rules []router.Rule) {
	w := new(tabwriter.Writer)
	buf := bytes.NewBuffer([]byte{})
	w.Init(buf, 0, 8, 0, '\t', 0)
	fmt.Fprintln(w, "Source \t Destination")

	for _, r := range rules {
		fmt.Fprintf(w, "%s \t %s\n", r.Src.String(), r.Dst.String())
	}

	w.Flush()
	fmt.Println("Rules:")
	fmt.Println(buf.String())
}
<|endoftext|>


<commit_before>package main

import (
	"encoding/binary"
	"io"
	"log"
	"os"
)

var EncryptionTypes = map[string]int{
	"SHA2":     1,
	"Rijndael": 2,
	"AES":      2,
	"ArcFour":  4,
	"TwoFish":  8,
}

type Metadata struct {
	signature1 uint32
	signature2 uint32
	flags      uint32
	version    uint32
	seed       [16]byte
	encryption [16]byte
	groups     uint32
	entries    uint32
	hash       [32]byte
	seed2      [32]byte
	rounds     uint32
}

type KeepassXDatabase struct {
	*Metadata
	payload []byte
}

func NewKeepassXDatabase() (*KeepassXDatabase, error) {
	return &KeepassXDatabase{
		Metadata: new(Metadata),
	}, nil
}

func (m *Metadata) ReadFrom(r io.Reader) (int64, error) {
	var buf [4]byte
	uint32Bytes := buf[:4]

	n, err := io.ReadFull(r, uint32Bytes)
	n64 := int64(n)
	m.signature1 = binary.LittleEndian.Uint32(uint32Bytes)

	n, err = io.ReadFull(r, uint32Bytes)
	n64 += int64(n)
	m.signature2 = binary.LittleEndian.Uint32(uint32Bytes)

	n, err = io.ReadFull(r, uint32Bytes)
	n64 += int64(n)
	m.flags = binary.LittleEndian.Uint32(uint32Bytes)

	n, err = io.ReadFull(r, uint32Bytes)
	n64 += int64(n)
	m.version = binary.LittleEndian.Uint32(uint32Bytes)

	var seed [16]byte
	n, err = io.ReadFull(r, seed[:])
	n64 += int64(n)
	m.seed = seed

	var encryption [16]byte
	n, err = io.ReadFull(r, encryption[:])
	n64 += int64(n)
	m.encryption = encryption

	n, err = io.ReadFull(r, uint32Bytes)
	n64 += int64(n)
	m.groups = binary.LittleEndian.Uint32(uint32Bytes)

	n, err = io.ReadFull(r, uint32Bytes)
	n64 += int64(n)
	m.entries = binary.LittleEndian.Uint32(uint32Bytes)

	var hash [32]byte
	n, err = io.ReadFull(r, hash[:])
	n64 += int64(n)
	m.hash = hash

	var seed2 [32]byte
	n, err = io.ReadFull(r, seed2[:])
	n64 += int64(n)
	m.seed2 = seed2

	n, err = io.ReadFull(r, uint32Bytes)
	n64 += int64(n)
	m.rounds = binary.LittleEndian.Uint32(uint32Bytes)

	return n64, err
}

func (k *KeepassXDatabase) decryptPayload(payload []byte, seed [16]byte, seed2 [32]byte, rounds uint32, flags uint32, encryption [16]byte) ([]byte, error) {
	var err error
	var data []byte
	return data, err
}

func (k *KeepassXDatabase) parsePayload(payload []byte) error {
	return nil
}

func (k *KeepassXDatabase) ReadFrom(r io.Reader) (int64, error) {
	n, err := k.Metadata.ReadFrom(r)
	if err != nil {
		return n, err
	}
	var content []byte
	_, err = io.ReadFull(r, content[:])
	payload, err := k.decryptPayload(content, k.seed, k.seed2, k.rounds, k.flags, k.encryption)
	err = k.parsePayload(payload)
	return n, err
}

func main() {
	path := os.Args[1]
	f, err := os.OpenFile(path, os.O_RDONLY, 0)
	if err != nil {
		log.Fatalf("%v", err)
	}
	defer f.Close()
	db, err := NewKeepassXDatabase()
	if err != nil {
		log.Fatalf("%v", err)
	}
	_, err = db.ReadFrom(f)
	if err != nil {
		log.Fatalf("%v", err)
	}
}
<commit_msg>Better param name<commit_after>package main

import (
	"encoding/binary"
	"io"
	"log"
	"os"
)

var EncryptionTypes = map[string]int{
	"SHA2":     1,
	"Rijndael": 2,
	"AES":      2,
	"ArcFour":  4,
	"TwoFish":  8,
}

type Metadata struct {
	signature1 uint32
	signature2 uint32
	flags      uint32
	version    uint32
	seed       [16]byte
	encryption [16]byte
	groups     uint32
	entries    uint32
	hash       [32]byte
	seed2      [32]byte
	rounds     uint32
}

type KeepassXDatabase struct {
	*Metadata
	payload []byte
}

func NewKeepassXDatabase() (*KeepassXDatabase, error) {
	return &KeepassXDatabase{
		Metadata: new(Metadata),
	}, nil
}

func (m *Metadata) ReadFrom(r io.Reader) (int64, error) {
	var buf [4]byte
	uint32Bytes := buf[:4]

	n, err := io.ReadFull(r, uint32Bytes)
	n64 := int64(n)
	m.signature1 = binary.LittleEndian.Uint32(uint32Bytes)

	n, err = io.ReadFull(r, uint32Bytes)
	n64 += int64(n)
	m.signature2 = binary.LittleEndian.Uint32(uint32Bytes)

	n, err = io.ReadFull(r, uint32Bytes)
	n64 += int64(n)
	m.flags = binary.LittleEndian.Uint32(uint32Bytes)

	n, err = io.ReadFull(r, uint32Bytes)
	n64 += int64(n)
	m.version = binary.LittleEndian.Uint32(uint32Bytes)

	var seed [16]byte
	n, err = io.ReadFull(r, seed[:])
	n64 += int64(n)
	m.seed = seed

	var encryption [16]byte
	n, err = io.ReadFull(r, encryption[:])
	n64 += int64(n)
	m.encryption = encryption

	n, err = io.ReadFull(r, uint32Bytes)
	n64 += int64(n)
	m.groups = binary.LittleEndian.Uint32(uint32Bytes)

	n, err = io.ReadFull(r, uint32Bytes)
	n64 += int64(n)
	m.entries = binary.LittleEndian.Uint32(uint32Bytes)

	var hash [32]byte
	n, err = io.ReadFull(r, hash[:])
	n64 += int64(n)
	m.hash = hash

	var seed2 [32]byte
	n, err = io.ReadFull(r, seed2[:])
	n64 += int64(n)
	m.seed2 = seed2

	n, err = io.ReadFull(r, uint32Bytes)
	n64 += int64(n)
	m.rounds = binary.LittleEndian.Uint32(uint32Bytes)

	return n64, err
}

func (k *KeepassXDatabase) decryptPayload(content []byte, seed [16]byte, seed2 [32]byte, rounds uint32, flags uint32, encryption [16]byte) ([]byte, error) {
	var err error
	var data []byte
	return data, err
}

func (k *KeepassXDatabase) parsePayload(payload []byte) error {
	return nil
}

func (k *KeepassXDatabase) ReadFrom(r io.Reader) (int64, error) {
	n, err := k.Metadata.ReadFrom(r)
	if err != nil {
		return n, err
	}
	var content []byte
	_, err = io.ReadFull(r, content[:])
	payload, err := k.decryptPayload(content, k.seed, k.seed2, k.rounds, k.flags, k.encryption)
	err = k.parsePayload(payload)
	return n, err
}

func main() {
	path := os.Args[1]
	f, err := os.OpenFile(path, os.O_RDONLY, 0)
	if err != nil {
		log.Fatalf("%v", err)
	}
	defer f.Close()
	db, err := NewKeepassXDatabase()
	if err != nil {
		log.Fatalf("%v", err)
	}
	_, err = db.ReadFrom(f)
	if err != nil {
		log.Fatalf("%v", err)
	}
}
<|endoftext|>


<commit_before>package main

import (
	"bufio"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"os/user"
	"strings"
	"sync"
)

type database struct {
	AppServer string
	DbServer  string
	DbName    string
	User      string
	Pass      string
}

func main() {
	usr, err := user.Current()
	if err != nil {
		log.Fatalf("Couldn't obtain the current user err=%v", err)
	}

	databasesFile := usr.HomeDir + "/.databases.json"
	databases := map[string]database{}

	byts, err := ioutil.ReadFile(databasesFile)
	if err != nil {
		log.Fatalf("Couldn't read [%v] file. err=%v", databasesFile, err)
	}

	err = json.Unmarshal(byts, &databases)
	if err != nil {
		log.Fatalf("Couldn't unmarshal [%v] file. err=%v", databasesFile, err)
	}

	if len(databases) == 0 {
		log.Fatalf("Couldn't find any database configurations in [%v] file.", databasesFile)
	}

	sql := readInput(os.Stdin)
	if len(sql) <= 3 {
		log.Fatal("No SQL to run. Exiting.")
	}

	if len(os.Args[1:]) == 0 {
		log.Fatal("Target database unspecified; where should I run the query?")
	}

	targetDatabases := []string{}
	for _, k := range os.Args[1:] {
		if _, ok := databases[k]; k != "all" && !ok {
			log.Fatalf("Target database unknown: [%v]", k)
		}
		if k == "all" {
			targetDatabases = nil
			for k := range databases {
				targetDatabases = append(targetDatabases, k)
			}
			break
		}
		targetDatabases = append(targetDatabases, k)
	}

	var wg sync.WaitGroup

	for _, k := range targetDatabases {
		wg.Add(1)
		go func(db database, k string) {
			defer wg.Done()
			runSQL(db, sql, k, len(targetDatabases) > 1)
		}(databases[k], k)
	}

	wg.Wait()
}

func runSQL(db database, sql string, key string, prependKey bool) {
	userOption := ""
	if db.User != "" {
		userOption = fmt.Sprintf("-u %v ", db.User)
	}

	passOption := ""
	if db.Pass != "" {
		passOption = fmt.Sprintf("-p%v ", db.Pass)
	}

	hostOption := ""
	if db.DbServer != "" {
		hostOption = fmt.Sprintf("-h %v ", db.DbServer)
	}

	prepend := ""
	if prependKey {
		prepend = key + "\t"
	}

	mysql := "mysql"
	options := fmt.Sprintf(" -Nsr %v%v%v %v -e ", userOption, passOption, hostOption, db.DbName)
	query := fmt.Sprintf("\" %v \"", sql)

	var cmd *exec.Cmd
	if db.AppServer != "" {
		cmd = exec.Command("ssh", db.AppServer, mysql+options+query)
	} else {
		args := append(trimEmpty(strings.Split(options, " ")), sql)
		cmd = exec.Command("mysql", args...)
	}

	stdout, err := cmd.StdoutPipe()
	if err != nil {
		log.Printf("Cannot create pipe for running command on %v; not running.\n", key)
		return
	}

	if err := cmd.Start(); err != nil {
		log.Printf("Cannot start command on %v; not running.\n", key)
		return
	}

	scanner := bufio.NewScanner(stdout)
	for scanner.Scan() {
		fmt.Println(prepend + scanner.Text())
	}

	cmd.Process.Kill()
}

func readInput(r io.Reader) string {
	ls := []string{}
	var err error
	rd := bufio.NewReader(r)

	for {
		var s string
		s, err = rd.ReadString('\n')
		if err == io.EOF {
			return strings.Join(ls, " ")
		}
		s = strings.TrimSpace(s)
		if len(s) == 0 {
			continue
		}
		ls = append(ls, strings.TrimSpace(s))
	}
}

func trimEmpty(s []string) []string {
	var r []string
	for _, str := range s {
		if str != "" {
			r = append(r, str)
		}
	}
	return r
}
<commit_msg>Escapes single quotes in ssh-mode.<commit_after>package main

import (
	"bufio"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"os/user"
	"strings"
	"sync"
)

type database struct {
	AppServer string
	DbServer  string
	DbName    string
	User      string
	Pass      string
}

func main() {
	usr, err := user.Current()
	if err != nil {
		log.Fatalf("Couldn't obtain the current user err=%v", err)
	}

	databasesFile := usr.HomeDir + "/.databases.json"
	databases := map[string]database{}

	byts, err := ioutil.ReadFile(databasesFile)
	if err != nil {
		log.Fatalf("Couldn't read [%v] file. err=%v", databasesFile, err)
	}

	err = json.Unmarshal(byts, &databases)
	if err != nil {
		log.Fatalf("Couldn't unmarshal [%v] file. err=%v", databasesFile, err)
	}

	if len(databases) == 0 {
		log.Fatalf("Couldn't find any database configurations in [%v] file.", databasesFile)
	}

	sql := readInput(os.Stdin)
	if len(sql) <= 3 {
		log.Fatal("No SQL to run. Exiting.")
	}

	if len(os.Args[1:]) == 0 {
		log.Fatal("Target database unspecified; where should I run the query?")
	}

	targetDatabases := []string{}
	for _, k := range os.Args[1:] {
		if _, ok := databases[k]; k != "all" && !ok {
			log.Fatalf("Target database unknown: [%v]", k)
		}
		if k == "all" {
			targetDatabases = nil
			for k := range databases {
				targetDatabases = append(targetDatabases, k)
			}
			break
		}
		targetDatabases = append(targetDatabases, k)
	}

	var wg sync.WaitGroup

	for _, k := range targetDatabases {
		wg.Add(1)
		go func(db database, k string) {
			defer wg.Done()
			runSQL(db, sql, k, len(targetDatabases) > 1)
		}(databases[k], k)
	}

	wg.Wait()
}

func runSQL(db database, sql string, key string, prependKey bool) {
	userOption := ""
	if db.User != "" {
		userOption = fmt.Sprintf("-u %v ", db.User)
	}

	passOption := ""
	if db.Pass != "" {
		passOption = fmt.Sprintf("-p%v ", db.Pass)
	}

	hostOption := ""
	if db.DbServer != "" {
		hostOption = fmt.Sprintf("-h %v ", db.DbServer)
	}

	prepend := ""
	if prependKey {
		prepend = key + "\t"
	}

	mysql := "mysql"
	options := fmt.Sprintf(" -Nsr %v%v%v%v -e ", userOption, passOption, hostOption, db.DbName)

	var cmd *exec.Cmd
	if db.AppServer != "" {
		query := fmt.Sprintf(`'%v'`, strings.Replace(sql, `'`, `'"'"'`, -1))
		cmd = exec.Command("ssh", db.AppServer, mysql+options+query)
	} else {
		args := append(trimEmpty(strings.Split(options, " ")), sql)
		cmd = exec.Command("mysql", args...)
	}

	stdout, err := cmd.StdoutPipe()
	if err != nil {
		log.Printf("Cannot create pipe for running command on %v; not running.\n", key)
		return
	}

	if err := cmd.Start(); err != nil {
		log.Printf("Cannot start command on %v; not running.\n", key)
		return
	}

	scanner := bufio.NewScanner(stdout)
	for scanner.Scan() {
		fmt.Println(prepend + scanner.Text())
	}

	cmd.Process.Kill()
}

func readInput(r io.Reader) string {
	ls := []string{}
	var err error
	rd := bufio.NewReader(r)

	for {
		var s string
		s, err = rd.ReadString('\n')
		if err == io.EOF {
			return strings.Join(ls, " ")
		}
		s = strings.TrimSpace(s)
		if len(s) == 0 {
			continue
		}
		ls = append(ls, strings.TrimSpace(s))
	}
}

func trimEmpty(s []string) []string {
	var r []string
	for _, str := range s {
		if str != "" {
			r = append(r, str)
		}
	}
	return r
}
<|endoftext|>


<commit_before>// Copyright 2013 mongoapi authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"flag"
	"fmt"
	"github.com/bmizerany/pat"
	"log"
	"net/http"
)

const version = "0.1.1"

var printVersion bool

func init() {
	flag.BoolVar(&printVersion, "v", false, "Print version and exit")
	flag.Parse()
}

func buildMux() http.Handler {
	m := pat.New()
	m.Post("/resources", http.HandlerFunc(Add))
	m.Post("/resources/:name", Handler(Bind))
	m.Del("/resources/:name/hostname/:hostname", Handler(Unbind))
	m.Del("/resources/:name", Handler(Remove))
	m.Get("/resources/:name/status", Handler(Status))
	return m
}

func main() {
	if printVersion {
		fmt.Printf("mongoapi version %s", version)
		return
	}
	listen := coalesceEnv("0.0.0.0:3333", "MONGODB_API_LISTEN")
	log.Fatal(http.ListenAndServe(listen, buildMux()))
}
<commit_msg>main: bump to 0.2.0<commit_after>// Copyright 2013 mongoapi authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"flag"
	"fmt"
	"github.com/bmizerany/pat"
	"log"
	"net/http"
)

const version = "0.2.0"

var printVersion bool

func init() {
	flag.BoolVar(&printVersion, "v", false, "Print version and exit")
	flag.Parse()
}

func buildMux() http.Handler {
	m := pat.New()
	m.Post("/resources", http.HandlerFunc(Add))
	m.Post("/resources/:name", Handler(Bind))
	m.Del("/resources/:name/hostname/:hostname", Handler(Unbind))
	m.Del("/resources/:name", Handler(Remove))
	m.Get("/resources/:name/status", Handler(Status))
	return m
}

func main() {
	if printVersion {
		fmt.Printf("mongoapi version %s", version)
		return
	}
	listen := coalesceEnv("0.0.0.0:3333", "MONGODB_API_LISTEN")
	log.Fatal(http.ListenAndServe(listen, buildMux()))
}
<|endoftext|>


<commit_before>package main

import (
	"math/rand"
	"strings"
	"time"

	log "github.com/Sirupsen/logrus"
	"github.com/danielkrainas/gobag/cmd"
	"github.com/danielkrainas/gobag/context"

	_ "github.com/danielkrainas/shex/cmd/channels"
	_ "github.com/danielkrainas/shex/cmd/games"
	_ "github.com/danielkrainas/shex/cmd/mods"
	_ "github.com/danielkrainas/shex/cmd/profiles"
	//_ "github.com/danielkrainas/shex/cmd/add"
	_ "github.com/danielkrainas/shex/cmd/cache"
	//_ "github.com/danielkrainas/shex/cmd/export"
	//_ "github.com/danielkrainas/shex/cmd/install"
	//_ "github.com/danielkrainas/shex/cmd/list"
	//_ "github.com/danielkrainas/shex/cmd/remove"
	"github.com/danielkrainas/shex/cmd/root"
	//_ "github.com/danielkrainas/shex/cmd/set"
	//_ "github.com/danielkrainas/shex/cmd/sync"
	//_ "github.com/danielkrainas/shex/cmd/uninstall"
	//_ "github.com/danielkrainas/shex/cmd/use"
	_ "github.com/danielkrainas/shex/cmd/version"
)

var appVersion string

const defaultVersion = "0.0.0-dev"

func main() {
	if appVersion == "" {
		appVersion = defaultVersion
	}

	rand.Seed(time.Now().Unix())
	ctx := acontext.WithVersion(acontext.Background(), appVersion)
	ctx = context.WithValue(ctx, "app.name", strings.Title(root.Info.Use))

	dispatch := cmd.CreateDispatcher(ctx, root.Info)
	if err := dispatch(); err != nil {
		log.Fatalln(err)
	}
}
<commit_msg>fix shex main<commit_after>package main

import (
	"context"
	"math/rand"
	"strings"
	"time"

	log "github.com/Sirupsen/logrus"
	"github.com/danielkrainas/gobag/cmd"
	"github.com/danielkrainas/gobag/context"

	_ "github.com/danielkrainas/shex/cmd/channels"
	_ "github.com/danielkrainas/shex/cmd/games"
	_ "github.com/danielkrainas/shex/cmd/mods"
	_ "github.com/danielkrainas/shex/cmd/profiles"
	//_ "github.com/danielkrainas/shex/cmd/add"
	_ "github.com/danielkrainas/shex/cmd/cache"
	//_ "github.com/danielkrainas/shex/cmd/export"
	//_ "github.com/danielkrainas/shex/cmd/install"
	//_ "github.com/danielkrainas/shex/cmd/list"
	//_ "github.com/danielkrainas/shex/cmd/remove"
	"github.com/danielkrainas/shex/cmd/root"
	//_ "github.com/danielkrainas/shex/cmd/set"
	//_ "github.com/danielkrainas/shex/cmd/sync"
	//_ "github.com/danielkrainas/shex/cmd/uninstall"
	//_ "github.com/danielkrainas/shex/cmd/use"
	_ "github.com/danielkrainas/shex/cmd/version"
)

var appVersion string

const defaultVersion = "0.0.0-dev"

func main() {
	if appVersion == "" {
		appVersion = defaultVersion
	}

	rand.Seed(time.Now().Unix())
	ctx := acontext.WithVersion(acontext.Background(), appVersion)
	ctx = context.WithValue(ctx, "app.name", strings.Title(root.Info.Use))

	dispatch := cmd.CreateDispatcher(ctx, root.Info)
	if err := dispatch(); err != nil {
		log.Fatalln(err)
	}
}
<|endoftext|>