<commit_before>package btcwallet

import (
	"fmt"

	"github.com/go-errors/errors"
	"github.com/lightningnetwork/lnd/lnwallet"
	"github.com/roasbeef/btcd/btcec"
	"github.com/roasbeef/btcd/chaincfg/chainhash"
	"github.com/roasbeef/btcd/txscript"
	"github.com/roasbeef/btcd/wire"
	"github.com/roasbeef/btcutil"
	"github.com/roasbeef/btcwallet/waddrmgr"
	base "github.com/roasbeef/btcwallet/wallet"
)

// FetchInputInfo queries for the WalletController's knowledge of the passed
// outpoint. If the base wallet determines this output is under its control,
// then the original txout should be returned. Otherwise, a non-nil error value
// of ErrNotMine should be returned instead.
//
// This is a part of the WalletController interface.
func (b *BtcWallet) FetchInputInfo(prevOut *wire.OutPoint) (*wire.TxOut, error) {
	var (
		err    error
		output *wire.TxOut
	)

	// First check to see if the output is already within the utxo cache.
	// If so we can return directly saving a disk access.
	b.cacheMtx.RLock()
	if output, ok := b.utxoCache[*prevOut]; ok {
		b.cacheMtx.RUnlock()
		return output, nil
	}
	b.cacheMtx.RUnlock()

	// Otherwse, we manually look up the output within the tx store.
	txid := &prevOut.Hash
	txDetail, err := base.UnstableAPI(b.wallet).TxDetails(txid)
	if err != nil {
		return nil, err
	} else if txDetail == nil {
		return nil, lnwallet.ErrNotMine
	}

	output = txDetail.TxRecord.MsgTx.TxOut[prevOut.Index]

	b.cacheMtx.Lock()
	b.utxoCache[*prevOut] = output
	b.cacheMtx.Unlock()

	return output, nil
}

// fetchOutputKey attempts to fetch the managed address corresponding to the
// passed output script. This function is used to look up the proper key which
// should be used to sign a specified input.
func (b *BtcWallet) fetchOutputAddr(script []byte) (waddrmgr.ManagedAddress, error) {
	_, addrs, _, err := txscript.ExtractPkScriptAddrs(script, b.netParams)
	if err != nil {
		return nil, err
	}

	// If the case of a multi-sig output, several address may be extracted.
	// Therefore, we simply select the key for the first address we know
	// of.
	for _, addr := range addrs {
		addr, err := b.wallet.AddressInfo(addr)
		if err == nil {
			return addr, nil
		}
	}

	// TODO(roasbeef): use the errors.wrap package
	return nil, fmt.Errorf("address not found")
}

// fetchPrivKey attempts to retrieve the raw private key corresponding to the
// passed public key.
// TODO(roasbeef): alternatively can extract all the data pushes within the
// script, then attempt to match keys one by one
func (b *BtcWallet) fetchPrivKey(pub *btcec.PublicKey) (*btcec.PrivateKey, error) {
	hash160 := btcutil.Hash160(pub.SerializeCompressed())
	addr, err := btcutil.NewAddressWitnessPubKeyHash(hash160, b.netParams)
	if err != nil {
		return nil, err
	}

	return b.wallet.PrivKeyForAddress(addr)
}

// maybeTweakPrivKey examines the single and double tweak parameters on the
// passed sign descriptor and may perform a mapping on the passed private key
// in order to utilize the tweaks, if populated.
func maybeTweakPrivKey(signDesc *lnwallet.SignDescriptor,
	privKey *btcec.PrivateKey) (*btcec.PrivateKey, error) {

	var retPriv *btcec.PrivateKey
	switch {

	case signDesc.SingleTweak != nil:
		retPriv = lnwallet.TweakPrivKey(privKey,
			signDesc.SingleTweak)

	case signDesc.DoubleTweak != nil:
		retPriv = lnwallet.DeriveRevocationPrivKey(privKey,
			signDesc.DoubleTweak)

	default:
		retPriv = privKey
	}

	return retPriv, nil
}

// SignOutputRaw generates a signature for the passed transaction according to
// the data within the passed SignDescriptor.
//
// This is a part of the WalletController interface.
func (b *BtcWallet) SignOutputRaw(tx *wire.MsgTx,
	signDesc *lnwallet.SignDescriptor) ([]byte, error) {
	witnessScript := signDesc.WitnessScript

	// First attempt to fetch the private key which corresponds to the
	// specified public key.
	privKey, err := b.fetchPrivKey(signDesc.PubKey)
	if err != nil {
		return nil, err
	}

	// If a tweak (single or double) is specified, then we'll need to use
	// this tweak to derive the final private key to be used for signing
	// this output.
	privKey, err = maybeTweakPrivKey(signDesc, privKey)
	if err != nil {
		return nil, err
	}

	// TODO(roasbeef): generate sighash midstate if not present?

	amt := signDesc.Output.Value
	sig, err := txscript.RawTxInWitnessSignature(tx, signDesc.SigHashes,
		signDesc.InputIndex, amt, witnessScript, txscript.SigHashAll,
		privKey)
	if err != nil {
		return nil, err
	}

	// Chop off the sighash flag at the end of the signature.
	return sig[:len(sig)-1], nil
}

// ComputeInputScript generates a complete InputIndex for the passed
// transaction with the signature as defined within the passed SignDescriptor.
// This method is capable of generating the proper input script for both
// regular p2wkh output and p2wkh outputs nested within a regular p2sh output.
//
// This is a part of the WalletController interface.
func (b *BtcWallet) ComputeInputScript(tx *wire.MsgTx,
	signDesc *lnwallet.SignDescriptor) (*lnwallet.InputScript, error) {

	outputScript := signDesc.Output.PkScript
	walletAddr, err := b.fetchOutputAddr(outputScript)
	if err != nil {
		return nil, nil
	}

	pka := walletAddr.(waddrmgr.ManagedPubKeyAddress)
	privKey, err := pka.PrivKey()
	if err != nil {
		return nil, err
	}

	var witnessProgram []byte
	inputScript := &lnwallet.InputScript{}

	switch {

	// If we're spending p2wkh output nested within a p2sh output, then
	// we'll need to attach a sigScript in addition to witness data.
	case pka.IsNestedWitness():
		pubKey := privKey.PubKey()
		pubKeyHash := btcutil.Hash160(pubKey.SerializeCompressed())

		// Next, we'll generate a valid sigScript that'll allow us to
		// spend the p2sh output. The sigScript will contain only a
		// single push of the p2wkh witness program corresponding to
		// the matching public key of this address.
		p2wkhAddr, err := btcutil.NewAddressWitnessPubKeyHash(pubKeyHash,
			b.netParams)
		if err != nil {
			return nil, err
		}
		witnessProgram, err = txscript.PayToAddrScript(p2wkhAddr)
		if err != nil {
			return nil, err
		}

		bldr := txscript.NewScriptBuilder()
		bldr.AddData(witnessProgram)
		sigScript, err := bldr.Script()
		if err != nil {
			return nil, err
		}

		inputScript.ScriptSig = sigScript

	// Otherwise, this is a regular p2wkh output, so we include the
	// witness program itself as the subscript to generate the proper
	// sighash digest. As part of the new sighash digest algorithm, the
	// p2wkh witness program will be expanded into a regular p2kh
	// script.
	default:
		witnessProgram = outputScript
	}

	// If a tweak (single or double) is specified, then we'll need to use
	// this tweak to derive the final private key to be used for signing
	// this output.
	privKey, err = maybeTweakPrivKey(signDesc, privKey)
	if err != nil {
		return nil, err
	}

	// Generate a valid witness stack for the input.
	// TODO(roasbeef): adhere to passed HashType
	witnessScript, err := txscript.WitnessSignature(tx, signDesc.SigHashes,
		signDesc.InputIndex, signDesc.Output.Value, witnessProgram,
		txscript.SigHashAll, privKey, true)
	if err != nil {
		return nil, err
	}

	inputScript.Witness = witnessScript

	return inputScript, nil
}

// A compile time check to ensure that BtcWallet implements the Signer
// interface.
var _ lnwallet.Signer = (*BtcWallet)(nil)

// SignMessage attempts to sign a target message with the private key that
// corresponds to the passed public key. If the target private key is unable to
// be found, then an error will be returned. The actual digest signed is the
// double SHA-256 of the passed message.
//
// NOTE: This is a part of the MessageSigner interface.
func (b *BtcWallet) SignMessage(pubKey *btcec.PublicKey,
	msg []byte) (*btcec.Signature, error) {

	// First attempt to fetch the private key which corresponds to the
	// specified public key.
	privKey, err := b.fetchPrivKey(pubKey)
	if err != nil {
		return nil, err
	}

	// Double hash and sign the data.
	msgDigest := chainhash.DoubleHashB(msg)
	sign, err := privKey.Sign(msgDigest)
	if err != nil {
		return nil, errors.Errorf("unable sign the message: %v", err)
	}

	return sign, nil
}

// A compile time check to ensure that BtcWallet implements the MessageSigner
// interface.
var _ lnwallet.MessageSigner = (*BtcWallet)(nil)
<commit_msg>lnwallet/btcwallet: Use signDesc.HashType when signing<commit_after>package btcwallet

import (
	"fmt"

	"github.com/go-errors/errors"
	"github.com/lightningnetwork/lnd/lnwallet"
	"github.com/roasbeef/btcd/btcec"
	"github.com/roasbeef/btcd/chaincfg/chainhash"
	"github.com/roasbeef/btcd/txscript"
	"github.com/roasbeef/btcd/wire"
	"github.com/roasbeef/btcutil"
	"github.com/roasbeef/btcwallet/waddrmgr"
	base "github.com/roasbeef/btcwallet/wallet"
)

// FetchInputInfo queries for the WalletController's knowledge of the passed
// outpoint. If the base wallet determines this output is under its control,
// then the original txout should be returned. Otherwise, a non-nil error value
// of ErrNotMine should be returned instead.
//
// This is a part of the WalletController interface.
func (b *BtcWallet) FetchInputInfo(prevOut *wire.OutPoint) (*wire.TxOut, error) {
	var (
		err    error
		output *wire.TxOut
	)

	// First check to see if the output is already within the utxo cache.
	// If so we can return directly saving a disk access.
	b.cacheMtx.RLock()
	if output, ok := b.utxoCache[*prevOut]; ok {
		b.cacheMtx.RUnlock()
		return output, nil
	}
	b.cacheMtx.RUnlock()

	// Otherwse, we manually look up the output within the tx store.
	txid := &prevOut.Hash
	txDetail, err := base.UnstableAPI(b.wallet).TxDetails(txid)
	if err != nil {
		return nil, err
	} else if txDetail == nil {
		return nil, lnwallet.ErrNotMine
	}

	output = txDetail.TxRecord.MsgTx.TxOut[prevOut.Index]

	b.cacheMtx.Lock()
	b.utxoCache[*prevOut] = output
	b.cacheMtx.Unlock()

	return output, nil
}

// fetchOutputKey attempts to fetch the managed address corresponding to the
// passed output script. This function is used to look up the proper key which
// should be used to sign a specified input.
func (b *BtcWallet) fetchOutputAddr(script []byte) (waddrmgr.ManagedAddress, error) {
	_, addrs, _, err := txscript.ExtractPkScriptAddrs(script, b.netParams)
	if err != nil {
		return nil, err
	}

	// If the case of a multi-sig output, several address may be extracted.
	// Therefore, we simply select the key for the first address we know
	// of.
	for _, addr := range addrs {
		addr, err := b.wallet.AddressInfo(addr)
		if err == nil {
			return addr, nil
		}
	}

	// TODO(roasbeef): use the errors.wrap package
	return nil, fmt.Errorf("address not found")
}

