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

// Package encoding defines an interface for character encodings, such as Shift
// JIS and Windows 1252, that can convert to and from UTF-8.
//
// To convert the bytes of an io.Reader r from the encoding e to UTF-8:
//	rInUTF8 := transform.NewReader(r, e.NewDecoder())
// and to convert from UTF-8 to the encoding e:
//	wInUTF8 := transform.NewWriter(w, e.NewEncoder())
// In both cases, import "golang.org/x/text/transform".
//
// Encoding implementations are provided in other packages, such as
// golang.org/x/text/encoding/charmap and
// golang.org/x/text/encoding/japanese.
package encoding

import (
	"errors"
	"unicode/utf8"

	"golang.org/x/text/transform"
)

// Encoding is a character set encoding that can be transformed to and from
// UTF-8.
type Encoding interface {
	// NewDecoder returns a transformer that converts to UTF-8.
	//
	// Transforming source bytes that are not of that encoding will not
	// result in an error per se. Each byte that cannot be transcoded will
	// be represented in the output by the UTF-8 encoding of '\uFFFD', the
	// replacement rune.
	NewDecoder() transform.Transformer

	// NewEncoder returns a transformer that converts from UTF-8.
	//
	// Transforming source bytes that are not valid UTF-8 will not result in
	// an error per se. Each rune that cannot be transcoded will be
	// represented in the output by an encoding-specific replacement such as
	// "\x1a" (the ASCII substitute character) or "\xff\xfd". To return
	// early with error instead, use transform.Chain to preprocess the data
	// with a UTF8Validator.
	NewEncoder() transform.Transformer
}

// ASCIISub is the ASCII substitute character, as recommended by
// http://unicode.org/reports/tr36/#Text_Comparison
const ASCIISub = '\x1a'

// Nop is the nop encoding. Its transformed bytes are the same as the source
// bytes; it does not replace invalid UTF-8 sequences.
var Nop Encoding = nop{}

type nop struct{}

func (nop) NewDecoder() transform.Transformer {
	return transform.Nop
}

func (nop) NewEncoder() transform.Transformer {
	return transform.Nop
}

// Replacement is the replacement encoding. Decoding from the replacement
// encoding yields a single '\uFFFD' replacement rune. Encoding from UTF-8 to
// the replacement encoding yields the same as the source bytes except that
// invalid UTF-8 is converted to '\uFFFD'.
//
// It is defined at http://encoding.spec.whatwg.org/#replacement
var Replacement Encoding = replacement{}

type replacement struct{}

func (replacement) NewDecoder() transform.Transformer {
	return replacementDecoder{}
}

func (replacement) NewEncoder() transform.Transformer {
	return replacementEncoder{}
}

type replacementDecoder struct{ transform.NopResetter }

func (replacementDecoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
	if len(dst) < 3 {
		return 0, 0, transform.ErrShortDst
	}
	if atEOF {
		const fffd = "\ufffd"
		dst[0] = fffd[0]
		dst[1] = fffd[1]
		dst[2] = fffd[2]
		nDst = 3
	}
	return nDst, len(src), nil
}

type replacementEncoder struct{ transform.NopResetter }

func (replacementEncoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
	r, size := rune(0), 0

	for ; nSrc < len(src); nSrc += size {
		r = rune(src[nSrc])

		// Decode a 1-byte rune.
		if r < utf8.RuneSelf {
			size = 1

		} else {
			// Decode a multi-byte rune.
			r, size = utf8.DecodeRune(src[nSrc:])
			if size == 1 {
				// All valid runes of size 1 (those below utf8.RuneSelf) were
				// handled above. We have invalid UTF-8 or we haven't seen the
				// full character yet.
				if !atEOF && !utf8.FullRune(src[nSrc:]) {
					err = transform.ErrShortSrc
					break
				}
				r = '\ufffd'
			}
		}

		if nDst+utf8.RuneLen(r) > len(dst) {
			err = transform.ErrShortDst
			break
		}
		nDst += utf8.EncodeRune(dst[nDst:], r)
	}
	return nDst, nSrc, err
}

// ErrInvalidUTF8 means that a transformer encountered invalid UTF-8.
var ErrInvalidUTF8 = errors.New("encoding: invalid UTF-8")

// UTF8Validator is a transformer that returns ErrInvalidUTF8 on the first
// input byte that is not valid UTF-8.
var UTF8Validator transform.Transformer = utf8Validator{}

type utf8Validator struct{ transform.NopResetter }

func (utf8Validator) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
	n := len(src)
	if n > len(dst) {
		n = len(dst)
	}
	for i := 0; i < n; {
		if c := src[i]; c < utf8.RuneSelf {
			dst[i] = c
			i++
			continue
		}
		_, size := utf8.DecodeRune(src[i:])
		if size == 1 {
			// All valid runes of size 1 (those below utf8.RuneSelf) were
			// handled above. We have invalid UTF-8 or we haven't seen the
			// full character yet.
			err = ErrInvalidUTF8
			if !atEOF && !utf8.FullRune(src[i:]) {
				err = transform.ErrShortSrc
			}
			return i, i, err
		}
		if i+size > len(dst) {
			return i, i, transform.ErrShortDst
		}
		for ; size > 0; size-- {
			dst[i] = src[i]
			i++
		}
	}
	if len(src) > len(dst) {
		err = transform.ErrShortDst
	}
	return n, n, err
}
<commit_msg>auto commit<commit_after>// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package encoding defines an interface for character encodings, such as Shift
// JIS and Windows 1252, that can convert to and from UTF-8.
//
// To convert the bytes of an io.Reader r from the encoding e to UTF-8:
//	rInUTF8 := transform.NewReader(r, e.NewDecoder())
// and to convert from UTF-8 to the encoding e:
//	wInUTF8 := transform.NewWriter(w, e.NewEncoder())
// In both cases, import "golang.org/x/text/transform".
//
// Encoding implementations are provided in other packages, such as
// golang.org/x/text/encoding/charmap and
// golang.org/x/text/encoding/japanese.
package encoding

import (
	"errors"
	"unicode/utf8"

	//"golang.org/x/text/transform"
    "github.com/weisd/golang.org-x-text/transform"
)

// Encoding is a character set encoding that can be transformed to and from
// UTF-8.
type Encoding interface {
	// NewDecoder returns a transformer that converts to UTF-8.
	//
	// Transforming source bytes that are not of that encoding will not
	// result in an error per se. Each byte that cannot be transcoded will
	// be represented in the output by the UTF-8 encoding of '\uFFFD', the
	// replacement rune.
	NewDecoder() transform.Transformer

	// NewEncoder returns a transformer that converts from UTF-8.
	//
	// Transforming source bytes that are not valid UTF-8 will not result in
	// an error per se. Each rune that cannot be transcoded will be
	// represented in the output by an encoding-specific replacement such as
	// "\x1a" (the ASCII substitute character) or "\xff\xfd". To return
	// early with error instead, use transform.Chain to preprocess the data
	// with a UTF8Validator.
	NewEncoder() transform.Transformer
}

// ASCIISub is the ASCII substitute character, as recommended by
// http://unicode.org/reports/tr36/#Text_Comparison
const ASCIISub = '\x1a'

// Nop is the nop encoding. Its transformed bytes are the same as the source
// bytes; it does not replace invalid UTF-8 sequences.
var Nop Encoding = nop{}

type nop struct{}

func (nop) NewDecoder() transform.Transformer {
	return transform.Nop
}

func (nop) NewEncoder() transform.Transformer {
	return transform.Nop
}

// Replacement is the replacement encoding. Decoding from the replacement
// encoding yields a single '\uFFFD' replacement rune. Encoding from UTF-8 to
// the replacement encoding yields the same as the source bytes except that
// invalid UTF-8 is converted to '\uFFFD'.
//
// It is defined at http://encoding.spec.whatwg.org/#replacement
var Replacement Encoding = replacement{}

type replacement struct{}

func (replacement) NewDecoder() transform.Transformer {
	return replacementDecoder{}
}

func (replacement) NewEncoder() transform.Transformer {
	return replacementEncoder{}
}

type replacementDecoder struct{ transform.NopResetter }

func (replacementDecoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
	if len(dst) < 3 {
		return 0, 0, transform.ErrShortDst
	}
	if atEOF {
		const fffd = "\ufffd"
		dst[0] = fffd[0]
		dst[1] = fffd[1]
		dst[2] = fffd[2]
		nDst = 3
	}
	return nDst, len(src), nil
}

type replacementEncoder struct{ transform.NopResetter }

func (replacementEncoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
	r, size := rune(0), 0

	for ; nSrc < len(src); nSrc += size {
		r = rune(src[nSrc])

		// Decode a 1-byte rune.
		if r < utf8.RuneSelf {
			size = 1

		} else {
			// Decode a multi-byte rune.
			r, size = utf8.DecodeRune(src[nSrc:])
			if size == 1 {
				// All valid runes of size 1 (those below utf8.RuneSelf) were
				// handled above. We have invalid UTF-8 or we haven't seen the
				// full character yet.
				if !atEOF && !utf8.FullRune(src[nSrc:]) {
					err = transform.ErrShortSrc
					break
				}
				r = '\ufffd'
			}
		}

		if nDst+utf8.RuneLen(r) > len(dst) {
			err = transform.ErrShortDst
			break
		}
		nDst += utf8.EncodeRune(dst[nDst:], r)
	}
	return nDst, nSrc, err
}

// ErrInvalidUTF8 means that a transformer encountered invalid UTF-8.
var ErrInvalidUTF8 = errors.New("encoding: invalid UTF-8")

// UTF8Validator is a transformer that returns ErrInvalidUTF8 on the first
// input byte that is not valid UTF-8.
var UTF8Validator transform.Transformer = utf8Validator{}

type utf8Validator struct{ transform.NopResetter }

func (utf8Validator) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
	n := len(src)
	if n > len(dst) {
		n = len(dst)
	}
	for i := 0; i < n; {
		if c := src[i]; c < utf8.RuneSelf {
			dst[i] = c
			i++
			continue
		}
		_, size := utf8.DecodeRune(src[i:])
		if size == 1 {
			// All valid runes of size 1 (those below utf8.RuneSelf) were
			// handled above. We have invalid UTF-8 or we haven't seen the
			// full character yet.
			err = ErrInvalidUTF8
			if !atEOF && !utf8.FullRune(src[i:]) {
				err = transform.ErrShortSrc
			}
			return i, i, err
		}
		if i+size > len(dst) {
			return i, i, transform.ErrShortDst
		}
		for ; size > 0; size-- {
			dst[i] = src[i]
			i++
		}
	}
	if len(src) > len(dst) {
		err = transform.ErrShortDst
	}
	return n, n, err
}
<|endoftext|>


<commit_before>package main

import (
	"bufio"
	"fmt"
	"io/ioutil"
	"log"
	"math/rand"
	"os"
	"os/exec"
	"os/signal"
	"path"
	"runtime"
	"syscall"
	"time"

	"github.com/toorop/tmail/core"
	"github.com/toorop/tmail/rest"
	"github.com/toorop/tmail/scanner"
	"github.com/toorop/tmail/scope"

	"github.com/bitly/nsq/nsqd"
	"github.com/codegangsta/cli"
)

const (
	// TMAIL_VERSION version of tmail
	TMAIL_VERSION = "0.0.8.2"
)

func init() {
	runtime.GOMAXPROCS(runtime.NumCPU())
	var err error
	if err = scope.Bootstrap(); err != nil {
		log.Fatalln(err)
	}
	scope.Version = TMAIL_VERSION

	// Check local ip
	/*if _, err = scope.Cfg.GetLocalIps(); err != nil {
		log.Fatalln("bad config parameter TMAIL_DELIVERD_LOCAL_IPS", err.Error())
	}*/

	// Check base path structure
	requiredPaths := []string{"db", "nsq", "ssl"}
	for _, p := range requiredPaths {
		if err = os.MkdirAll(path.Join(core.GetBasePath(), p), 0700); err != nil {
			log.Fatalln("Unable to create path "+path.Join(core.GetBasePath(), p), " - ", err.Error())
		}
	}

	// TODO: if clusterMode check if nsqlookupd is available

	// On vérifie que la base est à jour
	if !dbIsOk(scope.DB) {
		var r []byte
		for {
			fmt.Print(fmt.Sprintf("Database 'driver: %s, source: %s' misses some tables.\r\nShould i create them ? (y/n):", scope.Cfg.GetDbDriver(), scope.Cfg.GetDbSource()))
			r, _, _ = bufio.NewReader(os.Stdin).ReadLine()
			if r[0] == 110 || r[0] == 121 {
				break
			}
		}
		if r[0] == 121 {
			if err = initDB(scope.DB); err != nil {
				log.Fatalln(err)
			}
		} else {
			log.Fatalln("See you soon...")
		}
	}

	// Dovecot support
	if scope.Cfg.GetDovecotSupportEnabled() {
		_, err := exec.LookPath(scope.Cfg.GetDovecotLda())
		if err != nil {
			log.Fatalln("Unable to find Dovecot LDA binary, checks your config poarameter TMAIL_DOVECOT_LDA ", err)
		}
	}

}

