<commit_before>// Copyright 2015-2017 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// bbramfs builds a simple initramfs given an existing built bb; see bb.go
// You have to run bb first, which creates cmds/bb/bbsh. cd to that directory,
// and run bbramfs, and you have a single binary which does all u-root commands.
package main

import (
	"flag"
	"fmt"
	"log"
	"os"
	"path/filepath"
	"strings"

	"github.com/u-root/u-root/pkg/cpio"
	_ "github.com/u-root/u-root/pkg/cpio/newc"
	"github.com/u-root/u-root/pkg/ldd"
)

var (
	// Paths contains the paths to put into the initramfs.
	// The index is a root directory, and the value is the place from which to
	// walk. The only required root is the bbsh dir itself, and the starting
	// walk is init -- i.e. we grab only one file. Should you wish to bring in,
	// e.g., /lib/modules/4.04, you would do add the root as / and the
	// starting point for the walk as lib/modules/4.04. That way we only preserve
	// as much of the path as we need, but we can preserve it all.
	paths      = map[string][]string{}
	extraPaths = flag.String("extra", "", `Extra paths to add in the form root:start, e.g. /:etc/hosts.
The path before the : is used as a starting point for a walk; the path after the : selects what things to put
into the initramfs starting at /. E.g., /tmp/prototype:/ will install the prototype file system into / of the initramfs`)
	extraCmds = flag.String("cmds", "", "Extra commands to add (full path, comma-separated string)")
	extraCpio = flag.String("cpio", "", "A list of cpio archives to include in the output")
)

func sanity() {
	goBinGo := filepath.Join(config.Goroot, "bin/go")
	_, err := os.Stat(goBinGo)
	if err == nil {
		config.Go = goBinGo
	}
	// but does the one in go/bin/OS_ARCH exist too?
	goBinGo = filepath.Join(config.Goroot, fmt.Sprintf("bin/%s_%s/go", config.Goos, config.Arch))
	_, err = os.Stat(goBinGo)
	if err == nil {
		config.Go = goBinGo
	}
	if config.Go == "" {
		log.Fatalf("Can't find a go binary! Is GOROOT set correctly?")
	}
}

// dirComponents takes a string and returns an array of strings,
// such that we can create the directory records for intermediate
// directories.
func dirComponents(dir string) []string {
	var dirlist []string
	if filepath.Dir(dir) == "." {
		return []string{}
	}
	for d := filepath.Dir(dir); d != "/"; d = filepath.Dir(d) {
		dirlist = append([]string{d}, dirlist...)
	}
	dirlist = append(dirlist, dir)
	return dirlist
}

// copyCommands takes a list of commands, generates the list of libs,
// and creates cpio records, including directory records.
func copyCommands(w cpio.Writer, cmds []string) {
	debug("copyCommands: start with %v", cmds)
	var recs []cpio.Record
	libs, err := uroot.LddList(cmds)
	if err != nil {
		log.Fatalf("%v\n", err)
	}
	cmds = append(cmds, libs...)
	for _, n := range cmds {
		debug("copyCommands: file %v", n)
		for _, n := range dirComponents(n) {
			debug("copyCommands: %v", n)
			r, err := cpio.GetRecord(n)
			if err != nil {
				log.Fatalf("%v: %v", n, err)
			}
			recs = append(recs, r)
		}
	}
	cpio.MakeReproducible(recs)
	if err := w.WriteRecords(recs); err != nil {
		log.Fatalf("%v\n", err)
	}
}

func ramfs() {
	archiver, err := cpio.Format("newc")
	if err != nil {
		log.Fatalf("Creating newc archiver: %v", err)
	}

	oname := fmt.Sprintf("/tmp/initramfs.%v_%v.cpio", config.Goos, config.Arch)
	f, err := os.Create(oname)
	if err != nil {
		log.Fatalf("%v\n", err)
	}

	w := archiver.Writer(f)
	cpio.MakeReproducible(devCPIO[:])
	if err := w.WriteRecords(devCPIO[:]); err != nil {
		log.Fatalf("%v\n", err)
	}

	paths[filepath.Join(config.Gopath, "src/github.com/u-root/u-root/bb/bbsh")] = []string{"init", "ubin"}

	if *extraPaths != "" {
		extras := strings.Split(*extraPaths, " ")
		for _, x := range extras {
			p := strings.Split(x, ":")
			if len(p) != 2 {
				p = append([]string{"/"}, p...)
			}
			paths[p[0]] = append(paths[p[0]], p[1])
		}
	}

	if *extraCmds != "" {
		copyCommands(w, strings.Split(*extraCmds, " "))
	}

	if *extraCpio != "" {
		extras := strings.Split(*extraCpio, " ")
		for _, x := range extras {
			a, err := cpio.Format("newc")
			if err != nil {
				log.Fatalf("Creating archiver: %v", err)
			}
			f, err := os.Open(x)
			if err != nil {
				log.Fatalf("%v: %v", x, err)
			}
			defer f.Close()
			rr := a.Reader(f)
			recs, err := rr.ReadRecords()
			if err != nil {
				log.Fatalf("read records: %v", err)
			}
			cpio.MakeReproducible(recs)
			if err := w.WriteRecords(recs); err != nil {
				log.Fatalf("%v\n", err)
			}
		}
	}

	// For all the 'roots' in paths, start walking at the name.
	debug("PATHS: %v", paths)
	for r, list := range paths {
		debug("PATHS: root %v", r)
		// we need to make all the path prefix directories.
		for _, n := range list {
			debug("\troot %v, name %v", r, n)
			for _, d := range dirComponents(n) {
				debug("\t\troot %v, name %v, component %v", r, n, d)
				rec, err := cpio.GetRecord(d)
				if err != nil {
					log.Fatalf("Getting record of %q failed: %v", d, err)
				}
				recs := []cpio.Record{rec}
				cpio.MakeReproducible(recs)
				if err := w.WriteRecords(recs); err != nil {
					log.Fatalf("%v\n", err)
				}
			}
		}
		for _, n := range list {
			if err := filepath.Walk(filepath.Join(r, n), func(name string, fi os.FileInfo, err error) error {
				if err != nil {
					return err
				}
				cn, err := filepath.Rel(r, name)
				if err != nil {
					log.Fatalf("filepath.Rel(%v, %v): %v", r, name, err)
				}
				debug("%v\n", cn)
				rec, err := cpio.GetRecord(name)
				if err != nil {
					log.Fatalf("Getting record of %q failed: %v", cn, err)
				}
				// the name in the cpio is relative to our starting point.
				rec.Name = cn
				recs := []cpio.Record{rec}
				cpio.MakeReproducible(recs)
				if err := w.WriteRecords(recs); err != nil {
					log.Fatalf("%v\n", err)
				}
				return nil
			}); err != nil {
				log.Fatalf("bbsh walk failed: %v", err)
			}
		}
	}

	if err := w.WriteTrailer(); err != nil {
		log.Fatalf("Error writing trailer record: %v", err)
	}
	fmt.Printf("Output file is in %v\n", oname)
}
<commit_msg>Added trim statements to remove starting and ending whitespace<commit_after>// Copyright 2015-2017 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// bbramfs builds a simple initramfs given an existing built bb; see bb.go
// You have to run bb first, which creates cmds/bb/bbsh. cd to that directory,
// and run bbramfs, and you have a single binary which does all u-root commands.
package main

import (
	"flag"
	"fmt"
	"log"
	"os"
	"path/filepath"
	"strings"

	"github.com/u-root/u-root/pkg/cpio"
	_ "github.com/u-root/u-root/pkg/cpio/newc"
	"github.com/u-root/u-root/pkg/ldd"
)

var (
	// Paths contains the paths to put into the initramfs.
	// The index is a root directory, and the value is the place from which to
	// walk. The only required root is the bbsh dir itself, and the starting
	// walk is init -- i.e. we grab only one file. Should you wish to bring in,
	// e.g., /lib/modules/4.04, you would do add the root as / and the
	// starting point for the walk as lib/modules/4.04. That way we only preserve
	// as much of the path as we need, but we can preserve it all.
	paths      = map[string][]string{}
	extraPaths = flag.String("extra", "", `Extra paths to add in the form root:start, e.g. /:etc/hosts.
The path before the : is used as a starting point for a walk; the path after the : selects what things to put
into the initramfs starting at /. E.g., /tmp/prototype:/ will install the prototype file system into / of the initramfs`)
	extraCmds = flag.String("cmds", "", "Extra commands to add (full path, comma-separated string)")
	extraCpio = flag.String("cpio", "", "A list of cpio archives to include in the output")
)

func sanity() {
	goBinGo := filepath.Join(config.Goroot, "bin/go")
	_, err := os.Stat(goBinGo)
	if err == nil {
		config.Go = goBinGo
	}
	// but does the one in go/bin/OS_ARCH exist too?
	goBinGo = filepath.Join(config.Goroot, fmt.Sprintf("bin/%s_%s/go", config.Goos, config.Arch))
	_, err = os.Stat(goBinGo)
	if err == nil {
		config.Go = goBinGo
	}
	if config.Go == "" {
		log.Fatalf("Can't find a go binary! Is GOROOT set correctly?")
	}
}

// dirComponents takes a string and returns an array of strings,
// such that we can create the directory records for intermediate
// directories.
func dirComponents(dir string) []string {
	var dirlist []string
	if filepath.Dir(dir) == "." {
		return []string{}
	}
	for d := filepath.Dir(dir); d != "/"; d = filepath.Dir(d) {
		dirlist = append([]string{d}, dirlist...)
	}
	dirlist = append(dirlist, dir)
	return dirlist
}

// copyCommands takes a list of commands, generates the list of libs,
// and creates cpio records, including directory records.
func copyCommands(w cpio.Writer, cmds []string) {
	debug("copyCommands: start with %v", cmds)
	var recs []cpio.Record
	libs, err := uroot.LddList(cmds)
	if err != nil {
		log.Fatalf("%v\n", err)
	}
	cmds = append(cmds, libs...)
	for _, n := range cmds {
		debug("copyCommands: file %v", n)
		for _, n := range dirComponents(n) {
			debug("copyCommands: %v", n)
			r, err := cpio.GetRecord(n)
			if err != nil {
				log.Fatalf("%v: %v", n, err)
			}
			recs = append(recs, r)
		}
	}
	cpio.MakeReproducible(recs)
	if err := w.WriteRecords(recs); err != nil {
		log.Fatalf("%v\n", err)
	}
}

func ramfs() {
	archiver, err := cpio.Format("newc")
	if err != nil {
		log.Fatalf("Creating newc archiver: %v", err)
	}

	oname := fmt.Sprintf("/tmp/initramfs.%v_%v.cpio", config.Goos, config.Arch)
	f, err := os.Create(oname)
	if err != nil {
		log.Fatalf("%v\n", err)
	}

	w := archiver.Writer(f)
	cpio.MakeReproducible(devCPIO[:])
	if err := w.WriteRecords(devCPIO[:]); err != nil {
		log.Fatalf("%v\n", err)
	}

	paths[filepath.Join(config.Gopath, "src/github.com/u-root/u-root/bb/bbsh")] = []string{"init", "ubin"}

	if *extraPaths != "" {
		extras := strings.Split(strings.Trim(*extraPaths, " "), " ")
		for _, x := range extras {
			p := strings.Split(x, ":")
			if len(p) != 2 {
				p = append([]string{"/"}, p...)
			}
			paths[p[0]] = append(paths[p[0]], p[1])
		}
	}

	if *extraCmds != "" {
		copyCommands(w, strings.Split(strings.Trim(*extraCmds, " "), " "))
	}

	if *extraCpio != "" {
		extras := strings.Split(strings.Trim(*extraCpio, " "), " ")
		for _, x := range extras {
			a, err := cpio.Format("newc")
			if err != nil {
				log.Fatalf("Creating archiver: %v", err)
			}
			f, err := os.Open(x)
			if err != nil {
				log.Fatalf("%v: %v", x, err)
			}
			defer f.Close()
			rr := a.Reader(f)
			recs, err := rr.ReadRecords()
			if err != nil {
				log.Fatalf("read records: %v", err)
			}
			cpio.MakeReproducible(recs)
			if err := w.WriteRecords(recs); err != nil {
				log.Fatalf("%v\n", err)
			}
		}
	}

	// For all the 'roots' in paths, start walking at the name.
	debug("PATHS: %v", paths)
	for r, list := range paths {
		debug("PATHS: root %v", r)
		// we need to make all the path prefix directories.
		for _, n := range list {
			debug("\troot %v, name %v", r, n)
			for _, d := range dirComponents(n) {
				debug("\t\troot %v, name %v, component %v", r, n, d)
				rec, err := cpio.GetRecord(d)
				if err != nil {
					log.Fatalf("Getting record of %q failed: %v", d, err)
				}
				recs := []cpio.Record{rec}
				cpio.MakeReproducible(recs)
				if err := w.WriteRecords(recs); err != nil {
					log.Fatalf("%v\n", err)
				}
			}
		}
		for _, n := range list {
			if err := filepath.Walk(filepath.Join(r, n), func(name string, fi os.FileInfo, err error) error {
				if err != nil {
					return err
				}
				cn, err := filepath.Rel(r, name)
				if err != nil {
					log.Fatalf("filepath.Rel(%v, %v): %v", r, name, err)
				}
				debug("%v\n", cn)
				rec, err := cpio.GetRecord(name)
				if err != nil {
					log.Fatalf("Getting record of %q failed: %v", cn, err)
				}
				// the name in the cpio is relative to our starting point.
				rec.Name = cn
				recs := []cpio.Record{rec}
				cpio.MakeReproducible(recs)
				if err := w.WriteRecords(recs); err != nil {
					log.Fatalf("%v\n", err)
				}
				return nil
			}); err != nil {
				log.Fatalf("bbsh walk failed: %v", err)
			}
		}
	}

	if err := w.WriteTrailer(); err != nil {
		log.Fatalf("Error writing trailer record: %v", err)
	}
	fmt.Printf("Output file is in %v\n", oname)
}
<|endoftext|>


