<commit_before>package contractmanager

import (
	"time"

	"github.com/NebulousLabs/Sia/build"
	"github.com/NebulousLabs/Sia/persist"
)

const (
	// logFile is the name of the file that is used for logging in the contract
	// manager.
	logFile = "contractmanager.log"

	// metadataFile is the name of the file that stores all of the sector
	// metadata associated with a storage folder.
	metadataFile = "siahostmetadata.dat"

	// sectorFile is the file that is placed inside of a storage folder to
	// house all of the sectors associated with a storage folder.
	sectorFile = "siahostdata.dat"

	// settingsFile is the name of the file that is used to save the contract
	// manager's settings.
	settingsFile = "contractmanager.json"

	// settingsFileTmp is the name of the file that is used to hold unfinished
	// writes to the contract manager's settings. After this file is completed,
	// a copy-on-write operation is performed to make sure that the contract
	// manager's persistent settings are updated atomically.
	settingsFileTmp = "contractmanager.json_temp"

	// walFile is the name of the file that is used to save the write ahead log
	// for the contract manager.
	walFile = "contractmanager.wal"

	// walFileTmp is used for incomplete writes to the WAL. Data could be
	// interrupted by power outages, etc., and is therefore written to a
	// temporary file before being atomically renamed to the correct name.
	walFileTmp = "contractmanager.wal_temp"
)

const (
	// folderAllocationStepSize is the amount of data that gets allocated at a
	// time when writing out the sparse sector file during a storageFolderAdd or
	// a storageFolderGrow.
	folderAllocationStepSize = 1 << 35

	// maxSectorBatchThreads is the maximum number of threads updating
	// sector counters on disk in AddSectorBatch and RemoveSectorBatch.
	maxSectorBatchThreads = 100

	// sectorMetadataDiskSize defines the number of bytes it takes to store the
	// metadata of a single sector on disk.
	sectorMetadataDiskSize = 14

	// storageFolderGranularity defines the number of sectors that a storage
	// folder must cleanly divide into. 64 sectors is a requirement due to the
	// way the storage folder bitfield (field 'Usage') is constructed - the
	// bitfield defines which sectors are available, and the bitfield must be
	// constructed 1 uint64 at a time (8 bytes, 64 bits, or 64 sectors).
	//
	// This corresponds to a granularity of 256 MiB on the production network,
	// which is a high granluarity relative the to the TiBs of storage that
	// hosts are expected to provide.
	storageFolderGranularity = 64
)

var (
	// settingsMetadata is the header that is used when writing the contract
	// manager's settings to disk.
	settingsMetadata = persist.Metadata{
		Header:  "Sia Contract Manager",
		Version: "1.2.0",
	}

	// walMetadata is the header that is used when writing the write ahead log
	// to disk, so that it may be identified at startup.
	walMetadata = persist.Metadata{
		Header:  "Sia Contract Manager WAL",
		Version: "1.2.0",
	}
)

var (
	// MaximumSectorsPerStorageFolder sets an upper bound on how large storage
	// folders in the host are allowed to be. There is a hard limit at 4
	// billion sectors because the sector location map only uses 4 bytes to
	// indicate the location of a sector.
	MaximumSectorsPerStorageFolder = build.Select(build.Var{
		Dev:      uint64(1 << 20), // 4 TiB
		Standard: uint64(1 << 32), // 16 PiB
		Testing:  uint64(1 << 12), // 16 GiB
	}).(uint64)

	// maximumStorageFolders defines the maximum number of storage folders that
	// the host can support.
	maximumStorageFolders = build.Select(build.Var{
		Dev:      uint64(1 << 5),
		Standard: uint64(1 << 16),
		Testing:  uint64(1 << 3),
	}).(uint64)

	// MinimumSectorsPerStorageFolder defines the minimum number of sectors
	// that a storage folder is allowed to have.
	MinimumSectorsPerStorageFolder = build.Select(build.Var{
		Dev:      uint64(1 << 6), // 16 MiB
		Standard: uint64(1 << 6), // 512 MiB
		Testing:  uint64(1 << 6), // 256 KiB
	}).(uint64)
)

var (
	// folderRecheckInitialInterval specifies the amount of time that the
	// contract manager will initially wait when checking to see if an
	// unavailable storage folder has become available.
	folderRecheckInitialInterval = build.Select(build.Var{
		Dev:      time.Second,
		Standard: time.Second * 5,
		Testing:  time.Second,
	}).(time.Duration)

	// maxFolderRecheckInterval specifies the maximum amount of time that the
	// contract manager will wait between checking if an unavailable storage
	// folder has become available.
	maxFolderRecheckInterval = build.Select(build.Var{
		Dev:      time.Second * 30,
		Standard: time.Second * 60 * 5,
		Testing:  time.Second * 8,
	}).(time.Duration)
)
<commit_msg>Change comment based on different sector sizes as defined in negotiate.go Change MinimumSectorsPerStorageFolder comments<commit_after>package contractmanager

import (
	"time"

	"github.com/NebulousLabs/Sia/build"
	"github.com/NebulousLabs/Sia/persist"
)

const (
	// logFile is the name of the file that is used for logging in the contract
	// manager.
	logFile = "contractmanager.log"

	// metadataFile is the name of the file that stores all of the sector
	// metadata associated with a storage folder.
	metadataFile = "siahostmetadata.dat"

	// sectorFile is the file that is placed inside of a storage folder to
	// house all of the sectors associated with a storage folder.
	sectorFile = "siahostdata.dat"

	// settingsFile is the name of the file that is used to save the contract
	// manager's settings.
	settingsFile = "contractmanager.json"

	// settingsFileTmp is the name of the file that is used to hold unfinished
	// writes to the contract manager's settings. After this file is completed,
	// a copy-on-write operation is performed to make sure that the contract
	// manager's persistent settings are updated atomically.
	settingsFileTmp = "contractmanager.json_temp"

	// walFile is the name of the file that is used to save the write ahead log
	// for the contract manager.
	walFile = "contractmanager.wal"

	// walFileTmp is used for incomplete writes to the WAL. Data could be
	// interrupted by power outages, etc., and is therefore written to a
	// temporary file before being atomically renamed to the correct name.
	walFileTmp = "contractmanager.wal_temp"
)

const (
	// folderAllocationStepSize is the amount of data that gets allocated at a
	// time when writing out the sparse sector file during a storageFolderAdd or
	// a storageFolderGrow.
	folderAllocationStepSize = 1 << 35

	// maxSectorBatchThreads is the maximum number of threads updating
	// sector counters on disk in AddSectorBatch and RemoveSectorBatch.
	maxSectorBatchThreads = 100

	// sectorMetadataDiskSize defines the number of bytes it takes to store the
	// metadata of a single sector on disk.
	sectorMetadataDiskSize = 14

	// storageFolderGranularity defines the number of sectors that a storage
	// folder must cleanly divide into. 64 sectors is a requirement due to the
	// way the storage folder bitfield (field 'Usage') is constructed - the
	// bitfield defines which sectors are available, and the bitfield must be
	// constructed 1 uint64 at a time (8 bytes, 64 bits, or 64 sectors).
	//
	// This corresponds to a granularity of 256 MiB on the production network,
	// which is a high granluarity relative the to the TiBs of storage that
	// hosts are expected to provide.
	storageFolderGranularity = 64
)

var (
	// settingsMetadata is the header that is used when writing the contract
	// manager's settings to disk.
	settingsMetadata = persist.Metadata{
		Header:  "Sia Contract Manager",
		Version: "1.2.0",
	}

	// walMetadata is the header that is used when writing the write ahead log
	// to disk, so that it may be identified at startup.
	walMetadata = persist.Metadata{
		Header:  "Sia Contract Manager WAL",
		Version: "1.2.0",
	}
)

var (
	// MaximumSectorsPerStorageFolder sets an upper bound on how large storage
	// folders in the host are allowed to be. There is a hard limit at 4
	// billion sectors because the sector location map only uses 4 bytes to
	// indicate the location of a sector.
	MaximumSectorsPerStorageFolder = build.Select(build.Var{
		Dev:      uint64(1 << 20), // 256 GiB
		Standard: uint64(1 << 32), // 16 PiB
		Testing:  uint64(1 << 12), // 16 MiB
	}).(uint64)

	// maximumStorageFolders defines the maximum number of storage folders that
	// the host can support.
	maximumStorageFolders = build.Select(build.Var{
		Dev:      uint64(1 << 5),
		Standard: uint64(1 << 16),
		Testing:  uint64(1 << 3),
	}).(uint64)

	// MinimumSectorsPerStorageFolder defines the minimum number of sectors
	// that a storage folder is allowed to have.
	MinimumSectorsPerStorageFolder = build.Select(build.Var{
		Dev:      uint64(1 << 6), // 16 MiB
		Standard: uint64(1 << 6), // 256 MiB
		Testing:  uint64(1 << 6), // 256 KiB
	}).(uint64)
)

var (
	// folderRecheckInitialInterval specifies the amount of time that the
	// contract manager will initially wait when checking to see if an
	// unavailable storage folder has become available.
	folderRecheckInitialInterval = build.Select(build.Var{
		Dev:      time.Second,
		Standard: time.Second * 5,
		Testing:  time.Second,
	}).(time.Duration)

	// maxFolderRecheckInterval specifies the maximum amount of time that the
	// contract manager will wait between checking if an unavailable storage
	// folder has become available.
	maxFolderRecheckInterval = build.Select(build.Var{
		Dev:      time.Second * 30,
		Standard: time.Second * 60 * 5,
		Testing:  time.Second * 8,
	}).(time.Duration)
)
<|endoftext|>


<commit_before>package bip32

import (
	"crypto/elliptic"
	"github.com/mndrix/btcutil"
	"math/big"
)

// Create the standard bitcoin elliptic curve
var curve elliptic.Curve = btcutil.Secp256k1()

const (
	// We use compressed public keys so their length is 33; not 65
	PublicKeyCompressedLength = 33
)

type Key []byte

func (key *Key) ToPublicKey() Key {
	return SerializePublicKey(curve.ScalarBaseMult([]byte(*key)))
}

func SerializePublicKey(x *big.Int, y *big.Int) Key {
	// Create empty key
	key := make(Key, 0, PublicKeyCompressedLength)

	// Add header; 2 if Y is even; 3 if it's odd
	header := byte(0x2)
	if y.Bit(0) == 1 {
		header++
	}
	key = append(key, header)

	// Get bytes of X-value
	xBytes := x.Bytes()

	// Pad the key so x is aligned with the LSB. Pad size is key length - header size (1) - xBytes size
	padLength := PublicKeyCompressedLength - 1 - len(xBytes)
	for i := 0; i < padLength; i++ {
		key = append(key, 0)
	}

	// Finally append the x value
	key = append(key, xBytes...)

	return key
}
<commit_msg>use byte buffer to write serialized public key<commit_after>package bip32

import (
	"crypto/elliptic"
	"github.com/mndrix/btcutil"
	"math/big"
  "bytes"
)

// Create the standard bitcoin elliptic curve
var curve elliptic.Curve = btcutil.Secp256k1()

const (
	// We use compressed public keys so their length is 33; not 65
	PublicKeyCompressedLength = 33
)

type Key []byte

func (key *Key) ToPublicKey() Key {
	return SerializePublicKey(curve.ScalarBaseMult([]byte(*key)))
}

func SerializePublicKey(x *big.Int, y *big.Int) Key {
  var key bytes.Buffer

  // Write header; 0x2 for even y value; 0x3 for odd
  header := byte(0x2)
  if y.Bit(0) == 1 {
    header = byte(0x3)
  }
  key.WriteByte(header)

  // Get bytes of X-value
  xBytes := x.Bytes()

  // Pad the key so x is aligned with the LSB. Pad size is key length - header size (1) - xBytes size
  for i := 0; i < (PublicKeyCompressedLength - 1 - len(xBytes)); i++ {
    key.WriteByte(0x0)
  }

  key.Write(xBytes)

  return Key(key.Bytes())
}
<|endoftext|>


<commit_before>// Copyright 2015, Joe Tsai. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE.md file.

// +build ignore

// Generates huffman.txt. This test file heavily favors prefix based encodings
// since some symbols are heavily favored over others. This leads to compression
// savings that can be gained by assigning shorter prefix codes to those more
// frequent symbols. The number of symbols used is large enough such that it
// avoids LZ77 dictionary matches.
package main

import "io/ioutil"
import "math/rand"
import "unicode/utf8"

const (
	name = "huffman.txt"
	size = 1 << 18
)

const (
	alpha1 = "abcdefghijklmnopqrstuvwxyz"
	alpha2 = alpha1 + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	alpha3 = alpha2 + "0123456789" + "+/"
)

func main() {
	var b []byte
	var r = rand.New(rand.NewSource(0))

	for len(b) < size {
		n := 16 + r.Int()%64 // Length of substring
		p := r.Float32()
		switch {
		case p <= 0.75:
			// Write strings of base64 encoded values.
			for i := 0; i < n; i++ {
				p := r.Float32()
				switch {
				case p < 0.1:
					// Write any lowercase letter.
					b = append(b, alpha1[r.Int()%len(alpha1)])
				case p < 0.7:
					// Write any lowercase or uppercase letter.
					b = append(b, alpha2[r.Int()%len(alpha2)])
				default:
					// Write any character from the base64 alphabet.
					b = append(b, alpha3[r.Int()%len(alpha3)])
				}
			}
		case p <= 1.00:
			// Write strings of utf8 encoded values.
			for i := 0; i < n; i++ {
				p := r.Float32()
				switch {
				case p <= 0.65:
					// Write a 2-byte long utf8 code point.
					var buf [4]byte
					cnt := utf8.EncodeRune(buf[:], rune(0x80+r.Int()%0x780))
					b = append(b, buf[:cnt]...)
				case p <= 1.00:
					// Write a 3-byte long utf8 code point.
					var buf [4]byte
					cnt := utf8.EncodeRune(buf[:], rune(0x800+r.Int()%0xF800))
					b = append(b, buf[:cnt]...)
				}
			}
		}
	}

	if err := ioutil.WriteFile(name, b[:size], 0664); err != nil {
		panic(err)
	}
}
<commit_msg>internal: trivial consistency fix for huffman.go<commit_after>// Copyright 2015, Joe Tsai. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE.md file.

