<commit_before>package main

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

	"github.com/codegangsta/cli"
	"github.com/opencontainers/runtime-spec/specs-go"
)

var execCommand = cli.Command{
	Name:  "exec",
	Usage: "exec a new program in runv container",
	ArgsUsage: `<container-id> <container command>

Where "<container-id>" is the name for the instance of the container and
"<container command>" is the command to be executed in the container.

For example, if the container is configured to run the linux ps command the
following will output a list of processes running in the container:

       # runv exec <container-id> ps`,
	Action: func(context *cli.Context) {
		root := context.GlobalString("root")
		container := context.Args().First()
		config, err := loadProcessConfig(context.Args().Get(1))
		if err != nil {
			fmt.Printf("load process config failed %v\n", err)
			os.Exit(-1)
		}
		if container == "" {
			fmt.Printf("Please specify container ID")
			os.Exit(-1)
		}
		if os.Geteuid() != 0 {
			fmt.Printf("runv should be run as root\n")
			os.Exit(-1)
		}
		conn, err := runvRequest(root, container, RUNV_EXECCMD, config.Args)
		if err != nil {
			fmt.Printf("exec failed: %v", err)
		}
		code, err := containerTtySplice(root, container, conn, false)
		os.Exit(code)
	},
}

// loadProcessConfig loads the process configuration from the provided path.
func loadProcessConfig(path string) (*specs.Process, error) {
	f, err := os.Open(path)
	if err != nil {
		if os.IsNotExist(err) {
			return nil, fmt.Errorf("JSON configuration file for %s not found", path)
		}
		return nil, err
	}
	defer f.Close()
	var s *specs.Process
	if err := json.NewDecoder(f).Decode(&s); err != nil {
		return nil, err
	}
	return s, nil
}
<commit_msg>update the command of runv exec<commit_after>package main

import (
	"encoding/json"
	"fmt"
	"os"
	"path/filepath"
	"strconv"
	"strings"

	"github.com/codegangsta/cli"
	"github.com/opencontainers/runtime-spec/specs-go"
)

var execCommand = cli.Command{
	Name:  "exec",
	Usage: "exec a new program in runv container",
	ArgsUsage: `<container-id> <container command>

Where "<container-id>" is the name for the instance of the container and
"<container command>" is the command to be executed in the container.

For example, if the container is configured to run the linux ps command the
following will output a list of processes running in the container:

       # runv exec <container-id> ps`,
	Flags: []cli.Flag{
		cli.StringFlag{
			Name:  "console",
			Usage: "[reject on runv] specify the pty slave path for use with the container",
		},
		cli.StringFlag{
			Name:  "cwd",
			Usage: "[TODO] current working directory in the container",
		},
		cli.StringSliceFlag{
			Name:  "env, e",
			Usage: "[TODO] set environment variables",
		},
		cli.BoolFlag{
			Name:  "tty, t",
			Usage: "[TODO] allocate a pseudo-TTY",
		},
		cli.StringFlag{
			Name:  "user, u",
			Usage: "[TODO] UID (format: <uid>[:<gid>])",
		},
		cli.StringFlag{
			Name:  "process, p",
			Usage: "path to the process.json",
		},
		cli.BoolFlag{
			Name:  "detach,d",
			Usage: "[TODO] detach from the container's process",
		},
		cli.StringFlag{
			Name:  "pid-file",
			Usage: "[TODO] specify the file to write the process id to",
		},
		cli.StringFlag{
			Name:  "process-label",
			Usage: "[ignore on runv] set the asm process label for the process commonly used with selinux",
		},
		cli.StringFlag{
			Name:  "apparmor",
			Usage: "[ignore on runv] set the apparmor profile for the process",
		},
		cli.BoolFlag{
			Name:  "no-new-privs",
			Usage: "[ignore on runv] set the no new privileges value for the process",
		},
		cli.StringSliceFlag{
			Name:  "cap, c",
			Usage: "[ignore on runv] add a capability to the bounding set for the process",
		},
		cli.BoolFlag{
			Name:  "no-subreaper",
			Usage: "[ignore on runv] disable the use of the subreaper used to reap reparented processes",
		},
	},
	Action: func(context *cli.Context) {
		root := context.GlobalString("root")
		container := context.Args().First()

		if context.String("console") != "" {
			fmt.Printf("--console is unsupported on runv\n")
			os.Exit(-1)
		}
		if container == "" {
			fmt.Printf("Please specify container ID\n")
			os.Exit(-1)
		}
		if os.Geteuid() != 0 {
			fmt.Printf("runv should be run as root\n")
			os.Exit(-1)
		}

		// get bundle path from state
		path := filepath.Join(root, container, stateJson)
		f, err := os.Open(path)
		if err != nil {
			fmt.Printf("open JSON configuration file failed: %v\n", err)
			os.Exit(-1)
		}
		defer f.Close()
		var s *specs.State
		if err := json.NewDecoder(f).Decode(&s); err != nil {
			fmt.Printf("parse JSON configuration file failed: %v\n", err)
			os.Exit(-1)
		}
		bundle := s.BundlePath

		// get process
		config, err := getProcess(context, bundle)
		if err != nil {
			fmt.Printf("get process config failed %v\n", err)
			os.Exit(-1)
		}
		conn, err := runvRequest(root, container, RUNV_EXECCMD, config.Args)
		if err != nil {
			fmt.Printf("exec failed: %v", err)
		}
		code, err := containerTtySplice(root, container, conn, false)
		os.Exit(code)
	},
}

// loadProcessConfig loads the process configuration from the provided path.
func loadProcessConfig(path string) (*specs.Process, error) {
	f, err := os.Open(path)
	if err != nil {
		if os.IsNotExist(err) {
			return nil, fmt.Errorf("JSON configuration file for %s not found", path)
		}
		return nil, err
	}
	defer f.Close()
	var s *specs.Process
	if err := json.NewDecoder(f).Decode(&s); err != nil {
		return nil, err
	}
	return s, nil
}

func getProcess(context *cli.Context, bundle string) (*specs.Process, error) {
	if path := context.String("process"); path != "" {
		return loadProcessConfig(path)
	}
	// process via cli flags
	spec, err := loadSpec(filepath.Join(bundle, specConfig))
	if err != nil {
		return nil, err
	}
	p := spec.Process
	p.Args = context.Args()[1:]
	// override the cwd, if passed
	if context.String("cwd") != "" {
		p.Cwd = context.String("cwd")
	}
	// append the passed env variables
	for _, e := range context.StringSlice("env") {
		p.Env = append(p.Env, e)
	}
	// set the tty
	if context.IsSet("tty") {
		p.Terminal = context.Bool("tty")
	}
	// override the user, if passed
	if context.String("user") != "" {
		u := strings.SplitN(context.String("user"), ":", 2)
		if len(u) > 1 {
			gid, err := strconv.Atoi(u[1])
			if err != nil {
				return nil, fmt.Errorf("parsing %s as int for gid failed: %v", u[1], err)
			}
			p.User.GID = uint32(gid)
		}
		uid, err := strconv.Atoi(u[0])
		if err != nil {
			return nil, fmt.Errorf("parsing %s as int for uid failed: %v", u[0], err)
		}
		p.User.UID = uint32(uid)
	}
	return &p, nil
}
<|endoftext|>


<commit_before>/*
This file contains functionality around storing configuration settings
inside Git's metadata storage for the repository.
*/

package git

import (
	"fmt"
	"log"
	"os"
	"regexp"
	"strconv"
	"strings"

	"github.com/Originate/git-town/src/util"
)

// AddToPerennialBranches adds the given branch as a perennial branch
func AddToPerennialBranches(branchName string) {
	SetPerennialBranches(append(GetPerennialBranches(), branchName))
}

// CompileAncestorBranches calculates and returns the list of ancestor branches
// of the given branch based off the "git-town-branch.XXX.parent" configuration values.
func CompileAncestorBranches(branchName string) (result []string) {
	current := branchName
	for {
		if IsMainBranch(current) || IsPerennialBranch(current) {
			return
		}
		parent := GetParentBranch(current)
		if parent == "" {
			return
		}
		result = append([]string{parent}, result...)
		current = parent
	}
}

// DeleteAllAncestorBranches removes all Git Town ancestor entries
// for all branches from the configuration.
func DeleteAllAncestorBranches() {
	for _, key := range getConfigurationKeysMatching("^git-town-branch\\..*\\.ancestors$") {
		removeConfigurationValue(key)
	}
}

// DeleteParentBranch removes the parent branch entry for the given branch
// from the Git configuration.
func DeleteParentBranch(branchName string) {
	removeConfigurationValue("git-town-branch." + branchName + ".parent")
}

// EnsureIsFeatureBranch asserts that the given branch is a feature branch.
func EnsureIsFeatureBranch(branchName, errorSuffix string) {
	util.Ensure(IsFeatureBranch(branchName), fmt.Sprintf("The branch '%s' is not a feature branch. %s", branchName, errorSuffix))
}

// GetAncestorBranches returns the names of all parent branches for the given branch,
// beginning but not including the parennial branch from which this hierarchy was cut.
// This information is read from the cache in the Git config,
// so might be out of date when the branch hierarchy has been modified.
func GetAncestorBranches(branchName string) []string {
	value := getConfigurationValue("git-town-branch." + branchName + ".ancestors")
	if value == "" {
		return []string{}
	}
	return strings.Split(value, " ")
}

// GetChildBranches returns the names of all branches for which the given branch
// is a parent.
func GetChildBranches(branchName string) (result []string) {
	for _, key := range getConfigurationKeysMatching("^git-town-branch\\..*\\.parent$") {
		parent := getConfigurationValue(key)
		if parent == branchName {
			child := strings.TrimSuffix(strings.TrimPrefix(key, "git-town-branch."), ".parent")
			result = append(result, child)
		}
	}
	return
}

// GetGlobalConfigurationValue returns the global git configuration value for the given key
func GetGlobalConfigurationValue(key string) (result string) {
	if hasConfigurationValue("global", key) {
		result = util.GetCommandOutput("git", "config", "--global", key)
	}
	return
}

// GetMainBranch returns the name of the main branch.
func GetMainBranch() string {
	return getConfigurationValue("git-town.main-branch-name")
}

// GetParentBranch returns the name of the parent branch of the given branch.
func GetParentBranch(branchName string) string {
	return getConfigurationValue("git-town-branch." + branchName + ".parent")
}

// GetPerennialBranches returns all branches that are marked as perennial.
func GetPerennialBranches() []string {
	result := getConfigurationValue("git-town.perennial-branch-names")
	if result == "" {
		return []string{}
	}
	return strings.Split(result, " ")
}

// GetPullBranchStrategy returns the currently configured pull branch strategy.
func GetPullBranchStrategy() string {
	return getConfigurationValueWithDefault("git-town.pull-branch-strategy", "rebase")
}

// GetRemoteOriginURL returns the URL for the "origin" remote.
// In tests this value can be stubbed.
func GetRemoteOriginURL() string {
	if os.Getenv("GIT_TOWN_ENV") == "test" {
		mockRemoteURL := getConfigurationValue("git-town.testing.remote-url")
		if mockRemoteURL != "" {
			return mockRemoteURL
		}
	}
	return util.GetCommandOutput("git", "remote", "get-url", "origin")
}

// GetRemoteUpstreamURL returns the URL of the "upstream" remote.
func GetRemoteUpstreamURL() string {
	return util.GetCommandOutput("git", "remote", "get-url", "upstream")
}

// GetURLHostname returns the hostname contained within the given Git URL.
func GetURLHostname(url string) string {
	hostnameRegex, err := regexp.Compile("(^[^:]*://([^@]*@)?|git@)([^/:]+).*")
	if err != nil {
		log.Fatal("Error compiling hostname regular expression: ", err)
	}
	matches := hostnameRegex.FindStringSubmatch(url)
	if matches == nil {
		return ""
	}
	return matches[3]
}

// GetURLRepositoryName returns the repository name contains within the given Git URL.
func GetURLRepositoryName(url string) string {
	hostname := GetURLHostname(url)
	repositoryNameRegex, err := regexp.Compile(".*" + hostname + "[/:](.+)")
	if err != nil {
		log.Fatal("Error compiling repository name regular expression: ", err)
	}
	matches := repositoryNameRegex.FindStringSubmatch(url)
	if matches == nil {
		return ""
	}
	return strings.TrimSuffix(matches[1], ".git")
}

// HasGlobalConfigurationValue returns whether there is a global git configuration for the given key
func HasGlobalConfigurationValue(key string) bool {
	return util.DoesCommandOuputContainLine([]string{"git", "config", "-l", "--global", "--name"}, key)
}

// IsAncestorBranch returns whether the given branch is an ancestor of the other given branch.
func IsAncestorBranch(branchName, ancestorBranchName string) bool {
	ancestorBranches := CompileAncestorBranches(branchName)
	return util.DoesStringArrayContain(ancestorBranches, ancestorBranchName)
}

// HasCompiledAncestorBranches returns whether the Git Town configuration
// contains a cached ancestor list for the branch with the given name.
func HasCompiledAncestorBranches(branchName string) bool {
	return len(GetAncestorBranches(branchName)) > 0
}

// HasRemote returns whether the current repository contains a Git remote
// with the given name.
func HasRemote(name string) bool {
	return util.DoesCommandOuputContainLine([]string{"git", "remote"}, name)
}

// IsFeatureBranch returns whether the branch with the given name is
// a feature branch.
func IsFeatureBranch(branchName string) bool {
	return !IsMainBranch(branchName) && !IsPerennialBranch(branchName)
}