<commit_before>package images

import(
  "io"
  "io/ioutil"
  "encoding/json"
  "github.com/ricallinson/forgery"
  "github.com/spacedock-io/registry/db"
  "github.com/spacedock-io/registry/models"
  "github.com/spacedock-io/registry/cloudfiles"
)

func GetJson(req *f.Request, res *f.Response) {
  var image models.Image
  db.DB.First(&models.Image{Uuid: req.Params["id"]}).First(&image)

  res.Set("X-Docker-Size", string(image.Size))
  res.Set("X-Docker-Checksum", image.Checksum)

  res.Send(image.Json)
}

func PutJson(req *f.Request, res *f.Response) {
  var image models.Image
  var err error

  db.DB.Where(&models.Image{Uuid: req.Params["id"]}).First(&image)
  image.Json, err = ioutil.ReadAll(req.Request.Request.Body)

  if err == nil {
    db.DB.Save(&image)
  } else {
    res.Send(500)
  }
}

func GetLayer(req *f.Request, res *f.Response) {
  _, err := cloudfiles.Cloudfiles.ObjectGet(
    "default", req.Params["id"], res.Response.Writer, true, nil)
  if err == nil {
    res.Send(200)
  } else { res.Send(500) }
}

func PutLayer(req *f.Request, res *f.Response) {
  obj, err := cloudfiles.Cloudfiles.ObjectCreate(
    "default", req.Params["id"], true, "", "", nil)
  if err == nil {
    io.Copy(obj, req.Request.Request.Body)
    res.Send(200)
  } else { res.Send(500) }
}

func GetAncestry(req *f.Request, res *f.Response) {
  var image models.Image
  db.DB.First(&models.Image{Uuid: req.Params["id"]}).First(&image)

  data, err := json.Marshal(image.Ancestry)

  if err == nil {
    res.Send(data)
  } else { res.Send(500) }
}
<commit_msg>Use .Where in queries<commit_after>package images

import(
  "io"
  "io/ioutil"
  "encoding/json"
  "github.com/ricallinson/forgery"
  "github.com/spacedock-io/registry/db"
  "github.com/spacedock-io/registry/models"
  "github.com/spacedock-io/registry/cloudfiles"
)

func GetJson(req *f.Request, res *f.Response) {
  var image models.Image
  db.DB.Where(&models.Image{Uuid: req.Params["id"]}).First(&image)

  res.Set("X-Docker-Size", string(image.Size))
  res.Set("X-Docker-Checksum", image.Checksum)

  res.Send(image.Json)
}

func PutJson(req *f.Request, res *f.Response) {
  var image models.Image
  var err error

  db.DB.Where(&models.Image{Uuid: req.Params["id"]}).First(&image)
  image.Json, err = ioutil.ReadAll(req.Request.Request.Body)

  if err == nil {
    db.DB.Save(&image)
  } else {
    res.Send(500)
  }
}

func GetLayer(req *f.Request, res *f.Response) {
  _, err := cloudfiles.Cloudfiles.ObjectGet(
    "default", req.Params["id"], res.Response.Writer, true, nil)
  if err == nil {
    res.Send(200)
  } else { res.Send(500) }
}

func PutLayer(req *f.Request, res *f.Response) {
  obj, err := cloudfiles.Cloudfiles.ObjectCreate(
    "default", req.Params["id"], true, "", "", nil)
  if err == nil {
    io.Copy(obj, req.Request.Request.Body)
    res.Send(200)
  } else { res.Send(500) }
}

func GetAncestry(req *f.Request, res *f.Response) {
  var image models.Image
  db.DB.First(&models.Image{Uuid: req.Params["id"]}).First(&image)

  data, err := json.Marshal(image.Ancestry)

  if err == nil {
    res.Send(data)
  } else { res.Send(500) }
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"io"
	"log"
	"os"
)

var restoremode = flag.Bool("u", false, "restore the original separator characters")
var delimiter = flag.String("d", ",", "field separator character")
var delimitertab = flag.Bool("t", false, "use tab as field separator (overrides -d parameter)")
var quotechar = flag.String("q", "\"", "field quoting character")
var recordsep = flag.String("r", "\n", "record separator character")

var delimiterNonprintingByte byte = 31
var recordsepNonprintingByte byte = 30
var delimiterByte, quotecharByte, recordsepByte byte

func main() {
	flag.Parse() // Scans the arg list and sets up flags
	if *delimitertab {
		*delimiter = "\t"
	}
	var input *os.File
	if flag.NArg() > 0 {
		var err error
		input, err = os.Open(flag.Arg(0))
		if err != nil {
			log.Fatal(err)
		}
	} else {
		input = os.Stdin
	}

	mapFunction := replaceOriginalChars
	if *restoremode {
		mapFunction = restoreOriginalChars
	}
	stateQuoteInEffect := false         // only used in replace mode
	stateMaybeEscapedQuoteChar := false // only used in replace mode

	delimiterByte = byte((*delimiter)[0])
	quotecharByte = byte((*quotechar)[0])
	recordsepByte = byte((*recordsep)[0])

	data := make([]byte, 1024)
	for {
		if count, err := input.Read(data); err != nil {
			if err != io.EOF {
				log.Fatal(err)
			}
			break
		} else {
			for i := 0; i < count; i++ {
				data[i], stateQuoteInEffect, stateMaybeEscapedQuoteChar =
					mapFunction(data[i], stateQuoteInEffect, stateMaybeEscapedQuoteChar)
			}
			os.Stdout.Write(data[:count])
		}
	}
	err := input.Close()
	if err != nil {
		log.Fatal(err)
	}
}

func replaceOriginalChars(c byte, stateQuoteInEffect bool, stateMaybeEscapedQuoteChar bool) (byte, bool, bool) {
	d := c // default
	if stateMaybeEscapedQuoteChar {
		if c != quotecharByte {
			// this is the end of a quoted field
			stateQuoteInEffect = false
		}
		stateMaybeEscapedQuoteChar = false
	} else if stateQuoteInEffect {
		switch c {
		case quotecharByte:
			// this is either an escaped quote char or the end of a quoted
			// field. need to read one more character to decide which
			stateMaybeEscapedQuoteChar = true
		case delimiterByte:
			d = delimiterNonprintingByte
		case recordsepByte:
			d = recordsepNonprintingByte
		}
	} else {
		// quote not in effect
		if c == quotecharByte {
			stateQuoteInEffect = true
		}
	}
	return d, stateQuoteInEffect, stateMaybeEscapedQuoteChar
}

func restoreOriginalChars(c byte, stateQuoteInEffect bool, stateMaybeEscapedQuoteChar bool) (byte, bool, bool) {
	// need to have same input/output parameters as replaceOriginalChars()
	// so the state variables are included but not used
	switch c {
	case delimiterNonprintingByte:
		return delimiterByte, false, false
	case recordsepNonprintingByte:
		return recordsepByte, false, false
	}
	return c, false, false
}
<commit_msg>get rid of global variables, create functions with parameters to be re-used<commit_after>package main

import (
	"flag"
	"io"
	"log"
	"os"
)

const delimiterNonprintingByte,	recordsepNonprintingByte byte = 31, 30

func main() {
	restoremode := flag.Bool("u", false, "restore the original separator characters")
	delimiter := flag.String("d", ",", "field separator character")
	delimitertab := flag.Bool("t", false, "use tab as field separator (overrides -d parameter)")
	quotechar := flag.String("q", "\"", "field quoting character")
	recordsep := flag.String("r", "\n", "record separator character")
	flag.Parse() // Scans the arg list and sets up flags
	if *delimitertab {
		*delimiter = "\t"
	}
	delimiterByte := byte((*delimiter)[0])
	quotecharByte := byte((*quotechar)[0])
	recordsepByte := byte((*recordsep)[0])
	mapFunction := substituteNonprintingChars(delimiterByte, quotecharByte, recordsepByte)
	if *restoremode {
		mapFunction = restoreOriginalChars(delimiterByte, recordsepByte)
	}

	var input *os.File
	if flag.NArg() > 0 {
		var err error
		input, err = os.Open(flag.Arg(0))
		if err != nil {
			log.Fatal(err)
		}
	} else {
		input = os.Stdin
	}

	stateQuoteInEffect := false         // only used in replace mode
	stateMaybeEscapedQuoteChar := false // only used in replace mode
	data := make([]byte, 1024)
	for {
		if count, err := input.Read(data); err != nil {
			if err != io.EOF {
				log.Fatal(err)
			}
			break
		} else {
			for i := 0; i < count; i++ {
				data[i], stateQuoteInEffect, stateMaybeEscapedQuoteChar =
					mapFunction(data[i], stateQuoteInEffect, stateMaybeEscapedQuoteChar)
			}
			os.Stdout.Write(data[:count])
		}
	}
	err := input.Close()
	if err != nil {
		log.Fatal(err)
	}
}

func substituteNonprintingChars(delimiterByte byte, quotecharByte byte, recordsepByte byte) (func(byte, bool, bool) (byte, bool, bool)) {
	return func(c byte, stateQuoteInEffect bool, stateMaybeEscapedQuoteChar bool) (byte, bool, bool) {
		d := c // default
		if stateMaybeEscapedQuoteChar {
			if c != quotecharByte {
				// this is the end of a quoted field
				stateQuoteInEffect = false
			}
			stateMaybeEscapedQuoteChar = false
		} else if stateQuoteInEffect {
			switch c {
			case quotecharByte:
				// this is either an escaped quote char or the end of a quoted
				// field. need to read one more character to decide which
				stateMaybeEscapedQuoteChar = true
			case delimiterByte:
				d = delimiterNonprintingByte
			case recordsepByte:
				d = recordsepNonprintingByte
			}
		} else {
			// quote not in effect
			if c == quotecharByte {
				stateQuoteInEffect = true
			}
		}
		return d, stateQuoteInEffect, stateMaybeEscapedQuoteChar
	}
}

func restoreOriginalChars(delimiterByte byte, recordsepByte byte) (func(byte, bool, bool) (byte, bool, bool)) {
	return func(c byte, stateQuoteInEffect bool, stateMaybeEscapedQuoteChar bool) (byte, bool, bool) {
		// need to have same input/output parameters as replaceOriginalChars()
		// so the state variables are included but not used
		switch c {
		case delimiterNonprintingByte:
			return delimiterByte, false, false
		case recordsepNonprintingByte:
			return recordsepByte, false, false
		}
		return c, false, false
	}
}
<|endoftext|>


<commit_before>package main

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

	"github.com/julienschmidt/httprouter"
)

/* Read functions
 */
func ListUser(w http.ResponseWriter, r *http.Request,
	_ httprouter.Params) {
	defer PanicCatcher(w)

	returnChannel := make(chan somaResult)
	handler := handlerMap["userReadHandler"].(somaUserReadHandler)
	handler.input <- somaUserRequest{
		action: "list",
		reply:  returnChannel,
	}
	result := <-returnChannel

	// declare here since goto does not jump over declarations
	cReq := proto.NewUserFilter()
	if result.Failure() {
		goto skip
	}

	_ = DecodeJsonBody(r, &cReq)
	if cReq.Filter.User.UserName != "" {
		filtered := make([]somaUserResult, 0)
		for _, i := range result.Users {
			if i.User.UserName == cReq.Filter.User.UserName {
				filtered = append(filtered, i)
			}
		}
		result.Users = filtered
	}

skip:
	SendUserReply(&w, &result)
}

func ShowUser(w http.ResponseWriter, r *http.Request,
	params httprouter.Params) {
	defer PanicCatcher(w)

	returnChannel := make(chan somaResult)
	handler := handlerMap["userReadHandler"].(somaUserReadHandler)
	handler.input <- somaUserRequest{
		action: "show",
		reply:  returnChannel,
		User: proto.User{
			Id: params.ByName("user"),
		},
	}
	result := <-returnChannel
	SendUserReply(&w, &result)
}

/* Write functions
 */
func AddUser(w http.ResponseWriter, r *http.Request,
	_ httprouter.Params) {
	defer PanicCatcher(w)

	cReq := proto.NewUserRequest()
	err := DecodeJsonBody(r, &cReq)
	if err != nil {
		DispatchBadRequest(&w, err)
		return
	}

	returnChannel := make(chan somaResult)
	handler := handlerMap["userWriteHandler"].(somaUserWriteHandler)
	handler.input <- somaUserRequest{
		action: "add",
		reply:  returnChannel,
		User: proto.User{
			UserName:       cReq.User.UserName,
			FirstName:      cReq.User.FirstName,
			LastName:       cReq.User.LastName,
			EmployeeNumber: cReq.User.EmployeeNumber,
			MailAddress:    cReq.User.MailAddress,
			IsActive:       false,
			IsSystem:       cReq.User.IsSystem,
			IsDeleted:      false,
			TeamId:         cReq.User.TeamId,
		},
	}
	result := <-returnChannel
	SendUserReply(&w, &result)
}

func DeleteUser(w http.ResponseWriter, r *http.Request,
	params httprouter.Params) {
	defer PanicCatcher(w)
	action := "delete"

	cReq := proto.NewUserRequest()
	_ = DecodeJsonBody(r, &cReq)
	if cReq.Flags.Purge {
		action = "purge"
	}

	returnChannel := make(chan somaResult)
	handler := handlerMap["userWriteHandler"].(somaUserWriteHandler)
	handler.input <- somaUserRequest{
		action: action,
		reply:  returnChannel,
		User: proto.User{
			Id: params.ByName("user"),
		},
	}
	result := <-returnChannel
	SendUserReply(&w, &result)
}

