<commit_before>package main

import (
	"go/build"
	"io/ioutil"
	"os"
	"path/filepath"
	"sort"
	"strings"

	"github.com/LK4D4/vndr/godl"
)

func isCDir(path string) bool {
	fis, err := ioutil.ReadDir(path)
	if err != nil {
		return false
	}
	var hFound bool
	for _, fi := range fis {
		ext := filepath.Ext(fi.Name())
		if ext == ".cc" || ext == ".cpp" || ext == ".py" {
			return false
		}
		if ext == ".h" {
			hFound = true
		}
	}
	return hFound
}

func isGoFile(path string) bool {
	ext := filepath.Ext(path)
	return ext == ".go" || ext == ".c" || ext == ".h" || ext == ".s" || ext == ".proto"
}

// cleanVendor removes files from unused pacakges and non-go files
func cleanVendor(vendorDir string, realDeps []*build.Package) error {
	realPaths := make(map[string]bool)
	for _, pkg := range realDeps {
		realPaths[pkg.Dir] = true
	}
	var paths []string
	err := filepath.Walk(vendorDir, func(path string, i os.FileInfo, err error) error {
		if path == vendorDir {
			return nil
		}
		if err != nil {
			return nil
		}
		if strings.HasPrefix(i.Name(), ".") || strings.HasPrefix(i.Name(), "_") {
			return os.RemoveAll(path)
		}
		if i.IsDir() {
			if i.Name() == "testdata" {
				return os.RemoveAll(path)
			}
			if isCDir(path) {
				realPaths[path] = true
				return nil
			}
			if !realPaths[path] {
				paths = append(paths, path)
			}
			return nil
		}
		if i.Name() == "LICENSE" || i.Name() == "COPYING" || i.Name() == "PATENTS" {
			return nil
		}
		// remove files from non-deps, non-go files and test files
		if !realPaths[filepath.Dir(path)] || !isGoFile(path) || strings.HasSuffix(path, "_test.go") {
			return os.Remove(path)
		}
		return nil
	})
	if err != nil {
		return err
	}
	sort.Sort(sort.Reverse(sort.StringSlice(paths)))
	// iterate over paths (longest first)
	for _, p := range paths {
		// at this point we cleaned all files from unused deps dirs
		lst, err := ioutil.ReadDir(p)
		if err != nil {
			return err
		}
		if len(lst) != 0 {
			continue
		}
		// remove all directories if they're not in dependency paths
		if err := os.RemoveAll(p); err != nil {
			return err
		}
	}
	return nil
}

func cleanVCS(v *godl.VCS) error {
	if err := os.RemoveAll(filepath.Join(v.Root, "."+v.Type)); err != nil {
		return err
	}
	return filepath.Walk(v.Root, func(path string, i os.FileInfo, err error) error {
		if path == vendorDir {
			return nil
		}
		if !i.IsDir() {
			return nil
		}
		name := i.Name()
		if name == "vendor" || name == "Godeps" || name == "_vendor" {
			if err := os.RemoveAll(path); err != nil {
				return err
			}
			return filepath.SkipDir
		}
		return nil
	})
}
<commit_msg>keep NOTICE files for apache licenses<commit_after>package main

import (
	"go/build"
	"io/ioutil"
	"os"
	"path/filepath"
	"sort"
	"strings"

	"github.com/LK4D4/vndr/godl"
)

func isCDir(path string) bool {
	fis, err := ioutil.ReadDir(path)
	if err != nil {
		return false
	}
	var hFound bool
	for _, fi := range fis {
		ext := filepath.Ext(fi.Name())
		if ext == ".cc" || ext == ".cpp" || ext == ".py" {
			return false
		}
		if ext == ".h" {
			hFound = true
		}
	}
	return hFound
}

func isGoFile(path string) bool {
	ext := filepath.Ext(path)
	return ext == ".go" || ext == ".c" || ext == ".h" || ext == ".s" || ext == ".proto"
}

var licenseFiles = map[string]bool{
	"LICENSE": true,
	"COPYING": true,
	"PATENTS": true,
	"NOTICE":  true,
}

// cleanVendor removes files from unused pacakges and non-go files
func cleanVendor(vendorDir string, realDeps []*build.Package) error {
	realPaths := make(map[string]bool)
	for _, pkg := range realDeps {
		realPaths[pkg.Dir] = true
	}
	var paths []string
	err := filepath.Walk(vendorDir, func(path string, i os.FileInfo, err error) error {
		if path == vendorDir {
			return nil
		}
		if err != nil {
			return nil
		}
		if strings.HasPrefix(i.Name(), ".") || strings.HasPrefix(i.Name(), "_") {
			return os.RemoveAll(path)
		}
		if i.IsDir() {
			if i.Name() == "testdata" {
				return os.RemoveAll(path)
			}
			if isCDir(path) {
				realPaths[path] = true
				return nil
			}
			if !realPaths[path] {
				paths = append(paths, path)
			}
			return nil
		}

		// keep files for licenses
		if licenseFiles[i.Name()] {
			return nil
		}
		// remove files from non-deps, non-go files and test files
		if !realPaths[filepath.Dir(path)] || !isGoFile(path) || strings.HasSuffix(path, "_test.go") {
			return os.Remove(path)
		}
		return nil
	})
	if err != nil {
		return err
	}
	sort.Sort(sort.Reverse(sort.StringSlice(paths)))
	// iterate over paths (longest first)
	for _, p := range paths {
		// at this point we cleaned all files from unused deps dirs
		lst, err := ioutil.ReadDir(p)
		if err != nil {
			return err
		}
		if len(lst) != 0 {
			continue
		}
		// remove all directories if they're not in dependency paths
		if err := os.RemoveAll(p); err != nil {
			return err
		}
	}
	return nil
}

func cleanVCS(v *godl.VCS) error {
	if err := os.RemoveAll(filepath.Join(v.Root, "."+v.Type)); err != nil {
		return err
	}
	return filepath.Walk(v.Root, func(path string, i os.FileInfo, err error) error {
		if path == vendorDir {
			return nil
		}
		if !i.IsDir() {
			return nil
		}
		name := i.Name()
		if name == "vendor" || name == "Godeps" || name == "_vendor" {
			if err := os.RemoveAll(path); err != nil {
				return err
			}
			return filepath.SkipDir
		}
		return nil
	})
}
<|endoftext|>


<commit_before>// clock counts down to or up from a target time.
package main

import (
	"fmt"
	"time"
)

func main() {
	const (
		indent          = "\t"
		highlight_start = "\x1b[1;36m"
		highlight_end   = "\x1b[0m"
	)
	fmt.Print(indent, highlight_start, "Just Go", highlight_end, "\n")
	target := time.Date(2015, 11, 10, 0, 0, 0, 0, time.Local)
	fmt.Print(indent, target.Format(time.UnixDate), "\n")

	var (
		previous time.Time
		days     int
		sign     string
	)
	for {
		now := time.Now()
		now = now.Add(time.Duration(-now.Nanosecond())) // truncate to second
		if now != previous {
			previous = now
			remaining := target.Sub(now)
			if remaining >= 0 {
				sign = "-" // countdown is "T minus..."
			} else {
				sign = "+" // count up is "T plus..."
				remaining = -remaining
			}
			if remaining >= 24*time.Hour {
				days = int(remaining / (24 * time.Hour))
				remaining = remaining % (24 * time.Hour)
			}
			fmt.Print(indent, now.Format(time.UnixDate), "  ", sign)
			if days > 0 {
				fmt.Print(days, "d")
			}
			fmt.Print(remaining, "          \r")
		}
		time.Sleep(50 * time.Millisecond)
	}
}
<commit_msg>new target<commit_after>// clock counts down to or up from a target time.
package main

import (
	"fmt"
	"time"
)

func main() {
	const (
		indent          = "\t"
		highlight_start = "\x1b[1;36m"
		highlight_end   = "\x1b[0m"
	)
	fmt.Print(indent, highlight_start, "Just Go", highlight_end, "\n")
	target := time.Date(2015, 11, 16, 0, 0, 0, 0, time.UTC)
	fmt.Print(indent, target.Format(time.UnixDate), "\n")

	var (
		previous time.Time
		days     int
		sign     string
	)
	for {
		now := time.Now()
		now = now.Add(time.Duration(-now.Nanosecond())) // truncate to second
		if now != previous {
			previous = now
			remaining := target.Sub(now)
			if remaining >= 0 {
				sign = "-" // countdown is "T minus..."
			} else {
				sign = "+" // count up is "T plus..."
				remaining = -remaining
			}
			if remaining >= 24*time.Hour {
				days = int(remaining / (24 * time.Hour))
				remaining = remaining % (24 * time.Hour)
			}
			fmt.Print(indent, now.Format(time.UnixDate), "  ", sign)
			if days > 0 {
				fmt.Print(days, "d")
			}
			fmt.Print(remaining, "          \r")
		}
		time.Sleep(50 * time.Millisecond)
	}
}
<|endoftext|>


<commit_before>package dcpu

import "time"

// Clock implements Generic Clock (compatible).
type Clock struct {
	DCPU     *DCPU
	Interval uint16
	Message  uint16
	Ticks    uint16

	lastTick time.Time
}

const (
	duration  = time.Second / frequency
	frequency = 60
	id        = 0x12d0b402
	version   = 0x0001
)

// Execute runs the clock.
func (c *Clock) Execute() {
	if c.Interval > 0 {
		now := time.Now()

		if now.Sub(c.lastTick) > time.Duration(c.Interval)*duration {
			c.lastTick = now
			c.Ticks++
			if c.DCPU != nil && c.Message > 0 {
				c.DCPU.Interrupt(c.Message)
			}
		}
	}
}

// GetID returns the Clock id.
func (c *Clock) GetID() uint32 {
	return id
}

// GetManufacturerID returns the Clock manufacturer id.
func (c *Clock) GetManufacturerID() uint32 {
	return 0
}

// GetVersion returns the Clock version.
func (c *Clock) GetVersion() uint16 {
	return version
}

// HandleHardwareInterrupt handles messages from the DCPU.
func (c *Clock) HandleHardwareInterrupt() {
	switch {
	case c.DCPU == nil:
		return

	case c.DCPU.RegisterA == 0:
		c.Interval = c.DCPU.RegisterB
		c.Ticks = 0

	case c.DCPU.RegisterA == 1:
		c.DCPU.RegisterC = c.Ticks

	case c.DCPU.RegisterA == 2:
		c.Message = c.DCPU.RegisterB
	}
}
<commit_msg>Make an enum for Clock hardware interrupt values.<commit_after>package dcpu

import "time"

// Clock implements Generic Clock (compatible).
type Clock struct {
	DCPU     *DCPU
	Interval uint16
	Message  uint16
	Ticks    uint16

	lastTick time.Time
}

const (
	duration     = time.Second / frequency
	frequency    = 60
	clockID      = 0x12d0b402
	clockVersion = 0x0001
)

const (
	clockSetInterval = iota
	clockGetTicks
	clockSetInterruptMessage
)

// Execute runs the clock.
func (c *Clock) Execute() {
	if c.Interval > 0 {
		now := time.Now()

		if now.Sub(c.lastTick) > time.Duration(c.Interval)*duration {
			c.lastTick = now
			c.Ticks++
			if c.DCPU != nil && c.Message > 0 {
				c.DCPU.Interrupt(c.Message)
			}
		}
	}
}

// GetID returns the Clock id.
func (c *Clock) GetID() uint32 {
	return clockID
}

// GetManufacturerID returns the Clock manufacturer id.
func (c *Clock) GetManufacturerID() uint32 {
	return 0
}

// GetVersion returns the Clock version.
func (c *Clock) GetVersion() uint16 {
	return clockVersion
}

// HandleHardwareInterrupt handles messages from the DCPU.
func (c *Clock) HandleHardwareInterrupt() {
	if c.DCPU == nil {
		return
	}

	switch c.DCPU.RegisterA {
	case clockSetInterval:
		c.Interval = c.DCPU.RegisterB
		c.Ticks = 0

	case clockGetTicks:
		c.DCPU.RegisterC = c.Ticks

	case clockSetInterruptMessage:
		c.Message = c.DCPU.RegisterB
	}
}
<|endoftext|>


<commit_before>package build

import (
	"github.com/InnovaCo/serve/manifest"
	"github.com/InnovaCo/serve/utils"
)

func init() {
	manifest.PluginRegestry.Add("build.sbt", SbtBuild{})
}

type SbtBuild struct{}

func (p SbtBuild) Run(data manifest.Manifest) error {
  return utils.RunCmd(`sbt ';set version := "%s"' clean test %s`, data.GetString("version"), data.GetStringOr("sbt", ""))
}
<commit_msg>= sbt: set every version for multimodule projects<commit_after>package build

import (
	"github.com/InnovaCo/serve/manifest"
	"github.com/InnovaCo/serve/utils"
)

func init() {
	manifest.PluginRegestry.Add("build.sbt", SbtBuild{})
}

type SbtBuild struct{}

func (p SbtBuild) Run(data manifest.Manifest) error {
  return utils.RunCmd(`sbt ';set every version := "%s"' clean test %s`, data.GetString("version"), data.GetStringOr("sbt", ""))
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	csv "github.com/whosonfirst/go-whosonfirst-csv"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"path"
	"path/filepath"
	"sync"
	"time"
)

var count int

var source = flag.String("source", "http://whosonfirst.mapzen.com/data/", "Where to look for files")
var dest = flag.String("dest", "", "Where to write files")

// PLEASE FOR TO MAKE ALL OF THIS IN TO A PROPER PACKAGE

func ParseFile(file string) error {

	abs_path, _ := filepath.Abs(file)
	reader, read_err := csv.NewDictReader(abs_path)

	if read_err != nil {
		log.Println(read_err)
		return read_err
	}

	wg := new(sync.WaitGroup)

	for {
		row, err := reader.Read()

		if err == io.EOF {
			break
		}

		if err != nil {
			return err
		}

		rel_path, ok := row["path"]

		if !ok {
			continue
		}

		wg.Add(1)

		go func() {
			defer wg.Done()
			FetchStore(rel_path)
		}()
	}

	wg.Wait()

	return nil
}

func FetchStore(rel_path string) error {

	count += 1

	remote_abspath := *source + rel_path
	local_abspath := path.Join(*dest, rel_path)

	// has_changed := false

	_, err := os.Stat(local_abspath)

	if ! os.IsNotExist(err) {
	   // Check whether file has changed here

	   log.Printf("%s already exists\n", local_abspath)
	   return nil
	} else {

	  local_root := path.Dir(local_abspath)

	  _, err := os.Stat(local_root)

	  if os.IsNotExist(err) {
		log.Printf("create %s\n", local_root)
		os.MkdirAll(local_root, 0755)
		}
	}

	log.Printf("fetch '%s' and store in %s\n", remote_abspath, local_abspath)

	rsp, fetch_err := http.Get(remote_abspath)

	if fetch_err != nil {
		log.Fatal(fetch_err)
		return fetch_err
	}

	contents, read_err := ioutil.ReadAll(rsp.Body)

	if read_err != nil {
		log.Println(read_err)
	}

	write_err := ioutil.WriteFile(local_abspath, contents, 0644)

	if write_err != nil {
		log.Println(write_err)
	}

	return nil
}