// fetchPrivKey attempts to retrieve the raw private key corresponding to the
// passed public key.
// TODO(roasbeef): alternatively can extract all the data pushes within the
// script, then attempt to match keys one by one
func (b *BtcWallet) fetchPrivKey(pub *btcec.PublicKey) (*btcec.PrivateKey, error) {
	hash160 := btcutil.Hash160(pub.SerializeCompressed())
	addr, err := btcutil.NewAddressWitnessPubKeyHash(hash160, b.netParams)
	if err != nil {
		return nil, err
	}

	return b.wallet.PrivKeyForAddress(addr)
}

// maybeTweakPrivKey examines the single and double tweak parameters on the
// passed sign descriptor and may perform a mapping on the passed private key
// in order to utilize the tweaks, if populated.
func maybeTweakPrivKey(signDesc *lnwallet.SignDescriptor,
	privKey *btcec.PrivateKey) (*btcec.PrivateKey, error) {

	var retPriv *btcec.PrivateKey
	switch {

	case signDesc.SingleTweak != nil:
		retPriv = lnwallet.TweakPrivKey(privKey,
			signDesc.SingleTweak)

	case signDesc.DoubleTweak != nil:
		retPriv = lnwallet.DeriveRevocationPrivKey(privKey,
			signDesc.DoubleTweak)

	default:
		retPriv = privKey
	}

	return retPriv, nil
}

// SignOutputRaw generates a signature for the passed transaction according to
// the data within the passed SignDescriptor.
//
// This is a part of the WalletController interface.
func (b *BtcWallet) SignOutputRaw(tx *wire.MsgTx,
	signDesc *lnwallet.SignDescriptor) ([]byte, error) {
	witnessScript := signDesc.WitnessScript

	// First attempt to fetch the private key which corresponds to the
	// specified public key.
	privKey, err := b.fetchPrivKey(signDesc.PubKey)
	if err != nil {
		return nil, err
	}

	// If a tweak (single or double) is specified, then we'll need to use
	// this tweak to derive the final private key to be used for signing
	// this output.
	privKey, err = maybeTweakPrivKey(signDesc, privKey)
	if err != nil {
		return nil, err
	}

	// TODO(roasbeef): generate sighash midstate if not present?

	amt := signDesc.Output.Value
	sig, err := txscript.RawTxInWitnessSignature(tx, signDesc.SigHashes,
		signDesc.InputIndex, amt, witnessScript, signDesc.HashType,
		privKey)
	if err != nil {
		return nil, err
	}

	// Chop off the sighash flag at the end of the signature.
	return sig[:len(sig)-1], nil
}

// ComputeInputScript generates a complete InputIndex for the passed
// transaction with the signature as defined within the passed SignDescriptor.
// This method is capable of generating the proper input script for both
// regular p2wkh output and p2wkh outputs nested within a regular p2sh output.
//
// This is a part of the WalletController interface.
func (b *BtcWallet) ComputeInputScript(tx *wire.MsgTx,
	signDesc *lnwallet.SignDescriptor) (*lnwallet.InputScript, error) {

	outputScript := signDesc.Output.PkScript
	walletAddr, err := b.fetchOutputAddr(outputScript)
	if err != nil {
		return nil, nil
	}

	pka := walletAddr.(waddrmgr.ManagedPubKeyAddress)
	privKey, err := pka.PrivKey()
	if err != nil {
		return nil, err
	}

	var witnessProgram []byte
	inputScript := &lnwallet.InputScript{}

	switch {

	// If we're spending p2wkh output nested within a p2sh output, then
	// we'll need to attach a sigScript in addition to witness data.
	case pka.IsNestedWitness():
		pubKey := privKey.PubKey()
		pubKeyHash := btcutil.Hash160(pubKey.SerializeCompressed())

		// Next, we'll generate a valid sigScript that'll allow us to
		// spend the p2sh output. The sigScript will contain only a
		// single push of the p2wkh witness program corresponding to
		// the matching public key of this address.
		p2wkhAddr, err := btcutil.NewAddressWitnessPubKeyHash(pubKeyHash,
			b.netParams)
		if err != nil {
			return nil, err
		}
		witnessProgram, err = txscript.PayToAddrScript(p2wkhAddr)
		if err != nil {
			return nil, err
		}

		bldr := txscript.NewScriptBuilder()
		bldr.AddData(witnessProgram)
		sigScript, err := bldr.Script()
		if err != nil {
			return nil, err
		}

		inputScript.ScriptSig = sigScript

	// Otherwise, this is a regular p2wkh output, so we include the
	// witness program itself as the subscript to generate the proper
	// sighash digest. As part of the new sighash digest algorithm, the
	// p2wkh witness program will be expanded into a regular p2kh
	// script.
	default:
		witnessProgram = outputScript
	}

	// If a tweak (single or double) is specified, then we'll need to use
	// this tweak to derive the final private key to be used for signing
	// this output.
	privKey, err = maybeTweakPrivKey(signDesc, privKey)
	if err != nil {
		return nil, err
	}

	// Generate a valid witness stack for the input.
	// TODO(roasbeef): adhere to passed HashType
	witnessScript, err := txscript.WitnessSignature(tx, signDesc.SigHashes,
		signDesc.InputIndex, signDesc.Output.Value, witnessProgram,
		signDesc.HashType, privKey, true)
	if err != nil {
		return nil, err
	}

	inputScript.Witness = witnessScript

	return inputScript, nil
}

// A compile time check to ensure that BtcWallet implements the Signer
// interface.
var _ lnwallet.Signer = (*BtcWallet)(nil)

// SignMessage attempts to sign a target message with the private key that
// corresponds to the passed public key. If the target private key is unable to
// be found, then an error will be returned. The actual digest signed is the
// double SHA-256 of the passed message.
//
// NOTE: This is a part of the MessageSigner interface.
func (b *BtcWallet) SignMessage(pubKey *btcec.PublicKey,
	msg []byte) (*btcec.Signature, error) {

	// First attempt to fetch the private key which corresponds to the
	// specified public key.
	privKey, err := b.fetchPrivKey(pubKey)
	if err != nil {
		return nil, err
	}

	// Double hash and sign the data.
	msgDigest := chainhash.DoubleHashB(msg)
	sign, err := privKey.Sign(msgDigest)
	if err != nil {
		return nil, errors.Errorf("unable sign the message: %v", err)
	}

	return sign, nil
}

// A compile time check to ensure that BtcWallet implements the MessageSigner
// interface.
var _ lnwallet.MessageSigner = (*BtcWallet)(nil)
<|endoftext|>


<commit_before>// Articles
package golb

import (
	"errors"
	"strings"
	"time"
)

type Articles []*Article

type Article struct {
	Date     time.Time
	Title    string
	Slug     string
	Body     string
	Tags     []string
	Enabled  bool
	Author   string
	Comments Comments
}

func (a *Article) makeSlug() {
	r := strings.NewReplacer(" ", "-")
	a.Slug = strings.ToLower(r.Replace(a.Title))
}

func (a *Article) Publish() {
	a.Date = time.Now()
	a.Enabled = true
}

func (a *Article) Suppress() {
	a.Enabled = false
}

func (a Articles) Len() int {
	return len(a)
}

func (a Articles) Swap(i, j int) {
	a[i], a[j] = a[j], a[i]
}

func (a Articles) Less(i, j int) bool {
	return a[i].Date.Before(a[j].Date)
}

func (a *Articles) Add(article *Article) {
	article.Date = time.Now()
	if article.Slug == "" {
		article.makeSlug()
	}
	*a = append(*a, article)
}

func (a Articles) Find(slug string) (*Article, error) {
	for i, _ := range a {
		if a[i].Slug == slug {
			return a[i], nil
		}
	}
	return &Article{}, errors.New("not found")
}

func (a Articles) CountTags() map[string]int {
	tags := make(map[string]int)
	for _, article := range a {
		for _, tag := range article.Tags {
			tags[tag]++
		}
	}
	return tags
}

func (a Article) HasTag(tag string) bool {
	for _, t := range a.Tags {
		if t == tag {
			return true
		}
	}
	return false
}
<commit_msg>tag stub<commit_after>// Articles
package golb

import (
	"errors"
	"strings"
	"time"
)

type Articles []*Article
type TagMap map[string]Articles
type Tags map[string]int

type Article struct {
	Date     time.Time
	Title    string
	Slug     string
	Body     string
	Tags     []string
	Enabled  bool
	Author   string
	Comments Comments
}

func (a *Article) makeSlug() {
	r := strings.NewReplacer(" ", "-")
	a.Slug = strings.ToLower(r.Replace(a.Title))
}

func (a *Article) Publish() {
	a.Date = time.Now()
	a.Enabled = true
}

func (a *Article) Suppress() {
	a.Enabled = false
}

func (a Articles) Len() int {
	return len(a)
}

func (a Articles) Swap(i, j int) {
	a[i], a[j] = a[j], a[i]
}

func (a Articles) Less(i, j int) bool {
	return a[i].Date.Before(a[j].Date)
}

func (a *Articles) Add(article *Article) {
	article.Date = time.Now()
	if article.Slug == "" {
		article.makeSlug()
	}
	*a = append(*a, article)
}

func (a Articles) Find(slug string) (*Article, error) {
	for i, _ := range a {
		if a[i].Slug == slug {
			return a[i], nil
		}
	}
	return &Article{}, errors.New("not found")
}

func (a Articles) CountTags() Tags {
	tags := make(Tags)
	for _, article := range a {
		for _, tag := range article.Tags {
			tags[tag]++
		}
	}
	return tags
}

func (a Article) HasTag(tag string) bool {
	for _, t := range a.Tags {
		if t == tag {
			return true
		}
	}
	return false
}

func (a *Articles) TagMap() TagMap {
	tm := make(TagMap)
	for tag, _ := range a.CountTags() {
		for _, article := range *a {
			if article.HasTag(tag) {
				tm[tag] = append(tm[tag], article)
			}
		}
	}
	return tm
}
<|endoftext|>


<commit_before>package main