/* Utility
 */
func SendUserReply(w *http.ResponseWriter, r *somaResult) {
	result := proto.NewUserResult()
	if r.MarkErrors(&result) {
		goto dispatch
	}
	for _, i := range (*r).Users {
		*result.Users = append(*result.Users, i.User)
		if i.ResultError != nil {
			*result.Errors = append(*result.Errors, i.ResultError.Error())
		}
	}

dispatch:
	json, err := json.Marshal(result)
	if err != nil {
		DispatchInternalError(w, err)
		return
	}
	DispatchJsonReply(w, &json)
	return
}

// vim: ts=4 sw=4 sts=4 noet fenc=utf-8 ffs=unix
<commit_msg>Enforce username not containing : char<commit_after>package main

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

	"github.com/julienschmidt/httprouter"
)

/* Read functions
 */
func ListUser(w http.ResponseWriter, r *http.Request,
	_ httprouter.Params) {
	defer PanicCatcher(w)

	returnChannel := make(chan somaResult)
	handler := handlerMap["userReadHandler"].(somaUserReadHandler)
	handler.input <- somaUserRequest{
		action: "list",
		reply:  returnChannel,
	}
	result := <-returnChannel

	// declare here since goto does not jump over declarations
	cReq := proto.NewUserFilter()
	if result.Failure() {
		goto skip
	}

	_ = DecodeJsonBody(r, &cReq)
	if cReq.Filter.User.UserName != "" {
		filtered := make([]somaUserResult, 0)
		for _, i := range result.Users {
			if i.User.UserName == cReq.Filter.User.UserName {
				filtered = append(filtered, i)
			}
		}
		result.Users = filtered
	}

skip:
	SendUserReply(&w, &result)
}

func ShowUser(w http.ResponseWriter, r *http.Request,
	params httprouter.Params) {
	defer PanicCatcher(w)

	returnChannel := make(chan somaResult)
	handler := handlerMap["userReadHandler"].(somaUserReadHandler)
	handler.input <- somaUserRequest{
		action: "show",
		reply:  returnChannel,
		User: proto.User{
			Id: params.ByName("user"),
		},
	}
	result := <-returnChannel
	SendUserReply(&w, &result)
}

/* Write functions
 */
func AddUser(w http.ResponseWriter, r *http.Request,
	_ httprouter.Params) {
	defer PanicCatcher(w)

	cReq := proto.NewUserRequest()
	err := DecodeJsonBody(r, &cReq)
	if err != nil {
		DispatchBadRequest(&w, err)
		return
	}
	if strings.Contains(cReq.User.UserName, `:`) {
		DispatchBadRequest(&w, fmt.Errorf(`Invalid username containing : character`))
		return
	}

	returnChannel := make(chan somaResult)
	handler := handlerMap["userWriteHandler"].(somaUserWriteHandler)
	handler.input <- somaUserRequest{
		action: "add",
		reply:  returnChannel,
		User: proto.User{
			UserName:       cReq.User.UserName,
			FirstName:      cReq.User.FirstName,
			LastName:       cReq.User.LastName,
			EmployeeNumber: cReq.User.EmployeeNumber,
			MailAddress:    cReq.User.MailAddress,
			IsActive:       false,
			IsSystem:       cReq.User.IsSystem,
			IsDeleted:      false,
			TeamId:         cReq.User.TeamId,
		},
	}
	result := <-returnChannel
	SendUserReply(&w, &result)
}

func DeleteUser(w http.ResponseWriter, r *http.Request,
	params httprouter.Params) {
	defer PanicCatcher(w)
	action := "delete"

	cReq := proto.NewUserRequest()
	_ = DecodeJsonBody(r, &cReq)
	if cReq.Flags.Purge {
		action = "purge"
	}

	returnChannel := make(chan somaResult)
	handler := handlerMap["userWriteHandler"].(somaUserWriteHandler)
	handler.input <- somaUserRequest{
		action: action,
		reply:  returnChannel,
		User: proto.User{
			Id: params.ByName("user"),
		},
	}
	result := <-returnChannel
	SendUserReply(&w, &result)
}

/* Utility
 */
func SendUserReply(w *http.ResponseWriter, r *somaResult) {
	result := proto.NewUserResult()
	if r.MarkErrors(&result) {
		goto dispatch
	}
	for _, i := range (*r).Users {
		*result.Users = append(*result.Users, i.User)
		if i.ResultError != nil {
			*result.Errors = append(*result.Errors, i.ResultError.Error())
		}
	}

dispatch:
	json, err := json.Marshal(result)
	if err != nil {
		DispatchInternalError(w, err)
		return
	}
	DispatchJsonReply(w, &json)
	return
}

// vim: ts=4 sw=4 sts=4 noet fenc=utf-8 ffs=unix
<|endoftext|>


<commit_before>// Copyright (c) 2013 The AUTHORS
//
// Use of this source code is governed by The MIT License
// that can be found in the LICENSE file.

package main

import (
	// Stdlib
	"encoding/binary"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"os"
	"regexp"
	"strings"

	// Salsa
	"github.com/tchap/salsa/utils/flagutil"
	"github.com/tchap/salsa/utils/httputil"

	// Others
	"github.com/tchap/gocli"
	"gopkg.in/yaml.v1"
)

const crxURLTemplate = "https://clients2.google.com/service/update2/crx?response=redirect&x=id%3D~~~~%26uc"

// Subcommand initialisation and registration.
func init() {
	chromeExt := &gocli.Command{
		UsageLine: `
  chrome_ext SUBCMD`,
		Short: "manipulate Chrome Web Store extensions",
	}

	getName := &gocli.Command{
		UsageLine: `
  get_name MANIFEST_FILE`,
		Short:  "get name string from a manifest file",
		Action: getNameFromManifest,
	}
	chromeExt.MustRegisterSubcommand(getName)

	getVersion := &gocli.Command{
		UsageLine: `
  get_version MANIFEST_FILE`,
		Short:  "get version string from a manifest file",
		Action: getVersionFromManifest,
	}
	chromeExt.MustRegisterSubcommand(getVersion)

	getCrx := &gocli.Command{
		UsageLine: `
  get_crx [-zip] EXTENSION_ID FILENAME`,
		Short: "download Chrome extensions from Chrome Web Store",
		Long: `
  Download the extensions identified by EXTENSION_ID and save it in FILENAME.
  In case EXTENSION_ID is actually a URL, the address is used to retrieve the
  package directly.
		`,
		Action: runGetCrx,
	}
	getCrx.Flags.BoolVar(&convertCrxToZip, "zip", convertCrxToZip, "convert crx to zip")
	chromeExt.MustRegisterSubcommand(getCrx)

	genPackageJson := &gocli.Command{
		UsageLine: `
  gen_package_json [-dep NAME:VERSION ...] MANIFEST_FILE`,
		Short: "generate package.json from manifest.json",
		Long: `
  Generate package.json in the current working directory from manifest.json
  living at MANIFEST_FILE.

  Project name and version from manifest.json is reused, dependencies can be
  added to package.json using -dep option, which can be used multiple times.
  NAME is used as the dependency map key, version as the value
		`,
		Action: runGenPackageJson,
	}
	genPackageJson.Flags.Var(packageJsonDeps, "dep", "add a dependency into package.json")
	chromeExt.MustRegisterSubcommand(genPackageJson)

	getApp().MustRegisterSubcommand(chromeExt)
}

var convertCrxToZip bool

// Subcommand handler.
func runGetCrx(cmd *gocli.Command, args []string) {
	if len(args) != 2 {
		cmd.Usage()
		os.Exit(2)
	}

	var (
		id       = args[0]
		filename = args[1]
	)

	var packageURL string
	if regexp.MustCompile("^https?://").MatchString(id) {
		packageURL = id
	} else {
		packageURL = strings.Replace(crxURLTemplate, "~~~~", id, 1)
	}

	if config.Verbose() {
		fmt.Println("GET", packageURL)
	}
	if config.Dry() {
		return
	}

	// Download CRX.
	resp, err := httputil.Get(packageURL, nil)
	if err != nil {
		log.Fatalf("Error: failed to download crx: %v\n", err)
	}
	if resp.StatusCode >= 300 {
		log.Fatalf("Error: failed to download crx: %v\n", resp.Status)
	}
	defer resp.Body.Close()

	// Convert CRX to ZIP if requested.
	if convertCrxToZip {
		var (
			publicKeyLen uint32
			signatureLen uint32
		)

		// Drop magic number.
		if err := binary.Read(resp.Body, binary.LittleEndian, &publicKeyLen); err != nil {
			log.Fatal(err)
		}
		// Drop version.
		if err := binary.Read(resp.Body, binary.LittleEndian, &publicKeyLen); err != nil {
			log.Fatal(err)
		}
		// Read public key length.
		if err := binary.Read(resp.Body, binary.LittleEndian, &publicKeyLen); err != nil {
			log.Fatal(err)
		}
		// Read signature length.
		if err := binary.Read(resp.Body, binary.LittleEndian, &signatureLen); err != nil {
			log.Fatal(err)
		}

		var p []byte
		if publicKeyLen > signatureLen {
			p = make([]byte, publicKeyLen)
		} else {
			p = make([]byte, signatureLen)
		}

		// Drop the public key.
		b := p[:publicKeyLen]
		for i := uint32(0); i != publicKeyLen; {
			n, err := resp.Body.Read(b[i:])
			if err != nil {
				log.Fatal(err)
			}
			i += uint32(n)
		}
		// Drop the signature.
		b = p[:signatureLen]
		for i := uint32(0); i != signatureLen; {
			n, err := resp.Body.Read(b[i:])
			if err != nil {
				log.Fatal(err)
			}
			i += uint32(n)
		}
	}

	// Write it to the file.
	file, err := os.OpenFile(filename, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644)
	if err != nil {
		log.Fatalf("Error: %v\n", err)
	}
	defer file.Close()

	n, err := io.Copy(file, resp.Body)
	if err != nil {
		log.Fatalf("Error: %v\n", err)
	}

	fmt.Printf("Wrote %v bytes\n", n)
}

var packageJsonDeps = flagutil.NewMapValue()

// Subcommand handler.
func runGenPackageJson(cmd *gocli.Command, args []string) {
	if len(args) != 1 {
		cmd.Usage()
		os.Exit(2)
	}

	var manifestFilename = args[0]

	content, err := ioutil.ReadFile(manifestFilename)
	if err != nil {
		log.Fatalf("Error: %v", err)
	}

	var packageJson struct {
		Name         string            `json:"name"`
		Version      string            `json:"version"`
		Dependencies map[string]string `json:"dependencies"`
	}

	if err := yaml.Unmarshal(content, &packageJson); err != nil {
		log.Fatalf("Error: failed to unmarshal manifest.json: %v", err)
	}

	packageJson.Name = strings.ToLower(packageJson.Name)
	packageJson.Name = strings.Replace(packageJson.Name, " ", "-", -1)
	packageJson.Dependencies = packageJsonDeps.M
	switch strings.Count(packageJson.Version, ".") {
	case 0:
		packageJson.Version += ".0.0"
	case 1:
		packageJson.Version += ".0"
	}

	content, err = json.MarshalIndent(packageJson, "", "  ")
	if err != nil {
		log.Fatalf("Error: failed to marshal package.json: %v", err)
	}

	file, err := os.OpenFile("package.json", os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644)
	if err != nil {
		log.Fatalf("Error: %v", err)
	}
	defer file.Close()

	if _, err := file.Write(content); err != nil {
		log.Fatalf("Error: %v", err)
	}

	fmt.Println("package.json created")
}

func getNameFromManifest(cmd *gocli.Command, args []string) {
	if len(args) != 1 {
		cmd.Usage()
		os.Exit(2)
	}

	manifest, err := loadManifest(args[0])
	if err != nil {
		log.Fatalf("Error: %v\n", err)
	}

	fmt.Print(manifest.Name)
}

func getVersionFromManifest(cmd *gocli.Command, args []string) {
	if len(args) != 1 {
		cmd.Usage()
		os.Exit(2)
	}

	manifest, err := loadManifest(args[0])
	if err != nil {
		log.Fatalf("Error: %v\n", err)
	}

	fmt.Print(manifest.Version)
}

type manifest struct {
	Name    string
	Version string
}

func loadManifest(filename string) (*manifest, error) {
	content, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil, err
	}

	var m manifest
	if err := yaml.Unmarshal(content, &m); err != nil {
		return nil, err
	}

	return &m, nil
}
<commit_msg>bugfix: gen_package_json: Version string reloaded<commit_after>// Copyright (c) 2013 The AUTHORS
//
// Use of this source code is governed by The MIT License
// that can be found in the LICENSE file.

package main

import (
	// Stdlib
	"encoding/binary"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"os"
	"regexp"
	"strings"

	// Salsa
	"github.com/tchap/salsa/utils/flagutil"
	"github.com/tchap/salsa/utils/httputil"

	// Others
	"github.com/tchap/gocli"
	"gopkg.in/yaml.v1"
)

const crxURLTemplate = "https://clients2.google.com/service/update2/crx?response=redirect&x=id%3D~~~~%26uc"