// IsMainBranch returns whether the branch with the given name
// is the main branch of the repository.
func IsMainBranch(branchName string) bool {
	return branchName == GetMainBranch()
}

// IsPerennialBranch returns whether the branch with the given name is
// a perennial branch.
func IsPerennialBranch(branchName string) bool {
	perennialBranches := GetPerennialBranches()
	return util.DoesStringArrayContain(perennialBranches, branchName)
}

// RemoveAllConfiguration removes all Git Town configuration
func RemoveAllConfiguration() {
	util.GetCommandOutput("git", "config", "--remove-section", "git-town")
}

// RemoveFromPerennialBranches removes the given branch as a perennial branch
func RemoveFromPerennialBranches(branchName string) {
	SetPerennialBranches(util.RemoveStringFromSlice(GetPerennialBranches(), branchName))
}

// SetAncestorBranches stores the given list of branches as ancestors
// for the given branch in the Git Town configuration.
func SetAncestorBranches(branchName string, ancestorBranches []string) {
	setConfigurationValue("git-town-branch."+branchName+".ancestors", strings.Join(ancestorBranches, " "))
}

// SetMainBranch marks the given branch as the main branch
// in the Git Town configuration.
func SetMainBranch(branchName string) {
	setConfigurationValue("git-town.main-branch-name", branchName)
}

// SetParentBranch marks the given branch as the direct parent of the other given branch
// in the Git Town configuration.
func SetParentBranch(branchName, parentBranchName string) {
	setConfigurationValue("git-town-branch."+branchName+".parent", parentBranchName)
}

// SetPerennialBranches marks the given branches as perennial branches
func SetPerennialBranches(branchNames []string) {
	setConfigurationValue("git-town.perennial-branch-names", strings.Join(branchNames, " "))
}

// SetPullBranchStrategy updates the configured pull branch strategy.
func SetPullBranchStrategy(strategy string) {
	setConfigurationValue("git-town.pull-branch-strategy", strategy)
}

// ShouldHackPush returns whether the current repository is configured to push
// freshly created branches up to the origin remote.
func ShouldHackPush() bool {
	return getConfigurationValueWithDefault("git-town.hack-push-flag", "false") == "true"
}

// UpdateShouldHackPush updates whether the current repository is configured to push
// freshly created branches up to the origin remote.
func UpdateShouldHackPush(value bool) {
	setConfigurationValue("git-town.hack-push-flag", strconv.FormatBool(value))
}

// Helpers

func getConfigurationValue(key string) (result string) {
	if hasConfigurationValue("local", key) {
		result = util.GetCommandOutput("git", "config", key)
	}
	return
}

func getConfigurationValueWithDefault(key, defaultValue string) string {
	value := getConfigurationValue(key)
	if value == "" {
		return defaultValue
	}
	return value
}

func getConfigurationKeysMatching(toMatch string) (result []string) {
	configRegexp, err := regexp.Compile(toMatch)
	if err != nil {
		log.Fatalf("Error compiling configuration regular expression (%s): %v", toMatch, err)
	}
	lines := util.GetCommandOutput("git", "config", "-l", "--local", "--name")
	for _, line := range strings.Split(lines, "\n") {
		if configRegexp.MatchString(line) {
			result = append(result, line)
		}
	}
	return
}

func hasConfigurationValue(location, key string) bool {
	return util.DoesCommandOuputContainLine([]string{"git", "config", "-l", "--" + location, "--name"}, key)
}

func setConfigurationValue(key, value string) {
	util.GetCommandOutput("git", "config", key, value)
}

func removeConfigurationValue(key string) {
	util.GetCommandOutput("git", "config", "--unset", key)
}
<commit_msg>Fix spelling<commit_after>/*
This file contains functionality around storing configuration settings
inside Git's metadata storage for the repository.
*/

package git

import (
	"fmt"
	"log"
	"os"
	"regexp"
	"strconv"
	"strings"

	"github.com/Originate/git-town/src/util"
)

// AddToPerennialBranches adds the given branch as a perennial branch
func AddToPerennialBranches(branchName string) {
	SetPerennialBranches(append(GetPerennialBranches(), branchName))
}

// CompileAncestorBranches calculates and returns the list of ancestor branches
// of the given branch based off the "git-town-branch.XXX.parent" configuration values.
func CompileAncestorBranches(branchName string) (result []string) {
	current := branchName
	for {
		if IsMainBranch(current) || IsPerennialBranch(current) {
			return
		}
		parent := GetParentBranch(current)
		if parent == "" {
			return
		}
		result = append([]string{parent}, result...)
		current = parent
	}
}

// DeleteAllAncestorBranches removes all Git Town ancestor entries
// for all branches from the configuration.
func DeleteAllAncestorBranches() {
	for _, key := range getConfigurationKeysMatching("^git-town-branch\\..*\\.ancestors$") {
		removeConfigurationValue(key)
	}
}

// DeleteParentBranch removes the parent branch entry for the given branch
// from the Git configuration.
func DeleteParentBranch(branchName string) {
	removeConfigurationValue("git-town-branch." + branchName + ".parent")
}

// EnsureIsFeatureBranch asserts that the given branch is a feature branch.
func EnsureIsFeatureBranch(branchName, errorSuffix string) {
	util.Ensure(IsFeatureBranch(branchName), fmt.Sprintf("The branch '%s' is not a feature branch. %s", branchName, errorSuffix))
}

// GetAncestorBranches returns the names of all parent branches for the given branch,
// beginning but not including the parennial branch from which this hierarchy was cut.
// This information is read from the cache in the Git config,
// so might be out of date when the branch hierarchy has been modified.
func GetAncestorBranches(branchName string) []string {
	value := getConfigurationValue("git-town-branch." + branchName + ".ancestors")
	if value == "" {
		return []string{}
	}
	return strings.Split(value, " ")
}

// GetChildBranches returns the names of all branches for which the given branch
// is a parent.
func GetChildBranches(branchName string) (result []string) {
	for _, key := range getConfigurationKeysMatching("^git-town-branch\\..*\\.parent$") {
		parent := getConfigurationValue(key)
		if parent == branchName {
			child := strings.TrimSuffix(strings.TrimPrefix(key, "git-town-branch."), ".parent")
			result = append(result, child)
		}
	}
	return
}

// GetGlobalConfigurationValue returns the global Git configuration value for the given key
func GetGlobalConfigurationValue(key string) (result string) {
	if hasConfigurationValue("global", key) {
		result = util.GetCommandOutput("git", "config", "--global", key)
	}
	return
}

// GetMainBranch returns the name of the main branch.
func GetMainBranch() string {
	return getConfigurationValue("git-town.main-branch-name")
}

// GetParentBranch returns the name of the parent branch of the given branch.
func GetParentBranch(branchName string) string {
	return getConfigurationValue("git-town-branch." + branchName + ".parent")
}

// GetPerennialBranches returns all branches that are marked as perennial.
func GetPerennialBranches() []string {
	result := getConfigurationValue("git-town.perennial-branch-names")
	if result == "" {
		return []string{}
	}
	return strings.Split(result, " ")
}

// GetPullBranchStrategy returns the currently configured pull branch strategy.
func GetPullBranchStrategy() string {
	return getConfigurationValueWithDefault("git-town.pull-branch-strategy", "rebase")
}

// GetRemoteOriginURL returns the URL for the "origin" remote.
// In tests this value can be stubbed.
func GetRemoteOriginURL() string {
	if os.Getenv("GIT_TOWN_ENV") == "test" {
		mockRemoteURL := getConfigurationValue("git-town.testing.remote-url")
		if mockRemoteURL != "" {
			return mockRemoteURL
		}
	}
	return util.GetCommandOutput("git", "remote", "get-url", "origin")
}

// GetRemoteUpstreamURL returns the URL of the "upstream" remote.
func GetRemoteUpstreamURL() string {
	return util.GetCommandOutput("git", "remote", "get-url", "upstream")
}

// GetURLHostname returns the hostname contained within the given Git URL.
func GetURLHostname(url string) string {
	hostnameRegex, err := regexp.Compile("(^[^:]*://([^@]*@)?|git@)([^/:]+).*")
	if err != nil {
		log.Fatal("Error compiling hostname regular expression: ", err)
	}
	matches := hostnameRegex.FindStringSubmatch(url)
	if matches == nil {
		return ""
	}
	return matches[3]
}

// GetURLRepositoryName returns the repository name contains within the given Git URL.
func GetURLRepositoryName(url string) string {
	hostname := GetURLHostname(url)
	repositoryNameRegex, err := regexp.Compile(".*" + hostname + "[/:](.+)")
	if err != nil {
		log.Fatal("Error compiling repository name regular expression: ", err)
	}
	matches := repositoryNameRegex.FindStringSubmatch(url)
	if matches == nil {
		return ""
	}
	return strings.TrimSuffix(matches[1], ".git")
}

// HasGlobalConfigurationValue returns whether there is a global git configuration for the given key
func HasGlobalConfigurationValue(key string) bool {
	return util.DoesCommandOuputContainLine([]string{"git", "config", "-l", "--global", "--name"}, key)
}

// IsAncestorBranch returns whether the given branch is an ancestor of the other given branch.
func IsAncestorBranch(branchName, ancestorBranchName string) bool {
	ancestorBranches := CompileAncestorBranches(branchName)
	return util.DoesStringArrayContain(ancestorBranches, ancestorBranchName)
}

// HasCompiledAncestorBranches returns whether the Git Town configuration
// contains a cached ancestor list for the branch with the given name.
func HasCompiledAncestorBranches(branchName string) bool {
	return len(GetAncestorBranches(branchName)) > 0
}

// HasRemote returns whether the current repository contains a Git remote
// with the given name.
func HasRemote(name string) bool {
	return util.DoesCommandOuputContainLine([]string{"git", "remote"}, name)
}

// IsFeatureBranch returns whether the branch with the given name is
// a feature branch.
func IsFeatureBranch(branchName string) bool {
	return !IsMainBranch(branchName) && !IsPerennialBranch(branchName)
}

// IsMainBranch returns whether the branch with the given name
// is the main branch of the repository.
func IsMainBranch(branchName string) bool {
	return branchName == GetMainBranch()
}

// IsPerennialBranch returns whether the branch with the given name is
// a perennial branch.
func IsPerennialBranch(branchName string) bool {
	perennialBranches := GetPerennialBranches()
	return util.DoesStringArrayContain(perennialBranches, branchName)
}

// RemoveAllConfiguration removes all Git Town configuration
func RemoveAllConfiguration() {
	util.GetCommandOutput("git", "config", "--remove-section", "git-town")
}

// RemoveFromPerennialBranches removes the given branch as a perennial branch
func RemoveFromPerennialBranches(branchName string) {
	SetPerennialBranches(util.RemoveStringFromSlice(GetPerennialBranches(), branchName))
}

// SetAncestorBranches stores the given list of branches as ancestors
// for the given branch in the Git Town configuration.
func SetAncestorBranches(branchName string, ancestorBranches []string) {
	setConfigurationValue("git-town-branch."+branchName+".ancestors", strings.Join(ancestorBranches, " "))
}

// SetMainBranch marks the given branch as the main branch
// in the Git Town configuration.
func SetMainBranch(branchName string) {
	setConfigurationValue("git-town.main-branch-name", branchName)
}

// SetParentBranch marks the given branch as the direct parent of the other given branch
// in the Git Town configuration.
func SetParentBranch(branchName, parentBranchName string) {
	setConfigurationValue("git-town-branch."+branchName+".parent", parentBranchName)
}

// SetPerennialBranches marks the given branches as perennial branches
func SetPerennialBranches(branchNames []string) {
	setConfigurationValue("git-town.perennial-branch-names", strings.Join(branchNames, " "))
}

// SetPullBranchStrategy updates the configured pull branch strategy.
func SetPullBranchStrategy(strategy string) {
	setConfigurationValue("git-town.pull-branch-strategy", strategy)
}

// ShouldHackPush returns whether the current repository is configured to push
// freshly created branches up to the origin remote.
func ShouldHackPush() bool {
	return getConfigurationValueWithDefault("git-town.hack-push-flag", "false") == "true"
}

// UpdateShouldHackPush updates whether the current repository is configured to push
// freshly created branches up to the origin remote.
func UpdateShouldHackPush(value bool) {
	setConfigurationValue("git-town.hack-push-flag", strconv.FormatBool(value))
}

// Helpers

func getConfigurationValue(key string) (result string) {
	if hasConfigurationValue("local", key) {
		result = util.GetCommandOutput("git", "config", key)
	}
	return
}

func getConfigurationValueWithDefault(key, defaultValue string) string {
	value := getConfigurationValue(key)
	if value == "" {
		return defaultValue
	}
	return value
}

func getConfigurationKeysMatching(toMatch string) (result []string) {
	configRegexp, err := regexp.Compile(toMatch)
	if err != nil {
		log.Fatalf("Error compiling configuration regular expression (%s): %v", toMatch, err)
	}
	lines := util.GetCommandOutput("git", "config", "-l", "--local", "--name")
	for _, line := range strings.Split(lines, "\n") {
		if configRegexp.MatchString(line) {
			result = append(result, line)
		}
	}
	return
}

func hasConfigurationValue(location, key string) bool {
	return util.DoesCommandOuputContainLine([]string{"git", "config", "-l", "--" + location, "--name"}, key)
}

func setConfigurationValue(key, value string) {
	util.GetCommandOutput("git", "config", key, value)
}