// +build ignore

// Generates huffman.txt. This test file heavily favors prefix based encodings
// since some symbols are heavily favored over others. This leads to compression
// savings that can be gained by assigning shorter prefix codes to those more
// frequent symbols. The number of symbols used is large enough such that it
// avoids LZ77 dictionary matches.
package main

import "io/ioutil"
import "math/rand"
import "unicode/utf8"

const (
	name = "huffman.txt"
	size = 1 << 18
)

const (
	alpha1 = "abcdefghijklmnopqrstuvwxyz"
	alpha2 = alpha1 + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	alpha3 = alpha2 + "0123456789" + "+/"
)

func main() {
	var b []byte
	var r = rand.New(rand.NewSource(0))

	for len(b) < size {
		n := 16 + r.Int()%64 // Length of substring
		p := r.Float32()
		switch {
		case p <= 0.75:
			// Write strings of base64 encoded values.
			for i := 0; i < n; i++ {
				p := r.Float32()
				switch {
				case p <= 0.1:
					// Write any lowercase letter.
					b = append(b, alpha1[r.Int()%len(alpha1)])
				case p <= 0.7:
					// Write any lowercase or uppercase letter.
					b = append(b, alpha2[r.Int()%len(alpha2)])
				case p <= 1.0:
					// Write any character from the base64 alphabet.
					b = append(b, alpha3[r.Int()%len(alpha3)])
				}
			}
		case p <= 1.00:
			// Write strings of utf8 encoded values.
			for i := 0; i < n; i++ {
				p := r.Float32()
				switch {
				case p <= 0.65:
					// Write a 2-byte long utf8 code point.
					var buf [4]byte
					cnt := utf8.EncodeRune(buf[:], rune(0x80+r.Int()%0x780))
					b = append(b, buf[:cnt]...)
				case p <= 1.00:
					// Write a 3-byte long utf8 code point.
					var buf [4]byte
					cnt := utf8.EncodeRune(buf[:], rune(0x800+r.Int()%0xF800))
					b = append(b, buf[:cnt]...)
				}
			}
		}
	}

	if err := ioutil.WriteFile(name, b[:size], 0664); err != nil {
		panic(err)
	}
}
<|endoftext|>


<commit_before>// Copyright 2016 The Gosl 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 tsr

import (
	"math"
	"testing"

	"github.com/cpmech/gosl/chk"
	"github.com/cpmech/gosl/io"
	"github.com/cpmech/gosl/la"
	"github.com/cpmech/gosl/num"
	"github.com/cpmech/gosl/utl"
)

func Test_smpinvs01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("smpinvs01")

	a, b, β, ϵ := -1.0, 0.5, 1e-3, 1e-3

	L := []float64{-8.0, -8.0, -8.0}
	N := make([]float64, 3)
	n := make([]float64, 3)
	m := SmpDirector(N, L, a, b, β, ϵ)
	SmpUnitDirector(n, m, N)
	io.Pforan("L = %v\n", L)
	io.Pforan("N = %v\n", N)
	io.Pforan("m = %v\n", m)
	io.Pforan("n = %v\n", n)
	chk.Vector(tst, "n", 1e-15, n, []float64{a / SQ3, a / SQ3, a / SQ3})

	p, q, err := GenInvs(L, n, a)
	if err != nil {
		chk.Panic("GenInvs failed:\n%v", err.Error())
	}
	io.Pforan("p = %v\n", p)
	io.Pforan("q = %v\n", q)
	if q < 0.0 || q > GENINVSQEPS {
		chk.Panic("q=%g is incorrect", q)
	}
	if math.Abs(p-a*L[0]) > 1e-14 {
		chk.Panic("p=%g is incorrect. err = %g", p, math.Abs(p-a*L[0]))
	}
}

func Test_smpinvs02(tst *testing.T) {

	//verbose()
	chk.PrintTitle("smpinvs02")

	// coefficients for smp invariants
	smp_a := -1.0
	smp_b := 0.5
	smp_β := 1e-1 // derivative values become too high with
	smp_ϵ := 1e-1 // small β and ϵ @ zero

	// constants for checking derivatives
	dver := chk.Verbose
	dtol := 1e-9
	dtol2 := 1e-8

	// run tests
	nd := test_nd
	for idxA := 0; idxA < len(test_nd); idxA++ {
		//for idxA := 0; idxA < 1; idxA++ {
		//for idxA := 10; idxA < 11; idxA++ {

		// tensor and eigenvalues
		A := test_AA[idxA]
		a := M_Alloc2(nd[idxA])
		Ten2Man(a, A)
		L := make([]float64, 3)
		M_EigenValsNum(L, a)

		// SMP director
		N := make([]float64, 3)
		n := make([]float64, 3)
		m := SmpDirector(N, L, smp_a, smp_b, smp_β, smp_ϵ)
		SmpUnitDirector(n, m, N)

		// output
		io.PfYel("\n\ntst # %d ###################################################################################\n", idxA)
		io.Pforan("L = %v\n", L)
		io.Pforan("N = %v\n", N)
		io.Pforan("m = %v\n", m)
		io.Pfpink("n = %v\n", n)
		chk.Vector(tst, "L", 1e-12, L, test_λ[idxA])
		chk.Scalar(tst, "norm(n)==1", 1e-15, la.VecNorm(n), 1)
		chk.Scalar(tst, "m=norm(N)", 1e-14, m, la.VecNorm(N))

		// dN/dL
		var tmp float64
		N_tmp := make([]float64, 3)
		dNdL := make([]float64, 3)
		SmpDirectorDeriv1(dNdL, L, smp_a, smp_b, smp_β, smp_ϵ)
		io.Pfpink("\ndNdL = %v\n", dNdL)
		for i := 0; i < 3; i++ {
			dnum, _ := num.DerivCentral(func(x float64, args ...interface{}) (res float64) {
				tmp, L[i] = L[i], x
				SmpDirector(N_tmp, L, smp_a, smp_b, smp_β, smp_ϵ)
				L[i] = tmp
				return N_tmp[i]
			}, L[i], 1e-6)
			chk.AnaNum(tst, io.Sf("dN/dL[%d][%d]", i, i), dtol, dNdL[i], dnum, dver)
		}

		// dm/dL
		n_tmp := make([]float64, 3)
		dmdL := make([]float64, 3)
		SmpNormDirectorDeriv1(dmdL, m, N, dNdL)
		io.Pfpink("\ndmdL = %v\n", dmdL)
		for j := 0; j < 3; j++ {
			dnum, _ := num.DerivCentral(func(x float64, args ...interface{}) (res float64) {
				tmp, L[j] = L[j], x
				m_tmp := SmpDirector(N_tmp, L, smp_a, smp_b, smp_β, smp_ϵ)
				L[j] = tmp
				return m_tmp
			}, L[j], 1e-6)
			chk.AnaNum(tst, io.Sf("dm/dL[%d]", j), dtol, dmdL[j], dnum, dver)
		}

		// dn/dL
		dndL := la.MatAlloc(3, 3)
		SmpUnitDirectorDeriv1(dndL, m, N, dNdL, dmdL)
		io.Pfpink("\ndndL = %v\n", dndL)
		for i := 0; i < 3; i++ {
			for j := 0; j < 3; j++ {
				dnum, _ := num.DerivCentral(func(x float64, args ...interface{}) (res float64) {
					tmp, L[j] = L[j], x
					m_tmp := SmpDirector(N_tmp, L, smp_a, smp_b, smp_β, smp_ϵ)
					SmpUnitDirector(n_tmp, m_tmp, N_tmp)
					L[j] = tmp
					return n_tmp[i]
				}, L[j], 1e-6)
				chk.AnaNum(tst, io.Sf("dn/dL[%d][%d]", i, j), dtol, dndL[i][j], dnum, dver)
			}
		}

		// change tolerance
		dtol2_tmp := dtol2
		if idxA == 10 || idxA == 11 {
			dtol2 = 1e-6
		}

		// d²m/dLdL
		dNdL_tmp := make([]float64, 3)
		dmdL_tmp := make([]float64, 3)
		d2NdL2 := make([]float64, 3)
		d2mdLdL := la.MatAlloc(3, 3)
		SmpDirectorDeriv2(d2NdL2, L, smp_a, smp_b, smp_β, smp_ϵ)
		SmpNormDirectorDeriv2(d2mdLdL, L, smp_a, smp_b, smp_β, smp_ϵ, m, N, dNdL, d2NdL2, dmdL)
		io.Pfpink("\nd2mdLdL = %v\n", d2mdLdL)
		for i := 0; i < 3; i++ {
			for j := 0; j < 3; j++ {
				dnum, _ := num.DerivCentral(func(x float64, args ...interface{}) (res float64) {
					tmp, L[j] = L[j], x
					m_tmp := SmpDirector(N_tmp, L, smp_a, smp_b, smp_β, smp_ϵ)
					SmpDirectorDeriv1(dNdL_tmp, L, smp_a, smp_b, smp_β, smp_ϵ)
					SmpNormDirectorDeriv1(dmdL_tmp, m_tmp, N_tmp, dNdL_tmp)
					L[j] = tmp
					return dmdL_tmp[i]
				}, L[j], 1e-6)
				chk.AnaNum(tst, io.Sf("d2m/dL[%d]dL[%d]", i, j), dtol2, d2mdLdL[i][j], dnum, dver)
			}
		}

		// d²N/dLdL
		io.Pfpink("\nd²N/dLdL\n")
		for i := 0; i < 3; i++ {
			dnum, _ := num.DerivCentral(func(x float64, args ...interface{}) (res float64) {
				tmp, L[i] = L[i], x
				SmpDirectorDeriv1(dNdL_tmp, L, smp_a, smp_b, smp_β, smp_ϵ)
				L[i] = tmp
				return dNdL_tmp[i]
			}, L[i], 1e-6)
			chk.AnaNum(tst, io.Sf("d²N[%d]/dL[%d]dL[%d]", i, i, i), dtol2, d2NdL2[i], dnum, dver)
		}

		// d²n/dLdL
		io.Pfpink("\nd²n/dLdL\n")
		dndL_tmp := la.MatAlloc(3, 3)
		d2ndLdL := utl.Deep3alloc(3, 3, 3)
		SmpUnitDirectorDeriv2(d2ndLdL, m, N, dNdL, d2NdL2, dmdL, n, d2mdLdL, dndL)
		for i := 0; i < 3; i++ {
			for j := 0; j < 3; j++ {
				for k := 0; k < 3; k++ {
					dnum, _ := num.DerivCentral(func(x float64, args ...interface{}) (res float64) {
						tmp, L[k] = L[k], x
						m_tmp := SmpDirector(N_tmp, L, smp_a, smp_b, smp_β, smp_ϵ)
						SmpDirectorDeriv1(dNdL_tmp, L, smp_a, smp_b, smp_β, smp_ϵ)
						SmpNormDirectorDeriv1(dmdL_tmp, m_tmp, N_tmp, dNdL_tmp)
						SmpUnitDirectorDeriv1(dndL_tmp, m_tmp, N_tmp, dNdL_tmp, dmdL_tmp)
						L[k] = tmp
						return dndL_tmp[i][j]
					}, L[k], 1e-6)
					chk.AnaNum(tst, io.Sf("d²n[%d]/dL[%d]dL[%d]", i, j, k), dtol2, d2ndLdL[i][j][k], dnum, dver)
				}
			}
		}

		// recover tolerance
		dtol2 = dtol2_tmp

		// SMP derivs
		//if false {
		if true {
			io.Pfpink("\nSMP derivs\n")
			dndL_ := la.MatAlloc(3, 3)
			dNdL_ := make([]float64, 3)
			d2ndLdL_ := utl.Deep3alloc(3, 3, 3)
			N_ := make([]float64, 3)
			F_ := make([]float64, 3)
			G_ := make([]float64, 3)
			m_ := SmpDerivs1(dndL_, dNdL_, N_, F_, G_, L, smp_a, smp_b, smp_β, smp_ϵ)
			SmpDerivs2(d2ndLdL_, L, smp_a, smp_b, smp_β, smp_ϵ, m_, N_, F_, G_, dNdL_, dndL_)
			chk.Scalar(tst, "m_", 1e-14, m_, m)
			chk.Vector(tst, "N_", 1e-15, N_, N)
			chk.Vector(tst, "dNdL_", 1e-15, dNdL_, dNdL)
			chk.Matrix(tst, "dndL_", 1e-13, dndL_, dndL)
			chk.Deep3(tst, "d2ndLdL_", 1e-11, d2ndLdL_, d2ndLdL)
		}
	}
}
<commit_msg>Use chk.Deriv in t_smpinvs_test<commit_after>// Copyright 2016 The Gosl 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 tsr

import (
	"math"
	"testing"

	"github.com/cpmech/gosl/chk"
	"github.com/cpmech/gosl/io"
	"github.com/cpmech/gosl/la"
	"github.com/cpmech/gosl/utl"
)

func Test_smpinvs01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("smpinvs01")

	a, b, β, ϵ := -1.0, 0.5, 1e-3, 1e-3

	L := []float64{-8.0, -8.0, -8.0}
	N := make([]float64, 3)
	n := make([]float64, 3)
	m := SmpDirector(N, L, a, b, β, ϵ)
	SmpUnitDirector(n, m, N)
	io.Pforan("L = %v\n", L)
	io.Pforan("N = %v\n", N)
	io.Pforan("m = %v\n", m)
	io.Pforan("n = %v\n", n)
	chk.Vector(tst, "n", 1e-15, n, []float64{a / SQ3, a / SQ3, a / SQ3})

	p, q, err := GenInvs(L, n, a)
	if err != nil {
		chk.Panic("GenInvs failed:\n%v", err.Error())
	}
	io.Pforan("p = %v\n", p)
	io.Pforan("q = %v\n", q)
	if q < 0.0 || q > GENINVSQEPS {
		chk.Panic("q=%g is incorrect", q)
	}
	if math.Abs(p-a*L[0]) > 1e-14 {
		chk.Panic("p=%g is incorrect. err = %g", p, math.Abs(p-a*L[0]))
	}
}

