<commit_before>package model

import (
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"

	"gopkg.in/yaml.v1"
)

type Artifact struct {
	// Descriptor api version. 1 is the default.
	Api int `json:"api" yaml:"api"`

	// Name of the group of artifacts. Cannot contain "/" or whitespace characters.
	Group string
	// Name of the artifact. Cannot contain "/" or whitespace characters.
	Name string `yaml:"artifact"`
	// Use semantic versioning or include the "SNAPSHOT" keyword for non-fixed versions.
	Version string
	// Represents the file extension.
	Type string
	// If true then use "any" for the operating system name when archiving/fetching.
	AnyOS bool `yaml:"anyos"`
}

// StorageBase returns the file name to which the artifact is stored.
func (a Artifact) StorageBase() string {
	return fmt.Sprintf("%s-%s.%s", a.Name, a.Version, a.Type)
}

// StorageLocation returns the relative resource path to store the artifact.
func (a Artifact) StorageLocation(osname string) string {
	return filepath.Join(strings.Replace(a.Group, ".", "/", -1), a.Name, a.Version, osname, a.StorageBase())
}

// IsSnapshot returns true if the version has the substring "SNAPSHOT".
func (a Artifact) IsSnapshot() bool {
	return strings.Index(a.Version, "SNAPSHOT") != -1
}

// Verify will inspect all required fields. Exit if one does.
func (a Artifact) Verify() {
	if len(a.Group) == 0 {
		Fatalf("group of artifact [%#v] cannot be empty", a)
	}
	if len(a.Name) == 0 {
		Fatalf("name of artifact [%#v] cannot be empty", a)
	}
	if len(a.Version) == 0 {
		Fatalf("version of artifact [%#v] cannot be empty", a)
	}
	if len(a.Type) == 0 {
		Fatalf("type (extension) of artifact [%#v] cannot be empty", a)
	}
}

func (a Artifact) PluginParameters() (params []string) {
	return append(params,
		"--group="+a.Group,
		"--artifact="+a.Name,
		"--version="+a.Version,
		"--type="+a.Type)
}

// LoadArtifact parses an Artifact by reading the src file.
func LoadArtifact(src string) (a Artifact, e error) {
	f, err := os.Open(src)
	if err != nil {
		return a, err
	}
	defer f.Close()

	data, _ := ioutil.ReadAll(f)
	err = yaml.Unmarshal(data, &a)
	if err != nil {
		return a, err
	}
	return a, nil
}
<commit_msg>use v1, time to do Godeps...<commit_after>package model

import (
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"

	"gopkg.in/yaml.v2"
)

type Artifact struct {
	// Descriptor api version. 1 is the default.
	Api int `json:"api" yaml:"api"`

	// Name of the group of artifacts. Cannot contain "/" or whitespace characters.
	Group string
	// Name of the artifact. Cannot contain "/" or whitespace characters.
	Name string `yaml:"artifact"`
	// Use semantic versioning or include the "SNAPSHOT" keyword for non-fixed versions.
	Version string
	// Represents the file extension.
	Type string
	// If true then use "any" for the operating system name when archiving/fetching.
	AnyOS bool `yaml:"anyos"`
}

// StorageBase returns the file name to which the artifact is stored.
func (a Artifact) StorageBase() string {
	return fmt.Sprintf("%s-%s.%s", a.Name, a.Version, a.Type)
}

// StorageLocation returns the relative resource path to store the artifact.
func (a Artifact) StorageLocation(osname string) string {
	return filepath.Join(strings.Replace(a.Group, ".", "/", -1), a.Name, a.Version, osname, a.StorageBase())
}

// IsSnapshot returns true if the version has the substring "SNAPSHOT".
func (a Artifact) IsSnapshot() bool {
	return strings.Index(a.Version, "SNAPSHOT") != -1
}

// Verify will inspect all required fields. Exit if one does.
func (a Artifact) Verify() {
	if len(a.Group) == 0 {
		Fatalf("group of artifact [%#v] cannot be empty", a)
	}
	if len(a.Name) == 0 {
		Fatalf("name of artifact [%#v] cannot be empty", a)
	}
	if len(a.Version) == 0 {
		Fatalf("version of artifact [%#v] cannot be empty", a)
	}
	if len(a.Type) == 0 {
		Fatalf("type (extension) of artifact [%#v] cannot be empty", a)
	}
}

func (a Artifact) PluginParameters() (params []string) {
	return append(params,
		"--group="+a.Group,
		"--artifact="+a.Name,
		"--version="+a.Version,
		"--type="+a.Type)
}

// LoadArtifact parses an Artifact by reading the src file.
func LoadArtifact(src string) (a Artifact, e error) {
	f, err := os.Open(src)
	if err != nil {
		return a, err
	}
	defer f.Close()

	data, _ := ioutil.ReadAll(f)
	err = yaml.Unmarshal(data, &a)
	if err != nil {
		return a, err
	}
	return a, nil
}
<|endoftext|>


<commit_before>// reflectwalk is a package that allows you to "walk" complex structures
// similar to how you may "walk" a filesystem: visiting every element one
// by one and calling callback functions allowing you to handle and manipulate
// those elements.
package reflectwalk

import (
	"reflect"
)

type Location uint

const (
	MapKey Location = iota
	MapValue
	StructField
	WalkLoc
)

// PrimitiveWalker implementations are able to handle primitive values
// within complex structures. Primitive values are numbers, strings,
// booleans, funcs, chans.
//
// These primitive values are often members of more complex
// structures (slices, maps, etc.) that are walkable by other interfaces.
type PrimitiveWalker interface {
	Primitive(reflect.Value) error
}

// MapWalker implementations are able to handle individual elements
// found within a map structure.
type MapWalker interface {
	MapElem(k, v reflect.Value) error
}

// SliceWalker implementations are able to handle slice elements found
// within complex structures.
type SliceWalker interface {
	SliceElem(int, reflect.Value) error
}

// StructWalker is an interface that has methods that are called for
// structs when a Walk is done.
type StructWalker interface {
	StructField(reflect.StructField, reflect.Value) error
}

// EnterExitWalker implementations are notified before and after
// they walk deeper into complex structures (into struct fields,
// into slice elements, etc.)
type EnterExitWalker interface {
	Enter(Location) error
	Exit(Location) error
}

// Walk takes an arbitrary value and an interface and traverses the
// value, calling callbacks on the interface if they are supported.
// The interface should implement one or more of the walker interfaces
// in this package, such as PrimitiveWalker, StructWalker, etc.
func Walk(data, walker interface{}) (err error) {
	v := reflect.Indirect(reflect.ValueOf(data))
	ew, ok := walker.(EnterExitWalker)
	if ok {
		err = ew.Enter(WalkLoc)
	}

	if err == nil {
		err = walk(v, walker)
	}

	if ok && err == nil {
		err = ew.Exit(WalkLoc)
	}

	return
}

func walk(v reflect.Value, w interface{}) error {
	k := v.Kind()
	if k >= reflect.Int && k <= reflect.Complex128 {
		k = reflect.Int
	}

	switch k {
	// Primitives
	case reflect.Bool:
		fallthrough
	case reflect.Chan:
		fallthrough
	case reflect.Func:
		fallthrough
	case reflect.Int:
		fallthrough
	case reflect.String:
		return walkPrimitive(v, w)
	case reflect.Map:
		return walkMap(v, w)
	case reflect.Slice:
		return walkSlice(v, w)
	case reflect.Struct:
		return walkStruct(v, w)
	default:
		panic("unsupported type: " + k.String())
	}
}

func walkMap(v reflect.Value, w interface{}) error {
	for _, k := range v.MapKeys() {
		kv := v.MapIndex(k)

		if mw, ok := w.(MapWalker); ok {
			if err := mw.MapElem(k, kv); err != nil {
				return err
			}
		}

		ew, ok := w.(EnterExitWalker)
		if ok {
			ew.Enter(MapKey)
		}

		if err := walk(k, w); err != nil {
			return err
		}

		if ok {
			ew.Exit(MapKey)
			ew.Enter(MapValue)
		}

		if err := walk(kv, w); err != nil {
			return err
		}

		if ok {
			ew.Exit(MapValue)
		}
	}

	return nil
}

func walkPrimitive(v reflect.Value, w interface{}) error {
	if pw, ok := w.(PrimitiveWalker); ok {
		return pw.Primitive(v)
	}

	return nil
}

func walkSlice(v reflect.Value, w interface{}) (err error) {
	for i := 0; i < v.Len(); i++ {
		elem := v.Index(i)

		if sw, ok := w.(SliceWalker); ok {
			if err := sw.SliceElem(i, elem); err != nil {
				return err
			}
		}

		if err := walk(elem, w); err != nil {
			return err
		}
	}

	return nil
}

func walkStruct(v reflect.Value, w interface{}) (err error) {
	vt := v.Type()
	for i := 0; i < vt.NumField(); i++ {
		sf := vt.Field(i)
		f := v.FieldByIndex([]int{i})

		if sw, ok := w.(StructWalker); ok {
			err = sw.StructField(sf, f)
			if err != nil {
				return
			}
		}

		ew, ok := w.(EnterExitWalker)
		if ok {
			ew.Enter(StructField)
		}

		err = walk(f, w)
		if err != nil {
			return
		}

		if ok {
			ew.Exit(StructField)
		}
	}

	return nil
}
<commit_msg>enter/exit sliceelem<commit_after>// reflectwalk is a package that allows you to "walk" complex structures
// similar to how you may "walk" a filesystem: visiting every element one
// by one and calling callback functions allowing you to handle and manipulate
// those elements.
package reflectwalk

import (
	"reflect"
)

type Location uint

const (
	MapKey Location = iota
	MapValue
	SliceElem
	StructField
	WalkLoc
)

// PrimitiveWalker implementations are able to handle primitive values
// within complex structures. Primitive values are numbers, strings,
// booleans, funcs, chans.
//
// These primitive values are often members of more complex
// structures (slices, maps, etc.) that are walkable by other interfaces.
type PrimitiveWalker interface {
	Primitive(reflect.Value) error
}

// MapWalker implementations are able to handle individual elements
// found within a map structure.
type MapWalker interface {
	MapElem(k, v reflect.Value) error
}

// SliceWalker implementations are able to handle slice elements found
// within complex structures.
type SliceWalker interface {
	SliceElem(int, reflect.Value) error
}

// StructWalker is an interface that has methods that are called for
// structs when a Walk is done.
type StructWalker interface {
	StructField(reflect.StructField, reflect.Value) error
}

// EnterExitWalker implementations are notified before and after
// they walk deeper into complex structures (into struct fields,
// into slice elements, etc.)
type EnterExitWalker interface {
	Enter(Location) error
	Exit(Location) error
}

// Walk takes an arbitrary value and an interface and traverses the
// value, calling callbacks on the interface if they are supported.
// The interface should implement one or more of the walker interfaces
// in this package, such as PrimitiveWalker, StructWalker, etc.
func Walk(data, walker interface{}) (err error) {
	v := reflect.Indirect(reflect.ValueOf(data))
	ew, ok := walker.(EnterExitWalker)
	if ok {
		err = ew.Enter(WalkLoc)
	}

	if err == nil {
		err = walk(v, walker)
	}

	if ok && err == nil {
		err = ew.Exit(WalkLoc)
	}

	return
}

func walk(v reflect.Value, w interface{}) error {
	k := v.Kind()
	if k >= reflect.Int && k <= reflect.Complex128 {
		k = reflect.Int
	}

	switch k {
	// Primitives
	case reflect.Bool:
		fallthrough
	case reflect.Chan:
		fallthrough
	case reflect.Func:
		fallthrough
	case reflect.Int:
		fallthrough
	case reflect.String:
		return walkPrimitive(v, w)
	case reflect.Map:
		return walkMap(v, w)
	case reflect.Slice:
		return walkSlice(v, w)
	case reflect.Struct:
		return walkStruct(v, w)
	default:
		panic("unsupported type: " + k.String())
	}
}

func walkMap(v reflect.Value, w interface{}) error {
	for _, k := range v.MapKeys() {
		kv := v.MapIndex(k)

		if mw, ok := w.(MapWalker); ok {
			if err := mw.MapElem(k, kv); err != nil {
				return err
			}
		}

		ew, ok := w.(EnterExitWalker)
		if ok {
			ew.Enter(MapKey)
		}

		if err := walk(k, w); err != nil {
			return err
		}

		if ok {
			ew.Exit(MapKey)
			ew.Enter(MapValue)
		}

		if err := walk(kv, w); err != nil {
			return err
		}

		if ok {
			ew.Exit(MapValue)
		}
	}

	return nil
}

func walkPrimitive(v reflect.Value, w interface{}) error {
	if pw, ok := w.(PrimitiveWalker); ok {
		return pw.Primitive(v)
	}

	return nil
}

func walkSlice(v reflect.Value, w interface{}) (err error) {
	for i := 0; i < v.Len(); i++ {
		elem := v.Index(i)

		if sw, ok := w.(SliceWalker); ok {
			if err := sw.SliceElem(i, elem); err != nil {
				return err
			}
		}

		ew, ok := w.(EnterExitWalker)
		if ok {
			ew.Enter(SliceElem)
		}

		if err := walk(elem, w); err != nil {
			return err
		}

		if ok {
			ew.Exit(SliceElem)
		}
	}

	return nil
}

func walkStruct(v reflect.Value, w interface{}) (err error) {
	vt := v.Type()
	for i := 0; i < vt.NumField(); i++ {
		sf := vt.Field(i)
		f := v.FieldByIndex([]int{i})

		if sw, ok := w.(StructWalker); ok {
			err = sw.StructField(sf, f)
			if err != nil {
				return
			}
		}

		ew, ok := w.(EnterExitWalker)
		if ok {
			ew.Enter(StructField)
		}

		err = walk(f, w)
		if err != nil {
			return
		}

		if ok {
			ew.Exit(StructField)
		}
	}

	return nil
}
<|endoftext|>


<commit_before>package main

import (
	"bufio"
	"fmt"
	"os"
	"os/exec"
	"strings"

	"github.com/DanielRenne/GoCore/core/extensions"
	"github.com/DanielRenne/GoCore/core/logger"
	"github.com/DanielRenne/GoCore/core/utils"
	"github.com/davidrenne/heredoc"
)

