<commit_before>// psmcli
// Copyright (C) 2014 Procera Networks, Inc.

package main

import (
	"encoding/json"
	"flag"
	"log"
	"net"
	"os"
	"strings"

	"code.google.com/p/go.crypto/ssh/terminal"
)

var (
	Version = "unknown-dev"
)

func main() {
	log.SetOutput(os.Stdout)
	log.SetFlags(0)

	verbose := flag.Bool("v", false, "Verbose output")
	flag.Usage = usage
	flag.Parse()
	dst := flag.Arg(0)

	if dst == "" {
		usage()
		os.Exit(2)
	}

	// Add default port 3994 if it's missing in the dst string

	host, port, err := net.SplitHostPort(dst)
	if err != nil && strings.Contains(err.Error(), "missing port") {
		dst = net.JoinHostPort(dst, "3994")
	} else if err != nil {
		log.Println(err)
		return
	} else if port == "" {
		dst = net.JoinHostPort(host, "3994")
	}

	// Connect to PSM

	conn, err := newConnection(dst)
	if err != nil {
		log.Println(err)
		return
	}
	log.Println("psmcli", Version, "connected to", conn.conn.RemoteAddr())
	log.Println("^D to quit")
	log.Println("")

	// Use system.version as dummy call to check if we can proceed without
	// authentication.

	res, err := conn.run(command{Method: "system.version"})
	if err != nil {
		log.Println(err)
		return
	}

	initialPrompt := "$ "
	if res.Error.Code == CodeAccessDenied {
		initialPrompt = "Username: "
	}

	// Set up a terminal

	oldState, err := terminal.MakeRaw(0)
	if err != nil {
		log.Println(err)
		return
	}
	defer terminal.Restore(0, oldState)
	term := terminal.NewTerminal(os.NewFile(0, "terminal"), initialPrompt)

	h, w, err := terminal.GetSize(0)
	if err != nil {
		log.Println(err)
		return
	}
	term.SetSize(h, w)

	user := "default"
	for res.Error.Code == CodeAccessDenied {
		term.SetPrompt("Username: ")
		user, err = term.ReadLine()
		if err != nil {
			log.Println(err)
			return
		}
		pass, err := term.ReadPassword("Password: ")
		if err != nil {
			log.Println(err)
			return
		}
		res, err = conn.run(command{Method: "system.login", Params: []interface{}{user, pass}})
		if err != nil {
			log.Println(err)
			return
		}
		if res.Error.Code != 0 {
			log.Println(res.Error.Message)
			log.Println()
		} else {
			res, err = conn.run(command{Method: "system.version"})
			if err != nil {
				log.Println(err)
				return
			}
		}
	}

	// Print version and hostname as identification

	version, ok := res.Result.(string)
	if !ok {
		version = "(unknown)"
	}

	res, err = conn.run(command{Method: "system.hostname"})
	if err != nil {
		log.Println(err)
		return
	}
	hostname, ok := res.Result.(string)
	if !ok {
		hostname = "(unknown)"
	}

	log.Println("PSM version", version, "at", hostname)

	res, err = conn.run(command{Method: "model.isReadOnly"})
	if err != nil {
		log.Println(err)
		return
	}

	// Set up the prompt as user@host, root-style if the model is read/write
	// otherwise user-style.

	var roRw = " # "
	if ro, ok := res.Result.(bool); ok && ro {
		roRw = " $ "
	}

	hostnameParts := strings.SplitN(hostname, ".", 2)
	hostname = hostnameParts[0]
	term.SetPrompt(user + "@" + hostname + roRw)

	log.Println()

	// Set up tab completion based on announced commands and parameters

	tabcomp := completer{
		term: term,
	}

	smd, err := conn.smd()
	if err != nil {
		log.Fatal(err)
	}
	tabcomp.importSMD(smd.Result.Services)
	tabcomp.words["help"] = completionWord{}

	term.AutoCompleteCallback = tabcomp.complete

	// Start the REPL

	for {
		line, err := term.ReadLine()
		if err != nil {
			return
		}

		line = strings.TrimSpace(line)
		if line == "" {
			continue
		}

		if line == "help" || line == "?" {
			tabcomp.printHelp()
			continue
		}

		fields := strings.Fields(line)
		if len(fields) < 2 {
			log.Println("incomplete command")
			continue
		}

		cmd := parseCommand(fields)

		if *verbose {
			// Print the command locally
			bs, _ := json.Marshal(cmd)
			log.Printf("> %s", bs)
		}

		// Execute command on PSM

		res, err := conn.run(cmd)
		if err != nil {
			log.Println(err)
			return
		}

		printResponse(res)
	}
}

func usage() {
	log.Println("psmcli", Version)
	log.Println()
	log.Println("Usage:")
	log.Println("  psmcli [-v] <host:port>")
}

var nextID int

func parseCommand(fields []string) command {
	// The command is the first two parts joined with a dot.

	cmd := command{
		ID:     nextID,
		Method: fields[0] + "." + fields[1],
	}
	nextID++

	// Look for key=val,key=val sequences among params and make them objects.
	// Not stuff that starts with "(" though, because that might be an LDAP
	// query expression.

	for _, param := range fields[2:] {
		if strings.Contains(param, "=") && !strings.HasPrefix(param, "(") {
			parts := strings.Split(param, ",")
			obj := map[string]string{}
			for _, part := range parts {
				kv := strings.SplitN(part, "=", 2)
				obj[kv[0]] = kv[1]
			}
			cmd.Params = append(cmd.Params, obj)
		} else {
			cmd.Params = append(cmd.Params, param)
		}
	}

	return cmd
}

func printResponse(res response) {
	if res.Error.Code != 0 {
		log.Printf("Error %d: %s", res.Error.Code, res.Error.Message)
	} else if res.Result != nil {
		switch result := res.Result.(type) {
		case []interface{}:
			for _, res := range result {
				switch res := res.(type) {
				case string, int, json.Number:
					log.Println(res)
				default:
					bs, _ := json.MarshalIndent(res, "", "    ")
					log.Printf("%s\n\n", bs)
				}
			}

		case map[string]interface{}:
			bs, _ := json.MarshalIndent(result, "", "    ")
			log.Printf("%s\n\n", bs)

		default:
			log.Println(result)
		}
	} else {
		log.Println("OK")
	}
}
<commit_msg>Fix startup and shutdown prints<commit_after>// psmcli
// Copyright (C) 2014 Procera Networks, Inc.

package main

import (
	"encoding/json"
	"flag"
	"log"
	"net"
	"os"
	"strings"

	"code.google.com/p/go.crypto/ssh/terminal"
)

var (
	Version = "unknown-dev"
)

func main() {
	log.SetOutput(os.Stdout)
	log.SetFlags(0)

	verbose := flag.Bool("v", false, "Verbose output")
	flag.Usage = usage
	flag.Parse()
	dst := flag.Arg(0)

	if dst == "" {
		usage()
		os.Exit(2)
	}

	log.Println("psmcli", Version)
	log.Println("^D to quit")

	// Add default port 3994 if it's missing in the dst string

	host, port, err := net.SplitHostPort(dst)
	if err != nil && strings.Contains(err.Error(), "missing port") {
		dst = net.JoinHostPort(dst, "3994")
	} else if err != nil {
		log.Println(err)
		return
	} else if port == "" {
		dst = net.JoinHostPort(host, "3994")
	}

	// Connect to PSM

	conn, err := newConnection(dst)
	if err != nil {
		log.Println(err)
		return
	}
	log.Println("Connected to", conn.conn.RemoteAddr())
	log.Println("")

	// Use system.version as dummy call to check if we can proceed without
	// authentication.

	res, err := conn.run(command{Method: "system.version"})
	if err != nil {
		log.Println(err)
		return
	}

	initialPrompt := "$ "
	if res.Error.Code == CodeAccessDenied {
		initialPrompt = "Username: "
	}

	// Set up a terminal

	oldState, err := terminal.MakeRaw(0)
	if err != nil {
		log.Println(err)
		return
	}
	defer func() {
		terminal.Restore(0, oldState)
		log.Println("")
	}()

	term := terminal.NewTerminal(os.NewFile(0, "terminal"), initialPrompt)

	h, w, err := terminal.GetSize(0)
	if err != nil {
		log.Println(err)
		return
	}
	term.SetSize(h, w)

	user := "default"
	for res.Error.Code == CodeAccessDenied {
		term.SetPrompt("Username: ")
		user, err = term.ReadLine()
		if err != nil {
			log.Println(err)
			return
		}
		pass, err := term.ReadPassword("Password: ")
		if err != nil {
			log.Println(err)
			return
		}
		res, err = conn.run(command{Method: "system.login", Params: []interface{}{user, pass}})
		if err != nil {
			log.Println(err)
			return
		}
		if res.Error.Code != 0 {
			log.Println(res.Error.Message)
			log.Println()
		} else {
			res, err = conn.run(command{Method: "system.version"})
			if err != nil {
				log.Println(err)
				return
			}
		}
	}

	// Print version and hostname as identification

	version, ok := res.Result.(string)
	if !ok {
		version = "(unknown)"
	}

	res, err = conn.run(command{Method: "system.hostname"})
	if err != nil {
		log.Println(err)
		return
	}
	hostname, ok := res.Result.(string)
	if !ok {
		hostname = "(unknown)"
	}

	log.Println("PSM version", version, "at", hostname)

	res, err = conn.run(command{Method: "model.isReadOnly"})
	if err != nil {
		log.Println(err)
		return
	}

	// Set up the prompt as user@host, root-style if the model is read/write
	// otherwise user-style.

	var roRw = " # "
	if ro, ok := res.Result.(bool); ok && ro {
		roRw = " $ "
	}

	hostnameParts := strings.SplitN(hostname, ".", 2)
	hostname = hostnameParts[0]
	term.SetPrompt(user + "@" + hostname + roRw)

	log.Println()

	// Set up tab completion based on announced commands and parameters

	tabcomp := completer{
		term: term,
	}

	smd, err := conn.smd()
	if err != nil {
		log.Fatal(err)
	}
	tabcomp.importSMD(smd.Result.Services)
	tabcomp.words["help"] = completionWord{}

	term.AutoCompleteCallback = tabcomp.complete

	// Start the REPL

	for {
		line, err := term.ReadLine()
		if err != nil {
			return
		}

		line = strings.TrimSpace(line)
		if line == "" {
			continue
		}

		if line == "help" || line == "?" {
			tabcomp.printHelp()
			continue
		}

		fields := strings.Fields(line)
		if len(fields) < 2 {
			log.Println("incomplete command")
			continue
		}

		cmd := parseCommand(fields)

		if *verbose {
			// Print the command locally
			bs, _ := json.Marshal(cmd)
			log.Printf("> %s", bs)
		}

		// Execute command on PSM

		res, err := conn.run(cmd)
		if err != nil {
			log.Println(err)
			return
		}

		printResponse(res)
	}
}

func usage() {
	log.Println("psmcli", Version)
	log.Println()
	log.Println("Usage:")
	log.Println("  psmcli [-v] <host:port>")
}

var nextID int

func parseCommand(fields []string) command {
	// The command is the first two parts joined with a dot.

	cmd := command{
		ID:     nextID,
		Method: fields[0] + "." + fields[1],
	}
	nextID++

	// Look for key=val,key=val sequences among params and make them objects.
	// Not stuff that starts with "(" though, because that might be an LDAP
	// query expression.

	for _, param := range fields[2:] {
		if strings.Contains(param, "=") && !strings.HasPrefix(param, "(") {
			parts := strings.Split(param, ",")
			obj := map[string]string{}
			for _, part := range parts {
				kv := strings.SplitN(part, "=", 2)
				obj[kv[0]] = kv[1]
			}
			cmd.Params = append(cmd.Params, obj)
		} else {
			cmd.Params = append(cmd.Params, param)
		}
	}

	return cmd
}

func printResponse(res response) {
	if res.Error.Code != 0 {
		log.Printf("Error %d: %s", res.Error.Code, res.Error.Message)
	} else if res.Result != nil {
		switch result := res.Result.(type) {
		case []interface{}:
			for _, res := range result {
				switch res := res.(type) {
				case string, int, json.Number:
					log.Println(res)
				default:
					bs, _ := json.MarshalIndent(res, "", "    ")
					log.Printf("%s\n\n", bs)
				}
			}

		case map[string]interface{}:
			bs, _ := json.MarshalIndent(result, "", "    ")
			log.Printf("%s\n\n", bs)

		default:
			log.Println(result)
		}
	} else {
		log.Println("OK")
	}
}
<|endoftext|>