// Subcommand initialisation and registration.
func init() {
	chromeExt := &gocli.Command{
		UsageLine: `
  chrome_ext SUBCMD`,
		Short: "manipulate Chrome Web Store extensions",
	}

	getName := &gocli.Command{
		UsageLine: `
  get_name MANIFEST_FILE`,
		Short:  "get name string from a manifest file",
		Action: getNameFromManifest,
	}
	chromeExt.MustRegisterSubcommand(getName)

	getVersion := &gocli.Command{
		UsageLine: `
  get_version MANIFEST_FILE`,
		Short:  "get version string from a manifest file",
		Action: getVersionFromManifest,
	}
	chromeExt.MustRegisterSubcommand(getVersion)

	getCrx := &gocli.Command{
		UsageLine: `
  get_crx [-zip] EXTENSION_ID FILENAME`,
		Short: "download Chrome extensions from Chrome Web Store",
		Long: `
  Download the extensions identified by EXTENSION_ID and save it in FILENAME.
  In case EXTENSION_ID is actually a URL, the address is used to retrieve the
  package directly.
		`,
		Action: runGetCrx,
	}
	getCrx.Flags.BoolVar(&convertCrxToZip, "zip", convertCrxToZip, "convert crx to zip")
	chromeExt.MustRegisterSubcommand(getCrx)

	genPackageJson := &gocli.Command{
		UsageLine: `
  gen_package_json [-dep NAME:VERSION ...] MANIFEST_FILE`,
		Short: "generate package.json from manifest.json",
		Long: `
  Generate package.json in the current working directory from manifest.json
  living at MANIFEST_FILE.

  Project name and version from manifest.json is reused, dependencies can be
  added to package.json using -dep option, which can be used multiple times.
  NAME is used as the dependency map key, version as the value
		`,
		Action: runGenPackageJson,
	}
	genPackageJson.Flags.Var(packageJsonDeps, "dep", "add a dependency into package.json")
	chromeExt.MustRegisterSubcommand(genPackageJson)

	getApp().MustRegisterSubcommand(chromeExt)
}

var convertCrxToZip bool

// Subcommand handler.
func runGetCrx(cmd *gocli.Command, args []string) {
	if len(args) != 2 {
		cmd.Usage()
		os.Exit(2)
	}

	var (
		id       = args[0]
		filename = args[1]
	)

	var packageURL string
	if regexp.MustCompile("^https?://").MatchString(id) {
		packageURL = id
	} else {
		packageURL = strings.Replace(crxURLTemplate, "~~~~", id, 1)
	}

	if config.Verbose() {
		fmt.Println("GET", packageURL)
	}
	if config.Dry() {
		return
	}

	// Download CRX.
	resp, err := httputil.Get(packageURL, nil)
	if err != nil {
		log.Fatalf("Error: failed to download crx: %v\n", err)
	}
	if resp.StatusCode >= 300 {
		log.Fatalf("Error: failed to download crx: %v\n", resp.Status)
	}
	defer resp.Body.Close()

	// Convert CRX to ZIP if requested.
	if convertCrxToZip {
		var (
			publicKeyLen uint32
			signatureLen uint32
		)

		// Drop magic number.
		if err := binary.Read(resp.Body, binary.LittleEndian, &publicKeyLen); err != nil {
			log.Fatal(err)
		}
		// Drop version.
		if err := binary.Read(resp.Body, binary.LittleEndian, &publicKeyLen); err != nil {
			log.Fatal(err)
		}
		// Read public key length.
		if err := binary.Read(resp.Body, binary.LittleEndian, &publicKeyLen); err != nil {
			log.Fatal(err)
		}
		// Read signature length.
		if err := binary.Read(resp.Body, binary.LittleEndian, &signatureLen); err != nil {
			log.Fatal(err)
		}

		var p []byte
		if publicKeyLen > signatureLen {
			p = make([]byte, publicKeyLen)
		} else {
			p = make([]byte, signatureLen)
		}

		// Drop the public key.
		b := p[:publicKeyLen]
		for i := uint32(0); i != publicKeyLen; {
			n, err := resp.Body.Read(b[i:])
			if err != nil {
				log.Fatal(err)
			}
			i += uint32(n)
		}
		// Drop the signature.
		b = p[:signatureLen]
		for i := uint32(0); i != signatureLen; {
			n, err := resp.Body.Read(b[i:])
			if err != nil {
				log.Fatal(err)
			}
			i += uint32(n)
		}
	}

	// Write it to the file.
	file, err := os.OpenFile(filename, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644)
	if err != nil {
		log.Fatalf("Error: %v\n", err)
	}
	defer file.Close()

	n, err := io.Copy(file, resp.Body)
	if err != nil {
		log.Fatalf("Error: %v\n", err)
	}

	fmt.Printf("Wrote %v bytes\n", n)
}

var packageJsonDeps = flagutil.NewMapValue()

// Subcommand handler.
func runGenPackageJson(cmd *gocli.Command, args []string) {
	if len(args) != 1 {
		cmd.Usage()
		os.Exit(2)
	}

	var manifestFilename = args[0]

	content, err := ioutil.ReadFile(manifestFilename)
	if err != nil {
		log.Fatalf("Error: %v", err)
	}

	var packageJson struct {
		Name         string            `json:"name"`
		Version      string            `json:"version"`
		Dependencies map[string]string `json:"dependencies"`
	}

	if err := yaml.Unmarshal(content, &packageJson); err != nil {
		log.Fatalf("Error: failed to unmarshal manifest.json: %v", err)
	}

	packageJson.Name = strings.ToLower(packageJson.Name)
	packageJson.Name = strings.Replace(packageJson.Name, " ", "-", -1)
	packageJson.Dependencies = packageJsonDeps.M
	switch strings.Count(packageJson.Version, ".") {
	case 0:
		packageJson.Version += ".0.0"
	case 1:
		packageJson.Version += ".0"
	case 2:
	case 3:
		i := strings.LastIndex(packageJson.Version, ".")
		packageJson.Version = packageJson.Version[:i] + "-" + packageJson.Version[i+1:]
	default:
		panic("invalid version string: " + packageJson.Version)
	}

	content, err = json.MarshalIndent(packageJson, "", "  ")
	if err != nil {
		log.Fatalf("Error: failed to marshal package.json: %v", err)
	}

	file, err := os.OpenFile("package.json", os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644)
	if err != nil {
		log.Fatalf("Error: %v", err)
	}
	defer file.Close()

	if _, err := file.Write(content); err != nil {
		log.Fatalf("Error: %v", err)
	}

	fmt.Println("package.json created")
}

func getNameFromManifest(cmd *gocli.Command, args []string) {
	if len(args) != 1 {
		cmd.Usage()
		os.Exit(2)
	}

	manifest, err := loadManifest(args[0])
	if err != nil {
		log.Fatalf("Error: %v\n", err)
	}

	fmt.Print(manifest.Name)
}

func getVersionFromManifest(cmd *gocli.Command, args []string) {
	if len(args) != 1 {
		cmd.Usage()
		os.Exit(2)
	}

	manifest, err := loadManifest(args[0])
	if err != nil {
		log.Fatalf("Error: %v\n", err)
	}

	fmt.Print(manifest.Version)
}

type manifest struct {
	Name    string
	Version string
}

func loadManifest(filename string) (*manifest, error) {
	content, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil, err
	}

	var m manifest
	if err := yaml.Unmarshal(content, &m); err != nil {
		return nil, err
	}

	return &m, nil
}
<|endoftext|>


<commit_before>package ci

import (
	"bufio"
	"encoding/json"
	"github.com/xlvector/caspercloud"
	"io/ioutil"
	"log"
	"net"
	"net/http"
	"os/exec"
	"testing"
	"time"
)

var client *http.Client

const (
	ENDPOINT = "http://127.0.0.1:8000"
)

func init() {
	go runMockSite()
	client = &http.Client{}
}

func runMockSite() {
	service := caspercloud.NewCasperServer()
	http.Handle("/submit", service)
	l, e := net.Listen("tcp", ":8000")
	if e != nil {
		log.Fatal("listen error:", e)
	}
	http.Serve(l, nil)
}

func getJson(link string) map[string]string {
	resp, err := client.Get(link)
	if err != nil {
		log.Println("fail to get resp")
		return nil
	}
	defer resp.Body.Close()
	out, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Println("fail to read output")
		return nil
	}
	ret := make(map[string]string)
	json.Unmarshal(out, &ret)
	return ret
}

func runCmd(cmd *exec.Cmd, b *testing.B, info bool) {
	stdout, err := cmd.StdoutPipe()
	if err != nil {
		log.Panicln("can not get stdout pipe:", err)
	}
	bufout := bufio.NewReader(stdout)
	err = cmd.Start()
	if err != nil {
		b.Error("fail to run curl")
	}
	for {
		line, err := bufout.ReadString('\n')
		if err != nil {
			break
		}
		if info {
			log.Println(line)
		}
	}
	cmd.Wait()
}

func BenchmarkCurl(b *testing.B) {
	for i := 0; i < b.N; i++ {
		cmd := exec.Command("curl", ENDPOINT+"/hello")
		runCmd(cmd, b, false)
	}
}

func BenchmarkHttpClient(b *testing.B) {
	for i := 0; i < b.N; i++ {
		resp, err := client.Get(ENDPOINT + "/hello")
		if err != nil {
			b.Error(err)
		}
		defer resp.Body.Close()
		ioutil.ReadAll(resp.Body)
	}
}

func BenchmarkCasperJs(b *testing.B) {
	for i := 0; i < b.N; i++ {
		cmd := exec.Command("casperjs", "mock.js")
		runCmd(cmd, b, false)
	}
}

func BenchmarkCurl100(b *testing.B) {
	for i := 0; i < b.N; i++ {
		cmd := exec.Command("curl", ENDPOINT+"/hello?query=[1-100]")
		runCmd(cmd, b, false)
	}
}

func BenchmarkHttpClient100(b *testing.B) {
	for i := 0; i < b.N; i++ {
		for j := 0; j < 100; j++ {
			resp, err := client.Get(ENDPOINT + "/hello")
			if err != nil {
				b.Error(err)
			}
			defer resp.Body.Close()
			ioutil.ReadAll(resp.Body)
		}
	}
}

func BenchmarkCasperJs100(b *testing.B) {
	for i := 0; i < b.N; i++ {
		cmd := exec.Command("casperjs", "mock_100.js")
		runCmd(cmd, b, false)
	}
}

func BenchmarkPhantomJs(b *testing.B) {
	for i := 0; i < b.N; i++ {
		cmd := exec.Command("phantomjs", "loadspeed.js", ENDPOINT+"/hello", "1")
		runCmd(cmd, b, true)
	}
}

func TestHello(t *testing.T) {
	for i := 0; i < 5; i++ {
		go func() {
			queries := []string{"sina", "twitter", "xlvector", "bigtong"}
			for _, q := range queries {
				time.Sleep(time.Millisecond * 50)
				ret := getJson(ENDPOINT + "/submit?tmpl=hello&_query=" + q)
				if v, _ := ret["result"]; v != q {
					t.Error(v)
				}
			}
		}()
	}
	time.Sleep(time.Second * 5)
}

func TestForm(t *testing.T) {
	for i := 0; i < 5; i++ {
		go func() {
			ret := getJson(ENDPOINT + "/submit?tmpl=form")
			id, ok := ret["id"]
			if !ok {
				t.Error("can not find id in result")
				return
			}
			ret = getJson(ENDPOINT + "/submit?tmpl=form&_phone=18612345678&id=" + id)
			log.Println(ret)
			ret = getJson(ENDPOINT + "/submit?tmpl=form&_verify_code=123456&id=" + id)
			log.Println(ret)
			if ret["result"] != "Thanks" {
				t.Error("result not right:" + ret["result"])
			}
		}()
	}
	time.Sleep(time.Second * 5)
}
<commit_msg>print result when fail ci<commit_after>package ci

import (
	"bufio"
	"encoding/json"
	"github.com/xlvector/caspercloud"
	"io/ioutil"
	"log"
	"net"
	"net/http"
	"os/exec"
	"testing"
	"time"
)

var client *http.Client

const (
	ENDPOINT = "http://127.0.0.1:8000"
)

func init() {
	go runMockSite()
	client = &http.Client{}
}

func runMockSite() {
	service := caspercloud.NewCasperServer()
	http.Handle("/submit", service)
	l, e := net.Listen("tcp", ":8000")
	if e != nil {
		log.Fatal("listen error:", e)
	}
	http.Serve(l, nil)
}

func getJson(link string) map[string]string {
	resp, err := client.Get(link)
	if err != nil {
		log.Println("fail to get resp")
		return nil
	}
	defer resp.Body.Close()
	out, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Println("fail to read output")
		return nil
	}
	ret := make(map[string]string)
	json.Unmarshal(out, &ret)
	return ret
}

func runCmd(cmd *exec.Cmd, b *testing.B, info bool) {
	stdout, err := cmd.StdoutPipe()
	if err != nil {
		log.Panicln("can not get stdout pipe:", err)
	}
	bufout := bufio.NewReader(stdout)
	err = cmd.Start()
	if err != nil {
		b.Error("fail to run curl")
	}
	for {
		line, err := bufout.ReadString('\n')
		if err != nil {
			break
		}
		if info {
			log.Println(line)
		}
	}
	cmd.Wait()
}

func BenchmarkCurl(b *testing.B) {
	for i := 0; i < b.N; i++ {
		cmd := exec.Command("curl", ENDPOINT+"/hello")
		runCmd(cmd, b, false)
	}
}

func BenchmarkHttpClient(b *testing.B) {
	for i := 0; i < b.N; i++ {
		resp, err := client.Get(ENDPOINT + "/hello")
		if err != nil {
			b.Error(err)
		}
		defer resp.Body.Close()
		ioutil.ReadAll(resp.Body)
	}
}

func BenchmarkCasperJs(b *testing.B) {
	for i := 0; i < b.N; i++ {
		cmd := exec.Command("casperjs", "mock.js")
		runCmd(cmd, b, false)
	}
}

