<commit_before>package main

import (
	"os"
	"path"

	boshlog "github.com/cloudfoundry/bosh-agent/logger"
	boshsys "github.com/cloudfoundry/bosh-agent/system"
	boshuuid "github.com/cloudfoundry/bosh-agent/uuid"

	bmcmd "github.com/cloudfoundry/bosh-micro-cli/cmd"
	bmconfig "github.com/cloudfoundry/bosh-micro-cli/config"

	bmui "github.com/cloudfoundry/bosh-micro-cli/ui"
)

const mainLogTag = "main"

func main() {
	logger := boshlog.NewLogger(boshlog.LevelError)
	defer logger.HandlePanic("Main")
	fileSystem := boshsys.NewOsFileSystem(logger)
	workspace := path.Join(os.Getenv("HOME"), ".bosh_micro")
	userConfigPath := path.Join(os.Getenv("HOME"), ".bosh_micro.json")
	config, configService := loadUserConfig(userConfigPath, fileSystem, logger)

	uuidGenerator := boshuuid.NewGenerator()

	ui := bmui.NewUI(os.Stdout, os.Stderr, logger)

	cmdFactory := bmcmd.NewFactory(
		config,
		configService,
		fileSystem,
		ui,
		logger,
		uuidGenerator,
		workspace,
	)

	cmdRunner := bmcmd.NewRunner(cmdFactory)

	err := cmdRunner.Run(os.Args[1:])
	if err != nil {
		fail(err, logger)
	}
}

func loadUserConfig(userConfigPath string, fileSystem boshsys.FileSystem, logger boshlog.Logger) (
	bmconfig.UserConfig,
	bmconfig.UserConfigService,
) {
	userConfigService := bmconfig.NewFileSystemUserConfigService(userConfigPath, fileSystem, logger)
	userConfig, err := userConfigService.Load()
	if err != nil {
		fail(err, logger)
	}

	return userConfig, userConfigService
}

func fail(err error, logger boshlog.Logger) {
	logger.Error(mainLogTag, "BOSH Micro CLI failed with: `%s'", err.Error())
	os.Exit(1)
}
<commit_msg>Use err directly rather than err.Error in main.go<commit_after>package main

import (
	"os"
	"path"

	boshlog "github.com/cloudfoundry/bosh-agent/logger"
	boshsys "github.com/cloudfoundry/bosh-agent/system"
	boshuuid "github.com/cloudfoundry/bosh-agent/uuid"

	bmcmd "github.com/cloudfoundry/bosh-micro-cli/cmd"
	bmconfig "github.com/cloudfoundry/bosh-micro-cli/config"

	bmui "github.com/cloudfoundry/bosh-micro-cli/ui"
)

const mainLogTag = "main"

func main() {
	logger := boshlog.NewLogger(boshlog.LevelError)
	defer logger.HandlePanic("Main")
	fileSystem := boshsys.NewOsFileSystem(logger)
	workspace := path.Join(os.Getenv("HOME"), ".bosh_micro")
	userConfigPath := path.Join(os.Getenv("HOME"), ".bosh_micro.json")
	config, configService := loadUserConfig(userConfigPath, fileSystem, logger)

	uuidGenerator := boshuuid.NewGenerator()

	ui := bmui.NewUI(os.Stdout, os.Stderr, logger)

	cmdFactory := bmcmd.NewFactory(
		config,
		configService,
		fileSystem,
		ui,
		logger,
		uuidGenerator,
		workspace,
	)

	cmdRunner := bmcmd.NewRunner(cmdFactory)

	err := cmdRunner.Run(os.Args[1:])
	if err != nil {
		fail(err, logger)
	}
}

func loadUserConfig(userConfigPath string, fileSystem boshsys.FileSystem, logger boshlog.Logger) (
	bmconfig.UserConfig,
	bmconfig.UserConfigService,
) {
	userConfigService := bmconfig.NewFileSystemUserConfigService(userConfigPath, fileSystem, logger)
	userConfig, err := userConfigService.Load()
	if err != nil {
		fail(err, logger)
	}

	return userConfig, userConfigService
}

func fail(err error, logger boshlog.Logger) {
	logger.Error(mainLogTag, "BOSH Micro CLI failed with: %s", err)
	os.Exit(1)
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"fmt"
	"log"
	"os"
	"os/exec"
)

func main() {
	log.SetFlags(log.Lshortfile)
	log.SetPrefix("again: ")

	fail := flag.Bool("fail", false, "run until command fails")
	flag.Parse()
	cmdname := flag.Arg(0)
	if cmdname == "" {
		fmt.Println("usage:", "again cmdname [cmdargs...]")
		os.Exit(1)
	}
	for {
		cmd := exec.Command(cmdname, flag.Args()[1:]...)
		cmd.Stdin = os.Stdin
		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr

		err := cmd.Run()
		if err != nil && !*fail {
			log.Print(err)
		} else if err != nil || !*fail {
			break
		}
	}
}
<commit_msg>Add -sleep flag<commit_after>package main

import (
	"flag"
	"fmt"
	"log"
	"os"
	"os/exec"
	"time"
)

func main() {
	log.SetFlags(log.Lshortfile)
	log.SetPrefix("again: ")

	fail := flag.Bool("fail", false, "run until command fails")
	sleep := flag.Duration("sleep", 0, "how long to sleep before running again?")
	flag.Parse()
	cmdname := flag.Arg(0)
	if cmdname == "" {
		fmt.Println("usage:", "again cmdname [cmdargs...]")
		os.Exit(1)
	}
	for {
		cmd := exec.Command(cmdname, flag.Args()[1:]...)
		cmd.Stdin = os.Stdin
		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr

		err := cmd.Run()
		if err != nil && !*fail {
			log.Print(err)
		} else if err != nil || !*fail {
			break
		}

		if *sleep != 0 {
			time.Sleep(*sleep)
		}
	}
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"os"
	"io"
	"bufio"
	"flag"
	"regexp"
	"strings"
	"strconv"
	. "./src"
	. "./src/interface"
)

//fasta形式で併記されている文字列を読み取り、配列に返す。
func readfasta(i string) (string,string) {

	var reader *bufio.Reader
	var line []byte
	var err error
	var ary [2]string
	// ファイルを読み込みモードでオープン
	read_file, _ := os.OpenFile(i, os.O_RDONLY, 0600)
	// Readerを用意
	reader = bufio.NewReader(read_file)

	//line, _ , err = reader.ReadLine()
	var j = -1
	for {
		line, _ , err = reader.ReadLine()
		// EOFなら終了
		if err == io.EOF {
			break
		}
		// 1行読み出す
		if line[0] == 62 {
      j++
			ary[j] = ""
  	}else{
		  ary[j] += strings.ToLower(string(line))
	  }
	}
	return ary[0], ary[1]
}

func readconfig(i string) Constants {
	var reader *bufio.Reader
	var line []byte
	var err error
  var ary [4][]string
  var ary2int [][]int

	// ファイルを読み込みモードでオープン
	read_file, _ := os.OpenFile(i, os.O_RDONLY, 0600)
	// Readerを用意
	reader = bufio.NewReader(read_file)

	line, _ , err = reader.ReadLine()
	tmp := strings.Split(string(line)," ")

	d, _ := strconv.Atoi(tmp[0])
	e, _ := strconv.Atoi(tmp[1])

	for j:=0;;j++ {
		line,_, err = reader.ReadLine()
		// EOFなら終了
		if err == io.EOF {
			break
		}
		// 1行読み出す
		ary[j] = strings.Fields(string(line))
		var tem []int
    for _, value := range ary[j] {
			temp, _ := strconv.Atoi(value)
			tem = append(tem,temp)
		}
		ary2int = append(ary2int,tem)
	}
	charlist := "acgt"
	var settings *Constants
	settings = NewConstants(d,e,ary2int,charlist)
	return *settings
}

func readfile(i string) string {
	var reader *bufio.Reader
	var line []byte
	var err error

	var ary string
	// ファイルを読み込みモードでオープン
	read_file, _ := os.OpenFile(i, os.O_RDONLY, 0600)
	// Readerを用意
	reader = bufio.NewReader(read_file)

	line, _ , err = reader.ReadLine()
	for {
		// EOFなら終了
		if err == io.EOF {
			break
		}
		// 1行読み出す
		line,_, err = reader.ReadLine()
		ary += string(line)
	}
	return ary
}

func main() {
	flag.Parse()
	var ary string
	var ary2 string
	var settings Constants

	if flag.Arg(1)==""{ //正規表現で、ドットを含むのであれば。
		ary = readfile("sequence.fasta")
		ary2 = readfile("sequence2.fasta")
	} else if m,_ := regexp.MatchString("\\.txt",flag.Arg(1)); m {
		ary, ary2 = readfasta(flag.Arg(1))
		settings = readconfig(flag.Arg(2))
	} else if m,_ := regexp.MatchString("\\.",flag.Arg(2)); m {
		ary = readfile(flag.Arg(1))
		ary2 = readfile(flag.Arg(2))
	} else {
		ary = flag.Arg(1)
		ary2 = flag.Arg(2)
	}

	var lcs DPMatrix
	switch flag.Arg(0) {
		case "1": lcs = NewLCS(ary,ary2)
		case "2": lcs = NewSW(ary,ary2)
		case "3": lcs = NewGotoh(ary,ary2,settings)
		default : lcs = NewNW(ary,ary2)
	}

	lcs.Length() // Exec alignment
	var lx,ly = lcs.Strlen()
	var p,q,r = lcs.Print(lx,ly)
	j:=0
	fmt.Println("Score:",lcs.Score())
	for i:=50; i<len(p)+50; i+=50 {
		if i > len(p) { i = len(p)}
		fmt.Println("from",j,"to",i)
		fmt.Println(p[j:i])
		fmt.Println(q[j:i])
		fmt.Println(r[j:i])
		j = i+1
	}
}
<commit_msg>Fix command line flag<commit_after>package main

import (
	"fmt"
	"os"
	"io"
	"bufio"
	"flag"
	"regexp"
	"strings"
	"strconv"
	. "./src"
	. "./src/interface"
)

//fasta形式で併記されている文字列を読み取り、配列に返す。
func readfasta(i string) (string,string) {

	var reader *bufio.Reader
	var line []byte
	var err error
	var ary [2]string
	// ファイルを読み込みモードでオープン
	read_file, _ := os.OpenFile(i, os.O_RDONLY, 0600)
	// Readerを用意
	reader = bufio.NewReader(read_file)

	//line, _ , err = reader.ReadLine()
	var j = -1
	for {
		line, _ , err = reader.ReadLine()
		// EOFなら終了
		if err == io.EOF {
			break
		}
		// 1行読み出す
		if line[0] == 62 {
      j++
			ary[j] = ""
  	}else{
		  ary[j] += strings.ToLower(string(line))
	  }
	}
	return ary[0], ary[1]
}

func readconfig(i string) Constants {
	var reader *bufio.Reader
	var line []byte
	var err error
  var ary [4][]string
  var ary2int [][]int

	// ファイルを読み込みモードでオープン
	read_file, _ := os.OpenFile(i, os.O_RDONLY, 0600)
	// Readerを用意
	reader = bufio.NewReader(read_file)

	line, _ , err = reader.ReadLine()
	tmp := strings.Split(string(line)," ")

	d, _ := strconv.Atoi(tmp[0])
	e, _ := strconv.Atoi(tmp[1])

	for j:=0;;j++ {
		line,_, err = reader.ReadLine()
		// EOFなら終了
		if err == io.EOF {
			break
		}
		// 1行読み出す
		ary[j] = strings.Fields(string(line))
		var tem []int
    for _, value := range ary[j] {
			temp, _ := strconv.Atoi(value)
			tem = append(tem,temp)
		}
		ary2int = append(ary2int,tem)
	}
	charlist := "acgt"
	var settings *Constants
	settings = NewConstants(d,e,ary2int,charlist)
	return *settings
}

func readfile(i string) string {
	var reader *bufio.Reader
	var line []byte
	var err error

	var ary string
	// ファイルを読み込みモードでオープン
	read_file, _ := os.OpenFile(i, os.O_RDONLY, 0600)
	// Readerを用意
	reader = bufio.NewReader(read_file)

	line, _ , err = reader.ReadLine()
	for {
		// EOFなら終了
		if err == io.EOF {
			break
		}
		// 1行読み出す
		line,_, err = reader.ReadLine()
		ary += string(line)
	}
	return ary
}