<commit_before>package main

import (
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"mime/multipart"
	"net/http"
	"os"

	"github.com/codegangsta/cli"
)

var (
	// exitCode to terminate
	exitCode = 0

	// endpoint of the Gyazo upload API endpoint.
	endpoint = "http://upload.gyazo.com/upload.cgi"
)

const usageText = `Gyazo command-line uploader

EXAMPLE:

  $ gyazo foo.png
  $ gyazo ~/Downloads/bar.jpg`

func main() {
	app := cli.NewApp()
	app.Name = "gyazo"
	app.Version = Version
	app.Usage = usageText
	app.Author = "Tomohiro TAIRA"
	app.Email = "tomohiro.t@gmail.com"
	app.Action = upload
	app.Run(os.Args)

	os.Exit(exitCode)
}

func upload(c *cli.Context) {
	if len(c.Args()) == 0 {
		fmt.Fprintln(os.Stderr, "Try `gyazo --help` for more information")
		exitCode = 1
		return
	}

	filename := c.Args().First()
	content, err := os.Open(filename)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to open and read %s\n", filename)
		return
	}

	// Create multipart/form-data
	var buf bytes.Buffer
	w := multipart.NewWriter(&buf)

	f, err := w.CreateFormFile("imagedata", filename)
	if err != nil {
		exitCode = 1
		return
	}

	if _, err = io.Copy(f, content); err != nil {
		exitCode = 1
		return
	}

	w.Close()

	res, err := http.Post(endpoint, w.FormDataContentType(), &buf)
	defer res.Body.Close()
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to upload: %s", err)
		exitCode = 1
		return
	}

	url, err := ioutil.ReadAll(res.Body)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Response error: %s", err)
		exitCode = 1
		return
	}

	fmt.Println(string(url))
}
<commit_msg>Support internal Gyazo server<commit_after>package main

import (
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"mime/multipart"
	"net/http"
	"os"

	"github.com/codegangsta/cli"
)

var (
	// exitCode to terminate
	exitCode = 0

	// endpoint of the Gyazo upload API endpoint.
	endpoint = "http://upload.gyazo.com/upload.cgi"
)

const usageText = `Gyazo command-line uploader

EXAMPLE:

  $ gyazo foo.png
  $ gyazo ~/Downloads/bar.jpg`

// Set the endpoint of Gyazo upload API endpoint
func init() {
	if os.Getenv("GYAZO_SERVER_URL") != "" {
		endpoint = os.Getenv("GYAZO_SERVER_URL")
	}
}

func main() {
	app := cli.NewApp()
	app.Name = "gyazo"
	app.Version = Version
	app.Usage = usageText
	app.Author = "Tomohiro TAIRA"
	app.Email = "tomohiro.t@gmail.com"
	app.Action = upload
	app.Run(os.Args)

	os.Exit(exitCode)
}

func upload(c *cli.Context) {
	if len(c.Args()) == 0 {
		fmt.Fprintln(os.Stderr, "Try `gyazo --help` for more information")
		exitCode = 1
		return
	}

	filename := c.Args().First()
	content, err := os.Open(filename)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to open and read %s\n", filename)
		return
	}

	// Create multipart/form-data
	var buf bytes.Buffer
	w := multipart.NewWriter(&buf)

	f, err := w.CreateFormFile("imagedata", filename)
	if err != nil {
		exitCode = 1
		return
	}

	if _, err = io.Copy(f, content); err != nil {
		exitCode = 1
		return
	}

	w.Close()

	res, err := http.Post(endpoint, w.FormDataContentType(), &buf)
	defer res.Body.Close()
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to upload: %s", err)
		exitCode = 1
		return
	}

	url, err := ioutil.ReadAll(res.Body)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Response error: %s", err)
		exitCode = 1
		return
	}

	fmt.Println(string(url))
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"os"

	"github.com/codegangsta/cli"
)

var Cfg Config
var utl util.SomaUtil
var store db.DB

const rfc3339Milli string = "2006-01-02T15:04:05.000Z07:00"

func main() {
	app := cli.NewApp()
	app.Name = "somaadm"
	app.Usage = "SOMA Administrative Interface"
	app.Version = "0.5.18"
	app.EnableBashCompletion = true

	app = registerCommands(*app)
	app = registerFlags(*app)

	if err := app.Run(os.Args); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}

// vim: ts=4 sw=4 sts=4 noet fenc=utf-8 ffs=unix
<commit_msg>Update version for 0.8.0 release<commit_after>package main

import (
	"fmt"
	"os"

	"github.com/codegangsta/cli"
)

var Cfg Config
var utl util.SomaUtil
var store db.DB

const rfc3339Milli string = "2006-01-02T15:04:05.000Z07:00"

func main() {
	app := cli.NewApp()
	app.Name = "somaadm"
	app.Usage = "SOMA Administrative Interface"
	app.Version = "0.8.0"
	app.EnableBashCompletion = true

	app = registerCommands(*app)
	app = registerFlags(*app)

	if err := app.Run(os.Args); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}

// vim: ts=4 sw=4 sts=4 noet fenc=utf-8 ffs=unix
<|endoftext|>


<commit_before>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
`

// Params stores the git clone parameters used to
// configure and customzie the git clone behavior.
type Params struct {
	Depth           int               `json:"depth"`
	Recursive       bool              `json:"recursive"`
	SkipVerify      bool              `json:"skip_verify"`
	Tags            bool              `json:"tags"`
	Submodules      map[string]string `json:"submodule_override"`
	SubmoduleRemote bool              `json:"submodule_update_remote"`
}

var (
	buildCommit string
)

func main() {
	fmt.Printf("Drone Git Plugin built from %s\n", buildCommit)

	v := new(Params)
	r := new(plugin.Repo)
	b := new(plugin.Build)
	w := new(plugin.Workspace)
	plugin.Param("repo", r)
	plugin.Param("build", b)
	plugin.Param("workspace", w)
	plugin.Param("vargs", &v)
	plugin.MustParse()

	err := clone(r, b, w, v)
	if err != nil {
		os.Exit(1)
	}
}

// Clone clones the repository and build revision
// into the build workspace.
func clone(r *plugin.Repo, b *plugin.Build, w *plugin.Workspace, v *Params) error {
	err := os.MkdirAll(w.Path, 0777)
	if err != nil {
		fmt.Printf("Error creating directory %s. %s\n", w.Path, err)
		return err
	}

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

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

	var cmds []*exec.Cmd

	if v.SkipVerify {
		cmds = append(cmds, skipVerify())
	}

	// check for a .git directory and whether it's empty
	if isDirEmpty(filepath.Join(w.Path, ".git")) {
		cmds = append(cmds, initGit())
		cmds = append(cmds, remote(r))
	}

	switch {
	case isPullRequest(b) || isTag(b):
		cmds = append(cmds, fetch(b, v.Tags, v.Depth))
		cmds = append(cmds, checkoutHead(b))
	default:
		cmds = append(cmds, fetch(b, v.Tags, v.Depth))
		cmds = append(cmds, checkoutSha(b))
	}

	for name, url := range v.Submodules {
		cmds = append(cmds, remapSubmodule(name, url))
	}

	if v.Recursive {
		cmds = append(cmds, updateSubmodules(v.SubmoduleRemote))
	}

	for _, cmd := range cmds {
		cmd.Dir = w.Path
		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr
		trace(cmd)
		err := cmd.Run()
		if err != nil {
			return err
		}
	}

	return nil
}

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

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

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

// Checkout executes a git checkout command.
func checkoutSha(b *plugin.Build) *exec.Cmd {
	return exec.Command(
		"git",
		"reset",
		"--hard",
		"-q",
		b.Commit,
	)
}

// fetch retuns git command that fetches from origin. If tags is true
// then tags will be fetched.
func fetch(b *plugin.Build, tags bool, depth int) *exec.Cmd {
	tags_option := "--no-tags"
	if tags {
		tags_option = "--tags"
	}
	cmd := exec.Command(
		"git",
		"fetch",
		tags_option,
	)
	if depth != 0 {
		cmd.Args = append(cmd.Args, fmt.Sprintf("--depth=%d", depth))
	}
	cmd.Args = append(cmd.Args, "origin")
	cmd.Args = append(cmd.Args, fmt.Sprintf("+%s:", b.Ref))

	return cmd
}

// updateSubmodules recursively initializes and updates submodules.
func updateSubmodules(remote bool) *exec.Cmd {
	cmd := exec.Command(
		"git",
		"submodule",
		"update",
		"--init",
		"--recursive",
	)

	if remote {
		cmd.Args = append(cmd.Args, "--remote")
	}

	return cmd
}

// skipVerify returns a git command that, when executed
// configures git to skip ssl verification. This should
// may be used with self-signed certificates.
func skipVerify() *exec.Cmd {
	return exec.Command(
		"git",
		"config",
		"--global",
		"http.sslVerify",
		"false",
	)
}

// remapSubmodule returns a git command that, when executed
// configures git to remap submodule urls.
func remapSubmodule(name, url string) *exec.Cmd {
	name = fmt.Sprintf("submodule.%s.url", name)
	return exec.Command(
		"git",
		"config",
		"--global",
		name,
		url,
	)
}

// 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.Workspace) error {
	if in.Netrc == nil || 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.Workspace) error {
	if in.Keys == nil || len(in.Keys.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.Keys.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
}

func isPullRequest(b *plugin.Build) bool {
	return b.Event == plugin.EventPull
}

func isTag(b *plugin.Build) bool {
	return b.Event == plugin.EventTag ||
		strings.HasPrefix(b.Ref, "refs/tags/")
}
<commit_msg>Revert "Added git --global flag to remapSubmodule to support multi-level recursive HTTPS …"<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
`

// Params stores the git clone parameters used to
// configure and customzie the git clone behavior.
type Params struct {
	Depth           int               `json:"depth"`
	Recursive       bool              `json:"recursive"`
	SkipVerify      bool              `json:"skip_verify"`
	Tags            bool              `json:"tags"`
	Submodules      map[string]string `json:"submodule_override"`
	SubmoduleRemote bool              `json:"submodule_update_remote"`
}

var (
	buildCommit string
)

func main() {
	fmt.Printf("Drone Git Plugin built from %s\n", buildCommit)

	v := new(Params)
	r := new(plugin.Repo)
	b := new(plugin.Build)
	w := new(plugin.Workspace)
	plugin.Param("repo", r)
	plugin.Param("build", b)
	plugin.Param("workspace", w)
	plugin.Param("vargs", &v)
	plugin.MustParse()

	err := clone(r, b, w, v)
	if err != nil {
		os.Exit(1)
	}
}

// Clone clones the repository and build revision
// into the build workspace.
func clone(r *plugin.Repo, b *plugin.Build, w *plugin.Workspace, v *Params) error {
	err := os.MkdirAll(w.Path, 0777)
	if err != nil {
		fmt.Printf("Error creating directory %s. %s\n", w.Path, err)
		return err
	}

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

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

	var cmds []*exec.Cmd

	if v.SkipVerify {
		cmds = append(cmds, skipVerify())
	}

	// check for a .git directory and whether it's empty
	if isDirEmpty(filepath.Join(w.Path, ".git")) {
		cmds = append(cmds, initGit())
		cmds = append(cmds, remote(r))
	}

	switch {
	case isPullRequest(b) || isTag(b):
		cmds = append(cmds, fetch(b, v.Tags, v.Depth))
		cmds = append(cmds, checkoutHead(b))
	default:
		cmds = append(cmds, fetch(b, v.Tags, v.Depth))
		cmds = append(cmds, checkoutSha(b))
	}

	for name, url := range v.Submodules {
		cmds = append(cmds, remapSubmodule(name, url))
	}

	if v.Recursive {
		cmds = append(cmds, updateSubmodules(v.SubmoduleRemote))
	}

	for _, cmd := range cmds {
		cmd.Dir = w.Path
		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr
		trace(cmd)
		err := cmd.Run()
		if err != nil {
			return err
		}
	}

	return nil
}

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

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

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

// Checkout executes a git checkout command.
func checkoutSha(b *plugin.Build) *exec.Cmd {
	return exec.Command(
		"git",
		"reset",
		"--hard",
		"-q",
		b.Commit,
	)
}

// fetch retuns git command that fetches from origin. If tags is true
// then tags will be fetched.
func fetch(b *plugin.Build, tags bool, depth int) *exec.Cmd {
	tags_option := "--no-tags"
	if tags {
		tags_option = "--tags"
	}
	cmd := exec.Command(
		"git",
		"fetch",
		tags_option,
	)
	if depth != 0 {
		cmd.Args = append(cmd.Args, fmt.Sprintf("--depth=%d", depth))
	}
	cmd.Args = append(cmd.Args, "origin")
	cmd.Args = append(cmd.Args, fmt.Sprintf("+%s:", b.Ref))

	return cmd
}

