<commit_before>package main

import (
	"fmt"
	"io"
	"os"
	"runtime/pprof"

	cmds "github.com/jbenet/go-ipfs/commands"
	cmdsCli "github.com/jbenet/go-ipfs/commands/cli"
	cmdsHttp "github.com/jbenet/go-ipfs/commands/http"
	"github.com/jbenet/go-ipfs/config"
	"github.com/jbenet/go-ipfs/core"
	commands "github.com/jbenet/go-ipfs/core/commands2"
	daemon "github.com/jbenet/go-ipfs/daemon2"
	u "github.com/jbenet/go-ipfs/util"
)

// log is the command logger
var log = u.Logger("cmd/ipfs")

func main() {
	args := os.Args[1:]

	req, root, err := cmdsCli.Parse(args, Root, commands.Root)
	if err != nil {
		fmt.Println(err)
		fmt.Println(Root.Help)
		os.Exit(1)
	}

	options, err := getOptions(req, root)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	if help, found := options.Option("help"); found && help.(bool) {
		fmt.Println(req.Command().Help)
		os.Exit(0)
	}

	if debug, found := options.Option("debug"); found && debug.(bool) {
		u.Debug = true

		// if debugging, setup profiling.
		if u.Debug {
			ofi, err := os.Create("cpu.prof")
			if err != nil {
				fmt.Println(err)
				return
			}
			pprof.StartCPUProfile(ofi)
			defer ofi.Close()
			defer pprof.StopCPUProfile()
		}
	}

	configPath, err := getConfigRoot(options)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	conf, err := getConfig(configPath)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	ctx := req.Context()
	ctx.ConfigRoot = configPath
	ctx.Config = conf

	if _, found := options.Option("encoding"); !found {
		if req.Command().Format != nil {
			req.SetOption("encoding", cmds.Text)
		} else {
			req.SetOption("encoding", cmds.JSON)
		}
	}

	var res cmds.Response
	if root == Root {
		res = root.Call(req)

	} else {
		local, found := options.Option("local")

		if (!found || !local.(bool)) && daemon.Locked(configPath) {
			res, err = cmdsHttp.Send(req)
			if err != nil {
				fmt.Println(err)
				os.Exit(1)
			}

		} else {
			node, err := core.NewIpfsNode(conf, false)
			if err != nil {
				fmt.Println(err)
				os.Exit(1)
			}
			ctx.Node = node

			res = root.Call(req)
		}
	}

	if res.Error() != nil {
		fmt.Println(res.Error().Error())

		if req.Command().Help != "" && res.Error().Code == cmds.ErrClient {
			// TODO: convert from markdown to ANSI terminal format?
			fmt.Println(req.Command().Help)
		}

		os.Exit(1)
	}

	_, err = io.Copy(os.Stdout, res)
	if err != nil {
		fmt.Println(err.Error())
	}
}

func getOptions(req cmds.Request, root *cmds.Command) (cmds.Request, error) {
	tempReq := cmds.NewRequest(req.Path(), req.Options(), nil, nil, nil)

	options, err := root.GetOptions(tempReq.Path())
	if err != nil {
		return nil, err
	}

	err = tempReq.ConvertOptions(options)
	if err != nil {
		return nil, err
	}

	return tempReq, nil
}

func getConfigRoot(req cmds.Request) (string, error) {
	if opt, found := req.Option("config"); found {
		return opt.(string), nil
	}

	configPath, err := config.PathRoot()
	if err != nil {
		return "", err
	}
	return configPath, nil
}

func getConfig(path string) (*config.Config, error) {
	configFile, err := config.Filename(path)
	if err != nil {
		return nil, err
	}

	return config.Load(configFile)
}
<commit_msg>commands: Removed inpout stream from Request<commit_after>package main

import (
	"fmt"
	"io"
	"os"
	"runtime/pprof"

	cmds "github.com/jbenet/go-ipfs/commands"
	cmdsCli "github.com/jbenet/go-ipfs/commands/cli"
	cmdsHttp "github.com/jbenet/go-ipfs/commands/http"
	"github.com/jbenet/go-ipfs/config"
	"github.com/jbenet/go-ipfs/core"
	commands "github.com/jbenet/go-ipfs/core/commands2"
	daemon "github.com/jbenet/go-ipfs/daemon2"
	u "github.com/jbenet/go-ipfs/util"
)

// log is the command logger
var log = u.Logger("cmd/ipfs")

func main() {
	args := os.Args[1:]

	req, root, err := cmdsCli.Parse(args, Root, commands.Root)
	if err != nil {
		fmt.Println(err)
		fmt.Println(Root.Help)
		os.Exit(1)
	}

	options, err := getOptions(req, root)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	if help, found := options.Option("help"); found && help.(bool) {
		fmt.Println(req.Command().Help)
		os.Exit(0)
	}

	if debug, found := options.Option("debug"); found && debug.(bool) {
		u.Debug = true

		// if debugging, setup profiling.
		if u.Debug {
			ofi, err := os.Create("cpu.prof")
			if err != nil {
				fmt.Println(err)
				return
			}
			pprof.StartCPUProfile(ofi)
			defer ofi.Close()
			defer pprof.StopCPUProfile()
		}
	}

	configPath, err := getConfigRoot(options)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	conf, err := getConfig(configPath)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	ctx := req.Context()
	ctx.ConfigRoot = configPath
	ctx.Config = conf

	if _, found := options.Option("encoding"); !found {
		if req.Command().Format != nil {
			req.SetOption("encoding", cmds.Text)
		} else {
			req.SetOption("encoding", cmds.JSON)
		}
	}

	var res cmds.Response
	if root == Root {
		res = root.Call(req)

	} else {
		local, found := options.Option("local")

		if (!found || !local.(bool)) && daemon.Locked(configPath) {
			res, err = cmdsHttp.Send(req)
			if err != nil {
				fmt.Println(err)
				os.Exit(1)
			}

		} else {
			node, err := core.NewIpfsNode(conf, false)
			if err != nil {
				fmt.Println(err)
				os.Exit(1)
			}
			ctx.Node = node

			res = root.Call(req)
		}
	}

	if res.Error() != nil {
		fmt.Println(res.Error().Error())

		if req.Command().Help != "" && res.Error().Code == cmds.ErrClient {
			// TODO: convert from markdown to ANSI terminal format?
			fmt.Println(req.Command().Help)
		}

		os.Exit(1)
	}

	_, err = io.Copy(os.Stdout, res)
	if err != nil {
		fmt.Println(err.Error())
	}
}

func getOptions(req cmds.Request, root *cmds.Command) (cmds.Request, error) {
	tempReq := cmds.NewRequest(req.Path(), req.Options(), nil, nil)

	options, err := root.GetOptions(tempReq.Path())
	if err != nil {
		return nil, err
	}

	err = tempReq.ConvertOptions(options)
	if err != nil {
		return nil, err
	}

	return tempReq, nil
}

func getConfigRoot(req cmds.Request) (string, error) {
	if opt, found := req.Option("config"); found {
		return opt.(string), nil
	}

	configPath, err := config.PathRoot()
	if err != nil {
		return "", err
	}
	return configPath, nil
}

func getConfig(path string) (*config.Config, error) {
	configFile, err := config.Filename(path)
	if err != nil {
		return nil, err
	}

	return config.Load(configFile)
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"net/http"
	"net/url"

	log "github.com/Sirupsen/logrus"
	"github.com/hellofresh/janus/pkg/api"
	"github.com/hellofresh/janus/pkg/errors"
	"github.com/hellofresh/janus/pkg/middleware"
	"github.com/hellofresh/janus/pkg/oauth"
	"github.com/hellofresh/janus/pkg/proxy"
	"github.com/hellofresh/janus/pkg/router"
	"github.com/hellofresh/janus/pkg/web"
	"github.com/hellofresh/stats-go"
	mgo "gopkg.in/mgo.v2"
)

func main() {
	var repo api.Repository
	var oAuthServersRepo oauth.Repository
	var readOnlyAPI bool
	var err error

	log.Info("Janus starting...")

	sectionsTestsMap, err := stats.ParseSectionsTestsMap(globalConfig.Stats.IDs)
	if err != nil {
		log.WithError(err).WithField("config", globalConfig.Stats.IDs).
			Error("Failed to parse stats second level IDs from env")
		sectionsTestsMap = map[stats.PathSection]stats.SectionTestDefinition{}
	}
	log.WithField("config", globalConfig.Stats.IDs).WithField("map", sectionsTestsMap.String()).
		Debug("Setting stats second level IDs")

	statsClient := stats.NewStatsdStatsClient(globalConfig.Stats.DSN, globalConfig.Stats.Prefix).
		SetHttpMetricCallback(stats.NewHasIDAtSecondLevelCallback(sectionsTestsMap))
	defer statsClient.Close()

	dsnURL, err := url.Parse(globalConfig.Database.DSN)
	switch dsnURL.Scheme {
	case "mongodb":
		log.WithField("dsn", globalConfig.Database.DSN).Debug("Trying to connect to DB")
		session, err := mgo.Dial(globalConfig.Database.DSN)
		if err != nil {
			log.Panic(err)
		}

		defer session.Close()

		log.Debug("Connected to mongodb")
		session.SetMode(mgo.Monotonic, true)

		log.Debug("Loading API definitions from Mongo DB")
		repo, err = api.NewMongoAppRepository(session)
		if err != nil {
			log.Panic(err)
		}

		// create the proxy
		log.Debug("Loading OAuth servers definitions from Mongo DB")
		oAuthServersRepo, err = oauth.NewMongoRepository(session)
		if err != nil {
			log.Panic(err)
		}
	case "file":
		var apiPath = dsnURL.Path + "/apis"
		var authPath = dsnURL.Path + "/auth"

		log.WithField("path", apiPath).Debug("Loading API definitions from file system")
		repo, err = api.NewFileSystemRepository(apiPath)
		if err != nil {
			log.Panic(err)
		}

		log.WithField("path", authPath).Debug("Loading OAuth servers definitions from file system")
		oAuthServersRepo, err = oauth.NewFileSystemRepository(authPath)
		if err != nil {
			log.Panic(err)
		}

		readOnlyAPI = true
	default:
		log.WithError(errors.ErrInvalidScheme).Error("No Database selected")
	}

	transport := oauth.NewAwareTransport(statsClient, storage, oAuthServersRepo)
	p := proxy.WithParams(proxy.Params{
		Transport:              transport,
		FlushInterval:          globalConfig.BackendFlushInterval,
		IdleConnectionsPerHost: globalConfig.MaxIdleConnsPerHost,
		CloseIdleConnsPeriod:   globalConfig.CloseIdleConnsPeriod,
		InsecureSkipVerify:     globalConfig.InsecureSkipVerify,
	})
	defer p.Close()

	// create router with a custom not found handler
	router.DefaultOptions.NotFoundHandler = web.NotFound
	r := router.NewHTTPTreeMuxWithOptions(router.DefaultOptions)
	r.Use(
		middleware.NewStats(statsClient).Handler,
		middleware.NewLogger().Handler,
		middleware.NewRecovery(web.RecoveryHandler).Handler,
		middleware.NewOpenTracing(globalConfig.IsHTTPS()).Handler,
	)

	// create proxy register
	register := proxy.NewRegister(r, p)

	apiLoader := api.NewLoader(register, storage, oAuthServersRepo)
	apiLoader.LoadDefinitions(repo)

	oauthLoader := oauth.NewLoader(register, storage)
	oauthLoader.LoadDefinitions(oAuthServersRepo)

	wp := web.Provider{
		Port:     globalConfig.APIPort,
		Cred:     globalConfig.Credentials,
		APIRepo:  repo,
		AuthRepo: oAuthServersRepo,
		ReadOnly: readOnlyAPI,
	}
	wp.Provide()

	log.Fatal(listenAndServe(r))
}

func listenAndServe(handler http.Handler) error {
	address := fmt.Sprintf(":%v", globalConfig.Port)
	log.WithField("address", address).Info("Listening on")
	log.Info("Janus started")
	if globalConfig.IsHTTPS() {
		return http.ListenAndServeTLS(address, globalConfig.CertPathTLS, globalConfig.KeyPathTLS, handler)
	}

	log.Info("Certificate and certificate key were not found, defaulting to HTTP")
	return http.ListenAndServe(address, handler)
}
<commit_msg>Passing the stats client to the proxy<commit_after>package main