import (
	"os"
	"testing"

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

var a = AUR{baseDir}

// Test getting deps from AUR.
func TestGetDeps(t *testing.T) {
	deps := make(map[string]struct{})

	err := a.getDeps([]string{"sway-git"}, deps)
	assert.NoError(t, err)
	assert.Len(t, deps, 4)
	_, ok := deps["sway-git"]
	assert.True(t, ok)
	_, ok = deps["wlroots-git"]
	assert.True(t, ok)

	deps = make(map[string]struct{})
	err = a.getDeps([]string{"virtualbox-host-modules-mainline"}, deps)
	assert.NoError(t, err)
	assert.Len(t, deps, 2)

	err = os.RemoveAll(baseDir)
	assert.NoError(t, err)
}

// Test Getting sources from AUR.
func TestAURGet(t *testing.T) {
	_, err := a.Get([]string{"sway-git"})
	assert.NoError(t, err)

	err = os.RemoveAll(baseDir)
	assert.NoError(t, err)
}
<commit_msg>Fix tests<commit_after>package main

import (
	"os"
	"testing"

	"github.com/stretchr/testify/require"
)

var a = AUR{baseDir}

// Test getting deps from AUR.
func TestGetDeps(t *testing.T) {
	deps := make(map[string]struct{})

	err := a.getDeps([]string{"sway-git"}, deps)
	require.NoError(t, err)
	require.Len(t, deps, 2)
	require.Contains(t, deps, "sway-git")
	require.Contains(t, deps, "wlroots-git")

	deps = make(map[string]struct{})
	err = a.getDeps([]string{"virtualbox-host-modules-mainline"}, deps)
	require.NoError(t, err)
	require.Len(t, deps, 2)

	err = os.RemoveAll(baseDir)
	require.NoError(t, err)
}

// Test Getting sources from AUR.
func TestAURGet(t *testing.T) {
	_, err := a.Get([]string{"sway-git"})
	require.NoError(t, err)

	err = os.RemoveAll(baseDir)
	require.NoError(t, err)
}
<|endoftext|>


<commit_before>package outreach

import (
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"strconv"
)

type Prospect struct {
	Data ProspectData `json: "data"`
}

type ProspectData struct {
	Attributes ProspectAttributes `json: "attributes"`
}

type ProspectAttributes struct {
	Address  ProspectAddress  `json: "address,omitempty"`
	Company  ProspectCompany  `json: "company,omitempty"`
	Contact  ProspectContact  `json: "contact"`
	Personal ProspectPersonal `json: "personal"`
	Social   ProspectSocial   `json: "socila,omitempty"`
	Meta     ProspectMeta     `json: "metadata,omitempty"`
}

type ProspectAddress struct {
	City    string   `json: "city,omitempty"`
	State   string   `json: "state,omitempty"`
	Country string   `json: "country,omitempty"`
	Street  []string `json: "street,omitempty"`
	Zip     string   `json: "zip,omitempty"`
}

type ProspectCompany struct {
	Name     string `json: "name,omitempty"`
	Type     string `json: "type,omitempty"`
	Industry string `json: "industry,omitempty"`
	Size     string `json: "size,omitempty"`
	Locality string `json: "locality,omitempty"`
}

type ProspectContact struct {
	Timezone string        `json: "timezone,omitempty"`
	Email    string        `json: "email"`
	Phone    ProspectPhone `json: "phone,omitempty"`
}

type ProspectPhone struct {
	Personal string `json: "personal,omitempty"`
	Work     string `json: "work,omitempty"`
}

type ProspectPersonal struct {
	Name       ProspectName `json: "name"`
	Gender     string       `json: "gender,omitempty"`
	Occupation string       `json: "occupation,omitempty"`
	Title      string       `json: "title,omitempty"`
}

type ProspectName struct {
	First string `json: "first"`
	Last  string `json: "last"`
}

type ProspectSocial struct {
	Website  string `json: "website,omitempty"`
	Facebook string `json: "facebook,omitempty"`
	LinkedIn string `json: "linkedin,omitempty"`
	Plus     string `json: "plus,omitempty"`
	Quora    string `json: "quora,omitempty"`
	Twitter  string `json: "twitter,omitempty"`
}

type ProspectMeta struct {
	OptOut bool     `json: "opted_out"`
	Source string   `json: "source,omitempty"`
	Notes  []string `json: "notes,omitempty"`
	Tags   []string `json: "tags,omitempty"`
	Custom []string `json: "custom,omitempty"`
}

type ProspectResponse struct {
	Data   ProspectResponseData
	Errors []map[string]interface{}
}

type ProspectResponseData struct {
	ID int
}

type ProspectInstance struct {
	Client *http.Client
}

func (p *Prospect) Read(t []byte) (int, error) {
	j, e := json.Marshal(p)
	if e != nil {
		return 0, e
	}

	if len(t) < len(j) {
		return len(t), nil
	}

	copy(t, j)
	return len(j), io.EOF
}

func (p *Prospect) Close() error {
	return nil
}

func (i *ProspectInstance) Get(id int) (Prospect, error) {
	p := Prospect{}

	if i.Client == nil {
		return p, errors.New("You must assign a HTTP client.")
	}

	resp, e := i.Client.Get("https://api.outreach.io/1.0/prospect/" + strconv.Itoa(id))
	if e != nil {
		return p, e
	}

	body, e := ioutil.ReadAll(resp.Body)
	if e != nil {
		return p, e
	}

	e = json.Unmarshal(body, &p)
	if e != nil {
		return p, e
	}

	return p, nil
}

func (i *ProspectInstance) Post(p Prospect) (ProspectResponse, error) {
	pr := ProspectResponse{}

	if i.Client == nil {
		return pr, errors.New("You must assign a HTTP client.")
	}

	resp, e := i.Client.Post("https://api.outreach.io/1.0/prospect", "application/json", &p)
	if e != nil {
		return pr, e
	}

	body, e := ioutil.ReadAll(resp.Body)
	if e != nil {
		return pr, e
	}

	e = json.Unmarshal(body, &pr)
	if e != nil {
		return pr, e
	}

	if len(pr.Errors) > 0 {
		return pr, fmt.Errorf("Got error response: %+v\n", pr.Errors)
	}

	return pr, nil
}
<commit_msg>Removed spaces in the json tags<commit_after>package outreach

import (
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"strconv"
)

type Prospect struct {
	Data ProspectData `json:"data"`
}

type ProspectData struct {
	Attributes ProspectAttributes `json:"attributes"`
}

type ProspectAttributes struct {
	Address  ProspectAddress  `json:"address,omitempty"`
	Company  ProspectCompany  `json:"company,omitempty"`
	Contact  ProspectContact  `json:"contact"`
	Personal ProspectPersonal `json:"personal"`
	Social   ProspectSocial   `json:"socila,omitempty"`
	Meta     ProspectMeta     `json:"metadata,omitempty"`
}

type ProspectAddress struct {
	City    string   `json:"city,omitempty"`
	State   string   `json:"state,omitempty"`
	Country string   `json:"country,omitempty"`
	Street  []string `json:"street,omitempty"`
	Zip     string   `json:"zip,omitempty"`
}

type ProspectCompany struct {
	Name     string `json:"name,omitempty"`
	Type     string `json:"type,omitempty"`
	Industry string `json:"industry,omitempty"`
	Size     string `json:"size,omitempty"`
	Locality string `json:"locality,omitempty"`
}

type ProspectContact struct {
	Timezone string        `json:"timezone,omitempty"`
	Email    string        `json:"email"`
	Phone    ProspectPhone `json:"phone,omitempty"`
}

type ProspectPhone struct {
	Personal string `json:"personal,omitempty"`
	Work     string `json:"work,omitempty"`
}

type ProspectPersonal struct {
	Name       ProspectName `json:"name"`
	Gender     string       `json:"gender,omitempty"`
	Occupation string       `json:"occupation,omitempty"`
	Title      string       `json:"title,omitempty"`
}

type ProspectName struct {
	First string `json:"first"`
	Last  string `json:"last"`
}

type ProspectSocial struct {
	Website  string `json:"website,omitempty"`
	Facebook string `json:"facebook,omitempty"`
	LinkedIn string `json:"linkedin,omitempty"`
	Plus     string `json:"plus,omitempty"`
	Quora    string `json:"quora,omitempty"`
	Twitter  string `json:"twitter,omitempty"`
}

type ProspectMeta struct {
	OptOut bool     `json:"opted_out"`
	Source string   `json:"source,omitempty"`
	Notes  []string `json:"notes,omitempty"`
	Tags   []string `json:"tags,omitempty"`
	Custom []string `json:"custom,omitempty"`
}

type ProspectResponse struct {
	Data   ProspectResponseData
	Errors []map[string]interface{}
}

type ProspectResponseData struct {
	ID int
}

type ProspectInstance struct {
	Client *http.Client
}

func (p *Prospect) Read(t []byte) (int, error) {
	j, e := json.Marshal(p)
	if e != nil {
		return 0, e
	}

	if len(t) < len(j) {
		return len(t), nil
	}

	copy(t, j)
	return len(j), io.EOF
}

func (p *Prospect) Close() error {
	return nil
}

func (i *ProspectInstance) Get(id int) (Prospect, error) {
	p := Prospect{}

	if i.Client == nil {
		return p, errors.New("You must assign a HTTP client.")
	}

	resp, e := i.Client.Get("https://api.outreach.io/1.0/prospect/" + strconv.Itoa(id))
	if e != nil {
		return p, e
	}

	body, e := ioutil.ReadAll(resp.Body)
	if e != nil {
		return p, e
	}

	e = json.Unmarshal(body, &p)
	if e != nil {
		return p, e
	}

	return p, nil
}

func (i *ProspectInstance) Post(p Prospect) (ProspectResponse, error) {
	pr := ProspectResponse{}

	if i.Client == nil {
		return pr, errors.New("You must assign a HTTP client.")
	}

	resp, e := i.Client.Post("https://api.outreach.io/1.0/prospect", "application/json", &p)
	if e != nil {
		return pr, e
	}

	body, e := ioutil.ReadAll(resp.Body)
	if e != nil {
		return pr, e
	}

	e = json.Unmarshal(body, &pr)
	if e != nil {
		return pr, e
	}

	if len(pr.Errors) > 0 {
		return pr, fmt.Errorf("Got error response: %+v\n", pr.Errors)
	}

	return pr, nil
}
<|endoftext|>


<commit_before>package test

import (
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"time"

	"github.com/fatih/color"
	"github.com/ghodss/yaml"
	"github.com/servehub/serve/manifest"
	"github.com/servehub/utils"
	"github.com/servehub/utils/mergemap"
)

func init() {
	manifest.PluginRegestry.Add("test.component", TestComponent{})
}

type TestComponent struct{}

func (p TestComponent) Run(data manifest.Manifest) error {
	if data.GetString("env") != data.GetString("current-env") {
		log.Printf("No component test found for `%s`.\n", data.GetString("current-env"))
		return nil
	}

	for i, multiComponent := range data.GetArray("components") {
		component := manifest.ParseJSON(data.GetTree("compose.services.component").String())
		merged, _ := mergemap.Merge(component.Unwrap().(map[string]interface{}), multiComponent.Unwrap().(map[string]interface{}))
		name := fmt.Sprintf("component-%d", i+2)

		data.Set("compose.services."+name, merged)
		data.ArrayAppend("compose.services.tests.depends_on", name)
	}

	for _, task := range data.GetArray("pre-hooks") {
		if err := utils.RunCmd(`%s`, task.Unwrap()); err != nil {
			log.Println(color.RedString("%s: %s", task, err))
			return err
		}
	}

	bytes, err := yaml.Marshal(data.GetTree("compose").Unwrap())
	if err != nil {
		return fmt.Errorf("error on serialize yaml: %v", err)
	}

	tmpfile, err := ioutil.TempFile("", "serve-component-test")
	if err != nil {
		return fmt.Errorf("error create tmpfile: %v", err)
	}

	defer func() {
		tmpfile.Close()
		os.Remove(tmpfile.Name())
	}()

	if _, err := tmpfile.Write(bytes); err != nil {
		return fmt.Errorf("error write to tmpfile: %v", err)
	}

	checkFile := data.GetStringOr("check-file-exist", "")
	if checkFile != "" {
		os.Remove(checkFile)
	}

	if err := utils.RunCmd("docker-compose -p %s -f %s pull", data.GetString("name"), tmpfile.Name()); err != nil {
		return fmt.Errorf("error on pull new images for docker-compose: %v", err)
	}

	defer func() {
		utils.RunCmd("docker-compose -p %s -f %s down -v --remove-orphans", data.GetString("name"), tmpfile.Name())
	}()

	timeout, err := time.ParseDuration(data.GetString("timeout"))
	if err != nil {
		return fmt.Errorf("error parse timeout: %v", err)
	}

	go func() {
		select {
		case <-time.After(timeout):
			color.Red("Timeout exceeded for tests, exit...")
			utils.RunCmd("docker-compose -p %s -f %s down -v --remove-orphans", data.GetString("name"), tmpfile.Name())
		}
	}()

	if res := utils.RunCmd("DOCKER_CLIENT_TIMEOUT=300 COMPOSE_HTTP_TIMEOUT=300 docker-compose -p %s -f %s up --abort-on-container-exit", data.GetString("name"), tmpfile.Name()); res != nil {
		return fmt.Errorf("error on running docker-compose with tests: %s", res)
	}

	if checkFile != "" {
		if info, err := os.Stat(checkFile); os.IsNotExist(err) || info.Size() < 16 {
			return fmt.Errorf("check file not exist! %s", checkFile)
		}
	}

	for _, task := range data.GetArray("post-hooks") {
		if err := utils.RunCmd(`%s`, task.Unwrap()); err != nil {
			log.Println(color.RedString("%s: %s", task, err))
			return err
		}
	}

	return nil
}
<commit_msg>text.component: always execute post-hooks<commit_after>package test

import (
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"time"

	"github.com/fatih/color"
	"github.com/ghodss/yaml"
	"github.com/servehub/serve/manifest"
	"github.com/servehub/utils"
	"github.com/servehub/utils/mergemap"
)

func init() {
	manifest.PluginRegestry.Add("test.component", TestComponent{})
}

type TestComponent struct{}

func (p TestComponent) Run(data manifest.Manifest) error {
	if data.GetString("env") != data.GetString("current-env") {
		log.Printf("No component test found for `%s`.\n", data.GetString("current-env"))
		return nil
	}

	for i, multiComponent := range data.GetArray("components") {
		component := manifest.ParseJSON(data.GetTree("compose.services.component").String())
		merged, _ := mergemap.Merge(component.Unwrap().(map[string]interface{}), multiComponent.Unwrap().(map[string]interface{}))
		name := fmt.Sprintf("component-%d", i+2)

		data.Set("compose.services."+name, merged)
		data.ArrayAppend("compose.services.tests.depends_on", name)
	}

	for _, task := range data.GetArray("pre-hooks") {
		if err := utils.RunCmd(`%s`, task.Unwrap()); err != nil {
			log.Println(color.RedString("%s: %s", task, err))
			return err
		}
	}

	bytes, err := yaml.Marshal(data.GetTree("compose").Unwrap())
	if err != nil {
		return fmt.Errorf("error on serialize yaml: %v", err)
	}

	tmpfile, err := ioutil.TempFile("", "serve-component-test")
	if err != nil {
		return fmt.Errorf("error create tmpfile: %v", err)
	}

	defer func() {
		tmpfile.Close()
		os.Remove(tmpfile.Name())
	}()

	if _, err := tmpfile.Write(bytes); err != nil {
		return fmt.Errorf("error write to tmpfile: %v", err)
	}

	checkFile := data.GetStringOr("check-file-exist", "")
	if checkFile != "" {
		os.Remove(checkFile)
	}

	if err := utils.RunCmd("docker-compose -p %s -f %s pull", data.GetString("name"), tmpfile.Name()); err != nil {
		return fmt.Errorf("error on pull new images for docker-compose: %v", err)
	}

	defer func() {
		utils.RunCmd("docker-compose -p %s -f %s down -v --remove-orphans", data.GetString("name"), tmpfile.Name())
	}()

	timeout, err := time.ParseDuration(data.GetString("timeout"))
	if err != nil {
		return fmt.Errorf("error parse timeout: %v", err)
	}

	go func() {
		select {
		case <-time.After(timeout):
			color.Red("Timeout exceeded for tests, exit...")
			utils.RunCmd("docker-compose -p %s -f %s down -v --remove-orphans", data.GetString("name"), tmpfile.Name())
		}
	}()

	if res := utils.RunCmd("DOCKER_CLIENT_TIMEOUT=300 COMPOSE_HTTP_TIMEOUT=300 docker-compose -p %s -f %s up --abort-on-container-exit", data.GetString("name"), tmpfile.Name()); res != nil {
		err = fmt.Errorf("error on running docker-compose with tests: %s", res)
	}

	if checkFile != "" && err == nil {
		if info, err := os.Stat(checkFile); os.IsNotExist(err) || info.Size() < 16 {
			err = fmt.Errorf("check file not exist! %s", checkFile)
		}
	}

	for _, task := range data.GetArray("post-hooks") {
		if err := utils.RunCmd(`%s`, task.Unwrap()); err != nil {
			log.Println(color.RedString("%s: %s", task, err))
			return err
		}
	}

	return err
}
<|endoftext|>


<commit_before>package main

func pushFile(filename string, tag string, description string) {
	println("pushing file....")
}
<commit_msg>read one file for push<commit_after>package main

import (
	"encoding/json"
	"github.com/davecgh/go-spew/spew"
	"io/ioutil"
	"log"
)

type Gist struct {
	Files       map[string]GistFile `json:"files"`
	Public      bool                `json:"public"`
	Description string              `json:"description"`
}

type GistFile struct {
	Content string `json:"content"`
}

func pushFile(filename string, tag string, description string) {
	println("pushing file....")
	files := map[string]GistFile{}

	content, err := ioutil.ReadFile(filename)
	if err != nil {
		log.Fatal("Read file error: ", err)
	}
	files[filename] = GistFile{string(content)}

	gist := Gist{
		files,
		true,
		description,
	}
	data, err := json.Marshal(gist)
	if err != nil {
		log.Fatal("JSON Error: ", err)
	}
	spew.Dump(gist)
	spew.Dump(data)
}
<|endoftext|>


<commit_before>package pygments

import (
	"bytes"
	"log"
	"os/exec"
	"strings"
)

var (
	bin = "pygmentize"
)

func Binary(path string) {
	bin = path
	return
}

func Highlight(code string, lexer string, format string, enc string) string {

	if _, err := exec.LookPath(bin); err != nil {
		log.Fatal("You do not have " + bin + " installed!")
	}

	cmd := exec.Command(bin, "-l"+lexer, "-f"+format, "-O encoding="+enc)
	cmd.Stdin = strings.NewReader(code)

	var out bytes.Buffer
	cmd.Stdout = &out

	var stderr bytes.Buffer
	cmd.Stderr = &stderr

	if err := cmd.Run(); err != nil {
		log.Print(stderr.String())
		log.Fatal(err)
	}

	return out.String()
}
<commit_msg>Added package synopsis<commit_after>// Pygments wrapper for golang. Pygments is a syntax highlighter

package pygments

import (
	"bytes"
	"log"
	"os/exec"
	"strings"
)

var (
	bin = "pygmentize"
)

func Binary(path string) {
	bin = path
	return
}

func Highlight(code string, lexer string, format string, enc string) string {

	if _, err := exec.LookPath(bin); err != nil {
		log.Fatal("You do not have " + bin + " installed!")
	}

	cmd := exec.Command(bin, "-l"+lexer, "-f"+format, "-O encoding="+enc)
	cmd.Stdin = strings.NewReader(code)

	var out bytes.Buffer
	cmd.Stdout = &out

	var stderr bytes.Buffer
	cmd.Stderr = &stderr

	if err := cmd.Run(); err != nil {
		log.Print(stderr.String())
		log.Fatal(err)
	}

	return out.String()
}
<|endoftext|>


<commit_before>// osop
// Copyright (C) 2014-2016 Karol 'Kenji Takahashi' Woźniak
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package main

import (
	"bufio"
	"fmt"
	"net"
	"os"
	"path/filepath"
	"strings"
)

type Bspwm struct {
	connection net.Conn
	reader     *bufio.Reader
}

type bspwmDesktop struct {
	Name  string
	State string
}

type bspwmMonitor struct {
	Name     string
	State    byte
	Index    uint
	Desktops []bspwmDesktop
}

type bspwmResponse struct {
	Monitors []*bspwmMonitor
}

func (b *Bspwm) GetEvented() (interface{}, error) {
	return b.Get()
}

func (b *Bspwm) Get() (interface{}, error) {
	status, err := b.reader.ReadString('\n')
	if err != nil {
		panic(err)
	}

	res := bspwmResponse{}
	var monitor *bspwmMonitor
	var index uint
	for _, piece := range strings.Split(status[1:len(status)], ":") {
		state, value := piece[0], piece[1:len(piece)]
		switch state {
		case 'M', 'm':
			monitor = &bspwmMonitor{Name: value, State: state, Index: index}
			res.Monitors = append(res.Monitors, monitor)
			index += 1
		case 'L':
			// TODO
		case 'O', 'o', 'F', 'f', 'U', 'u':
			monitor.Desktops = append(monitor.Desktops, bspwmDesktop{
				Name:  value,
				State: string(state),
			})
		}
	}
	return res, nil
}

func (b *Bspwm) Init(config config) error {
	socket := os.Getenv("BSPWM_SOCKET")
	if socket == "" {
		sockets, err := filepath.Glob("/tmp/bspwm*")
		if err != nil || len(socket) < 1 {
			return fmt.Errorf("Cannot find socket file")
		}
		socket = sockets[0]
	}

	conn, err := net.Dial("unix", socket)
	if err != nil {
		return fmt.Errorf("Cannot connect to socket: `%s`", err)
	}

	conn.Write([]byte("control\x00--subscribe\x00"))

	b.connection = conn
	b.reader = bufio.NewReader(conn)
	return nil
}

func init() {
	registry.AddReceiver("bspwm", &Bspwm{}, bspwmResponse{})
}
<commit_msg>Update bspwm to support new version (again).<commit_after>// osop
// Copyright (C) 2014-2016 Karol 'Kenji Takahashi' Woźniak
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package main

import (
	"bufio"
	"fmt"
	"net"
	"os"
	"path/filepath"
	"strings"
)

type Bspwm struct {
	connection net.Conn
	reader     *bufio.Reader
}

type bspwmDesktop struct {
	Name  string
	State string
}

type bspwmMonitor struct {
	Name     string
	State    byte
	Index    uint
	Desktops []bspwmDesktop
}

type bspwmResponse struct {
	Monitors []*bspwmMonitor
}

func (b *Bspwm) GetEvented() (interface{}, error) {
	return b.Get()
}

func (b *Bspwm) Get() (interface{}, error) {
	status, err := b.reader.ReadString('\n')
	if err != nil {
		return nil, fmt.Errorf("Cannot read from socket: `%s`", err)
	}

	res := bspwmResponse{}
	var monitor *bspwmMonitor
	var index uint
	for _, piece := range strings.Split(status[1:len(status)], ":") {
		state, value := piece[0], piece[1:len(piece)]
		switch state {
		case 'M', 'm':
			monitor = &bspwmMonitor{Name: value, State: state, Index: index}
			res.Monitors = append(res.Monitors, monitor)
			index += 1
		case 'L':
			// TODO
		case 'O', 'o', 'F', 'f', 'U', 'u':
			monitor.Desktops = append(monitor.Desktops, bspwmDesktop{
				Name:  value,
				State: string(state),
			})
		}
	}
	return res, nil
}

func (b *Bspwm) Init(config config) error {
	socket := os.Getenv("BSPWM_SOCKET")
	if socket == "" {
		sockets, err := filepath.Glob("/tmp/bspwm*")
		if err != nil || len(sockets) < 1 {
			return fmt.Errorf("Cannot find socket file")
		}
		socket = sockets[0]
	}

	conn, err := net.Dial("unix", socket)
	if err != nil {
		return fmt.Errorf("Cannot connect to socket: `%s`", err)
	}

	if _, err = conn.Write([]byte("subscribe\x00report\x00")); err != nil {
		conn.Close()
		return fmt.Errorf("Cannot write to socket: `%s`", err)
	}

	b.connection = conn
	b.reader = bufio.NewReader(conn)
	return nil
}

func init() {
	registry.AddReceiver("bspwm", &Bspwm{}, bspwmResponse{})
}
<|endoftext|>


<commit_before>package aws

import (
	"fmt"
	"log"
	"strconv"
	"time"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/awserr"
	"github.com/aws/aws-sdk-go/service/apigateway"
	"github.com/hashicorp/errwrap"
	"github.com/hashicorp/terraform/helper/resource"
	"github.com/hashicorp/terraform/helper/schema"
)

func resourceAwsApiGatewayRestApi() *schema.Resource {
	return &schema.Resource{
		Create: resourceAwsApiGatewayRestApiCreate,
		Read:   resourceAwsApiGatewayRestApiRead,
		Update: resourceAwsApiGatewayRestApiUpdate,
		Delete: resourceAwsApiGatewayRestApiDelete,

		Schema: map[string]*schema.Schema{
			"name": {
				Type:     schema.TypeString,
				Required: true,
			},

			"description": {
				Type:     schema.TypeString,
				Optional: true,
			},

			"binary_media_types": {
				Type:     schema.TypeList,
				Optional: true,
				Elem:     &schema.Schema{Type: schema.TypeString},
			},

			"body": {
				Type:     schema.TypeString,
				Optional: true,
			},

			"minimum_compression_size": {
				Type:         schema.TypeInt,
				Optional:     true,
				Default:      -1,
				ValidateFunc: validateIntegerInRange(-1, 10485760),
			},

			"root_resource_id": {
				Type:     schema.TypeString,
				Computed: true,
			},

			"created_date": {
				Type:     schema.TypeString,
				Computed: true,
			},

			"endpoint_type": {
				Type:         schema.TypeString,
				Optional:     true,
				Default:      "EDGE",
				ValidateFunc: validateAwsApiGatewayEndpointType,
			},
		},
	}
}

func resourceAwsApiGatewayRestApiCreate(d *schema.ResourceData, meta interface{}) error {
	conn := meta.(*AWSClient).apigateway
	log.Printf("[DEBUG] Creating API Gateway")

	var description *string
	if d.Get("description").(string) != "" {
		description = aws.String(d.Get("description").(string))
	}

	params := &apigateway.CreateRestApiInput{
		Name:        aws.String(d.Get("name").(string)),
		Description: description,
		EndpointConfiguration: &apigateway.EndpointConfiguration{
			Types: []*string{aws.String(d.Get("endpoint_type").(string))},
		},
	}

	binaryMediaTypes, binaryMediaTypesOk := d.GetOk("binary_media_types")
	if binaryMediaTypesOk {
		params.BinaryMediaTypes = expandStringList(binaryMediaTypes.([]interface{}))
	}

	minimumCompressionSize := d.Get("minimum_compression_size").(int)
	if minimumCompressionSize > -1 {
		params.MinimumCompressionSize = aws.Int64(int64(minimumCompressionSize))
	}

	gateway, err := conn.CreateRestApi(params)
	if err != nil {
		return fmt.Errorf("Error creating API Gateway: %s", err)
	}

	d.SetId(*gateway.Id)

	if body, ok := d.GetOk("body"); ok {
		log.Printf("[DEBUG] Initializing API Gateway from OpenAPI spec %s", d.Id())
		_, err := conn.PutRestApi(&apigateway.PutRestApiInput{
			RestApiId: gateway.Id,
			Mode:      aws.String(apigateway.PutModeOverwrite),
			Body:      []byte(body.(string)),
		})
		if err != nil {
			return errwrap.Wrapf("Error creating API Gateway specification: {{err}}", err)
		}
	}

	if err = resourceAwsApiGatewayRestApiRefreshResources(d, meta); err != nil {
		return err
	}

	return resourceAwsApiGatewayRestApiRead(d, meta)
}

func resourceAwsApiGatewayRestApiRefreshResources(d *schema.ResourceData, meta interface{}) error {
	conn := meta.(*AWSClient).apigateway

	resp, err := conn.GetResources(&apigateway.GetResourcesInput{
		RestApiId: aws.String(d.Id()),
	})
	if err != nil {
		return err
	}

	for _, item := range resp.Items {
		if *item.Path == "/" {
			d.Set("root_resource_id", item.Id)
			break
		}
	}

	return nil
}

func resourceAwsApiGatewayRestApiRead(d *schema.ResourceData, meta interface{}) error {
	conn := meta.(*AWSClient).apigateway
	log.Printf("[DEBUG] Reading API Gateway %s", d.Id())

	api, err := conn.GetRestApi(&apigateway.GetRestApiInput{
		RestApiId: aws.String(d.Id()),
	})
	if err != nil {
		if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" {
			log.Printf("[WARN] API Gateway (%s) not found, removing from state", d.Id())
			d.SetId("")
			return nil
		}
		return err
	}

	d.Set("name", api.Name)
	d.Set("description", api.Description)
	d.Set("binary_media_types", api.BinaryMediaTypes)
	if api.MinimumCompressionSize == nil {
		d.Set("minimum_compression_size", -1)
	} else {
		d.Set("minimum_compression_size", api.MinimumCompressionSize)
	}
	if err := d.Set("created_date", api.CreatedDate.Format(time.RFC3339)); err != nil {
		log.Printf("[DEBUG] Error setting created_date: %s", err)
	}

	return nil
}