func Test_smpinvs02(tst *testing.T) {

	//verbose()
	chk.PrintTitle("smpinvs02")

	// coefficients for smp invariants
	smp_a := -1.0
	smp_b := 0.5
	smp_β := 1e-1 // derivative values become too high with
	smp_ϵ := 1e-1 // small β and ϵ @ zero

	// constants for checking derivatives
	dver := chk.Verbose
	dtol := 1e-9
	dtol2 := 1e-8

	// run tests
	nd := test_nd
	for idxA := 0; idxA < len(test_nd); idxA++ {
		//for idxA := 0; idxA < 1; idxA++ {
		//for idxA := 10; idxA < 11; idxA++ {

		// tensor and eigenvalues
		A := test_AA[idxA]
		a := M_Alloc2(nd[idxA])
		Ten2Man(a, A)
		L := make([]float64, 3)
		M_EigenValsNum(L, a)

		// SMP director
		N := make([]float64, 3)
		n := make([]float64, 3)
		m := SmpDirector(N, L, smp_a, smp_b, smp_β, smp_ϵ)
		SmpUnitDirector(n, m, N)

		// output
		io.PfYel("\n\ntst # %d ###################################################################################\n", idxA)
		io.Pforan("L = %v\n", L)
		io.Pforan("N = %v\n", N)
		io.Pforan("m = %v\n", m)
		io.Pfpink("n = %v\n", n)
		chk.Vector(tst, "L", 1e-12, L, test_λ[idxA])
		chk.Scalar(tst, "norm(n)==1", 1e-15, la.VecNorm(n), 1)
		chk.Scalar(tst, "m=norm(N)", 1e-14, m, la.VecNorm(N))

		// dN/dL
		var tmp float64
		N_tmp := make([]float64, 3)
		dNdL := make([]float64, 3)
		SmpDirectorDeriv1(dNdL, L, smp_a, smp_b, smp_β, smp_ϵ)
		io.Pfpink("\ndNdL = %v\n", dNdL)
		for i := 0; i < 3; i++ {
			chk.DerivScaSca(tst, io.Sf("dN%d/dL%d", i, i), dtol, dNdL[i], L[i], 1e-1, dver, func(x float64) (float64, error) {
				tmp, L[i] = L[i], x
				SmpDirector(N_tmp, L, smp_a, smp_b, smp_β, smp_ϵ)
				L[i] = tmp
				return N_tmp[i], nil
			})
		}

		// dm/dL
		dmdL := make([]float64, 3)
		SmpNormDirectorDeriv1(dmdL, m, N, dNdL)
		io.Pfpink("\ndmdL = %v\n", dmdL)
		chk.DerivScaVec(tst, "dm/dL", dtol, dmdL, L, 1e-1, dver, func(x []float64) (float64, error) {
			return SmpDirector(N_tmp, x, smp_a, smp_b, smp_β, smp_ϵ), nil
		})

		// dn/dL
		dndL := la.MatAlloc(3, 3)
		SmpUnitDirectorDeriv1(dndL, m, N, dNdL, dmdL)
		io.Pfpink("\ndndL = %v\n", dndL)
		chk.DerivVecVec(tst, "dn/dL", dtol, dndL, L, 1e-1, dver, func(f, x []float64) error {
			m_tmp := SmpDirector(N_tmp, x, smp_a, smp_b, smp_β, smp_ϵ)
			SmpUnitDirector(f, m_tmp, N_tmp) // f := n
			return nil
		})

		// change tolerance
		dtol2_tmp := dtol2
		if idxA == 10 || idxA == 11 {
			dtol2 = 1e-6
		}

		// d²m/dLdL
		dNdL_tmp := make([]float64, 3)
		dmdL_tmp := make([]float64, 3)
		d2NdL2 := make([]float64, 3)
		d2mdLdL := la.MatAlloc(3, 3)
		SmpDirectorDeriv2(d2NdL2, L, smp_a, smp_b, smp_β, smp_ϵ)
		SmpNormDirectorDeriv2(d2mdLdL, L, smp_a, smp_b, smp_β, smp_ϵ, m, N, dNdL, d2NdL2, dmdL)
		io.Pfpink("\nd2mdLdL = %v\n", d2mdLdL)
		chk.DerivVecVec(tst, "d2m/dLdL", dtol2, d2mdLdL, L, 1e-6, dver, func(f, x []float64) error {
			m_tmp := SmpDirector(N_tmp, x, smp_a, smp_b, smp_β, smp_ϵ)
			SmpDirectorDeriv1(dNdL_tmp, x, smp_a, smp_b, smp_β, smp_ϵ)
			SmpNormDirectorDeriv1(f, m_tmp, N_tmp, dNdL_tmp) // f := dmdL
			return nil
		})

		// d²N/dLdL
		io.Pfpink("\nd²N/dLdL\n")
		for i := 0; i < 3; i++ {
			chk.DerivScaSca(tst, io.Sf("d²N%d/dL%ddL%d", i, i, i), dtol2, d2NdL2[i], L[i], 1e-6, dver, func(x float64) (float64, error) {
				tmp, L[i] = L[i], x
				SmpDirectorDeriv1(dNdL_tmp, L, smp_a, smp_b, smp_β, smp_ϵ)
				L[i] = tmp
				return dNdL_tmp[i], nil
			})
		}

		// d²n/dLdL
		io.Pfpink("\nd²n/dLdL\n")
		dndL_tmp := la.MatAlloc(3, 3)
		d2ndLdL := utl.Deep3alloc(3, 3, 3)
		SmpUnitDirectorDeriv2(d2ndLdL, m, N, dNdL, d2NdL2, dmdL, n, d2mdLdL, dndL)
		for i := 0; i < 3; i++ {
			chk.DerivVecVec(tst, io.Sf("d²n%d/dLdL", i), dtol2, d2ndLdL[i], L, 1e-6, dver, func(f, x []float64) error {
				m_tmp := SmpDirector(N_tmp, x, smp_a, smp_b, smp_β, smp_ϵ)
				SmpDirectorDeriv1(dNdL_tmp, x, smp_a, smp_b, smp_β, smp_ϵ)
				SmpNormDirectorDeriv1(dmdL_tmp, m_tmp, N_tmp, dNdL_tmp)
				SmpUnitDirectorDeriv1(dndL_tmp, m_tmp, N_tmp, dNdL_tmp, dmdL_tmp)
				copy(f, dndL_tmp[i])
				return nil
			})
		}

		// recover tolerance
		dtol2 = dtol2_tmp

		// SMP derivs
		//if false {
		if true {
			io.Pfpink("\nSMP derivs\n")
			dndL_ := la.MatAlloc(3, 3)
			dNdL_ := make([]float64, 3)
			d2ndLdL_ := utl.Deep3alloc(3, 3, 3)
			N_ := make([]float64, 3)
			F_ := make([]float64, 3)
			G_ := make([]float64, 3)
			m_ := SmpDerivs1(dndL_, dNdL_, N_, F_, G_, L, smp_a, smp_b, smp_β, smp_ϵ)
			SmpDerivs2(d2ndLdL_, L, smp_a, smp_b, smp_β, smp_ϵ, m_, N_, F_, G_, dNdL_, dndL_)
			chk.Scalar(tst, "m_", 1e-14, m_, m)
			chk.Vector(tst, "N_", 1e-15, N_, N)
			chk.Vector(tst, "dNdL_", 1e-15, dNdL_, dNdL)
			chk.Matrix(tst, "dndL_", 1e-13, dndL_, dndL)
			chk.Deep3(tst, "d2ndLdL_", 1e-11, d2ndLdL_, d2ndLdL)
		}
	}
}
<|endoftext|>


<commit_before>package lex

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
)

type TokenType int

const EOF rune = -1
const TokError TokenType = -1

type pos struct {
	line int
	col  int
	pos  int
}

func (p *pos) CopyTo(to *pos) {
	to.line = p.line
	to.col = p.col
	to.pos = p.pos
}

func (p *pos) Advance(r rune) {
	p.col++
	p.pos++
	if r == '\n' {
		p.line++
		p.col = 0
	}
}

type Token struct {
	Type  TokenType
	Value string
	Line  int
	Col   int
	Pos   int
	Extra interface{}
}

func (tok *Token) String() string {
	return fmt.Sprintf("%d:%d(%d): token=%d %q",
		tok.Line, tok.Col, tok.Pos, tok.Type, tok.Value)
}

type StateFn func(*Lexer) StateFn

type Lexer struct {
	r      *bufio.Reader
	tokens chan Token
	eof    bool
	// currently bufferred value
	value []rune
	// Position in the stream
	pos      pos
	prevPos  pos
	tokenPos pos
}

func NewLexer(r io.Reader) *Lexer {
	return &Lexer{
		r:      bufio.NewReader(r),
		tokens: make(chan Token, 0),
	}
}

func NewLexerString(s string) *Lexer {
	return NewLexer(bytes.NewBufferString(s))
}

func (lex *Lexer) Next() rune {
	if r, _, err := lex.r.ReadRune(); err != nil {
		if err != io.EOF {
			lex.Errorf(TokError, err.Error())
		}
		lex.value = append(lex.value, r)
		lex.eof = true
		return EOF
	} else {
		lex.pos.CopyTo(&lex.prevPos)
		lex.pos.Advance(r)
		lex.value = append(lex.value, r)
		return r
	}
}

func (lex *Lexer) Peek() rune {
	r := lex.Next()
	lex.Backup()
	return r
}

func (lex *Lexer) Backup() {
	lex.r.UnreadRune()
	lex.value = lex.value[0 : len(lex.value)-1]
	lex.prevPos.CopyTo(&lex.pos)
}

// Line() returns current line number in the reader
func (lex *Lexer) Line() int {
	return lex.pos.line
}

// Line() returns current column number in the current line of reader
func (lex *Lexer) Col() int {
	return lex.pos.col
}

// Line() returns current position in the reader (in runes)
func (lex *Lexer) Pos() int {
	return lex.pos.pos
}

// Value() returns currently buffered token value
func (lex *Lexer) Value() string {
	return string(lex.value)
}

// Ignore() removes currently buffered token value
func (lex *Lexer) Ignore() {
	lex.pos.CopyTo(&lex.tokenPos)
	lex.value = []rune{}
}

func (lex *Lexer) Emit(t TokenType) {
	lex.tokens <- Token{t, lex.Value(), lex.tokenPos.line, lex.tokenPos.col, lex.tokenPos.pos}
	lex.pos.CopyTo(&lex.tokenPos)
	lex.value = []rune{}
}

func (lex *Lexer) Errorf(t TokenType, s string, args ...interface{}) StateFn {
	value := fmt.Sprintf(s, args...)
	lex.tokens <- Token{t, value, lex.Line(), lex.Col(), lex.Pos()}
	return nil
}

func (lex *Lexer) Run(start StateFn) <-chan Token {
	go func() {
		for state := start; state != nil && !lex.eof; {
			state = state(lex)
		}
		close(lex.tokens)
	}()
	return lex.tokens
}
<commit_msg>fixed struct initializers<commit_after>package lex

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
)

type TokenType int

const EOF rune = -1
const TokError TokenType = -1

type pos struct {
	line int
	col  int
	pos  int
}

func (p *pos) CopyTo(to *pos) {
	to.line = p.line
	to.col = p.col
	to.pos = p.pos
}

func (p *pos) Advance(r rune) {
	p.col++
	p.pos++
	if r == '\n' {
		p.line++
		p.col = 0
	}
}

type Token struct {
	Type  TokenType
	Value string
	Line  int
	Col   int
	Pos   int
	Extra interface{}
}

func (tok *Token) String() string {
	return fmt.Sprintf("%d:%d(%d): token=%d %q",
		tok.Line, tok.Col, tok.Pos, tok.Type, tok.Value)
}

type StateFn func(*Lexer) StateFn

type Lexer struct {
	r      *bufio.Reader
	tokens chan Token
	eof    bool
	// currently bufferred value
	value []rune
	// Position in the stream
	pos      pos
	prevPos  pos
	tokenPos pos
}

func NewLexer(r io.Reader) *Lexer {
	return &Lexer{
		r:      bufio.NewReader(r),
		tokens: make(chan Token, 0),
	}
}

func NewLexerString(s string) *Lexer {
	return NewLexer(bytes.NewBufferString(s))
}

func (lex *Lexer) Next() rune {
	if r, _, err := lex.r.ReadRune(); err != nil {
		if err != io.EOF {
			lex.Errorf(TokError, err.Error())
		}
		lex.value = append(lex.value, r)
		lex.eof = true
		return EOF
	} else {
		lex.pos.CopyTo(&lex.prevPos)
		lex.pos.Advance(r)
		lex.value = append(lex.value, r)
		return r
	}
}

func (lex *Lexer) Peek() rune {
	r := lex.Next()
	lex.Backup()
	return r
}

func (lex *Lexer) Backup() {
	lex.r.UnreadRune()
	lex.value = lex.value[0 : len(lex.value)-1]
	lex.prevPos.CopyTo(&lex.pos)
}

// Line() returns current line number in the reader
func (lex *Lexer) Line() int {
	return lex.pos.line
}

// Line() returns current column number in the current line of reader
func (lex *Lexer) Col() int {
	return lex.pos.col
}

// Line() returns current position in the reader (in runes)
func (lex *Lexer) Pos() int {
	return lex.pos.pos
}

// Value() returns currently buffered token value
func (lex *Lexer) Value() string {
	return string(lex.value)
}

// Ignore() removes currently buffered token value
func (lex *Lexer) Ignore() {
	lex.pos.CopyTo(&lex.tokenPos)
	lex.value = []rune{}
}

func (lex *Lexer) Emit(t TokenType) {
	lex.tokens <- Token{t, lex.Value(), lex.tokenPos.line, lex.tokenPos.col, lex.tokenPos.pos, nil}
	lex.pos.CopyTo(&lex.tokenPos)
	lex.value = []rune{}
}

func (lex *Lexer) Errorf(t TokenType, s string, args ...interface{}) StateFn {
	value := fmt.Sprintf(s, args...)
	lex.tokens <- Token{t, value, lex.Line(), lex.Col(), lex.Pos(), nil}
	return nil
}

