<commit_before>/*
Copyright The Helm Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"strconv"
	"strings"
	"syscall"

	"github.com/pkg/errors"
	"github.com/spf13/cobra"
	"sigs.k8s.io/yaml"

	"helm.sh/helm/v3/internal/completion"
	"helm.sh/helm/v3/pkg/plugin"
)

const (
	pluginStaticCompletionFile        = "completion.yaml"
	pluginDynamicCompletionExecutable = "plugin.complete"
)

type pluginError struct {
	error
	code int
}

// loadPlugins loads plugins into the command list.
//
// This follows a different pattern than the other commands because it has
// to inspect its environment and then add commands to the base command
// as it finds them.
func loadPlugins(baseCmd *cobra.Command, out io.Writer) {

	// If HELM_NO_PLUGINS is set to 1, do not load plugins.
	if os.Getenv("HELM_NO_PLUGINS") == "1" {
		return
	}

	found, err := plugin.FindPlugins(settings.PluginsDirectory)
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to load plugins: %s", err)
		return
	}

	processParent := func(cmd *cobra.Command, args []string) ([]string, error) {
		k, u := manuallyProcessArgs(args)
		if err := cmd.Parent().ParseFlags(k); err != nil {
			return nil, err
		}
		return u, nil
	}

	// If we are dealing with the completion command, we try to load more details about the plugins
	// if available, so as to allow for command and flag completion
	if subCmd, _, err := baseCmd.Find(os.Args[1:]); err == nil && subCmd.Name() == "completion" {
		loadPluginsForCompletion(baseCmd, found)
		return
	}

	// Now we create commands for all of these.
	for _, plug := range found {
		plug := plug
		md := plug.Metadata
		if md.Usage == "" {
			md.Usage = fmt.Sprintf("the %q plugin", md.Name)
		}

		// This function is used to setup the environment for the plugin and then
		// call the executable specified by the parameter 'main'
		callPluginExecutable := func(cmd *cobra.Command, main string, argv []string, out io.Writer) error {
			env := os.Environ()
			for k, v := range settings.EnvVars() {
				env = append(env, fmt.Sprintf("%s=%s", k, v))
			}

			prog := exec.Command(main, argv...)
			prog.Env = env
			prog.Stdin = os.Stdin
			prog.Stdout = out
			prog.Stderr = os.Stderr
			if err := prog.Run(); err != nil {
				if eerr, ok := err.(*exec.ExitError); ok {
					os.Stderr.Write(eerr.Stderr)
					status := eerr.Sys().(syscall.WaitStatus)
					return pluginError{
						error: errors.Errorf("plugin %q exited with error", md.Name),
						code:  status.ExitStatus(),
					}
				}
				return err
			}
			return nil
		}

		c := &cobra.Command{
			Use:   md.Name,
			Short: md.Usage,
			Long:  md.Description,
			RunE: func(cmd *cobra.Command, args []string) error {
				u, err := processParent(cmd, args)
				if err != nil {
					return err
				}

				// Call setupEnv before PrepareCommand because
				// PrepareCommand uses os.ExpandEnv and expects the
				// setupEnv vars.
				plugin.SetupPluginEnv(settings, md.Name, plug.Dir)
				main, argv, prepCmdErr := plug.PrepareCommand(u)
				if prepCmdErr != nil {
					os.Stderr.WriteString(prepCmdErr.Error())
					return errors.Errorf("plugin %q exited with error", md.Name)
				}

				return callPluginExecutable(cmd, main, argv, out)
			},
			// This passes all the flags to the subcommand.
			DisableFlagParsing: true,
		}

		// Setup dynamic completion for the plugin
		completion.RegisterValidArgsFunc(c, func(cmd *cobra.Command, args []string, toComplete string) ([]string, completion.BashCompDirective) {
			u, err := processParent(cmd, args)
			if err != nil {
				return nil, completion.BashCompDirectiveError
			}

			// We will call the dynamic completion script of the plugin
			main := strings.Join([]string{plug.Dir, pluginDynamicCompletionExecutable}, string(filepath.Separator))

			argv := []string{}
			if !md.IgnoreFlags {
				argv = append(argv, u...)
				argv = append(argv, toComplete)
			}
			plugin.SetupPluginEnv(settings, md.Name, plug.Dir)

			completion.CompDebugln(fmt.Sprintf("calling %s with args %v", main, argv))
			buf := new(bytes.Buffer)
			if err := callPluginExecutable(cmd, main, argv, buf); err != nil {
				return nil, completion.BashCompDirectiveError
			}

			var completions []string
			for _, comp := range strings.Split(buf.String(), "\n") {
				// Remove any empty lines
				if len(comp) > 0 {
					completions = append(completions, comp)
				}
			}

			// Check if the last line of output is of the form :<integer>, which
			// indicates the BashCompletionDirective.
			directive := completion.BashCompDirectiveDefault
			if len(completions) > 0 {
				lastLine := completions[len(completions)-1]
				if len(lastLine) > 1 && lastLine[0] == ':' {
					if strInt, err := strconv.Atoi(lastLine[1:]); err == nil {
						directive = completion.BashCompDirective(strInt)
						completions = completions[:len(completions)-1]
					}
				}
			}

			return completions, directive
		})

		// TODO: Make sure a command with this name does not already exist.
		baseCmd.AddCommand(c)
	}
}

// manuallyProcessArgs processes an arg array, removing special args.
//
// Returns two sets of args: known and unknown (in that order)
func manuallyProcessArgs(args []string) ([]string, []string) {
	known := []string{}
	unknown := []string{}
	kvargs := []string{"--kube-context", "--namespace", "-n", "--kubeconfig", "--kube-apiserver", "--kube-token", "--registry-config", "--repository-cache", "--repository-config"}
	knownArg := func(a string) bool {
		for _, pre := range kvargs {
			if strings.HasPrefix(a, pre+"=") {
				return true
			}
		}
		return false
	}

	isKnown := func(v string) string {
		for _, i := range kvargs {
			if i == v {
				return v
			}
		}
		return ""
	}

	for i := 0; i < len(args); i++ {
		switch a := args[i]; a {
		case "--debug":
			known = append(known, a)
		case isKnown(a):
			known = append(known, a)
			i++
			if i < len(args) {
				known = append(known, args[i])
			}
		default:
			if knownArg(a) {
				known = append(known, a)
				continue
			}
			unknown = append(unknown, a)
		}
	}
	return known, unknown
}

// pluginCommand represents the optional completion.yaml file of a plugin
type pluginCommand struct {
	Name      string          `json:"name"`
	ValidArgs []string        `json:"validArgs"`
	Flags     []string        `json:"flags"`
	Commands  []pluginCommand `json:"commands"`
}

// loadPluginsForCompletion will load and parse any completion.yaml provided by the plugins
func loadPluginsForCompletion(baseCmd *cobra.Command, plugins []*plugin.Plugin) {
	for _, plug := range plugins {
		// Parse the yaml file providing the plugin's subcmds and flags
		cmds, err := loadFile(strings.Join(
			[]string{plug.Dir, pluginStaticCompletionFile}, string(filepath.Separator)))

		if err != nil {
			// The file could be missing or invalid.  Either way, we at least create the command
			// for the plugin name.
			if settings.Debug {
				log.Output(2, fmt.Sprintf("[info] %s\n", err.Error()))
			}
			cmds = &pluginCommand{Name: plug.Metadata.Name}
		}

		// We know what the plugin name must be.
		// Let's set it in case the Name field was not specified correctly in the file.
		// This insures that we will at least get the plugin name to complete, even if
		// there is a problem with the completion.yaml file
		cmds.Name = plug.Metadata.Name

		addPluginCommands(baseCmd, cmds)
	}
}

// addPluginCommands is a recursive method that adds the different levels
// of sub-commands and flags for the plugins that provide such information
func addPluginCommands(baseCmd *cobra.Command, cmds *pluginCommand) {
	if cmds == nil {
		return
	}

	if len(cmds.Name) == 0 {
		// Missing name for a command
		if settings.Debug {
			log.Output(2, fmt.Sprintf("[info] sub-command name field missing for %s", baseCmd.CommandPath()))
		}
		return
	}

	// Create a fake command just so the completion script will include it
	c := &cobra.Command{
		Use:       cmds.Name,
		ValidArgs: cmds.ValidArgs,
		// A Run is required for it to be a valid command without subcommands
		Run: func(cmd *cobra.Command, args []string) {},
	}
	baseCmd.AddCommand(c)

	// Create fake flags.
	if len(cmds.Flags) > 0 {
		// The flags can be created with any type, since we only need them for completion.
		// pflag does not allow to create short flags without a corresponding long form
		// so we look for all short flags and match them to any long flag.  This will allow
		// plugins to provide short flags without a long form.
		// If there are more short-flags than long ones, we'll create an extra long flag with
		// the same single letter as the short form.
		shorts := []string{}
		longs := []string{}
		for _, flag := range cmds.Flags {
			if len(flag) == 1 {
				shorts = append(shorts, flag)
			} else {
				longs = append(longs, flag)
			}
		}

		f := c.Flags()
		if len(longs) >= len(shorts) {
			for i := range longs {
				if i < len(shorts) {
					f.BoolP(longs[i], shorts[i], false, "")
				} else {
					f.Bool(longs[i], false, "")
				}
			}
		} else {
			for i := range shorts {
				if i < len(longs) {
					f.BoolP(longs[i], shorts[i], false, "")
				} else {
					// Create a long flag with the same name as the short flag.
					// Not a perfect solution, but its better than ignoring the extra short flags.
					f.BoolP(shorts[i], shorts[i], false, "")
				}
			}
		}
	}

	// Recursively add any sub-commands
	for _, cmd := range cmds.Commands {
		addPluginCommands(c, &cmd)
	}
}

// loadFile takes a yaml file at the given path, parses it and returns a pluginCommand object
func loadFile(path string) (*pluginCommand, error) {
	cmds := new(pluginCommand)
	b, err := ioutil.ReadFile(path)
	if err != nil {
		return cmds, errors.New(fmt.Sprintf("File (%s) not provided by plugin. No plugin auto-completion possible.", path))
	}

	err = yaml.Unmarshal(b, cmds)
	return cmds, err
}
<commit_msg>fix(comp): Prepare plugin completion for Cobra 1.0<commit_after>/*
Copyright The Helm Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"strconv"
	"strings"
	"syscall"

	"github.com/pkg/errors"
	"github.com/spf13/cobra"
	"sigs.k8s.io/yaml"

	"helm.sh/helm/v3/internal/completion"
	"helm.sh/helm/v3/pkg/plugin"
)

const (
	pluginStaticCompletionFile        = "completion.yaml"
	pluginDynamicCompletionExecutable = "plugin.complete"
)

type pluginError struct {
	error
	code int
}

// loadPlugins loads plugins into the command list.
//
// This follows a different pattern than the other commands because it has
// to inspect its environment and then add commands to the base command
// as it finds them.
func loadPlugins(baseCmd *cobra.Command, out io.Writer) {

	// If HELM_NO_PLUGINS is set to 1, do not load plugins.
	if os.Getenv("HELM_NO_PLUGINS") == "1" {
		return
	}

	found, err := plugin.FindPlugins(settings.PluginsDirectory)
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to load plugins: %s", err)
		return
	}

	// Now we create commands for all of these.
	for _, plug := range found {
		plug := plug
		md := plug.Metadata
		if md.Usage == "" {
			md.Usage = fmt.Sprintf("the %q plugin", md.Name)
		}

		c := &cobra.Command{
			Use:   md.Name,
			Short: md.Usage,
			Long:  md.Description,
			RunE: func(cmd *cobra.Command, args []string) error {
				u, err := processParent(cmd, args)
				if err != nil {
					return err
				}

				// Call setupEnv before PrepareCommand because
				// PrepareCommand uses os.ExpandEnv and expects the
				// setupEnv vars.
				plugin.SetupPluginEnv(settings, md.Name, plug.Dir)
				main, argv, prepCmdErr := plug.PrepareCommand(u)
				if prepCmdErr != nil {
					os.Stderr.WriteString(prepCmdErr.Error())
					return errors.Errorf("plugin %q exited with error", md.Name)
				}

				return callPluginExecutable(md.Name, main, argv, out)
			},
			// This passes all the flags to the subcommand.
			DisableFlagParsing: true,
		}
		// TODO: Make sure a command with this name does not already exist.
		baseCmd.AddCommand(c)

		// For completion, we try to load more details about the plugins so as to allow for command and
		// flag completion of the plugin itself.
		// We only do this when necessary (for the "completion" and "__complete" commands) to avoid the
		// risk of a rogue plugin affecting Helm's normal behavior.
		subCmd, _, err := baseCmd.Find(os.Args[1:])
		if (err == nil && (subCmd.Name() == "completion" || subCmd.Name() == completion.CompRequestCmd)) ||
			/* for the tests */ subCmd == baseCmd.Root() {
			loadCompletionForPlugin(c, plug)
		}
	}
}