func main() {

	flag.Parse()
	args := flag.Args()

	start := time.Now()
	count = 0

	wg := new(sync.WaitGroup)

	for _, file := range args {

		wg.Add(1)

		go func() {
			defer wg.Done()
			log.Println(file)
			ParseFile(file)
		}()
	}

	wg.Wait()

	since := time.Since(start)
	secs := float64(since) / 1e9

	log.Printf("processed %d files in %f seconds\n", count, secs)
}
<commit_msg>check whether files have changed<commit_after>package main

import (
	"crypto/md5"
	enc "encoding/hex"
	"flag"
	csv "github.com/whosonfirst/go-whosonfirst-csv"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"path"
	"path/filepath"
	"strings"
	"sync"
	"time"
)

var count int

var source = flag.String("source", "http://whosonfirst.mapzen.com/data/", "Where to look for files")
var dest = flag.String("dest", "", "Where to write files")

// PLEASE FOR TO MAKE ALL OF THIS IN TO A PROPER PACKAGE

func ParseFile(file string) error {

	abs_path, _ := filepath.Abs(file)
	reader, read_err := csv.NewDictReader(abs_path)

	if read_err != nil {
		log.Println(read_err)
		return read_err
	}

	wg := new(sync.WaitGroup)

	for {
		row, err := reader.Read()

		if err == io.EOF {
			break
		}

		if err != nil {
			return err
		}

		rel_path, ok := row["path"]

		if !ok {
			continue
		}

		wg.Add(1)

		go func() {
			defer wg.Done()
			FetchStore(rel_path)
		}()
	}

	wg.Wait()

	return nil
}

func FetchStore(rel_path string) error {

	count += 1

	remote_abspath := *source + rel_path
	local_abspath := path.Join(*dest, rel_path)

	// has_changed := false

	_, err := os.Stat(local_abspath)

	if !os.IsNotExist(err) {

		change, _ := HasChanged(local_abspath, remote_abspath)

		if !change {
			return nil
		}

	} else {

		local_root := path.Dir(local_abspath)

		_, err := os.Stat(local_root)

		if os.IsNotExist(err) {
			log.Printf("create %s\n", local_root)
			os.MkdirAll(local_root, 0755)
		}
	}

	log.Printf("fetch '%s' and store in %s\n", remote_abspath, local_abspath)

	rsp, fetch_err := http.Get(remote_abspath)

	if fetch_err != nil {
		log.Fatal(fetch_err)
		return fetch_err
	}

	contents, read_err := ioutil.ReadAll(rsp.Body)

	if read_err != nil {
		log.Println(read_err)
	}

	write_err := ioutil.WriteFile(local_abspath, contents, 0644)

	if write_err != nil {
		log.Println(write_err)
	}

	return nil
}

func HasChanged(local string, remote string) (bool, error) {

	change := true

	body, err := ioutil.ReadFile(local)

	if err != nil {
		return change, err
	}

	hash := md5.Sum(body)
	local_hash := enc.EncodeToString(hash[:])

	rsp, err := http.Head(remote)

	if err != nil {
		return change, err
	}

	etag := rsp.Header.Get("Etag")
	remote_hash := strings.Replace(etag, "\"", "", -1)

	if local_hash == remote_hash {
		change = false
	}

	log.Printf("hash %s etag %s change %t\n", local_hash, remote_hash, change)

	return change, nil
}

func main() {

	flag.Parse()
	args := flag.Args()

	start := time.Now()
	count = 0

	wg := new(sync.WaitGroup)

	for _, file := range args {

		wg.Add(1)

		go func() {
			defer wg.Done()
			log.Println(file)
			ParseFile(file)
		}()
	}

	wg.Wait()

	since := time.Since(start)
	secs := float64(since) / 1e9

	log.Printf("processed %d files in %f seconds\n", count, secs)
}
<|endoftext|>


<commit_before>package hots

import (
	"errors"
	"fmt"
	"io/ioutil"
	"net/url"
	"regexp"
	"strconv"

	"github.com/StalkR/goircbot/lib/transport"
)

type Score struct {
	Medal string
	Score int
	MMR   int
}

type Stats struct {
	PlayerID   int
	TeamLeague Score
	HeroLeague Score
	QuickMatch Score
}

func (s *Stats) String() string {
	tl := fmt.Sprintf("Team League: %s %d (MMR %d)", s.TeamLeague.Medal, s.TeamLeague.Score, s.TeamLeague.MMR)
	if s.TeamLeague.Medal == "" {
		tl = "Team League: n/a"
	}
	hl := fmt.Sprintf("Hero League: %s %d (MMR %d)", s.HeroLeague.Medal, s.HeroLeague.Score, s.HeroLeague.MMR)
	if s.HeroLeague.Medal == "" {
		hl = "Hero League: n/a"
	}
	qm := fmt.Sprintf("Quick Match: %s %d (MMR %d)", s.QuickMatch.Medal, s.QuickMatch.Score, s.QuickMatch.MMR)
	if s.QuickMatch.Medal == "" {
		qm = "Quick Match: n/a"
	}
	return fmt.Sprintf("%s, %s, %s - %s?PlayerID=%d", tl, hl, qm, statsURL, s.PlayerID)
}

func NewStats(id int) (*Stats, error) {
	b, err := get(id)
	if err != nil {
		return nil, err
	}
	return parse(string(b))
}

const statsURL = "https://www.hotslogs.com/Player/Profile"

func get(id int) ([]byte, error) {
	c, err := transport.Client(statsURL)
	if err != nil {
		return nil, err
	}
	v := url.Values{}
	v.Add("PlayerID", fmt.Sprintf("%d", id))
	resp, err := c.Get(fmt.Sprintf("%s?%s", statsURL, v.Encode()))
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	b, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}
	return b, nil
}

var (
	playerIDRE   = regexp.MustCompile(`<link href="https://www\.hotslogs\.com/Player/Profile\?PlayerID=(\d+)"`)
	teamLeagueRE = regexp.MustCompile(`<th>Team League</th><td><img[^>]*>[^<]*<span>(\w+) (\d+) \(Current MMR: (\d+)\)</span>`)
	heroLeagueRE = regexp.MustCompile(`<th>Hero League</th><td><img[^>]*>[^<]*<span>(\w+) (\d+) \(Current MMR: (\d+)\)</span>`)
	quickMatchRE = regexp.MustCompile(`<th>Quick Match</th><td><img[^>]*>[^<]*<span>(\w+) (\d+) \(Current MMR: (\d+)\)</span>`)
)

func parse(page string) (*Stats, error) {
	var s Stats
	if m := playerIDRE.FindStringSubmatch(page); m != nil {
		playerID, err := strconv.Atoi(m[1])
		if err != nil {
			return nil, err
		}
		s.PlayerID = playerID
	} else {
		return nil, errors.New("hots: could not find player ID")
	}
	if m := teamLeagueRE.FindStringSubmatch(page); m != nil {
		s.TeamLeague.Medal = m[1]
		score, err := strconv.Atoi(m[2])
		if err != nil {
			return nil, err
		}
		s.TeamLeague.Score = score
		mmr, err := strconv.Atoi(m[3])
		if err != nil {
			return nil, err
		}
		s.TeamLeague.MMR = mmr
	}
	if m := heroLeagueRE.FindStringSubmatch(page); m != nil {
		s.HeroLeague.Medal = m[1]
		score, err := strconv.Atoi(m[2])
		if err != nil {
			return nil, err
		}
		s.HeroLeague.Score = score
		mmr, err := strconv.Atoi(m[3])
		if err != nil {
			return nil, err
		}
		s.HeroLeague.MMR = mmr
	}
	if m := quickMatchRE.FindStringSubmatch(page); m != nil {
		s.QuickMatch.Medal = m[1]
		score, err := strconv.Atoi(m[2])
		if err != nil {
			return nil, err
		}
		s.QuickMatch.Score = score
		mmr, err := strconv.Atoi(m[3])
		if err != nil {
			return nil, err
		}
		s.QuickMatch.MMR = mmr
	}
	return &s, nil
}
<commit_msg>plugins: hots: fix parsing<commit_after>package hots

import (
	"errors"
	"fmt"
	"io/ioutil"
	"net/url"
	"regexp"
	"strconv"

	"github.com/StalkR/goircbot/lib/transport"
)

type Score struct {
	Medal string
	Score int
	MMR   int
}

type Stats struct {
	PlayerID   int
	TeamLeague Score
	HeroLeague Score
	QuickMatch Score
}

func (s *Stats) String() string {
	tl := fmt.Sprintf("Team League: %s %d (MMR %d)", s.TeamLeague.Medal, s.TeamLeague.Score, s.TeamLeague.MMR)
	if s.TeamLeague.Medal == "" {
		tl = "Team League: n/a"
	}
	hlScore := "*"
	if s.HeroLeague.Score != -1 {
		hlScore = fmt.Sprintf("%d", s.HeroLeague.Score)
	}
	hl := fmt.Sprintf("Hero League: %s %v (MMR %d)", s.HeroLeague.Medal, hlScore, s.HeroLeague.MMR)
	if s.HeroLeague.Medal == "" {
		hl = "Hero League: n/a"
	}
	qm := fmt.Sprintf("Quick Match: %s %d (MMR %d)", s.QuickMatch.Medal, s.QuickMatch.Score, s.QuickMatch.MMR)
	if s.QuickMatch.Medal == "" {
		qm = "Quick Match: n/a"
	}
	return fmt.Sprintf("%s, %s, %s - %s?PlayerID=%d", tl, hl, qm, statsURL, s.PlayerID)
}

func NewStats(id int) (*Stats, error) {
	b, err := get(id)
	if err != nil {
		return nil, err
	}
	return parse(string(b))
}

const statsURL = "https://www.hotslogs.com/Player/Profile"

func get(id int) ([]byte, error) {
	c, err := transport.Client(statsURL)
	if err != nil {
		return nil, err
	}
	v := url.Values{}
	v.Add("PlayerID", fmt.Sprintf("%d", id))
	resp, err := c.Get(fmt.Sprintf("%s?%s", statsURL, v.Encode()))
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	b, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}
	return b, nil
}

var (
	playerIDRE   = regexp.MustCompile(`<link href="https://www\.hotslogs\.com/Player/Profile\?PlayerID=(\d+)"`)
	teamLeagueRE = regexp.MustCompile(`<td>Team League</td><td><img[^>]*>[^<]*<span>(\w+) (\d+) \(MMR: (\d+)\)</span>`)
	heroLeagueRE = regexp.MustCompile(`<td>Hero League</td><td><img[^>]*>[^<]*<span>(\w+) (\d+|<div[^>]*>\*</div>) \(MMR: (\d+)\)</span>`)
	quickMatchRE = regexp.MustCompile(`<td>Quick Match</td><td><img[^>]*>[^<]*<span>(\w+) (\d+) \(MMR: (\d+)\)</span>`)
)

func parse(page string) (*Stats, error) {
	var s Stats
	if m := playerIDRE.FindStringSubmatch(page); m != nil {
		playerID, err := strconv.Atoi(m[1])
		if err != nil {
			return nil, err
		}
		s.PlayerID = playerID
	} else {
		return nil, errors.New("hots: could not find player ID")
	}
	if m := teamLeagueRE.FindStringSubmatch(page); m != nil {
		s.TeamLeague.Medal = m[1]
		score, err := strconv.Atoi(m[2])
		if err != nil {
			return nil, err
		}
		s.TeamLeague.Score = score
		mmr, err := strconv.Atoi(m[3])
		if err != nil {
			return nil, err
		}
		s.TeamLeague.MMR = mmr
	}
	if m := heroLeagueRE.FindStringSubmatch(page); m != nil {
		s.HeroLeague.Medal = m[1]
		score, err := strconv.Atoi(m[2])
		if err != nil {
			score = -1
		}
		s.HeroLeague.Score = score
		mmr, err := strconv.Atoi(m[3])
		if err != nil {
			return nil, err
		}
		s.HeroLeague.MMR = mmr
	}
	if m := quickMatchRE.FindStringSubmatch(page); m != nil {
		s.QuickMatch.Medal = m[1]
		score, err := strconv.Atoi(m[2])
		if err != nil {
			return nil, err
		}
		s.QuickMatch.Score = score
		mmr, err := strconv.Atoi(m[3])
		if err != nil {
			return nil, err
		}
		s.QuickMatch.MMR = mmr
	}
	return &s, nil
}
<|endoftext|>


<commit_before>/*
Copyright (c) 2012 krepa098 (krepa098 at gmail dot com)
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose, including commercial applications,
and to alter it and redistribute it freely, subject to the following restrictions:
	1.	The origin of this software must not be misrepresented; you must not claim that you wrote the original software.
		If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
	2. 	Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
	3. 	This notice may not be removed or altered from any source distribution.
*/

package GoSFML2

// #include <SFML/Graphics.h>
// int getSizeColor() { return sizeof(sfColor); }
import "C"

/////////////////////////////////////
///		STRUCTS
/////////////////////////////////////

type Color struct {
	R byte
	G byte
	B byte
	A byte //< 0=transparent
}

/////////////////////////////////////
///		CONSTS
/////////////////////////////////////

var (
	Color_Black = Color{0, 0, 0, 255}
	Color_Red   = Color{255, 0, 0, 255}
	Color_Green = Color{0, 255, 0, 255}
	Color_Blue  = Color{0, 0, 255, 255}
	Color_White = Color{255, 255, 255, 255}
)

/////////////////////////////////////
///		FUNCS
/////////////////////////////////////

func (this Color) Add(other Color) (newColor Color) {
	newColor.fromC(C.sfColor_add(this.toC(), other.toC()))
	return
}

func (this Color) Modulate(other Color) (newColor Color) {
	newColor.fromC(C.sfColor_modulate(this.toC(), other.toC()))
	return
}

/////////////////////////////////////
///		GO <-> C
/////////////////////////////////////

func (this *Color) fromC(color C.sfColor) {
	this.R = byte(color.r)
	this.G = byte(color.g)
	this.B = byte(color.b)
	this.A = byte(color.a)
}

func (this *Color) toC() C.sfColor {
	return C.sfColor{r: C.sfUint8(this.R), g: C.sfUint8(this.G), b: C.sfUint8(this.B), a: C.sfUint8(this.A)}
}