func main() {
  var config string
  flag.StringVar(&config,"c","empty","Write a pass of the config file including gap costs and a substitution matrix")
  flag.StringVar(&config,"config","empty","Write a pass of the config file including gap costs and a substitution matrix")
	flag.Parse()
	var ary string
	var ary2 string
	var settings Constants

  if config != "empty"{
		settings = readconfig(config)
  }

	if flag.Arg(1)==""{ //正規表現で、ドットを含むのであれば。
		ary = readfile("sequence.fasta")
		ary2 = readfile("sequence2.fasta")
	} else if m,_ := regexp.MatchString("\\.txt",flag.Arg(1)); m {
		ary, ary2 = readfasta(flag.Arg(1))
	} else if m,_ := regexp.MatchString("\\.",flag.Arg(2)); m {
		ary = readfile(flag.Arg(1))
		ary2 = readfile(flag.Arg(2))
	} else {
		ary = flag.Arg(1)
		ary2 = flag.Arg(2)
	}

	var lcs DPMatrix
	switch flag.Arg(0) {
		case "1": lcs = NewLCS(ary,ary2)
		case "2": lcs = NewSW(ary,ary2)
		case "3": lcs = NewGotoh(ary,ary2,settings)
		default : lcs = NewNW(ary,ary2)
	}

	lcs.Length() // Exec alignment
	var lx,ly = lcs.Strlen()
	var p,q,r = lcs.Print(lx,ly)
	j:=0
	fmt.Println("Score:",lcs.Score())
	for i:=50; i<len(p)+50; i+=50 {
		if i > len(p) { i = len(p)}
		fmt.Println("from",j,"to",i)
		fmt.Println(p[j:i])
		fmt.Println(q[j:i])
		fmt.Println(r[j:i])
		j = i+1
	}
}
<|endoftext|>


<commit_before>package main

import (
	"bufio"
	"encoding/binary"
	"encoding/json"
	"fmt"
	"os"

	"github.com/coreos/etcd/client"
	"github.com/coreos/etcd/mvcc/mvccpb"
	"github.com/golang/protobuf/proto"
)

func transform(n *client.Node) *mvccpb.KeyValue {
	if n.Dir {
		fmt.Fprintf(os.Stderr, "Dir: %s\n", n.Key)
		return nil
	}
	fmt.Fprintf(os.Stderr, "Key: %s\n", n.Key)
	kv := &mvccpb.KeyValue{
		Key:            []byte(n.Key),
		Value:          []byte(n.Value),
		CreateRevision: int64(n.CreatedIndex),
		ModRevision:    int64(n.ModifiedIndex),
		// We can't get version from etcd2 nodes. Assuming all KVs has version 1.
		Version: 1,
	}
	return kv
}

func ExitError(err error) {
	fmt.Fprintf(os.Stderr, "Err: %v\n", err)
	os.Exit(1)
}

func main() {
	reader := bufio.NewReader(os.Stdin)
	decoder := json.NewDecoder(reader)
	writer := bufio.NewWriter(os.Stdout)

	fmt.Fprintf(os.Stderr, "Starting...\n")
	defer fmt.Fprintf(os.Stderr, "Exit...\n")
	for {
		node := &client.Node{}
		if err := decoder.Decode(node); err != nil {
			ExitError(err)
		}
		if node.Key == "" {
			return
		}

		kv := transform(node)
		if kv == nil {
			continue
		}

		data, err := proto.Marshal(kv)
		if err != nil {
			ExitError(err)
		}
		buf := make([]byte, 8)
		binary.LittleEndian.PutUint64(buf, uint64(len(data)))
		if _, err := writer.Write(buf); err != nil {
			ExitError(err)
		}
		if _, err := writer.Write(data); err != nil {
			ExitError(err)
		}
	}
}
<commit_msg>rid bufio<commit_after>package main

import (
	"encoding/binary"
	"encoding/json"
	"fmt"
	"io"
	"os"

	"github.com/coreos/etcd/client"
	"github.com/coreos/etcd/mvcc/mvccpb"
	"github.com/golang/protobuf/proto"
)

func transform(n *client.Node) *mvccpb.KeyValue {
	if n.Dir {
		fmt.Fprintf(os.Stderr, "Dir: %s\n", n.Key)
		return nil
	}
	fmt.Fprintf(os.Stderr, "Key: %s\n", n.Key)
	kv := &mvccpb.KeyValue{
		Key:            []byte(n.Key),
		Value:          []byte(n.Value),
		CreateRevision: int64(n.CreatedIndex),
		ModRevision:    int64(n.ModifiedIndex),
		// We can't get version from etcd2 nodes. Assuming all KVs has version 1.
		Version: 1,
	}
	return kv
}

func ExitError(err error) {
	fmt.Fprintf(os.Stderr, "Err: %v\n", err)
	os.Exit(1)
}

func main() {
	reader := os.Stdin
	decoder := json.NewDecoder(reader)
	writer := os.Stdout

	fmt.Fprintf(os.Stderr, "Starting...\n")
	defer fmt.Fprintf(os.Stderr, "Exiting...\n")
	defer os.Stdout.Sync()
	defer os.Stdout.Close()

	buf := make([]byte, 8)
	for {
		node := &client.Node{}
		if err := decoder.Decode(node); err != nil {
			if err == io.EOF {
				return
			}
			ExitError(err)
		}

		kv := transform(node)
		if kv == nil {
			continue
		}

		data, err := proto.Marshal(kv)
		if err != nil {
			ExitError(err)
		}
		binary.LittleEndian.PutUint64(buf, uint64(len(data)))
		if _, err := writer.Write(buf); err != nil {
			ExitError(err)
		}
		if _, err := writer.Write(data); err != nil {
			ExitError(err)
		}
	}
}
<|endoftext|>


<commit_before>package main

import (
	"encoding/json"
	"io/ioutil"
	"os"
)

type Configuration struct {
	RepositoryDirectory string
	RepositoryFile      string
	RepositoryLogMode   bool

	HistoryCountDefault int
	LastCountDefault    int
}

type Settings struct {
	Configs Configuration
}

func (sts *Settings) LoadSettings() {
	parrot.Info("Load settings if provided")

	file, err := ioutil.ReadFile("/home/gianluca/Projects/golang/bin/conf.json")
	if err != nil {
		parrot.Error("Warning", err)
		os.Exit(1)
	}
	//m := new(Dispatch)
	//var m interface{}
	json.Unmarshal(file, &sts.Configs)
}

func RepositoryDirectory(sts *Settings) string {
	if sts.Configs == (Configuration{}) {
		return ConstRepositoryDirectory
	}

	if sts.Configs.RepositoryDirectory == "" {
		return ConstRepositoryDirectory
	}

	return sts.Configs.RepositoryDirectory
}

func RepositoryFile(sts *Settings) string {
	if sts.Configs == (Configuration{}) {
		return ConstRepositoryFile
	}

	if sts.Configs.RepositoryFile == "" {
		return ConstRepositoryFile
	}

	return sts.Configs.RepositoryFile
}

func RepositoryLogMode(sts *Settings) bool {
	if sts.Configs == (Configuration{}) {
		return ConstRepositoryLogMode
	}

	return sts.Configs.RepositoryLogMode
}

func HistoryCountDefault(sts *Settings) int {
	if sts.Configs == (Configuration{}) {
		return ConstHistoryCountDefault
	}

	return sts.Configs.HistoryCountDefault
}

func LastCountDefault(sts *Settings) int {
	if sts.Configs == (Configuration{}) {
		return ConstLastCountDefault
	}

	return sts.Configs.LastCountDefault
}
<commit_msg>removed comments<commit_after>package main

import (
	"encoding/json"
	"io/ioutil"
	"os"
)

type Configuration struct {
	RepositoryDirectory string
	RepositoryFile      string
	RepositoryLogMode   bool

	HistoryCountDefault int
	LastCountDefault    int
}

type Settings struct {
	Configs Configuration
}

func (sts *Settings) LoadSettings() {
	file, err := ioutil.ReadFile("/home/gianluca/Projects/golang/bin/conf.json")
	if err != nil {
		parrot.Error("Warning", err)
		os.Exit(1)
	}
	//m := new(Dispatch)
	//var m interface{}
	json.Unmarshal(file, &sts.Configs)
}

func RepositoryDirectory(sts *Settings) string {
	if sts.Configs == (Configuration{}) {
		return ConstRepositoryDirectory
	}

	if sts.Configs.RepositoryDirectory == "" {
		return ConstRepositoryDirectory
	}

	return sts.Configs.RepositoryDirectory
}

func RepositoryFile(sts *Settings) string {
	if sts.Configs == (Configuration{}) {
		return ConstRepositoryFile
	}

	if sts.Configs.RepositoryFile == "" {
		return ConstRepositoryFile
	}

	return sts.Configs.RepositoryFile
}

func RepositoryLogMode(sts *Settings) bool {
	if sts.Configs == (Configuration{}) {
		return ConstRepositoryLogMode
	}

	return sts.Configs.RepositoryLogMode
}

func HistoryCountDefault(sts *Settings) int {
	if sts.Configs == (Configuration{}) {
		return ConstHistoryCountDefault
	}

	return sts.Configs.HistoryCountDefault
}

func LastCountDefault(sts *Settings) int {
	if sts.Configs == (Configuration{}) {
		return ConstLastCountDefault
	}

	return sts.Configs.LastCountDefault
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"log"
	"net"
	"os"
	"os/exec"
	"os/signal"
	"syscall"
	"unsafe"

	"github.com/songgao/water"
)

/*
#cgo CFLAGS: -I ./libzt/include
#cgo darwin LDFLAGS: -L ./libzt/darwin/ -lzt -lstdc++

#include "libzt.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <netdb.h>
*/
import "C"

const NETWORK_ID = "8056c2e21c000001"
const PORT = 50718 // 7878
const BUF_SIZE = 2000

func setupCleanUpOnInterrupt() chan bool {
	signalChan := make(chan os.Signal, 1)
	signal.Notify(signalChan, os.Interrupt)

	cleanupDone := make(chan bool)

	go func() {
		for range signalChan {
			fmt.Println("\nReceived an interrupt, shutting dow.\n")

			cleanupDone <- true
		}
	}()
	return cleanupDone
}

func getOtherIP() string {
	if len(os.Args) >= 2 {
		return os.Args[1]
	} else {
		return ""
	}
}

func validate(value C.int, message string) {
	if value < 0 {
		fmt.Println(message)
		os.Exit(1)
	}
}

func bindAndListen(sockfd C.int) int {
	serverSocket := syscall.RawSockaddrInet6{Flowinfo: 0, Family: syscall.AF_INET6, Port: PORT}
	retVal := C.zts_bind(sockfd, (*C.struct_sockaddr)(unsafe.Pointer(&serverSocket)), C.sizeof_struct_sockaddr_in6)
	validate(retVal, "ERROR on binding")
	fmt.Println("Bind Complete")

	C.zts_listen(sockfd, 1)
	fmt.Println("Listening")

	clientSocket := syscall.RawSockaddrInet6{}
	clientSocketLength := C.sizeof_struct_sockaddr_in6
	newSockfd := C.zts_accept(sockfd, (*C.struct_sockaddr)(unsafe.Pointer(&clientSocket)), (*C.socklen_t)(unsafe.Pointer(&clientSocketLength)))
	validate(newSockfd, "ERROR on accept")
	fmt.Println("Accepted")

	clientIpAddress := make([]byte, C.ZT_MAX_IPADDR_LEN)
	C.inet_ntop(syscall.AF_INET6, unsafe.Pointer(&clientSocket.Addr), (*C.char)(unsafe.Pointer(&clientIpAddress[0])), C.ZT_MAX_IPADDR_LEN)
	fmt.Printf("Incoming connection from client having IPv6 address: %s\n", string(clientIpAddress[:C.ZT_MAX_IPADDR_LEN]))

	return int(newSockfd)
}

func parseIPV6(ipString string) [16]byte {
	ip := net.ParseIP(ipString)
	var arr [16]byte
	copy(arr[:], ip)
	return arr
}

func ifconfig(args ...string) {
	cmd := exec.Command("ifconfig", args...)
	cmd.Stderr = os.Stderr
	cmd.Stdout = os.Stdout
	cmd.Stdin = os.Stdin
	err := cmd.Run()
	if nil != err {
		log.Fatalln("Error running command:", err)
	}
}

func setupTun(initater bool) *water.Interface {
	iface, _ := water.New(water.Config{
		DeviceType: water.TUN,
	})

	log.Printf("Interface Name: %s\n", iface.Name())

	if initater {
		ifconfig(iface.Name(), "10.1.0.10", "10.1.0.20", "up")
	} else {
		ifconfig(iface.Name(), "10.1.0.20", "10.1.0.10", "up")
	}

	return iface
}

func initZT() {
	C.zts_simple_start(C.CString("./zt"), C.CString(NETWORK_ID))

	ipv4Address := make([]byte, C.ZT_MAX_IPADDR_LEN)
	ipv6Address := make([]byte, C.ZT_MAX_IPADDR_LEN)

	C.zts_get_ipv4_address(C.CString(NETWORK_ID), (*C.char)(unsafe.Pointer(&ipv4Address[0])), C.ZT_MAX_IPADDR_LEN)
	log.Printf("ipv4 = %s \n", string(ipv4Address[:C.ZT_MAX_IPADDR_LEN]))

	C.zts_get_ipv6_address(C.CString(NETWORK_ID), (*C.char)(unsafe.Pointer(&ipv6Address[0])), C.ZT_MAX_IPADDR_LEN)
	log.Printf("ipv6 = %s \n", string(ipv6Address[:C.ZT_MAX_IPADDR_LEN]))
}

