<commit_before>package human_test

import (
	"fmt"
	"github.com/anexia-it/go-human"
	"net"
	"os"
)

// SimpleChild test struct
type SimpleChild struct {
	Name      string  // no tag
	Property1 uint64  `human:"-"`          // Ignored
	Property2 float64 `human:",omitempty"` // Omitted if empty
}

// SimpleTest test struct
type SimpleTest struct {
	Var1  string      // no tag
	Var2  int         `human:"variable_2"`
	Child SimpleChild // embedded struct
}

// MapTest test struct
type MapTest struct {
	Val1      uint64
	Map       map[string]SimpleChild
	StructMap map[SimpleChild]uint8
}

// SliceTest test struct
type SliceTest struct {
	IntSlice    []int
	StructSlice []SimpleChild
}

// MapSliceTest test struct with slice of maps
type MapSliceTest struct {
	StructMapSlice []map[string]int
}

// TextMarshalerTest test struct
type TextMarshalerTest struct {
	Ip net.IP // implements encoding.TextMarshaler interface
}

// TagFailTest test struct
type TagFailTest struct {
	Test int `human:"&§/$"` // invalid tag name
}

// AnonymousFieldTest test struct
type AnonymousFieldTest struct {
	int  // ignored
	Text string
}

// UnexportedFieldTest test struct
type UnexportedFieldTest struct {
	unexported int  // ignored
	Text string
}

// Encode test with simple test struct to test encode with ignored fields and omit if field is empty
func ExampleEncoder_Encode_simpleOmitEmpty() {
	enc, err := human.NewEncoder(os.Stdout)
	if err != nil {
		return
	}
	testStruct := SimpleTest{
		Var1: "v1",
		Var2: 2,
		Child: SimpleChild{
			Name:      "theChild",
			Property1: 3, // should be ignored
			Property2: 0, // empty, should be omitted
		},
	}

	if err := enc.Encode(testStruct); err != nil {
		fmt.Printf("ERROR: %s\n", err.Error())
		return
	}

	// Output: Var1: v1
	// variable_2: 2
	// Child:
	//   Name: theChild
}

// Encode test with simple test struct to test encode with ignored fields
func ExampleEncoder_Encode_simple() {
	enc, err := human.NewEncoder(os.Stdout)
	if err != nil {
		return
	}
	testStruct := SimpleTest{
		Var1: "v1",
		Var2: 2,
		Child: SimpleChild{
			Name:      "theChild",
			Property1: 3, // should be ignored
			Property2: 4.5,
		},
	}

	if err := enc.Encode(testStruct); err != nil {
		fmt.Printf("ERROR: %s\n", err.Error())
		return
	}

	// Output: Var1: v1
	// variable_2: 2
	// Child:
	//   Name: theChild
	//   Property2: 4.5
}

// Encode test with two maps.
// Map: map string -> struct
// StructMap: map struct -> int
func ExampleEncoder_Encode_simpleMap() {
	enc, err := human.NewEncoder(os.Stdout)
	if err != nil {
		return
	}

	child1 := SimpleChild{
		Name:      "Person1",
		Property2: 4.5,
		Property1: 0, // should be ignored
	}

	child2 := SimpleChild{
		Name: "Person2",
	}
	stringMap := map[string]SimpleChild{
		"One": child1,
		"Two": child2,
	}
	structMap := map[SimpleChild]uint8{
		child1: 1,
		child2: 2,
	}
	testStruct := MapTest{
		Map:       stringMap,
		StructMap: structMap,
	}

	if err := enc.Encode(testStruct); err != nil {
		fmt.Printf("ERROR: %s\n", err.Error())
		return
	}

	// Output: Val1: 0
	// Map:
	//   * One: Name: Person1
	//     Property2: 4.5
	//   * Two: Name: Person2
	// StructMap:
	//   * {Person1 0 4.5}: 1
	//   * {Person2 0 0}: 2
}

// Encode test with slice of integers and slice of structs
func ExampleEncoder_Encode_simpleSlice() {
	enc, err := human.NewEncoder(os.Stdout)
	if err != nil {
		return
	}

	child1 := SimpleChild{
		Name:      "Person1",
		Property2: 4.5,
		Property1: 0, // should be ignored
	}

	child2 := SimpleChild{
		Name: "Person2",
	}
	structSlice := []SimpleChild{child1, child2}
	testStruct := SliceTest{
		IntSlice:    []int{1, 2, 3, 4, 5},
		StructSlice: structSlice,
	}

	// Output: IntSlice:
	//   * 1
	//   * 2
	//   * 3
	//   * 4
	//   * 5
	// StructSlice:
	//   * Name: Person1
	//     Property2: 4.5
	//   * Name: Person2

	if err := enc.Encode(testStruct); err != nil {
		fmt.Printf("ERROR: %s\n", err.Error())
		return
	}
}

// Encode test with slice of maps and each map is of type map[string]int
func ExampleEncoder_Encode_structMapSlice() {
	enc, err := human.NewEncoder(os.Stdout, human.OptionListSymbols("+", "-"))
	if err != nil {
		return
	}

	mapSliceElement1 := map[string]int{
		"one": 1,
		"two": 2,
		"tenthousandonehundredfourtytwo": 10142,
	}
	slice := []map[string]int{mapSliceElement1, mapSliceElement1}
	testStruct := MapSliceTest{
		StructMapSlice: slice,
	}

	if err := enc.Encode(testStruct); err != nil {
		fmt.Printf("ERROR: %s\n", err.Error())
		return
	}

	//Output: StructMapSlice:
	//   +
	//     - one: 1
	//     - tenthousandonehundredfourtytwo: 10142
	//     - two: 2
	//   +
	//     - one: 1
	//     - tenthousandonehundredfourtytwo: 10142
	//     - two: 2

}

// Encode test with TextMarshaler implemented by field
func ExampleEncoder_Encode_textMarshaler() {
	enc, err := human.NewEncoder(os.Stdout)
	if err != nil {
		return
	}

	addr := TextMarshalerTest{
		Ip: net.ParseIP("127.0.0.1"),
	}
	if err := enc.Encode(addr); err != nil {
		fmt.Printf("ERROR: %s\n", err.Error())
		return
	}

	// Output: Ip:[49 50 55 46 48 46 48 46 49]

}

// Encode test with invalid tag name
func ExampleEncoder_Encode_tagError() {
	enc, err := human.NewEncoder(os.Stdout)
	if err != nil {
		return
	}

	testStruct := TagFailTest{
		Test: 1,
	}

	if err := enc.Encode(testStruct); err != nil {
		fmt.Printf("ERROR: %s\n", err.Error())
		return
	}

	// Output: ERROR: 1 error occurred:
	//
	// * Invalid tag: '&§/$'
	//
}

// Encode test with anonymous field
func ExampleEncoder_Encode_anonymousFiled() {
	enc, err := human.NewEncoder(os.Stdout)
	if err != nil {
		return
	}

	//anonymous int field is ignored
	testStruct := AnonymousFieldTest{
		Text: "test",
	}

	if err := enc.Encode(testStruct); err != nil {
		fmt.Printf("ERROR: %s\n", err.Error())
		return
	}

	// Output: Text: test
}

// Encode test with unexported field
func ExampleEncoder_Encode_unexportedField() {
	enc, err := human.NewEncoder(os.Stdout)
	if err != nil {
		return
	}

	// unexported int field is ignored
	testStruct := UnexportedFieldTest{
		Text: "test",
	}

	if err := enc.Encode(testStruct); err != nil {
		fmt.Printf("ERROR: %s\n", err.Error())
		return
	}

	// Output: Text: test
}

// Encode test iterating slice of structs
func ExampleEncoder_Encode_iteratingSlice() {
	enc, err := human.NewEncoder(os.Stdout)
	if err != nil {
		return
	}

	child1 := SimpleChild{
		Name:      "Person1",
		Property2: 4.5,
		Property1: 0, // should be ignored
	}

	child2 := SimpleChild{
		Name:      "Person2",
		Property2: 4.5,
		Property1: 0, // should be ignored
	}

	structSlice := []SimpleChild{child1, child2}
	testStruct := SliceTest{
		StructSlice: structSlice,
	}

	// Output: Name: Person1
	// Property2: 4.5
	// Name: Person2
	// Property2: 4.5

	for _, s := range testStruct.StructSlice {
		if err := enc.Encode(s); err != nil {
			fmt.Printf("ERROR; %s\n", err)
			return
		}
	}

}
<commit_msg>Add failing tests for anonymous struct pointer<commit_after>package human_test

import (
	"fmt"
	"github.com/anexia-it/go-human"
	"net"
	"os"
)

// SimpleChild test struct
type SimpleChild struct {
	Name      string  // no tag
	Property1 uint64  `human:"-"`          // Ignored
	Property2 float64 `human:",omitempty"` // Omitted if empty
}

// SimpleTest test struct
type SimpleTest struct {
	Var1  string      // no tag
	Var2  int         `human:"variable_2"`
	Child SimpleChild // embedded struct
}

// MapTest test struct
type MapTest struct {
	Val1      uint64
	Map       map[string]SimpleChild
	StructMap map[SimpleChild]uint8
}

// SliceTest test struct
type SliceTest struct {
	IntSlice    []int
	StructSlice []SimpleChild
}

// MapSliceTest test struct with slice of maps
type MapSliceTest struct {
	StructMapSlice []map[string]int
}

// TextMarshalerTest test struct
type TextMarshalerTest struct {
	Ip net.IP // implements encoding.TextMarshaler interface
}

// TagFailTest test struct
type TagFailTest struct {
	Test int `human:"&§/$"` // invalid tag name
}

// AnonymousFieldTest test struct
type AnonymousFieldTest struct {
	int  // ignored
	Text string
}

// UnexportedFieldTest test struct
type UnexportedFieldTest struct {
	unexported int  // ignored
	Text string
}

type AnonymousStructPtrInnerTest struct {
	Inner string
}

type AnonymousStructPtrTest struct {
	*AnonymousStructPtrInnerTest
	Outer string
}

// Encode test with simple test struct to test encode with ignored fields and omit if field is empty
func ExampleEncoder_Encode_simpleOmitEmpty() {
	enc, err := human.NewEncoder(os.Stdout)
	if err != nil {
		return
	}
	testStruct := SimpleTest{
		Var1: "v1",
		Var2: 2,
		Child: SimpleChild{
			Name:      "theChild",
			Property1: 3, // should be ignored
			Property2: 0, // empty, should be omitted
		},
	}

	if err := enc.Encode(testStruct); err != nil {
		fmt.Printf("ERROR: %s\n", err.Error())
		return
	}

	// Output: Var1: v1
	// variable_2: 2
	// Child:
	//   Name: theChild
}

// Encode test with simple test struct to test encode with ignored fields
func ExampleEncoder_Encode_simple() {
	enc, err := human.NewEncoder(os.Stdout)
	if err != nil {
		return
	}
	testStruct := SimpleTest{
		Var1: "v1",
		Var2: 2,
		Child: SimpleChild{
			Name:      "theChild",
			Property1: 3, // should be ignored
			Property2: 4.5,
		},
	}

	if err := enc.Encode(testStruct); err != nil {
		fmt.Printf("ERROR: %s\n", err.Error())
		return
	}

	// Output: Var1: v1
	// variable_2: 2
	// Child:
	//   Name: theChild
	//   Property2: 4.5
}

// Encode test with two maps.
// Map: map string -> struct
// StructMap: map struct -> int
func ExampleEncoder_Encode_simpleMap() {
	enc, err := human.NewEncoder(os.Stdout)
	if err != nil {
		return
	}

	child1 := SimpleChild{
		Name:      "Person1",
		Property2: 4.5,
		Property1: 0, // should be ignored
	}

	child2 := SimpleChild{
		Name: "Person2",
	}
	stringMap := map[string]SimpleChild{
		"One": child1,
		"Two": child2,
	}
	structMap := map[SimpleChild]uint8{
		child1: 1,
		child2: 2,
	}
	testStruct := MapTest{
		Map:       stringMap,
		StructMap: structMap,
	}

	if err := enc.Encode(testStruct); err != nil {
		fmt.Printf("ERROR: %s\n", err.Error())
		return
	}

	// Output: Val1: 0
	// Map:
	//   * One: Name: Person1
	//     Property2: 4.5
	//   * Two: Name: Person2
	// StructMap:
	//   * {Person1 0 4.5}: 1
	//   * {Person2 0 0}: 2
}

// Encode test with slice of integers and slice of structs
func ExampleEncoder_Encode_simpleSlice() {
	enc, err := human.NewEncoder(os.Stdout)
	if err != nil {
		return
	}

	child1 := SimpleChild{
		Name:      "Person1",
		Property2: 4.5,
		Property1: 0, // should be ignored
	}

	child2 := SimpleChild{
		Name: "Person2",
	}
	structSlice := []SimpleChild{child1, child2}
	testStruct := SliceTest{
		IntSlice:    []int{1, 2, 3, 4, 5},
		StructSlice: structSlice,
	}

	// Output: IntSlice:
	//   * 1
	//   * 2
	//   * 3
	//   * 4
	//   * 5
	// StructSlice:
	//   * Name: Person1
	//     Property2: 4.5
	//   * Name: Person2

	if err := enc.Encode(testStruct); err != nil {
		fmt.Printf("ERROR: %s\n", err.Error())
		return
	}
}

// Encode test with slice of maps and each map is of type map[string]int
func ExampleEncoder_Encode_structMapSlice() {
	enc, err := human.NewEncoder(os.Stdout, human.OptionListSymbols("+", "-"))
	if err != nil {
		return
	}

	mapSliceElement1 := map[string]int{
		"one": 1,
		"two": 2,
		"tenthousandonehundredfourtytwo": 10142,
	}
	slice := []map[string]int{mapSliceElement1, mapSliceElement1}
	testStruct := MapSliceTest{
		StructMapSlice: slice,
	}

	if err := enc.Encode(testStruct); err != nil {
		fmt.Printf("ERROR: %s\n", err.Error())
		return
	}

	//Output: StructMapSlice:
	//   +
	//     - one: 1
	//     - tenthousandonehundredfourtytwo: 10142
	//     - two: 2
	//   +
	//     - one: 1
	//     - tenthousandonehundredfourtytwo: 10142
	//     - two: 2

}

// Encode test with TextMarshaler implemented by field
func ExampleEncoder_Encode_textMarshaler() {
	enc, err := human.NewEncoder(os.Stdout)
	if err != nil {
		return
	}

	addr := TextMarshalerTest{
		Ip: net.ParseIP("127.0.0.1"),
	}
	if err := enc.Encode(addr); err != nil {
		fmt.Printf("ERROR: %s\n", err.Error())
		return
	}

	// Output: Ip:[49 50 55 46 48 46 48 46 49]

}