/////////////////////////////////////
///		Testing
/////////////////////////////////////

func sizeofColor() int {
	return int(C.getSizeColor())
}
<commit_msg>additional colors<commit_after>/*
Copyright (c) 2012 krepa098 (krepa098 at gmail dot com)
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose, including commercial applications,
and to alter it and redistribute it freely, subject to the following restrictions:
	1.	The origin of this software must not be misrepresented; you must not claim that you wrote the original software.
		If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
	2. 	Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
	3. 	This notice may not be removed or altered from any source distribution.
*/

package GoSFML2

// #include <SFML/Graphics.h>
// int getSizeColor() { return sizeof(sfColor); }
import "C"

/////////////////////////////////////
///		STRUCTS
/////////////////////////////////////

type Color struct {
	R byte
	G byte
	B byte
	A byte //< 0=transparent
}

/////////////////////////////////////
///		CONSTS
/////////////////////////////////////

var (
	Color_Black       = Color{0, 0, 0, 255}
	Color_White       = Color{255, 255, 255, 255}
	Color_Red         = Color{255, 0, 0, 255}
	Color_Green       = Color{0, 255, 0, 255}
	Color_Blue        = Color{0, 0, 255, 255}
	Color_Yellow      = Color{255, 255, 0, 255}
	Color_Magenta     = Color{255, 0, 255, 255}
	Color_Cyan        = Color{0, 255, 255, 255}
	Color_Transparent = Color{0, 0, 0, 0}
)

/////////////////////////////////////
///		FUNCS
/////////////////////////////////////

func (this Color) Add(other Color) (newColor Color) {
	newColor.fromC(C.sfColor_add(this.toC(), other.toC()))
	return
}

func (this Color) Modulate(other Color) (newColor Color) {
	newColor.fromC(C.sfColor_modulate(this.toC(), other.toC()))
	return
}

/////////////////////////////////////
///		GO <-> C
/////////////////////////////////////

func (this *Color) fromC(color C.sfColor) {
	this.R = byte(color.r)
	this.G = byte(color.g)
	this.B = byte(color.b)
	this.A = byte(color.a)
}

func (this *Color) toC() C.sfColor {
	return C.sfColor{r: C.sfUint8(this.R), g: C.sfUint8(this.G), b: C.sfUint8(this.B), a: C.sfUint8(this.A)}
}

/////////////////////////////////////
///		Testing
/////////////////////////////////////

func sizeofColor() int {
	return int(C.getSizeColor())
}
<|endoftext|>


<commit_before>package builder
import (
	"github.com/blablacar/cnt/log"
	"os"
	"github.com/blablacar/cnt/utils"
	"os/exec"
	"io"
	"bytes"
	"strings"
	"github.com/appc/spec/discovery"
	"github.com/blablacar/cnt/config"
	"io/ioutil"
)

func (cnt *Cnt) Build() error {
	log.Get().Info("Building Image : ", cnt.manifest.Aci.Name)

	os.MkdirAll(cnt.rootfs, 0777)

	cnt.processFrom()

	cnt.runlevelBuildSetup()
	cnt.copyRunlevelsBuild()
//	cnt.copyInstallAndCreatePacker()

	cnt.writeBuildScript()
	cnt.writeRktManifest()
	cnt.writeCntManifest() // TODO move that, here because we update the version number to generated version

	cnt.runBuild()
	cnt.copyRunlevelsPrestart()
	cnt.copyAttributes()
	cnt.copyConfd()
	cnt.copyFiles()

	cnt.tarAci()
	//	ExecCmd("chown " + os.Getenv("SUDO_USER") + ": " + target + "/*") //TODO chown
	return nil
}

///////////////////////////////////////////////////////

func (cnt *Cnt) writeCntManifest() {
	utils.CopyFile(cnt.path +  "/"+ MANIFEST, cnt.target + "/"+ MANIFEST)
}

func (cnt *Cnt) runBuild() {
	if res, err := utils.IsDirEmpty(cnt.target + RUNLEVELS_BUILD); res || err != nil {
		return
	}
	if err := utils.ExecCmd("systemd-nspawn", "--version"); err == nil {
		log.Get().Info("Run with systemd-nspawn")
		if err := utils.ExecCmd("systemd-nspawn", "--directory=" + cnt.rootfs, "--capability=all",
			"--bind=" + cnt.target + "/:/target", "--share-system", "target/build.sh"); err != nil {
			log.Get().Panic("Build step did not succeed", err)
		}
	} else {
		log.Get().Info("Run with docker")

		//
		log.Get().Info("Prepare Docker");
		first := exec.Command("bash", "-c", "cd " + cnt.rootfs + " && tar cf - .")
		second := exec.Command("docker", "import", "-", "")

		reader, writer := io.Pipe()
		first.Stdout = writer
		second.Stdin = reader

		var buff bytes.Buffer
		second.Stdout = &buff

		first.Start()
		second.Start()
		first.Wait()
		writer.Close()
		second.Wait()
		imgId := strings.TrimSpace(buff.String())

		//
		log.Get().Info("Run Docker\n");
		cmd := []string{"run", "--name=" + ShortName(cnt.manifest.NameAndVersion), "-v", cnt.target + ":/target", imgId, "/target/build.sh"}
		utils.ExecCmd("docker", "rm", ShortName(cnt.manifest.NameAndVersion))
		if err := utils.ExecCmd("docker", cmd...); err != nil {
			panic(err)
		}

		//
		log.Get().Info("Release Docker");
		if cnt.manifest.Build.NoBuildImage() {
			os.RemoveAll(cnt.rootfs)
			os.Mkdir(cnt.rootfs, 0777)

			if err := utils.ExecCmd("docker", "export", "-o", cnt.target + "/dockerfs.tar", ShortName(cnt.manifest.NameAndVersion)); err != nil {
				panic(err)
			}

			utils.ExecCmd("tar", "xpf", cnt.target + "/dockerfs.tar", "-C", cnt.rootfs)
		}
		if err := utils.ExecCmd("docker", "rm", ShortName(cnt.manifest.NameAndVersion)); err != nil {
			panic(err)
		}
		if err := utils.ExecCmd("docker", "rmi", imgId); err != nil {
			panic(err)
		}

	}
}

func (cnt *Cnt) processFrom() {
	if cnt.manifest.From != "" {
		log.Get().Info("Prepare rootfs from " + cnt.manifest.From)

		app, err := discovery.NewAppFromString(cnt.manifest.From)
		if app.Labels["os"] == "" {
			app.Labels["os"] = "linux"
		}
		if app.Labels["arch"] == "" {
			app.Labels["arch"] = "amd64"
		}

		endpoint, _, err := discovery.DiscoverEndpoints(*app, false)
		if err != nil {
			panic(err)
		}

		url := endpoint.ACIEndpoints[0].ACI

		aciPath := config.GetConfig().AciPath + "/" + cnt.manifest.From
		if _, err := os.Stat(aciPath + "/image.aci"); cnt.args.ForceUpdate || os.IsNotExist(err) {
			if err := os.MkdirAll(aciPath, 0755); err != nil {
				log.Get().Panic(err)
			}
			utils.ExecCmd("wget", "-O", aciPath + "/image.aci", url)
		} else {
			log.Get().Info("Image " + cnt.manifest.From + " Already exists locally, will not be downloaded")
		}

		utils.ExecCmd("tar", "xpf", aciPath + "/image.aci", "-C", cnt.target)

		//		utils.ExecCmd("rkt", "--insecure-skip-verify=true", "fetch", cnt.manifest.From)
		//		utils.ExecCmd("rkt", "image", "export", "--overwrite", cnt.manifest.From, cnt.target + "/from.aci")
		//		utils.ExecCmd("tar", "xf", cnt.target + "/from.aci", "-C", cnt.target)
		//		os.Remove(cnt.target + "/from.aci")
	}
}

func (cnt *Cnt) copyRunlevelsBuild() {
	if err := os.MkdirAll(cnt.target + RUNLEVELS, 0755); err != nil {
		log.Get().Panic(err)
	}
	utils.CopyDir(cnt.path + RUNLEVELS, cnt.target + RUNLEVELS)
}

func (cnt *Cnt) runlevelBuildSetup() {
	files, err := ioutil.ReadDir(cnt.path + RUNLEVELS_BUILD_SETUP) // already sorted by name
	if err != nil {
		return
	}

	os.Setenv("TARGET", cnt.target)
	for _, f := range files {
		if !f.IsDir() {
			log.Get().Info("Running Build setup level : ", f.Name())
			if err := utils.ExecCmd(cnt.path + RUNLEVELS_BUILD_SETUP + "/" +f.Name()); err != nil {
				log.Get().Panic(err)
			}
		}
	}
}

func (cnt *Cnt) tarAci() {
	dir, _ := os.Getwd();
	log.Get().Debug("chdir to", cnt.target)
	os.Chdir(cnt.target);

	args := []string{"manifest", "rootfs/"}

	if _, err := os.Stat(cnt.path + RUNLEVELS_BUILD_INHERIT_EARLY); err == nil {
		args = append(args, strings.TrimPrefix(RUNLEVELS_BUILD_INHERIT_EARLY,"/"))
	}
	if _, err := os.Stat(cnt.path + RUNLEVELS_BUILD_INHERIT_LATE); err == nil {
		args = append(args, strings.TrimPrefix(RUNLEVELS_BUILD_INHERIT_LATE,"/"))
	}

	utils.Tar(cnt.args.Zip, "image.aci", args...)
	log.Get().Debug("chdir to", dir)
	os.Chdir(dir);
}

//func (cnt *Cnt) copyInstallAndCreatePacker() {
//	if _, err := os.Stat(cnt.path + "/install.sh"); err == nil {
//		utils.CopyFile(cnt.path + "/install.sh", cnt.target + "/install.sh")
//		sum, _ := utils.ChecksumFile(cnt.target + "/install.sh")
//		lastSum, err := ioutil.ReadFile(cnt.target + "/install.sh.SUM")
//		if err != nil || !bytes.Equal(lastSum, sum) {
//			utils.WritePackerFiles(cnt.target)
//			ioutil.WriteFile(cnt.target + "/install.sh.SUM", sum, 0755)
//			return
//		}
//	}
//	utils.RemovePackerFiles(cnt.target)
//}

func (cnt *Cnt) copyRunlevelsPrestart() {
	if err := os.MkdirAll(cnt.rootfs + "/etc/prestart/late-prestart.d", 0755); err != nil {
		log.Get().Panic(err)
	}
	if err := os.MkdirAll(cnt.rootfs + "/etc/prestart/early-prestart.d", 0755); err != nil {
		log.Get().Panic(err)
	}
	utils.CopyDir(cnt.path + RUNLEVELS_PRESTART, cnt.rootfs + "/etc/prestart/early-prestart.d")
	utils.CopyDir(cnt.path + RUNLEVELS_LATESTART, cnt.rootfs + "/etc/prestart/late-prestart.d")
}

func (cnt *Cnt) copyConfd() {
	if err := os.MkdirAll(cnt.rootfs + "/etc/prestart/", 0755); err != nil {
		log.Get().Panic(err)
	}
	utils.CopyDir(cnt.path + CONFD_CONFIG, cnt.rootfs + "/etc/prestart/conf.d")
	utils.CopyDir(cnt.path + CONFD_TEMPLATE, cnt.rootfs + "/etc/prestart/templates")
}

func (cnt *Cnt) copyFiles() {
	utils.CopyDir(cnt.path + FILES_PATH, cnt.rootfs)
}

func (cnt *Cnt) copyAttributes() {
	if err := os.MkdirAll(cnt.rootfs + "/etc/prestart/attributes/" + ShortNameId(cnt.manifest.Aci.Name), 0755); err != nil {
		log.Get().Panic(err)
	}
	utils.CopyDir(cnt.path + ATTRIBUTES, cnt.rootfs + "/etc/prestart/attributes/" + ShortNameId(cnt.manifest.Aci.Name))
}

func (cnt *Cnt) writeBuildScript() {
	rootfs := "${TARGET}/rootfs"
	if cnt.manifest.Build.NoBuildImage() {
		rootfs = ""
	}
	build := strings.Replace(buildScript, "%%ROOTFS%%", rootfs, 1)
	ioutil.WriteFile(cnt.target + "/build.sh", []byte(build), 0777)
}

func (cnt *Cnt) writeRktManifest() {
	log.Get().Debug("Writing aci manifest")
	if val, _ := cnt.manifest.Aci.Labels.Get("version"); val == "" {
		changeVersion(&cnt.manifest.Aci.Labels, utils.GenerateVersion())
	}
	version, _ := cnt.manifest.Aci.Labels.Get("version")
	utils.WriteImageManifest(&cnt.manifest.Aci, cnt.target + "/manifest", cnt.manifest.Aci.Name, version)
}
<commit_msg>add BASEDIR to build-setup<commit_after>package builder
import (
	"github.com/blablacar/cnt/log"
	"os"
	"github.com/blablacar/cnt/utils"
	"os/exec"
	"io"
	"bytes"
	"strings"
	"github.com/appc/spec/discovery"
	"github.com/blablacar/cnt/config"
	"io/ioutil"
)

func (cnt *Cnt) Build() error {
	log.Get().Info("Building Image : ", cnt.manifest.Aci.Name)

	os.MkdirAll(cnt.rootfs, 0777)

	cnt.processFrom()

	cnt.runlevelBuildSetup()
	cnt.copyRunlevelsBuild()
//	cnt.copyInstallAndCreatePacker()

	cnt.writeBuildScript()
	cnt.writeRktManifest()
	cnt.writeCntManifest() // TODO move that, here because we update the version number to generated version

	cnt.runBuild()
	cnt.copyRunlevelsPrestart()
	cnt.copyAttributes()
	cnt.copyConfd()
	cnt.copyFiles()

	cnt.tarAci()
	//	ExecCmd("chown " + os.Getenv("SUDO_USER") + ": " + target + "/*") //TODO chown
	return nil
}

///////////////////////////////////////////////////////

func (cnt *Cnt) writeCntManifest() {
	utils.CopyFile(cnt.path +  "/"+ MANIFEST, cnt.target + "/"+ MANIFEST)
}

