<commit_before>package diskutil

import (
	"encoding/json"
	"errors"
	"fmt"
	"os"
	"os/exec"
	"path"
	"strings"
)

const (
	keyExitResult               string = "Exit Code:"
	keyVdVirtualDrive           string = "Virtual Drive"
	keyVdTargetId               string = "Target Id"
	keyVdName                   string = "Name"
	keyVdSize                   string = "Size"
	keyVdState                  string = "State"
	keyVdNumberOfDrives         string = "Number Of Drives"
	keyVdEncryptiontype         string = "Encryption type"
	keyPdEnclosureDeviceId      string = "Enclosure Device ID"
	keyPdSlotNumber             string = "Slot Number"
	keyPdDeviceId               string = "Device Id"
	keyPdMediaErrorCount        string = "Media Error Count"
	keyPdOtherErrorCount        string = "Other Error Count"
	keyPdPredictiveFailureCount string = "Predictive Failure Count"
	keyPdPdtype                 string = "PD type"
	keyPdRawSize                string = "Raw Size"
	keyPdFirmwareState          string = "Firmware state"
	keyPdInquiryData            string = "Inquiry Data"
	keyPdDriveTemperature       string = "Drive Temperature"

	typeString int = iota
	typeInt
	typeUint64
)

// DiskStatus is a struct to get all Adapters' Stat of the server
type DiskStatus struct {
	megacliPath  string
	adapterCount int
	AdapterStats []AdapterStat `json:"adapter_stats"`
}

// String() is used to get the print string.
func (d *DiskStatus) String() string {
	data, err := json.Marshal(d)
	if err != nil {
		return err.Error()
	}
	return string(data)
}

// ToJson() is used to get the json encoded string.
func (d *DiskStatus) ToJson() (string, error) {
	data, err := json.Marshal(d)
	if err != nil {
		return "", err
	}
	return string(data), nil
}

func fileExist(filename string) bool {
	_, err := os.Stat(filename)
	return err == nil || os.IsExist(err)
}

// NewDiskStatus() use the megaCliPath and apapterCount to build a DiskStatus.
func NewDiskStatus(megaCliPath string, adapterCount int) (*DiskStatus, error) {
	megaCliPath = path.Clean(megaCliPath)
	if !fileExist(megaCliPath) {
		return nil, errors.New("megaCli not exist")
	}
	ds := new(DiskStatus)
	ds.megacliPath = megaCliPath
	ds.adapterCount = adapterCount
	return ds, nil
}

func execCmd(command, args string) (string, error) {
	// fmt.Println("Command: ", command)
	// fmt.Println("Arguments: ", args)
	var argArray []string
	if args != "" {
		argArray = strings.Split(args, " ")
	} else {
		argArray = make([]string, 0)
	}

	cmd := exec.Command(command, argArray...)
	buf, err := cmd.Output()
	if err != nil {
		fmt.Fprintf(os.Stderr, "The command failed to perform: %s (Command: %s, Arguments: %s)", err, command, args)
		return "", err
	}

	// fmt.Fprintf(os.Stdout, "Result: %s", buf)
	return string(buf), nil
}

// Get() is used to get all the stat of a DiskStatus.
func (d *DiskStatus) Get() error {
	ads := make([]AdapterStat, 0)

	command := d.megacliPath
	for i := 0; i < d.adapterCount; i++ {
		ad := AdapterStat{
			AdapterId: i,
		}
		err := ad.getMegaRaidVdInfo(command)
		if err != nil {
			d.AdapterStats = nil
			return err
		}
		err = ad.getMegaRaidPdInfo(command)
		if err != nil {
			d.AdapterStats = nil
			return err
		}
		ads = append(ads, ad)
	}

	d.AdapterStats = ads
	return nil
}

// GetVirtualDrive() is used to get the VirtualDriveStat of a DiskStatus.
func (d *DiskStatus) GetVirtualDrive() error {
	ads := make([]AdapterStat, 0)

	command := d.megacliPath
	for i := 0; i < d.adapterCount; i++ {
		ad := AdapterStat{
			AdapterId: i,
		}
		err := ad.getMegaRaidVdInfo(command)
		if err != nil {
			d.AdapterStats = nil
			return err
		}
		ads = append(ads, ad)
	}

	d.AdapterStats = ads
	return nil
}

// GetPhysicalDrive() is used to get the PhysicalDriveStat of a DiskStatus.
func (d *DiskStatus) GetPhysicalDrive() error {
	ads := make([]AdapterStat, 0)

	command := d.megacliPath
	for i := 0; i < d.adapterCount; i++ {
		ad := AdapterStat{
			AdapterId: i,
		}
		err := ad.getMegaRaidPdInfo(command)
		if err != nil {
			d.AdapterStats = nil
			return err
		}
		ads = append(ads, ad)
	}

	d.AdapterStats = ads
	return nil
}

// ListBrokenDrive() is used to list the Broken Drives of a DiskStatus.
func (d *DiskStatus) ListBrokenDrive() ([]VirtualDriveStat, []PhysicalDriveStat, error) {
	brokenVds, err := d.ListBrokenVirtualDrive()
	if err != nil {
		return nil, nil, err
	}

	brokenPds, err := d.ListBrokenPhysicalDrive()
	if err != nil {
		return nil, nil, err
	}

	return brokenVds, brokenPds, nil
}

// ListBrokenVirtualDrive() is used to list the Broken Virtual Drives of a DiskStatus.
func (d *DiskStatus) ListBrokenVirtualDrive() ([]VirtualDriveStat, error) {
	err := d.GetVirtualDrive()
	if err != nil {
		return nil, err
	}

	brokenVds := make([]VirtualDriveStat, 0)
	for _, ads := range d.AdapterStats {
		for _, vds := range ads.VirtualDriveStats {
			if vds.State != "Optimal" {
				brokenVds = append(brokenVds, vds)
			}
		}
	}
	return brokenVds, nil
}

// ListBrokenPhysicalDrive() is used to list the Broken Physical Drives of a DiskStatus.
func (d *DiskStatus) ListBrokenPhysicalDrive() ([]PhysicalDriveStat, error) {
	err := d.GetPhysicalDrive()
	if err != nil {
		return nil, err
	}

	brokenPds := make([]PhysicalDriveStat, 0)
	for _, ads := range d.AdapterStats {
		for _, pds := range ads.PhysicalDriveStats {
			if !strings.Contains(pds.FirmwareState, "Online") {
				brokenPds = append(brokenPds, pds)
			}
		}
	}
	return brokenPds, nil
}
<commit_msg>Remove some unuseful codes #1<commit_after>package diskutil

import (
	"encoding/json"
	"errors"
	"fmt"
	"os"
	"os/exec"
	"path"
	"strings"
)

const (
	keyExitResult               string = "Exit Code:"
	keyVdVirtualDrive           string = "Virtual Drive"
	keyVdTargetId               string = "Target Id"
	keyVdName                   string = "Name"
	keyVdSize                   string = "Size"
	keyVdState                  string = "State"
	keyVdNumberOfDrives         string = "Number Of Drives"
	keyVdEncryptiontype         string = "Encryption type"
	keyPdEnclosureDeviceId      string = "Enclosure Device ID"
	keyPdSlotNumber             string = "Slot Number"
	keyPdDeviceId               string = "Device Id"
	keyPdMediaErrorCount        string = "Media Error Count"
	keyPdOtherErrorCount        string = "Other Error Count"
	keyPdPredictiveFailureCount string = "Predictive Failure Count"
	keyPdPdtype                 string = "PD type"
	keyPdRawSize                string = "Raw Size"
	keyPdFirmwareState          string = "Firmware state"
	keyPdInquiryData            string = "Inquiry Data"
	keyPdDriveTemperature       string = "Drive Temperature"

	typeString int = iota
	typeInt
	typeUint64
)

// DiskStatus is a struct to get all Adapters' Stat of the server
type DiskStatus struct {
	megacliPath  string
	adapterCount int
	AdapterStats []AdapterStat `json:"adapter_stats"`
}

// String() is used to get the print string.
func (d *DiskStatus) String() string {
	data, err := json.Marshal(d)
	if err != nil {
		return err.Error()
	}
	return string(data)
}

// ToJson() is used to get the json encoded string.
func (d *DiskStatus) ToJson() (string, error) {
	data, err := json.Marshal(d)
	if err != nil {
		return "", err
	}
	return string(data), nil
}

func fileExist(filename string) bool {
	_, err := os.Stat(filename)
	return err == nil || os.IsExist(err)
}

// NewDiskStatus() use the megaCliPath and apapterCount to build a DiskStatus.
func NewDiskStatus(megaCliPath string, adapterCount int) (*DiskStatus, error) {
	megaCliPath = path.Clean(megaCliPath)
	if !fileExist(megaCliPath) {
		return nil, errors.New("megaCli not exist")
	}
	ds := new(DiskStatus)
	ds.megacliPath = megaCliPath
	ds.adapterCount = adapterCount
	return ds, nil
}

func execCmd(command, args string) (string, error) {
	var argArray []string
	if args != "" {
		argArray = strings.Split(args, " ")
	} else {
		argArray = make([]string, 0)
	}

	cmd := exec.Command(command, argArray...)
	buf, err := cmd.Output()
	if err != nil {
		fmt.Fprintf(os.Stderr, "The command failed to perform: %s (Command: %s, Arguments: %s)", err, command, args)
		return "", err
	}

	return string(buf), nil
}

// Get() is used to get all the stat of a DiskStatus.
func (d *DiskStatus) Get() error {
	ads := make([]AdapterStat, 0)

	command := d.megacliPath
	for i := 0; i < d.adapterCount; i++ {
		ad := AdapterStat{
			AdapterId: i,
		}
		err := ad.getMegaRaidVdInfo(command)
		if err != nil {
			d.AdapterStats = nil
			return err
		}
		err = ad.getMegaRaidPdInfo(command)
		if err != nil {
			d.AdapterStats = nil
			return err
		}
		ads = append(ads, ad)
	}

	d.AdapterStats = ads
	return nil
}

// GetVirtualDrive() is used to get the VirtualDriveStat of a DiskStatus.
func (d *DiskStatus) GetVirtualDrive() error {
	ads := make([]AdapterStat, 0)

	command := d.megacliPath
	for i := 0; i < d.adapterCount; i++ {
		ad := AdapterStat{
			AdapterId: i,
		}
		err := ad.getMegaRaidVdInfo(command)
		if err != nil {
			d.AdapterStats = nil
			return err
		}
		ads = append(ads, ad)
	}

	d.AdapterStats = ads
	return nil
}

// GetPhysicalDrive() is used to get the PhysicalDriveStat of a DiskStatus.
func (d *DiskStatus) GetPhysicalDrive() error {
	ads := make([]AdapterStat, 0)

	command := d.megacliPath
	for i := 0; i < d.adapterCount; i++ {
		ad := AdapterStat{
			AdapterId: i,
		}
		err := ad.getMegaRaidPdInfo(command)
		if err != nil {
			d.AdapterStats = nil
			return err
		}
		ads = append(ads, ad)
	}

	d.AdapterStats = ads
	return nil
}

// ListBrokenDrive() is used to list the Broken Drives of a DiskStatus.
func (d *DiskStatus) ListBrokenDrive() ([]VirtualDriveStat, []PhysicalDriveStat, error) {
	brokenVds, err := d.ListBrokenVirtualDrive()
	if err != nil {
		return nil, nil, err
	}

	brokenPds, err := d.ListBrokenPhysicalDrive()
	if err != nil {
		return nil, nil, err
	}

	return brokenVds, brokenPds, nil
}

// ListBrokenVirtualDrive() is used to list the Broken Virtual Drives of a DiskStatus.
func (d *DiskStatus) ListBrokenVirtualDrive() ([]VirtualDriveStat, error) {
	err := d.GetVirtualDrive()
	if err != nil {
		return nil, err
	}

	brokenVds := make([]VirtualDriveStat, 0)
	for _, ads := range d.AdapterStats {
		for _, vds := range ads.VirtualDriveStats {
			if vds.State != "Optimal" {
				brokenVds = append(brokenVds, vds)
			}
		}
	}
	return brokenVds, nil
}

// ListBrokenPhysicalDrive() is used to list the Broken Physical Drives of a DiskStatus.
func (d *DiskStatus) ListBrokenPhysicalDrive() ([]PhysicalDriveStat, error) {
	err := d.GetPhysicalDrive()
	if err != nil {
		return nil, err
	}

	brokenPds := make([]PhysicalDriveStat, 0)
	for _, ads := range d.AdapterStats {
		for _, pds := range ads.PhysicalDriveStats {
			if !strings.Contains(pds.FirmwareState, "Online") {
				brokenPds = append(brokenPds, pds)
			}
		}
	}
	return brokenPds, nil
}
<|endoftext|>


<commit_before><commit_msg>rpcclient: Remove existsexpiredtickets.<commit_after><|endoftext|>


<commit_before>package extra

import (
	"bufio"
	"encoding/json"
	"fmt"
	"os"
	"os/exec"
	"os/signal"
	"strconv"
	"time"

	"github.com/pkg/errors"

	"github.com/jmoiron/sqlx"

	"github.com/elpinal/coco3/extra/ast"
	"github.com/elpinal/coco3/extra/parser" // Only for ParseError.
	"github.com/elpinal/coco3/extra/typed"
	"github.com/elpinal/coco3/extra/types"
)

type Env struct {
	cmds map[string]typed.Command
	Option
}

type Option struct {
	DB *sqlx.DB
}

func New(opt Option) Env {
	return Env{
		Option: opt,
		cmds: map[string]typed.Command{
			"exec":    execCommand,
			"cd":      cdCommand,
			"exit":    exitCommand,
			"free":    freeCommand,
			"history": historyCommand,

			"git":   gitCommand,
			"cargo": cargoCommand,
			"go":    goCommand,
			"stack": stackCommand,

			"vim":    vimCommand,
			"emacs":  emacsCommand,
			"screen": screenCommand,
		},
	}
}

func WithoutDefault() Env {
	return Env{cmds: make(map[string]typed.Command)}
}

func (e *Env) Bind(name string, c typed.Command) {
	e.cmds[name] = c
}

