<commit_before>// Package c2go contains the main function for running the executable.
//
// Installation
//
//     go get -u github.com/elliotchance/c2go
//
// Usage
//
//     c2go myfile.c
//
package main

import (
	"flag"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"os/exec"
	"path"
	"path/filepath"
	"runtime"
	"strings"

	"errors"

	"github.com/elliotchance/c2go/ast"
	"github.com/elliotchance/c2go/preprocessor"
	"github.com/elliotchance/c2go/program"
	"github.com/elliotchance/c2go/transpiler"
)

// Version can be requested through the command line with:
//
//     c2go -v
//
// See https://github.com/elliotchance/c2go/wiki/Release-Process
const Version = "v0.18.7 Tantalum 2017-12-18"

var stderr io.Writer = os.Stderr

// ProgramArgs defines the options available when processing the program. There
// is no constructor since the zeroed out values are the appropriate defaults -
// you need only set the options you need.
//
// TODO: Better separation on CLI modes
// https://github.com/elliotchance/c2go/issues/134
//
// Do not instantiate this directly. Instead use DefaultProgramArgs(); then
// modify any specific attributes.
type ProgramArgs struct {
	verbose     bool
	ast         bool
	inputFiles  []string
	clangFlags  []string
	outputFile  string
	packageName string

	// A private option to output the Go as a *_test.go file.
	outputAsTest bool
}

// DefaultProgramArgs default value of ProgramArgs
func DefaultProgramArgs() ProgramArgs {
	return ProgramArgs{
		verbose:      false,
		ast:          false,
		packageName:  "main",
		clangFlags:   []string{},
		outputAsTest: false,
	}
}

func readAST(data []byte) []string {
	return strings.Split(string(data), "\n")
}

type treeNode struct {
	indent int
	node   ast.Node
}

func convertLinesToNodes(lines []string) []treeNode {
	nodes := make([]treeNode, len(lines))
	var counter int
	for _, line := range lines {
		if strings.TrimSpace(line) == "" {
			continue
		}

		// It is tempting to discard null AST nodes, but these may
		// have semantic importance: for example, they represent omitted
		// for-loop conditions, as in for(;;).
		line = strings.Replace(line, "<<<NULL>>>", "NullStmt", 1)
		trimmed := strings.TrimLeft(line, "|\\- `")
		node := ast.Parse(trimmed)
		indentLevel := (len(line) - len(trimmed)) / 2
		nodes[counter] = treeNode{indentLevel, node}
		counter++
	}
	nodes = nodes[0:counter]

	return nodes
}

func convertLinesToNodesParallel(lines []string) []treeNode {
	// function f separate full list on 2 parts and
	// then each part can recursive run function f
	var f func([]string, int) []treeNode

	f = func(lines []string, deep int) []treeNode {
		deep = deep - 2
		part := len(lines) / 2

		var tr1 = make(chan []treeNode)
		var tr2 = make(chan []treeNode)

		go func(lines []string, deep int) {
			if deep <= 0 || len(lines) < deep {
				tr1 <- convertLinesToNodes(lines)
				return
			}
			tr1 <- f(lines, deep)
		}(lines[0:part], deep)

		go func(lines []string, deep int) {
			if deep <= 0 || len(lines) < deep {
				tr2 <- convertLinesToNodes(lines)
				return
			}
			tr2 <- f(lines, deep)
		}(lines[part:], deep)

		defer close(tr1)
		defer close(tr2)

		return append(<-tr1, <-tr2...)
	}

	// Parameter of deep - can be any, but effective to use
	// same amount of CPU
	return f(lines, runtime.NumCPU())
}

// buildTree converts an array of nodes, each prefixed with a depth into a tree.
func buildTree(nodes []treeNode, depth int) []ast.Node {
	if len(nodes) == 0 {
		return []ast.Node{}
	}

	// Split the list into sections, treat each section as a tree with its own
	// root.
	sections := [][]treeNode{}
	for _, node := range nodes {
		if node.indent == depth {
			sections = append(sections, []treeNode{node})
		} else {
			sections[len(sections)-1] = append(sections[len(sections)-1], node)
		}
	}

	results := []ast.Node{}
	for _, section := range sections {
		slice := []treeNode{}
		for _, n := range section {
			if n.indent > depth {
				slice = append(slice, n)
			}
		}

		children := buildTree(slice, depth+1)
		for _, child := range children {
			section[0].node.AddChild(child)
		}
		results = append(results, section[0].node)
	}

	return results
}

// Start begins transpiling an input file.
func Start(args ProgramArgs) (err error) {
	if args.verbose {
		fmt.Println("Start tanspiling ...")
	}

	if os.Getenv("GOPATH") == "" {
		return fmt.Errorf("The $GOPATH must be set")
	}

	// 1. Compile it first (checking for errors)
	for _, in := range args.inputFiles {
		_, err := os.Stat(in)
		if err != nil {
			return fmt.Errorf("Input file %s is not found", in)
		}
	}

	// 2. Preprocess
	if args.verbose {
		fmt.Println("Running clang preprocessor...")
	}

	pp, err := preprocessor.Analyze(args.inputFiles, args.clangFlags)
	if err != nil {
		return err
	}

	if args.verbose {
		fmt.Println("Writing preprocessor ...")
	}
	dir, err := ioutil.TempDir("", "c2go")
	if err != nil {
		return fmt.Errorf("Cannot create temp folder: %v", err)
	}
	defer os.RemoveAll(dir) // clean up

	ppFilePath := path.Join(dir, "pp.c")
	err = ioutil.WriteFile(ppFilePath, pp, 0644)
	if err != nil {
		return fmt.Errorf("writing to %s failed: %v", ppFilePath, err)
	}

	// 3. Generate JSON from AST
	if args.verbose {
		fmt.Println("Running clang for AST tree...")
	}
	astPP, err := exec.Command("clang", "-Xclang", "-ast-dump", "-fsyntax-only", "-fno-color-diagnostics", ppFilePath).Output()
	if err != nil {
		// If clang fails it still prints out the AST, so we have to run it
		// again to get the real error.
		errBody, _ := exec.Command("clang", ppFilePath).CombinedOutput()

		panic("clang failed: " + err.Error() + ":\n\n" + string(errBody))
	}

	if args.verbose {
		fmt.Println("Reading clang AST tree...")
	}
	lines := readAST(astPP)
	if args.ast {
		for _, l := range lines {
			fmt.Println(l)
		}
		fmt.Println()

		return nil
	}

	p := program.NewProgram()
	p.Verbose = args.verbose
	p.OutputAsTest = args.outputAsTest

	// Converting to nodes
	if args.verbose {
		fmt.Println("Converting to nodes...")
	}
	nodes := convertLinesToNodesParallel(lines)

	// build tree
	if args.verbose {
		fmt.Println("Building tree...")
	}
	tree := buildTree(nodes, 0)
	ast.FixPositions(tree)

	// Repair the floating literals. See RepairFloatingLiteralsFromSource for
	// more information.
	floatingErrors := ast.RepairFloatingLiteralsFromSource(tree[0], ppFilePath)

	for _, fErr := range floatingErrors {
		message := fmt.Sprintf("could not read exact floating literal: %s",
			fErr.Err.Error())
		p.AddMessage(p.GenerateWarningMessage(errors.New(message), fErr.Node))
	}

	outputFilePath := args.outputFile

	if outputFilePath == "" {
		// Choose inputFile for creating name of output file
		input := args.inputFiles[0]
		// We choose name for output Go code at the base
		// on filename for choosed input file
		cleanFileName := filepath.Clean(filepath.Base(input))
		extension := filepath.Ext(input)
		outputFilePath = cleanFileName[0:len(cleanFileName)-len(extension)] + ".go"
	}

	// transpile ast tree
	if args.verbose {
		fmt.Println("Transpiling tree...")
	}

	err = transpiler.TranspileAST(args.outputFile, args.packageName, p, tree[0].(ast.Node))
	if err != nil {
		return fmt.Errorf("cannot transpile AST : %v", err)
	}

	// write the output Go code
	if args.verbose {
		fmt.Println("Writing the output Go code...")
	}
	err = ioutil.WriteFile(outputFilePath, []byte(p.String()), 0644)
	if err != nil {
		return fmt.Errorf("writing Go output file failed: %v", err)
	}

	return nil
}

type inputDataFlags []string

func (i *inputDataFlags) String() (s string) {
	for pos, item := range *i {
		s += fmt.Sprintf("Flag %d. %s\n", pos, item)
	}
	return
}

func (i *inputDataFlags) Set(value string) error {
	*i = append(*i, value)
	return nil
}

var clangFlags inputDataFlags

func init() {
	transpileCommand.Var(&clangFlags, "clang-flag", "Pass arguments to clang. You may provide multiple -clang-flag items.")
}

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

func main() {
	code := runCommand()
	if code != 0 {
		os.Exit(code)
	}
}

func runCommand() int {

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

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

	transpileCommand.SetOutput(stderr)
	astCommand.SetOutput(stderr)

	flag.Parse()

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

	if flag.NArg() < 1 {
		flag.Usage()
		return 1
	}

	args := DefaultProgramArgs()

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

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

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

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

		args.inputFiles = transpileCommand.Args()
		args.outputFile = *outputFlag
		args.packageName = *packageFlag
		args.verbose = *verboseFlag
		args.clangFlags = clangFlags
	default:
		flag.Usage()
		return 1
	}

	if err := Start(args); err != nil {
		fmt.Printf("Error: %v\n", err)
		return 1
	}

	return 0
}
<commit_msg>Bump version: v0.19.0 Uranium 2017-12-19<commit_after>// Package c2go contains the main function for running the executable.
//
// Installation
//
//     go get -u github.com/elliotchance/c2go
//
// Usage
//
//     c2go myfile.c
//
package main

import (
	"flag"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"os/exec"
	"path"
	"path/filepath"
	"runtime"
	"strings"

	"errors"

	"github.com/elliotchance/c2go/ast"
	"github.com/elliotchance/c2go/preprocessor"
	"github.com/elliotchance/c2go/program"
	"github.com/elliotchance/c2go/transpiler"
)

// Version can be requested through the command line with:
//
//     c2go -v
//
// See https://github.com/elliotchance/c2go/wiki/Release-Process
const Version = "v0.19.0 Uranium 2017-12-19"

var stderr io.Writer = os.Stderr

// ProgramArgs defines the options available when processing the program. There
// is no constructor since the zeroed out values are the appropriate defaults -
// you need only set the options you need.
//
// TODO: Better separation on CLI modes
// https://github.com/elliotchance/c2go/issues/134
//
// Do not instantiate this directly. Instead use DefaultProgramArgs(); then
// modify any specific attributes.
type ProgramArgs struct {
	verbose     bool
	ast         bool
	inputFiles  []string
	clangFlags  []string
	outputFile  string
	packageName string

	// A private option to output the Go as a *_test.go file.
	outputAsTest bool
}

// DefaultProgramArgs default value of ProgramArgs
func DefaultProgramArgs() ProgramArgs {
	return ProgramArgs{
		verbose:      false,
		ast:          false,
		packageName:  "main",
		clangFlags:   []string{},
		outputAsTest: false,
	}
}

func readAST(data []byte) []string {
	return strings.Split(string(data), "\n")
}

type treeNode struct {
	indent int
	node   ast.Node
}

func convertLinesToNodes(lines []string) []treeNode {
	nodes := make([]treeNode, len(lines))
	var counter int
	for _, line := range lines {
		if strings.TrimSpace(line) == "" {
			continue
		}

		// It is tempting to discard null AST nodes, but these may
		// have semantic importance: for example, they represent omitted
		// for-loop conditions, as in for(;;).
		line = strings.Replace(line, "<<<NULL>>>", "NullStmt", 1)
		trimmed := strings.TrimLeft(line, "|\\- `")
		node := ast.Parse(trimmed)
		indentLevel := (len(line) - len(trimmed)) / 2
		nodes[counter] = treeNode{indentLevel, node}
		counter++
	}
	nodes = nodes[0:counter]

	return nodes
}

func convertLinesToNodesParallel(lines []string) []treeNode {
	// function f separate full list on 2 parts and
	// then each part can recursive run function f
	var f func([]string, int) []treeNode

	f = func(lines []string, deep int) []treeNode {
		deep = deep - 2
		part := len(lines) / 2

		var tr1 = make(chan []treeNode)
		var tr2 = make(chan []treeNode)

		go func(lines []string, deep int) {
			if deep <= 0 || len(lines) < deep {
				tr1 <- convertLinesToNodes(lines)
				return
			}
			tr1 <- f(lines, deep)
		}(lines[0:part], deep)

		go func(lines []string, deep int) {
			if deep <= 0 || len(lines) < deep {
				tr2 <- convertLinesToNodes(lines)
				return
			}
			tr2 <- f(lines, deep)
		}(lines[part:], deep)

		defer close(tr1)
		defer close(tr2)

		return append(<-tr1, <-tr2...)
	}

	// Parameter of deep - can be any, but effective to use
	// same amount of CPU
	return f(lines, runtime.NumCPU())
}