import (
	"fmt"
	"net/http"
	"net/url"

	log "github.com/Sirupsen/logrus"
	"github.com/hellofresh/janus/pkg/api"
	"github.com/hellofresh/janus/pkg/errors"
	"github.com/hellofresh/janus/pkg/middleware"
	"github.com/hellofresh/janus/pkg/oauth"
	"github.com/hellofresh/janus/pkg/proxy"
	"github.com/hellofresh/janus/pkg/router"
	"github.com/hellofresh/janus/pkg/web"
	"github.com/hellofresh/stats-go"
	mgo "gopkg.in/mgo.v2"
)

func main() {
	var repo api.Repository
	var oAuthServersRepo oauth.Repository
	var readOnlyAPI bool
	var err error

	log.Info("Janus starting...")

	sectionsTestsMap, err := stats.ParseSectionsTestsMap(globalConfig.Stats.IDs)
	if err != nil {
		log.WithError(err).WithField("config", globalConfig.Stats.IDs).
			Error("Failed to parse stats second level IDs from env")
		sectionsTestsMap = map[stats.PathSection]stats.SectionTestDefinition{}
	}
	log.WithField("config", globalConfig.Stats.IDs).WithField("map", sectionsTestsMap.String()).
		Debug("Setting stats second level IDs")

	statsClient := stats.NewStatsdStatsClient(globalConfig.Stats.DSN, globalConfig.Stats.Prefix).
		SetHttpMetricCallback(stats.NewHasIDAtSecondLevelCallback(sectionsTestsMap))
	defer statsClient.Close()

	dsnURL, err := url.Parse(globalConfig.Database.DSN)
	switch dsnURL.Scheme {
	case "mongodb":
		log.WithField("dsn", globalConfig.Database.DSN).Debug("Trying to connect to DB")
		session, err := mgo.Dial(globalConfig.Database.DSN)
		if err != nil {
			log.Panic(err)
		}

		defer session.Close()

		log.Debug("Connected to mongodb")
		session.SetMode(mgo.Monotonic, true)

		log.Debug("Loading API definitions from Mongo DB")
		repo, err = api.NewMongoAppRepository(session)
		if err != nil {
			log.Panic(err)
		}

		// create the proxy
		log.Debug("Loading OAuth servers definitions from Mongo DB")
		oAuthServersRepo, err = oauth.NewMongoRepository(session)
		if err != nil {
			log.Panic(err)
		}
	case "file":
		var apiPath = dsnURL.Path + "/apis"
		var authPath = dsnURL.Path + "/auth"

		log.WithField("path", apiPath).Debug("Loading API definitions from file system")
		repo, err = api.NewFileSystemRepository(apiPath)
		if err != nil {
			log.Panic(err)
		}

		log.WithField("path", authPath).Debug("Loading OAuth servers definitions from file system")
		oAuthServersRepo, err = oauth.NewFileSystemRepository(authPath)
		if err != nil {
			log.Panic(err)
		}

		readOnlyAPI = true
	default:
		log.WithError(errors.ErrInvalidScheme).Error("No Database selected")
	}

	p := proxy.WithParams(proxy.Params{
		StatsClient:            statsClient,
		FlushInterval:          globalConfig.BackendFlushInterval,
		IdleConnectionsPerHost: globalConfig.MaxIdleConnsPerHost,
		CloseIdleConnsPeriod:   globalConfig.CloseIdleConnsPeriod,
		InsecureSkipVerify:     globalConfig.InsecureSkipVerify,
	})
	defer p.Close()

	// create router with a custom not found handler
	router.DefaultOptions.NotFoundHandler = web.NotFound
	r := router.NewHTTPTreeMuxWithOptions(router.DefaultOptions)
	r.Use(
		middleware.NewStats(statsClient).Handler,
		middleware.NewLogger().Handler,
		middleware.NewRecovery(web.RecoveryHandler).Handler,
		middleware.NewOpenTracing(globalConfig.IsHTTPS()).Handler,
	)

	// create proxy register
	register := proxy.NewRegister(r, p)

	apiLoader := api.NewLoader(register, storage, oAuthServersRepo)
	apiLoader.LoadDefinitions(repo)

	oauthLoader := oauth.NewLoader(register, storage)
	oauthLoader.LoadDefinitions(oAuthServersRepo)

	wp := web.Provider{
		Port:     globalConfig.APIPort,
		Cred:     globalConfig.Credentials,
		APIRepo:  repo,
		AuthRepo: oAuthServersRepo,
		ReadOnly: readOnlyAPI,
	}
	wp.Provide()

	log.Fatal(listenAndServe(r))
}

func listenAndServe(handler http.Handler) error {
	address := fmt.Sprintf(":%v", globalConfig.Port)
	log.WithField("address", address).Info("Listening on")
	log.Info("Janus started")
	if globalConfig.IsHTTPS() {
		return http.ListenAndServeTLS(address, globalConfig.CertPathTLS, globalConfig.KeyPathTLS, handler)
	}

	log.Info("Certificate and certificate key were not found, defaulting to HTTP")
	return http.ListenAndServe(address, handler)
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"os"
	"net"
	"time"

	"github.com/tj/go-gracefully"
	"github.com/travisjeffery/jocko/broker"
	"github.com/travisjeffery/jocko/protocol"
	"github.com/travisjeffery/jocko/raft"
	"github.com/travisjeffery/jocko/serf"
	"github.com/travisjeffery/jocko/server"
	"github.com/travisjeffery/simplelog"
	"gopkg.in/alecthomas/kingpin.v2"
)

var (
	cli       = kingpin.New("jocko", "Jocko, a Go implementation of Kafka")
	logDir    = cli.Flag("logdir", "A comma separated list of directories under which to store log files").Default("/tmp/jocko").String()
	debugLogs = cli.Flag("debug", "Enable debug logs").Default("false").Bool()

	brokerCmd            = cli.Command("broker", "Operations on brokers")
	brokerCmdRaftAddr    = brokerCmd.Flag("raftaddr", "Address for Raft to bind and advertise on").Default("127.0.0.1:9093").String()
	brokerCmdBrokerAddr  = brokerCmd.Flag("brokeraddr", "Address for broker to bind on").Default("0.0.0.0:9092").String()
	brokerCmdSerfAddr    = brokerCmd.Flag("serfaddr", "Address for Serf to bind on").Default("0.0.0.0:9094").String()
	brokerCmdSerfMembers = brokerCmd.Flag("serfmembers", "List of existing Serf members").Strings()
	brokerCmdBrokerID    = brokerCmd.Flag("id", "Broker ID").Int32()

	topic                  = cli.Command("topic", "Operations on topics")
	topicBrokerAddr        = topic.Flag("brokeraddr", "Address for Broker to bind on").Default("0.0.0.0:9092").String()
	topicTopic             = topic.Flag("topic", "Name of topic to create").String()
	topicPartitions        = topic.Flag("partitions", "Number of partitions").Default("1").Int32()
	topicReplicationFactor = topic.Flag("replicationfactor", "Replication factor").Default("1").Int16()
)

func main() {
	logLevel := simplelog.INFO
	if *debugLogs {
		logLevel = simplelog.DEBUG
	}
	logger := simplelog.New(os.Stdout, logLevel, "jocko")

	switch kingpin.MustParse(cli.Parse(os.Args[1:])) {
	case brokerCmd.FullCommand():
		os.Exit(CmdBrokers(logger))

	case topic.FullCommand():
		os.Exit(CmdTopic(logger))
	}
}

func CmdBrokers(logger *simplelog.Logger) int {
	serf, err := serf.New(
		serf.Logger(logger),
		serf.Addr(*brokerCmdSerfAddr),
		serf.InitMembers(*brokerCmdSerfMembers),
	)

	raft, err := raft.New(
		raft.Logger(logger),
		raft.DataDir(*logDir),
		raft.Addr(*brokerCmdRaftAddr),
	)

	store, err := broker.New(*brokerCmdBrokerID,
		broker.LogDir(*logDir),
		broker.Logger(logger),
		broker.Addr(*brokerCmdBrokerAddr),
		broker.Serf(serf),
		broker.Raft(raft),
	)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error with new broker: %s\n", err)
		os.Exit(1)
	}
	srv := server.New(*brokerCmdBrokerAddr, store, logger)
	if err := srv.Start(); err != nil {
		fmt.Fprintf(os.Stderr, "Error starting server: %s\n", err)
		os.Exit(1)
	}

	defer srv.Close()

	gracefully.Timeout = 10 * time.Second
	gracefully.Shutdown()

	if err := store.Shutdown(); err != nil {
		panic(err)
	}

	return 0
}

func CmdTopic(logger *simplelog.Logger) int {
	logger.Info("Create topic")
	addr, err := net.ResolveTCPAddr("tcp", *topicBrokerAddr)
	if err != nil {
		panic(err)
	}

	conn, err := net.DialTCP("tcp", nil, addr)
	if err != nil {
		panic(err)
	}

	client := server.NewClient(conn)

	resp, err := client.CreateTopic("cmd/createtopic", &protocol.CreateTopicRequest{
		Topic:             *topicTopic,
		NumPartitions:     *topicPartitions,
		ReplicationFactor: *topicReplicationFactor,
		ReplicaAssignment: nil,
		Configs:           nil,
	})

	if err != nil {
		panic(err)
	}

	for _, topicErrCode := range resp.TopicErrorCodes {
		msg := "ok"
		if topicErrCode.ErrorCode == 41 {
			msg = "err not controller"
		}
		fmt.Printf("create topic %s: %s\n", topicErrCode.Topic, msg)
	}

	return 0
}
<commit_msg>cli: improve logs<commit_after>package main

import (
	"fmt"
	"net"
	"os"
	"time"

	"github.com/tj/go-gracefully"
	"github.com/travisjeffery/jocko/broker"
	"github.com/travisjeffery/jocko/protocol"
	"github.com/travisjeffery/jocko/raft"
	"github.com/travisjeffery/jocko/serf"
	"github.com/travisjeffery/jocko/server"
	"github.com/travisjeffery/simplelog"
	"gopkg.in/alecthomas/kingpin.v2"
)

var (
	cli       = kingpin.New("jocko", "Jocko, Go implementation of Kafka")
	debugLogs = cli.Flag("debug", "Enable debug logs").Default("false").Bool()

	brokerCmd            = cli.Command("broker", "Run a Jocko broker")
	brokerCmdRaftAddr    = brokerCmd.Flag("raft-addr", "Address for Raft to bind and advertise on").Default("127.0.0.1:9093").String()
	brokerCmdLogDir      = brokerCmd.Flag("log-dir", "A comma separated list of directories under which to store log files").Default("/tmp/jocko").String()
	brokerCmdBrokerAddr  = brokerCmd.Flag("broker-addr", "Address for broker to bind on").Default("0.0.0.0:9092").String()
	brokerCmdSerfAddr    = brokerCmd.Flag("serf-addr", "Address for Serf to bind on").Default("0.0.0.0:9094").String()
	brokerCmdSerfMembers = brokerCmd.Flag("serf-members", "List of existing Serf members").Strings()
	brokerCmdBrokerID    = brokerCmd.Flag("id", "Broker ID").Int32()

	topic                  = cli.Command("topic", "Manage topics")
	topicBrokerAddr        = topic.Flag("broker-addr", "Address for Broker to bind on").Default("0.0.0.0:9092").String()
	topicTopic             = topic.Flag("topic", "Name of topic to create").String()
	topicPartitions        = topic.Flag("partitions", "Number of partitions").Default("1").Int32()
	topicReplicationFactor = topic.Flag("replication-factor", "Replication factor").Default("1").Int16()
)

func main() {
	logLevel := simplelog.INFO
	if *debugLogs {
		logLevel = simplelog.DEBUG
	}
	logger := simplelog.New(os.Stdout, logLevel, "jocko")

	switch kingpin.MustParse(cli.Parse(os.Args[1:])) {
	case brokerCmd.FullCommand():
		os.Exit(CmdBrokers(logger))

	case topic.FullCommand():
		os.Exit(CmdTopic(logger))
	}
}