func (e *Env) Eval(command *ast.Command) error {
	if command == nil {
		return nil
	}
	tc, found := e.cmds[command.Name.Lit]
	if !found {
		return &parser.ParseError{
			Msg:    fmt.Sprintf("no such typed command: %q", command.Name.Lit),
			Line:   command.Name.Line,
			Column: command.Name.Column,
		}
	}
	if len(command.Args) != len(tc.Params) {
		return &parser.ParseError{
			Msg:    fmt.Sprintf("the length of args (%d) != the one of params (%d)", len(command.Args), len(tc.Params)),
			Line:   command.Name.Line,
			Column: command.Name.Column,
		}
	}
	for i, arg := range command.Args {
		if arg.Type() != tc.Params[i] {
			return &parser.ParseError{
				Msg:    fmt.Sprintf("type mismatch: (%v) (type of %v) does not match with (%v) (expected type)", arg.Type(), arg, tc.Params[i]),
				Line:   command.Name.Line,
				Column: command.Name.Column,
			}
		}
	}

	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)
	defer close(c)
	defer signal.Stop(c)

	return tc.Fn(command.Args, e.DB)
}

func toSlice(list ast.List) ([]string, error) {
	ret := make([]string, 0, list.Length())
	for {
		switch x := list.(type) {
		case *ast.Cons:
			ret = append(ret, x.Head)
			list = x.Tail
		case *ast.Empty:
			return ret, nil
		default:
			return nil, fmt.Errorf("unexpected list type: %T", x)
		}
	}
}

var execCommand = typed.Command{
	Params: []types.Type{types.String, types.StringList},
	Fn: func(args []ast.Expr, _ *sqlx.DB) error {
		cmdArgs, err := toSlice(args[1].(ast.List))
		if err != nil {
			return errors.Wrap(err, "exec")
		}
		cmd := stdCmd(args[0].(*ast.String).Lit, cmdArgs...)
		return cmd.Run()
	},
}

var cdCommand = typed.Command{
	Params: []types.Type{types.String},
	Fn: func(args []ast.Expr, _ *sqlx.DB) error {
		return os.Chdir(args[0].(*ast.String).Lit)
	},
}

var exitCommand = typed.Command{
	Params: []types.Type{types.Int},
	Fn: func(args []ast.Expr, _ *sqlx.DB) error {
		n, err := strconv.Atoi(args[0].(*ast.Int).Lit)
		if err != nil {
			return err
		}
		os.Exit(n)
		return nil
	},
}

var freeCommand = typed.Command{
	Params: []types.Type{types.String, types.StringList},
	Fn: func(args []ast.Expr, _ *sqlx.DB) error {
		cmdArgs, err := toSlice(args[1].(ast.List))
		if err != nil {
			return errors.Wrap(err, "free")
		}
		name := args[0].(*ast.String).Lit
		cmd := exec.Cmd{Path: name, Args: append([]string{name}, cmdArgs...)}
		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr
		cmd.Stdin = os.Stdin
		return cmd.Run()
	},
}

func commandsInCommand(name string) func([]ast.Expr, *sqlx.DB) error {
	return func(args []ast.Expr, _ *sqlx.DB) error {
		cmdArgs, err := toSlice(args[1].(ast.List))
		if err != nil {
			return errors.Wrap(err, name)
		}
		var cmd *exec.Cmd
		switch lit := args[0].(*ast.Ident).Lit; lit {
		case "command":
			cmd = stdCmd(name, cmdArgs...)
		default:
			cmd = stdCmd(name, append([]string{lit}, cmdArgs...)...)
		}
		return cmd.Run()
	}
}

var gitCommand = typed.Command{
	Params: []types.Type{types.Ident, types.StringList},
	Fn:     commandsInCommand("git"),
}

var cargoCommand = typed.Command{
	Params: []types.Type{types.Ident, types.StringList},
	Fn:     commandsInCommand("cargo"),
}

var goCommand = typed.Command{
	Params: []types.Type{types.Ident, types.StringList},
	Fn:     commandsInCommand("go"),
}

var stackCommand = typed.Command{
	Params: []types.Type{types.Ident, types.StringList},
	Fn:     commandsInCommand("stack"),
}

func stdCmd(name string, args ...string) *exec.Cmd {
	cmd := exec.Command(name, args...)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	cmd.Stdin = os.Stdin
	return cmd
}

func stdExec(name string) func([]ast.Expr, *sqlx.DB) error {
	return func(_ []ast.Expr, _ *sqlx.DB) error {
		return stdCmd(name).Run()
	}
}

var vimCommand = typed.Command{
	Params: []types.Type{},
	Fn:     stdExec("vim"),
}

var emacsCommand = typed.Command{
	Params: []types.Type{},
	Fn:     stdExec("emacs"),
}

func withEnv(s string, cmd *exec.Cmd) *exec.Cmd {
	if cmd.Env == nil {
		cmd.Env = os.Environ()
	}
	cmd.Env = append(cmd.Env, s)
	return cmd
}

var screenCommand = typed.Command{
	Params: []types.Type{},
	Fn: func(_ []ast.Expr, _ *sqlx.DB) error {
		return withEnv("LANG=en_US.UTF-8", stdCmd("screen")).Run()
	},
}

type execution struct {
	Time time.Time
	Line string
}

var historyCommand = typed.Command{
	Params: []types.Type{types.String},
	Fn: func(e []ast.Expr, db *sqlx.DB) error {
		var jsonFormat bool
		var enc *json.Encoder
		switch format := e[0].(*ast.String).Lit; format {
		case "json":
			jsonFormat = true
		case "lines":
		default:
			return fmt.Errorf("history: format %q is not supported", format)
		}

		buf := bufio.NewWriter(os.Stdout)
		if jsonFormat {
			enc = json.NewEncoder(buf)
		}
		rows, err := db.Queryx("select * from command_info")
		if err != nil {
			return err
		}
		data := execution{}
		for rows.Next() {
			err := rows.StructScan(&data)
			if err != nil {
				return err
			}
			if jsonFormat {
				err := enc.Encode(data)
				if err != nil {
					return err
				}
			} else {
				buf.WriteString(data.Time.Format("Mon, 02 Jan 2006 15:04:05"))
				buf.Write([]byte("  "))
				buf.WriteString(data.Line)
				buf.WriteByte('\n')
			}
		}
		return buf.Flush()
	},
}
<commit_msg>Add 'ls' and 'man' typed command<commit_after>package extra

import (
	"bufio"
	"encoding/json"
	"fmt"
	"os"
	"os/exec"
	"os/signal"
	"strconv"
	"time"

	"github.com/pkg/errors"

	"github.com/jmoiron/sqlx"

	"github.com/elpinal/coco3/extra/ast"
	"github.com/elpinal/coco3/extra/parser" // Only for ParseError.
	"github.com/elpinal/coco3/extra/typed"
	"github.com/elpinal/coco3/extra/types"
)

type Env struct {
	cmds map[string]typed.Command
	Option
}

type Option struct {
	DB *sqlx.DB
}

func New(opt Option) Env {
	return Env{
		Option: opt,
		cmds: map[string]typed.Command{
			"exec":    execCommand,
			"cd":      cdCommand,
			"exit":    exitCommand,
			"free":    freeCommand,
			"history": historyCommand,

			"ls":  lsCommand,
			"man": manCommand,

			"git":   gitCommand,
			"cargo": cargoCommand,
			"go":    goCommand,
			"stack": stackCommand,

			"vim":    vimCommand,
			"emacs":  emacsCommand,
			"screen": screenCommand,
		},
	}
}

func WithoutDefault() Env {
	return Env{cmds: make(map[string]typed.Command)}
}

func (e *Env) Bind(name string, c typed.Command) {
	e.cmds[name] = c
}

func (e *Env) Eval(command *ast.Command) error {
	if command == nil {
		return nil
	}
	tc, found := e.cmds[command.Name.Lit]
	if !found {
		return &parser.ParseError{
			Msg:    fmt.Sprintf("no such typed command: %q", command.Name.Lit),
			Line:   command.Name.Line,
			Column: command.Name.Column,
		}
	}
	if len(command.Args) != len(tc.Params) {
		return &parser.ParseError{
			Msg:    fmt.Sprintf("the length of args (%d) != the one of params (%d)", len(command.Args), len(tc.Params)),
			Line:   command.Name.Line,
			Column: command.Name.Column,
		}
	}
	for i, arg := range command.Args {
		if arg.Type() != tc.Params[i] {
			return &parser.ParseError{
				Msg:    fmt.Sprintf("type mismatch: (%v) (type of %v) does not match with (%v) (expected type)", arg.Type(), arg, tc.Params[i]),
				Line:   command.Name.Line,
				Column: command.Name.Column,
			}
		}
	}

	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)
	defer close(c)
	defer signal.Stop(c)

	return tc.Fn(command.Args, e.DB)
}

func toSlice(list ast.List) ([]string, error) {
	ret := make([]string, 0, list.Length())
	for {
		switch x := list.(type) {
		case *ast.Cons:
			ret = append(ret, x.Head)
			list = x.Tail
		case *ast.Empty:
			return ret, nil
		default:
			return nil, fmt.Errorf("unexpected list type: %T", x)
		}
	}
}

var execCommand = typed.Command{
	Params: []types.Type{types.String, types.StringList},
	Fn: func(args []ast.Expr, _ *sqlx.DB) error {
		cmdArgs, err := toSlice(args[1].(ast.List))
		if err != nil {
			return errors.Wrap(err, "exec")
		}
		cmd := stdCmd(args[0].(*ast.String).Lit, cmdArgs...)
		return cmd.Run()
	},
}

var cdCommand = typed.Command{
	Params: []types.Type{types.String},
	Fn: func(args []ast.Expr, _ *sqlx.DB) error {
		return os.Chdir(args[0].(*ast.String).Lit)
	},
}

var exitCommand = typed.Command{
	Params: []types.Type{types.Int},
	Fn: func(args []ast.Expr, _ *sqlx.DB) error {
		n, err := strconv.Atoi(args[0].(*ast.Int).Lit)
		if err != nil {
			return err
		}
		os.Exit(n)
		return nil
	},
}

var freeCommand = typed.Command{
	Params: []types.Type{types.String, types.StringList},
	Fn: func(args []ast.Expr, _ *sqlx.DB) error {
		cmdArgs, err := toSlice(args[1].(ast.List))
		if err != nil {
			return errors.Wrap(err, "free")
		}
		name := args[0].(*ast.String).Lit
		cmd := exec.Cmd{Path: name, Args: append([]string{name}, cmdArgs...)}
		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr
		cmd.Stdin = os.Stdin
		return cmd.Run()
	},
}

func commandsInCommand(name string) func([]ast.Expr, *sqlx.DB) error {
	return func(args []ast.Expr, _ *sqlx.DB) error {
		cmdArgs, err := toSlice(args[1].(ast.List))
		if err != nil {
			return errors.Wrap(err, name)
		}
		var cmd *exec.Cmd
		switch lit := args[0].(*ast.Ident).Lit; lit {
		case "command":
			cmd = stdCmd(name, cmdArgs...)
		default:
			cmd = stdCmd(name, append([]string{lit}, cmdArgs...)...)
		}
		return cmd.Run()
	}
}

var gitCommand = typed.Command{
	Params: []types.Type{types.Ident, types.StringList},
	Fn:     commandsInCommand("git"),
}

var cargoCommand = typed.Command{
	Params: []types.Type{types.Ident, types.StringList},
	Fn:     commandsInCommand("cargo"),
}

var goCommand = typed.Command{
	Params: []types.Type{types.Ident, types.StringList},
	Fn:     commandsInCommand("go"),
}

var stackCommand = typed.Command{
	Params: []types.Type{types.Ident, types.StringList},
	Fn:     commandsInCommand("stack"),
}

func stdCmd(name string, args ...string) *exec.Cmd {
	cmd := exec.Command(name, args...)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	cmd.Stdin = os.Stdin
	return cmd
}

func stdExec(name string, args ...string) func([]ast.Expr, *sqlx.DB) error {
	return func(_ []ast.Expr, _ *sqlx.DB) error {
		return stdCmd(name, args...).Run()
	}
}

var vimCommand = typed.Command{
	Params: []types.Type{},
	Fn:     stdExec("vim"),
}

var emacsCommand = typed.Command{
	Params: []types.Type{},
	Fn:     stdExec("emacs"),
}

func withEnv(s string, cmd *exec.Cmd) *exec.Cmd {
	if cmd.Env == nil {
		cmd.Env = os.Environ()
	}
	cmd.Env = append(cmd.Env, s)
	return cmd
}

var screenCommand = typed.Command{
	Params: []types.Type{},
	Fn: func(_ []ast.Expr, _ *sqlx.DB) error {
		return withEnv("LANG=en_US.UTF-8", stdCmd("screen")).Run()
	},
}

type execution struct {
	Time time.Time
	Line string
}

var historyCommand = typed.Command{
	Params: []types.Type{types.String},
	Fn: func(e []ast.Expr, db *sqlx.DB) error {
		var jsonFormat bool
		var enc *json.Encoder
		switch format := e[0].(*ast.String).Lit; format {
		case "json":
			jsonFormat = true
		case "lines":
		default:
			return fmt.Errorf("history: format %q is not supported", format)
		}

		buf := bufio.NewWriter(os.Stdout)
		if jsonFormat {
			enc = json.NewEncoder(buf)
		}
		rows, err := db.Queryx("select * from command_info")
		if err != nil {
			return err
		}
		data := execution{}
		for rows.Next() {
			err := rows.StructScan(&data)
			if err != nil {
				return err
			}
			if jsonFormat {
				err := enc.Encode(data)
				if err != nil {
					return err
				}
			} else {
				buf.WriteString(data.Time.Format("Mon, 02 Jan 2006 15:04:05"))
				buf.Write([]byte("  "))
				buf.WriteString(data.Line)
				buf.WriteByte('\n')
			}
		}
		return buf.Flush()
	},
}

var lsCommand = typed.Command{
	Params: []types.Type{},
	Fn:     stdExec("ls", "--show-control-chars", "--color=auto"),
}