// updateSubmodules recursively initializes and updates submodules.
func updateSubmodules(remote bool) *exec.Cmd {
	cmd := exec.Command(
		"git",
		"submodule",
		"update",
		"--init",
		"--recursive",
	)

	if remote {
		cmd.Args = append(cmd.Args, "--remote")
	}

	return cmd
}

// skipVerify returns a git command that, when executed
// configures git to skip ssl verification. This should
// may be used with self-signed certificates.
func skipVerify() *exec.Cmd {
	return exec.Command(
		"git",
		"config",
		"--global",
		"http.sslVerify",
		"false",
	)
}

// remapSubmodule returns a git command that, when executed
// configures git to remap submodule urls.
func remapSubmodule(name, url string) *exec.Cmd {
	name = fmt.Sprintf("submodule.%s.url", name)
	return exec.Command(
		"git",
		"config",
		name,
		url,
	)
}

// 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.Workspace) error {
	if in.Netrc == nil || 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.Workspace) error {
	if in.Keys == nil || len(in.Keys.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.Keys.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
}

func isPullRequest(b *plugin.Build) bool {
	return b.Event == plugin.EventPull
}

func isTag(b *plugin.Build) bool {
	return b.Event == plugin.EventTag ||
		strings.HasPrefix(b.Ref, "refs/tags/")
}
<|endoftext|>


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

package main

import (
	"flag"
	"fmt"
	"net/http"
	"os"
	"os/signal"
	"strings"
	"syscall"

	"github.com/hpcloud/tail"
	"github.com/martin-helmich/prometheus-nginxlog-exporter/config"
	"github.com/martin-helmich/prometheus-nginxlog-exporter/discovery"
	"github.com/prometheus/client_golang/prometheus"
	"github.com/satyrius/gonx"
)

// Metrics is a struct containing pointers to all metrics that should be
// exposed to Prometheus
type Metrics struct {
	countTotal      *prometheus.CounterVec
	bytesTotal      *prometheus.CounterVec
	upstreamSeconds *prometheus.SummaryVec
	responseSeconds *prometheus.SummaryVec
}

// Init initializes a metrics struct
func (m *Metrics) Init(cfg *config.NamespaceConfig) {
	labels := []string{"method", "status"}
	labels = append(labels, cfg.LabelNames()...)

	m.countTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
		Namespace: cfg.Name,
		Name:      "http_response_count_total",
		Help:      "Amount of processed HTTP requests",
	}, labels)

	m.bytesTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
		Namespace: cfg.Name,
		Name:      "http_response_size_bytes",
		Help:      "Total amount of transferred bytes",
	}, labels)

	m.upstreamSeconds = prometheus.NewSummaryVec(prometheus.SummaryOpts{
		Namespace: cfg.Name,
		Name:      "http_upstream_time_seconds",
		Help:      "Time needed by upstream servers to handle requests",
	}, labels)

	m.responseSeconds = prometheus.NewSummaryVec(prometheus.SummaryOpts{
		Namespace: cfg.Name,
		Name:      "http_response_time_seconds",
		Help:      "Time needed by NGINX to handle requests",
	}, labels)

	prometheus.MustRegister(m.countTotal)
	prometheus.MustRegister(m.bytesTotal)
	prometheus.MustRegister(m.upstreamSeconds)
	prometheus.MustRegister(m.responseSeconds)
}

func main() {
	var opts config.StartupFlags
	var cfg = config.Config{
		Listen: config.ListenConfig{
			Port:    4040,
			Address: "0.0.0.0",
		},
	}

	flag.IntVar(&opts.ListenPort, "listen-port", 4040, "HTTP port to listen on")
	flag.StringVar(&opts.Format, "format", `$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"`, "NGINX access log format")
	flag.StringVar(&opts.Namespace, "namespace", "nginx", "namespace to use for metric names")
	flag.StringVar(&opts.ConfigFile, "config-file", "", "Configuration file to read from")
	flag.Parse()

	opts.Filenames = flag.Args()

	if opts.ConfigFile != "" {
		fmt.Printf("loading configuration file %s\n", opts.ConfigFile)
		if err := config.LoadConfigFromFile(&cfg, opts.ConfigFile); err != nil {
			panic(err)
		}
	} else if err := config.LoadConfigFromFlags(&cfg, &opts); err != nil {
		panic(err)
	}

	fmt.Printf("using configuration %s\n", cfg)

	if cfg.Consul.Enable {
		registrator, err := discovery.NewConsulRegistrator(&cfg)
		if err != nil {
			panic(err)
		}

		fmt.Printf("registering service in Consul\n")
		if err := registrator.RegisterConsul(); err != nil {
			panic(err)
		}

		exitChan := make(chan os.Signal, 1)
		signal.Notify(exitChan, os.Interrupt, syscall.SIGTERM)

		go func() {
			<-exitChan
			fmt.Printf("unregistering service in Consul\n")
			registrator.UnregisterConsul()
			os.Exit(0)
		}()
	}

	for _, ns := range cfg.Namespaces {
		fmt.Printf("starting listener for namespace %s\n", ns.Name)

		go func(nsCfg config.NamespaceConfig) {
			parser := gonx.NewParser(nsCfg.Format)

			metrics := Metrics{}
			metrics.Init(&nsCfg)

			for _, f := range nsCfg.SourceFiles {
				t, err := tail.TailFile(f, tail.Config{
					Follow: true,
					ReOpen: true,
					Poll:   true,
				})
				if err != nil {
					panic(err)
				}

				go func() {
					staticLabelValues := nsCfg.LabelValues()
					labelValues := make([]string, len(staticLabelValues)+2)

					for i := range staticLabelValues {
						labelValues[i+2] = staticLabelValues[i]
					}

					for line := range t.Lines {
						entry, err := parser.ParseString(line.Text)
						if err != nil {
							fmt.Printf("error while parsing line '%s': %s", line.Text, err)
							continue
						}

						labelValues[0] = "UNKNOWN"
						labelValues[1] = "0"

						if request, err := entry.Field("request"); err == nil {
							f := strings.Split(request, " ")
							labelValues[0] = f[0]
						}

						if s, err := entry.Field("status"); err == nil {
							labelValues[1] = s
						}

						metrics.countTotal.WithLabelValues(labelValues...).Inc()

						if bytes, err := entry.FloatField("body_bytes_sent"); err == nil {
							metrics.bytesTotal.WithLabelValues(labelValues...).Add(bytes)
						}

						if upstreamTime, err := entry.FloatField("upstream_response_time"); err == nil {
							metrics.upstreamSeconds.WithLabelValues(labelValues...).Observe(upstreamTime)
						}

						if responseTime, err := entry.FloatField("request_time"); err == nil {
							metrics.responseSeconds.WithLabelValues(labelValues...).Observe(responseTime)
						}
					}
				}()
			}
		}(ns)
	}

	listenAddr := fmt.Sprintf("%s:%d", cfg.Listen.Address, cfg.Listen.Port)
	fmt.Printf("running HTTP server on address %s\n", listenAddr)

	http.Handle("/metrics", prometheus.Handler())
	http.ListenAndServe(listenAddr, nil)
}
<commit_msg>Fix concurrency issue when setting up log file consumers<commit_after>/*
 * Copyright 2016 Martin Helmich <kontakt@martin-helmich.de>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package main

import (
	"flag"
	"fmt"
	"net/http"
	"os"
	"os/signal"
	"strings"
	"syscall"

	"github.com/hpcloud/tail"
	"github.com/martin-helmich/prometheus-nginxlog-exporter/config"
	"github.com/martin-helmich/prometheus-nginxlog-exporter/discovery"
	"github.com/prometheus/client_golang/prometheus"
	"github.com/satyrius/gonx"
)

// Metrics is a struct containing pointers to all metrics that should be
// exposed to Prometheus
type Metrics struct {
	countTotal      *prometheus.CounterVec
	bytesTotal      *prometheus.CounterVec
	upstreamSeconds *prometheus.SummaryVec
	responseSeconds *prometheus.SummaryVec
}

// Init initializes a metrics struct
func (m *Metrics) Init(cfg *config.NamespaceConfig) {
	labels := []string{"method", "status"}
	labels = append(labels, cfg.LabelNames()...)

	m.countTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
		Namespace: cfg.Name,
		Name:      "http_response_count_total",
		Help:      "Amount of processed HTTP requests",
	}, labels)

	m.bytesTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
		Namespace: cfg.Name,
		Name:      "http_response_size_bytes",
		Help:      "Total amount of transferred bytes",
	}, labels)

	m.upstreamSeconds = prometheus.NewSummaryVec(prometheus.SummaryOpts{
		Namespace: cfg.Name,
		Name:      "http_upstream_time_seconds",
		Help:      "Time needed by upstream servers to handle requests",
	}, labels)

	m.responseSeconds = prometheus.NewSummaryVec(prometheus.SummaryOpts{
		Namespace: cfg.Name,
		Name:      "http_response_time_seconds",
		Help:      "Time needed by NGINX to handle requests",
	}, labels)

	prometheus.MustRegister(m.countTotal)
	prometheus.MustRegister(m.bytesTotal)
	prometheus.MustRegister(m.upstreamSeconds)
	prometheus.MustRegister(m.responseSeconds)
}

func main() {
	var opts config.StartupFlags
	var cfg = config.Config{
		Listen: config.ListenConfig{
			Port:    4040,
			Address: "0.0.0.0",
		},
	}

	flag.IntVar(&opts.ListenPort, "listen-port", 4040, "HTTP port to listen on")
	flag.StringVar(&opts.Format, "format", `$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"`, "NGINX access log format")
	flag.StringVar(&opts.Namespace, "namespace", "nginx", "namespace to use for metric names")
	flag.StringVar(&opts.ConfigFile, "config-file", "", "Configuration file to read from")
	flag.Parse()

	opts.Filenames = flag.Args()

	if opts.ConfigFile != "" {
		fmt.Printf("loading configuration file %s\n", opts.ConfigFile)
		if err := config.LoadConfigFromFile(&cfg, opts.ConfigFile); err != nil {
			panic(err)
		}
	} else if err := config.LoadConfigFromFlags(&cfg, &opts); err != nil {
		panic(err)
	}

	fmt.Printf("using configuration %s\n", cfg)

	if cfg.Consul.Enable {
		registrator, err := discovery.NewConsulRegistrator(&cfg)
		if err != nil {
			panic(err)
		}

		fmt.Printf("registering service in Consul\n")
		if err := registrator.RegisterConsul(); err != nil {
			panic(err)
		}

		exitChan := make(chan os.Signal, 1)
		signal.Notify(exitChan, os.Interrupt, syscall.SIGTERM)

		go func() {
			<-exitChan
			fmt.Printf("unregistering service in Consul\n")
			registrator.UnregisterConsul()
			os.Exit(0)
		}()
	}

	for _, ns := range cfg.Namespaces {
		fmt.Printf("starting listener for namespace %s\n", ns.Name)

		go func(nsCfg config.NamespaceConfig) {
			parser := gonx.NewParser(nsCfg.Format)

			metrics := Metrics{}
			metrics.Init(&nsCfg)

			for _, f := range nsCfg.SourceFiles {
				t, err := tail.TailFile(f, tail.Config{
					Follow: true,
					ReOpen: true,
					Poll:   true,
				})
				if err != nil {
					panic(err)
				}

				go func(nsCfg config.NamespaceConfig) {
					staticLabelValues := nsCfg.LabelValues()
					labelValues := make([]string, len(staticLabelValues)+2)

					for i := range staticLabelValues {
						labelValues[i+2] = staticLabelValues[i]
					}

					for line := range t.Lines {
						entry, err := parser.ParseString(line.Text)
						if err != nil {
							fmt.Printf("error while parsing line '%s': %s", line.Text, err)
							continue
						}

						labelValues[0] = "UNKNOWN"
						labelValues[1] = "0"

						if request, err := entry.Field("request"); err == nil {
							f := strings.Split(request, " ")
							labelValues[0] = f[0]
						}

						if s, err := entry.Field("status"); err == nil {
							labelValues[1] = s
						}

						metrics.countTotal.WithLabelValues(labelValues...).Inc()

						if bytes, err := entry.FloatField("body_bytes_sent"); err == nil {
							metrics.bytesTotal.WithLabelValues(labelValues...).Add(bytes)
						}

						if upstreamTime, err := entry.FloatField("upstream_response_time"); err == nil {
							metrics.upstreamSeconds.WithLabelValues(labelValues...).Observe(upstreamTime)
						}

						if responseTime, err := entry.FloatField("request_time"); err == nil {
							metrics.responseSeconds.WithLabelValues(labelValues...).Observe(responseTime)
						}
					}
				}(nsCfg)
			}
		}(ns)
	}

	listenAddr := fmt.Sprintf("%s:%d", cfg.Listen.Address, cfg.Listen.Port)
	fmt.Printf("running HTTP server on address %s\n", listenAddr)

	http.Handle("/metrics", prometheus.Handler())
	http.ListenAndServe(listenAddr, nil)
}
<|endoftext|>


<commit_before>/*
Copyright IBM Corp. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
*/