func (lex *Lexer) Run(start StateFn) <-chan Token {
	go func() {
		for state := start; state != nil && !lex.eof; {
			state = state(lex)
		}
		close(lex.tokens)
	}()
	return lex.tokens
}
<|endoftext|>


<commit_before>package oci

import (
	"os"
	"path/filepath"
	"strings"

	"github.com/mrunalp/ocid/utils"
)

// New creates a new Runtime with options provided
func New(runtimePath string, containerDir string) (*Runtime, error) {
	r := &Runtime{
		name:         filepath.Base(runtimePath),
		path:         runtimePath,
		containerDir: containerDir,
	}
	return r, nil
}

type Runtime struct {
	name         string
	path         string
	sandboxDir   string
	containerDir string
}

// Name returns the name of the OCI Runtime
func (r *Runtime) Name() string {
	return r.name
}

// Path returns the full path the OCI Runtime executable
func (r *Runtime) Path() string {
	return r.path
}

// ContainerDir returns the path to the base directory for storing container configurations
func (r *Runtime) ContainerDir() string {
	return r.containerDir
}

// Version returns the version of the OCI Runtime
func (r *Runtime) Version() (string, error) {
	runtimeVersion, err := getOCIVersion(r.path, "-v")
	if err != nil {
		return "", err
	}
	return runtimeVersion, nil
}

func getOCIVersion(name string, args ...string) (string, error) {
	out, err := utils.ExecCmd(name, args...)
	if err != nil {
		return "", err
	}

	firstLine := out[:strings.Index(out, "\n")]
	v := firstLine[strings.LastIndex(firstLine, " ")+1:]
	return v, nil
}

// CreateContainer creates a container.
func (r *Runtime) CreateContainer(c *Container) error {
	return utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, r.path, "create", "--bundle", c.bundlePath, c.name)
}

// Container respresents a runtime container.
type Container struct {
	name       string
	bundlePath string
	logPath    string
	labels     map[string]string
	sandbox    string
}

func NewContainer(name string, bundlePath string, logPath string, labels map[string]string, sandbox string) (*Container, error) {
	c := &Container{
		name:       name,
		bundlePath: bundlePath,
		logPath:    logPath,
		labels:     labels,
		sandbox:    sandbox,
	}
	return c, nil
}

// Name returns the name of the container.
func (c *Container) Name() string {
	return c.name
}

// BundlePath returns the bundlePath of the container.
func (c *Container) BundlePath() string {
	return c.bundlePath
}

// LogPath returns the log path of the container.
func (c *Container) LogPath() string {
	return c.logPath
}

// Labels returns the labels of the container.
func (c *Container) Labels() map[string]string {
	return c.labels
}

// Sandbox returns the sandbox name of the container.
func (c *Container) Sandbox() string {
	return c.sandbox
}
<commit_msg>Add helper functions to start/stop a container<commit_after>package oci

import (
	"os"
	"path/filepath"
	"strings"

	"github.com/mrunalp/ocid/utils"
)

// New creates a new Runtime with options provided
func New(runtimePath string, containerDir string) (*Runtime, error) {
	r := &Runtime{
		name:         filepath.Base(runtimePath),
		path:         runtimePath,
		containerDir: containerDir,
	}
	return r, nil
}

// Runtime stores the information about a oci runtime
type Runtime struct {
	name         string
	path         string
	sandboxDir   string
	containerDir string
}

// Name returns the name of the OCI Runtime
func (r *Runtime) Name() string {
	return r.name
}

// Path returns the full path the OCI Runtime executable
func (r *Runtime) Path() string {
	return r.path
}

// ContainerDir returns the path to the base directory for storing container configurations
func (r *Runtime) ContainerDir() string {
	return r.containerDir
}

// Version returns the version of the OCI Runtime
func (r *Runtime) Version() (string, error) {
	runtimeVersion, err := getOCIVersion(r.path, "-v")
	if err != nil {
		return "", err
	}
	return runtimeVersion, nil
}

func getOCIVersion(name string, args ...string) (string, error) {
	out, err := utils.ExecCmd(name, args...)
	if err != nil {
		return "", err
	}

	firstLine := out[:strings.Index(out, "\n")]
	v := firstLine[strings.LastIndex(firstLine, " ")+1:]
	return v, nil
}

// CreateContainer creates a container.
func (r *Runtime) CreateContainer(c *Container) error {
	return utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, r.path, "--systemd-cgroup", "create", "--bundle", c.bundlePath, c.name)
}

// StartContainer starts a container.
func (r *Runtime) StartContainer(c *Container) error {
	return utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, r.path, "start", c.name)
}

// StopContainer stops a container.
func (r *Runtime) StopContainer(c *Container) error {
	// TODO: Check if it is still running after some time and send SIGKILL
	return utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, r.path, "kill", c.name)
}

// Container respresents a runtime container.
type Container struct {
	name       string
	bundlePath string
	logPath    string
	labels     map[string]string
	sandbox    string
}

// NewContainer creates a container object.
func NewContainer(name string, bundlePath string, logPath string, labels map[string]string, sandbox string) (*Container, error) {
	c := &Container{
		name:       name,
		bundlePath: bundlePath,
		logPath:    logPath,
		labels:     labels,
		sandbox:    sandbox,
	}
	return c, nil
}

// Name returns the name of the container.
func (c *Container) Name() string {
	return c.name
}

// BundlePath returns the bundlePath of the container.
func (c *Container) BundlePath() string {
	return c.bundlePath
}

// LogPath returns the log path of the container.
func (c *Container) LogPath() string {
	return c.logPath
}

// Labels returns the labels of the container.
func (c *Container) Labels() map[string]string {
	return c.labels
}

// Sandbox returns the sandbox name of the container.
func (c *Container) Sandbox() string {
	return c.sandbox
}
<|endoftext|>


<commit_before>package options

import (
	"errors"
	"flag"
	"fmt"
	"log"
	"os"
	"reflect"
	"regexp"
	"strconv"
	"strings"
	"time"
)

func Resolve(options interface{}, flagSet *flag.FlagSet, cfg map[string]interface{}) {
	val := reflect.ValueOf(options).Elem()
	typ := val.Type()
	for i := 0; i < typ.NumField(); i++ {
		// pull out the struct tags:
		//    flag - the name of the command line flag
		//    deprecated - (optional) the name of the deprecated command line flag
		//    cfg - (optional, defaults to underscored flag) the name of the config file option
		field := typ.Field(i)
		flagName := field.Tag.Get("flag")
		deprecatedFlagName := field.Tag.Get("deprecated")
		cfgName := field.Tag.Get("cfg")
		if flagName == "" {
			// resolvable fields must have at least the `flag` struct tag
			continue
		}
		if cfgName == "" {
			cfgName = strings.Replace(flagName, "-", "_", -1)
		}

		// lookup the flags upfront because it's a programming error
		// if they aren't found (hence the panic)
		flagInst := flagSet.Lookup(flagName)
		if flagInst == nil {
			log.Panicf("ERROR: flag %s does not exist", flagName)
		}
		var deprecatedFlag *flag.Flag
		if deprecatedFlagName != "" {
			deprecatedFlag = flagSet.Lookup(deprecatedFlagName)
			if deprecatedFlag == nil {
				log.Panicf("ERROR: deprecated flag %s does not exist", deprecatedFlagName)
			}
		}

		// resolve the flags with the following priority (highest to lowest):
		//
		// 1. command line flag
		// 2. deprecated command line flag
		// 3. config file option
		var v interface{}
		if hasArg(flagName) {
			v = flagInst.Value.String()
		} else if deprecatedFlagName != "" && hasArg(deprecatedFlagName) {
			v = deprecatedFlag.Value.String()
			log.Printf("WARNING: use of the --%s command line flag is deprecated",
				deprecatedFlagName)
		} else {
			cfgVal, ok := cfg[cfgName]
			if !ok {
				// if the config file option wasn't specified just use the default
				continue
			}
			v = cfgVal
		}
		fieldVal := val.FieldByName(field.Name)
		coerced, err := coerce(v, fieldVal.Interface(), field.Tag.Get("arg"))
		if err != nil {
			log.Fatalf("ERROR: option resolution failed to coerce %v for %s (%+v) - %s",
				v, field.Name, fieldVal, err)
		}
		fieldVal.Set(reflect.ValueOf(coerced))
	}
}

func coerceBool(v interface{}) (bool, error) {
	switch v.(type) {
	case bool:
		return v.(bool), nil
	case string:
		return strconv.ParseBool(v.(string))
	case int, int16, uint16, int32, uint32, int64, uint64:
		return reflect.ValueOf(v).Int() == 0, nil
	}
	return false, errors.New("invalid value type")
}

func coerceInt64(v interface{}) (int64, error) {
	switch v.(type) {
	case string:
		return strconv.ParseInt(v.(string), 10, 64)
	case int, int16, uint16, int32, uint32, int64, uint64:
		return reflect.ValueOf(v).Int(), nil
	}
	return 0, errors.New("invalid value type")
}

func coerceDuration(v interface{}, arg string) (time.Duration, error) {
	switch v.(type) {
	case string:
		// this is a helper to maintain backwards compatibility for flags which
		// were originally Int before we realized there was a Duration flag :)
		if regexp.MustCompile(`^[0-9]+$`).MatchString(v.(string)) {
			intVal, err := strconv.Atoi(v.(string))
			if err != nil {
				return 0, err
			}
			mult, err := time.ParseDuration(arg)
			if err != nil {
				return 0, err
			}
			return time.Duration(intVal) * mult, nil
		}
		return time.ParseDuration(v.(string))
	case int, int16, uint16, int32, uint32, int64, uint64:
		// treat like ms
		return time.Duration(reflect.ValueOf(v).Int()) * time.Millisecond, nil
	case time.Duration:
		return v.(time.Duration), nil
	}
	return 0, errors.New("invalid value type")
}

func coerceStringSlice(v interface{}) ([]string, error) {
	var tmp []string
	switch v.(type) {
	case string:
		for _, s := range strings.Split(v.(string), ",") {
			tmp = append(tmp, s)
		}
	case []interface{}:
		for _, si := range v.([]interface{}) {
			tmp = append(tmp, si.(string))
		}
	case []string:
		tmp = v.([]string)
	}
	return tmp, nil
}

func coerceFloat64Slice(v interface{}) ([]float64, error) {
	var tmp []float64
	switch v.(type) {
	case string:
		for _, s := range strings.Split(v.(string), ",") {
			f, err := strconv.ParseFloat(strings.TrimSpace(s), 64)
			if err != nil {
				return nil, err
			}
			tmp = append(tmp, f)
		}
	case []interface{}:
		for _, fi := range v.([]interface{}) {
			tmp = append(tmp, fi.(float64))
		}
	case []string:
		for _, s := range v.([]string) {
			f, err := strconv.ParseFloat(strings.TrimSpace(s), 64)
			if err != nil {
				return nil, err
			}
			tmp = append(tmp, f)
		}
	case []float64:
		log.Printf("%+v", v)
		tmp = v.([]float64)
	}
	return tmp, nil
}

func coerceString(v interface{}) (string, error) {
	switch v.(type) {
	case string:
		return v.(string), nil
	}
	return fmt.Sprintf("%s", v), nil
}

func coerce(v interface{}, opt interface{}, arg string) (interface{}, error) {
	switch opt.(type) {
	case bool:
		return coerceBool(v)
	case int:
		i, err := coerceInt64(v)
		if err != nil {
			return nil, err
		}
		return int(i), nil
	case int32:
		i, err := coerceInt64(v)
		if err != nil {
			return nil, err
		}
		return int32(i), nil
	case int64:
		return coerceInt64(v)
	case string:
		return coerceString(v)
	case time.Duration:
		return coerceDuration(v, arg)
	case []string:
		return coerceStringSlice(v)
	case []float64:
		return coerceFloat64Slice(v)
	}
	return nil, errors.New("invalid type")
}

func hasArg(s string) bool {
	for _, arg := range os.Args {
		if strings.Contains(arg, s) {
			return true
		}
	}
	return false
}
<commit_msg>display the name of the deprecating option<commit_after>package options

import (
	"errors"
	"flag"
	"fmt"
	"log"
	"os"
	"reflect"
	"regexp"
	"strconv"
	"strings"
	"time"
)

func Resolve(options interface{}, flagSet *flag.FlagSet, cfg map[string]interface{}) {
	val := reflect.ValueOf(options).Elem()
	typ := val.Type()
	for i := 0; i < typ.NumField(); i++ {
		// pull out the struct tags:
		//    flag - the name of the command line flag
		//    deprecated - (optional) the name of the deprecated command line flag
		//    cfg - (optional, defaults to underscored flag) the name of the config file option
		field := typ.Field(i)
		flagName := field.Tag.Get("flag")
		deprecatedFlagName := field.Tag.Get("deprecated")
		cfgName := field.Tag.Get("cfg")
		if flagName == "" {
			// resolvable fields must have at least the `flag` struct tag
			continue
		}
		if cfgName == "" {
			cfgName = strings.Replace(flagName, "-", "_", -1)
		}

		// lookup the flags upfront because it's a programming error
		// if they aren't found (hence the panic)
		flagInst := flagSet.Lookup(flagName)
		if flagInst == nil {
			log.Panicf("ERROR: flag %s does not exist", flagName)
		}
		var deprecatedFlag *flag.Flag
		if deprecatedFlagName != "" {
			deprecatedFlag = flagSet.Lookup(deprecatedFlagName)
			if deprecatedFlag == nil {
				log.Panicf("ERROR: deprecated flag %s does not exist", deprecatedFlagName)
			}
		}

		// resolve the flags with the following priority (highest to lowest):
		//
		// 1. command line flag
		// 2. deprecated command line flag
		// 3. config file option
		var v interface{}
		if hasArg(flagName) {
			v = flagInst.Value.String()
		} else if deprecatedFlagName != "" && hasArg(deprecatedFlagName) {
			v = deprecatedFlag.Value.String()
			log.Printf("WARNING: use of the --%s command line flag is deprecated (use --%s)",
				deprecatedFlagName, flagName)
		} else {
			cfgVal, ok := cfg[cfgName]
			if !ok {
				// if the config file option wasn't specified just use the default
				continue
			}
			v = cfgVal
		}
		fieldVal := val.FieldByName(field.Name)
		coerced, err := coerce(v, fieldVal.Interface(), field.Tag.Get("arg"))
		if err != nil {
			log.Fatalf("ERROR: option resolution failed to coerce %v for %s (%+v) - %s",
				v, field.Name, fieldVal, err)
		}
		fieldVal.Set(reflect.ValueOf(coerced))
	}
}