var manCommand = typed.Command{
	Params: []types.Type{types.String},
	Fn: func(e []ast.Expr, _ *sqlx.DB) error {
		lit := e[0].(*ast.String).Lit
		return stdCmd("man", lit).Run()
	},
}
<|endoftext|>


<commit_before>// Copyright 2018 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.

// dlp is an example of using the DLP API.
package main

import (
	"context"
	"flag"
	"fmt"
	"io"
	"log"
	"os"

	dlp "cloud.google.com/go/dlp/apiv2beta1"
	dlppb "google.golang.org/genproto/googleapis/privacy/dlp/v2beta1"
)

func inspect(w io.Writer, client *dlp.Client, s string) {
	rcr := &dlppb.InspectContentRequest{
		InspectConfig: &dlppb.InspectConfig{
			InfoTypes: []*dlppb.InfoType{
				{
					Name: "US_SOCIAL_SECURITY_NUMBER",
				},
			},
			MinLikelihood: dlppb.Likelihood_LIKELIHOOD_UNSPECIFIED,
		},
		Items: []*dlppb.ContentItem{
			{
				Type: "text/plain",
				DataItem: &dlppb.ContentItem_Data{
					Data: []byte(s),
				},
			},
		},
	}
	r, err := client.InspectContent(context.Background(), rcr)
	if err != nil {
		log.Fatal(err)
	}
	fs := r.GetResults()[0].GetFindings()
	for _, f := range fs {
		fmt.Fprintf(w, "%s\n", f.GetInfoType().GetName())
	}
}

func redact(w io.Writer, client *dlp.Client, s string) {
	rcr := &dlppb.RedactContentRequest{
		InspectConfig: &dlppb.InspectConfig{
			InfoTypes: []*dlppb.InfoType{
				{
					Name: "US_SOCIAL_SECURITY_NUMBER",
				},
			},
			MinLikelihood: dlppb.Likelihood_LIKELIHOOD_UNSPECIFIED,
		},
		ReplaceConfigs: []*dlppb.RedactContentRequest_ReplaceConfig{
			{
				InfoType:    &dlppb.InfoType{Name: "US_SOCIAL_SECURITY_NUMBER"},
				ReplaceWith: "[redacted]",
			},
		},
		Items: []*dlppb.ContentItem{
			{
				Type: "text/plain",
				DataItem: &dlppb.ContentItem_Data{
					Data: []byte(s),
				},
			},
		},
	}
	r, err := client.RedactContent(context.Background(), rcr)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Fprintf(w, "%s\n", r.GetItems()[0].GetData())
}

func infoTypes(w io.Writer, client *dlp.Client, s string) {
	rcr := &dlppb.ListInfoTypesRequest{
		Category: s,
	}
	r, err := client.ListInfoTypes(context.Background(), rcr)
	if err != nil {
		log.Fatal(err)
	}
	for _, it := range r.GetInfoTypes() {
		fmt.Fprintf(w, "%s\n", it.GetName())
	}
}

func categories(w io.Writer, client *dlp.Client) {
	rcr := &dlppb.ListRootCategoriesRequest{}
	r, err := client.ListRootCategories(context.Background(), rcr)
	if err != nil {
		log.Fatal(err)
	}
	for _, c := range r.GetCategories() {
		fmt.Fprintf(w, "%s (%s)\n", c.GetName(), c.GetDisplayName())
	}
}
func main() {
	ctx := context.Background()
	client, err := dlp.NewClient(ctx)
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	flag.Parse()

	if flag.NArg() != 2 {
		fmt.Fprintf(os.Stderr, `Usage: %s CMD "string"\n`, os.Args[0])
		os.Exit(1)
	}
	switch flag.Arg(0) {
	case "inspect":
		inspect(os.Stdout, client, flag.Arg(1))
	case "redact":
		redact(os.Stdout, client, flag.Arg(1))
	case "infoTypes":
		infoTypes(os.Stdout, client, flag.Arg(1))
	case "categories":
		categories(os.Stdout, client)

	}
}
<commit_msg>dlp: add basic deidentify example<commit_after>// Copyright 2018 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.

// dlp is an example of using the DLP API.
package main

import (
	"context"
	"flag"
	"fmt"
	"io"
	"log"
	"os"

	dlp "cloud.google.com/go/dlp/apiv2beta1"
	dlppb "google.golang.org/genproto/googleapis/privacy/dlp/v2beta1"
)

func inspect(w io.Writer, client *dlp.Client, s string) {
	rcr := &dlppb.InspectContentRequest{
		InspectConfig: &dlppb.InspectConfig{
			InfoTypes: []*dlppb.InfoType{
				{
					Name: "US_SOCIAL_SECURITY_NUMBER",
				},
			},
			MinLikelihood: dlppb.Likelihood_LIKELIHOOD_UNSPECIFIED,
		},
		Items: []*dlppb.ContentItem{
			{
				Type: "text/plain",
				DataItem: &dlppb.ContentItem_Data{
					Data: []byte(s),
				},
			},
		},
	}
	r, err := client.InspectContent(context.Background(), rcr)
	if err != nil {
		log.Fatal(err)
	}
	fs := r.GetResults()[0].GetFindings()
	for _, f := range fs {
		fmt.Fprintf(w, "%s\n", f.GetInfoType().GetName())
	}
}

func redact(w io.Writer, client *dlp.Client, s string) {
	rcr := &dlppb.RedactContentRequest{
		InspectConfig: &dlppb.InspectConfig{
			InfoTypes: []*dlppb.InfoType{
				{
					Name: "US_SOCIAL_SECURITY_NUMBER",
				},
			},
			MinLikelihood: dlppb.Likelihood_LIKELIHOOD_UNSPECIFIED,
		},
		ReplaceConfigs: []*dlppb.RedactContentRequest_ReplaceConfig{
			{
				InfoType:    &dlppb.InfoType{Name: "US_SOCIAL_SECURITY_NUMBER"},
				ReplaceWith: "[redacted]",
			},
		},
		Items: []*dlppb.ContentItem{
			{
				Type: "text/plain",
				DataItem: &dlppb.ContentItem_Data{
					Data: []byte(s),
				},
			},
		},
	}
	r, err := client.RedactContent(context.Background(), rcr)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Fprintf(w, "%s\n", r.GetItems()[0].GetData())
}

func infoTypes(w io.Writer, client *dlp.Client, s string) {
	rcr := &dlppb.ListInfoTypesRequest{
		Category: s,
	}
	r, err := client.ListInfoTypes(context.Background(), rcr)
	if err != nil {
		log.Fatal(err)
	}
	for _, it := range r.GetInfoTypes() {
		fmt.Fprintf(w, "%s\n", it.GetName())
	}
}

func categories(w io.Writer, client *dlp.Client) {
	rcr := &dlppb.ListRootCategoriesRequest{}
	r, err := client.ListRootCategories(context.Background(), rcr)
	if err != nil {
		log.Fatal(err)
	}
	for _, c := range r.GetCategories() {
		fmt.Fprintf(w, "%s (%s)\n", c.GetName(), c.GetDisplayName())
	}
}

func deID(w io.Writer, client *dlp.Client, s string) {
	rcr := &dlppb.DeidentifyContentRequest{
		DeidentifyConfig: &dlppb.DeidentifyConfig{
			Transformation: &dlppb.DeidentifyConfig_InfoTypeTransformations{
				InfoTypeTransformations: &dlppb.InfoTypeTransformations{
					Transformations: []*dlppb.InfoTypeTransformations_InfoTypeTransformation{
						{
							InfoTypes: []*dlppb.InfoType{},
							PrimitiveTransformation: &dlppb.PrimitiveTransformation{
								Transformation: &dlppb.PrimitiveTransformation_CharacterMaskConfig{
									CharacterMaskConfig: &dlppb.CharacterMaskConfig{
										MaskingCharacter: "*",
									},
								},
							},
						},
					},
				},
			},
		},
		InspectConfig: &dlppb.InspectConfig{
			InfoTypes: []*dlppb.InfoType{
				{
					Name: "US_SOCIAL_SECURITY_NUMBER",
				},
			},
			MinLikelihood: dlppb.Likelihood_LIKELIHOOD_UNSPECIFIED,
		},
		Items: []*dlppb.ContentItem{
			{
				Type: "text/plain",
				DataItem: &dlppb.ContentItem_Data{
					Data: []byte(s),
				},
			},
		},
	}
	r, err := client.DeidentifyContent(context.Background(), rcr)
	if err != nil {
		log.Fatal(err)
	}
	for _, i := range r.GetItems() {
		fmt.Fprintf(w, "%s\n", i.GetData())
	}
}
func main() {
	ctx := context.Background()
	client, err := dlp.NewClient(ctx)
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	flag.Parse()

	if flag.NArg() != 2 {
		fmt.Fprintf(os.Stderr, `Usage: %s CMD "string"\n`, os.Args[0])
		os.Exit(1)
	}
	switch flag.Arg(0) {
	case "inspect":
		inspect(os.Stdout, client, flag.Arg(1))
	case "redact":
		redact(os.Stdout, client, flag.Arg(1))
	case "infoTypes":
		infoTypes(os.Stdout, client, flag.Arg(1))
	case "categories":
		categories(os.Stdout, client)
	case "deid":
		deID(os.Stdout, client, flag.Arg(1))
	}
}
<|endoftext|>


<commit_before>package timebatchdb

import (
    "testing"
    )

func TestMessenger(t *testing.T) {
    msg,err := ConnectMessenger("localhost:4222")
    if err!=nil {
        t.Errorf("Couldn't connect: %s",err)
        return
    }
    defer msg.Close()

    msg2,err := ConnectMessenger("localhost:4222")
    if err!=nil {
        t.Errorf("Couldn't connect: %s",err)
        return
    }
    defer msg2.Close()

    recvchan := make(chan *Message)
    _,err = msg2.SubChannel("user1/>",recvchan)
    if err != nil {
        t.Errorf("Couldn't bind channel: %s",err)
        return
    }

    //Now, publish a message
    err = msg.Publish("user1/item1/stream1",1000,[]byte("Hello World!"))
    if (err != nil) {
        t.Errorf("Couldn't publish: %s",err)
        return
    }

    m := <- recvchan

    if (m.Timestamp!=1000 || string(m.Data)!="Hello World!" || m.Key!="user1/item1/stream1") {
        t.Errorf("Incorrect read %s",m)
        return
    }

}
<commit_msg>Added timeout to messenger test<commit_after>package timebatchdb

import (
    "testing"
    "time"
    )

func TestMessenger(t *testing.T) {
    msg,err := ConnectMessenger("localhost:4222")
    if err!=nil {
        t.Errorf("Couldn't connect: %s",err)
        return
    }
    defer msg.Close()

    msg2,err := ConnectMessenger("localhost:4222")
    if err!=nil {
        t.Errorf("Couldn't connect: %s",err)
        return
    }
    defer msg2.Close()

    recvchan := make(chan *Message)
    _,err = msg2.SubChannel("user1/>",recvchan)
    if err != nil {
        t.Errorf("Couldn't bind channel: %s",err)
        return
    }

    //Now, publish a message
    err = msg.Publish("user1/item1/stream1",1000,[]byte("Hello World!"))
    if (err != nil) {
        t.Errorf("Couldn't publish: %s",err)
        return
    }

    go func() {
        time.Sleep(1*time.Second)
        recvchan <- &Message{0,"TIMEOUT",nil}
    }()

    m := <- recvchan
    if (m.Key=="TIMEOUT") {
        t.Errorf("Message read timed out!")
        return
    }

    if (m.Timestamp!=1000 || string(m.Data)!="Hello World!" || m.Key!="user1/item1/stream1") {
        t.Errorf("Incorrect read %s",m)
        return
    }

}
<|endoftext|>


<commit_before>package algoliaconnector

import (
	"encoding/json"
	"fmt"
	"socialapi/models"
	"strconv"

	"github.com/algolia/algoliasearch-client-go/algoliasearch"
	"github.com/koding/logging"
	"github.com/streadway/amqp"
)

var (
	ErrAlgoliaObjectIdNotFoundMsg = "ObjectID does not exist"
	ErrAlgoliaIndexNotExistMsg    = "Index messages.test does not exist"
)

type IndexSet map[string]*algoliasearch.Index

type Controller struct {
	log     logging.Logger
	client  *algoliasearch.Client
	indexes *IndexSet
}

// IsAlgoliaError checks if the given algolia error string and given messages
// are same according their data structure
func IsAlgoliaError(err error, message string) bool {
	if err == nil {
		return false
	}

	v := &algoliaErrorRes{}

	if err := json.Unmarshal([]byte(err.Error()), v); err != nil {
		return false
	}

	if v.Message == message {
		return true
	}

	return false
}

type algoliaErrorRes struct {
	Message string `json:"message"`
	Status  int    `json:"status"`
}

func (i *IndexSet) Get(name string) (*algoliasearch.Index, error) {
	index, ok := (*i)[name]
	if !ok {
		return nil, fmt.Errorf("Unknown index: '%s'", name)
	}
	return index, nil
}

func (c *Controller) DefaultErrHandler(delivery amqp.Delivery, err error) bool {
	c.log.Error(err.Error())
	return false
}

func New(log logging.Logger, client *algoliasearch.Client, indexSuffix string) *Controller {
	return &Controller{
		log:    log,
		client: client,
		indexes: &IndexSet{
			"topics":   client.InitIndex("topics" + indexSuffix),
			"accounts": client.InitIndex("accounts" + indexSuffix),
			"messages": client.InitIndex("messages" + indexSuffix),
		},
	}
}

func (f *Controller) TopicSaved(data *models.Channel) error {
	if data.TypeConstant != models.Channel_TYPE_TOPIC {
		return nil
	}
	return f.insert("topics", map[string]interface{}{
		"objectID": strconv.FormatInt(data.Id, 10),
		"name":     data.Name,
		"purpose":  data.Purpose,
	})
}

func (f *Controller) AccountSaved(data *models.Account) error {
	return f.insert("accounts", map[string]interface{}{
		"objectID": data.OldId,
		"nick":     data.Nick,
	})
}

