<commit_before>package git

import (
	"encoding/binary"
	"errors"
	"fmt"
	"os"
	"path/filepath"

	"github.com/yosisa/go-git/lru"
)

var packMagic = [4]byte{'P', 'A', 'C', 'K'}

var ErrObjectNotFound = errors.New("Object not found")

var packEntryCache = lru.NewWithEvict(1<<24, func(key interface{}, value interface{}) {
	value.(*packEntry).Release()
})

type PackHeader struct {
	Magic   [4]byte
	Version uint32
	Total   uint32
}

type Pack struct {
	PackHeader
	r   packReader
	idx *PackIndexV2
}

func OpenPack(path string) (*Pack, error) {
	path = filepath.Clean(path)
	ext := filepath.Ext(path)
	base := path[:len(path)-len(ext)]
	idx, err := OpenPackIndex(base + ".idx")
	if err != nil {
		return nil, err
	}
	f, err := os.Open(base + ".pack")
	if err != nil {
		return nil, err
	}
	pack := &Pack{
		r:   newPackReader(f),
		idx: idx,
	}
	err = pack.verify()
	return pack, err
}

func (p *Pack) verify() (err error) {
	if err = binary.Read(p.r, binary.BigEndian, &p.PackHeader); err != nil {
		return
	}
	if p.Magic != packMagic || p.Version != 2 {
		return ErrUnknownFormat
	}
	return
}

func (p *Pack) Close() error {
	return p.r.Close()
}

func (p *Pack) Object(id SHA1, repo *Repository) (Object, error) {
	entry, err := p.entry(id)
	if err != nil {
		return nil, err
	}
	obj := newObject(entry.Type(), id, repo)
	b, err := entry.ReadAll()
	if err != nil {
		return nil, err
	}
	obj.Parse(b)
	return obj, nil
}

func (p *Pack) entry(id SHA1) (*packEntry, error) {
	entry := p.idx.Entry(id)
	if entry == nil {
		return nil, ErrObjectNotFound
	}
	return p.entryAt(entry.Offset)
}

func (p *Pack) entryAt(offset int64) (*packEntry, error) {
	if pe, ok := packEntryCache.Get(offset); ok {
		return pe.(*packEntry), nil
	}

	if _, err := p.r.Seek(offset, os.SEEK_SET); err != nil {
		return nil, err
	}

	header, err := readPackEntryHeader(p.r)
	if err != nil {
		return nil, err
	}
	size := header[0].Size0()
	typ := header[0].Type()
	for i, l := 0, len(header)-1; i < l; i++ {
		size = (header[i+1].Size() << uint(4+7*i)) | size
	}

	pe := &packEntry{
		offset:    offset,
		headerLen: len(header),
	}
	switch typ {
	case packEntryCommit:
		pe.typ = "commit"
	case packEntryTree:
		pe.typ = "tree"
	case packEntryBlob:
		pe.typ = "blob"
	case packEntryTag:
		pe.typ = "tag"
	case packEntryOfsDelta:
		header, err := readPackEntryHeader(p.r)
		if err != nil {
			return nil, err
		}
		ofs := header[0].Size()
		for _, h := range header[1:] {
			ofs += 1
			ofs = (ofs << 7) + h.Size()
		}
		delta, err := p.readDelta()
		if err != nil {
			return nil, err
		}

		entry, err := p.entryAt(offset - ofs)
		if err != nil {
			return nil, err
		}
		pe.typ = entry.Type()
		if pe.buf, err = applyDelta(entry, delta); err != nil {
			return nil, err
		}
		packEntryCache.Add(offset, pe)
		return pe, nil
	case packEntryRefDelta:
		id, err := readSHA1(p.r)
		if err != nil {
			return nil, err
		}
		delta, err := p.readDelta()
		if err != nil {
			return nil, err
		}

		entry, err := p.entry(id)
		if err != nil {
			return nil, err
		}
		pe.typ = entry.Type()
		if pe.buf, err = applyDelta(entry, delta); err != nil {
			return nil, err
		}
		packEntryCache.Add(offset, pe)
		return pe, nil
	default:
		return nil, fmt.Errorf("Unknown pack entry type: %d", typ)
	}

	pe.pr = p.r
	packEntryCache.Add(offset, pe)
	return pe, nil
}

func (p *Pack) readDelta() (*bytesBuffer, error) {
	zr, err := p.r.ZlibReader()
	if err != nil {
		return nil, err
	}
	defer zr.Close()
	return newBytesBuffer(zr)
}

type packEntryType byte

const (
	packEntryNone packEntryType = iota
	packEntryCommit
	packEntryTree
	packEntryBlob
	packEntryTag
	_
	packEntryOfsDelta
	packEntryRefDelta
)

type packEntry struct {
	typ       string
	buf       *bytesBuffer
	pr        packReader
	offset    int64
	headerLen int
}

func (p *packEntry) Type() string {
	return p.typ
}

func (p *packEntry) ReadAll() ([]byte, error) {
	if p.buf == nil {
		if p.pr.Offset() != p.offset {
			if _, err := p.pr.Seek(p.offset+int64(p.headerLen), os.SEEK_SET); err != nil {
				return nil, err
			}
		}
		zr, err := p.pr.ZlibReader()
		if err != nil {
			return nil, err
		}
		defer zr.Close()

		if p.buf, err = newBytesBuffer(zr); err != nil {
			return nil, err
		}
		packEntryCache.Add(p.offset, p)
	}
	return p.buf.Bytes(), nil
}

func (p *packEntry) Close() (err error) {
	// Don't release bytesBuffer here so that cached entry to be valid
	return
}

func (p *packEntry) Release() {
	if p.buf != nil {
		p.buf.Close()
	}
}

func (p *packEntry) Size() int {
	size := len(p.typ) + 8 + 8 + 8 + 8
	if p.buf != nil {
		size += p.buf.Len()
	}
	return size
}

type packEntryHeader byte

func (b packEntryHeader) MSB() bool {
	return (b >> 7) == 1
}

func (b packEntryHeader) Type() packEntryType {
	return packEntryType((b >> 4) & 0x07)
}

func (b packEntryHeader) Size0() int64 {
	return int64(b & 0x0f)
}

func (b packEntryHeader) Size() int64 {
	return int64(b & 0x7f)
}

var packEntryHeaderScratch []packEntryHeader = make([]packEntryHeader, 0, 10)

func readPackEntryHeader(br byteReader) (header []packEntryHeader, err error) {
	header = packEntryHeaderScratch[:0]
	for {
		var b byte
		if b, err = br.ReadByte(); err != nil {
			return
		}
		h := packEntryHeader(b)
		header = append(header, h)
		if !h.MSB() {
			return
		}
	}
}
<commit_msg>Fix data mixing due to recycling in used buffer<commit_after>package git

import (
	"encoding/binary"
	"errors"
	"fmt"
	"os"
	"path/filepath"
	"sync/atomic"

	"github.com/yosisa/go-git/lru"
)

var packMagic = [4]byte{'P', 'A', 'C', 'K'}

var ErrObjectNotFound = errors.New("Object not found")

var packEntryCache = lru.NewWithEvict(1<<24, func(key interface{}, value interface{}) {
	value.(*packEntry).Close()
})

type PackHeader struct {
	Magic   [4]byte
	Version uint32
	Total   uint32
}

type Pack struct {
	PackHeader
	r   packReader
	idx *PackIndexV2
}

func OpenPack(path string) (*Pack, error) {
	path = filepath.Clean(path)
	ext := filepath.Ext(path)
	base := path[:len(path)-len(ext)]
	idx, err := OpenPackIndex(base + ".idx")
	if err != nil {
		return nil, err
	}
	f, err := os.Open(base + ".pack")
	if err != nil {
		return nil, err
	}
	pack := &Pack{
		r:   newPackReader(f),
		idx: idx,
	}
	err = pack.verify()
	return pack, err
}

func (p *Pack) verify() (err error) {
	if err = binary.Read(p.r, binary.BigEndian, &p.PackHeader); err != nil {
		return
	}
	if p.Magic != packMagic || p.Version != 2 {
		return ErrUnknownFormat
	}
	return
}

func (p *Pack) Close() error {
	return p.r.Close()
}

func (p *Pack) Object(id SHA1, repo *Repository) (Object, error) {
	entry, err := p.entry(id)
	if err != nil {
		return nil, err
	}
	obj := newObject(entry.Type(), id, repo)
	b, err := entry.ReadAll()
	if err != nil {
		return nil, err
	}
	obj.Parse(b)
	return obj, nil
}

func (p *Pack) entry(id SHA1) (*packEntry, error) {
	entry := p.idx.Entry(id)
	if entry == nil {
		return nil, ErrObjectNotFound
	}
	return p.entryAt(entry.Offset)
}

func (p *Pack) entryAt(offset int64) (*packEntry, error) {
	if pe, ok := packEntryCache.Get(offset); ok {
		if entry := pe.(*packEntry); entry.markInUse() {
			return entry, nil
		}
	}

	if _, err := p.r.Seek(offset, os.SEEK_SET); err != nil {
		return nil, err
	}

	header, err := readPackEntryHeader(p.r)
	if err != nil {
		return nil, err
	}
	size := header[0].Size0()
	typ := header[0].Type()
	for i, l := 0, len(header)-1; i < l; i++ {
		size = (header[i+1].Size() << uint(4+7*i)) | size
	}

	pe := &packEntry{
		offset:    offset,
		headerLen: len(header),
		used:      1,
	}

	switch typ {
	case packEntryCommit:
		pe.typ = "commit"
	case packEntryTree:
		pe.typ = "tree"
	case packEntryBlob:
		pe.typ = "blob"
	case packEntryTag:
		pe.typ = "tag"
	case packEntryOfsDelta:
		header, err := readPackEntryHeader(p.r)
		if err != nil {
			return nil, err
		}
		ofs := header[0].Size()
		for _, h := range header[1:] {
			ofs += 1
			ofs = (ofs << 7) + h.Size()
		}
		delta, err := p.readDelta()
		if err != nil {
			return nil, err
		}

		entry, err := p.entryAt(offset - ofs)
		if err != nil {
			return nil, err
		}
		pe.typ = entry.Type()
		if pe.buf, err = applyDelta(entry, delta); err != nil {
			return nil, err
		}
		packEntryCache.Add(offset, pe)
		return pe, nil
	case packEntryRefDelta:
		id, err := readSHA1(p.r)
		if err != nil {
			return nil, err
		}
		delta, err := p.readDelta()
		if err != nil {
			return nil, err
		}

		entry, err := p.entry(id)
		if err != nil {
			return nil, err
		}
		pe.typ = entry.Type()
		if pe.buf, err = applyDelta(entry, delta); err != nil {
			return nil, err
		}
		packEntryCache.Add(offset, pe)
		return pe, nil
	default:
		return nil, fmt.Errorf("Unknown pack entry type: %d", typ)
	}

	pe.pr = p.r
	packEntryCache.Add(offset, pe)
	return pe, nil
}

func (p *Pack) readDelta() (*bytesBuffer, error) {
	zr, err := p.r.ZlibReader()
	if err != nil {
		return nil, err
	}
	defer zr.Close()
	return newBytesBuffer(zr)
}

type packEntryType byte

const (
	packEntryNone packEntryType = iota
	packEntryCommit
	packEntryTree
	packEntryBlob
	packEntryTag
	_
	packEntryOfsDelta
	packEntryRefDelta
)

type packEntry struct {
	typ       string
	buf       *bytesBuffer
	pr        packReader
	offset    int64
	headerLen int
	used      int32
}