package transaction

import (
	"fmt"

	"github.com/hyperledger/fabric/common/flogging"
	"github.com/hyperledger/fabric/core/ledger"
	"github.com/hyperledger/fabric/protos/common"
	"github.com/pkg/errors"
)

var logger = flogging.MustGetLogger("fabtoken-processor")

// Processor implements the interface 'github.com/hyperledger/fabric/core/ledger/customtx/Processor'
// for FabToken transactions
type Processor struct {
	TMSManager TMSManager
}

func (p *Processor) GenerateSimulationResults(txEnv *common.Envelope, simulator ledger.TxSimulator, initializingLedger bool) error {
	// Extract channel header and token transaction
	ch, ttx, ci, err := UnmarshalTokenTransaction(txEnv.Payload)
	if err != nil {
		return errors.WithMessage(err, "failed unmarshalling token transaction")
	}

	// Get a TMSTxProcessor that corresponds to the channel
	txProcessor, err := p.TMSManager.GetTxProcessor(ch.ChannelId)
	if err != nil {
		return errors.WithMessage(err, "failed getting committer")
	}

	// Extract the read dependencies and ledger updates associated to the transaction using simulator
	err = txProcessor.ProcessTx(ch.TxId, ci, ttx, simulator)
	if err != nil {
		return errors.WithMessage(err, fmt.Sprintf("failed committing transaction for channel %s", ch.ChannelId))
	}

	return err
}
<commit_msg>[FAB-13344] remove dead code from token<commit_after>/*
Copyright IBM Corp. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
*/

package transaction

import (
	"fmt"

	"github.com/hyperledger/fabric/core/ledger"
	"github.com/hyperledger/fabric/protos/common"
	"github.com/pkg/errors"
)

// Processor implements the interface 'github.com/hyperledger/fabric/core/ledger/customtx/Processor'
// for FabToken transactions
type Processor struct {
	TMSManager TMSManager
}

func (p *Processor) GenerateSimulationResults(txEnv *common.Envelope, simulator ledger.TxSimulator, initializingLedger bool) error {
	// Extract channel header and token transaction
	ch, ttx, ci, err := UnmarshalTokenTransaction(txEnv.Payload)
	if err != nil {
		return errors.WithMessage(err, "failed unmarshalling token transaction")
	}

	// Get a TMSTxProcessor that corresponds to the channel
	txProcessor, err := p.TMSManager.GetTxProcessor(ch.ChannelId)
	if err != nil {
		return errors.WithMessage(err, "failed getting committer")
	}

	// Extract the read dependencies and ledger updates associated to the transaction using simulator
	err = txProcessor.ProcessTx(ch.TxId, ci, ttx, simulator)
	if err != nil {
		return errors.WithMessage(err, fmt.Sprintf("failed committing transaction for channel %s", ch.ChannelId))
	}

	return err
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"os"
	"os/signal"
	"strings"
	"syscall"

	"github.com/contactless/wb-rules/wbrules"
	"github.com/contactless/wbgo"
)

const (
	DRIVER_CLIENT_ID = "rules"
	DRIVER_CONV_ID   = "wb-rules"
	ENGINE_CLIENT_ID = "wb-rules-engine"

	PERSISTENT_DB_FILE      = "/var/lib/wirenboard/wbrules-persistent.db"
	VIRTUAL_DEVICES_DB_FILE = "/var/lib/wirenboard/wbrules-vdev.db"

	WBRULES_MODULES_ENV = "WB_RULES_MODULES"
)

func main() {
	brokerAddress := flag.String("broker", "tcp://localhost:1883", "MQTT broker url")
	editDir := flag.String("editdir", "", "Editable script directory")
	debug := flag.Bool("debug", false, "Enable debugging")
	noQueues := flag.Bool("debug-queues", false, "Don't use queues in wbgo driver (debugging)")
	useSyslog := flag.Bool("syslog", false, "Use syslog for logging")
	mqttDebug := flag.Bool("mqttdebug", false, "Enable MQTT debugging")
	precise := flag.Bool("precise", false, "Don't reown devices without driver")
	cleanup := flag.Bool("cleanup", false, "Clean up MQTT data on unload")

	persistentDbFile := flag.String("pdb", PERSISTENT_DB_FILE, "Persistent storage DB file")
	vdevDbFile := flag.String("vdb", VIRTUAL_DEVICES_DB_FILE, "Virtual devices values DB file")

	flag.Parse()
	if flag.NArg() < 1 {
		wbgo.Error.Fatal("must specify rule file/directory name(s)")
	}
	if *useSyslog {
		wbgo.UseSyslog()
	}
	if *debug {
		wbgo.SetDebuggingEnabled(true)
	}
	if *mqttDebug {
		wbgo.EnableMQTTDebugLog(*useSyslog)
	}
	wbgo.MaybeInitProfiling(nil)

	driverMqttClient := wbgo.NewPahoMQTTClient(*brokerAddress, DRIVER_CLIENT_ID)
	driverArgs := wbgo.NewDriverArgs().
		SetId(DRIVER_CONV_ID).
		SetMqtt(driverMqttClient).
		SetUseStorage(*vdevDbFile != "").
		SetStoragePath(*vdevDbFile).
		SetReownUnknownDevices(!*precise)

	if *noQueues {
		driverArgs.SetTesting()
	}

	driver, err := wbgo.NewDriverBase(driverArgs)
	if err != nil {
		wbgo.Error.Fatalf("error creating driver: %s", err)
	}

	wbgo.Info.Println("driver is created")

	if err := driver.StartLoop(); err != nil {
		wbgo.Error.Fatalf("error starting the driver: %s", err)
	}

	wbgo.Info.Println("driver loop is started")
	ready := make(chan struct{})
	driver.OnRetainReady(func(tx wbgo.DriverTx) {
		close(ready)
	})
	driver.SetFilter(&wbgo.AllDevicesFilter{})

	wbgo.Info.Println("wait for driver to become ready")
	<-ready
	wbgo.Info.Println("driver is ready")

	engineOptions := wbrules.NewESEngineOptions()
	engineOptions.SetPersistentDBFile(*persistentDbFile)
	engineOptions.SetModulesDirs(strings.Split(os.Getenv(WBRULES_MODULES_ENV), ":"))
	engineOptions.SetCleanupOnStop(*cleanup)

	if *noQueues {
		engineOptions.SetTesting(true)
	}

	engineMqttClient := wbgo.NewPahoMQTTClient(*brokerAddress, ENGINE_CLIENT_ID)
	engine, err := wbrules.NewESEngine(driver, engineMqttClient, engineOptions)
	if err != nil {
		wbgo.Error.Fatalf("error creating engine: %s", err)
	}
	engine.Start()

	gotSome := false
	watcher := wbgo.NewDirWatcher("\\.js$", engine)
	if *editDir != "" {
		engine.SetSourceRoot(*editDir)
	}
	for _, path := range flag.Args() {
		if err := watcher.Load(path); err != nil {
			wbgo.Error.Printf("error loading script file/dir %s: %s", path, err)
		} else {
			gotSome = true
		}
	}
	if !gotSome {
		wbgo.Error.Fatalf("no valid scripts found")
	}
	wbgo.Info.Println("all rule files are loaded")

	if *editDir != "" {
		rpc := wbgo.NewMQTTRPCServer("wbrules", engineMqttClient)
		rpc.Register(wbrules.NewEditor(engine))
		rpc.Start()
	}

	// wait for quit signal
	c := make(chan os.Signal, 1)
	signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
	<-c

	engine.Stop()
	driver.StopLoop()
	driver.Close()
}
<commit_msg>main: init signal handler earlier<commit_after>package main

import (
	"flag"
	"os"
	"os/signal"
	"strings"
	"syscall"

	"github.com/contactless/wb-rules/wbrules"
	"github.com/contactless/wbgo"
)

const (
	DRIVER_CLIENT_ID = "rules"
	DRIVER_CONV_ID   = "wb-rules"
	ENGINE_CLIENT_ID = "wb-rules-engine"

	PERSISTENT_DB_FILE      = "/var/lib/wirenboard/wbrules-persistent.db"
	VIRTUAL_DEVICES_DB_FILE = "/var/lib/wirenboard/wbrules-vdev.db"

	WBRULES_MODULES_ENV = "WB_RULES_MODULES"
)

func main() {
	brokerAddress := flag.String("broker", "tcp://localhost:1883", "MQTT broker url")
	editDir := flag.String("editdir", "", "Editable script directory")
	debug := flag.Bool("debug", false, "Enable debugging")
	noQueues := flag.Bool("debug-queues", false, "Don't use queues in wbgo driver (debugging)")
	useSyslog := flag.Bool("syslog", false, "Use syslog for logging")
	mqttDebug := flag.Bool("mqttdebug", false, "Enable MQTT debugging")
	precise := flag.Bool("precise", false, "Don't reown devices without driver")
	cleanup := flag.Bool("cleanup", false, "Clean up MQTT data on unload")

	persistentDbFile := flag.String("pdb", PERSISTENT_DB_FILE, "Persistent storage DB file")
	vdevDbFile := flag.String("vdb", VIRTUAL_DEVICES_DB_FILE, "Virtual devices values DB file")

	flag.Parse()
	if flag.NArg() < 1 {
		wbgo.Error.Fatal("must specify rule file/directory name(s)")
	}
	if *useSyslog {
		wbgo.UseSyslog()
	}
	if *debug {
		wbgo.SetDebuggingEnabled(true)
	}
	if *mqttDebug {
		wbgo.EnableMQTTDebugLog(*useSyslog)
	}
	wbgo.MaybeInitProfiling(nil)

	// prepare exit signal channel
	exitCh := make(chan os.Signal, 1)
	signal.Notify(exitCh, syscall.SIGINT, syscall.SIGTERM)

	driverMqttClient := wbgo.NewPahoMQTTClient(*brokerAddress, DRIVER_CLIENT_ID)
	driverArgs := wbgo.NewDriverArgs().
		SetId(DRIVER_CONV_ID).
		SetMqtt(driverMqttClient).
		SetUseStorage(*vdevDbFile != "").
		SetStoragePath(*vdevDbFile).
		SetReownUnknownDevices(!*precise)

	if *noQueues {
		driverArgs.SetTesting()
	}

	driver, err := wbgo.NewDriverBase(driverArgs)
	if err != nil {
		wbgo.Error.Fatalf("error creating driver: %s", err)
	}

	wbgo.Info.Println("driver is created")

	if err := driver.StartLoop(); err != nil {
		wbgo.Error.Fatalf("error starting the driver: %s", err)
	}

	wbgo.Info.Println("driver loop is started")
	ready := make(chan struct{})
	driver.OnRetainReady(func(tx wbgo.DriverTx) {
		close(ready)
	})
	driver.SetFilter(&wbgo.AllDevicesFilter{})

	wbgo.Info.Println("wait for driver to become ready")
	<-ready
	wbgo.Info.Println("driver is ready")

	engineOptions := wbrules.NewESEngineOptions()
	engineOptions.SetPersistentDBFile(*persistentDbFile)
	engineOptions.SetModulesDirs(strings.Split(os.Getenv(WBRULES_MODULES_ENV), ":"))
	engineOptions.SetCleanupOnStop(*cleanup)

	if *noQueues {
		engineOptions.SetTesting(true)
	}

	engineMqttClient := wbgo.NewPahoMQTTClient(*brokerAddress, ENGINE_CLIENT_ID)
	engine, err := wbrules.NewESEngine(driver, engineMqttClient, engineOptions)
	if err != nil {
		wbgo.Error.Fatalf("error creating engine: %s", err)
	}
	engine.Start()

	gotSome := false
	watcher := wbgo.NewDirWatcher("\\.js$", engine)
	if *editDir != "" {
		engine.SetSourceRoot(*editDir)
	}
	for _, path := range flag.Args() {
		if err := watcher.Load(path); err != nil {
			wbgo.Error.Printf("error loading script file/dir %s: %s", path, err)
		} else {
			gotSome = true
		}
	}
	if !gotSome {
		wbgo.Error.Fatalf("no valid scripts found")
	}
	wbgo.Info.Println("all rule files are loaded")

	if *editDir != "" {
		rpc := wbgo.NewMQTTRPCServer("wbrules", engineMqttClient)
		rpc.Register(wbrules.NewEditor(engine))
		rpc.Start()
	}

	// wait for quit signal
	<-exitCh

	engine.Stop()
	driver.StopLoop()
	driver.Close()
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"fmt"
	"io"
	"log"
	"os"
	"os/exec"
	"sync"
	"text/tabwriter"
	"time"
)