func connectToOther() int {
	arr := parseIPV6(getOtherIP())

	clientSocket := syscall.RawSockaddrInet6{Flowinfo: 0, Family: syscall.AF_INET6, Port: PORT, Addr: arr}

	sockfd := C.zts_socket(syscall.AF_INET6, syscall.SOCK_STREAM, 0)
	validate(sockfd, "Error in opening socket")

	retVal := C.zts_connect(sockfd, (*C.struct_sockaddr)(unsafe.Pointer(&clientSocket)), C.sizeof_struct_sockaddr_in6)
	validate(retVal, "Error in connect client")

	return (int)(sockfd)
}

func validateErr(err error, message string) {
	if err != nil {
		log.Println(message)
	}
}

func bridge(iface *water.Interface, sockfd int) {
	buffer1 := make([]byte, BUF_SIZE)
	go func() {
		for {
			plen, err := iface.Read(buffer1)
			validateErr(err, "Error reading from tun")

			_, writeErr := syscall.Write(sockfd, buffer1[:plen])
			validateErr(writeErr, "Error writing to zt")
		}
	}()

	buffer2 := make([]byte, BUF_SIZE)

	go func() {
		for {
			plen, err := syscall.Read(sockfd, buffer2)
			validateErr(err, "Error reading from zt")

			_, writeErr := iface.Write(buffer2[:plen])
			validateErr(writeErr, "Error writing to tun")

		}
	}()
}

func main() {
	initZT()

	sockfd := C.zts_socket(syscall.AF_INET6, syscall.SOCK_STREAM, 0)
	validate(sockfd, "Error in opening socket")

	if len(getOtherIP()) == 0 {
		iface := setupTun(true)

		newSockfd := bindAndListen(sockfd)

		bridge(iface, newSockfd)
	} else {
		iface := setupTun(false)
		sockfd := connectToOther()

		bridge(iface, sockfd)
	}

	<-setupCleanUpOnInterrupt()
}
<commit_msg>Stop stuff on exit<commit_after>package main

import (
	"fmt"
	"log"
	"net"
	"os"
	"os/exec"
	"os/signal"
	"syscall"
	"unsafe"

	"github.com/songgao/water"
)

/*
#cgo CFLAGS: -I ./libzt/include
#cgo darwin LDFLAGS: -L ./libzt/darwin/ -lzt -lstdc++

#include "libzt.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <netdb.h>
*/
import "C"

const NETWORK_ID = "8056c2e21c000001"
const PORT = 50718 // 7878
const BUF_SIZE = 2000

func setupCleanUpOnInterrupt() chan bool {
	signalChan := make(chan os.Signal, 1)
	signal.Notify(signalChan, os.Interrupt)

	cleanupDone := make(chan bool)

	go func() {
		for range signalChan {
			fmt.Println("\nReceived an interrupt, shutting dow.\n")

			cleanupDone <- true
		}
	}()
	return cleanupDone
}

func getOtherIP() string {
	if len(os.Args) >= 2 {
		return os.Args[1]
	} else {
		return ""
	}
}

func validate(value C.int, message string) {
	if value < 0 {
		fmt.Println(message)
		os.Exit(1)
	}
}

func bindAndListen(sockfd C.int) int {
	serverSocket := syscall.RawSockaddrInet6{Flowinfo: 0, Family: syscall.AF_INET6, Port: PORT}
	retVal := C.zts_bind(sockfd, (*C.struct_sockaddr)(unsafe.Pointer(&serverSocket)), C.sizeof_struct_sockaddr_in6)
	validate(retVal, "ERROR on binding")
	fmt.Println("Bind Complete")

	C.zts_listen(sockfd, 1)
	fmt.Println("Listening")

	clientSocket := syscall.RawSockaddrInet6{}
	clientSocketLength := C.sizeof_struct_sockaddr_in6
	newSockfd := C.zts_accept(sockfd, (*C.struct_sockaddr)(unsafe.Pointer(&clientSocket)), (*C.socklen_t)(unsafe.Pointer(&clientSocketLength)))
	validate(newSockfd, "ERROR on accept")
	fmt.Println("Accepted")

	clientIpAddress := make([]byte, C.ZT_MAX_IPADDR_LEN)
	C.inet_ntop(syscall.AF_INET6, unsafe.Pointer(&clientSocket.Addr), (*C.char)(unsafe.Pointer(&clientIpAddress[0])), C.ZT_MAX_IPADDR_LEN)
	fmt.Printf("Incoming connection from client having IPv6 address: %s\n", string(clientIpAddress[:C.ZT_MAX_IPADDR_LEN]))

	return int(newSockfd)
}

func parseIPV6(ipString string) [16]byte {
	ip := net.ParseIP(ipString)
	var arr [16]byte
	copy(arr[:], ip)
	return arr
}

func ifconfig(args ...string) {
	cmd := exec.Command("ifconfig", args...)
	cmd.Stderr = os.Stderr
	cmd.Stdout = os.Stdout
	cmd.Stdin = os.Stdin
	err := cmd.Run()
	if nil != err {
		log.Fatalln("Error running command:", err)
	}
}

func setupTun(initater bool) *water.Interface {
	iface, _ := water.New(water.Config{
		DeviceType: water.TUN,
	})

	log.Printf("Interface Name: %s\n", iface.Name())

	if initater {
		ifconfig(iface.Name(), "10.1.0.10", "10.1.0.20", "up")
	} else {
		ifconfig(iface.Name(), "10.1.0.20", "10.1.0.10", "up")
	}

	return iface
}

func initZT() {
	C.zts_simple_start(C.CString("./zt"), C.CString(NETWORK_ID))

	ipv4Address := make([]byte, C.ZT_MAX_IPADDR_LEN)
	ipv6Address := make([]byte, C.ZT_MAX_IPADDR_LEN)

	C.zts_get_ipv4_address(C.CString(NETWORK_ID), (*C.char)(unsafe.Pointer(&ipv4Address[0])), C.ZT_MAX_IPADDR_LEN)
	log.Printf("ipv4 = %s \n", string(ipv4Address[:C.ZT_MAX_IPADDR_LEN]))

	C.zts_get_ipv6_address(C.CString(NETWORK_ID), (*C.char)(unsafe.Pointer(&ipv6Address[0])), C.ZT_MAX_IPADDR_LEN)
	log.Printf("ipv6 = %s \n", string(ipv6Address[:C.ZT_MAX_IPADDR_LEN]))
}

func connectToOther() int {
	arr := parseIPV6(getOtherIP())

	clientSocket := syscall.RawSockaddrInet6{Flowinfo: 0, Family: syscall.AF_INET6, Port: PORT, Addr: arr}

	sockfd := C.zts_socket(syscall.AF_INET6, syscall.SOCK_STREAM, 0)
	validate(sockfd, "Error in opening socket")

	retVal := C.zts_connect(sockfd, (*C.struct_sockaddr)(unsafe.Pointer(&clientSocket)), C.sizeof_struct_sockaddr_in6)
	validate(retVal, "Error in connect client")

	return (int)(sockfd)
}

func validateErr(err error, message string) {
	if err != nil {
		log.Println(message)
	}
}

func bridge(iface *water.Interface, sockfd int) {
	buffer1 := make([]byte, BUF_SIZE)
	go func() {
		for {
			plen, err := iface.Read(buffer1)
			validateErr(err, "Error reading from tun")

			_, writeErr := syscall.Write(sockfd, buffer1[:plen])
			validateErr(writeErr, "Error writing to zt")
		}
	}()

	buffer2 := make([]byte, BUF_SIZE)

	go func() {
		for {
			plen, err := syscall.Read(sockfd, buffer2)
			validateErr(err, "Error reading from zt")

			_, writeErr := iface.Write(buffer2[:plen])
			validateErr(writeErr, "Error writing to tun")

		}
	}()
}

func main() {
	initZT()
	defer C.zts_stop()

	sockfd := C.zts_socket(syscall.AF_INET6, syscall.SOCK_STREAM, 0)
	validate(sockfd, "Error in opening socket")
	defer C.zts_close(sockfd)

	if len(getOtherIP()) == 0 {
		iface := setupTun(true)

		newSockfd := bindAndListen(sockfd)

		bridge(iface, newSockfd)
	} else {
		iface := setupTun(false)
		sockfd := connectToOther()

		bridge(iface, sockfd)
	}

	<-setupCleanUpOnInterrupt()
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"fmt"
	"os"

	"github.com/pkg/errors"
	"github.com/scgolang/sc"
)

// controller controls the behavior of the app
type controller struct {
	command  string
	output   *string
	flagSets map[string]*flag.FlagSet
}

func newController() *controller {
	c := &controller{}
	c.flagSets = make(map[string]*flag.FlagSet)
	c.flagSets["format"] = flag.NewFlagSet("format", flag.ExitOnError)
	c.flagSets["diff"] = flag.NewFlagSet("diff", flag.ExitOnError)
	c.output = c.flagSets["format"].String("output", "json", "output format")
	return c
}

// die prints an error message and kills the process
func (c *controller) die(err error) {
	fmt.Fprintf(os.Stderr, "%s\n", err.Error())
	os.Exit(1)
}

// diff runs the diff command
func (c *controller) diff() error {
	fset := c.flagSets["diff"]

	if expected, got := 2, len(fset.Args()); expected != got {
		return errors.Errorf("expected %d args, got %d", expected, got)
	}
	f1, err := os.Open(fset.Arg(0))
	if err != nil {
		return err
	}
	f2, err := os.Open(fset.Arg(1))
	if err != nil {
		return err
	}
	s1, err := sc.ReadSynthdef(f1)
	if err != nil {
		return err
	}
	s2, err := sc.ReadSynthdef(f2)
	if err != nil {
		return err
	}
	diffs, err := differ{}.do(s1, s2)
	if err != nil {
		return err
	}
	fmt.Printf("%50s%50s\n", fset.Arg(0), fset.Arg(1))
	for _, diff := range diffs {
		fmt.Printf("%50s%50s\n", diff[0], diff[1])
	}
	return nil
}

// format runs the format command
func (c *controller) format() error {
	r, err := os.Open(c.flagSets["format"].Arg(0))
	if err != nil {
		return err
	}
	d, err := sc.ReadSynthdef(r)
	if err != nil {
		return err
	}
	switch *c.output {
	case "json":
		err = d.WriteJSON(os.Stdout)
	case "dot":
		err = d.WriteGraph(os.Stdout)
	default:
		err = d.WriteXML(os.Stdout)
	}
	return err
}

// run runs a command
func (c *controller) run() error {
	switch c.command {
	case "format":
		return c.format()
	case "diff":
		return c.diff()
	}
	return nil
}

// usage prints a usage message on stderr
func (c *controller) usage() {
	w, prog := os.Stderr, os.Args[0]
	fmt.Fprintf(w, "Usage:\n")
	fmt.Fprintf(w, "%s COMMAND [OPTIONS]\n\n", prog)
	fmt.Fprintf(w, "Commands:\n")
	for cmd, _ := range c.flagSets {
		fmt.Fprintf(w, "%s\n", cmd)
	}
}

func main() {
	controller := newController()
	if len(os.Args) < 2 {
		controller.usage()
		os.Exit(1)
	}
	// determine if it is a valid command
	command := controller.flagSets[os.Args[1]]
	if command == nil {
		controller.usage()
		os.Exit(1)
	}
	// parse cli flags for the command
	err := command.Parse(os.Args[2:])
	if err != nil {
		if err == flag.ErrHelp {
			controller.usage()
			os.Exit(0)
		} else {
			controller.die(err)
		}
	}
	// run the command
	err = controller.run()
	if err != nil {
		controller.die(err)
	}
}
<commit_msg>fix command run<commit_after>package main

import (
	"flag"
	"fmt"
	"os"

	"github.com/pkg/errors"
	"github.com/scgolang/sc"
)

// controller controls the behavior of the app
type controller struct {
	command  string
	output   *string
	flagSets map[string]*flag.FlagSet
}

func newController() *controller {
	c := &controller{}
	c.flagSets = make(map[string]*flag.FlagSet)
	c.flagSets["format"] = flag.NewFlagSet("format", flag.ExitOnError)
	c.flagSets["diff"] = flag.NewFlagSet("diff", flag.ExitOnError)
	c.output = c.flagSets["format"].String("output", "json", "output format")
	return c
}

// die prints an error message and kills the process
func (c *controller) die(err error) {
	fmt.Fprintf(os.Stderr, "%s\n", err.Error())
	os.Exit(1)
}