// MAIN
func main() {
	rand.Seed(time.Now().UTC().UnixNano())

	// Synch tables to structs
	if err := autoMigrateDB(scope.DB); err != nil {
		log.Fatalln(err)
	}

	app := cli.NewApp()
	app.Name = "tmail"
	app.Usage = "smtp server... and a little more"
	app.Author = "Stéphane Depierrepont aka toorop"
	app.Email = "toorop@toorop.fr"
	app.Version = TMAIL_VERSION
	app.Commands = cliCommands
	app.Action = func(c *cli.Context) {
		if len(c.Args()) != 0 {
			cli.ShowAppHelp(c)
		} else {
			// if there nothing to do do nothing
			if !scope.Cfg.GetLaunchDeliverd() && !scope.Cfg.GetLaunchSmtpd() {
				log.Fatalln("I have nothing to do, so i do nothing. Bye.")
			}

			// Loop
			sigChan := make(chan os.Signal, 1)
			signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)

			// Chanel to comunicate between all elements
			//daChan := make(chan string)

			// nsqd
			opts := nsqd.NewNSQDOptions()
			// logs
			//opts.Logger = log.New(os.Stderr, "[nsqd] ", log.Ldate|log.Ltime|log.Lmicroseconds)
			opts.Logger = log.New(ioutil.Discard, "", 0)
			if scope.Cfg.GetDebugEnabled() {
				opts.Logger = scope.Log
			}
			opts.Verbose = scope.Cfg.GetDebugEnabled() // verbosity
			opts.DataPath = core.GetBasePath() + "/nsq"
			// if cluster get lookupd addresses
			if scope.Cfg.GetClusterModeEnabled() {
				opts.NSQLookupdTCPAddresses = scope.Cfg.GetNSQLookupdTcpAddresses()
			}

			// deflate (compression)
			opts.DeflateEnabled = true

			// if a message timeout it returns to the queue: https://groups.google.com/d/msg/nsq-users/xBQF1q4srUM/kX22TIoIs-QJ
			// msg timeout : base time to wait from consummer before requeuing a message
			// note: deliverd consumer return immediatly (message is handled in a go routine)
			// Ce qui est au dessus est faux malgres la go routine il attends toujours a la réponse
			// et c'est normal car le message est toujours "in flight"
			// En fait ce timeout c'est le temps durant lequel le message peut rester dans le state "in flight"
			// autrement dit c'est le temps maxi que peu prendre deliverd.processMsg
			opts.MsgTimeout = 10 * time.Minute

			// maximum duration before a message will timeout
			opts.MaxMsgTimeout = 15 * time.Hour

			// maximum requeuing timeout for a message
			// je pense que si le client ne demande pas de requeue dans ce delais alors
			// le message et considéré comme traité
			opts.MaxReqTimeout = 1 * time.Hour

			// Number of message in RAM before synching to disk
			opts.MemQueueSize = 0

			nsqd := nsqd.NewNSQD(opts)
			nsqd.LoadMetadata()
			err := nsqd.PersistMetadata()
			if err != nil {
				log.Fatalf("ERROR: failed to persist metadata - %s", err.Error())
			}
			nsqd.Main()

			// smtpd
			if scope.Cfg.GetLaunchSmtpd() {
				// If clamav is enabled test it
				if scope.Cfg.GetSmtpdClamavEnabled() {
					if err = scanner.NewClamav().Ping(); err != nil {
						log.Fatalln("Unable to connect to clamd -", err)
					}
				}

				smtpdDsns, err := core.GetDsnsFromString(scope.Cfg.GetSmtpdDsns())
				if err != nil {
					log.Fatalln("unable to parse smtpd dsn -", err)
				}
				for _, dsn := range smtpdDsns {
					go core.NewSmtpd(dsn).ListenAndServe()
					scope.Log.Info("smtpd " + dsn.String() + " launched.")
				}
			}

			// deliverd
			//deliverd.Scope = scope
			go core.LaunchDeliverd()

			// HTTP REST server
			if scope.Cfg.GetRestServerLaunch() {
				go rest.LaunchServer()
			}

			<-sigChan
			scope.Log.Info("Exiting...")

			// close NsqQueueProducer if exists
			scope.NsqQueueProducer.Stop()

			// flush nsqd memory to disk
			nsqd.Exit()
		}
	}
	app.Run(os.Args)

}
<commit_msg>version<commit_after>package main

import (
	"bufio"
	"fmt"
	"io/ioutil"
	"log"
	"math/rand"
	"os"
	"os/exec"
	"os/signal"
	"path"
	"runtime"
	"syscall"
	"time"

	"github.com/toorop/tmail/core"
	"github.com/toorop/tmail/rest"
	"github.com/toorop/tmail/scanner"
	"github.com/toorop/tmail/scope"

	"github.com/bitly/nsq/nsqd"
	"github.com/codegangsta/cli"
)

const (
	// TMAIL_VERSION version of tmail
	TMAIL_VERSION = "0.0.8.3"
)

func init() {
	runtime.GOMAXPROCS(runtime.NumCPU())
	var err error
	if err = scope.Bootstrap(); err != nil {
		log.Fatalln(err)
	}
	scope.Version = TMAIL_VERSION

	// Check local ip
	/*if _, err = scope.Cfg.GetLocalIps(); err != nil {
		log.Fatalln("bad config parameter TMAIL_DELIVERD_LOCAL_IPS", err.Error())
	}*/

	// Check base path structure
	requiredPaths := []string{"db", "nsq", "ssl"}
	for _, p := range requiredPaths {
		if err = os.MkdirAll(path.Join(core.GetBasePath(), p), 0700); err != nil {
			log.Fatalln("Unable to create path "+path.Join(core.GetBasePath(), p), " - ", err.Error())
		}
	}

	// TODO: if clusterMode check if nsqlookupd is available

	// On vérifie que la base est à jour
	if !dbIsOk(scope.DB) {
		var r []byte
		for {
			fmt.Print(fmt.Sprintf("Database 'driver: %s, source: %s' misses some tables.\r\nShould i create them ? (y/n):", scope.Cfg.GetDbDriver(), scope.Cfg.GetDbSource()))
			r, _, _ = bufio.NewReader(os.Stdin).ReadLine()
			if r[0] == 110 || r[0] == 121 {
				break
			}
		}
		if r[0] == 121 {
			if err = initDB(scope.DB); err != nil {
				log.Fatalln(err)
			}
		} else {
			log.Fatalln("See you soon...")
		}
	}

	// Dovecot support
	if scope.Cfg.GetDovecotSupportEnabled() {
		_, err := exec.LookPath(scope.Cfg.GetDovecotLda())
		if err != nil {
			log.Fatalln("Unable to find Dovecot LDA binary, checks your config poarameter TMAIL_DOVECOT_LDA ", err)
		}
	}

}

// MAIN
func main() {
	rand.Seed(time.Now().UTC().UnixNano())

	// Synch tables to structs
	if err := autoMigrateDB(scope.DB); err != nil {
		log.Fatalln(err)
	}

	app := cli.NewApp()
	app.Name = "tmail"
	app.Usage = "smtp server... and a little more"
	app.Author = "Stéphane Depierrepont aka toorop"
	app.Email = "toorop@toorop.fr"
	app.Version = TMAIL_VERSION
	app.Commands = cliCommands
	app.Action = func(c *cli.Context) {
		if len(c.Args()) != 0 {
			cli.ShowAppHelp(c)
		} else {
			// if there nothing to do do nothing
			if !scope.Cfg.GetLaunchDeliverd() && !scope.Cfg.GetLaunchSmtpd() {
				log.Fatalln("I have nothing to do, so i do nothing. Bye.")
			}

			// Loop
			sigChan := make(chan os.Signal, 1)
			signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)

			// Chanel to comunicate between all elements
			//daChan := make(chan string)

			// nsqd
			opts := nsqd.NewNSQDOptions()
			// logs
			//opts.Logger = log.New(os.Stderr, "[nsqd] ", log.Ldate|log.Ltime|log.Lmicroseconds)
			opts.Logger = log.New(ioutil.Discard, "", 0)
			if scope.Cfg.GetDebugEnabled() {
				opts.Logger = scope.Log
			}
			opts.Verbose = scope.Cfg.GetDebugEnabled() // verbosity
			opts.DataPath = core.GetBasePath() + "/nsq"
			// if cluster get lookupd addresses
			if scope.Cfg.GetClusterModeEnabled() {
				opts.NSQLookupdTCPAddresses = scope.Cfg.GetNSQLookupdTcpAddresses()
			}

			// deflate (compression)
			opts.DeflateEnabled = true

			// if a message timeout it returns to the queue: https://groups.google.com/d/msg/nsq-users/xBQF1q4srUM/kX22TIoIs-QJ
			// msg timeout : base time to wait from consummer before requeuing a message
			// note: deliverd consumer return immediatly (message is handled in a go routine)
			// Ce qui est au dessus est faux malgres la go routine il attends toujours a la réponse
			// et c'est normal car le message est toujours "in flight"
			// En fait ce timeout c'est le temps durant lequel le message peut rester dans le state "in flight"
			// autrement dit c'est le temps maxi que peu prendre deliverd.processMsg
			opts.MsgTimeout = 10 * time.Minute

			// maximum duration before a message will timeout
			opts.MaxMsgTimeout = 15 * time.Hour

			// maximum requeuing timeout for a message
			// je pense que si le client ne demande pas de requeue dans ce delais alors
			// le message et considéré comme traité
			opts.MaxReqTimeout = 1 * time.Hour

			// Number of message in RAM before synching to disk
			opts.MemQueueSize = 0

			nsqd := nsqd.NewNSQD(opts)
			nsqd.LoadMetadata()
			err := nsqd.PersistMetadata()
			if err != nil {
				log.Fatalf("ERROR: failed to persist metadata - %s", err.Error())
			}
			nsqd.Main()

			// smtpd
			if scope.Cfg.GetLaunchSmtpd() {
				// If clamav is enabled test it
				if scope.Cfg.GetSmtpdClamavEnabled() {
					if err = scanner.NewClamav().Ping(); err != nil {
						log.Fatalln("Unable to connect to clamd -", err)
					}
				}

				smtpdDsns, err := core.GetDsnsFromString(scope.Cfg.GetSmtpdDsns())
				if err != nil {
					log.Fatalln("unable to parse smtpd dsn -", err)
				}
				for _, dsn := range smtpdDsns {
					go core.NewSmtpd(dsn).ListenAndServe()
					scope.Log.Info("smtpd " + dsn.String() + " launched.")
				}
			}

			// deliverd
			//deliverd.Scope = scope
			go core.LaunchDeliverd()

			// HTTP REST server
			if scope.Cfg.GetRestServerLaunch() {
				go rest.LaunchServer()
			}

			<-sigChan
			scope.Log.Info("Exiting...")

			// close NsqQueueProducer if exists
			scope.NsqQueueProducer.Stop()

			// flush nsqd memory to disk
			nsqd.Exit()
		}
	}
	app.Run(os.Args)

}
<|endoftext|>


<commit_before>package itembase

import (
	"crypto/rand"
	"errors"
	"fmt"
	"golang.org/x/net/context"
	"golang.org/x/oauth2"
	"log"
	"net/http"
)

func (c *client) newConf() *oauth2.Config {
	var endpointURL string

	if c.production {
		endpointURL = "https://accounts.itembase.com/oauth/v2"
		c.me = "https://users.itembase.com/v1/me"
		c.root = "https://api.itembase.io/v1"
		c.activation = "https://solutionservice.itembase.com"
	} else {
		endpointURL = "http://sandbox.accounts.itembase.io/oauth/v2"
		c.me = "http://sandbox.users.itembase.io/v1/me"
		c.root = "http://sandbox.api.itembase.io/v1"
		c.activation = "http://sandbox.solutionservice.itembase.io"
	}

	return &oauth2.Config{
		ClientID:     c.options.ClientID,
		ClientSecret: c.options.ClientSecret,
		Scopes:       c.options.Scopes,
		RedirectURL:  c.options.RedirectURL,
		Endpoint: oauth2.Endpoint{
			AuthURL:  endpointURL + "/auth",
			TokenURL: endpointURL + "/token",
		},
	}
}

func (c *client) SaveToken(userID string, token *oauth2.Token) (err error) {
	if c.options.TokenHandler.TokenSaver != nil {
		err = c.options.TokenHandler.TokenSaver(userID, token)
	} else {
		err = errors.New("No Token Store!")
	}
	return
}

func (c *client) GetCachedToken(userID string) (token *oauth2.Token, err error) {
	if c.options.TokenHandler.TokenLoader != nil {
		token, err = c.options.TokenHandler.TokenLoader(userID)
	} else {
		err = errors.New("No Token Cache!")
	}
	return
}

func (c *client) GiveTokenPermissions(authURL string) (authcode string, err error) {
	// add logic for handing retrieving code for oauth exchange and matching state
	// For example throw an error, and send email to user instead with this link

	if c.options.TokenHandler.TokenPermissions != nil {
		if authcode, err = c.options.TokenHandler.TokenPermissions(authURL); err != nil {
			log.Fatal(err)
		}
	} else {
		if _, err := fmt.Scan(&authcode); err != nil {
			log.Fatal(err)
		}
	}

	// Use the authorization code that is pushed to the redirect URL.
	// NewTransportWithCode will do the handshake to retrieve
	// an access token and initiate a Transport that is
	// authorized and authenticated by the retrieved token.
	return
}

// UserOAuthClient returns an oauth2 client for a specific user
func (c *client) UserOAuthClient(ctx context.Context, config *oauth2.Config, userID string) (client *http.Client, err error) {
	var userToken *oauth2.Token

	if userToken, err = c.GetCachedToken(userID); err != nil {
		// if token for user is not cached then go through oauth2 flow
		if userToken, err = c.newUserToken(ctx, config, userID); err != nil {
			return
		}
	}

	if !userToken.Valid() { // if user token is expired
		userToken = &oauth2.Token{RefreshToken: userToken.RefreshToken}
	}

	return config.Client(ctx, userToken), err
}

func (c *client) newUserToken(ctx context.Context, config *oauth2.Config, userID string) (*oauth2.Token, error) {
	stateBytes := make([]byte, 32)
	_, err := rand.Read(stateBytes)
	if err != nil {
		log.Fatalf("Unable to read random bytes: %v", err)
		return nil, err
	}
	state := fmt.Sprintf("%x", stateBytes)
	authURL := config.AuthCodeURL(state, oauth2.AccessTypeOffline)

	authcode, err := c.GiveTokenPermissions(authURL)

	token, err := config.Exchange(oauth2.NoContext, authcode)
	if err != nil {
		log.Fatalf("Exchange error: %v", err)
		return nil, err
	}
	c.SaveToken(userID, token) // save token to datastore

	return token, nil
}

func (c *client) getUserToken(userID string) (Token *oauth2.Token) {
	config := c.newConf()
	client, err := c.UserOAuthClient(oauth2.NoContext, config, userID)

	_, err = client.Get(c.me)
	if err == nil {
		fmt.Errorf("Fetch should return an error if no refresh token is set")
	}

	Token, err = client.Transport.(*oauth2.Transport).Source.Token()

	if err != nil {
		log.Fatal("Exchange error: %v", err)
	}

	c.SaveToken(userID, Token)

	return
}
<commit_msg>print URL to go if necessary<commit_after>package itembase

import (
	"crypto/rand"
	"errors"
	"fmt"
	"golang.org/x/net/context"
	"golang.org/x/oauth2"
	"log"
	"net/http"
)