const MaxInstances = 100

var nInstances = flag.Int("n", 1, fmt.Sprintf("Liczba instancji, z zakresu [1,%d]", MaxInstances))
var stdoutHandling = flag.String("stdout", "contest", "Obługa standardowego wyjścia: contest, all, tagged, files")
var stderrHandling = flag.String("stderr", "all", "Obsługa standardowe wyjścia diagnostycznego: all, tagged, files")
var filesPrefix = flag.String("prefix", "", "Prefiks nazwy plików wyjściowych generowanych przez -stdout=files i -stderr=files")
var warnRemaining = flag.Bool("warn_unreceived", true, "Ostrzegaj o wiadomościach, które pozostały nieodebrane po zakończeniu się instancji")
var stats = flag.Bool("print_stats", false, "Na koniec wypisz statystyki dotyczące poszczególnych instancji")

var binaryPath string

func writeFile(streamType string, i int, r io.Reader) error {
	basename := binaryPath // XXX -- remove extension
	if *filesPrefix != "" {
		basename = *filesPrefix
	}
	filename := fmt.Sprintf("%s.%s.%d", basename, streamType, i)
	f, err := os.Create(filename)
	if err != nil {
		return err
	}
	_, err = io.Copy(f, r)
	if err1 := f.Close(); err == nil {
		err = err1
	}
	return err
}

func Usage() {
	fmt.Fprintf(os.Stderr, "Uzycie: %s [opcje] program_do_uruchomienia\n", os.Args[0])
	flag.PrintDefaults()
	fmt.Fprintf(os.Stderr, `Sposoby obsługi wyjścia:
  contest: Wymuszaj, żeby tylko jedna instancja pisała na standardowe wyjście. Przekieruj jej wyjście na standardowe wyjście tego programu.
  all: Przekieruj wyjście wszystkich instancji na analogiczne wyjście tego programu.
  tagged: Przekieruj wyjście wszystkich instancji na analogiczne wyjście tego programy, dopisując numer instancji na początku każdej linijki.
  files: Zapisz wyjście każdej instancji w osobnym pliku.
`)
}

func main() {
	flag.Usage = Usage
	flag.Parse()
	if flag.NArg() != 1 {
		fmt.Fprintf(os.Stderr, "Nie podałeś programu do uruchomienia\n")
		flag.Usage()
		os.Exit(1)
	}
	binaryPath = flag.Arg(0)

	if *nInstances < 1 || *nInstances > MaxInstances {
		fmt.Fprintf(os.Stderr, "Liczba instancji powinna być z zakresu [1,%d], a podałeś %d\n", MaxInstances, *nInstances)
		flag.Usage()
		os.Exit(1)
	}
	var writeStdout func(int, io.Reader) error
	contestStdout := &ContestStdout{Output: os.Stdout}
	switch *stdoutHandling {
	case "contest":
		// This is handled specially (without a pipe) below.
	case "all":
	case "tagged":
		writeStdout = func(i int, r io.Reader) error { return TagStream(fmt.Sprintf("STDOUT %d: ", i), os.Stdout, r) }
	case "files":
		writeStdout = func(i int, r io.Reader) error { return writeFile("stdout", i, r) }
	default:
		fmt.Fprintf(os.Stderr, "Niewłaściwa metoda obsługi standardowego wyjścia: %s", *stdoutHandling)
		flag.Usage()
		os.Exit(1)
	}
	var writeStderr func(int, io.Reader) error
	switch *stderrHandling {
	case "all":
	case "tagged":
		writeStderr = func(i int, r io.Reader) error { return TagStream(fmt.Sprintf("STDERR %d: ", i), os.Stderr, r) }
	case "files":
		writeStdout = func(i int, r io.Reader) error { return writeFile("stderr", i, r) }
	default:
		fmt.Fprintf(os.Stderr, "Niewłaściwa metoda obsługi standardowego wyjścia diagnostycznego: %s", *stdoutHandling)
		flag.Usage()
		os.Exit(1)
	}

	stdinPipe, err := NewFilePipe()
	if err != nil {
		log.Fatal(err)
	}
	defer stdinPipe.Release()
	go func() {
		_, err := io.Copy(stdinPipe, os.Stdin)
		if err != nil {
			log.Fatal(err)
		}
		err = stdinPipe.Close()
		if err != nil {
			log.Fatal(err)
		}
	}()
	progs := make([]*exec.Cmd, *nInstances)
	var wg sync.WaitGroup
	closeAfterWait := []io.Closer{}
	for i := range progs {
		cmd := exec.Command(flag.Arg(0))
		w, err := cmd.StdinPipe()
		if err != nil {
			log.Fatal(err)
		}
		go func() {
			// We don't care about errors from the writer (we expect broken pipe if the process has exited
			// before reading all of its input), but we do care about errors when reading from the filepipe.
			if _, err := io.Copy(WrapWriter(w), stdinPipe.Reader()); err != nil {
				if _, ok := err.(WriterError); !ok {
					log.Fatal(err)
				}
			}
			w.Close()
		}()
		makeFromWrite := func(writeProc func(int, io.Reader) error, w io.Writer) io.Writer {
			if writeProc == nil {
				return w
			}
			pr, pw := io.Pipe()
			closeAfterWait = append(closeAfterWait, pw)
			i := i
			wg.Add(1)
			go func() {
				err := writeProc(i, pr)
				if err != nil {
					// All the errors we can get are not caused by instances' invalid behaviour, but
					// by system issues (can't create a file, broken pipe on real stdout/err, etc.)
					log.Fatal(err)
				}
				wg.Done()
			}()
			return pw
		}
		if *stdoutHandling == "contest" {
			cmd.Stdout = contestStdout.NewWriter(i)
		} else {
			cmd.Stdout = makeFromWrite(writeStdout, os.Stdout)
		}
		cmd.Stderr = makeFromWrite(writeStderr, os.Stderr)
		progs[i] = cmd
	}
	instances, err := RunInstances(progs)
	for _, f := range closeAfterWait {
		f.Close()
	}
	wg.Wait()
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	for i, instance := range instances {
		buf := instance.ShutdownQueues()
		if *warnRemaining && len(buf) > 0 {
			fmt.Fprintf(os.Stderr, "Uwaga: Instancja %d nie odebrała %d wiadomości dla niej przeznaczonych przed swoim zakończeniem.\n", i, len(buf))
		}
	}
	var maxTime time.Duration
	var lastInstance int
	for i, instance := range instances {
		if instanceTime := instance.TimeRunning + instance.TimeBlocked; instanceTime >= maxTime {
			maxTime = instanceTime
			lastInstance = i
		}
	}
	fmt.Fprintf(os.Stderr, "Czas trwania: %v (najdłużej działająca instancja: %d)\n", maxTime, lastInstance)
	if *stats {
		w := tabwriter.NewWriter(os.Stderr, 2, 1, 1, ' ', 0)
		io.WriteString(w, "Instancja\tCzas całkowity\tCzas CPU\tCzas oczekiwania\tWysłane wiadomości\tWysłane bajty\n")
		for i, instance := range instances {
			fmt.Fprintf(w, "%d\t%v\t%v\t%v\t%d\t%d\n", i, instance.TimeRunning + instance.TimeBlocked, instance.TimeRunning, instance.TimeBlocked, instance.MessagesSent, instance.MessageBytesSent)
		}
		w.Flush()
	}
}
<commit_msg>fixed stdout/err filename generation<commit_after>package main

import (
	"flag"
	"fmt"
	"io"
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"sync"
	"text/tabwriter"
	"time"
)

const MaxInstances = 100

var nInstances = flag.Int("n", 1, fmt.Sprintf("Liczba instancji, z zakresu [1,%d]", MaxInstances))
var stdoutHandling = flag.String("stdout", "contest", "Obługa standardowego wyjścia: contest, all, tagged, files")
var stderrHandling = flag.String("stderr", "all", "Obsługa standardowe wyjścia diagnostycznego: all, tagged, files")
var filesPrefix = flag.String("prefix", "", "Prefiks nazwy plików wyjściowych generowanych przez -stdout=files i -stderr=files")
var warnRemaining = flag.Bool("warn_unreceived", true, "Ostrzegaj o wiadomościach, które pozostały nieodebrane po zakończeniu się instancji")
var stats = flag.Bool("print_stats", false, "Na koniec wypisz statystyki dotyczące poszczególnych instancji")

var binaryPath string

func writeFile(streamType string, i int, r io.Reader) error {
	binaryDir, binaryFile := filepath.Split(binaryPath)
	if idx := strings.LastIndex(binaryFile, "."); idx != -1 {
		binaryFile = binaryFile[:idx]
	}
	basename := filepath.Join(binaryDir, binaryFile)
	if *filesPrefix != "" {
		basename = *filesPrefix
	}
	filename := fmt.Sprintf("%s.%s.%d", basename, streamType, i)
	f, err := os.Create(filename)
	if err != nil {
		return err
	}
	_, err = io.Copy(f, r)
	if err1 := f.Close(); err == nil {
		err = err1
	}
	return err
}

func Usage() {
	fmt.Fprintf(os.Stderr, "Uzycie: %s [opcje] program_do_uruchomienia\n", os.Args[0])
	flag.PrintDefaults()
	fmt.Fprintf(os.Stderr, `Sposoby obsługi wyjścia:
  contest: Wymuszaj, żeby tylko jedna instancja pisała na standardowe wyjście. Przekieruj jej wyjście na standardowe wyjście tego programu.
  all: Przekieruj wyjście wszystkich instancji na analogiczne wyjście tego programu.
  tagged: Przekieruj wyjście wszystkich instancji na analogiczne wyjście tego programy, dopisując numer instancji na początku każdej linijki.
  files: Zapisz wyjście każdej instancji w osobnym pliku.
`)
}