func processParent(cmd *cobra.Command, args []string) ([]string, error) {
	k, u := manuallyProcessArgs(args)
	if err := cmd.Parent().ParseFlags(k); err != nil {
		return nil, err
	}
	return u, nil
}

// This function is used to setup the environment for the plugin and then
// call the executable specified by the parameter 'main'
func callPluginExecutable(pluginName string, main string, argv []string, out io.Writer) error {
	env := os.Environ()
	for k, v := range settings.EnvVars() {
		env = append(env, fmt.Sprintf("%s=%s", k, v))
	}

	prog := exec.Command(main, argv...)
	prog.Env = env
	prog.Stdin = os.Stdin
	prog.Stdout = out
	prog.Stderr = os.Stderr
	if err := prog.Run(); err != nil {
		if eerr, ok := err.(*exec.ExitError); ok {
			os.Stderr.Write(eerr.Stderr)
			status := eerr.Sys().(syscall.WaitStatus)
			return pluginError{
				error: errors.Errorf("plugin %q exited with error", pluginName),
				code:  status.ExitStatus(),
			}
		}
		return err
	}
	return nil
}

// manuallyProcessArgs processes an arg array, removing special args.
//
// Returns two sets of args: known and unknown (in that order)
func manuallyProcessArgs(args []string) ([]string, []string) {
	known := []string{}
	unknown := []string{}
	kvargs := []string{"--kube-context", "--namespace", "-n", "--kubeconfig", "--kube-apiserver", "--kube-token", "--registry-config", "--repository-cache", "--repository-config"}
	knownArg := func(a string) bool {
		for _, pre := range kvargs {
			if strings.HasPrefix(a, pre+"=") {
				return true
			}
		}
		return false
	}

	isKnown := func(v string) string {
		for _, i := range kvargs {
			if i == v {
				return v
			}
		}
		return ""
	}

	for i := 0; i < len(args); i++ {
		switch a := args[i]; a {
		case "--debug":
			known = append(known, a)
		case isKnown(a):
			known = append(known, a)
			i++
			if i < len(args) {
				known = append(known, args[i])
			}
		default:
			if knownArg(a) {
				known = append(known, a)
				continue
			}
			unknown = append(unknown, a)
		}
	}
	return known, unknown
}

// pluginCommand represents the optional completion.yaml file of a plugin
type pluginCommand struct {
	Name      string          `json:"name"`
	ValidArgs []string        `json:"validArgs"`
	Flags     []string        `json:"flags"`
	Commands  []pluginCommand `json:"commands"`
}

// loadCompletionForPlugin will load and parse any completion.yaml provided by the plugin
// and add the dynamic completion hook to call the optional plugin.complete
func loadCompletionForPlugin(pluginCmd *cobra.Command, plugin *plugin.Plugin) {
	// Parse the yaml file providing the plugin's sub-commands and flags
	cmds, err := loadFile(strings.Join(
		[]string{plugin.Dir, pluginStaticCompletionFile}, string(filepath.Separator)))

	if err != nil {
		// The file could be missing or invalid.  No static completion for this plugin.
		if settings.Debug {
			log.Output(2, fmt.Sprintf("[info] %s\n", err.Error()))
		}
		// Continue to setup dynamic completion.
		cmds = &pluginCommand{}
	}

	// Preserve the Usage string specified for the plugin
	cmds.Name = pluginCmd.Use

	addPluginCommands(plugin, pluginCmd, cmds)
}

// addPluginCommands is a recursive method that adds each different level
// of sub-commands and flags for the plugins that have provided such information
func addPluginCommands(plugin *plugin.Plugin, baseCmd *cobra.Command, cmds *pluginCommand) {
	if cmds == nil {
		return
	}

	if len(cmds.Name) == 0 {
		// Missing name for a command
		if settings.Debug {
			log.Output(2, fmt.Sprintf("[info] sub-command name field missing for %s", baseCmd.CommandPath()))
		}
		return
	}

	baseCmd.Use = cmds.Name
	baseCmd.ValidArgs = cmds.ValidArgs
	// Setup the same dynamic completion for each plugin sub-command.
	// This is because if dynamic completion is triggered, there is a single executable
	// to call (plugin.complete), so every sub-commands calls it in the same fashion.
	if cmds.Commands == nil {
		// Only setup dynamic completion if there are no sub-commands.  This avoids
		// calling plugin.complete at every completion, which greatly simplifies
		// development of plugin.complete for plugin developers.
		completion.RegisterValidArgsFunc(baseCmd, func(cmd *cobra.Command, args []string, toComplete string) ([]string, completion.BashCompDirective) {
			return pluginDynamicComp(plugin, cmd, args, toComplete)
		})
	}

	// Create fake flags.
	if len(cmds.Flags) > 0 {
		// The flags can be created with any type, since we only need them for completion.
		// pflag does not allow to create short flags without a corresponding long form
		// so we look for all short flags and match them to any long flag.  This will allow
		// plugins to provide short flags without a long form.
		// If there are more short-flags than long ones, we'll create an extra long flag with
		// the same single letter as the short form.
		shorts := []string{}
		longs := []string{}
		for _, flag := range cmds.Flags {
			if len(flag) == 1 {
				shorts = append(shorts, flag)
			} else {
				longs = append(longs, flag)
			}
		}

		f := baseCmd.Flags()
		if len(longs) >= len(shorts) {
			for i := range longs {
				if i < len(shorts) {
					f.BoolP(longs[i], shorts[i], false, "")
				} else {
					f.Bool(longs[i], false, "")
				}
			}
		} else {
			for i := range shorts {
				if i < len(longs) {
					f.BoolP(longs[i], shorts[i], false, "")
				} else {
					// Create a long flag with the same name as the short flag.
					// Not a perfect solution, but its better than ignoring the extra short flags.
					f.BoolP(shorts[i], shorts[i], false, "")
				}
			}
		}
	}

	// Recursively add any sub-commands
	for _, cmd := range cmds.Commands {
		// Create a fake command so that completion can be done for the sub-commands of the plugin
		subCmd := &cobra.Command{
			// This prevents Cobra from removing the flags.  We want to keep the flags to pass them
			// to the dynamic completion script of the plugin.
			DisableFlagParsing: true,
			// A Run is required for it to be a valid command without subcommands
			Run: func(cmd *cobra.Command, args []string) {},
		}
		baseCmd.AddCommand(subCmd)
		addPluginCommands(plugin, subCmd, &cmd)
	}
}

// loadFile takes a yaml file at the given path, parses it and returns a pluginCommand object
func loadFile(path string) (*pluginCommand, error) {
	cmds := new(pluginCommand)
	b, err := ioutil.ReadFile(path)
	if err != nil {
		return cmds, errors.New(fmt.Sprintf("File (%s) not provided by plugin. No plugin auto-completion possible.", path))
	}

	err = yaml.Unmarshal(b, cmds)
	return cmds, err
}

// pluginDynamicComp call the plugin.complete script of the plugin (if available)
// to obtain the dynamic completion choices.  It must pass all the flags and sub-commands
// specified in the command-line to the plugin.complete executable (except helm's global flags)
func pluginDynamicComp(plug *plugin.Plugin, cmd *cobra.Command, args []string, toComplete string) ([]string, completion.BashCompDirective) {
	md := plug.Metadata

	u, err := processParent(cmd, args)
	if err != nil {
		return nil, completion.BashCompDirectiveError
	}

	// We will call the dynamic completion script of the plugin
	main := strings.Join([]string{plug.Dir, pluginDynamicCompletionExecutable}, string(filepath.Separator))

	// We must include all sub-commands passed on the command-line.
	// To do that, we pass-in the entire CommandPath, except the first two elements
	// which are 'helm' and 'pluginName'.
	argv := strings.Split(cmd.CommandPath(), " ")[2:]
	if !md.IgnoreFlags {
		argv = append(argv, u...)
		argv = append(argv, toComplete)
	}
	plugin.SetupPluginEnv(settings, md.Name, plug.Dir)

	completion.CompDebugln(fmt.Sprintf("calling %s with args %v", main, argv))
	buf := new(bytes.Buffer)
	if err := callPluginExecutable(md.Name, main, argv, buf); err != nil {
		// The dynamic completion file is optional for a plugin, so this error is ok.
		completion.CompDebugln(fmt.Sprintf("Unable to call %s: %v", main, err.Error()))
		return nil, completion.BashCompDirectiveDefault
	}

	var completions []string
	for _, comp := range strings.Split(buf.String(), "\n") {
		// Remove any empty lines
		if len(comp) > 0 {
			completions = append(completions, comp)
		}
	}

	// Check if the last line of output is of the form :<integer>, which
	// indicates the BashCompletionDirective.
	directive := completion.BashCompDirectiveDefault
	if len(completions) > 0 {
		lastLine := completions[len(completions)-1]
		if len(lastLine) > 1 && lastLine[0] == ':' {
			if strInt, err := strconv.Atoi(lastLine[1:]); err == nil {
				directive = completion.BashCompDirective(strInt)
				completions = completions[:len(completions)-1]
			}
		}
	}

	return completions, directive
}
<|endoftext|>


<commit_before>package main

import (
	"bytes"
	"flag"
	"io/ioutil"
	"log"
	"net"
	"net/http"
	"time"

	"github.com/funkygao/golib/stress"
)

func main() {
	flag.Parse()

	http.DefaultClient.Timeout = time.Second * 30
	stress.RunStress(pubLoop)
}

func pubLoop(seq int) {
	timeout := 3 * time.Second
	httpClient := &http.Client{
		Transport: &http.Transport{
			Proxy: http.ProxyFromEnvironment,
			Dial: (&net.Dialer{
				Timeout:   timeout,
				KeepAlive: 60 * time.Second,
			}).Dial,
			TLSHandshakeTimeout: timeout,
		},
	}

	for i := 0; i < 1000; i++ {
		pub(seq, i, httpClient)
	}
}

func pub(seq int, n int, httpClient *http.Client) {
	req, err := http.NewRequest("POST",
		"http://localhost:9090/v1/topics/foobar?ack=2&timeout=1&retry=3",
		bytes.NewBuffer([]byte("m=hello world")))
	if err != nil {
		log.Fatalf("Error Occured. %+v", err)
		stress.IncCounter("fail", 1)
		return
	}
	req.Header.Set("Pubkey", "mypubkey")
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

	// use httpClient to send request
	response, err := httpClient.Do(req)
	if err != nil && response == nil {
		stress.IncCounter("fail", 1)
		log.Printf("Error sending request to API endpoint. %+v\n", err)
	} else {
		// Close the connection to reuse it
		defer response.Body.Close()

		if response.StatusCode != http.StatusOK {
			stress.IncCounter("fail", 1)
			return
		}

		// Let's check if the work actually is done
		// We have seen inconsistencies even when we get 200 OK response
		body, err := ioutil.ReadAll(response.Body)
		if err != nil {
			log.Fatalf("Couldn't parse response body. %+v", err)
		}

		stress.IncCounter("success", 1)

		if false {
			log.Println("Response Body:", string(body))
		}
	}

}
<commit_msg>compare direct kafka client and gateway benchmark<commit_after>package main

import (
	"bytes"
	"flag"
	"io/ioutil"
	"log"
	"net"
	"net/http"
	"time"

	"github.com/Shopify/sarama"
	"github.com/funkygao/golib/stress"
)

var (
	mode  string
	loops int
)

func main() {
	flag.IntVar(&loops, "loops", 1000, "loops in each thread")
	flag.StringVar(&mode, "mode", "gw", "<gw|kafka>")
	flag.Parse()

	switch mode {
	case "gw":
		http.DefaultClient.Timeout = time.Second * 30
		stress.RunStress(pubGatewayLoop)

	case "kafka":
		stress.RunStress(pubKafkaLoop)
	}

}

func pubKafkaLoop(seq int) {
	cf := sarama.NewConfig()
	cf.Producer.RequiredAcks = sarama.WaitForLocal
	cf.Producer.Partitioner = sarama.NewHashPartitioner
	cf.Producer.Timeout = time.Second
	//cf.Producer.Compression = sarama.CompressionSnappy
	cf.Producer.Retry.Max = 3
	producer, err := sarama.NewSyncProducer([]string{"localhost:9092"}, cf)
	if err != nil {
		stress.IncCounter("fail", 1)
		log.Println(err)
		return
	}

	defer producer.Close()
	msg := "hello world"
	for i := 0; i < loops; i++ {
		_, _, err := producer.SendMessage(&sarama.ProducerMessage{
			Topic: "foobar",
			Value: sarama.StringEncoder(msg),
		})
		if err == nil {
			stress.IncCounter("ok", 1)
		} else {
			stress.IncCounter("fail", 1)
		}
	}

}

func pubGatewayLoop(seq int) {
	timeout := 3 * time.Second
	httpClient := &http.Client{
		Transport: &http.Transport{
			Proxy: http.ProxyFromEnvironment,
			Dial: (&net.Dialer{
				Timeout:   timeout,
				KeepAlive: 60 * time.Second,
			}).Dial,
			TLSHandshakeTimeout: timeout,
		},
	}

	for i := 0; i < loops; i++ {
		pubGateway(seq, i, httpClient)
	}
}

