<commit_before>package main

import (
	"bytes"
	"errors"
	"flag"
	"fmt"
	"github.com/mrosset/util"
	"github.com/mrosset/util/console/command"
	"github.com/mrosset/util/file"
	"github.com/mrosset/util/json"
	"github.com/mrosset/via/pkg"
	"gopkg.in/urfave/cli.v2"
	"io"
	"log"
	"net/http"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
)

var (
	elog     = log.New(os.Stderr, "", log.Lshortfile)
	root     = flag.String("r", "/", "root directory")
	verbose  = flag.Bool("v", false, "verbose output")
	finstall = flag.Bool("i", true, "install package after build (default true)")
	fdebug   = flag.Bool("d", false, "debug output")
	config   = via.GetConfig()
	fclean   = flag.Bool("c", false, "clean before build")
	fupdate  = flag.Bool("u", false, "force download source")
	fdeps    = flag.Bool("deps", false, "build depends if needed")

	app = &cli.App{
		Name:  "via",
		Usage: "a systems package manager",
	}

	// build command
	cBuild = &cli.Command{
		Name:   "build",
		Usage:  "builds a plan locally",
		Action: local,
		Flags: []cli.Flag{
			&cli.BoolFlag{
				Name:  "c",
				Value: false,
				Usage: "clean build directory before building",
			},
			&cli.BoolFlag{
				Name:  "v",
				Value: false,
				Usage: "displays more information when building",
			},
			&cli.BoolFlag{
				Name:  "d",
				Value: false,
				Usage: "displays debugging information when building",
			},
			&cli.BoolFlag{
				Name:  "i",
				Value: false,
				Usage: "install package after building",
			},
		},
	}

	// dock command
	cDock = &cli.Command{
		Name:   "dock",
		Usage:  "builds a package inside a clean docker image",
		Action: dock,
		Flags: []cli.Flag{
			&cli.BoolFlag{
				Name:  "c",
				Value: false,
				Usage: "clean build directory before building",
			},
		},
	}

	// install command
	cInstall = &cli.Command{
		Name:   "install",
		Usage:  "installs package",
		Action: install,
	}
)

func main() {
	app.Commands = []*cli.Command{
		cInstall,
		cBuild,
		cDock,
	}
	err := app.Run(os.Args)
	if err != nil {
		log.Fatal(err)
	}
	return
	flag.Parse()
	via.Verbose(*verbose)
	via.Update(*fupdate)
	via.Deps(*fdeps)

	via.Root(*root)
	util.Verbose = *verbose
	via.Debug(*fdebug)
	command.Add("add", add, "add plan/s to git index")
	command.Add("branch", branch, "prints plan branch to stdout")
	command.Add("cd", cd, "returns a bash evaluable cd path")
	command.Add("checkout", checkout, "changes plan branch")
	command.Add("clean", clean, "clean build dir")
	command.Add("config", fnConfig, "prints config to stdout")
	command.Add("create", create, "create plan from URL")
	command.Add("diff", diff, "prints git diff for plan(s)")
	command.Add("edit", edit, "calls EDITOR to edit plan")
	command.Add("elf", elf, "prints elf information to stdout")
	command.Add("ipfs", ipfs, "test ipfs connection")
	command.Add("lint", lint, "lint plans")
	command.Add("log", plog, "print config log for plan")
	command.Add("owns", owns, "finds which package owns a file")
	command.Add("options", options, "prints the GNU configure options for a package")
	command.Add("pack", pack, "package plan")
	command.Add("remove", remove, "remove package")
	command.Add("repo", repo, "update repo")
	command.Add("search", search, "search for plans (currently lists all use grep)")
	command.Add("show", fnShow, "prints plan to stdout")
	command.Add("sync", sync, "fetch remote repo data")
	command.Add("synchashs", synchashs, "DEV ONLY sync the plans Oid with binary banch")
	if *fdebug {
		pdebug()
	}
	err = command.Run()
	if err != nil {
		elog.Fatal(err)
	}
	return
}

func install(ctx *cli.Context) error {
	if !ctx.Args().Present() {
		return fmt.Errorf("install requires a 'PLAN' argument. see: 'via help install'")
	}
	return via.Install(ctx.Args().First())
}

func local(ctx *cli.Context) error {
	if !ctx.Args().Present() {
		return fmt.Errorf("build requires a 'PLAN' argument. see: 'via help build'")
	}
	plan, err := via.NewPlan(ctx.Args().First())
	if err != nil {
		return err
	}

	via.Verbose(ctx.Bool("v"))
	via.Debug(ctx.Bool("d"))

	if ctx.Bool("c") {
		via.Clean(plan.Name)
	}
	err = via.BuildSteps(plan)
	if err != nil {
		return err
	}
	if ctx.Bool("i") {
		return via.Install(plan.Name)
	}
	return nil
}

func pdebug() {
	path, _ := os.LookupEnv("PATH")
	home, _ := os.LookupEnv("HOME")
	fmt.Println("PATH", path)
	fmt.Println("HOME", home)
	which("GCC", "gcc")
	which("PYTHON", "python")
}

func which(label, path string) {
	fmt.Printf("%s ", label)
	cmd := exec.Command("which", path)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	err := cmd.Run()
	if err != nil {
		elog.Println(err)
	}
}

func ipfs() error {
	res, err := http.Get(config.Binary)
	io.Copy(os.Stdout, res.Body)
	return err
}

func cd() error {
	if len(command.Args()) < 1 {
		return errors.New("you need to specify a config path")
	}
	arg := command.Args()[0]
	switch arg {
	case "plans":
		fmt.Printf("cd %s", config.Plans)
	default:
		err := fmt.Sprintf("config path %s not found", arg)
		return errors.New(err)
	}
	return nil
}

func add() error {
	if len(command.Args()) < 1 {
		return errors.New("no plans specified")
	}
	for _, arg := range command.Args() {
		glob := filepath.Join(config.Plans, "*", arg+".json")
		res, err := filepath.Glob(glob)
		if err != nil {
			return err
		}
		git := exec.Command("git", "add", strings.Join(res, " "))
		git.Dir = config.Plans
		git.Stdout = os.Stdout
		git.Stderr = os.Stderr
		err = git.Run()
		if err != nil {
			return err
		}
	}
	return nil
}

func diff() error {
	if len(command.Args()) < 1 {
		return errors.New("no plans specified")
	}
	for _, arg := range command.Args() {
		glob := filepath.Join(config.Plans, "*", arg+".json")
		res, err := filepath.Glob(glob)
		if err != nil {
			return err
		}
		git := exec.Command("git", "diff", strings.Join(res, " "))
		git.Dir = config.Plans
		git.Stdout = os.Stdout
		git.Stderr = os.Stderr
		err = git.Run()
		if err != nil {
			return err
		}
	}
	return nil
}

func checkout() error {
	if len(command.Args()) < 1 {
		return errors.New("git branch needs to be specified")
	}
	arg := command.Args()[0]
	git := exec.Command("git", "checkout", arg)
	git.Dir = config.Plans
	git.Stdout = os.Stdout
	git.Stderr = os.Stderr
	return git.Run()
}

func branch() error {
	git := exec.Command("git", "branch")
	git.Dir = config.Plans
	git.Stdout = os.Stdout
	git.Stderr = os.Stderr
	return git.Run()

}
func edit() error {
	var (
		editor = os.Getenv("EDITOR")
		arg0   = command.Args()[0]
		p      = filepath.Join(config.Plans, "config.json")
		err    error
	)
	if arg0 != "config" {
		p, err = via.FindPlanPath(arg0)
		if err != nil {
			return err
		}
	}
	cmd := exec.Command(editor, p)
	cmd.Stdin = os.Stdin
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	err = cmd.Run()
	if err != nil {
		return err
	}
	elog.Println("linting...")
	via.Verbose(false)
	return via.Lint()
}

func create() error {
	url := command.Args()[0]
	group := command.Args()[1]
	err := via.Create(url, group)
	if err != nil {
		return err
	}
	return nil
}

func plog() error {
	for _, arg := range command.Args() {
		plan, err := via.NewPlan(arg)
		if err != nil {
			return err
		}
		f := filepath.Join(plan.BuildDir(), "config.log")
		err = file.Cat(os.Stdout, f)
		if err != nil {
			log.Fatal(err)
		}
	}
	return nil
}

func dock(ctx *cli.Context) error {
	return via.CircuitBuild(ctx.Args().First())
}

func pack() error {
	for _, arg := range command.Args() {
		plan, err := via.NewPlan(arg)
		if err != nil {
			return err
		}
		err = via.Package("", plan)
		if err != nil {
			return err
		}
	}
	return nil
}

func list() error {
	for _, arg := range command.Args() {
		plan, err := via.NewPlan(arg)
		if err != nil {
			return err
		}
		for _, f := range plan.Files {
			fmt.Println(f)
		}
	}
	return nil
}

func remove() error {
	return command.ArgsDo(via.Remove)
}

func lint() error {
	return via.Lint()
}

func fnShow() error {
	for _, arg := range command.Args() {
		plan, err := via.NewPlan(arg)
		if err != nil {
			elog.Fatal(err)
		}
		buf := new(bytes.Buffer)
		less := exec.Command("less")
		less.Stdin = buf
		less.Stdout = os.Stdout
		less.Stderr = os.Stderr
		err = json.WritePretty(&plan, buf)
		if err != nil {
			fmt.Println(err)
		}
		less.Run()
	}
	return nil
}

func fnConfig() error {
	err := json.WritePretty(via.GetConfig(), os.Stdout)
	if err != nil {
		return err
	}
	return nil
}

func clean() error {
	return command.ArgsDo(via.Clean)
}

func elf() error {
	for _, arg := range command.Args() {
		err := via.Readelf(arg)
		if err != nil {
			return err
		}
	}
	return nil
}

func sync() error {
	return via.PlanSync()
}

func synchashs() error {
	via.SyncHashs()
	return nil
}
func owns() error {
	rfiles, err := via.ReadRepoFiles()
	if err != nil {
		return err
	}
	for _, arg := range command.Args() {
		owner := rfiles.Owns(arg)
		if owner == "" {
			fmt.Println(arg+":", "owner not found.")
			continue
		}
		fmt.Println(owner)
	}
	return nil
}

func options() error {
	if len(command.Args()) < 1 {

	}
	arg := command.Args()[0]

	plan, err := via.NewPlan(arg)
	if err != nil {
		return err
	}
	c := filepath.Join(plan.GetStageDir(), "configure")
	fmt.Println(c)
	cmd := exec.Command(c, "--help")
	cmd.Stdout = os.Stdout
	cmd.Stdin = os.Stdin
	cmd.Stderr = os.Stderr
	return cmd.Run()
}

func repo() error {
	return via.RepoCreate()
}

func search() error {
	plans, err := via.GetPlans()
	if err != nil {
		return err
	}
	plans.SortSize().Print()
	return nil
}
<commit_msg>add -r flag to install<commit_after>package main

import (
	"bytes"
	"errors"
	"flag"
	"fmt"
	"github.com/mrosset/util"
	"github.com/mrosset/util/console/command"
	"github.com/mrosset/util/file"
	"github.com/mrosset/util/json"
	"github.com/mrosset/via/pkg"
	"gopkg.in/urfave/cli.v2"
	"io"
	"log"
	"net/http"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
)

var (
	elog     = log.New(os.Stderr, "", log.Lshortfile)
	root     = flag.String("r", "/", "root directory")
	verbose  = flag.Bool("v", false, "verbose output")
	finstall = flag.Bool("i", true, "install package after build (default true)")
	fdebug   = flag.Bool("d", false, "debug output")
	config   = via.GetConfig()
	fclean   = flag.Bool("c", false, "clean before build")
	fupdate  = flag.Bool("u", false, "force download source")
	fdeps    = flag.Bool("deps", false, "build depends if needed")

	app = &cli.App{
		Name:  "via",
		Usage: "a systems package manager",
	}

	// build command
	cBuild = &cli.Command{
		Name:   "build",
		Usage:  "builds a plan locally",
		Action: local,
		Flags: []cli.Flag{
			&cli.BoolFlag{
				Name:  "c",
				Value: false,
				Usage: "clean build directory before building",
			},
			&cli.BoolFlag{
				Name:  "v",
				Value: false,
				Usage: "displays more information when building",
			},
			&cli.BoolFlag{
				Name:  "d",
				Value: false,
				Usage: "displays debugging information when building",
			},
			&cli.BoolFlag{
				Name:  "i",
				Value: false,
				Usage: "install package after building",
			},
		},
	}

	// dock command
	cDock = &cli.Command{
		Name:   "dock",
		Usage:  "builds a package inside a clean docker image",
		Action: dock,
		Flags: []cli.Flag{
			&cli.BoolFlag{
				Name:  "c",
				Value: false,
				Usage: "clean build directory before building",
			},
		},
	}

	// install command
	cInstall = &cli.Command{
		Name:   "install",
		Usage:  "installs package",
		Action: install,
		Flags: []cli.Flag{
			&cli.StringFlag{
				Name:  "r",
				Value: "/",
				Usage: "use `\"DIR\"` as root",
			},
		},
	}
)