// diff runs the diff command
func (c *controller) diff() error {
	fset := c.flagSets["diff"]

	if expected, got := 2, len(fset.Args()); expected != got {
		return errors.Errorf("expected %d args, got %d", expected, got)
	}
	f1, err := os.Open(fset.Arg(0))
	if err != nil {
		return err
	}
	f2, err := os.Open(fset.Arg(1))
	if err != nil {
		return err
	}
	s1, err := sc.ReadSynthdef(f1)
	if err != nil {
		return err
	}
	s2, err := sc.ReadSynthdef(f2)
	if err != nil {
		return err
	}
	diffs, err := differ{}.do(s1, s2)
	if err != nil {
		return err
	}
	fmt.Printf("%-50s%-50s\n", fset.Arg(0), fset.Arg(1))
	for _, diff := range diffs {
		fmt.Printf("%-50s%-50s\n", diff[0], diff[1])
	}
	return nil
}

// format runs the format command
func (c *controller) format() error {
	r, err := os.Open(c.flagSets["format"].Arg(0))
	if err != nil {
		return err
	}
	d, err := sc.ReadSynthdef(r)
	if err != nil {
		return err
	}
	switch *c.output {
	case "json":
		err = d.WriteJSON(os.Stdout)
	case "dot":
		err = d.WriteGraph(os.Stdout)
	default:
		err = d.WriteXML(os.Stdout)
	}
	return err
}

// run runs a command
func (c *controller) run() error {
	switch c.command {
	case "format":
		return c.format()
	case "diff":
		return c.diff()
	}
	return nil
}

// usage prints a usage message on stderr
func (c *controller) usage() {
	w, prog := os.Stderr, os.Args[0]
	fmt.Fprintf(w, "Usage:\n")
	fmt.Fprintf(w, "%s COMMAND [OPTIONS]\n\n", prog)
	fmt.Fprintf(w, "Commands:\n")
	for cmd, _ := range c.flagSets {
		fmt.Fprintf(w, "%s\n", cmd)
	}
}

func main() {
	controller := newController()

	if len(os.Args) < 2 {
		controller.usage()
		os.Exit(1)
	}
	controller.command = os.Args[1]

	command := controller.flagSets[controller.command]

	if command == nil {
		controller.usage()
		os.Exit(1)
	}
	// parse cli flags for the command
	if err := command.Parse(os.Args[2:]); err != nil {
		if err == flag.ErrHelp {
			controller.usage()
			os.Exit(0)
		} else {
			controller.die(err)
		}
	}
	// run the command
	if err := controller.run(); err != nil {
		controller.die(err)
	}
}
<|endoftext|>


<commit_before>package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"path/filepath"
	"strconv"

	"github.com/docker/libcompose/project"
	"k8s.io/kubernetes/pkg/api"
)

var (
	composeFile string
	outputDir   string
)

func init() {
	flag.StringVar(&composeFile, "compose-file", "docker-compose.yml", "Specify an alternate compose `file`")
	flag.StringVar(&outputDir, "output-dir", "output", "Kubernetes configs output `directory`")
}