func (p *packEntry) Type() string {
	return p.typ
}

func (p *packEntry) ReadAll() ([]byte, error) {
	if p.buf == nil {
		if p.pr.Offset() != p.offset {
			if _, err := p.pr.Seek(p.offset+int64(p.headerLen), os.SEEK_SET); err != nil {
				return nil, err
			}
		}
		zr, err := p.pr.ZlibReader()
		if err != nil {
			return nil, err
		}
		defer zr.Close()

		if p.buf, err = newBytesBuffer(zr); err != nil {
			return nil, err
		}
		packEntryCache.Add(p.offset, p)
	}
	return p.buf.Bytes(), nil
}

func (p *packEntry) Close() (err error) {
	// Release bytesBuffer only if no one used and not in the lru cache.
	if n := atomic.AddInt32(&p.used, -1); n < 0 && p.buf != nil {
		p.buf.Close()
	}
	return
}

func (p *packEntry) markInUse() bool {
	return atomic.AddInt32(&p.used, 1) > 0
}

func (p *packEntry) Size() int {
	size := len(p.typ) + 8 + 8 + 8 + 8
	if p.buf != nil {
		size += p.buf.Len()
	}
	return size
}

type packEntryHeader byte

func (b packEntryHeader) MSB() bool {
	return (b >> 7) == 1
}

func (b packEntryHeader) Type() packEntryType {
	return packEntryType((b >> 4) & 0x07)
}

func (b packEntryHeader) Size0() int64 {
	return int64(b & 0x0f)
}

func (b packEntryHeader) Size() int64 {
	return int64(b & 0x7f)
}

var packEntryHeaderScratch []packEntryHeader = make([]packEntryHeader, 0, 10)

func readPackEntryHeader(br byteReader) (header []packEntryHeader, err error) {
	header = packEntryHeaderScratch[:0]
	for {
		var b byte
		if b, err = br.ReadByte(); err != nil {
			return
		}
		h := packEntryHeader(b)
		header = append(header, h)
		if !h.MSB() {
			return
		}
	}
}
<|endoftext|>


<commit_before>package gamq

import (
	"bufio"
	"bytes"
	"testing"
)

func TestConnectionManager_parseClientCommand_helpMessageReturns(t *testing.T) {
	underTest := ConnectionManager{}

	buf := new(bytes.Buffer)
	bufWriter := bufio.NewWriter(buf)
	mockClient := Client{Name: "Mock", Writer: bufWriter}

	underTest.parseClientCommand("HELP", &mockClient)

	if buf.String() == UNRECOGNISEDCOMMANDTEXT {
		t.Fail()
	}
}

func TestConnectionManager_parseClientCommand_isCaseInsensitive(t *testing.T) {
	underTest := ConnectionManager{}

	buf := new(bytes.Buffer)
	bufWriter := bufio.NewWriter(buf)
	mockClient := Client{Name: "Mock", Writer: bufWriter}

	underTest.parseClientCommand("help", &mockClient)

	if buf.String() == UNRECOGNISEDCOMMANDTEXT {
		t.Fail()
	}
}
<commit_msg>Fixed test.<commit_after>package gamq

import (
	"bufio"
	"bytes"
	"testing"
)

func TestConnectionManager_parseClientCommand_helpMessageReturns(t *testing.T) {
	underTest := ConnectionManager{}

	buf := new(bytes.Buffer)
	bufWriter := bufio.NewWriter(buf)
	mockClient := Client{Name: "Mock", Writer: bufWriter}

	underTest.parseClientCommand([]string{"HELP"}, &mockClient)

	if buf.String() == UNRECOGNISEDCOMMANDTEXT {
		t.Fail()
	}
}

func TestConnectionManager_parseClientCommand_isCaseInsensitive(t *testing.T) {
	underTest := ConnectionManager{}

	buf := new(bytes.Buffer)
	bufWriter := bufio.NewWriter(buf)
	mockClient := Client{Name: "Mock", Writer: bufWriter}

	underTest.parseClientCommand([]string{"help"}, &mockClient)

	if buf.String() == UNRECOGNISEDCOMMANDTEXT {
		t.Fail()
	}
}
<|endoftext|>


<commit_before>// Copyright 2016 Takbok, Inc. All rights reserved.
//
// 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 demo

import (
	"bufio"
	"bytes"
	"encoding/csv"
	"fmt"
	"log"
	"net/http"
	"strings"

	"appengine"

	newappengine "google.golang.org/appengine"
)

func init() {
	http.HandleFunc("/import", handleImport)
	http.HandleFunc("/import/do", handleImportDo)
}

func handleImport(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		http.Redirect(w, r, "/?error=noDirectAccess", http.StatusTemporaryRedirect)
		return
	}

	url, err := getProperDomainNameFromUrl(r.FormValue("url"))
	if err != nil {
		http.Redirect(w, r, "/?error=badUrl", http.StatusTemporaryRedirect)
		return
	}

	if !isUrlOnGoogleApp(w, r, url) {
		http.Redirect(w, r, "/?error=notOnGoogleApps", http.StatusTemporaryRedirect)
		return
	}

	r.ParseMultipartForm(32 << 20)
	inp_file, _, err = r.FormFile("inputfile")
	if err != nil {
		log.Print("\n returning bcoz of error 1")
		log.Print(err)
		return
	}
	defer inp_file.Close()

	x := AppState{url}
	ctx := appengine.NewContext(r)
	config.RedirectURL = fmt.Sprintf(`http://%s/import/do`, r.Host)

	url = config.AuthCodeURL(x.encodeState())
	ctx.Infof("Auth: %v", url)

	http.Redirect(w, r, url, http.StatusTemporaryRedirect)
}

func handleImportDo(w http.ResponseWriter, r *http.Request) {
	y := r.FormValue("state")

	state := new(AppState)
	state.decodeState(y)

	ctx := appengine.NewContext(r)
	newctx := newappengine.NewContext(r)

	tok, err := config.Exchange(newctx, r.FormValue("code"))
	if err != nil {
		ctx.Errorf("exchange error: %v", err)
		return
	}

	client := config.Client(newctx, tok)

	cr := csv.NewReader(bufio.NewReader(inp_file))
	records, err := cr.ReadAll()
	if err != nil {
		log.Print("\n CSV file error")
		ctx.Errorf("%v", err)
		return
	}

	names := records[0]
	datalen := len(records)
	log.Print("\n Loop started")

	for i := 1; i < datalen; i++ {
		rec := records[i]
		buf := new(bytes.Buffer)
		fmt.Fprintf(buf, `<atom:entry xmlns:atom='http://www.w3.org/2005/Atom' xmlns:gd='http://schemas.google.com/g/2005'>
<atom:category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact' />
<atom:content type='text'>Notes</atom:content>
`)
		numExtended, M := 0, 10
		for j, s := range names {
			if s == "Name" {
				fmt.Fprintf(buf, "<gd:name><gd:fullName>%v</gd:fullName></gd:name>\n", rec[j])
			} else if s == "E-mail Address" {
				fmt.Fprintf(buf, "<gd:email rel='http://schemas.google.com/g/2005#home' address='%v'/>", rec[j])
			} else if strings.HasPrefix(s, "E-mail ") && strings.HasSuffix(s, " Address") {
				var num uint
				fmt.Sscanf(s, "E-mail %v Address", &num)
				if numExtended < M && ((0 < num && num < 6) || s == "E-mail Address") {
					fmt.Fprintf(buf, `<gd:extendedProperty name="%v" value="%v" />`+"\n", s, rec[j])
					numExtended++
				}
			} else if numExtended < M {
				fmt.Fprintf(buf, `<gd:extendedProperty name="%v" value="%v" />`+"\n", s, rec[j])
				numExtended++
			}
		}

		fmt.Fprintf(buf, `</atom:entry>`)

		res, _ := client.Post(fmt.Sprintf(feedUrl, state.Domain), `application/atom+xml`, strings.NewReader(buf.String()))

		fmt.Fprintf(w, "Result: %v
", res.Status)
	}
}
<commit_msg>Import routine modification. Support for multiple emails and ims along with 10 extended properties<commit_after>// Copyright 2016 Takbok, Inc. All rights reserved.
//
// 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 demo

import (
	"bufio"
	"bytes"
	"encoding/csv"
	"fmt"
	"log"
	"net/http"
	"strings"

	"appengine"

	newappengine "google.golang.org/appengine"
)

func init() {
	http.HandleFunc("/import", handleImport)
	http.HandleFunc("/import/do", handleImportDo)
}

func handleImport(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		http.Redirect(w, r, "/?error=noDirectAccess", http.StatusTemporaryRedirect)
		return
	}

	url, err := getProperDomainNameFromUrl(r.FormValue("url"))
	if err != nil {
		http.Redirect(w, r, "/?error=badUrl", http.StatusTemporaryRedirect)
		return
	}

	if !isUrlOnGoogleApp(w, r, url) {
		http.Redirect(w, r, "/?error=notOnGoogleApps", http.StatusTemporaryRedirect)
		return
	}

	r.ParseMultipartForm(32 << 20)
	inp_file, _, err = r.FormFile("inputfile")
	if err != nil {
		log.Print("\n returning bcoz of error 1")
		log.Print(err)
		return
	}
	defer inp_file.Close()

	x := AppState{url}
	ctx := appengine.NewContext(r)
	config.RedirectURL = fmt.Sprintf(`http://%s/import/do`, r.Host)

	url = config.AuthCodeURL(x.encodeState())
	ctx.Infof("Auth: %v", url)

	http.Redirect(w, r, url, http.StatusTemporaryRedirect)
}

