<commit_before>package cmd

import (
	"os/exec"
	"strings"
	"testing"

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

func Test_mrListAssignedTo(t *testing.T) {
	t.Parallel()
	repo := copyTestRepo(t)
	cmd := exec.Command(labBinaryPath, "mr", "list", "--assignee=zaquestion")
	cmd.Dir = repo

	b, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatal(err)
	}

	mrs := strings.Split(string(b), "\n")
	t.Log(mrs)
	require.Contains(t, mrs, "!1 Test MR for lab list")
	require.NotContains(t, mrs, "!3")
	require.NotContains(t, mrs, "filtering with labels and lists")
}

func Test_mrList(t *testing.T) {
	t.Parallel()
	repo := copyTestRepo(t)
	cmd := exec.Command(labBinaryPath, "mr", "list")
	cmd.Dir = repo

	b, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatal(err)
	}

	mrs := strings.Split(string(b), "\n")
	t.Log(mrs)
	require.Contains(t, mrs, "!1 Test MR for lab list")
}

func Test_mrListFlagLabel(t *testing.T) {
	t.Parallel()
	repo := copyTestRepo(t)
	cmd := exec.Command(labBinaryPath, "mr", "list", "-l", "confirmed")
	cmd.Dir = repo

	b, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatal(err)
	}

	mrs := strings.Split(string(b), "\n")
	t.Log(mrs)
	require.Contains(t, mrs, "!3 for testings filtering with labels and lists")
}

func Test_mrListStateMerged(t *testing.T) {
	t.Parallel()
	repo := copyTestRepo(t)
	cmd := exec.Command(labBinaryPath, "mr", "list", "-s", "merged")
	cmd.Dir = repo

	b, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatal(err)
	}

	mrs := strings.Split(string(b), "\n")
	t.Log(mrs)
	require.Contains(t, mrs, "!4 merged merge request")
}

func Test_mrListStateClosed(t *testing.T) {
	t.Parallel()
	repo := copyTestRepo(t)
	cmd := exec.Command(labBinaryPath, "mr", "list", "-s", "closed")
	cmd.Dir = repo

	b, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatal(err)
	}

	mrs := strings.Split(string(b), "\n")
	t.Log(mrs)
	require.Contains(t, mrs, "!5 closed mr")

}

func Test_mrListFivePerPage(t *testing.T) {
	t.Parallel()
	repo := copyTestRepo(t)
	cmd := exec.Command(labBinaryPath, "mr", "list", "-n", "5")
	cmd.Dir = repo

	b, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatal(err)
	}

	mrs := strings.Split(string(b), "\n")
	t.Log(mrs)
	require.Contains(t, mrs, "!1 Test MR for lab list")
}

func Test_mrFilterByTargetBranch(t *testing.T) {
	t.Parallel()
	repo := copyTestRepo(t)
	cmd := exec.Command(labBinaryPath, "mr", "list", "-t", "non-existing")
	cmd.Dir = repo

	b, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatal(err)
	}

	mrs := getAppOutput(b)
	assert.Empty(t, mrs, "Expected to find no MRs for non-existent branch")
}

var (
	latestCreatedTestMR = "!19 MR for closing and reopening"
	latestUpdatedTestMR = "!19 MR for closing and reopening"
)

func Test_mrListByTargetBranch(t *testing.T) {
	t.Parallel()
	repo := copyTestRepo(t)
	cmd := exec.Command(labBinaryPath, "mr", "list", "-t", "master")
	cmd.Dir = repo

	b, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatal(err)
	}

	mrs := strings.Split(string(b), "\n")
	require.Equal(t, latestUpdatedTestMR, mrs[0])
}

// updated,asc
// !1
func Test_mrListUpdatedAscending(t *testing.T) {
	t.Parallel()
	repo := copyTestRepo(t)
	cmd := exec.Command(labBinaryPath, "mr", "list", "--number=1", "--order=updated_at", "--sort=asc")
	cmd.Dir = repo

	b, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatal(err)
	}

	mrs := strings.Split(string(b), "\n")
	t.Log(mrs)
	require.Contains(t, mrs, "!3 for testings filtering with labels and lists")
}

// updatead,desc
// !18
func Test_mrListUpdatedDescending(t *testing.T) {
	t.Parallel()
	repo := copyTestRepo(t)
	cmd := exec.Command(labBinaryPath, "mr", "list", "--number=1", "--order=updated_at", "--sort=desc")
	cmd.Dir = repo

	b, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatal(err)
	}

	mrs := strings.Split(string(b), "\n")
	t.Log(mrs)
	require.Equal(t, latestUpdatedTestMR, mrs[0])
}

// created,asc
// !1
func Test_mrListCreatedAscending(t *testing.T) {
	t.Parallel()
	repo := copyTestRepo(t)
	cmd := exec.Command(labBinaryPath, "mr", "list", "--number=1", "--order=created_at", "--sort=asc")
	cmd.Dir = repo

	b, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatal(err)
	}

	mrs := strings.Split(string(b), "\n")
	t.Log(mrs)
	require.Contains(t, mrs, "!1 Test MR for lab list")
}

// created,desc
// !18
func Test_mrListCreatedDescending(t *testing.T) {
	t.Parallel()
	repo := copyTestRepo(t)
	cmd := exec.Command(labBinaryPath, "mr", "list", "--number=1", "--order=created_at", "--sort=desc")
	cmd.Dir = repo

	b, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatal(err)
	}

	mrs := strings.Split(string(b), "\n")
	t.Log(mrs)
	require.Equal(t, latestCreatedTestMR, mrs[0])
}
<commit_msg>mr_list: Fix test<commit_after>package cmd

import (
	"os/exec"
	"strings"
	"testing"

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

func Test_mrListAssignedTo(t *testing.T) {
	t.Parallel()
	repo := copyTestRepo(t)
	cmd := exec.Command(labBinaryPath, "mr", "list", "--assignee=zaquestion")
	cmd.Dir = repo

	b, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatal(err)
	}

	mrs := strings.Split(string(b), "\n")
	t.Log(mrs)
	require.Contains(t, mrs, "!1 Test MR for lab list")
	require.NotContains(t, mrs, "!3")
	require.NotContains(t, mrs, "filtering with labels and lists")
}

func Test_mrList(t *testing.T) {
	t.Parallel()
	repo := copyTestRepo(t)
	cmd := exec.Command(labBinaryPath, "mr", "list")
	cmd.Dir = repo

	b, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatal(err)
	}

	mrs := strings.Split(string(b), "\n")
	t.Log(mrs)
	require.Contains(t, mrs, "!1 Test MR for lab list")
}

func Test_mrListFlagLabel(t *testing.T) {
	t.Parallel()
	repo := copyTestRepo(t)
	cmd := exec.Command(labBinaryPath, "mr", "list", "-l", "confirmed")
	cmd.Dir = repo

	b, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatal(err)
	}

	mrs := strings.Split(string(b), "\n")
	t.Log(mrs)
	require.Contains(t, mrs, "!3 for testings filtering with labels and lists")
}

func Test_mrListStateMerged(t *testing.T) {
	t.Parallel()
	repo := copyTestRepo(t)
	cmd := exec.Command(labBinaryPath, "mr", "list", "-s", "merged")
	cmd.Dir = repo

	b, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatal(err)
	}

	mrs := strings.Split(string(b), "\n")
	t.Log(mrs)
	require.Contains(t, mrs, "!4 merged merge request")
}

func Test_mrListStateClosed(t *testing.T) {
	t.Parallel()
	repo := copyTestRepo(t)
	cmd := exec.Command(labBinaryPath, "mr", "list", "-a", "-s", "closed")
	cmd.Dir = repo

	b, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatal(err)
	}

	mrs := strings.Split(string(b), "\n")
	t.Log(mrs)
	require.Contains(t, mrs, "!5 closed mr")

}

func Test_mrListFivePerPage(t *testing.T) {
	t.Parallel()
	repo := copyTestRepo(t)
	cmd := exec.Command(labBinaryPath, "mr", "list", "-n", "5")
	cmd.Dir = repo

	b, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatal(err)
	}

	mrs := strings.Split(string(b), "\n")
	t.Log(mrs)
	require.Contains(t, mrs, "!1 Test MR for lab list")
}

func Test_mrFilterByTargetBranch(t *testing.T) {
	t.Parallel()
	repo := copyTestRepo(t)
	cmd := exec.Command(labBinaryPath, "mr", "list", "-t", "non-existing")
	cmd.Dir = repo

	b, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatal(err)
	}

	mrs := getAppOutput(b)
	assert.Empty(t, mrs, "Expected to find no MRs for non-existent branch")
}

var (
	latestCreatedTestMR = "!19 MR for closing and reopening"
	latestUpdatedTestMR = "!19 MR for closing and reopening"
)

func Test_mrListByTargetBranch(t *testing.T) {
	t.Parallel()
	repo := copyTestRepo(t)
	cmd := exec.Command(labBinaryPath, "mr", "list", "-t", "master")
	cmd.Dir = repo

	b, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatal(err)
	}

	mrs := strings.Split(string(b), "\n")
	require.Equal(t, latestUpdatedTestMR, mrs[0])
}

// updated,asc
// !1
func Test_mrListUpdatedAscending(t *testing.T) {
	t.Parallel()
	repo := copyTestRepo(t)
	cmd := exec.Command(labBinaryPath, "mr", "list", "--number=1", "--order=updated_at", "--sort=asc")
	cmd.Dir = repo

	b, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatal(err)
	}

	mrs := strings.Split(string(b), "\n")
	t.Log(mrs)
	require.Contains(t, mrs, "!3 for testings filtering with labels and lists")
}

// updatead,desc
// !18
func Test_mrListUpdatedDescending(t *testing.T) {
	t.Parallel()
	repo := copyTestRepo(t)
	cmd := exec.Command(labBinaryPath, "mr", "list", "--number=1", "--order=updated_at", "--sort=desc")
	cmd.Dir = repo

	b, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatal(err)
	}

	mrs := strings.Split(string(b), "\n")
	t.Log(mrs)
	require.Equal(t, latestUpdatedTestMR, mrs[0])
}

// created,asc
// !1
func Test_mrListCreatedAscending(t *testing.T) {
	t.Parallel()
	repo := copyTestRepo(t)
	cmd := exec.Command(labBinaryPath, "mr", "list", "--number=1", "--order=created_at", "--sort=asc")
	cmd.Dir = repo

	b, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatal(err)
	}

	mrs := strings.Split(string(b), "\n")
	t.Log(mrs)
	require.Contains(t, mrs, "!1 Test MR for lab list")
}

// created,desc
// !18
func Test_mrListCreatedDescending(t *testing.T) {
	t.Parallel()
	repo := copyTestRepo(t)
	cmd := exec.Command(labBinaryPath, "mr", "list", "--number=1", "--order=created_at", "--sort=desc")
	cmd.Dir = repo

	b, err := cmd.CombinedOutput()
	if err != nil {
		t.Fatal(err)
	}

	mrs := strings.Split(string(b), "\n")
	t.Log(mrs)
	require.Equal(t, latestCreatedTestMR, mrs[0])
}
<|endoftext|>


<commit_before>/*
Command rqbench is a rqlite load test utility.
*/

package main

import (
	"flag"
	"fmt"
	"time"

	"os"
)

var addr string
var numReqs int
var batchSz int
var tx bool
var tp string

const name = `rqbench`
const desc = `rqbench is a simple load testing utility for rqlite.`

func init() {
	flag.StringVar(&addr, "a", "localhost:4001", "Node address")
	flag.IntVar(&numReqs, "n", 100, "Number of requests")
	flag.IntVar(&batchSz, "b", 1, "Statements per request")
	flag.BoolVar(&tx, "x", false, "Use explicit transaction per request")
	flag.StringVar(&tp, "t", "http", "Transport to use")
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "\n%s\n\n", desc)
		fmt.Fprintf(os.Stderr, "Usage: %s [arguments] <SQL statement>\n", name)
		flag.PrintDefaults()
	}
}

func main() {
	flag.Parse()

	// Ensure the SQL statement is set
	if flag.NArg() == 0 {
		flag.Usage()
		os.Exit(1)
	}
	stmt := flag.Args()[0]

	if tp != "http" {
		fmt.Fprintf(os.Stderr, "not a valid transport: %s\n", tp)
	}

	tester := NewHTTPTester(addr)
	if err := tester.Prepare(stmt, batchSz, tx); err != nil {
		fmt.Println("failed to prepare test:", err.Error())
		os.Exit(1)
	}

	d, err := run(tester, numReqs)
	if err != nil {
		fmt.Println("failed to run test:", err.Error())
		os.Exit(1)
	}
	fmt.Println("Total duration:", d)
	fmt.Printf("Requests/sec: %.2f\n", float64((numReqs))/d.Seconds())
	fmt.Printf("Statements/sec: %.2f\n", float64((numReqs*batchSz))/d.Seconds())
}

type Tester interface {
	Prepare(stmt string, bSz int, tx bool) error
	Once() (time.Duration, error)
}

func run(t Tester, n int) (time.Duration, error) {
	var dur time.Duration

	for i := 0; i < n; i++ {
		if d, err := t.Once(); err != nil {
			return 0, err
		} else {
			dur += d
		}
	}
	return dur, nil
}
<commit_msg>More informative output during load testing<commit_after>/*
Command rqbench is a rqlite load test utility.
*/

package main

import (
	"flag"
	"fmt"
	"time"

	"os"
)

var addr string
var numReqs int
var batchSz int
var modPrint int
var tx bool
var tp string