func main() {
	flag.Parse()

	p := project.NewProject(&project.Context{
		ProjectName: "kube",
		ComposeFile: composeFile,
	})

	err := p.Parse()
	if err != nil {
		log.Fatal(err)
	}
	os.MkdirAll(outputDir, 0755)

	for name, service := range p.Configs {
		rc := &api.ReplicationController{
			TypeMeta: api.TypeMeta{
				Kind:       "ReplicationController",
				APIVersion: "v1",
			},
			ObjectMeta: api.ObjectMeta{
				Name:   name,
				Labels: map[string]string{"service": name},
			},
			Spec: api.ReplicationControllerSpec{
				Replicas: 1,
				Selector: map[string]string{"service": name},
				Template: &api.PodTemplateSpec{
					ObjectMeta: api.ObjectMeta{
						Labels: map[string]string{"service": name},
					},
					Spec: api.PodSpec{
						Containers: []api.Container{
							{
								Name:  name,
								Image: service.Image,
							},
						},
					},
				},
			},
		}

		// Configure the container ports.
		ports := make([]api.ContainerPort, 0)
		for _, port := range service.Ports {
			if portNumber, err := strconv.Atoi(port); err == nil {
				ports = append(ports, api.ContainerPort{ContainerPort: portNumber})
				continue
			}
			log.Fatal("invalid container port %s for service %s", port, name)
		}

		rc.Spec.Template.Spec.Containers[0].Ports = ports

		// Configure the container restart policy.
		switch service.Restart {
		case "", "always":
			rc.Spec.Template.Spec.RestartPolicy = api.RestartPolicyAlways
		case "no":
			rc.Spec.Template.Spec.RestartPolicy = api.RestartPolicyNever
		case "on-failure":
			rc.Spec.Template.Spec.RestartPolicy = api.RestartPolicyOnFailure
		default:
			log.Fatalf("unknown restart policy %s for service %s", service.Restart, name)
		}

		data, err := json.MarshalIndent(rc, "", "  ")
		if err != nil {
			log.Fatal(err)
		}

		// Save the replication controller for the Docker compose service to the
		// configs directory.
		outputFileName := fmt.Sprintf("%s-rc.yaml", name)
		outputFilePath := filepath.Join(outputDir, outputFileName)
		if err := ioutil.WriteFile(outputFilePath, data, 0644); err != nil {
			log.Fatal(err)
		}
		fmt.Println(outputFilePath)
	}
}
<commit_msg>add license header<commit_after>/*
Copyright 2015 Kelsey Hightower 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 (
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"path/filepath"
	"strconv"

	"github.com/docker/libcompose/project"
	"k8s.io/kubernetes/pkg/api"
)

var (
	composeFile string
	outputDir   string
)

func init() {
	flag.StringVar(&composeFile, "compose-file", "docker-compose.yml", "Specify an alternate compose `file`")
	flag.StringVar(&outputDir, "output-dir", "output", "Kubernetes configs output `directory`")
}

func main() {
	flag.Parse()

	p := project.NewProject(&project.Context{
		ProjectName: "kube",
		ComposeFile: composeFile,
	})

	err := p.Parse()
	if err != nil {
		log.Fatal(err)
	}
	os.MkdirAll(outputDir, 0755)

	for name, service := range p.Configs {
		rc := &api.ReplicationController{
			TypeMeta: api.TypeMeta{
				Kind:       "ReplicationController",
				APIVersion: "v1",
			},
			ObjectMeta: api.ObjectMeta{
				Name:   name,
				Labels: map[string]string{"service": name},
			},
			Spec: api.ReplicationControllerSpec{
				Replicas: 1,
				Selector: map[string]string{"service": name},
				Template: &api.PodTemplateSpec{
					ObjectMeta: api.ObjectMeta{
						Labels: map[string]string{"service": name},
					},
					Spec: api.PodSpec{
						Containers: []api.Container{
							{
								Name:  name,
								Image: service.Image,
							},
						},
					},
				},
			},
		}

		// Configure the container ports.
		ports := make([]api.ContainerPort, 0)
		for _, port := range service.Ports {
			if portNumber, err := strconv.Atoi(port); err == nil {
				ports = append(ports, api.ContainerPort{ContainerPort: portNumber})
				continue
			}
			log.Fatal("invalid container port %s for service %s", port, name)
		}

		rc.Spec.Template.Spec.Containers[0].Ports = ports

		// Configure the container restart policy.
		switch service.Restart {
		case "", "always":
			rc.Spec.Template.Spec.RestartPolicy = api.RestartPolicyAlways
		case "no":
			rc.Spec.Template.Spec.RestartPolicy = api.RestartPolicyNever
		case "on-failure":
			rc.Spec.Template.Spec.RestartPolicy = api.RestartPolicyOnFailure
		default:
			log.Fatalf("unknown restart policy %s for service %s", service.Restart, name)
		}

		data, err := json.MarshalIndent(rc, "", "  ")
		if err != nil {
			log.Fatal(err)
		}

		// Save the replication controller for the Docker compose service to the
		// configs directory.
		outputFileName := fmt.Sprintf("%s-rc.yaml", name)
		outputFilePath := filepath.Join(outputDir, outputFileName)
		if err := ioutil.WriteFile(outputFilePath, data, 0644); err != nil {
			log.Fatal(err)
		}
		fmt.Println(outputFilePath)
	}
}
<|endoftext|>


<commit_before>package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"os"
	"path"

	"github.com/streadway/amqp"
)

var (
	uri         = flag.String("uri", "amqp://guest:guest@localhost:5672/", "AMQP URI")
	queue       = flag.String("queue", "", "AMQP queue name")
	maxMessages = flag.Uint("max-messages", 1000, "Maximum number of messages to dump")
	outputDir   = flag.String("output-dir", ".", "Directory in which to save the dumped messages")
	full        = flag.Bool("full", false, "Dump the message, its properties and headers")
	verbose     = flag.Bool("verbose", false, "Print progress")
)

func main() {
	flag.Parse()
	err := DumpMessagesFromQueue(*uri, *queue, *maxMessages, *outputDir)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", err)
		os.Exit(1)
	}
}

func DumpMessagesFromQueue(amqpURI string, queueName string, maxMessages uint, outputDir string) error {
	if queueName == "" {
		return fmt.Errorf("Must supply queue name")
	}

	VerboseLog(fmt.Sprintf("Dialing %q", amqpURI))
	conn, err := amqp.Dial(amqpURI)
	if err != nil {
		return fmt.Errorf("Dial: %s", err)
	}

	defer func() {
		conn.Close()
		VerboseLog("AMQP connection closed")
	}()

	channel, err := conn.Channel()
	if err != nil {
		return fmt.Errorf("Channel: %s", err)
	}

	VerboseLog(fmt.Sprintf("Pulling messages from queue %q", queueName))
	for messagesReceived := uint(0); messagesReceived < maxMessages; messagesReceived++ {
		msg, ok, err := channel.Get(queueName,
			false, // autoAck
		)
		if err != nil {
			return fmt.Errorf("Queue get: %s", err)
		}

		if !ok {
			VerboseLog("No more messages in queue")
			break
		}

		err = SaveMessageToFile(msg.Body, outputDir, messagesReceived)
		if err != nil {
			return fmt.Errorf("Save message: %s", err)
		}

		if *full {
			err = SavePropsAndHeadersToFile(msg, outputDir, messagesReceived)
			if err != nil {
				return fmt.Errorf("Save props and headers: %s", err)
			}
		}
	}

	return nil
}

func SaveMessageToFile(body []byte, outputDir string, counter uint) error {
	filePath := GenerateFilePath(outputDir, counter)
	err := ioutil.WriteFile(filePath, body, 0644)
	if err != nil {
		return err
	}

	fmt.Println(filePath)

	return nil
}

func GetProperties(msg amqp.Delivery) map[string]interface{} {
	props := map[string]interface{}{
		"app_id":           msg.AppId,
		"content_encoding": msg.ContentEncoding,
		"content_type":     msg.ContentType,
		"correlation_id":   msg.CorrelationId,
		"delivery_mode":    msg.DeliveryMode,
		"expiration":       msg.Expiration,
		"message_id":       msg.MessageId,
		"priority":         msg.Priority,
		"reply_to":         msg.ReplyTo,
		"type":             msg.Type,
		"user_id":          msg.UserId,
	}

	if !msg.Timestamp.IsZero() {
		props["timestamp"] = msg.Timestamp.String()
	}

	for k, v := range props {
		if v == "" {
			delete(props, k)
		}
	}

	return props
}

func SavePropsAndHeadersToFile(msg amqp.Delivery, outputDir string, counter uint) error {
	extras := make(map[string]interface{})
	extras["properties"] = GetProperties(msg)
	extras["headers"] = msg.Headers

	data, err := json.MarshalIndent(extras, "", "  ")
	if err != nil {
		return err
	}

	filePath := GenerateFilePath(outputDir, counter) + "-headers+properties.json"
	err = ioutil.WriteFile(filePath, data, 0644)
	if err != nil {
		return err
	}

	fmt.Println(filePath)

	return nil
}

func GenerateFilePath(outputDir string, counter uint) string {
	return path.Join(outputDir, fmt.Sprintf("msg-%04d", counter))
}

func VerboseLog(msg string) {
	if *verbose {
		fmt.Println("*", msg)
	}
}
<commit_msg>Add -insecure-tls switch to skip verifying TLS certificates<commit_after>package main

import (
	"crypto/tls"
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"os"
	"path"
	"strings"

	"github.com/streadway/amqp"
)

var (
	uri          = flag.String("uri", "amqp://guest:guest@localhost:5672/", "AMQP URI")
	insecure_tls = flag.Bool("insecure-tls", false, "Insecure TLS mode: don't check certificates")
	queue        = flag.String("queue", "", "AMQP queue name")
	maxMessages  = flag.Uint("max-messages", 1000, "Maximum number of messages to dump")
	outputDir    = flag.String("output-dir", ".", "Directory in which to save the dumped messages")
	full         = flag.Bool("full", false, "Dump the message, its properties and headers")
	verbose      = flag.Bool("verbose", false, "Print progress")
)

func main() {
	flag.Parse()
	err := DumpMessagesFromQueue(*uri, *queue, *maxMessages, *outputDir)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", err)
		os.Exit(1)
	}
}

func dial(amqpURI string) (*amqp.Connection, error) {
	VerboseLog(fmt.Sprintf("Dialing %q", amqpURI))
	if *insecure_tls && strings.HasPrefix(amqpURI, "amqps://") {
		tlsConfig := new(tls.Config)
		tlsConfig.InsecureSkipVerify = true
		conn, err := amqp.DialTLS(amqpURI, tlsConfig)
		return conn, err
	}
	conn, err := amqp.Dial(amqpURI)
	return conn, err
}

func DumpMessagesFromQueue(amqpURI string, queueName string, maxMessages uint, outputDir string) error {
	if queueName == "" {
		return fmt.Errorf("Must supply queue name")
	}

	conn, err := dial(amqpURI)
	if err != nil {
		return fmt.Errorf("Dial: %s", err)
	}

	defer func() {
		conn.Close()
		VerboseLog("AMQP connection closed")
	}()

	channel, err := conn.Channel()
	if err != nil {
		return fmt.Errorf("Channel: %s", err)
	}

	VerboseLog(fmt.Sprintf("Pulling messages from queue %q", queueName))
	for messagesReceived := uint(0); messagesReceived < maxMessages; messagesReceived++ {
		msg, ok, err := channel.Get(queueName,
			false, // autoAck
		)
		if err != nil {
			return fmt.Errorf("Queue get: %s", err)
		}

		if !ok {
			VerboseLog("No more messages in queue")
			break
		}

		err = SaveMessageToFile(msg.Body, outputDir, messagesReceived)
		if err != nil {
			return fmt.Errorf("Save message: %s", err)
		}

		if *full {
			err = SavePropsAndHeadersToFile(msg, outputDir, messagesReceived)
			if err != nil {
				return fmt.Errorf("Save props and headers: %s", err)
			}
		}
	}

	return nil
}

func SaveMessageToFile(body []byte, outputDir string, counter uint) error {
	filePath := GenerateFilePath(outputDir, counter)
	err := ioutil.WriteFile(filePath, body, 0644)
	if err != nil {
		return err
	}

	fmt.Println(filePath)

	return nil
}

func GetProperties(msg amqp.Delivery) map[string]interface{} {
	props := map[string]interface{}{
		"app_id":           msg.AppId,
		"content_encoding": msg.ContentEncoding,
		"content_type":     msg.ContentType,
		"correlation_id":   msg.CorrelationId,
		"delivery_mode":    msg.DeliveryMode,
		"expiration":       msg.Expiration,
		"message_id":       msg.MessageId,
		"priority":         msg.Priority,
		"reply_to":         msg.ReplyTo,
		"type":             msg.Type,
		"user_id":          msg.UserId,
	}

	if !msg.Timestamp.IsZero() {
		props["timestamp"] = msg.Timestamp.String()
	}

	for k, v := range props {
		if v == "" {
			delete(props, k)
		}
	}

	return props
}

func SavePropsAndHeadersToFile(msg amqp.Delivery, outputDir string, counter uint) error {
	extras := make(map[string]interface{})
	extras["properties"] = GetProperties(msg)
	extras["headers"] = msg.Headers

	data, err := json.MarshalIndent(extras, "", "  ")
	if err != nil {
		return err
	}

	filePath := GenerateFilePath(outputDir, counter) + "-headers+properties.json"
	err = ioutil.WriteFile(filePath, data, 0644)
	if err != nil {
		return err
	}

	fmt.Println(filePath)

	return nil
}

func GenerateFilePath(outputDir string, counter uint) string {
	return path.Join(outputDir, fmt.Sprintf("msg-%04d", counter))
}

func VerboseLog(msg string) {
	if *verbose {
		fmt.Println("*", msg)
	}
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"strings"
	"time"

	"github.com/buildkite/buildkite-agent-metrics/backend"
	"github.com/buildkite/buildkite-agent-metrics/collector"
	"github.com/buildkite/buildkite-agent-metrics/version"
)

var bk backend.Backend

func main() {
	var (
		token       = flag.String("token", "", "A Buildkite Agent Registration Token")
		interval    = flag.Duration("interval", 0, "Update metrics every interval, rather than once")
		showVersion = flag.Bool("version", false, "Show the version")
		quiet       = flag.Bool("quiet", false, "Only print errors")
		debug       = flag.Bool("debug", false, "Show debug output")
		debugHttp   = flag.Bool("debug-http", false, "Show full http traces")
		dryRun      = flag.Bool("dry-run", false, "Whether to only print metrics")
		endpoint    = flag.String("endpoint", "https://agent.buildkite.com/v3", "A custom Buildkite Agent API endpoint")

		// backend config
		backendOpt     = flag.String("backend", "cloudwatch", "Specify the backend to use: cloudwatch, statsd, prometheus, stackdriver")
		statsdHost     = flag.String("statsd-host", "127.0.0.1:8125", "Specify the StatsD server")
		statsdTags     = flag.Bool("statsd-tags", false, "Whether your StatsD server supports tagging like Datadog")
		prometheusAddr = flag.String("prometheus-addr", ":8080", "Prometheus metrics transport bind address")
		prometheusPath = flag.String("prometheus-path", "/metrics", "Prometheus metrics transport path")
		clwRegion      = flag.String("cloudwatch-region", "", "AWS Region to connect to, defaults to $AWS_REGION or us-east-1")
		clwDimensions  = flag.String("cloudwatch-dimensions", "", "Cloudwatch dimensions to index metrics under, in the form of Key=Value, Other=Value")
		gcpProjectID   = flag.String("stackdriver-projectid", "", "Specify Stackdriver Project ID")
		nrAppName      = flag.String("newrelic-app-name", "", "New Relic application name for metric events")
		nrLicenseKey   = flag.String("newrelic-license-key", "", "New Relic license key for publishing events")
	)

	// custom config for multiple queues
	var queues stringSliceFlag
	flag.Var(&queues, "queue", "Specific queues to process")

	flag.Parse()

	if *showVersion {
		fmt.Printf("buildkite-agent-metrics %s\n", version.Version)
		os.Exit(0)
	}

	if *token == "" {
		fmt.Println("Must provide a value for -token")
		os.Exit(1)
	}

	var err error
	switch strings.ToLower(*backendOpt) {
	case "cloudwatch":
		region := *clwRegion
		if envRegion := os.Getenv(`AWS_REGION`); region == "" && envRegion != "" {
			region = envRegion
		} else {
			region = `us-east-1`
		}
		dimensions, err := backend.ParseCloudWatchDimensions(*clwDimensions)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
		bk = backend.NewCloudWatchBackend(region, dimensions)
	case "statsd":
		bk, err = backend.NewStatsDBackend(*statsdHost, *statsdTags)
		if err != nil {
			fmt.Printf("Error starting StatsD, err: %v\n", err)
			os.Exit(1)
		}
	case "prometheus":
		bk = backend.NewPrometheusBackend(*prometheusPath, *prometheusAddr)
	case "stackdriver":
		bk, err = backend.NewStackDriverBackend(*gcpProjectID)
		if err != nil {
			fmt.Printf("Error starting Stackdriver backend, err: %v\n", err)
			os.Exit(1)
		}
	case "newrelic":
		bk, err = backend.NewNewRelicBackend(*nrAppName, *nrLicenseKey)
		if err != nil {
			fmt.Printf("Error starting New Relic client: %v\n", err)
			os.Exit(1)
		}
	default:
		fmt.Println("Must provide a supported backend: cloudwatch, statsd, prometheus, stackdriver, newrelic")
		os.Exit(1)
	}

	if *quiet {
		log.SetOutput(ioutil.Discard)
	}

	userAgent := fmt.Sprintf("buildkite-agent-metrics/%s buildkite-agent-metrics-cli", version.Version)
	if *interval > 0 {
		userAgent += fmt.Sprintf(" interval=%s", *interval)
	}

	c := collector.Collector{
		UserAgent: userAgent,
		Endpoint:  *endpoint,
		Token:     *token,
		Queues:    []string(queues),
		Quiet:     *quiet,
		Debug:     *debug,
		DebugHttp: *debugHttp,
	}

	f := func() (time.Duration, error) {
		t := time.Now()

		result, err := c.Collect()
		if err != nil {
			return time.Duration(0), err
		}

		if !*dryRun {
			err = bk.Collect(result)
			if err != nil {
				return time.Duration(0), err
			}
		}

		log.Printf("Finished in %s", time.Now().Sub(t))
		return result.PollDuration, nil
	}

	minPollDuration, err := f()
	if err != nil {
		fmt.Println(err)
	}

	if *interval > 0 {
		for {
			waitTime := *interval

			// Respect the min poll duration returned by the API
			if *interval < minPollDuration {
				log.Printf("Increasing poll duration based on rate-limit headers")
				waitTime = minPollDuration
			}

			log.Printf("Waiting for %v (minimum of %v)", waitTime, minPollDuration)
			time.Sleep(waitTime)

			minPollDuration, err = f()
			if err != nil {
				fmt.Println(err)
			}
		}
	}
}

type stringSliceFlag []string

func (i *stringSliceFlag) String() string {
	return fmt.Sprintf("%v", *i)
}

func (i *stringSliceFlag) Set(value string) error {
	*i = append(*i, value)
	return nil
}
<commit_msg>Support getting agent token from BUILDKITE_AGENT_TOKEN env var<commit_after>package main

import (
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"strings"
	"time"

	"github.com/buildkite/buildkite-agent-metrics/backend"
	"github.com/buildkite/buildkite-agent-metrics/collector"
	"github.com/buildkite/buildkite-agent-metrics/version"
)

var bk backend.Backend

func main() {
	var (
		token       = flag.String("token", "", "A Buildkite Agent Registration Token")
		interval    = flag.Duration("interval", 0, "Update metrics every interval, rather than once")
		showVersion = flag.Bool("version", false, "Show the version")
		quiet       = flag.Bool("quiet", false, "Only print errors")
		debug       = flag.Bool("debug", false, "Show debug output")
		debugHttp   = flag.Bool("debug-http", false, "Show full http traces")
		dryRun      = flag.Bool("dry-run", false, "Whether to only print metrics")
		endpoint    = flag.String("endpoint", "https://agent.buildkite.com/v3", "A custom Buildkite Agent API endpoint")

		// backend config
		backendOpt     = flag.String("backend", "cloudwatch", "Specify the backend to use: cloudwatch, statsd, prometheus, stackdriver")
		statsdHost     = flag.String("statsd-host", "127.0.0.1:8125", "Specify the StatsD server")
		statsdTags     = flag.Bool("statsd-tags", false, "Whether your StatsD server supports tagging like Datadog")
		prometheusAddr = flag.String("prometheus-addr", ":8080", "Prometheus metrics transport bind address")
		prometheusPath = flag.String("prometheus-path", "/metrics", "Prometheus metrics transport path")
		clwRegion      = flag.String("cloudwatch-region", "", "AWS Region to connect to, defaults to $AWS_REGION or us-east-1")
		clwDimensions  = flag.String("cloudwatch-dimensions", "", "Cloudwatch dimensions to index metrics under, in the form of Key=Value, Other=Value")
		gcpProjectID   = flag.String("stackdriver-projectid", "", "Specify Stackdriver Project ID")
		nrAppName      = flag.String("newrelic-app-name", "", "New Relic application name for metric events")
		nrLicenseKey   = flag.String("newrelic-license-key", "", "New Relic license key for publishing events")
	)

	// custom config for multiple queues
	var queues stringSliceFlag
	flag.Var(&queues, "queue", "Specific queues to process")

	flag.Parse()

	if *showVersion {
		fmt.Printf("buildkite-agent-metrics %s\n", version.Version)
		os.Exit(0)
	}

	if *token == "" {
		if bkToken := os.Getenv("BUILDKITE_AGENT_TOKEN"); bkToken != "" {
			*token = bkToken
		} else {
			fmt.Println("Must provide a token")
			os.Exit(1)
		}
	}

	var err error
	switch strings.ToLower(*backendOpt) {
	case "cloudwatch":
		region := *clwRegion
		if envRegion := os.Getenv(`AWS_REGION`); region == "" && envRegion != "" {
			region = envRegion
		} else {
			region = `us-east-1`
		}
		dimensions, err := backend.ParseCloudWatchDimensions(*clwDimensions)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
		bk = backend.NewCloudWatchBackend(region, dimensions)
	case "statsd":
		bk, err = backend.NewStatsDBackend(*statsdHost, *statsdTags)
		if err != nil {
			fmt.Printf("Error starting StatsD, err: %v\n", err)
			os.Exit(1)
		}
	case "prometheus":
		bk = backend.NewPrometheusBackend(*prometheusPath, *prometheusAddr)
	case "stackdriver":
		bk, err = backend.NewStackDriverBackend(*gcpProjectID)
		if err != nil {
			fmt.Printf("Error starting Stackdriver backend, err: %v\n", err)
			os.Exit(1)
		}
	case "newrelic":
		bk, err = backend.NewNewRelicBackend(*nrAppName, *nrLicenseKey)
		if err != nil {
			fmt.Printf("Error starting New Relic client: %v\n", err)
			os.Exit(1)
		}
	default:
		fmt.Println("Must provide a supported backend: cloudwatch, statsd, prometheus, stackdriver, newrelic")
		os.Exit(1)
	}

	if *quiet {
		log.SetOutput(ioutil.Discard)
	}

	userAgent := fmt.Sprintf("buildkite-agent-metrics/%s buildkite-agent-metrics-cli", version.Version)
	if *interval > 0 {
		userAgent += fmt.Sprintf(" interval=%s", *interval)
	}

	c := collector.Collector{
		UserAgent: userAgent,
		Endpoint:  *endpoint,
		Token:     *token,
		Queues:    []string(queues),
		Quiet:     *quiet,
		Debug:     *debug,
		DebugHttp: *debugHttp,
	}

	f := func() (time.Duration, error) {
		t := time.Now()

		result, err := c.Collect()
		if err != nil {
			return time.Duration(0), err
		}

		if !*dryRun {
			err = bk.Collect(result)
			if err != nil {
				return time.Duration(0), err
			}
		}

		log.Printf("Finished in %s", time.Now().Sub(t))
		return result.PollDuration, nil
	}

	minPollDuration, err := f()
	if err != nil {
		fmt.Println(err)
	}

	if *interval > 0 {
		for {
			waitTime := *interval

			// Respect the min poll duration returned by the API
			if *interval < minPollDuration {
				log.Printf("Increasing poll duration based on rate-limit headers")
				waitTime = minPollDuration
			}

			log.Printf("Waiting for %v (minimum of %v)", waitTime, minPollDuration)
			time.Sleep(waitTime)

			minPollDuration, err = f()
			if err != nil {
				fmt.Println(err)
			}
		}
	}
}

type stringSliceFlag []string

func (i *stringSliceFlag) String() string {
	return fmt.Sprintf("%v", *i)
}

func (i *stringSliceFlag) Set(value string) error {
	*i = append(*i, value)
	return nil
}
<|endoftext|>


<commit_before>/*
Package goJson implements a simple library for json parsing.
*/
package goJson