func handleImportDo(w http.ResponseWriter, r *http.Request) {
	y := r.FormValue("state")

	state := new(AppState)
	state.decodeState(y)

	ctx := appengine.NewContext(r)
	newctx := newappengine.NewContext(r)

	tok, err := config.Exchange(newctx, r.FormValue("code"))
	if err != nil {
		ctx.Errorf("exchange error: %v", err)
		return
	}

	client := config.Client(newctx, tok)

	cr := csv.NewReader(bufio.NewReader(inp_file))
	records, err := cr.ReadAll()
	if err != nil {
		log.Print("\n CSV file error")
		ctx.Errorf("%v", err)
		return
	}

	names := records[0]
	datalen := len(records)
	log.Print("\n Loop started")

	for i := 1; i < datalen; i++ {
		rec := records[i]
		buf := new(bytes.Buffer)
		fmt.Fprintf(buf, `<atom:entry xmlns:atom='http://www.w3.org/2005/Atom' xmlns:gd='http://schemas.google.com/g/2005'>
<atom:category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact' />
<atom:content type='text'>Notes</atom:content>
`)
		var nameBuf, emailBuf, imBuf, orgBuf, extendedBuf string
		//var phoneBuf string
		orgBuf = `<gd:organization rel="http://schemas.google.com/g/2005#work" primary="true">` + "\n"
		numChk, maxChk := 0, 9
		for j, s := range names {
			if s == "E-mail Address" {
				emailBuf += fmt.Sprintf(`<gd:email rel="http://schemas.google.com/g/2005#home" address="%v" primary="true"/>` + "\n", rec[j])
				continue
			}
			if strings.Contains(s, "E-mail") {
				emailBuf += fmt.Sprintf(`<gd:email rel="http://schemas.google.com/g/2005#other" address="%v"/>` + "\n", rec[j])
				continue
			}
			if strings.Contains(s, "IM") {
				imBuf += fmt.Sprintf(`<gd:im address="%v" rel="http://schemas.google.com/g/2005#other"/>` + "\n", rec[j])
				continue
			}

			switch(s){
				case "Name" :
				nameBuf += fmt.Sprintf("\n" + `<gd:fullName>%v</gd:fullName>`, rec[j])
				break
				case "GivenName" :
				nameBuf += fmt.Sprintf("\n" + `<gd:givenName>%v</gd:givenName>`, rec[j])
				break
				case "FamilyName" :
				nameBuf += fmt.Sprintf("\n" + `<gd:familyName>%v</gd:familyName>`, rec[j])
				break;
				/*case "Company" :
				orgBuf += fmt.Sprintf(`<gd:orgName>%v</gd:orgName>` + "\n", rec[j])
				break
				case "Job Title" :
				orgBuf += fmt.Sprintf(`<gd:orgTitle>%v</gd:orgTitle>` + "\n", rec[j])
				break
				case "Department" :
				orgBuf += fmt.Sprintf(`<gd:orgDepartment>%v</gd:orgDepartment>` + "\n", rec[j])
				break
				case "Job Description" :
				orgBuf += fmt.Sprintf(`<gd:orgJobDescription>%v</gd:orgJobDescription>` + "\n", rec[j])
				break
				case "Business Fax" :
				phoneBuf += fmt.Sprintf(`<gd:phoneNumber rel="http://schemas.google.com/g/2005#work_fax" label="%v">\n%v\n</gd:phoneNumber>\n`, s, rec[j])
				break;
				case "Business Phone" :
				phoneBuf += fmt.Sprintf(`<gd:phoneNumber rel="http://schemas.google.com/g/2005#work" label="%v">\n%v\n</gd:phoneNumber>\n`, s, rec[j])
				break;
				case "Business Phone 2" :
				phoneBuf += fmt.Sprintf(`<gd:phoneNumber rel="http://schemas.google.com/g/2005#other" label="%v">\n%v\n</gd:phoneNumber>\n`, s, rec[j])
				break;
				case "Home Fax" :
				phoneBuf += fmt.Sprintf(`<gd:phoneNumber rel="http://schemas.google.com/g/2005#home_fax" label="%v">\n%v\n</gd:phoneNumber>\n`, s, rec[j])
				break;
				case "Home Phone" :
				phoneBuf += fmt.Sprintf(`<gd:phoneNumber rel="http://schemas.google.com/g/2005#home" label="%v">\n%v\n</gd:phoneNumber>\n`, s, rec[j])
				break;
				case "Home Phone 2" :
				phoneBuf += fmt.Sprintf(`<gd:phoneNumber rel="http://schemas.google.com/g/2005#other" label="%v">\n%v\n</gd:phoneNumber>\n`, s, rec[j])
				break;
				case "Other Phone" :
				phoneBuf += fmt.Sprintf(`<gd:phoneNumber rel="http://schemas.google.com/g/2005#other" label="%v">'%v\n</gd:phoneNumber>\n`, s, rec[j])
				break;
				case "Mobile Phone" :
				phoneBuf += fmt.Sprintf(`<gd:phoneNumber rel="http://schemas.google.com/g/2005#mobile" label="%v">\n%v\n</gd:phoneNumber>\n`, s, rec[j])
				break;
				case "Pager" :
				phoneBuf += fmt.Sprintf(`<gd:phoneNumber rel="http://schemas.google.com/g/2005#pager" label="%v">\n%v\n</gd:phoneNumber>\n`, s, rec[j])
				break;*/
				default :
				if numChk > maxChk {
					continue
				}
				extendedBuf += fmt.Sprintf(`<gd:extendedProperty name="%v" value="%v" />` + "\n", s, rec[j])
				numChk += 1
				break
			}
		}
		nameBuf = "<gd:name>" + nameBuf + "</gd:name>\n"

		fmt.Fprintf(buf, nameBuf)
		fmt.Fprintf(buf, emailBuf)
		fmt.Fprintf(buf, imBuf)
		orgBuf += "</gd:organization>\n"
		//fmt.Fprintf(buf, orgBuf)
		//fmt.Fprintf(buf, phoneBuf)
		fmt.Fprintf(buf, extendedBuf)

		fmt.Fprintf(buf, `</atom:entry>`)

		res, _ := client.Post(fmt.Sprintf(feedUrl, state.Domain), `application/atom+xml`, strings.NewReader(buf.String()))

		fmt.Fprintf(w, "Result: %v
", res.Status)

	}
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"github.com/BytemarkHosting/bytemark-client/cmd/bytemark/util"
	"github.com/BytemarkHosting/bytemark-client/lib"
	"github.com/BytemarkHosting/bytemark-client/lib/brain"
	"github.com/BytemarkHosting/bytemark-client/mocks"
	"github.com/cheekybits/is"
	"github.com/urfave/cli"
	"strings"
	"testing"
)

func TestCommandConfigSet(t *testing.T) {
	is := is.New(t)
	config, _ := baseTestSetup(t, false)

	config.When("GetV", "user").Return(util.ConfigVar{"user", "old-test-user", "config"})
	config.When("GetIgnoreErr", "user").Return("old-test-user")

	config.When("SetPersistent", "user", "test-user", "CMD set").Times(1)

	err := global.App.Run(strings.Split("bytemark config set user test-user", " "))
	is.Nil(err)

	if ok, vErr := config.Verify(); !ok {
		t.Fatal(vErr)
	}

	err = global.App.Run(strings.Split("bytemark config set flimflam test-user", " "))
	is.NotNil(err)
}

func endpointTests(t *testing.T, varname string, fnUnderTest func(string) error) {
	endpoints := map[string]bool{
		"https://uk0.bigv.io":      false,
		"https://uk0.bigv.io/":     false,
		"test":                     true,
		"/bivoac":                  true,
		"http://insecure-endpoint": false,
		"gopher://really":          true,
	}

	for e, shouldError := range endpoints {
		err := fnUnderTest(e)
		if shouldError && err == nil {
			t.Errorf("testing set %s %s: should error, but didn't\r\n", varname, e)
		} else if !shouldError && err != nil {
			t.Errorf("testing set %s %s: should not error, but did with '%s'\r\n", varname, e, err.Error())
		}
	}
}

func accountTests(t *testing.T, ctx *Context, c *mocks.Client, fnUnderTest validationFn) {
	accounts := map[string]bool{
		"":                    false,
		"existent-account":    true,
		"nonexistent-account": false,
	}

	for a, shouldError := range accounts {
		setupAccountTest(c, a, shouldError)
		err := fnUnderTest(ctx, a)
		if shouldError && err == nil {
			t.Errorf("testing set account %s: should error, but didn't\r\n", a)
		} else if !shouldError && err != nil {
			t.Errorf("testing set account %s: should not error, but did with '%s'\r\n", a, err.Error())
		}
		if ok, vErr := c.Verify(); !ok {
			t.Fatal(vErr)
		}
		c.Reset()
		ctx.Reset()

	}
}

func setupAccountTest(c *mocks.Client, name string, shouldError bool) {
	c.When("ParseAccountName", name, []string{""}).Return(name)

	if shouldError {
		c.When("GetAccount", name).Return(nil, &lib.NotFoundError{})
	} else {
		c.When("GetAccount", name).Return(&lib.Account{}, nil)
	}
}

func setupGroupTest(c *mocks.Client, name string, shouldError bool) {
	groupName := lib.GroupName{Group: name}
	c.When("ParseGroupName", name, []*lib.GroupName{{}}).Return(&groupName)
	if shouldError {
		c.When("GetGroup", &groupName).Return(nil, &lib.NotFoundError{}).Times(1)
	} else {
		c.When("GetGroup", &groupName).Return(&brain.Group{}, nil).Times(1)
	}
}

type validationFn func(ctx *Context, name string) error

func groupTests(t *testing.T, c *Context, client *mocks.Client, fnUnderTest validationFn) {
	groups := map[string]bool{
		"":                  true,
		"existent-group":    false,
		"nonexistent-group": true,
	}

	for g, shouldError := range groups {
		t.Log(g)
		setupGroupTest(client, g, shouldError)
		err := fnUnderTest(c, g)
		if shouldError && err == nil {
			t.Errorf("testing set group %s: should error, but didn't\r\n", g)
		} else if !shouldError && err != nil {
			t.Errorf("testing set group %s: should not error, but did with '%s'\r\n", g, err.Error())
		}
		if ok, vErr := client.Verify(); !ok {
			t.Fatalf("testing set group %s: %v\r\n", g, vErr)
		}
		client.Reset()
		c.Reset()
	}
}

func TestEndpointValidation(t *testing.T) {
	endpointTests(t, "endpoint (direct)", validateEndpointForConfig)
}

func TestValidation(t *testing.T) {
	config, client := baseTestSetup(t, false)

	config.When("GetGroup").Return(&lib.GroupName{Group: "", Account: ""})
	config.When("GetIgnoreErr", "account").Return("")
	config.When("GetIgnoreErr", "token").Return("test-token")

	flagset := flag.NewFlagSet("TestValidation", flag.ContinueOnError)
	flagset.Bool("force", false, "")

	ctx := Context{
		Context: cli.NewContext(global.App, flagset, nil),
	}

	t.Logf("Testing validateConfigValue\r\n")
	for _, varname := range []string{"endpoint", "api-endpoint", "auth-endpoint", "billing-endpoint", "spp-endpoint"} {
		endpointTests(t, varname, func(endpoint string) error {
			return validateConfigValue(&ctx, varname, endpoint)
		})
	}
	groupTests(t, &ctx, client, func(c *Context, groupName string) error {
		return validateConfigValue(c, "group", groupName)
	})
	accountTests(t, &ctx, client, func(c *Context, accountName string) error {
		return validateConfigValue(c, "account", accountName)
	})

	if ok, vErr := config.Verify(); !ok {
		t.Fatal(vErr)
	}
}

func TestGroupValidation(t *testing.T) {
	config, client := baseTestSetup(t, false)

	config.When("GetGroup").Return(&lib.GroupName{Group: "", Account: ""})
	config.When("GetIgnoreErr", "account").Return("")
	config.When("GetIgnoreErr", "token").Return("test-token")

	flagset := flag.NewFlagSet("TestValidation", flag.ContinueOnError)
	flagset.Bool("force", false, "")

	ctx := Context{
		Context: cli.NewContext(global.App, flagset, nil),
	}
	t.Logf("Testing validateGroupForConfig\r\n")
	groupTests(t, &ctx, client, validateGroupForConfig)
}

func TestAccountValidation(t *testing.T) {
	config, client := baseTestSetup(t, false)

	config.When("GetGroup").Return(lib.GroupName{Group: "default-group", Account: "default-account"})
	config.When("GetIgnoreErr", "account").Return("")
	config.When("GetIgnoreErr", "token").Return("test-token")

	ctx := Context{}

	accountTests(t, &ctx, client, validateAccountForConfig)
}
<commit_msg>Better testing for config set.<commit_after>package main

import (
	"flag"
	"github.com/BytemarkHosting/bytemark-client/cmd/bytemark/util"
	"github.com/BytemarkHosting/bytemark-client/lib"
	"github.com/BytemarkHosting/bytemark-client/lib/brain"
	"github.com/BytemarkHosting/bytemark-client/mocks"
	"github.com/cheekybits/is"
	"github.com/urfave/cli"
	"strings"
	"testing"
)

func TestConfigAccountValidation(t *testing.T) {
	config, client := baseTestSetup(t, false)

	config.When("GetGroup").Return(lib.GroupName{Group: "default-group", Account: "default-account"})
	config.When("GetIgnoreErr", "account").Return("")
	config.When("GetIgnoreErr", "token").Return("test-token")

	ctx := Context{}

	runAccountTests(t, &ctx, client, getValidationTests()["account"], validateAccountForConfig)
}

func TestConfigGroupValidation(t *testing.T) {
	config, client := baseTestSetup(t, false)

	config.When("GetGroup").Return(&lib.GroupName{Group: "", Account: ""})
	config.When("GetIgnoreErr", "account").Return("")
	config.When("GetIgnoreErr", "token").Return("test-token")

	flagset := flag.NewFlagSet("TestValidation", flag.ContinueOnError)
	flagset.Bool("force", false, "")

	ctx := Context{
		Context: cli.NewContext(global.App, flagset, nil),
	}
	t.Logf("Testing validateGroupForConfig\r\n")
	runGroupTests(t, &ctx, client, getValidationTests()["group"], validateGroupForConfig)
}

func TestConfigEndpointValidation(t *testing.T) {
	endpoints := getValidationTests()["endpoint"]
	runEndpointTests(t, "endpoint (direct)", endpoints, validateEndpointForConfig)
}

func TestConfigValidations(t *testing.T) {
	config, client := baseTestSetup(t, false)

	config.When("GetGroup").Return(&lib.GroupName{Group: "", Account: ""})
	config.When("GetIgnoreErr", "account").Return("")
	config.When("GetIgnoreErr", "token").Return("test-token")

	flagset := flag.NewFlagSet("TestValidationWithForce", flag.ContinueOnError)
	flagset.Bool("force", false, "")

	ctx := Context{
		Context: cli.NewContext(global.App, flagset, nil),
	}

	tests := getValidationTests()

	for _, varname := range []string{"endpoint", "api-endpoint", "auth-endpoint", "billing-endpoint", "spp-endpoint"} {
		runEndpointTests(t, varname, tests["endpoint"], func(endpoint string) error {
			return validateConfigValue(&ctx, varname, endpoint)
		})
	}
	runGroupTests(t, &ctx, client, tests["group"], func(c *Context, groupName string) error {
		return validateConfigValue(c, "group", groupName)
	})
	runAccountTests(t, &ctx, client, tests["account"], func(c *Context, accountName string) error {

		return validateConfigValue(c, "account", accountName)
	})
	runDebugLevelTests(t, tests["debug-level"], func(level string) error {
		return validateConfigValue(&ctx, "debug-level", level)
	})

	if ok, vErr := config.Verify(); !ok {
		t.Fatal(vErr)
	}
}

func TestCommandConfigSet(t *testing.T) {
	is := is.New(t)
	config, client := baseTestSetup(t, false)

	// setup sets up all the necessary config defaulty stuff for all our tests

	setup := func() {
		config.Reset()
		client.Reset()
		config.When("GetV", "user").Return(util.ConfigVar{"user", "old-test-user", "config"})
		config.When("GetV", "account").Return(util.ConfigVar{"account", "", ""})
		config.When("GetV", "endpoint").Return(util.ConfigVar{"endpoint", "", ""})
		config.When("GetV", "group").Return(util.ConfigVar{"group", "", ""})
		config.When("GetV", "debug-level").Return(util.ConfigVar{"debug-level", "", ""})
		config.When("Get", "token").Return("test-token", nil)
		config.When("GetIgnoreErr", "user").Return("old-test-user")
		config.When("GetIgnoreErr", "yubikey").Return("")
		config.When("GetIgnoreErr", "account").Return("")
		config.When("GetGroup").Return(&lib.GroupName{})
		client.When("AuthWithToken", "test-token").Return(nil)
	}

	setup()

	config.When("SetPersistent", "user", "test-user", "CMD set").Times(1)

	err := global.App.Run(strings.Split("bytemark config set user test-user", " "))
	is.Nil(err)

	if ok, vErr := config.Verify(); !ok {
		t.Fatal(vErr)
	}

	err = global.App.Run(strings.Split("bytemark config set flimflam test-user", " "))
	is.NotNil(err)

	// test setting all the other variables

	tests := getValidationTests()
	for varname := range tests {
		for value, errSpec := range tests[varname] {
			setup()
			switch varname {
			case "account":
				setupAccountTest(client, value, errSpec.err)
			case "group":
				setupGroupTest(client, value, errSpec.err)
			}
			if errSpec.shouldErr == false {
				config.When("GetIgnoreErr", varname).Return("")
				config.When("SetPersistent", varname, value, "CMD set").Times(1)
			}

			err = global.App.Run([]string{"bytemark", "config", "set", varname, value})
			if errSpec.shouldErr && err == nil {
				t.Errorf("bytemark config set %s %s should've errored but didn't.\r\n", varname, value)
			} else if !errSpec.shouldErr && err != nil {
				t.Errorf("bytemark config set %s %s should've suceeded but didn't: %s\r\n", varname, value, err.Error())
			}
			if ok, vErr := config.Verify(); !ok {
				t.Errorf("bytemark config set %s %s - config.Verify error: %s\r\n", varname, value, vErr)
			}
		}
	}

	// now test that --force works.

	for varname := range tests {
		for value := range tests[varname] {
			setup()
			config.When("GetIgnoreErr", varname).Return("")
			config.When("SetPersistent", varname, value, "CMD set").Times(1)

			err = global.App.Run([]string{"bytemark", "config", "set", "--force", varname, value})
			if err != nil {
				t.Errorf("bytemark config set %s %s should've suceeded but didn't: %s\r\n", varname, value, err.Error())
			}
			if ok, vErr := config.Verify(); !ok {
				t.Errorf("bytemark config set %s %s - config.Verify error: %s\r\n", varname, value, vErr)
			}
		}
	}

}

func setupAccountTest(c *mocks.Client, name string, err error) {
	c.When("ParseAccountName", name, []string{""}).Return(name)

	if err != nil {
		c.When("GetAccount", name).Return(nil, err)
	} else {
		c.When("GetAccount", name).Return(&lib.Account{}, nil)
	}
}

func setupGroupTest(c *mocks.Client, name string, err error) {
	groupName := lib.GroupName{Group: name}
	c.When("ParseGroupName", name, []*lib.GroupName{{}}).Return(&groupName)
	if err != nil {
		c.When("GetGroup", &groupName).Return(nil, err).Times(1)
	} else {
		c.When("GetGroup", &groupName).Return(&brain.Group{}, nil).Times(1)
	}
}

type validationFn func(ctx *Context, name string) error

func runAccountTests(t *testing.T, ctx *Context, c *mocks.Client, accounts map[string]errSpec, fnUnderTest validationFn) {
	for a, spec := range accounts {
		setupAccountTest(c, a, spec.err)
		err := fnUnderTest(ctx, a)
		if spec.shouldErr && err == nil {
			t.Errorf("testing set account %s: should error, but didn't\r\n", a)
		} else if !spec.shouldErr && err != nil {
			t.Errorf("testing set account %s: should not error, but did with '%s'\r\n", a, err.Error())
		}
		if ok, vErr := c.Verify(); !ok {
			t.Fatal(vErr)
		}
		c.Reset()
		ctx.Reset()
	}
}

func runDebugLevelTests(t *testing.T, debugLvls map[string]errSpec, fnUnderTest func(string) error) {
	for level, spec := range debugLvls {
		err := fnUnderTest(level)
		if spec.shouldErr && err == nil {
			t.Errorf("testing set debug-level %s: should error, but didn't\r\n", level)
		} else if !spec.shouldErr && err != nil {
			t.Errorf("testing set debug-level %s: should not error, but did with '%s'\r\n", level, err.Error())
		}
	}

}

func runEndpointTests(t *testing.T, varname string, endpoints map[string]errSpec, fnUnderTest func(string) error) {
	for e, spec := range endpoints {
		err := fnUnderTest(e)
		if spec.shouldErr && err == nil {
			t.Errorf("testing set %s %s: should error, but didn't\r\n", varname, e)
		} else if !spec.shouldErr && err != nil {
			t.Errorf("testing set %s %s: should not error, but did with '%s'\r\n", varname, e, err.Error())
		}
	}
}

func runGroupTests(t *testing.T, c *Context, client *mocks.Client, groups map[string]errSpec, fnUnderTest validationFn) {
	for g, spec := range groups {
		t.Log(g)
		setupGroupTest(client, g, spec.err)
		err := fnUnderTest(c, g)
		if spec.shouldErr && err == nil {
			t.Errorf("testing set group %s: should error, but didn't\r\n", g)
		} else if !spec.shouldErr && err != nil {
			t.Errorf("testing set group %s: should not error, but did with '%s'\r\n", g, err.Error())
		}
		if ok, vErr := client.Verify(); !ok {
			t.Fatalf("testing set group %s: %v\r\n", g, vErr)
		}
		client.Reset()
		c.Reset()
	}
}

type errSpec struct {
	// true if there should be an error
	shouldErr bool
	// the error to output from the lib mock
	err error
}

func getValidationTests() map[string]map[string]errSpec {
	return map[string]map[string]errSpec{
		"endpoint": map[string]errSpec{
			"https://uk0.bigv.io":          {shouldErr: false},
			"https://uk0.bigv.io/":         {shouldErr: false},
			"test":                         {shouldErr: true},
			"/bivoac":                      {shouldErr: true},
			"http://insecure-endpoint":     {shouldErr: false},
			"gopher://really":              {shouldErr: true},
			"http://hamlet's uncle did it": {shouldErr: true},
			"http:///no-hostname":          {shouldErr: true},
		},
		"account": map[string]errSpec{
			"":                    {shouldErr: true, err: lib.NotFoundError{}},
			"existent-account":    {shouldErr: false},
			"nonexistent-account": {shouldErr: true, err: lib.NotFoundError{}},
		},

		"group": map[string]errSpec{
			"":                  {shouldErr: true, err: lib.NotFoundError{}},
			"existent-group":    {shouldErr: false},
			"nonexistent-group": {shouldErr: true, err: lib.NotFoundError{}},
		},

		"debug-level": map[string]errSpec{
			"":         {shouldErr: true},
			"1":        {shouldErr: false},
			"9000":     {shouldErr: false},
			"-5":       {shouldErr: true},
			"barfbags": {shouldErr: true},
		},
	}
}
<|endoftext|>


<commit_before>package bitmap

import (
	"math"
	"testing"

	"github.com/RoaringBitmap/roaring"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"github.com/anacrolix/missinggo/iter"
	"github.com/anacrolix/missinggo/slices"
)

func TestEmptyBitmap(t *testing.T) {
	var bm Bitmap
	assert.False(t, bm.Contains(0))
	bm.Remove(0)
	it := iter.NewIterator(&bm)
	assert.Panics(t, func() { it.Value() })
	assert.False(t, it.Next())
}

func bitmapSlice(bm *Bitmap) (ret []int) {
	sl := iter.IterableAsSlice(bm)
	slices.MakeInto(&ret, sl)
	return
}

func TestSimpleBitmap(t *testing.T) {
	bm := new(Bitmap)
	assert.EqualValues(t, []int(nil), bitmapSlice(bm))
	bm.Add(0)
	assert.True(t, bm.Contains(0))
	assert.False(t, bm.Contains(1))
	assert.EqualValues(t, 1, bm.Len())
	bm.Add(3)
	assert.True(t, bm.Contains(0))
	assert.True(t, bm.Contains(3))
	assert.EqualValues(t, []int{0, 3}, bitmapSlice(bm))
	assert.EqualValues(t, 2, bm.Len())
	bm.Remove(0)
	assert.EqualValues(t, []int{3}, bitmapSlice(bm))
	assert.EqualValues(t, 1, bm.Len())
}

func TestSub(t *testing.T) {
	var left, right Bitmap
	left.Add(2, 5, 4)
	right.Add(3, 2, 6)
	assert.Equal(t, []int{4, 5}, Sub(left, right).ToSortedSlice())
	assert.Equal(t, []int{3, 6}, Sub(right, left).ToSortedSlice())
}

func TestSubUninited(t *testing.T) {
	var left, right Bitmap
	assert.EqualValues(t, []int(nil), Sub(left, right).ToSortedSlice())
}

func TestAddRange(t *testing.T) {
	var bm Bitmap
	bm.AddRange(21, 26)
	bm.AddRange(9, 14)
	bm.AddRange(11, 16)
	bm.Remove(12)
	assert.EqualValues(t, []int{9, 10, 11, 13, 14, 15, 21, 22, 23, 24, 25}, bm.ToSortedSlice())
	assert.EqualValues(t, 11, bm.Len())
	bm.Clear()
	bm.AddRange(3, 7)
	bm.AddRange(0, 3)
	bm.AddRange(2, 4)
	bm.Remove(3)
	assert.EqualValues(t, []int{0, 1, 2, 4, 5, 6}, bm.ToSortedSlice())
	assert.EqualValues(t, 6, bm.Len())
}

func TestRemoveRange(t *testing.T) {
	var bm Bitmap
	bm.AddRange(3, 12)
	assert.EqualValues(t, 9, bm.Len())
	bm.RemoveRange(14, -1)
	assert.EqualValues(t, 9, bm.Len())
	bm.RemoveRange(2, 5)
	assert.EqualValues(t, 7, bm.Len())
	bm.RemoveRange(10, -1)
	assert.EqualValues(t, 5, bm.Len())
}

func TestLimits(t *testing.T) {
	var bm Bitmap
	assert.Panics(t, func() { bm.Add(math.MaxInt64) })
	bm.Add(-1)
	assert.EqualValues(t, 1, bm.Len())
	assert.EqualValues(t, []int{MaxInt}, bm.ToSortedSlice())
}

func TestRoaringRangeEnd(t *testing.T) {
	r := roaring.New()
	r.Add(roaring.MaxUint32)
	require.EqualValues(t, 1, r.GetCardinality())
	r.RemoveRange(0, roaring.MaxUint32)
	assert.EqualValues(t, 1, r.GetCardinality())
	r.RemoveRange(0, math.MaxUint64)
	assert.EqualValues(t, 1, r.GetCardinality())
	r.RemoveRange(0, 0x100000001)
	assert.EqualValues(t, 1, r.GetCardinality())
	r.RemoveRange(0, 0x100000000)
	assert.EqualValues(t, 0, r.GetCardinality())
}
<commit_msg>Fix test now that roaring master handles RemoveRange uint64 end values<commit_after>package bitmap

import (
	"math"
	"testing"

	"github.com/RoaringBitmap/roaring"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"github.com/anacrolix/missinggo/iter"
	"github.com/anacrolix/missinggo/slices"
)

func TestEmptyBitmap(t *testing.T) {
	var bm Bitmap
	assert.False(t, bm.Contains(0))
	bm.Remove(0)
	it := iter.NewIterator(&bm)
	assert.Panics(t, func() { it.Value() })
	assert.False(t, it.Next())
}

func bitmapSlice(bm *Bitmap) (ret []int) {
	sl := iter.IterableAsSlice(bm)
	slices.MakeInto(&ret, sl)
	return
}

func TestSimpleBitmap(t *testing.T) {
	bm := new(Bitmap)
	assert.EqualValues(t, []int(nil), bitmapSlice(bm))
	bm.Add(0)
	assert.True(t, bm.Contains(0))
	assert.False(t, bm.Contains(1))
	assert.EqualValues(t, 1, bm.Len())
	bm.Add(3)
	assert.True(t, bm.Contains(0))
	assert.True(t, bm.Contains(3))
	assert.EqualValues(t, []int{0, 3}, bitmapSlice(bm))
	assert.EqualValues(t, 2, bm.Len())
	bm.Remove(0)
	assert.EqualValues(t, []int{3}, bitmapSlice(bm))
	assert.EqualValues(t, 1, bm.Len())
}

func TestSub(t *testing.T) {
	var left, right Bitmap
	left.Add(2, 5, 4)
	right.Add(3, 2, 6)
	assert.Equal(t, []int{4, 5}, Sub(left, right).ToSortedSlice())
	assert.Equal(t, []int{3, 6}, Sub(right, left).ToSortedSlice())
}

func TestSubUninited(t *testing.T) {
	var left, right Bitmap
	assert.EqualValues(t, []int(nil), Sub(left, right).ToSortedSlice())
}

func TestAddRange(t *testing.T) {
	var bm Bitmap
	bm.AddRange(21, 26)
	bm.AddRange(9, 14)
	bm.AddRange(11, 16)
	bm.Remove(12)
	assert.EqualValues(t, []int{9, 10, 11, 13, 14, 15, 21, 22, 23, 24, 25}, bm.ToSortedSlice())
	assert.EqualValues(t, 11, bm.Len())
	bm.Clear()
	bm.AddRange(3, 7)
	bm.AddRange(0, 3)
	bm.AddRange(2, 4)
	bm.Remove(3)
	assert.EqualValues(t, []int{0, 1, 2, 4, 5, 6}, bm.ToSortedSlice())
	assert.EqualValues(t, 6, bm.Len())
}

func TestRemoveRange(t *testing.T) {
	var bm Bitmap
	bm.AddRange(3, 12)
	assert.EqualValues(t, 9, bm.Len())
	bm.RemoveRange(14, -1)
	assert.EqualValues(t, 9, bm.Len())
	bm.RemoveRange(2, 5)
	assert.EqualValues(t, 7, bm.Len())
	bm.RemoveRange(10, -1)
	assert.EqualValues(t, 5, bm.Len())
}

func TestLimits(t *testing.T) {
	var bm Bitmap
	assert.Panics(t, func() { bm.Add(math.MaxInt64) })
	bm.Add(-1)
	assert.EqualValues(t, 1, bm.Len())
	assert.EqualValues(t, []int{MaxInt}, bm.ToSortedSlice())
}

func TestRoaringRangeEnd(t *testing.T) {
	r := roaring.New()
	r.Add(roaring.MaxUint32)
	require.EqualValues(t, 1, r.GetCardinality())
	r.RemoveRange(0, roaring.MaxUint32)
	assert.EqualValues(t, 1, r.GetCardinality())
	r.RemoveRange(0, math.MaxUint64)
	assert.EqualValues(t, 0, r.GetCardinality())
}
<|endoftext|>


<commit_before>package command

import (
	"flag"
	"fmt"
	"net"
	"sort"
	"strings"
	"sync"
	"time"

	"github.com/Shopify/sarama"
	"github.com/funkygao/gafka/ctx"
	"github.com/funkygao/gafka/zk"
	"github.com/funkygao/gocli"
	"github.com/funkygao/termui"
)

type TopBroker struct {
	Ui  cli.Ui
	Cmd string

	mu sync.Mutex

	zone, cluster, topic string
	drawMode             bool
	interval             time.Duration
	shortIp              bool

	offsets     map[string]int64 // host => offset sum
	lastOffsets map[string]int64
}

func (this *TopBroker) Run(args []string) (exitCode int) {
	cmdFlags := flag.NewFlagSet("topbroker", flag.ContinueOnError)
	cmdFlags.Usage = func() { this.Ui.Output(this.Help()) }
	cmdFlags.StringVar(&this.zone, "z", ctx.ZkDefaultZone(), "")
	cmdFlags.StringVar(&this.cluster, "c", "", "")
	cmdFlags.StringVar(&this.topic, "t", "", "")
	cmdFlags.BoolVar(&this.drawMode, "d", false, "")
	cmdFlags.BoolVar(&this.shortIp, "shortip", false, "")
	cmdFlags.DurationVar(&this.interval, "i", time.Second*5, "refresh interval")

	if err := cmdFlags.Parse(args); err != nil {
		return 1
	}

	this.offsets = make(map[string]int64)
	this.lastOffsets = make(map[string]int64)

	if this.interval.Seconds() < 1 {
		this.interval = time.Second
	}

	zkzone := zk.NewZkZone(zk.DefaultConfig(this.zone, ctx.ZoneZkAddrs(this.zone)))
	zkzone.ForSortedClusters(func(zkcluster *zk.ZkCluster) {
		if !patternMatched(zkcluster.Name(), this.cluster) {
			return
		}

		go this.clusterTopProducers(zkcluster)
	})

	if this.drawMode {
		this.drawDashboard()
		return
	}

	ticker := time.NewTicker(this.interval)
	defer ticker.Stop()

	for {
		select {
		case <-ticker.C:
			refreshScreen()
			this.showAndResetCounters(true)

		}
	}

	return
}

func (this *TopBroker) drawDashboard() {
	ticker := time.NewTicker(this.interval)
	defer ticker.Stop()

	termui.Init()
	width := termui.TermWidth()
	termui.Close()
	maxWidth := width - 23
	var maxQps float64
	for {
		select {
		case <-ticker.C:
			refreshScreen()
			datas := this.showAndResetCounters(false)
			maxQps = .0
			for _, data := range datas {
				if data.qps > maxQps {
					maxQps = data.qps
				}
			}

			if maxQps < 1 {
				continue
			}

			for _, data := range datas {
				if data.qps < 0 {
					data.qps = -data.qps // FIXME
				}
				w := int(data.qps*100/maxQps) * maxWidth / 100
				this.Ui.Output(fmt.Sprintf("%20s [%-118s]",
					data.host,
					strings.Repeat("|", w)))
			}

		}
	}

}

type brokerQps struct {
	host string
	qps  float64
}

func (this *TopBroker) showAndResetCounters(show bool) []brokerQps {
	this.mu.Lock()
	defer this.mu.Unlock()

	d := this.interval.Seconds()
	sortedHost := make([]string, 0, len(this.offsets))
	for host, _ := range this.offsets {
		sortedHost = append(sortedHost, host)
	}
	sort.Strings(sortedHost)

	if show {
		this.Ui.Output(fmt.Sprintf("%20s %8s", "host", "mps"))
	}

	totalQps := .0
	r := make([]brokerQps, 0, len(sortedHost))
	for _, host := range sortedHost {
		offset := this.offsets[host]
		qps := float64(0)
		if lastOffset, present := this.lastOffsets[host]; present {
			qps = float64(offset-lastOffset) / d
		}

		r = append(r, brokerQps{host, qps})
		totalQps += qps

		if show {
			this.Ui.Output(fmt.Sprintf("%20s %10.1f", host, qps))
		}
	}

	if show {
		this.Ui.Output(fmt.Sprintf("%20s %10.1f", "-TOTAL-", totalQps))
	}

	for host, offset := range this.offsets {
		this.lastOffsets[host] = offset
	}
	this.offsets = make(map[string]int64)

	return r
}

func (this *TopBroker) clusterTopProducers(zkcluster *zk.ZkCluster) {
	kfk, err := sarama.NewClient(zkcluster.BrokerList(), sarama.NewConfig())
	if err != nil {
		return
	}
	defer kfk.Close()

	for {
		topics, err := kfk.Topics()
		swallow(err)

		for _, topic := range topics {
			if !patternMatched(topic, this.topic) {
				continue
			}

			partions, err := kfk.WritablePartitions(topic)
			swallow(err)
			for _, partitionID := range partions {
				leader, err := kfk.Leader(topic, partitionID)
				swallow(err)

				latestOffset, err := kfk.GetOffset(topic, partitionID,
					sarama.OffsetNewest)
				if err != nil {
					panic(err)
				}

				host, _, err := net.SplitHostPort(leader.Addr())
				swallow(err)

				if this.shortIp {
					host = shortIp(host)
				}

				this.mu.Lock()
				if _, present := this.offsets[host]; !present {
					this.offsets[host] = 0
				}
				this.offsets[host] += latestOffset
				this.mu.Unlock()
			}
		}

		time.Sleep(time.Second)
		kfk.RefreshMetadata(topics...)
	}
}

func (*TopBroker) Synopsis() string {
	return "Unix “top” like utility for kafka brokers"
}

func (this *TopBroker) Help() string {
	help := fmt.Sprintf(`
Usage: %s topbroker [options]

    Unix “top” like utility for kafka brokers

Options:

    -z zone
      Default %s

    -c cluster pattern

    -t topic pattern

    -i interval
      Refresh interval in seconds.
      e,g. 5s

    -d
      Draw dashboard in graph.

    -shortip

`, this.Cmd, ctx.ZkDefaultZone())
	return strings.TrimSpace(help)
}
<commit_msg>add color for the qps bar<commit_after>package command

import (
	"flag"
	"fmt"
	"net"
	"sort"
	"strings"
	"sync"
	"time"

	"github.com/Shopify/sarama"
	"github.com/funkygao/gafka/ctx"
	"github.com/funkygao/gafka/zk"
	"github.com/funkygao/gocli"
	"github.com/funkygao/golib/color"
	"github.com/funkygao/termui"
)

type TopBroker struct {
	Ui  cli.Ui
	Cmd string

	mu sync.Mutex

	zone, cluster, topic string
	drawMode             bool
	interval             time.Duration
	shortIp              bool

	offsets     map[string]int64 // host => offset sum
	lastOffsets map[string]int64
}

func (this *TopBroker) Run(args []string) (exitCode int) {
	cmdFlags := flag.NewFlagSet("topbroker", flag.ContinueOnError)
	cmdFlags.Usage = func() { this.Ui.Output(this.Help()) }
	cmdFlags.StringVar(&this.zone, "z", ctx.ZkDefaultZone(), "")
	cmdFlags.StringVar(&this.cluster, "c", "", "")
	cmdFlags.StringVar(&this.topic, "t", "", "")
	cmdFlags.BoolVar(&this.drawMode, "d", false, "")
	cmdFlags.BoolVar(&this.shortIp, "shortip", false, "")
	cmdFlags.DurationVar(&this.interval, "i", time.Second*5, "refresh interval")

	if err := cmdFlags.Parse(args); err != nil {
		return 1
	}

	this.offsets = make(map[string]int64)
	this.lastOffsets = make(map[string]int64)

	if this.interval.Seconds() < 1 {
		this.interval = time.Second
	}

	zkzone := zk.NewZkZone(zk.DefaultConfig(this.zone, ctx.ZoneZkAddrs(this.zone)))
	zkzone.ForSortedClusters(func(zkcluster *zk.ZkCluster) {
		if !patternMatched(zkcluster.Name(), this.cluster) {
			return
		}

		go this.clusterTopProducers(zkcluster)
	})

	if this.drawMode {
		this.drawDashboard()
		return
	}

	ticker := time.NewTicker(this.interval)
	defer ticker.Stop()

	for {
		select {
		case <-ticker.C:
			refreshScreen()
			this.showAndResetCounters(true)

		}
	}

	return
}

func (this *TopBroker) drawDashboard() {
	ticker := time.NewTicker(this.interval)
	defer ticker.Stop()

	termui.Init()
	width := termui.TermWidth()
	termui.Close()
	maxWidth := width - 23
	var maxQps float64
	for {
		select {
		case <-ticker.C:
			refreshScreen()
			datas := this.showAndResetCounters(false)
			maxQps = .0
			for _, data := range datas {
				if data.qps > maxQps {
					maxQps = data.qps
				}
			}

			if maxQps < 1 {
				continue
			}

			for _, data := range datas {
				if data.qps < 0 {
					data.qps = -data.qps // FIXME
				}

				w := int(data.qps*100/maxQps) * maxWidth / 100
				qps := fmt.Sprintf("%.1f", data.qps)
				bar := ""
				barColorLen := 0
				for i := 0; i < w-len(qps); i++ {
					bar += color.Green("|")
					barColorLen += 9 // color.Green will add extra 9 chars
				}
				bar += qps
				for i := len(bar) - barColorLen; i < maxWidth; i++ {
					bar += " "
				}

				this.Ui.Output(fmt.Sprintf("%20s [%-s]", data.host, bar))
			}

		}
	}

}

type brokerQps struct {
	host string
	qps  float64
}

func (this *TopBroker) showAndResetCounters(show bool) []brokerQps {
	this.mu.Lock()
	defer this.mu.Unlock()

	d := this.interval.Seconds()
	sortedHost := make([]string, 0, len(this.offsets))
	for host, _ := range this.offsets {
		sortedHost = append(sortedHost, host)
	}
	sort.Strings(sortedHost)

	if show {
		this.Ui.Output(fmt.Sprintf("%20s %8s", "host", "mps"))
	}

	totalQps := .0
	r := make([]brokerQps, 0, len(sortedHost))
	for _, host := range sortedHost {
		offset := this.offsets[host]
		qps := float64(0)
		if lastOffset, present := this.lastOffsets[host]; present {
			qps = float64(offset-lastOffset) / d
		}

		r = append(r, brokerQps{host, qps})
		totalQps += qps

		if show {
			this.Ui.Output(fmt.Sprintf("%20s %10.1f", host, qps))
		}
	}

	if show {
		this.Ui.Output(fmt.Sprintf("%20s %10.1f", "-TOTAL-", totalQps))
	}

	for host, offset := range this.offsets {
		this.lastOffsets[host] = offset
	}
	this.offsets = make(map[string]int64)

	return r
}

func (this *TopBroker) clusterTopProducers(zkcluster *zk.ZkCluster) {
	kfk, err := sarama.NewClient(zkcluster.BrokerList(), sarama.NewConfig())
	if err != nil {
		return
	}
	defer kfk.Close()

	for {
		topics, err := kfk.Topics()
		swallow(err)

		for _, topic := range topics {
			if !patternMatched(topic, this.topic) {
				continue
			}

			partions, err := kfk.WritablePartitions(topic)
			swallow(err)
			for _, partitionID := range partions {
				leader, err := kfk.Leader(topic, partitionID)
				swallow(err)

				latestOffset, err := kfk.GetOffset(topic, partitionID,
					sarama.OffsetNewest)
				if err != nil {
					panic(err)
				}

				host, _, err := net.SplitHostPort(leader.Addr())
				swallow(err)

				if this.shortIp {
					host = shortIp(host)
				}

				this.mu.Lock()
				if _, present := this.offsets[host]; !present {
					this.offsets[host] = 0
				}
				this.offsets[host] += latestOffset
				this.mu.Unlock()
			}
		}

		time.Sleep(time.Second)
		kfk.RefreshMetadata(topics...)
	}
}

func (*TopBroker) Synopsis() string {
	return "Unix “top” like utility for kafka brokers"
}

func (this *TopBroker) Help() string {
	help := fmt.Sprintf(`
Usage: %s topbroker [options]

    Unix “top” like utility for kafka brokers

Options:

    -z zone
      Default %s

    -c cluster pattern

    -t topic pattern

    -i interval
      Refresh interval in seconds.
      e,g. 5s

    -d
      Draw dashboard in graph.

    -shortip

`, this.Cmd, ctx.ZkDefaultZone())
	return strings.TrimSpace(help)
}
<|endoftext|>


<commit_before>package engine

// todo unit test me
import (
	"fmt"
	"github.com/julienmoumne/hotshell/cmd/hs/formatter"
	"github.com/julienmoumne/hotshell/cmd/hs/item"
	"github.com/julienmoumne/hotshell/cmd/term"
	"os"
	"os/signal"
)

type controller struct {
	activeItem       *item.Item
	lastActivatedCmd item.Key
	term             term.Term
	activeSubprocess bool
}

func (c *controller) Start(root *item.Item) (bool, error) {
	if err := c.initTerm(); err != nil {
		return false, err
	}
	defer func() {
		if err := c.term.Close(); err != nil {
			fmt.Println(err)
		}
	}()

	c.initSignals()
	defer c.resetSignals()

	fmt.Print("\n")
	c.activeItem = item.Activate(root)
	return c.mainLoop()
}

func (c *controller) initTerm() (err error) {
	c.term, err = term.NewTerm()
	return
}

func (c *controller) mainLoop() (bool, error) {
	c.printPrompt()
	for {
		key, err := c.term.ReadUserChoice()
		if err != nil {
			return false, err
		}

		switch key {

		case item.EotKey:
			fmt.Print("\n")
			return false, nil
		case item.PreviousMenuKey:
			if c.activeItem.Parent != nil {
				c.triggerItem(key, c.activeItem.Parent)
			}
		case item.BashKey:
			c.triggerItem(key, item.BashCmd)
		case item.RepeatKey:
			c.triggerLastCmd()
		case item.ReloadKey:
			c.printKey(item.ReloadKey)
			return true, nil
		default:
			if selectedItem, err := c.activeItem.GetItem(key); err == nil {
				c.triggerItem(key, selectedItem)
			}
		}
	}
}

func (c *controller) printPrompt() {
	fmt.Print(formatter.KeyActivatedFmt(" ? "))
}

func (c *controller) triggerLastCmd() {
	if c.lastActivatedCmd == (item.Key{}) {
		return
	}

	var it *item.Item
	if c.lastActivatedCmd == item.BashKey {
		it = item.BashCmd
	} else {
		it, _ = c.activeItem.GetItem(c.lastActivatedCmd)
	}

	c.triggerItem(c.lastActivatedCmd, it)
}

func (c *controller) printKey(key item.Key) {
	fmt.Print(formatter.KeyActivatedFmt("%v\n\n", key.String()))
}

func (c *controller) triggerItem(key item.Key, it *item.Item) {
	c.activeSubprocess = true
	defer func() {
		c.activeSubprocess = false
	}()

	c.printKey(key)

	nextMenu := item.Activate(it)

	bashModeActivated := nextMenu == nil
	cmdActivated := nextMenu == c.activeItem
	if bashModeActivated || cmdActivated {
		c.lastActivatedCmd = key
		item.Activate(c.activeItem)
	} else {
		c.lastActivatedCmd = item.Key{}
		c.activeItem = nextMenu
	}

	c.printPrompt()
}

func (c *controller) initSignals() {
	channel := make(chan os.Signal, 1)
	signal.Notify(channel, os.Interrupt)
	go func() {
		for _ = range channel {
			if c.activeSubprocess {
				continue
			}
			c.term.Restore()
			fmt.Print("\n")
			os.Exit(0)
		}
	}()
}

func (c *controller) resetSignals() {
	signal.Reset()
}
<commit_msg>doc(term): document weird behavior with EOT code<commit_after>package engine

// todo unit test me
import (
	"fmt"
	"github.com/julienmoumne/hotshell/cmd/hs/formatter"
	"github.com/julienmoumne/hotshell/cmd/hs/item"
	"github.com/julienmoumne/hotshell/cmd/term"
	"os"
	"os/signal"
)

type controller struct {
	activeItem       *item.Item
	lastActivatedCmd item.Key
	term             term.Term
	activeSubprocess bool
}

func (c *controller) Start(root *item.Item) (bool, error) {
	if err := c.initTerm(); err != nil {
		return false, err
	}
	defer func() {
		if err := c.term.Close(); err != nil {
			fmt.Println(err)
		}
	}()

	c.initSignals()
	defer c.resetSignals()

	fmt.Print("\n")
	c.activeItem = item.Activate(root)
	return c.mainLoop()
}

func (c *controller) initTerm() (err error) {
	c.term, err = term.NewTerm()
	return
}

func (c *controller) mainLoop() (bool, error) {
	c.printPrompt()
	for {
		key, err := c.term.ReadUserChoice()
		if err != nil {
			return false, err
		}

		switch key {

		case item.EotKey:
			fmt.Print("\n")
			return false, nil
		case item.PreviousMenuKey:
			if c.activeItem.Parent != nil {
				c.triggerItem(key, c.activeItem.Parent)
			}
		case item.BashKey:
			c.triggerItem(key, item.BashCmd)
		case item.RepeatKey:
			c.triggerLastCmd()
		case item.ReloadKey:
			c.printKey(item.ReloadKey)
			return true, nil
		default:
			if selectedItem, err := c.activeItem.GetItem(key); err == nil {
				c.triggerItem(key, selectedItem)
			}
		}
	}
}

func (c *controller) printPrompt() {
	fmt.Print(formatter.KeyActivatedFmt(" ? "))
}

func (c *controller) triggerLastCmd() {
	if c.lastActivatedCmd == (item.Key{}) {
		return
	}

	var it *item.Item
	if c.lastActivatedCmd == item.BashKey {
		it = item.BashCmd
	} else {
		it, _ = c.activeItem.GetItem(c.lastActivatedCmd)
	}

	c.triggerItem(c.lastActivatedCmd, it)
}

func (c *controller) printKey(key item.Key) {
	fmt.Print(formatter.KeyActivatedFmt("%v\n\n", key.String()))
}

func (c *controller) triggerItem(key item.Key, it *item.Item) {
	c.activeSubprocess = true
	defer func() {
		c.activeSubprocess = false
	}()

	c.printKey(key)

	nextMenu := item.Activate(it)

	menulessCmdActivated := nextMenu == nil
	cmdActivated := nextMenu == c.activeItem
	if menulessCmdActivated || cmdActivated {
		c.lastActivatedCmd = key
		item.Activate(c.activeItem)
	} else {
		c.lastActivatedCmd = item.Key{}
		c.activeItem = nextMenu
	}

	c.printPrompt()
}

func (c *controller) initSignals() {
	channel := make(chan os.Signal, 1)
	signal.Notify(channel, os.Interrupt)
	go func() {
		for _ = range channel {
			if c.activeSubprocess {
				continue
			}
			c.term.Restore()
			fmt.Print("\n")
			os.Exit(0)
		}
	}()
}

func (c *controller) resetSignals() {
	signal.Reset()
}
<|endoftext|>


<commit_before>/*
Copyright 2016 The Kubernetes Authors.

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 cmd

import (
	"fmt"
	"io"

	"github.com/renstrom/dedent"
	"github.com/spf13/cobra"

	kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
	kubenode "k8s.io/kubernetes/cmd/kubeadm/app/node"
	"k8s.io/kubernetes/cmd/kubeadm/app/preflight"
	kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
)

var (
	joinDoneMsgf = dedent.Dedent(`
		Node join complete:
		* Certificate signing request sent to master and response
		  received.
		* Kubelet informed of new secure connection details.

		Run 'kubectl get nodes' on the master to see this machine join.
		`)
)

// NewCmdJoin returns "kubeadm join" command.
func NewCmdJoin(out io.Writer) *cobra.Command {
	cfg := &kubeadmapi.NodeConfiguration{}
	var skipPreFlight bool
	cmd := &cobra.Command{
		Use:   "join",
		Short: "Run this on any machine you wish to join an existing cluster.",
		Run: func(cmd *cobra.Command, args []string) {
			err := RunJoin(out, cmd, args, cfg, skipPreFlight)
			kubeadmutil.CheckErr(err)
		},
	}

	cmd.PersistentFlags().StringVar(
		&cfg.Secrets.GivenToken, "token", "",
		"(required) Shared secret used to secure bootstrap. Must match the output of 'kubeadm init'",
	)

	cmd.PersistentFlags().BoolVar(
		&skipPreFlight, "skip-preflight-checks", false,
		"skip preflight checks normally run before modifying the system",
	)

	return cmd
}

// RunJoin executes worked node provisioning and tries to join an existing cluster.
func RunJoin(out io.Writer, cmd *cobra.Command, args []string, s *kubeadmapi.NodeConfiguration, skipPreFlight bool) error {
	// TODO(phase1+) this we are missing args from the help text, there should be a way to tell cobra about it
	if !skipPreFlight {
		fmt.Println("Running pre-flight checks")
		err := preflight.RunJoinNodeChecks()
		if err != nil {
			return &preflight.PreFlightError{Msg: err.Error()}
		}
	} else {
		fmt.Println("Skipping pre-flight checks")
	}

	if len(args) == 0 {
		return fmt.Errorf("<cmd/join> must specify master IP address (see --help)")
	}
	s.MasterAddresses = append(s.MasterAddresses, args...)

	ok, err := kubeadmutil.UseGivenTokenIfValid(&s.Secrets)
	if !ok {
		if err != nil {
			return fmt.Errorf("<cmd/join> %v (see --help)\n", err)
		}
		return fmt.Errorf("Must specify --token (see --help)\n")
	}

	clusterInfo, err := kubenode.RetrieveTrustedClusterInfo(s)
	if err != nil {
		return err
	}

	connectionDetails, err := kubenode.EstablishMasterConnection(s, clusterInfo)
	if err != nil {
		return err
	}

	kubeconfig, err := kubenode.PerformTLSBootstrap(connectionDetails)
	if err != nil {
		return err
	}

	err = kubeadmutil.WriteKubeconfigIfNotExists("kubelet", kubeconfig)
	if err != nil {
		return err
	}

	fmt.Fprintf(out, joinDoneMsgf)
	return nil
}
<commit_msg>kubeadm join: Added support for config file.<commit_after>/*
Copyright 2016 The Kubernetes Authors.

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 cmd

import (
	"fmt"
	"io"
	"io/ioutil"

	"github.com/renstrom/dedent"
	"github.com/spf13/cobra"

	kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
	kubenode "k8s.io/kubernetes/cmd/kubeadm/app/node"
	"k8s.io/kubernetes/cmd/kubeadm/app/preflight"
	kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
	"k8s.io/kubernetes/pkg/api"
	"k8s.io/kubernetes/pkg/runtime"
)

var (
	joinDoneMsgf = dedent.Dedent(`
		Node join complete:
		* Certificate signing request sent to master and response
		  received.
		* Kubelet informed of new secure connection details.

		Run 'kubectl get nodes' on the master to see this machine join.
		`)
)

// NewCmdJoin returns "kubeadm join" command.
func NewCmdJoin(out io.Writer) *cobra.Command {
	cfg := &kubeadmapi.NodeConfiguration{}
	var skipPreFlight bool
	var cfgPath string
	cmd := &cobra.Command{
		Use:   "join",
		Short: "Run this on any machine you wish to join an existing cluster.",
		Run: func(cmd *cobra.Command, args []string) {
			j, err := NewJoin(cfgPath, args, cfg, skipPreFlight)
			kubeadmutil.CheckErr(err)
			kubeadmutil.CheckErr(j.Run(out))
		},
	}

	cmd.PersistentFlags().StringVar(
		&cfg.Secrets.GivenToken, "token", "",
		"(required) Shared secret used to secure bootstrap. Must match the output of 'kubeadm init'",
	)

	cmd.PersistentFlags().StringVar(&cfgPath, "config", "", "Path to kubeadm config file")

	cmd.PersistentFlags().BoolVar(
		&skipPreFlight, "skip-preflight-checks", false,
		"skip preflight checks normally run before modifying the system",
	)

	return cmd
}

type Join struct {
	cfg *kubeadmapi.NodeConfiguration
}

func NewJoin(cfgPath string, args []string, cfg *kubeadmapi.NodeConfiguration, skipPreFlight bool) (*Join, error) {
	if cfgPath != "" {
		b, err := ioutil.ReadFile(cfgPath)
		if err != nil {
			return nil, fmt.Errorf("unable to read config from %q [%v]", cfgPath, err)
		}
		if err := runtime.DecodeInto(api.Codecs.UniversalDecoder(), b, cfg); err != nil {
			return nil, fmt.Errorf("unable to decode config from %q [%v]", cfgPath, err)
		}
	}

	if !skipPreFlight {
		fmt.Println("Running pre-flight checks")
		err := preflight.RunJoinNodeChecks()
		if err != nil {
			return nil, &preflight.PreFlightError{Msg: err.Error()}
		}
	} else {
		fmt.Println("Skipping pre-flight checks")
	}

	// TODO(phase1+) this we are missing args from the help text, there should be a way to tell cobra about it
	if len(args) == 0 {
		return nil, fmt.Errorf("must specify master IP address (see --help)")
	}
	cfg.MasterAddresses = append(cfg.MasterAddresses, args...)

	ok, err := kubeadmutil.UseGivenTokenIfValid(&cfg.Secrets)
	if !ok {
		if err != nil {
			return nil, fmt.Errorf("%v (see --help)\n", err)
		}
		return nil, fmt.Errorf("Must specify --token (see --help)\n")
	}

	return &Join{cfg: cfg}, nil
}

// Run executes worked node provisioning and tries to join an existing cluster.
func (j *Join) Run(out io.Writer) error {
	clusterInfo, err := kubenode.RetrieveTrustedClusterInfo(j.cfg)
	if err != nil {
		return err
	}

	connectionDetails, err := kubenode.EstablishMasterConnection(j.cfg, clusterInfo)
	if err != nil {
		return err
	}

	kubeconfig, err := kubenode.PerformTLSBootstrap(connectionDetails)
	if err != nil {
		return err
	}

	err = kubeadmutil.WriteKubeconfigIfNotExists("kubelet", kubeconfig)
	if err != nil {
		return err
	}

	fmt.Fprintf(out, joinDoneMsgf)
	return nil
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"log"
	"os"
	"os/signal"
	"time"

	"git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golang.git"
)

// コマンドラインフラグの初期値
const (
	defHost     = "test.mosquitto.org"
	defPort     = "1883"
	defClientID = "mqtt-rp-pir-sub"
	defUsername = ""
	defPassword = ""
	defTopic    = "mqtt-rp-pir"
	defQoS      = 0
)

// MQTTブローカ切断時の待ち時間(ms)
const quiesce = 1000

// 割り込みシグナルのチェック間隔(ms)
const checkInterval = 1000

// handleはメッセージ受信時の処理を実施する。
func handle(_ *mqtt.MqttClient, msg mqtt.Message) {
	log.Println(msg.Payload())
}

func main() {
	// シグナル通知設定
	chSig := make(chan os.Signal, 1)
	signal.Notify(chSig, os.Interrupt, os.Kill)

	// コマンドラインフラグのパース
	host := flag.String("h", defHost, "MQTTブローカサーバのホスト名")
	port := flag.String("p", defPort, "MQTTブローカサーバのポート番号")
	clientID := flag.String("c", defClientID, "クライアントID")
	username := flag.String("u", defUsername, "認証ユーザ名")
	password := flag.String("pw", defPassword, "認証パスワード")
	topic := flag.String("t", defTopic, "トピック")
	qos := flag.Int("q", defQoS, "QoS")

	flag.Parse()

	// MQTTクライアントの作成
	opts := mqtt.NewClientOptions()
	opts.AddBroker("tcp://" + *host + ":" + *port)
	opts.SetClientId(*clientID)
	if *username != "" && *password != "" {
		opts.SetUsername(*username)
		opts.SetPassword(*password)
	}

	cli := mqtt.NewClient(opts)

	// ブローカサーバへの接続
	log.Println("ブローカサーバへ接続しています...")
	if _, err := cli.Start(); err != nil {
		panic(err)
	}

	defer func() {
		log.Println("ブローカサーバから切断しています...")
		cli.Disconnect(quiesce)
		log.Println("ブローカサーバから切断しました。")
	}()

	log.Println("ブローカサーバへ接続しました。")

	// フィルタの設定
	filter, err := mqtt.NewTopicFilter(*topic, byte(*qos))
	if err != nil {
		panic(err)
	}

	// Subscriptionの開始
	log.Println("Subscriptionを開始しています...")
	if _, err := cli.StartSubscription(handle, filter); err != nil {
		panic(err)
	}

	defer func() {
		// Subscriptionの終了
		log.Println("Subscriptionを終了しています...")
		receipt, err := cli.EndSubscription(*topic)
		if err != nil {
			panic(err)
		}

		// Subscriptionの終了完了を待つ
		<-receipt
		log.Println("Subscriptionを終了しました。")
	}()

	log.Println("Subscriptionを開始しました。")

	// 割り込み発生まで待つ
WaitLoop:
	for {
		select {
		case <-chSig:
			break WaitLoop
		default:
			time.Sleep(checkInterval * time.Millisecond)
		}
	}
}
<commit_msg>Update cmd/mqtt-rp-pir-sub/main.go<commit_after>package main

import (
	"flag"
	"log"
	"os"
	"os/signal"
	"time"

	"git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golang.git"
)

// コマンドラインフラグの初期値
const (
	defHost     = "test.mosquitto.org"
	defPort     = "1883"
	defClientID = "mqtt-rp-pir-sub"
	defUsername = ""
	defPassword = ""
	defTopic    = "mqtt-rp-pir"
	defQoS      = 0
)

// MQTTブローカ切断時の待ち時間(ms)
const quiesce = 1000

// 割り込みシグナルのチェック間隔(ms)
const checkInterval = 1000

// handleはメッセージ受信時の処理を実施する。
func handle(_ *mqtt.MqttClient, msg mqtt.Message) {
	log.Println(string(msg.Payload()))
}

func main() {
	// シグナル通知設定
	chSig := make(chan os.Signal, 1)
	signal.Notify(chSig, os.Interrupt, os.Kill)

	// コマンドラインフラグのパース
	host := flag.String("h", defHost, "MQTTブローカサーバのホスト名")
	port := flag.String("p", defPort, "MQTTブローカサーバのポート番号")
	clientID := flag.String("c", defClientID, "クライアントID")
	username := flag.String("u", defUsername, "認証ユーザ名")
	password := flag.String("pw", defPassword, "認証パスワード")
	topic := flag.String("t", defTopic, "トピック")
	qos := flag.Int("q", defQoS, "QoS")

	flag.Parse()

	// MQTTクライアントの作成
	opts := mqtt.NewClientOptions()
	opts.AddBroker("tcp://" + *host + ":" + *port)
	opts.SetClientId(*clientID)
	if *username != "" && *password != "" {
		opts.SetUsername(*username)
		opts.SetPassword(*password)
	}

	cli := mqtt.NewClient(opts)

	// ブローカサーバへの接続
	log.Println("ブローカサーバへ接続しています...")
	if _, err := cli.Start(); err != nil {
		panic(err)
	}

	defer func() {
		log.Println("ブローカサーバから切断しています...")
		cli.Disconnect(quiesce)
		log.Println("ブローカサーバから切断しました。")
	}()

	log.Println("ブローカサーバへ接続しました。")

	// フィルタの設定
	filter, err := mqtt.NewTopicFilter(*topic, byte(*qos))
	if err != nil {
		panic(err)
	}

	// Subscriptionの開始
	log.Println("Subscriptionを開始しています...")
	if _, err := cli.StartSubscription(handle, filter); err != nil {
		panic(err)
	}

	defer func() {
		// Subscriptionの終了
		log.Println("Subscriptionを終了しています...")
		receipt, err := cli.EndSubscription(*topic)
		if err != nil {
			panic(err)
		}

		// Subscriptionの終了完了を待つ
		<-receipt
		log.Println("Subscriptionを終了しました。")
	}()

	log.Println("Subscriptionを開始しました。")

	// 割り込み発生まで待つ
WaitLoop:
	for {
		select {
		case <-chSig:
			break WaitLoop
		default:
			time.Sleep(checkInterval * time.Millisecond)
		}
	}
}
<|endoftext|>


<commit_before>package eden

import (
	"log"
	"net/http"

	"github.com/julienschmidt/httprouter"
)

// New returns a new *router to be used to configure the endpoints
func New() *Router {
	r := Router{}
	r.router = httprouter.New()
	r.middleware = nil

	return &r
}

// ServeHTTP calls the httprouter.ServeHTTP and implements router as an http.Handler
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	r.router.ServeHTTP(w, req)
}

// Run runs the server listening on the given address
func (r *Router) Run(address string) error {
	log.Printf("Starting server on %s", address)
	if err := http.ListenAndServe(address, r); err != nil {
		return err
	}

	return nil
}

// RunTLS runs a TLS server listening on the given address with the given cert and key
func (r *Router) RunTLS(address string, cert string, key string) error {
	if err := http.ListenAndServeTLS(address, cert, key, r); err != nil {
		return err
	}
	return nil
}

/*
 * TODO: So now there is a lot to be done here, I have the context pretty much
 * working I think, but there is more to do there. The context also needs to
 * be able to be created from the beginning. There should also proabably be an error
 * handling middleware for logging the issues. As well as for logging
 * response times.
 */
<commit_msg>Added log to TLS server starting as well<commit_after>package eden

import (
	"log"
	"net/http"

	"github.com/julienschmidt/httprouter"
)

// New returns a new *router to be used to configure the endpoints
func New() *Router {
	r := Router{}
	r.router = httprouter.New()
	r.middleware = nil

	return &r
}

// ServeHTTP calls the httprouter.ServeHTTP and implements router as an http.Handler
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	r.router.ServeHTTP(w, req)
}