func main() {
	app.Commands = []*cli.Command{
		cInstall,
		cBuild,
		cDock,
	}
	err := app.Run(os.Args)
	if err != nil {
		log.Fatal(err)
	}
	return
	flag.Parse()
	via.Verbose(*verbose)
	via.Update(*fupdate)
	via.Deps(*fdeps)

	via.Root(*root)
	util.Verbose = *verbose
	via.Debug(*fdebug)
	command.Add("add", add, "add plan/s to git index")
	command.Add("branch", branch, "prints plan branch to stdout")
	command.Add("cd", cd, "returns a bash evaluable cd path")
	command.Add("checkout", checkout, "changes plan branch")
	command.Add("clean", clean, "clean build dir")
	command.Add("config", fnConfig, "prints config to stdout")
	command.Add("create", create, "create plan from URL")
	command.Add("diff", diff, "prints git diff for plan(s)")
	command.Add("edit", edit, "calls EDITOR to edit plan")
	command.Add("elf", elf, "prints elf information to stdout")
	command.Add("ipfs", ipfs, "test ipfs connection")
	command.Add("lint", lint, "lint plans")
	command.Add("log", plog, "print config log for plan")
	command.Add("owns", owns, "finds which package owns a file")
	command.Add("options", options, "prints the GNU configure options for a package")
	command.Add("pack", pack, "package plan")
	command.Add("remove", remove, "remove package")
	command.Add("repo", repo, "update repo")
	command.Add("search", search, "search for plans (currently lists all use grep)")
	command.Add("show", fnShow, "prints plan to stdout")
	command.Add("sync", sync, "fetch remote repo data")
	command.Add("synchashs", synchashs, "DEV ONLY sync the plans Oid with binary banch")
	if *fdebug {
		pdebug()
	}
	err = command.Run()
	if err != nil {
		elog.Fatal(err)
	}
	return
}

func install(ctx *cli.Context) error {
	if !ctx.Args().Present() {
		return fmt.Errorf("install requires a 'PLAN' argument. see: 'via help install'")
	}

	via.Root(ctx.String("r"))
	return via.Install(ctx.Args().First())
}

func local(ctx *cli.Context) error {
	if !ctx.Args().Present() {
		return fmt.Errorf("build requires a 'PLAN' argument. see: 'via help build'")
	}
	plan, err := via.NewPlan(ctx.Args().First())
	if err != nil {
		return err
	}

	via.Verbose(ctx.Bool("v"))
	via.Debug(ctx.Bool("d"))

	if ctx.Bool("c") {
		via.Clean(plan.Name)
	}
	err = via.BuildSteps(plan)
	if err != nil {
		return err
	}
	if ctx.Bool("i") {
		return via.Install(plan.Name)
	}
	return nil
}

func pdebug() {
	path, _ := os.LookupEnv("PATH")
	home, _ := os.LookupEnv("HOME")
	fmt.Println("PATH", path)
	fmt.Println("HOME", home)
	which("GCC", "gcc")
	which("PYTHON", "python")
}

func which(label, path string) {
	fmt.Printf("%s ", label)
	cmd := exec.Command("which", path)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	err := cmd.Run()
	if err != nil {
		elog.Println(err)
	}
}

func ipfs() error {
	res, err := http.Get(config.Binary)
	io.Copy(os.Stdout, res.Body)
	return err
}

func cd() error {
	if len(command.Args()) < 1 {
		return errors.New("you need to specify a config path")
	}
	arg := command.Args()[0]
	switch arg {
	case "plans":
		fmt.Printf("cd %s", config.Plans)
	default:
		err := fmt.Sprintf("config path %s not found", arg)
		return errors.New(err)
	}
	return nil
}

func add() error {
	if len(command.Args()) < 1 {
		return errors.New("no plans specified")
	}
	for _, arg := range command.Args() {
		glob := filepath.Join(config.Plans, "*", arg+".json")
		res, err := filepath.Glob(glob)
		if err != nil {
			return err
		}
		git := exec.Command("git", "add", strings.Join(res, " "))
		git.Dir = config.Plans
		git.Stdout = os.Stdout
		git.Stderr = os.Stderr
		err = git.Run()
		if err != nil {
			return err
		}
	}
	return nil
}

func diff() error {
	if len(command.Args()) < 1 {
		return errors.New("no plans specified")
	}
	for _, arg := range command.Args() {
		glob := filepath.Join(config.Plans, "*", arg+".json")
		res, err := filepath.Glob(glob)
		if err != nil {
			return err
		}
		git := exec.Command("git", "diff", strings.Join(res, " "))
		git.Dir = config.Plans
		git.Stdout = os.Stdout
		git.Stderr = os.Stderr
		err = git.Run()
		if err != nil {
			return err
		}
	}
	return nil
}

func checkout() error {
	if len(command.Args()) < 1 {
		return errors.New("git branch needs to be specified")
	}
	arg := command.Args()[0]
	git := exec.Command("git", "checkout", arg)
	git.Dir = config.Plans
	git.Stdout = os.Stdout
	git.Stderr = os.Stderr
	return git.Run()
}

func branch() error {
	git := exec.Command("git", "branch")
	git.Dir = config.Plans
	git.Stdout = os.Stdout
	git.Stderr = os.Stderr
	return git.Run()

}
func edit() error {
	var (
		editor = os.Getenv("EDITOR")
		arg0   = command.Args()[0]
		p      = filepath.Join(config.Plans, "config.json")
		err    error
	)
	if arg0 != "config" {
		p, err = via.FindPlanPath(arg0)
		if err != nil {
			return err
		}
	}
	cmd := exec.Command(editor, p)
	cmd.Stdin = os.Stdin
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	err = cmd.Run()
	if err != nil {
		return err
	}
	elog.Println("linting...")
	via.Verbose(false)
	return via.Lint()
}

func create() error {
	url := command.Args()[0]
	group := command.Args()[1]
	err := via.Create(url, group)
	if err != nil {
		return err
	}
	return nil
}

func plog() error {
	for _, arg := range command.Args() {
		plan, err := via.NewPlan(arg)
		if err != nil {
			return err
		}
		f := filepath.Join(plan.BuildDir(), "config.log")
		err = file.Cat(os.Stdout, f)
		if err != nil {
			log.Fatal(err)
		}
	}
	return nil
}

func dock(ctx *cli.Context) error {
	return via.CircuitBuild(ctx.Args().First())
}

func pack() error {
	for _, arg := range command.Args() {
		plan, err := via.NewPlan(arg)
		if err != nil {
			return err
		}
		err = via.Package("", plan)
		if err != nil {
			return err
		}
	}
	return nil
}

func list() error {
	for _, arg := range command.Args() {
		plan, err := via.NewPlan(arg)
		if err != nil {
			return err
		}
		for _, f := range plan.Files {
			fmt.Println(f)
		}
	}
	return nil
}

func remove() error {
	return command.ArgsDo(via.Remove)
}

func lint() error {
	return via.Lint()
}

func fnShow() error {
	for _, arg := range command.Args() {
		plan, err := via.NewPlan(arg)
		if err != nil {
			elog.Fatal(err)
		}
		buf := new(bytes.Buffer)
		less := exec.Command("less")
		less.Stdin = buf
		less.Stdout = os.Stdout
		less.Stderr = os.Stderr
		err = json.WritePretty(&plan, buf)
		if err != nil {
			fmt.Println(err)
		}
		less.Run()
	}
	return nil
}

func fnConfig() error {
	err := json.WritePretty(via.GetConfig(), os.Stdout)
	if err != nil {
		return err
	}
	return nil
}

func clean() error {
	return command.ArgsDo(via.Clean)
}

func elf() error {
	for _, arg := range command.Args() {
		err := via.Readelf(arg)
		if err != nil {
			return err
		}
	}
	return nil
}

func sync() error {
	return via.PlanSync()
}

func synchashs() error {
	via.SyncHashs()
	return nil
}
func owns() error {
	rfiles, err := via.ReadRepoFiles()
	if err != nil {
		return err
	}
	for _, arg := range command.Args() {
		owner := rfiles.Owns(arg)
		if owner == "" {
			fmt.Println(arg+":", "owner not found.")
			continue
		}
		fmt.Println(owner)
	}
	return nil
}

func options() error {
	if len(command.Args()) < 1 {

	}
	arg := command.Args()[0]

	plan, err := via.NewPlan(arg)
	if err != nil {
		return err
	}
	c := filepath.Join(plan.GetStageDir(), "configure")
	fmt.Println(c)
	cmd := exec.Command(c, "--help")
	cmd.Stdout = os.Stdout
	cmd.Stdin = os.Stdin
	cmd.Stderr = os.Stderr
	return cmd.Run()
}

func repo() error {
	return via.RepoCreate()
}

func search() error {
	plans, err := via.GetPlans()
	if err != nil {
		return err
	}
	plans.SortSize().Print()
	return nil
}
<|endoftext|>


<commit_before>/*
	Program that acts similar to cat()
	except instead of using stdout it creates
	a private gist in Github

	Uses an environment variable GITHUB_API to
	determine the users api key.
*/

package main

import (
	"bytes"
	"flag"
	"fmt"
	"io"
	"os"

	"code.google.com/p/goauth2/oauth"
	"github.com/google/go-github/github"
)

const tokenEnv = "GITHUB_API"

type GistFiles map[github.GistFilename]github.GistFile

// Create GistFiles from stdin
func FromStdin(gistFiles GistFiles, fExt string) github.GistFile {
	var stdOutBuff bytes.Buffer

	_, err := io.Copy(&stdOutBuff, os.Stdin)
	if err != nil {
		fmt.Printf("Error copying stdin to buffer: %v\n", err)
		os.Exit(1)
	}

	gistStr := stdOutBuff.String()
	return github.GistFile{Content: &gistStr}
}

// Cats file content together into a gistFile
func FromFiles(gistFiles GistFiles, files []string, fExt string) github.GistFile {
	var stdOutBuff bytes.Buffer

	for _, fileName := range files {
		file, err := os.Open(fileName)
		if err != nil {
			fmt.Printf("Error opening file %s: %v", fileName, err)
			os.Exit(1)
		}

		if _, err := io.Copy(&stdOutBuff, file); err != nil {
			fmt.Printf("Error copying file contents to buffer: %v\n", err)
		}
	}

	gistStr := stdOutBuff.String()
	return github.GistFile{Content: &gistStr}
}

func main() {
	var public = flag.Bool("public", false, "makes the gist public")
	var extension = flag.String("ext", "txt", "file extension to use")
	flag.Parse()

	apiToken := os.Getenv(tokenEnv)
	if apiToken == "" {
		fmt.Println("must set environment variable $GITHUB_API")
		os.Exit(1)
	}

	t := &oauth.Transport{
		Token: &oauth.Token{AccessToken: apiToken},
	}
	client := github.NewClient(t.Client())

	args := flag.Args()
	gistFiles := make(GistFiles)
	gistFilename := github.GistFilename(fmt.Sprintf("gistfile1.%s", *extension))
	if len(args) == 0 {
		gistFiles[gistFilename] = FromStdin(gistFiles, *extension)
	} else {
		gistFiles[gistFilename] = FromFiles(gistFiles, args, *extension)
	}

	gist := github.Gist{
		Files:  gistFiles,
		Public: public,
	}

	g, _, err := client.Gists.Create(&gist)
	if err == nil {
		fmt.Printf("New gist created: %s\n", *g.HTMLURL)
		os.Exit(0)
	} else {
		fmt.Printf("Error creating new gist: %v\n", err)
		os.Exit(1)
	}
}
<commit_msg>Just print out the new url<commit_after>/*
	Program that acts similar to cat()
	except instead of using stdout it creates
	a private gist in Github

	Uses an environment variable GITHUB_API to
	determine the users api key.
*/

package main

import (
	"bytes"
	"flag"
	"fmt"
	"io"
	"os"

	"code.google.com/p/goauth2/oauth"
	"github.com/google/go-github/github"
)

const tokenEnv = "GITHUB_API"

type GistFiles map[github.GistFilename]github.GistFile

// Create GistFiles from stdin
func FromStdin(gistFiles GistFiles, fExt string) github.GistFile {
	var stdOutBuff bytes.Buffer

	_, err := io.Copy(&stdOutBuff, os.Stdin)
	if err != nil {
		fmt.Printf("Error copying stdin to buffer: %v\n", err)
		os.Exit(1)
	}

	gistStr := stdOutBuff.String()
	return github.GistFile{Content: &gistStr}
}

// Cats file content together into a gistFile
func FromFiles(gistFiles GistFiles, files []string, fExt string) github.GistFile {
	var stdOutBuff bytes.Buffer

	for _, fileName := range files {
		file, err := os.Open(fileName)
		if err != nil {
			fmt.Printf("Error opening file %s: %v", fileName, err)
			os.Exit(1)
		}

		if _, err := io.Copy(&stdOutBuff, file); err != nil {
			fmt.Printf("Error copying file contents to buffer: %v\n", err)
		}
	}

	gistStr := stdOutBuff.String()
	return github.GistFile{Content: &gistStr}
}

func main() {
	var public = flag.Bool("public", false, "makes the gist public")
	var extension = flag.String("ext", "txt", "file extension to use")
	flag.Parse()

	apiToken := os.Getenv(tokenEnv)
	if apiToken == "" {
		fmt.Println("must set environment variable $GITHUB_API")
		os.Exit(1)
	}

	t := &oauth.Transport{
		Token: &oauth.Token{AccessToken: apiToken},
	}
	client := github.NewClient(t.Client())

	args := flag.Args()
	gistFiles := make(GistFiles)
	gistFilename := github.GistFilename(fmt.Sprintf("gistfile1.%s", *extension))
	if len(args) == 0 {
		gistFiles[gistFilename] = FromStdin(gistFiles, *extension)
	} else {
		gistFiles[gistFilename] = FromFiles(gistFiles, args, *extension)
	}

	gist := github.Gist{
		Files:  gistFiles,
		Public: public,
	}

	g, _, err := client.Gists.Create(&gist)
	if err == nil {
		fmt.Println(*g.HTMLURL)
		os.Exit(0)
	} else {
		fmt.Printf("Error creating new gist: %v\n", err)
		os.Exit(1)
	}
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"os"

	"github.com/aws/aws-sdk-go/aws/credentials"
	"github.com/spf13/cobra"
)