// Encode test with invalid tag name
func ExampleEncoder_Encode_tagError() {
	enc, err := human.NewEncoder(os.Stdout)
	if err != nil {
		return
	}

	testStruct := TagFailTest{
		Test: 1,
	}

	if err := enc.Encode(testStruct); err != nil {
		fmt.Printf("ERROR: %s\n", err.Error())
		return
	}

	// Output: ERROR: 1 error occurred:
	//
	// * Invalid tag: '&§/$'
	//
}

// Encode test with anonymous field
func ExampleEncoder_Encode_anonymousFiled() {
	enc, err := human.NewEncoder(os.Stdout)
	if err != nil {
		return
	}

	//anonymous int field is ignored
	testStruct := AnonymousFieldTest{
		Text: "test",
	}

	if err := enc.Encode(testStruct); err != nil {
		fmt.Printf("ERROR: %s\n", err.Error())
		return
	}

	// Output: Text: test
}

// Encode test with unexported field
func ExampleEncoder_Encode_unexportedField() {
	enc, err := human.NewEncoder(os.Stdout)
	if err != nil {
		return
	}

	// unexported int field is ignored
	testStruct := UnexportedFieldTest{
		Text: "test",
	}

	if err := enc.Encode(testStruct); err != nil {
		fmt.Printf("ERROR: %s\n", err.Error())
		return
	}

	// Output: Text: test
}

// Encode test iterating slice of structs
func ExampleEncoder_Encode_iteratingSlice() {
	enc, err := human.NewEncoder(os.Stdout)
	if err != nil {
		return
	}

	child1 := SimpleChild{
		Name:      "Person1",
		Property2: 4.5,
		Property1: 0, // should be ignored
	}

	child2 := SimpleChild{
		Name:      "Person2",
		Property2: 4.5,
		Property1: 0, // should be ignored
	}

	structSlice := []SimpleChild{child1, child2}
	testStruct := SliceTest{
		StructSlice: structSlice,
	}

	// Output: Name: Person1
	// Property2: 4.5
	// Name: Person2
	// Property2: 4.5

	for _, s := range testStruct.StructSlice {
		if err := enc.Encode(s); err != nil {
			fmt.Printf("ERROR: %s\n", err.Error())
			return
		}
	}
}

func ExampleEncoder_Encode_AnonymousStructPointer() {
	enc, err := human.NewEncoder(os.Stdout)
	if err != nil {
		return
	}

	outer := &AnonymousStructPtrTest{
		AnonymousStructPtrInnerTest: &AnonymousStructPtrInnerTest{
			Inner: "inner",
		},
		Outer: "outer",
	}

	// Output: Outer: outer
	// Inner: inner
	if err = enc.Encode(outer); err != nil {
		fmt.Printf("ERROR: %s\n", err.Error())
	}
}
<|endoftext|>


<commit_before>package jsoniter_test

import (
	"fmt"
	"github.com/json-iterator/go"
	"os"
	"encoding/json"
)

func ExampleMarshal() {
	type ColorGroup struct {
		ID     int
		Name   string
		Colors []string
	}
	group := ColorGroup{
		ID:     1,
		Name:   "Reds",
		Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
	}
	b, err := jsoniter.Marshal(group)
	if err != nil {
		fmt.Println("error:", err)
	}
	os.Stdout.Write(b)
	// Output:
	// {"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}
}

func ExampleUnMarshal() {
	var jsonBlob = []byte(`[
		{"Name": "Platypus", "Order": "Monotremata"},
		{"Name": "Quoll",    "Order": "Dasyuromorphia"}
	]`)
	type Animal struct {
	Name  string
	Order string
	}
	var animals []Animal
	err := json.Unmarshal(jsonBlob, &animals)
	if err != nil {
	fmt.Println("error:", err)
	}
	fmt.Printf("%+v", animals)
	// Output:
	// [{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]
}<commit_msg>example unmarshal<commit_after>package jsoniter_test

import (
	"fmt"
	"github.com/json-iterator/go"
	"os"
	"encoding/json"
)

func ExampleMarshal() {
	type ColorGroup struct {
		ID     int
		Name   string
		Colors []string
	}
	group := ColorGroup{
		ID:     1,
		Name:   "Reds",
		Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
	}
	b, err := jsoniter.Marshal(group)
	if err != nil {
		fmt.Println("error:", err)
	}
	os.Stdout.Write(b)
	// Output:
	// {"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}
}

func ExampleUnmarshal() {
	var jsonBlob = []byte(`[
		{"Name": "Platypus", "Order": "Monotremata"},
		{"Name": "Quoll",    "Order": "Dasyuromorphia"}
	]`)
	type Animal struct {
	Name  string
	Order string
	}
	var animals []Animal
	err := json.Unmarshal(jsonBlob, &animals)
	if err != nil {
	fmt.Println("error:", err)
	}
	fmt.Printf("%+v", animals)
	// Output:
	// [{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]
}<|endoftext|>


<commit_before>package mpb_test

import (
	"math/rand"
	"time"

	"github.com/vbauerster/mpb"
)

func Example() {
	// Star mpb's rendering goroutine.
	// If you don't plan to cancel, feed with nil
	// otherwise provide context.Context, see cancel example
	p := mpb.New(nil)
	// Set custom width for every bar, which mpb will contain
	// The default one in 70
	p.SetWidth(80)
	// Set custom format for every bar, the default one is "[=>-]"
	p.Format("╢▌▌░╟")
	// Set custom refresh rate, the default one is 100 ms
	p.RefreshRate(120 * time.Millisecond)

	// Add a bar. You're not limited to just one bar, add many if you need.
	bar := p.AddBar(100).PrependName("Single Bar:", 0).AppendPercentage()

	for i := 0; i < 100; i++ {
		bar.Incr(1) // increment progress bar
		time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
	}

	// Don't forget to stop mpb's rendering goroutine
	p.Stop()

	// You cannot add bars after p.Stop() has been called
	// p.AddBar(100) // will panic
}

func ExampleBar_InProgress() {
	p := mpb.New(nil)
	bar := p.AddBar(100).AppendPercentage()

	for bar.InProgress() {
		bar.Incr(1)
		time.Sleep(time.Millisecond * 20)
	}
}
<commit_msg>AppendFunc, PrependFunc examples<commit_after>package mpb_test

import (
	"fmt"
	"math/rand"
	"time"

	"github.com/vbauerster/mpb"
)

func Example() {
	// Star mpb's rendering goroutine.
	// If you don't plan to cancel, feed with nil
	// otherwise provide context.Context, see cancel example
	p := mpb.New(nil)
	// Set custom width for every bar, which mpb will contain
	// The default one in 70
	p.SetWidth(80)
	// Set custom format for every bar, the default one is "[=>-]"
	p.Format("╢▌▌░╟")
	// Set custom refresh rate, the default one is 100 ms
	p.RefreshRate(120 * time.Millisecond)

	// Add a bar. You're not limited to just one bar, add many if you need.
	bar := p.AddBar(100).PrependName("Single Bar:", 0).AppendPercentage()

	for i := 0; i < 100; i++ {
		bar.Incr(1) // increment progress bar
		time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
	}

	// Don't forget to stop mpb's rendering goroutine
	p.Stop()

	// You cannot add bars after p.Stop() has been called
	// p.AddBar(100) // will panic
}

func ExampleBar_InProgress() {
	p := mpb.New(nil)
	bar := p.AddBar(100).AppendPercentage()

	for bar.InProgress() {
		bar.Incr(1)
		time.Sleep(time.Millisecond * 20)
	}
}

func ExampleBar_PrependFunc() {
	decor := func(s *mpb.Statistics) string {
		str := fmt.Sprintf("%d/%d", s.Current, s.Total)
		return fmt.Sprintf("%8s", str)
	}

	totalItem := 100
	p := mpb.New(nil)
	bar := p.AddBar(int64(totalItem)).PrependFunc(decor)

	for i := 0; i < totalItem; i++ {
		bar.Incr(1) // increment progress bar
		time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
	}
}

func ExampleBar_AppendFunc() {
	decor := func(s *mpb.Statistics) string {
		str := fmt.Sprintf("%d/%d", s.Current, s.Total)
		return fmt.Sprintf("%8s", str)
	}

	totalItem := 100
	p := mpb.New(nil)
	bar := p.AddBar(int64(totalItem)).AppendFunc(decor)

	for i := 0; i < totalItem; i++ {
		bar.Incr(1) // increment progress bar
		time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
	}
}
<|endoftext|>


<commit_before>package file

import (
	"fmt"
	"regexp"
	"strconv"
)

var appVersion = "2.1.0"

// GetAppVersion return app version
func GetAppVersion() string {
	return appVersion
}

// getAppDbVersion return app version recorded in database
func getAppDbVersion() (string, error) {
	var version string

	res, err := db.Query("select id from version")

	if err != nil {
		return "", fmt.Errorf("Can't retrieve database app version")
	}

	defer func() {
		if e := res.Close(); e != nil {
			err = e
		}
	}()

	if err == nil {
		for res.Next() {
			err = res.Scan(&version)
		}
	}

	return version, err
}

// HasMajorVersionEqualFrom check if major version in given version is equal to app version
func HasMajorVersionEqualFrom() (bool, error) {
	re := regexp.MustCompile(`^(\d)\.(\d)\.(\d)(?:(?:\-|\+).*)?`)

	dbVer, err := getAppDbVersion()

	if err != nil {
		return false, err
	}

	dbVerComp := re.FindStringSubmatch(dbVer)
	appVerComp := re.FindStringSubmatch(appVersion)

	if len(dbVerComp) != 4 {
		return false, fmt.Errorf("Wrong version format : %s, must follows semver", dbVer)
	}

	appVerMajor, err := strconv.Atoi(appVerComp[1])

	if err != nil {
		return false, err
	}

	verMajor, err := strconv.Atoi(dbVerComp[1])

	if err != nil {
		return false, err
	}

	if verMajor == appVerMajor {
		return true, nil
	}

	return false, fmt.Errorf("Database version : %s and app version : %s don't have same major version", dbVer, appVersion)
}
<commit_msg>feat(version) : 2.1.1<commit_after>package file

import (
	"fmt"
	"regexp"
	"strconv"
)

var appVersion = "2.1.1"

// GetAppVersion return app version
func GetAppVersion() string {
	return appVersion
}

// getAppDbVersion return app version recorded in database
func getAppDbVersion() (string, error) {
	var version string

	res, err := db.Query("select id from version")

	if err != nil {
		return "", fmt.Errorf("Can't retrieve database app version")
	}

	defer func() {
		if e := res.Close(); e != nil {
			err = e
		}
	}()

	if err == nil {
		for res.Next() {
			err = res.Scan(&version)
		}
	}

	return version, err
}

// HasMajorVersionEqualFrom check if major version in given version is equal to app version
func HasMajorVersionEqualFrom() (bool, error) {
	re := regexp.MustCompile(`^(\d)\.(\d)\.(\d)(?:(?:\-|\+).*)?`)

	dbVer, err := getAppDbVersion()

	if err != nil {
		return false, err
	}

	dbVerComp := re.FindStringSubmatch(dbVer)
	appVerComp := re.FindStringSubmatch(appVersion)

	if len(dbVerComp) != 4 {
		return false, fmt.Errorf("Wrong version format : %s, must follows semver", dbVer)
	}

	appVerMajor, err := strconv.Atoi(appVerComp[1])

	if err != nil {
		return false, err
	}

	verMajor, err := strconv.Atoi(dbVerComp[1])

	if err != nil {
		return false, err
	}

	if verMajor == appVerMajor {
		return true, nil
	}

	return false, fmt.Errorf("Database version : %s and app version : %s don't have same major version", dbVer, appVersion)
}
<|endoftext|>


<commit_before>package models

import (
	"fmt"

	"github.com/eirka/eirka-libs/config"
	"github.com/eirka/eirka-libs/db"
	e "github.com/eirka/eirka-libs/errors"
	"github.com/eirka/eirka-libs/validate"
)

// TagSearchModel holds the parameters from the request and also the key for the cache
type TagSearchModel struct {
	Ib     uint
	Term   string
	Result TagSearchType
}

// TagSearchType is the top level of the JSON response
type TagSearchType struct {
	Body []Tags `json:"tagsearch"`
}

// Get will gather the information from the database and return it as JSON serialized data
func (i *TagSearchModel) Get() (err error) {

	// Initialize response header
	response := TagSearchType{}

	// Get Database handle
	dbase, err := db.GetDb()
	if err != nil {
		return
	}

	tags := []Tags{}

	// Validate tag input
	if i.Term != "" {
		tag := validate.Validate{Input: i.Term, Max: config.Settings.Limits.TagMaxLength, Min: config.Settings.Limits.TagMinLength}
		if tag.MinLength() {
			return e.ErrInvalidParam
		} else if tag.MaxLength() {
			return e.ErrInvalidParam
		}
	}

	rows, err := dbase.Query(`SELECT count,tag_id,tag_name,tagtype_id
	FROM (SELECT count(image_id) as count,ib_id,tags.tag_id,tag_name,tagtype_id
	FROM tags
	LEFT JOIN tagmap on tags.tag_id = tagmap.tag_id
	WHERE ib_id = ? AND MATCH(tag_name) AGAINST ('?*' IN BOOLEAN MODE)
	group by tag_id) as a
	ORDER BY tag_name ASC`, i.Ib, i.Term)
	if err != nil {
		return
	}

	for rows.Next() {
		// Initialize posts struct
		tag := Tags{}
		// Scan rows and place column into struct
		err := rows.Scan(&tag.Total, &tag.Id, &tag.Tag, &tag.Type)
		if err != nil {
			return err
		}

		// Append rows to info struct
		tags = append(tags, tag)
	}
	err = rows.Err()
	if err != nil {
		return
	}

	// Add pagedresponse to the response struct
	response.Body = tags

	// This is the data we will serialize
	i.Result = response

	return

}
<commit_msg>use fulltext index for searches<commit_after>package models

import (
	"github.com/eirka/eirka-libs/config"
	"github.com/eirka/eirka-libs/db"
	e "github.com/eirka/eirka-libs/errors"
	"github.com/eirka/eirka-libs/validate"
)

// TagSearchModel holds the parameters from the request and also the key for the cache
type TagSearchModel struct {
	Ib     uint
	Term   string
	Result TagSearchType
}