import (
	"bytes"
	"encoding/json"
	"reflect"
)

func ParseJson(b []byte) (*Root, error) {
	r := bytes.NewReader(b)
	j := new(Root)
	var Val map[string]interface{}
	err := json.NewDecoder(r).Decode(&Val)
	j.Val = Val
	return j, err
}

type Root struct {
	Val interface{}
}

func (r *Root) Get(Name string) *Root {
	currentItem := r.Val.(map[string]interface{})[Name]
	root := new(Root)
	root.Val = currentItem.(map[string]interface{})
	return root
}

func (r *Root) GetAll(Name string) ([]*Root, error) {
	currentItem := r.Val.(map[string]interface{})[Name]
	result := []*Root{}
	if reflect.TypeOf(currentItem) != map[string]interface{} {
            return nil, errors.New(name+" is not array!")
        }
	items := currentItem.([]interface{})
	for _, elem := range items {
		root := new(Root)
		root.Val = elem
		result = append(result, root)
	}
	return result, nil
}

func (r *Root) String(Name string) string {
	return r.Val.(map[string]interface{})[Name].(string)
}

func (r *Root) Float(Name string) float64 {
	return r.Val.(map[string]interface{})[Name].(float64)
}

func (r *Root) Int(Name string) int {
	return int(r.Val.(map[string]interface{})[Name].(float64))
}

func (r *Root) Strings(Name string) []string {
	s := reflect.ValueOf(r.Val.(map[string]interface{})[Name])
	arr := make([]string, s.Len())
	for i := 0; i < s.Len(); i++ {
		arr[i] = s.Index(i).Interface().(string)
	}
	return arr
}
<commit_msg>error<commit_after>/*
Package goJson implements a simple library for json parsing.
*/
package goJson

import (
	"bytes"
	"encoding/json"
	"reflect"
)

func ParseJson(b []byte) (*Root, error) {
	r := bytes.NewReader(b)
	j := new(Root)
	var Val map[string]interface{}
	err := json.NewDecoder(r).Decode(&Val)
	j.Val = Val
	return j, err
}

type Root struct {
	Val interface{}
}

func (r *Root) Get(Name string) *Root {
	currentItem := r.Val.(map[string]interface{})[Name]
	root := new(Root)
	root.Val = currentItem.(map[string]interface{})
	return root
}

func (r *Root) GetAll(Name string) ([]*Root, error) {
	currentItem := r.Val.(map[string]interface{})[Name]
	result := []*Root{}
	if reflect.TypeOf(currentItem) != map[string]interface{} {
            return nil, errors.New(Name+" is not array!")
        }
	items := currentItem.([]interface{})
	for _, elem := range items {
		root := new(Root)
		root.Val = elem
		result = append(result, root)
	}
	return result, nil
}

func (r *Root) String(Name string) string {
	return r.Val.(map[string]interface{})[Name].(string)
}

func (r *Root) Float(Name string) float64 {
	return r.Val.(map[string]interface{})[Name].(float64)
}

func (r *Root) Int(Name string) int {
	return int(r.Val.(map[string]interface{})[Name].(float64))
}

func (r *Root) Strings(Name string) []string {
	s := reflect.ValueOf(r.Val.(map[string]interface{})[Name])
	arr := make([]string, s.Len())
	for i := 0; i < s.Len(); i++ {
		arr[i] = s.Index(i).Interface().(string)
	}
	return arr
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"log"
	"math"
	"os/user"
	"strings"

	"github.com/mattn/go-gtk/gdk"
	"github.com/mattn/go-gtk/gio"
	"github.com/mattn/go-gtk/gtk"
	gsv "github.com/mattn/go-gtk/gtksourceview"

	"github.com/sg3des/argum"
)

var (
	ui   *UI
	conf *Conf

	gvfsPath = "/run/user/%s/gvfs/"

	newtabiter int

	langManager = gsv.SourceLanguageManagerGetDefault()
	languages   = langManager.GetLanguageIds()

	charsets = []string{
		CHARSET_UTF8,
		"utf-16",
		"",
		"ISO-8859-2",
		"ISO-8859-7",
		"ISO-8859-9",
		"ISO-8859-15",
		"ShiftJIS",
		"EUC-KR",
		"gb18030",
		"Big5",
		"TIS-620",
		"KOI8-R",
		"",
		"windows-874",
		"windows-1250",
		"windows-1251",
		"windows-1252",
		"windows-1253",
		"windows-1254",
		"windows-1255",
		"windows-1256",
		"windows-1257",
		"windows-1258",
		"",
		CHARSET_BINARY}
)

var args struct {
	Files []string `argum:"pos"`
}

func init() {
	log.SetFlags(log.Lshortfile)
	argum.MustParse(&args)

	user, _ := user.Current()
	gvfsPath = fmt.Sprintf(gvfsPath, user.Uid)

	conf = NewConf()

	if len(args.Files) == 0 {
		args.Files = append(args.Files, "")
	}
}

func main() {
	gtk.Init(nil)
	ui = CreateUI()

	for _, filename := range args.Files {
		ui.NewTab(filename)
	}

	gtk.Main()
}

func issetLanguage(lang string) bool {
	for _, langID := range languages {
		if langID == lang {
			return true
		}
	}
	return false
}

func xmlLanguages() string {
	//construct sections
	structure := structureLanguages()

	var xmldata []string
	for section, langs := range structure {
		xmldata = append(xmldata, "<menu action='"+section+"'>")
		for _, l := range langs {
			xmldata = append(xmldata, "<menuitem action='"+l.name+"' />")
		}
		xmldata = append(xmldata, "</menu>")
	}

	return strings.Join(xmldata, "\n")
}

type language struct {
	n    int
	name string
}

func structureLanguages() map[string][]language {
	var structure = make(map[string][]language)
	for n, langname := range languages {
		lang := langManager.GetLanguage(langname)
		section := lang.GetSection()
		if _, ok := structure[section]; !ok {
			structure[section] = []language{}
		}
		structure[section] = append(structure[section], language{n, langname})
	}
	return structure
}

func xmlEncodings() string {
	var xmldata []string
	for _, c := range charsets {
		if len(c) == 0 {
			xmldata = append(xmldata, "<separator />")
		} else {
			xmldata = append(xmldata, "<menuitem action='"+c+"' />")
		}
	}
	return strings.Join(xmldata, "\n")
}

func errorMessage(err error) {
	m := gtk.NewMessageDialogWithMarkup(nil, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, err.Error())
	m.Run()
	m.Destroy()
}

func convertColor(col []int) *gdk.Color {
	r := uint16(math.Pow(float64(col[0]), 2))
	g := uint16(math.Pow(float64(col[1]), 2))
	b := uint16(math.Pow(float64(col[2]), 2))

	return gdk.NewColorRGB(r, g, b)
}

func resolveFilename(filename string) string {
	return gio.NewGFileForURI(filename).GetPath()
}
<commit_msg>fix resolve filename<commit_after>package main

import (
	"fmt"
	"log"
	"math"
	"net/url"
	"os/user"
	"strings"

	"github.com/mattn/go-gtk/gdk"
	"github.com/mattn/go-gtk/gio"
	"github.com/mattn/go-gtk/gtk"
	gsv "github.com/mattn/go-gtk/gtksourceview"

	"github.com/sg3des/argum"
)

var (
	ui   *UI
	conf *Conf

	gvfsPath = "/run/user/%s/gvfs/"

	newtabiter int

	langManager = gsv.SourceLanguageManagerGetDefault()
	languages   = langManager.GetLanguageIds()

	charsets = []string{
		CHARSET_UTF8,
		"utf-16",
		"",
		"ISO-8859-2",
		"ISO-8859-7",
		"ISO-8859-9",
		"ISO-8859-15",
		"ShiftJIS",
		"EUC-KR",
		"gb18030",
		"Big5",
		"TIS-620",
		"KOI8-R",
		"",
		"windows-874",
		"windows-1250",
		"windows-1251",
		"windows-1252",
		"windows-1253",
		"windows-1254",
		"windows-1255",
		"windows-1256",
		"windows-1257",
		"windows-1258",
		"",
		CHARSET_BINARY}
)

var args struct {
	Files []string `argum:"pos"`
}

func init() {
	log.SetFlags(log.Lshortfile)
	argum.MustParse(&args)

	user, _ := user.Current()
	gvfsPath = fmt.Sprintf(gvfsPath, user.Uid)

	conf = NewConf()

	if len(args.Files) == 0 {
		args.Files = append(args.Files, "")
	}
}

func main() {
	gtk.Init(nil)
	ui = CreateUI()

	for _, filename := range args.Files {
		ui.NewTab(filename)
	}

	gtk.Main()
}

func issetLanguage(lang string) bool {
	for _, langID := range languages {
		if langID == lang {
			return true
		}
	}
	return false
}

func xmlLanguages() string {
	//construct sections
	structure := structureLanguages()

	var xmldata []string
	for section, langs := range structure {
		xmldata = append(xmldata, "<menu action='"+section+"'>")
		for _, l := range langs {
			xmldata = append(xmldata, "<menuitem action='"+l.name+"' />")
		}
		xmldata = append(xmldata, "</menu>")
	}

	return strings.Join(xmldata, "\n")
}

type language struct {
	n    int
	name string
}

func structureLanguages() map[string][]language {
	var structure = make(map[string][]language)
	for n, langname := range languages {
		lang := langManager.GetLanguage(langname)
		section := lang.GetSection()
		if _, ok := structure[section]; !ok {
			structure[section] = []language{}
		}
		structure[section] = append(structure[section], language{n, langname})
	}
	return structure
}

func xmlEncodings() string {
	var xmldata []string
	for _, c := range charsets {
		if len(c) == 0 {
			xmldata = append(xmldata, "<separator />")
		} else {
			xmldata = append(xmldata, "<menuitem action='"+c+"' />")
		}
	}
	return strings.Join(xmldata, "\n")
}

func errorMessage(err error) {
	m := gtk.NewMessageDialogWithMarkup(nil, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, err.Error())
	m.Run()
	m.Destroy()
}

func convertColor(col []int) *gdk.Color {
	r := uint16(math.Pow(float64(col[0]), 2))
	g := uint16(math.Pow(float64(col[1]), 2))
	b := uint16(math.Pow(float64(col[2]), 2))

	return gdk.NewColorRGB(r, g, b)
}