func (c *client) newConf() *oauth2.Config {
	var endpointURL string

	if c.production {
		endpointURL = "https://accounts.itembase.com/oauth/v2"
		c.me = "https://users.itembase.com/v1/me"
		c.root = "https://api.itembase.io/v1"
		c.activation = "https://solutionservice.itembase.com"
	} else {
		endpointURL = "http://sandbox.accounts.itembase.io/oauth/v2"
		c.me = "http://sandbox.users.itembase.io/v1/me"
		c.root = "http://sandbox.api.itembase.io/v1"
		c.activation = "http://sandbox.solutionservice.itembase.io"
	}

	return &oauth2.Config{
		ClientID:     c.options.ClientID,
		ClientSecret: c.options.ClientSecret,
		Scopes:       c.options.Scopes,
		RedirectURL:  c.options.RedirectURL,
		Endpoint: oauth2.Endpoint{
			AuthURL:  endpointURL + "/auth",
			TokenURL: endpointURL + "/token",
		},
	}
}

func (c *client) SaveToken(userID string, token *oauth2.Token) (err error) {
	if c.options.TokenHandler.TokenSaver != nil {
		err = c.options.TokenHandler.TokenSaver(userID, token)
	} else {
		err = errors.New("No Token Store!")
	}
	return
}

func (c *client) GetCachedToken(userID string) (token *oauth2.Token, err error) {
	if c.options.TokenHandler.TokenLoader != nil {
		token, err = c.options.TokenHandler.TokenLoader(userID)
	} else {
		err = errors.New("No Token Cache!")
	}
	return
}

func (c *client) GiveTokenPermissions(authURL string) (authcode string, err error) {
	// add logic for handing retrieving code for oauth exchange and matching state
	// For example throw an error, and send email to user instead with this link

	if c.options.TokenHandler.TokenPermissions != nil {
		if authcode, err = c.options.TokenHandler.TokenPermissions(authURL); err != nil {
			log.Fatal(err)
		}
	} else {
		fmt.Println("Please go to the following URL : ", authURL)
		if _, err := fmt.Scan(&authcode); err != nil {
			log.Fatal(err)
		}
	}

	// Use the authorization code that is pushed to the redirect URL.
	// NewTransportWithCode will do the handshake to retrieve
	// an access token and initiate a Transport that is
	// authorized and authenticated by the retrieved token.
	return
}

// UserOAuthClient returns an oauth2 client for a specific user
func (c *client) UserOAuthClient(ctx context.Context, config *oauth2.Config, userID string) (client *http.Client, err error) {
	var userToken *oauth2.Token

	if userToken, err = c.GetCachedToken(userID); err != nil {
		// if token for user is not cached then go through oauth2 flow
		if userToken, err = c.newUserToken(ctx, config, userID); err != nil {
			return
		}
	}

	if !userToken.Valid() { // if user token is expired
		userToken = &oauth2.Token{RefreshToken: userToken.RefreshToken}
	}

	return config.Client(ctx, userToken), err
}

func (c *client) newUserToken(ctx context.Context, config *oauth2.Config, userID string) (*oauth2.Token, error) {
	stateBytes := make([]byte, 32)
	_, err := rand.Read(stateBytes)
	if err != nil {
		log.Fatalf("Unable to read random bytes: %v", err)
		return nil, err
	}
	state := fmt.Sprintf("%x", stateBytes)
	authURL := config.AuthCodeURL(state, oauth2.AccessTypeOffline)

	authcode, err := c.GiveTokenPermissions(authURL)

	token, err := config.Exchange(oauth2.NoContext, authcode)
	if err != nil {
		log.Fatalf("Exchange error: %v", err)
		return nil, err
	}
	c.SaveToken(userID, token) // save token to datastore

	return token, nil
}

func (c *client) getUserToken(userID string) (Token *oauth2.Token) {
	config := c.newConf()
	client, err := c.UserOAuthClient(oauth2.NoContext, config, userID)

	_, err = client.Get(c.me)
	if err == nil {
		fmt.Errorf("Fetch should return an error if no refresh token is set")
	}

	Token, err = client.Transport.(*oauth2.Transport).Source.Token()

	if err != nil {
		log.Fatal("Exchange error: %v", err)
	}

	c.SaveToken(userID, Token)

	return
}
<|endoftext|>


<commit_before>package ohaus

import (
	"bufio"
	"encoding/json"
	serial "github.com/tarm/serial"
	"log"
	"math/rand"
	"os"
	"strconv"
	"strings"
	"time"
)

type Scale struct {
	PortName string
}

type Datum struct {
	Time   time.Time `json:"time"`
	Weight float64   `json:"weight"`
	Unit   string    `json:"unit"`
}

func (scale Scale) Open() (port *serial.Port, err error) {
	c := &serial.Config{Name: scale.PortName, Baud: 9600}
	port, err = serial.OpenPort(c)
	return
}

func (scale Scale) Read(port *serial.Port) (value string, err error) {
	port.Write([]byte("IP\r\n"))
	scanner := bufio.NewScanner(port)
	scanner.Scan()
	value = scanner.Text()
	err = scanner.Err()
	return
}

func (scale Scale) TestReader(c chan Datum) {
	var d Datum
	d.Unit = "kg"
	for {
		d.Time = time.Now()
		d.Weight = rand.Float64()
		c <- d
		time.Sleep(2 * time.Second)
	}
}

func (scale Scale) Reader(c chan Datum) {
	f, err := os.OpenFile("backup-data.json", os.O_APPEND|os.O_WRONLY, 0600)
	if err != nil {
		panic(err)
	}

	defer f.Close()

	var d Datum
	for {
		port, err := scale.Open()

		if err != nil {
			log.Println(err)
			time.Sleep(2 * time.Second)
			continue
		}
		for {
			current_time := time.Now()
			v, err := scale.Read(port)
			if err != nil {
				port.Close()
				log.Println(err)
				break
			}
			value := strings.Split(strings.Trim(v, " "), " ")
			weight, err := strconv.ParseFloat(value[0], 64)
			if err != nil || len(value) < 2 {
				port.Close()
				log.Println(err)
				break
			}

			d.Time = current_time
			d.Weight = weight
			d.Unit = value[1]

			c <- d

			text, err := json.Marshal(d)
			if err != nil {
				log.Println(err)
				continue
			}
			if _, err = f.WriteString(string(text)); err != nil {
				log.Println(err)
				continue
			}

			time.Sleep(10 * time.Second)
		}

	}
}
<commit_msg>change to 60 sec interval<commit_after>package ohaus

import (
	"bufio"
	"encoding/json"
	serial "github.com/tarm/serial"
	"log"
	"math/rand"
	"os"
	"strconv"
	"strings"
	"time"
)

type Scale struct {
	PortName string
}

type Datum struct {
	Time   time.Time `json:"time"`
	Weight float64   `json:"weight"`
	Unit   string    `json:"unit"`
}

func (scale Scale) Open() (port *serial.Port, err error) {
	c := &serial.Config{Name: scale.PortName, Baud: 9600}
	port, err = serial.OpenPort(c)
	return
}

func (scale Scale) Read(port *serial.Port) (value string, err error) {
	port.Write([]byte("IP\r\n"))
	scanner := bufio.NewScanner(port)
	scanner.Scan()
	value = scanner.Text()
	err = scanner.Err()
	return
}

func (scale Scale) TestReader(c chan Datum) {
	var d Datum
	d.Unit = "kg"
	for {
		d.Time = time.Now()
		d.Weight = rand.Float64()
		c <- d
		time.Sleep(2 * time.Second)
	}
}

func (scale Scale) Reader(c chan Datum) {
	f, err := os.OpenFile("backup-data.json", os.O_APPEND|os.O_WRONLY, 0600)
	if err != nil {
		panic(err)
	}

	defer f.Close()

	var d Datum
	for {
		port, err := scale.Open()

		if err != nil {
			log.Println(err)
			time.Sleep(2 * time.Second)
			continue
		}
		for {
			current_time := time.Now()
			v, err := scale.Read(port)
			if err != nil {
				port.Close()
				log.Println(err)
				break
			}
			value := strings.Split(strings.Trim(v, " "), " ")
			weight, err := strconv.ParseFloat(value[0], 64)
			if err != nil || len(value) < 2 {
				port.Close()
				log.Println(err)
				break
			}

			d.Time = current_time
			d.Weight = weight
			d.Unit = value[1]

			c <- d

			text, err := json.Marshal(d)
			if err != nil {
				log.Println(err)
				continue
			}
			if _, err = f.WriteString(string(text)); err != nil {
				log.Println(err)
				continue
			}

			time.Sleep(60 * time.Second)
		}

	}
}
<|endoftext|>


<commit_before>package template

import (
	"fmt"
	"strings"

	"github.com/wallix/awless/template/ast"
)

func (te *Template) Revert() (*Template, error) {
	var lines []string

	for _, cmd := range te.CmdNodesReverseIterator() {
		if isRevertible(cmd) {
			var revertAction string
			var params []string

			switch cmd.Action {
			case "create":
				revertAction = "delete"
				switch cmd.Entity {
				case "tag":
					continue
				}
			case "start":
				revertAction = "stop"
			case "stop":
				revertAction = "start"
			case "detach":
				revertAction = "attach"
			case "attach":
				revertAction = "detach"
			}

			switch cmd.Action {
			case "start", "stop", "attach", "detach":
				for k, v := range cmd.Params {
					params = append(params, fmt.Sprintf("%s=%s", k, v))
				}
			case "create":
				params = append(params, fmt.Sprintf("id=%s", cmd.CmdResult))
			}

			lines = append(lines, fmt.Sprintf("%s %s %s", revertAction, cmd.Entity, strings.Join(params, " ")))

			if cmd.Action == "create" && cmd.Entity == "instance" {
				lines = append(lines, fmt.Sprintf("check instance id=%s state=terminated timeout=180", cmd.CmdResult))
			}
		}
	}

	text := strings.Join(lines, "\n")
	tpl, err := Parse(text)
	if err != nil {
		return nil, fmt.Errorf("revert: \n%s\n%s", text, err)
	}

	return tpl, nil
}

func IsRevertible(t *Template) bool {
	revertible := true
	t.visitCommandNodes(func(cmd *ast.CommandNode) {
		if !isRevertible(cmd) {
			revertible = false
		}
	})
	return revertible
}

func isRevertible(cmd *ast.CommandNode) bool {
	if cmd.CmdErr != nil {
		return false
	}

	if cmd.Action == "check" {
		return false
	}

	if v, ok := cmd.CmdResult.(string); ok && v != "" {
		if cmd.Action == "create" || cmd.Action == "start" || cmd.Action == "stop" {
			return true
		}
	}

	return cmd.Action == "attach" || cmd.Action == "detach" || cmd.Action == "check" || (cmd.Action == "create" && cmd.Entity == "tag")
}
<commit_msg>Templates are revertible if at least one operation is revertible<commit_after>package template

import (
	"fmt"
	"strings"

	"github.com/wallix/awless/template/ast"
)

func (te *Template) Revert() (*Template, error) {
	var lines []string

	for _, cmd := range te.CmdNodesReverseIterator() {
		if isRevertible(cmd) {
			var revertAction string
			var params []string

			switch cmd.Action {
			case "create":
				revertAction = "delete"
				switch cmd.Entity {
				case "tag":
					continue
				}
			case "start":
				revertAction = "stop"
			case "stop":
				revertAction = "start"
			case "detach":
				revertAction = "attach"
			case "attach":
				revertAction = "detach"
			}

			switch cmd.Action {
			case "start", "stop", "attach", "detach":
				for k, v := range cmd.Params {
					params = append(params, fmt.Sprintf("%s=%s", k, v))
				}
			case "create":
				params = append(params, fmt.Sprintf("id=%s", cmd.CmdResult))
			}

			lines = append(lines, fmt.Sprintf("%s %s %s", revertAction, cmd.Entity, strings.Join(params, " ")))

			if cmd.Action == "create" && cmd.Entity == "instance" {
				lines = append(lines, fmt.Sprintf("check instance id=%s state=terminated timeout=180", cmd.CmdResult))
			}
		}
	}

	text := strings.Join(lines, "\n")
	tpl, err := Parse(text)
	if err != nil {
		return nil, fmt.Errorf("revert: \n%s\n%s", text, err)
	}

	return tpl, nil
}

func IsRevertible(t *Template) bool {
	revertible := false
	t.visitCommandNodes(func(cmd *ast.CommandNode) {
		if isRevertible(cmd) {
			revertible = true
		}
	})
	return revertible
}

func isRevertible(cmd *ast.CommandNode) bool {
	if cmd.CmdErr != nil {
		return false
	}

	if cmd.Action == "check" {
		return false
	}

	if v, ok := cmd.CmdResult.(string); ok && v != "" {
		if cmd.Action == "create" || cmd.Action == "start" || cmd.Action == "stop" {
			return true
		}
	}

	return cmd.Action == "attach" || cmd.Action == "detach" || cmd.Action == "check" || (cmd.Action == "create" && cmd.Entity == "tag")
}
<|endoftext|>


<commit_before>package main

// #include <relic.h>
/*
void g1_free_w(ep_st* p){
  g1_free(p);
}
void g1_null_w(ep_st* p){
  g1_null(p);
}
void g1_new_w(ep_st* p){
  g1_new(p);
}
void g1_rand_w(ep_st* p){
  g1_rand(p);
}
int g1_cmp_w(ep_st* p, ep_st* q){
  return g1_cmp(p,q);
}
void g1_dbl_w(ep_st* r, ep_st* p){
  g1_dbl(r,p);
}
void g1_norm_w(ep_st* r, ep_st* p){
  g1_norm(r,p);
}
void g1_neg_w(ep_st* r, ep_st* p){
  g1_neg(r, p);
}
void g1_copy_w(ep_st* t, ep_st* f){
  g1_copy(t, f);
}
void g1_sub_w(ep_st* r, ep_st* p, ep_st* q){
	g1_sub(r,p,q);
}
void g1_add_w(ep_st* r, ep_st* p, ep_st* q){
  g1_add(r,p,q);
}
void g1_mul_w(ep_st* r, ep_st* p, bn_st* k){
  g1_mul(r,p,k);
}
void g1_read_bin_w(ep_st* r, uint8_t* bin, int len){
	g1_read_bin(r, bin, len);
}
int g1_size_bin_w(ep_st* p, int pack){
	return g1_size_bin(p, pack);
}
void g1_write_bin_w(uint8_t* rbin, int len, ep_st* p, int pack){
	g1_write_bin(rbin, len, p, pack);
}
*/
import "C"
import (
	"crypto/cipher"
	"encoding/hex"
	"errors"
	"io"

	"gopkg.in/dedis/kyber.v1"
)

type pointG1 struct {
	g1        C.ep_st
	generator string
}

