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

// Command xrd-ls lists directory contents on a remote xrootd server.
//
// Usage:
//
//  $> xrd-ls [OPTIONS] <dir-1> [<dir-2> [...]]
//
// Example:
//
//  $> xrd-ls root://server.example.com/some/dir
//  $> xrd-ls -l root://server.example.com/some/dir
//  $> xrd-ls -R root://server.example.com/some/dir
//  $> xrd-ls -l -R root://server.example.com/some/dir
//
// Options:
//   -R	list subdirectories recursively
//   -l	use a long listing format
package main

import (
	"context"
	"flag"
	"fmt"
	"io"
	"log"
	"os"
	"path"
	"text/tabwriter"

	"github.com/pkg/errors"
	xrdclient "go-hep.org/x/hep/xrootd/client"
	"go-hep.org/x/hep/xrootd/xrdfs"
	"go-hep.org/x/hep/xrootd/xrdio"
)

func init() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, `xrd-ls lists directory contents on a remote xrootd server.

Usage:

 $> xrd-ls [OPTIONS] <dir-1> [<dir-2> [...]]

Example:

 $> xrd-ls root://server.example.com/some/dir
 $> xrd-ls -l root://server.example.com/some/dir
 $> xrd-ls -R root://server.example.com/some/dir
 $> xrd-ls -l -R root://server.example.com/some/dir

Options:
`)
		flag.PrintDefaults()
	}
}

func main() {
	log.SetPrefix("xrd-ls: ")
	log.SetFlags(0)

	var (
		recFlag  = flag.Bool("R", false, "list subdirectories recursively")
		longFlag = flag.Bool("l", false, "use a long listing format")
	)

	flag.Parse()

	if flag.NArg() == 0 {
		flag.Usage()
		log.Fatalf("missing directory operand")
	}

	for _, dir := range flag.Args() {
		err := xrdls(dir, *longFlag, *recFlag)
		if err != nil {
			log.Fatalf("could not list %q content: %v", dir, err)
		}
	}
}

func xrdls(name string, long, recursive bool) error {
	url, err := xrdio.Parse(name)
	if err != nil {
		return errors.Errorf("could not parse %q: %v", name, err)
	}

	ctx := context.Background()

	c, err := xrdclient.NewClient(ctx, url.Addr, url.User)
	if err != nil {
		return errors.Errorf("could not create client: %v", err)
	}
	defer c.Close()

	fs := c.FS()

	fi, err := fs.Stat(ctx, url.Path)
	if err != nil {
		return errors.Errorf("could not stat %q: %v", url.Path, err)
	}
	err = display(ctx, fs, url.Path, fi, long, recursive)
	if err != nil {
		return err
	}

	return nil
}

func display(ctx context.Context, fs xrdfs.FileSystem, root string, fi os.FileInfo, long, recursive bool) error {
	if !fi.IsDir() {
		format(os.Stdout, root, fi, long)
		return nil
	}

	end := ""
	if recursive {
		end = ":"
	}

	fmt.Printf("%s%s\n", path.Join(root, fi.Name()), end)
	dir := path.Join(root, fi.Name())
	if long {
		fmt.Printf("total %d\n", fi.Size())
	}
	ents, err := fs.Dirlist(ctx, dir)
	if err != nil {
		return errors.Errorf("could not list dir %q: %v", dir, err)
	}
	o := tabwriter.NewWriter(os.Stdout, 8, 4, 0, ' ', tabwriter.AlignRight)
	for _, e := range ents {
		format(o, root, e, long)
	}
	o.Flush()
	fmt.Printf("\n")
	if recursive {
		for _, e := range ents {
			if !e.IsDir() {
				continue
			}
			err := display(ctx, fs, dir, e, long, recursive)
			if err != nil {
				return err
			}
		}
	}

	return nil
}

func format(o io.Writer, root string, fi os.FileInfo, long bool) {
	if !long {
		fmt.Fprintf(o, "%s\n", path.Join(root, fi.Name()))
		return
	}

	fmt.Fprintf(o, "%v\t %d\t %s\t %s\n", fi.Mode(), fi.Size(), fi.ModTime().Format("Jan 02 15:04"), fi.Name())
}
<commit_msg>xrootd/cmd/xrd-ls: change empty lines in printout<commit_after>// Copyright 2018 The go-hep Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Command xrd-ls lists directory contents on a remote xrootd server.
//
// Usage:
//
//  $> xrd-ls [OPTIONS] <dir-1> [<dir-2> [...]]
//
// Example:
//
//  $> xrd-ls root://server.example.com/some/dir
//  $> xrd-ls -l root://server.example.com/some/dir
//  $> xrd-ls -R root://server.example.com/some/dir
//  $> xrd-ls -l -R root://server.example.com/some/dir
//
// Options:
//   -R	list subdirectories recursively
//   -l	use a long listing format
package main

import (
	"context"
	"flag"
	"fmt"
	"io"
	"log"
	"os"
	"path"
	"text/tabwriter"

	"github.com/pkg/errors"
	xrdclient "go-hep.org/x/hep/xrootd/client"
	"go-hep.org/x/hep/xrootd/xrdfs"
	"go-hep.org/x/hep/xrootd/xrdio"
)

func init() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, `xrd-ls lists directory contents on a remote xrootd server.

Usage:

 $> xrd-ls [OPTIONS] <dir-1> [<dir-2> [...]]

Example:

 $> xrd-ls root://server.example.com/some/dir
 $> xrd-ls -l root://server.example.com/some/dir
 $> xrd-ls -R root://server.example.com/some/dir
 $> xrd-ls -l -R root://server.example.com/some/dir

Options:
`)
		flag.PrintDefaults()
	}
}

func main() {
	log.SetPrefix("xrd-ls: ")
	log.SetFlags(0)

	var (
		recFlag  = flag.Bool("R", false, "list subdirectories recursively")
		longFlag = flag.Bool("l", false, "use a long listing format")
	)

	flag.Parse()

	if flag.NArg() == 0 {
		flag.Usage()
		log.Fatalf("missing directory operand")
	}

	for i, dir := range flag.Args() {
		if i > 0 {
			// separate consecutive files by an empty line
			fmt.Printf("\n")
		}
		err := xrdls(dir, *longFlag, *recFlag)
		if err != nil {
			log.Fatalf("could not list %q content: %v", dir, err)
		}
	}
}

func xrdls(name string, long, recursive bool) error {
	url, err := xrdio.Parse(name)
	if err != nil {
		return errors.Errorf("could not parse %q: %v", name, err)
	}

	ctx := context.Background()

	c, err := xrdclient.NewClient(ctx, url.Addr, url.User)
	if err != nil {
		return errors.Errorf("could not create client: %v", err)
	}
	defer c.Close()

	fs := c.FS()

	fi, err := fs.Stat(ctx, url.Path)
	if err != nil {
		return errors.Errorf("could not stat %q: %v", url.Path, err)
	}
	err = display(ctx, fs, url.Path, fi, long, recursive)
	if err != nil {
		return err
	}

	return nil
}

func display(ctx context.Context, fs xrdfs.FileSystem, root string, fi os.FileInfo, long, recursive bool) error {
	if !fi.IsDir() {
		format(os.Stdout, root, fi, long)
		return nil
	}

	end := ""
	if recursive {
		end = ":"
	}

	fmt.Printf("%s%s\n", path.Join(root, fi.Name()), end)
	dir := path.Join(root, fi.Name())
	if long {
		fmt.Printf("total %d\n", fi.Size())
	}
	ents, err := fs.Dirlist(ctx, dir)
	if err != nil {
		return errors.Errorf("could not list dir %q: %v", dir, err)
	}
	o := tabwriter.NewWriter(os.Stdout, 8, 4, 0, ' ', tabwriter.AlignRight)
	for _, e := range ents {
		format(o, root, e, long)
	}
	o.Flush()
	if recursive {
		for _, e := range ents {
			if !e.IsDir() {
				continue
			}
			// make an empty line before going into a subdirectory.
			fmt.Printf("\n")
			err := display(ctx, fs, dir, e, long, recursive)
			if err != nil {
				return err
			}
		}
	}

	return nil
}

func format(o io.Writer, root string, fi os.FileInfo, long bool) {
	if !long {
		fmt.Fprintf(o, "%s\n", path.Join(root, fi.Name()))
		return
	}

	fmt.Fprintf(o, "%v\t %d\t %s\t %s\n", fi.Mode(), fi.Size(), fi.ModTime().Format("Jan 02 15:04"), fi.Name())
}
<|endoftext|>


<commit_before>package hydrator

import (
	"fmt"
	"os"
	"reflect"
	"strconv"
	"strings"

	"golang.org/x/net/context"

	etcd "github.com/coreos/etcd/client"
	"github.com/mcuadros/go-defaults"
)

var (
	DefaultSeparator    = "."
	Debug               = false
	DebugEnvironmentVar = "ETCD_HYDRATOR_DEBUG"
)

func init() {
	if len(os.Getenv(DebugEnvironmentVar)) != 0 {
		Debug = true
	}
}

type Hydrator struct {
	client    etcd.Client
	kAPI      etcd.KeysAPI
	filler    *defaults.Filler
	Folder    string
	Separator string
}

func NewHydrator(client etcd.Client) *Hydrator {
	h := &Hydrator{
		client:    client,
		kAPI:      etcd.NewKeysAPI(client),
		Separator: DefaultSeparator,
	}

	funcs := make(map[reflect.Kind]defaults.FillerFunc, 0)

	funcs[reflect.Bool] = func(field *defaults.FieldData) {
		raw := h.getKey(field)
		if raw == "" {
			return
		}

		value, _ := strconv.ParseBool(raw)
		field.Value.SetBool(value)
	}

	funcs[reflect.String] = func(field *defaults.FieldData) {
		raw := h.getKey(field)
		if raw == "" {
			return
		}

		field.Value.SetString(raw)
	}

	funcs[reflect.Int] = func(field *defaults.FieldData) {
		raw := h.getKey(field)
		if raw == "" {
			return
		}

		value, _ := strconv.ParseInt(raw, 10, 64)
		field.Value.SetInt(value)
	}

	funcs[reflect.Int8] = funcs[reflect.Int]
	funcs[reflect.Int16] = funcs[reflect.Int]
	funcs[reflect.Int32] = funcs[reflect.Int]
	funcs[reflect.Int64] = funcs[reflect.Int]

	funcs[reflect.Float32] = func(field *defaults.FieldData) {
		raw := h.getKey(field)
		if raw == "" {
			return
		}

		value, _ := strconv.ParseFloat(raw, 64)
		field.Value.SetFloat(value)
	}

	funcs[reflect.Float64] = funcs[reflect.Float32]

	funcs[reflect.Uint] = func(field *defaults.FieldData) {
		raw := h.getKey(field)
		if raw == "" {
			return
		}

		value, _ := strconv.ParseUint(raw, 10, 64)
		field.Value.SetUint(value)
	}

	funcs[reflect.Uint8] = funcs[reflect.Uint]
	funcs[reflect.Uint16] = funcs[reflect.Uint]
	funcs[reflect.Uint32] = funcs[reflect.Uint]
	funcs[reflect.Uint64] = funcs[reflect.Uint]

	funcs[reflect.Slice] = func(field *defaults.FieldData) {
		if field.Value.Type().Elem().Kind() == reflect.Uint8 {
			if field.Value.Bytes() != nil {
				return
			}

			raw := h.getKey(field)
			if raw == "" {
				return
			}

			field.Value.SetBytes([]byte(raw))
		}
	}

	funcs[reflect.Struct] = func(field *defaults.FieldData) {
		fields := h.filler.GetFieldsFromValue(field.Value, field)
		h.filler.SetDefaultValues(fields)
	}

	h.filler = &defaults.Filler{FuncByKind: funcs, Tag: "etcd"}

	return h
}

func (h *Hydrator) Hydrate(variable interface{}) {
	if Debug {
		fmt.Printf("Hydrating var: %q\n", reflect.TypeOf(variable).String())
	}

	h.filler.Fill(variable)
}

func (h *Hydrator) getKey(field *defaults.FieldData) string {
	key := h.buildKey(field)

	response, err := h.kAPI.Get(context.Background(), key, nil)
	if err != nil {
		fmt.Printf("%V, %s", err, err.(etcd.Error).Code)
		if eerr, ok := err.(etcd.Error); ok && eerr.Code == etcd.ErrorCodeKeyNotFound {
			fmt.Printf("Missing key %q\n", key)
			return ""
		}

		panic(err)
	}

	value := response.Node.Value

	if Debug {
		fmt.Printf("Recovered key %q with value %q\n", key, value)
	}

	return value
}

func (h *Hydrator) buildKey(field *defaults.FieldData) string {
	if h.Folder == "" {
		return h.buildFilename(field)
	}

	return fmt.Sprintf("%s/%s", h.Folder, h.buildFilename(field))
}

func (h *Hydrator) buildFilename(field *defaults.FieldData) string {
	key := field.TagValue
	if key == "" {
		key = strings.ToLower(field.Field.Name)
	}

	if field.Parent == nil {
		return key
	}

	return h.buildFilename(field.Parent) + h.Separator + key
}
<commit_msg>fixing missing key debug line<commit_after>package hydrator