func talk(msg string) {
	utils.TalkDirty(msg)
	logger.Message("Message: "+msg, logger.GREEN)
}

func cdGoPath() {
	err := os.Chdir(os.Getenv("GOPATH"))
	errorOut("cd gopath", err, false)
}

func errorOut(line string, err error, dontExit bool) {
	if err != nil {
		msg := "Errored out: " + err.Error()
		logger.Message(msg+" "+line, logger.RED)
		utils.TalkDirty(msg)
		if !dontExit {
			os.Exit(2)
		}
	} else {
		logger.Message("Success: "+line, logger.GREEN)
	}
}

func main() {
	var appName string
	var username string
	var databaseType string
	var humanTitle string
	var createGit string
	var pushGit string
	var colorPalette string

	logger.Message("Welcome to the GoCore createApp tool!  Thank you for using GoCore.", logger.YELLOW)
	logger.Message("We hold these below truths to be self-evident", logger.WHITE)
	logger.Message(fmt.Sprintf("% x", []byte{100, 97, 118, 105, 100, 32, 114, 101, 110, 110, 101, 32, 105, 115, 32, 99, 111, 111, 108, 33, 10}), logger.MAGENTA)

	//also should ensure first char of appName is lower
	for {
		reader := bufio.NewReader(os.Stdin)
		fmt.Print("GoCore AppName: ")
		appName, _ = reader.ReadString('\n')
		appName = strings.Trim(appName, "\n")
		ok := false
		if strings.Index(appName, " ") == -1 {
			ok = true
		} else {
			fmt.Println("No spaces please")
		}
		if ok {
			break
		}
	}

	reader := bufio.NewReader(os.Stdin)
	fmt.Print("Title of all pages: ")
	humanTitle, _ = reader.ReadString('\n')
	humanTitle = strings.Trim(humanTitle, "\n")

	for {
		reader := bufio.NewReader(os.Stdin)
		fmt.Print("Github.com Username: ")
		username, _ = reader.ReadString('\n')
		username = strings.Trim(username, "\n")
		ok := false
		if strings.Index(username, " ") == -1 {
			ok = true
		} else {
			fmt.Println("No spaces please")
		}
		if ok {
			break
		}
	}

	databaseType = "mongo"
	for {
		reader := bufio.NewReader(os.Stdin)
		fmt.Print("'mongo' or 'bolt' (defaults mongo due to most support): ")
		databaseType, _ = reader.ReadString('\n')
		databaseType = strings.Trim(databaseType, "\n")
		ok := false
		if databaseType == "mongo" || databaseType == "bolt" {
			ok = true
		} else {
			fmt.Println("Invalid type 'mongo' or 'bolt'")
		}
		if ok {
			break
		}
	}

	createGit = "y"
	for {
		reader := bufio.NewReader(os.Stdin)
		fmt.Print("Create and commit initial git repository (y or n) (defaults y): ")
		createGit, _ = reader.ReadString('\n')
		createGit = strings.Trim(createGit, "\n")
		ok := false
		if createGit == "y" || createGit == "n" {
			ok = true
		} else {
			fmt.Println("Invalid type 'n' or 'y'")
		}
		if ok {
			break
		}
	}

	if createGit == "y" {

		for {
			reader := bufio.NewReader(os.Stdin)
			fmt.Print("If public repo.  Would you like to push this to github.com? (defaults y): ")
			pushGit, _ = reader.ReadString('\n')
			pushGit = strings.Trim(pushGit, "\n")
			ok := false
			if pushGit == "y" || pushGit == "n" {
				ok = true
			} else {
				fmt.Println("Invalid type 'n' or 'y'")
			}
			if ok {
				break
			}
		}
	}
	//logger.Message("Next choose a color palette:", logger.WHITE)
	//logger.Message("(default) BlueGrey and Orange value=bgo", logger.BLUE)
	//logger.Message("(default) Green and White value=irish", logger.GREEN)

	//reader = bufio.NewReader(os.Stdin)
	//fmt.Print("Color value: ")
	//colorPalette, _ = reader.ReadString('\n')
	//colorPalette = strings.Trim(colorPalette, "\n")

	cdGoPath()

	camelUpper := strings.ToTitle(string(appName[0])) + string(appName[1:])

	err := extensions.WriteToFile(colorPalette, "/tmp/colorPalette", 644)
	errorOut("extensions.WriteToFile "+colorPalette+" to /tmp/colorPalette", err, false)

	err = extensions.WriteToFile(humanTitle, "/tmp/humanTitle", 644)
	errorOut("extensions.WriteToFile "+humanTitle+" to /tmp/humanTitle", err, false)

	err = extensions.WriteToFile(databaseType, "/tmp/databaseType", 644)
	errorOut("extensions.WriteToFile "+databaseType+" to /tmp/databaseType", err, false)

	path := "src/github.com/" + username
	err = os.MkdirAll(path, 0644)
	errorOut("os.MkdirAll("+path+", 0644)", err, false)

	fmt.Println("adsffdas name :", appName)
	fmt.Println(path)
	appPath := path + "/" + appName

	_, err = os.Stat(appPath)
	if err == nil {
		err := extensions.RemoveDirectory(appPath)
		errorOut("extensions.RemoveDirectory(appPath)", err, false)
	}

	err = os.MkdirAll(appPath, 0644)
	errorOut("os.MkdirAll("+appPath+", 0644)", err, false)

	modelBuildPath := appPath + "/modelBuild" + camelUpper + "/"

	err = os.MkdirAll(modelBuildPath, 0644)
	errorOut("os.MkdirAll("+modelBuildPath+", 0644)", err, false)

	buildPath := appPath + "/build" + camelUpper + "/"

	err = os.MkdirAll(buildPath, 0644)
	errorOut("os.MkdirAll("+buildPath+", 0644)", err, false)

	template := `
package main

import (
	"flag"
	"github.com/DanielRenne/GoCore/%s"
)

func main() {
	// allow -configFile=test.json to be passed to build different configs other than webConfig.json
	configFile := flag.String("configFile", "webConfig.json", "Configuration File Name.  Ex...  webConfig.json")
	flag.Parse()
	%s.Initialize("src/github.com/` + username + "/" + appName + `", *configFile)
}

`
	buildGoFile := buildPath + "build" + camelUpper + ".go"
	err = extensions.WriteAndGoFormat(heredoc.Docf(template, "buildCore", "buildCore"), buildGoFile)
	errorOut("extensions.WriteAndGoFormat "+buildGoFile, err, false)

	modelGoFile := modelBuildPath + "modelBuild" + camelUpper + ".go"
	err = extensions.WriteAndGoFormat(heredoc.Docf(template, "modelBuild", "modelBuild"), modelGoFile)
	errorOut("extensions.WriteAndGoFormat "+modelGoFile, err, false)

	talk("Copying app generation files")

	cmd := exec.Command("go", "run", buildGoFile)
	err = cmd.Run()
	errorOut("running go run "+buildGoFile, err, false)

	cmd = exec.Command("go", "run", appPath+"/install"+camelUpper+"/install"+camelUpper+".go")
	err = cmd.Run()
	errorOut("running "+appPath+"/install"+camelUpper, err, false)

	cdGoPath()
	err = os.Chdir(appPath + "/bin")
	errorOut("cd bin", err, false)

	cmd = exec.Command("bash", "format")
	err = cmd.Start()
	errorOut("formatting all code", err, false)

	cdGoPath()
	err = os.Chdir(appPath)
	errorOut("cd appPath", err, false)

	if createGit == "y" {
		talk("Adding github files")

		cmd = exec.Command("git", "init")
		err = cmd.Run()
		errorOut("git init", err, false)

		cmd = exec.Command("git", "add", ".")
		err = cmd.Run()
		errorOut("git add", err, false)

		cmd = exec.Command("git", "commit", "-m", "Initial GoCore App Generation")
		err = cmd.Run()
		errorOut("git commit", err, false)

		cmd = exec.Command("git", "remote", "add", "origin", "https://github.com/"+username+"/"+appName+".git")
		err = cmd.Run()
		errorOut("git commit", err, false)

		if pushGit == "y" {
			cmd = exec.Command("curl", "-u", username, "https://api.github.com/user/repos", "-d", "{\"name\": \"$appName\"}")
			err = cmd.Run()
			errorOut("curl create repo on API", err, true)
			cmd = exec.Command("git", "push", "-u", username, "origin", "master")
			err = cmd.Run()
			errorOut("git push", err, true)
		}
	}

	cdGoPath()
	cmd = exec.Command("go", "install", strings.Replace(modelBuildPath, "src/", "", -1))
	err = cmd.Run()
	errorOut("go install models", err, false)

	cmd = exec.Command("bash", appPath+"/bin/start_app")
	err = cmd.Start()
	errorOut("running gocore app server!", err, false)

}
<commit_msg>Update main.go<commit_after>package main

import (
	"bufio"
	"fmt"
	"os"
	"os/exec"
	"strings"

	"github.com/DanielRenne/GoCore/core/extensions"
	"github.com/DanielRenne/GoCore/core/logger"
	"github.com/DanielRenne/GoCore/core/utils"
	"github.com/davidrenne/heredoc"
)

func talk(msg string) {
	utils.TalkDirty(msg)
	logger.Message("Message: "+msg, logger.GREEN)
}

func cdGoPath() {
	err := os.Chdir(os.Getenv("GOPATH"))
	errorOut("cd gopath", err, false)
}

func errorOut(line string, err error, dontExit bool) {
	if err != nil {
		msg := "Errored out: " + err.Error()
		logger.Message(msg+" "+line, logger.RED)
		utils.TalkDirty(msg)
		if !dontExit {
			os.Exit(2)
		}
	} else {
		logger.Message("Success: "+line, logger.GREEN)
	}
}

func main() {
	var appName string
	var username string
	var databaseType string
	var humanTitle string
	var createGit string
	var pushGit string
	var colorPalette string

	logger.Message("Welcome to the GoCore createApp tool!  Thank you for using GoCore.", logger.YELLOW)
	logger.Message("We hold these below truths to be self-evident", logger.WHITE)
	logger.Message(fmt.Sprintf("% x", []byte{100, 97, 118, 105, 100, 32, 114, 101, 110, 110, 101, 32, 105, 115, 32, 99, 111, 111, 108, 33, 10}), logger.MAGENTA)

	//also should ensure first char of appName is lower
	for {
		reader := bufio.NewReader(os.Stdin)
		fmt.Print("GoCore AppName: ")
		appName, _ = reader.ReadString('\n')
		appName = strings.Trim(appName, "\n")
		ok := false
		if strings.Index(appName, " ") == -1 {
			ok = true
		} else {
			fmt.Println("No spaces please")
		}
		if ok {
			break
		}
	}

	reader := bufio.NewReader(os.Stdin)
	fmt.Print("Title of all pages: ")
	humanTitle, _ = reader.ReadString('\n')
	humanTitle = strings.Trim(humanTitle, "\n")

	for {
		reader := bufio.NewReader(os.Stdin)
		fmt.Print("Github.com Username: ")
		username, _ = reader.ReadString('\n')
		username = strings.Trim(username, "\n")
		ok := false
		if strings.Index(username, " ") == -1 {
			ok = true
		} else {
			fmt.Println("No spaces please")
		}
		if ok {
			break
		}
	}

	databaseType = "mongo"
	for {
		reader := bufio.NewReader(os.Stdin)
		fmt.Print("'mongo' or 'bolt' (defaults mongo due to most support): ")
		databaseType, _ = reader.ReadString('\n')
		databaseType = strings.Trim(databaseType, "\n")
		ok := false
		if databaseType == "mongo" || databaseType == "bolt" {
			ok = true
		} else {
			fmt.Println("Invalid type 'mongo' or 'bolt'")
		}
		if ok {
			break
		}
	}

	createGit = "y"
	for {
		reader := bufio.NewReader(os.Stdin)
		fmt.Print("Create and commit initial git repository (y or n) (defaults y): ")
		createGit, _ = reader.ReadString('\n')
		createGit = strings.Trim(createGit, "\n")
		ok := false
		if createGit == "y" || createGit == "n" {
			ok = true
		} else {
			fmt.Println("Invalid type 'n' or 'y'")
		}
		if ok {
			break
		}
	}

	if createGit == "y" {

		for {
			reader := bufio.NewReader(os.Stdin)
			fmt.Print("If public repo.  Would you like to push this to github.com? (defaults y): ")
			pushGit, _ = reader.ReadString('\n')
			pushGit = strings.Trim(pushGit, "\n")
			ok := false
			if pushGit == "y" || pushGit == "n" {
				ok = true
			} else {
				fmt.Println("Invalid type 'n' or 'y'")
			}
			if ok {
				break
			}
		}
	}
	//logger.Message("Next choose a color palette:", logger.WHITE)
	//logger.Message("(default) BlueGrey and Orange value=bgo", logger.BLUE)
	//logger.Message("(default) Green and White value=irish", logger.GREEN)

	//reader = bufio.NewReader(os.Stdin)
	//fmt.Print("Color value: ")
	//colorPalette, _ = reader.ReadString('\n')
	//colorPalette = strings.Trim(colorPalette, "\n")

	cdGoPath()

	camelUpper := strings.ToTitle(string(appName[0])) + string(appName[1:])

	err := extensions.WriteToFile(colorPalette, "/tmp/colorPalette", 644)
	errorOut("extensions.WriteToFile "+colorPalette+" to /tmp/colorPalette", err, false)

	err = extensions.WriteToFile(humanTitle, "/tmp/humanTitle", 644)
	errorOut("extensions.WriteToFile "+humanTitle+" to /tmp/humanTitle", err, false)

	err = extensions.WriteToFile(databaseType, "/tmp/databaseType", 644)
	errorOut("extensions.WriteToFile "+databaseType+" to /tmp/databaseType", err, false)

	path := "src/github.com/" + username
	err = os.MkdirAll(path, 0644)
	errorOut("os.MkdirAll("+path+", 0644)", err, false)

	fmt.Println("adsffdas name :", appName)
	fmt.Println(path)
	appPath := path + "/" + appName

	_, err = os.Stat(appPath)
	if err == nil {
		err := extensions.RemoveDirectory(appPath)
		errorOut("extensions.RemoveDirectory(appPath)", err, false)
	}

	err = os.MkdirAll(appPath, 0644)
	errorOut("os.MkdirAll("+appPath+", 0644)", err, false)

	modelBuildPath := appPath + "/modelBuild" + camelUpper + "/"

	err = os.MkdirAll(modelBuildPath, 0644)
	errorOut("os.MkdirAll("+modelBuildPath+", 0644)", err, false)

	buildPath := appPath + "/build" + camelUpper + "/"

	err = os.MkdirAll(buildPath, 0644)
	errorOut("os.MkdirAll("+buildPath+", 0644)", err, false)

	template := `
package main

import (
	"flag"
	"github.com/DanielRenne/GoCore/%s"
)

func main() {
	// allow -configFile=test.json to be passed to build different configs other than webConfig.json
	configFile := flag.String("configFile", "webConfig.json", "Configuration File Name.  Ex...  webConfig.json")
	flag.Parse()
	%s.Initialize("src/github.com/` + username + "/" + appName + `", *configFile)
}

`
	buildGoFile := buildPath + "build" + camelUpper + ".go"
	err = extensions.WriteAndGoFormat(heredoc.Docf(template, "buildCore", "buildCore"), buildGoFile)
	errorOut("extensions.WriteAndGoFormat "+buildGoFile, err, false)

	modelGoFile := modelBuildPath + "modelBuild" + camelUpper + ".go"
	err = extensions.WriteAndGoFormat(heredoc.Docf(template, "modelBuild", "modelBuild"), modelGoFile)
	errorOut("extensions.WriteAndGoFormat "+modelGoFile, err, false)

	talk("Copying app generation files")

	cmd := exec.Command("go", "run", buildGoFile)
	err = cmd.Run()
	errorOut("running go run "+buildGoFile, err, false)

	cmd = exec.Command("go", "run", appPath+"/install"+camelUpper+"/install"+camelUpper+".go")
	err = cmd.Run()
	errorOut("running "+appPath+"/install"+camelUpper, err, false)

	cdGoPath()
	err = os.Chdir(appPath + "/bin")
	errorOut("cd bin", err, false)

	cmd = exec.Command("bash", "format")
	err = cmd.Start()
	errorOut("formatting all code", err, false)

	cdGoPath()
	err = os.Chdir(appPath)
	errorOut("cd appPath", err, false)

	if createGit == "y" {
		talk("Adding github files")

		cmd = exec.Command("git", "init")
		err = cmd.Run()
		errorOut("git init", err, false)

		cmd = exec.Command("git", "add", ".")
		err = cmd.Run()
		errorOut("git add", err, false)

		cmd = exec.Command("git", "commit", "-m", "Initial GoCore App Generation")
		err = cmd.Run()
		errorOut("git commit", err, false)

		cmd = exec.Command("git", "remote", "add", "origin", "https://github.com/"+username+"/"+appName+".git")
		err = cmd.Run()
		errorOut("git commit", err, false)

		if pushGit == "y" {
			cmd = exec.Command("curl", "-u", username, "https://api.github.com/user/repos", "-d", "{\"name\": \"" + appName + "\"}")
			err = cmd.Run()
			errorOut("curl create repo on API", err, true)
			cmd = exec.Command("git", "push", "-u", username, "origin", "master")
			err = cmd.Run()
			errorOut("git push", err, true)
		}
	}

	cdGoPath()
	cmd = exec.Command("go", "install", strings.Replace(modelBuildPath, "src/", "", -1))
	err = cmd.Run()
	errorOut("go install models", err, false)

	cmd = exec.Command("bash", appPath+"/bin/start_app")
	err = cmd.Start()
	errorOut("running gocore app server!", err, false)

}
<|endoftext|>