// buildTree converts an array of nodes, each prefixed with a depth into a tree.
func buildTree(nodes []treeNode, depth int) []ast.Node {
	if len(nodes) == 0 {
		return []ast.Node{}
	}

	// Split the list into sections, treat each section as a tree with its own
	// root.
	sections := [][]treeNode{}
	for _, node := range nodes {
		if node.indent == depth {
			sections = append(sections, []treeNode{node})
		} else {
			sections[len(sections)-1] = append(sections[len(sections)-1], node)
		}
	}

	results := []ast.Node{}
	for _, section := range sections {
		slice := []treeNode{}
		for _, n := range section {
			if n.indent > depth {
				slice = append(slice, n)
			}
		}

		children := buildTree(slice, depth+1)
		for _, child := range children {
			section[0].node.AddChild(child)
		}
		results = append(results, section[0].node)
	}

	return results
}

// Start begins transpiling an input file.
func Start(args ProgramArgs) (err error) {
	if args.verbose {
		fmt.Println("Start tanspiling ...")
	}

	if os.Getenv("GOPATH") == "" {
		return fmt.Errorf("The $GOPATH must be set")
	}

	// 1. Compile it first (checking for errors)
	for _, in := range args.inputFiles {
		_, err := os.Stat(in)
		if err != nil {
			return fmt.Errorf("Input file %s is not found", in)
		}
	}

	// 2. Preprocess
	if args.verbose {
		fmt.Println("Running clang preprocessor...")
	}

	pp, err := preprocessor.Analyze(args.inputFiles, args.clangFlags)
	if err != nil {
		return err
	}

	if args.verbose {
		fmt.Println("Writing preprocessor ...")
	}
	dir, err := ioutil.TempDir("", "c2go")
	if err != nil {
		return fmt.Errorf("Cannot create temp folder: %v", err)
	}
	defer os.RemoveAll(dir) // clean up

	ppFilePath := path.Join(dir, "pp.c")
	err = ioutil.WriteFile(ppFilePath, pp, 0644)
	if err != nil {
		return fmt.Errorf("writing to %s failed: %v", ppFilePath, err)
	}

	// 3. Generate JSON from AST
	if args.verbose {
		fmt.Println("Running clang for AST tree...")
	}
	astPP, err := exec.Command("clang", "-Xclang", "-ast-dump", "-fsyntax-only", "-fno-color-diagnostics", ppFilePath).Output()
	if err != nil {
		// If clang fails it still prints out the AST, so we have to run it
		// again to get the real error.
		errBody, _ := exec.Command("clang", ppFilePath).CombinedOutput()

		panic("clang failed: " + err.Error() + ":\n\n" + string(errBody))
	}

	if args.verbose {
		fmt.Println("Reading clang AST tree...")
	}
	lines := readAST(astPP)
	if args.ast {
		for _, l := range lines {
			fmt.Println(l)
		}
		fmt.Println()

		return nil
	}

	p := program.NewProgram()
	p.Verbose = args.verbose
	p.OutputAsTest = args.outputAsTest

	// Converting to nodes
	if args.verbose {
		fmt.Println("Converting to nodes...")
	}
	nodes := convertLinesToNodesParallel(lines)

	// build tree
	if args.verbose {
		fmt.Println("Building tree...")
	}
	tree := buildTree(nodes, 0)
	ast.FixPositions(tree)

	// Repair the floating literals. See RepairFloatingLiteralsFromSource for
	// more information.
	floatingErrors := ast.RepairFloatingLiteralsFromSource(tree[0], ppFilePath)

	for _, fErr := range floatingErrors {
		message := fmt.Sprintf("could not read exact floating literal: %s",
			fErr.Err.Error())
		p.AddMessage(p.GenerateWarningMessage(errors.New(message), fErr.Node))
	}

	outputFilePath := args.outputFile

	if outputFilePath == "" {
		// Choose inputFile for creating name of output file
		input := args.inputFiles[0]
		// We choose name for output Go code at the base
		// on filename for choosed input file
		cleanFileName := filepath.Clean(filepath.Base(input))
		extension := filepath.Ext(input)
		outputFilePath = cleanFileName[0:len(cleanFileName)-len(extension)] + ".go"
	}

	// transpile ast tree
	if args.verbose {
		fmt.Println("Transpiling tree...")
	}

	err = transpiler.TranspileAST(args.outputFile, args.packageName, p, tree[0].(ast.Node))
	if err != nil {
		return fmt.Errorf("cannot transpile AST : %v", err)
	}

	// write the output Go code
	if args.verbose {
		fmt.Println("Writing the output Go code...")
	}
	err = ioutil.WriteFile(outputFilePath, []byte(p.String()), 0644)
	if err != nil {
		return fmt.Errorf("writing Go output file failed: %v", err)
	}

	return nil
}

type inputDataFlags []string

func (i *inputDataFlags) String() (s string) {
	for pos, item := range *i {
		s += fmt.Sprintf("Flag %d. %s\n", pos, item)
	}
	return
}

func (i *inputDataFlags) Set(value string) error {
	*i = append(*i, value)
	return nil
}

var clangFlags inputDataFlags

func init() {
	transpileCommand.Var(&clangFlags, "clang-flag", "Pass arguments to clang. You may provide multiple -clang-flag items.")
}

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

func main() {
	code := runCommand()
	if code != 0 {
		os.Exit(code)
	}
}

func runCommand() int {

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

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

	transpileCommand.SetOutput(stderr)
	astCommand.SetOutput(stderr)

	flag.Parse()

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

	if flag.NArg() < 1 {
		flag.Usage()
		return 1
	}

	args := DefaultProgramArgs()

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

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

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

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

		args.inputFiles = transpileCommand.Args()
		args.outputFile = *outputFlag
		args.packageName = *packageFlag
		args.verbose = *verboseFlag
		args.clangFlags = clangFlags
	default:
		flag.Usage()
		return 1
	}

	if err := Start(args); err != nil {
		fmt.Printf("Error: %v\n", err)
		return 1
	}

	return 0
}
<|endoftext|>


<commit_before>package main

import (
	"github.com/realglobe-Inc/edo/driver"
	"github.com/realglobe-Inc/edo/util"
	"github.com/realglobe-Inc/go-lib-rg/erro"
	"github.com/realglobe-Inc/go-lib-rg/rglog"
	"net/http"
	"os"
)

var exitCode = 0

func exit() {
	if exitCode != 0 {
		os.Exit(exitCode)
	}
}

func main() {
	defer exit()
	defer rglog.Flush()

	hndl := util.InitLog("github.com/realglobe-Inc")

	param, err := parseParameters(os.Args...)
	if err != nil {
		log.Err(erro.Unwrap(err))
		log.Debug(err)
		exitCode = 1
		return
	}

	hndl.SetLevel(param.consLv)
	if err := util.SetupLog("github.com/realglobe-Inc", param.logType, param.logLv, param.acpLogPath, param.fluAddr, param.acpFluTag); err != nil {
		log.Err(erro.Unwrap(err))
		log.Debug(err)
		exitCode = 1
		return
	}

	if err := mainCore(param); err != nil {
		err = erro.Wrap(err)
		log.Err(erro.Unwrap(err))
		log.Debug(err)
		exitCode = 1
		return
	}

	log.Info("Shut down.")
}

// system を準備する。
func mainCore(param *parameters) error {

	var priKeyCont driver.KeyValueStore
	switch param.priKeyContType {
	case "file":
		priKeyCont = driver.NewFileKeyValueStore(param.priKeyContPath, func(before string) string {
			return before + ".pem"
		}, nil, func(data []byte) (interface{}, error) {
			return util.ParseRsaPrivateKey(string(data))
		}, param.caExpiDur)
		log.Info("Use file code container " + param.priKeyContPath + ".")
	default:
		return erro.New("invalid code container type " + param.priKeyContType + ".")
	}

	sys := newSystem(
		priKeyCont,
		param.taId,
		param.hashName,
	)
	return serve(sys, param.acpSocType, param.acpSocPath, param.acpSocPort, param.acpProtType)
}

// 振り分ける。
const (
	proxyApiPath = "/"
)

func serve(sys *system, socType, socPath string, socPort int, protType string) error {
	routes := map[string]util.HandlerFunc{
		proxyApiPath: wrapper(func(w http.ResponseWriter, r *http.Request) error {
			return proxyApi(sys, w, r)
		}),
	}
	return util.Serve(socType, socPath, socPort, protType, routes)
}

func wrapper(f func(http.ResponseWriter, *http.Request) error) func(http.ResponseWriter, *http.Request) error {
	return func(w http.ResponseWriter, r *http.Request) error {
		if err := f(w, r); err != nil {
			err = erro.Wrap(err)
			w.Header().Set(headerAccProxErr, "error")
			return err
		}
		return nil
	}
}
<commit_msg>X-Edo-Access-Proxy-Error の値を具体的にした<commit_after>package main

import (
	"github.com/realglobe-Inc/edo/driver"
	"github.com/realglobe-Inc/edo/util"
	"github.com/realglobe-Inc/go-lib-rg/erro"
	"github.com/realglobe-Inc/go-lib-rg/rglog"
	"net/http"
	"os"
)

var exitCode = 0

func exit() {
	if exitCode != 0 {
		os.Exit(exitCode)
	}
}

func main() {
	defer exit()
	defer rglog.Flush()

	hndl := util.InitLog("github.com/realglobe-Inc")

	param, err := parseParameters(os.Args...)
	if err != nil {
		log.Err(erro.Unwrap(err))
		log.Debug(err)
		exitCode = 1
		return
	}

	hndl.SetLevel(param.consLv)
	if err := util.SetupLog("github.com/realglobe-Inc", param.logType, param.logLv, param.acpLogPath, param.fluAddr, param.acpFluTag); err != nil {
		log.Err(erro.Unwrap(err))
		log.Debug(err)
		exitCode = 1
		return
	}

	if err := mainCore(param); err != nil {
		err = erro.Wrap(err)
		log.Err(erro.Unwrap(err))
		log.Debug(err)
		exitCode = 1
		return
	}

	log.Info("Shut down.")
}

// system を準備する。
func mainCore(param *parameters) error {

	var priKeyCont driver.KeyValueStore
	switch param.priKeyContType {
	case "file":
		priKeyCont = driver.NewFileKeyValueStore(param.priKeyContPath, func(before string) string {
			return before + ".pem"
		}, nil, func(data []byte) (interface{}, error) {
			return util.ParseRsaPrivateKey(string(data))
		}, param.caExpiDur)
		log.Info("Use file code container " + param.priKeyContPath + ".")
	default:
		return erro.New("invalid code container type " + param.priKeyContType + ".")
	}

	sys := newSystem(
		priKeyCont,
		param.taId,
		param.hashName,
	)
	return serve(sys, param.acpSocType, param.acpSocPath, param.acpSocPort, param.acpProtType)
}

// 振り分ける。
const (
	proxyApiPath = "/"
)

func serve(sys *system, socType, socPath string, socPort int, protType string) error {
	routes := map[string]util.HandlerFunc{
		proxyApiPath: wrapper(func(w http.ResponseWriter, r *http.Request) error {
			return proxyApi(sys, w, r)
		}),
	}
	return util.Serve(socType, socPath, socPort, protType, routes)
}

func wrapper(f func(http.ResponseWriter, *http.Request) error) func(http.ResponseWriter, *http.Request) error {
	return func(w http.ResponseWriter, r *http.Request) error {
		if err := f(w, r); err != nil {
			err = erro.Wrap(err)
			var msg string
			for baseErr := err; msg == ""; {
				switch e := erro.Unwrap(baseErr).(type) {
				case *util.HttpStatusError:
					if e.Cause() == nil {
						msg = e.Message()
					} else {
						baseErr = e.Cause()
					}
				case *erro.Tracer:
					baseErr = e.Cause()
				default:
					msg = e.Error()
				}
			}
			w.Header().Set(headerAccProxErr, msg)
			return err
		}
		return nil
	}
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"os"
	"strings"
	"time"

	"github.com/cloudfoundry-incubator/cf-debug-server"
	"github.com/cloudfoundry-incubator/cf-lager"
	"github.com/cloudfoundry-incubator/converger/converger_process"
	"github.com/cloudfoundry-incubator/converger/lrpreprocessor"
	"github.com/cloudfoundry-incubator/converger/lrpwatcher"
	Bbs "github.com/cloudfoundry-incubator/runtime-schema/bbs"
	"github.com/cloudfoundry-incubator/runtime-schema/bbs/shared"
	"github.com/cloudfoundry-incubator/runtime-schema/heartbeater"
	"github.com/cloudfoundry/gunk/group_runner"
	"github.com/cloudfoundry/gunk/timeprovider"
	"github.com/cloudfoundry/storeadapter"
	"github.com/cloudfoundry/storeadapter/etcdstoreadapter"
	"github.com/cloudfoundry/storeadapter/workerpool"
	"github.com/nu7hatch/gouuid"
	"github.com/pivotal-golang/lager"
	"github.com/tedsuo/ifrit"
	"github.com/tedsuo/ifrit/sigmon"
)

var etcdCluster = flag.String(
	"etcdCluster",
	"http://127.0.0.1:4001",
	"comma-separated list of etcd addresses (http://ip:port)",
)