func newPointG1() *pointG1 {
	pg1 := new(pointG1)
	C.g1_new_w(&pg1.g1)
	//runtime.SetFinalizer(&pg1.g1, clear)
	return pg1
}

func (p *pointG1) Equal(q kyber.Point) bool {
	pg := q.(*pointG1)
	i := C.g1_cmp_w(&p.g1, &pg.g1)
	switch i {
	case C.CMP_EQ:
		return true
	case C.CMP_NE:
		return false
	default:
		panic("Error in C casting")
	}
}

func (p *pointG1) Null() kyber.Point {
	C.g1_null_w(&p.g1)
	return p
}

//what is a standard base point? get any slice of bytes and put in a curve ep_map
func (p *pointG1) Base() kyber.Point {
	//get a base
	panic("not implemented")
}

func (p *pointG1) Add(p1, p2 kyber.Point) kyber.Point {
	pg1 := p1.(*pointG1)
	pg2 := p2.(*pointG1)
	C.g1_add_w(&p.g1, &pg1.g1, &pg2.g1)
	return p
}

func (p *pointG1) Sub(p1, p2 kyber.Point) kyber.Point {
	pg1 := p1.(*pointG1)
	pg2 := p2.(*pointG1)
	C.g1_sub_w(&p.g1, &pg1.g1, &pg2.g1)
	return p
}

func (p *pointG1) Neg(p1 kyber.Point) kyber.Point {
	pg1 := p1.(*pointG1)
	C.g1_neg_w(&p.g1, &pg1.g1)
	return p
}

func (p *pointG1) Mul(s kyber.Scalar, p1 kyber.Point) kyber.Point {
	//Waiting for a scalar implementation
	return p
}

func (p *pointG1) Clone() kyber.Point {
	p2 := new(pointG1)
	C.g1_new_w(&p2.g1)
	C.g1_copy_w(&p2.g1, &p.g1)
	// what is this? casting? how does it work?
	return p2
}

//is this just to return a byte representation of p?
func (p *pointG1) Data() ([]byte, error) {
	panic("not implemented")
}

//what is this?
func (p *pointG1) EmbedLen() int {
	panic("not implemented")
}

//what is this?
func (p *pointG1) Embed(data []byte, rand cipher.Stream) kyber.Point {
	panic("not implemented")
}

func (p *pointG1) Pick(rand cipher.Stream) kyber.Point {
	//TODO: rand is currently not being used because RELIC randomizes a point
	// by reference.
	C.g1_rand_w(&p.g1)
	return p
}

func (p *pointG1) PickR() kyber.Point {
	//TODO: rand is currently not being used because RELIC randomizes a point
	// by reference.
	C.g1_rand_w(&p.g1)
	return p
}

func (p *pointG1) Set(p2 kyber.Point) kyber.Point {
	pg2 := p2.(*pointG1)
	C.g1_copy_w(&p.g1, &pg2.g1)
	return p
}

func (p *pointG1) SetVarTime(varTime bool) error {
	return errors.New("ErrVarTime")
}

func (p *pointG1) String() string {
	buff, err := p.MarshalBinary()
	if err != nil {
		panic("Error in marshalling")
	}
	return hex.EncodeToString(buff)
}

// -- -- -- -- Marshalling

func (p *pointG1) MarshalBinary() ([]byte, error) {
	len := p.MarshalSize()
	//Get len from byte size
	//NOTE: I'm making the casting specific so you can have an idea of how to play
	// around with this value.
	clen := C.int(int32(len))
	//Point compression flag
	pack := C.int(0)
	//We obtain a C array allocated in the C heap using buff. CBytes returns an unsafe.Pointer
	buff := make([]byte, len, len)
	b := C.CBytes(buff)
	defer C.free(b)
	//We perform the write operation on the C array, which has to be casted to match the signature
	C.g1_write_bin_w((*C.uint8_t)(b), clen, &p.g1, pack)
	//We transform the unsafe.Pointer back to a []byte
	//GoBytes takes C data with explicity length and returns Go []byte
	copy(buff, C.GoBytes(b, clen))
	//TODO: Implement error-checking functionality
	return buff, nil
}

func (p *pointG1) MarshalTo(w io.Writer) (int, error) {
	return pointMarshalTo(p, w)
}

func (p *pointG1) UnmarshalBinary(buff []byte) error {
	//Get len from byte size
	//NOTE: I'm making the casting specific so you can have an idea of how to play
	// around with this value.
	len := C.int(int32(len(buff)))
	//Go []byte slice to C array -- returns an unsafe.Pointer
	//RELIC uses a uint8 array to represent a byte array. Therefore, we will take
	// a byte slice, convert it to a C array, cast it to have the right signature,
	// and pass a pointer to it.
	b := C.CBytes(buff)
	defer C.free(b)
	C.g1_read_bin_w(&p.g1, (*C.uint8_t)(b), len)
	//TODO: Implement error-checking functionality
	return nil
}

func (p *pointG1) UnmarshalFrom(r io.Reader) (int, error) {
	return pointUnmarshalFrom(p, r)
}

func (p *pointG1) MarshalSize() int {
	//Point compression flag
	pack := C.int(0)
	return int(C.g1_size_bin_w(&p.g1, pack))
}

// -- -- -- -- Helper functions

func pointMarshalTo(p kyber.Point, w io.Writer) (int, error) {
	buf, err := p.MarshalBinary()
	if err != nil {
		return 0, err
	}
	return w.Write(buf)
}

func pointUnmarshalFrom(p kyber.Point, r io.Reader) (int, error) {
	if strm, ok := r.(cipher.Stream); ok {
		p.Pick(strm)
		return -1, nil // no byte-count when picking randomly
	}
	buf := make([]byte, p.MarshalSize())
	n, err := io.ReadFull(r, buf)
	if err != nil {
		return n, err
	}
	return n, p.UnmarshalBinary(buf)
}
<commit_msg>renamed variables in g1<commit_after>package main

// #include <relic.h>
/*
void g1_free_w(ep_st* p){
  g1_free(p);
}
void g1_null_w(ep_st* p){
  g1_null(p);
}
void g1_new_w(ep_st* p){
  g1_new(p);
}
void g1_rand_w(ep_st* p){
  g1_rand(p);
}
int g1_cmp_w(ep_st* p, ep_st* q){
  return g1_cmp(p,q);
}
void g1_dbl_w(ep_st* r, ep_st* p){
  g1_dbl(r,p);
}
void g1_norm_w(ep_st* r, ep_st* p){
  g1_norm(r,p);
}
void g1_neg_w(ep_st* r, ep_st* p){
  g1_neg(r, p);
}
void g1_copy_w(ep_st* t, ep_st* f){
  g1_copy(t, f);
}
void g1_sub_w(ep_st* r, ep_st* p, ep_st* q){
	g1_sub(r,p,q);
}
void g1_add_w(ep_st* r, ep_st* p, ep_st* q){
  g1_add(r,p,q);
}
void g1_mul_w(ep_st* r, ep_st* p, bn_st* k){
  g1_mul(r,p,k);
}
void g1_read_bin_w(ep_st* r, uint8_t* bin, int len){
	g1_read_bin(r, bin, len);
}
int g1_size_bin_w(ep_st* p, int pack){
	return g1_size_bin(p, pack);
}
void g1_write_bin_w(uint8_t* rbin, int len, ep_st* p, int pack){
	g1_write_bin(rbin, len, p, pack);
}
*/
import "C"
import (
	"crypto/cipher"
	"encoding/hex"
	"errors"
	"io"

	"gopkg.in/dedis/kyber.v1"
)

type pointG1 struct {
	g         C.ep_st
	generator string
}

func newPointG1() *pointG1 {
	pg := new(pointG1)
	C.g1_new_w(&pg.g)
	//runtime.SetFinalizer(&pg.g, clear)
	return pg
}

func (p *pointG1) Equal(q kyber.Point) bool {
	pg := q.(*pointG1)
	i := C.g1_cmp_w(&p.g, &pg.g)
	switch i {
	case C.CMP_EQ:
		return true
	case C.CMP_NE:
		return false
	default:
		panic("Error in C casting")
	}
}

func (p *pointG1) Null() kyber.Point {
	C.g1_null_w(&p.g)
	return p
}

//what is a standard base point? get any slice of bytes and put in a curve ep_map
func (p *pointG1) Base() kyber.Point {
	//get a base
	panic("not implemented")
}

func (p *pointG1) Add(p1, p2 kyber.Point) kyber.Point {
	pg1 := p1.(*pointG1)
	pg2 := p2.(*pointG1)
	C.g1_add_w(&p.g, &pg1.g, &pg2.g)
	return p
}

func (p *pointG1) Sub(p1, p2 kyber.Point) kyber.Point {
	pg1 := p1.(*pointG1)
	pg2 := p2.(*pointG1)
	C.g1_sub_w(&p.g, &pg1.g, &pg2.g)
	return p
}

func (p *pointG1) Neg(p1 kyber.Point) kyber.Point {
	pg := p1.(*pointG1)
	C.g1_neg_w(&p.g, &pg.g)
	return p
}

func (p *pointG1) Mul(s kyber.Scalar, p1 kyber.Point) kyber.Point {
	//Waiting for a scalar implementation
	return p
}

func (p *pointG1) Clone() kyber.Point {
	p2 := new(pointG1)
	C.g1_new_w(&p2.g)
	C.g1_copy_w(&p2.g, &p.g)
	// what is this? casting? how does it work?
	return p2
}

//is this just to return a byte representation of p?
func (p *pointG1) Data() ([]byte, error) {
	panic("not implemented")
}

//what is this?
func (p *pointG1) EmbedLen() int {
	panic("not implemented")
}

//what is this?
func (p *pointG1) Embed(data []byte, rand cipher.Stream) kyber.Point {
	panic("not implemented")
}

func (p *pointG1) Pick(rand cipher.Stream) kyber.Point {
	//TODO: rand is currently not being used because RELIC randomizes a point
	// by reference.
	C.g1_rand_w(&p.g)
	return p
}

func (p *pointG1) PickR() kyber.Point {
	//TODO: rand is currently not being used because RELIC randomizes a point
	// by reference.
	C.g1_rand_w(&p.g)
	return p
}

func (p *pointG1) Set(p2 kyber.Point) kyber.Point {
	pg2 := p2.(*pointG1)
	C.g1_copy_w(&p.g, &pg2.g)
	return p
}

func (p *pointG1) SetVarTime(varTime bool) error {
	return errors.New("ErrVarTime")
}

func (p *pointG1) String() string {
	buff, err := p.MarshalBinary()
	if err != nil {
		panic("Error in marshalling")
	}
	return hex.EncodeToString(buff)
}

// -- -- -- -- Marshalling

func (p *pointG1) MarshalBinary() ([]byte, error) {
	len := p.MarshalSize()
	//Get len from byte size
	//NOTE: I'm making the casting specific so you can have an idea of how to play
	// around with this value.
	clen := C.int(int32(len))
	//Point compression flag
	pack := C.int(0)
	//We obtain a C array allocated in the C heap using buff. CBytes returns an unsafe.Pointer
	buff := make([]byte, len, len)
	b := C.CBytes(buff)
	defer C.free(b)
	//We perform the write operation on the C array, which has to be casted to match the signature
	C.g1_write_bin_w((*C.uint8_t)(b), clen, &p.g, pack)
	//We transform the unsafe.Pointer back to a []byte
	//GoBytes takes C data with explicity length and returns Go []byte
	copy(buff, C.GoBytes(b, clen))
	//TODO: Implement error-checking functionality
	return buff, nil
}

func (p *pointG1) MarshalTo(w io.Writer) (int, error) {
	return pointMarshalTo(p, w)
}

func (p *pointG1) UnmarshalBinary(buff []byte) error {
	//Get len from byte size
	//NOTE: I'm making the casting specific so you can have an idea of how to play
	// around with this value.
	len := C.int(int32(len(buff)))
	//Go []byte slice to C array -- returns an unsafe.Pointer
	//RELIC uses a uint8 array to represent a byte array. Therefore, we will take
	// a byte slice, convert it to a C array, cast it to have the right signature,
	// and pass a pointer to it.
	b := C.CBytes(buff)
	defer C.free(b)
	C.g1_read_bin_w(&p.g, (*C.uint8_t)(b), len)
	//TODO: Implement error-checking functionality
	return nil
}

func (p *pointG1) UnmarshalFrom(r io.Reader) (int, error) {
	return pointUnmarshalFrom(p, r)
}

func (p *pointG1) MarshalSize() int {
	//Point compression flag
	pack := C.int(0)
	return int(C.g1_size_bin_w(&p.g, pack))
}

// -- -- -- -- Helper functions

func pointMarshalTo(p kyber.Point, w io.Writer) (int, error) {
	buf, err := p.MarshalBinary()
	if err != nil {
		return 0, err
	}
	return w.Write(buf)
}

func pointUnmarshalFrom(p kyber.Point, r io.Reader) (int, error) {
	if strm, ok := r.(cipher.Stream); ok {
		p.Pick(strm)
		return -1, nil // no byte-count when picking randomly
	}
	buf := make([]byte, p.MarshalSize())
	n, err := io.ReadFull(r, buf)
	if err != nil {
		return n, err
	}
	return n, p.UnmarshalBinary(buf)
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"os"
	"time"

	git "github.com/libgit2/git2go"
)

type ColorType string

const (
	Red    ColorType = "\x1b[0;31m"
	Yellow           = "\x1b[0;33m"
	Green            = "\x1b[0;32m"

	BaseBranch string = "master"
)

func NewRepo() *git.Repository {
	repo, err := git.OpenRepository(".")
	if err != nil {
		// @todo improve message
		fmt.Printf("Could not open repository at '.'\n")
		os.Exit(1)
	}
	return repo
}

func NewBranchIterator(repo *git.Repository) *git.BranchIterator {
	i, err := repo.NewBranchIterator(git.BranchLocal)
	if err != nil {
		// @todo improve message
		fmt.Printf("Can't list branches\n")
		os.Exit(1)
	}
	return i
}

func LookupBaseOid(repo *git.Repository) *git.Oid {
	base_branch, err := repo.LookupBranch(BaseBranch, git.BranchLocal)
	if err != nil {
		fmt.Printf("Error looking up %s\n", BaseBranch)
		os.Exit(1)
	}

	return base_branch.Target()
}

type Comparison struct {
	Repo    *git.Repository
	BaseOid *git.Oid
	Branch  *git.Branch
	Oid     *git.Oid

	ahead  int
	behind int
}