func removeConfigurationValue(key string) {
	util.GetCommandOutput("git", "config", "--unset", key)
}
<|endoftext|>


<commit_before>package main

import (
	"bufio"
	"bytes"
	"fmt"
	"net/http"
	"os"

	"github.com/mozillazg/request"
)

func get(a *request.Args) {
	resp, err := request.Get("http://httpbin.org/get", a)
	defer resp.Body.Close()
	if err == nil {
		fmt.Println(resp.Ok())
		fmt.Println(resp.Reason())
	}
	d, _ := resp.Json()
	fmt.Println(d.Get("url"))
	fmt.Println(d.Get("args"))
}

func head(a *request.Args) {
	resp, err := request.Head("http://httpbin.org/get", a)
	if err == nil {
		fmt.Println(resp.Ok())
		fmt.Println(resp.Reason())
	}
	defer resp.Body.Close()
}

func json(a *request.Args) {
	resp, err := request.Get("http://httpbin.org/get", a)
	if err != nil {
		return
	}

	d, err := resp.Json()
	if err != nil {
		return
	}
	fmt.Println(d.Get("headers").Get("User-Agent"))
	defer resp.Body.Close()
}

func gzip(a *request.Args) {
	resp, err := request.Get("http://httpbin.org/gzip", a)
	if err != nil {
		return
	}

	d, err := resp.Json()
	if err != nil {
		return
	}
	fmt.Println(d.Get("headers").Get("Accept-Encoding"))
	fmt.Println(resp.Header.Get("Content-Encoding"))
	s, err := resp.Text()
	fmt.Println(s)
	defer resp.Body.Close()
}

func post(a *request.Args) {
	resp, err := request.Post("http://httpbin.org/post", a)
	defer resp.Body.Close()
	if err != nil {
		fmt.Print(err)
		return
	}

	fmt.Println(resp.Ok())
	d, err := resp.Json()
	if err != nil {
		fmt.Print(err)
	}
	fmt.Println(d.Get("headers").Get("Content-Type"))
	fmt.Println(d.Get("form"))
	fmt.Println(d.Get("url"))
	fmt.Println(d.Get("args"))
}

func customHeaders(a *request.Args) {
	a.Headers = map[string]string{
		"Accept-Encoding": "gzip,deflate,sdch",
		"Accept":          "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
	}
	resp, err := request.Get("http://httpbin.org/get", a)
	defer resp.Body.Close()
	if err == nil {
		fmt.Println(resp.Ok())
	}
	d, err := resp.Json()
	if err != nil {
		fmt.Print(err)
	}
	fmt.Println(d.Get("headers").Get("User-Agent"))
	fmt.Println(d.Get("headers").Get("Accept-Encoding"))
	fmt.Println(d.Get("headers").Get("Accept"))
}

func put(a *request.Args) {
	resp, _ := request.Put("http://httpbin.org/put", a)
	defer resp.Body.Close()

	fmt.Println(resp.Ok())
	d, _ := resp.Json()
	fmt.Println(d.Get("headers").Get("Content-Type"))
	fmt.Println(d.Get("form"))
}

func patch(a *request.Args) {
	resp, _ := request.Patch("http://httpbin.org/patch", a)
	defer resp.Body.Close()

	fmt.Println(resp.Ok())
	d, _ := resp.Json()
	fmt.Println(d.Get("headers").Get("Content-Type"))
	fmt.Println(d.Get("form"))
}

func deleteF(a *request.Args) {
	resp, _ := request.Delete("http://httpbin.org/delete", a)
	defer resp.Body.Close()

	fmt.Println(resp.Ok())
	d, _ := resp.Json()
	fmt.Println(d.Get("headers").Get("Content-Type"))
	fmt.Println(d.Get("form"))
}

func options(a *request.Args) {
	resp, _ := request.Options("http://httpbin.org/get", a)
	defer resp.Body.Close()

	fmt.Println(resp.Ok())
	fmt.Println("Allow:", resp.Header.Get("Allow"))
}

func getParams(a *request.Args) {
	resp, err := request.Get("http://httpbin.org/get?foobar=123", a)
	defer resp.Body.Close()
	if err == nil {
		fmt.Println(resp.Ok())
		fmt.Println(resp.Reason())
	}
	d, _ := resp.Json()
	fmt.Println(d.Get("url"))
	fmt.Println(d.Get("args"))
}

func cookies(a *request.Args) {
	resp, _ := request.Get("http://httpbin.org/cookies", a)
	defer resp.Body.Close()
	d, _ := resp.Json()
	fmt.Println(d.Get("cookies"))
	fmt.Println(resp.Cookies())
	url := resp.Request.URL
	fmt.Println(a.Client.Jar.Cookies(url))
}

func file(a *request.Args) {
	resp, _ := request.Post("http://httpbin.org/post", a)
	defer resp.Body.Close()
	fmt.Println(resp.Ok())
	d, _ := resp.Json()
	fmt.Println(d.Get("url"))
	fmt.Println(d.Get("files"))
	fmt.Println(d.Get("form"))
}

func files(a *request.Args) {
	resp, _ := request.Post("http://httpbin.org/post", a)
	defer resp.Body.Close()
	fmt.Println(resp.Ok())
	d, _ := resp.Json()
	fmt.Println(d.Get("url"))
	fmt.Println(d.Get("files"))
}

func jsonPost(a *request.Args) {
	resp, _ := request.Post("http://httpbin.org/post", a)
	defer resp.Body.Close()
	fmt.Println(resp.Ok())
	d, _ := resp.Json()
	fmt.Println(d.Get("json"))
}

func main() {
	c := &http.Client{}
	a := request.NewArgs(c)

	fmt.Println("=====GET: ")
	get(a)
	fmt.Println("=====HEAD: ")
	head(a)
	fmt.Println("=====JSON: ")
	json(a)
	fmt.Println("=====GZIP: ")
	gzip(a)

	a.Data = map[string]string{
		"key": "value",
		"a":   "123",
	}
	fmt.Println("=====POST: ")
	post(a)

	fmt.Println("=====Custom Headers: ")
	customHeaders(a)

	a = request.NewArgs(c)
	a.Data = map[string]string{
		"key": "value",
		"a":   "123",
	}
	fmt.Println("=====PUT: ")
	put(a)
	fmt.Println("=====PATCH: ")
	patch(a)
	fmt.Println("=====DELTE: ")
	deleteF(a)
	fmt.Println("=====OPTIONS: ")
	options(a)

	a = request.NewArgs(c)
	a.Params = map[string]string{
		"a":   "abc",
		"key": "value",
	}
	fmt.Println("=====Params: ")
	get(a)
	getParams(a)
	post(a)

	fmt.Println("=====Cookies: ")
	a = request.NewArgs(c)
	cookies(a)
	a.Cookies = map[string]string{
		"name": "value",
		"foo":  "bar",
	}
	cookies(a)

	fmt.Println("=====File: ")
	a = request.NewArgs(c)
	b := &bytes.Buffer{}
	w := bufio.NewWriter(b)
	f := []byte{
		'a',
		'b',
		'c',
		'd',
	}
	_, _ = w.Write(f)
	w.Flush()
	f2, _ := os.Open("test.txt")

	a.Data = map[string]string{
		"key": "value",
		"a":   "123",
	}
	a.Files = []request.FileField{
		request.FileField{"abc", "abc.txt", b},
		request.FileField{"test", "test.txt", f2},
	}
	file(a)
	f2, _ = os.Open("test.txt")
	a.Files = []request.FileField{
		request.FileField{"abc", "abc.txt", f2},
	}
	file(a)

	fmt.Println("=====JSON POST: ")
	type j struct {
		A string            `json:"a"`
		B map[string]string `json:"b"`
		C []string          `json:"c"`
		D []int             `json:"d"`
		E int               `json:"e"`
	}
	a = request.NewArgs(c)
	d := j{
		A: "hello",
		B: map[string]string{
			"a": "A",
			"b": "B",
			"c": "C",
		},
		C: []string{"lala", "aaaa"},
		D: []int{1, 2, 3},
		E: 5,
	}
	fmt.Println(d)
	a.Json = d
	jsonPost(a)
	a.Json = []int{1, 2, 3, 4}
	jsonPost(a)
}
<commit_msg>rm example<commit_after><|endoftext|>


<commit_before>package buspirate

import "github.com/davecheney/buspirate"
import "time"

// ExampleBusPirate pulses a LED connected to the AUX pin.
func ExampleBusPirate_SetPWM() {
	bp, err := buspirate.Open("/dev/ttyACM0")
	if err != nil {
		panic(err)
	}
	duty := 0.1
	delta := 0.1
	for {
		bp.SetPWM(duty)
		time.Sleep(50 * time.Millisecond)
		duty += delta
		if duty > 1.0 {
			duty = 1.0
			delta = -delta
		}
		if duty < 0 {
			duty = 0.0
			delta = -delta
		}
	}
}
<commit_msg>fix example text<commit_after>package buspirate

import "github.com/davecheney/buspirate"
import "time"

// Pulse a LED connected to the AUX pin.
func ExampleBusPirate_SetPWM() {
	bp, err := buspirate.Open("/dev/ttyACM0")
	if err != nil {
		panic(err)
	}
	duty := 0.1
	delta := 0.1
	for {
		bp.SetPWM(duty)
		time.Sleep(50 * time.Millisecond)
		duty += delta
		if duty > 1.0 {
			duty = 1.0
			delta = -delta
		}
		if duty < 0 {
			duty = 0.0
			delta = -delta
		}
	}
}
<|endoftext|>


<commit_before>package forge_test

import (
	"bytes"
	"fmt"

	"github.com/brettlangdon/forge"
)

func Example() {
	// Parse a `SectionValue` from `example.cfg`
	settings, err := forge.ParseFile("example.cfg")
	if err != nil {
		panic(err)
	}

	// Get a single value
	if settings.Exists("global") {
		// Get `global` casted as `StringValue`
		value := settings.GetString("global")
		fmt.Printf("global = \"%s\"\r\n", value.GetValue())
	}

	// Get a nested value
	value, err := settings.Resolve("primary.included_setting")
	fmt.Printf("primary.included_setting = \"%s\"\r\n", value.GetValue())

	// You can also traverse down the sections manually
	primary, err := settings.GetSection("primary")
	value, err := primary.GetString("included_setting")
	fmt.Printf("primary.included_setting = \"%s\"\r\n", value)

	// Convert settings to a map
	settingsMap, err := settings.ToMap()
	fmt.Printf("global = \"%s\"\r\n", settingsMap["global"])

	// Convert settings to JSON
	jsonBytes, err := settings.ToJSON()
	fmt.Printf("\r\n\r\n%s\r\n", string(jsonBytes))
}

func ExampleParseFile() {
	// Parse a `SectionValue` from `example.cfg`
	settings, err := forge.ParseFile("example.cfg")
	if err != nil {
		panic(err)
	}
}

func ExampleParseString() {
	// Parse a `SectionValue` from string containing the config
	data := "amount = 500;"
	settings, err := forge.ParseString(data)
	if err != nil {
		panic(err)
	}

	fmt.Println(settings.GetInteger("amount"))
}

func ExampleParseBytes() {
	// Parse a `SectionValue` from []byte containing the config
	data := []byte("amount = 500;")
	settings, err := forge.ParseBytes(data)
	if err != nil {
		panic(err)
	}

	fmt.Println(settings.GetInteger("amount"))
}

func ExampleParseReader() {
	// Parse a `SectionValue` from []byte containing the config
	data := []byte("amount = 500;")
	reader := bytes.NewBuffer(data)
	settings, err := forge.ParseReader(reader)
	if err != nil {
		panic(err)
	}

	fmt.Println(settings.GetInteger("amount"))
}
<commit_msg>fix up examples<commit_after>package forge_test

import (
	"bytes"
	"fmt"

	"github.com/brettlangdon/forge"
)

func Example() {
	// Parse a `SectionValue` from `example.cfg`
	settings, err := forge.ParseFile("example.cfg")
	if err != nil {
		panic(err)
	}

	// Get a single value
	if settings.Exists("global") {
		// Get `global` casted as a string
		value, _ := settings.GetString("global")
		fmt.Printf("global = \"%s\"\r\n", value)
	}

	// Get a nested value
	value, err := settings.Resolve("primary.included_setting")
	fmt.Printf("primary.included_setting = \"%s\"\r\n", value.GetValue())

	// You can also traverse down the sections manually
	primary, err := settings.GetSection("primary")
	strVal, err := primary.GetString("included_setting")
	fmt.Printf("primary.included_setting = \"%s\"\r\n", strVal)

	// Convert settings to a map
	settingsMap := settings.ToMap()
	fmt.Printf("global = \"%s\"\r\n", settingsMap["global"])

	// Convert settings to JSON
	jsonBytes, err := settings.ToJSON()
	fmt.Printf("\r\n\r\n%s\r\n", string(jsonBytes))
}

func ExampleParseFile() {
	// Parse a `SectionValue` from `example.cfg`
	settings, err := forge.ParseFile("example.cfg")
	if err != nil {
		panic(err)
	}
	fmt.Println(settings)
}

func ExampleParseString() {
	// Parse a `SectionValue` from string containing the config
	data := "amount = 500;"
	settings, err := forge.ParseString(data)
	if err != nil {
		panic(err)
	}

	fmt.Println(settings.GetInteger("amount"))
}

func ExampleParseBytes() {
	// Parse a `SectionValue` from []byte containing the config
	data := []byte("amount = 500;")
	settings, err := forge.ParseBytes(data)
	if err != nil {
		panic(err)
	}

	fmt.Println(settings.GetInteger("amount"))
}