<commit_before>// Copyright 2015 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.

package main

import (
	"crypto/sha256"
	"encoding/hex"
	"flag"
	"fmt"
	"os"

	"github.com/FactomProject/factom"
)

func get(args []string) {
	os.Args = args
	flag.Parse()
	args = flag.Args()
	if len(args) < 1 {
		man("get")
		return
	}

	switch args[0] {
	case "head":
		getHead()
	case "dblock":
		getDBlock(args)
	case "chain":
		getChain(args)
	case "eblock":
		getEBlock(args)
	case "entry":
		getEntry(args)
	case "chainid":
		getChainId(args)
	default:
		man("get")
	}
}

func getHead() {
	head, err := factom.GetDBlockHead()
	if err != nil {
		errorln(err)
		return
	}
	fmt.Println(head.KeyMR)
}

// We expect each element to be its own part in a chain ID
func getChainId(args []string) {
	if len(args) < 2 {
		fmt.Printf("No Chain Specification provided.  See help")
	}
	sum := sha256.New()
	fmt.Println("The chain components:")
	for i, str := range args {
		if i > 0 {
			fmt.Println("    ", str)
			x := sha256.Sum256([]byte(str))
			sum.Write(x[:])
		}
	}
	chainId := sum.Sum(nil)
	fmt.Println("produce the ChainID:")

	fmt.Println("    ", hex.EncodeToString(chainId))
}

func getDBlock(args []string) {
	os.Args = args
	flag.Parse()
	args = flag.Args()
	if len(args) < 1 {
		man("getDBlock")
		return
	}

	keymr := args[0]
	dblock, err := factom.GetDBlock(keymr)
	if err != nil {
		errorln(err)
		return
	}

	fmt.Println("PrevBlockKeyMR:", dblock.Header.PrevBlockKeyMR)
	fmt.Println("Timestamp:", dblock.Header.Timestamp)
	fmt.Println("SequenceNumber:", dblock.Header.SequenceNumber)

	for _, v := range dblock.EntryBlockList {
		fmt.Println("EntryBlock {")
		fmt.Println("	ChainID", v.ChainID)
		fmt.Println("	KeyMR", v.KeyMR)
		fmt.Println("}")
	}
}

func getChain(args []string) {
	os.Args = args
	flag.Parse()
	args = flag.Args()
	if len(args) < 1 {
		man("getChain")
		return
	}

	chainid := args[0]
	chain, err := factom.GetChainHead(chainid)
	if err != nil {
		errorln(err)
		return
	}

	fmt.Println(chain.ChainHead)
}

func getEBlock(args []string) {
	os.Args = args
	flag.Parse()
	args = flag.Args()
	if len(args) < 1 {
		man("getEBlock")
		return
	}

	keymr := args[0]
	eblock, err := factom.GetEBlock(keymr)
	if err != nil {
		errorln(err)
		return
	}

	fmt.Println("BlockSequenceNumber:", eblock.Header.BlockSequenceNumber)
	fmt.Println("ChainID:", eblock.Header.ChainID)
	fmt.Println("PrevKeyMR:", eblock.Header.PrevKeyMR)
	fmt.Println("Timestamp:", eblock.Header.Timestamp)

	for _, v := range eblock.EntryList {
		fmt.Println("EBEntry {")
		fmt.Println("	Timestamp", v.Timestamp)
		fmt.Println("	EntryHash", v.EntryHash)
		fmt.Println("}")
	}
}

func getEntry(args []string) {
	os.Args = args
	flag.Parse()
	args = flag.Args()
	if len(args) < 1 {
		man("getEntry")
		return
	}

	hash := args[0]
	entry, err := factom.GetEntry(hash)
	if err != nil {
		errorln(err)
		return
	}

	fmt.Println("ChainID:", entry.ChainID)
	for _, v := range entry.ExtIDs {
		fmt.Println("ExtID:", v)
	}
	data, _ := hex.DecodeString(entry.Content)

	str := fmt.Sprintf("Content:\n")
	p := data
	for len(p) >= 16 {
		str = str + fmt.Sprintf("% x | % x  || ", p[:8], p[8:16])
		str = str + fmt.Sprintf("%+q\n", p[:16])
		p = p[:16]
	}
	if len(p) > 8 {
		str = str + fmt.Sprintf("% x | % x", p[:8], p[8:])
		for i := len(p); i < 16; i++ {
			str = str + "   "
		}
		str = str + fmt.Sprintf("  || %+q\n", p)
	} else {
		str = str + fmt.Sprintf("% x\n", p)
		for i := len(p); i < 8; i++ {
			str = str + "   "
		}
		str = str + fmt.Sprintf(" |                           || %+q\n", p)
	}
	fmt.Print(str)
}
<commit_msg>entry formatting fix<commit_after>// Copyright 2015 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.

package main

import (
	"crypto/sha256"
	"encoding/hex"
	"flag"
	"fmt"
	"os"

	"github.com/FactomProject/factom"
)

func get(args []string) {
	os.Args = args
	flag.Parse()
	args = flag.Args()
	if len(args) < 1 {
		man("get")
		return
	}

	switch args[0] {
	case "head":
		getHead()
	case "dblock":
		getDBlock(args)
	case "chain":
		getChain(args)
	case "eblock":
		getEBlock(args)
	case "entry":
		getEntry(args)
	case "chainid":
		getChainId(args)
	default:
		man("get")
	}
}

func getHead() {
	head, err := factom.GetDBlockHead()
	if err != nil {
		errorln(err)
		return
	}
	fmt.Println(head.KeyMR)
}

// We expect each element to be its own part in a chain ID
func getChainId(args []string) {
	if len(args) < 2 {
		fmt.Printf("No Chain Specification provided.  See help")
	}
	sum := sha256.New()
	fmt.Println("The chain components:")
	for i, str := range args {
		if i > 0 {
			fmt.Println("    ", str)
			x := sha256.Sum256([]byte(str))
			sum.Write(x[:])
		}
	}
	chainId := sum.Sum(nil)
	fmt.Println("produce the ChainID:")

	fmt.Println("    ", hex.EncodeToString(chainId))
}

func getDBlock(args []string) {
	os.Args = args
	flag.Parse()
	args = flag.Args()
	if len(args) < 1 {
		man("getDBlock")
		return
	}

	keymr := args[0]
	dblock, err := factom.GetDBlock(keymr)
	if err != nil {
		errorln(err)
		return
	}

	fmt.Println("PrevBlockKeyMR:", dblock.Header.PrevBlockKeyMR)
	fmt.Println("Timestamp:", dblock.Header.Timestamp)
	fmt.Println("SequenceNumber:", dblock.Header.SequenceNumber)

	for _, v := range dblock.EntryBlockList {
		fmt.Println("EntryBlock {")
		fmt.Println("	ChainID", v.ChainID)
		fmt.Println("	KeyMR", v.KeyMR)
		fmt.Println("}")
	}
}

func getChain(args []string) {
	os.Args = args
	flag.Parse()
	args = flag.Args()
	if len(args) < 1 {
		man("getChain")
		return
	}

	chainid := args[0]
	chain, err := factom.GetChainHead(chainid)
	if err != nil {
		errorln(err)
		return
	}

	fmt.Println(chain.ChainHead)
}

func getEBlock(args []string) {
	os.Args = args
	flag.Parse()
	args = flag.Args()
	if len(args) < 1 {
		man("getEBlock")
		return
	}

	keymr := args[0]
	eblock, err := factom.GetEBlock(keymr)
	if err != nil {
		errorln(err)
		return
	}

	fmt.Println("BlockSequenceNumber:", eblock.Header.BlockSequenceNumber)
	fmt.Println("ChainID:", eblock.Header.ChainID)
	fmt.Println("PrevKeyMR:", eblock.Header.PrevKeyMR)
	fmt.Println("Timestamp:", eblock.Header.Timestamp)

	for _, v := range eblock.EntryList {
		fmt.Println("EBEntry {")
		fmt.Println("	Timestamp", v.Timestamp)
		fmt.Println("	EntryHash", v.EntryHash)
		fmt.Println("}")
	}
}

func getEntry(args []string) {
	os.Args = args
	flag.Parse()
	args = flag.Args()
	if len(args) < 1 {
		man("getEntry")
		return
	}

	hash := args[0]
	entry, err := factom.GetEntry(hash)
	if err != nil {
		errorln(err)
		return
	}

	fmt.Println("ChainID:", entry.ChainID)
	for _, v := range entry.ExtIDs {
		fmt.Println("ExtID:", v)
	}
	data, _ := hex.DecodeString(entry.Content)

	fmt.Println("Content:")
	fmt.Println(string(data))
}
<|endoftext|>


<commit_before>package ping

import (
	"bytes"
	"github.com/hsheth2/logs"
	"network/icmpp"
	"network/ipv4p"
	"time"
)

const (
	PING_ECHO_REQUEST_TYPE = 8
	PING_ECHO_REPLY_TYPE   = 0
	PING_ICMP_CODE         = 0
	PING_START_ID          = 8000
)

type Ping_Manager struct {
	// Responding to pings
	input  chan *icmpp.ICMP_In
	output map[string](*ipv4p.IP_Writer)

	// Sending pings
	reply             chan *icmpp.ICMP_In
	currentIdentifier uint16
	identifiers       map[uint16](chan *icmpp.ICMP_In)
}

func NewPing_Manager(icmprm *icmpp.ICMP_Read_Manager) (*Ping_Manager, error) {
	input, err := icmprm.Bind(8)
	if err != nil {
		return nil, err
	}

	reply, err := icmprm.Bind(0)
	if err != nil {
		return nil, err
	}

	pm := &Ping_Manager{
		input:             input,
		output:            make(map[string](*ipv4p.IP_Writer)),
		reply:             reply,
		currentIdentifier: PING_START_ID,
		identifiers:       make(map[uint16](chan *icmpp.ICMP_In)),
	}

	go pm.ping_replier()

	return pm, nil
}

var GlobalPingManager = func() *Ping_Manager {
	pm, err := NewPing_Manager(icmpp.GlobalICMPReadManager)
	if err != nil {
		logs.Error.Fatal(err)
	}
	return pm
}()

func (pm *Ping_Manager) getIP_Writer(ip string) (*ipv4p.IP_Writer, error) {
	if _, ok := pm.output[ip]; !ok {
		wt, err := ipv4p.NewIP_Writer(ip, ipv4p.ICMP_PROTO)
		if err != nil {
			return nil, err
		}
		pm.output[ip] = wt
	}
	return pm.output[ip], nil
}