func main() {
	envCmd := &cobra.Command{
		Use:   "env",
		Short: "export AWS variables from an AWS_PROFILE",
		Run: func(cmd *cobra.Command, args []string) {
			if os.Getenv("AWS_PROFILE") != "" {
				profile := os.Getenv("AWS_PROFILE")

				credsValue, err := credentials.NewSharedCredentials("", profile).Get()

				if err != nil {
					fmt.Printf("No exported. Review your AWS credentials for %s", profile)
					os.Exit(1)
				}

				fmt.Printf("export AWS_ACCESS_KEY_ID=%s\n", credsValue.AccessKeyID)
				fmt.Printf("export AWS_SECRET_ACCESS_KEY=%s\n", credsValue.SecretAccessKey)

				fmt.Printf("# Run this command to set the variables:\n")
				fmt.Printf("eval $(aws-profile env)\n")
			}
		},
	}
	unenvCmd := &cobra.Command{
		Use:   "unenv",
		Short: "unset AWS variables",
		Run: func(cmd *cobra.Command, args []string) {
			fmt.Printf("unset AWS_ACCESS_KEY_ID\n")
			fmt.Printf("unset AWS_SECRET_ACCESS_KEY\n")
			fmt.Printf("unset AWS_PROFILE\n")

			fmt.Printf("# Run this command to unset the variables:\n")
			fmt.Printf("eval $(aws-profile unenv)\n")
		},
	}

	rootCmd := &cobra.Command{Use: "aws-profile"}
	rootCmd.AddCommand(envCmd, unenvCmd)

	if err := rootCmd.Execute(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}
<commit_msg>Comment eval<commit_after>package main

import (
	"fmt"
	"os"

	"github.com/aws/aws-sdk-go/aws/credentials"
	"github.com/spf13/cobra"
)

func main() {
	envCmd := &cobra.Command{
		Use:   "env",
		Short: "export AWS variables from an AWS_PROFILE",
		Run: func(cmd *cobra.Command, args []string) {
			if os.Getenv("AWS_PROFILE") != "" {
				profile := os.Getenv("AWS_PROFILE")

				credsValue, err := credentials.NewSharedCredentials("", profile).Get()

				if err != nil {
					fmt.Printf("No exported. Review your AWS credentials for %s\n", profile)
					os.Exit(1)
				}

				fmt.Printf("export AWS_ACCESS_KEY_ID=%s\n", credsValue.AccessKeyID)
				fmt.Printf("export AWS_SECRET_ACCESS_KEY=%s\n", credsValue.SecretAccessKey)

				fmt.Printf("# Run this command to set the variables:\n")
				fmt.Printf("# eval $(aws-profile env)\n")
			}
		},
	}
	unenvCmd := &cobra.Command{
		Use:   "unenv",
		Short: "unset AWS variables",
		Run: func(cmd *cobra.Command, args []string) {
			fmt.Printf("unset AWS_ACCESS_KEY_ID\n")
			fmt.Printf("unset AWS_SECRET_ACCESS_KEY\n")
			fmt.Printf("unset AWS_PROFILE\n")

			fmt.Printf("# Run this command to unset the variables:\n")
			fmt.Printf("eval $(aws-profile unenv)\n")
		},
	}

	rootCmd := &cobra.Command{Use: "aws-profile"}
	rootCmd.AddCommand(envCmd, unenvCmd)

	if err := rootCmd.Execute(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}
<|endoftext|>


<commit_before>package main

import (
	"crypto/rand"
	"crypto/rsa"
	"crypto/x509"
	"encoding/json"
	"encoding/pem"
	"github.com/kofemann/autoca/ca"
	"github.com/kofemann/autoca/config"
	"log"
	"net/http"
	"os"
	"strconv"
	"time"
)

type CertificateResponse struct {
	Cert string `json:"cert"`
	Key  string `json:"Key"`
}

type WebCa struct {
	ca   *autoca.AutoCA
	conf *config.Conf
}

func (webca *WebCa) handle(rw http.ResponseWriter, req *http.Request) {

	t := webca.ca.GetCertificateTemplate("localhost", time.Now(), time.Now().AddDate(0, 0, webca.conf.Cert.Days))

	privatekey, err := rsa.GenerateKey(rand.Reader, 2048)
	if err != nil {
		http.Error(rw, err.Error(), http.StatusInternalServerError)
	}

	publickey := &privatekey.PublicKey

	x, err := webca.ca.CreateCertificate(t, publickey)
	if err != nil {
		http.Error(rw, err.Error(), http.StatusInternalServerError)
	}

	certOut := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: x})
	keyOut := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privatekey)})

	cert := CertificateResponse{
		Cert: string(certOut),
		Key:  string(keyOut),
	}

	msg, err := json.Marshal(cert)
	if err != nil {
		http.Error(rw, err.Error(), http.StatusInternalServerError)
	}
	rw.Write(msg)
}

func main() {

	conf, err := config.GetConf()
	if err != nil {
		os.Exit(1)
	}

	ca := &autoca.AutoCA{}
	err = ca.Init(conf.CA.CertFile, conf.CA.KeyFile, conf.CA.KeyPass)
	if err != nil {
		os.Exit(2)
	}

	webca := &WebCa{ca: ca, conf: conf}
	http.HandleFunc("/certificate", webca.handle)
	err = http.ListenAndServe(":"+strconv.Itoa(conf.Web.Port), nil)
	if err != nil {
		log.Fatal("Failed to start server:", err)
	}
}
<commit_msg>web: dunamically discover host name<commit_after>package main

import (
	"crypto/rand"
	"crypto/rsa"
	"crypto/x509"
	"encoding/json"
	"encoding/pem"
	"github.com/kofemann/autoca/ca"
	"github.com/kofemann/autoca/config"
	"log"
	"net"
	"net/http"
	"os"
	"strconv"
	"time"
)

type CertificateResponse struct {
	Cert string `json:"cert"`
	Key  string `json:"Key"`
}

type WebCa struct {
	ca   *autoca.AutoCA
	conf *config.Conf
}

func (webca *WebCa) handle(rw http.ResponseWriter, req *http.Request) {

	host, _, err := net.SplitHostPort(req.RemoteAddr)
	if err != nil {
		http.Error(rw, err.Error(), http.StatusInternalServerError)
	}

	hostNames, err := net.LookupAddr(host)
	if err != nil {
		http.Error(rw, err.Error(), http.StatusInternalServerError)
	}

	t := webca.ca.GetCertificateTemplate(hostNames[0], time.Now(), time.Now().AddDate(0, 0, webca.conf.Cert.Days))

	privatekey, err := rsa.GenerateKey(rand.Reader, 2048)
	if err != nil {
		http.Error(rw, err.Error(), http.StatusInternalServerError)
	}

	publickey := &privatekey.PublicKey

	x, err := webca.ca.CreateCertificate(t, publickey)
	if err != nil {
		http.Error(rw, err.Error(), http.StatusInternalServerError)
	}

	certOut := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: x})
	keyOut := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privatekey)})

	cert := CertificateResponse{
		Cert: string(certOut),
		Key:  string(keyOut),
	}

	msg, err := json.Marshal(cert)
	if err != nil {
		http.Error(rw, err.Error(), http.StatusInternalServerError)
	}
	rw.Write(msg)
}

func main() {

	conf, err := config.GetConf()
	if err != nil {
		os.Exit(1)
	}

	ca := &autoca.AutoCA{}
	err = ca.Init(conf.CA.CertFile, conf.CA.KeyFile, conf.CA.KeyPass)
	if err != nil {
		os.Exit(2)
	}

	webca := &WebCa{ca: ca, conf: conf}
	http.HandleFunc("/certificate", webca.handle)
	err = http.ListenAndServe(":"+strconv.Itoa(conf.Web.Port), nil)
	if err != nil {
		log.Fatal("Failed to start server:", err)
	}
}
<|endoftext|>


<commit_before>package main

import (
	"context"
	"fmt"
	"os"

	"github.com/google/go-github/github"
	"github.com/urfave/cli"
	"golang.org/x/oauth2"
)

func main() {
	var lang, sort, direction string
	app := cli.NewApp()
	app.Version = "1.0"
	app.Author = "monmaru"
	app.UsageText = "$ ghstar <GitHub User Name>"
	app.Flags = []cli.Flag{
		cli.StringFlag{
			Name:        "lang, l",
			Usage:       "Filter the repository by the name of the programming language",
			Destination: &lang,
		},
		cli.StringFlag{
			Name:        "sort, s",
			Usage:       "Sort. You can specify either created, updated or pushed",
			Value:       "created",
			Destination: &sort,
		},
		cli.StringFlag{
			Name:        "direction, d",
			Usage:       "Sorting direction. You can specify either desc or asc.",
			Value:       "desc",
			Destination: &direction,
		},
	}
	app.Action = func(c *cli.Context) error {
		if c.NArg() == 0 {
			fmt.Print(c.App.UsageText)
			return nil
		}

		params := &params{
			lang:      lang,
			sort:      sort,
			direction: direction,
		}
		return listRepositories(c.Args().First(), params)
	}
	app.Run(os.Args)
}

type params struct {
	lang, sort, direction string
}

func listRepositories(user string, params *params) error {
	client := newGitHubClient()
	opts := &github.ActivityListStarredOptions{
		Sort:      params.sort,
		Direction: params.direction,
	}
	opts.Page = 1
	opts.PerPage = 50

	for {
		starredRepos, res, err := client.Activity.ListStarred(context.Background(), user, opts)
		if err != nil {
			return err
		}

		for _, r := range starredRepos {
			repo := *r.Repository
			if !isEmpty(params.lang) && ((repo.Language == nil) || (*repo.Language != params.lang)) {
				continue
			}

			show(repo)
		}

		if res.NextPage == 0 {
			break
		}
		opts.Page = res.NextPage
	}
	return nil
}

func newGitHubClient() *github.Client {
	token := os.Getenv("GITHUB_API_TOKEN")
	if isEmpty(token) {
		return github.NewClient(nil)
	}
	ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
	return github.NewClient(oauth2.NewClient(context.Background(), ts))
}

func show(repo github.Repository) {
	fmt.Println("-----------------------------------------")
	fmt.Println(*repo.HTMLURL)
	if repo.Description != nil {
		fmt.Println(*repo.Description)
	}
	if repo.Language != nil {
		fmt.Printf("[Lang:%v] ", *repo.Language)
	}
	if repo.StargazersCount != nil {
		fmt.Printf("[Star:%v] ", *repo.StargazersCount)
	}
	if repo.ForksCount != nil {
		fmt.Printf("[Fork:%v] ", *repo.ForksCount)
	}
	if repo.UpdatedAt != nil {
		fmt.Printf("[Updated at %v]", *repo.UpdatedAt)
	}
	fmt.Println("")
}

func isEmpty(s string) bool { return len(s) == 0 }
<commit_msg>Supported colorized outputs<commit_after>package main

import (
	"context"
	"fmt"
	"os"

	"github.com/fatih/color"
	"github.com/google/go-github/github"
	"github.com/urfave/cli"
	"golang.org/x/oauth2"
)

func main() {
	var lang, sort, direction string
	app := cli.NewApp()
	app.Version = "1.0"
	app.Author = "monmaru"
	app.UsageText = "$ ghstar <GitHub User Name>"
	app.Flags = []cli.Flag{
		cli.StringFlag{
			Name:        "lang, l",
			Usage:       "Filter the repository by the name of the programming language",
			Destination: &lang,
		},
		cli.StringFlag{
			Name:        "sort, s",
			Usage:       "Sort. You can specify either created, updated or pushed",
			Value:       "created",
			Destination: &sort,
		},
		cli.StringFlag{
			Name:        "direction, d",
			Usage:       "Sorting direction. You can specify either desc or asc.",
			Value:       "desc",
			Destination: &direction,
		},
	}
	app.Action = func(c *cli.Context) error {
		if c.NArg() == 0 {
			fmt.Print(c.App.UsageText)
			return nil
		}

		params := &params{
			lang:      lang,
			sort:      sort,
			direction: direction,
		}
		return listRepositories(c.Args().First(), params)
	}
	app.Run(os.Args)
}

type params struct {
	lang, sort, direction string
}

func listRepositories(user string, params *params) error {
	client := newGitHubClient()
	opts := &github.ActivityListStarredOptions{
		Sort:      params.sort,
		Direction: params.direction,
	}
	opts.Page = 1
	opts.PerPage = 50

	for {
		starredRepos, res, err := client.Activity.ListStarred(context.Background(), user, opts)
		if err != nil {
			return err
		}

		for _, r := range starredRepos {
			repo := *r.Repository
			if !isEmpty(params.lang) && ((repo.Language == nil) || (*repo.Language != params.lang)) {
				continue
			}

			show(repo)
		}

		if res.NextPage == 0 {
			break
		}
		opts.Page = res.NextPage
	}
	return nil
}

func newGitHubClient() *github.Client {
	token := os.Getenv("GITHUB_API_TOKEN")
	if isEmpty(token) {
		return github.NewClient(nil)
	}
	ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
	return github.NewClient(oauth2.NewClient(context.Background(), ts))
}