func NewComparison(repo *git.Repository, base_oid *git.Oid, branch *git.Branch) *Comparison {
	c := new(Comparison)

	c.Repo = repo
	c.BaseOid = base_oid

	c.Branch = branch
	c.Oid = branch.Target()

	c.ahead = -1
	c.behind = -1

	return c
}

func (c Comparison) Name() string {
	name, err := c.Branch.Name()
	if err != nil {
		fmt.Printf("Can't get branch name\n")
		os.Exit(1)
	}
	return name
}

func (c Comparison) IsHead() bool {
	head, err := c.Branch.IsHead()
	if err != nil {
		fmt.Printf("Can't get IsHead\n")
		os.Exit(1)
	}
	return head
}

func (c Comparison) IsMerged() bool {
	if c.Oid.String() == c.BaseOid.String() {
		return true
	} else {
		merged, err := c.Repo.DescendantOf(c.BaseOid, c.Oid)
		if err != nil {
			fmt.Printf("Could not get descendant of '%s' and '%s'.\n", c.BaseOid, c.Oid)
			os.Exit(1)
		}
		return merged
	}
}

func (c Comparison) Commit() *git.Commit {
	commit, err := c.Repo.LookupCommit(c.Oid)
	if err != nil {
		fmt.Printf("Could not lookup commit '%s'.\n", c.Oid)
		os.Exit(1)
	}
	return commit
}

// @todo red for old commits
func (c Comparison) Color() ColorType {
	if c.IsHead() {
		return Green
	} else {
		return Yellow
	}
}

func (c Comparison) When() time.Time {
	sig := c.Commit().Committer()
	return sig.When
}

func (c *Comparison) Ahead() int {
	c.ComputeAheadBehind()
	return c.ahead
}

func (c *Comparison) Behind() int {
	c.ComputeAheadBehind()
	return c.behind
}

func (c *Comparison) ComputeAheadBehind() {
	if c.ahead > -1 && c.behind > -1 {
		return
	}

	var err error
	c.ahead, c.behind, err = c.Repo.AheadBehind(c.Oid, c.BaseOid)
	if err != nil {
		fmt.Printf("Error getting ahead/behind\n", c.BaseOid)
		os.Exit(1)
	}
}

func main() {
	repo := NewRepo()
	branch_iterator := NewBranchIterator(repo)
	base_oid := LookupBaseOid(repo)

	// type BranchIteratorFunc func(*Branch, BranchType) error
	branch_iterator.ForEach(func(branch *git.Branch, btype git.BranchType) error {
		comp := NewComparison(repo, base_oid, branch)

		merged_string := ""
		if comp.IsMerged() {
			merged_string = "(merged)"
		}

		fmt.Printf(
			"%s%s | %-30s           | behind: %4d | ahead: %4d %s\n",
			comp.Color(),
			comp.When().Format("2006-01-02 15:04"),
			comp.Name(),
			comp.Behind(),
			comp.Ahead(),
			merged_string)

		return nil
	})

	// @todo store all comparisons in a list that can be sorted before printing.
	// @todo filter them things
}
<commit_msg>Use pointer receiver everywhere. Use helper exit function.<commit_after>package main

import (
	"fmt"
	"os"
	"time"

	git "github.com/libgit2/git2go"
)

type ColorType string

const (
	Red    ColorType = "\x1b[0;31m"
	Yellow           = "\x1b[0;33m"
	Green            = "\x1b[0;32m"

	BaseBranch string = "master"
)

func exit(msg string, args ...string) {
	fmt.Printf(msg, args)
	os.Exit(1)
}

func NewRepo() *git.Repository {
	repo, err := git.OpenRepository(".")
	if err != nil {
		// @todo improve message
		exit("Could not open repository at '.'\n")
	}
	return repo
}

func NewBranchIterator(repo *git.Repository) *git.BranchIterator {
	i, err := repo.NewBranchIterator(git.BranchLocal)
	if err != nil {
		// @todo improve message
		exit("Can't list branches\n")
	}
	return i
}

func LookupBaseOid(repo *git.Repository) *git.Oid {
	base_branch, err := repo.LookupBranch(BaseBranch, git.BranchLocal)
	if err != nil {
		exit("Error looking up %s\n", BaseBranch)
	}

	return base_branch.Target()
}

type Comparison struct {
	Repo    *git.Repository
	BaseOid *git.Oid
	Branch  *git.Branch
	Oid     *git.Oid

	ahead  int
	behind int
}

func NewComparison(repo *git.Repository, base_oid *git.Oid, branch *git.Branch) *Comparison {
	c := new(Comparison)

	c.Repo = repo
	c.BaseOid = base_oid

	c.Branch = branch
	c.Oid = branch.Target()

	c.ahead = -1
	c.behind = -1

	return c
}

func (c *Comparison) Name() string {
	name, err := c.Branch.Name()
	if err != nil {
		exit("Can't get branch name\n")
	}
	return name
}

func (c *Comparison) IsHead() bool {
	head, err := c.Branch.IsHead()
	if err != nil {
		exit("Can't get IsHead\n")
	}
	return head
}

func (c *Comparison) IsMerged() bool {
	if c.Oid.String() == c.BaseOid.String() {
		return true
	} else {
		merged, err := c.Repo.DescendantOf(c.BaseOid, c.Oid)
		if err != nil {
			exit("Could not get descendant of '%s' and '%s'.\n", c.BaseOid.String(), c.Oid.String())
		}
		return merged
	}
}

func (c *Comparison) Commit() *git.Commit {
	commit, err := c.Repo.LookupCommit(c.Oid)
	if err != nil {
		exit("Could not lookup commit '%s'.\n", c.Oid.String())
	}
	return commit
}

// @todo red for old commits
func (c *Comparison) Color() ColorType {
	if c.IsHead() {
		return Green
	} else {
		return Yellow
	}
}

func (c *Comparison) When() time.Time {
	sig := c.Commit().Committer()
	return sig.When
}

func (c *Comparison) Ahead() int {
	c.ComputeAheadBehind()
	return c.ahead
}

func (c *Comparison) Behind() int {
	c.ComputeAheadBehind()
	return c.behind
}

func (c *Comparison) ComputeAheadBehind() {
	if c.ahead > -1 && c.behind > -1 {
		return
	}

	var err error
	c.ahead, c.behind, err = c.Repo.AheadBehind(c.Oid, c.BaseOid)
	if err != nil {
		exit("Error getting ahead/behind\n", c.BaseOid.String())
	}
}

func main() {
	repo := NewRepo()
	branch_iterator := NewBranchIterator(repo)
	base_oid := LookupBaseOid(repo)

	// type BranchIteratorFunc func(*Branch, BranchType) error
	branch_iterator.ForEach(func(branch *git.Branch, btype git.BranchType) error {
		comp := NewComparison(repo, base_oid, branch)

		merged_string := ""
		if comp.IsMerged() {
			merged_string = "(merged)"
		}

		fmt.Printf(
			"%s%s | %-30s           | behind: %4d | ahead: %4d %s\n",
			comp.Color(),
			comp.When().Format("2006-01-02 15:04"),
			comp.Name(),
			comp.Behind(),
			comp.Ahead(),
			merged_string)

		return nil
	})

	// @todo store all comparisons in a list that can be sorted before printing.
	// @todo filter them things
}
<|endoftext|>


<commit_before>// Package client is a simple library for http.Client to sign Akamai OPEN Edgegrid API requests
package client

import (
	"bytes"
	"errors"
	"io"
	"io/ioutil"
	"net/http"
	"net/url"
	"runtime"
	"strings"

	"github.com/akamai/AkamaiOPEN-edgegrid-golang/edgegrid"
	"github.com/akamai/AkamaiOPEN-edgegrid-golang/jsonhooks-v1"
)

var (
	libraryVersion = "0.3.0"
	// UserAgent is the User-Agent value sent for all requests
	UserAgent = "Akamai-Open-Edgegrid-golang/" + libraryVersion + " golang/" + strings.TrimPrefix(runtime.Version(), "go")
	// Client is the *http.Client to use
	Client = http.DefaultClient
)

// NewRequest creates an HTTP request that can be sent to Akamai APIs. A relative URL can be provided in path, which will be resolved to the
// Host specified in Config. If body is specified, it will be sent as the request body.
func NewRequest(config edgegrid.Config, method, path string, body io.Reader) (*http.Request, error) {
	baseURL, err := url.Parse("https://" + config.Host)
	if err != nil {
		return nil, err
	}

	rel, err := url.Parse(strings.TrimPrefix(path, "/"))
	if err != nil {
		return nil, err
	}

	u := baseURL.ResolveReference(rel)

	req, err := http.NewRequest(method, u.String(), body)
	if err != nil {
		return nil, err
	}

	req.Header.Add("User-Agent", UserAgent)

	return req, nil
}

// NewJSONRequest creates an HTTP request that can be sent to the Akamai APIs with a JSON body
// The JSON body is encoded and the Content-Type/Accept headers are set automatically.
func NewJSONRequest(config edgegrid.Config, method, path string, body interface{}) (*http.Request, error) {
	jsonBody, err := jsonhooks.Marshal(body)
	if err != nil {
		return nil, err
	}

	buf := bytes.NewReader(jsonBody)
	req, err := NewRequest(config, method, path, buf)
	if err != nil {
		return nil, err
	}

	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Accept", "application/json,*/*")

	return req, nil
}

// Do performs a given HTTP Request, signed with the Akamai OPEN Edgegrid
// Authorization header. An edgegrid.Response or an error is returned.
func Do(config edgegrid.Config, req *http.Request) (*http.Response, error) {
	req = edgegrid.AddRequestHeader(config, req)
	res, err := Client.Do(req)
	if err != nil {
		return nil, err
	}

	return res, nil
}

// BodyJSON unmarshals the Response.Body into a given data structure
func BodyJSON(r *http.Response, data interface{}) error {
	if data == nil {
		return errors.New("You must pass in an interface{}")
	}

	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return err
	}
	err = jsonhooks.Unmarshal(body, data)

	return err
}
<commit_msg>Bump client version to 0.4.0<commit_after>// Package client is a simple library for http.Client to sign Akamai OPEN Edgegrid API requests
package client

import (
	"bytes"
	"errors"
	"io"
	"io/ioutil"
	"net/http"
	"net/url"
	"runtime"
	"strings"

	"github.com/akamai/AkamaiOPEN-edgegrid-golang/edgegrid"
	"github.com/akamai/AkamaiOPEN-edgegrid-golang/jsonhooks-v1"
)

var (
	libraryVersion = "0.4.0"
	// UserAgent is the User-Agent value sent for all requests
	UserAgent = "Akamai-Open-Edgegrid-golang/" + libraryVersion + " golang/" + strings.TrimPrefix(runtime.Version(), "go")
	// Client is the *http.Client to use
	Client = http.DefaultClient
)

// NewRequest creates an HTTP request that can be sent to Akamai APIs. A relative URL can be provided in path, which will be resolved to the
// Host specified in Config. If body is specified, it will be sent as the request body.
func NewRequest(config edgegrid.Config, method, path string, body io.Reader) (*http.Request, error) {
	baseURL, err := url.Parse("https://" + config.Host)
	if err != nil {
		return nil, err
	}

	rel, err := url.Parse(strings.TrimPrefix(path, "/"))
	if err != nil {
		return nil, err
	}

	u := baseURL.ResolveReference(rel)

	req, err := http.NewRequest(method, u.String(), body)
	if err != nil {
		return nil, err
	}

	req.Header.Add("User-Agent", UserAgent)

	return req, nil
}

// NewJSONRequest creates an HTTP request that can be sent to the Akamai APIs with a JSON body
// The JSON body is encoded and the Content-Type/Accept headers are set automatically.
func NewJSONRequest(config edgegrid.Config, method, path string, body interface{}) (*http.Request, error) {
	jsonBody, err := jsonhooks.Marshal(body)
	if err != nil {
		return nil, err
	}

	buf := bytes.NewReader(jsonBody)
	req, err := NewRequest(config, method, path, buf)
	if err != nil {
		return nil, err
	}

	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Accept", "application/json,*/*")

	return req, nil
}

// Do performs a given HTTP Request, signed with the Akamai OPEN Edgegrid
// Authorization header. An edgegrid.Response or an error is returned.
func Do(config edgegrid.Config, req *http.Request) (*http.Response, error) {
	req = edgegrid.AddRequestHeader(config, req)
	res, err := Client.Do(req)
	if err != nil {
		return nil, err
	}

	return res, nil
}

// BodyJSON unmarshals the Response.Body into a given data structure
func BodyJSON(r *http.Response, data interface{}) error {
	if data == nil {
		return errors.New("You must pass in an interface{}")
	}

	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return err
	}
	err = jsonhooks.Unmarshal(body, data)

	return err
}
<|endoftext|>


<commit_before>package matutil

import (

//	"fmt"
	"testing"
	"math"
	"code.google.com/p/gomatrix/matrix"
)

func TestColSliceValid(t *testing.T) {
	rows := 3
	columns := 2
	mat := matrix.MakeDenseMatrix([]float64{1, 2, 3, 4, 5, 6}, rows, columns)
	c := ColSlice(mat, 1)
	if len(c) != rows {
		t.Errorf("Returned slice has len=%d instead of %d.", len(c), rows)
	}
}

func TestAppendColInvalid(t *testing.T) {
	rows := 3
	columns := 2
	mat := matrix.MakeDenseMatrix([]float64{1, 2, 3, 4, 5, 6}, rows, columns)
	col := []float64{1.1, 2.2, 3.3, 4.4}
	mat, err := AppendCol(mat, col)
	if err == nil {
		t.Errorf("AppendCol err=%v", err)
	}
}

func TestAppendColValid(t *testing.T) {
	rows := 3
	columns := 2
	mat := matrix.MakeDenseMatrix([]float64{1, 2, 3, 4, 5, 6}, rows, columns)
	col := []float64{1.1, 2.2, 3.3}
	mat, err := AppendCol(mat, col)
	if err != nil {
		t.Errorf("AppendCol err=%v", err)
	}
}

func TestPow(t *testing.T) {
	p00 := float64(3)
	p01 := float64(4)
	mat := matrix.MakeDenseMatrix([]float64{p00, p01}, 1, 2)
	raised := Pow(mat, 2)

	r00 := raised.Get(0, 0)
	if r00 != 9 {
		t.Errorf("TestPow r00 should be 9, but is %f", r00)
	}
	r01 := raised.Get(0, 1)
	if r01 != 16 {
		t.Errorf("TestPow r01 should be 16, but is %f", r01)
	}
}