func (cnt *Cnt) runBuild() {
	if res, err := utils.IsDirEmpty(cnt.target + RUNLEVELS_BUILD); res || err != nil {
		return
	}
	if err := utils.ExecCmd("systemd-nspawn", "--version"); err == nil {
		log.Get().Info("Run with systemd-nspawn")
		if err := utils.ExecCmd("systemd-nspawn", "--directory=" + cnt.rootfs, "--capability=all",
			"--bind=" + cnt.target + "/:/target", "--share-system", "target/build.sh"); err != nil {
			log.Get().Panic("Build step did not succeed", err)
		}
	} else {
		log.Get().Info("Run with docker")

		//
		log.Get().Info("Prepare Docker");
		first := exec.Command("bash", "-c", "cd " + cnt.rootfs + " && tar cf - .")
		second := exec.Command("docker", "import", "-", "")

		reader, writer := io.Pipe()
		first.Stdout = writer
		second.Stdin = reader

		var buff bytes.Buffer
		second.Stdout = &buff

		first.Start()
		second.Start()
		first.Wait()
		writer.Close()
		second.Wait()
		imgId := strings.TrimSpace(buff.String())

		//
		log.Get().Info("Run Docker\n");
		cmd := []string{"run", "--name=" + ShortName(cnt.manifest.NameAndVersion), "-v", cnt.target + ":/target", imgId, "/target/build.sh"}
		utils.ExecCmd("docker", "rm", ShortName(cnt.manifest.NameAndVersion))
		if err := utils.ExecCmd("docker", cmd...); err != nil {
			panic(err)
		}

		//
		log.Get().Info("Release Docker");
		if cnt.manifest.Build.NoBuildImage() {
			os.RemoveAll(cnt.rootfs)
			os.Mkdir(cnt.rootfs, 0777)

			if err := utils.ExecCmd("docker", "export", "-o", cnt.target + "/dockerfs.tar", ShortName(cnt.manifest.NameAndVersion)); err != nil {
				panic(err)
			}

			utils.ExecCmd("tar", "xpf", cnt.target + "/dockerfs.tar", "-C", cnt.rootfs)
		}
		if err := utils.ExecCmd("docker", "rm", ShortName(cnt.manifest.NameAndVersion)); err != nil {
			panic(err)
		}
		if err := utils.ExecCmd("docker", "rmi", imgId); err != nil {
			panic(err)
		}

	}
}

func (cnt *Cnt) processFrom() {
	if cnt.manifest.From != "" {
		log.Get().Info("Prepare rootfs from " + cnt.manifest.From)

		app, err := discovery.NewAppFromString(cnt.manifest.From)
		if app.Labels["os"] == "" {
			app.Labels["os"] = "linux"
		}
		if app.Labels["arch"] == "" {
			app.Labels["arch"] = "amd64"
		}

		endpoint, _, err := discovery.DiscoverEndpoints(*app, false)
		if err != nil {
			panic(err)
		}

		url := endpoint.ACIEndpoints[0].ACI

		aciPath := config.GetConfig().AciPath + "/" + cnt.manifest.From
		if _, err := os.Stat(aciPath + "/image.aci"); cnt.args.ForceUpdate || os.IsNotExist(err) {
			if err := os.MkdirAll(aciPath, 0755); err != nil {
				log.Get().Panic(err)
			}
			utils.ExecCmd("wget", "-O", aciPath + "/image.aci", url)
		} else {
			log.Get().Info("Image " + cnt.manifest.From + " Already exists locally, will not be downloaded")
		}

		utils.ExecCmd("tar", "xpf", aciPath + "/image.aci", "-C", cnt.target)

		//		utils.ExecCmd("rkt", "--insecure-skip-verify=true", "fetch", cnt.manifest.From)
		//		utils.ExecCmd("rkt", "image", "export", "--overwrite", cnt.manifest.From, cnt.target + "/from.aci")
		//		utils.ExecCmd("tar", "xf", cnt.target + "/from.aci", "-C", cnt.target)
		//		os.Remove(cnt.target + "/from.aci")
	}
}

func (cnt *Cnt) copyRunlevelsBuild() {
	if err := os.MkdirAll(cnt.target + RUNLEVELS, 0755); err != nil {
		log.Get().Panic(err)
	}
	utils.CopyDir(cnt.path + RUNLEVELS, cnt.target + RUNLEVELS)
}

func (cnt *Cnt) runlevelBuildSetup() {
	files, err := ioutil.ReadDir(cnt.path + RUNLEVELS_BUILD_SETUP) // already sorted by name
	if err != nil {
		return
	}

	os.Setenv("BASEDIR", cnt.path)
	os.Setenv("TARGET", cnt.target)
	for _, f := range files {
		if !f.IsDir() {
			log.Get().Info("Running Build setup level : ", f.Name())
			if err := utils.ExecCmd(cnt.path + RUNLEVELS_BUILD_SETUP + "/" +f.Name()); err != nil {
				log.Get().Panic(err)
			}
		}
	}
}

func (cnt *Cnt) tarAci() {
	dir, _ := os.Getwd();
	log.Get().Debug("chdir to", cnt.target)
	os.Chdir(cnt.target);

	args := []string{"manifest", "rootfs/"}

	if _, err := os.Stat(cnt.path + RUNLEVELS_BUILD_INHERIT_EARLY); err == nil {
		args = append(args, strings.TrimPrefix(RUNLEVELS_BUILD_INHERIT_EARLY,"/"))
	}
	if _, err := os.Stat(cnt.path + RUNLEVELS_BUILD_INHERIT_LATE); err == nil {
		args = append(args, strings.TrimPrefix(RUNLEVELS_BUILD_INHERIT_LATE,"/"))
	}

	utils.Tar(cnt.args.Zip, "image.aci", args...)
	log.Get().Debug("chdir to", dir)
	os.Chdir(dir);
}

//func (cnt *Cnt) copyInstallAndCreatePacker() {
//	if _, err := os.Stat(cnt.path + "/install.sh"); err == nil {
//		utils.CopyFile(cnt.path + "/install.sh", cnt.target + "/install.sh")
//		sum, _ := utils.ChecksumFile(cnt.target + "/install.sh")
//		lastSum, err := ioutil.ReadFile(cnt.target + "/install.sh.SUM")
//		if err != nil || !bytes.Equal(lastSum, sum) {
//			utils.WritePackerFiles(cnt.target)
//			ioutil.WriteFile(cnt.target + "/install.sh.SUM", sum, 0755)
//			return
//		}
//	}
//	utils.RemovePackerFiles(cnt.target)
//}

func (cnt *Cnt) copyRunlevelsPrestart() {
	if err := os.MkdirAll(cnt.rootfs + "/etc/prestart/late-prestart.d", 0755); err != nil {
		log.Get().Panic(err)
	}
	if err := os.MkdirAll(cnt.rootfs + "/etc/prestart/early-prestart.d", 0755); err != nil {
		log.Get().Panic(err)
	}
	utils.CopyDir(cnt.path + RUNLEVELS_PRESTART, cnt.rootfs + "/etc/prestart/early-prestart.d")
	utils.CopyDir(cnt.path + RUNLEVELS_LATESTART, cnt.rootfs + "/etc/prestart/late-prestart.d")
}

func (cnt *Cnt) copyConfd() {
	if err := os.MkdirAll(cnt.rootfs + "/etc/prestart/", 0755); err != nil {
		log.Get().Panic(err)
	}
	utils.CopyDir(cnt.path + CONFD_CONFIG, cnt.rootfs + "/etc/prestart/conf.d")
	utils.CopyDir(cnt.path + CONFD_TEMPLATE, cnt.rootfs + "/etc/prestart/templates")
}

func (cnt *Cnt) copyFiles() {
	utils.CopyDir(cnt.path + FILES_PATH, cnt.rootfs)
}

func (cnt *Cnt) copyAttributes() {
	if err := os.MkdirAll(cnt.rootfs + "/etc/prestart/attributes/" + ShortNameId(cnt.manifest.Aci.Name), 0755); err != nil {
		log.Get().Panic(err)
	}
	utils.CopyDir(cnt.path + ATTRIBUTES, cnt.rootfs + "/etc/prestart/attributes/" + ShortNameId(cnt.manifest.Aci.Name))
}

func (cnt *Cnt) writeBuildScript() {
	rootfs := "${TARGET}/rootfs"
	if cnt.manifest.Build.NoBuildImage() {
		rootfs = ""
	}
	build := strings.Replace(buildScript, "%%ROOTFS%%", rootfs, 1)
	ioutil.WriteFile(cnt.target + "/build.sh", []byte(build), 0777)
}

func (cnt *Cnt) writeRktManifest() {
	log.Get().Debug("Writing aci manifest")
	if val, _ := cnt.manifest.Aci.Labels.Get("version"); val == "" {
		changeVersion(&cnt.manifest.Aci.Labels, utils.GenerateVersion())
	}
	version, _ := cnt.manifest.Aci.Labels.Get("version")
	utils.WriteImageManifest(&cnt.manifest.Aci, cnt.target + "/manifest", cnt.manifest.Aci.Name, version)
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"io"
	"os"

	"github.com/edsrzf/fineline"
)

func main() {
	ctx := newCtx()
	ctx.stdin = os.Stdin
	ctx.stdout = os.Stdout
	ctx.stderr = os.Stderr

	l := fineline.NewLineReader()
	l.Prompt = "$ "
	l.SetMaxHistory(10)

	for {
		str, err := l.Read(nil)
		if err != nil {
			if err != io.EOF {
				fmt.Println("error", err)
			} else {
				fmt.Println()
			}
			break
		}
		parser := newParser(str + "\n")
		cmd := parser.parseCommand()
		cmd.exec(nil, ctx)
	}
}
<commit_msg>Basic tab completion<commit_after>package main

import (
	"fmt"
	"io"
	"os"

	"github.com/edsrzf/fineline"
)

func main() {
	ctx := newCtx()
	ctx.stdin = os.Stdin
	ctx.stdout = os.Stdout
	ctx.stderr = os.Stderr

	completer := &fineline.FilenameCompleter{"/"}

	l := fineline.NewLineReader(completer)
	l.Prompt = "$ "
	l.SetMaxHistory(10)

	for {
		str, err := l.Read()
		if err != nil {
			if err != io.EOF {
				fmt.Println("error", err)
			} else {
				fmt.Println()
			}
			break
		}
		parser := newParser(str + "\n")
		cmd := parser.parseCommand()
		cmd.exec(nil, ctx)
	}
}
<|endoftext|>


<commit_before>package shared

import "errors"

/*
Errors of Tinzenite.
*/
var (
	ErrUnsupported      = errors.New("feature currently unsupported")
	ErrIsTinzenite      = errors.New("already a Tinzenite directory")
	ErrNotTinzenite     = errors.New("path is not valid Tinzenite directory")
	ErrNoTinIgnore      = errors.New("no .tinignore file found")
	ErrUntracked        = errors.New("object is not tracked in the model")
	ErrNilInternalState = errors.New("internal state has illegal NIL values")
	ErrConflict         = errors.New("conflict, can not apply")
	ErrIllegalFileState = errors.New("illegal file state detected")
)

/*
Internal errors of Tinzenite.
*/
var (
	errWrongObject = errors.New("wrong ObjectInfo")
)

// constant value here
const (
	/*RANDOMSEEDLENGTH is the amount of bytes used as cryptographic hash seed.*/
	RANDOMSEEDLENGTH = 32
	/*IDMAXLENGTH is the length in chars of new random identification hashes.*/
	IDMAXLENGTH = 16
	/*KEYLENGTH is the length of the encryption key used for challenges and file encryption.*/
	KEYLENGTH = 256
	/*FILEPERMISSIONMODE used for all file operations.*/
	FILEPERMISSIONMODE = 0777
	/*CHUNKSIZE for hashing and encryption.*/
	CHUNKSIZE = 8 * 1024
)

// Path constants here
const (
	TINZENITEDIR  = ".tinzenite"
	TINIGNORE     = ".tinignore"
	DIRECTORYLIST = "directory.list"
	LOCALDIR      = "local"
	TEMPDIR       = "temp"
	RECEIVINGDIR  = "receiving"
	REMOVEDIR     = "removed"
	ORGDIR        = "org"
	PEERSDIR      = "peers"
	ENDING        = ".json"
	AUTHJSON      = "auth" + ENDING
	MODELJSON     = "model" + ENDING
	SELFPEERJSON  = "self" + ENDING
	BOOTJSON      = "boot" + ENDING
)
<commit_msg>const update<commit_after>package shared

import (
	"errors"
	"os"
)

/*
Errors of Tinzenite.
*/
var (
	ErrUnsupported      = errors.New("feature currently unsupported")
	ErrIsTinzenite      = errors.New("already a Tinzenite directory")
	ErrNotTinzenite     = errors.New("path is not valid Tinzenite directory")
	ErrNoTinIgnore      = errors.New("no .tinignore file found")
	ErrUntracked        = errors.New("object is not tracked in the model")
	ErrNilInternalState = errors.New("internal state has illegal NIL values")
	ErrConflict         = errors.New("conflict, can not apply")
	ErrIllegalFileState = errors.New("illegal file state detected")
)

/*
Internal errors of Tinzenite.
*/
var (
	errWrongObject = errors.New("wrong ObjectInfo")
)

// constant value here
const (
	/*RANDOMSEEDLENGTH is the amount of bytes used as cryptographic hash seed.*/
	RANDOMSEEDLENGTH = 32
	/*IDMAXLENGTH is the length in chars of new random identification hashes.*/
	IDMAXLENGTH = 16
	/*KEYLENGTH is the length of the encryption key used for challenges and file encryption.*/
	KEYLENGTH = 256
	/*FILEPERMISSIONMODE used for all file operations.*/
	FILEPERMISSIONMODE = 0777
	/*FILEFLAGCREATEAPPEND is the flag required to create a file or append to it if it already exists.*/
	FILEFLAGCREATEAPPEND = os.O_CREATE | os.O_RDWR | os.O_APPEND
	/*CHUNKSIZE for hashing and encryption.*/
	CHUNKSIZE = 8 * 1024
)

// Path constants here
const (
	TINZENITEDIR  = ".tinzenite"
	TINIGNORE     = ".tinignore"
	DIRECTORYLIST = "directory.list"
	LOCALDIR      = "local"
	TEMPDIR       = "temp"
	RECEIVINGDIR  = "receiving"
	REMOVEDIR     = "removed"
	ORGDIR        = "org"
	PEERSDIR      = "peers"
	ENDING        = ".json"
	AUTHJSON      = "auth" + ENDING
	MODELJSON     = "model" + ENDING
	SELFPEERJSON  = "self" + ENDING
	BOOTJSON      = "boot" + ENDING
)
<|endoftext|>


<commit_before>package main

import (
	"reflect"
	"testing"

	"github.com/davecgh/go-spew/spew"
	"github.com/nullbio/sqlboiler/bdb"
	"github.com/nullbio/sqlboiler/strmangle"
)

type fakeDB int