func CmdBrokers(logger *simplelog.Logger) int {
	serf, err := serf.New(
		serf.Logger(logger),
		serf.Addr(*brokerCmdSerfAddr),
		serf.InitMembers(*brokerCmdSerfMembers),
	)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error starting serf: %s", err)
		os.Exit(1)
	}

	raft, err := raft.New(
		raft.Logger(logger),
		raft.DataDir(*brokerCmdLogDir),
		raft.Addr(*brokerCmdRaftAddr),
	)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error starting raft: %s", err)
		os.Exit(1)
	}

	store, err := broker.New(*brokerCmdBrokerID,
		broker.LogDir(*brokerCmdLogDir),
		broker.Logger(logger),
		broker.Addr(*brokerCmdBrokerAddr),
		broker.Serf(serf),
		broker.Raft(raft),
	)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error starting broker: %s\n", err)
		os.Exit(1)
	}
	srv := server.New(*brokerCmdBrokerAddr, store, logger)
	if err := srv.Start(); err != nil {
		fmt.Fprintf(os.Stderr, "Error starting server: %s\n", err)
		os.Exit(1)
	}

	defer srv.Close()

	gracefully.Timeout = 10 * time.Second
	gracefully.Shutdown()

	if err := store.Shutdown(); err != nil {
		fmt.Fprintf(os.Stderr, "Error shutting down store: %s\n", err)
		os.Exit(1)
	}

	return 0
}

func CmdTopic(logger *simplelog.Logger) int {
	fmt.Fprintf(os.Stdout, "Creating topic: %s", *topicTopic)

	addr, err := net.ResolveTCPAddr("tcp", *topicBrokerAddr)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error shutting down store: %s\n", err)
		os.Exit(1)
	}

	conn, err := net.DialTCP("tcp", nil, addr)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error connecting to broker: %s\n", err)
		os.Exit(1)
	}

	client := server.NewClient(conn)

	resp, err := client.CreateTopic("cmd/createtopic", &protocol.CreateTopicRequest{
		Topic:             *topicTopic,
		NumPartitions:     *topicPartitions,
		ReplicationFactor: *topicReplicationFactor,
		ReplicaAssignment: nil,
		Configs:           nil,
	})
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error with request to broker: %s\n", err)
		os.Exit(1)
	}

	for _, topicErrCode := range resp.TopicErrorCodes {
		if topicErrCode.ErrorCode != protocol.ErrNone.Code() {
			fmt.Fprintf(os.Stderr, "Error code: %s\n", topicErrCode.ErrorCode)
			os.Exit(1)
		}
	}

	fmt.Printf("Created topic: %s\n", *topicTopic)

	return 0
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"launchpad.net/gnuflag"
	"launchpad.net/juju-core/cmd"
	"launchpad.net/juju-core/log"
	"launchpad.net/juju-core/state"
	"launchpad.net/juju-core/worker"
	"launchpad.net/juju-core/worker/uniter"
	"launchpad.net/tomb"
	"time"
)

// UnitAgent is a cmd.Command responsible for running a unit agent.
type UnitAgent struct {
	tomb     tomb.Tomb
	Conf     AgentConf
	UnitName string
}

// Info returns usage information for the command.
func (a *UnitAgent) Info() *cmd.Info {
	return &cmd.Info{"unit", "", "run a juju unit agent", ""}
}

// Init initializes the command for running.
func (a *UnitAgent) Init(f *gnuflag.FlagSet, args []string) error {
	a.Conf.addFlags(f)
	f.StringVar(&a.UnitName, "unit-name", "", "name of the unit to run")
	if err := f.Parse(true, args); err != nil {
		return err
	}
	if a.UnitName == "" {
		return requiredError("unit-name")
	}
	if !state.IsUnitName(a.UnitName) {
		return fmt.Errorf(`--unit-name option expects "<service>/<n>" argument`)
	}
	return a.Conf.checkArgs(f.Args())
}

// Stop stops the unit agent.
func (a *UnitAgent) Stop() error {
	a.tomb.Kill(nil)
	return a.tomb.Wait()
}

// Run runs a unit agent.
func (a *UnitAgent) Run(ctx *cmd.Context) error {
	defer log.Printf("unit agent exiting")
	defer a.tomb.Done()
	for a.tomb.Err() == tomb.ErrStillAlive {
		err := a.runOnce()
		if ug, ok := err.(*UpgradeReadyError); ok {
			if err = ug.ChangeAgentTools(); err == nil {
				// Return and let upstart deal with the restart.
				return ug
			}
		}
		if err == worker.ErrDead {
			log.Printf("uniter: unit is dead")
			return nil
		}
		if err == nil {
			log.Printf("uniter: workers died with no error")
		} else {
			log.Printf("uniter: %v", err)
		}
		select {
		case <-a.tomb.Dying():
			a.tomb.Kill(err)
		case <-time.After(retryDelay):
			log.Printf("rerunning uniter")
		}
	}
	return a.tomb.Err()
}

// runOnce runs a uniter once.
func (a *UnitAgent) runOnce() error {
	st, err := state.Open(&a.Conf.StateInfo)
	if err != nil {
		return err
	}
	defer st.Close()
	unit, err := st.Unit(a.UnitName)
	if state.IsNotFound(err) || err == nil && unit.Life() == state.Dead {
		return worker.ErrDead
	}
	if err != nil {
		return err
	}
	return runTasks(a.tomb.Dying(),
		uniter.NewUniter(st, unit.Name(), a.Conf.DataDir),
		NewUpgrader(st, unit, a.Conf.DataDir),
	)
}
<commit_msg>cmd/jujud: make log msgs more consistent<commit_after>package main

import (
	"fmt"
	"launchpad.net/gnuflag"
	"launchpad.net/juju-core/cmd"
	"launchpad.net/juju-core/log"
	"launchpad.net/juju-core/state"
	"launchpad.net/juju-core/worker"
	"launchpad.net/juju-core/worker/uniter"
	"launchpad.net/tomb"
	"time"
)

// UnitAgent is a cmd.Command responsible for running a unit agent.
type UnitAgent struct {
	tomb     tomb.Tomb
	Conf     AgentConf
	UnitName string
}

// Info returns usage information for the command.
func (a *UnitAgent) Info() *cmd.Info {
	return &cmd.Info{"unit", "", "run a juju unit agent", ""}
}

// Init initializes the command for running.
func (a *UnitAgent) Init(f *gnuflag.FlagSet, args []string) error {
	a.Conf.addFlags(f)
	f.StringVar(&a.UnitName, "unit-name", "", "name of the unit to run")
	if err := f.Parse(true, args); err != nil {
		return err
	}
	if a.UnitName == "" {
		return requiredError("unit-name")
	}
	if !state.IsUnitName(a.UnitName) {
		return fmt.Errorf(`--unit-name option expects "<service>/<n>" argument`)
	}
	return a.Conf.checkArgs(f.Args())
}

// Stop stops the unit agent.
func (a *UnitAgent) Stop() error {
	a.tomb.Kill(nil)
	return a.tomb.Wait()
}

// Run runs a unit agent.
func (a *UnitAgent) Run(ctx *cmd.Context) error {
	defer log.Printf("uniter: unit agent exiting")
	defer a.tomb.Done()
	for a.tomb.Err() == tomb.ErrStillAlive {
		err := a.runOnce()
		if ug, ok := err.(*UpgradeReadyError); ok {
			if err = ug.ChangeAgentTools(); err == nil {
				// Return and let upstart deal with the restart.
				return ug
			}
		}
		if err == worker.ErrDead {
			log.Printf("uniter: unit is dead")
			return nil
		}
		if err == nil {
			log.Printf("uniter: workers died with no error")
		} else {
			log.Printf("uniter: %v", err)
		}
		select {
		case <-a.tomb.Dying():
			a.tomb.Kill(err)
		case <-time.After(retryDelay):
			log.Printf("uniter: rerunning uniter")
		}
	}
	return a.tomb.Err()
}

// runOnce runs a uniter once.
func (a *UnitAgent) runOnce() error {
	st, err := state.Open(&a.Conf.StateInfo)
	if err != nil {
		return err
	}
	defer st.Close()
	unit, err := st.Unit(a.UnitName)
	if state.IsNotFound(err) || err == nil && unit.Life() == state.Dead {
		return worker.ErrDead
	}
	if err != nil {
		return err
	}
	return runTasks(a.tomb.Dying(),
		uniter.NewUniter(st, unit.Name(), a.Conf.DataDir),
		NewUpgrader(st, unit, a.Conf.DataDir),
	)
}
<|endoftext|>


<commit_before>package main

import (
	"os"
	"fmt"
)

func main() {
	for _, s := range os.Args[1:] {
		fi, _ := os.Stat(s)
		mt := fi.ModTime()
		fmt.Printf("%d %s", mt, s)
	}
}
<commit_msg>mtime: Check for error return from Stat<commit_after>package main

import (
	"fmt"
	"os"
)

func main() {
	for _, s := range os.Args[1:] {
		fi, err := os.Stat(s)
		if err != nil {
			fmt.Fprintln(os.Stderr, err.Error())
		}
		mt := fi.ModTime()
		fmt.Printf("%d %s", mt, s)
	}
}
<|endoftext|>


<commit_before>// Copyright 2015 Reborndb Org. All Rights Reserved.
// Licensed under the MIT (MIT-LICENSE.txt) license.

package main

import (
	"fmt"
	"net/http"
	_ "net/http/pprof"
	"path"
	"runtime"
	"strconv"

	"github.com/reborndb/reborn/pkg/proxy/router"
	"github.com/reborndb/reborn/pkg/utils"

	"github.com/docopt/docopt-go"
	log "github.com/ngaut/logging"
)

var (
	cpus       = 2
	addr       = ":9000"
	httpAddr   = ":9001"
	proxyID    = ""
	configFile = "config.ini"
)

var usage = `usage: proxy [options]

options:
   -c <config_file>               set config file
   -L <log_file>                  set output log file, default is stdout
   --log-level=<loglevel>         set log level: info, warn, error, debug [default: info]
   --cpu=<cpu_num>                num of cpu cores that proxy can use
   --addr=<proxy_listen_addr>     proxy listen address, example: 0.0.0.0:9000
   --id=<proxy_id>                proxy id, global unique, can not be empty
   --http-addr=<debug_http_addr>  debug vars http server
`

var banner string = `
    ____       __                     ____  ____
   / __ \___  / /_  ____  _________  / __ \/ __ )
  / /_/ / _ \/ __ \/ __ \/ ___/ __ \/ / / / __  |
 / _, _/  __/ /_/ / /_/ / /  / / / / /_/ / /_/ /
/_/ |_|\___/_.___/\____/_/  /_/ /_/_____/_____/

`

func handleSetLogLevel(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	level := r.Form.Get("level")
	log.SetLevelByString(level)
	log.Info("set log level to", level)
}

func main() {
	fmt.Print(banner)
	log.SetLevelByString("info")

	args, err := docopt.Parse(usage, nil, true, "reborn proxy v0.1", true)
	if err != nil {
		log.Error(err)
	}

	// set config file
	if args["-c"] != nil {
		configFile = args["-c"].(string)
	}

	// set output log file
	if args["-L"] != nil {
		log.SetOutputByName(args["-L"].(string))
	}

	// set log level
	if args["--log-level"] != nil {
		log.SetLevelByString(args["--log-level"].(string))
	}

	// set cpu
	if args["--cpu"] != nil {
		cpus, err = strconv.Atoi(args["--cpu"].(string))
		if err != nil {
			log.Fatal(err)
		}
	}

	// set addr
	if args["--addr"] != nil {
		addr = args["--addr"].(string)
	}

	// set http addr
	if args["--http-addr"] != nil {
		httpAddr = args["--http-addr"].(string)
	}

	if args["--id"] != nil {
		proxyID = args["--id"].(string)
	} else {
		log.Fatalf("invalid empty proxy id")
	}

	dumppath := utils.GetExecutorPath()

	log.Info("dump file path:", dumppath)
	log.CrashLog(path.Join(dumppath, "reborn-proxy.dump"))

	router.CheckUlimit(1024)
	runtime.GOMAXPROCS(cpus)

	http.HandleFunc("/setloglevel", handleSetLogLevel)
	go http.ListenAndServe(httpAddr, nil)
	log.Info("running on ", addr)
	conf, err := router.LoadConf(configFile)
	if err != nil {
		log.Fatal(err)
	}
	s := router.NewServer(addr, httpAddr, proxyID, conf)
	s.Run()
	log.Warning("exit")
}
<commit_msg>support dump-path cmd flag<commit_after>// Copyright 2015 Reborndb Org. All Rights Reserved.
// Licensed under the MIT (MIT-LICENSE.txt) license.

package main