func show(repo github.Repository) {
	fmt.Println("-----------------------------------------")
	color.Green(*repo.HTMLURL)
	if repo.Description != nil {
		fmt.Println(*repo.Description)
	}
	if repo.Language != nil {
		fmt.Printf("[Lang:%v] ", *repo.Language)
	}
	if repo.StargazersCount != nil {
		fmt.Printf("[Star:%v] ", *repo.StargazersCount)
	}
	if repo.ForksCount != nil {
		fmt.Printf("[Fork:%v] ", *repo.ForksCount)
	}
	if repo.UpdatedAt != nil {
		fmt.Printf("[Updated at %v]", *repo.UpdatedAt)
	}
	fmt.Println("")
}

func isEmpty(s string) bool { return len(s) == 0 }
<|endoftext|>


<commit_before>package main

import (
	"database/sql"
	"flag"
	"fmt"
	"github.com/matee911/go-sync2async/cfg"
	"github.com/matee911/go-sync2async/db"
	//"github.com/matee911/go-sync2async/judge"
	"github.com/matee911/go-sync2async/logging"
	//"github.com/matee911/go-sync2async/transaction"
	"github.com/matee911/go-sync2async/dvs"
	"io"
	"log"
	"net/http"
	"os"
	"time"
)

type Request struct {
	resultChan    chan string
	TransactionId int
}

var (
	config cfg.Config
	dbConn *sql.DB
)

const (
	userAgent string = "NProxy/0.1a"
)

func parseArguments() (configPath string, dropSequence bool) {
	flag.StringVar(&configPath, "config", "sync2async.json", "Path to configuration file")
	flag.BoolVar(&dropSequence, "dropsequence", false, "Drop TransactionID sequence")
	flag.Parse()
	return
}

func init() {
	var err error
	configPath, dropSequence := parseArguments()
	config.ReadFromJson(configPath)

	// Prepare DB
	dbConn, err := db.Connect(&config)
	if err != nil {
		log.Println(err)
		os.Exit(1)
	}

	prepErr := db.PrepareDb(dbConn, dropSequence)
	if prepErr != nil {
		log.Println(prepErr)
		os.Exit(1)
	} else if (prepErr == nil) && dropSequence {
		os.Exit(0)
	}
}

func licenseHttpHandler(commandsChannel chan dvs.Command) func(http.ResponseWriter, *http.Request) {
	return func(res http.ResponseWriter, req *http.Request) {
		defer logging.HttpRequest(time.Now(), req)
		req.ParseForm()

		res.Header().Set("Content-Type", "application/json")
		res.Header().Set("Server", userAgent)

		// NUID (Nagra Unique IDentifier) [int, max 10-digits long]
		address, err := validateAddress(req.PostForm.Get("address"))
		if err != nil {
			http.Error(res, CreateErrorResponse(1, "invalid address", err.Error()).String(), 400)
			return
		}

		// Chipset Type String (please refer to DVS documentation)
		// [text in format: xxxx xxxx xxxx xxxx xx]
		chipset_type_string, err := validateChipset(req.PostForm.Get("chipset_type_string"))
		if err != nil {
			http.Error(res, CreateErrorResponse(1, "invalid chipset", err.Error()).String(), 400)
			return
		}

		// Content identifier [int, max 9-digits long]
		content_id, err := validateContent(req.PostForm.Get("content_id"))
		if err != nil {
			http.Error(res, CreateErrorResponse(1, "invalid content", err.Error()).String(), 400)
			return
		}
		// any binary string data, that will be sent to authorization module
		// for additional authorization (eg. user credentials, token)
		extra := req.PostForm.Get("extra")

		log.Printf("addr: %d chip: %s content: %s extra: %s", address, chipset_type_string, content_id, extra)

		commandsChannel <- dvs.NoCommand()

		//judge.AskForPermission("ala", &config)

		/*
		   errcode - high-level error code
		     400
		     403
		     500
		*/

	}
}

func main() {

	commandsChannel := make(chan dvs.Command, 100)
	master := dvs.Master{SourceIds: config.SourceIds, DestinationId: config.DestinationId, MopPpid: config.MopPpid, Address: config.DVSAddr, CommandsChannel: commandsChannel}
	master.Connect()
	// Close?

	//	http.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
	//		commandsChannel <- dvs.NoCommand()
	//		io.WriteString(res, "Hello")
	//	})

	// Heartbeat
	heartbeat := time.NewTicker(time.Second * time.Duration(config.Heartbeat))
	defer heartbeat.Stop()
	go func() {
		for t := range heartbeat.C {
			//Ping(&connection, &config)
			log.Printf("Heartbeat: %v", t)
		}
	}()

	ping_http_handler := func(res http.ResponseWriter, req *http.Request) {
		defer logging.HttpRequest(time.Now(), req)

		//transactionId := transaction.GetId(dbConn)
		//request := Request{resultChan: make(chan string), TransactionId: transactionId}
		//mapping[transactionId] = &request

		// change to CallDVS
		//go func(request *Request) {
		//	connection.Write(dvs.NoCommand(1, 1, 1, 1))
		//	request.resultChan <- strconv.Itoa(request.TransactionId)
		//}(&request)

		res.Header().Set("Content-Type", "text/plain")
		res.Header().Set("Server", userAgent)
		//select {
		//case r := <-request.resultChan:
		//	io.WriteString(res, r)
		// TODO(m): ladowanie czasu z konfigu i castowanie
		//case <-time.After(time.Duration(config.Timeout) * time.Second):
		//	io.WriteString(res, "zepsute")
		//}
		io.WriteString(res, "io")
	}

	http.HandleFunc("/license", licenseHttpHandler(commandsChannel))
	http.HandleFunc("/ping", ping_http_handler)
	addr := fmt.Sprintf(":%v", config.Port)
	log.Printf("Listening on port %v", config.Port)
	log.Fatal(http.ListenAndServe(addr, nil))
	os.Exit(1)
}
<commit_msg>cleanup<commit_after>package main

import (
	"database/sql"
	"flag"
	"fmt"
	"github.com/matee911/go-sync2async/cfg"
	"github.com/matee911/go-sync2async/db"
	//"github.com/matee911/go-sync2async/judge"
	"github.com/matee911/go-sync2async/logging"
	//"github.com/matee911/go-sync2async/transaction"
	"github.com/matee911/go-sync2async/dvs"
	"io"
	"log"
	"net/http"
	"os"
	"time"
)

type Request struct {
	resultChan    chan string
	TransactionId int
}

var (
	config cfg.Config
	dbConn *sql.DB
)

const (
	userAgent string = "NProxy/0.1a"
)

func parseArguments() (configPath string, dropSequence bool) {
	flag.StringVar(&configPath, "config", "sync2async.json", "Path to configuration file")
	flag.BoolVar(&dropSequence, "dropsequence", false, "Drop TransactionID sequence")
	flag.Parse()
	return
}

func init() {
	var err error
	configPath, dropSequence := parseArguments()
	config.ReadFromJson(configPath)

	// Prepare DB
	dbConn, err := db.Connect(&config)
	if err != nil {
		log.Println(err)
		os.Exit(1)
	}

	prepErr := db.PrepareDb(dbConn, dropSequence)
	if prepErr != nil {
		log.Println(prepErr)
		os.Exit(1)
	} else if (prepErr == nil) && dropSequence {
		os.Exit(0)
	}
}

func licenseHttpHandler(commandsChannel chan dvs.Command) func(http.ResponseWriter, *http.Request) {
	return func(res http.ResponseWriter, req *http.Request) {
		defer logging.HttpRequest(time.Now(), req)
		req.ParseForm()

		res.Header().Set("Content-Type", "application/json")
		res.Header().Set("Server", userAgent)

		// NUID (Nagra Unique IDentifier) [int, max 10-digits long]
		address, err := validateAddress(req.PostForm.Get("address"))
		if err != nil {
			http.Error(res, CreateErrorResponse(1, "invalid address", err.Error()).String(), 400)
			return
		}

		// Chipset Type String (please refer to DVS documentation)
		// [text in format: xxxx xxxx xxxx xxxx xx]
		chipset_type_string, err := validateChipset(req.PostForm.Get("chipset_type_string"))
		if err != nil {
			http.Error(res, CreateErrorResponse(1, "invalid chipset", err.Error()).String(), 400)
			return
		}

		// Content identifier [int, max 9-digits long]
		content_id, err := validateContent(req.PostForm.Get("content_id"))
		if err != nil {
			http.Error(res, CreateErrorResponse(1, "invalid content", err.Error()).String(), 400)
			return
		}
		// any binary string data, that will be sent to authorization module
		// for additional authorization (eg. user credentials, token)
		extra := req.PostForm.Get("extra")

		log.Printf("addr: %d chip: %s content: %s extra: %s", address, chipset_type_string, content_id, extra)

		commandsChannel <- dvs.NoCommand()

		//judge.AskForPermission("ala", &config)

		/*
		   errcode - high-level error code
		     400
		     403
		     500
		*/
	}
}

func pingHttpHandler(commandsChannel chan dvs.Command) func(http.ResponseWriter, *http.Request) {
	return func(res http.ResponseWriter, req *http.Request) {
		defer logging.HttpRequest(time.Now(), req)
		req.ParseForm()

		res.Header().Set("Content-Type", "application/json")
		res.Header().Set("Server", userAgent)

		commandsChannel <- dvs.NoCommand()
		io.WriteString(res, "Hello")

		//select {
		//case r := <-request.resultChan:
		//	io.WriteString(res, r)
		// TODO(m): ladowanie czasu z konfigu i castowanie
		//case <-time.After(time.Duration(config.Timeout) * time.Second):
		//	io.WriteString(res, "zepsute")
		//}
	}
}

func main() {

	commandsChannel := make(chan dvs.Command, 100)
	master := dvs.Master{SourceIds: config.SourceIds, DestinationId: config.DestinationId, MopPpid: config.MopPpid, Address: config.DVSAddr, CommandsChannel: commandsChannel}
	master.Connect()
	// Close?

	// Heartbeat
	heartbeat := time.NewTicker(time.Second * time.Duration(config.Heartbeat))
	defer heartbeat.Stop()
	go func() {
		for t := range heartbeat.C {
			//Ping(&connection, &config)
			commandsChannel <- dvs.NoCommand()
			log.Printf("Heartbeat: %v", t)
		}
	}()

	// Registered handlers
	http.HandleFunc("/license", licenseHttpHandler(commandsChannel))
	http.HandleFunc("/ping", pingHttpHandler(commandsChannel))

	addr := fmt.Sprintf(":%v", config.Port)
	log.Printf("Listening on port %v", config.Port)
	log.Fatal(http.ListenAndServe(addr, nil))
	os.Exit(1)
}
<|endoftext|>


<commit_before>// Copyright 2015 Christian Gärtner. All rights reserved.
// Use of this source code is governed by a MIT license
// that can be found in the LICENSE file.

package main

import (
	"flag"
	"fmt"
	"os"
	"github.com/achtern/kluver/compiler"
)

func main() {

	includePath := flag.String("include-path", "./", "path prefix for all imported libs")
	flag.Parse()

	tail := flag.Args();

	if len(tail) == 0 {
		fmt.Fprintf(os.Stderr, "missing path to shader source file\n")
		os.Exit(1)
	}

	shader, err := compiler.New("example.shader", *includePath, compiler.FileLoader{})
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println("VERTEX--")
	fmt.Println(shader.GetVertex())
	fmt.Println("FRAGMENT--")
	fmt.Println(shader.GetFragment())
}
<commit_msg>the provided path argument for the inital .shader file is now being used<commit_after>// Copyright 2015 Christian Gärtner. All rights reserved.
// Use of this source code is governed by a MIT license
// that can be found in the LICENSE file.

package main

import (
	"flag"
	"fmt"
	"os"
	"github.com/achtern/kluver/compiler"
)

func main() {

	includePath := flag.String("include-path", "./", "path prefix for all imported libs")
	flag.Parse()

	tail := flag.Args();

	if len(tail) == 0 {
		fmt.Fprintf(os.Stderr, "missing path to shader source file\n")
		os.Exit(1)
	}

	shaderSourcePath := tail[0]

	shader, err := compiler.New(shaderSourcePath, *includePath, compiler.FileLoader{})
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println("VERTEX--")
	fmt.Println(shader.GetVertex())
	fmt.Println("FRAGMENT--")
	fmt.Println(shader.GetFragment())
}
<|endoftext|>


<commit_before>package main

import (
	"database/sql"
	"fmt"
	"log"
	"net/http"
	"os"

	"github.com/sotoz/Ferrytale/controller"
	"github.com/sotoz/Ferrytale/database"
)

// Config describes the configuration struct for the application.
type Config struct {
	Host string
	Port string
}

func main() {

	db, err := sql.Open("mysql", fmt.Sprintf("%s", os.Getenv("DATABASE_URL")))
	if err != nil {
		log.Fatalf("Could not open database: %s", err)
	}
	database.DBCon = db
	err = db.Ping()
	if err != nil {
		log.Fatalf("cannot connect to the database: %s", err)
	}
	defer db.Close()

	c := Config{
		Host: os.Getenv("APPLICATION_HOST"),
		Port: os.Getenv("APPLICATION_PORT"),
	}

	log.Print("Ferrytale started...")

	http.ListenAndServe(c.Host+":"+c.Port, controller.Router())
}
<commit_msg>log.Fatal at http.ListenAndServe<commit_after>package main