import (
	"fmt"
	"os"
	"reflect"
	"strconv"
	"strings"

	"golang.org/x/net/context"

	etcd "github.com/coreos/etcd/client"
	"github.com/mcuadros/go-defaults"
)

var (
	DefaultSeparator    = "."
	Debug               = false
	DebugEnvironmentVar = "ETCD_HYDRATOR_DEBUG"
)

func init() {
	if len(os.Getenv(DebugEnvironmentVar)) != 0 {
		Debug = true
	}
}

type Hydrator struct {
	client    etcd.Client
	kAPI      etcd.KeysAPI
	filler    *defaults.Filler
	Folder    string
	Separator string
}

func NewHydrator(client etcd.Client) *Hydrator {
	h := &Hydrator{
		client:    client,
		kAPI:      etcd.NewKeysAPI(client),
		Separator: DefaultSeparator,
	}

	funcs := make(map[reflect.Kind]defaults.FillerFunc, 0)

	funcs[reflect.Bool] = func(field *defaults.FieldData) {
		raw := h.getKey(field)
		if raw == "" {
			return
		}

		value, _ := strconv.ParseBool(raw)
		field.Value.SetBool(value)
	}

	funcs[reflect.String] = func(field *defaults.FieldData) {
		raw := h.getKey(field)
		if raw == "" {
			return
		}

		field.Value.SetString(raw)
	}

	funcs[reflect.Int] = func(field *defaults.FieldData) {
		raw := h.getKey(field)
		if raw == "" {
			return
		}

		value, _ := strconv.ParseInt(raw, 10, 64)
		field.Value.SetInt(value)
	}

	funcs[reflect.Int8] = funcs[reflect.Int]
	funcs[reflect.Int16] = funcs[reflect.Int]
	funcs[reflect.Int32] = funcs[reflect.Int]
	funcs[reflect.Int64] = funcs[reflect.Int]

	funcs[reflect.Float32] = func(field *defaults.FieldData) {
		raw := h.getKey(field)
		if raw == "" {
			return
		}

		value, _ := strconv.ParseFloat(raw, 64)
		field.Value.SetFloat(value)
	}

	funcs[reflect.Float64] = funcs[reflect.Float32]

	funcs[reflect.Uint] = func(field *defaults.FieldData) {
		raw := h.getKey(field)
		if raw == "" {
			return
		}

		value, _ := strconv.ParseUint(raw, 10, 64)
		field.Value.SetUint(value)
	}

	funcs[reflect.Uint8] = funcs[reflect.Uint]
	funcs[reflect.Uint16] = funcs[reflect.Uint]
	funcs[reflect.Uint32] = funcs[reflect.Uint]
	funcs[reflect.Uint64] = funcs[reflect.Uint]

	funcs[reflect.Slice] = func(field *defaults.FieldData) {
		if field.Value.Type().Elem().Kind() == reflect.Uint8 {
			if field.Value.Bytes() != nil {
				return
			}

			raw := h.getKey(field)
			if raw == "" {
				return
			}

			field.Value.SetBytes([]byte(raw))
		}
	}

	funcs[reflect.Struct] = func(field *defaults.FieldData) {
		fields := h.filler.GetFieldsFromValue(field.Value, field)
		h.filler.SetDefaultValues(fields)
	}

	h.filler = &defaults.Filler{FuncByKind: funcs, Tag: "etcd"}

	return h
}

func (h *Hydrator) Hydrate(variable interface{}) {
	if Debug {
		fmt.Printf("Hydrating var: %q\n", reflect.TypeOf(variable).String())
	}

	h.filler.Fill(variable)
}

func (h *Hydrator) getKey(field *defaults.FieldData) string {
	key := h.buildKey(field)

	response, err := h.kAPI.Get(context.Background(), key, nil)
	if err != nil {
		if eerr, ok := err.(etcd.Error); ok && eerr.Code == etcd.ErrorCodeKeyNotFound {
			if Debug {
				fmt.Printf("Missing key %q\n", key)
			}

			return ""
		}

		panic(err)
	}

	value := response.Node.Value

	if Debug {
		fmt.Printf("Recovered key %q with value %q\n", key, value)
	}

	return value
}

func (h *Hydrator) buildKey(field *defaults.FieldData) string {
	if h.Folder == "" {
		return h.buildFilename(field)
	}

	return fmt.Sprintf("%s/%s", h.Folder, h.buildFilename(field))
}

func (h *Hydrator) buildFilename(field *defaults.FieldData) string {
	key := field.TagValue
	if key == "" {
		key = strings.ToLower(field.Field.Name)
	}

	if field.Parent == nil {
		return key
	}

	return h.buildFilename(field.Parent) + h.Separator + key
}
<|endoftext|>