func pubGateway(seq int, n int, httpClient *http.Client) {
	req, err := http.NewRequest("POST",
		"http://localhost:9090/v1/topics/foobar?ack=2&timeout=1&retry=3",
		bytes.NewBuffer([]byte("m=hello world")))
	if err != nil {
		log.Fatalf("Error Occured. %+v", err)
		stress.IncCounter("fail", 1)
		return
	}
	req.Header.Set("Pubkey", "mypubkey")
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

	// use httpClient to send request
	response, err := httpClient.Do(req)
	if err != nil && response == nil {
		stress.IncCounter("fail", 1)
		log.Printf("Error sending request to API endpoint. %+v", err)
	} else {
		// Close the connection to reuse it
		defer response.Body.Close()

		if response.StatusCode != http.StatusOK {
			stress.IncCounter("fail", 1)
			return
		}

		// Let's check if the work actually is done
		// We have seen inconsistencies even when we get 200 OK response
		body, err := ioutil.ReadAll(response.Body)
		if err != nil {
			log.Fatalf("Couldn't parse response body. %+v", err)
		}

		stress.IncCounter("ok", 1)

		if false {
			log.Println("Response Body:", string(body))
		}
	}

}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"math/rand"
	"net/http"
	"strconv"
	"sync"
	"sync/atomic"
)

func main() {
	var wg sync.WaitGroup
	total := 1000 * 1000
	workers := 100
	wg.Add(total)
	sem := make(chan int, workers)
	cnt := int64(total)
	log.Println("Allocating")
	for _, i := range rand.Perm(total) {
		go setPixel(i, sem, &cnt, &wg)
	}
	log.Println("Allocated, waiting")
	wg.Wait()
}

func setPixel(i int, sem chan int, cnt *int64, wg *sync.WaitGroup) {
	c := &http.Client{}
	defer wg.Done()
	sem <- 1
	r, err := c.Get(fmt.Sprintf("http://127.0.0.1:8080/write?x=%v&y=%v&c=%v&u=%v", i%1000, i/1000, rand.Int()%4, "user"+strconv.Itoa(rand.Int()%100000)))
	<-sem
	if err != nil {
		log.Printf("Error %v", err)
		return
	}
	io.Copy(ioutil.Discard, r.Body)
	r.Body.Close()
	atomic.AddInt64(cnt, -1)
	if *cnt%10000 == 0 {
		log.Println(*cnt)
	}
}
<commit_msg>Longer test<commit_after>package main

import (
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"math/rand"
	"net/http"
	"strconv"
	"sync"
	"sync/atomic"
	"time"
)

func main() {
	start := time.Now()
	var wg sync.WaitGroup
	total := 1000 * 1000 * 5
	workers := 100
	wg.Add(total)
	sem := make(chan int, workers)
	cnt := int64(total)
	c := &http.Client{Transport: &http.Transport{
		MaxIdleConnsPerHost: workers,
	}}
	log.Println("Allocating")
	for _, i := range rand.Perm(total) {
		go setPixel(i, sem, &cnt, &wg, c)
	}
	log.Println("Allocated, waiting")
	wg.Wait()
	end := time.Now()
	log.Printf("%v writes in %v using %v workers, rate %v/s", total, end.Sub(start), workers, float64(total)/end.Sub(start).Seconds())
}

func setPixel(i int, sem chan int, cnt *int64, wg *sync.WaitGroup, c *http.Client) {
	defer wg.Done()
	sem <- 1
	r, err := c.Get(fmt.Sprintf("http://127.0.0.1:8080/write?x=%v&y=%v&c=%v&u=%v", i%1000, (i/1000)%1000, rand.Int()%4, "user"+strconv.Itoa(rand.Int()%100000)))
	<-sem
	if err != nil {
		log.Printf("Error %v", err)
		return
	}
	defer func() {
		io.Copy(ioutil.Discard, r.Body)
		r.Body.Close()
	}()
	atomic.AddInt64(cnt, -1)
	if *cnt%10000 == 0 {
		log.Println(*cnt)
	}
}
<|endoftext|>


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

import (
	"crypto/rand"
	"crypto/rsa"
	"crypto/x509"
	"encoding/pem"
	"golang.org/x/crypto/ssh"
	"io/ioutil"
	"net/http"
	"os"
	"path/filepath"
	"strings"

	"github.com/fabric8io/gofabric8/client"
	"github.com/fabric8io/gofabric8/util"
	oclient "github.com/openshift/origin/pkg/client"
	tapi "github.com/openshift/origin/pkg/template/api"
	"github.com/spf13/cobra"
	flag "github.com/spf13/pflag"
	"k8s.io/kubernetes/pkg/api"
	k8sclient "k8s.io/kubernetes/pkg/client"
	"k8s.io/kubernetes/pkg/fields"
	cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
	"k8s.io/kubernetes/pkg/labels"
	"k8s.io/kubernetes/pkg/runtime"
)

type Keypair struct {
	pub  []byte
	priv []byte
}

func NewCmdSecrets(f *cmdutil.Factory) *cobra.Command {
	cmd := &cobra.Command{
		Use:   "secrets",
		Short: "Set up Secrets on your Kubernetes or OpenShift environment",
		Long:  `set up Secrets on your Kubernetes or OpenShift environment`,
		PreRun: func(cmd *cobra.Command, args []string) {
			showBanner()
		},
		Run: func(cmd *cobra.Command, args []string) {
			c, cfg := client.NewClient(f)
			ns, _, _ := f.DefaultNamespace()
			util.Info("Setting up secrets on your ")
			util.Success(string(util.TypeOfMaster(c)))
			util.Info(" installation at ")
			util.Success(cfg.Host)
			util.Info(" in namespace ")
			util.Successf("%s\n\n", ns)

			if confirmAction(cmd.Flags()) {
				typeOfMaster := util.TypeOfMaster(c)

				if typeOfMaster == util.Kubernetes {
					util.Fatal("Support for Kubernetes not yet available...\n")
				} else {
					oc, _ := client.NewOpenShiftClient(cfg)
					t := getTemplates(oc, ns)

					count := 0
					// get all the Templates and find the annotations on any Pods
					for _, i := range t.Items {
						// convert TemplateList.Objects to Kubernetes resources
						_ = runtime.DecodeList(i.Objects, api.Scheme, runtime.UnstructuredJSONScheme)
						for _, rc := range i.Objects {
							switch rc := rc.(type) {
							case *api.ReplicationController:
								for secretType, secretDataIdentifiers := range rc.Spec.Template.Annotations {
									count += createAndPrintSecrets(secretDataIdentifiers, secretType, c, f, cmd.Flags())
								}
							}
						}
					}

					if count == 0 {
						util.Info("No secrets created as no fabric8 secrets annotations found in the templates\n")
						util.Info("For more details see: https://github.com/fabric8io/fabric8/blob/master/docs/secretAnnotations.md\n")
					}
				}
			}
		},
	}
	cmd.PersistentFlags().BoolP("print-import-folder-structure", "", true, "Prints the folder structures that are being used by the template annotations to import secrets")
	cmd.PersistentFlags().BoolP("write-generated-keys", "", false, "Write generated secrets to the local filesystem")
	cmd.PersistentFlags().BoolP("generate-secrets-data", "g", true, "Generate secrets data if secrets cannot be found to import from the local filesystem")
	return cmd
}

func createSecret(c *k8sclient.Client, f *cmdutil.Factory, flags *flag.FlagSet, secretDataIdentifiers string, secretType string, keysNames []string) (Result, error) {
	var secret = secret(secretDataIdentifiers, secretType, keysNames, flags)
	ns, _, err := f.DefaultNamespace()
	if err != nil {
		return Failure, err
	}
	rs, err := c.Secrets(ns).Create(&secret)
	if rs != nil {
		return Success, err
	}
	return Failure, err
}

func createAndPrintSecrets(secretDataIdentifiers string, secretType string, c *k8sclient.Client, fa *cmdutil.Factory, flags *flag.FlagSet) int {
	count := 0
	// check to see if multiple public and private keys are needed
	var dataType = strings.Split(secretType, "/")
	switch dataType[1] {
	case "secret-ssh-key":
		items := strings.Split(secretDataIdentifiers, ",")
		for i := range items {
			var name = items[i]
			r, err := createSecret(c, fa, flags, name, secretType, nil)
			printResult(name+" secret", r, err)
			if err == nil {
				count++
			}
		}
	case "secret-ssh-public-key":
		// if this is just a public key then the secret name is at the start of the string
		f := func(c rune) bool {
			return c == ',' || c == '[' || c == ']'
		}
		secrets := strings.FieldsFunc(secretDataIdentifiers, f)
		numOfSecrets := len(secrets)

		var keysNames []string
		if numOfSecrets > 0 {
			// if multiple secrets
			for i := 1; i < numOfSecrets; i++ {
				keysNames = append(keysNames, secrets[i])
			}
		} else {
			// only single secret required
			keysNames[0] = "ssh-key.pub"
		}

		r, err := createSecret(c, fa, flags, secrets[0], secretType, keysNames)

		printResult(secrets[0]+" secret", r, err)
		if err == nil {
			count++
		}

	default:
		gpgKeyName := []string{"gpg.conf", "secring.gpg", "pubring.gpg", "trustdb.gpg"}
		r, err := createSecret(c, fa, flags, secretDataIdentifiers, secretType, gpgKeyName)
		printResult(secretDataIdentifiers+" secret", r, err)
		if err == nil {
			count++
		}
	}
	return count
}

func secret(name string, secretType string, keysNames []string, flags *flag.FlagSet) api.Secret {
	return api.Secret{
		ObjectMeta: api.ObjectMeta{
			Name: name,
		},
		Type: api.SecretType(secretType),
		Data: getSecretData(secretType, name, keysNames, flags),
	}
}

func check(e error) {
	if e != nil {
		util.Warnf("Warning: %s\n", e)
	}
}

func logSecretImport(file string) {
	util.Infof("Importing secret: %s\n", file)
}

func getSecretData(secretType string, name string, keysNames []string, flags *flag.FlagSet) map[string][]byte {
	var dataType = strings.Split(secretType, "/")
	var data = make(map[string][]byte)

	switch dataType[1] {
	case "secret-ssh-key":
		if flags.Lookup("print-import-folder-structure").Value.String() == "true" {
			logSecretImport(name + "/ssh-key")
			logSecretImport(name + "/ssh-key.pub")
		}

		sshKey, err1 := ioutil.ReadFile(name + "/ssh-key")
		sshKeyPub, err2 := ioutil.ReadFile(name + "/ssh-key.pub")

		// if we cant find the public and private key to import, and generation flag is set then lets generate the keys
		if (err1 != nil && err2 != nil) && flags.Lookup("generate-secrets-data").Value.String() == "true" {
			util.Info("No secrets found on local filesystem, generating SSH public and private key pair\n")
			keypair := generateSshKeyPair()
			if flags.Lookup("write-generated-keys").Value.String() == "true" {
				writeFile(name+"/ssh-key", keypair.priv)
				writeFile(name+"/ssh-key.pub", keypair.pub)
			}
			data["ssh-key"] = keypair.priv
			data["ssh-key.pub"] = keypair.pub

		} else if (err1 != nil || err2 != nil) && flags.Lookup("generate-secrets-data").Value.String() == "true" {
			util.Infof("Found some keys to import but with errors so unable to generate SSH public and private key pair. %s\n", name)
			check(err1)
			check(err2)
		} else {
			// if we're not generating the keys and there's an error importing them then still create the secret but with empty data
			check(err1)
			check(err2)

			data["ssh-key"] = sshKey
			data["ssh-key.pub"] = sshKeyPub
		}
		return data

	case "secret-ssh-public-key":

		for i := 0; i < len(keysNames); i++ {
			if flags.Lookup("print-import-folder-structure").Value.String() == "true" {
				logSecretImport(name + "/" + keysNames[i])
			}

			sshPub, err := ioutil.ReadFile(name + "/" + keysNames[i])
			// if we cant find the public key to import and generation flag is set then lets generate the key
			if (err != nil) && flags.Lookup("generate-secrets-data").Value.String() == "true" {
				util.Info("No secrets found on local filesystem, generating SSH public key\n")
				keypair := generateSshKeyPair()
				if flags.Lookup("write-generated-keys").Value.String() == "true" {
					writeFile(name+"/ssh-key.pub", keypair.pub)
				}
				data[keysNames[i]] = keypair.pub

			} else {
				// if we're not generating the keys and there's an error importing them then still create the secret but with empty data
				check(err)
				data[keysNames[i]] = sshPub
			}
		}
		return data

	case "secret-gpg-key":
		for i := 0; i < len(keysNames); i++ {
			if flags.Lookup("print-import-folder-structure").Value.String() == "true" {
				logSecretImport(name + "/" + keysNames[i])
			}
			gpg, err := ioutil.ReadFile(name + "/" + keysNames[i])
			check(err)

			data[keysNames[i]] = gpg
		}

	case "secret-docker-cfg":

		if flags.Lookup("print-import-folder-structure").Value.String() == "true" {
			logSecretImport(name + "/config.json")
		}
		dockerCfg, err := ioutil.ReadFile(name + "/config.json")
		check(err)

		data["config.json"] = dockerCfg

	case "secret-maven-settings":

		if flags.Lookup("print-import-folder-structure").Value.String() == "true" {
			logSecretImport(name + "/settings.xml")
		}
		mvn, err := ioutil.ReadFile(name + "/settings.xml")
		check(err)
		if err != nil && flags.Lookup("generate-secrets-data").Value.String() == "true" {
			defaultSettingsXML := "https://raw.githubusercontent.com/fabric8io/gofabric8/master/default-secrets/mvnsettings.xml"
			logSecretImport("Using deafult maven settings from " + defaultSettingsXML)
			resp, err := http.Get(defaultSettingsXML)
			if err != nil {
				util.Fatalf("Cannot get fabric8 version to deploy: %v", err)
			}
			defer resp.Body.Close()
			// read xml http response
			mvn, err := ioutil.ReadAll(resp.Body)
			if err != nil {
				util.Fatalf("Cannot get fabric8 version to deploy: %v", err)
			}
			data["settings.xml"] = mvn
		} else {
			data["settings.xml"] = mvn
		}

		return data
	default:
		util.Fatalf("No matching data type %s\n", dataType)
	}
	return data
}