import (
	"fmt"
	"net/http"
	_ "net/http/pprof"
	"path"
	"runtime"
	"strconv"

	"github.com/reborndb/reborn/pkg/proxy/router"
	"github.com/reborndb/reborn/pkg/utils"

	"github.com/docopt/docopt-go"
	log "github.com/ngaut/logging"
)

var (
	cpus       = 2
	addr       = ":9000"
	httpAddr   = ":9001"
	proxyID    = ""
	configFile = "config.ini"
)

var usage = `usage: proxy [options]

options:
   -c <config_file>               set config file
   -L <log_file>                  set output log file, default is stdout
   --log-level=<loglevel>         set log level: info, warn, error, debug [default: info]
   --cpu=<cpu_num>                num of cpu cores that proxy can use
   --addr=<proxy_listen_addr>     proxy listen address, example: 0.0.0.0:9000
   --id=<proxy_id>                proxy id, global unique, can not be empty
   --http-addr=<debug_http_addr>  debug vars http server
   --dump-path=<path>             dump path to log crash error
`

var banner string = `
    ____       __                     ____  ____
   / __ \___  / /_  ____  _________  / __ \/ __ )
  / /_/ / _ \/ __ \/ __ \/ ___/ __ \/ / / / __  |
 / _, _/  __/ /_/ / /_/ / /  / / / / /_/ / /_/ /
/_/ |_|\___/_.___/\____/_/  /_/ /_/_____/_____/

`

func handleSetLogLevel(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	level := r.Form.Get("level")
	log.SetLevelByString(level)
	log.Info("set log level to", level)
}

func main() {
	fmt.Print(banner)
	log.SetLevelByString("info")

	args, err := docopt.Parse(usage, nil, true, "reborn proxy v0.1", true)
	if err != nil {
		log.Error(err)
	}

	// set config file
	if args["-c"] != nil {
		configFile = args["-c"].(string)
	}

	// set output log file
	if args["-L"] != nil {
		log.SetOutputByName(args["-L"].(string))
	}

	// set log level
	if args["--log-level"] != nil {
		log.SetLevelByString(args["--log-level"].(string))
	}

	// set cpu
	if args["--cpu"] != nil {
		cpus, err = strconv.Atoi(args["--cpu"].(string))
		if err != nil {
			log.Fatal(err)
		}
	}

	// set addr
	if args["--addr"] != nil {
		addr = args["--addr"].(string)
	}

	// set http addr
	if args["--http-addr"] != nil {
		httpAddr = args["--http-addr"].(string)
	}

	if args["--id"] != nil {
		proxyID = args["--id"].(string)
	} else {
		log.Fatalf("invalid empty proxy id")
	}

	dumppath := utils.GetExecutorPath()
	if args["--dump-path"] != nil {
		dumppath = args["--dump-path"].(string)
	}

	log.Info("dump file path:", dumppath)
	log.CrashLog(path.Join(dumppath, "reborn-proxy.dump"))

	router.CheckUlimit(1024)
	runtime.GOMAXPROCS(cpus)

	http.HandleFunc("/setloglevel", handleSetLogLevel)
	go http.ListenAndServe(httpAddr, nil)
	log.Info("running on ", addr)
	conf, err := router.LoadConf(configFile)
	if err != nil {
		log.Fatal(err)
	}
	s := router.NewServer(addr, httpAddr, proxyID, conf)
	s.Run()
	log.Warning("exit")
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"log"
	"os"
	"os/signal"
	"syscall"
	"time"

	"github.com/VojtechVitek/rerun"
)

type argType int

const (
	argNone argType = iota
	argWatch
	argIgnore
	argRun
)

func main() {
	watcher, err := rerun.NewWatcher()
	if err != nil {
		log.Fatal(err)
	}

	args := os.Args[1:]
	mode := argNone

	// Parse command line arguments.
	// -watch dirs ...
	// -ignore dirs ...
	// -run command ...
loop:
	for i, arg := range args {
		switch mode {
		case argNone, argWatch, argIgnore:
			switch arg {
			case "-watch":
				mode = argWatch
				continue
			case "-ignore":
				mode = argIgnore
				continue
			case "-run":
				mode = argRun
				continue
			}
		}

		switch mode {
		case argWatch:
			watcher.Add(arg)
		case argIgnore:
			watcher.Ignore(arg)
		case argRun:
			args = args[i:]
			break loop
		default:
			break loop
		}
	}

	if mode == argNone {
		log.Fatal("TODO: interactive mode")
	}
	// yay
	cmd, err := rerun.StartCommand(args...)
	if err != nil {
		log.Fatal(err)
	}
	defer cmd.Kill()
	fmt.Printf("\033c%v\n", cmd)

	sig := make(chan os.Signal, 1)
	signal.Notify(sig, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
	go func() {
		<-sig
		cmd.Kill()
		watcher.Close()
		os.Exit(1)
	}()

	for changeSet := range watcher.Watch(200 * time.Millisecond) {
		cmd.Kill()

		if changeSet.Error != nil {
			log.Fatal(err)
		}

		plural := ""
		if len(changeSet.Files) > 1 {
			plural = "s"
		}
		fmt.Printf("\033c\033[32m# %v file%v changed.\033[0m\n%v\n", len(changeSet.Files), plural, cmd)

		if err := cmd.Start(); err != nil {
			fmt.Printf("ERROR: %v\n", err)
		}
	}

	watcher.Close()
}
<commit_msg>Defer watcher.Close()<commit_after>package main

import (
	"fmt"
	"log"
	"os"
	"os/signal"
	"syscall"
	"time"

	"github.com/VojtechVitek/rerun"
)

type argType int

const (
	argNone argType = iota
	argWatch
	argIgnore
	argRun
)

func main() {
	watcher, err := rerun.NewWatcher()
	if err != nil {
		log.Fatal(err)
	}
	defer watcher.Close()

	args := os.Args[1:]
	mode := argNone

	// Parse command line arguments.
	// -watch dirs ...
	// -ignore dirs ...
	// -run command ...
loop:
	for i, arg := range args {
		switch mode {
		case argNone, argWatch, argIgnore:
			switch arg {
			case "-watch":
				mode = argWatch
				continue
			case "-ignore":
				mode = argIgnore
				continue
			case "-run":
				mode = argRun
				continue
			}
		}

		switch mode {
		case argWatch:
			watcher.Add(arg)
		case argIgnore:
			watcher.Ignore(arg)
		case argRun:
			args = args[i:]
			break loop
		default:
			break loop
		}
	}

	if mode == argNone {
		log.Fatal("TODO: interactive mode")
	}

	cmd, err := rerun.StartCommand(args...)
	if err != nil {
		log.Fatal(err)
	}
	defer cmd.Kill()
	fmt.Printf("\033c%v\n", cmd)

	sig := make(chan os.Signal, 1)
	signal.Notify(sig, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
	go func() {
		<-sig
		cmd.Kill()
		watcher.Close()
		os.Exit(1)
	}()

	for changeSet := range watcher.Watch(200 * time.Millisecond) {
		cmd.Kill()

		if changeSet.Error != nil {
			log.Fatal(err)
		}

		plural := ""
		if len(changeSet.Files) > 1 {
			plural = "s"
		}
		fmt.Printf("\033c\033[32m# %v file%v changed.\033[0m\n%v\n", len(changeSet.Files), plural, cmd)

		if err := cmd.Start(); err != nil {
			fmt.Printf("ERROR: %v\n", err)
		}
	}
}
<|endoftext|>


<commit_before>// Copyright 2015, David Howden
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"encoding/json"

	"tchaik.com/index"
	"tchaik.com/index/attr"
)

// Group is a wrapper type for an index.Group which implements MarshalJSON
// for transmitting groups.
type Group struct {
	index.Group
	Key index.Key
}

// MarshalJSON implements json.Marshaler.
func (g *Group) MarshalJSON() ([]byte, error) {
	return json.Marshal(g.build())
}

// stringSliceEqual is a function used to compare two interface{} types which are assumed
// to be of type []string (or interface{}(nil)).
func stringSliceEqual(x, y interface{}) bool {
	// Annoyingly we have to cater for zero values from map[string]interface{}
	// which don't have the correct type wrapping the nil.
	if x == nil || y == nil {
		return x == nil && y == nil
	}
	xs := x.([]string) // NB: panics here are acceptable: should not be called on a non-'Strings' field.
	ys := y.([]string)
	if len(xs) != len(ys) {
		return false
	}
	for i, xss := range xs {
		if ys[i] != xss {
			return false
		}
	}
	return true
}

type groupField struct {
	index.Group

	collection index.Collection
}

func (g groupField) Field(field string) interface{} {
	if field == "AlbumArtist" || field == "Artist" {
		if stringSliceEqual(g.Group.Field(field), g.collection.Field(field)) {
			return nil
		}
	}
	return g.Group.Field(field)
}

func buildCollection(h group, c index.Collection) group {
	for _, k := range c.Keys() {
		g := c.Get(k)
		g = index.FirstTrackAttr(attr.Strings("AlbumArtist"), g)
		g = index.CommonGroupAttr([]attr.Interface{attr.Strings("Artist")}, g)

		g = groupField{
			Group:      g,
			collection: c,
		}

		h.Groups = append(h.Groups, group{
			Name:        g.Name(),
			Key:         k,
			AlbumArtist: g.Field("AlbumArtist"),
			Artist:      g.Field("Artist"),
		})
	}
	return h
}

// Track is a simple wrapper for an index.Track.
type Track struct {
	index.Track

	group index.Group
}

// GetString implements index.Track.
func (t *Track) GetString(field string) string {
	// Always return the default value for these fields.
	if field == "ID" || field == "Name" {
		return t.Track.GetString(field)
	}

	if t.group.Field(field) != nil {
		return ""
	}
	return t.Track.GetString(field)
}

// GetStrings implements index.Track.
func (t *Track) GetStrings(field string) []string {
	if t.group.Field(field) != nil {
		return nil
	}
	return t.Track.GetStrings(field)
}

// GetInt implements index.Track.
func (t *Track) GetInt(field string) int {
	if field == "TotalTime" {
		return t.Track.GetInt(field)
	}

	if t.group.Field(field) != nil {
		return 0
	}
	return t.Track.GetInt(field)
}

func (t *Track) MarshalJSON() ([]byte, error) {
	return json.Marshal(struct {
		ID          string   `json:"id,omitempty"`
		Name        string   `json:"name,omitempty"`
		Album       string   `json:"album,omitempty"`
		Artist      []string `json:"artist,omitempty"`
		AlbumArtist []string `json:"albumArtist,omitempty"`
		Composer    []string `json:"composer,omitempty"`
		Kind        string   `json:"kind,omitempty"`
		Year        int      `json:"year,omitempty"`
		DiscNumber  int      `json:"discNumber,omitempty"`
		TotalTime   int      `json:"totalTime,omitempty"`
		BitRate     int      `json:"bitRate,omitempty"`
	}{
		ID:          t.GetString("ID"),
		Name:        t.GetString("Name"),
		TotalTime:   t.GetInt("TotalTime"),
		Artist:      t.GetStrings("Artist"),
		AlbumArtist: t.GetStrings("AlbumArtist"),
		Composer:    t.GetStrings("Composer"),
		Album:       t.GetString("Album"),
		Kind:        t.GetString("Kind"),
		Year:        t.GetInt("Year"),
		DiscNumber:  t.GetInt("DiscNumber"),
		BitRate:     t.GetInt("BitRate"),
	})
}

type group struct {
	Name        string        `json:"name"`
	Key         index.Key     `json:"key"`
	TotalTime   interface{}   `json:"totalTime,omitempty"`
	Artist      interface{}   `json:"artist,omitempty"`
	AlbumArtist interface{}   `json:"albumArtist,omitempty"`
	Composer    interface{}   `json:"composer,omitempty"`
	BitRate     interface{}   `json:"bitRate,omitempty"`
	DiscNumber  interface{}   `json:"discNumber,omitempty"`
	ListStyle   interface{}   `json:"listStyle,omitempty"`
	ID          interface{}   `json:"id,omitempty"`
	Year        interface{}   `json:"year,omitempty"`
	Kind        interface{}   `json:"kind,omitempty"`
	Favourite   interface{}   `json:"favourite,omitempty"`
	Checklist   interface{}   `json:"checklist,omitempty"`
	Groups      []group       `json:"groups,omitempty"`
	Tracks      []index.Track `json:"tracks,omitempty"`
}

func (g *Group) build() group {
	h := group{
		Name:        g.Name(),
		Key:         g.Key,
		TotalTime:   g.Field("TotalTime"),
		Artist:      g.Field("Artist"),
		AlbumArtist: g.Field("AlbumArtist"),
		Composer:    g.Field("Composer"),
		Year:        g.Field("Year"),
		BitRate:     g.Field("BitRate"),
		DiscNumber:  g.Field("DiscNumber"),
		ListStyle:   g.Field("ListStyle"),
		Kind:        g.Field("Kind"),
		ID:          g.Field("ID"),
		Favourite:   g.Field("Favourite"),
		Checklist:   g.Field("Checklist"),
	}

	if c, ok := g.Group.(index.Collection); ok {
		return buildCollection(h, c)
	}

	for _, t := range g.Tracks() {
		h.Tracks = append(h.Tracks, &Track{
			Track: t,
			group: g,
		})
	}
	return h
}

type rootCollection struct {
	index.Collection
}

func (r *rootCollection) Get(k index.Key) index.Group {
	g := r.Collection.Get(k)
	if g == nil {
		return g
	}

	index.Sort(g.Tracks(), index.MultiSort(index.SortByString("Kind"), index.SortByInt("DiscNumber"), index.SortByInt("TrackNumber")))
	g = index.Transform(g, index.SplitList("Artist", "AlbumArtist", "Composer"))
	g = index.Transform(g, index.TrimTrackNumPrefix)
	c := index.Collect(g, index.ByPrefix("Name"))
	g = index.SubTransform(c, index.TrimEnumPrefix)
	g = index.SumGroupIntAttr("TotalTime", g)
	commonFields := []attr.Interface{
		attr.String("Album"),
		attr.Strings("Artist"),
		attr.Strings("AlbumArtist"),
		attr.Strings("Composer"),
		attr.String("Kind"),
		attr.Int("Year"),
		attr.Int("BitRate"),
		attr.Int("DiscNumber"),
	}
	g = index.CommonGroupAttr(commonFields, g)
	g = index.RemoveEmptyCollections(g)
	return g
}
<commit_msg>Remove unnecessary build function.<commit_after>// Copyright 2015, David Howden
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"encoding/json"

	"tchaik.com/index"
	"tchaik.com/index/attr"
)