func (f *Controller) MessageListSaved(listing *models.ChannelMessageList) error {
	message := models.NewChannelMessage()

	if err := message.ById(listing.MessageId); err != nil {
		return err
	}

	objectId := strconv.FormatInt(message.Id, 10)
	channelId := strconv.FormatInt(listing.ChannelId, 10)

	record, err := f.get("messages", objectId)
	if err != nil &&
		!IsAlgoliaError(err, ErrAlgoliaObjectIdNotFoundMsg) &&
		!IsAlgoliaError(err, ErrAlgoliaIndexNotExistMsg) {
		return err
	}

	if record == nil {
		return f.insert("messages", map[string]interface{}{
			"objectID": objectId,
			"body":     message.Body,
			"_tags":    []string{channelId},
		})
	}

	return f.partialUpdate("messages", map[string]interface{}{
		"objectID": objectId,
		"_tags":    appendMessageTag(record, channelId),
	})
}

func (f *Controller) MessageListDeleted(listing *models.ChannelMessageList) error {
	index, err := f.indexes.Get("messages")
	if err != nil {
		return err
	}

	objectId := strconv.FormatInt(listing.MessageId, 10)

	record, err := f.get("messages", objectId)
	if err != nil &&
		!IsAlgoliaError(err, ErrAlgoliaObjectIdNotFoundMsg) &&
		!IsAlgoliaError(err, ErrAlgoliaIndexNotExistMsg) {
		return err
	}

	if tags, ok := record["_tags"]; ok {
		if t, ok := tags.([]interface{}); ok && len(t) == 1 {
			if _, err = index.DeleteObject(objectId); err != nil {
				return err
			}
			return nil
		}
	}

	return f.partialUpdate("messages", map[string]interface{}{
		"objectID": objectId,
		"_tags":    removeMessageTag(record, strconv.FormatInt(listing.ChannelId, 10)),
	})
}

func (f *Controller) MessageUpdated(message *models.ChannelMessage) error {
	return f.partialUpdate("messages", map[string]interface{}{
		"objectID": strconv.FormatInt(message.Id, 10),
		"body":     message.Body,
	})
}
<commit_msg>algolia: add public channel id into account document tags<commit_after>package algoliaconnector

import (
	"encoding/json"
	"fmt"
	"socialapi/models"
	"socialapi/request"
	"strconv"

	"github.com/algolia/algoliasearch-client-go/algoliasearch"
	"github.com/koding/logging"
	"github.com/streadway/amqp"
)

var (
	ErrAlgoliaObjectIdNotFoundMsg = "ObjectID does not exist"
	ErrAlgoliaIndexNotExistMsg    = "Index messages.test does not exist"
)

type IndexSet map[string]*algoliasearch.Index

type Controller struct {
	log             logging.Logger
	client          *algoliasearch.Client
	indexes         *IndexSet
	kodingChannelId string
}

// IsAlgoliaError checks if the given algolia error string and given messages
// are same according their data structure
func IsAlgoliaError(err error, message string) bool {
	if err == nil {
		return false
	}

	v := &algoliaErrorRes{}

	if err := json.Unmarshal([]byte(err.Error()), v); err != nil {
		return false
	}

	if v.Message == message {
		return true
	}

	return false
}

type algoliaErrorRes struct {
	Message string `json:"message"`
	Status  int    `json:"status"`
}

func (i *IndexSet) Get(name string) (*algoliasearch.Index, error) {
	index, ok := (*i)[name]
	if !ok {
		return nil, fmt.Errorf("Unknown index: '%s'", name)
	}
	return index, nil
}

func (c *Controller) DefaultErrHandler(delivery amqp.Delivery, err error) bool {
	c.log.Error(err.Error())
	return false
}

func New(log logging.Logger, client *algoliasearch.Client, indexSuffix string) *Controller {
	// TODO later on listen channel_participant_added event and remove this koding channel fetch
	c := models.NewChannel()
	q := request.NewQuery()
	q.GroupName = "koding"
	q.Name = "public"
	q.Type = models.Channel_TYPE_GROUP

	channel, err := c.ByName(q)
	if err != nil {
		log.Fatal("Could not fetch koding channel: %s:", err)
	}

	return &Controller{
		log:    log,
		client: client,
		indexes: &IndexSet{
			"topics":   client.InitIndex("topics" + indexSuffix),
			"accounts": client.InitIndex("accounts" + indexSuffix),
			"messages": client.InitIndex("messages" + indexSuffix),
		},
		kodingChannelId: strconv.FormatInt(channel.Id, 10),
	}
}

func (f *Controller) TopicSaved(data *models.Channel) error {
	if data.TypeConstant != models.Channel_TYPE_TOPIC {
		return nil
	}
	return f.insert("topics", map[string]interface{}{
		"objectID": strconv.FormatInt(data.Id, 10),
		"name":     data.Name,
		"purpose":  data.Purpose,
	})
}

func (f *Controller) AccountSaved(data *models.Account) error {
	return f.insert("accounts", map[string]interface{}{
		"objectID": data.OldId,
		"nick":     data.Nick,
		"_tags":    []string{f.kodingChannelId},
	})
}

func (f *Controller) MessageListSaved(listing *models.ChannelMessageList) error {
	message := models.NewChannelMessage()

	if err := message.ById(listing.MessageId); err != nil {
		return err
	}

	objectId := strconv.FormatInt(message.Id, 10)
	channelId := strconv.FormatInt(listing.ChannelId, 10)

	record, err := f.get("messages", objectId)
	if err != nil &&
		!IsAlgoliaError(err, ErrAlgoliaObjectIdNotFoundMsg) &&
		!IsAlgoliaError(err, ErrAlgoliaIndexNotExistMsg) {
		return err
	}

	if record == nil {
		return f.insert("messages", map[string]interface{}{
			"objectID": objectId,
			"body":     message.Body,
			"_tags":    []string{channelId},
		})
	}

	return f.partialUpdate("messages", map[string]interface{}{
		"objectID": objectId,
		"_tags":    appendMessageTag(record, channelId),
	})
}

func (f *Controller) MessageListDeleted(listing *models.ChannelMessageList) error {
	index, err := f.indexes.Get("messages")
	if err != nil {
		return err
	}

	objectId := strconv.FormatInt(listing.MessageId, 10)

	record, err := f.get("messages", objectId)
	if err != nil &&
		!IsAlgoliaError(err, ErrAlgoliaObjectIdNotFoundMsg) &&
		!IsAlgoliaError(err, ErrAlgoliaIndexNotExistMsg) {
		return err
	}

	if tags, ok := record["_tags"]; ok {
		if t, ok := tags.([]interface{}); ok && len(t) == 1 {
			if _, err = index.DeleteObject(objectId); err != nil {
				return err
			}
			return nil
		}
	}

	return f.partialUpdate("messages", map[string]interface{}{
		"objectID": objectId,
		"_tags":    removeMessageTag(record, strconv.FormatInt(listing.ChannelId, 10)),
	})
}

func (f *Controller) MessageUpdated(message *models.ChannelMessage) error {
	return f.partialUpdate("messages", map[string]interface{}{
		"objectID": strconv.FormatInt(message.Id, 10),
		"body":     message.Body,
	})
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"fmt"
	"io/ioutil"
	"net"
	"net/http"
	"net/http/httputil"
	"net/url"
	"os"
	"strings"
)

var (
	method, data, Header string
	Verbose              bool
)

func usage() {
	fmt.Println("curl_unix [-X=METHOD -d='DATA'] <URL>")
}

func setupFlags() {
	flag.StringVar(&method, "X", "GET", "Method of the HTTP request")
	flag.StringVar(&data, "d", "", "Body to send in the request")
	flag.StringVar(&Header, "H", "", "Additional headers: k1:v1|k2:v2|...")
	flag.BoolVar(&Verbose, "v", false, "Verbose information")
	flag.Parse()
}

func checkURL() (*url.URL, error) {
	u, err := url.Parse(flag.Args()[0])
	if err != nil {
		return nil, err
	}
	if u.Scheme != "unix" {
		return nil, fmt.Errorf("Scheme must be unix ie. unix:///var/run/daemon/sock:/path")
	}
	return u, nil
}

func main() {
	if len(os.Args) == 1 {
		usage()
		os.Exit(1)
	}
	setupFlags()
	u, err := checkURL()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	hostAndPath := strings.Split(u.Path, ":")
	u.Host = hostAndPath[0]
	u.Path = hostAndPath[1]

	reader := strings.NewReader(data)
	if len(data) > 0 {
		// If there are data the request can't be GET (curl behavior)
		if method == "GET" {
			method = "POST"
		}
	}

	req, err := http.NewRequest(method, "http://localhost:4243"+u.Path, reader)
	if err != nil {
		fmt.Println("Fail to create http request", err)
		os.Exit(1)
	}
	if err := addHeaders(req); err != nil {
		fmt.Println("Fail to add headers:", err)
		os.Exit(1)
	}

	conn, err := net.Dial("unix", u.Host)
	if err != nil {
		fmt.Println("Fail to connect to", u.Host, ":", err)
		os.Exit(1)
	}
	client := httputil.NewClientConn(conn, nil)
	res, err := requestExecute(client, req)
	if err != nil {
		fmt.Println("Fail to achieve http request over unix socket", err)
		os.Exit(1)
	}

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		fmt.Println("Invalid body in answer")
		os.Exit(1)
	}

	fmt.Println(string(body))
	defer res.Body.Close()

}
<commit_msg>update usage()<commit_after>package main

import (
	"flag"
	"fmt"
	"io/ioutil"
	"net"
	"net/http"
	"net/http/httputil"
	"net/url"
	"os"
	"strings"
)

var (
	method, data, Header string
	Verbose              bool
)

func usage() {
	flag.Usage()
	fmt.Println("\n→ ./curl-unix-socket [options] <URL: unix:///path/file.sock:/path>")
}

func setupFlags() {
	flag.StringVar(&method, "X", "GET", "Method of the HTTP request")
	flag.StringVar(&data, "d", "", "Body to send in the request")
	flag.StringVar(&Header, "H", "", "Additional headers: k1:v1|k2:v2|...")
	flag.BoolVar(&Verbose, "v", false, "Verbose information")
	flag.Parse()
}

func checkURL() (*url.URL, error) {
	u, err := url.Parse(flag.Args()[0])
	if err != nil {
		return nil, err
	}
	if u.Scheme != "unix" {
		return nil, fmt.Errorf("Scheme must be unix ie. unix:///var/run/daemon/sock:/path")
	}
	return u, nil
}

func main() {
	setupFlags()
	if len(os.Args) == 1 {
		usage()
		os.Exit(1)
	}
	u, err := checkURL()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	hostAndPath := strings.Split(u.Path, ":")
	u.Host = hostAndPath[0]
	u.Path = hostAndPath[1]

	reader := strings.NewReader(data)
	if len(data) > 0 {
		// If there are data the request can't be GET (curl behavior)
		if method == "GET" {
			method = "POST"
		}
	}

	req, err := http.NewRequest(method, "http://localhost:4243"+u.Path, reader)
	if err != nil {
		fmt.Println("Fail to create http request", err)
		os.Exit(1)
	}
	if err := addHeaders(req); err != nil {
		fmt.Println("Fail to add headers:", err)
		os.Exit(1)
	}

	conn, err := net.Dial("unix", u.Host)
	if err != nil {
		fmt.Println("Fail to connect to", u.Host, ":", err)
		os.Exit(1)
	}
	client := httputil.NewClientConn(conn, nil)
	res, err := requestExecute(client, req)
	if err != nil {
		fmt.Println("Fail to achieve http request over unix socket", err)
		os.Exit(1)
	}

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		fmt.Println("Invalid body in answer")
		os.Exit(1)
	}

	fmt.Println(string(body))
	defer res.Body.Close()

}
<|endoftext|>


<commit_before>package terraform

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"os/exec"
	"strings"

	"terraform-resource/models"
	"terraform-resource/storage"
)

type Client struct {
	Model         models.Terraform
	StorageDriver storage.Storage
	LogWriter     io.Writer
}

func (c Client) Apply() error {
	tmpDir, err := ioutil.TempDir(os.TempDir(), "terraform-resource-client")
	if err != nil {
		return fmt.Errorf("Failed to create temporary working dir at '%s'", os.TempDir())
	}
	defer os.RemoveAll(tmpDir)

	initCmd := terraformCmd([]string{
		"init",
		c.Model.Source,
		tmpDir,
	})
	if initOutput, initErr := initCmd.CombinedOutput(); initErr != nil {
		return fmt.Errorf("terraform init command failed.\nError: %s\nOutput: %s", initErr, initOutput)
	}

	getCmd := terraformCmd([]string{
		"get",
		"-update",
		tmpDir,
	})
	if getOutput, getErr := getCmd.CombinedOutput(); getErr != nil {
		return fmt.Errorf("terraform get command failed.\nError: %s\nOutput: %s", getErr, getOutput)
	}

	applyArgs := []string{
		"apply",
		"-backup='-'",  // no need to backup state file
		"-input=false", // do not prompt for inputs
		fmt.Sprintf("-state=%s", c.Model.StateFileLocalPath),
	}
	for key, val := range c.Model.Vars {
		applyArgs = append(applyArgs, "-var", fmt.Sprintf("'%s=%v'", key, val))
	}
	applyArgs = append(applyArgs, tmpDir)

	applyCmd := terraformCmd(applyArgs)
	applyCmd.Stdout = c.LogWriter
	applyCmd.Stderr = c.LogWriter
	err = applyCmd.Run()
	if err != nil {
		return fmt.Errorf("Failed to run Terraform command: %s", err)
	}

	return nil
}