func generateSshKeyPair() Keypair {

	priv, err := rsa.GenerateKey(rand.Reader, 2014)
	if err != nil {
		util.Fatalf("Error generating key", err)
	}

	// Get der format. priv_der []byte
	priv_der := x509.MarshalPKCS1PrivateKey(priv)

	// pem.Block
	// blk pem.Block
	priv_blk := pem.Block{
		Type:    "RSA PRIVATE KEY",
		Headers: nil,
		Bytes:   priv_der,
	}

	// Resultant private key in PEM format.
	// priv_pem string
	priv_pem := string(pem.EncodeToMemory(&priv_blk))

	// Public Key generation
	sshPublicKey, err := ssh.NewPublicKey(&priv.PublicKey)
	pubBytes := ssh.MarshalAuthorizedKey(sshPublicKey)

	return Keypair{
		pub:  []byte(pubBytes),
		priv: []byte(priv_pem),
	}
}

func getTemplates(c *oclient.Client, ns string) *tapi.TemplateList {

	rc, err := c.Templates(ns).List(labels.Everything(), fields.Everything())
	if err != nil {
		util.Fatalf("No Templates found in namespace %s\n", ns)
	}
	return rc
}

func writeFile(path string, contents []byte) {
	dir := strings.Split(path, string(filepath.Separator))
	os.MkdirAll("."+string(filepath.Separator)+dir[0], 0700)
	f, err := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0600)
	if err != nil {
		panic(err)
	}
	defer f.Close()
	if _, err := f.Write(contents); err != nil {
		panic(err)
	}
}
<commit_msg>move jenkins github api key from env var to a secret see fabric8io/fabric8#5558<commit_after>/**
 * Copyright (C) 2015 Red Hat, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cmds

import (
	"crypto/rand"
	"crypto/rsa"
	"crypto/x509"
	"encoding/pem"
	"golang.org/x/crypto/ssh"
	"io/ioutil"
	"net/http"
	"os"
	"path/filepath"
	"strings"

	"github.com/fabric8io/gofabric8/client"
	"github.com/fabric8io/gofabric8/util"
	oclient "github.com/openshift/origin/pkg/client"
	tapi "github.com/openshift/origin/pkg/template/api"
	"github.com/spf13/cobra"
	flag "github.com/spf13/pflag"
	"k8s.io/kubernetes/pkg/api"
	k8sclient "k8s.io/kubernetes/pkg/client"
	"k8s.io/kubernetes/pkg/fields"
	cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
	"k8s.io/kubernetes/pkg/labels"
	"k8s.io/kubernetes/pkg/runtime"
)

type Keypair struct {
	pub  []byte
	priv []byte
}

func NewCmdSecrets(f *cmdutil.Factory) *cobra.Command {
	cmd := &cobra.Command{
		Use:   "secrets",
		Short: "Set up Secrets on your Kubernetes or OpenShift environment",
		Long:  `set up Secrets on your Kubernetes or OpenShift environment`,
		PreRun: func(cmd *cobra.Command, args []string) {
			showBanner()
		},
		Run: func(cmd *cobra.Command, args []string) {
			c, cfg := client.NewClient(f)
			ns, _, _ := f.DefaultNamespace()
			util.Info("Setting up secrets on your ")
			util.Success(string(util.TypeOfMaster(c)))
			util.Info(" installation at ")
			util.Success(cfg.Host)
			util.Info(" in namespace ")
			util.Successf("%s\n\n", ns)

			if confirmAction(cmd.Flags()) {
				typeOfMaster := util.TypeOfMaster(c)

				if typeOfMaster == util.Kubernetes {
					util.Fatal("Support for Kubernetes not yet available...\n")
				} else {
					oc, _ := client.NewOpenShiftClient(cfg)
					t := getTemplates(oc, ns)

					count := 0
					// get all the Templates and find the annotations on any Pods
					for _, i := range t.Items {
						// convert TemplateList.Objects to Kubernetes resources
						_ = runtime.DecodeList(i.Objects, api.Scheme, runtime.UnstructuredJSONScheme)
						for _, rc := range i.Objects {
							switch rc := rc.(type) {
							case *api.ReplicationController:
								for secretType, secretDataIdentifiers := range rc.Spec.Template.Annotations {
									count += createAndPrintSecrets(secretDataIdentifiers, secretType, c, f, cmd.Flags())
								}
							}
						}
					}

					if count == 0 {
						util.Info("No secrets created as no fabric8 secrets annotations found in the templates\n")
						util.Info("For more details see: https://github.com/fabric8io/fabric8/blob/master/docs/secretAnnotations.md\n")
					}
				}
			}
		},
	}
	cmd.PersistentFlags().BoolP("print-import-folder-structure", "", true, "Prints the folder structures that are being used by the template annotations to import secrets")
	cmd.PersistentFlags().BoolP("write-generated-keys", "", false, "Write generated secrets to the local filesystem")
	cmd.PersistentFlags().BoolP("generate-secrets-data", "g", true, "Generate secrets data if secrets cannot be found to import from the local filesystem")
	return cmd
}

func createSecret(c *k8sclient.Client, f *cmdutil.Factory, flags *flag.FlagSet, secretDataIdentifiers string, secretType string, keysNames []string) (Result, error) {
	var secret = secret(secretDataIdentifiers, secretType, keysNames, flags)
	ns, _, err := f.DefaultNamespace()
	if err != nil {
		return Failure, err
	}
	rs, err := c.Secrets(ns).Create(&secret)
	if rs != nil {
		return Success, err
	}
	return Failure, err
}

func createAndPrintSecrets(secretDataIdentifiers string, secretType string, c *k8sclient.Client, fa *cmdutil.Factory, flags *flag.FlagSet) int {
	count := 0
	// check to see if multiple public and private keys are needed
	var dataType = strings.Split(secretType, "/")
	switch dataType[1] {
	case "secret-ssh-key":
		items := strings.Split(secretDataIdentifiers, ",")
		for i := range items {
			var name = items[i]
			r, err := createSecret(c, fa, flags, name, secretType, nil)
			printResult(name+" secret", r, err)
			if err == nil {
				count++
			}
		}
	case "secret-ssh-public-key":
		// if this is just a public key then the secret name is at the start of the string
		f := func(c rune) bool {
			return c == ',' || c == '[' || c == ']'
		}
		secrets := strings.FieldsFunc(secretDataIdentifiers, f)
		numOfSecrets := len(secrets)

		var keysNames []string
		if numOfSecrets > 0 {
			// if multiple secrets
			for i := 1; i < numOfSecrets; i++ {
				keysNames = append(keysNames, secrets[i])
			}
		} else {
			// only single secret required
			keysNames[0] = "ssh-key.pub"
		}

		r, err := createSecret(c, fa, flags, secrets[0], secretType, keysNames)

		printResult(secrets[0]+" secret", r, err)
		if err == nil {
			count++
		}

	default:
		gpgKeyName := []string{"gpg.conf", "secring.gpg", "pubring.gpg", "trustdb.gpg"}
		r, err := createSecret(c, fa, flags, secretDataIdentifiers, secretType, gpgKeyName)
		printResult(secretDataIdentifiers+" secret", r, err)
		if err == nil {
			count++
		}
	}
	return count
}

func secret(name string, secretType string, keysNames []string, flags *flag.FlagSet) api.Secret {
	return api.Secret{
		ObjectMeta: api.ObjectMeta{
			Name: name,
		},
		Type: api.SecretType(secretType),
		Data: getSecretData(secretType, name, keysNames, flags),
	}
}

func check(e error) {
	if e != nil {
		util.Warnf("Warning: %s\n", e)
	}
}

func logSecretImport(file string) {
	util.Infof("Importing secret: %s\n", file)
}

func getSecretData(secretType string, name string, keysNames []string, flags *flag.FlagSet) map[string][]byte {
	var dataType = strings.Split(secretType, "/")
	var data = make(map[string][]byte)

	switch dataType[1] {
	case "secret-ssh-key":
		if flags.Lookup("print-import-folder-structure").Value.String() == "true" {
			logSecretImport(name + "/ssh-key")
			logSecretImport(name + "/ssh-key.pub")
		}

		sshKey, err1 := ioutil.ReadFile(name + "/ssh-key")
		sshKeyPub, err2 := ioutil.ReadFile(name + "/ssh-key.pub")

		// if we cant find the public and private key to import, and generation flag is set then lets generate the keys
		if (err1 != nil && err2 != nil) && flags.Lookup("generate-secrets-data").Value.String() == "true" {
			util.Info("No secrets found on local filesystem, generating SSH public and private key pair\n")
			keypair := generateSshKeyPair()
			if flags.Lookup("write-generated-keys").Value.String() == "true" {
				writeFile(name+"/ssh-key", keypair.priv)
				writeFile(name+"/ssh-key.pub", keypair.pub)
			}
			data["ssh-key"] = keypair.priv
			data["ssh-key.pub"] = keypair.pub

		} else if (err1 != nil || err2 != nil) && flags.Lookup("generate-secrets-data").Value.String() == "true" {
			util.Infof("Found some keys to import but with errors so unable to generate SSH public and private key pair. %s\n", name)
			check(err1)
			check(err2)
		} else {
			// if we're not generating the keys and there's an error importing them then still create the secret but with empty data
			check(err1)
			check(err2)

			data["ssh-key"] = sshKey
			data["ssh-key.pub"] = sshKeyPub
		}
		return data

	case "secret-ssh-public-key":

		for i := 0; i < len(keysNames); i++ {
			if flags.Lookup("print-import-folder-structure").Value.String() == "true" {
				logSecretImport(name + "/" + keysNames[i])
			}

			sshPub, err := ioutil.ReadFile(name + "/" + keysNames[i])
			// if we cant find the public key to import and generation flag is set then lets generate the key
			if (err != nil) && flags.Lookup("generate-secrets-data").Value.String() == "true" {
				util.Info("No secrets found on local filesystem, generating SSH public key\n")
				keypair := generateSshKeyPair()
				if flags.Lookup("write-generated-keys").Value.String() == "true" {
					writeFile(name+"/ssh-key.pub", keypair.pub)
				}
				data[keysNames[i]] = keypair.pub

			} else {
				// if we're not generating the keys and there's an error importing them then still create the secret but with empty data
				check(err)
				data[keysNames[i]] = sshPub
			}
		}
		return data

	case "secret-gpg-key":
		for i := 0; i < len(keysNames); i++ {
			if flags.Lookup("print-import-folder-structure").Value.String() == "true" {
				logSecretImport(name + "/" + keysNames[i])
			}
			gpg, err := ioutil.ReadFile(name + "/" + keysNames[i])
			check(err)

			data[keysNames[i]] = gpg
		}

	case "secret-github-api-token":

		if flags.Lookup("print-import-folder-structure").Value.String() == "true" {
			logSecretImport(name + "/apitoken")
		}
		apitoken, err := ioutil.ReadFile(name + "/apitoken")
		check(err)

		data["apitoken"] = apitoken

	case "secret-docker-cfg":

		if flags.Lookup("print-import-folder-structure").Value.String() == "true" {
			logSecretImport(name + "/config.json")
		}
		dockerCfg, err := ioutil.ReadFile(name + "/config.json")
		check(err)

		data["config.json"] = dockerCfg

	case "secret-maven-settings":

		if flags.Lookup("print-import-folder-structure").Value.String() == "true" {
			logSecretImport(name + "/settings.xml")
		}
		mvn, err := ioutil.ReadFile(name + "/settings.xml")
		check(err)
		if err != nil && flags.Lookup("generate-secrets-data").Value.String() == "true" {
			defaultSettingsXML := "https://raw.githubusercontent.com/fabric8io/gofabric8/master/default-secrets/mvnsettings.xml"
			logSecretImport("Using deafult maven settings from " + defaultSettingsXML)
			resp, err := http.Get(defaultSettingsXML)
			if err != nil {
				util.Fatalf("Cannot get fabric8 version to deploy: %v", err)
			}
			defer resp.Body.Close()
			// read xml http response
			mvn, err := ioutil.ReadAll(resp.Body)
			if err != nil {
				util.Fatalf("Cannot get fabric8 version to deploy: %v", err)
			}
			data["settings.xml"] = mvn
		} else {
			data["settings.xml"] = mvn
		}

		return data
	default:
		util.Fatalf("No matching data type %s\n", dataType)
	}
	return data
}