func (pm *Ping_Manager) ping_response_dealer() {
	for {
		ping := <-pm.reply
		// TODO verify checksum as well
		identNum := uint16(ping.Header.Opt >> 16)
		if _, ok := pm.identifiers[identNum]; !ok {
			logs.Info.Println("Dropped something from response dealer")
			continue
		}
		pm.identifiers[identNum] <- ping
	}
}

func (pm *Ping_Manager) ping_replier() {
	for {
		ping := <-pm.input
		wr, err := pm.getIP_Writer(ping.RIP)
		if err != nil {
			logs.Error.Println(err)
			continue
		}
		logs.Info.Println("replying:", ping)
		go pm.respondTo(wr, ping)
	}
}
func (pm *Ping_Manager) respondTo(writer *ipv4p.IP_Writer, ping *icmpp.ICMP_In) error {
	header := ping.Header
	header.TypeF = PING_ECHO_REPLY_TYPE

	// make packet
	bts, err := header.MarshalICMPHeader()
	if err != nil {
		logs.Error.Println(err)
		return err
	}

	// send
	err = writer.WriteTo(bts)
	if err != nil {
		logs.Error.Println(err)
	}

	return nil
}

func (pm *Ping_Manager) SendPing(ip string, interval, timeout time.Duration, numPings uint16) error {
	pm.currentIdentifier++
	ipWriter, err := pm.getIP_Writer(ip)
	if err != nil {
		logs.Error.Println(err)
		return err
	}

	channel := make(chan *icmpp.ICMP_In)
	pm.identifiers[pm.currentIdentifier] = channel

	seqChannel := make(map[uint16](chan *icmpp.ICMP_In))

	go func() {
		for i := uint16(1); i <= numPings; i++ {
			// prepare packet
			packet := &icmpp.ICMP_Header{
				TypeF: PING_ECHO_REQUEST_TYPE,
				Code:  PING_ICMP_CODE,
				Opt:   uint32(pm.currentIdentifier<<16 | i),
				Data:  []byte("abcdefg"), // TODO legitify
			}

			// make data
			data, err := packet.MarshalICMPHeader()
			if err != nil {
				logs.Error.Println(err)
				return
			}

			// send
			err = ipWriter.WriteTo(data)
			if err != nil {
				logs.Error.Println(err)
				return
			}
			go func(seqChan chan *icmpp.ICMP_In, timeout time.Duration, header *icmpp.ICMP_Header) {
				time1 := time.Now()
				timeoutTimer := time.NewTimer(timeout)
				for {
					select {
					case pingResonse := <-seqChan:
						if !bytes.Equal(pingResonse.Header.Data, header.Data) {
							logs.Info.Println("Dropped packet cuz data !=")
							continue
						}
						time2 := time.Now()
						logs.Info.Printf("%d bytes from %s: icmp_seq=%d time=%f ms", len(header.Data)+icmpp.ICMP_Header_MinSize,
							pingResonse.RIP,
							uint16(header.Opt),
							time2.Sub(time1).Seconds()/1000) // put ttl
					case <-timeoutTimer.C:
						logs.Info.Println("Seq num of", uint16(header.Opt), "timed out")
						return
					}
				}
			}(seqChannel[i], timeout, packet)

			// not last
			if i != numPings {
				time.Sleep(interval)
			}
		}
	}()

	go func(inChan chan *icmpp.ICMP_In, seqChan map[uint16](chan *icmpp.ICMP_In)) {
		// TODO verify IPs
		for {
			icmp_in := <-inChan
			if _, ok := seqChan[uint16(icmp_in.Header.Opt)]; !ok {
				seqChan[uint16(icmp_in.Header.Opt)] = make(chan *icmpp.ICMP_In)
			}
			seqChan[uint16(icmp_in.Header.Opt)] <- icmp_in
		}
	}(channel, seqChannel)

	time.Sleep(time.Duration(numPings) * timeout)
	return nil
}
<commit_msg>stuff<commit_after>package ping

import (
	"bytes"
	"github.com/hsheth2/logs"
	"network/icmpp"
	"network/ipv4p"
	"time"
)

const (
	PING_ECHO_REQUEST_TYPE = 8
	PING_ECHO_REPLY_TYPE   = 0
	PING_ICMP_CODE         = 0
	PING_START_ID          = 8000
)

type Ping_Manager struct {
	// Responding to pings
	input  chan *icmpp.ICMP_In
	output map[string](*ipv4p.IP_Writer)

	// Sending pings
	reply             chan *icmpp.ICMP_In
	currentIdentifier uint16
	identifiers       map[uint16](chan *icmpp.ICMP_In)
}

func NewPing_Manager(icmprm *icmpp.ICMP_Read_Manager) (*Ping_Manager, error) {
	input, err := icmprm.Bind(8)
	if err != nil {
		return nil, err
	}

	reply, err := icmprm.Bind(0)
	if err != nil {
		return nil, err
	}

	pm := &Ping_Manager{
		input:             input,
		output:            make(map[string](*ipv4p.IP_Writer)),
		reply:             reply,
		currentIdentifier: PING_START_ID,
		identifiers:       make(map[uint16](chan *icmpp.ICMP_In)),
	}

	go pm.ping_replier()
	go pm.ping_response_dealer()

	return pm, nil
}

var GlobalPingManager = func() *Ping_Manager {
	pm, err := NewPing_Manager(icmpp.GlobalICMPReadManager)
	if err != nil {
		logs.Error.Fatal(err)
	}
	return pm
}()

func (pm *Ping_Manager) getIP_Writer(ip string) (*ipv4p.IP_Writer, error) {
	if _, ok := pm.output[ip]; !ok {
		wt, err := ipv4p.NewIP_Writer(ip, ipv4p.ICMP_PROTO)
		if err != nil {
			return nil, err
		}
		pm.output[ip] = wt
	}
	return pm.output[ip], nil
}

func (pm *Ping_Manager) ping_response_dealer() {
	for {
		ping := <-pm.reply
		// TODO verify checksum as well
		identNum := uint16(ping.Header.Opt >> 16)
		if _, ok := pm.identifiers[identNum]; !ok {
			logs.Info.Println("Dropped something from response dealer, identnum=", identNum, "options=", ping.Header.Opt)
			continue
		}
		pm.identifiers[identNum] <- ping
	}
}

func (pm *Ping_Manager) ping_replier() {
	for {
		ping := <-pm.input
		wr, err := pm.getIP_Writer(ping.RIP)
		if err != nil {
			logs.Error.Println(err)
			continue
		}
		//logs.Info.Println("replying:", ping)
		go pm.respondTo(wr, ping)
	}
}
func (pm *Ping_Manager) respondTo(writer *ipv4p.IP_Writer, ping *icmpp.ICMP_In) error {
	header := ping.Header
	header.TypeF = PING_ECHO_REPLY_TYPE

	// make packet
	bts, err := header.MarshalICMPHeader()
	if err != nil {
		logs.Error.Println(err)
		return err
	}

	// send
	err = writer.WriteTo(bts)
	if err != nil {
		logs.Error.Println(err)
	}

	return nil
}

func (pm *Ping_Manager) SendPing(ip string, interval, timeout time.Duration, numPings uint16) error {
	pm.currentIdentifier++
	ipWriter, err := pm.getIP_Writer(ip)
	if err != nil {
		logs.Error.Println(err)
		return err
	}

	channel := make(chan *icmpp.ICMP_In)
	pm.identifiers[pm.currentIdentifier] = channel

	seqChannel := make(map[uint16](chan *icmpp.ICMP_In))

	go func() {
		for i := uint16(1); i <= numPings; i++ {
			// prepare packet
			packet := &icmpp.ICMP_Header{
				TypeF: PING_ECHO_REQUEST_TYPE,
				Code:  PING_ICMP_CODE,
				Opt:   uint32(pm.currentIdentifier)<<16 | uint32(i),
				Data:  []byte("abcdefg"), // TODO legitify
			}

			// make data
			data, err := packet.MarshalICMPHeader()
			if err != nil {
				logs.Error.Println(err)
				return
			}

			// send
			err = ipWriter.WriteTo(data)
			if err != nil {
				logs.Error.Println(err)
				return
			}
			time1 := time.Now()
			timeoutTimer := time.NewTimer(timeout)
			seqChannel[i] = make(chan *icmpp.ICMP_In)
			go func(seqChan chan *icmpp.ICMP_In, header *icmpp.ICMP_Header, time1 *time.Time, timer *time.Timer) {
				for {
					select {
					case pingResonse := <-seqChan:
						if !bytes.Equal(pingResonse.Header.Data, header.Data) {
							logs.Info.Println("Dropped packet cuz data !=")
							continue
						}
						time2 := time.Now()
						logs.Info.Printf("%d bytes from %s: icmp_seq=%d time=%f ms",
							len(header.Data)+icmpp.ICMP_Header_MinSize,
							pingResonse.RIP,
							uint16(header.Opt),
							time2.Sub(*time1).Seconds()*1000) // put ttl
						return
					case <-timer.C:
						logs.Info.Println("Seq num of", uint16(header.Opt), "timed out")
						return
					}
				}
			}(seqChannel[i], packet, &time1, timeoutTimer)

			// not last
			if i != numPings {
				time.Sleep(interval)
			}
		}
	}()

	go func(inChan chan *icmpp.ICMP_In, seqChan map[uint16](chan *icmpp.ICMP_In)) {
		// TODO verify IPs
		for {
			icmp_in := <-inChan
			//logs.Info.Println("icmp in =",icmp_in.Header.Opt)
			seqChan[uint16(icmp_in.Header.Opt)] <- icmp_in
		}
		//	TODO term plz
	}(channel, seqChannel)

	time.Sleep(time.Duration(numPings) * timeout)
	return nil
}
<|endoftext|>


<commit_before>// Hap - the simple and effective provisioner
// Copyright (c) 2014 Garrett Woodworth (https://github.com/gwoo)
// The BSD License http://opensource.org/licenses/bsd-license.php.

package hap

import (
	"fmt"
	"os/exec"
)

// Git struct
type Git struct {
	Repo string
	Work string
}

// Check whether the git executable exists
func (g Git) Exists() error {
	o, err := exec.LookPath("git")
	if err != nil {
		return fmt.Errorf("%s\n%s", o, err)
	}
	return nil
}

// Add and Commit all files, including untracked to the repo
func (g Git) Commit(message string) ([]byte, error) {
	cmd := exec.Command("git", "add", ".")
	cmd.Dir = g.Work
	result, err := cmd.CombinedOutput()
	if err != nil {
		return result, err
	}
	cmd = exec.Command("git", "commit", "-q", "-m", message)
	cmd.Dir = g.Work
	return cmd.CombinedOutput()
}

// Force push to the remote repo
func (g Git) Push(branch string) ([]byte, error) {
	if branch == "" {
		branch = "master"
	}
	cmd := exec.Command("git", "push", "-f", "-q", g.Repo, branch)
	cmd.Dir = g.Work
	return cmd.CombinedOutput()
}

// Add this hook to the remote repo
const postReceiveHook string = `cat > ".git/hooks/post-receive" << "EOF"
#!/bin/bash

test "${PWD%/.git}" != "$PWD" && cd ..
unset GIT_DIR GIT_WORK_TREE
read oldrev newrev ref
branch=${ref#refs/heads/}
git reset --hard
git checkout ${branch}
EOF`
<commit_msg>Add submodule support to post receive.<commit_after>// Hap - the simple and effective provisioner
// Copyright (c) 2014 Garrett Woodworth (https://github.com/gwoo)
// The BSD License http://opensource.org/licenses/bsd-license.php.

package hap

import (
	"fmt"
	"os/exec"
)

// Git struct
type Git struct {
	Repo string
	Work string
}

// Check whether the git executable exists
func (g Git) Exists() error {
	o, err := exec.LookPath("git")
	if err != nil {
		return fmt.Errorf("%s\n%s", o, err)
	}
	return nil
}

// Add and Commit all files, including untracked to the repo
func (g Git) Commit(message string) ([]byte, error) {
	cmd := exec.Command("git", "add", ".")
	cmd.Dir = g.Work
	result, err := cmd.CombinedOutput()
	if err != nil {
		return result, err
	}
	cmd = exec.Command("git", "commit", "-q", "-m", message)
	cmd.Dir = g.Work
	return cmd.CombinedOutput()
}

// Force push to the remote repo
func (g Git) Push(branch string) ([]byte, error) {
	if branch == "" {
		branch = "master"
	}
	cmd := exec.Command("git", "push", "-f", "-q", g.Repo, branch)
	cmd.Dir = g.Work
	return cmd.CombinedOutput()
}

// Add this hook to the remote repo
const postReceiveHook string = `cat > ".git/hooks/post-receive" << "EOF"
#!/bin/bash

test "${PWD%/.git}" != "$PWD" && cd ..
unset GIT_DIR GIT_WORK_TREE
read oldrev newrev ref
branch=${ref#refs/heads/}
git reset --hard
git checkout ${branch}
git submodule update --init
EOF`
<|endoftext|>


<commit_before>package death

import (
	"fmt"
	"os"
	"os/signal"
	"sync"
)

type SIGNAL string

const (
	TERMINATED SIGNAL = "terminated"
	INTERRUPT  SIGNAL = "interrupt"
	UNKNOWN    SIGNAL = "unknown"
)

type Death struct {
	wg           sync.WaitGroup
	sigChannel   chan os.Signal
	deathChannel chan string
	deathSignals map[SIGNAL]bool
}

func NewDeath() (death *Death) {
	death = &Death{}
	death.sigChannel = make(chan os.Signal, 10)
	death.deathChannel = make(chan string, 10)
	signal.Notify(death.sigChannel)
	go listenForSignal(death.sigChannel, death.deathChannel)
	return death
}

func (d *Death) SetDeathSignals(signals ...SIGNAL) {
	for _, sig := range signals {
		d.deathSignals[sig] = true
	}
}

func (d *Death) RemoveDeathSignals(signals ...SIGNAL) {
	for _, sig := range signals {
		delete(d.deathSignals, sig)
	}
}