func (c Client) Destroy() error {
	tmpDir, err := ioutil.TempDir(os.TempDir(), "terraform-resource-client")
	if err != nil {
		return fmt.Errorf("Failed to create temporary working dir at '%s'", os.TempDir())
	}
	defer os.RemoveAll(tmpDir)

	initCmd := terraformCmd([]string{
		"init",
		c.Model.Source,
		tmpDir,
	})
	if initOutput, initErr := initCmd.CombinedOutput(); initErr != nil {
		return fmt.Errorf("terraform init command failed.\nError: %s\nOutput: %s", initErr, initOutput)
	}

	getCmd := terraformCmd([]string{
		"get",
		"-update",
		tmpDir,
	})
	if getOutput, getErr := getCmd.CombinedOutput(); getErr != nil {
		return fmt.Errorf("terraform get command failed.\nError: %s\nOutput: %s", getErr, getOutput)
	}

	destroyArgs := []string{
		"destroy",
		"-backup='-'", // no need to backup state file
		"-force",      // do not prompt for confirmation
		fmt.Sprintf("-state=%s", c.Model.StateFileLocalPath),
	}
	for key, val := range c.Model.Vars {
		destroyArgs = append(destroyArgs, "-var", fmt.Sprintf("'%s=%v'", key, val))
	}
	destroyArgs = append(destroyArgs, tmpDir)

	destroyCmd := terraformCmd(destroyArgs)
	destroyCmd.Stdout = c.LogWriter
	destroyCmd.Stderr = c.LogWriter
	err = destroyCmd.Run()
	if err != nil {
		return fmt.Errorf("Failed to run Terraform command: %s", err)
	}

	return nil
}

func (c Client) Output() (map[string]interface{}, error) {
	outputCmd := terraformCmd([]string{
		"output",
		fmt.Sprintf("-state=%s", c.Model.StateFileLocalPath),
	})
	rawOutput, err := outputCmd.CombinedOutput()
	if err != nil {
		return nil, fmt.Errorf("Failed to retrieve output.\nError: %s\nOutput: %s", err, rawOutput)
	}

	output := map[string]interface{}{}
	scanner := bufio.NewScanner(bytes.NewReader(rawOutput))
	for scanner.Scan() {
		thisLine := strings.Split(scanner.Text(), " = ")
		key, value := thisLine[0], thisLine[1]
		output[key] = value
	}
	if err := scanner.Err(); err != nil {
		return nil, fmt.Errorf("Failed to parse output.\nError: %s\nOutput: %s", err, rawOutput)
	}

	return output, nil
}

func terraformCmd(args []string) *exec.Cmd {
	return exec.Command("/bin/bash", "-c", fmt.Sprintf("terraform %s", strings.Join(args, " ")))
}
<commit_msg>Swap bash for sh when shelling out to terraform<commit_after>package terraform

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"os/exec"
	"strings"

	"terraform-resource/models"
	"terraform-resource/storage"
)

type Client struct {
	Model         models.Terraform
	StorageDriver storage.Storage
	LogWriter     io.Writer
}

func (c Client) Apply() error {
	tmpDir, err := ioutil.TempDir(os.TempDir(), "terraform-resource-client")
	if err != nil {
		return fmt.Errorf("Failed to create temporary working dir at '%s'", os.TempDir())
	}
	defer os.RemoveAll(tmpDir)

	initCmd := terraformCmd([]string{
		"init",
		c.Model.Source,
		tmpDir,
	})
	if initOutput, initErr := initCmd.CombinedOutput(); initErr != nil {
		return fmt.Errorf("terraform init command failed.\nError: %s\nOutput: %s", initErr, initOutput)
	}

	getCmd := terraformCmd([]string{
		"get",
		"-update",
		tmpDir,
	})
	if getOutput, getErr := getCmd.CombinedOutput(); getErr != nil {
		return fmt.Errorf("terraform get command failed.\nError: %s\nOutput: %s", getErr, getOutput)
	}

	applyArgs := []string{
		"apply",
		"-backup='-'",  // no need to backup state file
		"-input=false", // do not prompt for inputs
		fmt.Sprintf("-state=%s", c.Model.StateFileLocalPath),
	}
	for key, val := range c.Model.Vars {
		applyArgs = append(applyArgs, "-var", fmt.Sprintf("'%s=%v'", key, val))
	}
	applyArgs = append(applyArgs, tmpDir)

	applyCmd := terraformCmd(applyArgs)
	applyCmd.Stdout = c.LogWriter
	applyCmd.Stderr = c.LogWriter
	err = applyCmd.Run()
	if err != nil {
		return fmt.Errorf("Failed to run Terraform command: %s", err)
	}

	return nil
}

func (c Client) Destroy() error {
	tmpDir, err := ioutil.TempDir(os.TempDir(), "terraform-resource-client")
	if err != nil {
		return fmt.Errorf("Failed to create temporary working dir at '%s'", os.TempDir())
	}
	defer os.RemoveAll(tmpDir)

	initCmd := terraformCmd([]string{
		"init",
		c.Model.Source,
		tmpDir,
	})
	if initOutput, initErr := initCmd.CombinedOutput(); initErr != nil {
		return fmt.Errorf("terraform init command failed.\nError: %s\nOutput: %s", initErr, initOutput)
	}

	getCmd := terraformCmd([]string{
		"get",
		"-update",
		tmpDir,
	})
	if getOutput, getErr := getCmd.CombinedOutput(); getErr != nil {
		return fmt.Errorf("terraform get command failed.\nError: %s\nOutput: %s", getErr, getOutput)
	}

	destroyArgs := []string{
		"destroy",
		"-backup='-'", // no need to backup state file
		"-force",      // do not prompt for confirmation
		fmt.Sprintf("-state=%s", c.Model.StateFileLocalPath),
	}
	for key, val := range c.Model.Vars {
		destroyArgs = append(destroyArgs, "-var", fmt.Sprintf("'%s=%v'", key, val))
	}
	destroyArgs = append(destroyArgs, tmpDir)

	destroyCmd := terraformCmd(destroyArgs)
	destroyCmd.Stdout = c.LogWriter
	destroyCmd.Stderr = c.LogWriter
	err = destroyCmd.Run()
	if err != nil {
		return fmt.Errorf("Failed to run Terraform command: %s", err)
	}

	return nil
}

func (c Client) Output() (map[string]interface{}, error) {
	outputCmd := terraformCmd([]string{
		"output",
		fmt.Sprintf("-state=%s", c.Model.StateFileLocalPath),
	})
	rawOutput, err := outputCmd.CombinedOutput()
	if err != nil {
		return nil, fmt.Errorf("Failed to retrieve output.\nError: %s\nOutput: %s", err, rawOutput)
	}

	output := map[string]interface{}{}
	scanner := bufio.NewScanner(bytes.NewReader(rawOutput))
	for scanner.Scan() {
		thisLine := strings.Split(scanner.Text(), " = ")
		key, value := thisLine[0], thisLine[1]
		output[key] = value
	}
	if err := scanner.Err(); err != nil {
		return nil, fmt.Errorf("Failed to parse output.\nError: %s\nOutput: %s", err, rawOutput)
	}

	return output, nil
}

func terraformCmd(args []string) *exec.Cmd {
	return exec.Command("/bin/sh", "-c", fmt.Sprintf("terraform %s", strings.Join(args, " ")))
}
<|endoftext|>


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

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"html/template"
	"io"
	"io/ioutil"
	"math"
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
)

// htmlOutput reads the profile data from profile and generates an HTML
// coverage report, writing it to outfile. If outfile is empty,
// it writes the report to a temporary file and opens it in a web browser.
func htmlOutput(profile, outfile string) error {
	profiles, err := ParseProfiles(profile)
	if err != nil {
		return err
	}

	var d templateData

	for _, profile := range profiles {
		fn := profile.FileName
		if profile.Mode == "set" {
			d.Set = true
		}
		file, err := findFile(fn)
		if err != nil {
			return err
		}
		src, err := ioutil.ReadFile(file)
		if err != nil {
			return fmt.Errorf("can't read %q: %v", fn, err)
		}
		var buf bytes.Buffer
		err = htmlGen(&buf, src, profile.Boundaries(src))
		if err != nil {
			return err
		}
		d.Files = append(d.Files, &templateFile{
			Name:     fn,
			Body:     template.HTML(buf.String()),
			Coverage: percentCovered(profile),
		})
	}

	var out *os.File
	if outfile == "" {
		var dir string
		dir, err = ioutil.TempDir("", "cover")
		if err != nil {
			return err
		}
		out, err = os.Create(filepath.Join(dir, "coverage.html"))
	} else {
		out, err = os.Create(outfile)
	}
	err = htmlTemplate.Execute(out, d)
	if err == nil {
		err = out.Close()
	}
	if err != nil {
		return err
	}

	if outfile == "" {
		if !startBrowser("file://" + out.Name()) {
			fmt.Fprintf(os.Stderr, "HTML output written to %s\n", out.Name())
		}
	}

	return nil
}

// percentCovered returns, as a percentage, the fraction of the statements in
// the profile covered by the test run.
// In effect, it reports the coverage of a given source file.
func percentCovered(p *Profile) float64 {
	var total, covered int64
	for _, b := range p.Blocks {
		total += int64(b.NumStmt)
		if b.Count > 0 {
			covered += int64(b.NumStmt)
		}
	}
	if total == 0 {
		return 0
	}
	return float64(covered) / float64(total) * 100
}

// htmlGen generates an HTML coverage report with the provided filename,
// source code, and tokens, and writes it to the given Writer.
func htmlGen(w io.Writer, src []byte, boundaries []Boundary) error {
	dst := bufio.NewWriter(w)
	for i := range src {
		for len(boundaries) > 0 && boundaries[0].Offset == i {
			b := boundaries[0]
			if b.Start {
				n := 0
				if b.Count > 0 {
					n = int(math.Floor(b.Norm*9)) + 1
				}
				fmt.Fprintf(dst, `<span class="cov%v" title="%v">`, n, b.Count)
			} else {
				dst.WriteString("")
			}
			boundaries = boundaries[1:]
		}
		switch b := src[i]; b {
		case '>':
			dst.WriteString(">")
		case '<':
			dst.WriteString("<")
		case '&':
			dst.WriteString("&")
		case '\t':
			dst.WriteString("        ")
		default:
			dst.WriteByte(b)
		}
	}
	return dst.Flush()
}

// startBrowser tries to open the URL in a browser
// and reports whether it succeeds.
func startBrowser(url string) bool {
	// try to start the browser
	var args []string
	switch runtime.GOOS {
	case "darwin":
		args = []string{"open"}
	case "windows":
		args = []string{"cmd", "/c", "start"}
	default:
		args = []string{"xdg-open"}
	}
	cmd := exec.Command(args[0], append(args[1:], url)...)
	return cmd.Start() == nil
}

// rgb returns an rgb value for the specified coverage value
// between 0 (no coverage) and 10 (max coverage).
func rgb(n int) string {
	if n == 0 {
		return "rgb(192, 0, 0)" // Red
	}
	// Gradient from gray to green.
	r := 128 - 12*(n-1)
	g := 128 + 12*(n-1)
	b := 128 + 3*(n-1)
	return fmt.Sprintf("rgb(%v, %v, %v)", r, g, b)
}

// colors generates the CSS rules for coverage colors.
func colors() template.CSS {
	var buf bytes.Buffer
	for i := 0; i < 11; i++ {
		fmt.Fprintf(&buf, ".cov%v { color: %v }\n", i, rgb(i))
	}
	return template.CSS(buf.String())
}

var htmlTemplate = template.Must(template.New("html").Funcs(template.FuncMap{
	"colors": colors,
}).Parse(tmplHTML))

type templateData struct {
	Files []*templateFile
	Set   bool
}

type templateFile struct {
	Name     string
	Body     template.HTML
	Coverage float64
}

const tmplHTML = `
<!DOCTYPE html>

		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

				<select id="files">
				{{range $i, $f := .Files}}
				<option value="file{{$i}}">{{$f.Name}} ({{printf "%.1f" $f.Coverage}}%)</option>
				{{end}}
				</select>

				not tracked
			{{if .Set}}
				not covered
				covered
			{{else}}
				no coverage
				low coverage
				*
				*
				*
				*
				*
				*
				*
				*
				high coverage
			{{end}}

		{{range $i, $f := .Files}}

{{$f.Body}}

		{{end}}

`
<commit_msg>cmd/cover: allow part selection to be retained across page refreshes<commit_after>// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"html/template"
	"io"
	"io/ioutil"
	"math"
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
)

// htmlOutput reads the profile data from profile and generates an HTML
// coverage report, writing it to outfile. If outfile is empty,
// it writes the report to a temporary file and opens it in a web browser.
func htmlOutput(profile, outfile string) error {
	profiles, err := ParseProfiles(profile)
	if err != nil {
		return err
	}

	var d templateData

	for _, profile := range profiles {
		fn := profile.FileName
		if profile.Mode == "set" {
			d.Set = true
		}
		file, err := findFile(fn)
		if err != nil {
			return err
		}
		src, err := ioutil.ReadFile(file)
		if err != nil {
			return fmt.Errorf("can't read %q: %v", fn, err)
		}
		var buf bytes.Buffer
		err = htmlGen(&buf, src, profile.Boundaries(src))
		if err != nil {
			return err
		}
		d.Files = append(d.Files, &templateFile{
			Name:     fn,
			Body:     template.HTML(buf.String()),
			Coverage: percentCovered(profile),
		})
	}

	var out *os.File
	if outfile == "" {
		var dir string
		dir, err = ioutil.TempDir("", "cover")
		if err != nil {
			return err
		}
		out, err = os.Create(filepath.Join(dir, "coverage.html"))
	} else {
		out, err = os.Create(outfile)
	}
	err = htmlTemplate.Execute(out, d)
	if err == nil {
		err = out.Close()
	}
	if err != nil {
		return err
	}

	if outfile == "" {
		if !startBrowser("file://" + out.Name()) {
			fmt.Fprintf(os.Stderr, "HTML output written to %s\n", out.Name())
		}
	}

	return nil
}

// percentCovered returns, as a percentage, the fraction of the statements in
// the profile covered by the test run.
// In effect, it reports the coverage of a given source file.
func percentCovered(p *Profile) float64 {
	var total, covered int64
	for _, b := range p.Blocks {
		total += int64(b.NumStmt)
		if b.Count > 0 {
			covered += int64(b.NumStmt)
		}
	}
	if total == 0 {
		return 0
	}
	return float64(covered) / float64(total) * 100
}