func generateSshKeyPair() Keypair {

	priv, err := rsa.GenerateKey(rand.Reader, 2014)
	if err != nil {
		util.Fatalf("Error generating key", err)
	}

	// Get der format. priv_der []byte
	priv_der := x509.MarshalPKCS1PrivateKey(priv)

	// pem.Block
	// blk pem.Block
	priv_blk := pem.Block{
		Type:    "RSA PRIVATE KEY",
		Headers: nil,
		Bytes:   priv_der,
	}

	// Resultant private key in PEM format.
	// priv_pem string
	priv_pem := string(pem.EncodeToMemory(&priv_blk))

	// Public Key generation
	sshPublicKey, err := ssh.NewPublicKey(&priv.PublicKey)
	pubBytes := ssh.MarshalAuthorizedKey(sshPublicKey)

	return Keypair{
		pub:  []byte(pubBytes),
		priv: []byte(priv_pem),
	}
}

func getTemplates(c *oclient.Client, ns string) *tapi.TemplateList {

	rc, err := c.Templates(ns).List(labels.Everything(), fields.Everything())
	if err != nil {
		util.Fatalf("No Templates found in namespace %s\n", ns)
	}
	return rc
}

func writeFile(path string, contents []byte) {
	dir := strings.Split(path, string(filepath.Separator))
	os.MkdirAll("."+string(filepath.Separator)+dir[0], 0700)
	f, err := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0600)
	if err != nil {
		panic(err)
	}
	defer f.Close()
	if _, err := f.Write(contents); err != nil {
		panic(err)
	}
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"

	"github.com/status-im/status-go/geth/params"
	"gopkg.in/urfave/cli.v1"
)

var (
	faucetCommand = cli.Command{
		Action: faucetCommandHandler,
		Name:   "faucet",
		Usage:  "Starts faucet node (light node used by faucet service to request Ether)",
		Flags: []cli.Flag{
			HTTPPortFlag,
		},
	}
)

// faucetCommandHandler handles `statusd faucet` command
func faucetCommandHandler(ctx *cli.Context) error {
	config, err := parseFaucetCommandConfig(ctx)
	if err != nil {
		return fmt.Errorf("can not parse config: %v", err)
	}

	fmt.Println("Starting Status Faucet node..")
	if err := statusAPI.StartNode(config); err != nil {
		return err
	}

	// wait till node has been stopped
	node, err := statusAPI.NodeManager().Node()
	if err != nil {
		return nil
	}
	node.Wait()

	return nil
}

// parseFaucetCommandConfig parses incoming CLI options and returns node configuration object
func parseFaucetCommandConfig(ctx *cli.Context) (*params.NodeConfig, error) {
	nodeConfig, err := makeNodeConfig(ctx)
	if err != nil {
		return nil, err
	}

	// select sub-protocols
	nodeConfig.LightEthConfig.Enabled = true
	nodeConfig.WhisperConfig.Enabled = false
	nodeConfig.SwarmConfig.Enabled = false

	// RPC configuration
	nodeConfig.APIModules = "eth"
	nodeConfig.HTTPHost = "0.0.0.0" // allow to connect from anywhere
	nodeConfig.HTTPPort = ctx.Int(HTTPPortFlag.Name)
	nodeConfig.RPCEnabled = ctx.Bool(HTTPEnabledFlag.Name)

	// extra options
	nodeConfig.BootClusterConfig.Enabled = true

	return nodeConfig, nil
}
<commit_msg>Fixed: faucet now accepts HTTPEnabledFlag<commit_after>package main

import (
	"fmt"

	"github.com/status-im/status-go/geth/params"
	"gopkg.in/urfave/cli.v1"
)

var (
	faucetCommand = cli.Command{
		Action: faucetCommandHandler,
		Name:   "faucet",
		Usage:  "Starts faucet node (light node used by faucet service to request Ether)",
		Flags: []cli.Flag{
			HTTPPortFlag,
			HTTPEnabledFlag,
		},
	}
)

// faucetCommandHandler handles `statusd faucet` command
func faucetCommandHandler(ctx *cli.Context) error {
	config, err := parseFaucetCommandConfig(ctx)
	if err != nil {
		return fmt.Errorf("can not parse config: %v", err)
	}

	fmt.Println("Starting Status Faucet node..")
	if err := statusAPI.StartNode(config); err != nil {
		return err
	}

	// wait till node has been stopped
	node, err := statusAPI.NodeManager().Node()
	if err != nil {
		return nil
	}
	node.Wait()

	return nil
}

// parseFaucetCommandConfig parses incoming CLI options and returns node configuration object
func parseFaucetCommandConfig(ctx *cli.Context) (*params.NodeConfig, error) {
	nodeConfig, err := makeNodeConfig(ctx)
	if err != nil {
		return nil, err
	}

	// select sub-protocols
	nodeConfig.LightEthConfig.Enabled = true
	nodeConfig.WhisperConfig.Enabled = false
	nodeConfig.SwarmConfig.Enabled = false

	// RPC configuration
	nodeConfig.APIModules = "eth"
	nodeConfig.HTTPHost = "0.0.0.0" // allow to connect from anywhere
	nodeConfig.HTTPPort = ctx.Int(HTTPPortFlag.Name)
	nodeConfig.RPCEnabled = ctx.Bool(HTTPEnabledFlag.Name)

	// extra options
	nodeConfig.BootClusterConfig.Enabled = true

	return nodeConfig, nil
}
<|endoftext|>


<commit_before>// Command waveform is a simple utility which reads an audio file from stdin,
// processes it into a waveform image using input flags, and writes a PNG image
// of the generated waveform to stdout.
package main

import (
	"flag"
	"image/color"
	"image/png"
	"log"
	"os"
	"strconv"

	"github.com/mdlayher/waveform"
)

const (
	// app is the name of this application
	app = "waveform"
)

var (
	// strBGColor is the hex color value used to color the background of the waveform image
	strBGColor = flag.String("bg", "#FFFFFF", "hex background color of output waveform image")

	// strFGColor is the hex color value used to color the foreground of the waveform image
	strFGColor = flag.String("fg", "#000000", "hex foreground color of output waveform image")

	// strAltColor is the hex color value used to set the alternate color of the waveform image
	strAltColor = flag.String("alt", "", "hex alternate color of output waveform image")

	// resolution is the number of times audio is read and the waveform is drawn,
	// per second of audio
	resolution = flag.Uint("resolution", 1, "number of times audio is read and drawn per second of audio")

	// scaleX is the scaling factor for the output waveform file's X-axis
	scaleX = flag.Uint("x", 1, "scaling factor for image X-axis")

	// scaleY is the scaling factor for the output waveform file's Y-axis
	scaleY = flag.Uint("y", 1, "scaling factor for image Y-axis")

	// sharpness is the factor used to add curvature to a scaled image, preventing
	// "blocky" images at higher scaling
	sharpness = flag.Uint("sharpness", 1, "sharpening factor used to add curvature to a scaled image")
)

func main() {
	// Parse flags
	flag.Parse()

	// Move all logging output to stderr, as output image will occupy
	// the stdout stream
	log.SetOutput(os.Stderr)
	log.SetPrefix(app + ": ")

	// Create image background color from input hex color string, or default
	// to black if invalid
	colorR, colorG, colorB := hexToRGB(*strBGColor)
	bgColor := color.RGBA{colorR, colorG, colorB, 255}

	// Create image foreground color from input hex color string, or default
	// to black if invalid
	colorR, colorG, colorB = hexToRGB(*strFGColor)
	fgColor := color.RGBA{colorR, colorG, colorB, 255}

	// Create image alternate color from input hex color string, or default
	// to foreground color if empty
	altColor := fgColor
	if *strAltColor != "" {
		colorR, colorG, colorB = hexToRGB(*strAltColor)
		altColor = color.RGBA{colorR, colorG, colorB, 255}
	}

	// Generate a waveform image from stdin, using values passed from
	// flags as options
	img, err := waveform.Generate(os.Stdin,
		waveform.Colors(fgColor, bgColor),
		waveform.ColorFunction(waveform.StripeColor(fgColor, altColor)),
		waveform.Resolution(*resolution),
		waveform.Scale(*scaleX, *scaleY),
		waveform.ScaleClipping(),
		waveform.Sharpness(*sharpness),
	)
	if err != nil {
		// Set of known errors
		knownErr := map[error]struct{}{
			waveform.ErrFormat:        struct{}{},
			waveform.ErrInvalidData:   struct{}{},
			waveform.ErrUnexpectedEOS: struct{}{},
		}

		// On known error, fatal log
		if _, ok := knownErr[err]; ok {
			log.Fatal(err)
		}

		// Unknown errors, panic
		panic(err)
	}

	// Encode results as PNG to stdout
	if err := png.Encode(os.Stdout, img); err != nil {
		panic(err)
	}
}

// hexToRGB converts a hex string to a RGB triple.
// Credit: https://code.google.com/p/gorilla/source/browse/color/hex.go?r=ef489f63418265a7249b1d53bdc358b09a4a2ea0
func hexToRGB(h string) (uint8, uint8, uint8) {
	if len(h) > 0 && h[0] == '#' {
		h = h[1:]
	}
	if len(h) == 3 {
		h = h[:1] + h[:1] + h[1:2] + h[1:2] + h[2:] + h[2:]
	}
	if len(h) == 6 {
		if rgb, err := strconv.ParseUint(string(h), 16, 32); err == nil {
			return uint8(rgb >> 16), uint8((rgb >> 8) & 0xFF), uint8(rgb & 0xFF)
		}
	}
	return 0, 0, 0
}
<commit_msg>cmd/waveform: add -fn flag for changing ColorFunc<commit_after>// Command waveform is a simple utility which reads an audio file from stdin,
// processes it into a waveform image using input flags, and writes a PNG image
// of the generated waveform to stdout.
package main

import (
	"flag"
	"fmt"
	"image/color"
	"image/png"
	"log"
	"os"
	"strconv"

	"github.com/mdlayher/waveform"
)

const (
	// app is the name of this application
	app = "waveform"

	// Names of available color functions
	fnFuzz   = "fuzz"
	fnSolid  = "solid"
	fnStripe = "stripe"
)

var (
	// strBGColor is the hex color value used to color the background of the waveform image
	strBGColor = flag.String("bg", "#FFFFFF", "hex background color of output waveform image")

	// strFGColor is the hex color value used to color the foreground of the waveform image
	strFGColor = flag.String("fg", "#000000", "hex foreground color of output waveform image")

	// strAltColor is the hex color value used to set the alternate color of the waveform image
	strAltColor = flag.String("alt", "", "hex alternate color of output waveform image")

	// resolution is the number of times audio is read and the waveform is drawn,
	// per second of audio
	resolution = flag.Uint("resolution", 1, "number of times audio is read and drawn per second of audio")

	// scaleX is the scaling factor for the output waveform file's X-axis
	scaleX = flag.Uint("x", 1, "scaling factor for image X-axis")

	// scaleY is the scaling factor for the output waveform file's Y-axis
	scaleY = flag.Uint("y", 1, "scaling factor for image Y-axis")

	// sharpness is the factor used to add curvature to a scaled image, preventing
	// "blocky" images at higher scaling
	sharpness = flag.Uint("sharpness", 1, "sharpening factor used to add curvature to a scaled image")

	// strFn is an identifier which selects the ColorFunc used to color the waveform image
	strFn = flag.String("fn", fnSolid, "function used to color output waveform image "+fnOptions)
)

// fnOptions is the help string which lists available options
var fnOptions = fmt.Sprintf("[options: %s, %s, %s]", fnFuzz, fnSolid, fnStripe)