func main() {
	flag.Usage = Usage
	flag.Parse()
	if flag.NArg() != 1 {
		fmt.Fprintf(os.Stderr, "Nie podałeś programu do uruchomienia\n")
		flag.Usage()
		os.Exit(1)
	}
	binaryPath = flag.Arg(0)

	if *nInstances < 1 || *nInstances > MaxInstances {
		fmt.Fprintf(os.Stderr, "Liczba instancji powinna być z zakresu [1,%d], a podałeś %d\n", MaxInstances, *nInstances)
		flag.Usage()
		os.Exit(1)
	}
	var writeStdout func(int, io.Reader) error
	contestStdout := &ContestStdout{Output: os.Stdout}
	switch *stdoutHandling {
	case "contest":
		// This is handled specially (without a pipe) below.
	case "all":
	case "tagged":
		writeStdout = func(i int, r io.Reader) error { return TagStream(fmt.Sprintf("STDOUT %d: ", i), os.Stdout, r) }
	case "files":
		writeStdout = func(i int, r io.Reader) error { return writeFile("stdout", i, r) }
	default:
		fmt.Fprintf(os.Stderr, "Niewłaściwa metoda obsługi standardowego wyjścia: %s", *stdoutHandling)
		flag.Usage()
		os.Exit(1)
	}
	var writeStderr func(int, io.Reader) error
	switch *stderrHandling {
	case "all":
	case "tagged":
		writeStderr = func(i int, r io.Reader) error { return TagStream(fmt.Sprintf("STDERR %d: ", i), os.Stderr, r) }
	case "files":
		writeStdout = func(i int, r io.Reader) error { return writeFile("stderr", i, r) }
	default:
		fmt.Fprintf(os.Stderr, "Niewłaściwa metoda obsługi standardowego wyjścia diagnostycznego: %s", *stdoutHandling)
		flag.Usage()
		os.Exit(1)
	}

	stdinPipe, err := NewFilePipe()
	if err != nil {
		log.Fatal(err)
	}
	defer stdinPipe.Release()
	go func() {
		_, err := io.Copy(stdinPipe, os.Stdin)
		if err != nil {
			log.Fatal(err)
		}
		err = stdinPipe.Close()
		if err != nil {
			log.Fatal(err)
		}
	}()
	progs := make([]*exec.Cmd, *nInstances)
	var wg sync.WaitGroup
	closeAfterWait := []io.Closer{}
	for i := range progs {
		cmd := exec.Command(flag.Arg(0))
		w, err := cmd.StdinPipe()
		if err != nil {
			log.Fatal(err)
		}
		go func() {
			// We don't care about errors from the writer (we expect broken pipe if the process has exited
			// before reading all of its input), but we do care about errors when reading from the filepipe.
			if _, err := io.Copy(WrapWriter(w), stdinPipe.Reader()); err != nil {
				if _, ok := err.(WriterError); !ok {
					log.Fatal(err)
				}
			}
			w.Close()
		}()
		makeFromWrite := func(writeProc func(int, io.Reader) error, w io.Writer) io.Writer {
			if writeProc == nil {
				return w
			}
			pr, pw := io.Pipe()
			closeAfterWait = append(closeAfterWait, pw)
			i := i
			wg.Add(1)
			go func() {
				err := writeProc(i, pr)
				if err != nil {
					// All the errors we can get are not caused by instances' invalid behaviour, but
					// by system issues (can't create a file, broken pipe on real stdout/err, etc.)
					log.Fatal(err)
				}
				wg.Done()
			}()
			return pw
		}
		if *stdoutHandling == "contest" {
			cmd.Stdout = contestStdout.NewWriter(i)
		} else {
			cmd.Stdout = makeFromWrite(writeStdout, os.Stdout)
		}
		cmd.Stderr = makeFromWrite(writeStderr, os.Stderr)
		progs[i] = cmd
	}
	instances, err := RunInstances(progs)
	for _, f := range closeAfterWait {
		f.Close()
	}
	wg.Wait()
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	for i, instance := range instances {
		buf := instance.ShutdownQueues()
		if *warnRemaining && len(buf) > 0 {
			fmt.Fprintf(os.Stderr, "Uwaga: Instancja %d nie odebrała %d wiadomości dla niej przeznaczonych przed swoim zakończeniem.\n", i, len(buf))
		}
	}
	var maxTime time.Duration
	var lastInstance int
	for i, instance := range instances {
		if instanceTime := instance.TimeRunning + instance.TimeBlocked; instanceTime >= maxTime {
			maxTime = instanceTime
			lastInstance = i
		}
	}
	fmt.Fprintf(os.Stderr, "Czas trwania: %v (najdłużej działająca instancja: %d)\n", maxTime, lastInstance)
	if *stats {
		w := tabwriter.NewWriter(os.Stderr, 2, 1, 1, ' ', 0)
		io.WriteString(w, "Instancja\tCzas całkowity\tCzas CPU\tCzas oczekiwania\tWysłane wiadomości\tWysłane bajty\n")
		for i, instance := range instances {
			fmt.Fprintf(w, "%d\t%v\t%v\t%v\t%d\t%d\n", i, instance.TimeRunning + instance.TimeBlocked, instance.TimeRunning, instance.TimeBlocked, instance.MessagesSent, instance.MessageBytesSent)
		}
		w.Flush()
	}
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"github.com/docopt/docopt-go"
	"os/exec"
)

func main() {
	usage := `Git Substitute.

Usage:
  git-substitute <search-pattern> <replace-pattern>
  git-substitute -h | --help
  git-substitute -V | --version

Options:
  -h --help     Show this screen.
  --version     Show version.`

	arguments, _ := docopt.Parse(usage, nil, true, "Git Substitute 1.0", false)
	grep := exec.Command("git", "grep", "-El", fmt.Sprintf("%s", arguments["<search-pattern>"]))
	sed := exec.Command("xargs", "sed", "-ri", fmt.Sprintf("s/%s/%s/g", arguments["<search-pattern>"], arguments["<replace-pattern>"]))
	grepOut, _ := grep.StdoutPipe()
	grep.Start()
	sed.Stdin = grepOut
	output, _ := sed.CombinedOutput()
	fmt.Print(string(output))
}
<commit_msg>add optional path argument<commit_after>package main

import (
	"fmt"
	"github.com/docopt/docopt-go"
	"os/exec"
)

func main() {
	usage := `Git Substitute.

Usage:
  git-substitute <search-pattern> <replace-pattern> [<path>]
  git-substitute -h | --help
  git-substitute -V | --version

Options:
  -h --help     Show this screen.
  --version     Show version.`

	arguments, _ := docopt.Parse(usage, nil, true, "Git Substitute 1.0", false)
	grepArgs := []string{"grep", "-El", fmt.Sprintf("%s", arguments["<search-pattern>"])}
	if arguments["<path>"] != nil {
		grepArgs = append(grepArgs, arguments["<path>"].(string))
	}
	grep := exec.Command("git", grepArgs...)
	sed := exec.Command("xargs", "sed", "-ri", fmt.Sprintf("s/%s/%s/g", arguments["<search-pattern>"], arguments["<replace-pattern>"]))
	grepOut, _ := grep.StdoutPipe()
	grep.Start()
	sed.Stdin = grepOut
	output, _ := sed.CombinedOutput()
	fmt.Print(string(output))
}
<|endoftext|>


<commit_before>package main

import (
	"boltapi/boltdb"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"reflect"

	"github.com/gorilla/mux"
)

type userRequest struct {
	db, bucket, key string
}

func init() {
	log.SetFlags(log.LstdFlags | log.Lshortfile)
}

var database *boltdb.Database

const dbsFolder string = "/home/ubuntu/boltdbs"

func main() {

	if _, err := os.Stat(dbsFolder); os.IsNotExist(err) {
		os.Mkdir(dbsFolder, os.ModePerm)
	}

	port := ":8080"
	log.Println("Server Listening on Port: ", port)

	router := mux.NewRouter().StrictSlash(true)

	router.HandleFunc("/dbs/", requestHandler)
	router.HandleFunc("/dbs/{db}/", requestHandler)
	router.HandleFunc("/dbs/{db}/stats/", requestHandler)
	router.HandleFunc("/dbs/{db}/compact/", requestHandler)
	router.HandleFunc("/dbs/{db}/buckets/{bucketName}/keys/{keyName}", requestHandler)

	log.Fatal(http.ListenAndServe(port, router))
}

func requestHandler(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	var userRequest userRequest
	userRequest.db = vars["db"]
	userRequest.bucket = vars["bucketName"]
	userRequest.key = vars["keyName"]

	if len(userRequest.db) > 0 && (database == nil || (userRequest.db != database.CurrentDB())) {
		var err error
		database, err = openDB(userRequest.db)
		handleErr(err)
	}

	switch r.Method {
	case "PUT":
		put(w, r, userRequest)
	case "GET":
		get(w, r, userRequest)
	case "DELETE":
		delete(w, r, userRequest)
	}
}

func openDB(dbfile string) (d *boltdb.Database, err error) {
	if database != nil {
		err := database.DB.Close()
		handleErr(err)
	}
	dbPath := dbsFolder + "/" + dbfile
	database, err := boltdb.NewDatabase(dbPath)
	handleErr(err)
	return database, err
}

func getCurrentDB(w http.ResponseWriter, r *http.Request, userRequest userRequest) {
	if database == nil {
		log.Println("DB = Nil")
		w.Write([]byte("{\"database\":\"none\"}"))
	} else {
		dbPath := database.CurrentDB()
		w.Write([]byte("{\"database\":\"" + dbPath + "\"}"))
	}
}

func get(w http.ResponseWriter, r *http.Request, userRequest userRequest) {
	log.Println(userRequest)
	if len(userRequest.key) > 0 {
		//User has specified a db, bucket & key
		res, err := database.Get([]byte(userRequest.bucket), []byte(userRequest.key))
		handleErr(err)
		response := "{\"" + userRequest.key + "\":\"" + string(res) + "\"}"
		w.Write([]byte(response))
	} else if len(userRequest.db) > 0 {
		//User has only specified a db, return data about this db
		res := database.BK.Stats()
		log.Println(reflect.TypeOf(res), res)

	} else {
		//User has not specified any db, return data about all DBs
	}

}

func put(w http.ResponseWriter, r *http.Request, userRequest userRequest) {
	if len(userRequest.key) > 0 {
		//User has specified a db, bucket & key
		vars := mux.Vars(r)
		bucket := vars["bucket"]
		key := vars["key"]
		val, err := ioutil.ReadAll(r.Body)
		handleErr(err)
		database.Put([]byte(bucket), []byte(key), []byte(val))
	} else if len(userRequest.db) > 0 {
		//User has only specified a db, open a new database
	} else {
		//User has not specified any db, do nothing
	}

}
func delete(w http.ResponseWriter, r *http.Request, userRequest userRequest) error {
	if database != nil {
		err := database.DB.Close()
		handleErr(err)
	}
	dbFolder := "/home/ubuntu/"
	dbPath := dbFolder + userRequest.db
	err := os.Remove(dbPath)
	handleErr(err)
	return err
}
func handleErr(err error) {
	if err != nil {
		log.Fatal(err)
	}
}
<commit_msg>requestHandler Update logic to handle newly defined API endpoints.<commit_after>package main

import (
	"boltapi/boltdb"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"reflect"
	"strings"

	"github.com/gorilla/mux"
)

type userRequest struct {
	db, bucket, key, resource string
}

func init() {
	log.SetFlags(log.LstdFlags | log.Lshortfile)
}

var database *boltdb.Database

const dbsFolder string = "/home/ubuntu/boltdbs"

func main() {

	if _, err := os.Stat(dbsFolder); os.IsNotExist(err) {
		os.Mkdir(dbsFolder, os.ModePerm)
	}

	port := ":8080"
	log.Println("Server Listening on Port: ", port)

	router := mux.NewRouter().StrictSlash(true)

	//TODO: Router paths need to be cleaned up with regular expressions/options
	router.HandleFunc("/dbs/", requestHandler)
	router.HandleFunc("/dbs/{db}/", requestHandler)
	router.HandleFunc("/dbs/{db}/stats/", requestHandler)
	router.HandleFunc("/dbs/{db}/compact/", requestHandler)
	router.HandleFunc("/dbs/{db}/buckets/", requestHandler)
	router.HandleFunc("/dbs/{db}/buckets/{bucketName}", requestHandler)
	router.HandleFunc("/dbs/{db}/buckets/{bucketName}/keys", requestHandler)
	router.HandleFunc("/dbs/{db}/buckets/{bucketName}/keys/{keyName}", requestHandler)

	log.Fatal(http.ListenAndServe(port, router))
}

func requestHandler(w http.ResponseWriter, r *http.Request) {
	//Get variables {db}, {bucketName}, and {keyName} from user request URL
	vars := mux.Vars(r)
	var userRequest userRequest
	userRequest.db = vars["db"]
	userRequest.bucket = vars["bucketName"]
	userRequest.key = vars["keyName"]

	reqURI := r.URL.RequestURI()
	reqURI = reqURI[1 : len(reqURI)-1]
	uri := strings.Split(reqURI, "/")

	if len(userRequest.db) > 0 && (database == nil || (userRequest.db != database.CurrentDB())) {
		var err error
		database, err = openDB(userRequest.db)
		handleErr(err)
	}

	if (len(uri) % 2) == 0 {
		//Even number of entries in uri means we ended with either a specific db, bucket or key
		log.Println("Even URI")
		if len(vars["keyName"]) > 0 {
			/*
				If we have a keyName, then the user has also specified a dbs and bucket
				Possible Actions:
					GET - Read a value given a key in the URL
					PUT - Insert a value from the body, given a key in the URL
					DELETE - Delete a key/value pair, given a key in the URL
			*/
		} else if len(vars["bucketName"]) > 0 {
			/*
				We have a bucketName but not a keyName
				Possible Actions:
					DELETE - Delete bucket & all contents.
			*/
		} else if len(vars["db"]) > 0 {
			/*
				We only have a database name.
				Possible actions:
					DELETE - Delite entire database
			*/
		}
	} else {
		//Odd number of entries in uri means we ended with either dbs, buckets or keys (general)
		cmd := uri[len(uri)-1]
		log.Println(cmd)

	}

	w.Write([]byte("None"))
	/*
		switch r.Method {
		case "PUT":
			put(w, r, userRequest)
		case "GET":
			get(w, r, userRequest)
		case "DELETE":
			delete(w, r, userRequest)
		}*/
}