func (fakeDB) TableNames() ([]string, error) {
	return []string{"users", "videos", "contests", "notifications", "users_videos_tags"}, nil
}
func (fakeDB) Columns(tableName string) ([]bdb.Column, error) {
	return map[string][]bdb.Column{
		"users":    []bdb.Column{{Name: "id", Type: "int32"}},
		"contests": []bdb.Column{{Name: "id", Type: "int32", Nullable: true}},
		"videos": []bdb.Column{
			{Name: "id", Type: "int32"},
			{Name: "user_id", Type: "int32", Nullable: true},
			{Name: "contest_id", Type: "int32"},
		},
		"notifications": []bdb.Column{
			{Name: "user_id", Type: "int32"},
			{Name: "source_id", Type: "int32", Nullable: true},
		},
		"users_videos_tags": []bdb.Column{
			{Name: "user_id", Type: "int32"},
			{Name: "video_id", Type: "int32"},
		},
	}[tableName], nil
}
func (fakeDB) ForeignKeyInfo(tableName string) ([]bdb.ForeignKey, error) {
	return map[string][]bdb.ForeignKey{
		"videos": []bdb.ForeignKey{
			{Name: "videos_user_id_fk", Column: "user_id", ForeignTable: "users", ForeignColumn: "id"},
			{Name: "videos_contest_id_fk", Column: "contest_id", ForeignTable: "contests", ForeignColumn: "id"},
		},
		"notifications": []bdb.ForeignKey{
			{Name: "notifications_user_id_fk", Column: "user_id", ForeignTable: "users", ForeignColumn: "id"},
			{Name: "notifications_source_id_fk", Column: "source_id", ForeignTable: "users", ForeignColumn: "id"},
		},
		"users_videos_tags": []bdb.ForeignKey{
			{Name: "user_id_fk", Column: "user_id", ForeignTable: "users", ForeignColumn: "id"},
			{Name: "video_id_fk", Column: "video_id", ForeignTable: "videos", ForeignColumn: "id"},
		},
	}[tableName], nil
}
func (fakeDB) TranslateColumnType(c bdb.Column) bdb.Column {
	if c.Nullable {
		c.Type = "null." + strmangle.TitleCase(c.Type)
	}
	return c
}
func (fakeDB) PrimaryKeyInfo(tableName string) (*bdb.PrimaryKey, error) {
	return map[string]*bdb.PrimaryKey{
		"users_videos_tags": &bdb.PrimaryKey{
			Name:    "user_video_id_pkey",
			Columns: []string{"user_id", "video_id"},
		},
	}[tableName], nil
}
func (fakeDB) Open() error { return nil }
func (fakeDB) Close()      {}

func TestTextsFromForeignKey(t *testing.T) {
	t.Parallel()

	tables, err := bdb.Tables(fakeDB(0))
	if err != nil {
		t.Fatal(err)
	}

	videos := bdb.GetTable(tables, "videos")
	texts := textsFromForeignKey(tables, videos, videos.FKeys[0])
	expect := RelationshipToOneTexts{}

	expect.LocalTable.NameGo = "Video"
	expect.LocalTable.ColumnNameGo = "User"

	expect.ForeignTable.NameGo = "User"
	expect.ForeignTable.ColumnNameGo = "ID"

	expect.Function.Varname = "user"
	expect.Function.Receiver = "v"

	expect.Function.LocalAssignment = "UserID.Int32"
	expect.Function.ForeignAssignment = "ID"

	if !reflect.DeepEqual(expect, texts) {
		t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))
	}
}

func TestTextsFromRelationship(t *testing.T) {
	t.Parallel()

	tables, err := bdb.Tables(fakeDB(0))
	if err != nil {
		t.Fatal(err)
	}

	users := bdb.GetTable(tables, "users")
	texts := textsFromRelationship(tables, users, users.ToManyRelationships[0])
	expect := RelationshipToManyTexts{}
	expect.LocalTable.NameGo = "User"
	expect.LocalTable.NameSingular = "user"

	expect.ForeignTable.NameGo = "Video"
	expect.ForeignTable.NameSingular = "video"
	expect.ForeignTable.NamePluralGo = "Videos"
	expect.ForeignTable.NameHumanReadable = "videos"
	expect.ForeignTable.Slice = "videoSlice"

	expect.Function.Name = "Videos"
	expect.Function.Receiver = "u"
	expect.Function.LocalAssignment = "ID"
	expect.Function.ForeignAssignment = "UserID.Int32"

	if !reflect.DeepEqual(expect, texts) {
		t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))
	}

	texts = textsFromRelationship(tables, users, users.ToManyRelationships[1])
	expect = RelationshipToManyTexts{}
	expect.LocalTable.NameGo = "User"
	expect.LocalTable.NameSingular = "user"

	expect.ForeignTable.NameGo = "Notification"
	expect.ForeignTable.NameSingular = "notification"
	expect.ForeignTable.NamePluralGo = "Notifications"
	expect.ForeignTable.NameHumanReadable = "notifications"
	expect.ForeignTable.Slice = "notificationSlice"

	expect.Function.Name = "Notifications"
	expect.Function.Receiver = "u"
	expect.Function.LocalAssignment = "ID"
	expect.Function.ForeignAssignment = "UserID"

	if !reflect.DeepEqual(expect, texts) {
		t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))
	}

	texts = textsFromRelationship(tables, users, users.ToManyRelationships[2])
	expect = RelationshipToManyTexts{}
	expect.LocalTable.NameGo = "User"
	expect.LocalTable.NameSingular = "user"

	expect.ForeignTable.NameGo = "Notification"
	expect.ForeignTable.NameSingular = "notification"
	expect.ForeignTable.NamePluralGo = "Notifications"
	expect.ForeignTable.NameHumanReadable = "notifications"
	expect.ForeignTable.Slice = "notificationSlice"

	expect.Function.Name = "SourceNotifications"
	expect.Function.Receiver = "u"
	expect.Function.LocalAssignment = "ID"
	expect.Function.ForeignAssignment = "SourceID.Int32"

	if !reflect.DeepEqual(expect, texts) {
		t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))
	}

	texts = textsFromRelationship(tables, users, users.ToManyRelationships[3])
	expect = RelationshipToManyTexts{}
	expect.LocalTable.NameGo = "User"
	expect.LocalTable.NameSingular = "user"

	expect.ForeignTable.NameGo = "Video"
	expect.ForeignTable.NameSingular = "video"
	expect.ForeignTable.NamePluralGo = "Videos"
	expect.ForeignTable.NameHumanReadable = "videos"
	expect.ForeignTable.Slice = "videoSlice"

	expect.Function.Name = "Videos"
	expect.Function.Receiver = "u"
	expect.Function.LocalAssignment = "ID"
	expect.Function.ForeignAssignment = "ID"

	if !reflect.DeepEqual(expect, texts) {
		t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))
	}
}
<commit_msg>Fix Patrick's jackal test.<commit_after>package main

import (
	"reflect"
	"testing"

	"github.com/davecgh/go-spew/spew"
	"github.com/nullbio/sqlboiler/bdb"
	"github.com/nullbio/sqlboiler/strmangle"
)

type fakeDB int

func (fakeDB) TableNames() ([]string, error) {
	return []string{"users", "videos", "contests", "notifications", "users_videos_tags"}, nil
}
func (fakeDB) Columns(tableName string) ([]bdb.Column, error) {
	return map[string][]bdb.Column{
		"users":    []bdb.Column{{Name: "id", Type: "int32"}},
		"contests": []bdb.Column{{Name: "id", Type: "int32", Nullable: true}},
		"videos": []bdb.Column{
			{Name: "id", Type: "int32"},
			{Name: "user_id", Type: "int32", Nullable: true},
			{Name: "contest_id", Type: "int32"},
		},
		"notifications": []bdb.Column{
			{Name: "user_id", Type: "int32"},
			{Name: "source_id", Type: "int32", Nullable: true},
		},
		"users_videos_tags": []bdb.Column{
			{Name: "user_id", Type: "int32"},
			{Name: "video_id", Type: "int32"},
		},
	}[tableName], nil
}
func (fakeDB) ForeignKeyInfo(tableName string) ([]bdb.ForeignKey, error) {
	return map[string][]bdb.ForeignKey{
		"videos": []bdb.ForeignKey{
			{Name: "videos_user_id_fk", Column: "user_id", ForeignTable: "users", ForeignColumn: "id"},
			{Name: "videos_contest_id_fk", Column: "contest_id", ForeignTable: "contests", ForeignColumn: "id"},
		},
		"notifications": []bdb.ForeignKey{
			{Name: "notifications_user_id_fk", Column: "user_id", ForeignTable: "users", ForeignColumn: "id"},
			{Name: "notifications_source_id_fk", Column: "source_id", ForeignTable: "users", ForeignColumn: "id"},
		},
		"users_videos_tags": []bdb.ForeignKey{
			{Name: "user_id_fk", Column: "user_id", ForeignTable: "users", ForeignColumn: "id"},
			{Name: "video_id_fk", Column: "video_id", ForeignTable: "videos", ForeignColumn: "id"},
		},
	}[tableName], nil
}
func (fakeDB) TranslateColumnType(c bdb.Column) bdb.Column {
	if c.Nullable {
		c.Type = "null." + strmangle.TitleCase(c.Type)
	}
	return c
}
func (fakeDB) PrimaryKeyInfo(tableName string) (*bdb.PrimaryKey, error) {
	return map[string]*bdb.PrimaryKey{
		"users_videos_tags": &bdb.PrimaryKey{
			Name:    "user_video_id_pkey",
			Columns: []string{"user_id", "video_id"},
		},
	}[tableName], nil
}
func (fakeDB) Open() error { return nil }
func (fakeDB) Close()      {}

func TestTextsFromForeignKey(t *testing.T) {
	t.Parallel()

	tables, err := bdb.Tables(fakeDB(0))
	if err != nil {
		t.Fatal(err)
	}

	videos := bdb.GetTable(tables, "videos")
	texts := textsFromForeignKey(tables, videos, videos.FKeys[0])
	expect := RelationshipToOneTexts{}

	expect.LocalTable.NameGo = "Video"
	expect.LocalTable.ColumnNameGo = "User"

	expect.ForeignTable.NameGo = "User"
	expect.ForeignTable.ColumnNameGo = "ID"

	expect.Function.Varname = "user"
	expect.Function.Receiver = "v"

	expect.Function.LocalAssignment = "UserID.Int32"
	expect.Function.ForeignAssignment = "ID"

	if !reflect.DeepEqual(expect, texts) {
		t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))
	}
}

func TestTextsFromRelationship(t *testing.T) {
	t.Parallel()

	tables, err := bdb.Tables(fakeDB(0))
	if err != nil {
		t.Fatal(err)
	}

	users := bdb.GetTable(tables, "users")
	texts := textsFromRelationship(tables, users, users.ToManyRelationships[0])
	expect := RelationshipToManyTexts{}
	expect.LocalTable.NameGo = "User"
	expect.LocalTable.NameSingular = "user"

	expect.ForeignTable.NameGo = "Video"
	expect.ForeignTable.NameSingular = "video"
	expect.ForeignTable.NamePluralGo = "Videos"
	expect.ForeignTable.NameHumanReadable = "videos"
	expect.ForeignTable.Slice = "VideoSlice"

	expect.Function.Name = "Videos"
	expect.Function.Receiver = "u"
	expect.Function.LocalAssignment = "ID"
	expect.Function.ForeignAssignment = "UserID.Int32"

	if !reflect.DeepEqual(expect, texts) {
		t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))
	}

	texts = textsFromRelationship(tables, users, users.ToManyRelationships[1])
	expect = RelationshipToManyTexts{}
	expect.LocalTable.NameGo = "User"
	expect.LocalTable.NameSingular = "user"

	expect.ForeignTable.NameGo = "Notification"
	expect.ForeignTable.NameSingular = "notification"
	expect.ForeignTable.NamePluralGo = "Notifications"
	expect.ForeignTable.NameHumanReadable = "notifications"
	expect.ForeignTable.Slice = "NotificationSlice"

	expect.Function.Name = "Notifications"
	expect.Function.Receiver = "u"
	expect.Function.LocalAssignment = "ID"
	expect.Function.ForeignAssignment = "UserID"

	if !reflect.DeepEqual(expect, texts) {
		t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))
	}

	texts = textsFromRelationship(tables, users, users.ToManyRelationships[2])
	expect = RelationshipToManyTexts{}
	expect.LocalTable.NameGo = "User"
	expect.LocalTable.NameSingular = "user"

	expect.ForeignTable.NameGo = "Notification"
	expect.ForeignTable.NameSingular = "notification"
	expect.ForeignTable.NamePluralGo = "Notifications"
	expect.ForeignTable.NameHumanReadable = "notifications"
	expect.ForeignTable.Slice = "NotificationSlice"

	expect.Function.Name = "SourceNotifications"
	expect.Function.Receiver = "u"
	expect.Function.LocalAssignment = "ID"
	expect.Function.ForeignAssignment = "SourceID.Int32"

	if !reflect.DeepEqual(expect, texts) {
		t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))
	}

	texts = textsFromRelationship(tables, users, users.ToManyRelationships[3])
	expect = RelationshipToManyTexts{}
	expect.LocalTable.NameGo = "User"
	expect.LocalTable.NameSingular = "user"

	expect.ForeignTable.NameGo = "Video"
	expect.ForeignTable.NameSingular = "video"
	expect.ForeignTable.NamePluralGo = "Videos"
	expect.ForeignTable.NameHumanReadable = "videos"
	expect.ForeignTable.Slice = "VideoSlice"

	expect.Function.Name = "Videos"
	expect.Function.Receiver = "u"
	expect.Function.LocalAssignment = "ID"
	expect.Function.ForeignAssignment = "ID"

	if !reflect.DeepEqual(expect, texts) {
		t.Errorf("Want:\n%s\nGot:\n%s\n", spew.Sdump(expect), spew.Sdump(texts))
	}
}
<|endoftext|>


<commit_before>package main

import "github.com/genghisjahn/textlength"
import "fmt"
import "net/http"
import "html/template"
import "time"
import "strings"

var items []textlength.TextLengthItem

func main() {
	items, _ = textlength.BuildItems(10000)
	http.HandleFunc("/process/", processHandler)
	http.HandleFunc("/query/", queryHandler)
	http.ListenAndServe("localhost:8888", nil)
}

func processHandler(rw http.ResponseWriter, req *http.Request) {
	text := strings.TrimSpace(req.PostFormValue("text"))
	fmt.Printf("Processing text: {%v} at %v.\n", text, time.Now())
	result, _ := textlength.ProcessText(text, items)
	fmt.Fprintf(rw, "%v", result)
	fmt.Printf("Returned {%v} at %v.\n", result, time.Now())
}

func queryHandler(rw http.ResponseWriter, req *http.Request) {
	fmt.Printf("Request recieved for index.html at %v\n", time.Now())
	t, _ := template.ParseFiles("index.html")
	t.Execute(rw, t)
}
<commit_msg>added some start up instructions<commit_after>package main