func resolveFilename(filename string) string {
	u, err := url.Parse(filename)
	if err != nil {
		return filename
	}

	//if scheme exist, ex: 'sftp://' then parse path with how URI
	if u.Scheme != "" {
		filename = gio.NewGFileForURI(filename).GetPath()
	} else {
		filename = gio.NewGFileForPath(filename).GetPath()
	}

	return filename
}
<|endoftext|>


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

package main
/*
 *  Filename:    godirs.go
 *  Author:      Bryan Matsuo <bmatsuo@soe.ucsc.edu>
 *  Created:     Tue Jul  5 22:13:49 PDT 2011
 *  Description:
 *  Usage:       godirs [options] ARGUMENT ...
 */
import (
    "os"
    "fmt"
    "flag"
    "sync"
    "time"
    //"log"
    //"strings"
    "path/filepath"
    "container/list"
)
const (
    stdDelay = 50e3
)

type Options struct {
    list     func(string) ([]string,[]int64)
    beClever bool
    dir      string
    verbose  bool
}
var opt = Options{}
func SetupFlags() *flag.FlagSet {
    var fs = flag.NewFlagSet("godirs", flag.ExitOnError)
    fs.BoolVar(&(opt.beClever), "c", false, "Use the clever GoQueue.")
    fs.BoolVar(&(opt.verbose), "v", false, "Verbose program output.")
    return fs
}
func VerifyFlags(fs *flag.FlagSet) {
    if fs.NArg() < 1 {
        fmt.Fprintf(os.Stderr, "missing DIR argument.")
    }
}
func ParseFlags() {
    var fs = SetupFlags()
    fs.Parse(os.Args[1:])
    VerifyFlags(fs)
    opt.dir = fs.Arg(0)
    if opt.beClever {
        opt.list = WalkerList
    } else {
        opt.list = StupidWalkerList
    }
}

type GoQueue struct {
    //BaseWait     int64
    MaxProc      int
    //waittime     int64
    //maxwait      int64
    waitingToRun bool
    waitingOnQ   bool
    started      bool
    nextWake     chan bool
    restart      chan bool
    kill         chan bool
    processing   int             // Number of QueueTasks running
    startLock    *sync.Mutex     // Keep Start() race free.
    qLock        *sync.Mutex     // Lock the queue and the waitingOnQ flag.
    pLock        *sync.Mutex     // Lock the process counter
    waiting      *list.List      // Waiting processes
    idcount      int64           // pid counter
}
func NewGoQueue() *GoQueue {
    var rl = new(GoQueue)
    rl.startLock = new(sync.Mutex)
    rl.qLock     = new(sync.Mutex)
    rl.pLock     = new(sync.Mutex)
    rl.restart   = make(chan bool)
    rl.kill      = make(chan bool)
    rl.nextWake  = make(chan bool)
    rl.waiting   = list.New()
    rl.MaxProc   = 20
    //rl.BaseWait  = 10
    //rl.waittime  = rl.BaseWait
    //rl.maxwait   = 500e6
    rl.idcount   = 0
    return rl
}

//  Goroutines called from a GoQueue are given an int identifier unique
//  to that routine.
type GoQueueTask struct {
    id int64
    f  func(int64)
}
func (gq *GoQueue) Enqueue(f func(int64)) int64 {
    // Wrap the function so it works with the goroutine limiting code.
    var gqtFunc = func (id int64) {
        f(id)

        // Decrement the process counter.
        gq.pLock.Lock()
        gq.processing--
        var procWaiting = gq.waitingToRun
        if procWaiting {
            gq.waitingToRun = false
        }
        gq.pLock.Unlock()

        // Start any waiting process.
        if procWaiting {
            gq.nextWake<-true
        }
    }

    // Lock the queue and enqueue a new task.
    gq.qLock.Lock()
    gq.idcount++
    var id = gq.idcount
    gq.waiting.PushBack(GoQueueTask{id, gqtFunc})
    var loopWaiting = gq.waitingOnQ
    if loopWaiting {
        gq.waitingOnQ = false
    }
    gq.qLock.Unlock()

    // Restart the Start() loop if it was deemed necessary.
    if loopWaiting {
        gq.restart<-true
    }

    return id
}
func (gq *GoQueue) Stop() {
    gq.startLock.Lock()
    if !gq.started {
        gq.startLock.Unlock()
        return
    }
    gq.qLock.Lock()
    gq.waitingToRun = false
    gq.waitingOnQ = false
    close(gq.restart)
    close(gq.kill)
    close(gq.nextWake)
    gq.qLock.Unlock()
    gq.startLock.Unlock()
}
/*
func (gq *GoQueue) mustWait() {
    gq.waittime <<= 2
    if gq.waittime > gq.maxwait {
        gq.waittime = gq.maxwait
    }
}
func (gq *GoQueue) wait() {
    time.Sleep(gq.waittime)
}
*/
func (gq *GoQueue) next() {
    for true {
        // Attempt to start processing the file.
        gq.pLock.Lock()
        if gq.processing >= gq.MaxProc {
            // Too many threads, wait and try again.
            gq.waitingToRun = true
            //gq.mustWait()
            gq.pLock.Unlock()
            //gq.wait()
            var cont, ok =<-gq.nextWake
            if !ok {
                gq.nextWake = make(chan bool)
                return
            }
            if !cont {
                return
            }
            continue
        }
        // Keep the books and reset wait time before unlocking.
        gq.waitingToRun = false
        gq.processing++
        //gq.waittime = gq.BaseWait
        gq.pLock.Unlock()

        // Get an element from the queue.
        gq.qLock.Lock()
        var taskelm = gq.waiting.Front()
        gq.waiting.Remove(taskelm)
        gq.qLock.Unlock()

        // Begin processing and asyncronously return.
        var task = taskelm.Value.(GoQueueTask)
        go task.f(task.id)
        return
    }
}
func (gq *GoQueue) Start() {
    gq.startLock.Lock()
    if gq.started {
        panic("already started")
    }
    select {
    case _, okKill :=<-gq.kill:
        if !okKill {
            gq.kill = make(chan bool)
        }
    case _, okRestart :=<-gq.restart:
        if !okRestart {
            gq.restart = make(chan bool)
        }
    default:
    }
    gq.started = true
    gq.startLock.Unlock()
    for true {
        select {
        case die, ok :=<-gq.kill:
            if !ok {
                gq.kill = make(chan bool)
                return
            }
            if die {
                return
            }
        default:
            gq.qLock.Lock()
            gq.waitingOnQ = gq.waiting.Len() == 0
            gq.qLock.Unlock()

            if !gq.waitingOnQ {
                gq.next()
                continue
            }
            var cont, ok =<-gq.restart
            if !ok {
                gq.restart = make(chan bool)
                return
            }
            if !cont {
                return
            }
        }
    }
}

type Walker struct {
    gq  *GoQueue
    done   chan bool
    paths  []string
    sizes  []int64
    lock   *sync.Mutex
    wg     *sync.WaitGroup
}
func NewWalker() *Walker {
    var w    = new(Walker)
    w.gq     = NewGoQueue()
    w.lock   = new(sync.Mutex)
    w.wg     = new(sync.WaitGroup)
    w.done   = make(chan bool)
    w.paths  = make([]string, 0, 1)
    w.sizes  = make([]int64, 0, 1)
    return w
}
func (w *Walker) VisitFile(path string, info *os.FileInfo) {
    var f = func (id int64) {
        var stat, err = os.Stat(path)
        if err != nil {
            panic(err)
        }
        time.Sleep(stdDelay)
        w.lock.Lock()
        w.sizes = append(w.sizes, stat.Size)
        w.paths = append(w.paths, path)
        w.lock.Unlock()
        w.wg.Done()
    }
    w.wg.Add(1)
    w.gq.Enqueue(f)
}
func (w *Walker) VisitDir(path string, f *os.FileInfo) bool {
    return true
}
func WalkerList(dir string) ([]string, []int64) {
    var w = NewWalker()
    var errors = make(chan os.Error)
    go func() {
        for e := range errors {
            panic("Walk error: " + e.String())
        }
    } ()
    go w.gq.Start()
    filepath.Walk(dir, w, errors)
    w.wg.Wait()
    return w.paths, w.sizes
}

type StupidWalker struct {
    paths  []string
    sizes  []int64
}
func NewStupidWalker() *StupidWalker {
    var sw    = new(StupidWalker)
    sw.paths  = make([]string, 0, 1)
    sw.sizes  = make([]int64, 0, 1)
    return sw
}
func (sw *StupidWalker) VisitFile(path string, info *os.FileInfo) {
    var stat, err = os.Stat(path)
    if err != nil {
        panic(err)
    }
    time.Sleep(stdDelay)
    sw.sizes = append(sw.sizes, stat.Size)
    sw.paths = append(sw.paths, path)
}
func (sw *StupidWalker) VisitDir(path string, f *os.FileInfo) bool {
    return true
}
func StupidWalkerList(dir string) ([]string, []int64) {
    var sw = NewStupidWalker()
    var errors = make(chan os.Error)
    var done = make(chan bool)
    go func() {
        for e := range errors {
            panic("Walk error: " + e.String())
        }
        done<-true
    } ()
    filepath.Walk(dir, sw, errors)
    close(errors)
    <-done
    return sw.paths, sw.sizes
}

func main() {
    ParseFlags()
    var paths, sizes = opt.list(opt.dir)
    for i, path := range paths {
        fmt.Printf("\t%50s %d\n", path, sizes[i])
    }
}

/*
type recLister struct {
    BaseWait   int64
    MaxProc    int
    waittime   int64
    maxwait    int64
    processing int
    mutex      *sync.Mutex
    errors     chan os.Error
    done       chan bool
    paths      []string
    sizes      []int64
}
func newRecLister() *recLister {
    var rl = new(recLister)
    rl.mutex    = new(sync.Mutex)
    rl.errors   = make(chan os.Error)
    rl.done     = make(chan bool)
    rl.paths    = make([]string, 0, 1)
    rl.sizes    = make([]int64, 0, 1)
    rl.MaxProc  = 5
    rl.BaseWait = 10
    rl.waittime = rl.BaseWait
    rl.maxwait  = 500e6
    return rl
}
func (rl *recLister) VisitDir(path string, f *os.FileInfo) bool {
    //rl.paths = append(rl.paths, path)
    return true
}
func (rl *recLister) visitFile(path string, f *os.FileInfo) {
    var stat, err = os.Stat(path)
    if err != nil {
        panic(err)
    }
    rl.sizes = append(rl.sizes, stat.Size)
    rl.paths = append(rl.paths, path)
    //time.Sleep(5e9)
    rl.mutex.Lock()
    rl.processing--
    rl.mutex.Unlock()
}
func (rl *recLister) VisitFile(path string, f *os.FileInfo) {
    for true {
        // Attempt to start processing the file.
        rl.mutex.Lock()
        if rl.processing >= rl.MaxProc {
            // Too many threads, wait and try again.
            rl.waittime <<= 2
            if rl.waittime > rl.maxwait {
                rl.waittime = rl.maxwait
            }
            rl.mutex.Unlock()
            time.Sleep(rl.waittime)
            continue
        }
        // Keep the books and reset wait time before unlocking and processing.
        rl.processing++
        rl.waittime = rl.BaseWait
        rl.mutex.Unlock()
        go rl.visitFile(path, f)
        return
    }
}

func RecFileList(dir string) ([]string, []int64) {
    var rl = newRecLister()
    go func() {
        for e := range rl.errors {
            panic("Walk error: " + e.String())
        }
        rl.done <- true
    } ()
    filepath.Walk(dir, rl, rl.errors)
    close(rl.errors)
    <-rl.done
    return rl.paths, rl.sizes
}
*/
<commit_msg>Remove commented out waiting code.<commit_after>// Copyright 2011, Bryan Matsuo. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main
/*
 *  Filename:    godirs.go
 *  Author:      Bryan Matsuo <bmatsuo@soe.ucsc.edu>
 *  Created:     Tue Jul  5 22:13:49 PDT 2011
 *  Description:
 *  Usage:       godirs [options] ARGUMENT ...
 */
import (
    "os"
    "fmt"
    "flag"
    "sync"
    "time"
    //"log"
    //"strings"
    "path/filepath"
    "container/list"
)
const (
    stdDelay = 50e3
)

type Options struct {
    list     func(string) ([]string,[]int64)
    beClever bool
    dir      string
    verbose  bool
}
var opt = Options{}
func SetupFlags() *flag.FlagSet {
    var fs = flag.NewFlagSet("godirs", flag.ExitOnError)
    fs.BoolVar(&(opt.beClever), "c", false, "Use the clever GoQueue.")
    fs.BoolVar(&(opt.verbose), "v", false, "Verbose program output.")
    return fs
}
func VerifyFlags(fs *flag.FlagSet) {
    if fs.NArg() < 1 {
        fmt.Fprintf(os.Stderr, "missing DIR argument.")
    }
}
func ParseFlags() {
    var fs = SetupFlags()
    fs.Parse(os.Args[1:])
    VerifyFlags(fs)
    opt.dir = fs.Arg(0)
    if opt.beClever {
        opt.list = WalkerList
    } else {
        opt.list = StupidWalkerList
    }
}