func BenchmarkCurl100(b *testing.B) {
	for i := 0; i < b.N; i++ {
		cmd := exec.Command("curl", ENDPOINT+"/hello?query=[1-100]")
		runCmd(cmd, b, false)
	}
}

func BenchmarkHttpClient100(b *testing.B) {
	for i := 0; i < b.N; i++ {
		for j := 0; j < 100; j++ {
			resp, err := client.Get(ENDPOINT + "/hello")
			if err != nil {
				b.Error(err)
			}
			defer resp.Body.Close()
			ioutil.ReadAll(resp.Body)
		}
	}
}

func BenchmarkCasperJs100(b *testing.B) {
	for i := 0; i < b.N; i++ {
		cmd := exec.Command("casperjs", "mock_100.js")
		runCmd(cmd, b, false)
	}
}

func BenchmarkPhantomJs(b *testing.B) {
	for i := 0; i < b.N; i++ {
		cmd := exec.Command("phantomjs", "loadspeed.js", ENDPOINT+"/hello", "1")
		runCmd(cmd, b, true)
	}
}

func TestHello(t *testing.T) {
	for i := 0; i < 5; i++ {
		go func() {
			queries := []string{"sina", "twitter", "xlvector", "bigtong"}
			for _, q := range queries {
				time.Sleep(time.Millisecond * 50)
				ret := getJson(ENDPOINT + "/submit?tmpl=hello&_query=" + q)
				if v, _ := ret["result"]; v != q {
					t.Error(v)
				}
			}
		}()
	}
	time.Sleep(time.Second * 5)
}

func TestForm(t *testing.T) {
	for i := 0; i < 5; i++ {
		go func() {
			ret := getJson(ENDPOINT + "/submit?tmpl=form")
			id, ok := ret["id"]
			if !ok {
				t.Error("can not find id in result")
				return
			}
			ret = getJson(ENDPOINT + "/submit?tmpl=form&_phone=18612345678&id=" + id)
			log.Println(ret)
			ret = getJson(ENDPOINT + "/submit?tmpl=form&_verify_code=123456&id=" + id)
			log.Println(ret)
			if v, ok := ret["result"]; !ok || v != "Thanks" {
				log.Println(v)
				t.Error("result not right:" + v)
			}
		}()
	}
	time.Sleep(time.Second * 5)
}
<|endoftext|>


<commit_before>// The package provides methods for converting amounts between currencies. The
// exchange rates are provided by the ECB (http://www.ecb.europa.eu/).
//
// Author: Michael Banzon
package currency

import (
	"errors"
	"fmt"
	"time"
)

const (
	eur     = "EUR"
	unknown = "Unknown currency: %s"
)

type CurrencyConverter struct {
	date       time.Time
	currencies map[string]float64
}

type SingleCurrencyConverter struct {
	date             time.Time
	from, to         string
	fromRate, toRate float64
}

func NewConverter() (*CurrencyConverter, error) {
	currencyTime, currencies, err := parseEcbData()
	if err != nil {
		return nil, err
	}
	converter := CurrencyConverter{date: currencyTime, currencies: currencies}
	return &converter, nil
}

func (c *CurrencyConverter) Age() float64 {
	delta := c.date.Sub(time.Now())
	return delta.Hours() / 24
}

func (c *CurrencyConverter) ShouldRenew() bool {
	if c.Age() >= 1 {
		today := time.Now()
		if today.Weekday() > time.Sunday && today.Weekday() < time.Saturday {
			return true
		}
	}
	return false
}

func (c *CurrencyConverter) Renew() error {
	date, currencies, err := parseEcbData()
	if err != nil {
		return err
	} else {
		c.date = date
		c.currencies = currencies
	}
}

func (c *CurrencyConverter) Convert(amount float64, from string, to string) (float64, error) {
	fromRate, fromOk := c.currencies[from]
	if !fromOk {
		return 0, errors.New(fmt.Sprintf(unknown, from))
	}

	toRate, toOk := c.currencies[to]
	if !toOk {
		return 0, errors.New(fmt.Sprintf(unknown, to))
	}

	return amount / fromRate * toRate, nil
}

func (c *CurrencyConverter) MultiConvert(amounts []float64, from, to string) ([]float64, error) {
	convertedAmounts := make([]float64, len(amounts))
	var e error
	for i, amount := range amounts {
		converted, err := c.Convert(amount, from, to)
		if err != nil {
			e = err
		}
		convertedAmounts[i] = converted
	}
	return convertedAmounts, e
}

func (c *CurrencyConverter) GetSingleCurrencyConverter(from, to string) (*SingleCurrencyConverter, error) {
	fromRate, fromOk := c.currencies[from]
	if !fromOk {
		return nil, errors.New(fmt.Sprintf(unknown, from))
	}
	toRate, toOk := c.currencies[to]
	if !toOk {
		return nil, errors.New(fmt.Sprintf(unknown, to))
	}

	converter := SingleCurrencyConverter{date: c.date, from: from, to: to, fromRate: fromRate, toRate: toRate}
	return &converter, nil
}

func (c *SingleCurrencyConverter) Convert(amount float64) float64 {
	return amount / c.fromRate * c.toRate
}

func (c *SingleCurrencyConverter) MultiConvert(amounts []float64) []float64 {
	converted := make([]float64, len(amounts))
	for i, amount := range amounts {
		converted[i] = c.Convert(amount)
	}
	return converted
}
<commit_msg>Comments.<commit_after>// The package provides methods for converting amounts between currencies. The
// exchange rates are provided by the ECB (http://www.ecb.europa.eu/).
//
// Author: Michael Banzon
package currency

import (
	"errors"
	"fmt"
	"time"
)

const (
	eur     = "EUR"
	unknown = "Unknown currency: %s"
)

// The CurrencyConverter struct holds the data that enables the conversion.
// Upon creation the data is fetched from the ECB and parsed into the struct.
type CurrencyConverter struct {
	date       time.Time
	currencies map[string]float64
}

// The SingleCurrencyConverter struct holds data about how to convert amounts
// between two pre-defined currencies.
type SingleCurrencyConverter struct {
	date             time.Time
	from, to         string
	fromRate, toRate float64
}

// Creates a new converter by fetching the required data from the ECB.
func NewConverter() (*CurrencyConverter, error) {
	currencyTime, currencies, err := parseEcbData()
	if err != nil {
		return nil, err
	}
	converter := CurrencyConverter{date: currencyTime, currencies: currencies}
	return &converter, nil
}

// Calculates the age in days of the CurrencyConverter. The age is calculated
// using the date supplied in the currency feed from the ECB.
func (c *CurrencyConverter) Age() float64 {
	delta := c.date.Sub(time.Now())
	return delta.Hours() / 24
}

func (c *CurrencyConverter) ShouldRenew() bool {
	if c.Age() >= 1 {
		today := time.Now()
		if today.Weekday() > time.Sunday && today.Weekday() < time.Saturday {
			return true
		}
	}
	return false
}

func (c *CurrencyConverter) Renew() error {
	date, currencies, err := parseEcbData()
	if err != nil {
		return err
	} else {
		c.date = date
		c.currencies = currencies
	}
}

func (c *CurrencyConverter) Convert(amount float64, from string, to string) (float64, error) {
	fromRate, fromOk := c.currencies[from]
	if !fromOk {
		return 0, errors.New(fmt.Sprintf(unknown, from))
	}

	toRate, toOk := c.currencies[to]
	if !toOk {
		return 0, errors.New(fmt.Sprintf(unknown, to))
	}

	return amount / fromRate * toRate, nil
}

func (c *CurrencyConverter) MultiConvert(amounts []float64, from, to string) ([]float64, error) {
	convertedAmounts := make([]float64, len(amounts))
	var e error
	for i, amount := range amounts {
		converted, err := c.Convert(amount, from, to)
		if err != nil {
			e = err
		}
		convertedAmounts[i] = converted
	}
	return convertedAmounts, e
}

func (c *CurrencyConverter) GetSingleCurrencyConverter(from, to string) (*SingleCurrencyConverter, error) {
	fromRate, fromOk := c.currencies[from]
	if !fromOk {
		return nil, errors.New(fmt.Sprintf(unknown, from))
	}
	toRate, toOk := c.currencies[to]
	if !toOk {
		return nil, errors.New(fmt.Sprintf(unknown, to))
	}

	converter := SingleCurrencyConverter{date: c.date, from: from, to: to, fromRate: fromRate, toRate: toRate}
	return &converter, nil
}

func (c *SingleCurrencyConverter) Convert(amount float64) float64 {
	return amount / c.fromRate * c.toRate
}

func (c *SingleCurrencyConverter) MultiConvert(amounts []float64) []float64 {
	converted := make([]float64, len(amounts))
	for i, amount := range amounts {
		converted[i] = c.Convert(amount)
	}
	return converted
}
<|endoftext|>


<commit_before>// The package provides methods for converting amounts between currencies. The
// exchange rates are provided by the ECB (http://www.ecb.europa.eu/).
package currency

import (
	"errors"
	"fmt"
	"time"
)

const (
	eur     = "EUR"
	unknown = "Unknown currency: %s"
)

// The CurrencyConverter struct holds the data that enables the conversion.
// Upon creation the data is fetched from the ECB and parsed into the struct.
type CurrencyConverter struct {
	date             time.Time
	currencies       map[string]float64
	singleConverters []*SingleCurrencyConverter
}

// The SingleCurrencyConverter struct holds data about how to convert amounts
// between two pre-defined currencies.
type SingleCurrencyConverter struct {
	date             time.Time
	from, to         string
	fromRate, toRate float64
}

// Creates a new converter by fetching the required data from the ECB.
func NewConverter() (*CurrencyConverter, error) {
	currencyTime, currencies, err := parseEcbData()
	if err != nil {
		return nil, err
	}
	converter := CurrencyConverter{date: currencyTime, currencies: currencies}
	return &converter, nil
}

func (c *CurrencyConverter) GetCurrencies() []string {
	currencies := make([]string, len(c.currencies))
	index := 0
	for currency, _ := range c.currencies {
		currencies[index] = currency
		index++
	}
	return currencies
}

func (c *CurrencyConverter) HasCurrency(currency string) bool {
	_, ok := c.currencies[currency]
	return ok
}

// Calculates the age in days of the CurrencyConverter. The age is calculated
// using the date supplied in the currency feed from the ECB.
func (c *CurrencyConverter) Age() float64 {
	delta := c.date.Sub(time.Now())
	return delta.Hours() / 24
}

// Returns true if the currencies stores are so old they should be renewed from
// the ECB server.
func (c *CurrencyConverter) ShouldRenew() bool {
	if c.Age() >= 1 {
		today := time.Now()
		if today.Weekday() > time.Sunday && today.Weekday() < time.Saturday {
			return true
		}
	}
	return false
}

// Renew the currency data by fetching the  from the ECB server. This will
// also update all the SingleCurrencyConverter created from this CurrencyConverter.
func (c *CurrencyConverter) Renew() error {
	date, currencies, err := parseEcbData()
	if err != nil {
		return err
	} else {
		c.date = date
		c.currencies = currencies
		for _, s := range c.singleConverters {
			s.renew(c)
		}
		return nil
	}
}

// Converts an amount between two currencies.
func (c *CurrencyConverter) Convert(amount float64, from string, to string) (float64, error) {
	fromRate, fromOk := c.currencies[from]
	if !fromOk {
		return 0, errors.New(fmt.Sprintf(unknown, from))
	}

	toRate, toOk := c.currencies[to]
	if !toOk {
		return 0, errors.New(fmt.Sprintf(unknown, to))
	}

	return amount / fromRate * toRate, nil
}

// Converts a slice of amounts from one currency to another.
func (c *CurrencyConverter) MultiConvert(amounts []float64, from, to string) ([]float64, error) {
	convertedAmounts := make([]float64, len(amounts))
	var e error
	for i, amount := range amounts {
		converted, err := c.Convert(amount, from, to)
		if err != nil {
			e = err
		}
		convertedAmounts[i] = converted
	}
	return convertedAmounts, e
}

// Creates a SingleCurrencyConverter that easilly translates amounts between
// two fixed currencies.
func (c *CurrencyConverter) GetSingleCurrencyConverter(from, to string) (*SingleCurrencyConverter, error) {
	fromRate, fromOk := c.currencies[from]
	if !fromOk {
		return nil, errors.New(fmt.Sprintf(unknown, from))
	}
	toRate, toOk := c.currencies[to]
	if !toOk {
		return nil, errors.New(fmt.Sprintf(unknown, to))
	}

	converter := SingleCurrencyConverter{date: c.date, from: from, to: to, fromRate: fromRate, toRate: toRate}
	c.singleConverters = append(c.singleConverters, &converter)
	return &converter, nil
}

// Converts a single amount.
func (c *SingleCurrencyConverter) Convert(amount float64) float64 {
	return amount / c.fromRate * c.toRate
}

// Converts multiple amounts.
func (c *SingleCurrencyConverter) MultiConvert(amounts []float64) []float64 {
	converted := make([]float64, len(amounts))
	for i, amount := range amounts {
		converted[i] = c.Convert(amount)
	}
	return converted
}

func (c *SingleCurrencyConverter) renew(r *CurrencyConverter) error {
	fromRate, fromOk := r.currencies[c.from]
	if !fromOk {
		return errors.New(fmt.Sprintf(unknown, c.from))
	}
	toRate, toOk := r.currencies[c.to]
	if !toOk {
		return errors.New(fmt.Sprintf(unknown, c.to))
	}

	c.fromRate = fromRate
	c.toRate = toRate
	c.date = r.date
	return nil
}
<commit_msg>Comment.<commit_after>// The package provides methods for converting amounts between currencies. The
// exchange rates are provided by the ECB (http://www.ecb.europa.eu/).
package currency

import (
	"errors"
	"fmt"
	"time"
)