import "github.com/genghisjahn/textlength"
import "fmt"
import "net/http"
import "html/template"
import "time"
import "strings"

var items []textlength.TextLengthItem

func main() {
	items, _ = textlength.BuildItems(10000)
	fmt.Printf("Text Length web server is running. %v\n", time.Now())
	fmt.Printf("Navigate your browser to http://localhost:8888/query/\n")
	http.HandleFunc("/process/", processHandler)
	http.HandleFunc("/query/", queryHandler)
	http.ListenAndServe("localhost:8888", nil)
}

func processHandler(rw http.ResponseWriter, req *http.Request) {
	text := strings.TrimSpace(req.PostFormValue("text"))
	fmt.Printf("Processing text: {%v} at %v.\n", text, time.Now())
	result, _ := textlength.ProcessText(text, items)
	fmt.Fprintf(rw, "%v", result)
	fmt.Printf("Returned {%v} at %v.\n", result, time.Now())
}

func queryHandler(rw http.ResponseWriter, req *http.Request) {
	fmt.Printf("Request recieved for index.html at %v\n", time.Now())
	t, _ := template.ParseFiles("index.html")
	t.Execute(rw, t)
}
<|endoftext|>


<commit_before>package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"strings"
	"time"
)

func doHTTP(url string, input io.Reader) (io.ReadCloser, error) {
	method := "GET"
	if input != nil {
		method = "POST"
	}
	rq, err := http.NewRequest(method, url, input)
	if err != nil {
		return nil, err
	}
	rq.Header.Set("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)")
	if input != nil {
		rq.Header.Set("Content-Type", "application/json")
	}
	rsp, err := http.DefaultClient.Do(rq)
	if err != nil {
		return nil, err
	}
	if rsp.StatusCode != http.StatusOK {
		rsp.Body.Close()
		return nil, fmt.Errorf("got %s fetching %s", rsp.Status, url)
	}
	return rsp.Body, nil
}

func tryHTTP(url string, baseDelay time.Duration, loops int,
	input io.Reader) (io.ReadCloser, error) {

	delay := baseDelay
	for {
		output, err := doHTTP(url, input)
		if err == nil {
			return output, nil
		}
		fmt.Printf("fetching failed with: %s\n", err)
		loops -= 1
		if loops <= 0 {
			return nil, err
		}
		time.Sleep(delay)
		delay *= 2
	}
}

func doJson(url string, baseDelay time.Duration, loops int, input interface{},
	output interface{}) error {

	var post io.Reader
	if input != nil {
		body := &bytes.Buffer{}
		err := json.NewEncoder(body).Encode(input)
		if err != nil {
			return err
		}
		post = body
	}
	result, err := tryHTTP(url, baseDelay, loops, post)
	if err != nil {
		return err
	}
	defer result.Close()
	return json.NewDecoder(result).Decode(output)
}

type SearchPaging struct {
	Range      int `json:"range"`
	StartIndex int `json:"startIndex"`
}

type SearchSorts struct {
	Direction string `json:"direction"`
	Type      string `json:"type"`
}

type SearchFilters struct {
	EnableFilter    bool          `json:"activeFiltre"`
	Functions       []int         `json:"fonctions"`
	Places          []int         `json:"lieux"`
	Keywords        string        `json:"motsCles"`
	Experience      []int         `json:"niveauxExperience"`
	Paging          SearchPaging  `json:"pagination"`
	MinSalary       int           `json:"salaireMinimum"`
	MaxSalary       int           `json:"salaireMaximum"`
	Sectors         []int         `json:"secteursActivite"`
	Sorts           []SearchSorts `json:"sorts"`
	ClientType      string        `json:"typeClient"`
	ContractTypes   []int         `json:"typesContrat"`
	ConventionTypes []int         `json:"typesConvention"`
}

func searchOffers(start, count, minSalary int, locations []int) ([]string, error) {
	filter := &SearchFilters{
		EnableFilter: true,
		Functions:    []int{},
		Places:       locations,
		Experience:   []int{},
		Paging: SearchPaging{
			Range:      count,
			StartIndex: start,
		},
		MinSalary: minSalary,
		MaxSalary: 1000,
		Sectors:   []int{},
		Sorts: []SearchSorts{
			{
				Direction: "DESCENDING",
				Type:      "DATE",
			},
		},
		ClientType:      "CADRE",
		ContractTypes:   []int{},
		ConventionTypes: []int{},
	}
	results := &struct {
		Results []struct {
			URI string `json:"@uriOffre"`
		} `json:"resultats"`
	}{}
	url := "https://cadres.apec.fr/cms/webservices/rechercheOffre/ids"
	err := doJson(url, 5*time.Second, 5, filter, results)
	if err != nil {
		return nil, err
	}
	ids := []string{}
	for _, uri := range results.Results {
		parts := strings.Split(uri.URI, "numeroOffre=")
		if len(parts) != 2 {
			return nil, fmt.Errorf("invalid offer identifier: %s", uri.URI)
		}
		ids = append(ids, parts[1])
	}
	return ids, nil
}

func getOffer(id string) ([]byte, error) {
	u := "https://cadres.apec.fr/cms/webservices/offre/public?numeroOffre=" + id
	output, err := tryHTTP(u, time.Second, 5, nil)
	if err != nil {
		return nil, err
	}
	defer output.Close()
	return ioutil.ReadAll(output)
}

func enumerateOffers(minSalary int, locations []int, callback func([]string) error) error {
	start := 0
	count := 250
	delay := 5 * time.Second
	for ; ; time.Sleep(delay) {
		fmt.Printf("fetching from %d to %d\n", start, start+count)
		ids, err := searchOffers(start, count, minSalary, locations)
		if err != nil {
			return err
		}
		start += count
		err = callback(ids)
		if err != nil {
			return err
		}
		if len(ids) < count {
			break
		}
	}
	return nil
}

var (
	crawlCmd       = app.Command("crawl", "crawl APEC offers")
	crawlMinSalary = crawlCmd.Arg("min-salary", "minimum salary in kEUR").Default("50").Int()
	crawlLocations = crawlCmd.Flag("location", "offer location code").Ints()
)

func crawlOffers(cfg *Config) error {
	store, err := OpenStore(cfg.Store())
	if err != nil {
		return err
	}
	added, deleted := 0, 0
	seen := map[string]bool{}
	err = enumerateOffers(*crawlMinSalary, *crawlLocations, func(ids []string) error {
		for _, id := range ids {
			seen[id] = true
			ok, err := store.Has(id)
			if err != nil {
				return err
			}
			if ok {
				continue
			}
			fmt.Printf("fetching %s\n", id)
			data, err := getOffer(id)
			if err != nil {
				return err
			}
			time.Sleep(time.Second)
			err = store.Put(id, data)
			if err != nil {
				return err
			}
			added += 1
		}
		return nil
	})
	if err != nil {
		return err
	}
	ids, err := store.List()
	if err != nil {
		return err
	}
	for _, id := range ids {
		if !seen[id] {
			fmt.Printf("deleting %s\n", id)
			store.Delete(id)
			deleted += 1
		}
	}
	fmt.Printf("%d added, %d deleted, %d total\n", added, deleted, store.Size())
	return nil
}
<commit_msg>crawl: change default min salary to 0<commit_after>package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"strings"
	"time"
)

func doHTTP(url string, input io.Reader) (io.ReadCloser, error) {
	method := "GET"
	if input != nil {
		method = "POST"
	}
	rq, err := http.NewRequest(method, url, input)
	if err != nil {
		return nil, err
	}
	rq.Header.Set("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)")
	if input != nil {
		rq.Header.Set("Content-Type", "application/json")
	}
	rsp, err := http.DefaultClient.Do(rq)
	if err != nil {
		return nil, err
	}
	if rsp.StatusCode != http.StatusOK {
		rsp.Body.Close()
		return nil, fmt.Errorf("got %s fetching %s", rsp.Status, url)
	}
	return rsp.Body, nil
}

func tryHTTP(url string, baseDelay time.Duration, loops int,
	input io.Reader) (io.ReadCloser, error) {

	delay := baseDelay
	for {
		output, err := doHTTP(url, input)
		if err == nil {
			return output, nil
		}
		fmt.Printf("fetching failed with: %s\n", err)
		loops -= 1
		if loops <= 0 {
			return nil, err
		}
		time.Sleep(delay)
		delay *= 2
	}
}

func doJson(url string, baseDelay time.Duration, loops int, input interface{},
	output interface{}) error {

	var post io.Reader
	if input != nil {
		body := &bytes.Buffer{}
		err := json.NewEncoder(body).Encode(input)
		if err != nil {
			return err
		}
		post = body
	}
	result, err := tryHTTP(url, baseDelay, loops, post)
	if err != nil {
		return err
	}
	defer result.Close()
	return json.NewDecoder(result).Decode(output)
}

type SearchPaging struct {
	Range      int `json:"range"`
	StartIndex int `json:"startIndex"`
}

type SearchSorts struct {
	Direction string `json:"direction"`
	Type      string `json:"type"`
}

type SearchFilters struct {
	EnableFilter    bool          `json:"activeFiltre"`
	Functions       []int         `json:"fonctions"`
	Places          []int         `json:"lieux"`
	Keywords        string        `json:"motsCles"`
	Experience      []int         `json:"niveauxExperience"`
	Paging          SearchPaging  `json:"pagination"`
	MinSalary       int           `json:"salaireMinimum"`
	MaxSalary       int           `json:"salaireMaximum"`
	Sectors         []int         `json:"secteursActivite"`
	Sorts           []SearchSorts `json:"sorts"`
	ClientType      string        `json:"typeClient"`
	ContractTypes   []int         `json:"typesContrat"`
	ConventionTypes []int         `json:"typesConvention"`
}

func searchOffers(start, count, minSalary int, locations []int) ([]string, error) {
	filter := &SearchFilters{
		EnableFilter: true,
		Functions:    []int{},
		Places:       locations,
		Experience:   []int{},
		Paging: SearchPaging{
			Range:      count,
			StartIndex: start,
		},
		MinSalary: minSalary,
		MaxSalary: 1000,
		Sectors:   []int{},
		Sorts: []SearchSorts{
			{
				Direction: "DESCENDING",
				Type:      "DATE",
			},
		},
		ClientType:      "CADRE",
		ContractTypes:   []int{},
		ConventionTypes: []int{},
	}
	results := &struct {
		Results []struct {
			URI string `json:"@uriOffre"`
		} `json:"resultats"`
	}{}
	url := "https://cadres.apec.fr/cms/webservices/rechercheOffre/ids"
	err := doJson(url, 5*time.Second, 5, filter, results)
	if err != nil {
		return nil, err
	}
	ids := []string{}
	for _, uri := range results.Results {
		parts := strings.Split(uri.URI, "numeroOffre=")
		if len(parts) != 2 {
			return nil, fmt.Errorf("invalid offer identifier: %s", uri.URI)
		}
		ids = append(ids, parts[1])
	}
	return ids, nil
}

func getOffer(id string) ([]byte, error) {
	u := "https://cadres.apec.fr/cms/webservices/offre/public?numeroOffre=" + id
	output, err := tryHTTP(u, time.Second, 5, nil)
	if err != nil {
		return nil, err
	}
	defer output.Close()
	return ioutil.ReadAll(output)
}

func enumerateOffers(minSalary int, locations []int, callback func([]string) error) error {
	start := 0
	count := 250
	delay := 5 * time.Second
	for ; ; time.Sleep(delay) {
		fmt.Printf("fetching from %d to %d\n", start, start+count)
		ids, err := searchOffers(start, count, minSalary, locations)
		if err != nil {
			return err
		}
		start += count
		err = callback(ids)
		if err != nil {
			return err
		}
		if len(ids) < count {
			break
		}
	}
	return nil
}

var (
	crawlCmd       = app.Command("crawl", "crawl APEC offers")
	crawlMinSalary = crawlCmd.Arg("min-salary", "minimum salary in kEUR").Default("0").Int()
	crawlLocations = crawlCmd.Flag("location", "offer location code").Ints()
)

func crawlOffers(cfg *Config) error {
	store, err := OpenStore(cfg.Store())
	if err != nil {
		return err
	}
	added, deleted := 0, 0
	seen := map[string]bool{}
	err = enumerateOffers(*crawlMinSalary, *crawlLocations, func(ids []string) error {
		for _, id := range ids {
			seen[id] = true
			ok, err := store.Has(id)
			if err != nil {
				return err
			}
			if ok {
				continue
			}
			fmt.Printf("fetching %s\n", id)
			data, err := getOffer(id)
			if err != nil {
				return err
			}
			time.Sleep(time.Second)
			err = store.Put(id, data)
			if err != nil {
				return err
			}
			added += 1
		}
		return nil
	})
	if err != nil {
		return err
	}
	ids, err := store.List()
	if err != nil {
		return err
	}
	for _, id := range ids {
		if !seen[id] {
			fmt.Printf("deleting %s\n", id)
			store.Delete(id)
			deleted += 1
		}
	}
	fmt.Printf("%d added, %d deleted, %d total\n", added, deleted, store.Size())
	return nil
}
<|endoftext|>


<commit_before>package gockle

import (
	"fmt"

	"github.com/maraino/go-mock"
)

var mySession = &SessionMock{}

func ExampleIteratorScanMap() {
	var iteratorMock = &IteratorMock{}

	iteratorMock.When("ScanMap", mock.Any).Call(func(m map[string]interface{}) bool {
		m["id"] = 1
		m["name"] = "alex"

		return false
	})

	iteratorMock.When("Close").Return(nil)

	var sessionMock = &SessionMock{}

	const query = "select * from users"

	sessionMock.When("ScanIterator", query, mock.Any).Return(iteratorMock)
	sessionMock.When("Close").Return()

	var session Session = sessionMock
	var iterator = session.ScanIterator(query)
	var row = map[string]interface{}{}

	for more := true; more; {
		more = iterator.ScanMap(row)

		fmt.Printf("id = %v, name = %v\n", row["id"], row["name"])
	}

	if err := iterator.Close(); err != nil {
		fmt.Println(err)
	}

	session.Close()

	// Output: id = 1, name = alex
}

func ExampleSessionBatch() {
	var batchMock = &BatchMock{}

	batchMock.When("Add", "insert into users (id, name) values (1, 'alex')", mock.Any).Return()
	batchMock.When("Exec").Return(fmt.Errorf("invalid"))

	var sessionMock = &SessionMock{}

	sessionMock.When("Batch", BatchLogged).Return(batchMock)
	sessionMock.When("Close").Return()

	var session Session = sessionMock
	var batch = session.Batch(BatchLogged)

	batch.Add("insert into users (id, name) values (1, 'alex')")

	if err := batch.Exec(); err != nil {
		fmt.Println(err)
	}

	session.Close()

	// Output: invalid
}

