<commit_before>package shell

import (
	"log"
	"os"
	"os/exec"
	"strings"

	prompt "github.com/c-bata/go-prompt"
	"github.com/pachyderm/pachyderm/src/server/pkg/uuid"
	"github.com/spf13/cobra"
)

const (
	completionAnnotation string = "completion"
	ldThreshold          int    = 2
)

type CompletionFunc func(flag, arg string) []prompt.Suggest

var completions map[string]CompletionFunc = make(map[string]CompletionFunc)

func RegisterCompletionFunc(cmd *cobra.Command, completionFunc CompletionFunc) {
	id := uuid.NewWithoutDashes()

	if cmd.Annotations == nil {
		cmd.Annotations = make(map[string]string)
	} else if _, ok := cmd.Annotations[completionAnnotation]; ok {
		panic("duplicate completion func registration")
	}
	cmd.Annotations[completionAnnotation] = id
	completions[id] = completionFunc
}

type shell struct {
	rootCmd *cobra.Command
}

func newShell(rootCmd *cobra.Command) *shell {
	return &shell{rootCmd: rootCmd}
}

func (s *shell) executor(in string) {
	cmd := exec.Command("bash")
	cmd.Stdin = strings.NewReader("pachctl " + in)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	cmd.Run()
	return
}

func (s *shell) suggestor(in prompt.Document) []prompt.Suggest {
	args := strings.Fields(in.Text)
	if len(strings.TrimSuffix(in.Text, " ")) < len(in.Text) {
		args = append(args, "")
	}
	cmd := s.rootCmd
	text := ""
	if len(args) > 0 {
		var err error
		cmd, _, err = s.rootCmd.Traverse(args[:len(args)-1])
		if err != nil {
			log.Fatal(err)
		}
		text = args[len(args)-1]
	}
	flag := ""
	if len(args) > 1 {
		if args[len(args)-2][0] == '-' {
			flag = args[len(args)-2]
		}
	}
	suggestions := cmd.SuggestionsFor(text)
	if len(suggestions) > 0 {
		var result []prompt.Suggest
		for _, suggestion := range suggestions {
			cmd, _, err := cmd.Traverse([]string{suggestion})
			if err != nil {
				log.Fatal(err)
			}
			result = append(result, prompt.Suggest{
				Text:        suggestion,
				Description: cmd.Short,
			})
		}
		return result
	}
	if id, ok := cmd.Annotations[completionAnnotation]; ok {
		completionFunc := completions[id]
		suggests := completionFunc(flag, text)
		var result []prompt.Suggest
		for _, s := range suggests {
			sText := s.Text
			if len(text) < len(sText) {
				sText = sText[:len(text)]
			}
			if ld(sText, text, true) < ldThreshold {
				result = append(result, s)
			}
		}
		return result
	}
	return nil
}

func (s *shell) run() {
	prompt.New(
		s.executor,
		s.suggestor,
		prompt.OptionPrefix("🐘 > "),
	).Run()
}

// Run runs a prompt, it does not return.
func Run(rootCmd *cobra.Command) {
	newShell(rootCmd).run()
}

// ld computes the Levenshtein Distance for two strings.
func ld(s, t string, ignoreCase bool) int {
	if ignoreCase {
		s = strings.ToLower(s)
		t = strings.ToLower(t)
	}
	d := make([][]int, len(s)+1)
	for i := range d {
		d[i] = make([]int, len(t)+1)
	}
	for i := range d {
		d[i][0] = i
	}
	for j := range d[0] {
		d[0][j] = j
	}
	for j := 1; j <= len(t); j++ {
		for i := 1; i <= len(s); i++ {
			if s[i-1] == t[j-1] {
				d[i][j] = d[i-1][j-1]
			} else {
				min := d[i-1][j]
				if d[i][j-1] < min {
					min = d[i][j-1]
				}
				if d[i-1][j-1] < min {
					min = d[i-1][j-1]
				}
				d[i][j] = min + 1
			}
		}

	}
	return d[len(s)][len(t)]
}
<commit_msg>Adds context to prompt.<commit_after>package shell

import (
	"fmt"
	"log"
	"os"
	"os/exec"
	"strings"

	prompt "github.com/c-bata/go-prompt"
	"github.com/pachyderm/pachyderm/src/client/pkg/config"
	"github.com/pachyderm/pachyderm/src/server/pkg/uuid"
	"github.com/spf13/cobra"
)

const (
	completionAnnotation string = "completion"
	ldThreshold          int    = 2
	pachChar             rune   = '🐘'
)

type CompletionFunc func(flag, arg string) []prompt.Suggest

var completions map[string]CompletionFunc = make(map[string]CompletionFunc)

func RegisterCompletionFunc(cmd *cobra.Command, completionFunc CompletionFunc) {
	id := uuid.NewWithoutDashes()

	if cmd.Annotations == nil {
		cmd.Annotations = make(map[string]string)
	} else if _, ok := cmd.Annotations[completionAnnotation]; ok {
		panic("duplicate completion func registration")
	}
	cmd.Annotations[completionAnnotation] = id
	completions[id] = completionFunc
}

type shell struct {
	rootCmd *cobra.Command
}

func newShell(rootCmd *cobra.Command) *shell {
	return &shell{rootCmd: rootCmd}
}

func (s *shell) executor(in string) {
	cmd := exec.Command("bash")
	cmd.Stdin = strings.NewReader("pachctl " + in)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	cmd.Run()
	return
}

func (s *shell) suggestor(in prompt.Document) []prompt.Suggest {
	args := strings.Fields(in.Text)
	if len(strings.TrimSuffix(in.Text, " ")) < len(in.Text) {
		args = append(args, "")
	}
	cmd := s.rootCmd
	text := ""
	if len(args) > 0 {
		var err error
		cmd, _, err = s.rootCmd.Traverse(args[:len(args)-1])
		if err != nil {
			log.Fatal(err)
		}
		text = args[len(args)-1]
	}
	flag := ""
	if len(args) > 1 {
		if args[len(args)-2][0] == '-' {
			flag = args[len(args)-2]
		}
	}
	suggestions := cmd.SuggestionsFor(text)
	if len(suggestions) > 0 {
		var result []prompt.Suggest
		for _, suggestion := range suggestions {
			cmd, _, err := cmd.Traverse([]string{suggestion})
			if err != nil {
				log.Fatal(err)
			}
			result = append(result, prompt.Suggest{
				Text:        suggestion,
				Description: cmd.Short,
			})
		}
		return result
	}
	if id, ok := cmd.Annotations[completionAnnotation]; ok {
		completionFunc := completions[id]
		suggests := completionFunc(flag, text)
		var result []prompt.Suggest
		for _, s := range suggests {
			sText := s.Text
			if len(text) < len(sText) {
				sText = sText[:len(text)]
			}
			if ld(sText, text, true) < ldThreshold {
				result = append(result, s)
			}
		}
		return result
	}
	return nil
}

func (s *shell) run() {
	prompt.New(
		s.executor,
		s.suggestor,
		prompt.OptionPrefix(fmt.Sprintf("%c > ", pachChar)),
		prompt.OptionLivePrefix(func() (string, bool) {
			cfg, err := config.Read()
			if err != nil {
				return "", false
			}
			activeContext, _, err := cfg.ActiveContext()
			if err != nil {
				return "", false
			}
			return fmt.Sprintf("%c  context:(%s) >", pachChar, activeContext), true
		}),
	).Run()
}

// Run runs a prompt, it does not return.
func Run(rootCmd *cobra.Command) {
	newShell(rootCmd).run()
}

// ld computes the Levenshtein Distance for two strings.
func ld(s, t string, ignoreCase bool) int {
	if ignoreCase {
		s = strings.ToLower(s)
		t = strings.ToLower(t)
	}
	d := make([][]int, len(s)+1)
	for i := range d {
		d[i] = make([]int, len(t)+1)
	}
	for i := range d {
		d[i][0] = i
	}
	for j := range d[0] {
		d[0][j] = j
	}
	for j := 1; j <= len(t); j++ {
		for i := 1; i <= len(s); i++ {
			if s[i-1] == t[j-1] {
				d[i][j] = d[i-1][j-1]
			} else {
				min := d[i-1][j]
				if d[i][j-1] < min {
					min = d[i][j-1]
				}
				if d[i-1][j-1] < min {
					min = d[i-1][j-1]
				}
				d[i][j] = min + 1
			}
		}

	}
	return d[len(s)][len(t)]
}
<|endoftext|>


<commit_before>package router