func getSignal(sig string) SIGNAL {
	switch sig {
	case string(TERMINATED):
		{
			return TERMINATED
		}
	case string(INTERRUPT):
		{
			return INTERRUPT
		}
	default:
		{
			fmt.Println("Unknown signal received: ", sig)
			return UNKNOWN
		}
	}
}

func (d *Death) listenForDeath() {
	d.wg.Add(1)
	defer d.wg.Done()
	for sig := range d.deathChannel {
		signal := getSignal(sig)
		_, ok := d.deathSignals[signal]
		if ok {
			return
		}
	}
}

//Wait for death
func (d *Death) WaitForDeath() {
	d.wg.Wait()
}

//Manage death of application by signal
func listenForSignal(c <-chan os.Signal, death chan string) {
	for sig := range c {
		death <- sig.String()
	}
}
<commit_msg>cleanup<commit_after>package death

import (
	"fmt"
	"os"
	"os/signal"
	"sync"
)

type SIGNAL string

const (
	TERMINATED SIGNAL = "terminated"
	INTERRUPT  SIGNAL = "interrupt"
	UNKNOWN    SIGNAL = "unknown"
)

type Death struct {
	wg           sync.WaitGroup
	sigChannel   chan os.Signal
	deathChannel chan string
	deathSignals map[SIGNAL]bool
}

func NewDeath() (death *Death) {
	death = &Death{}
	death.sigChannel = make(chan os.Signal, 10)
	death.deathChannel = make(chan string, 10)
	signal.Notify(death.sigChannel)
	go listenForSignal(death.sigChannel, death.deathChannel)
	return death
}

func (d *Death) SetDeathSignals(signals ...SIGNAL) {
	for _, sig := range signals {
		d.deathSignals[sig] = true
	}
}

func (d *Death) RemoveDeathSignals(signals ...SIGNAL) {
	for _, sig := range signals {
		delete(d.deathSignals, sig)
	}
}

func getSignal(sig string) SIGNAL {
	switch sig {
	case string(TERMINATED):
		{
			return TERMINATED
		}
	case string(INTERRUPT):
		{
			return INTERRUPT
		}
	default:
		{
			fmt.Println("Unknown signal received: ", sig)
			return UNKNOWN
		}
	}
}

func (d *Death) listenForDeath() {
	for sig := range d.deathChannel {
		signal := getSignal(sig)
		_, ok := d.deathSignals[signal]
		if ok {
			return
		}
	}
}

//Wait for death
func (d *Death) WaitForDeath() {
	d.listenForDeath()
}

//Manage death of application by signal
func listenForSignal(c <-chan os.Signal, death chan string) {
	for sig := range c {
		death <- sig.String()
	}
}

func (d *Death) Close() {
	if nil != d {
		close(d.sigChannel)
		close(d.deathChannel)
	}
}
<|endoftext|>


<commit_before>/*
Package gme decodes game music files.

This package requires cgo and uses code from
http://blargg.8bitalley.com/libs/audio.html#Game_Music_Emu.
*/
package gme

/*
#include "gme.h"
static short short_index(short *s, int i) {
  return s[i];
}
*/
import "C"

import (
	"fmt"
	"io"
	"time"
	"unsafe"
)

const (
	FadeLength = time.Second * 8
)

var (
	// InfoOnly is the sample rate to New if only track information is needed.
	InfoOnly = C.gme_info_only
)

// New opens the file from b with given sample rate.
func New(b []byte, sampleRate int) (*GME, error) {
	var g GME
	data := unsafe.Pointer(&b[0])
	cerror := C.gme_open_data(data, C.long(len(b)), &g.emu, C.int(sampleRate))
	if err := gmeError(cerror); err != nil {
		return nil, err
	}
	return &g, nil
}

// GME decodes game music.
type GME struct {
	emu *_Ctype_struct_Music_Emu
}

type Track struct {
	PlayLength time.Duration

	// Times; negative if unknown.
	// Length is the total length, if specified by file.
	Length      time.Duration
	IntroLength time.Duration
	LoopLength  time.Duration

	System    string
	Game      string
	Song      string
	Author    string
	Copyright string
	Comment   string
	Dumper    string
}

// Tracks returns the number of tracks in the file.
func (g *GME) Tracks() int {
	return int(C.gme_track_count(g.emu))
}

// Track returns information about the n-th track, 0-based.
func (g *GME) Track(track int) (Track, error) {
	var t *_Ctype_struct_gme_info_t
	cerror := C.gme_track_info(g.emu, &t, C.int(track))
	if err := gmeError(cerror); err != nil {
		return Track{}, err
	}
	return Track{
		PlayLength:  time.Duration(t.play_length) * time.Millisecond,
		Length:      time.Duration(t.length) * time.Millisecond,
		IntroLength: time.Duration(t.intro_length) * time.Millisecond,
		LoopLength:  time.Duration(t.loop_length) * time.Millisecond,
		System:      C.GoString(t.system),
		Game:        C.GoString(t.game),
		Song:        C.GoString(t.song),
		Author:      C.GoString(t.game),
		Copyright:   C.GoString(t.copyright),
		Comment:     C.GoString(t.comment),
		Dumper:      C.GoString(t.dumper),
	}, nil
}

// Start initializes the n-th track for playback, 0-based.
func (g *GME) Start(track int) error {
	err := gmeError(C.gme_start_track(g.emu, C.int(track)))
	if err != nil {
		return err
	}
	t, err := g.Track(track)
	if err != nil {
		return err
	}
	C.gme_set_fade(g.emu, C.int(t.PlayLength/time.Millisecond))
	return nil
}

// Played returns the played time of the current track.
func (g *GME) Played() time.Duration {
	return time.Duration(C.gme_tell(g.emu)) * time.Millisecond
}

// Ended returns whether the current track has ended.
func (g *GME) Ended() bool {
	return C.gme_track_ended(g.emu) == 1
}

// Play decodes the next samples into data. Data is populated with two channels
// interleaved.
func (g *GME) Play(data []int16) (err error) {
	b := make([]C.short, len(data))
	datablock := (*C.short)(unsafe.Pointer(&b[0]))
	cerror := C.gme_play(g.emu, C.int(len(b)), datablock)
	if err := gmeError(cerror); err != nil {
		return err
	}
	for i := range data {
		data[i] = int16(C.short_index(datablock, C.int(i)))
	}
	if g.Ended() {
		return io.EOF
	}
	return nil
}

// Close closes the GME file and frees its used memory.
func (g *GME) Close() {
	if g.emu != nil {
		C.gme_delete(g.emu)
		g.emu = nil
	}
}

// Warning returns the last warning produced and clears it.
func (g *GME) Warning() string {
	if g == nil || g.emu == nil {
		return ""
	}
	return C.GoString(C.gme_warning(g.emu))
}

func gmeError(e _Ctype_gme_err_t) error {
	if e == nil {
		return nil
	}
	return fmt.Errorf("gme: %v", C.GoString(e))
}
<commit_msg>update for go 1.12<commit_after>/*
Package gme decodes game music files.

This package requires cgo and uses code from
http://blargg.8bitalley.com/libs/audio.html#Game_Music_Emu.
*/
package gme

/*
#include "gme.h"
static short short_index(short *s, int i) {
  return s[i];
}
*/
import "C"

import (
	"fmt"
	"io"
	"time"
	"unsafe"
)

const (
	FadeLength = time.Second * 8
)

var (
	// InfoOnly is the sample rate to New if only track information is needed.
	InfoOnly = C.gme_info_only
)

// New opens the file from b with given sample rate.
func New(b []byte, sampleRate int) (*GME, error) {
	var g GME
	data := unsafe.Pointer(&b[0])
	cerror := C.gme_open_data(data, C.long(len(b)), &g.emu, C.int(sampleRate))
	if err := gmeError(cerror); err != nil {
		return nil, err
	}
	return &g, nil
}

// GME decodes game music.
type GME struct {
	emu *C.struct_Music_Emu
}

type Track struct {
	PlayLength time.Duration

	// Times; negative if unknown.
	// Length is the total length, if specified by file.
	Length      time.Duration
	IntroLength time.Duration
	LoopLength  time.Duration

	System    string
	Game      string
	Song      string
	Author    string
	Copyright string
	Comment   string
	Dumper    string
}

// Tracks returns the number of tracks in the file.
func (g *GME) Tracks() int {
	return int(C.gme_track_count(g.emu))
}

// Track returns information about the n-th track, 0-based.
func (g *GME) Track(track int) (Track, error) {
	var t *C.struct_gme_info_t
	cerror := C.gme_track_info(g.emu, &t, C.int(track))
	if err := gmeError(cerror); err != nil {
		return Track{}, err
	}
	return Track{
		PlayLength:  time.Duration(t.play_length) * time.Millisecond,
		Length:      time.Duration(t.length) * time.Millisecond,
		IntroLength: time.Duration(t.intro_length) * time.Millisecond,
		LoopLength:  time.Duration(t.loop_length) * time.Millisecond,
		System:      C.GoString(t.system),
		Game:        C.GoString(t.game),
		Song:        C.GoString(t.song),
		Author:      C.GoString(t.game),
		Copyright:   C.GoString(t.copyright),
		Comment:     C.GoString(t.comment),
		Dumper:      C.GoString(t.dumper),
	}, nil
}

// Start initializes the n-th track for playback, 0-based.
func (g *GME) Start(track int) error {
	err := gmeError(C.gme_start_track(g.emu, C.int(track)))
	if err != nil {
		return err
	}
	t, err := g.Track(track)
	if err != nil {
		return err
	}
	C.gme_set_fade(g.emu, C.int(t.PlayLength/time.Millisecond))
	return nil
}

// Played returns the played time of the current track.
func (g *GME) Played() time.Duration {
	return time.Duration(C.gme_tell(g.emu)) * time.Millisecond
}

// Ended returns whether the current track has ended.
func (g *GME) Ended() bool {
	return C.gme_track_ended(g.emu) == 1
}

// Play decodes the next samples into data. Data is populated with two channels
// interleaved.
func (g *GME) Play(data []int16) (err error) {
	b := make([]C.short, len(data))
	datablock := (*C.short)(unsafe.Pointer(&b[0]))
	cerror := C.gme_play(g.emu, C.int(len(b)), datablock)
	if err := gmeError(cerror); err != nil {
		return err
	}
	for i := range data {
		data[i] = int16(C.short_index(datablock, C.int(i)))
	}
	if g.Ended() {
		return io.EOF
	}
	return nil
}

// Close closes the GME file and frees its used memory.
func (g *GME) Close() {
	if g.emu != nil {
		C.gme_delete(g.emu)
		g.emu = nil
	}
}

// Warning returns the last warning produced and clears it.
func (g *GME) Warning() string {
	if g == nil || g.emu == nil {
		return ""
	}
	return C.GoString(C.gme_warning(g.emu))
}

func gmeError(e C.gme_err_t) error {
	if e == nil {
		return nil
	}
	return fmt.Errorf("gme: %v", C.GoString(e))
}
<|endoftext|>


<commit_before>package main

import (
  "fmt"
  "net/http"
  "encoding/json"
  "io/ioutil"
  "regexp"
  "io"
  "os"

// memory checking
  // "runtime"
)

type Response map[string]interface{}

func (r Response) String() (s string) {
        b, err := json.Marshal(r)
        if err != nil {
                s = ""
                return
        }
        s = string(b)
        return
}

// fi, err := os.Open("input.txt")
//     if err != nil { panic(err) }
//     // close fi on exit and check for its returned error
//     defer func() {
//         if err := fi.Close(); err != nil {
//             panic(err)
//         }
//     }()

//     // open output file

//     // make a buffer to keep chunks that are read
//     buf := make([]byte, 1024)
//     for {
//         // read a chunk
//         n, err := fi.Read(buf)
//         if err != nil && err != io.EOF { panic(err) }
//         if n == 0 { break }

//         // write a chunk
//         if _, err := fo.Write(buf[:n]); err != nil {
//             panic(err)
//         }
//     }

func fileSend(w http.ResponseWriter, path string) {
  w.Header().Set("Content-Type", "application/octet-stream")

  buf := make([]byte, 1024)
  re := regexp.MustCompile("(.*)/file")
  fmt.Printf("%s\n", re.FindStringSubmatch(path)[1])

  fileIn, err := os.Open(fmt.Sprintf("%s%s", config.Root, path))
  if err != nil { panic(err) }
  defer func() {
    if err := fileIn.Close(); err != nil {
      panic(err)
    }
  }()

  for {
    // read a chunk
    n, err := fileIn.Read(buf)
    if err != nil && err != io.EOF { panic(err) }
    if n == 0 { break }

    // write a chunk
    if _, err := w.Write(buf[:n]); err != nil {
      panic(err)
    }
  }

  // fmt.Fprint(w, "thanks for all the bytes (%d)!\n", total)
}

func fileInfo(w http.ResponseWriter, path string) {
  f, err := os.Stat(fmt.Sprintf("%s%s/data", config.Root, path))
  if err != nil {
    http.Error(w, err.Error(), 500)
  }
  fmt.Fprint(w, Response{"size:": f.Size()})
  // fmt.Println("name:", f.Name())
  // fmt.Println("size:", f.Size(), "bytes")
  // fmt.Println("mode:", f.Mode())
  // fmt.Println("time:", f.ModTime())
}

func fileRecieve(w http.ResponseWriter, r *http.Request) {
  w.Header().Set("Content-Type", "application/json")

  buf := make([]byte, 1024)
  total := 0

  err := os.MkdirAll(fmt.Sprintf("%s%s/", config.Root, r.URL.Path), 0777)
  if err != nil {
    http.Error(w, err.Error(), 500)
  }
  fileOut, err := os.Create(fmt.Sprintf("%s%s/data", config.Root, r.URL.Path))
  if err != nil { panic(err) }
  defer func() {
    if err := fileOut.Close(); err != nil {
      panic(err)
    }
  }()

  for {
    // read a chunk
    n, err := r.Body.Read(buf)
    total += n
    if err != nil && err != io.EOF { panic(err) }
    if n == 0 { break }

    // write a chunk
   if _, err := fileOut.Write(buf[:n]); err != nil {
      panic(err)
    }
  }

  fmt.Fprint(w, Response{"success": true, "length": total})
  // fmt.Fprint(w, "thanks for all the bytes (%d)!\n", total)
}