// Group is a wrapper type for an index.Group which implements MarshalJSON
// for transmitting groups.
type Group struct {
	index.Group
	Key index.Key
}

// MarshalJSON implements json.Marshaler.
func (g *Group) MarshalJSON() ([]byte, error) {
	h := group{
		Name:        g.Name(),
		Key:         g.Key,
		TotalTime:   g.Field("TotalTime"),
		Artist:      g.Field("Artist"),
		AlbumArtist: g.Field("AlbumArtist"),
		Composer:    g.Field("Composer"),
		Year:        g.Field("Year"),
		BitRate:     g.Field("BitRate"),
		DiscNumber:  g.Field("DiscNumber"),
		ListStyle:   g.Field("ListStyle"),
		Kind:        g.Field("Kind"),
		ID:          g.Field("ID"),
		Favourite:   g.Field("Favourite"),
		Checklist:   g.Field("Checklist"),
	}

	if c, ok := g.Group.(index.Collection); ok {
		return json.Marshal(buildCollection(h, c))
	}

	for _, t := range g.Tracks() {
		h.Tracks = append(h.Tracks, &Track{
			Track: t,
			group: g,
		})
	}
	return json.Marshal(h)
}

// stringSliceEqual is a function used to compare two interface{} types which are assumed
// to be of type []string (or interface{}(nil)).
func stringSliceEqual(x, y interface{}) bool {
	// Annoyingly we have to cater for zero values from map[string]interface{}
	// which don't have the correct type wrapping the nil.
	if x == nil || y == nil {
		return x == nil && y == nil
	}
	xs := x.([]string) // NB: panics here are acceptable: should not be called on a non-'Strings' field.
	ys := y.([]string)
	if len(xs) != len(ys) {
		return false
	}
	for i, xss := range xs {
		if ys[i] != xss {
			return false
		}
	}
	return true
}

type groupField struct {
	index.Group

	collection index.Collection
}

func (g groupField) Field(field string) interface{} {
	if field == "AlbumArtist" || field == "Artist" {
		if stringSliceEqual(g.Group.Field(field), g.collection.Field(field)) {
			return nil
		}
	}
	return g.Group.Field(field)
}

func buildCollection(h group, c index.Collection) group {
	for _, k := range c.Keys() {
		g := c.Get(k)
		g = index.FirstTrackAttr(attr.Strings("AlbumArtist"), g)
		g = index.CommonGroupAttr([]attr.Interface{attr.Strings("Artist")}, g)

		g = groupField{
			Group:      g,
			collection: c,
		}

		h.Groups = append(h.Groups, group{
			Name:        g.Name(),
			Key:         k,
			AlbumArtist: g.Field("AlbumArtist"),
			Artist:      g.Field("Artist"),
		})
	}
	return h
}

// Track is a simple wrapper for an index.Track.
type Track struct {
	index.Track

	group index.Group
}

// GetString implements index.Track.
func (t *Track) GetString(field string) string {
	// Always return the default value for these fields.
	if field == "ID" || field == "Name" {
		return t.Track.GetString(field)
	}

	if t.group.Field(field) != nil {
		return ""
	}
	return t.Track.GetString(field)
}

// GetStrings implements index.Track.
func (t *Track) GetStrings(field string) []string {
	if t.group.Field(field) != nil {
		return nil
	}
	return t.Track.GetStrings(field)
}

// GetInt implements index.Track.
func (t *Track) GetInt(field string) int {
	if field == "TotalTime" {
		return t.Track.GetInt(field)
	}

	if t.group.Field(field) != nil {
		return 0
	}
	return t.Track.GetInt(field)
}

func (t *Track) MarshalJSON() ([]byte, error) {
	return json.Marshal(struct {
		ID          string   `json:"id,omitempty"`
		Name        string   `json:"name,omitempty"`
		Album       string   `json:"album,omitempty"`
		Artist      []string `json:"artist,omitempty"`
		AlbumArtist []string `json:"albumArtist,omitempty"`
		Composer    []string `json:"composer,omitempty"`
		Kind        string   `json:"kind,omitempty"`
		Year        int      `json:"year,omitempty"`
		DiscNumber  int      `json:"discNumber,omitempty"`
		TotalTime   int      `json:"totalTime,omitempty"`
		BitRate     int      `json:"bitRate,omitempty"`
	}{
		ID:          t.GetString("ID"),
		Name:        t.GetString("Name"),
		TotalTime:   t.GetInt("TotalTime"),
		Artist:      t.GetStrings("Artist"),
		AlbumArtist: t.GetStrings("AlbumArtist"),
		Composer:    t.GetStrings("Composer"),
		Album:       t.GetString("Album"),
		Kind:        t.GetString("Kind"),
		Year:        t.GetInt("Year"),
		DiscNumber:  t.GetInt("DiscNumber"),
		BitRate:     t.GetInt("BitRate"),
	})
}

type group struct {
	Name        string        `json:"name"`
	Key         index.Key     `json:"key"`
	TotalTime   interface{}   `json:"totalTime,omitempty"`
	Artist      interface{}   `json:"artist,omitempty"`
	AlbumArtist interface{}   `json:"albumArtist,omitempty"`
	Composer    interface{}   `json:"composer,omitempty"`
	BitRate     interface{}   `json:"bitRate,omitempty"`
	DiscNumber  interface{}   `json:"discNumber,omitempty"`
	ListStyle   interface{}   `json:"listStyle,omitempty"`
	ID          interface{}   `json:"id,omitempty"`
	Year        interface{}   `json:"year,omitempty"`
	Kind        interface{}   `json:"kind,omitempty"`
	Favourite   interface{}   `json:"favourite,omitempty"`
	Checklist   interface{}   `json:"checklist,omitempty"`
	Groups      []group       `json:"groups,omitempty"`
	Tracks      []index.Track `json:"tracks,omitempty"`
}

type rootCollection struct {
	index.Collection
}

func (r *rootCollection) Get(k index.Key) index.Group {
	g := r.Collection.Get(k)
	if g == nil {
		return g
	}

	index.Sort(g.Tracks(), index.MultiSort(index.SortByString("Kind"), index.SortByInt("DiscNumber"), index.SortByInt("TrackNumber")))
	g = index.Transform(g, index.SplitList("Artist", "AlbumArtist", "Composer"))
	g = index.Transform(g, index.TrimTrackNumPrefix)
	c := index.Collect(g, index.ByPrefix("Name"))
	g = index.SubTransform(c, index.TrimEnumPrefix)
	g = index.SumGroupIntAttr("TotalTime", g)
	commonFields := []attr.Interface{
		attr.String("Album"),
		attr.Strings("Artist"),
		attr.Strings("AlbumArtist"),
		attr.Strings("Composer"),
		attr.String("Kind"),
		attr.Int("Year"),
		attr.Int("BitRate"),
		attr.Int("DiscNumber"),
	}
	g = index.CommonGroupAttr(commonFields, g)
	g = index.RemoveEmptyCollections(g)
	return g
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
	"time"

	"github.com/gonuts/commander"
	"github.com/gonuts/flag"
	"github.com/hwaf/gas"
)

func hwaf_make_cmd_self_bdist() *commander.Command {
	cmd := &commander.Command{
		Run:       hwaf_run_cmd_self_bdist,
		UsageLine: "bdist [options]",
		Short:     "create a binary distribution of hwaf itself",
		Long: `
self bdist creates a binary distribution of hwaf itself.

ex:
 $ hwaf self bdist
 $ hwaf self bdist -version=20130101
`,
		Flag: *flag.NewFlagSet("hwaf-self-bdist", flag.ExitOnError),
	}
	cmd.Flag.Bool("v", false, "enable verbose output")
	cmd.Flag.String("version", "", "version of the binary distribution (default: 'time now')")

	return cmd
}