func ExampleParseReader() {
	// Parse a `SectionValue` from []byte containing the config
	data := []byte("amount = 500;")
	reader := bytes.NewBuffer(data)
	settings, err := forge.ParseReader(reader)
	if err != nil {
		panic(err)
	}

	fmt.Println(settings.GetInteger("amount"))
}
<|endoftext|>


<commit_before>package multiconfig

import (
	"fmt"
	"os"
)

func ExampleDefaultLoader() {
	// Our struct which is used for configuration
	type ServerConfig struct {
		Name    string `default:"gopher"`
		Port    int    `default:"6060"`
		Enabled bool
		Users   []string
	}

	// Instantiate a default loader.
	d := NewWithPath("testdata/config.toml")

	s := &ServerConfig{}

	// It first reads from config.toml, next from environment variables and
	// finally from command line flags. Panic's if loading fails.
	d.MustLoad(s)

	fmt.Println("Host-->", s.Name)
	fmt.Println("Port-->", s.Port)

	// Output:
	// Host--> koding
	// Port--> 6060

}

func ExampleMultiLoader() {
	// Our struct which is used for configuration
	type ServerConfig struct {
		Name     string
		Port     int
		Enabled  bool
		Users    []string
		Postgres Postgres
	}

	os.Setenv("SERVERCONFIG_NAME", "koding")
	os.Setenv("SERVERCONFIG_PORT", "6060")

	// Create a custom multi loader intance based on your needs.
	f := &FlagLoader{}
	e := &EnvironmentLoader{}

	l := MultiLoader(f, e)

	// Load configs into our s variable from the sources above
	s := &ServerConfig{}
	err := l.Load(s)
	if err != nil {
		panic(err)
	}

	fmt.Println("Host-->", s.Name)
	fmt.Println("Port-->", s.Port)

	// Output:
	// Host--> koding
	// Port--> 6060
}

func ExampleEnvironmentLoader() {
	// Our struct which is used for configuration
	type ServerConfig struct {
		Name     string
		Port     int
		Enabled  bool
		Users    []string
		Postgres Postgres
	}

	// Assume those values defined before running the Loader
	os.Setenv("SERVERCONFIG_NAME", "koding")
	os.Setenv("SERVERCONFIG_PORT", "6060")

	// Instantiate loader
	l := &EnvironmentLoader{}

	s := &ServerConfig{}
	err := l.Load(s)
	if err != nil {
		panic(err)
	}

	fmt.Println("Host-->", s.Name)
	fmt.Println("Port-->", s.Port)

	// Output:
	// Host--> koding
	// Port--> 6060
}

func ExampleTOMLLoader() {
	// Our struct which is used for configuration
	type ServerConfig struct {
		Name     string
		Port     int
		Enabled  bool
		Users    []string
		Postgres Postgres
	}

	// Instantiate loader
	l := &TOMLLoader{Path: testTOML}

	s := &ServerConfig{}
	err := l.Load(s)
	if err != nil {
		panic(err)
	}

	fmt.Println("Host-->", s.Name)
	fmt.Println("Users-->", s.Users)

	// Output:
	// Host--> koding
	// Users--> [ankara istanbul]
}

func ExampleJSONLoader() {
	// Our struct which is used for configuration
	type ServerConfig struct {
		Name     string
		Port     int
		Enabled  bool
		Users    []string
		Postgres Postgres
	}

	// Instantiate loader
	l := &JSONLoader{Path: testJSON}

	s := &ServerConfig{}
	err := l.Load(s)
	if err != nil {
		panic(err)
	}

	fmt.Println("Host-->", s.Name)
	fmt.Println("Users-->", s.Users)

	// Output:
	// Host--> koding
	// Users--> [ankara istanbul]
}
<commit_msg>example_test: improve doc in defaultLoader<commit_after>package multiconfig

import (
	"fmt"
	"os"
)

func ExampleDefaultLoader() {
	// Our struct which is used for configuration
	type ServerConfig struct {
		Name    string `default:"gopher"`
		Port    int    `default:"6060"`
		Enabled bool
		Users   []string
	}

	// Instantiate a default loader.
	d := NewWithPath("testdata/config.toml")

	s := &ServerConfig{}

	// It first sets the default values for each field with tag values defined
	// with "default", next it reads from config.toml, from environment
	// variables and finally from command line flags. It panic's if loading fails.
	d.MustLoad(s)

	fmt.Println("Host-->", s.Name)
	fmt.Println("Port-->", s.Port)

	// Output:
	// Host--> koding
	// Port--> 6060

}

func ExampleMultiLoader() {
	// Our struct which is used for configuration
	type ServerConfig struct {
		Name     string
		Port     int
		Enabled  bool
		Users    []string
		Postgres Postgres
	}

	os.Setenv("SERVERCONFIG_NAME", "koding")
	os.Setenv("SERVERCONFIG_PORT", "6060")

	// Create a custom multi loader intance based on your needs.
	f := &FlagLoader{}
	e := &EnvironmentLoader{}

	l := MultiLoader(f, e)

	// Load configs into our s variable from the sources above
	s := &ServerConfig{}
	err := l.Load(s)
	if err != nil {
		panic(err)
	}

	fmt.Println("Host-->", s.Name)
	fmt.Println("Port-->", s.Port)

	// Output:
	// Host--> koding
	// Port--> 6060
}

func ExampleEnvironmentLoader() {
	// Our struct which is used for configuration
	type ServerConfig struct {
		Name     string
		Port     int
		Enabled  bool
		Users    []string
		Postgres Postgres
	}

	// Assume those values defined before running the Loader
	os.Setenv("SERVERCONFIG_NAME", "koding")
	os.Setenv("SERVERCONFIG_PORT", "6060")

	// Instantiate loader
	l := &EnvironmentLoader{}

	s := &ServerConfig{}
	err := l.Load(s)
	if err != nil {
		panic(err)
	}

	fmt.Println("Host-->", s.Name)
	fmt.Println("Port-->", s.Port)

	// Output:
	// Host--> koding
	// Port--> 6060
}

func ExampleTOMLLoader() {
	// Our struct which is used for configuration
	type ServerConfig struct {
		Name     string
		Port     int
		Enabled  bool
		Users    []string
		Postgres Postgres
	}

	// Instantiate loader
	l := &TOMLLoader{Path: testTOML}

	s := &ServerConfig{}
	err := l.Load(s)
	if err != nil {
		panic(err)
	}

	fmt.Println("Host-->", s.Name)
	fmt.Println("Users-->", s.Users)

	// Output:
	// Host--> koding
	// Users--> [ankara istanbul]
}

func ExampleJSONLoader() {
	// Our struct which is used for configuration
	type ServerConfig struct {
		Name     string
		Port     int
		Enabled  bool
		Users    []string
		Postgres Postgres
	}

	// Instantiate loader
	l := &JSONLoader{Path: testJSON}

	s := &ServerConfig{}
	err := l.Load(s)
	if err != nil {
		panic(err)
	}

	fmt.Println("Host-->", s.Name)
	fmt.Println("Users-->", s.Users)

	// Output:
	// Host--> koding
	// Users--> [ankara istanbul]
}
<|endoftext|>


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

package docker_test

import (
	"archive/tar"
	"bytes"
	"github.com/fsouza/go-dockerclient"
	"io"
	"log"
	"time"
)

func ExampleClient_AttachToContainer() {
	client, err := docker.NewClient("http://localhost:4243")
	if err != nil {
		log.Fatal(err)
	}
	// Reading logs from container a84849 and sending them to buf.
	var buf bytes.Buffer
	err = client.AttachToContainer(docker.AttachToContainerOptions{
		Container:    "a84849",
		OutputStream: &buf,
		Logs:         true,
		Stdout:       true,
		Stderr:       true,
	})
	if err != nil {
		log.Fatal(err)
	}
	log.Println(buf.String())
	// Attaching to stdout and streaming.
	buf.Reset()
	err = client.AttachToContainer(docker.AttachToContainerOptions{
		Container:    "a84849",
		OutputStream: &buf,
		Stdout:       true,
		Stream:       true,
	})
	if err != nil {
		log.Fatal(err)
	}
	log.Println(buf.String())
}

func ExampleClient_CopyFromContainer() {
	client, err := docker.NewClient("http://localhost:4243")
	if err != nil {
		log.Fatal(err)
	}
	cid := "a84849"
	// Copy resulting file
	var buf bytes.Buffer
	filename := "/tmp/output.txt"
	err = client.CopyFromContainer(docker.CopyFromContainerOptions{
		Container:    cid,
		Resource:     filename,
		OutputStream: &buf,
	})
	if err != nil {
		log.Fatalf("Error while copying from %s: %s\n", cid, err)
	}
	content := new(bytes.Buffer)
	r := bytes.NewReader(buf.Bytes())
	tr := tar.NewReader(r)
	tr.Next()
	if err != nil && err != io.EOF {
		log.Fatal(err)
	}
	if _, err := io.Copy(content, tr); err != nil {
		log.Fatal(err)
	}
	log.Println(buf.String())
}

func ExampleClient_BuildImage() {
	client, err := docker.NewClient("http://localhost:4243")
	if err != nil {
		log.Fatal(err)
	}
	t := time.Now()
	inputbuf, outputbuf := bytes.NewBuffer(nil), bytes.NewBuffer(nil)
	tr := tar.NewWriter(inputbuf)
	tr.WriteHeader(&tar.Header{Name: "Dockerfile", Size: 10, ModTime: t, AccessTime: t, ChangeTime: t})
	tr.Write([]byte("FROM base\n"))
	tr.Close()
	opts := docker.BuildImageOptions{
		Name:         "test",
		InputStream:  inputbuf,
		OutputStream: outputbuf,
	}
	if imageid, err := client.BuildImage(opts); err != nil {
		log.Fatal(err)
	} else {
		log.Println("build image success, imageid:", imageid)
	}
}
<commit_msg>example_test: fix year in copyright header<commit_after>// Copyright 2014 go-dockerclient authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package docker_test

import (
	"archive/tar"
	"bytes"
	"github.com/fsouza/go-dockerclient"
	"io"
	"log"
	"time"
)

func ExampleClient_AttachToContainer() {
	client, err := docker.NewClient("http://localhost:4243")
	if err != nil {
		log.Fatal(err)
	}
	// Reading logs from container a84849 and sending them to buf.
	var buf bytes.Buffer
	err = client.AttachToContainer(docker.AttachToContainerOptions{
		Container:    "a84849",
		OutputStream: &buf,
		Logs:         true,
		Stdout:       true,
		Stderr:       true,
	})
	if err != nil {
		log.Fatal(err)
	}
	log.Println(buf.String())
	// Attaching to stdout and streaming.
	buf.Reset()
	err = client.AttachToContainer(docker.AttachToContainerOptions{
		Container:    "a84849",
		OutputStream: &buf,
		Stdout:       true,
		Stream:       true,
	})
	if err != nil {
		log.Fatal(err)
	}
	log.Println(buf.String())
}

func ExampleClient_CopyFromContainer() {
	client, err := docker.NewClient("http://localhost:4243")
	if err != nil {
		log.Fatal(err)
	}
	cid := "a84849"
	// Copy resulting file
	var buf bytes.Buffer
	filename := "/tmp/output.txt"
	err = client.CopyFromContainer(docker.CopyFromContainerOptions{
		Container:    cid,
		Resource:     filename,
		OutputStream: &buf,
	})
	if err != nil {
		log.Fatalf("Error while copying from %s: %s\n", cid, err)
	}
	content := new(bytes.Buffer)
	r := bytes.NewReader(buf.Bytes())
	tr := tar.NewReader(r)
	tr.Next()
	if err != nil && err != io.EOF {
		log.Fatal(err)
	}
	if _, err := io.Copy(content, tr); err != nil {
		log.Fatal(err)
	}
	log.Println(buf.String())
}

func ExampleClient_BuildImage() {
	client, err := docker.NewClient("http://localhost:4243")
	if err != nil {
		log.Fatal(err)
	}
	t := time.Now()
	inputbuf, outputbuf := bytes.NewBuffer(nil), bytes.NewBuffer(nil)
	tr := tar.NewWriter(inputbuf)
	tr.WriteHeader(&tar.Header{Name: "Dockerfile", Size: 10, ModTime: t, AccessTime: t, ChangeTime: t})
	tr.Write([]byte("FROM base\n"))
	tr.Close()
	opts := docker.BuildImageOptions{
		Name:         "test",
		InputStream:  inputbuf,
		OutputStream: outputbuf,
	}
	if imageid, err := client.BuildImage(opts); err != nil {
		log.Fatal(err)
	} else {
		log.Println("build image success, imageid:", imageid)
	}
}
<|endoftext|>


<commit_before>package enmime_test

import (
	"fmt"
	"os"

	"github.com/jhillyerd/enmime"
)