const name = `rqbench`
const desc = `rqbench is a simple load testing utility for rqlite.`

func init() {
	flag.StringVar(&addr, "a", "localhost:4001", "Node address")
	flag.IntVar(&numReqs, "n", 100, "Number of requests")
	flag.IntVar(&batchSz, "b", 1, "Statements per request")
	flag.IntVar(&modPrint, "m", 0, "Print progress every m requests")
	flag.BoolVar(&tx, "x", false, "Use explicit transaction per request")
	flag.StringVar(&tp, "t", "http", "Transport to use")
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "\n%s\n\n", desc)
		fmt.Fprintf(os.Stderr, "Usage: %s [arguments] <SQL statement>\n", name)
		flag.PrintDefaults()
	}
}

func main() {
	flag.Parse()

	// Ensure the SQL statement is set
	if flag.NArg() == 0 {
		flag.Usage()
		os.Exit(1)
	}
	stmt := flag.Args()[0]

	if tp != "http" {
		fmt.Fprintf(os.Stderr, "not a valid transport: %s\n", tp)
	}

	tester := NewHTTPTester(addr)
	if err := tester.Prepare(stmt, batchSz, tx); err != nil {
		fmt.Println("failed to prepare test:", err.Error())
		os.Exit(1)
	}

	d, err := run(tester, numReqs)
	if err != nil {
		fmt.Println("failed to run test:", err.Error())
		os.Exit(1)
	}
	fmt.Println("Total duration:", d)
	fmt.Printf("Requests/sec: %.2f\n", float64((numReqs))/d.Seconds())
	fmt.Printf("Statements/sec: %.2f\n", float64((numReqs*batchSz))/d.Seconds())
}

type Tester interface {
	Prepare(stmt string, bSz int, tx bool) error
	Once() (time.Duration, error)
}

func run(t Tester, n int) (time.Duration, error) {
	var dur time.Duration

	for i := 0; i < n; i++ {
		d, err := t.Once()
		if err != nil {
			return 0, err
		} else {
			dur += d
		}

		if modPrint != 0 && i != 0 && i%modPrint == 0 {
			fmt.Printf("%d requests completed in %s\n", i, d)
		}
	}
	return dur, nil
}
<|endoftext|>


<commit_before>package main

import (
	"encoding/csv"
	"flag"
	"fmt"
	"io"
	"log"
	"os"

	"github.com/davecgh/go-spew/spew"

	"github.com/donatj/sqlread"
	"github.com/donatj/sqlread/mapcache"
)

var filename string

var (
	nocache = flag.Bool("nocache", false, "disable caching")
)

func init() {
	flag.Parse()

	filename = flag.Arg(0)
}

func main() {
	// return
	log.Println("starting initial pass")

	unbuff, err := os.Open(filename)
	if err != nil {
		log.Fatal(err)
	}

	cache := mapcache.New(unbuff)
	tree, err := cache.Get()
	if err != nil && err != mapcache.ErrCacheMiss {
		log.Fatal(err)
	}

	fmt.Println(*nocache)

	if err == mapcache.ErrCacheMiss || *nocache {
		l, li := sqlread.Lex(unbuff)
		go func() {
			l.Run(sqlread.StartState)
		}()

		sp := sqlread.NewSummaryParser()

		p := sqlread.Parse(li)
		err = p.Run(sp.ParseStart)
		if err != nil {
			log.Fatal(err)
		}

		if !*nocache {
			cache.Store(sp.Tree)
		}

		tree = sp.Tree
	} else {
		log.Println("loaded from cache")
	}

	log.Println("finished initial pass")

	//for tbl, _ := range t {
	//	fmt.Println(tbl)
	//}

	_ = tree

	interactive(tree, unbuff)
}

func interactive(tree sqlread.SummaryTree, buff io.ReaderAt) {
	w := csv.NewWriter(os.Stdout)
	sw := NewStdinWrap(os.Stdin)

	intp := sqlread.Intp{}
	for {
		stdinlex, stdli := sqlread.Lex(sw)
		go func() {
			stdinlex.Run(intp.StartIntpState)
		}()

		qp := sqlread.NewQueryParser()

		p := sqlread.Parse(stdli)
		err := p.Run(qp.ParseStart)
		if err != nil {
			log.Println("query error: ", err)
			sw.Flush()
			continue
		}

		for _, qry := range qp.Tree.Queries {
			if err := execQuery(tree, qry, buff, w); err != nil {
				log.Println(err)
			}
		}

		for i := uint(0); i < qp.Tree.ShowTables; i++ {
			showTables(tree, w)
		}

		for _, sctbl := range qp.Tree.ShowColumns {
			if err := showColumns(tree, sctbl, w); err != nil {
				log.Println(err)
			}
		}

		if qp.Tree.Quit {
			return
		}

		if intp.EOF {
			break
		}

		sw.Flush()
		log.Println("restarting lexer")
	}
}

func execQuery(tree sqlread.SummaryTree, qry sqlread.Query, buff io.ReaderAt, w *csv.Writer) error {
	tbl, tok := tree[qry.Table]
	if !tok {
		return fmt.Errorf("table `%s` not found", qry.Table)
	}
	colind := []int{}
	for _, col := range qry.Columns {
		found := false
		for tci, tcol := range tbl.Cols {
			if col == "*" || col == tcol.Name {
				found = true
				colind = append(colind, tci)
			}
		}

		if !found {
			return fmt.Errorf("column `%s` not found", col)
		}
	}
	for _, loc := range tbl.DataLocs {
		start := loc.Start.Pos
		end := loc.End.Pos

		sl, sli := sqlread.LexSection(buff, start, end-start+1)
		go func() {
			sl.Run(sqlread.StartState)
		}()

		sp := sqlread.NewInsertDetailParser()

		spr := sqlread.Parse(sli)
		go func() {
			err := spr.Run(sp.ParseStart)
			if err != nil {
				log.Fatal(err)
			}
		}()

		for {
			row, ok := <-sp.Out
			if !ok {
				w.Flush()
				break
			}

			out := make([]string, len(colind))
			for i, ci := range colind {
				out[i] = row[ci]
			}

			w.Write(out)
		}
	}

	return nil
}

func showColumns(tree sqlread.SummaryTree, sctbl string, w *csv.Writer) error {
	tbl, tok := tree[sctbl]
	if !tok {
		return fmt.Errorf("table `%s` not found", sctbl)
	}
	for _, col := range tbl.Cols {
		w.Write([]string{col.Name, col.Type})
	}
	w.Flush()

	return nil
}

func showTables(tree sqlread.SummaryTree, w *csv.Writer) {
	for cv, _ := range tree {
		w.Write([]string{cv})
	}
	w.Flush()
}
<commit_msg>Fix imports<commit_after>package main

import (
	"encoding/csv"
	"flag"
	"fmt"
	"io"
	"log"
	"os"

	"github.com/donatj/sqlread"
	"github.com/donatj/sqlread/mapcache"
)

var filename string

var (
	nocache = flag.Bool("nocache", false, "disable caching")
)

func init() {
	flag.Parse()

	filename = flag.Arg(0)
}

func main() {
	// return
	log.Println("starting initial pass")

	unbuff, err := os.Open(filename)
	if err != nil {
		log.Fatal(err)
	}

	cache := mapcache.New(unbuff)
	tree, err := cache.Get()
	if err != nil && err != mapcache.ErrCacheMiss {
		log.Fatal(err)
	}

	fmt.Println(*nocache)

	if err == mapcache.ErrCacheMiss || *nocache {
		l, li := sqlread.Lex(unbuff)
		go func() {
			l.Run(sqlread.StartState)
		}()

		sp := sqlread.NewSummaryParser()

		p := sqlread.Parse(li)
		err = p.Run(sp.ParseStart)
		if err != nil {
			log.Fatal(err)
		}

		if !*nocache {
			cache.Store(sp.Tree)
		}

		tree = sp.Tree
	} else {
		log.Println("loaded from cache")
	}

	log.Println("finished initial pass")

	//for tbl, _ := range t {
	//	fmt.Println(tbl)
	//}

	_ = tree

	interactive(tree, unbuff)
}

func interactive(tree sqlread.SummaryTree, buff io.ReaderAt) {
	w := csv.NewWriter(os.Stdout)
	sw := NewStdinWrap(os.Stdin)

	intp := sqlread.Intp{}
	for {
		stdinlex, stdli := sqlread.Lex(sw)
		go func() {
			stdinlex.Run(intp.StartIntpState)
		}()

		qp := sqlread.NewQueryParser()

		p := sqlread.Parse(stdli)
		err := p.Run(qp.ParseStart)
		if err != nil {
			log.Println("query error: ", err)
			sw.Flush()
			continue
		}

		for _, qry := range qp.Tree.Queries {
			if err := execQuery(tree, qry, buff, w); err != nil {
				log.Println(err)
			}
		}

		for i := uint(0); i < qp.Tree.ShowTables; i++ {
			showTables(tree, w)
		}

		for _, sctbl := range qp.Tree.ShowColumns {
			if err := showColumns(tree, sctbl, w); err != nil {
				log.Println(err)
			}
		}

		if qp.Tree.Quit {
			return
		}

		if intp.EOF {
			break
		}

		sw.Flush()
		log.Println("restarting lexer")
	}
}

func execQuery(tree sqlread.SummaryTree, qry sqlread.Query, buff io.ReaderAt, w *csv.Writer) error {
	tbl, tok := tree[qry.Table]
	if !tok {
		return fmt.Errorf("table `%s` not found", qry.Table)
	}
	colind := []int{}
	for _, col := range qry.Columns {
		found := false
		for tci, tcol := range tbl.Cols {
			if col == "*" || col == tcol.Name {
				found = true
				colind = append(colind, tci)
			}
		}

		if !found {
			return fmt.Errorf("column `%s` not found", col)
		}
	}
	for _, loc := range tbl.DataLocs {
		start := loc.Start.Pos
		end := loc.End.Pos

		sl, sli := sqlread.LexSection(buff, start, end-start+1)
		go func() {
			sl.Run(sqlread.StartState)
		}()

		sp := sqlread.NewInsertDetailParser()

		spr := sqlread.Parse(sli)
		go func() {
			err := spr.Run(sp.ParseStart)
			if err != nil {
				log.Fatal(err)
			}
		}()

		for {
			row, ok := <-sp.Out
			if !ok {
				w.Flush()
				break
			}

			out := make([]string, len(colind))
			for i, ci := range colind {
				out[i] = row[ci]
			}

			w.Write(out)
		}
	}

	return nil
}

func showColumns(tree sqlread.SummaryTree, sctbl string, w *csv.Writer) error {
	tbl, tok := tree[sctbl]
	if !tok {
		return fmt.Errorf("table `%s` not found", sctbl)
	}
	for _, col := range tbl.Cols {
		w.Write([]string{col.Name, col.Type})
	}
	w.Flush()

	return nil
}

func showTables(tree sqlread.SummaryTree, w *csv.Writer) {
	for cv, _ := range tree {
		w.Write([]string{cv})
	}
	w.Flush()
}
<|endoftext|>


<commit_before>package test

import (
	"io"
	"net/http"
	"testing"
)

type Response struct {
	path, query, contenttype, body string
}

func GetHandler(t *testing.T, path string, responseBody string) http.HandlerFunc {
	response := &Response{
		path:        path,
		contenttype: "application/json",
		body:        responseBody,
	}
	handler := func(w http.ResponseWriter, r *http.Request) {
		// Check request.
		if g, w := r.URL.Path, response.path; g != w {
			t.Errorf("request got path %s, want %s", g, w)
		}

		// Send response.
		w.Header().Set("Content-Type", response.contenttype)
		io.WriteString(w, response.body)
	}
	return handler
}
<commit_msg>delete unused file<commit_after><|endoftext|>


<commit_before>// Command webhook starts a web server which listens on GitHub's POST requests.
// The payload of each request is verified against its signature, unmarshalled
// into corresponding event struct and the applied to the template script provided
// by a user.
//
// Usage
//
//   webhook [-cert file -key file] [-addr address] -secret key script
//
// The struct being passed to the template script is:
//
//   type Event struct {
//   	Name    string
//   	Payload interface{}
//   }
//
// The Name field denotes underlying type for the Payload. Full mapping between
// possible Name values and Payload types is listed in the documentation of
// the webhook package.
//
// Template scripts use template syntax of text/template package. Each template
// script has registered extra control functions:
//
//   env
//   	An alias for os.Getenv.
//   log
//   	An alias for log.Println. Used only for side-effect, returns empty string.
//   logf
//   	An alias for log.Printf. Used only for side-effect, returns empty string.
//   exec
//   	An alias for exec.Command. Returned value is the process' output read
//   	from its os.Stdout.
//
// Example
//
// In order to log an e-mail of each person that pushed to your repository, create
// a template script with the following content:
//
//   $ cat >push.tsc <<EOF
//   > {{if .Name | eq "push"}}
//   >   {{logf "%s pushed to %s" .Payload.Pusher.Email .Payload.Repository.Name}}
//   > {{endif}}
//   > EOF
//
// And start the webhook:
//
//   $ webhook -secret secret123 push.tsc
//   2015/03/13 21:32:15 INFO Listening on [::]:8080 . . .
//
// Webhook listens on 0.0.0.0:8080 by default.
//
// The -cert and -key flags are used to provide paths for the certificate and private
// key files. When specified, webhook serves HTTPS connections by default on 0.0.0.0:8443.
//
// The -addr flag can be used to specify a network address for the webhook to listen on.
//
// The -secret flag sets the secret value to verify the signature of GitHub's payloads.
// The value is required and cannot be empty.
//
// The -log flag redirects output to the given file.
//
// The script argument is a path to the template script file which is used as a handler
// for incoming events.
package main