func resourceAwsApiGatewayRestApiUpdateOperations(d *schema.ResourceData) []*apigateway.PatchOperation {
	operations := make([]*apigateway.PatchOperation, 0)

	if d.HasChange("name") {
		operations = append(operations, &apigateway.PatchOperation{
			Op:    aws.String("replace"),
			Path:  aws.String("/name"),
			Value: aws.String(d.Get("name").(string)),
		})
	}

	if d.HasChange("description") {
		operations = append(operations, &apigateway.PatchOperation{
			Op:    aws.String("replace"),
			Path:  aws.String("/description"),
			Value: aws.String(d.Get("description").(string)),
		})
	}

	if d.HasChange("minimum_compression_size") {
		minimumCompressionSize := d.Get("minimum_compression_size").(int)
		var value string
		if minimumCompressionSize > -1 {
			value = strconv.Itoa(minimumCompressionSize)
		}
		operations = append(operations, &apigateway.PatchOperation{
			Op:    aws.String("replace"),
			Path:  aws.String("/minimumCompressionSize"),
			Value: aws.String(value),
		})
	}

	if d.HasChange("binary_media_types") {
		o, n := d.GetChange("binary_media_types")
		prefix := "binaryMediaTypes"

		old := o.([]interface{})
		new := n.([]interface{})

		// Remove every binary media types. Simpler to remove and add new ones,
		// since there are no replacings.
		for _, v := range old {
			operations = append(operations, &apigateway.PatchOperation{
				Op:   aws.String("remove"),
				Path: aws.String(fmt.Sprintf("/%s/%s", prefix, escapeJsonPointer(v.(string)))),
			})
		}

		// Handle additions
		if len(new) > 0 {
			for _, v := range new {
				operations = append(operations, &apigateway.PatchOperation{
					Op:   aws.String("add"),
					Path: aws.String(fmt.Sprintf("/%s/%s", prefix, escapeJsonPointer(v.(string)))),
				})
			}
		}
	}

	if d.HasChange("endpoint_type") {
		operations = append(operations, &apigateway.PatchOperation{
			Op:    aws.String("replace"),
			Path:  aws.String("/endpointConfiguration/types/0"),
			Value: aws.String(d.Get("endpoint_type").(string)),
		})
	}

	return operations
}