// Run runs the server listening on the given address
func (r *Router) Run(address string) error {
	log.Printf("Starting server on %s", address)
	if err := http.ListenAndServe(address, r); err != nil {
		return err
	}

	return nil
}

// RunTLS runs a TLS server listening on the given address with the given cert and key
func (r *Router) RunTLS(address string, cert string, key string) error {
	log.Printf("Starting TLS server on %s", address)
	if err := http.ListenAndServeTLS(address, cert, key, r); err != nil {
		return err
	}
	return nil
}

/*
 * TODO: So now there is a lot to be done here, I have the context pretty much
 * working I think, but there is more to do there. The context also needs to
 * be able to be created from the beginning. There should also proabably be an error
 * handling middleware for logging the issues. As well as for logging
 * response times.
 */
<|endoftext|>


<commit_before>package main

var headerTmpl = `
//Generated by https://github.com/c4milo/govsphere
//Do not modify
//Copyright (c) 2014, Cloudescape. All rights reserved.

`

var importsTmpl = `
import (
	gowsdl "github.com/c4milo/gowsdl/generator"
)
`

var moTmpl = doTmpl

var doTmpl = `
{{$comment := comment .Description}}
{{$type := makePublic .Name true}}
{{if $comment}} {{$comment}} {{end}}
type {{$type}} struct {
	{{$extends := toGoType .Extends}}
	{{$namespace := .Namespace}}
	{{lookUpNamespace $extends $namespace}}
	{{range .Properties}}
		{{$fieldComment := comment .Description}}
		{{$privileges := comment .RequiredPrivileges}}
		{{$fieldType := toGoType .Type}}
		{{if $fieldComment}} {{$fieldComment}} {{end}}{{if $privileges}} {{$privileges}} {{end}}
		{{makePublic .Name true}} {{lookUpNamespace $fieldType $namespace}}
	{{end}}
}

{{range .Methods}}
{{$mcomment := comment .Description}}
{{if $mcomment}} {{$mcomment}} {{end}}
func ({{$namespace}} *{{$type}}) {{makePublic .Name true}}(
{{range .Parameters}} {{$ptype := toGoType .Type}} {{replaceReservedWords .Name}} {{lookUpNamespace $ptype $namespace}}, {{end}}
) ({{if .ReturnValue.Type}}{{toGoType .ReturnValue.Type}},{{end}} error) {
	{{if .ReturnValue.Type}}
		return nil, nil
	{{else}}
		return nil
	{{end}}
}
{{end}}
`