import (
	"bytes"
	"crypto/rand"
	"crypto/tls"
	"flag"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"net"
	"net/http"
	"os"
	"os/exec"
	"path/filepath"
	"text/template"
	"time"

	"github.com/rjeczalik/gh/webhook"
)

const usage = `usage: webhook [-cert file -key file] [-addr address] -secret key script

Starts a web server which listens on GitHub's POST requests. The payload of each
request is verified against its signature, unmarshalled into corresponding event
struct and the applied to the template script provided by a user.

The struct being passed to the template script is:

	type Event struct {
		Name    string
		Payload interface{}
	}

The Name field denotes underlying type for the Payload. Full mapping between
possible Name values and Payload types is listed in the documentation of
the webhook package.

Template scripts use template syntax of text/template package. Each template
script has registered extra control functions:

	env
		An alias for os.Getenv.
	log
		An alias for log.Println. Used only for side-effect, returns empty string.
	logf
		An alias for log.Printf. Used only for side-effect, returns empty string.
	exec
		An alias for exec.Command. Returned value is the process' output read
		from its os.Stdout.

Example

In order to log an e-mail of each person that pushed to your repository, create
a template script with the following content:

	$ cat >push.tsc <EOF
	> {{if .Name eq "push"}}
	>   {{logf "%s pushed to %s" .Payload.Pusher.Email .Payload.Repository.Name}}
	> {{endif}}
	> EOF

And start the webhook:

	$ webhook -secret secret123 push.tsc
	2015/03/13 21:32:15 INFO Listening on [::]:8080 . . .

Webhook listens on 0.0.0.0:8080 by default.

The -cert and -key flags are used to provide paths for certificate and private
key files. When specified, webhook serves HTTPS connection by default on 0.0.0.0:8443.

The -addr flag can be used to specify a network address for the webhook to listen on.

The -secret flag sets the secret value to verify the signature of GitHub's payloads.
The value is required and cannot be empty.

The -log flag redirects output to the given file.

The script argument is a path to the template script file which is used as a handler
for incoming events.`

var (
	cert    = flag.String("cert", "", "Certificate file.")
	key     = flag.String("key", "", "Private key file.")
	addr    = flag.String("addr", "", "Network address to listen on. Default is :8080 for HTTP and :8443 for HTTPS.")
	secret  = flag.String("secret", "", "GitHub secret value used for signing payloads.")
	debug   = flag.Bool("debug", false, "Dumps verified payloads into testdata directory.")
	logfile = flag.String("log", "", "Redirects output to the given file.")
)

type Event struct {
	Name    string      // https://developer.github.com/webhooks/#events
	Payload interface{} // https://developer.github.com/v3/activity/events/types/
}

var scriptFuncs = template.FuncMap{
	"env": func(s string) string {
		return os.Getenv(s)
	},
	"exec": func(cmd string, args ...string) (string, error) {
		out, err := exec.Command(cmd, args...).Output()
		if *debug {
			log.Printf("[DEBUG] exec cmd=%s args=%q err=%v", cmd, args, err)
		}
		return string(bytes.TrimSpace(out)), err
	},
	"log": func(v ...interface{}) string {
		if len(v) != 0 {
			log.Println(v...)
		}
		return ""
	},
	"logf": func(format string, v ...interface{}) string {
		if format == "" {
			return ""
		}
		if len(v) == 0 {
			log.Printf("%s", format)
		} else {
			log.Printf(format, v...)
		}
		return ""
	},
}

type templater struct {
	tmpl *template.Template
}

func newTemplater(file string) (templater, error) {
	tmpl := template.New(filepath.Base(file)).Funcs(scriptFuncs)
	tmpl, err := tmpl.ParseFiles(flag.Arg(0))
	if err != nil {
		return templater{}, err
	}
	return templater{tmpl: tmpl}, nil
}

func (h templater) All(event string, payload interface{}) {
	if err := h.tmpl.Execute(ioutil.Discard, Event{Name: event, Payload: payload}); err != nil {
		log.Println("ERROR template error:", err)
		return
	}
}

type dumper struct {
	http.Handler
}

func (d dumper) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	var buf bytes.Buffer
	req.Body = ioutil.NopCloser(io.TeeReader(req.Body, &buf))
	d.Handler.ServeHTTP(w, req)
	go dump(req.Header.Get("X-GitHub-Event"), buf.Bytes())
}

func now() string {
	return time.Now().UTC().Format("2006-01-02 at 03.04.05.000")
}

func dump(event string, p []byte) {
	switch {
	case event == "":
		log.Println("[DEBUG] ERROR empty event name")
		return
	case len(p) == 0:
		log.Println("[DEBUG] ERROR empty payload")
		return
	}
	if err := os.MkdirAll("testdata", 0755); err != nil {
		log.Println("[DEBUG] ERROR creating testdata:", err)
		return
	}
	name := filepath.Join("testdata", fmt.Sprintf("%s-%s.json", event, now()))
	if err := ioutil.WriteFile(name, p, 0644); err != nil {
		log.Printf("[DEBUG] ERROR creating %s: %v", name, err)
	}
}

func nonil(s ...string) string {
	for _, s := range s {
		if s != "" {
			return s
		}
	}
	return ""
}

func die(v interface{}) {
	fmt.Fprintln(os.Stderr, v)
	os.Exit(1)
}

func main() {
	if len(os.Args) == 1 {
		die(usage)
	}
	flag.CommandLine.Usage = func() {
		fmt.Fprintln(os.Stderr, usage)
	}
	flag.Parse()
	if flag.NArg() != 1 || flag.Arg(0) == "" {
		die("invalid number of arguments")
	}
	if (*cert == "") != (*key == "") {
		die("both -cert and -key flags must be provided")
	}
	if *logfile != "" {
		f, err := os.OpenFile(*logfile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
		if err != nil {
			die(err)
		}
		log.SetOutput(f)
		defer f.Close()
	}
	tmpl, err := newTemplater(flag.Arg(0))
	if err != nil {
		die(err)
	}
	var listener net.Listener
	if *cert != "" {
		crt, err := tls.LoadX509KeyPair(*cert, *key)
		if err != nil {
			die(err)
		}
		cfg := &tls.Config{
			Certificates: []tls.Certificate{crt},
			Rand:         rand.Reader,
			// Don't offer SSL3.
			MinVersion: tls.VersionTLS10,
			MaxVersion: tls.VersionTLS12,
			// Don't offer RC4 ciphers.
			CipherSuites: []uint16{
				tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
				tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
				tls.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
				tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
				tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
				tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
				tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA,
				tls.TLS_RSA_WITH_AES_128_CBC_SHA,
				tls.TLS_RSA_WITH_AES_256_CBC_SHA,
			},
		}
		l, err := tls.Listen("tcp", nonil(*addr, "0.0.0.0:8443"), cfg)
		if err != nil {
			die(err)
		}
		listener = l
	} else {
		l, err := net.Listen("tcp", nonil(*addr, "0.0.0.0:8080"))
		if err != nil {
			die(err)
		}
		listener = l
	}
	var handler http.Handler = webhook.New(*secret, tmpl)
	if *debug {
		handler = dumper{Handler: handler}
	}
	log.Printf("INFO Listening on %s . . .", listener.Addr())
	if err := http.Serve(listener, handler); err != nil {
		die(err)
	}
}
<commit_msg>cmd/webhook: document -debug flag<commit_after>// Command webhook starts a web server which listens on GitHub's POST requests.
// The payload of each request is verified against its signature, unmarshalled
// into corresponding event struct and the applied to the template script provided
// by a user.
//
// Usage
//
//   webhook [-cert file -key file] [-addr address] [-log file] -secret key script
//
// The struct being passed to the template script is:
//
//   type Event struct {
//   	Name    string
//   	Payload interface{}
//   }
//
// The Name field denotes underlying type for the Payload. Full mapping between
// possible Name values and Payload types is listed in the documentation of
// the webhook package.
//
// Template scripts use template syntax of text/template package. Each template
// script has registered extra control functions:
//
//   env
//   	An alias for os.Getenv.
//   log
//   	An alias for log.Println. Used only for side-effect, returns empty string.
//   logf
//   	An alias for log.Printf. Used only for side-effect, returns empty string.
//   exec
//   	An alias for exec.Command. Returned value is the process' output read
//   	from its os.Stdout.
//
// Example
//
// In order to log an e-mail of each person that pushed to your repository, create
// a template script with the following content:
//
//   $ cat >push.tsc <<EOF
//   > {{if .Name | eq "push"}}
//   >   {{logf "%s pushed to %s" .Payload.Pusher.Email .Payload.Repository.Name}}
//   > {{endif}}
//   > EOF
//
// And start the webhook:
//
//   $ webhook -secret secret123 push.tsc
//   2015/03/13 21:32:15 INFO Listening on [::]:8080 . . .
//
// Webhook listens on 0.0.0.0:8080 by default.
//
// The -cert and -key flags are used to provide paths for the certificate and private
// key files. When specified, webhook serves HTTPS connections by default on 0.0.0.0:8443.
//
// The -addr flag can be used to specify a network address for the webhook to listen on.
//
// The -secret flag sets the secret value to verify the signature of GitHub's payloads.
// The value is required and cannot be empty.
//
// The -log flag redirects output to the given file.
//
// The -debug flag makes webhook dump each received JSON payload into
// $PWD/testdata/<event>-<timestamp>.json file.
//
// The script argument is a path to the template script file which is used as a handler
// for incoming events.
package main

import (
	"bytes"
	"crypto/rand"
	"crypto/tls"
	"flag"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"net"
	"net/http"
	"os"
	"os/exec"
	"path/filepath"
	"text/template"
	"time"

	"github.com/rjeczalik/gh/webhook"
)

const usage = `usage: webhook [-cert file -key file] [-addr address] [-log file] -secret key script

Starts a web server which listens on GitHub's POST requests. The payload of each
request is verified against its signature, unmarshalled into corresponding event
struct and the applied to the template script provided by a user.

The struct being passed to the template script is:

	type Event struct {
		Name    string
		Payload interface{}
	}

The Name field denotes underlying type for the Payload. Full mapping between
possible Name values and Payload types is listed in the documentation of
the webhook package.

Template scripts use template syntax of text/template package. Each template
script has registered extra control functions:

	env
		An alias for os.Getenv.
	log
		An alias for log.Println. Used only for side-effect, returns empty string.
	logf
		An alias for log.Printf. Used only for side-effect, returns empty string.
	exec
		An alias for exec.Command. Returned value is the process' output read
		from its os.Stdout.

Example

In order to log an e-mail of each person that pushed to your repository, create
a template script with the following content:

	$ cat >push.tsc <EOF
	> {{if .Name eq "push"}}
	>   {{logf "%s pushed to %s" .Payload.Pusher.Email .Payload.Repository.Name}}
	> {{endif}}
	> EOF

And start the webhook:

	$ webhook -secret secret123 push.tsc
	2015/03/13 21:32:15 INFO Listening on [::]:8080 . . .

Webhook listens on 0.0.0.0:8080 by default.

The -cert and -key flags are used to provide paths for certificate and private
key files. When specified, webhook serves HTTPS connection by default on 0.0.0.0:8443.

The -addr flag can be used to specify a network address for the webhook to listen on.

The -secret flag sets the secret value to verify the signature of GitHub's payloads.
The value is required and cannot be empty.

The -log flag redirects output to the given file.

The -debug flag makes webhook dump each received JSON payload into
$PWD/testdata/<event>-<timestamp>.json file.

The script argument is a path to the template script file which is used as a handler
for incoming events.`

var (
	cert    = flag.String("cert", "", "Certificate file.")
	key     = flag.String("key", "", "Private key file.")
	addr    = flag.String("addr", "", "Network address to listen on. Default is :8080 for HTTP and :8443 for HTTPS.")
	secret  = flag.String("secret", "", "GitHub secret value used for signing payloads.")
	debug   = flag.Bool("debug", false, "Dumps verified payloads into testdata directory.")
	logfile = flag.String("log", "", "Redirects output to the given file.")
)

type Event struct {
	Name    string      // https://developer.github.com/webhooks/#events
	Payload interface{} // https://developer.github.com/v3/activity/events/types/
}

var scriptFuncs = template.FuncMap{
	"env": func(s string) string {
		return os.Getenv(s)
	},
	"exec": func(cmd string, args ...string) (string, error) {
		out, err := exec.Command(cmd, args...).Output()
		if *debug {
			log.Printf("[DEBUG] exec cmd=%s args=%q err=%v", cmd, args, err)
		}
		return string(bytes.TrimSpace(out)), err
	},
	"log": func(v ...interface{}) string {
		if len(v) != 0 {
			log.Println(v...)
		}
		return ""
	},
	"logf": func(format string, v ...interface{}) string {
		if format == "" {
			return ""
		}
		if len(v) == 0 {
			log.Printf("%s", format)
		} else {
			log.Printf(format, v...)
		}
		return ""
	},
}

type templater struct {
	tmpl *template.Template
}

func newTemplater(file string) (templater, error) {
	tmpl := template.New(filepath.Base(file)).Funcs(scriptFuncs)
	tmpl, err := tmpl.ParseFiles(flag.Arg(0))
	if err != nil {
		return templater{}, err
	}
	return templater{tmpl: tmpl}, nil
}

func (h templater) All(event string, payload interface{}) {
	if err := h.tmpl.Execute(ioutil.Discard, Event{Name: event, Payload: payload}); err != nil {
		log.Println("ERROR template error:", err)
		return
	}
}

type dumper struct {
	http.Handler
}

func (d dumper) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	var buf bytes.Buffer
	req.Body = ioutil.NopCloser(io.TeeReader(req.Body, &buf))
	d.Handler.ServeHTTP(w, req)
	go dump(req.Header.Get("X-GitHub-Event"), buf.Bytes())
}