func resourceAwsApiGatewayRestApiUpdate(d *schema.ResourceData, meta interface{}) error {
	conn := meta.(*AWSClient).apigateway
	log.Printf("[DEBUG] Updating API Gateway %s", d.Id())

	if d.HasChange("body") {
		if body, ok := d.GetOk("body"); ok {
			log.Printf("[DEBUG] Updating API Gateway from OpenAPI spec: %s", d.Id())
			_, err := conn.PutRestApi(&apigateway.PutRestApiInput{
				RestApiId: aws.String(d.Id()),
				Mode:      aws.String(apigateway.PutModeOverwrite),
				Body:      []byte(body.(string)),
			})
			if err != nil {
				return errwrap.Wrapf("Error updating API Gateway specification: {{err}}", err)
			}
		}
	}

	_, err := conn.UpdateRestApi(&apigateway.UpdateRestApiInput{
		RestApiId:       aws.String(d.Id()),
		PatchOperations: resourceAwsApiGatewayRestApiUpdateOperations(d),
	})

	if err != nil {
		return err
	}
	log.Printf("[DEBUG] Updated API Gateway %s", d.Id())

	return resourceAwsApiGatewayRestApiRead(d, meta)
}

func resourceAwsApiGatewayRestApiDelete(d *schema.ResourceData, meta interface{}) error {
	conn := meta.(*AWSClient).apigateway
	log.Printf("[DEBUG] Deleting API Gateway: %s", d.Id())

	return resource.Retry(10*time.Minute, func() *resource.RetryError {
		_, err := conn.DeleteRestApi(&apigateway.DeleteRestApiInput{
			RestApiId: aws.String(d.Id()),
		})
		if err == nil {
			return nil
		}

		if apigatewayErr, ok := err.(awserr.Error); ok && apigatewayErr.Code() == "NotFoundException" {
			return nil
		}

		return resource.NonRetryableError(err)
	})
}

func validateAwsApiGatewayEndpointType(v interface{}, k string) (ws []string, errors []error) {
	value := v.(string)
	types := map[string]bool{
		apigateway.EndpointTypeRegional: true,
		apigateway.EndpointTypeEdge:     true,
	}

	if !types[value] {
		errors = append(errors, fmt.Errorf("Endpoint type %s is invalid. Valid types are 'EDGE' and 'REGIONAL'", value))
	}

	return
}
<commit_msg>Minor regional/edge API gateway implementation tweaks.<commit_after>package aws

import (
	"fmt"
	"log"
	"strconv"
	"time"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/awserr"
	"github.com/aws/aws-sdk-go/service/apigateway"
	"github.com/hashicorp/errwrap"
	"github.com/hashicorp/terraform/helper/resource"
	"github.com/hashicorp/terraform/helper/schema"
	"github.com/hashicorp/terraform/helper/validation"
)

func resourceAwsApiGatewayRestApi() *schema.Resource {
	return &schema.Resource{
		Create: resourceAwsApiGatewayRestApiCreate,
		Read:   resourceAwsApiGatewayRestApiRead,
		Update: resourceAwsApiGatewayRestApiUpdate,
		Delete: resourceAwsApiGatewayRestApiDelete,

		Schema: map[string]*schema.Schema{
			"name": {
				Type:     schema.TypeString,
				Required: true,
			},

			"description": {
				Type:     schema.TypeString,
				Optional: true,
			},

			"binary_media_types": {
				Type:     schema.TypeList,
				Optional: true,
				Elem:     &schema.Schema{Type: schema.TypeString},
			},

			"body": {
				Type:     schema.TypeString,
				Optional: true,
			},

			"minimum_compression_size": {
				Type:         schema.TypeInt,
				Optional:     true,
				Default:      -1,
				ValidateFunc: validateIntegerInRange(-1, 10485760),
			},

			"root_resource_id": {
				Type:     schema.TypeString,
				Computed: true,
			},

			"created_date": {
				Type:     schema.TypeString,
				Computed: true,
			},

			"endpoint_type": {
				Type:     schema.TypeString,
				Optional: true,
				Default:  apigateway.EndpointTypeEdge,
				ValidateFunc: validation.StringInSlice([]string{
					apigateway.EndpointTypeEdge,
					apigateway.EndpointTypeRegional,
				}, false),
			},
		},
	}
}