func TestSumRows(t *testing.T) {
	p00 := 3.0
	p01 := 4.0
	p10 := 3.5
	p11 := 4.6
	mat := matrix.MakeDenseMatrix([]float64{p00, p01, p10, p11}, 2, 2)
	sums := SumRows(mat)

	numRows, numCols := sums.GetSize()
	if numRows != 2 || numCols != 1 {
		t.Errorf("SumRows returned a %dx%d matrix.  It should be 2x1.", numRows, numCols)
	}
	s00 := sums.Get(0, 0)
	if s00 != (p00 + p01) {
		t.Errorf("SumRows row 0 col 0 is %d.  It should be %d.", s00, p00+p01)
	}
	s10 := sums.Get(1, 0)
	if s10 != (p10 + p11) {
		t.Errorf("SumRows row 1 col 2 is %d.  It should be %d.", s10, p10+p11)
	}
}

func TestSumCols(t *testing.T) {
	p00 := 3.0
	p01 := 4.0
	p10 := 3.5
	p11 := 4.6
	mat := matrix.MakeDenseMatrix([]float64{p00, p01, p10, p11}, 2, 2)
	sums := SumCols(mat)

	numRows, numCols := sums.GetSize()
	if numRows != 1 || numCols != 2 {
		t.Errorf("SumCols returned a %dx%d matrix.  It should be 1x2.", numRows, numCols)
	}
	s00 := sums.Get(0, 0)
	if s00 != (p00 + p10) {
		t.Errorf("SumCols row 0 col 0 is %d.  It should be %d.", s00, p00+p10)
	}
	s10 := sums.Get(0, 1)
	if s10 != (p01 + p11) {
		t.Errorf("SumCols row 0 col 1 is %d.  It should be %d.", s10, p01+p11)
	}
}

func TestFiltCol(t *testing.T) {
	mat := matrix.MakeDenseMatrix([]float64{2, 1, 4, 2, 6, 3,8, 4, 10, 5, 1, 1}, 5, 2)
	matches, err := FiltCol(mat, 2.0, 4.0, 1)
	if err != nil {
		t.Errorf("FiltCol returned error: %v", err)
		return
	}

	r, _ := matches.GetSize()
	if r != 3 {
		t.Errorf("FiltCol: expected 3 rows and got %d", r)
	}

	m0 := matches.Get(0,1)
	if m0 != 2 {
		t.Errorf("FiltCol: expected row 0 col 1 to be 2, but got %f",m0)
	}

	m1 := matches.Get(1, 1)
	if m1 != 3 {
		t.Errorf("FiltCol: expected row 1 col 1 to be 3, but got %f",m1)
	}

	m2 := matches.Get(2, 1)
	if m2 != 4 {
		t.Errorf("FiltCol: expected row 1 col 1 to be 3, but got %f",m2)
	}
}

func TestFiltColMap(t *testing.T) {
	mat := matrix.MakeDenseMatrix([]float64{2, 1, 4, 2, 6, 3,8, 4, 10, 5, 1, 1}, 5, 2)
	matches, err := FiltColMap(mat, 2.0, 4.0, 1)
	if err != nil {
		t.Errorf("FiltColMap returned error: %v", err)
		return
	}

	if len(matches) != 3 {
		t.Errorf("FiltColMap expecte a map of len 3, but got len %d", len(matches))
	}

	if matches[1] != 2 || matches[2] != 3 || matches[3] != 4 {
		t.Errorf("FiltColMap expected a map with vals 2, 3, 4 but got %v", matches)
	}
}

func TestEuclidDist(t *testing.T) {
	var ed EuclidDist
	rows := 1
	columns := 2

	centroid := matrix.MakeDenseMatrix([]float64{4.6, 9.5}, rows, columns)
	point := matrix.MakeDenseMatrix([]float64{3.0, 4.1}, rows, columns)
	calcEd, err := ed.CalcDist(centroid, point)
	if err != nil {
		t.Errorf("EuclidDist: returned an error.  err=%v", err)
	}

	expectedEd := 5.632051 //expected value
	epsilon := .000001

	na := math.Nextafter(expectedEd, expectedEd + 1)
	diff := math.Abs(calcEd - na)

	if diff > epsilon {
		t.Errorf("EuclidDist: excpected %f but received %f.  The difference %f exceeds epsilon %f", expectedEd, calcEd, diff, epsilon)
	}
}

func BenchmarkEuclidDist(b *testing.B) {
	var ed EuclidDist
	rows := 1
	columns := 2

	centroid := matrix.MakeDenseMatrix([]float64{4.6, 9.5}, rows, columns)
	point := matrix.MakeDenseMatrix([]float64{3.0, 4.1}, rows, columns)
    for i := 0; i < b.N; i++ {
		_, _ = ed.CalcDist(centroid, point)
    }
}

func TestManhattanDist(t *testing.T) {
	var md ManhattanDist
	rows := 1
	columns := 2

	a := matrix.MakeDenseMatrix([]float64{4.6, 9.5}, rows, columns)
	b := matrix.MakeDenseMatrix([]float64{3.0, 4.1}, rows, columns)

	calcMd, err := md.CalcDist(a, b)
	if err != nil {
		t.Errorf("ManhattandDist: returned an error.  err=%v", err)
	}

	// 1.6 + 5.4 = 7.0
	if calcMd != float64(7.0) {
		t.Errorf("ManhattanDist: should be 7.0, but returned %f", calcMd)
	}
}
//TODO: test for MeanCols<commit_msg>Corrected test for FiltCol()<commit_after>package matutil

import (

//	"fmt"
	"testing"
	"math"
	"code.google.com/p/gomatrix/matrix"
)

func TestColSliceValid(t *testing.T) {
	rows := 3
	columns := 2
	mat := matrix.MakeDenseMatrix([]float64{1, 2, 3, 4, 5, 6}, rows, columns)
	c := ColSlice(mat, 1)
	if len(c) != rows {
		t.Errorf("Returned slice has len=%d instead of %d.", len(c), rows)
	}
}

func TestAppendColInvalid(t *testing.T) {
	rows := 3
	columns := 2
	mat := matrix.MakeDenseMatrix([]float64{1, 2, 3, 4, 5, 6}, rows, columns)
	col := []float64{1.1, 2.2, 3.3, 4.4}
	mat, err := AppendCol(mat, col)
	if err == nil {
		t.Errorf("AppendCol err=%v", err)
	}
}

func TestAppendColValid(t *testing.T) {
	rows := 3
	columns := 2
	mat := matrix.MakeDenseMatrix([]float64{1, 2, 3, 4, 5, 6}, rows, columns)
	col := []float64{1.1, 2.2, 3.3}
	mat, err := AppendCol(mat, col)
	if err != nil {
		t.Errorf("AppendCol err=%v", err)
	}
}

func TestPow(t *testing.T) {
	p00 := float64(3)
	p01 := float64(4)
	mat := matrix.MakeDenseMatrix([]float64{p00, p01}, 1, 2)
	raised := Pow(mat, 2)

	r00 := raised.Get(0, 0)
	if r00 != 9 {
		t.Errorf("TestPow r00 should be 9, but is %f", r00)
	}
	r01 := raised.Get(0, 1)
	if r01 != 16 {
		t.Errorf("TestPow r01 should be 16, but is %f", r01)
	}
}

func TestSumRows(t *testing.T) {
	p00 := 3.0
	p01 := 4.0
	p10 := 3.5
	p11 := 4.6
	mat := matrix.MakeDenseMatrix([]float64{p00, p01, p10, p11}, 2, 2)
	sums := SumRows(mat)

	numRows, numCols := sums.GetSize()
	if numRows != 2 || numCols != 1 {
		t.Errorf("SumRows returned a %dx%d matrix.  It should be 2x1.", numRows, numCols)
	}
	s00 := sums.Get(0, 0)
	if s00 != (p00 + p01) {
		t.Errorf("SumRows row 0 col 0 is %d.  It should be %d.", s00, p00+p01)
	}
	s10 := sums.Get(1, 0)
	if s10 != (p10 + p11) {
		t.Errorf("SumRows row 1 col 2 is %d.  It should be %d.", s10, p10+p11)
	}
}

func TestSumCols(t *testing.T) {
	p00 := 3.0
	p01 := 4.0
	p10 := 3.5
	p11 := 4.6
	mat := matrix.MakeDenseMatrix([]float64{p00, p01, p10, p11}, 2, 2)
	sums := SumCols(mat)

	numRows, numCols := sums.GetSize()
	if numRows != 1 || numCols != 2 {
		t.Errorf("SumCols returned a %dx%d matrix.  It should be 1x2.", numRows, numCols)
	}
	s00 := sums.Get(0, 0)
	if s00 != (p00 + p10) {
		t.Errorf("SumCols row 0 col 0 is %d.  It should be %d.", s00, p00+p10)
	}
	s10 := sums.Get(0, 1)
	if s10 != (p01 + p11) {
		t.Errorf("SumCols row 0 col 1 is %d.  It should be %d.", s10, p01+p11)
	}
}

func TestFiltCol(t *testing.T) {
	mat := matrix.MakeDenseMatrix([]float64{2, 1, 4, 2, 6, 3, 8, 4, 10, 5, 1, 1}, 5, 2)
	// mat, max, min, column
	matches, err := FiltCol(mat, 2.0, 4.0, 1)
	if err != nil {
		t.Errorf("FiltCol returned error: %v", err)
		return
	}

	r, _ := matches.GetSize()
	if r != 3 {
		t.Errorf("FiltCol: expected 3 rows and got %d", r)
	}

	m0 := matches.Get(1,1)
	if m0 != 2 {
		t.Errorf("FiltCol: expected row 0 col 1 to be 2, but got %f",m0)
	}

	m1 := matches.Get(2, 1)
	if m1 != 3 {
		t.Errorf("FiltCol: expected row 1 col 1 to be 3, but got %f",m1)
	}

	m2 := matches.Get(3, 1)
	if m2 != 4 {
		t.Errorf("FiltCol: expected row 1 col 1 to be 3, but got %f",m2)
	}
}

func TestFiltColMap(t *testing.T) {
	mat := matrix.MakeDenseMatrix([]float64{2, 1, 4, 2, 6, 3,8, 4, 10, 5, 1, 1}, 5, 2)
	matches, err := FiltColMap(mat, 2.0, 4.0, 1)
	if err != nil {
		t.Errorf("FiltColMap returned error: %v", err)
		return
	}

	if len(matches) != 3 {
		t.Errorf("FiltColMap expecte a map of len 3, but got len %d", len(matches))
	}

	if matches[1] != 2 || matches[2] != 3 || matches[3] != 4 {
		t.Errorf("FiltColMap expected a map with vals 2, 3, 4 but got %v", matches)
	}
}

func TestEuclidDist(t *testing.T) {
	var ed EuclidDist
	rows := 1
	columns := 2

	centroid := matrix.MakeDenseMatrix([]float64{4.6, 9.5}, rows, columns)
	point := matrix.MakeDenseMatrix([]float64{3.0, 4.1}, rows, columns)
	calcEd, err := ed.CalcDist(centroid, point)
	if err != nil {
		t.Errorf("EuclidDist: returned an error.  err=%v", err)
	}

	expectedEd := 5.632051 //expected value
	epsilon := .000001

	na := math.Nextafter(expectedEd, expectedEd + 1)
	diff := math.Abs(calcEd - na)

	if diff > epsilon {
		t.Errorf("EuclidDist: excpected %f but received %f.  The difference %f exceeds epsilon %f", expectedEd, calcEd, diff, epsilon)
	}
}

func BenchmarkEuclidDist(b *testing.B) {
	var ed EuclidDist
	rows := 1
	columns := 2

	centroid := matrix.MakeDenseMatrix([]float64{4.6, 9.5}, rows, columns)
	point := matrix.MakeDenseMatrix([]float64{3.0, 4.1}, rows, columns)
    for i := 0; i < b.N; i++ {
		_, _ = ed.CalcDist(centroid, point)
    }
}

func TestManhattanDist(t *testing.T) {
	var md ManhattanDist
	rows := 1
	columns := 2

	a := matrix.MakeDenseMatrix([]float64{4.6, 9.5}, rows, columns)
	b := matrix.MakeDenseMatrix([]float64{3.0, 4.1}, rows, columns)

	calcMd, err := md.CalcDist(a, b)
	if err != nil {
		t.Errorf("ManhattandDist: returned an error.  err=%v", err)
	}

	// 1.6 + 5.4 = 7.0
	if calcMd != float64(7.0) {
		t.Errorf("ManhattanDist: should be 7.0, but returned %f", calcMd)
	}
}
//TODO: test for MeanCols<|endoftext|>


<commit_before>// Copyright 2014 Thomas Keschl. All rights reserved.  Use of this
// source code is governed by a MIT-style license that can be found in
// the LICENSE file.

package main

import(
	"io/ioutil"
	"crypto/md5"
	"crypto/sha1"
	"fmt"
	"flag"
	"hash"
)

func main() {
	inputFilePtr := flag.String("f", "", "path to the file to generate the hash for")
	inputHashPtr := flag.String("c", "", "the hash string to compare against")
	inputTypePtr := flag.String("t", "md5", "the hashing function to use (valid options are md5, sha1)")
	flag.Parse()

	// ensure a file has been entered.
	if len(*inputFilePtr) == 0 {
		fmt.Printf("Need to enter a file to hash.")
		return
	}

	filepath := *inputFilePtr

	f,err := ioutil.ReadFile(filepath)
	if err != nil {
		fmt.Printf("%s", err)
		return
	}

	hashStr := *inputTypePtr

	hash := generateSum(hashStr)
	if hash == nil {
		fmt.Printf("No such hashing function: {0}", hashStr)
	}
	hash.Write(f)

	sum := hash.Sum(nil)
	sumStr := fmt.Sprintf("%x", sum)

	if len(*inputHashPtr) != 0 {
		compareStr := *inputHashPtr
		if compareStr == sumStr {
			fmt.Printf("They match!")
		} else {
			fmt.Printf("They don't match!")
		}
		return;
	}

	fmt.Printf(sumStr)
}

func generateSum(hashStr string) hash.Hash {
	switch {
		case hashStr == "md5":
			return md5.New()
		case hashStr == "sha1":
			return sha1.New()
	}
	return nil
}<commit_msg>Change all printf to println; Fix error with string format.<commit_after>// Copyright 2014 Thomas Keschl. All rights reserved.  Use of this
// source code is governed by a MIT-style license that can be found in
// the LICENSE file.