func handler(w http.ResponseWriter, r *http.Request) {

  switch r.Method {
  case "GET":
    file, _ := regexp.MatchString("/file$", r.URL.Path)
    data, _ := regexp.MatchString("/data$", r.URL.Path)
    if file || data {
      fileSend(w, r.URL.Path)
    } else {
      fileInfo(w, r.URL.Path)
    }
  case "POST":
    fileRecieve(w, r)
  }

// MEMORY STUFF
  // memstats := new(runtime.MemStats)
  // runtime.ReadMemStats(memstats)
  // runtime.GC()
  // fmt.Println("memstats before GC: bytes = ", memstats.HeapAlloc, " footprint = ", memstats.Sys)
  // fmt.Println("**********")
  // // memstats = new(runtime.MemStats)
  // runtime.ReadMemStats(memstats)
  // fmt.Println("memstats after GC:  bytes = ", memstats.HeapAlloc, " footprint = ", memstats.Sys)
}

func main() {
  http.HandleFunc("/", handler)
  fmt.Printf(":%d\n", config.Port)
  http.ListenAndServe(fmt.Sprintf(":%d", config.Port), nil)
}

// CONFIG STUFF
type Config struct {
  Port int
  Root string
}

var config Config

func init() {
  if len(os.Args) > 1 {
    println(os.Args[1])
    file, e := ioutil.ReadFile(os.Args[1])
    if e != nil {
        println("File error: %v\n", e)
        os.Exit(1)
    }
    println(string(file))
    if e := json.Unmarshal(file, &config); e != nil {
      fmt.Printf("Json error: %v\n", e)
    }
    println(config.Port)
  } else {
    config.Port = 8080
    config.Root = "./storage"
  }
}<commit_msg>pushed all that to gob<commit_after>package main

import (
  "fmt"
  "net/http"
  "encoding/json"
  "io/ioutil"
  "regexp"
  "io"
  "os"

// memory checking
  "runtime"
)

type Response map[string]interface{}

func (r Response) String() (s string) {
        b, err := json.Marshal(r)
        if err != nil {
                s = ""
                return
        }
        s = string(b)
        return
}

// fi, err := os.Open("input.txt")
//     if err != nil { panic(err) }
//     // close fi on exit and check for its returned error
//     defer func() {
//         if err := fi.Close(); err != nil {
//             panic(err)
//         }
//     }()

//     // open output file

//     // make a buffer to keep chunks that are read
//     buf := make([]byte, 1024)
//     for {
//         // read a chunk
//         n, err := fi.Read(buf)
//         if err != nil && err != io.EOF { panic(err) }
//         if n == 0 { break }

//         // write a chunk
//         if _, err := fo.Write(buf[:n]); err != nil {
//             panic(err)
//         }
//     }

func fileSend(w http.ResponseWriter, r *http.Request) {
  if len(r.Header["Token"]) > 0 {
    re := regexp.MustCompile("(.*)/(file|data)")

    token := r.Header["Token"][0]

    key, err := ioutil.ReadFile(fmt.Sprintf("%s%s/key", config.Root, re.FindStringSubmatch(r.URL.Path)[1]))
    if err != nil { http.Error(w, err.Error(), 500) }

    if token == string(key) {

      http.ServeFile(w, r, fmt.Sprintf("%s%s/data", config.Root, re.FindStringSubmatch(r.URL.Path)[1]))

    } else {
      http.Error(w, Response{"success": false, "error": "I didnt get a Token"}.String(), 401)
    }
  } else {
    http.Error(w, Response{"success": false, "error": "I didnt get a Token"}.String(), 401)
  }
}

func fileInfo(w http.ResponseWriter, r *http.Request) {
  if len(r.Header["Token"]) > 0 {
    token := r.Header["Token"][0]
    key, err := ioutil.ReadFile(fmt.Sprintf("%s%s/key", config.Root, r.URL.Path))
    if err != nil { http.Error(w, err.Error(), 500) }

    if token == string(key) {
      f, err := os.Stat(fmt.Sprintf("%s%s/data", config.Root, r.URL.Path))
      if err != nil {
        http.Error(w, err.Error(), 500)
      }
      fmt.Fprint(w, Response{"size:": f.Size()})
    } else {
      http.Error(w, Response{"success": false, "error": "I didnt get a Token"}.String(), 401)
    }

  } else {
    http.Error(w, Response{"success": false, "error": "I didnt get a Token"}.String(), 401)
  }

  // fmt.Println("name:", f.Name())
  // fmt.Println("size:", f.Size(), "bytes")
  // fmt.Println("mode:", f.Mode())
  // fmt.Println("time:", f.ModTime())
}

func fileRecieve(w http.ResponseWriter, r *http.Request) {
  w.Header().Set("Content-Type", "application/json")
  if len(r.Header["Token"]) > 0 {

    err := os.MkdirAll(fmt.Sprintf("%s%s/", config.Root, r.URL.Path), 0777)
    if err != nil {
      http.Error(w, err.Error(), 500)
    }

    token := r.Header["Token"][0]
    key, err := os.Create(fmt.Sprintf("%s%s/key", config.Root, r.URL.Path))
    if err != nil { panic(err) }
    key.Write([]byte(token))
    if err := key.Close(); err != nil {
      panic(err)
    }

    fileOut, err := os.Create(fmt.Sprintf("%s%s/data", config.Root, r.URL.Path))
    if err != nil { panic(err) }
    defer func() {
      if err := fileOut.Close(); err != nil {
        panic(err)
      }
    }()

    buf := make([]byte, 1024)
    total := 0
    for {
      // read a chunk
      n, err := r.Body.Read(buf)
      total += n
      if err != nil && err != io.EOF { panic(err) }
      if n == 0 { break }

      // write a chunk
     if _, err := fileOut.Write(buf[:n]); err != nil {
        panic(err)
      }
    }

    fmt.Fprint(w, Response{"success": true, "length": total})
    // fmt.Fprint(w, "thanks for all the bytes (%d)!\n", total)

  } else {
    http.Error(w, Response{"success": false, "error": "I didnt get a Token"}.String(), 401)
  }

}

func handler(w http.ResponseWriter, r *http.Request) {

  switch r.Method {
  case "GET":
    file, _ := regexp.MatchString("/file$", r.URL.Path)
    data, _ := regexp.MatchString("/data$", r.URL.Path)
    if file || data {
      fileSend(w, r)
    } else {
      fileInfo(w, r)
    }
  case "POST":
    fileRecieve(w, r)
  }

// MEMORY STUFF
  memstats := new(runtime.MemStats)
  runtime.ReadMemStats(memstats)
  runtime.GC()
  fmt.Println("memstats before GC: bytes = ", memstats.HeapAlloc, " footprint = ", memstats.Sys)
  fmt.Println("**********")
  // memstats = new(runtime.MemStats)
  runtime.ReadMemStats(memstats)
  fmt.Println("memstats after GC:  bytes = ", memstats.HeapAlloc, " footprint = ", memstats.Sys)
}

func main() {
  http.HandleFunc("/", handler)
  fmt.Printf(":%d\n", config.Port)
  http.ListenAndServe(fmt.Sprintf(":%d", config.Port), nil)
}

// CONFIG STUFF
type Config struct {
  Port int
  Root string
}

var config Config

func init() {
  if len(os.Args) > 1 {
    println(os.Args[1])
    file, e := ioutil.ReadFile(os.Args[1])
    if e != nil {
        println("File error: %v\n", e)
        os.Exit(1)
    }
    println(string(file))
    if e := json.Unmarshal(file, &config); e != nil {
      fmt.Printf("Json error: %v\n", e)
    }
    println(config.Port)
  } else {
    config.Port = 8080
    config.Root = "./storage"
  }
}<|endoftext|>


<commit_before>// Copyright © 2016 Thomas Rabaix <thomas.rabaix@gmail.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.

package composer

import (
	"encoding/json"
	"fmt"
	"time"
)

type ProviderInclude map[string]struct {
	Sha256 string `json:"sha256"`
}

type PackagesResult struct {
	Packages         json.RawMessage `json:"packages"`
	Notify           string          `json:"notify"`
	NotifyBatch      string          `json:"notify-batch"`
	ProvidersURL     string          `json:"providers-url"`
	Search           string          `json:"search"`
	ProviderIncludes ProviderInclude `json:"provider-includes"`
}

type ProvidersResult struct {
	Providers map[string]struct {
		Sha256 string `json:"sha256"`
	} `json:"providers"`
	Code string `json:"-"`
}

// package description
type Package struct {
	Name              string   `json:"name"`
	Abandoned         string   `json:"abandoned"`
	Description       string   `json:"description"`
	Keywords          []string `json:"keywords"`
	Homepage          string   `json:"homepage"`
	Version           string   `json:"version"`
	VersionNormalized string   `json:"version_normalized"`
	License           []string `json:"license"`
	Bin               []string `json:"bin"`
	Authors           []struct {
		Name     string `json:"name"`
		Email    string `json:"email"`
		Homepage string `json:"homepage"`
		Role     string `json:"role"`
	} `json:"authors"`
	Source struct {
		Type      string `json:"type"`
		URL       string `json:"url"`
		Reference string `json:"reference"`
	} `json:"source"`
	Dist struct {
		Type      string `json:"type"`
		URL       string `json:"url"`
		Reference string `json:"reference"`
		Shasum    string `json:"shasum"`
	} `json:"dist"`
	Extra         *json.RawMessage `json:"extra"`
	TargetDir     string            `json:"target-dir"`
	Type          string            `json:"type"`
	Time          time.Time         `json:"time"`
	Autoload      *json.RawMessage  `json:"autoload"`
	Conflict      map[string]string `json:"conflict"`
	Provide       map[string]string `json:"provide"`
	Require       map[string]string `json:"require"`
	RequireDevmap map[string]string `json:"require-dev"`
	Suggest       map[string]string `json:"suggest"`
	UID           int               `json:"uid"`
}

// used to load the packages.json file
type PackageResult struct {
	Packages map[string]map[string]*Package `json:"packages"`
}

type PackageInformation struct {
	Server        string        `json:"server"`
	PackageResult PackageResult `json:"-"`
	Package       string        `json:"package"`
	Exist         bool          `json:"-"`
	HashSource    string        `json:"hash_source"`
	HashTarget    string        `json:"hash_target"`
}

func (pi *PackageInformation) GetSourceKey() string {
	return fmt.Sprintf("%s$%s.json", pi.Package, pi.HashSource)
}

func (pi *PackageInformation) GetTargetKey() string {
	return fmt.Sprintf("%s$%s", pi.Package, pi.HashTarget)
}
<commit_msg>fix(composer): fix wront serialization type<commit_after>// Copyright © 2016 Thomas Rabaix <thomas.rabaix@gmail.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.

package composer

import (
	"encoding/json"
	"fmt"
	"time"
)

type ProviderInclude map[string]struct {
	Sha256 string `json:"sha256"`
}

type PackagesResult struct {
	Packages         json.RawMessage `json:"packages"`
	Notify           string          `json:"notify"`
	NotifyBatch      string          `json:"notify-batch"`
	ProvidersURL     string          `json:"providers-url"`
	Search           string          `json:"search"`
	ProviderIncludes ProviderInclude `json:"provider-includes"`
}

type ProvidersResult struct {
	Providers map[string]struct {
		Sha256 string `json:"sha256"`
	} `json:"providers"`
	Code string `json:"-"`
}

// package description
type Package struct {
	Name              string   `json:"name"`
	Abandoned         *json.RawMessage   `json:"abandoned"`
	Description       string   `json:"description"`
	Keywords          []string `json:"keywords"`
	Homepage          string   `json:"homepage"`
	Version           string   `json:"version"`
	VersionNormalized string   `json:"version_normalized"`
	License           []string `json:"license"`
	Bin               []string `json:"bin"`
	Authors           []struct {
		Name     string `json:"name"`
		Email    string `json:"email"`
		Homepage string `json:"homepage"`
		Role     string `json:"role"`
	} `json:"authors"`
	Source struct {
		Type      string `json:"type"`
		URL       string `json:"url"`
		Reference string `json:"reference"`
	} `json:"source"`
	Dist struct {
		Type      string `json:"type"`
		URL       string `json:"url"`
		Reference string `json:"reference"`
		Shasum    string `json:"shasum"`
	} `json:"dist"`
	Extra         *json.RawMessage `json:"extra"`
	TargetDir     string            `json:"target-dir"`
	Type          string            `json:"type"`
	Time          time.Time         `json:"time"`
	Autoload      *json.RawMessage  `json:"autoload"`
	Conflict      map[string]string `json:"conflict"`
	Provide       map[string]string `json:"provide"`
	Require       map[string]string `json:"require"`
	RequireDevmap map[string]string `json:"require-dev"`
	Suggest       map[string]string `json:"suggest"`
	UID           int               `json:"uid"`
}

// used to load the packages.json file
type PackageResult struct {
	Packages map[string]map[string]*Package `json:"packages"`
}

type PackageInformation struct {
	Server        string        `json:"server"`
	PackageResult PackageResult `json:"-"`
	Package       string        `json:"package"`
	Exist         bool          `json:"-"`
	HashSource    string        `json:"hash_source"`
	HashTarget    string        `json:"hash_target"`
}

func (pi *PackageInformation) GetSourceKey() string {
	return fmt.Sprintf("%s$%s.json", pi.Package, pi.HashSource)
}

func (pi *PackageInformation) GetTargetKey() string {
	return fmt.Sprintf("%s$%s", pi.Package, pi.HashTarget)
}
<|endoftext|>


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

package build

import (
	"crypto/hmac"
	"fmt"
	"http"
	"json"
	"os"

	"appengine"
	"appengine/datastore"
	"cache"
)

const commitsPerPage = 30

// defaultPackages specifies the Package records to be created by initHandler.
var defaultPackages = []*Package{
	&Package{Name: "Go"},
}