const (
	eur     = "EUR"
	unknown = "Unknown currency: %s"
)

// The CurrencyConverter struct holds the data that enables the conversion.
// Upon creation the data is fetched from the ECB and parsed into the struct.
type CurrencyConverter struct {
	date             time.Time
	currencies       map[string]float64
	singleConverters []*SingleCurrencyConverter
}

// The SingleCurrencyConverter struct holds data about how to convert amounts
// between two pre-defined currencies.
type SingleCurrencyConverter struct {
	date             time.Time
	from, to         string
	fromRate, toRate float64
}

// Creates a new converter by fetching the required data from the ECB.
func NewConverter() (*CurrencyConverter, error) {
	currencyTime, currencies, err := parseEcbData()
	if err != nil {
		return nil, err
	}
	converter := CurrencyConverter{date: currencyTime, currencies: currencies}
	return &converter, nil
}

func (c *CurrencyConverter) GetCurrencies() []string {
	currencies := make([]string, len(c.currencies))
	index := 0
	for currency, _ := range c.currencies {
		currencies[index] = currency
		index++
	}
	return currencies
}

// Returns true if the currency is known by the converter.
func (c *CurrencyConverter) HasCurrency(currency string) bool {
	_, ok := c.currencies[currency]
	return ok
}

// Calculates the age in days of the CurrencyConverter. The age is calculated
// using the date supplied in the currency feed from the ECB.
func (c *CurrencyConverter) Age() float64 {
	delta := c.date.Sub(time.Now())
	return delta.Hours() / 24
}

// Returns true if the currencies stores are so old they should be renewed from
// the ECB server.
func (c *CurrencyConverter) ShouldRenew() bool {
	if c.Age() >= 1 {
		today := time.Now()
		if today.Weekday() > time.Sunday && today.Weekday() < time.Saturday {
			return true
		}
	}
	return false
}

// Renew the currency data by fetching the  from the ECB server. This will
// also update all the SingleCurrencyConverter created from this CurrencyConverter.
func (c *CurrencyConverter) Renew() error {
	date, currencies, err := parseEcbData()
	if err != nil {
		return err
	} else {
		c.date = date
		c.currencies = currencies
		for _, s := range c.singleConverters {
			s.renew(c)
		}
		return nil
	}
}

// Converts an amount between two currencies.
func (c *CurrencyConverter) Convert(amount float64, from string, to string) (float64, error) {
	fromRate, fromOk := c.currencies[from]
	if !fromOk {
		return 0, errors.New(fmt.Sprintf(unknown, from))
	}

	toRate, toOk := c.currencies[to]
	if !toOk {
		return 0, errors.New(fmt.Sprintf(unknown, to))
	}

	return amount / fromRate * toRate, nil
}

// Converts a slice of amounts from one currency to another.
func (c *CurrencyConverter) MultiConvert(amounts []float64, from, to string) ([]float64, error) {
	convertedAmounts := make([]float64, len(amounts))
	var e error
	for i, amount := range amounts {
		converted, err := c.Convert(amount, from, to)
		if err != nil {
			e = err
		}
		convertedAmounts[i] = converted
	}
	return convertedAmounts, e
}

// Creates a SingleCurrencyConverter that easilly translates amounts between
// two fixed currencies.
func (c *CurrencyConverter) GetSingleCurrencyConverter(from, to string) (*SingleCurrencyConverter, error) {
	fromRate, fromOk := c.currencies[from]
	if !fromOk {
		return nil, errors.New(fmt.Sprintf(unknown, from))
	}
	toRate, toOk := c.currencies[to]
	if !toOk {
		return nil, errors.New(fmt.Sprintf(unknown, to))
	}

	converter := SingleCurrencyConverter{date: c.date, from: from, to: to, fromRate: fromRate, toRate: toRate}
	c.singleConverters = append(c.singleConverters, &converter)
	return &converter, nil
}

// Converts a single amount.
func (c *SingleCurrencyConverter) Convert(amount float64) float64 {
	return amount / c.fromRate * c.toRate
}

// Converts multiple amounts.
func (c *SingleCurrencyConverter) MultiConvert(amounts []float64) []float64 {
	converted := make([]float64, len(amounts))
	for i, amount := range amounts {
		converted[i] = c.Convert(amount)
	}
	return converted
}

func (c *SingleCurrencyConverter) renew(r *CurrencyConverter) error {
	fromRate, fromOk := r.currencies[c.from]
	if !fromOk {
		return errors.New(fmt.Sprintf(unknown, c.from))
	}
	toRate, toOk := r.currencies[c.to]
	if !toOk {
		return errors.New(fmt.Sprintf(unknown, c.to))
	}

	c.fromRate = fromRate
	c.toRate = toRate
	c.date = r.date
	return nil
}
<|endoftext|>


<commit_before>/*
 * Package handles connecting and writing to the database.
 */
package database

import (
	"database/sql"
	"errors"
	"fmt"
	"log"
	"strconv"
	"time"

	"github.com/lib/pq"

	"github.com/seadsystem/Backend/DB/landingzone/constants"
	"github.com/seadsystem/Backend/DB/landingzone/decoders/eGaugeDecoders"
	"github.com/seadsystem/Backend/DB/landingzone/decoders/seadPlugDecoders"
)

var NoData error = errors.New("No data in packet.")
var InvalidTime error = errors.New("Invalid time.")

type DB struct {
	conn *sql.DB
}

func New() (DB, error) {
	conn, err := sql.Open("postgres", fmt.Sprintf("host=%s user=%s dbname=%s password=%s port=%d sslmode=disable", constants.DB_SOCKET, constants.DB_USER, constants.DB_NAME, constants.DB_PASSWORD, constants.DB_PORT))
	return DB{conn}, err
}

func (db DB) SetMaxOpenConns(n int) {
	db.conn.SetMaxOpenConns(n)
}

func (db DB) InsertSeadPacket(data seadPlugDecoders.SeadPacket) {
	log.Println("Beginning transaction...")
	// Begin transaction. Required for bulk insert
	txn, err := db.conn.Begin()
	if err != nil {
		log.Println(err)
		return
	}

	// Packet wide declerations
	data_type := string(data.Type)
	interp_time := data.Timestamp // Set timestamp for first data point to time in packet

	// Prepare bulk insert statement
	stmt, err := txn.Prepare(pq.CopyIn("data_raw", "serial", "type", "data", "time"))
	if err != nil {
		log.Println(err)
		goto closetrans
	}

	log.Println("Processing data...")

	// Process data
	for _, element := range data.Data {
		if constants.Verbose {
			log.Println("Data:", element)
			log.Println("Time:", interp_time)
		}

		_, err = stmt.Exec(data.Serial, data_type, element, interp_time) // Insert data. This is buffered.
		interp_time = interp_time.Add(data.Period)                       // Add data point time spacing for next data point
		if err != nil {
			log.Println(err)
			break
		}
	}

closetrans:
	log.Println("Closing off transaction...")

	// Flush buffer
	_, err = stmt.Exec()
	if err != nil {
		log.Fatal(err)
	}

	// Close prepared statement
	err = stmt.Close()
	if err != nil {
		log.Fatal(err)
	}

	// Commit transaction
	err = txn.Commit()
	if err != nil {
		log.Fatal(err)
	}

	log.Println("Transaction closed")
}

func (db DB) InsertEGaugePacket(packet eGaugeDecoders.Packet) (err error) {
	log.Println("Peliminary data processing...")

	// Process data packet
	log.Println("Reading serial:", packet.Serial)
	serial, err := strconv.ParseInt(packet.Serial, 0, 64)
	if err != nil || serial <= 0 {
		log.Println()
		if err != nil {
			log.Println("Error reading serial:", err)
		} else {
			log.Println("Invalid serial:", serial)
		}
		return
	}

	log.Println("Beginning transaction...")
	// Begin transaction. Required for bulk insert
	txn, err := db.conn.Begin()
	if err != nil {
		log.Println(err)
		return
	}

	// Select best data set
	if len(packet.Data) == 0 {
		log.Println("Error: No data in packet")
		return NoData
	}

	data := &packet.Data[0]
	columns := &data.Cnames
	for i := 1; i < len(packet.Data); i++ {
		if packet.Data[i].Delta < data.Delta {
			data = &packet.Data[i]
		}
	}

	// First and last rows don't contain data
	if len(data.Rows) <= 2 {
		log.Println("Error: Packet only contains summary.")
		return NoData
	}

	// Get data set start time
	log.Println("Reading start time:", data.Timestamp)
	startUnixTime, err := strconv.ParseInt(data.Timestamp, 0, 64)
	if err != nil || startUnixTime <= 0 {
		log.Println()
		if err != nil {
			log.Println("Error reading start time:", err)
		} else {
			log.Println("Invalid start time:", startUnixTime)
		}
		return
	}
	startTime := time.Unix(startUnixTime, 0)

	log.Println("Columns:", *columns)
	interp_time := startTime // Set timestamp for first data point to time in packet

	// Prepare bulk insert statement
	stmt, err := txn.Prepare(pq.CopyIn("data_raw", "serial", "type", "device", "data", "time"))
	if err != nil {
		log.Println(err)
		goto closetrans
	}

	// Skip first and last rows because it is not a data point
	for _, row := range data.Rows[1 : len(data.Rows)-1] {
		if constants.Verbose {
			log.Println("Row:", row.Columns)
			log.Println("Time:", interp_time)
		}

		if len(row.Columns) != len(*columns) {
			log.Println("Error: Invalid row.")
			continue
		}

		for i := 0; i < len(*columns); i++ {
			if constants.Verbose {
				log.Printf(
					"%d, %s, %s, %d, %v\n",
					serial,
					(*columns)[i].Type,
					(*columns)[i].Name,
					row.Columns[i],
					interp_time,
				)
			}
			_, err = stmt.Exec(
				serial,
				(*columns)[i].Type,
				(*columns)[i].Name,
				row.Columns[i],
				interp_time,
			) // Insert data. This is buffered.
			if err != nil {
				log.Println(err)
				break
			}
		}
		interp_time = interp_time.Add(time.Duration(data.Delta) * time.Second) // Add data point time spacing for next data point
	}

closetrans:
	log.Println("Closing off transaction...")

	// Flush buffer
	_, ferr := stmt.Exec()
	if ferr != nil {
		log.Fatal(ferr)
	}

	// Close prepared statement
	ferr = stmt.Close()
	if ferr != nil {
		log.Fatal(ferr)
	}

	// Commit transaction
	ferr = txn.Commit()
	if ferr != nil {
		log.Fatal(ferr)
	}

	log.Println("Transaction closed")

	return err
}
<commit_msg>Based on experimentation, we devide time delta by 2 and keep last row.<commit_after>/*
 * Package handles connecting and writing to the database.
 */
package database

import (
	"database/sql"
	"errors"
	"fmt"
	"log"
	"strconv"
	"time"

	"github.com/lib/pq"

	"github.com/seadsystem/Backend/DB/landingzone/constants"
	"github.com/seadsystem/Backend/DB/landingzone/decoders/eGaugeDecoders"
	"github.com/seadsystem/Backend/DB/landingzone/decoders/seadPlugDecoders"
)

var NoData error = errors.New("No data in packet.")
var InvalidTime error = errors.New("Invalid time.")

type DB struct {
	conn *sql.DB
}

func New() (DB, error) {
	conn, err := sql.Open("postgres", fmt.Sprintf("host=%s user=%s dbname=%s password=%s port=%d sslmode=disable", constants.DB_SOCKET, constants.DB_USER, constants.DB_NAME, constants.DB_PASSWORD, constants.DB_PORT))
	return DB{conn}, err
}

func (db DB) SetMaxOpenConns(n int) {
	db.conn.SetMaxOpenConns(n)
}

func (db DB) InsertSeadPacket(data seadPlugDecoders.SeadPacket) {
	log.Println("Beginning transaction...")
	// Begin transaction. Required for bulk insert
	txn, err := db.conn.Begin()
	if err != nil {
		log.Println(err)
		return
	}

	// Packet wide declerations
	data_type := string(data.Type)
	interp_time := data.Timestamp // Set timestamp for first data point to time in packet

	// Prepare bulk insert statement
	stmt, err := txn.Prepare(pq.CopyIn("data_raw", "serial", "type", "data", "time"))
	if err != nil {
		log.Println(err)
		goto closetrans
	}

	log.Println("Processing data...")

	// Process data
	for _, element := range data.Data {
		if constants.Verbose {
			log.Println("Data:", element)
			log.Println("Time:", interp_time)
		}

		_, err = stmt.Exec(data.Serial, data_type, element, interp_time) // Insert data. This is buffered.
		interp_time = interp_time.Add(data.Period)                       // Add data point time spacing for next data point
		if err != nil {
			log.Println(err)
			break
		}
	}

closetrans:
	log.Println("Closing off transaction...")

	// Flush buffer
	_, err = stmt.Exec()
	if err != nil {
		log.Fatal(err)
	}

	// Close prepared statement
	err = stmt.Close()
	if err != nil {
		log.Fatal(err)
	}

	// Commit transaction
	err = txn.Commit()
	if err != nil {
		log.Fatal(err)
	}

	log.Println("Transaction closed")
}