func now() string {
	return time.Now().UTC().Format("2006-01-02 at 03.04.05.000")
}

func dump(event string, p []byte) {
	switch {
	case event == "":
		log.Println("[DEBUG] ERROR empty event name")
		return
	case len(p) == 0:
		log.Println("[DEBUG] ERROR empty payload")
		return
	}
	if err := os.MkdirAll("testdata", 0755); err != nil {
		log.Println("[DEBUG] ERROR creating testdata:", err)
		return
	}
	name := filepath.Join("testdata", fmt.Sprintf("%s-%s.json", event, now()))
	if err := ioutil.WriteFile(name, p, 0644); err != nil {
		log.Printf("[DEBUG] ERROR creating %s: %v", name, err)
	}
}

func nonil(s ...string) string {
	for _, s := range s {
		if s != "" {
			return s
		}
	}
	return ""
}

func die(v interface{}) {
	fmt.Fprintln(os.Stderr, v)
	os.Exit(1)
}

func main() {
	if len(os.Args) == 1 {
		die(usage)
	}
	flag.CommandLine.Usage = func() {
		fmt.Fprintln(os.Stderr, usage)
	}
	flag.Parse()
	if flag.NArg() != 1 || flag.Arg(0) == "" {
		die("invalid number of arguments")
	}
	if (*cert == "") != (*key == "") {
		die("both -cert and -key flags must be provided")
	}
	if *logfile != "" {
		f, err := os.OpenFile(*logfile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
		if err != nil {
			die(err)
		}
		log.SetOutput(f)
		defer f.Close()
	}
	tmpl, err := newTemplater(flag.Arg(0))
	if err != nil {
		die(err)
	}
	var listener net.Listener
	if *cert != "" {
		crt, err := tls.LoadX509KeyPair(*cert, *key)
		if err != nil {
			die(err)
		}
		cfg := &tls.Config{
			Certificates: []tls.Certificate{crt},
			Rand:         rand.Reader,
			// Don't offer SSL3.
			MinVersion: tls.VersionTLS10,
			MaxVersion: tls.VersionTLS12,
			// Don't offer RC4 ciphers.
			CipherSuites: []uint16{
				tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
				tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
				tls.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
				tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
				tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
				tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
				tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA,
				tls.TLS_RSA_WITH_AES_128_CBC_SHA,
				tls.TLS_RSA_WITH_AES_256_CBC_SHA,
			},
		}
		l, err := tls.Listen("tcp", nonil(*addr, "0.0.0.0:8443"), cfg)
		if err != nil {
			die(err)
		}
		listener = l
	} else {
		l, err := net.Listen("tcp", nonil(*addr, "0.0.0.0:8080"))
		if err != nil {
			die(err)
		}
		listener = l
	}
	var handler http.Handler = webhook.New(*secret, tmpl)
	if *debug {
		handler = dumper{Handler: handler}
	}
	log.Printf("INFO Listening on %s . . .", listener.Addr())
	if err := http.Serve(listener, handler); err != nil {
		die(err)
	}
}
<|endoftext|>


<commit_before>package main

import (
	"crypto/sha1"
	"crypto/sha256"
	"flag"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"syscall"

	"github.com/zaolin/go-tpm/tpm"
	"golang.org/x/crypto/ed25519"
)

const (
	tpmDevice  string = "/dev/tpm0"
	mountPath  string = "/mnt/vboot"
	filesystem string = "ext3"
)

var (
	publicKey            = flag.String("pubkey", "/etc/sig.pub", "A public key which should verify the signature.")
	pcr                  = flag.Uint("pcr", 12, "The pcr index used for measuring the kernel before kexec.")
	bootDev              = flag.String("boot-device", "/dev/sda1", "The boot device which is used to kexec into a signed kernel.")
	linuxKernel          = flag.String("kernel", "/mnt/vboot/kernel", "Kernel image file path.")
	linuxKernelSignature = flag.String("kernel-sig", "/mnt/vboot/kernel.sig", "Kernel image signature file path.")
	initrd               = flag.String("initrd", "/mnt/vboot/initrd", "Initrd file path.")
	initrdSignature      = flag.String("initrd-sig", "/mnt/vboot/initrd.sig", "Initrd signature file path.")
	debug                = flag.Bool("debug", false, "Enables debug mode.")
	noTPM                = flag.Bool("no-tpm", false, "Disables tpm measuring process.")
)

func die(err error) {
	if *debug {
		panic(err)
	}
	if err := syscall.Reboot(syscall.LINUX_REBOOT_CMD_POWER_OFF); err != nil {
		log.Fatalf("reboot err: %v", err)
	}
}

func main() {
	flag.Parse()

	if err := os.MkdirAll(mountPath, os.ModePerm); err != nil {
		die(err)
	}

	if err := syscall.Mount(*bootDev, mountPath, filesystem, syscall.MS_RDONLY, ""); err != nil {
		die(err)
	}

	paths := []string{*publicKey, *linuxKernel, *linuxKernelSignature, *initrd, *initrdSignature}
	files := make(map[string][]byte)

	for _, element := range paths {
		data, err := ioutil.ReadFile(element)
		if err != nil {
			die(err)
		} else {
			files[element] = data
		}
	}

	kernelDigest := sha256.Sum256(files[*linuxKernel])
	initrdDigest := sha256.Sum256(files[*initrd])

	pcrDigestKernel := sha1.Sum(files[*linuxKernel])
	pcrDigestInitrd := sha1.Sum(files[*initrd])

	kernelSuccess := ed25519.Verify(files[*publicKey], kernelDigest[:], files[*linuxKernelSignature])
	initrdSuccess := ed25519.Verify(files[*publicKey], initrdDigest[:], files[*initrdKernelSignature])

	if !kernelSuccess || !initrdSuccess {
		die(nil)
	}

	if !*noTPM {
		rwc, err := tpm.OpenTPM(tpmDevice)
		if err != nil {
			die(err)
		}

		tpm.PcrExtend(rwc, uint32(*pcr), pcrDigestKernel)
		tpm.PcrExtend(rwc, uint32(*pcr), pcrDigestInitrd)
	}

	binary, lookErr := exec.LookPath("kexec")
	if lookErr != nil {
		die(lookErr)
	}

	args := []string{"kexec", "-initrd", *initrd, *linuxKernel}
	env := os.Environ()

	if execErr := syscall.Exec(binary, args, env); execErr != nil {
		die(execErr)
	}
}
<commit_msg>cmds/vboot: fix my own typo, too.<commit_after>package main

import (
	"crypto/sha1"
	"crypto/sha256"
	"flag"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"syscall"

	"github.com/zaolin/go-tpm/tpm"
	"golang.org/x/crypto/ed25519"
)

const (
	tpmDevice  string = "/dev/tpm0"
	mountPath  string = "/mnt/vboot"
	filesystem string = "ext3"
)

var (
	publicKey            = flag.String("pubkey", "/etc/sig.pub", "A public key which should verify the signature.")
	pcr                  = flag.Uint("pcr", 12, "The pcr index used for measuring the kernel before kexec.")
	bootDev              = flag.String("boot-device", "/dev/sda1", "The boot device which is used to kexec into a signed kernel.")
	linuxKernel          = flag.String("kernel", "/mnt/vboot/kernel", "Kernel image file path.")
	linuxKernelSignature = flag.String("kernel-sig", "/mnt/vboot/kernel.sig", "Kernel image signature file path.")
	initrd               = flag.String("initrd", "/mnt/vboot/initrd", "Initrd file path.")
	initrdSignature      = flag.String("initrd-sig", "/mnt/vboot/initrd.sig", "Initrd signature file path.")
	debug                = flag.Bool("debug", false, "Enables debug mode.")
	noTPM                = flag.Bool("no-tpm", false, "Disables tpm measuring process.")
)

func die(err error) {
	if *debug {
		panic(err)
	}
	if err := syscall.Reboot(syscall.LINUX_REBOOT_CMD_POWER_OFF); err != nil {
		log.Fatalf("reboot err: %v", err)
	}
}

func main() {
	flag.Parse()

	if err := os.MkdirAll(mountPath, os.ModePerm); err != nil {
		die(err)
	}

	if err := syscall.Mount(*bootDev, mountPath, filesystem, syscall.MS_RDONLY, ""); err != nil {
		die(err)
	}

	paths := []string{*publicKey, *linuxKernel, *linuxKernelSignature, *initrd, *initrdSignature}
	files := make(map[string][]byte)

	for _, element := range paths {
		data, err := ioutil.ReadFile(element)
		if err != nil {
			die(err)
		} else {
			files[element] = data
		}
	}

	kernelDigest := sha256.Sum256(files[*linuxKernel])
	initrdDigest := sha256.Sum256(files[*initrd])

	pcrDigestKernel := sha1.Sum(files[*linuxKernel])
	pcrDigestInitrd := sha1.Sum(files[*initrd])

	kernelSuccess := ed25519.Verify(files[*publicKey], kernelDigest[:], files[*linuxKernelSignature])
	initrdSuccess := ed25519.Verify(files[*publicKey], initrdDigest[:], files[*initrdSignature])

	if !kernelSuccess || !initrdSuccess {
		die(nil)
	}

	if !*noTPM {
		rwc, err := tpm.OpenTPM(tpmDevice)
		if err != nil {
			die(err)
		}

		tpm.PcrExtend(rwc, uint32(*pcr), pcrDigestKernel)
		tpm.PcrExtend(rwc, uint32(*pcr), pcrDigestInitrd)
	}

	binary, lookErr := exec.LookPath("kexec")
	if lookErr != nil {
		die(lookErr)
	}

	args := []string{"kexec", "-initrd", *initrd, *linuxKernel}
	env := os.Environ()

	if execErr := syscall.Exec(binary, args, env); execErr != nil {
		die(execErr)
	}
}
<|endoftext|>


<commit_before>package greenskeeper

import (
	"os/exec"

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

	"testing"
)

func TestGreenskeeper(t *testing.T) {
	RegisterFailHandler(Fail)
	RunSpecs(t, "Greenskeeper Suite")
}

var _ = BeforeSuite(func() {
	cmd := exec.Command("groupadd", "mewtwo")
	session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
	Expect(err).NotTo(HaveOccurred())
	Eventually(session).Should(gexec.Exit(0))

	cmd = exec.Command("useradd", "mew", "-G", "mewtwo")
	session, err = gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
	Expect(err).NotTo(HaveOccurred())
	Eventually(session).Should(gexec.Exit(0))
})
<commit_msg>Reduce flakiness of greenskeeper suite<commit_after>package greenskeeper

import (
	"os/exec"
	"os/user"
	"testing"
	"time"

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

func TestGreenskeeper(t *testing.T) {
	RegisterFailHandler(Fail)
	RunSpecs(t, "Greenskeeper Suite")
}

var _ = BeforeSuite(func() {
	if _, err := user.LookupGroup("mewtwo"); err == user.UnknownGroupError("mewtwo") {
		createGroup("mewtwo")
	} else {
		Expect(err).NotTo(HaveOccurred())
	}

	if _, err := user.Lookup("mew"); err == user.UnknownUserError("mew") {
		createUser("mew", "mewtwo")
	} else {
		Expect(err).NotTo(HaveOccurred())
	}
})

func createGroup(name string) {
	cmd := exec.Command("groupadd", name)
	session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
	Expect(err).NotTo(HaveOccurred())
	Eventually(session, time.Second*5).Should(gexec.Exit(0))
}

func createUser(name, group string) {
	cmd := exec.Command("useradd", name, "-G", group)
	session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
	Expect(err).NotTo(HaveOccurred())
	Eventually(session, time.Second*5).Should(gexec.Exit(0))
}
<|endoftext|>


<commit_before>package terraform

import (
	"fmt"
	"log"
	"sync"
	"sync/atomic"

	"github.com/hashicorp/terraform/config"
	"github.com/hashicorp/terraform/depgraph"
	"github.com/hashicorp/terraform/helper/multierror"
)

// This is a function type used to implement a walker for the resource
// tree internally on the Terraform structure.
type genericWalkFunc func(*Resource) (map[string]string, error)

// Context represents all the context that Terraform needs in order to
// perform operations on infrastructure. This structure is built using
// ContextOpts and NewContext. See the documentation for those.
//
// Additionally, a context can be created from a Plan using Plan.Context.
type Context struct {
	config    *config.Config
	diff      *Diff
	hooks     []Hook
	state     *State
	providers map[string]ResourceProviderFactory
	variables map[string]string

	l     sync.Mutex
	cond  *sync.Cond
	runCh <-chan struct{}
	sh    *stopHook
}

// ContextOpts are the user-creatable configuration structure to create
// a context with NewContext.
type ContextOpts struct {
	Config    *config.Config
	Diff      *Diff
	Hooks     []Hook
	State     *State
	Providers map[string]ResourceProviderFactory
	Variables map[string]string
}

// NewContext creates a new context.
//
// Once a context is created, the pointer values within ContextOpts should
// not be mutated in any way, since the pointers are copied, not the values
// themselves.
func NewContext(opts *ContextOpts) *Context {
	sh := new(stopHook)

	// Copy all the hooks and add our stop hook. We don't append directly
	// to the Config so that we're not modifying that in-place.
	hooks := make([]Hook, len(opts.Hooks)+1)
	copy(hooks, opts.Hooks)
	hooks[len(opts.Hooks)] = sh

	return &Context{
		config:    opts.Config,
		diff:      opts.Diff,
		hooks:     hooks,
		state:     opts.State,
		providers: opts.Providers,
		variables: opts.Variables,

		cond: sync.NewCond(new(sync.Mutex)),
		sh:   sh,
	}
}

// Apply applies the changes represented by this context and returns
// the resulting state.
//
// In addition to returning the resulting state, this context is updated
// with the latest state.
func (c *Context) Apply() (*State, error) {
	v := c.acquireRun()
	defer c.releaseRun(v)

	g, err := Graph(&GraphOpts{
		Config:    c.config,
		Diff:      c.diff,
		Providers: c.providers,
		State:     c.state,
	})
	if err != nil {
		return nil, err
	}

	// Create our result. Make sure we preserve the prior states
	s := new(State)
	s.init()
	if c.state != nil {
		for k, v := range c.state.Resources {
			s.Resources[k] = v
		}
	}

	// Walk
	err = g.Walk(c.applyWalkFn(s))

	// Update our state, even if we have an error, for partial updates
	c.state = s

	return s, err
}

// Plan generates an execution plan for the given context.
//
// The execution plan encapsulates the context and can be stored
// in order to reinstantiate a context later for Apply.
//
// Plan also updates the diff of this context to be the diff generated
// by the plan, so Apply can be called after.
func (c *Context) Plan(opts *PlanOpts) (*Plan, error) {
	v := c.acquireRun()
	defer c.releaseRun(v)

	g, err := Graph(&GraphOpts{
		Config:    c.config,
		Providers: c.providers,
		State:     c.state,
	})
	if err != nil {
		return nil, err
	}

	p := &Plan{
		Config: c.config,
		Vars:   c.variables,
		State:  c.state,
	}
	err = g.Walk(c.planWalkFn(p, opts))

	// Update the diff so that our context is up-to-date
	c.diff = p.Diff

	return p, err
}

// Refresh goes through all the resources in the state and refreshes them
// to their latest state. This will update the state that this context
// works with, along with returning it.
//
// Even in the case an error is returned, the state will be returned and
// will potentially be partially updated.
func (c *Context) Refresh() (*State, error) {
	v := c.acquireRun()
	defer c.releaseRun(v)

	g, err := Graph(&GraphOpts{
		Config:    c.config,
		Providers: c.providers,
		State:     c.state,
	})
	if err != nil {
		return c.state, err
	}

	s := new(State)
	s.init()
	err = g.Walk(c.refreshWalkFn(s))

	// Update our state
	c.state = s

	return s, err
}

// Stop stops the running task.
//
// Stop will block until the task completes.
func (c *Context) Stop() {
	c.l.Lock()
	ch := c.runCh

	// If we aren't running, then just return
	if ch == nil {
		c.l.Unlock()
		return
	}

	// Tell the hook we want to stop
	c.sh.Stop()

	// Wait for us to stop
	c.l.Unlock()
	<-ch
}

// Validate validates the configuration and returns any warnings or errors.
func (c *Context) Validate() ([]string, []error) {
	var rerr *multierror.Error

	// Validate the configuration itself
	if err := c.config.Validate(); err != nil {
		rerr = multierror.ErrorAppend(rerr, err)
	}

	// Validate the user variables
	if errs := smcUserVariables(c.config, c.variables); len(errs) > 0 {
		rerr = multierror.ErrorAppend(rerr, errs...)
	}

	var errs []error
	if rerr != nil && len(rerr.Errors) > 0 {
		errs = rerr.Errors
	}

	return nil, errs
}

func (c *Context) acquireRun() chan<- struct{} {
	c.l.Lock()
	defer c.l.Unlock()

	// Wait for no channel to exist
	for c.runCh != nil {
		c.l.Unlock()
		ch := c.runCh
		<-ch
		c.l.Lock()
	}

	ch := make(chan struct{})
	c.runCh = ch
	return ch
}

func (c *Context) releaseRun(ch chan<- struct{}) {
	c.l.Lock()
	defer c.l.Unlock()

	close(ch)
	c.runCh = nil
	c.sh.Reset()
}

func (c *Context) applyWalkFn(result *State) depgraph.WalkFunc {
	var l sync.Mutex

	// Initialize the result
	result.init()

	cb := func(r *Resource) (map[string]string, error) {
		diff := r.Diff
		if diff.Empty() {
			return r.Vars(), nil
		}

		if !diff.Destroy {
			var err error
			diff, err = r.Provider.Diff(r.State, r.Config)
			if err != nil {
				return nil, err
			}
		}

		// TODO(mitchellh): we need to verify the diff doesn't change
		// anything and that the diff has no computed values (pre-computed)

		for _, h := range c.hooks {
			handleHook(h.PreApply(r.Id, r.State, diff))
		}

		// With the completed diff, apply!
		log.Printf("[DEBUG] %s: Executing Apply", r.Id)
		rs, err := r.Provider.Apply(r.State, diff)
		if err != nil {
			return nil, err
		}

		// Make sure the result is instantiated
		if rs == nil {
			rs = new(ResourceState)
		}

		// Force the resource state type to be our type
		rs.Type = r.State.Type

		var errs []error
		for ak, av := range rs.Attributes {
			// If the value is the unknown variable value, then it is an error.
			// In this case we record the error and remove it from the state
			if av == config.UnknownVariableValue {
				errs = append(errs, fmt.Errorf(
					"Attribute with unknown value: %s", ak))
				delete(rs.Attributes, ak)
			}
		}

		// Update the resulting diff
		l.Lock()
		if rs.ID == "" {
			delete(result.Resources, r.Id)
		} else {
			result.Resources[r.Id] = rs
		}
		l.Unlock()

		// Update the state for the resource itself
		r.State = rs

		for _, h := range c.hooks {
			handleHook(h.PostApply(r.Id, r.State))
		}

		// Determine the new state and update variables
		err = nil
		if len(errs) > 0 {
			err = &multierror.Error{Errors: errs}
		}

		return r.Vars(), err
	}

	return c.genericWalkFn(c.variables, cb)
}

func (c *Context) planWalkFn(result *Plan, opts *PlanOpts) depgraph.WalkFunc {
	var l sync.Mutex

	// If we were given nil options, instantiate it
	if opts == nil {
		opts = new(PlanOpts)
	}

	// Initialize the result
	result.init()

	cb := func(r *Resource) (map[string]string, error) {
		var diff *ResourceDiff

		for _, h := range c.hooks {
			handleHook(h.PreDiff(r.Id, r.State))
		}

		if opts.Destroy {
			if r.State.ID != "" {
				log.Printf("[DEBUG] %s: Making for destroy", r.Id)
				diff = &ResourceDiff{Destroy: true}
			} else {
				log.Printf("[DEBUG] %s: Not marking for destroy, no ID", r.Id)
			}
		} else if r.Config == nil {
			log.Printf("[DEBUG] %s: Orphan, marking for destroy", r.Id)

			// This is an orphan (no config), so we mark it to be destroyed
			diff = &ResourceDiff{Destroy: true}
		} else {
			log.Printf("[DEBUG] %s: Executing diff", r.Id)

			// Get a diff from the newest state
			var err error
			diff, err = r.Provider.Diff(r.State, r.Config)
			if err != nil {
				return nil, err
			}
		}

		l.Lock()
		if !diff.Empty() {
			result.Diff.Resources[r.Id] = diff
		}
		l.Unlock()

		for _, h := range c.hooks {
			handleHook(h.PostDiff(r.Id, diff))
		}

		// Determine the new state and update variables
		if !diff.Empty() {
			r.State = r.State.MergeDiff(diff)
		}

		return r.Vars(), nil
	}

	return c.genericWalkFn(c.variables, cb)
}

func (c *Context) refreshWalkFn(result *State) depgraph.WalkFunc {
	var l sync.Mutex

	cb := func(r *Resource) (map[string]string, error) {
		for _, h := range c.hooks {
			handleHook(h.PreRefresh(r.Id, r.State))
		}

		rs, err := r.Provider.Refresh(r.State)
		if err != nil {
			return nil, err
		}
		if rs == nil {
			rs = new(ResourceState)
		}

		// Fix the type to be the type we have
		rs.Type = r.State.Type

		l.Lock()
		result.Resources[r.Id] = rs
		l.Unlock()

		for _, h := range c.hooks {
			handleHook(h.PostRefresh(r.Id, rs))
		}

		return nil, nil
	}

	return c.genericWalkFn(c.variables, cb)
}

func (c *Context) genericWalkFn(
	invars map[string]string,
	cb genericWalkFunc) depgraph.WalkFunc {
	var l sync.RWMutex

	// Initialize the variables for application
	vars := make(map[string]string)
	for k, v := range invars {
		vars[fmt.Sprintf("var.%s", k)] = v
	}

	// This will keep track of whether we're stopped or not
	var stop uint32 = 0

	return func(n *depgraph.Noun) error {
		// If it is the root node, ignore
		if n.Name == GraphRootNode {
			return nil
		}

		// If we're stopped, return right away
		if atomic.LoadUint32(&stop) != 0 {
			return nil
		}

		switch m := n.Meta.(type) {
		case *GraphNodeResource:
		case *GraphNodeResourceProvider:
			var rc *ResourceConfig
			if m.Config != nil {
				if err := m.Config.RawConfig.Interpolate(vars); err != nil {
					panic(err)
				}
				rc = NewResourceConfig(m.Config.RawConfig)
			}

			for k, p := range m.Providers {
				log.Printf("[INFO] Configuring provider: %s", k)
				err := p.Configure(rc)
				if err != nil {
					return err
				}
			}

			return nil
		}

		rn := n.Meta.(*GraphNodeResource)

		l.RLock()
		if len(vars) > 0 && rn.Config != nil {
			if err := rn.Config.RawConfig.Interpolate(vars); err != nil {
				panic(fmt.Sprintf("Interpolate error: %s", err))
			}

			// Force the config to be set later
			rn.Resource.Config = nil
		}
		l.RUnlock()

		// Make sure that at least some resource configuration is set
		if !rn.Orphan {
			if rn.Resource.Config == nil {
				if rn.Config == nil {
					rn.Resource.Config = new(ResourceConfig)
				} else {
					rn.Resource.Config = NewResourceConfig(rn.Config.RawConfig)
				}
			}
		} else {
			rn.Resource.Config = nil
		}

		// Handle recovery of special panic scenarios
		defer func() {
			if v := recover(); v != nil {
				if v == HookActionHalt {
					atomic.StoreUint32(&stop, 1)
				} else {
					panic(v)
				}
			}
		}()

		// Call the callack
		log.Printf("[INFO] Walking: %s", rn.Resource.Id)
		newVars, err := cb(rn.Resource)
		if err != nil {
			return err
		}

		if len(newVars) > 0 {
			// Acquire a lock since this function is called in parallel
			l.Lock()
			defer l.Unlock()

			// Update variables
			for k, v := range newVars {
				vars[k] = v
			}
		}

		return nil
	}
}
<commit_msg>terraform: Context doesn't need a sync.Cond<commit_after>package terraform

import (
	"fmt"
	"log"
	"sync"
	"sync/atomic"

	"github.com/hashicorp/terraform/config"
	"github.com/hashicorp/terraform/depgraph"
	"github.com/hashicorp/terraform/helper/multierror"
)

// This is a function type used to implement a walker for the resource
// tree internally on the Terraform structure.
type genericWalkFunc func(*Resource) (map[string]string, error)

// Context represents all the context that Terraform needs in order to
// perform operations on infrastructure. This structure is built using
// ContextOpts and NewContext. See the documentation for those.
//
// Additionally, a context can be created from a Plan using Plan.Context.
type Context struct {
	config    *config.Config
	diff      *Diff
	hooks     []Hook
	state     *State
	providers map[string]ResourceProviderFactory
	variables map[string]string

	l     sync.Mutex
	runCh <-chan struct{}
	sh    *stopHook
}

// ContextOpts are the user-creatable configuration structure to create
// a context with NewContext.
type ContextOpts struct {
	Config    *config.Config
	Diff      *Diff
	Hooks     []Hook
	State     *State
	Providers map[string]ResourceProviderFactory
	Variables map[string]string
}

// NewContext creates a new context.
//
// Once a context is created, the pointer values within ContextOpts should
// not be mutated in any way, since the pointers are copied, not the values
// themselves.
func NewContext(opts *ContextOpts) *Context {
	sh := new(stopHook)

	// Copy all the hooks and add our stop hook. We don't append directly
	// to the Config so that we're not modifying that in-place.
	hooks := make([]Hook, len(opts.Hooks)+1)
	copy(hooks, opts.Hooks)
	hooks[len(opts.Hooks)] = sh

	return &Context{
		config:    opts.Config,
		diff:      opts.Diff,
		hooks:     hooks,
		state:     opts.State,
		providers: opts.Providers,
		variables: opts.Variables,

		sh:   sh,
	}
}

// Apply applies the changes represented by this context and returns
// the resulting state.
//
// In addition to returning the resulting state, this context is updated
// with the latest state.
func (c *Context) Apply() (*State, error) {
	v := c.acquireRun()
	defer c.releaseRun(v)

	g, err := Graph(&GraphOpts{
		Config:    c.config,
		Diff:      c.diff,
		Providers: c.providers,
		State:     c.state,
	})
	if err != nil {
		return nil, err
	}

	// Create our result. Make sure we preserve the prior states
	s := new(State)
	s.init()
	if c.state != nil {
		for k, v := range c.state.Resources {
			s.Resources[k] = v
		}
	}

	// Walk
	err = g.Walk(c.applyWalkFn(s))

	// Update our state, even if we have an error, for partial updates
	c.state = s

	return s, err
}

// Plan generates an execution plan for the given context.
//
// The execution plan encapsulates the context and can be stored
// in order to reinstantiate a context later for Apply.
//
// Plan also updates the diff of this context to be the diff generated
// by the plan, so Apply can be called after.
func (c *Context) Plan(opts *PlanOpts) (*Plan, error) {
	v := c.acquireRun()
	defer c.releaseRun(v)

	g, err := Graph(&GraphOpts{
		Config:    c.config,
		Providers: c.providers,
		State:     c.state,
	})
	if err != nil {
		return nil, err
	}

	p := &Plan{
		Config: c.config,
		Vars:   c.variables,
		State:  c.state,
	}
	err = g.Walk(c.planWalkFn(p, opts))

	// Update the diff so that our context is up-to-date
	c.diff = p.Diff

	return p, err
}

// Refresh goes through all the resources in the state and refreshes them
// to their latest state. This will update the state that this context
// works with, along with returning it.
//
// Even in the case an error is returned, the state will be returned and
// will potentially be partially updated.
func (c *Context) Refresh() (*State, error) {
	v := c.acquireRun()
	defer c.releaseRun(v)

	g, err := Graph(&GraphOpts{
		Config:    c.config,
		Providers: c.providers,
		State:     c.state,
	})
	if err != nil {
		return c.state, err
	}

	s := new(State)
	s.init()
	err = g.Walk(c.refreshWalkFn(s))

	// Update our state
	c.state = s

	return s, err
}

// Stop stops the running task.
//
// Stop will block until the task completes.
func (c *Context) Stop() {
	c.l.Lock()
	ch := c.runCh

	// If we aren't running, then just return
	if ch == nil {
		c.l.Unlock()
		return
	}

	// Tell the hook we want to stop
	c.sh.Stop()

	// Wait for us to stop
	c.l.Unlock()
	<-ch
}

// Validate validates the configuration and returns any warnings or errors.
func (c *Context) Validate() ([]string, []error) {
	var rerr *multierror.Error

	// Validate the configuration itself
	if err := c.config.Validate(); err != nil {
		rerr = multierror.ErrorAppend(rerr, err)
	}

	// Validate the user variables
	if errs := smcUserVariables(c.config, c.variables); len(errs) > 0 {
		rerr = multierror.ErrorAppend(rerr, errs...)
	}

	var errs []error
	if rerr != nil && len(rerr.Errors) > 0 {
		errs = rerr.Errors
	}

	return nil, errs
}

func (c *Context) acquireRun() chan<- struct{} {
	c.l.Lock()
	defer c.l.Unlock()

	// Wait for no channel to exist
	for c.runCh != nil {
		c.l.Unlock()
		ch := c.runCh
		<-ch
		c.l.Lock()
	}

	ch := make(chan struct{})
	c.runCh = ch
	return ch
}

func (c *Context) releaseRun(ch chan<- struct{}) {
	c.l.Lock()
	defer c.l.Unlock()

	close(ch)
	c.runCh = nil
	c.sh.Reset()
}

func (c *Context) applyWalkFn(result *State) depgraph.WalkFunc {
	var l sync.Mutex

	// Initialize the result
	result.init()

	cb := func(r *Resource) (map[string]string, error) {
		diff := r.Diff
		if diff.Empty() {
			return r.Vars(), nil
		}

		if !diff.Destroy {
			var err error
			diff, err = r.Provider.Diff(r.State, r.Config)
			if err != nil {
				return nil, err
			}
		}

		// TODO(mitchellh): we need to verify the diff doesn't change
		// anything and that the diff has no computed values (pre-computed)

		for _, h := range c.hooks {
			handleHook(h.PreApply(r.Id, r.State, diff))
		}

		// With the completed diff, apply!
		log.Printf("[DEBUG] %s: Executing Apply", r.Id)
		rs, err := r.Provider.Apply(r.State, diff)
		if err != nil {
			return nil, err
		}

		// Make sure the result is instantiated
		if rs == nil {
			rs = new(ResourceState)
		}

		// Force the resource state type to be our type
		rs.Type = r.State.Type

		var errs []error
		for ak, av := range rs.Attributes {
			// If the value is the unknown variable value, then it is an error.
			// In this case we record the error and remove it from the state
			if av == config.UnknownVariableValue {
				errs = append(errs, fmt.Errorf(
					"Attribute with unknown value: %s", ak))
				delete(rs.Attributes, ak)
			}
		}

		// Update the resulting diff
		l.Lock()
		if rs.ID == "" {
			delete(result.Resources, r.Id)
		} else {
			result.Resources[r.Id] = rs
		}
		l.Unlock()

		// Update the state for the resource itself
		r.State = rs

		for _, h := range c.hooks {
			handleHook(h.PostApply(r.Id, r.State))
		}

		// Determine the new state and update variables
		err = nil
		if len(errs) > 0 {
			err = &multierror.Error{Errors: errs}
		}

		return r.Vars(), err
	}

	return c.genericWalkFn(c.variables, cb)
}

func (c *Context) planWalkFn(result *Plan, opts *PlanOpts) depgraph.WalkFunc {
	var l sync.Mutex

	// If we were given nil options, instantiate it
	if opts == nil {
		opts = new(PlanOpts)
	}

	// Initialize the result
	result.init()

	cb := func(r *Resource) (map[string]string, error) {
		var diff *ResourceDiff

		for _, h := range c.hooks {
			handleHook(h.PreDiff(r.Id, r.State))
		}

		if opts.Destroy {
			if r.State.ID != "" {
				log.Printf("[DEBUG] %s: Making for destroy", r.Id)
				diff = &ResourceDiff{Destroy: true}
			} else {
				log.Printf("[DEBUG] %s: Not marking for destroy, no ID", r.Id)
			}
		} else if r.Config == nil {
			log.Printf("[DEBUG] %s: Orphan, marking for destroy", r.Id)

			// This is an orphan (no config), so we mark it to be destroyed
			diff = &ResourceDiff{Destroy: true}
		} else {
			log.Printf("[DEBUG] %s: Executing diff", r.Id)

			// Get a diff from the newest state
			var err error
			diff, err = r.Provider.Diff(r.State, r.Config)
			if err != nil {
				return nil, err
			}
		}

		l.Lock()
		if !diff.Empty() {
			result.Diff.Resources[r.Id] = diff
		}
		l.Unlock()

		for _, h := range c.hooks {
			handleHook(h.PostDiff(r.Id, diff))
		}

		// Determine the new state and update variables
		if !diff.Empty() {
			r.State = r.State.MergeDiff(diff)
		}

		return r.Vars(), nil
	}

	return c.genericWalkFn(c.variables, cb)
}

func (c *Context) refreshWalkFn(result *State) depgraph.WalkFunc {
	var l sync.Mutex

	cb := func(r *Resource) (map[string]string, error) {
		for _, h := range c.hooks {
			handleHook(h.PreRefresh(r.Id, r.State))
		}

		rs, err := r.Provider.Refresh(r.State)
		if err != nil {
			return nil, err
		}
		if rs == nil {
			rs = new(ResourceState)
		}

		// Fix the type to be the type we have
		rs.Type = r.State.Type

		l.Lock()
		result.Resources[r.Id] = rs
		l.Unlock()

		for _, h := range c.hooks {
			handleHook(h.PostRefresh(r.Id, rs))
		}

		return nil, nil
	}

	return c.genericWalkFn(c.variables, cb)
}

func (c *Context) genericWalkFn(
	invars map[string]string,
	cb genericWalkFunc) depgraph.WalkFunc {
	var l sync.RWMutex

	// Initialize the variables for application
	vars := make(map[string]string)
	for k, v := range invars {
		vars[fmt.Sprintf("var.%s", k)] = v
	}

	// This will keep track of whether we're stopped or not
	var stop uint32 = 0

	return func(n *depgraph.Noun) error {
		// If it is the root node, ignore
		if n.Name == GraphRootNode {
			return nil
		}

		// If we're stopped, return right away
		if atomic.LoadUint32(&stop) != 0 {
			return nil
		}

		switch m := n.Meta.(type) {
		case *GraphNodeResource:
		case *GraphNodeResourceProvider:
			var rc *ResourceConfig
			if m.Config != nil {
				if err := m.Config.RawConfig.Interpolate(vars); err != nil {
					panic(err)
				}
				rc = NewResourceConfig(m.Config.RawConfig)
			}

			for k, p := range m.Providers {
				log.Printf("[INFO] Configuring provider: %s", k)
				err := p.Configure(rc)
				if err != nil {
					return err
				}
			}

			return nil
		}

		rn := n.Meta.(*GraphNodeResource)

		l.RLock()
		if len(vars) > 0 && rn.Config != nil {
			if err := rn.Config.RawConfig.Interpolate(vars); err != nil {
				panic(fmt.Sprintf("Interpolate error: %s", err))
			}

			// Force the config to be set later
			rn.Resource.Config = nil
		}
		l.RUnlock()

		// Make sure that at least some resource configuration is set
		if !rn.Orphan {
			if rn.Resource.Config == nil {
				if rn.Config == nil {
					rn.Resource.Config = new(ResourceConfig)
				} else {
					rn.Resource.Config = NewResourceConfig(rn.Config.RawConfig)
				}
			}
		} else {
			rn.Resource.Config = nil
		}

		// Handle recovery of special panic scenarios
		defer func() {
			if v := recover(); v != nil {
				if v == HookActionHalt {
					atomic.StoreUint32(&stop, 1)
				} else {
					panic(v)
				}
			}
		}()

		// Call the callack
		log.Printf("[INFO] Walking: %s", rn.Resource.Id)
		newVars, err := cb(rn.Resource)
		if err != nil {
			return err
		}

		if len(newVars) > 0 {
			// Acquire a lock since this function is called in parallel
			l.Lock()
			defer l.Unlock()

			// Update variables
			for k, v := range newVars {
				vars[k] = v
			}
		}

		return nil
	}
}
<|endoftext|>


<commit_before>package terraform

import (
	"fmt"

	"github.com/hashicorp/go-version"
)

// The main version number that is being run at the moment.
const Version = "0.10.3"

// A pre-release marker for the version. If this is "" (empty string)
// then it means that it is a final release. Otherwise, this is a pre-release
// such as "dev" (in development), "beta", "rc1", etc.
var VersionPrerelease = "dev"

// SemVersion is an instance of version.Version. This has the secondary
// benefit of verifying during tests and init time that our version is a
// proper semantic version, which should always be the case.
var SemVersion = version.Must(version.NewVersion(Version))

// VersionHeader is the header name used to send the current terraform version
// in http requests.
const VersionHeader = "Terraform-Version"

func VersionString() string {
	if VersionPrerelease != "" {
		return fmt.Sprintf("%s-%s", Version, VersionPrerelease)
	}
	return Version
}
<commit_msg>v0.10.3<commit_after>package terraform

import (
	"fmt"

	"github.com/hashicorp/go-version"
)

// The main version number that is being run at the moment.
const Version = "0.10.3"

// A pre-release marker for the version. If this is "" (empty string)
// then it means that it is a final release. Otherwise, this is a pre-release
// such as "dev" (in development), "beta", "rc1", etc.
var VersionPrerelease = ""

// SemVersion is an instance of version.Version. This has the secondary
// benefit of verifying during tests and init time that our version is a
// proper semantic version, which should always be the case.
var SemVersion = version.Must(version.NewVersion(Version))

// VersionHeader is the header name used to send the current terraform version
// in http requests.
const VersionHeader = "Terraform-Version"

func VersionString() string {
	if VersionPrerelease != "" {
		return fmt.Sprintf("%s-%s", Version, VersionPrerelease)
	}
	return Version
}
<|endoftext|>


<commit_before>package body_test

import (
	"context"
	"io/ioutil"
	"net/http"
	"testing"

	"encoding/json"
	"reflect"

	"go.delic.rs/cliware"
	"go.delic.rs/cliware-middlewares/body"
)

func TestString(t *testing.T) {
	for _, data := range []struct {
		Data           string
		Method         string
		ExpectedMethod string
	}{
		{"my content", "GET", "POST"},
		{"", "POST", ""},
		{"š", "GET", "POST"},
	} {
		middleware := body.String(data.Data)
		ctx := context.Background()
		req := cliware.EmptyRequest()
		req.Method = data.Method
		h := createHandler()
		_, err := middleware.Exec(h).Handle(ctx, req)
		if err != nil {
			t.Error("Got error processing request: ", err)
		}

		expectedMethod := data.ExpectedMethod
		if expectedMethod == "" {
			expectedMethod = data.Method
		}
		if req.Method != expectedMethod {
			t.Errorf("Wrong method on request. Expected: \"%s\", got \"%s\"", expectedMethod, req.Method)
		}
		contentLength := int64(len([]byte(data.Data)))
		if req.ContentLength != contentLength {
			t.Errorf("Wrong content length. Expected: %d, got %d.", contentLength, req.ContentLength)
		}
		bodyBytes, _ := ioutil.ReadAll(req.Body)
		body := string(bodyBytes)
		if body != data.Data {
			t.Errorf("Wrong body. Expected: \"%s\", got: \"%s\".", data.Data, body)
		}
	}
}

func TestJSON(t *testing.T) {
	for _, data := range []struct {
		Data          interface{}
		ResponseData  interface{}
		ExpectedError error
	}{
		{`{"foo": "bar"}`, &map[string]string{"foo": "bar"}, nil},
		{[]byte(`{"foo": "bar"}`), &map[string]string{"foo": "bar"}, nil},
		{
			struct {
				Foo string `json:"foo"`
			}{
				Foo: "bar",
			}, &map[string]string{"foo": "bar"}, nil},
	} {
		req := cliware.EmptyRequest()
		handler := createHandler()
		_, err := body.JSON(data.Data).Exec(handler).Handle(nil, req)
		if data.ExpectedError != nil {
			if data.ExpectedError != err {
				t.Errorf("Wrong error. Expected: %s, got: %s", data.ExpectedError, err)
			}
		} else {
			if err != nil {
				t.Error("Got unexpected error processing request: ", err)
			}
		}

		if req.Method != "POST" {
			t.Error("Wrong request method. Expected: POST, got: ", req.Method)
		}
		if req.Header.Get("Content-Type") != "application/json" {
			t.Error("Wrong content-type. Expected application/json, got: ", req.Header.Get("Content-Type"))
		}
		bodyBytes, _ := ioutil.ReadAll(req.Body)
		body := new(map[string]string)
		err = json.Unmarshal(bodyBytes, body)
		if err != nil {
			t.Error("Failed to unmarshal response json: ", err)
		}
		if !reflect.DeepEqual(body, data.ResponseData) {
			t.Errorf("Wrong body. Expected: %v, got: %v.", data.ResponseData, body)
		}
	}
}

func createHandler() cliware.Handler {
	return cliware.HandlerFunc(func(ctx context.Context, req *http.Request) (resp *http.Response, err error) {
		return nil, nil
	})
}

// type handler struct {
// 	// fn     m.RequestHandler
// 	called bool
// }

// func (h *handler) Handle(ctx context.Context, req *http.Request) (resp *http.Response, err error) {
// 	h.called = true
// 	return nil, nil
// }

// func newHandler() *handler {
// 	h := &handler{}
// 	// h.fn = m.RequestHandlerFunc(func(ctx context.Context, req *http.Request) (resp *http.Response, err error) {
// 	// 	h.called = true
// 	// })
// 	return h
// }
<commit_msg>Delete some commented code.<commit_after>package body_test

import (
	"context"
	"io/ioutil"
	"net/http"
	"testing"

	"encoding/json"
	"reflect"

	"go.delic.rs/cliware"
	"go.delic.rs/cliware-middlewares/body"
)

func TestString(t *testing.T) {
	for _, data := range []struct {
		Data           string
		Method         string
		ExpectedMethod string
	}{
		{"my content", "GET", "POST"},
		{"", "POST", ""},
		{"š", "GET", "POST"},
	} {
		middleware := body.String(data.Data)
		ctx := context.Background()
		req := cliware.EmptyRequest()
		req.Method = data.Method
		h := createHandler()
		_, err := middleware.Exec(h).Handle(ctx, req)
		if err != nil {
			t.Error("Got error processing request: ", err)
		}

		expectedMethod := data.ExpectedMethod
		if expectedMethod == "" {
			expectedMethod = data.Method
		}
		if req.Method != expectedMethod {
			t.Errorf("Wrong method on request. Expected: \"%s\", got \"%s\"", expectedMethod, req.Method)
		}
		contentLength := int64(len([]byte(data.Data)))
		if req.ContentLength != contentLength {
			t.Errorf("Wrong content length. Expected: %d, got %d.", contentLength, req.ContentLength)
		}
		bodyBytes, _ := ioutil.ReadAll(req.Body)
		body := string(bodyBytes)
		if body != data.Data {
			t.Errorf("Wrong body. Expected: \"%s\", got: \"%s\".", data.Data, body)
		}
	}
}

func TestJSON(t *testing.T) {
	for _, data := range []struct {
		Data          interface{}
		ResponseData  interface{}
		ExpectedError error
	}{
		{`{"foo": "bar"}`, &map[string]string{"foo": "bar"}, nil},
		{[]byte(`{"foo": "bar"}`), &map[string]string{"foo": "bar"}, nil},
		{
			struct {
				Foo string `json:"foo"`
			}{
				Foo: "bar",
			}, &map[string]string{"foo": "bar"}, nil},
	} {
		req := cliware.EmptyRequest()
		handler := createHandler()
		_, err := body.JSON(data.Data).Exec(handler).Handle(nil, req)
		if data.ExpectedError != nil {
			if data.ExpectedError != err {
				t.Errorf("Wrong error. Expected: %s, got: %s", data.ExpectedError, err)
			}
		} else {
			if err != nil {
				t.Error("Got unexpected error processing request: ", err)
			}
		}

		if req.Method != "POST" {
			t.Error("Wrong request method. Expected: POST, got: ", req.Method)
		}
		if req.Header.Get("Content-Type") != "application/json" {
			t.Error("Wrong content-type. Expected application/json, got: ", req.Header.Get("Content-Type"))
		}
		bodyBytes, _ := ioutil.ReadAll(req.Body)
		body := new(map[string]string)
		err = json.Unmarshal(bodyBytes, body)
		if err != nil {
			t.Error("Failed to unmarshal response json: ", err)
		}
		if !reflect.DeepEqual(body, data.ResponseData) {
			t.Errorf("Wrong body. Expected: %v, got: %v.", data.ResponseData, body)
		}
	}
}

func createHandler() cliware.Handler {
	return cliware.HandlerFunc(func(ctx context.Context, req *http.Request) (resp *http.Response, err error) {
		return nil, nil
	})
}
<|endoftext|>


<commit_before>package brocadevtm

/*
import (
	"time"
	"log"
)

import (
	"github.com/sky-uk/go-rest-api"
	"log"
	"time"
)

// Config is a struct for containing the provider parameters.
type Config struct {
	Debug       bool
	Insecure    bool
	VTMUser     string
	VTMPassword string
	VTMServer   string
	Headers     map[string]string
	Timeout     time.Duration
}

// Client returns a new client for accessing VMWare vSphere.
func (c *Config) Client() (*rest.Client, error) {
	log.Printf("[INFO] Brocade vTM Client configured for URL: %s", c.VTMServer)
	vtmClient := rest.Client{"https://" + c.VTMServer, c.VTMUser, c.VTMPassword, c.Insecure, c.Debug, c.Headers, c.Timeout}
	return &vtmClient, nil
}
*/
<commit_msg>Removed config.go - no longer required<commit_after><|endoftext|>


<commit_before>// Copyright (c) Alex Ellis 2017. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

package proxy

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
	"time"

	"github.com/openfaas/faas-cli/stack"
	"github.com/openfaas/faas/gateway/requests"
)

// FunctionResourceRequest defines a request to set function resources
type FunctionResourceRequest struct {
	Limits   *stack.FunctionResources
	Requests *stack.FunctionResources
}

// DeployFunction call FaaS server to deploy a new function
func DeployFunction(fprocess string, gateway string, functionName string, image string,
	language string, replace bool, envVars map[string]string, network string,
	constraints []string, update bool, secrets []string, labels map[string]string, functionResourceRequest1 FunctionResourceRequest) {

	// Need to alter Gateway to allow nil/empty string as fprocess, to avoid this repetition.
	var fprocessTemplate string
	if len(fprocess) > 0 {
		fprocessTemplate = fprocess
	}

	gateway = strings.TrimRight(gateway, "/")

	if replace {
		DeleteFunction(gateway, functionName)
	}

	req := requests.CreateFunctionRequest{
		EnvProcess:  fprocessTemplate,
		Image:       image,
		Network:     network,
		Service:     functionName,
		EnvVars:     envVars,
		Constraints: constraints,
		Secrets:     secrets, // TODO: allow registry auth to be specified or read from local Docker credentials store
		Labels:      &labels,
	}

	req.Limits = &requests.FunctionResources{}
	req.Requests = &requests.FunctionResources{}

	if functionResourceRequest1.Limits != nil && len(functionResourceRequest1.Limits.Memory) > 0 {
		req.Limits.Memory = functionResourceRequest1.Limits.Memory
	}

	if functionResourceRequest1.Requests != nil && len(functionResourceRequest1.Requests.Memory) > 0 {
		req.Requests.Memory = functionResourceRequest1.Requests.Memory
	}

	if functionResourceRequest1.Limits != nil && len(functionResourceRequest1.Limits.CPU) > 0 {
		req.Limits.CPU = functionResourceRequest1.Limits.CPU
	}

	if functionResourceRequest1.Requests != nil && len(functionResourceRequest1.Requests.CPU) > 0 {
		req.Requests.CPU = functionResourceRequest1.Requests.CPU
	}

	reqBytes, _ := json.Marshal(&req)
	reader := bytes.NewReader(reqBytes)
	var request *http.Request

	timeout := 60 * time.Second
	client := MakeHTTPClient(&timeout)

	method := http.MethodPost
	// "application/json"
	if update {
		method = http.MethodPut
	}

	var err error
	request, err = http.NewRequest(method, gateway+"/system/functions", reader)
	SetAuth(request, gateway)
	if err != nil {
		fmt.Println(err)
		return
	}

	res, err := client.Do(request)
	if err != nil {
		fmt.Println("Is FaaS deployed? Do you need to specify the --gateway flag?")
		fmt.Println(err)
		return
	}

	if res.Body != nil {
		defer res.Body.Close()
	}

	switch res.StatusCode {
	case http.StatusOK, http.StatusCreated, http.StatusAccepted:
		if update {
			fmt.Println("Updated.")
		} else {
			fmt.Println("Deployed.")
		}

		deployedURL := fmt.Sprintf("URL: %s/function/%s\n", gateway, functionName)
		fmt.Println(deployedURL)
	case http.StatusUnauthorized:
		fmt.Println("unauthorized access, run \"faas-cli login\" to setup authentication for this server")
	default:
		bytesOut, err := ioutil.ReadAll(res.Body)
		if err == nil {
			fmt.Printf("Unexpected status: %d, message: %s\n", res.StatusCode, string(bytesOut))
		}
	}

	fmt.Println(res.Status)
}
<commit_msg>Set requests/limits to nil when not required<commit_after>// Copyright (c) Alex Ellis 2017. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

package proxy

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
	"time"

	"github.com/openfaas/faas-cli/stack"
	"github.com/openfaas/faas/gateway/requests"
)