func hwaf_run_cmd_self_bdist(cmd *commander.Command, args []string) {
	var err error

	n := "hwaf-self-" + cmd.Name()

	switch len(args) {
	case 0:
		// ok
	default:
		err = fmt.Errorf("%s: does NOT take any argument", n)
		handle_err(err)
	}

	verbose := cmd.Flag.Lookup("v").Value.Get().(bool)

	bdist_name := "hwaf"
	bdist_vers := cmd.Flag.Lookup("version").Value.Get().(string)
	bdist_variant := fmt.Sprintf("%s-%s", runtime.GOOS, runtime.GOARCH)

	if bdist_vers == "" {
		bdist_vers = time.Now().Format("20060102")
	}

	dirname := fmt.Sprintf("%s-%s-%s", bdist_name, bdist_vers, bdist_variant)
	fname := dirname + ".tar.gz"

	if verbose {
		fmt.Printf("%s [%s]...\n", n, fname)
	}

	tmpdir, err := ioutil.TempDir("", "hwaf-self-bdist-")
	handle_err(err)
	defer os.RemoveAll(tmpdir)
	//fmt.Printf(">>> [%s]\n", tmpdir)

	//
	top := filepath.Join(tmpdir, dirname)
	// create hierarchy of dirs for bdist
	for _, dir := range []string{
		"bin",
		"share",
		filepath.Join("share", "hwaf"),
	} {
		err = os.MkdirAll(filepath.Join(top, dir), 0755)
		handle_err(err)
	}

	// add hep-waftools cache
	hwaf_dir, err := gas.Abs("github.com/hwaf/hwaf")
	handle_err(err)

	src_hwaf_tools := filepath.Join(hwaf_dir, "py-hwaftools")
	hwaf_tools := filepath.Join(top, "share", "hwaf", "tools")

	err = copytree(hwaf_tools, src_hwaf_tools)
	handle_err(err)

	// remove git stuff
	err = os.RemoveAll(filepath.Join(hwaf_tools, ".git"))
	handle_err(err)

	// add share/hwaf/hwaf.conf
	err = ioutil.WriteFile(
		filepath.Join(top, "share", "hwaf", "hwaf.conf"),
		[]byte(`# hwaf config file
[hwaf]

## EOF ##
`),
		0644,
	)
	handle_err(err)

	// temporary GOPATH - install go-deps
	gopath := filepath.Join(tmpdir, "gocode")
	err = os.MkdirAll(gopath, 0755)
	handle_err(err)

	orig_gopath := os.Getenv("GOPATH")
	err = os.Setenv("GOPATH", gopath)
	handle_err(err)
	defer os.Setenv("GOPATH", orig_gopath)

	for _, gopkg := range []string{
		"github.com/hwaf/hwaf",
		"github.com/hwaf/hwaf-cmt2yml",
		"github.com/hwaf/hwaf-rcore2yml",
	} {
		goget := exec.Command("go", "get", "-v", gopkg)
		goget.Dir = gopath
		if verbose {
			goget.Stdout = os.Stdout
			goget.Stderr = os.Stderr
		}
		err = goget.Run()
		handle_err(err)

		// install under /bin
		dst_fname := filepath.Join(top, "bin", filepath.Base(gopkg))
		dst, err := os.OpenFile(dst_fname, os.O_WRONLY|os.O_CREATE, 0755)
		handle_err(err)
		defer func(dst *os.File) {
			err := dst.Sync()
			handle_err(err)
			err = dst.Close()
			handle_err(err)
		}(dst)

		src_fname := filepath.Join(gopath, "bin", filepath.Base(gopkg))
		if !path_exists(src_fname) {
			// maybe a cross-compilation ?
			src_fname = filepath.Join(gopath, "bin", runtime.GOOS+"_"+runtime.GOARCH, filepath.Base(gopkg))
		}
		src, err := os.Open(src_fname)
		handle_err(err)
		defer func(src *os.File) {
			err := src.Close()
			handle_err(err)
		}(src)

		_, err = io.Copy(dst, src)
		handle_err(err)
	}

	// add waf-bin
	waf_fname := filepath.Join(top, "bin", "waf")
	if path_exists(waf_fname) {
		err = os.Remove(waf_fname)
		handle_err(err)
	}
	waf_dst, err := os.OpenFile(waf_fname, os.O_WRONLY|os.O_CREATE, 0777)
	handle_err(err)
	defer func() {
		err = waf_dst.Sync()
		handle_err(err)
		err = waf_dst.Close()
		handle_err(err)
	}()

	waf_src, err := os.Open(filepath.Join(
		gopath, "src", "github.com", "hwaf", "hwaf", "waf"),
	)
	handle_err(err)
	defer waf_src.Close()
	_, err = io.Copy(waf_dst, waf_src)
	handle_err(err)

	const bq = "`"
	// add setup.sh
	setup_fname, err := os.Create(filepath.Join(top, "setup-hwaf.sh"))
	handle_err(err)
	defer setup_fname.Close()
	_, err = fmt.Fprintf(setup_fname, `#!/bin/sh

if [ "x${BASH_ARGV[0]}" = "x" ]; then
    ## assume zsh
    SOURCE="$0"
else
    SOURCE="${BASH_SOURCE[0]}"
fi

DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
echo ":: adding [$DIR/bin] to PATH"
export PATH=$DIR/bin:$PATH
## EOF
`)
	handle_err(err)

	handle_err(setup_fname.Sync())

	// add setup.csh
	csetup_fname, err := os.Create(filepath.Join(top, "setup-hwaf.csh"))
	handle_err(err)
	defer csetup_fname.Close()
	_, err = fmt.Fprintf(csetup_fname, `#!/bin/csh
# Absolute path to this script
set SCRIPT=%sreadlink -f "$0"%s
# Absolute path this script is in
set SCRIPTPATH=%sdirname "$SCRIPT"%s
echo ":: adding [$SCRIPTPATH/bin] to PATH"
setenv PATH $SCRIPTPATH/bin:$PATH

## EOF
`, bq, bq, bq, bq)
	handle_err(err)
	handle_err(csetup_fname.Sync())

	pwd, err := os.Getwd()
	handle_err(err)

	// package everything up
	err = _tar_gz(filepath.Join(pwd, fname), top)
	handle_err(err)

	if verbose {
		fmt.Printf("%s [%s]... [ok]\n", n, fname)
	}
}

// EOF
<commit_msg>self-bdist: add hwaf-gen-extpackdist to tarball<commit_after>package main

import (
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
	"time"

	"github.com/gonuts/commander"
	"github.com/gonuts/flag"
	"github.com/hwaf/gas"
)

func hwaf_make_cmd_self_bdist() *commander.Command {
	cmd := &commander.Command{
		Run:       hwaf_run_cmd_self_bdist,
		UsageLine: "bdist [options]",
		Short:     "create a binary distribution of hwaf itself",
		Long: `
self bdist creates a binary distribution of hwaf itself.

ex:
 $ hwaf self bdist
 $ hwaf self bdist -version=20130101
`,
		Flag: *flag.NewFlagSet("hwaf-self-bdist", flag.ExitOnError),
	}
	cmd.Flag.Bool("v", false, "enable verbose output")
	cmd.Flag.String("version", "", "version of the binary distribution (default: 'time now')")

	return cmd
}

func hwaf_run_cmd_self_bdist(cmd *commander.Command, args []string) {
	var err error

	n := "hwaf-self-" + cmd.Name()

	switch len(args) {
	case 0:
		// ok
	default:
		err = fmt.Errorf("%s: does NOT take any argument", n)
		handle_err(err)
	}

	verbose := cmd.Flag.Lookup("v").Value.Get().(bool)

	bdist_name := "hwaf"
	bdist_vers := cmd.Flag.Lookup("version").Value.Get().(string)
	bdist_variant := fmt.Sprintf("%s-%s", runtime.GOOS, runtime.GOARCH)

	if bdist_vers == "" {
		bdist_vers = time.Now().Format("20060102")
	}

	dirname := fmt.Sprintf("%s-%s-%s", bdist_name, bdist_vers, bdist_variant)
	fname := dirname + ".tar.gz"

	if verbose {
		fmt.Printf("%s [%s]...\n", n, fname)
	}

	tmpdir, err := ioutil.TempDir("", "hwaf-self-bdist-")
	handle_err(err)
	defer os.RemoveAll(tmpdir)
	//fmt.Printf(">>> [%s]\n", tmpdir)

	//
	top := filepath.Join(tmpdir, dirname)
	// create hierarchy of dirs for bdist
	for _, dir := range []string{
		"bin",
		"share",
		filepath.Join("share", "hwaf"),
	} {
		err = os.MkdirAll(filepath.Join(top, dir), 0755)
		handle_err(err)
	}

	// add hep-waftools cache
	hwaf_dir, err := gas.Abs("github.com/hwaf/hwaf")
	handle_err(err)

	src_hwaf_tools := filepath.Join(hwaf_dir, "py-hwaftools")
	hwaf_tools := filepath.Join(top, "share", "hwaf", "tools")

	err = copytree(hwaf_tools, src_hwaf_tools)
	handle_err(err)

	// remove git stuff
	err = os.RemoveAll(filepath.Join(hwaf_tools, ".git"))
	handle_err(err)

	// add share/hwaf/hwaf.conf
	err = ioutil.WriteFile(
		filepath.Join(top, "share", "hwaf", "hwaf.conf"),
		[]byte(`# hwaf config file
[hwaf]

## EOF ##
`),
		0644,
	)
	handle_err(err)

	// temporary GOPATH - install go-deps
	gopath := filepath.Join(tmpdir, "gocode")
	err = os.MkdirAll(gopath, 0755)
	handle_err(err)

	orig_gopath := os.Getenv("GOPATH")
	err = os.Setenv("GOPATH", gopath)
	handle_err(err)
	defer os.Setenv("GOPATH", orig_gopath)

	for _, gopkg := range []string{
		"github.com/hwaf/hwaf",
		"github.com/hwaf/hwaf-cmt2yml",
		"github.com/hwaf/hwaf-gen-extpackdist",
		"github.com/hwaf/hwaf-rcore2yml",
	} {
		goget := exec.Command("go", "get", "-v", gopkg)
		goget.Dir = gopath
		if verbose {
			goget.Stdout = os.Stdout
			goget.Stderr = os.Stderr
		}
		err = goget.Run()
		handle_err(err)

		// install under /bin
		dst_fname := filepath.Join(top, "bin", filepath.Base(gopkg))
		dst, err := os.OpenFile(dst_fname, os.O_WRONLY|os.O_CREATE, 0755)
		handle_err(err)
		defer func(dst *os.File) {
			err := dst.Sync()
			handle_err(err)
			err = dst.Close()
			handle_err(err)
		}(dst)

		src_fname := filepath.Join(gopath, "bin", filepath.Base(gopkg))
		if !path_exists(src_fname) {
			// maybe a cross-compilation ?
			src_fname = filepath.Join(gopath, "bin", runtime.GOOS+"_"+runtime.GOARCH, filepath.Base(gopkg))
		}
		src, err := os.Open(src_fname)
		handle_err(err)
		defer func(src *os.File) {
			err := src.Close()
			handle_err(err)
		}(src)

		_, err = io.Copy(dst, src)
		handle_err(err)
	}

	// add waf-bin
	waf_fname := filepath.Join(top, "bin", "waf")
	if path_exists(waf_fname) {
		err = os.Remove(waf_fname)
		handle_err(err)
	}
	waf_dst, err := os.OpenFile(waf_fname, os.O_WRONLY|os.O_CREATE, 0777)
	handle_err(err)
	defer func() {
		err = waf_dst.Sync()
		handle_err(err)
		err = waf_dst.Close()
		handle_err(err)
	}()

	waf_src, err := os.Open(filepath.Join(
		gopath, "src", "github.com", "hwaf", "hwaf", "waf"),
	)
	handle_err(err)
	defer waf_src.Close()
	_, err = io.Copy(waf_dst, waf_src)
	handle_err(err)

	const bq = "`"
	// add setup.sh
	setup_fname, err := os.Create(filepath.Join(top, "setup-hwaf.sh"))
	handle_err(err)
	defer setup_fname.Close()
	_, err = fmt.Fprintf(setup_fname, `#!/bin/sh

if [ "x${BASH_ARGV[0]}" = "x" ]; then
    ## assume zsh
    SOURCE="$0"
else
    SOURCE="${BASH_SOURCE[0]}"
fi

DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
echo ":: adding [$DIR/bin] to PATH"
export PATH=$DIR/bin:$PATH
## EOF
`)
	handle_err(err)

	handle_err(setup_fname.Sync())

	// add setup.csh
	csetup_fname, err := os.Create(filepath.Join(top, "setup-hwaf.csh"))
	handle_err(err)
	defer csetup_fname.Close()
	_, err = fmt.Fprintf(csetup_fname, `#!/bin/csh
# Absolute path to this script
set SCRIPT=%sreadlink -f "$0"%s
# Absolute path this script is in
set SCRIPTPATH=%sdirname "$SCRIPT"%s
echo ":: adding [$SCRIPTPATH/bin] to PATH"
setenv PATH $SCRIPTPATH/bin:$PATH

## EOF
`, bq, bq, bq, bq)
	handle_err(err)
	handle_err(csetup_fname.Sync())

	pwd, err := os.Getwd()
	handle_err(err)

	// package everything up
	err = _tar_gz(filepath.Join(pwd, fname), top)
	handle_err(err)

	if verbose {
		fmt.Printf("%s [%s]... [ok]\n", n, fname)
	}
}

// EOF
<|endoftext|>


<commit_before>// Bubble sorts the first n elements of an array of integers ([]int) into
// accending order
package main

import "fmt"

func main() {
	val := []int{6, 5, 4, 3, 2, 9}

	bubble(val, 400)
	fmt.Printf("%v\n", val)

}

func bubble(v []int, n int) {

	if n > len(v) {
		n = len(v)
	}

	for pass := n - 1; pass >= 1; pass-- {
		for compare := 0; compare < pass; compare++ {
			if v[compare] > v[compare+1] {
				v[compare], v[compare+1] = v[compare+1], v[compare]
			}
		}
	}
}
<commit_msg>redo bubble<commit_after>// Bubble sorts the first n elements of an array of integers ([]int) into
// accending order
package main

import "fmt"

func main() {
	val := []int{6, 5, 4, 3, 2, 9}

	bubble(val, 400)
	fmt.Printf("%v\n", val)

}

//
func bubble(v []int, n int) {
	for pass := len(v) - 1; pass >= 0; pass-- {
		for compare := 0; compare < pass; compare++ {
			if v[compare] > v[compare+1] {
				//swap
				v[compare], v[compare+1] = v[compare+1], v[compare]
			}
		}
	}

}
<|endoftext|>


<commit_before>// Copyright 2012-present Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.