// commitHandler retrieves commit data or records a new commit.
//
// For GET requests it returns a Commit value for the specified
// packagePath and hash.
//
// For POST requests it reads a JSON-encoded Commit value from the request
// body and creates a new Commit entity. It also updates the "tip" Tag for
// each new commit at tip.
//
// This handler is used by a gobuilder process in -commit mode.
func commitHandler(r *http.Request) (interface{}, os.Error) {
	c := appengine.NewContext(r)
	com := new(Commit)

	if r.Method == "GET" {
		com.PackagePath = r.FormValue("packagePath")
		com.Hash = r.FormValue("hash")
		if err := datastore.Get(c, com.Key(c), com); err != nil {
			return nil, fmt.Errorf("getting Commit: %v", err)
		}
		return com, nil
	}
	if r.Method != "POST" {
		return nil, errBadMethod(r.Method)
	}

	// POST request
	defer r.Body.Close()
	if err := json.NewDecoder(r.Body).Decode(com); err != nil {
		return nil, fmt.Errorf("decoding Body: %v", err)
	}
	if len(com.Desc) > maxDatastoreStringLen {
		com.Desc = com.Desc[:maxDatastoreStringLen]
	}
	if err := com.Valid(); err != nil {
		return nil, fmt.Errorf("validating Commit: %v", err)
	}
	defer cache.Tick(c)
	tx := func(c appengine.Context) os.Error {
		return addCommit(c, com)
	}
	return nil, datastore.RunInTransaction(c, tx, nil)
}

// addCommit adds the Commit entity to the datastore and updates the tip Tag.
// It must be run inside a datastore transaction.
func addCommit(c appengine.Context, com *Commit) os.Error {
	var tc Commit // temp value so we don't clobber com
	err := datastore.Get(c, com.Key(c), &tc)
	if err != datastore.ErrNoSuchEntity {
		// if this commit is already in the datastore, do nothing
		if err == nil {
			return nil
		}
		return fmt.Errorf("getting Commit: %v", err)
	}
	// get the next commit number
	p, err := GetPackage(c, com.PackagePath)
	if err != nil {
		return fmt.Errorf("GetPackage: %v", err)
	}
	com.Num = p.NextNum
	p.NextNum++
	if _, err := datastore.Put(c, p.Key(c), p); err != nil {
		return fmt.Errorf("putting Package: %v", err)
	}
	// if this isn't the first Commit test the parent commit exists
	if com.Num > 0 {
		n, err := datastore.NewQuery("Commit").
			Filter("Hash =", com.ParentHash).
			Ancestor(p.Key(c)).
			Count(c)
		if err != nil {
			return fmt.Errorf("testing for parent Commit: %v", err)
		}
		if n == 0 {
			return os.NewError("parent commit not found")
		}
	}
	// update the tip Tag if this is the Go repo
	if p.Path == "" {
		t := &Tag{Kind: "tip", Hash: com.Hash}
		if _, err = datastore.Put(c, t.Key(c), t); err != nil {
			return fmt.Errorf("putting Tag: %v", err)
		}
	}
	// put the Commit
	if _, err = datastore.Put(c, com.Key(c), com); err != nil {
		return fmt.Errorf("putting Commit: %v", err)
	}
	return nil
}

// tagHandler records a new tag. It reads a JSON-encoded Tag value from the
// request body and updates the Tag entity for the Kind of tag provided.
//
// This handler is used by a gobuilder process in -commit mode.
func tagHandler(r *http.Request) (interface{}, os.Error) {
	if r.Method != "POST" {
		return nil, errBadMethod(r.Method)
	}

	t := new(Tag)
	defer r.Body.Close()
	if err := json.NewDecoder(r.Body).Decode(t); err != nil {
		return nil, err
	}
	if err := t.Valid(); err != nil {
		return nil, err
	}
	c := appengine.NewContext(r)
	defer cache.Tick(c)
	_, err := datastore.Put(c, t.Key(c), t)
	return nil, err
}

// Todo is a todoHandler response.
type Todo struct {
	Kind string // "build-go-commit" or "build-package"
	Data interface{}
}

// todoHandler returns the next action to be performed by a builder.
// It expects "builder" and "kind" query parameters and returns a *Todo value.
// Multiple "kind" parameters may be specified.
func todoHandler(r *http.Request) (interface{}, os.Error) {
	c := appengine.NewContext(r)
	now := cache.Now(c)
	key := "build-todo-" + r.Form.Encode()
	cachedTodo := new(Todo)
	if cache.Get(r, now, key, cachedTodo) {
		return cachedTodo, nil
	}
	var todo *Todo
	var err os.Error
	builder := r.FormValue("builder")
	for _, kind := range r.Form["kind"] {
		var data interface{}
		switch kind {
		case "build-go-commit":
			data, err = buildTodo(c, builder, "", "")
		case "build-package":
			packagePath := r.FormValue("packagePath")
			goHash := r.FormValue("goHash")
			data, err = buildTodo(c, builder, packagePath, goHash)
		}
		if data != nil || err != nil {
			todo = &Todo{Kind: kind, Data: data}
			break
		}
	}
	if err == nil {
		cache.Set(r, now, key, todo)
	}
	return todo, err
}

// buildTodo returns the next Commit to be built (or nil if none available).
//
// If packagePath and goHash are empty, it scans the first 20 Go Commits in
// Num-descending order and returns the first one it finds that doesn't have a
// Result for this builder.
//
// If provided with non-empty packagePath and goHash args, it scans the first
// 20 Commits in Num-descending order for the specified packagePath and
// returns the first that doesn't have a Result for this builder and goHash.
func buildTodo(c appengine.Context, builder, packagePath, goHash string) (interface{}, os.Error) {
	p, err := GetPackage(c, packagePath)
	if err != nil {
		return nil, err
	}

	t := datastore.NewQuery("Commit").
		Ancestor(p.Key(c)).
		Limit(commitsPerPage).
		Order("-Num").
		Run(c)
	for {
		com := new(Commit)
		if _, err := t.Next(com); err != nil {
			if err == datastore.Done {
				err = nil
			}
			return nil, err
		}
		if com.Result(builder, goHash) == nil {
			return com, nil
		}
	}
	panic("unreachable")
}

// packagesHandler returns a list of the non-Go Packages monitored
// by the dashboard.
func packagesHandler(r *http.Request) (interface{}, os.Error) {
	c := appengine.NewContext(r)
	now := cache.Now(c)
	const key = "build-packages"
	var p []*Package
	if cache.Get(r, now, key, &p) {
		return p, nil
	}
	p, err := Packages(c)
	if err != nil {
		return nil, err
	}
	cache.Set(r, now, key, p)
	return p, nil
}

// resultHandler records a build result.
// It reads a JSON-encoded Result value from the request body,
// creates a new Result entity, and updates the relevant Commit entity.
// If the Log field is not empty, resultHandler creates a new Log entity
// and updates the LogHash field before putting the Commit entity.
func resultHandler(r *http.Request) (interface{}, os.Error) {
	if r.Method != "POST" {
		return nil, errBadMethod(r.Method)
	}

	c := appengine.NewContext(r)
	res := new(Result)
	defer r.Body.Close()
	if err := json.NewDecoder(r.Body).Decode(res); err != nil {
		return nil, fmt.Errorf("decoding Body: %v", err)
	}
	if err := res.Valid(); err != nil {
		return nil, fmt.Errorf("validating Result: %v", err)
	}
	defer cache.Tick(c)
	// store the Log text if supplied
	if len(res.Log) > 0 {
		hash, err := PutLog(c, res.Log)
		if err != nil {
			return nil, fmt.Errorf("putting Log: %v", err)
		}
		res.LogHash = hash
	}
	tx := func(c appengine.Context) os.Error {
		// check Package exists
		if _, err := GetPackage(c, res.PackagePath); err != nil {
			return fmt.Errorf("GetPackage: %v", err)
		}
		// put Result
		if _, err := datastore.Put(c, res.Key(c), res); err != nil {
			return fmt.Errorf("putting Result: %v", err)
		}
		// add Result to Commit
		com := &Commit{PackagePath: res.PackagePath, Hash: res.Hash}
		if err := com.AddResult(c, res); err != nil {
			return fmt.Errorf("AddResult: %v", err)
		}
		// Send build failure notifications, if necessary.
		// Note this must run after the call AddResult, which
		// populates the Commit's ResultData field.
		return notifyOnFailure(c, com, res.Builder)
	}
	return nil, datastore.RunInTransaction(c, tx, nil)
}

// logHandler displays log text for a given hash.
// It handles paths like "/log/hash".
func logHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-type", "text/plain")
	c := appengine.NewContext(r)
	hash := r.URL.Path[len("/log/"):]
	key := datastore.NewKey(c, "Log", hash, 0, nil)
	l := new(Log)
	if err := datastore.Get(c, key, l); err != nil {
		logErr(w, r, err)
		return
	}
	b, err := l.Text()
	if err != nil {
		logErr(w, r, err)
		return
	}
	w.Write(b)
}

type dashHandler func(*http.Request) (interface{}, os.Error)

type dashResponse struct {
	Response interface{}
	Error    string
}

// errBadMethod is returned by a dashHandler when
// the request has an unsuitable method.
type errBadMethod string

func (e errBadMethod) String() string {
	return "bad method: " + string(e)
}

// AuthHandler wraps a http.HandlerFunc with a handler that validates the
// supplied key and builder query parameters.
func AuthHandler(h dashHandler) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		c := appengine.NewContext(r)

		// Put the URL Query values into r.Form to avoid parsing the
		// request body when calling r.FormValue.
		r.Form = r.URL.Query()

		var err os.Error
		var resp interface{}

		// Validate key query parameter for POST requests only.
		key := r.FormValue("key")
		builder := r.FormValue("builder")
		if r.Method == "POST" && !validKey(c, key, builder) {
			err = os.NewError("invalid key: " + key)
		}

		// Call the original HandlerFunc and return the response.
		if err == nil {
			resp, err = h(r)
		}

		// Write JSON response.
		dashResp := &dashResponse{Response: resp}
		if err != nil {
			c.Errorf("%v", err)
			dashResp.Error = err.String()
		}
		w.Header().Set("Content-Type", "application/json")
		if err = json.NewEncoder(w).Encode(dashResp); err != nil {
			c.Criticalf("encoding response: %v", err)
		}
	}
}

func initHandler(w http.ResponseWriter, r *http.Request) {
	// TODO(adg): devise a better way of bootstrapping new packages
	c := appengine.NewContext(r)
	defer cache.Tick(c)
	for _, p := range defaultPackages {
		if err := datastore.Get(c, p.Key(c), new(Package)); err == nil {
			continue
		} else if err != datastore.ErrNoSuchEntity {
			logErr(w, r, err)
			return
		}
		if _, err := datastore.Put(c, p.Key(c), p); err != nil {
			logErr(w, r, err)
			return
		}
	}
	fmt.Fprint(w, "OK")
}

func keyHandler(w http.ResponseWriter, r *http.Request) {
	builder := r.FormValue("builder")
	if builder == "" {
		logErr(w, r, os.NewError("must supply builder in query string"))
		return
	}
	c := appengine.NewContext(r)
	fmt.Fprint(w, builderKey(c, builder))
}

func init() {
	// admin handlers
	http.HandleFunc("/init", initHandler)
	http.HandleFunc("/key", keyHandler)

	// authenticated handlers
	http.HandleFunc("/commit", AuthHandler(commitHandler))
	http.HandleFunc("/packages", AuthHandler(packagesHandler))
	http.HandleFunc("/result", AuthHandler(resultHandler))
	http.HandleFunc("/tag", AuthHandler(tagHandler))
	http.HandleFunc("/todo", AuthHandler(todoHandler))

	// public handlers
	http.HandleFunc("/log/", logHandler)
}

func validHash(hash string) bool {
	// TODO(adg): correctly validate a hash
	return hash != ""
}

func validKey(c appengine.Context, key, builder string) bool {
	if appengine.IsDevAppServer() {
		return true
	}
	if key == secretKey(c) {
		return true
	}
	return key == builderKey(c, builder)
}

func builderKey(c appengine.Context, builder string) string {
	h := hmac.NewMD5([]byte(secretKey(c)))
	h.Write([]byte(builder))
	return fmt.Sprintf("%x", h.Sum())
}

func logErr(w http.ResponseWriter, r *http.Request, err os.Error) {
	appengine.NewContext(r).Errorf("Error: %v", err)
	w.WriteHeader(http.StatusInternalServerError)
	fmt.Fprint(w, "Error: ", err)
}
<commit_msg>dashboard: fix todo caching nil<commit_after>// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package build

import (
	"crypto/hmac"
	"fmt"
	"http"
	"json"
	"os"

	"appengine"
	"appengine/datastore"
	"cache"
)

const commitsPerPage = 30

// defaultPackages specifies the Package records to be created by initHandler.
var defaultPackages = []*Package{
	&Package{Name: "Go"},
}

// commitHandler retrieves commit data or records a new commit.
//
// For GET requests it returns a Commit value for the specified
// packagePath and hash.
//
// For POST requests it reads a JSON-encoded Commit value from the request
// body and creates a new Commit entity. It also updates the "tip" Tag for
// each new commit at tip.
//
// This handler is used by a gobuilder process in -commit mode.
func commitHandler(r *http.Request) (interface{}, os.Error) {
	c := appengine.NewContext(r)
	com := new(Commit)

	if r.Method == "GET" {
		com.PackagePath = r.FormValue("packagePath")
		com.Hash = r.FormValue("hash")
		if err := datastore.Get(c, com.Key(c), com); err != nil {
			return nil, fmt.Errorf("getting Commit: %v", err)
		}
		return com, nil
	}
	if r.Method != "POST" {
		return nil, errBadMethod(r.Method)
	}

	// POST request
	defer r.Body.Close()
	if err := json.NewDecoder(r.Body).Decode(com); err != nil {
		return nil, fmt.Errorf("decoding Body: %v", err)
	}
	if len(com.Desc) > maxDatastoreStringLen {
		com.Desc = com.Desc[:maxDatastoreStringLen]
	}
	if err := com.Valid(); err != nil {
		return nil, fmt.Errorf("validating Commit: %v", err)
	}
	defer cache.Tick(c)
	tx := func(c appengine.Context) os.Error {
		return addCommit(c, com)
	}
	return nil, datastore.RunInTransaction(c, tx, nil)
}