func main() {
	// Parse flags
	flag.Parse()

	// Move all logging output to stderr, as output image will occupy
	// the stdout stream
	log.SetOutput(os.Stderr)
	log.SetPrefix(app + ": ")

	// Create image background color from input hex color string, or default
	// to black if invalid
	colorR, colorG, colorB := hexToRGB(*strBGColor)
	bgColor := color.RGBA{colorR, colorG, colorB, 255}

	// Create image foreground color from input hex color string, or default
	// to black if invalid
	colorR, colorG, colorB = hexToRGB(*strFGColor)
	fgColor := color.RGBA{colorR, colorG, colorB, 255}

	// Create image alternate color from input hex color string, or default
	// to foreground color if empty
	altColor := fgColor
	if *strAltColor != "" {
		colorR, colorG, colorB = hexToRGB(*strAltColor)
		altColor = color.RGBA{colorR, colorG, colorB, 255}
	}

	// Set of available functions
	fnSet := map[string]waveform.ColorFunc{
		fnFuzz:   waveform.FuzzColor(fgColor, altColor),
		fnSolid:  waveform.SolidColor,
		fnStripe: waveform.StripeColor(fgColor, altColor),
	}

	// Validate user-selected function
	colorFn, ok := fnSet[*strFn]
	if !ok {
		log.Fatalf("unknown function: %q %s", *strFn, fnOptions)
	}

	// Generate a waveform image from stdin, using values passed from
	// flags as options
	img, err := waveform.Generate(os.Stdin,
		waveform.Colors(fgColor, bgColor),
		waveform.ColorFunction(colorFn),
		waveform.Resolution(*resolution),
		waveform.Scale(*scaleX, *scaleY),
		waveform.ScaleClipping(),
		waveform.Sharpness(*sharpness),
	)
	if err != nil {
		// Set of known errors
		knownErr := map[error]struct{}{
			waveform.ErrFormat:        struct{}{},
			waveform.ErrInvalidData:   struct{}{},
			waveform.ErrUnexpectedEOS: struct{}{},
		}

		// On known error, fatal log
		if _, ok := knownErr[err]; ok {
			log.Fatal(err)
		}

		// Unknown errors, panic
		panic(err)
	}

	// Encode results as PNG to stdout
	if err := png.Encode(os.Stdout, img); err != nil {
		panic(err)
	}
}

// hexToRGB converts a hex string to a RGB triple.
// Credit: https://code.google.com/p/gorilla/source/browse/color/hex.go?r=ef489f63418265a7249b1d53bdc358b09a4a2ea0
func hexToRGB(h string) (uint8, uint8, uint8) {
	if len(h) > 0 && h[0] == '#' {
		h = h[1:]
	}
	if len(h) == 3 {
		h = h[:1] + h[:1] + h[1:2] + h[1:2] + h[2:] + h[2:]
	}
	if len(h) == 6 {
		if rgb, err := strconv.ParseUint(string(h), 16, 32); err == nil {
			return uint8(rgb >> 16), uint8((rgb >> 8) & 0xFF), uint8(rgb & 0xFF)
		}
	}
	return 0, 0, 0
}
<|endoftext|>


<commit_before>package commands

import (
	"bitbucket.org/kardianos/osext"
	"fmt"
	"github.com/codegangsta/cli"
	"io/ioutil"
	"os"
)

var BinstubsCmd = cli.Command{
	Name:  "binstubs",
	Usage: "Generate binstubs for the commands specified on devstep.yml",
	Action: func(c *cli.Context) {
		commands := project.Config().Commands

		if len(commands) == 0 {
			fmt.Println("No binstubs specified!")
			os.Exit(0)
		}

		binstubsPath := ".devstep/bin"
		os.MkdirAll("./"+binstubsPath, 0700)

		executable, _ := osext.Executable()

		for _, cmd := range commands {
			script := []byte("#!/usr/bin/env bash\neval \"" + executable + " run -- " + cmd.Name + " $@\"")
			err := ioutil.WriteFile(binstubsPath+"/"+cmd.Name, script, 0755)
			if err != nil {
				fmt.Printf("Error creating binstub '%s'\n%s\n", binstubsPath+"/"+cmd.Name, err)
				os.Exit(1)
			}
			fmt.Printf("Generated binstub for '%s' in '%s'\n", cmd.Name, binstubsPath)
		}
	},
}
<commit_msg>Fix osext import<commit_after>package commands

import (
	"github.com/kardianos/osext"
	"fmt"
	"github.com/codegangsta/cli"
	"io/ioutil"
	"os"
)

var BinstubsCmd = cli.Command{
	Name:  "binstubs",
	Usage: "Generate binstubs for the commands specified on devstep.yml",
	Action: func(c *cli.Context) {
		commands := project.Config().Commands

		if len(commands) == 0 {
			fmt.Println("No binstubs specified!")
			os.Exit(0)
		}

		binstubsPath := ".devstep/bin"
		os.MkdirAll("./"+binstubsPath, 0700)

		executable, _ := osext.Executable()

		for _, cmd := range commands {
			script := []byte("#!/usr/bin/env bash\neval \"" + executable + " run -- " + cmd.Name + " $@\"")
			err := ioutil.WriteFile(binstubsPath+"/"+cmd.Name, script, 0755)
			if err != nil {
				fmt.Printf("Error creating binstub '%s'\n%s\n", binstubsPath+"/"+cmd.Name, err)
				os.Exit(1)
			}
			fmt.Printf("Generated binstub for '%s' in '%s'\n", cmd.Name, binstubsPath)
		}
	},
}
<|endoftext|>


<commit_before>package commands

import (
	"errors"
	"io/ioutil"
	"os"
	"path/filepath"

	"github.com/github/git-lfs/lfs"
	"github.com/github/git-lfs/vendor/_nuts/github.com/spf13/cobra"
)

var (
	logsCmd = &cobra.Command{
		Use:   "logs",
		Short: "View error logs",
		Run:   logsCommand,
	}

	logsLastCmd = &cobra.Command{
		Use:   "last",
		Short: "View latest error log",
		Run:   logsLastCommand,
	}

	logsShowCmd = &cobra.Command{
		Use:   "show",
		Short: "View a single error log",
		Run:   logsShowCommand,
	}

	logsClearCmd = &cobra.Command{
		Use:   "clear",
		Short: "Clear all logs",
		Run:   logsClearCommand,
	}

	logsBoomtownCmd = &cobra.Command{
		Use:   "boomtown",
		Short: "Trigger a sample error",
		Run:   logsBoomtownCommand,
	}
)

func logsCommand(cmd *cobra.Command, args []string) {
	for _, path := range sortedLogs() {
		Print(path)
	}
}

func logsLastCommand(cmd *cobra.Command, args []string) {
	logs := sortedLogs()
	if len(logs) < 1 {
		Print("No logs to show")
		return
	}

	logsShowCommand(cmd, logs[len(logs)-1:])
}

func logsShowCommand(cmd *cobra.Command, args []string) {
	if len(args) == 0 {
		Print("Supply a log name.")
		return
	}

	name := args[0]
	by, err := ioutil.ReadFile(filepath.Join(lfs.LocalLogDir, name))
	if err != nil {
		Exit("Error reading log: %s", name)
	}

	Debug("Reading log: %s", name)
	os.Stdout.Write(by)
}

func logsClearCommand(cmd *cobra.Command, args []string) {
	err := os.RemoveAll(lfs.LocalLogDir)
	if err != nil {
		Panic(err, "Error clearing %s", lfs.LocalLogDir)
	}

	Print("Cleared %s", lfs.LocalLogDir)
}

func logsBoomtownCommand(cmd *cobra.Command, args []string) {
	Debug("Debug message")
	err := lfs.Errorf(errors.New("Inner error message!"), "Error!")
	Panic(err, "Welcome to Boomtown")
	Debug("Never seen")
}

func sortedLogs() []string {
	fileinfos, err := ioutil.ReadDir(lfs.LocalLogDir)
	if err != nil {
		return []string{}
	}

	names := make([]string, len(fileinfos))
	for index, info := range fileinfos {
		names[index] = info.Name()
	}

	return names
}

func init() {
	logsCmd.AddCommand(logsLastCmd, logsShowCmd, logsClearCmd, logsBoomtownCmd)
	RootCmd.AddCommand(logsCmd)
}
<commit_msg>ララララー ラララー ララ<commit_after>package commands

import (
	"errors"
	"io/ioutil"
	"os"
	"path/filepath"

	"github.com/github/git-lfs/lfs"
	"github.com/github/git-lfs/vendor/_nuts/github.com/spf13/cobra"
)

var (
	logsCmd = &cobra.Command{
		Use:   "logs",
		Short: "View error logs",
		Run:   logsCommand,
	}

	logsLastCmd = &cobra.Command{
		Use:   "last",
		Short: "View latest error log",
		Run:   logsLastCommand,
	}

	logsShowCmd = &cobra.Command{
		Use:   "show",
		Short: "View a single error log",
		Run:   logsShowCommand,
	}

	logsClearCmd = &cobra.Command{
		Use:   "clear",
		Short: "Clear all logs",
		Run:   logsClearCommand,
	}

	logsBoomtownCmd = &cobra.Command{
		Use:   "boomtown",
		Short: "Trigger a sample error",
		Run:   logsBoomtownCommand,
	}
)

func logsCommand(cmd *cobra.Command, args []string) {
	for _, path := range sortedLogs() {
		Print(path)
	}
}

func logsLastCommand(cmd *cobra.Command, args []string) {
	logs := sortedLogs()
	if len(logs) < 1 {
		Print("No logs to show")
		return
	}

	logsShowCommand(cmd, logs[len(logs)-1:])
}

func logsShowCommand(cmd *cobra.Command, args []string) {
	if len(args) == 0 {
		Print("Supply a log name.")
		return
	}

	name := args[0]
	by, err := ioutil.ReadFile(filepath.Join(lfs.LocalLogDir, name))
	if err != nil {
		Exit("Error reading log: %s", name)
	}

	Debug("Reading log: %s", name)
	os.Stdout.Write(by)
}

func logsClearCommand(cmd *cobra.Command, args []string) {
	err := os.RemoveAll(lfs.LocalLogDir)
	if err != nil {
		Panic(err, "Error clearing %s", lfs.LocalLogDir)
	}

	Print("Cleared %s", lfs.LocalLogDir)
}

func logsBoomtownCommand(cmd *cobra.Command, args []string) {
	Debug("Debug message")
	err := lfs.Errorf(errors.New("Inner error message!"), "Error!")
	Panic(err, "Welcome to Boomtown")
	Debug("Never seen")
}

func sortedLogs() []string {
	fileinfos, err := ioutil.ReadDir(lfs.LocalLogDir)
	if err != nil {
		return []string{}
	}

	names := make([]string, 0, len(fileinfos))
	for index, info := range fileinfos {
		if info.IsDir() {
			continue
		}
		names[index] = info.Name()
	}

	return names
}

func init() {
	logsCmd.AddCommand(logsLastCmd, logsShowCmd, logsClearCmd, logsBoomtownCmd)
	RootCmd.AddCommand(logsCmd)
}
<|endoftext|>


<commit_before>package commands

import (
	"os"

	"github.com/git-lfs/git-lfs/errors"
	"github.com/git-lfs/git-lfs/git"
	"github.com/git-lfs/git-lfs/lfs"
	"github.com/git-lfs/git-lfs/tq"
	"github.com/rubyist/tracerx"
	"github.com/spf13/cobra"
)

var (
	pushDryRun    = false
	pushObjectIDs = false
	pushAll       = false
	useStdin      = false

	// shares some global vars and functions with command_pre_push.go
)

// pushCommand pushes local objects to a Git LFS server.  It takes two
// arguments:
//
//   `<remote> <remote ref>`
//
// Remote must be a remote name, not a URL
//
// pushCommand calculates the git objects to send by comparing the range
// of commits between the local and remote git servers.
func pushCommand(cmd *cobra.Command, args []string) {
	if len(args) == 0 {
		Print("Specify a remote and a remote branch name (`git lfs push origin master`)")
		os.Exit(1)
	}

	requireGitVersion()

	// Remote is first arg
	if err := cfg.SetValidPushRemote(args[0]); err != nil {
		Exit("Invalid remote name %q: %s", args[0], err)
	}

	ctx := newUploadContext(pushDryRun)
	if pushObjectIDs {
		if len(args) < 2 {
			Print("Usage: git lfs push --object-id <remote> <lfs-object-id> [lfs-object-id] ...")
			return
		}

		uploadsWithObjectIDs(ctx, args[1:])
	} else {
		if len(args) < 1 {
			Print("Usage: git lfs push --dry-run <remote> [ref]")
			return
		}

		uploadsBetweenRefAndRemote(ctx, args[1:])
	}
}

func uploadsBetweenRefAndRemote(ctx *uploadContext, refnames []string) {
	tracerx.Printf("Upload refs %v to remote %v", refnames, ctx.Remote)

	updates, err := lfsPushRefs(refnames, pushAll)
	if err != nil {
		Error(err.Error())
		Exit("Error getting local refs.")
	}

	if err := uploadForRefUpdates(ctx, updates, pushAll); err != nil {
		ExitWithError(err)
	}
}

func uploadsWithObjectIDs(ctx *uploadContext, oids []string) {
	pointers := make([]*lfs.WrappedPointer, len(oids))
	for i, oid := range oids {
		mp, err := ctx.gitfilter.ObjectPath(oid)
		if err != nil {
			ExitWithError(errors.Wrap(err, "Unable to find local media path:"))
		}

		stat, err := os.Stat(mp)
		if err != nil {
			ExitWithError(errors.Wrap(err, "Unable to stat local media path"))
		}

		pointers[i] = &lfs.WrappedPointer{
			Name: mp,
			Pointer: &lfs.Pointer{
				Oid:  oid,
				Size: stat.Size(),
			},
		}
	}

	q := ctx.NewQueue(tq.RemoteRef(currentRemoteRef()))
	ctx.UploadPointers(q, pointers...)
	ctx.CollectErrors(q)
	ctx.ReportErrors()
}