package v4

import (
	"bytes"
	"io/ioutil"
	"net/http"
	"net/url"
	"strings"
	"time"

	"github.com/aws/aws-sdk-go/aws/credentials"
	v4 "github.com/aws/aws-sdk-go/aws/signer/v4"
)

// NewV4SigningClient returns an *http.Client that will sign all requests with AWS V4 Signing.
func NewV4SigningClient(creds *credentials.Credentials, region string) *http.Client {
	return NewV4SigningClientWithHTTPClient(creds, region, http.DefaultClient)
}

// NewV4SigningClientWithHTTPClient returns an *http.Client that will sign all requests with AWS V4 Signing.
func NewV4SigningClientWithHTTPClient(creds *credentials.Credentials, region string, httpClient *http.Client) *http.Client {
	return &http.Client{
		Transport: Transport{
			client: httpClient,
			creds:  creds,
			signer: v4.NewSigner(creds),
			region: region,
		},
	}
}

// Transport is a RoundTripper that will sign requests with AWS V4 Signing
type Transport struct {
	client *http.Client
	creds  *credentials.Credentials
	signer *v4.Signer
	region string
}

// RoundTrip uses the underlying RoundTripper transport, but signs request first with AWS V4 Signing
func (st Transport) RoundTrip(req *http.Request) (*http.Response, error) {
	if h, ok := req.Header["Authorization"]; ok && len(h) > 0 && strings.HasPrefix(h[0], "AWS4") {
		// Received a signed request, just pass it on.
		return st.client.Do(req)
	}

	req.URL.Scheme = "https"
	if strings.Contains(req.URL.RawPath, "%2C") {
		// Escaping path
		req.URL.RawPath = url.PathEscape(req.URL.RawPath)
	}
	now := time.Now().UTC()
	req.Header.Set("Date", now.Format(time.RFC3339))
	var err error
	switch req.Body {
	case nil:
		_, err = st.signer.Sign(req, nil, "es", st.region, now)
	default:
		buf, err := ioutil.ReadAll(req.Body)
		if err != nil {
			return nil, err
		}
		req.Body = ioutil.NopCloser(bytes.NewReader(buf))
		_, err = st.signer.Sign(req, bytes.NewReader(buf), "es", st.region, time.Now().UTC())
	}
	if err != nil {
		return nil, err
	}
	resp, err := st.client.Do(req)
	if resp != nil && resp.Body != nil {
		defer resp.Body.Close()
	}
	if err != nil {
		return nil, err
	}
	if resp.Body != nil {
		buf := new(bytes.Buffer)
		_, err = buf.ReadFrom(resp.Body)
		if err != nil {
			return nil, err
		}
		resp.Body = ioutil.NopCloser(bytes.NewReader(buf.Bytes()))
	}
	return resp, nil
}
<commit_msg>Fix AWS v4 signer when body implements io.ReadSeeker<commit_after>// Copyright 2012-present Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.

package v4

import (
	"bytes"
	"io"
	"io/ioutil"
	"net/http"
	"net/url"
	"strings"
	"time"

	"github.com/aws/aws-sdk-go/aws/credentials"
	v4 "github.com/aws/aws-sdk-go/aws/signer/v4"
)

// NewV4SigningClient returns an *http.Client that will sign all requests with AWS V4 Signing.
func NewV4SigningClient(creds *credentials.Credentials, region string) *http.Client {
	return NewV4SigningClientWithHTTPClient(creds, region, http.DefaultClient)
}

// NewV4SigningClientWithHTTPClient returns an *http.Client that will sign all requests with AWS V4 Signing.
func NewV4SigningClientWithHTTPClient(creds *credentials.Credentials, region string, httpClient *http.Client) *http.Client {
	return &http.Client{
		Transport: Transport{
			client: httpClient,
			creds:  creds,
			signer: v4.NewSigner(creds),
			region: region,
		},
	}
}

// Transport is a RoundTripper that will sign requests with AWS V4 Signing
type Transport struct {
	client *http.Client
	creds  *credentials.Credentials
	signer *v4.Signer
	region string
}

// RoundTrip uses the underlying RoundTripper transport, but signs request first with AWS V4 Signing
func (st Transport) RoundTrip(req *http.Request) (*http.Response, error) {
	if h, ok := req.Header["Authorization"]; ok && len(h) > 0 && strings.HasPrefix(h[0], "AWS4") {
		// Received a signed request, just pass it on.
		return st.client.Do(req)
	}

	req.URL.Scheme = "https"
	if strings.Contains(req.URL.RawPath, "%2C") {
		// Escaping path
		req.URL.RawPath = url.PathEscape(req.URL.RawPath)
	}

	now := time.Now().UTC()
	req.Header.Set("Date", now.Format(time.RFC3339))

	var err error
	switch req.Body {
	case nil:
		_, err = st.signer.Sign(req, nil, "es", st.region, now)
	default:
		switch body := req.Body.(type) {
		case io.ReadSeeker:
			_, err = st.signer.Sign(req, body, "es", st.region, now)
		default:
			buf, err := ioutil.ReadAll(req.Body)
			if err != nil {
				return nil, err
			}
			req.Body = ioutil.NopCloser(bytes.NewReader(buf))
			_, err = st.signer.Sign(req, bytes.NewReader(buf), "es", st.region, time.Now().UTC())
		}
	}
	if err != nil {
		return nil, err
	}
	return st.client.Do(req)
}
<|endoftext|>


<commit_before>// Package visits provides a barebones mechanic to greet players.
// Only the joining player will see this greeting and the number
// of times he has joined.
package cod

import (
	"database/sql"
	"fmt"
	integrated "github.com/adabei/goldenbot-integrated/cod"
	"github.com/adabei/goldenbot/events"
	"github.com/adabei/goldenbot/events/cod"
	"github.com/adabei/goldenbot/rcon"
	"log"
)

type VisitsConfig struct {
	Prefix       string
	FirstMessage string
	Message      string
}

const schema = `
create table visits (
  players_id text primary key,
  total integer default 1
);`

type Visits struct {
	Config   VisitsConfig
	requests chan rcon.RCONQuery
	events   chan interface{}
	db       *sql.DB
}

func NewVisits(config VisitsConfig, requests chan rcon.RCONQuery, ea events.Aggregator, db *sql.DB) *Visits {
	v := new(Visits)
	v.Config = config
	v.requests = requests
	v.events = ea.Subscribe(v)
	v.db = db
	return v
}

func (v *Visits) Setup() error {
	_, err := v.db.Exec(schema)
	return err
}

func (v *Visits) Start() {
	for {
		in := <-v.events
		if ev, ok := in.(cod.Join); ok {
			if exists(v.db, ev.GUID) {
				// update
				log.Println("visits: updating total for player", ev.GUID)
				_, err := v.db.Exec("update visits set total = total + 1 where players_id = ?;", ev.GUID)

				if err != nil {
					log.Fatal("visits: fatal error in update:", err)
				}
			} else {
				// insert new
				log.Println("visits: inserting player", ev.GUID, "into database")
				_, err := v.db.Exec("insert into visits(players_id) values(?);", ev.GUID)

				if err != nil {
					log.Fatal("visits: fatal error in insert:", err)
				}
			}

			var total int
			err := v.db.QueryRow("select total from visits where players_id = ?", ev.GUID).Scan(&total)
			if err != nil {
				total = 1
			}

			var msg string
			if total != 1 {
				msg = fmt.Sprintf(v.Config.Message, ev.Name, total)
			} else {
				msg = fmt.Sprintf(v.Config.FirstMessage, ev.Name)
			}

			num, err := integrated.Num(ev.GUID)
			if err == nil {
				v.requests <- rcon.RCONQuery{Command: "tell " + string(num) + " " +
					v.Config.Prefix + msg, Response: nil}
			} else {
				log.Println("visits: could not resolve num for player", ev.GUID)
			}
		}
	}
}

func exists(db *sql.DB, id string) bool {
	var guid string
	err := db.QueryRow("select players_id from visits where players_id = ?", id).Scan(&guid)
	if err != nil {
		return false
	}

	return true
}
<commit_msg>adjusts to the new integrated interface<commit_after>// Package visits provides a barebones mechanic to greet players.
// Only the joining player will see this greeting and the number
// of times he has joined.
package cod

import (
	"database/sql"
	"fmt"
	integrated "github.com/adabei/goldenbot-integrated/cod"
	"github.com/adabei/goldenbot/events"
	"github.com/adabei/goldenbot/events/cod"
	"github.com/adabei/goldenbot/rcon"
	"log"
	"strconv"
)

type VisitsConfig struct {
	Prefix       string
	FirstMessage string
	Message      string
}

const schema = `
create table visits (
  players_id text primary key,
  total integer default 1
);`

type Visits struct {
	Config   VisitsConfig
	requests chan rcon.RCONQuery
	events   chan interface{}
	db       *sql.DB
}

func NewVisits(config VisitsConfig, requests chan rcon.RCONQuery, ea events.Aggregator, db *sql.DB) *Visits {
	v := new(Visits)
	v.Config = config
	v.requests = requests
	v.events = ea.Subscribe(v)
	v.db = db
	return v
}

func (v *Visits) Setup() error {
	_, err := v.db.Exec(schema)
	return err
}

func (v *Visits) Start() {
	for {
		in := <-v.events
		if ev, ok := in.(cod.Join); ok {
			if exists(v.db, ev.GUID) {
				// update
				log.Println("visits: updating total for player", ev.GUID)
				_, err := v.db.Exec("update visits set total = total + 1 where players_id = ?;", ev.GUID)

				if err != nil {
					log.Fatal("visits: fatal error in update:", err)
				}
			} else {
				// insert new
				log.Println("visits: inserting player", ev.GUID, "into database")
				_, err := v.db.Exec("insert into visits(players_id) values(?);", ev.GUID)

				if err != nil {
					log.Fatal("visits: fatal error in insert:", err)
				}
			}

			var total int
			err := v.db.QueryRow("select total from visits where players_id = ?", ev.GUID).Scan(&total)
			if err != nil {
				total = 1
			}

			var msg string
			if total != 1 {
				msg = fmt.Sprintf(v.Config.Message, ev.Name, total)
			} else {
				msg = fmt.Sprintf(v.Config.FirstMessage, ev.Name)
			}

			if num, ok := integrated.Num(ev.GUID); ok {
				log.Println("visits: welcoming player with guid", ev.GUID, "and num", num)
				v.requests <- rcon.RCONQuery{Command: "tell " + strconv.Itoa(num) + " " +
					v.Config.Prefix + msg, Response: nil}
			} else {
				log.Println("visits: could not resolve num for player", ev.GUID)
			}
		}
	}
}

func exists(db *sql.DB, id string) bool {
	var guid string
	err := db.QueryRow("select players_id from visits where players_id = ?", id).Scan(&guid)
	if err != nil {
		return false
	}

	return true
}
<|endoftext|>


<commit_before>package model

import (
	"time"
)

// Performance of a funds
type Performance struct {
	ID            string    `json:"id"`
	URL           string    `json:"url"`
	Isin          string    `json:"isin"`
	Label         string    `json:"label"`
	Category      string    `json:"category"`
	Rating        string    `json:"rating"`
	OneMonth      float64   `json:"1m"`
	ThreeMonths   float64   `json:"3m"`
	SixMonths     float64   `json:"6m"`
	OneYear       float64   `json:"1y"`
	VolThreeYears float64   `json:"v3y"`
	Score         float64   `json:"score"`
	Update        time.Time `json:"ts"`
}

// GetID returns Performance's ID
func (p Performance) GetID() string {
	return p.ID
}

// ComputeScore calculate score from Performance's values
func (p *Performance) ComputeScore() {
	score := (0.25 * p.OneMonth) + (0.3 * p.ThreeMonths) + (0.25 * p.SixMonths) + (0.2 * p.OneYear) - (0.1 * p.VolThreeYears)
	p.Score = float64(int(score*100)) / 100
}
<commit_msg>Update performance.go<commit_after>package model

import (
	"time"
)

// Performance of a funds
type Performance struct {
	ID            string    `json:"id"`
	Isin          string    `json:"isin"`
	Label         string    `json:"label"`
	Category      string    `json:"category"`
	Rating        string    `json:"rating"`
	OneMonth      float64   `json:"1m"`
	ThreeMonths   float64   `json:"3m"`
	SixMonths     float64   `json:"6m"`
	OneYear       float64   `json:"1y"`
	VolThreeYears float64   `json:"v3y"`
	Score         float64   `json:"score"`
	Update        time.Time `json:"ts"`
}