func Example() {
	r, _ := os.Open("testdata/mail/qp-utf8-header.raw")
	env, _ := enmime.ReadEnvelope(r) // Parse message body with enmime

	// Headers can be retrieved via Envelope.GetHeader(name)
	fmt.Printf("From: %v\n", env.GetHeader("From"))

	// Address-type headers can be parsed into a list of decoded mail.Address structs
	alist, _ := env.AddressList("To")
	for _, addr := range alist {
		fmt.Printf("To: %s <%s>\n", addr.Name, addr.Address)
	}

	// enmime can decode quoted-printable headers
	fmt.Printf("Subject: %v\n", env.GetHeader("Subject"))

	// The plain text body is available as mime.Text
	fmt.Printf("Text Body: %v chars\n", len(env.Text))

	// The HTML body is stored in mime.HTML
	fmt.Printf("HTML Body: %v chars\n", len(env.HTML))

	// mime.Inlines is a slice of inlined attacments
	fmt.Printf("Inlines: %v\n", len(env.Inlines))

	// mime.Attachments contains the non-inline attachments
	fmt.Printf("Attachments: %v\n", len(env.Attachments))

	// Output:
	// From: James Hillyerd <jamehi03@jamehi03lx.noa.com>
	// To: Mirosław Marczak <marczak@inbucket.com>
	// Subject: MIME UTF8 Test ¢ More Text
	// Text Body: 1300 chars
	// HTML Body: 1736 chars
	// Inlines: 0
	// Attachments: 0
}
<commit_msg>Prevent panics in example/doc test<commit_after>package enmime_test

import (
	"fmt"
	"os"

	"github.com/jhillyerd/enmime"
)

func Example() {
	// Open a sample message file
	r, err := os.Open("testdata/mail/qp-utf8-header.raw")
	if err != nil {
		fmt.Print(err)
		return
	}

	// Parse message body with enmime
	env, err := enmime.ReadEnvelope(r)
	if err != nil {
		fmt.Print(err)
		return
	}

	// Headers can be retrieved via Envelope.GetHeader(name)
	fmt.Printf("From: %v\n", env.GetHeader("From"))

	// Address-type headers can be parsed into a list of decoded mail.Address structs
	alist, _ := env.AddressList("To")
	for _, addr := range alist {
		fmt.Printf("To: %s <%s>\n", addr.Name, addr.Address)
	}

	// enmime can decode quoted-printable headers
	fmt.Printf("Subject: %v\n", env.GetHeader("Subject"))

	// The plain text body is available as mime.Text
	fmt.Printf("Text Body: %v chars\n", len(env.Text))

	// The HTML body is stored in mime.HTML
	fmt.Printf("HTML Body: %v chars\n", len(env.HTML))

	// mime.Inlines is a slice of inlined attacments
	fmt.Printf("Inlines: %v\n", len(env.Inlines))

	// mime.Attachments contains the non-inline attachments
	fmt.Printf("Attachments: %v\n", len(env.Attachments))

	// Output:
	// From: James Hillyerd <jamehi03@jamehi03lx.noa.com>
	// To: Mirosław Marczak <marczak@inbucket.com>
	// Subject: MIME UTF8 Test ¢ More Text
	// Text Body: 1300 chars
	// HTML Body: 1736 chars
	// Inlines: 0
	// Attachments: 0
}
<|endoftext|>


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

package goson_test

import (
	"encoding/json"
	"fmt"
	"io"
	"log"
	"os"
	"strings"
)

func ExampleMarshal() {
	type ColorGroup struct {
		ID     int
		Name   string
		Colors []string
	}
	group := ColorGroup{
		ID:     1,
		Name:   "Reds",
		Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
	}
	b, err := json.Marshal(group)
	if err != nil {
		fmt.Println("error:", err)
	}
	os.Stdout.Write(b)
	// Output:
	// {"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}
}

func ExampleUnmarshal() {
	var jsonBlob = []byte(`[
		{"Name": "Platypus", "Order": "Monotremata"},
		{"Name": "Quoll",    "Order": "Dasyuromorphia"}
	]`)
	type Animal struct {
		Name  string
		Order string
	}
	var animals []Animal
	err := json.Unmarshal(jsonBlob, &animals)
	if err != nil {
		fmt.Println("error:", err)
	}
	fmt.Printf("%+v", animals)
	// Output:
	// [{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]
}

// This example uses a Decoder to decode a stream of distinct JSON values.
func ExampleDecoder() {
	const jsonStream = `
		{"Name": "Ed", "Text": "Knock knock."}
		{"Name": "Sam", "Text": "Who's there?"}
		{"Name": "Ed", "Text": "Go fmt."}
		{"Name": "Sam", "Text": "Go fmt who?"}
		{"Name": "Ed", "Text": "Go fmt yourself!"}
	`
	type Message struct {
		Name, Text string
	}
	dec := json.NewDecoder(strings.NewReader(jsonStream))
	for {
		var m Message
		if err := dec.Decode(&m); err == io.EOF {
			break
		} else if err != nil {
			log.Fatal(err)
		}
		fmt.Printf("%s: %s\n", m.Name, m.Text)
	}
	// Output:
	// Ed: Knock knock.
	// Sam: Who's there?
	// Ed: Go fmt.
	// Sam: Go fmt who?
	// Ed: Go fmt yourself!
}
<commit_msg>add example<commit_after>// Copyright 2011 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package goson_test

import (
	"fmt"
	"io"
	"launchpad.net/goson"
	"log"
	"os"
	"strings"
)

func ExampleMarshal() {
	type ColorGroup struct {
		ID     int
		Name   string
		Colors []string
	}
	group := ColorGroup{
		ID:     1,
		Name:   "Reds",
		Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
	}
	b, err := goson.Marshal(group)
	if err != nil {
		fmt.Println("error:", err)
	}
	os.Stdout.Write(b)
	// Output:
	// {"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}
}

func ExampleUnmarshal() {
	var gosonBlob = []byte(`[
		{"Name": "Platypus", "Order": "Monotremata"},
		{"Name": "Quoll",    "Order": "Dasyuromorphia"}
	]`)
	type Animal struct {
		Name  string
		Order string
	}
	var animals []Animal
	err := goson.Unmarshal(gosonBlob, &animals)
	if err != nil {
		fmt.Println("error:", err)
	}
	fmt.Printf("%+v", animals)
	// Output:
	// [{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]
}

// This example uses a Decoder to decode a stream of distinct JSON values.
func ExampleDecoder() {
	const gosonStream = `
		{"Name": "Ed", "Text": "Knock knock."}
		{"Name": "Sam", "Text": "Who's there?"}
		{"Name": "Ed", "Text": "Go fmt."}
		{"Name": "Sam", "Text": "Go fmt who?"}
		{"Name": "Ed", "Text": "Go fmt yourself!"}
	`
	type Message struct {
		Name, Text string
	}
	dec := goson.NewDecoder(strings.NewReader(gosonStream))
	for {
		var m Message
		if err := dec.Decode(&m); err == io.EOF {
			break
		} else if err != nil {
			log.Fatal(err)
		}
		fmt.Printf("%s: %s\n", m.Name, m.Text)
	}
	// Output:
	// Ed: Knock knock.
	// Sam: Who's there?
	// Ed: Go fmt.
	// Sam: Go fmt who?
	// Ed: Go fmt yourself!
}

func ExampleMarshalIndent() {
	type ColorGroup struct {
		ID     int
		Name   string
		Colors []string
	}
	group := ColorGroup{
		ID:     1,
		Name:   "Reds",
		Colors: []string{"Crimson", "Red", "Ruby", "Maroon", "Dark amber"},
	}
	b, err := goson.MarshalIndent(group, "", "\t")
	if err != nil {
		fmt.Println("error:", err)
	}
	os.Stdout.Write(b)
	// Output:
	// {
	// 	ID: 1
	// 	Name: Reds
	// 	Colors: [
	// 		Crimson
	// 		Red
	// 		Ruby
	// 		Maroon
	// 		"Dark amber"
	// 	]
	// }
}
<|endoftext|>


<commit_before>package mint_test

import "github.com/otiai10/mint"
import "testing"

// This is example of `Expect` and `ToBe`
// `ToBe` can assert to eqaal
func ExampleMint_Expect_ToBe(t *testing.T) {
	yours := 100
	expected := 100
	mint.Expect(t, yours).ToBe(expected)
}

// This is example of `Dry`
// `Dry` does NOT os.Exit(1)
// but returns `ProxyTestee` with `Result`
func ExampleMint_Dry(t *testing.T) {
	yours := 100
	expected := 100
	result := mint.Expect(t, yours).Dry().ToBe(expected).Result
	if !result.OK {
		t.Fail()
	}
}

// `Blend` provide *mint.Testee.
// It can save the repeating of `t`.
func ExampleMint_Expect(t *testing.T) {
	// get blended mint
	m := mint.Blend(t)

	yours := 100
	m.Expect(yours).ToBe(100)
	result := m.Expect(yours).Dry().ToBe(200).Result
	if result.OK {
		t.Fail()
	}
	m.Expect(yours).Not().ToBe(200)
}
<commit_msg>Fix Examples in godoc<commit_after>package mint_test

import "github.com/otiai10/mint"
import "testing"

// "Expect" provides "*mint.Testee".
// It has assertion methods such as "ToBe".
func ExampleExpect(t *testing.T) {
	mint.Expect(t, 100).ToBe(100)
	mint.Expect(t, 100).TyepOf("int")
}

// "*Testee.ToBe" can assert the testee to equal the parameter of this func.
// OS will exit with code 1, when the assertion fail.
// If you don't want to exit, see "Dry()".
func ExampleTestee_ToBe(t *testing.T) {
	mint.Expect(t, 100).ToBe(100)
}

// "*Testee.TypeOf" can assert the type of testee to equal the parameter of this func.
// OS will exit with code 1, when the assertion fail.
// If you don't want to exit, see "Dry()".
func ExampleTestee_TypeOf(t *testing.T) {
	mint.Expect(t, 100).TypeOf("int")
}

// "*Testee.Not" makes following assertion conversed.
func ExampleTestee_Not(t *testing.T) {
	mint.Expect(t, 100).Not().ToBe(200)
	mint.Expect(t, 100).Not().TyepOf("string")
}

// "*Testee.Dry" makes the testee NOT to call "os.Exit(1)".
// Use this if you want to fail test in a purpose.
func ExampleTestee_Dry(t *testing.T) {
	result := mint.Expect(t, 100).Dry().ToBe(100).Result
	if !result.OK {
		t.Fail()
	}
}

// "Blend" provides (blended) *mint.Mint.
// You can save writing "t" repeatedly.
func ExampleBlend(t *testing.T) {
	// get blended mint
	m := mint.Blend(t)

	m.Expect(100).ToBe(100)
	m.Expect(100).Not().ToBe(200)
}
<|endoftext|>


<commit_before>package facade

import (
	"fmt"
	"log"
	"os"
	"path"
	"time"

	"github.com/nyaxt/otaru"
	"github.com/nyaxt/otaru/blobstore"
	"github.com/nyaxt/otaru/blobstore/cachedblobstore"
	"github.com/nyaxt/otaru/btncrypt"
	"github.com/nyaxt/otaru/chunkstore"
	oflags "github.com/nyaxt/otaru/flags"
	"github.com/nyaxt/otaru/gcloud/auth"
	"github.com/nyaxt/otaru/gcloud/datastore"
	"github.com/nyaxt/otaru/gcloud/gcs"
	"github.com/nyaxt/otaru/inodedb"
	"github.com/nyaxt/otaru/inodedb/blobstoredbstatesnapshotio"
	"github.com/nyaxt/otaru/metadata"
	"github.com/nyaxt/otaru/mgmt"
	"github.com/nyaxt/otaru/scheduler"
	"github.com/nyaxt/otaru/util"
)

type Otaru struct {
	C btncrypt.Cipher

	S *scheduler.Scheduler

	Clisrc auth.ClientSource
	DSCfg  *datastore.Config
	GL     *datastore.GlobalLocker

	MetadataBS blobstore.BlobStore
	DefaultBS  blobstore.BlobStore

	BackendBS blobstore.BlobStore

	CacheTgtBS *blobstore.FileBlobStore
	CBS        *cachedblobstore.CachedBlobStore
	CSS        *util.PeriodicRunner

	SSLoc blobstoredbstatesnapshotio.SSLocator
	SIO   *blobstoredbstatesnapshotio.DBStateSnapshotIO

	TxIO   inodedb.DBTransactionLogIO
	TxIOSS *util.PeriodicRunner

	IDBBE *inodedb.DB
	IDBS  *inodedb.DBService
	IDBSS *util.PeriodicRunner

	FS   *otaru.FileSystem
	MGMT *mgmt.Server
}