var convergeRepeatInterval = flag.Duration(
	"convergeRepeatInterval",
	30*time.Second,
	"the interval, in seconds, between runs of the converge process",
)

var kickPendingTaskDuration = flag.Duration(
	"kickPendingTaskDuration",
	30*time.Second,
	"the interval, in seconds, between kicks to pending tasks",
)

var expirePendingTaskDuration = flag.Duration(
	"expirePendingTaskDuration",
	30*time.Minute,
	"unclaimed tasks are marked as failed, after this time (in seconds)",
)

var kickPendingLRPStartAuctionDuration = flag.Duration(
	"kickPendingLRPStartAuctionDuration",
	30*time.Second,
	"the interval, in seconds, between kicks to pending start auctions for long-running process",
)

var expireClaimedLRPStartAuctionDuration = flag.Duration(
	"expireClaimedLRPStartAuctionDuration",
	300*time.Second,
	"unclaimed start auctions for long-running processes are deleted, after this time (in seconds)",
)

func main() {
	flag.Parse()

	logger := cf_lager.New("converger")

	etcdAdapter := initializeStore(logger)
	bbs := Bbs.NewConvergerBBS(etcdAdapter, timeprovider.NewTimeProvider(), logger)

	cf_debug_server.Run()

	uuid, err := uuid.NewV4()
	if err != nil {
		logger.Fatal("Couldn't generate uuid", err)
	}

	heartbeater := heartbeater.New(etcdAdapter, shared.LockSchemaPath("converge_lock"), uuid.String(), *convergeRepeatInterval, logger)

	converger := converger_process.New(
		bbs,
		logger,
		*convergeRepeatInterval,
		*kickPendingTaskDuration,
		*expirePendingTaskDuration,
		*kickPendingLRPStartAuctionDuration,
		*expireClaimedLRPStartAuctionDuration,
	)

	watcher := lrpwatcher.New(bbs, lrpreprocessor.New(bbs), logger)

	monitor := sigmon.New(group_runner.New([]group_runner.Member{
		{"heartbeater", heartbeater},
		{"converger", converger},
		{"watcher", watcher},
	}))

	process := ifrit.Envoke(monitor)

	logger.Info("started")

	err = <-process.Wait()
	if err != nil {
		logger.Error("exited-with-failure", err)
		os.Exit(1)
	}

	logger.Info("exited")
}

func initializeStore(logger lager.Logger) storeadapter.StoreAdapter {
	etcdAdapter := etcdstoreadapter.NewETCDStoreAdapter(
		strings.Split(*etcdCluster, ","),
		workerpool.NewWorkerPool(10),
	)

	err := etcdAdapter.Connect()
	if err != nil {
		logger.Fatal("failed-to-connect-to-etcd", err)
	}

	return etcdAdapter
}
<commit_msg>say started before acquiring lock<commit_after>package main

import (
	"flag"
	"os"
	"strings"
	"time"

	"github.com/cloudfoundry-incubator/cf-debug-server"
	"github.com/cloudfoundry-incubator/cf-lager"
	"github.com/cloudfoundry-incubator/converger/converger_process"
	"github.com/cloudfoundry-incubator/converger/lrpreprocessor"
	"github.com/cloudfoundry-incubator/converger/lrpwatcher"
	Bbs "github.com/cloudfoundry-incubator/runtime-schema/bbs"
	"github.com/cloudfoundry-incubator/runtime-schema/bbs/shared"
	"github.com/cloudfoundry-incubator/runtime-schema/heartbeater"
	"github.com/cloudfoundry/gunk/group_runner"
	"github.com/cloudfoundry/gunk/timeprovider"
	"github.com/cloudfoundry/storeadapter"
	"github.com/cloudfoundry/storeadapter/etcdstoreadapter"
	"github.com/cloudfoundry/storeadapter/workerpool"
	"github.com/nu7hatch/gouuid"
	"github.com/pivotal-golang/lager"
	"github.com/tedsuo/ifrit"
	"github.com/tedsuo/ifrit/sigmon"
)

var etcdCluster = flag.String(
	"etcdCluster",
	"http://127.0.0.1:4001",
	"comma-separated list of etcd addresses (http://ip:port)",
)

var convergeRepeatInterval = flag.Duration(
	"convergeRepeatInterval",
	30*time.Second,
	"the interval, in seconds, between runs of the converge process",
)

var kickPendingTaskDuration = flag.Duration(
	"kickPendingTaskDuration",
	30*time.Second,
	"the interval, in seconds, between kicks to pending tasks",
)

var expirePendingTaskDuration = flag.Duration(
	"expirePendingTaskDuration",
	30*time.Minute,
	"unclaimed tasks are marked as failed, after this time (in seconds)",
)

var kickPendingLRPStartAuctionDuration = flag.Duration(
	"kickPendingLRPStartAuctionDuration",
	30*time.Second,
	"the interval, in seconds, between kicks to pending start auctions for long-running process",
)

var expireClaimedLRPStartAuctionDuration = flag.Duration(
	"expireClaimedLRPStartAuctionDuration",
	300*time.Second,
	"unclaimed start auctions for long-running processes are deleted, after this time (in seconds)",
)

func main() {
	flag.Parse()

	logger := cf_lager.New("converger")

	etcdAdapter := initializeStore(logger)
	bbs := Bbs.NewConvergerBBS(etcdAdapter, timeprovider.NewTimeProvider(), logger)

	cf_debug_server.Run()

	uuid, err := uuid.NewV4()
	if err != nil {
		logger.Fatal("Couldn't generate uuid", err)
	}

	heartbeater := heartbeater.New(etcdAdapter, shared.LockSchemaPath("converge_lock"), uuid.String(), *convergeRepeatInterval, logger)

	converger := converger_process.New(
		bbs,
		logger,
		*convergeRepeatInterval,
		*kickPendingTaskDuration,
		*expirePendingTaskDuration,
		*kickPendingLRPStartAuctionDuration,
		*expireClaimedLRPStartAuctionDuration,
	)

	watcher := lrpwatcher.New(bbs, lrpreprocessor.New(bbs), logger)

	monitor := sigmon.New(group_runner.New([]group_runner.Member{
		{"heartbeater", heartbeater},
		{"converger", converger},
		{"watcher", watcher},
	}))

	logger.Info("started-waiting-for-lock")

	process := ifrit.Envoke(monitor)

	logger.Info("acquired-lock")

	err = <-process.Wait()
	if err != nil {
		logger.Error("exited-with-failure", err)
		os.Exit(1)
	}

	logger.Info("exited")
}

func initializeStore(logger lager.Logger) storeadapter.StoreAdapter {
	etcdAdapter := etcdstoreadapter.NewETCDStoreAdapter(
		strings.Split(*etcdCluster, ","),
		workerpool.NewWorkerPool(10),
	)

	err := etcdAdapter.Connect()
	if err != nil {
		logger.Fatal("failed-to-connect-to-etcd", err)
	}

	return etcdAdapter
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"os"

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

var (
	version = "0.3.0"

	flag            = pflag.NewFlagSet("ltsvp", pflag.ContinueOnError)
	list            = pflag.StringP("keys", "k", "", "")
	outputDelimiter = pflag.StringP("output-delimiter", "d", "\t", "")
	remainLTSV      = pflag.BoolP("remain-ltsv", "r", false, "")
	isHelp          = pflag.BoolP("help", "h", false, "")
	isVersion       = pflag.BoolP("version", "v", false, "")
)

func printUsage() {
	os.Stderr.WriteString(`
Usage: ltsvp OPTION... [FILE]...
Print selected parts of LTSV from each FILE to standard output.

Options:
  -k, --keys=LIST
                 select only these keys (required)
  -D, --output-delimiter=STRING
                 use STRING to separate parts (default: \t)
  -r, --remain-ltsv
                 print selected parts as LTSV
  --help
                 display this help text and exit
  --version
                 output version information and exit

LIST is made up of keys separated by commas.
  host           # Select host
  host,time,ua   # Select host, time, and ua
`[1:])
}

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

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

func printErr(err error) {
	fmt.Fprintln(os.Stderr, "ltsvp:", err)
}

func guideToHelp() {
	os.Stderr.WriteString(`
Try 'ltsvp --help' for more information.
`[1:])
}

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

	keys := ParseKeysList(*list)
	r, err := argf.From(flag.Args())
	if err != nil {
		printErr(err)
		return 2
	}

	l := NewLTSVScanner(keys, r)
	l.OutputDelimiter = *outputDelimiter
	l.RemainLTSV = *remainLTSV
	if err := do(l); err != nil {
		printErr(err)
		return 1
	}
	return 0
}

func main() {
	e := _main()
	os.Exit(e)
}
<commit_msg>Extract &main.name<commit_after>package main