// addCommit adds the Commit entity to the datastore and updates the tip Tag.
// It must be run inside a datastore transaction.
func addCommit(c appengine.Context, com *Commit) os.Error {
	var tc Commit // temp value so we don't clobber com
	err := datastore.Get(c, com.Key(c), &tc)
	if err != datastore.ErrNoSuchEntity {
		// if this commit is already in the datastore, do nothing
		if err == nil {
			return nil
		}
		return fmt.Errorf("getting Commit: %v", err)
	}
	// get the next commit number
	p, err := GetPackage(c, com.PackagePath)
	if err != nil {
		return fmt.Errorf("GetPackage: %v", err)
	}
	com.Num = p.NextNum
	p.NextNum++
	if _, err := datastore.Put(c, p.Key(c), p); err != nil {
		return fmt.Errorf("putting Package: %v", err)
	}
	// if this isn't the first Commit test the parent commit exists
	if com.Num > 0 {
		n, err := datastore.NewQuery("Commit").
			Filter("Hash =", com.ParentHash).
			Ancestor(p.Key(c)).
			Count(c)
		if err != nil {
			return fmt.Errorf("testing for parent Commit: %v", err)
		}
		if n == 0 {
			return os.NewError("parent commit not found")
		}
	}
	// update the tip Tag if this is the Go repo
	if p.Path == "" {
		t := &Tag{Kind: "tip", Hash: com.Hash}
		if _, err = datastore.Put(c, t.Key(c), t); err != nil {
			return fmt.Errorf("putting Tag: %v", err)
		}
	}
	// put the Commit
	if _, err = datastore.Put(c, com.Key(c), com); err != nil {
		return fmt.Errorf("putting Commit: %v", err)
	}
	return nil
}

// tagHandler records a new tag. It reads a JSON-encoded Tag value from the
// request body and updates the Tag entity for the Kind of tag provided.
//
// This handler is used by a gobuilder process in -commit mode.
func tagHandler(r *http.Request) (interface{}, os.Error) {
	if r.Method != "POST" {
		return nil, errBadMethod(r.Method)
	}

	t := new(Tag)
	defer r.Body.Close()
	if err := json.NewDecoder(r.Body).Decode(t); err != nil {
		return nil, err
	}
	if err := t.Valid(); err != nil {
		return nil, err
	}
	c := appengine.NewContext(r)
	defer cache.Tick(c)
	_, err := datastore.Put(c, t.Key(c), t)
	return nil, err
}

// Todo is a todoHandler response.
type Todo struct {
	Kind string // "build-go-commit" or "build-package"
	Data interface{}
}

// todoHandler returns the next action to be performed by a builder.
// It expects "builder" and "kind" query parameters and returns a *Todo value.
// Multiple "kind" parameters may be specified.
func todoHandler(r *http.Request) (interface{}, os.Error) {
	c := appengine.NewContext(r)
	now := cache.Now(c)
	key := "build-todo-" + r.Form.Encode()
	var todo *Todo
	if cache.Get(r, now, key, &todo) {
		return todo, nil
	}
	var err os.Error
	builder := r.FormValue("builder")
	for _, kind := range r.Form["kind"] {
		var data interface{}
		switch kind {
		case "build-go-commit":
			data, err = buildTodo(c, builder, "", "")
		case "build-package":
			packagePath := r.FormValue("packagePath")
			goHash := r.FormValue("goHash")
			data, err = buildTodo(c, builder, packagePath, goHash)
		}
		if data != nil || err != nil {
			todo = &Todo{Kind: kind, Data: data}
			break
		}
	}
	if err == nil {
		cache.Set(r, now, key, todo)
	}
	return todo, err
}

// buildTodo returns the next Commit to be built (or nil if none available).
//
// If packagePath and goHash are empty, it scans the first 20 Go Commits in
// Num-descending order and returns the first one it finds that doesn't have a
// Result for this builder.
//
// If provided with non-empty packagePath and goHash args, it scans the first
// 20 Commits in Num-descending order for the specified packagePath and
// returns the first that doesn't have a Result for this builder and goHash.
func buildTodo(c appengine.Context, builder, packagePath, goHash string) (interface{}, os.Error) {
	p, err := GetPackage(c, packagePath)
	if err != nil {
		return nil, err
	}

	t := datastore.NewQuery("Commit").
		Ancestor(p.Key(c)).
		Limit(commitsPerPage).
		Order("-Num").
		Run(c)
	for {
		com := new(Commit)
		if _, err := t.Next(com); err != nil {
			if err == datastore.Done {
				err = nil
			}
			return nil, err
		}
		if com.Result(builder, goHash) == nil {
			return com, nil
		}
	}
	panic("unreachable")
}

// packagesHandler returns a list of the non-Go Packages monitored
// by the dashboard.
func packagesHandler(r *http.Request) (interface{}, os.Error) {
	c := appengine.NewContext(r)
	now := cache.Now(c)
	const key = "build-packages"
	var p []*Package
	if cache.Get(r, now, key, &p) {
		return p, nil
	}
	p, err := Packages(c)
	if err != nil {
		return nil, err
	}
	cache.Set(r, now, key, p)
	return p, nil
}

// resultHandler records a build result.
// It reads a JSON-encoded Result value from the request body,
// creates a new Result entity, and updates the relevant Commit entity.
// If the Log field is not empty, resultHandler creates a new Log entity
// and updates the LogHash field before putting the Commit entity.
func resultHandler(r *http.Request) (interface{}, os.Error) {
	if r.Method != "POST" {
		return nil, errBadMethod(r.Method)
	}

	c := appengine.NewContext(r)
	res := new(Result)
	defer r.Body.Close()
	if err := json.NewDecoder(r.Body).Decode(res); err != nil {
		return nil, fmt.Errorf("decoding Body: %v", err)
	}
	if err := res.Valid(); err != nil {
		return nil, fmt.Errorf("validating Result: %v", err)
	}
	defer cache.Tick(c)
	// store the Log text if supplied
	if len(res.Log) > 0 {
		hash, err := PutLog(c, res.Log)
		if err != nil {
			return nil, fmt.Errorf("putting Log: %v", err)
		}
		res.LogHash = hash
	}
	tx := func(c appengine.Context) os.Error {
		// check Package exists
		if _, err := GetPackage(c, res.PackagePath); err != nil {
			return fmt.Errorf("GetPackage: %v", err)
		}
		// put Result
		if _, err := datastore.Put(c, res.Key(c), res); err != nil {
			return fmt.Errorf("putting Result: %v", err)
		}
		// add Result to Commit
		com := &Commit{PackagePath: res.PackagePath, Hash: res.Hash}
		if err := com.AddResult(c, res); err != nil {
			return fmt.Errorf("AddResult: %v", err)
		}
		// Send build failure notifications, if necessary.
		// Note this must run after the call AddResult, which
		// populates the Commit's ResultData field.
		return notifyOnFailure(c, com, res.Builder)
	}
	return nil, datastore.RunInTransaction(c, tx, nil)
}

// logHandler displays log text for a given hash.
// It handles paths like "/log/hash".
func logHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-type", "text/plain")
	c := appengine.NewContext(r)
	hash := r.URL.Path[len("/log/"):]
	key := datastore.NewKey(c, "Log", hash, 0, nil)
	l := new(Log)
	if err := datastore.Get(c, key, l); err != nil {
		logErr(w, r, err)
		return
	}
	b, err := l.Text()
	if err != nil {
		logErr(w, r, err)
		return
	}
	w.Write(b)
}

type dashHandler func(*http.Request) (interface{}, os.Error)

type dashResponse struct {
	Response interface{}
	Error    string
}

// errBadMethod is returned by a dashHandler when
// the request has an unsuitable method.
type errBadMethod string

func (e errBadMethod) String() string {
	return "bad method: " + string(e)
}

// AuthHandler wraps a http.HandlerFunc with a handler that validates the
// supplied key and builder query parameters.
func AuthHandler(h dashHandler) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		c := appengine.NewContext(r)

		// Put the URL Query values into r.Form to avoid parsing the
		// request body when calling r.FormValue.
		r.Form = r.URL.Query()

		var err os.Error
		var resp interface{}

		// Validate key query parameter for POST requests only.
		key := r.FormValue("key")
		builder := r.FormValue("builder")
		if r.Method == "POST" && !validKey(c, key, builder) {
			err = os.NewError("invalid key: " + key)
		}

		// Call the original HandlerFunc and return the response.
		if err == nil {
			resp, err = h(r)
		}

		// Write JSON response.
		dashResp := &dashResponse{Response: resp}
		if err != nil {
			c.Errorf("%v", err)
			dashResp.Error = err.String()
		}
		w.Header().Set("Content-Type", "application/json")
		if err = json.NewEncoder(w).Encode(dashResp); err != nil {
			c.Criticalf("encoding response: %v", err)
		}
	}
}

func initHandler(w http.ResponseWriter, r *http.Request) {
	// TODO(adg): devise a better way of bootstrapping new packages
	c := appengine.NewContext(r)
	defer cache.Tick(c)
	for _, p := range defaultPackages {
		if err := datastore.Get(c, p.Key(c), new(Package)); err == nil {
			continue
		} else if err != datastore.ErrNoSuchEntity {
			logErr(w, r, err)
			return
		}
		if _, err := datastore.Put(c, p.Key(c), p); err != nil {
			logErr(w, r, err)
			return
		}
	}
	fmt.Fprint(w, "OK")
}

func keyHandler(w http.ResponseWriter, r *http.Request) {
	builder := r.FormValue("builder")
	if builder == "" {
		logErr(w, r, os.NewError("must supply builder in query string"))
		return
	}
	c := appengine.NewContext(r)
	fmt.Fprint(w, builderKey(c, builder))
}

func init() {
	// admin handlers
	http.HandleFunc("/init", initHandler)
	http.HandleFunc("/key", keyHandler)

	// authenticated handlers
	http.HandleFunc("/commit", AuthHandler(commitHandler))
	http.HandleFunc("/packages", AuthHandler(packagesHandler))
	http.HandleFunc("/result", AuthHandler(resultHandler))
	http.HandleFunc("/tag", AuthHandler(tagHandler))
	http.HandleFunc("/todo", AuthHandler(todoHandler))

	// public handlers
	http.HandleFunc("/log/", logHandler)
}

func validHash(hash string) bool {
	// TODO(adg): correctly validate a hash
	return hash != ""
}

func validKey(c appengine.Context, key, builder string) bool {
	if appengine.IsDevAppServer() {
		return true
	}
	if key == secretKey(c) {
		return true
	}
	return key == builderKey(c, builder)
}

func builderKey(c appengine.Context, builder string) string {
	h := hmac.NewMD5([]byte(secretKey(c)))
	h.Write([]byte(builder))
	return fmt.Sprintf("%x", h.Sum())
}

func logErr(w http.ResponseWriter, r *http.Request, err os.Error) {
	appengine.NewContext(r).Errorf("Error: %v", err)
	w.WriteHeader(http.StatusInternalServerError)
	fmt.Fprint(w, "Error: ", err)
}
<|endoftext|>


<commit_before>package middleware

import (
	"strings"

	"github.com/gin-gonic/gin"

	oauth_service "github.com/torinos-io/api/service/oauth_service"
	user_store "github.com/torinos-io/api/store/user_store"
	"github.com/torinos-io/api/type/model"
	"github.com/torinos-io/api/type/system"
)

const (
	currentUserContextName = "CurrentUser"
)

// SetCurrentUser sets current authenticated user from authorization header
func SetCurrentUser(appContext *system.AppContext) gin.HandlerFunc {
	return func(c *gin.Context) {
		h := c.GetHeader("Authorization")

		if h == "" {
			c.Next()
			return
		}

		splited := strings.Split(h, "Bearer")

		if len(splited) < 2 {
			c.Next()
			return
		}

		token := strings.TrimSpace(splited[1])

		userStore := user_store.New(appContext.MainDB)
		service := oauth_service.New(oauth_service.Context{
			Config:    appContext.Config,
			UserStore: userStore,
		})

		user, err := service.FindByAccessToken(token)

		if err != nil {
			c.Next()
			return
		}

		c.Set(currentUserContextName, user)
		c.Next()
	}
}

// GetCurrentUser returns current authenticated user from context
func GetCurrentUser(c *gin.Context) *model.User {
	if v, exists := c.Get(currentUserContextName); exists {
		if user, ok := v.(*model.User); ok {
			return user
		}
	}

	return nil
}
<commit_msg>Use regexp to get token<commit_after>package middleware

import (
	"regexp"

	"github.com/gin-gonic/gin"

	oauth_service "github.com/torinos-io/api/service/oauth_service"
	user_store "github.com/torinos-io/api/store/user_store"
	"github.com/torinos-io/api/type/model"
	"github.com/torinos-io/api/type/system"
)

const (
	currentUserContextName = "CurrentUser"
)

// SetCurrentUser sets current authenticated user from authorization header
func SetCurrentUser(appContext *system.AppContext) gin.HandlerFunc {
	return func(c *gin.Context) {
		authorizationHeaderPattern := regexp.MustCompile(`^Bearer\s(?:["']?)([^"]+)(?:["']?)$`)

		h := c.GetHeader("Authorization")

		if h == "" {
			c.Next()
			return
		}

		m := authorizationHeaderPattern.FindStringSubmatch(h)

		if len(m) != 2 {
			c.Next()
			return
		}

		if m[1] == "" {
			c.Next()
			return
		}

		userStore := user_store.New(appContext.MainDB)
		service := oauth_service.New(oauth_service.Context{
			Config:    appContext.Config,
			UserStore: userStore,
		})

		user, err := service.FindByAccessToken(m[1])

		if err != nil {
			c.Next()
			return
		}

		c.Set(currentUserContextName, user)
		c.Next()
	}
}

// GetCurrentUser returns current authenticated user from context
func GetCurrentUser(c *gin.Context) *model.User {
	if v, exists := c.Get(currentUserContextName); exists {
		if user, ok := v.(*model.User); ok {
			return user
		}
	}

	return nil
}
<|endoftext|>