// FunctionResourceRequest defines a request to set function resources
type FunctionResourceRequest struct {
	Limits   *stack.FunctionResources
	Requests *stack.FunctionResources
}

// DeployFunction call FaaS server to deploy a new function
func DeployFunction(fprocess string, gateway string, functionName string, image string,
	language string, replace bool, envVars map[string]string, network string,
	constraints []string, update bool, secrets []string, labels map[string]string, functionResourceRequest1 FunctionResourceRequest) {

	// Need to alter Gateway to allow nil/empty string as fprocess, to avoid this repetition.
	var fprocessTemplate string
	if len(fprocess) > 0 {
		fprocessTemplate = fprocess
	}

	gateway = strings.TrimRight(gateway, "/")

	if replace {
		DeleteFunction(gateway, functionName)
	}

	req := requests.CreateFunctionRequest{
		EnvProcess:  fprocessTemplate,
		Image:       image,
		Network:     network,
		Service:     functionName,
		EnvVars:     envVars,
		Constraints: constraints,
		Secrets:     secrets, // TODO: allow registry auth to be specified or read from local Docker credentials store
		Labels:      &labels,
	}

	hasLimits := false
	req.Limits = &requests.FunctionResources{}
	if functionResourceRequest1.Limits != nil && len(functionResourceRequest1.Limits.Memory) > 0 {
		hasLimits = true
		req.Limits.Memory = functionResourceRequest1.Limits.Memory
	}
	if functionResourceRequest1.Limits != nil && len(functionResourceRequest1.Limits.CPU) > 0 {
		hasLimits = true
		req.Limits.CPU = functionResourceRequest1.Limits.CPU
	}
	if !hasLimits {
		req.Limits = nil
	}

	hasRequests := false
	req.Requests = &requests.FunctionResources{}
	if functionResourceRequest1.Requests != nil && len(functionResourceRequest1.Requests.Memory) > 0 {
		hasRequests = true
		req.Requests.Memory = functionResourceRequest1.Requests.Memory
	}
	if functionResourceRequest1.Requests != nil && len(functionResourceRequest1.Requests.CPU) > 0 {
		hasRequests = true
		req.Requests.CPU = functionResourceRequest1.Requests.CPU
	}

	if !hasRequests {
		req.Requests = nil
	}

	reqBytes, _ := json.Marshal(&req)
	reader := bytes.NewReader(reqBytes)
	var request *http.Request

	timeout := 60 * time.Second
	client := MakeHTTPClient(&timeout)

	method := http.MethodPost
	// "application/json"
	if update {
		method = http.MethodPut
	}

	var err error
	request, err = http.NewRequest(method, gateway+"/system/functions", reader)
	SetAuth(request, gateway)
	if err != nil {
		fmt.Println(err)
		return
	}

	res, err := client.Do(request)
	if err != nil {
		fmt.Println("Is FaaS deployed? Do you need to specify the --gateway flag?")
		fmt.Println(err)
		return
	}

	if res.Body != nil {
		defer res.Body.Close()
	}

	switch res.StatusCode {
	case http.StatusOK, http.StatusCreated, http.StatusAccepted:
		if update {
			fmt.Println("Updated.")
		} else {
			fmt.Println("Deployed.")
		}

		deployedURL := fmt.Sprintf("URL: %s/function/%s\n", gateway, functionName)
		fmt.Println(deployedURL)
	case http.StatusUnauthorized:
		fmt.Println("unauthorized access, run \"faas-cli login\" to setup authentication for this server")
	default:
		bytesOut, err := ioutil.ReadAll(res.Body)
		if err == nil {
			fmt.Printf("Unexpected status: %d, message: %s\n", res.StatusCode, string(bytesOut))
		}
	}

	fmt.Println(res.Status)
}
<|endoftext|>


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