import (
	"fmt"
	"os"
	"sort"
	"testing"

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

type SortableRules []Rule

func (r SortableRules) Len() int {
	return len(r)
}
func (r SortableRules) Less(i, j int) bool {
	return r[i].Name < r[j].Name
}
func (r SortableRules) Swap(i, j int) {
	tmp := r[j]
	r[j] = r[i]
	r[i] = tmp
}

func TestParsesWellFormatedConfig(t *testing.T) {
	conf := []byte(`
routes:
  rule-one:
    matchers:
      title: ["authorize-app"]
    output:
      type: "notifications"
      channel: "#team"
      icon: ":rocket:"
      message: "authorized %{foo.bar} in ${SCHOOL}"
      user: "@fishman"
  rule-two:
    matchers:
      foo.bar: ["multiple", "matches"]
      baz: ["whatever"]
    output:
      type: "alerts"
      series: "other-series"
      dimensions: ["baz"]
      stat_type: "gauge"
`)
	expected := SortableRules{
		Rule{
			Name:     "rule-one",
			Matchers: RuleMatchers{"title": []string{"authorize-app"}},
			Output: RuleOutput{
				"type":    "notifications",
				"channel": "#team",
				"icon":    ":rocket:",
				"message": `authorized %{foo.bar} in Hogwarts`,
				"user":    "@fishman",
			},
		},
		Rule{
			Name: "rule-two",
			Matchers: RuleMatchers{
				"foo.bar": []string{"multiple", "matches"},
				"baz":     []string{"whatever"},
			},
			Output: RuleOutput{
				"type":       "alerts",
				"series":     "other-series",
				"dimensions": []interface{}{"baz"},
				"stat_type":  "gauge",
			},
		},
	}

	err := os.Setenv("SCHOOL", "Hogwarts")
	assert.Nil(t, err)

	router, err := newFromConfigBytes(conf)
	assert.Nil(t, err)

	actual := SortableRules(router.rules)
	sort.Sort(expected)
	sort.Sort(actual)
	assert.Equal(t, expected, actual)
}

func TestOnlyNonemptyStringMatcherValues(t *testing.T) {
	confTmpl := `
routes:
  non-string-values:
    matchers:
      no-numbers: [%s]
    output:
      type: "analytics"
      series: "fun"
`

	// Make sure the template works
	conf := []byte(fmt.Sprintf(confTmpl, "\"valid\""))
	_, err := newFromConfigBytes(conf)
	assert.Nil(t, err)

	for _, invalidVal := range []string{"5", "true", "[]", "{}", `""`} {
		conf := []byte(fmt.Sprintf(confTmpl, invalidVal))
		_, err := newFromConfigBytes(conf)
		assert.Error(t, err)
	}
}

func TestNoSpecialsInMatcher(t *testing.T) {
	confFieldTmpl := `
routes:
  complicated-fields:
    matchers:
      "%s": ["hallo?"]
    output:
      type: "analytics"
      series: "fun"
`
	confValTmpl := `
routes:
  complicated-values:
    matchers:
      title: ["%s"]
    output:
      type: "analytics"
      series: "fun"
`

	// Make sure templates work
	for _, tmpl := range []string{confFieldTmpl, confValTmpl} {
		conf := []byte(fmt.Sprintf(tmpl, "valid"))
		_, err := newFromConfigBytes(conf)
		assert.Nil(t, err)
	}

	invalids := []string{"${wut}", "%{wut}", "$huh", "}ok?", "nope{", `100% fail`}
	for _, invalid := range invalids {
		for _, tmpl := range []string{confFieldTmpl, confValTmpl} {
			conf := []byte(fmt.Sprintf(tmpl, invalid))
			_, err := newFromConfigBytes(conf)
			assert.Error(t, err)
		}
	}
}

func TestNoDupMatchers(t *testing.T) {
	confTmpl := `
routes:
  sloppy:
    matchers:
      title: [%s]
    output:
      type: "analytics"
      series: "fun"
`

	validConf := []byte(fmt.Sprintf(confTmpl, `"non-repeated", "name"`))
	_, err := newFromConfigBytes(validConf)
	assert.Nil(t, err)

	invalidConf := []byte(fmt.Sprintf(confTmpl, `"repeated", "repeated", "name"`))
	_, err = newFromConfigBytes(invalidConf)
	assert.Error(t, err)
}

func TestOutputRequiresCorrectTypes(t *testing.T) {
	confTmpl := `
routes:
  wrong:
    matchers:
      title: ["test"]
    output:
      type: "alerts"
      series: %s
      dimensions: %s
      stat_type: "gauge"
`

	validConf := []byte(fmt.Sprintf(confTmpl, `"my-series"`, `["dim1", "dim2"]`))
	_, err := newFromConfigBytes(validConf)
	assert.Nil(t, err)

	invalidConf0 := []byte(fmt.Sprintf(confTmpl, `["my-series"]`, `["dim1", "dim2"]`))
	_, err = newFromConfigBytes(invalidConf0)
	assert.Error(t, err)

	invalidConf1 := []byte(fmt.Sprintf(confTmpl, `"my-series"`, `"dim1"`))
	_, err = newFromConfigBytes(invalidConf1)
	assert.Error(t, err)
}

func TestOutputRequiresAllKeys(t *testing.T) {
	confTmpl := `
routes:
  wrong:
    matchers:
      title: ["test"]
    output:
      type: "alerts"%s
      dimensions: ["dim1", "dim2"]
      stat_type: "gauge"
`

	validConf := []byte(fmt.Sprintf(confTmpl, `
      series: "whatever"`))
	_, err := newFromConfigBytes(validConf)
	assert.Nil(t, err)

	invalidConf := []byte(fmt.Sprintf(confTmpl, ``))
	_, err = newFromConfigBytes(invalidConf)
	assert.Error(t, err)
}

func TestOutputNoExtraKeysAllowed(t *testing.T) {
	confTmpl := `
routes:
  wrong:
    matchers:
      title: ["test"]
    output:
      type: "alerts"%s
      dimensions: ["dim1", "dim2"]
      stat_type: "gauge"
`

	validConf := []byte(fmt.Sprintf(confTmpl, `
      series: "whatever"`))
	_, err := newFromConfigBytes(validConf)
	assert.Nil(t, err)

	invalidConf := []byte(fmt.Sprintf(confTmpl, `
      series: "whatever"
      something-else: "hi there"`))
	_, err = newFromConfigBytes(invalidConf)
	assert.Error(t, err)
}
<commit_msg>Added tests to check different error cases<commit_after>package router

import (
	"fmt"
	"os"
	"sort"
	"testing"

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

type SortableRules []Rule

func (r SortableRules) Len() int {
	return len(r)
}
func (r SortableRules) Less(i, j int) bool {
	return r[i].Name < r[j].Name
}
func (r SortableRules) Swap(i, j int) {
	tmp := r[j]
	r[j] = r[i]
	r[i] = tmp
}

func TestParsesWellFormatedConfig(t *testing.T) {
	conf := []byte(`
routes:
  rule-one:
    matchers:
      title: ["authorize-app"]
    output:
      type: "notifications"
      channel: "#team"
      icon: ":rocket:"
      message: "authorized %{foo.bar} in ${SCHOOL}"
      user: "@fishman"
  rule-two:
    matchers:
      foo.bar: ["multiple", "matches"]
      baz: ["whatever"]
    output:
      type: "alerts"
      series: "other-series"
      dimensions: ["baz"]
      stat_type: "gauge"
`)
	expected := SortableRules{
		Rule{
			Name:     "rule-one",
			Matchers: RuleMatchers{"title": []string{"authorize-app"}},
			Output: RuleOutput{
				"type":    "notifications",
				"channel": "#team",
				"icon":    ":rocket:",
				"message": `authorized %{foo.bar} in Hogwarts`,
				"user":    "@fishman",
			},
		},
		Rule{
			Name: "rule-two",
			Matchers: RuleMatchers{
				"foo.bar": []string{"multiple", "matches"},
				"baz":     []string{"whatever"},
			},
			Output: RuleOutput{
				"type":       "alerts",
				"series":     "other-series",
				"dimensions": []interface{}{"baz"},
				"stat_type":  "gauge",
			},
		},
	}

	err := os.Setenv("SCHOOL", "Hogwarts")
	assert.Nil(t, err)

	router, err := newFromConfigBytes(conf)
	assert.Nil(t, err)

	actual := SortableRules(router.rules)
	sort.Sort(expected)
	sort.Sort(actual)
	assert.Equal(t, expected, actual)
}

func TestOnlyNonemptyStringMatcherValues(t *testing.T) {
	confTmpl := `
routes:
  non-string-values:
    matchers:
      no-numbers: [%s]
    output:
      type: "analytics"
      series: "fun"
`

	// Make sure the template works
	conf := []byte(fmt.Sprintf(confTmpl, "\"valid\""))
	_, err := newFromConfigBytes(conf)
	assert.Nil(t, err)

	for _, invalidVal := range []string{"5", "true", "[]", "{}", `""`} {
		conf := []byte(fmt.Sprintf(confTmpl, invalidVal))
		_, err := newFromConfigBytes(conf)
		assert.Error(t, err)
	}
}

func TestNoSpecialsInMatcher(t *testing.T) {
	confFieldTmpl := `
routes:
  complicated-fields:
    matchers:
      "%s": ["hallo?"]
    output:
      type: "analytics"
      series: "fun"
`
	confValTmpl := `
routes:
  complicated-values:
    matchers:
      title: ["%s"]
    output:
      type: "analytics"
      series: "fun"
`

	// Make sure templates work
	for _, tmpl := range []string{confFieldTmpl, confValTmpl} {
		conf := []byte(fmt.Sprintf(tmpl, "valid"))
		_, err := newFromConfigBytes(conf)
		assert.Nil(t, err)
	}

	invalids := []string{"${wut}", "%{wut}", "$huh", "}ok?", "nope{", `100% fail`}
	for _, invalid := range invalids {
		for _, tmpl := range []string{confFieldTmpl, confValTmpl} {
			conf := []byte(fmt.Sprintf(tmpl, invalid))
			_, err := newFromConfigBytes(conf)
			assert.Error(t, err)
		}
	}
}

func TestNoDupMatchers(t *testing.T) {
	confTmpl := `
routes:
  sloppy:
    matchers:
      title: [%s]
    output:
      type: "analytics"
      series: "fun"
`

	validConf := []byte(fmt.Sprintf(confTmpl, `"non-repeated", "name"`))
	_, err := newFromConfigBytes(validConf)
	assert.Nil(t, err)

	invalidConf := []byte(fmt.Sprintf(confTmpl, `"repeated", "repeated", "name"`))
	_, err = newFromConfigBytes(invalidConf)
	assert.Error(t, err)
}

func TestErrorsThrownWithTypeOCaught(t *testing.T) {
	assert := assert.New(t)

	config := `
route: # Shouldn't routes (plural)
  non-string-values:
    matchers:
      errors: [ "type-o" ]
    output:
      type: "analytics"
      series: "fun"
`
	_, err := newFromConfigBytes([]byte(config))
	assert.Error(err)

	config = `
routes:
  non-string-values:
    matcher: # Shouldn't matches (plural)
      errors: [ "type-o" ]
    output:
      type: "analytics"
      series: "fun"
`
	_, err = newFromConfigBytes([]byte(config))
	assert.Error(err)

	config = `
routes:
  $non-string-values: # Invalid rule name
    matchers:
      errors: [ "type-o" ]
    output:
      type: "analytics"
      series: "fun"
`
	_, err = newFromConfigBytes([]byte(config))
	assert.Error(err)

	config = `
routes:
  $non-string-values: # Invalid rule name
    matchers:
      errors: [ "type-o" ]
    outputs: # Should be output (signular)
      type: "analytics"
      series: "fun"
`
	_, err = newFromConfigBytes([]byte(config))
	assert.Error(err)
}

func TestOutputRequiresCorrectTypes(t *testing.T) {
	confTmpl := `
routes:
  wrong:
    matchers:
      title: ["test"]
    output:
      type: "alerts"
      series: %s
      dimensions: %s
      stat_type: "gauge"
`

	validConf := []byte(fmt.Sprintf(confTmpl, `"my-series"`, `["dim1", "dim2"]`))
	_, err := newFromConfigBytes(validConf)
	assert.Nil(t, err)

	invalidConf0 := []byte(fmt.Sprintf(confTmpl, `["my-series"]`, `["dim1", "dim2"]`))
	_, err = newFromConfigBytes(invalidConf0)
	assert.Error(t, err)

	invalidConf1 := []byte(fmt.Sprintf(confTmpl, `"my-series"`, `"dim1"`))
	_, err = newFromConfigBytes(invalidConf1)
	assert.Error(t, err)
}

func TestOutputRequiresAllKeys(t *testing.T) {
	confTmpl := `
routes:
  wrong:
    matchers:
      title: ["test"]
    output:
      type: "alerts"%s
      dimensions: ["dim1", "dim2"]
      stat_type: "gauge"
`

	validConf := []byte(fmt.Sprintf(confTmpl, `
      series: "whatever"`))
	_, err := newFromConfigBytes(validConf)
	assert.Nil(t, err)

	invalidConf := []byte(fmt.Sprintf(confTmpl, ``))
	_, err = newFromConfigBytes(invalidConf)
	assert.Error(t, err)
}

func TestOutputNoExtraKeysAllowed(t *testing.T) {
	confTmpl := `
routes:
  wrong:
    matchers:
      title: ["test"]
    output:
      type: "alerts"%s
      dimensions: ["dim1", "dim2"]
      stat_type: "gauge"
`

	validConf := []byte(fmt.Sprintf(confTmpl, `
      series: "whatever"`))
	_, err := newFromConfigBytes(validConf)
	assert.Nil(t, err)

	invalidConf := []byte(fmt.Sprintf(confTmpl, `
      series: "whatever"
      something-else: "hi there"`))
	_, err = newFromConfigBytes(invalidConf)
	assert.Error(t, err)
}
<|endoftext|>


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

package cache

import (
	"bytes"
	"crypto/sha256"
	"fmt"
	"hash"
	"io"
	"os"
	"runtime"
	"strings"
	"sync"
)

var debugHash = false // set when GODEBUG=gocachehash=1

// HashSize is the number of bytes in a hash.
const HashSize = 32

// A Hash provides access to the canonical hash function used to index the cache.
// The current implementation uses salted SHA256, but clients must not assume this.
type Hash struct {
	h    hash.Hash
	name string        // for debugging
	buf  *bytes.Buffer // for verify
}

// hashSalt is a salt string added to the beginning of every hash
// created by NewHash. Using the Go version makes sure that different
// versions of the go command (or even different Git commits during
// work on the development branch) do not address the same cache
// entries, so that a bug in one version does not affect the execution
// of other versions. This salt will result in additional ActionID files
// in the cache, but not additional copies of the large output files,
// which are still addressed by unsalted SHA256.
//
// We strip any GOEXPERIMENTs the go tool was built with from this
// version string on the assumption that they shouldn't affect go tool
// execution. This also allows bootstrapping to converge faster
// because dist builds go_bootstrap without any experiments.
var hashSalt = []byte(stripExperiment(runtime.Version()))

// stripExperiment strips any GOEXPERIMENT configuration from the Go
// version string.
func stripExperiment(version string) string {
	if i := strings.Index(version, " X:"); i >= 0 {
		return version[:i]
	}
	return version
}

// Subkey returns an action ID corresponding to mixing a parent
// action ID with a string description of the subkey.
func Subkey(parent ActionID, desc string) ActionID {
	h := sha256.New()
	h.Write([]byte("subkey:"))
	h.Write(parent[:])
	h.Write([]byte(desc))
	var out ActionID
	h.Sum(out[:0])
	if debugHash {
		fmt.Fprintf(os.Stderr, "HASH subkey %x %q = %x\n", parent, desc, out)
	}
	if verify {
		hashDebug.Lock()
		hashDebug.m[out] = fmt.Sprintf("subkey %x %q", parent, desc)
		hashDebug.Unlock()
	}
	return out
}

// NewHash returns a new Hash.
// The caller is expected to Write data to it and then call Sum.
func NewHash(name string) *Hash {
	h := &Hash{h: sha256.New(), name: name}
	if debugHash {
		fmt.Fprintf(os.Stderr, "HASH[%s]\n", h.name)
	}
	h.Write(hashSalt)
	if verify {
		h.buf = new(bytes.Buffer)
	}
	return h
}

// Write writes data to the running hash.
func (h *Hash) Write(b []byte) (int, error) {
	if debugHash {
		fmt.Fprintf(os.Stderr, "HASH[%s]: %q\n", h.name, b)
	}
	if h.buf != nil {
		h.buf.Write(b)
	}
	return h.h.Write(b)
}

// Sum returns the hash of the data written previously.
func (h *Hash) Sum() [HashSize]byte {
	var out [HashSize]byte
	h.h.Sum(out[:0])
	if debugHash {
		fmt.Fprintf(os.Stderr, "HASH[%s]: %x\n", h.name, out)
	}
	if h.buf != nil {
		hashDebug.Lock()
		if hashDebug.m == nil {
			hashDebug.m = make(map[[HashSize]byte]string)
		}
		hashDebug.m[out] = h.buf.String()
		hashDebug.Unlock()
	}
	return out
}

// In GODEBUG=gocacheverify=1 mode,
// hashDebug holds the input to every computed hash ID,
// so that we can work backward from the ID involved in a
// cache entry mismatch to a description of what should be there.
var hashDebug struct {
	sync.Mutex
	m map[[HashSize]byte]string
}

// reverseHash returns the input used to compute the hash id.
func reverseHash(id [HashSize]byte) string {
	hashDebug.Lock()
	s := hashDebug.m[id]
	hashDebug.Unlock()
	return s
}

var hashFileCache struct {
	sync.Mutex
	m map[string][HashSize]byte
}

// FileHash returns the hash of the named file.
// It caches repeated lookups for a given file,
// and the cache entry for a file can be initialized
// using SetFileHash.
// The hash used by FileHash is not the same as
// the hash used by NewHash.
func FileHash(file string) ([HashSize]byte, error) {
	hashFileCache.Lock()
	out, ok := hashFileCache.m[file]
	hashFileCache.Unlock()

	if ok {
		return out, nil
	}

	h := sha256.New()
	f, err := os.Open(file)
	if err != nil {
		if debugHash {
			fmt.Fprintf(os.Stderr, "HASH %s: %v\n", file, err)
		}
		return [HashSize]byte{}, err
	}
	_, err = io.Copy(h, f)
	f.Close()
	if err != nil {
		if debugHash {
			fmt.Fprintf(os.Stderr, "HASH %s: %v\n", file, err)
		}
		return [HashSize]byte{}, err
	}
	h.Sum(out[:0])
	if debugHash {
		fmt.Fprintf(os.Stderr, "HASH %s: %x\n", file, out)
	}

	SetFileHash(file, out)
	return out, nil
}

// SetFileHash sets the hash returned by FileHash for file.
func SetFileHash(file string, sum [HashSize]byte) {
	hashFileCache.Lock()
	if hashFileCache.m == nil {
		hashFileCache.m = make(map[string][HashSize]byte)
	}
	hashFileCache.m[file] = sum
	hashFileCache.Unlock()
}
<commit_msg>cmd/go: clarify comment on HashSeed<commit_after>// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package cache

import (
	"bytes"
	"crypto/sha256"
	"fmt"
	"hash"
	"io"
	"os"
	"runtime"
	"strings"
	"sync"
)

var debugHash = false // set when GODEBUG=gocachehash=1

// HashSize is the number of bytes in a hash.
const HashSize = 32

// A Hash provides access to the canonical hash function used to index the cache.
// The current implementation uses salted SHA256, but clients must not assume this.
type Hash struct {
	h    hash.Hash
	name string        // for debugging
	buf  *bytes.Buffer // for verify
}

// hashSalt is a salt string added to the beginning of every hash
// created by NewHash. Using the Go version makes sure that different
// versions of the go command (or even different Git commits during
// work on the development branch) do not address the same cache
// entries, so that a bug in one version does not affect the execution
// of other versions. This salt will result in additional ActionID files
// in the cache, but not additional copies of the large output files,
// which are still addressed by unsalted SHA256.
//
// We strip any GOEXPERIMENTs the go tool was built with from this
// version string on the assumption that they shouldn't affect go tool
// execution. This allows bootstrapping to converge faster: dist builds
// go_bootstrap without any experiments, so by stripping experiments
// go_bootstrap and the final go binary will use the same salt.
var hashSalt = []byte(stripExperiment(runtime.Version()))

// stripExperiment strips any GOEXPERIMENT configuration from the Go
// version string.
func stripExperiment(version string) string {
	if i := strings.Index(version, " X:"); i >= 0 {
		return version[:i]
	}
	return version
}

// Subkey returns an action ID corresponding to mixing a parent
// action ID with a string description of the subkey.
func Subkey(parent ActionID, desc string) ActionID {
	h := sha256.New()
	h.Write([]byte("subkey:"))
	h.Write(parent[:])
	h.Write([]byte(desc))
	var out ActionID
	h.Sum(out[:0])
	if debugHash {
		fmt.Fprintf(os.Stderr, "HASH subkey %x %q = %x\n", parent, desc, out)
	}
	if verify {
		hashDebug.Lock()
		hashDebug.m[out] = fmt.Sprintf("subkey %x %q", parent, desc)
		hashDebug.Unlock()
	}
	return out
}

// NewHash returns a new Hash.
// The caller is expected to Write data to it and then call Sum.
func NewHash(name string) *Hash {
	h := &Hash{h: sha256.New(), name: name}
	if debugHash {
		fmt.Fprintf(os.Stderr, "HASH[%s]\n", h.name)
	}
	h.Write(hashSalt)
	if verify {
		h.buf = new(bytes.Buffer)
	}
	return h
}

// Write writes data to the running hash.
func (h *Hash) Write(b []byte) (int, error) {
	if debugHash {
		fmt.Fprintf(os.Stderr, "HASH[%s]: %q\n", h.name, b)
	}
	if h.buf != nil {
		h.buf.Write(b)
	}
	return h.h.Write(b)
}

// Sum returns the hash of the data written previously.
func (h *Hash) Sum() [HashSize]byte {
	var out [HashSize]byte
	h.h.Sum(out[:0])
	if debugHash {
		fmt.Fprintf(os.Stderr, "HASH[%s]: %x\n", h.name, out)
	}
	if h.buf != nil {
		hashDebug.Lock()
		if hashDebug.m == nil {
			hashDebug.m = make(map[[HashSize]byte]string)
		}
		hashDebug.m[out] = h.buf.String()
		hashDebug.Unlock()
	}
	return out
}

// In GODEBUG=gocacheverify=1 mode,
// hashDebug holds the input to every computed hash ID,
// so that we can work backward from the ID involved in a
// cache entry mismatch to a description of what should be there.
var hashDebug struct {
	sync.Mutex
	m map[[HashSize]byte]string
}

// reverseHash returns the input used to compute the hash id.
func reverseHash(id [HashSize]byte) string {
	hashDebug.Lock()
	s := hashDebug.m[id]
	hashDebug.Unlock()
	return s
}

var hashFileCache struct {
	sync.Mutex
	m map[string][HashSize]byte
}

// FileHash returns the hash of the named file.
// It caches repeated lookups for a given file,
// and the cache entry for a file can be initialized
// using SetFileHash.
// The hash used by FileHash is not the same as
// the hash used by NewHash.
func FileHash(file string) ([HashSize]byte, error) {
	hashFileCache.Lock()
	out, ok := hashFileCache.m[file]
	hashFileCache.Unlock()

	if ok {
		return out, nil
	}

	h := sha256.New()
	f, err := os.Open(file)
	if err != nil {
		if debugHash {
			fmt.Fprintf(os.Stderr, "HASH %s: %v\n", file, err)
		}
		return [HashSize]byte{}, err
	}
	_, err = io.Copy(h, f)
	f.Close()
	if err != nil {
		if debugHash {
			fmt.Fprintf(os.Stderr, "HASH %s: %v\n", file, err)
		}
		return [HashSize]byte{}, err
	}
	h.Sum(out[:0])
	if debugHash {
		fmt.Fprintf(os.Stderr, "HASH %s: %x\n", file, out)
	}

	SetFileHash(file, out)
	return out, nil
}

// SetFileHash sets the hash returned by FileHash for file.
func SetFileHash(file string, sum [HashSize]byte) {
	hashFileCache.Lock()
	if hashFileCache.m == nil {
		hashFileCache.m = make(map[string][HashSize]byte)
	}
	hashFileCache.m[file] = sum
	hashFileCache.Unlock()
}
<|endoftext|>


<commit_before>package LLVMCodegen

import (
	"io/ioutil"
	"os"
	"os/exec"
	"strings"

	"github.com/ark-lang/ark/src/parser"
	"llvm.org/llvm/bindings/go/llvm"
)

type OutputType int

const (
	OUTPUT_ASSEMBLY OutputType = iota
	OUTPUT_OBJECT
	OUTPUT_LLVM_IR
	OUTPUT_LLVM_BC
	OUTPUT_EXECUTABLE
)

func (v *Codegen) createBitcode(file *parser.Module) string {
	filename := file.Name + ".bc"

	fileHandle, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
	if err != nil {
		v.err("Couldn't create bitcode file "+filename+": `%s`", err.Error())
	}
	defer fileHandle.Close()

	if err := llvm.WriteBitcodeToFile(file.Module, fileHandle); err != nil {
		v.err("failed to write bitcode to file for "+file.Name+": `%s`", err.Error())
	}

	return filename
}

func (v *Codegen) bitcodeToASM(filename string) string {
	asmName := filename

	if strings.HasSuffix(filename, ".bc") {
		asmName = asmName[:len(asmName)-3]
	}

	asmName += ".s"

	cmd := exec.Command("llc", filename, "-o", asmName)
	if out, err := cmd.CombinedOutput(); err != nil {
		v.err("Failed to convert bitcode to assembly: `%s`\n%s", err.Error(), string(out))
	}

	return asmName
}

func (v *Codegen) asmToObject(filename string) string {
	objName := filename

	if strings.HasSuffix(filename, ".s") {
		objName = objName[:len(objName)-2]
	}

	objName += ".o"

	if v.Compiler == "" {
		envcc := os.Getenv("CC")
		if envcc != "" {
			v.Compiler = envcc
		} else {
			v.Compiler = "cc"
		}
	}

	args := append(v.CompilerArgs, "-fno-PIE", "-c", filename, "-o", objName)

	cmd := exec.Command(v.Compiler, args...)
	if out, err := cmd.CombinedOutput(); err != nil {
		v.err("Failed to convert assembly to object file: `%s`\n%s", err.Error(), string(out))
	}

	return objName
}

func (v *Codegen) createIR(mod *parser.Module) string {
	filename := mod.Name + ".ll"

	err := ioutil.WriteFile(filename, []byte(mod.Module.String()), 0666)
	if err != nil {
		v.err("Couldn't write IR file "+filename+": `%s`", err.Error())
	}

	return filename
}

func (v *Codegen) createBinary() {
	// god this is a long and ugly function

	if v.OutputType == OUTPUT_LLVM_IR {
		for _, file := range v.input {
			v.createIR(file)
		}
		return
	}

	linkArgs := append(v.LinkerArgs, "-fno-PIE", "-nodefaultlibs", "-lc", "-lm")

	bitcodeFiles := []string{}

	for _, file := range v.input {
		bitcodeFiles = append(bitcodeFiles, v.createBitcode(file))
	}

	if v.OutputType == OUTPUT_LLVM_BC {
		return
	}

	asmFiles := []string{}

	for _, name := range bitcodeFiles {
		asmName := v.bitcodeToASM(name)
		asmFiles = append(asmFiles, asmName)
	}

	for _, bc := range bitcodeFiles {
		if err := os.Remove(bc); err != nil {
			v.err("Failed to remove "+bc+": `%s`", err.Error())
		}
	}

	if v.OutputType == OUTPUT_ASSEMBLY {
		return
	}

	objFiles := []string{}

	for _, asmFile := range asmFiles {
		objName := v.asmToObject(asmFile)

		objFiles = append(objFiles, objName)
		linkArgs = append(linkArgs, objName)
	}

	for _, asmFile := range asmFiles {
		os.Remove(asmFile)
	}

	if v.OutputType == OUTPUT_OBJECT {
		return
	}

	if v.OutputName == "" {
		panic("OutputName is empty")
	}

	linkArgs = append(linkArgs, "-o", v.OutputName)

	if v.Linker == "" {
		v.Linker = "cc"
	}

	cmd := exec.Command(v.Linker, linkArgs...)
	if out, err := cmd.CombinedOutput(); err != nil {
		v.err("failed to link object files: `%s`\n%s", err.Error(), string(out))
	}

	for _, objFile := range objFiles {
		os.Remove(objFile)
	}
}
<commit_msg>Use codegen output name when generating bitcode.<commit_after>package LLVMCodegen

import (
	"io/ioutil"
	"os"
	"os/exec"
	"strings"

	"github.com/ark-lang/ark/src/parser"
	"llvm.org/llvm/bindings/go/llvm"
)

type OutputType int

const (
	OUTPUT_ASSEMBLY OutputType = iota
	OUTPUT_OBJECT
	OUTPUT_LLVM_IR
	OUTPUT_LLVM_BC
	OUTPUT_EXECUTABLE
)

func (v *Codegen) createBitcode(file *parser.Module) string {
	filename := v.OutputName + "-" + file.Name + ".bc"

	fileHandle, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
	if err != nil {
		v.err("Couldn't create bitcode file "+filename+": `%s`", err.Error())
	}
	defer fileHandle.Close()

	if err := llvm.WriteBitcodeToFile(file.Module, fileHandle); err != nil {
		v.err("failed to write bitcode to file for "+file.Name+": `%s`", err.Error())
	}

	return filename
}

func (v *Codegen) bitcodeToASM(filename string) string {
	asmName := filename

	if strings.HasSuffix(filename, ".bc") {
		asmName = asmName[:len(asmName)-3]
	}

	asmName += ".s"

	cmd := exec.Command("llc", filename, "-o", asmName)
	if out, err := cmd.CombinedOutput(); err != nil {
		v.err("Failed to convert bitcode to assembly: `%s`\n%s", err.Error(), string(out))
	}

	return asmName
}

func (v *Codegen) asmToObject(filename string) string {
	objName := filename

	if strings.HasSuffix(filename, ".s") {
		objName = objName[:len(objName)-2]
	}

	objName += ".o"

	if v.Compiler == "" {
		envcc := os.Getenv("CC")
		if envcc != "" {
			v.Compiler = envcc
		} else {
			v.Compiler = "cc"
		}
	}

	args := append(v.CompilerArgs, "-fno-PIE", "-c", filename, "-o", objName)

	cmd := exec.Command(v.Compiler, args...)
	if out, err := cmd.CombinedOutput(); err != nil {
		v.err("Failed to convert assembly to object file: `%s`\n%s", err.Error(), string(out))
	}

	return objName
}

func (v *Codegen) createIR(mod *parser.Module) string {
	filename := v.OutputName + ".ll"

	err := ioutil.WriteFile(filename, []byte(mod.Module.String()), 0666)
	if err != nil {
		v.err("Couldn't write IR file "+filename+": `%s`", err.Error())
	}

	return filename
}

func (v *Codegen) createBinary() {
	// god this is a long and ugly function

	if v.OutputType == OUTPUT_LLVM_IR {
		for _, file := range v.input {
			v.createIR(file)
		}
		return
	}

	linkArgs := append(v.LinkerArgs, "-fno-PIE", "-nodefaultlibs", "-lc", "-lm")

	bitcodeFiles := []string{}

	for _, file := range v.input {
		bitcodeFiles = append(bitcodeFiles, v.createBitcode(file))
	}

	if v.OutputType == OUTPUT_LLVM_BC {
		return
	}

	asmFiles := []string{}

	for _, name := range bitcodeFiles {
		asmName := v.bitcodeToASM(name)
		asmFiles = append(asmFiles, asmName)
	}

	for _, bc := range bitcodeFiles {
		if err := os.Remove(bc); err != nil {
			v.err("Failed to remove "+bc+": `%s`", err.Error())
		}
	}

	if v.OutputType == OUTPUT_ASSEMBLY {
		return
	}

	objFiles := []string{}

	for _, asmFile := range asmFiles {
		objName := v.asmToObject(asmFile)

		objFiles = append(objFiles, objName)
		linkArgs = append(linkArgs, objName)
	}

	for _, asmFile := range asmFiles {
		os.Remove(asmFile)
	}

	if v.OutputType == OUTPUT_OBJECT {
		return
	}

	if v.OutputName == "" {
		panic("OutputName is empty")
	}

	linkArgs = append(linkArgs, "-o", v.OutputName)

	if v.Linker == "" {
		v.Linker = "cc"
	}

	cmd := exec.Command(v.Linker, linkArgs...)
	if out, err := cmd.CombinedOutput(); err != nil {
		v.err("failed to link object files: `%s`\n%s", err.Error(), string(out))
	}

	for _, objFile := range objFiles {
		os.Remove(objFile)
	}
}
<|endoftext|>


<commit_before>package www

import (
	"config"
	"dto"
	"fmt"
	"io"
	"net/http"
	"os"
	"strconv"
	"strings"
	"time"

	"github.com/afex/hystrix-go/hystrix"
	"github.com/fvbock/endless"
	"github.com/gorilla/handlers"
	"github.com/inconshreveable/log15"
	"github.com/labstack/echo"
)

var log log15.Logger

func StartWebServer() error {
	log = log15.New("module", "webserver")
	conf, err := config.GetConfig()
	if err != nil {
		return err
	}

	var hystrixTimeout time.Duration
	conf.Hystrix.Timeout = strings.TrimSpace(conf.Hystrix.Timeout)
	if conf.Hystrix.Timeout != "" {
		hystrixTimeout, err = time.ParseDuration(conf.Hystrix.Timeout)
		if err != nil || hystrixTimeout < time.Millisecond {
			hystrixTimeout = time.Second
			log.Error(fmt.Sprintf("Use default time for hystrix timeout %s", hystrixTimeout))
		}
	}

	hystrix.ConfigureCommand("waitFor", hystrix.CommandConfig{
		Timeout:                int(int64(hystrixTimeout) / int64(time.Millisecond)), // converted into Millisecond.
		MaxConcurrentRequests:  conf.Hystrix.MaxConcurrentRequests,
		ErrorPercentThreshold:  conf.Hystrix.ErrorPercentThreshold,
		RequestVolumeThreshold: conf.Hystrix.RequestVolumeThreshold,
		SleepWindow:            conf.Hystrix.SleepWindow,
	})

	conf.Endless.DefaultHammerTime = strings.TrimSpace(conf.Endless.DefaultHammerTime)
	if conf.Endless.DefaultHammerTime != "" {
		duration, err := time.ParseDuration(conf.Endless.DefaultHammerTime)
		if err == nil {
			endless.DefaultHammerTime = duration
		} else {
			log.Error("Bad format for Endless/DefaultHammerTime " + conf.Endless.DefaultHammerTime + " err: " + err.Error())
		}
	}

	e := echo.New()
	e.Post("/api/v1/tweet", createTweetV1)
	e.Get("/api/v1/tweets/:id", getAllTweetForV1)
	e.Get("/api/v1/wait/:timeout", waitFor)
	e.Get("/api/v1/wait_protected/:timeout", waitForProtected)
	//e.Static("/", "/www/static")
	log.Info("Launching server on " + conf.Web.Address)
	err = endless.ListenAndServe(conf.Web.Address, handlers.LoggingHandler(os.Stdout, handlers.CompressHandler(e.Router())))
	if err != nil {
		log.Error(fmt.Sprintf("Error during start web server : %s", err))
	}
	return nil
}

func getAllTweetForV1(c *echo.Context) error {
	id := c.Param("id")
	tweets, err := dto.GetAllTweetsForTimeLine(id)
	if err != nil {
		return c.JSON(http.StatusInternalServerError, err)
	}
	if len(tweets) == 0 {
		return c.JSON(http.StatusNotFound, nil)
	}
	return c.JSON(http.StatusOK, tweets)
}

func createTweetV1(c *echo.Context) error {
	tweet := new(dto.Tweet)
	err := c.Bind(tweet)
	if err != nil {
		log.Error(err.Error())
		return c.JSON(http.StatusBadRequest, nil)
	}
	tweet.GenerateId()
	err = tweet.Insert()
	if err != nil {
		return c.JSON(http.StatusInternalServerError, err)
	}
	return c.JSON(http.StatusOK, tweet)
}

func waitFor(c *echo.Context) error {
	timeout, err := time.ParseDuration(c.Param("timeout"))
	if err != nil {
		timeout = 500 * time.Millisecond
	}

	time.Sleep(timeout)
	return c.JSON(http.StatusOK, timeout.String()+" by pid = "+strconv.Itoa(os.Getpid()))
}

func waitForProtected(c *echo.Context) error {
	var response *http.Response
	hystrix.Do("waitFor", func() error {
		var err error
		response, err = http.Get("http://127.0.0.1:8080/api/v1/wait/" + c.Param("timeout"))
		//response, err = http.Get(fmt.Sprintf("%s://%s%s", c.Request().URL.Scheme, c.Request().URL.Host, c.Request().URL.Path))
		if err != nil {
			return err
		}
		r := response.Body
		w := c.Response().Writer()
		io.Copy(w, r)
		return nil
	}, func(err error) error {
		log.Error(err.Error())
		c.JSON(http.StatusInternalServerError, err.Error())
		return nil
	})

	return nil
}
<commit_msg>Improve log when restart.<commit_after>package www

import (
	"config"
	"dto"
	"fmt"
	"io"
	"net/http"
	"os"
	"strconv"
	"strings"
	"sync/atomic"
	"syscall"
	"time"

	"github.com/afex/hystrix-go/hystrix"
	"github.com/fvbock/endless"
	"github.com/gorilla/handlers"
	"github.com/inconshreveable/log15"
	"github.com/labstack/echo"
)

var (
	log        log15.Logger
	terminated int32
)

func StartWebServer() error {
	log = log15.New("module", "webserver")
	conf, err := config.GetConfig()
	if err != nil {
		return err
	}

	var hystrixTimeout time.Duration
	conf.Hystrix.Timeout = strings.TrimSpace(conf.Hystrix.Timeout)
	if conf.Hystrix.Timeout != "" {
		hystrixTimeout, err = time.ParseDuration(conf.Hystrix.Timeout)
		if err != nil || hystrixTimeout < time.Millisecond {
			hystrixTimeout = time.Second
			log.Error(fmt.Sprintf("Use default time for hystrix timeout %s", hystrixTimeout))
		}
	}

	hystrix.ConfigureCommand("waitFor", hystrix.CommandConfig{
		Timeout:                int(int64(hystrixTimeout) / int64(time.Millisecond)), // converted into Millisecond.
		MaxConcurrentRequests:  conf.Hystrix.MaxConcurrentRequests,
		ErrorPercentThreshold:  conf.Hystrix.ErrorPercentThreshold,
		RequestVolumeThreshold: conf.Hystrix.RequestVolumeThreshold,
		SleepWindow:            conf.Hystrix.SleepWindow,
	})

	conf.Endless.DefaultHammerTime = strings.TrimSpace(conf.Endless.DefaultHammerTime)
	if conf.Endless.DefaultHammerTime != "" {
		duration, err := time.ParseDuration(conf.Endless.DefaultHammerTime)
		if err == nil {
			endless.DefaultHammerTime = duration
		} else {
			log.Error("Bad format for Endless/DefaultHammerTime " + conf.Endless.DefaultHammerTime + " err: " + err.Error())
		}
	}

	e := echo.New()
	e.Post("/api/v1/tweet", createTweetV1)
	e.Get("/api/v1/tweets/:id", getAllTweetForV1)
	e.Get("/api/v1/wait/:timeout", waitFor)
	e.Get("/api/v1/wait_protected/:timeout", waitForProtected)
	//e.Static("/", "/www/static")
	log.Info(fmt.Sprintf("Launching server [pid=%s] on %s", strconv.Itoa(os.Getpid()), conf.Web.Address))
	//err = endless.ListenAndServe(conf.Web.Address, handlers.LoggingHandler(os.Stdout, handlers.CompressHandler(e.Router())))
	srv := endless.NewServer(conf.Web.Address, handlers.LoggingHandler(os.Stdout, handlers.CompressHandler(e.Router())))
	preHookFunc := func() {
		atomic.StoreInt32(&terminated, 1)
	}
	srv.RegisterSignalHook(endless.PRE_SIGNAL, syscall.SIGHUP, preHookFunc)
	srv.RegisterSignalHook(endless.PRE_SIGNAL, syscall.SIGINT, preHookFunc)
	srv.RegisterSignalHook(endless.PRE_SIGNAL, syscall.SIGTERM, preHookFunc)
	err = srv.ListenAndServe()
	if atomic.LoadInt32(&terminated) == 0 {
		if err != nil {
			log.Error(fmt.Sprintf("During startup of server [pid=%s], this error has occurred : %s", strconv.Itoa(os.Getpid()), err))
		}
		return err
	} else {
		log.Info(fmt.Sprintf("Server [pid=%s] is going to shutdown", strconv.Itoa(os.Getpid())))
		return nil
	}
}

func getAllTweetForV1(c *echo.Context) error {
	id := c.Param("id")
	tweets, err := dto.GetAllTweetsForTimeLine(id)
	if err != nil {
		return c.JSON(http.StatusInternalServerError, err)
	}
	if len(tweets) == 0 {
		return c.JSON(http.StatusNotFound, nil)
	}
	return c.JSON(http.StatusOK, tweets)
}

func createTweetV1(c *echo.Context) error {
	tweet := new(dto.Tweet)
	err := c.Bind(tweet)
	if err != nil {
		log.Error(err.Error())
		return c.JSON(http.StatusBadRequest, nil)
	}
	tweet.GenerateId()
	err = tweet.Insert()
	if err != nil {
		return c.JSON(http.StatusInternalServerError, err)
	}
	return c.JSON(http.StatusOK, tweet)
}

func waitFor(c *echo.Context) error {
	timeout, err := time.ParseDuration(c.Param("timeout"))
	if err != nil {
		timeout = 500 * time.Millisecond
	}

	time.Sleep(timeout)
	return c.JSON(http.StatusOK, timeout.String()+" by pid = "+strconv.Itoa(os.Getpid()))
}

func waitForProtected(c *echo.Context) error {
	var response *http.Response
	hystrix.Do("waitFor", func() error {
		var err error
		response, err = http.Get("http://127.0.0.1:8080/api/v1/wait/" + c.Param("timeout"))
		//response, err = http.Get(fmt.Sprintf("%s://%s%s", c.Request().URL.Scheme, c.Request().URL.Host, c.Request().URL.Path))
		if err != nil {
			return err
		}
		r := response.Body
		w := c.Response().Writer()
		io.Copy(w, r)
		return nil
	}, func(err error) error {
		log.Error(err.Error())
		c.JSON(http.StatusInternalServerError, err.Error())
		return nil
	})

	return nil
}
<|endoftext|>


<commit_before>package utils

import (
	"bytes"
	"text/template"
	"os/exec"
	"syscall"
	"fmt"
	"github.com/pkg/errors"
	"github.com/Sirupsen/logrus"
	"io/ioutil"
	"os"
)

type Bash struct {
	Command string
	PipeFail bool
	Arguments map[string]string
	NoLog bool

	retCode int
	stdout string
	stderr string
	err error
}

func (b *Bash) build() error {
	Assert(b.Command != "", "Command cannot be emptry string")

	if (b.Arguments != nil) {
		tmpl, err := template.New("script").Parse(b.Command)
		if err != nil {
			return err
		}

		var buf bytes.Buffer
		err = tmpl.Execute(&buf, b.Arguments)
		if err != nil {
			return err
		}

		b.Command = buf.String()
	}

	if b.PipeFail {
		b.Command = fmt.Sprintf("set -o pipefail; %s", b.Command)
	}

	return nil
}

func (b *Bash) Run() error {
	ret, so, se, err := b.RunWithReturn()
	if err != nil {
		return errors.Wrap(err, fmt.Sprintf("failed to execute the command[%s] because of an internal errro",  b.Command))
	}

	if ret != 0 {
		return errors.New(fmt.Sprintf("failed to exectue the command[%s]\nreturn code:%d\nstdout:%s\nstderr:%s\n",
			b.Command, ret, so, se))
	}

	return nil
}

func (b *Bash) RunWithReturn() (retCode int, stdout, stderr string, err error) {
	if err = b.build(); err != nil {
		b.err = err
		return -1, "", "", err
	}

	if !b.NoLog {
		logrus.Debugf("shell start: %s", b.Command)
	}

	var so, se bytes.Buffer
	var cmd *exec.Cmd

	if len(b.Command) > 1024* 4 {
		content := []byte(b.Command)
		tmpfile, err := ioutil.TempFile("", "zvrcommand"); PanicOnError(err)
		tmpfile.Write(content); PanicOnError(err)
		//path := "/home/vyos/zvrcommand"
		cmd = exec.Command("bash", "-c", tmpfile.Name())
		//ioutil.WriteFile(path, content, 0777)
		//cmd = exec.Command("bash", "-c", path)
		defer func() {
			tmpfile.Close()
			os.Remove(tmpfile.Name())
		}()
	} else {
		cmd = exec.Command("bash", "-c", b.Command)
	}

	cmd.Stdout = &so
	cmd.Stderr = &se

	var waitStatus syscall.WaitStatus
	if err := cmd.Run(); err != nil {
		if exitError, ok := err.(*exec.ExitError); ok {
			waitStatus = exitError.Sys().(syscall.WaitStatus)
			retCode = waitStatus.ExitStatus()
		} else {
			panic(errors.Errorf("unable to get return code, %s", err))
		}
	} else {
		waitStatus = cmd.ProcessState.Sys().(syscall.WaitStatus)
		retCode = waitStatus.ExitStatus()
	}

	stdout = string(so.Bytes())
	stderr = string(se.Bytes())

	b.retCode = retCode
	b.stdout = stdout
	b.stderr = stderr

	if !b.NoLog {
		logrus.WithFields(logrus.Fields{
			"return code": fmt.Sprintf("%v", retCode),
			"stdout": stdout,
			"stderr": stderr,
		}).Debugf("shell done: %s", b.Command)
	}

	return
}

func (bash *Bash) PanicIfError() {
	if bash.err != nil {
		panic(errors.New(fmt.Sprintf("shell failure[command: %v], internal error: %v",
			bash.Command, bash.err)))
	}

	if bash.retCode != 0 {
		panic(errors.New(fmt.Sprintf("shell failure[command: %v, return code: %v, stdout: %v, stderr: %v",
			bash.Command, bash.retCode, bash.stdout, bash.stderr)))
	}
}

func NewBash() *Bash {
	return &Bash{}
}

<commit_msg>use /home/vyos for command execution<commit_after>package utils

import (
	"bytes"
	"text/template"
	"os/exec"
	"syscall"
	"fmt"
	"github.com/pkg/errors"
	"github.com/Sirupsen/logrus"
	"io/ioutil"
	"os"
)

type Bash struct {
	Command string
	PipeFail bool
	Arguments map[string]string
	NoLog bool

	retCode int
	stdout string
	stderr string
	err error
}

func (b *Bash) build() error {
	Assert(b.Command != "", "Command cannot be emptry string")

	if (b.Arguments != nil) {
		tmpl, err := template.New("script").Parse(b.Command)
		if err != nil {
			return err
		}

		var buf bytes.Buffer
		err = tmpl.Execute(&buf, b.Arguments)
		if err != nil {
			return err
		}

		b.Command = buf.String()
	}

	if b.PipeFail {
		b.Command = fmt.Sprintf("set -o pipefail; %s", b.Command)
	}

	return nil
}

func (b *Bash) Run() error {
	ret, so, se, err := b.RunWithReturn()
	if err != nil {
		return errors.Wrap(err, fmt.Sprintf("failed to execute the command[%s] because of an internal errro",  b.Command))
	}

	if ret != 0 {
		return errors.New(fmt.Sprintf("failed to exectue the command[%s]\nreturn code:%d\nstdout:%s\nstderr:%s\n",
			b.Command, ret, so, se))
	}

	return nil
}

func (b *Bash) RunWithReturn() (retCode int, stdout, stderr string, err error) {
	if err = b.build(); err != nil {
		b.err = err
		return -1, "", "", err
	}

	if !b.NoLog {
		logrus.Debugf("shell start: %s", b.Command)
	}

	var so, se bytes.Buffer
	var cmd *exec.Cmd

	if len(b.Command) > 1024* 4 {
		content := []byte(b.Command)
		tmpfile, err := ioutil.TempFile("/home/vyos", "zvrcommand"); PanicOnError(err)
		tmpfile.Write(content); PanicOnError(err)
		//path := "/home/vyos/zvrcommand"
		cmd = exec.Command("bash", "-c", tmpfile.Name())
		//ioutil.WriteFile(path, content, 0777)
		//cmd = exec.Command("bash", "-c", path)
		defer func() {
			tmpfile.Close()
			os.Remove(tmpfile.Name())
		}()
	} else {
		cmd = exec.Command("bash", "-c", b.Command)
	}

	cmd.Stdout = &so
	cmd.Stderr = &se

	var waitStatus syscall.WaitStatus
	if err := cmd.Run(); err != nil {
		if exitError, ok := err.(*exec.ExitError); ok {
			waitStatus = exitError.Sys().(syscall.WaitStatus)
			retCode = waitStatus.ExitStatus()
		} else {
			panic(errors.Errorf("unable to get return code, %s", err))
		}
	} else {
		waitStatus = cmd.ProcessState.Sys().(syscall.WaitStatus)
		retCode = waitStatus.ExitStatus()
	}

	stdout = string(so.Bytes())
	stderr = string(se.Bytes())

	b.retCode = retCode
	b.stdout = stdout
	b.stderr = stderr

	if !b.NoLog {
		logrus.WithFields(logrus.Fields{
			"return code": fmt.Sprintf("%v", retCode),
			"stdout": stdout,
			"stderr": stderr,
		}).Debugf("shell done: %s", b.Command)
	}

	return
}

func (bash *Bash) PanicIfError() {
	if bash.err != nil {
		panic(errors.New(fmt.Sprintf("shell failure[command: %v], internal error: %v",
			bash.Command, bash.err)))
	}

	if bash.retCode != 0 {
		panic(errors.New(fmt.Sprintf("shell failure[command: %v, return code: %v, stdout: %v, stderr: %v",
			bash.Command, bash.retCode, bash.stdout, bash.stderr)))
	}
}

func NewBash() *Bash {
	return &Bash{}
}

<|endoftext|>


<commit_before>// +build !windows,!darwin

// 7 july 2014

package ui

import (
	"unsafe"
)

// #include "gtk_unix.h"
// extern gboolean windowClosing(GtkWidget *, GdkEvent *, gpointer);
import "C"

type window struct {
	widget	*C.GtkWidget
	wc		*C.GtkContainer
	bin		*C.GtkBin
	window	*C.GtkWindow

	closing	*event

	*container
}

func newWindow(title string, width int, height int, control Control) *window {
	widget := C.gtk_window_new(C.GTK_WINDOW_TOPLEVEL)
	ctitle := togstr(title)
	defer freegstr(ctitle)
	w := &window{
		widget:		widget,
		wc:			(*C.GtkContainer)(unsafe.Pointer(widget)),
		bin:			(*C.GtkBin)(unsafe.Pointer(widget)),
		window:		(*C.GtkWindow)(unsafe.Pointer(widget)),
		closing:		newEvent(),
	}
	C.gtk_window_set_title(w.window, ctitle)
	g_signal_connect(
		C.gpointer(unsafe.Pointer(w.window)),
		"delete-event",
		C.GCallback(C.windowClosing),
		C.gpointer(unsafe.Pointer(w)))
	C.gtk_window_resize(w.window, C.gint(width), C.gint(height))
	w.container = newContainer(control)
	// TODO make a method of container
	C.gtk_container_add(w.wc, w.container.layoutwidget)
	return w
}

func (w *window) Title() string {
	return fromgstr(C.gtk_window_get_title(w.window))
}

func (w *window) SetTitle(title string) {
	ctitle := togstr(title)
	defer freegstr(ctitle)
	C.gtk_window_set_title(w.window, ctitle)
}

func (w *window) Show() {
	C.gtk_widget_show_all(w.widget)
}

func (w *window) Hide() {
	C.gtk_widget_hide(w.widget)
}

func (w *window) Close() {
	C.gtk_widget_destroy(w.widget)
}

func (w *window) OnClosing(e func() bool) {
	w.closing.setbool(e)
}

//export windowClosing
func windowClosing(wid *C.GtkWidget, e *C.GdkEvent, data C.gpointer) C.gboolean {
	w := (*window)(unsafe.Pointer(data))
	close := w.closing.fire()
	if close {
		return C.GDK_EVENT_PROPAGATE		// will do gtk_widget_destroy(), which is what we want (thanks ebassi in irc.gimp.net/#gtk+)
	}
	return C.GDK_EVENT_STOP				// keeps window alive
}
<commit_msg>More TODOs.<commit_after>// +build !windows,!darwin

// 7 july 2014

package ui

import (
	"unsafe"
)

// #include "gtk_unix.h"
// extern gboolean windowClosing(GtkWidget *, GdkEvent *, gpointer);
import "C"

type window struct {
	widget	*C.GtkWidget
	wc		*C.GtkContainer
	bin		*C.GtkBin
	window	*C.GtkWindow

	closing	*event

	*container
}

func newWindow(title string, width int, height int, control Control) *window {
	widget := C.gtk_window_new(C.GTK_WINDOW_TOPLEVEL)
	ctitle := togstr(title)
	defer freegstr(ctitle)
	w := &window{
		widget:		widget,
		wc:			(*C.GtkContainer)(unsafe.Pointer(widget)),
		bin:			(*C.GtkBin)(unsafe.Pointer(widget)),
		window:		(*C.GtkWindow)(unsafe.Pointer(widget)),
		closing:		newEvent(),
	}
	C.gtk_window_set_title(w.window, ctitle)
	g_signal_connect(
		C.gpointer(unsafe.Pointer(w.window)),
		"delete-event",
		C.GCallback(C.windowClosing),
		C.gpointer(unsafe.Pointer(w)))
	C.gtk_window_resize(w.window, C.gint(width), C.gint(height))
	w.container = newContainer(control)
	// TODO make a method of container? because this would conflict with tabs...
	C.gtk_container_add(w.wc, w.container.layoutwidget)
	return w
}

func (w *window) Title() string {
	return fromgstr(C.gtk_window_get_title(w.window))
}

func (w *window) SetTitle(title string) {
	ctitle := togstr(title)
	defer freegstr(ctitle)
	C.gtk_window_set_title(w.window, ctitle)
}

func (w *window) Show() {
	C.gtk_widget_show_all(w.widget)
}

func (w *window) Hide() {
	C.gtk_widget_hide(w.widget)
}

func (w *window) Close() {
	C.gtk_widget_destroy(w.widget)
}

func (w *window) OnClosing(e func() bool) {
	w.closing.setbool(e)
}

//export windowClosing
func windowClosing(wid *C.GtkWidget, e *C.GdkEvent, data C.gpointer) C.gboolean {
	w := (*window)(unsafe.Pointer(data))
	close := w.closing.fire()
	if close {
		return C.GDK_EVENT_PROPAGATE		// will do gtk_widget_destroy(), which is what we want (thanks ebassi in irc.gimp.net/#gtk+)
	}
	return C.GDK_EVENT_STOP				// keeps window alive
}
<|endoftext|>


<commit_before>package pager551

type pager struct{}

func New() *pager {
	return &pager{}
}
<commit_msg>refs #2 ページネーション用のインスタンスを生成する<commit_after>package pager551

import "strconv"

type pager struct {
	AllItems     int64
	ItemsPerPage int64
	Page         int64
	Prev         *pageItem
	Next         *pageItem
	Pages        []*pageItem
	minPage      int64
	maxPage      int64
}

type pageItem struct {
	Disable bool
	Active  bool
	Label   string
	Page    int64
}

func New(allItems, itemsPerPage, page int64) *pager {
	pager := &pager{
		AllItems:     allItems,
		ItemsPerPage: itemsPerPage,
		Page:         page,
	}

	pager.load()

	return pager
}

func (p *pager) load() {
	if p.AllItems <= p.ItemsPerPage {
		p.noPageItems()
		return
	}

	max := p.AllItems / p.ItemsPerPage
	if p.AllItems%p.ItemsPerPage != 0 {
		max++
	}

	if max > 5 {
		p.minPage = p.Page - 2
		p.maxPage = p.Page + 2
		switch {
		case p.minPage < 1:
			p.minPage = 1
			p.maxPage = p.minPage + 4
		case p.maxPage > max:
			p.maxPage = max
			p.minPage = p.maxPage - 4
		}
	} else {
		p.minPage = 1
		p.maxPage = max
	}

	p.setPageItems()

}

func (p *pager) noPageItems() {
	prev := prevPageItem()
	next := nextPageItem()
	page := pagePageItem(1)

	prev.Disable = true
	next.Disable = true
	page.Active = true

	p.Prev = prev
	p.Next = next
	p.Pages = append(p.Pages, page)

}

func (p *pager) setPageItems() {
	p.setPrevItem()
	p.setNextItem()
	p.setPageItem()

}

func (p *pager) setPrevItem() {
	prev := prevPageItem()

	if p.Page <= 1 {
		prev.Disable = true
	} else {
		prev.Page = p.Page - 1
	}

	p.Prev = prev

}

func (p *pager) setNextItem() {
	next := nextPageItem()

	if p.maxPage <= p.Page {
		next.Disable = true
	} else {
		next.Page = p.Page + 1
	}

	p.Next = next

}

func (p *pager) setPageItem() {
	for i := p.minPage; i <= p.maxPage; i++ {
		page := pagePageItem(i)
		if i == p.Page {
			page.Active = true
		}
		p.Pages = append(p.Pages, page)
	}
}

func prevPageItem() *pageItem {
	return &pageItem{
		Disable: false,
		Active:  false,
		Label:   "«",
		Page:    0,
	}
}

func nextPageItem() *pageItem {
	return &pageItem{
		Disable: false,
		Active:  false,
		Label:   "»",
		Page:    0,
	}
}

func pagePageItem(page int64) *pageItem {
	return &pageItem{
		Disable: false,
		Active:  false,
		Label:   strconv.FormatInt(page, 10),
		Page:    page,
	}
}
<|endoftext|>


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

package main

import (
	"flag"
	"fmt"
	"log"
	"net/http"
	"strings"

	"github.com/gregjones/httpcache"
	"github.com/gregjones/httpcache/diskcache"
	"github.com/willnorris/imageproxy/proxy"
)

// goxc values
var (
	// VERSION is the version string for imageproxy.
	VERSION string
	// BUILD_DATE is the timestamp of when imageproxy was built.
	BUILD_DATE string
)

var addr = flag.String("addr", "localhost:8080", "TCP address to listen on")
var whitelist = flag.String("whitelist", "", "comma separated list of allowed remote hosts")
var cacheDir = flag.String("cacheDir", "", "directory to use for file cache")
var version = flag.Bool("version", false, "print version information")

func main() {
	flag.Parse()

	if *version {
		fmt.Printf("%v\nBuild: %v\n", VERSION, BUILD_DATE)
		return
	}

	fmt.Printf("imageproxy (version %v) listening on %s\n", VERSION, *addr)

	var c httpcache.Cache
	if *cacheDir != "" {
		c = diskcache.New(*cacheDir)
	} else {
		c = httpcache.NewMemoryCache()
	}

	p := proxy.NewProxy(nil, c)
	p.MaxWidth = 2000
	p.MaxHeight = 2000
	if *whitelist != "" {
		p.Whitelist = strings.Split(*whitelist, ",")
	}
	server := &http.Server{
		Addr:    *addr,
		Handler: p,
	}
	err := server.ListenAndServe()
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}
<commit_msg>add default VERSION string<commit_after>// Copyright 2013 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
	"flag"
	"fmt"
	"log"
	"net/http"
	"strings"

	"github.com/gregjones/httpcache"
	"github.com/gregjones/httpcache/diskcache"
	"github.com/willnorris/imageproxy/proxy"
)

// goxc values
var (
	// VERSION is the version string for imageproxy.
	VERSION = "HEAD"

	// BUILD_DATE is the timestamp of when imageproxy was built.
	BUILD_DATE string
)

var addr = flag.String("addr", "localhost:8080", "TCP address to listen on")
var whitelist = flag.String("whitelist", "", "comma separated list of allowed remote hosts")
var cacheDir = flag.String("cacheDir", "", "directory to use for file cache")
var version = flag.Bool("version", false, "print version information")

func main() {
	flag.Parse()

	if *version {
		fmt.Printf("%v\nBuild: %v\n", VERSION, BUILD_DATE)
		return
	}

	var c httpcache.Cache
	if *cacheDir != "" {
		c = diskcache.New(*cacheDir)
	} else {
		c = httpcache.NewMemoryCache()
	}

	p := proxy.NewProxy(nil, c)
	p.MaxWidth = 2000
	p.MaxHeight = 2000
	if *whitelist != "" {
		p.Whitelist = strings.Split(*whitelist, ",")
	}

	server := &http.Server{
		Addr:    *addr,
		Handler: p,
	}

	fmt.Printf("imageproxy (version %v) listening on %s\n", VERSION, server.Addr)
	err := server.ListenAndServe()
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}
<|endoftext|>


<commit_before>package disk

import (
	bosherr "github.com/cloudfoundry/bosh-utils/errors"

	"bosh-google-cpi/api"
	"bosh-google-cpi/util"

	"google.golang.org/api/compute/v1"
)

func (d GoogleDiskService) Resize(id string, newSize int) error {
	newsizeGB := &compute.DisksResizeRequest{
		SizeGb: int64(newSize),
	}

	disk, found, err := d.Find(id, "")
	if err != nil {
		return err
	}
	if !found {
		return api.NewDiskNotFoundError(id, false)
	}

	if disk.SizeGb == newsizeGB.SizeGb {
		d.logger.Debug(googleDiskServiceLogTag, "Skipping resize Google Disk '%s', becasue current value '%s'is equal to new value '%s'", id, disk.SizeGb, newsizeGB)
	} else if disk.SizeGb > newsizeGB.SizeGb {
		return bosherr.WrapErrorf(err, "Skipping resize Google Disk '%s', cannot resize volume to a smaller size from '%s' to '%s'", id, disk.SizeGb, newsizeGB)
	}

	d.logger.Debug(googleDiskServiceLogTag, "Resizing Google Disk '%s'", id)
	operation, err := d.computeService.Disks.Resize(d.project, util.ResourceSplitter(disk.Zone), id, newsizeGB).Do()
	if err != nil {
		return bosherr.WrapErrorf(err, "Failed to resize Google Disk '%s'", id)
	}

	if _, err = d.operationService.Waiter(operation, disk.Zone, ""); err != nil {
		return bosherr.WrapErrorf(err, "Failed to resize Google Disk '%s'", id)
	}

	return nil
}
<commit_msg>value is not a string<commit_after>package disk

import (
	bosherr "github.com/cloudfoundry/bosh-utils/errors"

	"bosh-google-cpi/api"
	"bosh-google-cpi/util"

	"google.golang.org/api/compute/v1"
)

func (d GoogleDiskService) Resize(id string, newSize int) error {
	newsizeGB := &compute.DisksResizeRequest{
		SizeGb: int64(newSize),
	}

	disk, found, err := d.Find(id, "")
	if err != nil {
		return err
	}
	if !found {
		return api.NewDiskNotFoundError(id, false)
	}

	if disk.SizeGb == newsizeGB.SizeGb {
		d.logger.Debug(googleDiskServiceLogTag, "Skipping resize Google Disk '%s', becasue current value '%#v'is equal to new value '%#v'", id, disk.SizeGb, newsizeGB)
	} else if disk.SizeGb > newsizeGB.SizeGb {
		return bosherr.WrapErrorf(err, "Skipping resize Google Disk '%s', cannot resize volume to a smaller size from '%#v' to '%#v'", id, disk.SizeGb, newsizeGB)
	}

	d.logger.Debug(googleDiskServiceLogTag, "Resizing Google Disk '%s'", id)
	operation, err := d.computeService.Disks.Resize(d.project, util.ResourceSplitter(disk.Zone), id, newsizeGB).Do()
	if err != nil {
		return bosherr.WrapErrorf(err, "Failed to resize Google Disk '%s'", id)
	}

	if _, err = d.operationService.Waiter(operation, disk.Zone, ""); err != nil {
		return bosherr.WrapErrorf(err, "Failed to resize Google Disk '%s'", id)
	}

	return nil
}
<|endoftext|>


<commit_before>package commandevaluators

import (
	"errors"
	"log"

	"github.com/byuoitav/av-api/base"
	"github.com/byuoitav/av-api/dbo"
)

//ChangeVideoInputVideoswitcher f
type ChangeVideoInputVideoswitcher struct {
}

//Evaluate f
func (c *ChangeVideoInputVideoswitcher) Evaluate(room base.PublicRoom) ([]base.ActionStructure, error) {
	actionList := []base.ActionStructure{}

	if len(room.CurrentVideoInput) != 0 {
		devices, err := dbo.GetDevicesByBuildingAndRoomAndRole(room.Building, room.Room, "VideoOut")
		if err != nil {
			return []base.ActionStructure{}, err
		}

		switcher, err := dbo.GetDevicesByBuildingAndRoomAndRole(room.Building, room.Room, "VideoSwitcher")
		if err != nil {
			return []base.ActionStructure{}, err
		}
		if len(switcher) != 1 {
			return []base.ActionStructure{}, errors.New("too many switchers/none available")
		}

		for _, device := range devices {
			for _, port := range switcher[0].Ports {
				if port.Destination == device.Name && port.Source == room.CurrentVideoInput {
					m := make(map[string]string)
					m["port"] = port.Name

					tempAction := base.ActionStructure{
						Action:              "ChangeInput",
						GeneratingEvaluator: "ChangeVideoInputVideoswitcher",
						Device:              switcher[0],
						Parameters:          m,
						DeviceSpecific:      false,
						Overridden:          false,
					}

					actionList = append(actionList, tempAction)
					break
				}
			}
		}
	}
	return actionList, nil
}

//Validate f
func (c *ChangeVideoInputVideoswitcher) Validate(action base.ActionStructure) error {
	// me just guessing what to do?
	//
	//
	//
	log.Printf("Validating action for command %v", action.Action)

	// check if ChangeInput is a valid name of a command (ok is a bool)
	ok, _ := CheckCommands(action.Device.Commands, "ChangeInput")

	// returns and error if the ChangeInput command doesn't exist or if the command isn't ChangeInput
	if !ok || action.Action != "ChangeInput" {
		log.Printf("ERROR. %s is an invalid command for %s", action.Action, action.Device.Name)
		return errors.New(action.Action + "is not an invalid command for " + action.Device.Name)
	}

	log.Print("done.")
	return nil
	//
	//
	//
	//  end danny's code
}

//GetIncompatibleCommands f
func (c *ChangeVideoInputVideoswitcher) GetIncompatibleCommands() []string {
	return nil
}
<commit_msg>started adding way to switch just a display(?)<commit_after>package commandevaluators

import (
	"errors"
	"log"

	"github.com/byuoitav/av-api/base"
	"github.com/byuoitav/av-api/dbo"
	"github.com/byuoitav/configuration-database-microservice/accessors"
)

//ChangeVideoInputVideoswitcher the struct that implements the CommandEvaluation struct
type ChangeVideoInputVideoswitcher struct {
}

//Evaluate f
func (c *ChangeVideoInputVideoswitcher) Evaluate(room base.PublicRoom) ([]base.ActionStructure, error) {
	actionList := []base.ActionStructure{}

	if len(room.CurrentVideoInput) != 0 {
		devices, err := dbo.GetDevicesByBuildingAndRoomAndRole(room.Building, room.Room, "VideoOut")
		if err != nil {
			return []base.ActionStructure{}, err
		}

		switcher, err := dbo.GetDevicesByBuildingAndRoomAndRole(room.Building, room.Room, "VideoSwitcher")
		if err != nil {
			return []base.ActionStructure{}, err
		}
		if len(switcher) != 1 {
			return []base.ActionStructure{}, errors.New("too many switchers/none available")
		}

		for _, device := range devices {
			for _, port := range switcher[0].Ports {
				if port.Destination == device.Name && port.Source == room.CurrentVideoInput {
					m := make(map[string]string)
					m["port"] = port.Name

					tempAction := base.ActionStructure{
						Action:              "ChangeInput",
						GeneratingEvaluator: "ChangeVideoInputVideoswitcher",
						Device:              switcher[0],
						Parameters:          m,
						DeviceSpecific:      false,
						Overridden:          false,
					}

					actionList = append(actionList, tempAction)
					break
				}
			}
		}
	}

	// if there is at least one display
	if len(room.Displays) != 0 {

		// interate through all displays in the room, create an ActionStructure if it has an input
		for _, display := range room.Displays {

			// if the display has an input, create the action
			if len(display.Input) != 0 {

				tempAction := base.ActionStructure{
					Action:              "ChangeInput",
					GeneratingEvaluator: "ChangeVideoInputVideoswitcher",
					Device:              display, // or is it the switcher[0]?
					Parameters:          m,
					DeviceSpecific:      true,
					Overridden:          false,
				}
			}
		}

	}
	return actionList, nil
}

//GetSwitcherAndCreateAction f
func (c *ChangeVideoInputVideoswitcher) GetSwitcherAndCreateAction(room, device accessors.Device) {

}

//Validate f
func (c *ChangeVideoInputVideoswitcher) Validate(action base.ActionStructure) error {
	log.Printf("Validating action for command %v", action.Action)

	// check if ChangeInput is a valid name of a command (ok is a bool)
	ok, _ := CheckCommands(action.Device.Commands, "ChangeInput")

	// returns and error if the ChangeInput command doesn't exist or if the command isn't ChangeInput
	if !ok || action.Action != "ChangeInput" {
		log.Printf("ERROR. %s is an invalid command for %s", action.Action, action.Device.Name)
		return errors.New(action.Action + "is not an invalid command for " + action.Device.Name)
	}

	log.Print("done.")
	return nil
}

//GetIncompatibleCommands f
func (c *ChangeVideoInputVideoswitcher) GetIncompatibleCommands() []string {
	return nil
}
<|endoftext|>


<commit_before>// Package instructor is
package instructor

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

// Finder is a function type that is used to load an object, serialized into a struct
// from the integrating-applications list of structs
type Finder func(string) (interface{}, error)

// Converter is a function type that is used to convert a string to an associated type
// You'd wrap whatever logic you needed, including something like just JSON Unmarshalling
// to turn a string representation of a value into a concrete instance of it's type.
// Four come out of the box for you: string, bool, int, and float64
type Converter func(string) (interface{}, error)

// Internal types used to be more explicit about the purposes of these maps
type heap map[string]interface{}
type finders map[string]Finder
type converters map[string]Converter
type fragment struct {
	token Token
	text  string
}
type statement []fragment

// Version is the current semver for this tool
const Version = "0.1.6"

// Instructor is an instance of the object which will allow you to inspect structs
type Instructor struct {
	interpreter *interpreter
}

// New returns a new Instructor
func New() *Instructor {
	return &Instructor{
		interpreter: newInterpreter(),
	}
}

// RegisterFinder is for registering one of your custom finders to look up your structs
func (i *Instructor) RegisterFinder(name string, f Finder) {
	i.interpreter.RegisterFinder(name, f)
}

// RegisterConverter is for registering one of your custom converters to convert cli arguments to typed values
func (i *Instructor) RegisterConverter(name string, c Converter) {
	i.interpreter.RegisterConverter(name, c)
}

// REPL will enter the read eval print loop, blocking the main thread until it exits
func (i *Instructor) REPL() error {
	// Buffered reader off of STDIN
	reader := bufio.NewReader(os.Stdin)
	input := ""
	stop := false
	var err error
	// Print welcome message
	fmt.Printf("Welcome to Inspector v%s\n", Version)
	fmt.Printf("For a list of commands, type help\n")
	for !stop {
		fmt.Printf("instructor %s >>", Version)
		if input, err = reader.ReadString('\n'); err != nil {
			fmt.Printf("Error reading from STDIN: %s\n", err.Error())
		}
		input = strings.TrimSpace(input)
		switch input {
		case "quit":
			stop = true
		case "help":
			fmt.Println("You can call the following commands:")
			fmt.Println("quit : exits the REPL")
			fmt.Println("help : prints this screen")
			fmt.Println("find : Looks up an object by it's type and ID")
			fmt.Println("\t\tEx: u = find(User,\"123456789\")")
			fmt.Println("You can call methods or invoke Properties on an object. You can provide arguments by giving their type and value, in the order they're defined on the method")
			fmt.Println("\t\tEx: u.Strawmethod(false ,50)")
			fmt.Println("\t\tEc: u.Strawproperty")
		default:
			l := newLexer(strings.NewReader(input))
			var f fragment
			s := make(statement, 0)
			for f.token != EOF {
				f = l.scan()
				s = append(s, f)
			}
			// TODO just lass the lexer straight into evaluate
			i.interpreter.Evaluate(s)
		}
	}
	return nil
}
<commit_msg>Version bump to 0.1.7<commit_after>// Package instructor is
package instructor

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

// Finder is a function type that is used to load an object, serialized into a struct
// from the integrating-applications list of structs
type Finder func(string) (interface{}, error)

// Converter is a function type that is used to convert a string to an associated type
// You'd wrap whatever logic you needed, including something like just JSON Unmarshalling
// to turn a string representation of a value into a concrete instance of it's type.
// Four come out of the box for you: string, bool, int, and float64
type Converter func(string) (interface{}, error)

// Internal types used to be more explicit about the purposes of these maps
type heap map[string]interface{}
type finders map[string]Finder
type converters map[string]Converter
type fragment struct {
	token Token
	text  string
}
type statement []fragment

// Version is the current semver for this tool
const Version = "0.1.7"

// Instructor is an instance of the object which will allow you to inspect structs
type Instructor struct {
	interpreter *interpreter
}

// New returns a new Instructor
func New() *Instructor {
	return &Instructor{
		interpreter: newInterpreter(),
	}
}

// RegisterFinder is for registering one of your custom finders to look up your structs
func (i *Instructor) RegisterFinder(name string, f Finder) {
	i.interpreter.RegisterFinder(name, f)
}

// RegisterConverter is for registering one of your custom converters to convert cli arguments to typed values
func (i *Instructor) RegisterConverter(name string, c Converter) {
	i.interpreter.RegisterConverter(name, c)
}

// REPL will enter the read eval print loop, blocking the main thread until it exits
func (i *Instructor) REPL() error {
	// Buffered reader off of STDIN
	reader := bufio.NewReader(os.Stdin)
	input := ""
	stop := false
	var err error
	// Print welcome message
	fmt.Printf("Welcome to Inspector v%s\n", Version)
	fmt.Printf("For a list of commands, type help\n")
	for !stop {
		fmt.Printf("instructor %s >>", Version)
		if input, err = reader.ReadString('\n'); err != nil {
			fmt.Printf("Error reading from STDIN: %s\n", err.Error())
		}
		input = strings.TrimSpace(input)
		switch input {
		case "quit":
			stop = true
		case "help":
			fmt.Println("You can call the following commands:")
			fmt.Println("quit : exits the REPL")
			fmt.Println("help : prints this screen")
			fmt.Println("find : Looks up an object by it's type and ID")
			fmt.Println("\t\tEx: u = find(User,\"123456789\")")
			fmt.Println("You can call methods or invoke Properties on an object. You can provide arguments by giving their type and value, in the order they're defined on the method")
			fmt.Println("\t\tEx: u.Strawmethod(false ,50)")
			fmt.Println("\t\tEc: u.Strawproperty")
		default:
			l := newLexer(strings.NewReader(input))
			var f fragment
			s := make(statement, 0)
			for f.token != EOF {
				f = l.scan()
				s = append(s, f)
			}
			// TODO just lass the lexer straight into evaluate
			i.interpreter.Evaluate(s)
		}
	}
	return nil
}
<|endoftext|>


<commit_before>package ztex

import (
	"fmt"
	"strings"
)

// FlashEnabled indicates whether or not the flash is enabled.
type FlashEnabled uint8

// String returns a human-readable description of whether or not the
// flash is enabled.
func (f FlashEnabled) String() string {
	switch f {
	case 0:
		return "Disabled"
	case 1:
		return "Enabled"
	default:
		return "Unknown"
	}
}

// FlashSector represents the size of a sector in the flash.
type FlashSector [2]uint8

// String returns a human-readable description of the size of a sector
// in the flash.
func (f FlashSector) String() string { return binaryPrefix(f.Number(), "B") }

// Number returns the size of a sector in the flash (in bytes).
func (f FlashSector) Number() uint64 {
	z := uint64(bytesToUint16(f))
	if z&0x8000 != 0 {
		z = 1 << (z & 0x7fff)
	}
	return z
}

// FlashCount represents the number of sectors in the flash.
type FlashCount [4]uint8

// String returns a human-readable description of the number of sectors
// in the flash.
func (f FlashCount) String() string { return fmt.Sprintf("%v", f.Number()) }

// Number returns the number of sectors in the flash.
func (f FlashCount) Number() uint32 { return bytesToUint32(f) }

// FlashError represents the error code in the flash.
type FlashError uint8

// FlashStatus indicates the current status of the flash.
type FlashStatus struct {
	FlashEnabled
	FlashSector
	FlashCount
	FlashError
}

// String returns a human-readable description of the flash status.
func (f FlashStatus) String() string {
	x := []string{}
	x = append(x, fmt.Sprintf("Enabled(%v)", f.FlashEnabled))
	x = append(x, fmt.Sprintf("Sector(%v)", f.FlashSector))
	x = append(x, fmt.Sprintf("Count(%v)", f.FlashCount))
	x = append(x, fmt.Sprintf("Error(%v)", f.FlashError))
	return strings.Join(x, ", ")
}
<commit_msg>Add descriptions for flash error strings.<commit_after>package ztex

import (
	"fmt"
	"strings"
)

// FlashEnabled indicates whether or not the flash is enabled.
type FlashEnabled uint8

// String returns a human-readable description of whether or not the
// flash is enabled.
func (f FlashEnabled) String() string {
	switch f {
	case 0:
		return "Disabled"
	case 1:
		return "Enabled"
	default:
		return "Unknown"
	}
}

// FlashSector represents the size of a sector in the flash.
type FlashSector [2]uint8

// String returns a human-readable description of the size of a sector
// in the flash.
func (f FlashSector) String() string { return binaryPrefix(f.Number(), "B") }

// Number returns the size of a sector in the flash (in bytes).
func (f FlashSector) Number() uint64 {
	z := uint64(bytesToUint16(f))
	if z&0x8000 != 0 {
		z = 1 << (z & 0x7fff)
	}
	return z
}

// FlashCount represents the number of sectors in the flash.
type FlashCount [4]uint8

// String returns a human-readable description of the number of sectors
// in the flash.
func (f FlashCount) String() string { return fmt.Sprintf("%v", f.Number()) }

// Number returns the number of sectors in the flash.
func (f FlashCount) Number() uint32 { return bytesToUint32(f) }

// FlashError represents the error code in the flash.
type FlashError uint8

// String returns a human-readable description of the flash error code.
func (f FlashError) String() string {
	switch f {
	case 0:
		return "Ok"
	case 1:
		return "Command Error"
	case 2:
		return "Timeout Error"
	case 3:
		return "Busy Error"
	case 4:
		return "Pending Error"
	case 5:
		return "Read Error"
	case 6:
		return "Write Error"
	case 7:
		return "Unsupported Error"
	case 8:
		return "Runtime Error"
	default:
		return fmt.Sprintf("Unknown Error [%v]", uint8(f))
	}
}

// FlashStatus indicates the current status of the flash.
type FlashStatus struct {
	FlashEnabled
	FlashSector
	FlashCount
	FlashError
}

// String returns a human-readable description of the flash status.
func (f FlashStatus) String() string {
	x := []string{}
	x = append(x, fmt.Sprintf("Enabled(%v)", f.FlashEnabled))
	x = append(x, fmt.Sprintf("Sector(%v)", f.FlashSector))
	x = append(x, fmt.Sprintf("Count(%v)", f.FlashCount))
	x = append(x, fmt.Sprintf("Error(%v)", f.FlashError))
	return strings.Join(x, ", ")
}
<|endoftext|>


<commit_before>package parse_test

import (
	. "github.com/yanda15/hdc/hive"
	"log"
	"testing"
	"time"
)

var h *Hive

type Sample7 struct {
	Code        string `tag_name:"code"`
	Description string `tag_name:"description"`
	Total_emp   int    `tag_name:"total_emp"`
	Salary      int    `tag_name:"salary"`
}

type SampleParse struct {
	Code        string    `tag_name:"code"`
	Description string    `tag_name:"description"`
	Total_emp   int       `tag_name:"total_emp"`
	Salary      int       `tag_name:"salary"`
	Date        time.Time `tag_name:"date"`
}

func TestParseOutput(t *testing.T) {
	res := []string{"'00-0000','All Occupations CSV','134354250','40690','2014-05-01'", "'00-0000','All Occupations NEXT','134354250','40690','2014-05-01'"}
	tmp := []SampleParse{}
	Parse([]string{}, res, &tmp, "csv", "yyyy-MM-dd")
	log.Println(tmp)

	res = []string{"00-0000,All Occupations CSV2,134354250,40690,2014-Oct-01", "00-0000,All Occupations CSV2 NEXT,134354250,40690,2014-Dec-01"}
	tmp = []SampleParse{}
	Parse([]string{}, res, &tmp, "csv", "yyyy-MMM-dd")
	log.Println(tmp)

	res = []string{"'00-0000'\t'All Occupations TSV'\t'134354250'\t'40690'\t'2014-Dec-05'", "'00-0000'\t'All Occupations TSV NEXT'\t'134354250'\t'40690'\t'2014-Dec-05'"}
	tmp = []SampleParse{}
	Parse([]string{}, res, &tmp, "tsv", "yyyy-MMM-dd")
	log.Println(tmp)

	resj := []string{"{ \"code\" : \"00-0000\" , \"description\" : \"All Occupations JSON\" "}
	tmpj := []SampleParse{}
	Parse([]string{}, resj, &tmpj, "json", "")
	log.Println(tmpj)

	resj = []string{", \"total_emp\" : 134354, \"salary\" : 40690,\"Date\" : \"2012-04-23T18:25:43Z\" },{ \"code\" : \"00-2222\""}
	tmpj = []SampleParse{}
	Parse([]string{}, resj, &tmpj, "json", "")
	log.Println(tmpj)

	resj = []string{",\"description\" : \"All Occupations INTERFACE\" , \"total_emp\" : 222, \"salary\" : 2222,\"Date\" : \"2012-05-23T18:25:43Z\" },{ \"code\" : \"00-2222\",\"description\" : \"All Occupations NEXT\" , \"total_emp\" : 222, \"salary\" : 2222,\"Date\" : \"2012-05-23T18:25:43Z\" }", "{ \"code\" : \"00-2222\",\"description\" : \"All Occupations Last\" , \"total_emp\" : 222, \"salary\" : 2222,\"Date\" : \"2012-05-23T18:25:43Z\" }"}
	var tmpx interface{}
	Parse([]string{}, resj, &tmpx, "json", "")
	log.Println(tmpx)

}

func TestParseOutputOneStruct(t *testing.T) {

	res := "'00-0000','All Occupations CSV','134354250','40690','2014-05-01'"
	var tmp interface{}
	e := Parse([]string{"code", "desc", "emp", "sal", "date"}, res, &tmp, "csv", "yyyy-MM-dd")
	log.Println(tmp)
	// log.Println(e)

	res = "00-0000,All Occupations CSV2,134354250,40690,2014-05-01"
	tmpt := SampleParse{}
	Parse([]string{}, res, &tmpt, "csv", "yyyy-MM-dd")
	log.Println(tmpt)

	res = "'00-0000'\t'All Occupations TSV'\t'13.4354.250'\t'40690'\t'2014-Dec-05'"
	var tmpz interface{}
	e = Parse([]string{"code", "desc", "emp", "sal", "date"}, res, &tmpz, "tsv", "yyyy-MM-dd")
	log.Println(tmpz)
	// log.Println(e)
	_ = e

	//try to parse json with different line
	resj := "{ \"code\" : \"00-0000\" , \"description\" : \"All Occupations JSON\" "
	tmpj := SampleParse{}
	Parse([]string{}, resj, &tmpj, "json", "")
	log.Println(tmpj)

	resj = ", \"total_emp\" : 134354, \"salary\" : 40690,\"Date\" : \"2012-04-23T18:25:43Z\" },{ \"code\" : \"00-2222\""
	tmpj = SampleParse{}
	Parse([]string{}, resj, &tmpj, "json", "")
	log.Println(tmpj)

	resj = ",\"description\" : \"All Occupations INTERFACE\" , \"total_emp\" : 222, \"salary\" : 2222,\"Date\" : \"2012-05-23T18:25:43Z\" },{ \"code\" : \"00-2222\",\"description\" : \"All Occupations NEXT\" , \"total_emp\" : 222, \"salary\" : 2222,\"Date\" : \"2012-05-23T18:25:43Z\" }"
	var tmpx interface{}
	Parse([]string{}, resj, &tmpx, "json", "")
	log.Println(tmpx)

}
<commit_msg>import changes<commit_after>package parse_test

import (
	. "github.com/eaciit/hdc/hive"
	"log"
	"testing"
	"time"
)

var h *Hive

type Sample7 struct {
	Code        string `tag_name:"code"`
	Description string `tag_name:"description"`
	Total_emp   int    `tag_name:"total_emp"`
	Salary      int    `tag_name:"salary"`
}

type SampleParse struct {
	Code        string    `tag_name:"code"`
	Description string    `tag_name:"description"`
	Total_emp   int       `tag_name:"total_emp"`
	Salary      int       `tag_name:"salary"`
	Date        time.Time `tag_name:"date"`
}

func TestParseOutput(t *testing.T) {
	res := []string{"'00-0000','All Occupations CSV','134354250','40690','2014-05-01'", "'00-0000','All Occupations NEXT','134354250','40690','2014-05-01'"}
	tmp := []SampleParse{}
	Parse([]string{}, res, &tmp, "csv", "yyyy-MM-dd")
	log.Println(tmp)

	res = []string{"00-0000,All Occupations CSV2,134354250,40690,2014-Oct-01", "00-0000,All Occupations CSV2 NEXT,134354250,40690,2014-Dec-01"}
	tmp = []SampleParse{}
	Parse([]string{}, res, &tmp, "csv", "yyyy-MMM-dd")
	log.Println(tmp)

	res = []string{"'00-0000'\t'All Occupations TSV'\t'134354250'\t'40690'\t'2014-Dec-05'", "'00-0000'\t'All Occupations TSV NEXT'\t'134354250'\t'40690'\t'2014-Dec-05'"}
	tmp = []SampleParse{}
	Parse([]string{}, res, &tmp, "tsv", "yyyy-MMM-dd")
	log.Println(tmp)

	resj := []string{"{ \"code\" : \"00-0000\" , \"description\" : \"All Occupations JSON\" "}
	tmpj := []SampleParse{}
	Parse([]string{}, resj, &tmpj, "json", "")
	log.Println(tmpj)

	resj = []string{", \"total_emp\" : 134354, \"salary\" : 40690,\"Date\" : \"2012-04-23T18:25:43Z\" },{ \"code\" : \"00-2222\""}
	tmpj = []SampleParse{}
	Parse([]string{}, resj, &tmpj, "json", "")
	log.Println(tmpj)

	resj = []string{",\"description\" : \"All Occupations INTERFACE\" , \"total_emp\" : 222, \"salary\" : 2222,\"Date\" : \"2012-05-23T18:25:43Z\" },{ \"code\" : \"00-2222\",\"description\" : \"All Occupations NEXT\" , \"total_emp\" : 222, \"salary\" : 2222,\"Date\" : \"2012-05-23T18:25:43Z\" }", "{ \"code\" : \"00-2222\",\"description\" : \"All Occupations Last\" , \"total_emp\" : 222, \"salary\" : 2222,\"Date\" : \"2012-05-23T18:25:43Z\" }"}
	var tmpx interface{}
	Parse([]string{}, resj, &tmpx, "json", "")
	log.Println(tmpx)

}

func TestParseOutputOneStruct(t *testing.T) {

	res := "'00-0000','All Occupations CSV','134354250','40690','2014-05-01'"
	var tmp interface{}
	e := Parse([]string{"code", "desc", "emp", "sal", "date"}, res, &tmp, "csv", "yyyy-MM-dd")
	log.Println(tmp)
	// log.Println(e)

	res = "00-0000,All Occupations CSV2,134354250,40690,2014-05-01"
	tmpt := SampleParse{}
	Parse([]string{}, res, &tmpt, "csv", "yyyy-MM-dd")
	log.Println(tmpt)

	res = "'00-0000'\t'All Occupations TSV'\t'13.4354.250'\t'40690'\t'2014-Dec-05'"
	var tmpz interface{}
	e = Parse([]string{"code", "desc", "emp", "sal", "date"}, res, &tmpz, "tsv", "yyyy-MM-dd")
	log.Println(tmpz)
	// log.Println(e)
	_ = e

	//try to parse json with different line
	resj := "{ \"code\" : \"00-0000\" , \"description\" : \"All Occupations JSON\" "
	tmpj := SampleParse{}
	Parse([]string{}, resj, &tmpj, "json", "")
	log.Println(tmpj)

	resj = ", \"total_emp\" : 134354, \"salary\" : 40690,\"Date\" : \"2012-04-23T18:25:43Z\" },{ \"code\" : \"00-2222\""
	tmpj = SampleParse{}
	Parse([]string{}, resj, &tmpj, "json", "")
	log.Println(tmpj)

	resj = ",\"description\" : \"All Occupations INTERFACE\" , \"total_emp\" : 222, \"salary\" : 2222,\"Date\" : \"2012-05-23T18:25:43Z\" },{ \"code\" : \"00-2222\",\"description\" : \"All Occupations NEXT\" , \"total_emp\" : 222, \"salary\" : 2222,\"Date\" : \"2012-05-23T18:25:43Z\" }"
	var tmpx interface{}
	Parse([]string{}, resj, &tmpx, "json", "")
	log.Println(tmpx)

}
<|endoftext|>


<commit_before>// Copyright 2016 Koichi Shiraishi. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package commands

import (
	"encoding/json"
	"fmt"
	"os/exec"
	"time"

	"nvim-go/config"
	"nvim-go/context"
	"nvim-go/nvim"
	"nvim-go/nvim/profile"
	"nvim-go/nvim/quickfix"

	"github.com/garyburd/neovim-go/vim"
	"github.com/garyburd/neovim-go/vim/plugin"
)

func init() {
	plugin.HandleCommand("Gometalinter", &plugin.CommandOptions{Eval: "getcwd()"}, cmdMetalinter)
}

func cmdMetalinter(v *vim.Vim, cwd string) {
	go Metalinter(v, cwd)
}

type metalinterResult struct {
	Linter   string `json:"linter"`   // name of linter tool
	Severity string `json:"severity"` // result of type
	Path     string `json:"path"`     // path of file
	Line     int    `json:"line"`     // line of file
	Col      int    `json:"col"`      // col of file
	Message  string `json:"message"`  // description of linter message
}

// Metalinter lint the Go sources from current buffer's package use gometalinter tool.
func Metalinter(v *vim.Vim, cwd string) error {
	defer profile.Start(time.Now(), "GoMetaLinter")
	var ctxt = context.Build{}
	defer ctxt.SetContext(cwd)()

	var (
		loclist []*quickfix.ErrorlistData
		b       vim.Buffer
		w       vim.Window
	)

	p := v.NewPipeline()
	p.CurrentBuffer(&b)
	p.CurrentWindow(&w)
	if err := p.Wait(); err != nil {
		return err
	}

	args := []string{cwd + "/...", "--json", "--disable-all", "--deadline", config.MetalinterDeadline}
	for _, t := range config.MetalinterTools {
		args = append(args, "--enable", t)
	}
	if len(config.MetalinterSkipDir) != 0 {
		for _, dir := range config.MetalinterSkipDir {
			args = append(args, "--skip", dir)
		}
	}

	cmd := exec.Command("gometalinter", args...)
	cmd.Dir = cwd
	stdout, _ := cmd.Output()
	cmd.Run()

	var result = []metalinterResult{}
	if err := json.Unmarshal(stdout, &result); err != nil {
		fmt.Println(err)
	}

	for _, r := range result {
		var errorType string
		switch r.Severity {
		case "error":
			errorType = "E"
		case "warning":
			errorType = "W"
		}
		loclist = append(loclist, &quickfix.ErrorlistData{
			FileName: r.Path,
			LNum:     r.Line,
			Col:      r.Col,
			Text:     r.Linter + ": " + r.Message,
			Type:     errorType,
		})
	}

	if err := quickfix.SetLoclist(v, loclist); err != nil {
		return nvim.Echomsg(v, "Gometalinter: %v", err)
	}
	return quickfix.OpenLoclist(v, w, loclist, true)
}
<commit_msg>cmds/metalinter: Fix base directory to use context & Add resurt sort<commit_after>// Copyright 2016 Koichi Shiraishi. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package commands

import (
	"encoding/json"
	"os/exec"
	"sort"
	"strings"
	"time"

	"nvim-go/config"
	"nvim-go/context"
	"nvim-go/nvim"
	"nvim-go/nvim/profile"
	"nvim-go/nvim/quickfix"

	"github.com/garyburd/neovim-go/vim"
	"github.com/garyburd/neovim-go/vim/plugin"
)

func init() {
	plugin.HandleCommand("Gometalinter", &plugin.CommandOptions{Eval: "getcwd()"}, cmdMetalinter)
}

func cmdMetalinter(v *vim.Vim, cwd string) {
	go Metalinter(v, cwd)
}

type metalinterResult struct {
	Linter   string `json:"linter"`   // name of linter tool
	Severity string `json:"severity"` // result of type
	Path     string `json:"path"`     // path of file
	Line     int    `json:"line"`     // line of file
	Col      int    `json:"col"`      // col of file
	Message  string `json:"message"`  // description of linter message
}

// Metalinter lint the Go sources from current buffer's package use gometalinter tool.
func Metalinter(v *vim.Vim, cwd string) error {
	defer profile.Start(time.Now(), "GoMetaLinter")
	var ctxt = context.Build{}
	defer ctxt.SetContext(cwd)()

	var (
		loclist []*quickfix.ErrorlistData
		b       vim.Buffer
		w       vim.Window
	)

	p := v.NewPipeline()
	p.CurrentBuffer(&b)
	p.CurrentWindow(&w)
	if err := p.Wait(); err != nil {
		return err
	}

	var args []string
	switch ctxt.Tool {
	case "go":
		args = append(args, cwd+"/...")
	case "gb":
		args = append(args, ctxt.ProjectDir+"/...")
	}
	args = append(args, []string{"--json", "--disable-all", "--deadline", config.MetalinterDeadline}...)

	for _, t := range config.MetalinterTools {
		args = append(args, "--enable", t)
	}
	if len(config.MetalinterSkipDir) != 0 {
		for _, dir := range config.MetalinterSkipDir {
			args = append(args, "--skip", dir)
		}
	}

	cmd := exec.Command("gometalinter", args...)
	stdout, err := cmd.Output()
	cmd.Run()

	var result = []metalinterResult{}
	if err != nil {
		if err := json.Unmarshal(stdout, &result); err != nil {
			return err
		}
	}

	sort.Sort(byPath(result))

	for _, r := range result {
		loclist = append(loclist, &quickfix.ErrorlistData{
			FileName: nvim.ToRelPath(r.Path, cwd),
			LNum:     r.Line,
			Col:      r.Col,
			Text:     r.Linter + ": " + r.Message,
			Type:     strings.ToUpper(r.Severity[:1]),
		})
	}

	if err := quickfix.SetLoclist(v, loclist); err != nil {
		return nvim.Echomsg(v, "Gometalinter: %v", err)
	}
	return quickfix.OpenLoclist(v, w, loclist, true)
}

type byPath []metalinterResult

func (a byPath) Len() int           { return len(a) }
func (a byPath) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a byPath) Less(i, j int) bool { return a[i].Path < a[j].Path }
<|endoftext|>