import (
	"database/sql"
	"fmt"
	"log"
	"net/http"
	"os"

	"github.com/sotoz/Ferrytale/controller"
	"github.com/sotoz/Ferrytale/database"
)

// Config describes the configuration struct for the application.
type Config struct {
	Host string
	Port string
}

func main() {

	db, err := sql.Open("mysql", fmt.Sprintf("%s", os.Getenv("DATABASE_URL")))
	if err != nil {
		log.Fatalf("Could not open database: %s", err)
	}
	database.DBCon = db
	err = db.Ping()
	if err != nil {
		log.Fatalf("cannot connect to the database: %s", err)
	}
	defer db.Close()

	c := Config{
		Host: os.Getenv("APPLICATION_HOST"),
		Port: os.Getenv("APPLICATION_PORT"),
	}

	log.Print("Ferrytale started...")

	log.Fatal(http.ListenAndServe(c.Host+":"+c.Port, controller.Router()))
}
<|endoftext|>


<commit_before>package main

import (
	"encoding/json"
	"fmt"
	"io"
	"log"
	"os"
	"strconv"
	"syscall"
	"time"

	flags "github.com/jessevdk/go-flags"
)

type Request struct {
	Execute   string                 `json:"execute"`
	Arguments map[string]interface{} `json:"arguments,omitempty"`
}

type Response struct {
	Return interface{} `json:"return"`
}

func (r *Request) Write(b []byte) (int, error) {
	fmt.Printf("%s\n", b)
	return len(b), nil
}

var finish bool = false

var ppid int

func init() {
	ppid = os.Getpid()
}

func main() {
	var req Request
	var f io.ReadWriteCloser

	parser := flags.NewParser(&options, flags.PrintErrors)
	_, err := parser.Parse()
	if err != nil {
		log.Printf(err.Error())
		os.Exit(1)
	}

	if options.Help {
		parser.WriteHelp(os.Stdout)
		os.Exit(0)
	}

	parent := os.Getenv("PARENT")
	if parent != "" {
		if pid, err := strconv.Atoi(parent); err == nil {
			if proc, err := os.FindProcess(pid); err == nil {
				err = proc.Kill()
				if err != nil {
					log.Printf(err.Error())
					os.Exit(1)
				}
			}
		}
	} else {
		pids := getPids("qemu-ga", true)
		err = syscall.Setpgid(0, 0)
		if err != nil {
			log.Printf("setpgid err: %s\n", err.Error())
		} else {
			syscall.Setsid()
			for _, pid := range pids {
				syscall.Kill(pid, syscall.SIGTERM)
			}
			time.Sleep(10 * time.Second)
		}
	}

	os.Chdir("/")
	syscall.Close(0)
	syscall.Close(1)
	syscall.Close(2)

	wait := 10
	for {
		f, err = os.OpenFile(options.Path, os.O_RDWR, os.FileMode(os.ModeCharDevice|0600))
		if err == nil {
			break
		}
		if wait < 0 {
			log.Fatal("Failed to open device:", err)
			os.Exit(1)
		}
		wait -= 1
		time.Sleep(5 * time.Second)
	}

	defer f.Close()
	dec := json.NewDecoder(f)
	dec.UseNumber()
	enc := json.NewEncoder(f)

	for {
		time.Sleep(500 * time.Millisecond)
		err = dec.Decode(&req)
		if err == nil {
			for _, cmd := range commands {
				if cmd.Name == req.Execute && cmd.Func != nil {
					go handle(enc, cmd.Func, req.Arguments)
				}
			}
		}

	}

}

func handle(enc *json.Encoder, fn func(map[string]interface{}) interface{}, args map[string]interface{}) {
	enc.Encode(fn(args))
}
<commit_msg>add version output<commit_after>package main

import (
	"encoding/json"
	"fmt"
	"io"
	"log"
	"os"
	"strconv"
	"syscall"
	"time"

	flags "github.com/jessevdk/go-flags"
)

type Request struct {
	Execute   string                 `json:"execute"`
	Arguments map[string]interface{} `json:"arguments,omitempty"`
}

type Response struct {
	Return interface{} `json:"return"`
}

func (r *Request) Write(b []byte) (int, error) {
	fmt.Printf("%s\n", b)
	return len(b), nil
}

var finish bool = false

var ppid int

func init() {
	ppid = os.Getpid()
}

func main() {
	var req Request
	var f io.ReadWriteCloser

	parser := flags.NewParser(&options, flags.PrintErrors)
	_, err := parser.Parse()
	if err != nil {
		fmt.Printf(err.Error())
		os.Exit(1)
	}

	if options.Help {
		parser.WriteHelp(os.Stdout)
		os.Exit(0)
	}

	if options.Version {
		fmt.Printf("%s\n", Version)
		os.Exit(0)
	}

	parent := os.Getenv("PARENT")
	if parent != "" {
		if pid, err := strconv.Atoi(parent); err == nil {
			if proc, err := os.FindProcess(pid); err == nil {
				err = proc.Kill()
				if err != nil {
					log.Printf(err.Error())
					os.Exit(1)
				}
			}
		}
	} else {
		pids := getPids("qemu-ga", true)
		err = syscall.Setpgid(0, 0)
		if err != nil {
			log.Printf("setpgid err: %s\n", err.Error())
		} else {
			syscall.Setsid()
			for _, pid := range pids {
				syscall.Kill(pid, syscall.SIGTERM)
			}
			time.Sleep(10 * time.Second)
		}
	}

	os.Chdir("/")
	syscall.Close(0)
	syscall.Close(1)
	syscall.Close(2)

	wait := 10
	for {
		f, err = os.OpenFile(options.Path, os.O_RDWR, os.FileMode(os.ModeCharDevice|0600))
		if err == nil {
			break
		}
		if wait < 0 {
			log.Fatal("Failed to open device:", err)
			os.Exit(1)
		}
		wait -= 1
		time.Sleep(5 * time.Second)
	}

	defer f.Close()
	dec := json.NewDecoder(f)
	dec.UseNumber()
	enc := json.NewEncoder(f)

	for {
		time.Sleep(500 * time.Millisecond)
		err = dec.Decode(&req)
		if err == nil {
			for _, cmd := range commands {
				if cmd.Name == req.Execute && cmd.Func != nil {
					go handle(enc, cmd.Func, req.Arguments)
				}
			}
		}

	}

}

func handle(enc *json.Encoder, fn func(map[string]interface{}) interface{}, args map[string]interface{}) {
	enc.Encode(fn(args))
}
<|endoftext|>


<commit_before>package main

// A little reading I need to do:
// https://tour.golang.org/concurrency/2
// https://blog.golang.org/pipelines

import (
	"flag"
	"fmt"
	"github.com/fsnotify/fsnotify"
	"log"
	"os"
	"os/exec"
	"os/signal"
	"time"
	"strings"
	"syscall"
//	"github.com/hackborn/ghost/ghost"
)

type Runner struct {
	Cmd exec.Cmd
}

// Take an arg of the pattern "-str" and
// clean it up -- trim hyphens and white space.
func cleanArg(s string) string {
	return strings.Trim(s, "-")
}

func makeArgs() map[string]string {
	// This is a hybrid of the raw os args and the flag parsing.
	tmp := make(map[string]*string)
	for _, a := range os.Args[1:] {
		i := strings.Index(a, "=")
		if i > -1 {
			key := cleanArg(a[:i])
			tmp[key] = flag.String(key, "", "")
		}
	}
	flag.Parse()

	// Compile the parsed flags into a map.
	m := make(map[string]string)
	for k, v := range tmp {
		m[k] = *v;
	}
	return m
}

func main() {
	args := makeArgs();
	fmt.Println("ARGS", args);

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

	c := make(chan os.Signal, 2)
    signal.Notify(c, os.Interrupt, syscall.SIGTERM)
    go func() {
        <-c
//        cleanup()
        fmt.Println("signal quit")
        os.Exit(1)
    }()
    /*
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)
	go func(){
	    for sig := range c {
	    	fmt.Println("GOT SIGNAL", sig)
	        // sig is a ^C, handle it
	        log.Fatal("signal")
	    }
	}()
	*/
	fmt.Println("after signal notify")

	go func() {
		for {
			select {
			case event := <-watcher.Events:
				log.Println("event:", event)
				if event.Op&fsnotify.Write == fsnotify.Write {
					log.Println("modified file:", event.Name)
				}
			case err := <-watcher.Errors:
				log.Println("error:", err)
			}
		}
	}()

	err = watcher.Add("C:/work/dev/go/src/github.com/hackborn/ghost")
	if err != nil {
		log.Fatal(err)
	}

	runBroomServer()

	done := make(chan bool)
	<-done
}

func buildBroomTools() {
	go func() {
		cmd := exec.Command("go", "build")
		cmd.Dir = "C:/work/dev/go/src/github.com/hackborn/hello"
		err := cmd.Start()
		if err != nil {
			log.Println("Build command Start() failed:", err)
			log.Fatal(err)
		} else {
			log.Printf("Waiting for build to finish...\n")
			err = cmd.Wait()
			log.Printf("Build finished with error: %v\n", err)
		}
	}()
}

func runBroomServer() {
	cmd := exec.Command("C:/work/dev/go/src/github.com/hackborn/broom_server/server/server.exe")
	cmd.Dir = "C:/work/dev/go/src/github.com/hackborn/broom_server/server"
	cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
//	cmd.Run()

    // How to stop:
    // http://stackoverflow.com/questions/11886531/terminating-a-process-started-with-os-exec-in-golang
	done := make(chan error, 1)
	go func() {
	    done <- cmd.Run()
	}()
	select {
	case <-time.After(10 * time.Second):
	    if err := cmd.Process.Kill(); err != nil {
	        log.Fatal("failed to kill: ", err)
	    }
	    log.Println("process killed as timeout reached")
	case err := <-done:
	    if err != nil {
	        log.Printf("process done with error = %v", err)
	    } else {
	        log.Print("process done gracefully without error")
	    }
	}
}

func runBroomServerOld() {
	go func() {
		cmd := exec.Command("C:/work/dev/go/src/github.com/hackborn/broom_server/server/server.exe")
		cmd.Dir = "C:/work/dev/go/src/github.com/hackborn/broom_server/server"
		cmd.Stdout = os.Stdout
	    cmd.Stderr = os.Stderr
		cmd.Run()
/*
		stdout, err := cmd.StdoutPipe()

		if err := cmd.Start(); err == nil {
			b, _ := ioutil.ReadAll(cmd.Stdout)
			fmt.Println("server:", string(b))
		}
		fmt.Println("done with server, err:", err)
*/
/*
//		if err == nil {
			err := cmd.Start()
//		}
		if err != nil {
			log.Println("runBroomServer() failed:", err)
			log.Fatal(err)
		} else {
			log.Printf("Waiting for runBroomServer to finish...\n")
			err = cmd.Wait()
			log.Printf("runBroomServer finished with error: %v\n", err)
		}
*/
	}()
}<commit_msg>Add setting the config to use from command line.<commit_after>package main

// A little reading I need to do:
// https://tour.golang.org/concurrency/2
// https://blog.golang.org/pipelines

import (
	"flag"
	"fmt"
	"github.com/fsnotify/fsnotify"
	"log"
	"os"
	"os/exec"
	"os/signal"
	"time"
	"strings"
	"syscall"
//	"github.com/hackborn/ghost/ghost"
)

type Runner struct {
	Cmd exec.Cmd
}

// Take an arg of the pattern "-str" and
// clean it up -- trim hyphens and white space.
func cleanArg(s string) string {
	return strings.Trim(s, "-")
}

func makeArgs() (string, map[string]string) {
	// This is a hybrid of the raw os args and the flag parsing.
	tmp := make(map[string]*string)
	for _, a := range os.Args[1:] {
		i := strings.Index(a, "=")
		if i > -1 {
			key := cleanArg(a[:i])
			tmp[key] = flag.String(key, "", "")
		}
	}
	flag.Parse()

	// Compile the parsed flags into a map.
	cfg := ""
	m := make(map[string]string)
	for k, v := range tmp {
		if k == "config" {
			cfg = *v;
		} else {
			m[k] = *v;
		}
	}
	return cfg, m
}

func main() {
	cfg, args := makeArgs();
	fmt.Println("CFG", cfg);
	fmt.Println("ARGS", args);

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

	c := make(chan os.Signal, 2)
    signal.Notify(c, os.Interrupt, syscall.SIGTERM)
    go func() {
        <-c
//        cleanup()
        fmt.Println("signal quit")
        os.Exit(1)
    }()
    /*
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)
	go func(){
	    for sig := range c {
	    	fmt.Println("GOT SIGNAL", sig)
	        // sig is a ^C, handle it
	        log.Fatal("signal")
	    }
	}()
	*/
	fmt.Println("after signal notify")

	go func() {
		for {
			select {
			case event := <-watcher.Events:
				log.Println("event:", event)
				if event.Op&fsnotify.Write == fsnotify.Write {
					log.Println("modified file:", event.Name)
				}
			case err := <-watcher.Errors:
				log.Println("error:", err)
			}
		}
	}()

	err = watcher.Add("C:/work/dev/go/src/github.com/hackborn/ghost")
	if err != nil {
		log.Fatal(err)
	}

	runBroomServer()

	done := make(chan bool)
	<-done
}

func buildBroomTools() {
	go func() {
		cmd := exec.Command("go", "build")
		cmd.Dir = "C:/work/dev/go/src/github.com/hackborn/hello"
		err := cmd.Start()
		if err != nil {
			log.Println("Build command Start() failed:", err)
			log.Fatal(err)
		} else {
			log.Printf("Waiting for build to finish...\n")
			err = cmd.Wait()
			log.Printf("Build finished with error: %v\n", err)
		}
	}()
}