package migrations

import (
	"fmt"
	"strings"
	"time"

	"github.com/Unknwon/com"
	"github.com/go-xorm/xorm"

	"github.com/gogits/gogs/modules/log"
	"github.com/gogits/gogs/modules/setting"
)

const _MIN_DB_VER = 0

type Migration interface {
	Description() string
	Migrate(*xorm.Engine) error
}

type migration struct {
	description string
	migrate     func(*xorm.Engine) error
}

func NewMigration(desc string, fn func(*xorm.Engine) error) Migration {
	return &migration{desc, fn}
}

func (m *migration) Description() string {
	return m.description
}

func (m *migration) Migrate(x *xorm.Engine) error {
	return m.migrate(x)
}

// The version table. Should have only one row with id==1
type Version struct {
	Id      int64
	Version int64
}

// This is a sequence of migrations. Add new migrations to the bottom of the list.
// If you want to "retire" a migration, remove it from the top of the list and
// update _MIN_VER_DB accordingly
var migrations = []Migration{
	NewMigration("generate collaboration from access", accessToCollaboration), // V0 -> V1
}

// Migrate database to current version
func Migrate(x *xorm.Engine) error {
	if err := x.Sync(new(Version)); err != nil {
		return fmt.Errorf("sync: %v", err)
	}

	currentVersion := &Version{Id: 1}
	has, err := x.Get(currentVersion)
	if err != nil {
		return fmt.Errorf("get: %v", err)
	} else if !has {
		// If the user table does not exist it is a fresh installation and we
		// can skip all migrations
		needsMigration, err := x.IsTableExist("user")
		if err != nil {
			return err
		}
		if needsMigration {
			isEmpty, err := x.IsTableEmpty("user")
			if err != nil {
				return err
			}
			// If the user table is empty it is a fresh installation and we can
			// skip all migrations
			needsMigration = !isEmpty
		}
		if !needsMigration {
			currentVersion.Version = int64(_MIN_DB_VER + len(migrations))
		}

		if _, err = x.InsertOne(currentVersion); err != nil {
			return fmt.Errorf("insert: %v", err)
		}
	}

	v := currentVersion.Version
	for i, m := range migrations[v-_MIN_DB_VER:] {
		log.Info("Migration: %s", m.Description())
		if err = m.Migrate(x); err != nil {
			return fmt.Errorf("do migrate: %v", err)
		}
		currentVersion.Version = v + int64(i) + 1
		if _, err = x.Id(1).Update(currentVersion); err != nil {
			return err
		}
	}
	return nil
}