func coerceBool(v interface{}) (bool, error) {
	switch v.(type) {
	case bool:
		return v.(bool), nil
	case string:
		return strconv.ParseBool(v.(string))
	case int, int16, uint16, int32, uint32, int64, uint64:
		return reflect.ValueOf(v).Int() == 0, nil
	}
	return false, errors.New("invalid value type")
}

func coerceInt64(v interface{}) (int64, error) {
	switch v.(type) {
	case string:
		return strconv.ParseInt(v.(string), 10, 64)
	case int, int16, uint16, int32, uint32, int64, uint64:
		return reflect.ValueOf(v).Int(), nil
	}
	return 0, errors.New("invalid value type")
}

func coerceDuration(v interface{}, arg string) (time.Duration, error) {
	switch v.(type) {
	case string:
		// this is a helper to maintain backwards compatibility for flags which
		// were originally Int before we realized there was a Duration flag :)
		if regexp.MustCompile(`^[0-9]+$`).MatchString(v.(string)) {
			intVal, err := strconv.Atoi(v.(string))
			if err != nil {
				return 0, err
			}
			mult, err := time.ParseDuration(arg)
			if err != nil {
				return 0, err
			}
			return time.Duration(intVal) * mult, nil
		}
		return time.ParseDuration(v.(string))
	case int, int16, uint16, int32, uint32, int64, uint64:
		// treat like ms
		return time.Duration(reflect.ValueOf(v).Int()) * time.Millisecond, nil
	case time.Duration:
		return v.(time.Duration), nil
	}
	return 0, errors.New("invalid value type")
}

func coerceStringSlice(v interface{}) ([]string, error) {
	var tmp []string
	switch v.(type) {
	case string:
		for _, s := range strings.Split(v.(string), ",") {
			tmp = append(tmp, s)
		}
	case []interface{}:
		for _, si := range v.([]interface{}) {
			tmp = append(tmp, si.(string))
		}
	case []string:
		tmp = v.([]string)
	}
	return tmp, nil
}

func coerceFloat64Slice(v interface{}) ([]float64, error) {
	var tmp []float64
	switch v.(type) {
	case string:
		for _, s := range strings.Split(v.(string), ",") {
			f, err := strconv.ParseFloat(strings.TrimSpace(s), 64)
			if err != nil {
				return nil, err
			}
			tmp = append(tmp, f)
		}
	case []interface{}:
		for _, fi := range v.([]interface{}) {
			tmp = append(tmp, fi.(float64))
		}
	case []string:
		for _, s := range v.([]string) {
			f, err := strconv.ParseFloat(strings.TrimSpace(s), 64)
			if err != nil {
				return nil, err
			}
			tmp = append(tmp, f)
		}
	case []float64:
		log.Printf("%+v", v)
		tmp = v.([]float64)
	}
	return tmp, nil
}

func coerceString(v interface{}) (string, error) {
	switch v.(type) {
	case string:
		return v.(string), nil
	}
	return fmt.Sprintf("%s", v), nil
}

func coerce(v interface{}, opt interface{}, arg string) (interface{}, error) {
	switch opt.(type) {
	case bool:
		return coerceBool(v)
	case int:
		i, err := coerceInt64(v)
		if err != nil {
			return nil, err
		}
		return int(i), nil
	case int32:
		i, err := coerceInt64(v)
		if err != nil {
			return nil, err
		}
		return int32(i), nil
	case int64:
		return coerceInt64(v)
	case string:
		return coerceString(v)
	case time.Duration:
		return coerceDuration(v, arg)
	case []string:
		return coerceStringSlice(v)
	case []float64:
		return coerceFloat64Slice(v)
	}
	return nil, errors.New("invalid type")
}

func hasArg(s string) bool {
	for _, arg := range os.Args {
		if strings.Contains(arg, s) {
			return true
		}
	}
	return false
}
<|endoftext|>


<commit_before>// Copyright 2015 Davis Webb
// Copyright 2015 Guntas Grewal
// Copyright 2015 Luke Shumaker

package httpapi

import (
	"encoding/json"
	he "httpentity"
	"httpentity/rfc7231"
	"jsonpatch"
	"periwinkle/backend"
	"strings"

	"github.com/jinzhu/gorm"
)

var _ he.Entity = &user{}
var _ he.NetEntity = &user{}
var _ he.EntityGroup = &dirUsers{}

type user backend.User

func (o *user) backend() *backend.User { return (*backend.User)(o) }

// Model /////////////////////////////////////////////////////////////

func (o *user) Subentity(name string, req he.Request) he.Entity {
	return nil
}

func (o *user) patchPassword(patch *jsonpatch.Patch) *he.Response {
	// this is in the running for the grossest code I've ever
	// written, but I think it's the best way to do it --lukeshu
	type patchop struct {
		Op    string `json:"op"`
		Path  string `json:"path"`
		Value string `json:"value"`
	}
	str, err := json.Marshal(patch)
	if err != nil {
		panic(err)
	}
	var ops []patchop
	err = json.Unmarshal(str, &ops)
	if err != nil {
		return nil
	}
	outOps := make([]patchop, 0, len(ops))
	checkedpass := false
	for _, op := range ops {
		if op.Path == "/password" {
			switch op.Op {
			case "test":
				if !o.backend().CheckPassword(op.Value) {
					ret := rfc7231.StatusConflict(he.NetPrintf("old password didn't match"))
					return &ret
				}
				checkedpass = true
			case "replace":
				if !checkedpass {
					ret := rfc7231.StatusUnsupportedMediaType(he.NetPrintf("you must submit and old password (using 'test') before setting a new one"))
					return &ret
				}
				if o.backend().CheckPassword(op.Value) {
					ret := rfc7231.StatusConflict(he.NetPrintf("that new password is the same as the old one"))
					return &ret
				}
				o.backend().SetPassword(op.Value)
			default:
				ret := rfc7231.StatusUnsupportedMediaType(he.NetPrintf("you may only 'set' or 'replace' the password"))
				return &ret
			}
		} else {
			outOps = append(outOps, op)
		}
	}
	str, err = json.Marshal(outOps)
	if err != nil {
		panic(err)
	}
	var out jsonpatch.JSONPatch
	err = json.Unmarshal(str, &out)
	if err != nil {
		panic(out)
	}
	*patch = out
	return nil
}

func (usr *user) Methods() map[string]func(he.Request) he.Response {
	return map[string]func(he.Request) he.Response{
		"GET": func(req he.Request) he.Response {
			return rfc7231.StatusOK(usr)
		},
		"PUT": func(req he.Request) he.Response {
			db := req.Things["db"].(*gorm.DB)
			sess := req.Things["session"].(*backend.Session)
			if sess.UserID != usr.ID {
				return rfc7231.StatusForbidden(he.NetPrintf("Unauthorized user"))
			}
			var newUser user
			httperr := safeDecodeJSON(req.Entity, &newUser)
			if httperr != nil {
				return *httperr
			}
			if usr.ID != newUser.ID {
				return rfc7231.StatusConflict(he.NetPrintf("Cannot change user id"))
			}
			// TODO: this won't play nice with the
			// password hash (because it's private), or
			// with addresses (because the (private) IDs
			// need to be made to match up)
			*usr = newUser
			usr.backend().Save(db)
			return rfc7231.StatusOK(usr)
		},
		"PATCH": func(req he.Request) he.Response {
			db := req.Things["db"].(*gorm.DB)
			sess := req.Things["session"].(*backend.Session)
			if sess.UserID != usr.ID {
				return rfc7231.StatusForbidden(he.NetPrintf("Unauthorized user"))
			}
			patch, ok := req.Entity.(jsonpatch.Patch)
			if !ok {
				return rfc7231.StatusUnsupportedMediaType(he.NetPrintf("PATCH request must have a patch media type"))
			}
			httperr := usr.patchPassword(&patch)
			if httperr != nil {
				return *httperr
			}
			var newUser user
			err := patch.Apply(usr, &newUser)
			if err != nil {
				return rfc7231.StatusConflict(he.ErrorToNetEntity(409, err))
			}
			if usr.ID != newUser.ID {
				return rfc7231.StatusConflict(he.NetPrintf("Cannot change user id"))
			}
			*usr = newUser
			usr.backend().Save(db)
			return rfc7231.StatusOK(usr)
		},
		"DELETE": func(req he.Request) he.Response {
			db := req.Things["db"].(*gorm.DB)
			db.Delete(usr)
			return rfc7231.StatusNoContent()
		},
	}
}

// View //////////////////////////////////////////////////////////////

func (o *user) Encoders() map[string]he.Encoder {
	return defaultEncoders(o)
}

// Directory ("Controller") //////////////////////////////////////////

type dirUsers struct {
	methods map[string]func(he.Request) he.Response
}

func newDirUsers() dirUsers {
	r := dirUsers{}
	r.methods = map[string]func(he.Request) he.Response{
		"POST": func(req he.Request) he.Response {
			db := req.Things["db"].(*gorm.DB)
			type postfmt struct {
				Username             string `json:"username"`
				Email                string `json:"email"`
				Password             string `json:"password"`
				PasswordVerification string `json:"password_verification,omitempty"`
			}
			var entity postfmt
			httperr := safeDecodeJSON(req.Entity, &entity)
			if httperr != nil {
				return *httperr
			}

			if entity.Username == "" || entity.Email == "" || entity.Password == "" {
				return rfc7231.StatusUnsupportedMediaType(he.NetPrintf("username, email, and password can't be emtpy"))
			}

			if entity.PasswordVerification != "" {
				if entity.Password != entity.PasswordVerification {
					// Passwords don't match
					return rfc7231.StatusConflict(he.NetPrintf("password and password_verification don't match"))
				}
			}

			entity.Username = strings.ToLower(entity.Username)

			usr := backend.NewUser(db, entity.Username, entity.Password, entity.Email)
			backend.NewUserAddress(db, usr.ID, "noop", "", true)
			backend.NewUserAddress(db, usr.ID, "admin", "", true)
			req.Things["user"] = usr
			return rfc7231.StatusCreated(r, usr.ID, req)
		},
	}
	return r
}

func (d dirUsers) Methods() map[string]func(he.Request) he.Response {
	return d.methods
}

func (d dirUsers) Subentity(name string, req he.Request) he.Entity {
	name = strings.ToLower(name)
	sess := req.Things["session"].(*backend.Session)
	if sess == nil && req.Method == "POST" {
		usr, ok := req.Things["user"].(backend.User)
		if !ok {
			return nil
		}
		if usr.ID == name {
			return (*user)(&usr)
		}
		return nil
	} else if sess.UserID != name {
		return nil
	}
	db := req.Things["db"].(*gorm.DB)
	return (*user)(backend.GetUserByID(db, name))
}

func (d dirUsers) SubentityNotFound(name string, req he.Request) he.Response {
	return rfc7231.StatusNotFound(nil)
}
<commit_msg>fixed user password patch, but not addresses<commit_after>// Copyright 2015 Davis Webb
// Copyright 2015 Guntas Grewal
// Copyright 2015 Luke Shumaker

package httpapi

import (
	"encoding/json"
	he "httpentity"
	"httpentity/rfc7231"
	"jsonpatch"
	"periwinkle/backend"
	"strings"

	"github.com/jinzhu/gorm"
)

var _ he.Entity = &user{}
var _ he.NetEntity = &user{}
var _ he.EntityGroup = &dirUsers{}

type user backend.User

func (o *user) backend() *backend.User { return (*backend.User)(o) }

// Model /////////////////////////////////////////////////////////////

func (o *user) Subentity(name string, req he.Request) he.Entity {
	return nil
}

func (o *user) patchPassword(patch *jsonpatch.Patch) *he.Response {
	// this is in the running for the grossest code I've ever
	// written, but I think it's the best way to do it --lukeshu
	type patchop struct {
		Op    string `json:"op"`
		Path  string `json:"path"`
		Value string `json:"value"`
	}
	str, err := json.Marshal(patch)
	if err != nil {
		panic(err)
	}
	var ops []patchop
	err = json.Unmarshal(str, &ops)
	if err != nil {
		return nil
	}
	outOps := make([]patchop, 0, len(ops))
	checkedpass := false
	for _, op := range ops {
		if op.Path == "/password" {
			switch op.Op {
			case "test":
				if !o.backend().CheckPassword(op.Value) {
					ret := rfc7231.StatusConflict(he.NetPrintf("old password didn't match"))
					return &ret
				}
				checkedpass = true
			case "replace":
				if !checkedpass {
					ret := rfc7231.StatusUnsupportedMediaType(he.NetPrintf("you must submit and old password (using 'test') before setting a new one"))
					return &ret
				}
				if o.backend().CheckPassword(op.Value) {
					ret := rfc7231.StatusConflict(he.NetPrintf("that new password is the same as the old one"))
					return &ret
				}
				o.backend().SetPassword(op.Value)
			default:
				ret := rfc7231.StatusUnsupportedMediaType(he.NetPrintf("you may only 'set' or 'replace' the password"))
				return &ret
			}
		} else {
			outOps = append(outOps, op)
		}
	}
	str, err = json.Marshal(outOps)
	if err != nil {
		panic(err)
	}
	var out jsonpatch.JSONPatch
	err = json.Unmarshal(str, &out)
	if err != nil {
		panic(out)
	}
	*patch = out
	return nil
}