var enumTmpl = `
{{$type := .Name}}
{{$comment := comment .Description}}
{{if $comment}} {{$comment}} {{end}}
type {{$type}} string
const (
	{{range .Constants}}
		{{$constComment := comment .Description}}
		{{if $constComment}} {{$constComment}} {{end}}
		{{makePublic .Name true}}_{{$type}} {{$type}} = "{{.Name}}"
	{{end}}
)
`

var faultTmpl = doTmpl
<commit_msg>Minor change<commit_after>package main

var headerTmpl = `
//Generated by https://github.com/c4milo/govsphere
//Do not modify
//Copyright (c) 2014, Cloudescape. All rights reserved.

`

var importsTmpl = `
import (
	gowsdl "github.com/c4milo/gowsdl/generator"
)
`

var moTmpl = doTmpl

var doTmpl = `
{{$comment := comment .Description}}
{{$type := makePublic .Name true}}
{{$namespace := .Namespace}}
{{if $comment}} {{$comment}} {{end}}
type {{$type}} struct {
	{{$extends := toGoType .Extends}}
	{{lookUpNamespace $extends $namespace}}
	{{range .Properties}}
		{{$fieldComment := comment .Description}}
		{{$privileges := comment .RequiredPrivileges}}
		{{$fieldType := toGoType .Type}}
		{{if $fieldComment}} {{$fieldComment}} {{end}}{{if $privileges}} {{$privileges}} {{end}}
		{{makePublic .Name true}} {{lookUpNamespace $fieldType $namespace}}
	{{end}}
}

{{range .Methods}}
{{$mcomment := comment .Description}}
{{if $mcomment}} {{$mcomment}} {{end}}
func ({{$namespace}} *{{$type}}) {{makePublic .Name true}}(
{{range .Parameters}} {{$ptype := toGoType .Type}} {{replaceReservedWords .Name}} {{lookUpNamespace $ptype $namespace}}, {{end}}
) ({{if .ReturnValue.Type}}{{toGoType .ReturnValue.Type}},{{end}} error) {
	{{if .ReturnValue.Type}}
		return nil, nil
	{{else}}
		return nil
	{{end}}
}
{{end}}
`

var enumTmpl = `
{{$type := .Name}}
{{$comment := comment .Description}}
{{if $comment}} {{$comment}} {{end}}
type {{$type}} string
const (
	{{range .Constants}}
		{{$constComment := comment .Description}}
		{{if $constComment}} {{$constComment}} {{end}}
		{{makePublic .Name true}}_{{$type}} {{$type}} = "{{.Name}}"
	{{end}}
)
`

var faultTmpl = doTmpl
<|endoftext|>