type GoQueue struct {
    MaxProc      int
    waitingToRun bool
    waitingOnQ   bool
    started      bool
    nextWake     chan bool
    restart      chan bool
    kill         chan bool
    processing   int             // Number of QueueTasks running
    startLock    *sync.Mutex     // Keep Start() race free.
    qLock        *sync.Mutex     // Lock the queue and the waitingOnQ flag.
    pLock        *sync.Mutex     // Lock the process counter
    waiting      *list.List      // Waiting processes
    idcount      int64           // pid counter
}
func NewGoQueue() *GoQueue {
    var rl = new(GoQueue)
    rl.startLock = new(sync.Mutex)
    rl.qLock     = new(sync.Mutex)
    rl.pLock     = new(sync.Mutex)
    rl.restart   = make(chan bool)
    rl.kill      = make(chan bool)
    rl.nextWake  = make(chan bool)
    rl.waiting   = list.New()
    rl.MaxProc   = 20
    rl.idcount   = 0
    return rl
}

//  Goroutines called from a GoQueue are given an int identifier unique
//  to that routine.
type GoQueueTask struct {
    id int64
    f  func(int64)
}
func (gq *GoQueue) Enqueue(f func(int64)) int64 {
    // Wrap the function so it works with the goroutine limiting code.
    var gqtFunc = func (id int64) {
        f(id)

        // Decrement the process counter.
        gq.pLock.Lock()
        gq.processing--
        var procWaiting = gq.waitingToRun
        if procWaiting {
            gq.waitingToRun = false
        }
        gq.pLock.Unlock()

        // Start any waiting process.
        if procWaiting {
            gq.nextWake<-true
        }
    }

    // Lock the queue and enqueue a new task.
    gq.qLock.Lock()
    gq.idcount++
    var id = gq.idcount
    gq.waiting.PushBack(GoQueueTask{id, gqtFunc})
    var loopWaiting = gq.waitingOnQ
    if loopWaiting {
        gq.waitingOnQ = false
    }
    gq.qLock.Unlock()

    // Restart the Start() loop if it was deemed necessary.
    if loopWaiting {
        gq.restart<-true
    }

    return id
}
func (gq *GoQueue) Stop() {
    gq.startLock.Lock()
    if !gq.started {
        gq.startLock.Unlock()
        return
    }
    gq.qLock.Lock()
    gq.waitingToRun = false
    gq.waitingOnQ = false
    close(gq.restart)
    close(gq.kill)
    close(gq.nextWake)
    gq.qLock.Unlock()
    gq.startLock.Unlock()
}
func (gq *GoQueue) next() {
    for true {
        // Attempt to start processing the file.
        gq.pLock.Lock()
        if gq.processing >= gq.MaxProc {
            // Too many threads, wait and try again.
            gq.waitingToRun = true
            gq.pLock.Unlock()
            var cont, ok =<-gq.nextWake
            if !ok {
                gq.nextWake = make(chan bool)
                return
            }
            if !cont {
                return
            }
            continue
        }
        // Keep the books and reset wait time before unlocking.
        gq.waitingToRun = false
        gq.processing++
        gq.pLock.Unlock()

        // Get an element from the queue.
        gq.qLock.Lock()
        var taskelm = gq.waiting.Front()
        gq.waiting.Remove(taskelm)
        gq.qLock.Unlock()

        // Begin processing and asyncronously return.
        var task = taskelm.Value.(GoQueueTask)
        go task.f(task.id)
        return
    }
}
func (gq *GoQueue) Start() {
    gq.startLock.Lock()
    if gq.started {
        panic("already started")
    }
    select {
    case _, okKill :=<-gq.kill:
        if !okKill {
            gq.kill = make(chan bool)
        }
    case _, okRestart :=<-gq.restart:
        if !okRestart {
            gq.restart = make(chan bool)
        }
    default:
    }
    gq.started = true
    gq.startLock.Unlock()
    for true {
        select {
        case die, ok :=<-gq.kill:
            if !ok {
                gq.kill = make(chan bool)
                return
            }
            if die {
                return
            }
        default:
            gq.qLock.Lock()
            gq.waitingOnQ = gq.waiting.Len() == 0
            gq.qLock.Unlock()

            if !gq.waitingOnQ {
                gq.next()
                continue
            }
            var cont, ok =<-gq.restart
            if !ok {
                gq.restart = make(chan bool)
                return
            }
            if !cont {
                return
            }
        }
    }
}

type Walker struct {
    gq  *GoQueue
    done   chan bool
    paths  []string
    sizes  []int64
    lock   *sync.Mutex
    wg     *sync.WaitGroup
}
func NewWalker() *Walker {
    var w    = new(Walker)
    w.gq     = NewGoQueue()
    w.lock   = new(sync.Mutex)
    w.wg     = new(sync.WaitGroup)
    w.done   = make(chan bool)
    w.paths  = make([]string, 0, 1)
    w.sizes  = make([]int64, 0, 1)
    return w
}
func (w *Walker) VisitFile(path string, info *os.FileInfo) {
    var f = func (id int64) {
        var stat, err = os.Stat(path)
        if err != nil {
            panic(err)
        }
        time.Sleep(stdDelay)
        w.lock.Lock()
        w.sizes = append(w.sizes, stat.Size)
        w.paths = append(w.paths, path)
        w.lock.Unlock()
        w.wg.Done()
    }
    w.wg.Add(1)
    w.gq.Enqueue(f)
}
func (w *Walker) VisitDir(path string, f *os.FileInfo) bool {
    return true
}
func WalkerList(dir string) ([]string, []int64) {
    var w = NewWalker()
    var errors = make(chan os.Error)
    go func() {
        for e := range errors {
            panic("Walk error: " + e.String())
        }
    } ()
    go w.gq.Start()
    filepath.Walk(dir, w, errors)
    w.wg.Wait()
    return w.paths, w.sizes
}

type StupidWalker struct {
    paths  []string
    sizes  []int64
}
func NewStupidWalker() *StupidWalker {
    var sw    = new(StupidWalker)
    sw.paths  = make([]string, 0, 1)
    sw.sizes  = make([]int64, 0, 1)
    return sw
}
func (sw *StupidWalker) VisitFile(path string, info *os.FileInfo) {
    var stat, err = os.Stat(path)
    if err != nil {
        panic(err)
    }
    time.Sleep(stdDelay)
    sw.sizes = append(sw.sizes, stat.Size)
    sw.paths = append(sw.paths, path)
}
func (sw *StupidWalker) VisitDir(path string, f *os.FileInfo) bool {
    return true
}
func StupidWalkerList(dir string) ([]string, []int64) {
    var sw = NewStupidWalker()
    var errors = make(chan os.Error)
    var done = make(chan bool)
    go func() {
        for e := range errors {
            panic("Walk error: " + e.String())
        }
        done<-true
    } ()
    filepath.Walk(dir, sw, errors)
    close(errors)
    <-done
    return sw.paths, sw.sizes
}

func main() {
    ParseFlags()
    var paths, sizes = opt.list(opt.dir)
    for i, path := range paths {
        fmt.Printf("\t%50s %d\n", path, sizes[i])
    }
}

/*
type recLister struct {
    BaseWait   int64
    MaxProc    int
    waittime   int64
    maxwait    int64
    processing int
    mutex      *sync.Mutex
    errors     chan os.Error
    done       chan bool
    paths      []string
    sizes      []int64
}
func newRecLister() *recLister {
    var rl = new(recLister)
    rl.mutex    = new(sync.Mutex)
    rl.errors   = make(chan os.Error)
    rl.done     = make(chan bool)
    rl.paths    = make([]string, 0, 1)
    rl.sizes    = make([]int64, 0, 1)
    rl.MaxProc  = 5
    rl.BaseWait = 10
    rl.waittime = rl.BaseWait
    rl.maxwait  = 500e6
    return rl
}
func (rl *recLister) VisitDir(path string, f *os.FileInfo) bool {
    //rl.paths = append(rl.paths, path)
    return true
}
func (rl *recLister) visitFile(path string, f *os.FileInfo) {
    var stat, err = os.Stat(path)
    if err != nil {
        panic(err)
    }
    rl.sizes = append(rl.sizes, stat.Size)
    rl.paths = append(rl.paths, path)
    //time.Sleep(5e9)
    rl.mutex.Lock()
    rl.processing--
    rl.mutex.Unlock()
}
func (rl *recLister) VisitFile(path string, f *os.FileInfo) {
    for true {
        // Attempt to start processing the file.
        rl.mutex.Lock()
        if rl.processing >= rl.MaxProc {
            // Too many threads, wait and try again.
            rl.waittime <<= 2
            if rl.waittime > rl.maxwait {
                rl.waittime = rl.maxwait
            }
            rl.mutex.Unlock()
            time.Sleep(rl.waittime)
            continue
        }
        // Keep the books and reset wait time before unlocking and processing.
        rl.processing++
        rl.waittime = rl.BaseWait
        rl.mutex.Unlock()
        go rl.visitFile(path, f)
        return
    }
}

func RecFileList(dir string) ([]string, []int64) {
    var rl = newRecLister()
    go func() {
        for e := range rl.errors {
            panic("Walk error: " + e.String())
        }
        rl.done <- true
    } ()
    filepath.Walk(dir, rl, rl.errors)
    close(rl.errors)
    <-rl.done
    return rl.paths, rl.sizes
}
*/
<|endoftext|>


<commit_before>package godist

// Job interface represents the functionality that a concrete job data
// structure must provide in order to submit to the workflow.
//
// Parse is responsible for any processing on a Job after
// initialization. It ought return nil if errors were encountered
// during parsing.
//
// Expand expands the Job structure into multiple Task structures, or
// returns a nil slice and an error. The Task structures should have a
// reference to their Job, so the Task results can be Integrated into
// the Job's response.
//
// Integrate receives a Task structure and integrates its results into
// the Job's results. Once the Job has received and integrated results
// from all Task structures, it ought return itself so the Workflow
// can pass the Job to the Respond stage. Prior to receiving all Task
// results, Integrate ought return nil.
//
// Respond processes the Job results and sends the results to the
// caller in the requested format, or sends an error.
//
// Wait should block until the Job has been responded to.
type Job interface {
	Parse() error
	Expand() ([]Task, error)
	Integrate(Task) Job
	Respond() (int, error)
	Wait()
}

// Task interface represents the functionality that a concrete task
// data structure must provide in order to be processed by the
// workflow.
//
// Perform actually performs the work needed by the task, and either
// returns nil or an error, as appropriate.
//
// Integrate invokes the Job's Integrate method above, but is required
// because once the Workflow has performed a Task, it does not have a
// reference to the Task's Job structure. See 'sillyTask'
// implementation of Integrate for an example.
type Task interface {
	Perform() error
	Error() error
	Integrate() Job
}

// Workflow interface allows different implementations of a workflow.
type Workflow interface {
	SubmitWithCallback(Job, func())
	SubmitAndWait(Job)
	Quit()
}
<commit_msg>english<commit_after>package godist

// Job interface represents the functionality that a concrete job data
// structure must provide in order to submit to the workflow.
//
// Parse is responsible for any processing on a Job after
// initialization. It ought return nil if no errors were encountered
// during parsing.
//
// Expand expands the Job structure into multiple Task structures, or
// returns a nil slice and an error. The Task structures should have a
// reference to their Job, so the Task results can be Integrated into
// the Job's response.
//
// Integrate receives a Task structure and integrates its results into
// the Job's results. Once the Job has received and integrated results
// from all Task structures, it ought return itself so the Workflow
// can pass the Job to the Respond stage. Prior to receiving all Task
// results, Integrate ought return nil.
//
// Respond processes the Job results and sends the results to the
// caller in the requested format, or sends an error.
//
// Wait should block until the Job has been responded to.
type Job interface {
	Parse() error
	Expand() ([]Task, error)
	Integrate(Task) Job
	Respond() (int, error)
	Wait()
}

// Task interface represents the functionality that a concrete task
// data structure must provide in order to be processed by the
// workflow.
//
// Perform actually performs the work needed by the task, and either
// returns nil or an error, as appropriate.
//
// Integrate invokes the Job's Integrate method above, but is required
// because once the Workflow has performed a Task, it does not have a
// reference to the Task's Job structure. See 'sillyTask'
// implementation of Integrate for an example.
type Task interface {
	Perform() error
	Error() error
	Integrate() Job
}

// Workflow interface allows different implementations of a workflow.
type Workflow interface {
	SubmitWithCallback(Job, func())
	SubmitAndWait(Job)
	Quit()
}
<|endoftext|>