// TagSearchType is the top level of the JSON response
type TagSearchType struct {
	Body []Tags `json:"tagsearch"`
}

// Get will gather the information from the database and return it as JSON serialized data
func (i *TagSearchModel) Get() (err error) {

	// Initialize response header
	response := TagSearchType{}

	// Get Database handle
	dbase, err := db.GetDb()
	if err != nil {
		return
	}

	tags := []Tags{}

	// Validate tag input
	if i.Term != "" {
		tag := validate.Validate{Input: i.Term, Max: config.Settings.Limits.TagMaxLength, Min: config.Settings.Limits.TagMinLength}
		if tag.MinLength() {
			return e.ErrInvalidParam
		} else if tag.MaxLength() {
			return e.ErrInvalidParam
		}
	}

	rows, err := dbase.Query(`SELECT count,tag_id,tag_name,tagtype_id
	FROM (SELECT count(image_id) as count,ib_id,tags.tag_id,tag_name,tagtype_id
	FROM tags
	LEFT JOIN tagmap on tags.tag_id = tagmap.tag_id
	WHERE ib_id = ? AND MATCH(tag_name) AGAINST ('?*' IN BOOLEAN MODE)
	group by tag_id) as a
	ORDER BY tag_name ASC`, i.Ib, i.Term)
	if err != nil {
		return
	}

	for rows.Next() {
		// Initialize posts struct
		tag := Tags{}
		// Scan rows and place column into struct
		err := rows.Scan(&tag.Total, &tag.Id, &tag.Tag, &tag.Type)
		if err != nil {
			return err
		}

		// Append rows to info struct
		tags = append(tags, tag)
	}
	err = rows.Err()
	if err != nil {
		return
	}

	// Add pagedresponse to the response struct
	response.Body = tags

	// This is the data we will serialize
	i.Result = response

	return

}
<|endoftext|>


<commit_before>package models

import "log"

// It is very important that NewValueSetMap gets called to get a populated ValueSetMap
type ValueSetMap map[string][]CodeSet

func NewValueSetMap(vs []ValueSet) ValueSetMap {
	vsMap := make(ValueSetMap)
	for _, valueSet := range vs {
		vsMap[valueSet.Oid] = valueSet.CodeSetMap()
	}
	return vsMap
}

// ValueSet is a set of concepts relating to a particular topic
type ValueSet struct {
	Oid         string    `json:"oid"`
	DisplayName string    `json:"display_name"`
	Version     string    `json:"version"`
	Concepts    []Concept `json:"concepts"`
}

// CodeSetMap returns a slice of CodeSets, each containing Concepts for a different code system
func (vs *ValueSet) CodeSetMap() []CodeSet {
	var cs = make(map[string][]Concept)
	for _, concept := range vs.Concepts {
		cs[concept.CodeSystemName] = append(cs[concept.CodeSystemName], concept)
	}
	var retVal = []CodeSet{}
	for key, val := range cs {
		retVal = append(retVal, CodeSet{Set: key, Values: val})
	}
	return retVal
}

// Concept is a mapping between code/system/version and a display name
type Concept struct {
	Code              string `json:"code"`
	CodeSystem        string `json:"code_system"`
	CodeSystemName    string `json:"code_system_name"`
	CodeSystemVersion string `json:"code_system_version"`
	DisplayName       string `json:"display_name"`
}

// CodeSet is a struct grouping Concepts by CodeSystem
type CodeSet struct {
	Set    string
	Values []Concept
}

func (v ValueSetMap) CodeDisplayWithPreferredCodeForField(entry *Entry, coded *Coded, MapDataCriteria Mdc, codeType string, field string) CodeDisplay {
	codeDisplay, err := entry.GetCodeDisplay(codeType)
	if err != nil {
		log.Fatalln(err)
	}
	codeDisplay.MapDataCriteria = MapDataCriteria
	for i := range MapDataCriteria.FieldOids[field] {
		codeDisplay.PreferredCode = coded.PreferredCode(codeDisplay.PreferredCodeSets, codeDisplay.CodeSetRequired, codeDisplay.ValueSetPreferred, v, MapDataCriteria.FieldOids[field][i])
		if codeDisplay.PreferredCode.Code != "" {
			//Put the relevant oid in the 0 index for export
			oldoid := MapDataCriteria.FieldOids[field][0]
			MapDataCriteria.FieldOids[field][0] = MapDataCriteria.FieldOids[field][i]
			MapDataCriteria.FieldOids[field][i] = oldoid
			break
		}
	}
	return codeDisplay
}

func (v ValueSetMap) CodeDisplayWithPreferredCode(entry *Entry, coded *Coded, MapDataCriteria Mdc, codeType string) CodeDisplay {
	codeDisplay, err := entry.GetCodeDisplay(codeType)
	if err != nil {
		log.Fatalln(err)
	}
	codeDisplay.MapDataCriteria = MapDataCriteria
	codeDisplay.PreferredCode = coded.PreferredCode(codeDisplay.PreferredCodeSets, codeDisplay.CodeSetRequired, codeDisplay.ValueSetPreferred, v, MapDataCriteria.ValueSetOid)
	return codeDisplay
}

func (v ValueSetMap) CodeDisplayWithPreferredCodeForResultValue(entry *Entry, coded *Coded, MapDataCriteria Mdc, codeType string) CodeDisplay {
	codeDisplay, err := entry.GetCodeDisplay(codeType)
	if err != nil {
		log.Fatalln(err)
	}
	codeDisplay.MapDataCriteria = MapDataCriteria
	for ResultOidInd := 0; ResultOidInd < len(MapDataCriteria.ResultOids); ResultOidInd++ {
		codeDisplay.PreferredCode = coded.PreferredCode(codeDisplay.PreferredCodeSets, codeDisplay.CodeSetRequired, codeDisplay.ValueSetPreferred, v, MapDataCriteria.ResultOids[ResultOidInd])
		if codeDisplay.PreferredCode.Code != "" {
			break
		}
	}
	return codeDisplay
}

func (v ValueSetMap) CodeDisplayWithPreferredCodeAndLaterality(entry *Entry, coded *Coded, codeType string, laterality Laterality, MapDataCriteria Mdc) CodeDisplay {
	codeDisplay, err := entry.GetCodeDisplay(codeType)
	if err != nil {
		log.Fatal(err)
	}
	codeDisplay.PreferredCode = coded.PreferredCode(codeDisplay.PreferredCodeSets, codeDisplay.CodeSetRequired, codeDisplay.ValueSetPreferred, v, MapDataCriteria.ValueSetOid)
	codeDisplay.Laterality = laterality
	codeDisplay.MapDataCriteria = MapDataCriteria
	return codeDisplay
}

func (v ValueSetMap) OidForCode(codedValue CodedConcept, valuesetOids []string) string {
	for _, vsoid := range valuesetOids {
		oidlist := v[vsoid]
		if codeSetContainsCode(oidlist, codedValue) {
			return vsoid
		}
	}
	return ""
}

func codeSetContainsCode(sets []CodeSet, codedValue CodedConcept) bool {
	for _, cs := range sets {
		for _, val := range cs.Values {
			if (val.CodeSystem == codedValue.CodeSystem ||
				val.CodeSystemName == codedValue.CodeSystemName ||
				val.CodeSystemName == codedValue.CodeSystem) &&
				val.Code == codedValue.Code {
				return true
			}
		}
	}
	return false
}
<commit_msg>Add oid to the range<commit_after>package models

import "log"

// It is very important that NewValueSetMap gets called to get a populated ValueSetMap
type ValueSetMap map[string][]CodeSet

func NewValueSetMap(vs []ValueSet) ValueSetMap {
	vsMap := make(ValueSetMap)
	for _, valueSet := range vs {
		vsMap[valueSet.Oid] = valueSet.CodeSetMap()
	}
	return vsMap
}

// ValueSet is a set of concepts relating to a particular topic
type ValueSet struct {
	Oid         string    `json:"oid"`
	DisplayName string    `json:"display_name"`
	Version     string    `json:"version"`
	Concepts    []Concept `json:"concepts"`
}

// CodeSetMap returns a slice of CodeSets, each containing Concepts for a different code system
func (vs *ValueSet) CodeSetMap() []CodeSet {
	var cs = make(map[string][]Concept)
	for _, concept := range vs.Concepts {
		cs[concept.CodeSystemName] = append(cs[concept.CodeSystemName], concept)
	}
	var retVal = []CodeSet{}
	for key, val := range cs {
		retVal = append(retVal, CodeSet{Set: key, Values: val})
	}
	return retVal
}

// Concept is a mapping between code/system/version and a display name
type Concept struct {
	Code              string `json:"code"`
	CodeSystem        string `json:"code_system"`
	CodeSystemName    string `json:"code_system_name"`
	CodeSystemVersion string `json:"code_system_version"`
	DisplayName       string `json:"display_name"`
}

// CodeSet is a struct grouping Concepts by CodeSystem
type CodeSet struct {
	Set    string
	Values []Concept
}

func (v ValueSetMap) CodeDisplayWithPreferredCodeForField(entry *Entry, coded *Coded, MapDataCriteria Mdc, codeType string, field string) CodeDisplay {
	codeDisplay, err := entry.GetCodeDisplay(codeType)
	if err != nil {
		log.Fatalln(err)
	}
	codeDisplay.MapDataCriteria = MapDataCriteria
	for i, oid := range MapDataCriteria.FieldOids[field] {
		codeDisplay.PreferredCode = coded.PreferredCode(codeDisplay.PreferredCodeSets, codeDisplay.CodeSetRequired, codeDisplay.ValueSetPreferred, v, MapDataCriteria.FieldOids[field][i])
		if codeDisplay.PreferredCode.Code != "" {
			//Put the relevant oid in the 0 index for export
			oldoid := MapDataCriteria.FieldOids[field][0]
			MapDataCriteria.FieldOids[field][0] = oid
			MapDataCriteria.FieldOids[field][i] = oldoid
			break
		}
	}
	return codeDisplay
}

func (v ValueSetMap) CodeDisplayWithPreferredCode(entry *Entry, coded *Coded, MapDataCriteria Mdc, codeType string) CodeDisplay {
	codeDisplay, err := entry.GetCodeDisplay(codeType)
	if err != nil {
		log.Fatalln(err)
	}
	codeDisplay.MapDataCriteria = MapDataCriteria
	codeDisplay.PreferredCode = coded.PreferredCode(codeDisplay.PreferredCodeSets, codeDisplay.CodeSetRequired, codeDisplay.ValueSetPreferred, v, MapDataCriteria.ValueSetOid)
	return codeDisplay
}

func (v ValueSetMap) CodeDisplayWithPreferredCodeForResultValue(entry *Entry, coded *Coded, MapDataCriteria Mdc, codeType string) CodeDisplay {
	codeDisplay, err := entry.GetCodeDisplay(codeType)
	if err != nil {
		log.Fatalln(err)
	}
	codeDisplay.MapDataCriteria = MapDataCriteria
	for ResultOidInd := 0; ResultOidInd < len(MapDataCriteria.ResultOids); ResultOidInd++ {
		codeDisplay.PreferredCode = coded.PreferredCode(codeDisplay.PreferredCodeSets, codeDisplay.CodeSetRequired, codeDisplay.ValueSetPreferred, v, MapDataCriteria.ResultOids[ResultOidInd])
		if codeDisplay.PreferredCode.Code != "" {
			break
		}
	}
	return codeDisplay
}

func (v ValueSetMap) CodeDisplayWithPreferredCodeAndLaterality(entry *Entry, coded *Coded, codeType string, laterality Laterality, MapDataCriteria Mdc) CodeDisplay {
	codeDisplay, err := entry.GetCodeDisplay(codeType)
	if err != nil {
		log.Fatal(err)
	}
	codeDisplay.PreferredCode = coded.PreferredCode(codeDisplay.PreferredCodeSets, codeDisplay.CodeSetRequired, codeDisplay.ValueSetPreferred, v, MapDataCriteria.ValueSetOid)
	codeDisplay.Laterality = laterality
	codeDisplay.MapDataCriteria = MapDataCriteria
	return codeDisplay
}

func (v ValueSetMap) OidForCode(codedValue CodedConcept, valuesetOids []string) string {
	for _, vsoid := range valuesetOids {
		oidlist := v[vsoid]
		if codeSetContainsCode(oidlist, codedValue) {
			return vsoid
		}
	}
	return ""
}

func codeSetContainsCode(sets []CodeSet, codedValue CodedConcept) bool {
	for _, cs := range sets {
		for _, val := range cs.Values {
			if (val.CodeSystem == codedValue.CodeSystem ||
				val.CodeSystemName == codedValue.CodeSystemName ||
				val.CodeSystemName == codedValue.CodeSystem) &&
				val.Code == codedValue.Code {
				return true
			}
		}
	}
	return false
}
<|endoftext|>


<commit_before>package main

import (
	"bufio"
	"bytes"
	"crypto/aes"
	"crypto/cipher"
	"crypto/rand"
	"crypto/sha1"
	"encoding/binary"
	"errors"
	"fmt"
	"golang.org/x/crypto/pbkdf2"
	"io"
	"os"
)

const keySize = 32
const nIterations = 4096

var hmacFun = sha1.New

var salt = [8]byte{20, 183, 239, 38, 44, 214, 22, 141}

type encryptionService struct {
	lockKey          []byte
	encryptedDataKey []byte
	backupDataKey    []byte
	reader           *bufio.Reader
}

func main() {
	s := &encryptionService{
		reader: bufio.NewReader(os.Stdin),
	}
	for {
		s.processCommand()
	}
}

func (s *encryptionService) readCommand() (byte, []byte) {
	var size uint16
	err := binary.Read(s.reader, binary.BigEndian, &size)
	if err != nil {
		processReadError(err)
	}
	if size < 1 {
		panic("Command is too short")
	}
	command, err := s.reader.ReadByte()
	if err != nil {
		processReadError(err)
	}
	if size == 1 {
		return command, nil
	}
	buf := make([]byte, size-1)
	n, err := s.reader.Read(buf)
	if err != nil {
		processReadError(err)
	}
	if uint16(n) != size-1 {
		panic("Error reading input")
	}
	return command, buf
}

func processReadError(err error) {
	if err == io.EOF {
		// parent died. close normally
		os.Exit(0)
	}
	panic(fmt.Sprintf("Error reading input %v", err))
}

func doReply(data []byte) {
	err := binary.Write(os.Stdout, binary.BigEndian, uint16(len(data)))
	if err != nil {
		panic(fmt.Sprintf("Error writing data %v", err))
	}
	os.Stdout.Write(data)
}