func runBroomServer() {
	cmd := exec.Command("C:/work/dev/go/src/github.com/hackborn/broom_server/server/server.exe")
	cmd.Dir = "C:/work/dev/go/src/github.com/hackborn/broom_server/server"
	cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
//	cmd.Run()

    // How to stop:
    // http://stackoverflow.com/questions/11886531/terminating-a-process-started-with-os-exec-in-golang
	done := make(chan error, 1)
	go func() {
	    done <- cmd.Run()
	}()
	select {
	case <-time.After(10 * time.Second):
	    if err := cmd.Process.Kill(); err != nil {
	        log.Fatal("failed to kill: ", err)
	    }
	    log.Println("process killed as timeout reached")
	case err := <-done:
	    if err != nil {
	        log.Printf("process done with error = %v", err)
	    } else {
	        log.Print("process done gracefully without error")
	    }
	}
}

func runBroomServerOld() {
	go func() {
		cmd := exec.Command("C:/work/dev/go/src/github.com/hackborn/broom_server/server/server.exe")
		cmd.Dir = "C:/work/dev/go/src/github.com/hackborn/broom_server/server"
		cmd.Stdout = os.Stdout
	    cmd.Stderr = os.Stderr
		cmd.Run()
/*
		stdout, err := cmd.StdoutPipe()

		if err := cmd.Start(); err == nil {
			b, _ := ioutil.ReadAll(cmd.Stdout)
			fmt.Println("server:", string(b))
		}
		fmt.Println("done with server, err:", err)
*/
/*
//		if err == nil {
			err := cmd.Start()
//		}
		if err != nil {
			log.Println("runBroomServer() failed:", err)
			log.Fatal(err)
		} else {
			log.Printf("Waiting for runBroomServer to finish...\n")
			err = cmd.Wait()
			log.Printf("runBroomServer finished with error: %v\n", err)
		}
*/
	}()
}<|endoftext|>


<commit_before>package main

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

	"github.com/buildkite/buildkite-metrics/backend"
	"github.com/buildkite/buildkite-metrics/collector"
	"github.com/buildkite/buildkite-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")
		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")
		clwDimensions  = flag.String("cloudwatch-dimensions", "", "Cloudwatch dimensions to index metrics under, in the form of Key=Value, Other=Value")

		// filters
		queue = flag.String("queue", "", "Only include a specific queue")
	)

	flag.Parse()

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

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

	switch strings.ToLower(*backendOpt) {
	case "cloudwatch":
		dimensions, err := backend.ParseCloudWatchDimensions(*clwDimensions)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
		bk = backend.NewCloudWatchBackend(dimensions)
	case "statsd":
		var err error
		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)
	default:
		fmt.Println("Must provide a supported backend: cloudwatch, statsd, prometheus")
		os.Exit(1)
	}

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

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

	c := collector.Collector{
		UserAgent: userAgent,
		Endpoint:  *endpoint,
		Token:     *token,
		Queue:     *queue,
		Quiet:     *quiet,
		Debug:     *debug,
		DebugHttp: *debugHttp,
	}

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

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

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

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

	if err := f(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	if *interval > 0 {
		for _ = range time.NewTicker(*interval).C {
			if err := f(); err != nil {
				fmt.Println(err)
				os.Exit(1)
			}
		}
	}
}
<commit_msg>Use new package in main<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")
		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")
		clwDimensions  = flag.String("cloudwatch-dimensions", "", "Cloudwatch dimensions to index metrics under, in the form of Key=Value, Other=Value")

		// filters
		queue = flag.String("queue", "", "Only include a specific queue")
	)

	flag.Parse()

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

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

	switch strings.ToLower(*backendOpt) {
	case "cloudwatch":
		dimensions, err := backend.ParseCloudWatchDimensions(*clwDimensions)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
		bk = backend.NewCloudWatchBackend(dimensions)
	case "statsd":
		var err error
		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)
	default:
		fmt.Println("Must provide a supported backend: cloudwatch, statsd, prometheus")
		os.Exit(1)
	}

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

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

	c := collector.Collector{
		UserAgent: userAgent,
		Endpoint:  *endpoint,
		Token:     *token,
		Queue:     *queue,
		Quiet:     *quiet,
		Debug:     *debug,
		DebugHttp: *debugHttp,
	}

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

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

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

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

	if err := f(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	if *interval > 0 {
		for _ = range time.NewTicker(*interval).C {
			if err := f(); err != nil {
				fmt.Println(err)
				os.Exit(1)
			}
		}
	}
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"fmt"
	"log"
	"net/http"
	"regexp"
	"sort"
	"time"

	"github.com/gorilla/feeds"
	rss "github.com/jteeuwen/go-pkg-rss"
)

func main() {
	port := flag.String("port", "8080", "HTTP Port to listen on")
	flag.Parse()
	http.HandleFunc("/", rssHandler)
	log.Fatal(http.ListenAndServe(":"+*port, nil))
}

func rssHandler(rw http.ResponseWriter, r *http.Request) {
	master := &feeds.Feed{
		Title:       "thoughtbot",
		Link:        &feeds.Link{Href: "https://rss.thoughtbot.com"},
		Description: "All the thoughts fit to bot.",
		Author:      &feeds.Author{Name: "thoughtbot", Email: "hello@thoughtbot.com"},
		Created:     time.Now(),
	}

	for _, feed := range sourceFeeds {
		fetch(feed, master)
	}

	sort.Sort(byCreated(master.Items))

	result, _ := master.ToAtom()
	fmt.Fprintln(rw, result)
}

func fetch(feed sourceFeed, master *feeds.Feed) {
	fetcher := rss.New(5, true, chanHandler, makeHandler(master, feed.name))
	fetcher.Fetch(feed.uri, nil)
}

func chanHandler(feed *rss.Feed, newchannels []*rss.Channel) {
	// no need to do anything...
}

func makeHandler(master *feeds.Feed, sourceName string) rss.ItemHandlerFunc {
	return func(feed *rss.Feed, ch *rss.Channel, items []*rss.Item) {
		for i := 0; i < len(items); i++ {
			published, _ := items[i].ParsedPubDate()
			weekAgo := time.Now().AddDate(0, 0, -7)

			if published.After(weekAgo) {
				item := &feeds.Item{
					Title:       stripPodcastEpisodePrefix(items[i].Title),
					Link:        &feeds.Link{Href: items[i].Links[0].Href},
					Description: items[i].Description,
					Author:      &feeds.Author{Name: sourceName},
					Created:     published,
				}
				master.Add(item)
			}
		}
	}
}

type byCreated []*feeds.Item

type sourceFeed struct {
	uri  string
	name string
}

// sourceFeeds defines the list of thoughtbot RSS feeds
// to be combined into a master feed.
var sourceFeeds = []sourceFeed{
	{uri: "https://robots.thoughtbot.com/summaries.xml", name: "Giant Robots blog"},
	{uri: "http://simplecast.fm/podcasts/271/rss", name: "Giant Robots podcast"},
	{uri: "http://simplecast.fm/podcasts/272/rss", name: "Build Phase podcast"},
	{uri: "http://simplecast.fm/podcasts/282/rss", name: "The Bike Shed podcast"},
	{uri: "http://simplecast.fm/podcasts/1088/rss", name: "Tentative podcast"},
	{uri: "https://upcase.com/the-weekly-iteration.rss", name: "The Weekly Iteration videos"},
}

func (s byCreated) Len() int {
	return len(s)
}

func (s byCreated) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}

func (s byCreated) Less(i, j int) bool {
	return s[j].Created.Before(s[i].Created)
}

var podcastEpisodePrefix = regexp.MustCompile(`^\d+: `)

func stripPodcastEpisodePrefix(s string) string {
	return podcastEpisodePrefix.ReplaceAllString(s, "")
}
<commit_msg>Add HTTP timeout to RSS fetch<commit_after>package main

import (
	"flag"
	"fmt"
	"log"
	"net/http"
	"regexp"
	"sort"
	"time"

	"github.com/gorilla/feeds"
	rss "github.com/jteeuwen/go-pkg-rss"
)

func main() {
	port := flag.String("port", "8080", "HTTP Port to listen on")
	flag.Parse()
	http.HandleFunc("/", rssHandler)
	log.Fatal(http.ListenAndServe(":"+*port, nil))
}

func rssHandler(rw http.ResponseWriter, r *http.Request) {
	master := &feeds.Feed{
		Title:       "thoughtbot",
		Link:        &feeds.Link{Href: "https://rss.thoughtbot.com"},
		Description: "All the thoughts fit to bot.",
		Author:      &feeds.Author{Name: "thoughtbot", Email: "hello@thoughtbot.com"},
		Created:     time.Now(),
	}

	for _, feed := range sourceFeeds {
		fetch(feed, master)
	}

	sort.Sort(byCreated(master.Items))

	result, _ := master.ToAtom()
	fmt.Fprintln(rw, result)
}

func fetch(feed sourceFeed, master *feeds.Feed) {
	fetcher := rss.New(5, true, chanHandler, makeHandler(master, feed.name))
	client := &http.Client{
		Timeout: time.Second,
	}

	fetcher.FetchClient(feed.uri, client, nil)
}

func chanHandler(feed *rss.Feed, newchannels []*rss.Channel) {
	// no need to do anything...
}

func makeHandler(master *feeds.Feed, sourceName string) rss.ItemHandlerFunc {
	return func(feed *rss.Feed, ch *rss.Channel, items []*rss.Item) {
		for i := 0; i < len(items); i++ {
			published, _ := items[i].ParsedPubDate()
			weekAgo := time.Now().AddDate(0, 0, -7)

			if published.After(weekAgo) {
				item := &feeds.Item{
					Title:       stripPodcastEpisodePrefix(items[i].Title),
					Link:        &feeds.Link{Href: items[i].Links[0].Href},
					Description: items[i].Description,
					Author:      &feeds.Author{Name: sourceName},
					Created:     published,
				}
				master.Add(item)
			}
		}
	}
}

type byCreated []*feeds.Item

type sourceFeed struct {
	uri  string
	name string
}

// sourceFeeds defines the list of thoughtbot RSS feeds
// to be combined into a master feed.
var sourceFeeds = []sourceFeed{
	{uri: "https://robots.thoughtbot.com/summaries.xml", name: "Giant Robots blog"},
	{uri: "http://simplecast.fm/podcasts/271/rss", name: "Giant Robots podcast"},
	{uri: "http://simplecast.fm/podcasts/272/rss", name: "Build Phase podcast"},
	{uri: "http://simplecast.fm/podcasts/282/rss", name: "The Bike Shed podcast"},
	{uri: "http://simplecast.fm/podcasts/1088/rss", name: "Tentative podcast"},
	{uri: "https://upcase.com/the-weekly-iteration.rss", name: "The Weekly Iteration videos"},
}

func (s byCreated) Len() int {
	return len(s)
}

func (s byCreated) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}

func (s byCreated) Less(i, j int) bool {
	return s[j].Created.Before(s[i].Created)
}

var podcastEpisodePrefix = regexp.MustCompile(`^\d+: `)

func stripPodcastEpisodePrefix(s string) string {
	return podcastEpisodePrefix.ReplaceAllString(s, "")
}
<|endoftext|>


<commit_before>package main

import (
	"crypto/tls"
	"flag"
	"fmt"
	"log"
	"net"
	"os"
	"strings"

	"github.com/42wim/matterircd/config"
	irckit "github.com/42wim/matterircd/mm-go-irckit"
	"github.com/google/gops/agent"
	"github.com/sirupsen/logrus"
	"github.com/spf13/pflag"
	"github.com/spf13/viper"
)

var (
	version = "0.19.5-dev"
	githash string
	logger  *logrus.Entry
	v       *viper.Viper
)

func main() {
	if err := agent.Listen(agent.Options{}); err != nil {
		log.Fatal(err)
	}
	ourlog := logrus.New()
	ourlog.Formatter = &logrus.TextFormatter{FullTimestamp: true}
	logger = ourlog.WithFields(logrus.Fields{"module": "matterircd"})
	config.Logger = logger

	// config related. instantiate a new config.Config to store flags
	flagConfig := flag.String("conf", "matterircd.toml", "config file")

	// bools for showing version/enabling debug
	flag.Bool("version", false, "show version")
	flag.Bool("debug", false, "enable debug logging")

	// bind related cfg
	flag.String("bind", "127.0.0.1:6667", "interface:port to bind to, or a path to bind to a Unix socket.")

	// TLS related cfg
	flag.String("tlsbind", "", "interface:port to bind to. (e.g 127.0.0.1:6697)")
	flag.String("tlsdir", ".", "directory to look for key.pem and cert.pem.")
	flag.Parse()

	// Attempt to load values from the config file
	if _, err := os.Stat(*flagConfig); err == nil {
		v, err = config.LoadConfig(*flagConfig)
		if err != nil {
			log.Fatal(err)
		}
	} else {
		v = viper.New()
	}

	pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
	pflag.Parse()
	v.BindPFlags(pflag.CommandLine)

	if v.GetBool("debug") {
		logger.Info("enabling debug")
		ourlog.Level = logrus.DebugLevel
		irckit.SetLogLevel("debug")
	}

	if v.GetBool("version") {
		fmt.Printf("version: %s %s\n", version, githash)
		return
	}

	irckit.SetLogger(logger)

	logger.Infof("Running version %s %s", version, githash)
	if strings.Contains(version, "-dev") {
		logger.Infof("WARNING: THIS IS A DEVELOPMENT VERSION. Things may break.")
	}

	if v.GetString("tlsbind") != "" {
		go func() {
			logger.Infof("Listening on %s (TLS)", v.GetString("tlsbind"))
			socket := tlsbind()
			defer socket.Close()
			start(socket)
		}()
	}

	// backwards compatible

	if v.GetString("bind") != "" {
		go func() {
			var network string
			if strings.ContainsRune(v.GetString("bind"), os.PathSeparator) {
				network = "unix"
			} else {
				network = "tcp"
			}

			socket, err := net.Listen(network, v.GetString("bind"))
			if err != nil {
				logger.Errorf("Can not listen on %s: %v", v.GetString("bind"), err)
				os.Exit(1)
			}

			logger.Infof("Listening on %s", v.GetString("bind"))

			defer socket.Close()
			start(socket)
		}()
	}

	select {}
}