func NewOtaru(cfg *Config, oneshotcfg *OneshotConfig) (*Otaru, error) {
	o := &Otaru{}

	var err error

	key := btncrypt.KeyFromPassword(cfg.Password)
	o.C, err = btncrypt.NewCipher(key)
	if err != nil {
		o.Close()
		return nil, fmt.Errorf("Failed to init Cipher: %v", err)
	}

	o.S = scheduler.NewScheduler()

	if !cfg.LocalDebug {
		o.Clisrc, err = auth.GetGCloudClientSource(cfg.CredentialsFilePath, cfg.TokenCacheFilePath, false)
		if err != nil {
			o.Close()
			return nil, fmt.Errorf("Failed to init GCloudClientSource: %v", err)
		}
		o.DSCfg = datastore.NewConfig(cfg.ProjectName, cfg.BucketName, o.C, o.Clisrc)
		o.GL = datastore.NewGlobalLocker(o.DSCfg, genHostName(), "FIXME: fill info")
		if err := o.GL.Lock(); err != nil {
			return nil, err
		}
	}

	o.CacheTgtBS, err = blobstore.NewFileBlobStore(cfg.CacheDir, oflags.O_RDWRCREATE)
	if err != nil {
		o.Close()
		return nil, fmt.Errorf("Failed to init FileBlobStore: %v", err)
	}

	if !cfg.LocalDebug {
		o.DefaultBS, err = gcs.NewGCSBlobStore(cfg.ProjectName, cfg.BucketName, o.Clisrc, oflags.O_RDWRCREATE)
		if err != nil {
			o.Close()
			return nil, fmt.Errorf("Failed to init GCSBlobStore: %v", err)
		}
		if !cfg.UseSeparateBucketForMetadata {
			o.BackendBS = o.DefaultBS
		} else {
			metabucketname := fmt.Sprintf("%s-meta", cfg.BucketName)
			o.MetadataBS, err = gcs.NewGCSBlobStore(cfg.ProjectName, metabucketname, o.Clisrc, oflags.O_RDWRCREATE)
			if err != nil {
				o.Close()
				return nil, fmt.Errorf("Failed to init GCSBlobStore (metadata): %v", err)
			}

			o.BackendBS = blobstore.Mux{
				blobstore.MuxEntry{metadata.IsMetadataBlobpath, o.MetadataBS},
				blobstore.MuxEntry{nil, o.DefaultBS},
			}
		}
	} else {
		o.BackendBS, err = blobstore.NewFileBlobStore(path.Join(os.Getenv("HOME"), ".otaru", "bbs"), oflags.O_RDWRCREATE)
	}

	queryFn := chunkstore.NewQueryChunkVersion(o.C)
	o.CBS, err = cachedblobstore.New(o.BackendBS, o.CacheTgtBS, oflags.O_RDWRCREATE /* FIXME */, queryFn)
	if err != nil {
		o.Close()
		return nil, fmt.Errorf("Failed to init CachedBlobStore: %v", err)
	}
	if err := o.CBS.RestoreState(o.C); err != nil {
		log.Printf("Warning: Attempted to restore cachedblobstore state but failed: %v", err)
	}
	o.CSS = cachedblobstore.NewCacheSyncScheduler(o.CBS)

	if !cfg.LocalDebug {
		o.SSLoc = datastore.NewINodeDBSSLocator(o.DSCfg)
	} else {
		panic("Implement mock sslocator that doesn't depend on gcloud/datastore")
	}
	o.SIO = blobstoredbstatesnapshotio.New(o.CBS, o.C, o.SSLoc)

	if !cfg.LocalDebug {
		txio := datastore.NewDBTransactionLogIO(o.DSCfg)
		o.TxIO = o.TxIO
		o.TxIOSS = util.NewSyncScheduler(txio, 300*time.Millisecond)
	} else {
		o.TxIO = inodedb.NewSimpleDBTransactionLogIO()
	}

	if oneshotcfg.Mkfs {
		o.IDBBE, err = inodedb.NewEmptyDB(o.SIO, o.TxIO)
		if err != nil {
			o.Close()
			return nil, fmt.Errorf("NewEmptyDB failed: %v", err)
		}
	} else {
		o.IDBBE, err = inodedb.NewDB(o.SIO, o.TxIO)
		if err != nil {
			o.Close()
			return nil, fmt.Errorf("NewDB failed: %v", err)
		}
	}

	o.IDBS = inodedb.NewDBService(o.IDBBE)
	o.IDBSS = util.NewSyncScheduler(o.IDBS, 30*time.Second)

	o.FS = otaru.NewFileSystem(o.IDBS, o.CBS, o.C)
	o.MGMT = mgmt.NewServer()
	if err := o.runMgmtServer(); err != nil {
		o.Close()
		return nil, fmt.Errorf("Mgmt server run failed: %v", err)
	}

	return o, nil
}

func (o *Otaru) Close() error {
	errs := []error{}

	if o.S != nil {
		o.S.AbortAllAndStop()
	}

	if o.FS != nil {
		if err := o.FS.Sync(); err != nil {
			errs = append(errs, err)
		}
	}

	if o.IDBSS != nil {
		o.IDBSS.Stop()
	}

	if o.IDBS != nil {
		o.IDBS.Quit()
	}

	if o.IDBBE != nil {
		if err := o.IDBBE.Sync(); err != nil {
			errs = append(errs, err)
		}
	}

	if o.CSS != nil {
		o.CSS.Stop()
	}

	if o.CBS != nil {
		if err := o.CBS.SaveState(o.C); err != nil {
			errs = append(errs, err)
		}
	}

	if o.GL != nil {
		if err := o.GL.Unlock(); err != nil {
			errs = append(errs, err)
		}
	}

	return util.ToErrors(errs)
}
<commit_msg>sync cachedblobstore on close<commit_after>package facade

import (
	"fmt"
	"log"
	"os"
	"path"
	"time"

	"github.com/nyaxt/otaru"
	"github.com/nyaxt/otaru/blobstore"
	"github.com/nyaxt/otaru/blobstore/cachedblobstore"
	"github.com/nyaxt/otaru/btncrypt"
	"github.com/nyaxt/otaru/chunkstore"
	oflags "github.com/nyaxt/otaru/flags"
	"github.com/nyaxt/otaru/gcloud/auth"
	"github.com/nyaxt/otaru/gcloud/datastore"
	"github.com/nyaxt/otaru/gcloud/gcs"
	"github.com/nyaxt/otaru/inodedb"
	"github.com/nyaxt/otaru/inodedb/blobstoredbstatesnapshotio"
	"github.com/nyaxt/otaru/metadata"
	"github.com/nyaxt/otaru/mgmt"
	"github.com/nyaxt/otaru/scheduler"
	"github.com/nyaxt/otaru/util"
)

type Otaru struct {
	C btncrypt.Cipher

	S *scheduler.Scheduler

	Clisrc auth.ClientSource
	DSCfg  *datastore.Config
	GL     *datastore.GlobalLocker

	MetadataBS blobstore.BlobStore
	DefaultBS  blobstore.BlobStore

	BackendBS blobstore.BlobStore

	CacheTgtBS *blobstore.FileBlobStore
	CBS        *cachedblobstore.CachedBlobStore
	CSS        *util.PeriodicRunner

	SSLoc blobstoredbstatesnapshotio.SSLocator
	SIO   *blobstoredbstatesnapshotio.DBStateSnapshotIO

	TxIO   inodedb.DBTransactionLogIO
	TxIOSS *util.PeriodicRunner

	IDBBE *inodedb.DB
	IDBS  *inodedb.DBService
	IDBSS *util.PeriodicRunner

	FS   *otaru.FileSystem
	MGMT *mgmt.Server
}

func NewOtaru(cfg *Config, oneshotcfg *OneshotConfig) (*Otaru, error) {
	o := &Otaru{}

	var err error

	key := btncrypt.KeyFromPassword(cfg.Password)
	o.C, err = btncrypt.NewCipher(key)
	if err != nil {
		o.Close()
		return nil, fmt.Errorf("Failed to init Cipher: %v", err)
	}

	o.S = scheduler.NewScheduler()

	if !cfg.LocalDebug {
		o.Clisrc, err = auth.GetGCloudClientSource(cfg.CredentialsFilePath, cfg.TokenCacheFilePath, false)
		if err != nil {
			o.Close()
			return nil, fmt.Errorf("Failed to init GCloudClientSource: %v", err)
		}
		o.DSCfg = datastore.NewConfig(cfg.ProjectName, cfg.BucketName, o.C, o.Clisrc)
		o.GL = datastore.NewGlobalLocker(o.DSCfg, genHostName(), "FIXME: fill info")
		if err := o.GL.Lock(); err != nil {
			return nil, err
		}
	}

	o.CacheTgtBS, err = blobstore.NewFileBlobStore(cfg.CacheDir, oflags.O_RDWRCREATE)
	if err != nil {
		o.Close()
		return nil, fmt.Errorf("Failed to init FileBlobStore: %v", err)
	}

	if !cfg.LocalDebug {
		o.DefaultBS, err = gcs.NewGCSBlobStore(cfg.ProjectName, cfg.BucketName, o.Clisrc, oflags.O_RDWRCREATE)
		if err != nil {
			o.Close()
			return nil, fmt.Errorf("Failed to init GCSBlobStore: %v", err)
		}
		if !cfg.UseSeparateBucketForMetadata {
			o.BackendBS = o.DefaultBS
		} else {
			metabucketname := fmt.Sprintf("%s-meta", cfg.BucketName)
			o.MetadataBS, err = gcs.NewGCSBlobStore(cfg.ProjectName, metabucketname, o.Clisrc, oflags.O_RDWRCREATE)
			if err != nil {
				o.Close()
				return nil, fmt.Errorf("Failed to init GCSBlobStore (metadata): %v", err)
			}

			o.BackendBS = blobstore.Mux{
				blobstore.MuxEntry{metadata.IsMetadataBlobpath, o.MetadataBS},
				blobstore.MuxEntry{nil, o.DefaultBS},
			}
		}
	} else {
		o.BackendBS, err = blobstore.NewFileBlobStore(path.Join(os.Getenv("HOME"), ".otaru", "bbs"), oflags.O_RDWRCREATE)
	}

	queryFn := chunkstore.NewQueryChunkVersion(o.C)
	o.CBS, err = cachedblobstore.New(o.BackendBS, o.CacheTgtBS, oflags.O_RDWRCREATE /* FIXME */, queryFn)
	if err != nil {
		o.Close()
		return nil, fmt.Errorf("Failed to init CachedBlobStore: %v", err)
	}
	if err := o.CBS.RestoreState(o.C); err != nil {
		log.Printf("Warning: Attempted to restore cachedblobstore state but failed: %v", err)
	}
	o.CSS = cachedblobstore.NewCacheSyncScheduler(o.CBS)

	if !cfg.LocalDebug {
		o.SSLoc = datastore.NewINodeDBSSLocator(o.DSCfg)
	} else {
		panic("Implement mock sslocator that doesn't depend on gcloud/datastore")
	}
	o.SIO = blobstoredbstatesnapshotio.New(o.CBS, o.C, o.SSLoc)

	if !cfg.LocalDebug {
		txio := datastore.NewDBTransactionLogIO(o.DSCfg)
		o.TxIO = o.TxIO
		o.TxIOSS = util.NewSyncScheduler(txio, 300*time.Millisecond)
	} else {
		o.TxIO = inodedb.NewSimpleDBTransactionLogIO()
	}

	if oneshotcfg.Mkfs {
		o.IDBBE, err = inodedb.NewEmptyDB(o.SIO, o.TxIO)
		if err != nil {
			o.Close()
			return nil, fmt.Errorf("NewEmptyDB failed: %v", err)
		}
	} else {
		o.IDBBE, err = inodedb.NewDB(o.SIO, o.TxIO)
		if err != nil {
			o.Close()
			return nil, fmt.Errorf("NewDB failed: %v", err)
		}
	}

	o.IDBS = inodedb.NewDBService(o.IDBBE)
	o.IDBSS = util.NewSyncScheduler(o.IDBS, 30*time.Second)

	o.FS = otaru.NewFileSystem(o.IDBS, o.CBS, o.C)
	o.MGMT = mgmt.NewServer()
	if err := o.runMgmtServer(); err != nil {
		o.Close()
		return nil, fmt.Errorf("Mgmt server run failed: %v", err)
	}

	return o, nil
}

func (o *Otaru) Close() error {
	errs := []error{}

	if o.S != nil {
		o.S.AbortAllAndStop()
	}

	if o.FS != nil {
		if err := o.FS.Sync(); err != nil {
			errs = append(errs, err)
		}
	}

	if o.IDBSS != nil {
		o.IDBSS.Stop()
	}

	if o.IDBS != nil {
		o.IDBS.Quit()
	}

	if o.IDBBE != nil {
		if err := o.IDBBE.Sync(); err != nil {
			errs = append(errs, err)
		}
	}

	if o.CSS != nil {
		o.CSS.Stop()
	}

	if o.CBS != nil {
		if err := o.CBS.Sync(); err != nil {
			errs = append(errs, err)
		}
		if err := o.CBS.SaveState(o.C); err != nil {
			errs = append(errs, err)
		}
	}

	if o.GL != nil {
		if err := o.GL.Unlock(); err != nil {
			errs = append(errs, err)
		}
	}

	return util.ToErrors(errs)
}
<|endoftext|>