func ExampleSessionScanMapSlice() {
	var sessionMock = &SessionMock{}

	const query = "select * from users"

	sessionMock.When("ScanMapSlice", query, mock.Any).Return([]map[string]interface{}{{"id": 1, "name": "alex"}}, nil)
	sessionMock.When("Close").Return()

	var session Session = sessionMock
	var rows, _ = session.ScanMapSlice(query)

	for _, row := range rows {
		fmt.Printf("id = %v, name = %v\n", row["id"], row["name"])
	}

	session.Close()

	// Output: id = 1, name = alex
}
<commit_msg>Vet: Fix example names to match code<commit_after>package gockle

import (
	"fmt"

	"github.com/maraino/go-mock"
)

var mySession = &SessionMock{}

func ExampleIterator_ScanMap() {
	var iteratorMock = &IteratorMock{}

	iteratorMock.When("ScanMap", mock.Any).Call(func(m map[string]interface{}) bool {
		m["id"] = 1
		m["name"] = "alex"

		return false
	})

	iteratorMock.When("Close").Return(nil)

	var sessionMock = &SessionMock{}

	const query = "select * from users"

	sessionMock.When("ScanIterator", query, mock.Any).Return(iteratorMock)
	sessionMock.When("Close").Return()

	var session Session = sessionMock
	var iterator = session.ScanIterator(query)
	var row = map[string]interface{}{}

	for more := true; more; {
		more = iterator.ScanMap(row)

		fmt.Printf("id = %v, name = %v\n", row["id"], row["name"])
	}

	if err := iterator.Close(); err != nil {
		fmt.Println(err)
	}

	session.Close()

	// Output: id = 1, name = alex
}

func ExampleSession_Batch() {
	var batchMock = &BatchMock{}

	batchMock.When("Add", "insert into users (id, name) values (1, 'alex')", mock.Any).Return()
	batchMock.When("Exec").Return(fmt.Errorf("invalid"))

	var sessionMock = &SessionMock{}

	sessionMock.When("Batch", BatchLogged).Return(batchMock)
	sessionMock.When("Close").Return()

	var session Session = sessionMock
	var batch = session.Batch(BatchLogged)

	batch.Add("insert into users (id, name) values (1, 'alex')")

	if err := batch.Exec(); err != nil {
		fmt.Println(err)
	}

	session.Close()

	// Output: invalid
}

func ExampleSession_ScanMapSlice() {
	var sessionMock = &SessionMock{}

	const query = "select * from users"

	sessionMock.When("ScanMapSlice", query, mock.Any).Return([]map[string]interface{}{{"id": 1, "name": "alex"}}, nil)
	sessionMock.When("Close").Return()

	var session Session = sessionMock
	var rows, _ = session.ScanMapSlice(query)

	for _, row := range rows {
		fmt.Printf("id = %v, name = %v\n", row["id"], row["name"])
	}

	session.Close()

	// Output: id = 1, name = alex
}
<|endoftext|>


<commit_before>package linkheader_test

import (
	"fmt"

	"github.com/tomnomnom/linkheader"
)

func ExampleParse() {
	header := "<https://api.github.com/user/58276/repos?page=2>; rel=\"next\"," +
		"<https://api.github.com/user/58276/repos?page=2>; rel=\"last\""
	links := linkheader.Parse(header)

	for _, link := range links {
		fmt.Printf("URL: %s; Rel: %s\n", link.URL, link.Rel)
	}

	// Output:
	// URL: https://api.github.com/user/58276/repos?page=2; Rel: next
	// URL: https://api.github.com/user/58276/repos?page=2; Rel: last
}

func ExampleParseMultiple() {
	headers := []string{
		"<https://api.github.com/user/58276/repos?page=2>; rel=\"next\"",
		"<https://api.github.com/user/58276/repos?page=2>; rel=\"last\"",
	}
	links := linkheader.ParseMultiple(headers)

	for _, link := range links {
		fmt.Printf("URL: %s; Rel: %s\n", link.URL, link.Rel)
	}

	// Output:
	// URL: https://api.github.com/user/58276/repos?page=2; Rel: next
	// URL: https://api.github.com/user/58276/repos?page=2; Rel: last
}

func ExampleLinks_FilterByRel() {
	header := "<https://api.github.com/user/58276/repos?page=2>; rel=\"next\"," +
		"<https://api.github.com/user/58276/repos?page=2>; rel=\"last\""
	links := linkheader.Parse(header)

	for _, link := range links.FilterByRel("last") {
		fmt.Printf("URL: %s; Rel: %s\n", link.URL, link.Rel)
	}

	// Output:
	// URL: https://api.github.com/user/58276/repos?page=2; Rel: last

}
<commit_msg>Adds example for link string method<commit_after>package linkheader_test

import (
	"fmt"

	"github.com/tomnomnom/linkheader"
)

func ExampleParse() {
	header := "<https://api.github.com/user/58276/repos?page=2>; rel=\"next\"," +
		"<https://api.github.com/user/58276/repos?page=2>; rel=\"last\""
	links := linkheader.Parse(header)

	for _, link := range links {
		fmt.Printf("URL: %s; Rel: %s\n", link.URL, link.Rel)
	}

	// Output:
	// URL: https://api.github.com/user/58276/repos?page=2; Rel: next
	// URL: https://api.github.com/user/58276/repos?page=2; Rel: last
}

func ExampleParseMultiple() {
	headers := []string{
		"<https://api.github.com/user/58276/repos?page=2>; rel=\"next\"",
		"<https://api.github.com/user/58276/repos?page=2>; rel=\"last\"",
	}
	links := linkheader.ParseMultiple(headers)

	for _, link := range links {
		fmt.Printf("URL: %s; Rel: %s\n", link.URL, link.Rel)
	}

	// Output:
	// URL: https://api.github.com/user/58276/repos?page=2; Rel: next
	// URL: https://api.github.com/user/58276/repos?page=2; Rel: last
}

func ExampleLinks_FilterByRel() {
	header := "<https://api.github.com/user/58276/repos?page=2>; rel=\"next\"," +
		"<https://api.github.com/user/58276/repos?page=2>; rel=\"last\""
	links := linkheader.Parse(header)

	for _, link := range links.FilterByRel("last") {
		fmt.Printf("URL: %s; Rel: %s\n", link.URL, link.Rel)
	}

	// Output:
	// URL: https://api.github.com/user/58276/repos?page=2; Rel: last

}

func ExampleLink_String() {
	link := linkheader.Link{
		URL: "http://example.com/page/2",
		Rel: "next",
	}

	fmt.Printf("Link: %s\n", link.String())

	// Output:
	// Link: <http://example.com/page/2>; rel="next"
}
<|endoftext|>


<commit_before>package handlers_test

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
	"net/http/httptest"

	"github.com/cloudfoundry/hm9000/models"
	"github.com/cloudfoundry/hm9000/testhelpers/appfixture"
	"github.com/cloudfoundry/storeadapter/fakestoreadapter"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

func decodeBulkResponse(response string) (bulkAppResp map[string]AppResponse) {
	err := json.Unmarshal([]byte(response), &bulkAppResp)
	Expect(err).NotTo(HaveOccurred())
	return
}

var _ = Describe("BulkAppState", func() {
	Context("when the store has an unexpected error", func() {
		It("should return an empty hash", func() {
			conf := defaultConf()
			conf.StoreAdapter.GetErrInjector = fakestoreadapter.NewFakeStoreAdapterErrorInjector("desired", fmt.Errorf("No desired state for you!"))

			handler, store, err := makeHandlerAndStore(conf)
			Expect(err).ToNot(HaveOccurred())
			freshenTheStore(store)

			request, _ := http.NewRequest("POST", "/bulk_app_state", bytes.NewBufferString(`[{"droplet":"something","version":"whatever"}]`))
			response := httptest.NewRecorder()
			handler.ServeHTTP(response, request)

			Expect(response.Body.String()).To(Equal("{}"))
		})
	})

	Context("when the store is not fresh", func() {
		It("returns an empty hash", func() {
			conf := defaultConf()
			app := appfixture.NewAppFixture()

			handler, store, err := makeHandlerAndStore(conf)
			Expect(err).ToNot(HaveOccurred())

			crashCount := models.CrashCount{
				AppGuid:       app.AppGuid,
				AppVersion:    app.AppVersion,
				InstanceIndex: 1,
				CrashCount:    2,
			}
			store.SyncDesiredState(app.DesiredState(3))
			store.SyncHeartbeats(app.Heartbeat(3))
			store.SaveCrashCounts(crashCount)

			request_body := fmt.Sprintf(`[{"droplet":"%s","version":"%s"}]`, app.AppGuid, app.AppVersion)
			request, _ := http.NewRequest("POST", "/bulk_app_state", bytes.NewBufferString(request_body))
			response := httptest.NewRecorder()
			handler.ServeHTTP(response, request)

			Expect(response.Body.String()).To(Equal("{}"))
		})
	})

	Context("when the store is fresh", func() {
		It("returns an empty hash when invalid request json is provided", func() {
			conf := defaultConf()
			app := appfixture.NewAppFixture()

			handler, store, err := makeHandlerAndStore(conf)
			Expect(err).ToNot(HaveOccurred())

			crashCount := models.CrashCount{
				AppGuid:       app.AppGuid,
				AppVersion:    app.AppVersion,
				InstanceIndex: 1,
				CrashCount:    2,
			}
			store.SyncDesiredState(app.DesiredState(3))
			store.SyncHeartbeats(app.Heartbeat(3))
			store.SaveCrashCounts(crashCount)
			freshenTheStore(store)

			request, _ := http.NewRequest("POST", "/bulk_app_state", bytes.NewBufferString("asdf{}"))
			response := httptest.NewRecorder()
			handler.ServeHTTP(response, request)

			Expect(response.Body.String()).To(Equal("{}"))
		})

		Context("when the app query parameters do not correspond to an existing app", func() {
			It("returns empty hash", func() {
				conf := defaultConf()
				handler, store, err := makeHandlerAndStore(conf)
				Expect(err).ToNot(HaveOccurred())
				freshenTheStore(store)

				request, _ := http.NewRequest("POST", "/bulk_app_state", bytes.NewBufferString(`[{"droplet":"elephant","version":"pink-flamingo"}]`))
				response := httptest.NewRecorder()
				handler.ServeHTTP(response, request)

				Expect(response.Body.String()).To(Equal("{}"))
			})
		})

		Context("when the app query parameters correspond to an existing app", func() {
			It("should return the actual instances and crashes of the app", func() {
				conf := defaultConf()
				app := appfixture.NewAppFixture()

				handler, store, err := makeHandlerAndStore(conf)
				Expect(err).ToNot(HaveOccurred())

				crashCount := models.CrashCount{
					AppGuid:       app.AppGuid,
					AppVersion:    app.AppVersion,
					InstanceIndex: 1,
					CrashCount:    2,
				}
				store.SyncDesiredState(app.DesiredState(3))
				store.SyncHeartbeats(app.Heartbeat(3))
				store.SaveCrashCounts(crashCount)
				freshenTheStore(store)

				request_body := fmt.Sprintf(`[{"droplet":"%s","version":"%s"}]`, app.AppGuid, app.AppVersion)
				request, _ := http.NewRequest("POST", "/bulk_app_state", bytes.NewBufferString(request_body))
				response := httptest.NewRecorder()
				handler.ServeHTTP(response, request)

				expectedInstanceHeartbeats := []models.InstanceHeartbeat{
					app.InstanceAtIndex(0).Heartbeat(),
					app.InstanceAtIndex(1).Heartbeat(),
					app.InstanceAtIndex(2).Heartbeat(),
				}
				expectedApp := AppResponse{
					AppGuid:            app.AppGuid,
					AppVersion:         app.AppVersion,
					Desired:            app.DesiredState(3),
					InstanceHeartbeats: expectedInstanceHeartbeats,
					CrashCounts:        []models.CrashCount{crashCount},
				}

				decodedResponse := decodeBulkResponse(response.Body.String())
				Expect(decodedResponse).To(HaveLen(1))
				Expect(decodedResponse).To(HaveKey(expectedApp.AppGuid))
				receivedApp := decodedResponse[expectedApp.AppGuid]
				Expect(receivedApp.AppGuid).To(Equal(expectedApp.AppGuid))
				Expect(receivedApp.AppVersion).To(Equal(expectedApp.AppVersion))
				Expect(receivedApp.Desired).To(Equal(expectedApp.Desired))
				Expect(receivedApp.InstanceHeartbeats).To(ConsistOf(expectedApp.InstanceHeartbeats))
				Expect(receivedApp.CrashCounts).To(ConsistOf(expectedApp.CrashCounts))
			})
		})

		Context("when some of the apps are not found", func() {
			It("responds with the apps that are present", func() {
				conf := defaultConf()
				app := appfixture.NewAppFixture()

				handler, store, err := makeHandlerAndStore(conf)
				Expect(err).ToNot(HaveOccurred())
				freshenTheStore(store)

				crashCount := models.CrashCount{
					AppGuid:       app.AppGuid,
					AppVersion:    app.AppVersion,
					InstanceIndex: 1,
					CrashCount:    2,
				}
				store.SyncDesiredState(app.DesiredState(3))
				store.SyncHeartbeats(app.Heartbeat(3))
				store.SaveCrashCounts(crashCount)

				requestBody := fmt.Sprintf(`[{"droplet":"%s","version":"%s"},{"droplet":"jam-sandwich","version":"123"}]`, app.AppGuid, app.AppVersion)
				request, _ := http.NewRequest("POST", "/bulk_app_state", bytes.NewBufferString(requestBody))
				response := httptest.NewRecorder()
				handler.ServeHTTP(response, request)

				expectedInstanceHeartbeats := []models.InstanceHeartbeat{
					app.InstanceAtIndex(0).Heartbeat(),
					app.InstanceAtIndex(1).Heartbeat(),
					app.InstanceAtIndex(2).Heartbeat(),
				}
				expectedApp := AppResponse{
					AppGuid:            app.AppGuid,
					AppVersion:         app.AppVersion,
					Desired:            app.DesiredState(3),
					InstanceHeartbeats: expectedInstanceHeartbeats,
					CrashCounts:        []models.CrashCount{crashCount},
				}

				decodedResponse := decodeBulkResponse(response.Body.String())
				Expect(decodedResponse).To(HaveLen(1))
				Expect(decodedResponse).To(HaveKey(expectedApp.AppGuid))
				receivedApp := decodedResponse[expectedApp.AppGuid]
				Expect(receivedApp.AppGuid).To(Equal(expectedApp.AppGuid))
				Expect(receivedApp.AppVersion).To(Equal(expectedApp.AppVersion))
				Expect(receivedApp.Desired).To(Equal(expectedApp.Desired))
				Expect(receivedApp.InstanceHeartbeats).To(ConsistOf(expectedApp.InstanceHeartbeats))
				Expect(receivedApp.CrashCounts).To(ConsistOf(expectedApp.CrashCounts))
			})
		})
	})
})
<commit_msg>Add some test dependencies back<commit_after>package handlers_test

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
	"net/http/httptest"
	"time"

	"github.com/cloudfoundry/gunk/timeprovider/faketimeprovider"
	"github.com/cloudfoundry/hm9000/apiserver/handlers"
	"github.com/cloudfoundry/hm9000/config"
	"github.com/cloudfoundry/hm9000/helpers/logger"
	"github.com/cloudfoundry/hm9000/models"
	"github.com/cloudfoundry/hm9000/store"
	"github.com/cloudfoundry/hm9000/testhelpers/appfixture"
	"github.com/cloudfoundry/hm9000/testhelpers/fakelogger"
	"github.com/cloudfoundry/storeadapter/fakestoreadapter"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