// lfsPushRefs returns valid ref updates from the given ref and --all arguments.
// Either one or more refs can be explicitly specified, or --all indicates all
// local refs are pushed.
func lfsPushRefs(refnames []string, pushAll bool) ([]*git.RefUpdate, error) {
	localrefs, err := git.LocalRefs()
	if err != nil {
		return nil, err
	}

	if pushAll && len(refnames) == 0 {
		refs := make([]*git.RefUpdate, len(localrefs))
		for i, lr := range localrefs {
			refs[i] = git.NewRefUpdate(cfg.Git, cfg.PushRemote(), lr, nil)
		}
		return refs, nil
	}

	reflookup := make(map[string]*git.Ref, len(localrefs))
	for _, ref := range localrefs {
		reflookup[ref.Name] = ref
	}

	refs := make([]*git.RefUpdate, len(refnames))
	for i, name := range refnames {
		if left, ok := reflookup[name]; ok {
			refs[i] = git.NewRefUpdate(cfg.Git, cfg.PushRemote(), left, nil)
		} else {
			left := &git.Ref{Name: name, Type: git.RefTypeOther, Sha: name}
			refs[i] = git.NewRefUpdate(cfg.Git, cfg.PushRemote(), left, nil)
		}
	}

	return refs, nil
}

func init() {
	RegisterCommand("push", pushCommand, func(cmd *cobra.Command) {
		cmd.Flags().BoolVarP(&pushDryRun, "dry-run", "d", false, "Do everything except actually send the updates")
		cmd.Flags().BoolVarP(&pushObjectIDs, "object-id", "o", false, "Push LFS object ID(s)")
		cmd.Flags().BoolVarP(&pushAll, "all", "a", false, "Push all objects for the current ref to the remote.")
	})
}
<commit_msg>commands: change default branch name<commit_after>package commands

import (
	"os"

	"github.com/git-lfs/git-lfs/errors"
	"github.com/git-lfs/git-lfs/git"
	"github.com/git-lfs/git-lfs/lfs"
	"github.com/git-lfs/git-lfs/tq"
	"github.com/rubyist/tracerx"
	"github.com/spf13/cobra"
)

var (
	pushDryRun    = false
	pushObjectIDs = false
	pushAll       = false
	useStdin      = false

	// shares some global vars and functions with command_pre_push.go
)

// pushCommand pushes local objects to a Git LFS server.  It takes two
// arguments:
//
//   `<remote> <remote ref>`
//
// Remote must be a remote name, not a URL
//
// pushCommand calculates the git objects to send by comparing the range
// of commits between the local and remote git servers.
func pushCommand(cmd *cobra.Command, args []string) {
	if len(args) == 0 {
		Print("Specify a remote and a remote branch name (`git lfs push origin main`)")
		os.Exit(1)
	}

	requireGitVersion()

	// Remote is first arg
	if err := cfg.SetValidPushRemote(args[0]); err != nil {
		Exit("Invalid remote name %q: %s", args[0], err)
	}

	ctx := newUploadContext(pushDryRun)
	if pushObjectIDs {
		if len(args) < 2 {
			Print("Usage: git lfs push --object-id <remote> <lfs-object-id> [lfs-object-id] ...")
			return
		}

		uploadsWithObjectIDs(ctx, args[1:])
	} else {
		if len(args) < 1 {
			Print("Usage: git lfs push --dry-run <remote> [ref]")
			return
		}

		uploadsBetweenRefAndRemote(ctx, args[1:])
	}
}

func uploadsBetweenRefAndRemote(ctx *uploadContext, refnames []string) {
	tracerx.Printf("Upload refs %v to remote %v", refnames, ctx.Remote)

	updates, err := lfsPushRefs(refnames, pushAll)
	if err != nil {
		Error(err.Error())
		Exit("Error getting local refs.")
	}

	if err := uploadForRefUpdates(ctx, updates, pushAll); err != nil {
		ExitWithError(err)
	}
}

func uploadsWithObjectIDs(ctx *uploadContext, oids []string) {
	pointers := make([]*lfs.WrappedPointer, len(oids))
	for i, oid := range oids {
		mp, err := ctx.gitfilter.ObjectPath(oid)
		if err != nil {
			ExitWithError(errors.Wrap(err, "Unable to find local media path:"))
		}

		stat, err := os.Stat(mp)
		if err != nil {
			ExitWithError(errors.Wrap(err, "Unable to stat local media path"))
		}

		pointers[i] = &lfs.WrappedPointer{
			Name: mp,
			Pointer: &lfs.Pointer{
				Oid:  oid,
				Size: stat.Size(),
			},
		}
	}

	q := ctx.NewQueue(tq.RemoteRef(currentRemoteRef()))
	ctx.UploadPointers(q, pointers...)
	ctx.CollectErrors(q)
	ctx.ReportErrors()
}

// lfsPushRefs returns valid ref updates from the given ref and --all arguments.
// Either one or more refs can be explicitly specified, or --all indicates all
// local refs are pushed.
func lfsPushRefs(refnames []string, pushAll bool) ([]*git.RefUpdate, error) {
	localrefs, err := git.LocalRefs()
	if err != nil {
		return nil, err
	}

	if pushAll && len(refnames) == 0 {
		refs := make([]*git.RefUpdate, len(localrefs))
		for i, lr := range localrefs {
			refs[i] = git.NewRefUpdate(cfg.Git, cfg.PushRemote(), lr, nil)
		}
		return refs, nil
	}

	reflookup := make(map[string]*git.Ref, len(localrefs))
	for _, ref := range localrefs {
		reflookup[ref.Name] = ref
	}

	refs := make([]*git.RefUpdate, len(refnames))
	for i, name := range refnames {
		if left, ok := reflookup[name]; ok {
			refs[i] = git.NewRefUpdate(cfg.Git, cfg.PushRemote(), left, nil)
		} else {
			left := &git.Ref{Name: name, Type: git.RefTypeOther, Sha: name}
			refs[i] = git.NewRefUpdate(cfg.Git, cfg.PushRemote(), left, nil)
		}
	}

	return refs, nil
}

func init() {
	RegisterCommand("push", pushCommand, func(cmd *cobra.Command) {
		cmd.Flags().BoolVarP(&pushDryRun, "dry-run", "d", false, "Do everything except actually send the updates")
		cmd.Flags().BoolVarP(&pushObjectIDs, "object-id", "o", false, "Push LFS object ID(s)")
		cmd.Flags().BoolVarP(&pushAll, "all", "a", false, "Push all objects for the current ref to the remote.")
	})
}
<|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 binary_test

import (
	"bytes"
	"encoding/binary"
	"fmt"
	"math"
)

func ExampleWrite() {
	buf := new(bytes.Buffer)
	var pi float64 = math.Pi
	err := binary.Write(buf, binary.LittleEndian, pi)
	if err != nil {
		fmt.Println("binary.Write failed:", err)
	}
	fmt.Printf("% x", buf.Bytes())
	// Output: 18 2d 44 54 fb 21 09 40
}

func ExampleWrite_multi() {
	buf := new(bytes.Buffer)
	var data = []interface{}{
		uint16(61374),
		int8(-54),
		uint8(254),
	}
	for _, v := range data {
		err := binary.Write(buf, binary.LittleEndian, v)
		if err != nil {
			fmt.Println("binary.Write failed:", err)
		}
	}
	fmt.Printf("%x", buf.Bytes())
	// Output: beefcafe
}

func ExampleRead() {
	var pi float64
	b := []byte{0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40}
	buf := bytes.NewBuffer(b)
	err := binary.Read(buf, binary.LittleEndian, &pi)
	if err != nil {
		fmt.Println("binary.Read failed:", err)
	}
	fmt.Print(pi)
	// Output: 3.141592653589793
}
<commit_msg>encoding/binary: use bytes.Reader in read 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 binary_test

import (
	"bytes"
	"encoding/binary"
	"fmt"
	"math"
)

func ExampleWrite() {
	buf := new(bytes.Buffer)
	var pi float64 = math.Pi
	err := binary.Write(buf, binary.LittleEndian, pi)
	if err != nil {
		fmt.Println("binary.Write failed:", err)
	}
	fmt.Printf("% x", buf.Bytes())
	// Output: 18 2d 44 54 fb 21 09 40
}

func ExampleWrite_multi() {
	buf := new(bytes.Buffer)
	var data = []interface{}{
		uint16(61374),
		int8(-54),
		uint8(254),
	}
	for _, v := range data {
		err := binary.Write(buf, binary.LittleEndian, v)
		if err != nil {
			fmt.Println("binary.Write failed:", err)
		}
	}
	fmt.Printf("%x", buf.Bytes())
	// Output: beefcafe
}

func ExampleRead() {
	var pi float64
	b := []byte{0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40}
	buf := bytes.NewReader(b)
	err := binary.Read(buf, binary.LittleEndian, &pi)
	if err != nil {
		fmt.Println("binary.Read failed:", err)
	}
	fmt.Print(pi)
	// Output: 3.141592653589793
}
<|endoftext|>


<commit_before>// Copyright 2016 The Gosl 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 plt

import (
	"math"
	"testing"

	"github.com/cpmech/gosl/chk"
	"github.com/cpmech/gosl/utl"
)

func Test_args01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("args01")

	var a A

	// plot and basic options
	a.C = "red"
	a.M = "o"
	a.Ls = "--"
	a.Lw = 1.2
	a.Ms = -1
	a.L = "gosl"
	a.Me = 2
	a.Z = 123
	a.Mec = "blue"
	a.Mew = 0.3
	a.Void = true
	a.NoClip = true

	// shapes
	a.Ha = "center"
	a.Va = "center"
	a.Fc = "magenta"
	a.Ec = "yellow"

	// text and extra arguments
	a.Fsz = 7

	l := a.String(false)
	chk.String(tst, l, "color='red',marker='o',ls='--',lw=1.2,label='gosl',markevery=2,zorder=123,markeredgecolor='blue',mew=0.3,markerfacecolor='none',clip_on=0,facecolor='magenta',edgecolor='yellow',ha='center',va='center',fontsize=7")
}

func Test_args02(tst *testing.T) {

	//verbose()
	chk.PrintTitle("args02")

	a := &A{
		Colors:  []string{"red", "tan", "lime"},
		Type:    "bar",
		Stacked: true,
		NoFill:  true,
		Nbins:   10,
		Normed:  true,
	}

	l := a.String(true)
	chk.String(tst, l, "color=['red','tan','lime'],histtype='bar',stacked=1,fill=0,bins=10,normed=1")
}

func Test_nlevels01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("nlevels01")

	nlevels := 3
	Z := [][]float64{
		{0, 1}, {0, 1},
		{0, 1}, {0, 1},
	}

	l := getContourLevels(nlevels, Z)
	chk.String(tst, l, "[0,0.5,1]")
}

func Test_plot01(tst *testing.T) {

	verbose()
	chk.PrintTitle("plot01")

	if chk.Verbose {

		x := utl.LinSpace(0.0, 1.0, 11)
		y := make([]float64, len(x))
		for i := 0; i < len(x); i++ {
			y[i] = x[i] * x[i]
		}

		Reset(false, nil)
		SetFontSizes(&A{Fsz: 20, FszLbl: 20, FszXtck: 10, FszYtck: 10})
		Plot(x, y, &A{L: "first", C: "r", M: "o", Ls: "-", Lw: 2, NoClip: true})
		Plot(y, x, &A{L: "second", C: "b", M: ".", Ls: ":", Lw: 40})
		Text(0.2, 0.8, "HERE", &A{Fsz: 20, Ha: "center", Va: "center"})
		SetTicksX(0.1, 0.01, "%.3f")
		SetTicksY(0.2, 0.1, "%.2f")
		HideBorders(&A{HideR: true, HideT: true})
		Gll(`$\varepsilon$`, `$\sigma$`, &A{
			LegOut:  true,
			LegNcol: 3,
			FszLeg:  14,
			HideR:   true,
		})

		err := Save("/tmp/gosl", "t_plot01")
		if err != nil {
			tst.Errorf("%v", err)
		}
	}
}

func Test_plot02(tst *testing.T) {

	//verbose()
	chk.PrintTitle("plot02")

	if chk.Verbose {

		Reset(true, &A{Eps: true, WidthPt: 380})
		ReplaceAxes(0, 0, 1, 1, 0.04, 0.04, "the x", "the y", &A{Style: "->"}, &A{})
		Arrow(0, 0, 1, 1, &A{C: "orange"})
		AxHline(0, &A{C: "red"})
		AxVline(0, &A{C: "blue"})
		Annotate(0, 0, "TEST", &A{C: "green", FigFraction: true})
		AnnotateXlabels(0, "HERE", &A{Fsz: 10})
		SupTitle("suptitle goes here", &A{C: "red"})
		Title("title goes here", &A{C: "cyan"})
		Text(0.5, 0.5, "TEXT", &A{C: "orange", Va: "top"})
		Cross(0.5, 0.5, nil)
		PlotOne(0, 0, &A{M: "*"})

		err := Save("/tmp/gosl", "t_plot02")
		if err != nil {
			tst.Errorf("%v", err)
		}
	}
}