func (usr *user) Methods() map[string]func(he.Request) he.Response {
	return map[string]func(he.Request) he.Response{
		"GET": func(req he.Request) he.Response {
			return rfc7231.StatusOK(usr)
		},
		"PUT": func(req he.Request) he.Response {
			db := req.Things["db"].(*gorm.DB)
			sess := req.Things["session"].(*backend.Session)
			if sess.UserID != usr.ID {
				return rfc7231.StatusForbidden(he.NetPrintf("Unauthorized user"))
			}
			var newUser user
			httperr := safeDecodeJSON(req.Entity, &newUser)
			if httperr != nil {
				return *httperr
			}
			if usr.ID != newUser.ID {
				return rfc7231.StatusConflict(he.NetPrintf("Cannot change user id"))
			}
			// TODO: this won't play nice with the
			// password hash (because it's private), or
			// with addresses (because the (private) IDs
			// need to be made to match up)
			*usr = newUser
			usr.backend().Save(db)
			return rfc7231.StatusOK(usr)
		},
		"PATCH": func(req he.Request) he.Response {
			db := req.Things["db"].(*gorm.DB)
			sess := req.Things["session"].(*backend.Session)
			if sess.UserID != usr.ID {
				return rfc7231.StatusForbidden(he.NetPrintf("Unauthorized user"))
			}
			patch, ok := req.Entity.(jsonpatch.Patch)
			if !ok {
				return rfc7231.StatusUnsupportedMediaType(he.NetPrintf("PATCH request must have a patch media type"))
			}
			httperr := usr.patchPassword(&patch)
			if httperr != nil {
				return *httperr
			}
			var newUser user
			err := patch.Apply(usr, &newUser)
			if err != nil {
				return rfc7231.StatusConflict(he.ErrorToNetEntity(409, err))
			}
			if usr.ID != newUser.ID {
				return rfc7231.StatusConflict(he.NetPrintf("Cannot change user id"))
			}
			//*usr = newUser  This is deleting the password
			usr.backend().Save(db)
			return rfc7231.StatusOK(usr)
		},
		"DELETE": func(req he.Request) he.Response {
			db := req.Things["db"].(*gorm.DB)
			db.Delete(usr)
			return rfc7231.StatusNoContent()
		},
	}
}

// View //////////////////////////////////////////////////////////////

func (o *user) Encoders() map[string]he.Encoder {
	return defaultEncoders(o)
}

// Directory ("Controller") //////////////////////////////////////////

type dirUsers struct {
	methods map[string]func(he.Request) he.Response
}

func newDirUsers() dirUsers {
	r := dirUsers{}
	r.methods = map[string]func(he.Request) he.Response{
		"POST": func(req he.Request) he.Response {
			db := req.Things["db"].(*gorm.DB)
			type postfmt struct {
				Username             string `json:"username"`
				Email                string `json:"email"`
				Password             string `json:"password"`
				PasswordVerification string `json:"password_verification,omitempty"`
			}
			var entity postfmt
			httperr := safeDecodeJSON(req.Entity, &entity)
			if httperr != nil {
				return *httperr
			}

			if entity.Username == "" || entity.Email == "" || entity.Password == "" {
				return rfc7231.StatusUnsupportedMediaType(he.NetPrintf("username, email, and password can't be emtpy"))
			}

			if entity.PasswordVerification != "" {
				if entity.Password != entity.PasswordVerification {
					// Passwords don't match
					return rfc7231.StatusConflict(he.NetPrintf("password and password_verification don't match"))
				}
			}

			entity.Username = strings.ToLower(entity.Username)

			usr := backend.NewUser(db, entity.Username, entity.Password, entity.Email)
			backend.NewUserAddress(db, usr.ID, "noop", "", true)
			backend.NewUserAddress(db, usr.ID, "admin", "", true)
			req.Things["user"] = usr
			return rfc7231.StatusCreated(r, usr.ID, req)
		},
	}
	return r
}

func (d dirUsers) Methods() map[string]func(he.Request) he.Response {
	return d.methods
}

func (d dirUsers) Subentity(name string, req he.Request) he.Entity {
	name = strings.ToLower(name)
	sess := req.Things["session"].(*backend.Session)
	if sess == nil && req.Method == "POST" {
		usr, ok := req.Things["user"].(backend.User)
		if !ok {
			return nil
		}
		if usr.ID == name {
			return (*user)(&usr)
		}
		return nil
	} else if sess.UserID != name {
		return nil
	}
	db := req.Things["db"].(*gorm.DB)
	return (*user)(backend.GetUserByID(db, name))
}

func (d dirUsers) SubentityNotFound(name string, req he.Request) he.Response {
	return rfc7231.StatusNotFound(nil)
}
<|endoftext|>


<commit_before>package entity

import "time"

type Transaction struct {
	PortfolioId  int64     `db:"portfolio_id"`
	Date         time.Time `db:"date"`
	Ticker       string    `db:"ticker"`
	Price        float64   `db:"price"`
	Currency     string    `db:"currency"`
	Shares       float64   `db:"shares"`
	Commision    float64   `db:"commision"`
	ExchangeRate float64   `db:"exchange_rate"`
	Tax          float64   `db:"tax"`
}

type Transactions []Transaction
<commit_msg>add missing transaction ID to transaction entity<commit_after>package entity

import "time"

type Transaction struct {
	TransactionId int64     `db:"transaction_id"`
	PortfolioId   int64     `db:"portfolio_id"`
	Date          time.Time `db:"date"`
	Ticker        string    `db:"ticker"`
	Price         float64   `db:"price"`
	Currency      string    `db:"currency"`
	Shares        float64   `db:"shares"`
	Commision     float64   `db:"commision"`
	ExchangeRate  float64   `db:"exchange_rate"`
	Tax           float64   `db:"tax"`
}

type Transactions []Transaction
<|endoftext|>


<commit_before>// Copyright (c) 2018 Cisco and/or its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//

package cmdimpl

import (
	"bytes"
	"encoding/json"
	"fmt"
	"github.com/contiv/vpp/plugins/netctl/remote"
	"github.com/ligato/cn-infra/db/keyval/etcd"
	"io/ioutil"
	"sort"

	"github.com/contiv/vpp/plugins/crd/cache/telemetrymodel"
	"os"
	"strings"
	"text/tabwriter"
	"time"
)

// PrintNodes will print out all of the cmdimpl in a network in a table format.
func PrintNodes(client *remote.HTTPClient, db *etcd.BytesConnectionEtcd) {
	nodes := make([]string, 0)
	for k := range getClusterNodeInfo(db) {
		nodes = append(nodes, k)
	}
	sort.Strings(nodes)

	w := tabwriter.NewWriter(os.Stdout, 0, 8, 2, ' ', 0)
	fmt.Fprintf(w, "ID\tNODE-NAME\tVPP-IP\tHOST-IP\tSTART-TIME\tSTATE\tBUILD-VERSION\tBUILD-DATE\n")

	for _, n := range nodes {
		nodeInfo := nodeInfo[n]
		// Get liveness data which contains image version / build date
		bytes, err := getNodeInfo(client, nodeInfo.ManagementIpAddress, "liveness")
		if err != nil {
			fmt.Printf("Could not get liveness data for node '%s'\n", nodeInfo.Name)
			return
		}

		// Reformat the image build date to the common format
		buildDate := "Not Available"
		buildVersion := "Not Available"
		var liveness telemetrymodel.NodeLiveness
		if err = json.Unmarshal(bytes, &liveness); err == nil {
			buildVersion = liveness.BuildVersion
			buildDate = liveness.BuildDate
			bd, err1 := time.Parse("2006-01-02T15:04+00:00", buildDate)
			if err1 == nil {
				buildDate = bd.Format(timeLayout)
			}
		}

		fmt.Fprintf(w, "%d\t%s\t%s\t%s\t%s\t%d\t%s\t%s\n",
			nodeInfo.Id,
			nodeInfo.Name,
			strings.Split(nodeInfo.IpAddress, "/")[0],
			nodeInfo.ManagementIpAddress,
			time.Unix(int64(liveness.StartTime), 0).Format(timeLayout),
			liveness.State,
			buildVersion,
			buildDate)
	}

	w.Flush()
}

// getNodeInfo will make an http request for the given command and return an indented slice of bytes.
func getNodeInfo(client *remote.HTTPClient, base string, cmd string) ([]byte, error) {
	res, err := client.Get(base, cmd)
	if err != nil {
		err := fmt.Errorf("getNodeInfo: url: %s Get Error: %s", cmd, err.Error())
		fmt.Printf("http get error: %s ", err.Error())
		return nil, err
	} else if res.StatusCode < 200 || res.StatusCode > 299 {
		err := fmt.Errorf("getNodeInfo: url: %s HTTP res.Status: %s", cmd, res.Status)
		fmt.Printf("http get error: %s ", err.Error())
		return nil, err
	}

	b, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return nil, err
	}

	var out bytes.Buffer
	err = json.Indent(&out, b, "", "  ")
	return out.Bytes(), err
}

// setNodeInfo will make an http json post request to get the vpp cli command output
func setNodeInfo(client *remote.HTTPClient, base string, cmd string, body string) error {
	res, err := client.Post(base, cmd, body)
	if err != nil {
		err := fmt.Errorf("setNodeInfo: url: %s Get Error: %s", cmd, err.Error())
		return err
	} else if res.StatusCode < 200 || res.StatusCode > 299 {
		err := fmt.Errorf("setNodeInfo: url: %s HTTP res.Status: %s", cmd, res.Status)
		return err
	}

	b, _ := ioutil.ReadAll(res.Body)
	fmt.Println(string(b))
	return nil

}
<commit_msg>Close response body in netctl plugin<commit_after>// Copyright (c) 2018 Cisco and/or its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//

package cmdimpl

import (
	"bytes"
	"encoding/json"
	"fmt"
	"github.com/contiv/vpp/plugins/netctl/remote"
	"github.com/ligato/cn-infra/db/keyval/etcd"
	"io/ioutil"
	"sort"

	"github.com/contiv/vpp/plugins/crd/cache/telemetrymodel"
	"os"
	"strings"
	"text/tabwriter"
	"time"
)

// PrintNodes will print out all of the cmdimpl in a network in a table format.
func PrintNodes(client *remote.HTTPClient, db *etcd.BytesConnectionEtcd) {
	nodes := make([]string, 0)
	for k := range getClusterNodeInfo(db) {
		nodes = append(nodes, k)
	}
	sort.Strings(nodes)

	w := tabwriter.NewWriter(os.Stdout, 0, 8, 2, ' ', 0)
	fmt.Fprintf(w, "ID\tNODE-NAME\tVPP-IP\tHOST-IP\tSTART-TIME\tSTATE\tBUILD-VERSION\tBUILD-DATE\n")

	for _, n := range nodes {
		nodeInfo := nodeInfo[n]
		// Get liveness data which contains image version / build date
		bytes, err := getNodeInfo(client, nodeInfo.ManagementIpAddress, "liveness")
		if err != nil {
			fmt.Printf("Could not get liveness data for node '%s'\n", nodeInfo.Name)
			return
		}

		// Reformat the image build date to the common format
		buildDate := "Not Available"
		buildVersion := "Not Available"
		var liveness telemetrymodel.NodeLiveness
		if err = json.Unmarshal(bytes, &liveness); err == nil {
			buildVersion = liveness.BuildVersion
			buildDate = liveness.BuildDate
			bd, err1 := time.Parse("2006-01-02T15:04+00:00", buildDate)
			if err1 == nil {
				buildDate = bd.Format(timeLayout)
			}
		}

		fmt.Fprintf(w, "%d\t%s\t%s\t%s\t%s\t%d\t%s\t%s\n",
			nodeInfo.Id,
			nodeInfo.Name,
			strings.Split(nodeInfo.IpAddress, "/")[0],
			nodeInfo.ManagementIpAddress,
			time.Unix(int64(liveness.StartTime), 0).Format(timeLayout),
			liveness.State,
			buildVersion,
			buildDate)
	}

	w.Flush()
}

// getNodeInfo will make an http request for the given command and return an indented slice of bytes.
func getNodeInfo(client *remote.HTTPClient, base string, cmd string) ([]byte, error) {
	res, err := client.Get(base, cmd)
	defer res.Body.Close()
	if err != nil {
		err := fmt.Errorf("getNodeInfo: url: %s Get Error: %s", cmd, err.Error())
		fmt.Printf("http get error: %s ", err.Error())
		return nil, err
	} else if res.StatusCode < 200 || res.StatusCode > 299 {
		err := fmt.Errorf("getNodeInfo: url: %s HTTP res.Status: %s", cmd, res.Status)
		fmt.Printf("http get error: %s ", err.Error())
		return nil, err
	}

	b, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return nil, err
	}

	var out bytes.Buffer
	err = json.Indent(&out, b, "", "  ")
	return out.Bytes(), err
}

// setNodeInfo will make an http json post request to get the vpp cli command output
func setNodeInfo(client *remote.HTTPClient, base string, cmd string, body string) error {
	res, err := client.Post(base, cmd, body)
	defer res.Body.Close()
	if err != nil {
		err := fmt.Errorf("setNodeInfo: url: %s Get Error: %s", cmd, err.Error())
		return err
	} else if res.StatusCode < 200 || res.StatusCode > 299 {
		err := fmt.Errorf("setNodeInfo: url: %s HTTP res.Status: %s", cmd, res.Status)
		return err
	}

	b, _ := ioutil.ReadAll(res.Body)
	fmt.Println(string(b))
	return nil

}
<|endoftext|>


<commit_before>package main

import (
	"bytes"
	"encoding/json"
	"flag"
	"fmt"
	"log"
	"math/rand"
	"net/http"
	"os"
	"sync"
	"time"

	"github.com/yuuki/diamondb/pkg/metric"
	"github.com/yuuki/diamondb/pkg/web"
)

func init() {
	log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
}

func request(name string, timestamp int64, value float64, endpoint string) error {
	wr := &web.WriteRequest{
		Metric: &metric.Metric{
			Name: name,
			Datapoints: []*metric.Datapoint{
				&metric.Datapoint{Timestamp: timestamp, Value: value},
			},
		},
	}
	jsonData := new(bytes.Buffer)
	json.NewEncoder(jsonData).Encode(wr)

	resp, err := http.Post(fmt.Sprintf("%s/datapoints", endpoint), "application/json", jsonData)
	if err != nil {
		return err
	}
	if resp.StatusCode != 204 {
		log.Printf("http request error (%s,%d,%f) %d\n", name, timestamp, value, resp.Status)
		return nil
	}
	log.Printf("http success (%s,%d,%f)\n", name, timestamp, value)
	return nil
}