// GetID returns Performance's ID
func (p Performance) GetID() string {
	return p.ID
}

// ComputeScore calculate score from Performance's values
func (p *Performance) ComputeScore() {
	score := (0.25 * p.OneMonth) + (0.3 * p.ThreeMonths) + (0.25 * p.SixMonths) + (0.2 * p.OneYear) - (0.1 * p.VolThreeYears)
	p.Score = float64(int(score*100)) / 100
}
<|endoftext|>


<commit_before>package models

//import (
//	"github.com/astaxie/beego"
//)

type AdResponse struct {
	StatusCode       int
	AdspaceKey       string
	DemandAdspaceKey string
	ResponseTime     int64
	Bid              string
	Adunit           *AdUnit
}

type CommonResponse struct {
	StatusCode int     `json:"statusCode"`
	AdspaceKey string  `json:"adspaceKey"`
	Bid        string  `json:"bid"`
	Adunit     *AdUnit `json:"adunit"`
}

func (this *AdResponse) GenerateCommonResponse() CommonResponse {
	res := CommonResponse{}
	res.StatusCode = this.StatusCode
	res.AdspaceKey = this.AdspaceKey
	res.Bid = this.Bid
	res.Adunit = this.Adunit

	return res
}

func (this *AdResponse) SetDemandAdspaceKey(dkey string) {
	this.DemandAdspaceKey = dkey
}

func (this *AdResponse) GetDemandAdspaceKey() string {
	return this.DemandAdspaceKey
}

func (this *AdResponse) SetResponseTime(responseTime int64) {
	this.ResponseTime = responseTime
}

func (this *AdResponse) GetResponseTime() int64 {
	return this.ResponseTime
}

func (this *AdResponse) AddImpTracking(url string) {
	if this.Adunit == nil {
		return
	}

	if this.Adunit.ImpTrackingUrls != nil {
		this.Adunit.ImpTrackingUrls = append(this.Adunit.ImpTrackingUrls, url)
	} else {
		this.Adunit.ImpTrackingUrls = []string{url}
	}

}

func (this *AdResponse) AddClkTracking(url string) {
	if this.Adunit == nil {
		return
	}

	if this.Adunit.ClkTrackingUrls != nil {
		this.Adunit.ClkTrackingUrls = append(this.Adunit.ClkTrackingUrls, url)
	} else {
		this.Adunit.ClkTrackingUrls = []string{url}
	}

}

type AdUnit struct {
	Cid             string   `json:"cid"`
	ClickUrl        string   `json:"clickUrl"`
	DisplayText     string   `json:"displayText"`
	ImageUrls       []string `json:"imageUrls"`
	ImpTrackingUrls []string `json:"impTrackingUrls"`
	ClkTrackingUrls []string `json:"clkTrackingUrls"`
	AdWidth         int      `json:"adWidth"`
	AdHeight        int      `json:"adHeight"`
}

type MHAdUnit struct {
	Adspaceid   string
	Returncode  int
	Cid         string
	Adwidth     int
	Adheight    int
	Adtype      int
	Imgurl      string
	Clickurl    string
	Imgtracking []string
	Thclkurl    []string
}

type AdspaceData struct {
	AdspaceKey string
	SecretKey  string
}

type PmpAdplaceInfo struct {
	PmpAdspaceKey    string
	DemandId         int
	DemandAdspaceKey string
	DemandSecretKey  string
}

type DemandInfo struct {
	DemandId           int
	Name               string
	RequestUrlTemplate string
	Timeout            int
	InvokeFuncName     string
}
<commit_msg>add url response<commit_after>package models

//import (
//	"github.com/astaxie/beego"
//)

type AdResponse struct {
	StatusCode       int
	AdspaceKey       string
	DemandAdspaceKey string
	ResponseTime     int64
	Bid              string
	Adunit           *AdUnit
}

type UrlResponse struct {
	StatusCode int    `json:"statusCode"`
	AdspaceKey string `json:"adspaceKey"`
	Bid        string `json:"bid"`
	DisplayUrl string `json:"displayUrl"`
}

type CommonResponse struct {
	StatusCode int     `json:"statusCode"`
	AdspaceKey string  `json:"adspaceKey"`
	Bid        string  `json:"bid"`
	Adunit     *AdUnit `json:"adunit"`
}

func (this *AdResponse) GenerateCommonResponse() CommonResponse {
	res := CommonResponse{}
	res.StatusCode = this.StatusCode
	res.AdspaceKey = this.AdspaceKey
	res.Bid = this.Bid
	res.Adunit = this.Adunit

	return res
}

func (this *AdResponse) SetDemandAdspaceKey(dkey string) {
	this.DemandAdspaceKey = dkey
}

func (this *AdResponse) GetDemandAdspaceKey() string {
	return this.DemandAdspaceKey
}

func (this *AdResponse) SetResponseTime(responseTime int64) {
	this.ResponseTime = responseTime
}

func (this *AdResponse) GetResponseTime() int64 {
	return this.ResponseTime
}

func (this *AdResponse) AddImpTracking(url string) {
	if this.Adunit == nil {
		return
	}

	if this.Adunit.ImpTrackingUrls != nil {
		this.Adunit.ImpTrackingUrls = append(this.Adunit.ImpTrackingUrls, url)
	} else {
		this.Adunit.ImpTrackingUrls = []string{url}
	}

}

func (this *AdResponse) AddClkTracking(url string) {
	if this.Adunit == nil {
		return
	}

	if this.Adunit.ClkTrackingUrls != nil {
		this.Adunit.ClkTrackingUrls = append(this.Adunit.ClkTrackingUrls, url)
	} else {
		this.Adunit.ClkTrackingUrls = []string{url}
	}

}

type AdUnit struct {
	Cid             string   `json:"cid"`
	ClickUrl        string   `json:"clickUrl"`
	DisplayText     string   `json:"displayText"`
	ImageUrls       []string `json:"imageUrls"`
	ImpTrackingUrls []string `json:"impTrackingUrls"`
	ClkTrackingUrls []string `json:"clkTrackingUrls"`
	AdWidth         int      `json:"adWidth"`
	AdHeight        int      `json:"adHeight"`
}

type MHAdUnit struct {
	Adspaceid   string
	Returncode  int
	Cid         string
	Adwidth     int
	Adheight    int
	Adtype      int
	Imgurl      string
	Clickurl    string
	Imgtracking []string
	Thclkurl    []string
}

type AdspaceData struct {
	AdspaceKey string
	SecretKey  string
}

type PmpAdplaceInfo struct {
	PmpAdspaceKey    string
	DemandId         int
	DemandAdspaceKey string
	DemandSecretKey  string
}

type DemandInfo struct {
	DemandId           int
	Name               string
	RequestUrlTemplate string
	Timeout            int
	InvokeFuncName     string
}
<|endoftext|>


<commit_before>package main

import (
	"encoding/csv"
	"fmt"
	. "github.com/abhiyerra/workmachine/app"
	"os"
)

type ImageTagging struct {
	ImageUrl             InputField  `work_desc:"Use this image to fill the information below." work_id:"image_url" work_type:"image"`
	Tags                 OutputField `work_desc:"List all the relavent tags for this image. Ex. trees, castle, person" work_id:"tags"`
	TextInImage          OutputField `work_desc:"Put any text that appears on the image here. One line per block of text." work_id:"text_in_image"`
	IsCorrectOrientation OutputField `work_desc:"Is the image in the correct orientation?" work_id:"is_correct_orientation" work_type:"checkbox"`
	IsLandscape          OutputField `work_desc:"Is the image of a landscape?" work_id:"is_landscape" work_type:"checkbox"`
	IsPattern            OutputField `work_desc:"Is the image of a pattern?" work_id:"is_pattern" work_type:"checkbox"`
	IsPerson             OutputField `work_desc:"Is the image of a person?" work_id:"is_person" work_type:"checkbox"`
	TraditionalClothing  OutputField `work_desc:"Are they wearing traditional costume?" work_id:"tradtional_clothing" work_type:"checkbox"`
}

func imageUrls() (images []ImageTagging) {
	file, err := os.Open("list_of_pictures.csv")
	if err != nil {
		panic(err)
	}

	reader := csv.NewReader(file)
	records, err := reader.ReadAll()

	for _, i := range records {
		//		fmt.Printf("%s\n", i[1])
		images = append(images, ImageTagging{ImageUrl: InputField(i[1])})
	}

	if err != nil {
		panic(err)
	}

	return
}

func main() {
	results_file, err := os.Create("results.csv")
	if err != nil {
		panic(err)
	}
	defer results_file.Close()

	writer := csv.NewWriter(results_file)

	image_tasks := Task{
		Title:       "Tag the appropriate images",
		Description: "Look at the image and fill out the appropriate fields. We want to be able to tag all the images correctly. Fill out any appropriate tag that you see.",
		Write: func(j *Job) {
			fmt.Printf("%v\n", j)

			var output []string

			for _, i := range j.InputFields {
				output = append(output, i.Value)
				fmt.Println(i.Value)
			}

			for _, i := range j.OutputFields {
				output = append(output, i.Value)
				fmt.Println(i.Value)
			}

			if err := writer.Write(output); err != nil {
				panic(err)
			}
			writer.Flush()
		},
		Tasks: imageUrls(),
	}

	fmt.Println("Loaded and starting")
	serve := HtmlServe{}
	go Serve()

	fmt.Println("Serving")
	var backend Assigner = serve
	NewBatch(image_tasks).Run(backend)

}
<commit_msg>More details about the input.<commit_after>package main

import (
	"encoding/csv"
	"fmt"
	. "github.com/abhiyerra/workmachine/app"
	"os"
)

type ImageTagging struct {
	ImageUrl             InputField  `work_desc:"Use this image to fill the information below." work_id:"image_url" work_type:"image"`
	Tags                 OutputField `work_desc:"List all the relavent tags for this image. Ex. trees, castle, person" work_id:"tags"`
	TextInImage          OutputField `work_desc:"Put any text that appears on the image here. One line per block of text." work_id:"text_in_image"`
	IsCorrectOrientation OutputField `work_desc:"Is the image in the correct orientation?" work_id:"is_correct_orientation" work_type:"checkbox"`
	IsLandscape          OutputField `work_desc:"Is the image of a landscape?" work_id:"is_landscape" work_type:"checkbox"`
	IsPattern            OutputField `work_desc:"Is the image of a pattern?" work_id:"is_pattern" work_type:"checkbox"`
	IsPerson             OutputField `work_desc:"Is the image of a person?" work_id:"is_person" work_type:"checkbox"`
	TraditionalClothing  OutputField `work_desc:"Are they wearing traditional costume?" work_id:"tradtional_clothing" work_type:"checkbox"`
}

func imageUrls() (images []ImageTagging) {
	file, err := os.Open("list_of_pictures.csv")
	if err != nil {
		panic(err)
	}

	reader := csv.NewReader(file)
	records, err := reader.ReadAll()

	for _, i := range records {
		//		fmt.Printf("%s\n", i[1])
		images = append(images, ImageTagging{ImageUrl: InputField(i[1])})
	}

	if err != nil {
		panic(err)
	}

	return
}

func main() {
	results_file, err := os.Create("results.csv")
	if err != nil {
		panic(err)
	}
	defer results_file.Close()

	writer := csv.NewWriter(results_file)

	image_urls := imageUrls()

	image_tasks := Task{
		Title:       "Tag the appropriate images",
		Description: "Look at the image and fill out the appropriate fields. We want to be able to tag all the images correctly. Fill out any appropriate tag that you see.",
		Write: func(j *Job) {
			fmt.Printf("%v\n", j)

			var output []string

			for _, i := range j.InputFields {
				output = append(output, i.Value)
				fmt.Println(i.Value)
			}

			for _, i := range j.OutputFields {
				output = append(output, i.Value)
				fmt.Println(i.Value)
			}

			if err := writer.Write(output); err != nil {
				panic(err)
			}
			writer.Flush()
		},
		Tasks: image_urls,
	}

	fmt.Printf("Loaded %d images and starting\n", len(image_urls))
	serve := HtmlServe{}
	go Serve()

	fmt.Println("Serving")
	var backend Assigner = serve
	NewBatch(image_tasks).Run(backend)

}
<|endoftext|>