func decodeBulkResponse(response string) (bulkAppResp map[string]AppResponse) {
	err := json.Unmarshal([]byte(response), &bulkAppResp)
	Expect(err).NotTo(HaveOccurred())
	return
}

type AppResponse struct {
	AppGuid    string `json:"droplet"`
	AppVersion string `json:"version"`

	Desired            models.DesiredAppState     `json:"desired"`
	InstanceHeartbeats []models.InstanceHeartbeat `json:"instance_heartbeats"`
	CrashCounts        []models.CrashCount        `json:"crash_counts"`
}

type HandlerConf struct {
	StoreAdapter *fakestoreadapter.FakeStoreAdapter
	TimeProvider *faketimeprovider.FakeTimeProvider
	Logger       logger.Logger
	MaxInFlight  int
}

func defaultConf() HandlerConf {
	return HandlerConf{
		StoreAdapter: fakestoreadapter.New(),
		TimeProvider: &faketimeprovider.FakeTimeProvider{
			TimeToProvide: time.Unix(100, 0),
		},
		Logger: fakelogger.NewFakeLogger(),
	}
}

func makeHandlerAndStore(conf HandlerConf) (http.Handler, store.Store, error) {
	config, _ := config.DefaultConfig()

	store := store.NewStore(config, conf.StoreAdapter, fakelogger.NewFakeLogger())

	handler, err := handlers.New(conf.Logger, store, conf.TimeProvider)
	return handler, store, err
}

func decodeResponse(response string) (appResp AppResponse) {
	err := json.Unmarshal([]byte(response), &appResp)
	Ω(err).ShouldNot(HaveOccurred())
	return appResp
}

func freshenTheStore(store store.Store) {
	store.BumpDesiredFreshness(time.Unix(0, 0))
	store.BumpActualFreshness(time.Unix(0, 0))
}

var _ = Describe("BulkAppState", func() {
	Context("when the store has an unexpected error", func() {
		It("should return an empty hash", func() {
			conf := defaultConf()
			conf.StoreAdapter.GetErrInjector = fakestoreadapter.NewFakeStoreAdapterErrorInjector("desired", fmt.Errorf("No desired state for you!"))

			handler, store, err := makeHandlerAndStore(conf)
			Expect(err).ToNot(HaveOccurred())
			freshenTheStore(store)

			request, _ := http.NewRequest("POST", "/bulk_app_state", bytes.NewBufferString(`[{"droplet":"something","version":"whatever"}]`))
			response := httptest.NewRecorder()
			handler.ServeHTTP(response, request)

			Expect(response.Body.String()).To(Equal("{}"))
		})
	})

	Context("when the store is not fresh", func() {
		It("returns an empty hash", func() {
			conf := defaultConf()
			app := appfixture.NewAppFixture()

			handler, store, err := makeHandlerAndStore(conf)
			Expect(err).ToNot(HaveOccurred())

			crashCount := models.CrashCount{
				AppGuid:       app.AppGuid,
				AppVersion:    app.AppVersion,
				InstanceIndex: 1,
				CrashCount:    2,
			}
			store.SyncDesiredState(app.DesiredState(3))
			store.SyncHeartbeats(app.Heartbeat(3))
			store.SaveCrashCounts(crashCount)

			request_body := fmt.Sprintf(`[{"droplet":"%s","version":"%s"}]`, app.AppGuid, app.AppVersion)
			request, _ := http.NewRequest("POST", "/bulk_app_state", bytes.NewBufferString(request_body))
			response := httptest.NewRecorder()
			handler.ServeHTTP(response, request)

			Expect(response.Body.String()).To(Equal("{}"))
		})
	})

	Context("when the store is fresh", func() {
		It("returns an empty hash when invalid request json is provided", func() {
			conf := defaultConf()
			app := appfixture.NewAppFixture()

			handler, store, err := makeHandlerAndStore(conf)
			Expect(err).ToNot(HaveOccurred())

			crashCount := models.CrashCount{
				AppGuid:       app.AppGuid,
				AppVersion:    app.AppVersion,
				InstanceIndex: 1,
				CrashCount:    2,
			}
			store.SyncDesiredState(app.DesiredState(3))
			store.SyncHeartbeats(app.Heartbeat(3))
			store.SaveCrashCounts(crashCount)
			freshenTheStore(store)

			request, _ := http.NewRequest("POST", "/bulk_app_state", bytes.NewBufferString("asdf{}"))
			response := httptest.NewRecorder()
			handler.ServeHTTP(response, request)

			Expect(response.Body.String()).To(Equal("{}"))
		})

		Context("when the app query parameters do not correspond to an existing app", func() {
			It("returns empty hash", func() {
				conf := defaultConf()
				handler, store, err := makeHandlerAndStore(conf)
				Expect(err).ToNot(HaveOccurred())
				freshenTheStore(store)

				request, _ := http.NewRequest("POST", "/bulk_app_state", bytes.NewBufferString(`[{"droplet":"elephant","version":"pink-flamingo"}]`))
				response := httptest.NewRecorder()
				handler.ServeHTTP(response, request)

				Expect(response.Body.String()).To(Equal("{}"))
			})
		})

		Context("when the app query parameters correspond to an existing app", func() {
			It("should return the actual instances and crashes of the app", func() {
				conf := defaultConf()
				app := appfixture.NewAppFixture()

				handler, store, err := makeHandlerAndStore(conf)
				Expect(err).ToNot(HaveOccurred())

				crashCount := models.CrashCount{
					AppGuid:       app.AppGuid,
					AppVersion:    app.AppVersion,
					InstanceIndex: 1,
					CrashCount:    2,
				}
				store.SyncDesiredState(app.DesiredState(3))
				store.SyncHeartbeats(app.Heartbeat(3))
				store.SaveCrashCounts(crashCount)
				freshenTheStore(store)

				request_body := fmt.Sprintf(`[{"droplet":"%s","version":"%s"}]`, app.AppGuid, app.AppVersion)
				request, _ := http.NewRequest("POST", "/bulk_app_state", bytes.NewBufferString(request_body))
				response := httptest.NewRecorder()
				handler.ServeHTTP(response, request)

				expectedInstanceHeartbeats := []models.InstanceHeartbeat{
					app.InstanceAtIndex(0).Heartbeat(),
					app.InstanceAtIndex(1).Heartbeat(),
					app.InstanceAtIndex(2).Heartbeat(),
				}
				expectedApp := AppResponse{
					AppGuid:            app.AppGuid,
					AppVersion:         app.AppVersion,
					Desired:            app.DesiredState(3),
					InstanceHeartbeats: expectedInstanceHeartbeats,
					CrashCounts:        []models.CrashCount{crashCount},
				}

				decodedResponse := decodeBulkResponse(response.Body.String())
				Expect(decodedResponse).To(HaveLen(1))
				Expect(decodedResponse).To(HaveKey(expectedApp.AppGuid))
				receivedApp := decodedResponse[expectedApp.AppGuid]
				Expect(receivedApp.AppGuid).To(Equal(expectedApp.AppGuid))
				Expect(receivedApp.AppVersion).To(Equal(expectedApp.AppVersion))
				Expect(receivedApp.Desired).To(Equal(expectedApp.Desired))
				Expect(receivedApp.InstanceHeartbeats).To(ConsistOf(expectedApp.InstanceHeartbeats))
				Expect(receivedApp.CrashCounts).To(ConsistOf(expectedApp.CrashCounts))
			})
		})

		Context("when some of the apps are not found", func() {
			It("responds with the apps that are present", func() {
				conf := defaultConf()
				app := appfixture.NewAppFixture()

				handler, store, err := makeHandlerAndStore(conf)
				Expect(err).ToNot(HaveOccurred())
				freshenTheStore(store)

				crashCount := models.CrashCount{
					AppGuid:       app.AppGuid,
					AppVersion:    app.AppVersion,
					InstanceIndex: 1,
					CrashCount:    2,
				}
				store.SyncDesiredState(app.DesiredState(3))
				store.SyncHeartbeats(app.Heartbeat(3))
				store.SaveCrashCounts(crashCount)

				requestBody := fmt.Sprintf(`[{"droplet":"%s","version":"%s"},{"droplet":"jam-sandwich","version":"123"}]`, app.AppGuid, app.AppVersion)
				request, _ := http.NewRequest("POST", "/bulk_app_state", bytes.NewBufferString(requestBody))
				response := httptest.NewRecorder()
				handler.ServeHTTP(response, request)

				expectedInstanceHeartbeats := []models.InstanceHeartbeat{
					app.InstanceAtIndex(0).Heartbeat(),
					app.InstanceAtIndex(1).Heartbeat(),
					app.InstanceAtIndex(2).Heartbeat(),
				}
				expectedApp := AppResponse{
					AppGuid:            app.AppGuid,
					AppVersion:         app.AppVersion,
					Desired:            app.DesiredState(3),
					InstanceHeartbeats: expectedInstanceHeartbeats,
					CrashCounts:        []models.CrashCount{crashCount},
				}

				decodedResponse := decodeBulkResponse(response.Body.String())
				Expect(decodedResponse).To(HaveLen(1))
				Expect(decodedResponse).To(HaveKey(expectedApp.AppGuid))
				receivedApp := decodedResponse[expectedApp.AppGuid]
				Expect(receivedApp.AppGuid).To(Equal(expectedApp.AppGuid))
				Expect(receivedApp.AppVersion).To(Equal(expectedApp.AppVersion))
				Expect(receivedApp.Desired).To(Equal(expectedApp.Desired))
				Expect(receivedApp.InstanceHeartbeats).To(ConsistOf(expectedApp.InstanceHeartbeats))
				Expect(receivedApp.CrashCounts).To(ConsistOf(expectedApp.CrashCounts))
			})
		})
	})
})
<|endoftext|>


<commit_before>package Cmd

import ()

/// 时间指令
const TIME_USERCMD = 2
const GM_USERCMD = 62

type stTimerUserCmd struct {
	StNullUserCmd
}

/// 网关向用户发送游戏时间
const GAMETIME_TIMER_USERCMD_PARA = 1

type StGameTimeTimerUserCmd struct {
	stTimerUserCmd
	QwGameTime uint64 /**< 游戏时间 */
	DwTempID   uint64 /**< 断线重连随机数 */
}

func NewStGameTimeTimerUserCmd() *StGameTimeTimerUserCmd {
	cmd := &StGameTimeTimerUserCmd{}
	cmd.ByCmd = TIME_USERCMD
	cmd.ByParam = GAMETIME_TIMER_USERCMD_PARA
	return cmd
}

/// 网关向用户发送游戏时间
const REQUESTUSERGAMETIME_TIMER_USERCMD_PARA = 2

type StRequestUserGameTimeTimerUserCmd struct {
	stTimerUserCmd
}

func NewStRequestUserGameTimeTimerUserCmd() *StRequestUserGameTimeTimerUserCmd {
	cmd := &StRequestUserGameTimeTimerUserCmd{}
	cmd.ByCmd = TIME_USERCMD
	cmd.ByParam = REQUESTUSERGAMETIME_TIMER_USERCMD_PARA
	return cmd
}

/// 网关向用户发送游戏时间
const USERGAMETIME_TIMER_USERCMD_PARA = 3

type StUserGameTimeTimerUserCmd struct {
	stTimerUserCmd
	qwGameTime uint64 /**< 用户游戏时间 */
	mac        uint64 /**< 用户MAC */
}

func NewStUserGameTimeTimerUserCmd() *StUserGameTimeTimerUserCmd {
	cmd := &StUserGameTimeTimerUserCmd{}
	cmd.ByCmd = TIME_USERCMD
	cmd.ByParam = USERGAMETIME_TIMER_USERCMD_PARA
	cmd.qwGameTime = 0
	cmd.mac = 0
	return cmd
}
<commit_msg>test code<commit_after>package Cmd

import ()

/// 时间指令
const TIME_USERCMD = 2
const GM_USERCMD = 62

type stTimerUserCmd struct {
	StNullUserCmd
}

/// 网关向用户发送游戏时间
const GAMETIME_TIMER_USERCMD_PARA = 1

type StGameTimeTimerUserCmd struct {
	stTimerUserCmd
	QwGameTime uint64 /**< 游戏时间 */
	DwTempID   uint64 /**< 断线重连随机数 */
}

func NewStGameTimeTimerUserCmd() *StGameTimeTimerUserCmd {
	cmd := &StGameTimeTimerUserCmd{}
	cmd.ByCmd = TIME_USERCMD
	cmd.ByParam = GAMETIME_TIMER_USERCMD_PARA
	return cmd
}

/// 网关向用户发送游戏时间
const REQUESTUSERGAMETIME_TIMER_USERCMD_PARA = 2

type StRequestUserGameTimeTimerUserCmd struct {
	stTimerUserCmd
}

func NewStRequestUserGameTimeTimerUserCmd() *StRequestUserGameTimeTimerUserCmd {
	cmd := &StRequestUserGameTimeTimerUserCmd{}
	cmd.ByCmd = TIME_USERCMD
	cmd.ByParam = REQUESTUSERGAMETIME_TIMER_USERCMD_PARA
	return cmd
}

/// 网关向用户发送游戏时间
const USERGAMETIME_TIMER_USERCMD_PARA = 3

type StUserGameTimeTimerUserCmd struct {
	stTimerUserCmd
	QwGameTime uint64 /**< 用户游戏时间 */
	Mac        uint64 /**< 用户MAC */
}

func NewStUserGameTimeTimerUserCmd() *StUserGameTimeTimerUserCmd {
	cmd := &StUserGameTimeTimerUserCmd{}
	cmd.ByCmd = TIME_USERCMD
	cmd.ByParam = USERGAMETIME_TIMER_USERCMD_PARA
	cmd.QwGameTime = 0
	cmd.Qac = 0
	return cmd
}
<|endoftext|>