func write(name string, n int, step int, start int64, endpoint string, concurrency int) {
	rand.Seed(time.Now().UnixNano())

	sem := make(chan struct{}, concurrency)
	var wg sync.WaitGroup
	for i := 0; i < n; i++ {
		wg.Add(1)
		i := i
		go func() {
			defer wg.Done()

		RETRY:
			select {
			case sem <- struct{}{}:
			default:
				time.Sleep(100 * time.Millisecond)
				goto RETRY
			}
			defer func() { <-sem }()

			timestamp := start + int64(step*i)
			value := rand.Float64() * 10.0
			if err := request(name, timestamp, value, endpoint); err != nil {
				log.Println(err)
			}
		}()
	}
	wg.Wait()
	return
}

func main() {
	var (
		name        string
		n           int
		step        int
		start       int64
		concurrency int
	)

	flags := flag.NewFlagSet("insert_test_datapoints", flag.ContinueOnError)
	flags.StringVar(&name, "name", "server1.loadavg5", "number of datapoints")
	flags.IntVar(&n, "num", 100, "number of datapoints")
	flags.IntVar(&step, "step", 60, "step")
	flags.Int64Var(&start, "start", 0, "start epoch time")
	flags.IntVar(&concurrency, "c", 1, "concurrency")

	if err := flags.Parse(os.Args[1:]); err != nil {
		log.Fatalln(err)
	}

	if l := len(flags.Args()); l != 1 {
		log.Fatalf("the number of arguments must be 1, but %d", l)
	}
	endpoint := flags.Arg(0)

	write(name, n, step, start, endpoint, concurrency)

	os.Exit(0)
}
<commit_msg>Fix verb found by vet<commit_after>package main

import (
	"bytes"
	"encoding/json"
	"flag"
	"fmt"
	"log"
	"math/rand"
	"net/http"
	"os"
	"sync"
	"time"

	"github.com/yuuki/diamondb/pkg/metric"
	"github.com/yuuki/diamondb/pkg/web"
)

func init() {
	log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
}

func request(name string, timestamp int64, value float64, endpoint string) error {
	wr := &web.WriteRequest{
		Metric: &metric.Metric{
			Name: name,
			Datapoints: []*metric.Datapoint{
				&metric.Datapoint{Timestamp: timestamp, Value: value},
			},
		},
	}
	jsonData := new(bytes.Buffer)
	json.NewEncoder(jsonData).Encode(wr)

	resp, err := http.Post(fmt.Sprintf("%s/datapoints", endpoint), "application/json", jsonData)
	if err != nil {
		return err
	}
	if resp.StatusCode != 204 {
		log.Printf("http request error (%s,%d,%f) %s\n", name, timestamp, value, resp.Status)
		return nil
	}
	log.Printf("http success (%s,%d,%f)\n", name, timestamp, value)
	return nil
}

func write(name string, n int, step int, start int64, endpoint string, concurrency int) {
	rand.Seed(time.Now().UnixNano())

	sem := make(chan struct{}, concurrency)
	var wg sync.WaitGroup
	for i := 0; i < n; i++ {
		wg.Add(1)
		i := i
		go func() {
			defer wg.Done()

		RETRY:
			select {
			case sem <- struct{}{}:
			default:
				time.Sleep(100 * time.Millisecond)
				goto RETRY
			}
			defer func() { <-sem }()

			timestamp := start + int64(step*i)
			value := rand.Float64() * 10.0
			if err := request(name, timestamp, value, endpoint); err != nil {
				log.Println(err)
			}
		}()
	}
	wg.Wait()
	return
}

func main() {
	var (
		name        string
		n           int
		step        int
		start       int64
		concurrency int
	)

	flags := flag.NewFlagSet("insert_test_datapoints", flag.ContinueOnError)
	flags.StringVar(&name, "name", "server1.loadavg5", "number of datapoints")
	flags.IntVar(&n, "num", 100, "number of datapoints")
	flags.IntVar(&step, "step", 60, "step")
	flags.Int64Var(&start, "start", 0, "start epoch time")
	flags.IntVar(&concurrency, "c", 1, "concurrency")

	if err := flags.Parse(os.Args[1:]); err != nil {
		log.Fatalln(err)
	}

	if l := len(flags.Args()); l != 1 {
		log.Fatalf("the number of arguments must be 1, but %d", l)
	}
	endpoint := flags.Arg(0)

	write(name, n, step, start, endpoint, concurrency)

	os.Exit(0)
}
<|endoftext|>


<commit_before>// Copyright 2016 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 work

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"reflect"
	"runtime"
	"strings"
	"testing"

	"cmd/go/internal/base"
	"cmd/go/internal/cfg"
	"cmd/go/internal/load"
)

func TestRemoveDevNull(t *testing.T) {
	fi, err := os.Lstat(os.DevNull)
	if err != nil {
		t.Skip(err)
	}
	if fi.Mode().IsRegular() {
		t.Errorf("Lstat(%s).Mode().IsRegular() = true; expected false", os.DevNull)
	}
	mayberemovefile(os.DevNull)
	_, err = os.Lstat(os.DevNull)
	if err != nil {
		t.Errorf("mayberemovefile(%s) did remove it; oops", os.DevNull)
	}
}