func tlsbind() net.Listener {
	cert, err := tls.LoadX509KeyPair(v.GetString("tlsdir")+"/cert.pem", v.GetString("tlsdir")+"/key.pem")
	if err != nil {
		logger.Errorf("could not load TLS, incorrect directory? Error: %s", err)
		os.Exit(1)
	}

	tlsConfig := tls.Config{Certificates: []tls.Certificate{cert}}
	listenerTLS, err := tls.Listen("tcp", v.GetString("tlsbind"), &tlsConfig)
	if err != nil {
		logger.Errorf("Can not listen on %s: %v\n", v.GetString("tlsbind"), err)
		os.Exit(1)
	}

	logger.Info("TLS listening on ", v.GetString("tlsbind"))

	return listenerTLS
}

func start(socket net.Listener) {
	for {
		conn, err := socket.Accept()
		if err != nil {
			logger.Errorf("Failed to accept connection: %v", err)
			return
		}

		go func() {
			newsrv := irckit.ServerConfig{Name: "matterircd", Version: version}.Server()

			logger.Infof("New connection: %s", conn.RemoteAddr())

			user := irckit.NewUserBridge(conn, newsrv, v)
			err = newsrv.Connect(user)
			if err != nil {
				logger.Errorf("Failed to join: %v", err)
				return
			}
		}()
	}
}
<commit_msg>Do not parse flag with stdlib anymore<commit_after>package main

import (
	"crypto/tls"
	"flag"
	"fmt"
	"log"
	"net"
	"os"
	"strings"

	"github.com/42wim/matterircd/config"
	irckit "github.com/42wim/matterircd/mm-go-irckit"
	"github.com/google/gops/agent"
	"github.com/sirupsen/logrus"
	"github.com/spf13/pflag"
	"github.com/spf13/viper"
)

var (
	version = "0.19.5-dev"
	githash string
	logger  *logrus.Entry
	v       *viper.Viper
)

func main() {
	if err := agent.Listen(agent.Options{}); err != nil {
		log.Fatal(err)
	}
	ourlog := logrus.New()
	ourlog.Formatter = &logrus.TextFormatter{FullTimestamp: true}
	logger = ourlog.WithFields(logrus.Fields{"module": "matterircd"})
	config.Logger = logger

	// config related. instantiate a new config.Config to store flags
	flagConfig := flag.String("conf", "matterircd.toml", "config file")

	// bools for showing version/enabling debug
	flag.Bool("version", false, "show version")
	flag.Bool("debug", false, "enable debug logging")

	// bind related cfg
	flag.String("bind", "127.0.0.1:6667", "interface:port to bind to, or a path to bind to a Unix socket.")

	// TLS related cfg
	flag.String("tlsbind", "", "interface:port to bind to. (e.g 127.0.0.1:6697)")
	flag.String("tlsdir", ".", "directory to look for key.pem and cert.pem.")

	// Attempt to load values from the config file
	if _, err := os.Stat(*flagConfig); err == nil {
		v, err = config.LoadConfig(*flagConfig)
		if err != nil {
			log.Fatal(err)
		}
	} else {
		v = viper.New()
	}

	pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
	pflag.Parse()
	v.BindPFlags(pflag.CommandLine)

	if v.GetBool("debug") {
		logger.Info("enabling debug")
		ourlog.Level = logrus.DebugLevel
		irckit.SetLogLevel("debug")
	}

	if v.GetBool("version") {
		fmt.Printf("version: %s %s\n", version, githash)
		return
	}

	irckit.SetLogger(logger)

	logger.Infof("Running version %s %s", version, githash)
	if strings.Contains(version, "-dev") {
		logger.Infof("WARNING: THIS IS A DEVELOPMENT VERSION. Things may break.")
	}

	if v.GetString("tlsbind") != "" {
		go func() {
			logger.Infof("Listening on %s (TLS)", v.GetString("tlsbind"))
			socket := tlsbind()
			defer socket.Close()
			start(socket)
		}()
	}

	// backwards compatible

	if v.GetString("bind") != "" {
		go func() {
			var network string
			if strings.ContainsRune(v.GetString("bind"), os.PathSeparator) {
				network = "unix"
			} else {
				network = "tcp"
			}

			socket, err := net.Listen(network, v.GetString("bind"))
			if err != nil {
				logger.Errorf("Can not listen on %s: %v", v.GetString("bind"), err)
				os.Exit(1)
			}

			logger.Infof("Listening on %s", v.GetString("bind"))

			defer socket.Close()
			start(socket)
		}()
	}

	select {}
}

func tlsbind() net.Listener {
	cert, err := tls.LoadX509KeyPair(v.GetString("tlsdir")+"/cert.pem", v.GetString("tlsdir")+"/key.pem")
	if err != nil {
		logger.Errorf("could not load TLS, incorrect directory? Error: %s", err)
		os.Exit(1)
	}

	tlsConfig := tls.Config{Certificates: []tls.Certificate{cert}}
	listenerTLS, err := tls.Listen("tcp", v.GetString("tlsbind"), &tlsConfig)
	if err != nil {
		logger.Errorf("Can not listen on %s: %v\n", v.GetString("tlsbind"), err)
		os.Exit(1)
	}

	logger.Info("TLS listening on ", v.GetString("tlsbind"))

	return listenerTLS
}

func start(socket net.Listener) {
	for {
		conn, err := socket.Accept()
		if err != nil {
			logger.Errorf("Failed to accept connection: %v", err)
			return
		}

		go func() {
			newsrv := irckit.ServerConfig{Name: "matterircd", Version: version}.Server()

			logger.Infof("New connection: %s", conn.RemoteAddr())

			user := irckit.NewUserBridge(conn, newsrv, v)
			err = newsrv.Connect(user)
			if err != nil {
				logger.Errorf("Failed to join: %v", err)
				return
			}
		}()
	}
}
<|endoftext|>


<commit_before>package main

import (
	"encoding/csv"
	"log"
	"os"
	"strconv"

	git2go "gopkg.in/libgit2/git2go.v25"
)

func main() {
	inputPath := "."
	if len(os.Args) > 1 && os.Args[1] != "" {
		inputPath = os.Args[1]
	}
	outputPath := "output.csv"
	if len(os.Args) > 2 && os.Args[2] != "" {
		outputPath = os.Args[2]
	}

	parseGitHistory(inputPath, outputPath)
}

func parseGitHistory(inputPath, outputPath string) {
	log.Println("Opening repo")
	repo, err := git2go.OpenRepository(inputPath)
	if err != nil {
		log.Fatal(err)
	}

	rv, err := repo.Walk()
	rv.Sorting(git2go.SortTime)
	rv.PushHead()

	log.Println("Parsing each commit in history")
	head, err := repo.Head()
	if err != nil {
		log.Fatal(err)
	}

	f, err := os.Create(outputPath)
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()
	w := csv.NewWriter(f)
	if err := w.Write(csvHeader); err != nil {
		log.Fatalln("error writing record to csv:", err)
	}

	gi := head.Target()
	for {
		commit, err := gitCommitFromOid(repo, gi)
		if err != nil {
			log.Fatal(err)
		}
		row := commitToCSVRow(commit)
		if err := w.Write(row); err != nil {
			log.Fatalln("error writing record to csv:", err)
		}

		// Go to next commit
		if err := rv.Next(gi); err != nil {
			break
		}
	}
	w.Flush()
	if err := w.Error(); err != nil {
		log.Fatal(err)
	}

	log.Println("Done.")
}

func gitCommitFromOid(repo *git2go.Repository, oid *git2go.Oid) (*git2go.Commit, error) {
	obj, err := repo.Lookup(oid)
	if err != nil {
		return nil, err
	}
	return obj.AsCommit()
}

var csvHeader = []string{
	"oid",
	"message",
	"time",
	"committer_name",
	"committer_email",
	"committer_time",
	"author_name",
	"author_email",
	"author_time",
	"parent_count",
}

func commitToCSVRow(c *git2go.Commit) []string {
	return []string{
		c.Object.Id().String(),      // oid
		c.Message(),                 // message
		c.Committer().When.String(), // time
		c.Committer().Name,          // committer_name
		c.Committer().Email,         // committer_email
		c.Committer().When.String(), // committer_time
		c.Author().Name,             // author_name
		c.Author().Email,            // author_email
		c.Author().When.String(),    // author_time

		strconv.FormatUint(uint64(c.ParentCount()), 10), // parent_count
	}
}
<commit_msg>Pass io.Writer to parseGitHistory<commit_after>package main

import (
	"encoding/csv"
	"io"
	"log"
	"os"
	"strconv"

	git2go "gopkg.in/libgit2/git2go.v25"
)

func main() {
	inputPath := "."
	if len(os.Args) > 1 && os.Args[1] != "" {
		inputPath = os.Args[1]
	}
	outputPath := "output.csv"
	if len(os.Args) > 2 && os.Args[2] != "" {
		outputPath = os.Args[2]
	}

	out, err := os.Create(outputPath)
	if err != nil {
		log.Fatal(err)
	}
	defer out.Close()

	parseGitHistory(inputPath, out)
}

func parseGitHistory(inputPath string, out io.Writer) {
	log.Println("Opening repo")
	repo, err := git2go.OpenRepository(inputPath)
	if err != nil {
		log.Fatal(err)
	}

	rv, err := repo.Walk()
	rv.Sorting(git2go.SortTime)
	rv.PushHead()

	log.Println("Parsing each commit in history")
	head, err := repo.Head()
	if err != nil {
		log.Fatal(err)
	}

	w := csv.NewWriter(out)
	if err := w.Write(csvHeader); err != nil {
		log.Fatalln("error writing record to csv:", err)
	}

	oid := head.Target()
	for {
		commit, err := gitCommitFromOid(repo, oid)
		if err != nil {
			log.Fatal(err)
		}

		row := commitToCSVRow(commit)
		if err := w.Write(row); err != nil {
			log.Fatalln("error writing record to csv:", err)
		}

		// Go to next commit
		if err := rv.Next(oid); err != nil {
			break
		}
	}

	w.Flush()
	if err := w.Error(); err != nil {
		log.Fatal(err)
	}

	log.Println("Done.")
}

func gitCommitFromOid(repo *git2go.Repository, oid *git2go.Oid) (*git2go.Commit, error) {
	obj, err := repo.Lookup(oid)
	if err != nil {
		return nil, err
	}
	return obj.AsCommit()
}

var csvHeader = []string{
	"oid",
	"message",
	"time",
	"committer_name",
	"committer_email",
	"committer_time",
	"author_name",
	"author_email",
	"author_time",
	"parent_count",
}

func commitToCSVRow(c *git2go.Commit) []string {
	return []string{
		c.Object.Id().String(),      // oid
		c.Message(),                 // message
		c.Committer().When.String(), // time
		c.Committer().Name,          // committer_name
		c.Committer().Email,         // committer_email
		c.Committer().When.String(), // committer_time
		c.Author().Name,             // author_name
		c.Author().Email,            // author_email
		c.Author().When.String(),    // author_time

		strconv.FormatUint(uint64(c.ParentCount()), 10), // parent_count
	}
}
<|endoftext|>


<commit_before>package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"go/ast"
	"go/parser"
	"go/token"
	"log"
	"os"
	"path/filepath"
	"regexp"
	"strings"
)

const usageDoc = `gogetimports: get a JSON-formatted list of imports

Usage:

  gogetimports ARGS <directory>

Flags:

  -only-third-parties  return only third party imports
`

var (
	flagThirdParties = flag.Bool("only-third-parties", false, "return only third party imports")

	// imports contains the list of import path.
	// filename:[]import path
	imports    = map[string][]string{}
	sourcePath = ""
)

func main() {
	flag.Usage = func() {
		fmt.Fprint(os.Stderr, usage)
	}
	flag.Parse()
	log.SetPrefix("gogetimports: ")

	args := flag.Args()
	if len(args) != 1 {
		usage()
	}
	sourcePath = args[0]

	if err := parseTree(); err != nil {
		log.Println(err)
		os.Exit(1)
	}

	enc := json.NewEncoder(os.Stdout)
	enc.Encode(imports)
}

func usage() {
	fmt.Fprintf(os.Stderr, usageDoc)
	os.Exit(1)
}

func parseTree() error {
	pathLen := len(sourcePath)
	// Parse recursively the given path if the recursive notation is found
	if pathLen >= 5 && sourcePath[pathLen-3:] == "..." {
		filepath.Walk(sourcePath[:pathLen-3], func(p string, f os.FileInfo, err error) error {
			if err != nil {
				log.Println(err)
				// resume walking
				return nil
			}

			if f.IsDir() {
				parseDir(p)
			}
			return nil
		})
	} else {
		parseDir(sourcePath)
	}
	return nil
}