func resourceAwsApiGatewayRestApiCreate(d *schema.ResourceData, meta interface{}) error {
	conn := meta.(*AWSClient).apigateway
	log.Printf("[DEBUG] Creating API Gateway")

	var description *string
	if d.Get("description").(string) != "" {
		description = aws.String(d.Get("description").(string))
	}

	params := &apigateway.CreateRestApiInput{
		Name:        aws.String(d.Get("name").(string)),
		Description: description,
		EndpointConfiguration: &apigateway.EndpointConfiguration{
			Types: []*string{aws.String(d.Get("endpoint_type").(string))},
		},
	}

	binaryMediaTypes, binaryMediaTypesOk := d.GetOk("binary_media_types")
	if binaryMediaTypesOk {
		params.BinaryMediaTypes = expandStringList(binaryMediaTypes.([]interface{}))
	}

	minimumCompressionSize := d.Get("minimum_compression_size").(int)
	if minimumCompressionSize > -1 {
		params.MinimumCompressionSize = aws.Int64(int64(minimumCompressionSize))
	}

	gateway, err := conn.CreateRestApi(params)
	if err != nil {
		return fmt.Errorf("Error creating API Gateway: %s", err)
	}

	d.SetId(*gateway.Id)

	if body, ok := d.GetOk("body"); ok {
		log.Printf("[DEBUG] Initializing API Gateway from OpenAPI spec %s", d.Id())
		_, err := conn.PutRestApi(&apigateway.PutRestApiInput{
			RestApiId: gateway.Id,
			Mode:      aws.String(apigateway.PutModeOverwrite),
			Body:      []byte(body.(string)),
		})
		if err != nil {
			return errwrap.Wrapf("Error creating API Gateway specification: {{err}}", err)
		}
	}

	if err = resourceAwsApiGatewayRestApiRefreshResources(d, meta); err != nil {
		return err
	}

	return resourceAwsApiGatewayRestApiRead(d, meta)
}

func resourceAwsApiGatewayRestApiRefreshResources(d *schema.ResourceData, meta interface{}) error {
	conn := meta.(*AWSClient).apigateway

	resp, err := conn.GetResources(&apigateway.GetResourcesInput{
		RestApiId: aws.String(d.Id()),
	})
	if err != nil {
		return err
	}

	for _, item := range resp.Items {
		if *item.Path == "/" {
			d.Set("root_resource_id", item.Id)
			break
		}
	}

	return nil
}

func resourceAwsApiGatewayRestApiRead(d *schema.ResourceData, meta interface{}) error {
	conn := meta.(*AWSClient).apigateway
	log.Printf("[DEBUG] Reading API Gateway %s", d.Id())

	api, err := conn.GetRestApi(&apigateway.GetRestApiInput{
		RestApiId: aws.String(d.Id()),
	})
	if err != nil {
		if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" {
			log.Printf("[WARN] API Gateway (%s) not found, removing from state", d.Id())
			d.SetId("")
			return nil
		}
		return err
	}

	d.Set("name", api.Name)
	d.Set("description", api.Description)
	d.Set("binary_media_types", api.BinaryMediaTypes)
	if api.MinimumCompressionSize == nil {
		d.Set("minimum_compression_size", -1)
	} else {
		d.Set("minimum_compression_size", api.MinimumCompressionSize)
	}
	if err := d.Set("created_date", api.CreatedDate.Format(time.RFC3339)); err != nil {
		log.Printf("[DEBUG] Error setting created_date: %s", err)
	}

	return nil
}

func resourceAwsApiGatewayRestApiUpdateOperations(d *schema.ResourceData) []*apigateway.PatchOperation {
	operations := make([]*apigateway.PatchOperation, 0)

	if d.HasChange("name") {
		operations = append(operations, &apigateway.PatchOperation{
			Op:    aws.String("replace"),
			Path:  aws.String("/name"),
			Value: aws.String(d.Get("name").(string)),
		})
	}

	if d.HasChange("description") {
		operations = append(operations, &apigateway.PatchOperation{
			Op:    aws.String("replace"),
			Path:  aws.String("/description"),
			Value: aws.String(d.Get("description").(string)),
		})
	}

	if d.HasChange("minimum_compression_size") {
		minimumCompressionSize := d.Get("minimum_compression_size").(int)
		var value string
		if minimumCompressionSize > -1 {
			value = strconv.Itoa(minimumCompressionSize)
		}
		operations = append(operations, &apigateway.PatchOperation{
			Op:    aws.String("replace"),
			Path:  aws.String("/minimumCompressionSize"),
			Value: aws.String(value),
		})
	}

	if d.HasChange("binary_media_types") {
		o, n := d.GetChange("binary_media_types")
		prefix := "binaryMediaTypes"

		old := o.([]interface{})
		new := n.([]interface{})

		// Remove every binary media types. Simpler to remove and add new ones,
		// since there are no replacings.
		for _, v := range old {
			operations = append(operations, &apigateway.PatchOperation{
				Op:   aws.String("remove"),
				Path: aws.String(fmt.Sprintf("/%s/%s", prefix, escapeJsonPointer(v.(string)))),
			})
		}

		// Handle additions
		if len(new) > 0 {
			for _, v := range new {
				operations = append(operations, &apigateway.PatchOperation{
					Op:   aws.String("add"),
					Path: aws.String(fmt.Sprintf("/%s/%s", prefix, escapeJsonPointer(v.(string)))),
				})
			}
		}
	}

	if d.HasChange("endpoint_type") {
		operations = append(operations, &apigateway.PatchOperation{
			Op:    aws.String("replace"),
			Path:  aws.String("/endpointConfiguration/types/0"),
			Value: aws.String(d.Get("endpoint_type").(string)),
		})
	}

	return operations
}

func resourceAwsApiGatewayRestApiUpdate(d *schema.ResourceData, meta interface{}) error {
	conn := meta.(*AWSClient).apigateway
	log.Printf("[DEBUG] Updating API Gateway %s", d.Id())

	if d.HasChange("body") {
		if body, ok := d.GetOk("body"); ok {
			log.Printf("[DEBUG] Updating API Gateway from OpenAPI spec: %s", d.Id())
			_, err := conn.PutRestApi(&apigateway.PutRestApiInput{
				RestApiId: aws.String(d.Id()),
				Mode:      aws.String(apigateway.PutModeOverwrite),
				Body:      []byte(body.(string)),
			})
			if err != nil {
				return errwrap.Wrapf("Error updating API Gateway specification: {{err}}", err)
			}
		}
	}

	_, err := conn.UpdateRestApi(&apigateway.UpdateRestApiInput{
		RestApiId:       aws.String(d.Id()),
		PatchOperations: resourceAwsApiGatewayRestApiUpdateOperations(d),
	})

	if err != nil {
		return err
	}
	log.Printf("[DEBUG] Updated API Gateway %s", d.Id())

	return resourceAwsApiGatewayRestApiRead(d, meta)
}

func resourceAwsApiGatewayRestApiDelete(d *schema.ResourceData, meta interface{}) error {
	conn := meta.(*AWSClient).apigateway
	log.Printf("[DEBUG] Deleting API Gateway: %s", d.Id())

	return resource.Retry(10*time.Minute, func() *resource.RetryError {
		_, err := conn.DeleteRestApi(&apigateway.DeleteRestApiInput{
			RestApiId: aws.String(d.Id()),
		})
		if err == nil {
			return nil
		}

		if apigatewayErr, ok := err.(awserr.Error); ok && apigatewayErr.Code() == "NotFoundException" {
			return nil
		}

		return resource.NonRetryableError(err)
	})
}
<|endoftext|>


<commit_before>package pointers_test

import (
    . "github.com/onsi/ginkgo"
    . "github.com/onsi/gomega"
)

type Dummy struct {
	Value int
}

func ChangeStuff(dolt *Dummy, newValue int) {
	dolt.Value = newValue
}

var _ = Describe("Testing some Pointers", func () {
	var deadmeat []Dummy

	BeforeEach(func () {
		deadmeat = make([]Dummy, 0, 4)
		deadmeat = append(deadmeat, Dummy{Value:90})
		deadmeat = append(deadmeat, Dummy{Value: 91})
		deadmeat = append(deadmeat, Dummy{Value: 92})
		deadmeat = append(deadmeat, Dummy{Value: 93})
		ChangeStuff(&deadmeat[3], 89)
	})

	It("Stuff was changed", func () {
		Expect(deadmeat[3].Value).To(Equal(89))
	})
	It("Allows me to modify a slice member", func () {
		ChangeStuff(&deadmeat[2], 37)
		Expect(deadmeat[2].Value).To(Equal(37))
	})
	// Other pointer related items here
})
<commit_msg>Added a test to verify that receiver arguments follow the same copy rules as other arguments<commit_after>package pointers_test