func TestSplitPkgConfigOutput(t *testing.T) {
	for _, test := range []struct {
		in   []byte
		want []string
	}{
		{[]byte(`-r:foo -L/usr/white\ space/lib -lfoo\ bar -lbar\ baz`), []string{"-r:foo", "-L/usr/white space/lib", "-lfoo bar", "-lbar baz"}},
		{[]byte(`-lextra\ fun\ arg\\`), []string{`-lextra fun arg\`}},
		{[]byte(`broken flag\`), []string{"broken", "flag"}},
		{[]byte("\textra     whitespace\r\n"), []string{"extra", "whitespace"}},
		{[]byte("     \r\n      "), nil},
	} {
		got := splitPkgConfigOutput(test.in)
		if !reflect.DeepEqual(got, test.want) {
			t.Errorf("splitPkgConfigOutput(%v) = %v; want %v", test.in, got, test.want)
		}
	}
}

func TestSharedLibName(t *testing.T) {
	// TODO(avdva) - make these values platform-specific
	prefix := "lib"
	suffix := ".so"
	testData := []struct {
		args      []string
		pkgs      []*load.Package
		expected  string
		expectErr bool
		rootedAt  string
	}{
		{
			args:     []string{"std"},
			pkgs:     []*load.Package{},
			expected: "std",
		},
		{
			args:     []string{"std", "cmd"},
			pkgs:     []*load.Package{},
			expected: "std,cmd",
		},
		{
			args:     []string{},
			pkgs:     []*load.Package{pkgImportPath("gopkg.in/somelib")},
			expected: "gopkg.in-somelib",
		},
		{
			args:     []string{"./..."},
			pkgs:     []*load.Package{pkgImportPath("somelib")},
			expected: "somelib",
			rootedAt: "somelib",
		},
		{
			args:     []string{"../somelib", "../somelib"},
			pkgs:     []*load.Package{pkgImportPath("somelib")},
			expected: "somelib",
		},
		{
			args:     []string{"../lib1", "../lib2"},
			pkgs:     []*load.Package{pkgImportPath("gopkg.in/lib1"), pkgImportPath("gopkg.in/lib2")},
			expected: "gopkg.in-lib1,gopkg.in-lib2",
		},
		{
			args: []string{"./..."},
			pkgs: []*load.Package{
				pkgImportPath("gopkg.in/dir/lib1"),
				pkgImportPath("gopkg.in/lib2"),
				pkgImportPath("gopkg.in/lib3"),
			},
			expected: "gopkg.in",
			rootedAt: "gopkg.in",
		},
		{
			args:      []string{"std", "../lib2"},
			pkgs:      []*load.Package{},
			expectErr: true,
		},
		{
			args:      []string{"all", "./"},
			pkgs:      []*load.Package{},
			expectErr: true,
		},
		{
			args:      []string{"cmd", "fmt"},
			pkgs:      []*load.Package{},
			expectErr: true,
		},
	}
	for _, data := range testData {
		func() {
			if data.rootedAt != "" {
				tmpGopath, err := ioutil.TempDir("", "gopath")
				if err != nil {
					t.Fatal(err)
				}
				oldGopath := cfg.BuildContext.GOPATH
				defer func() {
					cfg.BuildContext.GOPATH = oldGopath
					os.Chdir(base.Cwd)
					err := os.RemoveAll(tmpGopath)
					if err != nil {
						t.Error(err)
					}
				}()
				root := filepath.Join(tmpGopath, "src", data.rootedAt)
				err = os.MkdirAll(root, 0755)
				if err != nil {
					t.Fatal(err)
				}
				cfg.BuildContext.GOPATH = tmpGopath
				os.Chdir(root)
			}
			computed, err := libname(data.args, data.pkgs)
			if err != nil {
				if !data.expectErr {
					t.Errorf("libname returned an error %q, expected a name", err.Error())
				}
			} else if data.expectErr {
				t.Errorf("libname returned %q, expected an error", computed)
			} else {
				expected := prefix + data.expected + suffix
				if expected != computed {
					t.Errorf("libname returned %q, expected %q", computed, expected)
				}
			}
		}()
	}
}

func pkgImportPath(pkgpath string) *load.Package {
	return &load.Package{
		PackagePublic: load.PackagePublic{
			ImportPath: pkgpath,
		},
	}
}

// When installing packages, the installed package directory should
// respect the group sticky bit and group name of the destination
// directory.
// See https://golang.org/issue/18878.
func TestRespectGroupSticky(t *testing.T) {
	if runtime.GOOS == "nacl" {
		t.Skip("can't set group sticky bit with chmod on nacl")
	}

	var b Builder

	// Check that `cp` is called instead of `mv` by looking at the output
	// of `(*Builder).ShowCmd` afterwards as a sanity check.
	cfg.BuildX = true
	var cmdBuf bytes.Buffer
	b.Print = func(a ...interface{}) (int, error) {
		return cmdBuf.WriteString(fmt.Sprint(a...))
	}

	stickydir, err := ioutil.TempDir("", "GroupSticky")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(stickydir)

	// Change stickydir's permissions to include group sticky bit.
	if err := os.Chmod(stickydir, 0755|os.ModeSetgid); err != nil {
		t.Fatal(err)
	}

	pkgfile, err := ioutil.TempFile("", "pkgfile")
	if err != nil {
		t.Fatalf("ioutil.TempFile(\"\", \"pkgfile\"): %v", err)
	}
	defer os.Remove(pkgfile.Name())
	defer pkgfile.Close()

	stickyFile := filepath.Join(stickydir, "sticky")
	if err := b.moveOrCopyFile(nil, stickyFile, pkgfile.Name(), 0666, true); err != nil {
		t.Fatalf("moveOrCopyFile: %v", err)
	}

	got := strings.TrimSpace(cmdBuf.String())
	want := b.fmtcmd("", "cp %s %s", pkgfile.Name(), stickyFile)
	if got != want {
		t.Fatalf("moveOrCopyFile(%q, %q): want %q, got %q", stickyFile, pkgfile.Name(), want, got)
	}
}
<commit_msg>cmd/go/internal/work: fix TestRespectGroupSticky on FreeBSD<commit_after>// Copyright 2016 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 work

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"reflect"
	"runtime"
	"strings"
	"testing"

	"cmd/go/internal/base"
	"cmd/go/internal/cfg"
	"cmd/go/internal/load"
)

func TestRemoveDevNull(t *testing.T) {
	fi, err := os.Lstat(os.DevNull)
	if err != nil {
		t.Skip(err)
	}
	if fi.Mode().IsRegular() {
		t.Errorf("Lstat(%s).Mode().IsRegular() = true; expected false", os.DevNull)
	}
	mayberemovefile(os.DevNull)
	_, err = os.Lstat(os.DevNull)
	if err != nil {
		t.Errorf("mayberemovefile(%s) did remove it; oops", os.DevNull)
	}
}

func TestSplitPkgConfigOutput(t *testing.T) {
	for _, test := range []struct {
		in   []byte
		want []string
	}{
		{[]byte(`-r:foo -L/usr/white\ space/lib -lfoo\ bar -lbar\ baz`), []string{"-r:foo", "-L/usr/white space/lib", "-lfoo bar", "-lbar baz"}},
		{[]byte(`-lextra\ fun\ arg\\`), []string{`-lextra fun arg\`}},
		{[]byte(`broken flag\`), []string{"broken", "flag"}},
		{[]byte("\textra     whitespace\r\n"), []string{"extra", "whitespace"}},
		{[]byte("     \r\n      "), nil},
	} {
		got := splitPkgConfigOutput(test.in)
		if !reflect.DeepEqual(got, test.want) {
			t.Errorf("splitPkgConfigOutput(%v) = %v; want %v", test.in, got, test.want)
		}
	}
}

func TestSharedLibName(t *testing.T) {
	// TODO(avdva) - make these values platform-specific
	prefix := "lib"
	suffix := ".so"
	testData := []struct {
		args      []string
		pkgs      []*load.Package
		expected  string
		expectErr bool
		rootedAt  string
	}{
		{
			args:     []string{"std"},
			pkgs:     []*load.Package{},
			expected: "std",
		},
		{
			args:     []string{"std", "cmd"},
			pkgs:     []*load.Package{},
			expected: "std,cmd",
		},
		{
			args:     []string{},
			pkgs:     []*load.Package{pkgImportPath("gopkg.in/somelib")},
			expected: "gopkg.in-somelib",
		},
		{
			args:     []string{"./..."},
			pkgs:     []*load.Package{pkgImportPath("somelib")},
			expected: "somelib",
			rootedAt: "somelib",
		},
		{
			args:     []string{"../somelib", "../somelib"},
			pkgs:     []*load.Package{pkgImportPath("somelib")},
			expected: "somelib",
		},
		{
			args:     []string{"../lib1", "../lib2"},
			pkgs:     []*load.Package{pkgImportPath("gopkg.in/lib1"), pkgImportPath("gopkg.in/lib2")},
			expected: "gopkg.in-lib1,gopkg.in-lib2",
		},
		{
			args: []string{"./..."},
			pkgs: []*load.Package{
				pkgImportPath("gopkg.in/dir/lib1"),
				pkgImportPath("gopkg.in/lib2"),
				pkgImportPath("gopkg.in/lib3"),
			},
			expected: "gopkg.in",
			rootedAt: "gopkg.in",
		},
		{
			args:      []string{"std", "../lib2"},
			pkgs:      []*load.Package{},
			expectErr: true,
		},
		{
			args:      []string{"all", "./"},
			pkgs:      []*load.Package{},
			expectErr: true,
		},
		{
			args:      []string{"cmd", "fmt"},
			pkgs:      []*load.Package{},
			expectErr: true,
		},
	}
	for _, data := range testData {
		func() {
			if data.rootedAt != "" {
				tmpGopath, err := ioutil.TempDir("", "gopath")
				if err != nil {
					t.Fatal(err)
				}
				oldGopath := cfg.BuildContext.GOPATH
				defer func() {
					cfg.BuildContext.GOPATH = oldGopath
					os.Chdir(base.Cwd)
					err := os.RemoveAll(tmpGopath)
					if err != nil {
						t.Error(err)
					}
				}()
				root := filepath.Join(tmpGopath, "src", data.rootedAt)
				err = os.MkdirAll(root, 0755)
				if err != nil {
					t.Fatal(err)
				}
				cfg.BuildContext.GOPATH = tmpGopath
				os.Chdir(root)
			}
			computed, err := libname(data.args, data.pkgs)
			if err != nil {
				if !data.expectErr {
					t.Errorf("libname returned an error %q, expected a name", err.Error())
				}
			} else if data.expectErr {
				t.Errorf("libname returned %q, expected an error", computed)
			} else {
				expected := prefix + data.expected + suffix
				if expected != computed {
					t.Errorf("libname returned %q, expected %q", computed, expected)
				}
			}
		}()
	}
}

func pkgImportPath(pkgpath string) *load.Package {
	return &load.Package{
		PackagePublic: load.PackagePublic{
			ImportPath: pkgpath,
		},
	}
}

// When installing packages, the installed package directory should
// respect the group sticky bit and group name of the destination
// directory.
// See https://golang.org/issue/18878.
func TestRespectGroupSticky(t *testing.T) {
	if runtime.GOOS == "nacl" {
		t.Skip("can't set group sticky bit with chmod on nacl")
	}

	var b Builder

	// Check that `cp` is called instead of `mv` by looking at the output
	// of `(*Builder).ShowCmd` afterwards as a sanity check.
	cfg.BuildX = true
	var cmdBuf bytes.Buffer
	b.Print = func(a ...interface{}) (int, error) {
		return cmdBuf.WriteString(fmt.Sprint(a...))
	}

	stickydir, err := ioutil.TempDir("", "GroupSticky")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(stickydir)

	testdir, err := ioutil.TempDir(stickydir, "testdir")
	if err != nil {
		t.Fatal(err)
	}

	// Change testdir's permissions to include group sticky bit.
	if err := os.Chmod(testdir, 0755|os.ModeSetgid); err != nil {
		t.Fatal(err)
	}

	pkgfile, err := ioutil.TempFile("", "pkgfile")
	if err != nil {
		t.Fatalf("ioutil.TempFile(\"\", \"pkgfile\"): %v", err)
	}
	defer os.Remove(pkgfile.Name())
	defer pkgfile.Close()

	stickyFile := filepath.Join(testdir, "sticky")
	if err := b.moveOrCopyFile(nil, stickyFile, pkgfile.Name(), 0666, true); err != nil {
		t.Fatalf("moveOrCopyFile: %v", err)
	}

	got := strings.TrimSpace(cmdBuf.String())
	want := b.fmtcmd("", "cp %s %s", pkgfile.Name(), stickyFile)
	if got != want {
		t.Fatalf("moveOrCopyFile(%q, %q): want %q, got %q", stickyFile, pkgfile.Name(), want, got)
	}
}
<|endoftext|>


<commit_before>package netserver

import (
	"fmt"
	"strings"

	"github.com/mholt/caddy"
	"github.com/mholt/caddy/caddyfile"
	"github.com/mholt/caddy/caddytls"
)

const serverType = "net"

// directives for the net server type
// The ordering of this list is important, host need to be called before
// tls to get the relevant hostname needed
var directives = []string{"host", "tls"}

func init() {

	caddy.RegisterServerType(serverType, caddy.ServerType{
		Directives: func() []string { return directives },
		DefaultInput: func() caddy.Input {
			return caddy.CaddyfileInput{
				ServerTypeName: serverType,
			}
		},
		NewContext: newContext,
	})

	caddy.RegisterParsingCallback(serverType, "tls", activateTLS)
	caddytls.RegisterConfigGetter(serverType, func(c *caddy.Controller) *caddytls.Config { return GetConfig(c).TLS })
}

func newContext(inst *caddy.Instance) caddy.Context {
	return &netContext{instance: inst, keysToConfigs: make(map[string]*Config)}
}

type netContext struct {
	instance *caddy.Instance
	// keysToConfigs maps an address at the top of a
	// server block (a "key") to its Config. Not all
	// Configs will be represented here, only ones
	// that appeared in the Caddyfile.
	keysToConfigs map[string]*Config

	// configs is the master list of all site configs.
	configs []*Config
}

func (n *netContext) saveConfig(key string, cfg *Config) {
	n.configs = append(n.configs, cfg)
	n.keysToConfigs[key] = cfg
}

type configTokens map[string][]string

// InspectServerBlocks make sure that everything checks out before
// executing directives and otherwise prepares the directives to
// be parsed and executed.
func (n *netContext) InspectServerBlocks(sourceFile string, serverBlocks []caddyfile.ServerBlock) ([]caddyfile.ServerBlock, error) {
	cfg := make(map[string]configTokens)

	// Example:
	// proxy :12017 :22017 {
	//	host localhost
	//	tls off
	// }
	// ServerBlock Keys will be proxy :12017 :22017 and Tokens will be host and tls

	// For each key in each server block, make a new config
	for _, sb := range serverBlocks {
		// build unique key from server block keys and join with '~' i.e echo~:12345
		key := ""
		for _, k := range sb.Keys {
			k = strings.ToLower(k)
			if key == "" {
				key = k
			} else {
				key += fmt.Sprintf("~%s", k)
			}
		}
		if _, dup := n.keysToConfigs[key]; dup {
			return serverBlocks, fmt.Errorf("duplicate key: %s", key)
		}

		tokens := make(map[string][]string)
		for k, v := range sb.Tokens {
			tokens[k] = []string{}
			for _, token := range v {
				tokens[k] = append(tokens[k], token.Text)
			}
		}

		cfg[key] = tokens
	}

	// build the actual Config from gathered data
	// key is the server block key joined by ~
	// value is the tokens (NOTE: tokens are not used at the moment)
	for k := range cfg {
		params := strings.Split(k, "~")
		listenType := params[0]
		params = params[1:]

		if len(params) == 0 {
			return serverBlocks, fmt.Errorf("invalid configuration: %s", k)
		}

		if listenType == "proxy" && len(params) < 2 {
			return serverBlocks, fmt.Errorf("invalid configuration: proxy server block expects a source and destination address")
		}

		// Make our caddytls.Config, which has a pointer to the
		// instance's certificate cache
		caddytlsConfig := caddytls.NewConfig(n.instance)

		// Save the config to our master list, and key it for lookups
		c := &Config{
			TLS:        caddytlsConfig,
			Type:       listenType,
			ListenPort: params[0], // first element should always be the port
			Parameters: params,
		}

		n.saveConfig(k, c)
	}

	return serverBlocks, nil
}

// MakeServers uses the newly-created configs to create and return a list of server instances.
func (n *netContext) MakeServers() ([]caddy.Server, error) {
	//  create servers based on config type
	var servers []caddy.Server
	for _, cfg := range n.configs {
		switch cfg.Type {
		case "echo":
			s, err := NewEchoServer(cfg.Parameters[0], cfg)
			if err != nil {
				return nil, err
			}
			servers = append(servers, s)
		case "proxy":
			s, err := NewProxyServer(cfg.Parameters[0], cfg.Parameters[1], cfg)
			if err != nil {
				return nil, err
			}
			servers = append(servers, s)

		}
	}

	return servers, nil
}

// GetConfig gets the Config that corresponds to c.
// If none exist (should only happen in tests), then a
// new, empty one will be created.
func GetConfig(c *caddy.Controller) *Config {
	ctx := c.Context().(*netContext)
	key := strings.ToLower(c.Key)

	//only check for config if the value is proxy or echo
	//we need to do this because we specify the ports in the server block
	//and those values need to be ignored as they are also sent from caddy main process.
	if key == "echo" || key == "proxy" {
		if cfg, ok := ctx.keysToConfigs[key]; ok {
			return cfg
		}
	}

	// we should only get here if value of key in server block
	// is not echo or proxy i.e port number :12017
	// we can't return a nil because caddytls.RegisterConfigGetter will panic
	// so we return a default (blank) config value
	caddytlsConfig := caddytls.NewConfig(ctx.instance)

	return &Config{TLS: caddytlsConfig}
}
<commit_msg>fix issue 6<commit_after>package netserver

import (
	"fmt"
	"strings"

	"github.com/mholt/caddy"
	"github.com/mholt/caddy/caddyfile"
	"github.com/mholt/caddy/caddytls"
)

const serverType = "net"

// directives for the net server type
// The ordering of this list is important, host need to be called before
// tls to get the relevant hostname needed
var directives = []string{"host", "tls"}

func init() {

	caddy.RegisterServerType(serverType, caddy.ServerType{
		Directives: func() []string { return directives },
		DefaultInput: func() caddy.Input {
			return caddy.CaddyfileInput{
				ServerTypeName: serverType,
			}
		},
		NewContext: newContext,
	})

	caddy.RegisterParsingCallback(serverType, "tls", activateTLS)
	caddytls.RegisterConfigGetter(serverType, func(c *caddy.Controller) *caddytls.Config { return GetConfig(c).TLS })
}

func newContext(inst *caddy.Instance) caddy.Context {
	return &netContext{instance: inst, keysToConfigs: make(map[string]*Config)}
}

type netContext struct {
	instance *caddy.Instance
	// keysToConfigs maps an address at the top of a
	// server block (a "key") to its Config. Not all
	// Configs will be represented here, only ones
	// that appeared in the Caddyfile.
	keysToConfigs map[string]*Config

	// configs is the master list of all site configs.
	configs []*Config
}

func (n *netContext) saveConfig(key string, cfg *Config) {
	n.configs = append(n.configs, cfg)
	n.keysToConfigs[key] = cfg
}

type configTokens map[string][]string

// InspectServerBlocks make sure that everything checks out before
// executing directives and otherwise prepares the directives to
// be parsed and executed.
func (n *netContext) InspectServerBlocks(sourceFile string, serverBlocks []caddyfile.ServerBlock) ([]caddyfile.ServerBlock, error) {
	cfg := make(map[string]configTokens)

	// Example:
	// proxy :12017 :22017 {
	//	host localhost
	//	tls off
	// }
	// ServerBlock Keys will be proxy :12017 :22017 and Tokens will be host and tls

	// For each key in each server block, make a new config
	for _, sb := range serverBlocks {
		// build unique key from server block keys and join with '~' i.e echo~:12345
		key := ""
		for _, k := range sb.Keys {
			k = strings.ToLower(k)
			if key == "" {
				key = k
			} else {
				key += fmt.Sprintf("~%s", k)
			}
		}
		if _, dup := n.keysToConfigs[key]; dup {
			return serverBlocks, fmt.Errorf("duplicate key: %s", key)
		}

		tokens := make(map[string][]string)
		for k, v := range sb.Tokens {
			tokens[k] = []string{}
			for _, token := range v {
				tokens[k] = append(tokens[k], token.Text)
			}
		}

		cfg[key] = tokens
	}

	// build the actual Config from gathered data
	// key is the server block key joined by ~
	// value is the tokens (NOTE: tokens are not used at the moment)
	for k := range cfg {
		params := strings.Split(k, "~")
		listenType := params[0]
		params = params[1:]

		if len(params) == 0 {
			return serverBlocks, fmt.Errorf("invalid configuration: %s", k)
		}

		if listenType == "proxy" && len(params) < 2 {
			return serverBlocks, fmt.Errorf("invalid configuration: proxy server block expects a source and destination address")
		}

		// Make our caddytls.Config, which has a pointer to the
		// instance's certificate cache
		caddytlsConfig := caddytls.NewConfig(n.instance)

		// Save the config to our master list, and key it for lookups
		c := &Config{
			TLS:        caddytlsConfig,
			Type:       listenType,
			ListenPort: params[0], // first element should always be the port
			Parameters: params,
		}

		n.saveConfig(k, c)
	}

	return serverBlocks, nil
}

// MakeServers uses the newly-created configs to create and return a list of server instances.
func (n *netContext) MakeServers() ([]caddy.Server, error) {
	//  create servers based on config type
	var servers []caddy.Server
	for _, cfg := range n.configs {
		switch cfg.Type {
		case "echo":
			s, err := NewEchoServer(cfg.Parameters[0], cfg)
			if err != nil {
				return nil, err
			}
			servers = append(servers, s)
		case "proxy":
			s, err := NewProxyServer(cfg.Parameters[0], cfg.Parameters[1], cfg)
			if err != nil {
				return nil, err
			}
			servers = append(servers, s)

		}
	}

	return servers, nil
}

// GetConfig gets the Config that corresponds to c.
// If none exist (should only happen in tests), then a
// new, empty one will be created.
func GetConfig(c *caddy.Controller) *Config {
	ctx := c.Context().(*netContext)
	key := strings.Join(c.ServerBlockKeys, "~")

	//only check for config if the value is proxy or echo
	//we need to do this because we specify the ports in the server block
	//and those values need to be ignored as they are also sent from caddy main process.
	if strings.Contains(key, "echo") || strings.Contains(key, "proxy") {
		if cfg, ok := ctx.keysToConfigs[key]; ok {
			return cfg
		}
	}

	// we should only get here if value of key in server block
	// is not echo or proxy i.e port number :12017
	// we can't return a nil because caddytls.RegisterConfigGetter will panic
	// so we return a default (blank) config value
	caddytlsConfig := caddytls.NewConfig(ctx.instance)

	return &Config{TLS: caddytlsConfig}
}
<|endoftext|>