func replySuccessWithData(data []byte) {
	doReply(append([]byte{'S'}, data...))
}

func replySuccess() {
	doReply([]byte{'S'})
}

func replyError(error string) {
	doReply([]byte("E" + error))
}

func encodeKey(key []byte) []byte {
	if key == nil {
		return []byte{0}
	}
	return append([]byte{byte(len(key))}, key...)
}

func combineDataKeys(key1, key2 []byte) []byte {
	return append(encodeKey(key1), encodeKey(key2)...)
}

func (s *encryptionService) replySuccessWithDataKey() {
	replySuccessWithData(combineDataKeys(s.encryptedDataKey, s.backupDataKey))
}

func (s *encryptionService) processCommand() {
	command, data := s.readCommand()

	switch command {
	case 1:
		s.cmdSetPassword(data)
	case 2:
		s.cmdCreateDataKey()
	case 3:
		s.cmdSetDataKey(data)
	case 4:
		s.cmdGetDataKey()
	case 5:
		s.cmdEncrypt(data)
	case 6:
		s.cmdDecrypt(data)
	case 7:
		s.cmdChangePassword(data)
	case 8:
		s.cmdRotateDataKey()
	case 9:
		s.cmdClearBackupKey(data)
	default:
		panic(fmt.Sprintf("Unknown command %v", command))
	}
}

func (s *encryptionService) cmdSetPassword(data []byte) {
	s.lockKey = generateLockKey(data)
	replySuccess()
}

func (s *encryptionService) createDataKey() []byte {
	dataKey := make([]byte, keySize)
	if _, err := io.ReadFull(rand.Reader, dataKey); err != nil {
		panic(err.Error())
	}
	return aesgcmEncrypt(s.lockKey, dataKey)
}

func (s *encryptionService) cmdCreateDataKey() {
	if s.lockKey == nil {
		panic("Password was not set")
	}
	replySuccessWithData(combineDataKeys(s.createDataKey(), nil))
}

func readField(b []byte) ([]byte, []byte) {
	size := b[0]
	return b[1 : size+1], b[size+1:]
}

func (s *encryptionService) cmdSetDataKey(data []byte) {
	if s.lockKey == nil {
		panic("Password was not set")
	}
	encryptedDataKey, data := readField(data)
	backupDataKey, _ := readField(data)

	_, err := aesgcmDecrypt(s.lockKey, encryptedDataKey)
	if err != nil {
		replyError(err.Error())
		return
	}
	if len(backupDataKey) == 0 {
		s.backupDataKey = nil
	} else {
		_, err = aesgcmDecrypt(s.lockKey, backupDataKey)
		if err != nil {
			replyError(err.Error())
			return
		}
		s.backupDataKey = backupDataKey
	}
	s.encryptedDataKey = encryptedDataKey
	replySuccess()
}

func (s *encryptionService) cmdGetDataKey() {
	s.replySuccessWithDataKey()
}

func (s *encryptionService) cmdEncrypt(data []byte) {
	if s.lockKey == nil {
		panic("Password was not set")
	}
	dataKey, err := aesgcmDecrypt(s.lockKey, s.encryptedDataKey)
	if err != nil {
		replyError(err.Error())
		return
	}
	replySuccessWithData(aesgcmEncrypt(dataKey, data))
}

func (s *encryptionService) decryptWithKey(key []byte, data []byte) ([]byte, error) {
	if key == nil {
		return nil, errors.New("Unable to decrypt value")
	}
	dataKey, err := aesgcmDecrypt(s.lockKey, key)
	if err != nil {
		return nil, err
	}
	return aesgcmDecrypt(dataKey, data)
}

func (s *encryptionService) cmdDecrypt(data []byte) {
	if s.lockKey == nil {
		panic("Password was not set")
	}
	plaintext, err := s.decryptWithKey(s.encryptedDataKey, data)
	if err == nil {
		replySuccessWithData(plaintext)
		return
	}
	plaintext, err = s.decryptWithKey(s.backupDataKey, data)
	if err != nil {
		replyError(err.Error())
		return
	}
	replySuccessWithData(plaintext)
}

func (s *encryptionService) cmdChangePassword(data []byte) {
	if s.lockKey == nil {
		panic("Password was not set")
	}
	var backupDataKey []byte
	var err error
	if s.backupDataKey != nil {
		backupDataKey, err = aesgcmDecrypt(s.lockKey, s.backupDataKey)
		if err != nil {
			replyError(err.Error())
			return
		}
	}
	dataKey, err := aesgcmDecrypt(s.lockKey, s.encryptedDataKey)
	if err != nil {
		replyError(err.Error())
		return
	}
	s.lockKey = generateLockKey(data)
	s.encryptedDataKey = aesgcmEncrypt(s.lockKey, dataKey)
	if s.backupDataKey != nil {
		s.backupDataKey = aesgcmEncrypt(s.lockKey, backupDataKey)
	}
	s.replySuccessWithDataKey()
}

func (s *encryptionService) cmdRotateDataKey() {
	if s.lockKey == nil {
		panic("Password was not set")
	}
	if s.backupDataKey != nil {
		replyError("Data key rotation is in progress")
		return
	}
	s.backupDataKey = s.encryptedDataKey
	s.encryptedDataKey = s.createDataKey()
	s.replySuccessWithDataKey()
}

func (s *encryptionService) cmdClearBackupKey(keys []byte) {
	if !bytes.Equal(combineDataKeys(s.encryptedDataKey, s.backupDataKey), keys) {
		replyError("Key mismatch")
		return
	}
	if s.backupDataKey == nil {
		replySuccess()
		return
	}
	s.backupDataKey = nil
	s.replySuccessWithDataKey()
}

func generateLockKey(password []byte) []byte {
	return pbkdf2.Key(password, salt[:], nIterations, keySize, hmacFun)
}

func aesgcmEncrypt(key []byte, data []byte) []byte {
	block, err := aes.NewCipher(key)
	if err != nil {
		panic(err.Error())
	}
	aesgcm, err := cipher.NewGCM(block)
	if err != nil {
		panic(err.Error())
	}

	nonce := make([]byte, aesgcm.NonceSize())
	if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
		panic(err.Error())
	}
	return aesgcm.Seal(nonce[:aesgcm.NonceSize()], nonce, data, nil)
}

func aesgcmDecrypt(key []byte, data []byte) ([]byte, error) {
	block, err := aes.NewCipher(key)
	if err != nil {
		panic(err.Error())
	}
	aesgcm, err := cipher.NewGCM(block)
	if err != nil {
		panic(err.Error())
	}

	if len(data) < aesgcm.NonceSize() {
		return nil, errors.New("ciphertext is too short")
	}
	nonce := data[:aesgcm.NonceSize()]
	data = data[aesgcm.NonceSize():]

	return aesgcm.Open(nil, nonce, data, nil)
}
<commit_msg>MB-4114 add cipher id to encrypted value to support switch to other ciphers<commit_after>package main

import (
	"bufio"
	"bytes"
	"crypto/aes"
	"crypto/cipher"
	"crypto/rand"
	"crypto/sha1"
	"encoding/binary"
	"errors"
	"fmt"
	"golang.org/x/crypto/pbkdf2"
	"io"
	"os"
)

const keySize = 32
const nIterations = 4096

var hmacFun = sha1.New

var salt = [8]byte{20, 183, 239, 38, 44, 214, 22, 141}

type encryptionService struct {
	lockKey          []byte
	encryptedDataKey []byte
	backupDataKey    []byte
	reader           *bufio.Reader
}

func main() {
	s := &encryptionService{
		reader: bufio.NewReader(os.Stdin),
	}
	for {
		s.processCommand()
	}
}

func (s *encryptionService) readCommand() (byte, []byte) {
	var size uint16
	err := binary.Read(s.reader, binary.BigEndian, &size)
	if err != nil {
		processReadError(err)
	}
	if size < 1 {
		panic("Command is too short")
	}
	command, err := s.reader.ReadByte()
	if err != nil {
		processReadError(err)
	}
	if size == 1 {
		return command, nil
	}
	buf := make([]byte, size-1)
	n, err := s.reader.Read(buf)
	if err != nil {
		processReadError(err)
	}
	if uint16(n) != size-1 {
		panic("Error reading input")
	}
	return command, buf
}

func processReadError(err error) {
	if err == io.EOF {
		// parent died. close normally
		os.Exit(0)
	}
	panic(fmt.Sprintf("Error reading input %v", err))
}

func doReply(data []byte) {
	err := binary.Write(os.Stdout, binary.BigEndian, uint16(len(data)))
	if err != nil {
		panic(fmt.Sprintf("Error writing data %v", err))
	}
	os.Stdout.Write(data)
}

func replySuccessWithData(data []byte) {
	doReply(append([]byte{'S'}, data...))
}

func replySuccess() {
	doReply([]byte{'S'})
}

func replyError(error string) {
	doReply([]byte("E" + error))
}

func encodeKey(key []byte) []byte {
	if key == nil {
		return []byte{0}
	}
	return append([]byte{byte(len(key))}, key...)
}

func combineDataKeys(key1, key2 []byte) []byte {
	return append(encodeKey(key1), encodeKey(key2)...)
}

func (s *encryptionService) replySuccessWithDataKey() {
	replySuccessWithData(combineDataKeys(s.encryptedDataKey, s.backupDataKey))
}

func (s *encryptionService) processCommand() {
	command, data := s.readCommand()

	switch command {
	case 1:
		s.cmdSetPassword(data)
	case 2:
		s.cmdCreateDataKey()
	case 3:
		s.cmdSetDataKey(data)
	case 4:
		s.cmdGetDataKey()
	case 5:
		s.cmdEncrypt(data)
	case 6:
		s.cmdDecrypt(data)
	case 7:
		s.cmdChangePassword(data)
	case 8:
		s.cmdRotateDataKey()
	case 9:
		s.cmdClearBackupKey(data)
	default:
		panic(fmt.Sprintf("Unknown command %v", command))
	}
}

func (s *encryptionService) cmdSetPassword(data []byte) {
	s.lockKey = generateLockKey(data)
	replySuccess()
}

func (s *encryptionService) createDataKey() []byte {
	dataKey := make([]byte, keySize)
	if _, err := io.ReadFull(rand.Reader, dataKey); err != nil {
		panic(err.Error())
	}
	return encrypt(s.lockKey, dataKey)
}

func (s *encryptionService) cmdCreateDataKey() {
	if s.lockKey == nil {
		panic("Password was not set")
	}
	replySuccessWithData(combineDataKeys(s.createDataKey(), nil))
}

func readField(b []byte) ([]byte, []byte) {
	size := b[0]
	return b[1 : size+1], b[size+1:]
}

func (s *encryptionService) cmdSetDataKey(data []byte) {
	if s.lockKey == nil {
		panic("Password was not set")
	}
	encryptedDataKey, data := readField(data)
	backupDataKey, _ := readField(data)

	_, err := decrypt(s.lockKey, encryptedDataKey)
	if err != nil {
		replyError(err.Error())
		return
	}
	if len(backupDataKey) == 0 {
		s.backupDataKey = nil
	} else {
		_, err = decrypt(s.lockKey, backupDataKey)
		if err != nil {
			replyError(err.Error())
			return
		}
		s.backupDataKey = backupDataKey
	}
	s.encryptedDataKey = encryptedDataKey
	replySuccess()
}

func (s *encryptionService) cmdGetDataKey() {
	s.replySuccessWithDataKey()
}

func (s *encryptionService) cmdEncrypt(data []byte) {
	if s.lockKey == nil {
		panic("Password was not set")
	}
	dataKey, err := decrypt(s.lockKey, s.encryptedDataKey)
	if err != nil {
		replyError(err.Error())
		return
	}
	replySuccessWithData(encrypt(dataKey, data))
}

func (s *encryptionService) decryptWithKey(key []byte, data []byte) ([]byte, error) {
	if key == nil {
		return nil, errors.New("Unable to decrypt value")
	}
	dataKey, err := decrypt(s.lockKey, key)
	if err != nil {
		return nil, err
	}
	return decrypt(dataKey, data)
}

func (s *encryptionService) cmdDecrypt(data []byte) {
	if s.lockKey == nil {
		panic("Password was not set")
	}
	plaintext, err := s.decryptWithKey(s.encryptedDataKey, data)
	if err == nil {
		replySuccessWithData(plaintext)
		return
	}
	plaintext, err = s.decryptWithKey(s.backupDataKey, data)
	if err != nil {
		replyError(err.Error())
		return
	}
	replySuccessWithData(plaintext)
}

func (s *encryptionService) cmdChangePassword(data []byte) {
	if s.lockKey == nil {
		panic("Password was not set")
	}
	var backupDataKey []byte
	var err error
	if s.backupDataKey != nil {
		backupDataKey, err = decrypt(s.lockKey, s.backupDataKey)
		if err != nil {
			replyError(err.Error())
			return
		}
	}
	dataKey, err := decrypt(s.lockKey, s.encryptedDataKey)
	if err != nil {
		replyError(err.Error())
		return
	}
	s.lockKey = generateLockKey(data)
	s.encryptedDataKey = encrypt(s.lockKey, dataKey)
	if s.backupDataKey != nil {
		s.backupDataKey = encrypt(s.lockKey, backupDataKey)
	}
	s.replySuccessWithDataKey()
}

func (s *encryptionService) cmdRotateDataKey() {
	if s.lockKey == nil {
		panic("Password was not set")
	}
	if s.backupDataKey != nil {
		replyError("Data key rotation is in progress")
		return
	}
	s.backupDataKey = s.encryptedDataKey
	s.encryptedDataKey = s.createDataKey()
	s.replySuccessWithDataKey()
}

func (s *encryptionService) cmdClearBackupKey(keys []byte) {
	if !bytes.Equal(combineDataKeys(s.encryptedDataKey, s.backupDataKey), keys) {
		replyError("Key mismatch")
		return
	}
	if s.backupDataKey == nil {
		replySuccess()
		return
	}
	s.backupDataKey = nil
	s.replySuccessWithDataKey()
}

func generateLockKey(password []byte) []byte {
	return pbkdf2.Key(password, salt[:], nIterations, keySize, hmacFun)
}

func encrypt(key []byte, data []byte) []byte {
	encrypted := aesgcmEncrypt(key, data)
	return append([]byte{0}, encrypted...)
}

func decrypt(key []byte, data []byte) ([]byte, error) {
	if len(data) < 1 {
		return nil, errors.New("ciphertext is too short")
	}
	if data[0] != 0 {
		return nil, errors.New("unsupported cipher")
	}
	return aesgcmDecrypt(key, data[1:len(data)])
}