// htmlGen generates an HTML coverage report with the provided filename,
// source code, and tokens, and writes it to the given Writer.
func htmlGen(w io.Writer, src []byte, boundaries []Boundary) error {
	dst := bufio.NewWriter(w)
	for i := range src {
		for len(boundaries) > 0 && boundaries[0].Offset == i {
			b := boundaries[0]
			if b.Start {
				n := 0
				if b.Count > 0 {
					n = int(math.Floor(b.Norm*9)) + 1
				}
				fmt.Fprintf(dst, `<span class="cov%v" title="%v">`, n, b.Count)
			} else {
				dst.WriteString("")
			}
			boundaries = boundaries[1:]
		}
		switch b := src[i]; b {
		case '>':
			dst.WriteString(">")
		case '<':
			dst.WriteString("<")
		case '&':
			dst.WriteString("&")
		case '\t':
			dst.WriteString("        ")
		default:
			dst.WriteByte(b)
		}
	}
	return dst.Flush()
}

// startBrowser tries to open the URL in a browser
// and reports whether it succeeds.
func startBrowser(url string) bool {
	// try to start the browser
	var args []string
	switch runtime.GOOS {
	case "darwin":
		args = []string{"open"}
	case "windows":
		args = []string{"cmd", "/c", "start"}
	default:
		args = []string{"xdg-open"}
	}
	cmd := exec.Command(args[0], append(args[1:], url)...)
	return cmd.Start() == nil
}

// rgb returns an rgb value for the specified coverage value
// between 0 (no coverage) and 10 (max coverage).
func rgb(n int) string {
	if n == 0 {
		return "rgb(192, 0, 0)" // Red
	}
	// Gradient from gray to green.
	r := 128 - 12*(n-1)
	g := 128 + 12*(n-1)
	b := 128 + 3*(n-1)
	return fmt.Sprintf("rgb(%v, %v, %v)", r, g, b)
}

// colors generates the CSS rules for coverage colors.
func colors() template.CSS {
	var buf bytes.Buffer
	for i := 0; i < 11; i++ {
		fmt.Fprintf(&buf, ".cov%v { color: %v }\n", i, rgb(i))
	}
	return template.CSS(buf.String())
}

var htmlTemplate = template.Must(template.New("html").Funcs(template.FuncMap{
	"colors": colors,
}).Parse(tmplHTML))

type templateData struct {
	Files []*templateFile
	Set   bool
}

type templateFile struct {
	Name     string
	Body     template.HTML
	Coverage float64
}

const tmplHTML = `
<!DOCTYPE html>

		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

				<select id="files">
				{{range $i, $f := .Files}}
				<option value="file{{$i}}">{{$f.Name}} ({{printf "%.1f" $f.Coverage}}%)</option>
				{{end}}
				</select>

				not tracked
			{{if .Set}}
				not covered
				covered
			{{else}}
				no coverage
				low coverage
				*
				*
				*
				*
				*
				*
				*
				*
				high coverage
			{{end}}

		{{range $i, $f := .Files}}

{{$f.Body}}

		{{end}}

`
<|endoftext|>


<commit_before>package geom

type LineString struct {
	geom1
}

var _ T = &LineString{}

func NewLineString(layout Layout) *LineString {
	return NewLineStringFlat(layout, nil)
}

func NewLineStringFlat(layout Layout, flatCoords []float64) *LineString {
	ls := new(LineString)
	ls.layout = layout
	ls.stride = layout.Stride()
	ls.flatCoords = flatCoords
	return ls
}

func (ls *LineString) Area() float64 {
	return 0
}

func (ls *LineString) Clone() *LineString {
	flatCoords := make([]float64, len(ls.flatCoords))
	copy(flatCoords, ls.flatCoords)
	return NewLineStringFlat(ls.layout, flatCoords)
}

func (ls *LineString) Interpolate(val float64, dim int) (int, float64) {
	n := len(ls.flatCoords)
	if n == 0 {
		panic("geom: empty linestring")
	}
	if val <= ls.flatCoords[dim] {
		return 0, 0
	}
	if ls.flatCoords[n-ls.stride+dim] <= val {
		return (n - 1) / ls.stride, 0
	}
	low := 0
	high := n / ls.stride
	for low < high {
		mid := (low + high) / 2
		if val < ls.flatCoords[mid*ls.stride+dim] {
			high = mid
		} else {
			low = mid + 1
		}
	}
	low--
	val0 := ls.flatCoords[low*ls.stride+dim]
	if val == val0 {
		return low, 0
	}
	val1 := ls.flatCoords[(low+1)*ls.stride+dim]
	return low, (val - val0) / (val1 - val0)
}

func (ls *LineString) Length() float64 {
	return length1(ls.flatCoords, 0, len(ls.flatCoords), ls.stride)
}

func (ls *LineString) MustSetCoords(coords []Coord) *LineString {
	Must(ls.SetCoords(coords))
	return ls
}

func (ls *LineString) SetCoords(coords []Coord) (*LineString, error) {
	if err := ls.setCoords(coords); err != nil {
		return nil, err
	}
	return ls, nil
}

func (ls *LineString) SubLineString(start, stop int) *LineString {
	return NewLineStringFlat(ls.layout, ls.flatCoords[start*ls.stride:stop*ls.stride])
}
<commit_msg>Add initial LineString documentation<commit_after>package geom

// A LineString represents a single, unbroken line, linearly interpreted
// between zero or more control points.
type LineString struct {
	geom1
}

var _ T = &LineString{}

// NewLineString returns a new LineString with layout l and no control points.
func NewLineString(l Layout) *LineString {
	return NewLineStringFlat(l, nil)
}

// NewLineStringFlat returns a new LineString with layout l and control points
// flatCoords.
func NewLineStringFlat(layout Layout, flatCoords []float64) *LineString {
	ls := new(LineString)
	ls.layout = layout
	ls.stride = layout.Stride()
	ls.flatCoords = flatCoords
	return ls
}

// Area returns the length of ls, i.e. zero.
func (ls *LineString) Area() float64 {
	return 0
}

// Clone returns a copy of ls that does not alias ls.
func (ls *LineString) Clone() *LineString {
	flatCoords := make([]float64, len(ls.flatCoords))
	copy(flatCoords, ls.flatCoords)
	return NewLineStringFlat(ls.layout, flatCoords)
}

func (ls *LineString) Interpolate(val float64, dim int) (int, float64) {
	n := len(ls.flatCoords)
	if n == 0 {
		panic("geom: empty linestring")
	}
	if val <= ls.flatCoords[dim] {
		return 0, 0
	}
	if ls.flatCoords[n-ls.stride+dim] <= val {
		return (n - 1) / ls.stride, 0
	}
	low := 0
	high := n / ls.stride
	for low < high {
		mid := (low + high) / 2
		if val < ls.flatCoords[mid*ls.stride+dim] {
			high = mid
		} else {
			low = mid + 1
		}
	}
	low--
	val0 := ls.flatCoords[low*ls.stride+dim]
	if val == val0 {
		return low, 0
	}
	val1 := ls.flatCoords[(low+1)*ls.stride+dim]
	return low, (val - val0) / (val1 - val0)
}

// Length returns the length of ls.
func (ls *LineString) Length() float64 {
	return length1(ls.flatCoords, 0, len(ls.flatCoords), ls.stride)
}

// MustSetCoords is like SetCoords but it panics on any error.
func (ls *LineString) MustSetCoords(coords []Coord) *LineString {
	Must(ls.SetCoords(coords))
	return ls
}

// SetCoords sets the coordinates of ls.
func (ls *LineString) SetCoords(coords []Coord) (*LineString, error) {
	if err := ls.setCoords(coords); err != nil {
		return nil, err
	}
	return ls, nil
}

// SubLineString returns a LineString from starts at index start and stops at
// index stop of ls. The returned LineString aliases ls.
func (ls *LineString) SubLineString(start, stop int) *LineString {
	return NewLineStringFlat(ls.layout, ls.flatCoords[start*ls.stride:stop*ls.stride])
}
<|endoftext|>


<commit_before>package main

import (
	"path/filepath"
	"fmt"
	"os"
	"os/user"
	"strings"
)

func FileListing(endPattern string) []map[string]string {
	var paths []map[string]string

	filepath.Walk(
		".",
		func(path string, info os.FileInfo, err error) error {
			if strings.HasSuffix(path, endPattern) {
				dirname        := filepath.Dir(path)
				linked, _      := os.Readlink(path)
				linked          = strings.TrimSuffix(linked, ".placeholder")

				oldBase        := filepath.Join(dirname, linked)
				oldPath, _     := filepath.Abs(oldBase)

				newBase        := strings.Replace(path, "locations/", "", 1)
				newBase         = strings.TrimSuffix(newBase, endPattern)

				currentUser, _ := user.Current()
				newPath, _ := filepath.Abs(
					filepath.Join(
						currentUser.HomeDir,
						"." + newBase,
					),
				)

				currentPath := make(map[string]string)

				currentPath["old_path"] = oldPath
				currentPath["new_path"] = newPath
				paths = append(paths, currentPath)
			}
			return nil
		},
	)

	return paths
}

func DirectoryListing() []map[string]string {
	return FileListing(".directory.symlink")
}

func LocalsListing() []map[string]string {
	var files []map[string]string
	allFiles := FileListing(".symlink")
	for i := range allFiles {
		if strings.HasSuffix(allFiles[i]["new_path"], ".local") {
			files = append(files, allFiles[i])
		}
	}

	return files
}

func DotfileListing() []map[string]string {
	var files []map[string]string
	allFiles := FileListing(".symlink")
	for i := range allFiles {
		if !strings.HasSuffix(allFiles[i]["new_path"], ".directory") &&
			!strings.HasSuffix(allFiles[i]["new_path"], ".local") {
			files = append(files, allFiles[i])
		}
	}

	return files
}

func main() {
	files := FileListing(".symlink")
	for i := range files {
		fmt.Printf(files[i]["old_path"])
		fmt.Printf(" | ")
		fmt.Println(files[i]["new_path"])
	}
}
<commit_msg>gofmt<commit_after>package main

import (
	"fmt"
	"os"
	"os/user"
	"path/filepath"
	"strings"
)

func FileListing(endPattern string) []map[string]string {
	var paths []map[string]string

	filepath.Walk(
		".",
		func(path string, info os.FileInfo, err error) error {
			if strings.HasSuffix(path, endPattern) {
				dirname := filepath.Dir(path)
				linked, _ := os.Readlink(path)
				linked = strings.TrimSuffix(linked, ".placeholder")

				oldBase := filepath.Join(dirname, linked)
				oldPath, _ := filepath.Abs(oldBase)

				newBase := strings.Replace(path, "locations/", "", 1)
				newBase = strings.TrimSuffix(newBase, endPattern)

				currentUser, _ := user.Current()
				newPath, _ := filepath.Abs(
					filepath.Join(
						currentUser.HomeDir,
						"."+newBase,
					),
				)

				currentPath := make(map[string]string)

				currentPath["old_path"] = oldPath
				currentPath["new_path"] = newPath
				paths = append(paths, currentPath)
			}
			return nil
		},
	)

	return paths
}

func DirectoryListing() []map[string]string {
	return FileListing(".directory.symlink")
}

func LocalsListing() []map[string]string {
	var files []map[string]string
	allFiles := FileListing(".symlink")
	for i := range allFiles {
		if strings.HasSuffix(allFiles[i]["new_path"], ".local") {
			files = append(files, allFiles[i])
		}
	}

	return files
}

func DotfileListing() []map[string]string {
	var files []map[string]string
	allFiles := FileListing(".symlink")
	for i := range allFiles {
		if !strings.HasSuffix(allFiles[i]["new_path"], ".directory") &&
			!strings.HasSuffix(allFiles[i]["new_path"], ".local") {
			files = append(files, allFiles[i])
		}
	}

	return files
}

func main() {
	files := FileListing(".symlink")
	for i := range files {
		fmt.Printf(files[i]["old_path"])
		fmt.Printf(" | ")
		fmt.Println(files[i]["new_path"])
	}
}
<|endoftext|>


<commit_before>package cml

import (
	"os"
	"path/filepath"
	"testing"

	"github.com/seiflotfy/skizze/config"
	"github.com/seiflotfy/skizze/counters/abstract"
	"github.com/seiflotfy/skizze/utils"
)

func setupTests() {
	os.Setenv("SKZ_DATA_DIR", "/tmp/skizze_data")
	os.Setenv("SKZ_INFO_DIR", "/tmp/skizze_info")
	path, err := os.Getwd()
	utils.PanicOnError(err)
	path = filepath.Dir(path)
	configPath := filepath.Join(path, "../../../config/default.toml")
	os.Setenv("SKZ_CONFIG", configPath)
	tearDownTests()
}

func tearDownTests() {
	os.RemoveAll(config.GetConfig().GetDataDir())
	os.RemoveAll(config.GetConfig().GetInfoDir())
	os.Mkdir(config.GetConfig().GetDataDir(), 0777)
	os.Mkdir(config.GetConfig().GetInfoDir(), 0777)
}

// Ensures that Add adds to the set and Count returns the correct
// approximation.
func TestLog16AddAndCount(t *testing.T) {
	setupTests()
	defer tearDownTests()
	info := &abstract.Info{ID: "ultimates",
		Type:     abstract.Frequency,
		Capacity: 1000000, State: make(map[string]uint64)}

	log, _ := NewForCapacity16(info, 1000, 0.01)

	log.IncreaseCount([]byte("a"))
	log.IncreaseCount([]byte("b"))
	log.IncreaseCount([]byte("c"))
	log.IncreaseCount([]byte("b"))
	log.IncreaseCount([]byte("d"))
	log.IncreaseCount([]byte("a"))
	log.IncreaseCount([]byte("a"))
	if count := log.GetCount([]byte("a")); uint(count) != 3 {
		t.Errorf("expected 3, got %d", uint(count))
	}

	if count := log.GetCount([]byte("b")); uint(count) != 2 {
		t.Errorf("expected 2, got %d", uint(count))
	}

	if count := log.GetCount([]byte("c")); uint(count) != 1 {
		t.Errorf("expected 1, got %d", uint(count))
	}

	if count := log.GetCount([]byte("d")); uint(count) != 1 {
		t.Errorf("expected 1, got %d", uint(count))
	}

	if count := log.GetCount([]byte("x")); uint(count) != 0 {
		t.Errorf("expected 0, got %d", uint(count))
	}
}