<commit_before>/*
 * Copyright (c) 2011 Matt Jibson <matt.jibson@gmail.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

package fft

import (
	"math"
	"runtime"
	"testing"

	"github.com/mjibson/go-dsp/dsputils"
)

const (
	sqrt2_2 = math.Sqrt2 / 2
)

type fftTest struct {
	in  []float64
	out []complex128
}

var fftTests = []fftTest{
	// impulse responses
	fftTest{
		[]float64{1},
		[]complex128{complex(1, 0)},
	},
	fftTest{
		[]float64{1, 0},
		[]complex128{complex(1, 0), complex(1, 0)},
	},
	fftTest{
		[]float64{1, 0, 0, 0},
		[]complex128{complex(1, 0), complex(1, 0), complex(1, 0), complex(1, 0)},
	},
	fftTest{
		[]float64{1, 0, 0, 0, 0, 0, 0, 0},
		[]complex128{
			complex(1, 0),
			complex(1, 0),
			complex(1, 0),
			complex(1, 0),
			complex(1, 0),
			complex(1, 0),
			complex(1, 0),
			complex(1, 0)},
	},

	// shifted impulse response
	fftTest{
		[]float64{0, 1},
		[]complex128{complex(1, 0), complex(-1, 0)},
	},
	fftTest{
		[]float64{0, 1, 0, 0},
		[]complex128{complex(1, 0), complex(0, -1), complex(-1, 0), complex(0, 1)},
	},
	fftTest{
		[]float64{0, 1, 0, 0, 0, 0, 0, 0},
		[]complex128{
			complex(1, 0),
			complex(sqrt2_2, -sqrt2_2),
			complex(0, -1),
			complex(-sqrt2_2, -sqrt2_2),
			complex(-1, 0),
			complex(-sqrt2_2, sqrt2_2),
			complex(0, 1),
			complex(sqrt2_2, sqrt2_2)},
	},

	// other
	fftTest{
		[]float64{1, 2, 3, 4},
		[]complex128{
			complex(10, 0),
			complex(-2, 2),
			complex(-2, 0),
			complex(-2, -2)},
	},
	fftTest{
		[]float64{1, 3, 5, 7},
		[]complex128{
			complex(16, 0),
			complex(-4, 4),
			complex(-4, 0),
			complex(-4, -4)},
	},
	fftTest{
		[]float64{1, 2, 3, 4, 5, 6, 7, 8},
		[]complex128{
			complex(36, 0),
			complex(-4, 9.65685425),
			complex(-4, 4),
			complex(-4, 1.65685425),
			complex(-4, 0),
			complex(-4, -1.65685425),
			complex(-4, -4),
			complex(-4, -9.65685425)},
	},

	// non power of 2 lengths
	fftTest{
		[]float64{1, 0, 0, 0, 0},
		[]complex128{
			complex(1, 0),
			complex(1, 0),
			complex(1, 0),
			complex(1, 0),
			complex(1, 0)},
	},
	fftTest{
		[]float64{1, 2, 3},
		[]complex128{
			complex(6, 0),
			complex(-1.5, 0.8660254),
			complex(-1.5, -0.8660254)},
	},
	fftTest{
		[]float64{1, 1, 1},
		[]complex128{
			complex(3, 0),
			complex(0, 0),
			complex(0, 0)},
	},
}

type fft2Test struct {
	in  [][]float64
	out [][]complex128
}

var fft2Tests = []fft2Test{
	fft2Test{
		[][]float64{{1, 2, 3}, {3, 4, 5}},
		[][]complex128{
			{complex(18, 0), complex(-3, 1.73205081), complex(-3, -1.73205081)},
			{complex(-6, 0), complex(0, 0), complex(0, 0)}},
	},
	fft2Test{
		[][]float64{{0.1, 0.2, 0.3, 0.4, 0.5}, {1, 2, 3, 4, 5}, {3, 2, 1, 0, -1}},
		[][]complex128{
			{complex(21.5, 0), complex(-0.25, 0.34409548), complex(-0.25, 0.08122992), complex(-0.25, -0.08122992), complex(-0.25, -0.34409548)},
			{complex(-8.5, -8.66025404), complex(5.70990854, 4.6742225), complex(1.15694356, 4.41135694), complex(-1.65694356, 4.24889709), complex(-6.20990854, 3.98603154)},
			{complex(-8.5, 8.66025404), complex(-6.20990854, -3.98603154), complex(-1.65694356, -4.24889709), complex(1.15694356, -4.41135694), complex(5.70990854, -4.6742225)}},
	},
}

type fftnTest struct {
	in  []float64
	dim []int
	out []complex128
}

var fftnTests = []fftnTest{
	fftnTest{
		[]float64{4, 2, 3, 8, 5, 6, 7, 2, 13, 24, 13, 17},
		[]int{2, 2, 3},
		[]complex128{
			complex(104, 0), complex(12.5, 14.72243186), complex(12.5, -14.72243186),
			complex(-42, 0), complex(-10.5, 6.06217783), complex(-10.5, -6.06217783),

			complex(-48, 0), complex(-4.5, -11.25833025), complex(-4.5, 11.25833025),
			complex(22, 0), complex(8.5, -6.06217783), complex(8.5, 6.06217783)},
	},
}

type reverseBitsTest struct {
	in  uint
	sz  uint
	out uint
}

var reverseBitsTests = []reverseBitsTest{
	reverseBitsTest{0, 1, 0},
	reverseBitsTest{1, 2, 2},
	reverseBitsTest{1, 4, 8},
	reverseBitsTest{2, 4, 4},
	reverseBitsTest{3, 4, 12},
}

func TestFFT(t *testing.T) {
	for _, ft := range fftTests {
		v := FFTReal(ft.in)
		if !dsputils.PrettyCloseC(v, ft.out) {
			t.Error("FFT error\ninput:", ft.in, "\noutput:", v, "\nexpected:", ft.out)
		}

		vi := IFFT(ft.out)
		if !dsputils.PrettyCloseC(vi, dsputils.ToComplex(ft.in)) {
			t.Error("IFFT error\ninput:", ft.out, "\noutput:", vi, "\nexpected:", dsputils.ToComplex(ft.in))
		}
	}
}

func TestFFT2(t *testing.T) {
	for _, ft := range fft2Tests {
		v := FFT2Real(ft.in)
		if !dsputils.PrettyClose2(v, ft.out) {
			t.Error("FFT2 error\ninput:", ft.in, "\noutput:", v, "\nexpected:", ft.out)
		}

		vi := IFFT2(ft.out)
		if !dsputils.PrettyClose2(vi, dsputils.ToComplex2(ft.in)) {
			t.Error("IFFT2 error\ninput:", ft.out, "\noutput:", vi, "\nexpected:", dsputils.ToComplex2(ft.in))
		}
	}
}

func TestFFTN(t *testing.T) {
	for _, ft := range fftnTests {
		m := dsputils.MakeMatrix(dsputils.ToComplex(ft.in), ft.dim)
		o := dsputils.MakeMatrix(ft.out, ft.dim)
		v := FFTN(m)
		if !v.PrettyClose(o) {
			t.Error("FFTN error\ninput:", m, "\noutput:", v, "\nexpected:", o)
		}

		vi := IFFTN(o)
		if !vi.PrettyClose(m) {
			t.Error("IFFTN error\ninput:", o, "\noutput:", vi, "\nexpected:", m)
		}
	}
}

func TestReverseBits(t *testing.T) {
	for _, rt := range reverseBitsTests {
		v := reverseBits(rt.in, rt.sz)

		if v != rt.out {
			t.Error("reverse bits error\ninput:", rt.in, "\nsize:", rt.sz, "\noutput:", v, "\nexpected:", rt.out)
		}
	}
}

func TestFFTMulti(t *testing.T) {
	SetWorkerPoolSize(5)

	N := 1 << 8
	a := make([]complex128, N)
	for i := 0; i < N; i++ {
		a[i] = complex(float64(i)/float64(N), 0)
	}

	FFT(a)
}

// run with: go test -test.bench="."
func BenchmarkFFT(b *testing.B) {
	b.StopTimer()

	runtime.GOMAXPROCS(6)
	SetWorkerPoolSize(6)

	N := 1 << 20
	a := make([]complex128, N)
	for i := 0; i < N; i++ {
		a[i] = complex(float64(i)/float64(N), 0)
	}

	getRadix2Factors(N)

	b.StartTimer()

	for i := 0; i < b.N; i++ {
		FFT(a)
	}
}
<commit_msg>Call the new ensure function<commit_after>/*
 * Copyright (c) 2011 Matt Jibson <matt.jibson@gmail.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

package fft

import (
	"math"
	"runtime"
	"testing"

	"github.com/mjibson/go-dsp/dsputils"
)

const (
	sqrt2_2 = math.Sqrt2 / 2
)

type fftTest struct {
	in  []float64
	out []complex128
}

var fftTests = []fftTest{
	// impulse responses
	fftTest{
		[]float64{1},
		[]complex128{complex(1, 0)},
	},
	fftTest{
		[]float64{1, 0},
		[]complex128{complex(1, 0), complex(1, 0)},
	},
	fftTest{
		[]float64{1, 0, 0, 0},
		[]complex128{complex(1, 0), complex(1, 0), complex(1, 0), complex(1, 0)},
	},
	fftTest{
		[]float64{1, 0, 0, 0, 0, 0, 0, 0},
		[]complex128{
			complex(1, 0),
			complex(1, 0),
			complex(1, 0),
			complex(1, 0),
			complex(1, 0),
			complex(1, 0),
			complex(1, 0),
			complex(1, 0)},
	},

	// shifted impulse response
	fftTest{
		[]float64{0, 1},
		[]complex128{complex(1, 0), complex(-1, 0)},
	},
	fftTest{
		[]float64{0, 1, 0, 0},
		[]complex128{complex(1, 0), complex(0, -1), complex(-1, 0), complex(0, 1)},
	},
	fftTest{
		[]float64{0, 1, 0, 0, 0, 0, 0, 0},
		[]complex128{
			complex(1, 0),
			complex(sqrt2_2, -sqrt2_2),
			complex(0, -1),
			complex(-sqrt2_2, -sqrt2_2),
			complex(-1, 0),
			complex(-sqrt2_2, sqrt2_2),
			complex(0, 1),
			complex(sqrt2_2, sqrt2_2)},
	},

	// other
	fftTest{
		[]float64{1, 2, 3, 4},
		[]complex128{
			complex(10, 0),
			complex(-2, 2),
			complex(-2, 0),
			complex(-2, -2)},
	},
	fftTest{
		[]float64{1, 3, 5, 7},
		[]complex128{
			complex(16, 0),
			complex(-4, 4),
			complex(-4, 0),
			complex(-4, -4)},
	},
	fftTest{
		[]float64{1, 2, 3, 4, 5, 6, 7, 8},
		[]complex128{
			complex(36, 0),
			complex(-4, 9.65685425),
			complex(-4, 4),
			complex(-4, 1.65685425),
			complex(-4, 0),
			complex(-4, -1.65685425),
			complex(-4, -4),
			complex(-4, -9.65685425)},
	},

	// non power of 2 lengths
	fftTest{
		[]float64{1, 0, 0, 0, 0},
		[]complex128{
			complex(1, 0),
			complex(1, 0),
			complex(1, 0),
			complex(1, 0),
			complex(1, 0)},
	},
	fftTest{
		[]float64{1, 2, 3},
		[]complex128{
			complex(6, 0),
			complex(-1.5, 0.8660254),
			complex(-1.5, -0.8660254)},
	},
	fftTest{
		[]float64{1, 1, 1},
		[]complex128{
			complex(3, 0),
			complex(0, 0),
			complex(0, 0)},
	},
}

type fft2Test struct {
	in  [][]float64
	out [][]complex128
}

var fft2Tests = []fft2Test{
	fft2Test{
		[][]float64{{1, 2, 3}, {3, 4, 5}},
		[][]complex128{
			{complex(18, 0), complex(-3, 1.73205081), complex(-3, -1.73205081)},
			{complex(-6, 0), complex(0, 0), complex(0, 0)}},
	},
	fft2Test{
		[][]float64{{0.1, 0.2, 0.3, 0.4, 0.5}, {1, 2, 3, 4, 5}, {3, 2, 1, 0, -1}},
		[][]complex128{
			{complex(21.5, 0), complex(-0.25, 0.34409548), complex(-0.25, 0.08122992), complex(-0.25, -0.08122992), complex(-0.25, -0.34409548)},
			{complex(-8.5, -8.66025404), complex(5.70990854, 4.6742225), complex(1.15694356, 4.41135694), complex(-1.65694356, 4.24889709), complex(-6.20990854, 3.98603154)},
			{complex(-8.5, 8.66025404), complex(-6.20990854, -3.98603154), complex(-1.65694356, -4.24889709), complex(1.15694356, -4.41135694), complex(5.70990854, -4.6742225)}},
	},
}

type fftnTest struct {
	in  []float64
	dim []int
	out []complex128
}

var fftnTests = []fftnTest{
	fftnTest{
		[]float64{4, 2, 3, 8, 5, 6, 7, 2, 13, 24, 13, 17},
		[]int{2, 2, 3},
		[]complex128{
			complex(104, 0), complex(12.5, 14.72243186), complex(12.5, -14.72243186),
			complex(-42, 0), complex(-10.5, 6.06217783), complex(-10.5, -6.06217783),

			complex(-48, 0), complex(-4.5, -11.25833025), complex(-4.5, 11.25833025),
			complex(22, 0), complex(8.5, -6.06217783), complex(8.5, 6.06217783)},
	},
}

type reverseBitsTest struct {
	in  uint
	sz  uint
	out uint
}

var reverseBitsTests = []reverseBitsTest{
	reverseBitsTest{0, 1, 0},
	reverseBitsTest{1, 2, 2},
	reverseBitsTest{1, 4, 8},
	reverseBitsTest{2, 4, 4},
	reverseBitsTest{3, 4, 12},
}

func TestFFT(t *testing.T) {
	for _, ft := range fftTests {
		v := FFTReal(ft.in)
		if !dsputils.PrettyCloseC(v, ft.out) {
			t.Error("FFT error\ninput:", ft.in, "\noutput:", v, "\nexpected:", ft.out)
		}

		vi := IFFT(ft.out)
		if !dsputils.PrettyCloseC(vi, dsputils.ToComplex(ft.in)) {
			t.Error("IFFT error\ninput:", ft.out, "\noutput:", vi, "\nexpected:", dsputils.ToComplex(ft.in))
		}
	}
}

func TestFFT2(t *testing.T) {
	for _, ft := range fft2Tests {
		v := FFT2Real(ft.in)
		if !dsputils.PrettyClose2(v, ft.out) {
			t.Error("FFT2 error\ninput:", ft.in, "\noutput:", v, "\nexpected:", ft.out)
		}

		vi := IFFT2(ft.out)
		if !dsputils.PrettyClose2(vi, dsputils.ToComplex2(ft.in)) {
			t.Error("IFFT2 error\ninput:", ft.out, "\noutput:", vi, "\nexpected:", dsputils.ToComplex2(ft.in))
		}
	}
}

func TestFFTN(t *testing.T) {
	for _, ft := range fftnTests {
		m := dsputils.MakeMatrix(dsputils.ToComplex(ft.in), ft.dim)
		o := dsputils.MakeMatrix(ft.out, ft.dim)
		v := FFTN(m)
		if !v.PrettyClose(o) {
			t.Error("FFTN error\ninput:", m, "\noutput:", v, "\nexpected:", o)
		}

		vi := IFFTN(o)
		if !vi.PrettyClose(m) {
			t.Error("IFFTN error\ninput:", o, "\noutput:", vi, "\nexpected:", m)
		}
	}
}

func TestReverseBits(t *testing.T) {
	for _, rt := range reverseBitsTests {
		v := reverseBits(rt.in, rt.sz)

		if v != rt.out {
			t.Error("reverse bits error\ninput:", rt.in, "\nsize:", rt.sz, "\noutput:", v, "\nexpected:", rt.out)
		}
	}
}

func TestFFTMulti(t *testing.T) {
	SetWorkerPoolSize(5)

	N := 1 << 8
	a := make([]complex128, N)
	for i := 0; i < N; i++ {
		a[i] = complex(float64(i)/float64(N), 0)
	}

	FFT(a)
}

// run with: go test -test.bench="."
func BenchmarkFFT(b *testing.B) {
	b.StopTimer()

	runtime.GOMAXPROCS(6)
	SetWorkerPoolSize(6)

	N := 1 << 20
	a := make([]complex128, N)
	for i := 0; i < N; i++ {
		a[i] = complex(float64(i)/float64(N), 0)
	}

	EnsureRadix2Factors(N)

	b.StartTimer()

	for i := 0; i < b.N; i++ {
		FFT(a)
	}
}
<|endoftext|>


<commit_before>package find

import (
	"net/http"

	"github.com/lomik/graphite-clickhouse/config"
	"github.com/lomik/graphite-clickhouse/helper/clickhouse"
)

type Handler struct {
	config *config.Config
}

func NewHandler(config *config.Config) *Handler {
	return &Handler{
		config: config,
	}
}

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	r.ParseMultipartForm(1024 * 1024)

	query := r.FormValue("query")
	if len(query) == 0 {
		http.Error(w, "Query not set", http.StatusBadRequest)
		return
	}
	f, err := New(h.config, r.Context(), query)
	if err != nil {
		clickhouse.HandleError(w, err)
		return
	}

	h.Reply(w, r, f)
}

func (h *Handler) Reply(w http.ResponseWriter, r *http.Request, f *Find) {
	switch r.FormValue("format") {
	case "pickle":
		f.WritePickle(w)
	case "protobuf":
		f.WriteProtobuf(w)
	}
}
<commit_msg>fix metric finder content type<commit_after>package find

import (
	"net/http"

	"github.com/lomik/graphite-clickhouse/config"
	"github.com/lomik/graphite-clickhouse/helper/clickhouse"
)

type Handler struct {
	config *config.Config
}

func NewHandler(config *config.Config) *Handler {
	return &Handler{
		config: config,
	}
}

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	r.ParseMultipartForm(1024 * 1024)

	query := r.FormValue("query")
	if len(query) == 0 {
		http.Error(w, "Query not set", http.StatusBadRequest)
		return
	}
	f, err := New(h.config, r.Context(), query)
	if err != nil {
		clickhouse.HandleError(w, err)
		return
	}

	h.Reply(w, r, f)
}

func (h *Handler) Reply(w http.ResponseWriter, r *http.Request, f *Find) {
	switch r.FormValue("format") {
	case "pickle":
		f.WritePickle(w)
	case "protobuf":
		w.Header().Set("Content-Type", "application/x-protobuf")
		f.WriteProtobuf(w)
	}
}
<|endoftext|>


<commit_before>package lbot

import (
	"encoding/json"
	"io/ioutil"
	"net/http"
	"time"
)

// The LINE BOT
type Bot struct {
}

// mid, message? ID
type mid string

type eventString string

// When a user sends a message, the following data is sent to your server from the LINE platform.
type content struct {

	// Identifier of the message.
	Id string `json:id`
	// A numeric value indicating the type of message sent.
	ContentType int `json:contentType`
	// MID of the user who sent the message.
	From string `json:from`
	// Time and date request created. Displayed as the amount of time passed since 0:00:00 on January 1, 1970. The unit is given in milliseconds.
	CreatedTime       int `json:createdTime`
	parsedCreatedTime *time.Time

	// Array of user who will receive the message.
	To []String
	// Type of user who will receive the message. (1: To user )
	ToType int
	// Detailed information about the message
	// ContentMetadata

	// Posted text to be delivered. Note: users can send a message which has max 10,000 characters.
	Text string

	// Location data. This property is defined if the text message sent contains location data.
	// Location

}

type Result struct {
	// Fixed value "u2ddf2eb3c959e561f6c9fa2ea732e7eb8"
	From string `json:from`
	// Fixed value "1341301815"
	FromChannel string `json:fromChannel`
	// MID value granted by the BOT API server’s Channel
	To []mid `json:to`
	// Channel ID of the BOT API server
	ToChannel string `json:toChannel`
	// Identifier used to show the type of data
	EventType eventString `json:eventType`
	// ID string to identify each event
	Id string `json:id`
	// Actual data relayed by the message
	Content []content `json:content`
}

type Request struct {
	vaild  bool
	Result []Result `json:result`
}

func ParseRequest(r *http.Request) error {
	result := Request{}
	data, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return err
	}
	json.Unmarshal(data, &result)
}

// checkSignature reports whether messageMAC is a valid HMAC tag for message.
func CheckSignature(message, messageMAC, key []byte) bool {
	mac := hmac.New(sha256.New, key)
	mac.Write(message)
	expectedMAC := mac.Sum(nil)
	return hmac.Equal(messageMAC, expectedMAC)
}
<commit_msg>add struct<commit_after>package lbot

import (
	"crypto/hmac"
	"crypto/sha256"
	"encoding/json"
	"io/ioutil"
	"net/http"
	"time"
)

// The LINE BOT
type Bot struct {
}

// mid, message? ID
type mid string

type eventString string

// When a user sends a message, the following data is sent to your server from the LINE platform.
type content struct {

	// Identifier of the message.
	Id string `json:id`
	// A numeric value indicating the type of message sent.
	ContentType int `json:contentType`
	// MID of the user who sent the message.
	From string `json:from`
	// Time and date request created. Displayed as the amount of time passed since 0:00:00 on January 1, 1970. The unit is given in milliseconds.
	CreatedTime       int `json:createdTime`
	parsedCreatedTime *time.Time

	// Array of user who will receive the message.
	To []string
	// Type of user who will receive the message. (1: To user )
	ToType int
	// Detailed information about the message
	// ContentMetadata

	// Posted text to be delivered. Note: users can send a message which has max 10,000 characters.
	Text string

	// Location data. This property is defined if the text message sent contains location data.
	// Location

}

type Result struct {
	// Fixed value "u2ddf2eb3c959e561f6c9fa2ea732e7eb8"
	From string `json:from`
	// Fixed value "1341301815"
	FromChannel string `json:fromChannel`
	// MID value granted by the BOT API server’s Channel
	To []mid `json:to`
	// Channel ID of the BOT API server
	ToChannel string `json:toChannel`
	// Identifier used to show the type of data
	EventType eventString `json:eventType`
	// ID string to identify each event
	Id string `json:id`
	// Actual data relayed by the message
	Content []content `json:content`
}

type Request struct {
	vaild  bool
	Result []Result `json:result`
}

func ParseRequest(r *http.Request) (*Request, error) {
	result := Request{}
	data, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return nil, err
	}
	json.Unmarshal(data, &result)
	return &result, nil
}

// checkSignature reports whether messageMAC is a valid HMAC tag for message.
func CheckSignature(message, messageMAC, key []byte) bool {
	mac := hmac.New(sha256.New, key)
	mac.Write(message)
	expectedMAC := mac.Sum(nil)
	return hmac.Equal(messageMAC, expectedMAC)
}
<|endoftext|>


<commit_before>package database

import (
	"github.com/go-redis/redis"
	"github.com/steffen25/golang.zone/config"
	"strconv"
)

var client *redis.Client

type RedisDB struct {
	*redis.Client
}

// Use this in the app.go's New function and add the struct type in the App struct in app.go
func NewRedisDB(dbCfg config.RedisConfig) (*RedisDB, error) {
	port := strconv.Itoa(dbCfg.Post)
	client = redis.NewClient(&redis.Options{
		Addr:     dbCfg.Host+":"+port,
		Password: "", // no password set
		DB:       0, // use default DB
	})

	_, err := client.Ping().Result()
	if err != nil {
		return nil, err
	}

	return &RedisDB{client}, err
}<commit_msg>Removed global redis client<commit_after>package database

import (
	"github.com/go-redis/redis"
	"github.com/steffen25/golang.zone/config"
	"strconv"
)

type RedisDB struct {
	*redis.Client
}

// Use this in the app.go's New function and add the struct type in the App struct in app.go
func NewRedisDB(dbCfg config.RedisConfig) (*RedisDB, error) {
	port := strconv.Itoa(dbCfg.Post)
	client := redis.NewClient(&redis.Options{
		Addr:     dbCfg.Host+":"+port,
		Password: "", // no password set
		DB:       0, // use default DB
	})

	_, err := client.Ping().Result()
	if err != nil {
		return nil, err
	}

	return &RedisDB{client}, err
}<|endoftext|>


<commit_before>package databoard

import (
	"testing"

	"github.com/google/go-github/github"
)

func TestNew(t *testing.T) {
	d := New(nil)
	d.c = (*github.Client)(nil)
	d.Token = ""
	d.Owner = ""
	d.Repo = ""
}
<commit_msg>[test] add Databoard.GetLatestRelease()<commit_after>package databoard

import (
	"context"
	"os"
	"testing"

	"github.com/google/go-github/github"
)

func TestNew(t *testing.T) {
	d := New(nil)
	d.c = (*github.Client)(nil)
	d.Token = ""
	d.Owner = ""
	d.Repo = ""
}

func initFromEnv(t *testing.T, d *Databoard) (*Databoard, bool) {
	d.Token = os.Getenv("GITHUB_TOKEN")
	d.Owner = os.Getenv("GITHUB_OWNER")
	d.Repo = os.Getenv("GITHUB_REPO")

	if d.Token == "" || d.Owner == "" || d.Repo == "" {
		t.Error("expect $GITHUB_TOKEN,$GITHUB_OWNER,$GITHUB_REPO")
		return nil, false
	}
	return d, true
}
func TestDataboard_GetLateRelease(t *testing.T) {
	d, ok := initFromEnv(t, New(github.NewClient(nil)))
	if !ok {
		return
	}
	_, err := d.GetLatestRelease(context.TODO())
	if err != nil {
		t.Fatal(err)
	}
}
<|endoftext|>


<commit_before>// Copyright (C) 2017 ScyllaDB
// Use of this source code is governed by a ALv2-style
// license that can be found in the LICENSE file.

package dbutil

import (
	"github.com/scylladb/gocqlx/v2"
	"github.com/scylladb/gocqlx/v2/table"
)

// RewriteTable rewrites src table to dst table.
// Rows can be transformed using the transform function.
// Additional options can be passed to modify the insert query.
func RewriteTable(session gocqlx.Session, dst, src *table.Table, transform func(map[string]interface{}), options ...func(q *gocqlx.Queryx)) error {
	insert := dst.InsertQuery(session)
	defer insert.Release()

	// Apply query options
	for _, o := range options {
		o(insert)
	}

	// Iterate over all rows and reinsert them to dst table
	iter := session.Query(src.SelectAll()).Iter()
	m := make(map[string]interface{})
	for iter.MapScan(m) {
		if transform != nil {
			transform(m)
		}
		if err := insert.BindMap(m).Exec(); err != nil {
			return err
		}
		m = map[string]interface{}{}
	}
	return iter.Close()
}
<commit_msg>dbutil: RewriteTable add possibility to skip rows<commit_after>// Copyright (C) 2017 ScyllaDB
// Use of this source code is governed by a ALv2-style
// license that can be found in the LICENSE file.

package dbutil

import (
	"github.com/scylladb/gocqlx/v2"
	"github.com/scylladb/gocqlx/v2/table"
)

// RewriteTable rewrites src table to dst table.
// Rows can be transformed using the transform function.
// If row map is empty after transformation the row is skipped.
// Additional options can be passed to modify the insert query.
func RewriteTable(session gocqlx.Session, dst, src *table.Table, transform func(map[string]interface{}), options ...func(q *gocqlx.Queryx)) error {
	insert := dst.InsertQuery(session)
	defer insert.Release()

	// Apply query options
	for _, o := range options {
		o(insert)
	}

	// Iterate over all rows and reinsert them to dst table
	iter := session.Query(src.SelectAll()).Iter()
	m := make(map[string]interface{})
	for iter.MapScan(m) {
		if transform != nil {
			transform(m)
		}
		if len(m) == 0 {
			continue // map is empty - no need to clean
		}
		if err := insert.BindMap(m).Exec(); err != nil {
			return err
		}
		m = map[string]interface{}{}
	}
	return iter.Close()
}
<|endoftext|>