func aesgcmEncrypt(key []byte, data []byte) []byte {
	block, err := aes.NewCipher(key)
	if err != nil {
		panic(err.Error())
	}
	aesgcm, err := cipher.NewGCM(block)
	if err != nil {
		panic(err.Error())
	}

	nonce := make([]byte, aesgcm.NonceSize())
	if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
		panic(err.Error())
	}
	return aesgcm.Seal(nonce[:aesgcm.NonceSize()], nonce, data, nil)
}

func aesgcmDecrypt(key []byte, data []byte) ([]byte, error) {
	block, err := aes.NewCipher(key)
	if err != nil {
		panic(err.Error())
	}
	aesgcm, err := cipher.NewGCM(block)
	if err != nil {
		panic(err.Error())
	}

	if len(data) < aesgcm.NonceSize() {
		return nil, errors.New("ciphertext is too short")
	}
	nonce := data[:aesgcm.NonceSize()]
	data = data[aesgcm.NonceSize():]

	return aesgcm.Open(nil, nonce, data, nil)
}
<|endoftext|>


<commit_before>package crc16_test

import (
	"fmt"

	"bitbucket.org/victor-_-/crc16"
)

func ExampleChecksum() {
	c := crc16.New(crc16.Kermit)
	crc := crc16.Checksum([]byte("123456789"), c)
	fmt.Printf("CRC value is %#v\n", crc)
	// Output: CRC value is 0x2189
}

func ExampleUpdate() {
	c := crc16.New(crc16.Kermit)
	crc := crc16.Checksum([]byte("123456789"), c)

	if u := crc16.Update(crc, c, crc16.Bytes(crc, c)); u != 0 {
		fmt.Println("Data is corrupted")
	} else {
		fmt.Println("Data is correct")
	}
	// Output: Data is correct
}

func ExampleConfig_x25() {
	transmitter := crc16.New(crc16.Config{
		Poly:      0x1021,
		Init:      0xffff,
		RefIn:     true,
		RefOut:    true,
		XorOut:    0xffff,
		ByteOrder: crc16.LittleEndian,
	})

	b := []byte("123456789")
	crc := crc16.Checksum(b, transmitter)

	// Append CRC to data
	b = append(b, crc16.Bytes(crc, transmitter)...)

	receiver := crc16.New(crc16.Config{
		Poly:   0x1021,
		Init:   0xffff,
		RefIn:  true,
		RefOut: true,
	})

	if crc16.Checksum(b, receiver) != 0xf0b8 {
		fmt.Println("Data is corrupted")
	} else {
		fmt.Println("Data is correct")
	}
	// Output: Data is correct
}

func ExampleBytes() {
	// It uses little endian
	k := crc16.New(crc16.Kermit)
	kval := crc16.Checksum([]byte("123456789"), k)
	fmt.Printf("%#v %#v\n", kval, crc16.Bytes(kval, k))

	// It uses big endian
	x := crc16.New(crc16.XModem)
	xval := crc16.Checksum([]byte("123456789"), x)
	fmt.Printf("%#v %#v\n", xval, crc16.Bytes(xval, x))

	// Something not assigned byte order
	s := crc16.New(crc16.Config{Poly: 0x1021})
	sval := crc16.Checksum([]byte("123456789"), s)
	fmt.Printf("%#v %#v\n", sval, crc16.Bytes(sval, s))

	// Output:
	// 0x2189 []byte{0x89, 0x21}
	// 0x31c3 []byte{0x31, 0xc3}
	// 0x31c3 []byte(nil)
}
<commit_msg>Change import path of example code to github<commit_after>package crc16_test

import (
	"fmt"

	"github.com/kim89098/crc16"
)

func ExampleChecksum() {
	c := crc16.New(crc16.Kermit)
	crc := crc16.Checksum([]byte("123456789"), c)
	fmt.Printf("CRC value is %#v\n", crc)
	// Output: CRC value is 0x2189
}

func ExampleUpdate() {
	c := crc16.New(crc16.Kermit)
	crc := crc16.Checksum([]byte("123456789"), c)

	if u := crc16.Update(crc, c, crc16.Bytes(crc, c)); u != 0 {
		fmt.Println("Data is corrupted")
	} else {
		fmt.Println("Data is correct")
	}
	// Output: Data is correct
}

func ExampleConfig_x25() {
	transmitter := crc16.New(crc16.Config{
		Poly:      0x1021,
		Init:      0xffff,
		RefIn:     true,
		RefOut:    true,
		XorOut:    0xffff,
		ByteOrder: crc16.LittleEndian,
	})

	b := []byte("123456789")
	crc := crc16.Checksum(b, transmitter)

	// Append CRC to data
	b = append(b, crc16.Bytes(crc, transmitter)...)

	receiver := crc16.New(crc16.Config{
		Poly:   0x1021,
		Init:   0xffff,
		RefIn:  true,
		RefOut: true,
	})

	if crc16.Checksum(b, receiver) != 0xf0b8 {
		fmt.Println("Data is corrupted")
	} else {
		fmt.Println("Data is correct")
	}
	// Output: Data is correct
}

func ExampleBytes() {
	// It uses little endian
	k := crc16.New(crc16.Kermit)
	kval := crc16.Checksum([]byte("123456789"), k)
	fmt.Printf("%#v %#v\n", kval, crc16.Bytes(kval, k))

	// It uses big endian
	x := crc16.New(crc16.XModem)
	xval := crc16.Checksum([]byte("123456789"), x)
	fmt.Printf("%#v %#v\n", xval, crc16.Bytes(xval, x))

	// Something not assigned byte order
	s := crc16.New(crc16.Config{Poly: 0x1021})
	sval := crc16.Checksum([]byte("123456789"), s)
	fmt.Printf("%#v %#v\n", sval, crc16.Bytes(sval, s))

	// Output:
	// 0x2189 []byte{0x89, 0x21}
	// 0x31c3 []byte{0x31, 0xc3}
	// 0x31c3 []byte(nil)
}
<|endoftext|>


<commit_before>package vcard_test

import (
	"io"
	"log"
	"os"
	"bufio"

	"github.com/emersion/go-vcard"
)

func ExampleNewDecoder() {
	f, err := os.Open("cards.vcf")
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	dec := vcard.NewDecoder(f)
	for {
		card, err := dec.Decode()
		if err == io.EOF {
			break
		} else if err != nil {
			log.Fatal(err)
		}

		log.Println(card.PreferredValue(vcard.FieldFormattedName))
	}
}

func ExampleNewEncoder() {
	destFile, err := os.Create("new_cards.vcf")
	if err != nil {
		panic(err)
	}
	defer destFile.Close()

	data := [][]string {
		{"John", "Webber", "Maxwell", "(+1) 199 8714",},
		{"Donald", "", "Ron", "(+44) 421 8913"},
		{"Eric", "E.", "Peter", "(+37) 221 9903",},
	}

	var (
		// card is a map of strings to []*vcard.Field objects
		card vcard.Card

		// buffer the writes to save disk I/O time
		writeBuffer = bufio.NewWriter(writeTo)

		// destination where the vcard will be encoded to
		writer = vcard.NewEncoder(writeBuffer)
	)

	for _, entry := range data {
		contact := NewContact(entry)
		card[vcard.FieldFormattedName] = contact.ReadFormattedName(contactModel)
		card[vcard.FieldName] = contact.ReadName(contactModel)
		card[vcard.FieldTelephone] = contact.ReadTelephone(contactModel)
		vcard.ToV4(card)
		err := writer.Encode(card)
		if err != nil {
			panic(err)
		}

	}
}

// encoding a vcard can be done using the following method

// Contact is our model for contact data
type Contact struct {
	FirstName  string
	MiddleName string
	LastName   string
	Telephone  string
}

// ReadName formats a Contact object into a []*vcard.Field object containing the
// name of the contact with its components
func (c *contact.Contact) ReadName() []*vcard.Field {
	return []*vcard.Field{
		&vcard.Field{
			Value: c.FirstName + ";" + c.LastName + ";" + c.MiddleName,
			Params: map[string][]string{
				vcard.ParamSortAs: []string{
					c.FirstName + " " + c.LastName,
				},
			},
		},
	}
}

// ReadFormattedName formats a Contact object into a []*vcard.Field object containing
// the formatted name of the contact
func (c *contact.Contact) ReadFormattedName() []*vcard.Field {
	return []*vcard.Field{
		&vcard.Field{
			Value: c.FirstName + " " + c.LastName + " " + c.MiddleName,
		},
	}
}

// ReadTelephone formats a telephone number string into a vcard.Field object
func (c *contact.Contact) ReadTelephone() []*vcard.Field {
	return []*vcard.Field{
		&vcard.Field{
			Value: c.Telephone,
		},
	}
}

// NewContact constructs a Contact object
func NewContact(strslc [4]string) *Contact {
	fn, mn, ln, tel := strslc[0], strslc[1], strslc[2], strslc[3]
	return &Contact{
		FirstName:  fn,
		MiddleName: mn,
		LastName:   ln,
		Telephone:  tel,
	}
}

<commit_msg>Fixed bug from passing undefined arguments<commit_after>package vcard_test

import (
	"io"
	"log"
	"os"
	"bufio"

	"github.com/emersion/go-vcard"
)

func ExampleNewDecoder() {
	f, err := os.Open("cards.vcf")
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	dec := vcard.NewDecoder(f)
	for {
		card, err := dec.Decode()
		if err == io.EOF {
			break
		} else if err != nil {
			log.Fatal(err)
		}

		log.Println(card.PreferredValue(vcard.FieldFormattedName))
	}
}

func ExampleNewEncoder() {
	destFile, err := os.Create("new_cards.vcf")
	if err != nil {
		log.Fatal(err)
	}
	defer destFile.Close()

	data := [][]string {
		{"John", "Webber", "Maxwell", "(+1) 199 8714",},
		{"Donald", "", "Ron", "(+44) 421 8913"},
		{"Eric", "E.", "Peter", "(+37) 221 9903",},
	}

	var (
		// card is a map of strings to []*vcard.Field objects
		card vcard.Card

		// buffer the writes to save disk I/O time
		writeBuffer = bufio.NewWriter(destFile)

		// destination where the vcard will be encoded to
		writer = vcard.NewEncoder(writeBuffer)
	)

	for _, entry := range data {
		contact := NewContact(entry)
		card[vcard.FieldFormattedName] = contact.ReadFormattedName()
		card[vcard.FieldName] = contact.ReadName()
		card[vcard.FieldTelephone] = contact.ReadTelephone()
		vcard.ToV4(card)
		err := writer.Encode(card)
		if err != nil {
			log.Fatal(err)
		}

	}
	err = writeBuffer.Flush()
	if err != nil {
		log.Fatal(err)
	}
}

// encoding a vcard can be done using the following method

// Contact is our model for contact data
type Contact struct {
	FirstName  string
	MiddleName string
	LastName   string
	Telephone  string
}

// ReadName formats a Contact object into a []*vcard.Field object containing the
// name of the contact with its components
func (c *contact.Contact) ReadName() []*vcard.Field {
	return []*vcard.Field{
		&vcard.Field{
			Value: c.FirstName + ";" + c.LastName + ";" + c.MiddleName,
			Params: map[string][]string{
				vcard.ParamSortAs: []string{
					c.FirstName + " " + c.LastName,
				},
			},
		},
	}
}

// ReadFormattedName formats a Contact object into a []*vcard.Field object containing
// the formatted name of the contact
func (c *contact.Contact) ReadFormattedName() []*vcard.Field {
	return []*vcard.Field{
		&vcard.Field{
			Value: c.FirstName + " " + c.LastName + " " + c.MiddleName,
		},
	}
}

// ReadTelephone formats a telephone number string into a vcard.Field object
func (c *contact.Contact) ReadTelephone() []*vcard.Field {
	return []*vcard.Field{
		&vcard.Field{
			Value: c.Telephone,
		},
	}
}

// NewContact constructs a Contact object
func NewContact(strslc [4]string) *Contact {
	fn, mn, ln, tel := strslc[0], strslc[1], strslc[2], strslc[3]
	return &Contact{
		FirstName:  fn,
		MiddleName: mn,
		LastName:   ln,
		Telephone:  tel,
	}
}

<|endoftext|>


<commit_before>package netlink_test

import (
	"log"

	"github.com/mdlayher/netlink"
)

// This example demonstrates using a netlink.Conn to execute requests against
// netlink.
func ExampleConn_execute() {
	// Speak to generic netlink using netlink
	const familyGeneric = 16

	c, err := netlink.Dial(familyGeneric, nil)
	if err != nil {
		log.Fatalf("failed to dial netlink: %v", err)
	}
	defer c.Close()

	// Ask netlink to send us an acknowledgement, which will contain
	// a copy of the header we sent to it
	req := netlink.Message{
		Header: netlink.Header{
			// Package netlink will automatically set header fields
			// which are set to zero
			Flags: netlink.HeaderFlagsRequest | netlink.HeaderFlagsAcknowledge,
		},
	}

	// Perform a request, receive replies, and validate the replies
	msgs, err := c.Execute(req)
	if err != nil {
		log.Fatalf("failed to execute request: %v", err)
	}

	if c := len(msgs); c != 1 {
		log.Fatalf("expected 1 message, but got: %d", c)
	}

	// Decode the copied request header, starting after 4 bytes
	// indicating "success"
	var res netlink.Message
	if err := (&res).UnmarshalBinary(msgs[0].Data[4:]); err != nil {
		log.Fatalf("failed to unmarshal response: %v", err)
	}

	log.Printf("res: %+v", res)
}

// This example demonstrates using a netlink.Conn to listen for multicast group
// messages generated by the addition and deletion of network interfaces.
func ExampleConn_listenMulticast() {
	const (
		// Speak to route netlink using netlink
		familyRoute = 0

		// Listen for events triggered by addition or deletion of
		// network interfaces
		rtmGroupLink = 0x1
	)

	c, err := netlink.Dial(familyRoute, &netlink.Config{
		// Groups is a bitmask; more than one group can be specified
		// by OR'ing multiple group values together
		Groups: rtmGroupLink,
	})
	if err != nil {
		log.Fatalf("failed to dial netlink: %v", err)
	}
	defer c.Close()

	for {
		// Listen for netlink messages triggered by multicast groups
		msgs, err := c.Receive()
		if err != nil {
			log.Fatalf("failed to receive messages: %v", err)
		}

		log.Printf("msgs: %+v", msgs)
	}
}
<commit_msg>netlink: add AttributeDecoder example<commit_after>package netlink_test