// Ensures that Reset restores the sketch to its original state.
func TestLog16Reset(t *testing.T) {
	setupTests()
	defer tearDownTests()
	info := &abstract.Info{ID: "ultimates",
		Type:     abstract.Frequency,
		Capacity: 1000000, State: make(map[string]uint64)}
	log, _ := NewForCapacity16(info, 1000, 0.001)
	log.IncreaseCount([]byte("a"))
	log.IncreaseCount([]byte("b"))
	log.IncreaseCount([]byte("c"))
	log.IncreaseCount([]byte("b"))
	log.IncreaseCount([]byte("d"))
	log.IncreaseCount([]byte("a"))
	log.IncreaseCount([]byte("a"))

	log.Reset()

	for i := uint(0); i < log.k; i++ {
		for j := uint(0); j < log.w; j++ {
			if x, _ := log.store.get(i, j); x != 0 {
				t.Errorf("expected matrix to be completely empty, got %d", x)
			}
		}
	}
}
<commit_msg>Fix compilation error<commit_after>package cml

import (
	"os"
	"path/filepath"
	"testing"

	"github.com/seiflotfy/skizze/config"
	"github.com/seiflotfy/skizze/counters/abstract"
	"github.com/seiflotfy/skizze/utils"
)

func setupTests() {
	os.Setenv("SKZ_DATA_DIR", "/tmp/skizze_data")
	os.Setenv("SKZ_INFO_DIR", "/tmp/skizze_info")
	path, err := os.Getwd()
	utils.PanicOnError(err)
	path = filepath.Dir(path)
	configPath := filepath.Join(path, "../../../config/default.toml")
	os.Setenv("SKZ_CONFIG", configPath)
	tearDownTests()
}

func tearDownTests() {
	os.RemoveAll(config.GetConfig().GetDataDir())
	os.RemoveAll(config.GetConfig().GetInfoDir())
	os.Mkdir(config.GetConfig().GetDataDir(), 0777)
	os.Mkdir(config.GetConfig().GetInfoDir(), 0777)
}

// Ensures that Add adds to the set and Count returns the correct
// approximation.
func TestLog16AddAndCount(t *testing.T) {
	setupTests()
	defer tearDownTests()
	info := &abstract.Info{ID: "ultimates",
		Type:     abstract.CML,
		Capacity: 1000000, State: make(map[string]uint64)}

	log, _ := NewForCapacity16(info, 1000, 0.01)

	log.IncreaseCount([]byte("a"))
	log.IncreaseCount([]byte("b"))
	log.IncreaseCount([]byte("c"))
	log.IncreaseCount([]byte("b"))
	log.IncreaseCount([]byte("d"))
	log.IncreaseCount([]byte("a"))
	log.IncreaseCount([]byte("a"))
	if count := log.GetCount([]byte("a")); uint(count) != 3 {
		t.Errorf("expected 3, got %d", uint(count))
	}

	if count := log.GetCount([]byte("b")); uint(count) != 2 {
		t.Errorf("expected 2, got %d", uint(count))
	}

	if count := log.GetCount([]byte("c")); uint(count) != 1 {
		t.Errorf("expected 1, got %d", uint(count))
	}

	if count := log.GetCount([]byte("d")); uint(count) != 1 {
		t.Errorf("expected 1, got %d", uint(count))
	}

	if count := log.GetCount([]byte("x")); uint(count) != 0 {
		t.Errorf("expected 0, got %d", uint(count))
	}
}

// Ensures that Reset restores the sketch to its original state.
func TestLog16Reset(t *testing.T) {
	setupTests()
	defer tearDownTests()
	info := &abstract.Info{ID: "ultimates",
		Type:     abstract.CML,
		Capacity: 1000000, State: make(map[string]uint64)}
	log, _ := NewForCapacity16(info, 1000, 0.001)
	log.IncreaseCount([]byte("a"))
	log.IncreaseCount([]byte("b"))
	log.IncreaseCount([]byte("c"))
	log.IncreaseCount([]byte("b"))
	log.IncreaseCount([]byte("d"))
	log.IncreaseCount([]byte("a"))
	log.IncreaseCount([]byte("a"))

	log.Reset()

	for i := uint(0); i < log.k; i++ {
		for j := uint(0); j < log.w; j++ {
			if x, _ := log.store.get(i, j); x != 0 {
				t.Errorf("expected matrix to be completely empty, got %d", x)
			}
		}
	}
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"os"
	"os/user"
	"path/filepath"
	"strings"
)

func IsFile(path string) (bool, error) {
	stat, err := os.Stat(path)
	if err != nil {
		return false, err
	} else if stat.IsDir() {
		return false, nil
	} else {
		return true, nil
	}
}

func IsSymlink(path string) (bool, error) {
	stat, err := os.Readlink(path)
	if err != nil {
		return false, err
	} else {
		return true, nil
	}
}

func IsDir(path string) (bool, error) {
	stat, err := os.Stat(path)
	if err != nil {
		return false, err
	} else if stat.IsDir() {
		return true, nil
	} else {
		return false, nil
	}
}

func FileListing(endPattern string) []map[string]string {
	var paths []map[string]string

	filepath.Walk(
		".",
		func(path string, info os.FileInfo, err error) error {
			if strings.HasSuffix(path, endPattern) {
				dirname := filepath.Dir(path)
				linked, _ := os.Readlink(path)
				linked = strings.TrimSuffix(linked, ".placeholder")

				oldBase := filepath.Join(dirname, linked)
				oldPath, _ := filepath.Abs(oldBase)

				newBase := strings.Replace(path, "locations/", "", 1)
				newBase = strings.TrimSuffix(newBase, endPattern)

				currentUser, _ := user.Current()
				newPath, _ := filepath.Abs(
					filepath.Join(
						currentUser.HomeDir,
						"."+newBase,
					),
				)

				currentPath := make(map[string]string)

				currentPath["old_path"] = oldPath
				currentPath["new_path"] = newPath
				paths = append(paths, currentPath)
			}
			return nil
		},
	)

	return paths
}

func DirectoryListing() []map[string]string {
	return FileListing(".directory.symlink")
}

func LocalsListing() []map[string]string {
	var files []map[string]string
	allFiles := FileListing(".symlink")
	for i := range allFiles {
		if strings.HasSuffix(allFiles[i]["new_path"], ".local") {
			files = append(files, allFiles[i])
		}
	}

	return files
}

func DotfileListing() []map[string]string {
	var files []map[string]string
	allFiles := FileListing(".symlink")
	for i := range allFiles {
		if !strings.HasSuffix(allFiles[i]["new_path"], ".directory") &&
			!strings.HasSuffix(allFiles[i]["new_path"], ".local") {
			files = append(files, allFiles[i])
		}
	}

	return files
}

func CurrentFile(f map[string]string) map[string]string {
	_, newPath := f["old_path"], f["new_path"]
	if d, _ := IsDir(newPath); d {
		return map[string]string{
			"type": "directory",
			"path": newPath,
			"old_path": newPath,
			"new_path": newPath,
		}
	} else if d, _ := IsSymlink(newPath); d {
		linked, _ := os.Readlink(newPath)
		return map[string]string{
			"type": "symlink",
			"path": newPath,
			"old_path": linked,
			"new_path": newPath,
		}
	} else {
		return map[string]string{
			"type": "file",
			"path": newPath,
			"old_path": newPath,
			"new_path": newPath,
		}
	}
}

func MaybeOverwriteSymlink(oldLink, newLink, noInput) {
	if noInput {
		fmt.Println(
			"Warn!  | Overwrite | path: " +
			newLink["new_path"] + ", " +
			"old: " + oldLink["old_path"] + ", " +
			"new: " + newLink["old_path"],
		)
	}
}

func main() {
	IsSymlink("kdljaflksd.go")
	IsSymlink("locations/bash_profile.symlink")
	files := FileListing(".symlink")
	for i := range files {
		fmt.Printf(files[i]["old_path"])
		fmt.Printf(" | ")
		fmt.Println(files[i]["new_path"])
	}
}
<commit_msg>gofmt<commit_after>package main

import (
	"fmt"
	"os"
	"os/user"
	"path/filepath"
	"strings"
)

func IsFile(path string) (bool, error) {
	stat, err := os.Stat(path)
	if err != nil {
		return false, err
	} else if stat.IsDir() {
		return false, nil
	} else {
		return true, nil
	}
}

func IsSymlink(path string) (bool, error) {
	stat, err := os.Readlink(path)
	if err != nil {
		return false, err
	} else {
		return true, nil
	}
}

func IsDir(path string) (bool, error) {
	stat, err := os.Stat(path)
	if err != nil {
		return false, err
	} else if stat.IsDir() {
		return true, nil
	} else {
		return false, nil
	}
}

func FileListing(endPattern string) []map[string]string {
	var paths []map[string]string

	filepath.Walk(
		".",
		func(path string, info os.FileInfo, err error) error {
			if strings.HasSuffix(path, endPattern) {
				dirname := filepath.Dir(path)
				linked, _ := os.Readlink(path)
				linked = strings.TrimSuffix(linked, ".placeholder")

				oldBase := filepath.Join(dirname, linked)
				oldPath, _ := filepath.Abs(oldBase)

				newBase := strings.Replace(path, "locations/", "", 1)
				newBase = strings.TrimSuffix(newBase, endPattern)

				currentUser, _ := user.Current()
				newPath, _ := filepath.Abs(
					filepath.Join(
						currentUser.HomeDir,
						"."+newBase,
					),
				)

				currentPath := make(map[string]string)

				currentPath["old_path"] = oldPath
				currentPath["new_path"] = newPath
				paths = append(paths, currentPath)
			}
			return nil
		},
	)

	return paths
}

func DirectoryListing() []map[string]string {
	return FileListing(".directory.symlink")
}

func LocalsListing() []map[string]string {
	var files []map[string]string
	allFiles := FileListing(".symlink")
	for i := range allFiles {
		if strings.HasSuffix(allFiles[i]["new_path"], ".local") {
			files = append(files, allFiles[i])
		}
	}

	return files
}

func DotfileListing() []map[string]string {
	var files []map[string]string
	allFiles := FileListing(".symlink")
	for i := range allFiles {
		if !strings.HasSuffix(allFiles[i]["new_path"], ".directory") &&
			!strings.HasSuffix(allFiles[i]["new_path"], ".local") {
			files = append(files, allFiles[i])
		}
	}

	return files
}

func CurrentFile(f map[string]string) map[string]string {
	_, newPath := f["old_path"], f["new_path"]
	if d, _ := IsDir(newPath); d {
		return map[string]string{
			"type":     "directory",
			"path":     newPath,
			"old_path": newPath,
			"new_path": newPath,
		}
	} else if d, _ := IsSymlink(newPath); d {
		linked, _ := os.Readlink(newPath)
		return map[string]string{
			"type":     "symlink",
			"path":     newPath,
			"old_path": linked,
			"new_path": newPath,
		}
	} else {
		return map[string]string{
			"type":     "file",
			"path":     newPath,
			"old_path": newPath,
			"new_path": newPath,
		}
	}
}

func MaybeOverwriteSymlink(oldLink, newLink, noInput) {
	if noInput {
		fmt.Println(
			"Warn!  | Overwrite | path: " +
				newLink["new_path"] + ", " +
				"old: " + oldLink["old_path"] + ", " +
				"new: " + newLink["old_path"],
		)
	}
}

func main() {
	IsSymlink("kdljaflksd.go")
	IsSymlink("locations/bash_profile.symlink")
	files := FileListing(".symlink")
	for i := range files {
		fmt.Printf(files[i]["old_path"])
		fmt.Printf(" | ")
		fmt.Println(files[i]["new_path"])
	}
}
<|endoftext|>


<commit_before>package workerpool_test

import (
	"errors"
	"runtime"
	"sync/atomic"
	"testing"
	"time"

	"github.com/dc0d/workerpool"
	"github.com/stretchr/testify/assert"
)

var (
	ErrTimeout = errors.New(`TIMEOUT`)
)

func waitFunc(f func(), exitDelay time.Duration) error {
	funcDone := make(chan struct{})
	go func() {
		defer close(funcDone)
		f()
	}()

	if exitDelay <= 0 {
		<-funcDone

		return nil
	}

	select {
	case <-time.After(exitDelay):
		return ErrTimeout
	case <-funcDone:
	}

	return nil
}

const (
	_timeout = time.Second * 5
)

func TestNegWorkers(t *testing.T) {
	assert := assert.New(t)
	pool := workerpool.New(-1)

	quit := make(chan struct{})

	n := int64(runtime.NumCPU())
	var backSlot int64
	var job = func() {
		atomic.AddInt64(&backSlot, 1)
		select {
		case <-quit:
		}
	}
	for pool.Queue(job, time.Millisecond*50) {
	}

	actual := atomic.LoadInt64(&backSlot)
	assert.Equal(n, actual)

	close(quit)

	assert.NoError(waitFunc(pool.Stop, _timeout))
}

func TestZeroWorkers(t *testing.T) {
	assert := assert.New(t)
	pool := workerpool.New(0)

	var backSlot int64 = 10
	var job = func() {
		atomic.StoreInt64(&backSlot, 110)
	}
	pool.Queue(job)

	assert.Equal(int64(10), atomic.LoadInt64(&backSlot))

	pool.Expand(1, 0, nil)

	done := make(chan bool)
	job = func() {
		defer close(done)
		atomic.StoreInt64(&backSlot, 73)
	}
	assert.True(pool.Queue(job, time.Millisecond*50))
	<-done

	assert.Equal(int64(73), atomic.LoadInt64(&backSlot))

	assert.NoError(waitFunc(pool.Stop, _timeout))
}

func TestAbsoluteTimeout(t *testing.T) {
	assert := assert.New(t)
	initialWorkers := 1
	extraWorkers := 10

	pool := workerpool.New(initialWorkers, 2)

	quit1 := make(chan struct{})
	pool.Expand(extraWorkers, 0, quit1)

	done := make(chan bool)
	absoluteTimeout := func() {
		defer close(done)
		<-time.After(time.Millisecond * 100)
		close(quit1)
	}

	go absoluteTimeout()
	<-done
	<-time.After(time.Millisecond * 400)

	assert.NoError(waitFunc(pool.Stop, _timeout))
}