import (
	"fmt"
	"os"

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

var (
	name    = "ltsvp"
	version = "0.3.0"

	flag            = pflag.NewFlagSet(name, pflag.ContinueOnError)
	list            = pflag.StringP("keys", "k", "", "")
	outputDelimiter = pflag.StringP("output-delimiter", "d", "\t", "")
	remainLTSV      = pflag.BoolP("remain-ltsv", "r", false, "")
	isHelp          = pflag.BoolP("help", "h", false, "")
	isVersion       = pflag.BoolP("version", "v", false, "")
)

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

Options:
  -k, --keys=LIST
                 select only these keys (required)
  -D, --output-delimiter=STRING
                 use STRING to separate parts (default: \t)
  -r, --remain-ltsv
                 print selected parts as LTSV
  --help
                 display this help text and exit
  --version
                 output version information and exit

LIST is made up of keys separated by commas.
  host           # Select host
  host,time,ua   # Select host, time, and ua
`[1:], name)
}

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

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

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

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

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

	keys := ParseKeysList(*list)
	r, err := argf.From(flag.Args())
	if err != nil {
		printErr(err)
		return 2
	}

	l := NewLTSVScanner(keys, r)
	l.OutputDelimiter = *outputDelimiter
	l.RemainLTSV = *remainLTSV
	if err := do(l); err != nil {
		printErr(err)
		return 1
	}
	return 0
}

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


<commit_before>package main

import (
	"os"

	"github.com/github/hub/commands"
	"github.com/github/hub/github"
	"github.com/github/hub/ui"
)

func main() {
	defer github.CaptureCrash()

	err := commands.CmdRunner.Execute()
	if !err.Ran {
		ui.Errorln(err.Error())
	}
	os.Exit(err.ExitCode)
}
<commit_msg>Enforce go1.8+ via build flags too<commit_after>// +build go1.8

package main

import (
	"os"

	"github.com/github/hub/commands"
	"github.com/github/hub/github"
	"github.com/github/hub/ui"
)

func main() {
	defer github.CaptureCrash()

	err := commands.CmdRunner.Execute()
	if !err.Ran {
		ui.Errorln(err.Error())
	}
	os.Exit(err.ExitCode)
}
<|endoftext|>


<commit_before>package main

import (
	"log"

	"github.com/soygul/gcm-ccs"
)

func main() {
	c, err := ccs.Connect(Conf.GCM.CCSHost, Conf.GCM.SenderID, Conf.GCM.APIKey, Conf.App.Debug)
	if err != nil {
		log.Fatalf("NBusy messege server failed to start.")
	}
	log.Println("NBusy messege server started.")

	for {
		m, err := c.Receive()
		if err != nil {
			log.Printf("Incoming CCS error: %v\n", err)
		}
		go readHandler(m)
	}
}

func readHandler(m *ccs.InMsg) {
	log.Printf("Incoming CCS message: %v\n", m)
}
<commit_msg>fix typo<commit_after>package main

import (
	"log"

	"github.com/soygul/gcm-ccs"
)

func main() {
	c, err := ccs.Connect(Conf.GCM.CCSHost, Conf.GCM.SenderID, Conf.GCM.APIKey, Conf.App.Debug)
	if err != nil {
		log.Fatalf("NBusy message server failed to start.")
	}
	log.Println("NBusy message server started.")

	for {
		m, err := c.Receive()
		if err != nil {
			log.Printf("Incoming CCS error: %v\n", err)
		}
		go readHandler(m)
	}
}

func readHandler(m *ccs.InMsg) {
	log.Printf("Incoming CCS message: %v\n", m)
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"os"
)

const aURL = "http://artii.herokuapp.com"

func main() {
	args := os.Args[1:]

	if len(args) == 0 {
		fmt.Printf("Usage:\n")
		return
	}

	switch args[0] {
	case "fonts":
		fmt.Printf("%v", fontList())
	}

	fmt.Println(draw(args[0]))
}

func fontList() string {
	url := aURL + "/fonts_list"

	resp, err := http.Get(url)
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatal(err)
	}

	s := string(body) + "\n"
	return s
}

func draw(s string) string {
	url := fmt.Sprintf("%s/make?text=%s", aURL, s)

	resp, err := http.Get(url)
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatal(err)
	}

	a := string(body)
	return a
}
<commit_msg>Address when args passed to cli are in quotes<commit_after>package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"strings"
)

const aURL = "http://artii.herokuapp.com"

func main() {
	args := os.Args[1:]

	if len(args) == 0 {
		fmt.Printf("Usage:\n")
		return
	}

	switch args[0] {
	case "fonts":
		fmt.Printf("%v", fontList())
		return
	}

	fmt.Println(draw(args))
}

func fontList() string {
	url := aURL + "/fonts_list"

	resp, err := http.Get(url)
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatal(err)
	}

	s := string(body) + "\n"
	return s
}

func draw(s []string) string {
	f := strings.Split(s[0], " ")
	js := strings.Join(f, "+")
	url := fmt.Sprintf("%s/make?text=%s", aURL, js)

	resp, err := http.Get(url)
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatal(err)
	}

	a := string(body)
	return a
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"image"
	"image/png"
	"log"
	"net/http"
	"os"
	"strings"

	// Load all the file formats
	_ "code.google.com/p/go.image/bmp"
	_ "code.google.com/p/go.image/tiff"
	_ "github.com/ftrvxmtrx/groke/image/pcx"
	_ "github.com/jeffallen/g/image/xbm"
	_ "github.com/knieriem/g/image/pnm"
	_ "github.com/mewrnd/blizzconv/images/cel"
	_ "github.com/mewrnd/blizzconv/images/cl2"
	_ "image/gif"
	_ "image/jpeg"
)

func convert(w http.ResponseWriter, r *http.Request) {
	f, fh, err := r.FormFile("img")
	if err != nil {
		http.Error(w, "Missing image data.", 500)
		return
	}

	img, kind, err := image.Decode(f)
	if err != nil || img == nil {
		http.Error(w, err.Error(), 500)
		return
	}

	fn := fh.Filename
	dot := strings.LastIndex(fn, ".")
	if dot >= 0 {
		fn = fn[0:dot]
	}
	fn = fmt.Sprintf("%v.png", fn)

	log.Print("Image filename: ", fh.Filename,
		" type: ", fh.Header["Content-Type"],
		" decoded-as: ", kind,
		" new-fn: ", fn)

	w.Header().Add("Content-Disposition",
		fmt.Sprintf("attachment; filename=\"%v\"", fn))
	err = png.Encode(w, img)
	if err != nil {
		http.Error(w, err.Error(), 500)
	}
	return
}

func main() {
	log.Println("Starting with args:", os.Args)
	http.HandleFunc("/convert", convert)
	http.Handle("/", http.FileServer(http.Dir("static")))

	log.Fatal(http.ListenAndServe(":8080", nil))
}
<commit_msg>go back to using upstream now that they fixed the bug<commit_after>package main

import (
	"fmt"
	"image"
	"image/png"
	"log"
	"net/http"
	"os"
	"strings"

	// Load all the file formats
	_ "code.google.com/p/go.image/bmp"
	_ "code.google.com/p/go.image/tiff"
	_ "github.com/ftrvxmtrx/groke/image/pcx"
	_ "github.com/knieriem/g/image/xbm"
	_ "github.com/knieriem/g/image/pnm"
	_ "github.com/mewrnd/blizzconv/images/cel"
	_ "github.com/mewrnd/blizzconv/images/cl2"
	_ "image/gif"
	_ "image/jpeg"
)

func convert(w http.ResponseWriter, r *http.Request) {
	f, fh, err := r.FormFile("img")
	if err != nil {
		http.Error(w, "Missing image data.", 500)
		return
	}

	img, kind, err := image.Decode(f)
	if err != nil || img == nil {
		http.Error(w, err.Error(), 500)
		return
	}

	fn := fh.Filename
	dot := strings.LastIndex(fn, ".")
	if dot >= 0 {
		fn = fn[0:dot]
	}
	fn = fmt.Sprintf("%v.png", fn)

	log.Print("Image filename: ", fh.Filename,
		" type: ", fh.Header["Content-Type"],
		" decoded-as: ", kind,
		" new-fn: ", fn)

	w.Header().Add("Content-Disposition",
		fmt.Sprintf("attachment; filename=\"%v\"", fn))
	err = png.Encode(w, img)
	if err != nil {
		http.Error(w, err.Error(), 500)
	}
	return
}

func main() {
	log.Println("Starting with args:", os.Args)
	http.HandleFunc("/convert", convert)
	http.Handle("/", http.FileServer(http.Dir("static")))

	log.Fatal(http.ListenAndServe(":8080", nil))
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"os"
	"os/exec"
	"os/signal"
	"regexp"
	"strings"

	"github.com/ninjasphere/gatt"
	"github.com/ninjasphere/go-ninja/logger"
)

var log = logger.GetLogger("driver-go-blecombined")
var fpDriver *FlowerPowerDriver
var wpDriver *WaypointDriver
var tagDriver *BLETagDriver
var client *gatt.Client //kill me
var sent = false

func main() {

	log.Infof("BLE Driver Starting")
	out, err := exec.Command("hciconfig").Output()
	if err != nil {
		log.Errorf(fmt.Sprintf("Error: %s", err))
	}
	re := regexp.MustCompile("([0-9A-F]{2}\\:{0,1}){6}")
	mac := strings.Replace(re.FindString(string(out)), ":", "", -1)
	log.Infof("The local mac is %s\n", mac)

	client = &gatt.Client{
		StateChange: func(newState string) {
			log.Infof("Client state change: %s", newState)
		},
	}

	fpDriver, err = NewFlowerPowerDriver(client)
	if err != nil {
		log.Errorf("Failed to create FlowerPower driver: ", err)
	}

	wpDriver, err = NewWaypointDriver(client)
	if err != nil {
		log.FatalError(err, "Failed to create waypoint driver")
	}

	tagDriver, err = NewBLETagDriver(client)
	if err != nil {
		log.FatalError(err, "Failed to create BLE Tag driver")
	}

	client.Advertisement = handleAdvertisement

	log.Debugf("Starting client scan")
	err = client.Start()
	if err != nil {
		log.FatalError(err, "Failed to start client")
	}

	err = client.StartScanning(true)
	if err != nil {
		log.FatalError(err, "Failed to start scanning")
	}

	//----------------------------------------------------------------------------------------

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

	// Block until a signal is received.
	s := <-c
	fmt.Println("Got signal:", s)
}

func handleAdvertisement(device *gatt.DiscoveredDevice) {
	if device.Advertisement.LocalName == "NinjaSphereWaypoint" {
		log.Debugf("Found waypoint %s", device.Address)
		wpDriver.handleSphereWaypoint(device)
	}

	for uuid := range device.Advertisement.ServiceUuids {
		if uuid == flowerPowerServiceUuid {
			if fpDriver.announcedFlowerPowers[device.Address] {
				return
			}
			log.Debugf("Found Flower Power %s", device.Address)
			err := NewFlowerPower(fpDriver, device)
			if err != nil {
				log.Errorf("Error creating FlowerPower device ", err)
			}
		}
	}

	for uuid := range device.Advertisement.ServiceUuids {
		if uuid == stickNFindServiceUuid {
			err := NewBLETag(tagDriver, device)
			if err != nil {
				log.Errorf("Error creating BLE Tag device ", err)
			}
		}
	}
}
<commit_msg>test: time bomb<commit_after>package main

import (
	"fmt"
	"os"
	"os/exec"
	"os/signal"
	"regexp"
	"strings"
	"time"

	"github.com/ninjasphere/gatt"
	"github.com/ninjasphere/go-ninja/logger"
)

var log = logger.GetLogger("driver-go-blecombined")
var fpDriver *FlowerPowerDriver
var wpDriver *WaypointDriver
var tagDriver *BLETagDriver
var client *gatt.Client //kill me
var sent = false

func main() {

	log.Infof("BLE Driver Starting")
	out, err := exec.Command("hciconfig").Output()
	if err != nil {
		log.Errorf(fmt.Sprintf("Error: %s", err))
	}
	re := regexp.MustCompile("([0-9A-F]{2}\\:{0,1}){6}")
	mac := strings.Replace(re.FindString(string(out)), ":", "", -1)
	log.Infof("The local mac is %s\n", mac)

	client = &gatt.Client{
		StateChange: func(newState string) {
			log.Infof("Client state change: %s", newState)
		},
	}

	fpDriver, err = NewFlowerPowerDriver(client)
	if err != nil {
		log.Errorf("Failed to create FlowerPower driver: ", err)
	}

	wpDriver, err = NewWaypointDriver(client)
	if err != nil {
		log.FatalError(err, "Failed to create waypoint driver")
	}

	tagDriver, err = NewBLETagDriver(client)
	if err != nil {
		log.FatalError(err, "Failed to create BLE Tag driver")
	}

	client.Advertisement = handleAdvertisement

	log.Debugf("Starting client scan")
	err = client.Start()
	if err != nil {
		log.FatalError(err, "Failed to start client")
	}

	err = client.StartScanning(true)
	if err != nil {
		log.FatalError(err, "Failed to start scanning")
	}

	timer := time.NewTimer(time.Second * 10)
	go func() {
		<-timer.C
		log.Infof("hurk")
		panic("barf")
	}()

	//----------------------------------------------------------------------------------------

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

	// Block until a signal is received.
	s := <-c
	fmt.Println("Got signal:", s)
}

func handleAdvertisement(device *gatt.DiscoveredDevice) {
	if device.Advertisement.LocalName == "NinjaSphereWaypoint" {
		log.Debugf("Found waypoint %s", device.Address)
		wpDriver.handleSphereWaypoint(device)
	}

	for uuid := range device.Advertisement.ServiceUuids {
		if uuid == flowerPowerServiceUuid {
			if fpDriver.announcedFlowerPowers[device.Address] {
				return
			}
			log.Debugf("Found Flower Power %s", device.Address)
			err := NewFlowerPower(fpDriver, device)
			if err != nil {
				log.Errorf("Error creating FlowerPower device ", err)
			}
		}
	}

	for uuid := range device.Advertisement.ServiceUuids {
		if uuid == stickNFindServiceUuid {
			err := NewBLETag(tagDriver, device)
			if err != nil {
				log.Errorf("Error creating BLE Tag device ", err)
			}
		}
	}
}
<|endoftext|>


<commit_before>package main

import (
	"os"

	"github.com/Sirupsen/logrus"
	"github.com/codegangsta/cli"
	"github.com/datawolf/index-cli/action"
)

var Version = "0.0.0"

func exit(err error) {
	if err != nil {
		logrus.Fatal(err)
	}
}

func beforeApp(c *cli.Context) error {
	if c.Bool("debug") {
		logrus.SetLevel(logrus.DebugLevel)
	}

	return nil
}

func main() {
	app := cli.NewApp()
	app.Name = os.Args[0]
	app.Version = Version
	app.Usage = "The cli tool to access the index and hub."
	app.Before = beforeApp
	app.EnableBashCompletion = true
	app.Authors = []cli.Author{
		cli.Author{
			Name:  "w00291922",
			Email: "long.wanglong@huawei.com",
		},
	}

	app.Flags = []cli.Flag{
		cli.BoolFlag{
			Name:  "debug, d",
			Usage: "Debug logging",
		},
	}

	app.Commands = []cli.Command{
		{
			Name:     "login",
			Usage:    "Login the hub",
			HideHelp: true,
			Action:   action.Login,
		},
		{
			Name:     "logout",
			Usage:    "Logout the hub",
			HideHelp: true,
			Action:   action.Logout,
		},
		{
			Name:     "status",
			Usage:    "Get the status of the rnd-dockerhub",
			HideHelp: true,
			Action:   action.Status,
		},
		{
			Name:     "userinfo",
			Usage:    "Get the user info of yourself",
			HideHelp: true,
			Action:   action.UserInfo,
		},
		{
			Name:        "user",
			ShortName:   "u",
			Usage:       "Control user",
			HideHelp:    true,
			Subcommands: userSubcommand(),
		},
		{
			Name:        "repo",
			ShortName:   "r",
			Usage:       "Control the Repository",
			HideHelp:    true,
			Subcommands: repoSubcommand(),
		},
		{
			Name:     "search",
			Usage:    "Search rnd-dockerhub for images",
			HideHelp: true,
			Action:   action.Search,
			Flags: []cli.Flag{
				cli.StringFlag{
					Name:  "stars, s",
					Usage: "Only displays with at least x stars",
				},
			},
		},
	}

	exit(app.Run(os.Args))
}

func repoSubcommand() []cli.Command {
	return []cli.Command{
		{
			Name:   "get",
			Usage:  "Get Specific Repository's Property",
			Action: action.RepoGetProperty,
		},
		{
			Name:   "set",
			Usage:  "Set Specific Repository's Property",
			Action: action.RepoSetProperty,
			Flags: []cli.Flag{
				cli.StringFlag{
					Name:  "access, a",
					Usage: "Set access level for an images(private,public,protect)",
				},
				cli.StringFlag{
					Name:  "description, d",
					Usage: "Set description an images",
				},
			},
		},
		{
			Name:   "del",
			Usage:  "Delete Specific Repository",
			Action: action.RepoDelete,
		},
		{
			Name:   "rmi",
			Usage:  "Delete Repository With Specific tag",
			Action: action.RepoDeleteTag,
		},
	}
}

func userSubcommand() []cli.Command {
	return []cli.Command{
		{
			Name:   "create",
			Usage:  "create an user",
			Action: action.CreateUser,
		},
		{
			Name:   "update",
			Usage:  "update an user",
			Action: action.UpdateUser,
		},
	}
}
<commit_msg>update the cli description<commit_after>package main

import (
	"os"

	"github.com/Sirupsen/logrus"
	"github.com/codegangsta/cli"
	"github.com/datawolf/index-cli/action"
)

var Version = "0.0.0"

func exit(err error) {
	if err != nil {
		logrus.Fatal(err)
	}
}

func beforeApp(c *cli.Context) error {
	if c.Bool("debug") {
		logrus.SetLevel(logrus.DebugLevel)
	}

	return nil
}

func main() {
	app := cli.NewApp()
	app.Name = os.Args[0]
	app.Version = Version
	app.Usage = "The cli tool that access rnd-dockerhub.huawei.com."
	app.Before = beforeApp
	app.EnableBashCompletion = true
	app.Authors = []cli.Author{
		cli.Author{
			Name:  "w00291922",
			Email: "long.wanglong@huawei.com",
		},
	}

	app.Flags = []cli.Flag{
		cli.BoolFlag{
			Name:  "debug, d",
			Usage: "Debug logging",
		},
	}

	app.Commands = []cli.Command{
		{
			Name:     "login",
			Usage:    "Login the hub",
			HideHelp: true,
			Action:   action.Login,
		},
		{
			Name:     "logout",
			Usage:    "Logout the hub",
			HideHelp: true,
			Action:   action.Logout,
		},
		{
			Name:     "status",
			Usage:    "Get the status of the rnd-dockerhub",
			HideHelp: true,
			Action:   action.Status,
		},
		{
			Name:     "userinfo",
			Usage:    "Get the user info of yourself",
			HideHelp: true,
			Action:   action.UserInfo,
		},
		{
			Name:        "user",
			ShortName:   "u",
			Usage:       "Control user",
			HideHelp:    true,
			Subcommands: userSubcommand(),
		},
		{
			Name:        "repo",
			ShortName:   "r",
			Usage:       "Control the Repository",
			HideHelp:    true,
			Subcommands: repoSubcommand(),
		},
		{
			Name:     "search",
			Usage:    "Search rnd-dockerhub for images",
			HideHelp: true,
			Action:   action.Search,
			Flags: []cli.Flag{
				cli.StringFlag{
					Name:  "stars, s",
					Usage: "Only displays with at least x stars",
				},
			},
		},
	}

	exit(app.Run(os.Args))
}

func repoSubcommand() []cli.Command {
	return []cli.Command{
		{
			Name:   "get",
			Usage:  "Get Specific Repository's Property",
			Action: action.RepoGetProperty,
		},
		{
			Name:   "set",
			Usage:  "Set Specific Repository's Property",
			Action: action.RepoSetProperty,
			Flags: []cli.Flag{
				cli.StringFlag{
					Name:  "access, a",
					Usage: "Set access level for an images(private,public,protect)",
				},
				cli.StringFlag{
					Name:  "description, d",
					Usage: "Set description an images",
				},
			},
		},
		{
			Name:   "del",
			Usage:  "Delete Specific Repository",
			Action: action.RepoDelete,
		},
		{
			Name:   "rmi",
			Usage:  "Delete Repository With Specific tag",
			Action: action.RepoDeleteTag,
		},
	}
}

func userSubcommand() []cli.Command {
	return []cli.Command{
		{
			Name:   "create",
			Usage:  "create an user",
			Action: action.CreateUser,
		},
		{
			Name:   "update",
			Usage:  "update an user",
			Action: action.UpdateUser,
		},
	}
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
)

func main() {
	fmt.Println("DUECES")
}
<commit_msg>ALH: Created traffic command<commit_after>package main

import (
	"fmt"
	"log"
	"os"
	"os/exec"
)

// Listen to status codes from unix commands.
// 0 - Green.. it's done
// 1 - Red.. something went wrong
// other - Yellow.. still in progress

// Change status bar color

func main() {
	args := os.Args[1:]

	if len(args) == 0 {
		log.Fatalln("Not enough arguments")
	}

	cmd := exec.Command(args[0], args[1:]...)

	data, err := cmd.Output()
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Print(string(data))
}
<|endoftext|>


<commit_before>package main

import (
	"bufio"
	"encoding/binary"
	"flag"
	"io"
	"io/ioutil"
	"log"
	"math"
	"os"
	"path/filepath"
	"runtime"
	"syscall"
	"sync"
)

type block struct {
	filePath    string
	numBytes    uint16
	buffer      []byte
	startOfFile bool
	endOfFile   bool
	uid         int
	gid         int
	mode        os.FileMode
}

var blockSize uint16
var verbose bool
var logger *log.Logger

const dataBlockFlag byte = 1 << 0
const startOfFileFlag byte = 1 << 1
const endOfFileFlag byte = 1 << 2

func main() {
	extract := flag.Bool("x", false, "extract archive")
	create := flag.Bool("c", false, "create archive")
	inputFileName := flag.String("i", "", "input file for extraction; defaults to stdin (-x only)")
	outputFileName := flag.String("o", "", "output file for creation; defaults to stdout (-c only)")
	requestedBlockSize := flag.Uint("block-size", 4096, "internal block-size (-c only)")
	dirReaderCount := flag.Int("dir-readers", 16, "number of simultaneous directory readers (-c only)")
	fileReaderCount := flag.Int("file-readers", 16, "number of simultaneous file readers (-c only)")
	directoryScanQueueSize := flag.Int("queue-dir", 128, "queue size for scanning directories (-c only)")
	fileReadQueueSize := flag.Int("queue-read", 128, "queue size for reading files (-c only)")
	fileWriteQueueSize := flag.Int("queue-write", 128, "queue size for archive write (-c only); increasing can cause increased memory usage")
	multiCpu := flag.Int("multicpu", 1, "maximum number of CPUs that can be executing simultaneously")

	flag.BoolVar(&verbose, "v", false, "verbose output on stderr")
	flag.Parse()

	runtime.GOMAXPROCS(*multiCpu)

	logger = log.New(os.Stderr, "", 0)

	if *requestedBlockSize > math.MaxUint16 {
		logger.Fatalln("block-size must be less than or equal to", math.MaxUint16)
	}
	blockSize = uint16(*requestedBlockSize)

	if *extract {
		var inputFile *os.File
		if *inputFileName != "" {
			file, err := os.Open(*inputFileName)
			if err != nil {
				logger.Fatalln("Error opening input file:", err.Error())
			}
			inputFile = file
		} else {
			inputFile = os.Stdin
		}

		bufferedInputFile := bufio.NewReader(inputFile)
		archiveReader(bufferedInputFile)
		inputFile.Close()

	} else if *create {
		if flag.NArg() == 0 {
			logger.Fatalln("Directories to archive must be specified")
		}

		var directoryScanQueue = make(chan string, *directoryScanQueueSize)
		var fileReadQueue = make(chan string, *fileReadQueueSize)
		var fileWriteQueue = make(chan block, *fileWriteQueueSize)
		var workInProgress sync.WaitGroup

		var outputFile *os.File
		if *outputFileName != "" {
			file, err := os.Create(*outputFileName)
			if err != nil {
				logger.Fatalln("Error creating output file:", err.Error())
			}
			outputFile = file
		} else {
			outputFile = os.Stdout
		}

		bufferedOutputFile := bufio.NewWriter(outputFile)
		go archiveWriter(bufferedOutputFile, fileWriteQueue, &workInProgress)
		for i := 0; i < *dirReaderCount; i++ {
			go directoryScanner(directoryScanQueue, fileReadQueue, &workInProgress)
		}
		for i := 0; i < *fileReaderCount; i++ {
			go fileReader(fileReadQueue, fileWriteQueue, &workInProgress)
		}

		for i := 0; i < flag.NArg(); i++ {
			workInProgress.Add(1)
			directoryScanQueue <- flag.Arg(i)
		}

		workInProgress.Wait()
		close(directoryScanQueue)
		close(fileReadQueue)
		close(fileWriteQueue)
		bufferedOutputFile.Flush()
		outputFile.Close()
	} else {
		logger.Fatalln("extract (-x) or create (-c) flag must be provided")
	}
}

func directoryScanner(directoryScanQueue chan string, fileReadQueue chan string, workInProgress *sync.WaitGroup) {
	for directoryPath := range directoryScanQueue {
		if verbose {
			logger.Println(directoryPath)
		}

		files, err := ioutil.ReadDir(directoryPath)
		if err == nil {
			workInProgress.Add(len(files))
			for _, file := range files {
				filePath := filepath.Join(directoryPath, file.Name())
				if file.IsDir() {
					directoryScanQueue <- filePath
				} else {
					fileReadQueue <- filePath
				}
			}
		} else {
			logger.Println("directory read error:", err.Error())
		}

		workInProgress.Done()
	}
}

func fileReader(fileReadQueue <-chan string, fileWriterQueue chan block, workInProgress *sync.WaitGroup) {
	for filePath := range fileReadQueue {
		if verbose {
			logger.Println(filePath)
		}

		file, err := os.Open(filePath)
		if err == nil {
			workInProgress.Add(1)

			var uid int = 0
			var gid int = 0
			var mode os.FileMode = 0
			fi, err := file.Stat()
			if err == nil {
				sysinfo := fi.Sys()
				if sysinfo != nil {
					stat_t := sysinfo.(*syscall.Stat_t)
					if stat_t != nil {
						uid = int(stat_t.Uid)
						gid = int(stat_t.Gid)
					}
				}
				mode = fi.Mode()
			}

			fileWriterQueue <- block{filePath, 0, nil, true, false, uid, gid, mode}

			bufferedFile := bufio.NewReader(file)

			for {
				buffer := make([]byte, blockSize)
				bytesRead, err := bufferedFile.Read(buffer)
				if err == io.EOF {
					break
				} else if err != nil {
					logger.Println("file read error; file contents will be incomplete:", err.Error())
					break
				}

				workInProgress.Add(1)
				fileWriterQueue <- block{filePath, uint16(bytesRead), buffer, false, false, 0, 0, 0}
			}

			workInProgress.Add(1)
			fileWriterQueue <- block{filePath, 0, nil, false, true, 0, 0, 0}

			file.Close()
		} else {
			logger.Println("file open error:", err.Error())
		}

		workInProgress.Done()
	}
}

func archiveWriter(output io.Writer, fileWriterQueue <-chan block, workInProgress *sync.WaitGroup) {
	flags := make([]byte, 1)

	for block := range fileWriterQueue {
		filePath := []byte(block.filePath)
		err := binary.Write(output, binary.BigEndian, uint16(len(filePath)))
		if err != nil {
			logger.Fatalln("Archive write error:", err.Error())
		}
		_, err = output.Write(filePath)
		if err != nil {
			logger.Fatalln("Archive write error:", err.Error())
		}

		if block.startOfFile {
			flags[0] = startOfFileFlag
			_, err = output.Write(flags)
			if err != nil {
				logger.Fatalln("Archive write error:", err.Error())
			}
			err = binary.Write(output, binary.BigEndian, uint32(block.uid))
			if err != nil {
				logger.Fatalln("Archive write error:", err.Error())
			}
			err = binary.Write(output, binary.BigEndian, uint32(block.gid))
			if err != nil {
				logger.Fatalln("Archive write error:", err.Error())
			}
			err = binary.Write(output, binary.BigEndian, block.mode)
			if err != nil {
				logger.Fatalln("Archive write error:", err.Error())
			}
		} else if block.endOfFile {
			flags[0] = endOfFileFlag
			_, err = output.Write(flags)
			if err != nil {
				logger.Fatalln("Archive write error:", err.Error())
			}
		} else {
			flags[0] = dataBlockFlag
			_, err = output.Write(flags)
			if err != nil {
				logger.Fatalln("Archive write error:", err.Error())
			}

			err = binary.Write(output, binary.BigEndian, uint16(block.numBytes))
			if err != nil {
				logger.Fatalln("Archive write error:", err.Error())
			}

			_, err = output.Write(block.buffer[:block.numBytes])
			if err != nil {
				logger.Fatalln("Archive write error:", err.Error())
			}
		}

		workInProgress.Done()
	}
}

func archiveReader(file io.Reader) {
	var workInProgress sync.WaitGroup
	fileOutputChan := make(map[string]chan block)

	for {
		var pathSize uint16
		err := binary.Read(file, binary.BigEndian, &pathSize)
		if err == io.EOF {
			break
		} else if err != nil {
			logger.Fatalln("Archive read error:", err.Error())
		}

		buf := make([]byte, pathSize)
		_, err = io.ReadFull(file, buf)
		if err != nil {
			logger.Fatalln("Archive read error:", err.Error())
		}
		filePath := string(buf)

		flag := make([]byte, 1)
		_, err = io.ReadFull(file, flag)
		if err != nil {
			logger.Fatalln("Archive read error:", err.Error())
		}

		if flag[0] == startOfFileFlag {
			var uid uint32
			var gid uint32
			var mode os.FileMode

			err = binary.Read(file, binary.BigEndian, &uid)
			if err != nil {
				logger.Fatalln("Archive read error:", err.Error())
			}

			err = binary.Read(file, binary.BigEndian, &gid)
			if err != nil {
				logger.Fatalln("Archive read error:", err.Error())
			}

			err = binary.Read(file, binary.BigEndian, &mode)
			if err != nil {
				logger.Fatalln("Archive read error:", err.Error())
			}

			c := make(chan block, 1)
			fileOutputChan[filePath] = c
			workInProgress.Add(1)
			go writeFile(c, &workInProgress)
			c <- block{filePath, 0, nil, true, false, int(uid), int(gid), mode}
		} else if flag[0] == endOfFileFlag {
			c := fileOutputChan[filePath]
			c <- block{filePath, 0, nil, false, true, 0, 0, 0}
			close(c)
			delete(fileOutputChan, filePath)
		} else if flag[0] == dataBlockFlag {
			var blockSize uint16
			err = binary.Read(file, binary.BigEndian, &blockSize)
			if err != nil {
				logger.Fatalln("Archive read error:", err.Error())
			}

			blockData := make([]byte, blockSize)
			_, err = io.ReadFull(file, blockData)
			if err != nil {
				logger.Fatalln("Archive read error:", err.Error())
			}

			c := fileOutputChan[filePath]
			c <- block{filePath, blockSize, blockData, false, false, 0, 0, 0}
		} else {
			logger.Fatalln("Archive error: unrecognized block flag", flag[0])
		}
	}

	workInProgress.Wait()
}

func writeFile(blockSource chan block, workInProgress *sync.WaitGroup) {
	var file *os.File = nil
	var bufferedFile *bufio.Writer
	for block := range blockSource {
		if block.startOfFile {
			if verbose {
				logger.Println(block.filePath)
			}

			dir, _ := filepath.Split(block.filePath)
			err := os.MkdirAll(dir, os.ModeDir|0755)
			if err != nil {
				logger.Fatalln("Directory create error:", err.Error())
			}

			tmp, err := os.Create(block.filePath)
			if err != nil {
				logger.Fatalln("File create error:", err.Error())
			}
			file = tmp
			bufferedFile = bufio.NewWriter(file)

			err = file.Chown(block.uid, block.gid)
			if err != nil {
				logger.Println("Unable to chown file to", block.uid, "/", block.gid, ":", err.Error())
			}
			err = file.Chmod(block.mode)
			if err != nil {
				logger.Println("Unable to chmod file to", block.mode, ":", err.Error())
			}
		} else if block.endOfFile {
			bufferedFile.Flush()
			file.Close()
			file = nil
		} else {
			_, err := bufferedFile.Write(block.buffer[:block.numBytes])
			if err != nil {
				logger.Fatalln("File write error:", err.Error())
			}
		}
	}
	workInProgress.Done()
}
<commit_msg>move workInProgress to be where it was queued<commit_after>package main

import (
	"bufio"
	"encoding/binary"
	"flag"
	"io"
	"io/ioutil"
	"log"
	"math"
	"os"
	"path/filepath"
	"runtime"
	"syscall"
	"sync"
)

type block struct {
	filePath    string
	numBytes    uint16
	buffer      []byte
	startOfFile bool
	endOfFile   bool
	uid         int
	gid         int
	mode        os.FileMode
}

var blockSize uint16
var verbose bool
var logger *log.Logger

const dataBlockFlag byte = 1 << 0
const startOfFileFlag byte = 1 << 1
const endOfFileFlag byte = 1 << 2

func main() {
	extract := flag.Bool("x", false, "extract archive")
	create := flag.Bool("c", false, "create archive")
	inputFileName := flag.String("i", "", "input file for extraction; defaults to stdin (-x only)")
	outputFileName := flag.String("o", "", "output file for creation; defaults to stdout (-c only)")
	requestedBlockSize := flag.Uint("block-size", 4096, "internal block-size (-c only)")
	dirReaderCount := flag.Int("dir-readers", 16, "number of simultaneous directory readers (-c only)")
	fileReaderCount := flag.Int("file-readers", 16, "number of simultaneous file readers (-c only)")
	directoryScanQueueSize := flag.Int("queue-dir", 128, "queue size for scanning directories (-c only)")
	fileReadQueueSize := flag.Int("queue-read", 128, "queue size for reading files (-c only)")
	fileWriteQueueSize := flag.Int("queue-write", 128, "queue size for archive write (-c only); increasing can cause increased memory usage")
	multiCpu := flag.Int("multicpu", 1, "maximum number of CPUs that can be executing simultaneously")

	flag.BoolVar(&verbose, "v", false, "verbose output on stderr")
	flag.Parse()

	runtime.GOMAXPROCS(*multiCpu)

	logger = log.New(os.Stderr, "", 0)

	if *requestedBlockSize > math.MaxUint16 {
		logger.Fatalln("block-size must be less than or equal to", math.MaxUint16)
	}
	blockSize = uint16(*requestedBlockSize)

	if *extract {
		var inputFile *os.File
		if *inputFileName != "" {
			file, err := os.Open(*inputFileName)
			if err != nil {
				logger.Fatalln("Error opening input file:", err.Error())
			}
			inputFile = file
		} else {
			inputFile = os.Stdin
		}

		bufferedInputFile := bufio.NewReader(inputFile)
		archiveReader(bufferedInputFile)
		inputFile.Close()

	} else if *create {
		if flag.NArg() == 0 {
			logger.Fatalln("Directories to archive must be specified")
		}

		var directoryScanQueue = make(chan string, *directoryScanQueueSize)
		var fileReadQueue = make(chan string, *fileReadQueueSize)
		var fileWriteQueue = make(chan block, *fileWriteQueueSize)
		var workInProgress sync.WaitGroup

		var outputFile *os.File
		if *outputFileName != "" {
			file, err := os.Create(*outputFileName)
			if err != nil {
				logger.Fatalln("Error creating output file:", err.Error())
			}
			outputFile = file
		} else {
			outputFile = os.Stdout
		}

		bufferedOutputFile := bufio.NewWriter(outputFile)
		go archiveWriter(bufferedOutputFile, fileWriteQueue, &workInProgress)
		for i := 0; i < *dirReaderCount; i++ {
			go directoryScanner(directoryScanQueue, fileReadQueue, &workInProgress)
		}
		for i := 0; i < *fileReaderCount; i++ {
			go fileReader(fileReadQueue, fileWriteQueue, &workInProgress)
		}

		for i := 0; i < flag.NArg(); i++ {
			workInProgress.Add(1)
			directoryScanQueue <- flag.Arg(i)
		}

		workInProgress.Wait()
		close(directoryScanQueue)
		close(fileReadQueue)
		close(fileWriteQueue)
		bufferedOutputFile.Flush()
		outputFile.Close()
	} else {
		logger.Fatalln("extract (-x) or create (-c) flag must be provided")
	}
}

func directoryScanner(directoryScanQueue chan string, fileReadQueue chan string, workInProgress *sync.WaitGroup) {
	for directoryPath := range directoryScanQueue {
		if verbose {
			logger.Println(directoryPath)
		}

		files, err := ioutil.ReadDir(directoryPath)
		if err == nil {
			workInProgress.Add(len(files))
			for _, file := range files {
				filePath := filepath.Join(directoryPath, file.Name())
				if file.IsDir() {
					directoryScanQueue <- filePath
				} else {
					fileReadQueue <- filePath
				}
			}
		} else {
			logger.Println("directory read error:", err.Error())
		}

		workInProgress.Done()
	}
}

func fileReader(fileReadQueue <-chan string, fileWriterQueue chan block, workInProgress *sync.WaitGroup) {
	for filePath := range fileReadQueue {
		if verbose {
			logger.Println(filePath)
		}

		file, err := os.Open(filePath)
		if err == nil {

			var uid int = 0
			var gid int = 0
			var mode os.FileMode = 0
			fi, err := file.Stat()
			if err == nil {
				sysinfo := fi.Sys()
				if sysinfo != nil {
					stat_t := sysinfo.(*syscall.Stat_t)
					if stat_t != nil {
						uid = int(stat_t.Uid)
						gid = int(stat_t.Gid)
					}
				}
				mode = fi.Mode()
			}

			workInProgress.Add(1)
			fileWriterQueue <- block{filePath, 0, nil, true, false, uid, gid, mode}

			bufferedFile := bufio.NewReader(file)

			for {
				buffer := make([]byte, blockSize)
				bytesRead, err := bufferedFile.Read(buffer)
				if err == io.EOF {
					break
				} else if err != nil {
					logger.Println("file read error; file contents will be incomplete:", err.Error())
					break
				}

				workInProgress.Add(1)
				fileWriterQueue <- block{filePath, uint16(bytesRead), buffer, false, false, 0, 0, 0}
			}

			workInProgress.Add(1)
			fileWriterQueue <- block{filePath, 0, nil, false, true, 0, 0, 0}

			file.Close()
		} else {
			logger.Println("file open error:", err.Error())
		}

		workInProgress.Done()
	}
}

func archiveWriter(output io.Writer, fileWriterQueue <-chan block, workInProgress *sync.WaitGroup) {
	flags := make([]byte, 1)

	for block := range fileWriterQueue {
		filePath := []byte(block.filePath)
		err := binary.Write(output, binary.BigEndian, uint16(len(filePath)))
		if err != nil {
			logger.Fatalln("Archive write error:", err.Error())
		}
		_, err = output.Write(filePath)
		if err != nil {
			logger.Fatalln("Archive write error:", err.Error())
		}

		if block.startOfFile {
			flags[0] = startOfFileFlag
			_, err = output.Write(flags)
			if err != nil {
				logger.Fatalln("Archive write error:", err.Error())
			}
			err = binary.Write(output, binary.BigEndian, uint32(block.uid))
			if err != nil {
				logger.Fatalln("Archive write error:", err.Error())
			}
			err = binary.Write(output, binary.BigEndian, uint32(block.gid))
			if err != nil {
				logger.Fatalln("Archive write error:", err.Error())
			}
			err = binary.Write(output, binary.BigEndian, block.mode)
			if err != nil {
				logger.Fatalln("Archive write error:", err.Error())
			}
		} else if block.endOfFile {
			flags[0] = endOfFileFlag
			_, err = output.Write(flags)
			if err != nil {
				logger.Fatalln("Archive write error:", err.Error())
			}
		} else {
			flags[0] = dataBlockFlag
			_, err = output.Write(flags)
			if err != nil {
				logger.Fatalln("Archive write error:", err.Error())
			}

			err = binary.Write(output, binary.BigEndian, uint16(block.numBytes))
			if err != nil {
				logger.Fatalln("Archive write error:", err.Error())
			}

			_, err = output.Write(block.buffer[:block.numBytes])
			if err != nil {
				logger.Fatalln("Archive write error:", err.Error())
			}
		}

		workInProgress.Done()
	}
}

func archiveReader(file io.Reader) {
	var workInProgress sync.WaitGroup
	fileOutputChan := make(map[string]chan block)

	for {
		var pathSize uint16
		err := binary.Read(file, binary.BigEndian, &pathSize)
		if err == io.EOF {
			break
		} else if err != nil {
			logger.Fatalln("Archive read error:", err.Error())
		}

		buf := make([]byte, pathSize)
		_, err = io.ReadFull(file, buf)
		if err != nil {
			logger.Fatalln("Archive read error:", err.Error())
		}
		filePath := string(buf)

		flag := make([]byte, 1)
		_, err = io.ReadFull(file, flag)
		if err != nil {
			logger.Fatalln("Archive read error:", err.Error())
		}

		if flag[0] == startOfFileFlag {
			var uid uint32
			var gid uint32
			var mode os.FileMode

			err = binary.Read(file, binary.BigEndian, &uid)
			if err != nil {
				logger.Fatalln("Archive read error:", err.Error())
			}

			err = binary.Read(file, binary.BigEndian, &gid)
			if err != nil {
				logger.Fatalln("Archive read error:", err.Error())
			}

			err = binary.Read(file, binary.BigEndian, &mode)
			if err != nil {
				logger.Fatalln("Archive read error:", err.Error())
			}

			c := make(chan block, 1)
			fileOutputChan[filePath] = c
			workInProgress.Add(1)
			go writeFile(c, &workInProgress)
			c <- block{filePath, 0, nil, true, false, int(uid), int(gid), mode}
		} else if flag[0] == endOfFileFlag {
			c := fileOutputChan[filePath]
			c <- block{filePath, 0, nil, false, true, 0, 0, 0}
			close(c)
			delete(fileOutputChan, filePath)
		} else if flag[0] == dataBlockFlag {
			var blockSize uint16
			err = binary.Read(file, binary.BigEndian, &blockSize)
			if err != nil {
				logger.Fatalln("Archive read error:", err.Error())
			}

			blockData := make([]byte, blockSize)
			_, err = io.ReadFull(file, blockData)
			if err != nil {
				logger.Fatalln("Archive read error:", err.Error())
			}

			c := fileOutputChan[filePath]
			c <- block{filePath, blockSize, blockData, false, false, 0, 0, 0}
		} else {
			logger.Fatalln("Archive error: unrecognized block flag", flag[0])
		}
	}

	workInProgress.Wait()
}

func writeFile(blockSource chan block, workInProgress *sync.WaitGroup) {
	var file *os.File = nil
	var bufferedFile *bufio.Writer
	for block := range blockSource {
		if block.startOfFile {
			if verbose {
				logger.Println(block.filePath)
			}

			dir, _ := filepath.Split(block.filePath)
			err := os.MkdirAll(dir, os.ModeDir|0755)
			if err != nil {
				logger.Fatalln("Directory create error:", err.Error())
			}

			tmp, err := os.Create(block.filePath)
			if err != nil {
				logger.Fatalln("File create error:", err.Error())
			}
			file = tmp
			bufferedFile = bufio.NewWriter(file)

			err = file.Chown(block.uid, block.gid)
			if err != nil {
				logger.Println("Unable to chown file to", block.uid, "/", block.gid, ":", err.Error())
			}
			err = file.Chmod(block.mode)
			if err != nil {
				logger.Println("Unable to chmod file to", block.mode, ":", err.Error())
			}
		} else if block.endOfFile {
			bufferedFile.Flush()
			file.Close()
			file = nil
		} else {
			_, err := bufferedFile.Write(block.buffer[:block.numBytes])
			if err != nil {
				logger.Fatalln("File write error:", err.Error())
			}
		}
	}
	workInProgress.Done()
}
<|endoftext|>


<commit_before>package main

import (
	"encoding/json"
	"flag"
	"fmt"
	//"github.com/kr/pretty"
	"log"
	"net/http"
	"strings"
)

var addr = flag.String("listen", "0.0.0.0:8080", "address to listen on")
var path = flag.String("listen_path", "/galaxy/gie_proxy", "path to listen on (for cookies)")
var cookieName = flag.String("cookie_name", "galaxysession", "cookie name")
var sessionMap = flag.String("storage", "./sessionMap.xml", "Session map file. Used to (re)store route lists across restarts")
var apiKey = flag.String("api_key", "THE_DEFAULT_IS_NOT_SECURE", "Key to access the API")

type frontend struct {
	Addr string
	Path string
}

type requestHandler struct {
	Transport    *http.Transport
	RouteMapping *RouteMapping
	Frontend     *frontend
}

type apiHandler struct {
	Transport    *http.Transport
	RouteMapping *RouteMapping
}

func (h *requestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	r.URL.Scheme = "http"
	// Add x-forwarded-for header
	addForwardedFor(r)

	// Their requested URL must agree with our prefix
	if !strings.HasPrefix(r.RequestURI, h.Frontend.Path) {
		http.Error(w, "unknown backend", http.StatusBadRequest)
		return
	}

	// Get their cookie
	cookie, err := r.Cookie(*cookieName)
	if err != nil {
		http.Error(w, "unknown auth cookie", http.StatusUnauthorized)
		return
	}

	log.Printf("Request for %s", r.RequestURI)
	// Find our route
	route, err := h.RouteMapping.FindRoute(
		r.RequestURI[len(h.Frontend.Path):], // Strip proxy prefix from path
		cookie.Value,
	)
	if err.Error() == "Could not find route" {
		http.Error(w, "unknown backend", http.StatusBadRequest)
	}
	// Now that we have a route, update when we last saw it.
	route.Seen()

	// Reset request URI
	r.RequestURI = ""
	r.URL.Host = route.BackendAddr
	// Strip frontend's path out
	//r.URL.Path = r.URL.Path[len(h.Frontend.Path):]

	connectErr := connectRoute(h, w, r)

	// If the backend is dead, remove it.
	// The next request from the user will be better behaved.
	if connectErr.Error() == "dead-backend" {
		h.RouteMapping.RemoveRoute(route)
	}
}

func connectRoute(h *requestHandler, w http.ResponseWriter, r *http.Request) error {
	var err error
	if shouldUpgradeWebsocket(r) {
		err = plumbWebsocket(w, r)
	} else {
		err = plumbHTTP(h, w, r)
	}
	return err
}

func main() {
	flag.Parse()
	// Load up route mapping
	rm := NewRouteMapping(sessionMap)
	// Build the frontend
	f := &frontend{
		Addr: *addr,
		Path: *path,
	}
	// Start our proxy
	log.Printf("Starting frontend ...")
	f.Start(rm)
}

func (h *apiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	// Authnz
	recvAPIKey := r.URL.Query().Get("api_key")
	// If it doesn't match what we expect, kick
	if recvAPIKey != *apiKey {
		http.Error(w, "Invalid API key", http.StatusUnauthorized)
		return
	}

	// Request Processing
	if r.Method == "GET" {
		// Get a list of routes
		jsonRoutes, err := json.MarshalIndent(h.RouteMapping.Routes, "", "    ")
		if err != nil {
			http.Error(w, "Data encoding error", http.StatusInternalServerError)
			return
		}
		fmt.Fprintf(w, string(jsonRoutes))
	} else if r.Method == "POST" {
		decoder := json.NewDecoder(r.Body)
		route := new(Route)
		err := decoder.Decode(&route)
		if err != nil {
			http.Error(w, "Invalid Route data", http.StatusBadRequest)
			return
		}

		// Seems like this should automatically be a decode exception?
		if route.FrontendPath == "" || route.BackendAddr == "" || route.AuthorizedCookie == "" {
			http.Error(w, "Invalid Route data", http.StatusBadRequest)
			return
		}

		// Create a new route
		h.RouteMapping.AddRoute(
			route.FrontendPath,
			route.BackendAddr,
			route.AuthorizedCookie,
		)

		// Then display all of them. TODO: refactor this?
		jsonRoutes, err := json.MarshalIndent(h.RouteMapping.Routes, "", "    ")
		if err != nil {
			http.Error(w, "Data encoding error", http.StatusInternalServerError)
			return
		}
		fmt.Fprintf(w, string(jsonRoutes))
	}
}

func (f *frontend) Start(rm *RouteMapping) {
	mux := http.NewServeMux()

	// Main request handler, processes every incoming request
	var requestHandler http.Handler = &requestHandler{
		Transport: &http.Transport{
			DisableKeepAlives:  false,
			DisableCompression: false,
		},
		RouteMapping: rm,
		Frontend:     f,
	}

	var apiHandler http.Handler = &apiHandler{
		Transport: &http.Transport{
			DisableKeepAlives:  false,
			DisableCompression: false,
		},
		RouteMapping: rm,
	}

	// The slash route handles ALL requests by passing to the request_handler
	// object
	mux.Handle("/api", apiHandler)
	mux.Handle("/", requestHandler)
	// Here we then launch the server from mux
	srv := &http.Server{Handler: mux, Addr: f.Addr}

	// Start
	if err := srv.ListenAndServe(); err != nil {
		log.Printf("Starting frontend failed: %v", err)
	}
}
<commit_msg>Refactor renderViewData view for complexity<commit_after>package main

import (
	"encoding/json"
	"flag"
	"fmt"
	//"github.com/kr/pretty"
	"log"
	"net/http"
	"strings"
)

var addr = flag.String("listen", "0.0.0.0:8080", "address to listen on")
var path = flag.String("listen_path", "/galaxy/gie_proxy", "path to listen on (for cookies)")
var cookieName = flag.String("cookie_name", "galaxysession", "cookie name")
var sessionMap = flag.String("storage", "./sessionMap.xml", "Session map file. Used to (re)store route lists across restarts")
var apiKey = flag.String("api_key", "THE_DEFAULT_IS_NOT_SECURE", "Key to access the API")

type frontend struct {
	Addr string
	Path string
}

type requestHandler struct {
	Transport    *http.Transport
	RouteMapping *RouteMapping
	Frontend     *frontend
}

type apiHandler struct {
	Transport    *http.Transport
	RouteMapping *RouteMapping
}

func (h *requestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	r.URL.Scheme = "http"
	// Add x-forwarded-for header
	addForwardedFor(r)

	// Their requested URL must agree with our prefix
	if !strings.HasPrefix(r.RequestURI, h.Frontend.Path) {
		http.Error(w, "unknown backend", http.StatusBadRequest)
		return
	}

	// Get their cookie
	cookie, err := r.Cookie(*cookieName)
	if err != nil {
		http.Error(w, "unknown auth cookie", http.StatusUnauthorized)
		return
	}

	log.Printf("Request for %s", r.RequestURI)
	// Find our route
	route, err := h.RouteMapping.FindRoute(
		r.RequestURI[len(h.Frontend.Path):], // Strip proxy prefix from path
		cookie.Value,
	)
	if err.Error() == "Could not find route" {
		http.Error(w, "unknown backend", http.StatusBadRequest)
	}
	// Now that we have a route, update when we last saw it.
	route.Seen()

	// Reset request URI
	r.RequestURI = ""
	r.URL.Host = route.BackendAddr
	// Strip frontend's path out
	//r.URL.Path = r.URL.Path[len(h.Frontend.Path):]

	connectErr := connectRoute(h, w, r)

	// If the backend is dead, remove it.
	// The next request from the user will be better behaved.
	if connectErr.Error() == "dead-backend" {
		h.RouteMapping.RemoveRoute(route)
	}
}

func connectRoute(h *requestHandler, w http.ResponseWriter, r *http.Request) error {
	var err error
	if shouldUpgradeWebsocket(r) {
		err = plumbWebsocket(w, r)
	} else {
		err = plumbHTTP(h, w, r)
	}
	return err
}

func main() {
	flag.Parse()
	// Load up route mapping
	rm := NewRouteMapping(sessionMap)
	// Build the frontend
	f := &frontend{
		Addr: *addr,
		Path: *path,
	}
	// Start our proxy
	log.Printf("Starting frontend ...")
	f.Start(rm)
}

func renderViewData(h *apiHandler, w http.ResponseWriter, r *http.Request) {
	jsonRoutes, err := json.MarshalIndent(h.RouteMapping.Routes, "", "    ")
	if err != nil {
		http.Error(w, "Data encoding error", http.StatusInternalServerError)
		return
	}
	fmt.Fprintf(w, string(jsonRoutes))
}

func (h *apiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	// Authnz
	recvAPIKey := r.URL.Query().Get("api_key")
	// If it doesn't match what we expect, kick
	if recvAPIKey != *apiKey {
		http.Error(w, "Invalid API key", http.StatusUnauthorized)
		return
	}

	// Request Processing
	if r.Method == "GET" {
		// Get a list of routes
		renderViewData(h, w, r)
	} else if r.Method == "POST" {
		decoder := json.NewDecoder(r.Body)
		route := new(Route)
		err := decoder.Decode(&route)
		if err != nil {
			http.Error(w, "Invalid Route data", http.StatusBadRequest)
			return
		}

		// Seems like this should automatically be a decode exception?
		if route.FrontendPath == "" || route.BackendAddr == "" || route.AuthorizedCookie == "" || len(route.ContainerIds) == 0 {
			http.Error(w, "Invalid Route data", http.StatusBadRequest)
			return
		}

		// Create a new route
		h.RouteMapping.AddRoute(
			route.FrontendPath,
			route.BackendAddr,
			route.AuthorizedCookie,
			route.ContainerIds,
		)

		renderViewData(h, w, r)
	}
}

func (f *frontend) Start(rm *RouteMapping) {
	mux := http.NewServeMux()

	// Main request handler, processes every incoming request
	var requestHandler http.Handler = &requestHandler{
		Transport: &http.Transport{
			DisableKeepAlives:  false,
			DisableCompression: false,
		},
		RouteMapping: rm,
		Frontend:     f,
	}
	rm.RegisterCleaner()

	var apiHandler http.Handler = &apiHandler{
		Transport: &http.Transport{
			DisableKeepAlives:  false,
			DisableCompression: false,
		},
		RouteMapping: rm,
	}

	// The slash route handles ALL requests by passing to the request_handler
	// object
	mux.Handle("/api", apiHandler)
	mux.Handle("/", requestHandler)
	// Here we then launch the server from mux
	srv := &http.Server{Handler: mux, Addr: f.Addr}

	// Start
	if err := srv.ListenAndServe(); err != nil {
		log.Printf("Starting frontend failed: %v", err)
	}
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"net/http"
	"os"
	"strconv"
	"time"

	"github.com/go-martini/martini"
	"github.com/golang/glog"
	"github.com/jinzhu/gorm"
	_ "github.com/lib/pq"
	"github.com/martini-contrib/render"
	"github.com/zakuni/foonwiki/models"
)

var db gorm.DB

func initDb() gorm.DB {
	db, err := gorm.Open("postgres", os.Getenv("DATABASE_URL"))
	checkErr(err, "sql.Open failed")
	db.DB()

	db.AutoMigrate(models.Wiki{})
	db.AutoMigrate(models.Page{})

	return db
}

func checkErr(err error, msg string) {
	if err != nil {
		glog.Fatalln(msg, err)
	}
}

func main() {
	// necessary for glog
	flag.Parse()

	db = initDb()
	defer db.Close()

	m := martini.Classic()
	m.Use(render.Renderer(render.Options{
		Layout: "layout",
	}))

	m.Get("/", func(req *http.Request, r render.Render) {
		qid := req.URL.Query().Get("id")
		if qid != "" {
			id, _ := strconv.ParseInt(qid, 10, 64)
			var page models.Page
			db.First(&page, id)

			r.HTML(200, "page", struct {
				Title string
				Page  models.Page
			}{
				page.Name,
				page,
			})
		} else {
			var newPages []models.Page
			var updatedPages []models.Page
			db.Limit(5).Order("created_at desc").Find(&newPages)
			db.Limit(5).Order("updated_at desc").Find(&updatedPages)

			r.HTML(200, "index", struct {
				Title        string
				NewPages     []models.Page
				UpdatedPages []models.Page
			}{
				"FoonWiki",
				newPages,
				updatedPages,
			})
		}
	})

	m.Get("/pages", func(req *http.Request, r render.Render) {
		qid := req.URL.Query().Get("sortedby")
		var allPages []models.Page

		if qid == "created" {
			db.Order("created_at desc").Find(&allPages)
		} else if qid == "updated" {
			db.Order("updated_at desc").Find(&allPages)
		}

		r.HTML(200, "pages", struct {
			Title string
			Pages []models.Page
		}{
			"AllPages",
			allPages,
		})
	})

	m.Get("/pages/new", func(r render.Render) {
		r.HTML(200, "page", nil)
	})

	m.Post("/pages/", func(req *http.Request, params martini.Params, r render.Render) {
		var page models.Page
		page.Name = req.FormValue("pagename")
		if page.Name == "" {
			page.Name = time.Now().Format("2006/01/02 15:04:05")
		}
		page.Content = req.FormValue("content")
		db.Save(&page)
		r.Redirect("/?id=" + strconv.FormatInt(page.Id, 10))
	})

	m.Get("/pages/:id", func(params martini.Params, r render.Render) {
		qid := params["id"]

		id, _ := strconv.ParseInt(qid, 10, 64)
		var page models.Page
		db.First(&page, id)

		r.HTML(200, "page", struct {
			Title string
			Page  models.Page
		}{
			page.Name,
			page,
		})
	})

	m.Post("/pages/:id", func(req *http.Request, params martini.Params, r render.Render) {
		var page models.Page
		id := params["id"]

		db.First(&page, id)
		page.Name = req.FormValue("pagename")
		if page.Name == "" {
			page.Name = time.Now().Format("2006/01/02 15:04:05")
		}
		page.Content = req.FormValue("content")
		db.Save(&page)

		r.HTML(200, "page", struct {
			Title string
			Page  models.Page
		}{
			page.Name,
			page,
		})
	})

	m.Get("/:wiki", func(params martini.Params, r render.Render) {
		var wiki models.Wiki
		var pages []models.Page
		wikiName := params["wiki"]

		db.Where(models.Wiki{Name: wikiName}).FirstOrInit(&wiki)
		db.Model(&wiki).Related(&pages)
		wiki.Pages = pages

		r.HTML(200, "wiki", struct {
			Title string
			Wiki  models.Wiki
		}{
			wikiName,
			wiki,
		})
	})

	m.Get("/:wiki/:page", func(params martini.Params, r render.Render) {
		var wiki models.Wiki
		var page models.Page
		wikiName := params["wiki"]
		pageName := params["page"]

		db.Where(models.Wiki{Name: wikiName}).FirstOrInit(&wiki)
		if !db.NewRecord(wiki) {
			db.Where(models.Page{Name: pageName, WikiId: wiki.Id}).FirstOrInit(&page)
		} else {
			page.Name = pageName
		}

		r.HTML(200, "page", struct {
			Title    string
			WikiName string
			Page     models.Page
		}{
			wikiName + "/" + pageName,
			wikiName,
			page,
		})
	})

	m.Post("/:wiki/:page", func(req *http.Request, params martini.Params, r render.Render) {
		var wiki models.Wiki
		var page models.Page
		wikiName := params["wiki"]
		pageName := params["page"]

		db.FirstOrCreate(&wiki, models.Wiki{Name: wikiName})
		db.FirstOrCreate(&page, models.Page{
			Name:   pageName,
			WikiId: wiki.Id,
		})
		page.Content = req.FormValue("content")
		page.Wiki = wiki
		db.Save(&page)

		r.HTML(200, "page", struct {
			Title    string
			WikiName string
			Page     models.Page
		}{
			wikiName + "/" + pageName,
			wikiName,
			page,
		})
	})

	m.Run()
}
<commit_msg>modify title of index.html<commit_after>package main

import (
	"flag"
	"net/http"
	"os"
	"strconv"
	"time"

	"github.com/go-martini/martini"
	"github.com/golang/glog"
	"github.com/jinzhu/gorm"
	_ "github.com/lib/pq"
	"github.com/martini-contrib/render"
	"github.com/zakuni/foonwiki/models"
)

var db gorm.DB

func initDb() gorm.DB {
	db, err := gorm.Open("postgres", os.Getenv("DATABASE_URL"))
	checkErr(err, "sql.Open failed")
	db.DB()

	db.AutoMigrate(models.Wiki{})
	db.AutoMigrate(models.Page{})

	return db
}

func checkErr(err error, msg string) {
	if err != nil {
		glog.Fatalln(msg, err)
	}
}

func main() {
	// necessary for glog
	flag.Parse()

	db = initDb()
	defer db.Close()

	m := martini.Classic()
	m.Use(render.Renderer(render.Options{
		Layout: "layout",
	}))

	m.Get("/", func(req *http.Request, r render.Render) {
		qid := req.URL.Query().Get("id")
		if qid != "" {
			id, _ := strconv.ParseInt(qid, 10, 64)
			var page models.Page
			db.First(&page, id)

			r.HTML(200, "page", struct {
				Title string
				Page  models.Page
			}{
				page.Name,
				page,
			})
		} else {
			var newPages []models.Page
			var updatedPages []models.Page
			db.Limit(5).Order("created_at desc").Find(&newPages)
			db.Limit(5).Order("updated_at desc").Find(&updatedPages)

			r.HTML(200, "index", struct {
				Title        string
				NewPages     []models.Page
				UpdatedPages []models.Page
			}{
				"",
				newPages,
				updatedPages,
			})
		}
	})

	m.Get("/pages", func(req *http.Request, r render.Render) {
		qid := req.URL.Query().Get("sortedby")
		var allPages []models.Page

		if qid == "created" {
			db.Order("created_at desc").Find(&allPages)
		} else if qid == "updated" {
			db.Order("updated_at desc").Find(&allPages)
		}

		r.HTML(200, "pages", struct {
			Title string
			Pages []models.Page
		}{
			"AllPages",
			allPages,
		})
	})

	m.Get("/pages/new", func(r render.Render) {
		r.HTML(200, "page", nil)
	})

	m.Post("/pages/", func(req *http.Request, params martini.Params, r render.Render) {
		var page models.Page
		page.Name = req.FormValue("pagename")
		if page.Name == "" {
			page.Name = time.Now().Format("2006/01/02 15:04:05")
		}
		page.Content = req.FormValue("content")
		db.Save(&page)
		r.Redirect("/?id=" + strconv.FormatInt(page.Id, 10))
	})

	m.Get("/pages/:id", func(params martini.Params, r render.Render) {
		qid := params["id"]

		id, _ := strconv.ParseInt(qid, 10, 64)
		var page models.Page
		db.First(&page, id)

		r.HTML(200, "page", struct {
			Title string
			Page  models.Page
		}{
			page.Name,
			page,
		})
	})

	m.Post("/pages/:id", func(req *http.Request, params martini.Params, r render.Render) {
		var page models.Page
		id := params["id"]

		db.First(&page, id)
		page.Name = req.FormValue("pagename")
		if page.Name == "" {
			page.Name = time.Now().Format("2006/01/02 15:04:05")
		}
		page.Content = req.FormValue("content")
		db.Save(&page)

		r.HTML(200, "page", struct {
			Title string
			Page  models.Page
		}{
			page.Name,
			page,
		})
	})

	m.Get("/:wiki", func(params martini.Params, r render.Render) {
		var wiki models.Wiki
		var pages []models.Page
		wikiName := params["wiki"]

		db.Where(models.Wiki{Name: wikiName}).FirstOrInit(&wiki)
		db.Model(&wiki).Related(&pages)
		wiki.Pages = pages

		r.HTML(200, "wiki", struct {
			Title string
			Wiki  models.Wiki
		}{
			wikiName,
			wiki,
		})
	})

	m.Get("/:wiki/:page", func(params martini.Params, r render.Render) {
		var wiki models.Wiki
		var page models.Page
		wikiName := params["wiki"]
		pageName := params["page"]

		db.Where(models.Wiki{Name: wikiName}).FirstOrInit(&wiki)
		if !db.NewRecord(wiki) {
			db.Where(models.Page{Name: pageName, WikiId: wiki.Id}).FirstOrInit(&page)
		} else {
			page.Name = pageName
		}

		r.HTML(200, "page", struct {
			Title    string
			WikiName string
			Page     models.Page
		}{
			wikiName + "/" + pageName,
			wikiName,
			page,
		})
	})

	m.Post("/:wiki/:page", func(req *http.Request, params martini.Params, r render.Render) {
		var wiki models.Wiki
		var page models.Page
		wikiName := params["wiki"]
		pageName := params["page"]

		db.FirstOrCreate(&wiki, models.Wiki{Name: wikiName})
		db.FirstOrCreate(&page, models.Page{
			Name:   pageName,
			WikiId: wiki.Id,
		})
		page.Content = req.FormValue("content")
		page.Wiki = wiki
		db.Save(&page)

		r.HTML(200, "page", struct {
			Title    string
			WikiName string
			Page     models.Page
		}{
			wikiName + "/" + pageName,
			wikiName,
			page,
		})
	})

	m.Run()
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"os"
	"os/exec"

	"github.com/drone/drone-plugin-go/plugin"
)

type deployer struct {
	Task  string `json:"task"`
	Stage string `json:"stage"`
}

func main() {
	d := new(deployer)
	w := new(plugin.Workspace)

	plugin.Param("vargs", d)
	plugin.Param("workspace", w)
	plugin.MustParse()

	c := exec.Command("/usr/bin/php", "/bin/dep", d.Task, d.Stage)
	c.Path = w.Path

	err := c.Run()
	if err != nil {
		fmt.Println("Error!")
		fmt.Println(err)
		os.Exit(1)
	}
	fmt.Println("Command completed successfully")
}
<commit_msg>switched Path to Dir<commit_after>package main

import (
	"fmt"
	"os"
	"os/exec"

	"github.com/drone/drone-plugin-go/plugin"
)

type deployer struct {
	Task  string `json:"task"`
	Stage string `json:"stage"`
}

func main() {
	d := new(deployer)
	w := new(plugin.Workspace)

	plugin.Param("vargs", d)
	plugin.Param("workspace", w)
	plugin.MustParse()

	c := exec.Command("/usr/bin/php", "/bin/dep", d.Task, d.Stage)
	c.Dir = w.Path

	err := c.Run()
	if err != nil {
		fmt.Println("Error!")
		fmt.Println(err)
		os.Exit(1)
	}
	fmt.Println("Command completed successfully")
}
<|endoftext|>


<commit_before>package main

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

	"github.com/drone/drone-plugin-go/plugin"
)

type Fabric struct {
	Commands  []string `json:"commands"`
}

func main() {
	workspace := plugin.Workspace{}
	vargs := Fabric{}

	plugin.Param("workspace", &workspace)
	plugin.Param("vargs", &vargs)

	// parse the parameters
	if err := plugin.Parse(); err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}

	err := ioutil.WriteFile("/tmp/ssh_key", []byte(workspace.Keys.Public), 0644)
	log.Println(workspace.Keys.Public)
	if err != nil {
		fmt.Println(err.Error())
		os.Exit(99)
	}

	fabfile := fmt.Sprintf("--fabfile=%s/fabfile.py", workspace.Path)

	for _, c := range vargs.Commands {
		command := fmt.Sprintf("%s %s", fabfile, c)
		fabArgs := strings.Split(command, " ")
		fabArgs = append(fabArgs, "-i", "/tmp/ssh_key")

		c := exec.Command("fab", fabArgs...)
		c.Stdout = os.Stdout
		c.Stderr = os.Stderr
		err := c.Run()
		if err != nil {
			log.Println(err)
			os.Exit(101)
		}
	}
}
<commit_msg>Private key<commit_after>package main

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

	"github.com/drone/drone-plugin-go/plugin"
)

type Fabric struct {
	Commands  []string `json:"commands"`
}

func main() {
	workspace := plugin.Workspace{}
	vargs := Fabric{}

	plugin.Param("workspace", &workspace)
	plugin.Param("vargs", &vargs)

	// parse the parameters
	if err := plugin.Parse(); err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}

	err := ioutil.WriteFile("/tmp/private_key", []byte(workspace.Keys.Private), 0644)
	if err != nil {
		fmt.Println(err.Error())
		os.Exit(99)
	}

	fabfile := fmt.Sprintf("--fabfile=%s/fabfile.py", workspace.Path)

	for _, c := range vargs.Commands {
		command := fmt.Sprintf("%s %s", fabfile, c)
		fabArgs := strings.Split(command, " ")
		fabArgs = append(fabArgs, "-i", "/tmp/private_key")

		c := exec.Command("fab", fabArgs...)
		c.Stdout = os.Stdout
		c.Stderr = os.Stderr
		err := c.Run()
		if err != nil {
			log.Println(err)
			os.Exit(101)
		}
	}
}
<|endoftext|>