import (
	"fmt"
	"log"

	"github.com/mdlayher/netlink/nlenc"
	"github.com/mdlayher/netlink/nltest"

	"github.com/mdlayher/netlink"
)

// This example demonstrates using a netlink.Conn to execute requests against
// netlink.
func ExampleConn_execute() {
	// Speak to generic netlink using netlink
	const familyGeneric = 16

	c, err := netlink.Dial(familyGeneric, nil)
	if err != nil {
		log.Fatalf("failed to dial netlink: %v", err)
	}
	defer c.Close()

	// Ask netlink to send us an acknowledgement, which will contain
	// a copy of the header we sent to it
	req := netlink.Message{
		Header: netlink.Header{
			// Package netlink will automatically set header fields
			// which are set to zero
			Flags: netlink.HeaderFlagsRequest | netlink.HeaderFlagsAcknowledge,
		},
	}

	// Perform a request, receive replies, and validate the replies
	msgs, err := c.Execute(req)
	if err != nil {
		log.Fatalf("failed to execute request: %v", err)
	}

	if c := len(msgs); c != 1 {
		log.Fatalf("expected 1 message, but got: %d", c)
	}

	// Decode the copied request header, starting after 4 bytes
	// indicating "success"
	var res netlink.Message
	if err := (&res).UnmarshalBinary(msgs[0].Data[4:]); err != nil {
		log.Fatalf("failed to unmarshal response: %v", err)
	}

	log.Printf("res: %+v", res)
}

// This example demonstrates using a netlink.Conn to listen for multicast group
// messages generated by the addition and deletion of network interfaces.
func ExampleConn_listenMulticast() {
	const (
		// Speak to route netlink using netlink
		familyRoute = 0

		// Listen for events triggered by addition or deletion of
		// network interfaces
		rtmGroupLink = 0x1
	)

	c, err := netlink.Dial(familyRoute, &netlink.Config{
		// Groups is a bitmask; more than one group can be specified
		// by OR'ing multiple group values together
		Groups: rtmGroupLink,
	})
	if err != nil {
		log.Fatalf("failed to dial netlink: %v", err)
	}
	defer c.Close()

	for {
		// Listen for netlink messages triggered by multicast groups
		msgs, err := c.Receive()
		if err != nil {
			log.Fatalf("failed to receive messages: %v", err)
		}

		log.Printf("msgs: %+v", msgs)
	}
}

// This example demonstrates using a netlink.AttributeDecoder to decode packed
// netlink attributes in a message payload.
func ExampleAttributeDecoder_decode() {
	// Create a netlink.AttributeDecoder using some example attribute bytes
	// that are prepared for this example.
	ad, err := netlink.NewAttributeDecoder(exampleAttributes())
	if err != nil {
		log.Fatalf("failed to create attribute decoder: %v", err)
	}

	// nested is a nested structure within out.
	type nested struct {
		A, B uint32
	}

	// out is an example structure we will use to unpack netlink attributes.
	var out struct {
		Number uint16
		String string
		Nested nested
	}

	// parseNested is an example function used to adapt the ad.Do method to
	// parse an arbitrary structure.
	//
	// It is recommended to create functions such as this one at the package
	// level, but for this example, it is necessary to create a closure to
	// fully show usage of the netlink.AttributeDecoder.
	parseNested := func(n *nested) func(b []byte) error {
		return func(b []byte) error {
			// Create an internal netlink.AttributeDecoder that operates on a
			// nested set of attributes passed by the external decoder.
			ad, err := netlink.NewAttributeDecoder(b)
			if err != nil {
				log.Fatalf("failed to create nested attribute decoder: %v", err)
			}

			// Iterate attributes until completion, checking the type of each
			// and decoding them as appropriate.
			for ad.Next() {
				switch ad.Type() {
				// A and B are both uint32 values, so decode them as such.
				case 1:
					n.A = ad.Uint32()
				case 2:
					n.B = ad.Uint32()
				}
			}

			// Return any error encountered while decoding.
			return ad.Err()
		}
	}

	// Iterate attributes until completion, checking the type of each and
	// decoding them as appropriate.
	for ad.Next() {
		// Check the type of the current attribute with ad.Type.  Typically you
		// will find netlink attribute types and data values in C headers as
		// constants.
		switch ad.Type() {
		case 1:
			// Number is a uint16, so decode it as such.
			out.Number = ad.Uint16()
		case 2:
			// String is a string, so decode it as such.
			out.String = ad.String()
		case 3:
			// Nested is a nested structure, so we will use our parseNested
			// function along with ad.Do to decoded it in a concise way.
			ad.Do(parseNested(&out.Nested))
		}
	}

	// Any errors encountered during decoding (including any errors from decoding
	// the nested attributes) will be returned here.
	if err := ad.Err(); err != nil {
		log.Fatalf("failed to decode attributes: %v", err)
	}

	fmt.Printf(`Number: %d
String: %q
Nested:
   - A: %d
   - B: %d`,
		out.Number, out.String, out.Nested.A, out.Nested.B,
	)
	// Output:
	// Number: 1
	// String: "hello world"
	// Nested:
	//    - A: 2
	//    - B: 3
}

func exampleAttributes() []byte {
	return nltest.MustMarshalAttributes([]netlink.Attribute{
		{
			Type: 1,
			Data: nlenc.Uint16Bytes(1),
		},
		{
			Type: 2,
			Data: nlenc.Bytes("hello world"),
		},
		{
			Type: 3,
			Data: nltest.MustMarshalAttributes([]netlink.Attribute{
				{
					Type: 1,
					Data: nlenc.Uint32Bytes(2),
				},
				{
					Type: 2,
					Data: nlenc.Uint32Bytes(3),
				},
			}),
		},
	})
}
<|endoftext|>


<commit_before>package reflection_test

func ExampleGetField() {
    s := MyStruct {
        FirstField: "first value",
        SecondField: 2,
        ThirdField: "third value",
    }

    fieldsToExtract := []string{"FirstField", "ThirdField"}

    for _, fieldName := range fieldsToExtract {
        value, err := reflections.GetField(s, fieldName)
        fmt.Println(value)
    }
}<commit_msg>Fix: missing example imports<commit_after>package reflections_test

import (
    "fmt"
    "github.com/oleiade/reflections"
)

func ExampleGetField() {
    s := MyStruct {
        FirstField: "first value",
        SecondField: 2,
        ThirdField: "third value",
    }

    fieldsToExtract := []string{"FirstField", "ThirdField"}

    for _, fieldName := range fieldsToExtract {
        value, err := reflections.GetField(s, fieldName)
        fmt.Println(value)
    }
}<|endoftext|>


<commit_before>package httpcontrol_test

import (
	"fmt"
	"io"
	"net/http"
	"os"
	"time"

	"github.com/daaku/go.httpcontrol"
)