package main

import(
	"io/ioutil"
	"crypto/md5"
	"crypto/sha1"
	"fmt"
	"flag"
	"hash"
)

func main() {
	inputFilePtr := flag.String("f", "", "path to the file to generate the hash for")
	inputHashPtr := flag.String("c", "", "the hash string to compare against")
	inputTypePtr := flag.String("t", "md5", "the hashing function to use (valid options are md5, sha1)")
	flag.Parse()

	// ensure a file has been entered.
	if len(*inputFilePtr) == 0 {
		fmt.Println("Need to enter a file to hash.")
		return
	}

	filepath := *inputFilePtr

	f,err := ioutil.ReadFile(filepath)
	if err != nil {
		fmt.Println("%s", err)
		return
	}

	hashStr := *inputTypePtr

	hash := generateSum(hashStr)
	if hash == nil {
		fmt.Println("No such hashing function: ", hashStr)
		return
	}
	hash.Write(f)

	sum := hash.Sum(nil)
	sumStr := fmt.Sprintf("%x", sum)

	if len(*inputHashPtr) != 0 {
		compareStr := *inputHashPtr
		if compareStr == sumStr {
			fmt.Printf("They match!")
		} else {
			fmt.Printf("They don't match!")
		}
		return;
	}

	fmt.Println(sumStr)
}

func generateSum(hashStr string) hash.Hash {
	switch {
		case hashStr == "md5":
			return md5.New()
		case hashStr == "sha1":
			return sha1.New()
	}
	return nil
}<|endoftext|>


<commit_before>package main

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

const (
	VERSION = "0.0.1"
)

// sort interface-typed arrays by first-class functions
type ByFn struct{
	elems []interface{}
	comp func(a, b interface{}) bool
}
func (c ByFn) Len() int { return len(c.elems)  }
func (c ByFn) Less(i, j int) bool { return c.comp(c.elems[i], c.elems[j]) }
func (c ByFn) Swap(i, j int) { c.elems[i], c.elems[j] = c.elems[j], c.elems[i] }

// generic api requests
func apiReq(meth string, url string) interface{} {
	client := &http.Client{}
	req, err := http.NewRequest(meth, url, nil)
	req.SetBasicAuth("x", os.Getenv("HEROKU_API_KEY"))
	req.Header.Add("User-Agent", fmt.Sprintf("hk/%s", VERSION))
	req.Header.Add("Accept", "application/json")
	res, err := client.Do(req)
		if err != nil {
		panic(err)
	}
	if (res.StatusCode == 401) {
		error("Unauthorized")
	}
	if (res.StatusCode != 200) {
		error("Unexpected error")
	}
	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		panic(err)
	}
	var data interface{}
	err = json.Unmarshal(body, &data)
	if err != nil {
		panic(err)
	}
	return data
}

// error formatting
func error(msg string) {
	fmt.Fprintf(os.Stderr, "Error: %s.\n", msg)
	os.Exit(1)
}

func unrecArg(arg string) {
	error(fmt.Sprintf("Unrecognized argument '%s'", arg))
}

func unrecCmd(cmd string) {
	error(fmt.Sprintf("'%s' is not an hk command. See 'hk help'", cmd))
}

// commands
func envHelp() {
	fmt.Printf("Usage: hk env -a <app>\n\n")
	fmt.Printf("Show all config vars.")
	os.Exit(0)
}

func env() {
	if (len(os.Args) != 4) || (os.Args[2] != "-a") {
		error("Invalid usage. See hk help env")
	}
	appName := os.Args[3]
	data := apiReq("GET", fmt.Sprintf("https://api.heroku.com/apps/%s/config_vars", appName))
	config := data.(map[string]interface{})
	for k, v := range config {
		fmt.Printf("%s=%v\n", k, v)
	}
	os.Exit(0)
}

func getHelp() {
	fmt.Printf("Usage: hk get -a <app> <key>\n\n")
	fmt.Printf("Get the value of a config var.\n")
	os.Exit(0)
}

func get() {
	if (len(os.Args) != 5) || (os.Args[2] != "-a") {
		error("Invalid usage. See hk help get")
	}
	appName := os.Args[3]
	key := os.Args[4]
	data := apiReq("GET", fmt.Sprintf("https://api.heroku.com/apps/%s/config_vars", appName))
	config := data.(map[string]string)
	value, found := config[key]
	if !found {
		error(fmt.Sprintf("No such key as '%s'", key))
	}
	fmt.Println(value)
	os.Exit(0)
}

func listHelp() {
	fmt.Printf("Usage: hk list\n\n")
	fmt.Printf("List accessible apps.\n")
	os.Exit(0)
}

func list() {
	if len(os.Args) != 2 {
		unrecArg(os.Args[2])
	}
	data := apiReq("GET", "https://api.heroku.com/apps")
	apps := data.([]interface{})
	for i := range apps {
		app := apps[i].(map[string]interface{})
		fmt.Printf("%s\n", app["name"])
	}
	os.Exit(0)
}

func psHelp() {
	fmt.Printf("Usage: hk ps -a <app>\n\n")
	fmt.Printf("List apps processes.\n")
	os.Exit(0)
}

func ps() {
	if (len(os.Args) != 4) || (os.Args[2] != "-a") {
		error("Invalid usage. See hk help ps")
	}
	appName := os.Args[3]
	data := apiReq("GET", fmt.Sprintf("https://api.heroku.com/apps/%s/ps", appName))
	processes := data.([]interface{})
	sort.Sort(ByFn{
		processes,
		func(a, b interface{}) bool {
			p1 := a.(map[string]interface{})["process"].(string)
			p2 := b.(map[string]interface{})["process"].(string)
		  return p1 < p2
	  }})
	fmt.Printf("Process           State       Command\n")
	fmt.Printf("----------------  ----------  ------------------------\n")
	for i := range processes {
		process := processes[i].(map[string]interface{})
		fmt.Printf("%-16s  %-10s  %s\n", process["process"], process["state"], process["command"])
	}
	os.Exit(0)
}

func versionHelp() {
	fmt.Printf("Usage: hk version\n\n")
	fmt.Printf("Show hk client version.\n")
	os.Exit(0)
}

func version() {
	if len(os.Args) != 2 {
	  unrecArg(os.Args[2])
  }
	fmt.Printf("%s\n", VERSION)
	os.Exit(0)
}

func help() {
	if len(os.Args) <= 2 {
		usage()
	} else {
		cmd := os.Args[2]
		switch cmd {
	  case "env":
		  envHelp()
	  case "get":
			getHelp()
		case "list":
		  listHelp()
	  case "ps":
		  psHelp()
		case "version":
			versionHelp()
		}
		unrecCmd(cmd)
	}
}

// top-level usage
func usage() {
	fmt.Printf("Usage: hk <command> [-a <app>] [command-specific-options]\n\n")
	fmt.Printf("Supported hk commands are:\n")
	fmt.Printf("  addons          List add-ons\n")
	fmt.Printf("  addons-add      Add an add-on\n")
	fmt.Printf("  addons-open     Open an add-on page\n")
	fmt.Printf("  addons-remove   Remove an add-on \n")
	fmt.Printf("  create          Create an app\n")
	fmt.Printf("  destroy         Destroy an app\n")
	fmt.Printf("  env             List config vars\n")
	fmt.Printf("  get             Get config var\n")
	fmt.Printf("  help            Show this help\n")
	fmt.Printf("  info            Show app info\n")
	fmt.Printf("  list            List apps\n")
	fmt.Printf("  login           Log in\n")
	fmt.Printf("  logout          Log out\n")
	fmt.Printf("  logs            Show logs\n")
	fmt.Printf("  open            Open app\n")
	fmt.Printf("  pg              List databases\n")
	fmt.Printf("  pg-info         Show database info\n")
	fmt.Printf("  pg-promote      Promote a database\n")
	fmt.Printf("  ps-psql         Open a psql database shell\n")
	fmt.Printf("  pg-wait         Await a database\n")
	fmt.Printf("  ps              List processes\n")
	fmt.Printf("  release         Show release info\n")
	fmt.Printf("  releases        List releases\n")
	fmt.Printf("  rename          Rename an app\n")
	fmt.Printf("  restart         Restart processes\n")
	fmt.Printf("  rollback        Rollback to a previous release\n")
	fmt.Printf("  run             Run a process\n")
	fmt.Printf("  set             Set config var\n")
	fmt.Printf("  scale           Scale processes\n")
	fmt.Printf("  run             Run a process\n")
	fmt.Printf("  stop            Stop a process\n")
	fmt.Printf("  token           Show auth token\n")
	fmt.Printf("  unset           Unset config vars\n")
	fmt.Printf("  version         Display version\n\n")
	fmt.Printf("See 'hk help <command>' for more information on a specific command.\n")
	os.Exit(0)
}

// entry point
func main() {
	if len(os.Args) <= 1 {
		usage()
	} else {
		cmd := os.Args[1]
		switch cmd {
		case "env":
			env()
		case "get":
			get()
		case "help":
			help()
		case "list":
			list()
		case "ps":
			ps()
		case "version":
			version()
		}
		unrecCmd(cmd)
	}
}
<commit_msg>extract cmd help<commit_after>package main

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

const (
	VERSION = "0.0.1"
)

// sort interface-typed arrays by first-class functions
type ByFn struct{
	elems []interface{}
	comp func(a, b interface{}) bool
}
func (c ByFn) Len() int { return len(c.elems)  }
func (c ByFn) Less(i, j int) bool { return c.comp(c.elems[i], c.elems[j]) }
func (c ByFn) Swap(i, j int) { c.elems[i], c.elems[j] = c.elems[j], c.elems[i] }

// generic api requests
func apiReq(meth string, url string) interface{} {
	client := &http.Client{}
	req, err := http.NewRequest(meth, url, nil)
	req.SetBasicAuth("x", os.Getenv("HEROKU_API_KEY"))
	req.Header.Add("User-Agent", fmt.Sprintf("hk/%s", VERSION))
	req.Header.Add("Accept", "application/json")
	res, err := client.Do(req)
		if err != nil {
		panic(err)
	}
	if (res.StatusCode == 401) {
		error("Unauthorized")
	}
	if (res.StatusCode != 200) {
		error("Unexpected error")
	}
	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		panic(err)
	}
	var data interface{}
	err = json.Unmarshal(body, &data)
	if err != nil {
		panic(err)
	}
	return data
}

// error formatting
func error(msg string) {
	fmt.Fprintf(os.Stderr, "Error: %s.\n", msg)
	os.Exit(1)
}

func unrecArg(arg string) {
	error(fmt.Sprintf("Unrecognized argument '%s'", arg))
}

func unrecCmd(cmd string) {
	error(fmt.Sprintf("'%s' is not an hk command. See 'hk help'", cmd))
}

// info formatting
func cmdHelp(usage string, desc string) {
	fmt.Printf("Usage: %s\n\n", usage)
	fmt.Printf("%s.\n", desc)
	os.Exit(0)
}

// commands
func envHelp() {
	cmdHelp("hk env -a <app>", "Show all config vars")
}

func env() {
	if (len(os.Args) != 4) || (os.Args[2] != "-a") {
		error("Invalid usage. See hk help env")
	}
	appName := os.Args[3]
	data := apiReq("GET", fmt.Sprintf("https://api.heroku.com/apps/%s/config_vars", appName))
	config := data.(map[string]interface{})
	for k, v := range config {
		fmt.Printf("%s=%v\n", k, v)
	}
	os.Exit(0)
}

func getHelp() {
	cmdHelp("hk get -a <app> <key>", "Get the value of a config var")
}

func get() {
	if (len(os.Args) != 5) || (os.Args[2] != "-a") {
		error("Invalid usage. See hk help get")
	}
	appName := os.Args[3]
	key := os.Args[4]
	data := apiReq("GET", fmt.Sprintf("https://api.heroku.com/apps/%s/config_vars", appName))
	config := data.(map[string]string)
	value, found := config[key]
	if !found {
		error(fmt.Sprintf("No such key as '%s'", key))
	}
	fmt.Println(value)
	os.Exit(0)
}

func listHelp() {
	cmdHelp("hk list", "List accessible apps")
}

func list() {
	if len(os.Args) != 2 {
		unrecArg(os.Args[2])
	}
	data := apiReq("GET", "https://api.heroku.com/apps")
	apps := data.([]interface{})
	for i := range apps {
		app := apps[i].(map[string]interface{})
		fmt.Printf("%s\n", app["name"])
	}
	os.Exit(0)
}

func psHelp() {
	cmdHelp("hk ps -a <app>", "List app processes")
}

func ps() {
	if (len(os.Args) != 4) || (os.Args[2] != "-a") {
		error("Invalid usage. See hk help ps")
	}
	appName := os.Args[3]
	data := apiReq("GET", fmt.Sprintf("https://api.heroku.com/apps/%s/ps", appName))
	processes := data.([]interface{})
	sort.Sort(ByFn{
		processes,
		func(a, b interface{}) bool {
			p1 := a.(map[string]interface{})["process"].(string)
			p2 := b.(map[string]interface{})["process"].(string)
		  return p1 < p2
	  }})
	fmt.Printf("Process           State       Command\n")
	fmt.Printf("----------------  ----------  ------------------------\n")
	for i := range processes {
		process := processes[i].(map[string]interface{})
		fmt.Printf("%-16s  %-10s  %s\n", process["process"], process["state"], process["command"])
	}
	os.Exit(0)
}

func versionHelp() {
	cmdHelp("hk version", "Show hk client version")
}

func version() {
	if len(os.Args) != 2 {
	  unrecArg(os.Args[2])
  }
	fmt.Printf("%s\n", VERSION)
	os.Exit(0)
}

func help() {
	if len(os.Args) <= 2 {
		usage()
	} else {
		cmd := os.Args[2]
		switch cmd {
	  case "env":
		  envHelp()
	  case "get":
			getHelp()
		case "list":
		  listHelp()
	  case "ps":
		  psHelp()
		case "version":
			versionHelp()
		}
		unrecCmd(cmd)
	}
}