func openDB(dbfile string) (d *boltdb.Database, err error) {
	if database != nil {
		err := database.DB.Close()
		handleErr(err)
	}
	dbPath := dbsFolder + "/" + dbfile
	database, err := boltdb.NewDatabase(dbPath)
	handleErr(err)
	return database, err
}

func getCurrentDB(w http.ResponseWriter, r *http.Request, userRequest userRequest) {
	if database == nil {
		log.Println("DB = Nil")
		w.Write([]byte("{\"database\":\"none\"}"))
	} else {
		dbPath := database.CurrentDB()
		w.Write([]byte("{\"database\":\"" + dbPath + "\"}"))
	}
}

func get(w http.ResponseWriter, r *http.Request, userRequest userRequest) {
	log.Println(userRequest)
	if len(userRequest.key) > 0 {
		//User has specified a db, bucket & key
		res, err := database.Get([]byte(userRequest.bucket), []byte(userRequest.key))
		handleErr(err)
		response := "{\"" + userRequest.key + "\":\"" + string(res) + "\"}"
		w.Write([]byte(response))
	} else if len(userRequest.db) > 0 {
		//User has only specified a db, return data about this db
		res := database.BK.Stats()
		log.Println(reflect.TypeOf(res), res)

	} else {
		//User has not specified any db, return data about all DBs
	}

}

func put(w http.ResponseWriter, r *http.Request, userRequest userRequest) {
	if len(userRequest.key) > 0 {
		//User has specified a db, bucket & key
		vars := mux.Vars(r)
		bucket := vars["bucket"]
		key := vars["key"]
		val, err := ioutil.ReadAll(r.Body)
		handleErr(err)
		database.Put([]byte(bucket), []byte(key), []byte(val))
	} else if len(userRequest.db) > 0 {
		//User has only specified a db, open a new database
	} else {
		//User has not specified any db, do nothing
	}

}
func delete(w http.ResponseWriter, r *http.Request, userRequest userRequest) error {
	if database != nil {
		err := database.DB.Close()
		handleErr(err)
	}
	dbFolder := "/home/ubuntu/"
	dbPath := dbFolder + userRequest.db
	err := os.Remove(dbPath)
	handleErr(err)
	return err
}
func handleErr(err error) {
	if err != nil {
		log.Fatal(err)
	}
}
<|endoftext|>


<commit_before>package main

import (
	"bytes"
	"errors"
	"flag"
	"fmt"
	"os"
	"strconv"
	"time"
)

const format = time.Kitchen

type status int

const (
	ok   status = 0
	fail status = 1
)

type RGB [3]uint8

func (r RGB) To256() uint8 {
	return 16 + (inr(r[0]) * 36) + (inr(r[1]) * 6) + inr(r[2])
}

func inr(a uint8) uint8 {
	return uint8((float64(a) / 255) * 5)
}

func (r RGB) MarshalText() (text []byte, err error) {
	return []byte(fmt.Sprintf("%#X%#X%#X", r[0], r[1], r[2])), nil
}

func (r RGB) String() string {
	t, _ := r.MarshalText()
	return "#" + string(t)
}

func (r *RGB) UnmarshalText(text []byte) (err error) {
	n, err := fmt.Fscanf(bytes.NewReader(text), "%2x%2x%2x", &r[0], &r[1], &r[2])
	if err != nil {
		return
	}

	if n < 3 {
		err = fmt.Errorf("string %+q is an invalid RGB hex code", text)
	}
	return
}

var (
	colour          string
	backgroundcolor string
	tag             string
	terminal        bool
)

func main() {
	var s status
	defer os.Exit(int(s))

	//Flags
	flag.StringVar(
		&colour,
		"colour",
		"",
		"(foreground)color: returned in 256 colours if no -bgcolour provided, "+
			" otherwise, the foreground color in the :hi statement.",
	)

	flag.StringVar(
		&backgroundcolor,
		"bgcolour",
		"",
		"backgroundcolor for :hi statements. Providing this flag will return a vim :hi statement"+
			", unless -terminal is also set.",
	)

	flag.StringVar(
		&tag,
		"tag",
		"Example",
		"Hi statement tag.",
	)

	flag.BoolVar(
		&terminal,
		"term",
		false,
		"If set, writes an Xterm 256 color colur escape code to stdout",
	)

	flag.Parse()
	for inf := range run() {
		var err error
		switch v := inf.(type) {
		case status:
			s = v

		case error:
			_, err = fmt.Fprintf(os.Stderr, "Error: %s\n", v.Error())

		case string:
			_, err = os.Stdout.Write([]byte(v + "\n"))
		}

		if err != nil {
			panic(fmt.Sprintf("Fatal error: %s\n", err.Error()))
		}
	}

}

func run() (chn <-chan interface{}) {
	c := make(chan interface{})
	chn = c
	go func() {
		defer close(c)

		if colour == "" {
			c <- errors.New("No colour provided (-colour flag)")
			return
		}

		var fg RGB

		if err := fg.UnmarshalText([]byte(colour)); err != nil {
			c <- "Colour parsing failed."
			c <- err
			return
		}

		if backgroundcolor == "" {
			if terminal {
				c <- "\x1b[38;5;" + strconv.Itoa(int(fg.To256())) + "m" + "\x1b[0m"
				return
			}

			c <- strconv.Itoa(int(fg.To256()))
			return
		}

		if backgroundcolor == "transparent" {
			backgroundcolor = ""
		}

		if backgroundcolor == "" {

			c <- fmt.Sprintf("hi %s guifg=%s ctermfg=%s", tag, colour, strconv.Itoa(int(fg.To256())))
			return
		}

		var bg RGB
		if err := bg.UnmarshalText([]byte(backgroundcolor)); err != nil {
			c <- "Background colour parsing failed."
			c <- err
			return
		}

		c <- fmt.Sprintf(
			"hi %s guifg=%s guibg=%s ctermfg=%s ctermbg=%s",
			tag,
			colour,
			strconv.Itoa(int(fg.To256())),
			backgroundcolor,
			strconv.Itoa(int(bg.To256())),
		)
	}()
	return
}
<commit_msg>Whoops, add # before hex colours<commit_after>package main

import (
	"bytes"
	"errors"
	"flag"
	"fmt"
	"os"
	"strconv"
	"time"
)

const format = time.Kitchen

type status int

const (
	ok   status = 0
	fail status = 1
)

type RGB [3]uint8

func (r RGB) To256() uint8 {
	return 16 + (inr(r[0]) * 36) + (inr(r[1]) * 6) + inr(r[2])
}

func inr(a uint8) uint8 {
	return uint8((float64(a) / 255) * 5)
}

func (r RGB) MarshalText() (text []byte, err error) {
	return []byte(fmt.Sprintf("%#X%#X%#X", r[0], r[1], r[2])), nil
}

func (r RGB) String() string {
	t, _ := r.MarshalText()
	return "#" + string(t)
}

func (r *RGB) UnmarshalText(text []byte) (err error) {
	n, err := fmt.Fscanf(bytes.NewReader(text), "%2x%2x%2x", &r[0], &r[1], &r[2])
	if err != nil {
		return
	}

	if n < 3 {
		err = fmt.Errorf("string %+q is an invalid RGB hex code", text)
	}
	return
}

var (
	colour          string
	backgroundcolor string
	tag             string
	terminal        bool
)

func main() {
	var s status
	defer os.Exit(int(s))

	//Flags
	flag.StringVar(
		&colour,
		"colour",
		"",
		"(foreground)color: returned in 256 colours if no -bgcolour provided, "+
			" otherwise, the foreground color in the :hi statement.",
	)

	flag.StringVar(
		&backgroundcolor,
		"bgcolour",
		"",
		"backgroundcolor for :hi statements. Providing this flag will return a vim :hi statement"+
			", unless -terminal is also set.",
	)

	flag.StringVar(
		&tag,
		"tag",
		"Example",
		"Hi statement tag.",
	)

	flag.BoolVar(
		&terminal,
		"term",
		false,
		"If set, writes an Xterm 256 color colur escape code to stdout",
	)

	flag.Parse()
	for inf := range run() {
		var err error
		switch v := inf.(type) {
		case status:
			s = v

		case error:
			_, err = fmt.Fprintf(os.Stderr, "Error: %s\n", v.Error())

		case string:
			_, err = os.Stdout.Write([]byte(v + "\n"))
		}

		if err != nil {
			panic(fmt.Sprintf("Fatal error: %s\n", err.Error()))
		}
	}

}

func run() (chn <-chan interface{}) {
	c := make(chan interface{})
	chn = c
	go func() {
		defer close(c)

		if colour == "" {
			c <- errors.New("No colour provided (-colour flag)")
			return
		}

		var fg RGB

		if err := fg.UnmarshalText([]byte(colour)); err != nil {
			c <- "Colour parsing failed."
			c <- err
			return
		}

		if backgroundcolor == "" {
			if terminal {
				c <- "\x1b[38;5;" + strconv.Itoa(int(fg.To256())) + "m" + "\x1b[0m"
				return
			}

			c <- strconv.Itoa(int(fg.To256()))
			return
		}

		if backgroundcolor == "transparent" {
			backgroundcolor = ""
		}

		if backgroundcolor == "" {

			c <- fmt.Sprintf("hi %s guifg=#%s ctermfg=%s", tag, colour, strconv.Itoa(int(fg.To256())))
			return
		}

		var bg RGB
		if err := bg.UnmarshalText([]byte(backgroundcolor)); err != nil {
			c <- "Background colour parsing failed."
			c <- err
			return
		}

		c <- fmt.Sprintf(
			"hi %s guifg=#%s guibg=#%s ctermfg=%s ctermbg=%s",
			tag,
			colour,
			strconv.Itoa(int(fg.To256())),
			backgroundcolor,
			strconv.Itoa(int(bg.To256())),
		)
	}()
	return
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"fmt"
	"io"
	"log"
	"math"
	"net/http"
	"os"
	"os/exec"
	"strings"
)

const BYTES = 2048
const NAME = "goGet"

func main() {

	continued := false
	flag.Usage = usage

	// Set the flag to the variable
	flag.BoolVar(&continued, "continue", false, NAME+" --continue [url]")
	flag.Parse()

	args := flag.Args()
	if len(args) < 1 {
		fmt.Printf("Input URL is missing.\n")
		os.Exit(1)
	}
	url_reader(args[0], continued)
}

func usage() {

	fmt.Fprintf(os.Stderr, "Usage: %s [url]\n", NAME)

	flag.PrintDefaults()
	os.Exit(2)
}

// Function to get the size of the local file, used when the downloading is continued
func file_size(file_name string) int64 {

	file, err := os.Open(file_name)
	if err != nil {
		return 0
	}
	defer file.Close()

	file_stat, err := file.Stat()
	return file_stat.Size()
}

// Showing the status of the download in console
func show_data(url string, file_name string, remote_size int64, file_part int64, percent *float64) {

	new_percent := math.Floor((float64(file_part) / float64(remote_size)) * 100.00)
	// Only refresh the data with integers
	if new_percent > *percent {
		clear := exec.Command("clear")
		clear.Stdout = os.Stdout
		clear.Run()
		fmt.Printf("Downloading file from %s\n", url)
		fmt.Printf("Saving file to: %s\n", file_name)
		fmt.Printf("%.0f%% of 100%%\n", new_percent)
		fmt.Printf("%d of %d bytes\n", file_part, remote_size)
	}
	*percent = new_percent
}

// Function for retrieve the data of the file from URL
func get_url(url string, continued bool) (string, int64, int64, *http.Response, error) {

	remote_size := int64(0)
	file_part := int64(0)
	file_name := string("")

	resp, err := http.Get(url)
	if err != nil {
		return file_name, remote_size, file_part, resp, err
	}
	last_slash := strings.LastIndex(resp.Request.URL.Path, "/") // Getting the index of the last slash
	file_name = resp.Request.URL.Path[last_slash+1:] // Saving the name of the file

	if continued {
		file_part = file_size(file_name) // Getting size of the local file

		// Saving the size of the remote file
		if file_part == resp.ContentLength {
			remote_size = resp.ContentLength
		// If local file and remote has diferent size, then set the range to download
		} else if file_part != resp.ContentLength {
			range_part := fmt.Sprintf("bytes=%d-%d", file_part, resp.ContentLength)
			resp.Request.Header.Add("Range", range_part)
			// Doing a new request with the set range
			client := &http.Client{}
			resp, err = client.Do(resp.Request)
			if err != nil {
				return file_name, remote_size, file_part, resp, err
			}
			remote_size = resp.ContentLength + file_part
		}
	} else {
		os.Remove(file_name) // If there is a previous file, this is deleted
		file, err := os.OpenFile(file_name, os.O_RDWR|os.O_CREATE, 0666) // Create a new file
		if err != nil {
			return file_name, remote_size, file_part, resp, err
		}
		file.Close()
		remote_size = resp.ContentLength
	}
	return file_name, remote_size, file_part, resp, err
}