func (db DB) InsertEGaugePacket(packet eGaugeDecoders.Packet) (err error) {
	log.Println("Peliminary data processing...")

	// Process data packet
	log.Println("Reading serial:", packet.Serial)
	serial, err := strconv.ParseInt(packet.Serial, 0, 64)
	if err != nil || serial <= 0 {
		log.Println()
		if err != nil {
			log.Println("Error reading serial:", err)
		} else {
			log.Println("Invalid serial:", serial)
		}
		return
	}

	log.Println("Beginning transaction...")
	// Begin transaction. Required for bulk insert
	txn, err := db.conn.Begin()
	if err != nil {
		log.Println(err)
		return
	}

	// Select best data set
	if len(packet.Data) == 0 {
		log.Println("Error: No data in packet")
		return NoData
	}

	data := &packet.Data[0]
	columns := &data.Cnames
	for i := 1; i < len(packet.Data); i++ {
		if packet.Data[i].Delta < data.Delta {
			data = &packet.Data[i]
		}
	}

	// First and last rows don't contain data
	if len(data.Rows) <= 2 {
		log.Println("Error: Packet only contains summary.")
		return NoData
	}

	// Get data set start time
	log.Println("Reading start time:", data.Timestamp)
	startUnixTime, err := strconv.ParseInt(data.Timestamp, 0, 64)
	if err != nil || startUnixTime <= 0 {
		log.Println()
		if err != nil {
			log.Println("Error reading start time:", err)
		} else {
			log.Println("Invalid start time:", startUnixTime)
		}
		return
	}
	startTime := time.Unix(startUnixTime, 0)

	log.Println("Columns:", *columns)
	interp_time := startTime // Set timestamp for first data point to time in packet

	// Prepare bulk insert statement
	stmt, err := txn.Prepare(pq.CopyIn("data_raw", "serial", "type", "device", "data", "time"))
	if err != nil {
		log.Println(err)
		goto closetrans
	}

	// Skip first row because it is not a data point
	for _, row := range data.Rows[1:len(data.Rows)] {
		if constants.Verbose {
			log.Println("Row:", row.Columns)
			log.Println("Time:", interp_time)
		}

		if len(row.Columns) != len(*columns) {
			log.Println("Error: Invalid row.")
			continue
		}

		for i := 0; i < len(*columns); i++ {
			if constants.Verbose {
				log.Printf(
					"%d, %s, %s, %d, %v\n",
					serial,
					(*columns)[i].Type,
					(*columns)[i].Name,
					row.Columns[i],
					interp_time,
				)
			}
			_, err = stmt.Exec(
				serial,
				(*columns)[i].Type,
				(*columns)[i].Name,
				row.Columns[i],
				interp_time,
			) // Insert data. This is buffered.
			if err != nil {
				log.Println(err)
				break
			}
		}
		interp_time = interp_time.Add(time.Duration(data.Delta) / 2 * time.Second) // Add data point time spacing for next data point
	}

closetrans:
	log.Println("Closing off transaction...")

	// Flush buffer
	_, ferr := stmt.Exec()
	if ferr != nil {
		log.Fatal(ferr)
	}

	// Close prepared statement
	ferr = stmt.Close()
	if ferr != nil {
		log.Fatal(ferr)
	}

	// Commit transaction
	ferr = txn.Commit()
	if ferr != nil {
		log.Fatal(ferr)
	}

	log.Println("Transaction closed")

	return err
}
<|endoftext|>


<commit_before>package handlers

import (
	"log"
	"net"
	"io"

	"github.com/seadsystem/Backend/DB/landingzone/constants"
)

// Handle a client's request
func HandleRequest(conn net.Conn) {
	log.Println("Got a connection.")

	var buffer []byte
	tempbuf := make([]byte, constants.INPUT_BUFFER_SIZE)

	for {
		log.Println("Reading bytes...")
		n, err := conn.Read(tempbuf)
		if err != nil {
			if err != io.EOF {
				log.Println("Read error:", err)
			}
			break
		}
		buffer = append(buffer, tempbuf[:n]...)

	}

	if len(buffer) < 1 {
		log.Println("Error: received empty request")
	} else {
		log.Println("Received data:")
		log.Println(string(buffer))
	}

	conn.Write([]byte("Response"))
	conn.Close()
}
<commit_msg>Added more logging.<commit_after>package handlers

import (
	"log"
	"net"
	"io"

	"github.com/seadsystem/Backend/DB/landingzone/constants"
)

// Handle a client's request
func HandleRequest(conn net.Conn) {
	log.Println("Got a connection.")

	var buffer []byte
	tempbuf := make([]byte, constants.INPUT_BUFFER_SIZE)

	for {
		log.Println("Reading bytes....")
		n, err := conn.Read(tempbuf)
		log.Println("Read some bytes...")
		if err != nil {
			if err != io.EOF {
				log.Println("Read error:", err)
			} else {
				log.Println("Done reading bytes.")
			}
			break
		}
		buffer = append(buffer, tempbuf[:n]...)

	}

	if len(buffer) < 1 {
		log.Println("Error: received empty request")
	} else {
		log.Println("Received data:")
		log.Println(string(buffer))
	}

	conn.Write([]byte("Response"))
	conn.Close()
}
<|endoftext|>


<commit_before>package cielo_test

import (
	"log"
	"os"
	"testing"

	"github.com/reginaldosousa/go-cielo"
	"github.com/stretchr/testify/assert"
)

var (
	merchantID, merchantKey string
)

func init() {
	merchantID = os.Getenv("CIELO_MERCHANT_ID")
	merchantKey = os.Getenv("CIELO_MERCHANT_KEY")
	if merchantID == "" || merchantKey == "" {
		log.Fatal("You must provide CIELO_MERCHANT_ID and CIELO_MERCHANT_KEY environment variables to run tests")
	}
}

func Test_CieloEcommerce_CreateSale(t *testing.T) {
	m := cielo.Merchant{
		ID:  merchantID,
		Key: merchantKey,
	}
	c := cielo.NewEcommerce(m, cielo.SandboxEnvironment)
	sale := cielo.Sale{
		Payment: &cielo.Payment{
			Type:         cielo.CreditCardPayment,
			Provider:     cielo.SimuladoProvider,
			Amount:       15700,
			Interest:     "ByMerchant",
			Installments: 1,
			CreditCard: &cielo.CreditCard{
				CardNumber:     "1234123412341231",
				Holder:         "John Bla",
				ExpirationDate: "12/2020",
				SecurityCode:   "123",
				Brand:          "Visa",
			},
			Country: "BRA",
		},
		MerchantOrderID: "123A",
		Customer: &cielo.Customer{
			Name: "John",
		},
	}
	s, err := c.CreateSale(sale)
	assert.NoError(t, err)
	assert.NotNil(t, s.Payment.PaymentID)
}
<commit_msg>Reorganize test code<commit_after>package cielo_test

import (
	"log"
	"os"
	"testing"

	"github.com/reginaldosousa/go-cielo"
	"github.com/stretchr/testify/assert"
)

var (
	merchantID, merchantKey string
	merchant                cielo.Merchant
	ecommerce               cielo.Ecommerce
)

func init() {
	merchantID = os.Getenv("CIELO_MERCHANT_ID")
	merchantKey = os.Getenv("CIELO_MERCHANT_KEY")
	if merchantID == "" || merchantKey == "" {
		log.Fatal("You must provide CIELO_MERCHANT_ID and CIELO_MERCHANT_KEY environment variables to run tests")
	}
	merchant = cielo.Merchant{
		ID:  merchantID,
		Key: merchantKey,
	}
	ecommerce = cielo.NewEcommerce(merchant, cielo.SandboxEnvironment)
}

func Test_CieloEcommerce_CreateSale(t *testing.T) {
	testCases := []struct {
		name    string
		getSale func() cielo.Sale
	}{
		{
			name: "basic sale with success",
			getSale: func() cielo.Sale {
				payment := newPayment(15700, newCreditCard("1234123412341231", "12/2020", "123"))
				customer := newCustomer("John")
				sale := cielo.Sale{
					Payment:         &payment,
					MerchantOrderID: "123A",
					Customer:        &customer,
				}
				return sale
			},
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			s, err := ecommerce.CreateSale(tc.getSale())
			assert.NoError(t, err)
			assert.NotNil(t, s.Payment.PaymentID)
		})
	}
}

func newPayment(amount uint32, creditCard cielo.CreditCard) cielo.Payment {
	return cielo.Payment{
		Type:         cielo.CreditCardPayment,
		Provider:     cielo.SimuladoProvider,
		Amount:       amount,
		Interest:     "ByMerchant",
		Installments: 1,
		CreditCard:   &creditCard,
		Country:      "BRA",
	}
}

func newCreditCard(number, expiration, securityCode string) cielo.CreditCard {
	return cielo.CreditCard{
		CardNumber:     number,
		Holder:         "John Bla",
		ExpirationDate: expiration,
		SecurityCode:   securityCode,
		Brand:          "Visa",
	}
}

func newCustomer(name string) cielo.Customer {
	return cielo.Customer{
		Name: name,
	}
}
<|endoftext|>


<commit_before>// Package bitutils provides a collection of utilities to deal with bits.
package bitutils

import (
	"fmt"
	"strconv"
)

// W is the length of a machine word.
const W = 64

// Magic constants.
const (
	Lsh2 = 0x5555555555555555
	Lsh4 = 0x3333333333333333
	Lsh8 = 0x0f0f0f0f0f0f0f0f

	Msh2 = 0xaaaaaaaaaaaaaaaa
	Msh4 = 0xcccccccccccccccc
	Msh8 = 0xf0f0f0f0f0f0f0f0

	Lsb2 = 0x5555555555555555
	Lsb4 = 0x1111111111111111
	Lsb8 = 0x0101010101010101

	Msb2 = 0xaaaaaaaaaaaaaaaa
	Msb4 = 0x8888888888888888
	Msb8 = 0x8080808080808080
)

// Word represents a 64-bit binary string.
type Word uint64

var (
	Pos  [W + 1]Word // Pos[i] has a 1 only at i.
	PosC [W + 1]Word // PosC[i] has a 0 only at i.
	Lsh  [W + 1]Word // Lsh[i] has 1s in its i LSBs.
	Msh  [W + 1]Word // Msh[i] has 1s in its i MSBs.
)

func init() {
	for i := 0; i < len(Pos); i++ {
		Pos[i] = Word(1) << uint(i)
		PosC[i] = ^Pos[i]
		Lsh[i] = Pos[i] - 1
		Msh[i] = Lsh[i] << uint(W-i)
	}
}

// ParseWord returns a Word from a string.
func ParseWord(s string) (Word, error) {
	w, err := strconv.ParseUint(s, 2, 64)
	return Word(w), err
}

// String returns binary string w[0]w[1]...w[63].
func (w Word) String() string {
	return fmt.Sprintf("%064b", w)
}

// Count1 returns the number of ones contained in w.
func (w Word) Count1() int {
	w -= (w >> 1) & Lsh2
	w = (w & Lsh4) + ((w >> 2) & Lsh4)
	w = (w + (w >> 4)) & Lsh8
	return int((w * Lsb8) >> 56)
}

// Count0 returns the number of zeros contained in w.
func (w Word) Count0() int {
	w = ^w
	return w.Count1()
}

// Count returns the number of b[0]'s contained in w.
func (w Word) Count(b int) int {
	w = w ^ (^Word(0) + Word(b))
	return w.Count1()
}

// Get returns w[i].
func (w Word) Get(i int) Word {
	w = w >> uint(i)
	return w & Pos[0]
}

// Set1 sets w[i] to 1.
func (w Word) Set1(i int) Word {
	return w | Pos[i]
}

// Set0 sets w[i] to 0.
func (w Word) Set0(i int) Word {
	return w & PosC[i]
}

// Flip flips w[i].
func (w Word) Flip(i int) Word {
	return w ^ Pos[i]
}

// Least1 returns a word that indicates the least 1 in w.
func (w Word) Least1() Word {
	if w == 0 {
		return 0
	}
	w = ((w - 1) ^ w) & w
	return w
}

// LeastIndex1 returns the index of the least 1 in w if exists and -1
// otherwise.
func (w Word) LeastIndex1() int {
	if w == 0 {
		return -1
	}
	w = (w - 1) ^ w
	return w.Count1() - 1
}

// Rank1 returns the number of ones in w[0]...w[i].
func (w Word) Rank1(i int) int {
	w = w << uint(W-i-1)
	return w.Count1()
}

// Rank0 returns the number of zeros in w[0]...w[i].
func (w Word) Rank0(i int) int {
	w = ^w << uint(W-i-1)
	return w.Count1()
}

func (w Word) zcmp8() Word {
	w = w | ((w | Msb8) - Lsb8)
	return (w & Msb8) >> 7
}

func (w Word) leq8(v Word) Word {
	w = (((v | Msb8) - (w & ^Word(Lsb8))) ^ w) ^ v
	return (w & Msb8) >> 7
}

// Select1 returns the ith 1 in w.
func (w Word) Select1(i int) int {
	s := w - ((w & Msb2) >> 1)
	s = (s & Lsh4) + ((s >> 2) & Lsh4)
	s = ((s + (s >> 4)) & Lsh8) * Lsb8
	b := ((s.leq8(Word(i)*Lsb8) * Lsb8) >> 53) & ^Word(0x0111)
	l := Word(i) - (((s << 8) >> b) & 0xff)
	s = ((((w >> b) & 0xff) * Lsb8) & 0x8040201008040201).zcmp8() * Lsb8
	if w = b + ((s.leq8(l * Lsb8) * Lsb8) >> 56); w != 0x48 {
		return int(w)
	} else {
		return -1
	}
}

// Select0 returns the ith 0 in w.
func (w Word) Select0(i int) int {
	w = ^w
	return w.Select1(i)
}
<commit_msg>Fix a format<commit_after>// Package bitutils provides a collection of utilities to deal with bits.
package bitutils

import (
	"fmt"
	"strconv"
)

// W is the length of a machine word.
const W = 64