func accessToCollaboration(x *xorm.Engine) error {
	type Collaboration struct {
		ID      int64 `xorm:"pk autoincr"`
		RepoID  int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
		UserID  int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
		Created time.Time
	}

	x.Sync(new(Collaboration))

	results, err := x.Query("SELECT u.id AS `uid`, a.repo_name AS `repo`, a.mode AS `mode`, a.created as `created` FROM `access` a JOIN `user` u ON a.user_name=u.lower_name")
	if err != nil {
		return err
	}

	sess := x.NewSession()
	defer func() {
		if sess.IsCommitedOrRollbacked {
			sess.Rollback()
		}
		sess.Close()
	}()
	if err = sess.Begin(); err != nil {
		return err
	}

	offset := strings.Split(time.Now().String(), " ")[2]
	for _, result := range results {
		mode := com.StrTo(result["mode"]).MustInt64()
		// Collaborators must have write access.
		if mode < 2 {
			continue
		}

		userID := com.StrTo(result["uid"]).MustInt64()
		repoRefName := string(result["repo"])

		var created time.Time
		switch {
		case setting.UseSQLite3:
			created, _ = time.Parse(time.RFC3339, string(result["created"]))
		case setting.UseMySQL:
			created, _ = time.Parse("2006-01-02 15:04:05-0700", string(result["created"])+offset)
		case setting.UsePostgreSQL:
			created, _ = time.Parse("2006-01-02T15:04:05Z-0700", string(result["created"])+offset)
		}

		// find owner of repository
		parts := strings.SplitN(repoRefName, "/", 2)
		ownerName := parts[0]
		repoName := parts[1]

		results, err := sess.Query("SELECT u.id as `uid`, ou.uid as `memberid` FROM `user` u LEFT JOIN org_user ou ON ou.org_id=u.id WHERE u.lower_name=?", ownerName)
		if err != nil {
			return err
		}
		if len(results) < 1 {
			continue
		}

		ownerID := com.StrTo(results[0]["uid"]).MustInt64()
		if ownerID == userID {
			continue
		}

		// test if user is member of owning organization
		isMember := false
		for _, member := range results {
			memberID := com.StrTo(member["memberid"]).MustInt64()
			// We can skip all cases that a user is member of the owning organization
			if memberID == userID {
				isMember = true
			}
		}
		if isMember {
			continue
		}

		results, err = sess.Query("SELECT id FROM `repository` WHERE owner_id=? AND lower_name=?", ownerID, repoName)
		if err != nil {
			return err
		} else if len(results) < 1 {
			continue
		}

		collaboration := &Collaboration{
			UserID: userID,
			RepoID: com.StrTo(results[0]["id"]).MustInt64(),
		}
		has, err := sess.Get(collaboration)
		if err != nil {
			return err
		} else if has {
			continue
		}

		collaboration.Created = created
		if _, err = sess.InsertOne(collaboration); err != nil {
			return err
		}
	}

	return sess.Commit()
}
<commit_msg>models/migrations: fix little logic error<commit_after>// Copyright 2015 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package migrations