// Function to get the file from URL and save it in to disc
func url_reader(url string, continued bool) int64 {

	file_name, remote_size, file_part, resp, err := get_url(url, continued)
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()

	accum := int64(file_part) // Read bytes
	percent := float64(0)
	buffer := make([]byte, BYTES)

	// Loop to read the remote file and write the local file
	for {
		n, err := resp.Body.Read(buffer)
		if err != nil && err != io.EOF {
			log.Fatal(err)
		}
		if n == 0 {
			return accum
		}
		if remote_size == accum {
			fmt.Printf("File is already on disc\n")
			return accum
		}
		err = file_writer(file_name, buffer[0:n], accum)
		if err != nil {
			log.Fatal(err)
		}
		accum += int64(n)
		// Refresh data in console
		show_data(url, file_name, remote_size, accum, &percent)
	}
	return accum
}

// Function to write the buffer in to the local file
func file_writer(file_name string, buffer []byte, offset int64) error {
	// For read and write access, if file doesn't exist this is created.
	file, err := os.OpenFile(file_name, os.O_RDWR|os.O_CREATE, 0660)
	if err != nil {
		return err
	}
	defer file.Close()
	// The write begin with an offset
	_, err = file.WriteAt(buffer, offset)
	if err != nil {
		return err
	}
	return nil
}
<commit_msg>Cambiando la forma de declarar las variables<commit_after>package main

import (
	"flag"
	"fmt"
	"io"
	"log"
	"math"
	"net/http"
	"os"
	"os/exec"
	"strings"
)

const BYTES = 2048
const NAME = "goGet"

func main() {

	continued := false
	flag.Usage = usage

	// Set the flag to the variable
	flag.BoolVar(&continued, "continue", false, NAME+" --continue [url]")
	flag.Parse()

	args := flag.Args()
	if len(args) < 1 {
		fmt.Printf("Input URL is missing.\n")
		os.Exit(1)
	}
	url_reader(args[0], continued)
}

func usage() {

	fmt.Fprintf(os.Stderr, "Usage: %s [url]\n", NAME)

	flag.PrintDefaults()
	os.Exit(2)
}

// Function to get the size of the local file, used when the downloading is continued
func file_size(file_name string) int64 {

	file, err := os.Open(file_name)
	if err != nil {
		return 0
	}
	defer file.Close()

	file_stat, err := file.Stat()
	return file_stat.Size()
}

// Showing the status of the download in console
func show_data(url string, file_name string, remote_size int64, file_part int64, percent *float64) {

	new_percent := math.Floor((float64(file_part) / float64(remote_size)) * 100.00)
	// Only refresh the data with integers
	if new_percent > *percent {
		clear := exec.Command("clear")
		clear.Stdout = os.Stdout
		clear.Run()
		fmt.Printf("Downloading file from %s\n", url)
		fmt.Printf("Saving file to: %s\n", file_name)
		fmt.Printf("%.0f%% of 100%%\n", new_percent)
		fmt.Printf("%d of %d bytes\n", file_part, remote_size)
	}
	*percent = new_percent
}

// Function for retrieve the data of the file from URL
func get_url(url string, continued bool) (string, int64, int64, *http.Response, error) {

	var remote_size, file_part int64
	var file_name string

	resp, err := http.Get(url)
	if err != nil {
		return file_name, remote_size, file_part, resp, err
	}
	last_slash := strings.LastIndex(resp.Request.URL.Path, "/") // Getting the index of the last slash
	file_name = resp.Request.URL.Path[last_slash+1:] // Saving the name of the file

	if continued {
		file_part = file_size(file_name) // Getting size of the local file

		// Saving the size of the remote file
		if file_part == resp.ContentLength {
			remote_size = resp.ContentLength
		// If local file and remote has diferent size, then set the range to download
		} else if file_part != resp.ContentLength {
			range_part := fmt.Sprintf("bytes=%d-%d", file_part, resp.ContentLength)
			resp.Request.Header.Add("Range", range_part)
			// Doing a new request with the set range
			client := &http.Client{}
			resp, err = client.Do(resp.Request)
			if err != nil {
				return file_name, remote_size, file_part, resp, err
			}
			remote_size = resp.ContentLength + file_part
		}
	} else {
		os.Remove(file_name) // If there is a previous file, this is deleted
		file, err := os.OpenFile(file_name, os.O_RDWR|os.O_CREATE, 0666) // Create a new file
		if err != nil {
			return file_name, remote_size, file_part, resp, err
		}
		file.Close()
		remote_size = resp.ContentLength
	}
	return file_name, remote_size, file_part, resp, err
}

// Function to get the file from URL and save it in to disc
func url_reader(url string, continued bool) int64 {

	file_name, remote_size, file_part, resp, err := get_url(url, continued)
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()

	accum := int64(file_part) // Read bytes
	percent := float64(0)
	buffer := make([]byte, BYTES)

	// Loop to read the remote file and write the local file
	for {
		n, err := resp.Body.Read(buffer)
		if err != nil && err != io.EOF {
			log.Fatal(err)
		}
		if n == 0 {
			return accum
		}
		if remote_size == accum {
			fmt.Printf("File is already on disc\n")
			return accum
		}
		err = file_writer(file_name, buffer[0:n], accum)
		if err != nil {
			log.Fatal(err)
		}
		accum += int64(n)
		// Refresh data in console
		show_data(url, file_name, remote_size, accum, &percent)
	}
	return accum
}

// Function to write the buffer in to the local file
func file_writer(file_name string, buffer []byte, offset int64) error {
	// For read and write access, if file doesn't exist this is created.
	file, err := os.OpenFile(file_name, os.O_RDWR|os.O_CREATE, 0660)
	if err != nil {
		return err
	}
	defer file.Close()
	// The write begin with an offset
	_, err = file.WriteAt(buffer, offset)
	if err != nil {
		return err
	}
	return nil
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"log"
	"os"
	"os/signal"
	"sync"
	"syscall"
	"time"
)

var (
	zones = &ZoneStore{
		store: make(map[string]Zone),
		m:     new(sync.RWMutex),
		seri:  make(map[string]uint64),
	}
	config     = &Configs{
		ip:		"10.5.4.59",
		port:	8888,
		auth:	"",
	}
	configPath	string
	listenOn    string
	recurseTo   string
	apiKey      string
	buildtime   string
	buildcommit string

)

type Configs struct {
	ip			string
	port		int
	auth		string
}

func main() {
	flag.StringVar(&configPath, "c", "conf.json", "The configs in JSON format")
	flag.StringVar(&listenOn, "l", "", "The IP to listen on (default = blank = ALL)")
	flag.StringVar(&recurseTo, "r", "", "Pass-through requests that we can't answer to other DNS server (address:port or empty=disabled)")
	flag.StringVar(&apiKey, "k", "", "API key for http notifications")
	flag.Parse()

	log.Println("firedns (2015) by Matis Hsiao is starting...")
	log.Printf("bult %s from commit %s", buildtime, buildcommit)

	Connect(config.ip,config.port,config.auth)
	prefetch(zones, true)

	server := &Server{
		host:     listenOn,
		port:     53,
		rTimeout: 5 * time.Second,
		wTimeout: 5 * time.Second,
		zones:    zones,
	}

	server.Run()

	go StartHTTP()

	sig := make(chan os.Signal)
	signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
	for {
		select {
		case s := <-sig:
			log.Fatalf("Signal (%d) received, stopping\n", s)
		}
	}
}
<commit_msg>add ssdb ip from command line args "c"<commit_after>package main

import (
	"flag"
	"log"
	"os"
	"os/signal"
	"sync"
	"syscall"
	"time"
)

var (
	zones = &ZoneStore{
		store: make(map[string]Zone),
		m:     new(sync.RWMutex),
		seri:  make(map[string]uint64),
	}
	config     = &Configs{
		ip:		"10.5.4.59",
		port:	8888,
		auth:	"",
	}
	configPath	string
	listenOn    string
	recurseTo   string
	apiKey      string
	buildtime   string
	buildcommit string

)

type Configs struct {
	ip			string
	port		int
	auth		string
}

func main() {
	flag.StringVar(&configPath, "c", "10.5.4.59", "The configs ip")
	flag.StringVar(&listenOn, "l", "", "The IP to listen on (default = blank = ALL)")
	flag.StringVar(&recurseTo, "r", "", "Pass-through requests that we can't answer to other DNS server (address:port or empty=disabled)")
	flag.StringVar(&apiKey, "k", "", "API key for http notifications")
	flag.Parse()

	log.Println("firedns (2015) by Matis Hsiao is starting...")
	log.Printf("bult %s from commit %s", buildtime, buildcommit)
	config.ip = configPath
	Connect(config.ip,config.port,config.auth)
	prefetch(zones, true)

	server := &Server{
		host:     listenOn,
		port:     53,
		rTimeout: 5 * time.Second,
		wTimeout: 5 * time.Second,
		zones:    zones,
	}

	server.Run()

	go StartHTTP()

	sig := make(chan os.Signal)
	signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
	for {
		select {
		case s := <-sig:
			log.Fatalf("Signal (%d) received, stopping\n", s)
		}
	}
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"github.com/tillberg/ansi-log"
	"os"
	"os/signal"
	"time"
)

var colors []string = []string{"red", "yellow", "magenta", "cyan", "blue"}

func writeStuff(num int, stop chan bool, done chan bool) {
	i := 0
	color := colors[num/4]
	out := log.New(os.Stderr, fmt.Sprintf("@(dim)[@(green:writer-%d)] ", num), log.Lelapsed)
	format := fmt.Sprintf("@(dim:My number is) @(%s:%%d)", color)
	for {
		select {
		case <-stop:
			out.Close()
			done <- true
			return
		default:
			out.Replacef(format, i)
			i += num
			if i > 10000 {
				i = 0
				out.Println()
			}
		}
		time.Sleep(time.Microsecond)
	}
}

func main() {
	log.EnableMultilineMode()
	log.EnableColorTemplate()
	stop := make(chan bool)
	done := make(chan bool)

	numLines := 20
	for n := 0; n < numLines; n++ {
		go writeStuff(n, stop, done)
	}
	signalChan := make(chan os.Signal, 1)
	cleanupDone := make(chan bool)
	signal.Notify(signalChan, os.Interrupt)
	go func() {
		for _ = range signalChan {
			log.Println("Received SIGINT. Shutting down...")
			for n := 0; n < numLines; n++ {
				stop <- true
			}
			for n := 0; n < numLines; n++ {
				<-done
			}
			log.Println("Exiting...")
			cleanupDone <- true
		}
	}()
	<-cleanupDone
}
<commit_msg>Update references to ansi-log to use new "alog" name<commit_after>package main

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

	"github.com/tillberg/ansi-log"
)

var colors []string = []string{"red", "yellow", "magenta", "cyan", "blue"}

func writeStuff(num int, stop chan bool, done chan bool) {
	i := 0
	color := colors[num/4]
	out := alog.New(os.Stderr, fmt.Sprintf("@(dim)[@(green:writer-%d)] ", num), alog.Lelapsed)
	format := fmt.Sprintf("@(dim:My number is) @(%s:%%d)", color)
	for {
		select {
		case <-stop:
			out.Close()
			done <- true
			return
		default:
			out.Replacef(format, i)
			i += num
			if i > 10000 {
				i = 0
				out.Println()
			}
		}
		time.Sleep(time.Microsecond)
	}
}

func main() {
	alog.EnableMultilineMode()
	alog.EnableColorTemplate()
	stop := make(chan bool)
	done := make(chan bool)

	numLines := 20
	for n := 0; n < numLines; n++ {
		go writeStuff(n, stop, done)
	}
	signalChan := make(chan os.Signal, 1)
	cleanupDone := make(chan bool)
	signal.Notify(signalChan, os.Interrupt)
	go func() {
		for _ = range signalChan {
			alog.Println("Received SIGINT. Shutting down...")
			for n := 0; n < numLines; n++ {
				stop <- true
			}
			for n := 0; n < numLines; n++ {
				<-done
			}
			alog.Println("Exiting...")
			cleanupDone <- true
		}
	}()
	<-cleanupDone
}
<|endoftext|>