<commit_before>/* Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.  See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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 siesta

import (
	"math/rand"
	"testing"
)

var numValues = 100

func TestInt8EncodingDecoding(t *testing.T) {
	buffer := make([]byte, numValues)
	encoder := NewBinaryEncoder(buffer)

	randValues := make([]int8, numValues)
	for i := 0; i < numValues; i++ {
		randValue := int8(rand.Int())
		encoder.WriteInt8(randValue)
		randValues[i] = randValue
	}

	decoder := NewBinaryDecoder(buffer)
	for i := 0; i < numValues; i++ {
		value, err := decoder.GetInt8()
		checkErr(t, err)
		assert(t, value, randValues[i])
	}
}

func TestInt16EncodingDecoding(t *testing.T) {
	buffer := make([]byte, numValues*2)
	encoder := NewBinaryEncoder(buffer)

	randValues := make([]int16, numValues)
	for i := 0; i < numValues; i++ {
		randValue := int16(rand.Int())
		encoder.WriteInt16(randValue)
		randValues[i] = randValue
	}

	decoder := NewBinaryDecoder(buffer)
	for i := 0; i < numValues; i++ {
		value, err := decoder.GetInt16()
		checkErr(t, err)
		assert(t, value, randValues[i])
	}
}

func TestInt32EncodingDecoding(t *testing.T) {
	buffer := make([]byte, numValues*4)
	encoder := NewBinaryEncoder(buffer)

	randValues := make([]int32, numValues)
	for i := 0; i < numValues; i++ {
		randValue := int32(rand.Int())
		encoder.WriteInt32(randValue)
		randValues[i] = randValue
	}

	decoder := NewBinaryDecoder(buffer)
	for i := 0; i < numValues; i++ {
		value, err := decoder.GetInt32()
		checkErr(t, err)
		assert(t, value, randValues[i])
	}
}

func TestInt64EncodingDecoding(t *testing.T) {
	buffer := make([]byte, numValues*8)
	encoder := NewBinaryEncoder(buffer)

	randValues := make([]int64, numValues)
	for i := 0; i < numValues; i++ {
		randValue := int64(rand.Int())
		encoder.WriteInt64(randValue)
		randValues[i] = randValue
	}

	decoder := NewBinaryDecoder(buffer)
	for i := 0; i < numValues; i++ {
		value, err := decoder.GetInt64()
		checkErr(t, err)
		assert(t, value, randValues[i])
	}
}

func TestStringEncodingDecoding(t *testing.T) {
	buffer := make([]byte, numValues*2+((numValues*(numValues+1))/2))
	encoder := NewBinaryEncoder(buffer)

	randValues := make([]string, numValues)
	for i := 0; i < numValues; i++ {
		randValue := randomString(i)
		encoder.WriteString(randValue)
		randValues[i] = randValue
	}

	decoder := NewBinaryDecoder(buffer)
	for i := 0; i < numValues; i++ {
		value, err := decoder.GetString()
		checkErr(t, err)
		assert(t, value, randValues[i])
	}
}

func TestBytesEncodingDecoding(t *testing.T) {
	buffer := make([]byte, numValues*4+((numValues*(numValues+1))/2))
	encoder := NewBinaryEncoder(buffer)

	randValues := make([][]byte, numValues)
	for i := 0; i < numValues; i++ {
		randValue := randomBytes(i)
		encoder.WriteBytes(randValue)
		randValues[i] = randValue
	}

	decoder := NewBinaryDecoder(buffer)
	for i := 0; i < numValues; i++ {
		value, err := decoder.GetBytes()
		checkErr(t, err)
		assert(t, value, randValues[i])
	}
}
<commit_msg>added tests for sizing encoder. re #5<commit_after>/* Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.  See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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 siesta

import (
	"math/rand"
	"testing"
)

var numValues = 100

func TestInt8EncodingDecoding(t *testing.T) {
	buffer := make([]byte, numValues)
	encoder := NewBinaryEncoder(buffer)

	randValues := make([]int8, numValues)
	for i := 0; i < numValues; i++ {
		randValue := int8(rand.Int())
		encoder.WriteInt8(randValue)
		randValues[i] = randValue
	}

	decoder := NewBinaryDecoder(buffer)
	for i := 0; i < numValues; i++ {
		value, err := decoder.GetInt8()
		checkErr(t, err)
		assert(t, value, randValues[i])
	}
}

func TestInt16EncodingDecoding(t *testing.T) {
	buffer := make([]byte, numValues*2)
	encoder := NewBinaryEncoder(buffer)

	randValues := make([]int16, numValues)
	for i := 0; i < numValues; i++ {
		randValue := int16(rand.Int())
		encoder.WriteInt16(randValue)
		randValues[i] = randValue
	}

	decoder := NewBinaryDecoder(buffer)
	for i := 0; i < numValues; i++ {
		value, err := decoder.GetInt16()
		checkErr(t, err)
		assert(t, value, randValues[i])
	}
}

func TestInt32EncodingDecoding(t *testing.T) {
	buffer := make([]byte, numValues*4)
	encoder := NewBinaryEncoder(buffer)

	randValues := make([]int32, numValues)
	for i := 0; i < numValues; i++ {
		randValue := int32(rand.Int())
		encoder.WriteInt32(randValue)
		randValues[i] = randValue
	}

	decoder := NewBinaryDecoder(buffer)
	for i := 0; i < numValues; i++ {
		value, err := decoder.GetInt32()
		checkErr(t, err)
		assert(t, value, randValues[i])
	}
}

func TestInt64EncodingDecoding(t *testing.T) {
	buffer := make([]byte, numValues*8)
	encoder := NewBinaryEncoder(buffer)

	randValues := make([]int64, numValues)
	for i := 0; i < numValues; i++ {
		randValue := int64(rand.Int())
		encoder.WriteInt64(randValue)
		randValues[i] = randValue
	}

	decoder := NewBinaryDecoder(buffer)
	for i := 0; i < numValues; i++ {
		value, err := decoder.GetInt64()
		checkErr(t, err)
		assert(t, value, randValues[i])
	}
}

func TestStringEncodingDecoding(t *testing.T) {
	buffer := make([]byte, (numValues+1)*2+((numValues*(numValues+1))/2))
	encoder := NewBinaryEncoder(buffer)

	randValues := make([]string, numValues+1)
	for i := 0; i <= numValues; i++ {
		randValue := randomString(i)
		encoder.WriteString(randValue)
		randValues[i] = randValue
	}

	decoder := NewBinaryDecoder(buffer)
	for i := 0; i <= numValues; i++ {
		value, err := decoder.GetString()
		checkErr(t, err)
		assert(t, value, randValues[i])
	}
}

func TestBytesEncodingDecoding(t *testing.T) {
	buffer := make([]byte, numValues*4+((numValues*(numValues+1))/2))
	encoder := NewBinaryEncoder(buffer)

	randValues := make([][]byte, numValues)
	for i := 0; i < numValues; i++ {
		randValue := randomBytes(i)
		encoder.WriteBytes(randValue)
		randValues[i] = randValue
	}

	decoder := NewBinaryDecoder(buffer)
	for i := 0; i < numValues; i++ {
		value, err := decoder.GetBytes()
		checkErr(t, err)
		assert(t, value, randValues[i])
	}
}

func TestSizingEncoder(t *testing.T) {
	int8encoder := NewSizingEncoder()
	for i := 0; i < numValues; i++ {
		int8encoder.WriteInt8(0)
	}
	assert(t, int8encoder.Size(), numValues)

	int16encoder := NewSizingEncoder()
	for i := 0; i < numValues; i++ {
		int16encoder.WriteInt16(0)
	}
	assert(t, int16encoder.Size(), numValues*2)

	int32encoder := NewSizingEncoder()
	for i := 0; i < numValues; i++ {
		int32encoder.WriteInt32(0)
	}
	assert(t, int32encoder.Size(), numValues*4)

	int64encoder := NewSizingEncoder()
	for i := 0; i < numValues; i++ {
		int64encoder.WriteInt64(0)
	}
	assert(t, int64encoder.Size(), numValues*8)

	stringEncoder := NewSizingEncoder()
	for i := 0; i <= numValues; i++ {
		stringEncoder.WriteString(randomString(i))
	}
	//we encode N strings with length from 0 to N, so the Size() should return (numValues+1)*2 which is size for int16 string lengths including the empty one
	//and N*(N+1)/2 for actual string values
	assert(t, stringEncoder.Size(), (numValues+1)*2+((numValues*(numValues+1))/2))

	bytesEncoder := NewSizingEncoder()
	for i := 0; i <= numValues; i++ {
		bytesEncoder.WriteBytes(randomBytes(i))
	}
	//we encode N arrays with length from 0 to N, so the Size() should return (numValues+1)*4 which is size for int32 arrays lengths including the empty one
	//and N*(N+1)/2 for actual arrays
	assert(t, bytesEncoder.Size(), (numValues+1)*4+((numValues*(numValues+1))/2))
}
<|endoftext|>


<commit_before>package raymond

import (
	"fmt"
	"io"
	"reflect"

	"github.com/aymerick/raymond/ast"
)

var (
	// @note borrowed from https://github.com/golang/go/tree/master/src/text/template/exec.go
	errorType       = reflect.TypeOf((*error)(nil)).Elem()
	fmtStringerType = reflect.TypeOf((*fmt.Stringer)(nil)).Elem()

	zero reflect.Value
)

// Template evaluation visitor
type EvalVisitor struct {
	wr   io.Writer
	tpl  *Template
	data interface{}
	ctx  []reflect.Value

	curNode  ast.Node
	curBlock *ast.BlockStatement
}

// Instanciate a new evaluation visitor
func NewEvalVisitor(wr io.Writer, tpl *Template, data interface{}) *EvalVisitor {
	return &EvalVisitor{
		wr:   wr,
		tpl:  tpl,
		data: data,
		ctx:  []reflect.Value{reflect.ValueOf(data)},
	}
}

func (v *EvalVisitor) pushCtx(ctx reflect.Value) {
	v.ctx = append(v.ctx, ctx)
}

func (v *EvalVisitor) popCtx() reflect.Value {
	var result reflect.Value

	result, v.ctx = v.ctx[len(v.ctx)-1], v.ctx[:len(v.ctx)-1]
	return result
}

func (v *EvalVisitor) curCtx() reflect.Value {
	return v.ctx[len(v.ctx)-1]
}

// fatal evaluation error
func (v *EvalVisitor) errPanic(err error) {
	panic(fmt.Errorf("Evaluation error: %s\nCurrent node:\n\t%s", err, v.curNode))
}

// fatal evaluation error message
func (v *EvalVisitor) errorf(format string, args ...interface{}) {
	v.errPanic(fmt.Errorf(format, args...))
}

// set current node
func (v *EvalVisitor) at(node ast.Node) {
	// log.Printf("at: %s", node)
	v.curNode = node
}

// evaluates field in given context
func (v *EvalVisitor) evalField(ctx reflect.Value, fieldName string) reflect.Value {
	result := zero

	if !ctx.IsValid() {
		return result
	}

	switch ctx.Kind() {
	case reflect.Struct:
		tField, ok := ctx.Type().FieldByName(fieldName)
		if !ok {
			v.errorf("%s is not a field of struct type %s", fieldName, ctx.Type())
		}

		if tField.PkgPath != "" {
			// field is unexported
			v.errorf("%s is an unexported field of struct type %s", fieldName, ctx.Type())
		}

		// struct field
		result = ctx.FieldByIndex(tField.Index)
	case reflect.Map:
		nameVal := reflect.ValueOf(fieldName)
		if nameVal.Type().AssignableTo(ctx.Type().Key()) {
			// map key
			result = ctx.MapIndex(nameVal)
		}
	}

	return result
}

func (v *EvalVisitor) strValue(value reflect.Value) string {
	result := ""

	ival, ok := printableValue(value)
	if !ok {
		v.errorf("Can't print value")
	}

	val := reflect.ValueOf(ival)

	switch val.Kind() {
	case reflect.Array, reflect.Slice:
		for i := 0; i < val.Len(); i++ {
			result += val.Index(i).String()
		}
	case reflect.Bool:
		result = "false"
		if val.Bool() {
			result = "true"
		}
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
		result = fmt.Sprintf("%d", ival)
	case reflect.Float32, reflect.Float64:
		result = fmt.Sprintf("%f", ival)
	case reflect.Invalid:
		result = ""
	default:
		result = fmt.Sprintf("%s", ival)
	}

	// log.Printf("strValue(%q) => %s", ival, result)

	return result
}

// printableValue returns the, possibly indirected, interface value inside v that
// is best for a call to formatted printer.
//
// @note borrowed from https://github.com/golang/go/tree/master/src/text/template/exec.go
func printableValue(v reflect.Value) (interface{}, bool) {
	if v.Kind() == reflect.Ptr {
		v, _ = indirect(v) // fmt.Fprint handles nil.
	}
	if !v.IsValid() {
		return "", true
	}

	if !v.Type().Implements(errorType) && !v.Type().Implements(fmtStringerType) {
		if v.CanAddr() && (reflect.PtrTo(v.Type()).Implements(errorType) || reflect.PtrTo(v.Type()).Implements(fmtStringerType)) {
			v = v.Addr()
		} else {
			switch v.Kind() {
			case reflect.Chan, reflect.Func:
				return nil, false
			}
		}
	}
	return v.Interface(), true
}

// indirect returns the item at the end of indirection, and a bool to indicate if it's nil.
// We indirect through pointers and empty interfaces (only) because
// non-empty interfaces have methods we might need.
//
// @note borrowed from https://github.com/golang/go/tree/master/src/text/template/exec.go
func indirect(v reflect.Value) (rv reflect.Value, isNil bool) {
	for ; v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface; v = v.Elem() {
		if v.IsNil() {
			return v, true
		}
		if v.Kind() == reflect.Interface && v.NumMethod() > 0 {
			break
		}
	}
	return v, false
}

// IsTruth reports whether the value is 'true', in the sense of not the zero of its type,
// and whether the value has a meaningful truth value.
//
// @note borrowed from https://github.com/golang/go/tree/master/src/text/template/exec.go
func IsTruth(val reflect.Value) (truth, ok bool) {
	if !val.IsValid() {
		// Something like var x interface{}, never set. It's a form of nil.
		return false, true
	}
	switch val.Kind() {
	case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
		truth = val.Len() > 0
	case reflect.Bool:
		truth = val.Bool()
	case reflect.Complex64, reflect.Complex128:
		truth = val.Complex() != 0
	case reflect.Chan, reflect.Func, reflect.Ptr, reflect.Interface:
		truth = !val.IsNil()
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		truth = val.Int() != 0
	case reflect.Float32, reflect.Float64:
		truth = val.Float() != 0
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
		truth = val.Uint() != 0
	case reflect.Struct:
		truth = true // Struct values are always true.
	default:
		return
	}
	return truth, true
}

// Finds given helper
func (v *EvalVisitor) findHelper(name string) Helper {
	// check template helpers
	if v.tpl.helpers[name] != nil {
		return v.tpl.helpers[name]
	}

	// check global helpers
	return FindHelper(name)
}

// Computes helper parameters from an expression
func (v *EvalVisitor) helperParams(node *ast.Expression) *HelperParams {
	var params []interface{}
	var hash map[string]interface{}

	for _, paramNode := range node.Params {
		param := paramNode.Accept(v)
		params = append(params, param)
	}

	// @todo Fill hash

	return NewHelperParams(v, params, hash)
}

//
// Visitor interface
//

// Statements

func (v *EvalVisitor) VisitProgram(node *ast.Program) interface{} {
	v.at(node)

	for _, n := range node.Body {
		n.Accept(v)
	}

	return nil
}

func (v *EvalVisitor) VisitMustache(node *ast.MustacheStatement) interface{} {
	v.at(node)

	// evaluate expression
	val := reflect.ValueOf(node.Expression.Accept(v))

	str := v.strValue(val)

	// write result
	if _, err := v.wr.Write([]byte(str)); err != nil {
		v.errPanic(err)
	}

	return nil
}

func (v *EvalVisitor) VisitBlock(node *ast.BlockStatement) interface{} {
	v.at(node)
	v.curBlock = node

	// evaluate expression
	val := reflect.ValueOf(node.Expression.Accept(v))

	v.pushCtx(val)

	truth, _ := IsTruth(val)
	if truth && (node.Program != nil) {
		node.Program.Accept(v)
	} else if node.Inverse != nil {
		node.Inverse.Accept(v)
	}

	v.popCtx()

	v.curBlock = nil

	return nil
}

func (v *EvalVisitor) VisitPartial(node *ast.PartialStatement) interface{} {
	v.at(node)

	// @todo
	return nil
}

func (v *EvalVisitor) VisitContent(node *ast.ContentStatement) interface{} {
	v.at(node)

	// write content as is
	if _, err := v.wr.Write([]byte(node.Value)); err != nil {
		v.errPanic(err)
	}

	return nil
}

func (v *EvalVisitor) VisitComment(node *ast.CommentStatement) interface{} {
	v.at(node)

	// ignore comments
	return ""
}

// Expressions

func (v *EvalVisitor) VisitExpression(node *ast.Expression) interface{} {
	v.at(node)

	// check if this is an helper
	if helperName := node.HelperName(); helperName != "" {
		if helper := v.findHelper(helperName); helper != nil {
			// call helper function
			return helper(v.helperParams(node))
		}
	}

	// field path
	if path := node.FieldPath(); path != nil {
		if val := path.Accept(v); val != nil {
			return val
		}

		// erroneous field path
		return nil
	}

	// literal
	if literal, ok := node.LiteralStr(); ok {
		if val := v.evalField(v.curCtx(), literal); val.IsValid() {
			return val.Interface()
		}

		// erroneous literal
		return nil
	}

	// wat
	return nil
}

func (v *EvalVisitor) VisitSubExpression(node *ast.SubExpression) interface{} {
	v.at(node)

	// @todo
	return nil
}

func (v *EvalVisitor) VisitPath(node *ast.PathExpression) interface{} {
	v.at(node)

	ctx := v.curCtx()

	for i := 0; i < len(node.Parts); i++ {
		ctx = v.evalField(ctx, node.Parts[i])
		if !ctx.IsValid() {
			break
		}
	}

	if !ctx.IsValid() {
		return nil
	}

	return ctx.Interface()
}

// Literals

func (v *EvalVisitor) VisitString(node *ast.StringLiteral) interface{} {
	v.at(node)

	return node.Value
}

func (v *EvalVisitor) VisitBoolean(node *ast.BooleanLiteral) interface{} {
	v.at(node)

	return node.Value
}

func (v *EvalVisitor) VisitNumber(node *ast.NumberLiteral) interface{} {
	v.at(node)

	return node.Number()
}

// Miscellaneous

func (v *EvalVisitor) VisitHash(node *ast.Hash) interface{} {
	v.at(node)

	// @todo
	return nil
}

func (v *EvalVisitor) VisitHashPair(node *ast.HashPair) interface{} {
	v.at(node)

	// @todo
	return nil
}
<commit_msg>Fixes comments<commit_after>package raymond

import (
	"fmt"
	"io"
	"reflect"

	"github.com/aymerick/raymond/ast"
)

var (
	// @note borrowed from https://github.com/golang/go/tree/master/src/text/template/exec.go
	errorType       = reflect.TypeOf((*error)(nil)).Elem()
	fmtStringerType = reflect.TypeOf((*fmt.Stringer)(nil)).Elem()

	zero reflect.Value
)

// Template evaluation visitor
type EvalVisitor struct {
	wr   io.Writer
	tpl  *Template
	data interface{}
	ctx  []reflect.Value

	curNode  ast.Node
	curBlock *ast.BlockStatement
}

// Instanciate a new evaluation visitor
func NewEvalVisitor(wr io.Writer, tpl *Template, data interface{}) *EvalVisitor {
	return &EvalVisitor{
		wr:   wr,
		tpl:  tpl,
		data: data,
		ctx:  []reflect.Value{reflect.ValueOf(data)},
	}
}

func (v *EvalVisitor) pushCtx(ctx reflect.Value) {
	v.ctx = append(v.ctx, ctx)
}

func (v *EvalVisitor) popCtx() reflect.Value {
	var result reflect.Value

	result, v.ctx = v.ctx[len(v.ctx)-1], v.ctx[:len(v.ctx)-1]
	return result
}

func (v *EvalVisitor) curCtx() reflect.Value {
	return v.ctx[len(v.ctx)-1]
}

// fatal evaluation error
func (v *EvalVisitor) errPanic(err error) {
	panic(fmt.Errorf("Evaluation error: %s\nCurrent node:\n\t%s", err, v.curNode))
}

// fatal evaluation error message
func (v *EvalVisitor) errorf(format string, args ...interface{}) {
	v.errPanic(fmt.Errorf(format, args...))
}

// set current node
func (v *EvalVisitor) at(node ast.Node) {
	// log.Printf("at: %s", node)
	v.curNode = node
}

// evaluates field in given context
func (v *EvalVisitor) evalField(ctx reflect.Value, fieldName string) reflect.Value {
	result := zero

	if !ctx.IsValid() {
		return result
	}

	switch ctx.Kind() {
	case reflect.Struct:
		tField, ok := ctx.Type().FieldByName(fieldName)
		if !ok {
			v.errorf("%s is not a field of struct type %s", fieldName, ctx.Type())
		}

		if tField.PkgPath != "" {
			// field is unexported
			v.errorf("%s is an unexported field of struct type %s", fieldName, ctx.Type())
		}

		// struct field
		result = ctx.FieldByIndex(tField.Index)
	case reflect.Map:
		nameVal := reflect.ValueOf(fieldName)
		if nameVal.Type().AssignableTo(ctx.Type().Key()) {
			// map key
			result = ctx.MapIndex(nameVal)
		}
	}

	return result
}

func (v *EvalVisitor) strValue(value reflect.Value) string {
	result := ""

	ival, ok := printableValue(value)
	if !ok {
		v.errorf("Can't print value")
	}

	val := reflect.ValueOf(ival)

	switch val.Kind() {
	case reflect.Array, reflect.Slice:
		for i := 0; i < val.Len(); i++ {
			result += val.Index(i).String()
		}
	case reflect.Bool:
		result = "false"
		if val.Bool() {
			result = "true"
		}
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
		result = fmt.Sprintf("%d", ival)
	case reflect.Float32, reflect.Float64:
		result = fmt.Sprintf("%f", ival)
	case reflect.Invalid:
		result = ""
	default:
		result = fmt.Sprintf("%s", ival)
	}

	// log.Printf("strValue(%q) => %s", ival, result)

	return result
}

// printableValue returns the, possibly indirected, interface value inside v that
// is best for a call to formatted printer.
//
// @note borrowed from https://github.com/golang/go/tree/master/src/text/template/exec.go
func printableValue(v reflect.Value) (interface{}, bool) {
	if v.Kind() == reflect.Ptr {
		v, _ = indirect(v) // fmt.Fprint handles nil.
	}
	if !v.IsValid() {
		return "", true
	}

	if !v.Type().Implements(errorType) && !v.Type().Implements(fmtStringerType) {
		if v.CanAddr() && (reflect.PtrTo(v.Type()).Implements(errorType) || reflect.PtrTo(v.Type()).Implements(fmtStringerType)) {
			v = v.Addr()
		} else {
			switch v.Kind() {
			case reflect.Chan, reflect.Func:
				return nil, false
			}
		}
	}
	return v.Interface(), true
}

// indirect returns the item at the end of indirection, and a bool to indicate if it's nil.
// We indirect through pointers and empty interfaces (only) because
// non-empty interfaces have methods we might need.
//
// @note borrowed from https://github.com/golang/go/tree/master/src/text/template/exec.go
func indirect(v reflect.Value) (rv reflect.Value, isNil bool) {
	for ; v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface; v = v.Elem() {
		if v.IsNil() {
			return v, true
		}
		if v.Kind() == reflect.Interface && v.NumMethod() > 0 {
			break
		}
	}
	return v, false
}

// IsTruth reports whether the value is 'true', in the sense of not the zero of its type,
// and whether the value has a meaningful truth value.
//
// @note borrowed from https://github.com/golang/go/tree/master/src/text/template/exec.go
func IsTruth(val reflect.Value) (truth, ok bool) {
	if !val.IsValid() {
		// Something like var x interface{}, never set. It's a form of nil.
		return false, true
	}
	switch val.Kind() {
	case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
		truth = val.Len() > 0
	case reflect.Bool:
		truth = val.Bool()
	case reflect.Complex64, reflect.Complex128:
		truth = val.Complex() != 0
	case reflect.Chan, reflect.Func, reflect.Ptr, reflect.Interface:
		truth = !val.IsNil()
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		truth = val.Int() != 0
	case reflect.Float32, reflect.Float64:
		truth = val.Float() != 0
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
		truth = val.Uint() != 0
	case reflect.Struct:
		truth = true // Struct values are always true.
	default:
		return
	}
	return truth, true
}

// Finds given helper
func (v *EvalVisitor) findHelper(name string) Helper {
	// check template helpers
	if v.tpl.helpers[name] != nil {
		return v.tpl.helpers[name]
	}

	// check global helpers
	return FindHelper(name)
}

// Computes helper parameters from an expression
func (v *EvalVisitor) helperParams(node *ast.Expression) *HelperParams {
	var params []interface{}
	var hash map[string]interface{}

	for _, paramNode := range node.Params {
		param := paramNode.Accept(v)
		params = append(params, param)
	}

	// @todo Fill hash

	return NewHelperParams(v, params, hash)
}

//
// Visitor interface
//

// Statements

func (v *EvalVisitor) VisitProgram(node *ast.Program) interface{} {
	v.at(node)

	for _, n := range node.Body {
		n.Accept(v)
	}

	return nil
}

func (v *EvalVisitor) VisitMustache(node *ast.MustacheStatement) interface{} {
	v.at(node)

	// evaluate expression
	val := reflect.ValueOf(node.Expression.Accept(v))

	str := v.strValue(val)

	// write result
	if _, err := v.wr.Write([]byte(str)); err != nil {
		v.errPanic(err)
	}

	return nil
}

func (v *EvalVisitor) VisitBlock(node *ast.BlockStatement) interface{} {
	v.at(node)
	v.curBlock = node

	// evaluate expression
	val := reflect.ValueOf(node.Expression.Accept(v))

	v.pushCtx(val)

	truth, _ := IsTruth(val)
	if truth && (node.Program != nil) {
		node.Program.Accept(v)
	} else if node.Inverse != nil {
		node.Inverse.Accept(v)
	}

	v.popCtx()

	v.curBlock = nil

	return nil
}

func (v *EvalVisitor) VisitPartial(node *ast.PartialStatement) interface{} {
	v.at(node)

	// @todo
	return nil
}

func (v *EvalVisitor) VisitContent(node *ast.ContentStatement) interface{} {
	v.at(node)

	// write content as is
	if _, err := v.wr.Write([]byte(node.Value)); err != nil {
		v.errPanic(err)
	}

	return nil
}

func (v *EvalVisitor) VisitComment(node *ast.CommentStatement) interface{} {
	v.at(node)

	// ignore comments
	return ""
}

// Expressions

func (v *EvalVisitor) VisitExpression(node *ast.Expression) interface{} {
	v.at(node)

	// check if this is an helper
	if helperName := node.HelperName(); helperName != "" {
		if helper := v.findHelper(helperName); helper != nil {
			// call helper function
			return helper(v.helperParams(node))
		}
	}

	// field path
	if path := node.FieldPath(); path != nil {
		if val := path.Accept(v); val != nil {
			return val
		}

		// invalid field path
		return nil
	}

	// literal
	if literal, ok := node.LiteralStr(); ok {
		if val := v.evalField(v.curCtx(), literal); val.IsValid() {
			return val.Interface()
		}

		return nil
	}

	// wat
	return nil
}

func (v *EvalVisitor) VisitSubExpression(node *ast.SubExpression) interface{} {
	v.at(node)

	// @todo
	return nil
}

func (v *EvalVisitor) VisitPath(node *ast.PathExpression) interface{} {
	v.at(node)

	ctx := v.curCtx()

	for i := 0; i < len(node.Parts); i++ {
		ctx = v.evalField(ctx, node.Parts[i])
		if !ctx.IsValid() {
			break
		}
	}

	if !ctx.IsValid() {
		return nil
	}

	return ctx.Interface()
}

// Literals

func (v *EvalVisitor) VisitString(node *ast.StringLiteral) interface{} {
	v.at(node)

	return node.Value
}

func (v *EvalVisitor) VisitBoolean(node *ast.BooleanLiteral) interface{} {
	v.at(node)

	return node.Value
}

func (v *EvalVisitor) VisitNumber(node *ast.NumberLiteral) interface{} {
	v.at(node)

	return node.Number()
}

// Miscellaneous

func (v *EvalVisitor) VisitHash(node *ast.Hash) interface{} {
	v.at(node)

	// @todo
	return nil
}

func (v *EvalVisitor) VisitHashPair(node *ast.HashPair) interface{} {
	v.at(node)

	// @todo
	return nil
}
<|endoftext|>


<commit_before>package main

import (
	"encoding/json"
	"errors"
	"flag"
	"fmt"
	"net"
	"time"

	"github.com/dustin/go-coap"
	"github.com/golang/glog"

	"github.com/couchbaselabs/consolio/types"
)

type wireEvent struct {
	DB   string
	Type string
	Data *json.RawMessage
}

var NotFound = errors.New("not found")

var evListen = flag.String("evin", ":8675", "Event input binding")

func evInfoHandler(l *net.UDPConn, a *net.UDPAddr, m *coap.Message) *coap.Message {
	if db == nil {
		glog.Warning("db isn't ready")
		return nil
	}

	// Verify we got something sensible
	cType, ok := m.Option(coap.ContentType).(uint32)
	if !ok || coap.MediaType(cType) != coap.AppJSON {
		glog.Warningf("Got invalid content type on request: %v",
			coap.MediaType(cType))
		return nil
	}

	we := wireEvent{}
	err := json.Unmarshal(m.Payload, &we)
	if err != nil {
		glog.Warningf("Error unmarshaling %s: %v", m.Payload, err)
		return nil
	}

	if we.DB == db.Name {
		glog.Infof("Skipping update for my own bucket: %s", m.Payload)
		return nil
	}

	err = db.Update("db-"+we.DB, 0, func(current []byte) ([]byte, error) {
		if len(current) == 0 {
			return nil, NotFound
		}

		item := consolio.Item{}
		err := json.Unmarshal(current, &item)
		if err != nil {
			return nil, err
		}

		switch we.Type {
		case "state":
			// TODO:  Implement state tracking
			item.LastChange = time.Now().UTC()
			glog.Infof("Got state change for %q: %s", we.DB, *we.Data)
		case "stats":
			item.LastStat = time.Now().UTC()
			glog.Infof("Got stats for %q: %s", we.DB, *we.Data)
			item.Stats = we.Data
		default:
			return nil, fmt.Errorf("Unknown event type: %q", we.Type)
		}

		return json.Marshal(&item)
	})

	if err != nil {
		glog.Warningf("Error updating %q as %v: %v: %v",
			we.DB, we.Type, *we.Data, err)
		return nil
	}

	var rv *coap.Message
	if m.IsConfirmable() {
		rv = &coap.Message{
			Type:      coap.Acknowledgement,
			Code:      coap.Created,
			MessageID: m.MessageID,
		}
	}
	return rv
}

func eventListener() {
	if *evListen == "" {
		return
	}

	handler := coap.NewServeMux()
	handler.HandleFunc("/dbinfo", evInfoHandler)

	glog.Fatal(coap.ListenAndServe("udp", *evListen, handler))
}
<commit_msg>Log event data on error as a string<commit_after>package main

import (
	"encoding/json"
	"errors"
	"flag"
	"fmt"
	"net"
	"time"

	"github.com/dustin/go-coap"
	"github.com/golang/glog"

	"github.com/couchbaselabs/consolio/types"
)

type wireEvent struct {
	DB   string
	Type string
	Data *json.RawMessage
}

var NotFound = errors.New("not found")

var evListen = flag.String("evin", ":8675", "Event input binding")

func evInfoHandler(l *net.UDPConn, a *net.UDPAddr, m *coap.Message) *coap.Message {
	if db == nil {
		glog.Warning("db isn't ready")
		return nil
	}

	// Verify we got something sensible
	cType, ok := m.Option(coap.ContentType).(uint32)
	if !ok || coap.MediaType(cType) != coap.AppJSON {
		glog.Warningf("Got invalid content type on request: %v",
			coap.MediaType(cType))
		return nil
	}

	we := wireEvent{}
	err := json.Unmarshal(m.Payload, &we)
	if err != nil {
		glog.Warningf("Error unmarshaling %s: %v", m.Payload, err)
		return nil
	}

	if we.DB == db.Name {
		glog.Infof("Skipping update for my own bucket: %s", m.Payload)
		return nil
	}

	err = db.Update("db-"+we.DB, 0, func(current []byte) ([]byte, error) {
		if len(current) == 0 {
			return nil, NotFound
		}

		item := consolio.Item{}
		err := json.Unmarshal(current, &item)
		if err != nil {
			return nil, err
		}

		switch we.Type {
		case "state":
			// TODO:  Implement state tracking
			item.LastChange = time.Now().UTC()
			glog.Infof("Got state change for %q: %s", we.DB, *we.Data)
		case "stats":
			item.LastStat = time.Now().UTC()
			glog.Infof("Got stats for %q: %s", we.DB, *we.Data)
			item.Stats = we.Data
		default:
			return nil, fmt.Errorf("Unknown event type: %q", we.Type)
		}

		return json.Marshal(&item)
	})

	if err != nil {
		glog.Warningf("Error updating %q as %v: %s: %v",
			we.DB, we.Type, *we.Data, err)
		return nil
	}

	var rv *coap.Message
	if m.IsConfirmable() {
		rv = &coap.Message{
			Type:      coap.Acknowledgement,
			Code:      coap.Created,
			MessageID: m.MessageID,
		}
	}
	return rv
}

func eventListener() {
	if *evListen == "" {
		return
	}

	handler := coap.NewServeMux()
	handler.HandleFunc("/dbinfo", evInfoHandler)

	glog.Fatal(coap.ListenAndServe("udp", *evListen, handler))
}
<|endoftext|>


<commit_before>// +build linux

package main

import (
	"encoding/json"
	"fmt"
	"os"
	"strconv"
	"strings"

	"github.com/opencontainers/runc/libcontainer"
	"github.com/opencontainers/runc/libcontainer/utils"
	"github.com/opencontainers/runtime-spec/specs-go"
	"github.com/urfave/cli"
)

var execCommand = cli.Command{
	Name:  "exec",
	Usage: "execute new process inside the container",
	ArgsUsage: `<container-id> <container command> [command options]

Where "<container-id>" is the name for the instance of the container and
"<container command>" is the command to be executed in the container.

EXAMPLE:
For example, if the container is configured to run the linux ps command the
following will output a list of processes running in the container:

       # runc exec <container-id> ps`,
	Flags: []cli.Flag{
		cli.StringFlag{
			Name:  "console-socket",
			Usage: "path to an AF_UNIX socket which will receive a file descriptor referencing the master end of the console's pseudoterminal",
		},
		cli.StringFlag{
			Name:  "cwd",
			Usage: "current working directory in the container",
		},
		cli.StringSliceFlag{
			Name:  "env, e",
			Usage: "set environment variables",
		},
		cli.BoolFlag{
			Name:  "tty, t",
			Usage: "allocate a pseudo-TTY",
		},
		cli.StringFlag{
			Name:  "user, u",
			Usage: "UID (format: <uid>[:<gid>])",
		},
		cli.StringFlag{
			Name:  "process, p",
			Usage: "path to the process.json",
		},
		cli.BoolFlag{
			Name:  "detach,d",
			Usage: "detach from the container's process",
		},
		cli.StringFlag{
			Name:  "pid-file",
			Value: "",
			Usage: "specify the file to write the process id to",
		},
		cli.StringFlag{
			Name:  "process-label",
			Usage: "set the asm process label for the process commonly used with selinux",
		},
		cli.StringFlag{
			Name:  "apparmor",
			Usage: "set the apparmor profile for the process",
		},
		cli.BoolFlag{
			Name:  "no-new-privs",
			Usage: "set the no new privileges value for the process",
		},
		cli.StringSliceFlag{
			Name:  "cap, c",
			Value: &cli.StringSlice{},
			Usage: "add a capability to the bounding set for the process",
		},
		cli.BoolFlag{
			Name:   "no-subreaper",
			Usage:  "disable the use of the subreaper used to reap reparented processes",
			Hidden: true,
		},
	},
	Action: func(context *cli.Context) error {
		if err := checkArgs(context, 2, minArgs); err != nil {
			return err
		}
		if os.Geteuid() != 0 {
			return fmt.Errorf("runc should be run as root")
		}
		if err := revisePidFile(context); err != nil {
			return err
		}
		status, err := execProcess(context)
		if err == nil {
			os.Exit(status)
		}
		return fmt.Errorf("exec failed: %v", err)
	},
	SkipArgReorder: true,
}

func execProcess(context *cli.Context) (int, error) {
	container, err := getContainer(context)
	if err != nil {
		return -1, err
	}
	status, err := container.Status()
	if err != nil {
		return -1, err
	}
	if status == libcontainer.Stopped {
		return -1, fmt.Errorf("cannot exec a container that has run and stopped")
	}
	path := context.String("process")
	if path == "" && len(context.Args()) == 1 {
		return -1, fmt.Errorf("process args cannot be empty")
	}
	detach := context.Bool("detach")
	state, err := container.State()
	if err != nil {
		return -1, err
	}
	bundle := utils.SearchLabels(state.Config.Labels, "bundle")
	p, err := getProcess(context, bundle)
	if err != nil {
		return -1, err
	}
	r := &runner{
		enableSubreaper: false,
		shouldDestroy:   false,
		container:       container,
		consoleSocket:   context.String("console-socket"),
		detach:          detach,
		pidFile:         context.String("pid-file"),
	}
	return r.run(p)
}

func getProcess(context *cli.Context, bundle string) (*specs.Process, error) {
	if path := context.String("process"); path != "" {
		f, err := os.Open(path)
		if err != nil {
			return nil, err
		}
		defer f.Close()
		var p specs.Process
		if err := json.NewDecoder(f).Decode(&p); err != nil {
			return nil, err
		}
		return &p, validateProcessSpec(&p)
	}
	// process via cli flags
	if err := os.Chdir(bundle); err != nil {
		return nil, err
	}
	spec, err := loadSpec(specConfig)
	if err != nil {
		return nil, err
	}
	p := spec.Process
	p.Args = context.Args()[1:]
	// override the cwd, if passed
	if context.String("cwd") != "" {
		p.Cwd = context.String("cwd")
	}
	if ap := context.String("apparmor"); ap != "" {
		p.ApparmorProfile = ap
	}
	if l := context.String("process-label"); l != "" {
		p.SelinuxLabel = l
	}
	if caps := context.StringSlice("cap"); len(caps) > 0 {
		p.Capabilities = caps
	}
	// append the passed env variables
	for _, e := range context.StringSlice("env") {
		p.Env = append(p.Env, e)
	}
	// set the tty
	if context.IsSet("tty") {
		p.Terminal = context.Bool("tty")
	}
	if context.IsSet("no-new-privs") {
		p.NoNewPrivileges = context.Bool("no-new-privs")
	}
	// override the user, if passed
	if context.String("user") != "" {
		u := strings.SplitN(context.String("user"), ":", 2)
		if len(u) > 1 {
			gid, err := strconv.Atoi(u[1])
			if err != nil {
				return nil, fmt.Errorf("parsing %s as int for gid failed: %v", u[1], err)
			}
			p.User.GID = uint32(gid)
		}
		uid, err := strconv.Atoi(u[0])
		if err != nil {
			return nil, fmt.Errorf("parsing %s as int for uid failed: %v", u[0], err)
		}
		p.User.UID = uint32(uid)
	}
	return &p, nil
}
<commit_msg>Simplify loop to a simple array append<commit_after>// +build linux

package main

import (
	"encoding/json"
	"fmt"
	"os"
	"strconv"
	"strings"

	"github.com/opencontainers/runc/libcontainer"
	"github.com/opencontainers/runc/libcontainer/utils"
	"github.com/opencontainers/runtime-spec/specs-go"
	"github.com/urfave/cli"
)

var execCommand = cli.Command{
	Name:  "exec",
	Usage: "execute new process inside the container",
	ArgsUsage: `<container-id> <container command> [command options]

Where "<container-id>" is the name for the instance of the container and
"<container command>" is the command to be executed in the container.

EXAMPLE:
For example, if the container is configured to run the linux ps command the
following will output a list of processes running in the container:

       # runc exec <container-id> ps`,
	Flags: []cli.Flag{
		cli.StringFlag{
			Name:  "console-socket",
			Usage: "path to an AF_UNIX socket which will receive a file descriptor referencing the master end of the console's pseudoterminal",
		},
		cli.StringFlag{
			Name:  "cwd",
			Usage: "current working directory in the container",
		},
		cli.StringSliceFlag{
			Name:  "env, e",
			Usage: "set environment variables",
		},
		cli.BoolFlag{
			Name:  "tty, t",
			Usage: "allocate a pseudo-TTY",
		},
		cli.StringFlag{
			Name:  "user, u",
			Usage: "UID (format: <uid>[:<gid>])",
		},
		cli.StringFlag{
			Name:  "process, p",
			Usage: "path to the process.json",
		},
		cli.BoolFlag{
			Name:  "detach,d",
			Usage: "detach from the container's process",
		},
		cli.StringFlag{
			Name:  "pid-file",
			Value: "",
			Usage: "specify the file to write the process id to",
		},
		cli.StringFlag{
			Name:  "process-label",
			Usage: "set the asm process label for the process commonly used with selinux",
		},
		cli.StringFlag{
			Name:  "apparmor",
			Usage: "set the apparmor profile for the process",
		},
		cli.BoolFlag{
			Name:  "no-new-privs",
			Usage: "set the no new privileges value for the process",
		},
		cli.StringSliceFlag{
			Name:  "cap, c",
			Value: &cli.StringSlice{},
			Usage: "add a capability to the bounding set for the process",
		},
		cli.BoolFlag{
			Name:   "no-subreaper",
			Usage:  "disable the use of the subreaper used to reap reparented processes",
			Hidden: true,
		},
	},
	Action: func(context *cli.Context) error {
		if err := checkArgs(context, 2, minArgs); err != nil {
			return err
		}
		if os.Geteuid() != 0 {
			return fmt.Errorf("runc should be run as root")
		}
		if err := revisePidFile(context); err != nil {
			return err
		}
		status, err := execProcess(context)
		if err == nil {
			os.Exit(status)
		}
		return fmt.Errorf("exec failed: %v", err)
	},
	SkipArgReorder: true,
}

func execProcess(context *cli.Context) (int, error) {
	container, err := getContainer(context)
	if err != nil {
		return -1, err
	}
	status, err := container.Status()
	if err != nil {
		return -1, err
	}
	if status == libcontainer.Stopped {
		return -1, fmt.Errorf("cannot exec a container that has run and stopped")
	}
	path := context.String("process")
	if path == "" && len(context.Args()) == 1 {
		return -1, fmt.Errorf("process args cannot be empty")
	}
	detach := context.Bool("detach")
	state, err := container.State()
	if err != nil {
		return -1, err
	}
	bundle := utils.SearchLabels(state.Config.Labels, "bundle")
	p, err := getProcess(context, bundle)
	if err != nil {
		return -1, err
	}
	r := &runner{
		enableSubreaper: false,
		shouldDestroy:   false,
		container:       container,
		consoleSocket:   context.String("console-socket"),
		detach:          detach,
		pidFile:         context.String("pid-file"),
	}
	return r.run(p)
}

func getProcess(context *cli.Context, bundle string) (*specs.Process, error) {
	if path := context.String("process"); path != "" {
		f, err := os.Open(path)
		if err != nil {
			return nil, err
		}
		defer f.Close()
		var p specs.Process
		if err := json.NewDecoder(f).Decode(&p); err != nil {
			return nil, err
		}
		return &p, validateProcessSpec(&p)
	}
	// process via cli flags
	if err := os.Chdir(bundle); err != nil {
		return nil, err
	}
	spec, err := loadSpec(specConfig)
	if err != nil {
		return nil, err
	}
	p := spec.Process
	p.Args = context.Args()[1:]
	// override the cwd, if passed
	if context.String("cwd") != "" {
		p.Cwd = context.String("cwd")
	}
	if ap := context.String("apparmor"); ap != "" {
		p.ApparmorProfile = ap
	}
	if l := context.String("process-label"); l != "" {
		p.SelinuxLabel = l
	}
	if caps := context.StringSlice("cap"); len(caps) > 0 {
		p.Capabilities = caps
	}
	// append the passed env variables
	p.Env = append(p.Env, context.StringSlice("env")...)

	// set the tty
	if context.IsSet("tty") {
		p.Terminal = context.Bool("tty")
	}
	if context.IsSet("no-new-privs") {
		p.NoNewPrivileges = context.Bool("no-new-privs")
	}
	// override the user, if passed
	if context.String("user") != "" {
		u := strings.SplitN(context.String("user"), ":", 2)
		if len(u) > 1 {
			gid, err := strconv.Atoi(u[1])
			if err != nil {
				return nil, fmt.Errorf("parsing %s as int for gid failed: %v", u[1], err)
			}
			p.User.GID = uint32(gid)
		}
		uid, err := strconv.Atoi(u[0])
		if err != nil {
			return nil, fmt.Errorf("parsing %s as int for uid failed: %v", u[0], err)
		}
		p.User.UID = uint32(uid)
	}
	return &p, nil
}
<|endoftext|>


<commit_before>// errchk $G $D/$F.go

// Copyright 2009 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 main

type Inst interface {
	Next()	*Inst;
}

type Regexp struct {
	code *[]Inst;
	start	Inst;
}

type Start struct {
	foo	*Inst;
}

func (start *Start) Next() *Inst { return nil }

func AddInst(Inst) *Inst {
	print("ok in addinst\n");
	return nil
}

func main() {
	re := new(Regexp);
	print("call addinst\n");
	var x Inst = AddInst(new(Start));	// ERROR "illegal"
	print("return from  addinst\n");
}
<commit_msg>Recognize gccgo error message: interface1.go:29:6: error: incompatible type in initialization (missing method Next)<commit_after>// errchk $G $D/$F.go

// Copyright 2009 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 main

type Inst interface {
	Next()	*Inst;
}

type Regexp struct {
	code *[]Inst;
	start	Inst;
}

type Start struct {
	foo	*Inst;
}

func (start *Start) Next() *Inst { return nil }

func AddInst(Inst) *Inst {
	print("ok in addinst\n");
	return nil
}

func main() {
	re := new(Regexp);
	print("call addinst\n");
	var x Inst = AddInst(new(Start));	// ERROR "illegal|incompatible"
	print("return from  addinst\n");
}
<|endoftext|>


<commit_before>package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"github.com/ActiveState/tail"
	"github.com/foize/go.fifo"
	"net/http"
	"os"
	"time"
)

type Configuration struct {
	Api_endpoint, Api_key, Project, Env string
	Shipping_interval                   int
	Components                          []Component
}

type Component struct {
	Name, File string
}

type Message struct {
	Project, Env, Component, Text string
	Timestamp                     int64
}

func main() {
	// declare our outgoing queue and http client
	outgoing := fifo.NewQueue()
	client := &http.Client{}

	// load the configuration
	file, _ := os.Open("config.json")
	decoder := json.NewDecoder(file)
	configuration := Configuration{}
	err := decoder.Decode(&configuration)
	if err != nil {
		fmt.Println("error:", err)
	}

	// start Filewatchers for the files
	for _, comp := range configuration.Components {
		fmt.Println("watching", comp.Name, comp.File)
		go Filewatcher(comp, configuration, outgoing)
	}

	// loop
	for {
		outlen := outgoing.Len()
		if outlen > 0 {
			// create a request body
			var data = bytes.NewBuffer([]byte(``))

			// add queue contents as payload
			for {
				item := outgoing.Next()
				if item == nil {
					break
				}

				fmt.Println("shipping item:", item)

			}

			// send request
			req, _ := http.NewRequest("POST", configuration.Api_endpoint, data)
			req.Header.Set("x-api-key", configuration.Api_key)

			fmt.Println("sending request of", outlen, "items")
			resp, err := client.Do(req)
			if err != nil {
				fmt.Println("error requesting:", err)
			} else {
				fmt.Println(req)
				fmt.Println(resp)
			}
		}
		time.Sleep(time.Duration(configuration.Shipping_interval) * time.Second)
	}
}

func Filewatcher(comp Component, conf Configuration, outgoing *fifo.Queue) {
	// tail a given file, add appended lines into a Message queue
	t, _ := tail.TailFile(comp.File, tail.Config{Follow: true})
	for line := range t.Lines {
		outgoing.Add(&Message{
			Project:   conf.Project,
			Env:       conf.Env,
			Component: comp.Name,
			Text:      line.Text,
			Timestamp: time.Now().Unix(),
		})
	}
}
<commit_msg>Encode the items to be submitted as JSON<commit_after>package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"github.com/ActiveState/tail"
	"github.com/foize/go.fifo"
	"net/http"
	"os"
	"time"
)

type Configuration struct {
	Api_endpoint, Api_key, Project, Env string
	Shipping_interval                   int
	Components                          []Component
}

type Component struct {
	Name, File string
}

type Message struct {
	Project, Env, Component, Text string
	Timestamp                     int64
}

func main() {
	// declare our outgoing queue and http client
	outgoing := fifo.NewQueue()
	client := &http.Client{}

	// load the configuration
	file, _ := os.Open("config.json")
	decoder := json.NewDecoder(file)
	configuration := Configuration{}
	err := decoder.Decode(&configuration)
	if err != nil {
		fmt.Println("error:", err)
	}

	// start Filewatchers for the files
	for _, comp := range configuration.Components {
		fmt.Println("watching", comp.Name, comp.File)
		go Filewatcher(comp, configuration, outgoing)
	}

	// loop
	for {
		outlen := outgoing.Len()
		if outlen > 0 {
			// create a request body
			var data = bytes.NewBuffer([]byte(``))
			encoder := json.NewEncoder(data)

			// add queue contents as payload
			var items []*Message
			for {
				item := outgoing.Next()
				if item == nil {
					break
				}

				fmt.Println("shipping item:", item)
				items = append(items, item.(*Message))
			}

			error := encoder.Encode(items)
			if error != nil {
				fmt.Println("Encoding error:", error)
				//TODO: Error handling? Abort run?
			}

			// send request
			req, _ := http.NewRequest("POST", configuration.Api_endpoint, data)
			req.Header.Set("x-api-key", configuration.Api_key)

			fmt.Println("sending request of", outlen, "items")
			resp, err := client.Do(req)
			if err != nil {
				fmt.Println("error requesting:", err)
			} else {
				fmt.Println(req)
				fmt.Println(resp)
			}
		}
		time.Sleep(time.Duration(configuration.Shipping_interval) * time.Second)
	}
}

func Filewatcher(comp Component, conf Configuration, outgoing *fifo.Queue) {
	// tail a given file, add appended lines into a Message queue
	t, _ := tail.TailFile(comp.File, tail.Config{Follow: true})
	for line := range t.Lines {
		outgoing.Add(&Message{
			Project:   conf.Project,
			Env:       conf.Env,
			Component: comp.Name,
			Text:      line.Text,
			Timestamp: time.Now().Unix(),
		})
	}
}
<|endoftext|>


<commit_before>package ir

import (
	"../vm"
	"github.com/stretchr/testify/assert"
	"math"
	"testing"
)

func TestCompileFunction(t *testing.T) {
	const n1, n2, n3 float64 = 2, 3, 4

	f := CompileFunction(
		vm.NewSignature(
			[]string{"f", "x1", "x2", "x3"}, []vm.OptionalArgument{}, "",
			[]string{}, []vm.OptionalArgument{}, "",
		),
		NewApp(0, NewPositionalArguments(1, NewApp(0, NewPositionalArguments(2, 3)))))

	x1 := float64(vm.PApp(f, vm.Pow, vm.NewNumber(n1), vm.NewNumber(n2), vm.NewNumber(n3)).Eval().(vm.NumberType))
	x2 := math.Pow(n1, math.Pow(n2, n3))

	t.Logf("%f == %f?", x1, x2)

	assert.Equal(t, x1, x2)
}
<commit_msg>Remove extra type annotation<commit_after>package ir

import (
	"../vm"
	"github.com/stretchr/testify/assert"
	"math"
	"testing"
)

func TestCompileFunction(t *testing.T) {
	const n1, n2, n3 = 2, 3, 4

	f := CompileFunction(
		vm.NewSignature(
			[]string{"f", "x1", "x2", "x3"}, []vm.OptionalArgument{}, "",
			[]string{}, []vm.OptionalArgument{}, "",
		),
		NewApp(0, NewPositionalArguments(1, NewApp(0, NewPositionalArguments(2, 3)))))

	x1 := float64(vm.PApp(f, vm.Pow, vm.NewNumber(n1), vm.NewNumber(n2), vm.NewNumber(n3)).Eval().(vm.NumberType))
	x2 := math.Pow(n1, math.Pow(n2, n3))

	t.Logf("%f == %f?", x1, x2)

	assert.Equal(t, x1, x2)
}
<|endoftext|>


<commit_before>package memdb

import "os"
import "bufio"
import "errors"
import "github.com/couchbase/goforestdb"
import "bytes"

const DiskBlockSize = 512 * 1024

var (
	ErrNotEnoughSpace = errors.New("Not enough space in the buffer")
)

type FileWriter interface {
	Open(path string) error
	WriteItem(*Item) error
	Close() error
}

type FileReader interface {
	Open(path string) error
	ReadItem() (*Item, error)
	Close() error
}

func newFileWriter(t FileType) FileWriter {
	var w FileWriter
	if t == RawdbFile {
		w = &rawFileWriter{}
	} else if t == ForestdbFile {
		w = &forestdbFileWriter{}
	}
	return w
}

func newFileReader(t FileType) FileReader {
	var r FileReader
	if t == RawdbFile {
		r = &rawFileReader{}
	} else if t == ForestdbFile {
		r = &forestdbFileReader{}
	}
	return r
}

type rawFileWriter struct {
	fd   *os.File
	w    *bufio.Writer
	buf  []byte
	path string
}

func (f *rawFileWriter) Open(path string) error {
	var err error
	f.fd, err = os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0755)
	if err == nil {
		f.buf = make([]byte, encodeBufSize)
		f.w = bufio.NewWriterSize(f.fd, DiskBlockSize)
	}
	return err
}

func (f *rawFileWriter) WriteItem(itm *Item) error {
	return itm.Encode(f.buf, f.w)
}

func (f *rawFileWriter) Close() error {
	terminator := &Item{
		data: []byte(nil),
	}

	if err := f.WriteItem(terminator); err != nil {
		return err
	}

	f.w.Flush()
	return f.fd.Close()
}

type rawFileReader struct {
	fd   *os.File
	r    *bufio.Reader
	buf  []byte
	path string
}

func (f *rawFileReader) Open(path string) error {
	var err error
	f.fd, err = os.Open(path)
	if err == nil {
		f.buf = make([]byte, encodeBufSize)
		f.r = bufio.NewReaderSize(f.fd, DiskBlockSize)
	}
	return err
}

func (f *rawFileReader) ReadItem() (*Item, error) {
	itm := &Item{}
	err := itm.Decode(f.buf, f.r)
	if len(itm.data) == 0 {
		itm = nil
	}

	return itm, err
}

func (f *rawFileReader) Close() error {
	return f.fd.Close()
}

type forestdbFileWriter struct {
	file  *forestdb.File
	store *forestdb.KVStore
	buf   []byte
	wbuf  bytes.Buffer
}

func (f *forestdbFileWriter) Open(path string) error {
	var err error
	cfg := forestdb.DefaultConfig()
	f.file, err = forestdb.Open(path, cfg)
	if err == nil {
		f.buf = make([]byte, encodeBufSize)
		f.store, err = f.file.OpenKVStoreDefault(nil)
	}

	return err
}

func (f *forestdbFileWriter) WriteItem(itm *Item) error {
	f.wbuf.Reset()
	err := itm.Encode(f.buf, &f.wbuf)
	if err == nil {
		err = f.store.SetKV(f.wbuf.Bytes(), nil)
	}

	return err
}

func (f *forestdbFileWriter) Close() error {
	err := f.file.Commit(forestdb.COMMIT_NORMAL)
	if err == nil {
		err = f.store.Close()
		if err == nil {
			err = f.file.Close()
		}
	}

	return err
}

type forestdbFileReader struct {
	file  *forestdb.File
	store *forestdb.KVStore
	iter  *forestdb.Iterator
	buf   []byte
}

func (f *forestdbFileReader) Open(path string) error {
	var err error
	cfg := forestdb.DefaultConfig()
	cfg.SetSeqTreeOpt(forestdb.SEQTREE_NOT_USE)
	cfg.SetBufferCacheSize(1024 * 1024 * 256)
	cfg.SetWalThreshold(1000000)

	f.file, err = forestdb.Open(path, cfg)
	if err == nil {
		f.buf = make([]byte, encodeBufSize)
		f.store, err = f.file.OpenKVStoreDefault(nil)
		if err == nil {
			f.iter, err = f.store.IteratorInit(nil, nil, forestdb.ITR_NONE)
		}
	}

	return err
}

func (f *forestdbFileReader) ReadItem() (*Item, error) {
	itm := &Item{}
	doc, err := f.iter.Get()
	if err == forestdb.RESULT_ITERATOR_FAIL {
		return nil, nil
	}

	f.iter.Next()
	if err == nil {
		rbuf := bytes.NewBuffer(doc.Key())
		err = itm.Decode(f.buf, rbuf)
	}

	return itm, err
}

func (f *forestdbFileReader) Close() error {
	f.iter.Close()
	f.store.Close()
	return f.file.Close()
}
<commit_msg>memdb: Change forestdb settings<commit_after>package memdb

import "os"
import "bufio"
import "errors"
import "github.com/couchbase/goforestdb"
import "bytes"

const DiskBlockSize = 512 * 1024

var (
	ErrNotEnoughSpace = errors.New("Not enough space in the buffer")
	forestdbConfig    *forestdb.Config
)

func init() {
	forestdbConfig = forestdb.DefaultConfig()
	forestdbConfig.SetSeqTreeOpt(forestdb.SEQTREE_NOT_USE)
	forestdbConfig.SetBufferCacheSize(1024 * 1024)

}

type FileWriter interface {
	Open(path string) error
	WriteItem(*Item) error
	Close() error
}

type FileReader interface {
	Open(path string) error
	ReadItem() (*Item, error)
	Close() error
}

func newFileWriter(t FileType) FileWriter {
	var w FileWriter
	if t == RawdbFile {
		w = &rawFileWriter{}
	} else if t == ForestdbFile {
		w = &forestdbFileWriter{}
	}
	return w
}

func newFileReader(t FileType) FileReader {
	var r FileReader
	if t == RawdbFile {
		r = &rawFileReader{}
	} else if t == ForestdbFile {
		r = &forestdbFileReader{}
	}
	return r
}

type rawFileWriter struct {
	fd   *os.File
	w    *bufio.Writer
	buf  []byte
	path string
}

func (f *rawFileWriter) Open(path string) error {
	var err error
	f.fd, err = os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0755)
	if err == nil {
		f.buf = make([]byte, encodeBufSize)
		f.w = bufio.NewWriterSize(f.fd, DiskBlockSize)
	}
	return err
}

func (f *rawFileWriter) WriteItem(itm *Item) error {
	return itm.Encode(f.buf, f.w)
}

func (f *rawFileWriter) Close() error {
	terminator := &Item{
		data: []byte(nil),
	}

	if err := f.WriteItem(terminator); err != nil {
		return err
	}

	f.w.Flush()
	return f.fd.Close()
}

type rawFileReader struct {
	fd   *os.File
	r    *bufio.Reader
	buf  []byte
	path string
}

func (f *rawFileReader) Open(path string) error {
	var err error
	f.fd, err = os.Open(path)
	if err == nil {
		f.buf = make([]byte, encodeBufSize)
		f.r = bufio.NewReaderSize(f.fd, DiskBlockSize)
	}
	return err
}

func (f *rawFileReader) ReadItem() (*Item, error) {
	itm := &Item{}
	err := itm.Decode(f.buf, f.r)
	if len(itm.data) == 0 {
		itm = nil
	}

	return itm, err
}

func (f *rawFileReader) Close() error {
	return f.fd.Close()
}

type forestdbFileWriter struct {
	file  *forestdb.File
	store *forestdb.KVStore
	buf   []byte
	wbuf  bytes.Buffer
}

func (f *forestdbFileWriter) Open(path string) error {
	var err error
	f.file, err = forestdb.Open(path, forestdbConfig)
	if err == nil {
		f.buf = make([]byte, encodeBufSize)
		f.store, err = f.file.OpenKVStoreDefault(nil)
	}

	return err
}

func (f *forestdbFileWriter) WriteItem(itm *Item) error {
	f.wbuf.Reset()
	err := itm.Encode(f.buf, &f.wbuf)
	if err == nil {
		err = f.store.SetKV(f.wbuf.Bytes(), nil)
	}

	return err
}

func (f *forestdbFileWriter) Close() error {
	err := f.file.Commit(forestdb.COMMIT_NORMAL)
	if err == nil {
		err = f.store.Close()
		if err == nil {
			err = f.file.Close()
		}
	}

	return err
}

type forestdbFileReader struct {
	file  *forestdb.File
	store *forestdb.KVStore
	iter  *forestdb.Iterator
	buf   []byte
}

func (f *forestdbFileReader) Open(path string) error {
	var err error

	f.file, err = forestdb.Open(path, forestdbConfig)
	if err == nil {
		f.buf = make([]byte, encodeBufSize)
		f.store, err = f.file.OpenKVStoreDefault(nil)
		if err == nil {
			f.iter, err = f.store.IteratorInit(nil, nil, forestdb.ITR_NONE)
		}
	}

	return err
}

func (f *forestdbFileReader) ReadItem() (*Item, error) {
	itm := &Item{}
	doc, err := f.iter.Get()
	if err == forestdb.RESULT_ITERATOR_FAIL {
		return nil, nil
	}

	f.iter.Next()
	if err == nil {
		rbuf := bytes.NewBuffer(doc.Key())
		err = itm.Decode(f.buf, rbuf)
	}

	return itm, err
}

func (f *forestdbFileReader) Close() error {
	f.iter.Close()
	f.store.Close()
	return f.file.Close()
}
<|endoftext|>


<commit_before>package agollo

import (
	"encoding/json"
	"errors"
	"fmt"
	"os"
)

const suffix = ".json"

var configFileMap=make(map[string]string,1)

//write config to file
func writeConfigFile(config *ApolloConfig, configPath string) error {
	if config == nil {
		logger.Error("apollo config is null can not write backup file")
		return errors.New("apollo config is null can not write backup file")
	}
	file, e := os.Create(getConfigFile(configPath,config.NamespaceName))
	if e != nil {
		logger.Errorf("writeConfigFile fail,error:", e)
		return e
	}
	defer file.Close()

	return json.NewEncoder(file).Encode(config)
}

//get real config file
func getConfigFile(configDir string,namespace string) string {
	fullPath := configFileMap[namespace]
	if fullPath == "" {
		filePath := fmt.Sprintf("%s%s", namespace, suffix)
		if configDir != "" {
			configFileMap[namespace] = fmt.Sprintf("%s/%s", configDir, filePath)
		} else {
			configFileMap[namespace] = filePath
		}

	}
	return fullPath
}

//load config from file
func loadConfigFile(configDir string,namespace string) (*ApolloConfig, error) {
	configFilePath := getConfigFile(configDir,namespace)
	logger.Info("load config file from :", configFilePath)
	file, e := os.Open(configFilePath)
	if e != nil {
		logger.Errorf("loadConfigFile fail,error:", e)
		return nil, e
	}
	defer file.Close()
	config := &ApolloConfig{}
	e = json.NewDecoder(file).Decode(config)

	if e != nil {
		logger.Errorf("loadConfigFile fail,error:", e)
		return nil, e
	}

	return config, e
}
<commit_msg>[Bug]Fixed #75 first time write backup file error<commit_after>package agollo

import (
	"encoding/json"
	"errors"
	"fmt"
	"os"
)

const suffix = ".json"

var configFileMap=make(map[string]string,1)

//write config to file
func writeConfigFile(config *ApolloConfig, configPath string) error {
	if config == nil {
		logger.Error("apollo config is null can not write backup file")
		return errors.New("apollo config is null can not write backup file")
	}
	file, e := os.Create(getConfigFile(configPath,config.NamespaceName))
	if e != nil {
		logger.Errorf("writeConfigFile fail,error:", e)
		return e
	}
	defer file.Close()

	return json.NewEncoder(file).Encode(config)
}

//get real config file
func getConfigFile(configDir string,namespace string) string {
	fullPath := configFileMap[namespace]
	if fullPath == "" {
		filePath := fmt.Sprintf("%s%s", namespace, suffix)
		if configDir != "" {
			configFileMap[namespace] = fmt.Sprintf("%s/%s", configDir, filePath)
		} else {
			configFileMap[namespace] = filePath
		}
	}
	return configFileMap[namespace]
}

//load config from file
func loadConfigFile(configDir string,namespace string) (*ApolloConfig, error) {
	configFilePath := getConfigFile(configDir,namespace)
	logger.Info("load config file from :", configFilePath)
	file, e := os.Open(configFilePath)
	if e != nil {
		logger.Errorf("loadConfigFile fail,error:", e)
		return nil, e
	}
	defer file.Close()
	config := &ApolloConfig{}
	e = json.NewDecoder(file).Decode(config)

	if e != nil {
		logger.Errorf("loadConfigFile fail,error:", e)
		return nil, e
	}

	return config, e
}
<|endoftext|>


<commit_before>package dictionary

import (
	"errors"
	"io/ioutil"
	"strings"
)

var (
	// DefaultDictionaryNotFound is an error returned when the default system dictionary
	// cannot be found or loaded.
	DefaultDictionaryNotFound = errors.New("Unable to find default dictionary")

	dictionaryLocations = []string{
		"/usr/share/dict/words",
		"/usr/dict/words",
	}
)

// Default returns the default dictionary on Unix systems.
func Default() (Interface, error) {
	for _, filename := range dictionaryLocations {
		d, err := Load(filename)
		if err == nil {
			return d, nil
		}
	}

	return nil, DefaultDictionaryNotFound
}

// Load loads a dictionary from a file containing newline separated words.
func Load(filename string) (Interface, error) {
	b, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil, err
	}

	return WithWords(strings.Split(string(b), "\n")...), nil
}
<commit_msg>trim space before splitting<commit_after>package dictionary

import (
	"errors"
	"io/ioutil"
	"strings"
)

var (
	// DefaultDictionaryNotFound is an error returned when the default system dictionary
	// cannot be found or loaded.
	DefaultDictionaryNotFound = errors.New("Unable to find default dictionary")

	dictionaryLocations = []string{
		"/usr/share/dict/words",
		"/usr/dict/words",
	}
)

// Default returns the default dictionary on Unix systems.
func Default() (Interface, error) {
	for _, filename := range dictionaryLocations {
		d, err := Load(filename)
		if err == nil {
			return d, nil
		}
	}

	return nil, DefaultDictionaryNotFound
}

// Load loads a dictionary from a file containing newline separated words.
func Load(filename string) (Interface, error) {
	b, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil, err
	}

	return WithWords(strings.Split(strings.TrimSpace(string(b)), "\n")...), nil
}
<|endoftext|>


<commit_before>package cli

import (
	"bytes"
	"fmt"
	"io"
	"reflect"
	"strings"
	"text/template"
)

// ToFishCompletion creates a fish completion string for the `*App`
// The function errors if either parsing or writing of the string fails.
func (a *App) ToFishCompletion() (string, error) {
	var w bytes.Buffer
	if err := a.writeFishCompletionTemplate(&w); err != nil {
		return "", err
	}
	return w.String(), nil
}

type fishCompletionTemplate struct {
	App         *App
	Completions []string
	AllCommands []string
}

func (a *App) writeFishCompletionTemplate(w io.Writer) error {
	const name = "cli"
	t, err := template.New(name).Parse(FishCompletionTemplate)
	if err != nil {
		return err
	}
	allCommands := []string{}

	// Add global flags
	completions := a.prepareFishFlags(a.VisibleFlags(), allCommands)

	// Add help flag
	if !a.HideHelp {
		completions = append(
			completions,
			a.prepareFishFlags([]Flag{HelpFlag}, allCommands)...,
		)
	}

	// Add version flag
	if !a.HideVersion {
		completions = append(
			completions,
			a.prepareFishFlags([]Flag{VersionFlag}, allCommands)...,
		)
	}

	// Add commands and their flags
	completions = append(
		completions,
		a.prepareFishCommands(a.VisibleCommands(), &allCommands, []string{})...,
	)

	return t.ExecuteTemplate(w, name, &fishCompletionTemplate{
		App:         a,
		Completions: completions,
		AllCommands: allCommands,
	})
}

func (a *App) prepareFishCommands(commands []*Command, allCommands *[]string, previousCommands []string) []string {
	completions := []string{}
	for _, command := range commands {
		if command.Hidden {
			continue
		}

		var completion strings.Builder
		completion.WriteString(fmt.Sprintf(
			"complete -r -c %s -n '%s' -a '%s'",
			a.Name,
			a.fishSubcommandHelper(previousCommands),
			strings.Join(command.Names(), " "),
		))

		if command.Usage != "" {
			completion.WriteString(fmt.Sprintf(" -d '%s'",
				escapeSingleQuotes(command.Usage)))
		}

		if !command.HideHelp {
			completions = append(
				completions,
				a.prepareFishFlags([]Flag{HelpFlag}, command.Names())...,
			)
		}

		*allCommands = append(*allCommands, command.Names()...)
		completions = append(completions, completion.String())
		completions = append(
			completions,
			a.prepareFishFlags(command.Flags, command.Names())...,
		)

		// recursevly iterate subcommands
		if len(command.Subcommands) > 0 {
			completions = append(
				completions,
				a.prepareFishCommands(
					command.Subcommands, allCommands, command.Names(),
				)...,
			)
		}
	}

	return completions
}

func (a *App) prepareFishFlags(flags []Flag, previousCommands []string) []string {
	completions := []string{}
	for _, f := range flags {
		flag, ok := f.(DocGenerationFlag)
		if !ok {
			continue
		}

		completion := &strings.Builder{}
		completion.WriteString(fmt.Sprintf(
			"complete -c %s -n '%s'",
			a.Name,
			a.fishSubcommandHelper(previousCommands),
		))

		fishAddFileFlag(f, completion)

		for idx, opt := range flag.Names() {
			if idx == 0 {
				completion.WriteString(fmt.Sprintf(
					" -l %s", strings.TrimSpace(opt),
				))
			} else {
				completion.WriteString(fmt.Sprintf(
					" -s %s", strings.TrimSpace(opt),
				))

			}
		}

		if flag.TakesValue() {
			completion.WriteString(" -r")
		}

		if flag.GetUsage() != "" {
			completion.WriteString(fmt.Sprintf(" -d '%s'",
				escapeSingleQuotes(flag.GetUsage())))
		}

		completions = append(completions, completion.String())
	}

	return completions
}

func fishAddFileFlag(flag Flag, completion *strings.Builder) {
	val := reflect.ValueOf(flag)
	// if flag is a non-nil pointer to a struct...
	if val.Kind() != reflect.Invalid && val.Elem().Kind() == reflect.Struct {
		field := val.Elem().FieldByName("TakesFile")
		// if flag's underlying type has a bool field called TakesFile, whose value is true...
		if field.Kind() == reflect.Bool && field.Bool() {
			// don't append '-f'
			return
		}
	}
	// append '-f', indicating that arguments to this flag are *not* meant to be file paths
	completion.WriteString(" -f")
}

func (a *App) fishSubcommandHelper(allCommands []string) string {
	fishHelper := fmt.Sprintf("__fish_%s_no_subcommand", a.Name)
	if len(allCommands) > 0 {
		fishHelper = fmt.Sprintf(
			"__fish_seen_subcommand_from %s",
			strings.Join(allCommands, " "),
		)
	}
	return fishHelper

}

func escapeSingleQuotes(input string) string {
	return strings.Replace(input, `'`, `\'`, -1)
}
<commit_msg>Revert "Refactor fishAddFileFlag for better flexibility [#1156]"<commit_after>package cli

import (
	"bytes"
	"fmt"
	"io"
	"strings"
	"text/template"
)

// ToFishCompletion creates a fish completion string for the `*App`
// The function errors if either parsing or writing of the string fails.
func (a *App) ToFishCompletion() (string, error) {
	var w bytes.Buffer
	if err := a.writeFishCompletionTemplate(&w); err != nil {
		return "", err
	}
	return w.String(), nil
}

type fishCompletionTemplate struct {
	App         *App
	Completions []string
	AllCommands []string
}

func (a *App) writeFishCompletionTemplate(w io.Writer) error {
	const name = "cli"
	t, err := template.New(name).Parse(FishCompletionTemplate)
	if err != nil {
		return err
	}
	allCommands := []string{}

	// Add global flags
	completions := a.prepareFishFlags(a.VisibleFlags(), allCommands)

	// Add help flag
	if !a.HideHelp {
		completions = append(
			completions,
			a.prepareFishFlags([]Flag{HelpFlag}, allCommands)...,
		)
	}

	// Add version flag
	if !a.HideVersion {
		completions = append(
			completions,
			a.prepareFishFlags([]Flag{VersionFlag}, allCommands)...,
		)
	}

	// Add commands and their flags
	completions = append(
		completions,
		a.prepareFishCommands(a.VisibleCommands(), &allCommands, []string{})...,
	)

	return t.ExecuteTemplate(w, name, &fishCompletionTemplate{
		App:         a,
		Completions: completions,
		AllCommands: allCommands,
	})
}

func (a *App) prepareFishCommands(commands []*Command, allCommands *[]string, previousCommands []string) []string {
	completions := []string{}
	for _, command := range commands {
		if command.Hidden {
			continue
		}

		var completion strings.Builder
		completion.WriteString(fmt.Sprintf(
			"complete -r -c %s -n '%s' -a '%s'",
			a.Name,
			a.fishSubcommandHelper(previousCommands),
			strings.Join(command.Names(), " "),
		))

		if command.Usage != "" {
			completion.WriteString(fmt.Sprintf(" -d '%s'",
				escapeSingleQuotes(command.Usage)))
		}

		if !command.HideHelp {
			completions = append(
				completions,
				a.prepareFishFlags([]Flag{HelpFlag}, command.Names())...,
			)
		}

		*allCommands = append(*allCommands, command.Names()...)
		completions = append(completions, completion.String())
		completions = append(
			completions,
			a.prepareFishFlags(command.Flags, command.Names())...,
		)

		// recursevly iterate subcommands
		if len(command.Subcommands) > 0 {
			completions = append(
				completions,
				a.prepareFishCommands(
					command.Subcommands, allCommands, command.Names(),
				)...,
			)
		}
	}

	return completions
}

func (a *App) prepareFishFlags(flags []Flag, previousCommands []string) []string {
	completions := []string{}
	for _, f := range flags {
		flag, ok := f.(DocGenerationFlag)
		if !ok {
			continue
		}

		completion := &strings.Builder{}
		completion.WriteString(fmt.Sprintf(
			"complete -c %s -n '%s'",
			a.Name,
			a.fishSubcommandHelper(previousCommands),
		))

		fishAddFileFlag(f, completion)

		for idx, opt := range flag.Names() {
			if idx == 0 {
				completion.WriteString(fmt.Sprintf(
					" -l %s", strings.TrimSpace(opt),
				))
			} else {
				completion.WriteString(fmt.Sprintf(
					" -s %s", strings.TrimSpace(opt),
				))

			}
		}

		if flag.TakesValue() {
			completion.WriteString(" -r")
		}

		if flag.GetUsage() != "" {
			completion.WriteString(fmt.Sprintf(" -d '%s'",
				escapeSingleQuotes(flag.GetUsage())))
		}

		completions = append(completions, completion.String())
	}

	return completions
}

func fishAddFileFlag(flag Flag, completion *strings.Builder) {
	switch f := flag.(type) {
	case *GenericFlag:
		if f.TakesFile {
			return
		}
	case *StringFlag:
		if f.TakesFile {
			return
		}
	case *StringSliceFlag:
		if f.TakesFile {
			return
		}
	case *PathFlag:
		if f.TakesFile {
			return
		}
	}
	completion.WriteString(" -f")
}

func (a *App) fishSubcommandHelper(allCommands []string) string {
	fishHelper := fmt.Sprintf("__fish_%s_no_subcommand", a.Name)
	if len(allCommands) > 0 {
		fishHelper = fmt.Sprintf(
			"__fish_seen_subcommand_from %s",
			strings.Join(allCommands, " "),
		)
	}
	return fishHelper

}

func escapeSingleQuotes(input string) string {
	return strings.Replace(input, `'`, `\'`, -1)
}
<|endoftext|>


<commit_before>package flag

import (
	"errors"
	"flag"
	"io"
	"os"
	"path/filepath"
	"time"
)

type FlagSetEx struct {
	*flag.FlagSet
	envPrefix string
}

var DefaultConfigFlagName = "config"

var ex = &FlagSetEx{
	flag.NewFlagSet(os.Args[0], flag.ContinueOnError),
	"",
}

// Bool defines a bool flag with specified name, default value, and usage string.
// The return value is the address of a bool variable that stores the value of the flag.
func Bool(name string, value bool) *bool {
	return ex.FlagSet.Bool(name, value, "")
}

// BoolVar defines a bool flag with specified name, default value, and usage string.
// The argument p points to a bool variable in which to store the value of the flag.
func BoolVar(p *bool, name string, value bool, usage string) {
	ex.FlagSet.BoolVar(p, name, value, usage)
}

// Int defines an int flag with specified name, default value, and usage string.
// The return value is the address of an int variable that stores the value of the flag.
func Int(name string, value int) *int {
	return ex.FlagSet.Int(name, value, "")
}

// IntVar defines an int flag with specified name, default value, and usage string.
// The argument p points to an int variable in which to store the value of the flag.
func IntVar(p *int, name string, value int, usage string) {
	ex.FlagSet.IntVar(p, name, value, usage)
}

// Int64 defines an int64 flag with specified name, default value, and usage string.
// The return value is the address of an int64 variable that stores the value of the flag.
func Int64(name string, value int64) *int64 {
	return ex.FlagSet.Int64(name, value, "")
}

// Int64Var defines an int64 flag with specified name, default value, and usage string.
// The argument p points to an int64 variable in which to store the value of the flag.
func Int64Var(p *int64, name string, value int64, usage string) {
	ex.FlagSet.Int64Var(p, name, value, usage)
}

// Uint defines a uint flag with specified name, default value, and usage string.
// The return value is the address of a uint  variable that stores the value of the flag.
func Uint(name string, value uint) *uint {
	return ex.FlagSet.Uint(name, value, "")
}

// UintVar defines a uint flag with specified name, default value, and usage string.
// The argument p points to a uint  variable in which to store the value of the flag.
func UintVar(p *uint, name string, value uint, usage string) {
	ex.FlagSet.UintVar(p, name, value, usage)
}

// Uint64 defines a uint64 flag with specified name, default value, and usage string.
// The return value is the address of a uint64 variable that stores the value of the flag.
func Uint64(name string, value uint64, usage string) *uint64 {
	return ex.FlagSet.Uint64(name, value, usage)
}

// Uint64Var defines a uint64 flag with specified name, default value, and usage string.
// The argument p points to a uint64 variable in which to store the value of the flag.
func Uint64Var(p *uint64, name string, value uint64, usage string) {
	ex.FlagSet.Uint64Var(p, name, value, usage)
}

// String defines a string flag with specified name, default value, and usage string.
// The return value is the address of a string variable that stores the value of the flag.
func String(name string, value string, usage string) *string {
	return ex.FlagSet.String(name, value, "")
}

// StringVar defines a string flag with specified name, default value, and usage string.
// The argument p points to a string variable in which to store the value of the flag.
func StringVar(p *string, name string, value string, usage string) {
	ex.FlagSet.StringVar(p, name, value, usage)
}

// Float64 defines a float64 config variable with a given name and default value.
func Float64(name string, value float64, usage string) *float64 {
	return ex.FlagSet.Float64(name, value, usage)
}

func Float64Var(p *float64, name string, value float64, usage string) {
	ex.FlagSet.Float64Var(p, name, value, usage)
}

// Duration defines a time.Duration config variable with a given name and default value.
func Duration(name string, value time.Duration, usage string) *time.Duration {
	return ex.FlagSet.Duration(name, value, usage)
}

func DurationVar(p *time.Duration, name string, value time.Duration, usage string) {
	ex.FlagSet.DurationVar(p, name, value, usage)
}

// Parse parses the command-line, environment variables and config file flags
// into the global ConfigSet.
// This must be called after all config flags have been defined but before the
// flags are accessed by the program.
func Parse() error {
	err := ex.FlagSet.Parse(os.Args[1:])
	if err != nil {
		return err
	}

	err = ex.ParseEnv(os.Environ())
	if err != nil {
		return err
	}

	cf := ex.FlagSet.Lookup(DefaultConfigFlagName)
	if cf == nil {
		return nil
	}
	path := cf.Value.String()
	if len(path) > 0 {
		info, err := os.Stat(path)
		if err != nil || info.IsDir() {
			return errors.New("Invalid config file.")
		}
		ext := filepath.Ext(path)
		switch ext {
		case ".toml":
			return ex.ParseTOML(path)
		default:
			return errors.New("Unsupported config file.")
		}
	}
	return nil
}

func SetOutput(output io.Writer) {
	ex.FlagSet.SetOutput(output)
}

func Init(name string, errorHandling flag.ErrorHandling) {
	ex.FlagSet.Init(name, errorHandling)
}
<commit_msg>export Set func<commit_after>package flag

import (
	"errors"
	"flag"
	"io"
	"os"
	"path/filepath"
	"time"
)

type FlagSetEx struct {
	*flag.FlagSet
	envPrefix string
}

var DefaultConfigFlagName = "config"

var ex = &FlagSetEx{
	flag.NewFlagSet(os.Args[0], flag.ContinueOnError),
	"",
}

// Bool defines a bool flag with specified name, default value, and usage string.
// The return value is the address of a bool variable that stores the value of the flag.
func Bool(name string, value bool) *bool {
	return ex.FlagSet.Bool(name, value, "")
}

// BoolVar defines a bool flag with specified name, default value, and usage string.
// The argument p points to a bool variable in which to store the value of the flag.
func BoolVar(p *bool, name string, value bool, usage string) {
	ex.FlagSet.BoolVar(p, name, value, usage)
}

// Int defines an int flag with specified name, default value, and usage string.
// The return value is the address of an int variable that stores the value of the flag.
func Int(name string, value int) *int {
	return ex.FlagSet.Int(name, value, "")
}

// IntVar defines an int flag with specified name, default value, and usage string.
// The argument p points to an int variable in which to store the value of the flag.
func IntVar(p *int, name string, value int, usage string) {
	ex.FlagSet.IntVar(p, name, value, usage)
}

// Int64 defines an int64 flag with specified name, default value, and usage string.
// The return value is the address of an int64 variable that stores the value of the flag.
func Int64(name string, value int64) *int64 {
	return ex.FlagSet.Int64(name, value, "")
}

// Int64Var defines an int64 flag with specified name, default value, and usage string.
// The argument p points to an int64 variable in which to store the value of the flag.
func Int64Var(p *int64, name string, value int64, usage string) {
	ex.FlagSet.Int64Var(p, name, value, usage)
}

// Uint defines a uint flag with specified name, default value, and usage string.
// The return value is the address of a uint  variable that stores the value of the flag.
func Uint(name string, value uint) *uint {
	return ex.FlagSet.Uint(name, value, "")
}

// UintVar defines a uint flag with specified name, default value, and usage string.
// The argument p points to a uint  variable in which to store the value of the flag.
func UintVar(p *uint, name string, value uint, usage string) {
	ex.FlagSet.UintVar(p, name, value, usage)
}

// Uint64 defines a uint64 flag with specified name, default value, and usage string.
// The return value is the address of a uint64 variable that stores the value of the flag.
func Uint64(name string, value uint64, usage string) *uint64 {
	return ex.FlagSet.Uint64(name, value, usage)
}

// Uint64Var defines a uint64 flag with specified name, default value, and usage string.
// The argument p points to a uint64 variable in which to store the value of the flag.
func Uint64Var(p *uint64, name string, value uint64, usage string) {
	ex.FlagSet.Uint64Var(p, name, value, usage)
}

// String defines a string flag with specified name, default value, and usage string.
// The return value is the address of a string variable that stores the value of the flag.
func String(name string, value string, usage string) *string {
	return ex.FlagSet.String(name, value, "")
}

// StringVar defines a string flag with specified name, default value, and usage string.
// The argument p points to a string variable in which to store the value of the flag.
func StringVar(p *string, name string, value string, usage string) {
	ex.FlagSet.StringVar(p, name, value, usage)
}

// Float64 defines a float64 config variable with a given name and default value.
func Float64(name string, value float64, usage string) *float64 {
	return ex.FlagSet.Float64(name, value, usage)
}

func Float64Var(p *float64, name string, value float64, usage string) {
	ex.FlagSet.Float64Var(p, name, value, usage)
}

// Duration defines a time.Duration config variable with a given name and default value.
func Duration(name string, value time.Duration, usage string) *time.Duration {
	return ex.FlagSet.Duration(name, value, usage)
}

func DurationVar(p *time.Duration, name string, value time.Duration, usage string) {
	ex.FlagSet.DurationVar(p, name, value, usage)
}

// Parse parses the command-line, environment variables and config file flags
// into the global ConfigSet.
// This must be called after all config flags have been defined but before the
// flags are accessed by the program.
func Parse() error {
	err := ex.FlagSet.Parse(os.Args[1:])
	if err != nil {
		return err
	}

	err = ex.ParseEnv(os.Environ())
	if err != nil {
		return err
	}

	cf := ex.FlagSet.Lookup(DefaultConfigFlagName)
	if cf == nil {
		return nil
	}
	path := cf.Value.String()
	if len(path) > 0 {
		info, err := os.Stat(path)
		if err != nil || info.IsDir() {
			return errors.New("Invalid config file.")
		}
		ext := filepath.Ext(path)
		switch ext {
		case ".toml":
			return ex.ParseTOML(path)
		default:
			return errors.New("Unsupported config file.")
		}
	}
	return nil
}

func SetOutput(output io.Writer) {
	ex.FlagSet.SetOutput(output)
}

func Init(name string, errorHandling flag.ErrorHandling) {
	ex.FlagSet.Init(name, errorHandling)
}

func Set(name, value string) error {
	return ex.FlagSet.Set(name, value)
}
<|endoftext|>