// top-level usage
func usage() {
	fmt.Printf("Usage: hk <command> [-a <app>] [command-specific-options]\n\n")
	fmt.Printf("Supported hk commands are:\n")
	fmt.Printf("  addons          List add-ons\n")
	fmt.Printf("  addons-add      Add an add-on\n")
	fmt.Printf("  addons-open     Open an add-on page\n")
	fmt.Printf("  addons-remove   Remove an add-on \n")
	fmt.Printf("  create          Create an app\n")
	fmt.Printf("  destroy         Destroy an app\n")
	fmt.Printf("  env             List config vars\n")
	fmt.Printf("  get             Get config var\n")
	fmt.Printf("  help            Show this help\n")
	fmt.Printf("  info            Show app info\n")
	fmt.Printf("  list            List apps\n")
	fmt.Printf("  login           Log in\n")
	fmt.Printf("  logout          Log out\n")
	fmt.Printf("  logs            Show logs\n")
	fmt.Printf("  open            Open app\n")
	fmt.Printf("  pg              List databases\n")
	fmt.Printf("  pg-info         Show database info\n")
	fmt.Printf("  pg-promote      Promote a database\n")
	fmt.Printf("  ps-psql         Open a psql database shell\n")
	fmt.Printf("  pg-wait         Await a database\n")
	fmt.Printf("  ps              List processes\n")
	fmt.Printf("  release         Show release info\n")
	fmt.Printf("  releases        List releases\n")
	fmt.Printf("  rename          Rename an app\n")
	fmt.Printf("  restart         Restart processes\n")
	fmt.Printf("  rollback        Rollback to a previous release\n")
	fmt.Printf("  run             Run a process\n")
	fmt.Printf("  set             Set config var\n")
	fmt.Printf("  scale           Scale processes\n")
	fmt.Printf("  run             Run a process\n")
	fmt.Printf("  stop            Stop a process\n")
	fmt.Printf("  token           Show auth token\n")
	fmt.Printf("  unset           Unset config vars\n")
	fmt.Printf("  version         Display version\n\n")
	fmt.Printf("See 'hk help <command>' for more information on a specific command.\n")
	os.Exit(0)
}

// entry point
func main() {
	if len(os.Args) <= 1 {
		usage()
	} else {
		cmd := os.Args[1]
		switch cmd {
		case "env":
			env()
		case "get":
			get()
		case "help":
			help()
		case "list":
			list()
		case "ps":
			ps()
		case "version":
			version()
		}
		unrecCmd(cmd)
	}
}
<|endoftext|>


<commit_before>//Command to test application without deploy:
//goapp serve app.yaml
//Command to deploy/update application:
//goapp deploy -application golangnode0 -version 0

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"html/template"
	"io/ioutil"
	"net/http"
	"time"

	"appengine"
	"appengine/urlfetch"
)

//predefined parameters
var maxNodes int = 10
var isAliveCheckPeriod int = 500 //in millisecs

//changeable parameters
var statusContent string = "Default status"
var statusLog string = ""

//nodesStates := make(map[int]map[string]string)
/*
example for this map
var nodesStates map[int]map[string]string{
	1: map[string]string{
		"alive":"1",
		"hasTask":"true",
		"taskStatus":"completed",
		"taskResult":"some_result_for_node"
	},
}
*/

type webPage struct {
	Title string
}

type nodeStats struct {
	NodeID           int    `json:"ID"`
	NodeCount        int    `json:"nodeCount"`
	HasTask          bool   `json:"hasTask"`
	TaskStatus       string `json:"taskStatus"` //running-copleted-loaded
	TaskResult       string `json:"taskResult"`
	TaskFragmentBody string `json:"taskFragmentBody"`
	TaskBody         string `json:"taskBody"`
}

type echoMessage struct {
	Title   string `json:"title"`
	Content string `json:"content"`
}

//types for periodical functions
type pFunc func()
type pFuncInt func(int)

//wrong func for Google App Engine deployment. Need to use appengine libs...=(
func echo() {

	url := "http://golangappnode1.appspot.com/status"

	var jsonStr = []byte(`{"msg":"Hello!"}`)
	req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		panic(err)
	}

	defer resp.Body.Close()

	body, _ := ioutil.ReadAll(resp.Body)
	statusContent = string(body)

}

func helloWorld(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello World!")
}

func startPage(w http.ResponseWriter, r *http.Request) {
	switch r.Method {
	case "GET":
		templatePage, _ := template.ParseFiles("start.html")
		templatePage.Execute(w, &webPage{"simplePage"})
	case "POST":
		r.ParseForm()
		//go echo()
		//fmt.Fprintf(w, "Successful read command/input from web-interface! Input contains - "+r.FormValue("nodeId")+" "+r.FormValue("echoContent"))
	}
}

func statusServer(w http.ResponseWriter, r *http.Request) {
	switch r.Method {
	case "GET":
		fmt.Fprintf(w, "Get status - "+statusContent)
	case "POST":
		buf := new(bytes.Buffer)
		buf.ReadFrom(r.Body)
		newStr := buf.String()

		/*inputMsg := echoMessage{}
		err2 := json.NewDecoder(r.Body).Decode(&inputMsg)
		if err2 != nil {
			panic(err2)
		}*/

		thisNodeStats := nodeStats{
			1,
			2,
			false,
			"not running",
			"empty",
			"empty fragment",
			"empty",
		}

		jsonNodeStats, err1 := json.Marshal(thisNodeStats)
		if err1 != nil {
			panic(err1)
		}

		fmt.Fprintf(w, "Get data by params in POST - OK "+string(jsonNodeStats))
		//statusContent = "POST request handled, " + "Node id: " + string(nodeSends.id) + ", Echo content: " + nodeSends.content
		statusContent = "POST request handled, " + newStr //+ "Input message object content: " + inputMsg.Title + inputMsg.Content
	}
}

//Functions for isAlive checking realization
func checkIsAlive(nodeId int) {
	nodeUrl := "http://goappnode" + string(nodeId) + "0.appspot.com/"
	resp, err := http.Get(nodeUrl)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()
	if resp.StatusCode == 200 {
		statusLog += "Node #" + string(nodeId) + " - online"
	} else {
		statusLog += "Node #" + string(nodeId) + " - offline"
	}
}

func periodicTask(period time.Duration, task pFuncInt) {
	for {
		task(1)
		time.Sleep(period * time.Millisecond)
	}
}

/*
func checkAliveNodes(t time.Tick) {
	resp, err := http.Get("http://goappnode1.appspot.com/isalive")
	if err != nil {
		panic(err)
	}

}
*/

func isAliveServer(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, 1)
}

func checkAliveStart(w http.ResponseWriter, r *http.Request) {
	go periodicTask(30000, checkIsAlive(1))
}

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

}
*/

func testEcho(w http.ResponseWriter, r *http.Request) {
	msg := echoMessage{
		"Message is",
		"",
	}

	r.ParseForm()
	c := appengine.NewContext(r)
	msg.Content = r.FormValue("echoContent")

	jsonMessage, err2 := json.Marshal(msg)
	if err2 != nil {
		panic(err2)
	}

	//jsonStr := []byte(`{"message":"` + r.FormValue("echoContent") + `"}`)
	jsonStr := []byte(jsonMessage)
	buf := bytes.NewBuffer(jsonStr)
	client := http.Client{Transport: &urlfetch.Transport{Context: c}}
	resp, err := client.Post("http://goappnode"+r.FormValue("nodeId")+".appspot.com/status", "application/octet-stream", buf)
	if err != nil {
		statusContent = err.Error()
		fmt.Println(err)
	}
	respBody, _ := ioutil.ReadAll(resp.Body)
	statusContent = "Response from node - " + string(respBody)
}

func showInfo(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintln(w, "Information page for test project.")
	fmt.Fprintln(w, "Language - Go;")
	fmt.Fprintln(w, "Platform - Google Application Engine;")
}

func init() {
	//view pages
	http.HandleFunc("/", startPage)
	http.HandleFunc("/helloworld", helloWorld)
	http.HandleFunc("/showinfo", showInfo)
	//service pages
	http.HandleFunc("/echo", testEcho)
	http.HandleFunc("/status", statusServer)
	http.HandleFunc("/isalive", isAliveServer)
	http.HandleFunc("/startcheck", checkAliveStart)

	//Wrong code for App Enine - server cant understand what it need to show
	//http.ListenAndServe(":80", nil)
}

//this func not needed for deploy on Google App Engine, init() func replace main()
/*
func main() {
	//fmt.Println("Hello, test server started on 8080 port.\n - /helloworld - show title page\n - /showinfo - show information about this thing")
	//http.ListenAndServe(":8080", nil)
	go sender()
}*/
<commit_msg>fix with args - remember it!<commit_after>//Command to test application without deploy:
//goapp serve app.yaml
//Command to deploy/update application:
//goapp deploy -application golangnode0 -version 0

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"html/template"
	"io/ioutil"
	"net/http"
	"time"

	"appengine"
	"appengine/urlfetch"
)

//predefined parameters
var maxNodes int = 10
var isAliveCheckPeriod int = 500 //in millisecs

//changeable parameters
var statusContent string = "Default status"
var statusLog string = ""

//nodesStates := make(map[int]map[string]string)
/*
example for this map
var nodesStates map[int]map[string]string{
	1: map[string]string{
		"alive":"1",
		"hasTask":"true",
		"taskStatus":"completed",
		"taskResult":"some_result_for_node"
	},
}
*/

type webPage struct {
	Title string
}

type nodeStats struct {
	NodeID           int    `json:"ID"`
	NodeCount        int    `json:"nodeCount"`
	HasTask          bool   `json:"hasTask"`
	TaskStatus       string `json:"taskStatus"` //running-copleted-loaded
	TaskResult       string `json:"taskResult"`
	TaskFragmentBody string `json:"taskFragmentBody"`
	TaskBody         string `json:"taskBody"`
}

type echoMessage struct {
	Title   string `json:"title"`
	Content string `json:"content"`
}

//types for periodical functions
type pFunc func()
type pFuncInt func(int)

//wrong func for Google App Engine deployment. Need to use appengine libs...=(
func echo() {

	url := "http://golangappnode1.appspot.com/status"

	var jsonStr = []byte(`{"msg":"Hello!"}`)
	req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		panic(err)
	}

	defer resp.Body.Close()

	body, _ := ioutil.ReadAll(resp.Body)
	statusContent = string(body)

}

func helloWorld(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello World!")
}

func startPage(w http.ResponseWriter, r *http.Request) {
	switch r.Method {
	case "GET":
		templatePage, _ := template.ParseFiles("start.html")
		templatePage.Execute(w, &webPage{"simplePage"})
	case "POST":
		r.ParseForm()
		//go echo()
		//fmt.Fprintf(w, "Successful read command/input from web-interface! Input contains - "+r.FormValue("nodeId")+" "+r.FormValue("echoContent"))
	}
}

func statusServer(w http.ResponseWriter, r *http.Request) {
	switch r.Method {
	case "GET":
		fmt.Fprintf(w, "Get status - "+statusContent)
	case "POST":
		buf := new(bytes.Buffer)
		buf.ReadFrom(r.Body)
		newStr := buf.String()

		/*inputMsg := echoMessage{}
		err2 := json.NewDecoder(r.Body).Decode(&inputMsg)
		if err2 != nil {
			panic(err2)
		}*/

		thisNodeStats := nodeStats{
			1,
			2,
			false,
			"not running",
			"empty",
			"empty fragment",
			"empty",
		}

		jsonNodeStats, err1 := json.Marshal(thisNodeStats)
		if err1 != nil {
			panic(err1)
		}

		fmt.Fprintf(w, "Get data by params in POST - OK "+string(jsonNodeStats))
		//statusContent = "POST request handled, " + "Node id: " + string(nodeSends.id) + ", Echo content: " + nodeSends.content
		statusContent = "POST request handled, " + newStr //+ "Input message object content: " + inputMsg.Title + inputMsg.Content
	}
}

//Functions for isAlive checking realization
func checkIsAlive(nodeId int) {
	nodeUrl := "http://goappnode" + string(nodeId) + "0.appspot.com/"
	resp, err := http.Get(nodeUrl)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()
	if resp.StatusCode == 200 {
		statusLog += "Node #" + string(nodeId) + " - online"
	} else {
		statusLog += "Node #" + string(nodeId) + " - offline"
	}
}

func periodicTask(period time.Duration, task pFuncInt, taskArg int) {
	for {
		task(taskArg)
		time.Sleep(period * time.Millisecond)
	}
}

/*
func checkAliveNodes(t time.Tick) {
	resp, err := http.Get("http://goappnode1.appspot.com/isalive")
	if err != nil {
		panic(err)
	}

}
*/

func isAliveServer(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, 1)
}

func checkAliveStart(w http.ResponseWriter, r *http.Request) {
	go periodicTask(30000, checkIsAlive, 1)
}

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

}
*/

func testEcho(w http.ResponseWriter, r *http.Request) {
	msg := echoMessage{
		"Message is",
		"",
	}

	r.ParseForm()
	c := appengine.NewContext(r)
	msg.Content = r.FormValue("echoContent")

	jsonMessage, err2 := json.Marshal(msg)
	if err2 != nil {
		panic(err2)
	}

	//jsonStr := []byte(`{"message":"` + r.FormValue("echoContent") + `"}`)
	jsonStr := []byte(jsonMessage)
	buf := bytes.NewBuffer(jsonStr)
	client := http.Client{Transport: &urlfetch.Transport{Context: c}}
	resp, err := client.Post("http://goappnode"+r.FormValue("nodeId")+".appspot.com/status", "application/octet-stream", buf)
	if err != nil {
		statusContent = err.Error()
		fmt.Println(err)
	}
	respBody, _ := ioutil.ReadAll(resp.Body)
	statusContent = "Response from node - " + string(respBody)
}

func showInfo(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintln(w, "Information page for test project.")
	fmt.Fprintln(w, "Language - Go;")
	fmt.Fprintln(w, "Platform - Google Application Engine;")
}

func init() {
	//view pages
	http.HandleFunc("/", startPage)
	http.HandleFunc("/helloworld", helloWorld)
	http.HandleFunc("/showinfo", showInfo)
	//service pages
	http.HandleFunc("/echo", testEcho)
	http.HandleFunc("/status", statusServer)
	http.HandleFunc("/isalive", isAliveServer)
	http.HandleFunc("/startcheck", checkAliveStart)

	//Wrong code for App Enine - server cant understand what it need to show
	//http.ListenAndServe(":80", nil)
}

//this func not needed for deploy on Google App Engine, init() func replace main()
/*
func main() {
	//fmt.Println("Hello, test server started on 8080 port.\n - /helloworld - show title page\n - /showinfo - show information about this thing")
	//http.ListenAndServe(":8080", nil)
	go sender()
}*/
<|endoftext|>