func TestTimeout(t *testing.T) {
	assert := assert.New(t)
	initialWorkers := 1
	extraWorkers := 10

	pool := workerpool.New(initialWorkers, 2)

	pool.Expand(extraWorkers, time.Millisecond*10, nil)

	assert.NoError(waitFunc(pool.Stop, _timeout))
}

func TestQuit(t *testing.T) {
	assert := assert.New(t)
	initialWorkers := 1
	extraWorkers := 10

	pool := workerpool.New(initialWorkers, 2)

	quit1 := make(chan struct{})
	pool.Expand(extraWorkers, 0, quit1)
	close(quit1)

	assert.NoError(waitFunc(pool.Stop, _timeout))
}

func TestWorkerPoolQuit(t *testing.T) {
	assert := assert.New(t)
	initialWorkers := 10
	extraWorkers := 10

	pool := workerpool.New(initialWorkers, 2)

	pool.Expand(extraWorkers, 0, nil)

	assert.NoError(waitFunc(pool.Stop, _timeout))
}

func TestTimeoutNoGoroutineLeak(t *testing.T) {
	initialWorkers := 10
	extraWorkers := 1000
	assert := assert.New(t)

	pool := workerpool.New(initialWorkers, 2)

	before := runtime.NumGoroutine()

	pool.Expand(extraWorkers, time.Millisecond*100, nil)

	go func() { // A
		for i := 0; i < extraWorkers; i++ {
			assert.True(pool.Queue(func() {
				time.Sleep(time.Millisecond * 10)
			}))
		}
	}()

	<-time.After(time.Millisecond * 500)
	go func() {
		for i := 0; i < initialWorkers*2; i++ {
			assert.True(pool.Queue(func() {
				time.Sleep(time.Millisecond * 10)
			}))
		}
	}()

	<-time.After(time.Millisecond * 500)
	after := runtime.NumGoroutine()
	if (after - before) > (initialWorkers * 2) {
		t.Fatal()
	}

	assert.NoError(waitFunc(pool.Stop, _timeout))
}

func ExampleWorkerPool() {
	pool := workerpool.New(-1)

	var v int64
	go func() {
		for i := 0; i < 100; i++ {
			pool.Queue(func() {
				atomic.AddInt64(&v, 1)
			})
		}
	}()

	pool.Stop()

	if v != 100 {
		panic("BOOM!")
	}
}

func ExampleWorkerPool_Expand() {
	pool := workerpool.New(-1)
	pool.Expand(1000, time.Millisecond, nil)

	var v int64
	go func() {
		for i := 0; i < 100; i++ {
			pool.Queue(func() {
				atomic.AddInt64(&v, 1)
			})
		}
	}()

	pool.Stop()

	if v != 100 {
		panic("BOOM!")
	}
}
<commit_msg>remove extra select from test<commit_after>package workerpool_test

import (
	"errors"
	"runtime"
	"sync/atomic"
	"testing"
	"time"

	"github.com/dc0d/workerpool"
	"github.com/stretchr/testify/assert"
)

var (
	ErrTimeout = errors.New(`TIMEOUT`)
)

func waitFunc(f func(), exitDelay time.Duration) error {
	funcDone := make(chan struct{})
	go func() {
		defer close(funcDone)
		f()
	}()

	if exitDelay <= 0 {
		<-funcDone

		return nil
	}

	select {
	case <-time.After(exitDelay):
		return ErrTimeout
	case <-funcDone:
	}

	return nil
}

const (
	_timeout = time.Second * 5
)

func TestNegWorkers(t *testing.T) {
	assert := assert.New(t)
	pool := workerpool.New(-1)

	quit := make(chan struct{})

	n := int64(runtime.NumCPU())
	var backSlot int64
	var job = func() {
		atomic.AddInt64(&backSlot, 1)
		<-quit
	}
	for pool.Queue(job, time.Millisecond*50) {
	}

	actual := atomic.LoadInt64(&backSlot)
	assert.Equal(n, actual)

	close(quit)

	assert.NoError(waitFunc(pool.Stop, _timeout))
}

func TestZeroWorkers(t *testing.T) {
	assert := assert.New(t)
	pool := workerpool.New(0)

	var backSlot int64 = 10
	var job = func() {
		atomic.StoreInt64(&backSlot, 110)
	}
	pool.Queue(job)

	assert.Equal(int64(10), atomic.LoadInt64(&backSlot))

	pool.Expand(1, 0, nil)

	done := make(chan bool)
	job = func() {
		defer close(done)
		atomic.StoreInt64(&backSlot, 73)
	}
	assert.True(pool.Queue(job, time.Millisecond*50))
	<-done

	assert.Equal(int64(73), atomic.LoadInt64(&backSlot))

	assert.NoError(waitFunc(pool.Stop, _timeout))
}

func TestAbsoluteTimeout(t *testing.T) {
	assert := assert.New(t)
	initialWorkers := 1
	extraWorkers := 10

	pool := workerpool.New(initialWorkers, 2)

	quit1 := make(chan struct{})
	pool.Expand(extraWorkers, 0, quit1)

	done := make(chan bool)
	absoluteTimeout := func() {
		defer close(done)
		<-time.After(time.Millisecond * 100)
		close(quit1)
	}

	go absoluteTimeout()
	<-done
	<-time.After(time.Millisecond * 400)

	assert.NoError(waitFunc(pool.Stop, _timeout))
}

func TestTimeout(t *testing.T) {
	assert := assert.New(t)
	initialWorkers := 1
	extraWorkers := 10

	pool := workerpool.New(initialWorkers, 2)

	pool.Expand(extraWorkers, time.Millisecond*10, nil)

	assert.NoError(waitFunc(pool.Stop, _timeout))
}

func TestQuit(t *testing.T) {
	assert := assert.New(t)
	initialWorkers := 1
	extraWorkers := 10

	pool := workerpool.New(initialWorkers, 2)

	quit1 := make(chan struct{})
	pool.Expand(extraWorkers, 0, quit1)
	close(quit1)

	assert.NoError(waitFunc(pool.Stop, _timeout))
}

func TestWorkerPoolQuit(t *testing.T) {
	assert := assert.New(t)
	initialWorkers := 10
	extraWorkers := 10

	pool := workerpool.New(initialWorkers, 2)

	pool.Expand(extraWorkers, 0, nil)

	assert.NoError(waitFunc(pool.Stop, _timeout))
}

func TestTimeoutNoGoroutineLeak(t *testing.T) {
	initialWorkers := 10
	extraWorkers := 1000
	assert := assert.New(t)

	pool := workerpool.New(initialWorkers, 2)

	before := runtime.NumGoroutine()

	pool.Expand(extraWorkers, time.Millisecond*100, nil)

	go func() { // A
		for i := 0; i < extraWorkers; i++ {
			assert.True(pool.Queue(func() {
				time.Sleep(time.Millisecond * 10)
			}))
		}
	}()

	<-time.After(time.Millisecond * 500)
	go func() {
		for i := 0; i < initialWorkers*2; i++ {
			assert.True(pool.Queue(func() {
				time.Sleep(time.Millisecond * 10)
			}))
		}
	}()

	<-time.After(time.Millisecond * 500)
	after := runtime.NumGoroutine()
	if (after - before) > (initialWorkers * 2) {
		t.Fatal()
	}

	assert.NoError(waitFunc(pool.Stop, _timeout))
}

func ExampleWorkerPool() {
	pool := workerpool.New(-1)

	var v int64
	go func() {
		for i := 0; i < 100; i++ {
			pool.Queue(func() {
				atomic.AddInt64(&v, 1)
			})
		}
	}()

	pool.Stop()

	if v != 100 {
		panic("BOOM!")
	}
}

func ExampleWorkerPool_Expand() {
	pool := workerpool.New(-1)
	pool.Expand(1000, time.Millisecond, nil)

	var v int64
	go func() {
		for i := 0; i < 100; i++ {
			pool.Queue(func() {
				atomic.AddInt64(&v, 1)
			})
		}
	}()

	pool.Stop()

	if v != 100 {
		panic("BOOM!")
	}
}
<|endoftext|>


<commit_before>package local

import (
	"encoding/json"
	"io/ioutil"
	"log"
	"os"
	"path/filepath"
	"sync/atomic"
	"time"

	"github.com/altairsix/pkg/context"
	"github.com/altairsix/pkg/types"
	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/dynamodb"
	"github.com/aws/aws-sdk-go/service/sns"
	"github.com/aws/aws-sdk-go/service/sqs"
)

const (
	Region = "us-west-2"
	Env    = "local"
)

var (
	Context  context.Kontext = context.Background(Env)
	DynamoDB *dynamodb.DynamoDB
	SNS      *sns.SNS
	SQS      *sqs.SQS
)

var (
	IDFactory types.IDFactory
)

func init() {
	dir, err := filepath.Abs(".")
	if err != nil {
		log.Fatalln(err)
	}

	env := map[string]string{}
	for i := 0; i < 5; i++ {
		dir = filepath.Join(dir, "..")
		filename := filepath.Join(dir, "test.json")
		data, err := ioutil.ReadFile(filename)
		if err != nil {
			continue
		}

		err = json.Unmarshal(data, &env)
		if err != nil {
			log.Fatalln(err)
		}
		break
	}

	for k, v := range env {
		os.Setenv(k, v)
	}

	region := os.Getenv("AWS_DEFAULT_REGION")
	if region == "" {
		region = "us-west-2"
	}

	cfg := &aws.Config{Region: aws.String(region)}
	s, err := session.NewSession(cfg)
	if err != nil {
		log.Fatalln(err)
	}

	DynamoDB = dynamodb.New(s)
	SNS = sns.New(s)
	SQS = sqs.New(s)

	id := time.Now().UnixNano()
	IDFactory = func() types.ID {
		atomic.AddInt64(&id, 1)
		return types.ID(id)
	}
}

func NewID() types.ID {
	return IDFactory.NewID()
}

func NewKey() types.Key {
	return IDFactory.NewKey()
}
<commit_msg>- added session to local properties<commit_after>package local

import (
	"encoding/json"
	"io/ioutil"
	"log"
	"os"
	"path/filepath"
	"sync/atomic"
	"time"

	"github.com/altairsix/pkg/context"
	"github.com/altairsix/pkg/types"
	"github.com/altairsix/pkg/web/session"
	"github.com/aws/aws-sdk-go/aws"
	aws_session "github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/dynamodb"
	"github.com/aws/aws-sdk-go/service/sns"
	"github.com/aws/aws-sdk-go/service/sqs"
	"github.com/gorilla/securecookie"
	"github.com/gorilla/sessions"
)

const (
	Region = "us-west-2"
	Env    = "local"
)

var (
	Context  context.Kontext = context.Background(Env)
	DynamoDB *dynamodb.DynamoDB
	SNS      *sns.SNS
	SQS      *sqs.SQS
)

var (
	SessionStore sessions.Store
)

var (
	IDFactory types.IDFactory
)

func init() {
	// Read Env
	//
	dir, err := filepath.Abs(".")
	if err != nil {
		log.Fatalln(err)
	}

	env := map[string]string{}
	for i := 0; i < 5; i++ {
		dir = filepath.Join(dir, "..")
		filename := filepath.Join(dir, "test.json")
		data, err := ioutil.ReadFile(filename)
		if err != nil {
			continue
		}

		err = json.Unmarshal(data, &env)
		if err != nil {
			log.Fatalln(err)
		}
		break
	}

	for k, v := range env {
		os.Setenv(k, v)
	}

	region := os.Getenv("AWS_DEFAULT_REGION")
	if region == "" {
		region = "us-west-2"
	}

	// Configure AWS
	//
	cfg := &aws.Config{Region: aws.String(region)}
	s, err := aws_session.NewSession(cfg)
	if err != nil {
		log.Fatalln(err)
	}

	DynamoDB = dynamodb.New(s)
	SNS = sns.New(s)
	SQS = sqs.New(s)

	// Setup IDFactory
	//
	id := time.Now().UnixNano()
	IDFactory = func() types.ID {
		atomic.AddInt64(&id, 1)
		return types.ID(id)
	}

	// SessionStore
	//
	codecs, err := session.EnvCodecs()
	if err != nil {
		hashKey := securecookie.GenerateRandomKey(64)
		blockKey := securecookie.GenerateRandomKey(32)

		codecs = []securecookie.Codec{securecookie.New(hashKey, blockKey)}
	}

	SessionStore = &sessions.CookieStore{
		Codecs: codecs,
		Options: &sessions.Options{
			Path:   "/",
			MaxAge: 86400 * 30,
		},
	}
}

func NewID() types.ID {
	return IDFactory.NewID()
}

func NewKey() types.Key {
	return IDFactory.NewKey()
}
<|endoftext|>


<commit_before>package geolookup

import (
	"fmt"
	"net"
	"net/http"
	"testing"

	"github.com/getlantern/testify/assert"
)

func TestCityLookup(t *testing.T) {
	city, err := LookupIPWithClient("198.199.72.101", nil)
	if assert.NoError(t, err) {
		assert.Equal(t, "New York", city.City.Names["en"])
	}
}

func TestNonDefaultClient(t *testing.T) {
	// Set up a client that will fail
	client := &http.Client{
		Transport: &http.Transport{
			Dial: func(network, addr string) (net.Conn, error) {
				return nil, fmt.Errorf("Failing intentionally")
			},
		},
	}

	_, err := LookupIPWithClient("", client)
	assert.Error(t, err, "Using bad client should have resulted in error")
}
<commit_msg>fixed test<commit_after>package geolookup

import (
	"fmt"
	"net"
	"net/http"
	"testing"

	"github.com/getlantern/testify/assert"
)

func TestCityLookup(t *testing.T) {
	city, _, err := LookupIPWithClient("198.199.72.101", nil)
	if assert.NoError(t, err) {
		assert.Equal(t, "New York", city.City.Names["en"])
	}
}

func TestNonDefaultClient(t *testing.T) {
	// Set up a client that will fail
	client := &http.Client{
		Transport: &http.Transport{
			Dial: func(network, addr string) (net.Conn, error) {
				return nil, fmt.Errorf("Failing intentionally")
			},
		},
	}

	_, _, err := LookupIPWithClient("", client)
	assert.Error(t, err, "Using bad client should have resulted in error")
}
<|endoftext|>