import (
	"fmt"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

type Dummy struct {
	Value int
}

func ChangeStuff(dolt *Dummy, newValue int) {
	dolt.Value = newValue
}

func (dolt Dummy) ChangeMyStuff(newValue int) {
	dolt.Value = newValue
	// This has no actually observable effect because dolt is a copy which only lives for the
	// lifetime of the invocation
	fmt.Printf("Dolt: %v\n", dolt)
}

var _ = Describe("Testing some Pointers", func() {
	var deadmeat []Dummy

	BeforeEach(func() {
		deadmeat = make([]Dummy, 0, 4)
		deadmeat = append(deadmeat, Dummy{Value: 90})
		deadmeat = append(deadmeat, Dummy{Value: 91})
		deadmeat = append(deadmeat, Dummy{Value: 92})
		deadmeat = append(deadmeat, Dummy{Value: 93})
		ChangeStuff(&deadmeat[3], 89)
	})

	It("Stuff was changed", func() {
		Expect(deadmeat[3].Value).To(Equal(89))
	})
	It("Allows me to modify a slice member", func() {
		ChangeStuff(&deadmeat[2], 37)
		Expect(deadmeat[2].Value).To(Equal(37))
	})
	// Other pointer related items here

	It("Calling a receiver function does creates a copy", func() {
		deadmeat[1].ChangeMyStuff(743)
		Expect(deadmeat[1].Value).To(Equal(91))

	})
})
<|endoftext|>


<commit_before>package qiniu

import (
	"net/url"
	"os"
	"strings"

	"github.com/7sDream/rikka/plugins"
	"qiniupkg.com/api.v7/conf"
	"qiniupkg.com/api.v7/kodo"
)

// Init is the plugin init function, will be called when plugin be load.
func (qnp qiniuPlugin) Init() {
	l.Info("Start plugin qiniu")

	access = os.Getenv(accessEnvKey)
	secret = os.Getenv(secretEnvKey)

	l.Info("Args access =", maskString(access, 5))
	l.Info("Args secret =", maskString(secret, 5))
	l.Info("Args bucket name =", *plugins.ArgBucketName)
	l.Info("Args bucket host =", *plugins.ArgBucketHost)
	l.Info("Args bucket path =", *plugins.ArgBucketPath)

	if access == "" {
		l.Fatal("No Qiniu access key provided， plesae add it into your env var use the name", accessEnvKey)
	}

	if secret == "" {
		l.Fatal("No Qiniu secret key provided, please add it into your env var use the name", secretEnvKey)
	}

	if *plugins.ArgBucketName == "" {
		l.Fatal("No bucket name provided, please add option -bname")
	}

	if *plugins.ArgBucketName == "" {
		l.Fatal("No bucket host provided, please add option -bhost")
	}

	// host process
	bucketAddr = *plugins.ArgBucketHost
	if !strings.HasPrefix(bucketAddr, "http") {
		bucketAddr = "http://" + bucketAddr
	}

	pURL, err := url.Parse(bucketAddr)
	if err != nil {
		l.Fatal("Invalid bucket host", bucketAddr, ":", err)
	}

	bucketAddr = pURL.Scheme + "://" + pURL.Host

	// name
	bucketName = *plugins.ArgBucketName

	// prefix
	bucketPrefix = *plugins.ArgBucketPath
	if strings.HasPrefix(bucketPrefix, "/") {
		bucketPrefix = bucketPrefix[1:]
	}
	if len(bucketPrefix) > 0 && !strings.HasSuffix(bucketPrefix, "/") {
		bucketPrefix = bucketPrefix + "/"
	}

	// set qiniu conf
	conf.ACCESS_KEY = access
	conf.SECRET_KEY = secret
	config := &kodo.Config{
		AccessKey: access,
		SecretKey: secret,
	}
	client = kodo.New(0, config)

	l.Info("Qiniu plugin start successfully")
}
<commit_msg>[Fix][Plugins][Qiniu] MaskString moved to util<commit_after>package qiniu

import (
	"net/url"
	"os"
	"strings"

	"github.com/7sDream/rikka/common/util"
	"github.com/7sDream/rikka/plugins"
	"qiniupkg.com/api.v7/conf"
	"qiniupkg.com/api.v7/kodo"
)

// Init is the plugin init function, will be called when plugin be load.
func (qnp qiniuPlugin) Init() {
	l.Info("Start plugin qiniu")

	access = os.Getenv(accessEnvKey)
	secret = os.Getenv(secretEnvKey)

	l.Info("Args access =", util.MaskString(access, 5))
	l.Info("Args secret =", util.MaskString(secret, 5))
	l.Info("Args bucket name =", *plugins.ArgBucketName)
	l.Info("Args bucket host =", *plugins.ArgBucketHost)
	l.Info("Args bucket path =", *plugins.ArgBucketPath)

	if access == "" {
		l.Fatal("No Qiniu access key provided， plesae add it into your env var use the name", accessEnvKey)
	}

	if secret == "" {
		l.Fatal("No Qiniu secret key provided, please add it into your env var use the name", secretEnvKey)
	}

	if *plugins.ArgBucketName == "" {
		l.Fatal("No bucket name provided, please add option -bname")
	}

	if *plugins.ArgBucketName == "" {
		l.Fatal("No bucket host provided, please add option -bhost")
	}

	// host process
	bucketAddr = *plugins.ArgBucketHost
	if !strings.HasPrefix(bucketAddr, "http") {
		bucketAddr = "http://" + bucketAddr
	}

	pURL, err := url.Parse(bucketAddr)
	if err != nil {
		l.Fatal("Invalid bucket host", bucketAddr, ":", err)
	}

	bucketAddr = pURL.Scheme + "://" + pURL.Host

	// name
	bucketName = *plugins.ArgBucketName

	// prefix
	bucketPrefix = *plugins.ArgBucketPath
	if strings.HasPrefix(bucketPrefix, "/") {
		bucketPrefix = bucketPrefix[1:]
	}
	if len(bucketPrefix) > 0 && !strings.HasSuffix(bucketPrefix, "/") {
		bucketPrefix = bucketPrefix + "/"
	}

	// set qiniu conf
	conf.ACCESS_KEY = access
	conf.SECRET_KEY = secret
	config := &kodo.Config{
		AccessKey: access,
		SecretKey: secret,
	}
	client = kodo.New(0, config)

	l.Info("Qiniu plugin start successfully")
}
<|endoftext|>


<commit_before>package slurp

import (
	"sync"

	"github.com/omeid/slurp/log"
)

type Waiter interface {
	Wait()
}

type Build struct {
	*C
	tasks taskstack

	cleanups []func()
}

func NewBuild() *Build {
	return &Build{C: &C{log.New()}, tasks: make(taskstack)}
}

func (b *Build) Task(name string, deps []string, Task Task) {

	if _, ok := b.tasks[name]; ok {
		b.Fatalf("Duplicate task: %s", name)
	}

	Deps := make(taskstack)
	t := task{name: name, deps: Deps, task: Task}

	for _, dep := range deps {
		d, ok := b.tasks[dep]
		if !ok {
			b.Fatalf("Missing Task %s. Required by Task %s.", dep, name)
		}
		_, ok = d.deps[name]
		if ok {
			b.Fatalf("Circular dependency %s requies %s and around.", d.name, name)
		}

		t.deps[dep] = d
	}

	b.tasks[name] = &t
}

func (b *Build) Run(c *C, tasks ...string) {
	b.Start(c, tasks...).Wait()
}

func (b *Build) Start(c *C, tasks ...string) Waiter {
	var wg sync.WaitGroup

	for _, name := range tasks {
		task, ok := b.tasks[name]
		if !ok {
			b.Printf("No Such Task: %s", task)
			break
		}
		wg.Add(1)
		go func() {
			defer wg.Done()
			err := task.run(c)
			if err != nil {
				b.Println(err)
			}
		}()
	}

	return &wg
}

// Register a function to be called when gulp exists.
func (b *Build) Defer(fn func()) {
	b.cleanups = append(b.cleanups, fn)
}

func (b *Build) Close() {
	for _, cleanup := range b.cleanups {
		cleanup()
	}
}
<commit_msg>Wait wait.<commit_after>package slurp

import (
	"sync"

	"github.com/omeid/slurp/log"
)

type Waiter interface {
	Wait()
}

type Build struct {
	*C
	tasks taskstack

	cleanups []func()
}

func NewBuild() *Build {
	return &Build{C: &C{log.New()}, tasks: make(taskstack)}
}

func (b *Build) Task(name string, deps []string, Task Task) {

	if _, ok := b.tasks[name]; ok {
		b.Fatalf("Duplicate task: %s", name)
	}

	Deps := make(taskstack)
	t := task{name: name, deps: Deps, task: Task}

	for _, dep := range deps {
		d, ok := b.tasks[dep]
		if !ok {
			b.Fatalf("Missing Task %s. Required by Task %s.", dep, name)
		}
		_, ok = d.deps[name]
		if ok {
			b.Fatalf("Circular dependency %s requies %s and around.", d.name, name)
		}

		t.deps[dep] = d
	}

	b.tasks[name] = &t
}

func (b *Build) Run(c *C, tasks ...string) {
	b.Start(c, tasks...).Wait()
}

func (b *Build) Start(c *C, tasks ...string) Waiter {
	var wg sync.WaitGroup

	for _, name := range tasks {
		task, ok := b.tasks[name]
		if !ok {
			b.Printf("No Such Task: %s", task)
			break
		}
		wg.Add(1)
		go func() {
			defer wg.Done()
			err := task.run(c)
			if err != nil {
				b.Println(err)
			}
		}()
	}

	return &wg
}

// Register a function to be called when gulp exists.
func (b *Build) Defer(fn func()) {
	b.cleanups = append(b.cleanups, fn)
}

// Helper function. waits forever.
func (b Build) Wait() {
	<-make(chan struct{})
}

func (b *Build) Close() {
	for _, cleanup := range b.cleanups {
		cleanup()
	}
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"os/exec"
)

func main() {
	buildProtoBuf()
	testCode("bitset")
	testCode("hashtree")
	testCode("pb")
	testCode("pconn")
	testCode("store")
	fmt.Println("\n\ndone all builds and tests")
}

func buildProtoBuf() {
	dir = "pb"
	defer func() { dir = "" }()
	err := doCmd(exec.Command("go", "test", "-v"))
	if err != nil {
		doCmd(exec.Command("protoc", "--gogo_out=.", "*.proto"))
	}
}

func testCode(packageName string) {
	dir = packageName
	defer func() { dir = "" }()
	noErr(doCmd(exec.Command("go", "test", "-v")))
}

func noErr(err error) {
	if err != nil {
		panic(err)
	}
}

var dir = ""

func doCmd(cmd *exec.Cmd) error {
	cmd.Dir = dir
	out, err := cmd.CombinedOutput()
	if err != nil {
		fmt.Println(cmd.Path)
		fmt.Println(cmd.Args)
		fmt.Printf("%s\n", out)
		fmt.Printf("error:%v\n", err)
	}
	return err
}
<commit_msg>make sure go get gets every sub package<commit_after>package main

import (
	"fmt"
	"os/exec"

	"github.com/xiegeo/fensan/bitset"
	"github.com/xiegeo/fensan/hashtree"
	"github.com/xiegeo/fensan/pb"
	"github.com/xiegeo/fensan/pconn"
	"github.com/xiegeo/fensan/store"
)

//make sure go get gets every sub package
var _ = bitset.CHECK_INTEX
var _ = hashtree.HashSize
var _ = &pb.StaticId{}
var _ = pconn.SendBytes
var _ = store.FileNone

func main() {
	buildProtoBuf()
	testCode("bitset")
	testCode("hashtree")
	testCode("pb")
	testCode("pconn")
	testCode("store")
	fmt.Println("\n\ndone all builds and tests")
}

func buildProtoBuf() {
	dir = "pb"
	defer func() { dir = "" }()
	err := doCmd(exec.Command("go", "test", "-v"))
	if err != nil {
		doCmd(exec.Command("protoc", "--gogo_out=.", "*.proto"))
	}
}

func testCode(packageName string) {
	dir = packageName
	defer func() { dir = "" }()
	noErr(doCmd(exec.Command("go", "test", "-v")))
}

func noErr(err error) {
	if err != nil {
		panic(err)
	}
}

var dir = ""

func doCmd(cmd *exec.Cmd) error {
	cmd.Dir = dir
	out, err := cmd.CombinedOutput()
	if err != nil {
		fmt.Println(cmd.Path)
		fmt.Println(cmd.Args)
		fmt.Printf("%s\n", out)
		fmt.Printf("error:%v\n", err)
	}
	return err
}
<|endoftext|>


<commit_before>package httpmaster

import (
	"fmt"

	"github.com/mesos/mesos-go/api/v1/lib/client"
	"github.com/mesos/mesos-go/api/v1/lib/httpcli"
	"github.com/mesos/mesos-go/api/v1/lib/master"
)

func classifyResponse(c *master.Call) (rc client.ResponseClass, err error) {
	if c == nil {
		err = httpcli.ProtocolError("nil master.Call not allowed")
		return
	}

	switch t := c.GetType(); t {
	// singleton
	case master.Call_GET_HEALTH,
		master.Call_GET_FLAGS,
		master.Call_GET_VERSION,
		master.Call_GET_METRICS,
		master.Call_GET_LOGGING_LEVEL,
		master.Call_LIST_FILES,
		master.Call_READ_FILE,
		master.Call_GET_STATE,
		master.Call_GET_AGENTS,
		master.Call_GET_FRAMEWORKS,
		master.Call_GET_EXECUTORS,
		master.Call_GET_TASKS,
		master.Call_GET_ROLES,
		master.Call_GET_WEIGHTS,
		master.Call_GET_MASTER,
		master.Call_GET_MAINTENANCE_STATUS,
		master.Call_GET_MAINTENANCE_SCHEDULE,
		master.Call_GET_QUOTA:
		rc = client.ResponseClassSingleton

	// streaming
	case master.Call_SUBSCRIBE:
		rc = client.ResponseClassStreaming

	// no-data
	case master.Call_SET_LOGGING_LEVEL,
		master.Call_UPDATE_WEIGHTS,
		master.Call_RESERVE_RESOURCES,
		master.Call_UNRESERVE_RESOURCES,
		master.Call_CREATE_VOLUMES,
		master.Call_DESTROY_VOLUMES,
		master.Call_UPDATE_MAINTENANCE_SCHEDULE,
		master.Call_START_MAINTENANCE,
		master.Call_STOP_MAINTENANCE,
		master.Call_SET_QUOTA,
		master.Call_REMOVE_QUOTA:
		rc = client.ResponseClassAuto

	default:
		err = httpcli.ProtocolError(fmt.Sprintf("unsupported master.Call type: %v", t))
	}
	return
}
<commit_msg>httpmaster: SUBSCRIBE should use ResponseClassAuto according to mesos API docs<commit_after>package httpmaster

import (
	"fmt"

	"github.com/mesos/mesos-go/api/v1/lib/client"
	"github.com/mesos/mesos-go/api/v1/lib/httpcli"
	"github.com/mesos/mesos-go/api/v1/lib/master"
)

func classifyResponse(c *master.Call) (rc client.ResponseClass, err error) {
	if c == nil {
		err = httpcli.ProtocolError("nil master.Call not allowed")
		return
	}

	switch t := c.GetType(); t {
	// singleton
	case master.Call_GET_HEALTH,
		master.Call_GET_FLAGS,
		master.Call_GET_VERSION,
		master.Call_GET_METRICS,
		master.Call_GET_LOGGING_LEVEL,
		master.Call_LIST_FILES,
		master.Call_READ_FILE,
		master.Call_GET_STATE,
		master.Call_GET_AGENTS,
		master.Call_GET_FRAMEWORKS,
		master.Call_GET_EXECUTORS,
		master.Call_GET_TASKS,
		master.Call_GET_ROLES,
		master.Call_GET_WEIGHTS,
		master.Call_GET_MASTER,
		master.Call_GET_MAINTENANCE_STATUS,
		master.Call_GET_MAINTENANCE_SCHEDULE,
		master.Call_GET_QUOTA:
		rc = client.ResponseClassSingleton

	// streaming
	case master.Call_SUBSCRIBE:
		// for some reason, the docs say that thr format is recordio (streaming) but HTTP negotiation
		// uses application/json or application/x-protobuf; use "auto" class, similar to sched/exec API
		rc = client.ResponseClassAuto

	// no-data
	case master.Call_SET_LOGGING_LEVEL,
		master.Call_UPDATE_WEIGHTS,
		master.Call_RESERVE_RESOURCES,
		master.Call_UNRESERVE_RESOURCES,
		master.Call_CREATE_VOLUMES,
		master.Call_DESTROY_VOLUMES,
		master.Call_UPDATE_MAINTENANCE_SCHEDULE,
		master.Call_START_MAINTENANCE,
		master.Call_STOP_MAINTENANCE,
		master.Call_SET_QUOTA,
		master.Call_REMOVE_QUOTA:
		rc = client.ResponseClassAuto

	default:
		err = httpcli.ProtocolError(fmt.Sprintf("unsupported master.Call type: %v", t))
	}
	return
}
<|endoftext|>


<commit_before>package integration

import (
	"os"
	"testing"
	"time"

	xtime "github.com/m3db/m3x/time"
	"github.com/stretchr/testify/require"
)

type readWriteTZCase struct {
	namespace  string
	id         string
	datapoints []readWriteTZDP
}

type readWriteTZDP struct {
	value     float64
	timestamp time.Time
}

// Make sure that everything works properly end-to-end even if the client issues
// a write in a timezone other than that of the server.
func TestWriteReadTimezone(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}

	// Ensure that the test is running with the local timezone set to US/Pacific.
	// Note that we do this instead of just manipulating the NowFn on the test
	// setup because there are ways to end up with a time object with a non-UTC
	// timezone besides calling time.Now(). For example, converting a unix
	// timestamp into a time.Time object will automatically associate the machine's
	// local timezone.
	os.Setenv("TZ", "US/Pacific")
	name, offset := time.Now().Zone()
	// The zone name will be PST or PDT depending on whether daylight savings
	// is currently in effect
	if name == "PDT" {
		require.Equal(t, offset, -25200)
	} else if name == "PST" {
		require.Equal(t, offset, -28800)
	} else {
		t.Fatalf("Zone should be PDT or PST, but was: %s", name)
	}

	// Load locations that we'll need later in the tests
	pacificLocation, err := time.LoadLocation("US/Pacific")
	require.NoError(t, err)
	nyLocation, err := time.LoadLocation("America/New_York")
	require.NoError(t, err)

	// Setup / start server
	opts := newTestOptions(t)
	setup, err := newTestSetup(t, opts)
	require.NoError(t, err)
	defer setup.close()
	require.NoError(t, setup.startServer())
	require.NoError(t, setup.waitUntilServerIsUp())

	// Make sure that the server's internal clock function returns pacific timezone
	start := setup.getNowFn()
	setup.setNowFn(start.In(pacificLocation))

	// Instantiate a client
	client := setup.m3dbClient
	session, err := client.DefaultSession()
	require.NoError(t, err)
	defer session.Close()

	// Generate test datapoints (all with NY timezone)
	namespace := opts.Namespaces()[0].ID().String()
	startNy := start.In(nyLocation)
	writeSeries := []readWriteTZCase{
		readWriteTZCase{
			namespace: namespace,
			id:        "some-id-1",
			datapoints: []readWriteTZDP{
				readWriteTZDP{
					value:     20.0,
					timestamp: startNy,
				},
				readWriteTZDP{
					value:     20.0,
					timestamp: startNy.Add(1 * time.Second),
				},
				readWriteTZDP{
					value:     20.0,
					timestamp: startNy.Add(2 * time.Second),
				},
			},
		},
		readWriteTZCase{
			namespace: namespace,
			id:        "some-id-2",
			datapoints: []readWriteTZDP{
				readWriteTZDP{
					value:     30.0,
					timestamp: startNy,
				},
				readWriteTZDP{
					value:     30.0,
					timestamp: startNy.Add(1 * time.Second),
				},
				readWriteTZDP{
					value:     30.0,
					timestamp: startNy.Add(2 * time.Second),
				},
			},
		},
	}

	// Write datapoints
	for _, series := range writeSeries {
		for _, write := range series.datapoints {
			err = session.Write(series.namespace, series.id, write.timestamp, write.value, xtime.Second, nil)
			require.NoError(t, err)
		}
	}

	// Read datapoints back
	iters, err := session.FetchAll(namespace, []string{"some-id-1", "some-id-2"}, startNy, startNy.Add(1*time.Hour))
	require.NoError(t, err)

	// Assert datapoints match what we wrote
	for i, iter := range iters.Iters() {
		for j := 0; iter.Next(); j++ {
			dp, _, _ := iter.Current()
			expectedDatapoint := writeSeries[i].datapoints[j]
			// Datapoints will comeback with the timezone set to the local timezone
			// of the machine that the client is running on. The Equal() method ensures
			// that the two time.Time struct's refer to the same instant in time
			require.True(t, expectedDatapoint.timestamp.Equal(dp.Timestamp))
			require.Equal(t, expectedDatapoint.value, dp.Value)
		}
	}
}
<commit_msg>Add build tag and open source legal language to integration test (#386)<commit_after>// +build integration

// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package integration

import (
	"os"
	"testing"
	"time"

	xtime "github.com/m3db/m3x/time"
	"github.com/stretchr/testify/require"
)

type readWriteTZCase struct {
	namespace  string
	id         string
	datapoints []readWriteTZDP
}

type readWriteTZDP struct {
	value     float64
	timestamp time.Time
}

// Make sure that everything works properly end-to-end even if the client issues
// a write in a timezone other than that of the server.
func TestWriteReadTimezone(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}

	// Ensure that the test is running with the local timezone set to US/Pacific.
	// Note that we do this instead of just manipulating the NowFn on the test
	// setup because there are ways to end up with a time object with a non-UTC
	// timezone besides calling time.Now(). For example, converting a unix
	// timestamp into a time.Time object will automatically associate the machine's
	// local timezone.
	os.Setenv("TZ", "US/Pacific")
	name, offset := time.Now().Zone()
	// The zone name will be PST or PDT depending on whether daylight savings
	// is currently in effect
	if name == "PDT" {
		require.Equal(t, offset, -25200)
	} else if name == "PST" {
		require.Equal(t, offset, -28800)
	} else {
		t.Fatalf("Zone should be PDT or PST, but was: %s", name)
	}

	// Load locations that we'll need later in the tests
	pacificLocation, err := time.LoadLocation("US/Pacific")
	require.NoError(t, err)
	nyLocation, err := time.LoadLocation("America/New_York")
	require.NoError(t, err)

	// Setup / start server
	opts := newTestOptions(t)
	setup, err := newTestSetup(t, opts)
	require.NoError(t, err)
	defer setup.close()
	require.NoError(t, setup.startServer())
	require.NoError(t, setup.waitUntilServerIsUp())

	// Make sure that the server's internal clock function returns pacific timezone
	start := setup.getNowFn()
	setup.setNowFn(start.In(pacificLocation))

	// Instantiate a client
	client := setup.m3dbClient
	session, err := client.DefaultSession()
	require.NoError(t, err)
	defer session.Close()

	// Generate test datapoints (all with NY timezone)
	namespace := opts.Namespaces()[0].ID().String()
	startNy := start.In(nyLocation)
	writeSeries := []readWriteTZCase{
		readWriteTZCase{
			namespace: namespace,
			id:        "some-id-1",
			datapoints: []readWriteTZDP{
				readWriteTZDP{
					value:     20.0,
					timestamp: startNy,
				},
				readWriteTZDP{
					value:     20.0,
					timestamp: startNy.Add(1 * time.Second),
				},
				readWriteTZDP{
					value:     20.0,
					timestamp: startNy.Add(2 * time.Second),
				},
			},
		},
		readWriteTZCase{
			namespace: namespace,
			id:        "some-id-2",
			datapoints: []readWriteTZDP{
				readWriteTZDP{
					value:     30.0,
					timestamp: startNy,
				},
				readWriteTZDP{
					value:     30.0,
					timestamp: startNy.Add(1 * time.Second),
				},
				readWriteTZDP{
					value:     30.0,
					timestamp: startNy.Add(2 * time.Second),
				},
			},
		},
	}

	// Write datapoints
	for _, series := range writeSeries {
		for _, write := range series.datapoints {
			err = session.Write(series.namespace, series.id, write.timestamp, write.value, xtime.Second, nil)
			require.NoError(t, err)
		}
	}

	// Read datapoints back
	iters, err := session.FetchAll(namespace, []string{"some-id-1", "some-id-2"}, startNy, startNy.Add(1*time.Hour))
	require.NoError(t, err)

	// Assert datapoints match what we wrote
	for i, iter := range iters.Iters() {
		for j := 0; iter.Next(); j++ {
			dp, _, _ := iter.Current()
			expectedDatapoint := writeSeries[i].datapoints[j]
			// Datapoints will comeback with the timezone set to the local timezone
			// of the machine that the client is running on. The Equal() method ensures
			// that the two time.Time struct's refer to the same instant in time
			require.True(t, expectedDatapoint.timestamp.Equal(dp.Timestamp))
			require.Equal(t, expectedDatapoint.value, dp.Value)
		}
	}
}
<|endoftext|>