func Example() {
	// This example doesn't use all available features, look at the API for
	// documentation on all options.
	transport := &httpcontrol.Transport{
		RequestTimeout: time.Minute,
		MaxTries:       3,
	}

	// It can then be used as the Transport in a HTTP client (or used directly
	// via RoundTrip).
	client := &http.Client{Transport: transport}

	res, err := client.Get("http://graph.facebook.com/naitik?fields=name")
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	defer res.Body.Close()

	// Just outputting the response for example purposes.
	if _, err := io.Copy(os.Stdout, res.Body); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	// This isn't strictly necessary, but it will shutdown the background
	// goroutine monitoring for request timeouts.
	if err := transport.Close(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	// Output: {"name":"Naitik Shah","id":"5526183"}
}
<commit_msg>remove Close from example since it isn't strictly necessary<commit_after>package httpcontrol_test

import (
	"fmt"
	"io"
	"net/http"
	"os"
	"time"

	"github.com/daaku/go.httpcontrol"
)

func Example() {
	// The provided Transport can be used via the HTTP client or used directly
	// via RoundTrip.
	client := &http.Client{
		Transport: &httpcontrol.Transport{
			RequestTimeout: time.Minute,
			MaxTries:       3,
		},
	}

	res, err := client.Get("http://graph.facebook.com/naitik?fields=name")
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	defer res.Body.Close()

	// Just outputting the response for example purposes.
	if _, err := io.Copy(os.Stdout, res.Body); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	// Output: {"name":"Naitik Shah","id":"5526183"}
}
<|endoftext|>


<commit_before>package skiplist_test

import (
	"fmt"

	. "github.com/kkdai/skiplist"
)

func ExampleNewSkipList() {
	//New a skiplist
	sl := NewSkipList()
}

func ExampleSkiplist_Insert() {
	//New a skiplist
	sl := NewSkipList()

	//Insert search key 50, value "5", value could be anything.
	sl.Insert(50, "5")
}

func ExampleSkiplist_Search() {
	//New a skiplist
	sl := NewSkipList()

	//Insert search key 50, value "5", value could be anything.
	sl.Insert(50, "5")
	sl.Insert(40, "4")
	sl.Insert(70, "7")
	sl.Insert(100, "10")

	//Search key, which time complexity O(log n)
	ret, err := sl.Search(50)
	if err == nil {
		fmt.Println("key 50: val->", ret)
	} else {
		fmt.Println("Not found, ", err)
	}
}

func ExampleSkiplist_Delete() {
	//New a skiplist
	sl := NewSkipList()

	//Insert search key 50, value "5", value could be anything.
	sl.Insert(70, "7")

	//Delete by search key
	err := sl.Delete(70)
	if err != nil {
		fmt.Println("Delete not found")
	}
}
<commit_msg>Fixed test build issue<commit_after>package skiplist_test

import (
	"fmt"

	. "github.com/kkdai/skiplist"
)

func ExampleNewSkipList() {
	//New a skiplist
	sl := NewSkipList()
	sl.DisplayAll()
}

func ExampleSkiplist_Insert() {
	//New a skiplist
	sl := NewSkipList()

	//Insert search key 50, value "5", value could be anything.
	sl.Insert(50, "5")
}

func ExampleSkiplist_Search() {
	//New a skiplist
	sl := NewSkipList()

	//Insert search key 50, value "5", value could be anything.
	sl.Insert(50, "5")
	sl.Insert(40, "4")
	sl.Insert(70, "7")
	sl.Insert(100, "10")

	//Search key, which time complexity O(log n)
	ret, err := sl.Search(50)
	if err == nil {
		fmt.Println("key 50: val->", ret)
	} else {
		fmt.Println("Not found, ", err)
	}
}

func ExampleSkiplist_Delete() {
	//New a skiplist
	sl := NewSkipList()

	//Insert search key 50, value "5", value could be anything.
	sl.Insert(70, "7")

	//Delete by search key
	err := sl.Delete(70)
	if err != nil {
		fmt.Println("Delete not found")
	}
}
<|endoftext|>


<commit_before>package goini

import (
	"fmt"
	"strconv"
	"strings"
)

func Example_section() {
	cp := NewRawConfigParser()
	cp.Parse(strings.NewReader(`
[section]
message=hello
	`))
	if config, err := cp.Finish(); err == nil {
		section := config.Sections()["section"]
		message := strings.Join(section.GetPropertyValues("message"), " ")
		fmt.Println(message)
	}
	// Output: hello
}

func Example_globalSection() {
	cp := NewRawConfigParser()
	cp.Parse(strings.NewReader(`
message=hello
message=world
	`))
	if config, err := cp.Finish(); err == nil {
		message := strings.Join(config.GlobalSection.GetPropertyValues("message"), " ")
		fmt.Println(message)
	}
	// Output: hello world
}

func Example_continuation() {
	cp := NewRawConfigParser()
	cp.Parse(strings.NewReader(`
message=hello \
world
	`))
	if config, err := cp.Finish(); err == nil {
		message := strings.Join(config.GlobalSection.GetPropertyValues("message"), " ")
		fmt.Println(message)
	}
	// Output: hello world
}

func Example_comment() {
	cp := NewRawConfigParser()
	cp.Parse(strings.NewReader(`
message=hello
; message=world
	`))
	if config, err := cp.Finish(); err == nil {
		messageParts := config.GlobalSection.GetPropertyValues("message")
		fmt.Println(strings.Join(messageParts, " "))
	}
	// Output: hello
}

func ExampleRawSection_GetPropertyNumber() {
	cp := NewRawConfigParser()
	cp.Parse(strings.NewReader(`
number=1
message=hello
message=world
	`))
	if config, err := cp.Finish(); err == nil {
		number, _ := config.GlobalSection.GetPropertyNumber("number")
		if i, e := number.Int64(); e == nil {
			fmt.Println(i)
		}
		message, _ := config.GlobalSection.GetPropertyNumber("message")
		fmt.Println(message)
	}
	// Output: 1
	// hello world
}

func ExampleDecodeOptionSet_Decode() {
	rawSection := RawSection{
		"key": []string{"1"},
	}

	spec := DecodeOptionSet{
		"key": &DecodeOption{UniqueOption,
			"A help message.",
			func(d interface{}, v string) error {
				if i, e := strconv.Atoi(v); e != nil {
					return e
				} else {
					*d.(*int) = i
					return nil
				}
			},
		},
	}

	var key int
	spec.Decode(&key, rawSection)
	fmt.Println(key)

	//Output: 1
}
<commit_msg>field name in composite literal<commit_after>package goini

import (
	"fmt"
	"strconv"
	"strings"
)

func Example_section() {
	cp := NewRawConfigParser()
	cp.Parse(strings.NewReader(`
[section]
message=hello
	`))
	if config, err := cp.Finish(); err == nil {
		section := config.Sections()["section"]
		message := strings.Join(section.GetPropertyValues("message"), " ")
		fmt.Println(message)
	}
	// Output: hello
}

func Example_globalSection() {
	cp := NewRawConfigParser()
	cp.Parse(strings.NewReader(`
message=hello
message=world
	`))
	if config, err := cp.Finish(); err == nil {
		message := strings.Join(config.GlobalSection.GetPropertyValues("message"), " ")
		fmt.Println(message)
	}
	// Output: hello world
}

func Example_continuation() {
	cp := NewRawConfigParser()
	cp.Parse(strings.NewReader(`
message=hello \
world
	`))
	if config, err := cp.Finish(); err == nil {
		message := strings.Join(config.GlobalSection.GetPropertyValues("message"), " ")
		fmt.Println(message)
	}
	// Output: hello world
}

func Example_comment() {
	cp := NewRawConfigParser()
	cp.Parse(strings.NewReader(`
message=hello
; message=world
	`))
	if config, err := cp.Finish(); err == nil {
		messageParts := config.GlobalSection.GetPropertyValues("message")
		fmt.Println(strings.Join(messageParts, " "))
	}
	// Output: hello
}

func ExampleRawSection_GetPropertyNumber() {
	cp := NewRawConfigParser()
	cp.Parse(strings.NewReader(`
number=1
message=hello
message=world
	`))
	if config, err := cp.Finish(); err == nil {
		number, _ := config.GlobalSection.GetPropertyNumber("number")
		if i, e := number.Int64(); e == nil {
			fmt.Println(i)
		}
		message, _ := config.GlobalSection.GetPropertyNumber("message")
		fmt.Println(message)
	}
	// Output: 1
	// hello world
}

func ExampleDecodeOptionSet_Decode() {
	rawSection := RawSection{
		"key": []string{"1"},
	}

	spec := DecodeOptionSet{
		"key": &DecodeOption{Kind: UniqueOption,
			Usage: "A help message.",
			Parse: func(d interface{}, v string) error {
				if i, e := strconv.Atoi(v); e != nil {
					return e
				} else {
					*d.(*int) = i
					return nil
				}
			},
		},
	}

	var key int
	spec.Decode(&key, rawSection)
	fmt.Println(key)

	//Output: 1
}
<|endoftext|>


<commit_before>// Copyright 2017 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.

// Sample application to demonstrate the log package.

package log_test

import (
	"context"
	"os"

	"zombiezen.com/go/log"
)

func Example() {
	// Initialize the global logger.
	// This should only happen in main and before any log statements.
	stderrLog := log.New(os.Stdout, "", 0, nil)
	log.SetDefault(stderrLog)

	// Once the logger is set, you can log from anywhere.
	ctx := context.Background()
	log.Infof(ctx, "Hello, World!")

	// Output:
	// Hello, World!
}
<commit_msg>Correct variable names in example<commit_after>// Copyright 2017 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.

// Sample application to demonstrate the log package.

package log_test

import (
	"context"
	"os"

	"zombiezen.com/go/log"
)

func Example() {
	// Initialize the global logger.
	// This should only happen in main and before any log statements.
	stdoutLog := log.New(os.Stdout, "", 0, nil)
	log.SetDefault(stdoutLog)

	// Once the logger is set, you can log from anywhere.
	ctx := context.Background()
	log.Infof(ctx, "Hello, World!")

	// Output:
	// Hello, World!
}
<|endoftext|>


<commit_before>package main

import (
	"dns"
	"flag"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func q(w dns.RequestWriter, m *dns.Msg) {
	w.Send(m)
	r, err := w.Receive()
	if err != nil {
		fmt.Printf("%s\n", err.Error())
	}
	w.Write(r)
}

func main() {
	dnssec := flag.Bool("dnssec", false, "request DNSSEC records")
	query := flag.Bool("question", false, "show question")
	short := flag.Bool("short", false, "abbreviate long DNSKEY and RRSIG RRs")
	check := flag.Bool("check", false, "check internal DNSSEC consistency")
	port := flag.Int("port", 53, "port number to use")
	aa := flag.Bool("aa", false, "set AA flag in query")
	ad := flag.Bool("ad", false, "set AD flag in query")
	cd := flag.Bool("cd", false, "set CD flag in query")
	rd := flag.Bool("rd", true, "unset RD flag in query")
	tcp := flag.Bool("tcp", false, "TCP mode")
	nsid := flag.Bool("nsid", false, "ask for NSID")
	fp := flag.Bool("fp", false, "enable server detection")
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage: %s [@server] [qtype] [qclass] [name ...]\n", os.Args[0])
		flag.PrintDefaults()
	}

	conf, _ := dns.ClientConfigFromFile("/etc/resolv.conf")
	nameserver := "@" + conf.Servers[0]
	qtype := uint16(0)
	qclass := uint16(dns.ClassINET) // Default qclass
	var qname []string

	flag.Parse()

Flags:
	for i := 0; i < flag.NArg(); i++ {
		// If it starts with @ it is a nameserver
		if flag.Arg(i)[0] == '@' {
			nameserver = flag.Arg(i)
			continue Flags
		}
		// First class, then type, to make ANY queries possible
		// And if it looks like type, it is a type
		if k, ok := dns.Str_rr[strings.ToUpper(flag.Arg(i))]; ok {
			qtype = k
			continue Flags
		}
		// If it looks like a class, it is a class
		if k, ok := dns.Str_class[strings.ToUpper(flag.Arg(i))]; ok {
			qclass = k
			continue Flags
		}
		// If it starts with TYPExxx it is unknown rr
		if strings.HasPrefix(flag.Arg(i), "TYPE") {
			i, e := strconv.Atoi(string([]byte(flag.Arg(i))[4:]))
			if e == nil {
				qtype = uint16(i)
				continue Flags
			}
		}

		// Anything else is a qname
		qname = append(qname, flag.Arg(i))
	}
	if len(qname) == 0 {
		qname = make([]string, 1)
		qname[0] = "."
		qtype = dns.TypeNS
	}
	if qtype == 0 {
		qtype = dns.TypeA
	}

	nameserver = string([]byte(nameserver)[1:]) // chop off @
	nameserver += ":" + strconv.Itoa(*port)

	// ipv6 todo
	// We use the async query handling, just to show how
	// it is to be used.
	dns.HandleQueryFunc(".", q)
	dns.ListenAndQuery(nil, nil)
	c := dns.NewClient()
	if *tcp {
		c.Net = "tcp"
	}

	m := new(dns.Msg)
	m.MsgHdr.Authoritative = *aa
	m.MsgHdr.AuthenticatedData = *ad
	m.MsgHdr.CheckingDisabled = *cd
	m.MsgHdr.RecursionDesired = *rd
	m.Question = make([]dns.Question, 1)
	if *dnssec || *nsid {
		o := new(dns.RR_OPT)
		o.Hdr.Name = "."
		o.Hdr.Rrtype = dns.TypeOPT
		if *dnssec {
			o.SetDo()
			o.SetUDPSize(dns.DefaultMsgSize)
		}
		if *nsid {
			o.SetNsid("")
		}
		m.Extra = append(m.Extra, o)
	}

	if *fp {
		startParse(nameserver)
		return
	}
	for _, v := range qname {
		m.Question[0] = dns.Question{v, qtype, qclass}
		m.Id = dns.Id()
		if *query {
			fmt.Printf("%s\n", msgToFingerprint(m))
			fmt.Printf("%s\n", m.String())
		}
		c.Do(m, nameserver)
	}

	i := 0
forever:
	for {
		select {
		case r := <-dns.DefaultReplyChan:
			if r.Reply != nil {
				if r.Reply.Rcode == dns.RcodeSuccess {
					if r.Request.Id != r.Reply.Id {
						fmt.Printf("Id mismatch\n")
					}
				}
				if *check {
					sigCheck(r.Reply, nameserver)
					nsecCheck(r.Reply)
				}
				if *short {
					r.Reply = shortMsg(r.Reply)
				}

				fmt.Printf("%v", r.Reply)
			}
			i++
			if i == len(qname) {
				break forever
			}
		}
	}
}

func sectionCheck(set []dns.RR, server string) {
	for _, rr := range set {
		if rr.Header().Rrtype == dns.TypeRRSIG {
			rrset := getRRset(set, rr.Header().Name, rr.(*dns.RR_RRSIG).TypeCovered)
			key := getKey(rr.(*dns.RR_RRSIG).SignerName, rr.(*dns.RR_RRSIG).KeyTag, server)
			if key == nil {
				fmt.Printf(";? DNSKEY %s/%d not found\n", rr.(*dns.RR_RRSIG).SignerName, rr.(*dns.RR_RRSIG).KeyTag)
                                continue
			}
			if err := rr.(*dns.RR_RRSIG).Verify(key, rrset); err != nil {
				fmt.Printf(";- Bogus signature,  %s does not validate (DNSKEY %s/%d)\n", shortSig(rr.(*dns.RR_RRSIG)), key.Header().Name, key.KeyTag())
			} else {
				fmt.Printf(";+ Secure signature, %s validates (DNSKEY %s/%d)\n", shortSig(rr.(*dns.RR_RRSIG)), key.Header().Name, key.KeyTag())
			}
		}
	}
}

// Check if we have nsec3 records and if so, check them
func nsecCheck(in *dns.Msg) {
        for _, r := range in.Answer {
                if r.Header().Rrtype == dns.TypeNSEC3 {
                        goto Check
                }
        }
        for _, r := range in.Ns {
                if r.Header().Rrtype == dns.TypeNSEC3 {
                        goto Check
                }
        }
        for _, r := range in.Extra {
                if r.Header().Rrtype == dns.TypeNSEC3 {
                        goto Check
                }
        }
        return
Check:
        w, err := in.Nsec3Verify(in.Question[0])
        switch w {
        case dns.NSEC3_NXDOMAIN:
                fmt.Printf(";+ Correct denial of existence (NSEC3/NXDOMAIN)\n")
        case dns.NSEC3_NODATA:
                fmt.Printf(";+ Correct denial of existence (NSEC3/NODATA)\n")
        default:
                // w == 0
                if err != nil {
	                fmt.Printf(";- Incorrect denial of existence (NSEC3): %s\n",err.Error())
                }
        }
}

// Check the sigs in the msg, get the signer's key (additional query), get the
// rrset from the message, check the signature(s)
func sigCheck(in *dns.Msg, server string) {
        sectionCheck(in.Answer, server)
        sectionCheck(in.Ns, server)
        sectionCheck(in.Extra, server)
}

// Return the RRset belonging to the signature with name and type t
func getRRset(l []dns.RR, name string, t uint16) []dns.RR {
	l1 := make([]dns.RR, 0)
	for _, rr := range l {
		if rr.Header().Name == name && rr.Header().Rrtype == t {
			l1 = append(l1, rr)
		}
	}
	return l1
}

// Get the key from the DNS (uses the local resolver) and return them.
// If nothing is found we return nil
func getKey(name string, keytag uint16, server string) *dns.RR_DNSKEY {
	c := dns.NewClient()
	m := new(dns.Msg)
	m.SetQuestion(name, dns.TypeDNSKEY)
	r, err := c.Exchange(m, server)
	if err != nil {
		return nil
	}
	for _, k := range r.Answer {
		if k1, ok := k.(*dns.RR_DNSKEY); ok {
			if k1.KeyTag() == keytag {
				return k1
			}
		}
	}
	return nil
}

// shorten RRSIG to "miek.nl RRSIG(NS)"
func shortSig(sig *dns.RR_RRSIG) string {
	return sig.Header().Name + " RRSIG(" + dns.Rr_str[sig.TypeCovered] + ")"
}

// Walk trough message and short Key data and Sig data
func shortMsg(in *dns.Msg) *dns.Msg {
	for i := 0; i < len(in.Answer); i++ {
		in.Answer[i] = shortRR(in.Answer[i])
	}
	for i := 0; i < len(in.Ns); i++ {
		in.Ns[i] = shortRR(in.Ns[i])
	}
	for i := 0; i < len(in.Extra); i++ {
		in.Extra[i] = shortRR(in.Extra[i])
	}
	return in
}

func shortRR(r dns.RR) dns.RR {
	switch t := r.(type) {
	case *dns.RR_DS:
		t.Digest = "..."
	case *dns.RR_DNSKEY:
		t.PublicKey = "..."
	case *dns.RR_RRSIG:
		t.Signature = "..."
	case *dns.RR_NSEC3:
		t.Salt = "-" // Nobody cares
		if len(t.TypeBitMap) > 5 {
			t.TypeBitMap = t.TypeBitMap[1:5]
		}
	}
	return r
}
<commit_msg>if the query uses tcp, use it also to get the keys<commit_after>package main

import (
	"dns"
	"flag"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func q(w dns.RequestWriter, m *dns.Msg) {
	w.Send(m)
	r, err := w.Receive()
	if err != nil {
		fmt.Printf("%s\n", err.Error())
	}
	w.Write(r)
}

func main() {
	dnssec := flag.Bool("dnssec", false, "request DNSSEC records")
	query := flag.Bool("question", false, "show question")
	short := flag.Bool("short", false, "abbreviate long DNSKEY and RRSIG RRs")
	check := flag.Bool("check", false, "check internal DNSSEC consistency")
	port := flag.Int("port", 53, "port number to use")
	aa := flag.Bool("aa", false, "set AA flag in query")
	ad := flag.Bool("ad", false, "set AD flag in query")
	cd := flag.Bool("cd", false, "set CD flag in query")
	rd := flag.Bool("rd", true, "unset RD flag in query")
	tcp := flag.Bool("tcp", false, "TCP mode")
	nsid := flag.Bool("nsid", false, "ask for NSID")
	fp := flag.Bool("fp", false, "enable server detection")
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage: %s [@server] [qtype] [qclass] [name ...]\n", os.Args[0])
		flag.PrintDefaults()
	}

	conf, _ := dns.ClientConfigFromFile("/etc/resolv.conf")
	nameserver := "@" + conf.Servers[0]
	qtype := uint16(0)
	qclass := uint16(dns.ClassINET) // Default qclass
	var qname []string

	flag.Parse()

Flags:
	for i := 0; i < flag.NArg(); i++ {
		// If it starts with @ it is a nameserver
		if flag.Arg(i)[0] == '@' {
			nameserver = flag.Arg(i)
			continue Flags
		}
		// First class, then type, to make ANY queries possible
		// And if it looks like type, it is a type
		if k, ok := dns.Str_rr[strings.ToUpper(flag.Arg(i))]; ok {
			qtype = k
			continue Flags
		}
		// If it looks like a class, it is a class
		if k, ok := dns.Str_class[strings.ToUpper(flag.Arg(i))]; ok {
			qclass = k
			continue Flags
		}
		// If it starts with TYPExxx it is unknown rr
		if strings.HasPrefix(flag.Arg(i), "TYPE") {
			i, e := strconv.Atoi(string([]byte(flag.Arg(i))[4:]))
			if e == nil {
				qtype = uint16(i)
				continue Flags
			}
		}

		// Anything else is a qname
		qname = append(qname, flag.Arg(i))
	}
	if len(qname) == 0 {
		qname = make([]string, 1)
		qname[0] = "."
		qtype = dns.TypeNS
	}
	if qtype == 0 {
		qtype = dns.TypeA
	}

	nameserver = string([]byte(nameserver)[1:]) // chop off @
	nameserver += ":" + strconv.Itoa(*port)

	// ipv6 todo
	// We use the async query handling, just to show how
	// it is to be used.
	dns.HandleQueryFunc(".", q)
	dns.ListenAndQuery(nil, nil)
	c := dns.NewClient()
	if *tcp {
		c.Net = "tcp"
	}

	m := new(dns.Msg)
	m.MsgHdr.Authoritative = *aa
	m.MsgHdr.AuthenticatedData = *ad
	m.MsgHdr.CheckingDisabled = *cd
	m.MsgHdr.RecursionDesired = *rd
	m.Question = make([]dns.Question, 1)
	if *dnssec || *nsid {
		o := new(dns.RR_OPT)
		o.Hdr.Name = "."
		o.Hdr.Rrtype = dns.TypeOPT
		if *dnssec {
			o.SetDo()
			o.SetUDPSize(dns.DefaultMsgSize)
		}
		if *nsid {
			o.SetNsid("")
		}
		m.Extra = append(m.Extra, o)
	}

	if *fp {
		startParse(nameserver)
		return
	}
	for _, v := range qname {
		m.Question[0] = dns.Question{v, qtype, qclass}
		m.Id = dns.Id()
		if *query {
			fmt.Printf("%s\n", msgToFingerprint(m))
			fmt.Printf("%s\n", m.String())
		}
		c.Do(m, nameserver)
	}

	i := 0
forever:
	for {
		select {
		case r := <-dns.DefaultReplyChan:
			if r.Reply != nil {
				if r.Reply.Rcode == dns.RcodeSuccess {
					if r.Request.Id != r.Reply.Id {
						fmt.Printf("Id mismatch\n")
					}
				}
				if *check {
					sigCheck(r.Reply, nameserver, *tcp)
					nsecCheck(r.Reply)
				}
				if *short {
					r.Reply = shortMsg(r.Reply)
				}

				fmt.Printf("%v", r.Reply)
			}
			i++
			if i == len(qname) {
				break forever
			}
		}
	}
}

func sectionCheck(set []dns.RR, server string, tcp bool) {
	for _, rr := range set {
		if rr.Header().Rrtype == dns.TypeRRSIG {
			rrset := getRRset(set, rr.Header().Name, rr.(*dns.RR_RRSIG).TypeCovered)
			key := getKey(rr.(*dns.RR_RRSIG).SignerName, rr.(*dns.RR_RRSIG).KeyTag, server, tcp)
			if key == nil {
				fmt.Printf(";? DNSKEY %s/%d not found\n", rr.(*dns.RR_RRSIG).SignerName, rr.(*dns.RR_RRSIG).KeyTag)
                                continue
			}
			if err := rr.(*dns.RR_RRSIG).Verify(key, rrset); err != nil {
				fmt.Printf(";- Bogus signature,  %s does not validate (DNSKEY %s/%d)\n", shortSig(rr.(*dns.RR_RRSIG)), key.Header().Name, key.KeyTag())
			} else {
				fmt.Printf(";+ Secure signature, %s validates (DNSKEY %s/%d)\n", shortSig(rr.(*dns.RR_RRSIG)), key.Header().Name, key.KeyTag())
			}
		}
	}
}

// Check if we have nsec3 records and if so, check them
func nsecCheck(in *dns.Msg) {
        for _, r := range in.Answer {
                if r.Header().Rrtype == dns.TypeNSEC3 {
                        goto Check
                }
        }
        for _, r := range in.Ns {
                if r.Header().Rrtype == dns.TypeNSEC3 {
                        goto Check
                }
        }
        for _, r := range in.Extra {
                if r.Header().Rrtype == dns.TypeNSEC3 {
                        goto Check
                }
        }
        return
Check:
        w, err := in.Nsec3Verify(in.Question[0])
        switch w {
        case dns.NSEC3_NXDOMAIN:
                fmt.Printf(";+ Correct denial of existence (NSEC3/NXDOMAIN)\n")
        case dns.NSEC3_NODATA:
                fmt.Printf(";+ Correct denial of existence (NSEC3/NODATA)\n")
        default:
                // w == 0
                if err != nil {
	                fmt.Printf(";- Incorrect denial of existence (NSEC3): %s\n",err.Error())
                }
        }
}

// Check the sigs in the msg, get the signer's key (additional query), get the
// rrset from the message, check the signature(s)
func sigCheck(in *dns.Msg, server string, tcp bool) {
        sectionCheck(in.Answer, server, tcp)
        sectionCheck(in.Ns, server, tcp)
        sectionCheck(in.Extra, server, tcp)
}

// Return the RRset belonging to the signature with name and type t
func getRRset(l []dns.RR, name string, t uint16) []dns.RR {
	l1 := make([]dns.RR, 0)
	for _, rr := range l {
		if rr.Header().Name == name && rr.Header().Rrtype == t {
			l1 = append(l1, rr)
		}
	}
	return l1
}

// Get the key from the DNS (uses the local resolver) and return them.
// If nothing is found we return nil
func getKey(name string, keytag uint16, server string, tcp bool) *dns.RR_DNSKEY {
	c := dns.NewClient()
        if tcp {
                c.Net = "tcp"
        }
	m := new(dns.Msg)
	m.SetQuestion(name, dns.TypeDNSKEY)
	r, err := c.Exchange(m, server)
	if err != nil {
		return nil
	}
	for _, k := range r.Answer {
		if k1, ok := k.(*dns.RR_DNSKEY); ok {
			if k1.KeyTag() == keytag {
				return k1
			}
		}
	}
	return nil
}

// shorten RRSIG to "miek.nl RRSIG(NS)"
func shortSig(sig *dns.RR_RRSIG) string {
	return sig.Header().Name + " RRSIG(" + dns.Rr_str[sig.TypeCovered] + ")"
}

// Walk trough message and short Key data and Sig data
func shortMsg(in *dns.Msg) *dns.Msg {
	for i := 0; i < len(in.Answer); i++ {
		in.Answer[i] = shortRR(in.Answer[i])
	}
	for i := 0; i < len(in.Ns); i++ {
		in.Ns[i] = shortRR(in.Ns[i])
	}
	for i := 0; i < len(in.Extra); i++ {
		in.Extra[i] = shortRR(in.Extra[i])
	}
	return in
}

func shortRR(r dns.RR) dns.RR {
	switch t := r.(type) {
	case *dns.RR_DS:
		t.Digest = "..."
	case *dns.RR_DNSKEY:
		t.PublicKey = "..."
	case *dns.RR_RRSIG:
		t.Signature = "..."
	case *dns.RR_NSEC3:
		t.Salt = "-" // Nobody cares
		if len(t.TypeBitMap) > 5 {
			t.TypeBitMap = t.TypeBitMap[1:5]
		}
	}
	return r
}
<|endoftext|>


<commit_before>/*
http://www.apache.org/licenses/LICENSE-2.0.txt

Copyright 2016 Intel Corporation

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package graphite

import (
	"bytes"
	"encoding/gob"
	"fmt"

	log "github.com/Sirupsen/logrus"

	plh "github.com/intelsdi-x/snap-plugin-publisher-graphite/logHelper"
	"github.com/intelsdi-x/snap/control/plugin"
	"github.com/intelsdi-x/snap/control/plugin/cpolicy"
	"github.com/intelsdi-x/snap/core/ctypes"
	"github.com/marpaia/graphite-golang"
)

const (
	name       = "graphite"
	version    = 2
	pluginType = plugin.PublisherPluginType
)

type graphitePublisher struct {
}

func NewGraphitePublisher() *graphitePublisher {
	return &graphitePublisher{}
}

func (f *graphitePublisher) Publish(contentType string, content []byte, config map[string]ctypes.ConfigValue) error {

	logger := plh.GetLogger(config, Meta())
	logger.Debug("Publishing started")
	var metrics []plugin.MetricType

	switch contentType {
	case plugin.SnapGOBContentType:
		dec := gob.NewDecoder(bytes.NewBuffer(content))
		if err := dec.Decode(&metrics); err != nil {
			logger.Errorf("Error decoding: error=%v content=%v", err, content)
			return err
		}
	default:
		logger.Errorf("Error unknown content type '%v'", contentType)
		return fmt.Errorf("Unknown content type '%s'", contentType)
	}

	logger.Debug("publishing %v metrics to %v", len(metrics), config)

	server := config["server"].(ctypes.ConfigValueStr).Value
	port := config["port"].(ctypes.ConfigValueInt).Value
	logger.Debug("Attempting to connect to %s:%d", server, port)
	var gite *graphite.Graphite
	var err error
	if pre, ok := config["prefix"]; ok {
		gite, err = graphite.NewGraphiteWithMetricPrefix(server, port, pre.(ctypes.ConfigValueStr).Value)
	} else {
		gite, err = graphite.NewGraphite(server, port)
	}
	if err != nil {
		logger.Errorf("Error Connecting to graphite at %s:%d. Error: %v", server, port, err)
		return fmt.Errorf("Error Connecting to graphite at %s:%d. Error: %v", server, port, err)
	}
	logger.Debug("Connected to %s:%s successfully", server, port)
	for _, m := range metrics {
		key := m.Namespace().Key()
		data := fmt.Sprintf("%v", m.Data())
		logger.Debug("Attempting to send %s:%s", key, data)
		err = gite.SimpleSend(key, data)
		if err != nil {
			logger.Errorf("Unable to send metric %s:%s to %s:%d. Error: %s", key, data, server, port, err)
			return fmt.Errorf("Unable to send metric %s:%s to %s:%d. Error: %s", key, data, server, port, err)
		}
		logger.Debug("Sent %s, %s", key, data)
	}
	return nil
}

func Meta() *plugin.PluginMeta {
	return plugin.NewPluginMeta(name, version, pluginType, []string{plugin.SnapGOBContentType}, []string{plugin.SnapGOBContentType})
}

func (f *graphitePublisher) GetConfigPolicy() (*cpolicy.ConfigPolicy, error) {
	cp := cpolicy.New()
	config := cpolicy.NewPolicyNode()
	config, err := plh.AddLogging(config)
	if err != nil {
		config = cpolicy.NewPolicyNode()
	}
	r1, err := cpolicy.NewStringRule("server", true)
	handleErr(err)
	r1.Description = "Address of graphite server"
	config.Add(r1)

	r2, err := cpolicy.NewIntegerRule("port", true)
	handleErr(err)
	r2.Description = "Port to connect on"
	config.Add(r2)

	r3, err := cpolicy.NewStringRule("prefix", false)
	handleErr(err)
	r3.Description = "Prefix to add to all metrics"
	config.Add(r3)

	cp.Add([]string{""}, config)
	fmt.Println(config)
	return cp, nil
}

func handleErr(e error) {
	if e != nil {
		log.Panic(e)
		panic(e)
	}
}
<commit_msg>Update version to 3<commit_after>/*
http://www.apache.org/licenses/LICENSE-2.0.txt

Copyright 2016 Intel Corporation

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package graphite

import (
	"bytes"
	"encoding/gob"
	"fmt"

	log "github.com/Sirupsen/logrus"

	plh "github.com/intelsdi-x/snap-plugin-publisher-graphite/logHelper"
	"github.com/intelsdi-x/snap/control/plugin"
	"github.com/intelsdi-x/snap/control/plugin/cpolicy"
	"github.com/intelsdi-x/snap/core/ctypes"
	"github.com/marpaia/graphite-golang"
)

const (
	name       = "graphite"
	version    = 3
	pluginType = plugin.PublisherPluginType
)

type graphitePublisher struct {
}

func NewGraphitePublisher() *graphitePublisher {
	return &graphitePublisher{}
}

func (f *graphitePublisher) Publish(contentType string, content []byte, config map[string]ctypes.ConfigValue) error {

	logger := plh.GetLogger(config, Meta())
	logger.Debug("Publishing started")
	var metrics []plugin.MetricType

	switch contentType {
	case plugin.SnapGOBContentType:
		dec := gob.NewDecoder(bytes.NewBuffer(content))
		if err := dec.Decode(&metrics); err != nil {
			logger.Errorf("Error decoding: error=%v content=%v", err, content)
			return err
		}
	default:
		logger.Errorf("Error unknown content type '%v'", contentType)
		return fmt.Errorf("Unknown content type '%s'", contentType)
	}

	logger.Debug("publishing %v metrics to %v", len(metrics), config)

	server := config["server"].(ctypes.ConfigValueStr).Value
	port := config["port"].(ctypes.ConfigValueInt).Value
	logger.Debug("Attempting to connect to %s:%d", server, port)
	var gite *graphite.Graphite
	var err error
	if pre, ok := config["prefix"]; ok {
		gite, err = graphite.NewGraphiteWithMetricPrefix(server, port, pre.(ctypes.ConfigValueStr).Value)
	} else {
		gite, err = graphite.NewGraphite(server, port)
	}
	if err != nil {
		logger.Errorf("Error Connecting to graphite at %s:%d. Error: %v", server, port, err)
		return fmt.Errorf("Error Connecting to graphite at %s:%d. Error: %v", server, port, err)
	}
	logger.Debug("Connected to %s:%s successfully", server, port)
	for _, m := range metrics {
		key := m.Namespace().Key()
		data := fmt.Sprintf("%v", m.Data())
		logger.Debug("Attempting to send %s:%s", key, data)
		err = gite.SimpleSend(key, data)
		if err != nil {
			logger.Errorf("Unable to send metric %s:%s to %s:%d. Error: %s", key, data, server, port, err)
			return fmt.Errorf("Unable to send metric %s:%s to %s:%d. Error: %s", key, data, server, port, err)
		}
		logger.Debug("Sent %s, %s", key, data)
	}
	return nil
}

func Meta() *plugin.PluginMeta {
	return plugin.NewPluginMeta(name, version, pluginType, []string{plugin.SnapGOBContentType}, []string{plugin.SnapGOBContentType})
}

func (f *graphitePublisher) GetConfigPolicy() (*cpolicy.ConfigPolicy, error) {
	cp := cpolicy.New()
	config := cpolicy.NewPolicyNode()
	config, err := plh.AddLogging(config)
	if err != nil {
		config = cpolicy.NewPolicyNode()
	}
	r1, err := cpolicy.NewStringRule("server", true)
	handleErr(err)
	r1.Description = "Address of graphite server"
	config.Add(r1)

	r2, err := cpolicy.NewIntegerRule("port", true)
	handleErr(err)
	r2.Description = "Port to connect on"
	config.Add(r2)

	r3, err := cpolicy.NewStringRule("prefix", false)
	handleErr(err)
	r3.Description = "Prefix to add to all metrics"
	config.Add(r3)

	cp.Add([]string{""}, config)
	fmt.Println(config)
	return cp, nil
}

func handleErr(e error) {
	if e != nil {
		log.Panic(e)
		panic(e)
	}
}
<|endoftext|>