import (
	"fmt"
	"strings"
	"time"

	"github.com/Unknwon/com"
	"github.com/go-xorm/xorm"

	"github.com/gogits/gogs/modules/log"
	"github.com/gogits/gogs/modules/setting"
)

const _MIN_DB_VER = 0

type Migration interface {
	Description() string
	Migrate(*xorm.Engine) error
}

type migration struct {
	description string
	migrate     func(*xorm.Engine) error
}

func NewMigration(desc string, fn func(*xorm.Engine) error) Migration {
	return &migration{desc, fn}
}

func (m *migration) Description() string {
	return m.description
}

func (m *migration) Migrate(x *xorm.Engine) error {
	return m.migrate(x)
}

// The version table. Should have only one row with id==1
type Version struct {
	Id      int64
	Version int64
}

// This is a sequence of migrations. Add new migrations to the bottom of the list.
// If you want to "retire" a migration, remove it from the top of the list and
// update _MIN_VER_DB accordingly
var migrations = []Migration{
	NewMigration("generate collaboration from access", accessToCollaboration), // V0 -> V1
}

// Migrate database to current version
func Migrate(x *xorm.Engine) error {
	if err := x.Sync(new(Version)); err != nil {
		return fmt.Errorf("sync: %v", err)
	}

	currentVersion := &Version{Id: 1}
	has, err := x.Get(currentVersion)
	if err != nil {
		return fmt.Errorf("get: %v", err)
	} else if !has {
		// If the user table does not exist it is a fresh installation and we
		// can skip all migrations.
		needsMigration, err := x.IsTableExist("user")
		if err != nil {
			return err
		}
		if needsMigration {
			isEmpty, err := x.IsTableEmpty("user")
			if err != nil {
				return err
			}
			// If the user table is empty it is a fresh installation and we can
			// skip all migrations.
			needsMigration = !isEmpty
		}
		if !needsMigration {
			currentVersion.Version = int64(_MIN_DB_VER + len(migrations))
		}

		if _, err = x.InsertOne(currentVersion); err != nil {
			return fmt.Errorf("insert: %v", err)
		}
	}

	v := currentVersion.Version
	for i, m := range migrations[v-_MIN_DB_VER:] {
		log.Info("Migration: %s", m.Description())
		if err = m.Migrate(x); err != nil {
			return fmt.Errorf("do migrate: %v", err)
		}
		currentVersion.Version = v + int64(i) + 1
		if _, err = x.Id(1).Update(currentVersion); err != nil {
			return err
		}
	}
	return nil
}

func sessionRelease(sess *xorm.Session) {
	if !sess.IsCommitedOrRollbacked {
		sess.Rollback()
	}
	sess.Close()
}

func accessToCollaboration(x *xorm.Engine) error {
	type Collaboration struct {
		ID      int64 `xorm:"pk autoincr"`
		RepoID  int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
		UserID  int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
		Created time.Time
	}

	x.Sync(new(Collaboration))

	results, err := x.Query("SELECT u.id AS `uid`, a.repo_name AS `repo`, a.mode AS `mode`, a.created as `created` FROM `access` a JOIN `user` u ON a.user_name=u.lower_name")
	if err != nil {
		return err
	}

	sess := x.NewSession()
	defer sessionRelease(sess)
	if err = sess.Begin(); err != nil {
		return err
	}

	offset := strings.Split(time.Now().String(), " ")[2]
	for _, result := range results {
		mode := com.StrTo(result["mode"]).MustInt64()
		// Collaborators must have write access.
		if mode < 2 {
			continue
		}

		userID := com.StrTo(result["uid"]).MustInt64()
		repoRefName := string(result["repo"])

		var created time.Time
		switch {
		case setting.UseSQLite3:
			created, _ = time.Parse(time.RFC3339, string(result["created"]))
		case setting.UseMySQL:
			created, _ = time.Parse("2006-01-02 15:04:05-0700", string(result["created"])+offset)
		case setting.UsePostgreSQL:
			created, _ = time.Parse("2006-01-02T15:04:05Z-0700", string(result["created"])+offset)
		}

		// find owner of repository
		parts := strings.SplitN(repoRefName, "/", 2)
		ownerName := parts[0]
		repoName := parts[1]

		results, err := sess.Query("SELECT u.id as `uid`, ou.uid as `memberid` FROM `user` u LEFT JOIN org_user ou ON ou.org_id=u.id WHERE u.lower_name=?", ownerName)
		if err != nil {
			return err
		}
		if len(results) < 1 {
			continue
		}

		ownerID := com.StrTo(results[0]["uid"]).MustInt64()
		if ownerID == userID {
			continue
		}

		// test if user is member of owning organization
		isMember := false
		for _, member := range results {
			memberID := com.StrTo(member["memberid"]).MustInt64()
			// We can skip all cases that a user is member of the owning organization
			if memberID == userID {
				isMember = true
			}
		}
		if isMember {
			continue
		}

		results, err = sess.Query("SELECT id FROM `repository` WHERE owner_id=? AND lower_name=?", ownerID, repoName)
		if err != nil {
			return err
		} else if len(results) < 1 {
			continue
		}

		collaboration := &Collaboration{
			UserID: userID,
			RepoID: com.StrTo(results[0]["id"]).MustInt64(),
		}
		has, err := sess.Get(collaboration)
		if err != nil {
			return err
		} else if has {
			continue
		}

		collaboration.Created = created
		if _, err = sess.InsertOne(collaboration); err != nil {
			return err
		}
	}

	return sess.Commit()
}
<|endoftext|>


<commit_before>package scummatlas

import "testing"

func TestStringOpcode(t *testing.T) {
	str, length := parsePrintOpcode([]byte{
		0x0F, 0x41, 0x42, 0x43, 0x44, 0x00, 0xAA, 0xAA}, 0)
	if str != "ABCD" {
		t.Errorf("String %v is not right", str)
	}
	if length != 7 {
		t.Errorf("Length %d is not right", length)
	}

	//OFFSET
	str, length = parsePrintOpcode([]byte{
		0xAA, 0xAA, 0x0F, 0x41, 0x42, 0x43, 0x44, 0x00, 0xAA, 0xAA}, 2)
	if str != "ABCD" {
		t.Errorf("String %v is not right", str)
	}
	if length != 7 {
		t.Errorf("Length %d is not right", length)
	}

	//ENCODINGS
	//TODO

}
<commit_msg>Fix string test<commit_after>package scummatlas

import "testing"

func TestStringOpcode(t *testing.T) {
	str, length := parsePrintOpcode([]byte{
		0x0F, 0x41, 0x42, 0x43, 0x44, 0x00, 0xAA, 0xAA}, 0)
	if str != "ABCD" {
		t.Errorf("String %v is not right", str)
	}
	if length != 5 {
		t.Errorf("Length %d is not right", length)
	}

	//OFFSET
	str, length = parsePrintOpcode([]byte{
		0xAA, 0xAA, 0x0F, 0x41, 0x42, 0x43, 0x44, 0x00, 0xAA, 0xAA}, 2)
	if str != "ABCD" {
		t.Errorf("String %v is not right", str)
	}
	if length != 5 {
		t.Errorf("Length %d is not right", length)
	}

	//ENCODINGS
	//TODO

}
<|endoftext|>