func parseDir(dir string) error {
	fset := token.NewFileSet()
	pkgs, err := parser.ParseDir(fset, dir, nil, 0)
	if err != nil {
		return err
	}

	for _, pkg := range pkgs {
		for fn, f := range pkg.Files {
			ast.Walk(&ImportVisitor{
				packageName: pkg.Name,
				fileName:    fn,
			}, f)
		}
	}

	return nil
}

// ImportVisitor carries the package name and file name
// for passing it to the imports map.
type ImportVisitor struct {
	packageName, fileName string
}

// Visit matches the ImportSpec and fills the imports map.
func (v *ImportVisitor) Visit(node ast.Node) ast.Visitor {
	if node != nil {
		switch t := node.(type) {
		case *ast.ImportSpec:
			// Cleanup the import path
			path := strings.Replace(t.Path.Value, `"`, "", 2)

			if *flagThirdParties && !isThirdParty(path) {
				return v
			}

			_, ok := imports[v.fileName]
			if !ok {
				imports[v.fileName] = make([]string, 0)
			}

			imports[v.fileName] = append(imports[v.fileName], path)
		}
	}
	return v
}

// isThirdParty determines if the given import path is a third party or not.
// It's safe to assume that if the first path of the import path looks like a domain name,
// then we're dealing with a third party.
func isThirdParty(path string) bool {
	r, err := regexp.Compile(`^(\w+)\.(\w+)/`)
	if err != nil {
		return false
	}
	return r.MatchString(path)
}
<commit_msg>Refactor the imports parsing<commit_after>package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"go/parser"
	"go/token"
	"log"
	"os"
	"path/filepath"
	"regexp"
	"strings"
)

const usageDoc = `gogetimports: get a JSON-formatted list of imports

Usage:

  gogetimports ARGS <directory>

Flags:

  -only-third-parties  return only third party imports
`

var (
	flagThirdParties = flag.Bool("only-third-parties", false, "return only third party imports")

	// imports contains the list of import path.
	// filename:[]import path
	imports    = map[string][]string{}
	sourcePath = ""
)

func main() {
	flag.Usage = func() {
		fmt.Fprint(os.Stderr, usage)
	}
	flag.Parse()
	log.SetPrefix("gogetimports: ")

	args := flag.Args()
	if len(args) != 1 {
		usage()
	}
	sourcePath = args[0]

	if err := parseTree(); err != nil {
		log.Println(err)
		os.Exit(1)
	}

	enc := json.NewEncoder(os.Stdout)
	enc.Encode(imports)
}

func usage() {
	fmt.Fprintf(os.Stderr, usageDoc)
	os.Exit(1)
}

func parseTree() error {
	pathLen := len(sourcePath)
	// Parse recursively the given path if the recursive notation is found
	if pathLen >= 5 && sourcePath[pathLen-3:] == "..." {
		filepath.Walk(sourcePath[:pathLen-3], func(p string, f os.FileInfo, err error) error {
			if err != nil {
				log.Println(err)
				// resume walking
				return nil
			}

			if f.IsDir() {
				parseDir(p)
			}
			return nil
		})
	} else {
		parseDir(sourcePath)
	}
	return nil
}

func parseDir(dir string) error {
	fset := token.NewFileSet()
	pkgs, err := parser.ParseDir(fset, dir, nil, parser.ImportsOnly)
	if err != nil {
		return err
	}

	for _, pkg := range pkgs {
		for fn, f := range pkg.Files {
			if _, ok := imports[fn]; !ok {
				imports[fn] = make([]string, 0)
			}
			for _, imprt := range f.Imports {
				// Cleanup the import path
				path := strings.Replace(imprt.Path.Value, `"`, "", 2)

				if *flagThirdParties && !isThirdParty(path) {
					continue
				}
				imports[fn] = append(imports[fn], path)
			}
		}
	}

	return nil
}

// isThirdParty determines if the given import path is a third party or not.
// It's safe to assume that if the first path of the import path looks like a domain name,
// then we're dealing with a third party.
func isThirdParty(path string) bool {
	r, err := regexp.Compile(`^(\w+)\.(\w+)/`)
	if err != nil {
		return false
	}
	return r.MatchString(path)
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"os/user"
	"path/filepath"
	"strings"

	"github.com/drone/drone-plugin-go/plugin"
)

var netrcFile = `
machine %s
login %s
password %s
`

func main() {
	v := struct {
		Path  string `json:"path"`
		Depth int    `json:"depth"`
	}{}

	c := new(plugin.Clone)
	plugin.Param("clone", c)
	plugin.Param("vargs", &v)
	if err := plugin.Parse(); err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}

	if v.Depth == 0 {
		v.Depth = 50
	}
	if len(v.Path) != 0 {
		c.Dir = filepath.Join("/drone/src", v.Path)
	}

	err := os.MkdirAll(c.Dir, 0777)
	if err != nil {
		fmt.Printf("Error creating directory %s. %s\n", c.Dir, err)
		os.Exit(2)
	}

	// generate the .netrc file
	if err := writeNetrc(c); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(3)
	}

	// write the rsa private key if provided
	if err := writeKey(c); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(4)
	}

	var cmds []*exec.Cmd
	if isPR(c) {
		cmds = append(cmds, clone(c))
		cmds = append(cmds, fetch(c))
		cmds = append(cmds, checkoutHead(c))
	} else {
		cmds = append(cmds, cloneBranch(c))
		cmds = append(cmds, checkoutSha(c))
	}

	for _, cmd := range cmds {
		cmd.Dir = c.Dir
		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr
		trace(cmd)
		err := cmd.Run()
		if err != nil {
			os.Exit(1)
		}
	}
}

// Returns true if cloning a pull request.
func isPR(c *plugin.Clone) bool {
	return strings.HasPrefix(c.Ref, "refs/pull/")
}

func isTag(c *plugin.Clone) bool {
	return strings.HasPrefix(c.Ref, "refs/tags/")
}

// Clone executes a git clone command.
func clone(c *plugin.Clone) *exec.Cmd {
	return exec.Command(
		"git",
		"clone",
		"--depth=50",
		"--recursive",
		c.Origin,
		c.Dir,
	)
}

// CloneBranch executes a git clone command
// for a single branch.
func cloneBranch(c *plugin.Clone) *exec.Cmd {
	return exec.Command(
		"git",
		"clone",
		"-b",
		c.Branch,
		"--depth=50",
		"--recursive",
		c.Origin,
		c.Dir,
	)
}

// Checkout executes a git checkout command.
func checkoutSha(c *plugin.Clone) *exec.Cmd {
	return exec.Command(
		"git",
		"checkout",
		"-qf",
		c.Sha,
	)
}

// Checkout executes a git checkout command.
func checkoutHead(c *plugin.Clone) *exec.Cmd {
	return exec.Command(
		"git",
		"checkout",
		"-qf",
		"FETCH_HEAD",
	)
}

// Fetch executes a git fetch to origin.
func fetch(c *plugin.Clone) *exec.Cmd {
	return exec.Command(
		"git",
		"fetch",
		"origin",
		fmt.Sprintf("+%s:", c.Ref),
	)
}

// Trace writes each command to standard error (preceded by a ‘$ ’) before it
// is executed. Used for debugging your build.
func trace(cmd *exec.Cmd) {
	fmt.Println("$", strings.Join(cmd.Args, " "))
}

// Writes the netrc file.
func writeNetrc(in *plugin.Clone) error {
	if len(in.Netrc.Machine) == 0 {
		return nil
	}
	out := fmt.Sprintf(
		netrcFile,
		in.Netrc.Machine,
		in.Netrc.Login,
		in.Netrc.Password,
	)
	home := "/root"
	u, err := user.Current()
	if err == nil {
		home = u.HomeDir
	}
	path := filepath.Join(home, ".netrc")
	return ioutil.WriteFile(path, []byte(out), 0600)
}

// Writes the RSA private key
func writeKey(in *plugin.Clone) error {
	if len(in.Keypair.Private) == 0 {
		return nil
	}
	home := "/root"
	u, err := user.Current()
	if err == nil {
		home = u.HomeDir
	}
	sshpath := filepath.Join(home, ".ssh")
	if err := os.MkdirAll(sshpath, 0700); err != nil {
		return err
	}
	confpath := filepath.Join(sshpath, "config")
	privpath := filepath.Join(sshpath, "id_rsa")
	ioutil.WriteFile(confpath, []byte("StrictHostKeyChecking no\n"), 0700)
	return ioutil.WriteFile(privpath, []byte(in.Keypair.Private), 0600)
}
<commit_msg>Modifying plugin to use git init instead of git clone so an empty directory is not necessary<commit_after>package main

import (
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"os/exec"
	"os/user"
	"path/filepath"
	"strings"

	"github.com/drone/drone-plugin-go/plugin"
)

var netrcFile = `
machine %s
login %s
password %s
`

func main() {
	v := struct {
		Path  string `json:"path"`
		Depth int    `json:"depth"`
	}{}

	c := new(plugin.Clone)
	plugin.Param("clone", c)
	plugin.Param("vargs", &v)
	if err := plugin.Parse(); err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}

	if v.Depth == 0 {
		v.Depth = 50
	}
	if len(v.Path) != 0 {
		c.Dir = filepath.Join("/drone/src", v.Path)
	}

	err := os.MkdirAll(c.Dir, 0777)
	if err != nil {
		fmt.Printf("Error creating directory %s. %s\n", c.Dir, err)
		os.Exit(2)
	}

	// generate the .netrc file
	if err := writeNetrc(c); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(3)
	}

	// write the rsa private key if provided
	if err := writeKey(c); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(4)
	}

	var cmds []*exec.Cmd
	// check for a .git directory and whether it's empty
  if isDirEmpty(filepath.Join(c.Dir, ".git")) {
		cmds = append(cmds, initGit())
		cmds = append(cmds, remote(c))
	}

	cmds = append(cmds, fetch(c))

	if isPR(c) {
		//cmds = append(cmds, clone(c))
		//cmds = append(cmds, fetch(c))
		cmds = append(cmds, checkoutHead(c))
	} else {
		//cmds = append(cmds, cloneBranch(c))
		cmds = append(cmds, checkoutSha(c))
	}

	for _, cmd := range cmds {
		cmd.Dir = c.Dir
		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr
		trace(cmd)
		err := cmd.Run()
		if err != nil {
			os.Exit(1)
		}
	}
}

// Returns true if cloning a pull request.
func isPR(c *plugin.Clone) bool {
	return strings.HasPrefix(c.Ref, "refs/pull/")
}

func isTag(c *plugin.Clone) bool {
	return strings.HasPrefix(c.Ref, "refs/tags/")
}

// Creates an empty git repository.
func initGit() *exec.Cmd {
	return exec.Command(
		"git",
		"init",
	)
}

// Sets the remote origin for the repository.
func remote(c *plugin.Clone) *exec.Cmd {
	return exec.Command(
		"git",
		"remote",
		"add",
		"origin",
		c.Origin,
	)
}

// Checkout executes a git checkout command.
func checkoutSha(c *plugin.Clone) *exec.Cmd {
	return exec.Command(
		"git",
		"checkout",
		"-qf",
		c.Sha,
	)
}

// Checkout executes a git checkout command.
func checkoutHead(c *plugin.Clone) *exec.Cmd {
	return exec.Command(
		"git",
		"checkout",
		"-qf",
		"FETCH_HEAD",
	)
}

// Fetch executes a git fetch to origin.
func fetch(c *plugin.Clone) *exec.Cmd {
	return exec.Command(
		"git",
		"fetch",
		"origin",
		fmt.Sprintf("+%s:", c.Ref),
	)
}

// Trace writes each command to standard error (preceded by a ‘$ ’) before it
// is executed. Used for debugging your build.
func trace(cmd *exec.Cmd) {
	fmt.Println("$", strings.Join(cmd.Args, " "))
}

// Writes the netrc file.
func writeNetrc(in *plugin.Clone) error {
	if len(in.Netrc.Machine) == 0 {
		return nil
	}
	out := fmt.Sprintf(
		netrcFile,
		in.Netrc.Machine,
		in.Netrc.Login,
		in.Netrc.Password,
	)
	home := "/root"
	u, err := user.Current()
	if err == nil {
		home = u.HomeDir
	}
	path := filepath.Join(home, ".netrc")
	return ioutil.WriteFile(path, []byte(out), 0600)
}

// Writes the RSA private key
func writeKey(in *plugin.Clone) error {
	if len(in.Keypair.Private) == 0 {
		return nil
	}
	home := "/root"
	u, err := user.Current()
	if err == nil {
		home = u.HomeDir
	}
	sshpath := filepath.Join(home, ".ssh")
	if err := os.MkdirAll(sshpath, 0700); err != nil {
		return err
	}
	confpath := filepath.Join(sshpath, "config")
	privpath := filepath.Join(sshpath, "id_rsa")
	ioutil.WriteFile(confpath, []byte("StrictHostKeyChecking no\n"), 0700)
	return ioutil.WriteFile(privpath, []byte(in.Keypair.Private), 0600)
}

func isDirEmpty(name string) bool {
	f, err := os.Open(name)
	if err != nil {
		return true;
	}
	defer f.Close()

	_, err = f.Readdir(1)
	if err == io.EOF {
	  return true
	}
	return false
}
<|endoftext|>