// Magic constants.
const (
	Lsh2 = 0x5555555555555555
	Lsh4 = 0x3333333333333333
	Lsh8 = 0x0f0f0f0f0f0f0f0f

	Msh2 = 0xaaaaaaaaaaaaaaaa
	Msh4 = 0xcccccccccccccccc
	Msh8 = 0xf0f0f0f0f0f0f0f0

	Lsb2 = 0x5555555555555555
	Lsb4 = 0x1111111111111111
	Lsb8 = 0x0101010101010101

	Msb2 = 0xaaaaaaaaaaaaaaaa
	Msb4 = 0x8888888888888888
	Msb8 = 0x8080808080808080
)

// Word represents a 64-bit binary string.
type Word uint64

var (
	Pos  [W + 1]Word // Pos[i] has a 1 only at i.
	PosC [W + 1]Word // PosC[i] has a 0 only at i.
	Lsh  [W + 1]Word // Lsh[i] has 1s in its i LSBs.
	Msh  [W + 1]Word // Msh[i] has 1s in its i MSBs.
)

func init() {
	for i := 0; i < len(Pos); i++ {
		Pos[i] = Word(1) << uint(i)
		PosC[i] = ^Pos[i]
		Lsh[i] = Pos[i] - 1
		Msh[i] = Lsh[i] << uint(W-i)
	}
}

// ParseWord returns a Word from a string.
func ParseWord(s string) (Word, error) {
	w, err := strconv.ParseUint(s, 2, 64)
	return Word(w), err
}

// String returns binary string w[0]w[1]...w[63].
func (w Word) String() string {
	return fmt.Sprintf("%064b", w)
}

// Count1 returns the number of ones contained in w.
func (w Word) Count1() int {
	w -= (w >> 1) & Lsh2
	w = (w & Lsh4) + ((w >> 2) & Lsh4)
	w = (w + (w >> 4)) & Lsh8
	return int((w * Lsb8) >> 56)
}

// Count0 returns the number of zeros contained in w.
func (w Word) Count0() int {
	w = ^w
	return w.Count1()
}

// Count returns the number of b[0]'s contained in w.
func (w Word) Count(b int) int {
	w = w ^ (^Word(0) + Word(b))
	return w.Count1()
}

// Get returns w[i].
func (w Word) Get(i int) Word {
	w = w >> uint(i)
	return w & Pos[0]
}

// Set1 sets w[i] to 1.
func (w Word) Set1(i int) Word {
	return w | Pos[i]
}

// Set0 sets w[i] to 0.
func (w Word) Set0(i int) Word {
	return w & PosC[i]
}

// Flip flips w[i].
func (w Word) Flip(i int) Word {
	return w ^ Pos[i]
}

// Least1 returns a word that indicates the least 1 in w.
func (w Word) Least1() Word {
	if w == 0 {
		return 0
	}
	w = ((w - 1) ^ w) & w
	return w
}

// LeastIndex1 returns the index of the least 1 in w if exists and -1
// otherwise.
func (w Word) LeastIndex1() int {
	if w == 0 {
		return -1
	}
	w = (w - 1) ^ w
	return w.Count1() - 1
}

// Rank1 returns the number of ones in w[0]...w[i].
func (w Word) Rank1(i int) int {
	w = w << uint(W-i-1)
	return w.Count1()
}

// Rank0 returns the number of zeros in w[0]...w[i].
func (w Word) Rank0(i int) int {
	w = ^w << uint(W-i-1)
	return w.Count1()
}

func (w Word) zcmp8() Word {
	w = w | ((w | Msb8) - Lsb8)
	return (w & Msb8) >> 7
}

func (w Word) leq8(v Word) Word {
	w = (((v | Msb8) - (w & ^Word(Lsb8))) ^ w) ^ v
	return (w & Msb8) >> 7
}

// Select1 returns the ith 1 in w.
func (w Word) Select1(i int) int {
	s := w - ((w & Msb2) >> 1)
	s = (s & Lsh4) + ((s >> 2) & Lsh4)
	s = ((s + (s >> 4)) & Lsh8) * Lsb8
	b := ((s.leq8(Word(i)*Lsb8) * Lsb8) >> 53) & ^Word(0x0111)
	l := Word(i) - (((s << 8) >> b) & 0xff)
	s = ((((w >> b) & 0xff) * Lsb8) & 0x8040201008040201).zcmp8() * Lsb8
	if w = b + ((s.leq8(l*Lsb8) * Lsb8) >> 56); w != 0x48 {
		return int(w)
	} else {
		return -1
	}
}

// Select0 returns the ith 0 in w.
func (w Word) Select0(i int) int {
	w = ^w
	return w.Select1(i)
}
<|endoftext|>


<commit_before>// Package createzip is a helper to create a new zip file and add files into the zip file.
package createzip

import (
	"archive/zip"
	"bufio"
	"fmt"
	"io"
	"net/http"
	"os"
	"path/filepath"
)

// ZipFile is wrapper of zip.Writer.
type ZipFile struct {
	w *zip.Writer
}

var (
	// DEBUG is debug mode.
	// Set to true to enable debug messages from this package.
	DEBUG bool = false
)

// New creates a new zip file. It should call Close() after use.
func New(w io.Writer) (zf *ZipFile) {
	return &ZipFile{zip.NewWriter(w)}
}

// NewForHTTP creates the downloadable zip for HTTP server dynamically.
func NewForHTTP(w http.ResponseWriter, zipFileName string) (zf *ZipFile) {
	w.Header().Set("Content-Type", "application/zip")
	w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", zipFileName))

	return New(w)
}

// Close closes the writer of zip. It should call Close() after use.
func (zf *ZipFile) Close() {
	zf.w.Close()
}

// Add creates a new file in the zip and copy the content from io.Reader.
//
//   Params:
//       fileNameInZip: file name(path) in the zip.
//       r: io.Reader. It will copy the io.Reader to the new create file's writer.
func (zf *ZipFile) Add(fileNameInZip string, r io.Reader) (err error) {
	w, err := zf.w.Create(fileNameInZip)
	if err != nil {
		if DEBUG {
			fmt.Printf("zw.Create(%v) err: %v\n", fileNameInZip, err)
		}
		return err
	}

	if _, err = io.Copy(w, r); err != nil {
		if DEBUG {
			fmt.Printf("io.Copy(w, r) err: %v\n", err)
		}
		return err
	}

	return nil
}

// AddFile creates a new file in the zip and copy the content from the original source file.
//
//   Params:
//       srcFilePath: original file path.
//       fileNameInZip: file name(path) in the zip. If it's empty(""), the file name will be root dir of the zip("./") + file name without dir of original source file.
func (zf *ZipFile) AddFile(srcFilePath, fileNameInZip string) (err error) {
	fi, err := os.Open(srcFilePath)
	if err != nil {
		if DEBUG {
			fmt.Printf("os.Open(%v) err: %v\n", srcFilePath, err)
		}
		return err
	}
	defer fi.Close()

	// If file name in zip is "", use the file name without dir.
	if fileNameInZip == "" {
		_, fileNameInZip = filepath.Split(srcFilePath)
	}

	r := bufio.NewReader(fi)

	return zf.Add(fileNameInZip, r)
}
<commit_msg>Remove type in 'var xx type = xx' to remove golint warnings.<commit_after>// Package createzip is a helper to create a new zip file and add files into the zip file.
package createzip

import (
	"archive/zip"
	"bufio"
	"fmt"
	"io"
	"net/http"
	"os"
	"path/filepath"
)

// ZipFile is wrapper of zip.Writer.
type ZipFile struct {
	w *zip.Writer
}

var (
	// DEBUG is debug mode.
	// Set to true to enable debug messages from this package.
	DEBUG = false
)

// New creates a new zip file. It should call Close() after use.
func New(w io.Writer) (zf *ZipFile) {
	return &ZipFile{zip.NewWriter(w)}
}

// NewForHTTP creates the downloadable zip for HTTP server dynamically.
func NewForHTTP(w http.ResponseWriter, zipFileName string) (zf *ZipFile) {
	w.Header().Set("Content-Type", "application/zip")
	w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", zipFileName))

	return New(w)
}

// Close closes the writer of zip. It should call Close() after use.
func (zf *ZipFile) Close() {
	zf.w.Close()
}

// Add creates a new file in the zip and copy the content from io.Reader.
//
//   Params:
//       fileNameInZip: file name(path) in the zip.
//       r: io.Reader. It will copy the io.Reader to the new create file's writer.
func (zf *ZipFile) Add(fileNameInZip string, r io.Reader) (err error) {
	w, err := zf.w.Create(fileNameInZip)
	if err != nil {
		if DEBUG {
			fmt.Printf("zw.Create(%v) err: %v\n", fileNameInZip, err)
		}
		return err
	}

	if _, err = io.Copy(w, r); err != nil {
		if DEBUG {
			fmt.Printf("io.Copy(w, r) err: %v\n", err)
		}
		return err
	}

	return nil
}

// AddFile creates a new file in the zip and copy the content from the original source file.
//
//   Params:
//       srcFilePath: original file path.
//       fileNameInZip: file name(path) in the zip. If it's empty(""), the file name will be root dir of the zip("./") + file name without dir of original source file.
func (zf *ZipFile) AddFile(srcFilePath, fileNameInZip string) (err error) {
	fi, err := os.Open(srcFilePath)
	if err != nil {
		if DEBUG {
			fmt.Printf("os.Open(%v) err: %v\n", srcFilePath, err)
		}
		return err
	}
	defer fi.Close()

	// If file name in zip is "", use the file name without dir.
	if fileNameInZip == "" {
		_, fileNameInZip = filepath.Split(srcFilePath)
	}

	r := bufio.NewReader(fi)

	return zf.Add(fileNameInZip, r)
}
<|endoftext|>


<commit_before>// Package main implements a test client that starts a scan, wait until it finishes and exports its results to a csv file.
package main

import (
	"flag"
	"github.com/attwad/nessie"
	"io/ioutil"
	"log"
	"strings"
	"time"
)

var apiURL, username, password string

func init() {
	flag.StringVar(&apiURL, "api_url", "", "")
	flag.StringVar(&username, "username", "", "Username to login with, in production read that from a file, do not set from the command line or it will end up in your history.")
	flag.StringVar(&password, "password", "", "Password that matches the provided username, in production read that from a file, do not set from the command line or it will end up in your history.")
	flag.Parse()
}

func main() {
	nessus, err := nessie.NewInsecureNessus(apiURL)
	if err != nil {
		panic(err)
	}

	if err := nessus.Login(username, password); err != nil {
		log.Println(err)
		return
	}
	log.Println("Logged-in")
	defer nessus.Logout()

	c, err := nessus.AllPlugins()
	if err != nil {
		panic(err)
	}
	i := 0
	for p := range c {
		i++
		log.Println(i, p.ID)
	}
	return

	var scanID int64 = 13
	// We only care about the last scan, so no use for the scan UUID here.
	if _, err = nessus.StartScan(scanID); err != nil {
		panic(err)
	}
	for {
		details, err := nessus.ScanDetails(scanID)
		if err != nil {
			panic(err)
		}
		if strings.ToLower(details.Info.Status) == "completed" {
			log.Println("Scan completed")
			break
		}
		log.Println("Scan is", details.Info.Status)
		time.Sleep(5 * time.Second)
	}

	exportID, err := nessus.ExportScan(scanID, nessie.ExportCSV)
	if err != nil {
		panic(err)
	}
	for {
		if finished, err := nessus.ExportFinished(scanID, exportID); err != nil {
			panic(err)
		} else if finished {
			log.Println("Scan export finished")
			break
		}
		log.Println("Scan export ongoing...")
		time.Sleep(5 * time.Second)
	}
	csv, err := nessus.DownloadExport(scanID, exportID)
	if err != nil {
		panic(err)
	}
	if err := ioutil.WriteFile("report.csv", csv, 0600); err != nil {
		panic(err)
	}
}
<commit_msg>remove debug cli example<commit_after>// Package main implements a test client that starts a scan, wait until it finishes and exports its results to a csv file.
package main

import (
	"flag"
	"github.com/attwad/nessie"
	"io/ioutil"
	"log"
	"strings"
	"time"
)

var apiURL, username, password string

func init() {
	flag.StringVar(&apiURL, "api_url", "", "")
	flag.StringVar(&username, "username", "", "Username to login with, in production read that from a file, do not set from the command line or it will end up in your history.")
	flag.StringVar(&password, "password", "", "Password that matches the provided username, in production read that from a file, do not set from the command line or it will end up in your history.")
	flag.Parse()
}

func main() {
	nessus, err := nessie.NewInsecureNessus(apiURL)
	if err != nil {
		panic(err)
	}

	if err := nessus.Login(username, password); err != nil {
		log.Println(err)
		return
	}
	log.Println("Logged-in")
	defer nessus.Logout()

	var scanID int64 = 13
	// We only care about the last scan, so no use for the scan UUID here.
	if _, err = nessus.StartScan(scanID); err != nil {
		panic(err)
	}
	for {
		details, err := nessus.ScanDetails(scanID)
		if err != nil {
			panic(err)
		}
		if strings.ToLower(details.Info.Status) == "completed" {
			log.Println("Scan completed")
			break
		}
		log.Println("Scan is", details.Info.Status)
		time.Sleep(5 * time.Second)
	}

	exportID, err := nessus.ExportScan(scanID, nessie.ExportCSV)
	if err != nil {
		panic(err)
	}
	for {
		if finished, err := nessus.ExportFinished(scanID, exportID); err != nil {
			panic(err)
		} else if finished {
			log.Println("Scan export finished")
			break
		}
		log.Println("Scan export ongoing...")
		time.Sleep(5 * time.Second)
	}
	csv, err := nessus.DownloadExport(scanID, exportID)
	if err != nil {
		panic(err)
	}
	if err := ioutil.WriteFile("report.csv", csv, 0600); err != nil {
		panic(err)
	}
}
<|endoftext|>