func Test_plot03(tst *testing.T) {

	//verbose()
	chk.PrintTitle("plot03")

	if chk.Verbose {

		// grid size
		xmin, xmax, N := -math.Pi/2.0+0.1, math.Pi/2.0-0.1, 21

		// mesh grid
		X, Y, F := utl.MeshGrid2dF(xmin, xmax, xmin, xmax, N, N, func(x, y float64) (z float64) {
			m := math.Pow(math.Cos(x), 2.0) + math.Pow(math.Cos(y), 2.0)
			z = -math.Pow(m, 2.0)
			return
		})

		// configuration
		a := &A{
			NumFmt:  "%.1f",
			Lw:      0.8,
			CbarLbl: "NICE",
			SelectC: "yellow",
			SelectV: -2.5,
			Nlevels: 10,
		}

		Reset(true, nil)
		Equal()
		ContourF(X, Y, F, a)

		err := Save("/tmp/gosl", "t_plot03")
		if err != nil {
			tst.Errorf("%v", err)
		}
	}
}

func Test_plot04(tst *testing.T) {

	//verbose()
	chk.PrintTitle("plot04")

	if chk.Verbose {

		// grid size
		xmin, xmax, N := -math.Pi/2.0+0.1, math.Pi/2.0-0.1, 21

		// mesh grid
		X, Y, F, U, V := utl.MeshGrid2dFG(xmin, xmax, xmin, xmax, N, N, func(x, y float64) (z, u, v float64) {
			m := math.Pow(math.Cos(x), 2.0) + math.Pow(math.Cos(y), 2.0)
			z = -math.Pow(m, 2.0)
			u = 4.0 * math.Cos(x) * math.Sin(x) * m
			v = 4.0 * math.Cos(y) * math.Sin(y) * m
			return
		})

		// configuration
		a := &A{
			Colors:   []string{"cyan", "blue", "yellow", "green"},
			Levels:   []float64{-4, -3, -2, -1, 0},
			NumFmt:   "%.1f",
			NoLines:  true,
			NoLabels: true,
			NoInline: true,
			NoCbar:   true,
			Lw:       1.5,
			SelectC:  "white",
			SelectV:  -2.5,
		}

		b := &A{
			CmapIdx: 4,
			SelectC: "black",
			SelectV: -2.5,
		}

		Reset(true, nil)
		Equal()
		ContourF(X, Y, F, a)
		ContourL(X, Y, F, b)
		Quiver(X, Y, U, V, nil)

		err := Save("/tmp/gosl", "t_plot04")
		if err != nil {
			tst.Errorf("%v", err)
		}
	}
}

func Test_plot05(tst *testing.T) {

	//verbose()
	chk.PrintTitle("plot05")

	if chk.Verbose {

		X := [][]float64{
			{1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 4, 5, 6}, // first series
			{-1, -1, 0, 1, 2, 3},                    // second series
			{5, 6, 7, 8},                            // third series
		}

		L := []string{
			"first",
			"second",
			"third",
		}

		a := &A{
			Colors:  []string{"red", "tan", "lime"},
			Ec:      "black",
			Lw:      0.5,
			Type:    "bar",
			Stacked: true,
		}

		Reset(true, nil)
		Hist(X, L, a)
		Gll("series", "count", nil)

		err := Save("/tmp/gosl", "t_plot05")
		if err != nil {
			tst.Errorf("%v", err)
		}
	}
}

func Test_plot06(tst *testing.T) {

	//verbose()
	chk.PrintTitle("plot06")

	if chk.Verbose {

		x := []float64{0, 1, 1, 1}
		y := []float64{0, 0, 1, 1}
		z := []float64{0, 0, 0, 1}

		np := 3

		X, Y, Z := utl.MeshGrid2dF(0, 1, 0, 1, np, np, func(x, y float64) float64 {
			return x + y
		})

		U, V, W := utl.MeshGrid2dF(0, 1, 0, 1, np, np, func(u, v float64) float64 {
			return u*u + v*v
		})

		Reset(true, nil)
		Plot3dLine(x, y, z, true, nil)
		Plot3dPoints(x, y, z, false, nil)
		Wireframe(X, Y, Z, false, nil)
		Surface(U, V, W, false, nil)
		//Camera(elev, azim float64, args *A)

		err := Save("/tmp/gosl", "t_plot06")
		if err != nil {
			tst.Errorf("%v", err)
		}
	}
}
<commit_msg>Hide test messages<commit_after>// Copyright 2016 The Gosl 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 plt

import (
	"math"
	"testing"

	"github.com/cpmech/gosl/chk"
	"github.com/cpmech/gosl/utl"
)

func Test_args01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("args01")

	var a A

	// plot and basic options
	a.C = "red"
	a.M = "o"
	a.Ls = "--"
	a.Lw = 1.2
	a.Ms = -1
	a.L = "gosl"
	a.Me = 2
	a.Z = 123
	a.Mec = "blue"
	a.Mew = 0.3
	a.Void = true
	a.NoClip = true

	// shapes
	a.Ha = "center"
	a.Va = "center"
	a.Fc = "magenta"
	a.Ec = "yellow"

	// text and extra arguments
	a.Fsz = 7

	l := a.String(false)
	chk.String(tst, l, "color='red',marker='o',ls='--',lw=1.2,label='gosl',markevery=2,zorder=123,markeredgecolor='blue',mew=0.3,markerfacecolor='none',clip_on=0,facecolor='magenta',edgecolor='yellow',ha='center',va='center',fontsize=7")
}

func Test_args02(tst *testing.T) {

	//verbose()
	chk.PrintTitle("args02")

	a := &A{
		Colors:  []string{"red", "tan", "lime"},
		Type:    "bar",
		Stacked: true,
		NoFill:  true,
		Nbins:   10,
		Normed:  true,
	}

	l := a.String(true)
	chk.String(tst, l, "color=['red','tan','lime'],histtype='bar',stacked=1,fill=0,bins=10,normed=1")
}

func Test_nlevels01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("nlevels01")

	nlevels := 3
	Z := [][]float64{
		{0, 1}, {0, 1},
		{0, 1}, {0, 1},
	}

	l := getContourLevels(nlevels, Z)
	chk.String(tst, l, "[0,0.5,1]")
}

func Test_plot01(tst *testing.T) {

	//verbose()
	chk.PrintTitle("plot01")

	if chk.Verbose {

		x := utl.LinSpace(0.0, 1.0, 11)
		y := make([]float64, len(x))
		for i := 0; i < len(x); i++ {
			y[i] = x[i] * x[i]
		}

		Reset(false, nil)
		SetFontSizes(&A{Fsz: 20, FszLbl: 20, FszXtck: 10, FszYtck: 10})
		Plot(x, y, &A{L: "first", C: "r", M: "o", Ls: "-", Lw: 2, NoClip: true})
		Plot(y, x, &A{L: "second", C: "b", M: ".", Ls: ":", Lw: 40})
		Text(0.2, 0.8, "HERE", &A{Fsz: 20, Ha: "center", Va: "center"})
		SetTicksX(0.1, 0.01, "%.3f")
		SetTicksY(0.2, 0.1, "%.2f")
		HideBorders(&A{HideR: true, HideT: true})
		Gll(`$\varepsilon$`, `$\sigma$`, &A{
			LegOut:  true,
			LegNcol: 3,
			FszLeg:  14,
			HideR:   true,
		})

		err := Save("/tmp/gosl", "t_plot01")
		if err != nil {
			tst.Errorf("%v", err)
		}
	}
}

func Test_plot02(tst *testing.T) {

	//verbose()
	chk.PrintTitle("plot02")

	if chk.Verbose {

		Reset(true, &A{Eps: true, WidthPt: 380})
		ReplaceAxes(0, 0, 1, 1, 0.04, 0.04, "the x", "the y", &A{Style: "->"}, &A{})
		Arrow(0, 0, 1, 1, &A{C: "orange"})
		AxHline(0, &A{C: "red"})
		AxVline(0, &A{C: "blue"})
		Annotate(0, 0, "TEST", &A{C: "green", FigFraction: true})
		AnnotateXlabels(0, "HERE", &A{Fsz: 10})
		SupTitle("suptitle goes here", &A{C: "red"})
		Title("title goes here", &A{C: "cyan"})
		Text(0.5, 0.5, "TEXT", &A{C: "orange", Va: "top"})
		Cross(0.5, 0.5, nil)
		PlotOne(0, 0, &A{M: "*"})

		err := Save("/tmp/gosl", "t_plot02")
		if err != nil {
			tst.Errorf("%v", err)
		}
	}
}

func Test_plot03(tst *testing.T) {

	//verbose()
	chk.PrintTitle("plot03")

	if chk.Verbose {

		// grid size
		xmin, xmax, N := -math.Pi/2.0+0.1, math.Pi/2.0-0.1, 21

		// mesh grid
		X, Y, F := utl.MeshGrid2dF(xmin, xmax, xmin, xmax, N, N, func(x, y float64) (z float64) {
			m := math.Pow(math.Cos(x), 2.0) + math.Pow(math.Cos(y), 2.0)
			z = -math.Pow(m, 2.0)
			return
		})

		// configuration
		a := &A{
			NumFmt:  "%.1f",
			Lw:      0.8,
			CbarLbl: "NICE",
			SelectC: "yellow",
			SelectV: -2.5,
			Nlevels: 10,
		}

		Reset(true, nil)
		Equal()
		ContourF(X, Y, F, a)

		err := Save("/tmp/gosl", "t_plot03")
		if err != nil {
			tst.Errorf("%v", err)
		}
	}
}

func Test_plot04(tst *testing.T) {

	//verbose()
	chk.PrintTitle("plot04")

	if chk.Verbose {

		// grid size
		xmin, xmax, N := -math.Pi/2.0+0.1, math.Pi/2.0-0.1, 21

		// mesh grid
		X, Y, F, U, V := utl.MeshGrid2dFG(xmin, xmax, xmin, xmax, N, N, func(x, y float64) (z, u, v float64) {
			m := math.Pow(math.Cos(x), 2.0) + math.Pow(math.Cos(y), 2.0)
			z = -math.Pow(m, 2.0)
			u = 4.0 * math.Cos(x) * math.Sin(x) * m
			v = 4.0 * math.Cos(y) * math.Sin(y) * m
			return
		})

		// configuration
		a := &A{
			Colors:   []string{"cyan", "blue", "yellow", "green"},
			Levels:   []float64{-4, -3, -2, -1, 0},
			NumFmt:   "%.1f",
			NoLines:  true,
			NoLabels: true,
			NoInline: true,
			NoCbar:   true,
			Lw:       1.5,
			SelectC:  "white",
			SelectV:  -2.5,
		}

		b := &A{
			CmapIdx: 4,
			SelectC: "black",
			SelectV: -2.5,
		}

		Reset(true, nil)
		Equal()
		ContourF(X, Y, F, a)
		ContourL(X, Y, F, b)
		Quiver(X, Y, U, V, nil)

		err := Save("/tmp/gosl", "t_plot04")
		if err != nil {
			tst.Errorf("%v", err)
		}
	}
}

func Test_plot05(tst *testing.T) {

	//verbose()
	chk.PrintTitle("plot05")

	if chk.Verbose {

		X := [][]float64{
			{1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 4, 5, 6}, // first series
			{-1, -1, 0, 1, 2, 3},                    // second series
			{5, 6, 7, 8},                            // third series
		}

		L := []string{
			"first",
			"second",
			"third",
		}

		a := &A{
			Colors:  []string{"red", "tan", "lime"},
			Ec:      "black",
			Lw:      0.5,
			Type:    "bar",
			Stacked: true,
		}

		Reset(true, nil)
		Hist(X, L, a)
		Gll("series", "count", nil)

		err := Save("/tmp/gosl", "t_plot05")
		if err != nil {
			tst.Errorf("%v", err)
		}
	}
}

func Test_plot06(tst *testing.T) {

	//verbose()
	chk.PrintTitle("plot06")

	if chk.Verbose {

		x := []float64{0, 1, 1, 1}
		y := []float64{0, 0, 1, 1}
		z := []float64{0, 0, 0, 1}

		np := 3

		X, Y, Z := utl.MeshGrid2dF(0, 1, 0, 1, np, np, func(x, y float64) float64 {
			return x + y
		})

		U, V, W := utl.MeshGrid2dF(0, 1, 0, 1, np, np, func(u, v float64) float64 {
			return u*u + v*v
		})

		Reset(true, nil)
		Plot3dLine(x, y, z, true, nil)
		Plot3dPoints(x, y, z, false, nil)
		Wireframe(X, Y, Z, false, nil)
		Surface(U, V, W, false, nil)
		//Camera(elev, azim float64, args *A)

		err := Save("/tmp/gosl", "t_plot06")
		if err != nil {
			tst.Errorf("%v", err)
		}
	}
}
<|endoftext|>


<commit_before><commit_msg>i/config: adjust the description<commit_after><|endoftext|>